Skip to contentSkip to conversation

pwm: Fix various bugs reported by Sashiko

PATCH Superseded Lee Jones 4 participants 24 messages Nov 5 – Nov 22

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover pwm: Fix various bugs reported by Sashiko

These issues were reported by Sashiko.

Lee Jones (8):
pwm: tegra: Prevent 32-bit period truncation and division by zero
pwm: mxs: Return early when disabling and fix error-path clock leak
pwm: loongson: Fix 32-bit control register truncation in
pwm_loongson_set_polarity()
pwm: axi-pwmgen: Enable clocks before accessing hardware registers in

    probe()

pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume
pwm: omap-dmtimer: Handle negative error codes from get_pwm_status()
pwm: tiehrpwm: Enable runtime PM before registering PWM chip
pwm: img: Unregister PWM chip before disabling runtime PM and clocks

 drivers/pwm/pwm-axi-pwmgen.c   | 20 ++++++++++----------
 drivers/pwm/pwm-fsl-ftm.c      |  7 +++++++
 drivers/pwm/pwm-img.c          |  8 ++++----
 drivers/pwm/pwm-loongson.c     |  2 +-
 drivers/pwm/pwm-mxs.c          | 32 ++++++++++++++++++--------------
 drivers/pwm/pwm-omap-dmtimer.c |  8 ++++++--
 drivers/pwm/pwm-tegra.c        | 14 +++++++-------
 drivers/pwm/pwm-tiehrpwm.c     | 15 ++++++++-------
 8 files changed, 61 insertions(+), 45 deletions(-)
Cc Andrew Bresticker <[email protected]>
Cc Binbin Zhou <[email protected]>
Cc David Lechner <[email protected]>
Cc Drew Fustini <[email protected]>
Cc Ezequiel Garcia <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Frank Li <[email protected]>
Cc Grant Erickson <[email protected]>
Cc Huacai Chen <[email protected]>
Cc Joachim Eastwood <[email protected]>
Cc Jonathan Hunter <[email protected]>
Cc Juxin Gao <[email protected]>
Cc Lokesh Vutla <[email protected]>
Cc Michael Hennerich <[email protected]>
Cc Mikko Perttunen <[email protected]>
Cc Naidu Tellapati <[email protected]>
Cc Neil Armstrong <[email protected]>
Cc NeilBrown <[email protected]>
Cc "Nuno Sá" <[email protected]>
Cc Pengutronix Kernel Team <[email protected]>
Cc "Philip, Avinash" <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Sai Masarapu <[email protected]>
Cc Sandipan Patra <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc Sergiu Cuciurean <[email protected]>
Cc Shawn Guo <[email protected]>
Cc Shenwei Wang <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Trevor Gamblin <[email protected]>
Cc "Uwe Kleine-König" <[email protected]>
Cc Vaibhav Bedia <[email protected]>
Cc Vladimir Zapolskiy <[email protected]>
Cc Xiubo Li <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#1 lore raw

PATCH 1/8 pwm: tegra: Prevent 32-bit period truncation and division by zero

tegra_pwm_apply() passes the 64-bit state->duty_cycle and state->period values into tegra_pwm_config(), which accepts signed 32-bit int parameters and performs DIV_ROUND_CLOSEST_ULL(c, period_ns) before validating period_ns against pc->min_period_ns. If a consumer requests a period that is a non-zero multiple of 2^32 nanoseconds, period_ns truncates to zero and triggers a divide-by-zero panic.

Widen duty_ns and period_ns to u64 and validate that period_ns lies within [pc->min_period_ns, U32_MAX] prior to dividing by period_ns.

Fixes 1d7796bdb63a ("pwm: tegra: Support dynamic clock frequency configuration")
Fixes fd3ddd4355c0 ("pwm: tegra: Implement .apply() callback")
---
 drivers/pwm/pwm-tegra.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/pwm/pwm-tegra.c +7 -7
