Linux LED subsystem development
 help / color / mirror / Atom feed
* [PATCH 0/2] leds: tps6131x: Fix torch-to-flash transitions
@ 2026-11-17  6:43 jiegu
  2026-11-17  6:43 ` [PATCH 1/2] leds: tps6131x: cancel torch refresh on flash mode switch jiegu
  2026-11-17  6:43 ` [PATCH 2/2] leds: tps6131x: clear torch brightness cache on flash takeover jiegu
  0 siblings, 2 replies; 5+ messages in thread
From: jiegu @ 2026-11-17  6:43 UTC (permalink / raw)
  To: Matthias Fend, Lee Jones, Pavel Machek; +Cc: linux-leds, linux-kernel, jiegu

Our product uses the TI TPS61311 white flash LED controller.
The flashlight can be controlled in two modes: flash and torch.
Currently, there is a conflict when the torch is enabled and the
camera is opened. The torch is turned off while the camera is
active, but it is automatically re-enabled after the watchdog
timeout expires.
Also, When the device switches from torch mode to flash or
external strobe mode, the hardware torch output is disabled,
but the LED class brightness cache may still keep the previous
non-zero torch value.

This series fixes both issues by cancelling the pending torch refresh
work before switching modes and clearing the cached torch brightness
after a successful mode change.

Tested on a custom product board using the TI TPS61311.

gujie (2):
  leds: tps6131x: cancel torch refresh on flash mode switch
  leds: tps6131x: clear torch brightness cache on flash takeover

 drivers/leds/flash/leds-tps6131x.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

-- 
2.43.0


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

* [PATCH 1/2] leds: tps6131x: cancel torch refresh on flash mode switch
  2026-11-17  6:43 [PATCH 0/2] leds: tps6131x: Fix torch-to-flash transitions jiegu
@ 2026-11-17  6:43 ` jiegu
  2026-11-17  7:04   ` sashiko-bot
  2026-11-17  6:43 ` [PATCH 2/2] leds: tps6131x: clear torch brightness cache on flash takeover jiegu
  1 sibling, 1 reply; 5+ messages in thread
From: jiegu @ 2026-11-17  6:43 UTC (permalink / raw)
  To: Matthias Fend, Lee Jones, Pavel Machek; +Cc: linux-leds, linux-kernel, gujie

From: gujie <jie.gu@leica-geosystems.com.cn>

The TPS6131X torch mode uses a delayed work item to refresh the watchdog
timer and keep torch mode active when driven over I2C. If userspace enables
torch and then the camera stack switches the LED into flash or external
strobe mode, the pending refresh work can still run later and force the
device back into torch mode.

Cancel the pending torch refresh work before switching into flash mode from
both the LED flash class strobe path and the V4L2 external strobe path.

Signed-off-by: gujie <jie.gu@leica-geosystems.com.cn>
---
 drivers/leds/flash/leds-tps6131x.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/leds/flash/leds-tps6131x.c b/drivers/leds/flash/leds-tps6131x.c
index 5c9a5af5af05..823e0462939c 100644
--- a/drivers/leds/flash/leds-tps6131x.c
+++ b/drivers/leds/flash/leds-tps6131x.c
@@ -378,6 +378,7 @@ static int tps6131x_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
 	struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
 	int ret;
 
+	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
 	guard(mutex)(&tps6131x->lock);
 
 	ret = tps6131x_set_mode(tps6131x, state ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
@@ -697,6 +698,7 @@ static int tps6131x_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, boo
 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
 	struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
 
+	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
 	guard(mutex)(&tps6131x->lock);
 
 	return tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
-- 
2.43.0


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

* [PATCH 2/2] leds: tps6131x: clear torch brightness cache on flash takeover
  2026-11-17  6:43 [PATCH 0/2] leds: tps6131x: Fix torch-to-flash transitions jiegu
  2026-11-17  6:43 ` [PATCH 1/2] leds: tps6131x: cancel torch refresh on flash mode switch jiegu
