linux-pwm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pwm: imx27: Fix rounding behavior
@ 2020-04-16  8:02 Uwe Kleine-König
  2020-06-02 12:48 ` Thierry Reding
  0 siblings, 1 reply; 4+ messages in thread
From: Uwe Kleine-König @ 2020-04-16  8:02 UTC (permalink / raw)
  To: Thierry Reding
  Cc: linux-pwm, Shawn Guo, Sascha Hauer, NXP Linux Team,
	Pengutronix Kernel Team, Fabio Estevam, linux-arm-kernel

To not trigger the warnings provided by CONFIG_PWM_DEBUG

 - use up-rounding in .get_state()
 - don't divide by the result of a division
 - don't use the rounded counter value for the period length to calculate
   the counter value for the duty cycle

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/pwm/pwm-imx27.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/drivers/pwm/pwm-imx27.c b/drivers/pwm/pwm-imx27.c
index a6e40d4c485f..732a6f3701e8 100644
--- a/drivers/pwm/pwm-imx27.c
+++ b/drivers/pwm/pwm-imx27.c
@@ -150,13 +150,12 @@ static void pwm_imx27_get_state(struct pwm_chip *chip,
 
 	prescaler = MX3_PWMCR_PRESCALER_GET(val);
 	pwm_clk = clk_get_rate(imx->clk_per);
-	pwm_clk = DIV_ROUND_CLOSEST_ULL(pwm_clk, prescaler);
 	val = readl(imx->mmio_base + MX3_PWMPR);
 	period = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
 
 	/* PWMOUT (Hz) = PWMCLK / (PWMPR + 2) */
-	tmp = NSEC_PER_SEC * (u64)(period + 2);
-	state->period = DIV_ROUND_CLOSEST_ULL(tmp, pwm_clk);
+	tmp = NSEC_PER_SEC * (u64)(period + 2) * prescaler;
+	state->period = DIV_ROUND_UP_ULL(tmp, pwm_clk);
 
 	/*
 	 * PWMSAR can be read only if PWM is enabled. If the PWM is disabled,
@@ -167,8 +166,8 @@ static void pwm_imx27_get_state(struct pwm_chip *chip,
 	else
 		val = imx->duty_cycle;
 
-	tmp = NSEC_PER_SEC * (u64)(val);
-	state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, pwm_clk);
+	tmp = NSEC_PER_SEC * (u64)(val) * prescaler;
+	state->duty_cycle = DIV_ROUND_UP_ULL(tmp, pwm_clk);
 
 	pwm_imx27_clk_disable_unprepare(imx);
 }
@@ -220,22 +219,23 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
 	struct pwm_state cstate;
 	unsigned long long c;
+	unsigned long long clkrate;
 	int ret;
 	u32 cr;
 
 	pwm_get_state(pwm, &cstate);
 
-	c = clk_get_rate(imx->clk_per);
-	c *= state->period;
+	clkrate = clk_get_rate(imx->clk_per);
+	c = clkrate * state->period;
 
-	do_div(c, 1000000000);
+	do_div(c, NSEC_PER_SEC);
 	period_cycles = c;
 
 	prescale = period_cycles / 0x10000 + 1;
 
 	period_cycles /= prescale;
-	c = (unsigned long long)period_cycles * state->duty_cycle;
-	do_div(c, state->period);
+	c = clkrate * state->duty_cycle;
+	do_div(c, NSEC_PER_SEC * prescale);
 	duty_cycles = c;
 
 	/*
-- 
2.26.0.rc2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH] pwm: imx27: Fix rounding behavior
  2020-04-16  8:02 [PATCH] pwm: imx27: Fix rounding behavior Uwe Kleine-König
@ 2020-06-02 12:48 ` Thierry Reding
  2020-06-02 20:42   ` Guru Das Srinagesh
  0 siblings, 1 reply; 4+ messages in thread
From: Thierry Reding @ 2020-06-02 12:48 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Shawn Guo, Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam,
	NXP Linux Team, linux-pwm, linux-arm-kernel

[-- Attachment #1: Type: text/plain, Size: 556 bytes --]

On Thu, Apr 16, 2020 at 10:02:45AM +0200, Uwe Kleine-König wrote:
> To not trigger the warnings provided by CONFIG_PWM_DEBUG
> 
>  - use up-rounding in .get_state()
>  - don't divide by the result of a division
>  - don't use the rounded counter value for the period length to calculate
>    the counter value for the duty cycle
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  drivers/pwm/pwm-imx27.c | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)

Applied, thanks.

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] pwm: imx27: Fix rounding behavior
  2020-06-02 12:48 ` Thierry Reding
