Lines Matching defs:sai
24 #define DRV_NAME "rockchip-sai"
73 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
79 sai->has_playback)
83 sai->has_capture)
89 static int rockchip_sai_fsync_lost_detect(struct rk_sai_dev *sai, bool en)
93 if (sai->is_master_mode || sai->version < SAI_VER_2311)
96 regmap_read(sai->regmap, SAI_FSCR, &fw);
99 regmap_update_bits(sai->regmap, SAI_INTCR,
101 regmap_update_bits(sai->regmap, SAI_INTCR,
109 regmap_update_bits(sai->regmap, SAI_FS_TIMEOUT,
116 static int rockchip_sai_fsync_err_detect(struct rk_sai_dev *sai,
119 if (sai->is_master_mode || sai->version < SAI_VER_2311)
122 regmap_update_bits(sai->regmap, SAI_INTCR,
124 regmap_update_bits(sai->regmap, SAI_INTCR,
131 static int rockchip_sai_poll_clk_idle(struct rk_sai_dev *sai)
136 if (sai->version >= SAI_VER_2307) {
139 idle = sai->version >= SAI_VER_2311 ? idle >> 1 : idle;
145 ret = regmap_read_poll_timeout_atomic(sai->regmap, reg, val,
148 dev_warn(sai->dev, "Failed to idle FS\n");
153 static int rockchip_sai_poll_stream_idle(struct rk_sai_dev *sai, bool playback, bool capture)
159 if (sai->version >= SAI_VER_2307) {
165 idle = sai->version >= SAI_VER_2311 ? idle >> 1 : idle;
174 ret = regmap_read_poll_timeout_atomic(sai->regmap, reg, val,
177 dev_warn(sai->dev, "Failed to idle stream\n");
184 * @sai: pointer to the driver instance's rk_sai_dev struct
192 * Context: Any context. Expects sai->xfer_lock to be held by caller.
194 static void rockchip_sai_xfer_clk_stop_and_wait(struct rk_sai_dev *sai, unsigned int *to_restore)
200 assert_spin_locked(&sai->xfer_lock);
202 regmap_read(sai->regmap, SAI_XFER, &val);
206 if (sai->is_master_mode)
207 regmap_update_bits(sai->regmap, SAI_XFER, mask, disable);
210 rockchip_sai_poll_clk_idle(sai);
218 struct rk_sai_dev *sai = dev_get_drvdata(dev);
221 rockchip_sai_fsync_lost_detect(sai, 0);
222 rockchip_sai_fsync_err_detect(sai, 0);
224 spin_lock_irqsave(&sai->xfer_lock, flags);
225 rockchip_sai_xfer_clk_stop_and_wait(sai, NULL);
226 spin_unlock_irqrestore(&sai->xfer_lock, flags);
228 regcache_cache_only(sai->regmap, true);
245 clk_disable_unprepare(sai->mclk);
246 clk_disable_unprepare(sai->hclk);
253 struct rk_sai_dev *sai = dev_get_drvdata(dev);
256 ret = clk_prepare_enable(sai->hclk);
260 ret = clk_prepare_enable(sai->mclk);
264 regcache_cache_only(sai->regmap, false);
265 regcache_mark_dirty(sai->regmap);
266 ret = regcache_sync(sai->regmap);
273 clk_disable_unprepare(sai->mclk);
275 clk_disable_unprepare(sai->hclk);
280 static void rockchip_sai_fifo_xrun_detect(struct rk_sai_dev *sai,
285 regmap_update_bits(sai->regmap, SAI_INTCR,
287 regmap_update_bits(sai->regmap, SAI_INTCR,
292 regmap_update_bits(sai->regmap, SAI_INTCR,
294 regmap_update_bits(sai->regmap, SAI_INTCR,
300 static void rockchip_sai_dma_ctrl(struct rk_sai_dev *sai,
304 rockchip_sai_fifo_xrun_detect(sai, stream, 0);
307 regmap_update_bits(sai->regmap, SAI_DMACR,
311 regmap_update_bits(sai->regmap, SAI_DMACR,
317 rockchip_sai_fifo_xrun_detect(sai, stream, 1);
320 static void rockchip_sai_reset(struct rk_sai_dev *sai)
331 reset_control_assert(sai->rst_h);
333 reset_control_deassert(sai->rst_h);
335 reset_control_assert(sai->rst_m);
337 reset_control_deassert(sai->rst_m);
341 regcache_mark_dirty(sai->regmap);
342 regcache_sync(sai->regmap);
345 static int rockchip_sai_clear(struct rk_sai_dev *sai, unsigned int clr)
350 regmap_update_bits(sai->regmap, SAI_CLR, clr, clr);
351 ret = regmap_read_poll_timeout_atomic(sai->regmap, SAI_CLR, val,
354 dev_warn(sai->dev, "Failed to clear %u\n", clr);
355 rockchip_sai_reset(sai);
361 static void rockchip_sai_xfer_start(struct rk_sai_dev *sai, int stream)
374 regmap_update_bits(sai->regmap, SAI_XFER, msk, val);
377 static void rockchip_sai_xfer_stop(struct rk_sai_dev *sai, int stream)
395 regmap_update_bits(sai->regmap, SAI_XFER, msk, val);
396 rockchip_sai_poll_stream_idle(sai, playback, capture);
398 rockchip_sai_clear(sai, clr);
401 static void rockchip_sai_start(struct rk_sai_dev *sai, int stream)
403 rockchip_sai_dma_ctrl(sai, stream, 1);
404 rockchip_sai_xfer_start(sai, stream);
407 static void rockchip_sai_stop(struct rk_sai_dev *sai, int stream)
409 rockchip_sai_dma_ctrl(sai, stream, 0);
410 rockchip_sai_xfer_stop(sai, stream);
413 static void rockchip_sai_fmt_create(struct rk_sai_dev *sai, unsigned int fmt)
418 assert_spin_locked(&sai->xfer_lock);
428 sai->fpw = FPW_HALF_FRAME_WIDTH;
437 sai->fpw = FPW_HALF_FRAME_WIDTH;
443 if (sai->is_tdm)
449 sai->fpw = FPW_HALF_FRAME_WIDTH;
458 sai->fpw = FPW_ONE_BCLK_WIDTH;
467 sai->fpw = FPW_ONE_BCLK_WIDTH;
470 dev_err(sai->dev, "Unsupported fmt %u\n", fmt);
474 regmap_update_bits(sai->regmap, SAI_TXCR, xcr_mask, xcr_val);
475 regmap_update_bits(sai->regmap, SAI_RXCR, xcr_mask, xcr_val);
476 regmap_update_bits(sai->regmap, SAI_TX_SHIFT, xsft_mask, xsft_val);
477 regmap_update_bits(sai->regmap, SAI_RX_SHIFT, xsft_mask, xsft_val);
478 regmap_update_bits(sai->regmap, SAI_FSCR, fscr_mask, fscr_val);
483 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
495 sai->is_master_mode = true;
499 sai->is_master_mode = false;
506 spin_lock_irqsave(&sai->xfer_lock, flags);
507 rockchip_sai_xfer_clk_stop_and_wait(sai, &clk_gates);
508 if (sai->initialized) {
509 if (sai->has_capture && sai->has_playback)
510 rockchip_sai_xfer_stop(sai, -1);
511 else if (sai->has_capture)
512 rockchip_sai_xfer_stop(sai, SNDRV_PCM_STREAM_CAPTURE);
514 rockchip_sai_xfer_stop(sai, SNDRV_PCM_STREAM_PLAYBACK);
516 rockchip_sai_clear(sai, 0);
517 sai->initialized = true;
520 regmap_update_bits(sai->regmap, SAI_CKR, mask, val);
541 regmap_update_bits(sai->regmap, SAI_CKR, mask, val);
543 rockchip_sai_fmt_create(sai, fmt);
547 regmap_update_bits(sai->regmap, SAI_XFER,
550 spin_unlock_irqrestore(&sai->xfer_lock, flags);
561 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
576 pm_runtime_get_sync(sai->dev);
578 regmap_read(sai->regmap, SAI_DMACR, &val);
582 lanes = sai->tx_lanes;
585 lanes = sai->rx_lanes;
588 if (!sai->is_tdm) {
591 dev_err(sai->dev, "not enough lanes (%d) for requested number of %s channels (%d)\n",
601 dev_dbg(sai->dev, "using %d lanes totalling %d%s channels for %s\n",
602 lanes, params_channels(params), sai->is_tdm ? " (TDM)" : "",
627 spin_lock_irqsave(&sai->xfer_lock, flags);
629 regmap_update_bits(sai->regmap, reg, SAI_XCR_VDW_MASK | SAI_XCR_CSR_MASK, val);
631 regmap_read(sai->regmap, reg, &val);
636 regmap_update_bits(sai->regmap, reg, SAI_XCR_SNB_MASK,
639 fscr = SAI_FSCR_FW(sai->fw_ratio * slot_width * ch_per_lane);
641 switch (sai->fpw) {
649 fscr |= SAI_FSCR_FPW(sai->fw_ratio * slot_width * ch_per_lane / 2);
652 dev_err(sai->dev, "Invalid Frame Pulse Width %d\n", sai->fpw);
657 regmap_update_bits(sai->regmap, SAI_FSCR,
660 if (sai->is_master_mode) {
661 bclk_rate = sai->fw_ratio * slot_width * ch_per_lane * params_rate(params);
662 ret = clk_set_rate(sai->mclk, sai->mclk_rate);
664 dev_err(sai->dev, "Failed to set mclk to %u: %pe\n",
665 sai->mclk_rate, ERR_PTR(ret));
669 mclk_rate = clk_get_rate(sai->mclk);
671 dev_err(sai->dev, "Mismatch mclk: %u, at least %u\n",
682 dev_err(sai->dev, "Mismatch mclk: %u, expected %u (+/- %dHz)\n",
688 regmap_update_bits(sai->regmap, SAI_CKR, SAI_CKR_MDIV_MASK,
693 spin_unlock_irqrestore(&sai->xfer_lock, flags);
695 pm_runtime_put(sai->dev);
703 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
709 if (sai->is_master_mode) {
725 spin_lock_irqsave(&sai->xfer_lock, flags);
726 regmap_update_bits(sai->regmap, SAI_XFER,
731 spin_unlock_irqrestore(&sai->xfer_lock, flags);
734 rockchip_sai_fsync_lost_detect(sai, 1);
735 rockchip_sai_fsync_err_detect(sai, 1);
740 static void rockchip_sai_path_config(struct rk_sai_dev *sai,
747 regmap_update_bits(sai->regmap, SAI_PATH_SEL,
749 SAI_RX_PATH(i, sai->sdi[i]));
752 regmap_update_bits(sai->regmap, SAI_PATH_SEL,
754 SAI_TX_PATH(i, sai->sdo[i]));
757 static int rockchip_sai_path_prepare(struct rk_sai_dev *sai,
767 path_prop = "rockchip,sai-rx-route";
768 data = sai->sdi;
769 lanes = &sai->rx_lanes;
771 path_prop = "rockchip,sai-tx-route";
772 data = sai->sdo;
773 lanes = &sai->tx_lanes;
780 dev_err(sai->dev, "found %d entries in %s, outside of range 1 to %d\n",
784 dev_err(sai->dev, "error in %s property: %pe\n", path_prop,
790 ret = device_property_read_u32_array(sai->dev, path_prop, data, num);
792 dev_err(sai->dev, "failed to read property '%s': %pe\n",
799 dev_err(sai->dev, "%s[%d] is %d, should be less than %d\n",
805 rockchip_sai_path_config(sai, num, is_rx);
810 static int rockchip_sai_parse_paths(struct rk_sai_dev *sai,
815 if (sai->has_playback) {
816 sai->tx_lanes = 1;
817 ret = rockchip_sai_path_prepare(sai, np, false);
819 dev_err(sai->dev, "Failed to prepare TX path: %pe\n",
825 if (sai->has_capture) {
826 sai->rx_lanes = 1;
827 ret = rockchip_sai_path_prepare(sai, np, true);
829 dev_err(sai->dev, "Failed to prepare RX path: %pe\n",
841 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
851 rockchip_sai_start(sai, substream->stream);
856 rockchip_sai_stop(sai, substream->stream);
869 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
872 sai->has_playback ? &sai->playback_dma_data : NULL,
873 sai->has_capture ? &sai->capture_dma_data : NULL);
881 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
887 if (sai->substreams[stream])
890 if (sai->wait_time[stream])
891 substream->wait_time = sai->wait_time[stream];
893 sai->substreams[stream] = substream;
901 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
906 sai->substreams[substream->stream] = NULL;
913 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
920 sai->is_tdm = false;
923 sai->is_tdm = true;
930 spin_lock_irqsave(&sai->xfer_lock, flags);
931 rockchip_sai_xfer_clk_stop_and_wait(sai, &clk_gates);
932 regmap_update_bits(sai->regmap, SAI_TXCR, SAI_XCR_SBW_MASK,
934 regmap_update_bits(sai->regmap, SAI_RXCR, SAI_XCR_SBW_MASK,
936 regmap_update_bits(sai->regmap, SAI_XFER,
939 spin_unlock_irqrestore(&sai->xfer_lock, flags);
948 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai);
950 sai->mclk_rate = freq;
1098 static int rockchip_sai_init_dai(struct rk_sai_dev *sai, struct resource *res,
1101 struct device_node *node = sai->dev->of_node;
1108 sai->has_playback = true;
1110 sai->has_capture = true;
1113 dai = devm_kmemdup(sai->dev, &rockchip_sai_dai,
1118 if (sai->has_playback) {
1129 sai->playback_dma_data.addr = res->start + SAI_TXDR;
1130 sai->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1131 sai->playback_dma_data.maxburst = MAXBURST_PER_FIFO;
1134 if (sai->has_capture) {
1145 sai->capture_dma_data.addr = res->start + SAI_RXDR;
1146 sai->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1147 sai->capture_dma_data.maxburst = MAXBURST_PER_FIFO;
1150 regmap_update_bits(sai->regmap, SAI_DMACR, SAI_DMACR_TDL_MASK,
1152 regmap_update_bits(sai->regmap, SAI_DMACR, SAI_DMACR_RDL_MASK,
1231 struct rk_sai_dev *sai = snd_soc_component_get_drvdata(component);
1233 ucontrol->value.integer.value[0] = sai->wait_time[SNDRV_PCM_STREAM_CAPTURE];
1242 struct rk_sai_dev *sai = snd_soc_component_get_drvdata(component);
1247 sai->wait_time[SNDRV_PCM_STREAM_CAPTURE] = ucontrol->value.integer.value[0];
1256 struct rk_sai_dev *sai = snd_soc_component_get_drvdata(component);
1258 ucontrol->value.integer.value[0] = sai->wait_time[SNDRV_PCM_STREAM_PLAYBACK];
1267 struct rk_sai_dev *sai = snd_soc_component_get_drvdata(component);
1272 sai->wait_time[SNDRV_PCM_STREAM_PLAYBACK] = ucontrol->value.integer.value[0];
1333 struct rk_sai_dev *sai = (struct rk_sai_dev *)devid;
1337 regmap_read(sai->regmap, SAI_INTSR, &val);
1339 dev_warn_ratelimited(sai->dev, "TX FIFO Underrun\n");
1340 regmap_update_bits(sai->regmap, SAI_INTCR,
1342 regmap_update_bits(sai->regmap, SAI_INTCR,
1345 substream = sai->substreams[SNDRV_PCM_STREAM_PLAYBACK];
1351 dev_warn_ratelimited(sai->dev, "RX FIFO Overrun\n");
1352 regmap_update_bits(sai->regmap, SAI_INTCR,
1354 regmap_update_bits(sai->regmap, SAI_INTCR,
1357 substream = sai->substreams[SNDRV_PCM_STREAM_CAPTURE];
1363 dev_warn_ratelimited(sai->dev, "Frame Sync Error\n");
1364 regmap_update_bits(sai->regmap, SAI_INTCR,
1366 regmap_update_bits(sai->regmap, SAI_INTCR,
1372 dev_warn_ratelimited(sai->dev, "Frame Sync Lost\n");
1373 regmap_update_bits(sai->regmap, SAI_INTCR,
1375 regmap_update_bits(sai->regmap, SAI_INTCR,
1386 struct rk_sai_dev *sai;
1392 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
1393 if (!sai)
1396 sai->dev = &pdev->dev;
1397 sai->fw_ratio = 1;
1399 sai->is_master_mode = true;
1400 dev_set_drvdata(&pdev->dev, sai);
1402 spin_lock_init(&sai->xfer_lock);
1404 sai->rst_h = devm_reset_control_get_optional_exclusive(&pdev->dev, "h");
1405 if (IS_ERR(sai->rst_h))
1406 return dev_err_probe(&pdev->dev, PTR_ERR(sai->rst_h),
1409 sai->rst_m = devm_reset_control_get_optional_exclusive(&pdev->dev, "m");
1410 if (IS_ERR(sai->rst_m))
1411 return dev_err_probe(&pdev->dev, PTR_ERR(sai->rst_m),
1419 sai->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
1421 if (IS_ERR(sai->regmap))
1422 return dev_err_probe(&pdev->dev, PTR_ERR(sai->regmap),
1428 IRQF_SHARED, node->name, sai);
1436 sai->mclk = devm_clk_get(&pdev->dev, "mclk");
1437 if (IS_ERR(sai->mclk))
1438 return dev_err_probe(&pdev->dev, PTR_ERR(sai->mclk),
1441 sai->hclk = devm_clk_get_enabled(&pdev->dev, "hclk");
1442 if (IS_ERR(sai->hclk))
1443 return dev_err_probe(&pdev->dev, PTR_ERR(sai->hclk),
1446 regmap_read(sai->regmap, SAI_VERSION, &sai->version);
1448 ret = rockchip_sai_init_dai(sai, res, &dai);
1452 ret = rockchip_sai_parse_paths(sai, node);
1485 clk_disable_unprepare(sai->hclk);
1510 { .compatible = "rockchip,rk3576-sai", },