• Uwe Kleine-König's avatar
    pwm: lpc18xx: Fix period handling · 8933d30c
    Uwe Kleine-König authored
    The calculation:
    
    	val = (u64)NSEC_PER_SEC * LPC18XX_PWM_TIMER_MAX;
    	do_div(val, lpc18xx_pwm->clk_rate);
    	lpc18xx_pwm->max_period_ns = val;
    
    is bogus because with NSEC_PER_SEC = 1000000000,
    LPC18XX_PWM_TIMER_MAX = 0xffffffff and clk_rate < NSEC_PER_SEC this
    overflows the (on lpc18xx (i.e. ARM32) 32 bit wide) unsigned int
    .max_period_ns. This results (dependant of the actual clk rate) in an
    arbitrary limitation of the maximal period.  E.g. for clkrate =
    333333333 (Hz) we get max_period_ns = 9 instead of 12884901897.
    
    So make .max_period_ns an u64 and pass period and duty as u64 to not
    discard relevant digits. And also make use of mul_u64_u64_div_u64()
    which prevents all overflows assuming clk_rate < NSEC_PER_SEC.
    
    Fixes: 841e6f90 ("pwm: NXP LPC18xx PWM/SCT driver")
    Signed-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
    Signed-off-by: default avatarThierry Reding <thierry.reding@gmail.com>
    8933d30c
pwm-lpc18xx-sct.c 13.8 KB