Revert "USB: musb: blackfin: pm: make it work"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / s3c24xx / s3c-pcm.c
blob2e020e1b4eab027640a0e92b4f0506305c0297f9
1 /* sound/soc/s3c24xx/s3c-pcm.c
3 * ALSA SoC Audio Layer - S3C PCM-Controller driver
5 * Copyright (c) 2009 Samsung Electronics Co. Ltd
6 * Author: Jaswinder Singh <jassi.brar@samsung.com>
7 * based upon I2S drivers by Ben Dooks.
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/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>
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>
29 #include <plat/audio.h>
30 #include <plat/dma.h>
32 #include "s3c-dma.h"
33 #include "s3c-pcm.h"
35 static struct s3c2410_dma_client s3c_pcm_dma_client_out = {
36 .name = "PCM Stereo out"
39 static struct s3c2410_dma_client s3c_pcm_dma_client_in = {
40 .name = "PCM Stereo in"
43 static struct s3c_dma_params s3c_pcm_stereo_out[] = {
44 [0] = {
45 .client = &s3c_pcm_dma_client_out,
46 .dma_size = 4,
48 [1] = {
49 .client = &s3c_pcm_dma_client_out,
50 .dma_size = 4,
54 static struct s3c_dma_params s3c_pcm_stereo_in[] = {
55 [0] = {
56 .client = &s3c_pcm_dma_client_in,
57 .dma_size = 4,
59 [1] = {
60 .client = &s3c_pcm_dma_client_in,
61 .dma_size = 4,
65 static struct s3c_pcm_info s3c_pcm[2];
67 static void s3c_pcm_snd_txctrl(struct s3c_pcm_info *pcm, int on)
69 void __iomem *regs = pcm->regs;
70 u32 ctl, clkctl;
72 clkctl = readl(regs + S3C_PCM_CLKCTL);
73 ctl = readl(regs + S3C_PCM_CTL);
74 ctl &= ~(S3C_PCM_CTL_TXDIPSTICK_MASK
75 << S3C_PCM_CTL_TXDIPSTICK_SHIFT);
77 if (on) {
78 ctl |= S3C_PCM_CTL_TXDMA_EN;
79 ctl |= S3C_PCM_CTL_TXFIFO_EN;
80 ctl |= S3C_PCM_CTL_ENABLE;
81 ctl |= (0x4<<S3C_PCM_CTL_TXDIPSTICK_SHIFT);
82 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
83 } else {
84 ctl &= ~S3C_PCM_CTL_TXDMA_EN;
85 ctl &= ~S3C_PCM_CTL_TXFIFO_EN;
87 if (!(ctl & S3C_PCM_CTL_RXFIFO_EN)) {
88 ctl &= ~S3C_PCM_CTL_ENABLE;
89 if (!pcm->idleclk)
90 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
94 writel(clkctl, regs + S3C_PCM_CLKCTL);
95 writel(ctl, regs + S3C_PCM_CTL);
98 static void s3c_pcm_snd_rxctrl(struct s3c_pcm_info *pcm, int on)
100 void __iomem *regs = pcm->regs;
101 u32 ctl, clkctl;
103 ctl = readl(regs + S3C_PCM_CTL);
104 clkctl = readl(regs + S3C_PCM_CLKCTL);
105 ctl &= ~(S3C_PCM_CTL_RXDIPSTICK_MASK
106 << S3C_PCM_CTL_RXDIPSTICK_SHIFT);
108 if (on) {
109 ctl |= S3C_PCM_CTL_RXDMA_EN;
110 ctl |= S3C_PCM_CTL_RXFIFO_EN;
111 ctl |= S3C_PCM_CTL_ENABLE;
112 ctl |= (0x20<<S3C_PCM_CTL_RXDIPSTICK_SHIFT);
113 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
114 } else {
115 ctl &= ~S3C_PCM_CTL_RXDMA_EN;
116 ctl &= ~S3C_PCM_CTL_RXFIFO_EN;
118 if (!(ctl & S3C_PCM_CTL_TXFIFO_EN)) {
119 ctl &= ~S3C_PCM_CTL_ENABLE;
120 if (!pcm->idleclk)
121 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
125 writel(clkctl, regs + S3C_PCM_CLKCTL);
126 writel(ctl, regs + S3C_PCM_CTL);
129 static int s3c_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
130 struct snd_soc_dai *dai)
132 struct snd_soc_pcm_runtime *rtd = substream->private_data;
133 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(rtd->cpu_dai);
134 unsigned long flags;
136 dev_dbg(pcm->dev, "Entered %s\n", __func__);
138 switch (cmd) {
139 case SNDRV_PCM_TRIGGER_START:
140 case SNDRV_PCM_TRIGGER_RESUME:
141 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
142 spin_lock_irqsave(&pcm->lock, flags);
144 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
145 s3c_pcm_snd_rxctrl(pcm, 1);
146 else
147 s3c_pcm_snd_txctrl(pcm, 1);
149 spin_unlock_irqrestore(&pcm->lock, flags);
150 break;
152 case SNDRV_PCM_TRIGGER_STOP:
153 case SNDRV_PCM_TRIGGER_SUSPEND:
154 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
155 spin_lock_irqsave(&pcm->lock, flags);
157 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
158 s3c_pcm_snd_rxctrl(pcm, 0);
159 else
160 s3c_pcm_snd_txctrl(pcm, 0);
162 spin_unlock_irqrestore(&pcm->lock, flags);
163 break;
165 default:
166 return -EINVAL;
169 return 0;
172 static int s3c_pcm_hw_params(struct snd_pcm_substream *substream,
173 struct snd_pcm_hw_params *params,
174 struct snd_soc_dai *socdai)
176 struct snd_soc_pcm_runtime *rtd = substream->private_data;
177 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(rtd->cpu_dai);
178 struct s3c_dma_params *dma_data;
179 void __iomem *regs = pcm->regs;
180 struct clk *clk;
181 int sclk_div, sync_div;
182 unsigned long flags;
183 u32 clkctl;
185 dev_dbg(pcm->dev, "Entered %s\n", __func__);
187 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
188 dma_data = pcm->dma_playback;
189 else
190 dma_data = pcm->dma_capture;
192 snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_data);
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;
202 spin_lock_irqsave(&pcm->lock, flags);
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;
211 /* Set the SCLK divider */
212 sclk_div = clk_get_rate(clk) / pcm->sclk_per_fs /
213 params_rate(params) / 2 - 1;
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);
220 /* Set the SYNC divider */
221 sync_div = pcm->sclk_per_fs - 1;
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);
228 writel(clkctl, regs + S3C_PCM_CLKCTL);
230 spin_unlock_irqrestore(&pcm->lock, flags);
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);
236 return 0;
239 static int s3c_pcm_set_fmt(struct snd_soc_dai *cpu_dai,
240 unsigned int fmt)
242 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
243 void __iomem *regs = pcm->regs;
244 unsigned long flags;
245 int ret = 0;
246 u32 ctl;
248 dev_dbg(pcm->dev, "Entered %s\n", __func__);
250 spin_lock_irqsave(&pcm->lock, flags);
252 ctl = readl(regs + S3C_PCM_CTL);
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;
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;
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;
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;
302 writel(ctl, regs + S3C_PCM_CTL);
304 exit:
305 spin_unlock_irqrestore(&pcm->lock, flags);
307 return ret;
310 static int s3c_pcm_set_clkdiv(struct snd_soc_dai *cpu_dai,
311 int div_id, int div)
313 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
315 switch (div_id) {
316 case S3C_PCM_SCLK_PER_FS:
317 pcm->sclk_per_fs = div;
318 break;
320 default:
321 return -EINVAL;
324 return 0;
327 static int s3c_pcm_set_sysclk(struct snd_soc_dai *cpu_dai,
328 int clk_id, unsigned int freq, int dir)
330 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
331 void __iomem *regs = pcm->regs;
332 u32 clkctl = readl(regs + S3C_PCM_CLKCTL);
334 switch (clk_id) {
335 case S3C_PCM_CLKSRC_PCLK:
336 clkctl |= S3C_PCM_CLKCTL_SERCLKSEL_PCLK;
337 break;
339 case S3C_PCM_CLKSRC_MUX:
340 clkctl &= ~S3C_PCM_CLKCTL_SERCLKSEL_PCLK;
342 if (clk_get_rate(pcm->cclk) != freq)
343 clk_set_rate(pcm->cclk, freq);
345 break;
347 default:
348 return -EINVAL;
351 writel(clkctl, regs + S3C_PCM_CLKCTL);
353 return 0;
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,
364 #define S3C_PCM_RATES SNDRV_PCM_RATE_8000_96000
366 #define S3C_PCM_DAI_DECLARE \
367 .symmetric_rates = 1, \
368 .ops = &s3c_pcm_dai_ops, \
369 .playback = { \
370 .channels_min = 2, \
371 .channels_max = 2, \
372 .rates = S3C_PCM_RATES, \
373 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
374 }, \
375 .capture = { \
376 .channels_min = 2, \
377 .channels_max = 2, \
378 .rates = S3C_PCM_RATES, \
379 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
382 struct snd_soc_dai_driver s3c_pcm_dai[] = {
383 [0] = {
384 .name = "samsung-pcm.0",
385 S3C_PCM_DAI_DECLARE,
387 [1] = {
388 .name = "samsung-pcm.1",
389 S3C_PCM_DAI_DECLARE,
392 EXPORT_SYMBOL_GPL(s3c_pcm_dai);
394 static __devinit int s3c_pcm_dev_probe(struct platform_device *pdev)
396 struct s3c_pcm_info *pcm;
397 struct resource *mem_res, *dmatx_res, *dmarx_res;
398 struct s3c_audio_pdata *pcm_pdata;
399 int ret;
401 /* Check for valid device index */
402 if ((pdev->id < 0) || pdev->id >= ARRAY_SIZE(s3c_pcm)) {
403 dev_err(&pdev->dev, "id %d out of range\n", pdev->id);
404 return -EINVAL;
407 pcm_pdata = pdev->dev.platform_data;
409 /* Check for availability of necessary resource */
410 dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
411 if (!dmatx_res) {
412 dev_err(&pdev->dev, "Unable to get PCM-TX dma resource\n");
413 return -ENXIO;
416 dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
417 if (!dmarx_res) {
418 dev_err(&pdev->dev, "Unable to get PCM-RX dma resource\n");
419 return -ENXIO;
422 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
423 if (!mem_res) {
424 dev_err(&pdev->dev, "Unable to get register resource\n");
425 return -ENXIO;
428 if (pcm_pdata && pcm_pdata->cfg_gpio && pcm_pdata->cfg_gpio(pdev)) {
429 dev_err(&pdev->dev, "Unable to configure gpio\n");
430 return -EINVAL;
433 pcm = &s3c_pcm[pdev->id];
434 pcm->dev = &pdev->dev;
436 spin_lock_init(&pcm->lock);
438 /* Default is 128fs */
439 pcm->sclk_per_fs = 128;
441 pcm->cclk = clk_get(&pdev->dev, "audio-bus");
442 if (IS_ERR(pcm->cclk)) {
443 dev_err(&pdev->dev, "failed to get audio-bus\n");
444 ret = PTR_ERR(pcm->cclk);
445 goto err1;
447 clk_enable(pcm->cclk);
449 /* record our pcm structure for later use in the callbacks */
450 dev_set_drvdata(&pdev->dev, pcm);
452 if (!request_mem_region(mem_res->start,
453 resource_size(mem_res), "samsung-pcm")) {
454 dev_err(&pdev->dev, "Unable to request register region\n");
455 ret = -EBUSY;
456 goto err2;
459 pcm->regs = ioremap(mem_res->start, 0x100);
460 if (pcm->regs == NULL) {
461 dev_err(&pdev->dev, "cannot ioremap registers\n");
462 ret = -ENXIO;
463 goto err3;
466 pcm->pclk = clk_get(&pdev->dev, "pcm");
467 if (IS_ERR(pcm->pclk)) {
468 dev_err(&pdev->dev, "failed to get pcm_clock\n");
469 ret = -ENOENT;
470 goto err4;
472 clk_enable(pcm->pclk);
474 ret = snd_soc_register_dai(&pdev->dev, &s3c_pcm_dai[pdev->id]);
475 if (ret != 0) {
476 dev_err(&pdev->dev, "failed to get pcm_clock\n");
477 goto err5;
480 s3c_pcm_stereo_in[pdev->id].dma_addr = mem_res->start
481 + S3C_PCM_RXFIFO;
482 s3c_pcm_stereo_out[pdev->id].dma_addr = mem_res->start
483 + S3C_PCM_TXFIFO;
485 s3c_pcm_stereo_in[pdev->id].channel = dmarx_res->start;
486 s3c_pcm_stereo_out[pdev->id].channel = dmatx_res->start;
488 pcm->dma_capture = &s3c_pcm_stereo_in[pdev->id];
489 pcm->dma_playback = &s3c_pcm_stereo_out[pdev->id];
491 return 0;
493 err5:
494 clk_disable(pcm->pclk);
495 clk_put(pcm->pclk);
496 err4:
497 iounmap(pcm->regs);
498 err3:
499 release_mem_region(mem_res->start, resource_size(mem_res));
500 err2:
501 clk_disable(pcm->cclk);
502 clk_put(pcm->cclk);
503 err1:
504 return ret;
507 static __devexit int s3c_pcm_dev_remove(struct platform_device *pdev)
509 struct s3c_pcm_info *pcm = &s3c_pcm[pdev->id];
510 struct resource *mem_res;
512 snd_soc_unregister_dai(&pdev->dev);
514 iounmap(pcm->regs);
516 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
517 release_mem_region(mem_res->start, resource_size(mem_res));
519 clk_disable(pcm->cclk);
520 clk_disable(pcm->pclk);
521 clk_put(pcm->pclk);
522 clk_put(pcm->cclk);
524 return 0;
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,
536 static int __init s3c_pcm_init(void)
538 return platform_driver_register(&s3c_pcm_driver);
540 module_init(s3c_pcm_init);
542 static void __exit s3c_pcm_exit(void)
544 platform_driver_unregister(&s3c_pcm_driver);
546 module_exit(s3c_pcm_exit);
548 /* Module information */
549 MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
550 MODULE_DESCRIPTION("S3C PCM Controller Driver");
551 MODULE_LICENSE("GPL");
552 MODULE_ALIAS("platform:samsung-pcm");