[PATCH v3 0/9] leds: st1202: fix multiple bugs in pattern engine and brightness handling

From: Manuel Fombuena

Date: Fri Jun 05 2026 - 10:00:05 EST


This series fixes several bugs in the LED1202 driver related to hardware
pattern programming and brightness control. The issues were found during
testing on a Linksys MX4200v2 router running OpenWrt.

--- Pattern sequence not stopped before reprogramming ---

The LED1202 datasheet (section 4.8) states that writes to PAT_REP and
pattern duration registers are only applied after the sequence completes
or is stopped. When running in infinite loop mode the sequence never
completes on its own, so these writes are silently ignored by the
hardware.

Patch 1: Stop the running sequence by clearing PATS in the
configuration register at the start of both pattern_clear() and
pattern_set(), ensuring the hardware accepts new values immediately.

Patch 2: Validate pattern input before stopping the sequence. An
out-of-range duration value should be rejected without disrupting a
running pattern, so input validation is moved ahead of the sequence
stop.

--- pattern_clear() leaving hardware in inconsistent state ---

Several independent bugs in pattern_clear() left the hardware in a state
that affected subsequent pattern or brightness operations.

Patch 3: Fix the duration prescaler formula and the skip marker written
by pattern_clear(). The original formula (value / ST1202_MILLIS_PATTERN_DUR_MIN
- 1) was off by one, producing durations ~22ms too short. Additionally,
pattern_clear() relied on the broken formula to produce register value 0
(the pattern skip marker) by passing the minimum duration. With the formula
corrected, pattern_clear() now writes 0 directly to unused duration
registers instead of going through the conversion function.

Patch 4: Restore Pattern0 PWM to full brightness (0x0FFF) after clearing.
pattern_clear() zeroes all PWM slots as part of the clear, but leaves
Pattern0 at zero, so a subsequent direct brightness write has no visible
effect until Pattern0 PWM is restored.

--- Spurious pattern sequence start during setup ---

Patch 5: Remove the write of PATS|PATSR to the configuration register
in st1202_setup(). This accidentally started a pattern sequence before
any pattern data was programmed, producing undefined output on startup.

--- Brightness control broken while pattern mode is active ---

Patch 6: Exit pattern mode in brightness_set() before writing the ILED
register. With PATS set, the LED output is determined by the pattern
engine regardless of the ILED value, making direct brightness writes
have no visible effect while a pattern is active.

Patch 7: Disable the hardware channel in brightness_set() when value
is zero. Previously only the ILED DAC was zeroed while the channel
remained enabled, causing residual current through the enabled channel
and a visible dim glow on the LED.

--- Input validation ---

Patch 8: Validate the reg property read from the device tree before
using it as an array index into chip->leds[]. A value >= ST1202_MAX_LEDS
would cause an out-of-bounds write during probe.

--- Documentation ---

Patch 9: Correct and extend the hw_pattern documentation. The maximum
pattern duration was stated as 5660ms but the correct value derived
from the prescaler formula is 5610ms. The repeat field documentation
is also corrected and the brightness range is made explicit.

--- Testing ---

Tested on LED1202 hardware via I2C. Register state verified with i2cget
at each step. Correct LED behaviour confirmed across pattern cycling,
infinite repeat, pattern_clear, and direct brightness control with
trigger=none.

--- Changes in v3 ---

In response to automated review feedback (Sashiko):

Patch 1: Extend commit message to clarify that the LED1202 has a single
global pattern sequencer shared across all channels, and that stopping
it in pattern_clear() is therefore an inherent hardware constraint rather
than a deliberate design choice.

Patches 3+4: Squashed into a single patch. The prescaler fix and the
skip marker fix are tightly coupled — the skip marker bug was a direct
consequence of the broken formula — and are clearer as one change.

Patch 5 (v2): Dropped. Resetting PAT_REP in pattern_clear() is
unnecessary because pattern_set() always stops the sequencer and writes
its own PAT_REP value before restarting. The reset introduced a spurious
failure point without fixing a real bug.

Patch 4 (was 6): Start the clearing loop at Pattern1 to avoid writing
Pattern0 twice (the loop previously zeroed it before the explicit full
restore).

Patch 5 (was 7): Simplify commit message — remove inaccurate claim that
the SHFT bit is never re-enabled after probe; pattern_clear() restores
it during probe.

New patch 8: Validate the reg device tree property against ST1202_MAX_LEDS
before using it as an array index.

Other pre-existing issues identified by the automated review (global
sequencer coordination, brightness_set sleeping in atomic context,
brightness_set_blocking ignoring the brightness value) are outside the
scope of this fix series and will be addressed in a follow-up submission.

--- Changes in v2 ---

Patch 3: Fix commit message wording — programmed durations were ~22ms
too short, not too long.
Patch 7: Fix Signed-off-by typo.
Patch 10: Add missing quotes around commit subject in Fixes: tag.

v1: https://lore.kernel.org/all/WA0P291MB03850F4E9B4EC7389FE89779C5052@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
v2: https://lore.kernel.org/all/WA0P291MB03855101311F0506B6448A8EC50D2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/

Manuel Fombuena (9):
leds: st1202: stop pattern sequence before reprogramming
leds: st1202: validate pattern input before stopping the sequence
leds: st1202: fix pattern duration prescaler and pattern_clear skip
marker
leds: st1202: restore Pattern0 PWM to full on after clearing pattern
leds: st1202: fix spurious pattern sequence start in setup
leds: st1202: fix brightness having no effect while pattern mode is
active
leds: st1202: disable channel when brightness is set to zero
leds: st1202: validate LED reg property against channel count
leds: st1202: correct and extend hw_pattern documentation

Documentation/leds/leds-st1202.rst | 9 ++-
drivers/leds/leds-st1202.c | 102 ++++++++++++++++++-----------
2 files changed, 68 insertions(+), 43 deletions(-)

--
2.54.0