Skip to contentSkip to conversation

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

PATCH Superseded Jean-Jacques Hiblot 3 participants 9 messages Sep 20, 2019 – Sep 24, 2019

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 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.
Because updating a regulator state can block, it is always defered to set_brightness_delayed().

changes in v4:
- Add a new patch to make led_set_brightness_sync() use led_set_brightness_nosleep() and then wait the work to be done
- Rework how the core knows how the regulator needs to be updated.

changes in v3:
- reword device-tree description
- reword commit log
- remove regulator updates from functions used in atomic context. If the regulator must be updated, it is defered to a workqueue.
- Fix led_set_brightness_sync() to work with the non-blocking function __led_set_brightness()

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

Jean-Jacques Hiblot (3):
led: make led_set_brightness_sync() use led_set_brightness_nosleep()
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                      | 17 ++++
 drivers/leds/led-core.c                       | 77 +++++++++++++++++--
 drivers/leds/leds.h                           |  3 +
 include/linux/leds.h                          |  5 ++
 5 files changed, 101 insertions(+), 7 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/3 led: make led_set_brightness_sync() use led_set_brightness_nosleep()

Making led_set_brightness_sync() use led_set_brightness_nosleep() has 2 advantages:
- works for LED controllers that do not provide brightness_set_blocking()
- When the blocking callback is used, it uses the workqueue to update the LED state, removing the need for mutual exclusion between led_set_brightness_sync() and set_brightness_delayed().

---
 drivers/leds/led-core.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/leds/led-core.c +7 -5
index f1f718dbe0f8..50e28a8f9357 100644
@@ -294,15 +294,17 @@ 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;
 
-	led_cdev->brightness = min(value, led_cdev->max_brightness);
-
-	if (led_cdev->flags & LED_SUSPENDED)
-		return 0;
+	ret = led_set_brightness_nosleep(led_cdev, value);
+	if (!ret)
+		return ret;
 
-	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
+	flush_work(&led_cdev->set_brightness_work);
+	return 0;
 }
 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
 
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v4 1/3] led: make led_set_brightness_sync() use led_set_brightness_nosleep()

Hi Jean,

Quoted 6 lines · Jean-Jacques Hiblot

Making led_set_brightness_sync() use led_set_brightness_nosleep() has 2 advantages:
- works for LED controllers that do not provide brightness_set_blocking()
- When the blocking callback is used, it uses the workqueue to update the LED state, removing the need for mutual exclusion between led_set_brightness_sync() and set_brightness_delayed().

And third:

- it compromises the "sync" part of the function name :-)

This function has been introduced specifically to be blocking and have the immediate effect. Its sole client is drivers/media/v4l2-core/v4l2-flash-led-class.c.

Quoted 34 lines
---
 drivers/leds/led-core.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/leds/led-core.c +7 -5
index f1f718dbe0f8..50e28a8f9357 100644
@@ -294,15 +294,17 @@ 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;
 
-	led_cdev->brightness = min(value, led_cdev->max_brightness);
-
-	if (led_cdev->flags & LED_SUSPENDED)
-		return 0;
+	ret = led_set_brightness_nosleep(led_cdev, value);
+	if (!ret)
+		return ret;
 
-	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
+	flush_work(&led_cdev->set_brightness_work);
+	return 0;
 }
 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
 
Signature · 2 lines
Best regards,
Jacek Anaszewski
#3 lore raw

Re: [PATCH v4 1/3] led: make led_set_brightness_sync() use led_set_brightness_nosleep()

Hi Jacek,

Quoted 9 lines · Jacek Anaszewski

Hi Jean,

Quoted 6 lines · Jean-Jacques Hiblot

Making led_set_brightness_sync() use led_set_brightness_nosleep() has 2 advantages:
- works for LED controllers that do not provide brightness_set_blocking()
- When the blocking callback is used, it uses the workqueue to update the LED state, removing the need for mutual exclusion between led_set_brightness_sync() and set_brightness_delayed().

Quoted 3 lines

And third:

- it compromises the "sync" part of the function name :-)

Making it sync is the role of the flush_work() function. It waits until the deferred work has been done.

JJ

Quoted 37 lines

This function has been introduced specifically to be blocking and have the immediate effect. Its sole client is drivers/media/v4l2-core/v4l2-flash-led-class.c.

Quoted 33 lines
---
  drivers/leds/led-core.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
drivers/leds/led-core.c +7 -5
index f1f718dbe0f8..50e28a8f9357 100644
@@ -294,15 +294,17 @@ 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;
 
-	led_cdev->brightness = min(value, led_cdev->max_brightness);
-
-	if (led_cdev->flags & LED_SUSPENDED)
-		return 0;
+	ret = led_set_brightness_nosleep(led_cdev, value);
+	if (!ret)
+		return ret;
 
-	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
+	flush_work(&led_cdev->set_brightness_work);
+	return 0;
  }
  EXPORT_SYMBOL_GPL(led_set_brightness_sync);
 
#4 lore raw

Re: [PATCH v4 1/3] led: make led_set_brightness_sync() use led_set_brightness_nosleep()

Hi Jean,

