Lines Matching +full:rk3288 +full:- +full:i2s

1 // SPDX-License-Identifier: GPL-2.0-only
4 * ALSA SoC Audio Layer - Rockchip I2S Controller driver
7 * Author: Jianqun <jay.xu@rock-chips.com>
24 #define DRV_NAME "rockchip-i2s"
48 * I2S controller hopes to start the tx and rx together,
62 static int i2s_pinctrl_select_bclk_on(struct rk_i2s_dev *i2s) in i2s_pinctrl_select_bclk_on() argument
66 if (!IS_ERR(i2s->pinctrl) && !IS_ERR_OR_NULL(i2s->bclk_on)) in i2s_pinctrl_select_bclk_on()
67 ret = pinctrl_select_state(i2s->pinctrl, i2s->bclk_on); in i2s_pinctrl_select_bclk_on()
70 dev_err(i2s->dev, "bclk enable failed %d\n", ret); in i2s_pinctrl_select_bclk_on()
75 static int i2s_pinctrl_select_bclk_off(struct rk_i2s_dev *i2s) in i2s_pinctrl_select_bclk_off() argument
80 if (!IS_ERR(i2s->pinctrl) && !IS_ERR_OR_NULL(i2s->bclk_off)) in i2s_pinctrl_select_bclk_off()
81 ret = pinctrl_select_state(i2s->pinctrl, i2s->bclk_off); in i2s_pinctrl_select_bclk_off()
84 dev_err(i2s->dev, "bclk disable failed %d\n", ret); in i2s_pinctrl_select_bclk_off()
91 struct rk_i2s_dev *i2s = dev_get_drvdata(dev); in i2s_runtime_suspend() local
93 regcache_cache_only(i2s->regmap, true); in i2s_runtime_suspend()
94 clk_disable_unprepare(i2s->mclk); in i2s_runtime_suspend()
101 struct rk_i2s_dev *i2s = dev_get_drvdata(dev); in i2s_runtime_resume() local
104 ret = clk_prepare_enable(i2s->mclk); in i2s_runtime_resume()
106 dev_err(i2s->dev, "clock enable failed %d\n", ret); in i2s_runtime_resume()
110 regcache_cache_only(i2s->regmap, false); in i2s_runtime_resume()
111 regcache_mark_dirty(i2s->regmap); in i2s_runtime_resume()
113 ret = regcache_sync(i2s->regmap); in i2s_runtime_resume()
115 clk_disable_unprepare(i2s->mclk); in i2s_runtime_resume()
125 static int rockchip_snd_txctrl(struct rk_i2s_dev *i2s, int on) in rockchip_snd_txctrl() argument
130 spin_lock(&i2s->lock); in rockchip_snd_txctrl()
132 ret = regmap_update_bits(i2s->regmap, I2S_DMACR, in rockchip_snd_txctrl()
137 ret = regmap_update_bits(i2s->regmap, I2S_XFER, in rockchip_snd_txctrl()
142 i2s->tx_start = true; in rockchip_snd_txctrl()
144 i2s->tx_start = false; in rockchip_snd_txctrl()
146 ret = regmap_update_bits(i2s->regmap, I2S_DMACR, in rockchip_snd_txctrl()
152 if (!i2s->rx_start) { in rockchip_snd_txctrl()
153 ret = regmap_update_bits(i2s->regmap, I2S_XFER, in rockchip_snd_txctrl()
159 ret = regmap_update_bits(i2s->regmap, I2S_CLR, in rockchip_snd_txctrl()
164 ret = regmap_read_poll_timeout_atomic(i2s->regmap, in rockchip_snd_txctrl()
171 dev_warn(i2s->dev, "fail to clear: %d\n", ret); in rockchip_snd_txctrl()
175 spin_unlock(&i2s->lock); in rockchip_snd_txctrl()
177 dev_err(i2s->dev, "lrclk update failed\n"); in rockchip_snd_txctrl()
182 static int rockchip_snd_rxctrl(struct rk_i2s_dev *i2s, int on) in rockchip_snd_rxctrl() argument
187 spin_lock(&i2s->lock); in rockchip_snd_rxctrl()
189 ret = regmap_update_bits(i2s->regmap, I2S_DMACR, in rockchip_snd_rxctrl()
195 ret = regmap_update_bits(i2s->regmap, I2S_XFER, in rockchip_snd_rxctrl()
200 i2s->rx_start = true; in rockchip_snd_rxctrl()
202 i2s->rx_start = false; in rockchip_snd_rxctrl()
204 ret = regmap_update_bits(i2s->regmap, I2S_DMACR, in rockchip_snd_rxctrl()
210 if (!i2s->tx_start) { in rockchip_snd_rxctrl()
211 ret = regmap_update_bits(i2s->regmap, I2S_XFER, in rockchip_snd_rxctrl()
217 ret = regmap_update_bits(i2s->regmap, I2S_CLR, in rockchip_snd_rxctrl()
222 ret = regmap_read_poll_timeout_atomic(i2s->regmap, in rockchip_snd_rxctrl()
229 dev_warn(i2s->dev, "fail to clear: %d\n", ret); in rockchip_snd_rxctrl()
233 spin_unlock(&i2s->lock); in rockchip_snd_rxctrl()
235 dev_err(i2s->dev, "lrclk update failed\n"); in rockchip_snd_rxctrl()
243 struct rk_i2s_dev *i2s = to_info(cpu_dai); in rockchip_i2s_set_fmt() local
247 pm_runtime_get_sync(cpu_dai->dev); in rockchip_i2s_set_fmt()
253 i2s->is_master_mode = true; in rockchip_i2s_set_fmt()
257 i2s->is_master_mode = false; in rockchip_i2s_set_fmt()
260 ret = -EINVAL; in rockchip_i2s_set_fmt()
264 regmap_update_bits(i2s->regmap, I2S_CKR, mask, val); in rockchip_i2s_set_fmt()
289 ret = -EINVAL; in rockchip_i2s_set_fmt()
293 regmap_update_bits(i2s->regmap, I2S_CKR, mask, val); in rockchip_i2s_set_fmt()
313 ret = -EINVAL; in rockchip_i2s_set_fmt()
317 regmap_update_bits(i2s->regmap, I2S_TXCR, mask, val); in rockchip_i2s_set_fmt()
337 ret = -EINVAL; in rockchip_i2s_set_fmt()
341 regmap_update_bits(i2s->regmap, I2S_RXCR, mask, val); in rockchip_i2s_set_fmt()
344 pm_runtime_put(cpu_dai->dev); in rockchip_i2s_set_fmt()
353 struct rk_i2s_dev *i2s = to_info(dai); in rockchip_i2s_hw_params() local
358 if (i2s->is_master_mode) { in rockchip_i2s_hw_params()
359 mclk_rate = clk_get_rate(i2s->mclk); in rockchip_i2s_hw_params()
360 bclk_rate = i2s->bclk_ratio * params_rate(params); in rockchip_i2s_hw_params()
362 return -EINVAL; in rockchip_i2s_hw_params()
366 regmap_update_bits(i2s->regmap, I2S_CKR, in rockchip_i2s_hw_params()
370 regmap_update_bits(i2s->regmap, I2S_CKR, in rockchip_i2s_hw_params()
394 return -EINVAL; in rockchip_i2s_hw_params()
411 dev_err(i2s->dev, "invalid channel: %d\n", in rockchip_i2s_hw_params()
413 return -EINVAL; in rockchip_i2s_hw_params()
416 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) in rockchip_i2s_hw_params()
417 regmap_update_bits(i2s->regmap, I2S_RXCR, in rockchip_i2s_hw_params()
421 regmap_update_bits(i2s->regmap, I2S_TXCR, in rockchip_i2s_hw_params()
425 if (!IS_ERR(i2s->grf) && i2s->pins) { in rockchip_i2s_hw_params()
426 regmap_read(i2s->regmap, I2S_TXCR, &val); in rockchip_i2s_hw_params()
444 val <<= i2s->pins->shift; in rockchip_i2s_hw_params()
445 val |= (I2S_IO_DIRECTION_MASK << i2s->pins->shift) << 16; in rockchip_i2s_hw_params()
446 regmap_write(i2s->grf, i2s->pins->reg_offset, val); in rockchip_i2s_hw_params()
449 regmap_update_bits(i2s->regmap, I2S_DMACR, I2S_DMACR_TDL_MASK, in rockchip_i2s_hw_params()
451 regmap_update_bits(i2s->regmap, I2S_DMACR, I2S_DMACR_RDL_MASK, in rockchip_i2s_hw_params()
455 if (dai->driver->symmetric_rate && rtd->dai_link->symmetric_rate) in rockchip_i2s_hw_params()
458 regmap_update_bits(i2s->regmap, I2S_CKR, in rockchip_i2s_hw_params()
467 struct rk_i2s_dev *i2s = to_info(dai); in rockchip_i2s_trigger() local
474 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) in rockchip_i2s_trigger()
475 ret = rockchip_snd_rxctrl(i2s, 1); in rockchip_i2s_trigger()
477 ret = rockchip_snd_txctrl(i2s, 1); in rockchip_i2s_trigger()
480 i2s_pinctrl_select_bclk_on(i2s); in rockchip_i2s_trigger()
485 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { in rockchip_i2s_trigger()
486 if (!i2s->tx_start) in rockchip_i2s_trigger()
487 i2s_pinctrl_select_bclk_off(i2s); in rockchip_i2s_trigger()
488 ret = rockchip_snd_rxctrl(i2s, 0); in rockchip_i2s_trigger()
490 if (!i2s->rx_start) in rockchip_i2s_trigger()
491 i2s_pinctrl_select_bclk_off(i2s); in rockchip_i2s_trigger()
492 ret = rockchip_snd_txctrl(i2s, 0); in rockchip_i2s_trigger()
496 ret = -EINVAL; in rockchip_i2s_trigger()
506 struct rk_i2s_dev *i2s = to_info(dai); in rockchip_i2s_set_bclk_ratio() local
508 i2s->bclk_ratio = ratio; in rockchip_i2s_set_bclk_ratio()
516 struct rk_i2s_dev *i2s = to_info(cpu_dai); in rockchip_i2s_set_sysclk() local
522 ret = clk_set_rate(i2s->mclk, freq); in rockchip_i2s_set_sysclk()
524 dev_err(i2s->dev, "Fail to set mclk %d\n", ret); in rockchip_i2s_set_sysclk()
531 struct rk_i2s_dev *i2s = snd_soc_dai_get_drvdata(dai); in rockchip_i2s_dai_probe() local
534 i2s->has_playback ? &i2s->playback_dma_data : NULL, in rockchip_i2s_dai_probe()
535 i2s->has_capture ? &i2s->capture_dma_data : NULL); in rockchip_i2s_dai_probe()
648 { .compatible = "rockchip,px30-i2s", },
649 { .compatible = "rockchip,rk1808-i2s", },
650 { .compatible = "rockchip,rk3036-i2s", },
651 { .compatible = "rockchip,rk3066-i2s", },
652 { .compatible = "rockchip,rk3128-i2s", },
653 { .compatible = "rockchip,rk3188-i2s", },
654 { .compatible = "rockchip,rk3228-i2s", },
655 { .compatible = "rockchip,rk3288-i2s", },
656 { .compatible = "rockchip,rk3308-i2s", },
657 { .compatible = "rockchip,rk3328-i2s", },
658 { .compatible = "rockchip,rk3366-i2s", },
659 { .compatible = "rockchip,rk3368-i2s", },
660 { .compatible = "rockchip,rk3399-i2s", .data = &rk3399_i2s_pins },
661 { .compatible = "rockchip,rk3588-i2s", },
662 { .compatible = "rockchip,rv1126-i2s", },
666 static int rockchip_i2s_init_dai(struct rk_i2s_dev *i2s, struct resource *res, in rockchip_i2s_init_dai() argument
669 struct device_node *node = i2s->dev->of_node; in rockchip_i2s_init_dai()
675 of_property_for_each_string(node, "dma-names", dma_names, dma_name) { in rockchip_i2s_init_dai()
677 i2s->has_playback = true; in rockchip_i2s_init_dai()
679 i2s->has_capture = true; in rockchip_i2s_init_dai()
682 dai = devm_kmemdup(i2s->dev, &rockchip_i2s_dai, in rockchip_i2s_init_dai()
685 return -ENOMEM; in rockchip_i2s_init_dai()
687 if (i2s->has_playback) { in rockchip_i2s_init_dai()
688 dai->playback.stream_name = "Playback"; in rockchip_i2s_init_dai()
689 dai->playback.channels_min = 2; in rockchip_i2s_init_dai()
690 dai->playback.channels_max = 8; in rockchip_i2s_init_dai()
691 dai->playback.rates = SNDRV_PCM_RATE_8000_192000; in rockchip_i2s_init_dai()
692 dai->playback.formats = SNDRV_PCM_FMTBIT_S8 | in rockchip_i2s_init_dai()
698 i2s->playback_dma_data.addr = res->start + I2S_TXDR; in rockchip_i2s_init_dai()
699 i2s->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; in rockchip_i2s_init_dai()
700 i2s->playback_dma_data.maxburst = 8; in rockchip_i2s_init_dai()
702 if (!of_property_read_u32(node, "rockchip,playback-channels", &val)) { in rockchip_i2s_init_dai()
704 dai->playback.channels_max = val; in rockchip_i2s_init_dai()
708 if (i2s->has_capture) { in rockchip_i2s_init_dai()
709 dai->capture.stream_name = "Capture"; in rockchip_i2s_init_dai()
710 dai->capture.channels_min = 2; in rockchip_i2s_init_dai()
711 dai->capture.channels_max = 8; in rockchip_i2s_init_dai()
712 dai->capture.rates = SNDRV_PCM_RATE_8000_192000; in rockchip_i2s_init_dai()
713 dai->capture.formats = SNDRV_PCM_FMTBIT_S8 | in rockchip_i2s_init_dai()
719 i2s->capture_dma_data.addr = res->start + I2S_RXDR; in rockchip_i2s_init_dai()
720 i2s->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; in rockchip_i2s_init_dai()
721 i2s->capture_dma_data.maxburst = 8; in rockchip_i2s_init_dai()
723 if (!of_property_read_u32(node, "rockchip,capture-channels", &val)) { in rockchip_i2s_init_dai()
725 dai->capture.channels_max = val; in rockchip_i2s_init_dai()
737 struct device_node *node = pdev->dev.of_node; in rockchip_i2s_probe()
738 struct rk_i2s_dev *i2s; in rockchip_i2s_probe() local
744 i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL); in rockchip_i2s_probe()
745 if (!i2s) in rockchip_i2s_probe()
746 return -ENOMEM; in rockchip_i2s_probe()
748 spin_lock_init(&i2s->lock); in rockchip_i2s_probe()
749 i2s->dev = &pdev->dev; in rockchip_i2s_probe()
751 i2s->grf = syscon_regmap_lookup_by_phandle(node, "rockchip,grf"); in rockchip_i2s_probe()
752 if (!IS_ERR(i2s->grf)) { in rockchip_i2s_probe()
753 i2s->pins = device_get_match_data(&pdev->dev); in rockchip_i2s_probe()
754 if (!i2s->pins) in rockchip_i2s_probe()
755 return -EINVAL; in rockchip_i2s_probe()
760 i2s->hclk = devm_clk_get(&pdev->dev, "i2s_hclk"); in rockchip_i2s_probe()
761 if (IS_ERR(i2s->hclk)) { in rockchip_i2s_probe()
762 dev_err(&pdev->dev, "Can't retrieve i2s bus clock\n"); in rockchip_i2s_probe()
763 return PTR_ERR(i2s->hclk); in rockchip_i2s_probe()
765 ret = clk_prepare_enable(i2s->hclk); in rockchip_i2s_probe()
767 dev_err(i2s->dev, "hclock enable failed %d\n", ret); in rockchip_i2s_probe()
771 i2s->mclk = devm_clk_get(&pdev->dev, "i2s_clk"); in rockchip_i2s_probe()
772 if (IS_ERR(i2s->mclk)) { in rockchip_i2s_probe()
773 dev_err(&pdev->dev, "Can't retrieve i2s master clock\n"); in rockchip_i2s_probe()
774 ret = PTR_ERR(i2s->mclk); in rockchip_i2s_probe()
784 i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs, in rockchip_i2s_probe()
786 if (IS_ERR(i2s->regmap)) { in rockchip_i2s_probe()
787 dev_err(&pdev->dev, in rockchip_i2s_probe()
789 ret = PTR_ERR(i2s->regmap); in rockchip_i2s_probe()
793 i2s->bclk_ratio = 64; in rockchip_i2s_probe()
794 i2s->pinctrl = devm_pinctrl_get(&pdev->dev); in rockchip_i2s_probe()
795 if (!IS_ERR(i2s->pinctrl)) { in rockchip_i2s_probe()
796 i2s->bclk_on = pinctrl_lookup_state(i2s->pinctrl, "bclk_on"); in rockchip_i2s_probe()
797 if (!IS_ERR_OR_NULL(i2s->bclk_on)) { in rockchip_i2s_probe()
798 i2s->bclk_off = pinctrl_lookup_state(i2s->pinctrl, "bclk_off"); in rockchip_i2s_probe()
799 if (IS_ERR_OR_NULL(i2s->bclk_off)) { in rockchip_i2s_probe()
800 dev_err(&pdev->dev, "failed to find i2s bclk_off\n"); in rockchip_i2s_probe()
801 ret = -EINVAL; in rockchip_i2s_probe()
806 dev_dbg(&pdev->dev, "failed to find i2s pinctrl\n"); in rockchip_i2s_probe()
809 i2s_pinctrl_select_bclk_off(i2s); in rockchip_i2s_probe()
811 dev_set_drvdata(&pdev->dev, i2s); in rockchip_i2s_probe()
813 pm_runtime_enable(&pdev->dev); in rockchip_i2s_probe()
814 if (!pm_runtime_enabled(&pdev->dev)) { in rockchip_i2s_probe()
815 ret = i2s_runtime_resume(&pdev->dev); in rockchip_i2s_probe()
820 ret = rockchip_i2s_init_dai(i2s, res, &dai); in rockchip_i2s_probe()
824 ret = devm_snd_soc_register_component(&pdev->dev, in rockchip_i2s_probe()
829 dev_err(&pdev->dev, "Could not register DAI\n"); in rockchip_i2s_probe()
833 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0); in rockchip_i2s_probe()
835 dev_err(&pdev->dev, "Could not register PCM\n"); in rockchip_i2s_probe()
842 if (!pm_runtime_status_suspended(&pdev->dev)) in rockchip_i2s_probe()
843 i2s_runtime_suspend(&pdev->dev); in rockchip_i2s_probe()
845 pm_runtime_disable(&pdev->dev); in rockchip_i2s_probe()
847 clk_disable_unprepare(i2s->hclk); in rockchip_i2s_probe()
853 struct rk_i2s_dev *i2s = dev_get_drvdata(&pdev->dev); in rockchip_i2s_remove() local
855 pm_runtime_disable(&pdev->dev); in rockchip_i2s_remove()
856 if (!pm_runtime_status_suspended(&pdev->dev)) in rockchip_i2s_remove()
857 i2s_runtime_suspend(&pdev->dev); in rockchip_i2s_remove()
859 clk_disable_unprepare(i2s->hclk); in rockchip_i2s_remove()
879 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");