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

pwm: rcar: Improve register calculation

There were several issues in the function rcar_pwm_set_counter():

- The u64 values period_ns and duty_ns were cast to int on function
call which might loose bits on 32 bit architectures.
Fix: Make parameters to rcar_pwm_set_counter() u64
- The algorithm divided by the result of a division which looses
precision.
Fix: Make use of mul_u64_u64_div_u64()
- The calculated values were just masked to fit the respective register
fields which again might loose bits.
Fix: Explicitly check for overlow

Implement the respective fixes.

A side effect of fixing the 2nd issue is that there is no division by 0
if clk_get_rate() returns 0.

Fixes: ed6c1476bf7f ("pwm: Add support for R-Car PWM Timer")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Link: https://lore.kernel.org/r/ab3dac794b2216cc1cc56d65c93dd164f8bd461b.1743501688.git.u.kleine-koenig@baylibre.com
[ukleinek: Added an explicit #include <linux/bitfield.h> to please the
0day build bot]
Link: https://lore.kernel.org/oe-kbuild-all/202504031354.VJtxScP5-lkp@intel.com/
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: Uwe Kleine-König <ukleinek@kernel.org>

authored by

Uwe Kleine-König and committed by
Uwe Kleine-König
e7327c19 7ca59947

+13 -11
+13 -11
drivers/pwm/pwm-rcar.c
··· 8 8 * - The hardware cannot generate a 0% duty cycle. 9 9 */ 10 10 11 + #include <linux/bitfield.h> 11 12 #include <linux/clk.h> 12 13 #include <linux/err.h> 13 14 #include <linux/io.h> ··· 103 102 rcar_pwm_write(rp, value, RCAR_PWMCR); 104 103 } 105 104 106 - static int rcar_pwm_set_counter(struct rcar_pwm_chip *rp, int div, int duty_ns, 107 - int period_ns) 105 + static int rcar_pwm_set_counter(struct rcar_pwm_chip *rp, int div, u64 duty_ns, 106 + u64 period_ns) 108 107 { 109 - unsigned long long one_cycle, tmp; /* 0.01 nanoseconds */ 108 + unsigned long long tmp; 110 109 unsigned long clk_rate = clk_get_rate(rp->clk); 111 110 u32 cyc, ph; 112 111 113 - one_cycle = NSEC_PER_SEC * 100ULL << div; 114 - do_div(one_cycle, clk_rate); 112 + /* div <= 24 == RCAR_PWM_MAX_DIVISION, so the shift doesn't overflow. */ 113 + tmp = mul_u64_u64_div_u64(period_ns, clk_rate, (u64)NSEC_PER_SEC << div); 114 + if (tmp > FIELD_MAX(RCAR_PWMCNT_CYC0_MASK)) 115 + tmp = FIELD_MAX(RCAR_PWMCNT_CYC0_MASK); 115 116 116 - tmp = period_ns * 100ULL; 117 - do_div(tmp, one_cycle); 118 - cyc = (tmp << RCAR_PWMCNT_CYC0_SHIFT) & RCAR_PWMCNT_CYC0_MASK; 117 + cyc = FIELD_PREP(RCAR_PWMCNT_CYC0_MASK, tmp); 119 118 120 - tmp = duty_ns * 100ULL; 121 - do_div(tmp, one_cycle); 122 - ph = tmp & RCAR_PWMCNT_PH0_MASK; 119 + tmp = mul_u64_u64_div_u64(duty_ns, clk_rate, (u64)NSEC_PER_SEC << div); 120 + if (tmp > FIELD_MAX(RCAR_PWMCNT_PH0_MASK)) 121 + tmp = FIELD_MAX(RCAR_PWMCNT_PH0_MASK); 122 + ph = FIELD_PREP(RCAR_PWMCNT_PH0_MASK, tmp); 123 123 124 124 /* Avoid prohibited setting */ 125 125 if (cyc == 0 || ph == 0)