Merge branch 'for-rmk' of git://gitorious.org/linux-gemini/mainline into devel
[pandora-kernel.git] / sound / arm / pxa2xx-ac97-lib.c
1 /*
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>
19
20 #include <sound/ac97_codec.h>
21 #include <sound/pxa2xx-lib.h>
22
23 #include <asm/irq.h>
24 #include <mach/regs-ac97.h>
25 #include <mach/pxa2xx-gpio.h>
26 #include <mach/audio.h>
27
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
34 /*
35  * Beware PXA27x bugs:
36  *
37  *   o Slot 12 read from modem space will hang controller.
38  *   o CDONE, SDONE interrupt fails after any slot 12 IO.
39  *
40  * We therefore have an hybrid approach for waiting on SDONE (interrupt or
41  * 1 jiffy timeout if interrupt never comes).
42  */
43
44 unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
45 {
46         unsigned short val = -1;
47         volatile u32 *reg_addr;
48
49         mutex_lock(&car_mutex);
50
51         /* set up primary or secondary codec space */
52         if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
53                 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
54         else
55                 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
56         reg_addr += (reg >> 1);
57
58         /* start read access across the ac97 link */
59         GSR = GSR_CDONE | GSR_SDONE;
60         gsr_bits = 0;
61         val = *reg_addr;
62         if (reg == AC97_GPIO_STATUS)
63                 goto out;
64         if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
65             !((GSR | gsr_bits) & GSR_SDONE)) {
66                 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
67                                 __func__, reg, GSR | gsr_bits);
68                 val = -1;
69                 goto out;
70         }
71
72         /* valid data now */
73         GSR = GSR_CDONE | GSR_SDONE;
74         gsr_bits = 0;
75         val = *reg_addr;
76         /* but we've just started another cycle... */
77         wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
78
79 out:    mutex_unlock(&car_mutex);
80         return val;
81 }
82 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
83
84 void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
85                         unsigned short val)
86 {
87         volatile u32 *reg_addr;
88
89         mutex_lock(&car_mutex);
90
91         /* set up primary or secondary codec space */
92         if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
93                 reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
94         else
95                 reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
96         reg_addr += (reg >> 1);
97
98         GSR = GSR_CDONE | GSR_SDONE;
99         gsr_bits = 0;
100         *reg_addr = val;
101         if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
102             !((GSR | gsr_bits) & GSR_CDONE))
103                 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
104                                 __func__, reg, GSR | gsr_bits);
105
106         mutex_unlock(&car_mutex);
107 }
108 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
109
110 #ifdef CONFIG_PXA25x
111 static inline void pxa_ac97_warm_pxa25x(void)
112 {
113         gsr_bits = 0;
114
115         GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
116         wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
117 }
118
119 static inline void pxa_ac97_cold_pxa25x(void)
120 {
121         GCR &=  GCR_COLD_RST;  /* clear everything but nCRST */
122         GCR &= ~GCR_COLD_RST;  /* then assert nCRST */
123
124         gsr_bits = 0;
125
126         GCR = GCR_COLD_RST;
127         GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
128         wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
129 }
130 #endif
131
132 #ifdef CONFIG_PXA27x
133 static inline void pxa_ac97_warm_pxa27x(void)
134 {
135         gsr_bits = 0;
136
137         /* warm reset broken on Bulverde,
138            so manually keep AC97 reset high */
139         pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
140         udelay(10);
141         GCR |= GCR_WARM_RST;
142         pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
143         udelay(500);
144 }
145
146 static inline void pxa_ac97_cold_pxa27x(void)
147 {
148         GCR &=  GCR_COLD_RST;  /* clear everything but nCRST */
149         GCR &= ~GCR_COLD_RST;  /* then assert nCRST */
150
151         gsr_bits = 0;
152
153         /* PXA27x Developers Manual section 13.5.2.2.1 */
154         clk_enable(ac97conf_clk);
155         udelay(5);
156         clk_disable(ac97conf_clk);
157         GCR = GCR_COLD_RST;
158         udelay(50);
159 }
160 #endif
161
162 #ifdef CONFIG_PXA3xx
163 static inline void pxa_ac97_warm_pxa3xx(void)
164 {
165         int timeout = 100;
166
167         gsr_bits = 0;
168
169         /* Can't use interrupts */
170         GCR |= GCR_WARM_RST;
171         while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
172                 mdelay(1);
173 }
174
175 static inline void pxa_ac97_cold_pxa3xx(void)
176 {
177         int timeout = 1000;
178
179         /* Hold CLKBPB for 100us */
180         GCR = 0;
181         GCR = GCR_CLKBPB;
182         udelay(100);
183         GCR = 0;
184
185         GCR &=  GCR_COLD_RST;  /* clear everything but nCRST */
186         GCR &= ~GCR_COLD_RST;  /* then assert nCRST */
187
188         gsr_bits = 0;
189
190         /* Can't use interrupts on PXA3xx */
191         GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
192
193         GCR = GCR_WARM_RST | GCR_COLD_RST;
194         while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
195                 mdelay(10);
196 }
197 #endif
198
199 bool pxa2xx_ac97_try_warm_reset(struct snd_ac97 *ac97)
200 {
201 #ifdef CONFIG_PXA25x
202         if (cpu_is_pxa25x())
203                 pxa_ac97_warm_pxa25x();
204         else
205 #endif
206 #ifdef CONFIG_PXA27x
207         if (cpu_is_pxa27x())
208                 pxa_ac97_warm_pxa27x();
209         else
210 #endif
211 #ifdef CONFIG_PXA3xx
212         if (cpu_is_pxa3xx())
213                 pxa_ac97_warm_pxa3xx();
214         else
215 #endif
216                 BUG();
217
218         if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
219                 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
220                                  __func__, gsr_bits);
221
222                 return false;
223         }
224
225         return true;
226 }
227 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
228
229 bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
230 {
231 #ifdef CONFIG_PXA25x
232         if (cpu_is_pxa25x())
233                 pxa_ac97_cold_pxa25x();
234         else
235 #endif
236 #ifdef CONFIG_PXA27x
237         if (cpu_is_pxa27x())
238                 pxa_ac97_cold_pxa27x();
239         else
240 #endif
241 #ifdef CONFIG_PXA3xx
242         if (cpu_is_pxa3xx())
243                 pxa_ac97_cold_pxa3xx();
244         else
245 #endif
246                 BUG();
247
248         if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
249                 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
250                                  __func__, gsr_bits);
251
252                 return false;
253         }
254
255         return true;
256 }
257 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
258
259
260 void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
261 {
262         GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
263         GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
264 }
265 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
266
267 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
268 {
269         long status;
270
271         status = GSR;
272         if (status) {
273                 GSR = status;
274                 gsr_bits |= status;
275                 wake_up(&gsr_wq);
276
277                 /* Although we don't use those we still need to clear them
278                    since they tend to spuriously trigger when MMC is used
279                    (hardware bug? go figure)... */
280                 if (cpu_is_pxa27x()) {
281                         MISR = MISR_EOC;
282                         PISR = PISR_EOC;
283                         MCSR = MCSR_EOC;
284                 }
285
286                 return IRQ_HANDLED;
287         }
288
289         return IRQ_NONE;
290 }
291
292 #ifdef CONFIG_PM
293 int pxa2xx_ac97_hw_suspend(void)
294 {
295         GCR |= GCR_ACLINK_OFF;
296         clk_disable(ac97_clk);
297         return 0;
298 }
299 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
300
301 int pxa2xx_ac97_hw_resume(void)
302 {
303         if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
304                 pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
305                 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
306                 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
307                 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
308         }
309         if (cpu_is_pxa27x()) {
310                 /* Use GPIO 113 as AC97 Reset on Bulverde */
311                 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
312         }
313         clk_enable(ac97_clk);
314         return 0;
315 }
316 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
317 #endif
318
319 int __devinit pxa2xx_ac97_hw_probe(struct platform_device *dev)
320 {
321         int ret;
322
323         if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
324                 pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
325                 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
326                 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
327                 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
328         }
329
330         if (cpu_is_pxa27x()) {
331                 /* Use GPIO 113 as AC97 Reset on Bulverde */
332                 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
333                 ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
334                 if (IS_ERR(ac97conf_clk)) {
335                         ret = PTR_ERR(ac97conf_clk);
336                         ac97conf_clk = NULL;
337                         goto err_conf;
338                 }
339         }
340
341         ac97_clk = clk_get(&dev->dev, "AC97CLK");
342         if (IS_ERR(ac97_clk)) {
343                 ret = PTR_ERR(ac97_clk);
344                 ac97_clk = NULL;
345                 goto err_clk;
346         }
347
348         ret = clk_enable(ac97_clk);
349         if (ret)
350                 goto err_clk2;
351
352         ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
353         if (ret < 0)
354                 goto err_irq;
355
356         return 0;
357
358 err_irq:
359         GCR |= GCR_ACLINK_OFF;
360 err_clk2:
361         clk_put(ac97_clk);
362         ac97_clk = NULL;
363 err_clk:
364         if (ac97conf_clk) {
365                 clk_put(ac97conf_clk);
366                 ac97conf_clk = NULL;
367         }
368 err_conf:
369         return ret;
370 }
371 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
372
373 void pxa2xx_ac97_hw_remove(struct platform_device *dev)
374 {
375         GCR |= GCR_ACLINK_OFF;
376         free_irq(IRQ_AC97, NULL);
377         if (ac97conf_clk) {
378                 clk_put(ac97conf_clk);
379                 ac97conf_clk = NULL;
380         }
381         clk_disable(ac97_clk);
382         clk_put(ac97_clk);
383         ac97_clk = NULL;
384 }
385 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
386
387 MODULE_AUTHOR("Nicolas Pitre");
388 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
389 MODULE_LICENSE("GPL");
390