blob: ea3be874c84f6803ea89d77254fe2f9294ce423e [file] [log] [blame]
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001/*
2 * linux/sound/arm/aaci.c - ARM PrimeCell AACI PL041 driver
3 *
4 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Documentation: ARM DDI 0173B
11 */
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/ioport.h>
16#include <linux/device.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
19#include <linux/err.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000020#include <linux/amba/bus.h>
Russell King88cdca92009-11-23 09:44:10 +010021#include <linux/io.h>
Russell Kingcb5a6ff2005-05-12 14:04:59 +020022
Russell Kingcb5a6ff2005-05-12 14:04:59 +020023#include <sound/core.h>
24#include <sound/initval.h>
25#include <sound/ac97_codec.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28
29#include "aaci.h"
Russell Kingcb5a6ff2005-05-12 14:04:59 +020030
31#define DRIVER_NAME "aaci-pl041"
32
33/*
34 * PM support is not complete. Turn it off.
35 */
36#undef CONFIG_PM
37
Takashi Iwaiceb9e472005-11-17 15:10:16 +010038static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020039{
40 u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
41
42 /*
43 * Ensure that the slot 1/2 RX registers are empty.
44 */
45 v = readl(aaci->base + AACI_SLFR);
46 if (v & SLFR_2RXV)
47 readl(aaci->base + AACI_SL2RX);
48 if (v & SLFR_1RXV)
49 readl(aaci->base + AACI_SL1RX);
50
51 writel(maincr, aaci->base + AACI_MAINCR);
52}
53
54/*
55 * P29:
56 * The recommended use of programming the external codec through slot 1
57 * and slot 2 data is to use the channels during setup routines and the
58 * slot register at any other time. The data written into slot 1, slot 2
59 * and slot 12 registers is transmitted only when their corresponding
60 * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
61 * register.
62 */
Kevin Hilman14d178a2007-02-07 05:46:47 +010063static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
64 unsigned short val)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020065{
66 struct aaci *aaci = ac97->private_data;
67 u32 v;
Kevin Hilman14d178a2007-02-07 05:46:47 +010068 int timeout = 5000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +020069
70 if (ac97->num >= 4)
71 return;
72
Ingo Molnar12aa7572006-01-16 16:36:05 +010073 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020074
75 aaci_ac97_select_codec(aaci, ac97);
76
77 /*
78 * P54: You must ensure that AACI_SL2TX is always written
79 * to, if required, before data is written to AACI_SL1TX.
80 */
81 writel(val << 4, aaci->base + AACI_SL2TX);
82 writel(reg << 12, aaci->base + AACI_SL1TX);
83
84 /*
85 * Wait for the transmission of both slots to complete.
86 */
87 do {
88 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +010089 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +010090
91 if (!timeout)
92 dev_err(&aaci->dev->dev,
93 "timeout waiting for write to complete\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +020094
Ingo Molnar12aa7572006-01-16 16:36:05 +010095 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020096}
97
98/*
99 * Read an AC'97 register.
100 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100101static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200102{
103 struct aaci *aaci = ac97->private_data;
104 u32 v;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100105 int timeout = 5000;
106 int retries = 10;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200107
108 if (ac97->num >= 4)
109 return ~0;
110
Ingo Molnar12aa7572006-01-16 16:36:05 +0100111 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200112
113 aaci_ac97_select_codec(aaci, ac97);
114
115 /*
116 * Write the register address to slot 1.
117 */
118 writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);
119
120 /*
121 * Wait for the transmission to complete.
122 */
123 do {
124 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100125 } while ((v & SLFR_1TXB) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +0100126
127 if (!timeout) {
128 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
129 v = ~0;
130 goto out;
131 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200132
133 /*
134 * Give the AC'97 codec more than enough time
135 * to respond. (42us = ~2 frames at 48kHz.)
136 */
137 udelay(42);
138
139 /*
140 * Wait for slot 2 to indicate data.
141 */
Kevin Hilman14d178a2007-02-07 05:46:47 +0100142 timeout = 5000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200143 do {
144 cond_resched();
145 v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100146 } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200147
Kevin Hilman14d178a2007-02-07 05:46:47 +0100148 if (!timeout) {
149 dev_err(&aaci->dev->dev, "timeout on RX valid\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200150 v = ~0;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100151 goto out;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200152 }
153
Kevin Hilman14d178a2007-02-07 05:46:47 +0100154 do {
155 v = readl(aaci->base + AACI_SL1RX) >> 12;
156 if (v == reg) {
157 v = readl(aaci->base + AACI_SL2RX) >> 4;
158 break;
159 } else if (--retries) {
160 dev_warn(&aaci->dev->dev,
161 "ac97 read back fail. retry\n");
162 continue;
163 } else {
164 dev_warn(&aaci->dev->dev,
165 "wrong ac97 register read back (%x != %x)\n",
166 v, reg);
167 v = ~0;
168 }
169 } while (retries);
170 out:
Ingo Molnar12aa7572006-01-16 16:36:05 +0100171 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200172 return v;
173}
174
175static inline void aaci_chan_wait_ready(struct aaci_runtime *aacirun)
176{
177 u32 val;
178 int timeout = 5000;
179
180 do {
181 val = readl(aacirun->base + AACI_SR);
182 } while (val & (SR_TXB|SR_RXB) && timeout--);
183}
184
185
186
187/*
188 * Interrupt support.
189 */
Kevin Hilman62578cb2007-02-07 05:41:37 +0100190static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200191{
Kevin Hilman41762b82007-02-07 05:45:32 +0100192 if (mask & ISR_ORINTR) {
193 dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
194 writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
195 }
196
197 if (mask & ISR_RXTOINTR) {
198 dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
199 writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
200 }
201
202 if (mask & ISR_RXINTR) {
203 struct aaci_runtime *aacirun = &aaci->capture;
204 void *ptr;
205
206 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700207 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
Kevin Hilman41762b82007-02-07 05:45:32 +0100208 writel(0, aacirun->base + AACI_IE);
209 return;
210 }
211 ptr = aacirun->ptr;
212
213 do {
214 unsigned int len = aacirun->fifosz;
215 u32 val;
216
217 if (aacirun->bytes <= 0) {
218 aacirun->bytes += aacirun->period;
219 aacirun->ptr = ptr;
220 spin_unlock(&aaci->lock);
221 snd_pcm_period_elapsed(aacirun->substream);
222 spin_lock(&aaci->lock);
223 }
224 if (!(aacirun->cr & CR_EN))
225 break;
226
227 val = readl(aacirun->base + AACI_SR);
228 if (!(val & SR_RXHF))
229 break;
230 if (!(val & SR_RXFF))
231 len >>= 1;
232
233 aacirun->bytes -= len;
234
235 /* reading 16 bytes at a time */
236 for( ; len > 0; len -= 16) {
237 asm(
238 "ldmia %1, {r0, r1, r2, r3}\n\t"
239 "stmia %0!, {r0, r1, r2, r3}"
240 : "+r" (ptr)
241 : "r" (aacirun->fifo)
242 : "r0", "r1", "r2", "r3", "cc");
243
244 if (ptr >= aacirun->end)
245 ptr = aacirun->start;
246 }
247 } while(1);
248 aacirun->ptr = ptr;
249 }
250
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200251 if (mask & ISR_URINTR) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100252 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
253 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200254 }
255
256 if (mask & ISR_TXINTR) {
257 struct aaci_runtime *aacirun = &aaci->playback;
258 void *ptr;
259
260 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700261 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200262 writel(0, aacirun->base + AACI_IE);
263 return;
264 }
265
266 ptr = aacirun->ptr;
267 do {
268 unsigned int len = aacirun->fifosz;
269 u32 val;
270
271 if (aacirun->bytes <= 0) {
272 aacirun->bytes += aacirun->period;
273 aacirun->ptr = ptr;
274 spin_unlock(&aaci->lock);
275 snd_pcm_period_elapsed(aacirun->substream);
276 spin_lock(&aaci->lock);
277 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100278 if (!(aacirun->cr & CR_EN))
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200279 break;
280
281 val = readl(aacirun->base + AACI_SR);
282 if (!(val & SR_TXHE))
283 break;
284 if (!(val & SR_TXFE))
285 len >>= 1;
286
287 aacirun->bytes -= len;
288
289 /* writing 16 bytes at a time */
290 for ( ; len > 0; len -= 16) {
291 asm(
292 "ldmia %0!, {r0, r1, r2, r3}\n\t"
293 "stmia %1, {r0, r1, r2, r3}"
294 : "+r" (ptr)
295 : "r" (aacirun->fifo)
296 : "r0", "r1", "r2", "r3", "cc");
297
298 if (ptr >= aacirun->end)
299 ptr = aacirun->start;
300 }
301 } while (1);
302
303 aacirun->ptr = ptr;
304 }
305}
306
David Howells7d12e782006-10-05 14:55:46 +0100307static irqreturn_t aaci_irq(int irq, void *devid)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200308{
309 struct aaci *aaci = devid;
310 u32 mask;
311 int i;
312
313 spin_lock(&aaci->lock);
314 mask = readl(aaci->base + AACI_ALLINTS);
315 if (mask) {
316 u32 m = mask;
317 for (i = 0; i < 4; i++, m >>= 7) {
318 if (m & 0x7f) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100319 aaci_fifo_irq(aaci, i, m);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200320 }
321 }
322 }
323 spin_unlock(&aaci->lock);
324
325 return mask ? IRQ_HANDLED : IRQ_NONE;
326}
327
328
329
330/*
331 * ALSA support.
332 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100333static struct snd_pcm_hardware aaci_hw_info = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200334 .info = SNDRV_PCM_INFO_MMAP |
335 SNDRV_PCM_INFO_MMAP_VALID |
336 SNDRV_PCM_INFO_INTERLEAVED |
337 SNDRV_PCM_INFO_BLOCK_TRANSFER |
338 SNDRV_PCM_INFO_RESUME,
339
340 /*
341 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
342 * words. It also doesn't support 12-bit at all.
343 */
344 .formats = SNDRV_PCM_FMTBIT_S16_LE,
345
Russell King6ca867c2009-12-18 17:48:35 +0000346 /* rates are setup from the AC'97 codec */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200347 .channels_min = 2,
348 .channels_max = 6,
349 .buffer_bytes_max = 64 * 1024,
350 .period_bytes_min = 256,
351 .period_bytes_max = PAGE_SIZE,
352 .periods_min = 4,
353 .periods_max = PAGE_SIZE / 16,
354};
355
Kevin Hilman41762b82007-02-07 05:45:32 +0100356static int __aaci_pcm_open(struct aaci *aaci,
357 struct snd_pcm_substream *substream,
358 struct aaci_runtime *aacirun)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200359{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100360 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200361 int ret;
362
363 aacirun->substream = substream;
364 runtime->private_data = aacirun;
365 runtime->hw = aaci_hw_info;
Russell King6ca867c2009-12-18 17:48:35 +0000366 runtime->hw.rates = aacirun->pcm->rates;
367 snd_pcm_limit_hw_rates(runtime);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200368
369 /*
370 * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
371 * mode, each 32-bit word contains one sample. If we're in
372 * compact mode, each 32-bit word contains two samples, effectively
373 * halving the FIFO size. However, we don't know for sure which
374 * we'll be using at this point. We set this to the lower limit.
375 */
376 runtime->hw.fifo_size = aaci->fifosize * 2;
377
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700378 ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200379 DRIVER_NAME, aaci);
380 if (ret)
381 goto out;
382
383 return 0;
384
385 out:
386 return ret;
387}
388
389
390/*
391 * Common ALSA stuff
392 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100393static int aaci_pcm_close(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200394{
395 struct aaci *aaci = substream->private_data;
396 struct aaci_runtime *aacirun = substream->runtime->private_data;
397
Kevin Hilman41762b82007-02-07 05:45:32 +0100398 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200399
400 aacirun->substream = NULL;
401 free_irq(aaci->dev->irq[0], aaci);
402
403 return 0;
404}
405
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100406static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200407{
408 struct aaci_runtime *aacirun = substream->runtime->private_data;
409
410 /*
411 * This must not be called with the device enabled.
412 */
Kevin Hilman41762b82007-02-07 05:45:32 +0100413 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200414
415 if (aacirun->pcm_open)
416 snd_ac97_pcm_close(aacirun->pcm);
417 aacirun->pcm_open = 0;
418
419 /*
420 * Clear out the DMA and any allocated buffers.
421 */
Takashi Iwaid6797322009-11-26 15:08:54 +0100422 snd_pcm_lib_free_pages(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200423
424 return 0;
425}
426
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100427static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200428 struct aaci_runtime *aacirun,
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100429 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200430{
431 int err;
432
433 aaci_pcm_hw_free(substream);
Russell King4acd57c2009-11-29 16:39:52 +0000434 if (aacirun->pcm_open) {
435 snd_ac97_pcm_close(aacirun->pcm);
436 aacirun->pcm_open = 0;
437 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200438
Takashi Iwaid6797322009-11-26 15:08:54 +0100439 err = snd_pcm_lib_malloc_pages(substream,
440 params_buffer_bytes(params));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200441 if (err < 0)
442 goto out;
443
Takashi Iwaicf5bd652009-12-01 16:36:56 +0100444 err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
445 params_channels(params),
446 aacirun->pcm->r[0].slots);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200447 if (err)
448 goto out;
449
450 aacirun->pcm_open = 1;
451
452 out:
453 return err;
454}
455
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100456static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200457{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100458 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200459 struct aaci_runtime *aacirun = runtime->private_data;
460
461 aacirun->start = (void *)runtime->dma_area;
Russell King88cdca92009-11-23 09:44:10 +0100462 aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200463 aacirun->ptr = aacirun->start;
464 aacirun->period =
465 aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
466
467 return 0;
468}
469
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100470static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200471{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100472 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200473 struct aaci_runtime *aacirun = runtime->private_data;
474 ssize_t bytes = aacirun->ptr - aacirun->start;
475
476 return bytes_to_frames(runtime, bytes);
477}
478
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200479
480/*
481 * Playback specific ALSA stuff
482 */
483static const u32 channels_to_txmask[] = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100484 [2] = CR_SL3 | CR_SL4,
485 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
486 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200487};
488
489/*
490 * We can support two and four channel audio. Unfortunately
491 * six channel audio requires a non-standard channel ordering:
492 * 2 -> FL(3), FR(4)
493 * 4 -> FL(3), FR(4), SL(7), SR(8)
494 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
495 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
496 * This requires an ALSA configuration file to correct.
497 */
498static unsigned int channel_list[] = { 2, 4, 6 };
499
500static int
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100501aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200502{
503 struct aaci *aaci = rule->private;
504 unsigned int chan_mask = 1 << 0, slots;
505
506 /*
507 * pcms[0] is the our 5.1 PCM instance.
508 */
509 slots = aaci->ac97_bus->pcms[0].r[0].slots;
510 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
511 chan_mask |= 1 << 1;
512 if (slots & (1 << AC97_SLOT_LFE))
513 chan_mask |= 1 << 2;
514 }
515
516 return snd_interval_list(hw_param_interval(p, rule->var),
517 ARRAY_SIZE(channel_list), channel_list,
518 chan_mask);
519}
520
Kevin Hilman41762b82007-02-07 05:45:32 +0100521static int aaci_pcm_open(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200522{
523 struct aaci *aaci = substream->private_data;
524 int ret;
525
526 /*
527 * Add rule describing channel dependency.
528 */
529 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
530 SNDRV_PCM_HW_PARAM_CHANNELS,
531 aaci_rule_channels, aaci,
532 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
533 if (ret)
534 return ret;
535
Kevin Hilman41762b82007-02-07 05:45:32 +0100536 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
537 ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
538 } else {
539 ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
540 }
541 return ret;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200542}
543
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100544static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
545 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200546{
547 struct aaci *aaci = substream->private_data;
548 struct aaci_runtime *aacirun = substream->runtime->private_data;
549 unsigned int channels = params_channels(params);
550 int ret;
551
552 WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
553 !channels_to_txmask[channels]);
554
555 ret = aaci_pcm_hw_params(substream, aacirun, params);
556
557 /*
558 * Enable FIFO, compact mode, 16 bits per sample.
559 * FIXME: double rate slots?
560 */
561 if (ret >= 0) {
Kevin Hilman41762b82007-02-07 05:45:32 +0100562 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200563 aacirun->cr |= channels_to_txmask[channels];
564
565 aacirun->fifosz = aaci->fifosize * 4;
Kevin Hilman41762b82007-02-07 05:45:32 +0100566 if (aacirun->cr & CR_COMPACT)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200567 aacirun->fifosz >>= 1;
568 }
569 return ret;
570}
571
572static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
573{
574 u32 ie;
575
576 ie = readl(aacirun->base + AACI_IE);
577 ie &= ~(IE_URIE|IE_TXIE);
578 writel(ie, aacirun->base + AACI_IE);
Kevin Hilman41762b82007-02-07 05:45:32 +0100579 aacirun->cr &= ~CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200580 aaci_chan_wait_ready(aacirun);
581 writel(aacirun->cr, aacirun->base + AACI_TXCR);
582}
583
584static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
585{
586 u32 ie;
587
588 aaci_chan_wait_ready(aacirun);
Kevin Hilman41762b82007-02-07 05:45:32 +0100589 aacirun->cr |= CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200590
591 ie = readl(aacirun->base + AACI_IE);
592 ie |= IE_URIE | IE_TXIE;
593 writel(ie, aacirun->base + AACI_IE);
594 writel(aacirun->cr, aacirun->base + AACI_TXCR);
595}
596
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100597static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200598{
599 struct aaci *aaci = substream->private_data;
600 struct aaci_runtime *aacirun = substream->runtime->private_data;
601 unsigned long flags;
602 int ret = 0;
603
604 spin_lock_irqsave(&aaci->lock, flags);
605 switch (cmd) {
606 case SNDRV_PCM_TRIGGER_START:
607 aaci_pcm_playback_start(aacirun);
608 break;
609
610 case SNDRV_PCM_TRIGGER_RESUME:
611 aaci_pcm_playback_start(aacirun);
612 break;
613
614 case SNDRV_PCM_TRIGGER_STOP:
615 aaci_pcm_playback_stop(aacirun);
616 break;
617
618 case SNDRV_PCM_TRIGGER_SUSPEND:
619 aaci_pcm_playback_stop(aacirun);
620 break;
621
622 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
623 break;
624
625 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
626 break;
627
628 default:
629 ret = -EINVAL;
630 }
631 spin_unlock_irqrestore(&aaci->lock, flags);
632
633 return ret;
634}
635
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100636static struct snd_pcm_ops aaci_playback_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100637 .open = aaci_pcm_open,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200638 .close = aaci_pcm_close,
639 .ioctl = snd_pcm_lib_ioctl,
640 .hw_params = aaci_pcm_playback_hw_params,
641 .hw_free = aaci_pcm_hw_free,
642 .prepare = aaci_pcm_prepare,
643 .trigger = aaci_pcm_playback_trigger,
644 .pointer = aaci_pcm_pointer,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200645};
646
Russell King8a371842007-02-20 15:44:23 +0000647static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
648 struct snd_pcm_hw_params *params)
Kevin Hilman41762b82007-02-07 05:45:32 +0100649{
650 struct aaci *aaci = substream->private_data;
651 struct aaci_runtime *aacirun = substream->runtime->private_data;
652 int ret;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200653
Kevin Hilman41762b82007-02-07 05:45:32 +0100654 ret = aaci_pcm_hw_params(substream, aacirun, params);
655
656 if (ret >= 0) {
657 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
658
659 /* Line in record: slot 3 and 4 */
660 aacirun->cr |= CR_SL3 | CR_SL4;
661
662 aacirun->fifosz = aaci->fifosize * 4;
663
664 if (aacirun->cr & CR_COMPACT)
665 aacirun->fifosz >>= 1;
666 }
667 return ret;
668}
669
670static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
671{
672 u32 ie;
673
674 aaci_chan_wait_ready(aacirun);
675
676 ie = readl(aacirun->base + AACI_IE);
677 ie &= ~(IE_ORIE | IE_RXIE);
678 writel(ie, aacirun->base+AACI_IE);
679
680 aacirun->cr &= ~CR_EN;
681
682 writel(aacirun->cr, aacirun->base + AACI_RXCR);
683}
684
685static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
686{
687 u32 ie;
688
689 aaci_chan_wait_ready(aacirun);
690
691#ifdef DEBUG
692 /* RX Timeout value: bits 28:17 in RXCR */
693 aacirun->cr |= 0xf << 17;
694#endif
695
696 aacirun->cr |= CR_EN;
697 writel(aacirun->cr, aacirun->base + AACI_RXCR);
698
699 ie = readl(aacirun->base + AACI_IE);
700 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
701 writel(ie, aacirun->base + AACI_IE);
702}
703
Russell King8a371842007-02-20 15:44:23 +0000704static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
705{
Kevin Hilman41762b82007-02-07 05:45:32 +0100706 struct aaci *aaci = substream->private_data;
707 struct aaci_runtime *aacirun = substream->runtime->private_data;
708 unsigned long flags;
709 int ret = 0;
710
711 spin_lock_irqsave(&aaci->lock, flags);
712
713 switch (cmd) {
714 case SNDRV_PCM_TRIGGER_START:
715 aaci_pcm_capture_start(aacirun);
716 break;
717
718 case SNDRV_PCM_TRIGGER_RESUME:
719 aaci_pcm_capture_start(aacirun);
720 break;
721
722 case SNDRV_PCM_TRIGGER_STOP:
723 aaci_pcm_capture_stop(aacirun);
724 break;
725
726 case SNDRV_PCM_TRIGGER_SUSPEND:
727 aaci_pcm_capture_stop(aacirun);
728 break;
729
730 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
731 break;
732
733 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
734 break;
735
736 default:
737 ret = -EINVAL;
738 }
739
740 spin_unlock_irqrestore(&aaci->lock, flags);
741
742 return ret;
743}
744
Russell King8a371842007-02-20 15:44:23 +0000745static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
Kevin Hilman41762b82007-02-07 05:45:32 +0100746{
747 struct snd_pcm_runtime *runtime = substream->runtime;
748 struct aaci *aaci = substream->private_data;
749
750 aaci_pcm_prepare(substream);
751
752 /* allow changing of sample rate */
753 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
754 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
755 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
756
757 /* Record select: Mic: 0, Aux: 3, Line: 4 */
758 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
759
760 return 0;
761}
762
Russell King8a371842007-02-20 15:44:23 +0000763static struct snd_pcm_ops aaci_capture_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100764 .open = aaci_pcm_open,
765 .close = aaci_pcm_close,
766 .ioctl = snd_pcm_lib_ioctl,
767 .hw_params = aaci_pcm_capture_hw_params,
768 .hw_free = aaci_pcm_hw_free,
769 .prepare = aaci_pcm_capture_prepare,
770 .trigger = aaci_pcm_capture_trigger,
771 .pointer = aaci_pcm_pointer,
Kevin Hilman41762b82007-02-07 05:45:32 +0100772};
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200773
774/*
775 * Power Management.
776 */
777#ifdef CONFIG_PM
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100778static int aaci_do_suspend(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200779{
780 struct aaci *aaci = card->private_data;
Takashi Iwai792a6c52005-11-17 17:19:25 +0100781 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
782 snd_pcm_suspend_all(aaci->pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200783 return 0;
784}
785
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100786static int aaci_do_resume(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200787{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100788 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200789 return 0;
790}
791
Richard Purdiee36d3942005-09-16 19:27:53 -0700792static int aaci_suspend(struct amba_device *dev, pm_message_t state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200793{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100794 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200795 return card ? aaci_do_suspend(card) : 0;
796}
797
798static int aaci_resume(struct amba_device *dev)
799{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100800 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200801 return card ? aaci_do_resume(card) : 0;
802}
803#else
804#define aaci_do_suspend NULL
805#define aaci_do_resume NULL
806#define aaci_suspend NULL
807#define aaci_resume NULL
808#endif
809
810
811static struct ac97_pcm ac97_defs[] __devinitdata = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100812 [0] = { /* Front PCM */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200813 .exclusive = 1,
814 .r = {
815 [0] = {
816 .slots = (1 << AC97_SLOT_PCM_LEFT) |
817 (1 << AC97_SLOT_PCM_RIGHT) |
818 (1 << AC97_SLOT_PCM_CENTER) |
819 (1 << AC97_SLOT_PCM_SLEFT) |
820 (1 << AC97_SLOT_PCM_SRIGHT) |
821 (1 << AC97_SLOT_LFE),
822 },
823 },
824 },
825 [1] = { /* PCM in */
826 .stream = 1,
827 .exclusive = 1,
828 .r = {
829 [0] = {
830 .slots = (1 << AC97_SLOT_PCM_LEFT) |
831 (1 << AC97_SLOT_PCM_RIGHT),
832 },
833 },
834 },
835 [2] = { /* Mic in */
836 .stream = 1,
837 .exclusive = 1,
838 .r = {
839 [0] = {
840 .slots = (1 << AC97_SLOT_MIC),
841 },
842 },
843 }
844};
845
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100846static struct snd_ac97_bus_ops aaci_bus_ops = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200847 .write = aaci_ac97_write,
848 .read = aaci_ac97_read,
849};
850
851static int __devinit aaci_probe_ac97(struct aaci *aaci)
852{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100853 struct snd_ac97_template ac97_template;
854 struct snd_ac97_bus *ac97_bus;
855 struct snd_ac97 *ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200856 int ret;
857
Philby John29a4f2d2009-10-13 16:30:22 +0530858 writel(0, aaci->base + AC97_POWERDOWN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200859 /*
860 * Assert AACIRESET for 2us
861 */
862 writel(0, aaci->base + AACI_RESET);
863 udelay(2);
864 writel(RESET_NRST, aaci->base + AACI_RESET);
865
866 /*
867 * Give the AC'97 codec more than enough time
868 * to wake up. (42us = ~2 frames at 48kHz.)
869 */
870 udelay(42);
871
872 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
873 if (ret)
874 goto out;
875
876 ac97_bus->clock = 48000;
877 aaci->ac97_bus = ac97_bus;
878
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100879 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200880 ac97_template.private_data = aaci;
881 ac97_template.num = 0;
882 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
883
884 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
885 if (ret)
886 goto out;
Kevin Hilman41762b82007-02-07 05:45:32 +0100887 aaci->ac97 = ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200888
889 /*
890 * Disable AC97 PC Beep input on audio codecs.
891 */
892 if (ac97_is_audio(ac97))
893 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
894
895 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
896 if (ret)
897 goto out;
898
899 aaci->playback.pcm = &ac97_bus->pcms[0];
Kevin Hilman41762b82007-02-07 05:45:32 +0100900 aaci->capture.pcm = &ac97_bus->pcms[1];
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200901
902 out:
903 return ret;
904}
905
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100906static void aaci_free_card(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200907{
908 struct aaci *aaci = card->private_data;
909 if (aaci->base)
910 iounmap(aaci->base);
911}
912
913static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
914{
915 struct aaci *aaci;
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100916 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100917 int err;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200918
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100919 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
920 THIS_MODULE, sizeof(struct aaci), &card);
921 if (err < 0)
Takashi Iwai631e8ad2008-09-01 15:31:50 +0200922 return NULL;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200923
924 card->private_free = aaci_free_card;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200925
926 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
927 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
928 snprintf(card->longname, sizeof(card->longname),
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -0700929 "%s at 0x%016llx, irq %d",
930 card->shortname, (unsigned long long)dev->res.start,
931 dev->irq[0]);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200932
933 aaci = card->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100934 mutex_init(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200935 spin_lock_init(&aaci->lock);
936 aaci->card = card;
937 aaci->dev = dev;
938
939 /* Set MAINCR to allow slot 1 and 2 data IO */
940 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
941 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
942
943 return aaci;
944}
945
946static int __devinit aaci_init_pcm(struct aaci *aaci)
947{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100948 struct snd_pcm *pcm;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200949 int ret;
950
Kevin Hilman41762b82007-02-07 05:45:32 +0100951 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200952 if (ret == 0) {
953 aaci->pcm = pcm;
954 pcm->private_data = aaci;
955 pcm->info_flags = 0;
956
957 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
958
959 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
Kevin Hilman41762b82007-02-07 05:45:32 +0100960 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
Takashi Iwaid6797322009-11-26 15:08:54 +0100961 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaid4946432009-12-18 20:25:30 +0100962 NULL, 0, 64 * 1024);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200963 }
964
965 return ret;
966}
967
968static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
969{
Kevin Hilman41762b82007-02-07 05:45:32 +0100970 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200971 int i;
972
Kevin Hilman41762b82007-02-07 05:45:32 +0100973 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200974
Kevin Hilman41762b82007-02-07 05:45:32 +0100975 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
976 writel(0, aacirun->fifo);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200977
Kevin Hilman41762b82007-02-07 05:45:32 +0100978 writel(0, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200979
980 /*
981 * Re-initialise the AACI after the FIFO depth test, to
982 * ensure that the FIFOs are empty. Unfortunately, merely
983 * disabling the channel doesn't clear the FIFO.
984 */
985 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
986 writel(aaci->maincr, aaci->base + AACI_MAINCR);
987
988 /*
989 * If we hit 4096, we failed. Go back to the specified
990 * fifo depth.
991 */
992 if (i == 4096)
993 i = 8;
994
995 return i;
996}
997
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100998static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200999{
1000 struct aaci *aaci;
1001 int ret, i;
1002
1003 ret = amba_request_regions(dev, NULL);
1004 if (ret)
1005 return ret;
1006
1007 aaci = aaci_init_card(dev);
Takashi Iwai631e8ad2008-09-01 15:31:50 +02001008 if (!aaci) {
1009 ret = -ENOMEM;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001010 goto out;
1011 }
1012
Linus Walleijdc890c22009-06-07 23:27:31 +01001013 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001014 if (!aaci->base) {
1015 ret = -ENOMEM;
1016 goto out;
1017 }
1018
1019 /*
1020 * Playback uses AACI channel 0
1021 */
1022 aaci->playback.base = aaci->base + AACI_CSCH1;
1023 aaci->playback.fifo = aaci->base + AACI_DR1;
1024
Kevin Hilman41762b82007-02-07 05:45:32 +01001025 /*
1026 * Capture uses AACI channel 0
1027 */
1028 aaci->capture.base = aaci->base + AACI_CSCH1;
1029 aaci->capture.fifo = aaci->base + AACI_DR1;
1030
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001031 for (i = 0; i < 4; i++) {
viro@ZenIV.linux.org.uke12ba642005-09-06 02:06:57 +01001032 void __iomem *base = aaci->base + i * 0x14;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001033
1034 writel(0, base + AACI_IE);
1035 writel(0, base + AACI_TXCR);
1036 writel(0, base + AACI_RXCR);
1037 }
1038
1039 writel(0x1fff, aaci->base + AACI_INTCLR);
1040 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1041
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001042 ret = aaci_probe_ac97(aaci);
1043 if (ret)
1044 goto out;
1045
Catalin Marinasf27f2182006-02-01 19:25:58 +00001046 /*
1047 * Size the FIFOs (must be multiple of 16).
1048 */
1049 aaci->fifosize = aaci_size_fifo(aaci);
1050 if (aaci->fifosize & 15) {
1051 printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
1052 aaci->fifosize);
1053 ret = -ENODEV;
1054 goto out;
1055 }
1056
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001057 ret = aaci_init_pcm(aaci);
1058 if (ret)
1059 goto out;
1060
Takashi Iwaia76af192005-09-05 16:56:40 +02001061 snd_card_set_dev(aaci->card, &dev->dev);
1062
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001063 ret = snd_card_register(aaci->card);
1064 if (ret == 0) {
1065 dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
Kevin Hilman41762b82007-02-07 05:45:32 +01001066 aaci->fifosize);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001067 amba_set_drvdata(dev, aaci->card);
1068 return ret;
1069 }
1070
1071 out:
1072 if (aaci)
1073 snd_card_free(aaci->card);
1074 amba_release_regions(dev);
1075 return ret;
1076}
1077
1078static int __devexit aaci_remove(struct amba_device *dev)
1079{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001080 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001081
1082 amba_set_drvdata(dev, NULL);
1083
1084 if (card) {
1085 struct aaci *aaci = card->private_data;
1086 writel(0, aaci->base + AACI_MAINCR);
1087
1088 snd_card_free(card);
1089 amba_release_regions(dev);
1090 }
1091
1092 return 0;
1093}
1094
1095static struct amba_id aaci_ids[] = {
1096 {
1097 .id = 0x00041041,
1098 .mask = 0x000fffff,
1099 },
1100 { 0, 0 },
1101};
1102
1103static struct amba_driver aaci_driver = {
1104 .drv = {
1105 .name = DRIVER_NAME,
1106 },
1107 .probe = aaci_probe,
1108 .remove = __devexit_p(aaci_remove),
1109 .suspend = aaci_suspend,
1110 .resume = aaci_resume,
1111 .id_table = aaci_ids,
1112};
1113
1114static int __init aaci_init(void)
1115{
1116 return amba_driver_register(&aaci_driver);
1117}
1118
1119static void __exit aaci_exit(void)
1120{
1121 amba_driver_unregister(&aaci_driver);
1122}
1123
1124module_init(aaci_init);
1125module_exit(aaci_exit);
1126
1127MODULE_LICENSE("GPL");
1128MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");