]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - sound/arm/pxa2xx-ac97.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-3.10.git] / sound / arm / pxa2xx-ac97.c
1 /*
2  * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
3  *
4  * Author:      Nicolas Pitre
5  * Created:     Dec 02, 2004
6  * Copyright:   MontaVista Software Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/interrupt.h>
18 #include <linux/wait.h>
19 #include <linux/clk.h>
20 #include <linux/delay.h>
21
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/ac97_codec.h>
25 #include <sound/initval.h>
26
27 #include <asm/irq.h>
28 #include <linux/mutex.h>
29 #include <asm/hardware.h>
30 #include <asm/arch/pxa-regs.h>
31 #include <asm/arch/pxa2xx-gpio.h>
32 #include <asm/arch/audio.h>
33
34 #include "pxa2xx-pcm.h"
35
36
37 static DEFINE_MUTEX(car_mutex);
38 static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
39 static volatile long gsr_bits;
40 static struct clk *ac97_clk;
41 #ifdef CONFIG_PXA27x
42 static struct clk *ac97conf_clk;
43 #endif
44
45 /*
46  * Beware PXA27x bugs:
47  *
48  *   o Slot 12 read from modem space will hang controller.
49  *   o CDONE, SDONE interrupt fails after any slot 12 IO.
50  *
51  * We therefore have an hybrid approach for waiting on SDONE (interrupt or
52  * 1 jiffy timeout if interrupt never comes).
53  */ 
54
55 static unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
56 {
57         unsigned short val = -1;
58         volatile u32 *reg_addr;
59
60         mutex_lock(&car_mutex);
61
62         /* set up primary or secondary codec space */
63         reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
64         reg_addr += (reg >> 1);
65
66         /* start read access across the ac97 link */
67         GSR = GSR_CDONE | GSR_SDONE;
68         gsr_bits = 0;
69         val = *reg_addr;
70         if (reg == AC97_GPIO_STATUS)
71                 goto out;
72         if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
73             !((GSR | gsr_bits) & GSR_SDONE)) {
74                 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
75                                 __FUNCTION__, reg, GSR | gsr_bits);
76                 val = -1;
77                 goto out;
78         }
79
80         /* valid data now */
81         GSR = GSR_CDONE | GSR_SDONE;
82         gsr_bits = 0;
83         val = *reg_addr;                        
84         /* but we've just started another cycle... */
85         wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
86
87 out:    mutex_unlock(&car_mutex);
88         return val;
89 }
90
91 static void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
92 {
93         volatile u32 *reg_addr;
94
95         mutex_lock(&car_mutex);
96
97         /* set up primary or secondary codec space */
98         reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
99         reg_addr += (reg >> 1);
100
101         GSR = GSR_CDONE | GSR_SDONE;
102         gsr_bits = 0;
103         *reg_addr = val;
104         if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
105             !((GSR | gsr_bits) & GSR_CDONE))
106                 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
107                                 __FUNCTION__, reg, GSR | gsr_bits);
108
109         mutex_unlock(&car_mutex);
110 }
111
112 static void pxa2xx_ac97_reset(struct snd_ac97 *ac97)
113 {
114         /* First, try cold reset */
115         GCR &=  GCR_COLD_RST;  /* clear everything but nCRST */
116         GCR &= ~GCR_COLD_RST;  /* then assert nCRST */
117
118         gsr_bits = 0;
119 #ifdef CONFIG_PXA27x
120         /* PXA27x Developers Manual section 13.5.2.2.1 */
121         clk_enable(ac97conf_clk);
122         udelay(5);
123         clk_disable(ac97conf_clk);
124         GCR = GCR_COLD_RST;
125         udelay(50);
126 #else
127         GCR = GCR_COLD_RST;
128         GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
129         wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
130 #endif
131
132         if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
133                 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
134                                  __FUNCTION__, gsr_bits);
135
136                 /* let's try warm reset */
137                 gsr_bits = 0;
138 #ifdef CONFIG_PXA27x
139                 /* warm reset broken on Bulverde,
140                    so manually keep AC97 reset high */
141                 pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH); 
142                 udelay(10);
143                 GCR |= GCR_WARM_RST;
144                 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
145                 udelay(500);
146 #else
147                 GCR |= GCR_WARM_RST|GCR_PRIRDY_IEN|GCR_SECRDY_IEN;
148                 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
149 #endif                  
150
151                 if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
152                         printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
153                                          __FUNCTION__, gsr_bits);
154         }
155
156         GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
157         GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
158 }
159
160 static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
161 {
162         long status;
163
164         status = GSR;
165         if (status) {
166                 GSR = status;
167                 gsr_bits |= status;
168                 wake_up(&gsr_wq);
169
170 #ifdef CONFIG_PXA27x
171                 /* Although we don't use those we still need to clear them
172                    since they tend to spuriously trigger when MMC is used
173                    (hardware bug? go figure)... */
174                 MISR = MISR_EOC;
175                 PISR = PISR_EOC;
176                 MCSR = MCSR_EOC;
177 #endif
178
179                 return IRQ_HANDLED;
180         }
181
182         return IRQ_NONE;
183 }
184
185 static struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
186         .read   = pxa2xx_ac97_read,
187         .write  = pxa2xx_ac97_write,
188         .reset  = pxa2xx_ac97_reset,
189 };
190
191 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_out = {
192         .name                   = "AC97 PCM out",
193         .dev_addr               = __PREG(PCDR),
194         .drcmr                  = &DRCMRTXPCDR,
195         .dcmd                   = DCMD_INCSRCADDR | DCMD_FLOWTRG |
196                                   DCMD_BURST32 | DCMD_WIDTH4,
197 };
198
199 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_in = {
200         .name                   = "AC97 PCM in",
201         .dev_addr               = __PREG(PCDR),
202         .drcmr                  = &DRCMRRXPCDR,
203         .dcmd                   = DCMD_INCTRGADDR | DCMD_FLOWSRC |
204                                   DCMD_BURST32 | DCMD_WIDTH4,
205 };
206
207 static struct snd_pcm *pxa2xx_ac97_pcm;
208 static struct snd_ac97 *pxa2xx_ac97_ac97;
209
210 static int pxa2xx_ac97_pcm_startup(struct snd_pcm_substream *substream)
211 {
212         struct snd_pcm_runtime *runtime = substream->runtime;
213         pxa2xx_audio_ops_t *platform_ops;
214         int r;
215
216         runtime->hw.channels_min = 2;
217         runtime->hw.channels_max = 2;
218
219         r = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
220             AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
221         runtime->hw.rates = pxa2xx_ac97_ac97->rates[r];
222         snd_pcm_limit_hw_rates(runtime);
223
224         platform_ops = substream->pcm->card->dev->platform_data;
225         if (platform_ops && platform_ops->startup)
226                 return platform_ops->startup(substream, platform_ops->priv);
227         else
228                 return 0;
229 }
230
231 static void pxa2xx_ac97_pcm_shutdown(struct snd_pcm_substream *substream)
232 {
233         pxa2xx_audio_ops_t *platform_ops;
234
235         platform_ops = substream->pcm->card->dev->platform_data;
236         if (platform_ops && platform_ops->shutdown)
237                 platform_ops->shutdown(substream, platform_ops->priv);
238 }
239
240 static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
241 {
242         struct snd_pcm_runtime *runtime = substream->runtime;
243         int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
244                   AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
245         return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
246 }
247
248 static struct pxa2xx_pcm_client pxa2xx_ac97_pcm_client = {
249         .playback_params        = &pxa2xx_ac97_pcm_out,
250         .capture_params         = &pxa2xx_ac97_pcm_in,
251         .startup                = pxa2xx_ac97_pcm_startup,
252         .shutdown               = pxa2xx_ac97_pcm_shutdown,
253         .prepare                = pxa2xx_ac97_pcm_prepare,
254 };
255
256 #ifdef CONFIG_PM
257
258 static int pxa2xx_ac97_do_suspend(struct snd_card *card, pm_message_t state)
259 {
260         pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
261
262         snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
263         snd_pcm_suspend_all(pxa2xx_ac97_pcm);
264         snd_ac97_suspend(pxa2xx_ac97_ac97);
265         if (platform_ops && platform_ops->suspend)
266                 platform_ops->suspend(platform_ops->priv);
267         GCR |= GCR_ACLINK_OFF;
268         clk_disable(ac97_clk);
269
270         return 0;
271 }
272
273 static int pxa2xx_ac97_do_resume(struct snd_card *card)
274 {
275         pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
276
277         clk_enable(ac97_clk);
278         if (platform_ops && platform_ops->resume)
279                 platform_ops->resume(platform_ops->priv);
280         snd_ac97_resume(pxa2xx_ac97_ac97);
281         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
282
283         return 0;
284 }
285
286 static int pxa2xx_ac97_suspend(struct platform_device *dev, pm_message_t state)
287 {
288         struct snd_card *card = platform_get_drvdata(dev);
289         int ret = 0;
290
291         if (card)
292                 ret = pxa2xx_ac97_do_suspend(card, PMSG_SUSPEND);
293
294         return ret;
295 }
296
297 static int pxa2xx_ac97_resume(struct platform_device *dev)
298 {
299         struct snd_card *card = platform_get_drvdata(dev);
300         int ret = 0;
301
302         if (card)
303                 ret = pxa2xx_ac97_do_resume(card);
304
305         return ret;
306 }
307
308 #else
309 #define pxa2xx_ac97_suspend     NULL
310 #define pxa2xx_ac97_resume      NULL
311 #endif
312
313 static int __devinit pxa2xx_ac97_probe(struct platform_device *dev)
314 {
315         struct snd_card *card;
316         struct snd_ac97_bus *ac97_bus;
317         struct snd_ac97_template ac97_template;
318         int ret;
319
320         ret = -ENOMEM;
321         card = snd_card_new(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
322                             THIS_MODULE, 0);
323         if (!card)
324                 goto err;
325
326         card->dev = &dev->dev;
327         strncpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
328
329         ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);
330         if (ret)
331                 goto err;
332
333         ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
334         if (ret < 0)
335                 goto err;
336
337         pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
338         pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
339         pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
340         pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
341 #ifdef CONFIG_PXA27x
342         /* Use GPIO 113 as AC97 Reset on Bulverde */
343         pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
344         ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
345         if (IS_ERR(ac97conf_clk)) {
346                 ret = PTR_ERR(ac97conf_clk);
347                 ac97conf_clk = NULL;
348                 goto err;
349         }
350 #endif
351
352         ac97_clk = clk_get(&dev->dev, "AC97CLK");
353         if (IS_ERR(ac97_clk)) {
354                 ret = PTR_ERR(ac97_clk);
355                 ac97_clk = NULL;
356                 goto err;
357         }
358         clk_enable(ac97_clk);
359
360         ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
361         if (ret)
362                 goto err;
363         memset(&ac97_template, 0, sizeof(ac97_template));
364         ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
365         if (ret)
366                 goto err;
367
368         snprintf(card->shortname, sizeof(card->shortname),
369                  "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
370         snprintf(card->longname, sizeof(card->longname),
371                  "%s (%s)", dev->dev.driver->name, card->mixername);
372
373         snd_card_set_dev(card, &dev->dev);
374         ret = snd_card_register(card);
375         if (ret == 0) {
376                 platform_set_drvdata(dev, card);
377                 return 0;
378         }
379
380  err:
381         if (card)
382                 snd_card_free(card);
383         if (ac97_clk) {
384                 GCR |= GCR_ACLINK_OFF;
385                 free_irq(IRQ_AC97, NULL);
386                 clk_disable(ac97_clk);
387                 clk_put(ac97_clk);
388                 ac97_clk = NULL;
389         }
390 #ifdef CONFIG_PXA27x
391         if (ac97conf_clk) {
392                 clk_put(ac97conf_clk);
393                 ac97conf_clk = NULL;
394         }
395 #endif
396         return ret;
397 }
398
399 static int __devexit pxa2xx_ac97_remove(struct platform_device *dev)
400 {
401         struct snd_card *card = platform_get_drvdata(dev);
402
403         if (card) {
404                 snd_card_free(card);
405                 platform_set_drvdata(dev, NULL);
406                 GCR |= GCR_ACLINK_OFF;
407                 free_irq(IRQ_AC97, NULL);
408                 clk_disable(ac97_clk);
409                 clk_put(ac97_clk);
410                 ac97_clk = NULL;
411 #ifdef CONFIG_PXA27x
412                 clk_put(ac97conf_clk);
413                 ac97conf_clk = NULL;
414 #endif
415         }
416
417         return 0;
418 }
419
420 static struct platform_driver pxa2xx_ac97_driver = {
421         .probe          = pxa2xx_ac97_probe,
422         .remove         = __devexit_p(pxa2xx_ac97_remove),
423         .suspend        = pxa2xx_ac97_suspend,
424         .resume         = pxa2xx_ac97_resume,
425         .driver         = {
426                 .name   = "pxa2xx-ac97",
427         },
428 };
429
430 static int __init pxa2xx_ac97_init(void)
431 {
432         return platform_driver_register(&pxa2xx_ac97_driver);
433 }
434
435 static void __exit pxa2xx_ac97_exit(void)
436 {
437         platform_driver_unregister(&pxa2xx_ac97_driver);
438 }
439
440 module_init(pxa2xx_ac97_init);
441 module_exit(pxa2xx_ac97_exit);
442
443 MODULE_AUTHOR("Nicolas Pitre");
444 MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
445 MODULE_LICENSE("GPL");