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>
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>
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
[] = {
45 .client
= &s3c_pcm_dma_client_out
,
49 .client
= &s3c_pcm_dma_client_out
,
54 static struct s3c_dma_params s3c_pcm_stereo_in
[] = {
56 .client
= &s3c_pcm_dma_client_in
,
60 .client
= &s3c_pcm_dma_client_in
,
65 static struct s3c_pcm_info s3c_pcm
[2];
67 static inline struct s3c_pcm_info
*to_info(struct snd_soc_dai
*cpu_dai
)
69 return cpu_dai
->private_data
;
72 static void s3c_pcm_snd_txctrl(struct s3c_pcm_info
*pcm
, int on
)
74 void __iomem
*regs
= pcm
->regs
;
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
);
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
;
89 ctl
&= ~S3C_PCM_CTL_TXDMA_EN
;
90 ctl
&= ~S3C_PCM_CTL_TXFIFO_EN
;
92 if (!(ctl
& S3C_PCM_CTL_RXFIFO_EN
)) {
93 ctl
&= ~S3C_PCM_CTL_ENABLE
;
95 clkctl
|= S3C_PCM_CLKCTL_SERCLK_EN
;
99 writel(clkctl
, regs
+ S3C_PCM_CLKCTL
);
100 writel(ctl
, regs
+ S3C_PCM_CTL
);
103 static void s3c_pcm_snd_rxctrl(struct s3c_pcm_info
*pcm
, int on
)
105 void __iomem
*regs
= pcm
->regs
;
108 ctl
= readl(regs
+ S3C_PCM_CTL
);
109 clkctl
= readl(regs
+ S3C_PCM_CLKCTL
);
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
;
117 ctl
&= ~S3C_PCM_CTL_RXDMA_EN
;
118 ctl
&= ~S3C_PCM_CTL_RXFIFO_EN
;
120 if (!(ctl
& S3C_PCM_CTL_TXFIFO_EN
)) {
121 ctl
&= ~S3C_PCM_CTL_ENABLE
;
123 clkctl
|= S3C_PCM_CLKCTL_SERCLK_EN
;
127 writel(clkctl
, regs
+ S3C_PCM_CLKCTL
);
128 writel(ctl
, regs
+ S3C_PCM_CTL
);
131 static int s3c_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
,
132 struct snd_soc_dai
*dai
)
134 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
135 struct s3c_pcm_info
*pcm
= to_info(rtd
->dai
->cpu_dai
);
138 dev_dbg(pcm
->dev
, "Entered %s\n", __func__
);
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
);
146 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
147 s3c_pcm_snd_rxctrl(pcm
, 1);
149 s3c_pcm_snd_txctrl(pcm
, 1);
151 spin_unlock_irqrestore(&pcm
->lock
, flags
);
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
);
159 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
160 s3c_pcm_snd_rxctrl(pcm
, 0);
162 s3c_pcm_snd_txctrl(pcm
, 0);
164 spin_unlock_irqrestore(&pcm
->lock
, flags
);
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
)
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
;
183 int sclk_div
, sync_div
;
187 dev_dbg(pcm
->dev
, "Entered %s\n", __func__
);
189 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
190 dai
->cpu_dai
->dma_data
= pcm
->dma_playback
;
192 dai
->cpu_dai
->dma_data
= pcm
->dma_capture
;
194 /* Strictly check for sample size */
195 switch (params_format(params
)) {
196 case SNDRV_PCM_FORMAT_S16_LE
:
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
)
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 \
233 SCLK_DIV=%d SYNC_DIV=%d\n",
234 clk_get_rate(clk
), pcm
->sclk_per_fs
,
240 static int s3c_pcm_set_fmt(struct snd_soc_dai
*cpu_dai
,
243 struct s3c_pcm_info
*pcm
= to_info(cpu_dai
);
244 void __iomem
*regs
= pcm
->regs
;
249 dev_dbg(pcm
->dev
, "Entered %s\n", __func__
);
251 spin_lock_irqsave(&pcm
->lock
, flags
);
253 ctl
= readl(regs
+ S3C_PCM_CTL
);
255 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
256 case SND_SOC_DAIFMT_NB_NF
:
257 /* Nothing to do, NB_NF by default */
260 dev_err(pcm
->dev
, "Unsupported clock inversion!\n");
265 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
266 case SND_SOC_DAIFMT_CBS_CFS
:
267 /* Nothing to do, Master by default */
270 dev_err(pcm
->dev
, "Unsupported master/slave format!\n");
275 switch (fmt
& SND_SOC_DAIFMT_CLOCK_MASK
) {
276 case SND_SOC_DAIFMT_CONT
:
279 case SND_SOC_DAIFMT_GATED
:
283 dev_err(pcm
->dev
, "Invalid Clock gating request!\n");
288 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
289 case SND_SOC_DAIFMT_DSP_A
:
290 ctl
|= S3C_PCM_CTL_TXMSB_AFTER_FSYNC
;
291 ctl
|= S3C_PCM_CTL_RXMSB_AFTER_FSYNC
;
293 case SND_SOC_DAIFMT_DSP_B
:
294 ctl
&= ~S3C_PCM_CTL_TXMSB_AFTER_FSYNC
;
295 ctl
&= ~S3C_PCM_CTL_RXMSB_AFTER_FSYNC
;
298 dev_err(pcm
->dev
, "Unsupported data format!\n");
303 writel(ctl
, regs
+ S3C_PCM_CTL
);
306 spin_unlock_irqrestore(&pcm
->lock
, flags
);
311 static int s3c_pcm_set_clkdiv(struct snd_soc_dai
*cpu_dai
,
314 struct s3c_pcm_info
*pcm
= to_info(cpu_dai
);
317 case S3C_PCM_SCLK_PER_FS
:
318 pcm
->sclk_per_fs
= div
;
328 static int s3c_pcm_set_sysclk(struct snd_soc_dai
*cpu_dai
,
329 int clk_id
, unsigned int freq
, int dir
)
331 struct s3c_pcm_info
*pcm
= to_info(cpu_dai
);
332 void __iomem
*regs
= pcm
->regs
;
333 u32 clkctl
= readl(regs
+ S3C_PCM_CLKCTL
);
336 case S3C_PCM_CLKSRC_PCLK
:
337 clkctl
|= S3C_PCM_CLKCTL_SERCLKSEL_PCLK
;
340 case S3C_PCM_CLKSRC_MUX
:
341 clkctl
&= ~S3C_PCM_CLKCTL_SERCLKSEL_PCLK
;
343 if (clk_get_rate(pcm
->cclk
) != freq
)
344 clk_set_rate(pcm
->cclk
, freq
);
352 writel(clkctl
, regs
+ S3C_PCM_CLKCTL
);
357 static struct snd_soc_dai_ops s3c_pcm_dai_ops
= {
358 .set_sysclk
= s3c_pcm_set_sysclk
,
359 .set_clkdiv
= s3c_pcm_set_clkdiv
,
360 .trigger
= s3c_pcm_trigger
,
361 .hw_params
= s3c_pcm_hw_params
,
362 .set_fmt
= s3c_pcm_set_fmt
,
365 #define S3C_PCM_RATES SNDRV_PCM_RATE_8000_96000
367 #define S3C_PCM_DECLARE(n) \
369 .name = "samsung-pcm", \
371 .symmetric_rates = 1, \
372 .ops = &s3c_pcm_dai_ops, \
376 .rates = S3C_PCM_RATES, \
377 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
382 .rates = S3C_PCM_RATES, \
383 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
387 struct snd_soc_dai s3c_pcm_dai
[] = {
391 EXPORT_SYMBOL_GPL(s3c_pcm_dai
);
393 static __devinit
int s3c_pcm_dev_probe(struct platform_device
*pdev
)
395 struct s3c_pcm_info
*pcm
;
396 struct snd_soc_dai
*dai
;
397 struct resource
*mem_res
, *dmatx_res
, *dmarx_res
;
398 struct s3c_audio_pdata
*pcm_pdata
;
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
);
407 pcm_pdata
= pdev
->dev
.platform_data
;
409 /* Check for availability of necessary resource */
410 dmatx_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
412 dev_err(&pdev
->dev
, "Unable to get PCM-TX dma resource\n");
416 dmarx_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
418 dev_err(&pdev
->dev
, "Unable to get PCM-RX dma resource\n");
422 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
424 dev_err(&pdev
->dev
, "Unable to get register resource\n");
428 if (pcm_pdata
&& pcm_pdata
->cfg_gpio
&& pcm_pdata
->cfg_gpio(pdev
)) {
429 dev_err(&pdev
->dev
, "Unable to configure gpio\n");
433 pcm
= &s3c_pcm
[pdev
->id
];
434 pcm
->dev
= &pdev
->dev
;
436 spin_lock_init(&pcm
->lock
);
438 dai
= &s3c_pcm_dai
[pdev
->id
];
439 dai
->dev
= &pdev
->dev
;
441 /* Default is 128fs */
442 pcm
->sclk_per_fs
= 128;
444 pcm
->cclk
= clk_get(&pdev
->dev
, "audio-bus");
445 if (IS_ERR(pcm
->cclk
)) {
446 dev_err(&pdev
->dev
, "failed to get audio-bus\n");
447 ret
= PTR_ERR(pcm
->cclk
);
450 clk_enable(pcm
->cclk
);
452 /* record our pcm structure for later use in the callbacks */
453 dai
->private_data
= pcm
;
455 if (!request_mem_region(mem_res
->start
,
456 resource_size(mem_res
), "samsung-pcm")) {
457 dev_err(&pdev
->dev
, "Unable to request register region\n");
462 pcm
->regs
= ioremap(mem_res
->start
, 0x100);
463 if (pcm
->regs
== NULL
) {
464 dev_err(&pdev
->dev
, "cannot ioremap registers\n");
469 pcm
->pclk
= clk_get(&pdev
->dev
, "pcm");
470 if (IS_ERR(pcm
->pclk
)) {
471 dev_err(&pdev
->dev
, "failed to get pcm_clock\n");
475 clk_enable(pcm
->pclk
);
477 ret
= snd_soc_register_dai(dai
);
479 dev_err(&pdev
->dev
, "failed to get pcm_clock\n");
483 s3c_pcm_stereo_in
[pdev
->id
].dma_addr
= mem_res
->start
485 s3c_pcm_stereo_out
[pdev
->id
].dma_addr
= mem_res
->start
488 s3c_pcm_stereo_in
[pdev
->id
].channel
= dmarx_res
->start
;
489 s3c_pcm_stereo_out
[pdev
->id
].channel
= dmatx_res
->start
;
491 pcm
->dma_capture
= &s3c_pcm_stereo_in
[pdev
->id
];
492 pcm
->dma_playback
= &s3c_pcm_stereo_out
[pdev
->id
];
497 clk_disable(pcm
->pclk
);
502 release_mem_region(mem_res
->start
, resource_size(mem_res
));
504 clk_disable(pcm
->cclk
);
510 static __devexit
int s3c_pcm_dev_remove(struct platform_device
*pdev
)
512 struct s3c_pcm_info
*pcm
= &s3c_pcm
[pdev
->id
];
513 struct resource
*mem_res
;
517 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
518 release_mem_region(mem_res
->start
, resource_size(mem_res
));
520 clk_disable(pcm
->cclk
);
521 clk_disable(pcm
->pclk
);
528 static struct platform_driver s3c_pcm_driver
= {
529 .probe
= s3c_pcm_dev_probe
,
530 .remove
= s3c_pcm_dev_remove
,
532 .name
= "samsung-pcm",
533 .owner
= THIS_MODULE
,
537 static int __init
s3c_pcm_init(void)
539 return platform_driver_register(&s3c_pcm_driver
);
541 module_init(s3c_pcm_init
);
543 static void __exit
s3c_pcm_exit(void)
545 platform_driver_unregister(&s3c_pcm_driver
);
547 module_exit(s3c_pcm_exit
);
549 /* Module information */
550 MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
551 MODULE_DESCRIPTION("S3C PCM Controller Driver");
552 MODULE_LICENSE("GPL");