leds-class.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. Hardware driven LEDs
  134. ====================
  135. Some LEDs can be programmed to be driven by hardware. This is not
  136. limited to blink but also to turn off or on autonomously.
  137. To support this feature, a LED needs to implement various additional
  138. ops and needs to declare specific support for the supported triggers.
  139. With hw control we refer to the LED driven by hardware.
  140. LED driver must define the following value to support hw control:
  141. - hw_control_trigger:
  142. unique trigger name supported by the LED in hw control
  143. mode.
  144. LED driver must implement the following API to support hw control:
  145. - hw_control_is_supported:
  146. check if the flags passed by the supported trigger can
  147. be parsed and activate hw control on the LED.
  148. Return 0 if the passed flags mask is supported and
  149. can be set with hw_control_set().
  150. If the passed flags mask is not supported -EOPNOTSUPP
  151. must be returned, the LED trigger will use software
  152. fallback in this case.
  153. Return a negative error in case of any other error like
  154. device not ready or timeouts.
  155. - hw_control_set:
  156. activate hw control. LED driver will use the provided
  157. flags passed from the supported trigger, parse them to
  158. a set of mode and setup the LED to be driven by hardware
  159. following the requested modes.
  160. Set LED_OFF via the brightness_set to deactivate hw control.
  161. Return 0 on success, a negative error number on failing to
  162. apply flags.
  163. - hw_control_get:
  164. get active modes from a LED already in hw control, parse
  165. them and set in flags the current active flags for the
  166. supported trigger.
  167. Return 0 on success, a negative error number on failing
  168. parsing the initial mode.
  169. Error from this function is NOT FATAL as the device may
  170. be in a not supported initial state by the attached LED
  171. trigger.
  172. - hw_control_get_device:
  173. return the device associated with the LED driver in
  174. hw control. A trigger might use this to match the
  175. returned device from this function with a configured
  176. device for the trigger as the source for blinking
  177. events and correctly enable hw control.
  178. (example a netdev trigger configured to blink for a
  179. particular dev match the returned dev from get_device
  180. to set hw control)
  181. Returns a pointer to a struct device or NULL if nothing
  182. is currently attached.
  183. LED driver can activate additional modes by default to workaround the
  184. impossibility of supporting each different mode on the supported trigger.
  185. Examples are hardcoding the blink speed to a set interval, enable special
  186. feature like bypassing blink if some requirements are not met.
  187. A trigger should first check if the hw control API are supported by the LED
  188. driver and check if the trigger is supported to verify if hw control is possible,
  189. use hw_control_is_supported to check if the flags are supported and only at
  190. the end use hw_control_set to activate hw control.
  191. A trigger can use hw_control_get to check if a LED is already in hw control
  192. and init their flags.
  193. When the LED is in hw control, no software blink is possible and doing so
  194. will effectively disable hw control.
  195. Known Issues
  196. ============
  197. The LED Trigger core cannot be a module as the simple trigger functions
  198. would cause nightmare dependency issues. I see this as a minor issue
  199. compared to the benefits the simple trigger functionality brings. The
  200. rest of the LED subsystem can be modular.