All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] leds: qcom-lpg: Fix PWM period limits
@ 2023-05-12 16:55 Bjorn Andersson
  2023-05-12 22:05 ` Steev Klimaszewski
  2023-05-13 10:09 ` Caleb Connolly
  0 siblings, 2 replies; 5+ messages in thread
From: Bjorn Andersson @ 2023-05-12 16:55 UTC (permalink / raw)
  To: Pavel Machek, Lee Jones, Anjelique Melendez
  Cc: Uwe Kleine-König, Luca Weiss, linux-leds, linux-kernel,
	linux-arm-msm

The introduction of high resolution PWM support moved the parenthesis of
the divisions in the calculation of min and max period. The result in
both divisions is in most cases truncation to 0, which limits the period
to the range of [0, 0].

Both numerators (and denominators) are within 64 bits, so the whole
expression can be put directly into the div64_u64, instead of doing it
partially.

Fixes: b00d2ed37617 ("leds: rgb: leds-qcom-lpg: Add support for high resolution PWM")
Signed-off-by: Bjorn Andersson <quic_bjorande@quicinc.com>
---

This fixes the regression in v6.4-rc1.

 drivers/leds/rgb/leds-qcom-lpg.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
index c9cea797a697..7287fadc00df 100644
--- a/drivers/leds/rgb/leds-qcom-lpg.c
+++ b/drivers/leds/rgb/leds-qcom-lpg.c
@@ -312,14 +312,14 @@ static int lpg_calc_freq(struct lpg_channel *chan, uint64_t period)
 		max_res = LPG_RESOLUTION_9BIT;
 	}
 
-	min_period = (u64)NSEC_PER_SEC *
-			div64_u64((1 << pwm_resolution_arr[0]), clk_rate_arr[clk_len - 1]);
+	min_period = div64_u64((u64)NSEC_PER_SEC * (1 << pwm_resolution_arr[0]),
+			       clk_rate_arr[clk_len - 1]);
 	if (period <= min_period)
 		return -EINVAL;
 
 	/* Limit period to largest possible value, to avoid overflows */
