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/regs-ac97.h>
25 #include <mach/pxa2xx-gpio.h>
26 #include <mach/audio.h>
28 static DEFINE_MUTEX(car_mutex
);
29 static DECLARE_WAIT_QUEUE_HEAD(gsr_wq
);
30 static volatile long gsr_bits
;
31 static struct clk
*ac97_clk
;
32 static struct clk
*ac97conf_clk
;
33 static int reset_gpio
;
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).
48 RESETGPIO_NORMAL_ALTFUNC
52 * set_resetgpio_mode - computes and sets the AC97_RESET gpio mode on PXA
53 * @mode: chosen action
55 * As the PXA27x CPUs suffer from a AC97 bug, a manual control of the reset line
56 * must be done to insure proper work of AC97 reset line. This function
57 * computes the correct gpio_mode for further use by reset functions, and
58 * applied the change through pxa_gpio_mode.
60 static void set_resetgpio_mode(int resetgpio_action
)
65 switch (resetgpio_action
) {
66 case RESETGPIO_NORMAL_ALTFUNC
:
67 if (reset_gpio
== 113)
68 mode
= 113 | GPIO_ALT_FN_2_OUT
;
70 mode
= 95 | GPIO_ALT_FN_1_OUT
;
72 case RESETGPIO_FORCE_LOW
:
73 mode
= reset_gpio
| GPIO_OUT
| GPIO_DFLT_LOW
;
75 case RESETGPIO_FORCE_HIGH
:
76 mode
= reset_gpio
| GPIO_OUT
| GPIO_DFLT_HIGH
;
84 unsigned short pxa2xx_ac97_read(struct snd_ac97
*ac97
, unsigned short reg
)
86 unsigned short val
= -1;
87 volatile u32
*reg_addr
;
89 mutex_lock(&car_mutex
);
91 /* set up primary or secondary codec space */
92 if (cpu_is_pxa25x() && reg
== AC97_GPIO_STATUS
)
93 reg_addr
= ac97
->num
? &SMC_REG_BASE
: &PMC_REG_BASE
;
95 reg_addr
= ac97
->num
? &SAC_REG_BASE
: &PAC_REG_BASE
;
96 reg_addr
+= (reg
>> 1);
98 /* start read access across the ac97 link */
99 GSR
= GSR_CDONE
| GSR_SDONE
;
102 if (reg
== AC97_GPIO_STATUS
)
104 if (wait_event_timeout(gsr_wq
, (GSR
| gsr_bits
) & GSR_SDONE
, 1) <= 0 &&
105 !((GSR
| gsr_bits
) & GSR_SDONE
)) {
106 printk(KERN_ERR
"%s: read error (ac97_reg=%d GSR=%#lx)\n",
107 __func__
, reg
, GSR
| gsr_bits
);
113 GSR
= GSR_CDONE
| GSR_SDONE
;
116 /* but we've just started another cycle... */
117 wait_event_timeout(gsr_wq
, (GSR
| gsr_bits
) & GSR_SDONE
, 1);
119 out
: mutex_unlock(&car_mutex
);
122 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read
);
124 void pxa2xx_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
127 volatile u32
*reg_addr
;
129 mutex_lock(&car_mutex
);
131 /* set up primary or secondary codec space */
132 if (cpu_is_pxa25x() && reg
== AC97_GPIO_STATUS
)
133 reg_addr
= ac97
->num
? &SMC_REG_BASE
: &PMC_REG_BASE
;
135 reg_addr
= ac97
->num
? &SAC_REG_BASE
: &PAC_REG_BASE
;
136 reg_addr
+= (reg
>> 1);
138 GSR
= GSR_CDONE
| GSR_SDONE
;
141 if (wait_event_timeout(gsr_wq
, (GSR
| gsr_bits
) & GSR_CDONE
, 1) <= 0 &&
142 !((GSR
| gsr_bits
) & GSR_CDONE
))
143 printk(KERN_ERR
"%s: write error (ac97_reg=%d GSR=%#lx)\n",
144 __func__
, reg
, GSR
| gsr_bits
);
146 mutex_unlock(&car_mutex
);
148 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write
);
151 static inline void pxa_ac97_warm_pxa25x(void)
155 GCR
|= GCR_WARM_RST
| GCR_PRIRDY_IEN
| GCR_SECRDY_IEN
;
156 wait_event_timeout(gsr_wq
, gsr_bits
& (GSR_PCR
| GSR_SCR
), 1);
159 static inline void pxa_ac97_cold_pxa25x(void)
161 GCR
&= GCR_COLD_RST
; /* clear everything but nCRST */
162 GCR
&= ~GCR_COLD_RST
; /* then assert nCRST */
167 GCR
|= GCR_CDONE_IE
|GCR_SDONE_IE
;
168 wait_event_timeout(gsr_wq
, gsr_bits
& (GSR_PCR
| GSR_SCR
), 1);
173 static inline void pxa_ac97_warm_pxa27x(void)
177 /* warm reset broken on Bulverde,
178 so manually keep AC97 reset high */
179 set_resetgpio_mode(RESETGPIO_FORCE_HIGH
);
182 set_resetgpio_mode(RESETGPIO_NORMAL_ALTFUNC
);
186 static inline void pxa_ac97_cold_pxa27x(void)
188 GCR
&= GCR_COLD_RST
; /* clear everything but nCRST */
189 GCR
&= ~GCR_COLD_RST
; /* then assert nCRST */
193 /* PXA27x Developers Manual section 13.5.2.2.1 */
194 clk_enable(ac97conf_clk
);
196 clk_disable(ac97conf_clk
);
203 static inline void pxa_ac97_warm_pxa3xx(void)
209 /* Can't use interrupts */
211 while (!((GSR
| gsr_bits
) & (GSR_PCR
| GSR_SCR
)) && timeout
--)
215 static inline void pxa_ac97_cold_pxa3xx(void)
219 /* Hold CLKBPB for 100us */
225 GCR
&= GCR_COLD_RST
; /* clear everything but nCRST */
226 GCR
&= ~GCR_COLD_RST
; /* then assert nCRST */
230 /* Can't use interrupts on PXA3xx */
231 GCR
&= ~(GCR_PRIRDY_IEN
|GCR_SECRDY_IEN
);
233 GCR
= GCR_WARM_RST
| GCR_COLD_RST
;
234 while (!(GSR
& (GSR_PCR
| GSR_SCR
)) && timeout
--)
239 bool pxa2xx_ac97_try_warm_reset(struct snd_ac97
*ac97
)
245 pxa_ac97_warm_pxa25x();
250 pxa_ac97_warm_pxa27x();
255 pxa_ac97_warm_pxa3xx();
259 gsr
= GSR
| gsr_bits
;
260 if (!(gsr
& (GSR_PCR
| GSR_SCR
))) {
261 printk(KERN_INFO
"%s: warm reset timeout (GSR=%#lx)\n",
269 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset
);
271 bool pxa2xx_ac97_try_cold_reset(struct snd_ac97
*ac97
)
277 pxa_ac97_cold_pxa25x();
282 pxa_ac97_cold_pxa27x();
287 pxa_ac97_cold_pxa3xx();
292 gsr
= GSR
| gsr_bits
;
293 if (!(gsr
& (GSR_PCR
| GSR_SCR
))) {
294 printk(KERN_INFO
"%s: cold reset timeout (GSR=%#lx)\n",
302 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset
);
305 void pxa2xx_ac97_finish_reset(struct snd_ac97
*ac97
)
307 GCR
&= ~(GCR_PRIRDY_IEN
|GCR_SECRDY_IEN
);
308 GCR
|= GCR_SDONE_IE
|GCR_CDONE_IE
;
310 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset
);
312 static irqreturn_t
pxa2xx_ac97_irq(int irq
, void *dev_id
)
322 /* Although we don't use those we still need to clear them
323 since they tend to spuriously trigger when MMC is used
324 (hardware bug? go figure)... */
325 if (cpu_is_pxa27x()) {
338 int pxa2xx_ac97_hw_suspend(void)
340 GCR
|= GCR_ACLINK_OFF
;
341 clk_disable(ac97_clk
);
344 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend
);
346 int pxa2xx_ac97_hw_resume(void)
348 if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
349 pxa_gpio_mode(GPIO31_SYNC_AC97_MD
);
350 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD
);
351 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD
);
352 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD
);
354 if (cpu_is_pxa27x()) {
355 /* Use GPIO 113 or 95 as AC97 Reset on Bulverde */
356 set_resetgpio_mode(RESETGPIO_NORMAL_ALTFUNC
);
358 clk_enable(ac97_clk
);
361 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume
);
364 int __devinit
pxa2xx_ac97_hw_probe(struct platform_device
*dev
)
367 pxa2xx_audio_ops_t
*pdata
= dev
->dev
.platform_data
;
370 switch (pdata
->reset_gpio
) {
373 reset_gpio
= pdata
->reset_gpio
;
381 dev_err(&dev
->dev
, "Invalid reset GPIO %d\n",
389 if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
390 pxa_gpio_mode(GPIO31_SYNC_AC97_MD
);
391 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD
);
392 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD
);
393 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD
);
396 if (cpu_is_pxa27x()) {
397 /* Use GPIO 113 as AC97 Reset on Bulverde */
398 set_resetgpio_mode(RESETGPIO_NORMAL_ALTFUNC
);
399 ac97conf_clk
= clk_get(&dev
->dev
, "AC97CONFCLK");
400 if (IS_ERR(ac97conf_clk
)) {
401 ret
= PTR_ERR(ac97conf_clk
);
407 ac97_clk
= clk_get(&dev
->dev
, "AC97CLK");
408 if (IS_ERR(ac97_clk
)) {
409 ret
= PTR_ERR(ac97_clk
);
414 ret
= clk_enable(ac97_clk
);
418 ret
= request_irq(IRQ_AC97
, pxa2xx_ac97_irq
, IRQF_DISABLED
, "AC97", NULL
);
425 GCR
|= GCR_ACLINK_OFF
;
431 clk_put(ac97conf_clk
);
437 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe
);
439 void pxa2xx_ac97_hw_remove(struct platform_device
*dev
)
441 GCR
|= GCR_ACLINK_OFF
;
442 free_irq(IRQ_AC97
, NULL
);
444 clk_put(ac97conf_clk
);
447 clk_disable(ac97_clk
);
451 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove
);
453 MODULE_AUTHOR("Nicolas Pitre");
454 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
455 MODULE_LICENSE("GPL");