Skip to contentSkip to conversation

Multicolor Framwork

PATCH Superseded Dan Murphy 2 participants 25 messages Jul 25, 2019 – Aug 27, 2019

Subsystems (3)

v4 of 17 Superseded Latest is v17 →

lore
Share & tools
lore

Dan Murphy

author
#0 lore raw

v4 cover Multicolor Framwork

Hello!

Here is the multicolor framework reworked with numerous changes. Most importantly is the ABI to produce a color.

brightness file controls the cluster based on the color intensity set. The color intensity can be set either through the colors/color_mix file, which requires all LED values within the cluster to be written or the LED's intensity can be controlled individually via the colors/<color>/intensity file.

This code has been tested using the LP5523 EVM and the LP50xx evms. Triggers work in this model. I have performed tests to confirm that the color requested stays consistent when ramping from LED_OFF to LED_FULL and back down to LED_OFF.

I anticipate another round of patches but I would like to get comments on all the code.

Dan

Dan Murphy (9):
leds: multicolor: Add sysfs interface definition
documention: leds: Add multicolor class documentation
dt: bindings: Add multicolor class dt bindings documention
dt-bindings: leds: Add multicolor ID to the color ID list
leds: Add multicolor ID to the color ID list
leds: multicolor: Introduce a multicolor class definition
dt: bindings: lp50xx: Introduce the lp50xx family of RGB drivers
leds: lp50xx: Add the LP50XX family of the RGB LED driver
leds: Update the lp55xx to use the multi color framework

 .../ABI/testing/sysfs-class-led-multicolor    |  67 ++
 .../bindings/leds/leds-class-multicolor.txt   |  96 +++
 .../devicetree/bindings/leds/leds-lp50xx.txt  | 148 ++++
 Documentation/leds/leds-class-multicolor.txt  | 152 ++++
 drivers/leds/Kconfig                          |  17 +
 drivers/leds/Makefile                         |   2 +
 drivers/leds/led-class-multicolor.c           | 402 +++++++++
 drivers/leds/led-core.c                       |   1 +
 drivers/leds/leds-lp50xx.c                    | 789 ++++++++++++++++++
 drivers/leds/leds-lp5523.c                    |  13 +
 drivers/leds/leds-lp55xx-common.c             | 153 +++-
 drivers/leds/leds-lp55xx-common.h             |  10 +
 include/dt-bindings/leds/common.h             |   3 +-
 include/linux/led-class-multicolor.h          |  96 +++
 include/linux/platform_data/leds-lp55xx.h     |   6 +
 modules.builtin.modinfo                       | Bin 0 -> 43550 bytes
 16 files changed, 1931 insertions(+), 24 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-led-multicolor
 create mode 100644 Documentation/devicetree/bindings/leds/leds-class-multicolor.txt
 create mode 100644 Documentation/devicetree/bindings/leds/leds-lp50xx.txt
 create mode 100644 Documentation/leds/leds-class-multicolor.txt
 create mode 100644 drivers/leds/led-class-multicolor.c
 create mode 100644 drivers/leds/leds-lp50xx.c
 create mode 100644 include/linux/led-class-multicolor.h
 create mode 100644 modules.builtin.modinfo
Signature · 1 line
2.22.0.214.g8dca754b1e
author
#20 lore raw

PATCH v4 9/9 leds: Update the lp55xx to use the multi color framework

Update the lp5523 to use the multi color framework.

---
 drivers/leds/leds-lp5523.c                |  13 ++
 drivers/leds/leds-lp55xx-common.c         | 153 ++++++++++++++++++----
 drivers/leds/leds-lp55xx-common.h         |  10 ++
 include/linux/platform_data/leds-lp55xx.h |   6 +
 modules.builtin.modinfo                   | Bin 0 -> 43550 bytes
 5 files changed, 159 insertions(+), 23 deletions(-)
 create mode 100644 modules.builtin.modinfo
drivers/leds/leds-lp5523.c +13 -0
index d0b931a136b9..45380b32563f 100644
@@ -791,6 +791,18 @@ static ssize_t store_master_fader_leds(struct device *dev,
 	return ret;
 }
 
+static int lp5523_led_intensity(struct lp55xx_led *led, int chan_num)
+{
+	struct lp55xx_chip *chip = led->chip;
+	int ret;
+
+	mutex_lock(&chip->lock);
+	ret = lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + chan_num,
+		     led->brightness);
+	mutex_unlock(&chip->lock);
+	return ret;
+}
+
 static int lp5523_led_brightness(struct lp55xx_led *led)
 {
 	struct lp55xx_chip *chip = led->chip;
@@ -857,6 +869,7 @@ static struct lp55xx_device_config lp5523_cfg = {
 	.max_channel  = LP5523_MAX_LEDS,
 	.post_init_device   = lp5523_post_init_device,
 	.brightness_fn      = lp5523_led_brightness,
+	.intensity_fn       = lp5523_led_intensity,
 	.set_led_current    = lp5523_set_led_current,
 	.firmware_cb        = lp5523_firmware_loaded,
 	.run_engine         = lp5523_run_engine,
drivers/leds/leds-lp55xx-common.c +130 -23

216 lines not on this page · raw message

drivers/leds/leds-lp55xx-common.h +10 -0
index 783ed5103ce5..0737e79bc21b 100644
@@ -12,6 +12,8 @@
 #ifndef _LEDS_LP55XX_COMMON_H
 #define _LEDS_LP55XX_COMMON_H
 
+#include <linux/led-class-multicolor.h>
+
 enum lp55xx_engine_index {
 	LP55XX_ENGINE_INVALID,
 	LP55XX_ENGINE_1,
@@ -109,6 +111,9 @@ struct lp55xx_device_config {
 	/* access brightness register */
 	int (*brightness_fn)(struct lp55xx_led *led);
 
+	/* access specific brightness register */
+	int (*intensity_fn)(struct lp55xx_led *led, int chan_num);
+
 	/* current setting function */
 	void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
 
@@ -159,6 +164,7 @@ struct lp55xx_chip {
  * struct lp55xx_led
  * @chan_nr         : Channel number
  * @cdev            : LED class device
+ * @mc_cdev	    : Multi color class device
  * @led_current     : Current setting at each led channel
  * @max_current     : Maximun current at each led channel
  * @brightness      : Brightness value
@@ -167,9 +173,13 @@ struct lp55xx_chip {
 struct lp55xx_led {
 	int chan_nr;
 	struct led_classdev cdev;
+	struct led_classdev_mc mc_cdev;
 	u8 led_current;
 	u8 max_current;
 	u8 brightness;
+/*	int num_colors;*/
+	int channel_color[LED_COLOR_ID_MAX];
+	int grouped_channels[LED_COLOR_ID_MAX];
 	struct lp55xx_chip *chip;
 };
 
include/linux/platform_data/leds-lp55xx.h +6 -0
index 96a787100fda..adce8cec411b 100644
@@ -12,6 +12,8 @@
 #ifndef _LEDS_LP55XX_H
 #define _LEDS_LP55XX_H
 
+#include <linux/led-class-multicolor.h>
+
 /* Clock configuration */
 #define LP55XX_CLOCK_AUTO	0
 #define LP55XX_CLOCK_INT	1
@@ -23,6 +25,10 @@ struct lp55xx_led_config {
 	u8 chan_nr;
 	u8 led_current; /* mA x10, 0 if led is not connected */
 	u8 max_current;
+	int num_colors;
+	int available_colors;
+	u32 channel_color[LED_COLOR_ID_MAX];
+	int grouped_channels[LED_COLOR_ID_MAX];
 };
 
 struct lp55xx_predef_pattern {
modules.builtin.modinfo new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..e528d8f57796621b6cfef52ad0da44551a482481
GIT binary patch
Signature · 1 line
2.22.0.214.g8dca754b1e
author
#6 lore raw

PATCH v4 3/9 dt: bindings: Add multicolor class dt bindings documention

In reply to Dan Murphy

Add DT bindings for the LEDs multicolor class framework.

---
 .../bindings/leds/leds-class-multicolor.txt   | 96 +++++++++++++++++++
 1 file changed, 96 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-class-multicolor.txt
Documentation/devicetree/bindings/leds/leds-class-multicolor.txt new file +96 -0
new file mode 100644
index 000000000000..5d36327b18fc
@@ -0,0 +1,96 @@
+* Multicolor LED properties
+
+Multicolor LEDs can consist of a RGB, RGBW or a RGBA LED clusters.  These devices
+can be grouped together and also provide a modeling mechanism so that the
+cluster LEDs can vary in hue and intensity to produce a wide range of colors.
+
+The nodes and properties defined in this document are unique to the multicolor
+LED class.  Common LED nodes and properties are inherited from the common.txt
+within this documentation directory.
+
+Required LED Child properties:
+	- color : For multicolor LED support this property should be defined as
+		  LED_COLOR_ID_MULTI and further definition can be found in
+		  include/linux/leds/common.h.
+
+led-controller@30 {
+	#address-cells = <1>;
+	#size-cells = <0>;
+	compatible = "ti,lp5024";
+	reg = <0x29>;
+
+	multi-led@1 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <1>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_;
+
+
+		led@3 {
+			reg = <3>;
+			color = <LED_COLOR_ID_RED>;
+		};
+
+		led@4 {
+			reg = <4>;
+			color = <LED_COLOR_ID_GREEN>;
+		};
+
+		led@5 {
+			reg = <5>;
+			color = <LED_COLOR_ID_BLUE>;
+		};
+	};
+
+	multi-led@2 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_ACTIVITY;
+		reg = <2>;
+		ti,led-bank = <2 3 5>;
+
+		led@6 {
+			reg = <0x6>;
+			color = <LED_COLOR_ID_RED>;
+			led-sources = <6 9 15>;
+		};
+
+		led@7 {
+			reg = <0x7>;
+			color = <LED_COLOR_ID_GREEN>;
+			led-sources = <7 10 16>;
+		};
+
+		led@8 {
+			reg = <0x8>;
+			color = <LED_COLOR_ID_BLUE>;
+			led-sources = <8 11 17>;
+		};
+	};
+
+	multi-led@4 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <4>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_ACTIVITY;
+
+		led@12 {
+			reg = <12>;
+			color = <LED_COLOR_ID_RED>;
+		};
+
+		led@13 {
+			reg = <13>;
+			color = <LED_COLOR_ID_GREEN>;
+		};
+
+		led@14 {
+			reg = <14>;
+			color = <LED_COLOR_ID_BLUE>;
+		};
+	};
+
+};
Signature · 1 line
2.22.0.214.g8dca754b1e
author
#17 lore raw

PATCH v4 8/9 leds: lp50xx: Add the LP50XX family of the RGB LED driver

In reply to Dan Murphy

Introduce the LP5036/30/24/18 RGB LED driver.
The difference in these parts are the number of
LED outputs where the:

LP5036 can control 36 LEDs
LP5030 can control 30 LEDs
LP5024 can control 24 LEDs
LP5018 can control 18 LEDs

The device has the ability to group LED output into control banks so that multiple LED banks can be controlled with the same mixing and brightness. Inversely the LEDs can also be controlled independently.

---
 drivers/leds/Kconfig       |   7 +
 drivers/leds/Makefile      |   1 +
 drivers/leds/leds-lp50xx.c | 789 +++++++++++++++++++++++++++++++++++++
 3 files changed, 797 insertions(+)
 create mode 100644 drivers/leds/leds-lp50xx.c
drivers/leds/Kconfig +7 -0
index 7f780d5b8490..69c037020f6b 100644
@@ -350,6 +350,13 @@ config LEDS_LP3952
 	  To compile this driver as a module, choose M here: the
 	  module will be called leds-lp3952.
 
+config LEDS_LP50XX
+	tristate "LED Support for TI LP5036/30/24/18 LED driver chip"
+	depends on LEDS_CLASS && REGMAP_I2C
+	help
+	  If you say yes here you get support for the Texas Instruments
+	  LP5036, LP5030, LP5024 and LP5018 LED driver.
+
 config LEDS_LP55XX_COMMON
 	tristate "Common Driver for TI/National LP5521/5523/55231/5562/8501"
 	depends on LEDS_LP5521 || LEDS_LP5523 || LEDS_LP5562 || LEDS_LP8501
drivers/leds/Makefile +1 -0
index 897b810257dd..438a5499f3ee 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_LEDS_GPIO_REGISTER)	+= leds-gpio-register.o
 obj-$(CONFIG_LEDS_GPIO)			+= leds-gpio.o
 obj-$(CONFIG_LEDS_LP3944)		+= leds-lp3944.o
 obj-$(CONFIG_LEDS_LP3952)		+= leds-lp3952.o
+obj-$(CONFIG_LEDS_LP50XX)		+= leds-lp50xx.o
 obj-$(CONFIG_LEDS_LP55XX_COMMON)	+= leds-lp55xx-common.o
 obj-$(CONFIG_LEDS_LP5521)		+= leds-lp5521.o
 obj-$(CONFIG_LEDS_LP5523)		+= leds-lp5523.o
drivers/leds/leds-lp50xx.c new file +789 -0

789 lines not on this page · raw message

Signature · 1 line
2.22.0.214.g8dca754b1e
author
#16 lore raw

PATCH v4 7/9 dt: bindings: lp50xx: Introduce the lp50xx family of RGB drivers

In reply to Dan Murphy

Introduce the bindings for the Texas Instruments LP5036, LP5030, LP5024, LP5018, LP5012 and LP5009 RGB LED device driver. The LP5036/30/24/18/12/9 can control RGB LEDs individually or as part of a control bank group. These devices have the ability to adjust the mixing control for the RGB LEDs to obtain different colors independent of the overall brightness of the LED grouping.

Datasheet:
http://www.ti.com/lit/ds/symlink/lp5012.pdf
http://www.ti.com/lit/ds/symlink/lp5024.pdf
http://www.ti.com/lit/ds/symlink/lp5036.pdf

---
 .../devicetree/bindings/leds/leds-lp50xx.txt  | 148 ++++++++++++++++++
 1 file changed, 148 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-lp50xx.txt
Documentation/devicetree/bindings/leds/leds-lp50xx.txt new file +148 -0
new file mode 100644
index 000000000000..9d05f43042e0
@@ -0,0 +1,148 @@
+* Texas Instruments - LP5009/12/18/24/30/36 RGB LED driver
+
+The LP50XX is multi-channel, I2C RGB LED Drivers that can group RGB LEDs into
+a LED group or control them individually.
+
+The difference in these RGB LED drivers is the number of supported RGB modules.
+
+Required parent properties:
+	- compatible:
+		"ti,lp5009"
+		"ti,lp5012"
+		"ti,lp5018"
+		"ti,lp5024"
+		"ti,lp5030"
+		"ti,lp5036"
+	- reg :  I2C slave address
+		lp5009/12 - 0x28
+		lp5018/24 - 0x28
+		lp5030/36 - 0x30
+	- #address-cells : 1
+	- #size-cells : 0
+
+Optional parent properties:
+	- enable-gpios : gpio pin to enable/disable the device.
+	- vled-supply : LED supply
+
+Required child properties:
+	- #address-cells : 1
+	- #size-cells : 0
+	- reg : This is the LED module number.
+	- color : see Documentation/devicetree/bindings/leds/common.txt
+	- function : see Documentation/devicetree/bindings/leds/common.txt
+
+Required child properties only is LED modules will be banked:
+	- ti,led-bank : This property denotes the LED module numbers that will
+			be controlled as a single RGB cluster.  Each LED module
+			number will be controlled by a single LED class instance.
+			There can only be one instance of the ti,led-bank
+			property for each device node.
+
+Required grandchildren properties:
+	- reg : A single entry denoting the LED module that controls
+		the RGB cluster.
+	- color : see Documentation/devicetree/bindings/leds/leds-multicolor.txt
+	- led-sources : see Documentation/devicetree/bindings/leds/common.txt
+
+The LED outputs associated with the LED modules are defined in Table 1 of the
+corresponding data sheets.
+
+LP5009 - 2 Total RGB cluster LED outputs 0-1
+LP5012 - 4 Total RGB cluster LED outputs 0-3
+LP5018 - 6 Total RGB cluster LED outputs 0-5
+LP5024 - 8 Total RGB cluster LED outputs 0-7
+LP5030 - 10 Total RGB cluster LED outputs 0-9
+LP5036 - 12 Total RGB cluster LED outputs 0-11
+
+Optional child properties:
+	- label : see Documentation/devicetree/bindings/leds/common.txt
+	- linux,default-trigger :
+	   see Documentation/devicetree/bindings/leds/common.txt
+
+Examples:
+led-controller@29 {
+	#address-cells = <1>;
+	#size-cells = <0>;
+	compatible = "ti,lp5024";
+	reg = <0x29>;
+	enable-gpios = <&gpio1 28 GPIO_ACTIVE_HIGH>;
+	vled-supply = <&vmmcsd_fixed>;
+
+	multi-led@1 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <1>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_STATUS;
+
+		led@3 {
+			reg = <3>;
+			color = <LED_COLOR_ID_RED>;
+		};
+
+		led@4 {
+			reg = <4>;
+			color = <LED_COLOR_ID_GREEN>;
+		};
+
+		led@5 {
+			reg = <5>;
+			color = <LED_COLOR_ID_BLUE>;
+		};
+	};
+
+	multi-led@2 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <2>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_STANDBY;
+		ti,led-bank = <2 3 5>;
+
+		led@6 {
+			reg = <0x6>;
+			color = <LED_COLOR_ID_RED>;
+			led-sources = <6 9 15>;
+		};
+
+		led@7 {
+			reg = <0x7>;
+			color = <LED_COLOR_ID_GREEN>;
+			led-sources = <7 10 16>;
+		};
+
+		led@8 {
+			reg = <0x8>;
+			color = <LED_COLOR_ID_BLUE>;
+			led-sources = <8 11 17>;
+		};
+	};
+
+	multi-led@4 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <4>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_ACTIVITY;
+
+		led@12 {
+			reg = <12>;
+			color = <LED_COLOR_ID_RED>;
+		};
+
+		led@13 {
+			reg = <13>;
+			color = <LED_COLOR_ID_GREEN>;
+		};
+
+		led@14 {
+			reg = <14>;
+			color = <LED_COLOR_ID_BLUE>;
+		};
+	};
+};
+
+For more product information please see the link below:
+http://www.ti.com/lit/ds/symlink/lp5012.pdf
+http://www.ti.com/lit/ds/symlink/lp5024.pdf
+http://www.ti.com/lit/ds/symlink/lp5036.pdf
Signature · 1 line
2.22.0.214.g8dca754b1e
author
#9 lore raw

PATCH v4 5/9 leds: Add multicolor ID to the color ID list

In reply to Dan Murphy

Add a new color ID that is declared as MULTICOLOR as with the multicolor framework declaring a definitive color is not accurate as the node can contain multiple colors.

---
 drivers/leds/led-core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/leds/led-core.c +1 -0
index f0c1c403f678..52422b1ee1f3 100644
@@ -34,6 +34,7 @@ const char * const led_colors[LED_COLOR_ID_MAX] = {
 	[LED_COLOR_ID_VIOLET] = "violet",
 	[LED_COLOR_ID_YELLOW] = "yellow",
 	[LED_COLOR_ID_IR] = "ir",
+	[LED_COLOR_ID_MULTI] = "multicolor",
 };
 EXPORT_SYMBOL_GPL(led_colors);
 
Signature · 1 line
2.22.0.214.g8dca754b1e
author
#10 lore raw

PATCH v4 6/9 leds: multicolor: Introduce a multicolor class definition

In reply to Dan Murphy

Introduce a multicolor class that groups colored LEDs within a LED node.

The framework allows for dynamically setting individual LEDs or setting brightness levels of LEDs and updating them virtually simultaneously.

---
 drivers/leds/Kconfig                 |  10 +
 drivers/leds/Makefile                |   1 +
 drivers/leds/led-class-multicolor.c  | 402 +++++++++++++++++++++++++++
 include/linux/led-class-multicolor.h |  96 +++++++
 4 files changed, 509 insertions(+)
 create mode 100644 drivers/leds/led-class-multicolor.c
 create mode 100644 include/linux/led-class-multicolor.h
drivers/leds/Kconfig +10 -0
index f7a3dd7ecf3d..7f780d5b8490 100644
@@ -30,6 +30,16 @@ config LEDS_CLASS_FLASH
 	  for the flash related features of a LED device. It can be built
 	  as a module.
 
