0001 ========================
0002 LED handling under Linux
0003 ========================
0004
0005 In its simplest form, the LED class just allows control of LEDs from
0006 userspace. LEDs appear in /sys/class/leds/. The maximum brightness of the
0007 LED is defined in max_brightness file. The brightness file will set the brightness
0008 of the LED (taking a value 0-max_brightness). Most LEDs don't have hardware
0009 brightness support so will just be turned on for non-zero brightness settings.
0010
0011 The class also introduces the optional concept of an LED trigger. A trigger
0012 is a kernel based source of led events. Triggers can either be simple or
0013 complex. A simple trigger isn't configurable and is designed to slot into
0014 existing subsystems with minimal additional code. Examples are the disk-activity,
0015 nand-disk and sharpsl-charge triggers. With led triggers disabled, the code
0016 optimises away.
0017
0018 Complex triggers while available to all LEDs have LED specific
0019 parameters and work on a per LED basis. The timer trigger is an example.
0020 The timer trigger will periodically change the LED brightness between
0021 LED_OFF and the current brightness setting. The "on" and "off" time can
0022 be specified via /sys/class/leds/<device>/delay_{on,off} in milliseconds.
0023 You can change the brightness value of a LED independently of the timer
0024 trigger. However, if you set the brightness value to LED_OFF it will
0025 also disable the timer trigger.
0026
0027 You can change triggers in a similar manner to the way an IO scheduler
0028 is chosen (via /sys/class/leds/<device>/trigger). Trigger specific
0029 parameters can appear in /sys/class/leds/<device> once a given trigger is
0030 selected.
0031
0032
0033 Design Philosophy
0034 =================
0035
0036 The underlying design philosophy is simplicity. LEDs are simple devices
0037 and the aim is to keep a small amount of code giving as much functionality
0038 as possible. Please keep this in mind when suggesting enhancements.
0039
0040
0041 LED Device Naming
0042 =================
0043
0044 Is currently of the form:
0045
0046 "devicename:color:function"
0047
0048 - devicename:
0049 it should refer to a unique identifier created by the kernel,
0050 like e.g. phyN for network devices or inputN for input devices, rather
0051 than to the hardware; the information related to the product and the bus
0052 to which given device is hooked is available in sysfs and can be
0053 retrieved using get_led_device_info.sh script from tools/leds; generally
0054 this section is expected mostly for LEDs that are somehow associated with
0055 other devices.
0056
0057 - color:
0058 one of LED_COLOR_ID_* definitions from the header
0059 include/dt-bindings/leds/common.h.
0060
0061 - function:
0062 one of LED_FUNCTION_* definitions from the header
0063 include/dt-bindings/leds/common.h.
0064
0065 If required color or function is missing, please submit a patch
0066 to linux-leds@vger.kernel.org.
0067
0068 It is possible that more than one LED with the same color and function will
0069 be required for given platform, differing only with an ordinal number.
0070 In this case it is preferable to just concatenate the predefined LED_FUNCTION_*
0071 name with required "-N" suffix in the driver. fwnode based drivers can use
0072 function-enumerator property for that and then the concatenation will be handled
0073 automatically by the LED core upon LED class device registration.
0074
0075 LED subsystem has also a protection against name clash, that may occur
0076 when LED class device is created by a driver of hot-pluggable device and
0077 it doesn't provide unique devicename section. In this case numerical
0078 suffix (e.g. "_1", "_2", "_3" etc.) is added to the requested LED class
0079 device name.
0080
0081 There might be still LED class drivers around using vendor or product name
0082 for devicename, but this approach is now deprecated as it doesn't convey
0083 any added value. Product information can be found in other places in sysfs
0084 (see tools/leds/get_led_device_info.sh).
0085
0086 Examples of proper LED names:
0087
0088 - "red:disk"
0089 - "white:flash"
0090 - "red:indicator"
0091 - "phy1:green:wlan"
0092 - "phy3::wlan"
0093 - ":kbd_backlight"
0094 - "input5::kbd_backlight"
0095 - "input3::numlock"
0096 - "input3::scrolllock"
0097 - "input3::capslock"
0098 - "mmc1::status"
0099 - "white:status"
0100
0101 get_led_device_info.sh script can be used for verifying if the LED name
0102 meets the requirements pointed out here. It performs validation of the LED class
0103 devicename sections and gives hints on expected value for a section in case
0104 the validation fails for it. So far the script supports validation
0105 of associations between LEDs and following types of devices:
0106
0107 - input devices
0108 - ieee80211 compliant USB devices
0109
0110 The script is open to extensions.
0111
0112 There have been calls for LED properties such as color to be exported as
0113 individual led class attributes. As a solution which doesn't incur as much
0114 overhead, I suggest these become part of the device name. The naming scheme
0115 above leaves scope for further attributes should they be needed. If sections
0116 of the name don't apply, just leave that section blank.
0117
0118
0119 Brightness setting API
0120 ======================
0121
0122 LED subsystem core exposes following API for setting brightness:
0123
0124 - led_set_brightness:
0125 it is guaranteed not to sleep, passing LED_OFF stops
0126 blinking,
0127
0128 - led_set_brightness_sync:
0129 for use cases when immediate effect is desired -
0130 it can block the caller for the time required for accessing
0131 device registers and can sleep, passing LED_OFF stops hardware
0132 blinking, returns -EBUSY if software blink fallback is enabled.
0133
0134
0135 LED registration API
0136 ====================
0137
0138 A driver wanting to register a LED classdev for use by other drivers /
0139 userspace needs to allocate and fill a led_classdev struct and then call
0140 `[devm_]led_classdev_register`. If the non devm version is used the driver
0141 must call led_classdev_unregister from its remove function before
0142 free-ing the led_classdev struct.
0143
0144 If the driver can detect hardware initiated brightness changes and thus
0145 wants to have a brightness_hw_changed attribute then the LED_BRIGHT_HW_CHANGED
0146 flag must be set in flags before registering. Calling
0147 led_classdev_notify_brightness_hw_changed on a classdev not registered with
0148 the LED_BRIGHT_HW_CHANGED flag is a bug and will trigger a WARN_ON.
0149
0150 Hardware accelerated blink of LEDs
0151 ==================================
0152
0153 Some LEDs can be programmed to blink without any CPU interaction. To
0154 support this feature, a LED driver can optionally implement the
0155 blink_set() function (see <linux/leds.h>). To set an LED to blinking,
0156 however, it is better to use the API function led_blink_set(), as it
0157 will check and implement software fallback if necessary.
0158
0159 To turn off blinking, use the API function led_brightness_set()
0160 with brightness value LED_OFF, which should stop any software
0161 timers that may have been required for blinking.
0162
0163 The blink_set() function should choose a user friendly blinking value
0164 if it is called with `*delay_on==0` && `*delay_off==0` parameters. In this
0165 case the driver should give back the chosen value through delay_on and
0166 delay_off parameters to the leds subsystem.
0167
0168 Setting the brightness to zero with brightness_set() callback function
0169 should completely turn off the LED and cancel the previously programmed
0170 hardware blinking function, if any.
0171
0172
0173 Known Issues
0174 ============
0175
0176 The LED Trigger core cannot be a module as the simple trigger functions
0177 would cause nightmare dependency issues. I see this as a minor issue
0178 compared to the benefits the simple trigger functionality brings. The
0179 rest of the LED subsystem can be modular.