[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250822045005.4127-1-rafael.v.volkmer@gmail.com>
Date: Fri, 22 Aug 2025 01:50:05 -0300
From: "Rafael V. Volkmer" <rafael.v.volkmer@...il.com>
To: rafael.v.volkmer@...il.com
Cc: linux-kernel@...r.kernel.org,
linux-pwm@...r.kernel.org,
ukleinek@...nel.org
Subject: [PATCH v5 5/6] pwm: tiehrpwm: account for active channels at probe
Handle already-running eHRPWM channels at probe. Detect active A/B from
AQCTL(A/B) when not software-forced in AQCSFRC, take one
pm_runtime_get_sync() per active channel, and enable tbclk only if at
least one is active. Keep PM refcounts and clocks consistent with the
hardware state.
Before: active hardware could miss runtime PM gets and clk enable.
After: PM/clk state matches active channels observed at probe.
Signed-off-by: Rafael V. Volkmer <rafael.v.volkmer@...il.com>
---
drivers/pwm/pwm-tiehrpwm.c | 55 +++++++++++++++++++++++++++++++++-----
1 file changed, 49 insertions(+), 6 deletions(-)
diff --git a/drivers/pwm/pwm-tiehrpwm.c b/drivers/pwm/pwm-tiehrpwm.c
index 1dae3b8b5..a801912be 100644
--- a/drivers/pwm/pwm-tiehrpwm.c
+++ b/drivers/pwm/pwm-tiehrpwm.c
@@ -121,6 +121,9 @@
#define NUM_PWM_CHANNEL 2 /* EHRPWM channels */
+#define PWM_CHA 0
+#define PWM_CHB 1
+
struct ehrpwm_context {
u16 tbctl;
u16 tbprd;
@@ -582,13 +585,31 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev)
struct ehrpwm_pwm_chip *pc;
struct pwm_chip *chip;
struct clk *clk;
- int ret;
+ int ret, ch_idx, ch_disable;
+
+ u16 aqcsfrc_reg, aqctla_reg, aqctlb_reg;
+
+ bool enabled_ch[NUM_PWM_CHANNEL] = { false, false };
chip = devm_pwmchip_alloc(&pdev->dev, NUM_PWM_CHANNEL, sizeof(*pc));
if (IS_ERR(chip))
return PTR_ERR(chip);
pc = to_ehrpwm_pwm_chip(chip);
+ pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(pc->mmio_base))
+ return PTR_ERR(pc->mmio_base);
+
+ aqcsfrc_reg = readw(pc->mmio_base + AQCSFRC);
+ aqctla_reg = readw(pc->mmio_base + AQCTLA);
+ aqctlb_reg = readw(pc->mmio_base + AQCTLB);
+
+ if (aqctla_reg != 0 && !FIELD_GET(AQCSFRC_CSFA_MASK, aqcsfrc_reg))
+ enabled_ch[PWM_CHA] = true;
+
+ if (aqctlb_reg != 0 && !FIELD_GET(AQCSFRC_CSFB_MASK, aqcsfrc_reg))
+ enabled_ch[PWM_CHB] = true;
+
clk = devm_clk_get(&pdev->dev, "fck");
if (IS_ERR(clk)) {
if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
@@ -608,10 +629,6 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev)
chip->ops = &ehrpwm_pwm_ops;
- pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
- if (IS_ERR(pc->mmio_base))
- return PTR_ERR(pc->mmio_base);
-
/* Acquire tbclk for Time Base EHRPWM submodule */
pc->tbclk = devm_clk_get(&pdev->dev, "tbclk");
if (IS_ERR(pc->tbclk))
@@ -623,17 +640,43 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev)
return ret;
}
+ if (enabled_ch[PWM_CHA] || enabled_ch[PWM_CHB]) {
+ ret = clk_enable(pc->tbclk);
+ if (ret) {
+ dev_err_probe(&pdev->dev, ret, "clk_enable(tbclk) failed\n");
+ goto err_clk_unprepare;
+ }
+ }
+
ret = pwmchip_add(chip);
if (ret < 0) {
dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
- goto err_clk_unprepare;
+ goto err_disable_tbclk;
}
platform_set_drvdata(pdev, chip);
pm_runtime_enable(&pdev->dev);
+ for (ch_idx = 0; ch_idx < NUM_PWM_CHANNEL; ch_idx++) {
+ if (enabled_ch[ch_idx]) {
+ ret = pm_runtime_get_sync(&pdev->dev);
+ if (ret < 0) {
+ for (ch_disable = 0; ch_disable <= ch_idx; ch_disable++) {
+ if (enabled_ch[ch_disable])
+ pm_runtime_put_noidle(&pdev->dev);
+ }
+
+ pwmchip_remove(chip);
+ pm_runtime_disable(&pdev->dev);
+ return ret;
+ }
+ }
+ }
return 0;
+err_disable_tbclk:
+ if (enabled_ch[PWM_CHA] || enabled_ch[PWM_CHB])
+ clk_disable(pc->tbclk);
err_clk_unprepare:
clk_unprepare(pc->tbclk);
--
2.43.0
Powered by blists - more mailing lists