All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] pwm: lpss: Prevent on_time_div overflow on lower frequencies
@ 2016-06-10 12:43 Mika Westerberg
  2016-06-10 13:40 ` Thierry Reding
  0 siblings, 1 reply; 2+ messages in thread
From: Mika Westerberg @ 2016-06-10 12:43 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Dan O'Donovan, Mika Westerberg, linux-pwm

If duty_ns is large enough multiplying it by 255 overflows and results
wrong duty cycle value being programmed. For example with 10ms duty when
period is 20ms (50%) we get

  255 * 10000000 / 20000000 = -87

because 255 * 10000000 overlows int. Whereas correct value should be

  255 * 10000000 / 20000000 = 127

Fix this by using unsigned long long as type for on_time_div and changing
integer literals to use proper type annotation.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
---
Resending with linux-pwm address corrected.

This is on top of Dan's patch:

  https://patchwork.ozlabs.org/patch/628683/

 drivers/pwm/pwm-lpss.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/pwm-lpss.c b/drivers/pwm/pwm-lpss.c
index 98dc8b80b79d..be4658b42882 100644
--- a/drivers/pwm/pwm-lpss.c
+++ b/drivers/pwm/pwm-lpss.c
@@ -91,7 +91,7 @@ static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
 			   int duty_ns, int period_ns)
 {
 	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
-	u8 on_time_div;
+	unsigned long long on_time_div;
 	unsigned long c, base_unit_range;
 	unsigned long long base_unit, freq = NSEC_PER_SEC;
 	u32 ctrl;
@@ -113,7 +113,9 @@ static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	if (duty_ns <= 0)
 		duty_ns = 1;
-	on_time_div = 255 - (255 * duty_ns / period_ns);
+	on_time_div = 255ULL * duty_ns;
+	do_div(on_time_div, period_ns);
+	on_time_div = 255ULL - on_time_div;
 
 	pm_runtime_get_sync(chip->dev);
 
-- 
2.8.1

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

* Re: [PATCH] pwm: lpss: Prevent on_time_div overflow on lower frequencies
  2016-06-10 12:43 [PATCH] pwm: lpss: Prevent on_time_div overflow on lower frequencies Mika Westerberg
@ 2016-06-10 13:40 ` Thierry Reding
  0 siblings, 0 replies; 2+ messages in thread
From: Thierry Reding @ 2016-06-10 13:40 UTC (permalink / raw)
  To: Mika Westerberg; +Cc: Dan O'Donovan, linux-pwm

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

On Fri, Jun 10, 2016 at 03:43:21PM +0300, Mika Westerberg wrote:
> If duty_ns is large enough multiplying it by 255 overflows and results
> wrong duty cycle value being programmed. For example with 10ms duty when
> period is 20ms (50%) we get
> 
>   255 * 10000000 / 20000000 = -87
> 
> because 255 * 10000000 overlows int. Whereas correct value should be
> 
>   255 * 10000000 / 20000000 = 127
> 
> Fix this by using unsigned long long as type for on_time_div and changing
> integer literals to use proper type annotation.
> 
> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> ---
> Resending with linux-pwm address corrected.
> 
> This is on top of Dan's patch:
> 
>   https://patchwork.ozlabs.org/patch/628683/
> 
>  drivers/pwm/pwm-lpss.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)

Applied, thanks.

Thierry

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

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

end of thread, other threads:[~2016-06-10 13:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-10 12:43 [PATCH] pwm: lpss: Prevent on_time_div overflow on lower frequencies Mika Westerberg
2016-06-10 13:40 ` Thierry Reding

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.