blob: 6c160a038b239a7c9e2baa0d3f941c68c7d68398 [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"
33#include "devdma.h"
34
35#define DRIVER_NAME "aaci-pl041"
36
37/*
38 * PM support is not complete. Turn it off.
39 */
40#undef CONFIG_PM
41
Takashi Iwaiceb9e472005-11-17 15:10:16 +010042static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020043{
44 u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
45
46 /*
47 * Ensure that the slot 1/2 RX registers are empty.
48 */
49 v = readl(aaci->base + AACI_SLFR);
50 if (v & SLFR_2RXV)
51 readl(aaci->base + AACI_SL2RX);
52 if (v & SLFR_1RXV)
53 readl(aaci->base + AACI_SL1RX);
54
55 writel(maincr, aaci->base + AACI_MAINCR);
56}
57
58/*
59 * P29:
60 * The recommended use of programming the external codec through slot 1
61 * and slot 2 data is to use the channels during setup routines and the
62 * slot register at any other time. The data written into slot 1, slot 2
63 * and slot 12 registers is transmitted only when their corresponding
64 * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
65 * register.
66 */
Kevin Hilman14d178a2007-02-07 05:46:47 +010067static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
68 unsigned short val)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020069{
70 struct aaci *aaci = ac97->private_data;
71 u32 v;
Kevin Hilman14d178a2007-02-07 05:46:47 +010072 int timeout = 5000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +020073
74 if (ac97->num >= 4)
75 return;
76
Ingo Molnar12aa7572006-01-16 16:36:05 +010077 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020078
79 aaci_ac97_select_codec(aaci, ac97);
80
81 /*
82 * P54: You must ensure that AACI_SL2TX is always written
83 * to, if required, before data is written to AACI_SL1TX.
84 */
85 writel(val << 4, aaci->base + AACI_SL2TX);
86 writel(reg << 12, aaci->base + AACI_SL1TX);
87
88 /*
89 * Wait for the transmission of both slots to complete.
90 */
91 do {
92 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +010093 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +010094
95 if (!timeout)
96 dev_err(&aaci->dev->dev,
97 "timeout waiting for write to complete\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +020098
Ingo Molnar12aa7572006-01-16 16:36:05 +010099 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200100}
101
102/*
103 * Read an AC'97 register.
104 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100105static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200106{
107 struct aaci *aaci = ac97->private_data;
108 u32 v;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100109 int timeout = 5000;
110 int retries = 10;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200111
112 if (ac97->num >= 4)
113 return ~0;
114
Ingo Molnar12aa7572006-01-16 16:36:05 +0100115 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200116
117 aaci_ac97_select_codec(aaci, ac97);
118
119 /*
120 * Write the register address to slot 1.
121 */
122 writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);
123
124 /*
125 * Wait for the transmission to complete.
126 */
127 do {
128 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100129 } while ((v & SLFR_1TXB) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +0100130
131 if (!timeout) {
132 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
133 v = ~0;
134 goto out;
135 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200136
137 /*
138 * Give the AC'97 codec more than enough time
139 * to respond. (42us = ~2 frames at 48kHz.)
140 */
141 udelay(42);
142
143 /*
144 * Wait for slot 2 to indicate data.
145 */
Kevin Hilman14d178a2007-02-07 05:46:47 +0100146 timeout = 5000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200147 do {
148 cond_resched();
149 v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100150 } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200151
Kevin Hilman14d178a2007-02-07 05:46:47 +0100152 if (!timeout) {
153 dev_err(&aaci->dev->dev, "timeout on RX valid\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200154 v = ~0;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100155 goto out;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200156 }
157
Kevin Hilman14d178a2007-02-07 05:46:47 +0100158 do {
159 v = readl(aaci->base + AACI_SL1RX) >> 12;
160 if (v == reg) {
161 v = readl(aaci->base + AACI_SL2RX) >> 4;
162 break;
163 } else if (--retries) {
164 dev_warn(&aaci->dev->dev,
165 "ac97 read back fail. retry\n");
166 continue;
167 } else {
168 dev_warn(&aaci->dev->dev,
169 "wrong ac97 register read back (%x != %x)\n",
170 v, reg);
171 v = ~0;
172 }
173 } while (retries);
174 out:
Ingo Molnar12aa7572006-01-16 16:36:05 +0100175 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200176 return v;
177}
178
179static inline void aaci_chan_wait_ready(struct aaci_runtime *aacirun)
180{
181 u32 val;
182 int timeout = 5000;
183
184 do {
185 val = readl(aacirun->base + AACI_SR);
186 } while (val & (SR_TXB|SR_RXB) && timeout--);
187}
188
189
190
191/*
192 * Interrupt support.
193 */
Kevin Hilman62578cb2007-02-07 05:41:37 +0100194static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200195{
Kevin Hilman41762b82007-02-07 05:45:32 +0100196 if (mask & ISR_ORINTR) {
197 dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
198 writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
199 }
200
201 if (mask & ISR_RXTOINTR) {
202 dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
203 writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
204 }
205
206 if (mask & ISR_RXINTR) {
207 struct aaci_runtime *aacirun = &aaci->capture;
208 void *ptr;
209
210 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700211 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
Kevin Hilman41762b82007-02-07 05:45:32 +0100212 writel(0, aacirun->base + AACI_IE);
213 return;
214 }
215 ptr = aacirun->ptr;
216
217 do {
218 unsigned int len = aacirun->fifosz;
219 u32 val;
220
221 if (aacirun->bytes <= 0) {
222 aacirun->bytes += aacirun->period;
223 aacirun->ptr = ptr;
224 spin_unlock(&aaci->lock);
225 snd_pcm_period_elapsed(aacirun->substream);
226 spin_lock(&aaci->lock);
227 }
228 if (!(aacirun->cr & CR_EN))
229 break;
230
231 val = readl(aacirun->base + AACI_SR);
232 if (!(val & SR_RXHF))
233 break;
234 if (!(val & SR_RXFF))
235 len >>= 1;
236
237 aacirun->bytes -= len;
238
239 /* reading 16 bytes at a time */
240 for( ; len > 0; len -= 16) {
241 asm(
242 "ldmia %1, {r0, r1, r2, r3}\n\t"
243 "stmia %0!, {r0, r1, r2, r3}"
244 : "+r" (ptr)
245 : "r" (aacirun->fifo)
246 : "r0", "r1", "r2", "r3", "cc");
247
248 if (ptr >= aacirun->end)
249 ptr = aacirun->start;
250 }
251 } while(1);
252 aacirun->ptr = ptr;
253 }
254
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200255 if (mask & ISR_URINTR) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100256 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
257 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200258 }
259
260 if (mask & ISR_TXINTR) {
261 struct aaci_runtime *aacirun = &aaci->playback;
262 void *ptr;
263
264 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700265 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200266 writel(0, aacirun->base + AACI_IE);
267 return;
268 }
269
270 ptr = aacirun->ptr;
271 do {
272 unsigned int len = aacirun->fifosz;
273 u32 val;
274
275 if (aacirun->bytes <= 0) {
276 aacirun->bytes += aacirun->period;
277 aacirun->ptr = ptr;
278 spin_unlock(&aaci->lock);
279 snd_pcm_period_elapsed(aacirun->substream);
280 spin_lock(&aaci->lock);
281 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100282 if (!(aacirun->cr & CR_EN))
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200283 break;
284
285 val = readl(aacirun->base + AACI_SR);
286 if (!(val & SR_TXHE))
287 break;
288 if (!(val & SR_TXFE))
289 len >>= 1;
290
291 aacirun->bytes -= len;
292
293 /* writing 16 bytes at a time */
294 for ( ; len > 0; len -= 16) {
295 asm(
296 "ldmia %0!, {r0, r1, r2, r3}\n\t"
297 "stmia %1, {r0, r1, r2, r3}"
298 : "+r" (ptr)
299 : "r" (aacirun->fifo)
300 : "r0", "r1", "r2", "r3", "cc");
301
302 if (ptr >= aacirun->end)
303 ptr = aacirun->start;
304 }
305 } while (1);
306
307 aacirun->ptr = ptr;
308 }
309}
310
David Howells7d12e782006-10-05 14:55:46 +0100311static irqreturn_t aaci_irq(int irq, void *devid)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200312{
313 struct aaci *aaci = devid;
314 u32 mask;
315 int i;
316
317 spin_lock(&aaci->lock);
318 mask = readl(aaci->base + AACI_ALLINTS);
319 if (mask) {
320 u32 m = mask;
321 for (i = 0; i < 4; i++, m >>= 7) {
322 if (m & 0x7f) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100323 aaci_fifo_irq(aaci, i, m);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200324 }
325 }
326 }
327 spin_unlock(&aaci->lock);
328
329 return mask ? IRQ_HANDLED : IRQ_NONE;
330}
331
332
333
334/*
335 * ALSA support.
336 */
337
338struct aaci_stream {
339 unsigned char codec_idx;
340 unsigned char rate_idx;
341};
342
343static struct aaci_stream aaci_streams[] = {
344 [ACSTREAM_FRONT] = {
345 .codec_idx = 0,
346 .rate_idx = AC97_RATES_FRONT_DAC,
347 },
348 [ACSTREAM_SURROUND] = {
349 .codec_idx = 0,
350 .rate_idx = AC97_RATES_SURR_DAC,
351 },
352 [ACSTREAM_LFE] = {
353 .codec_idx = 0,
354 .rate_idx = AC97_RATES_LFE_DAC,
355 },
356};
357
358static inline unsigned int aaci_rate_mask(struct aaci *aaci, int streamid)
359{
360 struct aaci_stream *s = aaci_streams + streamid;
361 return aaci->ac97_bus->codec[s->codec_idx]->rates[s->rate_idx];
362}
363
364static unsigned int rate_list[] = {
365 5512, 8000, 11025, 16000, 22050, 32000, 44100,
366 48000, 64000, 88200, 96000, 176400, 192000
367};
368
369/*
370 * Double-rate rule: we can support double rate iff channels == 2
371 * (unimplemented)
372 */
373static int
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100374aaci_rule_rate_by_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200375{
376 struct aaci *aaci = rule->private;
377 unsigned int rate_mask = SNDRV_PCM_RATE_8000_48000|SNDRV_PCM_RATE_5512;
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100378 struct snd_interval *c = hw_param_interval(p, SNDRV_PCM_HW_PARAM_CHANNELS);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200379
380 switch (c->max) {
381 case 6:
382 rate_mask &= aaci_rate_mask(aaci, ACSTREAM_LFE);
383 case 4:
384 rate_mask &= aaci_rate_mask(aaci, ACSTREAM_SURROUND);
385 case 2:
386 rate_mask &= aaci_rate_mask(aaci, ACSTREAM_FRONT);
387 }
388
389 return snd_interval_list(hw_param_interval(p, rule->var),
390 ARRAY_SIZE(rate_list), rate_list,
391 rate_mask);
392}
393
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100394static struct snd_pcm_hardware aaci_hw_info = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200395 .info = SNDRV_PCM_INFO_MMAP |
396 SNDRV_PCM_INFO_MMAP_VALID |
397 SNDRV_PCM_INFO_INTERLEAVED |
398 SNDRV_PCM_INFO_BLOCK_TRANSFER |
399 SNDRV_PCM_INFO_RESUME,
400
401 /*
402 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
403 * words. It also doesn't support 12-bit at all.
404 */
405 .formats = SNDRV_PCM_FMTBIT_S16_LE,
406
407 /* should this be continuous or knot? */
408 .rates = SNDRV_PCM_RATE_CONTINUOUS,
409 .rate_max = 48000,
410 .rate_min = 4000,
411 .channels_min = 2,
412 .channels_max = 6,
413 .buffer_bytes_max = 64 * 1024,
414 .period_bytes_min = 256,
415 .period_bytes_max = PAGE_SIZE,
416 .periods_min = 4,
417 .periods_max = PAGE_SIZE / 16,
418};
419
Kevin Hilman41762b82007-02-07 05:45:32 +0100420static int __aaci_pcm_open(struct aaci *aaci,
421 struct snd_pcm_substream *substream,
422 struct aaci_runtime *aacirun)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200423{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100424 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200425 int ret;
426
427 aacirun->substream = substream;
428 runtime->private_data = aacirun;
429 runtime->hw = aaci_hw_info;
430
431 /*
432 * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
433 * mode, each 32-bit word contains one sample. If we're in
434 * compact mode, each 32-bit word contains two samples, effectively
435 * halving the FIFO size. However, we don't know for sure which
436 * we'll be using at this point. We set this to the lower limit.
437 */
438 runtime->hw.fifo_size = aaci->fifosize * 2;
439
440 /*
441 * Add rule describing hardware rate dependency
442 * on the number of channels.
443 */
444 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
445 aaci_rule_rate_by_channels, aaci,
446 SNDRV_PCM_HW_PARAM_CHANNELS,
447 SNDRV_PCM_HW_PARAM_RATE, -1);
448 if (ret)
449 goto out;
450
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700451 ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200452 DRIVER_NAME, aaci);
453 if (ret)
454 goto out;
455
456 return 0;
457
458 out:
459 return ret;
460}
461
462
463/*
464 * Common ALSA stuff
465 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100466static int aaci_pcm_close(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200467{
468 struct aaci *aaci = substream->private_data;
469 struct aaci_runtime *aacirun = substream->runtime->private_data;
470
Kevin Hilman41762b82007-02-07 05:45:32 +0100471 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200472
473 aacirun->substream = NULL;
474 free_irq(aaci->dev->irq[0], aaci);
475
476 return 0;
477}
478
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100479static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200480{
481 struct aaci_runtime *aacirun = substream->runtime->private_data;
482
483 /*
484 * This must not be called with the device enabled.
485 */
Kevin Hilman41762b82007-02-07 05:45:32 +0100486 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200487
488 if (aacirun->pcm_open)
489 snd_ac97_pcm_close(aacirun->pcm);
490 aacirun->pcm_open = 0;
491
492 /*
493 * Clear out the DMA and any allocated buffers.
494 */
495 devdma_hw_free(NULL, substream);
496
497 return 0;
498}
499
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100500static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200501 struct aaci_runtime *aacirun,
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100502 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200503{
504 int err;
505
506 aaci_pcm_hw_free(substream);
Russell King4acd57c2009-11-29 16:39:52 +0000507 if (aacirun->pcm_open) {
508 snd_ac97_pcm_close(aacirun->pcm);
509 aacirun->pcm_open = 0;
510 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200511
512 err = devdma_hw_alloc(NULL, substream,
513 params_buffer_bytes(params));
514 if (err < 0)
515 goto out;
516
Kevin Hilman41762b82007-02-07 05:45:32 +0100517 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
518 err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
519 params_channels(params),
520 aacirun->pcm->r[0].slots);
521 else
522 err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
523 params_channels(params),
Russell King8ee763b2009-11-29 16:39:59 +0000524 aacirun->pcm->r[0].slots);
Kevin Hilman41762b82007-02-07 05:45:32 +0100525
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200526 if (err)
527 goto out;
528
529 aacirun->pcm_open = 1;
530
531 out:
532 return err;
533}
534
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100535static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200536{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100537 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200538 struct aaci_runtime *aacirun = runtime->private_data;
539
540 aacirun->start = (void *)runtime->dma_area;
541 aacirun->end = aacirun->start + runtime->dma_bytes;
542 aacirun->ptr = aacirun->start;
543 aacirun->period =
544 aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
545
546 return 0;
547}
548
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100549static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200550{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100551 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200552 struct aaci_runtime *aacirun = runtime->private_data;
553 ssize_t bytes = aacirun->ptr - aacirun->start;
554
555 return bytes_to_frames(runtime, bytes);
556}
557
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100558static int aaci_pcm_mmap(struct snd_pcm_substream *substream, struct vm_area_struct *vma)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200559{
560 return devdma_mmap(NULL, substream, vma);
561}
562
563
564/*
565 * Playback specific ALSA stuff
566 */
567static const u32 channels_to_txmask[] = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100568 [2] = CR_SL3 | CR_SL4,
569 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
570 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200571};
572
573/*
574 * We can support two and four channel audio. Unfortunately
575 * six channel audio requires a non-standard channel ordering:
576 * 2 -> FL(3), FR(4)
577 * 4 -> FL(3), FR(4), SL(7), SR(8)
578 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
579 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
580 * This requires an ALSA configuration file to correct.
581 */
582static unsigned int channel_list[] = { 2, 4, 6 };
583
584static int
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100585aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200586{
587 struct aaci *aaci = rule->private;
588 unsigned int chan_mask = 1 << 0, slots;
589
590 /*
591 * pcms[0] is the our 5.1 PCM instance.
592 */
593 slots = aaci->ac97_bus->pcms[0].r[0].slots;
594 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
595 chan_mask |= 1 << 1;
596 if (slots & (1 << AC97_SLOT_LFE))
597 chan_mask |= 1 << 2;
598 }
599
600 return snd_interval_list(hw_param_interval(p, rule->var),
601 ARRAY_SIZE(channel_list), channel_list,
602 chan_mask);
603}
604
Kevin Hilman41762b82007-02-07 05:45:32 +0100605static int aaci_pcm_open(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200606{
607 struct aaci *aaci = substream->private_data;
608 int ret;
609
610 /*
611 * Add rule describing channel dependency.
612 */
613 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
614 SNDRV_PCM_HW_PARAM_CHANNELS,
615 aaci_rule_channels, aaci,
616 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
617 if (ret)
618 return ret;
619
Kevin Hilman41762b82007-02-07 05:45:32 +0100620 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
621 ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
622 } else {
623 ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
624 }
625 return ret;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200626}
627
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100628static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
629 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200630{
631 struct aaci *aaci = substream->private_data;
632 struct aaci_runtime *aacirun = substream->runtime->private_data;
633 unsigned int channels = params_channels(params);
634 int ret;
635
636 WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
637 !channels_to_txmask[channels]);
638
639 ret = aaci_pcm_hw_params(substream, aacirun, params);
640
641 /*
642 * Enable FIFO, compact mode, 16 bits per sample.
643 * FIXME: double rate slots?
644 */
645 if (ret >= 0) {
Kevin Hilman41762b82007-02-07 05:45:32 +0100646 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200647 aacirun->cr |= channels_to_txmask[channels];
648
649 aacirun->fifosz = aaci->fifosize * 4;
Kevin Hilman41762b82007-02-07 05:45:32 +0100650 if (aacirun->cr & CR_COMPACT)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200651 aacirun->fifosz >>= 1;
652 }
653 return ret;
654}
655
656static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
657{
658 u32 ie;
659
660 ie = readl(aacirun->base + AACI_IE);
661 ie &= ~(IE_URIE|IE_TXIE);
662 writel(ie, aacirun->base + AACI_IE);
Kevin Hilman41762b82007-02-07 05:45:32 +0100663 aacirun->cr &= ~CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200664 aaci_chan_wait_ready(aacirun);
665 writel(aacirun->cr, aacirun->base + AACI_TXCR);
666}
667
668static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
669{
670 u32 ie;
671
672 aaci_chan_wait_ready(aacirun);
Kevin Hilman41762b82007-02-07 05:45:32 +0100673 aacirun->cr |= CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200674
675 ie = readl(aacirun->base + AACI_IE);
676 ie |= IE_URIE | IE_TXIE;
677 writel(ie, aacirun->base + AACI_IE);
678 writel(aacirun->cr, aacirun->base + AACI_TXCR);
679}
680
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100681static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200682{
683 struct aaci *aaci = substream->private_data;
684 struct aaci_runtime *aacirun = substream->runtime->private_data;
685 unsigned long flags;
686 int ret = 0;
687
688 spin_lock_irqsave(&aaci->lock, flags);
689 switch (cmd) {
690 case SNDRV_PCM_TRIGGER_START:
691 aaci_pcm_playback_start(aacirun);
692 break;
693
694 case SNDRV_PCM_TRIGGER_RESUME:
695 aaci_pcm_playback_start(aacirun);
696 break;
697
698 case SNDRV_PCM_TRIGGER_STOP:
699 aaci_pcm_playback_stop(aacirun);
700 break;
701
702 case SNDRV_PCM_TRIGGER_SUSPEND:
703 aaci_pcm_playback_stop(aacirun);
704 break;
705
706 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
707 break;
708
709 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
710 break;
711
712 default:
713 ret = -EINVAL;
714 }
715 spin_unlock_irqrestore(&aaci->lock, flags);
716
717 return ret;
718}
719
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100720static struct snd_pcm_ops aaci_playback_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100721 .open = aaci_pcm_open,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200722 .close = aaci_pcm_close,
723 .ioctl = snd_pcm_lib_ioctl,
724 .hw_params = aaci_pcm_playback_hw_params,
725 .hw_free = aaci_pcm_hw_free,
726 .prepare = aaci_pcm_prepare,
727 .trigger = aaci_pcm_playback_trigger,
728 .pointer = aaci_pcm_pointer,
729 .mmap = aaci_pcm_mmap,
730};
731
Russell King8a371842007-02-20 15:44:23 +0000732static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
733 struct snd_pcm_hw_params *params)
Kevin Hilman41762b82007-02-07 05:45:32 +0100734{
735 struct aaci *aaci = substream->private_data;
736 struct aaci_runtime *aacirun = substream->runtime->private_data;
737 int ret;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200738
Kevin Hilman41762b82007-02-07 05:45:32 +0100739 ret = aaci_pcm_hw_params(substream, aacirun, params);
740
741 if (ret >= 0) {
742 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
743
744 /* Line in record: slot 3 and 4 */
745 aacirun->cr |= CR_SL3 | CR_SL4;
746
747 aacirun->fifosz = aaci->fifosize * 4;
748
749 if (aacirun->cr & CR_COMPACT)
750 aacirun->fifosz >>= 1;
751 }
752 return ret;
753}
754
755static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
756{
757 u32 ie;
758
759 aaci_chan_wait_ready(aacirun);
760
761 ie = readl(aacirun->base + AACI_IE);
762 ie &= ~(IE_ORIE | IE_RXIE);
763 writel(ie, aacirun->base+AACI_IE);
764
765 aacirun->cr &= ~CR_EN;
766
767 writel(aacirun->cr, aacirun->base + AACI_RXCR);
768}
769
770static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
771{
772 u32 ie;
773
774 aaci_chan_wait_ready(aacirun);
775
776#ifdef DEBUG
777 /* RX Timeout value: bits 28:17 in RXCR */
778 aacirun->cr |= 0xf << 17;
779#endif
780
781 aacirun->cr |= CR_EN;
782 writel(aacirun->cr, aacirun->base + AACI_RXCR);
783
784 ie = readl(aacirun->base + AACI_IE);
785 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
786 writel(ie, aacirun->base + AACI_IE);
787}
788
Russell King8a371842007-02-20 15:44:23 +0000789static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
790{
Kevin Hilman41762b82007-02-07 05:45:32 +0100791 struct aaci *aaci = substream->private_data;
792 struct aaci_runtime *aacirun = substream->runtime->private_data;
793 unsigned long flags;
794 int ret = 0;
795
796 spin_lock_irqsave(&aaci->lock, flags);
797
798 switch (cmd) {
799 case SNDRV_PCM_TRIGGER_START:
800 aaci_pcm_capture_start(aacirun);
801 break;
802
803 case SNDRV_PCM_TRIGGER_RESUME:
804 aaci_pcm_capture_start(aacirun);
805 break;
806
807 case SNDRV_PCM_TRIGGER_STOP:
808 aaci_pcm_capture_stop(aacirun);
809 break;
810
811 case SNDRV_PCM_TRIGGER_SUSPEND:
812 aaci_pcm_capture_stop(aacirun);
813 break;
814
815 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
816 break;
817
818 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
819 break;
820
821 default:
822 ret = -EINVAL;
823 }
824
825 spin_unlock_irqrestore(&aaci->lock, flags);
826
827 return ret;
828}
829
Russell King8a371842007-02-20 15:44:23 +0000830static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
Kevin Hilman41762b82007-02-07 05:45:32 +0100831{
832 struct snd_pcm_runtime *runtime = substream->runtime;
833 struct aaci *aaci = substream->private_data;
834
835 aaci_pcm_prepare(substream);
836
837 /* allow changing of sample rate */
838 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
839 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
840 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
841
842 /* Record select: Mic: 0, Aux: 3, Line: 4 */
843 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
844
845 return 0;
846}
847
Russell King8a371842007-02-20 15:44:23 +0000848static struct snd_pcm_ops aaci_capture_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100849 .open = aaci_pcm_open,
850 .close = aaci_pcm_close,
851 .ioctl = snd_pcm_lib_ioctl,
852 .hw_params = aaci_pcm_capture_hw_params,
853 .hw_free = aaci_pcm_hw_free,
854 .prepare = aaci_pcm_capture_prepare,
855 .trigger = aaci_pcm_capture_trigger,
856 .pointer = aaci_pcm_pointer,
857 .mmap = aaci_pcm_mmap,
858};
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200859
860/*
861 * Power Management.
862 */
863#ifdef CONFIG_PM
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100864static int aaci_do_suspend(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200865{
866 struct aaci *aaci = card->private_data;
Takashi Iwai792a6c52005-11-17 17:19:25 +0100867 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
868 snd_pcm_suspend_all(aaci->pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200869 return 0;
870}
871
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100872static int aaci_do_resume(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200873{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100874 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200875 return 0;
876}
877
Richard Purdiee36d3942005-09-16 19:27:53 -0700878static int aaci_suspend(struct amba_device *dev, pm_message_t state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200879{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100880 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200881 return card ? aaci_do_suspend(card) : 0;
882}
883
884static int aaci_resume(struct amba_device *dev)
885{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100886 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200887 return card ? aaci_do_resume(card) : 0;
888}
889#else
890#define aaci_do_suspend NULL
891#define aaci_do_resume NULL
892#define aaci_suspend NULL
893#define aaci_resume NULL
894#endif
895
896
897static struct ac97_pcm ac97_defs[] __devinitdata = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100898 [0] = { /* Front PCM */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200899 .exclusive = 1,
900 .r = {
901 [0] = {
902 .slots = (1 << AC97_SLOT_PCM_LEFT) |
903 (1 << AC97_SLOT_PCM_RIGHT) |
904 (1 << AC97_SLOT_PCM_CENTER) |
905 (1 << AC97_SLOT_PCM_SLEFT) |
906 (1 << AC97_SLOT_PCM_SRIGHT) |
907 (1 << AC97_SLOT_LFE),
908 },
909 },
910 },
911 [1] = { /* PCM in */
912 .stream = 1,
913 .exclusive = 1,
914 .r = {
915 [0] = {
916 .slots = (1 << AC97_SLOT_PCM_LEFT) |
917 (1 << AC97_SLOT_PCM_RIGHT),
918 },
919 },
920 },
921 [2] = { /* Mic in */
922 .stream = 1,
923 .exclusive = 1,
924 .r = {
925 [0] = {
926 .slots = (1 << AC97_SLOT_MIC),
927 },
928 },
929 }
930};
931
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100932static struct snd_ac97_bus_ops aaci_bus_ops = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200933 .write = aaci_ac97_write,
934 .read = aaci_ac97_read,
935};
936
937static int __devinit aaci_probe_ac97(struct aaci *aaci)
938{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100939 struct snd_ac97_template ac97_template;
940 struct snd_ac97_bus *ac97_bus;
941 struct snd_ac97 *ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200942 int ret;
943
Philby John29a4f2d2009-10-13 16:30:22 +0530944 writel(0, aaci->base + AC97_POWERDOWN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200945 /*
946 * Assert AACIRESET for 2us
947 */
948 writel(0, aaci->base + AACI_RESET);
949 udelay(2);
950 writel(RESET_NRST, aaci->base + AACI_RESET);
951
952 /*
953 * Give the AC'97 codec more than enough time
954 * to wake up. (42us = ~2 frames at 48kHz.)
955 */
956 udelay(42);
957
958 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
959 if (ret)
960 goto out;
961
962 ac97_bus->clock = 48000;
963 aaci->ac97_bus = ac97_bus;
964
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100965 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200966 ac97_template.private_data = aaci;
967 ac97_template.num = 0;
968 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
969
970 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
971 if (ret)
972 goto out;
Kevin Hilman41762b82007-02-07 05:45:32 +0100973 aaci->ac97 = ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200974
975 /*
976 * Disable AC97 PC Beep input on audio codecs.
977 */
978 if (ac97_is_audio(ac97))
979 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
980
981 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
982 if (ret)
983 goto out;
984
985 aaci->playback.pcm = &ac97_bus->pcms[0];
Kevin Hilman41762b82007-02-07 05:45:32 +0100986 aaci->capture.pcm = &ac97_bus->pcms[1];
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200987
988 out:
989 return ret;
990}
991
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100992static void aaci_free_card(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200993{
994 struct aaci *aaci = card->private_data;
995 if (aaci->base)
996 iounmap(aaci->base);
997}
998
999static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
1000{
1001 struct aaci *aaci;
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001002 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001003 int err;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001004
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001005 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1006 THIS_MODULE, sizeof(struct aaci), &card);
1007 if (err < 0)
Takashi Iwai631e8ad2008-09-01 15:31:50 +02001008 return NULL;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001009
1010 card->private_free = aaci_free_card;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001011
1012 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
1013 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
1014 snprintf(card->longname, sizeof(card->longname),
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001015 "%s at 0x%016llx, irq %d",
1016 card->shortname, (unsigned long long)dev->res.start,
1017 dev->irq[0]);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001018
1019 aaci = card->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +01001020 mutex_init(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001021 spin_lock_init(&aaci->lock);
1022 aaci->card = card;
1023 aaci->dev = dev;
1024
1025 /* Set MAINCR to allow slot 1 and 2 data IO */
1026 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
1027 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
1028
1029 return aaci;
1030}
1031
1032static int __devinit aaci_init_pcm(struct aaci *aaci)
1033{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001034 struct snd_pcm *pcm;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001035 int ret;
1036
Kevin Hilman41762b82007-02-07 05:45:32 +01001037 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001038 if (ret == 0) {
1039 aaci->pcm = pcm;
1040 pcm->private_data = aaci;
1041 pcm->info_flags = 0;
1042
1043 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
1044
1045 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
Kevin Hilman41762b82007-02-07 05:45:32 +01001046 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001047 }
1048
1049 return ret;
1050}
1051
1052static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
1053{
Kevin Hilman41762b82007-02-07 05:45:32 +01001054 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001055 int i;
1056
Kevin Hilman41762b82007-02-07 05:45:32 +01001057 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001058
Kevin Hilman41762b82007-02-07 05:45:32 +01001059 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
1060 writel(0, aacirun->fifo);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001061
Kevin Hilman41762b82007-02-07 05:45:32 +01001062 writel(0, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001063
1064 /*
1065 * Re-initialise the AACI after the FIFO depth test, to
1066 * ensure that the FIFOs are empty. Unfortunately, merely
1067 * disabling the channel doesn't clear the FIFO.
1068 */
1069 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
1070 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1071
1072 /*
1073 * If we hit 4096, we failed. Go back to the specified
1074 * fifo depth.
1075 */
1076 if (i == 4096)
1077 i = 8;
1078
1079 return i;
1080}
1081
Alessandro Rubini03fbdb12009-05-20 22:39:08 +01001082static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001083{
1084 struct aaci *aaci;
1085 int ret, i;
1086
1087 ret = amba_request_regions(dev, NULL);
1088 if (ret)
1089 return ret;
1090
1091 aaci = aaci_init_card(dev);
Takashi Iwai631e8ad2008-09-01 15:31:50 +02001092 if (!aaci) {
1093 ret = -ENOMEM;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001094 goto out;
1095 }
1096
Linus Walleijdc890c22009-06-07 23:27:31 +01001097 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001098 if (!aaci->base) {
1099 ret = -ENOMEM;
1100 goto out;
1101 }
1102
1103 /*
1104 * Playback uses AACI channel 0
1105 */
1106 aaci->playback.base = aaci->base + AACI_CSCH1;
1107 aaci->playback.fifo = aaci->base + AACI_DR1;
1108
Kevin Hilman41762b82007-02-07 05:45:32 +01001109 /*
1110 * Capture uses AACI channel 0
1111 */
1112 aaci->capture.base = aaci->base + AACI_CSCH1;
1113 aaci->capture.fifo = aaci->base + AACI_DR1;
1114
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001115 for (i = 0; i < 4; i++) {
viro@ZenIV.linux.org.uke12ba642005-09-06 02:06:57 +01001116 void __iomem *base = aaci->base + i * 0x14;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001117
1118 writel(0, base + AACI_IE);
1119 writel(0, base + AACI_TXCR);
1120 writel(0, base + AACI_RXCR);
1121 }
1122
1123 writel(0x1fff, aaci->base + AACI_INTCLR);
1124 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1125
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001126 ret = aaci_probe_ac97(aaci);
1127 if (ret)
1128 goto out;
1129
Catalin Marinasf27f2182006-02-01 19:25:58 +00001130 /*
1131 * Size the FIFOs (must be multiple of 16).
1132 */
1133 aaci->fifosize = aaci_size_fifo(aaci);
1134 if (aaci->fifosize & 15) {
1135 printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
1136 aaci->fifosize);
1137 ret = -ENODEV;
1138 goto out;
1139 }
1140
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001141 ret = aaci_init_pcm(aaci);
1142 if (ret)
1143 goto out;
1144
Takashi Iwaia76af192005-09-05 16:56:40 +02001145 snd_card_set_dev(aaci->card, &dev->dev);
1146
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001147 ret = snd_card_register(aaci->card);
1148 if (ret == 0) {
1149 dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
Kevin Hilman41762b82007-02-07 05:45:32 +01001150 aaci->fifosize);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001151 amba_set_drvdata(dev, aaci->card);
1152 return ret;
1153 }
1154
1155 out:
1156 if (aaci)
1157 snd_card_free(aaci->card);
1158 amba_release_regions(dev);
1159 return ret;
1160}
1161
1162static int __devexit aaci_remove(struct amba_device *dev)
1163{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001164 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001165
1166 amba_set_drvdata(dev, NULL);
1167
1168 if (card) {
1169 struct aaci *aaci = card->private_data;
1170 writel(0, aaci->base + AACI_MAINCR);
1171
1172 snd_card_free(card);
1173 amba_release_regions(dev);
1174 }
1175
1176 return 0;
1177}
1178
1179static struct amba_id aaci_ids[] = {
1180 {
1181 .id = 0x00041041,
1182 .mask = 0x000fffff,
1183 },
1184 { 0, 0 },
1185};
1186
1187static struct amba_driver aaci_driver = {
1188 .drv = {
1189 .name = DRIVER_NAME,
1190 },
1191 .probe = aaci_probe,
1192 .remove = __devexit_p(aaci_remove),
1193 .suspend = aaci_suspend,
1194 .resume = aaci_resume,
1195 .id_table = aaci_ids,
1196};
1197
1198static int __init aaci_init(void)
1199{
1200 return amba_driver_register(&aaci_driver);
1201}
1202
1203static void __exit aaci_exit(void)
1204{
1205 amba_driver_unregister(&aaci_driver);
1206}
1207
1208module_init(aaci_init);
1209module_exit(aaci_exit);
1210
1211MODULE_LICENSE("GPL");
1212MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");