ALSA: hda - Add position_fix quirk for Biostar mobo
[pandora-kernel.git] / sound / soc / s3c24xx / s3c-pcm.c
1 /* sound/soc/s3c24xx/s3c-pcm.c
2  *
3  * ALSA SoC Audio Layer - S3C PCM-Controller driver
4  *
5  * Copyright (c) 2009 Samsung Electronics Co. Ltd
6  * Author: Jaswinder Singh <jassi.brar@samsung.com>
7  * based upon I2S drivers by Ben Dooks.
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/init.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/delay.h>
18 #include <linux/clk.h>
19 #include <linux/kernel.h>
20 #include <linux/gpio.h>
21 #include <linux/io.h>
22
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/initval.h>
27 #include <sound/soc.h>
28
29 #include <plat/audio.h>
30 #include <plat/dma.h>
31
32 #include "s3c-dma.h"
33 #include "s3c-pcm.h"
34
35 static struct s3c2410_dma_client s3c_pcm_dma_client_out = {
36         .name           = "PCM Stereo out"
37 };
38
39 static struct s3c2410_dma_client s3c_pcm_dma_client_in = {
40         .name           = "PCM Stereo in"
41 };
42
43 static struct s3c_dma_params s3c_pcm_stereo_out[] = {
44         [0] = {
45                 .client         = &s3c_pcm_dma_client_out,
46                 .dma_size       = 4,
47         },
48         [1] = {
49                 .client         = &s3c_pcm_dma_client_out,
50                 .dma_size       = 4,
51         },
52 };
53
54 static struct s3c_dma_params s3c_pcm_stereo_in[] = {
55         [0] = {
56                 .client         = &s3c_pcm_dma_client_in,
57                 .dma_size       = 4,
58         },
59         [1] = {
60                 .client         = &s3c_pcm_dma_client_in,
61                 .dma_size       = 4,
62         },
63 };
64
65 static struct s3c_pcm_info s3c_pcm[2];
66
67 static inline struct s3c_pcm_info *to_info(struct snd_soc_dai *cpu_dai)
68 {
69         return cpu_dai->private_data;
70 }
71
72 static void s3c_pcm_snd_txctrl(struct s3c_pcm_info *pcm, int on)
73 {
74         void __iomem *regs = pcm->regs;
75         u32 ctl, clkctl;
76
77         clkctl = readl(regs + S3C_PCM_CLKCTL);
78         ctl = readl(regs + S3C_PCM_CTL);
79         ctl &= ~(S3C_PCM_CTL_TXDIPSTICK_MASK
80                          << S3C_PCM_CTL_TXDIPSTICK_SHIFT);
81
82         if (on) {
83                 ctl |= S3C_PCM_CTL_TXDMA_EN;
84                 ctl |= S3C_PCM_CTL_TXFIFO_EN;
85                 ctl |= S3C_PCM_CTL_ENABLE;
86                 ctl |= (0x20<<S3C_PCM_CTL_TXDIPSTICK_SHIFT);
87                 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
88         } else {
89                 ctl &= ~S3C_PCM_CTL_TXDMA_EN;
90                 ctl &= ~S3C_PCM_CTL_TXFIFO_EN;
91
92                 if (!(ctl & S3C_PCM_CTL_RXFIFO_EN)) {
93                         ctl &= ~S3C_PCM_CTL_ENABLE;
94                         if (!pcm->idleclk)
95                                 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
96                 }
97         }
98
99         writel(clkctl, regs + S3C_PCM_CLKCTL);
100         writel(ctl, regs + S3C_PCM_CTL);
101 }
102
103 static void s3c_pcm_snd_rxctrl(struct s3c_pcm_info *pcm, int on)
104 {
105         void __iomem *regs = pcm->regs;
106         u32 ctl, clkctl;
107
108         ctl = readl(regs + S3C_PCM_CTL);
109         clkctl = readl(regs + S3C_PCM_CLKCTL);
110
111         if (on) {
112                 ctl |= S3C_PCM_CTL_RXDMA_EN;
113                 ctl |= S3C_PCM_CTL_RXFIFO_EN;
114                 ctl |= S3C_PCM_CTL_ENABLE;
115                 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
116         } else {
117                 ctl &= ~S3C_PCM_CTL_RXDMA_EN;
118                 ctl &= ~S3C_PCM_CTL_RXFIFO_EN;
119
120                 if (!(ctl & S3C_PCM_CTL_TXFIFO_EN)) {
121                         ctl &= ~S3C_PCM_CTL_ENABLE;
122                         if (!pcm->idleclk)
123                                 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
124                 }
125         }
126
127         writel(clkctl, regs + S3C_PCM_CLKCTL);
128         writel(ctl, regs + S3C_PCM_CTL);
129 }
130
131 static int s3c_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
132                                struct snd_soc_dai *dai)
133 {
134         struct snd_soc_pcm_runtime *rtd = substream->private_data;
135         struct s3c_pcm_info *pcm = to_info(rtd->dai->cpu_dai);
136         unsigned long flags;
137
138         dev_dbg(pcm->dev, "Entered %s\n", __func__);
139
140         switch (cmd) {
141         case SNDRV_PCM_TRIGGER_START:
142         case SNDRV_PCM_TRIGGER_RESUME:
143         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
144                 spin_lock_irqsave(&pcm->lock, flags);
145
146                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
147                         s3c_pcm_snd_rxctrl(pcm, 1);
148                 else
149                         s3c_pcm_snd_txctrl(pcm, 1);
150
151                 spin_unlock_irqrestore(&pcm->lock, flags);
152                 break;
153
154         case SNDRV_PCM_TRIGGER_STOP:
155         case SNDRV_PCM_TRIGGER_SUSPEND:
156         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
157                 spin_lock_irqsave(&pcm->lock, flags);
158
159                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
160                         s3c_pcm_snd_rxctrl(pcm, 0);
161                 else
162                         s3c_pcm_snd_txctrl(pcm, 0);
163
164                 spin_unlock_irqrestore(&pcm->lock, flags);
165                 break;
166
167         default:
168                 return -EINVAL;
169         }
170
171         return 0;
172 }
173
174 static int s3c_pcm_hw_params(struct snd_pcm_substream *substream,
175                                  struct snd_pcm_hw_params *params,
176                                  struct snd_soc_dai *socdai)
177 {
178         struct snd_soc_pcm_runtime *rtd = substream->private_data;
179         struct snd_soc_dai_link *dai = rtd->dai;
180         struct s3c_pcm_info *pcm = to_info(dai->cpu_dai);
181         void __iomem *regs = pcm->regs;
182         struct clk *clk;
183         int sclk_div, sync_div;
184         unsigned long flags;
185         u32 clkctl;
186
187         dev_dbg(pcm->dev, "Entered %s\n", __func__);
188
189         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
190                 dai->cpu_dai->dma_data = pcm->dma_playback;
191         else
192                 dai->cpu_dai->dma_data = pcm->dma_capture;
193
194         /* Strictly check for sample size */
195         switch (params_format(params)) {
196         case SNDRV_PCM_FORMAT_S16_LE:
197                 break;
198         default:
199                 return -EINVAL;
200         }
201
202         spin_lock_irqsave(&pcm->lock, flags);
203
204         /* Get hold of the PCMSOURCE_CLK */
205         clkctl = readl(regs + S3C_PCM_CLKCTL);
206         if (clkctl & S3C_PCM_CLKCTL_SERCLKSEL_PCLK)
207                 clk = pcm->pclk;
208         else
209                 clk = pcm->cclk;
210
211         /* Set the SCLK divider */
212         sclk_div = clk_get_rate(clk) / pcm->sclk_per_fs /
213                                         params_rate(params) / 2 - 1;
214
215         clkctl &= ~(S3C_PCM_CLKCTL_SCLKDIV_MASK
216                         << S3C_PCM_CLKCTL_SCLKDIV_SHIFT);
217         clkctl |= ((sclk_div & S3C_PCM_CLKCTL_SCLKDIV_MASK)
218                         << S3C_PCM_CLKCTL_SCLKDIV_SHIFT);
219
220         /* Set the SYNC divider */
221         sync_div = pcm->sclk_per_fs - 1;
222
223         clkctl &= ~(S3C_PCM_CLKCTL_SYNCDIV_MASK
224                                 << S3C_PCM_CLKCTL_SYNCDIV_SHIFT);
225         clkctl |= ((sync_div & S3C_PCM_CLKCTL_SYNCDIV_MASK)
226                                 << S3C_PCM_CLKCTL_SYNCDIV_SHIFT);
227
228         writel(clkctl, regs + S3C_PCM_CLKCTL);
229
230         spin_unlock_irqrestore(&pcm->lock, flags);
231
232         dev_dbg(pcm->dev, "PCMSOURCE_CLK-%lu SCLK=%ufs SCLK_DIV=%d SYNC_DIV=%d\n",
233                                 clk_get_rate(clk), pcm->sclk_per_fs,
234                                 sclk_div, sync_div);
235
236         return 0;
237 }
238
239 static int s3c_pcm_set_fmt(struct snd_soc_dai *cpu_dai,
240                                unsigned int fmt)
241 {
242         struct s3c_pcm_info *pcm = to_info(cpu_dai);
243         void __iomem *regs = pcm->regs;
244         unsigned long flags;
245         int ret = 0;
246         u32 ctl;
247
248         dev_dbg(pcm->dev, "Entered %s\n", __func__);
249
250         spin_lock_irqsave(&pcm->lock, flags);
251
252         ctl = readl(regs + S3C_PCM_CTL);
253
254         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
255         case SND_SOC_DAIFMT_NB_NF:
256                 /* Nothing to do, NB_NF by default */
257                 break;
258         default:
259                 dev_err(pcm->dev, "Unsupported clock inversion!\n");
260                 ret = -EINVAL;
261                 goto exit;
262         }
263
264         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
265         case SND_SOC_DAIFMT_CBS_CFS:
266                 /* Nothing to do, Master by default */
267                 break;
268         default:
269                 dev_err(pcm->dev, "Unsupported master/slave format!\n");
270                 ret = -EINVAL;
271                 goto exit;
272         }
273
274         switch (fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
275         case SND_SOC_DAIFMT_CONT:
276                 pcm->idleclk = 1;
277                 break;
278         case SND_SOC_DAIFMT_GATED:
279                 pcm->idleclk = 0;
280                 break;
281         default:
282                 dev_err(pcm->dev, "Invalid Clock gating request!\n");
283                 ret = -EINVAL;
284                 goto exit;
285         }
286
287         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
288         case SND_SOC_DAIFMT_DSP_A:
289                 ctl |= S3C_PCM_CTL_TXMSB_AFTER_FSYNC;
290                 ctl |= S3C_PCM_CTL_RXMSB_AFTER_FSYNC;
291                 break;
292         case SND_SOC_DAIFMT_DSP_B:
293                 ctl &= ~S3C_PCM_CTL_TXMSB_AFTER_FSYNC;
294                 ctl &= ~S3C_PCM_CTL_RXMSB_AFTER_FSYNC;
295                 break;
296         default:
297                 dev_err(pcm->dev, "Unsupported data format!\n");
298                 ret = -EINVAL;
299                 goto exit;
300         }
301
302         writel(ctl, regs + S3C_PCM_CTL);
303
304 exit:
305         spin_unlock_irqrestore(&pcm->lock, flags);
306
307         return ret;
308 }
309
310 static int s3c_pcm_set_clkdiv(struct snd_soc_dai *cpu_dai,
311                                                 int div_id, int div)
312 {
313         struct s3c_pcm_info *pcm = to_info(cpu_dai);
314
315         switch (div_id) {
316         case S3C_PCM_SCLK_PER_FS:
317                 pcm->sclk_per_fs = div;
318                 break;
319
320         default:
321                 return -EINVAL;
322         }
323
324         return 0;
325 }
326
327 static int s3c_pcm_set_sysclk(struct snd_soc_dai *cpu_dai,
328                                   int clk_id, unsigned int freq, int dir)
329 {
330         struct s3c_pcm_info *pcm = to_info(cpu_dai);
331         void __iomem *regs = pcm->regs;
332         u32 clkctl = readl(regs + S3C_PCM_CLKCTL);
333
334         switch (clk_id) {
335         case S3C_PCM_CLKSRC_PCLK:
336                 clkctl |= S3C_PCM_CLKCTL_SERCLKSEL_PCLK;
337                 break;
338
339         case S3C_PCM_CLKSRC_MUX:
340                 clkctl &= ~S3C_PCM_CLKCTL_SERCLKSEL_PCLK;
341
342                 if (clk_get_rate(pcm->cclk) != freq)
343                         clk_set_rate(pcm->cclk, freq);
344
345                 break;
346
347         default:
348                 return -EINVAL;
349         }
350
351         writel(clkctl, regs + S3C_PCM_CLKCTL);
352
353         return 0;
354 }
355
356 static struct snd_soc_dai_ops s3c_pcm_dai_ops = {
357         .set_sysclk     = s3c_pcm_set_sysclk,
358         .set_clkdiv     = s3c_pcm_set_clkdiv,
359         .trigger        = s3c_pcm_trigger,
360         .hw_params      = s3c_pcm_hw_params,
361         .set_fmt        = s3c_pcm_set_fmt,
362 };
363
364 #define S3C_PCM_RATES  SNDRV_PCM_RATE_8000_96000
365
366 #define S3C_PCM_DECLARE(n)                      \
367 {                                                               \
368         .name            = "samsung-pcm",                       \
369         .id              = (n),                         \
370         .symmetric_rates = 1,                                   \
371         .ops = &s3c_pcm_dai_ops,                                \
372         .playback = {                                           \
373                 .channels_min   = 2,                            \
374                 .channels_max   = 2,                            \
375                 .rates          = S3C_PCM_RATES,                \
376                 .formats        = SNDRV_PCM_FMTBIT_S16_LE,      \
377         },                                                      \
378         .capture = {                                            \
379                 .channels_min   = 2,                            \
380                 .channels_max   = 2,                            \
381                 .rates          = S3C_PCM_RATES,                \
382                 .formats        = SNDRV_PCM_FMTBIT_S16_LE,      \
383         },                                                      \
384 }
385
386 struct snd_soc_dai s3c_pcm_dai[] = {
387         S3C_PCM_DECLARE(0),
388         S3C_PCM_DECLARE(1),
389 };
390 EXPORT_SYMBOL_GPL(s3c_pcm_dai);
391
392 static __devinit int s3c_pcm_dev_probe(struct platform_device *pdev)
393 {
394         struct s3c_pcm_info *pcm;
395         struct snd_soc_dai *dai;
396         struct resource *mem_res, *dmatx_res, *dmarx_res;
397         struct s3c_audio_pdata *pcm_pdata;
398         int ret;
399
400         /* Check for valid device index */
401         if ((pdev->id < 0) || pdev->id >= ARRAY_SIZE(s3c_pcm)) {
402                 dev_err(&pdev->dev, "id %d out of range\n", pdev->id);
403                 return -EINVAL;
404         }
405
406         pcm_pdata = pdev->dev.platform_data;
407
408         /* Check for availability of necessary resource */
409         dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
410         if (!dmatx_res) {
411                 dev_err(&pdev->dev, "Unable to get PCM-TX dma resource\n");
412                 return -ENXIO;
413         }
414
415         dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
416         if (!dmarx_res) {
417                 dev_err(&pdev->dev, "Unable to get PCM-RX dma resource\n");
418                 return -ENXIO;
419         }
420
421         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
422         if (!mem_res) {
423                 dev_err(&pdev->dev, "Unable to get register resource\n");
424                 return -ENXIO;
425         }
426
427         if (pcm_pdata && pcm_pdata->cfg_gpio && pcm_pdata->cfg_gpio(pdev)) {
428                 dev_err(&pdev->dev, "Unable to configure gpio\n");
429                 return -EINVAL;
430         }
431
432         pcm = &s3c_pcm[pdev->id];
433         pcm->dev = &pdev->dev;
434
435         spin_lock_init(&pcm->lock);
436
437         dai = &s3c_pcm_dai[pdev->id];
438         dai->dev = &pdev->dev;
439
440         /* Default is 128fs */
441         pcm->sclk_per_fs = 128;
442
443         pcm->cclk = clk_get(&pdev->dev, "audio-bus");
444         if (IS_ERR(pcm->cclk)) {
445                 dev_err(&pdev->dev, "failed to get audio-bus\n");
446                 ret = PTR_ERR(pcm->cclk);
447                 goto err1;
448         }
449         clk_enable(pcm->cclk);
450
451         /* record our pcm structure for later use in the callbacks */
452         dai->private_data = pcm;
453
454         if (!request_mem_region(mem_res->start,
455                                 resource_size(mem_res), "samsung-pcm")) {
456                 dev_err(&pdev->dev, "Unable to request register region\n");
457                 ret = -EBUSY;
458                 goto err2;
459         }
460
461         pcm->regs = ioremap(mem_res->start, 0x100);
462         if (pcm->regs == NULL) {
463                 dev_err(&pdev->dev, "cannot ioremap registers\n");
464                 ret = -ENXIO;
465                 goto err3;
466         }
467
468         pcm->pclk = clk_get(&pdev->dev, "pcm");
469         if (IS_ERR(pcm->pclk)) {
470                 dev_err(&pdev->dev, "failed to get pcm_clock\n");
471                 ret = -ENOENT;
472                 goto err4;
473         }
474         clk_enable(pcm->pclk);
475
476         ret = snd_soc_register_dai(dai);
477         if (ret != 0) {
478                 dev_err(&pdev->dev, "failed to get pcm_clock\n");
479                 goto err5;
480         }
481
482         s3c_pcm_stereo_in[pdev->id].dma_addr = mem_res->start
483                                                         + S3C_PCM_RXFIFO;
484         s3c_pcm_stereo_out[pdev->id].dma_addr = mem_res->start
485                                                         + S3C_PCM_TXFIFO;
486
487         s3c_pcm_stereo_in[pdev->id].channel = dmarx_res->start;
488         s3c_pcm_stereo_out[pdev->id].channel = dmatx_res->start;
489
490         pcm->dma_capture = &s3c_pcm_stereo_in[pdev->id];
491         pcm->dma_playback = &s3c_pcm_stereo_out[pdev->id];
492
493         return 0;
494
495 err5:
496         clk_disable(pcm->pclk);
497         clk_put(pcm->pclk);
498 err4:
499         iounmap(pcm->regs);
500 err3:
501         release_mem_region(mem_res->start, resource_size(mem_res));
502 err2:
503         clk_disable(pcm->cclk);
504         clk_put(pcm->cclk);
505 err1:
506         return ret;
507 }
508
509 static __devexit int s3c_pcm_dev_remove(struct platform_device *pdev)
510 {
511         struct s3c_pcm_info *pcm = &s3c_pcm[pdev->id];
512         struct resource *mem_res;
513
514         iounmap(pcm->regs);
515
516         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
517         release_mem_region(mem_res->start, resource_size(mem_res));
518
519         clk_disable(pcm->cclk);
520         clk_disable(pcm->pclk);
521         clk_put(pcm->pclk);
522         clk_put(pcm->cclk);
523
524         return 0;
525 }
526
527 static struct platform_driver s3c_pcm_driver = {
528         .probe  = s3c_pcm_dev_probe,
529         .remove = s3c_pcm_dev_remove,
530         .driver = {
531                 .name = "samsung-pcm",
532                 .owner = THIS_MODULE,
533         },
534 };
535
536 static int __init s3c_pcm_init(void)
537 {
538         return platform_driver_register(&s3c_pcm_driver);
539 }
540 module_init(s3c_pcm_init);
541
542 static void __exit s3c_pcm_exit(void)
543 {
544         platform_driver_unregister(&s3c_pcm_driver);
545 }
546 module_exit(s3c_pcm_exit);
547
548 /* Module information */
549 MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
550 MODULE_DESCRIPTION("S3C PCM Controller Driver");
551 MODULE_LICENSE("GPL");