diff options
author | Uwe Kleine-König <u.kleine-koenig@pengutronix.de> | 2022-04-20 14:12:40 +0200 |
---|---|---|
committer | Thierry Reding <thierry.reding@gmail.com> | 2022-05-20 16:18:40 +0200 |
commit | 615f4e84461b71e5fed01d9f6d9d98ef3dd1d452 (patch) | |
tree | d66b85a1e12e2f88f2e26106c1a21b742c98d799 /.mailmap | |
parent | pwm: renesas-tpu: Improve maths to compute register settings (diff) | |
download | linux-615f4e84461b71e5fed01d9f6d9d98ef3dd1d452.tar.xz linux-615f4e84461b71e5fed01d9f6d9d98ef3dd1d452.zip |
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>
Diffstat (limited to '.mailmap')
0 files changed, 0 insertions, 0 deletions