mirror of https://lore.kernel.org/linux-amlogic/
 help / color / mirror / Atom feed
From: Jiawen Liu <1298662399@qq.com>
To: Sunny Luo <sunny.luo@amlogic.com>,
	Xianwei Zhao <xianwei.zhao@amlogic.com>,
	Mark Brown <broonie@kernel.org>
Cc: linux-amlogic@lists.infradead.org, linux-spi@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] spi: amlogic-spisg: fix managed clock ownership
Date: Sun, 29 Nov 2026 16:40:13 +0400	[thread overview]
Message-ID: <tencent_A1AF5465507D4851046481AE8A930A20EF06@qq.com> (raw)

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@qq.com>
---
 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


_______________________________________________
linux-amlogic mailing list
linux-amlogic@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-amlogic

             reply	other threads:[~2026-11-29 14:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 12:40 Jiawen Liu [this message]
2026-11-30 13:43 ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_A1AF5465507D4851046481AE8A930A20EF06@qq.com \
    --to=1298662399@qq.com \
    --cc=broonie@kernel.org \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=sunny.luo@amlogic.com \
    --cc=xianwei.zhao@amlogic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®