index 5cdbe120ba2d..f99ae2b3a276 100644
@@ -138,7 +138,7 @@ static inline void tegra_pwm_writel(struct pwm_device *pwm, unsigned int offset,
 }
 
 static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
-			    int duty_ns, int period_ns)
+			    u64 duty_ns, u64 period_ns)
 {
 	struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
 	unsigned long long c = duty_ns;
@@ -146,6 +146,12 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	u32 val = 0;
 	int err;
 
+	/*
+	 *  min period = max clock limit / TEGRA_PWM_DEPTH
+	 */
+	if (period_ns < pc->min_period_ns || period_ns > U32_MAX)
+		return -EINVAL;
+
 	/*
 	 * Convert from duty_ns / period_ns to a fixed number of duty ticks
 	 * per TEGRA_PWM_DEPTH cycles and make sure to round to the
@@ -156,12 +162,6 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	val = (u32)c << TEGRA_PWM_DUTY_SHIFT;
 
-	/*
-	 *  min period = max clock limit / TEGRA_PWM_DEPTH
-	 */
-	if (period_ns < pc->min_period_ns)
-		return -EINVAL;
-
 	/*
 	 * Compute the prescaler value for which TEGRA_PWM_DEPTH
 	 * cycles at the PWM clock rate will take period_ns nanoseconds.
Cc Sandipan Patra <[email protected]>
Cc "Uwe Kleine-König" <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#5 lore raw

PATCH 2/8 pwm: mxs: Return early when disabling and fix error-path clock leak

In reply to Lee Jones

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, and balance clk_prepare_enable() on the -EINVAL error path.

---
 drivers/pwm/pwm-mxs.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
drivers/pwm/pwm-mxs.c +18 -14
index e1a3df6549fc..16233764fd3c 100644
@@ -56,6 +56,14 @@ 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_is_enabled(pwm)) {
+			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
@@ -67,9 +75,6 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			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_is_enabled(pwm))
+				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_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);
 	}
 	return 0;
 }
Cc Rasmus Villemoes <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#8 lore raw

PATCH 3/8 pwm: loongson: Fix 32-bit control register truncation in pwm_loongson_set_polarity()

In reply to Lee Jones

pwm_loongson_set_polarity() stores the 32-bit value read from LOONGSON_PWM_REG_CTRL into a 16-bit local variable before writing it back to the register, silently clearing the upper 16 bits.

Change val from u16 to u32 to match the 32-bit register width used throughout the rest of the driver.

Fixes 2b62c89448dd ("pwm: Add Loongson PWM controller support")
---
 drivers/pwm/pwm-loongson.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-loongson.c +1 -1
index 98c809bde2b3..4e8b54c8016c 100644
@@ -76,7 +76,7 @@ static inline void pwm_loongson_writel(struct pwm_loongson_ddata *ddata,
 static int pwm_loongson_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
 				     enum pwm_polarity polarity)
 {
-	u16 val;
+	u32 val;
 	struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
 
 	val = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_CTRL);
Cc Binbin Zhou <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#9 lore raw

PATCH 4/8 pwm: axi-pwmgen: Enable clocks before accessing hardware registers in probe()

In reply to Lee Jones

axi_pwmgen_probe() calls axi_pwmgen_setup(), which reads and writes MMIO registers to verify the IP core version and determine the channel count, before acquiring and enabling axi_clk. If the bootloader leaves the AXI peripheral clock gated, accessing the registers causes an unclocked bus access fault.

Acquire and enable the clocks before calling axi_pwmgen_setup().

Fixes 41814fe5c782 ("pwm: Add driver for AXI PWM generator")
---
 drivers/pwm/pwm-axi-pwmgen.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/pwm/pwm-axi-pwmgen.c +10 -10
index b40522f01002..7c1b38124787 100644
@@ -270,16 +270,6 @@ static int axi_pwmgen_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(regmap),
 				     "failed to init register map\n");
 
-	ret = axi_pwmgen_setup(regmap, dev);
-	if (ret < 0)
-		return ret;
-
-	chip = devm_pwmchip_alloc(dev, ret, sizeof(*ddata));
-	if (IS_ERR(chip))
-		return PTR_ERR(chip);
-	ddata = pwmchip_get_drvdata(chip);
-	ddata->regmap = regmap;
-
 	/*
 	 * Using NULL here instead of "axi" for backwards compatibility. There
 	 * are some dtbs that don't give clock-names and have the "ext" clock
@@ -305,6 +295,16 @@ static int axi_pwmgen_probe(struct platform_device *pdev)
 	if (ret)
 		return dev_err_probe(dev, ret, "failed to get exclusive rate\n");
 
+	ret = axi_pwmgen_setup(regmap, dev);
+	if (ret < 0)
+		return ret;
+
+	chip = devm_pwmchip_alloc(dev, ret, sizeof(*ddata));
+	if (IS_ERR(chip))
+		return PTR_ERR(chip);
+	ddata = pwmchip_get_drvdata(chip);
+	ddata->regmap = regmap;
+
 	ddata->clk_rate_hz = clk_get_rate(clk);
 	if (!ddata->clk_rate_hz || ddata->clk_rate_hz > NSEC_PER_SEC)
 		return dev_err_probe(dev, -EINVAL,
Cc Drew Fustini <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#14 lore raw

PATCH 5/8 pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume

In reply to Lee Jones

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 across fsl_pwm_resume() around regcache_sync(), matching fsl_pwm_init().

Fixes 82a9c55a2bb0 ("pwm: fsl-ftm: Added a dedicated IP interface clock")
---
 drivers/pwm/pwm-fsl-ftm.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm-fsl-ftm.c +7 -0
index 35406b2e1925..9e5470e2efe6 100644
@@ -501,8 +501,13 @@ 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;
 
+	ret = clk_prepare_enable(fpc->ipg_clk);
+	if (ret)
+		return ret;
+
 	for (i = 0; i < chip->npwm; i++) {
 		struct pwm_device *pwm = &chip->pwms[i];
 
@@ -522,6 +527,8 @@ static int fsl_pwm_resume(struct device *dev)
 	regcache_cache_only(fpc->regmap, false);
 	regcache_sync(fpc->regmap);
 
+	clk_disable_unprepare(fpc->ipg_clk);
+
 	return 0;
 }
 #endif
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#16 lore raw

PATCH 6/8 pwm: omap-dmtimer: Handle negative error codes from get_pwm_status()

In reply to Lee Jones

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 check for negative error codes before testing register bits.

Fixes 6b28fb6f3ca3 ("pwm: omap-dmtimer: Implement .apply callback")
---
 drivers/pwm/pwm-omap-dmtimer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/pwm/pwm-omap-dmtimer.c +6 -2
index 51830a87275c..1e4d8b9e224f 100644
@@ -111,9 +111,11 @@ static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap)
  */
 static bool 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 false;
 
 	return !!(status & OMAP_TIMER_CTRL_ST);
 }
