Linux LED subsystem development
 help / color / mirror / Atom feed
* [PATCH v2] leds: pwm: Fix active-low brightness with default-state keep
@ 2026-11-19  2:33 raoxu
  2026-11-19  2:38 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: raoxu @ 2026-11-19  2:33 UTC (permalink / raw)
  To: lee
  Cc: pavel, griffin, Denis.Osterland, linux-leds, linux-kernel, raoxu,
	stable

From: Xu Rao <raoxu@uniontech.com>

With default-state set to "keep", led_pwm_add() derives the initial
brightness from the PWM duty cycle without accounting for active-low
LEDs. However, led_pwm_set() inverts the duty cycle for these LEDs.

For an enabled active-low PWM, zero duty corresponds to maximum LED
brightness, while full duty corresponds to off. The current calculation
reports the opposite values. This exposes an incorrect cached brightness
to userspace and can restore the wrong value after suspend. With an
optional enable GPIO, the cached brightness also determines the initial
GPIO value.

Invert the computed brightness for active-low LEDs, matching the
conversion in led_pwm_set(). Only derive brightness when the PWM is
enabled; otherwise retain the initial LED_OFF value rather than infer
nonzero brightness from an unused duty cycle. The output level of a
disabled PWM is not guaranteed by the PWM API, so this does not claim
to determine its physical LED state.

For an enabled PWM, reject a duty cycle greater than the period before
the conversion to prevent an out-of-range brightness or underflow during
inversion. Check enabled first, consistent with the PWM core ignoring
the other state fields for a disabled PWM. Keep the existing zero-period
fallback to default-state off.

For a valid nonzero-period keep state, do not reprogram the PWM. The
optional enable GPIO is still initialized from the cached brightness,
so retaining LED_OFF for a disabled PWM also deasserts that GPIO.

Fixes: 3d3d65bd2764 ("leds: pwm: add support for default-state device property")
Cc: stable@vger.kernel.org
Signed-off-by: Xu Rao <raoxu@uniontech.com>
---
Changes in v2:
- Move the AI-assistance disclosure below this separator, as requested
  by Lee Jones.
- Do not infer brightness from a disabled PWM's duty cycle; retain the
  initial LED_OFF value for both active-high and active-low LEDs.
- Validate the duty cycle only for an enabled PWM, consistent with the
  PWM core's state validation rules.
- Keep the existing zero-period fallback to default-state off.

The PWM API does not guarantee the output level of a disabled PWM.
Retaining LED_OFF is conservative bookkeeping, not a measurement of the
physical LED state. The optional enable GPIO is still set from this
cached brightness, so it will be deasserted for a disabled PWM.

For an enabled PWM with duty_cycle greater than period, this revision
returns -EINVAL rather than clamping an unknown state. This fails probe
for the pwm-leds device, including any sibling LEDs, and is a deliberate
choice to avoid deriving brightness from an invalid enabled state.

Validation:
- W=1 build of drivers/leds/leds-pwm.o with CONFIG_LEDS_PWM=y passed.
- Source-extracted initialization tests passed for 6,004 combinations
  of enabled/disabled, active-high/active-low and duty values, plus the
  existing zero-period fallback. These use stubs, not actual hardware.
- With period=1000 and max_brightness=255, active-low tests include:
    disabled, duty=1500: success, brightness=0
    disabled, duty=0:    success, brightness=0
    enabled, duty=1500:  -EINVAL
    enabled, duty=0:     success, brightness=255
    enabled, duty=1000:  success, brightness=0
- Physical LED, enable-GPIO and suspend/resume tests remain outstanding.

OpenCode AI assistance was used for source review, preparation of this
revision and test scaffolding. No hardware Tested-by is claimed.

 drivers/leds/leds-pwm.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
index 3e5eda8590bb..f06eecf7f5c0 100644
--- a/drivers/leds/leds-pwm.c
+++ b/drivers/leds/leds-pwm.c
@@ -128,9 +128,20 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
 		{
 		uint64_t brightness;
 
+		/* A disabled PWM's duty cycle does not describe its output. */
+		if (!led_data->pwmstate.enabled)
+			break;
+
+		if (led_data->pwmstate.duty_cycle > led_data->pwmstate.period)
+			return dev_err_probe(dev, -EINVAL,
+					     "PWM duty cycle exceeds period for %s\n",
+					     led->name);
+
 		brightness = led->max_brightness;
 		brightness *= led_data->pwmstate.duty_cycle;
 		do_div(brightness, led_data->pwmstate.period);
+		if (led_data->active_low)
+			brightness = led->max_brightness - brightness;
 		led_data->cdev.brightness = brightness;
 		}
 		break;
-- 
2.50.1

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

end of thread, other threads:[~2026-11-19  2:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  2:33 [PATCH v2] leds: pwm: Fix active-low brightness with default-state keep raoxu
2026-11-19  2:38 ` sashiko-bot

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