1 // SPDX-License-Identifier: GPL-2.0
3 * TDM driver for the StarFive JH7110 SoC
5 * Copyright (C) 2022 StarFive Technology Co., Ltd.
8 #include <linux/device.h>
9 #include <linux/reset.h>
10 #include <linux/module.h>
11 #include <linux/of_irq.h>
12 #include <linux/of_platform.h>
13 #include <linux/regmap.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/dma/starfive-dma.h>
16 #include <sound/soc.h>
17 #include <sound/soc-dai.h>
18 #include <sound/pcm_params.h>
19 #include <sound/initval.h>
20 #include <sound/tlv.h>
21 #include "starfive_tdm.h"
23 static inline u32 sf_tdm_readl(struct sf_tdm_dev *dev, u16 reg)
25 return readl_relaxed(dev->tdm_base + reg);
28 static inline void sf_tdm_writel(struct sf_tdm_dev *dev, u16 reg, u32 val)
30 writel_relaxed(val, dev->tdm_base + reg);
33 static void sf_tdm_save_context(struct sf_tdm_dev *dev)
35 dev->saved_reg_value[0] = sf_tdm_readl(dev, TDM_PCMGBCR);
36 dev->saved_reg_value[1] = sf_tdm_readl(dev, TDM_PCMTXCR);
37 dev->saved_reg_value[2] = sf_tdm_readl(dev, TDM_PCMRXCR);
38 dev->saved_reg_value[3] = sf_tdm_readl(dev, TDM_PCMDIV);
41 static void sf_tdm_restore_context(struct sf_tdm_dev *dev)
43 sf_tdm_writel(dev, TDM_PCMGBCR, dev->saved_reg_value[0]);
44 sf_tdm_writel(dev, TDM_PCMTXCR, dev->saved_reg_value[1]);
45 sf_tdm_writel(dev, TDM_PCMRXCR, dev->saved_reg_value[2]);
46 sf_tdm_writel(dev, TDM_PCMDIV, dev->saved_reg_value[3]);
49 static void sf_tdm_start(struct sf_tdm_dev *dev, struct snd_pcm_substream *substream)
54 data = sf_tdm_readl(dev, TDM_PCMGBCR);
55 sf_tdm_writel(dev, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
57 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
58 val = sf_tdm_readl(dev, TDM_PCMTXCR);
59 sf_tdm_writel(dev, TDM_PCMTXCR, val | PCMTXCR_TXEN);
61 val = sf_tdm_readl(dev, TDM_PCMRXCR);
62 sf_tdm_writel(dev, TDM_PCMRXCR, val | PCMRXCR_RXEN);
66 static void sf_tdm_stop(struct sf_tdm_dev *dev, struct snd_pcm_substream *substream)
70 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
71 val = sf_tdm_readl(dev, TDM_PCMTXCR);
73 sf_tdm_writel(dev, TDM_PCMTXCR, val);
75 val = sf_tdm_readl(dev, TDM_PCMRXCR);
77 sf_tdm_writel(dev, TDM_PCMRXCR, val);
80 val = sf_tdm_readl(dev, TDM_PCMGBCR);
81 val &= ~PCMGBCR_ENABLE;
82 sf_tdm_writel(dev, TDM_PCMGBCR, val);
85 static int sf_tdm_syncdiv(struct sf_tdm_dev *dev)
87 u32 sl, sscale, syncdiv;
89 sl = (dev->rx.sl >= dev->tx.sl) ? dev->rx.sl:dev->tx.sl;
90 sscale = (dev->rx.sscale >= dev->tx.sscale) ? dev->rx.sscale:dev->tx.sscale;
91 syncdiv = dev->pcmclk / dev->samplerate - 1;
93 if ((syncdiv + 1) < (sl * sscale)) {
94 pr_info("set syncdiv failed !\n");
98 if ((dev->syncm == TDM_SYNCM_LONG) &&
99 ((dev->rx.sscale <= 1) || (dev->tx.sscale <= 1))) {
100 if ((syncdiv + 1) <= sl) {
101 pr_info("set syncdiv failed! it must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
106 sf_tdm_writel(dev, TDM_PCMDIV, syncdiv);
110 static void sf_tdm_contrl(struct sf_tdm_dev *dev)
114 data = (dev->clkpolity << CLKPOL_BIT) |
115 (dev->elm << ELM_BIT) |
116 (dev->syncm << SYNCM_BIT) |
117 (dev->ms_mode << MS_BIT);
118 sf_tdm_writel(dev, TDM_PCMGBCR, data);
121 static void sf_tdm_config(struct sf_tdm_dev *dev, struct snd_pcm_substream *substream)
128 datarx = (dev->rx.ifl << IFL_BIT) |
129 (dev->rx.wl << WL_BIT) |
130 (dev->rx.sscale << SSCALE_BIT) |
131 (dev->rx.sl << SL_BIT) |
132 (dev->rx.lrj << LRJ_BIT);
134 datatx = (dev->tx.ifl << IFL_BIT) |
135 (dev->tx.wl << WL_BIT) |
136 (dev->tx.sscale << SSCALE_BIT) |
137 (dev->tx.sl << SL_BIT) |
138 (dev->tx.lrj << LRJ_BIT);
140 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
141 sf_tdm_writel(dev, TDM_PCMTXCR, datatx);
143 sf_tdm_writel(dev, TDM_PCMRXCR, datarx);
146 static void sf_tdm_clk_disable(struct sf_tdm_dev *priv)
148 clk_disable_unprepare(priv->clk_tdm);
149 clk_disable_unprepare(priv->clk_tdm_ext);
150 clk_disable_unprepare(priv->clk_tdm_internal);
151 clk_disable_unprepare(priv->clk_tdm_apb);
152 clk_disable_unprepare(priv->clk_apb0);
153 clk_disable_unprepare(priv->clk_tdm_ahb);
154 clk_disable_unprepare(priv->clk_ahb0);
155 clk_disable_unprepare(priv->clk_mclk_inner);
159 static int sf_tdm_runtime_suspend(struct device *dev)
161 struct sf_tdm_dev *priv = dev_get_drvdata(dev);
163 sf_tdm_clk_disable(priv);
167 static int sf_tdm_runtime_resume(struct device *dev)
169 struct sf_tdm_dev *priv = dev_get_drvdata(dev);
172 ret = clk_prepare_enable(priv->clk_mclk_inner);
174 dev_err(dev, "failed to prepare enable clk_mclk_inner\n");
178 ret = clk_prepare_enable(priv->clk_ahb0);
180 dev_err(dev, "Failed to prepare enable clk_ahb0\n");
184 ret = clk_prepare_enable(priv->clk_tdm_ahb);
186 dev_err(dev, "Failed to prepare enable clk_tdm_ahb\n");
190 ret = clk_prepare_enable(priv->clk_apb0);
192 dev_err(dev, "Failed to prepare enable clk_apb0\n");
196 ret = clk_prepare_enable(priv->clk_tdm_apb);
198 dev_err(dev, "Failed to prepare enable clk_tdm_apb\n");
202 ret = clk_prepare_enable(priv->clk_tdm_internal);
204 dev_err(dev, "Failed to prepare enable clk_tdm_intl\n");
208 ret = clk_prepare_enable(priv->clk_tdm_ext);
210 dev_err(dev, "Failed to prepare enable clk_tdm_ext\n");
211 goto dis_tdm_internal;
214 ret = clk_prepare_enable(priv->clk_tdm);
216 dev_err(dev, "Failed to prepare enable clk_tdm\n");
220 ret = reset_control_deassert(priv->resets);
222 dev_err(dev, "Failed to deassert tdm resets\n");
229 clk_disable_unprepare(priv->clk_tdm);
231 clk_disable_unprepare(priv->clk_tdm_ext);
233 clk_disable_unprepare(priv->clk_tdm_internal);
235 clk_disable_unprepare(priv->clk_tdm_apb);
237 clk_disable_unprepare(priv->clk_tdm_ahb);
239 clk_disable_unprepare(priv->clk_mclk_inner);
245 #ifdef CONFIG_PM_SLEEP
246 static int sf_tdm_suspend(struct snd_soc_component *component)
248 return pm_runtime_force_suspend(component->dev);
251 static int sf_tdm_resume(struct snd_soc_component *component)
253 return pm_runtime_force_resume(component->dev);
257 #define sf_tdm_suspend NULL
258 #define sf_tdm_resume NULL
262 * To stop dma first, we must implement this function, because it is
263 * called before stopping the stream.
265 static int sf_pcm_trigger(struct snd_soc_component *component,
266 struct snd_pcm_substream *substream, int cmd)
269 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
272 case SNDRV_PCM_TRIGGER_START:
273 case SNDRV_PCM_TRIGGER_RESUME:
274 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
277 case SNDRV_PCM_TRIGGER_STOP:
278 case SNDRV_PCM_TRIGGER_SUSPEND:
279 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
280 axi_dma_cyclic_stop(chan);
290 static const struct snd_soc_component_driver sf_tdm_component = {
291 .name = "jh7110-tdm",
292 .suspend = sf_tdm_suspend,
293 .resume = sf_tdm_resume,
294 .trigger = sf_pcm_trigger,
297 static int sf_tdm_hw_params(struct snd_pcm_substream *substream,
298 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
300 struct sf_tdm_dev *dev = snd_soc_dai_get_drvdata(dai);
301 int chan_wl, chan_sl, chan_nr;
302 unsigned int data_width;
303 unsigned int mclk_rate;
304 unsigned int dma_bus_width;
307 struct snd_dmaengine_dai_dma_data *dma_data = NULL;
308 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
309 struct snd_soc_dai_link *dai_link = rtd->dai_link;
311 dai_link->stop_dma_first = 1;
313 channels = params_channels(params);
314 data_width = params_width(params);
316 dev->samplerate = params_rate(params);
317 switch (dev->samplerate) {
318 /* There are some limitation when using 8k sample rate */
320 mclk_rate = 12288000;
321 if ((data_width == 16) || (channels == 1)) {
322 pr_err("TDM: not support 16bit or 1-channel when using 8k sample rate\n");
328 mclk_rate = 11289600;
331 mclk_rate = 12288000;
334 mclk_rate = 11289600;
337 mclk_rate = 12288000;
340 mclk_rate = 11289600;
343 mclk_rate = 12288000;
346 pr_err("TDM: not support sample rate:%d\n", dev->samplerate);
350 dev->pcmclk = channels * dev->samplerate * data_width;
352 switch (params_format(params)) {
353 case SNDRV_PCM_FORMAT_S16_LE:
354 chan_wl = TDM_16BIT_WORD_LEN;
355 chan_sl = TDM_16BIT_SLOT_LEN;
356 dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
359 case SNDRV_PCM_FORMAT_S32_LE:
360 chan_wl = TDM_32BIT_WORD_LEN;
361 chan_sl = TDM_32BIT_SLOT_LEN;
362 dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
366 dev_err(dev->dev, "tdm: unsupported PCM fmt");
370 chan_nr = params_channels(params);
372 case ONE_CHANNEL_SUPPORT:
373 case TWO_CHANNEL_SUPPORT:
374 case FOUR_CHANNEL_SUPPORT:
375 case SIX_CHANNEL_SUPPORT:
376 case EIGHT_CHANNEL_SUPPORT:
379 dev_err(dev->dev, "channel not supported\n");
383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
384 dev->tx.wl = chan_wl;
385 dev->tx.sl = chan_sl;
386 dev->tx.sscale = chan_nr;
387 dev->play_dma_data.addr_width = dma_bus_width;
388 dma_data = &dev->play_dma_data;
390 dev->rx.wl = chan_wl;
391 dev->rx.sl = chan_sl;
392 dev->rx.sscale = chan_nr;
393 dev->capture_dma_data.addr_width = dma_bus_width;
394 dma_data = &dev->capture_dma_data;
397 snd_soc_dai_set_dma_data(dai, substream, dma_data);
399 ret = clk_set_rate(dev->clk_mclk_inner, mclk_rate);
401 dev_info(dev->dev, "Can't set clk_mclk: %d\n", ret);
405 ret = clk_set_rate(dev->clk_tdm_internal, dev->pcmclk);
407 dev_info(dev->dev, "Can't set clk_tdm_internal: %d\n", ret);
411 ret = clk_set_parent(dev->clk_tdm, dev->clk_tdm_ext);
413 dev_info(dev->dev, "Can't set clock source for clk_tdm: %d\n", ret);
417 ret = clk_prepare_enable(dev->clk_tdm_ahb);
419 dev_err(dev->dev, "Failed to prepare enable clk_tdm_ahb\n");
423 ret = clk_prepare_enable(dev->clk_tdm_apb);
425 dev_err(dev->dev, "Failed to prepare enable clk_tdm_apb\n");
429 sf_tdm_config(dev, substream);
430 sf_tdm_save_context(dev);
435 static int sf_tdm_trigger(struct snd_pcm_substream *substream,
436 int cmd, struct snd_soc_dai *dai)
438 struct sf_tdm_dev *dev = snd_soc_dai_get_drvdata(dai);
442 case SNDRV_PCM_TRIGGER_START:
443 case SNDRV_PCM_TRIGGER_RESUME:
444 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
446 sf_tdm_restore_context(dev);
447 sf_tdm_start(dev, substream);
450 case SNDRV_PCM_TRIGGER_STOP:
451 case SNDRV_PCM_TRIGGER_SUSPEND:
452 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
454 sf_tdm_stop(dev, substream);
463 static int sf_tdm_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
465 struct sf_tdm_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
468 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
469 case SND_SOC_DAIFMT_CBM_CFM:
470 dev->ms_mode = TDM_AS_SLAVE;
472 case SND_SOC_DAIFMT_CBS_CFS:
473 dev->ms_mode = TDM_AS_MASTER;
475 case SND_SOC_DAIFMT_CBM_CFS:
476 case SND_SOC_DAIFMT_CBS_CFM:
480 dev_dbg(dev->dev, "tdm : Invalid master/slave format\n");
487 static const struct snd_soc_dai_ops sf_tdm_dai_ops = {
488 .hw_params = sf_tdm_hw_params,
489 .trigger = sf_tdm_trigger,
490 .set_fmt = sf_tdm_set_fmt,
493 static int sf_tdm_dai_probe(struct snd_soc_dai *dai)
495 struct sf_tdm_dev *dev = snd_soc_dai_get_drvdata(dai);
497 snd_soc_dai_init_dma_data(dai, &dev->play_dma_data, &dev->capture_dma_data);
498 snd_soc_dai_set_drvdata(dai, dev);
502 #define SF_TDM_RATES SNDRV_PCM_RATE_8000_48000
504 #define SF_TDM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
505 SNDRV_PCM_FMTBIT_S32_LE)
507 static struct snd_soc_dai_driver sf_tdm_dai = {
511 .stream_name = "Playback",
514 .rates = SF_TDM_RATES,
515 .formats = SF_TDM_FORMATS,
518 .stream_name = "Capture",
521 .rates = SF_TDM_RATES,
522 .formats = SF_TDM_FORMATS,
524 .ops = &sf_tdm_dai_ops,
525 .probe = sf_tdm_dai_probe,
529 static const struct snd_pcm_hardware jh71xx_pcm_hardware = {
530 .info = (SNDRV_PCM_INFO_MMAP |
531 SNDRV_PCM_INFO_MMAP_VALID |
532 SNDRV_PCM_INFO_PAUSE |
533 SNDRV_PCM_INFO_RESUME |
534 SNDRV_PCM_INFO_INTERLEAVED |
535 SNDRV_PCM_INFO_BLOCK_TRANSFER),
536 .buffer_bytes_max = 192512,
537 .period_bytes_min = 4096,
538 .period_bytes_max = 32768,
544 static const struct snd_dmaengine_pcm_config jh71xx_dmaengine_pcm_config = {
545 .pcm_hardware = &jh71xx_pcm_hardware,
546 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
547 .prealloc_buffer_size = 192512,
550 static void tdm_init_params(struct sf_tdm_dev *dev)
552 dev->clkpolity = TDM_TX_RASING_RX_FALLING;
553 if (dev->frame_mode == SHORT_LATER) {
554 dev->elm = TDM_ELM_LATE;
555 dev->syncm = TDM_SYNCM_SHORT;
556 } else if (dev->frame_mode == SHORT_EARLY) {
557 dev->elm = TDM_ELM_EARLY;
558 dev->syncm = TDM_SYNCM_SHORT;
560 dev->elm = TDM_ELM_EARLY;
561 dev->syncm = TDM_SYNCM_LONG;
564 dev->ms_mode = TDM_AS_SLAVE;
565 dev->rx.ifl = dev->tx.ifl = TDM_FIFO_HALF;
566 dev->rx.wl = dev->tx.wl = TDM_16BIT_WORD_LEN;
567 dev->rx.sscale = dev->tx.sscale = 2;
568 dev->rx.lrj = dev->tx.lrj = TDM_LEFT_JUSTIFT;
570 dev->play_dma_data.addr = TDM_FIFO;
571 dev->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
572 dev->play_dma_data.fifo_size = TDM_FIFO_DEPTH/2;
573 dev->play_dma_data.maxburst = 16;
575 dev->capture_dma_data.addr = TDM_FIFO;
576 dev->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
577 dev->capture_dma_data.fifo_size = TDM_FIFO_DEPTH/2;
578 dev->capture_dma_data.maxburst = 8;
581 static int sf_tdm_clk_reset_init(struct platform_device *pdev, struct sf_tdm_dev *dev)
585 static struct clk_bulk_data clks[] = {
586 { .id = "clk_ahb0" },
587 { .id = "clk_tdm_ahb" },
588 { .id = "clk_apb0" },
589 { .id = "clk_tdm_apb" },
590 { .id = "clk_tdm_internal" },
591 { .id = "clk_tdm_ext" },
593 { .id = "mclk_inner" },
596 ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(clks), clks);
598 dev_err(&pdev->dev, "failed to get tdm clocks\n");
602 dev->clk_ahb0 = clks[0].clk;
603 dev->clk_tdm_ahb = clks[1].clk;
604 dev->clk_apb0 = clks[2].clk;
605 dev->clk_tdm_apb = clks[3].clk;
606 dev->clk_tdm_internal = clks[4].clk;
607 dev->clk_tdm_ext = clks[5].clk;
608 dev->clk_tdm = clks[6].clk;
609 dev->clk_mclk_inner = clks[7].clk;
611 dev->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
612 if (IS_ERR(dev->resets)) {
613 ret = PTR_ERR(dev->resets);
614 dev_err(&pdev->dev, "Failed to get tdm resets");
618 ret = reset_control_assert(dev->resets);
620 dev_err(&pdev->dev, "Failed to assert tdm resets\n");
624 ret = clk_prepare_enable(dev->clk_mclk_inner);
626 dev_err(&pdev->dev, "failed to prepare enable clk_mclk_inner\n");
630 ret = clk_prepare_enable(dev->clk_ahb0);
632 dev_err(&pdev->dev, "Failed to prepare enable clk_ahb0\n");
636 ret = clk_prepare_enable(dev->clk_tdm_ahb);
638 dev_err(&pdev->dev, "Failed to prepare enable clk_tdm_ahb\n");
642 ret = clk_prepare_enable(dev->clk_apb0);
644 dev_err(&pdev->dev, "Failed to prepare enable clk_apb0\n");
648 ret = clk_prepare_enable(dev->clk_tdm_apb);
650 dev_err(&pdev->dev, "Failed to prepare enable clk_tdm_apb\n");
654 ret = clk_prepare_enable(dev->clk_tdm_internal);
656 dev_err(&pdev->dev, "Failed to prepare enable clk_tdm_intl\n");
660 ret = clk_prepare_enable(dev->clk_tdm_ext);
662 dev_err(&pdev->dev, "failed to prepare enable clk_tdm_ext\n");
663 goto dis_tdm_internal;
666 ret = clk_prepare_enable(dev->clk_tdm);
668 dev_err(&pdev->dev, "failed to prepare enable clk_tdm\n");
672 ret = reset_control_deassert(dev->resets);
674 dev_err(&pdev->dev, "Failed to deassert tdm resets\n");
681 clk_disable_unprepare(dev->clk_tdm);
683 clk_disable_unprepare(dev->clk_tdm_ext);
685 clk_disable_unprepare(dev->clk_tdm_internal);
687 clk_disable_unprepare(dev->clk_tdm_apb);
689 clk_disable_unprepare(dev->clk_apb0);
691 clk_disable_unprepare(dev->clk_tdm_ahb);
693 clk_disable_unprepare(dev->clk_ahb0);
695 clk_disable_unprepare(dev->clk_mclk_inner);
700 static int sf_tdm_probe(struct platform_device *pdev)
702 struct sf_tdm_dev *dev;
703 struct resource *res;
706 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
710 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
711 dev->tdm_base = devm_ioremap_resource(&pdev->dev, res);
712 if (IS_ERR(dev->tdm_base))
713 return PTR_ERR(dev->tdm_base);
715 dev->dev = &pdev->dev;
717 ret = sf_tdm_clk_reset_init(pdev, dev);
719 dev_err(&pdev->dev, "failed to enable audio-tdm clock\n");
723 dev->frame_mode = SHORT_LATER;
724 tdm_init_params(dev);
725 dev_set_drvdata(&pdev->dev, dev);
727 ret = devm_snd_soc_register_component(&pdev->dev, &sf_tdm_component,
730 dev_err(&pdev->dev, "failed to register dai\n");
734 ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
735 &jh71xx_dmaengine_pcm_config,
736 SND_DMAENGINE_PCM_FLAG_COMPAT);
738 dev_err(&pdev->dev, "could not register pcm: %d\n", ret);
742 pm_runtime_enable(&pdev->dev);
744 sf_tdm_clk_disable(dev);
750 static int sf_tdm_dev_remove(struct platform_device *pdev)
752 pm_runtime_disable(&pdev->dev);
755 static const struct of_device_id sf_tdm_of_match[] = {
756 {.compatible = "starfive,jh7110-tdm",},
759 MODULE_DEVICE_TABLE(of, sf_tdm_of_match);
761 static const struct dev_pm_ops sf_tdm_pm_ops = {
762 SET_RUNTIME_PM_OPS(sf_tdm_runtime_suspend,
763 sf_tdm_runtime_resume, NULL)
766 static struct platform_driver sf_tdm_driver = {
768 .name = "jh7110-tdm",
769 .of_match_table = sf_tdm_of_match,
770 .pm = &sf_tdm_pm_ops,
772 .probe = sf_tdm_probe,
773 .remove = sf_tdm_dev_remove,
775 module_platform_driver(sf_tdm_driver);
777 MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
778 MODULE_DESCRIPTION("Starfive TDM Controller Driver");
779 MODULE_LICENSE("GPL v2");