ALSA: AACI: fix channel mask selection
[deliverable/linux.git] / sound / arm / aaci.c
CommitLineData
cb5a6ffc
RK
1/*
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>
a62c80e5 20#include <linux/amba/bus.h>
88cdca9c 21#include <linux/io.h>
cb5a6ffc 22
cb5a6ffc
RK
23#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"
cb5a6ffc
RK
30
31#define DRIVER_NAME "aaci-pl041"
32
250c7a61
RK
33#define FRAME_PERIOD_US 21
34
cb5a6ffc
RK
35/*
36 * PM support is not complete. Turn it off.
37 */
38#undef CONFIG_PM
39
ceb9e476 40static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
cb5a6ffc
RK
41{
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 */
14d178a1
KH
65static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
66 unsigned short val)
cb5a6ffc
RK
67{
68 struct aaci *aaci = ac97->private_data;
250c7a61 69 int timeout;
cb5a6ffc
RK
70 u32 v;
71
72 if (ac97->num >= 4)
73 return;
74
12aa7579 75 mutex_lock(&aaci->ac97_sem);
cb5a6ffc
RK
76
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
250c7a61
RK
86 /* 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;
cb5a6ffc 91 do {
250c7a61 92 udelay(1);
cb5a6ffc 93 v = readl(aaci->base + AACI_SLFR);
f6f35bbe 94 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
14d178a1 95
69058cd6 96 if (v & (SLFR_1TXB|SLFR_2TXB))
14d178a1
KH
97 dev_err(&aaci->dev->dev,
98 "timeout waiting for write to complete\n");
cb5a6ffc 99
12aa7579 100 mutex_unlock(&aaci->ac97_sem);
cb5a6ffc
RK
101}
102
103/*
104 * Read an AC'97 register.
105 */
ceb9e476 106static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
cb5a6ffc
RK
107{
108 struct aaci *aaci = ac97->private_data;
250c7a61 109 int timeout, retries = 10;
cb5a6ffc
RK
110 u32 v;
111
112 if (ac97->num >= 4)
113 return ~0;
114
12aa7579 115 mutex_lock(&aaci->ac97_sem);
cb5a6ffc
RK
116
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
250c7a61
RK
124 /* 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;
cb5a6ffc 129 do {
250c7a61 130 udelay(1);
cb5a6ffc 131 v = readl(aaci->base + AACI_SLFR);
f6f35bbe 132 } while ((v & SLFR_1TXB) && --timeout);
14d178a1 133
69058cd6 134 if (v & SLFR_1TXB) {
14d178a1
KH
135 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
136 v = ~0;
137 goto out;
138 }
cb5a6ffc 139
250c7a61
RK
140 /* Now wait for the response frame */
141 udelay(FRAME_PERIOD_US);
cb5a6ffc 142
250c7a61
RK
143 /* And then wait an additional eight frame periods for data */
144 timeout = FRAME_PERIOD_US * 8;
cb5a6ffc 145 do {
250c7a61 146 udelay(1);
cb5a6ffc
RK
147 cond_resched();
148 v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
f6f35bbe 149 } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
cb5a6ffc 150
69058cd6 151 if (v != (SLFR_1RXV|SLFR_2RXV)) {
14d178a1 152 dev_err(&aaci->dev->dev, "timeout on RX valid\n");
cb5a6ffc 153 v = ~0;
14d178a1 154 goto out;
cb5a6ffc
RK
155 }
156
14d178a1
KH
157 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:
12aa7579 174 mutex_unlock(&aaci->ac97_sem);
cb5a6ffc
RK
175 return v;
176}
177
d6a89fef
RK
178static inline void
179aaci_chan_wait_ready(struct aaci_runtime *aacirun, unsigned long mask)
cb5a6ffc
RK
180{
181 u32 val;
182 int timeout = 5000;
183
184 do {
250c7a61 185 udelay(1);
cb5a6ffc 186 val = readl(aacirun->base + AACI_SR);
d6a89fef 187 } while (val & mask && timeout--);
cb5a6ffc
RK
188}
189
190
191
192/*
193 * Interrupt support.
194 */
62578cbf 195static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
cb5a6ffc 196{
41762b8c
KH
197 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) {
898eb71c 212 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
41762b8c
KH
213 writel(0, aacirun->base + AACI_IE);
214 return;
215 }
41762b8c 216
d6a89fef
RK
217 spin_lock(&aacirun->lock);
218
219 ptr = aacirun->ptr;
41762b8c
KH
220 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;
d6a89fef 227 spin_unlock(&aacirun->lock);
41762b8c 228 snd_pcm_period_elapsed(aacirun->substream);
d6a89fef 229 spin_lock(&aacirun->lock);
41762b8c
KH
230 }
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);
d6a89fef 255
41762b8c 256 aacirun->ptr = ptr;
d6a89fef
RK
257
258 spin_unlock(&aacirun->lock);
41762b8c
KH
259 }
260
cb5a6ffc 261 if (mask & ISR_URINTR) {
62578cbf
KH
262 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
263 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
cb5a6ffc
RK
264 }
265
266 if (mask & ISR_TXINTR) {
267 struct aaci_runtime *aacirun = &aaci->playback;
268 void *ptr;
269
270 if (!aacirun->substream || !aacirun->start) {
898eb71c 271 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
cb5a6ffc
RK
272 writel(0, aacirun->base + AACI_IE);
273 return;
274 }
275
d6a89fef
RK
276 spin_lock(&aacirun->lock);
277
cb5a6ffc
RK
278 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;
d6a89fef 286 spin_unlock(&aacirun->lock);
cb5a6ffc 287 snd_pcm_period_elapsed(aacirun->substream);
d6a89fef 288 spin_lock(&aacirun->lock);
cb5a6ffc 289 }
41762b8c 290 if (!(aacirun->cr & CR_EN))
cb5a6ffc
RK
291 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;
d6a89fef
RK
316
317 spin_unlock(&aacirun->lock);
cb5a6ffc
RK
318 }
319}
320
7d12e780 321static irqreturn_t aaci_irq(int irq, void *devid)
cb5a6ffc
RK
322{
323 struct aaci *aaci = devid;
324 u32 mask;
325 int i;
326
cb5a6ffc
RK
327 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) {
62578cbf 332 aaci_fifo_irq(aaci, i, m);
cb5a6ffc
RK
333 }
334 }
335 }
cb5a6ffc
RK
336
337 return mask ? IRQ_HANDLED : IRQ_NONE;
338}
339
340
341
342/*
343 * ALSA support.
344 */
ceb9e476 345static struct snd_pcm_hardware aaci_hw_info = {
cb5a6ffc
RK
346 .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
6ca867c8 358 /* rates are setup from the AC'97 codec */
cb5a6ffc 359 .channels_min = 2,
e831d80b 360 .channels_max = 2,
cb5a6ffc
RK
361 .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
e831d80b
RK
368/*
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)
cb5a6ffc 397{
ceb9e476 398 struct snd_pcm_runtime *runtime = substream->runtime;
e831d80b
RK
399 struct aaci *aaci = substream->private_data;
400 struct aaci_runtime *aacirun;
b60fb519 401 int ret = 0;
cb5a6ffc 402
e831d80b
RK
403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
404 aacirun = &aaci->playback;
405 } else {
406 aacirun = &aaci->capture;
407 }
408
cb5a6ffc
RK
409 aacirun->substream = substream;
410 runtime->private_data = aacirun;
411 runtime->hw = aaci_hw_info;
6ca867c8
RK
412 runtime->hw.rates = aacirun->pcm->rates;
413 snd_pcm_limit_hw_rates(runtime);
cb5a6ffc 414
e831d80b
RK
415 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 }
a08d5658 429
cb5a6ffc
RK
430 /*
431 * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
432 * mode, each 32-bit word contains one sample. If we're in
433 * compact mode, each 32-bit word contains two samples, effectively
434 * halving the FIFO size. However, we don't know for sure which
435 * we'll be using at this point. We set this to the lower limit.
436 */
437 runtime->hw.fifo_size = aaci->fifosize * 2;
438
b60fb519
RK
439 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);
cb5a6ffc 447
cb5a6ffc
RK
448 return ret;
449}
450
451
452/*
453 * Common ALSA stuff
454 */
ceb9e476 455static int aaci_pcm_close(struct snd_pcm_substream *substream)
cb5a6ffc
RK
456{
457 struct aaci *aaci = substream->private_data;
458 struct aaci_runtime *aacirun = substream->runtime->private_data;
459
41762b8c 460 WARN_ON(aacirun->cr & CR_EN);
cb5a6ffc
RK
461
462 aacirun->substream = NULL;
b60fb519
RK
463
464 mutex_lock(&aaci->irq_lock);
465 if (!--aaci->users)
466 free_irq(aaci->dev->irq[0], aaci);
467 mutex_unlock(&aaci->irq_lock);
cb5a6ffc
RK
468
469 return 0;
470}
471
ceb9e476 472static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
cb5a6ffc
RK
473{
474 struct aaci_runtime *aacirun = substream->runtime->private_data;
475
476 /*
477 * This must not be called with the device enabled.
478 */
41762b8c 479 WARN_ON(aacirun->cr & CR_EN);
cb5a6ffc
RK
480
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 */
d6797322 488 snd_pcm_lib_free_pages(substream);
cb5a6ffc
RK
489
490 return 0;
491}
492
58e8a474
RK
493/* 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
ceb9e476 500static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
ceb9e476 501 struct snd_pcm_hw_params *params)
cb5a6ffc 502{
58e8a474
RK
503 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;
cb5a6ffc
RK
507 int err;
508
509 aaci_pcm_hw_free(substream);
4acd57c3
RK
510 if (aacirun->pcm_open) {
511 snd_ac97_pcm_close(aacirun->pcm);
512 aacirun->pcm_open = 0;
513 }
cb5a6ffc 514
58e8a474
RK
515 /* channels is already limited to 2, 4, or 6 by aaci_rule_channels */
516 if (dbl && channels != 2)
517 return -EINVAL;
518
d6797322
TI
519 err = snd_pcm_lib_malloc_pages(substream,
520 params_buffer_bytes(params));
4e30b691 521 if (err >= 0) {
58e8a474 522 struct aaci *aaci = substream->private_data;
a08d5658 523
58e8a474 524 err = snd_ac97_pcm_open(aacirun->pcm, rate, channels,
a08d5658 525 aacirun->pcm->r[dbl].slots);
cb5a6ffc 526
4e30b691 527 aacirun->pcm_open = err == 0;
d3aee799 528 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
58e8a474 529 aacirun->cr |= channels_to_slotmask[channels + dbl * 2];
d3aee799
RK
530 aacirun->fifosz = aaci->fifosize * 4;
531
532 if (aacirun->cr & CR_COMPACT)
533 aacirun->fifosz >>= 1;
4e30b691 534 }
cb5a6ffc 535
cb5a6ffc
RK
536 return err;
537}
538
ceb9e476 539static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
cb5a6ffc 540{
ceb9e476 541 struct snd_pcm_runtime *runtime = substream->runtime;
cb5a6ffc
RK
542 struct aaci_runtime *aacirun = runtime->private_data;
543
4e30b691 544 aacirun->start = runtime->dma_area;
88cdca9c 545 aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
cb5a6ffc
RK
546 aacirun->ptr = aacirun->start;
547 aacirun->period =
548 aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
549
550 return 0;
551}
552
ceb9e476 553static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
cb5a6ffc 554{
ceb9e476 555 struct snd_pcm_runtime *runtime = substream->runtime;
cb5a6ffc
RK
556 struct aaci_runtime *aacirun = runtime->private_data;
557 ssize_t bytes = aacirun->ptr - aacirun->start;
558
559 return bytes_to_frames(runtime, bytes);
560}
561
cb5a6ffc
RK
562
563/*
564 * Playback specific ALSA stuff
565 */
cb5a6ffc
RK
566static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
567{
568 u32 ie;
569
570 ie = readl(aacirun->base + AACI_IE);
571 ie &= ~(IE_URIE|IE_TXIE);
572 writel(ie, aacirun->base + AACI_IE);
41762b8c 573 aacirun->cr &= ~CR_EN;
d6a89fef 574 aaci_chan_wait_ready(aacirun, SR_TXB);
cb5a6ffc
RK
575 writel(aacirun->cr, aacirun->base + AACI_TXCR);
576}
577
578static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
579{
580 u32 ie;
581
d6a89fef 582 aaci_chan_wait_ready(aacirun, SR_TXB);
41762b8c 583 aacirun->cr |= CR_EN;
cb5a6ffc
RK
584
585 ie = readl(aacirun->base + AACI_IE);
586 ie |= IE_URIE | IE_TXIE;
587 writel(ie, aacirun->base + AACI_IE);
588 writel(aacirun->cr, aacirun->base + AACI_TXCR);
589}
590
ceb9e476 591static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
cb5a6ffc 592{
cb5a6ffc
RK
593 struct aaci_runtime *aacirun = substream->runtime->private_data;
594 unsigned long flags;
595 int ret = 0;
596
d6a89fef
RK
597 spin_lock_irqsave(&aacirun->lock, flags);
598
cb5a6ffc
RK
599 switch (cmd) {
600 case SNDRV_PCM_TRIGGER_START:
601 aaci_pcm_playback_start(aacirun);
602 break;
603
604 case SNDRV_PCM_TRIGGER_RESUME:
605 aaci_pcm_playback_start(aacirun);
606 break;
607
608 case SNDRV_PCM_TRIGGER_STOP:
609 aaci_pcm_playback_stop(aacirun);
610 break;
611
612 case SNDRV_PCM_TRIGGER_SUSPEND:
613 aaci_pcm_playback_stop(aacirun);
614 break;
615
616 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
617 break;
618
619 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
620 break;
621
622 default:
623 ret = -EINVAL;
624 }
d6a89fef
RK
625
626 spin_unlock_irqrestore(&aacirun->lock, flags);
cb5a6ffc
RK
627
628 return ret;
629}
630
ceb9e476 631static struct snd_pcm_ops aaci_playback_ops = {
41762b8c 632 .open = aaci_pcm_open,
cb5a6ffc
RK
633 .close = aaci_pcm_close,
634 .ioctl = snd_pcm_lib_ioctl,
58e8a474 635 .hw_params = aaci_pcm_hw_params,
cb5a6ffc
RK
636 .hw_free = aaci_pcm_hw_free,
637 .prepare = aaci_pcm_prepare,
638 .trigger = aaci_pcm_playback_trigger,
639 .pointer = aaci_pcm_pointer,
cb5a6ffc
RK
640};
641
41762b8c
KH
642static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
643{
644 u32 ie;
645
d6a89fef 646 aaci_chan_wait_ready(aacirun, SR_RXB);
41762b8c
KH
647
648 ie = readl(aacirun->base + AACI_IE);
649 ie &= ~(IE_ORIE | IE_RXIE);
650 writel(ie, aacirun->base+AACI_IE);
651
652 aacirun->cr &= ~CR_EN;
cb5a6ffc 653
41762b8c
KH
654 writel(aacirun->cr, aacirun->base + AACI_RXCR);
655}
656
657static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
658{
659 u32 ie;
660
d6a89fef 661 aaci_chan_wait_ready(aacirun, SR_RXB);
41762b8c
KH
662
663#ifdef DEBUG
664 /* RX Timeout value: bits 28:17 in RXCR */
665 aacirun->cr |= 0xf << 17;
666#endif
667
668 aacirun->cr |= CR_EN;
669 writel(aacirun->cr, aacirun->base + AACI_RXCR);
670
671 ie = readl(aacirun->base + AACI_IE);
672 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
673 writel(ie, aacirun->base + AACI_IE);
674}
675
8a371840
RK
676static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
677{
41762b8c
KH
678 struct aaci_runtime *aacirun = substream->runtime->private_data;
679 unsigned long flags;
680 int ret = 0;
681
d6a89fef 682 spin_lock_irqsave(&aacirun->lock, flags);
41762b8c
KH
683
684 switch (cmd) {
685 case SNDRV_PCM_TRIGGER_START:
686 aaci_pcm_capture_start(aacirun);
687 break;
688
689 case SNDRV_PCM_TRIGGER_RESUME:
690 aaci_pcm_capture_start(aacirun);
691 break;
692
693 case SNDRV_PCM_TRIGGER_STOP:
694 aaci_pcm_capture_stop(aacirun);
695 break;
696
697 case SNDRV_PCM_TRIGGER_SUSPEND:
698 aaci_pcm_capture_stop(aacirun);
699 break;
700
701 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
702 break;
703
704 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
705 break;
706
707 default:
708 ret = -EINVAL;
709 }
710
d6a89fef 711 spin_unlock_irqrestore(&aacirun->lock, flags);
41762b8c
KH
712
713 return ret;
714}
715
8a371840 716static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
41762b8c
KH
717{
718 struct snd_pcm_runtime *runtime = substream->runtime;
719 struct aaci *aaci = substream->private_data;
720
721 aaci_pcm_prepare(substream);
722
723 /* allow changing of sample rate */
724 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
725 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
726 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
727
728 /* Record select: Mic: 0, Aux: 3, Line: 4 */
729 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
730
731 return 0;
732}
733
8a371840 734static struct snd_pcm_ops aaci_capture_ops = {
41762b8c
KH
735 .open = aaci_pcm_open,
736 .close = aaci_pcm_close,
737 .ioctl = snd_pcm_lib_ioctl,
58e8a474 738 .hw_params = aaci_pcm_hw_params,
41762b8c
KH
739 .hw_free = aaci_pcm_hw_free,
740 .prepare = aaci_pcm_capture_prepare,
741 .trigger = aaci_pcm_capture_trigger,
742 .pointer = aaci_pcm_pointer,
41762b8c 743};
cb5a6ffc
RK
744
745/*
746 * Power Management.
747 */
748#ifdef CONFIG_PM
ceb9e476 749static int aaci_do_suspend(struct snd_card *card, unsigned int state)
cb5a6ffc
RK
750{
751 struct aaci *aaci = card->private_data;
792a6c51
TI
752 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
753 snd_pcm_suspend_all(aaci->pcm);
cb5a6ffc
RK
754 return 0;
755}
756
ceb9e476 757static int aaci_do_resume(struct snd_card *card, unsigned int state)
cb5a6ffc 758{
792a6c51 759 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
cb5a6ffc
RK
760 return 0;
761}
762
e36d394d 763static int aaci_suspend(struct amba_device *dev, pm_message_t state)
cb5a6ffc 764{
ceb9e476 765 struct snd_card *card = amba_get_drvdata(dev);
cb5a6ffc
RK
766 return card ? aaci_do_suspend(card) : 0;
767}
768
769static int aaci_resume(struct amba_device *dev)
770{
ceb9e476 771 struct snd_card *card = amba_get_drvdata(dev);
cb5a6ffc
RK
772 return card ? aaci_do_resume(card) : 0;
773}
774#else
775#define aaci_do_suspend NULL
776#define aaci_do_resume NULL
777#define aaci_suspend NULL
778#define aaci_resume NULL
779#endif
780
781
782static struct ac97_pcm ac97_defs[] __devinitdata = {
41762b8c 783 [0] = { /* Front PCM */
cb5a6ffc
RK
784 .exclusive = 1,
785 .r = {
786 [0] = {
787 .slots = (1 << AC97_SLOT_PCM_LEFT) |
788 (1 << AC97_SLOT_PCM_RIGHT) |
789 (1 << AC97_SLOT_PCM_CENTER) |
790 (1 << AC97_SLOT_PCM_SLEFT) |
791 (1 << AC97_SLOT_PCM_SRIGHT) |
792 (1 << AC97_SLOT_LFE),
793 },
a08d5658
RK
794 [1] = {
795 .slots = (1 << AC97_SLOT_PCM_LEFT) |
796 (1 << AC97_SLOT_PCM_RIGHT) |
797 (1 << AC97_SLOT_PCM_LEFT_0) |
798 (1 << AC97_SLOT_PCM_RIGHT_0),
799 },
cb5a6ffc
RK
800 },
801 },
802 [1] = { /* PCM in */
803 .stream = 1,
804 .exclusive = 1,
805 .r = {
806 [0] = {
807 .slots = (1 << AC97_SLOT_PCM_LEFT) |
808 (1 << AC97_SLOT_PCM_RIGHT),
809 },
810 },
811 },
812 [2] = { /* Mic in */
813 .stream = 1,
814 .exclusive = 1,
815 .r = {
816 [0] = {
817 .slots = (1 << AC97_SLOT_MIC),
818 },
819 },
820 }
821};
822
ceb9e476 823static struct snd_ac97_bus_ops aaci_bus_ops = {
cb5a6ffc
RK
824 .write = aaci_ac97_write,
825 .read = aaci_ac97_read,
826};
827
828static int __devinit aaci_probe_ac97(struct aaci *aaci)
829{
ceb9e476
TI
830 struct snd_ac97_template ac97_template;
831 struct snd_ac97_bus *ac97_bus;
832 struct snd_ac97 *ac97;
cb5a6ffc
RK
833 int ret;
834
835 /*
836 * Assert AACIRESET for 2us
837 */
838 writel(0, aaci->base + AACI_RESET);
839 udelay(2);
840 writel(RESET_NRST, aaci->base + AACI_RESET);
841
842 /*
843 * Give the AC'97 codec more than enough time
844 * to wake up. (42us = ~2 frames at 48kHz.)
845 */
250c7a61 846 udelay(FRAME_PERIOD_US * 2);
cb5a6ffc
RK
847
848 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
849 if (ret)
850 goto out;
851
852 ac97_bus->clock = 48000;
853 aaci->ac97_bus = ac97_bus;
854
ceb9e476 855 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
cb5a6ffc
RK
856 ac97_template.private_data = aaci;
857 ac97_template.num = 0;
858 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
859
860 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
861 if (ret)
862 goto out;
41762b8c 863 aaci->ac97 = ac97;
cb5a6ffc
RK
864
865 /*
866 * Disable AC97 PC Beep input on audio codecs.
867 */
868 if (ac97_is_audio(ac97))
869 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
870
871 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
872 if (ret)
873 goto out;
874
875 aaci->playback.pcm = &ac97_bus->pcms[0];
41762b8c 876 aaci->capture.pcm = &ac97_bus->pcms[1];
cb5a6ffc
RK
877
878 out:
879 return ret;
880}
881
ceb9e476 882static void aaci_free_card(struct snd_card *card)
cb5a6ffc
RK
883{
884 struct aaci *aaci = card->private_data;
885 if (aaci->base)
886 iounmap(aaci->base);
887}
888
889static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
890{
891 struct aaci *aaci;
ceb9e476 892 struct snd_card *card;
bd7dd77c 893 int err;
cb5a6ffc 894
bd7dd77c
TI
895 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
896 THIS_MODULE, sizeof(struct aaci), &card);
897 if (err < 0)
631e8ad4 898 return NULL;
cb5a6ffc
RK
899
900 card->private_free = aaci_free_card;
cb5a6ffc
RK
901
902 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
903 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
904 snprintf(card->longname, sizeof(card->longname),
aa0a2ddc
GKH
905 "%s at 0x%016llx, irq %d",
906 card->shortname, (unsigned long long)dev->res.start,
907 dev->irq[0]);
cb5a6ffc
RK
908
909 aaci = card->private_data;
12aa7579 910 mutex_init(&aaci->ac97_sem);
b60fb519 911 mutex_init(&aaci->irq_lock);
cb5a6ffc
RK
912 aaci->card = card;
913 aaci->dev = dev;
914
915 /* Set MAINCR to allow slot 1 and 2 data IO */
916 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
917 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
918
919 return aaci;
920}
921
922static int __devinit aaci_init_pcm(struct aaci *aaci)
923{
ceb9e476 924 struct snd_pcm *pcm;
cb5a6ffc
RK
925 int ret;
926
41762b8c 927 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
cb5a6ffc
RK
928 if (ret == 0) {
929 aaci->pcm = pcm;
930 pcm->private_data = aaci;
931 pcm->info_flags = 0;
932
933 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
934
935 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
41762b8c 936 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
d6797322 937 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
d4946431 938 NULL, 0, 64 * 1024);
cb5a6ffc
RK
939 }
940
941 return ret;
942}
943
944static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
945{
41762b8c 946 struct aaci_runtime *aacirun = &aaci->playback;
cb5a6ffc
RK
947 int i;
948
41762b8c 949 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
cb5a6ffc 950
41762b8c
KH
951 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
952 writel(0, aacirun->fifo);
cb5a6ffc 953
41762b8c 954 writel(0, aacirun->base + AACI_TXCR);
cb5a6ffc
RK
955
956 /*
957 * Re-initialise the AACI after the FIFO depth test, to
958 * ensure that the FIFOs are empty. Unfortunately, merely
959 * disabling the channel doesn't clear the FIFO.
960 */
961 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
962 writel(aaci->maincr, aaci->base + AACI_MAINCR);
963
964 /*
965 * If we hit 4096, we failed. Go back to the specified
966 * fifo depth.
967 */
968 if (i == 4096)
969 i = 8;
970
971 return i;
972}
973
03fbdb15 974static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
cb5a6ffc
RK
975{
976 struct aaci *aaci;
977 int ret, i;
978
979 ret = amba_request_regions(dev, NULL);
980 if (ret)
981 return ret;
982
983 aaci = aaci_init_card(dev);
631e8ad4
TI
984 if (!aaci) {
985 ret = -ENOMEM;
cb5a6ffc
RK
986 goto out;
987 }
988
dc890c2d 989 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
cb5a6ffc
RK
990 if (!aaci->base) {
991 ret = -ENOMEM;
992 goto out;
993 }
994
995 /*
996 * Playback uses AACI channel 0
997 */
d6a89fef 998 spin_lock_init(&aaci->playback.lock);
cb5a6ffc
RK
999 aaci->playback.base = aaci->base + AACI_CSCH1;
1000 aaci->playback.fifo = aaci->base + AACI_DR1;
1001
41762b8c
KH
1002 /*
1003 * Capture uses AACI channel 0
1004 */
d6a89fef 1005 spin_lock_init(&aaci->capture.lock);
41762b8c
KH
1006 aaci->capture.base = aaci->base + AACI_CSCH1;
1007 aaci->capture.fifo = aaci->base + AACI_DR1;
1008
cb5a6ffc 1009 for (i = 0; i < 4; i++) {
e12ba644 1010 void __iomem *base = aaci->base + i * 0x14;
cb5a6ffc
RK
1011
1012 writel(0, base + AACI_IE);
1013 writel(0, base + AACI_TXCR);
1014 writel(0, base + AACI_RXCR);
1015 }
1016
1017 writel(0x1fff, aaci->base + AACI_INTCLR);
1018 writel(aaci->maincr, aaci->base + AACI_MAINCR);
b68b58fd
PJ
1019 /*
1020 * Fix: ac97 read back fail errors by reading
1021 * from any arbitrary aaci register.
1022 */
1023 readl(aaci->base + AACI_CSCH1);
f27f218c
CM
1024 ret = aaci_probe_ac97(aaci);
1025 if (ret)
1026 goto out;
1027
cb5a6ffc 1028 /*
f27f218c 1029 * Size the FIFOs (must be multiple of 16).
cb5a6ffc
RK
1030 */
1031 aaci->fifosize = aaci_size_fifo(aaci);
f27f218c
CM
1032 if (aaci->fifosize & 15) {
1033 printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
1034 aaci->fifosize);
1035 ret = -ENODEV;
cb5a6ffc 1036 goto out;
f27f218c 1037 }
cb5a6ffc
RK
1038
1039 ret = aaci_init_pcm(aaci);
1040 if (ret)
1041 goto out;
1042
a76af199
TI
1043 snd_card_set_dev(aaci->card, &dev->dev);
1044
cb5a6ffc
RK
1045 ret = snd_card_register(aaci->card);
1046 if (ret == 0) {
1047 dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
41762b8c 1048 aaci->fifosize);
cb5a6ffc
RK
1049 amba_set_drvdata(dev, aaci->card);
1050 return ret;
1051 }
1052
1053 out:
1054 if (aaci)
1055 snd_card_free(aaci->card);
1056 amba_release_regions(dev);
1057 return ret;
1058}
1059
1060static int __devexit aaci_remove(struct amba_device *dev)
1061{
ceb9e476 1062 struct snd_card *card = amba_get_drvdata(dev);
cb5a6ffc
RK
1063
1064 amba_set_drvdata(dev, NULL);
1065
1066 if (card) {
1067 struct aaci *aaci = card->private_data;
1068 writel(0, aaci->base + AACI_MAINCR);
1069
1070 snd_card_free(card);
1071 amba_release_regions(dev);
1072 }
1073
1074 return 0;
1075}
1076
1077static struct amba_id aaci_ids[] = {
1078 {
1079 .id = 0x00041041,
1080 .mask = 0x000fffff,
1081 },
1082 { 0, 0 },
1083};
1084
1085static struct amba_driver aaci_driver = {
1086 .drv = {
1087 .name = DRIVER_NAME,
1088 },
1089 .probe = aaci_probe,
1090 .remove = __devexit_p(aaci_remove),
1091 .suspend = aaci_suspend,
1092 .resume = aaci_resume,
1093 .id_table = aaci_ids,
1094};
1095
1096static int __init aaci_init(void)
1097{
1098 return amba_driver_register(&aaci_driver);
1099}
1100
1101static void __exit aaci_exit(void)
1102{
1103 amba_driver_unregister(&aaci_driver);
1104}
1105
1106module_init(aaci_init);
1107module_exit(aaci_exit);
1108
1109MODULE_LICENSE("GPL");
1110MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");
This page took 0.591578 seconds and 5 git commands to generate.