Skip to contentSkip to conversation

ASoC: mediatek: mt8183: Fix clock error handling

PATCH Superseded [email protected] 2 participants 9 messages Aug 28 – Sep 3

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover ASoC: mediatek: mt8183: Fix clock error handling

From: bui duc phuc <[email protected]>

Hi all,

This series fixes and cleans up clock error handling in the MT8183 ASoC driver.

Compile tested only.

Link v2:
https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Remove the redundant error log for apll1_mux_setting error handling.
- Add Fixes tags.
- Add a cover letter.

Changes in v3:
- Add a patch to fix clock handling in the mux disable path, as suggested by Cezary.
- Return directly from apllx_mux_setting() instead of using a goto label, as suggested by Cezary.
- Add a Reviewed-by tag.

Best regards,
Phuc

bui duc phuc (5):
ASoC: mediatek: mt8183: Fix wrong clock cleanup on clk_set_parent()
failure
ASoC: mediatek: mt8183: Fix clock handling in mux disable path
ASoC: mediatek: mt8183: Fix APLL enable error handling
ASoC: mediatek: mt8183: Use dev_err_probe() for error handling
ASoC: mediatek: mt8183: Drop redundant probe error messages

 sound/soc/mediatek/mt8183/mt8183-afe-clk.c | 95 ++++++++++++----------
 sound/soc/mediatek/mt8183/mt8183-afe-pcm.c |  8 +-
 2 files changed, 53 insertions(+), 50 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/5 ASoC: mediatek: mt8183: Fix wrong clock cleanup on clk_set_parent() failure

From: bui duc phuc <[email protected]>

In mt8183_afe_enable_clock(), when clk_set_parent() fails, the current error path incorrectly cleans up the previously enabled clock instead of the clock used by clk_set_parent().

Fix the error path to clean up the correct clock when clk_set_parent() fails.

Fixes a94aec035a12 ("ASoC: mediatek: mt8183: add platform driver")

---

Changes in v3:
- Add a Reviewed-by tag.

 sound/soc/mediatek/mt8183/mt8183-afe-clk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
sound/soc/mediatek/mt8183/mt8183-afe-clk.c +2 -2
index cc4f8f4d3dab..6ab0734ad136 100644
@@ -137,7 +137,7 @@ int mt8183_afe_enable_clock(struct mtk_base_afe *afe)
 		dev_err(afe->dev, "%s(), clk_set_parent %s-%s fail %d\n",
 			__func__, aud_clks[CLK_MUX_AUDIO],
 			aud_clks[CLK_CLK26M], ret);
-		goto CLK_MUX_AUDIO_ERR;
+		goto CLK_MUX_AUDIO_INTBUS_ERR;
 	}
 
 	ret = clk_prepare_enable(afe_priv->clk[CLK_MUX_AUDIOINTBUS]);
@@ -153,7 +153,7 @@ int mt8183_afe_enable_clock(struct mtk_base_afe *afe)
 		dev_err(afe->dev, "%s(), clk_set_parent %s-%s fail %d\n",
 			__func__, aud_clks[CLK_MUX_AUDIOINTBUS],
 			aud_clks[CLK_TOP_SYSPLL_D2_D4], ret);
-		goto CLK_MUX_AUDIO_INTBUS_ERR;
+		goto CLK_AFE_ERR;
 	}
 
 	ret = clk_prepare_enable(afe_priv->clk[CLK_AFE]);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/5 ASoC: mediatek: mt8183: Fix clock handling in mux disable path

In reply to [email protected]

From: bui duc phuc <[email protected]>

In the disable path of the appl*_mux_setting() functions, clk_disable_unprepare() is called after clk_set_parent(). If clk_set_parent() fails, clk_disable_unprepare() is skipped, potentially leaving one of the clocks enabled.

Move the clk_disable_unprepare() calls before clk_set_parent() so that the clocks are always disabled and unprepared in the disable path.

Fixes a94aec035a12 ("ASoC: mediatek: mt8183: add platform driver")
---
 sound/soc/mediatek/mt8183/mt8183-afe-clk.c | 70 +++++++++++-----------
 1 file changed, 36 insertions(+), 34 deletions(-)