@ 2020-06-02 20:42   ` Guru Das Srinagesh
  2020-06-02 22:36     ` Guru Das Srinagesh
  0 siblings, 1 reply; 4+ messages in thread
From: Guru Das Srinagesh @ 2020-06-02 20:42 UTC (permalink / raw)
  To: Thierry Reding
  Cc: linux-pwm, Shawn Guo, Sascha Hauer, NXP Linux Team,
	Pengutronix Kernel Team, Uwe Kleine-König, Fabio Estevam,
	linux-arm-kernel

On Tue, Jun 02, 2020 at 02:48:35PM +0200, Thierry Reding wrote:
> On Thu, Apr 16, 2020 at 10:02:45AM +0200, Uwe Kleine-König wrote:
> > To not trigger the warnings provided by CONFIG_PWM_DEBUG
> > 
> >  - use up-rounding in .get_state()
> >  - don't divide by the result of a division
> >  - don't use the rounded counter value for the period length to calculate
> >    the counter value for the duty cycle
> > 
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > ---
> >  drivers/pwm/pwm-imx27.c | 20 ++++++++++----------
> >  1 file changed, 10 insertions(+), 10 deletions(-)
> 
> Applied, thanks.
> 
> Thierry

Hi Thierry,

Just FYI, This change conflicts with one of my patches [1] in the "Convert
PWM period and duty cycle to u64" series.

[1]: https://patchwork.ozlabs.org/project/linux-pwm/patch/848494725fd1240ed877d0a1471dd11ccea01ff5.1590514331.git.gurus@codeaurora.org/

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH] pwm: imx27: Fix rounding behavior
  2020-06-02 20:42   ` Guru Das Srinagesh
@ 2020-06-02 22:36     ` Guru Das Srinagesh
  0 siblings, 0 replies; 4+ messages in thread
From: Guru Das Srinagesh @ 2020-06-02 22:36 UTC (permalink / raw)
  To: Thierry Reding
  Cc: Uwe Kleine-König, Shawn Guo, Sascha Hauer,
	Pengutronix Kernel Team, Fabio Estevam, NXP Linux Team, linux-pwm,
	linux-arm-kernel

On Tue, Jun 02, 2020 at 01:42:12PM -0700, Guru Das Srinagesh wrote:
> On Tue, Jun 02, 2020 at 02:48:35PM +0200, Thierry Reding wrote:
> > On Thu, Apr 16, 2020 at 10:02:45AM +0200, Uwe Kleine-König wrote:
> > > To not trigger the warnings provided by CONFIG_PWM_DEBUG
> > > 
> > >  - use up-rounding in .get_state()
> > >  - don't divide by the result of a division
> > >  - don't use the rounded counter value for the period length to calculate
> > >    the counter value for the duty cycle
> > > 
> > > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > > ---
> > >  drivers/pwm/pwm-imx27.c | 20 ++++++++++----------
> > >  1 file changed, 10 insertions(+), 10 deletions(-)
> > 
> > Applied, thanks.
> > 
> > Thierry
> 
> Hi Thierry,
> 
> Just FYI, This change conflicts with one of my patches [1] in the "Convert
> PWM period and duty cycle to u64" series.
> 
> [1]: https://patchwork.ozlabs.org/project/linux-pwm/patch/848494725fd1240ed877d0a1471dd11ccea01ff5.1590514331.git.gurus@codeaurora.org/

Uploaded v16 that resolves this issue.

Thank you.

Guru Das.

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

end of thread, other threads:[~2020-06-02 22:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-16  8:02 [PATCH] pwm: imx27: Fix rounding behavior Uwe Kleine-König
2020-06-02 12:48 ` Thierry Reding
2020-06-02 20:42   ` Guru Das Srinagesh
2020-06-02 22:36     ` Guru Das Srinagesh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).