diff mbox series

[1/5] gpio: mvebu: fix pwm get_state period calculation

Message ID e3afc6e297e495322971c26a79c6f841d5952fd1.1610362661.git.baruch@tkos.co.il (mailing list archive)
State New, archived
Headers show
Series gpio: mvebu: pwm fixes and improvements | expand

Commit Message

Baruch Siach Jan. 11, 2021, 11:17 a.m. UTC
The period is the sum of on and off values.

Reported-by: Russell King <linux@armlinux.org.uk>
Fixes: 757642f9a584e ("gpio: mvebu: Add limited PWM support")
Signed-off-by: Baruch Siach <baruch@tkos.co.il>
---
 drivers/gpio/gpio-mvebu.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

Comments

Uwe Kleine-König Jan. 11, 2021, 8:17 p.m. UTC | #1
Hello Baruch,

$Subject ~= s/get_state/.get_state/ ?

On Mon, Jan 11, 2021 at 01:17:02PM +0200, Baruch Siach wrote:
> The period is the sum of on and off values.
> 
> Reported-by: Russell King <linux@armlinux.org.uk>
> Fixes: 757642f9a584e ("gpio: mvebu: Add limited PWM support")
> Signed-off-by: Baruch Siach <baruch@tkos.co.il>
> ---
>  drivers/gpio/gpio-mvebu.c | 19 ++++++++-----------
>  1 file changed, 8 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/gpio/gpio-mvebu.c b/drivers/gpio/gpio-mvebu.c
> index 672681a976f5..a912a8fed197 100644
> --- a/drivers/gpio/gpio-mvebu.c
> +++ b/drivers/gpio/gpio-mvebu.c
> @@ -676,20 +676,17 @@ static void mvebu_pwm_get_state(struct pwm_chip *chip,
>  	else
>  		state->duty_cycle = 1;
>  
> +	val = (unsigned long long) u; /* on duration */
>  	regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), &u);
> -	val = (unsigned long long) u * NSEC_PER_SEC;
> +	val += (unsigned long long) u; /* period = on + off duration */
> +	val *= NSEC_PER_SEC;
>  	do_div(val, mvpwm->clk_rate);
> -	if (val < state->duty_cycle) {
> +	if (val > UINT_MAX)
> +		state->period = UINT_MAX;
> +	else if (val)
> +		state->period = val;
> +	else
>  		state->period = 1;
> -	} else {
> -		val -= state->duty_cycle;
> -		if (val > UINT_MAX)
> -			state->period = UINT_MAX;
> -		else if (val)
> -			state->period = val;
> -		else
> -			state->period = 1;
> -	}

The patch looks good, the patch description could be a bit more verbose.
Something like:

	Calculate the period as

		($on + $off) / clkrate

	instead of

		$off / clkrate - $on / clkrate

	.

Best regards
Uwe
Baruch Siach Jan. 13, 2021, 6:36 a.m. UTC | #2
Hi Uwe,

On Mon, Jan 11 2021, Uwe Kleine-König wrote:
> $Subject ~= s/get_state/.get_state/ ?

Ack.

> On Mon, Jan 11, 2021 at 01:17:02PM +0200, Baruch Siach wrote:
>> The period is the sum of on and off values.
>> 
>> Reported-by: Russell King <linux@armlinux.org.uk>
>> Fixes: 757642f9a584e ("gpio: mvebu: Add limited PWM support")
>> Signed-off-by: Baruch Siach <baruch@tkos.co.il>
>> ---
>>  drivers/gpio/gpio-mvebu.c | 19 ++++++++-----------
>>  1 file changed, 8 insertions(+), 11 deletions(-)
>> 
>> diff --git a/drivers/gpio/gpio-mvebu.c b/drivers/gpio/gpio-mvebu.c
>> index 672681a976f5..a912a8fed197 100644
>> --- a/drivers/gpio/gpio-mvebu.c
>> +++ b/drivers/gpio/gpio-mvebu.c
>> @@ -676,20 +676,17 @@ static void mvebu_pwm_get_state(struct pwm_chip *chip,
>>  	else
>>  		state->duty_cycle = 1;
>>  
>> +	val = (unsigned long long) u; /* on duration */
>>  	regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), &u);
>> -	val = (unsigned long long) u * NSEC_PER_SEC;
>> +	val += (unsigned long long) u; /* period = on + off duration */
>> +	val *= NSEC_PER_SEC;
>>  	do_div(val, mvpwm->clk_rate);
>> -	if (val < state->duty_cycle) {
>> +	if (val > UINT_MAX)
>> +		state->period = UINT_MAX;
>> +	else if (val)
>> +		state->period = val;
>> +	else
>>  		state->period = 1;
>> -	} else {
>> -		val -= state->duty_cycle;
>> -		if (val > UINT_MAX)
>> -			state->period = UINT_MAX;
>> -		else if (val)
>> -			state->period = val;
>> -		else
>> -			state->period = 1;
>> -	}
>
> The patch looks good, the patch description could be a bit more verbose.
> Something like:
>
> 	Calculate the period as
>
> 		($on + $off) / clkrate
>
> 	instead of
>
> 		$off / clkrate - $on / clkrate
>
> 	.

