Skip to contentSkip to conversation

leds: Add control of the voltage/current regulator to the LED core

PATCH Superseded Jean-Jacques Hiblot 5 participants 11 messages Jul 15, 2019 – Aug 19, 2019

Subsystems (2)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover leds: Add control of the voltage/current regulator to the LED core

This series makes it possible for the LED core to manage the power supply of a LED. It uses the regulator API to disable/enable the power if when the LED is turned on/off.
This is especially useful in situations where the LED driver/controller is not supplying the power.

changes in v2:
- use devm_regulator_get_optional() to avoid using the dummy regulator and do some unnecessary work

Jean-Jacques Hiblot (2):
dt-bindings: leds: document the "power-supply" property
leds: Add control of the voltage/current regulator to the LED core

 .../devicetree/bindings/leds/common.txt       |  6 +++
 drivers/leds/led-class.c                      | 15 ++++++
 drivers/leds/led-core.c                       | 50 +++++++++++++++++--
 drivers/leds/leds.h                           |  1 +
 include/linux/leds.h                          |  4 ++
 5 files changed, 73 insertions(+), 3 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/2 dt-bindings: leds: document the "power-supply" property

Most of the LEDs are powered by a voltage/current regulator. Describing it in the device-tree makes it possible for the LED core to enable/disable it when needed.

---
 Documentation/devicetree/bindings/leds/common.txt | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/leds/common.txt +6 -0
index 70876ac11367..539e124b1457 100644
@@ -61,6 +61,11 @@ Optional properties for child nodes:
 - panic-indicator : This property specifies that the LED should be used,
 		    if at all possible, as a panic indicator.
 
+- power-supply : A voltage/current regulator used to to power the LED. When a
+		 LED is turned off, the LED core disable its regulator. The
+		 same regulator can power many LED (or other) devices. It is
+		 turned off only when all of its users disabled it.
+
 - trigger-sources : List of devices which should be used as a source triggering
 		    this LED activity. Some LEDs can be related to a specific
 		    device and should somehow indicate its state. E.g. USB 2.0
@@ -106,6 +111,7 @@ gpio-leds {
 		label = "Status";
 		linux,default-trigger = "heartbeat";
 		gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>;
+		power-supply = <&led_regulator>;
 	};
 
 	usb {
Signature · 1 line
2.17.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] dt-bindings: leds: document the "power-supply" property

JJ

Quoted 14 lines · Jean-Jacques Hiblot

Most of the LEDs are powered by a voltage/current regulator. Describing it in the device-tree makes it possible for the LED core to enable/disable it when needed.

---
  Documentation/devicetree/bindings/leds/common.txt | 6 ++++++
  1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/leds/common.txt +0 -0
index 70876ac11367..539e124b1457 100644

Documentation/devicetree/bindings/leds/common.txt Optional properties for child nodes: · line 64

Quoted 4 lines
Documentation/devicetree/bindings/leds/common.txt +1 -0
@@ -61,6 +61,11 @@ Optional properties for child nodes:
  - panic-indicator : This property specifies that the LED should be used,
  		    if at all possible, as a panic indicator.
 
+- power-supply : A voltage/current regulator used to to power the LED. When a

Is the phandle to a voltage/current regulator used to to power the LED

Documentation/devicetree/bindings/leds/common.txt Optional properties for child nodes: · line 65

Quoted 1 line
Documentation/devicetree/bindings/leds/common.txt +1 -0
+		 LED is turned off, the LED core disable its regulator. The

The regulator is only disabled if it is the only consumer and/or the number of users = 0.

Quoted 7 lines
Documentation/devicetree/bindings/leds/common.txt +3 -0
+		 same regulator can power many LED (or other) devices. It is
+		 turned off only when all of its users disabled it.
+
  - trigger-sources : List of devices which should be used as a source triggering
  		    this LED activity. Some LEDs can be related to a specific
  		    device and should somehow indicate its state. E.g. USB 2.0

