* [PATCH 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support
@ 2026-11-17 9:31 Fenglin Wu
2026-11-17 9:31 ` [PATCH 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Fenglin Wu @ 2026-11-17 9:31 UTC (permalink / raw)
To: linux-arm-msm, Lee Jones, Pavel Machek, Griffin Kroah-Hartman,
Marijn Suijten, Bjorn Andersson, Anjelique Melendez,
Guru Das Srinagesh, Uwe Kleine-König
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, Fenglin Wu
This series of changes combines and rebased these 2 series as they were
changing the same driver file. It is easier to send them together to handle
the dependency:
https://lore.kernel.org/linux-leds/20260716-lpg-rgb-color-balance-fix-v6-1-b49d51528f61@oss.qualcomm.com/
https://lore.kernel.org/linux-leds/20260827-lpg-pwm-fm-support-v3-1-88ae5208c57b@oss.qualcomm.com/
Also, a new change is added to close the discussion in here:
https://lore.kernel.org/linux-leds/20260827133753.GM770273@google.com/
Following are the Changes in this series after the combination:
- Added changes to scale the pattern when the brightness and
multi_intensity is updated while the pattern is active.
- Added a new change for explicit LUT ownership tracking to fix single-entry
LUT leaks during pattern cleanup.
- Skipped LUT allocation for zero-intensity multicolor channels to fix
comments from Konrad.
- Renamed Frequency Mode identifiers consistently from `fm_*`/`frequency_mode`
to `freq_mode_*', and clean up the code and fix the naming issues to address
the review comments from Lee.
- Added logic to compare the timing accuracy between standard pwm mode and
frequency mode and select the better one.
The change series was tested on Maili QRD device with pmh0101 in these scenarios:
1. Tested the RGB LEDs by randomly adjusting the brightness, hw_pattern, and
multi_intensity when it is working in HW pattern mode, and the LED pattern
and color were confirmed to update correctly every time.
2. Exercised the PWM4 channel on pmh0101 by requesting it at different periods
with a 50% duty cycle, confirming that the frequency mode could be configured
correctly.
Signed-off-by: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
---
Fenglin Wu (4):
leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak
leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode
leds: rgb: leds-qcom-lpg: add mutex lock in lpg_pwm_get_state()
leds: rgb: leds-qcom-lpg: Add PWM frequency mode support
drivers/leds/rgb/leds-qcom-lpg.c | 643 ++++++++++++++++++++++++++++++++++-----
1 file changed, 562 insertions(+), 81 deletions(-)
---
base-commit: 4e88404a72e5e26720539e5555c383069cefb486
change-id: 20261103-led-qcom-leds-multiple-fixes-and-pwm-fm-support-3a04f85c60b9
Best regards,
--
Fenglin Wu <fenglin.wu@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak
2026-11-17 9:31 [PATCH 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
@ 2026-11-17 9:31 ` Fenglin Wu
2026-11-17 9:46 ` sashiko-bot
2026-11-17 9:31 ` [PATCH 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode Fenglin Wu
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Fenglin Wu @ 2026-11-17 9:31 UTC (permalink / raw)
To: linux-arm-msm, Lee Jones, Pavel Machek, Griffin Kroah-Hartman,
Marijn Suijten, Bjorn Andersson, Anjelique Melendez,
Guru Das Srinagesh, Uwe Kleine-König
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, Fenglin Wu
The LUT resource cleanup path currently treats identical high and
low indices as an unallocated range and skips clearing the bitmap
allocation. However, with a 2-step palindrome pattern, the high
and low indices are identical, which can leak the LUT resource
because the bitmap allocation remains uncleared.
Resolve this by tracking LUT ownership separately from the index
range. Designate the first channel as the owner of shared
allocations and each active multicolor channel as the owner
of its per-channel allocation, then track this ownership state
for both normal cleanup and allocation.
Fixes: 24e2d05d1b689 ("leds: Add driver for Qualcomm LPG")
Closes: https://lore.kernel.org/linux-leds/7e474cdf-fcb2-468d-844d-f8afb5976d16@oss.qualcomm.com/
Signed-off-by: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
---
drivers/leds/rgb/leds-qcom-lpg.c | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
index 24ffe079f221..e14a3316bf22 100644
--- a/drivers/leds/rgb/leds-qcom-lpg.c
+++ b/drivers/leds/rgb/leds-qcom-lpg.c
@@ -111,6 +111,7 @@ struct lpg_data;
* @ramp_hi_pause_ms: pause (in milliseconds) after iterating over pattern
* @pattern_lo_idx: start index of associated pattern
* @pattern_hi_idx: last index of associated pattern
+ * @has_lut: channel has the associated LUT allocation
*/
struct lpg_channel {
struct lpg *lpg;
@@ -147,6 +148,7 @@ struct lpg_channel {
unsigned int pattern_lo_idx;
unsigned int pattern_hi_idx;
+ bool has_lut;
};
/**
@@ -393,15 +395,19 @@ static int lpg_lut_store(struct lpg *lpg, struct led_pattern *pattern,
return 0;
}
-static void lpg_lut_free(struct lpg *lpg, unsigned int lo_idx, unsigned int hi_idx)
+static void lpg_lut_free(struct lpg_channel *chan)
{
+ struct lpg *lpg = chan->lpg;
int len;
- len = hi_idx - lo_idx + 1;
- if (len == 1)
- return;
+ if (chan->has_lut) {
+ len = chan->pattern_hi_idx - chan->pattern_lo_idx + 1;
+ bitmap_clear(lpg->lut_bitmap, chan->pattern_lo_idx, len);
+ }
- bitmap_clear(lpg->lut_bitmap, lo_idx, len);
+ chan->pattern_lo_idx = 0;
+ chan->pattern_hi_idx = 0;
+ chan->has_lut = false;
}
static int lpg_lut_sync(struct lpg *lpg, unsigned int mask)
@@ -1118,6 +1124,8 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
chan->pattern_lo_idx = lo_idx;
chan->pattern_hi_idx = hi_idx;
+ /* The 1st channel owns the LUT while other channels share it */
+ chan->has_lut = i == 0;
}
out_unlock:
@@ -1176,7 +1184,7 @@ static int lpg_pattern_clear(struct lpg_led *led)
mutex_lock(&lpg->lock);
chan = led->channels[0];
- lpg_lut_free(lpg, chan->pattern_lo_idx, chan->pattern_hi_idx);
+ lpg_lut_free(chan);
for (i = 0; i < led->num_channels; i++) {
chan = led->channels[i];
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode
2026-11-17 9:31 [PATCH 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
2026-11-17 9:31 ` [PATCH 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
@ 2026-11-17 9:31 ` Fenglin Wu
2026-11-17 9:50 ` sashiko-bot
2026-11-17 9:31 ` [PATCH 3/4] leds: rgb: leds-qcom-lpg: add mutex lock in lpg_pwm_get_state() Fenglin Wu
2026-11-17 9:31 ` [PATCH 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support Fenglin Wu
3 siblings, 1 reply; 9+ messages in thread
From: Fenglin Wu @ 2026-11-17 9:31 UTC (permalink / raw)
To: linux-arm-msm, Lee Jones, Pavel Machek, Griffin Kroah-Hartman,
Marijn Suijten, Bjorn Andersson, Anjelique Melendez,
Guru Das Srinagesh, Uwe Kleine-König
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, Fenglin Wu
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@oss.qualcomm.com>
---
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
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/4] leds: rgb: leds-qcom-lpg: add mutex lock in lpg_pwm_get_state()
2026-11-17 9:31 [PATCH 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
2026-11-17 9:31 ` [PATCH 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
2026-11-17 9:31 ` [PATCH 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode Fenglin Wu
@ 2026-11-17 9:31 ` Fenglin Wu
2026-11-17 9:40 ` sashiko-bot
2026-11-17 9:31 ` [PATCH 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support Fenglin Wu
3 siblings, 1 reply; 9+ messages in thread
From: Fenglin Wu @ 2026-11-17 9:31 UTC (permalink / raw)
To: linux-arm-msm, Lee Jones, Pavel Machek, Griffin Kroah-Hartman,
Marijn Suijten, Bjorn Andersson, Anjelique Melendez,
Guru Das Srinagesh, Uwe Kleine-König
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, Fenglin Wu
Guard mutex lock at the top of lpg_pwm_get_state(), to make the
register read atomic against the register write in lpg_pwm_apply().
Fixes: 24e2d05d1b68 ("leds: Add driver for Qualcomm LPG")
Signed-off-by: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
---
drivers/leds/rgb/leds-qcom-lpg.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
index 175b3680766f..971f92911606 100644
--- a/drivers/leds/rgb/leds-qcom-lpg.c
+++ b/drivers/leds/rgb/leds-qcom-lpg.c
@@ -1425,6 +1425,8 @@ static int lpg_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
u16 pwm_value;
int ret;
+ guard(mutex)(&lpg->lock);
+
ret = regmap_read(lpg->map, chan->base + LPG_SIZE_CLK_REG, &val);
if (ret)
return ret;
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support
2026-11-17 9:31 [PATCH 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
` (2 preceding siblings ...)
2026-11-17 9:31 ` [PATCH 3/4] leds: rgb: leds-qcom-lpg: add mutex lock in lpg_pwm_get_state() Fenglin Wu
@ 2026-11-17 9:31 ` Fenglin Wu
2026-11-17 9:51 ` sashiko-bot
3 siblings, 1 reply; 9+ messages in thread
From: Fenglin Wu @ 2026-11-17 9:31 UTC (permalink / raw)
To: linux-arm-msm, Lee Jones, Pavel Machek, Griffin Kroah-Hartman,
Marijn Suijten, Bjorn Andersson, Anjelique Melendez,
Guru Das Srinagesh, Uwe Kleine-König
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, Fenglin Wu
Certain PWM channels on a PMIC (e.g. PM8350C PWM4) support a Frequency
Mode (FM) that can generate waveforms with more frequency points than
the standard LPG PWM mode. The trade-off is that the duty cycle can
only be fixed at 50%. Add the FM support and try to use it When the
PWM channel is requested to set a duty cycle to exactly 50%.
Instead of always selecting the frequency mode which might generate
the PWM waveform with less timing accuracy, the Codex AI suggested
and implemented an update that compares the PWM period and duty timing
accuracy in standard PWM mode versus frequency mode, then selects
the better option.
The change was tested on the Maili QRD device with pmh0101 by exercising
the pwm4 channel with a fixed 50% duty cycle and a period range of 40us
to 55us in 0.5us steps, and confirmed in the log that correct frequency
mode configuration was selected.
Signed-off-by: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
---
drivers/leds/rgb/leds-qcom-lpg.c | 349 ++++++++++++++++++++++++++++++++++++---
1 file changed, 330 insertions(+), 19 deletions(-)
diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
index 971f92911606..68c73bce103c 100644
--- a/drivers/leds/rgb/leds-qcom-lpg.c
+++ b/drivers/leds/rgb/leds-qcom-lpg.c
@@ -22,6 +22,8 @@
#define LPG_SUBTYPE_PWM 0xb
#define LPG_SUBTYPE_HI_RES_PWM 0xc
#define LPG_SUBTYPE_LPG_LITE 0x11
+#define PWM_STATUS1_REG 0x08
+#define PWM_FM_PRESENT BIT(0)
#define LPG_PATTERN_CONFIG_REG 0x40
#define LPG_SIZE_CLK_REG 0x41
#define PWM_CLK_SELECT_MASK GENMASK(1, 0)
@@ -43,6 +45,10 @@
#define PWM_SEC_ACCESS_REG 0xd0
#define PWM_DTEST_REG(x) (0xe2 + (x) - 1)
+/* offset 0x50: PWM_FM_MODE_REG on LPG_SUBTYPE_PWM */
+#define PWM_FM_MODE_REG 0x50
+#define PWM_FM_ENABLE BIT(7)
+
#define SDAM_REG_PBS_SEQ_EN 0x42
#define SDAM_PBS_TRIG_SET 0xe5
#define SDAM_PBS_TRIG_CLR 0xe6
@@ -111,6 +117,8 @@ struct lpg_data;
* @ramp_hi_pause_ms: pause (in milliseconds) after iterating over pattern
* @pattern_lo_idx: start index of associated pattern
* @pattern_hi_idx: last index of associated pattern
+ * @freq_mode_capable: hardware supports Frequency Mode
+ * @enable_freq_mode: set the period using Frequency Mode
* @has_lut: channel has the associated LUT allocation
*/
struct lpg_channel {
@@ -148,6 +156,8 @@ struct lpg_channel {
unsigned int pattern_lo_idx;
unsigned int pattern_hi_idx;
+ bool freq_mode_capable;
+ bool enable_freq_mode;
bool has_lut;
};
@@ -247,11 +257,13 @@ struct lpg {
* @sdam_offset: Channel offset in LPG SDAM
* @base: base address for PWM channel registers
* @triled_mask: bitmask for controlling this channel in TRILED
+ * @freq_mode_capable: channel hardware supports Frequency Mode
*/
struct lpg_channel_data {
unsigned int sdam_offset;
unsigned int base;
u8 triled_mask;
+ bool freq_mode_capable;
};
/**
@@ -448,6 +460,116 @@ static const unsigned int lpg_pre_divs[] = {1, 3, 5, 6};
static const unsigned int lpg_pwm_resolution[] = {6, 9};
static const unsigned int lpg_pwm_resolution_hi_res[] = {8, 9, 10, 11, 12, 13, 14, 15};
+/*
+ * Compare exact physical durations without rounding to whole nanoseconds.
+ * Return positive if the candidate is longer, negative if the best is longer,
+ * and zero if both durations are equal.
+ */
+static int lpg_pwm_compare_candidate_duration(u64 candidate_duration_numerator,
+ unsigned int candidate_clk_rate,
+ u64 best_duration_numerator,
+ unsigned int best_clk_rate)
+{
+ u32 candidate_remainder;
+ u32 best_remainder;
+ u64 candidate_whole_ns;
+ u64 best_whole_ns;
+ u64 candidate_fraction;
+ u64 best_fraction;
+
+ candidate_whole_ns = div_u64_rem(candidate_duration_numerator,
+ candidate_clk_rate,
+ &candidate_remainder);
+ best_whole_ns = div_u64_rem(best_duration_numerator, best_clk_rate,
+ &best_remainder);
+ candidate_fraction = (u64)candidate_remainder * best_clk_rate;
+ best_fraction = (u64)best_remainder * candidate_clk_rate;
+
+ if (candidate_whole_ns != best_whole_ns)
+ return candidate_whole_ns > best_whole_ns ? 1 : -1;
+
+ if (candidate_fraction != best_fraction)
+ return candidate_fraction > best_fraction ? 1 : -1;
+
+ return 0;
+}
+
+/*
+ * Period calculation for PWM working in Frequency mode:
+ *
+ * (pwm_value + 1) * 2^(exponent + 1) * NSEC_PER_SEC
+ * period = --------------------------------------------------
+ * clock_rate
+ *
+ * The duty cycle is fixed at half of the period.
+ */
+#define PWM_FM_PERIOD_NUMERATOR(exponent, pwm_value) \
+ (((u64)((pwm_value) + 1) << ((exponent) + 1)) * NSEC_PER_SEC)
+
+static int lpg_calc_freq_in_freq_mode(struct lpg_channel *chan, u64 period_ns, u64 duty_ns)
+{
+ unsigned int best_clk_sel = 0;
+ unsigned int best_pwm_exp = 0;
+ unsigned int best_pwm_value = 0;
+ u64 best_period_numerator = 0;
+ u64 max_period_ns;
+
+ max_period_ns = div_u64(PWM_FM_PERIOD_NUMERATOR(LPG_MAX_M, U8_MAX),
+ lpg_clk_rates[1]);
+ period_ns = min(period_ns, max_period_ns);
+ duty_ns = min(duty_ns, max_period_ns / 2);
+
+ for (unsigned int clk_sel = 1;
+ clk_sel < ARRAY_SIZE(lpg_clk_rates); clk_sel++) {
+ for (unsigned int pwm_exp = 0; pwm_exp <= LPG_MAX_M; pwm_exp++) {
+ u64 duty_counter_length;
+ u64 counter_length;
+ u64 period_numerator;
+
+ counter_length = div64_u64(period_ns * lpg_clk_rates[clk_sel],
+ (u64)NSEC_PER_SEC << (pwm_exp + 1));
+ duty_counter_length = div64_u64(duty_ns * lpg_clk_rates[clk_sel],
+ (u64)NSEC_PER_SEC << pwm_exp);
+ /* The counter must satisfy both period and fixed 50% duty bounds. */
+ counter_length = min(counter_length, duty_counter_length);
+ counter_length = min(counter_length, (u64)U8_MAX + 1);
+ if (!counter_length)
+ continue;
+
+ period_numerator =
+ PWM_FM_PERIOD_NUMERATOR(pwm_exp, counter_length - 1);
+ /*
+ * All candidates are within the requested bounds, so the
+ * longest period has the smallest timing error.
+ */
+ if (!best_clk_sel ||
+ lpg_pwm_compare_candidate_duration(period_numerator,
+ lpg_clk_rates[clk_sel],
+ best_period_numerator,
+ lpg_clk_rates[best_clk_sel]) > 0) {
+ best_clk_sel = clk_sel;
+ best_pwm_exp = pwm_exp;
+ best_pwm_value = counter_length - 1;
+ best_period_numerator = period_numerator;
+ }
+ }
+ }
+
+ if (!best_clk_sel)
+ return -EINVAL;
+
+ chan->clk_sel = best_clk_sel;
+ chan->pre_div_exp = best_pwm_exp;
+ chan->pwm_value = best_pwm_value;
+ /* Frequency mode does not use PRE-DIV and PWM resolution settings. */
+ chan->pre_div_sel = 0;
+ chan->pwm_resolution_sel = 0;
+ chan->period = DIV_ROUND_UP_ULL(best_period_numerator,
+ lpg_clk_rates[best_clk_sel]);
+
+ return 0;
+}
+
static int lpg_calc_freq(struct lpg_channel *chan, uint64_t period)
{
unsigned int i, pwm_resolution_count, best_pwm_resolution_sel = 0;
@@ -575,6 +697,112 @@ static void lpg_calc_duty(struct lpg_channel *chan, uint64_t duty)
chan->pwm_value = min(val, max);
}
+static u64 lpg_pwm_duration_numerator(const struct lpg_channel *chan, bool duty_cycle)
+{
+ unsigned int resolution;
+ unsigned int pwm_value;
+
+ if (chan->enable_freq_mode) {
+ u64 period_numerator;
+
+ period_numerator = PWM_FM_PERIOD_NUMERATOR(chan->pre_div_exp, chan->pwm_value);
+ return duty_cycle ? period_numerator / 2 : period_numerator;
+ }
+
+ if (chan->subtype == LPG_SUBTYPE_HI_RES_PWM)
+ resolution = lpg_pwm_resolution_hi_res[chan->pwm_resolution_sel];
+ else
+ resolution = lpg_pwm_resolution[chan->pwm_resolution_sel];
+
+ pwm_value = duty_cycle ? chan->pwm_value : BIT(resolution) - 1;
+ return (u64)NSEC_PER_SEC * pwm_value * lpg_pre_divs[chan->pre_div_sel] *
+ BIT(chan->pre_div_exp);
+}
+
+static bool lpg_pwm_config_has_smaller_timing_error(const struct lpg_channel *candidate,
+ const struct lpg_channel *best)
+{
+ const unsigned int *clk_rates;
+ unsigned int candidate_clk_rate;
+ unsigned int best_clk_rate;
+ u64 candidate_duration_numerator;
+ u64 best_duration_numerator;
+ int period_comparison;
+ int duty_comparison;
+
+ clk_rates = candidate->subtype == LPG_SUBTYPE_HI_RES_PWM ?
+ lpg_clk_rates_hi_res : lpg_clk_rates;
+ candidate_clk_rate = clk_rates[candidate->clk_sel];
+ best_clk_rate = clk_rates[best->clk_sel];
+
+ /*
+ * Both configurations are within the requested bounds, so the longer
+ * period has the smaller error. Use duty only to break a period tie.
+ */
+ candidate_duration_numerator =
+ lpg_pwm_duration_numerator(candidate, false);
+ best_duration_numerator =
+ lpg_pwm_duration_numerator(best, false);
+ period_comparison =
+ lpg_pwm_compare_candidate_duration(candidate_duration_numerator,
+ candidate_clk_rate,
+ best_duration_numerator,
+ best_clk_rate);
+ if (period_comparison)
+ return period_comparison > 0;
+
+ candidate_duration_numerator =
+ lpg_pwm_duration_numerator(candidate, true);
+ best_duration_numerator =
+ lpg_pwm_duration_numerator(best, true);
+
+ duty_comparison =
+ lpg_pwm_compare_candidate_duration(candidate_duration_numerator,
+ candidate_clk_rate,
+ best_duration_numerator,
+ best_clk_rate);
+
+ return duty_comparison > 0;
+}
+
+static int lpg_calc_pwm_state(struct lpg_channel *chan,
+ const struct pwm_state *state)
+{
+ struct lpg_channel standard = *chan;
+ struct lpg_channel frequency = *chan;
+ u64 half_period_ns = state->period / 2;
+ int ret;
+
+ standard.enable_freq_mode = false;
+ ret = lpg_calc_freq(&standard, state->period);
+ if (!ret)
+ lpg_calc_duty(&standard, state->duty_cycle);
+
+ if (chan->freq_mode_capable &&
+ (state->duty_cycle == half_period_ns ||
+ state->duty_cycle == half_period_ns + (state->period & 1)) &&
+ !lpg_calc_freq_in_freq_mode(&frequency, state->period,
+ state->duty_cycle)) {
+ frequency.enable_freq_mode = true;
+ if (ret) {
+ *chan = frequency;
+ return 0;
+ }
+
+ if (lpg_pwm_config_has_smaller_timing_error(&standard, &frequency))
+ *chan = standard;
+ else
+ *chan = frequency;
+
+ return 0;
+ }
+
+ if (!ret)
+ *chan = standard;
+
+ return ret;
+}
+
static void lpg_apply_freq(struct lpg_channel *chan)
{
unsigned long val;
@@ -815,11 +1043,50 @@ static void lpg_apply_dtest(struct lpg_channel *chan)
chan->dtest_value);
}
-static void lpg_apply(struct lpg_channel *chan)
+static int lpg_apply_freq_mode(struct lpg_channel *chan)
{
+ struct lpg *lpg = chan->lpg;
+ bool enable_freq_mode = false;
+
+ if (!chan->freq_mode_capable)
+ return 0;
+
+ if (chan->enable_freq_mode && chan->enabled)
+ enable_freq_mode = true;
+
+ return regmap_update_bits(lpg->map, chan->base + PWM_FM_MODE_REG,
+ PWM_FM_ENABLE,
+ enable_freq_mode ? PWM_FM_ENABLE : 0);
+}
+
+static int __lpg_apply(struct lpg_channel *chan, bool check_freq_mode)
+{
+ bool enable_freq_mode = chan->enable_freq_mode && chan->enabled;
+ unsigned int val;
+ int ret;
+
lpg_disable_glitch(chan);
+
+ if (check_freq_mode && chan->freq_mode_capable) {
+ ret = regmap_read(chan->lpg->map,
+ chan->base + PWM_FM_MODE_REG, &val);
+ if (ret)
+ goto out_glitch;
+
+ if (!!(val & PWM_FM_ENABLE) != enable_freq_mode) {
+ ret = regmap_update_bits(chan->lpg->map,
+ chan->base + PWM_ENABLE_CONTROL_REG,
+ LPG_ENABLE_CONTROL_OUTPUT, 0);
+ if (ret)
+ goto out_glitch;
+ }
+ }
+
lpg_apply_freq(chan);
lpg_apply_pwm_value(chan);
+ ret = lpg_apply_freq_mode(chan);
+ if (ret && check_freq_mode)
+ goto out_glitch;
lpg_apply_control(chan);
lpg_apply_sync(chan);
if (chan->lpg->lpg_chan_sdam)
@@ -827,6 +1094,17 @@ static void lpg_apply(struct lpg_channel *chan)
else
lpg_apply_lut_control(chan);
lpg_enable_glitch(chan);
+
+ return 0;
+
+out_glitch:
+ lpg_enable_glitch(chan);
+ return ret;
+}
+
+static void lpg_apply(struct lpg_channel *chan)
+{
+ __lpg_apply(chan, false);
}
static void lpg_brightness_set(struct lpg_led *led, struct led_classdev *cdev,
@@ -1388,28 +1666,30 @@ static int lpg_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
{
struct lpg *lpg = lpg_pwm_from_chip(chip);
struct lpg_channel *chan = &lpg->channels[pwm->hwpwm];
+ struct lpg_channel config;
int ret = 0;
if (state->polarity != PWM_POLARITY_NORMAL)
return -EINVAL;
- mutex_lock(&lpg->lock);
+ guard(mutex)(&lpg->lock);
+ config = *chan;
+ config.enable_freq_mode = false;
if (state->enabled) {
- ret = lpg_calc_freq(chan, state->period);
- if (ret < 0)
- goto out_unlock;
-
- lpg_calc_duty(chan, state->duty_cycle);
+ ret = lpg_calc_pwm_state(&config, state);
+ if (ret)
+ return ret;
}
- chan->enabled = state->enabled;
- lpg_apply(chan);
+ config.enabled = state->enabled;
+ ret = __lpg_apply(&config, true);
+ if (ret)
+ return ret;
-out_unlock:
- mutex_unlock(&lpg->lock);
+ *chan = config;
- return ret;
+ return 0;
}
static int lpg_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
@@ -1417,8 +1697,10 @@ static int lpg_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
{
struct lpg *lpg = lpg_pwm_from_chip(chip);
struct lpg_channel *chan = &lpg->channels[pwm->hwpwm];
+ bool freq_mode_enabled = false;
unsigned int resolution;
unsigned int pre_div;
+ unsigned int clk_idx;
unsigned int refclk;
unsigned int val;
unsigned int m;
@@ -1432,7 +1714,7 @@ static int lpg_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
return ret;
if (chan->subtype == LPG_SUBTYPE_HI_RES_PWM) {
- unsigned int clk_idx = FIELD_GET(PWM_CLK_SELECT_HI_RES_MASK, val);
+ clk_idx = FIELD_GET(PWM_CLK_SELECT_HI_RES_MASK, val);
if (clk_idx >= ARRAY_SIZE(lpg_clk_rates_hi_res))
return -EINVAL;
@@ -1440,7 +1722,8 @@ static int lpg_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
refclk = lpg_clk_rates_hi_res[clk_idx];
resolution = lpg_pwm_resolution_hi_res[FIELD_GET(PWM_SIZE_HI_RES_MASK, val)];
} else {
- refclk = lpg_clk_rates[FIELD_GET(PWM_CLK_SELECT_MASK, val)];
+ clk_idx = FIELD_GET(PWM_CLK_SELECT_MASK, val);
+ refclk = lpg_clk_rates[clk_idx];
resolution = lpg_pwm_resolution[FIELD_GET(PWM_SIZE_SELECT_MASK, val)];
}
@@ -1456,9 +1739,28 @@ static int lpg_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
if (ret)
return ret;
- state->period = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * ((1 << resolution) - 1) *
- pre_div * (1 << m), refclk);
- state->duty_cycle = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * pwm_value * pre_div * (1 << m), refclk);
+ if (chan->freq_mode_capable) {
+ ret = regmap_read(lpg->map, chan->base + PWM_FM_MODE_REG, &val);
+ if (ret)
+ return ret;
+
+ freq_mode_enabled = !!(val & PWM_FM_ENABLE);
+ }
+
+ if (freq_mode_enabled) {
+ u64 period_numerator;
+
+ pwm_value &= 0xff;
+ period_numerator = PWM_FM_PERIOD_NUMERATOR(m, pwm_value);
+ state->period = DIV_ROUND_UP_ULL(period_numerator, refclk);
+ state->duty_cycle = DIV_ROUND_UP_ULL(period_numerator / 2,
+ refclk);
+ } else {
+ state->period = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC *
+ ((1 << resolution) - 1) * pre_div * (1 << m), refclk);
+ state->duty_cycle = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC *
+ pwm_value * pre_div * (1 << m), refclk);
+ }
} else {
state->period = 0;
state->duty_cycle = 0;
@@ -1646,7 +1948,8 @@ static int lpg_init_channels(struct lpg *lpg)
{
const struct lpg_data *data = lpg->data;
struct lpg_channel *chan;
- int i;
+ unsigned int status;
+ int ret, i;
for (i = 0; i < data->num_channels; i++) {
chan = &lpg->channels[i];
@@ -1658,6 +1961,14 @@ static int lpg_init_channels(struct lpg *lpg)
chan->sdam_offset = data->channels[i].sdam_offset;
regmap_read(lpg->map, chan->base + LPG_SUBTYPE_REG, &chan->subtype);
+
+ if (chan->subtype == LPG_SUBTYPE_PWM && data->channels[i].freq_mode_capable) {
+ ret = regmap_read(lpg->map, chan->base + PWM_STATUS1_REG, &status);
+ if (ret < 0)
+ return ret;
+
+ chan->freq_mode_capable = !!(status & PWM_FM_PRESENT);
+ }
}
return 0;
@@ -1981,7 +2292,7 @@ static const struct lpg_data pm8350c_pwm_data = {
{ .base = 0xe800, .triled_mask = BIT(7), .sdam_offset = 0x48 },
{ .base = 0xe900, .triled_mask = BIT(6), .sdam_offset = 0x56 },
{ .base = 0xea00, .triled_mask = BIT(5), .sdam_offset = 0x64 },
- { .base = 0xeb00 },
+ { .base = 0xeb00, .freq_mode_capable = true },
},
};
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 3/4] leds: rgb: leds-qcom-lpg: add mutex lock in lpg_pwm_get_state()
2026-11-17 9:31 ` [PATCH 3/4] leds: rgb: leds-qcom-lpg: add mutex lock in lpg_pwm_get_state() Fenglin Wu
@ 2026-11-17 9:40 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 9:40 UTC (permalink / raw)
To: Fenglin Wu; +Cc: linux-leds, lee
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] leds: rgb: leds-qcom-lpg: subject capitalization and commit msg
rationale
--
commit 836798f643edbd8121d300d8532e2745d0e06b99
Author: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
> leds: rgb: leds-qcom-lpg: add mutex lock in lpg_pwm_get_state()
>
> Guard mutex lock at the top of lpg_pwm_get_state(), to make the
> register read atomic against the register write in lpg_pwm_apply().
>
> Fixes: 24e2d05d1b68 ("leds: Add driver for Qualcomm LPG")
> Signed-off-by: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
[Severity: Low]
Is the commit message accurate about what lpg->lock serializes against in
mainline?
Since 1cc2e1faafb3 ("pwm: Add more locking"), the PWM core serializes
chip->ops->apply() and chip->ops->get_state() under chip->nonatomic_lock
in pwm_apply_might_sleep() and pwm_get_state_hw(), so lpg_pwm_get_state()
and lpg_pwm_apply() cannot run concurrently. Taking lpg->lock in
lpg_pwm_get_state() still serializes pwm_dbg_show() -> pwm_get_state_hw()
against LED callbacks such as lpg_brightness_single_set(),
lpg_brightness_mc_set() and lpg_blink_set(), which program channel
registers via lpg_apply() under lpg->lock without holding
chip->nonatomic_lock (in addition to fixing the race with lpg_pwm_apply()
on stable kernels prior to 1cc2e1faafb3 ("pwm: Add more locking")).
Also, per LED subsystem convention, the subject description after the
driver prefix should be capitalized ("leds: rgb: leds-qcom-lpg: Add mutex
lock in lpg_pwm_get_state()").
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-led-qcom-leds-multiple-fixes-and-pwm-fm-support-v1-0-7a862240683f@oss.qualcomm.com?part=3
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak
2026-11-17 9:31 ` [PATCH 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
@ 2026-11-17 9:46 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 9:46 UTC (permalink / raw)
To: Fenglin Wu; +Cc: linux-leds, lee
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] leds: rgb: leds-qcom-lpg: single-entry LUT still treated as
no pattern
--
commit 75c60a80d1268626e9de041f3218e5d2d8274c04
Author: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak
This commit adds a has_lut flag to struct lpg_channel to track LUT
ownership separately from the pattern index range so that single-entry
LUT allocations are freed when clearing a pattern.
> diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
> index 24ffe079f2215..e14a3316bf221 100644
> --- a/drivers/leds/rgb/leds-qcom-lpg.c
> +++ b/drivers/leds/rgb/leds-qcom-lpg.c
[ ... ]
> @@ -1118,6 +1124,8 @@ static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
>
> chan->pattern_lo_idx = lo_idx;
> chan->pattern_hi_idx = hi_idx;
> + /* The 1st channel owns the LUT while other channels share it */
> + chan->has_lut = i == 0;
> }
[Severity: Medium]
Can a 2-step palindrome pattern actually run when ping_pong compresses
it to a single LUT entry with lo_idx == hi_idx?
Although lpg_lut_free() now checks chan->has_lut, the pattern activation
paths still check whether chan->pattern_lo_idx != chan->pattern_hi_idx:
drivers/leds/rgb/leds-qcom-lpg.c:lpg_brightness_set() {
...
if (brightness == LED_OFF) {
chan->enabled = false;
chan->ramp_enabled = false;
} else if (chan->pattern_lo_idx != chan->pattern_hi_idx) {
...
}
lpg_apply_control() and lpg_apply_lut_control() check the same condition,
so a single-entry LUT never enables the ramp generator and falls back to
static full-brightness PWM output. In addition, the comment earlier in
lpg_pattern_set() notes that a pattern of length 1 causes the hardware
to iterate through the entire LUT, suggesting ping_pong compression
should not reduce a 2-step pattern to actual_len == 1 in the first place.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-led-qcom-leds-multiple-fixes-and-pwm-fm-support-v1-0-7a862240683f@oss.qualcomm.com?part=1
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode
2026-11-17 9:31 ` [PATCH 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode Fenglin Wu
@ 2026-11-17 9:50 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 9:50 UTC (permalink / raw)
To: Fenglin Wu; +Cc: linux-leds, lee
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] leds: qcom-lpg: unupdated brightness in lpg_pattern_mc_set()
stops pattern
- [Medium] leds: qcom-lpg: lpg_pattern_mc_update() scales LUTs by
intensity not brightness
--
commit a4d32f1ef168124af51072d5038cd7e0df021524
Author: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode
This commit scales multicolor hardware patterns per LPG channel and caches
the normalized pattern so per-channel LUTs can be rebuilt when brightness
or multi_intensity changes.
> diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
> index e14a3316bf221..175b3680766f1 100644
> --- a/drivers/leds/rgb/leds-qcom-lpg.c
> +++ b/drivers/leds/rgb/leds-qcom-lpg.c
[ ... ]
> @@ -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;
> }
[ ... ]
> @@ -1152,24 +1197,133 @@ static int lpg_pattern_single_set(struct led_classdev *cdev,
[ ... ]
> +/* 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;
> + }
[Severity: Medium]
Should scaled[j].brightness multiply by mc->subled_info[i].brightness
instead of mc->subled_info[i].intensity?
When lpg_brightness_mc_set() runs, led_mc_calc_color_components() stores
the brightness-adjusted channel values in mc->subled_info[i].brightness,
leaving mc->subled_info[i].intensity unchanged. Using .intensity here
means changing brightness while a pattern is active never scales the
programmed LUT values; instead, channels stay at full intensity until
mc->subled_info[i].brightness rounds down to 0 and abruptly shuts the
channel off, skewing the color balance.
[ ... ]
> +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;
> }
[Severity: High]
Does leaving cdev->brightness at 0 in lpg_pattern_mc_set() cause a later
multi_intensity write to stop the running pattern?
cdev->brightness is set to 0 when the pattern trigger activates (or at
probe when default-state is not "on"), and pattern_trig_start_pattern() ->
lpg_pattern_mc_set() never updates it. If userspace then writes to
multi_intensity while the pattern is running, multi_intensity_store()
calls led_set_brightness(cdev, cdev->brightness) with 0, which invokes
lpg_brightness_mc_set(cdev, 0), zeroes every mc->subled_info[i].brightness,
frees all per-channel LUTs in lpg_pattern_mc_update(), and disables all
channels.
Also, on LUT-backed LPG hardware where cdev->max_brightness is 511
(LPG_RESOLUTION_9BIT - 1), passing LED_FULL (255) instead of
cdev->max_brightness to led_mc_calc_color_components() rounds
mc->subled_info[i].brightness down to 0 when intensity is 1, leaving that
channel disabled.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-led-qcom-leds-multiple-fixes-and-pwm-fm-support-v1-0-7a862240683f@oss.qualcomm.com?part=2
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support
2026-11-17 9:31 ` [PATCH 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support Fenglin Wu
@ 2026-11-17 9:51 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 9:51 UTC (permalink / raw)
To: Fenglin Wu; +Cc: linux-leds, lee
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] leds: rgb: leds-qcom-lpg: corrupted get_state() after
disabling freq mode
--
commit 2a10b372b4d1a0325069cf3509d6445ec569b2c0
Author: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
leds: rgb: leds-qcom-lpg: Add PWM frequency mode support
This commit adds Frequency Mode (FM) support for capable Qualcomm LPG PWM
channels when a 50% duty cycle is requested and Frequency Mode provides
better timing accuracy than standard PWM mode.
> diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
> index 971f929116063..68c73bce103c3 100644
> --- a/drivers/leds/rgb/leds-qcom-lpg.c
> +++ b/drivers/leds/rgb/leds-qcom-lpg.c
[ ... ]
> @@ -815,11 +1043,50 @@ static void lpg_apply_dtest(struct lpg_channel *chan)
> chan->dtest_value);
> }
>
> -static void lpg_apply(struct lpg_channel *chan)
> +static int lpg_apply_freq_mode(struct lpg_channel *chan)
> +{
> + struct lpg *lpg = chan->lpg;
> + bool enable_freq_mode = false;
> +
> + if (!chan->freq_mode_capable)
> + return 0;
> +
> + if (chan->enable_freq_mode && chan->enabled)
> + enable_freq_mode = true;
> +
> + return regmap_update_bits(lpg->map, chan->base + PWM_FM_MODE_REG,
> + PWM_FM_ENABLE,
> + enable_freq_mode ? PWM_FM_ENABLE : 0);
> +}
[Severity: Medium]
Does clearing PWM_FM_ENABLE in lpg_apply_freq_mode() when a channel is
disabled cause lpg_pwm_get_state() to report a corrupted period and
duty_cycle?
When lpg_pwm_apply() disables a channel that was running in Frequency
Mode, lpg_apply_freq() and lpg_apply_pwm_value() return early because
!chan->enabled, leaving the Frequency Mode register values in
LPG_SIZE_CLK_REG, LPG_PREDIV_CLK_REG and PWM_VALUE_REG. However,
lpg_apply_freq_mode() still runs and clears PWM_FM_ENABLE in
PWM_FM_MODE_REG (and lpg_pwm_apply() also resets config.enable_freq_mode
to false before checking state->enabled).
A subsequent lpg_pwm_get_state() call then sees PWM_FM_ENABLE == 0 and
decodes the leftover Frequency Mode register values with the standard PWM
formula, corrupting the initial state read back by pwm_device_request() if
the channel is re-requested or re-exported via sysfs. Should
lpg_apply_freq_mode() return early when !chan->enabled (matching
lpg_apply_freq() and lpg_apply_pwm_value()), with lpg_pwm_apply()
resetting config.enable_freq_mode only inside the if (state->enabled)
block?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-led-qcom-leds-multiple-fixes-and-pwm-fm-support-v1-0-7a862240683f@oss.qualcomm.com?part=4
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-17 9:51 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 9:31 [PATCH 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
2026-11-17 9:31 ` [PATCH 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
2026-11-17 9:46 ` sashiko-bot
2026-11-17 9:31 ` [PATCH 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode Fenglin Wu
2026-11-17 9:50 ` sashiko-bot
2026-11-17 9:31 ` [PATCH 3/4] leds: rgb: leds-qcom-lpg: add mutex lock in lpg_pwm_get_state() Fenglin Wu
2026-11-17 9:40 ` sashiko-bot
2026-11-17 9:31 ` [PATCH 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support Fenglin Wu
2026-11-17 9:51 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox