Skip to contentSkip to conversation

Multicolor PWM LED support

PATCH Superseded [email protected] 6 participants 14 messages Jan 26, 2022 – Feb 12, 2022 1 message not in the archive

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Multicolor PWM LED support

From: Sven Schwermer <[email protected]>

Hi,

This patch series is getting mature. I have removed the RFC tag for this version. The initial discussion happened here [1].

I would appreciate if anyone would test this code. It runs on my i.MX6ULL-based hardware.

Best regards,
Sven

[1]:https://lore.kernel.org/linux-leds/[email protected]/

Sven Schwermer (2):
dt-bindings: leds: Add multicolor PWM LED bindings
leds: Add PWM multicolor driver

 .../bindings/leds/leds-pwm-multicolor.yaml    |  75 +++++++
 drivers/leds/Kconfig                          |   8 +
 drivers/leds/Makefile                         |   1 +
 drivers/leds/leds-pwm-multicolor.c            | 184 ++++++++++++++++++
 4 files changed, 268 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml
 create mode 100644 drivers/leds/leds-pwm-multicolor.c

Interdiff against v2:

Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml +2 -3
index b82b26f2e140..5a7ed5e1bb9f 100644
@@ -17,8 +17,7 @@ properties:
   compatible:
     const: pwm-leds-multicolor
 
-patternProperties:
-  '^multi-led@[0-9a-f]$':
+  multi-led:
     type: object
     allOf:
       - $ref: leds-class-multicolor.yaml#
@@ -51,7 +50,7 @@ examples:
     rgb-led {
         compatible = "pwm-leds-multicolor";
 
-        multi-led@0 {
+        multi-led {
           color = <LED_COLOR_ID_RGB>;
           function = LED_FUNCTION_INDICATOR;
           max-brightness = <65535>;
drivers/leds/leds-pwm-multicolor.c +15 -15
index c54bed4536d3..bc4d21ddd74a 100644
@@ -5,18 +5,18 @@
  * Copyright 2022 Sven Schwermer <[email protected]>
  */
 
-#include <linux/module.h>
+#include <linux/err.h>
 #include <linux/kernel.h>
-#include <linux/platform_device.h>
 #include <linux/led-class-multicolor.h>
 #include <linux/leds.h>
-#include <linux/err.h>
-#include <linux/pwm.h>
+#include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
 
 struct pwm_led {
 	struct pwm_device *pwm;
-	struct pwm_state pwmstate;
+	struct pwm_state state;
 };
 
 struct pwm_mc_led {
@@ -39,14 +39,14 @@ static int led_pwm_mc_set(struct led_classdev *cdev,
 	mutex_lock(&priv->lock);
 
 	for (i = 0; i < mc_cdev->num_colors; ++i) {
-		duty = priv->leds[i].pwmstate.period;
+		duty = priv->leds[i].state.period;
 		duty *= mc_cdev->subled_info[i].brightness;
 		do_div(duty, cdev->max_brightness);
 
-		priv->leds[i].pwmstate.duty_cycle = duty;
-		priv->leds[i].pwmstate.enabled = duty > 0;
+		priv->leds[i].state.duty_cycle = duty;
+		priv->leds[i].state.enabled = duty > 0;
 		ret = pwm_apply_state(priv->leds[i].pwm,
-				      &priv->leds[i].pwmstate);
+				      &priv->leds[i].state);
 		if (ret)
 			break;
 	}
@@ -83,7 +83,7 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 			    GFP_KERNEL);
 	if (!priv) {
 		ret = -ENOMEM;
-		goto out;
+		goto release_mcnode;
 	}
 	mutex_init(&priv->lock);
 
@@ -96,8 +96,6 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 
 	/* init the multicolor's LED class device */
 	cdev = &priv->mc_cdev.led_cdev;
-	fwnode_property_read_string(mcnode, "label", &cdev->name);
-	cdev->brightness = LED_OFF;
 	fwnode_property_read_u32(mcnode, "max-brightness",
 				 &cdev->max_brightness);
 	cdev->flags = LED_CORE_SUSPENDRESUME;
@@ -110,19 +108,19 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 		if (IS_ERR(pwmled->pwm)) {
 			ret = PTR_ERR(pwmled->pwm);
 			dev_err(&pdev->dev, "unable to request PWM: %d\n", ret);
+			fwnode_handle_put(fwnode);
 			goto destroy_mutex;
 		}
-		pwm_init_state(pwmled->pwm, &pwmled->pwmstate);
+		pwm_init_state(pwmled->pwm, &pwmled->state);
 
 		ret = fwnode_property_read_u32(fwnode, "color", &color);
 		if (ret) {
 			dev_err(&pdev->dev, "cannot read color: %d\n", ret);
+			fwnode_handle_put(fwnode);
 			goto destroy_mutex;
 		}
 
 		subled[priv->mc_cdev.num_colors].color_index = color;
-		subled[priv->mc_cdev.num_colors].channel =
-			priv->mc_cdev.num_colors;
 		++priv->mc_cdev.num_colors;
 	}
 
@@ -149,6 +147,8 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 
 destroy_mutex:
 	mutex_destroy(&priv->lock);
+release_mcnode:
+	fwnode_handle_put(mcnode);
 out:
 	return ret;
 }
Signature · 1 line
2.35.0
#1 lore raw

PATCH v3 1/2 dt-bindings: leds: Add multicolor PWM LED bindings

From: Sven Schwermer <[email protected]>

This allows to group multiple PWM-connected monochrome LEDs into multicolor LEDs, e.g. RGB LEDs.

---

Notes:
Changes in v3:
* Remove multi-led unit name

 .../bindings/leds/leds-pwm-multicolor.yaml    | 75 +++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +75 -0
new file mode 100644
index 000000000000..5a7ed5e1bb9f
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/leds-pwm-multicolor.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Multi-color LEDs connected to PWM
+
+maintainers:
+  - Sven Schwermer <[email protected]>
+
+description: |
+  This driver combines several monochrome PWM LEDs into one multi-color
+  LED using the multicolor LED class.
+
+properties:
+  compatible:
+    const: pwm-leds-multicolor
+
+  multi-led:
+    type: object
+    allOf:
+      - $ref: leds-class-multicolor.yaml#
+
+    patternProperties:
+      "^led-[0-9a-z]+$":
+        type: object
+        properties:
+          pwms:
+            maxItems: 1
+
+          pwm-names: true
+
+          color:
+            $ref: common.yaml#/properties/color
+
+        required:
+          - pwms
+          - color
+
+required:
+  - compatible
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/leds/common.h>
+
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
+
+        multi-led {
+          color = <LED_COLOR_ID_RGB>;
+          function = LED_FUNCTION_INDICATOR;
+          max-brightness = <65535>;
+
+          led-red {
+              pwms = <&pwm1 0 1000000>;
+              color = <LED_COLOR_ID_RED>;
+          };
+
+          led-green {
+              pwms = <&pwm2 0 1000000>;
+              color = <LED_COLOR_ID_GREEN>;
+          };
+
+          led-blue {
+              pwms = <&pwm3 0 1000000>;
+              color = <LED_COLOR_ID_BLUE>;
+          };
+        };
+    };
+
+...
Signature · 1 line
2.35.0
#2 lore raw

Re: [PATCH v3 1/2] dt-bindings: leds: Add multicolor PWM LED bindings

Hi Sven,

Quoted 7 lines · On 1/26/22 11:48 AM, [email protected] wrote:

From: Sven Schwermer <[email protected]>

This allows to group multiple PWM-connected monochrome LEDs into multicolor LEDs, e.g. RGB LEDs.

---

[...]

Quoted 4 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +4 -0
+
+additionalProperties: false
+
+examples:

Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml line 56

