2 * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
4 * Author: Nicolas Pitre
5 * Created: Dec 02, 2004
6 * Copyright: MontaVista Software Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/wait.h>
18 #include <linux/delay.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/ac97_codec.h>
23 #include <sound/initval.h>
24 #include <sound/soc.h>
27 #include <linux/mutex.h>
28 #include <asm/hardware.h>
29 #include <asm/arch/pxa-regs.h>
30 #include <asm/arch/audio.h>
32 #include "pxa2xx-pcm.h"
33 #include "pxa2xx-ac97.h"
35 static DEFINE_MUTEX(car_mutex);
36 static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
37 static volatile long gsr_bits;
42 * o Slot 12 read from modem space will hang controller.
43 * o CDONE, SDONE interrupt fails after any slot 12 IO.
45 * We therefore have an hybrid approach for waiting on SDONE (interrupt or
46 * 1 jiffy timeout if interrupt never comes).
49 static unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97,
52 unsigned short val = -1;
53 volatile u32 *reg_addr;
55 mutex_lock(&car_mutex);
57 /* set up primary or secondary codec/modem space */
59 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
61 if (reg == AC97_GPIO_STATUS)
62 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
64 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
66 reg_addr += (reg >> 1);
69 if (reg == AC97_GPIO_STATUS) {
70 /* read from controller cache */
76 /* start read access across the ac97 link */
77 GSR = GSR_CDONE | GSR_SDONE;
81 wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
82 if (!((GSR | gsr_bits) & GSR_SDONE)) {
83 printk(KERN_ERR "%s: read error (ac97_reg=%x GSR=%#lx)\n",
84 __FUNCTION__, reg, GSR | gsr_bits);
90 GSR = GSR_CDONE | GSR_SDONE;
93 /* but we've just started another cycle... */
94 wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
96 out: mutex_unlock(&car_mutex);
100 static void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
103 volatile u32 *reg_addr;
105 mutex_lock(&car_mutex);
107 /* set up primary or secondary codec/modem space */
109 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
111 if (reg == AC97_GPIO_STATUS)
112 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
114 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
116 reg_addr += (reg >> 1);
118 GSR = GSR_CDONE | GSR_SDONE;
121 wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1);
122 if (!((GSR | gsr_bits) & GSR_CDONE))
123 printk(KERN_ERR "%s: write error (ac97_reg=%x GSR=%#lx)\n",
124 __FUNCTION__, reg, GSR | gsr_bits);
126 mutex_unlock(&car_mutex);
129 static void pxa2xx_ac97_warm_reset(struct snd_ac97 *ac97)
134 /* warm reset broken on Bulverde,
135 so manually keep AC97 reset high */
136 pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
139 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
142 GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
143 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
146 if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
147 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
148 __FUNCTION__, gsr_bits);
150 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
151 GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
154 static void pxa2xx_ac97_cold_reset(struct snd_ac97 *ac97)
156 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
157 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
161 /* PXA27x Developers Manual section 13.5.2.2.1 */
162 pxa_set_cken(CKEN_AC97CONF, 1);
164 pxa_set_cken(CKEN_AC97CONF, 0);
169 GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
170 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
173 if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
174 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
175 __FUNCTION__, gsr_bits);
177 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
178 GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
181 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
192 /* Although we don't use those we still need to clear them
193 since they tend to spuriously trigger when MMC is used
194 (hardware bug? go figure)... */
206 struct snd_ac97_bus_ops soc_ac97_ops = {
207 .read = pxa2xx_ac97_read,
208 .write = pxa2xx_ac97_write,
209 .warm_reset = pxa2xx_ac97_warm_reset,
210 .reset = pxa2xx_ac97_cold_reset,
213 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_out = {
214 .name = "AC97 PCM Stereo out",
215 .dev_addr = __PREG(PCDR),
216 .drcmr = &DRCMRTXPCDR,
217 .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
218 DCMD_BURST32 | DCMD_WIDTH4,
221 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_in = {
222 .name = "AC97 PCM Stereo in",
223 .dev_addr = __PREG(PCDR),
224 .drcmr = &DRCMRRXPCDR,
225 .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
226 DCMD_BURST32 | DCMD_WIDTH4,
229 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_out = {
230 .name = "AC97 Aux PCM (Slot 5) Mono out",
231 .dev_addr = __PREG(MODR),
232 .drcmr = &DRCMRTXMODR,
233 .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
234 DCMD_BURST16 | DCMD_WIDTH2,
237 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_in = {
238 .name = "AC97 Aux PCM (Slot 5) Mono in",
239 .dev_addr = __PREG(MODR),
240 .drcmr = &DRCMRRXMODR,
241 .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
242 DCMD_BURST16 | DCMD_WIDTH2,
245 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_mic_mono_in = {
246 .name = "AC97 Mic PCM (Slot 6) Mono in",
247 .dev_addr = __PREG(MCDR),
248 .drcmr = &DRCMRRXMCDR,
249 .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
250 DCMD_BURST16 | DCMD_WIDTH2,
254 static int pxa2xx_ac97_suspend(struct platform_device *pdev,
255 struct snd_soc_cpu_dai *dai)
257 GCR |= GCR_ACLINK_OFF;
258 pxa_set_cken(CKEN_AC97, 0);
262 static int pxa2xx_ac97_resume(struct platform_device *pdev,
263 struct snd_soc_cpu_dai *dai)
265 pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
266 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
267 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
268 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
270 /* Use GPIO 113 as AC97 Reset on Bulverde */
271 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
273 pxa_set_cken(CKEN_AC97, 1);
278 #define pxa2xx_ac97_suspend NULL
279 #define pxa2xx_ac97_resume NULL
282 static int pxa2xx_ac97_probe(struct platform_device *pdev)
286 ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
290 pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
291 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
292 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
293 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
295 /* Use GPIO 113 as AC97 Reset on Bulverde */
296 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
298 pxa_set_cken(CKEN_AC97, 1);
302 if (CKEN & (1 << CKEN_AC97)) {
303 GCR |= GCR_ACLINK_OFF;
304 free_irq(IRQ_AC97, NULL);
305 pxa_set_cken(CKEN_AC97, 0);
310 static void pxa2xx_ac97_remove(struct platform_device *pdev)
312 GCR |= GCR_ACLINK_OFF;
313 free_irq(IRQ_AC97, NULL);
314 pxa_set_cken(CKEN_AC97, 0);
317 static int pxa2xx_ac97_hw_params(struct snd_pcm_substream *substream,
318 struct snd_pcm_hw_params *params)
320 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
323 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
324 cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_out;
326 cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_in;
331 static int pxa2xx_ac97_hw_aux_params(struct snd_pcm_substream *substream,
332 struct snd_pcm_hw_params *params)
334 struct snd_soc_pcm_runtime *rtd = substream->private_data;
335 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
337 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
338 cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_out;
340 cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_in;
345 static int pxa2xx_ac97_hw_mic_params(struct snd_pcm_substream *substream,
346 struct snd_pcm_hw_params *params)
348 struct snd_soc_pcm_runtime *rtd = substream->private_data;
349 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
351 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
354 cpu_dai->dma_data = &pxa2xx_ac97_pcm_mic_mono_in;
359 #define PXA2XX_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
360 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
361 SNDRV_PCM_RATE_48000)
364 * There is only 1 physical AC97 interface for pxa2xx, but it
365 * has extra fifo's that can be used for aux DACs and ADCs.
367 struct snd_soc_cpu_dai pxa_ac97_dai[] = {
369 .name = "pxa2xx-ac97",
371 .type = SND_SOC_DAI_AC97,
372 .probe = pxa2xx_ac97_probe,
373 .remove = pxa2xx_ac97_remove,
374 .suspend = pxa2xx_ac97_suspend,
375 .resume = pxa2xx_ac97_resume,
377 .stream_name = "AC97 Playback",
380 .rates = PXA2XX_AC97_RATES,
381 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
383 .stream_name = "AC97 Capture",
386 .rates = PXA2XX_AC97_RATES,
387 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
389 .hw_params = pxa2xx_ac97_hw_params,},
392 .name = "pxa2xx-ac97-aux",
394 .type = SND_SOC_DAI_AC97,
396 .stream_name = "AC97 Aux Playback",
399 .rates = PXA2XX_AC97_RATES,
400 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
402 .stream_name = "AC97 Aux Capture",
405 .rates = PXA2XX_AC97_RATES,
406 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
408 .hw_params = pxa2xx_ac97_hw_aux_params,},
411 .name = "pxa2xx-ac97-mic",
413 .type = SND_SOC_DAI_AC97,
415 .stream_name = "AC97 Mic Capture",
418 .rates = PXA2XX_AC97_RATES,
419 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
421 .hw_params = pxa2xx_ac97_hw_mic_params,},
425 EXPORT_SYMBOL_GPL(pxa_ac97_dai);
426 EXPORT_SYMBOL_GPL(soc_ac97_ops);
428 MODULE_AUTHOR("Nicolas Pitre");
429 MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
430 MODULE_LICENSE("GPL");