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;
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 u32 __iomem *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 = ac97_reg_base +
61 (slot ? SMC_REG_BASE : PMC_REG_BASE);
63 reg_addr = ac97_reg_base +
64 (slot ? SAC_REG_BASE : PAC_REG_BASE);
65 reg_addr += (reg >> 1);
67 /* start read access across the ac97 link */
68 writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
70 val = (readl(reg_addr) & 0xffff);
71 if (reg == AC97_GPIO_STATUS)
73 if (wait_event_timeout(gsr_wq, (readl(ac97_reg_base + GSR) | gsr_bits) & GSR_SDONE, 1) <= 0 &&
74 !((readl(ac97_reg_base + GSR) | gsr_bits) & GSR_SDONE)) {
75 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
76 __func__, reg, readl(ac97_reg_base + GSR) | gsr_bits);
82 writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
84 val = (readl(reg_addr) & 0xffff);
85 /* but we've just started another cycle... */
86 wait_event_timeout(gsr_wq, (readl(ac97_reg_base + GSR) | gsr_bits) & GSR_SDONE, 1);
88 out: mutex_unlock(&car_mutex);
91 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
93 int pxa2xx_ac97_write(int slot, unsigned short reg, unsigned short val)
95 u32 __iomem *reg_addr;
98 mutex_lock(&car_mutex);
100 /* set up primary or secondary codec space */
101 if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
102 reg_addr = ac97_reg_base +
103 (slot ? SMC_REG_BASE : PMC_REG_BASE);
105 reg_addr = ac97_reg_base +
106 (slot ? SAC_REG_BASE : PAC_REG_BASE);
107 reg_addr += (reg >> 1);
109 writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
111 writel(val, reg_addr);
112 if (wait_event_timeout(gsr_wq, (readl(ac97_reg_base + GSR) | gsr_bits) & GSR_CDONE, 1) <= 0 &&
113 !((readl(ac97_reg_base + GSR) | gsr_bits) & GSR_CDONE)) {
114 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
115 __func__, reg, readl(ac97_reg_base + GSR) | gsr_bits);
119 mutex_unlock(&car_mutex);
122 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
125 static inline void pxa_ac97_warm_pxa25x(void)
129 writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
132 static inline void pxa_ac97_cold_pxa25x(void)
134 writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
135 writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
139 writel(GCR_COLD_RST, ac97_reg_base + GCR);
144 static inline void pxa_ac97_warm_pxa27x(void)
148 /* warm reset broken on Bulverde, so manually keep AC97 reset high */
149 pxa27x_configure_ac97reset(reset_gpio, true);
151 writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
152 pxa27x_configure_ac97reset(reset_gpio, false);
156 static inline void pxa_ac97_cold_pxa27x(void)
158 writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
159 writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
163 /* PXA27x Developers Manual section 13.5.2.2.1 */
164 clk_prepare_enable(ac97conf_clk);
166 clk_disable_unprepare(ac97conf_clk);
167 writel(GCR_COLD_RST | GCR_WARM_RST, ac97_reg_base + GCR);
172 static inline void pxa_ac97_warm_pxa3xx(void)
176 /* Can't use interrupts */
177 writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
180 static inline void pxa_ac97_cold_pxa3xx(void)
182 /* Hold CLKBPB for 100us */
183 writel(0, ac97_reg_base + GCR);
184 writel(GCR_CLKBPB, ac97_reg_base + GCR);
186 writel(0, ac97_reg_base + GCR);
188 writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
189 writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
193 /* Can't use interrupts on PXA3xx */
194 writel(readl(ac97_reg_base + GCR) & (~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN)), ac97_reg_base + GCR);
196 writel(GCR_WARM_RST | GCR_COLD_RST, ac97_reg_base + GCR);
200 bool pxa2xx_ac97_try_warm_reset(void)
203 unsigned int timeout = 100;
207 pxa_ac97_warm_pxa25x();
212 pxa_ac97_warm_pxa27x();
217 pxa_ac97_warm_pxa3xx();
222 while (!((readl(ac97_reg_base + GSR) | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
225 gsr = readl(ac97_reg_base + GSR) | gsr_bits;
226 if (!(gsr & (GSR_PCR | GSR_SCR))) {
227 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
235 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
237 bool pxa2xx_ac97_try_cold_reset(void)
240 unsigned int timeout = 1000;
244 pxa_ac97_cold_pxa25x();
249 pxa_ac97_cold_pxa27x();
254 pxa_ac97_cold_pxa3xx();
259 while (!((readl(ac97_reg_base + GSR) | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
262 gsr = readl(ac97_reg_base + GSR) | gsr_bits;
263 if (!(gsr & (GSR_PCR | GSR_SCR))) {
264 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
272 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
275 void pxa2xx_ac97_finish_reset(void)
277 u32 gcr = readl(ac97_reg_base + GCR);
278 gcr &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
279 gcr |= GCR_SDONE_IE|GCR_CDONE_IE;
280 writel(gcr, ac97_reg_base + GCR);
282 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
284 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
288 status = readl(ac97_reg_base + GSR);
290 writel(status, ac97_reg_base + GSR);
294 /* Although we don't use those we still need to clear them
295 since they tend to spuriously trigger when MMC is used
296 (hardware bug? go figure)... */
297 if (cpu_is_pxa27x()) {
298 writel(MISR_EOC, ac97_reg_base + MISR);
299 writel(PISR_EOC, ac97_reg_base + PISR);
300 writel(MCSR_EOC, ac97_reg_base + MCSR);
310 int pxa2xx_ac97_hw_suspend(void)
312 writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
313 clk_disable_unprepare(ac97_clk);
316 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
318 int pxa2xx_ac97_hw_resume(void)
320 clk_prepare_enable(ac97_clk);
323 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
326 int pxa2xx_ac97_hw_probe(struct platform_device *dev)
330 pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
332 ac97_reg_base = devm_platform_ioremap_resource(dev, 0);
333 if (IS_ERR(ac97_reg_base)) {
334 dev_err(&dev->dev, "Missing MMIO resource\n");
335 return PTR_ERR(ac97_reg_base);
339 switch (pdata->reset_gpio) {
342 reset_gpio = pdata->reset_gpio;
350 dev_err(&dev->dev, "Invalid reset GPIO %d\n",
353 } else if (!pdata && dev->dev.of_node) {
354 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
357 pdata->reset_gpio = of_get_named_gpio(dev->dev.of_node,
359 if (pdata->reset_gpio == -ENOENT)
360 pdata->reset_gpio = -1;
361 else if (pdata->reset_gpio < 0)
362 return pdata->reset_gpio;
363 reset_gpio = pdata->reset_gpio;
369 if (cpu_is_pxa27x()) {
371 * This gpio is needed for a work-around to a bug in the ac97
372 * controller during warm reset. The direction and level is set
373 * here so that it is an output driven high when switching from
374 * AC97_nRESET alt function to generic gpio.
376 ret = gpio_request_one(reset_gpio, GPIOF_OUT_INIT_HIGH,
377 "pxa27x ac97 reset");
379 pr_err("%s: gpio_request_one() failed: %d\n",
383 pxa27x_configure_ac97reset(reset_gpio, false);
385 ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
386 if (IS_ERR(ac97conf_clk)) {
387 ret = PTR_ERR(ac97conf_clk);
393 ac97_clk = clk_get(&dev->dev, "AC97CLK");
394 if (IS_ERR(ac97_clk)) {
395 ret = PTR_ERR(ac97_clk);
400 ret = clk_prepare_enable(ac97_clk);
404 irq = platform_get_irq(dev, 0);
410 ret = request_irq(irq, pxa2xx_ac97_irq, 0, "AC97", NULL);
417 writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
423 clk_put(ac97conf_clk);
429 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
431 void pxa2xx_ac97_hw_remove(struct platform_device *dev)
434 gpio_free(reset_gpio);
435 writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
436 free_irq(platform_get_irq(dev, 0), NULL);
438 clk_put(ac97conf_clk);
441 clk_disable_unprepare(ac97_clk);
445 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
447 u32 pxa2xx_ac97_read_modr(void)
452 return readl(ac97_reg_base + MODR);
454 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read_modr);
456 u32 pxa2xx_ac97_read_misr(void)
461 return readl(ac97_reg_base + MISR);
463 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read_misr);
465 MODULE_AUTHOR("Nicolas Pitre");
466 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
467 MODULE_LICENSE("GPL");