@@ -126,9 +128,11 @@ static bool pwm_omap_dmtimer_is_enabled(struct pwm_omap_dmtimer_chip *omap)
  */
 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 PWM_POLARITY_NORMAL;
 
 	return !!(status & OMAP_TIMER_CTRL_SCPWM);
 }
Cc Lokesh Vutla <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#18 lore raw

PATCH 7/8 pwm: tiehrpwm: Enable runtime PM before registering PWM chip

In reply to Lee Jones

ehrpwm_pwm_probe() calls pwmchip_add() before setting driver data and enabling runtime PM. Because pwmchip_add() immediately exposes the PWM channels to consumers, a consumer can invoke ehrpwm_pwm_apply() before pm_runtime_enable() runs, causing registers to be accessed while the device remains runtime-suspended.

Set driver data and enable runtime PM before calling pwmchip_add(), and disable runtime PM on the error path.

Fixes 19891b20e7c2 ("pwm: pwm-tiehrpwm: PWM driver support for EHRPWM")
---
 drivers/pwm/pwm-tiehrpwm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/pwm/pwm-tiehrpwm.c +8 -7
index e3407d0a1f7f..f9dd3b91c73a 100644
@@ -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;
Cc "Philip, Avinash" <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#20 lore raw

PATCH 8/8 pwm: img: Unregister PWM chip before disabling runtime PM and clocks

In reply to Lee Jones

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() returns -EACCES or succeeds without re-enabling clocks, leading to unclocked register access.

Call pwmchip_remove() before disabling runtime PM and suspending the device.

Fixes 277bb6a29e00 ("pwm: Imagination Technologies PWM DAC driver")
---
 drivers/pwm/pwm-img.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pwm/pwm-img.c +4 -4
index 91e0b19f0c08..b6e090674c07 100644
@@ -338,11 +338,11 @@ 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);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		img_pwm_runtime_suspend(&pdev->dev);
 }
 
 #ifdef CONFIG_PM_SLEEP
Cc Naidu Tellapati <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
#2 lore raw

Re: [PATCH 1/8] pwm: tegra: Prevent 32-bit period truncation and division by zero

In reply to Lee Jones

Hello Lee,

Quoted 7 lines · Lee Jones

tegra_pwm_apply() passes the 64-bit state->duty_cycle and state->period values into tegra_pwm_config(), which accepts signed 32-bit int parameters and performs DIV_ROUND_CLOSEST_ULL(c, period_ns) before validating period_ns against pc->min_period_ns. If a consumer requests a period that is a non-zero multiple of 2^32 nanoseconds, period_ns truncates to zero and triggers a divide-by-zero panic.

Quoted 8 lines

Widen duty_ns and period_ns to u64 and validate that period_ns lies within [pc->min_period_ns, U32_MAX] prior to dividing by period_ns.

Fixes 1d7796bdb63a ("pwm: tegra: Support dynamic clock frequency configuration")
Fixes fd3ddd4355c0 ("pwm: tegra: Implement .apply() callback")

This is already addressed in
9ad5602d4943 ("pwm: tegra: Fix value discarding for duty and period") which is next-20261102 and later.

Best regards
Uwe

#10 lore raw

Re: [PATCH 4/8] pwm: axi-pwmgen: Enable clocks before accessing hardware registers in probe()

In reply to Lee Jones
Quoted 5 lines · Lee Jones

axi_pwmgen_probe() calls axi_pwmgen_setup(), which reads and writes MMIO registers to verify the IP core version and determine the channel count, before acquiring and enabling axi_clk. If the bootloader leaves the AXI peripheral clock gated, accessing the registers causes an unclocked bus access fault.

Do you have the hardware? Or what convinced you that an access fault happens?

Best regards
Uwe

Reviewed-by on v1 2/8
#6 lore raw

Re: [PATCH 2/8] pwm: mxs: Return early when disabling and fix error-path clock leak

In reply to Lee Jones
Quoted 9 lines · Lee Jones

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.

Quoted 8 lines

Handle the !state->enabled path upfront and return early, and balance clk_prepare_enable() on the -EINVAL error path.

--

Quoted 73 lines
 drivers/pwm/pwm-mxs.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
drivers/pwm/pwm-mxs.c +18 -14
index e1a3df6549fc..16233764fd3c 100644
@@ -56,6 +56,14 @@ 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_is_enabled(pwm)) {
+			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
@@ -67,9 +75,6 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			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_is_enabled(pwm))
+				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_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);
 	}
 	return 0;
 }

Cc: Rasmus Villemoes <[email protected]>
--
2.56.0.398.g882cbad275-goog

author
#11 lore raw

Re: [PATCH 4/8] pwm: axi-pwmgen: Enable clocks before accessing hardware registers in probe()