+config LEDS_CLASS_MULTI_COLOR
+	tristate "LED Mulit Color LED Class Support"
+	depends on LEDS_CLASS
+	help
+	  This option enables the multicolor LED sysfs class in /sys/class/leds.
+	  It wraps LED Class and adds multicolor LED specific sysfs attributes
+	  and kernel internal API to it. You'll need this to provide support
+	  for multicolor LEDs that are grouped together. This class is not
+	  intended for single color LEDs. It can be built as a module.
+
 config LEDS_BRIGHTNESS_HW_CHANGED
 	bool "LED Class brightness_hw_changed attribute support"
 	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index 41fb073a39c1..897b810257dd 100644
@@ -4,6 +4,7 @@
 obj-$(CONFIG_NEW_LEDS)			+= led-core.o
 obj-$(CONFIG_LEDS_CLASS)		+= led-class.o
 obj-$(CONFIG_LEDS_CLASS_FLASH)		+= led-class-flash.o
+obj-$(CONFIG_LEDS_CLASS_MULTI_COLOR)	+= led-class-multicolor.o
 obj-$(CONFIG_LEDS_TRIGGERS)		+= led-triggers.o
 
 # LED Platform Drivers
drivers/leds/led-class-multicolor.c new file +402 -0

402 lines not on this page · raw message

include/linux/led-class-multicolor.h new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.22.0.214.g8dca754b1e
author
#1 lore raw

PATCH v4 1/9 leds: multicolor: Add sysfs interface definition

In reply to Dan Murphy

Add a documentation of LED Multicolor LED class specific sysfs attributes.

---
 .../ABI/testing/sysfs-class-led-multicolor    | 67 +++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-led-multicolor
Documentation/ABI/testing/sysfs-class-led-multicolor new file +67 -0
new file mode 100644
index 000000000000..59839f0eae76
@@ -0,0 +1,67 @@
+What:		/sys/class/leds/<led>/brightness
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read/write
+		The multicolor class will redirect the device drivers call back
+		function for brightness control to the multicolor class
+		brightness control function.
+
+		Writing to this file will update all LEDs within the group to a
+		calculated percentage of what each color LED in the group is set
+		to.  Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.
+
+		The value of the color is from 0 to
+		/sys/class/leds/<led>/max_brightness.
+
+What:		/sys/class/leds/<led>/colors/color_mix
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read/write
+		The color_mix file allows writing all registered multicolor LEDs
+		virtually at the same time.  The value(s) written to this file
+		contain the intensity values for each multicolor LED within
+		the colors directory.  The color indexes are reported in the
+		color_id file as defined in this document.
+		Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.
+
+What:		/sys/class/leds/<led>/colors/<led_color>/color_id
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read only
+		This file when read will return the index of the color in the
+		color_mix.
+		Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.
+
+What:		/sys/class/leds/<led>/colors/<led_color>/intensity
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read/write
+		The led_color directory is dynamically created based on the
+		colors defined by the registrar of the class.
+		The led_color can be but not limited to red, green, blue,
+		white, amber, yellow and violet.  Drivers can also declare a
+		LED color for presentation.  There is one directory per color
+		presented.  The brightness file is created under each
+		led_color directory and controls the individual LED color
+		setting.
+
+		The value of the color is from 0 to
+		/sys/class/leds/<led>/colors/<led_color>/max_intensity.
+
+What:		/sys/class/leds/<led>/colors/<led_color>/max_intensity
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read only
+		Maximum intensity level for the LED color, default is
+		255 (LED_FULL).
+
+		If the LED does not support different intensity levels, this
+		should be 1.
Signature · 1 line
2.22.0.214.g8dca754b1e
author
#4 lore raw

PATCH v4 2/9 documention: leds: Add multicolor class documentation

In reply to Dan Murphy

Add the support documentation on the multicolor LED framework. This document defines the directores and file generated by the multicolor framework. It also documents usage.

---
 Documentation/leds/leds-class-multicolor.txt | 152 +++++++++++++++++++
 1 file changed, 152 insertions(+)
 create mode 100644 Documentation/leds/leds-class-multicolor.txt
Documentation/leds/leds-class-multicolor.txt new file +152 -0
new file mode 100644
index 000000000000..3b354e523660
@@ -0,0 +1,152 @@
+
+Multi Color LED handling under Linux
+=================================================
+
+Author: Dan Murphy <[email protected]>
+
+Description
+-----------
+There are varying monochrome LED colors available for application.  These
+LEDs can be used as a single use case LED or can be mixed with other color
+LEDs to produce the full spectrum of color.  Color LEDs that are grouped
+can be presented under a single LED node with individual color control.
+The multicolor class groups these LEDs and allows dynamically setting the value
+of a single LED or setting the intensity values of the LEDs in the group and
+updating the LEDs virtually simultaneously.
+
+Multicolor Class Control
+-------------------------
+The multicolor class presents the LED groups under a directory called "colors".
+This directory is a child under the LED parent node created by the led_class
+framework.  The led_class framework is documented in led-class.txt within this
+documentation directory.
+
+Each colored LED is given its own directory.  These directories can be, but not
+limited to red, green, blue, white, amber, yellow and violet.  Under these
+directories the intensity and max_intensity files are presented for each LED.
+
+
+Directory Layout Example
+------------------------
+root:/sys/class/leds/rgb:grouped_leds# ls -lR colors/
+colors/:
+drwxr-xr-x    2 root     root             0 Jun 28 20:21 blue
+drwxr-xr-x    2 root     root             0 Jun 28 20:21 green
+drwxr-xr-x    2 root     root             0 Jun 28 20:21 red
+-rw-------    1 root     root          4096 Jun 28 20:21 color_mix
+
+colors/blue:
+-rw-------    1 root     root          4096 Jun 28 20:21 intensity
+-r--------    1 root     root          4096 Jun 28 20:27 max_intensity
+-r--------    1 root     root          4096 Jun 28 20:21 color_id
+
+colors/green:
+-rw-------    1 root     root          4096 Jun 28 20:22 intensity
+-r--------    1 root     root          4096 Jun 28 20:27 max_intensity
+-r--------    1 root     root          4096 Jun 28 20:21 color_id
+
+colors/red:
+-rw-------    1 root     root          4096 Jun 28 20:21 intensity
+-r--------    1 root     root          4096 Jun 28 20:27 max_intensity
+-r--------    1 root     root          4096 Jun 28 20:21 color_id
+
+Multicolor Color Mixing
+-----------------------
+Multicolor monochrome LEDs intensity can be modified and mixed to produce a
+varying array of colors.  The color_mix file gives the user the ability to write
+all the monochrome LEDs registered in the directory with a single call.
+To create a specific color from monochrome LEDs the color_mix file needs to be
+written with each color's intensity.  The order in which the monochrome LEDs
+should be written is based on the colors color_id.
+
+For example:
+cat /sys/class/leds/rgb:grouped_leds/red/color_id
+0
+cat /sys/class/leds/rgb:grouped_leds/green/color_id
+1
+cat /sys/class/leds/rgb:grouped_leds/blue/color_id
+2
+
+red - color_id = 0
+green - color_id = 1
+blue - color_id = 2
+
+These id's are the order in which to write the color_mix file.
+
+echo "<red> <green> <blue>" > color_mix
+
+echo "80 00 80" > color_mix
+
+The order of the monochrome LEDs are determined during multicolor class
+registration and will not change unless unregistered and re-registered.
+
+Other example with amber monochrome LED:
+blue - color_id = 0
+amber - color_id = 1
+
+In this exampe blue is at ID 0 and amber ID is 1 so the user would write
+echo "<blue> <amber>" > color_mix
+
+echo "38 80" > color_mix
+
+If a single monochrome LED needs to be modified then the user would write the
+colors/<color>/intensity file.
+
+
+Multicolor Class Brightness Control
+-----------------------------------
+The multiclor class framework will calculate each monochrome LEDs intensity.
+
+The brightness level for each LED is calculated based on the color LED
+intensity setting divided by the color LED max intensity setting multiplied by
+the requested value.
+
+led_brightness = requested_value * led_color_intensity/led_color_max_intensity
+
+Example:
+Three LEDs are present in the group as defined in "Directory Layout Example"
+within this document.
+
+A user first writes the color LED brightness file with the brightness level that
+is necessary to achieve a blueish violet output from the RGB LED group.
+
+echo 138 > /sys/class/leds/rgb:grouped_leds/red/intensity
+echo 43 > /sys/class/leds/rgb:grouped_leds/green/intensity
+echo 226 > /sys/class/leds/rgb:grouped_leds/blue/intensity
+
+red -
+	intensity = 138
+	max_intensity = 255
+green -
+	intensity = 43
+	max_intensity = 255
+blue -
+	intensity = 226
+	max_intensity = 255
+
+The user can control the brightness of that RGB group by writing the parent
+'brightness' control.  Assuming a parent max_brightness of 255 the user may want
+to dim the LED color group to half.  The user would write a value of 128 to the
+parent brightness file then the values written to each LED will be adjusted
+base on this value
+
+cat /sys/class/leds/rgb:grouped_leds/max_brightness
+255
+echo 128 > /sys/class/leds/rgb:grouped_leds/brightness
+
+adjusted_red_value = 128 * 138/255 = 69
+adjusted_green_value = 128 * 43/255 = 21
+adjusted_blue_value = 128 * 226/255 = 113
+
+Reading the parent brightness file will return the current brightness value of
+the color LED group.
+
+cat /sys/class/leds/rgb:grouped_leds/max_brightness
+255
+
+echo 128 > /sys/class/leds/rgb:grouped_leds/brightness
+
+cat /sys/class/leds/rgb:grouped_leds/max_brightness
+128
+
+
Signature · 1 line
2.22.0.214.g8dca754b1e
author
#8 lore raw

PATCH v4 4/9 dt-bindings: leds: Add multicolor ID to the color ID list

In reply to Dan Murphy

Add a new color ID that is declared as MULTICOLOR as with the multicolor framework declaring a definitive color is not accurate as the node can contain multiple colors.

---
 include/dt-bindings/leds/common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/dt-bindings/leds/common.h +2 -1
index 9e1256a7c1bf..7006d15f71e8 100644
@@ -29,7 +29,8 @@
 #define LED_COLOR_ID_VIOLET	5
 #define LED_COLOR_ID_YELLOW	6
 #define LED_COLOR_ID_IR		7
-#define LED_COLOR_ID_MAX	8
+#define LED_COLOR_ID_MULTI	8
+#define LED_COLOR_ID_MAX	9
 
 /* Standard LED functions */
 #define LED_FUNCTION_ACTIVITY "activity"
Signature · 1 line
2.22.0.214.g8dca754b1e
#2 lore raw

Re: [PATCH v4 1/9] leds: multicolor: Add sysfs interface definition

In reply to Dan Murphy

Hi Dan,

Thank you for the v4.

I have a bunch of comments below. Please take a look.

Quoted 18 lines · Dan Murphy

Add a documentation of LED Multicolor LED class specific sysfs attributes.

---
 .../ABI/testing/sysfs-class-led-multicolor    | 67 +++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-led-multicolor
Documentation/ABI/testing/sysfs-class-led-multicolor new file +3 -0
new file mode 100644
index 000000000000..59839f0eae76
@@ -0,0 +1,67 @@
+What:		/sys/class/leds/<led>/brightness
+Date:		Sept 2019
+KernelVersion:	TBD

Documentation/ABI/testing/sysfs-class-led-multicolor line 13

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +10 -0
+Contact:	Dan Murphy <[email protected]>
+Description:	read/write
+		The multicolor class will redirect the device drivers call back
+		function for brightness control to the multicolor class
+		brightness control function.
+
+		Writing to this file will update all LEDs within the group to a
+		calculated percentage of what each color LED in the group is set
+		to.  Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.

Instead of redirecting the reader to led-class-multicolor.txt I'd prefer to have at least the formula to calculate the colors laid out here. Aside of that - it is more helpful to have a full path to the referenced file.

Documentation/ABI/testing/sysfs-class-led-multicolor line 24

Quoted 11 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +11 -0
+
+		The value of the color is from 0 to
+		/sys/class/leds/<led>/max_brightness.
+
+What:		/sys/class/leds/<led>/colors/color_mix
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read/write
+		The color_mix file allows writing all registered multicolor LEDs
+		virtually at the same time.  The value(s) written to this file

I'd drop parentheses form "value(s)". Multi color LED class device is supposed to always have more then one LED. And if I understand it correctly we have to pass intensities of all colors supported by LED multicolor class device here, even we're changing single one.

Documentation/ABI/testing/sysfs-class-led-multicolor line 27

Quoted 3 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +3 -0
+		contain the intensity values for each multicolor LED within
+		the colors directory.  The color indexes are reported in the
+		color_id file as defined in this document.

This is a bit misleading. It sounds as if single color_id file would be reporting more than one index.

Documentation/ABI/testing/sysfs-class-led-multicolor line 29

Quoted 2 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +2 -0
+		Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.

Here, similarly as for brightness, I would prefer to have complete documentation of this file.

How about:

The values written to this file should contain the intensity values of each multicolor LED within the colors directory. The index of given color is reported by the color_id file present in colors/<color> directory. The index determines the position in the sequence of intensities on which the related intensity should be passed to this file.

And here we could have the examples from leds-class-multicolor.txt.

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +10 -0
+
+What:		/sys/class/leds/<led>/colors/<led_color>/color_id
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read only
+		This file when read will return the index of the color in the
+		color_mix.
+		Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.

Documentation/ABI/testing/sysfs-class-led-multicolor line 49

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +10 -0
+
+What:		/sys/class/leds/<led>/colors/<led_color>/intensity
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read/write
+		The led_color directory is dynamically created based on the
+		colors defined by the registrar of the class.
+		The led_color can be but not limited to red, green, blue,
+		white, amber, yellow and violet.  Drivers can also declare a

Instead of this vague sentence about the available colors I propose to maintain the list of supported colors in leds-class.rst or in a separate file and keep it in sync with the led_colors array. Then we could refer to that file here.

Documentation/ABI/testing/sysfs-class-led-multicolor line 50

Quoted 1 line
Documentation/ABI/testing/sysfs-class-led-multicolor new file +1 -0
+		LED color for presentation.  There is one directory per color

I'd not let drivers define their custom colors. It would entail issues related to lack of generic LED_COLOR_ID and DT parsing failure.

Quoted 18 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +17 -0
+		presented.  The brightness file is created under each
+		led_color directory and controls the individual LED color
+		setting.
+
+		The value of the color is from 0 to
+		/sys/class/leds/<led>/colors/<led_color>/max_intensity.
+
+What:		/sys/class/leds/<led>/colors/<led_color>/max_intensity
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy <[email protected]>
+Description:	read only
+		Maximum intensity level for the LED color, default is
+		255 (LED_FULL).
+
+		If the LED does not support different intensity levels, this
+		should be 1.
Signature · 2 lines
Best regards,
Jacek Anaszewski
#5 lore raw

Re: [PATCH v4 2/9] documention: leds: Add multicolor class documentation

In reply to Dan Murphy

Dan,

Now we will need this in rst format. It doesn't involve much tweaking to the txt, but some details like links to other documents can be enhanced.

I've also come across an issue of lack of line break after listed quoted strings but I don't see such occurrence here. There might be still some other quirks emerging after translating rst to html format.

Please check Documentation/doc-guide/sphinx.rst for starter.

Quoted 2 lines · Dan Murphy

Add the support documentation on the multicolor LED framework. This document defines the directores and file generated by the

s/directores/directories/

Quoted 29 lines

multicolor framework. It also documents usage.

---
 Documentation/leds/leds-class-multicolor.txt | 152 +++++++++++++++++++
 1 file changed, 152 insertions(+)
 create mode 100644 Documentation/leds/leds-class-multicolor.txt
Documentation/leds/leds-class-multicolor.txt new file +15 -0
new file mode 100644
index 000000000000..3b354e523660
@@ -0,0 +1,152 @@
+
+Multi Color LED handling under Linux
+=================================================
+
+Author: Dan Murphy <[email protected]>
+
+Description
+-----------
+There are varying monochrome LED colors available for application.  These
+LEDs can be used as a single use case LED or can be mixed with other color
+LEDs to produce the full spectrum of color.  Color LEDs that are grouped
+can be presented under a single LED node with individual color control.
+The multicolor class groups these LEDs and allows dynamically setting the value
+of a single LED or setting the intensity values of the LEDs in the group and
+updating the LEDs virtually simultaneously.

Documentation/leds/leds-class-multicolor.txt line 25

Quoted 10 lines
Documentation/leds/leds-class-multicolor.txt new file +10 -0
+
+Multicolor Class Control
+-------------------------
+The multicolor class presents the LED groups under a directory called "colors".
+This directory is a child under the LED parent node created by the led_class
+framework.  The led_class framework is documented in led-class.txt within this
+documentation directory.
+
+Each colored LED is given its own directory.  These directories can be, but not
+limited to red, green, blue, white, amber, yellow and violet.  Under these

Similarly as in 1/9 please refer to the document with available LED colors and mention that required color should be added if it is missing.

Quoted 52 lines
Documentation/leds/leds-class-multicolor.txt new file +52 -0
+directories the intensity and max_intensity files are presented for each LED.
+
+
+Directory Layout Example
+------------------------
+root:/sys/class/leds/rgb:grouped_leds# ls -lR colors/
+colors/:
+drwxr-xr-x    2 root     root             0 Jun 28 20:21 blue
+drwxr-xr-x    2 root     root             0 Jun 28 20:21 green
+drwxr-xr-x    2 root     root             0 Jun 28 20:21 red
+-rw-------    1 root     root          4096 Jun 28 20:21 color_mix
+
+colors/blue:
+-rw-------    1 root     root          4096 Jun 28 20:21 intensity
+-r--------    1 root     root          4096 Jun 28 20:27 max_intensity
+-r--------    1 root     root          4096 Jun 28 20:21 color_id
+
+colors/green:
+-rw-------    1 root     root          4096 Jun 28 20:22 intensity
+-r--------    1 root     root          4096 Jun 28 20:27 max_intensity
+-r--------    1 root     root          4096 Jun 28 20:21 color_id
+
+colors/red:
+-rw-------    1 root     root          4096 Jun 28 20:21 intensity
+-r--------    1 root     root          4096 Jun 28 20:27 max_intensity
+-r--------    1 root     root          4096 Jun 28 20:21 color_id
+
+Multicolor Color Mixing
+-----------------------
+Multicolor monochrome LEDs intensity can be modified and mixed to produce a
+varying array of colors.  The color_mix file gives the user the ability to write
+all the monochrome LEDs registered in the directory with a single call.
+To create a specific color from monochrome LEDs the color_mix file needs to be
+written with each color's intensity.  The order in which the monochrome LEDs
+should be written is based on the colors color_id.
+
+For example:
+cat /sys/class/leds/rgb:grouped_leds/red/color_id
+0
+cat /sys/class/leds/rgb:grouped_leds/green/color_id
+1
+cat /sys/class/leds/rgb:grouped_leds/blue/color_id
+2
+
+red - color_id = 0
+green - color_id = 1
+blue - color_id = 2
+
+These id's are the order in which to write the color_mix file.
+
+echo "<red> <green> <blue>" > color_mix
+

Documentation/leds/leds-class-multicolor.txt line 87

Quoted 10 lines
Documentation/leds/leds-class-multicolor.txt new file +10 -0
+echo "80 00 80" > color_mix
+
+The order of the monochrome LEDs are determined during multicolor class
+registration and will not change unless unregistered and re-registered.
+
+Other example with amber monochrome LED:
+blue - color_id = 0
+amber - color_id = 1
+
+In this exampe blue is at ID 0 and amber ID is 1 so the user would write

s/exampe/example/

Quoted 34 lines
Documentation/leds/leds-class-multicolor.txt new file +34 -0
+echo "<blue> <amber>" > color_mix
+
+echo "38 80" > color_mix
+
+If a single monochrome LED needs to be modified then the user would write the
+colors/<color>/intensity file.
+
+
+Multicolor Class Brightness Control
+-----------------------------------
+The multiclor class framework will calculate each monochrome LEDs intensity.
+
+The brightness level for each LED is calculated based on the color LED
+intensity setting divided by the color LED max intensity setting multiplied by
+the requested value.
+
+led_brightness = requested_value * led_color_intensity/led_color_max_intensity
+
+Example:
+Three LEDs are present in the group as defined in "Directory Layout Example"
+within this document.
+
+A user first writes the color LED brightness file with the brightness level that
+is necessary to achieve a blueish violet output from the RGB LED group.
+
+echo 138 > /sys/class/leds/rgb:grouped_leds/red/intensity
+echo 43 > /sys/class/leds/rgb:grouped_leds/green/intensity
+echo 226 > /sys/class/leds/rgb:grouped_leds/blue/intensity
+
+red -
+	intensity = 138
+	max_intensity = 255
+green -
+	intensity = 43

Documentation/leds/leds-class-multicolor.txt line 131

Quoted 10 lines
Documentation/leds/leds-class-multicolor.txt new file +10 -0
+	max_intensity = 255
+blue -
+	intensity = 226
+	max_intensity = 255
+
+The user can control the brightness of that RGB group by writing the parent
+'brightness' control.  Assuming a parent max_brightness of 255 the user may want
+to dim the LED color group to half.  The user would write a value of 128 to the
+parent brightness file then the values written to each LED will be adjusted
+base on this value

s/base/based/

Quoted 8 lines
Documentation/leds/leds-class-multicolor.txt new file +8 -0
+
+cat /sys/class/leds/rgb:grouped_leds/max_brightness
+255
+echo 128 > /sys/class/leds/rgb:grouped_leds/brightness
+
+adjusted_red_value = 128 * 138/255 = 69
+adjusted_green_value = 128 * 43/255 = 21
+adjusted_blue_value = 128 * 226/255 = 113

Documentation/leds/leds-class-multicolor.txt line 149

Quoted 10 lines
Documentation/leds/leds-class-multicolor.txt new file +10 -0
+
+Reading the parent brightness file will return the current brightness value of
+the color LED group.
+
+cat /sys/class/leds/rgb:grouped_leds/max_brightness
+255
+
+echo 128 > /sys/class/leds/rgb:grouped_leds/brightness
+
+cat /sys/class/leds/rgb:grouped_leds/max_brightness

s/max_brightness/brightness/

But I'd skip the three above examples - they are trivial and this part of the interface works identically as the current LED class interface.

Quoted 4 lines
Documentation/leds/leds-class-multicolor.txt new file +3 -0
+128
+
+
Signature · 2 lines
Best regards,
Jacek Anaszewski
#7 lore raw

Re: [PATCH v4 3/9] dt: bindings: Add multicolor class dt bindings documention

In reply to Dan Murphy

Dan,

Quoted 14 lines · Dan Murphy

Add DT bindings for the LEDs multicolor class framework.

---
 .../bindings/leds/leds-class-multicolor.txt   | 96 +++++++++++++++++++
 1 file changed, 96 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-class-multicolor.txt
Documentation/devicetree/bindings/leds/leds-class-multicolor.txt new file +0 -0
new file mode 100644
index 000000000000..5d36327b18fc

Documentation/devicetree/bindings/leds/leds-class-multicolor.txt line 3

Quoted 3 lines
Documentation/devicetree/bindings/leds/leds-class-multicolor.txt new file +3 -0
@@ -0,0 +1,96 @@
+* Multicolor LED properties
+
+Multicolor LEDs can consist of a RGB, RGBW or a RGBA LED clusters.  These devices

There are also RGBAUV LEDs. And maybe some other, I'd not strive to mention all multicolor LED combinations available on the market.

Documentation/devicetree/bindings/leds/leds-class-multicolor.txt line 5

Quoted 2 lines
Documentation/devicetree/bindings/leds/leds-class-multicolor.txt new file +2 -0
+can be grouped together and also provide a modeling mechanism so that the
+cluster LEDs can vary in hue and intensity to produce a wide range of colors.

It looks as if RGBW, RGB and RGBA LEDs could be grouped together with this LED multicolor class, which is not what it does. We need to rephrase that rather to underline the possibility of grouping all the LED colors of e.g. RGBA under single multi color LED child node.

Quoted 12 lines
Documentation/devicetree/bindings/leds/leds-class-multicolor.txt new file +12 -0
+
+The nodes and properties defined in this document are unique to the multicolor
+LED class.  Common LED nodes and properties are inherited from the common.txt
+within this documentation directory.
+
+Required LED Child properties:
+	- color : For multicolor LED support this property should be defined as
+		  LED_COLOR_ID_MULTI and further definition can be found in
+		  include/linux/leds/common.h.
+
+led-controller@30 {
+	#address-cells = <1>;

Documentation/devicetree/bindings/leds/leds-class-multicolor.txt line 27

Quoted 10 lines
Documentation/devicetree/bindings/leds/leds-class-multicolor.txt new file +10 -0
+	#size-cells = <0>;
+	compatible = "ti,lp5024";
+	reg = <0x29>;
+
+	multi-led@1 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <1>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_;

suffix is missing here

Quoted 70 lines
Documentation/devicetree/bindings/leds/leds-class-multicolor.txt new file +69 -0
+
+
+		led@3 {
+			reg = <3>;
+			color = <LED_COLOR_ID_RED>;
+		};
+
+		led@4 {
+			reg = <4>;
+			color = <LED_COLOR_ID_GREEN>;
+		};
+
+		led@5 {
+			reg = <5>;
+			color = <LED_COLOR_ID_BLUE>;
+		};
+	};
+
+	multi-led@2 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_ACTIVITY;
+		reg = <2>;
+		ti,led-bank = <2 3 5>;
+
+		led@6 {
+			reg = <0x6>;
+			color = <LED_COLOR_ID_RED>;
+			led-sources = <6 9 15>;
+		};
+
+		led@7 {
+			reg = <0x7>;
+			color = <LED_COLOR_ID_GREEN>;
+			led-sources = <7 10 16>;
+		};
+
+		led@8 {
+			reg = <0x8>;
+			color = <LED_COLOR_ID_BLUE>;
+			led-sources = <8 11 17>;
+		};
+	};
+
+	multi-led@4 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <4>;
+		color = <LED_COLOR_ID_MULTI>;
+		function = LED_FUNCTION_ACTIVITY;
+
+		led@12 {
+			reg = <12>;
+			color = <LED_COLOR_ID_RED>;
+		};
+
+		led@13 {
+			reg = <13>;
+			color = <LED_COLOR_ID_GREEN>;
+		};
+
+		led@14 {
+			reg = <14>;
+			color = <LED_COLOR_ID_BLUE>;
+		};
+	};
+
+};
Signature · 2 lines
Best regards,
Jacek Anaszewski
#11 lore raw

Re: [PATCH v4 6/9] leds: multicolor: Introduce a multicolor class definition

In reply to Dan Murphy

Dan,

Quoted 22 lines · Dan Murphy

Introduce a multicolor class that groups colored LEDs within a LED node.

The framework allows for dynamically setting individual LEDs or setting brightness levels of LEDs and updating them virtually simultaneously.

---
 drivers/leds/Kconfig                 |  10 +
 drivers/leds/Makefile                |   1 +
 drivers/leds/led-class-multicolor.c  | 402 +++++++++++++++++++++++++++
 include/linux/led-class-multicolor.h |  96 +++++++
 4 files changed, 509 insertions(+)
 create mode 100644 drivers/leds/led-class-multicolor.c
 create mode 100644 include/linux/led-class-multicolor.h
drivers/leds/Kconfig +0 -0
index f7a3dd7ecf3d..7f780d5b8490 100644

drivers/leds/Kconfig config LEDS_CLASS_FLASH · line 38

Quoted 9 lines
drivers/leds/Kconfig +6 -0
@@ -30,6 +30,16 @@ config LEDS_CLASS_FLASH
 	  for the flash related features of a LED device. It can be built
 	  as a module.
 
+config LEDS_CLASS_MULTI_COLOR
+	tristate "LED Mulit Color LED Class Support"
+	depends on LEDS_CLASS
+	help
+	  This option enables the multicolor LED sysfs class in /sys/class/leds.
+	  It wraps LED Class and adds multicolor LED specific sysfs attributes

s/Class/class/

I'll need to fix it in LED flash class entry too.

Quoted 43 lines
drivers/leds/Kconfig +4 -0
+	  and kernel internal API to it. You'll need this to provide support
+	  for multicolor LEDs that are grouped together. This class is not
+	  intended for single color LEDs. It can be built as a module.
+
 config LEDS_BRIGHTNESS_HW_CHANGED
 	bool "LED Class brightness_hw_changed attribute support"
 	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index 41fb073a39c1..897b810257dd 100644
@@ -4,6 +4,7 @@
 obj-$(CONFIG_NEW_LEDS)			+= led-core.o
 obj-$(CONFIG_LEDS_CLASS)		+= led-class.o
 obj-$(CONFIG_LEDS_CLASS_FLASH)		+= led-class-flash.o
+obj-$(CONFIG_LEDS_CLASS_MULTI_COLOR)	+= led-class-multicolor.o
 obj-$(CONFIG_LEDS_TRIGGERS)		+= led-triggers.o
 
 # LED Platform Drivers
drivers/leds/led-class-multicolor.c new file +18 -0
new file mode 100644
index 000000000000..123443e6d3eb
@@ -0,0 +1,402 @@
+// SPDX-License-Identifier: GPL-2.0
+// LED Multi Color class interface
+// Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
+
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/led-class-multicolor.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+
+#include "leds.h"
+
+struct led_classdev_mc_priv {
+	struct led_classdev_mc *mcled_cdev;
+
+	struct device_attribute max_intensity_attr;
+	struct device_attribute intensity_attr;

drivers/leds/led-class-multicolor.c line 28

Quoted 10 lines
drivers/leds/led-class-multicolor.c new file +10 -0
+	struct device_attribute color_index_attr;
+
+	enum led_brightness max_intensity;
+	enum led_brightness intensity;
+	enum led_brightness new_intensity;
+
+	struct list_head list;
+
+	int color_id;
+	int color_index;

We need to differentiate both more meaningfully.
Maybe led_color_id and cluster_color_id?

drivers/leds/led-class-multicolor.c line 30

Quoted 2 lines
drivers/leds/led-class-multicolor.c new file +2 -0
+};
+static int calculate_brightness(int brightness, int intensity, int max_intensity)

How about led_mc_calculate_brightness ?

drivers/leds/led-class-multicolor.c line 39

Quoted 9 lines
drivers/leds/led-class-multicolor.c new file +9 -0
+{
+	if (brightness && intensity && max_intensity)
+		return brightness * intensity / max_intensity;
+
+	return LED_OFF;
+}
+
+void set_cluster_brightness(struct led_classdev_mc *mcled_cdev,
+			    enum led_brightness brightness, int adj_value[])

s/set_cluster/led_mc_set_cluster/

And I think adj_value is not telling too much. How about:

s/adj_value/color_intensity/

drivers/leds/led-class-multicolor.c line 51

Quoted 12 lines
drivers/leds/led-class-multicolor.c new file +12 -0
+{
+	struct led_classdev_mc_data *data = mcled_cdev->data;
+	struct led_classdev_mc_priv *priv;
+	int i = 0;
+
+	list_for_each_entry(priv, &data->color_list, list) {
+		adj_value[i] = calculate_brightness(brightness, priv->intensity,
+						    priv->max_intensity);
+		i++;
+	}
+
+	data->cluster_brightness = brightness;

This property duplicates brightness stored in struct led_classdev and thus it is redundant.

drivers/leds/led-class-multicolor.c line 55

Quoted 4 lines
drivers/leds/led-class-multicolor.c new file +4 -0
+}
+
+void get_cluster_brightness(struct led_classdev_mc *mcled_cdev,
+				  int brightness_val[])

s/get_cluster/led_mc_get_cluster/

s/brightness_val/color_intensity/

Quoted 37 lines
drivers/leds/led-class-multicolor.c new file +37 -0
+{
+	struct led_classdev_mc_data *data = mcled_cdev->data;
+	struct led_classdev_mc_priv *priv;
+	int i = 0;
+
+	list_for_each_entry(priv, &data->color_list, list) {
+		brightness_val[i] = priv->intensity;
+		i++;
+	}
+}
+
+static ssize_t color_index_show(struct device *dev,
+				   struct device_attribute *color_index_attr,
+				   char *buf)
+{
+	struct led_classdev_mc_priv *priv = container_of(color_index_attr,
+						    struct led_classdev_mc_priv,
+						      color_index_attr);
+
+	return sprintf(buf, "%d\n", priv->color_id);
+}
+
+static ssize_t color_mix_store(struct device *dev,
+				struct device_attribute *color_mix_attr,
+				const char *buf, size_t size)
+{
+	struct led_classdev_mc_data *data = container_of(color_mix_attr,
+						    struct led_classdev_mc_data,
+						      color_mix_attr);
+	struct led_classdev_mc *mcled_cdev = data->mcled_cdev;
+	const struct led_multicolor_ops *ops = mcled_cdev->ops;
+	struct led_classdev_mc_priv *priv;
+	unsigned int value[LED_COLOR_ID_MAX];
+	int adj_brightness;
+	int nrchars, offset = 0;
+	unsigned int i;
+	int ret;

drivers/leds/led-class-multicolor.c line 102

Quoted 10 lines
drivers/leds/led-class-multicolor.c new file +10 -0
+
+	for (i = 0; i < mcled_cdev->num_leds; i++) {
+		ret = sscanf(buf + offset, "%i%n", &value[i], &nrchars);
+		if (ret != 1)
+			break;
+
+		offset += nrchars;
+	}
+
+	if (i != mcled_cdev->num_leds) {

Shouldn't we return error if i != mcled_cdev->num_leds - 1 ? How can we know which color the value will be for if there is less values passed than the total number of colors in the cluster?

drivers/leds/led-class-multicolor.c line 104

Quoted 2 lines
drivers/leds/led-class-multicolor.c new file +2 -0
+		for (; i < LED_COLOR_ID_MAX; i++)
+			value[i] = 0;

What use case is it for?

Quoted 6 lines
drivers/leds/led-class-multicolor.c new file +6 -0
+	}
+
+	list_for_each_entry(priv, &data->color_list, list) {
+		if (data->cluster_brightness) {
+			adj_brightness = calculate_brightness(data->cluster_brightness,
+							      value[priv->color_index],

drivers/leds/led-class-multicolor.c line 120

Quoted 10 lines
drivers/leds/led-class-multicolor.c new file +10 -0
+							     priv->max_intensity);
+			ret = ops->set_color_brightness(priv->mcled_cdev,
+							priv->color_id,
+							adj_brightness);
+			if (ret < 0)
+				goto done;
+		}
+
+		priv->intensity = value[priv->color_index];
+	}

Here we could use just brightness_set op as a single call. We should always write all colors as a result of write to color_mix anyway.

Quoted 19 lines
drivers/leds/led-class-multicolor.c new file +19 -0
+
+done:
+	return size;
+}
+
+static ssize_t intensity_store(struct device *dev,
+				struct device_attribute *intensity_attr,
+				const char *buf, size_t size)
+{
+	struct led_classdev_mc_priv *priv = container_of(intensity_attr,
+						    struct led_classdev_mc_priv,
+						      intensity_attr);
+	struct led_classdev_mc_data *data = priv->mcled_cdev->data;
+	struct led_classdev *led_cdev = priv->mcled_cdev->led_cdev;
+	struct led_multicolor_ops *ops = priv->mcled_cdev->ops;
+	unsigned int adj_value;
+	unsigned long value;
+	ssize_t ret;
+

drivers/leds/led-class-multicolor.c line 149

Quoted 10 lines
drivers/leds/led-class-multicolor.c new file +10 -0
+	mutex_lock(&led_cdev->led_access);
+
+	ret = kstrtoul(buf, 10, &value);
+	if (ret)
+		goto unlock;
+
+	if (value > priv->max_intensity) {
+		ret = -EINVAL;
+		goto unlock;
+	}

We don't fail in this case in LED class, so let's proceed in the same way here:

priv->intensity = min(value, priv->max_brightness);

drivers/leds/led-class-multicolor.c line 160

Quoted 11 lines
drivers/leds/led-class-multicolor.c new file +11 -0
+
+	priv->new_intensity = value;
+
+	if (data->cluster_brightness) {
+		adj_value = calculate_brightness(data->cluster_brightness,
+					priv->new_intensity,
+					priv->max_intensity);
+		ret = ops->set_color_brightness(priv->mcled_cdev,
+						priv->color_id, adj_value);
+		if (ret < 0) {
+			priv->new_intensity = priv->intensity;

This is unnecessary complication. Just write the calculated iout intensity.

We need to highlight it in the documentation that exact requested color intensity values are written to the hardware only when brightness == max_brightness.

Quoted 51 lines
drivers/leds/led-class-multicolor.c new file +51 -0
+			goto unlock;
+		}
+	}
+
+	priv->intensity = priv->new_intensity;
+	ret = size;
+
+unlock:
+	mutex_unlock(&led_cdev->led_access);
+	return ret;
+}
+
+static ssize_t intensity_show(struct device *dev,
+			      struct device_attribute *intensity_attr,
+			      char *buf)
+{
+	struct led_classdev_mc_priv *priv = container_of(intensity_attr,
+						    struct led_classdev_mc_priv,
+						      intensity_attr);
+	const struct led_multicolor_ops *ops = priv->mcled_cdev->ops;
+	int value = 0;
+
+	if (ops->get_color_brightness) {
+		value = ops->get_color_brightness(priv->mcled_cdev,
+						  priv->color_id);
+		priv->intensity = value;
+	} else {
+		value = priv->intensity;
+	}
+
+	return sprintf(buf, "%d\n", value);
+}
+
+static ssize_t max_intensity_show(struct device *dev,
+				   struct device_attribute *max_intensity_attr,
+				   char *buf)
+{
+	struct led_classdev_mc_priv *priv = container_of(max_intensity_attr,
+						    struct led_classdev_mc_priv,
+						      max_intensity_attr);
+
+	return sprintf(buf, "%d\n", priv->max_intensity);
+}
+
+static int led_multicolor_init_color(struct led_classdev_mc_data *data,
+				     struct led_classdev_mc *mcled_cdev,
+				     int color_id, int color_index)
+{
+	struct led_classdev *led_cdev = mcled_cdev->led_cdev;
+	struct led_classdev_mc_priv *mc_priv;
+	int ret;

drivers/leds/led-class-multicolor.c line 221

Quoted 10 lines
drivers/leds/led-class-multicolor.c new file +10 -0
+
+	mc_priv = devm_kzalloc(led_cdev->dev, sizeof(*mc_priv), GFP_KERNEL);
+	if (!mc_priv)
+		return -ENOMEM;
+
+	mc_priv->color_id = color_id;
+	mc_priv->mcled_cdev = mcled_cdev;
+
+	data->led_kobj = kobject_create_and_add(led_colors[color_id],
+						data->color_kobj);

You should not use kobject API in the drivers. See [0].

Quoted 274 lines
drivers/leds/led-class-multicolor.c new file +181 -0
+	if (!data->led_kobj)
+		return -EINVAL;
+
+	sysfs_attr_init(&mc_priv->intensity_attr.attr);
+	mc_priv->intensity_attr.attr.name = "intensity";
+	mc_priv->intensity_attr.attr.mode = S_IRUGO | S_IWUGO;
+	mc_priv->intensity_attr.store = intensity_store;
+	mc_priv->intensity_attr.show = intensity_show;
+	ret = sysfs_create_file(data->led_kobj,
+				&mc_priv->intensity_attr.attr);
+	if (ret)
+		goto err_out;
+
+	sysfs_attr_init(&mc_priv->max_intensity_attr.attr);
+	mc_priv->max_intensity_attr.attr.name = "max_intensity";
+	mc_priv->max_intensity_attr.attr.mode = S_IRUGO;
+	mc_priv->max_intensity_attr.show = max_intensity_show;
+	ret = sysfs_create_file(data->led_kobj,
+				&mc_priv->max_intensity_attr.attr);
+	if (ret)
+		goto err_out;
+
+	sysfs_attr_init(&mc_priv->color_index_attr.attr);
+	mc_priv->color_index_attr.attr.name = "color_id";
+	mc_priv->color_index_attr.attr.mode = S_IRUGO;
+	mc_priv->color_index_attr.show = color_index_show;
+	ret = sysfs_create_file(data->led_kobj,
+				&mc_priv->color_index_attr.attr);
+	if (ret)
+		goto err_out;
+
+	mc_priv->max_intensity = LED_FULL;
+	mc_priv->color_index = color_index;
+	list_add_tail(&mc_priv->list, &data->color_list);
+
+err_out:
+	return ret;
+}
+
+static int led_multicolor_init_color_dir(struct led_classdev_mc_data *data,
+					 struct led_classdev_mc *mcled_cdev)
+{
+	struct led_classdev *led_cdev = mcled_cdev->led_cdev;
+	u32 color_id;
+	int ret;
+	int i, j = 0;
+
+	data->color_kobj = kobject_create_and_add("colors",
+						  &led_cdev->dev->kobj);
+	if (!data->color_kobj)
+		return -EINVAL;
+
+	data->mcled_cdev = mcled_cdev;
+
+	for (i = 0; i < LED_COLOR_ID_MAX; i++) {
+		color_id = (mcled_cdev->available_colors & (1 << i));
+		if (color_id) {
+			ret = led_multicolor_init_color(data, mcled_cdev, i, j);
+			if (ret)
+				break;
+
+			j++;
+		}
+	}
+
+	sysfs_attr_init(&data->color_mix_attr.attr);
+	data->color_mix_attr.attr.name = "color_mix";
+	data->color_mix_attr.attr.mode = S_IRUGO | S_IWUGO;
+	data->color_mix_attr.store = color_mix_store;
+	ret = sysfs_create_file(data->color_kobj,
+				&data->color_mix_attr.attr);
+
+	return ret;
+}
+
+int led_classdev_multicolor_register_ext(struct device *parent,
+				     struct led_classdev_mc *mcled_cdev,
+				     struct led_init_data *init_data)
+{
+	struct led_classdev *led_cdev;
+	struct led_multicolor_ops *ops;
+	struct led_classdev_mc_data *data;
+	int ret;
+
+	if (!mcled_cdev)
+		return -EINVAL;
+
+	ops = mcled_cdev->ops;
+	if (!ops || !ops->set_color_brightness)
+		return -EINVAL;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	mcled_cdev->data = data;
+	led_cdev = mcled_cdev->led_cdev;
+	INIT_LIST_HEAD(&data->color_list);
+
+	/* Register led class device */
+	ret = led_classdev_register_ext(parent, led_cdev, init_data);
+	if (ret)
+		return ret;
+
+	return led_multicolor_init_color_dir(data, mcled_cdev);
+}
+EXPORT_SYMBOL_GPL(led_classdev_multicolor_register_ext);
+
+void led_classdev_multicolor_unregister(struct led_classdev_mc *mcled_cdev)
+{
+	if (!mcled_cdev)
+		return;
+
+	led_classdev_unregister(mcled_cdev->led_cdev);
+}
+EXPORT_SYMBOL_GPL(led_classdev_multicolor_unregister);
+
+static void devm_led_classdev_multicolor_release(struct device *dev, void *res)
+{
+	led_classdev_multicolor_unregister(*(struct led_classdev_mc **)res);
+}
+
+/**
+ * devm_of_led_classdev_register - resource managed led_classdev_register()
+ *
+ * @parent: parent of LED device
+ * @led_cdev: the led_classdev structure for this device.
+ */
+int devm_led_classdev_multicolor_register(struct device *parent,
+					  struct led_classdev_mc *mcled_cdev)
+{
+	struct led_classdev_mc **dr;
+	int ret;
+
+	dr = devres_alloc(devm_led_classdev_multicolor_release,
+			  sizeof(*dr), GFP_KERNEL);
+	if (!dr)
+		return -ENOMEM;
+
+	ret = led_classdev_multicolor_register(parent, mcled_cdev);
+	if (ret) {
+		devres_free(dr);
+		return ret;
+	}
+
+	*dr = mcled_cdev;
+	devres_add(parent, dr);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_led_classdev_multicolor_register);
+
+static int devm_led_classdev_multicolor_match(struct device *dev,
+					      void *res, void *data)
+{
+	struct mcled_cdev **p = res;
+
+	if (WARN_ON(!p || !*p))
+		return 0;
+
+	return *p == data;
+}
+
+/**
+ * devm_led_classdev_multicolor_unregister() - resource managed
+ *					led_classdev_multicolor_unregister()
+ * @parent: The device to unregister.
+ * @mcled_cdev: the led_classdev_mc structure for this device.
+ */
+void devm_led_classdev_multicolor_unregister(struct device *dev,
+				  struct led_classdev_mc *mcled_cdev)
+{
+	WARN_ON(devres_release(dev,
+			       devm_led_classdev_multicolor_release,
+			       devm_led_classdev_multicolor_match, mcled_cdev));
+}
+EXPORT_SYMBOL_GPL(devm_led_classdev_multicolor_unregister);
+
+MODULE_AUTHOR("Dan Murphy <[email protected]>");
+MODULE_DESCRIPTION("Multi Color LED class interface");
+MODULE_LICENSE("GPL v2");
include/linux/led-class-multicolor.h new file +86 -0
new file mode 100644
index 000000000000..082883606246
@@ -0,0 +1,96 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* LED Multicolor class interface
+ * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
+ */
+
+#ifndef __LINUX_MULTICOLOR_LEDS_H_INCLUDED
+#define __LINUX_MULTICOLOR_LEDS_H_INCLUDED
+
+#include <linux/leds.h>
+#include <dt-bindings/leds/common.h>
+
+struct led_classdev_mc;
+
+struct led_classdev_mc_data {
+	struct led_classdev_mc *mcled_cdev;
+
+	struct kobject *color_kobj;
+	struct kobject *led_kobj;
+
+	struct device_attribute color_mix_attr;
+
+	struct list_head color_list;
+
+	enum led_brightness cluster_brightness;
+};
+
+struct led_multicolor_ops {
+	/* Set brightness for a specific color id */
+	int (*set_color_brightness)(struct led_classdev_mc *mcled_cdev,
+				    int color_id, int value);
+	/* Read current color setting */
+	int (*get_color_brightness)(struct led_classdev_mc *mcled_cdev,
+				    int color_id);
+};
+
+struct led_classdev_mc {
+	/* led class device */
+	struct led_classdev *led_cdev;
+
+	/* multicolor led specific ops */
+	struct led_multicolor_ops *ops;
+
+	struct led_classdev_mc_data *data;
+
+	u32 available_colors;
+	int num_leds;
+};
+
+static inline struct led_classdev_mc *lcdev_to_mccdev(
+						struct led_classdev *lcdev)
+{
+	return container_of(lcdev, struct led_classdev_mc, led_cdev);
+}
+
+/**
+ * led_classdev_multicolor_register_ext - register a new object of led_classdev
+ *				      class with support for multicolor LEDs
+ * @parent: the multicolor LED to register
+ * @mcled_cdev: the led_classdev_mc structure for this device
+ * @init_data: the LED class Multi color device initialization data
+ *
+ * Returns: 0 on success or negative error value on failure
+ */
+extern int led_classdev_multicolor_register_ext(struct device *parent,
+					    struct led_classdev_mc *mcled_cdev,
+					    struct led_init_data *init_data);
+
+#define led_classdev_multicolor_register(parent, mcled_cdev)		\
+	led_classdev_multicolor_register_ext(parent, mcled_cdev, NULL)
+
+/**
+ * led_classdev_multicolor_unregister - unregisters an object of led_classdev
+ *					class with support for multicolor LEDs
+ * @mcled_cdev: the multicolor LED to unregister
+ *
+ * Unregister a previously registered via led_classdev_multicolor_register
+ * object
+ */
+extern void led_classdev_multicolor_unregister(struct led_classdev_mc *mcled_cdev);
+
+extern int devm_led_classdev_multicolor_register(struct device *parent,
+					struct led_classdev_mc *mcled_cdev);
+
+extern void devm_led_classdev_multicolor_unregister(struct device *parent,
+					   struct led_classdev_mc *mcled_cdev);
+

include/linux/led-class-multicolor.h line 96

Quoted 10 lines
include/linux/led-class-multicolor.h new file +10 -0
+/* Set brightness for the monochrome LED cluster */
+extern void set_cluster_brightness(struct led_classdev_mc *mcled_cdev,
+			    enum led_brightness brightness,
+			    int adj_value[]);
+
+/* Read current cluster brightness */
+extern void get_cluster_brightness(struct led_classdev_mc *mcled_cdev,
+				   int brightness_val[]);
+
+#endif	/* __LINUX_MULTICOLOR_LEDS_H_INCLUDED */

I'll review remaining patches (drivers) next time.

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

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

Re: [PATCH v4 8/9] leds: lp50xx: Add the LP50XX family of the RGB LED driver

In reply to Dan Murphy

Hi Dan,

Thank you for the patch I see few things to improve.

Quoted 54 lines · Dan Murphy

Introduce the LP5036/30/24/18 RGB LED driver.
The difference in these parts are the number of
LED outputs where the:

LP5036 can control 36 LEDs
LP5030 can control 30 LEDs
LP5024 can control 24 LEDs
LP5018 can control 18 LEDs

The device has the ability to group LED output into control banks so that multiple LED banks can be controlled with the same mixing and brightness. Inversely the LEDs can also be controlled independently.

---
 drivers/leds/Kconfig       |   7 +
 drivers/leds/Makefile      |   1 +
 drivers/leds/leds-lp50xx.c | 789 +++++++++++++++++++++++++++++++++++++
 3 files changed, 797 insertions(+)
 create mode 100644 drivers/leds/leds-lp50xx.c
drivers/leds/Kconfig +7 -0
index 7f780d5b8490..69c037020f6b 100644
@@ -350,6 +350,13 @@ config LEDS_LP3952
 	  To compile this driver as a module, choose M here: the
 	  module will be called leds-lp3952.
 
+config LEDS_LP50XX
+	tristate "LED Support for TI LP5036/30/24/18 LED driver chip"
+	depends on LEDS_CLASS && REGMAP_I2C
+	help
+	  If you say yes here you get support for the Texas Instruments
+	  LP5036, LP5030, LP5024 and LP5018 LED driver.
+
 config LEDS_LP55XX_COMMON
 	tristate "Common Driver for TI/National LP5521/5523/55231/5562/8501"
 	depends on LEDS_LP5521 || LEDS_LP5523 || LEDS_LP5562 || LEDS_LP8501
drivers/leds/Makefile +1 -0
index 897b810257dd..438a5499f3ee 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_LEDS_GPIO_REGISTER)	+= leds-gpio-register.o
 obj-$(CONFIG_LEDS_GPIO)			+= leds-gpio.o
 obj-$(CONFIG_LEDS_LP3944)		+= leds-lp3944.o
 obj-$(CONFIG_LEDS_LP3952)		+= leds-lp3952.o
+obj-$(CONFIG_LEDS_LP50XX)		+= leds-lp50xx.o
 obj-$(CONFIG_LEDS_LP55XX_COMMON)	+= leds-lp55xx-common.o
 obj-$(CONFIG_LEDS_LP5521)		+= leds-lp5521.o
 obj-$(CONFIG_LEDS_LP5523)		+= leds-lp5523.o
drivers/leds/leds-lp50xx.c new file +0 -0
new file mode 100644
index 000000000000..5a32410e4f34

[...]

