diff mbox series

[2/2] backlight: pwm_bl: Don't disable the PWM to disable the backlight

Message ID 20230109204758.610400-2-u.kleine-koenig@pengutronix.de (mailing list archive)
State Handled Elsewhere
Headers show
Series [1/2] backlight: pwm_bl: configure pwm only once per backlight toggle | expand

Commit Message

Uwe Kleine-König Jan. 9, 2023, 8:47 p.m. UTC
Most but not all PWMs drive the PWM pin to its inactive state when
disabled. Rely on the lowlevel PWM implementation to implement
duty_cycle = 0 in an energy efficient way and don't disable the PWM.

This fixes backlight disabling e.g. on i.MX6 when an inverted PWM is
used.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/video/backlight/pwm_bl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Daniel Thompson Jan. 10, 2023, 4:26 p.m. UTC | #1
On Mon, Jan 09, 2023 at 09:47:58PM +0100, Uwe Kleine-König wrote:
> Most but not all PWMs drive the PWM pin to its inactive state when
> disabled. Rely on the lowlevel PWM implementation to implement
> duty_cycle = 0 in an energy efficient way and don't disable the PWM.

I'm a little worried about this one.

I thought the PWM APIs allow the duty cycle to be rounded up or down
slightly during the apply.

So when you say "rely on the lowlevel to implement duty_cycle = 0 to..."
is it confirmed that this is true (and that all PWMs *can* implement
a duty_cycle of 0 without rounding up)?


Daniel.


> This fixes backlight disabling e.g. on i.MX6 when an inverted PWM is
> used.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  drivers/video/backlight/pwm_bl.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
> index 0509fecd5715..7bdc5d570a12 100644
> --- a/drivers/video/backlight/pwm_bl.c
> +++ b/drivers/video/backlight/pwm_bl.c
> @@ -109,7 +109,7 @@ static int pwm_backlight_update_status(struct backlight_device *bl)
>  		pwm_backlight_power_off(pb);
>
>  		pwm_get_state(pb->pwm, &state);
> -		state.enabled = false;
> +		state.enabled = true;
>  		state.duty_cycle = 0;
>  		pwm_apply_state(pb->pwm, &state);
>  	}
> --
> 2.39.0
>
Uwe Kleine-König Jan. 10, 2023, 5:35 p.m. UTC | #2
Hello Daniel,

On Tue, Jan 10, 2023 at 04:26:14PM +0000, Daniel Thompson wrote:
> On Mon, Jan 09, 2023 at 09:47:58PM +0100, Uwe Kleine-König wrote:
> > Most but not all PWMs drive the PWM pin to its inactive state when
> > disabled. Rely on the lowlevel PWM implementation to implement
> > duty_cycle = 0 in an energy efficient way and don't disable the PWM.
> 
> I'm a little worried about this one.
> 
> I thought the PWM APIs allow the duty cycle to be rounded up or down
> slightly during the apply.

In my book only rounding down is correct, but in practise there is some
deviation.

Nearly all PWMs can implement a zero duty cycle. Those that cannot but
emit a constant inactive signal when disabled are expected to disable
when .duty_cycle = 0 is requested. (And for those that can neither
implement a zero duty_cycle nor emit the inactive level (not sure there
is any) all bets are lost with and without my patch.)
So if this case will be hit (and noticed) this is fixable.

However there are hardware PWMs that just freeze in their current state
when disabled (e.g. mxs). That's why .duty_cycle=0 + .enabled=true is
the safer bet. Only disable a PWM if you don't rely on the output state.
See also commit 80a22fde803af6f390be49ee5ced6ee75595ba05.

> So when you say "rely on the lowlevel to implement duty_cycle = 0 to..."
> is it confirmed that this is true (and that all PWMs *can* implement
> a duty_cycle of 0 without rounding up)?

The scenario I had in mind that can realistically go wrong here is that
a lowlevel driver that has the property that the inactive level is
emitted for a disabled HW doesn't actually disable when .duty_cycle=0 is
requested and so might consume slightly more energy. But I'm confident
my patch is an improvement and I don't expect regressions. (Famous last
words :-)

I suggest to amend the commit log and add something like:

   If this change results in a regression, the bug is in the lowlevel
   pwm driver.

Best regards
Uwe
Daniel Thompson Jan. 10, 2023, 5:50 p.m. UTC | #3
On Tue, Jan 10, 2023 at 06:35:00PM +0100, Uwe Kleine-König wrote:
> Hello Daniel,
>
> On Tue, Jan 10, 2023 at 04:26:14PM +0000, Daniel Thompson wrote:
> > On Mon, Jan 09, 2023 at 09:47:58PM +0100, Uwe Kleine-König wrote:
> > > Most but not all PWMs drive the PWM pin to its inactive state when
> > > disabled. Rely on the lowlevel PWM implementation to implement
> > > duty_cycle = 0 in an energy efficient way and don't disable the PWM.
> >
> > I'm a little worried about this one.
> >
> > I thought the PWM APIs allow the duty cycle to be rounded up or down
> > slightly during the apply.
>
> In my book only rounding down is correct, but in practise there is some
> deviation.
>
> Nearly all PWMs can implement a zero duty cycle. Those that cannot but
> emit a constant inactive signal when disabled are expected to disable
> when .duty_cycle = 0 is requested. (And for those that can neither
> implement a zero duty_cycle nor emit the inactive level (not sure there
> is any) all bets are lost with and without my patch.)
> So if this case will be hit (and noticed) this is fixable.
>
> However there are hardware PWMs that just freeze in their current state
> when disabled (e.g. mxs). That's why .duty_cycle=0 + .enabled=true is
> the safer bet. Only disable a PWM if you don't rely on the output state.
> See also commit 80a22fde803af6f390be49ee5ced6ee75595ba05.

Reading this, it does strike me that if pwm_bl has a regulator or an
enable GPIO then it does not rely on the output state. We could use
the presence of either of these to choose to disable the PWM
(which could potentially undrive the pin to save power).


> > So when you say "rely on the lowlevel to implement duty_cycle = 0 to..."
> > is it confirmed that this is true (and that all PWMs *can* implement
> > a duty_cycle of 0 without rounding up)?
>
> The scenario I had in mind that can realistically go wrong here is that
> a lowlevel driver that has the property that the inactive level is
> emitted for a disabled HW doesn't actually disable when .duty_cycle=0 is
> requested and so might consume slightly more energy. But I'm confident
> my patch is an improvement and I don't expect regressions. (Famous last
> words :-)
>
> I suggest to amend the commit log and add something like:
>
>    If this change results in a regression, the bug is in the lowlevel
>    pwm driver.

I guess I can live with that :-) .

If the reasoning about regulator or enable GPIO makes sense then let's
implement that. If not, a terse comment in the code reminding some
future version of me that disabled PWM has undefined state (making
clear that the absense of enable = false is deliberate) would be useful!


Daniel.
diff mbox series

Patch

diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
index 0509fecd5715..7bdc5d570a12 100644
--- a/drivers/video/backlight/pwm_bl.c
+++ b/drivers/video/backlight/pwm_bl.c
@@ -109,7 +109,7 @@  static int pwm_backlight_update_status(struct backlight_device *bl)
 		pwm_backlight_power_off(pb);
 
 		pwm_get_state(pb->pwm, &state);
-		state.enabled = false;
+		state.enabled = true;
 		state.duty_cycle = 0;
 		pwm_apply_state(pb->pwm, &state);
 	}