public inbox for linux-pwm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] pwm: imx27: fix overflow for bigger periods
@ 2020-12-07 14:13 Uwe Kleine-König
  2020-12-07 15:20 ` Johannes Pointner
  2020-12-10 17:34 ` Thierry Reding
  0 siblings, 2 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2020-12-07 14:13 UTC (permalink / raw)
  To: Johannes Pointner, Thierry Reding, Lee Jones
  Cc: linux-pwm, kernel, Fabio Estevam, NXP Linux Team

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>
---
Hello,

for a similar patch I said "that looses more precision than I thought at
first", but I think this was wrong. And if it looses precision the same
applies to the calculation of period_cycles which uses the same
operations.

I'm a bit at doubt how urgent this patch is. The regression was
introduced in 5.8-rc1.

Best regards
Uwe

 drivers/pwm/pwm-imx27.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/pwm/pwm-imx27.c b/drivers/pwm/pwm-imx27.c
index c50d453552bd..86bcafd23e4f 100644
--- a/drivers/pwm/pwm-imx27.c
+++ b/drivers/pwm/pwm-imx27.c
@@ -235,8 +235,9 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	period_cycles /= prescale;
 	c = clkrate * state->duty_cycle;
-	do_div(c, NSEC_PER_SEC * prescale);
+	do_div(c, NSEC_PER_SEC);
 	duty_cycles = c;
+	duty_cycles /= prescale;
 
 	/*
 	 * according to imx pwm RM, the real period value should be PERIOD
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-10 17:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-07 14:13 [PATCH] pwm: imx27: fix overflow for bigger periods Uwe Kleine-König
2020-12-07 15:20 ` Johannes Pointner
2020-12-10 17:34 ` Thierry Reding

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox