[PATCH 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode

From: Fenglin Wu

Date: Tue Nov 17 2026 - 04:32:56 EST


The multicolor hardware-pattern path has two related issues.

First, lpg_pattern_set() programs one unscaled LUT and assigns it to
every LPG channel. RGB and multicolor LEDs therefore replay identical
brightness values regardless of multi_intensity, making the output
white-balanced rather than preserving the requested color.

Fix this by normalizing the requested pattern once, scaling its
brightness values for each enabled sub-LED, and allocating and
programming a separate LUT range for every active LPG channel. Stop the
channels before replacing or releasing their LUTs, and roll back all
per-channel allocations if an update fails.

Second, a pattern is scaled only when it is installed. Updating
brightness or multi_intensity while it is active leaves already-enabled
channels with stale LUT values. A channel that was disabled when the
pattern was installed has no LUT and falls back to static PWM when it is
enabled later.

Fix this by caching the normalized, unscaled pattern and repeat state.
On each multicolor brightness callback, stop the running pattern,
release its per-channel LUTs, rebuild them from the current sub-LED
intensities, and restart the active channels. Clear the cached active
state when the pattern is removed.

The second issue was identified and fixed by OpenAI Codex when I used
it to review the original change.

Both of the fixes have been tested on a Maili QRD device and confirmed
that the HW pattern of a multicolor LED could be scaled and refreshed
correctly whenever hw_pattern, brightness, multi_intensity were updated.

Fixes: 24e2d05d1b68 ("leds: Add driver for Qualcomm LPG")
Fixes: 6ab1f766a80a ("leds: rgb: leds-qcom-lpg: Add support for PPG through single SDAM")
Fixes: 5e9ff626861a ("leds: rgb: leds-qcom-lpg: Include support for PPG with dedicated LUT SDAM")
Signed-off-by: Fenglin Wu <fenglin.wu@xxxxxxxxxxxxxxxx>
---
drivers/leds/rgb/leds-qcom-lpg.c | 274 +++++++++++++++++++++++++++++++--------
1 file changed, 217 insertions(+), 57 deletions(-)

diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
index e14a3316bf22..175b3680766f 100644
--- a/drivers/leds/rgb/leds-qcom-lpg.c
+++ b/drivers/leds/rgb/leds-qcom-lpg.c
@@ -151,11 +151,32 @@ struct lpg_channel {
bool has_lut;
};

+/**
+ * struct lpg_pattern - The LPG pattern normalized from the LED pattern
+ * @data: The pattern data array
+ * @len: number of entries to write to the LUT
+ * @delta_t: common step duration in ms
+ * @lo_pause: low-pause duration in ms
+ * @hi_pause: high-pause duration in ms
+ * @ping_pong: true if the pattern support reverse
+ */
+struct lpg_pattern {
+ struct led_pattern *data;
+ unsigned int len;
+ unsigned int delta_t;
+ unsigned int lo_pause;
+ unsigned int hi_pause;
+ bool ping_pong;
+};
+
/**
* struct lpg_led - logical LED object
* @lpg: lpg context reference
* @cdev: LED class device
* @mcdev: Multicolor LED class device
+ * @pattern: unscaled active multicolor pattern; zero length when inactive
+ * @scaled_pattern: scratch buffer for per-channel LUT updates
+ * @pattern_oneshot: whether the cached pattern is played only once
* @channels: list of channels associated with the LED
* @num_channels: number of @channels
*/
@@ -164,6 +185,9 @@ struct lpg_led {

struct led_classdev cdev;
struct led_classdev_mc mcdev;
+ struct lpg_pattern pattern;
+ struct led_pattern *scaled_pattern;
+ bool pattern_oneshot;

unsigned int num_channels;
struct lpg_channel *channels[] __counted_by(num_channels);
@@ -876,18 +900,25 @@ static int lpg_brightness_single_set(struct led_classdev *cdev,
return 0;
}

+static int lpg_pattern_mc_update(struct lpg_led *led);
+
static int lpg_brightness_mc_set(struct led_classdev *cdev,
enum led_brightness value)
{
struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
struct lpg_led *led = container_of(mc, struct lpg_led, mcdev);
+ int ret = 0;

- mutex_lock(&led->lpg->lock);
+ guard(mutex)(&led->lpg->lock);

led_mc_calc_color_components(mc, value);
- lpg_brightness_set(led, cdev, mc->subled_info);
+ if (led->pattern.len) {
+ ret = lpg_pattern_mc_update(led);
+ if (ret)
+ return ret;
+ }

- mutex_unlock(&led->lpg->lock);
+ lpg_brightness_set(led, cdev, mc->subled_info);

return 0;
}
@@ -966,23 +997,15 @@ static int lpg_blink_mc_set(struct led_classdev *cdev,
return ret;
}

-static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
- u32 len, int repeat)
+static int lpg_prepare_pattern(struct lpg *lpg, struct led_pattern *led_pattern,
+ u32 len, int repeat, struct lpg_pattern *prep)
{
- struct lpg_channel *chan;
- struct lpg *lpg = led->lpg;
- struct led_pattern *pattern;
unsigned int brightness_a;
unsigned int brightness_b;
- unsigned int hi_pause = 0;
- unsigned int lo_pause = 0;
unsigned int actual_len;
unsigned int delta_t;
- unsigned int lo_idx;
- unsigned int hi_idx;
unsigned int i;
bool ping_pong = true;
- int ret = -EINVAL;

/* Hardware only support oneshot or indefinite loops */
if (repeat != -1 && repeat != 1)
@@ -1002,15 +1025,16 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
if (len % 2)
return -EINVAL;

- pattern = kzalloc_objs(*pattern, len / 2);
+ struct led_pattern *pattern __free(kfree) = kzalloc_objs(*pattern, len / 2);
+
if (!pattern)
return -ENOMEM;

for (i = 0; i < len; i += 2) {
if (led_pattern[i].brightness != led_pattern[i + 1].brightness)
- goto out_free_pattern;
+ return -EINVAL;
if (led_pattern[i + 1].delta_t != 0)
- goto out_free_pattern;
+ return -EINVAL;

pattern[i / 2].brightness = led_pattern[i].brightness;
pattern[i / 2].delta_t = led_pattern[i].delta_t;
@@ -1023,7 +1047,7 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
* through the entire LUT, so prohibit this.
*/
if (len < 2)
- goto out_free_pattern;
+ return -EINVAL;

/*
* The LPG plays patterns with at a fixed pace, a "low pause" can be
@@ -1080,13 +1104,13 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
* specify hi pause. Reject other variations.
*/
if (i != actual_len - 1)
- goto out_free_pattern;
+ return -EINVAL;
}
}

/* LPG_RAMP_DURATION_REG is a 9bit */
if (delta_t >= BIT(9))
- goto out_free_pattern;
+ return -EINVAL;

/*
* Find "low pause" and "high pause" in the pattern in the LUT case.
@@ -1094,33 +1118,59 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
* duration of all steps.
*/
if (lpg->lut_base || lpg->lut_sdam) {
- lo_pause = pattern[0].delta_t;
- hi_pause = pattern[actual_len - 1].delta_t;
+ prep->lo_pause = pattern[0].delta_t;
+ prep->hi_pause = pattern[actual_len - 1].delta_t;
} else {
if (delta_t != pattern[0].delta_t || delta_t != pattern[actual_len - 1].delta_t)
- goto out_free_pattern;
+ return -EINVAL;
+ prep->lo_pause = 0;
+ prep->hi_pause = 0;
}

+ prep->data = no_free_ptr(pattern);
+ prep->len = actual_len;
+ prep->delta_t = delta_t;
+ prep->ping_pong = ping_pong;

- mutex_lock(&lpg->lock);
+ return 0;
+}
+
+static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
+ u32 len, int repeat)
+{
+ struct lpg_channel *chan;
+ struct lpg *lpg = led->lpg;
+ struct lpg_pattern pattern;
+ unsigned int lo_idx;
+ unsigned int hi_idx;
+ unsigned int i;
+ int ret;
+
+ ret = lpg_prepare_pattern(lpg, led_pattern, len, repeat, &pattern);
+ if (ret < 0)
+ return ret;
+
+ struct led_pattern *prep_data __free(kfree) = pattern.data;
+
+ guard(mutex)(&lpg->lock);

if (lpg->lut_base)
- ret = lpg_lut_store(lpg, pattern, actual_len, &lo_idx, &hi_idx);
+ ret = lpg_lut_store(lpg, prep_data, pattern.len, &lo_idx, &hi_idx);
else
- ret = lpg_lut_store_sdam(lpg, pattern, actual_len, &lo_idx, &hi_idx);
+ ret = lpg_lut_store_sdam(lpg, prep_data, pattern.len, &lo_idx, &hi_idx);

if (ret < 0)
- goto out_unlock;
+ return ret;

for (i = 0; i < led->num_channels; i++) {
chan = led->channels[i];

- chan->ramp_tick_ms = delta_t;
- chan->ramp_ping_pong = ping_pong;
+ chan->ramp_tick_ms = pattern.delta_t;
+ chan->ramp_ping_pong = pattern.ping_pong;
chan->ramp_oneshot = repeat != -1;

- chan->ramp_lo_pause_ms = lo_pause;
- chan->ramp_hi_pause_ms = hi_pause;
+ chan->ramp_lo_pause_ms = pattern.lo_pause;
+ chan->ramp_hi_pause_ms = pattern.hi_pause;

chan->pattern_lo_idx = lo_idx;
chan->pattern_hi_idx = hi_idx;
@@ -1128,11 +1178,6 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
chan->has_lut = i == 0;
}

-out_unlock:
- mutex_unlock(&lpg->lock);
-out_free_pattern:
- kfree(pattern);
-
return ret;
}

@@ -1152,24 +1197,133 @@ static int lpg_pattern_single_set(struct led_classdev *cdev,
return 0;
}

-static int lpg_pattern_mc_set(struct led_classdev *cdev,
- struct led_pattern *pattern, u32 len,
- int repeat)
+/* Caller must hold lpg->lock. Stop all readers before releasing their LUTs. */
+static int lpg_pattern_stop(struct lpg_led *led)
{
- struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
- struct lpg_led *led = container_of(mc, struct lpg_led, mcdev);
+ struct lpg *lpg = led->lpg;
+ struct lpg_channel *chan;
unsigned int triled_mask = 0;
- int ret, i;
+ unsigned int i;
+ int ret;

for (i = 0; i < led->num_channels; i++)
triled_mask |= led->channels[i]->triled_mask;
- triled_set(led->lpg, triled_mask, 0);
+ ret = triled_set(lpg, triled_mask, 0);
+ if (ret)
+ return ret;

- ret = lpg_pattern_set(led, pattern, len, repeat);
+ for (i = 0; i < led->num_channels; i++) {
+ chan = led->channels[i];
+ ret = lpg_sdam_configure_triggers(chan, 0);
+ if (ret < 0)
+ return ret;
+ ret = regmap_update_bits(lpg->map, chan->base + PWM_ENABLE_CONTROL_REG,
+ LPG_ENABLE_CONTROL_OUTPUT |
+ LPG_ENABLE_CONTROL_RAMP_GEN, 0);
+ if (ret)
+ return ret;
+ ret = lpg_clear_pbs_trigger(lpg, chan->lut_mask);
+ if (ret)
+ return ret;
+
+ chan->enabled = false;
+ chan->ramp_enabled = false;
+ }
+
+ return 0;
+}
+
+/* Caller must hold lpg->lock. */
+static int lpg_pattern_mc_update(struct lpg_led *led)
+{
+ struct led_classdev_mc *mc = &led->mcdev;
+ struct led_classdev *cdev = &mc->led_cdev;
+ struct lpg_pattern *pattern = &led->pattern;
+ struct led_pattern *scaled = led->scaled_pattern;
+ struct lpg *lpg = led->lpg;
+ struct lpg_channel *chan;
+ unsigned int lo_idx, hi_idx;
+ unsigned int i, j;
+ int ret;
+
+ ret = lpg_pattern_stop(led);
+ if (ret)
+ return ret;
+
+ for (i = 0; i < led->num_channels; i++)
+ lpg_lut_free(led->channels[i]);
+
+ /* Rebuild each channel from the unscaled pattern and current intensity. */
+ for (i = 0; i < led->num_channels; i++) {
+ chan = led->channels[i];
+ if (!mc->subled_info[i].brightness)
+ continue;
+
+ for (j = 0; j < pattern->len; j++) {
+ scaled[j].brightness = DIV_ROUND_CLOSEST(
+ (u32)pattern->data[j].brightness * mc->subled_info[i].intensity,
+ cdev->max_brightness);
+ scaled[j].delta_t = pattern->data[j].delta_t;
+ }
+
+ if (lpg->lut_base)
+ ret = lpg_lut_store(lpg, scaled, pattern->len, &lo_idx, &hi_idx);
+ else
+ ret = lpg_lut_store_sdam(lpg, scaled, pattern->len, &lo_idx, &hi_idx);
+ if (ret < 0)
+ goto out_free;
+
+ chan->ramp_tick_ms = pattern->delta_t;
+ chan->ramp_ping_pong = pattern->ping_pong;
+ chan->ramp_oneshot = led->pattern_oneshot;
+ chan->ramp_lo_pause_ms = pattern->lo_pause;
+ chan->ramp_hi_pause_ms = pattern->hi_pause;
+ chan->pattern_lo_idx = lo_idx;
+ chan->pattern_hi_idx = hi_idx;
+ chan->has_lut = true;
+ }
+
+ return 0;
+
+out_free:
+ for (i = 0; i < led->num_channels; i++)
+ lpg_lut_free(led->channels[i]);
+ return ret;
+}
+
+static int lpg_pattern_mc_set(struct led_classdev *cdev,
+ struct led_pattern *led_pattern, u32 len,
+ int repeat)
+{
+ struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
+ struct lpg_led *led = container_of(mc, struct lpg_led, mcdev);
+ struct lpg *lpg = led->lpg;
+ struct lpg_pattern pattern;
+ int ret;
+
+ ret = lpg_prepare_pattern(lpg, led_pattern, len, repeat, &pattern);
if (ret < 0)
return ret;

+ struct led_pattern *prep_data __free(kfree) = pattern.data;
+
+ if (pattern.len > lpg->lut_size)
+ return -EINVAL;
+
+ guard(mutex)(&lpg->lock);
+
+ memcpy(led->pattern.data, prep_data, pattern.len * sizeof(*prep_data));
+ pattern.data = led->pattern.data;
+ led->pattern = pattern;
+ led->pattern_oneshot = repeat != -1;
+
led_mc_calc_color_components(mc, LED_FULL);
+ ret = lpg_pattern_mc_update(led);
+ if (ret) {
+ led->pattern.len = 0;
+ return ret;
+ }
+
lpg_brightness_set(led, cdev, mc->subled_info);

return 0;
@@ -1177,24 +1331,19 @@ static int lpg_pattern_mc_set(struct led_classdev *cdev,

static int lpg_pattern_clear(struct lpg_led *led)
{
- struct lpg_channel *chan;
struct lpg *lpg = led->lpg;
- int i;
-
- mutex_lock(&lpg->lock);
+ unsigned int i;
+ int ret;

- chan = led->channels[0];
- lpg_lut_free(chan);
+ guard(mutex)(&lpg->lock);

- for (i = 0; i < led->num_channels; i++) {
- chan = led->channels[i];
- lpg_sdam_configure_triggers(chan, 0);
- lpg_clear_pbs_trigger(chan->lpg, chan->lut_mask);
- chan->pattern_lo_idx = 0;
- chan->pattern_hi_idx = 0;
- }
+ led->pattern.len = 0;
+ ret = lpg_pattern_stop(led);
+ if (ret)
+ return ret;

- mutex_unlock(&lpg->lock);
+ for (i = 0; i < led->num_channels; i++)
+ lpg_lut_free(led->channels[i]);

return 0;
}
@@ -1433,6 +1582,17 @@ static int lpg_add_led(struct lpg *lpg, struct fwnode_handle *fw)

/* Register pattern accessors if we have a LUT block or when using PPG */
if (lpg->lut_base || lpg->lpg_chan_sdam) {
+ led->pattern.data = devm_kcalloc(lpg->dev, lpg->lut_size,
+ sizeof(*led->pattern.data), GFP_KERNEL);
+ if (!led->pattern.data)
+ return -ENOMEM;
+
+ led->scaled_pattern = devm_kcalloc(lpg->dev, lpg->lut_size,
+ sizeof(*led->scaled_pattern),
+ GFP_KERNEL);
+ if (!led->scaled_pattern)
+ return -ENOMEM;
+
cdev->pattern_set = lpg_pattern_mc_set;
cdev->pattern_clear = lpg_pattern_mc_clear;
}

--
2.43.0