In reply to Uwe Kleine-König
Quoted 9 lines · Uwe Kleine-König
Quoted 5 lines · Lee Jones

axi_pwmgen_probe() calls axi_pwmgen_setup(), which reads and writes MMIO registers to verify the IP core version and determine the channel count, before acquiring and enabling axi_clk. If the bootloader leaves the AXI peripheral clock gated, accessing the registers causes an unclocked bus access fault.

Do you have the hardware? Or what convinced you that an access fault happens?

These are Sashiko bugs, as we discussed out-of-band.

None of these have been tested on real h/w.

Signature · 1 line
Lee Jones
author
#12 lore raw

Re: [PATCH 4/8] pwm: axi-pwmgen: Enable clocks before accessing hardware registers in probe()

Quoted 9 lines · Lee Jones
Quoted 7 lines · Uwe Kleine-König
Quoted 5 lines · Lee Jones

axi_pwmgen_probe() calls axi_pwmgen_setup(), which reads and writes MMIO registers to verify the IP core version and determine the channel count, before acquiring and enabling axi_clk. If the bootloader leaves the AXI peripheral clock gated, accessing the registers causes an unclocked bus access fault.

Quoted 6 lines
Quoted 2 lines

Do you have the hardware? Or what convinced you that an access fault happens?

These are Sashiko bugs, as we discussed out-of-band.

None of these have been tested on real h/w.

I'll make that clear in the cover-letters going forward.

Signature · 1 line
Lee Jones
maintainer Reviewed-by on v1 4/8
#13 lore raw

Re: [PATCH 4/8] pwm: axi-pwmgen: Enable clocks before accessing hardware registers in probe()

In reply to Lee Jones
Quoted 6 lines · Lee Jones

axi_pwmgen_probe() calls axi_pwmgen_setup(), which reads and writes MMIO registers to verify the IP core version and determine the channel count, before acquiring and enabling axi_clk. If the bootloader leaves the AXI peripheral clock gated, accessing the registers causes an unclocked bus access fault.

Quoted 7 lines

Acquire and enable the clocks before calling axi_pwmgen_setup().

Fixes 41814fe5c782 ("pwm: Add driver for AXI PWM generator")

---

FWIW, I think this is a valid fix. Likely never happens because the AXI clock is mostly "assumed enabled" but we already had some edge cases in other cores related to not having the clock enabled. Anyways, LGTM:

Quoted 45 lines
 drivers/pwm/pwm-axi-pwmgen.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/pwm/pwm-axi-pwmgen.c +10 -10
index b40522f01002..7c1b38124787 100644
@@ -270,16 +270,6 @@ static int axi_pwmgen_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(regmap),
 				     "failed to init register map\n");
 
-	ret = axi_pwmgen_setup(regmap, dev);
-	if (ret < 0)
-		return ret;
-
-	chip = devm_pwmchip_alloc(dev, ret, sizeof(*ddata));
-	if (IS_ERR(chip))
-		return PTR_ERR(chip);
-	ddata = pwmchip_get_drvdata(chip);
-	ddata->regmap = regmap;
-
 	/*
 	 * Using NULL here instead of "axi" for backwards compatibility. There
 	 * are some dtbs that don't give clock-names and have the "ext" clock
@@ -305,6 +295,16 @@ static int axi_pwmgen_probe(struct platform_device *pdev)
 	if (ret)
 		return dev_err_probe(dev, ret, "failed to get exclusive rate\n");
 
+	ret = axi_pwmgen_setup(regmap, dev);
+	if (ret < 0)
+		return ret;
+
+	chip = devm_pwmchip_alloc(dev, ret, sizeof(*ddata));
+	if (IS_ERR(chip))
+		return PTR_ERR(chip);
+	ddata = pwmchip_get_drvdata(chip);
+	ddata->regmap = regmap;
+
 	ddata->clk_rate_hz = clk_get_rate(clk);
 	if (!ddata->clk_rate_hz || ddata->clk_rate_hz > NSEC_PER_SEC)
 		return dev_err_probe(dev, -EINVAL,
Cc Drew Fustini <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
author
#3 lore raw

Re: [PATCH 1/8] pwm: tegra: Prevent 32-bit period truncation and division by zero

In reply to Uwe Kleine-König
Quoted 19 lines · Uwe Kleine-König

Hello Lee,

Quoted 7 lines · Lee Jones

tegra_pwm_apply() passes the 64-bit state->duty_cycle and state->period values into tegra_pwm_config(), which accepts signed 32-bit int parameters and performs DIV_ROUND_CLOSEST_ULL(c, period_ns) before validating period_ns against pc->min_period_ns. If a consumer requests a period that is a non-zero multiple of 2^32 nanoseconds, period_ns truncates to zero and triggers a divide-by-zero panic.

Quoted 8 lines

Widen duty_ns and period_ns to u64 and validate that period_ns lies within [pc->min_period_ns, U32_MAX] prior to dividing by period_ns.

Fixes 1d7796bdb63a ("pwm: tegra: Support dynamic clock frequency configuration")
Fixes fd3ddd4355c0 ("pwm: tegra: Implement .apply() callback")
Quoted 3 lines

This is already addressed in
9ad5602d4943 ("pwm: tegra: Fix value discarding for duty and period") which is next-20261102 and later.

Would you like me to resubmit the set without this or can you omit it?

Signature · 1 line
Lee Jones
#4 lore raw

Re: [PATCH 1/8] pwm: tegra: Prevent 32-bit period truncation and division by zero

Hello Lee,

Quoted 6 lines · Lee Jones
Quoted 3 lines · Uwe Kleine-König

This is already addressed in
9ad5602d4943 ("pwm: tegra: Fix value discarding for duty and period") which is next-20261102 and later.

Would you like me to resubmit the set without this or can you omit it?

It's fine, I'll pick your series partially when I came around to give it a 2nd look

Best reagrds
Uwe

#17 lore raw

Re: [PATCH 6/8] pwm: omap-dmtimer: Handle negative error codes from get_pwm_status()

In reply to Lee Jones
Quoted 36 lines · Lee Jones

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 check for negative error codes before testing register bits.

Fixes 6b28fb6f3ca3 ("pwm: omap-dmtimer: Implement .apply callback")
---
 drivers/pwm/pwm-omap-dmtimer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/pwm/pwm-omap-dmtimer.c +3 -1
index 51830a87275c..1e4d8b9e224f 100644
@@ -111,9 +111,11 @@ static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap)
  */
 static bool 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 false;
 
 	return !!(status & OMAP_TIMER_CTRL_ST);
 }

drivers/pwm/pwm-omap-dmtimer.c static bool pwm_omap_dmtimer_is_enabled(struct pwm_omap_dmtimer_chip *omap) · line 135

Quoted 9 lines
drivers/pwm/pwm-omap-dmtimer.c +3 -1
@@ -126,9 +128,11 @@ static bool pwm_omap_dmtimer_is_enabled(struct pwm_omap_dmtimer_chip *omap)
  */
 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 PWM_POLARITY_NORMAL;

This looks incomplete. The function is used in pwm_omap_dmtimer_apply() as follows:

        if (pwm_omap_dmtimer_polarity(omap) != state->polarity)
                pwm_omap_dmtimer_set_polarity(chip, pwm, state->polarity);

IMHO if there is a problem with reading out the current polarity apply should fail. With the current approach the polarity setting might just be ignored. The case above is a bit similar, but I think there it's less likely that strange things happen.

Best regards
Uwe

#15 lore raw

Re: [PATCH 5/8] pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume

In reply to Lee Jones

Hello Lee,

Quoted 37 lines · Lee Jones

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 across fsl_pwm_resume() around regcache_sync(), matching fsl_pwm_init().

