* [PATCH v2 1/5] pwm: mxs: Return early when disabling and fix error-path clock leak
2026-11-23 7:52 [PATCH v2 0/5] pwm: Fix various bugs reported by Sashiko Lee Jones
@ 2026-11-23 7:52 ` Lee Jones
2026-11-23 7:52 ` [PATCH v2 2/5] pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume Lee Jones
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Lee Jones @ 2026-11-23 7:52 UTC (permalink / raw)
To: lee, Uwe Kleine-König, Frank Li, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Geert Uytterhoeven,
Chen Wang, Uwe Kleine-König (The Capable Hub),
Shawn Guo, Rasmus Villemoes, Thierry Reding, linux-pwm, imx,
linux-arm-kernel, linux-kernel
Cc: stable
When disabling a PWM channel (!state->enabled), the PWM core permits
state->period to be zero. mxs_pwm_apply() currently falls through into
the period and duty-cycle calculations even when !state->enabled,
causing do_div(c, state->period) to divide by zero when state->period is
zero. In addition, if the requested period exceeds the maximum divider
(div >= PERIOD_CDIV_MAX) while the PWM channel is currently disabled,
mxs_pwm_apply() returns -EINVAL without disabling and unpreparing
mxs->clk.
Handle the !state->enabled path upfront and return early, balance
clk_prepare_enable() on the -EINVAL error path, and check
pwm->state.enabled directly rather than calling the consumer helper
pwm_is_enabled().
Fixes: bf29c2ff82fd ("pwm: mxs: Implement ->apply()")
Closes: https://sashiko.dev/#/bug/linux-710f863f-6964-4e4f-990a-e5043dbc1a87
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
v1 => v2:
- Check pwm->state.enabled directly instead of calling the consumer
helper pwm_is_enabled() (Uwe)
- Drop Frank Li's Reviewed-by due to code changes
drivers/pwm/pwm-mxs.c | 34 +++++++++++++++++++---------------
1 file changed, 19 insertions(+), 15 deletions(-)
diff --git a/drivers/pwm/pwm-mxs.c b/drivers/pwm/pwm-mxs.c
index e1a3df6549fc..768401b23dbb 100644
--- a/drivers/pwm/pwm-mxs.c
+++ b/drivers/pwm/pwm-mxs.c
@@ -56,20 +56,25 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
unsigned long long c;
unsigned int pol_bits;
+ if (!state->enabled) {
+ if (pwm->state.enabled) {
+ writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
+ clk_disable_unprepare(mxs->clk);
+ }
+ return 0;
+ }
+
/*
* If the PWM channel is disabled, make sure to turn on the
* clock before calling clk_get_rate() and writing to the
* registers. Otherwise, just keep it enabled.
*/
- if (!pwm_is_enabled(pwm)) {
+ if (!pwm->state.enabled) {
ret = clk_prepare_enable(mxs->clk);
if (ret)
return ret;
}
- if (!state->enabled && pwm_is_enabled(pwm))
- writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
-
rate = clk_get_rate(mxs->clk);
while (1) {
c = rate >> cdiv_shift[div];
@@ -78,8 +83,11 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
if (c < PERIOD_PERIOD_MAX)
break;
div++;
- if (div >= PERIOD_CDIV_MAX)
+ if (div >= PERIOD_CDIV_MAX) {
+ if (!pwm->state.enabled)
+ clk_disable_unprepare(mxs->clk);
return -EINVAL;
+ }
}
period_cycles = c;
@@ -101,16 +109,12 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
writel(PERIOD_PERIOD(period_cycles) | pol_bits | PERIOD_CDIV(div),
mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
- if (state->enabled) {
- if (!pwm_is_enabled(pwm)) {
- /*
- * The clock was enabled above. Just enable
- * the channel in the control register.
- */
- writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
- }
- } else {
- clk_disable_unprepare(mxs->clk);
+ if (!pwm->state.enabled) {
+ /*
+ * The clock was enabled above. Just enable
+ * the channel in the control register.
+ */
+ writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
}
return 0;
}
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 2/5] pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume
2026-11-23 7:52 [PATCH v2 0/5] pwm: Fix various bugs reported by Sashiko Lee Jones
2026-11-23 7:52 ` [PATCH v2 1/5] pwm: mxs: Return early when disabling and fix error-path clock leak Lee Jones
@ 2026-11-23 7:52 ` Lee Jones
2026-11-23 7:52 ` [PATCH v2 3/5] pwm: omap-dmtimer: Propagate get_pwm_status() errors in apply() Lee Jones
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Lee Jones @ 2026-11-23 7:52 UTC (permalink / raw)
To: lee, Uwe Kleine-König, Xiubo Li, shenwei.wang,
Thierry Reding, linux-pwm, linux-kernel
Cc: stable
On SoCs with a dedicated IP interface clock (ipg_clk), register accesses
require ipg_clk to be enabled in addition to the ftm_sys clock managed
by regmap_mmio. In fsl_pwm_resume(), ipg_clk is only enabled inside the
channel loop for channels with PWMF_REQUESTED set, so if no channels are
currently requested, regcache_sync() writes to the hardware registers
while ipg_clk is disabled.
Enable ipg_clk around regcache_sync() in fsl_pwm_resume(), matching
fsl_pwm_init().
Fixes: 82a9c55a2bb0 ("pwm: fsl-ftm: Added a dedicated IP interface clock")
Closes: https://sashiko.dev/#/bug/linux-858851cf-55a9-4776-883c-69c05f3961f2
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
v1 => v2:
- Move clk_prepare_enable(fpc->ipg_clk) after the channel loop and add
an explanatory comment (Uwe)
drivers/pwm/pwm-fsl-ftm.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/pwm/pwm-fsl-ftm.c b/drivers/pwm/pwm-fsl-ftm.c
index 35406b2e1925..a73cea34dd82 100644
--- a/drivers/pwm/pwm-fsl-ftm.c
+++ b/drivers/pwm/pwm-fsl-ftm.c
@@ -501,6 +501,7 @@ static int fsl_pwm_resume(struct device *dev)
{
struct pwm_chip *chip = dev_get_drvdata(dev);
struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
+ int ret;
int i;
for (i = 0; i < chip->npwm; i++) {
@@ -518,10 +519,21 @@ static int fsl_pwm_resume(struct device *dev)
clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
}
+ /*
+ * regmap_mmio only manages ftm_sys, whereas register accesses on SoCs
+ * with a dedicated IP interface clock also require ipg_clk. Enable
+ * ipg_clk around regcache_sync() in case no channels are requested.
+ */
+ ret = clk_prepare_enable(fpc->ipg_clk);
+ if (ret)
+ return ret;
+
/* restore all registers from cache */
regcache_cache_only(fpc->regmap, false);
regcache_sync(fpc->regmap);
+ clk_disable_unprepare(fpc->ipg_clk);
+
return 0;
}
#endif
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 3/5] pwm: omap-dmtimer: Propagate get_pwm_status() errors in apply()
2026-11-23 7:52 [PATCH v2 0/5] pwm: Fix various bugs reported by Sashiko Lee Jones
2026-11-23 7:52 ` [PATCH v2 1/5] pwm: mxs: Return early when disabling and fix error-path clock leak Lee Jones
2026-11-23 7:52 ` [PATCH v2 2/5] pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume Lee Jones
@ 2026-11-23 7:52 ` Lee Jones
2026-11-23 7:52 ` [PATCH v2 4/5] pwm: tiehrpwm: Enable runtime PM before registering PWM chip Lee Jones
2026-11-23 7:52 ` [PATCH v2 5/5] pwm: img: Unregister PWM chip before disabling runtime PM and clocks Lee Jones
4 siblings, 0 replies; 6+ messages in thread
From: Lee Jones @ 2026-11-23 7:52 UTC (permalink / raw)
To: lee, Uwe Kleine-König, Geert Uytterhoeven,
Uwe Kleine-König (The Capable Hub),
Chen Wang, Neil Armstrong, Joachim Eastwood, NeilBrown,
Grant Erickson, Lokesh Vutla, Thierry Reding, linux-pwm,
linux-kernel
Cc: stable
omap->pdata->get_pwm_status() returns a signed int that can be a
negative error code on runtime PM failure or invalid timer cookie.
pwm_omap_dmtimer_is_enabled() and pwm_omap_dmtimer_polarity() store the
return value in an unsigned u32 and test OMAP_TIMER_CTRL_ST (BIT(0)) and
OMAP_TIMER_CTRL_SCPWM (BIT(7)), misinterpreting negative error codes
(such as -EINVAL or -EACCES) as an enabled or inverted timer.
Store the return value in a signed int and propagate negative error
codes through to pwm_omap_dmtimer_apply().
Fixes: 6b28fb6f3ca3 ("pwm: omap-dmtimer: Implement .apply callback")
Closes: https://sashiko.dev/#/bug/linux-f4782faa-466d-43a8-85d8-6d6181ee2243
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
v1 => v2:
- Propagate negative get_pwm_status() error codes through to
pwm_omap_dmtimer_apply() (Uwe)
drivers/pwm/pwm-omap-dmtimer.c | 54 +++++++++++++++++++++++++---------
1 file changed, 40 insertions(+), 14 deletions(-)
diff --git a/drivers/pwm/pwm-omap-dmtimer.c b/drivers/pwm/pwm-omap-dmtimer.c
index 51830a87275c..08d444b3928e 100644
--- a/drivers/pwm/pwm-omap-dmtimer.c
+++ b/drivers/pwm/pwm-omap-dmtimer.c
@@ -107,13 +107,15 @@ static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap)
* pwm_omap_dmtimer_is_enabled() - Detect if the pwm is enabled.
* @omap: Pointer to pwm omap dm timer chip
*
- * Return true if pwm is enabled else false.
+ * Return: 1 if pwm is enabled, 0 if disabled, or negative error code.
*/
-static bool pwm_omap_dmtimer_is_enabled(struct pwm_omap_dmtimer_chip *omap)
+static int pwm_omap_dmtimer_is_enabled(struct pwm_omap_dmtimer_chip *omap)
{
- u32 status;
+ int status;
status = omap->pdata->get_pwm_status(omap->dm_timer);
+ if (status < 0)
+ return status;
return !!(status & OMAP_TIMER_CTRL_ST);
}
@@ -122,13 +124,15 @@ static bool pwm_omap_dmtimer_is_enabled(struct pwm_omap_dmtimer_chip *omap)
* pwm_omap_dmtimer_polarity() - Detect the polarity of pwm.
* @omap: Pointer to pwm omap dm timer chip
*
- * Return the polarity of pwm.
+ * Return: Polarity of pwm, or negative error code.
*/
static int pwm_omap_dmtimer_polarity(struct pwm_omap_dmtimer_chip *omap)
{
- u32 status;
+ int status;
status = omap->pdata->get_pwm_status(omap->dm_timer);
+ if (status < 0)
+ return status;
return !!(status & OMAP_TIMER_CTRL_SCPWM);
}
@@ -237,16 +241,21 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
* @chip: Pointer to PWM controller
* @pwm: Pointer to PWM channel
* @polarity: New pwm polarity to be set
+ *
+ * Return: 0 if successfully changed the polarity else appropriate error.
*/
-static void pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,
- struct pwm_device *pwm,
- enum pwm_polarity polarity)
+static int pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ enum pwm_polarity polarity)
{
struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
- bool enabled;
+ int enabled;
/* Disable the PWM before changing the polarity. */
enabled = pwm_omap_dmtimer_is_enabled(omap);
+ if (enabled < 0)
+ return enabled;
+
if (enabled)
omap->pdata->stop(omap->dm_timer);
@@ -257,6 +266,8 @@ static void pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,
if (enabled)
pwm_omap_dmtimer_start(omap);
+
+ return 0;
}
/**
@@ -272,22 +283,37 @@ static int pwm_omap_dmtimer_apply(struct pwm_chip *chip,
const struct pwm_state *state)
{
struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
- int ret;
+ int enabled, polarity, ret;
- if (pwm_omap_dmtimer_is_enabled(omap) && !state->enabled) {
+ enabled = pwm_omap_dmtimer_is_enabled(omap);
+ if (enabled < 0)
+ return enabled;
+
+ if (enabled && !state->enabled) {
omap->pdata->stop(omap->dm_timer);
return 0;
}
- if (pwm_omap_dmtimer_polarity(omap) != state->polarity)
- pwm_omap_dmtimer_set_polarity(chip, pwm, state->polarity);
+ polarity = pwm_omap_dmtimer_polarity(omap);
+ if (polarity < 0)
+ return polarity;
+
+ if (polarity != state->polarity) {
+ ret = pwm_omap_dmtimer_set_polarity(chip, pwm, state->polarity);
+ if (ret)
+ return ret;
+ }
ret = pwm_omap_dmtimer_config(chip, pwm, state->duty_cycle,
state->period);
if (ret)
return ret;
- if (!pwm_omap_dmtimer_is_enabled(omap) && state->enabled) {
+ enabled = pwm_omap_dmtimer_is_enabled(omap);
+ if (enabled < 0)
+ return enabled;
+
+ if (!enabled && state->enabled) {
omap->pdata->set_pwm(omap->dm_timer,
state->polarity == PWM_POLARITY_INVERSED,
true,
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 4/5] pwm: tiehrpwm: Enable runtime PM before registering PWM chip
2026-11-23 7:52 [PATCH v2 0/5] pwm: Fix various bugs reported by Sashiko Lee Jones
` (2 preceding siblings ...)
2026-11-23 7:52 ` [PATCH v2 3/5] pwm: omap-dmtimer: Propagate get_pwm_status() errors in apply() Lee Jones
@ 2026-11-23 7:52 ` Lee Jones
2026-11-23 7:52 ` [PATCH v2 5/5] pwm: img: Unregister PWM chip before disabling runtime PM and clocks Lee Jones
4 siblings, 0 replies; 6+ messages in thread
From: Lee Jones @ 2026-11-23 7:52 UTC (permalink / raw)
To: lee, Uwe Kleine-König, Gokul Praveen, Philip, Avinash,
Thierry Reding, Vaibhav Bedia, linux-pwm, linux-kernel
Cc: stable
ehrpwm_pwm_probe() calls pwmchip_add() before setting driver data and
enabling runtime PM. Because pwmchip_add() immediately exposes the PWM
channels to consumers while runtime PM is still disabled on the
initially RPM_SUSPENDED device, a consumer invoking ehrpwm_pwm_apply()
before pm_runtime_enable() runs causes guard(pm_runtime_active) to fail
with -EACCES without resuming the device and accesses unclocked MMIO
registers.
Set driver data and enable runtime PM before calling pwmchip_add(),
disable runtime PM on the error path, and disable runtime PM before
unpreparing tbclk in ehrpwm_pwm_remove() to match.
Fixes: 19891b20e7c2 ("pwm: pwm-tiehrpwm: PWM driver support for EHRPWM")
Closes: https://sashiko.dev/#/bug/linux-28b36a80-e4a9-4bb6-97bc-7ca02895570a
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
v1 => v2:
- Clarify runtime PM state prior to pm_runtime_enable() in the commit
description (Uwe)
- Disable runtime PM before unpreparing tbclk in ehrpwm_pwm_remove() to
match probe ordering (Uwe)
drivers/pwm/pwm-tiehrpwm.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/drivers/pwm/pwm-tiehrpwm.c b/drivers/pwm/pwm-tiehrpwm.c
index e3407d0a1f7f..73fe2f91243d 100644
--- a/drivers/pwm/pwm-tiehrpwm.c
+++ b/drivers/pwm/pwm-tiehrpwm.c
@@ -461,18 +461,19 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev)
return ret;
}
- ret = pwmchip_add(chip);
- if (ret < 0) {
- dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
- goto err_clk_unprepare;
- }
-
platform_set_drvdata(pdev, chip);
pm_runtime_enable(&pdev->dev);
+ ret = pwmchip_add(chip);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
+ goto err_pm_disable;
+ }
+
return 0;
-err_clk_unprepare:
+err_pm_disable:
+ pm_runtime_disable(&pdev->dev);
clk_unprepare(pc->tbclk);
return ret;
@@ -485,9 +486,9 @@ static void ehrpwm_pwm_remove(struct platform_device *pdev)
pwmchip_remove(chip);
- clk_unprepare(pc->tbclk);
-
pm_runtime_disable(&pdev->dev);
+
+ clk_unprepare(pc->tbclk);
}
static void ehrpwm_pwm_save_context(struct pwm_chip *chip)
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 5/5] pwm: img: Unregister PWM chip before disabling runtime PM and clocks
2026-11-23 7:52 [PATCH v2 0/5] pwm: Fix various bugs reported by Sashiko Lee Jones
` (3 preceding siblings ...)
2026-11-23 7:52 ` [PATCH v2 4/5] pwm: tiehrpwm: Enable runtime PM before registering PWM chip Lee Jones
@ 2026-11-23 7:52 ` Lee Jones
4 siblings, 0 replies; 6+ messages in thread
From: Lee Jones @ 2026-11-23 7:52 UTC (permalink / raw)
To: lee, Uwe Kleine-König, Sai Masarapu, Vladimir Zapolskiy,
Naidu Tellapati, Thierry Reding, Andrew Bresticker, linux-pwm,
linux-kernel
Cc: stable
img_pwm_remove() disables runtime PM and gates the peripheral clocks via
img_pwm_runtime_suspend() before calling pwmchip_remove(). While the
PWM chip is still registered, a consumer can call img_pwm_apply(), where
pm_runtime_resume_and_get() fails with -EACCES or img_pwm_disable()
writes to unclocked registers after img_pwm_runtime_suspend() has gated
the clocks.
Call pwmchip_remove() before disabling runtime PM, clearing autosuspend,
and suspending the device. Because chip and its private data are
allocated with devm_pwmchip_alloc(), driver data remains valid for any
pending runtime PM callbacks invoked during pm_runtime_disable() and
img_pwm_runtime_suspend() until devres teardown runs after
img_pwm_remove() returns.
Fixes: 277bb6a29e00 ("pwm: Imagination Technologies PWM DAC driver")
Closes: https://sashiko.dev/#/bug/linux-b06bab8d-dbf2-4ba2-9f5d-c5e141076abd
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
v1 => v2:
- Clear autosuspend in img_pwm_remove() and document why driver data
remains valid for runtime PM callbacks after pwmchip_remove() (Uwe)
drivers/pwm/pwm-img.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/drivers/pwm/pwm-img.c b/drivers/pwm/pwm-img.c
index 91e0b19f0c08..7bf086b127c2 100644
--- a/drivers/pwm/pwm-img.c
+++ b/drivers/pwm/pwm-img.c
@@ -338,11 +338,12 @@ static void img_pwm_remove(struct platform_device *pdev)
{
struct pwm_chip *chip = platform_get_drvdata(pdev);
- pm_runtime_disable(&pdev->dev);
- if (!pm_runtime_status_suspended(&pdev->dev))
- img_pwm_runtime_suspend(&pdev->dev);
-
pwmchip_remove(chip);
+
+ pm_runtime_disable(&pdev->dev);
+ pm_runtime_dont_use_autosuspend(&pdev->dev);
+ if (!pm_runtime_status_suspended(&pdev->dev))
+ img_pwm_runtime_suspend(&pdev->dev);
}
#ifdef CONFIG_PM_SLEEP
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 6+ messages in thread