Quoted 28 lines
drivers/leds/leds-lp50xx.c new file +28 -0
+static int lp50xx_probe_dt(struct lp50xx *priv)
+{
+	u32 led_strings[LP5036_MAX_LED_MODULES];
+	struct fwnode_handle *child = NULL;
+	struct fwnode_handle *led_node = NULL;
+	struct led_init_data init_data;
+	struct lp50xx_led *led;
+	int num_colors;
+	u32 color_id;
+	int led_number;
+	size_t i = 0;
+	int ret;
+
+	priv->enable_gpio = devm_gpiod_get_optional(&priv->client->dev,
+						   "enable", GPIOD_OUT_LOW);
+	if (IS_ERR(priv->enable_gpio)) {
+		ret = PTR_ERR(priv->enable_gpio);
+		dev_err(&priv->client->dev, "Failed to get enable gpio: %d\n",
+			ret);
+		return ret;
+	}
+
+	priv->regulator = devm_regulator_get(&priv->client->dev, "vled");
+	if (IS_ERR(priv->regulator))
+		priv->regulator = NULL;
+
+	if (priv->model_id == LP5009)
+		priv->max_leds = LP5009_MAX_LED_MODULES;

drivers/leds/leds-lp50xx.c line 587

Quoted 10 lines
drivers/leds/leds-lp50xx.c new file +10 -0
+	else if (priv->model_id == LP5012)
+		priv->max_leds = LP5012_MAX_LED_MODULES;
+	else if (priv->model_id == LP5018)
+		priv->max_leds = LP5018_MAX_LED_MODULES;
+	else if (priv->model_id == LP5024)
+		priv->max_leds = LP5024_MAX_LED_MODULES;
+	else if (priv->model_id == LP5030)
+		priv->max_leds = LP5030_MAX_LED_MODULES;
+	else
+		priv->max_leds = LP5036_MAX_LED_MODULES;

You could simplify that by initializing data property of lp50xx_leds_match array elements and then call of_match_device. You can compare e.g. drivers/leds/leds-pca9532.c

Quoted 35 lines
drivers/leds/leds-lp50xx.c new file +35 -0
+	device_for_each_child_node(&priv->client->dev, child) {
+		led = &priv->leds[i];
+		if (fwnode_property_present(child, "ti,led-bank")) {
+			ret = fwnode_property_read_u32_array(child,
+							     "ti,led-bank",
+							     NULL, 0);
+			ret = fwnode_property_read_u32_array(child,
+							     "ti,led-bank",
+							     led_strings,
+							     ret);
+
+			priv->num_of_banked_leds = ARRAY_SIZE(led_strings);
+
+			ret = lp50xx_set_banks(priv, led_strings);
+			if (ret) {
+				dev_err(&priv->client->dev,
+					"Cannot setup banked LEDs\n");
+				fwnode_handle_put(child);
+				goto child_out;
+			}
+			led->ctrl_bank_enabled = 1;
+
+		} else {
+			ret = fwnode_property_read_u32(child, "reg",
+					       &led_number);
+
+			led->led_number = led_number;
+		}
+		if (ret) {
+			dev_err(&priv->client->dev,
+				"led sourcing property missing\n");
+			fwnode_handle_put(child);
+			goto child_out;
+		}
+

drivers/leds/leds-lp50xx.c line 633

Quoted 10 lines
drivers/leds/leds-lp50xx.c new file +10 -0
+		if (led_number > priv->max_leds) {
+			dev_err(&priv->client->dev,
+				"led-sources property is invalid\n");
+			ret = -EINVAL;
+			fwnode_handle_put(child);
+			goto child_out;
+		}
+
+		init_data.fwnode = child;
+		init_data.devicename = priv->client->name;

We'd rather not have devicename in the name of this LED class device according to the new LED naming convention. It's not a hot-pluggable device, so for properly designed DT we should not encounter name clash.

drivers/leds/leds-lp50xx.c line 634

Quoted 1 line
drivers/leds/leds-lp50xx.c new file +1 -0
+		init_data.default_label = ":";

default_label should not be set for new drivers. See the comment in the include/linux/leds.h:

/*
* string to be used for devicename section of LED class device
* either for label based LED name composition path or for fwnode
* based when devname_mandatory is true
*/

This is new driver and we should not add any convenience fallbacks for label based DT bindings. Label DT property has been marked deprecated, so for new drivers it should be deemed non-existent.

Quoted 16 lines
drivers/leds/leds-lp50xx.c new file +16 -0
+
+		fwnode_property_read_string(child, "linux,default-trigger",
+				    &led->led_dev.default_trigger);
+		num_colors = 0;
+
+		fwnode_for_each_child_node(child, led_node) {
+			ret = fwnode_property_read_u32(led_node, "color",
+						       &color_id);
+			if (ret)
+				dev_err(&priv->client->dev,
+				"Cannot read color\n");
+
+			led->mc_cdev.available_colors |= (1 << color_id);
+			num_colors++;
+
+		}

drivers/leds/leds-lp50xx.c line 660

Quoted 10 lines
drivers/leds/leds-lp50xx.c new file +10 -0
+
+		led->priv = priv;
+		led->mc_cdev.ops = &lp50xx_mc_ops;
+		led->mc_cdev.num_leds = num_colors;
+		led->mc_cdev.led_cdev = &led->led_dev;
+		led->led_dev.brightness_set_blocking = lp50xx_brightness_set;
+		led->led_dev.brightness_get = lp50xx_brightness_get;
+		ret = led_classdev_multicolor_register_ext(&priv->client->dev,
+						       &led->mc_cdev,
+						       &init_data);

Why not devm_* prefixed version?

[...]

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

Re: [PATCH v4 9/9] leds: Update the lp55xx to use the multi color framework

In reply to Dan Murphy

Hi Dan,

Thank you for the patch. My comments are below.

Quoted 62 lines · Dan Murphy

Update the lp5523 to use the multi color framework.

---
 drivers/leds/leds-lp5523.c                |  13 ++
 drivers/leds/leds-lp55xx-common.c         | 153 ++++++++++++++++++----
 drivers/leds/leds-lp55xx-common.h         |  10 ++
 include/linux/platform_data/leds-lp55xx.h |   6 +
 modules.builtin.modinfo                   | Bin 0 -> 43550 bytes
 5 files changed, 159 insertions(+), 23 deletions(-)
 create mode 100644 modules.builtin.modinfo
drivers/leds/leds-lp5523.c +13 -0
index d0b931a136b9..45380b32563f 100644
@@ -791,6 +791,18 @@ static ssize_t store_master_fader_leds(struct device *dev,
 	return ret;
 }
 
+static int lp5523_led_intensity(struct lp55xx_led *led, int chan_num)
+{
+	struct lp55xx_chip *chip = led->chip;
+	int ret;
+
+	mutex_lock(&chip->lock);
+	ret = lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + chan_num,
+		     led->brightness);
+	mutex_unlock(&chip->lock);
+	return ret;
+}
+
 static int lp5523_led_brightness(struct lp55xx_led *led)
 {
 	struct lp55xx_chip *chip = led->chip;
@@ -857,6 +869,7 @@ static struct lp55xx_device_config lp5523_cfg = {
 	.max_channel  = LP5523_MAX_LEDS,
 	.post_init_device   = lp5523_post_init_device,
 	.brightness_fn      = lp5523_led_brightness,
+	.intensity_fn       = lp5523_led_intensity,
 	.set_led_current    = lp5523_set_led_current,
 	.firmware_cb        = lp5523_firmware_loaded,
 	.run_engine         = lp5523_run_engine,
drivers/leds/leds-lp55xx-common.c +5 -0
index 44ced02b49f9..32a4ab8664ca 100644
@@ -35,6 +35,11 @@ static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
 	return cdev_to_lp55xx_led(dev_get_drvdata(dev));
 }
 
+static struct lp55xx_led *mcdev_to_lp55xx_led(struct led_classdev_mc *mc_dev)
+{
+	return container_of(mc_dev, struct lp55xx_led, mc_cdev);
+}
+
 static void lp55xx_reset_device(struct lp55xx_chip *chip)
 {
 	struct lp55xx_device_config *cfg = chip->cfg;
@@ -136,20 +141,55 @@ static int lp55xx_set_brightness(struct led_classdev *cdev,
 {
 	struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);

drivers/leds/leds-lp55xx-common.c static int lp55xx_set_brightness(struct led_classdev *cdev, · line 152

Quoted 10 lines
drivers/leds/leds-lp55xx-common.c +9 -0
 	struct lp55xx_device_config *cfg = led->chip->cfg;
+	int adj_value[LED_COLOR_ID_MAX];
+	int ret;
+	int i;
+
+	if (led->mc_cdev.num_leds > 1) {
+		set_cluster_brightness(&led->mc_cdev, brightness, adj_value);
+		for (i = 0; i < led->mc_cdev.num_leds; i++) {
+			led->brightness = adj_value[i];
+			ret = cfg->intensity_fn(led, led->grouped_channels[i]);

At first glance I would have hard time discerning between brightness_fn and intensity_fn purpose. Maybe color_intensity_fn?

drivers/leds/leds-lp55xx-common.c static int lp55xx_set_brightness(struct led_classdev *cdev, · line 157

Quoted 5 lines
drivers/leds/leds-lp55xx-common.c +5 -0
+			if (ret)
+				break;
+		}
+	} else {
+		led->brightness = (u8)brightness;

What benefit stems actually from having the copy of brightness in struct lp55xx_led, when we already have struct led_classdev there? I know that this is pre-existing, but could be optimized away while at it. We can have local u8 variable in the op setting brightness and cast enum_led_brightness to it before passing to lp55xx_write().

Quoted 47 lines
drivers/leds/leds-lp55xx-common.c +26 -3
+		ret = cfg->brightness_fn(led);
+	}
+
+	return ret;
+}
+
+static int lp55xx_set_color(struct led_classdev_mc *mcled_cdev,
+			    int color, int value)
+{
+	struct lp55xx_led *led = mcdev_to_lp55xx_led(mcled_cdev);
+	struct lp55xx_device_config *cfg = led->chip->cfg;
+	int i;
+
+	for (i = 0; i < led->mc_cdev.num_leds; i++) {
+		if (led->channel_color[i] == color)
+			break;
+	}
 
-	led->brightness = (u8)brightness;
-	return cfg->brightness_fn(led);
+	led->brightness = (u8)value;
+	return cfg->intensity_fn(led, led->grouped_channels[i]);
 }
 
+static struct led_multicolor_ops lp55xx_mc_ops = {
+	.set_color_brightness = lp55xx_set_color,
+};
+
 static int lp55xx_init_led(struct lp55xx_led *led,
 			struct lp55xx_chip *chip, int chan)
 {
 	struct lp55xx_platform_data *pdata = chip->pdata;
 	struct lp55xx_device_config *cfg = chip->cfg;
 	struct device *dev = &chip->cl->dev;
+	int max_channel = cfg->max_channel;
 	char name[32];
 	int ret;
-	int max_channel = cfg->max_channel;
 
 	if (chan >= max_channel) {
 		dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
@@ -159,10 +199,37 @@ static int lp55xx_init_led(struct lp55xx_led *led,
 	if (pdata->led_config[chan].led_current == 0)
 		return 0;
 
+	if (pdata->led_config[chan].name) {
+		led->cdev.name = pdata->led_config[chan].name;

drivers/leds/leds-lp55xx-common.c static int lp55xx_init_led(struct lp55xx_led *led, · line 213

Quoted 10 lines
drivers/leds/leds-lp55xx-common.c +10 -0
+	} else {
+		snprintf(name, sizeof(name), "%s:channel%d",
+			pdata->label ? : chip->cl->name, chan);
+		led->cdev.name = name;
+	}
+
+	if (pdata->led_config[chan].num_colors > 1) {
+		led->mc_cdev.led_cdev = &led->cdev;
+		led->cdev.brightness_set_blocking = lp55xx_set_brightness;
+		led->cdev.name = led->cdev.name;

?

Quoted 31 lines
drivers/leds/leds-lp55xx-common.c +16 -5
+		led->cdev.groups = lp55xx_led_groups;
+		led->mc_cdev.ops = &lp55xx_mc_ops;
+		led->mc_cdev.num_leds = pdata->led_config[chan].num_colors;
+		led->mc_cdev.available_colors = pdata->led_config[chan].available_colors;
+		memcpy(led->channel_color,
+		       pdata->led_config[chan].channel_color,
+		       sizeof(led->channel_color));
+		memcpy(led->grouped_channels,
+		       pdata->led_config[chan].grouped_channels,
+		       sizeof(led->grouped_channels));
+	} else {
+
+		led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
+		led->cdev.brightness_set_blocking = lp55xx_set_brightness;
+	}	led->cdev.groups = lp55xx_led_groups;
+
 	led->led_current = pdata->led_config[chan].led_current;
 	led->max_current = pdata->led_config[chan].max_current;
 	led->chan_nr = pdata->led_config[chan].chan_nr;
-	led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
 
 	if (led->chan_nr >= max_channel) {
 		dev_err(dev, "Use channel numbers between 0 and %d\n",
@@ -170,18 +237,11 @@ static int lp55xx_init_led(struct lp55xx_led *led,
 		return -EINVAL;
 	}
 
-	led->cdev.brightness_set_blocking = lp55xx_set_brightness;
-	led->cdev.groups = lp55xx_led_groups;
-
-	if (pdata->led_config[chan].name) {

drivers/leds/leds-lp55xx-common.c static int lp55xx_init_led(struct lp55xx_led *led, · line 243

Quoted 10 lines
drivers/leds/leds-lp55xx-common.c +4 -6
-		led->cdev.name = pdata->led_config[chan].name;
-	} else {
-		snprintf(name, sizeof(name), "%s:channel%d",
-			pdata->label ? : chip->cl->name, chan);
-		led->cdev.name = name;
-	}
+	if (pdata->led_config[chan].num_colors > 1)
+		ret = led_classdev_multicolor_register(dev, &led->mc_cdev);
+	else
+		ret = led_classdev_register(dev, &led->cdev);

Why not devm ?

Quoted 29 lines
drivers/leds/leds-lp55xx-common.c +13 -2
-	ret = led_classdev_register(dev, &led->cdev);
 	if (ret) {
 		dev_err(dev, "led register err: %d\n", ret);
 		return ret;
@@ -466,7 +526,6 @@ int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
 		dev_err(&chip->cl->dev, "empty brightness configuration\n");
 		return -EINVAL;
 	}
-
 	for (i = 0; i < num_channels; i++) {
 
 		/* do not initialize channels that are not connected */
@@ -538,6 +597,39 @@ void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
 }
 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
 
+static int lp5xx_parse_channel_child(struct device_node *np,
+				     struct lp55xx_led_config *cfg,
+				     int chan_num)
+{
+	struct device_node *child;
+	int num_colors = 0;
+	u32 color_id;
+	u32 led_number;
+	int ret;
+
+	cfg[chan_num].default_trigger =
+			of_get_property(np, "linux,default-trigger", NULL);
+

drivers/leds/leds-lp55xx-common.c void lp55xx_unregister_sysfs(struct lp55xx_chip *chip) · line 622

Quoted 10 lines
drivers/leds/leds-lp55xx-common.c +10 -0
+	for_each_child_of_node(np, child) {
+		of_property_read_string(child, "chan-name",
+					&cfg[chan_num].name);
+		of_property_read_u8(child, "led-cur",
+				    &cfg[chan_num].led_current);
+		of_property_read_u8(child, "max-cur",
+				    &cfg[chan_num].max_current);
+		of_property_read_u32(child, "color", &color_id);
+		cfg[chan_num].channel_color[num_colors] = color_id;
+		cfg[chan_num].available_colors |= 1 << color_id;

set_bit(color_id, &cfg[chan_num].available_colors);

Quoted 25 lines
drivers/leds/leds-lp55xx-common.c +12 -0
+		ret = of_property_read_u32(child, "reg", &led_number);
+		cfg[chan_num].grouped_channels[num_colors] = led_number;
+		num_colors++;
+	}
+
+	cfg->num_colors = num_colors;
+
+	return 0;
+}
+
 struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
 						      struct device_node *np)
 {
@@ -545,6 +637,8 @@ struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
 	struct lp55xx_platform_data *pdata;
 	struct lp55xx_led_config *cfg;
 	int num_channels;
+	int num_chan_children;
+	u32 led_number;
 	int i = 0;
 
 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
@@ -565,13 +659,26 @@ struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
 	pdata->num_channels = num_channels;
 

drivers/leds/leds-lp55xx-common.c struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev, · line 663

Quoted 10 lines
drivers/leds/leds-lp55xx-common.c +2 -7
 	for_each_child_of_node(np, child) {
-		cfg[i].chan_nr = i;
-
-		of_property_read_string(child, "chan-name", &cfg[i].name);
-		of_property_read_u8(child, "led-cur", &cfg[i].led_current);
-		of_property_read_u8(child, "max-cur", &cfg[i].max_current);
-		cfg[i].default_trigger =
-			of_get_property(child, "linux,default-trigger", NULL);
+		num_chan_children = of_get_child_count(child);
+		if (num_chan_children != 0)

You're already parsing color. If it is present and equals LED_COLOR_ID_MULTI then we should expect children in the node.

Quoted 5 lines
drivers/leds/leds-lp55xx-common.c +5 -0
+			lp5xx_parse_channel_child(child, cfg, i);
+		else {
+			of_property_read_string(child, "chan-name",
+						&cfg[i].name);
+			of_property_read_u8(child, "led-cur",

drivers/leds/leds-lp55xx-common.c struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev, · line 678

Quoted 10 lines
drivers/leds/leds-lp55xx-common.c +10 -0
+					    &cfg[i].led_current);
+			of_property_read_u8(child, "max-cur",
+					    &cfg[i].max_current);
+			cfg[i].default_trigger =
+				of_get_property(child, "linux,default-trigger",
+						NULL);
+			of_property_read_u32(child, "reg", &led_number);
+
+			if (led_number < 0 || led_number > 6)
+				return ERR_PTR(EINVAL);

I see code redundancy here. There are common properties to parse in both multi color and monochrome LED case: chan-name, led-cur, max-cur, linux,default-trigger, reg. We could have one function for parsing this common subset.

Quoted 65 lines
drivers/leds/leds-lp55xx-common.c +3 -0
+
+			cfg[i].chan_nr = led_number;
+		}
 
 		i++;
 	}
drivers/leds/leds-lp55xx-common.h +10 -0
index 783ed5103ce5..0737e79bc21b 100644
@@ -12,6 +12,8 @@
 #ifndef _LEDS_LP55XX_COMMON_H
 #define _LEDS_LP55XX_COMMON_H
 
+#include <linux/led-class-multicolor.h>
+
 enum lp55xx_engine_index {
 	LP55XX_ENGINE_INVALID,
 	LP55XX_ENGINE_1,
@@ -109,6 +111,9 @@ struct lp55xx_device_config {
 	/* access brightness register */
 	int (*brightness_fn)(struct lp55xx_led *led);
 
+	/* access specific brightness register */
+	int (*intensity_fn)(struct lp55xx_led *led, int chan_num);
+
 	/* current setting function */
 	void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
 
@@ -159,6 +164,7 @@ struct lp55xx_chip {
  * struct lp55xx_led
  * @chan_nr         : Channel number
  * @cdev            : LED class device
+ * @mc_cdev	    : Multi color class device
  * @led_current     : Current setting at each led channel
  * @max_current     : Maximun current at each led channel
  * @brightness      : Brightness value
@@ -167,9 +173,13 @@ struct lp55xx_chip {
 struct lp55xx_led {
 	int chan_nr;
 	struct led_classdev cdev;
+	struct led_classdev_mc mc_cdev;
 	u8 led_current;
 	u8 max_current;
 	u8 brightness;
+/*	int num_colors;*/
+	int channel_color[LED_COLOR_ID_MAX];
+	int grouped_channels[LED_COLOR_ID_MAX];
 	struct lp55xx_chip *chip;
 };
 
include/linux/platform_data/leds-lp55xx.h +2 -0
index 96a787100fda..adce8cec411b 100644
@@ -12,6 +12,8 @@
 #ifndef _LEDS_LP55XX_H
 #define _LEDS_LP55XX_H
 
+#include <linux/led-class-multicolor.h>
+
 /* Clock configuration */
 #define LP55XX_CLOCK_AUTO	0
 #define LP55XX_CLOCK_INT	1

include/linux/platform_data/leds-lp55xx.h struct lp55xx_led_config { · line 29

Quoted 5 lines
include/linux/platform_data/leds-lp55xx.h +2 -0
@@ -23,6 +25,10 @@ struct lp55xx_led_config {
 	u8 chan_nr;
 	u8 led_current; /* mA x10, 0 if led is not connected */
 	u8 max_current;
+	int num_colors;
+	int available_colors;

unsigned long

modules.builtin.modinfo

Quoted 13 lines
include/linux/platform_data/leds-lp55xx.h +2 -0
+	u32 channel_color[LED_COLOR_ID_MAX];
+	int grouped_channels[LED_COLOR_ID_MAX];
 };
 
 struct lp55xx_predef_pattern {
modules.builtin.modinfo new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..e528d8f57796621b6cfef52ad0da44551a482481
GIT binary patch

[...]

This has gotten here by mistake I presume.

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

Re: [PATCH v4 8/9] leds: lp50xx: Add the LP50XX family of the RGB LED driver

In reply to Jacek Anaszewski

Jacek

Thanks for looking at the patch

Quoted 93 lines · Jacek Anaszewski

Hi Dan,

Thank you for the patch I see few things to improve.

Quoted 54 lines · Dan Murphy

Introduce the LP5036/30/24/18 RGB LED driver.
The difference in these parts are the number of
LED outputs where the:

LP5036 can control 36 LEDs
LP5030 can control 30 LEDs
LP5024 can control 24 LEDs
LP5018 can control 18 LEDs

The device has the ability to group LED output into control banks so that multiple LED banks can be controlled with the same mixing and brightness. Inversely the LEDs can also be controlled independently.

---
  drivers/leds/Kconfig       |   7 +
  drivers/leds/Makefile      |   1 +
  drivers/leds/leds-lp50xx.c | 789 +++++++++++++++++++++++++++++++++++++
  3 files changed, 797 insertions(+)

create mode 100644 drivers/leds/leds-lp50xx.c

drivers/leds/Kconfig +7 -0
index 7f780d5b8490..69c037020f6b 100644
@@ -350,6 +350,13 @@ config LEDS_LP3952
  	  To compile this driver as a module, choose M here: the
  	  module will be called leds-lp3952.
 
+config LEDS_LP50XX
+	tristate "LED Support for TI LP5036/30/24/18 LED driver chip"
+	depends on LEDS_CLASS && REGMAP_I2C
+	help
+	  If you say yes here you get support for the Texas Instruments
+	  LP5036, LP5030, LP5024 and LP5018 LED driver.
+
  config LEDS_LP55XX_COMMON
  	tristate "Common Driver for TI/National LP5521/5523/55231/5562/8501"
  	depends on LEDS_LP5521 || LEDS_LP5523 || LEDS_LP5562 || LEDS_LP8501
drivers/leds/Makefile +1 -0
index 897b810257dd..438a5499f3ee 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_LEDS_GPIO_REGISTER)	+= leds-gpio-register.o
  obj-$(CONFIG_LEDS_GPIO)			+= leds-gpio.o
  obj-$(CONFIG_LEDS_LP3944)		+= leds-lp3944.o
  obj-$(CONFIG_LEDS_LP3952)		+= leds-lp3952.o
+obj-$(CONFIG_LEDS_LP50XX)		+= leds-lp50xx.o
  obj-$(CONFIG_LEDS_LP55XX_COMMON)	+= leds-lp55xx-common.o
  obj-$(CONFIG_LEDS_LP5521)		+= leds-lp5521.o
  obj-$(CONFIG_LEDS_LP5523)		+= leds-lp5523.o
drivers/leds/leds-lp50xx.c new file +0 -0
new file mode 100644
index 000000000000..5a32410e4f34

[...]

Quoted 33 lines
drivers/leds/leds-lp50xx.c new file +33 -0
+static int lp50xx_probe_dt(struct lp50xx *priv)
+{
+	u32 led_strings[LP5036_MAX_LED_MODULES];
+	struct fwnode_handle *child = NULL;
+	struct fwnode_handle *led_node = NULL;
+	struct led_init_data init_data;
+	struct lp50xx_led *led;
+	int num_colors;
+	u32 color_id;
+	int led_number;
+	size_t i = 0;
+	int ret;
+
+	priv->enable_gpio = devm_gpiod_get_optional(&priv->client->dev,
+						   "enable", GPIOD_OUT_LOW);
+	if (IS_ERR(priv->enable_gpio)) {
+		ret = PTR_ERR(priv->enable_gpio);
+		dev_err(&priv->client->dev, "Failed to get enable gpio: %d\n",
+			ret);
+		return ret;
+	}
+
+	priv->regulator = devm_regulator_get(&priv->client->dev, "vled");
+	if (IS_ERR(priv->regulator))
+		priv->regulator = NULL;
+
+	if (priv->model_id == LP5009)
+		priv->max_leds = LP5009_MAX_LED_MODULES;
+	else if (priv->model_id == LP5012)
+		priv->max_leds = LP5012_MAX_LED_MODULES;
+	else if (priv->model_id == LP5018)
+		priv->max_leds = LP5018_MAX_LED_MODULES;
+	else if (priv->model_id == LP5024)
Quoted 8 lines

drivers/leds/leds-lp50xx.c line 587

Quoted 5 lines
drivers/leds/leds-lp50xx.c new file +5 -0
+		priv->max_leds = LP5024_MAX_LED_MODULES;
+	else if (priv->model_id == LP5030)
+		priv->max_leds = LP5030_MAX_LED_MODULES;
+	else
+		priv->max_leds = LP5036_MAX_LED_MODULES;

You could simplify that by initializing data property of lp50xx_leds_match array elements and then call of_match_device. You can compare e.g. drivers/leds/leds-pca9532.c

Well I am not using of_ calls in driver anymore.

I would prefer to use device_get_match_data(&client->dev);

Quoted 41 lines
Quoted 41 lines
drivers/leds/leds-lp50xx.c new file +41 -0
+	device_for_each_child_node(&priv->client->dev, child) {
+		led = &priv->leds[i];
+		if (fwnode_property_present(child, "ti,led-bank")) {
+			ret = fwnode_property_read_u32_array(child,
+							     "ti,led-bank",
+							     NULL, 0);
+			ret = fwnode_property_read_u32_array(child,
+							     "ti,led-bank",
+							     led_strings,
+							     ret);
+
+			priv->num_of_banked_leds = ARRAY_SIZE(led_strings);
+
+			ret = lp50xx_set_banks(priv, led_strings);
+			if (ret) {
+				dev_err(&priv->client->dev,
+					"Cannot setup banked LEDs\n");
+				fwnode_handle_put(child);
+				goto child_out;
+			}
+			led->ctrl_bank_enabled = 1;
+
+		} else {
+			ret = fwnode_property_read_u32(child, "reg",
+					       &led_number);
+
+			led->led_number = led_number;
+		}
+		if (ret) {
+			dev_err(&priv->client->dev,
+				"led sourcing property missing\n");
+			fwnode_handle_put(child);
+			goto child_out;
+		}
+
+		if (led_number > priv->max_leds) {
+			dev_err(&priv->client->dev,
+				"led-sources property is invalid\n");
+			ret = -EINVAL;
+			fwnode_handle_put(child);
+			goto child_out;
Quoted 8 lines

drivers/leds/leds-lp50xx.c line 633

Quoted 4 lines
drivers/leds/leds-lp50xx.c new file +4 -0
+		}
+
+		init_data.fwnode = child;
+		init_data.devicename = priv->client->name;

We'd rather not have devicename in the name of this LED class device according to the new LED naming convention. It's not a hot-pluggable device, so for properly designed DT we should not encounter name clash.

Ok I had this code from the previous led label patchset.

I will fix it

Quoted 11 lines

drivers/leds/leds-lp50xx.c line 634

Quoted 2 lines
drivers/leds/leds-lp50xx.c new file +1 -0
+		init_data.default_label = ":";

default_label should not be set for new drivers. See the comment in the include/linux/leds.h:

/*
* string to be used for devicename section of LED class device
* either for label based LED name composition path or for fwnode
* based when devname_mandatory is true
*/

Quoted 4 lines

This is new driver and we should not add any convenience fallbacks for label based DT bindings. Label DT property has been marked deprecated, so for new drivers it should be deemed non-existent.

Same as above

Quoted 19 lines
Quoted 19 lines
drivers/leds/leds-lp50xx.c new file +19 -0
+
+		fwnode_property_read_string(child, "linux,default-trigger",
+				    &led->led_dev.default_trigger);
+		num_colors = 0;
+
+		fwnode_for_each_child_node(child, led_node) {
+			ret = fwnode_property_read_u32(led_node, "color",
+						       &color_id);
+			if (ret)
+				dev_err(&priv->client->dev,
+				"Cannot read color\n");
+
+			led->mc_cdev.available_colors |= (1 << color_id);
+			num_colors++;
+
+		}
+
+		led->priv = priv;
+		led->mc_cdev.ops = &lp50xx_mc_ops;
Quoted 8 lines

drivers/leds/leds-lp50xx.c line 660

Quoted 7 lines
drivers/leds/leds-lp50xx.c new file +7 -0
+		led->mc_cdev.num_leds = num_colors;
+		led->mc_cdev.led_cdev = &led->led_dev;
+		led->led_dev.brightness_set_blocking = lp50xx_brightness_set;
+		led->led_dev.brightness_get = lp50xx_brightness_get;
+		ret = led_classdev_multicolor_register_ext(&priv->client->dev,
+						       &led->mc_cdev,
+						       &init_data);

Why not devm_* prefixed version?

I did create a devm_ version like you did for flash I will change it over

Dan

Quoted 3 lines

[...]

author
#22 lore raw

Re: [PATCH v4 9/9] leds: Update the lp55xx to use the multi color framework

In reply to Jacek Anaszewski

Jacek

Thanks for looking

Quoted 71 lines · Jacek Anaszewski

Hi Dan,

Thank you for the patch. My comments are below.

Quoted 66 lines · Dan Murphy

Update the lp5523 to use the multi color framework.

---
  drivers/leds/leds-lp5523.c                |  13 ++
  drivers/leds/leds-lp55xx-common.c         | 153 ++++++++++++++++++----
  drivers/leds/leds-lp55xx-common.h         |  10 ++
  include/linux/platform_data/leds-lp55xx.h |   6 +
  modules.builtin.modinfo                   | Bin 0 -> 43550 bytes
  5 files changed, 159 insertions(+), 23 deletions(-)

create mode 100644 modules.builtin.modinfo

drivers/leds/leds-lp5523.c +13 -0
index d0b931a136b9..45380b32563f 100644
@@ -791,6 +791,18 @@ static ssize_t store_master_fader_leds(struct device *dev,
  	return ret;
  }
 
+static int lp5523_led_intensity(struct lp55xx_led *led, int chan_num)
+{
+	struct lp55xx_chip *chip = led->chip;
+	int ret;
+
+	mutex_lock(&chip->lock);
+	ret = lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + chan_num,
+		     led->brightness);
+	mutex_unlock(&chip->lock);
+	return ret;
+}
+
  static int lp5523_led_brightness(struct lp55xx_led *led)
  {
  	struct lp55xx_chip *chip = led->chip;
@@ -857,6 +869,7 @@ static struct lp55xx_device_config lp5523_cfg = {
  	.max_channel  = LP5523_MAX_LEDS,
  	.post_init_device   = lp5523_post_init_device,
  	.brightness_fn      = lp5523_led_brightness,
+	.intensity_fn       = lp5523_led_intensity,
  	.set_led_current    = lp5523_set_led_current,
  	.firmware_cb        = lp5523_firmware_loaded,
  	.run_engine         = lp5523_run_engine,
drivers/leds/leds-lp55xx-common.c +8 -0
index 44ced02b49f9..32a4ab8664ca 100644
@@ -35,6 +35,11 @@ static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
  	return cdev_to_lp55xx_led(dev_get_drvdata(dev));
  }
 
+static struct lp55xx_led *mcdev_to_lp55xx_led(struct led_classdev_mc *mc_dev)
+{
+	return container_of(mc_dev, struct lp55xx_led, mc_cdev);
+}
+
  static void lp55xx_reset_device(struct lp55xx_chip *chip)
  {
  	struct lp55xx_device_config *cfg = chip->cfg;
@@ -136,20 +141,55 @@ static int lp55xx_set_brightness(struct led_classdev *cdev,
  {
  	struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
  	struct lp55xx_device_config *cfg = led->chip->cfg;
+	int adj_value[LED_COLOR_ID_MAX];
+	int ret;
+	int i;
Quoted 9 lines

drivers/leds/leds-lp55xx-common.c static int lp55xx_set_brightness(struct led_classdev *cdev, · line 152

Quoted 6 lines
drivers/leds/leds-lp55xx-common.c +6 -0
+
+	if (led->mc_cdev.num_leds > 1) {
+		set_cluster_brightness(&led->mc_cdev, brightness, adj_value);
+		for (i = 0; i < led->mc_cdev.num_leds; i++) {
+			led->brightness = adj_value[i];
+			ret = cfg->intensity_fn(led, led->grouped_channels[i]);

At first glance I would have hard time discerning between brightness_fn and intensity_fn purpose. Maybe color_intensity_fn?

Ack

Quoted 11 lines

drivers/leds/leds-lp55xx-common.c static int lp55xx_set_brightness(struct led_classdev *cdev, · line 157

Quoted 5 lines
drivers/leds/leds-lp55xx-common.c +5 -0
+			if (ret)
+				break;
+		}
+	} else {
+		led->brightness = (u8)brightness;

What benefit stems actually from having the copy of brightness in struct lp55xx_led, when we already have struct led_classdev there? I know that this is pre-existing, but could be optimized away while at it. We can have local u8 variable in the op setting brightness and cast enum_led_brightness to it before passing to lp55xx_write().

This was a carry over from the original driver.  Ln 140.

I am not looking to change existing code functionality.

First pass was just to introduce mc_fw with no disruption or regression

to the base functionality.

Quoted 45 lines
Quoted 45 lines
drivers/leds/leds-lp55xx-common.c +24 -3
+		ret = cfg->brightness_fn(led);
+	}
+
+	return ret;
+}
+
+static int lp55xx_set_color(struct led_classdev_mc *mcled_cdev,
+			    int color, int value)
+{
+	struct lp55xx_led *led = mcdev_to_lp55xx_led(mcled_cdev);
+	struct lp55xx_device_config *cfg = led->chip->cfg;
+	int i;
+
+	for (i = 0; i < led->mc_cdev.num_leds; i++) {
+		if (led->channel_color[i] == color)
+			break;
+	}
 
-	led->brightness = (u8)brightness;
-	return cfg->brightness_fn(led);
+	led->brightness = (u8)value;
+	return cfg->intensity_fn(led, led->grouped_channels[i]);
  }
 
+static struct led_multicolor_ops lp55xx_mc_ops = {
+	.set_color_brightness = lp55xx_set_color,
+};
+
  static int lp55xx_init_led(struct lp55xx_led *led,
  			struct lp55xx_chip *chip, int chan)
  {
  	struct lp55xx_platform_data *pdata = chip->pdata;
  	struct lp55xx_device_config *cfg = chip->cfg;
  	struct device *dev = &chip->cl->dev;
+	int max_channel = cfg->max_channel;
  	char name[32];
  	int ret;
-	int max_channel = cfg->max_channel;
 
  	if (chan >= max_channel) {
  		dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
@@ -159,10 +199,37 @@ static int lp55xx_init_led(struct lp55xx_led *led,
  	if (pdata->led_config[chan].led_current == 0)
  		return 0;
 
Quoted 13 lines

drivers/leds/leds-lp55xx-common.c static int lp55xx_init_led(struct lp55xx_led *led, · line 213

Quoted 12 lines
drivers/leds/leds-lp55xx-common.c +12 -0
+	if (pdata->led_config[chan].name) {
+		led->cdev.name = pdata->led_config[chan].name;
+	} else {
+		snprintf(name, sizeof(name), "%s:channel%d",
+			pdata->label ? : chip->cl->name, chan);
+		led->cdev.name = name;
+	}
+
+	if (pdata->led_config[chan].num_colors > 1) {
+		led->mc_cdev.led_cdev = &led->cdev;
+		led->cdev.brightness_set_blocking = lp55xx_set_brightness;
+		led->cdev.name = led->cdev.name;

?

Duplicate I see that now.

Quoted 27 lines
Quoted 27 lines
drivers/leds/leds-lp55xx-common.c +16 -1
+		led->cdev.groups = lp55xx_led_groups;
+		led->mc_cdev.ops = &lp55xx_mc_ops;
+		led->mc_cdev.num_leds = pdata->led_config[chan].num_colors;
+		led->mc_cdev.available_colors = pdata->led_config[chan].available_colors;
+		memcpy(led->channel_color,
+		       pdata->led_config[chan].channel_color,
+		       sizeof(led->channel_color));
+		memcpy(led->grouped_channels,
+		       pdata->led_config[chan].grouped_channels,
+		       sizeof(led->grouped_channels));
+	} else {
+
+		led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
+		led->cdev.brightness_set_blocking = lp55xx_set_brightness;
+	}	led->cdev.groups = lp55xx_led_groups;
+
  	led->led_current = pdata->led_config[chan].led_current;
  	led->max_current = pdata->led_config[chan].max_current;
  	led->chan_nr = pdata->led_config[chan].chan_nr;
-	led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
 
  	if (led->chan_nr >= max_channel) {
  		dev_err(dev, "Use channel numbers between 0 and %d\n",
@@ -170,18 +237,11 @@ static int lp55xx_init_led(struct lp55xx_led *led,
  		return -EINVAL;
  	}
 
Quoted 15 lines
Quoted 4 lines
drivers/leds/leds-lp55xx-common.c +0 -4
-	led->cdev.brightness_set_blocking = lp55xx_set_brightness;
-	led->cdev.groups = lp55xx_led_groups;
-
-	if (pdata->led_config[chan].name) {

drivers/leds/leds-lp55xx-common.c static int lp55xx_init_led(struct lp55xx_led *led, · line 243

Quoted 10 lines
drivers/leds/leds-lp55xx-common.c +4 -6
-		led->cdev.name = pdata->led_config[chan].name;
-	} else {
-		snprintf(name, sizeof(name), "%s:channel%d",
-			pdata->label ? : chip->cl->name, chan);
-		led->cdev.name = name;
-	}
+	if (pdata->led_config[chan].num_colors > 1)
+		ret = led_classdev_multicolor_register(dev, &led->mc_cdev);
+	else
+		ret = led_classdev_register(dev, &led->cdev);

Why not devm ?

I will call the devm_ for multicolor but conversion for not MC should be done separately

Again not trying to regress original functionality.

Quoted 32 lines
Quoted 32 lines
drivers/leds/leds-lp55xx-common.c +16 -2
-	ret = led_classdev_register(dev, &led->cdev);
  	if (ret) {
  		dev_err(dev, "led register err: %d\n", ret);
  		return ret;
@@ -466,7 +526,6 @@ int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
  		dev_err(&chip->cl->dev, "empty brightness configuration\n");
  		return -EINVAL;
  	}
-
  	for (i = 0; i < num_channels; i++) {
 
  		/* do not initialize channels that are not connected */
@@ -538,6 +597,39 @@ void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
  }
  EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
 
+static int lp5xx_parse_channel_child(struct device_node *np,
+				     struct lp55xx_led_config *cfg,
+				     int chan_num)
+{
+	struct device_node *child;
+	int num_colors = 0;
+	u32 color_id;
+	u32 led_number;
+	int ret;
+
+	cfg[chan_num].default_trigger =
+			of_get_property(np, "linux,default-trigger", NULL);
+
+	for_each_child_of_node(np, child) {
+		of_property_read_string(child, "chan-name",
+					&cfg[chan_num].name);
Quoted 8 lines

drivers/leds/leds-lp55xx-common.c void lp55xx_unregister_sysfs(struct lp55xx_chip *chip) · line 622

Quoted 7 lines
drivers/leds/leds-lp55xx-common.c +7 -0
+		of_property_read_u8(child, "led-cur",
+				    &cfg[chan_num].led_current);
+		of_property_read_u8(child, "max-cur",
+				    &cfg[chan_num].max_current);
+		of_property_read_u32(child, "color", &color_id);
+		cfg[chan_num].channel_color[num_colors] = color_id;
+		cfg[chan_num].available_colors |= 1 << color_id;

set_bit(color_id, &cfg[chan_num].available_colors);

Ack

Quoted 26 lines
Quoted 26 lines
drivers/leds/leds-lp55xx-common.c +12 -0
+		ret = of_property_read_u32(child, "reg", &led_number);
+		cfg[chan_num].grouped_channels[num_colors] = led_number;
+		num_colors++;
+	}
+
+	cfg->num_colors = num_colors;
+
+	return 0;
+}
+
  struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
  						      struct device_node *np)
  {
@@ -545,6 +637,8 @@ struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
  	struct lp55xx_platform_data *pdata;
  	struct lp55xx_led_config *cfg;
  	int num_channels;
+	int num_chan_children;
+	u32 led_number;
  	int i = 0;
 
  	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
@@ -565,13 +659,26 @@ struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
  	pdata->num_channels = num_channels;
 
Quoted 12 lines

drivers/leds/leds-lp55xx-common.c struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev, · line 663

Quoted 10 lines
drivers/leds/leds-lp55xx-common.c +2 -7
  	for_each_child_of_node(np, child) {
-		cfg[i].chan_nr = i;
-
-		of_property_read_string(child, "chan-name", &cfg[i].name);
-		of_property_read_u8(child, "led-cur", &cfg[i].led_current);
-		of_property_read_u8(child, "max-cur", &cfg[i].max_current);
-		cfg[i].default_trigger =
-			of_get_property(child, "linux,default-trigger", NULL);
+		num_chan_children = of_get_child_count(child);
+		if (num_chan_children != 0)

You're already parsing color. If it is present and equals LED_COLOR_ID_MULTI then we should expect children in the node.

I don't see any parsing for color here but I suppose that I can add that now

since it is a common property and if == ID_MULTI then parse children.

Quoted 12 lines
Quoted 12 lines
drivers/leds/leds-lp55xx-common.c +11 -0
+			lp5xx_parse_channel_child(child, cfg, i);
+		else {
+			of_property_read_string(child, "chan-name",
+						&cfg[i].name);
+			of_property_read_u8(child, "led-cur",
+					    &cfg[i].led_current);
+			of_property_read_u8(child, "max-cur",
+					    &cfg[i].max_current);
+			cfg[i].default_trigger =
+				of_get_property(child, "linux,default-trigger",
+						NULL);
Quoted 8 lines

drivers/leds/leds-lp55xx-common.c struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev, · line 678

Quoted 4 lines
drivers/leds/leds-lp55xx-common.c +4 -0
+			of_property_read_u32(child, "reg", &led_number);
+
+			if (led_number < 0 || led_number > 6)
+				return ERR_PTR(EINVAL);

I see code redundancy here. There are common properties to parse in both multi color and monochrome LED case: chan-name, led-cur, max-cur, linux,default-trigger, reg. We could have one function for parsing this common subset.

ACK

Quoted 59 lines
Quoted 59 lines
drivers/leds/leds-lp55xx-common.c +3 -0
+
+			cfg[i].chan_nr = led_number;
+		}
 
  		i++;
  	}
drivers/leds/leds-lp55xx-common.h +10 -0
index 783ed5103ce5..0737e79bc21b 100644
@@ -12,6 +12,8 @@
  #ifndef _LEDS_LP55XX_COMMON_H
  #define _LEDS_LP55XX_COMMON_H
 
+#include <linux/led-class-multicolor.h>
+
  enum lp55xx_engine_index {
  	LP55XX_ENGINE_INVALID,
  	LP55XX_ENGINE_1,
@@ -109,6 +111,9 @@ struct lp55xx_device_config {
  	/* access brightness register */
  	int (*brightness_fn)(struct lp55xx_led *led);
 
+	/* access specific brightness register */
+	int (*intensity_fn)(struct lp55xx_led *led, int chan_num);
+
  	/* current setting function */
  	void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
 
@@ -159,6 +164,7 @@ struct lp55xx_chip {
   * struct lp55xx_led
   * @chan_nr         : Channel number
   * @cdev            : LED class device
+ * @mc_cdev	    : Multi color class device
   * @led_current     : Current setting at each led channel
   * @max_current     : Maximun current at each led channel
   * @brightness      : Brightness value
@@ -167,9 +173,13 @@ struct lp55xx_chip {
  struct lp55xx_led {
  	int chan_nr;
  	struct led_classdev cdev;
+	struct led_classdev_mc mc_cdev;
  	u8 led_current;
  	u8 max_current;
  	u8 brightness;
+/*	int num_colors;*/
+	int channel_color[LED_COLOR_ID_MAX];
+	int grouped_channels[LED_COLOR_ID_MAX];
  	struct lp55xx_chip *chip;
  };
 
include/linux/platform_data/leds-lp55xx.h +0 -0
index 96a787100fda..adce8cec411b 100644
@@ -12,6 +12,8 @@
  #ifndef _LEDS_LP55XX_H
  #define _LEDS_LP55XX_H
 
Quoted 12 lines

include/linux/platform_data/leds-lp55xx.h struct lp55xx_led_config { · line 29

Quoted 11 lines
include/linux/platform_data/leds-lp55xx.h +4 -0
+#include <linux/led-class-multicolor.h>
+
  /* Clock configuration */
  #define LP55XX_CLOCK_AUTO	0
  #define LP55XX_CLOCK_INT	1
@@ -23,6 +25,10 @@ struct lp55xx_led_config {
  	u8 chan_nr;
  	u8 led_current; /* mA x10, 0 if led is not connected */
  	u8 max_current;
+	int num_colors;
+	int available_colors;

unsigned long

ACK

Quoted 16 lines
Quoted 14 lines
include/linux/platform_data/leds-lp55xx.h +2 -0
+	u32 channel_color[LED_COLOR_ID_MAX];
+	int grouped_channels[LED_COLOR_ID_MAX];
  };
 
  struct lp55xx_predef_pattern {
modules.builtin.modinfo new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..e528d8f57796621b6cfef52ad0da44551a482481
GIT binary patch

[...]

Quoted 1 line

This has gotten here by mistake I presume.

Yes Will remove this

author
#12 lore raw

Re: [PATCH v4 6/9] leds: multicolor: Introduce a multicolor class definition

In reply to Jacek Anaszewski

Jacek

Quoted 85 lines · Jacek Anaszewski

Dan,

Quoted 22 lines · Dan Murphy

Introduce a multicolor class that groups colored LEDs within a LED node.

The framework allows for dynamically setting individual LEDs or setting brightness levels of LEDs and updating them virtually simultaneously.

---
  drivers/leds/Kconfig                 |  10 +
  drivers/leds/Makefile                |   1 +
  drivers/leds/led-class-multicolor.c  | 402 +++++++++++++++++++++++++++
  include/linux/led-class-multicolor.h |  96 +++++++
  4 files changed, 509 insertions(+)

create mode 100644 drivers/leds/led-class-multicolor.c create mode 100644 include/linux/led-class-multicolor.h

drivers/leds/Kconfig +0 -0
index f7a3dd7ecf3d..7f780d5b8490 100644

drivers/leds/Kconfig config LEDS_CLASS_FLASH · line 38

Quoted 9 lines
drivers/leds/Kconfig +6 -0
@@ -30,6 +30,16 @@ config LEDS_CLASS_FLASH
  	  for the flash related features of a LED device. It can be built
  	  as a module.
 
+config LEDS_CLASS_MULTI_COLOR
+	tristate "LED Mulit Color LED Class Support"
+	depends on LEDS_CLASS
+	help
+	  This option enables the multicolor LED sysfs class in /sys/class/leds.
+	  It wraps LED Class and adds multicolor LED specific sysfs attributes

s/Class/class/

I'll need to fix it in LED flash class entry too.

Quoted 47 lines
drivers/leds/Kconfig +4 -0
+	  and kernel internal API to it. You'll need this to provide support
+	  for multicolor LEDs that are grouped together. This class is not
+	  intended for single color LEDs. It can be built as a module.
+
  config LEDS_BRIGHTNESS_HW_CHANGED
  	bool "LED Class brightness_hw_changed attribute support"
  	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index 41fb073a39c1..897b810257dd 100644
@@ -4,6 +4,7 @@
  obj-$(CONFIG_NEW_LEDS)			+= led-core.o
  obj-$(CONFIG_LEDS_CLASS)		+= led-class.o
  obj-$(CONFIG_LEDS_CLASS_FLASH)		+= led-class-flash.o
+obj-$(CONFIG_LEDS_CLASS_MULTI_COLOR)	+= led-class-multicolor.o
  obj-$(CONFIG_LEDS_TRIGGERS)		+= led-triggers.o
 
  # LED Platform Drivers
drivers/leds/led-class-multicolor.c new file +22 -0
new file mode 100644
index 000000000000..123443e6d3eb
@@ -0,0 +1,402 @@
+// SPDX-License-Identifier: GPL-2.0
+// LED Multi Color class interface
+// Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
+
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/led-class-multicolor.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+
+#include "leds.h"
+
+struct led_classdev_mc_priv {
+	struct led_classdev_mc *mcled_cdev;
+
+	struct device_attribute max_intensity_attr;
+	struct device_attribute intensity_attr;
+	struct device_attribute color_index_attr;
+
+	enum led_brightness max_intensity;
+	enum led_brightness intensity;
Quoted 8 lines

drivers/leds/led-class-multicolor.c line 28

Quoted 6 lines
drivers/leds/led-class-multicolor.c new file +6 -0
+	enum led_brightness new_intensity;
+
+	struct list_head list;
+
+	int color_id;
+	int color_index;

We need to differentiate both more meaningfully.
Maybe led_color_id and cluster_color_id?

Thats looks better.

Quoted 3 lines

drivers/leds/led-class-multicolor.c line 30

Quoted 2 lines
drivers/leds/led-class-multicolor.c new file +2 -0
+};
+static int calculate_brightness(int brightness, int intensity, int max_intensity)

How about led_mc_calculate_brightness ?

I was trying to keep the function name short for this to keep it the LoC boundary.

But I can change it.

Quoted 12 lines

drivers/leds/led-class-multicolor.c line 39

Quoted 10 lines
drivers/leds/led-class-multicolor.c new file +9 -0
+{
+	if (brightness && intensity && max_intensity)
+		return brightness * intensity / max_intensity;
+
+	return LED_OFF;
+}
+
+void set_cluster_brightness(struct led_classdev_mc *mcled_cdev,
+			    enum led_brightness brightness, int adj_value[])

s/set_cluster/led_mc_set_cluster/

Quoted 3 lines

And I think adj_value is not telling too much. How about:

s/adj_value/color_intensity/

Ack

...

Ack

Quoted 15 lines

drivers/leds/led-class-multicolor.c line 51

Quoted 13 lines
drivers/leds/led-class-multicolor.c new file +12 -0
+{
+	struct led_classdev_mc_data *data = mcled_cdev->data;
+	struct led_classdev_mc_priv *priv;
+	int i = 0;
+
+	list_for_each_entry(priv, &data->color_list, list) {
+		adj_value[i] = calculate_brightness(brightness, priv->intensity,
+						    priv->max_intensity);
+		i++;
+	}
+
+	data->cluster_brightness = brightness;

This property duplicates brightness stored in struct led_classdev and thus it is redundant.

Ack

Quoted 7 lines

drivers/leds/led-class-multicolor.c line 55

Quoted 4 lines
drivers/leds/led-class-multicolor.c new file +4 -0
+}
+
+void get_cluster_brightness(struct led_classdev_mc *mcled_cdev,
+				  int brightness_val[])

s/get_cluster/led_mc_get_cluster/

s/brightness_val/color_intensity/

Ack

Ack

Quoted 43 lines
Quoted 43 lines
drivers/leds/led-class-multicolor.c new file +42 -0
+{
+	struct led_classdev_mc_data *data = mcled_cdev->data;
+	struct led_classdev_mc_priv *priv;
+	int i = 0;
+
+	list_for_each_entry(priv, &data->color_list, list) {
+		brightness_val[i] = priv->intensity;
+		i++;
+	}
+}
+
+static ssize_t color_index_show(struct device *dev,
+				   struct device_attribute *color_index_attr,
+				   char *buf)
+{
+	struct led_classdev_mc_priv *priv = container_of(color_index_attr,
+						    struct led_classdev_mc_priv,
+						      color_index_attr);
+
+	return sprintf(buf, "%d\n", priv->color_id);
+}
+
+static ssize_t color_mix_store(struct device *dev,
+				struct device_attribute *color_mix_attr,
+				const char *buf, size_t size)
+{
+	struct led_classdev_mc_data *data = container_of(color_mix_attr,
+						    struct led_classdev_mc_data,
+						      color_mix_attr);
+	struct led_classdev_mc *mcled_cdev = data->mcled_cdev;
+	const struct led_multicolor_ops *ops = mcled_cdev->ops;
+	struct led_classdev_mc_priv *priv;
+	unsigned int value[LED_COLOR_ID_MAX];
+	int adj_brightness;
+	int nrchars, offset = 0;
+	unsigned int i;
+	int ret;
+
+	for (i = 0; i < mcled_cdev->num_leds; i++) {
+		ret = sscanf(buf + offset, "%i%n", &value[i], &nrchars);
+		if (ret != 1)
+			break;
Quoted 8 lines

drivers/leds/led-class-multicolor.c line 102

Quoted 5 lines
drivers/leds/led-class-multicolor.c new file +5 -0
+
+		offset += nrchars;
+	}
+
+	if (i != mcled_cdev->num_leds) {

Shouldn't we return error if i != mcled_cdev->num_leds - 1 ? How can we know which color the value will be for if there is less values passed than the total number of colors in the cluster?

Ok so during my testing if I had the monochrome array as <R G B>

When I wrote only <R G> and no blue I was getting random values in the array for the

remaining indexes and the blue LED would randomly turn on/off at different levels.

So if the user passes in less then expected only ids with data will be written and the other colors will be turned off by the for loop below.

Quoted 4 lines

drivers/leds/led-class-multicolor.c line 104

Quoted 3 lines
drivers/leds/led-class-multicolor.c new file +2 -0
+		for (; i < LED_COLOR_ID_MAX; i++)
+			value[i] = 0;

What use case is it for?

See above but this should be

for (; i < mcled_cdev->num_leds; i++)

Quoted 11 lines
Quoted 11 lines
drivers/leds/led-class-multicolor.c new file +10 -0
+	}
+
+	list_for_each_entry(priv, &data->color_list, list) {
+		if (data->cluster_brightness) {
+			adj_brightness = calculate_brightness(data->cluster_brightness,
+							      value[priv->color_index],
+							     priv->max_intensity);
+			ret = ops->set_color_brightness(priv->mcled_cdev,
+							priv->color_id,
+							adj_brightness);
Quoted 8 lines

drivers/leds/led-class-multicolor.c line 120

Quoted 6 lines
drivers/leds/led-class-multicolor.c new file +6 -0
+			if (ret < 0)
+				goto done;
+		}
+
+		priv->intensity = value[priv->color_index];
+	}

Here we could use just brightness_set op as a single call. We should always write all colors as a result of write to color_mix anyway.

I guess what is gained by just passing the array down to the device driver and having it

parse the array and do the peripheral call?

Quoted 33 lines
Quoted 20 lines
drivers/leds/led-class-multicolor.c new file +19 -0
+
+done:
+	return size;
+}
+
+static ssize_t intensity_store(struct device *dev,
+				struct device_attribute *intensity_attr,
+				const char *buf, size_t size)
+{
+	struct led_classdev_mc_priv *priv = container_of(intensity_attr,
+						    struct led_classdev_mc_priv,
+						      intensity_attr);
+	struct led_classdev_mc_data *data = priv->mcled_cdev->data;
+	struct led_classdev *led_cdev = priv->mcled_cdev->led_cdev;
+	struct led_multicolor_ops *ops = priv->mcled_cdev->ops;
+	unsigned int adj_value;
+	unsigned long value;
+	ssize_t ret;
+

drivers/leds/led-class-multicolor.c line 149

Quoted 10 lines
drivers/leds/led-class-multicolor.c new file +10 -0
+	mutex_lock(&led_cdev->led_access);
+
+	ret = kstrtoul(buf, 10, &value);
+	if (ret)
+		goto unlock;
+
+	if (value > priv->max_intensity) {
+		ret = -EINVAL;
+		goto unlock;
+	}

We don't fail in this case in LED class, so let's proceed in the same way here:

Quoted 1 line

priv->intensity = min(value, priv->max_brightness);

ACK

Quoted 5 lines
Quoted 5 lines
drivers/leds/led-class-multicolor.c new file +5 -0
+
+	priv->new_intensity = value;
+
+	if (data->cluster_brightness) {
+		adj_value = calculate_brightness(data->cluster_brightness,
Quoted 8 lines

drivers/leds/led-class-multicolor.c line 160

Quoted 6 lines
drivers/leds/led-class-multicolor.c new file +6 -0
+					priv->new_intensity,
+					priv->max_intensity);
+		ret = ops->set_color_brightness(priv->mcled_cdev,
+						priv->color_id, adj_value);
+		if (ret < 0) {
+			priv->new_intensity = priv->intensity;

This is unnecessary complication. Just write the calculated iout intensity.

Not sure what complication you are referring to.

Quoted 3 lines

We need to highlight it in the documentation that exact requested color intensity values are written to the hardware only when brightness == max_brightness.

But that is not a true statement.  Thats not really how it was designed.

Quoted 55 lines
Quoted 55 lines
drivers/leds/led-class-multicolor.c new file +54 -0
+			goto unlock;
+		}
+	}
+
+	priv->intensity = priv->new_intensity;
+	ret = size;
+
+unlock:
+	mutex_unlock(&led_cdev->led_access);
+	return ret;
+}
+
+static ssize_t intensity_show(struct device *dev,
+			      struct device_attribute *intensity_attr,
+			      char *buf)
+{
+	struct led_classdev_mc_priv *priv = container_of(intensity_attr,
+						    struct led_classdev_mc_priv,
+						      intensity_attr);
+	const struct led_multicolor_ops *ops = priv->mcled_cdev->ops;
+	int value = 0;
+
+	if (ops->get_color_brightness) {
+		value = ops->get_color_brightness(priv->mcled_cdev,
+						  priv->color_id);
+		priv->intensity = value;
+	} else {
+		value = priv->intensity;
+	}
+
+	return sprintf(buf, "%d\n", value);
+}
+
+static ssize_t max_intensity_show(struct device *dev,
+				   struct device_attribute *max_intensity_attr,
+				   char *buf)
+{
+	struct led_classdev_mc_priv *priv = container_of(max_intensity_attr,
+						    struct led_classdev_mc_priv,
+						      max_intensity_attr);
+
+	return sprintf(buf, "%d\n", priv->max_intensity);
+}
+
+static int led_multicolor_init_color(struct led_classdev_mc_data *data,
+				     struct led_classdev_mc *mcled_cdev,
+				     int color_id, int color_index)
+{
+	struct led_classdev *led_cdev = mcled_cdev->led_cdev;
+	struct led_classdev_mc_priv *mc_priv;
+	int ret;
+
+	mc_priv = devm_kzalloc(led_cdev->dev, sizeof(*mc_priv), GFP_KERNEL);
+	if (!mc_priv)
Quoted 8 lines

drivers/leds/led-class-multicolor.c line 221

Quoted 7 lines
drivers/leds/led-class-multicolor.c new file +7 -0
+		return -ENOMEM;
+
+	mc_priv->color_id = color_id;
+	mc_priv->mcled_cdev = mcled_cdev;
+
+	data->led_kobj = kobject_create_and_add(led_colors[color_id],
+						data->color_kobj);

You should not use kobject API in the drivers. See [0].

I will look at it.

Dan

<snip> no other comments till v5

#23 lore raw

Re: [PATCH v4 9/9] leds: Update the lp55xx to use the multi color framework

In reply to Dan Murphy

Dan,

Quoted 3 lines · Dan Murphy

Jacek

Thanks for looking

You're welcome.

Quoted 22 lines · Jacek Anaszewski
Quoted 21 lines

Hi Dan,

Thank you for the patch. My comments are below.

Quoted 16 lines · Dan Murphy

Update the lp5523 to use the multi color framework.

---
  drivers/leds/leds-lp5523.c                |  13 ++ drivers/leds/leds-lp55xx-common.c         | 153 ++++++++++++++++++---- drivers/leds/leds-lp55xx-common.h         |  10 ++ include/linux/platform_data/leds-lp55xx.h |   6 + modules.builtin.modinfo                   | Bin 0 -> 43550 bytes

  5 files changed, 159 insertions(+), 23 deletions(-)

  create mode 100644 modules.builtin.modinfo

drivers/leds/leds-lp5523.c +0 -0
index d0b931a136b9..45380b32563f 100644

[...]

Quoted 12 lines
Quoted 6 lines
Quoted 6 lines

+            if (ret)
+                break;
+        }
+    } else {
+        led->brightness = (u8)brightness;

Quoted 6 lines

What benefit stems actually from having the copy of brightness in struct lp55xx_led, when we already have struct led_classdev there? I know that this is pre-existing, but could be optimized away while at it. We can have local u8 variable in the op setting brightness and cast enum_led_brightness to it before passing to lp55xx_write().

Quoted 7 lines

This was a carry over from the original driver.  Ln 140.

I am not looking to change existing code functionality.

First pass was just to introduce mc_fw with no disruption or regression

to the base functionality.

Sure thing. Just wanted to mention that for a record.

[...]

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

  -    led->cdev.brightness_set_blocking = lp55xx_set_brightness; -    led->cdev.groups = lp55xx_led_groups;
-
-    if (pdata->led_config[chan].name) {
-        led->cdev.name = pdata->led_config[chan].name;
-    } else {
-        snprintf(name, sizeof(name), "%s:channel%d",
-            pdata->label ? : chip->cl->name, chan);
-        led->cdev.name = name;
-    }
+    if (pdata->led_config[chan].num_colors > 1)
+        ret = led_classdev_multicolor_register(dev, &led->mc_cdev); +    else
+        ret = led_classdev_register(dev, &led->cdev);

Quoted 6 lines
Quoted 1 line

Why not devm ?

I will call the devm_ for multicolor but conversion for not MC should be done separately

Again not trying to regress original functionality.

Then it's better to switch to devm later. Only then it will allow to optimize the error paths.

Quoted 84 lines
Quoted 32 lines
Quoted 32 lines

-    ret = led_classdev_register(dev, &led->cdev); if (ret) {
          dev_err(dev, "led register err: %d\n", ret); return ret;

drivers/leds/leds-lp5523.c +15 -1
@@ -466,7 +526,6 @@ int lp55xx_register_leds(struct lp55xx_led *led,
 struct lp55xx_chip *chip)
           dev_err(&chip->cl->dev, "empty brightness configuration\n");
           return -EINVAL;
       }
-
       for (i = 0; i < num_channels; i++) {
             /* do not initialize channels that are not connected */
@@ -538,6 +597,39 @@ void lp55xx_unregister_sysfs(struct lp55xx_chip
 *chip)
   }
   EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
   +static int lp5xx_parse_channel_child(struct device_node *np,
+                     struct lp55xx_led_config *cfg,
+                     int chan_num)
+{
+    struct device_node *child;
+    int num_colors = 0;
+    u32 color_id;
+    u32 led_number;
+    int ret;
+
+    cfg[chan_num].default_trigger =
+            of_get_property(np, "linux,default-trigger", NULL);
+
+    for_each_child_of_node(np, child) {
+        of_property_read_string(child, "chan-name",
+                    &cfg[chan_num].name);
Quoted 8 lines

drivers/leds/leds-lp5523.c void lp55xx_unregister_sysfs(struct lp55xx_chip · line 622

Quoted 7 lines
drivers/leds/leds-lp5523.c +7 -0
+        of_property_read_u8(child, "led-cur",
+                    &cfg[chan_num].led_current);
+        of_property_read_u8(child, "max-cur",
+                    &cfg[chan_num].max_current);
+        of_property_read_u32(child, "color", &color_id);
+        cfg[chan_num].channel_color[num_colors] = color_id;
+        cfg[chan_num].available_colors |= 1 << color_id;

set_bit(color_id, &cfg[chan_num].available_colors);

Ack

Quoted 31 lines
Quoted 31 lines
drivers/leds/leds-lp5523.c +10 -0
+        ret = of_property_read_u32(child, "reg", &led_number);
+        cfg[chan_num].grouped_channels[num_colors] = led_number;
+        num_colors++;
+    }
+
+    cfg->num_colors = num_colors;
+
+    return 0;
+}
+

  struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
                                struct device_node *np)
  {

drivers/leds/leds-lp5523.c +2 -3
@@ -545,6 +637,8 @@ struct lp55xx_platform_data
 *lp55xx_of_populate_pdata(struct device *dev,
       struct lp55xx_platform_data *pdata;
       struct lp55xx_led_config *cfg;
       int num_channels;
+    int num_chan_children;
+    u32 led_number;
       int i = 0;
         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
@@ -565,13 +659,26 @@ struct lp55xx_platform_data
 *lp55xx_of_populate_pdata(struct device *dev,
       pdata->num_channels = num_channels;
         for_each_child_of_node(np, child) {
-        cfg[i].chan_nr = i;
-
-        of_property_read_string(child, "chan-name", &cfg[i].name);
Quoted 8 lines

drivers/leds/leds-lp5523.c struct lp55xx_platform_data · line 663

Quoted 6 lines
drivers/leds/leds-lp5523.c +2 -4
-        of_property_read_u8(child, "led-cur", &cfg[i].led_current);
-        of_property_read_u8(child, "max-cur", &cfg[i].max_current);
-        cfg[i].default_trigger =
-            of_get_property(child, "linux,default-trigger", NULL);
+        num_chan_children = of_get_child_count(child);
+        if (num_chan_children != 0)

You're already parsing color. If it is present and equals LED_COLOR_ID_MULTI then we should expect children in the node.

Quoted 2 lines

I don't see any parsing for color here but I suppose that I can add that now

I thought about that occurrence in lp5xx_parse_channel_child().

[...]

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

Re: [PATCH v4 9/9] leds: Update the lp55xx to use the multi color framework

Jacek

Quoted 5 lines · Jacek Anaszewski

Dan,

Quoted 2 lines · Dan Murphy

Jacek

Quoted 7 lines
Quoted 1 line

Thanks for looking

You're welcome.
[...}

I don't see any parsing for color here but I suppose that I can add that now
I thought about that occurrence in lp5xx_parse_channel_child().

No that is color of the child subnode.  These will be the actual color.

But we can check the color of the child node to the parent node and if that is MULTI_ID

then process it as a multi color interface.

Dan

Quoted 2 lines

[...]

#13 lore raw

Re: [PATCH v4 6/9] leds: multicolor: Introduce a multicolor class definition

In reply to Dan Murphy

Dan,

Quoted 100 lines · Dan Murphy

Jacek

Quoted 88 lines · Jacek Anaszewski

Dan,

Quoted 24 lines · Dan Murphy

Introduce a multicolor class that groups colored LEDs within a LED node.

The framework allows for dynamically setting individual LEDs or setting brightness levels of LEDs and updating them virtually simultaneously.

---
  drivers/leds/Kconfig                 |  10 + drivers/leds/Makefile                |   1 + drivers/leds/led-class-multicolor.c  | 402 +++++++++++++++++++++++++++ include/linux/led-class-multicolor.h |  96 +++++++

  4 files changed, 509 insertions(+)

  create mode 100644 drivers/leds/led-class-multicolor.c create mode 100644 include/linux/led-class-multicolor.h

drivers/leds/Kconfig +0 -0
index f7a3dd7ecf3d..7f780d5b8490 100644
@@ -30,6 +30,16 @@ config LEDS_CLASS_FLASH

        for the flash related features of a LED device. It can be built as a module.

Quoted 8 lines

  +config LEDS_CLASS_MULTI_COLOR
+    tristate "LED Mulit Color LED Class Support"
+    depends on LEDS_CLASS
+    help
+      This option enables the multicolor LED sysfs class in /sys/class/leds.
+      It wraps LED Class and adds multicolor LED specific sysfs attributes

s/Class/class/

I'll need to fix it in LED flash class entry too.

Quoted 49 lines

+      and kernel internal API to it. You'll need this to provide support
+      for multicolor LEDs that are grouped together. This class is not +      intended for single color LEDs. It can be built as a module. +
  config LEDS_BRIGHTNESS_HW_CHANGED
      bool "LED Class brightness_hw_changed attribute support" depends on LEDS_CLASS

drivers/leds/Makefile +0 -0
index 41fb073a39c1..897b810257dd 100644
@@ -4,6 +4,7 @@

  obj-$(CONFIG_NEW_LEDS)            += led-core.o obj-$(CONFIG_LEDS_CLASS)        += led-class.o obj-$(CONFIG_LEDS_CLASS_FLASH)        += led-class-flash.o +obj-$(CONFIG_LEDS_CLASS_MULTI_COLOR)    += led-class-multicolor.o obj-$(CONFIG_LEDS_TRIGGERS)        += led-triggers.o # LED Platform Drivers

drivers/leds/led-class-multicolor.c new file +3 -0
new file mode 100644
index 000000000000..123443e6d3eb
@@ -0,0 +1,402 @@
+// SPDX-License-Identifier: GPL-2.0
+// LED Multi Color class interface
+// Copyright (C) 2019 Texas Instruments Incorporated -

http://www.ti.com/

drivers/leds/led-class-multicolor.c new file +19 -0
+
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/led-class-multicolor.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+
+#include "leds.h"
+
+struct led_classdev_mc_priv {
+    struct led_classdev_mc *mcled_cdev;
+
+    struct device_attribute max_intensity_attr;
+    struct device_attribute intensity_attr;
+    struct device_attribute color_index_attr;
+
+    enum led_brightness max_intensity;
+    enum led_brightness intensity;
Quoted 8 lines

drivers/leds/led-class-multicolor.c line 28

Quoted 6 lines

+    enum led_brightness new_intensity;

drivers/leds/led-class-multicolor.c new file +5 -0
+
+    struct list_head list;
+
+    int color_id;
+    int color_index;

We need to differentiate both more meaningfully.
Maybe led_color_id and cluster_color_id?

Quoted 2 lines

Thats looks better.

Looking at it now I had to wonder what I had on mind. So maybe even better shot:

int color_id // so as original
int color_seq_pos // i.e. position in the color_mix sequence

[...]

Quoted 31 lines
Quoted 20 lines
Quoted 20 lines
drivers/leds/led-class-multicolor.c new file +20 -0
+static ssize_t color_mix_store(struct device *dev,
+                struct device_attribute *color_mix_attr,
+                const char *buf, size_t size)
+{
+    struct led_classdev_mc_data *data = container_of(color_mix_attr,
+                            struct led_classdev_mc_data,
+                              color_mix_attr);
+    struct led_classdev_mc *mcled_cdev = data->mcled_cdev;
+    const struct led_multicolor_ops *ops = mcled_cdev->ops;
+    struct led_classdev_mc_priv *priv;
+    unsigned int value[LED_COLOR_ID_MAX];
+    int adj_brightness;
+    int nrchars, offset = 0;
+    unsigned int i;
+    int ret;
+
+    for (i = 0; i < mcled_cdev->num_leds; i++) {
+        ret = sscanf(buf + offset, "%i%n", &value[i], &nrchars);
+        if (ret != 1)
+            break;
Quoted 8 lines

drivers/leds/led-class-multicolor.c line 102

Quoted 5 lines
drivers/leds/led-class-multicolor.c new file +5 -0
+
+        offset += nrchars;
+    }
+
+    if (i != mcled_cdev->num_leds) {

Shouldn't we return error if i != mcled_cdev->num_leds - 1 ? How can we know which color the value will be for if there is less values passed than the total number of colors in the cluster?

Ok so during my testing if I had the monochrome array as <R G B>

Quoted 8 lines

When I wrote only <R G> and no blue I was getting random values in the array for the

remaining indexes and the blue LED would randomly turn on/off at different levels.

So if the user passes in less then expected only ids with data will be written and the other colors will be turned off by the for loop below.

From what I see it will lead to wrong mapping of given color to the value array element in the following case:

echo "<green> <blue>" > color_mix

Then green intensity will be assigned to value[0] (expects red) and blue to value[1] (expects green). Unless I don't get something. Your ABI documentation doesn't mention any way to redefine the color_id returned by <color>/color_id file. And that is good.

Quoted 32 lines
Quoted 5 lines
Quoted 4 lines

+        for (; i < LED_COLOR_ID_MAX; i++)
+            value[i] = 0;

What use case is it for?

See above but this should be

for (; i < mcled_cdev->num_leds; i++)

Quoted 12 lines
Quoted 12 lines

+    }
+
+    list_for_each_entry(priv, &data->color_list, list) { +        if (data->cluster_brightness) {
+            adj_brightness = calculate_brightness(data->cluster_brightness,
+                                  value[priv->color_index], +                                 priv->max_intensity); +            ret = ops->set_color_brightness(priv->mcled_cdev, +                            priv->color_id,
+                            adj_brightness);

Quoted 8 lines
Quoted 6 lines

+            if (ret < 0)
+                goto done;
+        }
+
+        priv->intensity = value[priv->color_index]; +    }

Here we could use just brightness_set op as a single call. We should always write all colors as a result of write to color_mix anyway.

Quoted 4 lines

I guess what is gained by just passing the array down to the device driver and having it

parse the array and do the peripheral call?

Those array values would not be directly written to the device, but used for calculating the actual iout intensities. Driver will just have to call calculate_brightness() (sticking to the naming from this patch) and write the results calculated basing on brightness and max_brightness.

Quoted 1 line

[...]

Quoted 15 lines
Quoted 14 lines

drivers/leds/led-class-multicolor.c line 160

Quoted 12 lines
drivers/leds/led-class-multicolor.c new file +11 -0
+
+    priv->new_intensity = value;
+
+    if (data->cluster_brightness) {
+        adj_value = calculate_brightness(data->cluster_brightness,
+                    priv->new_intensity,
+                    priv->max_intensity);
+        ret = ops->set_color_brightness(priv->mcled_cdev,
+                        priv->color_id, adj_value);
+        if (ret < 0) {
+            priv->new_intensity = priv->intensity;

This is unnecessary complication. Just write the calculated iout intensity.

Quoted 1 line

Not sure what complication you are referring to.

The whole need for new_intensity and cluster_brightness, and then bringing back old intensity value on set_color_brightness() failure.

Quoted 6 lines
Quoted 4 lines

We need to highlight it in the documentation that exact requested color intensity values are written to the hardware only when brightness == max_brightness.

But that is not a true statement.  Thats not really how it was designed.

But it probably should be. It would simplify the design.

So my idea is like I previously described the way I had first understood this design:

The colors set under colors directory don't reflect the iout intensities, but are only used for calculating them, basing on the brightness and max_intensity values.

Effectively, after changing the colors/<color>/intensity the global (legacy monochrome) brightness value will be still valid, since iout color will be recalculated basing on it and the new color intensity.

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

Re: [PATCH v4 6/9] leds: multicolor: Introduce a multicolor class definition

Jacek

Quoted 2 lines · Jacek Anaszewski

Dan,

[...]

Quoted 25 lines

+    for (i = 0; i < mcled_cdev->num_leds; i++) {
+        ret = sscanf(buf + offset, "%i%n", &value[i], &nrchars); +        if (ret != 1)
+            break;

drivers/leds/led-class-multicolor.c new file +5 -0
+
+        offset += nrchars;
+    }
+
+    if (i != mcled_cdev->num_leds) {
Quoted 5 lines
Quoted 3 lines

Shouldn't we return error if i != mcled_cdev->num_leds - 1 ? How can we know which color the value will be for if there is less values passed than the total number of colors in the cluster?

Ok so during my testing if I had the monochrome array as <R G B>

Quoted 8 lines

When I wrote only <R G> and no blue I was getting random values in the array for the

remaining indexes and the blue LED would randomly turn on/off at different levels.

So if the user passes in less then expected only ids with data will be written and the other colors will be turned off by the for loop below.

From what I see it will lead to wrong mapping of given color to the
value array element in the following case:

Quoted 7 lines

echo "<green> <blue>" > color_mix

Then green intensity will be assigned to value[0] (expects red) and blue to value[1] (expects green). Unless I don't get something. Your ABI documentation doesn't mention any way to redefine the color_id returned by <color>/color_id file. And that is good.

This is exactly the issue I had previously brought up.  The user would need to

write all leading colors with a value, even if 0, to correctly set the target LEDs.

We can protect against the trailing colors but not leading colors.

The expectation is that the user space would read the color_id from the file and align

the array accordingly.  This is also why I exposed the intensity under the color so if the

user wanted to not use color_mix file they can update the intensity per LED color.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

+        for (; i < LED_COLOR_ID_MAX; i++)
+            value[i] = 0;

What use case is it for?

See above but this should be

for (; i < mcled_cdev->num_leds; i++)

I might be able to eliminate this loop by initializing the array to 0.

Quoted 22 lines
Quoted 22 lines
Quoted 11 lines
Quoted 11 lines

+    }
+
+    list_for_each_entry(priv, &data->color_list, list) { +        if (data->cluster_brightness) {
+            adj_brightness = calculate_brightness(data->cluster_brightness,
+                                  value[priv->color_index], +                                 priv->max_intensity); +            ret = ops->set_color_brightness(priv->mcled_cdev, +                            priv->color_id,
+                            adj_brightness);

Quoted 8 lines
Quoted 6 lines

+            if (ret < 0)
+                goto done;
+        }
+
+        priv->intensity = value[priv->color_index]; +    }

Here we could use just brightness_set op as a single call. We should always write all colors as a result of write to color_mix anyway.

I guess what is gained by just passing the array down to the device driver and having it

Quoted 6 lines
Quoted 1 line

parse the array and do the peripheral call?

Those array values would not be directly written to the device, but used for calculating the actual iout intensities. Driver will just have to call calculate_brightness() (sticking to the naming from this patch) and write the results calculated basing on brightness and max_brightness.

I would expect that we would do the same behavior for the color_mix file then.

Quoted 14 lines

[...]

Quoted 13 lines
Quoted 13 lines

drivers/leds/led-class-multicolor.c line 160

Quoted 11 lines
drivers/leds/led-class-multicolor.c new file +11 -0
+
+    priv->new_intensity = value;
+
+    if (data->cluster_brightness) {
+        adj_value = calculate_brightness(data->cluster_brightness,
+                    priv->new_intensity,
+                    priv->max_intensity);
+        ret = ops->set_color_brightness(priv->mcled_cdev,
+                        priv->color_id, adj_value);
+        if (ret < 0) {
+            priv->new_intensity = priv->intensity;

This is unnecessary complication. Just write the calculated iout intensity.

Quoted 3 lines
Quoted 1 line

Not sure what complication you are referring to.

The whole need for new_intensity and cluster_brightness, and then bringing back old intensity value on set_color_brightness() failure.

OK

Quoted 10 lines
Quoted 5 lines
Quoted 4 lines

We need to highlight it in the documentation that exact requested color intensity values are written to the hardware only when brightness == max_brightness.

But that is not a true statement.  Thats not really how it was designed.

But it probably should be. It would simplify the design.

So my idea is like I previously described the way I had first understood this design:

Quoted 8 lines

The colors set under colors directory don't reflect the iout intensities, but are only used for calculating them, basing on the brightness and max_intensity values.

Effectively, after changing the colors/<color>/intensity the global (legacy monochrome) brightness value will be still valid, since iout color will be recalculated basing on it and the new color intensity.

OK.  This this would remove the ops from the driver as it is no longer needed.

The color_mix file will work the same way.

What is the trigger then to update the LEDs?

We cannot write the same brightness value to trigger as the class blocks calling down

to the driver if brightness_in == brightness_current.

Dan

#15 lore raw

Re: [PATCH v4 6/9] leds: multicolor: Introduce a multicolor class definition

Dan,

Quoted 46 lines · Dan Murphy

Jacek

Quoted 2 lines · Jacek Anaszewski

Dan,

[...]

Quoted 25 lines

+    for (i = 0; i < mcled_cdev->num_leds; i++) {
+        ret = sscanf(buf + offset, "%i%n", &value[i], &nrchars); +        if (ret != 1)
+            break;

drivers/leds/led-class-multicolor.c new file +5 -0
+
+        offset += nrchars;
+    }
+
+    if (i != mcled_cdev->num_leds) {
Quoted 5 lines
Quoted 3 lines

Shouldn't we return error if i != mcled_cdev->num_leds - 1 ? How can we know which color the value will be for if there is less values passed than the total number of colors in the cluster?

Ok so during my testing if I had the monochrome array as <R G B>

Quoted 8 lines

When I wrote only <R G> and no blue I was getting random values in the array for the

remaining indexes and the blue LED would randomly turn on/off at different levels.

So if the user passes in less then expected only ids with data will be written and the other colors will be turned off by the for loop below.

 From what I see it will lead to wrong mapping of given color to the value array element in the following case:

Quoted 7 lines

echo "<green> <blue>" > color_mix

Then green intensity will be assigned to value[0] (expects red) and blue to value[1] (expects green). Unless I don't get something. Your ABI documentation doesn't mention any way to redefine the color_id returned by <color>/color_id file. And that is good.

This is exactly the issue I had previously brought up.  The user would need to

write all leading colors with a value, even if 0, to correctly set the target LEDs.

We can protect against the trailing colors but not leading colors.

Quoted 9 lines

The expectation is that the user space would read the color_id from the file and align

the array accordingly.  This is also why I exposed the intensity under the color so if the

user wanted to not use color_mix file they can update the intensity per LED color.

I can't see the problem. Just require user to _always_ pass the number of values equal to the number of colors under LED multicolor class device. No more, no less. And fail it this condition is not met.

Quoted 33 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

+        for (; i < LED_COLOR_ID_MAX; i++)
+            value[i] = 0;

What use case is it for?

See above but this should be

for (; i < mcled_cdev->num_leds; i++)

I might be able to eliminate this loop by initializing the array to 0.

Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
Quoted 11 lines

+    }
+
+    list_for_each_entry(priv, &data->color_list, list) { +        if (data->cluster_brightness) {
+            adj_brightness = calculate_brightness(data->cluster_brightness,
+                                  value[priv->color_index], +                                 priv->max_intensity); +            ret = ops->set_color_brightness(priv->mcled_cdev, +                            priv->color_id,
+                            adj_brightness);

Quoted 8 lines
Quoted 6 lines

+            if (ret < 0)
+                goto done;
+        }
+
+        priv->intensity = value[priv->color_index]; +    }

Here we could use just brightness_set op as a single call. We should always write all colors as a result of write to color_mix anyway.

I guess what is gained by just passing the array down to the device driver and having it

parse the array and do the peripheral call?

Quoted 8 lines
Quoted 5 lines

Those array values would not be directly written to the device, but used for calculating the actual iout intensities. Driver will just have to call calculate_brightness() (sticking to the naming from this patch) and write the results calculated basing on brightness and max_brightness.

I would expect that we would do the same behavior for the color_mix file then.

Yes, this is what I meant while mentioning use of brightness_set op for that. The final effect will be always the same - write of the altered iout intensity registers. What will differ will be factors of the equation that calculates iout intensity: for the call originating from color_mix file the changing factor will be color intensity and for the call originating from brightness file it will be brightness. In both cases on the driver side we need to call the same function to calculate iout values using the same equation.

Quoted 35 lines
Quoted 15 lines

[...]

Quoted 13 lines
Quoted 13 lines

drivers/leds/led-class-multicolor.c line 160

Quoted 11 lines
drivers/leds/led-class-multicolor.c new file +11 -0
+
+    priv->new_intensity = value;
+
+    if (data->cluster_brightness) {
+        adj_value = calculate_brightness(data->cluster_brightness,
+                    priv->new_intensity,
+                    priv->max_intensity);
+        ret = ops->set_color_brightness(priv->mcled_cdev,
+                        priv->color_id, adj_value);
+        if (ret < 0) {
+            priv->new_intensity = priv->intensity;

This is unnecessary complication. Just write the calculated iout intensity.

Quoted 3 lines
Quoted 1 line

Not sure what complication you are referring to.

The whole need for new_intensity and cluster_brightness, and then bringing back old intensity value on set_color_brightness() failure.

OK

Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

We need to highlight it in the documentation that exact requested color intensity values are written to the hardware only when brightness == max_brightness.

But that is not a true statement.  Thats not really how it was designed.

But it probably should be. It would simplify the design.

So my idea is like I previously described the way I had first understood this design:

The colors set under colors directory don't reflect the iout intensities, but are only used for calculating them, basing on the brightness and max_intensity values.

Quoted 8 lines
Quoted 4 lines

Effectively, after changing the colors/<color>/intensity the global (legacy monochrome) brightness value will be still valid, since iout color will be recalculated basing on it and the new color intensity.

OK.  This this would remove the ops from the driver as it is no longer needed.

The color_mix file will work the same way.

I've been pondering over the need for color_set, but it should be indeed not needed too. Write to <color>/intensity file will also call brightness_set - it will be comparing anyway old intensities and new ones and will write only the affected register.

Quoted 6 lines

What is the trigger then to update the LEDs?

We cannot write the same brightness value to trigger as the class blocks calling down

to the driver if brightness_in == brightness_current.

All files (intensity, color_mix and brightness) will use the same brightness_set op for changing the iout values.

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

Re: [PATCH v4 1/9] leds: multicolor: Add sysfs interface definition

In reply to Jacek Anaszewski

Jacek

OK finally getting back to this.

Quoted 31 lines · Jacek Anaszewski

Hi Dan,

Thank you for the v4.

I have a bunch of comments below. Please take a look.

Quoted 24 lines · Dan Murphy

Add a documentation of LED Multicolor LED class specific sysfs attributes.

---
  .../ABI/testing/sysfs-class-led-multicolor    | 67 +++++++++++++++++++
  1 file changed, 67 insertions(+)

create mode 100644 Documentation/ABI/testing/sysfs-class-led-multicolor

Documentation/ABI/testing/sysfs-class-led-multicolor new file +9 -0
new file mode 100644
index 000000000000..59839f0eae76
@@ -0,0 +1,67 @@
+What:		/sys/class/leds/<led>/brightness
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy<[email protected]>
+Description:	read/write
+		The multicolor class will redirect the device drivers call back
+		function for brightness control to the multicolor class
+		brightness control function.
+
Quoted 8 lines

Documentation/ABI/testing/sysfs-class-led-multicolor line 13

Quoted 4 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +4 -0
+		Writing to this file will update all LEDs within the group to a
+		calculated percentage of what each color LED in the group is set
+		to.  Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.

Instead of redirecting the reader to led-class-multicolor.txt I'd prefer to have at least the formula to calculate the colors laid out here. Aside of that - it is more helpful to have a full path to the referenced file.

Ack

Quoted 7 lines
Quoted 7 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +7 -0
+
+		The value of the color is from 0 to
+		/sys/class/leds/<led>/max_brightness.
+
+What:		/sys/class/leds/<led>/colors/color_mix
+Date:		Sept 2019
+KernelVersion:	TBD
Quoted 8 lines

Documentation/ABI/testing/sysfs-class-led-multicolor line 24

Quoted 4 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +4 -0
+Contact:	Dan Murphy<[email protected]>
+Description:	read/write
+		The color_mix file allows writing all registered multicolor LEDs
+		virtually at the same time.  The value(s) written to this file

I'd drop parentheses form "value(s)". Multi color LED class device is supposed to always have more then one LED. And if I understand it correctly we have to pass intensities of all colors supported by LED multicolor class device here, even we're changing single one.

Yes that is true.

Quoted 13 lines

Documentation/ABI/testing/sysfs-class-led-multicolor line 27

Quoted 3 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +3 -0
+		contain the intensity values for each multicolor LED within
+		the colors directory.  The color indexes are reported in the
+		color_id file as defined in this document.

This is a bit misleading. It sounds as if single color_id file would be reporting more than one index.

Documentation/ABI/testing/sysfs-class-led-multicolor line 29

Quoted 2 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +2 -0
+		Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.

Here, similarly as for brightness, I would prefer to have complete documentation of this file.

How about:

Quoted 8 lines

The values written to this file should contain the intensity values of each multicolor LED within the colors directory. The index of given color is reported by the color_id file present in colors/<color> directory. The index determines the position in the sequence of intensities on which the related intensity should be passed to this file.

And here we could have the examples from leds-class-multicolor.txt.

I prefer to keep the examples in the leds-class-multicolor.rst.

This is an ABI document not a document describing the code.

I updated the doc to what you have above.

Quoted 16 lines
Quoted 16 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +16 -0
+
+What:		/sys/class/leds/<led>/colors/<led_color>/color_id
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy<[email protected]>
+Description:	read only
+		This file when read will return the index of the color in the
+		color_mix.
+		Please refer to the leds-class-multicolor.txt in the
+		Documentation directory for a complete description.
+
+What:		/sys/class/leds/<led>/colors/<led_color>/intensity
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy<[email protected]>
+Description:	read/write
Quoted 8 lines

Documentation/ABI/testing/sysfs-class-led-multicolor line 49

Quoted 4 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +4 -0
+		The led_color directory is dynamically created based on the
+		colors defined by the registrar of the class.
+		The led_color can be but not limited to red, green, blue,
+		white, amber, yellow and violet.  Drivers can also declare a

Instead of this vague sentence about the available colors I propose to maintain the list of supported colors in leds-class.rst or in a separate file and keep it in sync with the led_colors array. Then we could refer to that file here.

I would rather point to the file that contains the colors.  This way we don't have added documentation

maintenance to add a new color.

Quoted 4 lines

Documentation/ABI/testing/sysfs-class-led-multicolor line 50

Quoted 2 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +1 -0
+		LED color for presentation.  There is one directory per color

I'd not let drivers define their custom colors. It would entail issues related to lack of generic LED_COLOR_ID and DT parsing failure.

Ack

Dan

Quoted 19 lines
Quoted 19 lines
Documentation/ABI/testing/sysfs-class-led-multicolor new file +17 -0
+		presented.  The brightness file is created under each
+		led_color directory and controls the individual LED color
+		setting.
+
+		The value of the color is from 0 to
+		/sys/class/leds/<led>/colors/<led_color>/max_intensity.
+
+What:		/sys/class/leds/<led>/colors/<led_color>/max_intensity
+Date:		Sept 2019
+KernelVersion:	TBD
+Contact:	Dan Murphy<[email protected]>
+Description:	read only
+		Maximum intensity level for the LED color, default is
+		255 (LED_FULL).
+
+		If the LED does not support different intensity levels, this
+		should be 1.