Fixes 82a9c55a2bb0 ("pwm: fsl-ftm: Added a dedicated IP interface clock")
---
 drivers/pwm/pwm-fsl-ftm.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm-fsl-ftm.c +5 -0
index 35406b2e1925..9e5470e2efe6 100644
@@ -501,8 +501,13 @@ 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;
 
+	ret = clk_prepare_enable(fpc->ipg_clk);
+	if (ret)
+		return ret;
+
 	for (i = 0; i < chip->npwm; i++) {
 		struct pwm_device *pwm = &chip->pwms[i];
 

drivers/pwm/pwm-fsl-ftm.c static int fsl_pwm_resume(struct device *dev) · line 532

Quoted 6 lines
drivers/pwm/pwm-fsl-ftm.c +2 -0
@@ -522,6 +527,8 @@ static int fsl_pwm_resume(struct device *dev)
 	regcache_cache_only(fpc->regmap, false);
 	regcache_sync(fpc->regmap);
 
+	clk_disable_unprepare(fpc->ipg_clk);
+
 	return 0;

Just from looking at the code (i.e. without the commit log available) it's not obvious why the clk_enable is needed. Can you add a comment and maybe move the enable call to after the loop?

Best regards
Uwe

#19 lore raw

Re: [PATCH 7/8] pwm: tiehrpwm: Enable runtime PM before registering PWM chip

In reply to Lee Jones

Hello,

Quoted 5 lines · Lee Jones

ehrpwm_pwm_probe() calls pwmchip_add() before setting driver data and enabling runtime PM. Because pwmchip_add() immediately exposes the PWM channels to consumers, a consumer can invoke ehrpwm_pwm_apply() before pm_runtime_enable() runs, causing registers to be accessed while the device remains runtime-suspended.

Is this correct? Until pm_runtime_enable() is called the device doesn't go into suspend, does it?

Quoted 24 lines

Set driver data and enable runtime PM before calling pwmchip_add(), and disable runtime PM on the error path.

Fixes 19891b20e7c2 ("pwm: pwm-tiehrpwm: PWM driver support for EHRPWM")
---
 drivers/pwm/pwm-tiehrpwm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/pwm/pwm-tiehrpwm.c +0 -5
index e3407d0a1f7f..f9dd3b91c73a 100644
@@ -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;
-	}

drivers/pwm/pwm-tiehrpwm.c static int ehrpwm_pwm_probe(struct platform_device *pdev) · line 472

Quoted 10 lines
drivers/pwm/pwm-tiehrpwm.c +6 -1
-
 	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;
+	}
+

If you switch the order here, it would be awesome to also switch in the .remove() callback to have the order match.

Quoted 12 lines
drivers/pwm/pwm-tiehrpwm.c +2 -1
 	return 0;
 
-err_clk_unprepare:
+err_pm_disable:
+	pm_runtime_disable(&pdev->dev);
 	clk_unprepare(pc->tbclk);
 
 	return ret;
Cc "Philip, Avinash" <[email protected]>
Signature · 1 line
2.56.0.398.g882cbad275-goog
#21 lore raw

Re: [PATCH 8/8] pwm: img: Unregister PWM chip before disabling runtime PM and clocks

In reply to Lee Jones
Quoted 24 lines · Lee Jones

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() returns -EACCES or succeeds without re-enabling clocks, leading to unclocked register access.

Call pwmchip_remove() before disabling runtime PM and suspending the device.

Fixes 277bb6a29e00 ("pwm: Imagination Technologies PWM DAC driver")
---
 drivers/pwm/pwm-img.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pwm/pwm-img.c +0 -0
index 91e0b19f0c08..b6e090674c07 100644
@@ -338,11 +338,11 @@ static void img_pwm_remove(struct platform_device *pdev)
 {
 	struct pwm_chip *chip = platform_get_drvdata(pdev);

drivers/pwm/pwm-img.c static void img_pwm_remove(struct platform_device *pdev) · line 345

Quoted 10 lines
drivers/pwm/pwm-img.c +4 -4
 
-	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);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		img_pwm_runtime_suspend(&pdev->dev);

