All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] leds: group-multicolor: Add support for initial value.
@ 2026-06-02 10:56 Martijn de Gouw
  2026-06-11 14:16 ` Lee Jones
  0 siblings, 1 reply; 2+ messages in thread
From: Martijn de Gouw @ 2026-06-02 10:56 UTC (permalink / raw)
  To: Jean-Jacques Hiblot, Lee Jones, Pavel Machek
  Cc: Martijn de Gouw, linux-leds, linux-kernel

When the driver is loaded, it turned off all LEDs in the group. This
patch changes the driver to take over existing LED states and set
the brighness and intensity in the group accordingly.

Signed-off-by: Martijn de Gouw <martijn.de.gouw@prodrive-technologies.com>
---
Changes in v4:
- Use DIV_ROUND_CLOSEST_ULL to avoid potential overflow when calculating
  the intensity
- Protect reading of led_cdev->brightness by acquire the lock.
- Link to v3: https://lore.kernel.org/linux-leds/20260316201321.2789158-1-martijn.de.gouw@prodrive-technologies.com/

Changes in v3:
- Use is_on boolean instead of storing the max_brightness twice
- Link to v2: https://lore.kernel.org/linux-leds/20251124210521.2064660-1-martijn.de.gouw@prodrive-technologies.com/

Changes in v2:
- Fix multiline comments
- Improve comments
- Link to v1: https://lore.kernel.org/linux-leds/20251111204556.2803878-1-martijn.de.gouw@prodrive-technologies.com/
---
 drivers/leds/rgb/leds-group-multicolor.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/drivers/leds/rgb/leds-group-multicolor.c b/drivers/leds/rgb/leds-group-multicolor.c
index 548c7dd63ba1e..7a77ee6776255 100644
--- a/drivers/leds/rgb/leds-group-multicolor.c
+++ b/drivers/leds/rgb/leds-group-multicolor.c
@@ -69,6 +69,7 @@ static int leds_gmc_probe(struct platform_device *pdev)
 	struct mc_subled *subled;
 	struct leds_multicolor *priv;
 	unsigned int max_brightness = 0;
+	bool is_on = false;
 	int i, ret, count = 0, common_flags = 0;
 
 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
@@ -96,6 +97,15 @@ static int leds_gmc_probe(struct platform_device *pdev)
 
 		max_brightness = max(max_brightness, led_cdev->max_brightness);
 
+		/*
+		 * If any LED is on, set brightness to the max brightness.
+		 * The actual brightness of the LED is set as intensity value.
+		 */
+		mutex_lock(&led_cdev->led_access);
+		if (led_cdev->brightness)
+			is_on = true;
+		mutex_unlock(&led_cdev->led_access);
+
 		count++;
 	}
 
@@ -109,14 +119,22 @@ static int leds_gmc_probe(struct platform_device *pdev)
 
 		subled[i].color_index = led_cdev->color;
 
-		/* Configure the LED intensity to its maximum */
-		subled[i].intensity = max_brightness;
+		/* Configure the LED intensity to its current brightness */
+		if (led_cdev->max_brightness) {
+			mutex_lock(&led_cdev->led_access);
+			subled[i].intensity = DIV_ROUND_CLOSEST_ULL((u64)led_cdev->brightness *
+								    max_brightness,
+								    led_cdev->max_brightness);
+			mutex_unlock(&led_cdev->led_access);
+		} else
+			subled[i].intensity = 0;
 	}
 
 	/* Initialise the multicolor's LED class device */
 	cdev = &priv->mc_cdev.led_cdev;
 	cdev->brightness_set_blocking = leds_gmc_set;
 	cdev->max_brightness = max_brightness;
+	cdev->brightness = is_on ? max_brightness : 0;
 	cdev->color = LED_COLOR_ID_MULTI;
 	priv->mc_cdev.num_colors = count;
 
-- 
2.39.2


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

end of thread, other threads:[~2026-06-11 14:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-02 10:56 [PATCH v4] leds: group-multicolor: Add support for initial value Martijn de Gouw
2026-06-11 14:16 ` Lee Jones

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.