pwm: imx27: Fix overflow for bigger periods
authorUwe Kleine-König <uwe@kleine-koenig.org>
Mon, 7 Dec 2020 14:13:24 +0000 (15:13 +0100)
committerThierry Reding <thierry.reding@gmail.com>
Thu, 17 Dec 2020 13:19:01 +0000 (14:19 +0100)
commit1ce65396e6b2386b4fd54f87beff0647a772e1cd
treecca99627399743fa0652c4a2186c20d80e7c7639
parent2f81b51d0d02074502ad27424c228ca760823668
pwm: imx27: Fix overflow for bigger periods

The second parameter of do_div is an u32 and NSEC_PER_SEC * prescale
overflows this for bigger periods. Assuming the usual pwm input clk rate
of 66 MHz this happens starting at requested period > 606060 ns.

Splitting the division into two operations doesn't loose any precision.
It doesn't need to be feared that c / NSEC_PER_SEC doesn't fit into the
unsigned long variable "duty_cycles" because in this case the assignment
above to period_cycles would already have been overflowing as
period >= duty_cycle and then the calculation is moot anyhow.

Fixes: aef1a3799b5c ("pwm: imx27: Fix rounding behavior")
Signed-off-by: Uwe Kleine-König <uwe@kleine-koenig.org>
Tested-by: Johannes Pointner <johannes.pointner@br-automation.com>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
drivers/pwm/pwm-imx27.c