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/regs-ac97.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
;
33 static struct clk
*ac97conf_clk
;
38 * o Slot 12 read from modem space will hang controller.
39 * o CDONE, SDONE interrupt fails after any slot 12 IO.
41 * We therefore have an hybrid approach for waiting on SDONE (interrupt or
42 * 1 jiffy timeout if interrupt never comes).
45 unsigned short pxa2xx_ac97_read(struct snd_ac97
*ac97
, unsigned short reg
)
47 unsigned short val
= -1;
48 volatile u32
*reg_addr
;
50 mutex_lock(&car_mutex
);
52 /* set up primary or secondary codec space */
53 if (cpu_is_pxa25x() && reg
== AC97_GPIO_STATUS
)
54 reg_addr
= ac97
->num
? &SMC_REG_BASE
: &PMC_REG_BASE
;
56 reg_addr
= ac97
->num
? &SAC_REG_BASE
: &PAC_REG_BASE
;
57 reg_addr
+= (reg
>> 1);
59 /* start read access across the ac97 link */
60 GSR
= GSR_CDONE
| GSR_SDONE
;
63 if (reg
== AC97_GPIO_STATUS
)
65 if (wait_event_timeout(gsr_wq
, (GSR
| gsr_bits
) & GSR_SDONE
, 1) <= 0 &&
66 !((GSR
| gsr_bits
) & GSR_SDONE
)) {
67 printk(KERN_ERR
"%s: read error (ac97_reg=%d GSR=%#lx)\n",
68 __func__
, reg
, GSR
| gsr_bits
);
74 GSR
= GSR_CDONE
| GSR_SDONE
;
77 /* but we've just started another cycle... */
78 wait_event_timeout(gsr_wq
, (GSR
| gsr_bits
) & GSR_SDONE
, 1);
80 out
: mutex_unlock(&car_mutex
);
83 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read
);
85 void pxa2xx_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
88 volatile u32
*reg_addr
;
90 mutex_lock(&car_mutex
);
92 /* set up primary or secondary codec space */
93 if (cpu_is_pxa25x() && reg
== AC97_GPIO_STATUS
)
94 reg_addr
= ac97
->num
? &SMC_REG_BASE
: &PMC_REG_BASE
;
96 reg_addr
= ac97
->num
? &SAC_REG_BASE
: &PAC_REG_BASE
;
97 reg_addr
+= (reg
>> 1);
99 GSR
= GSR_CDONE
| GSR_SDONE
;
102 if (wait_event_timeout(gsr_wq
, (GSR
| gsr_bits
) & GSR_CDONE
, 1) <= 0 &&
103 !((GSR
| gsr_bits
) & GSR_CDONE
))
104 printk(KERN_ERR
"%s: write error (ac97_reg=%d GSR=%#lx)\n",
105 __func__
, reg
, GSR
| gsr_bits
);
107 mutex_unlock(&car_mutex
);
109 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write
);
112 static inline void pxa_ac97_warm_pxa25x(void)
116 GCR
|= GCR_WARM_RST
| GCR_PRIRDY_IEN
| GCR_SECRDY_IEN
;
117 wait_event_timeout(gsr_wq
, gsr_bits
& (GSR_PCR
| GSR_SCR
), 1);
120 static inline void pxa_ac97_cold_pxa25x(void)
122 GCR
&= GCR_COLD_RST
; /* clear everything but nCRST */
123 GCR
&= ~GCR_COLD_RST
; /* then assert nCRST */
128 GCR
|= GCR_CDONE_IE
|GCR_SDONE_IE
;
129 wait_event_timeout(gsr_wq
, gsr_bits
& (GSR_PCR
| GSR_SCR
), 1);
134 static inline void pxa_ac97_warm_pxa27x(void)
138 /* warm reset broken on Bulverde,
139 so manually keep AC97 reset high */
140 pxa_gpio_mode(113 | GPIO_OUT
| GPIO_DFLT_HIGH
);
143 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT
);
147 static inline void pxa_ac97_cold_pxa27x(void)
149 GCR
&= GCR_COLD_RST
; /* clear everything but nCRST */
150 GCR
&= ~GCR_COLD_RST
; /* then assert nCRST */
154 /* PXA27x Developers Manual section 13.5.2.2.1 */
155 clk_enable(ac97conf_clk
);
157 clk_disable(ac97conf_clk
);
164 static inline void pxa_ac97_warm_pxa3xx(void)
170 /* Can't use interrupts */
172 while (!((GSR
| gsr_bits
) & (GSR_PCR
| GSR_SCR
)) && timeout
--)
176 static inline void pxa_ac97_cold_pxa3xx(void)
180 /* Hold CLKBPB for 100us */
186 GCR
&= GCR_COLD_RST
; /* clear everything but nCRST */
187 GCR
&= ~GCR_COLD_RST
; /* then assert nCRST */
191 /* Can't use interrupts on PXA3xx */
192 GCR
&= ~(GCR_PRIRDY_IEN
|GCR_SECRDY_IEN
);
194 GCR
= GCR_WARM_RST
| GCR_COLD_RST
;
195 while (!(GSR
& (GSR_PCR
| GSR_SCR
)) && timeout
--)
200 bool pxa2xx_ac97_try_warm_reset(struct snd_ac97
*ac97
)
204 pxa_ac97_warm_pxa25x();
209 pxa_ac97_warm_pxa27x();
214 pxa_ac97_warm_pxa3xx();
219 if (!((GSR
| gsr_bits
) & (GSR_PCR
| GSR_SCR
))) {
220 printk(KERN_INFO
"%s: warm reset timeout (GSR=%#lx)\n",
228 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset
);
230 bool pxa2xx_ac97_try_cold_reset(struct snd_ac97
*ac97
)
234 pxa_ac97_cold_pxa25x();
239 pxa_ac97_cold_pxa27x();
244 pxa_ac97_cold_pxa3xx();
249 if (!((GSR
| gsr_bits
) & (GSR_PCR
| GSR_SCR
))) {
250 printk(KERN_INFO
"%s: cold reset timeout (GSR=%#lx)\n",
258 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset
);
261 void pxa2xx_ac97_finish_reset(struct snd_ac97
*ac97
)
263 GCR
&= ~(GCR_PRIRDY_IEN
|GCR_SECRDY_IEN
);
264 GCR
|= GCR_SDONE_IE
|GCR_CDONE_IE
;
266 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset
);
268 static irqreturn_t
pxa2xx_ac97_irq(int irq
, void *dev_id
)
278 /* Although we don't use those we still need to clear them
279 since they tend to spuriously trigger when MMC is used
280 (hardware bug? go figure)... */
281 if (cpu_is_pxa27x()) {
294 int pxa2xx_ac97_hw_suspend(void)
296 GCR
|= GCR_ACLINK_OFF
;
297 clk_disable(ac97_clk
);
300 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend
);
302 int pxa2xx_ac97_hw_resume(void)
304 if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
305 pxa_gpio_mode(GPIO31_SYNC_AC97_MD
);
306 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD
);
307 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD
);
308 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD
);
310 if (cpu_is_pxa27x()) {
311 /* Use GPIO 113 as AC97 Reset on Bulverde */
312 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT
);
314 clk_enable(ac97_clk
);
317 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume
);
320 int __devinit
pxa2xx_ac97_hw_probe(struct platform_device
*dev
)
324 if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
325 pxa_gpio_mode(GPIO31_SYNC_AC97_MD
);
326 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD
);
327 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD
);
328 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD
);
331 if (cpu_is_pxa27x()) {
332 /* Use GPIO 113 as AC97 Reset on Bulverde */
333 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT
);
334 ac97conf_clk
= clk_get(&dev
->dev
, "AC97CONFCLK");
335 if (IS_ERR(ac97conf_clk
)) {
336 ret
= PTR_ERR(ac97conf_clk
);
342 ac97_clk
= clk_get(&dev
->dev
, "AC97CLK");
343 if (IS_ERR(ac97_clk
)) {
344 ret
= PTR_ERR(ac97_clk
);
349 ret
= clk_enable(ac97_clk
);
353 ret
= request_irq(IRQ_AC97
, pxa2xx_ac97_irq
, IRQF_DISABLED
, "AC97", NULL
);
360 GCR
|= GCR_ACLINK_OFF
;
366 clk_put(ac97conf_clk
);
372 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe
);
374 void pxa2xx_ac97_hw_remove(struct platform_device
*dev
)
376 GCR
|= GCR_ACLINK_OFF
;
377 free_irq(IRQ_AC97
, NULL
);
379 clk_put(ac97conf_clk
);
382 clk_disable(ac97_clk
);
386 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove
);
388 MODULE_AUTHOR("Nicolas Pitre");
389 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
390 MODULE_LICENSE("GPL");