@ 2026-11-17  6:43 ` jiegu
  2026-11-17  7:03   ` sashiko-bot
  1 sibling, 1 reply; 5+ messages in thread
From: jiegu @ 2026-11-17  6:43 UTC (permalink / raw)
  To: Matthias Fend, Lee Jones, Pavel Machek; +Cc: linux-leds, linux-kernel, gujie

From: gujie <jie.gu@leica-geosystems.com.cn>

When the device switches from torch mode to flash or external strobe
mode, the hardware torch output is disabled, but the LED class
brightness cache may still keep the previous non-zero torch value.

This leaves sysfs brightness reporting an enabled torch state even
though the light is already off.

Update the cached LED brightness to LED_OFF when flash control takes
over so userspace state matches the actual hardware state.

Signed-off-by: gujie <jie.gu@leica-geosystems.com.cn>
---
 drivers/leds/flash/leds-tps6131x.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/drivers/leds/flash/leds-tps6131x.c b/drivers/leds/flash/leds-tps6131x.c
index 823e0462939c..4f708cf56178 100644
--- a/drivers/leds/flash/leds-tps6131x.c
+++ b/drivers/leds/flash/leds-tps6131x.c
@@ -294,6 +294,11 @@ static int tps6131x_set_mode(struct tps6131x *tps6131x, enum tps6131x_mode mode,
 				       NULL, false, force);
 }
 
+static void tps6131x_sync_torch_brightness_off(struct tps6131x *tps6131x)
+{
+	tps6131x->fled_cdev.led_cdev.brightness = LED_OFF;
+}
+
 static void tps6131x_torch_refresh_handler(struct work_struct *work)
 {
 	struct tps6131x *tps6131x = container_of(work, struct tps6131x, torch_refresh_work.work);
@@ -398,6 +403,8 @@ static int tps6131x_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
 	if (ret)
 		return ret;
 
+	tps6131x_sync_torch_brightness_off(tps6131x);
+
 	return 0;
 }
 
@@ -697,12 +704,19 @@ static int tps6131x_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, boo
 {
 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
 	struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
+	int ret;
 
 	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
 	guard(mutex)(&tps6131x->lock);
 
-	return tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
-				 false);
+	ret = tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
+				false);
+	if (ret)
+		return ret;
+
+	tps6131x_sync_torch_brightness_off(tps6131x);
+
+	return 0;
 }
 
 static const struct v4l2_flash_ops tps6131x_v4l2_flash_ops = {
-- 
2.43.0


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

* Re: [PATCH 2/2] leds: tps6131x: clear torch brightness cache on flash takeover
  2026-11-17  6:43 ` [PATCH 2/2] leds: tps6131x: clear torch brightness cache on flash takeover jiegu
@ 2026-11-17  7:03   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-17  7:03 UTC (permalink / raw)
  To: jiegu; +Cc: linux-leds, lee

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] leds: tps6131x: Signed-off-by trailer uses handle instead of
  real name
- [Medium] leds: tps6131x: race between torch brightness work and flash
  takeover
- [Low] leds: tps6131x: missing Fixes trailer for commit b338a2ae9b31
- [Low] leds: tps6131x: lowercase commit subject description after
  prefix
- [Low] leds: tps6131x: stale torch brightness on error in
  tps6131x_strobe_set()
--

commit f89ca151830eb0b217f7a4d48fff59fc543e3ca3
Author: gujie <jie.gu@leica-geosystems.com.cn>

> leds: tps6131x: clear torch brightness cache on flash takeover

[Severity: Low]
Per LED subsystem conventions, the description after the "leds: tps6131x: "
prefix should be capitalized ("leds: tps6131x: Clear torch brightness cache
on flash takeover").

>
> When the device switches from torch mode to flash or external strobe
> mode, the hardware torch output is disabled, but the LED class
> brightness cache may still keep the previous non-zero torch value.
>
> This leaves sysfs brightness reporting an enabled torch state even
> though the light is already off.
>
> Update the cached LED brightness to LED_OFF when flash control takes
> over so userspace state matches the actual hardware state.
>
> Signed-off-by: gujie <jie.gu@leica-geosystems.com.cn>

[Severity: High]
The Signed-off-by trailer and Author header use a single-word handle
("gujie") instead of a full real human name, which does not meet the
Developer's Certificate of Origin requirements in
Documentation/process/submitting-patches.rst.

[Severity: Low]
Since this fixes a bug introduced with the driver, should it include a
Fixes tag?

  Fixes: b338a2ae9b31 ("leds: tps6131x: Add support for Texas Instruments TPS6131X flash LED driver")

> diff --git a/drivers/leds/flash/leds-tps6131x.c b/drivers/leds/flash/leds-tps6131x.c
> index 704c3e7dfcda0..4b36fd2e3778b 100644
> --- a/drivers/leds/flash/leds-tps6131x.c
> +++ b/drivers/leds/flash/leds-tps6131x.c
> @@ -294,6 +294,11 @@ static int tps6131x_set_mode(struct tps6131x *tps6131x, enum tps6131x_mode mode,
>  				       NULL, false, force);
>  }
>  
> +static void tps6131x_sync_torch_brightness_off(struct tps6131x *tps6131x)
> +{
> +	tps6131x->fled_cdev.led_cdev.brightness = LED_OFF;
> +}
> +
>  static void tps6131x_torch_refresh_handler(struct work_struct *work)
>  {
>  	struct tps6131x *tps6131x = container_of(work, struct tps6131x, torch_refresh_work.work);
> @@ -398,6 +403,8 @@ static int tps6131x_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
>  	if (ret)
>  		return ret;
>  
> +	tps6131x_sync_torch_brightness_off(tps6131x);
> +
>  	return 0;
>  }

[Severity: Low]
Can fled_cdev.led_cdev.brightness still remain stale if a TPS6131X_REG_3
update fails in tps6131x_strobe_set()?

drivers/leds/flash/leds-tps6131x.c:tps6131x_strobe_set() {
    ...
	ret = tps6131x_set_mode(tps6131x, state ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
				true);
	if (ret < 0)
		return ret;

	if (state) {
		ret = regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_3, TPS6131X_REG_3_SFT,
					      TPS6131X_REG_3_SFT, NULL, false, true);
		if (ret)
			return ret;
	}
    ...
}

Once tps6131x_set_mode() succeeds, the hardware torch output is already
disabled.  If either subsequent regmap_update_bits_base() call on
TPS6131X_REG_3 fails, returning before tps6131x_sync_torch_brightness_off()
leaves fled_cdev.led_cdev.brightness reporting a non-zero torch value while
the torch is off.  Similarly, because cancel_delayed_work_sync() runs
before tps6131x_set_mode() here and in tps6131x_flash_external_strobe_set(),
a failure in tps6131x_set_mode() leaves torch_refresh_work canceled so the
hardware watchdog shuts the torch down after 13 seconds without updating
fled_cdev.led_cdev.brightness.

> @@ -697,12 +704,19 @@ static int tps6131x_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, boo
>  {
>  	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
>  	struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
> +	int ret;
>  
>  	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
>  	guard(mutex)(&tps6131x->lock);
>  
> -	return tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
> -				 false);
> +	ret = tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
> +				false);
> +	if (ret)
> +		return ret;
> +
> +	tps6131x_sync_torch_brightness_off(tps6131x);
> +
> +	return 0;
>  }

[Severity: Medium]
Can a concurrent torch brightness update race with flash takeover and
re-enable hardware torch mode after fled_cdev.led_cdev.brightness has been
reset to LED_OFF?

Because tps6131x_led_class_setup() registers brightness_set_blocking,
writing a non-zero value to sysfs brightness updates led_cdev.brightness
immediately and queues set_brightness_work to run tps6131x_brightness_set()
asynchronously.  In addition, tps6131x_strobe_set() and
tps6131x_flash_external_strobe_set() call cancel_delayed_work_sync() on
torch_refresh_work before acquiring tps6131x->lock, whereas
tps6131x_brightness_set() schedules torch_refresh_work under tps6131x->lock:

CPU0 (kworker)                    CPU1 (strobe / v4l2)
tps6131x_brightness_set()
  guard(mutex)(&tps6131x->lock)
  tps6131x_set_mode(..., TORCH)
                                  tps6131x_strobe_set()
                                    cancel_delayed_work_sync(...)
                                    guard(mutex)(&tps6131x->lock)
  schedule_delayed_work(...)
                                    tps6131x_set_mode(..., FLASH)
                                    tps6131x_sync_torch_brightness_off()
                                      led_cdev.brightness = LED_OFF
tps6131x_torch_refresh_handler()
  tps6131x_set_mode(..., TORCH)  <-- torch re-enabled while brightness == 0

The same mismatch happens if set_brightness_work runs
tps6131x_brightness_set() with a queued non-zero delayed_set_value right
after tps6131x_strobe_set() or tps6131x_flash_external_strobe_set() clears
led_cdev.brightness: the hardware switches back to TPS6131X_MODE_TORCH
while sysfs brightness still reports LED_OFF.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117064328.4096518-1-jie.gu@leica-geosystems.com?part=2

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

* Re: [PATCH 1/2] leds: tps6131x: cancel torch refresh on flash mode switch
  2026-11-17  6:43 ` [PATCH 1/2] leds: tps6131x: cancel torch refresh on flash mode switch jiegu
@ 2026-11-17  7:04   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-17  7:04 UTC (permalink / raw)
  To: jiegu; +Cc: linux-leds, lee

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] leds: tps6131x: username handle used in Signed-off-by trailer
- [Medium] leds: tps6131x: torch_refresh_work race in
  tps6131x_strobe_set()