Quoted 14 lines · Jean-Jacques Hiblot

Hi Jacek,

Quoted 11 lines · Jacek Anaszewski

Hi Jean,

Quoted 8 lines · Jean-Jacques Hiblot

Making led_set_brightness_sync() use led_set_brightness_nosleep() has 2 advantages:
- works for LED controllers that do not provide brightness_set_blocking()
- When the blocking callback is used, it uses the workqueue to update the
   LED state, removing the need for mutual exclusion between led_set_brightness_sync() and set_brightness_delayed().

Quoted 6 lines
Quoted 3 lines

And third:

- it compromises the "sync" part of the function name :-)

Making it sync is the role of the flush_work() function. It waits until the deferred work has been done.

The thing is not in the blocking character of the function, but rather in the fastest possible way of setting torch brightness. led_set_brightness_nosleep() will defer brightness_set_blocking op to the workqueue so this condition will not be met then.

This function was added specifically for LED class flash v4l2 wrapper:
drivers/media/v4l2-core/v4l2-flash-led-class.c.

It may need an addition of support for brightness_set only drivers, but we haven't had a use case so far, since all client flash LED controllers are driven via blocking buses (there are not many of them).

Also, when LED flash class (and thus LED class also as a parent) is hijacked by v4l2-flash-led wrapper, its sysfs is disabled, so it is not possible to set e.g. timer trigger which could interfere with the led_set_brightness_sync() (and it also returns -EBUSY when blinking is enabled).

Quoted 36 lines
Quoted 35 lines

This function has been introduced specifically to be blocking and have the immediate effect. Its sole client is drivers/media/v4l2-core/v4l2-flash-led-class.c.

Quoted 31 lines

---
  drivers/leds/led-core.c | 12 +++++++-----

  1 file changed, 7 insertions(+), 5 deletions(-)
drivers/leds/led-core.c +7 -3
index f1f718dbe0f8..50e28a8f9357 100644
@@ -294,15 +294,17 @@ 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;
   -    led_cdev->brightness = min(value, led_cdev->max_brightness);
-
-    if (led_cdev->flags & LED_SUSPENDED)
-        return 0;
+    ret = led_set_brightness_nosleep(led_cdev, value);
+    if (!ret)
+        return ret;
   -    return __led_set_brightness_blocking(led_cdev,
 led_cdev->brightness);
+    flush_work(&led_cdev->set_brightness_work);
+    return 0;
   }
   EXPORT_SYMBOL_GPL(led_set_brightness_sync);

Signature · 2 lines
Best regards,
Jacek Anaszewski
#5 lore raw

Re: [PATCH v4 1/3] led: make led_set_brightness_sync() use led_set_brightness_nosleep()

Quoted 19 lines · Jacek Anaszewski

Hi Jean,

Quoted 16 lines · Jean-Jacques Hiblot

Hi Jacek,

Quoted 13 lines · Jacek Anaszewski

Hi Jean,

Quoted 8 lines · Jean-Jacques Hiblot

Making led_set_brightness_sync() use led_set_brightness_nosleep() has 2 advantages:
- works for LED controllers that do not provide brightness_set_blocking()
- When the blocking callback is used, it uses the workqueue to update the
   LED state, removing the need for mutual exclusion between led_set_brightness_sync() and set_brightness_delayed().

And third:

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

- it compromises the "sync" part of the function name :-)

Making it sync is the role of the flush_work() function. It waits until the deferred work has been done.

The thing is not in the blocking character of the function, but rather in the fastest possible way of setting torch brightness. led_set_brightness_nosleep() will defer brightness_set_blocking op to the workqueue so this condition will not be met then.

OK. I see the point there.

Quoted 8 lines

This function was added specifically for LED class flash v4l2 wrapper:
drivers/media/v4l2-core/v4l2-flash-led-class.c.

It may need an addition of support for brightness_set only drivers, but we haven't had a use case so far, since all client flash LED controllers are driven via blocking buses (there are not many of them).

Quoted 5 lines

Also, when LED flash class (and thus LED class also as a parent) is hijacked by v4l2-flash-led wrapper, its sysfs is disabled, so it is not possible to set e.g. timer trigger which could interfere with the led_set_brightness_sync() (and it also returns -EBUSY when blinking is enabled).

Then this is a really special use case and we don't really have to  worry about synchronization with the other ways to set the LED brightness. I'll drop any change to this function then.

Thanks for the detailed explanation.

JJ

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines

This function has been introduced specifically to be blocking and have the immediate effect. Its sole client is drivers/media/v4l2-core/v4l2-flash-led-class.c.

Quoted 31 lines
---
   drivers/leds/led-core.c | 12 +++++++-----
   1 file changed, 7 insertions(+), 5 deletions(-)
drivers/leds/led-core.c +7 -3
index f1f718dbe0f8..50e28a8f9357 100644
@@ -294,15 +294,17 @@ 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;
   -    led_cdev->brightness = min(value, led_cdev->max_brightness);
-
-    if (led_cdev->flags & LED_SUSPENDED)
-        return 0;
+    ret = led_set_brightness_nosleep(led_cdev, value);
+    if (!ret)
+        return ret;
   -    return __led_set_brightness_blocking(led_cdev,
 led_cdev->brightness);
