Linux-Amlogic Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] pwm: meson: modify and simplify calculation in meson_pwm_get_state
@ 2023-05-01 14:03 Heiner Kallweit
  2023-05-01 18:07 ` Uwe Kleine-König
  0 siblings, 1 reply; 3+ messages in thread
From: Heiner Kallweit @ 2023-05-01 14:03 UTC (permalink / raw)
  To: Jerome Brunet, Martin Blumenstingl, Neil Armstrong, Kevin Hilman,
	Uwe Kleine-König, thierry.reding@gmail.com
  Cc: linux-arm-kernel@lists.infradead.org,
	open list:ARM/Amlogic Meson..., linux-pwm, Dmitry Rokosov

I don't see a reason why we should treat the case lo < hi differently
and return 0 as period and duty_cycle. The current logic was added with
c375bcbaabdb ("pwm: meson: Read the full hardware state in
meson_pwm_get_state()"), Martin as original author doesn't remember why
it was implemented this way back then.
So let's handle it as normal use case and also remove the optimization
for lo == 0. I think the improved readability is worth it.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
v2:
- improve commit description
---
 drivers/pwm/pwm-meson.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
index 5732300eb..3865538dd 100644
--- a/drivers/pwm/pwm-meson.c
+++ b/drivers/pwm/pwm-meson.c
@@ -351,18 +351,8 @@ static int meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	channel->lo = FIELD_GET(PWM_LOW_MASK, value);
 	channel->hi = FIELD_GET(PWM_HIGH_MASK, value);
 
-	if (channel->lo == 0) {
-		state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);
-		state->duty_cycle = state->period;
-	} else if (channel->lo >= channel->hi) {
-		state->period = meson_pwm_cnt_to_ns(chip, pwm,
-						    channel->lo + channel->hi);
-		state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm,
-							channel->hi);
-	} else {
-		state->period = 0;
-		state->duty_cycle = 0;
-	}
+	state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->lo + channel->hi);
+	state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);
 
 	state->polarity = PWM_POLARITY_NORMAL;
 
-- 
2.40.0

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

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

* Re: [PATCH v2] pwm: meson: modify and simplify calculation in meson_pwm_get_state
  2023-05-01 14:03 [PATCH v2] pwm: meson: modify and simplify calculation in meson_pwm_get_state Heiner Kallweit
@ 2023-05-01 18:07 ` Uwe Kleine-König
  2023-05-02 20:27   ` Heiner Kallweit
  0 siblings, 1 reply; 3+ messages in thread
From: Uwe Kleine-König @ 2023-05-01 18:07 UTC (permalink / raw)
  To: Heiner Kallweit
  Cc: Jerome Brunet, Martin Blumenstingl, Neil Armstrong, Kevin Hilman,
	thierry.reding@gmail.com, linux-arm-kernel@lists.infradead.org,
	open list:ARM/Amlogic Meson..., linux-pwm, Dmitry Rokosov


[-- Attachment #1.1: Type: text/plain, Size: 2858 bytes --]

On Mon, May 01, 2023 at 04:03:16PM +0200, Heiner Kallweit wrote:
> I don't see a reason why we should treat the case lo < hi differently
> and return 0 as period and duty_cycle. The current logic was added with
> c375bcbaabdb ("pwm: meson: Read the full hardware state in
> meson_pwm_get_state()"), Martin as original author doesn't remember why
> it was implemented this way back then.
> So let's handle it as normal use case and also remove the optimization
> for lo == 0. I think the improved readability is worth it.
> 
> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> ---
> v2:
> - improve commit description
> ---
>  drivers/pwm/pwm-meson.c | 14 ++------------
>  1 file changed, 2 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
> index 5732300eb..3865538dd 100644
> --- a/drivers/pwm/pwm-meson.c
> +++ b/drivers/pwm/pwm-meson.c
> @@ -351,18 +351,8 @@ static int meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
>  	channel->lo = FIELD_GET(PWM_LOW_MASK, value);
>  	channel->hi = FIELD_GET(PWM_HIGH_MASK, value);
>  
> -	if (channel->lo == 0) {
> -		state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);
> -		state->duty_cycle = state->period;
> -	} else if (channel->lo >= channel->hi) {
> -		state->period = meson_pwm_cnt_to_ns(chip, pwm,
> -						    channel->lo + channel->hi);
> -		state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm,
> -							channel->hi);
> -	} else {
> -		state->period = 0;
> -		state->duty_cycle = 0;
> -	}

The last else branch is even wrong, isn't it? .apply() can for a greater
than 50% relative duty cycle well have lo < hi, right? So this is not a
mere optimisation but a fix?!

> +	state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->lo + channel->hi);
> +	state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);

Note that meson_pwm_calc() has a similar construct that can be
simplified in a similar way. All three variants have

	channel->pre_div = pre_div;

and the last else branch is universal and can replace the others.

Another issue I just spotted is that

	duty = state->duty_cycle

is wrong for state->duty_cycle > UINT_MAX. (Ditto the assignment to
period.) Making both duty and period u64 shoudl fix that. After that
duty_cnt > 0xffff cannot happen as the core ensures that duty_cycle <=
period.

Having said that, the proposed change here is an improvement, so:

Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

I also suggest to add a Fixes line, i.e.

Fixes: c375bcbaabdb ("pwm: meson: Read the full hardware state in meson_pwm_get_state()")

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

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

[-- Attachment #2: Type: text/plain, Size: 167 bytes --]

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

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

* Re: [PATCH v2] pwm: meson: modify and simplify calculation in meson_pwm_get_state
  2023-05-01 18:07 ` Uwe Kleine-König
