Skip to contentSkip to conversation

net: dsa: bcm_sf2: Use devm_clk_get_optional_enabled() in bcm_sf2_sw_probe()

PATCH Superseded Markus Elfring 4 participants 7 messages Nov 19 – Nov 20

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: dsa: bcm_sf2: Use devm_clk_get_optional_enabled() in bcm_sf2_sw_probe()

From: Markus Elfring <[email protected]>
Date: Thu, 19 Nov 2026 17:30:09 +0100

Reuse existing functionality from devm_clk_get_optional_enabled() instead of keeping duplicate source code.

The source code was transformed by using the Coccinelle software.

---
 drivers/net/dsa/bcm_sf2.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/net/dsa/bcm_sf2.c +1 -5
index 1747a1f20c78..011ce79392e8 100644
@@ -1467,14 +1467,10 @@ static int bcm_sf2_sw_probe(struct platform_device *pdev)
 		base++;
 	}
 
-	priv->clk = devm_clk_get_optional(&pdev->dev, "sw_switch");
+	priv->clk = devm_clk_get_optional_enabled(&pdev->dev, "sw_switch");
 	if (IS_ERR(priv->clk))
 		return PTR_ERR(priv->clk);
 
-	ret = clk_prepare_enable(priv->clk);
-	if (ret)
-		return ret;
-
 	priv->clk_mdiv = devm_clk_get_optional(&pdev->dev, "sw_switch_mdiv");
 	if (IS_ERR(priv->clk_mdiv)) {
 		ret = PTR_ERR(priv->clk_mdiv);
Signature · 1 line
2.56.1
#2 lore raw
In reply to Markus Elfring

Le 19/11/2026 à 17:37, Markus Elfring a écrit :

Quoted 23 lines

From: Markus Elfring <[email protected]>
Date: Thu, 19 Nov 2026 17:30:09 +0100

Reuse existing functionality from devm_clk_get_optional_enabled() instead of keeping duplicate source code.

The source code was transformed by using the Coccinelle software.

---
  drivers/net/dsa/bcm_sf2.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
drivers/net/dsa/bcm_sf2.c +1 -1
index 1747a1f20c78..011ce79392e8 100644
@@ -1467,14 +1467,10 @@ static int bcm_sf2_sw_probe(struct platform_device *pdev)
  		base++;
  	}
 
-	priv->clk = devm_clk_get_optional(&pdev->dev, "sw_switch");
+	priv->clk = devm_clk_get_optional_enabled(&pdev->dev, "sw_switch");

drivers/net/dsa/bcm_sf2.c static int bcm_sf2_sw_probe(struct platform_device *pdev) · line 1476

Quoted 10 lines
drivers/net/dsa/bcm_sf2.c +0 -4
  	if (IS_ERR(priv->clk))
  		return PTR_ERR(priv->clk);
 
-	ret = clk_prepare_enable(priv->clk);
-	if (ret)
-		return ret;
-
  	priv->clk_mdiv = devm_clk_get_optional(&pdev->dev, "sw_switch_mdiv");
  	if (IS_ERR(priv->clk_mdiv)) {
  		ret = PTR_ERR(priv->clk_mdiv);

Hi,

NACK, this patch is wrong because the remove function and the error handling path of the probe need to be updated.

CJ

maintainer
#3 lore raw
Quoted 36 lines · Christophe JAILLET

Le 19/11/2026 à 17:37, Markus Elfring a écrit :

Quoted 22 lines

From: Markus Elfring <[email protected]>
Date: Thu, 19 Nov 2026 17:30:09 +0100

Reuse existing functionality from devm_clk_get_optional_enabled() instead of keeping duplicate source code.

The source code was transformed by using the Coccinelle software.

---
  drivers/net/dsa/bcm_sf2.c | 6 +-----

  1 file changed, 1 insertion(+), 5 deletions(-)
drivers/net/dsa/bcm_sf2.c +0 -1
index 1747a1f20c78..011ce79392e8 100644
@@ -1467,14 +1467,10 @@ static int bcm_sf2_sw_probe(struct 
 platform_device *pdev)
           base++;
       }
-    priv->clk = devm_clk_get_optional(&pdev->dev, "sw_switch");

