1 // SPDX-License-Identifier: GPL-2.0
3 // ALSA SoC Audio Layer - S3C PCM-Controller driver
5 // Copyright (c) 2009 Samsung Electronics Co. Ltd
6 // Author: Jaswinder Singh <jassisinghbrar@gmail.com>
7 // based upon I2S drivers by Ben Dooks.
11 #include <linux/module.h>
12 #include <linux/pm_runtime.h>
14 #include <sound/soc.h>
15 #include <sound/pcm_params.h>
17 #include <linux/platform_data/asoc-s3c.h>
23 #define S3C_PCM_CTL 0x00
24 #define S3C_PCM_CLKCTL 0x04
25 #define S3C_PCM_TXFIFO 0x08
26 #define S3C_PCM_RXFIFO 0x0C
27 #define S3C_PCM_IRQCTL 0x10
28 #define S3C_PCM_IRQSTAT 0x14
29 #define S3C_PCM_FIFOSTAT 0x18
30 #define S3C_PCM_CLRINT 0x20
32 /* PCM_CTL Bit-Fields */
33 #define S3C_PCM_CTL_TXDIPSTICK_MASK 0x3f
34 #define S3C_PCM_CTL_TXDIPSTICK_SHIFT 13
35 #define S3C_PCM_CTL_RXDIPSTICK_MASK 0x3f
36 #define S3C_PCM_CTL_RXDIPSTICK_SHIFT 7
37 #define S3C_PCM_CTL_TXDMA_EN (0x1 << 6)
38 #define S3C_PCM_CTL_RXDMA_EN (0x1 << 5)
39 #define S3C_PCM_CTL_TXMSB_AFTER_FSYNC (0x1 << 4)
40 #define S3C_PCM_CTL_RXMSB_AFTER_FSYNC (0x1 << 3)
41 #define S3C_PCM_CTL_TXFIFO_EN (0x1 << 2)
42 #define S3C_PCM_CTL_RXFIFO_EN (0x1 << 1)
43 #define S3C_PCM_CTL_ENABLE (0x1 << 0)
45 /* PCM_CLKCTL Bit-Fields */
46 #define S3C_PCM_CLKCTL_SERCLK_EN (0x1 << 19)
47 #define S3C_PCM_CLKCTL_SERCLKSEL_PCLK (0x1 << 18)
48 #define S3C_PCM_CLKCTL_SCLKDIV_MASK 0x1ff
49 #define S3C_PCM_CLKCTL_SYNCDIV_MASK 0x1ff
50 #define S3C_PCM_CLKCTL_SCLKDIV_SHIFT 9
51 #define S3C_PCM_CLKCTL_SYNCDIV_SHIFT 0
53 /* PCM_TXFIFO Bit-Fields */
54 #define S3C_PCM_TXFIFO_DVALID (0x1 << 16)
55 #define S3C_PCM_TXFIFO_DATA_MSK (0xffff << 0)
57 /* PCM_RXFIFO Bit-Fields */
58 #define S3C_PCM_RXFIFO_DVALID (0x1 << 16)
59 #define S3C_PCM_RXFIFO_DATA_MSK (0xffff << 0)
61 /* PCM_IRQCTL Bit-Fields */
62 #define S3C_PCM_IRQCTL_IRQEN (0x1 << 14)
63 #define S3C_PCM_IRQCTL_WRDEN (0x1 << 12)
64 #define S3C_PCM_IRQCTL_TXEMPTYEN (0x1 << 11)
65 #define S3C_PCM_IRQCTL_TXALMSTEMPTYEN (0x1 << 10)
66 #define S3C_PCM_IRQCTL_TXFULLEN (0x1 << 9)
67 #define S3C_PCM_IRQCTL_TXALMSTFULLEN (0x1 << 8)
68 #define S3C_PCM_IRQCTL_TXSTARVEN (0x1 << 7)
69 #define S3C_PCM_IRQCTL_TXERROVRFLEN (0x1 << 6)
70 #define S3C_PCM_IRQCTL_RXEMPTEN (0x1 << 5)
71 #define S3C_PCM_IRQCTL_RXALMSTEMPTEN (0x1 << 4)
72 #define S3C_PCM_IRQCTL_RXFULLEN (0x1 << 3)
73 #define S3C_PCM_IRQCTL_RXALMSTFULLEN (0x1 << 2)
74 #define S3C_PCM_IRQCTL_RXSTARVEN (0x1 << 1)
75 #define S3C_PCM_IRQCTL_RXERROVRFLEN (0x1 << 0)
77 /* PCM_IRQSTAT Bit-Fields */
78 #define S3C_PCM_IRQSTAT_IRQPND (0x1 << 13)
79 #define S3C_PCM_IRQSTAT_WRD_XFER (0x1 << 12)
80 #define S3C_PCM_IRQSTAT_TXEMPTY (0x1 << 11)
81 #define S3C_PCM_IRQSTAT_TXALMSTEMPTY (0x1 << 10)
82 #define S3C_PCM_IRQSTAT_TXFULL (0x1 << 9)
83 #define S3C_PCM_IRQSTAT_TXALMSTFULL (0x1 << 8)
84 #define S3C_PCM_IRQSTAT_TXSTARV (0x1 << 7)
85 #define S3C_PCM_IRQSTAT_TXERROVRFL (0x1 << 6)
86 #define S3C_PCM_IRQSTAT_RXEMPT (0x1 << 5)
87 #define S3C_PCM_IRQSTAT_RXALMSTEMPT (0x1 << 4)
88 #define S3C_PCM_IRQSTAT_RXFULL (0x1 << 3)
89 #define S3C_PCM_IRQSTAT_RXALMSTFULL (0x1 << 2)
90 #define S3C_PCM_IRQSTAT_RXSTARV (0x1 << 1)
91 #define S3C_PCM_IRQSTAT_RXERROVRFL (0x1 << 0)
93 /* PCM_FIFOSTAT Bit-Fields */
94 #define S3C_PCM_FIFOSTAT_TXCNT_MSK (0x3f << 14)
95 #define S3C_PCM_FIFOSTAT_TXFIFOEMPTY (0x1 << 13)
96 #define S3C_PCM_FIFOSTAT_TXFIFOALMSTEMPTY (0x1 << 12)
97 #define S3C_PCM_FIFOSTAT_TXFIFOFULL (0x1 << 11)
98 #define S3C_PCM_FIFOSTAT_TXFIFOALMSTFULL (0x1 << 10)
99 #define S3C_PCM_FIFOSTAT_RXCNT_MSK (0x3f << 4)
100 #define S3C_PCM_FIFOSTAT_RXFIFOEMPTY (0x1 << 3)
101 #define S3C_PCM_FIFOSTAT_RXFIFOALMSTEMPTY (0x1 << 2)
102 #define S3C_PCM_FIFOSTAT_RXFIFOFULL (0x1 << 1)
103 #define S3C_PCM_FIFOSTAT_RXFIFOALMSTFULL (0x1 << 0)
106 * struct s3c_pcm_info - S3C PCM Controller information
107 * @dev: The parent device passed to use from the probe.
108 * @regs: The pointer to the device register block.
109 * @dma_playback: DMA information for playback channel.
110 * @dma_capture: DMA information for capture channel.
112 struct s3c_pcm_info
{
117 unsigned int sclk_per_fs
;
119 /* Whether to keep PCMSCLK enabled even when idle(no active xfer) */
120 unsigned int idleclk
;
125 struct snd_dmaengine_dai_dma_data
*dma_playback
;
126 struct snd_dmaengine_dai_dma_data
*dma_capture
;
129 static struct snd_dmaengine_dai_dma_data s3c_pcm_stereo_out
[] = {
138 static struct snd_dmaengine_dai_dma_data s3c_pcm_stereo_in
[] = {
147 static struct s3c_pcm_info s3c_pcm
[2];
149 static void s3c_pcm_snd_txctrl(struct s3c_pcm_info
*pcm
, int on
)
151 void __iomem
*regs
= pcm
->regs
;
154 clkctl
= readl(regs
+ S3C_PCM_CLKCTL
);
155 ctl
= readl(regs
+ S3C_PCM_CTL
);
156 ctl
&= ~(S3C_PCM_CTL_TXDIPSTICK_MASK
157 << S3C_PCM_CTL_TXDIPSTICK_SHIFT
);
160 ctl
|= S3C_PCM_CTL_TXDMA_EN
;
161 ctl
|= S3C_PCM_CTL_TXFIFO_EN
;
162 ctl
|= S3C_PCM_CTL_ENABLE
;
163 ctl
|= (0x4<<S3C_PCM_CTL_TXDIPSTICK_SHIFT
);
164 clkctl
|= S3C_PCM_CLKCTL_SERCLK_EN
;
166 ctl
&= ~S3C_PCM_CTL_TXDMA_EN
;
167 ctl
&= ~S3C_PCM_CTL_TXFIFO_EN
;
169 if (!(ctl
& S3C_PCM_CTL_RXFIFO_EN
)) {
170 ctl
&= ~S3C_PCM_CTL_ENABLE
;
172 clkctl
|= S3C_PCM_CLKCTL_SERCLK_EN
;
176 writel(clkctl
, regs
+ S3C_PCM_CLKCTL
);
177 writel(ctl
, regs
+ S3C_PCM_CTL
);
180 static void s3c_pcm_snd_rxctrl(struct s3c_pcm_info
*pcm
, int on
)
182 void __iomem
*regs
= pcm
->regs
;
185 ctl
= readl(regs
+ S3C_PCM_CTL
);
186 clkctl
= readl(regs
+ S3C_PCM_CLKCTL
);
187 ctl
&= ~(S3C_PCM_CTL_RXDIPSTICK_MASK
188 << S3C_PCM_CTL_RXDIPSTICK_SHIFT
);
191 ctl
|= S3C_PCM_CTL_RXDMA_EN
;
192 ctl
|= S3C_PCM_CTL_RXFIFO_EN
;
193 ctl
|= S3C_PCM_CTL_ENABLE
;
194 ctl
|= (0x20<<S3C_PCM_CTL_RXDIPSTICK_SHIFT
);
195 clkctl
|= S3C_PCM_CLKCTL_SERCLK_EN
;
197 ctl
&= ~S3C_PCM_CTL_RXDMA_EN
;
198 ctl
&= ~S3C_PCM_CTL_RXFIFO_EN
;
200 if (!(ctl
& S3C_PCM_CTL_TXFIFO_EN
)) {
201 ctl
&= ~S3C_PCM_CTL_ENABLE
;
203 clkctl
|= S3C_PCM_CLKCTL_SERCLK_EN
;
207 writel(clkctl
, regs
+ S3C_PCM_CLKCTL
);
208 writel(ctl
, regs
+ S3C_PCM_CTL
);
211 static int s3c_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
,
212 struct snd_soc_dai
*dai
)
214 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
215 struct s3c_pcm_info
*pcm
= snd_soc_dai_get_drvdata(rtd
->cpu_dai
);
218 dev_dbg(pcm
->dev
, "Entered %s\n", __func__
);
221 case SNDRV_PCM_TRIGGER_START
:
222 case SNDRV_PCM_TRIGGER_RESUME
:
223 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
224 spin_lock_irqsave(&pcm
->lock
, flags
);
226 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
227 s3c_pcm_snd_rxctrl(pcm
, 1);
229 s3c_pcm_snd_txctrl(pcm
, 1);
231 spin_unlock_irqrestore(&pcm
->lock
, flags
);
234 case SNDRV_PCM_TRIGGER_STOP
:
235 case SNDRV_PCM_TRIGGER_SUSPEND
:
236 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
237 spin_lock_irqsave(&pcm
->lock
, flags
);
239 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
240 s3c_pcm_snd_rxctrl(pcm
, 0);
242 s3c_pcm_snd_txctrl(pcm
, 0);
244 spin_unlock_irqrestore(&pcm
->lock
, flags
);
254 static int s3c_pcm_hw_params(struct snd_pcm_substream
*substream
,
255 struct snd_pcm_hw_params
*params
,
256 struct snd_soc_dai
*socdai
)
258 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
259 struct s3c_pcm_info
*pcm
= snd_soc_dai_get_drvdata(rtd
->cpu_dai
);
260 void __iomem
*regs
= pcm
->regs
;
262 int sclk_div
, sync_div
;
266 dev_dbg(pcm
->dev
, "Entered %s\n", __func__
);
268 /* Strictly check for sample size */
269 switch (params_width(params
)) {
276 spin_lock_irqsave(&pcm
->lock
, flags
);
278 /* Get hold of the PCMSOURCE_CLK */
279 clkctl
= readl(regs
+ S3C_PCM_CLKCTL
);
280 if (clkctl
& S3C_PCM_CLKCTL_SERCLKSEL_PCLK
)
285 /* Set the SCLK divider */
286 sclk_div
= clk_get_rate(clk
) / pcm
->sclk_per_fs
/
287 params_rate(params
) / 2 - 1;
289 clkctl
&= ~(S3C_PCM_CLKCTL_SCLKDIV_MASK
290 << S3C_PCM_CLKCTL_SCLKDIV_SHIFT
);
291 clkctl
|= ((sclk_div
& S3C_PCM_CLKCTL_SCLKDIV_MASK
)
292 << S3C_PCM_CLKCTL_SCLKDIV_SHIFT
);
294 /* Set the SYNC divider */
295 sync_div
= pcm
->sclk_per_fs
- 1;
297 clkctl
&= ~(S3C_PCM_CLKCTL_SYNCDIV_MASK
298 << S3C_PCM_CLKCTL_SYNCDIV_SHIFT
);
299 clkctl
|= ((sync_div
& S3C_PCM_CLKCTL_SYNCDIV_MASK
)
300 << S3C_PCM_CLKCTL_SYNCDIV_SHIFT
);
302 writel(clkctl
, regs
+ S3C_PCM_CLKCTL
);
304 spin_unlock_irqrestore(&pcm
->lock
, flags
);
306 dev_dbg(pcm
->dev
, "PCMSOURCE_CLK-%lu SCLK=%ufs SCLK_DIV=%d SYNC_DIV=%d\n",
307 clk_get_rate(clk
), pcm
->sclk_per_fs
,
313 static int s3c_pcm_set_fmt(struct snd_soc_dai
*cpu_dai
,
316 struct s3c_pcm_info
*pcm
= snd_soc_dai_get_drvdata(cpu_dai
);
317 void __iomem
*regs
= pcm
->regs
;
322 dev_dbg(pcm
->dev
, "Entered %s\n", __func__
);
324 spin_lock_irqsave(&pcm
->lock
, flags
);
326 ctl
= readl(regs
+ S3C_PCM_CTL
);
328 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
329 case SND_SOC_DAIFMT_IB_NF
:
330 /* Nothing to do, IB_NF by default */
333 dev_err(pcm
->dev
, "Unsupported clock inversion!\n");
338 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
339 case SND_SOC_DAIFMT_CBS_CFS
:
340 /* Nothing to do, Master by default */
343 dev_err(pcm
->dev
, "Unsupported master/slave format!\n");
348 switch (fmt
& SND_SOC_DAIFMT_CLOCK_MASK
) {
349 case SND_SOC_DAIFMT_CONT
:
352 case SND_SOC_DAIFMT_GATED
:
356 dev_err(pcm
->dev
, "Invalid Clock gating request!\n");
361 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
362 case SND_SOC_DAIFMT_DSP_A
:
363 ctl
|= S3C_PCM_CTL_TXMSB_AFTER_FSYNC
;
364 ctl
|= S3C_PCM_CTL_RXMSB_AFTER_FSYNC
;
366 case SND_SOC_DAIFMT_DSP_B
:
367 ctl
&= ~S3C_PCM_CTL_TXMSB_AFTER_FSYNC
;
368 ctl
&= ~S3C_PCM_CTL_RXMSB_AFTER_FSYNC
;
371 dev_err(pcm
->dev
, "Unsupported data format!\n");
376 writel(ctl
, regs
+ S3C_PCM_CTL
);
379 spin_unlock_irqrestore(&pcm
->lock
, flags
);
384 static int s3c_pcm_set_clkdiv(struct snd_soc_dai
*cpu_dai
,
387 struct s3c_pcm_info
*pcm
= snd_soc_dai_get_drvdata(cpu_dai
);
390 case S3C_PCM_SCLK_PER_FS
:
391 pcm
->sclk_per_fs
= div
;
401 static int s3c_pcm_set_sysclk(struct snd_soc_dai
*cpu_dai
,
402 int clk_id
, unsigned int freq
, int dir
)
404 struct s3c_pcm_info
*pcm
= snd_soc_dai_get_drvdata(cpu_dai
);
405 void __iomem
*regs
= pcm
->regs
;
406 u32 clkctl
= readl(regs
+ S3C_PCM_CLKCTL
);
409 case S3C_PCM_CLKSRC_PCLK
:
410 clkctl
|= S3C_PCM_CLKCTL_SERCLKSEL_PCLK
;
413 case S3C_PCM_CLKSRC_MUX
:
414 clkctl
&= ~S3C_PCM_CLKCTL_SERCLKSEL_PCLK
;
416 if (clk_get_rate(pcm
->cclk
) != freq
)
417 clk_set_rate(pcm
->cclk
, freq
);
425 writel(clkctl
, regs
+ S3C_PCM_CLKCTL
);
430 static const struct snd_soc_dai_ops s3c_pcm_dai_ops
= {
431 .set_sysclk
= s3c_pcm_set_sysclk
,
432 .set_clkdiv
= s3c_pcm_set_clkdiv
,
433 .trigger
= s3c_pcm_trigger
,
434 .hw_params
= s3c_pcm_hw_params
,
435 .set_fmt
= s3c_pcm_set_fmt
,
438 static int s3c_pcm_dai_probe(struct snd_soc_dai
*dai
)
440 struct s3c_pcm_info
*pcm
= snd_soc_dai_get_drvdata(dai
);
442 snd_soc_dai_init_dma_data(dai
, pcm
->dma_playback
, pcm
->dma_capture
);
447 #define S3C_PCM_RATES SNDRV_PCM_RATE_8000_96000
449 #define S3C_PCM_DAI_DECLARE \
450 .symmetric_rates = 1, \
451 .probe = s3c_pcm_dai_probe, \
452 .ops = &s3c_pcm_dai_ops, \
456 .rates = S3C_PCM_RATES, \
457 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
462 .rates = S3C_PCM_RATES, \
463 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
466 static struct snd_soc_dai_driver s3c_pcm_dai
[] = {
468 .name
= "samsung-pcm.0",
472 .name
= "samsung-pcm.1",
477 static const struct snd_soc_component_driver s3c_pcm_component
= {
481 static int s3c_pcm_dev_probe(struct platform_device
*pdev
)
483 struct s3c_pcm_info
*pcm
;
484 struct resource
*mem_res
;
485 struct s3c_audio_pdata
*pcm_pdata
;
486 dma_filter_fn filter
;
489 /* Check for valid device index */
490 if ((pdev
->id
< 0) || pdev
->id
>= ARRAY_SIZE(s3c_pcm
)) {
491 dev_err(&pdev
->dev
, "id %d out of range\n", pdev
->id
);
495 pcm_pdata
= pdev
->dev
.platform_data
;
497 if (pcm_pdata
&& pcm_pdata
->cfg_gpio
&& pcm_pdata
->cfg_gpio(pdev
)) {
498 dev_err(&pdev
->dev
, "Unable to configure gpio\n");
502 pcm
= &s3c_pcm
[pdev
->id
];
503 pcm
->dev
= &pdev
->dev
;
505 spin_lock_init(&pcm
->lock
);
507 /* Default is 128fs */
508 pcm
->sclk_per_fs
= 128;
510 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
511 pcm
->regs
= devm_ioremap_resource(&pdev
->dev
, mem_res
);
512 if (IS_ERR(pcm
->regs
))
513 return PTR_ERR(pcm
->regs
);
515 pcm
->cclk
= devm_clk_get(&pdev
->dev
, "audio-bus");
516 if (IS_ERR(pcm
->cclk
)) {
517 dev_err(&pdev
->dev
, "failed to get audio-bus clock\n");
518 return PTR_ERR(pcm
->cclk
);
520 ret
= clk_prepare_enable(pcm
->cclk
);
524 /* record our pcm structure for later use in the callbacks */
525 dev_set_drvdata(&pdev
->dev
, pcm
);
527 pcm
->pclk
= devm_clk_get(&pdev
->dev
, "pcm");
528 if (IS_ERR(pcm
->pclk
)) {
529 dev_err(&pdev
->dev
, "failed to get pcm clock\n");
530 ret
= PTR_ERR(pcm
->pclk
);
533 ret
= clk_prepare_enable(pcm
->pclk
);
537 s3c_pcm_stereo_in
[pdev
->id
].addr
= mem_res
->start
+ S3C_PCM_RXFIFO
;
538 s3c_pcm_stereo_out
[pdev
->id
].addr
= mem_res
->start
+ S3C_PCM_TXFIFO
;
542 s3c_pcm_stereo_in
[pdev
->id
].filter_data
= pcm_pdata
->dma_capture
;
543 s3c_pcm_stereo_out
[pdev
->id
].filter_data
= pcm_pdata
->dma_playback
;
544 filter
= pcm_pdata
->dma_filter
;
547 pcm
->dma_capture
= &s3c_pcm_stereo_in
[pdev
->id
];
548 pcm
->dma_playback
= &s3c_pcm_stereo_out
[pdev
->id
];
550 ret
= samsung_asoc_dma_platform_register(&pdev
->dev
, filter
,
553 dev_err(&pdev
->dev
, "failed to get register DMA: %d\n", ret
);
557 pm_runtime_enable(&pdev
->dev
);
559 ret
= devm_snd_soc_register_component(&pdev
->dev
, &s3c_pcm_component
,
560 &s3c_pcm_dai
[pdev
->id
], 1);
562 dev_err(&pdev
->dev
, "failed to get register DAI: %d\n", ret
);
569 pm_runtime_disable(&pdev
->dev
);
571 clk_disable_unprepare(pcm
->pclk
);
573 clk_disable_unprepare(pcm
->cclk
);
577 static int s3c_pcm_dev_remove(struct platform_device
*pdev
)
579 struct s3c_pcm_info
*pcm
= &s3c_pcm
[pdev
->id
];
581 pm_runtime_disable(&pdev
->dev
);
582 clk_disable_unprepare(pcm
->cclk
);
583 clk_disable_unprepare(pcm
->pclk
);
588 static struct platform_driver s3c_pcm_driver
= {
589 .probe
= s3c_pcm_dev_probe
,
590 .remove
= s3c_pcm_dev_remove
,
592 .name
= "samsung-pcm",
596 module_platform_driver(s3c_pcm_driver
);
598 /* Module information */
599 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
600 MODULE_DESCRIPTION("S3C PCM Controller Driver");
601 MODULE_LICENSE("GPL");
602 MODULE_ALIAS("platform:samsung-pcm");