blob: e59372887f3694020d744d68bd2a5cfd7f5a6e1c [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 Kingcb5a6ff2005-05-12 14:04:59 +020021
22#include <asm/io.h>
23#include <asm/irq.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010024#include <asm/sizes.h>
Russell Kingcb5a6ff2005-05-12 14:04:59 +020025
Russell Kingcb5a6ff2005-05-12 14:04:59 +020026#include <sound/core.h>
27#include <sound/initval.h>
28#include <sound/ac97_codec.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31
32#include "aaci.h"
Russell Kingcb5a6ff2005-05-12 14:04:59 +020033
34#define DRIVER_NAME "aaci-pl041"
35
36/*
37 * PM support is not complete. Turn it off.
38 */
39#undef CONFIG_PM
40
Takashi Iwaiceb9e472005-11-17 15:10:16 +010041static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020042{
43 u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
44
45 /*
46 * Ensure that the slot 1/2 RX registers are empty.
47 */
48 v = readl(aaci->base + AACI_SLFR);
49 if (v & SLFR_2RXV)
50 readl(aaci->base + AACI_SL2RX);
51 if (v & SLFR_1RXV)
52 readl(aaci->base + AACI_SL1RX);
53
54 writel(maincr, aaci->base + AACI_MAINCR);
55}
56
57/*
58 * P29:
59 * The recommended use of programming the external codec through slot 1
60 * and slot 2 data is to use the channels during setup routines and the
61 * slot register at any other time. The data written into slot 1, slot 2
62 * and slot 12 registers is transmitted only when their corresponding
63 * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
64 * register.
65 */
Kevin Hilman14d178a2007-02-07 05:46:47 +010066static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
67 unsigned short val)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020068{
69 struct aaci *aaci = ac97->private_data;
70 u32 v;
Kevin Hilman14d178a2007-02-07 05:46:47 +010071 int timeout = 5000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +020072
73 if (ac97->num >= 4)
74 return;
75
Ingo Molnar12aa7572006-01-16 16:36:05 +010076 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020077
78 aaci_ac97_select_codec(aaci, ac97);
79
80 /*
81 * P54: You must ensure that AACI_SL2TX is always written
82 * to, if required, before data is written to AACI_SL1TX.
83 */
84 writel(val << 4, aaci->base + AACI_SL2TX);
85 writel(reg << 12, aaci->base + AACI_SL1TX);
86
87 /*
88 * Wait for the transmission of both slots to complete.
89 */
90 do {
91 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +010092 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +010093
94 if (!timeout)
95 dev_err(&aaci->dev->dev,
96 "timeout waiting for write to complete\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +020097
Ingo Molnar12aa7572006-01-16 16:36:05 +010098 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020099}
100
101/*
102 * Read an AC'97 register.
103 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100104static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200105{
106 struct aaci *aaci = ac97->private_data;
107 u32 v;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100108 int timeout = 5000;
109 int retries = 10;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200110
111 if (ac97->num >= 4)
112 return ~0;
113
Ingo Molnar12aa7572006-01-16 16:36:05 +0100114 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200115
116 aaci_ac97_select_codec(aaci, ac97);
117
118 /*
119 * Write the register address to slot 1.
120 */
121 writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);
122
123 /*
124 * Wait for the transmission to complete.
125 */
126 do {
127 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100128 } while ((v & SLFR_1TXB) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +0100129
130 if (!timeout) {
131 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
132 v = ~0;
133 goto out;
134 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200135
136 /*
137 * Give the AC'97 codec more than enough time
138 * to respond. (42us = ~2 frames at 48kHz.)
139 */
140 udelay(42);
141
142 /*
143 * Wait for slot 2 to indicate data.
144 */
Kevin Hilman14d178a2007-02-07 05:46:47 +0100145 timeout = 5000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200146 do {
147 cond_resched();
148 v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100149 } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200150
Kevin Hilman14d178a2007-02-07 05:46:47 +0100151 if (!timeout) {
152 dev_err(&aaci->dev->dev, "timeout on RX valid\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200153 v = ~0;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100154 goto out;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200155 }
156
Kevin Hilman14d178a2007-02-07 05:46:47 +0100157 do {
158 v = readl(aaci->base + AACI_SL1RX) >> 12;
159 if (v == reg) {
160 v = readl(aaci->base + AACI_SL2RX) >> 4;
161 break;
162 } else if (--retries) {
163 dev_warn(&aaci->dev->dev,
164 "ac97 read back fail. retry\n");
165 continue;
166 } else {
167 dev_warn(&aaci->dev->dev,
168 "wrong ac97 register read back (%x != %x)\n",
169 v, reg);
170 v = ~0;
171 }
172 } while (retries);
173 out:
Ingo Molnar12aa7572006-01-16 16:36:05 +0100174 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200175 return v;
176}
177
178static inline void aaci_chan_wait_ready(struct aaci_runtime *aacirun)
179{
180 u32 val;
181 int timeout = 5000;
182
183 do {
184 val = readl(aacirun->base + AACI_SR);
185 } while (val & (SR_TXB|SR_RXB) && timeout--);
186}
187
188
189
190/*
191 * Interrupt support.
192 */
Kevin Hilman62578cb2007-02-07 05:41:37 +0100193static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200194{
Kevin Hilman41762b82007-02-07 05:45:32 +0100195 if (mask & ISR_ORINTR) {
196 dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
197 writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
198 }
199
200 if (mask & ISR_RXTOINTR) {
201 dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
202 writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
203 }
204
205 if (mask & ISR_RXINTR) {
206 struct aaci_runtime *aacirun = &aaci->capture;
207 void *ptr;
208
209 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700210 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
Kevin Hilman41762b82007-02-07 05:45:32 +0100211 writel(0, aacirun->base + AACI_IE);
212 return;
213 }
214 ptr = aacirun->ptr;
215
216 do {
217 unsigned int len = aacirun->fifosz;
218 u32 val;
219
220 if (aacirun->bytes <= 0) {
221 aacirun->bytes += aacirun->period;
222 aacirun->ptr = ptr;
223 spin_unlock(&aaci->lock);
224 snd_pcm_period_elapsed(aacirun->substream);
225 spin_lock(&aaci->lock);
226 }
227 if (!(aacirun->cr & CR_EN))
228 break;
229
230 val = readl(aacirun->base + AACI_SR);
231 if (!(val & SR_RXHF))
232 break;
233 if (!(val & SR_RXFF))
234 len >>= 1;
235
236 aacirun->bytes -= len;
237
238 /* reading 16 bytes at a time */
239 for( ; len > 0; len -= 16) {
240 asm(
241 "ldmia %1, {r0, r1, r2, r3}\n\t"
242 "stmia %0!, {r0, r1, r2, r3}"
243 : "+r" (ptr)
244 : "r" (aacirun->fifo)
245 : "r0", "r1", "r2", "r3", "cc");
246
247 if (ptr >= aacirun->end)
248 ptr = aacirun->start;
249 }
250 } while(1);
251 aacirun->ptr = ptr;
252 }
253
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200254 if (mask & ISR_URINTR) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100255 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
256 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200257 }
258
259 if (mask & ISR_TXINTR) {
260 struct aaci_runtime *aacirun = &aaci->playback;
261 void *ptr;
262
263 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700264 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200265 writel(0, aacirun->base + AACI_IE);
266 return;
267 }
268
269 ptr = aacirun->ptr;
270 do {
271 unsigned int len = aacirun->fifosz;
272 u32 val;
273
274 if (aacirun->bytes <= 0) {
275 aacirun->bytes += aacirun->period;
276 aacirun->ptr = ptr;
277 spin_unlock(&aaci->lock);
278 snd_pcm_period_elapsed(aacirun->substream);
279 spin_lock(&aaci->lock);
280 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100281 if (!(aacirun->cr & CR_EN))
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200282 break;
283
284 val = readl(aacirun->base + AACI_SR);
285 if (!(val & SR_TXHE))
286 break;
287 if (!(val & SR_TXFE))
288 len >>= 1;
289
290 aacirun->bytes -= len;
291
292 /* writing 16 bytes at a time */
293 for ( ; len > 0; len -= 16) {
294 asm(
295 "ldmia %0!, {r0, r1, r2, r3}\n\t"
296 "stmia %1, {r0, r1, r2, r3}"
297 : "+r" (ptr)
298 : "r" (aacirun->fifo)
299 : "r0", "r1", "r2", "r3", "cc");
300
301 if (ptr >= aacirun->end)
302 ptr = aacirun->start;
303 }
304 } while (1);
305
306 aacirun->ptr = ptr;
307 }
308}
309
David Howells7d12e782006-10-05 14:55:46 +0100310static irqreturn_t aaci_irq(int irq, void *devid)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200311{
312 struct aaci *aaci = devid;
313 u32 mask;
314 int i;
315
316 spin_lock(&aaci->lock);
317 mask = readl(aaci->base + AACI_ALLINTS);
318 if (mask) {
319 u32 m = mask;
320 for (i = 0; i < 4; i++, m >>= 7) {
321 if (m & 0x7f) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100322 aaci_fifo_irq(aaci, i, m);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200323 }
324 }
325 }
326 spin_unlock(&aaci->lock);
327
328 return mask ? IRQ_HANDLED : IRQ_NONE;
329}
330
331
332
333/*
334 * ALSA support.
335 */
336
337struct aaci_stream {
338 unsigned char codec_idx;
339 unsigned char rate_idx;
340};
341
342static struct aaci_stream aaci_streams[] = {
343 [ACSTREAM_FRONT] = {
344 .codec_idx = 0,
345 .rate_idx = AC97_RATES_FRONT_DAC,
346 },
347 [ACSTREAM_SURROUND] = {
348 .codec_idx = 0,
349 .rate_idx = AC97_RATES_SURR_DAC,
350 },
351 [ACSTREAM_LFE] = {
352 .codec_idx = 0,
353 .rate_idx = AC97_RATES_LFE_DAC,
354 },
355};
356
357static inline unsigned int aaci_rate_mask(struct aaci *aaci, int streamid)
358{
359 struct aaci_stream *s = aaci_streams + streamid;
360 return aaci->ac97_bus->codec[s->codec_idx]->rates[s->rate_idx];
361}
362
363static unsigned int rate_list[] = {
364 5512, 8000, 11025, 16000, 22050, 32000, 44100,
365 48000, 64000, 88200, 96000, 176400, 192000
366};
367
368/*
369 * Double-rate rule: we can support double rate iff channels == 2
370 * (unimplemented)
371 */
372static int
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100373aaci_rule_rate_by_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200374{
375 struct aaci *aaci = rule->private;
376 unsigned int rate_mask = SNDRV_PCM_RATE_8000_48000|SNDRV_PCM_RATE_5512;
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100377 struct snd_interval *c = hw_param_interval(p, SNDRV_PCM_HW_PARAM_CHANNELS);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200378
379 switch (c->max) {
380 case 6:
381 rate_mask &= aaci_rate_mask(aaci, ACSTREAM_LFE);
382 case 4:
383 rate_mask &= aaci_rate_mask(aaci, ACSTREAM_SURROUND);
384 case 2:
385 rate_mask &= aaci_rate_mask(aaci, ACSTREAM_FRONT);
386 }
387
388 return snd_interval_list(hw_param_interval(p, rule->var),
389 ARRAY_SIZE(rate_list), rate_list,
390 rate_mask);
391}
392
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100393static struct snd_pcm_hardware aaci_hw_info = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200394 .info = SNDRV_PCM_INFO_MMAP |
395 SNDRV_PCM_INFO_MMAP_VALID |
396 SNDRV_PCM_INFO_INTERLEAVED |
397 SNDRV_PCM_INFO_BLOCK_TRANSFER |
398 SNDRV_PCM_INFO_RESUME,
399
400 /*
401 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
402 * words. It also doesn't support 12-bit at all.
403 */
404 .formats = SNDRV_PCM_FMTBIT_S16_LE,
405
406 /* should this be continuous or knot? */
407 .rates = SNDRV_PCM_RATE_CONTINUOUS,
408 .rate_max = 48000,
409 .rate_min = 4000,
410 .channels_min = 2,
411 .channels_max = 6,
412 .buffer_bytes_max = 64 * 1024,
413 .period_bytes_min = 256,
414 .period_bytes_max = PAGE_SIZE,
415 .periods_min = 4,
416 .periods_max = PAGE_SIZE / 16,
417};
418
Kevin Hilman41762b82007-02-07 05:45:32 +0100419static int __aaci_pcm_open(struct aaci *aaci,
420 struct snd_pcm_substream *substream,
421 struct aaci_runtime *aacirun)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200422{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100423 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200424 int ret;
425
426 aacirun->substream = substream;
427 runtime->private_data = aacirun;
428 runtime->hw = aaci_hw_info;
429
430 /*
431 * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
432 * mode, each 32-bit word contains one sample. If we're in
433 * compact mode, each 32-bit word contains two samples, effectively
434 * halving the FIFO size. However, we don't know for sure which
435 * we'll be using at this point. We set this to the lower limit.
436 */
437 runtime->hw.fifo_size = aaci->fifosize * 2;
438
439 /*
440 * Add rule describing hardware rate dependency
441 * on the number of channels.
442 */
443 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
444 aaci_rule_rate_by_channels, aaci,
445 SNDRV_PCM_HW_PARAM_CHANNELS,
446 SNDRV_PCM_HW_PARAM_RATE, -1);
447 if (ret)
448 goto out;
449
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700450 ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200451 DRIVER_NAME, aaci);
452 if (ret)
453 goto out;
454
455 return 0;
456
457 out:
458 return ret;
459}
460
461
462/*
463 * Common ALSA stuff
464 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100465static int aaci_pcm_close(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200466{
467 struct aaci *aaci = substream->private_data;
468 struct aaci_runtime *aacirun = substream->runtime->private_data;
469
Kevin Hilman41762b82007-02-07 05:45:32 +0100470 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200471
472 aacirun->substream = NULL;
473 free_irq(aaci->dev->irq[0], aaci);
474
475 return 0;
476}
477
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100478static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200479{
480 struct aaci_runtime *aacirun = substream->runtime->private_data;
481
482 /*
483 * This must not be called with the device enabled.
484 */
Kevin Hilman41762b82007-02-07 05:45:32 +0100485 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200486
487 if (aacirun->pcm_open)
488 snd_ac97_pcm_close(aacirun->pcm);
489 aacirun->pcm_open = 0;
490
491 /*
492 * Clear out the DMA and any allocated buffers.
493 */
Takashi Iwaid6797322009-11-26 15:08:54 +0100494 snd_pcm_lib_free_pages(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200495
496 return 0;
497}
498
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100499static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200500 struct aaci_runtime *aacirun,
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100501 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200502{
503 int err;
504
505 aaci_pcm_hw_free(substream);
506
Takashi Iwaid6797322009-11-26 15:08:54 +0100507 err = snd_pcm_lib_malloc_pages(substream,
508 params_buffer_bytes(params));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200509 if (err < 0)
510 goto out;
511
Kevin Hilman41762b82007-02-07 05:45:32 +0100512 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
513 err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
514 params_channels(params),
515 aacirun->pcm->r[0].slots);
516 else
517 err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
518 params_channels(params),
519 aacirun->pcm->r[1].slots);
520
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200521 if (err)
522 goto out;
523
524 aacirun->pcm_open = 1;
525
526 out:
527 return err;
528}
529
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100530static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200531{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100532 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200533 struct aaci_runtime *aacirun = runtime->private_data;
534
535 aacirun->start = (void *)runtime->dma_area;
536 aacirun->end = aacirun->start + runtime->dma_bytes;
537 aacirun->ptr = aacirun->start;
538 aacirun->period =
539 aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
540
541 return 0;
542}
543
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100544static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200545{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100546 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200547 struct aaci_runtime *aacirun = runtime->private_data;
548 ssize_t bytes = aacirun->ptr - aacirun->start;
549
550 return bytes_to_frames(runtime, bytes);
551}
552
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200553
554/*
555 * Playback specific ALSA stuff
556 */
557static const u32 channels_to_txmask[] = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100558 [2] = CR_SL3 | CR_SL4,
559 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
560 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200561};
562
563/*
564 * We can support two and four channel audio. Unfortunately
565 * six channel audio requires a non-standard channel ordering:
566 * 2 -> FL(3), FR(4)
567 * 4 -> FL(3), FR(4), SL(7), SR(8)
568 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
569 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
570 * This requires an ALSA configuration file to correct.
571 */
572static unsigned int channel_list[] = { 2, 4, 6 };
573
574static int
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100575aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200576{
577 struct aaci *aaci = rule->private;
578 unsigned int chan_mask = 1 << 0, slots;
579
580 /*
581 * pcms[0] is the our 5.1 PCM instance.
582 */
583 slots = aaci->ac97_bus->pcms[0].r[0].slots;
584 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
585 chan_mask |= 1 << 1;
586 if (slots & (1 << AC97_SLOT_LFE))
587 chan_mask |= 1 << 2;
588 }
589
590 return snd_interval_list(hw_param_interval(p, rule->var),
591 ARRAY_SIZE(channel_list), channel_list,
592 chan_mask);
593}
594
Kevin Hilman41762b82007-02-07 05:45:32 +0100595static int aaci_pcm_open(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200596{
597 struct aaci *aaci = substream->private_data;
598 int ret;
599
600 /*
601 * Add rule describing channel dependency.
602 */
603 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
604 SNDRV_PCM_HW_PARAM_CHANNELS,
605 aaci_rule_channels, aaci,
606 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
607 if (ret)
608 return ret;
609
Kevin Hilman41762b82007-02-07 05:45:32 +0100610 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
611 ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
612 } else {
613 ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
614 }
615 return ret;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200616}
617
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100618static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
619 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200620{
621 struct aaci *aaci = substream->private_data;
622 struct aaci_runtime *aacirun = substream->runtime->private_data;
623 unsigned int channels = params_channels(params);
624 int ret;
625
626 WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
627 !channels_to_txmask[channels]);
628
629 ret = aaci_pcm_hw_params(substream, aacirun, params);
630
631 /*
632 * Enable FIFO, compact mode, 16 bits per sample.
633 * FIXME: double rate slots?
634 */
635 if (ret >= 0) {
Kevin Hilman41762b82007-02-07 05:45:32 +0100636 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200637 aacirun->cr |= channels_to_txmask[channels];
638
639 aacirun->fifosz = aaci->fifosize * 4;
Kevin Hilman41762b82007-02-07 05:45:32 +0100640 if (aacirun->cr & CR_COMPACT)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200641 aacirun->fifosz >>= 1;
642 }
643 return ret;
644}
645
646static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
647{
648 u32 ie;
649
650 ie = readl(aacirun->base + AACI_IE);
651 ie &= ~(IE_URIE|IE_TXIE);
652 writel(ie, aacirun->base + AACI_IE);
Kevin Hilman41762b82007-02-07 05:45:32 +0100653 aacirun->cr &= ~CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200654 aaci_chan_wait_ready(aacirun);
655 writel(aacirun->cr, aacirun->base + AACI_TXCR);
656}
657
658static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
659{
660 u32 ie;
661
662 aaci_chan_wait_ready(aacirun);
Kevin Hilman41762b82007-02-07 05:45:32 +0100663 aacirun->cr |= CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200664
665 ie = readl(aacirun->base + AACI_IE);
666 ie |= IE_URIE | IE_TXIE;
667 writel(ie, aacirun->base + AACI_IE);
668 writel(aacirun->cr, aacirun->base + AACI_TXCR);
669}
670
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100671static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200672{
673 struct aaci *aaci = substream->private_data;
674 struct aaci_runtime *aacirun = substream->runtime->private_data;
675 unsigned long flags;
676 int ret = 0;
677
678 spin_lock_irqsave(&aaci->lock, flags);
679 switch (cmd) {
680 case SNDRV_PCM_TRIGGER_START:
681 aaci_pcm_playback_start(aacirun);
682 break;
683
684 case SNDRV_PCM_TRIGGER_RESUME:
685 aaci_pcm_playback_start(aacirun);
686 break;
687
688 case SNDRV_PCM_TRIGGER_STOP:
689 aaci_pcm_playback_stop(aacirun);
690 break;
691
692 case SNDRV_PCM_TRIGGER_SUSPEND:
693 aaci_pcm_playback_stop(aacirun);
694 break;
695
696 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
697 break;
698
699 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
700 break;
701
702 default:
703 ret = -EINVAL;
704 }
705 spin_unlock_irqrestore(&aaci->lock, flags);
706
707 return ret;
708}
709
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100710static struct snd_pcm_ops aaci_playback_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100711 .open = aaci_pcm_open,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200712 .close = aaci_pcm_close,
713 .ioctl = snd_pcm_lib_ioctl,
714 .hw_params = aaci_pcm_playback_hw_params,
715 .hw_free = aaci_pcm_hw_free,
716 .prepare = aaci_pcm_prepare,
717 .trigger = aaci_pcm_playback_trigger,
718 .pointer = aaci_pcm_pointer,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200719};
720
Russell King8a371842007-02-20 15:44:23 +0000721static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
722 struct snd_pcm_hw_params *params)
Kevin Hilman41762b82007-02-07 05:45:32 +0100723{
724 struct aaci *aaci = substream->private_data;
725 struct aaci_runtime *aacirun = substream->runtime->private_data;
726 int ret;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200727
Kevin Hilman41762b82007-02-07 05:45:32 +0100728 ret = aaci_pcm_hw_params(substream, aacirun, params);
729
730 if (ret >= 0) {
731 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
732
733 /* Line in record: slot 3 and 4 */
734 aacirun->cr |= CR_SL3 | CR_SL4;
735
736 aacirun->fifosz = aaci->fifosize * 4;
737
738 if (aacirun->cr & CR_COMPACT)
739 aacirun->fifosz >>= 1;
740 }
741 return ret;
742}
743
744static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
745{
746 u32 ie;
747
748 aaci_chan_wait_ready(aacirun);
749
750 ie = readl(aacirun->base + AACI_IE);
751 ie &= ~(IE_ORIE | IE_RXIE);
752 writel(ie, aacirun->base+AACI_IE);
753
754 aacirun->cr &= ~CR_EN;
755
756 writel(aacirun->cr, aacirun->base + AACI_RXCR);
757}
758
759static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
760{
761 u32 ie;
762
763 aaci_chan_wait_ready(aacirun);
764
765#ifdef DEBUG
766 /* RX Timeout value: bits 28:17 in RXCR */
767 aacirun->cr |= 0xf << 17;
768#endif
769
770 aacirun->cr |= CR_EN;
771 writel(aacirun->cr, aacirun->base + AACI_RXCR);
772
773 ie = readl(aacirun->base + AACI_IE);
774 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
775 writel(ie, aacirun->base + AACI_IE);
776}
777
Russell King8a371842007-02-20 15:44:23 +0000778static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
779{
Kevin Hilman41762b82007-02-07 05:45:32 +0100780 struct aaci *aaci = substream->private_data;
781 struct aaci_runtime *aacirun = substream->runtime->private_data;
782 unsigned long flags;
783 int ret = 0;
784
785 spin_lock_irqsave(&aaci->lock, flags);
786
787 switch (cmd) {
788 case SNDRV_PCM_TRIGGER_START:
789 aaci_pcm_capture_start(aacirun);
790 break;
791
792 case SNDRV_PCM_TRIGGER_RESUME:
793 aaci_pcm_capture_start(aacirun);
794 break;
795
796 case SNDRV_PCM_TRIGGER_STOP:
797 aaci_pcm_capture_stop(aacirun);
798 break;
799
800 case SNDRV_PCM_TRIGGER_SUSPEND:
801 aaci_pcm_capture_stop(aacirun);
802 break;
803
804 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
805 break;
806
807 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
808 break;
809
810 default:
811 ret = -EINVAL;
812 }
813
814 spin_unlock_irqrestore(&aaci->lock, flags);
815
816 return ret;
817}
818
Russell King8a371842007-02-20 15:44:23 +0000819static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
Kevin Hilman41762b82007-02-07 05:45:32 +0100820{
821 struct snd_pcm_runtime *runtime = substream->runtime;
822 struct aaci *aaci = substream->private_data;
823
824 aaci_pcm_prepare(substream);
825
826 /* allow changing of sample rate */
827 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
828 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
829 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
830
831 /* Record select: Mic: 0, Aux: 3, Line: 4 */
832 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
833
834 return 0;
835}
836
Russell King8a371842007-02-20 15:44:23 +0000837static struct snd_pcm_ops aaci_capture_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100838 .open = aaci_pcm_open,
839 .close = aaci_pcm_close,
840 .ioctl = snd_pcm_lib_ioctl,
841 .hw_params = aaci_pcm_capture_hw_params,
842 .hw_free = aaci_pcm_hw_free,
843 .prepare = aaci_pcm_capture_prepare,
844 .trigger = aaci_pcm_capture_trigger,
845 .pointer = aaci_pcm_pointer,
Kevin Hilman41762b82007-02-07 05:45:32 +0100846};
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200847
848/*
849 * Power Management.
850 */
851#ifdef CONFIG_PM
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100852static int aaci_do_suspend(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200853{
854 struct aaci *aaci = card->private_data;
Takashi Iwai792a6c52005-11-17 17:19:25 +0100855 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
856 snd_pcm_suspend_all(aaci->pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200857 return 0;
858}
859
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100860static int aaci_do_resume(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200861{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100862 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200863 return 0;
864}
865
Richard Purdiee36d3942005-09-16 19:27:53 -0700866static int aaci_suspend(struct amba_device *dev, pm_message_t state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200867{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100868 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200869 return card ? aaci_do_suspend(card) : 0;
870}
871
872static int aaci_resume(struct amba_device *dev)
873{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100874 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200875 return card ? aaci_do_resume(card) : 0;
876}
877#else
878#define aaci_do_suspend NULL
879#define aaci_do_resume NULL
880#define aaci_suspend NULL
881#define aaci_resume NULL
882#endif
883
884
885static struct ac97_pcm ac97_defs[] __devinitdata = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100886 [0] = { /* Front PCM */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200887 .exclusive = 1,
888 .r = {
889 [0] = {
890 .slots = (1 << AC97_SLOT_PCM_LEFT) |
891 (1 << AC97_SLOT_PCM_RIGHT) |
892 (1 << AC97_SLOT_PCM_CENTER) |
893 (1 << AC97_SLOT_PCM_SLEFT) |
894 (1 << AC97_SLOT_PCM_SRIGHT) |
895 (1 << AC97_SLOT_LFE),
896 },
897 },
898 },
899 [1] = { /* PCM in */
900 .stream = 1,
901 .exclusive = 1,
902 .r = {
903 [0] = {
904 .slots = (1 << AC97_SLOT_PCM_LEFT) |
905 (1 << AC97_SLOT_PCM_RIGHT),
906 },
907 },
908 },
909 [2] = { /* Mic in */
910 .stream = 1,
911 .exclusive = 1,
912 .r = {
913 [0] = {
914 .slots = (1 << AC97_SLOT_MIC),
915 },
916 },
917 }
918};
919
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100920static struct snd_ac97_bus_ops aaci_bus_ops = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200921 .write = aaci_ac97_write,
922 .read = aaci_ac97_read,
923};
924
925static int __devinit aaci_probe_ac97(struct aaci *aaci)
926{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100927 struct snd_ac97_template ac97_template;
928 struct snd_ac97_bus *ac97_bus;
929 struct snd_ac97 *ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200930 int ret;
931
Philby John29a4f2d2009-10-13 16:30:22 +0530932 writel(0, aaci->base + AC97_POWERDOWN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200933 /*
934 * Assert AACIRESET for 2us
935 */
936 writel(0, aaci->base + AACI_RESET);
937 udelay(2);
938 writel(RESET_NRST, aaci->base + AACI_RESET);
939
940 /*
941 * Give the AC'97 codec more than enough time
942 * to wake up. (42us = ~2 frames at 48kHz.)
943 */
944 udelay(42);
945
946 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
947 if (ret)
948 goto out;
949
950 ac97_bus->clock = 48000;
951 aaci->ac97_bus = ac97_bus;
952
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100953 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200954 ac97_template.private_data = aaci;
955 ac97_template.num = 0;
956 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
957
958 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
959 if (ret)
960 goto out;
Kevin Hilman41762b82007-02-07 05:45:32 +0100961 aaci->ac97 = ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200962
963 /*
964 * Disable AC97 PC Beep input on audio codecs.
965 */
966 if (ac97_is_audio(ac97))
967 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
968
969 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
970 if (ret)
971 goto out;
972
973 aaci->playback.pcm = &ac97_bus->pcms[0];
Kevin Hilman41762b82007-02-07 05:45:32 +0100974 aaci->capture.pcm = &ac97_bus->pcms[1];
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200975
976 out:
977 return ret;
978}
979
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100980static void aaci_free_card(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200981{
982 struct aaci *aaci = card->private_data;
983 if (aaci->base)
984 iounmap(aaci->base);
985}
986
987static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
988{
989 struct aaci *aaci;
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100990 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100991 int err;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200992
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100993 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
994 THIS_MODULE, sizeof(struct aaci), &card);
995 if (err < 0)
Takashi Iwai631e8ad2008-09-01 15:31:50 +0200996 return NULL;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200997
998 card->private_free = aaci_free_card;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200999
1000 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
1001 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
1002 snprintf(card->longname, sizeof(card->longname),
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001003 "%s at 0x%016llx, irq %d",
1004 card->shortname, (unsigned long long)dev->res.start,
1005 dev->irq[0]);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001006
1007 aaci = card->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +01001008 mutex_init(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001009 spin_lock_init(&aaci->lock);
1010 aaci->card = card;
1011 aaci->dev = dev;
1012
1013 /* Set MAINCR to allow slot 1 and 2 data IO */
1014 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
1015 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
1016
1017 return aaci;
1018}
1019
1020static int __devinit aaci_init_pcm(struct aaci *aaci)
1021{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001022 struct snd_pcm *pcm;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001023 int ret;
1024
Kevin Hilman41762b82007-02-07 05:45:32 +01001025 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001026 if (ret == 0) {
1027 aaci->pcm = pcm;
1028 pcm->private_data = aaci;
1029 pcm->info_flags = 0;
1030
1031 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
1032
1033 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
Kevin Hilman41762b82007-02-07 05:45:32 +01001034 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
Takashi Iwaid6797322009-11-26 15:08:54 +01001035 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1036 NULL, 0, 64 * 104);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001037 }
1038
1039 return ret;
1040}
1041
1042static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
1043{
Kevin Hilman41762b82007-02-07 05:45:32 +01001044 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001045 int i;
1046
Kevin Hilman41762b82007-02-07 05:45:32 +01001047 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001048
Kevin Hilman41762b82007-02-07 05:45:32 +01001049 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
1050 writel(0, aacirun->fifo);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001051
Kevin Hilman41762b82007-02-07 05:45:32 +01001052 writel(0, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001053
1054 /*
1055 * Re-initialise the AACI after the FIFO depth test, to
1056 * ensure that the FIFOs are empty. Unfortunately, merely
1057 * disabling the channel doesn't clear the FIFO.
1058 */
1059 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
1060 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1061
1062 /*
1063 * If we hit 4096, we failed. Go back to the specified
1064 * fifo depth.
1065 */
1066 if (i == 4096)
1067 i = 8;
1068
1069 return i;
1070}
1071
Alessandro Rubini03fbdb12009-05-20 22:39:08 +01001072static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001073{
1074 struct aaci *aaci;
1075 int ret, i;
1076
1077 ret = amba_request_regions(dev, NULL);
1078 if (ret)
1079 return ret;
1080
1081 aaci = aaci_init_card(dev);
Takashi Iwai631e8ad2008-09-01 15:31:50 +02001082 if (!aaci) {
1083 ret = -ENOMEM;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001084 goto out;
1085 }
1086
Linus Walleijdc890c22009-06-07 23:27:31 +01001087 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001088 if (!aaci->base) {
1089 ret = -ENOMEM;
1090 goto out;
1091 }
1092
1093 /*
1094 * Playback uses AACI channel 0
1095 */
1096 aaci->playback.base = aaci->base + AACI_CSCH1;
1097 aaci->playback.fifo = aaci->base + AACI_DR1;
1098
Kevin Hilman41762b82007-02-07 05:45:32 +01001099 /*
1100 * Capture uses AACI channel 0
1101 */
1102 aaci->capture.base = aaci->base + AACI_CSCH1;
1103 aaci->capture.fifo = aaci->base + AACI_DR1;
1104
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001105 for (i = 0; i < 4; i++) {
viro@ZenIV.linux.org.uke12ba642005-09-06 02:06:57 +01001106 void __iomem *base = aaci->base + i * 0x14;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001107
1108 writel(0, base + AACI_IE);
1109 writel(0, base + AACI_TXCR);
1110 writel(0, base + AACI_RXCR);
1111 }
1112
1113 writel(0x1fff, aaci->base + AACI_INTCLR);
1114 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1115
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001116 ret = aaci_probe_ac97(aaci);
1117 if (ret)
1118 goto out;
1119
Catalin Marinasf27f2182006-02-01 19:25:58 +00001120 /*
1121 * Size the FIFOs (must be multiple of 16).
1122 */
1123 aaci->fifosize = aaci_size_fifo(aaci);
1124 if (aaci->fifosize & 15) {
1125 printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
1126 aaci->fifosize);
1127 ret = -ENODEV;
1128 goto out;
1129 }
1130
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001131 ret = aaci_init_pcm(aaci);
1132 if (ret)
1133 goto out;
1134
Takashi Iwaia76af192005-09-05 16:56:40 +02001135 snd_card_set_dev(aaci->card, &dev->dev);
1136
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001137 ret = snd_card_register(aaci->card);
1138 if (ret == 0) {
1139 dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
Kevin Hilman41762b82007-02-07 05:45:32 +01001140 aaci->fifosize);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001141 amba_set_drvdata(dev, aaci->card);
1142 return ret;
1143 }
1144
1145 out:
1146 if (aaci)
1147 snd_card_free(aaci->card);
1148 amba_release_regions(dev);
1149 return ret;
1150}
1151
1152static int __devexit aaci_remove(struct amba_device *dev)
1153{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001154 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001155
1156 amba_set_drvdata(dev, NULL);
1157
1158 if (card) {
1159 struct aaci *aaci = card->private_data;
1160 writel(0, aaci->base + AACI_MAINCR);
1161
1162 snd_card_free(card);
1163 amba_release_regions(dev);
1164 }
1165
1166 return 0;
1167}
1168
1169static struct amba_id aaci_ids[] = {
1170 {
1171 .id = 0x00041041,
1172 .mask = 0x000fffff,
1173 },
1174 { 0, 0 },
1175};
1176
1177static struct amba_driver aaci_driver = {
1178 .drv = {
1179 .name = DRIVER_NAME,
1180 },
1181 .probe = aaci_probe,
1182 .remove = __devexit_p(aaci_remove),
1183 .suspend = aaci_suspend,
1184 .resume = aaci_resume,
1185 .id_table = aaci_ids,
1186};
1187
1188static int __init aaci_init(void)
1189{
1190 return amba_driver_register(&aaci_driver);
1191}
1192
1193static void __exit aaci_exit(void)
1194{
1195 amba_driver_unregister(&aaci_driver);
1196}
1197
1198module_init(aaci_init);
1199module_exit(aaci_exit);
1200
1201MODULE_LICENSE("GPL");
1202MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");