All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] leds: leds-pwm: fix duty time overflow.
@ 2013-12-11  9:19 ` Xiubo Li
  0 siblings, 0 replies; 3+ messages in thread
From: Xiubo Li @ 2013-12-11  9:19 UTC (permalink / raw)
  To: cooloney, rpurdie, linux-leds, linux-kernel, b47053

Overflow maybe occurs when calculates the duty time. For instance,
the period time is 990000000ns, and the max_brightness is 127, when
setting the brightness to 12, the duty value will be 25906026ns, but
it should be 93543307ns.
---
 drivers/leds/leds-pwm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
index bb6f948..7cb2cd8 100644
--- a/drivers/leds/leds-pwm.c
+++ b/drivers/leds/leds-pwm.c
@@ -66,9 +66,11 @@ static void led_pwm_set(struct led_classdev *led_cdev,
 	struct led_pwm_data *led_dat =
 		container_of(led_cdev, struct led_pwm_data, cdev);
 	unsigned int max = led_dat->cdev.max_brightness;
-	unsigned int period =  led_dat->period;
+	unsigned long long duty =  led_dat->period;
 
-	led_dat->duty = brightness * period / max;
+	duty *= brightness;
+	do_div(duty, max);
+	led_dat->duty = duty;
 
 	if (led_dat->can_sleep)
 		schedule_work(&led_dat->work);
-- 
1.8.4

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

* [PATCH] leds: leds-pwm: fix duty time overflow.
@ 2013-12-11  9:19 ` Xiubo Li
  0 siblings, 0 replies; 3+ messages in thread
From: Xiubo Li @ 2013-12-11  9:19 UTC (permalink / raw)
  To: cooloney, rpurdie, linux-leds, linux-kernel, b47053

Overflow maybe occurs when calculates the duty time. For instance,
the period time is 990000000ns, and the max_brightness is 127, when
setting the brightness to 12, the duty value will be 25906026ns, but
it should be 93543307ns.
---
 drivers/leds/leds-pwm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
index bb6f948..7cb2cd8 100644
--- a/drivers/leds/leds-pwm.c
+++ b/drivers/leds/leds-pwm.c
@@ -66,9 +66,11 @@ static void led_pwm_set(struct led_classdev *led_cdev,
 	struct led_pwm_data *led_dat =
 		container_of(led_cdev, struct led_pwm_data, cdev);
 	unsigned int max = led_dat->cdev.max_brightness;
-	unsigned int period =  led_dat->period;
+	unsigned long long duty =  led_dat->period;
 
-	led_dat->duty = brightness * period / max;
+	duty *= brightness;
+	do_div(duty, max);
+	led_dat->duty = duty;
 
 	if (led_dat->can_sleep)
 		schedule_work(&led_dat->work);
-- 
1.8.4



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

* Re: [PATCH] leds: leds-pwm: fix duty time overflow.
  2013-12-11  9:19 ` Xiubo Li
  (?)
@ 2013-12-11 19:07 ` Bryan Wu
  -1 siblings, 0 replies; 3+ messages in thread
From: Bryan Wu @ 2013-12-11 19:07 UTC (permalink / raw)
  To: Xiubo Li; +Cc: rpurdie, Linux LED Subsystem, lkml, b47053

On Wed, Dec 11, 2013 at 1:19 AM, Xiubo Li <Li.Xiubo@freescale.com> wrote:
> Overflow maybe occurs when calculates the duty time. For instance,
> the period time is 990000000ns, and the max_brightness is 127, when
> setting the brightness to 12, the duty value will be 25906026ns, but
> it should be 93543307ns.

This looks good to me. Good catch. I will merge this.

Thanks,
-Bryan


> ---
>  drivers/leds/leds-pwm.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
> index bb6f948..7cb2cd8 100644
> --- a/drivers/leds/leds-pwm.c
> +++ b/drivers/leds/leds-pwm.c
> @@ -66,9 +66,11 @@ static void led_pwm_set(struct led_classdev *led_cdev,
>         struct led_pwm_data *led_dat =
>                 container_of(led_cdev, struct led_pwm_data, cdev);
>         unsigned int max = led_dat->cdev.max_brightness;
> -       unsigned int period =  led_dat->period;
> +       unsigned long long duty =  led_dat->period;
>
> -       led_dat->duty = brightness * period / max;
> +       duty *= brightness;
> +       do_div(duty, max);
> +       led_dat->duty = duty;
>
>         if (led_dat->can_sleep)
>                 schedule_work(&led_dat->work);
> --
> 1.8.4
>
>

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

end of thread, other threads:[~2013-12-11 19:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-11  9:19 [PATCH] leds: leds-pwm: fix duty time overflow Xiubo Li
2013-12-11  9:19 ` Xiubo Li
2013-12-11 19:07 ` Bryan Wu

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.