blob: c5699863643b4241538b563299a6cc275de57c27 [file] [log] [blame]
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001/*
2 * linux/sound/arm/aaci.c - ARM PrimeCell AACI PL041 driver
3 *
4 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Documentation: ARM DDI 0173B
11 */
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/ioport.h>
16#include <linux/device.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
19#include <linux/err.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000020#include <linux/amba/bus.h>
Russell King88cdca92009-11-23 09:44:10 +010021#include <linux/io.h>
Russell Kingcb5a6ff2005-05-12 14:04:59 +020022
Russell Kingcb5a6ff2005-05-12 14:04:59 +020023#include <sound/core.h>
24#include <sound/initval.h>
25#include <sound/ac97_codec.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28
29#include "aaci.h"
Russell Kingcb5a6ff2005-05-12 14:04:59 +020030
31#define DRIVER_NAME "aaci-pl041"
32
33/*
34 * PM support is not complete. Turn it off.
35 */
36#undef CONFIG_PM
37
Takashi Iwaiceb9e472005-11-17 15:10:16 +010038static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020039{
40 u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
41
42 /*
43 * Ensure that the slot 1/2 RX registers are empty.
44 */
45 v = readl(aaci->base + AACI_SLFR);
46 if (v & SLFR_2RXV)
47 readl(aaci->base + AACI_SL2RX);
48 if (v & SLFR_1RXV)
49 readl(aaci->base + AACI_SL1RX);
50
51 writel(maincr, aaci->base + AACI_MAINCR);
52}
53
54/*
55 * P29:
56 * The recommended use of programming the external codec through slot 1
57 * and slot 2 data is to use the channels during setup routines and the
58 * slot register at any other time. The data written into slot 1, slot 2
59 * and slot 12 registers is transmitted only when their corresponding
60 * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
61 * register.
62 */
Kevin Hilman14d178a2007-02-07 05:46:47 +010063static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
64 unsigned short val)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020065{
66 struct aaci *aaci = ac97->private_data;
67 u32 v;
Kevin Hilman14d178a2007-02-07 05:46:47 +010068 int timeout = 5000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +020069
70 if (ac97->num >= 4)
71 return;
72
Ingo Molnar12aa7572006-01-16 16:36:05 +010073 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020074
75 aaci_ac97_select_codec(aaci, ac97);
76
77 /*
78 * P54: You must ensure that AACI_SL2TX is always written
79 * to, if required, before data is written to AACI_SL1TX.
80 */
81 writel(val << 4, aaci->base + AACI_SL2TX);
82 writel(reg << 12, aaci->base + AACI_SL1TX);
83
84 /*
85 * Wait for the transmission of both slots to complete.
86 */
87 do {
88 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +010089 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +010090
91 if (!timeout)
92 dev_err(&aaci->dev->dev,
93 "timeout waiting for write to complete\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +020094
Ingo Molnar12aa7572006-01-16 16:36:05 +010095 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020096}
97
98/*
99 * Read an AC'97 register.
100 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100101static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200102{
103 struct aaci *aaci = ac97->private_data;
104 u32 v;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100105 int timeout = 5000;
106 int retries = 10;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200107
108 if (ac97->num >= 4)
109 return ~0;
110
Ingo Molnar12aa7572006-01-16 16:36:05 +0100111 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200112
113 aaci_ac97_select_codec(aaci, ac97);
114
115 /*
116 * Write the register address to slot 1.
117 */
118 writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);
119
120 /*
121 * Wait for the transmission to complete.
122 */
123 do {
124 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100125 } while ((v & SLFR_1TXB) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +0100126
127 if (!timeout) {
128 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
129 v = ~0;
130 goto out;
131 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200132
133 /*
134 * Give the AC'97 codec more than enough time
135 * to respond. (42us = ~2 frames at 48kHz.)
136 */
137 udelay(42);
138
139 /*
140 * Wait for slot 2 to indicate data.
141 */
Kevin Hilman14d178a2007-02-07 05:46:47 +0100142 timeout = 5000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200143 do {
144 cond_resched();
145 v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100146 } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200147
Kevin Hilman14d178a2007-02-07 05:46:47 +0100148 if (!timeout) {
149 dev_err(&aaci->dev->dev, "timeout on RX valid\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200150 v = ~0;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100151 goto out;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200152 }
153
Kevin Hilman14d178a2007-02-07 05:46:47 +0100154 do {
155 v = readl(aaci->base + AACI_SL1RX) >> 12;
156 if (v == reg) {
157 v = readl(aaci->base + AACI_SL2RX) >> 4;
158 break;
159 } else if (--retries) {
160 dev_warn(&aaci->dev->dev,
161 "ac97 read back fail. retry\n");
162 continue;
163 } else {
164 dev_warn(&aaci->dev->dev,
165 "wrong ac97 register read back (%x != %x)\n",
166 v, reg);
167 v = ~0;
168 }
169 } while (retries);
170 out:
Ingo Molnar12aa7572006-01-16 16:36:05 +0100171 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200172 return v;
173}
174
Russell Kingd6a89fe2009-12-18 17:48:50 +0000175static inline void
176aaci_chan_wait_ready(struct aaci_runtime *aacirun, unsigned long mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200177{
178 u32 val;
179 int timeout = 5000;
180
181 do {
182 val = readl(aacirun->base + AACI_SR);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000183 } while (val & mask && timeout--);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200184}
185
186
187
188/*
189 * Interrupt support.
190 */
Kevin Hilman62578cb2007-02-07 05:41:37 +0100191static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200192{
Kevin Hilman41762b82007-02-07 05:45:32 +0100193 if (mask & ISR_ORINTR) {
194 dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
195 writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
196 }
197
198 if (mask & ISR_RXTOINTR) {
199 dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
200 writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
201 }
202
203 if (mask & ISR_RXINTR) {
204 struct aaci_runtime *aacirun = &aaci->capture;
205 void *ptr;
206
207 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700208 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
Kevin Hilman41762b82007-02-07 05:45:32 +0100209 writel(0, aacirun->base + AACI_IE);
210 return;
211 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100212
Russell Kingd6a89fe2009-12-18 17:48:50 +0000213 spin_lock(&aacirun->lock);
214
215 ptr = aacirun->ptr;
Kevin Hilman41762b82007-02-07 05:45:32 +0100216 do {
217 unsigned int len = aacirun->fifosz;
218 u32 val;
219
220 if (aacirun->bytes <= 0) {
221 aacirun->bytes += aacirun->period;
222 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000223 spin_unlock(&aacirun->lock);
Kevin Hilman41762b82007-02-07 05:45:32 +0100224 snd_pcm_period_elapsed(aacirun->substream);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000225 spin_lock(&aacirun->lock);
Kevin Hilman41762b82007-02-07 05:45:32 +0100226 }
227 if (!(aacirun->cr & CR_EN))
228 break;
229
230 val = readl(aacirun->base + AACI_SR);
231 if (!(val & SR_RXHF))
232 break;
233 if (!(val & SR_RXFF))
234 len >>= 1;
235
236 aacirun->bytes -= len;
237
238 /* reading 16 bytes at a time */
239 for( ; len > 0; len -= 16) {
240 asm(
241 "ldmia %1, {r0, r1, r2, r3}\n\t"
242 "stmia %0!, {r0, r1, r2, r3}"
243 : "+r" (ptr)
244 : "r" (aacirun->fifo)
245 : "r0", "r1", "r2", "r3", "cc");
246
247 if (ptr >= aacirun->end)
248 ptr = aacirun->start;
249 }
250 } while(1);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000251
Kevin Hilman41762b82007-02-07 05:45:32 +0100252 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000253
254 spin_unlock(&aacirun->lock);
Kevin Hilman41762b82007-02-07 05:45:32 +0100255 }
256
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200257 if (mask & ISR_URINTR) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100258 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
259 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200260 }
261
262 if (mask & ISR_TXINTR) {
263 struct aaci_runtime *aacirun = &aaci->playback;
264 void *ptr;
265
266 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700267 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200268 writel(0, aacirun->base + AACI_IE);
269 return;
270 }
271
Russell Kingd6a89fe2009-12-18 17:48:50 +0000272 spin_lock(&aacirun->lock);
273
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200274 ptr = aacirun->ptr;
275 do {
276 unsigned int len = aacirun->fifosz;
277 u32 val;
278
279 if (aacirun->bytes <= 0) {
280 aacirun->bytes += aacirun->period;
281 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000282 spin_unlock(&aacirun->lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200283 snd_pcm_period_elapsed(aacirun->substream);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000284 spin_lock(&aacirun->lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200285 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100286 if (!(aacirun->cr & CR_EN))
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200287 break;
288
289 val = readl(aacirun->base + AACI_SR);
290 if (!(val & SR_TXHE))
291 break;
292 if (!(val & SR_TXFE))
293 len >>= 1;
294
295 aacirun->bytes -= len;
296
297 /* writing 16 bytes at a time */
298 for ( ; len > 0; len -= 16) {
299 asm(
300 "ldmia %0!, {r0, r1, r2, r3}\n\t"
301 "stmia %1, {r0, r1, r2, r3}"
302 : "+r" (ptr)
303 : "r" (aacirun->fifo)
304 : "r0", "r1", "r2", "r3", "cc");
305
306 if (ptr >= aacirun->end)
307 ptr = aacirun->start;
308 }
309 } while (1);
310
311 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000312
313 spin_unlock(&aacirun->lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200314 }
315}
316
David Howells7d12e782006-10-05 14:55:46 +0100317static irqreturn_t aaci_irq(int irq, void *devid)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200318{
319 struct aaci *aaci = devid;
320 u32 mask;
321 int i;
322
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200323 mask = readl(aaci->base + AACI_ALLINTS);
324 if (mask) {
325 u32 m = mask;
326 for (i = 0; i < 4; i++, m >>= 7) {
327 if (m & 0x7f) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100328 aaci_fifo_irq(aaci, i, m);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200329 }
330 }
331 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200332
333 return mask ? IRQ_HANDLED : IRQ_NONE;
334}
335
336
337
338/*
339 * ALSA support.
340 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100341static struct snd_pcm_hardware aaci_hw_info = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200342 .info = SNDRV_PCM_INFO_MMAP |
343 SNDRV_PCM_INFO_MMAP_VALID |
344 SNDRV_PCM_INFO_INTERLEAVED |
345 SNDRV_PCM_INFO_BLOCK_TRANSFER |
346 SNDRV_PCM_INFO_RESUME,
347
348 /*
349 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
350 * words. It also doesn't support 12-bit at all.
351 */
352 .formats = SNDRV_PCM_FMTBIT_S16_LE,
353
Russell King6ca867c2009-12-18 17:48:35 +0000354 /* rates are setup from the AC'97 codec */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200355 .channels_min = 2,
356 .channels_max = 6,
357 .buffer_bytes_max = 64 * 1024,
358 .period_bytes_min = 256,
359 .period_bytes_max = PAGE_SIZE,
360 .periods_min = 4,
361 .periods_max = PAGE_SIZE / 16,
362};
363
Kevin Hilman41762b82007-02-07 05:45:32 +0100364static int __aaci_pcm_open(struct aaci *aaci,
365 struct snd_pcm_substream *substream,
366 struct aaci_runtime *aacirun)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200367{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100368 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200369 int ret;
370
371 aacirun->substream = substream;
372 runtime->private_data = aacirun;
373 runtime->hw = aaci_hw_info;
Russell King6ca867c2009-12-18 17:48:35 +0000374 runtime->hw.rates = aacirun->pcm->rates;
375 snd_pcm_limit_hw_rates(runtime);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200376
Russell Kinga08d5652009-12-18 17:48:45 +0000377 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
378 aacirun->pcm->r[1].slots)
379 snd_ac97_pcm_double_rate_rules(runtime);
380
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200381 /*
382 * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
383 * mode, each 32-bit word contains one sample. If we're in
384 * compact mode, each 32-bit word contains two samples, effectively
385 * halving the FIFO size. However, we don't know for sure which
386 * we'll be using at this point. We set this to the lower limit.
387 */
388 runtime->hw.fifo_size = aaci->fifosize * 2;
389
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700390 ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200391 DRIVER_NAME, aaci);
392 if (ret)
393 goto out;
394
395 return 0;
396
397 out:
398 return ret;
399}
400
401
402/*
403 * Common ALSA stuff
404 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100405static int aaci_pcm_close(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200406{
407 struct aaci *aaci = substream->private_data;
408 struct aaci_runtime *aacirun = substream->runtime->private_data;
409
Kevin Hilman41762b82007-02-07 05:45:32 +0100410 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200411
412 aacirun->substream = NULL;
413 free_irq(aaci->dev->irq[0], aaci);
414
415 return 0;
416}
417
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100418static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200419{
420 struct aaci_runtime *aacirun = substream->runtime->private_data;
421
422 /*
423 * This must not be called with the device enabled.
424 */
Kevin Hilman41762b82007-02-07 05:45:32 +0100425 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200426
427 if (aacirun->pcm_open)
428 snd_ac97_pcm_close(aacirun->pcm);
429 aacirun->pcm_open = 0;
430
431 /*
432 * Clear out the DMA and any allocated buffers.
433 */
Takashi Iwaid6797322009-11-26 15:08:54 +0100434 snd_pcm_lib_free_pages(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200435
436 return 0;
437}
438
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100439static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200440 struct aaci_runtime *aacirun,
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100441 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200442{
443 int err;
444
445 aaci_pcm_hw_free(substream);
Russell King4acd57c2009-11-29 16:39:52 +0000446 if (aacirun->pcm_open) {
447 snd_ac97_pcm_close(aacirun->pcm);
448 aacirun->pcm_open = 0;
449 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200450
Takashi Iwaid6797322009-11-26 15:08:54 +0100451 err = snd_pcm_lib_malloc_pages(substream,
452 params_buffer_bytes(params));
Russell King4e30b692009-12-18 17:48:37 +0000453 if (err >= 0) {
Russell Kinga08d5652009-12-18 17:48:45 +0000454 unsigned int rate = params_rate(params);
455 int dbl = rate > 48000;
456
457 err = snd_ac97_pcm_open(aacirun->pcm, rate,
Russell King4e30b692009-12-18 17:48:37 +0000458 params_channels(params),
Russell Kinga08d5652009-12-18 17:48:45 +0000459 aacirun->pcm->r[dbl].slots);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200460
Russell King4e30b692009-12-18 17:48:37 +0000461 aacirun->pcm_open = err == 0;
Russell Kingd3aee792009-12-18 17:48:40 +0000462 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
463 aacirun->fifosz = aaci->fifosize * 4;
464
465 if (aacirun->cr & CR_COMPACT)
466 aacirun->fifosz >>= 1;
Russell King4e30b692009-12-18 17:48:37 +0000467 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200468
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200469 return err;
470}
471
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100472static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200473{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100474 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200475 struct aaci_runtime *aacirun = runtime->private_data;
476
Russell King4e30b692009-12-18 17:48:37 +0000477 aacirun->start = runtime->dma_area;
Russell King88cdca92009-11-23 09:44:10 +0100478 aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200479 aacirun->ptr = aacirun->start;
480 aacirun->period =
481 aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
482
483 return 0;
484}
485
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100486static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200487{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100488 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200489 struct aaci_runtime *aacirun = runtime->private_data;
490 ssize_t bytes = aacirun->ptr - aacirun->start;
491
492 return bytes_to_frames(runtime, bytes);
493}
494
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200495
496/*
497 * Playback specific ALSA stuff
498 */
499static const u32 channels_to_txmask[] = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100500 [2] = CR_SL3 | CR_SL4,
501 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
502 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200503};
504
505/*
506 * We can support two and four channel audio. Unfortunately
507 * six channel audio requires a non-standard channel ordering:
508 * 2 -> FL(3), FR(4)
509 * 4 -> FL(3), FR(4), SL(7), SR(8)
510 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
511 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
512 * This requires an ALSA configuration file to correct.
513 */
514static unsigned int channel_list[] = { 2, 4, 6 };
515
516static int
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100517aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200518{
519 struct aaci *aaci = rule->private;
520 unsigned int chan_mask = 1 << 0, slots;
521
522 /*
523 * pcms[0] is the our 5.1 PCM instance.
524 */
525 slots = aaci->ac97_bus->pcms[0].r[0].slots;
526 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
527 chan_mask |= 1 << 1;
528 if (slots & (1 << AC97_SLOT_LFE))
529 chan_mask |= 1 << 2;
530 }
531
532 return snd_interval_list(hw_param_interval(p, rule->var),
533 ARRAY_SIZE(channel_list), channel_list,
534 chan_mask);
535}
536
Kevin Hilman41762b82007-02-07 05:45:32 +0100537static int aaci_pcm_open(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200538{
539 struct aaci *aaci = substream->private_data;
540 int ret;
541
542 /*
543 * Add rule describing channel dependency.
544 */
545 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
546 SNDRV_PCM_HW_PARAM_CHANNELS,
547 aaci_rule_channels, aaci,
548 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
549 if (ret)
550 return ret;
551
Kevin Hilman41762b82007-02-07 05:45:32 +0100552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
553 ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
554 } else {
555 ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
556 }
557 return ret;
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{
563 struct aaci *aaci = substream->private_data;
564 struct aaci_runtime *aacirun = substream->runtime->private_data;
565 unsigned int channels = params_channels(params);
566 int ret;
567
568 WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
569 !channels_to_txmask[channels]);
570
571 ret = aaci_pcm_hw_params(substream, aacirun, params);
572
573 /*
574 * Enable FIFO, compact mode, 16 bits per sample.
575 * FIXME: double rate slots?
576 */
Russell Kingd3aee792009-12-18 17:48:40 +0000577 if (ret >= 0)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200578 aacirun->cr |= channels_to_txmask[channels];
579
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200580 return ret;
581}
582
583static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
584{
585 u32 ie;
586
587 ie = readl(aacirun->base + AACI_IE);
588 ie &= ~(IE_URIE|IE_TXIE);
589 writel(ie, aacirun->base + AACI_IE);
Kevin Hilman41762b82007-02-07 05:45:32 +0100590 aacirun->cr &= ~CR_EN;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000591 aaci_chan_wait_ready(aacirun, SR_TXB);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200592 writel(aacirun->cr, aacirun->base + AACI_TXCR);
593}
594
595static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
596{
597 u32 ie;
598
Russell Kingd6a89fe2009-12-18 17:48:50 +0000599 aaci_chan_wait_ready(aacirun, SR_TXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100600 aacirun->cr |= CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200601
602 ie = readl(aacirun->base + AACI_IE);
603 ie |= IE_URIE | IE_TXIE;
604 writel(ie, aacirun->base + AACI_IE);
605 writel(aacirun->cr, aacirun->base + AACI_TXCR);
606}
607
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100608static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200609{
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200610 struct aaci_runtime *aacirun = substream->runtime->private_data;
611 unsigned long flags;
612 int ret = 0;
613
Russell Kingd6a89fe2009-12-18 17:48:50 +0000614 spin_lock_irqsave(&aacirun->lock, flags);
615
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200616 switch (cmd) {
617 case SNDRV_PCM_TRIGGER_START:
618 aaci_pcm_playback_start(aacirun);
619 break;
620
621 case SNDRV_PCM_TRIGGER_RESUME:
622 aaci_pcm_playback_start(aacirun);
623 break;
624
625 case SNDRV_PCM_TRIGGER_STOP:
626 aaci_pcm_playback_stop(aacirun);
627 break;
628
629 case SNDRV_PCM_TRIGGER_SUSPEND:
630 aaci_pcm_playback_stop(aacirun);
631 break;
632
633 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
634 break;
635
636 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
637 break;
638
639 default:
640 ret = -EINVAL;
641 }
Russell Kingd6a89fe2009-12-18 17:48:50 +0000642
643 spin_unlock_irqrestore(&aacirun->lock, flags);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200644
645 return ret;
646}
647
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100648static struct snd_pcm_ops aaci_playback_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100649 .open = aaci_pcm_open,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200650 .close = aaci_pcm_close,
651 .ioctl = snd_pcm_lib_ioctl,
652 .hw_params = aaci_pcm_playback_hw_params,
653 .hw_free = aaci_pcm_hw_free,
654 .prepare = aaci_pcm_prepare,
655 .trigger = aaci_pcm_playback_trigger,
656 .pointer = aaci_pcm_pointer,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200657};
658
Russell King8a371842007-02-20 15:44:23 +0000659static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
660 struct snd_pcm_hw_params *params)
Kevin Hilman41762b82007-02-07 05:45:32 +0100661{
662 struct aaci *aaci = substream->private_data;
663 struct aaci_runtime *aacirun = substream->runtime->private_data;
664 int ret;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200665
Kevin Hilman41762b82007-02-07 05:45:32 +0100666 ret = aaci_pcm_hw_params(substream, aacirun, params);
Russell Kingd3aee792009-12-18 17:48:40 +0000667 if (ret >= 0)
Kevin Hilman41762b82007-02-07 05:45:32 +0100668 /* Line in record: slot 3 and 4 */
669 aacirun->cr |= CR_SL3 | CR_SL4;
670
Kevin Hilman41762b82007-02-07 05:45:32 +0100671 return ret;
672}
673
674static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
675{
676 u32 ie;
677
Russell Kingd6a89fe2009-12-18 17:48:50 +0000678 aaci_chan_wait_ready(aacirun, SR_RXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100679
680 ie = readl(aacirun->base + AACI_IE);
681 ie &= ~(IE_ORIE | IE_RXIE);
682 writel(ie, aacirun->base+AACI_IE);
683
684 aacirun->cr &= ~CR_EN;
685
686 writel(aacirun->cr, aacirun->base + AACI_RXCR);
687}
688
689static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
690{
691 u32 ie;
692
Russell Kingd6a89fe2009-12-18 17:48:50 +0000693 aaci_chan_wait_ready(aacirun, SR_RXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100694
695#ifdef DEBUG
696 /* RX Timeout value: bits 28:17 in RXCR */
697 aacirun->cr |= 0xf << 17;
698#endif
699
700 aacirun->cr |= CR_EN;
701 writel(aacirun->cr, aacirun->base + AACI_RXCR);
702
703 ie = readl(aacirun->base + AACI_IE);
704 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
705 writel(ie, aacirun->base + AACI_IE);
706}
707
Russell King8a371842007-02-20 15:44:23 +0000708static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
709{
Kevin Hilman41762b82007-02-07 05:45:32 +0100710 struct aaci_runtime *aacirun = substream->runtime->private_data;
711 unsigned long flags;
712 int ret = 0;
713
Russell Kingd6a89fe2009-12-18 17:48:50 +0000714 spin_lock_irqsave(&aacirun->lock, flags);
Kevin Hilman41762b82007-02-07 05:45:32 +0100715
716 switch (cmd) {
717 case SNDRV_PCM_TRIGGER_START:
718 aaci_pcm_capture_start(aacirun);
719 break;
720
721 case SNDRV_PCM_TRIGGER_RESUME:
722 aaci_pcm_capture_start(aacirun);
723 break;
724
725 case SNDRV_PCM_TRIGGER_STOP:
726 aaci_pcm_capture_stop(aacirun);
727 break;
728
729 case SNDRV_PCM_TRIGGER_SUSPEND:
730 aaci_pcm_capture_stop(aacirun);
731 break;
732
733 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
734 break;
735
736 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
737 break;
738
739 default:
740 ret = -EINVAL;
741 }
742
Russell Kingd6a89fe2009-12-18 17:48:50 +0000743 spin_unlock_irqrestore(&aacirun->lock, flags);
Kevin Hilman41762b82007-02-07 05:45:32 +0100744
745 return ret;
746}
747
Russell King8a371842007-02-20 15:44:23 +0000748static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
Kevin Hilman41762b82007-02-07 05:45:32 +0100749{
750 struct snd_pcm_runtime *runtime = substream->runtime;
751 struct aaci *aaci = substream->private_data;
752
753 aaci_pcm_prepare(substream);
754
755 /* allow changing of sample rate */
756 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
757 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
758 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
759
760 /* Record select: Mic: 0, Aux: 3, Line: 4 */
761 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
762
763 return 0;
764}
765
Russell King8a371842007-02-20 15:44:23 +0000766static struct snd_pcm_ops aaci_capture_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100767 .open = aaci_pcm_open,
768 .close = aaci_pcm_close,
769 .ioctl = snd_pcm_lib_ioctl,
770 .hw_params = aaci_pcm_capture_hw_params,
771 .hw_free = aaci_pcm_hw_free,
772 .prepare = aaci_pcm_capture_prepare,
773 .trigger = aaci_pcm_capture_trigger,
774 .pointer = aaci_pcm_pointer,
Kevin Hilman41762b82007-02-07 05:45:32 +0100775};
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200776
777/*
778 * Power Management.
779 */
780#ifdef CONFIG_PM
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100781static int aaci_do_suspend(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200782{
783 struct aaci *aaci = card->private_data;
Takashi Iwai792a6c52005-11-17 17:19:25 +0100784 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
785 snd_pcm_suspend_all(aaci->pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200786 return 0;
787}
788
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100789static int aaci_do_resume(struct snd_card *card, unsigned int state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200790{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100791 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200792 return 0;
793}
794
Richard Purdiee36d3942005-09-16 19:27:53 -0700795static int aaci_suspend(struct amba_device *dev, pm_message_t state)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200796{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100797 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200798 return card ? aaci_do_suspend(card) : 0;
799}
800
801static int aaci_resume(struct amba_device *dev)
802{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100803 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200804 return card ? aaci_do_resume(card) : 0;
805}
806#else
807#define aaci_do_suspend NULL
808#define aaci_do_resume NULL
809#define aaci_suspend NULL
810#define aaci_resume NULL
811#endif
812
813
814static struct ac97_pcm ac97_defs[] __devinitdata = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100815 [0] = { /* Front PCM */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200816 .exclusive = 1,
817 .r = {
818 [0] = {
819 .slots = (1 << AC97_SLOT_PCM_LEFT) |
820 (1 << AC97_SLOT_PCM_RIGHT) |
821 (1 << AC97_SLOT_PCM_CENTER) |
822 (1 << AC97_SLOT_PCM_SLEFT) |
823 (1 << AC97_SLOT_PCM_SRIGHT) |
824 (1 << AC97_SLOT_LFE),
825 },
Russell Kinga08d5652009-12-18 17:48:45 +0000826 [1] = {
827 .slots = (1 << AC97_SLOT_PCM_LEFT) |
828 (1 << AC97_SLOT_PCM_RIGHT) |
829 (1 << AC97_SLOT_PCM_LEFT_0) |
830 (1 << AC97_SLOT_PCM_RIGHT_0),
831 },
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200832 },
833 },
834 [1] = { /* PCM in */
835 .stream = 1,
836 .exclusive = 1,
837 .r = {
838 [0] = {
839 .slots = (1 << AC97_SLOT_PCM_LEFT) |
840 (1 << AC97_SLOT_PCM_RIGHT),
841 },
842 },
843 },
844 [2] = { /* Mic in */
845 .stream = 1,
846 .exclusive = 1,
847 .r = {
848 [0] = {
849 .slots = (1 << AC97_SLOT_MIC),
850 },
851 },
852 }
853};
854
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100855static struct snd_ac97_bus_ops aaci_bus_ops = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200856 .write = aaci_ac97_write,
857 .read = aaci_ac97_read,
858};
859
860static int __devinit aaci_probe_ac97(struct aaci *aaci)
861{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100862 struct snd_ac97_template ac97_template;
863 struct snd_ac97_bus *ac97_bus;
864 struct snd_ac97 *ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200865 int ret;
866
Philby John29a4f2d2009-10-13 16:30:22 +0530867 writel(0, aaci->base + AC97_POWERDOWN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200868 /*
869 * Assert AACIRESET for 2us
870 */
871 writel(0, aaci->base + AACI_RESET);
872 udelay(2);
873 writel(RESET_NRST, aaci->base + AACI_RESET);
874
875 /*
876 * Give the AC'97 codec more than enough time
877 * to wake up. (42us = ~2 frames at 48kHz.)
878 */
879 udelay(42);
880
881 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
882 if (ret)
883 goto out;
884
885 ac97_bus->clock = 48000;
886 aaci->ac97_bus = ac97_bus;
887
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100888 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200889 ac97_template.private_data = aaci;
890 ac97_template.num = 0;
891 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
892
893 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
894 if (ret)
895 goto out;
Kevin Hilman41762b82007-02-07 05:45:32 +0100896 aaci->ac97 = ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200897
898 /*
899 * Disable AC97 PC Beep input on audio codecs.
900 */
901 if (ac97_is_audio(ac97))
902 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
903
904 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
905 if (ret)
906 goto out;
907
908 aaci->playback.pcm = &ac97_bus->pcms[0];
Kevin Hilman41762b82007-02-07 05:45:32 +0100909 aaci->capture.pcm = &ac97_bus->pcms[1];
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200910
911 out:
912 return ret;
913}
914
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100915static void aaci_free_card(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200916{
917 struct aaci *aaci = card->private_data;
918 if (aaci->base)
919 iounmap(aaci->base);
920}
921
922static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
923{
924 struct aaci *aaci;
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100925 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100926 int err;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200927
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100928 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
929 THIS_MODULE, sizeof(struct aaci), &card);
930 if (err < 0)
Takashi Iwai631e8ad2008-09-01 15:31:50 +0200931 return NULL;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200932
933 card->private_free = aaci_free_card;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200934
935 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
936 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
937 snprintf(card->longname, sizeof(card->longname),
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -0700938 "%s at 0x%016llx, irq %d",
939 card->shortname, (unsigned long long)dev->res.start,
940 dev->irq[0]);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200941
942 aaci = card->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100943 mutex_init(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200944 aaci->card = card;
945 aaci->dev = dev;
946
947 /* Set MAINCR to allow slot 1 and 2 data IO */
948 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
949 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
950
951 return aaci;
952}
953
954static int __devinit aaci_init_pcm(struct aaci *aaci)
955{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100956 struct snd_pcm *pcm;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200957 int ret;
958
Kevin Hilman41762b82007-02-07 05:45:32 +0100959 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200960 if (ret == 0) {
961 aaci->pcm = pcm;
962 pcm->private_data = aaci;
963 pcm->info_flags = 0;
964
965 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
966
967 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
Kevin Hilman41762b82007-02-07 05:45:32 +0100968 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
Takashi Iwaid6797322009-11-26 15:08:54 +0100969 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaid4946432009-12-18 20:25:30 +0100970 NULL, 0, 64 * 1024);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200971 }
972
973 return ret;
974}
975
976static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
977{
Kevin Hilman41762b82007-02-07 05:45:32 +0100978 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200979 int i;
980
Kevin Hilman41762b82007-02-07 05:45:32 +0100981 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200982
Kevin Hilman41762b82007-02-07 05:45:32 +0100983 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
984 writel(0, aacirun->fifo);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200985
Kevin Hilman41762b82007-02-07 05:45:32 +0100986 writel(0, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200987
988 /*
989 * Re-initialise the AACI after the FIFO depth test, to
990 * ensure that the FIFOs are empty. Unfortunately, merely
991 * disabling the channel doesn't clear the FIFO.
992 */
993 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
994 writel(aaci->maincr, aaci->base + AACI_MAINCR);
995
996 /*
997 * If we hit 4096, we failed. Go back to the specified
998 * fifo depth.
999 */
1000 if (i == 4096)
1001 i = 8;
1002
1003 return i;
1004}
1005
Alessandro Rubini03fbdb12009-05-20 22:39:08 +01001006static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001007{
1008 struct aaci *aaci;
1009 int ret, i;
1010
1011 ret = amba_request_regions(dev, NULL);
1012 if (ret)
1013 return ret;
1014
1015 aaci = aaci_init_card(dev);
Takashi Iwai631e8ad2008-09-01 15:31:50 +02001016 if (!aaci) {
1017 ret = -ENOMEM;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001018 goto out;
1019 }
1020
Linus Walleijdc890c22009-06-07 23:27:31 +01001021 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001022 if (!aaci->base) {
1023 ret = -ENOMEM;
1024 goto out;
1025 }
1026
1027 /*
1028 * Playback uses AACI channel 0
1029 */
Russell Kingd6a89fe2009-12-18 17:48:50 +00001030 spin_lock_init(&aaci->playback.lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001031 aaci->playback.base = aaci->base + AACI_CSCH1;
1032 aaci->playback.fifo = aaci->base + AACI_DR1;
1033
Kevin Hilman41762b82007-02-07 05:45:32 +01001034 /*
1035 * Capture uses AACI channel 0
1036 */
Russell Kingd6a89fe2009-12-18 17:48:50 +00001037 spin_lock_init(&aaci->capture.lock);
Kevin Hilman41762b82007-02-07 05:45:32 +01001038 aaci->capture.base = aaci->base + AACI_CSCH1;
1039 aaci->capture.fifo = aaci->base + AACI_DR1;
1040
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001041 for (i = 0; i < 4; i++) {
viro@ZenIV.linux.org.uke12ba642005-09-06 02:06:57 +01001042 void __iomem *base = aaci->base + i * 0x14;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001043
1044 writel(0, base + AACI_IE);
1045 writel(0, base + AACI_TXCR);
1046 writel(0, base + AACI_RXCR);
1047 }
1048
1049 writel(0x1fff, aaci->base + AACI_INTCLR);
1050 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1051
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001052 ret = aaci_probe_ac97(aaci);
1053 if (ret)
1054 goto out;
1055
Catalin Marinasf27f2182006-02-01 19:25:58 +00001056 /*
1057 * Size the FIFOs (must be multiple of 16).
1058 */
1059 aaci->fifosize = aaci_size_fifo(aaci);
1060 if (aaci->fifosize & 15) {
1061 printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
1062 aaci->fifosize);
1063 ret = -ENODEV;
1064 goto out;
1065 }
1066
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001067 ret = aaci_init_pcm(aaci);
1068 if (ret)
1069 goto out;
1070
Takashi Iwaia76af192005-09-05 16:56:40 +02001071 snd_card_set_dev(aaci->card, &dev->dev);
1072
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001073 ret = snd_card_register(aaci->card);
1074 if (ret == 0) {
1075 dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
Kevin Hilman41762b82007-02-07 05:45:32 +01001076 aaci->fifosize);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001077 amba_set_drvdata(dev, aaci->card);
1078 return ret;
1079 }
1080
1081 out:
1082 if (aaci)
1083 snd_card_free(aaci->card);
1084 amba_release_regions(dev);
1085 return ret;
1086}
1087
1088static int __devexit aaci_remove(struct amba_device *dev)
1089{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001090 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001091
1092 amba_set_drvdata(dev, NULL);
1093
1094 if (card) {
1095 struct aaci *aaci = card->private_data;
1096 writel(0, aaci->base + AACI_MAINCR);
1097
1098 snd_card_free(card);
1099 amba_release_regions(dev);
1100 }
1101
1102 return 0;
1103}
1104
1105static struct amba_id aaci_ids[] = {
1106 {
1107 .id = 0x00041041,
1108 .mask = 0x000fffff,
1109 },
1110 { 0, 0 },
1111};
1112
1113static struct amba_driver aaci_driver = {
1114 .drv = {
1115 .name = DRIVER_NAME,
1116 },
1117 .probe = aaci_probe,
1118 .remove = __devexit_p(aaci_remove),
1119 .suspend = aaci_suspend,
1120 .resume = aaci_resume,
1121 .id_table = aaci_ids,
1122};
1123
1124static int __init aaci_init(void)
1125{
1126 return amba_driver_register(&aaci_driver);
1127}
1128
1129static void __exit aaci_exit(void)
1130{
1131 amba_driver_unregister(&aaci_driver);
1132}
1133
1134module_init(aaci_init);
1135module_exit(aaci_exit);
1136
1137MODULE_LICENSE("GPL");
1138MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");