Skip to contentSkip to conversation

usb: chipidea: Use devm_clk_get_optional_enabled() in two probe functions

PATCH Superseded Markus Elfring 2 participants 2 messages Nov 19

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: chipidea: Use devm_clk_get_optional_enabled() in two probe functions

From: Markus Elfring <[email protected]>
Date: Thu, 19 Nov 2026 19:34:37 +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/usb/chipidea/ci_hdrc_npcm.c | 6 +-----
 drivers/usb/chipidea/ci_hdrc_usb2.c | 8 +-------
 2 files changed, 2 insertions(+), 12 deletions(-)
drivers/usb/chipidea/ci_hdrc_npcm.c +1 -5
index 75a0223b578a..5052287f69c2 100644
@@ -47,14 +47,10 @@ static int npcm_udc_probe(struct platform_device *pdev)
 		return -ENOMEM;
 	platform_set_drvdata(pdev, ci);
 
-	ci->core_clk = devm_clk_get_optional(dev, NULL);
+	ci->core_clk = devm_clk_get_optional_enabled(dev, NULL);
 	if (IS_ERR(ci->core_clk))
 		return PTR_ERR(ci->core_clk);
 
-	ret = clk_prepare_enable(ci->core_clk);
-	if (ret)
-		return dev_err_probe(dev, ret, "failed to enable the clock: %d\n", ret);
-
 	ci->pdata.name = dev_name(dev);
 	ci->pdata.capoffset = DEF_CAPOFFSET;
 	ci->pdata.flags	= CI_HDRC_REQUIRES_ALIGNED_DMA |
drivers/usb/chipidea/ci_hdrc_usb2.c +1 -7
index b0200c5937cd..d33ab6873def 100644
@@ -70,16 +70,10 @@ static int ci_hdrc_usb2_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->clk = devm_clk_get_optional(dev, NULL);
+	priv->clk = devm_clk_get_optional_enabled(dev, NULL);
 	if (IS_ERR(priv->clk))
 		return PTR_ERR(priv->clk);
 
-	ret = clk_prepare_enable(priv->clk);
-	if (ret) {
-		dev_err(dev, "failed to enable the clock: %d\n", ret);
-		return ret;
-	}
-
 	ci_pdata->name = dev_name(dev);
 
 	priv->ci_pdev = ci_hdrc_add_device(dev, pdev->resource,
Signature · 1 line
2.56.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] usb: chipidea: npcm: double clk_disable_unprepare() on probe err and remove - [High] usb: chipidea…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] usb: chipidea: npcm: double clk_disable_unprepare() on probe err and remove
- [High] usb: chipidea: usb2: double clk_disable_unprepare() on probe err and remove
--

commit f08588246bb9a1d0ba3a56c799e77b7dacaf4937
Author: Markus Elfring <[email protected]>

usb: chipidea: Use devm_clk_get_optional_enabled() in two probe functions

This commit replaces devm_clk_get_optional() and clk_prepare_enable() with devm_clk_get_optional_enabled() in npcm_udc_probe() and ci_hdrc_usb2_probe().

Quoted 10 lines
drivers/usb/chipidea/ci_hdrc_npcm.c +1 -1
index 75a0223b578a8..5052287f69c2a 100644
@@ -47,14 +47,10 @@ static int npcm_udc_probe(struct platform_device *pdev)
 		return -ENOMEM;
 	platform_set_drvdata(pdev, ci);
 
-	ci->core_clk = devm_clk_get_optional(dev, NULL);
+	ci->core_clk = devm_clk_get_optional_enabled(dev, NULL);

drivers/usb/chipidea/ci_hdrc_npcm.c static int npcm_udc_probe(struct platform_device *pdev) · line 56

Quoted 10 lines
drivers/usb/chipidea/ci_hdrc_npcm.c +0 -4
 	if (IS_ERR(ci->core_clk))
 		return PTR_ERR(ci->core_clk);
 
-	ret = clk_prepare_enable(ci->core_clk);
-	if (ret)
-		return dev_err_probe(dev, ret, "failed to enable the clock: %d\n", ret);
-
 	ci->pdata.name = dev_name(dev);
 	ci->pdata.capoffset = DEF_CAPOFFSET;
 	ci->pdata.flags	= CI_HDRC_REQUIRES_ALIGNED_DMA |

[Severity: High]
Does this cause clk_disable_unprepare(ci->core_clk) to run twice when ci_hdrc_add_device() fails or when the device is removed?

devm_clk_get_optional_enabled() registers a devres action to disable and unprepare ci->core_clk automatically, but both the clk_err error path in npcm_udc_probe() and npcm_udc_remove() still call clk_disable_unprepare(ci->core_clk) manually, underflowing the clock's enable and prepare counts.

Quoted 12 lines
drivers/usb/chipidea/ci_hdrc_usb2.c +1 -1
index b0200c5937cd5..d33ab6873defb 100644
@@ -70,16 +70,10 @@ static int ci_hdrc_usb2_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->clk = devm_clk_get_optional(dev, NULL);
+	priv->clk = devm_clk_get_optional_enabled(dev, NULL);
 	if (IS_ERR(priv->clk))
 		return PTR_ERR(priv->clk);

drivers/usb/chipidea/ci_hdrc_usb2.c static int ci_hdrc_usb2_probe(struct platform_device *pdev) · line 79

Quoted 10 lines
drivers/usb/chipidea/ci_hdrc_usb2.c +0 -6
 
-	ret = clk_prepare_enable(priv->clk);
-	if (ret) {
-		dev_err(dev, "failed to enable the clock: %d\n", ret);
-		return ret;
-	}
-
 	ci_pdata->name = dev_name(dev);
 
 	priv->ci_pdev = ci_hdrc_add_device(dev, pdev->resource,

[Severity: High]
Similarly, won't priv->clk be disabled and unprepared twice if ci_hdrc_add_device() fails (or returns -EPROBE_DEFER) or when ci_hdrc_usb2_remove() runs?

The clk_err path in ci_hdrc_usb2_probe() and ci_hdrc_usb2_remove() still call clk_disable_unprepare(priv->clk) before devres cleanup calls it a second time.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1