Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <jjhiblot@traphandler.com>
Link: https://lore.kernel.org/r/20230728153731.3742339-3-jjhiblot@traphandler.com
Signed-off-by: Lee Jones <lee@kernel.org>

authored by

Jean-Jacques Hiblot and committed by
Lee Jones
c7d80059 afb48153

+31
+9
Documentation/ABI/testing/sysfs-class-led
··· 59 59 brightness. Reading this file when no hw brightness change 60 60 event has happened will return an ENODATA error. 61 61 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 + 62 71 What: /sys/class/leds/<led>/trigger 63 72 Date: March 2006 64 73 KernelVersion: 2.6.17
+21
drivers/leds/led-class.c
··· 76 76 } 77 77 static DEVICE_ATTR_RO(max_brightness); 78 78 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 + 79 92 #ifdef CONFIG_LEDS_TRIGGERS 80 93 static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0); 81 94 static struct bin_attribute *led_trigger_bin_attrs[] = { ··· 103 90 static struct attribute *led_class_attrs[] = { 104 91 &dev_attr_brightness.attr, 105 92 &dev_attr_max_brightness.attr, 93 + &dev_attr_color.attr, 106 94 NULL, 107 95 }; 108 96 ··· 500 486 fwnode_property_read_u32(init_data->fwnode, 501 487 "max-brightness", 502 488 &led_cdev->max_brightness); 489 + 490 + if (fwnode_property_present(init_data->fwnode, "color")) 491 + fwnode_property_read_u32(init_data->fwnode, "color", 492 + &led_cdev->color); 503 493 } 504 494 } else { 505 495 proposed_name = led_cdev->name; ··· 512 494 ret = led_classdev_next_name(proposed_name, final_name, sizeof(final_name)); 513 495 if (ret < 0) 514 496 return ret; 497 + 498 + if (led_cdev->color >= LED_COLOR_ID_MAX) 499 + dev_warn(parent, "LED %s color identifier out of range\n", final_name); 515 500 516 501 mutex_init(&led_cdev->led_access); 517 502 mutex_lock(&led_cdev->led_access);
+1
include/linux/leds.h
··· 100 100 const char *name; 101 101 unsigned int brightness; 102 102 unsigned int max_brightness; 103 + unsigned int color; 103 104 int flags; 104 105 105 106 /* Lower 16 bits reflect status */