drivers/net/dsa/bcm_sf2.c static int bcm_sf2_sw_probe(struct · line 1475

Quoted 10 lines
drivers/net/dsa/bcm_sf2.c +1 -4
+    priv->clk = devm_clk_get_optional_enabled(&pdev->dev, "sw_switch");
       if (IS_ERR(priv->clk))
           return PTR_ERR(priv->clk);
-    ret = clk_prepare_enable(priv->clk);
-    if (ret)
-        return ret;
-
       priv->clk_mdiv = devm_clk_get_optional(&pdev->dev, "sw_switch_mdiv");
       if (IS_ERR(priv->clk_mdiv)) {
           ret = PTR_ERR(priv->clk_mdiv);

Hi,

Quoted 1 line

NACK, this patch is wrong because the remove function and the error handling path of the probe need to be updated.

My bad you are right this creates the following regressions: devm_clk_get_optional_enabled() registers a devres action that calls clk_disable_unprepare() on detach. The patch leaves the manual clk_disable_unprepare(priv->clk) in two places in drivers/net/dsa/bcm_sf2.c: the probe error path (out_clk:) and bcm_sf2_sw_remove().

pw-bot: cr

Signature · 1 line
Florian
#4 lore raw
Quoted 6 lines
Quoted 1 line

NACK, this patch is wrong because the remove function and the error handling path of the probe need to be updated.

My bad you are right this creates the following regressions: devm_clk_get_optional_enabled() registers a devres action that calls clk_disable_unprepare() on detach. The patch leaves the manual clk_disable_unprepare(priv->clk) in two places in drivers/net/dsa/bcm_sf2.c: the probe error path (out_clk:) and bcm_sf2_sw_remove().

pw-bot: cr

Do you expect more functionality from a devm_clk_get_optional_enabled() call than from the combination of devm_clk_get_optional() and clk_prepare_enable() so far?

https://elixir.bootlin.com/linux/v7.4-rc1/source/include/linux/clk.h#L680-L699
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/driver-api/driver-model/devres.rst?id=d220c9443db6e5f20a60cad55c6a0ae7a75c63b8#n242

https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/net/dsa/bcm_sf2.c#L1365-L1568

Regards,
Markus

#5 lore raw
In reply to Christophe JAILLET
#6 lore raw
In reply to Markus Elfring
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The patch double-disables the switch clock.
- [Low] The commit message calls the change pure de-duplication ("Reuse existing functionality ... instead of keeping duplicate source code"),…

--

net: dsa: bcm_sf2: Use devm_clk_get_optional_enabled() in bcm_sf2_sw_probe()

This commit converts the "sw_switch" clock lookup in bcm_sf2_sw_probe() from devm_clk_get_optional() followed by clk_prepare_enable() to devm_clk_get_optional_enabled(). Coccinelle generated the change.

Quoted 2 lines

Reuse existing functionality from devm_clk_get_optional_enabled() instead of keeping duplicate source code.

[Severity: Low]
This isn't a bug, but does this description match what the patch does? The conversion changes behaviour. It adds a devres-managed clk_disable_unprepare() for priv->clk and keeps the manual disables (see the comment on the diff below).

In the patch as submitted, bcm_sf2_sw_remove() still tears down in the original order:

drivers/net/dsa/bcm_sf2.c:bcm_sf2_sw_remove() {
    ...
	clk_disable_unprepare(priv->clk_mdiv);
	clk_disable_unprepare(priv->clk);
	if (priv->type == BCM7278_DEVICE_ID)
		reset_control_assert(priv->rcdev);
}

So the ordering is unchanged for now. Once the manual priv->clk disables are dropped to fix the double-disable, devres would gate the sw_switch clock after clk_mdiv is disabled. On BCM7278 it would also gate it after reset_control_assert().

Could a respin describe and justify that ordering change? Could it also explain why priv->clk_mdiv stays manually managed while priv->clk is converted?

Quoted 7 lines
drivers/net/dsa/bcm_sf2.c +0 -0
index 1747a1f20c78e..011ce79392e8a 100644
@@ -1467,14 +1467,10 @@ static int bcm_sf2_sw_probe(struct platform_device *pdev)
 		base++;
 	}

drivers/net/dsa/bcm_sf2.c static int bcm_sf2_sw_probe(struct platform_device *pdev) · line 1477

Quoted 10 lines
drivers/net/dsa/bcm_sf2.c +1 -5
 
-	priv->clk = devm_clk_get_optional(&pdev->dev, "sw_switch");
+	priv->clk = devm_clk_get_optional_enabled(&pdev->dev, "sw_switch");
 	if (IS_ERR(priv->clk))
 		return PTR_ERR(priv->clk);
 
-	ret = clk_prepare_enable(priv->clk);
-	if (ret)
-		return ret;
-

[Severity: High]
Is the sw_switch clock now disabled and unprepared twice?

devm_clk_get_optional_enabled() registers clk_disable_unprepare() as the devres exit action:

drivers/clk/clk-devres.c:devm_clk_get_optional_enabled() {
	return __devm_clk_get(dev, id, clk_get_optional,
			      clk_prepare_enable, clk_disable_unprepare);
}

When devres is released, devm_clk_release() calls state->exit(state->clk).

The patch removes only the explicit clk_prepare_enable(priv->clk). The explicit disable at the out_clk label is still there:

drivers/net/dsa/bcm_sf2.c:bcm_sf2_sw_probe() {
    ...
out_clk:
	clk_disable_unprepare(priv->clk);
	return ret;
}

So is the one in bcm_sf2_sw_remove():

	clk_disable_unprepare(priv->clk_mdiv);
	clk_disable_unprepare(priv->clk);

Every probe failure after the clock is acquired ends at out_clk. That includes -EPROBE_DEFER from devm_clk_get_optional("sw_switch_mdiv"), so it happens on every deferred probe retry. Unbind and module removal go through bcm_sf2_sw_remove().

In both cases devres_release_all() runs afterwards and calls clk_disable_unprepare() a second time.

When the sw_switch clock is present in DT, the second call would hit this check in clk_core_disable():

	if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
		return;

It would also hit the matching "already unprepared" WARN in clk_core_unprepare().

If the clk_core is shared with another consumer, would the extra call drop that consumer's enable/prepare reference instead and gate a clock it still uses?

Should clk_disable_unprepare(priv->clk) be removed from both the out_clk path and bcm_sf2_sw_remove(), with the error labels adjusted to match?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/d772ca26-d7f4-4857-9278-628b87e2252e%40web.de