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>
20 #include <sound/ac97_codec.h>
21 #include <sound/pxa2xx-lib.h>
24 #include <mach/hardware.h>
25 #include <mach/pxa-regs.h>
26 #include <mach/pxa2xx-gpio.h>
27 #include <mach/audio.h>
29 static DEFINE_MUTEX(car_mutex);
30 static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
31 static volatile long gsr_bits;
32 static struct clk *ac97_clk;
34 static struct clk *ac97conf_clk;
40 * o Slot 12 read from modem space will hang controller.
41 * o CDONE, SDONE interrupt fails after any slot 12 IO.
43 * We therefore have an hybrid approach for waiting on SDONE (interrupt or
44 * 1 jiffy timeout if interrupt never comes).
47 unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
49 unsigned short val = -1;
50 volatile u32 *reg_addr;
52 mutex_lock(&car_mutex);
54 /* set up primary or secondary codec space */
55 #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
56 reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
58 if (reg == AC97_GPIO_STATUS)
59 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
61 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
63 reg_addr += (reg >> 1);
65 /* start read access across the ac97 link */
66 GSR = GSR_CDONE | GSR_SDONE;
69 if (reg == AC97_GPIO_STATUS)
71 if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
72 !((GSR | gsr_bits) & GSR_SDONE)) {
73 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
74 __func__, reg, GSR | gsr_bits);
80 GSR = GSR_CDONE | GSR_SDONE;
83 /* but we've just started another cycle... */
84 wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
86 out: mutex_unlock(&car_mutex);
89 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
91 void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
94 volatile u32 *reg_addr;
96 mutex_lock(&car_mutex);
98 /* set up primary or secondary codec space */
99 #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
100 reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
102 if (reg == AC97_GPIO_STATUS)
103 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
105 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
107 reg_addr += (reg >> 1);
109 GSR = GSR_CDONE | GSR_SDONE;
112 if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
113 !((GSR | gsr_bits) & GSR_CDONE))
114 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
115 __func__, reg, GSR | gsr_bits);
117 mutex_unlock(&car_mutex);
119 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
121 bool pxa2xx_ac97_try_warm_reset(struct snd_ac97 *ac97)
129 /* warm reset broken on Bulverde,
130 so manually keep AC97 reset high */
131 pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
134 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
136 #elif defined(CONFIG_PXA3xx)
137 /* Can't use interrupts */
139 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
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",
155 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
157 bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
162 /* Hold CLKBPB for 100us */
169 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
170 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
174 /* PXA27x Developers Manual section 13.5.2.2.1 */
175 clk_enable(ac97conf_clk);
177 clk_disable(ac97conf_clk);
180 #elif defined(CONFIG_PXA3xx)
181 /* Can't use interrupts on PXA3xx */
182 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
184 GCR = GCR_WARM_RST | GCR_COLD_RST;
185 while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
189 GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
190 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
193 if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
194 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
202 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
205 void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
207 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
208 GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
210 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
212 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
223 /* Although we don't use those we still need to clear them
224 since they tend to spuriously trigger when MMC is used
225 (hardware bug? go figure)... */
238 int pxa2xx_ac97_hw_suspend(void)
240 GCR |= GCR_ACLINK_OFF;
241 clk_disable(ac97_clk);
244 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
246 int pxa2xx_ac97_hw_resume(void)
248 pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
249 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
250 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
251 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
253 /* Use GPIO 113 as AC97 Reset on Bulverde */
254 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
256 clk_enable(ac97_clk);
259 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
262 int __devinit pxa2xx_ac97_hw_probe(struct platform_device *dev)
266 ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
270 pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
271 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
272 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
273 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
275 /* Use GPIO 113 as AC97 Reset on Bulverde */
276 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
277 ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
278 if (IS_ERR(ac97conf_clk)) {
279 ret = PTR_ERR(ac97conf_clk);
285 ac97_clk = clk_get(&dev->dev, "AC97CLK");
286 if (IS_ERR(ac97_clk)) {
287 ret = PTR_ERR(ac97_clk);
292 return clk_enable(ac97_clk);
295 GCR |= GCR_ACLINK_OFF;
298 clk_put(ac97conf_clk);
302 free_irq(IRQ_AC97, NULL);
306 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
308 void pxa2xx_ac97_hw_remove(struct platform_device *dev)
310 GCR |= GCR_ACLINK_OFF;
311 free_irq(IRQ_AC97, NULL);
313 clk_put(ac97conf_clk);
316 clk_disable(ac97_clk);
320 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
322 MODULE_AUTHOR("Nicolas Pitre");
323 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
324 MODULE_LICENSE("GPL");