Quoted 10 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +10 -0
+  - |
+    #include <dt-bindings/leds/common.h>
+
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
+
+        multi-led {
+          color = <LED_COLOR_ID_RGB>;
+          function = LED_FUNCTION_INDICATOR;
+          max-brightness = <65535>;

It doesn't make much sense to have such a big resolution of global multi color brightness. 255 will be sufficient.

Quoted 19 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +18 -0
+          led-red {
+              pwms = <&pwm1 0 1000000>;
+              color = <LED_COLOR_ID_RED>;
+          };
+
+          led-green {
+              pwms = <&pwm2 0 1000000>;
+              color = <LED_COLOR_ID_GREEN>;
+          };
+
+          led-blue {
+              pwms = <&pwm3 0 1000000>;
+              color = <LED_COLOR_ID_BLUE>;
+          };
+        };
+    };
+
+...
Signature · 2 lines
Best regards,
Jacek Anaszewski
#3 lore raw

Re: [PATCH v3 1/2] dt-bindings: leds: Add multicolor PWM LED bindings

Quoted 20 lines · On Thu, 27 Jan 2022 22:24:21 +0100 Jacek Anaszewski…

Hi Sven,

Quoted 7 lines · On 1/26/22 11:48 AM, [email protected] wrote:

From: Sven Schwermer <[email protected]>

This allows to group multiple PWM-connected monochrome LEDs into multicolor LEDs, e.g. RGB LEDs.

---

[...]

Quoted 9 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +9 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/leds/common.h>
+
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
Quoted 8 lines

Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml line 56

