1*4882a593Smuzhiyun======================== 2*4882a593SmuzhiyunLED handling under Linux 3*4882a593Smuzhiyun======================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunIn its simplest form, the LED class just allows control of LEDs from 6*4882a593Smuzhiyunuserspace. LEDs appear in /sys/class/leds/. The maximum brightness of the 7*4882a593SmuzhiyunLED is defined in max_brightness file. The brightness file will set the brightness 8*4882a593Smuzhiyunof the LED (taking a value 0-max_brightness). Most LEDs don't have hardware 9*4882a593Smuzhiyunbrightness support so will just be turned on for non-zero brightness settings. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunThe class also introduces the optional concept of an LED trigger. A trigger 12*4882a593Smuzhiyunis a kernel based source of led events. Triggers can either be simple or 13*4882a593Smuzhiyuncomplex. A simple trigger isn't configurable and is designed to slot into 14*4882a593Smuzhiyunexisting subsystems with minimal additional code. Examples are the disk-activity, 15*4882a593Smuzhiyunnand-disk and sharpsl-charge triggers. With led triggers disabled, the code 16*4882a593Smuzhiyunoptimises away. 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunComplex triggers while available to all LEDs have LED specific 19*4882a593Smuzhiyunparameters and work on a per LED basis. The timer trigger is an example. 20*4882a593SmuzhiyunThe timer trigger will periodically change the LED brightness between 21*4882a593SmuzhiyunLED_OFF and the current brightness setting. The "on" and "off" time can 22*4882a593Smuzhiyunbe specified via /sys/class/leds/<device>/delay_{on,off} in milliseconds. 23*4882a593SmuzhiyunYou can change the brightness value of a LED independently of the timer 24*4882a593Smuzhiyuntrigger. However, if you set the brightness value to LED_OFF it will 25*4882a593Smuzhiyunalso disable the timer trigger. 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunYou can change triggers in a similar manner to the way an IO scheduler 28*4882a593Smuzhiyunis chosen (via /sys/class/leds/<device>/trigger). Trigger specific 29*4882a593Smuzhiyunparameters can appear in /sys/class/leds/<device> once a given trigger is 30*4882a593Smuzhiyunselected. 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun 33*4882a593SmuzhiyunDesign Philosophy 34*4882a593Smuzhiyun================= 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunThe underlying design philosophy is simplicity. LEDs are simple devices 37*4882a593Smuzhiyunand the aim is to keep a small amount of code giving as much functionality 38*4882a593Smuzhiyunas possible. Please keep this in mind when suggesting enhancements. 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun 41*4882a593SmuzhiyunLED Device Naming 42*4882a593Smuzhiyun================= 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunIs currently of the form: 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun "devicename:color:function" 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun- devicename: 49*4882a593Smuzhiyun it should refer to a unique identifier created by the kernel, 50*4882a593Smuzhiyun like e.g. phyN for network devices or inputN for input devices, rather 51*4882a593Smuzhiyun than to the hardware; the information related to the product and the bus 52*4882a593Smuzhiyun to which given device is hooked is available in sysfs and can be 53*4882a593Smuzhiyun retrieved using get_led_device_info.sh script from tools/leds; generally 54*4882a593Smuzhiyun this section is expected mostly for LEDs that are somehow associated with 55*4882a593Smuzhiyun other devices. 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun- color: 58*4882a593Smuzhiyun one of LED_COLOR_ID_* definitions from the header 59*4882a593Smuzhiyun include/dt-bindings/leds/common.h. 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun- function: 62*4882a593Smuzhiyun one of LED_FUNCTION_* definitions from the header 63*4882a593Smuzhiyun include/dt-bindings/leds/common.h. 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunIf required color or function is missing, please submit a patch 66*4882a593Smuzhiyunto linux-leds@vger.kernel.org. 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunIt is possible that more than one LED with the same color and function will 69*4882a593Smuzhiyunbe required for given platform, differing only with an ordinal number. 70*4882a593SmuzhiyunIn this case it is preferable to just concatenate the predefined LED_FUNCTION_* 71*4882a593Smuzhiyunname with required "-N" suffix in the driver. fwnode based drivers can use 72*4882a593Smuzhiyunfunction-enumerator property for that and then the concatenation will be handled 73*4882a593Smuzhiyunautomatically by the LED core upon LED class device registration. 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunLED subsystem has also a protection against name clash, that may occur 76*4882a593Smuzhiyunwhen LED class device is created by a driver of hot-pluggable device and 77*4882a593Smuzhiyunit doesn't provide unique devicename section. In this case numerical 78*4882a593Smuzhiyunsuffix (e.g. "_1", "_2", "_3" etc.) is added to the requested LED class 79*4882a593Smuzhiyundevice name. 80*4882a593Smuzhiyun 81*4882a593SmuzhiyunThere might be still LED class drivers around using vendor or product name 82*4882a593Smuzhiyunfor devicename, but this approach is now deprecated as it doesn't convey 83*4882a593Smuzhiyunany added value. Product information can be found in other places in sysfs 84*4882a593Smuzhiyun(see tools/leds/get_led_device_info.sh). 85*4882a593Smuzhiyun 86*4882a593SmuzhiyunExamples of proper LED names: 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun - "red:disk" 89*4882a593Smuzhiyun - "white:flash" 90*4882a593Smuzhiyun - "red:indicator" 91*4882a593Smuzhiyun - "phy1:green:wlan" 92*4882a593Smuzhiyun - "phy3::wlan" 93*4882a593Smuzhiyun - ":kbd_backlight" 94*4882a593Smuzhiyun - "input5::kbd_backlight" 95*4882a593Smuzhiyun - "input3::numlock" 96*4882a593Smuzhiyun - "input3::scrolllock" 97*4882a593Smuzhiyun - "input3::capslock" 98*4882a593Smuzhiyun - "mmc1::status" 99*4882a593Smuzhiyun - "white:status" 100*4882a593Smuzhiyun 101*4882a593Smuzhiyunget_led_device_info.sh script can be used for verifying if the LED name 102*4882a593Smuzhiyunmeets the requirements pointed out here. It performs validation of the LED class 103*4882a593Smuzhiyundevicename sections and gives hints on expected value for a section in case 104*4882a593Smuzhiyunthe validation fails for it. So far the script supports validation 105*4882a593Smuzhiyunof associations between LEDs and following types of devices: 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun - input devices 108*4882a593Smuzhiyun - ieee80211 compliant USB devices 109*4882a593Smuzhiyun 110*4882a593SmuzhiyunThe script is open to extensions. 111*4882a593Smuzhiyun 112*4882a593SmuzhiyunThere have been calls for LED properties such as color to be exported as 113*4882a593Smuzhiyunindividual led class attributes. As a solution which doesn't incur as much 114*4882a593Smuzhiyunoverhead, I suggest these become part of the device name. The naming scheme 115*4882a593Smuzhiyunabove leaves scope for further attributes should they be needed. If sections 116*4882a593Smuzhiyunof the name don't apply, just leave that section blank. 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunBrightness setting API 120*4882a593Smuzhiyun====================== 121*4882a593Smuzhiyun 122*4882a593SmuzhiyunLED subsystem core exposes following API for setting brightness: 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun - led_set_brightness: 125*4882a593Smuzhiyun it is guaranteed not to sleep, passing LED_OFF stops 126*4882a593Smuzhiyun blinking, 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun - led_set_brightness_sync: 129*4882a593Smuzhiyun for use cases when immediate effect is desired - 130*4882a593Smuzhiyun it can block the caller for the time required for accessing 131*4882a593Smuzhiyun device registers and can sleep, passing LED_OFF stops hardware 132*4882a593Smuzhiyun blinking, returns -EBUSY if software blink fallback is enabled. 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunLED registration API 136*4882a593Smuzhiyun==================== 137*4882a593Smuzhiyun 138*4882a593SmuzhiyunA driver wanting to register a LED classdev for use by other drivers / 139*4882a593Smuzhiyunuserspace needs to allocate and fill a led_classdev struct and then call 140*4882a593Smuzhiyun`[devm_]led_classdev_register`. If the non devm version is used the driver 141*4882a593Smuzhiyunmust call led_classdev_unregister from its remove function before 142*4882a593Smuzhiyunfree-ing the led_classdev struct. 143*4882a593Smuzhiyun 144*4882a593SmuzhiyunIf the driver can detect hardware initiated brightness changes and thus 145*4882a593Smuzhiyunwants to have a brightness_hw_changed attribute then the LED_BRIGHT_HW_CHANGED 146*4882a593Smuzhiyunflag must be set in flags before registering. Calling 147*4882a593Smuzhiyunled_classdev_notify_brightness_hw_changed on a classdev not registered with 148*4882a593Smuzhiyunthe LED_BRIGHT_HW_CHANGED flag is a bug and will trigger a WARN_ON. 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunHardware accelerated blink of LEDs 151*4882a593Smuzhiyun================================== 152*4882a593Smuzhiyun 153*4882a593SmuzhiyunSome LEDs can be programmed to blink without any CPU interaction. To 154*4882a593Smuzhiyunsupport this feature, a LED driver can optionally implement the 155*4882a593Smuzhiyunblink_set() function (see <linux/leds.h>). To set an LED to blinking, 156*4882a593Smuzhiyunhowever, it is better to use the API function led_blink_set(), as it 157*4882a593Smuzhiyunwill check and implement software fallback if necessary. 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunTo turn off blinking, use the API function led_brightness_set() 160*4882a593Smuzhiyunwith brightness value LED_OFF, which should stop any software 161*4882a593Smuzhiyuntimers that may have been required for blinking. 162*4882a593Smuzhiyun 163*4882a593SmuzhiyunThe blink_set() function should choose a user friendly blinking value 164*4882a593Smuzhiyunif it is called with `*delay_on==0` && `*delay_off==0` parameters. In this 165*4882a593Smuzhiyuncase the driver should give back the chosen value through delay_on and 166*4882a593Smuzhiyundelay_off parameters to the leds subsystem. 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunSetting the brightness to zero with brightness_set() callback function 169*4882a593Smuzhiyunshould completely turn off the LED and cancel the previously programmed 170*4882a593Smuzhiyunhardware blinking function, if any. 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun 173*4882a593SmuzhiyunKnown Issues 174*4882a593Smuzhiyun============ 175*4882a593Smuzhiyun 176*4882a593SmuzhiyunThe LED Trigger core cannot be a module as the simple trigger functions 177*4882a593Smuzhiyunwould cause nightmare dependency issues. I see this as a minor issue 178*4882a593Smuzhiyuncompared to the benefits the simple trigger functionality brings. The 179*4882a593Smuzhiyunrest of the LED subsystem can be modular. 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun 182*4882a593SmuzhiyunFuture Development 183*4882a593Smuzhiyun================== 184*4882a593Smuzhiyun 185*4882a593SmuzhiyunAt the moment, a trigger can't be created specifically for a single LED. 186*4882a593SmuzhiyunThere are a number of cases where a trigger might only be mappable to a 187*4882a593Smuzhiyunparticular LED (ACPI?). The addition of triggers provided by the LED driver 188*4882a593Smuzhiyunshould cover this option and be possible to add without breaking the 189*4882a593Smuzhiyuncurrent interface. 190