@ 2023-05-02 20:27   ` Heiner Kallweit
  0 siblings, 0 replies; 3+ messages in thread
From: Heiner Kallweit @ 2023-05-02 20:27 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Jerome Brunet, Martin Blumenstingl, Neil Armstrong, Kevin Hilman,
	thierry.reding@gmail.com, linux-arm-kernel@lists.infradead.org,
	open list:ARM/Amlogic Meson..., linux-pwm, Dmitry Rokosov

On 01.05.2023 20:07, Uwe Kleine-König wrote:
> On Mon, May 01, 2023 at 04:03:16PM +0200, Heiner Kallweit wrote:
>> I don't see a reason why we should treat the case lo < hi differently
>> and return 0 as period and duty_cycle. The current logic was added with
>> c375bcbaabdb ("pwm: meson: Read the full hardware state in
>> meson_pwm_get_state()"), Martin as original author doesn't remember why
>> it was implemented this way back then.
>> So let's handle it as normal use case and also remove the optimization
>> for lo == 0. I think the improved readability is worth it.
>>
>> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
>> ---
>> v2:
>> - improve commit description
>> ---
>>  drivers/pwm/pwm-meson.c | 14 ++------------
>>  1 file changed, 2 insertions(+), 12 deletions(-)
>>
>> diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
>> index 5732300eb..3865538dd 100644
>> --- a/drivers/pwm/pwm-meson.c
>> +++ b/drivers/pwm/pwm-meson.c
>> @@ -351,18 +351,8 @@ static int meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
>>  	channel->lo = FIELD_GET(PWM_LOW_MASK, value);
>>  	channel->hi = FIELD_GET(PWM_HIGH_MASK, value);
>>  
>> -	if (channel->lo == 0) {
>> -		state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);
>> -		state->duty_cycle = state->period;
>> -	} else if (channel->lo >= channel->hi) {
>> -		state->period = meson_pwm_cnt_to_ns(chip, pwm,
>> -						    channel->lo + channel->hi);
>> -		state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm,
>> -							channel->hi);
>> -	} else {
>> -		state->period = 0;
>> -		state->duty_cycle = 0;
>> -	}
> 
> The last else branch is even wrong, isn't it? .apply() can for a greater
> than 50% relative duty cycle well have lo < hi, right? So this is not a
> mere optimisation but a fix?!
> 

I *think* too that it's wrong. However I have no test hw and I'm not aware
of any problem caused by the current code. Therefore I was reluctant to make
the patch a fix.

>> +	state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->lo + channel->hi);
>> +	state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);
> 
> Note that meson_pwm_calc() has a similar construct that can be
> simplified in a similar way. All three variants have
> 
> 	channel->pre_div = pre_div;
> 
The pre_div member will be gone anyway with a patch series that is in
discussion currently ("make full use of CCF").

> and the last else branch is universal and can replace the others.
> 
> Another issue I just spotted is that
> 
> 	duty = state->duty_cycle
> 
> is wrong for state->duty_cycle > UINT_MAX. (Ditto the assignment to
> period.) Making both duty and period u64 shoudl fix that. After that
> duty_cnt > 0xffff cannot happen as the core ensures that duty_cycle <=
> period.
> 
I saw that one too. It's something for a follow-up patch.

> Having said that, the proposed change here is an improvement, so:
> 
> Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> 
> I also suggest to add a Fixes line, i.e.
> 
> Fixes: c375bcbaabdb ("pwm: meson: Read the full hardware state in meson_pwm_get_state()")
> 
OK

> Best regards
> Uwe
> 
Heiner


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

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

end of thread, other threads:[~2023-05-02 20:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-01 14:03 [PATCH v2] pwm: meson: modify and simplify calculation in meson_pwm_get_state Heiner Kallweit
2023-05-01 18:07 ` Uwe Kleine-König
2023-05-02 20:27   ` Heiner Kallweit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox