vmalloc: fix __GFP_HIGHMEM usage for vmalloc_32 on 32b systems
[linux/fpc-iii.git] / sound / arm / pxa2xx-ac97-lib.c
blob5950a9e218d962294b4e46f36e795e0af37b0250
1 /*
2 * Based on sound/arm/pxa2xx-ac97.c and sound/soc/pxa/pxa2xx-ac97.c
3 * which contain:
5 * Author: Nicolas Pitre
6 * Created: Dec 02, 2004
7 * Copyright: MontaVista Software Inc.
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.
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 #include <linux/module.h>
20 #include <linux/io.h>
21 #include <linux/gpio.h>
23 #include <sound/pxa2xx-lib.h>
25 #include <mach/irqs.h>
26 #include <mach/regs-ac97.h>
27 #include <mach/audio.h>
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;
36 extern void pxa27x_configure_ac97reset(int reset_gpio, bool to_gpio);
39 * Beware PXA27x bugs:
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)
50 int val = -ENODEV;
51 volatile u32 *reg_addr;
53 if (slot > 0)
54 return -ENODEV;
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 = slot ? &SMC_REG_BASE : &PMC_REG_BASE;
61 else
62 reg_addr = slot ? &SAC_REG_BASE : &PAC_REG_BASE;
63 reg_addr += (reg >> 1);
65 /* start read access across the ac97 link */
66 GSR = GSR_CDONE | GSR_SDONE;
67 gsr_bits = 0;
68 val = (*reg_addr & 0xffff);
69 if (reg == AC97_GPIO_STATUS)
70 goto out;
71 if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
72 !((GSR | gsr_bits) & GSR_SDONE)) {
73 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
74 __func__, reg, GSR | gsr_bits);
75 val = -ETIMEDOUT;
76 goto out;
79 /* valid data now */
80 GSR = GSR_CDONE | GSR_SDONE;
81 gsr_bits = 0;
82 val = (*reg_addr & 0xffff);
83 /* but we've just started another cycle... */
84 wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
86 out: mutex_unlock(&car_mutex);
87 return val;
89 EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
91 int pxa2xx_ac97_write(int slot, unsigned short reg, unsigned short val)
93 volatile u32 *reg_addr;
94 int ret = 0;
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 = slot ? &SMC_REG_BASE : &PMC_REG_BASE;
101 else
102 reg_addr = slot ? &SAC_REG_BASE : &PAC_REG_BASE;
103 reg_addr += (reg >> 1);
105 GSR = GSR_CDONE | GSR_SDONE;
106 gsr_bits = 0;
107 *reg_addr = val;
108 if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
109 !((GSR | gsr_bits) & GSR_CDONE)) {
110 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
111 __func__, reg, GSR | gsr_bits);
112 ret = -EIO;
115 mutex_unlock(&car_mutex);
116 return ret;
118 EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
120 #ifdef CONFIG_PXA25x
121 static inline void pxa_ac97_warm_pxa25x(void)
123 gsr_bits = 0;
125 GCR |= GCR_WARM_RST;
128 static inline void pxa_ac97_cold_pxa25x(void)
130 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
131 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
133 gsr_bits = 0;
135 GCR = GCR_COLD_RST;
137 #endif
139 #ifdef CONFIG_PXA27x
140 static inline void pxa_ac97_warm_pxa27x(void)
142 gsr_bits = 0;
144 /* warm reset broken on Bulverde, so manually keep AC97 reset high */
145 pxa27x_configure_ac97reset(reset_gpio, true);
146 udelay(10);
147 GCR |= GCR_WARM_RST;
148 pxa27x_configure_ac97reset(reset_gpio, false);
149 udelay(500);
152 static inline void pxa_ac97_cold_pxa27x(void)
154 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
155 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
157 gsr_bits = 0;
159 /* PXA27x Developers Manual section 13.5.2.2.1 */
160 clk_prepare_enable(ac97conf_clk);
161 udelay(5);
162 clk_disable_unprepare(ac97conf_clk);
163 GCR = GCR_COLD_RST | GCR_WARM_RST;
165 #endif
167 #ifdef CONFIG_PXA3xx
168 static inline void pxa_ac97_warm_pxa3xx(void)
170 gsr_bits = 0;
172 /* Can't use interrupts */
173 GCR |= GCR_WARM_RST;
176 static inline void pxa_ac97_cold_pxa3xx(void)
178 /* Hold CLKBPB for 100us */
179 GCR = 0;
180 GCR = GCR_CLKBPB;
181 udelay(100);
182 GCR = 0;
184 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
185 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
187 gsr_bits = 0;
189 /* Can't use interrupts on PXA3xx */
190 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
192 GCR = GCR_WARM_RST | GCR_COLD_RST;
194 #endif
196 bool pxa2xx_ac97_try_warm_reset(void)
198 unsigned long gsr;
199 unsigned int timeout = 100;
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 snd_BUG();
218 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
219 mdelay(1);
221 gsr = GSR | gsr_bits;
222 if (!(gsr & (GSR_PCR | GSR_SCR))) {
223 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
224 __func__, gsr);
226 return false;
229 return true;
231 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
233 bool pxa2xx_ac97_try_cold_reset(void)
235 unsigned long gsr;
236 unsigned int timeout = 1000;
238 #ifdef CONFIG_PXA25x
239 if (cpu_is_pxa25x())
240 pxa_ac97_cold_pxa25x();
241 else
242 #endif
243 #ifdef CONFIG_PXA27x
244 if (cpu_is_pxa27x())
245 pxa_ac97_cold_pxa27x();
246 else
247 #endif
248 #ifdef CONFIG_PXA3xx
249 if (cpu_is_pxa3xx())
250 pxa_ac97_cold_pxa3xx();
251 else
252 #endif
253 snd_BUG();
255 while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
256 mdelay(1);
258 gsr = GSR | gsr_bits;
259 if (!(gsr & (GSR_PCR | GSR_SCR))) {
260 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
261 __func__, gsr);
263 return false;
266 return true;
268 EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
271 void pxa2xx_ac97_finish_reset(void)
273 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
274 GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
276 EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
278 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
280 long status;
282 status = GSR;
283 if (status) {
284 GSR = status;
285 gsr_bits |= status;
286 wake_up(&gsr_wq);
288 /* Although we don't use those we still need to clear them
289 since they tend to spuriously trigger when MMC is used
290 (hardware bug? go figure)... */
291 if (cpu_is_pxa27x()) {
292 MISR = MISR_EOC;
293 PISR = PISR_EOC;
294 MCSR = MCSR_EOC;
297 return IRQ_HANDLED;
300 return IRQ_NONE;
303 #ifdef CONFIG_PM
304 int pxa2xx_ac97_hw_suspend(void)
306 GCR |= GCR_ACLINK_OFF;
307 clk_disable_unprepare(ac97_clk);
308 return 0;
310 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
312 int pxa2xx_ac97_hw_resume(void)
314 clk_prepare_enable(ac97_clk);
315 return 0;
317 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
318 #endif
320 int pxa2xx_ac97_hw_probe(struct platform_device *dev)
322 int ret;
323 pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
325 if (pdata) {
326 switch (pdata->reset_gpio) {
327 case 95:
328 case 113:
329 reset_gpio = pdata->reset_gpio;
330 break;
331 case 0:
332 reset_gpio = 113;
333 break;
334 case -1:
335 break;
336 default:
337 dev_err(&dev->dev, "Invalid reset GPIO %d\n",
338 pdata->reset_gpio);
340 } else {
341 if (cpu_is_pxa27x())
342 reset_gpio = 113;
345 if (cpu_is_pxa27x()) {
347 * This gpio is needed for a work-around to a bug in the ac97
348 * controller during warm reset. The direction and level is set
349 * here so that it is an output driven high when switching from
350 * AC97_nRESET alt function to generic gpio.
352 ret = gpio_request_one(reset_gpio, GPIOF_OUT_INIT_HIGH,
353 "pxa27x ac97 reset");
354 if (ret < 0) {
355 pr_err("%s: gpio_request_one() failed: %d\n",
356 __func__, ret);
357 goto err_conf;
359 pxa27x_configure_ac97reset(reset_gpio, false);
361 ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
362 if (IS_ERR(ac97conf_clk)) {
363 ret = PTR_ERR(ac97conf_clk);
364 ac97conf_clk = NULL;
365 goto err_conf;
369 ac97_clk = clk_get(&dev->dev, "AC97CLK");
370 if (IS_ERR(ac97_clk)) {
371 ret = PTR_ERR(ac97_clk);
372 ac97_clk = NULL;
373 goto err_clk;
376 ret = clk_prepare_enable(ac97_clk);
377 if (ret)
378 goto err_clk2;
380 ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
381 if (ret < 0)
382 goto err_irq;
384 return 0;
386 err_irq:
387 GCR |= GCR_ACLINK_OFF;
388 err_clk2:
389 clk_put(ac97_clk);
390 ac97_clk = NULL;
391 err_clk:
392 if (ac97conf_clk) {
393 clk_put(ac97conf_clk);
394 ac97conf_clk = NULL;
396 err_conf:
397 return ret;
399 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
401 void pxa2xx_ac97_hw_remove(struct platform_device *dev)
403 if (cpu_is_pxa27x())
404 gpio_free(reset_gpio);
405 GCR |= GCR_ACLINK_OFF;
406 free_irq(IRQ_AC97, NULL);
407 if (ac97conf_clk) {
408 clk_put(ac97conf_clk);
409 ac97conf_clk = NULL;
411 clk_disable_unprepare(ac97_clk);
412 clk_put(ac97_clk);
413 ac97_clk = NULL;
415 EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
417 MODULE_AUTHOR("Nicolas Pitre");
418 MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
419 MODULE_LICENSE("GPL");