Skip to contentSkip to conversation

leds: lp5860: fix LED teardown ordering

PATCH Superseded [email protected] 2 participants 2 messages Jul 20 – Aug 6 LED

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: lp5860: fix LED teardown ordering

From: surendra <[email protected]>

The driver disables the chip from its remove callback before devres unregisters the LEDs. LED unregistration turns the LEDs off through the driver brightness callback, which can then access disabled hardware.

Unregister the LEDs before disabling the chip, including when probe fails after some LEDs have been registered. Keep track of registered LEDs so the devres actions can be released explicitly.

Also remove the early mutex destruction from the SPI remove callback:
hardware teardown takes the same mutex.

Fixes f0a66563aa2d ("leds: Add support for TI LP5860 LED driver chip")
---
 drivers/leds/rgb/leds-lp5860-core.c | 13 ++++++++++---
 drivers/leds/rgb/leds-lp5860-spi.c  |  4 ----
 drivers/leds/rgb/leds-lp5860.h      |  1 +
 3 files changed, 11 insertions(+), 7 deletions(-)
drivers/leds/rgb/leds-lp5860-core.c +10 -3
index 75498311b080..9646d3985239 100644
@@ -149,12 +149,11 @@ static int lp5860_init_dt(struct lp5860 *lp)
 	struct led_classdev *led_cdev;
 	struct mc_subled *mc_led_info;
 	struct lp5860_led *led;
-	int led_index = 0;
 	int chan;
 	int ret;
 
 	device_for_each_child_node_scoped(lp->dev, multi_led) {
-		led = &lp->leds[led_index];
+		led = &lp->leds[lp->num_leds];
 
 		init_data.fwnode = multi_led;
 
@@ -188,12 +187,18 @@ static int lp5860_init_dt(struct lp5860 *lp)
 		if (ret)
 			return dev_err_probe(lp->dev, ret, "%pfwP: Failed to register Multi-Color LEDs\n",
 					     multi_led);
-		led_index++;
+		lp->num_leds++;
 	}
 
 	return 0;
 }
 
+static void lp5860_unregister_leds(struct lp5860 *lp)
+{
+	while (lp->num_leds)
+		devm_led_classdev_multicolor_unregister(lp->dev, &lp->leds[--lp->num_leds].mc_cdev);
+}
+
 int lp5860_device_init(struct device *dev)
 {
 	struct lp5860 *lp = dev_get_drvdata(dev);
@@ -221,6 +226,7 @@ int lp5860_device_init(struct device *dev)
 	return 0;
 
 err_disable:
+	lp5860_unregister_leds(lp);
 	lp5860_chip_enable(lp, LP5860_CHIP_DISABLE);
 	return ret;
 }
@@ -230,6 +236,7 @@ void lp5860_device_remove(struct device *dev)
 {
 	struct lp5860 *lp = dev_get_drvdata(dev);
 
+	lp5860_unregister_leds(lp);
 	lp5860_chip_enable(lp, LP5860_CHIP_DISABLE);
 }
 EXPORT_SYMBOL_GPL(lp5860_device_remove);
drivers/leds/rgb/leds-lp5860-spi.c +0 -4
index 5e0c44854a68..e1565eac90f9 100644
@@ -70,10 +70,6 @@ static int lp5860_probe(struct spi_device *spi)
 
 static void lp5860_remove(struct spi_device *spi)
 {
-	struct lp5860 *lp5860 = spi_get_drvdata(spi);
-
-	mutex_destroy(&lp5860->lock);
-
 	lp5860_device_remove(&spi->dev);
 }
 
drivers/leds/rgb/leds-lp5860.h +1 -0
index 940be0c6e8da..ce30fb5596c0 100644
@@ -258,6 +258,7 @@ struct lp5860 {
 	struct device *dev;
 	struct regmap *regmap;
 	struct mutex lock;
+	unsigned int num_leds;
 
 	DECLARE_FLEX_ARRAY(struct lp5860_led, leds);
 };
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 10 lines · On Mon, 20 Jul 2026, [email protected] wrote:

From: surendra <[email protected]>

The driver disables the chip from its remove callback before devres unregisters the LEDs. LED unregistration turns the LEDs off through the driver brightness callback, which can then access disabled hardware.

