* [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support
@ 2026-11-23 6:28 Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Fenglin Wu @ 2026-11-23 6:28 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, Jonathan Corbet,
Shuah Khan, Randy Dunlap
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, linux-doc,
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>
---
Changes in v2:
- Fixed the review comments from Sashiko
1. Replace the ``pattern_lo_idx == chan->pattern_hi_idx`` checks with the
``has_lut`` flag, which is also used when releasing LUT allocations.
This correctly distinguishes a valid single-entry LUT from a channel
without a LUT.
2. In ``lpg_pattern_mc_update()``, scale multicolor patterns using each
sub-LED’s calculated ``brightness`` rather than its ``intensity``.
Document the resulting multicolor pattern-scaling behavior following
feedback from an OpenAI-assisted review.
3. Add a LUT allocation preflight in ``lpg_pattern_mc_update()`` to ensure
the complete replacement pattern fits in the LUT before stopping and
releasing the existing pattern.
4. Stop recalculating sub-LED brightness using LED_FULL in ``lpg_pattern_mc_set()``.
Scale the pattern using the existing calculated sub-LED brightness instead.
5. Update patch 3/4 commit message to clarify that the lock in lpg_pwm_get_state()
serializes register reads against register writes performed by the LED
callbacks.
6. In ``lpg_apply_freq_mode()``, preserve the PWM_FM_ENABLE bit when
disabling a channel so that a subsequent get_state() call can still
report the correct period and duty cycle.
- Link to v1: https://patch.msgid.link/20261117-led-qcom-leds-multiple-fixes-and-pwm-fm-support-v1-0-7a862240683f@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
Documentation/leds/leds-qcom-lpg.rst | 9 +
drivers/leds/rgb/leds-qcom-lpg.c | 699 ++++++++++++++++++++++++++++++-----
2 files changed, 623 insertions(+), 85 deletions(-)
---
base-commit: c016466c8c8b01af27ac66d97b64fa9c9e431a3d
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] 7+ messages in thread
* [PATCH v2 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak
2026-11-23 6:28 [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
@ 2026-11-23 6:28 ` Fenglin Wu
2026-11-23 6:43 ` sashiko-bot
2026-11-23 6:28 ` [PATCH v2 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode Fenglin Wu
` (3 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Fenglin Wu @ 2026-11-23 6:28 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, Jonathan Corbet,
Shuah Khan, Randy Dunlap
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, linux-doc,
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 whether each channel uses a LUT separately
from the index range. Mark every channel using the shared allocation,
release the bitmap allocation once through the first channel, and clear
the LUT state for all channels during cleanup.
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 | 28 ++++++++++++++++++----------
1 file changed, 18 insertions(+), 10 deletions(-)
diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
index 24ffe079f221..5d0d015d67e7 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 uses 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)
@@ -624,7 +630,7 @@ static void lpg_sdam_apply_lut_control(struct lpg_channel *chan)
unsigned int hi_pause, lo_pause;
struct lpg *lpg = chan->lpg;
- if (!chan->ramp_enabled || chan->pattern_lo_idx == chan->pattern_hi_idx)
+ if (!chan->ramp_enabled || !chan->has_lut)
return;
hi_pause = DIV_ROUND_UP(chan->ramp_hi_pause_ms, chan->ramp_tick_ms);
@@ -668,7 +674,7 @@ static void lpg_apply_lut_control(struct lpg_channel *chan)
unsigned int hi_idx = chan->pattern_hi_idx;
u16 step = chan->ramp_tick_ms;
- if (!chan->ramp_enabled || chan->pattern_lo_idx == chan->pattern_hi_idx)
+ if (!chan->ramp_enabled || !chan->has_lut)
return;
hi_pause = DIV_ROUND_UP(chan->ramp_hi_pause_ms, step);
@@ -709,7 +715,7 @@ static void lpg_apply_control(struct lpg_channel *chan)
if (chan->enabled)
ctrl |= LPG_ENABLE_CONTROL_OUTPUT;
- if (chan->pattern_lo_idx != chan->pattern_hi_idx)
+ if (chan->has_lut)
ctrl |= LPG_ENABLE_CONTROL_RAMP_GEN;
else
ctrl |= LPG_ENABLE_CONTROL_SRC_PWM;
@@ -818,7 +824,7 @@ static void lpg_brightness_set(struct lpg_led *led, struct led_classdev *cdev,
if (brightness == LED_OFF) {
chan->enabled = false;
chan->ramp_enabled = false;
- } else if (chan->pattern_lo_idx != chan->pattern_hi_idx) {
+ } else if (chan->has_lut) {
lpg_calc_freq(chan, NSEC_PER_MSEC);
lpg_sdam_configure_triggers(chan, 1);
@@ -1118,6 +1124,7 @@ 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;
+ chan->has_lut = true;
}
out_unlock:
@@ -1176,7 +1183,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];
@@ -1184,6 +1191,7 @@ static int lpg_pattern_clear(struct lpg_led *led)
lpg_clear_pbs_trigger(chan->lpg, chan->lut_mask);
chan->pattern_lo_idx = 0;
chan->pattern_hi_idx = 0;
+ chan->has_lut = false;
}
mutex_unlock(&lpg->lock);
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode
2026-11-23 6:28 [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
@ 2026-11-23 6:28 ` Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 3/4] leds: rgb: leds-qcom-lpg: Add mutex lock in lpg_pwm_get_state() Fenglin Wu
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Fenglin Wu @ 2026-11-23 6:28 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, Jonathan Corbet,
Shuah Khan, Randy Dunlap
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, linux-doc,
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>
---
Documentation/leds/leds-qcom-lpg.rst | 9 +
drivers/leds/rgb/leds-qcom-lpg.c | 324 ++++++++++++++++++++++++++++-------
2 files changed, 275 insertions(+), 58 deletions(-)
diff --git a/Documentation/leds/leds-qcom-lpg.rst b/Documentation/leds/leds-qcom-lpg.rst
index d6a76c38c581..e83d8dde7eab 100644
--- a/Documentation/leds/leds-qcom-lpg.rst
+++ b/Documentation/leds/leds-qcom-lpg.rst
@@ -39,6 +39,15 @@ described below). As the LPG hardware is not able to perform the linear
transitions expected by the leds-trigger-pattern format, each entry in the
pattern must be followed a zero-length entry of the same brightness.
+For multicolor LEDs, each pattern brightness value is scaled by the component
+brightness calculated from the global ``brightness`` and ``multi_intensity``
+values. A component whose calculated brightness is zero does not have a LUT
+range allocated. Consequently, if ``hw_pattern`` is configured while global
+brightness is zero, the pattern is cached but no LUT range is programmed until
+a brightness or intensity update produces a nonzero component brightness.
+Changing either value while a hardware pattern is active rebuilds the
+per-component LUT ranges.
+
Simple pattern::
"255 500 255 0 0 500 0 0"
diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
index 5d0d015d67e7..763d6379bd2e 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);
@@ -179,6 +203,7 @@ struct lpg_led {
* @lut_base: base address of the LUT block (optional)
* @lut_size: number of entries in the LUT block
* @lut_bitmap: allocation bitmap for LUT entries
+ * @lut_bitmap_tmp: scratch bitmap for LUT allocation preflight
* @pbs_dev: PBS device
* @lpg_chan_sdam: LPG SDAM peripheral device
* @lut_sdam: LUT SDAM peripheral device
@@ -203,6 +228,7 @@ struct lpg {
u32 lut_base;
u32 lut_size;
unsigned long *lut_bitmap;
+ unsigned long *lut_bitmap_tmp;
struct pbs_dev *pbs_dev;
struct nvmem_device *lpg_chan_sdam;
@@ -876,18 +902,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 +999,16 @@ 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 +1028,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 +1050,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 +1107,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,44 +1121,65 @@ 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;
chan->has_lut = true;
}
-out_unlock:
- mutex_unlock(&lpg->lock);
-out_free_pattern:
- kfree(pattern);
-
return ret;
}
@@ -1151,50 +1199,196 @@ 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 = lpg_pattern_set(led, pattern, len, repeat);
- if (ret < 0)
+ ret = triled_set(lpg, triled_mask, 0);
+ if (ret)
return ret;
- led_mc_calc_color_components(mc, LED_FULL);
- lpg_brightness_set(led, cdev, mc->subled_info);
+ 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;
}
-static int lpg_pattern_clear(struct lpg_led *led)
+/* Caller must hold lpg->lock. */
+static int lpg_pattern_mc_preflight(struct lpg_led *led)
{
+ struct led_classdev_mc *mc = &led->mcdev;
+ struct lpg_pattern *pattern = &led->pattern;
+ struct lpg *lpg = led->lpg;
struct lpg_channel *chan;
+ unsigned int idx;
+ unsigned int i;
+ int len;
+
+ bitmap_copy(lpg->lut_bitmap_tmp, lpg->lut_bitmap, lpg->lut_size);
+
+ for (i = 0; i < led->num_channels; i++) {
+ chan = led->channels[i];
+ if (!chan->has_lut)
+ continue;
+
+ len = chan->pattern_hi_idx - chan->pattern_lo_idx + 1;
+ bitmap_clear(lpg->lut_bitmap_tmp, chan->pattern_lo_idx, len);
+ }
+
+ for (i = 0; i < led->num_channels; i++) {
+ if (!mc->subled_info[i].brightness)
+ continue;
+
+ idx = bitmap_find_next_zero_area(lpg->lut_bitmap_tmp, lpg->lut_size,
+ 0, pattern->len, 0);
+ if (idx >= lpg->lut_size)
+ return lpg->lut_base ? -ENOMEM : -ENOSPC;
+
+ bitmap_set(lpg->lut_bitmap_tmp, idx, pattern->len);
+ }
+
+ 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;
- int i;
+ struct lpg_channel *chan;
+ unsigned int lo_idx, hi_idx;
+ unsigned int i, j;
+ int ret;
- mutex_lock(&lpg->lock);
+ ret = lpg_pattern_mc_preflight(led);
+ if (ret)
+ return ret;
- chan = led->channels[0];
- lpg_lut_free(chan);
+ ret = lpg_pattern_stop(led);
+ if (ret)
+ return ret;
+
+ for (i = 0; i < led->num_channels; i++)
+ lpg_lut_free(led->channels[i]);
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;
- chan->has_lut = false;
+
+ /* Ignore pattern config for 0 brightness channel */
+ if (!mc->subled_info[i].brightness)
+ continue;
+
+ /* Rebuild the pattern based on component brightness. */
+ for (j = 0; j < pattern->len; j++) {
+ scaled[j].brightness =
+ DIV_ROUND_CLOSEST((u32)pattern->data[j].brightness *
+ mc->subled_info[i].brightness,
+ 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;
}
- mutex_unlock(&lpg->lock);
+ 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;
+
+ ret = lpg_pattern_mc_update(led);
+ if (ret) {
+ led->pattern.len = 0;
+ return ret;
+ }
+
+ lpg_brightness_set(led, cdev, mc->subled_info);
+
+ return 0;
+}
+
+static int lpg_pattern_clear(struct lpg_led *led)
+{
+ struct lpg *lpg = led->lpg;
+ unsigned int i;
+ int ret;
+
+ guard(mutex)(&lpg->lock);
+
+ ret = lpg_pattern_stop(led);
+ if (ret)
+ return ret;
+
+ for (i = 0; i < led->num_channels; i++)
+ lpg_lut_free(led->channels[i]);
+ led->pattern.len = 0;
return 0;
}
@@ -1433,6 +1627,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;
}
@@ -1549,6 +1754,9 @@ static int lpg_init_lut(struct lpg *lpg)
lpg->lut_bitmap = devm_bitmap_zalloc(lpg->dev, lpg->lut_size, GFP_KERNEL);
if (!lpg->lut_bitmap)
return -ENOMEM;
+ lpg->lut_bitmap_tmp = devm_bitmap_zalloc(lpg->dev, lpg->lut_size, GFP_KERNEL);
+ if (!lpg->lut_bitmap_tmp)
+ return -ENOMEM;
return 0;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/4] leds: rgb: leds-qcom-lpg: Add mutex lock in lpg_pwm_get_state()
2026-11-23 6:28 [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode Fenglin Wu
@ 2026-11-23 6:28 ` Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support Fenglin Wu
2026-11-23 6:43 ` [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the " sashiko-bot
4 siblings, 0 replies; 7+ messages in thread
From: Fenglin Wu @ 2026-11-23 6:28 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, Jonathan Corbet,
Shuah Khan, Randy Dunlap
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, linux-doc,
Fenglin Wu
Guard mutex lock at the top of lpg_pwm_get_state(), to make the
register read atomic against the register write in LED callbacks.
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 763d6379bd2e..5d8f82ed228a 100644
--- a/drivers/leds/rgb/leds-qcom-lpg.c
+++ b/drivers/leds/rgb/leds-qcom-lpg.c
@@ -1470,6 +1470,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] 7+ messages in thread
* [PATCH v2 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support
2026-11-23 6:28 [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
` (2 preceding siblings ...)
2026-11-23 6:28 ` [PATCH v2 3/4] leds: rgb: leds-qcom-lpg: Add mutex lock in lpg_pwm_get_state() Fenglin Wu
@ 2026-11-23 6:28 ` Fenglin Wu
2026-11-23 6:43 ` [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the " sashiko-bot
4 siblings, 0 replies; 7+ messages in thread
From: Fenglin Wu @ 2026-11-23 6:28 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, Jonathan Corbet,
Shuah Khan, Randy Dunlap
Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
Pavel Machek, linux-leds, linux-kernel, linux-pwm, linux-doc,
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 5d8f82ed228a..0858001f5ae3 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 uses 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;
};
@@ -249,11 +259,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;
};
/**
@@ -450,6 +462,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;
@@ -577,6 +699,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;
@@ -817,11 +1045,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 || !chan->enabled)
+ 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)
@@ -829,6 +1096,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,
@@ -1433,28 +1711,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,
@@ -1462,8 +1742,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;
@@ -1477,7 +1759,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;
@@ -1485,7 +1767,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)];
}
@@ -1501,9 +1784,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;
@@ -1691,7 +1993,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];
@@ -1703,6 +2006,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;
@@ -2029,7 +2340,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] 7+ messages in thread
* Re: [PATCH v2 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak
2026-11-23 6:28 ` [PATCH v2 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
@ 2026-11-23 6:43 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-23 6:43 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: broken 1-entry LUT and hi_pause in
lpg_pattern_set()
--
commit 08fbbc17e523563ad041317b9d25935cac2226e0
Author: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak
This commit adds a has_lut boolean to struct lpg_channel so single-entry
LUT allocations where pattern_lo_idx equals pattern_hi_idx are properly
tracked and freed in lpg->lut_bitmap.
> diff --git a/drivers/leds/rgb/leds-qcom-lpg.c b/drivers/leds/rgb/leds-qcom-lpg.c
> index 24ffe079f2215..5d0d015d67e78 100644
> --- a/drivers/leds/rgb/leds-qcom-lpg.c
> +++ b/drivers/leds/rgb/leds-qcom-lpg.c
[ ... ]
> @@ -668,7 +674,7 @@ static void lpg_apply_lut_control(struct lpg_channel *chan)
> unsigned int hi_idx = chan->pattern_hi_idx;
> u16 step = chan->ramp_tick_ms;
>
> - if (!chan->ramp_enabled || chan->pattern_lo_idx == chan->pattern_hi_idx)
> + if (!chan->ramp_enabled || !chan->has_lut)
> return;
>
> hi_pause = DIV_ROUND_UP(chan->ramp_hi_pause_ms, step);
> @@ -709,7 +715,7 @@ static void lpg_apply_control(struct lpg_channel *chan)
> if (chan->enabled)
> ctrl |= LPG_ENABLE_CONTROL_OUTPUT;
>
> - if (chan->pattern_lo_idx != chan->pattern_hi_idx)
> + if (chan->has_lut)
> ctrl |= LPG_ENABLE_CONTROL_RAMP_GEN;
> else
> ctrl |= LPG_ENABLE_CONTROL_SRC_PWM;
[Severity: Medium]
Does enabling the ramp generator when pattern_lo_idx == pattern_hi_idx
cause the hardware to iterate across the entire LUT on a 2-step
palindrome pattern?
In lpg_pattern_set(), a 1-entry LUT pattern is explicitly prohibited for
that reason, but the check runs before ping-pong compression halves a
2-step palindrome (len == 2) down to actual_len == 1:
drivers/leds/rgb/leds-qcom-lpg.c:lpg_pattern_set() {
...
/*
* Specifying a pattern of length 1 causes the hardware to iterate
* through the entire LUT, so prohibit this.
*/
if (len < 2)
goto out_free_pattern;
...
/* The pattern length to be written to the LUT */
if (ping_pong)
actual_len = (len + 1) / 2;
else
actual_len = len;
...
if (lpg->lut_base || lpg->lut_sdam) {
lo_pause = pattern[0].delta_t;
hi_pause = pattern[actual_len - 1].delta_t;
...
}
Previously, the pattern_lo_idx != pattern_hi_idx checks prevented
programming LPG_LO_IDX_REG == LPG_HI_IDX_REG and enabling
LPG_ENABLE_CONTROL_RAMP_GEN when actual_len == 1. With chan->has_lut
replacing those checks, a 2-step palindrome now programs a 1-entry LUT
range into the ramp generator, and also sets hi_pause from
pattern[0].delta_t instead of pattern[1].delta_t.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261122-led-qcom-leds-multiple-fixes-and-pwm-fm-support-v2-0-2e885f60fbeb@oss.qualcomm.com?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support
2026-11-23 6:28 [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
` (3 preceding siblings ...)
2026-11-23 6:28 ` [PATCH v2 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support Fenglin Wu
@ 2026-11-23 6:43 ` sashiko-bot
4 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-23 6:43 UTC (permalink / raw)
To: Fenglin Wu; +Cc: linux-leds, lee
Sashiko has finished reviewing this 4-patch series:
- Patches 2-4: no issues found.
- Patch 1 ("leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak"): 1
potential issue (1 Medium)
A detailed review has been sent as a separate reply to patch 1.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261122-led-qcom-leds-multiple-fixes-and-pwm-fm-support-v2-0-2e885f60fbeb@oss.qualcomm.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-23 6:43 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 6:28 [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the PWM frequency mode support Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 1/4] leds: rgb: leds-qcom-lpg: Fix single-entry LUT leak Fenglin Wu
2026-11-23 6:43 ` sashiko-bot
2026-11-23 6:28 ` [PATCH v2 2/4] leds: rgb: leds-qcom-lpg: Fix LED color balancing in HW pattern mode Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 3/4] leds: rgb: leds-qcom-lpg: Add mutex lock in lpg_pwm_get_state() Fenglin Wu
2026-11-23 6:28 ` [PATCH v2 4/4] leds: rgb: leds-qcom-lpg: Add PWM frequency mode support Fenglin Wu
2026-11-23 6:43 ` [PATCH v2 0/4] leds: leds-qcom-lpg: Add several fixes and the " sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox