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

This series fixes torch-to-flash transitions in the TPS6131x driver
by cancelling pending torch refresh work and clearing the cached
torch brightness when flash control takes over.

Matthias Fend reviewed and tested v1 on an i.MX8MP-EVK with a
TPS61310EVM. Thanks to Matthias for reviewing and testing, and to Lee
Jones for the review comments.

Changes in v2:
- Use my full name in the author and Signed-off-by fields.
- Capitalize the commit subjects after the driver prefix.
- Update the cached torch brightness directly at the call sites
  instead of using a helper.
- Add Fixes tags.
- Carry forward Matthias Fend's Reviewed-by and Tested-by tags.
 
v1: https://lore.kernel.org/linux-leds/20261117064328.4096518-1-jie.gu@leica-geosystems.com/T/

Jie Gu (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 | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

-- 
2.43.0


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

* [PATCH v2 1/2] leds: tps6131x: Cancel torch refresh on flash mode switch
  2026-11-20  7:58 [PATCH v2 0/2] leds: tps6131x: Fix torch-to-flash transitions Jie Gu
@ 2026-11-20  7:58 ` Jie Gu
  2026-11-20  8:09   ` sashiko-bot
  2026-11-20  7:58 ` [PATCH v2 2/2] leds: tps6131x: Clear torch brightness cache on flash takeover Jie Gu
  1 sibling, 1 reply; 5+ messages in thread
From: Jie Gu @ 2026-11-20  7:58 UTC (permalink / raw)
  To: Matthias Fend, Lee Jones, Pavel Machek; +Cc: linux-leds, linux-kernel, Jie Gu

From: Jie Gu <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.

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

Reviewed-by: Matthias Fend <matthias.fend@emfend.at>
Tested-by: Matthias Fend <matthias.fend@emfend.at>
Signed-off-by: Jie Gu <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 v2 2/2] leds: tps6131x: Clear torch brightness cache on flash takeover
  2026-11-20  7:58 [PATCH v2 0/2] leds: tps6131x: Fix torch-to-flash transitions Jie Gu
  2026-11-20  7:58 ` [PATCH v2 1/2] leds: tps6131x: Cancel torch refresh on flash mode switch Jie Gu
@ 2026-11-20  7:58 ` Jie Gu
  2026-11-20  8:09   ` sashiko-bot
  1 sibling, 1 reply; 5+ messages in thread
From: Jie Gu @ 2026-11-20  7:58 UTC (permalink / raw)
  To: Matthias Fend, Lee Jones, Pavel Machek; +Cc: linux-leds, linux-kernel, Jie Gu

From: Jie Gu <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.

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

Reviewed-by: Matthias Fend <matthias.fend@emfend.at>
Tested-by: Matthias Fend <matthias.fend@emfend.at>
Signed-off-by: Jie Gu <jie.gu@leica-geosystems.com.cn>
---
 drivers/leds/flash/leds-tps6131x.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/leds/flash/leds-tps6131x.c b/drivers/leds/flash/leds-tps6131x.c
index 823e0462939c..21957e4e3db4 100644
--- a/drivers/leds/flash/leds-tps6131x.c
+++ b/drivers/leds/flash/leds-tps6131x.c
@@ -398,6 +398,8 @@ static int tps6131x_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
 	if (ret)
 		return ret;
 
+	fled_cdev->led_cdev.brightness = LED_OFF;
+
 	return 0;
 }
 
