xref: /openbmc/linux/sound/sh/aica.c (revision 46eeaa11bdd1bc9e077bdf741d32ca7235d263c6)
1f3d080a2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2198de43dSAdrian McMenamin /*
3198de43dSAdrian McMenamin *
4198de43dSAdrian McMenamin * Copyright Adrian McMenamin 2005, 2006, 2007
5198de43dSAdrian McMenamin * <adrian@mcmen.demon.co.uk>
6198de43dSAdrian McMenamin * Requires firmware (BSD licenced) available from:
7198de43dSAdrian McMenamin * http://linuxdc.cvs.sourceforge.net/linuxdc/linux-sh-dc/sound/oss/aica/firmware/
8198de43dSAdrian McMenamin * or the maintainer
9198de43dSAdrian McMenamin */
10198de43dSAdrian McMenamin 
11198de43dSAdrian McMenamin #include <linux/init.h>
12198de43dSAdrian McMenamin #include <linux/jiffies.h>
13198de43dSAdrian McMenamin #include <linux/slab.h>
14198de43dSAdrian McMenamin #include <linux/time.h>
15198de43dSAdrian McMenamin #include <linux/wait.h>
1665a77217SPaul Gortmaker #include <linux/module.h>
17198de43dSAdrian McMenamin #include <linux/platform_device.h>
18198de43dSAdrian McMenamin #include <linux/firmware.h>
19198de43dSAdrian McMenamin #include <linux/timer.h>
20198de43dSAdrian McMenamin #include <linux/delay.h>
21198de43dSAdrian McMenamin #include <linux/workqueue.h>
226cbbfe1cSTakashi Iwai #include <linux/io.h>
23198de43dSAdrian McMenamin #include <sound/core.h>
24198de43dSAdrian McMenamin #include <sound/control.h>
25198de43dSAdrian McMenamin #include <sound/pcm.h>
26198de43dSAdrian McMenamin #include <sound/initval.h>
27198de43dSAdrian McMenamin #include <sound/info.h>
28198de43dSAdrian McMenamin #include <asm/dma.h>
29f15cbe6fSPaul Mundt #include <mach/sysasic.h>
30198de43dSAdrian McMenamin #include "aica.h"
31198de43dSAdrian McMenamin 
32198de43dSAdrian McMenamin MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
33198de43dSAdrian McMenamin MODULE_DESCRIPTION("Dreamcast AICA sound (pcm) driver");
34198de43dSAdrian McMenamin MODULE_LICENSE("GPL");
35f37325a9SBen Hutchings MODULE_FIRMWARE("aica_firmware.bin");
36198de43dSAdrian McMenamin 
37198de43dSAdrian McMenamin /* module parameters */
38198de43dSAdrian McMenamin #define CARD_NAME "AICA"
39198de43dSAdrian McMenamin static int index = -1;
40198de43dSAdrian McMenamin static char *id;
41a67ff6a5SRusty Russell static bool enable = 1;
42198de43dSAdrian McMenamin module_param(index, int, 0444);
43198de43dSAdrian McMenamin MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
44198de43dSAdrian McMenamin module_param(id, charp, 0444);
45198de43dSAdrian McMenamin MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
46198de43dSAdrian McMenamin module_param(enable, bool, 0644);
47198de43dSAdrian McMenamin MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
48198de43dSAdrian McMenamin 
49198de43dSAdrian McMenamin /* Simple platform device */
50198de43dSAdrian McMenamin static struct platform_device *pd;
51198de43dSAdrian McMenamin static struct resource aica_memory_space[2] = {
52198de43dSAdrian McMenamin 	{
53198de43dSAdrian McMenamin 	 .name = "AICA ARM CONTROL",
54198de43dSAdrian McMenamin 	 .start = ARM_RESET_REGISTER,
55198de43dSAdrian McMenamin 	 .flags = IORESOURCE_MEM,
56198de43dSAdrian McMenamin 	 .end = ARM_RESET_REGISTER + 3,
57198de43dSAdrian McMenamin 	 },
58198de43dSAdrian McMenamin 	{
59198de43dSAdrian McMenamin 	 .name = "AICA Sound RAM",
60198de43dSAdrian McMenamin 	 .start = SPU_MEMORY_BASE,
61198de43dSAdrian McMenamin 	 .flags = IORESOURCE_MEM,
62198de43dSAdrian McMenamin 	 .end = SPU_MEMORY_BASE + 0x200000 - 1,
63198de43dSAdrian McMenamin 	 },
64198de43dSAdrian McMenamin };
65198de43dSAdrian McMenamin 
66198de43dSAdrian McMenamin /* SPU specific functions */
67198de43dSAdrian McMenamin /* spu_write_wait - wait for G2-SH FIFO to clear */
spu_write_wait(void)68198de43dSAdrian McMenamin static void spu_write_wait(void)
69198de43dSAdrian McMenamin {
70198de43dSAdrian McMenamin 	int time_count;
71198de43dSAdrian McMenamin 	time_count = 0;
72198de43dSAdrian McMenamin 	while (1) {
73198de43dSAdrian McMenamin 		if (!(readl(G2_FIFO) & 0x11))
74198de43dSAdrian McMenamin 			break;
75198de43dSAdrian McMenamin 		/* To ensure hardware failure doesn't wedge kernel */
76198de43dSAdrian McMenamin 		time_count++;
77d20b9b20STakashi Iwai 		if (time_count > 0x10000) {
78d20b9b20STakashi Iwai 			snd_printk
79d20b9b20STakashi Iwai 			    ("WARNING: G2 FIFO appears to be blocked.\n");
80198de43dSAdrian McMenamin 			break;
81198de43dSAdrian McMenamin 		}
82198de43dSAdrian McMenamin 	}
83198de43dSAdrian McMenamin }
84198de43dSAdrian McMenamin 
85198de43dSAdrian McMenamin /* spu_memset - write to memory in SPU address space */
spu_memset(u32 toi,u32 what,int length)86198de43dSAdrian McMenamin static void spu_memset(u32 toi, u32 what, int length)
87198de43dSAdrian McMenamin {
88198de43dSAdrian McMenamin 	int i;
8944e0b682SAdrian McMenamin 	unsigned long flags;
905e246b85STakashi Iwai 	if (snd_BUG_ON(length % 4))
915e246b85STakashi Iwai 		return;
92198de43dSAdrian McMenamin 	for (i = 0; i < length; i++) {
93198de43dSAdrian McMenamin 		if (!(i % 8))
94198de43dSAdrian McMenamin 			spu_write_wait();
9544e0b682SAdrian McMenamin 		local_irq_save(flags);
96198de43dSAdrian McMenamin 		writel(what, toi + SPU_MEMORY_BASE);
9744e0b682SAdrian McMenamin 		local_irq_restore(flags);
98198de43dSAdrian McMenamin 		toi++;
99198de43dSAdrian McMenamin 	}
100198de43dSAdrian McMenamin }
101198de43dSAdrian McMenamin 
102198de43dSAdrian McMenamin /* spu_memload - write to SPU address space */
spu_memload(u32 toi,const void * from,int length)103f1dd4795STakashi Iwai static void spu_memload(u32 toi, const void *from, int length)
104198de43dSAdrian McMenamin {
10544e0b682SAdrian McMenamin 	unsigned long flags;
106f1dd4795STakashi Iwai 	const u32 *froml = from;
107198de43dSAdrian McMenamin 	u32 __iomem *to = (u32 __iomem *) (SPU_MEMORY_BASE + toi);
108198de43dSAdrian McMenamin 	int i;
109198de43dSAdrian McMenamin 	u32 val;
110198de43dSAdrian McMenamin 	length = DIV_ROUND_UP(length, 4);
111198de43dSAdrian McMenamin 	spu_write_wait();
112198de43dSAdrian McMenamin 	for (i = 0; i < length; i++) {
113198de43dSAdrian McMenamin 		if (!(i % 8))
114198de43dSAdrian McMenamin 			spu_write_wait();
115198de43dSAdrian McMenamin 		val = *froml;
11644e0b682SAdrian McMenamin 		local_irq_save(flags);
117198de43dSAdrian McMenamin 		writel(val, to);
11844e0b682SAdrian McMenamin 		local_irq_restore(flags);
119198de43dSAdrian McMenamin 		froml++;
120198de43dSAdrian McMenamin 		to++;
121198de43dSAdrian McMenamin 	}
122198de43dSAdrian McMenamin }
123198de43dSAdrian McMenamin 
124198de43dSAdrian McMenamin /* spu_disable - set spu registers to stop sound output */
spu_disable(void)125198de43dSAdrian McMenamin static void spu_disable(void)
126198de43dSAdrian McMenamin {
127198de43dSAdrian McMenamin 	int i;
12844e0b682SAdrian McMenamin 	unsigned long flags;
129198de43dSAdrian McMenamin 	u32 regval;
130198de43dSAdrian McMenamin 	spu_write_wait();
131198de43dSAdrian McMenamin 	regval = readl(ARM_RESET_REGISTER);
132198de43dSAdrian McMenamin 	regval |= 1;
133198de43dSAdrian McMenamin 	spu_write_wait();
13444e0b682SAdrian McMenamin 	local_irq_save(flags);
135198de43dSAdrian McMenamin 	writel(regval, ARM_RESET_REGISTER);
13644e0b682SAdrian McMenamin 	local_irq_restore(flags);
137198de43dSAdrian McMenamin 	for (i = 0; i < 64; i++) {
138198de43dSAdrian McMenamin 		spu_write_wait();
139198de43dSAdrian McMenamin 		regval = readl(SPU_REGISTER_BASE + (i * 0x80));
140198de43dSAdrian McMenamin 		regval = (regval & ~0x4000) | 0x8000;
141198de43dSAdrian McMenamin 		spu_write_wait();
14244e0b682SAdrian McMenamin 		local_irq_save(flags);
143198de43dSAdrian McMenamin 		writel(regval, SPU_REGISTER_BASE + (i * 0x80));
14444e0b682SAdrian McMenamin 		local_irq_restore(flags);
145198de43dSAdrian McMenamin 	}
146198de43dSAdrian McMenamin }
147198de43dSAdrian McMenamin 
148198de43dSAdrian McMenamin /* spu_enable - set spu registers to enable sound output */
spu_enable(void)149198de43dSAdrian McMenamin static void spu_enable(void)
150198de43dSAdrian McMenamin {
15144e0b682SAdrian McMenamin 	unsigned long flags;
152198de43dSAdrian McMenamin 	u32 regval = readl(ARM_RESET_REGISTER);
153198de43dSAdrian McMenamin 	regval &= ~1;
154198de43dSAdrian McMenamin 	spu_write_wait();
15544e0b682SAdrian McMenamin 	local_irq_save(flags);
156198de43dSAdrian McMenamin 	writel(regval, ARM_RESET_REGISTER);
15744e0b682SAdrian McMenamin 	local_irq_restore(flags);
158198de43dSAdrian McMenamin }
159198de43dSAdrian McMenamin 
160198de43dSAdrian McMenamin /*
161198de43dSAdrian McMenamin  * Halt the sound processor, clear the memory,
162198de43dSAdrian McMenamin  * load some default ARM7 code, and then restart ARM7
163198de43dSAdrian McMenamin */
spu_reset(void)164198de43dSAdrian McMenamin static void spu_reset(void)
165198de43dSAdrian McMenamin {
16644e0b682SAdrian McMenamin 	unsigned long flags;
167198de43dSAdrian McMenamin 	spu_disable();
168198de43dSAdrian McMenamin 	spu_memset(0, 0, 0x200000 / 4);
169198de43dSAdrian McMenamin 	/* Put ARM7 in endless loop */
17044e0b682SAdrian McMenamin 	local_irq_save(flags);
17148ccb2ceSPaul Mundt 	__raw_writel(0xea000002, SPU_MEMORY_BASE);
17244e0b682SAdrian McMenamin 	local_irq_restore(flags);
173198de43dSAdrian McMenamin 	spu_enable();
174198de43dSAdrian McMenamin }
175198de43dSAdrian McMenamin 
176198de43dSAdrian McMenamin /* aica_chn_start - write to spu to start playback */
aica_chn_start(void)177198de43dSAdrian McMenamin static void aica_chn_start(void)
178198de43dSAdrian McMenamin {
17944e0b682SAdrian McMenamin 	unsigned long flags;
180198de43dSAdrian McMenamin 	spu_write_wait();
18144e0b682SAdrian McMenamin 	local_irq_save(flags);
182198de43dSAdrian McMenamin 	writel(AICA_CMD_KICK | AICA_CMD_START, (u32 *) AICA_CONTROL_POINT);
18344e0b682SAdrian McMenamin 	local_irq_restore(flags);
184198de43dSAdrian McMenamin }
185198de43dSAdrian McMenamin 
186198de43dSAdrian McMenamin /* aica_chn_halt - write to spu to halt playback */
aica_chn_halt(void)187198de43dSAdrian McMenamin static void aica_chn_halt(void)
188198de43dSAdrian McMenamin {
18944e0b682SAdrian McMenamin 	unsigned long flags;
190198de43dSAdrian McMenamin 	spu_write_wait();
19144e0b682SAdrian McMenamin 	local_irq_save(flags);
192198de43dSAdrian McMenamin 	writel(AICA_CMD_KICK | AICA_CMD_STOP, (u32 *) AICA_CONTROL_POINT);
19344e0b682SAdrian McMenamin 	local_irq_restore(flags);
194198de43dSAdrian McMenamin }
195198de43dSAdrian McMenamin 
196198de43dSAdrian McMenamin /* ALSA code below */
197cd196fe2SBhumika Goyal static const struct snd_pcm_hardware snd_pcm_aica_playback_hw = {
198198de43dSAdrian McMenamin 	.info = (SNDRV_PCM_INFO_NONINTERLEAVED),
199198de43dSAdrian McMenamin 	.formats =
200198de43dSAdrian McMenamin 	    (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |
201198de43dSAdrian McMenamin 	     SNDRV_PCM_FMTBIT_IMA_ADPCM),
202198de43dSAdrian McMenamin 	.rates = SNDRV_PCM_RATE_8000_48000,
203198de43dSAdrian McMenamin 	.rate_min = 8000,
204198de43dSAdrian McMenamin 	.rate_max = 48000,
205198de43dSAdrian McMenamin 	.channels_min = 1,
206198de43dSAdrian McMenamin 	.channels_max = 2,
207198de43dSAdrian McMenamin 	.buffer_bytes_max = AICA_BUFFER_SIZE,
208198de43dSAdrian McMenamin 	.period_bytes_min = AICA_PERIOD_SIZE,
209198de43dSAdrian McMenamin 	.period_bytes_max = AICA_PERIOD_SIZE,
210198de43dSAdrian McMenamin 	.periods_min = AICA_PERIOD_NUMBER,
211198de43dSAdrian McMenamin 	.periods_max = AICA_PERIOD_NUMBER,
212198de43dSAdrian McMenamin };
213198de43dSAdrian McMenamin 
aica_dma_transfer(int channels,int buffer_size,struct snd_pcm_substream * substream)214198de43dSAdrian McMenamin static int aica_dma_transfer(int channels, int buffer_size,
215198de43dSAdrian McMenamin 			     struct snd_pcm_substream *substream)
216198de43dSAdrian McMenamin {
217198de43dSAdrian McMenamin 	int q, err, period_offset;
218198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard;
219198de43dSAdrian McMenamin 	struct snd_pcm_runtime *runtime;
22044e0b682SAdrian McMenamin 	unsigned long flags;
221ceac4bf3STakashi Iwai 	err = 0;
222198de43dSAdrian McMenamin 	dreamcastcard = substream->pcm->private_data;
223198de43dSAdrian McMenamin 	period_offset = dreamcastcard->clicks;
224198de43dSAdrian McMenamin 	period_offset %= (AICA_PERIOD_NUMBER / channels);
225198de43dSAdrian McMenamin 	runtime = substream->runtime;
226198de43dSAdrian McMenamin 	for (q = 0; q < channels; q++) {
22744e0b682SAdrian McMenamin 		local_irq_save(flags);
228198de43dSAdrian McMenamin 		err = dma_xfer(AICA_DMA_CHANNEL,
229198de43dSAdrian McMenamin 			       (unsigned long) (runtime->dma_area +
230198de43dSAdrian McMenamin 						(AICA_BUFFER_SIZE * q) /
231198de43dSAdrian McMenamin 						channels +
232198de43dSAdrian McMenamin 						AICA_PERIOD_SIZE *
233198de43dSAdrian McMenamin 						period_offset),
234198de43dSAdrian McMenamin 			       AICA_CHANNEL0_OFFSET + q * CHANNEL_OFFSET +
235198de43dSAdrian McMenamin 			       AICA_PERIOD_SIZE * period_offset,
236198de43dSAdrian McMenamin 			       buffer_size / channels, AICA_DMA_MODE);
23744e0b682SAdrian McMenamin 		if (unlikely(err < 0)) {
23844e0b682SAdrian McMenamin 			local_irq_restore(flags);
239198de43dSAdrian McMenamin 			break;
24044e0b682SAdrian McMenamin 		}
241198de43dSAdrian McMenamin 		dma_wait_for_completion(AICA_DMA_CHANNEL);
24244e0b682SAdrian McMenamin 		local_irq_restore(flags);
243198de43dSAdrian McMenamin 	}
244198de43dSAdrian McMenamin 	return err;
245198de43dSAdrian McMenamin }
246198de43dSAdrian McMenamin 
startup_aica(struct snd_card_aica * dreamcastcard)247198de43dSAdrian McMenamin static void startup_aica(struct snd_card_aica *dreamcastcard)
248198de43dSAdrian McMenamin {
249198de43dSAdrian McMenamin 	spu_memload(AICA_CHANNEL0_CONTROL_OFFSET,
250d20b9b20STakashi Iwai 		    dreamcastcard->channel, sizeof(struct aica_channel));
251198de43dSAdrian McMenamin 	aica_chn_start();
252198de43dSAdrian McMenamin }
253198de43dSAdrian McMenamin 
run_spu_dma(struct work_struct * work)254198de43dSAdrian McMenamin static void run_spu_dma(struct work_struct *work)
255198de43dSAdrian McMenamin {
256198de43dSAdrian McMenamin 	int buffer_size;
257198de43dSAdrian McMenamin 	struct snd_pcm_runtime *runtime;
258198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard;
259d20b9b20STakashi Iwai 	dreamcastcard =
260d20b9b20STakashi Iwai 	    container_of(work, struct snd_card_aica, spu_dma_work);
261d20b9b20STakashi Iwai 	runtime = dreamcastcard->substream->runtime;
262198de43dSAdrian McMenamin 	if (unlikely(dreamcastcard->dma_check == 0)) {
263d20b9b20STakashi Iwai 		buffer_size =
264d20b9b20STakashi Iwai 		    frames_to_bytes(runtime, runtime->buffer_size);
265198de43dSAdrian McMenamin 		if (runtime->channels > 1)
266198de43dSAdrian McMenamin 			dreamcastcard->channel->flags |= 0x01;
267d20b9b20STakashi Iwai 		aica_dma_transfer(runtime->channels, buffer_size,
268d20b9b20STakashi Iwai 				  dreamcastcard->substream);
269198de43dSAdrian McMenamin 		startup_aica(dreamcastcard);
270198de43dSAdrian McMenamin 		dreamcastcard->clicks =
271198de43dSAdrian McMenamin 		    buffer_size / (AICA_PERIOD_SIZE * runtime->channels);
272198de43dSAdrian McMenamin 		return;
273198de43dSAdrian McMenamin 	} else {
274198de43dSAdrian McMenamin 		aica_dma_transfer(runtime->channels,
275198de43dSAdrian McMenamin 				  AICA_PERIOD_SIZE * runtime->channels,
276d20b9b20STakashi Iwai 				  dreamcastcard->substream);
277198de43dSAdrian McMenamin 		snd_pcm_period_elapsed(dreamcastcard->substream);
278198de43dSAdrian McMenamin 		dreamcastcard->clicks++;
279198de43dSAdrian McMenamin 		if (unlikely(dreamcastcard->clicks >= AICA_PERIOD_NUMBER))
280198de43dSAdrian McMenamin 			dreamcastcard->clicks %= AICA_PERIOD_NUMBER;
281*61d47876SDuoming Zhou 		if (snd_pcm_running(dreamcastcard->substream))
282198de43dSAdrian McMenamin 			mod_timer(&dreamcastcard->timer, jiffies + 1);
283198de43dSAdrian McMenamin 	}
284198de43dSAdrian McMenamin }
285198de43dSAdrian McMenamin 
aica_period_elapsed(struct timer_list * t)286d522bb6aSKees Cook static void aica_period_elapsed(struct timer_list *t)
287198de43dSAdrian McMenamin {
288d522bb6aSKees Cook 	struct snd_card_aica *dreamcastcard = from_timer(dreamcastcard,
289d522bb6aSKees Cook 							      t, timer);
290534420c6STakashi Iwai 	struct snd_pcm_substream *substream = dreamcastcard->substream;
291198de43dSAdrian McMenamin 	/*timer function - so cannot sleep */
292198de43dSAdrian McMenamin 	int play_period;
293198de43dSAdrian McMenamin 	struct snd_pcm_runtime *runtime;
294*61d47876SDuoming Zhou 	if (!snd_pcm_running(substream))
295*61d47876SDuoming Zhou 		return;
296198de43dSAdrian McMenamin 	runtime = substream->runtime;
297198de43dSAdrian McMenamin 	dreamcastcard = substream->pcm->private_data;
298198de43dSAdrian McMenamin 	/* Have we played out an additional period? */
299198de43dSAdrian McMenamin 	play_period =
300198de43dSAdrian McMenamin 	    frames_to_bytes(runtime,
301198de43dSAdrian McMenamin 			    readl
302198de43dSAdrian McMenamin 			    (AICA_CONTROL_CHANNEL_SAMPLE_NUMBER)) /
303198de43dSAdrian McMenamin 	    AICA_PERIOD_SIZE;
304198de43dSAdrian McMenamin 	if (play_period == dreamcastcard->current_period) {
305198de43dSAdrian McMenamin 		/* reschedule the timer */
306198de43dSAdrian McMenamin 		mod_timer(&(dreamcastcard->timer), jiffies + 1);
307198de43dSAdrian McMenamin 		return;
308198de43dSAdrian McMenamin 	}
309198de43dSAdrian McMenamin 	if (runtime->channels > 1)
310198de43dSAdrian McMenamin 		dreamcastcard->current_period = play_period;
311198de43dSAdrian McMenamin 	if (unlikely(dreamcastcard->dma_check == 0))
312198de43dSAdrian McMenamin 		dreamcastcard->dma_check = 1;
31343aa56d9SBhaktipriya Shridhar 	schedule_work(&(dreamcastcard->spu_dma_work));
314198de43dSAdrian McMenamin }
315198de43dSAdrian McMenamin 
spu_begin_dma(struct snd_pcm_substream * substream)316198de43dSAdrian McMenamin static void spu_begin_dma(struct snd_pcm_substream *substream)
317198de43dSAdrian McMenamin {
318198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard;
319198de43dSAdrian McMenamin 	struct snd_pcm_runtime *runtime;
320198de43dSAdrian McMenamin 	runtime = substream->runtime;
321198de43dSAdrian McMenamin 	dreamcastcard = substream->pcm->private_data;
322d20b9b20STakashi Iwai 	/*get the queue to do the work */
32343aa56d9SBhaktipriya Shridhar 	schedule_work(&(dreamcastcard->spu_dma_work));
324da1a3924STakashi Iwai 	mod_timer(&dreamcastcard->timer, jiffies + 4);
325198de43dSAdrian McMenamin }
326198de43dSAdrian McMenamin 
snd_aicapcm_pcm_open(struct snd_pcm_substream * substream)327198de43dSAdrian McMenamin static int snd_aicapcm_pcm_open(struct snd_pcm_substream
328198de43dSAdrian McMenamin 				*substream)
329198de43dSAdrian McMenamin {
330198de43dSAdrian McMenamin 	struct snd_pcm_runtime *runtime;
331198de43dSAdrian McMenamin 	struct aica_channel *channel;
332198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard;
333198de43dSAdrian McMenamin 	if (!enable)
334198de43dSAdrian McMenamin 		return -ENOENT;
335198de43dSAdrian McMenamin 	dreamcastcard = substream->pcm->private_data;
336198de43dSAdrian McMenamin 	channel = kmalloc(sizeof(struct aica_channel), GFP_KERNEL);
337198de43dSAdrian McMenamin 	if (!channel)
338198de43dSAdrian McMenamin 		return -ENOMEM;
339198de43dSAdrian McMenamin 	/* set defaults for channel */
340198de43dSAdrian McMenamin 	channel->sfmt = SM_8BIT;
341198de43dSAdrian McMenamin 	channel->cmd = AICA_CMD_START;
342198de43dSAdrian McMenamin 	channel->vol = dreamcastcard->master_volume;
343198de43dSAdrian McMenamin 	channel->pan = 0x80;
344198de43dSAdrian McMenamin 	channel->pos = 0;
345198de43dSAdrian McMenamin 	channel->flags = 0;	/* default to mono */
346198de43dSAdrian McMenamin 	dreamcastcard->channel = channel;
347198de43dSAdrian McMenamin 	runtime = substream->runtime;
348198de43dSAdrian McMenamin 	runtime->hw = snd_pcm_aica_playback_hw;
349198de43dSAdrian McMenamin 	spu_enable();
350198de43dSAdrian McMenamin 	dreamcastcard->clicks = 0;
351198de43dSAdrian McMenamin 	dreamcastcard->current_period = 0;
352198de43dSAdrian McMenamin 	dreamcastcard->dma_check = 0;
353198de43dSAdrian McMenamin 	return 0;
354198de43dSAdrian McMenamin }
355198de43dSAdrian McMenamin 
snd_aicapcm_pcm_sync_stop(struct snd_pcm_substream * substream)356*61d47876SDuoming Zhou static int snd_aicapcm_pcm_sync_stop(struct snd_pcm_substream *substream)
357*61d47876SDuoming Zhou {
358*61d47876SDuoming Zhou 	struct snd_card_aica *dreamcastcard = substream->pcm->private_data;
359*61d47876SDuoming Zhou 
360*61d47876SDuoming Zhou 	del_timer_sync(&dreamcastcard->timer);
361*61d47876SDuoming Zhou 	cancel_work_sync(&dreamcastcard->spu_dma_work);
362*61d47876SDuoming Zhou 	return 0;
363*61d47876SDuoming Zhou }
364*61d47876SDuoming Zhou 
snd_aicapcm_pcm_close(struct snd_pcm_substream * substream)365198de43dSAdrian McMenamin static int snd_aicapcm_pcm_close(struct snd_pcm_substream
366198de43dSAdrian McMenamin 				 *substream)
367198de43dSAdrian McMenamin {
368198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard = substream->pcm->private_data;
369534420c6STakashi Iwai 	dreamcastcard->substream = NULL;
370198de43dSAdrian McMenamin 	kfree(dreamcastcard->channel);
371198de43dSAdrian McMenamin 	spu_disable();
372198de43dSAdrian McMenamin 	return 0;
373198de43dSAdrian McMenamin }
374198de43dSAdrian McMenamin 
snd_aicapcm_pcm_prepare(struct snd_pcm_substream * substream)375198de43dSAdrian McMenamin static int snd_aicapcm_pcm_prepare(struct snd_pcm_substream
376198de43dSAdrian McMenamin 				   *substream)
377198de43dSAdrian McMenamin {
378198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard = substream->pcm->private_data;
379198de43dSAdrian McMenamin 	if ((substream->runtime)->format == SNDRV_PCM_FORMAT_S16_LE)
380198de43dSAdrian McMenamin 		dreamcastcard->channel->sfmt = SM_16BIT;
381198de43dSAdrian McMenamin 	dreamcastcard->channel->freq = substream->runtime->rate;
382198de43dSAdrian McMenamin 	dreamcastcard->substream = substream;
383198de43dSAdrian McMenamin 	return 0;
384198de43dSAdrian McMenamin }
385198de43dSAdrian McMenamin 
snd_aicapcm_pcm_trigger(struct snd_pcm_substream * substream,int cmd)386198de43dSAdrian McMenamin static int snd_aicapcm_pcm_trigger(struct snd_pcm_substream
387198de43dSAdrian McMenamin 				   *substream, int cmd)
388198de43dSAdrian McMenamin {
389198de43dSAdrian McMenamin 	switch (cmd) {
390198de43dSAdrian McMenamin 	case SNDRV_PCM_TRIGGER_START:
391198de43dSAdrian McMenamin 		spu_begin_dma(substream);
392198de43dSAdrian McMenamin 		break;
393198de43dSAdrian McMenamin 	case SNDRV_PCM_TRIGGER_STOP:
394198de43dSAdrian McMenamin 		aica_chn_halt();
395198de43dSAdrian McMenamin 		break;
396198de43dSAdrian McMenamin 	default:
397198de43dSAdrian McMenamin 		return -EINVAL;
398198de43dSAdrian McMenamin 	}
399198de43dSAdrian McMenamin 	return 0;
400198de43dSAdrian McMenamin }
401198de43dSAdrian McMenamin 
snd_aicapcm_pcm_pointer(struct snd_pcm_substream * substream)402198de43dSAdrian McMenamin static unsigned long snd_aicapcm_pcm_pointer(struct snd_pcm_substream
403198de43dSAdrian McMenamin 					     *substream)
404198de43dSAdrian McMenamin {
405198de43dSAdrian McMenamin 	return readl(AICA_CONTROL_CHANNEL_SAMPLE_NUMBER);
406198de43dSAdrian McMenamin }
407198de43dSAdrian McMenamin 
40846a085a3SArvind Yadav static const struct snd_pcm_ops snd_aicapcm_playback_ops = {
409198de43dSAdrian McMenamin 	.open = snd_aicapcm_pcm_open,
410198de43dSAdrian McMenamin 	.close = snd_aicapcm_pcm_close,
411198de43dSAdrian McMenamin 	.prepare = snd_aicapcm_pcm_prepare,
412198de43dSAdrian McMenamin 	.trigger = snd_aicapcm_pcm_trigger,
413198de43dSAdrian McMenamin 	.pointer = snd_aicapcm_pcm_pointer,
414*61d47876SDuoming Zhou 	.sync_stop = snd_aicapcm_pcm_sync_stop,
415198de43dSAdrian McMenamin };
416198de43dSAdrian McMenamin 
417198de43dSAdrian McMenamin /* TO DO: set up to handle more than one pcm instance */
snd_aicapcmchip(struct snd_card_aica * dreamcastcard,int pcm_index)418198de43dSAdrian McMenamin static int __init snd_aicapcmchip(struct snd_card_aica
419198de43dSAdrian McMenamin 				  *dreamcastcard, int pcm_index)
420198de43dSAdrian McMenamin {
421198de43dSAdrian McMenamin 	struct snd_pcm *pcm;
422198de43dSAdrian McMenamin 	int err;
423198de43dSAdrian McMenamin 	/* AICA has no capture ability */
424198de43dSAdrian McMenamin 	err =
425d20b9b20STakashi Iwai 	    snd_pcm_new(dreamcastcard->card, "AICA PCM", pcm_index, 1, 0,
426d20b9b20STakashi Iwai 			&pcm);
427198de43dSAdrian McMenamin 	if (unlikely(err < 0))
428198de43dSAdrian McMenamin 		return err;
429198de43dSAdrian McMenamin 	pcm->private_data = dreamcastcard;
430198de43dSAdrian McMenamin 	strcpy(pcm->name, "AICA PCM");
431198de43dSAdrian McMenamin 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
432198de43dSAdrian McMenamin 			&snd_aicapcm_playback_ops);
433198de43dSAdrian McMenamin 	/* Allocate the DMA buffers */
434403d026cSTakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm,
435198de43dSAdrian McMenamin 				       SNDRV_DMA_TYPE_CONTINUOUS,
436a1aef488STakashi Iwai 				       NULL,
437198de43dSAdrian McMenamin 				       AICA_BUFFER_SIZE,
438198de43dSAdrian McMenamin 				       AICA_BUFFER_SIZE);
4392462bca0STakashi Iwai 	return 0;
440198de43dSAdrian McMenamin }
441198de43dSAdrian McMenamin 
442198de43dSAdrian McMenamin /* Mixer controls */
443a5ce8890STakashi Iwai #define aica_pcmswitch_info		snd_ctl_boolean_mono_info
444198de43dSAdrian McMenamin 
aica_pcmswitch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)445198de43dSAdrian McMenamin static int aica_pcmswitch_get(struct snd_kcontrol *kcontrol,
446198de43dSAdrian McMenamin 			      struct snd_ctl_elem_value *ucontrol)
447198de43dSAdrian McMenamin {
448198de43dSAdrian McMenamin 	ucontrol->value.integer.value[0] = 1;	/* TO DO: Fix me */
449198de43dSAdrian McMenamin 	return 0;
450198de43dSAdrian McMenamin }
451198de43dSAdrian McMenamin 
aica_pcmswitch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)452198de43dSAdrian McMenamin static int aica_pcmswitch_put(struct snd_kcontrol *kcontrol,
453198de43dSAdrian McMenamin 			      struct snd_ctl_elem_value *ucontrol)
454198de43dSAdrian McMenamin {
455198de43dSAdrian McMenamin 	if (ucontrol->value.integer.value[0] == 1)
456198de43dSAdrian McMenamin 		return 0;	/* TO DO: Fix me */
457198de43dSAdrian McMenamin 	else
458198de43dSAdrian McMenamin 		aica_chn_halt();
459198de43dSAdrian McMenamin 	return 0;
460198de43dSAdrian McMenamin }
461198de43dSAdrian McMenamin 
aica_pcmvolume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)462198de43dSAdrian McMenamin static int aica_pcmvolume_info(struct snd_kcontrol *kcontrol,
463198de43dSAdrian McMenamin 			       struct snd_ctl_elem_info *uinfo)
464198de43dSAdrian McMenamin {
465198de43dSAdrian McMenamin 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
466198de43dSAdrian McMenamin 	uinfo->count = 1;
467198de43dSAdrian McMenamin 	uinfo->value.integer.min = 0;
468198de43dSAdrian McMenamin 	uinfo->value.integer.max = 0xFF;
469198de43dSAdrian McMenamin 	return 0;
470198de43dSAdrian McMenamin }
471198de43dSAdrian McMenamin 
aica_pcmvolume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)472198de43dSAdrian McMenamin static int aica_pcmvolume_get(struct snd_kcontrol *kcontrol,
473198de43dSAdrian McMenamin 			      struct snd_ctl_elem_value *ucontrol)
474198de43dSAdrian McMenamin {
475198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard;
476198de43dSAdrian McMenamin 	dreamcastcard = kcontrol->private_data;
477198de43dSAdrian McMenamin 	if (unlikely(!dreamcastcard->channel))
478198de43dSAdrian McMenamin 		return -ETXTBSY;	/* we've not yet been set up */
479198de43dSAdrian McMenamin 	ucontrol->value.integer.value[0] = dreamcastcard->channel->vol;
480198de43dSAdrian McMenamin 	return 0;
481198de43dSAdrian McMenamin }
482198de43dSAdrian McMenamin 
aica_pcmvolume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)483198de43dSAdrian McMenamin static int aica_pcmvolume_put(struct snd_kcontrol *kcontrol,
484198de43dSAdrian McMenamin 			      struct snd_ctl_elem_value *ucontrol)
485198de43dSAdrian McMenamin {
486198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard;
4873b892467STakashi Iwai 	unsigned int vol;
488198de43dSAdrian McMenamin 	dreamcastcard = kcontrol->private_data;
489198de43dSAdrian McMenamin 	if (unlikely(!dreamcastcard->channel))
490198de43dSAdrian McMenamin 		return -ETXTBSY;
4913b892467STakashi Iwai 	vol = ucontrol->value.integer.value[0];
4923b892467STakashi Iwai 	if (vol > 0xff)
4933b892467STakashi Iwai 		return -EINVAL;
4943b892467STakashi Iwai 	if (unlikely(dreamcastcard->channel->vol == vol))
495198de43dSAdrian McMenamin 		return 0;
496198de43dSAdrian McMenamin 	dreamcastcard->channel->vol = ucontrol->value.integer.value[0];
497198de43dSAdrian McMenamin 	dreamcastcard->master_volume = ucontrol->value.integer.value[0];
498198de43dSAdrian McMenamin 	spu_memload(AICA_CHANNEL0_CONTROL_OFFSET,
499d20b9b20STakashi Iwai 		    dreamcastcard->channel, sizeof(struct aica_channel));
500198de43dSAdrian McMenamin 	return 1;
501198de43dSAdrian McMenamin }
502198de43dSAdrian McMenamin 
503905e46acSBhumika Goyal static const struct snd_kcontrol_new snd_aica_pcmswitch_control = {
504198de43dSAdrian McMenamin 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
505198de43dSAdrian McMenamin 	.name = "PCM Playback Switch",
506198de43dSAdrian McMenamin 	.index = 0,
507198de43dSAdrian McMenamin 	.info = aica_pcmswitch_info,
508198de43dSAdrian McMenamin 	.get = aica_pcmswitch_get,
509198de43dSAdrian McMenamin 	.put = aica_pcmswitch_put
510198de43dSAdrian McMenamin };
511198de43dSAdrian McMenamin 
512905e46acSBhumika Goyal static const struct snd_kcontrol_new snd_aica_pcmvolume_control = {
513198de43dSAdrian McMenamin 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
514198de43dSAdrian McMenamin 	.name = "PCM Playback Volume",
515198de43dSAdrian McMenamin 	.index = 0,
516198de43dSAdrian McMenamin 	.info = aica_pcmvolume_info,
517198de43dSAdrian McMenamin 	.get = aica_pcmvolume_get,
518198de43dSAdrian McMenamin 	.put = aica_pcmvolume_put
519198de43dSAdrian McMenamin };
520198de43dSAdrian McMenamin 
load_aica_firmware(void)521198de43dSAdrian McMenamin static int load_aica_firmware(void)
522198de43dSAdrian McMenamin {
523198de43dSAdrian McMenamin 	int err;
524198de43dSAdrian McMenamin 	const struct firmware *fw_entry;
525198de43dSAdrian McMenamin 	spu_reset();
526198de43dSAdrian McMenamin 	err = request_firmware(&fw_entry, "aica_firmware.bin", &pd->dev);
527198de43dSAdrian McMenamin 	if (unlikely(err))
528198de43dSAdrian McMenamin 		return err;
529877d0310SNick Andrew 	/* write firmware into memory */
530198de43dSAdrian McMenamin 	spu_disable();
531198de43dSAdrian McMenamin 	spu_memload(0, fw_entry->data, fw_entry->size);
532198de43dSAdrian McMenamin 	spu_enable();
533198de43dSAdrian McMenamin 	release_firmware(fw_entry);
534198de43dSAdrian McMenamin 	return err;
535198de43dSAdrian McMenamin }
536198de43dSAdrian McMenamin 
add_aicamixer_controls(struct snd_card_aica * dreamcastcard)537e74033a8SBill Pemberton static int add_aicamixer_controls(struct snd_card_aica *dreamcastcard)
538198de43dSAdrian McMenamin {
539198de43dSAdrian McMenamin 	int err;
540198de43dSAdrian McMenamin 	err = snd_ctl_add
541198de43dSAdrian McMenamin 	    (dreamcastcard->card,
542198de43dSAdrian McMenamin 	     snd_ctl_new1(&snd_aica_pcmvolume_control, dreamcastcard));
543198de43dSAdrian McMenamin 	if (unlikely(err < 0))
544198de43dSAdrian McMenamin 		return err;
545198de43dSAdrian McMenamin 	err = snd_ctl_add
546198de43dSAdrian McMenamin 	    (dreamcastcard->card,
547198de43dSAdrian McMenamin 	     snd_ctl_new1(&snd_aica_pcmswitch_control, dreamcastcard));
548198de43dSAdrian McMenamin 	if (unlikely(err < 0))
549198de43dSAdrian McMenamin 		return err;
550198de43dSAdrian McMenamin 	return 0;
551198de43dSAdrian McMenamin }
552198de43dSAdrian McMenamin 
snd_aica_remove(struct platform_device * devptr)553de0f49b8SUwe Kleine-König static void snd_aica_remove(struct platform_device *devptr)
554198de43dSAdrian McMenamin {
555198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard;
556198de43dSAdrian McMenamin 	dreamcastcard = platform_get_drvdata(devptr);
557198de43dSAdrian McMenamin 	snd_card_free(dreamcastcard->card);
558198de43dSAdrian McMenamin 	kfree(dreamcastcard);
559198de43dSAdrian McMenamin }
560198de43dSAdrian McMenamin 
snd_aica_probe(struct platform_device * devptr)561e74033a8SBill Pemberton static int snd_aica_probe(struct platform_device *devptr)
562198de43dSAdrian McMenamin {
563198de43dSAdrian McMenamin 	int err;
564198de43dSAdrian McMenamin 	struct snd_card_aica *dreamcastcard;
565d522bb6aSKees Cook 	dreamcastcard = kzalloc(sizeof(struct snd_card_aica), GFP_KERNEL);
566198de43dSAdrian McMenamin 	if (unlikely(!dreamcastcard))
567198de43dSAdrian McMenamin 		return -ENOMEM;
568e7182ac5STakashi Iwai 	err = snd_card_new(&devptr->dev, index, SND_AICA_DRIVER,
569e7182ac5STakashi Iwai 			   THIS_MODULE, 0, &dreamcastcard->card);
570bd7dd77cSTakashi Iwai 	if (unlikely(err < 0)) {
571198de43dSAdrian McMenamin 		kfree(dreamcastcard);
572bd7dd77cSTakashi Iwai 		return err;
573198de43dSAdrian McMenamin 	}
574198de43dSAdrian McMenamin 	strcpy(dreamcastcard->card->driver, "snd_aica");
575198de43dSAdrian McMenamin 	strcpy(dreamcastcard->card->shortname, SND_AICA_DRIVER);
576198de43dSAdrian McMenamin 	strcpy(dreamcastcard->card->longname,
577198de43dSAdrian McMenamin 	       "Yamaha AICA Super Intelligent Sound Processor for SEGA Dreamcast");
578d20b9b20STakashi Iwai 	/* Prepare to use the queue */
579d20b9b20STakashi Iwai 	INIT_WORK(&(dreamcastcard->spu_dma_work), run_spu_dma);
580534420c6STakashi Iwai 	timer_setup(&dreamcastcard->timer, aica_period_elapsed, 0);
581198de43dSAdrian McMenamin 	/* Load the PCM 'chip' */
582198de43dSAdrian McMenamin 	err = snd_aicapcmchip(dreamcastcard, 0);
583198de43dSAdrian McMenamin 	if (unlikely(err < 0))
584198de43dSAdrian McMenamin 		goto freedreamcast;
585198de43dSAdrian McMenamin 	/* Add basic controls */
586198de43dSAdrian McMenamin 	err = add_aicamixer_controls(dreamcastcard);
587198de43dSAdrian McMenamin 	if (unlikely(err < 0))
588198de43dSAdrian McMenamin 		goto freedreamcast;
589198de43dSAdrian McMenamin 	/* Register the card with ALSA subsystem */
590198de43dSAdrian McMenamin 	err = snd_card_register(dreamcastcard->card);
591198de43dSAdrian McMenamin 	if (unlikely(err < 0))
592198de43dSAdrian McMenamin 		goto freedreamcast;
593198de43dSAdrian McMenamin 	platform_set_drvdata(devptr, dreamcastcard);
594198de43dSAdrian McMenamin 	snd_printk
595198de43dSAdrian McMenamin 	    ("ALSA Driver for Yamaha AICA Super Intelligent Sound Processor\n");
596198de43dSAdrian McMenamin 	return 0;
597198de43dSAdrian McMenamin       freedreamcast:
598198de43dSAdrian McMenamin 	snd_card_free(dreamcastcard->card);
599198de43dSAdrian McMenamin 	kfree(dreamcastcard);
600198de43dSAdrian McMenamin 	return err;
601198de43dSAdrian McMenamin }
602198de43dSAdrian McMenamin 
603198de43dSAdrian McMenamin static struct platform_driver snd_aica_driver = {
604198de43dSAdrian McMenamin 	.probe = snd_aica_probe,
605de0f49b8SUwe Kleine-König 	.remove_new = snd_aica_remove,
606198de43dSAdrian McMenamin 	.driver = {
6078bf01d8aSTakashi Iwai 		.name = SND_AICA_DRIVER,
6088bf01d8aSTakashi Iwai 	},
609198de43dSAdrian McMenamin };
610198de43dSAdrian McMenamin 
aica_init(void)611198de43dSAdrian McMenamin static int __init aica_init(void)
612198de43dSAdrian McMenamin {
613198de43dSAdrian McMenamin 	int err;
614198de43dSAdrian McMenamin 	err = platform_driver_register(&snd_aica_driver);
615198de43dSAdrian McMenamin 	if (unlikely(err < 0))
616198de43dSAdrian McMenamin 		return err;
617198de43dSAdrian McMenamin 	pd = platform_device_register_simple(SND_AICA_DRIVER, -1,
618198de43dSAdrian McMenamin 					     aica_memory_space, 2);
619801678c5SHirofumi Nakagawa 	if (IS_ERR(pd)) {
620198de43dSAdrian McMenamin 		platform_driver_unregister(&snd_aica_driver);
621198de43dSAdrian McMenamin 		return PTR_ERR(pd);
622198de43dSAdrian McMenamin 	}
623198de43dSAdrian McMenamin 	/* Load the firmware */
624198de43dSAdrian McMenamin 	return load_aica_firmware();
625198de43dSAdrian McMenamin }
626198de43dSAdrian McMenamin 
aica_exit(void)627198de43dSAdrian McMenamin static void __exit aica_exit(void)
628198de43dSAdrian McMenamin {
629198de43dSAdrian McMenamin 	platform_device_unregister(pd);
630198de43dSAdrian McMenamin 	platform_driver_unregister(&snd_aica_driver);
631198de43dSAdrian McMenamin 	/* Kill any sound still playing and reset ARM7 to safe state */
632198de43dSAdrian McMenamin 	spu_reset();
633198de43dSAdrian McMenamin }
634198de43dSAdrian McMenamin 
635198de43dSAdrian McMenamin module_init(aica_init);
636198de43dSAdrian McMenamin module_exit(aica_exit);
637