blob: ab66d462d865c7d56552fea2ccad94d41a58f390 [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;
209 void *ptr;
210
211 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700212 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
Kevin Hilman41762b82007-02-07 05:45:32 +0100213 writel(0, aacirun->base + AACI_IE);
214 return;
215 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100216
Russell Kingd6a89fe2009-12-18 17:48:50 +0000217 spin_lock(&aacirun->lock);
218
219 ptr = aacirun->ptr;
Kevin Hilman41762b82007-02-07 05:45:32 +0100220 do {
221 unsigned int len = aacirun->fifosz;
222 u32 val;
223
224 if (aacirun->bytes <= 0) {
225 aacirun->bytes += aacirun->period;
226 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000227 spin_unlock(&aacirun->lock);
Kevin Hilman41762b82007-02-07 05:45:32 +0100228 snd_pcm_period_elapsed(aacirun->substream);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000229 spin_lock(&aacirun->lock);
Kevin Hilman41762b82007-02-07 05:45:32 +0100230 }
231 if (!(aacirun->cr & CR_EN))
232 break;
233
234 val = readl(aacirun->base + AACI_SR);
235 if (!(val & SR_RXHF))
236 break;
237 if (!(val & SR_RXFF))
238 len >>= 1;
239
240 aacirun->bytes -= len;
241
242 /* reading 16 bytes at a time */
243 for( ; len > 0; len -= 16) {
244 asm(
245 "ldmia %1, {r0, r1, r2, r3}\n\t"
246 "stmia %0!, {r0, r1, r2, r3}"
247 : "+r" (ptr)
248 : "r" (aacirun->fifo)
249 : "r0", "r1", "r2", "r3", "cc");
250
251 if (ptr >= aacirun->end)
252 ptr = aacirun->start;
253 }
254 } while(1);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000255
Kevin Hilman41762b82007-02-07 05:45:32 +0100256 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000257
258 spin_unlock(&aacirun->lock);
Kevin Hilman41762b82007-02-07 05:45:32 +0100259 }
260
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200261 if (mask & ISR_URINTR) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100262 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
263 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200264 }
265
266 if (mask & ISR_TXINTR) {
267 struct aaci_runtime *aacirun = &aaci->playback;
268 void *ptr;
269
270 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700271 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200272 writel(0, aacirun->base + AACI_IE);
273 return;
274 }
275
Russell Kingd6a89fe2009-12-18 17:48:50 +0000276 spin_lock(&aacirun->lock);
277
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200278 ptr = aacirun->ptr;
279 do {
280 unsigned int len = aacirun->fifosz;
281 u32 val;
282
283 if (aacirun->bytes <= 0) {
284 aacirun->bytes += aacirun->period;
285 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000286 spin_unlock(&aacirun->lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200287 snd_pcm_period_elapsed(aacirun->substream);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000288 spin_lock(&aacirun->lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200289 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100290 if (!(aacirun->cr & CR_EN))
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200291 break;
292
293 val = readl(aacirun->base + AACI_SR);
294 if (!(val & SR_TXHE))
295 break;
296 if (!(val & SR_TXFE))
297 len >>= 1;
298
299 aacirun->bytes -= len;
300
301 /* writing 16 bytes at a time */
302 for ( ; len > 0; len -= 16) {
303 asm(
304 "ldmia %0!, {r0, r1, r2, r3}\n\t"
305 "stmia %1, {r0, r1, r2, r3}"
306 : "+r" (ptr)
307 : "r" (aacirun->fifo)
308 : "r0", "r1", "r2", "r3", "cc");
309
310 if (ptr >= aacirun->end)
311 ptr = aacirun->start;
312 }
313 } while (1);
314
315 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000316
317 spin_unlock(&aacirun->lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200318 }
319}
320
David Howells7d12e782006-10-05 14:55:46 +0100321static irqreturn_t aaci_irq(int irq, void *devid)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200322{
323 struct aaci *aaci = devid;
324 u32 mask;
325 int i;
326
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200327 mask = readl(aaci->base + AACI_ALLINTS);
328 if (mask) {
329 u32 m = mask;
330 for (i = 0; i < 4; i++, m >>= 7) {
331 if (m & 0x7f) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100332 aaci_fifo_irq(aaci, i, m);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200333 }
334 }
335 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200336
337 return mask ? IRQ_HANDLED : IRQ_NONE;
338}
339
340
341
342/*
343 * ALSA support.
344 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100345static struct snd_pcm_hardware aaci_hw_info = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200346 .info = SNDRV_PCM_INFO_MMAP |
347 SNDRV_PCM_INFO_MMAP_VALID |
348 SNDRV_PCM_INFO_INTERLEAVED |
349 SNDRV_PCM_INFO_BLOCK_TRANSFER |
350 SNDRV_PCM_INFO_RESUME,
351
352 /*
353 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
354 * words. It also doesn't support 12-bit at all.
355 */
356 .formats = SNDRV_PCM_FMTBIT_S16_LE,
357
Russell King6ca867c2009-12-18 17:48:35 +0000358 /* rates are setup from the AC'97 codec */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200359 .channels_min = 2,
Russell Kinge831d802011-01-13 10:13:17 +0000360 .channels_max = 2,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200361 .buffer_bytes_max = 64 * 1024,
362 .period_bytes_min = 256,
363 .period_bytes_max = PAGE_SIZE,
364 .periods_min = 4,
365 .periods_max = PAGE_SIZE / 16,
366};
367
Russell Kinge831d802011-01-13 10:13:17 +0000368/*
369 * We can support two and four channel audio. Unfortunately
370 * six channel audio requires a non-standard channel ordering:
371 * 2 -> FL(3), FR(4)
372 * 4 -> FL(3), FR(4), SL(7), SR(8)
373 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
374 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
375 * This requires an ALSA configuration file to correct.
376 */
377static int aaci_rule_channels(struct snd_pcm_hw_params *p,
378 struct snd_pcm_hw_rule *rule)
379{
380 static unsigned int channel_list[] = { 2, 4, 6 };
381 struct aaci *aaci = rule->private;
382 unsigned int mask = 1 << 0, slots;
383
384 /* pcms[0] is the our 5.1 PCM instance. */
385 slots = aaci->ac97_bus->pcms[0].r[0].slots;
386 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
387 mask |= 1 << 1;
388 if (slots & (1 << AC97_SLOT_LFE))
389 mask |= 1 << 2;
390 }
391
392 return snd_interval_list(hw_param_interval(p, rule->var),
393 ARRAY_SIZE(channel_list), channel_list, mask);
394}
395
396static int aaci_pcm_open(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200397{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100398 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kinge831d802011-01-13 10:13:17 +0000399 struct aaci *aaci = substream->private_data;
400 struct aaci_runtime *aacirun;
Russell Kingb60fb512011-01-25 15:52:33 +0000401 int ret = 0;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200402
Russell Kinge831d802011-01-13 10:13:17 +0000403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
404 aacirun = &aaci->playback;
405 } else {
406 aacirun = &aaci->capture;
407 }
408
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200409 aacirun->substream = substream;
410 runtime->private_data = aacirun;
411 runtime->hw = aaci_hw_info;
Russell King6ca867c2009-12-18 17:48:35 +0000412 runtime->hw.rates = aacirun->pcm->rates;
413 snd_pcm_limit_hw_rates(runtime);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200414
Russell Kinge831d802011-01-13 10:13:17 +0000415 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
416 runtime->hw.channels_max = 6;
417
418 /* Add rule describing channel dependency. */
419 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
420 SNDRV_PCM_HW_PARAM_CHANNELS,
421 aaci_rule_channels, aaci,
422 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
423 if (ret)
424 return ret;
425
426 if (aacirun->pcm->r[1].slots)
427 snd_ac97_pcm_double_rate_rules(runtime);
428 }
Russell Kinga08d5652009-12-18 17:48:45 +0000429
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200430 /*
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
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
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100493static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200494 struct aaci_runtime *aacirun,
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100495 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200496{
497 int err;
Peter Huewe903b0eb2009-12-26 03:27:45 +0100498 struct aaci *aaci = substream->private_data;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200499
500 aaci_pcm_hw_free(substream);
Russell King4acd57c2009-11-29 16:39:52 +0000501 if (aacirun->pcm_open) {
502 snd_ac97_pcm_close(aacirun->pcm);
503 aacirun->pcm_open = 0;
504 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200505
Takashi Iwaid6797322009-11-26 15:08:54 +0100506 err = snd_pcm_lib_malloc_pages(substream,
507 params_buffer_bytes(params));
Russell King4e30b692009-12-18 17:48:37 +0000508 if (err >= 0) {
Russell Kinga08d5652009-12-18 17:48:45 +0000509 unsigned int rate = params_rate(params);
510 int dbl = rate > 48000;
511
512 err = snd_ac97_pcm_open(aacirun->pcm, rate,
Russell King4e30b692009-12-18 17:48:37 +0000513 params_channels(params),
Russell Kinga08d5652009-12-18 17:48:45 +0000514 aacirun->pcm->r[dbl].slots);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200515
Russell King4e30b692009-12-18 17:48:37 +0000516 aacirun->pcm_open = err == 0;
Russell Kingd3aee792009-12-18 17:48:40 +0000517 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
518 aacirun->fifosz = aaci->fifosize * 4;
519
520 if (aacirun->cr & CR_COMPACT)
521 aacirun->fifosz >>= 1;
Russell King4e30b692009-12-18 17:48:37 +0000522 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200523
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200524 return err;
525}
526
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100527static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200528{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100529 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200530 struct aaci_runtime *aacirun = runtime->private_data;
531
Russell King4e30b692009-12-18 17:48:37 +0000532 aacirun->start = runtime->dma_area;
Russell King88cdca92009-11-23 09:44:10 +0100533 aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200534 aacirun->ptr = aacirun->start;
535 aacirun->period =
536 aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
537
538 return 0;
539}
540
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100541static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200542{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100543 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200544 struct aaci_runtime *aacirun = runtime->private_data;
545 ssize_t bytes = aacirun->ptr - aacirun->start;
546
547 return bytes_to_frames(runtime, bytes);
548}
549
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200550
551/*
552 * Playback specific ALSA stuff
553 */
554static const u32 channels_to_txmask[] = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100555 [2] = CR_SL3 | CR_SL4,
556 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
557 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200558};
559
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100560static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
561 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200562{
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200563 struct aaci_runtime *aacirun = substream->runtime->private_data;
564 unsigned int channels = params_channels(params);
565 int ret;
566
567 WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
568 !channels_to_txmask[channels]);
569
570 ret = aaci_pcm_hw_params(substream, aacirun, params);
571
572 /*
573 * Enable FIFO, compact mode, 16 bits per sample.
574 * FIXME: double rate slots?
575 */
Russell Kingd3aee792009-12-18 17:48:40 +0000576 if (ret >= 0)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200577 aacirun->cr |= channels_to_txmask[channels];
578
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200579 return ret;
580}
581
582static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
583{
584 u32 ie;
585
586 ie = readl(aacirun->base + AACI_IE);
587 ie &= ~(IE_URIE|IE_TXIE);
588 writel(ie, aacirun->base + AACI_IE);
Kevin Hilman41762b82007-02-07 05:45:32 +0100589 aacirun->cr &= ~CR_EN;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000590 aaci_chan_wait_ready(aacirun, SR_TXB);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200591 writel(aacirun->cr, aacirun->base + AACI_TXCR);
592}
593
594static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
595{
596 u32 ie;
597
Russell Kingd6a89fe2009-12-18 17:48:50 +0000598 aaci_chan_wait_ready(aacirun, SR_TXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100599 aacirun->cr |= CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200600
601 ie = readl(aacirun->base + AACI_IE);
602 ie |= IE_URIE | IE_TXIE;
603 writel(ie, aacirun->base + AACI_IE);
604 writel(aacirun->cr, aacirun->base + AACI_TXCR);
605}
606
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100607static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200608{
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200609 struct aaci_runtime *aacirun = substream->runtime->private_data;
610 unsigned long flags;
611 int ret = 0;
612
Russell Kingd6a89fe2009-12-18 17:48:50 +0000613 spin_lock_irqsave(&aacirun->lock, flags);
614
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200615 switch (cmd) {
616 case SNDRV_PCM_TRIGGER_START:
617 aaci_pcm_playback_start(aacirun);
618 break;
619
620 case SNDRV_PCM_TRIGGER_RESUME:
621 aaci_pcm_playback_start(aacirun);
622 break;
623
624 case SNDRV_PCM_TRIGGER_STOP:
625 aaci_pcm_playback_stop(aacirun);
626 break;
627
628 case SNDRV_PCM_TRIGGER_SUSPEND:
629 aaci_pcm_playback_stop(aacirun);
630 break;
631
632 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
633 break;
634
635 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
636 break;
637
638 default:
639 ret = -EINVAL;
640 }
Russell Kingd6a89fe2009-12-18 17:48:50 +0000641
642 spin_unlock_irqrestore(&aacirun->lock, flags);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200643
644 return ret;
645}
646
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100647static struct snd_pcm_ops aaci_playback_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100648 .open = aaci_pcm_open,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200649 .close = aaci_pcm_close,
650 .ioctl = snd_pcm_lib_ioctl,
651 .hw_params = aaci_pcm_playback_hw_params,
652 .hw_free = aaci_pcm_hw_free,
653 .prepare = aaci_pcm_prepare,
654 .trigger = aaci_pcm_playback_trigger,
655 .pointer = aaci_pcm_pointer,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200656};
657
Russell King8a371842007-02-20 15:44:23 +0000658static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
659 struct snd_pcm_hw_params *params)
Kevin Hilman41762b82007-02-07 05:45:32 +0100660{
Kevin Hilman41762b82007-02-07 05:45:32 +0100661 struct aaci_runtime *aacirun = substream->runtime->private_data;
662 int ret;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200663
Kevin Hilman41762b82007-02-07 05:45:32 +0100664 ret = aaci_pcm_hw_params(substream, aacirun, params);
Russell Kingd3aee792009-12-18 17:48:40 +0000665 if (ret >= 0)
Kevin Hilman41762b82007-02-07 05:45:32 +0100666 /* Line in record: slot 3 and 4 */
667 aacirun->cr |= CR_SL3 | CR_SL4;
668
Kevin Hilman41762b82007-02-07 05:45:32 +0100669 return ret;
670}
671
672static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
673{
674 u32 ie;
675
Russell Kingd6a89fe2009-12-18 17:48:50 +0000676 aaci_chan_wait_ready(aacirun, SR_RXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100677
678 ie = readl(aacirun->base + AACI_IE);
679 ie &= ~(IE_ORIE | IE_RXIE);
680 writel(ie, aacirun->base+AACI_IE);
681
682 aacirun->cr &= ~CR_EN;
683
684 writel(aacirun->cr, aacirun->base + AACI_RXCR);
685}
686
687static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
688{
689 u32 ie;
690
Russell Kingd6a89fe2009-12-18 17:48:50 +0000691 aaci_chan_wait_ready(aacirun, SR_RXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100692
693#ifdef DEBUG
694 /* RX Timeout value: bits 28:17 in RXCR */
695 aacirun->cr |= 0xf << 17;
696#endif
697
698 aacirun->cr |= CR_EN;
699 writel(aacirun->cr, aacirun->base + AACI_RXCR);
700
701 ie = readl(aacirun->base + AACI_IE);
702 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
703 writel(ie, aacirun->base + AACI_IE);
704}
705
Russell King8a371842007-02-20 15:44:23 +0000706static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
707{
Kevin Hilman41762b82007-02-07 05:45:32 +0100708 struct aaci_runtime *aacirun = substream->runtime->private_data;
709 unsigned long flags;
710 int ret = 0;
711
Russell Kingd6a89fe2009-12-18 17:48:50 +0000712 spin_lock_irqsave(&aacirun->lock, flags);
Kevin Hilman41762b82007-02-07 05:45:32 +0100713
714 switch (cmd) {
715 case SNDRV_PCM_TRIGGER_START:
716 aaci_pcm_capture_start(aacirun);
717 break;
718
719 case SNDRV_PCM_TRIGGER_RESUME:
720 aaci_pcm_capture_start(aacirun);
721 break;
722
723 case SNDRV_PCM_TRIGGER_STOP:
724 aaci_pcm_capture_stop(aacirun);
725 break;
726
727 case SNDRV_PCM_TRIGGER_SUSPEND:
728 aaci_pcm_capture_stop(aacirun);
729 break;
730
731 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
732 break;
733
734 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
735 break;
736
737 default:
738 ret = -EINVAL;
739 }
740
Russell Kingd6a89fe2009-12-18 17:48:50 +0000741 spin_unlock_irqrestore(&aacirun->lock, flags);
Kevin Hilman41762b82007-02-07 05:45:32 +0100742
743 return ret;
744}
745
Russell King8a371842007-02-20 15:44:23 +0000746static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
Kevin Hilman41762b82007-02-07 05:45:32 +0100747{
748 struct snd_pcm_runtime *runtime = substream->runtime;
749 struct aaci *aaci = substream->private_data;
750
751 aaci_pcm_prepare(substream);
752
753 /* allow changing of sample rate */
754 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
755 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
756 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
757
758 /* Record select: Mic: 0, Aux: 3, Line: 4 */
759 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
760
761 return 0;
762}
763
Russell King8a371842007-02-20 15:44:23 +0000764static struct snd_pcm_ops aaci_capture_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100765 .open = aaci_pcm_open,
766 .close = aaci_pcm_close,
767 .ioctl = snd_pcm_lib_ioctl,
768 .hw_params = aaci_pcm_capture_hw_params,
769 .hw_free = aaci_pcm_hw_free,
770 .prepare = aaci_pcm_capture_prepare,
771 .trigger = aaci_pcm_capture_trigger,
772 .pointer = aaci_pcm_pointer,
Kevin Hilman41762b82007-02-07 05:45:32 +0100773};
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200774
775/*
776 * Power Management.
777 */
778#ifdef CONFIG_PM
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100779static int aaci_do_suspend(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200780{
781 struct aaci *aaci = card->private_data;
Takashi Iwai792a6c52005-11-17 17:19:25 +0100782 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
783 snd_pcm_suspend_all(aaci->pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200784 return 0;
785}
786
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100787static int aaci_do_resume(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200788{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100789 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200790 return 0;
791}
792
Richard Purdiee36d3942005-09-16 19:27:53 -0700793static int aaci_suspend(struct amba_device *dev, pm_message_t state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200794{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100795 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200796 return card ? aaci_do_suspend(card) : 0;
797}
798
799static int aaci_resume(struct amba_device *dev)
800{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100801 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200802 return card ? aaci_do_resume(card) : 0;
803}
804#else
805#define aaci_do_suspend NULL
806#define aaci_do_resume NULL
807#define aaci_suspend NULL
808#define aaci_resume NULL
809#endif
810
811
812static struct ac97_pcm ac97_defs[] __devinitdata = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100813 [0] = { /* Front PCM */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200814 .exclusive = 1,
815 .r = {
816 [0] = {
817 .slots = (1 << AC97_SLOT_PCM_LEFT) |
818 (1 << AC97_SLOT_PCM_RIGHT) |
819 (1 << AC97_SLOT_PCM_CENTER) |
820 (1 << AC97_SLOT_PCM_SLEFT) |
821 (1 << AC97_SLOT_PCM_SRIGHT) |
822 (1 << AC97_SLOT_LFE),
823 },
Russell Kinga08d5652009-12-18 17:48:45 +0000824 [1] = {
825 .slots = (1 << AC97_SLOT_PCM_LEFT) |
826 (1 << AC97_SLOT_PCM_RIGHT) |
827 (1 << AC97_SLOT_PCM_LEFT_0) |
828 (1 << AC97_SLOT_PCM_RIGHT_0),
829 },
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200830 },
831 },
832 [1] = { /* PCM in */
833 .stream = 1,
834 .exclusive = 1,
835 .r = {
836 [0] = {
837 .slots = (1 << AC97_SLOT_PCM_LEFT) |
838 (1 << AC97_SLOT_PCM_RIGHT),
839 },
840 },
841 },
842 [2] = { /* Mic in */
843 .stream = 1,
844 .exclusive = 1,
845 .r = {
846 [0] = {
847 .slots = (1 << AC97_SLOT_MIC),
848 },
849 },
850 }
851};
852
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100853static struct snd_ac97_bus_ops aaci_bus_ops = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200854 .write = aaci_ac97_write,
855 .read = aaci_ac97_read,
856};
857
858static int __devinit aaci_probe_ac97(struct aaci *aaci)
859{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100860 struct snd_ac97_template ac97_template;
861 struct snd_ac97_bus *ac97_bus;
862 struct snd_ac97 *ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200863 int ret;
864
865 /*
866 * Assert AACIRESET for 2us
867 */
868 writel(0, aaci->base + AACI_RESET);
869 udelay(2);
870 writel(RESET_NRST, aaci->base + AACI_RESET);
871
872 /*
873 * Give the AC'97 codec more than enough time
874 * to wake up. (42us = ~2 frames at 48kHz.)
875 */
Russell King250c7a62011-01-12 23:42:57 +0000876 udelay(FRAME_PERIOD_US * 2);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200877
878 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
879 if (ret)
880 goto out;
881
882 ac97_bus->clock = 48000;
883 aaci->ac97_bus = ac97_bus;
884
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100885 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200886 ac97_template.private_data = aaci;
887 ac97_template.num = 0;
888 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
889
890 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
891 if (ret)
892 goto out;
Kevin Hilman41762b82007-02-07 05:45:32 +0100893 aaci->ac97 = ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200894
895 /*
896 * Disable AC97 PC Beep input on audio codecs.
897 */
898 if (ac97_is_audio(ac97))
899 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
900
901 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
902 if (ret)
903 goto out;
904
905 aaci->playback.pcm = &ac97_bus->pcms[0];
Kevin Hilman41762b82007-02-07 05:45:32 +0100906 aaci->capture.pcm = &ac97_bus->pcms[1];
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200907
908 out:
909 return ret;
910}
911
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100912static void aaci_free_card(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200913{
914 struct aaci *aaci = card->private_data;
915 if (aaci->base)
916 iounmap(aaci->base);
917}
918
919static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
920{
921 struct aaci *aaci;
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100922 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100923 int err;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200924
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100925 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
926 THIS_MODULE, sizeof(struct aaci), &card);
927 if (err < 0)
Takashi Iwai631e8ad2008-09-01 15:31:50 +0200928 return NULL;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200929
930 card->private_free = aaci_free_card;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200931
932 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
933 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
934 snprintf(card->longname, sizeof(card->longname),
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -0700935 "%s at 0x%016llx, irq %d",
936 card->shortname, (unsigned long long)dev->res.start,
937 dev->irq[0]);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200938
939 aaci = card->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100940 mutex_init(&aaci->ac97_sem);
Russell Kingb60fb512011-01-25 15:52:33 +0000941 mutex_init(&aaci->irq_lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200942 aaci->card = card;
943 aaci->dev = dev;
944
945 /* Set MAINCR to allow slot 1 and 2 data IO */
946 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
947 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
948
949 return aaci;
950}
951
952static int __devinit aaci_init_pcm(struct aaci *aaci)
953{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100954 struct snd_pcm *pcm;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200955 int ret;
956
Kevin Hilman41762b82007-02-07 05:45:32 +0100957 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200958 if (ret == 0) {
959 aaci->pcm = pcm;
960 pcm->private_data = aaci;
961 pcm->info_flags = 0;
962
963 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
964
965 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
Kevin Hilman41762b82007-02-07 05:45:32 +0100966 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
Takashi Iwaid6797322009-11-26 15:08:54 +0100967 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaid4946432009-12-18 20:25:30 +0100968 NULL, 0, 64 * 1024);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200969 }
970
971 return ret;
972}
973
974static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
975{
Kevin Hilman41762b82007-02-07 05:45:32 +0100976 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200977 int i;
978
Kevin Hilman41762b82007-02-07 05:45:32 +0100979 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200980
Kevin Hilman41762b82007-02-07 05:45:32 +0100981 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
982 writel(0, aacirun->fifo);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200983
Kevin Hilman41762b82007-02-07 05:45:32 +0100984 writel(0, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200985
986 /*
987 * Re-initialise the AACI after the FIFO depth test, to
988 * ensure that the FIFOs are empty. Unfortunately, merely
989 * disabling the channel doesn't clear the FIFO.
990 */
991 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
992 writel(aaci->maincr, aaci->base + AACI_MAINCR);
993
994 /*
995 * If we hit 4096, we failed. Go back to the specified
996 * fifo depth.
997 */
998 if (i == 4096)
999 i = 8;
1000
1001 return i;
1002}
1003
Alessandro Rubini03fbdb12009-05-20 22:39:08 +01001004static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001005{
1006 struct aaci *aaci;
1007 int ret, i;
1008
1009 ret = amba_request_regions(dev, NULL);
1010 if (ret)
1011 return ret;
1012
1013 aaci = aaci_init_card(dev);
Takashi Iwai631e8ad2008-09-01 15:31:50 +02001014 if (!aaci) {
1015 ret = -ENOMEM;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001016 goto out;
1017 }
1018
Linus Walleijdc890c22009-06-07 23:27:31 +01001019 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001020 if (!aaci->base) {
1021 ret = -ENOMEM;
1022 goto out;
1023 }
1024
1025 /*
1026 * Playback uses AACI channel 0
1027 */
Russell Kingd6a89fe2009-12-18 17:48:50 +00001028 spin_lock_init(&aaci->playback.lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001029 aaci->playback.base = aaci->base + AACI_CSCH1;
1030 aaci->playback.fifo = aaci->base + AACI_DR1;
1031
Kevin Hilman41762b82007-02-07 05:45:32 +01001032 /*
1033 * Capture uses AACI channel 0
1034 */
Russell Kingd6a89fe2009-12-18 17:48:50 +00001035 spin_lock_init(&aaci->capture.lock);
Kevin Hilman41762b82007-02-07 05:45:32 +01001036 aaci->capture.base = aaci->base + AACI_CSCH1;
1037 aaci->capture.fifo = aaci->base + AACI_DR1;
1038
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001039 for (i = 0; i < 4; i++) {
viro@ZenIV.linux.org.uke12ba642005-09-06 02:06:57 +01001040 void __iomem *base = aaci->base + i * 0x14;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001041
1042 writel(0, base + AACI_IE);
1043 writel(0, base + AACI_TXCR);
1044 writel(0, base + AACI_RXCR);
1045 }
1046
1047 writel(0x1fff, aaci->base + AACI_INTCLR);
1048 writel(aaci->maincr, aaci->base + AACI_MAINCR);
Philby Johnb68b58f2010-03-26 21:37:51 +05301049 /*
1050 * Fix: ac97 read back fail errors by reading
1051 * from any arbitrary aaci register.
1052 */
1053 readl(aaci->base + AACI_CSCH1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001054 ret = aaci_probe_ac97(aaci);
1055 if (ret)
1056 goto out;
1057
Catalin Marinasf27f2182006-02-01 19:25:58 +00001058 /*
1059 * Size the FIFOs (must be multiple of 16).
1060 */
1061 aaci->fifosize = aaci_size_fifo(aaci);
1062 if (aaci->fifosize & 15) {
1063 printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
1064 aaci->fifosize);
1065 ret = -ENODEV;
1066 goto out;
1067 }
1068
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001069 ret = aaci_init_pcm(aaci);
1070 if (ret)
1071 goto out;
1072
Takashi Iwaia76af192005-09-05 16:56:40 +02001073 snd_card_set_dev(aaci->card, &dev->dev);
1074
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001075 ret = snd_card_register(aaci->card);
1076 if (ret == 0) {
1077 dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
Kevin Hilman41762b82007-02-07 05:45:32 +01001078 aaci->fifosize);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001079 amba_set_drvdata(dev, aaci->card);
1080 return ret;
1081 }
1082
1083 out:
1084 if (aaci)
1085 snd_card_free(aaci->card);
1086 amba_release_regions(dev);
1087 return ret;
1088}
1089
1090static int __devexit aaci_remove(struct amba_device *dev)
1091{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001092 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001093
1094 amba_set_drvdata(dev, NULL);
1095
1096 if (card) {
1097 struct aaci *aaci = card->private_data;
1098 writel(0, aaci->base + AACI_MAINCR);
1099
1100 snd_card_free(card);
1101 amba_release_regions(dev);
1102 }
1103
1104 return 0;
1105}
1106
1107static struct amba_id aaci_ids[] = {
1108 {
1109 .id = 0x00041041,
1110 .mask = 0x000fffff,
1111 },
1112 { 0, 0 },
1113};
1114
1115static struct amba_driver aaci_driver = {
1116 .drv = {
1117 .name = DRIVER_NAME,
1118 },
1119 .probe = aaci_probe,
1120 .remove = __devexit_p(aaci_remove),
1121 .suspend = aaci_suspend,
1122 .resume = aaci_resume,
1123 .id_table = aaci_ids,
1124};
1125
1126static int __init aaci_init(void)
1127{
1128 return amba_driver_register(&aaci_driver);
1129}
1130
1131static void __exit aaci_exit(void)
1132{
1133 amba_driver_unregister(&aaci_driver);
1134}
1135
1136module_init(aaci_init);
1137module_exit(aaci_exit);
1138
1139MODULE_LICENSE("GPL");
1140MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");