[PATCH] counter: microchip-tcb-capture: fix clk reference leak

From: Haotian Zhang

Date: Fri Oct 09 2026 - 01:58:37 EST


mchp_tc_probe() obtains a reference to each channel clock with
of_clk_get_by_name(), but that reference is never released: the only
teardown path, mchp_tc_clk_remove(), just calls clk_disable_unprepare().
The clk pointers are kept in a local array, so the reference obtained on
every successful probe is leaked, and when clk_prepare_enable() fails the
reference is leaked on that error path as well.

Release the reference with clk_put() in mchp_tc_clk_remove() after the
clock has been disabled and unprepared, and also call clk_put() when
clk_prepare_enable() fails.

Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
Assisted-by: DeepSeek-V4.1-Flash
Signed-off-by: Haotian Zhang <vulab@xxxxxxxxxxx>
---
drivers/counter/microchip-tcb-capture.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
index e53a8390756b..f1c900efd2d9 100644
--- a/drivers/counter/microchip-tcb-capture.c
+++ b/drivers/counter/microchip-tcb-capture.c
@@ -470,7 +470,10 @@ static int mchp_tc_irq_enable(struct counter_device *const counter, int irq)

static void mchp_tc_clk_remove(void *ptr)
{
- clk_disable_unprepare((struct clk *)ptr);
+ struct clk *clk = ptr;
+
+ clk_disable_unprepare(clk);
+ clk_put(clk);
}

static int mchp_tc_probe(struct platform_device *pdev)
@@ -528,8 +531,10 @@ static int mchp_tc_probe(struct platform_device *pdev)
}

ret = clk_prepare_enable(clk[i]);
- if (ret)
+ if (ret) {
+ clk_put(clk[i]);
return ret;
+ }

ret = devm_add_action_or_reset(&pdev->dev,
mchp_tc_clk_remove,
--
2.25.1