Skip to content

Commit c7d8005

Browse files
jjhiblotlag-linaro
authored andcommitted
leds: class: Store the color index in struct led_classdev
Store the color of the LED so that it is not lost after the LED's name has been composed. This color information can then be exposed to the user space or used by the LED consumer. Signed-off-by: Jean-Jacques Hiblot <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent afb4815 commit c7d8005

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Documentation/ABI/testing/sysfs-class-led

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ Description:
5959
brightness. Reading this file when no hw brightness change
6060
event has happened will return an ENODATA error.
6161

62+
What: /sys/class/leds/<led>/color
63+
Date: June 2023
64+
KernelVersion: 6.5
65+
Description:
66+
Color of the LED.
67+
68+
This is a read-only file. Reading this file returns the color
69+
of the LED as a string (e.g: "red", "green", "multicolor").
70+
6271
What: /sys/class/leds/<led>/trigger
6372
Date: March 2006
6473
KernelVersion: 2.6.17

drivers/leds/led-class.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ static ssize_t max_brightness_show(struct device *dev,
7676
}
7777
static DEVICE_ATTR_RO(max_brightness);
7878

79+
static ssize_t color_show(struct device *dev,
80+
struct device_attribute *attr, char *buf)
81+
{
82+
const char *color_text = "invalid";
83+
struct led_classdev *led_cdev = dev_get_drvdata(dev);
84+
85+
if (led_cdev->color < LED_COLOR_ID_MAX)
86+
color_text = led_colors[led_cdev->color];
87+
88+
return sysfs_emit(buf, "%s\n", color_text);
89+
}
90+
static DEVICE_ATTR_RO(color);
91+
7992
#ifdef CONFIG_LEDS_TRIGGERS
8093
static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
8194
static struct bin_attribute *led_trigger_bin_attrs[] = {
@@ -90,6 +103,7 @@ static const struct attribute_group led_trigger_group = {
90103
static struct attribute *led_class_attrs[] = {
91104
&dev_attr_brightness.attr,
92105
&dev_attr_max_brightness.attr,
106+
&dev_attr_color.attr,
93107
NULL,
94108
};
95109

@@ -486,6 +500,10 @@ int led_classdev_register_ext(struct device *parent,
486500
fwnode_property_read_u32(init_data->fwnode,
487501
"max-brightness",
488502
&led_cdev->max_brightness);
503+
504+
if (fwnode_property_present(init_data->fwnode, "color"))
505+
fwnode_property_read_u32(init_data->fwnode, "color",
506+
&led_cdev->color);
489507
}
490508
} else {
491509
proposed_name = led_cdev->name;
@@ -495,6 +513,9 @@ int led_classdev_register_ext(struct device *parent,
495513
if (ret < 0)
496514
return ret;
497515

516+
if (led_cdev->color >= LED_COLOR_ID_MAX)
517+
dev_warn(parent, "LED %s color identifier out of range\n", final_name);
518+
498519
mutex_init(&led_cdev->led_access);
499520
mutex_lock(&led_cdev->led_access);
500521
led_cdev->dev = device_create_with_groups(leds_class, parent, 0,

include/linux/leds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ struct led_classdev {
100100
const char *name;
101101
unsigned int brightness;
102102
unsigned int max_brightness;
103+
unsigned int color;
103104
int flags;
104105

105106
/* Lower 16 bits reflect status */

0 commit comments

Comments
 (0)