• Uwe Kleine-König's avatar
    pwm: renesas-tpu: Improve precision of period and duty_cycle calculation · 615f4e84
    Uwe Kleine-König authored
    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: 99b82abb ("pwm: Add Renesas TPU PWM driver")
    Signed-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
    Reviewed-by: default avatarGeert Uytterhoeven <geert+renesas@glider.be>
    Signed-off-by: default avatarThierry Reding <thierry.reding@gmail.com>
    615f4e84
pwm-renesas-tpu.c 12.1 KB