leds: add new LED driver for TI LP5812
Subsystems (5)
Nam Tran
maintainerv6 cover leds: add new LED driver for TI LP5812
This patch series adds support for the TI/National Semiconductor LP5812 4x3 matrix RGB LED driver. The driver supports features such as autonomous animation and time-cross-multiplexing (TCM) for dynamic LED effects.
---
Changes in v6:
- Add vcc-supply property to describe the LP5812 power supply.
- Remove chan-name property and entire LED subnodes, as they are not needed.
- Update LP5812 LED driver node to Raspberry Pi 4 B Device Tree, based on updated binding.
- Link to v5: https://lore.kernel.org/linux-leds/[email protected]/
Changes in v5:
- Rebase on v6.15-rc2
- Removed unused functions (lp5812_dump_regs, lp5812_update_bit).
- Address Krzysztof's review comments
- Link to v4: https://lore.kernel.org/linux-leds/[email protected]/
---
Nam Tran (5):
dt-bindings: leds: add TI/National Semiconductor LP5812 LED Driver
leds: add TI/National Semiconductor LP5812 LED Driver
docs: ABI: Document LP5812 LED sysfs interfaces
docs: leds: Document TI LP5812 LED driver
arm64: dts: Add LP5812 LED node for Raspberry Pi 4 Model B
.../ABI/testing/sysfs-bus-i2c-devices-lp5812 | 144 + .../devicetree/bindings/leds/ti,lp5812.yaml | 46 + Documentation/leds/leds-lp5812.rst | 79 + MAINTAINERS | 12 + .../arm/boot/dts/broadcom/bcm2711-rpi-4-b.dts | 10 + drivers/leds/Kconfig | 16 + drivers/leds/Makefile | 1 + drivers/leds/leds-lp5812.c | 2797 +++++++++++++++++ drivers/leds/leds-lp5812.h | 350 +++ 9 files changed, 3455 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-i2c-devices-lp5812 create mode 100644 Documentation/devicetree/bindings/leds/ti,lp5812.yaml create mode 100644 Documentation/leds/leds-lp5812.rst create mode 100644 drivers/leds/leds-lp5812.c create mode 100644 drivers/leds/leds-lp5812.h
base-commit: 8ffd015db85fea3e15a77027fda6c02ced4d2444
Signature · 1 line
2.25.1
Nam Tran
maintainerPATCH v6 1/5 dt-bindings: leds: add TI/National Semiconductor LP5812 LED Driver
The LP5812 is a 4×3 RGB LED driver with an autonomous animation engine and time-cross-multiplexing (TCM) support for up to 12 LEDs. It supports both analog (256 levels) and PWM (8-bit) dimming, including exponential PWM for smooth brightness control.
--- .../devicetree/bindings/leds/ti,lp5812.yaml | 46 +++++++++++++++++++ MAINTAINERS | 6 +++ 2 files changed, 52 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/ti,lp5812.yaml
Documentation/devicetree/bindings/leds/ti,lp5812.yaml new file +46 -0
@@ -0,0 +1,46 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/ti,lp5812.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: TI/National Semiconductor LP5812 LED Driver + +maintainers: + - Nam Tran <[email protected]> + +description: | + The LP5812 is an I2C LED Driver that can support LED matrix 4x3. + For more product information please see the link below: + https://www.ti.com/product/LP5812#tech-docs + +properties: + compatible: + const: ti,lp5812 + + reg: + maxItems: 1 + + vcc-supply: + description: Regulator providing power to the 'VCC' pin. + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + led-controller@1b { + compatible = "ti,lp5812"; + reg = <0x1b>; + vcc-supply = <&vdd_3v3_reg>; + }; + }; + +...
MAINTAINERS +6 -0
@@ -23934,6 +23934,12 @@ S: Supported F: Documentation/devicetree/bindings/iio/dac/ti,dac7612.yaml F: drivers/iio/dac/ti-dac7612.c +TEXAS INSTRUMENTS' LP5812 LED DRIVER +M: Nam Tran <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/leds/ti,lp5812.yaml + TEXAS INSTRUMENTS' LB8864 LED BACKLIGHT DRIVER M: Alexander Sverdlin <[email protected]> L: [email protected]
Signature · 1 line
2.25.1
Re: [PATCH v6 1/5] dt-bindings: leds: add TI/National Semiconductor LP5812 LED Driver
Quoted 11 lines · Nam Tran
The LP5812 is a 4×3 RGB LED driver with an autonomous animation engine and time-cross-multiplexing (TCM) support for up to 12 LEDs. It supports both analog (256 levels) and PWM (8-bit) dimming, including exponential PWM for smooth brightness control.
Signed-off-by Nam Tran <[email protected]>--- .../devicetree/bindings/leds/ti,lp5812.yaml | 46 +++++++++++++++++++ MAINTAINERS | 6 +++ 2 files changed, 52 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/ti,lp5812.yaml
---
<form letter>
This is an automated instruction, just in case, because many review tags are being ignored. If you know the process, you can skip it (please do not feel offended by me posting it here - no bad intentions intended). If you do not know the process, here is a short explanation:
Please add Acked-by/Reviewed-by/Tested-by tags when posting new versions of patchset, under or above your Signed-off-by tag, unless patch changed significantly (e.g. new properties added to the DT bindings). Tag is "received", when provided in a message replied to you on the mailing list. Tools like b4 can help here. However, there's no need to repost patches *only* to add the tags. The upstream maintainer will do that for tags received on the version they apply.
https://elixir.bootlin.com/linux/v6.12-rc3/source/Documentation/process/submitting-patches.rst#L577
</form letter>
Best regards,
Krzysztof
Nam Tran
maintainerPATCH v6 2/5 leds: add TI/National Semiconductor LP5812 LED Driver
In reply to Nam TranThe LP5812 is a 4×3 matrix RGB LED driver
with an autonomous animation engine
and time-cross-multiplexing (TCM) support for up to 12 LEDs. Each LED can be configured through the related registers to realize vivid and fancy lighting effects.
--- MAINTAINERS | 4 + drivers/leds/Kconfig | 16 + drivers/leds/Makefile | 1 + drivers/leds/leds-lp5812.c | 2797 ++++++++++++++++++++++++++++++++++++ drivers/leds/leds-lp5812.h | 350 +++++ 5 files changed, 3168 insertions(+) create mode 100644 drivers/leds/leds-lp5812.c create mode 100644 drivers/leds/leds-lp5812.h
MAINTAINERS +4 -0
@@ -23939,6 +23939,10 @@ M: Nam Tran <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/leds/ti,lp5812.yaml +F: drivers/leds/Kconfig +F: drivers/leds/Makefile +F: drivers/leds/leds-lp5812.c +F: drivers/leds/leds-lp5812.h TEXAS INSTRUMENTS' LB8864 LED BACKLIGHT DRIVER M: Alexander Sverdlin <[email protected]>
drivers/leds/Kconfig +16 -0
@@ -483,6 +483,22 @@ config LEDS_LP5569 Driver provides direct control via LED class and interface for programming the engines. +config LEDS_LP5812 + tristate "Enable LP5812 support LED matrix 4x3" + depends on LEDS_CLASS && I2C + depends on LEDS_CLASS_MULTICOLOR + help + If you say Y here you get support for TI LP5812 LED driver. + + The LP5812 is an I2C-based RGB LED driver that supports a 4x3 LED matrix. + It provides independent brightness control and color mixing for each LED. + This driver integrates with the Linux LED framework and LED multicolor class. + + To compile this driver as a module, choose M here: the + module will be called leds-lp5812. + + If unsure, say N. + config LEDS_LP8501 tristate "LED Support for TI LP8501 LED driver chip" depends on LEDS_CLASS && I2C
drivers/leds/Makefile +1 -0
@@ -54,6 +54,7 @@ obj-$(CONFIG_LEDS_LP5523) += leds-lp5523.o obj-$(CONFIG_LEDS_LP5562) += leds-lp5562.o obj-$(CONFIG_LEDS_LP5569) += leds-lp5569.o obj-$(CONFIG_LEDS_LP55XX_COMMON) += leds-lp55xx-common.o +obj-$(CONFIG_LEDS_LP5812) += leds-lp5812.o obj-$(CONFIG_LEDS_LP8501) += leds-lp8501.o obj-$(CONFIG_LEDS_LP8788) += leds-lp8788.o obj-$(CONFIG_LEDS_LP8860) += leds-lp8860.o
drivers/leds/leds-lp5812.c new file +2797 -0
2,797 lines not on this page · raw message
drivers/leds/leds-lp5812.h new file +350 -0
164 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.25.1
Part of the diff is not shown here. View the raw message.
Re: [PATCH v6 2/5] leds: add TI/National Semiconductor LP5812 LED Driver
Le 19/04/2025 à 20:43, Nam Tran a écrit :
Quoted 5 lines
The LP5812 is a 4×3 matrix RGB LED driver
with an autonomous animation engine
and time-cross-multiplexing (TCM) support for up to 12 LEDs. Each LED can be configured through the related registers to realize vivid and fancy lighting effects.
...
drivers/leds/leds-lp5812.c line 24
Quoted 4 lines
drivers/leds/leds-lp5812.c new file +4
-0+static int lp5812_init_dev_config (struct lp5812_chip *chip, +const char *drive_mode,int rm_led_sysfs); + +static struct drive_mode_led_map chip_leds_map[] = {
I think this could be const.
Quoted 4 lines
drivers/leds/leds-lp5812.c new file +4
-0+ { +"direct_mode" , + (const char *[]){LED0 ,LED1 ,LED2 ,LED3 ,NULL } + },
...
Quoted 14 lines
drivers/leds/leds-lp5812.c new file +14
-0+static int lp5812_get_phase_align (struct lp5812_chip *chip,int led_number, +int *phase_align_val) +{ +int ret; +int bit_pos; +u16 reg; +u8 reg_val; + + reg =DEV_CONFIG7 + (led_number /4 ); + bit_pos = (led_number %4 ) *2 ; + + ret =lp5812_read (chip, reg, ®_val); +if (ret) +return ret;
drivers/leds/leds-lp5812.c line 651
Quoted 10 lines
drivers/leds/leds-lp5812.c new file +10
-0+ + *phase_align_val = (reg_val >> bit_pos) &0x03 ; + +return ret; +} + +static int lp5812_get_led_mode (struct lp5812_chip *chip, +int led_number,enum control_mode *mode) +{ +int ret =0 ;
In several function, sometimes ret is initialized, sometimes it is not. See lp5812_get_led_mode() and lp5812_get_phase_align() just above.
This could be done in a more consistent way.
Quoted 16 lines
drivers/leds/leds-lp5812.c new file +16
-0+u16 reg; +u8 reg_val; + +if (led_number <0x8 ) + reg =DEV_CONFIG3 ; +else + reg =DEV_CONFIG4 ; + + ret =lp5812_read (chip, reg, ®_val); +if (ret) +return ret; + + *mode = (reg_val & (1 << (led_number %8 ))) ?AUTONOMOUS :MANUAL ; + +return 0 ; +}
...
drivers/leds/leds-lp5812.c line 1350
Quoted 2 lines
drivers/leds/leds-lp5812.c new file +2
-0+static void set_mix_sel_led (struct lp5812_chip *chip,int mix_sel_led) +{
Maybe init the 4 values at 0 first, then set to 1 only what is needed below? This would save a few lines of code.
Quoted 25 lines
drivers/leds/leds-lp5812.c new file +25
-0+if (mix_sel_led ==0 ) { + chip->u_drive_mode.s_drive_mode.mix_sel_led_0 =1 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_1 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_2 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_3 =0 ; + } +if (mix_sel_led ==1 ) { + chip->u_drive_mode.s_drive_mode.mix_sel_led_0 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_1 =1 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_2 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_3 =0 ; + } +if (mix_sel_led ==2 ) { + chip->u_drive_mode.s_drive_mode.mix_sel_led_0 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_1 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_2 =1 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_3 =0 ; + } +if (mix_sel_led ==3 ) { + chip->u_drive_mode.s_drive_mode.mix_sel_led_0 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_1 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_2 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_3 =1 ; + } +}
...
drivers/leds/leds-lp5812.c line 1709
Quoted 4 lines
drivers/leds/leds-lp5812.c new file +4
-0+static ssize_t dev_config_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{
The whole function could be simplified with sysfs_emit_at(). This avoids temp buffer, malloc/free and some copies.
See led_auto_animation_show() below.
Quoted 45 lines
drivers/leds/leds-lp5812.c new file +45
-0+int i; +int num_drive_mode; +char *mode_info; +char *total_str; +size_t total_length; +char *const_str ="\nPlease select below valid drive mode:\n" ; +char *const_ex_str ="For Ex: echo tcmscan:1:0 > dev_config\n" ; +int ret =0 ; +struct lp5812_chip *chip =i2c_get_clientdata (to_i2c_client (dev)); + +/* get drive mode and scan order */ +mutex_lock (&chip->lock); + ret =lp5812_get_drive_mode_scan_order (chip); +mutex_unlock (&chip->lock); +if (ret) +return -EIO ; + + mode_info =parse_dev_config_info (chip); +if (!mode_info) +return -ENOMEM ; + + num_drive_mode =ARRAY_SIZE (chip_leds_map); + total_length =strlen (mode_info) +strlen (const_str) + +strlen (const_ex_str) +1 ; +for (i =0 ; i < num_drive_mode; ++i) { + total_length +=strlen (chip_leds_map[i].drive_mode) + +strlen ("\n" ); + } + + total_str =kmalloc (total_length,GFP_KERNEL ); +if (!total_str) +return -ENOMEM ; + +sprintf (total_str,"%s%s%s" , mode_info, const_str, const_ex_str); +for (i =0 ; i < num_drive_mode; ++i) { +strcat (total_str, chip_leds_map[i].drive_mode); +strcat (total_str,"\n" ); + } + + ret =sysfs_emit (buf,"%s" , total_str); +kfree (mode_info); +kfree (total_str); + +return ret; +}
...
Quoted 35 lines
drivers/leds/leds-lp5812.c new file +35
-0+static ssize_t led_auto_animation_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +int ret; +char tmp_str[256 ] = {}; +char usage[128 ] = {}; +char *aeu_select ="AEU Select: " ; +char *start_pause_time ="Start pause time: " ; +char *stop_pause_time ="; Stop pause time: " ; +char *led_playback_time ="; LED Playback time: " ; +int aeu_selection, playback_time, start_pause, stop_pause; +struct lp5812_led *led =to_lp5812_led (kobj); +struct lp5812_chip *chip = led->priv; + +sprintf (usage,"%s%s" , +"Command usage: echo (aeu number):(start pause time):" , +"(stop pause time):(playback time) > autonomous_animation" ); + +mutex_lock (&chip->lock); + ret =led_get_autonomous_animation_config (led); +if (ret) { + ret = -EIO ; + goto out; + } + +/* parse config and feedback to userspace */ + aeu_selection = led->led_playback.s_led_playback.aeu_selection; + playback_time = led->led_playback.s_led_playback.led_playback_time; + start_pause = led->start_stop_pause_time.s_time.second; + stop_pause = led->start_stop_pause_time.s_time.first; +if (aeu_selection ==ONLY_AEU1 ) { + sprintf(tmp_str, "%s%s%s%s%s%s%s%s\n", aeu_select, + "Only use AEU1; ", start_pause_time, + time_name_array[start_pause], stop_pause_time, + time_name_array[stop_pause], led_playback_time,
drivers/leds/leds-lp5812.c line 2206
Quoted 10 lines
drivers/leds/leds-lp5812.c new file +10
-0+ led_playback_time_arr[playback_time]); + }else if (aeu_selection ==AEU1_AEU2 ) { +sprintf (tmp_str,"%s%s%s%s%s%s%s%s\n" , aeu_select, +"Use AEU1 and AEU2; " , start_pause_time, + time_name_array[start_pause], stop_pause_time, + time_name_array[stop_pause], led_playback_time, + led_playback_time_arr[playback_time]); + }else { + sprintf(tmp_str, "%s%s%s%s%s%s%s%s\n", aeu_select, + "Use AEU1,AEU2 and AEU3; ", start_pause_time,
Space missing after the , ?
drivers/leds/leds-lp5812.c line 2218
Quoted 12 lines
drivers/leds/leds-lp5812.c new file +12
-0+ time_name_array[start_pause], stop_pause_time, + time_name_array[stop_pause], led_playback_time, + led_playback_time_arr[playback_time]); + } +strcat (tmp_str, usage); +mutex_unlock (&chip->lock); +return sysfs_emit (buf,"%s\n" , tmp_str); + +out: +mutex_unlock (&chip->lock); +return ret; +}
In order to have it more readable (IMHO), use less buffers, make less copies, reduce code duplication and reduce the locking section, maybe something like (un-tested):
static ssize_t led_auto_animation_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
int aeu_selection, playback_time, start_pause, stop_pause;
struct lp5812_led *led = to_lp5812_led(kobj);
struct lp5812_chip *chip = led->priv;
int pos = 0;
int ret;
mutex_lock(&chip->lock);
ret = led_get_autonomous_animation_config(led);
if (ret) {
ret = -EIO;
goto out;
}
/* parse config and feedback to userspace */
aeu_selection = led->led_playback.s_led_playback.aeu_selection;
playback_time = led->led_playback.s_led_playback.led_playback_time;
start_pause = led->start_stop_pause_time.s_time.second;
stop_pause = led->start_stop_pause_time.s_time.first;
mutex_unlock(&chip->lock);
pos += sysfs_emit_at(buf, pos, "AEU Select: ");
if (aeu_selection == ONLY_AEU1)
pos += sysfs_emit_at(buf, pos, "Only use AEU1");
else if (aeu_selection == AEU1_AEU2)
pos += sysfs_emit_at(buf, pos, "Use AEU1 and AEU2");
else
pos += sysfs_emit_at(buf, pos, "Use AEU1, AEU2 and AEU3");
pos += sysfs_emit_at(buf, pos, "; Start pause time: %s",
time_name_array[start_pause]);
pos += sysfs_emit_at(buf, pos, "; Start pause time: %s",
time_name_array[start_pause]);
pos += sysfs_emit_at(buf, pos, "; LED Playback time: %s",
led_playback_time_arr[playback_time]);
pos += sysfs_emit_at(buf, pos, "\n");
pos += sysfs_emit_at(buf, pos, "Command usage: echo (aeu number):(start pause time):(stop pause time):(playback time) > autonomous_animation\n");
return pos;
out:
mutex_unlock(&chip->lock); return ret; }
...
drivers/leds/leds-lp5812.c line 2629
Quoted 10 lines
drivers/leds/leds-lp5812.c new file +10
-0+static ssize_t aeu_playback_time_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +int ret =0 ; +u8 val =0 ; +struct anim_engine_unit *aeu =to_anim_engine_unit (kobj); +struct lp5812_chip *chip = aeu->led->priv; + +mutex_lock (&chip->lock); + ret =led_aeu_playback_time_get_val (aeu, &val);
Maybe unlock here, to simplify code and be consistent with some other functions above? (led_pwm_dimming_scale_show(), ...)
Several other show/store function could be slightly simplified the same way.
Quoted 8 lines
drivers/leds/leds-lp5812.c new file +8
-0+if (ret !=0 ) { +mutex_unlock (&chip->lock); +return -EIO ; + } +mutex_unlock (&chip->lock); + +return sysfs_emit (buf,"%d\n" , val); +}
...
Quoted 9 lines
drivers/leds/leds-lp5812.h new file +9
-0+struct lp5812_led { +struct kobject kobj; +struct lp5812_chip *priv; +struct attribute_group attr_group; +int enable; +enum control_mode mode; +enum dimming_type dimming_type; +u8 lod_lsd; +u8 auto_pwm;
drivers/leds/leds-lp5812.h line 331
Quoted 10 lines
drivers/leds/leds-lp5812.h new file +10
-0+u8 aep_status; +u16 anim_base_addr; +int led_number;/* start from 0 */ +int is_sysfs_created; +const char *led_name; + +union led_playback led_playback; +union time start_stop_pause_time; + +int total_aeu;
What is the need to keeping it here?
It is set to MAX_AEU. Why not just use it directly?
If needed for future use, maybe, 'aeu' below should be a flex array?
drivers/leds/leds-lp5812.h line 343
Quoted 12 lines
drivers/leds/leds-lp5812.h new file +12
-0+struct anim_engine_unit aeu[MAX_AEU ]; +}; + +struct lp5812_chip { +struct i2c_client *i2c_cl; +struct mutex lock;/* Protects access to device registers */ +struct device *dev; +struct attribute_group attr_group; +const struct lp5812_specific_regs *regs; +const struct drive_mode_led_map *chip_leds_map; +enum device_command command; +int total_leds;
What is the need to keeping it here?
It is set to MAX_LEDS. Why not just use it directly?
If needed for future use, maybe, 'leds' below should be a flex array?
drivers/leds/leds-lp5812.h line 347
Quoted 4 lines
drivers/leds/leds-lp5812.h new file +4
-0+union scan_order u_scan_order; +union drive_mode_info u_drive_mode; + +struct lp5812_led leds[MAX_LEDS ];/* MAX 16 LEDs */
This comment looks useless, IMHO.
Quoted 3 lines
drivers/leds/leds-lp5812.h new file +3
-0+}; + +#endif /*_LEDS_LP5812_H_*/
Nam Tran
maintainerRe: [PATCH v6 2/5] leds: add TI/National Semiconductor LP5812 LED Driver
Quoted 7 lines · Christophe JAILLET
Quoted 5 lines · Nam Tran
The LP5812 is a 4×3 matrix RGB LED driver
with an autonomous animation engine
and time-cross-multiplexing (TCM) support for up to 12 LEDs. Each LED can be configured through the related registers to realize vivid and fancy lighting effects.
Quoted 8 lines
...
drivers/leds/leds-lp5812.c line 24
Quoted 4 lines
drivers/leds/leds-lp5812.c new file +4
-0+static int lp5812_init_dev_config (struct lp5812_chip *chip, +const char *drive_mode,int rm_led_sysfs); + +static struct drive_mode_led_map chip_leds_map[] = {I think this could be const.
I’ll update chip_leds_map to be const.
Quoted 19 lines
Quoted 19 lines
drivers/leds/leds-lp5812.c new file +19
-0+static int lp5812_get_phase_align (struct lp5812_chip *chip,int led_number, +int *phase_align_val) +{ +int ret; +int bit_pos; +u16 reg; +u8 reg_val; + + reg =DEV_CONFIG7 + (led_number /4 ); + bit_pos = (led_number %4 ) *2 ; + + ret =lp5812_read (chip, reg, ®_val); +if (ret) +return ret; + + *phase_align_val = (reg_val >> bit_pos) &0x03 ; + +return ret; +}
Quoted 8 lines
drivers/leds/leds-lp5812.c line 651
Quoted 5 lines
drivers/leds/leds-lp5812.c new file +5
-0+ +static int lp5812_get_led_mode (struct lp5812_chip *chip, +int led_number,enum control_mode *mode) +{ +int ret =0 ;In several function, sometimes ret is initialized, sometimes it is not. See
lp5812_get_led_mode()andlp5812_get_phase_align()just above.
Agreed, I’ll go through and update these functions to follow a consistent pattern.
Quoted 23 lines
drivers/leds/leds-lp5812.c line 1350
Quoted 2 lines
drivers/leds/leds-lp5812.c new file +2
-0+static void set_mix_sel_led (struct lp5812_chip *chip,int mix_sel_led) +{Maybe init the 4 values at 0 first, then set to 1 only what is needed below? This would save a few lines of code.
Quoted 17 lines
drivers/leds/leds-lp5812.c new file +17
-0+if (mix_sel_led ==0 ) { + chip->u_drive_mode.s_drive_mode.mix_sel_led_0 =1 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_1 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_2 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_3 =0 ; + } +if (mix_sel_led ==1 ) { + chip->u_drive_mode.s_drive_mode.mix_sel_led_0 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_1 =1 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_2 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_3 =0 ; + } +if (mix_sel_led ==2 ) { + chip->u_drive_mode.s_drive_mode.mix_sel_led_0 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_1 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_2 =1 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_3 =0 ;
Quoted 8 lines
drivers/leds/leds-lp5812.c line 1375
Quoted 8 lines
drivers/leds/leds-lp5812.c new file +8
-0+ } +if (mix_sel_led ==3 ) { + chip->u_drive_mode.s_drive_mode.mix_sel_led_0 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_1 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_2 =0 ; + chip->u_drive_mode.s_drive_mode.mix_sel_led_3 =1 ; + } +}
Yep, that’s a cleaner approach. I’ll update it accordingly.
Quoted 55 lines
drivers/leds/leds-lp5812.c line 1709
Quoted 4 lines
drivers/leds/leds-lp5812.c new file +4
-0+static ssize_t dev_config_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{The whole function could be simplified with
sysfs_emit_at(). This avoids temp buffer, malloc/free and some copies.See
led_auto_animation_show()below.Quoted 45 lines
drivers/leds/leds-lp5812.c new file +45
-0+int i; +int num_drive_mode; +char *mode_info; +char *total_str; +size_t total_length; +char *const_str ="\nPlease select below valid drive mode:\n" ; +char *const_ex_str ="For Ex: echo tcmscan:1:0 > dev_config\n" ; +int ret =0 ; +struct lp5812_chip *chip =i2c_get_clientdata (to_i2c_client (dev)); + +/* get drive mode and scan order */ +mutex_lock (&chip->lock); + ret =lp5812_get_drive_mode_scan_order (chip); +mutex_unlock (&chip->lock); +if (ret) +return -EIO ; + + mode_info =parse_dev_config_info (chip); +if (!mode_info) +return -ENOMEM ; + + num_drive_mode =ARRAY_SIZE (chip_leds_map); + total_length =strlen (mode_info) +strlen (const_str) + +strlen (const_ex_str) +1 ; +for (i =0 ; i < num_drive_mode; ++i) { + total_length +=strlen (chip_leds_map[i].drive_mode) + +strlen ("\n" ); + } + + total_str =kmalloc (total_length,GFP_KERNEL ); +if (!total_str) +return -ENOMEM ; + +sprintf (total_str,"%s%s%s" , mode_info, const_str, const_ex_str); +for (i =0 ; i < num_drive_mode; ++i) { +strcat (total_str, chip_leds_map[i].drive_mode); +strcat (total_str,"\n" ); + } + + ret =sysfs_emit (buf,"%s" , total_str); +kfree (mode_info); +kfree (total_str); + +return ret; +}
...
Quoted 45 lines
In order to have it more readable (IMHO), use less buffers, make less copies, reduce code duplication and reduce the locking section, maybe something like (un-tested):
static
ssize_tled_auto_animation_show(struct kobject *kobj,
structkobj_attribute*attr, char *buf){ int aeu_selection, playback_time, start_pause, stop_pause; struct lp5812_led *led = to_lp5812_led(kobj); struct lp5812_chip *chip = led->priv; int pos = 0; int ret; mutex_lock(&chip->lock); ret = led_get_autonomous_animation_config(led); if (ret) { ret = -EIO; goto out; } /* parse config and feedback to userspace */ aeu_selection = led->led_playback.s_led_playback.aeu_selection; playback_time = led->led_playback.s_led_playback.led_playback_time; start_pause = led->start_stop_pause_time.s_time.second; stop_pause = led->start_stop_pause_time.s_time.first; mutex_unlock(&chip->lock); pos += sysfs_emit_at(buf, pos, "AEU Select: "); if (aeu_selection == ONLY_AEU1) pos += sysfs_emit_at(buf, pos, "Only use AEU1"); else if (aeu_selection == AEU1_AEU2) pos += sysfs_emit_at(buf, pos, "Use AEU1 and AEU2"); else pos += sysfs_emit_at(buf, pos, "Use AEU1, AEU2 and AEU3"); pos += sysfs_emit_at(buf, pos, "; Start pause time: %s", time_name_array[start_pause]); pos += sysfs_emit_at(buf, pos, "; Start pause time: %s", time_name_array[start_pause]); pos += sysfs_emit_at(buf, pos, "; LED Playback time: %s", led_playback_time_arr[playback_time]); pos += sysfs_emit_at(buf, pos, "\n"); pos += sysfs_emit_at(buf, pos, "Command usage: echo (aeu number):(start
Quoted 8 lines
pause time):(stop pause time):(playback time) > autonomous_animation\n");
return pos;out:
mutex_unlock(&chip->lock); return ret; }
Great point! I'll refactor these functions as suggested.
Quoted 16 lines
drivers/leds/leds-lp5812.c line 2629
Quoted 10 lines
drivers/leds/leds-lp5812.c new file +10
-0+static ssize_t aeu_playback_time_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +int ret =0 ; +u8 val =0 ; +struct anim_engine_unit *aeu =to_anim_engine_unit (kobj); +struct lp5812_chip *chip = aeu->led->priv; + +mutex_lock (&chip->lock); + ret =led_aeu_playback_time_get_val (aeu, &val);Maybe unlock here, to simplify code and be consistent with some other functions above? (
led_pwm_dimming_scale_show(), ...)Several other show/store function could be slightly simplified the same way.
Quoted 8 lines
drivers/leds/leds-lp5812.c line 2441
Quoted 8 lines
drivers/leds/leds-lp5812.c new file +8
-0+if (ret !=0 ) { +mutex_unlock (&chip->lock); +return -EIO ; + } +mutex_unlock (&chip->lock); + +return sysfs_emit (buf,"%d\n" , val); +}
Agreed, I’ll unlock earlier to simplify the code and maintain consistency with other functions.
Quoted 41 lines
Quoted 9 lines
drivers/leds/leds-lp5812.h new file +9
-0+struct lp5812_led { +struct kobject kobj; +struct lp5812_chip *priv; +struct attribute_group attr_group; +int enable; +enum control_mode mode; +enum dimming_type dimming_type; +u8 lod_lsd; +u8 auto_pwm;drivers/leds/leds-lp5812.h line 331
Quoted 10 lines
drivers/leds/leds-lp5812.h new file +10
-0+u8 aep_status; +u16 anim_base_addr; +int led_number;/* start from 0 */ +int is_sysfs_created; +const char *led_name; + +union led_playback led_playback; +union time start_stop_pause_time; + +int total_aeu;What is the need to keeping it here?
It is set toMAX_AEU. Why not just use it directly?If needed for future use, maybe, 'aeu' below should be a flex array?
drivers/leds/leds-lp5812.h line 343
Quoted 12 lines
drivers/leds/leds-lp5812.h new file +12
-0+struct anim_engine_unit aeu[MAX_AEU ]; +}; + +struct lp5812_chip { +struct i2c_client *i2c_cl; +struct mutex lock;/* Protects access to device registers */ +struct device *dev; +struct attribute_group attr_group; +const struct lp5812_specific_regs *regs; +const struct drive_mode_led_map *chip_leds_map; +enum device_command command; +int total_leds;What is the need to keeping it here?
It is set toMAX_LEDS. Why not just use it directly?
Quoted 1 line
If needed for future use, maybe, 'leds' below should be a flex array?
You're right — since total_aeu and total_leds are always set to MAX_AEU and MAX_LEDS, respectively, there's no need to store them separately. I'll remove those fields and use the constants directly.
We'll keep the static arrays as-is, given the fixed hardware limits.
Thanks for the helpful comments!
Best regards,
Nam Tran
Nam Tran
maintainerPATCH v6 3/5 docs: ABI: Document LP5812 LED sysfs interfaces
In reply to Nam TranThe LP5812 is a 4 × 3 matrix RGB LED driver
with autonomous animation engine control.
The driver provides interfaces to configure
LED modes manual/autonomous, set PWM/DC values,
and manage autonomous animation engines.
--- .../ABI/testing/sysfs-bus-i2c-devices-lp5812 | 144 ++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 145 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-i2c-devices-lp5812
Documentation/ABI/testing/sysfs-bus-i2c-devices-lp5812 new file +144 -0
@@ -0,0 +1,144 @@ +What: /sys/bus/i2c/devices/.../lp5812_chip_setup/device_enable +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Enables or disables the LP5812 device. (RW) + 0 - Disable + 1 - Enable + +What: /sys/bus/i2c/devices/.../lp5812_chip_setup/dev_config +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Configures drive mode and scan order. (RW) + Some valid values: tcmscan:4:0:1:2:3 (default), tcmscan:3:0:1:2, mixscan:2:2:0:3, mixscan:3:0:1:2:3 + +What: /sys/bus/i2c/devices/.../lp5812_chip_setup/device_command +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Issues device-level commands. (WO) + Valid values: "update", "start", "stop", "pause", "continue" + +What: /sys/bus/i2c/devices/.../lp5812_chip_setup/sw_reset +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Triggers a software reset of the device. (WO) + 1 - resets device + 0 - does not reset device + +What: /sys/bus/i2c/devices/.../lp5812_chip_setup/fault_clear +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Clears fault status. (WO) + 1 - clears fault status + 0 - does not clear fault status + +What: /sys/bus/i2c/devices/.../lp5812_chip_setup/tsd_config_status +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Report the current thermal shutdown config status. (RO) + +What: /sys/bus/i2c/devices/.../led_<id>/activate +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Activate or deactivate the specified LED channel. (RW) + 1 - Activate + 0 - Deactivate + +What: /sys/bus/i2c/devices/.../led_<id>/mode +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Selects LED operation mode. (RW) + Valid values: "manual", "autonomous" + +What: /sys/bus/i2c/devices/.../led_<id>/manual_dc +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + DC current level in manual mode. (RW) + Valid values: 0 - 255 + +What: /sys/bus/i2c/devices/.../led_<id>/manual_pwm +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + PWM duty cycle in manual mode. (RW) + Valid values: 0 - 255 + +What: /sys/bus/i2c/devices/.../led_<id>/autonomous_dc +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + DC current level used in autonomous mode. (RW) + Valid values: 0 - 255 + +What: /sys/bus/i2c/devices/.../led_<id>/autonomous_dc +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + DC current level used in autonomous mode. (RW) + Valid values: 0 - 255 + +What: /sys/bus/i2c/devices/.../led_<id>/pwm_dimming_scale +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + PWM dimming scale type. (RW) + Valid values: "linear", "exponential" + +What: /sys/bus/i2c/devices/.../led_<id>/pwm_phase_align +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Configures PWM phase alignment. (RW) + Valid values: "forward", "middle", "backward" + +What: /sys/bus/i2c/devices/.../led_<id>/autonomous_animation +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Controls AEU configuration and playback. (RW) + Format: (aeu number):(start pause time):(stop pause time):(playback time) + with aeu number 1, 2, 3; playback time 0 - 15 + +What: /sys/bus/i2c/devices/.../led_<id>/aep_status +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Shows current animation pattern status, value from 0 to 7. (RO) + +What: /sys/bus/i2c/devices/.../led_<id>/auto_pwm_val +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + Shows the pwm value in autonomous mode when pause the animation, value from 0 to 255. (RO) + +What: /sys/bus/i2c/devices/.../led_<id>/lod_lsd +Date: May 2025 +KernelVersion: 6.16 +Contact: Nam Tran <[email protected]> +Description: + 0 0 mean no lod and lsd fault detected, 1 1 mean lod and lsd fault detected (RO)
MAINTAINERS +1 -0
@@ -23938,6 +23938,7 @@ TEXAS INSTRUMENTS' LP5812 LED DRIVER M: Nam Tran <[email protected]> L: [email protected] S: Maintained +F: Documentation/ABI/testing/sysfs-bus-i2c-devices-lp5812 F: Documentation/devicetree/bindings/leds/ti,lp5812.yaml F: drivers/leds/Kconfig F: drivers/leds/Makefile
Signature · 1 line
2.25.1
Nam Tran
maintainerPATCH v6 4/5 docs: leds: Document TI LP5812 LED driver
In reply to Nam TranThe driver provides sysfs interfaces to control and configure the LP5812 device and its LED channels.
The documetation describes the chip's capabilities, sysfs interface, and usage examples.
--- Documentation/leds/leds-lp5812.rst | 79 ++++++++++++++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 80 insertions(+) create mode 100644 Documentation/leds/leds-lp5812.rst
Documentation/leds/leds-lp5812.rst new file +79 -0
@@ -0,0 +1,79 @@ +======================== +Kernel driver for lp5812 +======================== + +* TI/National Semiconductor LP5812 LED Driver +* Datasheet: https://www.ti.com/product/LP5812#tech-docs + +Authors: Jared Zhou <[email protected]> + +Description +=========== + +The LP5812 is a 4x3 maxtrix LED driver with support for both manual and +autonomous animation control. It provides features such as: + +- PWM dimming and DC current control +- Slope time configuration +- Autonomous Engine Unit (AEU) for LED animation playback +- Flexible scan and drive mode configuration + +This driver provides sysfs interfaces to control and configure the LP5812 +device and its LED channels. + +Sysfs Interface +=============== + +LP5812 device exposes a chip-level sysfs group: + /sys/bus/i2c/devices/<i2c-dev-addr>/lp5812_chip_setup/ + +The following attributes are available at chip level: + - device_enable: Enable/disable the device (RW) + - dev_config: Configure drive mode and scan order (RW) + - device_command: Issue device-wide commands (WO) + - sw_reset: Reset the hardware (WO) + - fault_clear: Clear any device faults (WO) + - tsd_config_status: Read thermal shutdown config status (RO) + +Each LED channel is exposed as: + /sys/bus/i2c/devices/<i2c-dev-addr>/led_<id>/ + +Each LED exposes the following attributes: + - activate: Activate or deactivate the LED (RW) + - mode: manual or autonomous mode (RW) + - manual_dc: DC current value (0–255) (RW) + - manual_pwm: PWM duty cycle (0–255) (RW) + - autonomous_dc: DC current in autonomous mode (RW) + - pwm_dimming_scale: linear or exponential (RW) + - pwm_phase_align: PWM alignment mode (RW) + - autonomous_animation: Config autonomous animation mode with aeu number, start pause time, stop pause time, playback time (RW) + - aep_status: autonomous engine pattern status (RO) + - auto_pwm_val: pwm value in autonomous mode when pause the animation (RO) + - lod_lsd: lod and lsd fault detected status (RO) + +Example Usage +============= + +To control led_A0 in manual mode:: + echo 1 > /sys/bus/i2c/drivers/lp5812/xxxx/lp5812_chip_setup/device_enable + echo 1 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/activate + echo manual > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/mode + echo 100 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/manual_dc + echo 100 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/manual_pwm + +To control led_A0 in autonomous mode:: + echo 1 > /sys/bus/i2c/drivers/lp5812/xxxx/lp5812_chip_setup/device_enable + echo 1 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/activate + echo autonomous > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/mode + echo 1:10:10:15 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/autonomous_animation + echo 100 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/pwm1 + echo 100 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/pwm2 + echo 100 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/pwm3 + echo 100 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/pwm4 + echo 100 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/pwm5 + echo 5 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/slope_time_t1 + echo 5 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/slope_time_t2 + echo 5 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/slope_time_t3 + echo 5 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/slope_time_t4 + echo 1 > /sys/bus/i2c/drivers/lp5812/xxxx/led_A0/AEU1/playback_time + echo start > /sys/bus/i2c/drivers/lp5812/xxxx/lp5812_chip_setup/device_command
MAINTAINERS +1 -0
@@ -23940,6 +23940,7 @@ L: [email protected] S: Maintained F: Documentation/ABI/testing/sysfs-bus-i2c-devices-lp5812 F: Documentation/devicetree/bindings/leds/ti,lp5812.yaml +F: Documentation/leds/leds-lp5812.rst F: drivers/leds/Kconfig F: drivers/leds/Makefile F: drivers/leds/leds-lp5812.c
Signature · 1 line
2.25.1
Nam Tran
maintainerPATCH v6 5/5 arm64: dts: Add LP5812 LED node for Raspberry Pi 4 Model B
In reply to Nam TranAdd the LP5812 LED driver node to the Device Tree for Raspberry Pi 4 B. This enables the LED connected to the LP5812 to be controlled via I2C.
--- arch/arm/boot/dts/broadcom/bcm2711-rpi-4-b.dts | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/arm/boot/dts/broadcom/bcm2711-rpi-4-b.dts +10 -0
@@ -152,6 +152,16 @@ &hdmi1 { status = "okay"; }; +&i2c1 { + #address-cells = <1>; + #size-cells = <0>; + + led-controller@1b { + compatible = "ti,lp5812"; + reg = <0x1b>; + }; +}; + &led_act { gpios = <&gpio 42 GPIO_ACTIVE_HIGH>; };
Signature · 1 line
2.25.1