[PATCH] spi: amlogic-spisg: fix managed clock ownership
From: Jiawen Liu
Date: Sun Nov 29 2026 - 09:12:51 EST
devm_clk_get_enabled() registers automatic cleanup for core and pclk,
but the driver also disables both clocks manually during initialization,
runtime PM, and removal. Devres later releases the original managed
enable again.
Obtain both clock handles with devm_clk_get(). Enable core at the start
of clock initialization and disable it on all later setup errors. Enable
pclk explicitly only while configuring the divider, and balance that
temporary enable. The divider-derived sclk now owns the parent enable,
so disable sclk before core in probe error and active-remove cleanup.
Fixes: cef9991e04ae ("spi: Add Amlogic SPISG driver")
Signed-off-by: Jiawen Liu <1298662399@xxxxxx>
---
drivers/spi/spi-amlogic-spisg.c | 46 ++++++++++++++++++++++++---------
1 file changed, 34 insertions(+), 12 deletions(-)
diff --git a/drivers/spi/spi-amlogic-spisg.c b/drivers/spi/spi-amlogic-spisg.c
index c59df9b115eed..0b44875628858 100644
--- a/drivers/spi/spi-amlogic-spisg.c
+++ b/drivers/spi/spi-amlogic-spisg.c
@@ -692,16 +692,29 @@ static int aml_spisg_clk_init(struct spisg_device *spisg, void __iomem *base)
char name[32];
int ret, i;
- spisg->core = devm_clk_get_enabled(dev, "core");
+ spisg->core = devm_clk_get(dev, "core");
if (IS_ERR(spisg->core)) {
dev_err(dev, "core clock request failed\n");
return PTR_ERR(spisg->core);
}
- spisg->pclk = devm_clk_get_enabled(dev, "pclk");
+ ret = clk_prepare_enable(spisg->core);
+ if (ret) {
+ dev_err(dev, "core clock enable failed\n");
+ return ret;
+ }
+
+ spisg->pclk = devm_clk_get(dev, "pclk");
if (IS_ERR(spisg->pclk)) {
dev_err(dev, "pclk clock request failed\n");
- return PTR_ERR(spisg->pclk);
+ ret = PTR_ERR(spisg->pclk);
+ goto err_disable_core;
+ }
+
+ ret = clk_prepare_enable(spisg->pclk);
+ if (ret) {
+ dev_err(dev, "pclk clock enable failed\n");
+ goto err_disable_core;
}
clk_set_min_rate(spisg->pclk, SPISG_PCLK_RATE_MIN);
@@ -709,8 +722,10 @@ static int aml_spisg_clk_init(struct spisg_device *spisg, void __iomem *base)
clk_disable_unprepare(spisg->pclk);
tbl = devm_kzalloc_objs(dev, *tbl, (DIV_NUM + 1));
- if (!tbl)
- return -ENOMEM;
+ if (!tbl) {
+ ret = -ENOMEM;
+ goto err_disable_core;
+ }
for (i = 0; i < DIV_NUM; i++) {
tbl[i].val = i + SPISG_CLK_DIV_MIN - 1;
@@ -719,8 +734,10 @@ static int aml_spisg_clk_init(struct spisg_device *spisg, void __iomem *base)
spisg->tbl = tbl;
div = devm_kzalloc_obj(dev, *div);
- if (!div)
- return -ENOMEM;
+ if (!div) {
+ ret = -ENOMEM;
+ goto err_disable_core;
+ }
div->flags = CLK_DIVIDER_ROUND_CLOSEST;
div->reg = base + SPISG_REG_CFG_BUS;
@@ -745,20 +762,25 @@ static int aml_spisg_clk_init(struct spisg_device *spisg, void __iomem *base)
ret = devm_clk_hw_register(dev, &div->hw);
if (ret) {
dev_err(dev, "clock registration failed\n");
- return ret;
+ goto err_disable_core;
}
spisg->sclk = devm_clk_hw_get_clk(dev, &div->hw, NULL);
if (IS_ERR(spisg->sclk)) {
dev_err(dev, "get clock failed\n");
- return PTR_ERR(spisg->sclk);
+ ret = PTR_ERR(spisg->sclk);
+ goto err_disable_core;
}
ret = clk_prepare_enable(spisg->sclk);
if (ret)
- return ret;
+ goto err_disable_core;
return 0;
+
+err_disable_core:
+ clk_disable_unprepare(spisg->core);
+ return ret;
}
static int aml_spisg_probe(struct platform_device *pdev)
@@ -869,9 +891,9 @@ static int aml_spisg_probe(struct platform_device *pdev)
return 0;
out_clk:
+ clk_disable_unprepare(spisg->sclk);
if (spisg->core)
clk_disable_unprepare(spisg->core);
- clk_disable_unprepare(spisg->pclk);
return ret;
}
@@ -884,8 +906,8 @@ static void aml_spisg_remove(struct platform_device *pdev)
if (!pm_runtime_suspended(&pdev->dev)) {
pinctrl_pm_select_sleep_state(&spisg->pdev->dev);
+ clk_disable_unprepare(spisg->sclk);
clk_disable_unprepare(spisg->core);
- clk_disable_unprepare(spisg->pclk);
}
}
--
2.34.1