On Thu, Nov 05, 2026 at 02:38:04PM +0000, Lee Jones wrote: > 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. > > 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 > --- > drivers/pwm/pwm-mxs.c | 32 ++++++++++++++++++-------------- > 1 file changed, 18 insertions(+), 14 deletions(-) > > diff --git a/drivers/pwm/pwm-mxs.c b/drivers/pwm/pwm-mxs.c > index e1a3df6549fc..16233764fd3c 100644 > --- a/drivers/pwm/pwm-mxs.c > +++ b/drivers/pwm/pwm-mxs.c > @@ -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. > + 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)) Oh, calling pwm_is_enabled() is prior art :-\ > - 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