crc_pwm_apply() computes the duty cycle register value by dividing by the requested period: u64 level = state->duty_cycle * PWM_MAX_LEVEL; do_div(level, state->period); The PWM core accepts a period of zero for a disabled PWM: pwm_state_valid() returns true early in that case, and period_store() writes the user supplied value to state.period without further validation. Writing 0 to the period attribute of a disabled PWM therefore reaches this code with a zero divisor and crashes the kernel: $ echo 0 > /sys/class/pwm/pwmchip0/pwm0/period divide error: 0000 [#1] SMP Handle the disabled case up front and return after switching the backlight off. The duty cycle and clock divider are irrelevant while the output is off. Changes since v1: - Move the disabled path to an early return, as suggested by Uwe Kleine-König. With state->enabled known to be true below that point, the explicit .enabled tests can be dropped. - Program the duty cycle when enabling a previously disabled PWM. With the early return, a duty cycle set while the PWM was off is no longer written to the hardware, and the cached state makes the comparison below skip it on enable. Fixes: a3f37a104bc4 ("pwm: crc: Add Crystalcove (CRC) PWM driver") Cc: stable@vger.kernel.org Signed-off-by: Lin Huiyi --- drivers/pwm/pwm-crc.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/drivers/pwm/pwm-crc.c b/drivers/pwm/pwm-crc.c index 98ee5cdbd0..1389817f78 100644 --- a/drivers/pwm/pwm-crc.c +++ b/drivers/pwm/pwm-crc.c @@ -64,16 +64,21 @@ static int crc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, if (state->polarity != PWM_POLARITY_NORMAL) return -EINVAL; - if (pwm_is_enabled(pwm) && !state->enabled) { - err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 0); - if (err) { - dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err); - return err; + if (!state->enabled) { + if (pwm_is_enabled(pwm)) { + err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 0); + if (err) { + dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err); + return err; + } } + + return 0; } if (pwm_get_duty_cycle(pwm) != state->duty_cycle || - pwm_get_period(pwm) != state->period) { + pwm_get_period(pwm) != state->period || + !pwm_is_enabled(pwm)) { u64 level = state->duty_cycle * PWM_MAX_LEVEL; do_div(level, state->period); @@ -85,8 +90,7 @@ static int crc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, } } - if (pwm_is_enabled(pwm) && state->enabled && - pwm_get_period(pwm) != state->period) { + if (pwm_is_enabled(pwm) && pwm_get_period(pwm) != state->period) { /* changing the clk divisor, clear PWM_OUTPUT_ENABLE first */ err = regmap_write(crc_pwm->regmap, PWM0_CLK_DIV, 0); if (err) { @@ -95,20 +99,18 @@ static int crc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, } } - if (pwm_get_period(pwm) != state->period || - pwm_is_enabled(pwm) != state->enabled) { + if (pwm_get_period(pwm) != state->period || !pwm_is_enabled(pwm)) { int clk_div = crc_pwm_calc_clk_div(state->period); - int pwm_output_enable = state->enabled ? PWM_OUTPUT_ENABLE : 0; err = regmap_write(crc_pwm->regmap, PWM0_CLK_DIV, - clk_div | pwm_output_enable); + clk_div | PWM_OUTPUT_ENABLE); if (err) { dev_err(dev, "Error writing PWM0_CLK_DIV %d\n", err); return err; } } - if (!pwm_is_enabled(pwm) && state->enabled) { + if (!pwm_is_enabled(pwm)) { err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 1); if (err) { dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err); -- 2.56.1