Skip to content

Commit 615f4e8

Browse files
Uwe Kleine-Königthierryreding
authored andcommitted
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: 99b82ab ("pwm: Add Renesas TPU PWM driver") Signed-off-by: Uwe Kleine-König <[email protected]> Reviewed-by: Geert Uytterhoeven <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 3c17337 commit 615f4e8

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

drivers/pwm/pwm-renesas-tpu.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,29 @@ static void tpu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
242242
}
243243

244244
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)
246246
{
247247
struct tpu_pwm_device *tpd = pwm_get_chip_data(pwm);
248248
struct tpu_device *tpu = to_tpu_device(chip);
249249
unsigned int prescaler;
250250
bool duty_only = false;
251251
u32 clk_rate;
252-
u32 period;
252+
u64 period;
253253
u32 duty;
254254
int ret;
255255

256256
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+
}
257266

258-
period = clk_rate / (NSEC_PER_SEC / period_ns);
267+
period = mul_u64_u64_div_u64(clk_rate, period_ns, NSEC_PER_SEC);
259268

260269
/*
261270
* Find the minimal prescaler in [0..3] such that
@@ -292,18 +301,15 @@ static int tpu_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
292301

293302
period >>= 2 * prescaler;
294303

295-
if (duty_ns) {
296-
duty = (clk_rate >> 2 * prescaler)
297-
/ (NSEC_PER_SEC / duty_ns);
298-
if (duty > period)
299-
return -EINVAL;
300-
} else {
304+
if (duty_ns)
305+
duty = mul_u64_u64_div_u64(clk_rate, duty_ns,
306+
(u64)NSEC_PER_SEC << (2 * prescaler));
307+
else
301308
duty = 0;
302-
}
303309

304310
dev_dbg(&tpu->pdev->dev,
305311
"rate %u, prescaler %u, period %u, duty %u\n",
306-
clk_rate, 1 << (2 * prescaler), period, duty);
312+
clk_rate, 1 << (2 * prescaler), (u32)period, duty);
307313

308314
if (tpd->prescaler == prescaler && tpd->period == period)
309315
duty_only = true;

0 commit comments

Comments
 (0)