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/pxa2xx-lib.h>
25 #include <mach/irqs.h>
26 #include <mach/regs-ac97.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
;
34 static int reset_gpio
;
36 extern void pxa27x_configure_ac97reset(int reset_gpio
, bool to_gpio
);
41 * o Slot 12 read from modem space will hang controller.
42 * o CDONE, SDONE interrupt fails after any slot 12 IO.
44 * We therefore have an hybrid approach for waiting on SDONE (interrupt or
45 * 1 jiffy timeout if interrupt never comes).
48 int pxa2xx_ac97_read(int slot
, unsigned short reg
)
51 volatile u32
*reg_addr
;
56 mutex_lock(&car_mutex
);
58 /* set up primary or secondary codec space */
59 if (cpu_is_pxa25x() && reg
== AC97_GPIO_STATUS
)
60 reg_addr
= slot
? &SMC_REG_BASE
: &PMC_REG_BASE
;
62 reg_addr
= slot
? &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
;
68 val
= (*reg_addr
& 0xffff);
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
;
82 val
= (*reg_addr
& 0xffff);
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 int pxa2xx_ac97_write(int slot
, unsigned short reg
, unsigned short val
)
93 volatile u32
*reg_addr
;
96 mutex_lock(&car_mutex
);
98 /* set up primary or secondary codec space */
99 if (cpu_is_pxa25x() && reg
== AC97_GPIO_STATUS
)
100 reg_addr
= slot
? &SMC_REG_BASE
: &PMC_REG_BASE
;
102 reg_addr
= slot
? &SAC_REG_BASE
: &PAC_REG_BASE
;
103 reg_addr
+= (reg
>> 1);
105 GSR
= GSR_CDONE
| GSR_SDONE
;
108 if (wait_event_timeout(gsr_wq
, (GSR
| gsr_bits
) & GSR_CDONE
, 1) <= 0 &&
109 !((GSR
| gsr_bits
) & GSR_CDONE
)) {
110 printk(KERN_ERR
"%s: write error (ac97_reg=%d GSR=%#lx)\n",
111 __func__
, reg
, GSR
| gsr_bits
);
115 mutex_unlock(&car_mutex
);
118 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write
);
121 static inline void pxa_ac97_warm_pxa25x(void)
128 static inline void pxa_ac97_cold_pxa25x(void)
130 GCR
&= GCR_COLD_RST
; /* clear everything but nCRST */
131 GCR
&= ~GCR_COLD_RST
; /* then assert nCRST */
140 static inline void pxa_ac97_warm_pxa27x(void)
144 /* warm reset broken on Bulverde, so manually keep AC97 reset high */
145 pxa27x_configure_ac97reset(reset_gpio
, true);
148 pxa27x_configure_ac97reset(reset_gpio
, false);
152 static inline void pxa_ac97_cold_pxa27x(void)
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_prepare_enable(ac97conf_clk
);
162 clk_disable_unprepare(ac97conf_clk
);
163 GCR
= GCR_COLD_RST
| GCR_WARM_RST
;
168 static inline void pxa_ac97_warm_pxa3xx(void)
172 /* Can't use interrupts */
176 static inline void pxa_ac97_cold_pxa3xx(void)
178 /* Hold CLKBPB for 100us */
184 GCR
&= GCR_COLD_RST
; /* clear everything but nCRST */
185 GCR
&= ~GCR_COLD_RST
; /* then assert nCRST */
189 /* Can't use interrupts on PXA3xx */
190 GCR
&= ~(GCR_PRIRDY_IEN
|GCR_SECRDY_IEN
);
192 GCR
= GCR_WARM_RST
| GCR_COLD_RST
;
196 bool pxa2xx_ac97_try_warm_reset(void)
199 unsigned int timeout
= 100;
203 pxa_ac97_warm_pxa25x();
208 pxa_ac97_warm_pxa27x();
213 pxa_ac97_warm_pxa3xx();
218 while (!((GSR
| gsr_bits
) & (GSR_PCR
| GSR_SCR
)) && timeout
--)
221 gsr
= GSR
| gsr_bits
;
222 if (!(gsr
& (GSR_PCR
| GSR_SCR
))) {
223 printk(KERN_INFO
"%s: warm reset timeout (GSR=%#lx)\n",
231 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset
);
233 bool pxa2xx_ac97_try_cold_reset(void)
236 unsigned int timeout
= 1000;
240 pxa_ac97_cold_pxa25x();
245 pxa_ac97_cold_pxa27x();
250 pxa_ac97_cold_pxa3xx();
255 while (!((GSR
| gsr_bits
) & (GSR_PCR
| GSR_SCR
)) && timeout
--)
258 gsr
= GSR
| gsr_bits
;
259 if (!(gsr
& (GSR_PCR
| GSR_SCR
))) {
260 printk(KERN_INFO
"%s: cold reset timeout (GSR=%#lx)\n",
268 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset
);
271 void pxa2xx_ac97_finish_reset(void)
273 GCR
&= ~(GCR_PRIRDY_IEN
|GCR_SECRDY_IEN
);
274 GCR
|= GCR_SDONE_IE
|GCR_CDONE_IE
;
276 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset
);
278 static irqreturn_t
pxa2xx_ac97_irq(int irq
, void *dev_id
)
288 /* Although we don't use those we still need to clear them
289 since they tend to spuriously trigger when MMC is used
290 (hardware bug? go figure)... */
291 if (cpu_is_pxa27x()) {
304 int pxa2xx_ac97_hw_suspend(void)
306 GCR
|= GCR_ACLINK_OFF
;
307 clk_disable_unprepare(ac97_clk
);
310 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend
);
312 int pxa2xx_ac97_hw_resume(void)
314 clk_prepare_enable(ac97_clk
);
317 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume
);
320 int pxa2xx_ac97_hw_probe(struct platform_device
*dev
)
323 pxa2xx_audio_ops_t
*pdata
= dev
->dev
.platform_data
;
326 switch (pdata
->reset_gpio
) {
329 reset_gpio
= pdata
->reset_gpio
;
337 dev_err(&dev
->dev
, "Invalid reset GPIO %d\n",
345 if (cpu_is_pxa27x()) {
347 * This gpio is needed for a work-around to a bug in the ac97
348 * controller during warm reset. The direction and level is set
349 * here so that it is an output driven high when switching from
350 * AC97_nRESET alt function to generic gpio.
352 ret
= gpio_request_one(reset_gpio
, GPIOF_OUT_INIT_HIGH
,
353 "pxa27x ac97 reset");
355 pr_err("%s: gpio_request_one() failed: %d\n",
359 pxa27x_configure_ac97reset(reset_gpio
, false);
361 ac97conf_clk
= clk_get(&dev
->dev
, "AC97CONFCLK");
362 if (IS_ERR(ac97conf_clk
)) {
363 ret
= PTR_ERR(ac97conf_clk
);
369 ac97_clk
= clk_get(&dev
->dev
, "AC97CLK");
370 if (IS_ERR(ac97_clk
)) {
371 ret
= PTR_ERR(ac97_clk
);
376 ret
= clk_prepare_enable(ac97_clk
);
380 ret
= request_irq(IRQ_AC97
, pxa2xx_ac97_irq
, 0, "AC97", NULL
);
387 GCR
|= GCR_ACLINK_OFF
;
393 clk_put(ac97conf_clk
);
399 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe
);
401 void pxa2xx_ac97_hw_remove(struct platform_device
*dev
)
404 gpio_free(reset_gpio
);
405 GCR
|= GCR_ACLINK_OFF
;
406 free_irq(IRQ_AC97
, NULL
);
408 clk_put(ac97conf_clk
);
411 clk_disable_unprepare(ac97_clk
);
415 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove
);
417 MODULE_AUTHOR("Nicolas Pitre");
418 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
419 MODULE_LICENSE("GPL");