Calling pm_runtime_disable() might result in a call to img_pwm_runtime_resume() (does it?). In img_pwm_runtime_resume() we have:

	struct pwm_chip *chip = dev_get_drvdata(dev);
	struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
	...

which doesn't look like an operation that should be done after pwmchip_remove()?

Best regards
Uwe

#7 lore raw

Re: [PATCH 2/8] pwm: mxs: Return early when disabling and fix error-path clock leak

In reply to Lee Jones
Quoted 25 lines · Lee Jones

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, and balance clk_prepare_enable() on the -EINVAL error path.

---
 drivers/pwm/pwm-mxs.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
drivers/pwm/pwm-mxs.c +0 -0
index e1a3df6549fc..16233764fd3c 100644

drivers/pwm/pwm-mxs.c static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 60

Quoted 5 lines
drivers/pwm/pwm-mxs.c +2 -0
@@ -56,6 +56,14 @@ 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_is_enabled(pwm)) {

Please don't call pwm consumer functions from the lowlevel driver. Currently there is no locking in pwm_is_enabled() (as it only checks the last state that was applied and not the actual hardware state), but this calls for trouble. So either check pwm->state or even better the actual hardware bit.

Quoted 10 lines
drivers/pwm/pwm-mxs.c +6 -0
+			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

drivers/pwm/pwm-mxs.c static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 70

Quoted 4 lines
drivers/pwm/pwm-mxs.c +0 -1
@@ -67,9 +75,6 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			return ret;
 	}
 
-	if (!state->enabled && pwm_is_enabled(pwm))

Oh, calling pwm_is_enabled() is prior art :-\

drivers/pwm/pwm-mxs.c static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 80

Quoted 5 lines
drivers/pwm/pwm-mxs.c +0 -2
-		writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
-
 	rate = clk_get_rate(mxs->clk);
 	while (1) {
 		c = rate >> cdiv_shift[div];

Best regards
Uwe

#22 lore raw
In reply to Lee Jones

Hello Lee,

Quoted 8 lines · Lee Jones

These issues were reported by Sashiko.

Lee Jones (8):
pwm: tegra: Prevent 32-bit period truncation and division by zero
pwm: mxs: Return early when disabling and fix error-path clock leak
pwm: loongson: Fix 32-bit control register truncation in
pwm_loongson_set_polarity()
pwm: axi-pwmgen: Enable clocks before accessing hardware registers in

Quoted 5 lines
    probe()

pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume
pwm: omap-dmtimer: Handle negative error codes from get_pwm_status()
pwm: tiehrpwm: Enable runtime PM before registering PWM chip
pwm: img: Unregister PWM chip before disabling runtime PM and clocks

I applied patches #3 and #4 to

https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next

, for the others there is feedback to address on your side.

Thanks for your patches, very appreciated!

Best regards
Uwe

author
#23 lore raw
Quoted 17 lines · Uwe Kleine-König

Hello Lee,

Quoted 8 lines · Lee Jones

These issues were reported by Sashiko.

Lee Jones (8):
pwm: tegra: Prevent 32-bit period truncation and division by zero
pwm: mxs: Return early when disabling and fix error-path clock leak
pwm: loongson: Fix 32-bit control register truncation in
pwm_loongson_set_polarity()
pwm: axi-pwmgen: Enable clocks before accessing hardware registers in

Quoted 5 lines
    probe()

pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume
pwm: omap-dmtimer: Handle negative error codes from get_pwm_status()
pwm: tiehrpwm: Enable runtime PM before registering PWM chip
pwm: img: Unregister PWM chip before disabling runtime PM and clocks

Quoted 7 lines

I applied patches #3 and #4 to

https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next

, for the others there is feedback to address on your side.

Thanks for your patches, very appreciated!

No problem. I'll turn these around shortly.

Signature · 1 line
Lee Jones