blob: a148e275c80f26db21ec6668d197742fc46531d7 [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
Russell King250c7a62011-01-12 23:42:57 +000033#define FRAME_PERIOD_US 21
34
Russell Kingcb5a6ff2005-05-12 14:04:59 +020035/*
36 * PM support is not complete. Turn it off.
37 */
38#undef CONFIG_PM
39
Takashi Iwaiceb9e472005-11-17 15:10:16 +010040static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020041{
42 u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
43
44 /*
45 * Ensure that the slot 1/2 RX registers are empty.
46 */
47 v = readl(aaci->base + AACI_SLFR);
48 if (v & SLFR_2RXV)
49 readl(aaci->base + AACI_SL2RX);
50 if (v & SLFR_1RXV)
51 readl(aaci->base + AACI_SL1RX);
52
53 writel(maincr, aaci->base + AACI_MAINCR);
54}
55
56/*
57 * P29:
58 * The recommended use of programming the external codec through slot 1
59 * and slot 2 data is to use the channels during setup routines and the
60 * slot register at any other time. The data written into slot 1, slot 2
61 * and slot 12 registers is transmitted only when their corresponding
62 * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
63 * register.
64 */
Kevin Hilman14d178a2007-02-07 05:46:47 +010065static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
66 unsigned short val)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020067{
68 struct aaci *aaci = ac97->private_data;
Russell King250c7a62011-01-12 23:42:57 +000069 int timeout;
Russell Kingcb5a6ff2005-05-12 14:04:59 +020070 u32 v;
71
72 if (ac97->num >= 4)
73 return;
74
Ingo Molnar12aa7572006-01-16 16:36:05 +010075 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020076
77 aaci_ac97_select_codec(aaci, ac97);
78
79 /*
80 * P54: You must ensure that AACI_SL2TX is always written
81 * to, if required, before data is written to AACI_SL1TX.
82 */
83 writel(val << 4, aaci->base + AACI_SL2TX);
84 writel(reg << 12, aaci->base + AACI_SL1TX);
85
Russell King250c7a62011-01-12 23:42:57 +000086 /* Initially, wait one frame period */
87 udelay(FRAME_PERIOD_US);
88
89 /* And then wait an additional eight frame periods for it to be sent */
90 timeout = FRAME_PERIOD_US * 8;
Russell Kingcb5a6ff2005-05-12 14:04:59 +020091 do {
Russell King250c7a62011-01-12 23:42:57 +000092 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020093 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +010094 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +010095
Russell King69058cd2011-01-12 23:17:24 +000096 if (v & (SLFR_1TXB|SLFR_2TXB))
Kevin Hilman14d178a2007-02-07 05:46:47 +010097 dev_err(&aaci->dev->dev,
98 "timeout waiting for write to complete\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +020099
Ingo Molnar12aa7572006-01-16 16:36:05 +0100100 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200101}
102
103/*
104 * Read an AC'97 register.
105 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100106static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200107{
108 struct aaci *aaci = ac97->private_data;
Russell King250c7a62011-01-12 23:42:57 +0000109 int timeout, retries = 10;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200110 u32 v;
111
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
Russell King250c7a62011-01-12 23:42:57 +0000124 /* Initially, wait one frame period */
125 udelay(FRAME_PERIOD_US);
126
127 /* And then wait an additional eight frame periods for it to be sent */
128 timeout = FRAME_PERIOD_US * 8;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200129 do {
Russell King250c7a62011-01-12 23:42:57 +0000130 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200131 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100132 } while ((v & SLFR_1TXB) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +0100133
Russell King69058cd2011-01-12 23:17:24 +0000134 if (v & SLFR_1TXB) {
Kevin Hilman14d178a2007-02-07 05:46:47 +0100135 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
136 v = ~0;
137 goto out;
138 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200139
Russell King250c7a62011-01-12 23:42:57 +0000140 /* Now wait for the response frame */
141 udelay(FRAME_PERIOD_US);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200142
Russell King250c7a62011-01-12 23:42:57 +0000143 /* And then wait an additional eight frame periods for data */
144 timeout = FRAME_PERIOD_US * 8;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200145 do {
Russell King250c7a62011-01-12 23:42:57 +0000146 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200147 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
Russell King69058cd2011-01-12 23:17:24 +0000151 if (v != (SLFR_1RXV|SLFR_2RXV)) {
Kevin Hilman14d178a2007-02-07 05:46:47 +0100152 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
Russell Kingd6a89fe2009-12-18 17:48:50 +0000178static inline void
179aaci_chan_wait_ready(struct aaci_runtime *aacirun, unsigned long mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200180{
181 u32 val;
182 int timeout = 5000;
183
184 do {
Russell King250c7a62011-01-12 23:42:57 +0000185 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200186 val = readl(aacirun->base + AACI_SR);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000187 } while (val & mask && timeout--);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200188}
189
190
191
192/*
193 * Interrupt support.
194 */
Kevin Hilman62578cb2007-02-07 05:41:37 +0100195static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200196{
Kevin Hilman41762b82007-02-07 05:45:32 +0100197 if (mask & ISR_ORINTR) {
198 dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
199 writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
200 }
201
202 if (mask & ISR_RXTOINTR) {
203 dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
204 writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
205 }
206
207 if (mask & ISR_RXINTR) {
208 struct aaci_runtime *aacirun = &aaci->capture;
Russell Kingea51d0b2011-01-13 08:47:35 +0000209 bool period_elapsed = false;
Kevin Hilman41762b82007-02-07 05:45:32 +0100210 void *ptr;
211
212 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700213 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
Kevin Hilman41762b82007-02-07 05:45:32 +0100214 writel(0, aacirun->base + AACI_IE);
215 return;
216 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100217
Russell Kingd6a89fe2009-12-18 17:48:50 +0000218 spin_lock(&aacirun->lock);
219
220 ptr = aacirun->ptr;
Kevin Hilman41762b82007-02-07 05:45:32 +0100221 do {
Russell King5d350cb2011-01-13 22:25:10 +0000222 unsigned int len = aacirun->fifo_bytes;
Kevin Hilman41762b82007-02-07 05:45:32 +0100223 u32 val;
224
225 if (aacirun->bytes <= 0) {
226 aacirun->bytes += aacirun->period;
Russell Kingea51d0b2011-01-13 08:47:35 +0000227 period_elapsed = true;
Kevin Hilman41762b82007-02-07 05:45:32 +0100228 }
229 if (!(aacirun->cr & CR_EN))
230 break;
231
232 val = readl(aacirun->base + AACI_SR);
233 if (!(val & SR_RXHF))
234 break;
235 if (!(val & SR_RXFF))
236 len >>= 1;
237
238 aacirun->bytes -= len;
239
240 /* reading 16 bytes at a time */
241 for( ; len > 0; len -= 16) {
242 asm(
243 "ldmia %1, {r0, r1, r2, r3}\n\t"
244 "stmia %0!, {r0, r1, r2, r3}"
245 : "+r" (ptr)
246 : "r" (aacirun->fifo)
247 : "r0", "r1", "r2", "r3", "cc");
248
249 if (ptr >= aacirun->end)
250 ptr = aacirun->start;
251 }
252 } while(1);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000253
Kevin Hilman41762b82007-02-07 05:45:32 +0100254 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000255
256 spin_unlock(&aacirun->lock);
Russell Kingea51d0b2011-01-13 08:47:35 +0000257
258 if (period_elapsed)
259 snd_pcm_period_elapsed(aacirun->substream);
Kevin Hilman41762b82007-02-07 05:45:32 +0100260 }
261
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200262 if (mask & ISR_URINTR) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100263 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
264 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200265 }
266
267 if (mask & ISR_TXINTR) {
268 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingea51d0b2011-01-13 08:47:35 +0000269 bool period_elapsed = false;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200270 void *ptr;
271
272 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700273 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200274 writel(0, aacirun->base + AACI_IE);
275 return;
276 }
277
Russell Kingd6a89fe2009-12-18 17:48:50 +0000278 spin_lock(&aacirun->lock);
279
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200280 ptr = aacirun->ptr;
281 do {
Russell King5d350cb2011-01-13 22:25:10 +0000282 unsigned int len = aacirun->fifo_bytes;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200283 u32 val;
284
285 if (aacirun->bytes <= 0) {
286 aacirun->bytes += aacirun->period;
Russell Kingea51d0b2011-01-13 08:47:35 +0000287 period_elapsed = true;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200288 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100289 if (!(aacirun->cr & CR_EN))
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200290 break;
291
292 val = readl(aacirun->base + AACI_SR);
293 if (!(val & SR_TXHE))
294 break;
295 if (!(val & SR_TXFE))
296 len >>= 1;
297
298 aacirun->bytes -= len;
299
300 /* writing 16 bytes at a time */
301 for ( ; len > 0; len -= 16) {
302 asm(
303 "ldmia %0!, {r0, r1, r2, r3}\n\t"
304 "stmia %1, {r0, r1, r2, r3}"
305 : "+r" (ptr)
306 : "r" (aacirun->fifo)
307 : "r0", "r1", "r2", "r3", "cc");
308
309 if (ptr >= aacirun->end)
310 ptr = aacirun->start;
311 }
312 } while (1);
313
314 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000315
316 spin_unlock(&aacirun->lock);
Russell Kingea51d0b2011-01-13 08:47:35 +0000317
318 if (period_elapsed)
319 snd_pcm_period_elapsed(aacirun->substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200320 }
321}
322
David Howells7d12e782006-10-05 14:55:46 +0100323static irqreturn_t aaci_irq(int irq, void *devid)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200324{
325 struct aaci *aaci = devid;
326 u32 mask;
327 int i;
328
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200329 mask = readl(aaci->base + AACI_ALLINTS);
330 if (mask) {
331 u32 m = mask;
332 for (i = 0; i < 4; i++, m >>= 7) {
333 if (m & 0x7f) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100334 aaci_fifo_irq(aaci, i, m);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200335 }
336 }
337 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200338
339 return mask ? IRQ_HANDLED : IRQ_NONE;
340}
341
342
343
344/*
345 * ALSA support.
346 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100347static struct snd_pcm_hardware aaci_hw_info = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200348 .info = SNDRV_PCM_INFO_MMAP |
349 SNDRV_PCM_INFO_MMAP_VALID |
350 SNDRV_PCM_INFO_INTERLEAVED |
351 SNDRV_PCM_INFO_BLOCK_TRANSFER |
352 SNDRV_PCM_INFO_RESUME,
353
354 /*
355 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
356 * words. It also doesn't support 12-bit at all.
357 */
358 .formats = SNDRV_PCM_FMTBIT_S16_LE,
359
Russell King6ca867c2009-12-18 17:48:35 +0000360 /* rates are setup from the AC'97 codec */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200361 .channels_min = 2,
Russell Kinge831d802011-01-13 10:13:17 +0000362 .channels_max = 2,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200363 .buffer_bytes_max = 64 * 1024,
364 .period_bytes_min = 256,
365 .period_bytes_max = PAGE_SIZE,
366 .periods_min = 4,
367 .periods_max = PAGE_SIZE / 16,
368};
369
Russell Kinge831d802011-01-13 10:13:17 +0000370/*
371 * We can support two and four channel audio. Unfortunately
372 * six channel audio requires a non-standard channel ordering:
373 * 2 -> FL(3), FR(4)
374 * 4 -> FL(3), FR(4), SL(7), SR(8)
375 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
376 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
377 * This requires an ALSA configuration file to correct.
378 */
379static int aaci_rule_channels(struct snd_pcm_hw_params *p,
380 struct snd_pcm_hw_rule *rule)
381{
382 static unsigned int channel_list[] = { 2, 4, 6 };
383 struct aaci *aaci = rule->private;
384 unsigned int mask = 1 << 0, slots;
385
386 /* pcms[0] is the our 5.1 PCM instance. */
387 slots = aaci->ac97_bus->pcms[0].r[0].slots;
388 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
389 mask |= 1 << 1;
390 if (slots & (1 << AC97_SLOT_LFE))
391 mask |= 1 << 2;
392 }
393
394 return snd_interval_list(hw_param_interval(p, rule->var),
395 ARRAY_SIZE(channel_list), channel_list, mask);
396}
397
398static int aaci_pcm_open(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200399{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100400 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kinge831d802011-01-13 10:13:17 +0000401 struct aaci *aaci = substream->private_data;
402 struct aaci_runtime *aacirun;
Russell Kingb60fb512011-01-25 15:52:33 +0000403 int ret = 0;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200404
Russell Kinge831d802011-01-13 10:13:17 +0000405 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
406 aacirun = &aaci->playback;
407 } else {
408 aacirun = &aaci->capture;
409 }
410
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200411 aacirun->substream = substream;
412 runtime->private_data = aacirun;
413 runtime->hw = aaci_hw_info;
Russell King6ca867c2009-12-18 17:48:35 +0000414 runtime->hw.rates = aacirun->pcm->rates;
415 snd_pcm_limit_hw_rates(runtime);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200416
Russell Kinge831d802011-01-13 10:13:17 +0000417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
418 runtime->hw.channels_max = 6;
419
420 /* Add rule describing channel dependency. */
421 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
422 SNDRV_PCM_HW_PARAM_CHANNELS,
423 aaci_rule_channels, aaci,
424 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
425 if (ret)
426 return ret;
427
428 if (aacirun->pcm->r[1].slots)
429 snd_ac97_pcm_double_rate_rules(runtime);
430 }
Russell Kinga08d5652009-12-18 17:48:45 +0000431
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200432 /*
Russell King5d350cb2011-01-13 22:25:10 +0000433 * ALSA wants the byte-size of the FIFOs. As we only support
434 * 16-bit samples, this is twice the FIFO depth irrespective
435 * of whether it's in compact mode or not.
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200436 */
Russell King5d350cb2011-01-13 22:25:10 +0000437 runtime->hw.fifo_size = aaci->fifo_depth * 2;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200438
Russell Kingb60fb512011-01-25 15:52:33 +0000439 mutex_lock(&aaci->irq_lock);
440 if (!aaci->users++) {
441 ret = request_irq(aaci->dev->irq[0], aaci_irq,
442 IRQF_SHARED | IRQF_DISABLED, DRIVER_NAME, aaci);
443 if (ret != 0)
444 aaci->users--;
445 }
446 mutex_unlock(&aaci->irq_lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200447
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200448 return ret;
449}
450
451
452/*
453 * Common ALSA stuff
454 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100455static int aaci_pcm_close(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200456{
457 struct aaci *aaci = substream->private_data;
458 struct aaci_runtime *aacirun = substream->runtime->private_data;
459
Kevin Hilman41762b82007-02-07 05:45:32 +0100460 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200461
462 aacirun->substream = NULL;
Russell Kingb60fb512011-01-25 15:52:33 +0000463
464 mutex_lock(&aaci->irq_lock);
465 if (!--aaci->users)
466 free_irq(aaci->dev->irq[0], aaci);
467 mutex_unlock(&aaci->irq_lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200468
469 return 0;
470}
471
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100472static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200473{
474 struct aaci_runtime *aacirun = substream->runtime->private_data;
475
476 /*
477 * This must not be called with the device enabled.
478 */
Kevin Hilman41762b82007-02-07 05:45:32 +0100479 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200480
481 if (aacirun->pcm_open)
482 snd_ac97_pcm_close(aacirun->pcm);
483 aacirun->pcm_open = 0;
484
485 /*
486 * Clear out the DMA and any allocated buffers.
487 */
Takashi Iwaid6797322009-11-26 15:08:54 +0100488 snd_pcm_lib_free_pages(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200489
490 return 0;
491}
492
Russell King58e8a472011-01-26 16:59:39 +0000493/* Channel to slot mask */
494static const u32 channels_to_slotmask[] = {
495 [2] = CR_SL3 | CR_SL4,
496 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
497 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
498};
499
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100500static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100501 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200502{
Russell King58e8a472011-01-26 16:59:39 +0000503 struct aaci_runtime *aacirun = substream->runtime->private_data;
504 unsigned int channels = params_channels(params);
505 unsigned int rate = params_rate(params);
506 int dbl = rate > 48000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200507 int err;
508
509 aaci_pcm_hw_free(substream);
Russell King4acd57c2009-11-29 16:39:52 +0000510 if (aacirun->pcm_open) {
511 snd_ac97_pcm_close(aacirun->pcm);
512 aacirun->pcm_open = 0;
513 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200514
Russell King58e8a472011-01-26 16:59:39 +0000515 /* channels is already limited to 2, 4, or 6 by aaci_rule_channels */
516 if (dbl && channels != 2)
517 return -EINVAL;
518
Takashi Iwaid6797322009-11-26 15:08:54 +0100519 err = snd_pcm_lib_malloc_pages(substream,
520 params_buffer_bytes(params));
Russell King4e30b692009-12-18 17:48:37 +0000521 if (err >= 0) {
Russell King58e8a472011-01-26 16:59:39 +0000522 struct aaci *aaci = substream->private_data;
Russell Kinga08d5652009-12-18 17:48:45 +0000523
Russell King58e8a472011-01-26 16:59:39 +0000524 err = snd_ac97_pcm_open(aacirun->pcm, rate, channels,
Russell Kinga08d5652009-12-18 17:48:45 +0000525 aacirun->pcm->r[dbl].slots);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200526
Russell King4e30b692009-12-18 17:48:37 +0000527 aacirun->pcm_open = err == 0;
Russell Kingd3aee792009-12-18 17:48:40 +0000528 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
Russell King58e8a472011-01-26 16:59:39 +0000529 aacirun->cr |= channels_to_slotmask[channels + dbl * 2];
Russell Kingd3aee792009-12-18 17:48:40 +0000530
Russell King5d350cb2011-01-13 22:25:10 +0000531 /*
532 * fifo_bytes is the number of bytes we transfer to/from
533 * the FIFO, including padding. So that's x4. As we're
534 * in compact mode, the FIFO is half the size.
535 */
536 aacirun->fifo_bytes = aaci->fifo_depth * 4 / 2;
Russell King4e30b692009-12-18 17:48:37 +0000537 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200538
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200539 return err;
540}
541
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100542static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200543{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100544 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200545 struct aaci_runtime *aacirun = runtime->private_data;
546
Russell Kingc0dea822011-01-13 00:34:08 +0000547 aacirun->period = snd_pcm_lib_period_bytes(substream);
Russell King4e30b692009-12-18 17:48:37 +0000548 aacirun->start = runtime->dma_area;
Russell King88cdca92009-11-23 09:44:10 +0100549 aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200550 aacirun->ptr = aacirun->start;
Russell Kingc0dea822011-01-13 00:34:08 +0000551 aacirun->bytes = aacirun->period;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200552
553 return 0;
554}
555
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100556static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200557{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100558 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200559 struct aaci_runtime *aacirun = runtime->private_data;
560 ssize_t bytes = aacirun->ptr - aacirun->start;
561
562 return bytes_to_frames(runtime, bytes);
563}
564
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200565
566/*
567 * Playback specific ALSA stuff
568 */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200569static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
570{
571 u32 ie;
572
573 ie = readl(aacirun->base + AACI_IE);
574 ie &= ~(IE_URIE|IE_TXIE);
575 writel(ie, aacirun->base + AACI_IE);
Kevin Hilman41762b82007-02-07 05:45:32 +0100576 aacirun->cr &= ~CR_EN;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000577 aaci_chan_wait_ready(aacirun, SR_TXB);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200578 writel(aacirun->cr, aacirun->base + AACI_TXCR);
579}
580
581static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
582{
583 u32 ie;
584
Russell Kingd6a89fe2009-12-18 17:48:50 +0000585 aaci_chan_wait_ready(aacirun, SR_TXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100586 aacirun->cr |= CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200587
588 ie = readl(aacirun->base + AACI_IE);
589 ie |= IE_URIE | IE_TXIE;
590 writel(ie, aacirun->base + AACI_IE);
591 writel(aacirun->cr, aacirun->base + AACI_TXCR);
592}
593
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100594static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200595{
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200596 struct aaci_runtime *aacirun = substream->runtime->private_data;
597 unsigned long flags;
598 int ret = 0;
599
Russell Kingd6a89fe2009-12-18 17:48:50 +0000600 spin_lock_irqsave(&aacirun->lock, flags);
601
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200602 switch (cmd) {
603 case SNDRV_PCM_TRIGGER_START:
604 aaci_pcm_playback_start(aacirun);
605 break;
606
607 case SNDRV_PCM_TRIGGER_RESUME:
608 aaci_pcm_playback_start(aacirun);
609 break;
610
611 case SNDRV_PCM_TRIGGER_STOP:
612 aaci_pcm_playback_stop(aacirun);
613 break;
614
615 case SNDRV_PCM_TRIGGER_SUSPEND:
616 aaci_pcm_playback_stop(aacirun);
617 break;
618
619 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
620 break;
621
622 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
623 break;
624
625 default:
626 ret = -EINVAL;
627 }
Russell Kingd6a89fe2009-12-18 17:48:50 +0000628
629 spin_unlock_irqrestore(&aacirun->lock, flags);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200630
631 return ret;
632}
633
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100634static struct snd_pcm_ops aaci_playback_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100635 .open = aaci_pcm_open,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200636 .close = aaci_pcm_close,
637 .ioctl = snd_pcm_lib_ioctl,
Russell King58e8a472011-01-26 16:59:39 +0000638 .hw_params = aaci_pcm_hw_params,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200639 .hw_free = aaci_pcm_hw_free,
640 .prepare = aaci_pcm_prepare,
641 .trigger = aaci_pcm_playback_trigger,
642 .pointer = aaci_pcm_pointer,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200643};
644
Kevin Hilman41762b82007-02-07 05:45:32 +0100645static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
646{
647 u32 ie;
648
Russell Kingd6a89fe2009-12-18 17:48:50 +0000649 aaci_chan_wait_ready(aacirun, SR_RXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100650
651 ie = readl(aacirun->base + AACI_IE);
652 ie &= ~(IE_ORIE | IE_RXIE);
653 writel(ie, aacirun->base+AACI_IE);
654
655 aacirun->cr &= ~CR_EN;
656
657 writel(aacirun->cr, aacirun->base + AACI_RXCR);
658}
659
660static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
661{
662 u32 ie;
663
Russell Kingd6a89fe2009-12-18 17:48:50 +0000664 aaci_chan_wait_ready(aacirun, SR_RXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100665
666#ifdef DEBUG
667 /* RX Timeout value: bits 28:17 in RXCR */
668 aacirun->cr |= 0xf << 17;
669#endif
670
671 aacirun->cr |= CR_EN;
672 writel(aacirun->cr, aacirun->base + AACI_RXCR);
673
674 ie = readl(aacirun->base + AACI_IE);
675 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
676 writel(ie, aacirun->base + AACI_IE);
677}
678
Russell King8a371842007-02-20 15:44:23 +0000679static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
680{
Kevin Hilman41762b82007-02-07 05:45:32 +0100681 struct aaci_runtime *aacirun = substream->runtime->private_data;
682 unsigned long flags;
683 int ret = 0;
684
Russell Kingd6a89fe2009-12-18 17:48:50 +0000685 spin_lock_irqsave(&aacirun->lock, flags);
Kevin Hilman41762b82007-02-07 05:45:32 +0100686
687 switch (cmd) {
688 case SNDRV_PCM_TRIGGER_START:
689 aaci_pcm_capture_start(aacirun);
690 break;
691
692 case SNDRV_PCM_TRIGGER_RESUME:
693 aaci_pcm_capture_start(aacirun);
694 break;
695
696 case SNDRV_PCM_TRIGGER_STOP:
697 aaci_pcm_capture_stop(aacirun);
698 break;
699
700 case SNDRV_PCM_TRIGGER_SUSPEND:
701 aaci_pcm_capture_stop(aacirun);
702 break;
703
704 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
705 break;
706
707 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
708 break;
709
710 default:
711 ret = -EINVAL;
712 }
713
Russell Kingd6a89fe2009-12-18 17:48:50 +0000714 spin_unlock_irqrestore(&aacirun->lock, flags);
Kevin Hilman41762b82007-02-07 05:45:32 +0100715
716 return ret;
717}
718
Russell King8a371842007-02-20 15:44:23 +0000719static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
Kevin Hilman41762b82007-02-07 05:45:32 +0100720{
721 struct snd_pcm_runtime *runtime = substream->runtime;
722 struct aaci *aaci = substream->private_data;
723
724 aaci_pcm_prepare(substream);
725
726 /* allow changing of sample rate */
727 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
728 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
729 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
730
731 /* Record select: Mic: 0, Aux: 3, Line: 4 */
732 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
733
734 return 0;
735}
736
Russell King8a371842007-02-20 15:44:23 +0000737static struct snd_pcm_ops aaci_capture_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100738 .open = aaci_pcm_open,
739 .close = aaci_pcm_close,
740 .ioctl = snd_pcm_lib_ioctl,
Russell King58e8a472011-01-26 16:59:39 +0000741 .hw_params = aaci_pcm_hw_params,
Kevin Hilman41762b82007-02-07 05:45:32 +0100742 .hw_free = aaci_pcm_hw_free,
743 .prepare = aaci_pcm_capture_prepare,
744 .trigger = aaci_pcm_capture_trigger,
745 .pointer = aaci_pcm_pointer,
Kevin Hilman41762b82007-02-07 05:45:32 +0100746};
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200747
748/*
749 * Power Management.
750 */
751#ifdef CONFIG_PM
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100752static int aaci_do_suspend(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200753{
754 struct aaci *aaci = card->private_data;
Takashi Iwai792a6c52005-11-17 17:19:25 +0100755 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
756 snd_pcm_suspend_all(aaci->pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200757 return 0;
758}
759
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100760static int aaci_do_resume(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200761{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100762 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200763 return 0;
764}
765
Richard Purdiee36d3942005-09-16 19:27:53 -0700766static int aaci_suspend(struct amba_device *dev, pm_message_t state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200767{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100768 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200769 return card ? aaci_do_suspend(card) : 0;
770}
771
772static int aaci_resume(struct amba_device *dev)
773{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100774 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200775 return card ? aaci_do_resume(card) : 0;
776}
777#else
778#define aaci_do_suspend NULL
779#define aaci_do_resume NULL
780#define aaci_suspend NULL
781#define aaci_resume NULL
782#endif
783
784
785static struct ac97_pcm ac97_defs[] __devinitdata = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100786 [0] = { /* Front PCM */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200787 .exclusive = 1,
788 .r = {
789 [0] = {
790 .slots = (1 << AC97_SLOT_PCM_LEFT) |
791 (1 << AC97_SLOT_PCM_RIGHT) |
792 (1 << AC97_SLOT_PCM_CENTER) |
793 (1 << AC97_SLOT_PCM_SLEFT) |
794 (1 << AC97_SLOT_PCM_SRIGHT) |
795 (1 << AC97_SLOT_LFE),
796 },
Russell Kinga08d5652009-12-18 17:48:45 +0000797 [1] = {
798 .slots = (1 << AC97_SLOT_PCM_LEFT) |
799 (1 << AC97_SLOT_PCM_RIGHT) |
800 (1 << AC97_SLOT_PCM_LEFT_0) |
801 (1 << AC97_SLOT_PCM_RIGHT_0),
802 },
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200803 },
804 },
805 [1] = { /* PCM in */
806 .stream = 1,
807 .exclusive = 1,
808 .r = {
809 [0] = {
810 .slots = (1 << AC97_SLOT_PCM_LEFT) |
811 (1 << AC97_SLOT_PCM_RIGHT),
812 },
813 },
814 },
815 [2] = { /* Mic in */
816 .stream = 1,
817 .exclusive = 1,
818 .r = {
819 [0] = {
820 .slots = (1 << AC97_SLOT_MIC),
821 },
822 },
823 }
824};
825
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100826static struct snd_ac97_bus_ops aaci_bus_ops = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200827 .write = aaci_ac97_write,
828 .read = aaci_ac97_read,
829};
830
831static int __devinit aaci_probe_ac97(struct aaci *aaci)
832{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100833 struct snd_ac97_template ac97_template;
834 struct snd_ac97_bus *ac97_bus;
835 struct snd_ac97 *ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200836 int ret;
837
838 /*
839 * Assert AACIRESET for 2us
840 */
841 writel(0, aaci->base + AACI_RESET);
842 udelay(2);
843 writel(RESET_NRST, aaci->base + AACI_RESET);
844
845 /*
846 * Give the AC'97 codec more than enough time
847 * to wake up. (42us = ~2 frames at 48kHz.)
848 */
Russell King250c7a62011-01-12 23:42:57 +0000849 udelay(FRAME_PERIOD_US * 2);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200850
851 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
852 if (ret)
853 goto out;
854
855 ac97_bus->clock = 48000;
856 aaci->ac97_bus = ac97_bus;
857
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100858 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200859 ac97_template.private_data = aaci;
860 ac97_template.num = 0;
861 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
862
863 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
864 if (ret)
865 goto out;
Kevin Hilman41762b82007-02-07 05:45:32 +0100866 aaci->ac97 = ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200867
868 /*
869 * Disable AC97 PC Beep input on audio codecs.
870 */
871 if (ac97_is_audio(ac97))
872 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
873
874 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
875 if (ret)
876 goto out;
877
878 aaci->playback.pcm = &ac97_bus->pcms[0];
Kevin Hilman41762b82007-02-07 05:45:32 +0100879 aaci->capture.pcm = &ac97_bus->pcms[1];
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200880
881 out:
882 return ret;
883}
884
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100885static void aaci_free_card(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200886{
887 struct aaci *aaci = card->private_data;
888 if (aaci->base)
889 iounmap(aaci->base);
890}
891
892static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
893{
894 struct aaci *aaci;
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100895 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100896 int err;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200897
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100898 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
899 THIS_MODULE, sizeof(struct aaci), &card);
900 if (err < 0)
Takashi Iwai631e8ad2008-09-01 15:31:50 +0200901 return NULL;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200902
903 card->private_free = aaci_free_card;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200904
905 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
906 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
907 snprintf(card->longname, sizeof(card->longname),
Russell Kingf006d8fc2011-01-12 23:46:03 +0000908 "%s PL%03x rev%u at 0x%08llx, irq %d",
909 card->shortname, amba_part(dev), amba_rev(dev),
910 (unsigned long long)dev->res.start, dev->irq[0]);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200911
912 aaci = card->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100913 mutex_init(&aaci->ac97_sem);
Russell Kingb60fb512011-01-25 15:52:33 +0000914 mutex_init(&aaci->irq_lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200915 aaci->card = card;
916 aaci->dev = dev;
917
918 /* Set MAINCR to allow slot 1 and 2 data IO */
919 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
920 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
921
922 return aaci;
923}
924
925static int __devinit aaci_init_pcm(struct aaci *aaci)
926{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100927 struct snd_pcm *pcm;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200928 int ret;
929
Kevin Hilman41762b82007-02-07 05:45:32 +0100930 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200931 if (ret == 0) {
932 aaci->pcm = pcm;
933 pcm->private_data = aaci;
934 pcm->info_flags = 0;
935
936 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
937
938 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
Kevin Hilman41762b82007-02-07 05:45:32 +0100939 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
Takashi Iwaid6797322009-11-26 15:08:54 +0100940 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaid4946432009-12-18 20:25:30 +0100941 NULL, 0, 64 * 1024);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200942 }
943
944 return ret;
945}
946
947static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
948{
Kevin Hilman41762b82007-02-07 05:45:32 +0100949 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200950 int i;
951
Russell King5d350cb2011-01-13 22:25:10 +0000952 /*
953 * Enable the channel, but don't assign it to any slots, so
954 * it won't empty onto the AC'97 link.
955 */
Kevin Hilman41762b82007-02-07 05:45:32 +0100956 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200957
Kevin Hilman41762b82007-02-07 05:45:32 +0100958 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
959 writel(0, aacirun->fifo);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200960
Kevin Hilman41762b82007-02-07 05:45:32 +0100961 writel(0, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200962
963 /*
964 * Re-initialise the AACI after the FIFO depth test, to
965 * ensure that the FIFOs are empty. Unfortunately, merely
966 * disabling the channel doesn't clear the FIFO.
967 */
968 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
969 writel(aaci->maincr, aaci->base + AACI_MAINCR);
970
971 /*
Russell King5d350cb2011-01-13 22:25:10 +0000972 * If we hit 4096 entries, we failed. Go back to the specified
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200973 * fifo depth.
974 */
975 if (i == 4096)
976 i = 8;
977
978 return i;
979}
980
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100981static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200982{
983 struct aaci *aaci;
984 int ret, i;
985
986 ret = amba_request_regions(dev, NULL);
987 if (ret)
988 return ret;
989
990 aaci = aaci_init_card(dev);
Takashi Iwai631e8ad2008-09-01 15:31:50 +0200991 if (!aaci) {
992 ret = -ENOMEM;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200993 goto out;
994 }
995
Linus Walleijdc890c22009-06-07 23:27:31 +0100996 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200997 if (!aaci->base) {
998 ret = -ENOMEM;
999 goto out;
1000 }
1001
1002 /*
1003 * Playback uses AACI channel 0
1004 */
Russell Kingd6a89fe2009-12-18 17:48:50 +00001005 spin_lock_init(&aaci->playback.lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001006 aaci->playback.base = aaci->base + AACI_CSCH1;
1007 aaci->playback.fifo = aaci->base + AACI_DR1;
1008
Kevin Hilman41762b82007-02-07 05:45:32 +01001009 /*
1010 * Capture uses AACI channel 0
1011 */
Russell Kingd6a89fe2009-12-18 17:48:50 +00001012 spin_lock_init(&aaci->capture.lock);
Kevin Hilman41762b82007-02-07 05:45:32 +01001013 aaci->capture.base = aaci->base + AACI_CSCH1;
1014 aaci->capture.fifo = aaci->base + AACI_DR1;
1015
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001016 for (i = 0; i < 4; i++) {
viro@ZenIV.linux.org.uke12ba642005-09-06 02:06:57 +01001017 void __iomem *base = aaci->base + i * 0x14;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001018
1019 writel(0, base + AACI_IE);
1020 writel(0, base + AACI_TXCR);
1021 writel(0, base + AACI_RXCR);
1022 }
1023
1024 writel(0x1fff, aaci->base + AACI_INTCLR);
1025 writel(aaci->maincr, aaci->base + AACI_MAINCR);
Philby Johnb68b58f2010-03-26 21:37:51 +05301026 /*
1027 * Fix: ac97 read back fail errors by reading
1028 * from any arbitrary aaci register.
1029 */
1030 readl(aaci->base + AACI_CSCH1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001031 ret = aaci_probe_ac97(aaci);
1032 if (ret)
1033 goto out;
1034
Catalin Marinasf27f2182006-02-01 19:25:58 +00001035 /*
1036 * Size the FIFOs (must be multiple of 16).
Russell King5d350cb2011-01-13 22:25:10 +00001037 * This is the number of entries in the FIFO.
Catalin Marinasf27f2182006-02-01 19:25:58 +00001038 */
Russell King5d350cb2011-01-13 22:25:10 +00001039 aaci->fifo_depth = aaci_size_fifo(aaci);
1040 if (aaci->fifo_depth & 15) {
1041 printk(KERN_WARNING "AACI: FIFO depth %d not supported\n",
1042 aaci->fifo_depth);
Catalin Marinasf27f2182006-02-01 19:25:58 +00001043 ret = -ENODEV;
1044 goto out;
1045 }
1046
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001047 ret = aaci_init_pcm(aaci);
1048 if (ret)
1049 goto out;
1050
Takashi Iwaia76af192005-09-05 16:56:40 +02001051 snd_card_set_dev(aaci->card, &dev->dev);
1052
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001053 ret = snd_card_register(aaci->card);
1054 if (ret == 0) {
Russell King5d350cb2011-01-13 22:25:10 +00001055 dev_info(&dev->dev, "%s\n", aaci->card->longname);
1056 dev_info(&dev->dev, "FIFO %u entries\n", aaci->fifo_depth);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001057 amba_set_drvdata(dev, aaci->card);
1058 return ret;
1059 }
1060
1061 out:
1062 if (aaci)
1063 snd_card_free(aaci->card);
1064 amba_release_regions(dev);
1065 return ret;
1066}
1067
1068static int __devexit aaci_remove(struct amba_device *dev)
1069{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001070 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001071
1072 amba_set_drvdata(dev, NULL);
1073
1074 if (card) {
1075 struct aaci *aaci = card->private_data;
1076 writel(0, aaci->base + AACI_MAINCR);
1077
1078 snd_card_free(card);
1079 amba_release_regions(dev);
1080 }
1081
1082 return 0;
1083}
1084
1085static struct amba_id aaci_ids[] = {
1086 {
1087 .id = 0x00041041,
1088 .mask = 0x000fffff,
1089 },
1090 { 0, 0 },
1091};
1092
1093static struct amba_driver aaci_driver = {
1094 .drv = {
1095 .name = DRIVER_NAME,
1096 },
1097 .probe = aaci_probe,
1098 .remove = __devexit_p(aaci_remove),
1099 .suspend = aaci_suspend,
1100 .resume = aaci_resume,
1101 .id_table = aaci_ids,
1102};
1103
1104static int __init aaci_init(void)
1105{
1106 return amba_driver_register(&aaci_driver);
1107}
1108
1109static void __exit aaci_exit(void)
1110{
1111 amba_driver_unregister(&aaci_driver);
1112}
1113
1114module_init(aaci_init);
1115module_exit(aaci_exit);
1116
1117MODULE_LICENSE("GPL");
1118MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");