Skip to contentSkip to conversation

ASoC: rockchip: rockchip_sai: Hand over hclk control exclusively to Runtime PM

PATCH In review [email protected] 3 participants 6 messages Jun 22 – Jul 22

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

PATCH 1/1 ASoC: rockchip: rockchip_sai: Hand over hclk control exclusively to Runtime PM

From: bui duc phuc <[email protected]>

Although switching to devm_clk_get_enabled() in a previous patch was tested successfully, it introduces overlapping ownership of the clock lifecycle. Since the driver requires early register access to read the device version during probe(), enabling hclk at that point is mandatory. However, relying on devres for automatic disabling at unbind, while calling clk_disable_unprepare() manually at the end of probe() alongside Runtime PM's autosuspend, creates redundant and overlapping clock management.

While this mixed approach might not trigger errors under normal test conditions, it is architecturally sub-optimal. As stated in the code comments, Runtime PM should exclusively own the clock for register accesses after the initial configuration phase.

Clean up the design by:

1 Reverting back to devm_clk_get() to remove the implicit devres
enable/disable behavior.
2 Manually enabling and disabling hclk explicitly only around the
early register access before Runtime PM takes over.
3 Dropping the stray clk_disable_unprepare() at the end of probe()
so Runtime PM solely owns hclk afterward.

---

Links:
1 This change is based on the discussion around manual hclk handing during probe(),
as raised by Krysztof:
https://lore.kernel.org/all/[email protected]/#t
2 Background for the earlier devm_clk_get_enbabled() conversion:
https://lore.kernel.org/all/2818018.CQOukoFCf9@workhorse/

An alternative approach would be use devm_regmap_init_mmio_clk() and let regmap manage clock enablement around register accesses. If preferred, I can rework the driver accordingly.

 sound/soc/rockchip/rockchip_sai.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
sound/soc/rockchip/rockchip_sai.c +20 -7
index a195e96fed0a..ebb491fac77d 100644
@@ -1438,20 +1438,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
@@ -1482,8 +1492,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:
@@ -1493,6 +1501,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
maintainer
#1 lore raw
Quoted 5 lines · On Mon, Jun 22, 2026 at 07:56:13AM +0700, [email protected] wrote:

Although switching to devm_clk_get_enabled() in a previous patch was tested successfully, it introduces overlapping ownership of the clock lifecycle. Since the driver requires early register access to read the device version during probe(), enabling hclk at that point is mandatory.

Quoted 8 lines

Clean up the design by:

1 Reverting back to devm_clk_get() to remove the implicit devres
enable/disable behavior.
2 Manually enabling and disabling hclk explicitly only around the
early register access before Runtime PM takes over.
3 Dropping the stray clk_disable_unprepare() at the end of probe()
so Runtime PM solely owns hclk afterward.

Note that runtime PM can be disabled at build time so we might not have runtime PM at all...

Quoted 23 lines

Links:
1 This change is based on the discussion around manual hclk handing during probe(),
as raised by Krysztof:
https://lore.kernel.org/all/[email protected]/#t
2 Background for the earlier devm_clk_get_enbabled() conversion:
https://lore.kernel.org/all/2818018.CQOukoFCf9@workhorse/

An alternative approach would be use devm_regmap_init_mmio_clk() and let regmap manage clock enablement around register accesses. If preferred, I can rework the driver accordingly.

sound/soc/rockchip/rockchip_sai.c +5 -1
-	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");
+

sound/soc/rockchip/rockchip_sai.c static int rockchip_sai_probe(struct platform_device *pdev) · line 1495

Quoted 6 lines
sound/soc/rockchip/rockchip_sai.c +0 -2
@@ -1482,8 +1492,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;

Are you sure that the runtime PM state there is such that it knows a reference is held? The driver used pm_runtime_get_noresume() so the device didn't have RPM_ACTIVE set I think?

The runtime PM API really is a miserable collection of landmines :(

#2 lore raw

Hi Mark,

Thank you for your review.

Quoted 10 lines
Quoted 6 lines

1 Reverting back to devm_clk_get() to remove the implicit devres
enable/disable behavior.
2 Manually enabling and disabling hclk explicitly only around the
early register access before Runtime PM takes over.
3 Dropping the stray clk_disable_unprepare() at the end of probe()
so Runtime PM solely owns hclk afterward.

Note that runtime PM can be disabled at build time so we might not have runtime PM at all...

Thanks for pointing this out. You're right that with !CONFIG_PM, the driver only relies on the
two manual calls to rokchip_sai_runtime_resume() / suspend(), so hclk stays enabled the
whole time. I understand this is unvavoidable in that configuration, throgh, since there's no
Runtime PM to re-enable the clock when it's needed.

I'll update the commit message to reflect that the driver uses a combination of Runtime PM
and explicit manual enable/disable, rather than relying on Runtime PM alone.

Quoted 26 lines
Quoted 26 lines

Links:
1 This change is based on the discussion around manual hclk handing during probe(),
as raised by Krysztof:
https://lore.kernel.org/all/[email protected]/#t
2 Background for the earlier devm_clk_get_enbabled() conversion:
https://lore.kernel.org/all/2818018.CQOukoFCf9@workhorse/

An alternative approach would be use devm_regmap_init_mmio_clk() and let regmap manage clock enablement around register accesses. If preferred, I can rework the driver accordingly.

sound/soc/rockchip/rockchip_sai.c +5 -1
-     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");
+
@@ -1482,8 +1492,6 @@ static int rockchip_sai_probe(struct platform_device *pdev)
      pm_runtime_use_autosuspend(&pdev->dev);
      pm_runtime_put(&pdev->dev);
 
Quoted 7 lines

sound/soc/rockchip/rockchip_sai.c static int rockchip_sai_probe(struct platform_device *pdev) · line 1495

Quoted 3 lines
sound/soc/rockchip/rockchip_sai.c +0 -2
-     clk_disable_unprepare(sai->hclk);
-
      return 0;

Are you sure that the runtime PM state there is such that it knows a reference is held? The driver used pm_runtime_get_noresume() so the device didn't have RPM_ACTIVE set I think?

You are right, pm_runtime_get_noresume() doesn't set RPM_ACTIVE. I think we need to add
pm_runtime_set_active() before pm_runtime_enable(). Otherwise, with CONFIG_PM, the pm_runtime_put() at the end of probe() might skip the suspend, since the core still considers
the device suspended .

Quoted 2 lines

The runtime PM API really is a miserable collection of landmines :(

Yeah, plenty of landmines indeed :(
I checked, and rockchip_spdif.c does use devm_regmap_init_mmio_clk() for hclk, rather than wrapping every register access in pm_runtime_get_sync() / pm_runtime_put()
the way rockchip_sai does.

Best regards,
Phuc

#3 lore raw

Hi all,

Regarding the case where PM configuration is not enabled, with the old source code, I suspect there is an unbalanced clk_disable_unprepare() call on hclk when the driver is unbound after a successful probe under CONFIG_PM=n.

The actual enable_count / prepare_count sequence for hclk (with values clamped at 0) would be:

probe:

devm_clk_get_enabled 0 -> 1
runtime_resume (manual) 1 -> 2
clk_disable_unprepare 2 -> 1 (at the end of probe)

unbind:

remove -> runtime_suspend 1 -> 0 (ops->disable/unprepare executed here)
devm cleanup 0 -> WARN "already disabled" /
"already unprepared"

This conclusion is based solely on code inspection; I do not have hardware available to verify it.
I noticed that Nicolas tested and ACKed the use of devm_clk_get_enabled(), so I'm not sure whether that testing included the CONFIG_PM=n configuration.

https://lore.kernel.org/all/2818018.CQOukoFCf9@workhorse/

If it did, then I may have overlooked something.

@Nicolas (or anyone familiar with this), could you please help double-check if my understanding is correct?

Best regards,
Phuc

Quoted 81 lines · Bui Duc Phuc

Hi Mark,

Thank you for your review.

Quoted 10 lines
Quoted 6 lines

1 Reverting back to devm_clk_get() to remove the implicit devres
enable/disable behavior.
2 Manually enabling and disabling hclk explicitly only around the
early register access before Runtime PM takes over.
3 Dropping the stray clk_disable_unprepare() at the end of probe()
so Runtime PM solely owns hclk afterward.

Note that runtime PM can be disabled at build time so we might not have runtime PM at all...

Thanks for pointing this out. You're right that with !CONFIG_PM, the driver only relies on the
two manual calls to rokchip_sai_runtime_resume() / suspend(), so hclk stays enabled the
whole time. I understand this is unvavoidable in that configuration, throgh, since there's no
Runtime PM to re-enable the clock when it's needed.

I'll update the commit message to reflect that the driver uses a combination of Runtime PM
and explicit manual enable/disable, rather than relying on Runtime PM alone.

Quoted 26 lines
Quoted 26 lines

Links:
1 This change is based on the discussion around manual hclk handing during probe(),
as raised by Krysztof:
https://lore.kernel.org/all/[email protected]/#t
2 Background for the earlier devm_clk_get_enbabled() conversion:
https://lore.kernel.org/all/2818018.CQOukoFCf9@workhorse/

An alternative approach would be use devm_regmap_init_mmio_clk() and let regmap manage clock enablement around register accesses. If preferred, I can rework the driver accordingly.

sound/soc/rockchip/rockchip_sai.c +5 -1
-     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");
+
@@ -1482,8 +1492,6 @@ static int rockchip_sai_probe(struct platform_device *pdev)
      pm_runtime_use_autosuspend(&pdev->dev);
      pm_runtime_put(&pdev->dev);
 
Quoted 7 lines

sound/soc/rockchip/rockchip_sai.c static int rockchip_sai_probe(struct platform_device *pdev) · line 1495

Quoted 3 lines
sound/soc/rockchip/rockchip_sai.c +0 -2
-     clk_disable_unprepare(sai->hclk);
-
      return 0;

Are you sure that the runtime PM state there is such that it knows a reference is held? The driver used pm_runtime_get_noresume() so the device didn't have RPM_ACTIVE set I think?

You are right, pm_runtime_get_noresume() doesn't set RPM_ACTIVE. I think we need to add
pm_runtime_set_active() before pm_runtime_enable(). Otherwise, with CONFIG_PM, the pm_runtime_put() at the end of probe() might skip the suspend, since the core still considers
the device suspended .

Quoted 2 lines

The runtime PM API really is a miserable collection of landmines :(

Yeah, plenty of landmines indeed :(
I checked, and rockchip_spdif.c does use devm_regmap_init_mmio_clk() for hclk, rather than wrapping every register access in pm_runtime_get_sync() / pm_runtime_put()
the way rockchip_sai does.

Best regards,
Phuc

#4 lore raw

Hello, finally got around to responding to this.

Quoted 6 lines · On Wednesday, 24 June 2026 04:37:33 Central European Summer Time Bui Duc Phuc…

Hi all,

Regarding the case where PM configuration is not enabled, with the old source code, I suspect there is an unbalanced clk_disable_unprepare() call on hclk when the driver is unbound after a successful probe under CONFIG_PM=n.

If "old source code" refers to what's currently in the tree with the devm_clk_get_enabled, then yes. There's the manual resume function call which isn't balanced.

I think your follow-up patch does make sense in light of that.

Quoted 10 lines

The actual enable_count / prepare_count sequence for hclk (with values clamped at 0) would be:

probe:

devm_clk_get_enabled 0 -> 1
runtime_resume (manual) 1 -> 2
clk_disable_unprepare 2 -> 1 (at the end of probe)

Quoted 8 lines

unbind:

remove -> runtime_suspend 1 -> 0 (ops->disable/unprepare executed here)
devm cleanup 0 -> WARN "already disabled" /
"already unprepared"

This conclusion is based solely on code inspection; I do not have hardware available to verify it.

I'll e-mail you off-list to see if we can rectify that.

Quoted 2 lines

I noticed that Nicolas tested and ACKed the use of devm_clk_get_enabled(), so I'm not sure whether that testing included the CONFIG_PM=n configuration.

Nope, I basically never build with PM/RPM disabled.

Quoted 8 lines

https://lore.kernel.org/all/2818018.CQOukoFCf9@workhorse/

If it did, then I may have overlooked something.

@Nicolas (or anyone familiar with this), could you please help double-check if my understanding is correct?

Quoted 44 lines

Best regards,
Phuc

Quoted 38 lines · Bui Duc Phuc

Hi Mark,

Thank you for your review.

Quoted 10 lines
Quoted 6 lines

1 Reverting back to devm_clk_get() to remove the implicit devres
enable/disable behavior.
2 Manually enabling and disabling hclk explicitly only around the
early register access before Runtime PM takes over.
3 Dropping the stray clk_disable_unprepare() at the end of probe()
so Runtime PM solely owns hclk afterward.

Note that runtime PM can be disabled at build time so we might not have runtime PM at all...

Thanks for pointing this out. You're right that with !CONFIG_PM, the driver only relies on the
two manual calls to rokchip_sai_runtime_resume() / suspend(), so hclk stays enabled the
whole time. I understand this is unvavoidable in that configuration, throgh, since there's no
Runtime PM to re-enable the clock when it's needed.

I'll update the commit message to reflect that the driver uses a combination of Runtime PM
and explicit manual enable/disable, rather than relying on Runtime PM alone.

Quoted 10 lines
Quoted 10 lines

Links:
1 This change is based on the discussion around manual hclk handing during probe(),
as raised by Krysztof:
https://lore.kernel.org/all/[email protected]/#t
2 Background for the earlier devm_clk_get_enbabled() conversion:
https://lore.kernel.org/all/2818018.CQOukoFCf9@workhorse/

An alternative approach would be use devm_regmap_init_mmio_clk() and let regmap manage clock enablement around register accesses. If preferred, I can rework the driver accordingly.

Hm, I'm worried only ungating the clock around register accesses isn't going to go well if the clock is needed by the internal logic of the device as well.

Quoted 25 lines
Quoted 25 lines
Quoted 23 lines
Quoted 13 lines
sound/soc/rockchip/rockchip_sai.c +5 -1
-     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");
+

sound/soc/rockchip/rockchip_sai.c static int rockchip_sai_probe(struct platform_device *pdev) · line 1495

Quoted 6 lines
sound/soc/rockchip/rockchip_sai.c +0 -2
@@ -1482,8 +1492,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;

Are you sure that the runtime PM state there is such that it knows a reference is held? The driver used pm_runtime_get_noresume() so the device didn't have RPM_ACTIVE set I think?

Quoted 6 lines
Quoted 6 lines

You are right, pm_runtime_get_noresume() doesn't set RPM_ACTIVE. I think we need to add
pm_runtime_set_active() before pm_runtime_enable(). Otherwise, with CONFIG_PM, the pm_runtime_put() at the end of probe() might skip the suspend, since the core still considers
the device suspended .

Agreed, this needs fixing. That is an odd behaviour of the PM API.

Quoted 13 lines
Quoted 12 lines
Quoted 3 lines

The runtime PM API really is a miserable collection of landmines :(

Yeah, plenty of landmines indeed :(
I checked, and rockchip_spdif.c does use devm_regmap_init_mmio_clk() for hclk, rather than wrapping every register access in pm_runtime_get_sync() / pm_runtime_put()
the way rockchip_sai does.

Best regards,
Phuc

#5 lore raw

Hi Nicolas,

Thanks for confirming and for the detailed feedback.

Quoted 18 lines
Quoted 4 lines

Regarding the case where PM configuration is not enabled, with the old source code, I suspect there is an unbalanced clk_disable_unprepare() call on hclk when the driver is unbound after a successful probe under CONFIG_PM=n.

If "old source code" refers to what's currently in the tree with the devm_clk_get_enabled, then yes. There's the manual resume function call which isn't balanced.

I think your follow-up patch does make sense in light of that.

Quoted 2 lines

I noticed that Nicolas tested and ACKed the use of devm_clk_get_enabled(), so I'm not sure whether that testing included the CONFIG_PM=n configuration.

Nope, I basically never build with PM/RPM disabled.

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

You are right, pm_runtime_get_noresume() doesn't set RPM_ACTIVE. I think we need to add
pm_runtime_set_active() before pm_runtime_enable(). Otherwise, with CONFIG_PM, the pm_runtime_put() at the end of probe() might skip the suspend, since the core still considers
the device suspended .

Agreed, this needs fixing. That is an odd behaviour of the PM API.

I'll update the commit message accordingly and include the runtime PM state fix in the next revision.

Best regards,
Phuc