Unregister the LEDs before disabling the chip, including when probe fails after some LEDs have been registered. Keep track of registered LEDs so the devres actions can be released explicitly.

Quoted 5 lines

Also remove the early mutex destruction from the SPI remove callback:
hardware teardown takes the same mutex.

Fixes f0a66563aa2d ("leds: Add support for TI LP5860 LED driver chip")

Submissions should include your full name.

Quoted 30 lines
---
 drivers/leds/rgb/leds-lp5860-core.c | 13 ++++++++++---
 drivers/leds/rgb/leds-lp5860-spi.c  |  4 ----
 drivers/leds/rgb/leds-lp5860.h      |  1 +
 3 files changed, 11 insertions(+), 7 deletions(-)
drivers/leds/rgb/leds-lp5860-core.c +2 -3
index 75498311b080..9646d3985239 100644
@@ -149,12 +149,11 @@ static int lp5860_init_dt(struct lp5860 *lp)
 	struct led_classdev *led_cdev;
 	struct mc_subled *mc_led_info;
 	struct lp5860_led *led;
-	int led_index = 0;
 	int chan;
 	int ret;
 
 	device_for_each_child_node_scoped(lp->dev, multi_led) {
-		led = &lp->leds[led_index];
+		led = &lp->leds[lp->num_leds];
 
 		init_data.fwnode = multi_led;
 
@@ -188,12 +187,18 @@ static int lp5860_init_dt(struct lp5860 *lp)
 		if (ret)
 			return dev_err_probe(lp->dev, ret, "%pfwP: Failed to register Multi-Color LEDs\n",
 					     multi_led);
-		led_index++;
+		lp->num_leds++;

drivers/leds/rgb/leds-lp5860-core.c static int lp5860_init_dt(struct lp5860 *lp) · line 200

Quoted 10 lines
drivers/leds/rgb/leds-lp5860-core.c +5 -0
 	}
 
 	return 0;
 }
 
+static void lp5860_unregister_leds(struct lp5860 *lp)
+{
+	while (lp->num_leds)
+		devm_led_classdev_multicolor_unregister(lp->dev, &lp->leds[--lp->num_leds].mc_cdev);
+}

Please look into using 'devm_add_action_or_reset()' to handle the chip disable sequence. Doing so might allow the managed resource framework to handle the teardown order naturally, potentially letting us avoid manual unregistration and even remove the '.remove' callback entirely.

Quoted 49 lines
drivers/leds/rgb/leds-lp5860-core.c +2 -0
 int lp5860_device_init(struct device *dev)
 {
 	struct lp5860 *lp = dev_get_drvdata(dev);
@@ -221,6 +226,7 @@ int lp5860_device_init(struct device *dev)
 	return 0;
 
 err_disable:
+	lp5860_unregister_leds(lp);
 	lp5860_chip_enable(lp, LP5860_CHIP_DISABLE);
 	return ret;
 }
@@ -230,6 +236,7 @@ void lp5860_device_remove(struct device *dev)
 {
 	struct lp5860 *lp = dev_get_drvdata(dev);
 
+	lp5860_unregister_leds(lp);
 	lp5860_chip_enable(lp, LP5860_CHIP_DISABLE);
 }
 EXPORT_SYMBOL_GPL(lp5860_device_remove);
drivers/leds/rgb/leds-lp5860-spi.c +0 -4
index 5e0c44854a68..e1565eac90f9 100644
@@ -70,10 +70,6 @@ static int lp5860_probe(struct spi_device *spi)
 
 static void lp5860_remove(struct spi_device *spi)
 {
-	struct lp5860 *lp5860 = spi_get_drvdata(spi);
-
-	mutex_destroy(&lp5860->lock);
-
 	lp5860_device_remove(&spi->dev);
 }
 
drivers/leds/rgb/leds-lp5860.h +1 -0
index 940be0c6e8da..ce30fb5596c0 100644
@@ -258,6 +258,7 @@ struct lp5860 {
 	struct device *dev;
 	struct regmap *regmap;
 	struct mutex lock;
+	unsigned int num_leds;
 
 	DECLARE_FLEX_ARRAY(struct lp5860_led, leds);
 };
Signature · 1 line
2.55.0
Signature · 1 line
Lee Jones