-	max_period = (u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV *
-			div64_u64((1 << LPG_MAX_M), 1024);
+	max_period = div64_u64((u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV * (1 << LPG_MAX_M),
+			       1024);
 	if (period > max_period)
 		period = max_period;
 
-- 
2.25.1


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

* Re: [PATCH] leds: qcom-lpg: Fix PWM period limits
  2023-05-12 16:55 [PATCH] leds: qcom-lpg: Fix PWM period limits Bjorn Andersson
@ 2023-05-12 22:05 ` Steev Klimaszewski
  2023-05-13 10:09 ` Caleb Connolly
  1 sibling, 0 replies; 5+ messages in thread
From: Steev Klimaszewski @ 2023-05-12 22:05 UTC (permalink / raw)
  To: Bjorn Andersson
  Cc: Pavel Machek, Lee Jones, Anjelique Melendez,
	Uwe Kleine-König, Luca Weiss, linux-leds, linux-kernel,
	linux-arm-msm

On Fri, May 12, 2023 at 11:55 AM Bjorn Andersson
<quic_bjorande@quicinc.com> wrote:
>
> The introduction of high resolution PWM support moved the parenthesis of
> the divisions in the calculation of min and max period. The result in
> both divisions is in most cases truncation to 0, which limits the period
> to the range of [0, 0].
>
> Both numerators (and denominators) are within 64 bits, so the whole
> expression can be put directly into the div64_u64, instead of doing it
> partially.
>
> Fixes: b00d2ed37617 ("leds: rgb: leds-qcom-lpg: Add support for high resolution PWM")
> Signed-off-by: Bjorn Andersson <quic_bjorande@quicinc.com>
> ---
>
> This fixes the regression in v6.4-rc1.
>
>  drivers/leds/rgb/leds-qcom-lpg.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
> index c9cea797a697..7287fadc00df 100644
> --- a/drivers/leds/rgb/leds-qcom-lpg.c
> +++ b/drivers/leds/rgb/leds-qcom-lpg.c
> @@ -312,14 +312,14 @@ static int lpg_calc_freq(struct lpg_channel *chan, uint64_t period)
>                 max_res = LPG_RESOLUTION_9BIT;
>         }
>
> -       min_period = (u64)NSEC_PER_SEC *
> -                       div64_u64((1 << pwm_resolution_arr[0]), clk_rate_arr[clk_len - 1]);
> +       min_period = div64_u64((u64)NSEC_PER_SEC * (1 << pwm_resolution_arr[0]),
> +                              clk_rate_arr[clk_len - 1]);
>         if (period <= min_period)
>                 return -EINVAL;
>
>         /* Limit period to largest possible value, to avoid overflows */
> -       max_period = (u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV *
> -                       div64_u64((1 << LPG_MAX_M), 1024);
> +       max_period = div64_u64((u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV * (1 << LPG_MAX_M),
> +                              1024);
>         if (period > max_period)
>                 period = max_period;
>
> --
> 2.25.1
>
Thank you!
Without this fix, the display never activates on the Thinkpad X13s
Tested on the Lenovo Thinkpad X13s
Tested-by: Steev Klimaszewski <steev@kali.org>

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

* Re: [PATCH] leds: qcom-lpg: Fix PWM period limits
  2023-05-12 16:55 [PATCH] leds: qcom-lpg: Fix PWM period limits Bjorn Andersson
  2023-05-12 22:05 ` Steev Klimaszewski
@ 2023-05-13 10:09 ` Caleb Connolly
  2023-05-15  2:20   ` Bjorn Andersson
  1 sibling, 1 reply; 5+ messages in thread
From: Caleb Connolly @ 2023-05-13 10:09 UTC (permalink / raw)
  To: Bjorn Andersson, Pavel Machek, Lee Jones, Anjelique Melendez
  Cc: Uwe Kleine-König, Luca Weiss, linux-leds, linux-kernel,
	linux-arm-msm



On 12/05/2023 16:55, Bjorn Andersson wrote:
> The introduction of high resolution PWM support moved the parenthesis of
> the divisions in the calculation of min and max period. The result in
> both divisions is in most cases truncation to 0, which limits the period
> to the range of [0, 0].

Huh, TIL C gives multiplication and division the same precedence when
deciding order of operations.
> 
> Both numerators (and denominators) are within 64 bits, so the whole
> expression can be put directly into the div64_u64, instead of doing it
> partially.
> 
> Fixes: b00d2ed37617 ("leds: rgb: leds-qcom-lpg: Add support for high resolution PWM")
> Signed-off-by: Bjorn Andersson <quic_bjorande@quicinc.com>

Reviewed-by: Caleb Connolly <caleb.connolly@linaro.org>
> ---
> 
> This fixes the regression in v6.4-rc1.
> 
>  drivers/leds/rgb/leds-qcom-lpg.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
> index c9cea797a697..7287fadc00df 100644
> --- a/drivers/leds/rgb/leds-qcom-lpg.c
> +++ b/drivers/leds/rgb/leds-qcom-lpg.c
> @@ -312,14 +312,14 @@ static int lpg_calc_freq(struct lpg_channel *chan, uint64_t period)
>  		max_res = LPG_RESOLUTION_9BIT;
>  	}
>  
> -	min_period = (u64)NSEC_PER_SEC *
> -			div64_u64((1 << pwm_resolution_arr[0]), clk_rate_arr[clk_len - 1]);
> +	min_period = div64_u64((u64)NSEC_PER_SEC * (1 << pwm_resolution_arr[0]),
> +			       clk_rate_arr[clk_len - 1]);
>  	if (period <= min_period)
>  		return -EINVAL;
>  
>  	/* Limit period to largest possible value, to avoid overflows */
> -	max_period = (u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV *
> -			div64_u64((1 << LPG_MAX_M), 1024);
> +	max_period = div64_u64((u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV * (1 << LPG_MAX_M),
> +			       1024);
>  	if (period > max_period)
>  		period = max_period;
>  

-- 
Kind Regards,
Caleb (they/them)

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

* Re: [PATCH] leds: qcom-lpg: Fix PWM period limits
  2023-05-13 10:09 ` Caleb Connolly
@ 2023-05-15  2:20   ` Bjorn Andersson
  2023-05-15 15:39     ` Caleb Connolly
  0 siblings, 1 reply; 5+ messages in thread
From: Bjorn Andersson @ 2023-05-15  2:20 UTC (permalink / raw)
  To: Caleb Connolly
  Cc: Bjorn Andersson, Pavel Machek, Lee Jones, Anjelique Melendez,
	Uwe Kleine-König, Luca Weiss, linux-leds, linux-kernel,
	linux-arm-msm

On Sat, May 13, 2023 at 10:09:49AM +0000, Caleb Connolly wrote:
> 
> 
> On 12/05/2023 16:55, Bjorn Andersson wrote:
> > The introduction of high resolution PWM support moved the parenthesis of
> > the divisions in the calculation of min and max period. The result in
> > both divisions is in most cases truncation to 0, which limits the period
> > to the range of [0, 0].
> 
> Huh, TIL C gives multiplication and division the same precedence when
> deciding order of operations.

There where no explicit parenthesis in the original implementation. So
I guess it would be more correct to state that parenthesis was
introduced around part of the expression?

Let me know if you think the wording matters and you would prefer me to
rewrite it.

Regards,
Bjorn

> > 
> > Both numerators (and denominators) are within 64 bits, so the whole
> > expression can be put directly into the div64_u64, instead of doing it
> > partially.
> > 
> > Fixes: b00d2ed37617 ("leds: rgb: leds-qcom-lpg: Add support for high resolution PWM")
> > Signed-off-by: Bjorn Andersson <quic_bjorande@quicinc.com>
> 
> Reviewed-by: Caleb Connolly <caleb.connolly@linaro.org>
> > ---
> > 
> > This fixes the regression in v6.4-rc1.
> > 
> >  drivers/leds/rgb/leds-qcom-lpg.c | 8 ++++----
> >  1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
> > index c9cea797a697..7287fadc00df 100644
> > --- a/drivers/leds/rgb/leds-qcom-lpg.c
> > +++ b/drivers/leds/rgb/leds-qcom-lpg.c
> > @@ -312,14 +312,14 @@ static int lpg_calc_freq(struct lpg_channel *chan, uint64_t period)
> >  		max_res = LPG_RESOLUTION_9BIT;
> >  	}
> >  
> > -	min_period = (u64)NSEC_PER_SEC *
> > -			div64_u64((1 << pwm_resolution_arr[0]), clk_rate_arr[clk_len - 1]);
> > +	min_period = div64_u64((u64)NSEC_PER_SEC * (1 << pwm_resolution_arr[0]),
> > +			       clk_rate_arr[clk_len - 1]);
> >  	if (period <= min_period)
> >  		return -EINVAL;
> >  
> >  	/* Limit period to largest possible value, to avoid overflows */
> > -	max_period = (u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV *
> > -			div64_u64((1 << LPG_MAX_M), 1024);
> > +	max_period = div64_u64((u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV * (1 << LPG_MAX_M),
> > +			       1024);
> >  	if (period > max_period)
> >  		period = max_period;
> >  
> 
> -- 
> Kind Regards,
> Caleb (they/them)

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

* Re: [PATCH] leds: qcom-lpg: Fix PWM period limits
  2023-05-15  2:20   ` Bjorn Andersson
@ 2023-05-15 15:39     ` Caleb Connolly
  0 siblings, 0 replies; 5+ messages in thread
From: Caleb Connolly @ 2023-05-15 15:39 UTC (permalink / raw)
  To: Bjorn Andersson
  Cc: Bjorn Andersson, Pavel Machek, Lee Jones, Anjelique Melendez,
	Uwe Kleine-König, Luca Weiss, linux-leds, linux-kernel,
	linux-arm-msm



On 15/05/2023 02:20, Bjorn Andersson wrote:
> On Sat, May 13, 2023 at 10:09:49AM +0000, Caleb Connolly wrote:
>>
>>
>> On 12/05/2023 16:55, Bjorn Andersson wrote:
>>> The introduction of high resolution PWM support moved the parenthesis of
>>> the divisions in the calculation of min and max period. The result in
>>> both divisions is in most cases truncation to 0, which limits the period
>>> to the range of [0, 0].
>>
>> Huh, TIL C gives multiplication and division the same precedence when
>> deciding order of operations.
> 
> There where no explicit parenthesis in the original implementation. So
> I guess it would be more correct to state that parenthesis was
> introduced around part of the expression?

It might be clearer just to say the div64_u64 changed the order of
operations.
> 
> Let me know if you think the wording matters and you would prefer me to
> rewrite it.

Yeah, that would be appreciated

Thanks
> 
> Regards,
> Bjorn
> 
-- 
Kind Regards,
Caleb (they/them)

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

end of thread, other threads:[~2023-05-15 15:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-12 16:55 [PATCH] leds: qcom-lpg: Fix PWM period limits Bjorn Andersson
2023-05-12 22:05 ` Steev Klimaszewski
2023-05-13 10:09 ` Caleb Connolly
2023-05-15  2:20   ` Bjorn Andersson
2023-05-15 15:39     ` Caleb Connolly

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.