ALSA: AACI: cleanup aaci_pcm_hw_params
[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
33/*
34 * PM support is not complete. Turn it off.
35 */
36#undef CONFIG_PM
37
ceb9e476 38static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
cb5a6ffc
RK
39{
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 */
14d178a1
KH
63static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
64 unsigned short val)
cb5a6ffc
RK
65{
66 struct aaci *aaci = ac97->private_data;
67 u32 v;
14d178a1 68 int timeout = 5000;
cb5a6ffc
RK
69
70 if (ac97->num >= 4)
71 return;
72
12aa7579 73 mutex_lock(&aaci->ac97_sem);
cb5a6ffc
RK
74
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);
f6f35bbe 89 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
14d178a1
KH
90
91 if (!timeout)
92 dev_err(&aaci->dev->dev,
93 "timeout waiting for write to complete\n");
cb5a6ffc 94
12aa7579 95 mutex_unlock(&aaci->ac97_sem);
cb5a6ffc
RK
96}
97
98/*
99 * Read an AC'97 register.
100 */
ceb9e476 101static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
cb5a6ffc
RK
102{
103 struct aaci *aaci = ac97->private_data;
104 u32 v;
14d178a1
KH
105 int timeout = 5000;
106 int retries = 10;
cb5a6ffc
RK
107
108 if (ac97->num >= 4)
109 return ~0;
110
12aa7579 111 mutex_lock(&aaci->ac97_sem);
cb5a6ffc
RK
112
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);
f6f35bbe 125 } while ((v & SLFR_1TXB) && --timeout);
14d178a1
KH
126
127 if (!timeout) {
128 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
129 v = ~0;
130 goto out;
131 }
cb5a6ffc
RK
132
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 */
14d178a1 142 timeout = 5000;
cb5a6ffc
RK
143 do {
144 cond_resched();
145 v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
f6f35bbe 146 } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
cb5a6ffc 147
14d178a1
KH
148 if (!timeout) {
149 dev_err(&aaci->dev->dev, "timeout on RX valid\n");
cb5a6ffc 150 v = ~0;
14d178a1 151 goto out;
cb5a6ffc
RK
152 }
153
14d178a1
KH
154 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:
12aa7579 171 mutex_unlock(&aaci->ac97_sem);
cb5a6ffc
RK
172 return v;
173}
174
175static inline void aaci_chan_wait_ready(struct aaci_runtime *aacirun)
176{
177 u32 val;
178 int timeout = 5000;
179
180 do {
181 val = readl(aacirun->base + AACI_SR);
182 } while (val & (SR_TXB|SR_RXB) && timeout--);
183}
184
185
186
187/*
188 * Interrupt support.
189 */
62578cbf 190static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
cb5a6ffc 191{
41762b8c
KH
192 if (mask & ISR_ORINTR) {
193 dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
194 writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
195 }
196
197 if (mask & ISR_RXTOINTR) {
198 dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
199 writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
200 }
201
202 if (mask & ISR_RXINTR) {
203 struct aaci_runtime *aacirun = &aaci->capture;
204 void *ptr;
205
206 if (!aacirun->substream || !aacirun->start) {
898eb71c 207 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
41762b8c
KH
208 writel(0, aacirun->base + AACI_IE);
209 return;
210 }
211 ptr = aacirun->ptr;
212
213 do {
214 unsigned int len = aacirun->fifosz;
215 u32 val;
216
217 if (aacirun->bytes <= 0) {
218 aacirun->bytes += aacirun->period;
219 aacirun->ptr = ptr;
220 spin_unlock(&aaci->lock);
221 snd_pcm_period_elapsed(aacirun->substream);
222 spin_lock(&aaci->lock);
223 }
224 if (!(aacirun->cr & CR_EN))
225 break;
226
227 val = readl(aacirun->base + AACI_SR);
228 if (!(val & SR_RXHF))
229 break;
230 if (!(val & SR_RXFF))
231 len >>= 1;
232
233 aacirun->bytes -= len;
234
235 /* reading 16 bytes at a time */
236 for( ; len > 0; len -= 16) {
237 asm(
238 "ldmia %1, {r0, r1, r2, r3}\n\t"
239 "stmia %0!, {r0, r1, r2, r3}"
240 : "+r" (ptr)
241 : "r" (aacirun->fifo)
242 : "r0", "r1", "r2", "r3", "cc");
243
244 if (ptr >= aacirun->end)
245 ptr = aacirun->start;
246 }
247 } while(1);
248 aacirun->ptr = ptr;
249 }
250
cb5a6ffc 251 if (mask & ISR_URINTR) {
62578cbf
KH
252 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
253 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
cb5a6ffc
RK
254 }
255
256 if (mask & ISR_TXINTR) {
257 struct aaci_runtime *aacirun = &aaci->playback;
258 void *ptr;
259
260 if (!aacirun->substream || !aacirun->start) {
898eb71c 261 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
cb5a6ffc
RK
262 writel(0, aacirun->base + AACI_IE);
263 return;
264 }
265
266 ptr = aacirun->ptr;
267 do {
268 unsigned int len = aacirun->fifosz;
269 u32 val;
270
271 if (aacirun->bytes <= 0) {
272 aacirun->bytes += aacirun->period;
273 aacirun->ptr = ptr;
274 spin_unlock(&aaci->lock);
275 snd_pcm_period_elapsed(aacirun->substream);
276 spin_lock(&aaci->lock);
277 }
41762b8c 278 if (!(aacirun->cr & CR_EN))
cb5a6ffc
RK
279 break;
280
281 val = readl(aacirun->base + AACI_SR);
282 if (!(val & SR_TXHE))
283 break;
284 if (!(val & SR_TXFE))
285 len >>= 1;
286
287 aacirun->bytes -= len;
288
289 /* writing 16 bytes at a time */
290 for ( ; len > 0; len -= 16) {
291 asm(
292 "ldmia %0!, {r0, r1, r2, r3}\n\t"
293 "stmia %1, {r0, r1, r2, r3}"
294 : "+r" (ptr)
295 : "r" (aacirun->fifo)
296 : "r0", "r1", "r2", "r3", "cc");
297
298 if (ptr >= aacirun->end)
299 ptr = aacirun->start;
300 }
301 } while (1);
302
303 aacirun->ptr = ptr;
304 }
305}
306
7d12e780 307static irqreturn_t aaci_irq(int irq, void *devid)
cb5a6ffc
RK
308{
309 struct aaci *aaci = devid;
310 u32 mask;
311 int i;
312
313 spin_lock(&aaci->lock);
314 mask = readl(aaci->base + AACI_ALLINTS);
315 if (mask) {
316 u32 m = mask;
317 for (i = 0; i < 4; i++, m >>= 7) {
318 if (m & 0x7f) {
62578cbf 319 aaci_fifo_irq(aaci, i, m);
cb5a6ffc
RK
320 }
321 }
322 }
323 spin_unlock(&aaci->lock);
324
325 return mask ? IRQ_HANDLED : IRQ_NONE;
326}
327
328
329
330/*
331 * ALSA support.
332 */
ceb9e476 333static struct snd_pcm_hardware aaci_hw_info = {
cb5a6ffc
RK
334 .info = SNDRV_PCM_INFO_MMAP |
335 SNDRV_PCM_INFO_MMAP_VALID |
336 SNDRV_PCM_INFO_INTERLEAVED |
337 SNDRV_PCM_INFO_BLOCK_TRANSFER |
338 SNDRV_PCM_INFO_RESUME,
339
340 /*
341 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
342 * words. It also doesn't support 12-bit at all.
343 */
344 .formats = SNDRV_PCM_FMTBIT_S16_LE,
345
6ca867c8 346 /* rates are setup from the AC'97 codec */
cb5a6ffc
RK
347 .channels_min = 2,
348 .channels_max = 6,
349 .buffer_bytes_max = 64 * 1024,
350 .period_bytes_min = 256,
351 .period_bytes_max = PAGE_SIZE,
352 .periods_min = 4,
353 .periods_max = PAGE_SIZE / 16,
354};
355
41762b8c
KH
356static int __aaci_pcm_open(struct aaci *aaci,
357 struct snd_pcm_substream *substream,
358 struct aaci_runtime *aacirun)
cb5a6ffc 359{
ceb9e476 360 struct snd_pcm_runtime *runtime = substream->runtime;
cb5a6ffc
RK
361 int ret;
362
363 aacirun->substream = substream;
364 runtime->private_data = aacirun;
365 runtime->hw = aaci_hw_info;
6ca867c8
RK
366 runtime->hw.rates = aacirun->pcm->rates;
367 snd_pcm_limit_hw_rates(runtime);
cb5a6ffc
RK
368
369 /*
370 * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
371 * mode, each 32-bit word contains one sample. If we're in
372 * compact mode, each 32-bit word contains two samples, effectively
373 * halving the FIFO size. However, we don't know for sure which
374 * we'll be using at this point. We set this to the lower limit.
375 */
376 runtime->hw.fifo_size = aaci->fifosize * 2;
377
65ca68b3 378 ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
cb5a6ffc
RK
379 DRIVER_NAME, aaci);
380 if (ret)
381 goto out;
382
383 return 0;
384
385 out:
386 return ret;
387}
388
389
390/*
391 * Common ALSA stuff
392 */
ceb9e476 393static int aaci_pcm_close(struct snd_pcm_substream *substream)
cb5a6ffc
RK
394{
395 struct aaci *aaci = substream->private_data;
396 struct aaci_runtime *aacirun = substream->runtime->private_data;
397
41762b8c 398 WARN_ON(aacirun->cr & CR_EN);
cb5a6ffc
RK
399
400 aacirun->substream = NULL;
401 free_irq(aaci->dev->irq[0], aaci);
402
403 return 0;
404}
405
ceb9e476 406static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
cb5a6ffc
RK
407{
408 struct aaci_runtime *aacirun = substream->runtime->private_data;
409
410 /*
411 * This must not be called with the device enabled.
412 */
41762b8c 413 WARN_ON(aacirun->cr & CR_EN);
cb5a6ffc
RK
414
415 if (aacirun->pcm_open)
416 snd_ac97_pcm_close(aacirun->pcm);
417 aacirun->pcm_open = 0;
418
419 /*
420 * Clear out the DMA and any allocated buffers.
421 */
d6797322 422 snd_pcm_lib_free_pages(substream);
cb5a6ffc
RK
423
424 return 0;
425}
426
ceb9e476 427static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
cb5a6ffc 428 struct aaci_runtime *aacirun,
ceb9e476 429 struct snd_pcm_hw_params *params)
cb5a6ffc
RK
430{
431 int err;
432
433 aaci_pcm_hw_free(substream);
4acd57c3
RK
434 if (aacirun->pcm_open) {
435 snd_ac97_pcm_close(aacirun->pcm);
436 aacirun->pcm_open = 0;
437 }
cb5a6ffc 438
d6797322
TI
439 err = snd_pcm_lib_malloc_pages(substream,
440 params_buffer_bytes(params));
4e30b691
RK
441 if (err >= 0) {
442 err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
443 params_channels(params),
444 aacirun->pcm->r[0].slots);
cb5a6ffc 445
4e30b691
RK
446 aacirun->pcm_open = err == 0;
447 }
cb5a6ffc 448
cb5a6ffc
RK
449 return err;
450}
451
ceb9e476 452static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
cb5a6ffc 453{
ceb9e476 454 struct snd_pcm_runtime *runtime = substream->runtime;
cb5a6ffc
RK
455 struct aaci_runtime *aacirun = runtime->private_data;
456
4e30b691 457 aacirun->start = runtime->dma_area;
88cdca9c 458 aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
cb5a6ffc
RK
459 aacirun->ptr = aacirun->start;
460 aacirun->period =
461 aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
462
463 return 0;
464}
465
ceb9e476 466static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
cb5a6ffc 467{
ceb9e476 468 struct snd_pcm_runtime *runtime = substream->runtime;
cb5a6ffc
RK
469 struct aaci_runtime *aacirun = runtime->private_data;
470 ssize_t bytes = aacirun->ptr - aacirun->start;
471
472 return bytes_to_frames(runtime, bytes);
473}
474
cb5a6ffc
RK
475
476/*
477 * Playback specific ALSA stuff
478 */
479static const u32 channels_to_txmask[] = {
41762b8c
KH
480 [2] = CR_SL3 | CR_SL4,
481 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
482 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
cb5a6ffc
RK
483};
484
485/*
486 * We can support two and four channel audio. Unfortunately
487 * six channel audio requires a non-standard channel ordering:
488 * 2 -> FL(3), FR(4)
489 * 4 -> FL(3), FR(4), SL(7), SR(8)
490 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
491 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
492 * This requires an ALSA configuration file to correct.
493 */
494static unsigned int channel_list[] = { 2, 4, 6 };
495
496static int
ceb9e476 497aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
cb5a6ffc
RK
498{
499 struct aaci *aaci = rule->private;
500 unsigned int chan_mask = 1 << 0, slots;
501
502 /*
503 * pcms[0] is the our 5.1 PCM instance.
504 */
505 slots = aaci->ac97_bus->pcms[0].r[0].slots;
506 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
507 chan_mask |= 1 << 1;
508 if (slots & (1 << AC97_SLOT_LFE))
509 chan_mask |= 1 << 2;
510 }
511
512 return snd_interval_list(hw_param_interval(p, rule->var),
513 ARRAY_SIZE(channel_list), channel_list,
514 chan_mask);
515}
516
41762b8c 517static int aaci_pcm_open(struct snd_pcm_substream *substream)
cb5a6ffc
RK
518{
519 struct aaci *aaci = substream->private_data;
520 int ret;
521
522 /*
523 * Add rule describing channel dependency.
524 */
525 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
526 SNDRV_PCM_HW_PARAM_CHANNELS,
527 aaci_rule_channels, aaci,
528 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
529 if (ret)
530 return ret;
531
41762b8c
KH
532 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
533 ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
534 } else {
535 ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
536 }
537 return ret;
cb5a6ffc
RK
538}
539
ceb9e476
TI
540static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
541 struct snd_pcm_hw_params *params)
cb5a6ffc
RK
542{
543 struct aaci *aaci = substream->private_data;
544 struct aaci_runtime *aacirun = substream->runtime->private_data;
545 unsigned int channels = params_channels(params);
546 int ret;
547
548 WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
549 !channels_to_txmask[channels]);
550
551 ret = aaci_pcm_hw_params(substream, aacirun, params);
552
553 /*
554 * Enable FIFO, compact mode, 16 bits per sample.
555 * FIXME: double rate slots?
556 */
557 if (ret >= 0) {
41762b8c 558 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
cb5a6ffc
RK
559 aacirun->cr |= channels_to_txmask[channels];
560
561 aacirun->fifosz = aaci->fifosize * 4;
41762b8c 562 if (aacirun->cr & CR_COMPACT)
cb5a6ffc
RK
563 aacirun->fifosz >>= 1;
564 }
565 return ret;
566}
567
568static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
569{
570 u32 ie;
571
572 ie = readl(aacirun->base + AACI_IE);
573 ie &= ~(IE_URIE|IE_TXIE);
574 writel(ie, aacirun->base + AACI_IE);
41762b8c 575 aacirun->cr &= ~CR_EN;
cb5a6ffc
RK
576 aaci_chan_wait_ready(aacirun);
577 writel(aacirun->cr, aacirun->base + AACI_TXCR);
578}
579
580static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
581{
582 u32 ie;
583
584 aaci_chan_wait_ready(aacirun);
41762b8c 585 aacirun->cr |= CR_EN;
cb5a6ffc
RK
586
587 ie = readl(aacirun->base + AACI_IE);
588 ie |= IE_URIE | IE_TXIE;
589 writel(ie, aacirun->base + AACI_IE);
590 writel(aacirun->cr, aacirun->base + AACI_TXCR);
591}
592
ceb9e476 593static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
cb5a6ffc
RK
594{
595 struct aaci *aaci = substream->private_data;
596 struct aaci_runtime *aacirun = substream->runtime->private_data;
597 unsigned long flags;
598 int ret = 0;
599
600 spin_lock_irqsave(&aaci->lock, flags);
601 switch (cmd) {
602 case SNDRV_PCM_TRIGGER_START:
603 aaci_pcm_playback_start(aacirun);
604 break;
605
606 case SNDRV_PCM_TRIGGER_RESUME:
607 aaci_pcm_playback_start(aacirun);
608 break;
609
610 case SNDRV_PCM_TRIGGER_STOP:
611 aaci_pcm_playback_stop(aacirun);
612 break;
613
614 case SNDRV_PCM_TRIGGER_SUSPEND:
615 aaci_pcm_playback_stop(aacirun);
616 break;
617
618 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
619 break;
620
621 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
622 break;
623
624 default:
625 ret = -EINVAL;
626 }
627 spin_unlock_irqrestore(&aaci->lock, flags);
628
629 return ret;
630}
631
ceb9e476 632static struct snd_pcm_ops aaci_playback_ops = {
41762b8c 633 .open = aaci_pcm_open,
cb5a6ffc
RK
634 .close = aaci_pcm_close,
635 .ioctl = snd_pcm_lib_ioctl,
636 .hw_params = aaci_pcm_playback_hw_params,
637 .hw_free = aaci_pcm_hw_free,
638 .prepare = aaci_pcm_prepare,
639 .trigger = aaci_pcm_playback_trigger,
640 .pointer = aaci_pcm_pointer,
cb5a6ffc
RK
641};
642
8a371840
RK
643static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
644 struct snd_pcm_hw_params *params)
41762b8c
KH
645{
646 struct aaci *aaci = substream->private_data;
647 struct aaci_runtime *aacirun = substream->runtime->private_data;
648 int ret;
649
650 ret = aaci_pcm_hw_params(substream, aacirun, params);
651
652 if (ret >= 0) {
653 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
654
655 /* Line in record: slot 3 and 4 */
656 aacirun->cr |= CR_SL3 | CR_SL4;
657
658 aacirun->fifosz = aaci->fifosize * 4;
659
660 if (aacirun->cr & CR_COMPACT)
661 aacirun->fifosz >>= 1;
662 }
663 return ret;
664}
665
666static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
667{
668 u32 ie;
669
670 aaci_chan_wait_ready(aacirun);
671
672 ie = readl(aacirun->base + AACI_IE);
673 ie &= ~(IE_ORIE | IE_RXIE);
674 writel(ie, aacirun->base+AACI_IE);
675
676 aacirun->cr &= ~CR_EN;
cb5a6ffc 677
41762b8c
KH
678 writel(aacirun->cr, aacirun->base + AACI_RXCR);
679}
680
681static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
682{
683 u32 ie;
684
685 aaci_chan_wait_ready(aacirun);
686
687#ifdef DEBUG
688 /* RX Timeout value: bits 28:17 in RXCR */
689 aacirun->cr |= 0xf << 17;
690#endif
691
692 aacirun->cr |= CR_EN;
693 writel(aacirun->cr, aacirun->base + AACI_RXCR);
694
695 ie = readl(aacirun->base + AACI_IE);
696 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
697 writel(ie, aacirun->base + AACI_IE);
698}
699
8a371840
RK
700static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
701{
41762b8c
KH
702 struct aaci *aaci = substream->private_data;
703 struct aaci_runtime *aacirun = substream->runtime->private_data;
704 unsigned long flags;
705 int ret = 0;
706
707 spin_lock_irqsave(&aaci->lock, flags);
708
709 switch (cmd) {
710 case SNDRV_PCM_TRIGGER_START:
711 aaci_pcm_capture_start(aacirun);
712 break;
713
714 case SNDRV_PCM_TRIGGER_RESUME:
715 aaci_pcm_capture_start(aacirun);
716 break;
717
718 case SNDRV_PCM_TRIGGER_STOP:
719 aaci_pcm_capture_stop(aacirun);
720 break;
721
722 case SNDRV_PCM_TRIGGER_SUSPEND:
723 aaci_pcm_capture_stop(aacirun);
724 break;
725
726 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
727 break;
728
729 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
730 break;
731
732 default:
733 ret = -EINVAL;
734 }
735
736 spin_unlock_irqrestore(&aaci->lock, flags);
737
738 return ret;
739}
740
8a371840 741static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
41762b8c
KH
742{
743 struct snd_pcm_runtime *runtime = substream->runtime;
744 struct aaci *aaci = substream->private_data;
745
746 aaci_pcm_prepare(substream);
747
748 /* allow changing of sample rate */
749 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
750 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
751 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
752
753 /* Record select: Mic: 0, Aux: 3, Line: 4 */
754 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
755
756 return 0;
757}
758
8a371840 759static struct snd_pcm_ops aaci_capture_ops = {
41762b8c
KH
760 .open = aaci_pcm_open,
761 .close = aaci_pcm_close,
762 .ioctl = snd_pcm_lib_ioctl,
763 .hw_params = aaci_pcm_capture_hw_params,
764 .hw_free = aaci_pcm_hw_free,
765 .prepare = aaci_pcm_capture_prepare,
766 .trigger = aaci_pcm_capture_trigger,
767 .pointer = aaci_pcm_pointer,
41762b8c 768};
cb5a6ffc
RK
769
770/*
771 * Power Management.
772 */
773#ifdef CONFIG_PM
ceb9e476 774static int aaci_do_suspend(struct snd_card *card, unsigned int state)
cb5a6ffc
RK
775{
776 struct aaci *aaci = card->private_data;
792a6c51
TI
777 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
778 snd_pcm_suspend_all(aaci->pcm);
cb5a6ffc
RK
779 return 0;
780}
781
ceb9e476 782static int aaci_do_resume(struct snd_card *card, unsigned int state)
cb5a6ffc 783{
792a6c51 784 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
cb5a6ffc
RK
785 return 0;
786}
787
e36d394d 788static int aaci_suspend(struct amba_device *dev, pm_message_t state)
cb5a6ffc 789{
ceb9e476 790 struct snd_card *card = amba_get_drvdata(dev);
cb5a6ffc
RK
791 return card ? aaci_do_suspend(card) : 0;
792}
793
794static int aaci_resume(struct amba_device *dev)
795{
ceb9e476 796 struct snd_card *card = amba_get_drvdata(dev);
cb5a6ffc
RK
797 return card ? aaci_do_resume(card) : 0;
798}
799#else
800#define aaci_do_suspend NULL
801#define aaci_do_resume NULL
802#define aaci_suspend NULL
803#define aaci_resume NULL
804#endif
805
806
807static struct ac97_pcm ac97_defs[] __devinitdata = {
41762b8c 808 [0] = { /* Front PCM */
cb5a6ffc
RK
809 .exclusive = 1,
810 .r = {
811 [0] = {
812 .slots = (1 << AC97_SLOT_PCM_LEFT) |
813 (1 << AC97_SLOT_PCM_RIGHT) |
814 (1 << AC97_SLOT_PCM_CENTER) |
815 (1 << AC97_SLOT_PCM_SLEFT) |
816 (1 << AC97_SLOT_PCM_SRIGHT) |
817 (1 << AC97_SLOT_LFE),
818 },
819 },
820 },
821 [1] = { /* PCM in */
822 .stream = 1,
823 .exclusive = 1,
824 .r = {
825 [0] = {
826 .slots = (1 << AC97_SLOT_PCM_LEFT) |
827 (1 << AC97_SLOT_PCM_RIGHT),
828 },
829 },
830 },
831 [2] = { /* Mic in */
832 .stream = 1,
833 .exclusive = 1,
834 .r = {
835 [0] = {
836 .slots = (1 << AC97_SLOT_MIC),
837 },
838 },
839 }
840};
841
ceb9e476 842static struct snd_ac97_bus_ops aaci_bus_ops = {
cb5a6ffc
RK
843 .write = aaci_ac97_write,
844 .read = aaci_ac97_read,
845};
846
847static int __devinit aaci_probe_ac97(struct aaci *aaci)
848{
ceb9e476
TI
849 struct snd_ac97_template ac97_template;
850 struct snd_ac97_bus *ac97_bus;
851 struct snd_ac97 *ac97;
cb5a6ffc
RK
852 int ret;
853
29a4f2d3 854 writel(0, aaci->base + AC97_POWERDOWN);
cb5a6ffc
RK
855 /*
856 * Assert AACIRESET for 2us
857 */
858 writel(0, aaci->base + AACI_RESET);
859 udelay(2);
860 writel(RESET_NRST, aaci->base + AACI_RESET);
861
862 /*
863 * Give the AC'97 codec more than enough time
864 * to wake up. (42us = ~2 frames at 48kHz.)
865 */
866 udelay(42);
867
868 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
869 if (ret)
870 goto out;
871
872 ac97_bus->clock = 48000;
873 aaci->ac97_bus = ac97_bus;
874
ceb9e476 875 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
cb5a6ffc
RK
876 ac97_template.private_data = aaci;
877 ac97_template.num = 0;
878 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
879
880 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
881 if (ret)
882 goto out;
41762b8c 883 aaci->ac97 = ac97;
cb5a6ffc
RK
884
885 /*
886 * Disable AC97 PC Beep input on audio codecs.
887 */
888 if (ac97_is_audio(ac97))
889 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
890
891 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
892 if (ret)
893 goto out;
894
895 aaci->playback.pcm = &ac97_bus->pcms[0];
41762b8c 896 aaci->capture.pcm = &ac97_bus->pcms[1];
cb5a6ffc
RK
897
898 out:
899 return ret;
900}
901
ceb9e476 902static void aaci_free_card(struct snd_card *card)
cb5a6ffc
RK
903{
904 struct aaci *aaci = card->private_data;
905 if (aaci->base)
906 iounmap(aaci->base);
907}
908
909static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
910{
911 struct aaci *aaci;
ceb9e476 912 struct snd_card *card;
bd7dd77c 913 int err;
cb5a6ffc 914
bd7dd77c
TI
915 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
916 THIS_MODULE, sizeof(struct aaci), &card);
917 if (err < 0)
631e8ad4 918 return NULL;
cb5a6ffc
RK
919
920 card->private_free = aaci_free_card;
cb5a6ffc
RK
921
922 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
923 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
924 snprintf(card->longname, sizeof(card->longname),
aa0a2ddc
GKH
925 "%s at 0x%016llx, irq %d",
926 card->shortname, (unsigned long long)dev->res.start,
927 dev->irq[0]);
cb5a6ffc
RK
928
929 aaci = card->private_data;
12aa7579 930 mutex_init(&aaci->ac97_sem);
cb5a6ffc
RK
931 spin_lock_init(&aaci->lock);
932 aaci->card = card;
933 aaci->dev = dev;
934
935 /* Set MAINCR to allow slot 1 and 2 data IO */
936 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
937 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
938
939 return aaci;
940}
941
942static int __devinit aaci_init_pcm(struct aaci *aaci)
943{
ceb9e476 944 struct snd_pcm *pcm;
cb5a6ffc
RK
945 int ret;
946
41762b8c 947 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
cb5a6ffc
RK
948 if (ret == 0) {
949 aaci->pcm = pcm;
950 pcm->private_data = aaci;
951 pcm->info_flags = 0;
952
953 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
954
955 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
41762b8c 956 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
d6797322 957 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
d4946431 958 NULL, 0, 64 * 1024);
cb5a6ffc
RK
959 }
960
961 return ret;
962}
963
964static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
965{
41762b8c 966 struct aaci_runtime *aacirun = &aaci->playback;
cb5a6ffc
RK
967 int i;
968
41762b8c 969 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
cb5a6ffc 970
41762b8c
KH
971 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
972 writel(0, aacirun->fifo);
cb5a6ffc 973
41762b8c 974 writel(0, aacirun->base + AACI_TXCR);
cb5a6ffc
RK
975
976 /*
977 * Re-initialise the AACI after the FIFO depth test, to
978 * ensure that the FIFOs are empty. Unfortunately, merely
979 * disabling the channel doesn't clear the FIFO.
980 */
981 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
982 writel(aaci->maincr, aaci->base + AACI_MAINCR);
983
984 /*
985 * If we hit 4096, we failed. Go back to the specified
986 * fifo depth.
987 */
988 if (i == 4096)
989 i = 8;
990
991 return i;
992}
993
03fbdb15 994static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
cb5a6ffc
RK
995{
996 struct aaci *aaci;
997 int ret, i;
998
999 ret = amba_request_regions(dev, NULL);
1000 if (ret)
1001 return ret;
1002
1003 aaci = aaci_init_card(dev);
631e8ad4
TI
1004 if (!aaci) {
1005 ret = -ENOMEM;
cb5a6ffc
RK
1006 goto out;
1007 }
1008
dc890c2d 1009 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
cb5a6ffc
RK
1010 if (!aaci->base) {
1011 ret = -ENOMEM;
1012 goto out;
1013 }
1014
1015 /*
1016 * Playback uses AACI channel 0
1017 */
1018 aaci->playback.base = aaci->base + AACI_CSCH1;
1019 aaci->playback.fifo = aaci->base + AACI_DR1;
1020
41762b8c
KH
1021 /*
1022 * Capture uses AACI channel 0
1023 */
1024 aaci->capture.base = aaci->base + AACI_CSCH1;
1025 aaci->capture.fifo = aaci->base + AACI_DR1;
1026
cb5a6ffc 1027 for (i = 0; i < 4; i++) {
e12ba644 1028 void __iomem *base = aaci->base + i * 0x14;
cb5a6ffc
RK
1029
1030 writel(0, base + AACI_IE);
1031 writel(0, base + AACI_TXCR);
1032 writel(0, base + AACI_RXCR);
1033 }
1034
1035 writel(0x1fff, aaci->base + AACI_INTCLR);
1036 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1037
f27f218c
CM
1038 ret = aaci_probe_ac97(aaci);
1039 if (ret)
1040 goto out;
1041
cb5a6ffc 1042 /*
f27f218c 1043 * Size the FIFOs (must be multiple of 16).
cb5a6ffc
RK
1044 */
1045 aaci->fifosize = aaci_size_fifo(aaci);
f27f218c
CM
1046 if (aaci->fifosize & 15) {
1047 printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
1048 aaci->fifosize);
1049 ret = -ENODEV;
cb5a6ffc 1050 goto out;
f27f218c 1051 }
cb5a6ffc
RK
1052
1053 ret = aaci_init_pcm(aaci);
1054 if (ret)
1055 goto out;
1056
a76af199
TI
1057 snd_card_set_dev(aaci->card, &dev->dev);
1058
cb5a6ffc
RK
1059 ret = snd_card_register(aaci->card);
1060 if (ret == 0) {
1061 dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
41762b8c 1062 aaci->fifosize);
cb5a6ffc
RK
1063 amba_set_drvdata(dev, aaci->card);
1064 return ret;
1065 }
1066
1067 out:
1068 if (aaci)
1069 snd_card_free(aaci->card);
1070 amba_release_regions(dev);
1071 return ret;
1072}
1073
1074static int __devexit aaci_remove(struct amba_device *dev)
1075{
ceb9e476 1076 struct snd_card *card = amba_get_drvdata(dev);
cb5a6ffc
RK
1077
1078 amba_set_drvdata(dev, NULL);
1079
1080 if (card) {
1081 struct aaci *aaci = card->private_data;
1082 writel(0, aaci->base + AACI_MAINCR);
1083
1084 snd_card_free(card);
1085 amba_release_regions(dev);
1086 }
1087
1088 return 0;
1089}
1090
1091static struct amba_id aaci_ids[] = {
1092 {
1093 .id = 0x00041041,
1094 .mask = 0x000fffff,
1095 },
1096 { 0, 0 },
1097};
1098
1099static struct amba_driver aaci_driver = {
1100 .drv = {
1101 .name = DRIVER_NAME,
1102 },
1103 .probe = aaci_probe,
1104 .remove = __devexit_p(aaci_remove),
1105 .suspend = aaci_suspend,
1106 .resume = aaci_resume,
1107 .id_table = aaci_ids,
1108};
1109
1110static int __init aaci_init(void)
1111{
1112 return amba_driver_register(&aaci_driver);
1113}
1114
1115static void __exit aaci_exit(void)
1116{
1117 amba_driver_unregister(&aaci_driver);
1118}
1119
1120module_init(aaci_init);
1121module_exit(aaci_exit);
1122
1123MODULE_LICENSE("GPL");
1124MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");
This page took 0.448598 seconds and 5 git commands to generate.