ASoC: rockchip: rockchip_sai: Fix hclk and runtime PM handling in probe
Subsystems (4)
PATCH 1/1 ASoC: rockchip: rockchip_sai: Fix hclk and runtime PM handling in probe
Probe powers the device up by calling rockchip_sai_runtime_resume() directly, but runtime PM is not told about it and the status stays suspended. The pm_runtime_put() at the end of probe then does nothing, so the mclk and hclk enables taken by that call are never dropped: both clocks stay on while the device is idle, and mclk stays on after unbind.
hclk is also got with devm_clk_get_enabled(), although probe drops its own enable with clk_disable_unprepare() once the device is registered, so devres disables it a second time at unbind. With CONFIG_PM the two bugs cancel out for hclk; without it hclk is disabled once too often.
Get hclk with devm_clk_get() and enable it by hand, as before the conversion, and disable it on the probe error paths. Resume the device first, mark it active and only then enable runtime PM. Use pm_runtime_put_sync() on the error path, so the device is suspended before devres disables runtime PM.
The two fixes go together: either one alone leaves hclk unbalanced.
---
Two fixes for the clock handling in rockchip_sai_probe().
1. hclk is got with devm_clk_get_enabled(), but probe also drops the enable by hand once the device is registered, so devres disables the clock a second time at unbind. Get it with devm_clk_get() and enable it by hand, as before the conversion.
2. Probe powers the device up by calling the runtime resume callback directly without marking it active, so the pm_runtime_put() at the end of probe does nothing and mclk and hclk are never released. Resume first, mark the device active, then enable runtime PM.
Found by checks derived from the spec2patch kernel API specifications, with the help of an AI coding assistant (Claude); I reviewed the analysis and the fix.
--- sound/soc/rockchip/rockchip_sai.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)
sound/soc/rockchip/rockchip_sai.c +25 -9
@@ -1459,20 +1459,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" ); +/* +* hclk is only needed until runtime PM takes over below, so it is +* enabled by hand and not with devm_clk_get_enabled(). +*/ + 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) { +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 ret; + goto err_disable_hclk;/* * From here on, all register accesses need to be wrapped in
@@ -1480,11 +1490,15 @@ 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) -return dev_err_probe (&pdev->dev, ret,"Failed to resume device\n" ); +if (ret) { +dev_err_probe (&pdev->dev, ret,"Failed to resume device\n" ); + goto err_disable_hclk; + } + +pm_runtime_set_active (&pdev->dev); +pm_runtime_get_noresume (&pdev->dev); +devm_pm_runtime_enable (&pdev->dev); ret =devm_snd_dmaengine_pcm_register (&pdev->dev,NULL ,0 );if (ret)
@@ -1505,9 +1519,11 @@ static int rockchip_sai_probe(struct platform_device *pdev) err_runtime_suspend:if (IS_ENABLED (CONFIG_PM )) -pm_runtime_put (&pdev->dev); +pm_runtime_put_sync (&pdev->dev);else rockchip_sai_runtime_suspend (&pdev->dev); +err_disable_hclk: +clk_disable_unprepare (sai->hclk);return ret; }
---
base-commit: 25faec9ce61c01fb45abf812a222c5b43c3cd5c5
change-id: 20261129-s2p-d08-dd5b1bd68fab
Best regards,
Signature · 1 line
Arina Kharlamova <[email protected]>
Nicolas Frattaroli
maintainer+Cc Bui Duc Phuc
Quoted 121 lines · On Monday, 30 November 2026 23:56:00 Central European Standard Time Arina…
From: Arina Kharlamova <[email protected]>
Probe powers the device up by calling
rockchip_sai_runtime_resume()directly, but runtime PM is not told about it and the status stays suspended. Thepm_runtime_put()at the end of probe then does nothing, so the mclk and hclk enables taken by that call are never dropped: both clocks stay on while the device is idle, and mclk stays on after unbind.hclk is also got with
devm_clk_get_enabled(), although probe drops its own enable withclk_disable_unprepare()once the device is registered, so devres disables it a second time at unbind. WithCONFIG_PMthe two bugs cancel out for hclk; without it hclk is disabled once too often.Get hclk with
devm_clk_get()and enable it by hand, as before the conversion, and disable it on the probe error paths. Resume the device first, mark it active and only then enable runtime PM. Usepm_runtime_put_sync()on the error path, so the device is suspended before devres disables runtime PM.The two fixes go together: either one alone leaves hclk unbalanced.
Fixes cc78d1eaabad ("ASoC: rockchip: add Serial Audio Interface (SAI) driver")Fixes 03b778d19948 ("ASOC: rockchip: Use helper function devm_clk_get_enabled()")Signed-off-by Arina Kharlamova <[email protected]>---
Two fixes for the clock handling inrockchip_sai_probe().1. hclk is got with
devm_clk_get_enabled(), but probe also drops the enable by hand once the device is registered, so devres disables the clock a second time at unbind. Get it withdevm_clk_get()and enable it by hand, as before the conversion.2. Probe powers the device up by calling the runtime resume callback directly without marking it active, so the
pm_runtime_put()at the end of probe does nothing and mclk and hclk are never released. Resume first, mark the device active, then enable runtime PM.Found by checks derived from the spec2patch kernel API specifications, with the help of an AI coding assistant (Claude); I reviewed the analysis and the fix.
--- sound/soc/rockchip/rockchip_sai.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)sound/soc/rockchip/rockchip_sai.c +25
-10@@ -1459,20 +1459,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" ); +/* +* hclk is only needed until runtime PM takes over below, so it is +* enabled by hand and not with devm_clk_get_enabled(). +*/ + 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) { +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 ret; + goto err_disable_hclk;/* * From here on, all register accesses need to be wrapped in @@ -1480,11 +1490,15 @@ 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) -return dev_err_probe (&pdev->dev, ret,"Failed to resume device\n" ); +if (ret) { +dev_err_probe (&pdev->dev, ret,"Failed to resume device\n" ); + goto err_disable_hclk; + } + +pm_runtime_set_active (&pdev->dev); +pm_runtime_get_noresume (&pdev->dev); +devm_pm_runtime_enable (&pdev->dev); ret =devm_snd_dmaengine_pcm_register (&pdev->dev,NULL ,0 );if (ret)@@ -1505,9 +1519,11 @@ static int rockchip_sai_probe(struct platform_device *pdev) err_runtime_suspend:if (IS_ENABLED (CONFIG_PM )) -pm_runtime_put (&pdev->dev); +pm_runtime_put_sync (&pdev->dev);else rockchip_sai_runtime_suspend (&pdev->dev); +err_disable_hclk: +clk_disable_unprepare (sai->hclk);return ret; } ---base-commit:
25faec9ce61c01fb45abf812a222c5b43c3cd5c5
change-id: 20261129-s2p-d08-dd5b1bd68fab
Quoted 6 lines
Best regards,
Signature · 1 line
Arina Kharlamova <[email protected]>
I'll leave reviewing this one in-depth up to bui duc phuc, who has been sent hardware (by me) that uses this driver and has done work wrt runtime PM handling in this driver already. Entirely possible that instead of this mess there's a much simpler solution here, like setting up and enabling RPM earlier so this manual accounting during probe doesn't need to be done at all.
Regards,
Nicolas Frattaroli
_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Bui Duc Phuc
Reviewed-byHi Nicolas,
Quoted 4 lines
I'll leave reviewing this one in-depth up to bui duc phuc, who has been sent hardware (by me) that uses this driver and has done work wrt runtime PM handling in this driver already.
Thank you for personally sending me the board. I really appreciate your generosity. Having real hardware to test this driver on means a lot to an independent developer like me. It's still in transit, so I don't have it yet and can't test Arina's patch on it.
Quoted 13 lines
Quoted 13 lines
From: Arina Kharlamova <[email protected]>
Probe powers the device up by calling
rockchip_sai_runtime_resume()directly, but runtime PM is not told about it and the status stays suspended. Thepm_runtime_put()at the end of probe then does nothing, so the mclk and hclk enables taken by that call are never dropped: both clocks stay on while the device is idle, and mclk stays on after unbind.hclk is also got with
devm_clk_get_enabled(), although probe drops its own enable withclk_disable_unprepare()once the device is registered, so devres disables it a second time at unbind. WithCONFIG_PMthe two bugs cancel out for hclk; without it hclk is disabled once too often.
Quoted 7 lines
Quoted 7 lines
Get hclk with
devm_clk_get()and enable it by hand, as before the conversion, and disable it on the probe error paths. Resume the device first, mark it active and only then enable runtime PM. Usepm_runtime_put_sync()on the error path, so the device is suspended before devres disables runtime PM.The two fixes go together: either one alone leaves hclk unbalanced.
As for the review: The analysis is correct, and I checked that the clock enables counts balance on all paths, with and without CONFIG_PM.
FWIW, it addresses the same two issues as the patches I first posted in June, which haven't been reviewed yet:
Fix unbalanced clock reference count:
https://lore.kernel.org/all/[email protected]/ (v2)
Runtime PM status fix:
https://lore.kernel.org/all/[email protected]/
Arina's version does both in a single bisectable change with Fixes:
tags, so I'm fine with taking it instead of mine.
Quoted 4 lines
Quoted 4 lines
Fixes cc78d1eaabad ("ASoC: rockchip: add Serial Audio Interface (SAI) driver")Fixes 03b778d19948 ("ASOC: rockchip: Use helper function devm_clk_get_enabled()")Signed-off-by Arina Kharlamova <[email protected]>
Quoted 4 lines
Entirely possible that instead of this
mess there's a much simpler solution here, like setting up and enabling RPM earlier so this manual accounting during probe doesn't need to be done at all.
Agreed. Enabling runtime PM right after getting the clocks and using pm_runtime_resume_and_get()/pm_runtime_put_sync() around the register accesses removes the manual accounting and the manual hclk handling. For !CONFIG_PM, the device is powered up by hand and a devm action powers it down, which also replaces .remove. It also moves the IRQ request after the device setup, since the IRQ is IRQF_SHARED and the ISR reads registers.
Roughly:
-----------------------------------------------------------------------
static void rockchip_sai_runtime_suspend_action(void *data)
{
if (!pm_runtime_status_suspended(dev))
rockchip_sai_runtime_suspend(dev);
}rockchip_sai_probe()
{
/* resets, regmap, irq = platform_get_irq_optional() */
/* mclk, hclk = devm_clk_get(): no manual enable */
devm_add_action(dev, rockchip_sai_runtime_suspend_action, dev);
devm_pm_runtime_enable(dev);
if(!pm_runtime_enabled(dev)) /* !CONFIG_PM */
rockchip_sai_runtime_resume(dev);
pm_runtime_resume_and_get(dev);
/* on error above: pm_runtime_put_noidle() and return */
regmap_read(SAI_VERSION);
rockchip_sai_init_dai();
rockchip_sai_parse_paths();
if (irq > 0)
devm_request_irq(..., IRQF_SHARED, ...);
pm_runtime_put_sync(dev);
devm_snd_dmaengine_pcm_register();
return devm_snd_soc_register_component();
}/* .remove is dropped: the devm action powers the device down if it is
* still on, which is always the case with !CONFIG_PM */
-----------------------------------------------------------------------------
As this is untested and changes more behaviour than Arina's patch, I'd suggest taking hers as the fix for stable. Once the board arrives, I'll test her patch first and then send this as a proper patch on top.
Best regards,
Phuc
Mark Brown
maintainerQuoted 1 line · Arina Kharlamova
ASoC: rockchip:
rockchip_sai: Fix hclk and runtime PM handling in probe
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.5
Thanks!
[1/1] ASoC: rockchip: rockchip_sai: Fix hclk and runtime PM handling in probe
https://git.kernel.org/broonie/sound/c/c187d0ead70a
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark