2 * Based on sound/arm/pxa2xx-ac97.c and sound/soc/pxa/pxa2xx-ac97.c
5 * Author: Nicolas Pitre
6 * Created: Dec 02, 2004
7 * Copyright: MontaVista Software Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/module.h>
21 #include <linux/gpio.h>
23 #include <sound/ac97_codec.h>
24 #include <sound/pxa2xx-lib.h>
26 #include <mach/irqs.h>
27 #include <mach/regs-ac97.h>
28 #include <mach/audio.h>
30 static DEFINE_MUTEX(car_mutex);
31 static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
32 static volatile long gsr_bits;
33 static struct clk *ac97_clk;
34 static struct clk *ac97conf_clk;
35 static int reset_gpio;
37 extern void pxa27x_assert_ac97reset(int reset_gpio, int on);
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 unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
51 unsigned short val = -1;
52 volatile u32 *reg_addr;
54 mutex_lock(&car_mutex);
56 /* set up primary or secondary codec space */
57 if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
58 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
60 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
61 reg_addr += (reg >> 1);
63 /* start read access across the ac97 link */
64 GSR = GSR_CDONE | GSR_SDONE;
67 if (reg == AC97_GPIO_STATUS)
69 if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
70 !((GSR | gsr_bits) & GSR_SDONE)) {
71 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
72 __func__, reg, GSR | gsr_bits);
78 GSR = GSR_CDONE | GSR_SDONE;
81 /* but we've just started another cycle... */
82 wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
84 out: mutex_unlock(&car_mutex);
87 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
89 void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
92 volatile u32 *reg_addr;
94 mutex_lock(&car_mutex);
96 /* set up primary or secondary codec space */
97 if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
98 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
100 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
101 reg_addr += (reg >> 1);
103 GSR = GSR_CDONE | GSR_SDONE;
106 if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
107 !((GSR | gsr_bits) & GSR_CDONE))
108 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
109 __func__, reg, GSR | gsr_bits);
111 mutex_unlock(&car_mutex);
113 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
116 static inline void pxa_ac97_warm_pxa25x(void)
120 GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
121 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
124 static inline void pxa_ac97_cold_pxa25x(void)
126 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
127 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
132 GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
133 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
138 static inline void pxa_ac97_warm_pxa27x(void)
142 /* warm reset broken on Bulverde, so manually keep AC97 reset high */
143 pxa27x_assert_ac97reset(reset_gpio, 1);
146 pxa27x_assert_ac97reset(reset_gpio, 0);
150 static inline void pxa_ac97_cold_pxa27x(void)
152 unsigned int timeout;
154 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
155 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
159 /* PXA27x Developers Manual section 13.5.2.2.1 */
160 clk_enable(ac97conf_clk);
162 clk_disable(ac97conf_clk);
163 GCR = GCR_COLD_RST | GCR_WARM_RST;
164 timeout = 100; /* wait for the codec-ready bit to be set */
165 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
171 static inline void pxa_ac97_warm_pxa3xx(void)
177 /* Can't use interrupts */
179 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
183 static inline void pxa_ac97_cold_pxa3xx(void)
187 /* Hold CLKBPB for 100us */
193 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
194 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
198 /* Can't use interrupts on PXA3xx */
199 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
201 GCR = GCR_WARM_RST | GCR_COLD_RST;
202 while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
207 bool pxa2xx_ac97_try_warm_reset(struct snd_ac97 *ac97)
213 pxa_ac97_warm_pxa25x();
218 pxa_ac97_warm_pxa27x();
223 pxa_ac97_warm_pxa3xx();
227 gsr = GSR | gsr_bits;
228 if (!(gsr & (GSR_PCR | GSR_SCR))) {
229 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
237 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
239 bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
245 pxa_ac97_cold_pxa25x();
250 pxa_ac97_cold_pxa27x();
255 pxa_ac97_cold_pxa3xx();
260 gsr = GSR | gsr_bits;
261 if (!(gsr & (GSR_PCR | GSR_SCR))) {
262 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
270 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
273 void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
275 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
276 GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
278 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
280 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
290 /* Although we don't use those we still need to clear them
291 since they tend to spuriously trigger when MMC is used
292 (hardware bug? go figure)... */
293 if (cpu_is_pxa27x()) {
306 int pxa2xx_ac97_hw_suspend(void)
308 GCR |= GCR_ACLINK_OFF;
309 clk_disable(ac97_clk);
312 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
314 int pxa2xx_ac97_hw_resume(void)
316 clk_enable(ac97_clk);
319 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
322 int pxa2xx_ac97_hw_probe(struct platform_device *dev)
325 pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
328 switch (pdata->reset_gpio) {
331 reset_gpio = pdata->reset_gpio;
339 dev_err(&dev->dev, "Invalid reset GPIO %d\n",
347 if (cpu_is_pxa27x()) {
349 * This gpio is needed for a work-around to a bug in the ac97
350 * controller during warm reset. The direction and level is set
351 * here so that it is an output driven high when switching from
352 * AC97_nRESET alt function to generic gpio.
354 ret = gpio_request_one(reset_gpio, GPIOF_OUT_INIT_HIGH,
355 "pxa27x ac97 reset");
357 pr_err("%s: gpio_request_one() failed: %d\n",
361 pxa27x_assert_ac97reset(reset_gpio, 0);
363 ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
364 if (IS_ERR(ac97conf_clk)) {
365 ret = PTR_ERR(ac97conf_clk);
371 ac97_clk = clk_get(&dev->dev, "AC97CLK");
372 if (IS_ERR(ac97_clk)) {
373 ret = PTR_ERR(ac97_clk);
378 ret = clk_enable(ac97_clk);
382 ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
389 GCR |= GCR_ACLINK_OFF;
395 clk_put(ac97conf_clk);
401 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
403 void pxa2xx_ac97_hw_remove(struct platform_device *dev)
406 gpio_free(reset_gpio);
407 GCR |= GCR_ACLINK_OFF;
408 free_irq(IRQ_AC97, NULL);
410 clk_put(ac97conf_clk);
413 clk_disable(ac97_clk);
417 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
419 MODULE_AUTHOR("Nicolas Pitre");
420 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
421 MODULE_LICENSE("GPL");