Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pwm: tiehrpwm: Fix corner case in clock divisor calculation

The function set_prescale_div() is responsible for calculating the clock
divisor settings such that the input clock rate is divided down such that
the required period length is at most 0x10000 clock ticks. If period_cycles
is an integer multiple of 0x10000, the divisor period_cycles / 0x10000 is
good enough. So round up in the calculation of the required divisor and
compare it using >= instead of >.

Fixes: 19891b20e7c2 ("pwm: pwm-tiehrpwm: PWM driver support for EHRPWM")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Link: https://lore.kernel.org/r/85488616d7bfcd9c32717651d0be7e330e761b9c.1754927682.git.u.kleine-koenig@baylibre.com
Signed-off-by: Uwe Kleine-König <ukleinek@kernel.org>

authored by

Uwe Kleine-König and committed by
Uwe Kleine-König
00f83f0e bc7ce5bf

+2 -2
+2 -2
drivers/pwm/pwm-tiehrpwm.c
··· 161 161 162 162 *prescale_div = (1 << clkdiv) * 163 163 (hspclkdiv ? (hspclkdiv * 2) : 1); 164 - if (*prescale_div > rqst_prescaler) { 164 + if (*prescale_div >= rqst_prescaler) { 165 165 *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) | 166 166 (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT); 167 167 return 0; ··· 224 224 pc->period_cycles[pwm->hwpwm] = period_cycles; 225 225 226 226 /* Configure clock prescaler to support Low frequency PWM wave */ 227 - if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval, 227 + if (set_prescale_div(DIV_ROUND_UP(period_cycles, PERIOD_MAX), &ps_divval, 228 228 &tb_divval)) { 229 229 dev_err(pwmchip_parent(chip), "Unsupported values\n"); 230 230 return -EINVAL;