Quoted 5 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +5 -0
+
+        multi-led {
+          color = <LED_COLOR_ID_RGB>;
+          function = LED_FUNCTION_INDICATOR;
+          max-brightness = <65535>;  

It doesn't make much sense to have such a big resolution of global multi color brightness. 255 will be sufficient.

If the PWM supports it, why not?
On Omnia the default is 255, and since it is PWM, the change from 0/255 to 1/255 is much bigger then from, say, 15/255 to 16/255. So if 1/255 is too bright, you are then unable to set it less bright. I think 1024 or ever 65535 makes sense with PWMs.

Marek

#4 lore raw

Re: [PATCH v3 1/2] dt-bindings: leds: Add multicolor PWM LED bindings

Quoted 32 lines · Marek Behún
Quoted 20 lines · On Thu, 27 Jan 2022 22:24:21 +0100 Jacek Anaszewski…

Hi Sven,

Quoted 7 lines · On 1/26/22 11:48 AM, [email protected] wrote:

From: Sven Schwermer <[email protected]>

This allows to group multiple PWM-connected monochrome LEDs into multicolor LEDs, e.g. RGB LEDs.

---

[...]

Quoted 9 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +9 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/leds/common.h>
+
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
Quoted 8 lines

Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml line 56

Quoted 5 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +5 -0
+
+        multi-led {
+          color = <LED_COLOR_ID_RGB>;
+          function = LED_FUNCTION_INDICATOR;
+          max-brightness = <65535>;

It doesn't make much sense to have such a big resolution of global multi color brightness. 255 will be sufficient.

Quoted 5 lines

If the PWM supports it, why not?
On Omnia the default is 255, and since it is PWM, the change from 0/255 to 1/255 is much bigger then from, say, 15/255 to 16/255. So if 1/255 is too bright, you are then unable to set it less bright. I think 1024 or ever 65535 makes sense with PWMs.

With values other than 255 we will not achieve 24-bit RGB, which is one problem, and the other one is non-linear brightness that can be achieved with PWM. So probably we would need to add an additional note in the documentation [0], saying that changing global brightness allows to preserve combined LED hue only when all sub-leds are linear, and that it will not be the case for PWM LEDs.

And I propose to change multi-led 'color' DT property value from LED_COLOR_ID_RGB to LED_COLOR_ID_MULTI to avoid the impression that it will work as traditional 24-bit RGB.

[0] Documentation/leds/leds-class-multicolor.rst

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

Re: [PATCH v3 1/2] dt-bindings: leds: Add multicolor PWM LED bindings

Quoted 46 lines · On Sat, 29 Jan 2022 00:04:01 +0100 Jacek Anaszewski…
Quoted 32 lines · Marek Behún
Quoted 20 lines · On Thu, 27 Jan 2022 22:24:21 +0100 Jacek Anaszewski…

Hi Sven,

Quoted 7 lines · On 1/26/22 11:48 AM, [email protected] wrote:

From: Sven Schwermer <[email protected]>

This allows to group multiple PWM-connected monochrome LEDs into multicolor LEDs, e.g. RGB LEDs.

---

[...]

Quoted 9 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +9 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/leds/common.h>
+
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
Quoted 8 lines

Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml line 56

Quoted 5 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +5 -0
+
+        multi-led {
+          color = <LED_COLOR_ID_RGB>;
+          function = LED_FUNCTION_INDICATOR;
+          max-brightness = <65535>;  

It doesn't make much sense to have such a big resolution of global multi color brightness. 255 will be sufficient.

Quoted 5 lines

If the PWM supports it, why not?
On Omnia the default is 255, and since it is PWM, the change from 0/255 to 1/255 is much bigger then from, say, 15/255 to 16/255. So if 1/255 is too bright, you are then unable to set it less bright. I think 1024 or ever 65535 makes sense with PWMs.

With values other than 255 we will not achieve 24-bit RGB, which is one problem, and the other one is non-linear brightness that can be achieved with PWM. So probably we would need to add an additional note in the documentation [0], saying that changing global brightness allows to preserve combined LED hue only when all sub-leds are linear, and that it will not be the case for PWM LEDs.

Quoted 5 lines

And I propose to change multi-led 'color' DT property value from LED_COLOR_ID_RGB to LED_COLOR_ID_MULTI to avoid the impression that it will work as traditional 24-bit RGB.

[0] Documentation/leds/leds-class-multicolor.rst

I know that color curves were being discussed at the time multicolor was being introduced, and AFAIK Pavel didn't like it, but I don't remember the reasons anymore.

As far as I understand it though, for PWM LEDs there is an equation for gamma correction. So either we need to rename this LED to MULTI, or the driver needs to do gamma correction so that the LED behaves RGB.

Pavel, what is your opinion on this?

Marek

#6 lore raw

Re: [PATCH v3 1/2] dt-bindings: leds: Add multicolor PWM LED bindings

Hello,

Am Sat, Jan 29, 2022 at 12:26:39AM +0100 schrieb Marek Behún:

Quoted 53 lines
Quoted 50 lines · On Sat, 29 Jan 2022 00:04:01 +0100 Jacek Anaszewski…
Quoted 32 lines · Marek Behún
Quoted 20 lines · On Thu, 27 Jan 2022 22:24:21 +0100 Jacek Anaszewski…

Hi Sven,

Quoted 7 lines · On 1/26/22 11:48 AM, [email protected] wrote:

From: Sven Schwermer <[email protected]>

This allows to group multiple PWM-connected monochrome LEDs into multicolor LEDs, e.g. RGB LEDs.

---

[...]

Quoted 9 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +9 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/leds/common.h>
+
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
Quoted 8 lines

Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml line 56

Quoted 5 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +5 -0
+
+        multi-led {
+          color = <LED_COLOR_ID_RGB>;
+          function = LED_FUNCTION_INDICATOR;
+          max-brightness = <65535>;  

It doesn't make much sense to have such a big resolution of global multi color brightness. 255 will be sufficient.

Quoted 5 lines

If the PWM supports it, why not?
On Omnia the default is 255, and since it is PWM, the change from 0/255 to 1/255 is much bigger then from, say, 15/255 to 16/255. So if 1/255 is too bright, you are then unable to set it less bright. I think 1024 or ever 65535 makes sense with PWMs.

With values other than 255 we will not achieve 24-bit RGB, which is one problem, and the other one is non-linear brightness that can be achieved with PWM. So probably we would need to add an additional note in the documentation [0], saying that changing global brightness allows to preserve combined LED hue only when all sub-leds are linear, and that it will not be the case for PWM LEDs.

And I propose to change multi-led 'color' DT property value from LED_COLOR_ID_RGB to LED_COLOR_ID_MULTI to avoid the impression that it will work as traditional 24-bit RGB.

Quoted 8 lines
Quoted 1 line

[0] Documentation/leds/leds-class-multicolor.rst

I know that color curves were being discussed at the time multicolor was being introduced, and AFAIK Pavel didn't like it, but I don't remember the reasons anymore.

As far as I understand it though, for PWM LEDs there is an equation for gamma correction.

That's right, and it gets a little more complicated if you have RGB instead of a single LED. A start for reading might be this:

https://hackaday.com/2016/08/23/rgb-leds-how-to-master-gamma-and-hue-for-perfect-brightness/

(I had bookmarked that back when I was hacking on firmware for an 8bit microcontroller controlling an RGB LED through soft PWM few years ago. A very simple solution is a precalculated static lookup table.)

Quoted 2 lines

So either we need to rename this LED to MULTI, or the driver needs to do gamma correction so that the LED behaves RGB.

Do those devices marked as RGB currently, have that gamma correction integrated on chip? Examples?

Greets
Alex

#7 lore raw

Re: [PATCH v3 1/2] dt-bindings: leds: Add multicolor PWM LED bindings

Hi,

Quoted 7 lines · Alexander Dahl
Quoted 6 lines

I know that color curves were being discussed at the time multicolor was being introduced, and AFAIK Pavel didn't like it, but I don't remember the reasons anymore.

As far as I understand it though, for PWM LEDs there is an equation for gamma correction.

Quoted 8 lines

That's right, and it gets a little more complicated if you have RGB instead of a single LED. A start for reading might be this:

https://hackaday.com/2016/08/23/rgb-leds-how-to-master-gamma-and-hue-for-perfect-brightness/

(I had bookmarked that back when I was hacking on firmware for an 8bit microcontroller controlling an RGB LED through soft PWM few years ago. A very simple solution is a precalculated static lookup table.)

This is a very interesting topic which I was gonna ask about on this list anyways. IMHO, the Linux leds subsystem needs a uniform way to deal with lightness/gamma correction. There is some previous work in pwm_bl and led_bl with their brightness-levels device tree property and cie1931 correction function (drivers/video/backlight/pwm_bl.c). It would be very nice to have this functionality in the led class. Having something similar for multi-color as well would be fantastic.

Sven

#8 lore raw

Re: [PATCH v3 1/2] dt-bindings: leds: Add multicolor PWM LED bindings

In reply to Jacek Anaszewski

Hi!

Quoted 27 lines
Quoted 21 lines
Quoted 12 lines
Quoted 2 lines

---

[...]

Quoted 9 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +9 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/leds/common.h>
+
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
Quoted 8 lines

Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml line 56

Quoted 5 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +5 -0
+
+        multi-led {
+          color = <LED_COLOR_ID_RGB>;
+          function = LED_FUNCTION_INDICATOR;
+          max-brightness = <65535>;

It doesn't make much sense to have such a big resolution of global multi color brightness. 255 will be sufficient.

Quoted 5 lines

If the PWM supports it, why not?
On Omnia the default is 255, and since it is PWM, the change from 0/255 to 1/255 is much bigger then from, say, 15/255 to 16/255. So if 1/255 is too bright, you are then unable to set it less bright. I think 1024 or ever 65535 makes sense with PWMs.

Quoted 6 lines

With values other than 255 we will not achieve 24-bit RGB, which is one problem, and the other one is non-linear brightness that can be achieved with PWM. So probably we would need to add an additional note in the documentation [0], saying that changing global brightness allows to preserve combined LED hue only when all sub-leds are linear, and that it will not be the case for PWM LEDs.

I have yet to see a LED that was anything but linear. If we'll want to match colorspace of RGB displays, we'll need to do more work. (Often, 255/255/255 is not white with RGB LEDs, for example).

So 16-bit pwm makes sense to me.

Best regards,
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
#9 lore raw

PATCH v3 2/2 leds: Add PWM multicolor driver

In reply to [email protected]

From: Sven Schwermer <[email protected]>

By allowing to group multiple monochrome PWM LEDs into multicolor LEDs, all involved LEDs can be controlled in-sync. This enables using effects using triggers, etc.

---

Notes:
Changes in v3:
* Release fwnode handles
* Sort header includes
* Remove deprecated device tree properties
* Remove deprecated LED_OFF
* Remove subled channel assignment
* s/pwmstate/state/

 drivers/leds/Kconfig               |   8 ++
 drivers/leds/Makefile              |   1 +
 drivers/leds/leds-pwm-multicolor.c | 184 +++++++++++++++++++++++++++++
 3 files changed, 193 insertions(+)
 create mode 100644 drivers/leds/leds-pwm-multicolor.c
drivers/leds/Kconfig +8 -0
index 6090e647daee..bae1f63f6195 100644
@@ -552,6 +552,14 @@ config LEDS_PWM
 	help
 	  This option enables support for pwm driven LEDs
 
+config LEDS_PWM_MULTICOLOR
+	tristate "PWM driven multi-color LED Support"
+	depends on LEDS_CLASS_MULTICOLOR
+	depends on PWM
+	help
+	  This option enables support for PWM driven monochrome LEDs that are
+	  grouped into multicolor LEDs.
+
 config LEDS_REGULATOR
 	tristate "REGULATOR driven LED support"
 	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index e58ecb36360f..ba2c2c1edf12 100644
@@ -73,6 +73,7 @@ obj-$(CONFIG_LEDS_PCA963X)		+= leds-pca963x.o
 obj-$(CONFIG_LEDS_PM8058)		+= leds-pm8058.o
 obj-$(CONFIG_LEDS_POWERNV)		+= leds-powernv.o
 obj-$(CONFIG_LEDS_PWM)			+= leds-pwm.o
+obj-$(CONFIG_LEDS_PWM_MULTICOLOR)	+= leds-pwm-multicolor.o
 obj-$(CONFIG_LEDS_REGULATOR)		+= leds-regulator.o
 obj-$(CONFIG_LEDS_S3C24XX)		+= leds-s3c24xx.o
 obj-$(CONFIG_LEDS_SC27XX_BLTC)		+= leds-sc27xx-bltc.o
drivers/leds/leds-pwm-multicolor.c new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
2.35.0
#10 lore raw

Re: [PATCH v3 2/2] leds: Add PWM multicolor driver

Quoted 6 lines · On Wed, Jan 26, 2022 at 11:05 PM <[email protected]> wrote:

From: Sven Schwermer <[email protected]>

By allowing to group multiple monochrome PWM LEDs into multicolor LEDs, all involved LEDs can be controlled in-sync. This enables using effects using triggers, etc.

...

drivers/leds/Kconfig config LEDS_PWM · line 561

Quoted 3 lines
drivers/leds/Kconfig +3 -0
+       help
+         This option enables support for PWM driven monochrome LEDs that are
+         grouped into multicolor LEDs.

What would be the module name if compiled as a module?

...

drivers/leds/leds-pwm-multicolor.c line 11

Quoted 4 lines
drivers/leds/leds-pwm-multicolor.c new file +4 -0
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/led-class-multicolor.h>
+#include <linux/leds.h>

mod_devicetable.h

drivers/leds/leds-pwm-multicolor.c line 14

Quoted 3 lines
drivers/leds/leds-pwm-multicolor.c new file +3 -0
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>

property.h

Quoted 1 line

+#include <linux/pwm.h>

...

drivers/leds/leds-pwm-multicolor.c line 35

Quoted 5 lines
drivers/leds/leds-pwm-multicolor.c new file +5 -0
+       int i;
+       unsigned long long duty;
+       int ret = 0;
+       struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
+       struct pwm_mc_led *priv = container_of(mc_cdev, struct pwm_mc_led, mc_cdev);

Can we order in reversed xmas tree order?

struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev); struct pwm_mc_led *priv = container_of(mc_cdev, struct

pwm_mc_led, mc_cdev);
       unsigned long long duty;
       int ret = 0;
       int i;

Same for other functions.

...

drivers/leds/leds-pwm-multicolor.c line 75

Quoted 3 lines
drivers/leds/leds-pwm-multicolor.c new file +3 -0
+               dev_err(&pdev->dev, "expected multi-led node\n");
+               ret = -ENODEV;
+               goto out;

return dev_err_probe(dev, -ENODEV, ...);

...

drivers/leds/leds-pwm-multicolor.c line 80

Quoted 3 lines
drivers/leds/leds-pwm-multicolor.c new file +3 -0
+       /* count the nodes inside the multi-led node */
+       fwnode_for_each_child_node(mcnode, fwnode)
+               ++count;

Postincrement shall work the same way.

...

drivers/leds/leds-pwm-multicolor.c line 112

Quoted 4 lines
drivers/leds/leds-pwm-multicolor.c new file +4 -0
+                       ret = PTR_ERR(pwmled->pwm);
+                       dev_err(&pdev->dev, "unable to request PWM: %d\n", ret);
+                       fwnode_handle_put(fwnode);
+                       goto destroy_mutex;

fwnode_handle_put();
return dev_err_probe(...);

...

drivers/leds/leds-pwm-multicolor.c line 120

Quoted 3 lines
drivers/leds/leds-pwm-multicolor.c new file +3 -0
+                       dev_err(&pdev->dev, "cannot read color: %d\n", ret);
+                       fwnode_handle_put(fwnode);
+                       goto destroy_mutex;

fwnode_handle_put();
return dev_err_probe();

Quoted 1 line

+ }

...

Quoted 1 line

+ ++priv->mc_cdev.num_colors;

Postincrement shall work the same way.

Quoted 1 line

+ }

...

drivers/leds/leds-pwm-multicolor.c line 135

Quoted 4 lines
drivers/leds/leds-pwm-multicolor.c new file +4 -0
+               dev_err(&pdev->dev,
+                       "failed to register multicolor PWM led for %s: %d\n",
+                       cdev->name, ret);
+               goto destroy_mutex;

return dev_err_probe();

...

drivers/leds/leds-pwm-multicolor.c line 142

Quoted 3 lines
drivers/leds/leds-pwm-multicolor.c new file +3 -0
+               dev_err(&pdev->dev, "failed to set led PWM value for %s: %d",
+                       cdev->name, ret);
+               goto destroy_mutex;

return dev_err_probe();

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+destroy_mutex:
+       mutex_destroy(&priv->lock);

Wrong ordering here and in ->remove().

Don't mix devm_* with non-devm_* calls.

drivers/leds/leds-pwm-multicolor.c line 153

Quoted 5 lines
drivers/leds/leds-pwm-multicolor.c new file +4 -0
+release_mcnode:
+       fwnode_handle_put(mcnode);
 
+out:
+       return ret;

Return directly.

Quoted 9 lines
drivers/leds/leds-pwm-multicolor.c new file +9 -0
+}
+
+static int led_pwm_mc_remove(struct platform_device *pdev)
+{
+       struct pwm_mc_led *priv = platform_get_drvdata(pdev);
+
+       mutex_destroy(&priv->lock);
+       return 0;
+}

...

drivers/leds/leds-pwm-multicolor.c line 166

Quoted 4 lines
drivers/leds/leds-pwm-multicolor.c new file +3 -0
+static const struct of_device_id of_pwm_leds_mc_match[] = {
+       { .compatible = "pwm-leds-multicolor", },
 
+       {},

No comma needed for terminator entry.

Quoted 1 line

+};

...

drivers/leds/leds-pwm-multicolor.c line 178

Quoted 10 lines
drivers/leds/leds-pwm-multicolor.c new file +9 -0
+static struct platform_driver led_pwm_mc_driver = {
+       .probe          = led_pwm_mc_probe,
+       .remove         = led_pwm_mc_remove,
+       .driver         = {
+               .name   = "leds_pwm_multicolor",
+               .of_match_table = of_pwm_leds_mc_match,
+       },
+};
 
+

Redundant blank line.

Quoted 1 line

+module_platform_driver(led_pwm_mc_driver);

--
With Best Regards,
Andy Shevchenko

#11 lore raw

Re: [PATCH v3 2/2] leds: Add PWM multicolor driver

Hi Andy,

Thanks for looking through my patch. I have a couple of follow-up questions about your feedback:

Quoted 7 lines · Andy Shevchenko

drivers/leds/leds-pwm-multicolor.c line 112

Quoted 4 lines
drivers/leds/leds-pwm-multicolor.c new file +4 -0
+                       ret = PTR_ERR(pwmled->pwm);
+                       dev_err(&pdev->dev, "unable to request PWM: %d\n", ret);
+                       fwnode_handle_put(fwnode);
+                       goto destroy_mutex;

fwnode_handle_put();
return dev_err_probe(...);

This would skip the destruction of the mutex and releasing of mcnode. Isn't that problematic? The same goes for all of your comments of this kind.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+destroy_mutex:
+       mutex_destroy(&priv->lock);

Wrong ordering here and in ->remove().

Don't mix devm_* with non-devm_* calls.

What do you mean by this?

Best regards,
Sven

A message that is not in the archive (sent off-list or not yet received): CAHp75VeSD5bYERp=s9Dzd0xScVc+sYSdc8W4XBfCVXJgyWMPyA@mail.gmail.com

#13 lore raw

Re: [PATCH v3 2/2] leds: Add PWM multicolor driver

Quoted 1 line

Skipping mutex destruction is not critical, but in general if you wish to free allocated resource, it should be done in reversed order...

As far as I can tell, the ordering is already reversed:

mcnode = device_get_named_child_node(&pdev->dev, "multi-led"); /* ... */
mutex_init(&priv->lock);
/* ... */

fwnode_for_each_child_node(mcnode, fwnode) {
    /* ... */
    fwnode_handle_put(fwnode);
    goto destroy_mutex;
    /* ... */
}
/* ... */
destroy_mutex:
    mutex_destroy(&priv->lock);
release_mcnode:
    fwnode_handle_put(mcnode);
out:
    return ret;
Quoted 6 lines

+destroy_mutex:
+       mutex_destroy(&priv->lock);

Wrong ordering here and in ->remove().

Quoted 8 lines

Don't mix devm_* with non-devm_* calls.

What do you mean by this?

...which is exactly the issue with this code because of the use of devm_*() calls mixed with non-devm_*() ones.

TL;DR: ordering is broken here. And to fix it you need either a) to convert all calls to be devm_*(), or b) make them all non-devm, or c) regroup resource allocation so that all devm followed by non-devm ones.Which non-devm calls are you referring to?

