Skip to contentSkip to conversation

Multicolor PWM LED support

PATCH In review [email protected] 2 participants 6 messages Feb 7, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 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                          |  11 ++
 drivers/leds/Makefile                         |   1 +
 drivers/leds/leds-pwm-multicolor.c            | 186 ++++++++++++++++++
 4 files changed, 273 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml
 create mode 100644 drivers/leds/leds-pwm-multicolor.c

Interdiff against v4:

drivers/leds/leds-pwm-multicolor.c +40 -24
index 96712b8ca98e..45e38708ecb1 100644
@@ -58,6 +58,43 @@ static int led_pwm_mc_set(struct led_classdev *cdev,
 	return ret;
 }
 
+static int iterate_subleds(struct device *dev, struct pwm_mc_led *priv,
+			   struct fwnode_handle *mcnode)
+{
+	struct mc_subled *subled = priv->mc_cdev.subled_info;
+	struct fwnode_handle *fwnode;
+	struct pwm_led *pwmled;
+	u32 color;
+	int ret;
+
+	/* iterate over the nodes inside the multi-led node */
+	fwnode_for_each_child_node(mcnode, fwnode) {
+		pwmled = &priv->leds[priv->mc_cdev.num_colors];
+		pwmled->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
+		if (IS_ERR(pwmled->pwm)) {
+			ret = PTR_ERR(pwmled->pwm);
+			dev_err(dev, "unable to request PWM: %d\n", ret);
+			goto release_fwnode;
+		}
+		pwm_init_state(pwmled->pwm, &pwmled->state);
+
+		ret = fwnode_property_read_u32(fwnode, "color", &color);
+		if (ret) {
+			dev_err(dev, "cannot read color: %d\n", ret);
+			goto release_fwnode;
+		}
+
+		subled[priv->mc_cdev.num_colors].color_index = color;
+		priv->mc_cdev.num_colors++;
+	}
+
+	return 0;
+
+release_fwnode:
+	fwnode_handle_put(fwnode);
+	return ret;
+}
+
 static int led_pwm_mc_probe(struct platform_device *pdev)
 {
 	struct fwnode_handle *mcnode, *fwnode;
@@ -65,10 +102,8 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 	struct led_classdev *cdev;
 	struct mc_subled *subled;
 	struct pwm_mc_led *priv;
-	struct pwm_led *pwmled;
 	int count = 0;
 	int ret = 0;
-	u32 color;
 
 	mcnode = device_get_named_child_node(&pdev->dev, "multi-led");
 	if (!mcnode)
@@ -101,28 +136,9 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 	cdev->flags = LED_CORE_SUSPENDRESUME;
 	cdev->brightness_set_blocking = led_pwm_mc_set;
 
-	/* iterate over the nodes inside the multi-led node */
-	fwnode_for_each_child_node(mcnode, fwnode) {
-		pwmled = &priv->leds[priv->mc_cdev.num_colors];
-		pwmled->pwm = devm_fwnode_pwm_get(&pdev->dev, fwnode, NULL);
-		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 release_mcnode;
-		}
-		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 release_mcnode;
-		}
-
-		subled[priv->mc_cdev.num_colors].color_index = color;
-		priv->mc_cdev.num_colors++;
-	}
+	ret = iterate_subleds(&pdev->dev, priv, mcnode);
+	if (ret)
+		goto release_mcnode;
 
 	init_data.fwnode = mcnode;
 	ret = devm_led_classdev_multicolor_register_ext(&pdev->dev,
Signature · 1 line
2.35.1
#1 lore raw

PATCH v5 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 v5:
* (no changes)

Changes in v4:
* (no changes)

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.1
maintainer
#2 lore raw

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

Quoted 41 lines · On Mon, Feb 07, 2022 at 11:03:25AM +0100, [email protected] wrote:

From: Sven Schwermer <[email protected]>

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

---

Notes:
Changes in v5:
* (no changes)

Changes in v4:
* (no changes)

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 +13 -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

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

Quoted 10 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +10 -0
+  LED using the multicolor LED class.
+
+properties:
+  compatible:
+    const: pwm-leds-multicolor
+
+  multi-led:
+    type: object
+    allOf:
+      - $ref: leds-class-multicolor.yaml#

This schema says 'multi-led' here should have a child called "^multi-led@([0-9a-f])$". You are off a level.

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

Quoted 4 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +4 -0
+
+    patternProperties:
+      "^led-[0-9a-z]+$":
+        type: object

$ref: common.yaml#
additionalProperties: false

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

Quoted 8 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +8 -0
+        properties:
+          pwms:
+            maxItems: 1
+
+          pwm-names: true
+
+          color:
+            $ref: common.yaml#/properties/color

And then drop this ref.

Quoted 8 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +8 -0
+
+        required:
+          - pwms
+          - color
+
+required:
+  - compatible
+

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

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

Can't this be collapsed into 1 level? I don't see "pwm-leds-multicolor" having other child nodes.

Quoted 26 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +22 -0
+          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.1
#3 lore raw

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

Hi Rob,

Thanks for your comments.

Quoted 11 lines · Rob Herring

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

Quoted 8 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +8 -0
+properties:
+  compatible:
+    const: pwm-leds-multicolor
+
+  multi-led:
+    type: object
+    allOf:
+      - $ref: leds-class-multicolor.yaml#

This schema says 'multi-led' here should have a child called "^multi-led@([0-9a-f])$". You are off a level.

So it should have been?

properties:
compatible:
const: pwm-leds-multicolor
allOf:
- $ref: leds-class-multicolor.yaml#

This would imply that the multi-led node requires a unit address (reg property). That doesn't make sense in this case. How should we resolve this?

Quoted 7 lines

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

Quoted 4 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +4 -0
+
+    patternProperties:
+      "^led-[0-9a-z]+$":
+        type: object

$ref: common.yaml#
additionalProperties: false

Sounds good.

Quoted 10 lines

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

Quoted 8 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +8 -0
+        properties:
+          pwms:
+            maxItems: 1
+
+          pwm-names: true
+
+          color:
+            $ref: common.yaml#/properties/color

And then drop this ref.

Curiosity question: why? Should I refer to an unsigned integer type instead?

Quoted 7 lines

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

Quoted 4 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +4 -0
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
+
+        multi-led {

Can't this be collapsed into 1 level? I don't see "pwm-leds-multicolor" having other child nodes.

It could. The reason I added the multi-led level is because the leds-class-multicolor.yaml schema calls for it. Perhaps I missed the intention of that schema but isn't it there to create a uniform binding schema structure across drivers?

Best regards,
Sven

maintainer
#4 lore raw

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

Quoted 18 lines · Sven Schwermer

Hi Rob,

Thanks for your comments.

Quoted 11 lines · Rob Herring

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

Quoted 8 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +8 -0
+properties:
+  compatible:
+    const: pwm-leds-multicolor
+
+  multi-led:
+    type: object
+    allOf:
+      - $ref: leds-class-multicolor.yaml#

This schema says 'multi-led' here should have a child called "^multi-led@([0-9a-f])$". You are off a level.

Quoted 7 lines

So it should have been?

properties:
compatible:
const: pwm-leds-multicolor
allOf:
- $ref: leds-class-multicolor.yaml#

Not quite. DT property names and json-schema vocab names should never be at the same level. So allOf should be at the root level.

Quoted 2 lines

This would imply that the multi-led node requires a unit address (reg property). That doesn't make sense in this case. How should we resolve this?

I meant to mention that. Update the regex pattern to allow just 'multi-led': "^multi-led(@[0-9a-f])?$"

Quoted 20 lines
Quoted 6 lines

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

Quoted 3 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +3 -0
+    patternProperties:
+      "^led-[0-9a-z]+$":
+        type: object

$ref: common.yaml#
additionalProperties: false

Sounds good.

Quoted 10 lines

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

Quoted 8 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +8 -0
+        properties:
+          pwms:
+            maxItems: 1
+
+          pwm-names: true
+
+          color:
+            $ref: common.yaml#/properties/color

And then drop this ref.

Quoted 1 line

Curiosity question: why? Should I refer to an unsigned integer type instead?

Generally we want schemas to apply to nodes rather than individual properties. Think of each node as a class and nodes can be 1 or more subclasses. It's more important when using 'unevaluatedProperties' in combination with refs.

'color: true' is all you need here. So it's less duplication. Not so much here since it is just 1 property, but in general.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Documentation/devicetree/bindings/leds/leds-pwm-multicolor.yaml new file +4 -0
+    rgb-led {
+        compatible = "pwm-leds-multicolor";
+
+        multi-led {
Quoted 7 lines
Quoted 2 lines

Can't this be collapsed into 1 level? I don't see "pwm-leds-multicolor" having other child nodes.

It could. The reason I added the multi-led level is because the leds-class-multicolor.yaml schema calls for it. Perhaps I missed the intention of that schema but isn't it there to create a uniform binding schema structure across drivers?

Yeah, I guess that's a good enough reason.

Rob

#5 lore raw

PATCH v5 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 v5:
* Factor iteration over subleds out into function

Changes in v4:
* Remove mutex destruction and remove function
* Include missing headers
* Use post-increment instead of pre-increment
* Variable declarations in reverse xmas tree order
* Use dev_err_probe where possible
* Return immediately where possible
* Cosmetic changes
* Document LKM name

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               |  11 ++
 drivers/leds/Makefile              |   1 +
 drivers/leds/leds-pwm-multicolor.c | 186 +++++++++++++++++++++++++++++
 3 files changed, 198 insertions(+)
 create mode 100644 drivers/leds/leds-pwm-multicolor.c
drivers/leds/Kconfig +11 -0
index 6090e647daee..e70a46704076 100644
@@ -552,6 +552,17 @@ 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.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called leds-pwm-multicolor.
+
 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 +186 -0

186 lines not on this page · raw message

Signature · 1 line
2.35.1