Skip to contentSkip to conversation

leds: Add led_mc_set_brightness() and led_mc_trigger_event() functions

RFC In review Hans de Goede 4 participants 6 messages Mar 9, 2024 – Mar 22, 2024 LED

lore
Share & tools
lore
#0 lore raw

cover leds: Add led_mc_set_brightness() and led_mc_trigger_event() functions

Hi All,

Here is a patch-series adding led_mc_set_brightness() and led_mc_trigger_event() functions for changing multi-color LED colors from inside the kernel.

This is a preparation series for adding a new trigger to drivers/power/supply/power_supply_leds.c which changes the color of a RGB LED depending on if the battery is discharging (LED off) charging (LED red) or full (LED green)

This is marked as RFC since the power_supply_leds.c changes have not been written yet and as such this is compile-tested only atm. The main goal of this RFC is to gather review feedback on the chosen approach in these 2 patches.

Regards,

Hans

Hans de Goede (2):
leds: core: Add led_mc_set_brightness() function
leds: trigger: Add led_mc_trigger_event() function

 drivers/leds/led-class-multicolor.c |  1 +
 drivers/leds/led-core.c             | 31 +++++++++++++++++++++++++++++
 drivers/leds/led-triggers.c         | 20 +++++++++++++++++++
 include/linux/leds.h                | 26 ++++++++++++++++++++++++
 4 files changed, 78 insertions(+)
Signature · 1 line
2.43.2
#1 lore raw

RFC 1/2 leds: core: Add led_mc_set_brightness() function

Add a new led_mc_set_brightness() function for in kernel color/brightness changing of multi-color LEDs.

led-class-multicolor can be build as a module and led_mc_set_brightness() will have builtin callers, so put led_mc_set_brightness() inside led-core instead, just like how led_set_brightness() is part of the core and not of the led-class object.

This also adds a new LED_MULTI_COLOR led_classdev flag to allow led_mc_set_brightness() to verify that it is operating on a multi-color LED classdev, avoiding casting the passed in LED classdev to a multi-color LED classdev, when it actually is not a multi-color LED.

---
 drivers/leds/led-class-multicolor.c |  1 +
 drivers/leds/led-core.c             | 31 +++++++++++++++++++++++++++++
 include/linux/leds.h                | 20 +++++++++++++++++++
 3 files changed, 52 insertions(+)
drivers/leds/led-class-multicolor.c +1 -0
index ec62a4811613..df01c0e66c8b 100644
@@ -134,6 +134,7 @@ int led_classdev_multicolor_register_ext(struct device *parent,
 		return -EINVAL;
 
 	led_cdev = &mcled_cdev->led_cdev;
+	led_cdev->flags |= LED_MULTI_COLOR;
 	mcled_cdev->led_cdev.groups = led_multicolor_groups;
 
 	return led_classdev_register_ext(parent, led_cdev, init_data);
drivers/leds/led-core.c +31 -0
index 89c9806cc97f..5889753ebc74 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/kernel.h>
+#include <linux/led-class-multicolor.h>
 #include <linux/leds.h>
 #include <linux/list.h>
 #include <linux/module.h>
@@ -362,6 +363,36 @@ int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
 }
 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
 
+/*
+ * This is a led-core function because just like led_set_brightness()
+ * it is used in kernel by e.g. triggers.
+ */
+void led_mc_set_brightness(struct led_classdev *led_cdev,
+			   unsigned int *intensity_value, unsigned int num_colors,
+			   unsigned int brightness)
+{
+	struct led_classdev_mc *mcled_cdev;
+	unsigned int i;
+
+	if (!(led_cdev->flags & LED_MULTI_COLOR)) {
+		dev_err_once(led_cdev->dev, "%s: error not a multi-color LED\n",  __func__);
+		return;
+	}
+
+	mcled_cdev = lcdev_to_mccdev(led_cdev);
+	if (num_colors != mcled_cdev->num_colors) {
+		dev_err_once(led_cdev->dev, "%s: error num_colors mismatch %d != %d\n",
+			     __func__, num_colors, mcled_cdev->num_colors);
+		return;
+	}
+
+	for (i = 0; i < mcled_cdev->num_colors; i++)
+		mcled_cdev->subled_info[i].intensity = intensity_value[i];
+
+	led_set_brightness(led_cdev, brightness);
+}
+EXPORT_SYMBOL_GPL(led_mc_set_brightness);
+
 int led_update_brightness(struct led_classdev *led_cdev)
 {
 	int ret;
include/linux/leds.h +20 -0
index 4754b02d3a2c..fed88eb9e170 100644
@@ -115,6 +115,7 @@ struct led_classdev {
 #define LED_BRIGHT_HW_CHANGED	BIT(21)
 #define LED_RETAIN_AT_SHUTDOWN	BIT(22)
 #define LED_INIT_DEFAULT_TRIGGER BIT(23)
+#define LED_MULTI_COLOR		BIT(24)
 
 	/* set_brightness_work / blink_timer flags, atomic, private. */
 	unsigned long		work_flags;
@@ -392,6 +393,25 @@ void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness);
  */
 int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value);
 
+/**
+ * led_mc_set_brightness - set mc LED color intensity values and brightness
+ * @led_cdev: the LED to set
+ * @intensity_value: array of per color intensity values to set
+ * @num_colors: amount of entries in intensity_value array
+ * @brightness: the brightness to set the LED to
+ *
+ * Set a multi-color LED's per color intensity values and brightness.
+ * If necessary, this cancels the software blink timer. This function is
+ * guaranteed not to sleep.
+ *
+ * Calling this function on a non multi-color led_classdev or with the wrong
+ * num_colors value is an error. In this case an error will be logged once
+ * and the call will do nothing.
+ */
+void led_mc_set_brightness(struct led_classdev *led_cdev,
+			   unsigned int *intensity_value, unsigned int num_colors,
+			   unsigned int brightness);
+
 /**
  * led_update_brightness - update LED brightness
  * @led_cdev: the LED to query
Signature · 1 line
2.43.2
#2 lore raw

RFC 2/2 leds: trigger: Add led_mc_trigger_event() function

In reply to Hans de Goede

Add a new led_mc_trigger_event() function for triggers which want to change the color of a multi-color LED based on their trigger conditions.

---
This is a preparation patch for adding a new trigger to drivers/power/supply/power_supply_leds.c which changes the color of a RGB LED depending on if the battery is discharging (LED off) charging (LED red) or full (LED green)

---
 drivers/leds/led-triggers.c | 20 ++++++++++++++++++++
 include/linux/leds.h        |  6 ++++++
 2 files changed, 26 insertions(+)
drivers/leds/led-triggers.c +20 -0
index bd59a14a4a90..fcc4e7a7b12b 100644
@@ -380,6 +380,26 @@ void led_trigger_event(struct led_trigger *trig,
 }
 EXPORT_SYMBOL_GPL(led_trigger_event);
 
+void led_mc_trigger_event(struct led_trigger *trig,
+			  unsigned int *intensity_value, unsigned int num_colors,
+			  enum led_brightness brightness)
+{
+	struct led_classdev *led_cdev;
+
+	if (!trig)
+		return;
+
+	rcu_read_lock();
+	list_for_each_entry_rcu(led_cdev, &trig->led_cdevs, trig_list) {
+		if (!(led_cdev->flags & LED_MULTI_COLOR))
+			continue;
+
+		led_mc_set_brightness(led_cdev, intensity_value, num_colors, brightness);
+	}
+	rcu_read_unlock();
+}
+EXPORT_SYMBOL_GPL(led_mc_trigger_event);
+
 static void led_trigger_blink_setup(struct led_trigger *trig,
 			     unsigned long delay_on,
 			     unsigned long delay_off,
include/linux/leds.h +6 -0
index fed88eb9e170..5378e4cd03ff 100644
@@ -526,6 +526,9 @@ void led_trigger_register_simple(const char *name,
 				struct led_trigger **trigger);
 void led_trigger_unregister_simple(struct led_trigger *trigger);
 void led_trigger_event(struct led_trigger *trigger,  enum led_brightness event);
+void led_mc_trigger_event(struct led_trigger *trig,
+			  unsigned int *intensity_value, unsigned int num_colors,
+			  enum led_brightness brightness);
 void led_trigger_blink(struct led_trigger *trigger, unsigned long delay_on,
 		       unsigned long delay_off);
 void led_trigger_blink_oneshot(struct led_trigger *trigger,
@@ -562,6 +565,9 @@ static inline void led_trigger_register_simple(const char *name,
 static inline void led_trigger_unregister_simple(struct led_trigger *trigger) {}
 static inline void led_trigger_event(struct led_trigger *trigger,
 				enum led_brightness event) {}
+static inline void led_mc_trigger_event(struct led_trigger *trig,
+				unsigned int *intensity_value, unsigned int num_colors,
+				enum led_brightness brightness) {}
 static inline void led_trigger_blink(struct led_trigger *trigger,
 				      unsigned long delay_on,
 				      unsigned long delay_off) {}
Signature · 1 line
2.43.2
Reviewed-by on the v1 series
#3 lore raw
In reply to Hans de Goede

Hi Hans,

Quoted 25 lines · Hans de Goede

Hi All,

Here is a patch-series adding led_mc_set_brightness() and led_mc_trigger_event() functions for changing multi-color LED colors from inside the kernel.

This is a preparation series for adding a new trigger to drivers/power/supply/power_supply_leds.c which changes the color of a RGB LED depending on if the battery is discharging (LED off) charging (LED red) or full (LED green)

This is marked as RFC since the power_supply_leds.c changes have not been written yet and as such this is compile-tested only atm. The main goal of this RFC is to gather review feedback on the chosen approach in these 2 patches.

Regards,

Hans

Hans de Goede (2):
leds: core: Add led_mc_set_brightness() function
leds: trigger: Add led_mc_trigger_event() function

Quoted 6 lines
  drivers/leds/led-class-multicolor.c |  1 +
  drivers/leds/led-core.c             | 31 +++++++++++++++++++++++++++++
  drivers/leds/led-triggers.c         | 20 +++++++++++++++++++
  include/linux/leds.h                | 26 ++++++++++++++++++++++++
  4 files changed, 78 insertions(+)

LGTM.

Signature · 2 lines
Best regards,
Jacek Anaszewski
maintainer
#4 lore raw
In reply to Hans de Goede
Quoted 25 lines · Hans de Goede

Hi All,

Here is a patch-series adding led_mc_set_brightness() and led_mc_trigger_event() functions for changing multi-color LED colors from inside the kernel.

This is a preparation series for adding a new trigger to drivers/power/supply/power_supply_leds.c which changes the color of a RGB LED depending on if the battery is discharging (LED off) charging (LED red) or full (LED green)

This is marked as RFC since the power_supply_leds.c changes have not been written yet and as such this is compile-tested only atm. The main goal of this RFC is to gather review feedback on the chosen approach in these 2 patches.

Regards,

Hans

Hans de Goede (2):
leds: core: Add led_mc_set_brightness() function
leds: trigger: Add led_mc_trigger_event() function

Quoted 5 lines
 drivers/leds/led-class-multicolor.c |  1 +
 drivers/leds/led-core.c             | 31 +++++++++++++++++++++++++++++
 drivers/leds/led-triggers.c         | 20 +++++++++++++++++++
 include/linux/leds.h                | 26 ++++++++++++++++++++++++
 4 files changed, 78 insertions(+)

What is it you want me to do with this RFC patches Hans?

Signature · 1 line
Lee Jones [李琼斯]
#5 lore raw

Hi,

Quoted 34 lines · Lee Jones
Quoted 25 lines · Hans de Goede

Hi All,

Here is a patch-series adding led_mc_set_brightness() and led_mc_trigger_event() functions for changing multi-color LED colors from inside the kernel.

This is a preparation series for adding a new trigger to drivers/power/supply/power_supply_leds.c which changes the color of a RGB LED depending on if the battery is discharging (LED off) charging (LED red) or full (LED green)

This is marked as RFC since the power_supply_leds.c changes have not been written yet and as such this is compile-tested only atm. The main goal of this RFC is to gather review feedback on the chosen approach in these 2 patches.

Regards,

Hans

Hans de Goede (2):
leds: core: Add led_mc_set_brightness() function
leds: trigger: Add led_mc_trigger_event() function

Quoted 5 lines
 drivers/leds/led-class-multicolor.c |  1 +
 drivers/leds/led-core.c             | 31 +++++++++++++++++++++++++++++
 drivers/leds/led-triggers.c         | 20 +++++++++++++++++++
 include/linux/leds.h                | 26 ++++++++++++++++++++++++
 4 files changed, 78 insertions(+)
Quoted 5 lines

What is it you want me to do with this RFC patches Hans?

--
Lee Jones [李琼斯]

This is used for the Xiaomi pad2 indicator LED to show the battery status in a multi-color manner and it can be found in the following URL.
https://lore.kernel.org/linux-leds/[email protected]/T/#m577efb6549aeb29d2813faf8c51ed38e9e092598

Thank you

Signature · 2 lines
BR,
Kate