leds-class.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ========================
  2. LED handling under Linux
  3. ========================
  4. In its simplest form, the LED class just allows control of LEDs from
  5. userspace. LEDs appear in /sys/class/leds/. The maximum brightness of the
  6. LED is defined in max_brightness file. The brightness file will set the brightness
  7. of the LED (taking a value 0-max_brightness). Most LEDs don't have hardware
  8. brightness support so will just be turned on for non-zero brightness settings.
  9. The class also introduces the optional concept of an LED trigger. A trigger
  10. is a kernel based source of led events. Triggers can either be simple or
  11. complex. A simple trigger isn't configurable and is designed to slot into
  12. existing subsystems with minimal additional code. Examples are the disk-activity,
  13. nand-disk and sharpsl-charge triggers. With led triggers disabled, the code
  14. optimises away.
  15. Complex triggers while available to all LEDs have LED specific
  16. parameters and work on a per LED basis. The timer trigger is an example.
  17. The timer trigger will periodically change the LED brightness between
  18. LED_OFF and the current brightness setting. The "on" and "off" time can
  19. be specified via /sys/class/leds/<device>/delay_{on,off} in milliseconds.
  20. You can change the brightness value of a LED independently of the timer
  21. trigger. However, if you set the brightness value to LED_OFF it will
  22. also disable the timer trigger.
  23. You can change triggers in a similar manner to the way an IO scheduler
  24. is chosen (via /sys/class/leds/<device>/trigger). Trigger specific
  25. parameters can appear in /sys/class/leds/<device> once a given trigger is
  26. selected.
  27. Design Philosophy
  28. =================
  29. The underlying design philosophy is simplicity. LEDs are simple devices
  30. and the aim is to keep a small amount of code giving as much functionality
  31. as possible. Please keep this in mind when suggesting enhancements.
  32. LED Device Naming
  33. =================
  34. Is currently of the form:
  35. "devicename:color:function"
  36. - devicename:
  37. it should refer to a unique identifier created by the kernel,
  38. like e.g. phyN for network devices or inputN for input devices, rather
  39. than to the hardware; the information related to the product and the bus
  40. to which given device is hooked is available in sysfs and can be
  41. retrieved using get_led_device_info.sh script from tools/leds; generally
  42. this section is expected mostly for LEDs that are somehow associated with
  43. other devices.
  44. - color:
  45. one of LED_COLOR_ID_* definitions from the header
  46. include/dt-bindings/leds/common.h.
  47. - function:
  48. one of LED_FUNCTION_* definitions from the header
  49. include/dt-bindings/leds/common.h.
  50. If required color or function is missing, please submit a patch
  51. to linux-leds@vger.kernel.org.
  52. It is possible that more than one LED with the same color and function will
  53. be required for given platform, differing only with an ordinal number.
  54. In this case it is preferable to just concatenate the predefined LED_FUNCTION_*
  55. name with required "-N" suffix in the driver. fwnode based drivers can use
  56. function-enumerator property for that and then the concatenation will be handled
  57. automatically by the LED core upon LED class device registration.
  58. LED subsystem has also a protection against name clash, that may occur
  59. when LED class device is created by a driver of hot-pluggable device and
  60. it doesn't provide unique devicename section. In this case numerical
  61. suffix (e.g. "_1", "_2", "_3" etc.) is added to the requested LED class
  62. device name.
  63. There might be still LED class drivers around using vendor or product name
  64. for devicename, but this approach is now deprecated as it doesn't convey
  65. any added value. Product information can be found in other places in sysfs
  66. (see tools/leds/get_led_device_info.sh).
  67. Examples of proper LED names:
  68. - "red:disk"
  69. - "white:flash"
  70. - "red:indicator"
  71. - "phy1:green:wlan"
  72. - "phy3::wlan"
  73. - ":kbd_backlight"
  74. - "input5::kbd_backlight"
  75. - "input3::numlock"
  76. - "input3::scrolllock"
  77. - "input3::capslock"
  78. - "mmc1::status"
  79. - "white:status"
  80. get_led_device_info.sh script can be used for verifying if the LED name
  81. meets the requirements pointed out here. It performs validation of the LED class
  82. devicename sections and gives hints on expected value for a section in case
  83. the validation fails for it. So far the script supports validation
  84. of associations between LEDs and following types of devices:
  85. - input devices
  86. - ieee80211 compliant USB devices
  87. The script is open to extensions.
  88. There have been calls for LED properties such as color to be exported as
  89. individual led class attributes. As a solution which doesn't incur as much
  90. overhead, I suggest these become part of the device name. The naming scheme
  91. above leaves scope for further attributes should they be needed. If sections
  92. of the name don't apply, just leave that section blank.
  93. Brightness setting API
  94. ======================
  95. LED subsystem core exposes following API for setting brightness:
  96. - led_set_brightness:
  97. it is guaranteed not to sleep, passing LED_OFF stops
  98. blinking,
  99. - led_set_brightness_sync:
  100. for use cases when immediate effect is desired -
  101. it can block the caller for the time required for accessing
  102. device registers and can sleep, passing LED_OFF stops hardware
  103. blinking, returns -EBUSY if software blink fallback is enabled.
  104. LED registration API
  105. ====================
  106. A driver wanting to register a LED classdev for use by other drivers /
  107. userspace needs to allocate and fill a led_classdev struct and then call
  108. `[devm_]led_classdev_register`. If the non devm version is used the driver
  109. must call led_classdev_unregister from its remove function before
  110. free-ing the led_classdev struct.
  111. If the driver can detect hardware initiated brightness changes and thus
  112. wants to have a brightness_hw_changed attribute then the LED_BRIGHT_HW_CHANGED
  113. flag must be set in flags before registering. Calling
  114. led_classdev_notify_brightness_hw_changed on a classdev not registered with
  115. the LED_BRIGHT_HW_CHANGED flag is a bug and will trigger a WARN_ON.
  116. Hardware accelerated blink of LEDs
  117. ==================================
  118. Some LEDs can be programmed to blink without any CPU interaction. To
  119. support this feature, a LED driver can optionally implement the
  120. blink_set() function (see <linux/leds.h>). To set an LED to blinking,
  121. however, it is better to use the API function led_blink_set(), as it
  122. will check and implement software fallback if necessary.
  123. To turn off blinking, use the API function led_brightness_set()
  124. with brightness value LED_OFF, which should stop any software
  125. timers that may have been required for blinking.
  126. The blink_set() function should choose a user friendly blinking value
  127. if it is called with `*delay_on==0` && `*delay_off==0` parameters. In this
  128. case the driver should give back the chosen value through delay_on and
  129. delay_off parameters to the leds subsystem.
  130. Setting the brightness to zero with brightness_set() callback function
  131. should completely turn off the LED and cancel the previously programmed
  132. hardware blinking function, if any.
  133. Known Issues
  134. ============
  135. The LED Trigger core cannot be a module as the simple trigger functions
  136. would cause nightmare dependency issues. I see this as a minor issue
  137. compared to the benefits the simple trigger functionality brings. The
  138. rest of the LED subsystem can be modular.
  139. Future Development
  140. ==================
  141. At the moment, a trigger can't be created specifically for a single LED.
  142. There are a number of cases where a trigger might only be mappable to a
  143. particular LED (ACPI?). The addition of triggers provided by the LED driver
  144. should cover this option and be possible to add without breaking the
  145. current interface.