pwm: iqs620a: Fix overflow and optimize calculations
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Fri, 15 Jan 2021 07:32:39 +0000 (08:32 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 4 Mar 2021 10:38:17 +0000 (11:38 +0100)
commite47685ec4cb8d59f8706da532ef58b403871b534
treef91b3999b832b47aa982e4e570dfcd79e890ed70
parentdae49384d0d7695540e2d75168f323cef1384810
pwm: iqs620a: Fix overflow and optimize calculations

[ Upstream commit 72d6b2459dbd539c1369149e501fdc3dc8ddef16 ]

If state->duty_cycle is 0x100000000000000, the previous calculation of
duty_scale overflows and yields a duty cycle ratio of 0% instead of
100%. Fix this by clamping the requested duty cycle to the maximal
possible duty cycle first. This way it is possible to use a native
integer division instead of a (depending on the architecture) more
expensive 64bit division.

With this change in place duty_scale cannot be bigger than 256 which
allows to simplify the calculation of duty_val.

Fixes: 6f0841a8197b ("pwm: Add support for Azoteq IQS620A PWM generator")
Tested-by: Jeff LaBundy <jeff@labundy.com>
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-iqs620a.c