]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
pwm: tiehrpwm: Fix corner case in clock divisor calculation
authorUwe Kleine-König <u.kleine-koenig@baylibre.com>
Mon, 11 Aug 2025 16:01:02 +0000 (18:01 +0200)
committerUwe Kleine-König <ukleinek@kernel.org>
Mon, 15 Sep 2025 09:39:43 +0000 (11:39 +0200)
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>
drivers/pwm/pwm-tiehrpwm.c

index a23e48b8523dbf33bcd9d1fd719c82cb22d0d5a7..7a86cb090f76f13de092e29d7d074b3b99a1c761 100644 (file)
@@ -161,7 +161,7 @@ static int set_prescale_div(unsigned long rqst_prescaler, u16 *prescale_div,
 
                        *prescale_div = (1 << clkdiv) *
                                        (hspclkdiv ? (hspclkdiv * 2) : 1);
-                       if (*prescale_div > rqst_prescaler) {
+                       if (*prescale_div >= rqst_prescaler) {
                                *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) |
                                        (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT);
                                return 0;
@@ -224,7 +224,7 @@ static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
        pc->period_cycles[pwm->hwpwm] = period_cycles;
 
        /* Configure clock prescaler to support Low frequency PWM wave */
-       if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval,
+       if (set_prescale_div(DIV_ROUND_UP(period_cycles, PERIOD_MAX), &ps_divval,
                             &tb_divval)) {
                dev_err(pwmchip_parent(chip), "Unsupported values\n");
                return -EINVAL;