- [Low] leds: tps6131x: missing Fixes trailer for commit b338a2ae9b31
- [Low] leds: tps6131x: uncapitalized commit subject description
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] leds: tps6131x: use-after-free in
  tps6131x_torch_refresh_handler() [1]
--

commit eca7ab46a5d8dad2ca9e23fede2225cbcb7e5c34
Author: gujie <jie.gu@leica-geosystems.com.cn>

> leds: tps6131x: cancel torch refresh on flash mode switch

[Severity: Low]
In the LED subsystem, the commit subject description after the prefix
should be capitalized:

  leds: tps6131x: Cancel torch refresh on flash mode switch

>
> The TPS6131X torch mode uses a delayed work item to refresh the watchdog
> timer and keep torch mode active when driven over I2C. If userspace enables
> torch and then the camera stack switches the LED into flash or external
> strobe mode, the pending refresh work can still run later and force the
> device back into torch mode.
>
> Cancel the pending torch refresh work before switching into flash mode from
> both the LED flash class strobe path and the V4L2 external strobe path.
>
> Signed-off-by: gujie <jie.gu@leica-geosystems.com.cn>

[Severity: High]
The Author header and Signed-off-by trailer use an account handle (gujie)
rather than a full real human name (for example, Jie Gu
<jie.gu@leica-geosystems.com.cn>), which is required for Developer's
Certificate of Origin compliance.

[Severity: Low]
Since this fixes a bug in the initial driver commit, should it include a
Fixes tag?

  Fixes: b338a2ae9b31 ("leds: tps6131x: Add support for Texas Instruments TPS6131X flash LED driver")

