Skip to contentSkip to conversation

ASoC: fsl_sai: Defer TERE until stream setup

PATCH In review Fabio Estevam 3 participants 7 messages Oct 19 – Nov 6

Subsystems (3)
lore
Share & tools
lore

Fabio Estevam

author reviewer
#0 lore raw

PATCH 1/3 ASoC: fsl_sai: Defer TERE until stream setup

On i.MX8MP, TERE also enables MCLK output. Runtime resume sets it before hw_params has configured the stream, so clocks can start from stale register values.

In master mode, a custom board with a 400 MHz IPG clock drove BCLK at 200 MHz before hw_params rewrote xCR2. On first open, reset xCR4 also drove frame sync at about 100 MHz. Later opens can select the bus clock with DIV=0 and again drive BCLK prematurely.

Enable TERE at the end of hw_params after programming stream format and clock settings. During runtime resume, restore TERE only when a configured stream is being resumed.

Fixes 3e4a82612998 ("ASoC: fsl_sai: MCLK bind with TX/RX enable bit")
---
 sound/soc/fsl/fsl_sai.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
sound/soc/fsl/fsl_sai.c +16 -1
index d232c8f53061..51779d7f8bf8 100644
@@ -799,6 +799,15 @@ static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
 	regmap_write(sai->regmap, FSL_SAI_xMR(tx),
 		     ~GENMASK_U32(min(channels, slots) - 1, 0));
 
+	/*
+	 * On i.MX8MP, TERE also enables the master clock output. Enable it
+	 * after hw_params has configured the stream to avoid briefly driving
+	 * clocks from stale register values.
+	 */
+	if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output)
+		regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs),
+				   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
+
 	return 0;
 }
 
@@ -1983,7 +1992,13 @@ static int fsl_sai_runtime_resume(struct device *dev)
 	if (ret)
 		goto disable_rx_clk;
 
-	if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output)
+	/*
+	 * A fresh stream has not configured its clock divider yet. Keep TERE
+	 * disabled until hw_params in that case; an already configured stream
+	 * needs it restored after a system suspend.
+	 */
+	if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output &&
+	    sai->mclk_streams)
 		regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs),
 				   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
 
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH 2/3 ASoC: fsl_sai: Stop BCLK after the last stream is freed

On i.MX8MP, trigger stop leaves BCE set so MCLK remains available to a codec. When the final stream is freed, hw_free disables MCLK, but TERE and BCE can keep BCLK running from the newly selected clock source, including an unconfigured bus clock.

Clear TERE and BCE once no configured or active stream needs the clock. This preserves MCLK between trigger stop and hw_free.

The premature clock output was reproduced on a custom i.MX8MP board.

Fixes 197c53c8ecb3 ("ASoC: fsl_sai: Don't disable bitclock for i.MX8MP")
---
 sound/soc/fsl/fsl_sai.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
sound/soc/fsl/fsl_sai.c +10 -0
index 51779d7f8bf8..a135b3da37cf 100644
@@ -851,6 +851,16 @@ static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
 		}
 	}
 
+	/*
+	 * Keep MCLK available until the last stream is freed, as it may be
+	 * needed by a codec. Once no stream uses it, stop TERE and BCE too;
+	 * otherwise BCLK can continue from the clock source selected later.
+	 */
+	if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output &&
+	    !sai->mclk_streams && snd_soc_dai_active(cpu_dai) <= 1)
+		regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs),
+				   FSL_SAI_CSR_TERE | FSL_SAI_CSR_BCE, 0);
+
 	return 0;
 }
 
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 2/3] ASoC: fsl_sai: Stop BCLK after the last stream is freed

sound/soc/fsl/fsl_sai.c static int fsl_sai_hw_free(struct snd_pcm_substream *substream, · line 862

Quoted 10 lines

On i.MX8MP, trigger stop leaves BCE set so MCLK remains available to a codec. When the final stream is freed, hw_free disables MCLK, but TERE and BCE can keep BCLK running from the newly selected clock source, including an unconfigured bus clock.

Clear TERE and BCE once no configured or active stream needs the clock. This preserves MCLK between trigger stop and hw_free.

sound/soc/fsl/fsl_sai.c +2 -0
+		regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs),
+				   FSL_SAI_CSR_TERE | FSL_SAI_CSR_BCE, 0);

What about RCSR?

Signature · 1 line
Mark Brown <[email protected]>
author reviewer
#3 lore raw

PATCH 3/3 ASoC: fsl_sai: Select sleep pinctrl state during runtime suspend

In reply to Fabio Estevam

When runtime suspended, the SAI can leave its outputs at their last levels. If a connected device is unpowered, those signals can source current through its input protection circuitry.

Select the sleep pinctrl state after gating the clocks, and restore the default state before enabling them on runtime resume. Devices without a sleep state continue to use their existing pin configuration.

The pin-state issue was reproduced on a custom i.MX8MP board.

---
 sound/soc/fsl/fsl_sai.c | 3 +++
 1 file changed, 3 insertions(+)
sound/soc/fsl/fsl_sai.c +3 -0
index a135b3da37cf..ad21bd3b8a5d 100644
@@ -1959,6 +1959,7 @@ static int fsl_sai_runtime_suspend(struct device *dev)
 		cpu_latency_qos_remove_request(&sai->pm_qos_req);
 
 	regcache_cache_only(sai->regmap, true);
+	pinctrl_pm_select_sleep_state(dev);
 
 	return 0;
 }
@@ -1969,6 +1970,8 @@ static int fsl_sai_runtime_resume(struct device *dev)
 	unsigned int ofs = sai->soc_data->reg_offset;
 	int ret;
 
+	pinctrl_pm_select_default_state(dev);
+
 	ret = clk_prepare_enable(sai->bus_clk);
 	if (ret) {
 		dev_err(dev, "failed to enable bus clock: %d\n", ret);
Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH 3/3] ASoC: fsl_sai: Select sleep pinctrl state during runtime suspend

Quoted 21 lines · Fabio Estevam

When runtime suspended, the SAI can leave its outputs at their last levels. If a connected device is unpowered, those signals can source current through its input protection circuitry.

Select the sleep pinctrl state after gating the clocks, and restore the default state before enabling them on runtime resume. Devices without a sleep state continue to use their existing pin configuration.

The pin-state issue was reproduced on a custom i.MX8MP board.

---
 sound/soc/fsl/fsl_sai.c | 3 +++
 1 file changed, 3 insertions(+)
sound/soc/fsl/fsl_sai.c +0 -0
index a135b3da37cf..ad21bd3b8a5d 100644

sound/soc/fsl/fsl_sai.c static int fsl_sai_runtime_suspend(struct device *dev) · line 1962

Quoted 4 lines
sound/soc/fsl/fsl_sai.c +1 -0
@@ -1959,6 +1959,7 @@ static int fsl_sai_runtime_suspend(struct device *dev)
                cpu_latency_qos_remove_request(&sai->pm_qos_req);
 
        regcache_cache_only(sai->regmap, true);
+       pinctrl_pm_select_sleep_state(dev);

ASoC core already selects the sleep pinctrl state when a component becomes inactive
in soc_pcm_clean(), and restores the default state in __soc_pcm_open(). So should this be a duplicate?

Best regards
Shengjiu Wang

Quoted 15 lines
sound/soc/fsl/fsl_sai.c +2 -1
        return 0;
 }
@@ -1969,6 +1970,8 @@ static int fsl_sai_runtime_resume(struct device *dev)
        unsigned int ofs = sai->soc_data->reg_offset;
        int ret;
 
+       pinctrl_pm_select_default_state(dev);
+
        ret = clk_prepare_enable(sai->bus_clk);
        if (ret) {
                dev_err(dev, "failed to enable bus clock: %d\n", ret);
--

2.43.0

maintainer
#5 lore raw
In reply to Fabio Estevam
Quoted 16 lines

On i.MX8MP, TERE also enables MCLK output. Runtime resume sets it before hw_params has configured the stream, so clocks can start from stale register values.

In master mode, a custom board with a 400 MHz IPG clock drove BCLK at 200 MHz before hw_params rewrote xCR2. On first open, reset xCR4 also drove frame sync at about 100 MHz. Later opens can select the bus clock with DIV=0 and again drive BCLK prematurely.

Enable TERE at the end of hw_params after programming stream format and clock settings. During runtime resume, restore TERE only when a configured stream is being resumed.

Fixes 3e4a82612998 ("ASoC: fsl_sai: MCLK bind with TX/RX enable bit")

sound/soc/fsl/fsl_sai.c static int fsl_sai_hw_params(struct snd_pcm_substream *substream, · line 805

Quoted 4 lines
sound/soc/fsl/fsl_sai.c +4 -0
+	/*
+	 * On i.MX8MP, TERE also enables the master clock output. Enable it
+	 * after hw_params has configured the stream to avoid briefly driving
+	 * clocks from stale register values.

Has this been tested on wm896x boards like those referenced in the above commit? If the card tries to configure the PLL to use this MCLK in it's hw_params() that'll fail now.

sound/soc/fsl/fsl_sai.c static int fsl_sai_runtime_resume(struct device *dev) · line 2001

Quoted 7 lines
sound/soc/fsl/fsl_sai.c +7 -0
+	/*
+	 * A fresh stream has not configured its clock divider yet. Keep TERE
+	 * disabled until hw_params in that case; an already configured stream
+	 * needs it restored after a system suspend.
+	 */
+	if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output &&
+	    sai->mclk_streams)

Will this DTRT if the DAI is a clock consumer? It'd be a bit odd to use the MCLK and still drive the bus clocks from the CODEC but perhaps some device can only be clock provider.

Signature · 1 line
Mark Brown <[email protected]>
author reviewer
#6 lore raw

Hi Mark,

Quoted 3 lines · Mark Brown

Has this been tested on wm896x boards like those referenced in the above commit? If the card tries to configure the PLL to use this MCLK in it's hw_params() that'll fail now.

I'll try to get access to an i.MX board with a WM8962, address your comments and send a v2.

Thanks