18dab9197SMauro Carvalho Chehab======================== 28dab9197SMauro Carvalho ChehabLED handling under Linux 38dab9197SMauro Carvalho Chehab======================== 48dab9197SMauro Carvalho Chehab 58dab9197SMauro Carvalho ChehabIn its simplest form, the LED class just allows control of LEDs from 68dab9197SMauro Carvalho Chehabuserspace. LEDs appear in /sys/class/leds/. The maximum brightness of the 78dab9197SMauro Carvalho ChehabLED is defined in max_brightness file. The brightness file will set the brightness 88dab9197SMauro Carvalho Chehabof the LED (taking a value 0-max_brightness). Most LEDs don't have hardware 98dab9197SMauro Carvalho Chehabbrightness support so will just be turned on for non-zero brightness settings. 108dab9197SMauro Carvalho Chehab 118dab9197SMauro Carvalho ChehabThe class also introduces the optional concept of an LED trigger. A trigger 128dab9197SMauro Carvalho Chehabis a kernel based source of led events. Triggers can either be simple or 138dab9197SMauro Carvalho Chehabcomplex. A simple trigger isn't configurable and is designed to slot into 148dab9197SMauro Carvalho Chehabexisting subsystems with minimal additional code. Examples are the disk-activity, 158dab9197SMauro Carvalho Chehabnand-disk and sharpsl-charge triggers. With led triggers disabled, the code 168dab9197SMauro Carvalho Chehaboptimises away. 178dab9197SMauro Carvalho Chehab 188dab9197SMauro Carvalho ChehabComplex triggers while available to all LEDs have LED specific 198dab9197SMauro Carvalho Chehabparameters and work on a per LED basis. The timer trigger is an example. 208dab9197SMauro Carvalho ChehabThe timer trigger will periodically change the LED brightness between 218dab9197SMauro Carvalho ChehabLED_OFF and the current brightness setting. The "on" and "off" time can 228dab9197SMauro Carvalho Chehabbe specified via /sys/class/leds/<device>/delay_{on,off} in milliseconds. 238dab9197SMauro Carvalho ChehabYou can change the brightness value of a LED independently of the timer 248dab9197SMauro Carvalho Chehabtrigger. However, if you set the brightness value to LED_OFF it will 258dab9197SMauro Carvalho Chehabalso disable the timer trigger. 268dab9197SMauro Carvalho Chehab 278dab9197SMauro Carvalho ChehabYou can change triggers in a similar manner to the way an IO scheduler 288dab9197SMauro Carvalho Chehabis chosen (via /sys/class/leds/<device>/trigger). Trigger specific 298dab9197SMauro Carvalho Chehabparameters can appear in /sys/class/leds/<device> once a given trigger is 308dab9197SMauro Carvalho Chehabselected. 318dab9197SMauro Carvalho Chehab 328dab9197SMauro Carvalho Chehab 338dab9197SMauro Carvalho ChehabDesign Philosophy 348dab9197SMauro Carvalho Chehab================= 358dab9197SMauro Carvalho Chehab 368dab9197SMauro Carvalho ChehabThe underlying design philosophy is simplicity. LEDs are simple devices 378dab9197SMauro Carvalho Chehaband the aim is to keep a small amount of code giving as much functionality 388dab9197SMauro Carvalho Chehabas possible. Please keep this in mind when suggesting enhancements. 398dab9197SMauro Carvalho Chehab 408dab9197SMauro Carvalho Chehab 418dab9197SMauro Carvalho ChehabLED Device Naming 428dab9197SMauro Carvalho Chehab================= 438dab9197SMauro Carvalho Chehab 448dab9197SMauro Carvalho ChehabIs currently of the form: 458dab9197SMauro Carvalho Chehab 46bb4e9af0SJacek Anaszewski "devicename:color:function" 478dab9197SMauro Carvalho Chehab 48bb4e9af0SJacek Anaszewski- devicename: 49bb4e9af0SJacek Anaszewski it should refer to a unique identifier created by the kernel, 50bb4e9af0SJacek Anaszewski like e.g. phyN for network devices or inputN for input devices, rather 51bb4e9af0SJacek Anaszewski than to the hardware; the information related to the product and the bus 52bb4e9af0SJacek Anaszewski to which given device is hooked is available in sysfs and can be 53bb4e9af0SJacek Anaszewski retrieved using get_led_device_info.sh script from tools/leds; generally 54bb4e9af0SJacek Anaszewski this section is expected mostly for LEDs that are somehow associated with 55bb4e9af0SJacek Anaszewski other devices. 56bb4e9af0SJacek Anaszewski 57bb4e9af0SJacek Anaszewski- color: 58bb4e9af0SJacek Anaszewski one of LED_COLOR_ID_* definitions from the header 59bb4e9af0SJacek Anaszewski include/dt-bindings/leds/common.h. 60bb4e9af0SJacek Anaszewski 61bb4e9af0SJacek Anaszewski- function: 62bb4e9af0SJacek Anaszewski one of LED_FUNCTION_* definitions from the header 63bb4e9af0SJacek Anaszewski include/dt-bindings/leds/common.h. 64bb4e9af0SJacek Anaszewski 65bb4e9af0SJacek AnaszewskiIf required color or function is missing, please submit a patch 66bb4e9af0SJacek Anaszewskito linux-leds@vger.kernel.org. 67bb4e9af0SJacek Anaszewski 68bb4e9af0SJacek AnaszewskiIt is possible that more than one LED with the same color and function will 69bb4e9af0SJacek Anaszewskibe required for given platform, differing only with an ordinal number. 70bb4e9af0SJacek AnaszewskiIn this case it is preferable to just concatenate the predefined LED_FUNCTION_* 71bb4e9af0SJacek Anaszewskiname with required "-N" suffix in the driver. fwnode based drivers can use 72bb4e9af0SJacek Anaszewskifunction-enumerator property for that and then the concatenation will be handled 73bb4e9af0SJacek Anaszewskiautomatically by the LED core upon LED class device registration. 74bb4e9af0SJacek Anaszewski 75bb4e9af0SJacek AnaszewskiLED subsystem has also a protection against name clash, that may occur 76bb4e9af0SJacek Anaszewskiwhen LED class device is created by a driver of hot-pluggable device and 77bb4e9af0SJacek Anaszewskiit doesn't provide unique devicename section. In this case numerical 78bb4e9af0SJacek Anaszewskisuffix (e.g. "_1", "_2", "_3" etc.) is added to the requested LED class 79bb4e9af0SJacek Anaszewskidevice name. 80bb4e9af0SJacek Anaszewski 81bb4e9af0SJacek AnaszewskiThere might be still LED class drivers around using vendor or product name 82bb4e9af0SJacek Anaszewskifor devicename, but this approach is now deprecated as it doesn't convey 83bb4e9af0SJacek Anaszewskiany added value. Product information can be found in other places in sysfs 84bb4e9af0SJacek Anaszewski(see tools/leds/get_led_device_info.sh). 85bb4e9af0SJacek Anaszewski 86bb4e9af0SJacek AnaszewskiExamples of proper LED names: 87bb4e9af0SJacek Anaszewski 88bb4e9af0SJacek Anaszewski - "red:disk" 89bb4e9af0SJacek Anaszewski - "white:flash" 90bb4e9af0SJacek Anaszewski - "red:indicator" 91bb4e9af0SJacek Anaszewski - "phy1:green:wlan" 92bb4e9af0SJacek Anaszewski - "phy3::wlan" 93bb4e9af0SJacek Anaszewski - ":kbd_backlight" 94bb4e9af0SJacek Anaszewski - "input5::kbd_backlight" 95bb4e9af0SJacek Anaszewski - "input3::numlock" 96bb4e9af0SJacek Anaszewski - "input3::scrolllock" 97bb4e9af0SJacek Anaszewski - "input3::capslock" 98bb4e9af0SJacek Anaszewski - "mmc1::status" 99bb4e9af0SJacek Anaszewski - "white:status" 100bb4e9af0SJacek Anaszewski 101bb4e9af0SJacek Anaszewskiget_led_device_info.sh script can be used for verifying if the LED name 102bb4e9af0SJacek Anaszewskimeets the requirements pointed out here. It performs validation of the LED class 103bb4e9af0SJacek Anaszewskidevicename sections and gives hints on expected value for a section in case 104bb4e9af0SJacek Anaszewskithe validation fails for it. So far the script supports validation 105bb4e9af0SJacek Anaszewskiof associations between LEDs and following types of devices: 106bb4e9af0SJacek Anaszewski 107bb4e9af0SJacek Anaszewski - input devices 108bb4e9af0SJacek Anaszewski - ieee80211 compliant USB devices 109bb4e9af0SJacek Anaszewski 110bb4e9af0SJacek AnaszewskiThe script is open to extensions. 111bb4e9af0SJacek Anaszewski 112bb4e9af0SJacek AnaszewskiThere have been calls for LED properties such as color to be exported as 1138dab9197SMauro Carvalho Chehabindividual led class attributes. As a solution which doesn't incur as much 1148dab9197SMauro Carvalho Chehaboverhead, I suggest these become part of the device name. The naming scheme 1158dab9197SMauro Carvalho Chehababove leaves scope for further attributes should they be needed. If sections 1168dab9197SMauro Carvalho Chehabof the name don't apply, just leave that section blank. 1178dab9197SMauro Carvalho Chehab 1188dab9197SMauro Carvalho Chehab 1198dab9197SMauro Carvalho ChehabBrightness setting API 1208dab9197SMauro Carvalho Chehab====================== 1218dab9197SMauro Carvalho Chehab 1228dab9197SMauro Carvalho ChehabLED subsystem core exposes following API for setting brightness: 1238dab9197SMauro Carvalho Chehab 1248dab9197SMauro Carvalho Chehab - led_set_brightness: 1258dab9197SMauro Carvalho Chehab it is guaranteed not to sleep, passing LED_OFF stops 1268dab9197SMauro Carvalho Chehab blinking, 1278dab9197SMauro Carvalho Chehab 1288dab9197SMauro Carvalho Chehab - led_set_brightness_sync: 1298dab9197SMauro Carvalho Chehab for use cases when immediate effect is desired - 1308dab9197SMauro Carvalho Chehab it can block the caller for the time required for accessing 1318dab9197SMauro Carvalho Chehab device registers and can sleep, passing LED_OFF stops hardware 1328dab9197SMauro Carvalho Chehab blinking, returns -EBUSY if software blink fallback is enabled. 1338dab9197SMauro Carvalho Chehab 1348dab9197SMauro Carvalho Chehab 1358dab9197SMauro Carvalho ChehabLED registration API 1368dab9197SMauro Carvalho Chehab==================== 1378dab9197SMauro Carvalho Chehab 1388dab9197SMauro Carvalho ChehabA driver wanting to register a LED classdev for use by other drivers / 1398dab9197SMauro Carvalho Chehabuserspace needs to allocate and fill a led_classdev struct and then call 1408dab9197SMauro Carvalho Chehab`[devm_]led_classdev_register`. If the non devm version is used the driver 1418dab9197SMauro Carvalho Chehabmust call led_classdev_unregister from its remove function before 1428dab9197SMauro Carvalho Chehabfree-ing the led_classdev struct. 1438dab9197SMauro Carvalho Chehab 1448dab9197SMauro Carvalho ChehabIf the driver can detect hardware initiated brightness changes and thus 1458dab9197SMauro Carvalho Chehabwants to have a brightness_hw_changed attribute then the LED_BRIGHT_HW_CHANGED 1468dab9197SMauro Carvalho Chehabflag must be set in flags before registering. Calling 1478dab9197SMauro Carvalho Chehabled_classdev_notify_brightness_hw_changed on a classdev not registered with 1488dab9197SMauro Carvalho Chehabthe LED_BRIGHT_HW_CHANGED flag is a bug and will trigger a WARN_ON. 1498dab9197SMauro Carvalho Chehab 1508dab9197SMauro Carvalho ChehabHardware accelerated blink of LEDs 1518dab9197SMauro Carvalho Chehab================================== 1528dab9197SMauro Carvalho Chehab 1538dab9197SMauro Carvalho ChehabSome LEDs can be programmed to blink without any CPU interaction. To 1548dab9197SMauro Carvalho Chehabsupport this feature, a LED driver can optionally implement the 1558dab9197SMauro Carvalho Chehabblink_set() function (see <linux/leds.h>). To set an LED to blinking, 1568dab9197SMauro Carvalho Chehabhowever, it is better to use the API function led_blink_set(), as it 1578dab9197SMauro Carvalho Chehabwill check and implement software fallback if necessary. 1588dab9197SMauro Carvalho Chehab 1598dab9197SMauro Carvalho ChehabTo turn off blinking, use the API function led_brightness_set() 1608dab9197SMauro Carvalho Chehabwith brightness value LED_OFF, which should stop any software 1618dab9197SMauro Carvalho Chehabtimers that may have been required for blinking. 1628dab9197SMauro Carvalho Chehab 1638dab9197SMauro Carvalho ChehabThe blink_set() function should choose a user friendly blinking value 1648dab9197SMauro Carvalho Chehabif it is called with `*delay_on==0` && `*delay_off==0` parameters. In this 1658dab9197SMauro Carvalho Chehabcase the driver should give back the chosen value through delay_on and 1668dab9197SMauro Carvalho Chehabdelay_off parameters to the leds subsystem. 1678dab9197SMauro Carvalho Chehab 1688dab9197SMauro Carvalho ChehabSetting the brightness to zero with brightness_set() callback function 1698dab9197SMauro Carvalho Chehabshould completely turn off the LED and cancel the previously programmed 1708dab9197SMauro Carvalho Chehabhardware blinking function, if any. 1718dab9197SMauro Carvalho Chehab 172*8aa2fd7bSChristian MarangiHardware driven LEDs 173*8aa2fd7bSChristian Marangi==================== 174*8aa2fd7bSChristian Marangi 175*8aa2fd7bSChristian MarangiSome LEDs can be programmed to be driven by hardware. This is not 176*8aa2fd7bSChristian Marangilimited to blink but also to turn off or on autonomously. 177*8aa2fd7bSChristian MarangiTo support this feature, a LED needs to implement various additional 178*8aa2fd7bSChristian Marangiops and needs to declare specific support for the supported triggers. 179*8aa2fd7bSChristian Marangi 180*8aa2fd7bSChristian MarangiWith hw control we refer to the LED driven by hardware. 181*8aa2fd7bSChristian Marangi 182*8aa2fd7bSChristian MarangiLED driver must define the following value to support hw control: 183*8aa2fd7bSChristian Marangi 184*8aa2fd7bSChristian Marangi - hw_control_trigger: 185*8aa2fd7bSChristian Marangi unique trigger name supported by the LED in hw control 186*8aa2fd7bSChristian Marangi mode. 187*8aa2fd7bSChristian Marangi 188*8aa2fd7bSChristian MarangiLED driver must implement the following API to support hw control: 189*8aa2fd7bSChristian Marangi - hw_control_is_supported: 190*8aa2fd7bSChristian Marangi check if the flags passed by the supported trigger can 191*8aa2fd7bSChristian Marangi be parsed and activate hw control on the LED. 192*8aa2fd7bSChristian Marangi 193*8aa2fd7bSChristian Marangi Return 0 if the passed flags mask is supported and 194*8aa2fd7bSChristian Marangi can be set with hw_control_set(). 195*8aa2fd7bSChristian Marangi 196*8aa2fd7bSChristian Marangi If the passed flags mask is not supported -EOPNOTSUPP 197*8aa2fd7bSChristian Marangi must be returned, the LED trigger will use software 198*8aa2fd7bSChristian Marangi fallback in this case. 199*8aa2fd7bSChristian Marangi 200*8aa2fd7bSChristian Marangi Return a negative error in case of any other error like 201*8aa2fd7bSChristian Marangi device not ready or timeouts. 202*8aa2fd7bSChristian Marangi 203*8aa2fd7bSChristian Marangi - hw_control_set: 204*8aa2fd7bSChristian Marangi activate hw control. LED driver will use the provided 205*8aa2fd7bSChristian Marangi flags passed from the supported trigger, parse them to 206*8aa2fd7bSChristian Marangi a set of mode and setup the LED to be driven by hardware 207*8aa2fd7bSChristian Marangi following the requested modes. 208*8aa2fd7bSChristian Marangi 209*8aa2fd7bSChristian Marangi Set LED_OFF via the brightness_set to deactivate hw control. 210*8aa2fd7bSChristian Marangi 211*8aa2fd7bSChristian Marangi Return 0 on success, a negative error number on failing to 212*8aa2fd7bSChristian Marangi apply flags. 213*8aa2fd7bSChristian Marangi 214*8aa2fd7bSChristian Marangi - hw_control_get: 215*8aa2fd7bSChristian Marangi get active modes from a LED already in hw control, parse 216*8aa2fd7bSChristian Marangi them and set in flags the current active flags for the 217*8aa2fd7bSChristian Marangi supported trigger. 218*8aa2fd7bSChristian Marangi 219*8aa2fd7bSChristian Marangi Return 0 on success, a negative error number on failing 220*8aa2fd7bSChristian Marangi parsing the initial mode. 221*8aa2fd7bSChristian Marangi Error from this function is NOT FATAL as the device may 222*8aa2fd7bSChristian Marangi be in a not supported initial state by the attached LED 223*8aa2fd7bSChristian Marangi trigger. 224*8aa2fd7bSChristian Marangi 225*8aa2fd7bSChristian Marangi - hw_control_get_device: 226*8aa2fd7bSChristian Marangi return the device associated with the LED driver in 227*8aa2fd7bSChristian Marangi hw control. A trigger might use this to match the 228*8aa2fd7bSChristian Marangi returned device from this function with a configured 229*8aa2fd7bSChristian Marangi device for the trigger as the source for blinking 230*8aa2fd7bSChristian Marangi events and correctly enable hw control. 231*8aa2fd7bSChristian Marangi (example a netdev trigger configured to blink for a 232*8aa2fd7bSChristian Marangi particular dev match the returned dev from get_device 233*8aa2fd7bSChristian Marangi to set hw control) 234*8aa2fd7bSChristian Marangi 235*8aa2fd7bSChristian Marangi Returns a pointer to a struct device or NULL if nothing 236*8aa2fd7bSChristian Marangi is currently attached. 237*8aa2fd7bSChristian Marangi 238*8aa2fd7bSChristian MarangiLED driver can activate additional modes by default to workaround the 239*8aa2fd7bSChristian Marangiimpossibility of supporting each different mode on the supported trigger. 240*8aa2fd7bSChristian MarangiExamples are hardcoding the blink speed to a set interval, enable special 241*8aa2fd7bSChristian Marangifeature like bypassing blink if some requirements are not met. 242*8aa2fd7bSChristian Marangi 243*8aa2fd7bSChristian MarangiA trigger should first check if the hw control API are supported by the LED 244*8aa2fd7bSChristian Marangidriver and check if the trigger is supported to verify if hw control is possible, 245*8aa2fd7bSChristian Marangiuse hw_control_is_supported to check if the flags are supported and only at 246*8aa2fd7bSChristian Marangithe end use hw_control_set to activate hw control. 247*8aa2fd7bSChristian Marangi 248*8aa2fd7bSChristian MarangiA trigger can use hw_control_get to check if a LED is already in hw control 249*8aa2fd7bSChristian Marangiand init their flags. 250*8aa2fd7bSChristian Marangi 251*8aa2fd7bSChristian MarangiWhen the LED is in hw control, no software blink is possible and doing so 252*8aa2fd7bSChristian Marangiwill effectively disable hw control. 2538dab9197SMauro Carvalho Chehab 2548dab9197SMauro Carvalho ChehabKnown Issues 2558dab9197SMauro Carvalho Chehab============ 2568dab9197SMauro Carvalho Chehab 2578dab9197SMauro Carvalho ChehabThe LED Trigger core cannot be a module as the simple trigger functions 2588dab9197SMauro Carvalho Chehabwould cause nightmare dependency issues. I see this as a minor issue 2598dab9197SMauro Carvalho Chehabcompared to the benefits the simple trigger functionality brings. The 2608dab9197SMauro Carvalho Chehabrest of the LED subsystem can be modular. 261