Linux kernel and device drivers for NXP i.MX platforms
 help / color / mirror / Atom feed
* [PATCH v2 0/5] pwm: Fix various bugs reported by Sashiko
@ 2026-11-23  7:52 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
  0 siblings, 1 reply; 2+ messages in thread
From: Lee Jones @ 2026-11-23  7:52 UTC (permalink / raw)
  To: lee, Uwe Kleine-König, linux-pwm
  Cc: linux-kernel, Andrew Bresticker, Ezequiel Garcia, Fabio Estevam,
	Frank Li, Grant Erickson, imx, Joachim Eastwood, linux-arm-kernel,
	Lokesh Vutla, Naidu Tellapati, Neil Armstrong, NeilBrown,
	Pengutronix Kernel Team, Philip, Avinash, Rasmus Villemoes,
	Sai Masarapu, Sascha Hauer, Shawn Guo, shenwei.wang,
	Thierry Reding, Vaibhav Bedia, Xiubo Li

These issues were reported by Sashiko.

None of these changes have been tested on physical hardware.

v1 => v2:
 - Drop tegra (superseded by 9ad5602d4943) and loongson / axi-pwmgen
   (already applied)
 - mxs: Check pwm->state.enabled directly instead of calling the
   consumer helper pwm_is_enabled() (Uwe)
 - fsl-ftm: Move ipg_clk enablement after the channel loop and document
   why it is needed around regcache_sync() (Uwe)
 - omap-dmtimer: Propagate negative get_pwm_status() error codes out of
   pwm_omap_dmtimer_apply() (Uwe)
 - tiehrpwm: Clarify runtime PM state before pm_runtime_enable() in the
   commit description and match teardown ordering in ehrpwm_pwm_remove()
   (Uwe)
 - img: Clear autosuspend in img_pwm_remove() and document why driver
   data remains valid for runtime PM callbacks after pwmchip_remove()
   (Uwe)

v1: https://lore.kernel.org/all/20261105143847.2559586-1-lee@kernel.org/

Cc: Andrew Bresticker <abrestic@chromium.org>
Cc: Ezequiel Garcia <ezequiel.garcia@imgtec.com>
Cc: Fabio Estevam <festevam@gmail.com>
Cc: Frank Li <Frank.Li@nxp.com>
Cc: Grant Erickson <marathon96@gmail.com>
Cc: imx@lists.linux.dev
Cc: Joachim Eastwood <manabian@gmail.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-pwm@vger.kernel.org
Cc: Lokesh Vutla <lokeshvutla@ti.com>
Cc: Naidu Tellapati <naidu.tellapati@gmail.com>
Cc: Neil Armstrong <neil.armstrong@linaro.org>
Cc: NeilBrown <neil@brown.name>
Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
Cc: "Philip, Avinash" <avinashphilip@ti.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Sai Masarapu <Sai.Masarapu@imgtec.com>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: shenwei.wang@nxp.com
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Uwe Kleine-König <ukleinek@kernel.org>
Cc: Vaibhav Bedia <vaibhav.bedia@ti.com>
Cc: Xiubo Li <Li.Xiubo@freescale.com>

Lee Jones (5):
  pwm: mxs: Return early when disabling and fix error-path clock leak
  pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume
  pwm: omap-dmtimer: Propagate get_pwm_status() errors in apply()
  pwm: tiehrpwm: Enable runtime PM before registering PWM chip
  pwm: img: Unregister PWM chip before disabling runtime PM and clocks

 drivers/pwm/pwm-fsl-ftm.c      | 12 ++++++++
 drivers/pwm/pwm-img.c          |  9 +++---
 drivers/pwm/pwm-mxs.c          | 34 +++++++++++----------
 drivers/pwm/pwm-omap-dmtimer.c | 54 +++++++++++++++++++++++++---------
 drivers/pwm/pwm-tiehrpwm.c     | 19 ++++++------
 5 files changed, 86 insertions(+), 42 deletions(-)

-- 
2.56.1.750.gf70dc682cf-goog


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [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
  0 siblings, 0 replies; 2+ 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 related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23  7:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox