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