blob: 1ecd0a66ecd37384228caaedf27a928eb4cf0d04 [file] [log] [blame]
Dmitry Baryshkov9c636342008-09-10 05:01:17 +04001/*
2 * Based on sound/arm/pxa2xx-ac97.c and sound/soc/pxa/pxa2xx-ac97.c
3 * which contain:
4 *
5 * Author: Nicolas Pitre
6 * Created: Dec 02, 2004
7 * Copyright: MontaVista Software Inc.
8 *
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.
12 */
13
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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040019#include <linux/module.h>
Rob Herring23019a72012-03-20 14:33:19 -050020#include <linux/io.h>
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040021
22#include <sound/ac97_codec.h>
23#include <sound/pxa2xx-lib.h>
24
Rob Herring9482ee72012-01-03 17:10:17 -060025#include <mach/irqs.h>
Eric Miao1f017a92008-11-28 14:19:33 +080026#include <mach/regs-ac97.h>
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040027#include <mach/audio.h>
28
29static DEFINE_MUTEX(car_mutex);
30static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
31static volatile long gsr_bits;
32static struct clk *ac97_clk;
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040033static struct clk *ac97conf_clk;
Robert Jarzmik26ade892009-03-15 14:10:54 +010034static int reset_gpio;
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040035
Eric Miaofb1bf8c2010-01-04 16:30:58 +080036extern void pxa27x_assert_ac97reset(int reset_gpio, int on);
37
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040038/*
39 * Beware PXA27x bugs:
40 *
41 * o Slot 12 read from modem space will hang controller.
42 * o CDONE, SDONE interrupt fails after any slot 12 IO.
43 *
44 * We therefore have an hybrid approach for waiting on SDONE (interrupt or
45 * 1 jiffy timeout if interrupt never comes).
46 */
47
48unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
49{
50 unsigned short val = -1;
51 volatile u32 *reg_addr;
52
53 mutex_lock(&car_mutex);
54
55 /* set up primary or secondary codec space */
Marc Zyngier8825e8e2008-10-14 09:57:05 +010056 if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040057 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
58 else
59 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040060 reg_addr += (reg >> 1);
61
62 /* start read access across the ac97 link */
63 GSR = GSR_CDONE | GSR_SDONE;
64 gsr_bits = 0;
65 val = *reg_addr;
66 if (reg == AC97_GPIO_STATUS)
67 goto out;
68 if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
69 !((GSR | gsr_bits) & GSR_SDONE)) {
70 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
71 __func__, reg, GSR | gsr_bits);
72 val = -1;
73 goto out;
74 }
75
76 /* valid data now */
77 GSR = GSR_CDONE | GSR_SDONE;
78 gsr_bits = 0;
79 val = *reg_addr;
80 /* but we've just started another cycle... */
81 wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
82
83out: mutex_unlock(&car_mutex);
84 return val;
85}
86EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
87
88void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
89 unsigned short val)
90{
91 volatile u32 *reg_addr;
92
93 mutex_lock(&car_mutex);
94
95 /* set up primary or secondary codec space */
Marc Zyngier8825e8e2008-10-14 09:57:05 +010096 if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040097 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
98 else
99 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400100 reg_addr += (reg >> 1);
101
102 GSR = GSR_CDONE | GSR_SDONE;
103 gsr_bits = 0;
104 *reg_addr = val;
105 if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
106 !((GSR | gsr_bits) & GSR_CDONE))
107 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
108 __func__, reg, GSR | gsr_bits);
109
110 mutex_unlock(&car_mutex);
111}
112EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
113
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400114#ifdef CONFIG_PXA25x
115static inline void pxa_ac97_warm_pxa25x(void)
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400116{
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400117 gsr_bits = 0;
118
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400119 GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
120 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
121}
122
123static inline void pxa_ac97_cold_pxa25x(void)
124{
125 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
126 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
127
128 gsr_bits = 0;
129
130 GCR = GCR_COLD_RST;
131 GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
132 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
133}
134#endif
135
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400136#ifdef CONFIG_PXA27x
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400137static inline void pxa_ac97_warm_pxa27x(void)
138{
139 gsr_bits = 0;
140
Eric Miaofb1bf8c2010-01-04 16:30:58 +0800141 /* warm reset broken on Bulverde, so manually keep AC97 reset high */
142 pxa27x_assert_ac97reset(reset_gpio, 1);
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400143 udelay(10);
144 GCR |= GCR_WARM_RST;
Eric Miaofb1bf8c2010-01-04 16:30:58 +0800145 pxa27x_assert_ac97reset(reset_gpio, 0);
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400146 udelay(500);
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400147}
148
149static inline void pxa_ac97_cold_pxa27x(void)
150{
Mike Dunn41b645c2013-01-07 13:55:12 -0800151 unsigned int timeout;
152
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400153 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
154 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
155
156 gsr_bits = 0;
157
158 /* PXA27x Developers Manual section 13.5.2.2.1 */
159 clk_enable(ac97conf_clk);
160 udelay(5);
161 clk_disable(ac97conf_clk);
Mike Dunn41b645c2013-01-07 13:55:12 -0800162 GCR = GCR_COLD_RST | GCR_WARM_RST;
163 timeout = 100; /* wait for the codec-ready bit to be set */
164 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
165 mdelay(1);
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400166}
167#endif
168
169#ifdef CONFIG_PXA3xx
170static inline void pxa_ac97_warm_pxa3xx(void)
171{
172 int timeout = 100;
173
174 gsr_bits = 0;
175
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400176 /* Can't use interrupts */
177 GCR |= GCR_WARM_RST;
178 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
179 mdelay(1);
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400180}
181
182static inline void pxa_ac97_cold_pxa3xx(void)
183{
184 int timeout = 1000;
185
186 /* Hold CLKBPB for 100us */
187 GCR = 0;
188 GCR = GCR_CLKBPB;
189 udelay(100);
190 GCR = 0;
191
192 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
193 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
194
195 gsr_bits = 0;
196
197 /* Can't use interrupts on PXA3xx */
198 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
199
200 GCR = GCR_WARM_RST | GCR_COLD_RST;
201 while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
202 mdelay(10);
203}
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400204#endif
205
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400206bool pxa2xx_ac97_try_warm_reset(struct snd_ac97 *ac97)
207{
Luotao Fu057de502009-03-26 13:18:03 +0100208 unsigned long gsr;
209
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400210#ifdef CONFIG_PXA25x
Marc Zyngier8825e8e2008-10-14 09:57:05 +0100211 if (cpu_is_pxa25x())
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400212 pxa_ac97_warm_pxa25x();
213 else
214#endif
215#ifdef CONFIG_PXA27x
216 if (cpu_is_pxa27x())
217 pxa_ac97_warm_pxa27x();
218 else
219#endif
220#ifdef CONFIG_PXA3xx
221 if (cpu_is_pxa3xx())
222 pxa_ac97_warm_pxa3xx();
223 else
224#endif
225 BUG();
Luotao Fu057de502009-03-26 13:18:03 +0100226 gsr = GSR | gsr_bits;
227 if (!(gsr & (GSR_PCR | GSR_SCR))) {
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400228 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
Luotao Fu057de502009-03-26 13:18:03 +0100229 __func__, gsr);
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400230
231 return false;
232 }
233
234 return true;
235}
236EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
237
238bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
239{
Luotao Fu057de502009-03-26 13:18:03 +0100240 unsigned long gsr;
241
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400242#ifdef CONFIG_PXA25x
Marc Zyngier8825e8e2008-10-14 09:57:05 +0100243 if (cpu_is_pxa25x())
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400244 pxa_ac97_cold_pxa25x();
245 else
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400246#endif
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400247#ifdef CONFIG_PXA27x
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400248 if (cpu_is_pxa27x())
249 pxa_ac97_cold_pxa27x();
250 else
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400251#endif
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400252#ifdef CONFIG_PXA3xx
253 if (cpu_is_pxa3xx())
254 pxa_ac97_cold_pxa3xx();
255 else
256#endif
257 BUG();
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400258
Luotao Fu057de502009-03-26 13:18:03 +0100259 gsr = GSR | gsr_bits;
260 if (!(gsr & (GSR_PCR | GSR_SCR))) {
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400261 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
Luotao Fu057de502009-03-26 13:18:03 +0100262 __func__, gsr);
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400263
264 return false;
265 }
266
267 return true;
268}
269EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
270
271
272void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
273{
274 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
275 GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
276}
277EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
278
279static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
280{
281 long status;
282
283 status = GSR;
284 if (status) {
285 GSR = status;
286 gsr_bits |= status;
287 wake_up(&gsr_wq);
288
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400289 /* Although we don't use those we still need to clear them
290 since they tend to spuriously trigger when MMC is used
291 (hardware bug? go figure)... */
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400292 if (cpu_is_pxa27x()) {
293 MISR = MISR_EOC;
294 PISR = PISR_EOC;
295 MCSR = MCSR_EOC;
296 }
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400297
298 return IRQ_HANDLED;
299 }
300
301 return IRQ_NONE;
302}
303
304#ifdef CONFIG_PM
305int pxa2xx_ac97_hw_suspend(void)
306{
307 GCR |= GCR_ACLINK_OFF;
308 clk_disable(ac97_clk);
309 return 0;
310}
311EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
312
313int pxa2xx_ac97_hw_resume(void)
314{
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400315 clk_enable(ac97_clk);
316 return 0;
317}
318EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
319#endif
320
Bill Pembertone21596b2012-12-06 12:35:12 -0500321int pxa2xx_ac97_hw_probe(struct platform_device *dev)
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400322{
323 int ret;
Mark Browneae17752009-04-13 11:48:03 +0100324 pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
Robert Jarzmik26ade892009-03-15 14:10:54 +0100325
326 if (pdata) {
327 switch (pdata->reset_gpio) {
328 case 95:
329 case 113:
330 reset_gpio = pdata->reset_gpio;
331 break;
332 case 0:
333 reset_gpio = 113;
334 break;
335 case -1:
336 break;
337 default:
Takashi Iwai1f218692009-03-19 14:08:58 +0100338 dev_err(&dev->dev, "Invalid reset GPIO %d\n",
Robert Jarzmik26ade892009-03-15 14:10:54 +0100339 pdata->reset_gpio);
340 }
341 } else {
342 if (cpu_is_pxa27x())
343 reset_gpio = 113;
344 }
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400345
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400346 if (cpu_is_pxa27x()) {
347 /* Use GPIO 113 as AC97 Reset on Bulverde */
Eric Miaofb1bf8c2010-01-04 16:30:58 +0800348 pxa27x_assert_ac97reset(reset_gpio, 0);
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400349 ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
350 if (IS_ERR(ac97conf_clk)) {
351 ret = PTR_ERR(ac97conf_clk);
352 ac97conf_clk = NULL;
Dmitry Baryshkov79612332009-01-05 12:58:06 +0300353 goto err_conf;
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400354 }
355 }
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400356
357 ac97_clk = clk_get(&dev->dev, "AC97CLK");
358 if (IS_ERR(ac97_clk)) {
359 ret = PTR_ERR(ac97_clk);
360 ac97_clk = NULL;
Dmitry Baryshkov79612332009-01-05 12:58:06 +0300361 goto err_clk;
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400362 }
363
Dmitry Baryshkov79612332009-01-05 12:58:06 +0300364 ret = clk_enable(ac97_clk);
365 if (ret)
366 goto err_clk2;
367
Yong Zhang88e24c32011-09-22 16:59:20 +0800368 ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
Dmitry Baryshkov79612332009-01-05 12:58:06 +0300369 if (ret < 0)
370 goto err_irq;
371
372 return 0;
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400373
374err_irq:
375 GCR |= GCR_ACLINK_OFF;
Dmitry Baryshkov79612332009-01-05 12:58:06 +0300376err_clk2:
377 clk_put(ac97_clk);
378 ac97_clk = NULL;
379err_clk:
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400380 if (ac97conf_clk) {
381 clk_put(ac97conf_clk);
382 ac97conf_clk = NULL;
383 }
Dmitry Baryshkov79612332009-01-05 12:58:06 +0300384err_conf:
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400385 return ret;
386}
387EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
388
389void pxa2xx_ac97_hw_remove(struct platform_device *dev)
390{
391 GCR |= GCR_ACLINK_OFF;
392 free_irq(IRQ_AC97, NULL);
Dmitry Baryshkov9d1cf392008-09-10 05:01:18 +0400393 if (ac97conf_clk) {
394 clk_put(ac97conf_clk);
395 ac97conf_clk = NULL;
396 }
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400397 clk_disable(ac97_clk);
398 clk_put(ac97_clk);
399 ac97_clk = NULL;
400}
401EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
402
403MODULE_AUTHOR("Nicolas Pitre");
404MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
405MODULE_LICENSE("GPL");
406