xref: /linux/Documentation/leds/leds-class.rst (revision 7d1f84f1b94e79cb95d974ab3ec54cb3a4d64f5b)
1========================
2LED handling under Linux
3========================
4
5In its simplest form, the LED class just allows control of LEDs from
6userspace. LEDs appear in /sys/class/leds/. The maximum brightness of the
7LED is defined in max_brightness file. The brightness file will set the brightness
8of the LED (taking a value 0-max_brightness). Most LEDs don't have hardware
9brightness support so will just be turned on for non-zero brightness settings.
10
11The class also introduces the optional concept of an LED trigger. A trigger
12is a kernel based source of led events. Triggers can either be simple or
13complex. A simple trigger isn't configurable and is designed to slot into
14existing subsystems with minimal additional code. Examples are the disk-activity,
15nand-disk and sharpsl-charge triggers. With led triggers disabled, the code
16optimises away.
17
18Complex triggers while available to all LEDs have LED specific
19parameters and work on a per LED basis. The timer trigger is an example.
20The timer trigger will periodically change the LED brightness between
21LED_OFF and the current brightness setting. The "on" and "off" time can
22be specified via /sys/class/leds/<device>/delay_{on,off} in milliseconds.
23You can change the brightness value of a LED independently of the timer
24trigger. However, if you set the brightness value to LED_OFF it will
25also disable the timer trigger.
26
27You can change triggers in a similar manner to the way an IO scheduler
28is chosen (via /sys/class/leds/<device>/trigger). Trigger specific
29parameters can appear in /sys/class/leds/<device> once a given trigger is
30selected.
31
32
33Design Philosophy
34=================
35
36The underlying design philosophy is simplicity. LEDs are simple devices
37and the aim is to keep a small amount of code giving as much functionality
38as possible.  Please keep this in mind when suggesting enhancements.
39
40
41LED Device Naming
42=================
43
44Is currently of the form:
45
46	"devicename:color:function"
47
48- devicename:
49        it should refer to a unique identifier created by the kernel,
50        like e.g. phyN for network devices or inputN for input devices, rather
51        than to the hardware; the information related to the product and the bus
52        to which given device is hooked is available in sysfs and can be
53        retrieved using get_led_device_info.sh script from tools/leds; generally
54        this section is expected mostly for LEDs that are somehow associated with
55        other devices.
56
57- color:
58        one of LED_COLOR_ID_* definitions from the header
59        include/dt-bindings/leds/common.h.
60
61- function:
62        one of LED_FUNCTION_* definitions from the header
63        include/dt-bindings/leds/common.h.
64
65If required color or function is missing, please submit a patch
66to linux-leds@vger.kernel.org.
67
68It is possible that more than one LED with the same color and function will
69be required for given platform, differing only with an ordinal number.
70In this case it is preferable to just concatenate the predefined LED_FUNCTION_*
71name with required "-N" suffix in the driver. fwnode based drivers can use
72function-enumerator property for that and then the concatenation will be handled
73automatically by the LED core upon LED class device registration.
74
75LED subsystem has also a protection against name clash, that may occur
76when LED class device is created by a driver of hot-pluggable device and
77it doesn't provide unique devicename section. In this case numerical
78suffix (e.g. "_1", "_2", "_3" etc.) is added to the requested LED class
79device name.
80
81There might be still LED class drivers around using vendor or product name
82for devicename, but this approach is now deprecated as it doesn't convey
83any added value. Product information can be found in other places in sysfs
84(see tools/leds/get_led_device_info.sh).
85
86Examples of proper LED names:
87
88  - "red:disk"
89  - "white:flash"
90  - "red:indicator"
91  - "phy1:green:wlan"
92  - "phy3::wlan"
93  - ":kbd_backlight"
94  - "input5::kbd_backlight"
95  - "input3::numlock"
96  - "input3::scrolllock"
97  - "input3::capslock"
98  - "mmc1::status"
99  - "white:status"
100
101get_led_device_info.sh script can be used for verifying if the LED name
102meets the requirements pointed out here. It performs validation of the LED class
103devicename sections and gives hints on expected value for a section in case
104the validation fails for it. So far the script supports validation
105of associations between LEDs and following types of devices:
106
107        - input devices
108        - ieee80211 compliant USB devices
109
110The script is open to extensions.
111
112There have been calls for LED properties such as color to be exported as
113individual led class attributes. As a solution which doesn't incur as much
114overhead, I suggest these become part of the device name. The naming scheme
115above leaves scope for further attributes should they be needed. If sections
116of the name don't apply, just leave that section blank.
117
118
119Keyboard backlight control LED Device Naming
120============================================
121
122For backlit keyboards with a single brightness / color settings a single
123(multicolor) LED class device should be used to allow userspace to change
124the backlight brightness (and if possible the color). This LED class device
125must use "kbd_backlight" for the function part of the LED class device name.
126IOW the name must end with ":kbd_backlight".
127
128For backlit keyboards with multiple control zones, one (multicolor) LED class
129device should be used per zone. These LED class devices' name must follow:
130
131	"<devicename>:<color>:kbd_zoned_backlight-<zone_name>"
132
133and <devicename> must be the same for all zones of the same keyboard.
134
135<zone_name> should be descriptive of which part of the keyboard backlight
136the zone covers and should be suitable for userspace to show to an end user
137in an UI for controlling the zones.
138
139Where possible <zone_name> should be a value already used by other
140zoned keyboards with a similar or identical zone layout, e.g.:
141
142<devicename>:<color>:kbd_zoned_backlight-right
143<devicename>:<color>:kbd_zoned_backlight-middle
144<devicename>:<color>:kbd_zoned_backlight-left
145<devicename>:<color>:kbd_zoned_backlight-corners
146<devicename>:<color>:kbd_zoned_backlight-wasd
147
148or:
149
150<devicename>:<color>:kbd_zoned_backlight-main
151<devicename>:<color>:kbd_zoned_backlight-cursor
152<devicename>:<color>:kbd_zoned_backlight-numpad
153<devicename>:<color>:kbd_zoned_backlight-corners
154<devicename>:<color>:kbd_zoned_backlight-wasd
155
156Note that this is intended for keyboards with a limited number of zones,
157keyboards with per key addressable backlighting must not use LED class devices
158since the sysfs API is not suitable for rapidly change multiple LEDs in one
159"commit" as is necessary to do animations / special effects on such keyboards.
160
161An exception to the rule that all zones must follow:
162
163	"<devicename>:<color>:kbd_zoned_backlight-<zone_name>"
164
165is made for the special case where there is a single big zone which controls
166the backlighting of almost all of the keyboard and there are some small areas
167with separate control, like just the 4 cursor keys, or the WASD keys. In this
168case the main zone should use 'kbd_backlight' for the function part of the name
169for compatibility with (older) userspace code which is not aware of
170the "kbd_zoned_backlight-<zone_name>" function naming scheme.
171
172While the smaller zones should use the new zoned naming scheme. Such a setup
173would result in e.g.:
174
175<devicename>:<color>:kbd_backlight
176<devicename>:<color>:kbd_zoned_backlight-wasd
177
178"kbd_zoned_backlight-<zone_name>" aware userspace should be aware of this
179exception and check for a main zone with a "kbd_backlight" function-name.
180
181
182Brightness setting API
183======================
184
185LED subsystem core exposes following API for setting brightness:
186
187    - led_set_brightness:
188		it is guaranteed not to sleep, passing LED_OFF stops
189		blinking,
190
191    - led_set_brightness_sync:
192		for use cases when immediate effect is desired -
193		it can block the caller for the time required for accessing
194		device registers and can sleep, passing LED_OFF stops hardware
195		blinking, returns -EBUSY if software blink fallback is enabled.
196
197
198LED registration API
199====================
200
201A driver wanting to register a LED classdev for use by other drivers /
202userspace needs to allocate and fill a led_classdev struct and then call
203`[devm_]led_classdev_register`. If the non devm version is used the driver
204must call led_classdev_unregister from its remove function before
205free-ing the led_classdev struct.
206
207If the driver can detect hardware initiated brightness changes and thus
208wants to have a brightness_hw_changed attribute then the LED_BRIGHT_HW_CHANGED
209flag must be set in flags before registering. Calling
210led_classdev_notify_brightness_hw_changed on a classdev not registered with
211the LED_BRIGHT_HW_CHANGED flag is a bug and will trigger a WARN_ON.
212
213Hardware accelerated blink of LEDs
214==================================
215
216Some LEDs can be programmed to blink without any CPU interaction. To
217support this feature, a LED driver can optionally implement the
218blink_set() function (see <linux/leds.h>). To set an LED to blinking,
219however, it is better to use the API function led_blink_set(), as it
220will check and implement software fallback if necessary.
221
222To turn off blinking, use the API function led_brightness_set()
223with brightness value LED_OFF, which should stop any software
224timers that may have been required for blinking.
225
226The blink_set() function should choose a user friendly blinking value
227if it is called with `*delay_on==0` && `*delay_off==0` parameters. In this
228case the driver should give back the chosen value through delay_on and
229delay_off parameters to the leds subsystem.
230
231Setting the brightness to zero with brightness_set() callback function
232should completely turn off the LED and cancel the previously programmed
233hardware blinking function, if any.
234
235Hardware driven LEDs
236====================
237
238Some LEDs can be programmed to be driven by hardware. This is not
239limited to blink but also to turn off or on autonomously.
240To support this feature, a LED needs to implement various additional
241ops and needs to declare specific support for the supported triggers.
242
243With hw control we refer to the LED driven by hardware.
244
245A sysfs attribute `trigger_may_offload_to_hw` is provided for userspace
246to query supported triggers and their states.
247
248LED driver must define the following value to support hw control:
249
250    - hw_control_trigger:
251               unique trigger name supported by the LED in hw control
252               mode.
253
254LED driver must implement the following API to support hw control:
255    - hw_control_is_supported:
256                check if the flags passed by the supported trigger can
257                be parsed and activate hw control on the LED.
258
259                Return 0 if the passed flags mask is supported and
260                can be set with hw_control_set().
261
262                If the passed flags mask is not supported -EOPNOTSUPP
263                must be returned, the LED trigger will use software
264                fallback in this case.
265
266                Return a negative error in case of any other error like
267                device not ready or timeouts.
268
269     - hw_control_set:
270                activate hw control. LED driver will use the provided
271                flags passed from the supported trigger, parse them to
272                a set of mode and setup the LED to be driven by hardware
273                following the requested modes.
274
275                Set LED_OFF via the brightness_set to deactivate hw control.
276
277                Return 0 on success, a negative error number on failing to
278                apply flags.
279
280    - hw_control_get:
281                get active modes from a LED already in hw control, parse
282                them and set in flags the current active flags for the
283                supported trigger.
284
285                Return 0 on success, a negative error number on failing
286                parsing the initial mode.
287                Error from this function is NOT FATAL as the device may
288                be in a not supported initial state by the attached LED
289                trigger.
290
291    - hw_control_get_device:
292                return the device associated with the LED driver in
293                hw control. A trigger might use this to match the
294                returned device from this function with a configured
295                device for the trigger as the source for blinking
296                events and correctly enable hw control.
297                (example a netdev trigger configured to blink for a
298                particular dev match the returned dev from get_device
299                to set hw control)
300
301                Returns a pointer to a struct device or NULL if nothing
302                is currently attached.
303
304LED trigger should implement the following API to indicate hw control:
305    - hw_offloaded:
306                return a boolean indicating if the trigger is currently
307                offloaded to hardware.
308
309                If a trigger doesn't implement this callback, the default
310                value will be true for private triggers and false for generic
311                ones.
312
313LED driver can activate additional modes by default to workaround the
314impossibility of supporting each different mode on the supported trigger.
315Examples are hardcoding the blink speed to a set interval, enable special
316feature like bypassing blink if some requirements are not met.
317
318A trigger should first check if the hw control API are supported by the LED
319driver and check if the trigger is supported to verify if hw control is possible,
320use hw_control_is_supported to check if the flags are supported and only at
321the end use hw_control_set to activate hw control.
322
323A trigger can use hw_control_get to check if a LED is already in hw control
324and init their flags.
325
326Alternatively, a private trigger can be implemented along with the LED driver if
327the LED's hardware control doesn't fit any generic trigger. To associate the
328private trigger with the LED classdev, their `trigger_type` must be the same. To
329declare that the private trigger provides hardware control for the associated
330LED classdev, set the `hw_control_trigger` string to the trigger's name. Since
331both the LED classdev and the private trigger are in the same LED driver, it's
332not necessary for them to coordinate via `hw_control_*` callbacks.
333
334When the LED is in hw control, no software blink is possible and doing so
335will effectively disable hw control.
336
337Hardware-initiated trigger transition
338=====================================
339
340Some hardware can autonomously activate/deactivate hardware control. After that,
341the LED hardware notifies the LED driver.
342
343If the driver can detect such transitions and thus wants to notify the LED core
344to update the current trigger then the `LED_TRIG_HW_CHANGED` flag must be set in
345flags before registering. To update the current trigger accordingly, call
346`led_trigger_notify_hw_control_changed` on the LED classdev.
347
348This capability is restricted to the LED device's private trigger. The private
349trigger must have been properly registered (see above) and named after
350`hw_control_trigger`.
351
352Only two transitions are defined:
353
354- "none" => private trigger:
355        This happens when the hardware autonomously activates hardware control
356        and when "none" (i.e., no trigger) is currently active. If the private
357        trigger is already active when the method is called, this is essentially
358        a no-op.
359
360        The activation sequence for the private trigger will be executed as
361        normal.
362
363        The LED driver and its private trigger must be able to handle the
364        activation sequence even if the hardware is currently in hardware
365        control.
366
367        If error occurs in the activation sequence, the LED Trigger core reverts
368        the effective trigger to "none".
369
370- private trigger => "none"
371        This happens when the hardware autonomously deactivates hardware control
372        and when the private trigger is currently active. If "none" (i.e., no
373        trigger) is active when the method is called, this is essentially a
374        no-op.
375
376        The deactivation sequence for the private trigger will be executed as
377        normal, except that the current LED brightness is retained. The reason
378        for keeping the brightness unchanged is that some hardware may choose a
379        specific brightness instead of simply turning off the LED after
380        autonomously deactivating hardware control.
381
382        The LED driver and its private trigger must be able to handle the
383        deactivation sequence even if the hardware is not currently in hardware
384        control.
385
386If the current trigger is neither the private trigger nor "none", no transition
387will be made.
388
389Known Issues
390============
391
392The LED Trigger core cannot be a module as the simple trigger functions
393would cause nightmare dependency issues. I see this as a minor issue
394compared to the benefits the simple trigger functionality brings. The
395rest of the LED subsystem can be modular.
396