@@ -697,12 +699,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;
+
+	fled_cdev->led_cdev.brightness = LED_OFF;
+
+	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 v2 2/2] leds: tps6131x: Clear torch brightness cache on flash takeover
  2026-11-20  7:58 ` [PATCH v2 2/2] leds: tps6131x: Clear torch brightness cache on flash takeover Jie Gu
@ 2026-11-20  8:09   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-20  8:09 UTC (permalink / raw)
  To: Jie Gu; +Cc: linux-leds, lee

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] leds: tps6131x: race between torch brightness work and flash
  takeover
- [Low] leds: tps6131x: stale torch brightness on error in
  tps6131x_strobe_set()
--

commit 97db4f98d0889a2079f12eddeb42ef27c0ba097d
Author: Jie Gu <jie.gu@leica-geosystems.com.cn>

leds: tps6131x: Clear torch brightness cache on flash takeover

This commit resets fled_cdev->led_cdev.brightness to LED_OFF when
switching to flash or shutdown mode in tps6131x_strobe_set() and
tps6131x_flash_external_strobe_set().

> diff --git a/drivers/leds/flash/leds-tps6131x.c b/drivers/leds/flash/leds-tps6131x.c
> index 704c3e7dfcda0..76f452842f1bd 100644
> --- a/drivers/leds/flash/leds-tps6131x.c
> +++ b/drivers/leds/flash/leds-tps6131x.c
> @@ -398,6 +398,8 @@ static int tps6131x_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
>  	if (ret)
>  		return ret;
>  
> +	fled_cdev->led_cdev.brightness = LED_OFF;
> +
>  	return 0;
>  }
>  

[Severity: Low]
Can fled_cdev->led_cdev.brightness remain stale if one of the subsequent
TPS6131X_REG_3 writes in tps6131x_strobe_set() fails?

tps6131x_set_mode() has already switched the hardware out of
TPS6131X_MODE_TORCH before the TPS6131X_REG_3_SFT updates run, so an early
return on those writes leaves the torch off while brightness still reports
the old non-zero value.  In addition, both tps6131x_strobe_set() and
tps6131x_flash_external_strobe_set() cancel torch_refresh_work before
calling tps6131x_set_mode(), so if tps6131x_set_mode() itself fails, the
13-second hardware watchdog will shut the torch down without brightness
being cleared.

> @@ -697,12 +699,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;
> +
> +	fled_cdev->led_cdev.brightness = LED_OFF;
> +
> +	return 0;
>  }
>  
>  static const struct v4l2_flash_ops tps6131x_v4l2_flash_ops = {

[Severity: Medium]
Can a concurrent torch brightness update re-enable TPS6131X_MODE_TORCH
after tps6131x_strobe_set() or tps6131x_flash_external_strobe_set() sets
fled_cdev->led_cdev.brightness to LED_OFF?

Because tps6131x uses brightness_set_blocking, led_set_brightness_nosleep()
updates led_cdev->brightness and queues set_brightness_work asynchronously.
In addition, cancel_delayed_work_sync(&tps6131x->torch_refresh_work) runs
before acquiring tps6131x->lock, while tps6131x_brightness_set() schedules
torch_refresh_work under tps6131x->lock:

CPU0 (leds_wq)                    CPU1 (strobe / v4l2_flash)
tps6131x_brightness_set()
  guard(mutex)(&tps6131x->lock)
  tps6131x_set_mode(...,
    TPS6131X_MODE_TORCH, true)
                                  tps6131x_strobe_set()
                                    cancel_delayed_work_sync(
                                      &tps6131x->torch_refresh_work)
  schedule_delayed_work(
    &tps6131x->torch_refresh_work,
    ...)
                                    guard(mutex)(&tps6131x->lock)
                                    tps6131x_set_mode(...,
                                      TPS6131X_MODE_FLASH, true)
                                    fled_cdev->led_cdev.brightness = LED_OFF

tps6131x_torch_refresh_handler()
  tps6131x_set_mode(...,
    TPS6131X_MODE_TORCH, true)  <-- forces torch back on while brightness is 0

Similarly, if set_brightness_work was already queued with a non-zero
delayed_set_value before the strobe callback ran, set_brightness_delayed()
can execute tps6131x_brightness_set() right after the strobe callback
clears fled_cdev->led_cdev.brightness, turning the hardware torch on while
sysfs brightness stays at LED_OFF.

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

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

* Re: [PATCH v2 1/2] leds: tps6131x: Cancel torch refresh on flash mode switch
  2026-11-20  7:58 ` [PATCH v2 1/2] leds: tps6131x: Cancel torch refresh on flash mode switch Jie Gu
@ 2026-11-20  8:09   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-20  8:09 UTC (permalink / raw)
  To: Jie Gu; +Cc: linux-leds, lee

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] leds: tps6131x: torch_refresh_work race with
  tps6131x_brightness_set()
--

commit 7abce202322d69429cb0363b0f5444e8321b3409
Author: Jie Gu <jie.gu@leica-geosystems.com.cn>

leds: tps6131x: Cancel torch refresh on flash mode switch

This commit cancels pending torch_refresh_work before switching the
TPS6131X mode in tps6131x_strobe_set() and
tps6131x_flash_external_strobe_set().

> 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 an in-flight tps6131x_brightness_set() still re-arm
torch_refresh_work after cancel_delayed_work_sync() returns in
tps6131x_strobe_set() or tps6131x_flash_external_strobe_set()?

tps6131x_brightness_set() runs asynchronously on leds_wq via
led_cdev->set_brightness_work without holding led_cdev->led_access, and
neither flash_strobe_store() nor v4l2_flash_open() flushes
set_brightness_work:

kworker (leds_wq)                 CPU1
set_brightness_delayed()
  tps6131x_brightness_set()
    cancel_delayed_work_sync(...)
    guard(mutex)(&tps6131x->lock)
    regmap_update_bits(...)
                                  tps6131x_strobe_set()
                                    cancel_delayed_work_sync(...)
                                    guard(mutex)(&tps6131x->lock)
    schedule_delayed_work(...)
    /* unlock tps6131x->lock */
                                    tps6131x_set_mode(...,
                                      TPS6131X_MODE_FLASH, ...)
                                    /* unlock tps6131x->lock */
tps6131x_torch_refresh_handler()
  tps6131x_set_mode(...,
    TPS6131X_MODE_TORCH, true)  <-- forces device back into torch mode

When torch_refresh_work fires later, tps6131x_torch_refresh_handler()
unconditionally writes TPS6131X_MODE_TORCH and re-arms itself.

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

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

end of thread, other threads:[~2026-11-20  8:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  7:58 [PATCH v2 0/2] leds: tps6131x: Fix torch-to-flash transitions Jie Gu
2026-11-20  7:58 ` [PATCH v2 1/2] leds: tps6131x: Cancel torch refresh on flash mode switch Jie Gu
2026-11-20  8:09   ` sashiko-bot
2026-11-20  7:58 ` [PATCH v2 2/2] leds: tps6131x: Clear torch brightness cache on flash takeover Jie Gu
2026-11-20  8:09   ` sashiko-bot

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