sound/soc/mediatek/mt8183/mt8183-afe-clk.c +36 -34
index 6ab0734ad136..2dbe0b7f8bad 100644
@@ -265,25 +265,26 @@ static int apll1_mux_setting(struct mtk_base_afe *afe, bool enable)
 			goto ERR_SELECT_CLK_TOP_MUX_AUD_ENG1;
 		}
 	} else {
-		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_ENG1],
-				     afe_priv->clk[CLK_CLK26M]);
-		if (ret) {
-			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
-				__func__, aud_clks[CLK_TOP_MUX_AUD_ENG1],
-				aud_clks[CLK_CLK26M], ret);
-			goto EXIT;
-		}
 		clk_disable_unprepare(afe_priv->clk[CLK_TOP_MUX_AUD_ENG1]);
-
-		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_1],
-				     afe_priv->clk[CLK_CLK26M]);
-		if (ret) {
-			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
-				__func__, aud_clks[CLK_TOP_MUX_AUD_1],
-				aud_clks[CLK_CLK26M], ret);
-			goto EXIT;
-		}
 		clk_disable_unprepare(afe_priv->clk[CLK_TOP_MUX_AUD_1]);
+
+		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_ENG1],
+				     afe_priv->clk[CLK_CLK26M]);
+		if (ret) {
+			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
+				__func__, aud_clks[CLK_TOP_MUX_AUD_ENG1],
+				aud_clks[CLK_CLK26M], ret);
+			goto EXIT;
+		}
+
+		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_1],
+				     afe_priv->clk[CLK_CLK26M]);
+		if (ret) {
+			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
+				__func__, aud_clks[CLK_TOP_MUX_AUD_1],
+				aud_clks[CLK_CLK26M], ret);
+			goto EXIT;
+		}
 	}
 
 	return 0;
@@ -339,25 +340,26 @@ static int apll2_mux_setting(struct mtk_base_afe *afe, bool enable)
 			goto ERR_SELECT_CLK_TOP_MUX_AUD_ENG2;
 		}
 	} else {
-		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_ENG2],
-				     afe_priv->clk[CLK_CLK26M]);
-		if (ret) {
-			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
-				__func__, aud_clks[CLK_TOP_MUX_AUD_ENG2],
-				aud_clks[CLK_CLK26M], ret);
-			goto EXIT;
-		}
 		clk_disable_unprepare(afe_priv->clk[CLK_TOP_MUX_AUD_ENG2]);
-
-		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_2],
-				     afe_priv->clk[CLK_CLK26M]);
-		if (ret) {
-			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
-				__func__, aud_clks[CLK_TOP_MUX_AUD_2],
-				aud_clks[CLK_CLK26M], ret);
-			goto EXIT;
-		}
 		clk_disable_unprepare(afe_priv->clk[CLK_TOP_MUX_AUD_2]);
+
+		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_ENG2],
+				     afe_priv->clk[CLK_CLK26M]);
+		if (ret) {
+			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
+				__func__, aud_clks[CLK_TOP_MUX_AUD_ENG2],
+				aud_clks[CLK_CLK26M], ret);
+			goto EXIT;
+		}
+
+		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_2],
+				     afe_priv->clk[CLK_CLK26M]);
+		if (ret) {
+			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
+				__func__, aud_clks[CLK_TOP_MUX_AUD_2],
+				aud_clks[CLK_CLK26M], ret);
+			goto EXIT;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v3 2/5] ASoC: mediatek: mt8183: Fix clock handling in mux disable path

Quoted 34 lines · On 8/28/2026 7:05 AM, [email protected] wrote:
sound/soc/mediatek/mt8183/mt8183-afe-clk.c +11 -17
@@ -339,25 +340,26 @@ static int apll2_mux_setting(struct mtk_base_afe *afe, bool enable)
  			goto ERR_SELECT_CLK_TOP_MUX_AUD_ENG2;
  		}
  	} else {
-		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_ENG2],
-				     afe_priv->clk[CLK_CLK26M]);
-		if (ret) {
-			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
-				__func__, aud_clks[CLK_TOP_MUX_AUD_ENG2],
-				aud_clks[CLK_CLK26M], ret);
-			goto EXIT;
-		}
  		clk_disable_unprepare(afe_priv->clk[CLK_TOP_MUX_AUD_ENG2]);
-
-		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_2],
-				     afe_priv->clk[CLK_CLK26M]);
-		if (ret) {
-			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
-				__func__, aud_clks[CLK_TOP_MUX_AUD_2],
-				aud_clks[CLK_CLK26M], ret);
-			goto EXIT;
-		}
  		clk_disable_unprepare(afe_priv->clk[CLK_TOP_MUX_AUD_2]);
+
+		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_ENG2],
+				     afe_priv->clk[CLK_CLK26M]);
+		if (ret) {
+			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
+				__func__, aud_clks[CLK_TOP_MUX_AUD_ENG2],
+				aud_clks[CLK_CLK26M], ret);
+			goto EXIT;
+		}
+
+		ret = clk_set_parent(afe_priv->clk[CLK_TOP_MUX_AUD_2],

sound/soc/mediatek/mt8183/mt8183-afe-clk.c static int apll2_mux_setting(struct mtk_base_afe *afe, bool enable) · line 365

Quoted 10 lines
sound/soc/mediatek/mt8183/mt8183-afe-clk.c +7 -0
+				     afe_priv->clk[CLK_CLK26M]);
+		if (ret) {
+			dev_err(afe->dev, "%s clk_set_parent %s-%s fail %d\n",
+				__func__, aud_clks[CLK_TOP_MUX_AUD_2],
+				aud_clks[CLK_CLK26M], ret);
+			goto EXIT;
+		}
  	}
 
  	return 0;

I was thinking off a typical teardown procedure - everything is permissive:

// the disable path:

	ret = clk_set_parent(CLK_TOP_MUX_AUD_ENG2)
	if (ret)
		// just error reporting
	ret = clk_disable_unprepare(CLK_TOP_MUX_AUD_ENG2)
	if (ret)
		// just error reporting
	ret = clk_set_parent(CLK_TOP_MUX_AUD_2)
	if (ret)
		// just error reporting
	ret = clk_disable_unprepare(CLK_TOP_MUX_AUD_2)
	if (ret)
		// just error reporting

or, if one wants to do this properly: split apll1/2_mux_setting() functions into:

	void apll1/2_mux_disable()
	int apll1/2_mux_enable()

You do not have to go for the split, the first option is just fine.

#4 lore raw

Re: [PATCH v3 2/5] ASoC: mediatek: mt8183: Fix clock handling in mux disable path

Hi Cezary,

Thanks for the review and the suggestion.

Quoted 17 lines

I was thinking off a typical teardown procedure - everything is permissive:

// the disable path:

        ret = clk_set_parent(CLK_TOP_MUX_AUD_ENG2)
        if (ret)
                // just error reporting
        ret = clk_disable_unprepare(CLK_TOP_MUX_AUD_ENG2)
        if (ret)
                // just error reporting
        ret = clk_set_parent(CLK_TOP_MUX_AUD_2)
        if (ret)
                // just error reporting
        ret = clk_disable_unprepare(CLK_TOP_MUX_AUD_2)
        if (ret)
                // just error reporting
Quoted 6 lines

or, if one wants to do this properly: split apll1/2_mux_setting() functions into:

        void apll1/2_mux_disable()
        int apll1/2_mux_enable()

You do not have to go for the split, the first option is just fine.

I will follow the first approach and use a typical teardown procedure, where all steps are allowed to proceed and any errors are only reported.

Best regards,
Phuc

#5 lore raw

PATCH v3 3/5 ASoC: mediatek: mt8183: Fix APLL enable error handling

In reply to [email protected]

From: bui duc phuc <[email protected]>

Currently, the mt8183_apll*_enable() functions call mux_setting(afe, true) but do not check its return value to handle failures.

In addition, the cleanup paths of mt8183_apll*_enable() do not call mux_setting(afe, false) when the enable operation fails, while the mt8183_apll*_disable() functions do.

Add error handling for apll*_mux_setting() and call mux_setting(afe, false) in the cleanup paths when mt8183_apll*_enable() fails.

Fixes a94aec035a12 ("ASoC: mediatek: mt8183: add platform driver")

---

Changes in v3:
- Return directly from apllx_mux_setting() instead of using a goto label, as suggested by Cezary.

 sound/soc/mediatek/mt8183/mt8183-afe-clk.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
sound/soc/mediatek/mt8183/mt8183-afe-clk.c +10 -2
index 2dbe0b7f8bad..43d5898357dd 100644
@@ -384,7 +384,9 @@ int mt8183_apll1_enable(struct mtk_base_afe *afe)
 	int ret;
 
 	/* setting for APLL */
-	apll1_mux_setting(afe, true);
+	ret = apll1_mux_setting(afe, true);
+	if (ret)
+		return ret;
 
 	ret = clk_prepare_enable(afe_priv->clk[CLK_APLL22M]);
 	if (ret) {
@@ -413,6 +415,8 @@ int mt8183_apll1_enable(struct mtk_base_afe *afe)
 ERR_CLK_APLL1_TUNER:
 	clk_disable_unprepare(afe_priv->clk[CLK_APLL22M]);
 ERR_CLK_APLL22M:
+	apll1_mux_setting(afe, false);
+
 	return ret;
 }
 
@@ -438,7 +442,9 @@ int mt8183_apll2_enable(struct mtk_base_afe *afe)
 	int ret;
 
 	/* setting for APLL */
-	apll2_mux_setting(afe, true);
+	ret = apll2_mux_setting(afe, true);
+	if (ret)
+		return ret;
 
 	ret = clk_prepare_enable(afe_priv->clk[CLK_APLL24M]);
 	if (ret) {
@@ -467,6 +473,8 @@ int mt8183_apll2_enable(struct mtk_base_afe *afe)
 ERR_CLK_APLL2_TUNER:
 	clk_disable_unprepare(afe_priv->clk[CLK_APLL24M]);
 ERR_CLK_APLL24M:
+	apll2_mux_setting(afe, false);
+
 	return ret;
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v3 3/5
#6 lore raw

Re: [PATCH v3 3/5] ASoC: mediatek: mt8183: Fix APLL enable error handling

Quoted 9 lines · On 8/28/2026 7:05 AM, [email protected] wrote:

From: bui duc phuc <[email protected]>

Currently, the mt8183_apll*_enable() functions call mux_setting(afe, true) but do not check its return value to handle failures.

In addition, the cleanup paths of mt8183_apll*_enable() do not call mux_setting(afe, false) when the enable operation fails, while the mt8183_apll*_disable() functions do.

Quoted 5 lines

Add error handling for apll*_mux_setting() and call mux_setting(afe, false) in the cleanup paths when mt8183_apll*_enable() fails.

Fixes a94aec035a12 ("ASoC: mediatek: mt8183: add platform driver")
#7 lore raw

PATCH v3 4/5 ASoC: mediatek: mt8183: Use dev_err_probe() for error handling

In reply to [email protected]

From: bui duc phuc <[email protected]>

Replace dev_err() with dev_err_probe() to prevent log spam when probe returns -EPROBE_DEFER.

---

Changes in v3:
- Add a Reviewed-by tag.

 sound/soc/mediatek/mt8183/mt8183-afe-clk.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
sound/soc/mediatek/mt8183/mt8183-afe-clk.c +3 -6
index 43d5898357dd..c67424bed329 100644
@@ -101,12 +101,9 @@ int mt8183_init_clock(struct mtk_base_afe *afe)
 
 	for (i = 0; i < CLK_NUM; i++) {
 		afe_priv->clk[i] = devm_clk_get(afe->dev, aud_clks[i]);
-		if (IS_ERR(afe_priv->clk[i])) {
-			dev_err(afe->dev, "%s(), devm_clk_get %s fail, ret %ld\n",
-				__func__, aud_clks[i],
-				PTR_ERR(afe_priv->clk[i]));
-			return PTR_ERR(afe_priv->clk[i]);
-		}
+		if (IS_ERR(afe_priv->clk[i]))
+			return dev_err_probe(afe->dev, PTR_ERR(afe_priv->clk[i]),
+					     "failed to get clock %s\n", aud_clks[i]);
 	}
 
 	return 0;
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 5/5 ASoC: mediatek: mt8183: Drop redundant probe error messages

In reply to [email protected]

From: bui duc phuc <[email protected]>

The errors handled here are already reported by the called functions, either directly or deeper in the call chain. Therefore, the additional dev_err() calls are redundant and can be removed.

---

Changes in v3:
- Add a Reviewed-by tag.

 sound/soc/mediatek/mt8183/mt8183-afe-pcm.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
sound/soc/mediatek/mt8183/mt8183-afe-pcm.c +2 -6
index 2634699534db..46b7a2bb6aaa 100644
@@ -809,10 +809,8 @@ static int mt8183_afe_pcm_dev_probe(struct platform_device *pdev)
 
 	/* initial audio related clock */
 	ret = mt8183_init_clock(afe);
-	if (ret) {
-		dev_err(dev, "init clock error\n");
+	if (ret)
 		return ret;
-	}
 
 	pm_runtime_enable(dev);
 
@@ -903,10 +901,8 @@ static int mt8183_afe_pcm_dev_probe(struct platform_device *pdev)
 
 	ret = devm_request_irq(dev, irq_id, mt8183_afe_irq_handler,
 			       IRQF_TRIGGER_NONE, "asys-isr", (void *)afe);
-	if (ret) {
-		dev_err(dev, "could not request_irq for asys-isr\n");
+	if (ret)
 		goto err_pm_disable;
-	}
 
 	/* init sub_dais */
 	INIT_LIST_HEAD(&afe->sub_dais);
Signature · 1 line
2.43.0