I take this to refer to the next patch (2/5). This patch changes from
buggy

  $on / clkrate

to

  ($on + $off) / clkrate

baruch
Uwe Kleine-König Jan. 13, 2021, 8:17 a.m. UTC | #3
On Wed, Jan 13, 2021 at 08:36:12AM +0200, Baruch Siach wrote:
> Hi Uwe,
> 
> On Mon, Jan 11 2021, Uwe Kleine-König wrote:
> > $Subject ~= s/get_state/.get_state/ ?
> 
> Ack.
> 
> > On Mon, Jan 11, 2021 at 01:17:02PM +0200, Baruch Siach wrote:
> >> The period is the sum of on and off values.
> >> 
> >> Reported-by: Russell King <linux@armlinux.org.uk>
> >> Fixes: 757642f9a584e ("gpio: mvebu: Add limited PWM support")
> >> Signed-off-by: Baruch Siach <baruch@tkos.co.il>
> >> ---
> >>  drivers/gpio/gpio-mvebu.c | 19 ++++++++-----------
> >>  1 file changed, 8 insertions(+), 11 deletions(-)
> >> 
> >> diff --git a/drivers/gpio/gpio-mvebu.c b/drivers/gpio/gpio-mvebu.c
> >> index 672681a976f5..a912a8fed197 100644
> >> --- a/drivers/gpio/gpio-mvebu.c
> >> +++ b/drivers/gpio/gpio-mvebu.c
> >> @@ -676,20 +676,17 @@ static void mvebu_pwm_get_state(struct pwm_chip *chip,
> >>  	else
> >>  		state->duty_cycle = 1;
> >>  
> >> +	val = (unsigned long long) u; /* on duration */
> >>  	regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), &u);
> >> -	val = (unsigned long long) u * NSEC_PER_SEC;
> >> +	val += (unsigned long long) u; /* period = on + off duration */
> >> +	val *= NSEC_PER_SEC;
> >>  	do_div(val, mvpwm->clk_rate);
> >> -	if (val < state->duty_cycle) {
> >> +	if (val > UINT_MAX)
> >> +		state->period = UINT_MAX;
> >> +	else if (val)
> >> +		state->period = val;
> >> +	else
> >>  		state->period = 1;
> >> -	} else {
> >> -		val -= state->duty_cycle;
> >> -		if (val > UINT_MAX)
> >> -			state->period = UINT_MAX;
> >> -		else if (val)
> >> -			state->period = val;
> >> -		else
> >> -			state->period = 1;
> >> -	}
> >
> > The patch looks good, the patch description could be a bit more verbose.
> > Something like:
> >
> > 	Calculate the period as
> >
> > 		($on + $off) / clkrate
> >
> > 	instead of
> >
> > 		$off / clkrate - $on / clkrate
> >
> > 	.
> 
> I take this to refer to the next patch (2/5). This patch changes from
> buggy
> 
>   $on / clkrate

No, the previous calculation had

-		val -= state->duty_cycle;

which accounts for "- $on / clkrate".

Best regards
Uwe
diff mbox series

Patch

diff --git a/drivers/gpio/gpio-mvebu.c b/drivers/gpio/gpio-mvebu.c
index 672681a976f5..a912a8fed197 100644
--- a/drivers/gpio/gpio-mvebu.c
+++ b/drivers/gpio/gpio-mvebu.c
@@ -676,20 +676,17 @@  static void mvebu_pwm_get_state(struct pwm_chip *chip,
 	else
 		state->duty_cycle = 1;
 
+	val = (unsigned long long) u; /* on duration */
 	regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), &u);
-	val = (unsigned long long) u * NSEC_PER_SEC;
+	val += (unsigned long long) u; /* period = on + off duration */
+	val *= NSEC_PER_SEC;
 	do_div(val, mvpwm->clk_rate);
-	if (val < state->duty_cycle) {
+	if (val > UINT_MAX)
+		state->period = UINT_MAX;
+	else if (val)
+		state->period = val;
+	else
 		state->period = 1;
-	} else {
-		val -= state->duty_cycle;
-		if (val > UINT_MAX)
-			state->period = UINT_MAX;
-		else if (val)
-			state->period = val;
-		else
-			state->period = 1;
-	}
 
 	regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &u);
 	if (u)