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>
20 #include <linux/soc/pxa/cpu.h>
22 #include <sound/pxa2xx-lib.h>
24 #include <linux/platform_data/asoc-pxa.h>
26 #include "pxa2xx-ac97-regs.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;
34 static void __iomem *ac97_reg_base;
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 u32 __iomem *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 = ac97_reg_base +
59 (slot ? SMC_REG_BASE : PMC_REG_BASE);
61 reg_addr = ac97_reg_base +
62 (slot ? SAC_REG_BASE : PAC_REG_BASE);
63 reg_addr += (reg >> 1);
65 /* start read access across the ac97 link */
66 writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
68 val = (readl(reg_addr) & 0xffff);
69 if (reg == AC97_GPIO_STATUS)
71 if (wait_event_timeout(gsr_wq, (readl(ac97_reg_base + GSR) | gsr_bits) & GSR_SDONE, 1) <= 0 &&
72 !((readl(ac97_reg_base + GSR) | gsr_bits) & GSR_SDONE)) {
73 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
74 __func__, reg, readl(ac97_reg_base + GSR) | gsr_bits);
80 writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
82 val = (readl(reg_addr) & 0xffff);
83 /* but we've just started another cycle... */
84 wait_event_timeout(gsr_wq, (readl(ac97_reg_base + 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 u32 __iomem *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 = ac97_reg_base +
101 (slot ? SMC_REG_BASE : PMC_REG_BASE);
103 reg_addr = ac97_reg_base +
104 (slot ? SAC_REG_BASE : PAC_REG_BASE);
105 reg_addr += (reg >> 1);
107 writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
109 writel(val, reg_addr);
110 if (wait_event_timeout(gsr_wq, (readl(ac97_reg_base + GSR) | gsr_bits) & GSR_CDONE, 1) <= 0 &&
111 !((readl(ac97_reg_base + GSR) | gsr_bits) & GSR_CDONE)) {
112 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
113 __func__, reg, readl(ac97_reg_base + GSR) | gsr_bits);
117 mutex_unlock(&car_mutex);
120 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
123 static inline void pxa_ac97_warm_pxa25x(void)
127 writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
130 static inline void pxa_ac97_cold_pxa25x(void)
132 writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
133 writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
137 writel(GCR_COLD_RST, ac97_reg_base + GCR);
142 static inline void pxa_ac97_warm_pxa27x(void)
146 /* warm reset broken on Bulverde, so manually keep AC97 reset high */
147 pxa27x_configure_ac97reset(reset_gpio, true);
149 writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
150 pxa27x_configure_ac97reset(reset_gpio, false);
154 static inline void pxa_ac97_cold_pxa27x(void)
156 writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
157 writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
161 /* PXA27x Developers Manual section 13.5.2.2.1 */
162 clk_prepare_enable(ac97conf_clk);
164 clk_disable_unprepare(ac97conf_clk);
165 writel(GCR_COLD_RST | GCR_WARM_RST, ac97_reg_base + GCR);
170 static inline void pxa_ac97_warm_pxa3xx(void)
174 /* Can't use interrupts */
175 writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
178 static inline void pxa_ac97_cold_pxa3xx(void)
180 /* Hold CLKBPB for 100us */
181 writel(0, ac97_reg_base + GCR);
182 writel(GCR_CLKBPB, ac97_reg_base + GCR);
184 writel(0, ac97_reg_base + GCR);
186 writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
187 writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
191 /* Can't use interrupts on PXA3xx */
192 writel(readl(ac97_reg_base + GCR) & (~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN)), ac97_reg_base + GCR);
194 writel(GCR_WARM_RST | GCR_COLD_RST, ac97_reg_base + GCR);
198 bool pxa2xx_ac97_try_warm_reset(void)
201 unsigned int timeout = 100;
205 pxa_ac97_warm_pxa25x();
210 pxa_ac97_warm_pxa27x();
215 pxa_ac97_warm_pxa3xx();
220 while (!((readl(ac97_reg_base + GSR) | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
223 gsr = readl(ac97_reg_base + GSR) | gsr_bits;
224 if (!(gsr & (GSR_PCR | GSR_SCR))) {
225 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
233 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
235 bool pxa2xx_ac97_try_cold_reset(void)
238 unsigned int timeout = 1000;
242 pxa_ac97_cold_pxa25x();
247 pxa_ac97_cold_pxa27x();
252 pxa_ac97_cold_pxa3xx();
257 while (!((readl(ac97_reg_base + GSR) | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
260 gsr = readl(ac97_reg_base + GSR) | gsr_bits;
261 if (!(gsr & (GSR_PCR | GSR_SCR))) {
262 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
270 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
273 void pxa2xx_ac97_finish_reset(void)
275 u32 gcr = readl(ac97_reg_base + GCR);
276 gcr &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
277 gcr |= GCR_SDONE_IE|GCR_CDONE_IE;
278 writel(gcr, ac97_reg_base + GCR);
280 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
282 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
286 status = readl(ac97_reg_base + GSR);
288 writel(status, ac97_reg_base + GSR);
292 /* Although we don't use those we still need to clear them
293 since they tend to spuriously trigger when MMC is used
294 (hardware bug? go figure)... */
295 if (cpu_is_pxa27x()) {
296 writel(MISR_EOC, ac97_reg_base + MISR);
297 writel(PISR_EOC, ac97_reg_base + PISR);
298 writel(MCSR_EOC, ac97_reg_base + MCSR);
308 int pxa2xx_ac97_hw_suspend(void)
310 writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
311 clk_disable_unprepare(ac97_clk);
314 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
316 int pxa2xx_ac97_hw_resume(void)
318 clk_prepare_enable(ac97_clk);
321 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
324 int pxa2xx_ac97_hw_probe(struct platform_device *dev)
328 pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
330 ac97_reg_base = devm_platform_ioremap_resource(dev, 0);
331 if (IS_ERR(ac97_reg_base)) {
332 dev_err(&dev->dev, "Missing MMIO resource\n");
333 return PTR_ERR(ac97_reg_base);
337 switch (pdata->reset_gpio) {
340 reset_gpio = pdata->reset_gpio;
348 dev_err(&dev->dev, "Invalid reset GPIO %d\n",
351 } else if (!pdata && dev->dev.of_node) {
352 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
355 pdata->reset_gpio = of_get_named_gpio(dev->dev.of_node,
357 if (pdata->reset_gpio == -ENOENT)
358 pdata->reset_gpio = -1;
359 else if (pdata->reset_gpio < 0)
360 return pdata->reset_gpio;
361 reset_gpio = pdata->reset_gpio;
367 if (cpu_is_pxa27x()) {
369 * This gpio is needed for a work-around to a bug in the ac97
370 * controller during warm reset. The direction and level is set
371 * here so that it is an output driven high when switching from
372 * AC97_nRESET alt function to generic gpio.
374 ret = gpio_request_one(reset_gpio, GPIOF_OUT_INIT_HIGH,
375 "pxa27x ac97 reset");
377 pr_err("%s: gpio_request_one() failed: %d\n",
381 pxa27x_configure_ac97reset(reset_gpio, false);
383 ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
384 if (IS_ERR(ac97conf_clk)) {
385 ret = PTR_ERR(ac97conf_clk);
391 ac97_clk = clk_get(&dev->dev, "AC97CLK");
392 if (IS_ERR(ac97_clk)) {
393 ret = PTR_ERR(ac97_clk);
398 ret = clk_prepare_enable(ac97_clk);
402 irq = platform_get_irq(dev, 0);
408 ret = request_irq(irq, pxa2xx_ac97_irq, 0, "AC97", NULL);
415 writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
421 clk_put(ac97conf_clk);
427 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
429 void pxa2xx_ac97_hw_remove(struct platform_device *dev)
432 gpio_free(reset_gpio);
433 writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
434 free_irq(platform_get_irq(dev, 0), NULL);
436 clk_put(ac97conf_clk);
439 clk_disable_unprepare(ac97_clk);
443 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
445 u32 pxa2xx_ac97_read_modr(void)
450 return readl(ac97_reg_base + MODR);
452 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read_modr);
454 u32 pxa2xx_ac97_read_misr(void)
459 return readl(ac97_reg_base + MISR);
461 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read_misr);
463 MODULE_AUTHOR("Nicolas Pitre");
464 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
465 MODULE_LICENSE("GPL");