Documentation/devicetree/bindings/leds/common.txt gpio-leds { · line 117

Quoted 7 lines
Documentation/devicetree/bindings/leds/common.txt +1 -0
@@ -106,6 +111,7 @@ gpio-leds {
  		label = "Status";
  		linux,default-trigger = "heartbeat";
  		gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>;
+		power-supply = <&led_regulator>;
  	};
 
  	usb {
Acked-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] dt-bindings: leds: document the "power-supply" property

In reply to Jean-Jacques Hiblot
Quoted 5 lines · Jean-Jacques Hiblot

Most of the LEDs are powered by a voltage/current regulator. Describing it in the device-tree makes it possible for the LED core to enable/disable it when needed.

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#4 lore raw

PATCH v2 2/2 leds: Add control of the voltage/current regulator to the LED core

In reply to Jean-Jacques Hiblot

A LED is usually powered by a voltage/current regulator. Let the LED core know about it. This allows the LED core to turn on or off the power supply as needed.

---
 drivers/leds/led-class.c | 15 ++++++++++++
 drivers/leds/led-core.c  | 50 +++++++++++++++++++++++++++++++++++++---
 drivers/leds/leds.h      |  1 +
 include/linux/leds.h     |  4 ++++
 4 files changed, 67 insertions(+), 3 deletions(-)
drivers/leds/led-class.c +15 -0
index 4793e77808e2..cadd43c30d50 100644
@@ -253,6 +253,7 @@ int of_led_classdev_register(struct device *parent, struct device_node *np,
 {
 	char name[LED_MAX_NAME_SIZE];
 	int ret;
+	struct regulator *regulator;
 
 	ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
 	if (ret < 0)
@@ -272,6 +273,20 @@ int of_led_classdev_register(struct device *parent, struct device_node *np,
 		dev_warn(parent, "Led %s renamed to %s due to name collision",
 				led_cdev->name, dev_name(led_cdev->dev));
 
+	regulator = devm_regulator_get_optional(led_cdev->dev, "power");
+	if (IS_ERR(regulator)) {
+		if (PTR_ERR(regulator) != -ENODEV) {
+			dev_err(led_cdev->dev, "Cannot get the power supply for %s\n",
+				led_cdev->name);
+			device_unregister(led_cdev->dev);
+			mutex_unlock(&led_cdev->led_access);
+			return PTR_ERR(regulator);
+		}
+		led_cdev->regulator = NULL;
+	} else {
+		led_cdev->regulator = regulator;
+	}
+
 	if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
 		ret = led_add_brightness_hw_changed(led_cdev);
 		if (ret) {
drivers/leds/led-core.c +47 -3

98 lines not on this page · raw message

drivers/leds/leds.h +1 -0
index 47b229469069..5aa5c038bd38 100644
@@ -11,6 +11,7 @@
 
 #include <linux/rwsem.h>
 #include <linux/leds.h>
+#include <linux/regulator/consumer.h>
 
 static inline int led_get_brightness(struct led_classdev *led_cdev)
 {
include/linux/leds.h +4 -0
index 9b2bf574a17a..bee8e3f8dddd 100644
@@ -123,6 +123,10 @@ struct led_classdev {
 
 	/* Ensures consistent access to the LED Flash Class device */
 	struct mutex		led_access;
+
+	/* regulator */
+	struct regulator	*regulator;
+	bool			regulator_state;
 };
 
 extern int of_led_classdev_register(struct device *parent,
Signature · 1 line
2.17.1
Reviewed-by on v2 2/2
#5 lore raw

JJ

Quoted 3 lines · Jean-Jacques Hiblot

A LED is usually powered by a voltage/current regulator. Let the LED core know about it. This allows the LED core to turn on or off the power supply as needed.

This allows the LED core to turn on or off managed power supplies.

Quoted 22 lines
---
  drivers/leds/led-class.c | 15 ++++++++++++
  drivers/leds/led-core.c  | 50 +++++++++++++++++++++++++++++++++++++---
  drivers/leds/leds.h      |  1 +
  include/linux/leds.h     |  4 ++++
  4 files changed, 67 insertions(+), 3 deletions(-)
drivers/leds/led-class.c +1 -0
index 4793e77808e2..cadd43c30d50 100644
@@ -253,6 +253,7 @@ int of_led_classdev_register(struct device *parent, struct device_node *np,
  {
  	char name[LED_MAX_NAME_SIZE];
  	int ret;
+	struct regulator *regulator;
 
  	ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
  	if (ret < 0)

drivers/leds/led-class.c int of_led_classdev_register(struct device *parent, struct device_node *np, · line 276

Quoted 4 lines
drivers/leds/led-class.c +1 -0
@@ -272,6 +273,20 @@ int of_led_classdev_register(struct device *parent, struct device_node *np,
  		dev_warn(parent, "Led %s renamed to %s due to name collision",
  				led_cdev->name, dev_name(led_cdev->dev));
 
+	regulator = devm_regulator_get_optional(led_cdev->dev, "power");

Store the regulator in  led_cdev->regulator and drop the else case below.

Quoted 26 lines
drivers/leds/led-class.c +13 -0
+	if (IS_ERR(regulator)) {
+		if (PTR_ERR(regulator) != -ENODEV) {
+			dev_err(led_cdev->dev, "Cannot get the power supply for %s\n",
+				led_cdev->name);
+			device_unregister(led_cdev->dev);
+			mutex_unlock(&led_cdev->led_access);
+			return PTR_ERR(regulator);
+		}
+		led_cdev->regulator = NULL;
+	} else {
+		led_cdev->regulator = regulator;
+	}
+
  	if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
  		ret = led_add_brightness_hw_changed(led_cdev);
  		if (ret) {
drivers/leds/led-core.c +2 -0
index 7107cd7e87cf..a12b880b0a2f 100644
@@ -23,6 +23,33 @@ EXPORT_SYMBOL_GPL(leds_list_lock);
  LIST_HEAD(leds_list);
  EXPORT_SYMBOL_GPL(leds_list);
 
+static bool __led_need_regulator_update(struct led_classdev *led_cdev,
+					int brightness)

drivers/leds/led-core.c EXPORT_SYMBOL_GPL(leds_list_lock); · line 37

Quoted 10 lines
drivers/leds/led-core.c +10 -0
+{
+	bool new_state = (brightness != LED_OFF);
+
+	return led_cdev->regulator && led_cdev->regulator_state != new_state;
+}
+
+static int __led_handle_regulator(struct led_classdev *led_cdev,
+				int brightness)
+{
+	int rc;

Should there be a check for the regulator pointer.

If (!led_cdev->regulator)

    return 0;

Otherwise

<snip>

#6 lore raw

Hi Dan,

Quoted 40 lines · Dan Murphy

JJ

Quoted 3 lines · Jean-Jacques Hiblot

A LED is usually powered by a voltage/current regulator. Let the LED core know about it. This allows the LED core to turn on or off the power supply as needed.

This allows the LED core to turn on or off managed power supplies.

Quoted 17 lines

---

      if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) { ret = led_add_brightness_hw_changed(led_cdev); if (ret) {

drivers/leds/led-core.c +2 -0
index 7107cd7e87cf..a12b880b0a2f 100644
@@ -23,6 +23,33 @@ EXPORT_SYMBOL_GPL(leds_list_lock);
   LIST_HEAD(leds_list);
   EXPORT_SYMBOL_GPL(leds_list);
   +static bool __led_need_regulator_update(struct led_classdev *led_cdev,
+                    int brightness)
+{

drivers/leds/led-core.c EXPORT_SYMBOL_GPL(leds_list_lock); · line 37

Quoted 10 lines
drivers/leds/led-core.c +9 -0
+    bool new_state = (brightness != LED_OFF);
+
+    return led_cdev->regulator && led_cdev->regulator_state != 
 new_state;
+}
+
+static int __led_handle_regulator(struct led_classdev *led_cdev,
+                int brightness)
+{
+    int rc;

Should there be a check for the regulator pointer.

Quoted 3 lines

If (!led_cdev->regulator)

    return 0;

Not required because __led_need_regulator_update() returns false if led_cdev->regulator is NULL.

Thanks for the review

JJ

Quoted 8 lines

Otherwise

<snip>

#7 lore raw
In reply to Jean-Jacques Hiblot

Hi Jean,

Thank you for the patch.

I have one issue. Please refer below.

Quoted 84 lines · Jean-Jacques Hiblot

A LED is usually powered by a voltage/current regulator. Let the LED core know about it. This allows the LED core to turn on or off the power supply as needed.

---
 drivers/leds/led-class.c | 15 ++++++++++++
 drivers/leds/led-core.c  | 50 +++++++++++++++++++++++++++++++++++++---
 drivers/leds/leds.h      |  1 +
 include/linux/leds.h     |  4 ++++
 4 files changed, 67 insertions(+), 3 deletions(-)
drivers/leds/led-class.c +15 -0
index 4793e77808e2..cadd43c30d50 100644
@@ -253,6 +253,7 @@ int of_led_classdev_register(struct device *parent, struct device_node *np,
 {
 	char name[LED_MAX_NAME_SIZE];
 	int ret;
+	struct regulator *regulator;
 
 	ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
 	if (ret < 0)
@@ -272,6 +273,20 @@ int of_led_classdev_register(struct device *parent, struct device_node *np,
 		dev_warn(parent, "Led %s renamed to %s due to name collision",
 				led_cdev->name, dev_name(led_cdev->dev));
 
+	regulator = devm_regulator_get_optional(led_cdev->dev, "power");
+	if (IS_ERR(regulator)) {
+		if (PTR_ERR(regulator) != -ENODEV) {
+			dev_err(led_cdev->dev, "Cannot get the power supply for %s\n",
+				led_cdev->name);
+			device_unregister(led_cdev->dev);
+			mutex_unlock(&led_cdev->led_access);
+			return PTR_ERR(regulator);
+		}
+		led_cdev->regulator = NULL;
+	} else {
+		led_cdev->regulator = regulator;
+	}
+
 	if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
 		ret = led_add_brightness_hw_changed(led_cdev);
 		if (ret) {
drivers/leds/led-core.c +27 -0
index 7107cd7e87cf..a12b880b0a2f 100644
@@ -23,6 +23,33 @@ EXPORT_SYMBOL_GPL(leds_list_lock);
 LIST_HEAD(leds_list);
 EXPORT_SYMBOL_GPL(leds_list);
 
+static bool __led_need_regulator_update(struct led_classdev *led_cdev,
+					int brightness)
+{
+	bool new_state = (brightness != LED_OFF);
+
+	return led_cdev->regulator && led_cdev->regulator_state != new_state;
+}
+
+static int __led_handle_regulator(struct led_classdev *led_cdev,
+				int brightness)
+{
+	int rc;
+
+	if (__led_need_regulator_update(led_cdev, brightness)) {
+
+		if (brightness != LED_OFF)
+			rc = regulator_enable(led_cdev->regulator);
+		else
+			rc = regulator_disable(led_cdev->regulator);
+		if (rc)
+			return rc;
+
+		led_cdev->regulator_state = (brightness != LED_OFF);
+	}
+	return 0;
+}
+
 static int __led_set_brightness(struct led_classdev *led_cdev,
 				enum led_brightness value)
 {

drivers/leds/led-core.c static void led_timer_function(struct timer_list *t) · line 110

Quoted 4 lines
drivers/leds/led-core.c +1 -0
@@ -80,6 +107,7 @@ static void led_timer_function(struct timer_list *t)
 	}
 
 	led_set_brightness_nosleep(led_cdev, brightness);
+	__led_handle_regulator(led_cdev, brightness);

This cannot be called from atomic context since regulator_enable/disable use mutex beneath, that can sleep on contention. Therefore this call has to be made in two places instead:

__led_set_brightness()
__led_set_brightness_blocking()

Quoted 95 lines
drivers/leds/led-core.c +19 -3
 	/* Return in next iteration if led is in one-shot mode and we are in
 	 * the final blink state so that the led is toggled each delay_on +
@@ -115,6 +143,8 @@ static void set_brightness_delayed(struct work_struct *ws)
 	if (ret == -ENOTSUPP)
 		ret = __led_set_brightness_blocking(led_cdev,
 					led_cdev->delayed_set_value);
+	__led_handle_regulator(led_cdev, led_cdev->delayed_set_value);
+
 	if (ret < 0 &&
 	    /* LED HW might have been unplugged, therefore don't warn */
 	    !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
@@ -141,6 +171,7 @@ static void led_set_software_blink(struct led_classdev *led_cdev,
 	/* never on - just set to off */
 	if (!delay_on) {
 		led_set_brightness_nosleep(led_cdev, LED_OFF);
+		__led_handle_regulator(led_cdev, LED_OFF);
 		return;
 	}
 
@@ -148,6 +179,7 @@ static void led_set_software_blink(struct led_classdev *led_cdev,
 	if (!delay_off) {
 		led_set_brightness_nosleep(led_cdev,
 					   led_cdev->blink_brightness);
+		__led_handle_regulator(led_cdev, led_cdev->blink_brightness);
 		return;
 	}
 
@@ -256,8 +288,14 @@ void led_set_brightness_nopm(struct led_classdev *led_cdev,
 			      enum led_brightness value)
 {
 	/* Use brightness_set op if available, it is guaranteed not to sleep */
-	if (!__led_set_brightness(led_cdev, value))
-		return;
+	if (!__led_set_brightness(led_cdev, value)) {
+		/*
+		 * if regulator state doesn't need to be changed, that is all/
+		 * Otherwise delegate the change to a work queue
+		 */
+		if (!__led_need_regulator_update(led_cdev, value))
+			return;
+	}
 
 	/* If brightness setting can sleep, delegate it to a work queue task */
 	led_cdev->delayed_set_value = value;
@@ -280,6 +318,8 @@ EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
 int led_set_brightness_sync(struct led_classdev *led_cdev,
 			    enum led_brightness value)
 {
+	int ret;
+
 	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
 		return -EBUSY;
 
@@ -288,7 +328,11 @@ int led_set_brightness_sync(struct led_classdev *led_cdev,
 	if (led_cdev->flags & LED_SUSPENDED)
 		return 0;
 
-	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
+	ret = __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
+	if (ret)
+		return ret;
+
+	return __led_handle_regulator(led_cdev, led_cdev->brightness);
 }
 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
 
drivers/leds/leds.h +1 -0
index 47b229469069..5aa5c038bd38 100644
@@ -11,6 +11,7 @@
 
 #include <linux/rwsem.h>
 #include <linux/leds.h>
+#include <linux/regulator/consumer.h>
 
 static inline int led_get_brightness(struct led_classdev *led_cdev)
 {
include/linux/leds.h +4 -0
index 9b2bf574a17a..bee8e3f8dddd 100644
@@ -123,6 +123,10 @@ struct led_classdev {
 
 	/* Ensures consistent access to the LED Flash Class device */
 	struct mutex		led_access;
+
+	/* regulator */
+	struct regulator	*regulator;
+	bool			regulator_state;
 };
 
 extern int of_led_classdev_register(struct device *parent,
Signature · 2 lines
Best regards,
Jacek Anaszewski
#8 lore raw

Hi Jacek,

Quoted 11 lines · Jacek Anaszewski
drivers/leds/led-core.c +1 -0
@@ -80,6 +107,7 @@ static void led_timer_function(struct timer_list *t)
  	}
 
  	led_set_brightness_nosleep(led_cdev, brightness);
+	__led_handle_regulator(led_cdev, brightness);

This cannot be called from atomic context since regulator_enable/disable use mutex beneath, that can sleep on contention. Therefore this call has to be made in two places instead:

__led_set_brightness()
__led_set_brightness_blocking()

Thanks. I'll fix this in v3.

JJ

#9 lore raw
In reply to Jean-Jacques Hiblot
Quoted 1 line · Jean-Jacques Hiblot

A LED is usually powered by a voltage/current regulator. Let the LED core

This is almost certainly nitpicking but since there's enough other review comments that you will have to respin anyway ;-)

Is an LED really "usually powered by a voltage/current regulator"? Some LEDs have a software controlled power supply but I'm not sure it is the usual case.

Likewise its a little confusing to be talking about LEDs with an external current regulator since, although that is possible, it is also one the main features provided by LED driver chips.

Daniel.

#10 lore raw
Quoted 4 lines · Daniel Thompson
Quoted 1 line · Jean-Jacques Hiblot

A LED is usually powered by a voltage/current regulator. Let the LED core

This is almost certainly nitpicking but since there's enough other review comments that you will have to respin anyway ;-)

No problems. comments are welcome.

Quoted 3 lines

Is an LED really "usually powered by a voltage/current regulator"? Some LEDs have a software controlled power supply but I'm not sure it is the usual case.

True. I'll reword this.

Quoted 3 lines

Likewise its a little confusing to be talking about LEDs with an external current regulator since, although that is possible, it is also one the main features provided by LED driver chips.

In my experience LED drivers are quite often current sinks. In that case the power is provided externally, and sometimes by a managed regulator.

Thanks,

JJ

Quoted 2 lines

Daniel.