dt-bindings: leds: Add NCP5623 multi-LED Controller
Subsystems (3)
PATCH v2 1/2 dt-bindings: leds: Add NCP5623 multi-LED Controller
NCP5623 is DC-DC multi-LED controller which can be used for RGB illumination or backlight LCD display.
---
Changes in v2:
- Fix commit subject prefix
- drop | from the main description
- Use const in address reg
- Remove LEDs reg description
- Link to v1: https://lore.kernel.org/linux-kernel/[email protected]/T/
.../bindings/leds/onnn,ncp5623.yaml | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/onnn,ncp5623.yaml
Documentation/devicetree/bindings/leds/onnn,ncp5623.yaml new file +96 -0
@@ -0,0 +1,96 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/onnn,ncp5623.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ON Semiconductor NCP5623 multi-LED Driver + +maintainers: + - Abdel Alkuor <[email protected]> + +description: + NCP5623 Triple Output I2C Controlled LED Driver. + https://www.onsemi.com/pdf/datasheet/ncp5623-d.pdf + +properties: + compatible: + enum: + - onnn,ncp5623 + + reg: + const: 0x38 + + multi-led: + type: object + $ref: leds-class-multicolor.yaml# + unevaluatedProperties: false + + properties: + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + patternProperties: + "^led@[0-2]$": + type: object + $ref: common.yaml# + unevaluatedProperties: false + + properties: + reg: + minimum: 0 + maximum: 2 + + required: + - reg + - color + + required: + - "#address-cells" + - "#size-cells" + +required: + - compatible + - reg + - multi-led + +additionalProperties: false + +examples: + - | + #include <dt-bindings/leds/common.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + led-controller@38 { + compatible = "onnn,ncp5623"; + reg = <0x38>; + + multi-led { + color = <LED_COLOR_ID_RGB>; + + #address-cells = <1>; + #size-cells = <0>; + + led@0 { + reg = <0>; + color = <LED_COLOR_ID_RED>; + }; + + led@1 { + reg = <1>; + color = <LED_COLOR_ID_GREEN>; + }; + + led@2 { + reg = <2>; + color = <LED_COLOR_ID_BLUE>; + }; + }; + }; + };
Signature · 1 line
2.34.1
PATCH v2 2/2 leds: Add NCP5623 multi-led driver
NCP5623 is DC-DC multi-LEDs driver which has three PWMs that can be programmed up to 32 steps giving 32768 colors hue.
NCP5623 driver supports gradual dimming upward/downward with programmable delays. Also, the driver supports driving a single LED or multi-LED like RGB.
---
Changes in v2:
- Remove all custom attributes and use hw pattern instead
- Remove filename from the driver description
- Fix coding style
- Destroy the muttex in shutdown callback
- Register mcled device using none devm version as unregistering mcled device calls ncp5632_set_led which uses mutex hence we need to make sure the mutex is still available during the unregistering process.
- Link to v1: https://lore.kernel.org/linux-kernel/[email protected]/T/
drivers/leds/rgb/Kconfig | 11 ++ drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-ncp5623.c | 257 ++++++++++++++++++++++++++++++++ 3 files changed, 269 insertions(+) create mode 100644 drivers/leds/rgb/leds-ncp5623.c
drivers/leds/rgb/Kconfig +11 -0
@@ -27,6 +27,17 @@ config LEDS_KTD202X To compile this driver as a module, choose M here: the module will be called leds-ktd202x. +config LEDS_NCP5623 + tristate "LED support for NCP5623" + depends on I2C + depends on OF + help + This option enables support for ON semiconductor NCP5623 + Triple Output I2C Controlled RGB LED Driver. + + To compile this driver as a module, choose M here: the module + will be called leds-ncp5623. + config LEDS_PWM_MULTICOLOR tristate "PWM driven multi-color LED Support" depends on PWM
drivers/leds/rgb/Makefile +1 -0
@@ -2,6 +2,7 @@ obj-$(CONFIG_LEDS_GROUP_MULTICOLOR) += leds-group-multicolor.o obj-$(CONFIG_LEDS_KTD202X) += leds-ktd202x.o +obj-$(CONFIG_LEDS_NCP5623) += leds-ncp5623.o obj-$(CONFIG_LEDS_PWM_MULTICOLOR) += leds-pwm-multicolor.o obj-$(CONFIG_LEDS_QCOM_LPG) += leds-qcom-lpg.o obj-$(CONFIG_LEDS_MT6370_RGB) += leds-mt6370-rgb.o
drivers/leds/rgb/leds-ncp5623.c new file +257 -0
257 lines not on this page · raw message
Signature · 1 line
2.34.1
Lee Jones
maintainerRe: [PATCH v2 2/2] leds: Add NCP5623 multi-led driver
Quoted 69 lines · Abdel Alkuor
NCP5623 is DC-DC multi-LEDs driver which has three PWMs that can be programmed up to 32 steps giving 32768 colors hue.
NCP5623 driver supports gradual dimming upward/downward with programmable delays. Also, the driver supports driving a single LED or multi-LED like RGB.
Signed-off-by Abdel Alkuor <[email protected]>---
Changes in v2:
- Remove all custom attributes and use hw pattern instead
- Remove filename from the driver description
- Fix coding style
- Destroy the muttex in shutdown callback
- Register mcled device using none devm version as unregistering mcled device callsncp5632_set_ledwhich uses mutex hence we need to make sure the mutex is still available during the unregistering process.
- Link to v1: https://lore.kernel.org/linux-kernel/[email protected]/T/drivers/leds/rgb/Kconfig | 11 ++ drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-ncp5623.c | 257 ++++++++++++++++++++++++++++++++ 3 files changed, 269 insertions(+) create mode 100644 drivers/leds/rgb/leds-ncp5623.cdrivers/leds/rgb/Kconfig +11
-0@@ -27,6 +27,17 @@ config LEDS_KTD202X To compile this driver as a module, choose M here: the module will be called leds-ktd202x. +config LEDS_NCP5623 + tristate "LED support for NCP5623" + depends on I2C + depends on OF + help + This option enables support for ON semiconductor NCP5623 + Triple Output I2C Controlled RGB LED Driver. + + To compile this driver as a module, choose M here: the module + will be called leds-ncp5623. + config LEDS_PWM_MULTICOLOR tristate "PWM driven multi-color LED Support" depends on PWMdrivers/leds/rgb/Makefile +1
-0@@ -2,6 +2,7 @@ obj-$(CONFIG_LEDS_GROUP_MULTICOLOR) += leds-group-multicolor.o obj-$(CONFIG_LEDS_KTD202X) += leds-ktd202x.o +obj-$(CONFIG_LEDS_NCP5623) += leds-ncp5623.o obj-$(CONFIG_LEDS_PWM_MULTICOLOR) += leds-pwm-multicolor.o obj-$(CONFIG_LEDS_QCOM_LPG) += leds-qcom-lpg.o obj-$(CONFIG_LEDS_MT6370_RGB) += leds-mt6370-rgb.odrivers/leds/rgb/leds-ncp5623.c new file +4
-0@@ -0,0 +1,257 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* NCP5623 Multi-LED Driver +*
drivers/leds/rgb/leds-ncp5623.c line 14
Quoted 10 lines
drivers/leds/rgb/leds-ncp5623.c new file +10
-0+* Author: Abdel Alkuor <[email protected]> +* Datasheet: https://www.onsemi.com/pdf/datasheet/ncp5623-d.pdf +*/ + +#include <linux/i2c.h> +#include <linux/module.h> + +#include <linux/led-class-multicolor.h> + +#define NCP5623_REG (x) ((x) << 0x5)
What's 0x5? Probably worth defining.
Quoted 21 lines
drivers/leds/rgb/leds-ncp5623.c new file +21
-0+#define NCP5623_SHUTDOWN_REG NCP5623_REG(0x0) +#define NCP5623_ILED_REG NCP5623_REG(0x1) +#define NCP5623_PWM_REG (index) NCP5623_REG(0x2 + (index)) +#define NCP5623_UPWARD_STEP_REG NCP5623_REG(0x5) +#define NCP5623_DOWNWARD_STEP_REG NCP5623_REG(0x6) +#define NCP5623_DIMMING_TIME_REG NCP5623_REG(0x7) + +#define NCP5623_MAX_BRIGHTNESS 0x1f + +struct ncp5623 { +struct i2c_client *client; +struct led_classdev_mc mc_dev; +struct mutex lock; + +int old_brightness; +unsigned long delay; +}; + +static int ncp5623_write (struct i2c_client *client,u8 reg,u8 data) +{ +return i2c_smbus_write_byte_data (client, reg | data,0 );
drivers/leds/rgb/leds-ncp5623.c line 46
Quoted 10 lines
drivers/leds/rgb/leds-ncp5623.c new file +10
-0+} + +static int ncp5623_brightness_set (struct led_classdev *cdev, +enum led_brightness brightness) +{ +struct led_classdev_mc *mc_cdev =lcdev_to_mccdev (cdev); + struct ncp5623 *ncp = container_of(mc_cdev, struct ncp5623, mc_dev); +int ret; + +guard (mutex)(&ncp->lock);
Are these self-unlocking?
Quoted 13 lines
drivers/leds/rgb/leds-ncp5623.c new file +13
-0+if (ncp->delay &&time_is_after_jiffies (ncp->delay)) +return -EBUSY ; + + ncp->delay =0 ; + +for (int i =0 ; i < mc_cdev->num_colors; i++) { + ret =ncp5623_write (ncp->client, +NCP5623_PWM_REG (mc_cdev->subled_info[i].channel), +min (mc_cdev->subled_info[i].intensity, +NCP5623_MAX_BRIGHTNESS )); +if (ret) +return ret; + }
drivers/leds/rgb/leds-ncp5623.c line 70
Quoted 10 lines
drivers/leds/rgb/leds-ncp5623.c new file +10
-0+ + ret =ncp5623_write (ncp->client,NCP5623_DIMMING_TIME_REG ,0 ); +if (ret) +return ret; + + ret =ncp5623_write (ncp->client,NCP5623_ILED_REG , brightness); +if (ret) +return ret; + + ncp->old_brightness = brightness;
The nomenclature is confusing here.
For the most part, this will carry the present value, no?
Quoted 34 lines
drivers/leds/rgb/leds-ncp5623.c new file +34
-0+return 0 ; +} + +static int ncp5623_pattern_set (struct led_classdev *cdev, +struct led_pattern *pattern, +u32 len,int repeat) +{ +struct led_classdev_mc *mc_cdev =lcdev_to_mccdev (cdev); + struct ncp5623 *ncp = container_of(mc_cdev, struct ncp5623, mc_dev); +int brightness_diff; +u8 reg; +int ret; + +guard (mutex)(&ncp->lock); + +if (ncp->delay &&time_is_after_jiffies (ncp->delay)) +return -EBUSY ; + + ncp->delay =0 ; + +if (pattern[0 ].delta_t >240 || (pattern[0 ].delta_t %8 ) !=0 ) +return -EINVAL ; + + brightness_diff = pattern[0 ].brightness - ncp->old_brightness; + +if (brightness_diff ==0 ) +return 0 ; + +if (pattern[0 ].delta_t) { +if (brightness_diff >0 ) + reg =NCP5623_UPWARD_STEP_REG ; +else + reg =NCP5623_DOWNWARD_STEP_REG ; + }else {
drivers/leds/rgb/leds-ncp5623.c line 115
Quoted 10 lines
drivers/leds/rgb/leds-ncp5623.c new file +10
-0+ reg =NCP5623_ILED_REG ; + } + + ret =ncp5623_write (ncp->client, reg, +min (pattern[0 ].brightness,NCP5623_MAX_BRIGHTNESS )); +if (ret) +return ret; + + ret =ncp5623_write (ncp->client, +NCP5623_DIMMING_TIME_REG , pattern[0 ].delta_t /8 );
Why 8? Magic numbers should be replaced with #defines.
drivers/leds/rgb/leds-ncp5623.c line 122
Quoted 7 lines
drivers/leds/rgb/leds-ncp5623.c new file +7
-0+if (ret) +return ret; + +if (abs (brightness_diff) ==1 ) + ncp->delay =NCP5623_MAX_BRIGHTNESS + brightness_diff; +else + ncp->delay =abs (brightness_diff);
Please comment these lines.
drivers/leds/rgb/leds-ncp5623.c line 134
Quoted 11 lines
drivers/leds/rgb/leds-ncp5623.c new file +11
-0+ ncp->delay =msecs_to_jiffies (ncp->delay * pattern[0 ].delta_t) + jiffies; + + ncp->old_brightness = pattern[0 ].brightness; + +return 0 ; +} + +static int ncp5623_pattern_clear (struct led_classdev *led_cdev) +{ +return 0 ; +}
Not sure I see the point in this.
Is the .pattern_clear() compulsorily?
Quoted 17 lines
drivers/leds/rgb/leds-ncp5623.c new file +17
-0+static int ncp5623_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev; +struct fwnode_handle *mc_node, *led_node; +struct led_init_data init_data = { }; +int num_subleds =0 ; +struct ncp5623 *ncp; +struct mc_subled *subled_info; +u32 color_index; +u32 reg; +int ret; + + ncp =devm_kzalloc (dev,sizeof (*ncp),GFP_KERNEL ); +if (!ncp) +return -ENOMEM ; + + ncp->client = client;
drivers/leds/rgb/leds-ncp5623.c line 162
Quoted 10 lines
drivers/leds/rgb/leds-ncp5623.c new file +10
-0+ + mc_node =device_get_named_child_node (dev,"multi-led" ); +if (!mc_node) +return -EINVAL ; + +fwnode_for_each_child_node (mc_node, led_node) + num_subleds++; + + subled_info =devm_kcalloc (dev, num_subleds, +sizeof (*subled_info),GFP_KERNEL );
No need to wrap here. Checkpatch won't complain.
Quoted 29 lines
drivers/leds/rgb/leds-ncp5623.c new file +29
-0+if (!subled_info) { + ret = -ENOMEM ; + goto release_mc_node; + } + +fwnode_for_each_available_child_node (mc_node, led_node) { + ret =fwnode_property_read_u32 (led_node,"color" , &color_index); +if (ret) { +fwnode_handle_put (led_node); + goto release_mc_node; + } + + ret =fwnode_property_read_u32 (led_node,"reg" , ®); +if (ret) { +fwnode_handle_put (led_node); + goto release_mc_node; + } + + subled_info[ncp->mc_dev.num_colors].channel = reg; + subled_info[ncp->mc_dev.num_colors++].color_index = color_index; + } + + init_data.fwnode = mc_node; + + ncp->mc_dev.led_cdev.max_brightness =NCP5623_MAX_BRIGHTNESS ; + ncp->mc_dev.subled_info = subled_info; + ncp->mc_dev.led_cdev.brightness_set_blocking = ncp5623_brightness_set; + ncp->mc_dev.led_cdev.pattern_set = ncp5623_pattern_set; + ncp->mc_dev.led_cdev.pattern_clear = ncp5623_pattern_clear;
drivers/leds/rgb/leds-ncp5623.c line 201
Quoted 10 lines
drivers/leds/rgb/leds-ncp5623.c new file +10
-0+ ncp->mc_dev.led_cdev.default_trigger ="pattern" ; + +mutex_init (&ncp->lock); +i2c_set_clientdata (client, ncp); + + ret =led_classdev_multicolor_register_ext (dev, &ncp->mc_dev, &init_data); +if (ret) + goto destroy_lock; + +fwnode_handle_put (mc_node);
Didn't you just store this ~16 lines up?
Quoted 58 lines
drivers/leds/rgb/leds-ncp5623.c new file +55
-0+return 0 ; + +destroy_lock: +mutex_destroy (&ncp->lock); + +release_mc_node: +fwnode_handle_put (mc_node); + +return ret; +} + +static void ncp5623_remove (struct i2c_client *client) +{ +struct ncp5623 *ncp =i2c_get_clientdata (client); + +mutex_lock (&ncp->lock); + ncp->delay =0 ; +mutex_unlock (&ncp->lock); + +ncp5623_write (client,NCP5623_DIMMING_TIME_REG ,0 ); +led_classdev_multicolor_unregister (&ncp->mc_dev); +mutex_destroy (&ncp->lock); +} + +static void ncp5623_shutdown (struct i2c_client *client) +{ +struct ncp5623 *ncp =i2c_get_clientdata (client); + +if (!(ncp->mc_dev.led_cdev.flags &LED_RETAIN_AT_SHUTDOWN )) +ncp5623_write (client,NCP5623_SHUTDOWN_REG ,0 ); + +mutex_destroy (&ncp->lock); +} + +static const struct of_device_id ncp5623_id[] = { + { .compatible ="onnn,ncp5623" }, + { } +}; +MODULE_DEVICE_TABLE (of, ncp5623_id); + +static struct i2c_driver ncp5623_i2c_driver = { + .driver = { + .name ="ncp5623" , + .of_match_table = ncp5623_id, + }, + .probe = ncp5623_probe, + .remove = ncp5623_remove, + .shutdown = ncp5623_shutdown, +}; + +module_i2c_driver (ncp5623_i2c_driver); + +MODULE_AUTHOR ("Abdel Alkuor <[email protected]>" ); +MODULE_DESCRIPTION ("NCP5623 Multi-LED driver" ); +MODULE_LICENSE ("GPL" );Signature · 1 line
2.34.1
Signature · 1 line
Lee Jones [李琼斯]
Re: [PATCH v2 2/2] leds: Add NCP5623 multi-led driver
On Fri, Mar 01, 2024 at 08:50:46AM +0000, Lee Jones wrote:
Hi Lee,
Quoted 3 lines
Quoted 1 line
+#define
NCP5623_REG(x)((x) <<0x5)What's
0x5? Probably worth defining.
This is a function offset. I'll add a define.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ guard(mutex)(&ncp->lock);Are these self-unlocking?
Correct. Here is a short introduction about it
https://www.marcusfolkesson.se/blog/mutex-guards-in-the-linux-kernel/
Quoted 7 lines
drivers/leds/rgb/leds-ncp5623.c line 70
Quoted 2 lines
drivers/leds/rgb/leds-ncp5623.c new file +1
-0+ ncp->old_brightness = brightness;The nomenclature is confusing here.
For the most part, this will carry the present value, no?
Yes, I'll change it to current_brightness instead
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = ncp5623_write(ncp->client, + NCP5623_DIMMING_TIME_REG, pattern[0].delta_t / 8);Why 8? Magic numbers should be replaced with #defines.
This is dim step in ms. I'll add a define for it.
Quoted 9 lines
drivers/leds/rgb/leds-ncp5623.c line 134
Quoted 4 lines
drivers/leds/rgb/leds-ncp5623.c new file +4
-0+static int ncp5623_pattern_clear (struct led_classdev *led_cdev) +{ +return 0 ; +}Not sure I see the point in this.
Is the .pattern_clear() compulsorily?
Unfortunately, it is. For example, in pattern_trig_store_patterns, when hw pattern is used, it is expected to have pattern_clear implemented.
static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev,
const char *buf, const u32 *buf_int, size_t count, bool hw_pattern)
{
...
if (data->is_hw_pattern)
led_cdev->pattern_clear(led_cdev); ...
}
Quoted 11 lines
Quoted 11 lines
drivers/leds/rgb/leds-ncp5623.c new file +11
-0+ init_data.fwnode = mc_node; + + ncp->mc_dev.led_cdev.max_brightness =NCP5623_MAX_BRIGHTNESS ; + ncp->mc_dev.subled_info = subled_info; + ncp->mc_dev.led_cdev.brightness_set_blocking = ncp5623_brightness_set; + ncp->mc_dev.led_cdev.pattern_set = ncp5623_pattern_set; + ncp->mc_dev.led_cdev.pattern_clear = ncp5623_pattern_clear; + ncp->mc_dev.led_cdev.default_trigger ="pattern" ; + +mutex_init (&ncp->lock); +i2c_set_clientdata (client, ncp);
Quoted 9 lines
drivers/leds/rgb/leds-ncp5623.c line 201
Quoted 6 lines
drivers/leds/rgb/leds-ncp5623.c new file +6
-0+ + ret =led_classdev_multicolor_register_ext (dev, &ncp->mc_dev, &init_data); +if (ret) + goto destroy_lock; + +fwnode_handle_put (mc_node);Didn't you just store this ~16 lines up?
Ops! I'll remove it.
Thanks,
Abdel
Lee Jones
maintainerRe: [PATCH v2 2/2] leds: Add NCP5623 multi-led driver
Quoted 6 lines · Abdel Alkuor
On Fri, Mar 01, 2024 at 08:50:46AM +0000, Lee Jones wrote:
Hi Lee,
Quoted 3 lines
Quoted 1 line
+#define
NCP5623_REG(x)((x) <<0x5)What's
0x5? Probably worth defining.
Quoted 8 lines
This is a function offset. I'll add a define.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ guard(mutex)(&ncp->lock);Are these self-unlocking?
Correct. Here is a short introduction about it
https://www.marcusfolkesson.se/blog/mutex-guards-in-the-linux-kernel/
Neat.
Quoted 7 lines
Quoted 6 lines
drivers/leds/rgb/leds-ncp5623.c line 70
Quoted 1 line
drivers/leds/rgb/leds-ncp5623.c new file +1
-0+ ncp->old_brightness = brightness;The nomenclature is confusing here.
For the most part, this will carry the present value, no?
Yes, I'll change it to
current_brightnessinstead
Just 'brightness' will be fine.
Quoted 22 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = ncp5623_write(ncp->client, + NCP5623_DIMMING_TIME_REG, pattern[0].delta_t / 8);Why 8? Magic numbers should be replaced with #defines.
This is dim step in ms. I'll add a define for it.
Quoted 9 lines
drivers/leds/rgb/leds-ncp5623.c line 134
Quoted 4 lines
drivers/leds/rgb/leds-ncp5623.c new file +4
-0+static int ncp5623_pattern_clear (struct led_classdev *led_cdev) +{ +return 0 ; +}Not sure I see the point in this.
Is the .pattern_clear() compulsorily?
Unfortunately, it is. For example, in
pattern_trig_store_patterns, when hw pattern is used, it is expected to havepattern_clearimplemented.static
ssize_tpattern_trig_store_patterns(structled_classdev*led_cdev,
const char *buf, const u32*buf_int,size_tcount, boolhw_pattern)
Quoted 6 lines
{ ... if (data->is_hw_pattern) led_cdev->pattern_clear(led_cdev);...
}
Something's not right then. If this is required, are you sure you're not meant to do something here? If there are times when this is not required, it should be possible to omit it.
Signature · 1 line
Lee Jones [李琼斯]
Quoted 5 lines · Abdel Alkuor
NCP5623 is DC-DC multi-LED controller which can be used for RGB illumination or backlight LCD display.
Signed-off-by Abdel Alkuor <[email protected]>---
Best regards,
Krzysztof