1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on sound/arm/pxa2xx-ac97.c and sound/soc/pxa/pxa2xx-ac97.c
6 * Author: Nicolas Pitre
7 * Created: Dec 02, 2004
8 * Copyright: MontaVista Software Inc.
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/module.h>
18 #include <linux/gpio.h>
19 #include <linux/of_gpio.h>
21 #include <sound/pxa2xx-lib.h>
23 #include <mach/irqs.h>
24 #include <mach/regs-ac97.h>
25 #include <mach/audio.h>
27 static DEFINE_MUTEX(car_mutex);
28 static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
29 static volatile long gsr_bits;
30 static struct clk *ac97_clk;
31 static struct clk *ac97conf_clk;
32 static int reset_gpio;
34 extern void pxa27x_configure_ac97reset(int reset_gpio, bool to_gpio);
39 * o Slot 12 read from modem space will hang controller.
40 * o CDONE, SDONE interrupt fails after any slot 12 IO.
42 * We therefore have an hybrid approach for waiting on SDONE (interrupt or
43 * 1 jiffy timeout if interrupt never comes).
46 int pxa2xx_ac97_read(int slot, unsigned short reg)
49 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 = slot ? &SMC_REG_BASE : &PMC_REG_BASE;
60 reg_addr = slot ? &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;
66 val = (*reg_addr & 0xffff);
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;
80 val = (*reg_addr & 0xffff);
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 int pxa2xx_ac97_write(int slot, unsigned short reg, unsigned short val)
91 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 = slot ? &SMC_REG_BASE : &PMC_REG_BASE;
100 reg_addr = slot ? &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);
113 mutex_unlock(&car_mutex);
116 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
119 static inline void pxa_ac97_warm_pxa25x(void)
126 static inline void pxa_ac97_cold_pxa25x(void)
128 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
129 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
138 static inline void pxa_ac97_warm_pxa27x(void)
142 /* warm reset broken on Bulverde, so manually keep AC97 reset high */
143 pxa27x_configure_ac97reset(reset_gpio, true);
146 pxa27x_configure_ac97reset(reset_gpio, false);
150 static inline void pxa_ac97_cold_pxa27x(void)
152 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
153 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
157 /* PXA27x Developers Manual section 13.5.2.2.1 */
158 clk_prepare_enable(ac97conf_clk);
160 clk_disable_unprepare(ac97conf_clk);
161 GCR = GCR_COLD_RST | GCR_WARM_RST;
166 static inline void pxa_ac97_warm_pxa3xx(void)
170 /* Can't use interrupts */
174 static inline void pxa_ac97_cold_pxa3xx(void)
176 /* Hold CLKBPB for 100us */
182 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
183 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
187 /* Can't use interrupts on PXA3xx */
188 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
190 GCR = GCR_WARM_RST | GCR_COLD_RST;
194 bool pxa2xx_ac97_try_warm_reset(void)
197 unsigned int timeout = 100;
201 pxa_ac97_warm_pxa25x();
206 pxa_ac97_warm_pxa27x();
211 pxa_ac97_warm_pxa3xx();
216 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
219 gsr = GSR | gsr_bits;
220 if (!(gsr & (GSR_PCR | GSR_SCR))) {
221 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
229 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
231 bool pxa2xx_ac97_try_cold_reset(void)
234 unsigned int timeout = 1000;
238 pxa_ac97_cold_pxa25x();
243 pxa_ac97_cold_pxa27x();
248 pxa_ac97_cold_pxa3xx();
253 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
256 gsr = GSR | gsr_bits;
257 if (!(gsr & (GSR_PCR | GSR_SCR))) {
258 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
266 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
269 void pxa2xx_ac97_finish_reset(void)
271 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
272 GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
274 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
276 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
286 /* Although we don't use those we still need to clear them
287 since they tend to spuriously trigger when MMC is used
288 (hardware bug? go figure)... */
289 if (cpu_is_pxa27x()) {
302 int pxa2xx_ac97_hw_suspend(void)
304 GCR |= GCR_ACLINK_OFF;
305 clk_disable_unprepare(ac97_clk);
308 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
310 int pxa2xx_ac97_hw_resume(void)
312 clk_prepare_enable(ac97_clk);
315 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
318 int pxa2xx_ac97_hw_probe(struct platform_device *dev)
321 pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
324 switch (pdata->reset_gpio) {
327 reset_gpio = pdata->reset_gpio;
335 dev_err(&dev->dev, "Invalid reset GPIO %d\n",
338 } else if (!pdata && dev->dev.of_node) {
339 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
342 pdata->reset_gpio = of_get_named_gpio(dev->dev.of_node,
344 if (pdata->reset_gpio == -ENOENT)
345 pdata->reset_gpio = -1;
346 else if (pdata->reset_gpio < 0)
347 return pdata->reset_gpio;
348 reset_gpio = pdata->reset_gpio;
354 if (cpu_is_pxa27x()) {
356 * This gpio is needed for a work-around to a bug in the ac97
357 * controller during warm reset. The direction and level is set
358 * here so that it is an output driven high when switching from
359 * AC97_nRESET alt function to generic gpio.
361 ret = gpio_request_one(reset_gpio, GPIOF_OUT_INIT_HIGH,
362 "pxa27x ac97 reset");
364 pr_err("%s: gpio_request_one() failed: %d\n",
368 pxa27x_configure_ac97reset(reset_gpio, false);
370 ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
371 if (IS_ERR(ac97conf_clk)) {
372 ret = PTR_ERR(ac97conf_clk);
378 ac97_clk = clk_get(&dev->dev, "AC97CLK");
379 if (IS_ERR(ac97_clk)) {
380 ret = PTR_ERR(ac97_clk);
385 ret = clk_prepare_enable(ac97_clk);
389 ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
396 GCR |= GCR_ACLINK_OFF;
402 clk_put(ac97conf_clk);
408 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
410 void pxa2xx_ac97_hw_remove(struct platform_device *dev)
413 gpio_free(reset_gpio);
414 GCR |= GCR_ACLINK_OFF;
415 free_irq(IRQ_AC97, NULL);
417 clk_put(ac97conf_clk);
420 clk_disable_unprepare(ac97_clk);
424 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
426 MODULE_AUTHOR("Nicolas Pitre");
427 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
428 MODULE_LICENSE("GPL");