2 * linux/sound/arm/aaci.c - ARM PrimeCell AACI PL041 driver
4 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
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.
10 * Documentation: ARM DDI 0173B
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>
20 #include <linux/amba/bus.h>
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>
31 #define DRIVER_NAME "aaci-pl041"
33 #define FRAME_PERIOD_US 21
36 * PM support is not complete. Turn it off.
40 static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
42 u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
45 * Ensure that the slot 1/2 RX registers are empty.
47 v = readl(aaci->base + AACI_SLFR);
49 readl(aaci->base + AACI_SL2RX);
51 readl(aaci->base + AACI_SL1RX);
53 if (maincr != readl(aaci->base + AACI_MAINCR)) {
54 writel(maincr, aaci->base + AACI_MAINCR);
55 readl(aaci->base + AACI_MAINCR);
62 * The recommended use of programming the external codec through slot 1
63 * and slot 2 data is to use the channels during setup routines and the
64 * slot register at any other time. The data written into slot 1, slot 2
65 * and slot 12 registers is transmitted only when their corresponding
66 * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
69 static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
72 struct aaci *aaci = ac97->private_data;
79 mutex_lock(&aaci->ac97_sem);
81 aaci_ac97_select_codec(aaci, ac97);
84 * P54: You must ensure that AACI_SL2TX is always written
85 * to, if required, before data is written to AACI_SL1TX.
87 writel(val << 4, aaci->base + AACI_SL2TX);
88 writel(reg << 12, aaci->base + AACI_SL1TX);
90 /* Initially, wait one frame period */
91 udelay(FRAME_PERIOD_US);
93 /* And then wait an additional eight frame periods for it to be sent */
94 timeout = FRAME_PERIOD_US * 8;
97 v = readl(aaci->base + AACI_SLFR);
98 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
100 if (v & (SLFR_1TXB|SLFR_2TXB))
101 dev_err(&aaci->dev->dev,
102 "timeout waiting for write to complete\n");
104 mutex_unlock(&aaci->ac97_sem);
108 * Read an AC'97 register.
110 static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
112 struct aaci *aaci = ac97->private_data;
113 int timeout, retries = 10;
119 mutex_lock(&aaci->ac97_sem);
121 aaci_ac97_select_codec(aaci, ac97);
124 * Write the register address to slot 1.
126 writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);
128 /* Initially, wait one frame period */
129 udelay(FRAME_PERIOD_US);
131 /* And then wait an additional eight frame periods for it to be sent */
132 timeout = FRAME_PERIOD_US * 8;
135 v = readl(aaci->base + AACI_SLFR);
136 } while ((v & SLFR_1TXB) && --timeout);
139 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
144 /* Now wait for the response frame */
145 udelay(FRAME_PERIOD_US);
147 /* And then wait an additional eight frame periods for data */
148 timeout = FRAME_PERIOD_US * 8;
152 v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
153 } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
155 if (v != (SLFR_1RXV|SLFR_2RXV)) {
156 dev_err(&aaci->dev->dev, "timeout on RX valid\n");
162 v = readl(aaci->base + AACI_SL1RX) >> 12;
164 v = readl(aaci->base + AACI_SL2RX) >> 4;
166 } else if (--retries) {
167 dev_warn(&aaci->dev->dev,
168 "ac97 read back fail. retry\n");
171 dev_warn(&aaci->dev->dev,
172 "wrong ac97 register read back (%x != %x)\n",
178 mutex_unlock(&aaci->ac97_sem);
183 aaci_chan_wait_ready(struct aaci_runtime *aacirun, unsigned long mask)
190 val = readl(aacirun->base + AACI_SR);
191 } while (val & mask && timeout--);
199 static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
201 if (mask & ISR_ORINTR) {
202 dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
203 writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
206 if (mask & ISR_RXTOINTR) {
207 dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
208 writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
211 if (mask & ISR_RXINTR) {
212 struct aaci_runtime *aacirun = &aaci->capture;
215 if (!aacirun->substream || !aacirun->start) {
216 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
217 writel(0, aacirun->base + AACI_IE);
221 spin_lock(&aacirun->lock);
225 unsigned int len = aacirun->fifosz;
228 if (aacirun->bytes <= 0) {
229 aacirun->bytes += aacirun->period;
231 spin_unlock(&aacirun->lock);
232 snd_pcm_period_elapsed(aacirun->substream);
233 spin_lock(&aacirun->lock);
235 if (!(aacirun->cr & CR_EN))
238 val = readl(aacirun->base + AACI_SR);
239 if (!(val & SR_RXHF))
241 if (!(val & SR_RXFF))
244 aacirun->bytes -= len;
246 /* reading 16 bytes at a time */
247 for( ; len > 0; len -= 16) {
249 "ldmia %1, {r0, r1, r2, r3}\n\t"
250 "stmia %0!, {r0, r1, r2, r3}"
252 : "r" (aacirun->fifo)
253 : "r0", "r1", "r2", "r3", "cc");
255 if (ptr >= aacirun->end)
256 ptr = aacirun->start;
262 spin_unlock(&aacirun->lock);
265 if (mask & ISR_URINTR) {
266 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
267 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
270 if (mask & ISR_TXINTR) {
271 struct aaci_runtime *aacirun = &aaci->playback;
274 if (!aacirun->substream || !aacirun->start) {
275 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
276 writel(0, aacirun->base + AACI_IE);
280 spin_lock(&aacirun->lock);
284 unsigned int len = aacirun->fifosz;
287 if (aacirun->bytes <= 0) {
288 aacirun->bytes += aacirun->period;
290 spin_unlock(&aacirun->lock);
291 snd_pcm_period_elapsed(aacirun->substream);
292 spin_lock(&aacirun->lock);
294 if (!(aacirun->cr & CR_EN))
297 val = readl(aacirun->base + AACI_SR);
298 if (!(val & SR_TXHE))
300 if (!(val & SR_TXFE))
303 aacirun->bytes -= len;
305 /* writing 16 bytes at a time */
306 for ( ; len > 0; len -= 16) {
308 "ldmia %0!, {r0, r1, r2, r3}\n\t"
309 "stmia %1, {r0, r1, r2, r3}"
311 : "r" (aacirun->fifo)
312 : "r0", "r1", "r2", "r3", "cc");
314 if (ptr >= aacirun->end)
315 ptr = aacirun->start;
321 spin_unlock(&aacirun->lock);
325 static irqreturn_t aaci_irq(int irq, void *devid)
327 struct aaci *aaci = devid;
331 mask = readl(aaci->base + AACI_ALLINTS);
334 for (i = 0; i < 4; i++, m >>= 7) {
336 aaci_fifo_irq(aaci, i, m);
341 return mask ? IRQ_HANDLED : IRQ_NONE;
349 static struct snd_pcm_hardware aaci_hw_info = {
350 .info = SNDRV_PCM_INFO_MMAP |
351 SNDRV_PCM_INFO_MMAP_VALID |
352 SNDRV_PCM_INFO_INTERLEAVED |
353 SNDRV_PCM_INFO_BLOCK_TRANSFER |
354 SNDRV_PCM_INFO_RESUME,
357 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
358 * words. It also doesn't support 12-bit at all.
360 .formats = SNDRV_PCM_FMTBIT_S16_LE,
362 /* rates are setup from the AC'97 codec */
365 .buffer_bytes_max = 64 * 1024,
366 .period_bytes_min = 256,
367 .period_bytes_max = PAGE_SIZE,
369 .periods_max = PAGE_SIZE / 16,
372 static int __aaci_pcm_open(struct aaci *aaci,
373 struct snd_pcm_substream *substream,
374 struct aaci_runtime *aacirun)
376 struct snd_pcm_runtime *runtime = substream->runtime;
379 aacirun->substream = substream;
380 runtime->private_data = aacirun;
381 runtime->hw = aaci_hw_info;
382 runtime->hw.rates = aacirun->pcm->rates;
383 snd_pcm_limit_hw_rates(runtime);
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
386 aacirun->pcm->r[1].slots)
387 snd_ac97_pcm_double_rate_rules(runtime);
390 * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
391 * mode, each 32-bit word contains one sample. If we're in
392 * compact mode, each 32-bit word contains two samples, effectively
393 * halving the FIFO size. However, we don't know for sure which
394 * we'll be using at this point. We set this to the lower limit.
396 runtime->hw.fifo_size = aaci->fifosize * 2;
398 ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
413 static int aaci_pcm_close(struct snd_pcm_substream *substream)
415 struct aaci *aaci = substream->private_data;
416 struct aaci_runtime *aacirun = substream->runtime->private_data;
418 WARN_ON(aacirun->cr & CR_EN);
420 aacirun->substream = NULL;
421 free_irq(aaci->dev->irq[0], aaci);
426 static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
428 struct aaci_runtime *aacirun = substream->runtime->private_data;
431 * This must not be called with the device enabled.
433 WARN_ON(aacirun->cr & CR_EN);
435 if (aacirun->pcm_open)
436 snd_ac97_pcm_close(aacirun->pcm);
437 aacirun->pcm_open = 0;
440 * Clear out the DMA and any allocated buffers.
442 snd_pcm_lib_free_pages(substream);
447 static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
448 struct aaci_runtime *aacirun,
449 struct snd_pcm_hw_params *params)
452 struct aaci *aaci = substream->private_data;
454 aaci_pcm_hw_free(substream);
455 if (aacirun->pcm_open) {
456 snd_ac97_pcm_close(aacirun->pcm);
457 aacirun->pcm_open = 0;
460 err = snd_pcm_lib_malloc_pages(substream,
461 params_buffer_bytes(params));
463 unsigned int rate = params_rate(params);
464 int dbl = rate > 48000;
466 err = snd_ac97_pcm_open(aacirun->pcm, rate,
467 params_channels(params),
468 aacirun->pcm->r[dbl].slots);
470 aacirun->pcm_open = err == 0;
471 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
472 aacirun->fifosz = aaci->fifosize * 4;
474 if (aacirun->cr & CR_COMPACT)
475 aacirun->fifosz >>= 1;
481 static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
483 struct snd_pcm_runtime *runtime = substream->runtime;
484 struct aaci_runtime *aacirun = runtime->private_data;
486 aacirun->start = runtime->dma_area;
487 aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
488 aacirun->ptr = aacirun->start;
490 aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
495 static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
497 struct snd_pcm_runtime *runtime = substream->runtime;
498 struct aaci_runtime *aacirun = runtime->private_data;
499 ssize_t bytes = aacirun->ptr - aacirun->start;
501 return bytes_to_frames(runtime, bytes);
506 * Playback specific ALSA stuff
508 static const u32 channels_to_txmask[] = {
509 [2] = CR_SL3 | CR_SL4,
510 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
511 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
515 * We can support two and four channel audio. Unfortunately
516 * six channel audio requires a non-standard channel ordering:
518 * 4 -> FL(3), FR(4), SL(7), SR(8)
519 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
520 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
521 * This requires an ALSA configuration file to correct.
523 static unsigned int channel_list[] = { 2, 4, 6 };
526 aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
528 struct aaci *aaci = rule->private;
529 unsigned int chan_mask = 1 << 0, slots;
532 * pcms[0] is the our 5.1 PCM instance.
534 slots = aaci->ac97_bus->pcms[0].r[0].slots;
535 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
537 if (slots & (1 << AC97_SLOT_LFE))
541 return snd_interval_list(hw_param_interval(p, rule->var),
542 ARRAY_SIZE(channel_list), channel_list,
546 static int aaci_pcm_open(struct snd_pcm_substream *substream)
548 struct aaci *aaci = substream->private_data;
552 * Add rule describing channel dependency.
554 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
555 SNDRV_PCM_HW_PARAM_CHANNELS,
556 aaci_rule_channels, aaci,
557 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
561 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
562 ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
564 ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
569 static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
570 struct snd_pcm_hw_params *params)
572 struct aaci_runtime *aacirun = substream->runtime->private_data;
573 unsigned int channels = params_channels(params);
576 WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
577 !channels_to_txmask[channels]);
579 ret = aaci_pcm_hw_params(substream, aacirun, params);
582 * Enable FIFO, compact mode, 16 bits per sample.
583 * FIXME: double rate slots?
586 aacirun->cr |= channels_to_txmask[channels];
591 static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
595 ie = readl(aacirun->base + AACI_IE);
596 ie &= ~(IE_URIE|IE_TXIE);
597 writel(ie, aacirun->base + AACI_IE);
598 aacirun->cr &= ~CR_EN;
599 aaci_chan_wait_ready(aacirun, SR_TXB);
600 writel(aacirun->cr, aacirun->base + AACI_TXCR);
603 static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
607 aaci_chan_wait_ready(aacirun, SR_TXB);
608 aacirun->cr |= CR_EN;
610 ie = readl(aacirun->base + AACI_IE);
611 ie |= IE_URIE | IE_TXIE;
612 writel(ie, aacirun->base + AACI_IE);
613 writel(aacirun->cr, aacirun->base + AACI_TXCR);
616 static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
618 struct aaci_runtime *aacirun = substream->runtime->private_data;
622 spin_lock_irqsave(&aacirun->lock, flags);
625 case SNDRV_PCM_TRIGGER_START:
626 aaci_pcm_playback_start(aacirun);
629 case SNDRV_PCM_TRIGGER_RESUME:
630 aaci_pcm_playback_start(aacirun);
633 case SNDRV_PCM_TRIGGER_STOP:
634 aaci_pcm_playback_stop(aacirun);
637 case SNDRV_PCM_TRIGGER_SUSPEND:
638 aaci_pcm_playback_stop(aacirun);
641 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
644 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
651 spin_unlock_irqrestore(&aacirun->lock, flags);
656 static struct snd_pcm_ops aaci_playback_ops = {
657 .open = aaci_pcm_open,
658 .close = aaci_pcm_close,
659 .ioctl = snd_pcm_lib_ioctl,
660 .hw_params = aaci_pcm_playback_hw_params,
661 .hw_free = aaci_pcm_hw_free,
662 .prepare = aaci_pcm_prepare,
663 .trigger = aaci_pcm_playback_trigger,
664 .pointer = aaci_pcm_pointer,
667 static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
668 struct snd_pcm_hw_params *params)
670 struct aaci_runtime *aacirun = substream->runtime->private_data;
673 ret = aaci_pcm_hw_params(substream, aacirun, params);
675 /* Line in record: slot 3 and 4 */
676 aacirun->cr |= CR_SL3 | CR_SL4;
681 static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
685 aaci_chan_wait_ready(aacirun, SR_RXB);
687 ie = readl(aacirun->base + AACI_IE);
688 ie &= ~(IE_ORIE | IE_RXIE);
689 writel(ie, aacirun->base+AACI_IE);
691 aacirun->cr &= ~CR_EN;
693 writel(aacirun->cr, aacirun->base + AACI_RXCR);
696 static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
700 aaci_chan_wait_ready(aacirun, SR_RXB);
703 /* RX Timeout value: bits 28:17 in RXCR */
704 aacirun->cr |= 0xf << 17;
707 aacirun->cr |= CR_EN;
708 writel(aacirun->cr, aacirun->base + AACI_RXCR);
710 ie = readl(aacirun->base + AACI_IE);
711 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
712 writel(ie, aacirun->base + AACI_IE);
715 static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
717 struct aaci_runtime *aacirun = substream->runtime->private_data;
721 spin_lock_irqsave(&aacirun->lock, flags);
724 case SNDRV_PCM_TRIGGER_START:
725 aaci_pcm_capture_start(aacirun);
728 case SNDRV_PCM_TRIGGER_RESUME:
729 aaci_pcm_capture_start(aacirun);
732 case SNDRV_PCM_TRIGGER_STOP:
733 aaci_pcm_capture_stop(aacirun);
736 case SNDRV_PCM_TRIGGER_SUSPEND:
737 aaci_pcm_capture_stop(aacirun);
740 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
743 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
750 spin_unlock_irqrestore(&aacirun->lock, flags);
755 static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
757 struct snd_pcm_runtime *runtime = substream->runtime;
758 struct aaci *aaci = substream->private_data;
760 aaci_pcm_prepare(substream);
762 /* allow changing of sample rate */
763 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
764 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
765 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
767 /* Record select: Mic: 0, Aux: 3, Line: 4 */
768 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
773 static struct snd_pcm_ops aaci_capture_ops = {
774 .open = aaci_pcm_open,
775 .close = aaci_pcm_close,
776 .ioctl = snd_pcm_lib_ioctl,
777 .hw_params = aaci_pcm_capture_hw_params,
778 .hw_free = aaci_pcm_hw_free,
779 .prepare = aaci_pcm_capture_prepare,
780 .trigger = aaci_pcm_capture_trigger,
781 .pointer = aaci_pcm_pointer,
788 static int aaci_do_suspend(struct snd_card *card, unsigned int state)
790 struct aaci *aaci = card->private_data;
791 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
792 snd_pcm_suspend_all(aaci->pcm);
796 static int aaci_do_resume(struct snd_card *card, unsigned int state)
798 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
802 static int aaci_suspend(struct amba_device *dev, pm_message_t state)
804 struct snd_card *card = amba_get_drvdata(dev);
805 return card ? aaci_do_suspend(card) : 0;
808 static int aaci_resume(struct amba_device *dev)
810 struct snd_card *card = amba_get_drvdata(dev);
811 return card ? aaci_do_resume(card) : 0;
814 #define aaci_do_suspend NULL
815 #define aaci_do_resume NULL
816 #define aaci_suspend NULL
817 #define aaci_resume NULL
821 static struct ac97_pcm ac97_defs[] __devinitdata = {
822 [0] = { /* Front PCM */
826 .slots = (1 << AC97_SLOT_PCM_LEFT) |
827 (1 << AC97_SLOT_PCM_RIGHT) |
828 (1 << AC97_SLOT_PCM_CENTER) |
829 (1 << AC97_SLOT_PCM_SLEFT) |
830 (1 << AC97_SLOT_PCM_SRIGHT) |
831 (1 << AC97_SLOT_LFE),
834 .slots = (1 << AC97_SLOT_PCM_LEFT) |
835 (1 << AC97_SLOT_PCM_RIGHT) |
836 (1 << AC97_SLOT_PCM_LEFT_0) |
837 (1 << AC97_SLOT_PCM_RIGHT_0),
846 .slots = (1 << AC97_SLOT_PCM_LEFT) |
847 (1 << AC97_SLOT_PCM_RIGHT),
856 .slots = (1 << AC97_SLOT_MIC),
862 static struct snd_ac97_bus_ops aaci_bus_ops = {
863 .write = aaci_ac97_write,
864 .read = aaci_ac97_read,
867 static int __devinit aaci_probe_ac97(struct aaci *aaci)
869 struct snd_ac97_template ac97_template;
870 struct snd_ac97_bus *ac97_bus;
871 struct snd_ac97 *ac97;
875 * Assert AACIRESET for 2us
877 writel(0, aaci->base + AACI_RESET);
879 writel(RESET_NRST, aaci->base + AACI_RESET);
882 * Give the AC'97 codec more than enough time
883 * to wake up. (42us = ~2 frames at 48kHz.)
885 udelay(FRAME_PERIOD_US * 2);
887 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
891 ac97_bus->clock = 48000;
892 aaci->ac97_bus = ac97_bus;
894 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
895 ac97_template.private_data = aaci;
896 ac97_template.num = 0;
897 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
899 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
905 * Disable AC97 PC Beep input on audio codecs.
907 if (ac97_is_audio(ac97))
908 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
910 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
914 aaci->playback.pcm = &ac97_bus->pcms[0];
915 aaci->capture.pcm = &ac97_bus->pcms[1];
921 static void aaci_free_card(struct snd_card *card)
923 struct aaci *aaci = card->private_data;
928 static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
931 struct snd_card *card;
934 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
935 THIS_MODULE, sizeof(struct aaci), &card);
939 card->private_free = aaci_free_card;
941 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
942 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
943 snprintf(card->longname, sizeof(card->longname),
944 "%s at 0x%016llx, irq %d",
945 card->shortname, (unsigned long long)dev->res.start,
948 aaci = card->private_data;
949 mutex_init(&aaci->ac97_sem);
953 /* Set MAINCR to allow slot 1 and 2 data IO */
954 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
955 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
960 static int __devinit aaci_init_pcm(struct aaci *aaci)
965 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
968 pcm->private_data = aaci;
971 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
973 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
974 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
975 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
982 static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
984 struct aaci_runtime *aacirun = &aaci->playback;
987 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
989 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
990 writel(0, aacirun->fifo);
992 writel(0, aacirun->base + AACI_TXCR);
995 * Re-initialise the AACI after the FIFO depth test, to
996 * ensure that the FIFOs are empty. Unfortunately, merely
997 * disabling the channel doesn't clear the FIFO.
999 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
1000 readl(aaci->base + AACI_MAINCR);
1002 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1005 * If we hit 4096, we failed. Go back to the specified
1014 static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
1019 ret = amba_request_regions(dev, NULL);
1023 aaci = aaci_init_card(dev);
1029 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
1036 * Playback uses AACI channel 0
1038 spin_lock_init(&aaci->playback.lock);
1039 aaci->playback.base = aaci->base + AACI_CSCH1;
1040 aaci->playback.fifo = aaci->base + AACI_DR1;
1043 * Capture uses AACI channel 0
1045 spin_lock_init(&aaci->capture.lock);
1046 aaci->capture.base = aaci->base + AACI_CSCH1;
1047 aaci->capture.fifo = aaci->base + AACI_DR1;
1049 for (i = 0; i < 4; i++) {
1050 void __iomem *base = aaci->base + i * 0x14;
1052 writel(0, base + AACI_IE);
1053 writel(0, base + AACI_TXCR);
1054 writel(0, base + AACI_RXCR);
1057 writel(0x1fff, aaci->base + AACI_INTCLR);
1058 writel(aaci->maincr, aaci->base + AACI_MAINCR);
1060 * Fix: ac97 read back fail errors by reading
1061 * from any arbitrary aaci register.
1063 readl(aaci->base + AACI_CSCH1);
1064 ret = aaci_probe_ac97(aaci);
1069 * Size the FIFOs (must be multiple of 16).
1071 aaci->fifosize = aaci_size_fifo(aaci);
1072 if (aaci->fifosize & 15) {
1073 printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
1079 ret = aaci_init_pcm(aaci);
1083 snd_card_set_dev(aaci->card, &dev->dev);
1085 ret = snd_card_register(aaci->card);
1087 dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
1089 amba_set_drvdata(dev, aaci->card);
1095 snd_card_free(aaci->card);
1096 amba_release_regions(dev);
1100 static int __devexit aaci_remove(struct amba_device *dev)
1102 struct snd_card *card = amba_get_drvdata(dev);
1104 amba_set_drvdata(dev, NULL);
1107 struct aaci *aaci = card->private_data;
1108 writel(0, aaci->base + AACI_MAINCR);
1110 snd_card_free(card);
1111 amba_release_regions(dev);
1117 static struct amba_id aaci_ids[] = {
1125 static struct amba_driver aaci_driver = {
1127 .name = DRIVER_NAME,
1129 .probe = aaci_probe,
1130 .remove = __devexit_p(aaci_remove),
1131 .suspend = aaci_suspend,
1132 .resume = aaci_resume,
1133 .id_table = aaci_ids,
1136 static int __init aaci_init(void)
1138 return amba_driver_register(&aaci_driver);
1141 static void __exit aaci_exit(void)
1143 amba_driver_unregister(&aaci_driver);
1146 module_init(aaci_init);
1147 module_exit(aaci_exit);
1149 MODULE_LICENSE("GPL");
1150 MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");