1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2c72a1d60SRichard Purdie /* 3c72a1d60SRichard Purdie * LED Class Core 4c72a1d60SRichard Purdie * 5c72a1d60SRichard Purdie * Copyright 2005-2006 Openedhand Ltd. 6c72a1d60SRichard Purdie * 7c72a1d60SRichard Purdie * Author: Richard Purdie <rpurdie@openedhand.com> 8c72a1d60SRichard Purdie */ 9c72a1d60SRichard Purdie 10c72a1d60SRichard Purdie #include <linux/kernel.h> 11*5607ca92SHans de Goede #include <linux/led-class-multicolor.h> 1204713306SJacek Anaszewski #include <linux/leds.h> 13c72a1d60SRichard Purdie #include <linux/list.h> 14c72a1d60SRichard Purdie #include <linux/module.h> 1504713306SJacek Anaszewski #include <linux/mutex.h> 168e1f4561SKrzysztof Kozlowski #include <linux/of.h> 17bb4e9af0SJacek Anaszewski #include <linux/property.h> 1872f8da32SRichard Purdie #include <linux/rwsem.h> 198e1f4561SKrzysztof Kozlowski #include <linux/slab.h> 20bb4e9af0SJacek Anaszewski #include <uapi/linux/uleds.h> 21c72a1d60SRichard Purdie #include "leds.h" 22c72a1d60SRichard Purdie 2372f8da32SRichard Purdie DECLARE_RWSEM(leds_list_lock); 24c72a1d60SRichard Purdie EXPORT_SYMBOL_GPL(leds_list_lock); 254d404fd5SNémeth Márton 264d404fd5SNémeth Márton LIST_HEAD(leds_list); 274d404fd5SNémeth Márton EXPORT_SYMBOL_GPL(leds_list); 28a403d930SBryan Wu 29bb4e9af0SJacek Anaszewski const char * const led_colors[LED_COLOR_ID_MAX] = { 30bb4e9af0SJacek Anaszewski [LED_COLOR_ID_WHITE] = "white", 31bb4e9af0SJacek Anaszewski [LED_COLOR_ID_RED] = "red", 32bb4e9af0SJacek Anaszewski [LED_COLOR_ID_GREEN] = "green", 33bb4e9af0SJacek Anaszewski [LED_COLOR_ID_BLUE] = "blue", 34bb4e9af0SJacek Anaszewski [LED_COLOR_ID_AMBER] = "amber", 35bb4e9af0SJacek Anaszewski [LED_COLOR_ID_VIOLET] = "violet", 36bb4e9af0SJacek Anaszewski [LED_COLOR_ID_YELLOW] = "yellow", 37bb4e9af0SJacek Anaszewski [LED_COLOR_ID_IR] = "ir", 3810d3e0d8SDan Murphy [LED_COLOR_ID_MULTI] = "multicolor", 3954212f5aSPavel Machek [LED_COLOR_ID_RGB] = "rgb", 40a0679431SOndrej Jirman [LED_COLOR_ID_PURPLE] = "purple", 41a0679431SOndrej Jirman [LED_COLOR_ID_ORANGE] = "orange", 42a0679431SOndrej Jirman [LED_COLOR_ID_PINK] = "pink", 43a0679431SOndrej Jirman [LED_COLOR_ID_CYAN] = "cyan", 44a0679431SOndrej Jirman [LED_COLOR_ID_LIME] = "lime", 45bb4e9af0SJacek Anaszewski }; 46bb4e9af0SJacek Anaszewski EXPORT_SYMBOL_GPL(led_colors); 47bb4e9af0SJacek Anaszewski 48af0bfab9SAbanoub Sameh static int __led_set_brightness(struct led_classdev *led_cdev, unsigned int value) 49d4887af9SHeiner Kallweit { 50d4887af9SHeiner Kallweit if (!led_cdev->brightness_set) 51d4887af9SHeiner Kallweit return -ENOTSUPP; 52d4887af9SHeiner Kallweit 53d4887af9SHeiner Kallweit led_cdev->brightness_set(led_cdev, value); 54d4887af9SHeiner Kallweit 55d4887af9SHeiner Kallweit return 0; 56d4887af9SHeiner Kallweit } 57d4887af9SHeiner Kallweit 58af0bfab9SAbanoub Sameh static int __led_set_brightness_blocking(struct led_classdev *led_cdev, unsigned int value) 59d4887af9SHeiner Kallweit { 60d4887af9SHeiner Kallweit if (!led_cdev->brightness_set_blocking) 61d4887af9SHeiner Kallweit return -ENOTSUPP; 62d4887af9SHeiner Kallweit 63d4887af9SHeiner Kallweit return led_cdev->brightness_set_blocking(led_cdev, value); 64d4887af9SHeiner Kallweit } 65d4887af9SHeiner Kallweit 6649404665SKees Cook static void led_timer_function(struct timer_list *t) 67757b06aeSJacek Anaszewski { 6849404665SKees Cook struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer); 69757b06aeSJacek Anaszewski unsigned long brightness; 70757b06aeSJacek Anaszewski unsigned long delay; 71757b06aeSJacek Anaszewski 72757b06aeSJacek Anaszewski if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) { 7381fe8e5bSJacek Anaszewski led_set_brightness_nosleep(led_cdev, LED_OFF); 74a9c6ce57SHans de Goede clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 75757b06aeSJacek Anaszewski return; 76757b06aeSJacek Anaszewski } 77757b06aeSJacek Anaszewski 78a9c6ce57SHans de Goede if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP, 79a9c6ce57SHans de Goede &led_cdev->work_flags)) { 80a9c6ce57SHans de Goede clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 81757b06aeSJacek Anaszewski return; 82757b06aeSJacek Anaszewski } 83757b06aeSJacek Anaszewski 84757b06aeSJacek Anaszewski brightness = led_get_brightness(led_cdev); 85757b06aeSJacek Anaszewski if (!brightness) { 86757b06aeSJacek Anaszewski /* Time to switch the LED on. */ 87eb1610b4SHans de Goede if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, 88eb1610b4SHans de Goede &led_cdev->work_flags)) 89eb1610b4SHans de Goede brightness = led_cdev->new_blink_brightness; 90eb1610b4SHans de Goede else 91757b06aeSJacek Anaszewski brightness = led_cdev->blink_brightness; 92757b06aeSJacek Anaszewski delay = led_cdev->blink_delay_on; 93757b06aeSJacek Anaszewski } else { 94757b06aeSJacek Anaszewski /* Store the current brightness value to be able 95757b06aeSJacek Anaszewski * to restore it when the delay_off period is over. 96757b06aeSJacek Anaszewski */ 97757b06aeSJacek Anaszewski led_cdev->blink_brightness = brightness; 98757b06aeSJacek Anaszewski brightness = LED_OFF; 99757b06aeSJacek Anaszewski delay = led_cdev->blink_delay_off; 100757b06aeSJacek Anaszewski } 101757b06aeSJacek Anaszewski 10281fe8e5bSJacek Anaszewski led_set_brightness_nosleep(led_cdev, brightness); 103757b06aeSJacek Anaszewski 104757b06aeSJacek Anaszewski /* Return in next iteration if led is in one-shot mode and we are in 105757b06aeSJacek Anaszewski * the final blink state so that the led is toggled each delay_on + 106757b06aeSJacek Anaszewski * delay_off milliseconds in worst case. 107757b06aeSJacek Anaszewski */ 108a9c6ce57SHans de Goede if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) { 109a9c6ce57SHans de Goede if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) { 110757b06aeSJacek Anaszewski if (brightness) 111a9c6ce57SHans de Goede set_bit(LED_BLINK_ONESHOT_STOP, 112a9c6ce57SHans de Goede &led_cdev->work_flags); 113757b06aeSJacek Anaszewski } else { 114757b06aeSJacek Anaszewski if (!brightness) 115a9c6ce57SHans de Goede set_bit(LED_BLINK_ONESHOT_STOP, 116a9c6ce57SHans de Goede &led_cdev->work_flags); 117757b06aeSJacek Anaszewski } 118757b06aeSJacek Anaszewski } 119757b06aeSJacek Anaszewski 120757b06aeSJacek Anaszewski mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay)); 121757b06aeSJacek Anaszewski } 122757b06aeSJacek Anaszewski 123fa15d8c6SHans de Goede static void set_brightness_delayed_set_brightness(struct led_classdev *led_cdev, 124fa15d8c6SHans de Goede unsigned int value) 125757b06aeSJacek Anaszewski { 1261afcadfcSJacek Anaszewski int ret = 0; 127757b06aeSJacek Anaszewski 128fa15d8c6SHans de Goede ret = __led_set_brightness(led_cdev, value); 129d4887af9SHeiner Kallweit if (ret == -ENOTSUPP) 130fa15d8c6SHans de Goede ret = __led_set_brightness_blocking(led_cdev, value); 131d84d80f3SHeiner Kallweit if (ret < 0 && 132d84d80f3SHeiner Kallweit /* LED HW might have been unplugged, therefore don't warn */ 133d84d80f3SHeiner Kallweit !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) && 134d84d80f3SHeiner Kallweit (led_cdev->flags & LED_HW_PLUGGABLE))) 1351afcadfcSJacek Anaszewski dev_err(led_cdev->dev, 1361afcadfcSJacek Anaszewski "Setting an LED's brightness failed (%d)\n", ret); 137757b06aeSJacek Anaszewski } 138757b06aeSJacek Anaszewski 139fa15d8c6SHans de Goede static void set_brightness_delayed(struct work_struct *ws) 140fa15d8c6SHans de Goede { 141fa15d8c6SHans de Goede struct led_classdev *led_cdev = 142fa15d8c6SHans de Goede container_of(ws, struct led_classdev, set_brightness_work); 143fa15d8c6SHans de Goede 144fa15d8c6SHans de Goede if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) { 145fa15d8c6SHans de Goede led_stop_software_blink(led_cdev); 146fa15d8c6SHans de Goede set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags); 147fa15d8c6SHans de Goede } 148fa15d8c6SHans de Goede 149fa15d8c6SHans de Goede /* 150fa15d8c6SHans de Goede * Triggers may call led_set_brightness(LED_OFF), 151fa15d8c6SHans de Goede * led_set_brightness(LED_FULL) in quick succession to disable blinking 152fa15d8c6SHans de Goede * and turn the LED on. Both actions may have been scheduled to run 153fa15d8c6SHans de Goede * before this work item runs once. To make sure this works properly 154fa15d8c6SHans de Goede * handle LED_SET_BRIGHTNESS_OFF first. 155fa15d8c6SHans de Goede */ 156fa15d8c6SHans de Goede if (test_and_clear_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags)) 157fa15d8c6SHans de Goede set_brightness_delayed_set_brightness(led_cdev, LED_OFF); 158fa15d8c6SHans de Goede 159fa15d8c6SHans de Goede if (test_and_clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags)) 160fa15d8c6SHans de Goede set_brightness_delayed_set_brightness(led_cdev, led_cdev->delayed_set_value); 16122720a87SHans de Goede 16222720a87SHans de Goede if (test_and_clear_bit(LED_SET_BLINK, &led_cdev->work_flags)) { 16322720a87SHans de Goede unsigned long delay_on = led_cdev->delayed_delay_on; 16422720a87SHans de Goede unsigned long delay_off = led_cdev->delayed_delay_off; 16522720a87SHans de Goede 16622720a87SHans de Goede led_blink_set(led_cdev, &delay_on, &delay_off); 16722720a87SHans de Goede } 168fa15d8c6SHans de Goede } 169fa15d8c6SHans de Goede 170a403d930SBryan Wu static void led_set_software_blink(struct led_classdev *led_cdev, 171a403d930SBryan Wu unsigned long delay_on, 172a403d930SBryan Wu unsigned long delay_off) 173a403d930SBryan Wu { 174a403d930SBryan Wu int current_brightness; 175a403d930SBryan Wu 176a403d930SBryan Wu current_brightness = led_get_brightness(led_cdev); 177a403d930SBryan Wu if (current_brightness) 178a403d930SBryan Wu led_cdev->blink_brightness = current_brightness; 179a403d930SBryan Wu if (!led_cdev->blink_brightness) 180a403d930SBryan Wu led_cdev->blink_brightness = led_cdev->max_brightness; 181a403d930SBryan Wu 182a403d930SBryan Wu led_cdev->blink_delay_on = delay_on; 183a403d930SBryan Wu led_cdev->blink_delay_off = delay_off; 184a403d930SBryan Wu 1858d82fef8SStefan Sørensen /* never on - just set to off */ 1868d82fef8SStefan Sørensen if (!delay_on) { 18781fe8e5bSJacek Anaszewski led_set_brightness_nosleep(led_cdev, LED_OFF); 188a403d930SBryan Wu return; 1898d82fef8SStefan Sørensen } 190a403d930SBryan Wu 191a403d930SBryan Wu /* never off - just set to brightness */ 192a403d930SBryan Wu if (!delay_off) { 19381fe8e5bSJacek Anaszewski led_set_brightness_nosleep(led_cdev, 19481fe8e5bSJacek Anaszewski led_cdev->blink_brightness); 195a403d930SBryan Wu return; 196a403d930SBryan Wu } 197a403d930SBryan Wu 198a9c6ce57SHans de Goede set_bit(LED_BLINK_SW, &led_cdev->work_flags); 1999067359fSJiri Kosina mod_timer(&led_cdev->blink_timer, jiffies + 1); 200a403d930SBryan Wu } 201a403d930SBryan Wu 202a403d930SBryan Wu 20320c0e6b8SBryan Wu static void led_blink_setup(struct led_classdev *led_cdev, 204a403d930SBryan Wu unsigned long *delay_on, 205a403d930SBryan Wu unsigned long *delay_off) 206a403d930SBryan Wu { 207a9c6ce57SHans de Goede if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && 2085bb629c5SFabio Baltieri led_cdev->blink_set && 209a403d930SBryan Wu !led_cdev->blink_set(led_cdev, delay_on, delay_off)) 210a403d930SBryan Wu return; 211a403d930SBryan Wu 212a403d930SBryan Wu /* blink with 1 Hz as default if nothing specified */ 213a403d930SBryan Wu if (!*delay_on && !*delay_off) 214a403d930SBryan Wu *delay_on = *delay_off = 500; 215a403d930SBryan Wu 216a403d930SBryan Wu led_set_software_blink(led_cdev, *delay_on, *delay_off); 217a403d930SBryan Wu } 2185bb629c5SFabio Baltieri 219757b06aeSJacek Anaszewski void led_init_core(struct led_classdev *led_cdev) 220757b06aeSJacek Anaszewski { 221757b06aeSJacek Anaszewski INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed); 222757b06aeSJacek Anaszewski 22349404665SKees Cook timer_setup(&led_cdev->blink_timer, led_timer_function, 0); 224757b06aeSJacek Anaszewski } 225757b06aeSJacek Anaszewski EXPORT_SYMBOL_GPL(led_init_core); 226757b06aeSJacek Anaszewski 2275bb629c5SFabio Baltieri void led_blink_set(struct led_classdev *led_cdev, 2285bb629c5SFabio Baltieri unsigned long *delay_on, 2295bb629c5SFabio Baltieri unsigned long *delay_off) 2305bb629c5SFabio Baltieri { 2317b6af2c5SJacek Anaszewski del_timer_sync(&led_cdev->blink_timer); 2325bb629c5SFabio Baltieri 2337b6af2c5SJacek Anaszewski clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 234a9c6ce57SHans de Goede clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); 235a9c6ce57SHans de Goede clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); 2365bb629c5SFabio Baltieri 2375bb629c5SFabio Baltieri led_blink_setup(led_cdev, delay_on, delay_off); 2385bb629c5SFabio Baltieri } 2392806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_blink_set); 240a403d930SBryan Wu 2415bb629c5SFabio Baltieri void led_blink_set_oneshot(struct led_classdev *led_cdev, 2425bb629c5SFabio Baltieri unsigned long *delay_on, 2435bb629c5SFabio Baltieri unsigned long *delay_off, 2445bb629c5SFabio Baltieri int invert) 2455bb629c5SFabio Baltieri { 246a9c6ce57SHans de Goede if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && 2479067359fSJiri Kosina timer_pending(&led_cdev->blink_timer)) 2485bb629c5SFabio Baltieri return; 2495bb629c5SFabio Baltieri 250a9c6ce57SHans de Goede set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); 251a9c6ce57SHans de Goede clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); 2525bb629c5SFabio Baltieri 2535bb629c5SFabio Baltieri if (invert) 254a9c6ce57SHans de Goede set_bit(LED_BLINK_INVERT, &led_cdev->work_flags); 2555bb629c5SFabio Baltieri else 256a9c6ce57SHans de Goede clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags); 2575bb629c5SFabio Baltieri 2585bb629c5SFabio Baltieri led_blink_setup(led_cdev, delay_on, delay_off); 2595bb629c5SFabio Baltieri } 2602806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_blink_set_oneshot); 2615bb629c5SFabio Baltieri 26222720a87SHans de Goede void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on, 26322720a87SHans de Goede unsigned long delay_off) 26422720a87SHans de Goede { 26522720a87SHans de Goede /* If necessary delegate to a work queue task. */ 26622720a87SHans de Goede if (led_cdev->blink_set && led_cdev->brightness_set_blocking) { 26722720a87SHans de Goede led_cdev->delayed_delay_on = delay_on; 26822720a87SHans de Goede led_cdev->delayed_delay_off = delay_off; 26922720a87SHans de Goede set_bit(LED_SET_BLINK, &led_cdev->work_flags); 27022720a87SHans de Goede schedule_work(&led_cdev->set_brightness_work); 27122720a87SHans de Goede return; 27222720a87SHans de Goede } 27322720a87SHans de Goede 27422720a87SHans de Goede led_blink_set(led_cdev, &delay_on, &delay_off); 27522720a87SHans de Goede } 27622720a87SHans de Goede EXPORT_SYMBOL_GPL(led_blink_set_nosleep); 27722720a87SHans de Goede 278d23a22a7SFabio Baltieri void led_stop_software_blink(struct led_classdev *led_cdev) 279a403d930SBryan Wu { 2809067359fSJiri Kosina del_timer_sync(&led_cdev->blink_timer); 28143786482SFabio Baltieri led_cdev->blink_delay_on = 0; 28243786482SFabio Baltieri led_cdev->blink_delay_off = 0; 283a9c6ce57SHans de Goede clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 284d23a22a7SFabio Baltieri } 285d23a22a7SFabio Baltieri EXPORT_SYMBOL_GPL(led_stop_software_blink); 286d23a22a7SFabio Baltieri 287af0bfab9SAbanoub Sameh void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness) 288d23a22a7SFabio Baltieri { 289f1e80c07SJacek Anaszewski /* 2907cfe749fSTony Makkiel * If software blink is active, delay brightness setting 291f1e80c07SJacek Anaszewski * until the next timer tick. 292f1e80c07SJacek Anaszewski */ 293a9c6ce57SHans de Goede if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) { 294f1e80c07SJacek Anaszewski /* 295f1e80c07SJacek Anaszewski * If we need to disable soft blinking delegate this to the 296f1e80c07SJacek Anaszewski * work queue task to avoid problems in case we are called 297f1e80c07SJacek Anaszewski * from hard irq context. 298f1e80c07SJacek Anaszewski */ 299af0bfab9SAbanoub Sameh if (!brightness) { 300a9c6ce57SHans de Goede set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags); 301d23a22a7SFabio Baltieri schedule_work(&led_cdev->set_brightness_work); 302f1e80c07SJacek Anaszewski } else { 303a9c6ce57SHans de Goede set_bit(LED_BLINK_BRIGHTNESS_CHANGE, 304a9c6ce57SHans de Goede &led_cdev->work_flags); 305eb1610b4SHans de Goede led_cdev->new_blink_brightness = brightness; 306f1e80c07SJacek Anaszewski } 307d23a22a7SFabio Baltieri return; 308d23a22a7SFabio Baltieri } 30943786482SFabio Baltieri 31081fe8e5bSJacek Anaszewski led_set_brightness_nosleep(led_cdev, brightness); 311a403d930SBryan Wu } 3122806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness); 3133ef7de53SJacek Anaszewski 314af0bfab9SAbanoub Sameh void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value) 31581fe8e5bSJacek Anaszewski { 31681fe8e5bSJacek Anaszewski /* Use brightness_set op if available, it is guaranteed not to sleep */ 317d4887af9SHeiner Kallweit if (!__led_set_brightness(led_cdev, value)) 31881fe8e5bSJacek Anaszewski return; 31981fe8e5bSJacek Anaszewski 320fa15d8c6SHans de Goede /* 321fa15d8c6SHans de Goede * Brightness setting can sleep, delegate it to a work queue task. 322fa15d8c6SHans de Goede * value 0 / LED_OFF is special, since it also disables hw-blinking 323fa15d8c6SHans de Goede * (sw-blink disable is handled in led_set_brightness()). 324fa15d8c6SHans de Goede * To avoid a hw-blink-disable getting lost when a second brightness 325fa15d8c6SHans de Goede * change is done immediately afterwards (before the work runs), 326fa15d8c6SHans de Goede * it uses a separate work_flag. 327fa15d8c6SHans de Goede */ 328fa15d8c6SHans de Goede if (value) { 32981fe8e5bSJacek Anaszewski led_cdev->delayed_set_value = value; 330fa15d8c6SHans de Goede set_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags); 331fa15d8c6SHans de Goede } else { 332fa15d8c6SHans de Goede clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags); 33322720a87SHans de Goede clear_bit(LED_SET_BLINK, &led_cdev->work_flags); 334fa15d8c6SHans de Goede set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags); 335fa15d8c6SHans de Goede } 336fa15d8c6SHans de Goede 33781fe8e5bSJacek Anaszewski schedule_work(&led_cdev->set_brightness_work); 33881fe8e5bSJacek Anaszewski } 33981fe8e5bSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_nopm); 34081fe8e5bSJacek Anaszewski 341af0bfab9SAbanoub Sameh void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value) 34281fe8e5bSJacek Anaszewski { 34381fe8e5bSJacek Anaszewski led_cdev->brightness = min(value, led_cdev->max_brightness); 34481fe8e5bSJacek Anaszewski 34581fe8e5bSJacek Anaszewski if (led_cdev->flags & LED_SUSPENDED) 34681fe8e5bSJacek Anaszewski return; 34781fe8e5bSJacek Anaszewski 34881fe8e5bSJacek Anaszewski led_set_brightness_nopm(led_cdev, led_cdev->brightness); 34981fe8e5bSJacek Anaszewski } 35081fe8e5bSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_nosleep); 35181fe8e5bSJacek Anaszewski 352af0bfab9SAbanoub Sameh int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value) 35313ae79bbSJacek Anaszewski { 35413ae79bbSJacek Anaszewski if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) 35513ae79bbSJacek Anaszewski return -EBUSY; 35613ae79bbSJacek Anaszewski 35713ae79bbSJacek Anaszewski led_cdev->brightness = min(value, led_cdev->max_brightness); 35813ae79bbSJacek Anaszewski 35913ae79bbSJacek Anaszewski if (led_cdev->flags & LED_SUSPENDED) 36013ae79bbSJacek Anaszewski return 0; 36113ae79bbSJacek Anaszewski 362d4887af9SHeiner Kallweit return __led_set_brightness_blocking(led_cdev, led_cdev->brightness); 36313ae79bbSJacek Anaszewski } 36413ae79bbSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_sync); 36513ae79bbSJacek Anaszewski 366*5607ca92SHans de Goede /* 367*5607ca92SHans de Goede * This is a led-core function because just like led_set_brightness() 368*5607ca92SHans de Goede * it is used in the kernel by e.g. triggers. 369*5607ca92SHans de Goede */ 370*5607ca92SHans de Goede void led_mc_set_brightness(struct led_classdev *led_cdev, 371*5607ca92SHans de Goede unsigned int *intensity_value, unsigned int num_colors, 372*5607ca92SHans de Goede unsigned int brightness) 373*5607ca92SHans de Goede { 374*5607ca92SHans de Goede struct led_classdev_mc *mcled_cdev; 375*5607ca92SHans de Goede unsigned int i; 376*5607ca92SHans de Goede 377*5607ca92SHans de Goede if (!(led_cdev->flags & LED_MULTI_COLOR)) { 378*5607ca92SHans de Goede dev_err_once(led_cdev->dev, "error not a multi-color LED\n"); 379*5607ca92SHans de Goede return; 380*5607ca92SHans de Goede } 381*5607ca92SHans de Goede 382*5607ca92SHans de Goede mcled_cdev = lcdev_to_mccdev(led_cdev); 383*5607ca92SHans de Goede if (num_colors != mcled_cdev->num_colors) { 384*5607ca92SHans de Goede dev_err_once(led_cdev->dev, "error num_colors mismatch %u != %u\n", 385*5607ca92SHans de Goede num_colors, mcled_cdev->num_colors); 386*5607ca92SHans de Goede return; 387*5607ca92SHans de Goede } 388*5607ca92SHans de Goede 389*5607ca92SHans de Goede for (i = 0; i < mcled_cdev->num_colors; i++) 390*5607ca92SHans de Goede mcled_cdev->subled_info[i].intensity = intensity_value[i]; 391*5607ca92SHans de Goede 392*5607ca92SHans de Goede led_set_brightness(led_cdev, brightness); 393*5607ca92SHans de Goede } 394*5607ca92SHans de Goede EXPORT_SYMBOL_GPL(led_mc_set_brightness); 395*5607ca92SHans de Goede 3963ef7de53SJacek Anaszewski int led_update_brightness(struct led_classdev *led_cdev) 3973ef7de53SJacek Anaszewski { 39849e50aadSAndy Shevchenko int ret; 3993ef7de53SJacek Anaszewski 4003ef7de53SJacek Anaszewski if (led_cdev->brightness_get) { 4013ef7de53SJacek Anaszewski ret = led_cdev->brightness_get(led_cdev); 40249e50aadSAndy Shevchenko if (ret < 0) 40349e50aadSAndy Shevchenko return ret; 40449e50aadSAndy Shevchenko 4053ef7de53SJacek Anaszewski led_cdev->brightness = ret; 4063ef7de53SJacek Anaszewski } 4073ef7de53SJacek Anaszewski 40849e50aadSAndy Shevchenko return 0; 4093ef7de53SJacek Anaszewski } 4102806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_update_brightness); 411acd899e4SJacek Anaszewski 4128e1f4561SKrzysztof Kozlowski u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size) 4138e1f4561SKrzysztof Kozlowski { 414fd81d7e9SAndy Shevchenko struct fwnode_handle *fwnode = led_cdev->dev->fwnode; 4158e1f4561SKrzysztof Kozlowski u32 *pattern; 4168e1f4561SKrzysztof Kozlowski int count; 4178e1f4561SKrzysztof Kozlowski 418fd81d7e9SAndy Shevchenko count = fwnode_property_count_u32(fwnode, "led-pattern"); 4198e1f4561SKrzysztof Kozlowski if (count < 0) 4208e1f4561SKrzysztof Kozlowski return NULL; 4218e1f4561SKrzysztof Kozlowski 4228e1f4561SKrzysztof Kozlowski pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL); 4238e1f4561SKrzysztof Kozlowski if (!pattern) 4248e1f4561SKrzysztof Kozlowski return NULL; 4258e1f4561SKrzysztof Kozlowski 426fd81d7e9SAndy Shevchenko if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) { 4278e1f4561SKrzysztof Kozlowski kfree(pattern); 4288e1f4561SKrzysztof Kozlowski return NULL; 4298e1f4561SKrzysztof Kozlowski } 4308e1f4561SKrzysztof Kozlowski 4318e1f4561SKrzysztof Kozlowski *size = count; 4328e1f4561SKrzysztof Kozlowski 4338e1f4561SKrzysztof Kozlowski return pattern; 4348e1f4561SKrzysztof Kozlowski } 4358e1f4561SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(led_get_default_pattern); 4368e1f4561SKrzysztof Kozlowski 437acd899e4SJacek Anaszewski /* Caller must ensure led_cdev->led_access held */ 438acd899e4SJacek Anaszewski void led_sysfs_disable(struct led_classdev *led_cdev) 439acd899e4SJacek Anaszewski { 440acd899e4SJacek Anaszewski lockdep_assert_held(&led_cdev->led_access); 441acd899e4SJacek Anaszewski 442acd899e4SJacek Anaszewski led_cdev->flags |= LED_SYSFS_DISABLE; 443acd899e4SJacek Anaszewski } 444acd899e4SJacek Anaszewski EXPORT_SYMBOL_GPL(led_sysfs_disable); 445acd899e4SJacek Anaszewski 446acd899e4SJacek Anaszewski /* Caller must ensure led_cdev->led_access held */ 447acd899e4SJacek Anaszewski void led_sysfs_enable(struct led_classdev *led_cdev) 448acd899e4SJacek Anaszewski { 449acd899e4SJacek Anaszewski lockdep_assert_held(&led_cdev->led_access); 450acd899e4SJacek Anaszewski 451acd899e4SJacek Anaszewski led_cdev->flags &= ~LED_SYSFS_DISABLE; 452acd899e4SJacek Anaszewski } 453acd899e4SJacek Anaszewski EXPORT_SYMBOL_GPL(led_sysfs_enable); 454bb4e9af0SJacek Anaszewski 455bb4e9af0SJacek Anaszewski static void led_parse_fwnode_props(struct device *dev, 456bb4e9af0SJacek Anaszewski struct fwnode_handle *fwnode, 457bb4e9af0SJacek Anaszewski struct led_properties *props) 458bb4e9af0SJacek Anaszewski { 459bb4e9af0SJacek Anaszewski int ret; 460bb4e9af0SJacek Anaszewski 461bb4e9af0SJacek Anaszewski if (!fwnode) 462bb4e9af0SJacek Anaszewski return; 463bb4e9af0SJacek Anaszewski 464bb4e9af0SJacek Anaszewski if (fwnode_property_present(fwnode, "label")) { 465bb4e9af0SJacek Anaszewski ret = fwnode_property_read_string(fwnode, "label", &props->label); 466bb4e9af0SJacek Anaszewski if (ret) 467bb4e9af0SJacek Anaszewski dev_err(dev, "Error parsing 'label' property (%d)\n", ret); 468bb4e9af0SJacek Anaszewski return; 469bb4e9af0SJacek Anaszewski } 470bb4e9af0SJacek Anaszewski 471bb4e9af0SJacek Anaszewski if (fwnode_property_present(fwnode, "color")) { 472bb4e9af0SJacek Anaszewski ret = fwnode_property_read_u32(fwnode, "color", &props->color); 473bb4e9af0SJacek Anaszewski if (ret) 474bb4e9af0SJacek Anaszewski dev_err(dev, "Error parsing 'color' property (%d)\n", ret); 475bb4e9af0SJacek Anaszewski else if (props->color >= LED_COLOR_ID_MAX) 476bb4e9af0SJacek Anaszewski dev_err(dev, "LED color identifier out of range\n"); 477bb4e9af0SJacek Anaszewski else 478bb4e9af0SJacek Anaszewski props->color_present = true; 479bb4e9af0SJacek Anaszewski } 480bb4e9af0SJacek Anaszewski 481bb4e9af0SJacek Anaszewski 482bb4e9af0SJacek Anaszewski if (!fwnode_property_present(fwnode, "function")) 483bb4e9af0SJacek Anaszewski return; 484bb4e9af0SJacek Anaszewski 485bb4e9af0SJacek Anaszewski ret = fwnode_property_read_string(fwnode, "function", &props->function); 486bb4e9af0SJacek Anaszewski if (ret) { 487bb4e9af0SJacek Anaszewski dev_err(dev, 488bb4e9af0SJacek Anaszewski "Error parsing 'function' property (%d)\n", 489bb4e9af0SJacek Anaszewski ret); 490bb4e9af0SJacek Anaszewski } 491bb4e9af0SJacek Anaszewski 492bb4e9af0SJacek Anaszewski if (!fwnode_property_present(fwnode, "function-enumerator")) 493bb4e9af0SJacek Anaszewski return; 494bb4e9af0SJacek Anaszewski 495bb4e9af0SJacek Anaszewski ret = fwnode_property_read_u32(fwnode, "function-enumerator", 496bb4e9af0SJacek Anaszewski &props->func_enum); 497bb4e9af0SJacek Anaszewski if (ret) { 498bb4e9af0SJacek Anaszewski dev_err(dev, 499bb4e9af0SJacek Anaszewski "Error parsing 'function-enumerator' property (%d)\n", 500bb4e9af0SJacek Anaszewski ret); 501bb4e9af0SJacek Anaszewski } else { 502bb4e9af0SJacek Anaszewski props->func_enum_present = true; 503bb4e9af0SJacek Anaszewski } 504bb4e9af0SJacek Anaszewski } 505bb4e9af0SJacek Anaszewski 506bb4e9af0SJacek Anaszewski int led_compose_name(struct device *dev, struct led_init_data *init_data, 507bb4e9af0SJacek Anaszewski char *led_classdev_name) 508bb4e9af0SJacek Anaszewski { 509bb4e9af0SJacek Anaszewski struct led_properties props = {}; 510bb4e9af0SJacek Anaszewski struct fwnode_handle *fwnode = init_data->fwnode; 511bb4e9af0SJacek Anaszewski const char *devicename = init_data->devicename; 512bb4e9af0SJacek Anaszewski 513bb4e9af0SJacek Anaszewski if (!led_classdev_name) 514bb4e9af0SJacek Anaszewski return -EINVAL; 515bb4e9af0SJacek Anaszewski 516bb4e9af0SJacek Anaszewski led_parse_fwnode_props(dev, fwnode, &props); 517bb4e9af0SJacek Anaszewski 518bb4e9af0SJacek Anaszewski if (props.label) { 519bb4e9af0SJacek Anaszewski /* 520bb4e9af0SJacek Anaszewski * If init_data.devicename is NULL, then it indicates that 521bb4e9af0SJacek Anaszewski * DT label should be used as-is for LED class device name. 522bb4e9af0SJacek Anaszewski * Otherwise the label is prepended with devicename to compose 523bb4e9af0SJacek Anaszewski * the final LED class device name. 524bb4e9af0SJacek Anaszewski */ 525bb4e9af0SJacek Anaszewski if (!devicename) { 526bb4e9af0SJacek Anaszewski strscpy(led_classdev_name, props.label, 527bb4e9af0SJacek Anaszewski LED_MAX_NAME_SIZE); 528bb4e9af0SJacek Anaszewski } else { 529bb4e9af0SJacek Anaszewski snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", 530bb4e9af0SJacek Anaszewski devicename, props.label); 531bb4e9af0SJacek Anaszewski } 532bb4e9af0SJacek Anaszewski } else if (props.function || props.color_present) { 533bb4e9af0SJacek Anaszewski char tmp_buf[LED_MAX_NAME_SIZE]; 534bb4e9af0SJacek Anaszewski 535bb4e9af0SJacek Anaszewski if (props.func_enum_present) { 536bb4e9af0SJacek Anaszewski snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d", 537bb4e9af0SJacek Anaszewski props.color_present ? led_colors[props.color] : "", 538bb4e9af0SJacek Anaszewski props.function ?: "", props.func_enum); 539bb4e9af0SJacek Anaszewski } else { 540bb4e9af0SJacek Anaszewski snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s", 541bb4e9af0SJacek Anaszewski props.color_present ? led_colors[props.color] : "", 542bb4e9af0SJacek Anaszewski props.function ?: ""); 543bb4e9af0SJacek Anaszewski } 544bb4e9af0SJacek Anaszewski if (init_data->devname_mandatory) { 545bb4e9af0SJacek Anaszewski snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", 546bb4e9af0SJacek Anaszewski devicename, tmp_buf); 547bb4e9af0SJacek Anaszewski } else { 548bb4e9af0SJacek Anaszewski strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE); 549bb4e9af0SJacek Anaszewski 550bb4e9af0SJacek Anaszewski } 551bb4e9af0SJacek Anaszewski } else if (init_data->default_label) { 552bb4e9af0SJacek Anaszewski if (!devicename) { 553bb4e9af0SJacek Anaszewski dev_err(dev, "Legacy LED naming requires devicename segment"); 554bb4e9af0SJacek Anaszewski return -EINVAL; 555bb4e9af0SJacek Anaszewski } 556bb4e9af0SJacek Anaszewski snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", 557bb4e9af0SJacek Anaszewski devicename, init_data->default_label); 558bb4e9af0SJacek Anaszewski } else if (is_of_node(fwnode)) { 559bb4e9af0SJacek Anaszewski strscpy(led_classdev_name, to_of_node(fwnode)->name, 560bb4e9af0SJacek Anaszewski LED_MAX_NAME_SIZE); 561bb4e9af0SJacek Anaszewski } else 562bb4e9af0SJacek Anaszewski return -EINVAL; 563bb4e9af0SJacek Anaszewski 564bb4e9af0SJacek Anaszewski return 0; 565bb4e9af0SJacek Anaszewski } 566bb4e9af0SJacek Anaszewski EXPORT_SYMBOL_GPL(led_compose_name); 567791bc411SDenis Osterland-Heim 568791bc411SDenis Osterland-Heim enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode) 569791bc411SDenis Osterland-Heim { 570791bc411SDenis Osterland-Heim const char *state = NULL; 571791bc411SDenis Osterland-Heim 572791bc411SDenis Osterland-Heim if (!fwnode_property_read_string(fwnode, "default-state", &state)) { 573791bc411SDenis Osterland-Heim if (!strcmp(state, "keep")) 574791bc411SDenis Osterland-Heim return LEDS_DEFSTATE_KEEP; 575791bc411SDenis Osterland-Heim if (!strcmp(state, "on")) 576791bc411SDenis Osterland-Heim return LEDS_DEFSTATE_ON; 577791bc411SDenis Osterland-Heim } 578791bc411SDenis Osterland-Heim 579791bc411SDenis Osterland-Heim return LEDS_DEFSTATE_OFF; 580791bc411SDenis Osterland-Heim } 581791bc411SDenis Osterland-Heim EXPORT_SYMBOL_GPL(led_init_default_state_get); 582