Thanks for your patience,
Sven

#14 lore raw

Re: [PATCH v3 2/2] leds: Add PWM multicolor driver

Quoted 4 lines · Sven Schwermer
Quoted 3 lines

Skipping mutex destruction is not critical, but in general if you wish to free allocated resource, it should be done in reversed order...

As far as I can tell, the ordering is already reversed:

I can't see it.

What I see is that mutex_destroy() is called before the LED class unregisters (along with PWM and memory allocation). It means potentially it may be the issue when during ->probe() error path or ->remove() somebody calls for LED functions which rely on mutex presence (while it has been already destroyed).

Easiest way to fix this is to wrap mutex_destroy() to be a devm_*() kind of function.

Quoted 17 lines
Quoted 17 lines

+destroy_mutex:
+ mutex_destroy(&priv->lock);

Wrong ordering here and in ->remove().

Don't mix devm_* with non-devm_* calls.

What do you mean by this?

...which is exactly the issue with this code because of the use of devm_*() calls mixed with non-devm_*() ones.

TL;DR: ordering is broken here. And to fix it you need either a) to
convert all calls to be devm_*(), or b) make them all non-devm, or c)
regroup resource allocation so that all devm followed by non-devm ones.Which non-devm calls are you referring to?

Signature · 2 lines
With Best Regards,
Andy Shevchenko