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

pwm: renesas-tpu: Improve precision of period and duty_cycle calculation

Dividing by the result of a division looses precision. Consider for example
clk_rate = 33000000 and period_ns = 500001. Then

clk_rate / (NSEC_PER_SEC / period_ns)

has the exact value 16500.033, but in C this evaluates to 16508. It gets
worse for even bigger values of period_ns, so with period_ns = 500000001,
the exact result is 16500000.033 while in C we get 33000000.

For that reason use

clk_rate * period_ns / NSEC_PER_SEC

instead which doesn't suffer from this problem. To ensure this doesn't
overflow add a safeguard check for clk_rate.

Note that duty > period can never happen, so the respective check can be
dropped.

Incidentally this fixes a division by zero if period_ns > NSEC_PER_SEC.
Another side effect is that values bigger than INT_MAX for period and
duty_cyle are not wrongly discarded any more.

Fixes: 99b82abb0a35 ("pwm: Add Renesas TPU PWM driver")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>

authored by

Uwe Kleine-König and committed by
Thierry Reding
615f4e84 3c173376

+17 -11
+17 -11
drivers/pwm/pwm-renesas-tpu.c
··· 242 242 } 243 243 244 244 static int tpu_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 245 - int duty_ns, int period_ns, bool enabled) 245 + u64 duty_ns, u64 period_ns, bool enabled) 246 246 { 247 247 struct tpu_pwm_device *tpd = pwm_get_chip_data(pwm); 248 248 struct tpu_device *tpu = to_tpu_device(chip); 249 249 unsigned int prescaler; 250 250 bool duty_only = false; 251 251 u32 clk_rate; 252 - u32 period; 252 + u64 period; 253 253 u32 duty; 254 254 int ret; 255 255 256 256 clk_rate = clk_get_rate(tpu->clk); 257 + if (unlikely(clk_rate > NSEC_PER_SEC)) { 258 + /* 259 + * This won't happen in the nearer future, so this is only a 260 + * safeguard to prevent the following calculation from 261 + * overflowing. With this clk_rate * period_ns / NSEC_PER_SEC is 262 + * not greater than period_ns and so fits into an u64. 263 + */ 264 + return -EINVAL; 265 + } 257 266 258 - period = clk_rate / (NSEC_PER_SEC / period_ns); 267 + period = mul_u64_u64_div_u64(clk_rate, period_ns, NSEC_PER_SEC); 259 268 260 269 /* 261 270 * Find the minimal prescaler in [0..3] such that ··· 301 292 302 293 period >>= 2 * prescaler; 303 294 304 - if (duty_ns) { 305 - duty = (clk_rate >> 2 * prescaler) 306 - / (NSEC_PER_SEC / duty_ns); 307 - if (duty > period) 308 - return -EINVAL; 309 - } else { 295 + if (duty_ns) 296 + duty = mul_u64_u64_div_u64(clk_rate, duty_ns, 297 + (u64)NSEC_PER_SEC << (2 * prescaler)); 298 + else 310 299 duty = 0; 311 - } 312 300 313 301 dev_dbg(&tpu->pdev->dev, 314 302 "rate %u, prescaler %u, period %u, duty %u\n", 315 - clk_rate, 1 << (2 * prescaler), period, duty); 303 + clk_rate, 1 << (2 * prescaler), (u32)period, duty); 316 304 317 305 if (tpd->prescaler == prescaler && tpd->period == period) 318 306 duty_only = true;