> diff --git a/drivers/leds/flash/leds-tps6131x.c b/drivers/leds/flash/leds-tps6131x.c
> index 7f2c3895e6e98..704c3e7dfcda0 100644
> --- a/drivers/leds/flash/leds-tps6131x.c
> +++ b/drivers/leds/flash/leds-tps6131x.c
> @@ -378,6 +378,7 @@ static int tps6131x_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
>  	struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
>  	int ret;
>  
> +	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
>  	guard(mutex)(&tps6131x->lock);
>  
>  	ret = tps6131x_set_mode(tps6131x, state ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
> @@ -697,6 +698,7 @@ static int tps6131x_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, boo
>  	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
>  	struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
>  
> +	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
>  	guard(mutex)(&tps6131x->lock);
>  
>  	return tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,

[Severity: Medium]
Can tps6131x_brightness_set() still re-arm torch_refresh_work after
tps6131x_strobe_set() or tps6131x_flash_external_strobe_set() cancels it?

tps6131x_brightness_set() is registered as brightness_set_blocking, so
writing to the brightness sysfs attribute queues set_brightness_work and
returns before tps6131x_brightness_set() runs.  Inside
tps6131x_brightness_set(), torch_refresh_work is scheduled at the end of
the tps6131x->lock critical section after blocking I2C writes:

drivers/leds/flash/leds-tps6131x.c:tps6131x_brightness_set() {
    ...
	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
    ...
	guard(mutex)(&tps6131x->lock);
    ...
	if (brightness)
		schedule_delayed_work(&tps6131x->torch_refresh_work,
				      TPS6131X_TORCH_REFRESH_INTERVAL_JIFFIES);
}

Because the strobe callbacks call cancel_delayed_work_sync() before
acquiring tps6131x->lock, they can interleave with an in-flight
tps6131x_brightness_set():

CPU0 (leds_wq)                    CPU1 (strobe)
tps6131x_brightness_set()
  mutex_lock(&tps6131x->lock)
  tps6131x_set_mode(..., TORCH)
                                  tps6131x_strobe_set()
                                    cancel_delayed_work_sync()
                                    mutex_lock(&tps6131x->lock)
  schedule_delayed_work()
  mutex_unlock(&tps6131x->lock)
                                    tps6131x_set_mode(..., FLASH)
                                    mutex_unlock(&tps6131x->lock)
                                  tps6131x_torch_refresh_handler()
                                    tps6131x_set_mode(..., TORCH)
                                      <-- forces LED back into torch mode

When torch_refresh_work fires 10 seconds later,
tps6131x_torch_refresh_handler() unconditionally switches the controller
back to TPS6131X_MODE_TORCH and re-schedules itself.

[Severity: Critical]
This problem wasn't introduced by this patch, but can torch_refresh_work
cause a use-after-free in tps6131x_torch_refresh_handler() during device
removal [1]?

tps6131x_remove() calls cancel_delayed_work_sync() before returning, while
the LED class device is registered with
devm_led_classdev_flash_register_ext() and therefore stays exposed in
sysfs until devres runs after tps6131x_remove():

drivers/leds/flash/leds-tps6131x.c:tps6131x_remove() {
	struct tps6131x *tps6131x = i2c_get_clientdata(client);

	v4l2_flash_release(tps6131x->v4l2_flash);

	cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
}

A sysfs brightness write during that window can re-arm torch_refresh_work:

CPU0                              CPU1
tps6131x_remove()
  cancel_delayed_work_sync()
                                  tps6131x_brightness_set()
                                    schedule_delayed_work()
devres_release_all()
  led_classdev_unregister()
  kfree(tps6131x)
                                  tps6131x_torch_refresh_handler()
                                    <-- use-after-free

The same issue occurs if tps6131x_v4l2_setup() fails in tps6131x_probe(),
which returns without canceling torch_refresh_work before devres frees
tps6131x.  Registering torch_refresh_work with devm_delayed_work_autocancel()
after devm_led_classdev_flash_register_ext() would ensure the work is
canceled after the LED class device is unregistered.

[1] https://sashiko.dev/#/bug/linux-fe5d04b6-6727-4f3e-ba44-48a355c973f5

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117064328.4096518-1-jie.gu@leica-geosystems.com?part=1

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

end of thread, other threads:[~2026-11-17  7:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  6:43 [PATCH 0/2] leds: tps6131x: Fix torch-to-flash transitions jiegu
2026-11-17  6:43 ` [PATCH 1/2] leds: tps6131x: cancel torch refresh on flash mode switch jiegu
2026-11-17  7:04   ` sashiko-bot
2026-11-17  6:43 ` [PATCH 2/2] leds: tps6131x: clear torch brightness cache on flash takeover jiegu
2026-11-17  7:03   ` sashiko-bot

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