Skip to contentSkip to conversation

ASoC: rockchip: rockchip_sai: Fix unbalanced clock reference count when CONFIG_PM=n

PATCH New [email protected] 1 participant 2 messages Jul 22

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 ASoC: rockchip: rockchip_sai: Fix unbalanced clock reference count when CONFIG_PM=n

From: bui duc phuc <[email protected]>

When CONFIG_PM is disabled, Runtime PM callbacks are stubbed out. Using devm_clk_get_enabled() causes an unbalanced clk_disable_unprepare() upon driver unbind, as devm cleanup attempts to disable the clock a second time after manual disabling, triggering clock framework warnings. Replace devm_clk_get_enabled() with devm_clk_get() and manually manage hclk enablement around probe register accesses to keep clock reference counts balanced regardless of CONFIG_PM.

---

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

Changes in v2:
- Update the commit message.

 sound/soc/rockchip/rockchip_sai.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
sound/soc/rockchip/rockchip_sai.c +20 -7
index 585e89f61f0d..9a80e120dbda 100644
@@ -1443,20 +1443,30 @@ static int rockchip_sai_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, PTR_ERR(sai->mclk),
 				     "Failed to get mclk\n");
 
-	sai->hclk = devm_clk_get_enabled(&pdev->dev, "hclk");
+	sai->hclk = devm_clk_get(&pdev->dev, "hclk");
 	if (IS_ERR(sai->hclk))
 		return dev_err_probe(&pdev->dev, PTR_ERR(sai->hclk),
 				     "Failed to get hclk\n");
 
+	ret = clk_prepare_enable(sai->hclk);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "Failed to enable hclk\n");
+
 	regmap_read(sai->regmap, SAI_VERSION, &sai->version);
 
 	ret = rockchip_sai_init_dai(sai, res, &dai);
-	if (ret)
-		return dev_err_probe(&pdev->dev, ret, "Failed to initialize DAI\n");
+	if (ret) {
+		ret = dev_err_probe(&pdev->dev, ret, "Failed to initialize DAI\n");
+		goto err_disable_hclk;
+	}
 
 	ret = rockchip_sai_parse_paths(sai, node);
-	if (ret)
-		return dev_err_probe(&pdev->dev, ret, "Failed to parse paths\n");
+	if (ret) {
+		ret = dev_err_probe(&pdev->dev, ret, "Failed to parse paths\n");
+		goto err_disable_hclk;
+	}
+
+	clk_disable_unprepare(sai->hclk);
 
 	/*
 	 * From here on, all register accesses need to be wrapped in
@@ -1487,8 +1497,6 @@ static int rockchip_sai_probe(struct platform_device *pdev)
 	pm_runtime_use_autosuspend(&pdev->dev);
 	pm_runtime_put(&pdev->dev);
 
-	clk_disable_unprepare(sai->hclk);
-
 	return 0;
 
 err_runtime_suspend:
@@ -1498,6 +1506,11 @@ static int rockchip_sai_probe(struct platform_device *pdev)
 		rockchip_sai_runtime_suspend(&pdev->dev);
 
 	return ret;
+
+err_disable_hclk:
+	clk_disable_unprepare(sai->hclk);
+
+	return ret;
 }
 
 static void rockchip_sai_remove(struct platform_device *pdev)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 2/2 ASoC: rockchip: rockchip_sai: Fix runtime PM state after manual resume

From: bui duc phuc <[email protected]>

Probe manually resumes the device after pm_runtime_get_noresume(). Since the PM core is bypassed, runtime_status remains RPM_SUSPENDED even though the hardware is active. A subsequent pm_runtime_put() therefore skips runtime_suspend(), leaving the device permanently powered and preventing autosuspend.

Fix this by calling pm_runtime_set_active() after the manual resume succeeds, and defer pm_runtime_enable() until after the device has been manually resumed and marked active. Also call pm_runtime_put_noidle() on error to drop the usage count acquired by pm_runtime_get_noresume().

---
 sound/soc/rockchip/rockchip_sai.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
sound/soc/rockchip/rockchip_sai.c +5 -2
index 9a80e120dbda..55d17536c3b5 100644
@@ -1474,11 +1474,14 @@ static int rockchip_sai_probe(struct platform_device *pdev)
 	 *
 	 * NB: we don't rely on _resume_and_get in case of !CONFIG_PM
 	 */
-	devm_pm_runtime_enable(&pdev->dev);
 	pm_runtime_get_noresume(&pdev->dev);
 	ret = rockchip_sai_runtime_resume(&pdev->dev);
-	if (ret)
+	if (ret) {
+		pm_runtime_put_noidle(&pdev->dev);
 		return dev_err_probe(&pdev->dev, ret, "Failed to resume device\n");
+	}
+	pm_runtime_set_active(&pdev->dev);
+	devm_pm_runtime_enable(&pdev->dev);
 
 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
 	if (ret) {
Signature · 1 line
2.43.0