]> git.baikalelectronics.ru Git - kernel.git/commit
pwm: lpc18xx: Fix period handling
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Tue, 12 Jul 2022 16:15:19 +0000 (18:15 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 17 Aug 2022 12:23:16 +0000 (14:23 +0200)
commitc6a65bba5da2a735b14c38ed0daebec67c4f94da
tree61880252888ad04bb5b5a1a9f1847848a250135a
parent9e7714adf827b4c09963e7c31b352e1c77c38db2
pwm: lpc18xx: Fix period handling

[ Upstream commit b4a38f53fa36cecf0de5c995220b04e4859ce2f3 ]

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: 151ff3218e43 ("pwm: NXP LPC18xx PWM/SCT driver")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/pwm/pwm-lpc18xx-sct.c