+    flush_work(&led_cdev->set_brightness_work);
+    return 0;
   }
   EXPORT_SYMBOL_GPL(led_set_brightness_sync);
#6 lore raw

Re: [PATCH v4 1/3] led: make led_set_brightness_sync() use led_set_brightness_nosleep()

In reply to Jean-Jacques Hiblot
Thank you for the patch! Yet something to improve:

Hi Jean-Jacques,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.3 next-20190919]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Jean-Jacques-Hiblot/leds-Add-control-of-the-voltage-current-regulator-to-the-LED-core/20190920-220416
config: sh-allmodconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=sh
:::::: branch date: 2 hours ago
:::::: commit date: 2 hours ago

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

drivers//leds/led-core.c: In function 'led_set_brightness_sync':

>> drivers//leds/led-core.c:302:6: error: void value not ignored as it ought to be
     ret = led_set_brightness_nosleep(led_cdev, value);
         ^

# https://github.com/0day-ci/linux/commit/54301e6f4e910f292045a1afa62ef732791e1bb5
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout 54301e6f4e910f292045a1afa62ef732791e1bb5
vim +302 drivers//leds/led-core.c

81fe8e5b73e3f4 Jacek Anaszewski    2015-10-07  293  
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  294  int led_set_brightness_sync(struct led_classdev *led_cdev,
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  295  			    enum led_brightness value)
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  296  {
54301e6f4e910f Jean-Jacques Hiblot 2019-09-20  297  	int ret;
54301e6f4e910f Jean-Jacques Hiblot 2019-09-20  298  
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  299  	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  300  		return -EBUSY;
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  301  
54301e6f4e910f Jean-Jacques Hiblot 2019-09-20 @302  	ret = led_set_brightness_nosleep(led_cdev, value);
54301e6f4e910f Jean-Jacques Hiblot 2019-09-20  303  	if (!ret)
54301e6f4e910f Jean-Jacques Hiblot 2019-09-20  304  		return ret;
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  305  
54301e6f4e910f Jean-Jacques Hiblot 2019-09-20  306  	flush_work(&led_cdev->set_brightness_work);
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  307  	return 0;
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  308  }
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  309  EXPORT_SYMBOL_GPL(led_set_brightness_sync);
13ae79bbe4c214 Jacek Anaszewski    2015-10-07  310  

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#7 lore raw

PATCH v4 2/3 dt-bindings: leds: document the "power-supply" property

In reply to 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 +6 -0
index 9fa6f9795d50..54857c16573d 100644
@@ -77,6 +77,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
@@ -124,6 +129,7 @@ led-controller@0 {
 		function = LED_FUNCTION_STATUS;
 		linux,default-trigger = "heartbeat";
 		gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>;
+		power-supply = <&led_regulator>;
 	};
 
 	led1 {
Signature · 1 line
2.17.1
#8 lore raw

PATCH v4 3/3 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 | 17 +++++++++++
 drivers/leds/led-core.c  | 65 ++++++++++++++++++++++++++++++++++++++--
 drivers/leds/leds.h      |  3 ++
 include/linux/leds.h     |  5 ++++
 4 files changed, 88 insertions(+), 2 deletions(-)
drivers/leds/led-class.c +17 -0
index e11177d77b4c..d122b6982efd 100644
@@ -352,6 +352,7 @@ int led_classdev_register_ext(struct device *parent,
 	char final_name[LED_MAX_NAME_SIZE];
 	const char *proposed_name = composed_name;
 	int ret;
+	struct regulator *regulator;
 
 	if (init_data) {
 		if (init_data->devname_mandatory && !init_data->devicename) {
@@ -387,6 +388,22 @@ int led_classdev_register_ext(struct device *parent,
 		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 (regulator != ERR_PTR(-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;
+		led_cdev->regulator_state = REG_OFF;
+		atomic_set(&led_cdev->target_regulator_state, REG_UNKNOWN);
+	}
+
 	if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
 		ret = led_add_brightness_hw_changed(led_cdev);
 		if (ret) {
drivers/leds/led-core.c +63 -2

83 lines not on this page · raw message

drivers/leds/leds.h +3 -0
index 0b577cece8f7..02f261ce77f2 100644
@@ -11,6 +11,9 @@
 
 #include <linux/rwsem.h>
 #include <linux/leds.h>
+#include <linux/regulator/consumer.h>
+
+enum { REG_OFF = 0, REG_ON, REG_UNKNOWN };
 
 static inline int led_get_brightness(struct led_classdev *led_cdev)
 {
include/linux/leds.h +5 -0
index 88bf2ceaabe6..8ce7cf937192 100644
@@ -149,6 +149,11 @@ struct led_classdev {
 
 	/* Ensures consistent access to the LED Flash Class device */
 	struct mutex		led_access;
+
+	/* regulator */
+	struct regulator	*regulator;
+	int			regulator_state;
+	atomic_t		target_regulator_state;
 };
 
 /**
Signature · 1 line
2.17.1