xref: /linux/drivers/leds/led-core.c (revision a067943129b4ec6b835e02cfd5fbef01093c1471)
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>
1104713306SJacek Anaszewski #include <linux/leds.h>
12c72a1d60SRichard Purdie #include <linux/list.h>
13c72a1d60SRichard Purdie #include <linux/module.h>
1404713306SJacek Anaszewski #include <linux/mutex.h>
158e1f4561SKrzysztof Kozlowski #include <linux/of.h>
16bb4e9af0SJacek Anaszewski #include <linux/property.h>
1772f8da32SRichard Purdie #include <linux/rwsem.h>
188e1f4561SKrzysztof Kozlowski #include <linux/slab.h>
19bb4e9af0SJacek Anaszewski #include <uapi/linux/uleds.h>
20c72a1d60SRichard Purdie #include "leds.h"
21c72a1d60SRichard Purdie 
2272f8da32SRichard Purdie DECLARE_RWSEM(leds_list_lock);
23c72a1d60SRichard Purdie EXPORT_SYMBOL_GPL(leds_list_lock);
244d404fd5SNémeth Márton 
254d404fd5SNémeth Márton LIST_HEAD(leds_list);
264d404fd5SNémeth Márton EXPORT_SYMBOL_GPL(leds_list);
27a403d930SBryan Wu 
28bb4e9af0SJacek Anaszewski const char * const led_colors[LED_COLOR_ID_MAX] = {
29bb4e9af0SJacek Anaszewski 	[LED_COLOR_ID_WHITE] = "white",
30bb4e9af0SJacek Anaszewski 	[LED_COLOR_ID_RED] = "red",
31bb4e9af0SJacek Anaszewski 	[LED_COLOR_ID_GREEN] = "green",
32bb4e9af0SJacek Anaszewski 	[LED_COLOR_ID_BLUE] = "blue",
33bb4e9af0SJacek Anaszewski 	[LED_COLOR_ID_AMBER] = "amber",
34bb4e9af0SJacek Anaszewski 	[LED_COLOR_ID_VIOLET] = "violet",
35bb4e9af0SJacek Anaszewski 	[LED_COLOR_ID_YELLOW] = "yellow",
36bb4e9af0SJacek Anaszewski 	[LED_COLOR_ID_IR] = "ir",
3710d3e0d8SDan Murphy 	[LED_COLOR_ID_MULTI] = "multicolor",
3854212f5aSPavel Machek 	[LED_COLOR_ID_RGB] = "rgb",
39*a0679431SOndrej Jirman 	[LED_COLOR_ID_PURPLE] = "purple",
40*a0679431SOndrej Jirman 	[LED_COLOR_ID_ORANGE] = "orange",
41*a0679431SOndrej Jirman 	[LED_COLOR_ID_PINK] = "pink",
42*a0679431SOndrej Jirman 	[LED_COLOR_ID_CYAN] = "cyan",
43*a0679431SOndrej Jirman 	[LED_COLOR_ID_LIME] = "lime",
44bb4e9af0SJacek Anaszewski };
45bb4e9af0SJacek Anaszewski EXPORT_SYMBOL_GPL(led_colors);
46bb4e9af0SJacek Anaszewski 
47af0bfab9SAbanoub Sameh static int __led_set_brightness(struct led_classdev *led_cdev, unsigned int value)
48d4887af9SHeiner Kallweit {
49d4887af9SHeiner Kallweit 	if (!led_cdev->brightness_set)
50d4887af9SHeiner Kallweit 		return -ENOTSUPP;
51d4887af9SHeiner Kallweit 
52d4887af9SHeiner Kallweit 	led_cdev->brightness_set(led_cdev, value);
53d4887af9SHeiner Kallweit 
54d4887af9SHeiner Kallweit 	return 0;
55d4887af9SHeiner Kallweit }
56d4887af9SHeiner Kallweit 
57af0bfab9SAbanoub Sameh static int __led_set_brightness_blocking(struct led_classdev *led_cdev, unsigned int value)
58d4887af9SHeiner Kallweit {
59d4887af9SHeiner Kallweit 	if (!led_cdev->brightness_set_blocking)
60d4887af9SHeiner Kallweit 		return -ENOTSUPP;
61d4887af9SHeiner Kallweit 
62d4887af9SHeiner Kallweit 	return led_cdev->brightness_set_blocking(led_cdev, value);
63d4887af9SHeiner Kallweit }
64d4887af9SHeiner Kallweit 
6549404665SKees Cook static void led_timer_function(struct timer_list *t)
66757b06aeSJacek Anaszewski {
6749404665SKees Cook 	struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
68757b06aeSJacek Anaszewski 	unsigned long brightness;
69757b06aeSJacek Anaszewski 	unsigned long delay;
70757b06aeSJacek Anaszewski 
71757b06aeSJacek Anaszewski 	if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
7281fe8e5bSJacek Anaszewski 		led_set_brightness_nosleep(led_cdev, LED_OFF);
73a9c6ce57SHans de Goede 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
74757b06aeSJacek Anaszewski 		return;
75757b06aeSJacek Anaszewski 	}
76757b06aeSJacek Anaszewski 
77a9c6ce57SHans de Goede 	if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
78a9c6ce57SHans de Goede 			       &led_cdev->work_flags)) {
79a9c6ce57SHans de Goede 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
80757b06aeSJacek Anaszewski 		return;
81757b06aeSJacek Anaszewski 	}
82757b06aeSJacek Anaszewski 
83757b06aeSJacek Anaszewski 	brightness = led_get_brightness(led_cdev);
84757b06aeSJacek Anaszewski 	if (!brightness) {
85757b06aeSJacek Anaszewski 		/* Time to switch the LED on. */
86eb1610b4SHans de Goede 		if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE,
87eb1610b4SHans de Goede 					&led_cdev->work_flags))
88eb1610b4SHans de Goede 			brightness = led_cdev->new_blink_brightness;
89eb1610b4SHans de Goede 		else
90757b06aeSJacek Anaszewski 			brightness = led_cdev->blink_brightness;
91757b06aeSJacek Anaszewski 		delay = led_cdev->blink_delay_on;
92757b06aeSJacek Anaszewski 	} else {
93757b06aeSJacek Anaszewski 		/* Store the current brightness value to be able
94757b06aeSJacek Anaszewski 		 * to restore it when the delay_off period is over.
95757b06aeSJacek Anaszewski 		 */
96757b06aeSJacek Anaszewski 		led_cdev->blink_brightness = brightness;
97757b06aeSJacek Anaszewski 		brightness = LED_OFF;
98757b06aeSJacek Anaszewski 		delay = led_cdev->blink_delay_off;
99757b06aeSJacek Anaszewski 	}
100757b06aeSJacek Anaszewski 
10181fe8e5bSJacek Anaszewski 	led_set_brightness_nosleep(led_cdev, brightness);
102757b06aeSJacek Anaszewski 
103757b06aeSJacek Anaszewski 	/* Return in next iteration if led is in one-shot mode and we are in
104757b06aeSJacek Anaszewski 	 * the final blink state so that the led is toggled each delay_on +
105757b06aeSJacek Anaszewski 	 * delay_off milliseconds in worst case.
106757b06aeSJacek Anaszewski 	 */
107a9c6ce57SHans de Goede 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
108a9c6ce57SHans de Goede 		if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
109757b06aeSJacek Anaszewski 			if (brightness)
110a9c6ce57SHans de Goede 				set_bit(LED_BLINK_ONESHOT_STOP,
111a9c6ce57SHans de Goede 					&led_cdev->work_flags);
112757b06aeSJacek Anaszewski 		} else {
113757b06aeSJacek Anaszewski 			if (!brightness)
114a9c6ce57SHans de Goede 				set_bit(LED_BLINK_ONESHOT_STOP,
115a9c6ce57SHans de Goede 					&led_cdev->work_flags);
116757b06aeSJacek Anaszewski 		}
117757b06aeSJacek Anaszewski 	}
118757b06aeSJacek Anaszewski 
119757b06aeSJacek Anaszewski 	mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
120757b06aeSJacek Anaszewski }
121757b06aeSJacek Anaszewski 
122fa15d8c6SHans de Goede static void set_brightness_delayed_set_brightness(struct led_classdev *led_cdev,
123fa15d8c6SHans de Goede 						  unsigned int value)
124757b06aeSJacek Anaszewski {
1251afcadfcSJacek Anaszewski 	int ret = 0;
126757b06aeSJacek Anaszewski 
127fa15d8c6SHans de Goede 	ret = __led_set_brightness(led_cdev, value);
128d4887af9SHeiner Kallweit 	if (ret == -ENOTSUPP)
129fa15d8c6SHans de Goede 		ret = __led_set_brightness_blocking(led_cdev, value);
130d84d80f3SHeiner Kallweit 	if (ret < 0 &&
131d84d80f3SHeiner Kallweit 	    /* LED HW might have been unplugged, therefore don't warn */
132d84d80f3SHeiner Kallweit 	    !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
133d84d80f3SHeiner Kallweit 	    (led_cdev->flags & LED_HW_PLUGGABLE)))
1341afcadfcSJacek Anaszewski 		dev_err(led_cdev->dev,
1351afcadfcSJacek Anaszewski 			"Setting an LED's brightness failed (%d)\n", ret);
136757b06aeSJacek Anaszewski }
137757b06aeSJacek Anaszewski 
138fa15d8c6SHans de Goede static void set_brightness_delayed(struct work_struct *ws)
139fa15d8c6SHans de Goede {
140fa15d8c6SHans de Goede 	struct led_classdev *led_cdev =
141fa15d8c6SHans de Goede 		container_of(ws, struct led_classdev, set_brightness_work);
142fa15d8c6SHans de Goede 
143fa15d8c6SHans de Goede 	if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
144fa15d8c6SHans de Goede 		led_stop_software_blink(led_cdev);
145fa15d8c6SHans de Goede 		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
146fa15d8c6SHans de Goede 	}
147fa15d8c6SHans de Goede 
148fa15d8c6SHans de Goede 	/*
149fa15d8c6SHans de Goede 	 * Triggers may call led_set_brightness(LED_OFF),
150fa15d8c6SHans de Goede 	 * led_set_brightness(LED_FULL) in quick succession to disable blinking
151fa15d8c6SHans de Goede 	 * and turn the LED on. Both actions may have been scheduled to run
152fa15d8c6SHans de Goede 	 * before this work item runs once. To make sure this works properly
153fa15d8c6SHans de Goede 	 * handle LED_SET_BRIGHTNESS_OFF first.
154fa15d8c6SHans de Goede 	 */
155fa15d8c6SHans de Goede 	if (test_and_clear_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags))
156fa15d8c6SHans de Goede 		set_brightness_delayed_set_brightness(led_cdev, LED_OFF);
157fa15d8c6SHans de Goede 
158fa15d8c6SHans de Goede 	if (test_and_clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags))
159fa15d8c6SHans de Goede 		set_brightness_delayed_set_brightness(led_cdev, led_cdev->delayed_set_value);
16022720a87SHans de Goede 
16122720a87SHans de Goede 	if (test_and_clear_bit(LED_SET_BLINK, &led_cdev->work_flags)) {
16222720a87SHans de Goede 		unsigned long delay_on = led_cdev->delayed_delay_on;
16322720a87SHans de Goede 		unsigned long delay_off = led_cdev->delayed_delay_off;
16422720a87SHans de Goede 
16522720a87SHans de Goede 		led_blink_set(led_cdev, &delay_on, &delay_off);
16622720a87SHans de Goede 	}
167fa15d8c6SHans de Goede }
168fa15d8c6SHans de Goede 
169a403d930SBryan Wu static void led_set_software_blink(struct led_classdev *led_cdev,
170a403d930SBryan Wu 				   unsigned long delay_on,
171a403d930SBryan Wu 				   unsigned long delay_off)
172a403d930SBryan Wu {
173a403d930SBryan Wu 	int current_brightness;
174a403d930SBryan Wu 
175a403d930SBryan Wu 	current_brightness = led_get_brightness(led_cdev);
176a403d930SBryan Wu 	if (current_brightness)
177a403d930SBryan Wu 		led_cdev->blink_brightness = current_brightness;
178a403d930SBryan Wu 	if (!led_cdev->blink_brightness)
179a403d930SBryan Wu 		led_cdev->blink_brightness = led_cdev->max_brightness;
180a403d930SBryan Wu 
181a403d930SBryan Wu 	led_cdev->blink_delay_on = delay_on;
182a403d930SBryan Wu 	led_cdev->blink_delay_off = delay_off;
183a403d930SBryan Wu 
1848d82fef8SStefan Sørensen 	/* never on - just set to off */
1858d82fef8SStefan Sørensen 	if (!delay_on) {
18681fe8e5bSJacek Anaszewski 		led_set_brightness_nosleep(led_cdev, LED_OFF);
187a403d930SBryan Wu 		return;
1888d82fef8SStefan Sørensen 	}
189a403d930SBryan Wu 
190a403d930SBryan Wu 	/* never off - just set to brightness */
191a403d930SBryan Wu 	if (!delay_off) {
19281fe8e5bSJacek Anaszewski 		led_set_brightness_nosleep(led_cdev,
19381fe8e5bSJacek Anaszewski 					   led_cdev->blink_brightness);
194a403d930SBryan Wu 		return;
195a403d930SBryan Wu 	}
196a403d930SBryan Wu 
197a9c6ce57SHans de Goede 	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
1989067359fSJiri Kosina 	mod_timer(&led_cdev->blink_timer, jiffies + 1);
199a403d930SBryan Wu }
200a403d930SBryan Wu 
201a403d930SBryan Wu 
20220c0e6b8SBryan Wu static void led_blink_setup(struct led_classdev *led_cdev,
203a403d930SBryan Wu 		     unsigned long *delay_on,
204a403d930SBryan Wu 		     unsigned long *delay_off)
205a403d930SBryan Wu {
206a9c6ce57SHans de Goede 	if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
2075bb629c5SFabio Baltieri 	    led_cdev->blink_set &&
208a403d930SBryan Wu 	    !led_cdev->blink_set(led_cdev, delay_on, delay_off))
209a403d930SBryan Wu 		return;
210a403d930SBryan Wu 
211a403d930SBryan Wu 	/* blink with 1 Hz as default if nothing specified */
212a403d930SBryan Wu 	if (!*delay_on && !*delay_off)
213a403d930SBryan Wu 		*delay_on = *delay_off = 500;
214a403d930SBryan Wu 
215a403d930SBryan Wu 	led_set_software_blink(led_cdev, *delay_on, *delay_off);
216a403d930SBryan Wu }
2175bb629c5SFabio Baltieri 
218757b06aeSJacek Anaszewski void led_init_core(struct led_classdev *led_cdev)
219757b06aeSJacek Anaszewski {
220757b06aeSJacek Anaszewski 	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
221757b06aeSJacek Anaszewski 
22249404665SKees Cook 	timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
223757b06aeSJacek Anaszewski }
224757b06aeSJacek Anaszewski EXPORT_SYMBOL_GPL(led_init_core);
225757b06aeSJacek Anaszewski 
2265bb629c5SFabio Baltieri void led_blink_set(struct led_classdev *led_cdev,
2275bb629c5SFabio Baltieri 		   unsigned long *delay_on,
2285bb629c5SFabio Baltieri 		   unsigned long *delay_off)
2295bb629c5SFabio Baltieri {
2307b6af2c5SJacek Anaszewski 	del_timer_sync(&led_cdev->blink_timer);
2315bb629c5SFabio Baltieri 
2327b6af2c5SJacek Anaszewski 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
233a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
234a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
2355bb629c5SFabio Baltieri 
2365bb629c5SFabio Baltieri 	led_blink_setup(led_cdev, delay_on, delay_off);
2375bb629c5SFabio Baltieri }
2382806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_blink_set);
239a403d930SBryan Wu 
2405bb629c5SFabio Baltieri void led_blink_set_oneshot(struct led_classdev *led_cdev,
2415bb629c5SFabio Baltieri 			   unsigned long *delay_on,
2425bb629c5SFabio Baltieri 			   unsigned long *delay_off,
2435bb629c5SFabio Baltieri 			   int invert)
2445bb629c5SFabio Baltieri {
245a9c6ce57SHans de Goede 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
2469067359fSJiri Kosina 	     timer_pending(&led_cdev->blink_timer))
2475bb629c5SFabio Baltieri 		return;
2485bb629c5SFabio Baltieri 
249a9c6ce57SHans de Goede 	set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
250a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
2515bb629c5SFabio Baltieri 
2525bb629c5SFabio Baltieri 	if (invert)
253a9c6ce57SHans de Goede 		set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
2545bb629c5SFabio Baltieri 	else
255a9c6ce57SHans de Goede 		clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
2565bb629c5SFabio Baltieri 
2575bb629c5SFabio Baltieri 	led_blink_setup(led_cdev, delay_on, delay_off);
2585bb629c5SFabio Baltieri }
2592806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
2605bb629c5SFabio Baltieri 
26122720a87SHans de Goede void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on,
26222720a87SHans de Goede 			   unsigned long delay_off)
26322720a87SHans de Goede {
26422720a87SHans de Goede 	/* If necessary delegate to a work queue task. */
26522720a87SHans de Goede 	if (led_cdev->blink_set && led_cdev->brightness_set_blocking) {
26622720a87SHans de Goede 		led_cdev->delayed_delay_on = delay_on;
26722720a87SHans de Goede 		led_cdev->delayed_delay_off = delay_off;
26822720a87SHans de Goede 		set_bit(LED_SET_BLINK, &led_cdev->work_flags);
26922720a87SHans de Goede 		schedule_work(&led_cdev->set_brightness_work);
27022720a87SHans de Goede 		return;
27122720a87SHans de Goede 	}
27222720a87SHans de Goede 
27322720a87SHans de Goede 	led_blink_set(led_cdev, &delay_on, &delay_off);
27422720a87SHans de Goede }
27522720a87SHans de Goede EXPORT_SYMBOL_GPL(led_blink_set_nosleep);
27622720a87SHans de Goede 
277d23a22a7SFabio Baltieri void led_stop_software_blink(struct led_classdev *led_cdev)
278a403d930SBryan Wu {
2799067359fSJiri Kosina 	del_timer_sync(&led_cdev->blink_timer);
28043786482SFabio Baltieri 	led_cdev->blink_delay_on = 0;
28143786482SFabio Baltieri 	led_cdev->blink_delay_off = 0;
282a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
283d23a22a7SFabio Baltieri }
284d23a22a7SFabio Baltieri EXPORT_SYMBOL_GPL(led_stop_software_blink);
285d23a22a7SFabio Baltieri 
286af0bfab9SAbanoub Sameh void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
287d23a22a7SFabio Baltieri {
288f1e80c07SJacek Anaszewski 	/*
2897cfe749fSTony Makkiel 	 * If software blink is active, delay brightness setting
290f1e80c07SJacek Anaszewski 	 * until the next timer tick.
291f1e80c07SJacek Anaszewski 	 */
292a9c6ce57SHans de Goede 	if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
293f1e80c07SJacek Anaszewski 		/*
294f1e80c07SJacek Anaszewski 		 * If we need to disable soft blinking delegate this to the
295f1e80c07SJacek Anaszewski 		 * work queue task to avoid problems in case we are called
296f1e80c07SJacek Anaszewski 		 * from hard irq context.
297f1e80c07SJacek Anaszewski 		 */
298af0bfab9SAbanoub Sameh 		if (!brightness) {
299a9c6ce57SHans de Goede 			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
300d23a22a7SFabio Baltieri 			schedule_work(&led_cdev->set_brightness_work);
301f1e80c07SJacek Anaszewski 		} else {
302a9c6ce57SHans de Goede 			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
303a9c6ce57SHans de Goede 				&led_cdev->work_flags);
304eb1610b4SHans de Goede 			led_cdev->new_blink_brightness = brightness;
305f1e80c07SJacek Anaszewski 		}
306d23a22a7SFabio Baltieri 		return;
307d23a22a7SFabio Baltieri 	}
30843786482SFabio Baltieri 
30981fe8e5bSJacek Anaszewski 	led_set_brightness_nosleep(led_cdev, brightness);
310a403d930SBryan Wu }
3112806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness);
3123ef7de53SJacek Anaszewski 
313af0bfab9SAbanoub Sameh void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
31481fe8e5bSJacek Anaszewski {
31581fe8e5bSJacek Anaszewski 	/* Use brightness_set op if available, it is guaranteed not to sleep */
316d4887af9SHeiner Kallweit 	if (!__led_set_brightness(led_cdev, value))
31781fe8e5bSJacek Anaszewski 		return;
31881fe8e5bSJacek Anaszewski 
319fa15d8c6SHans de Goede 	/*
320fa15d8c6SHans de Goede 	 * Brightness setting can sleep, delegate it to a work queue task.
321fa15d8c6SHans de Goede 	 * value 0 / LED_OFF is special, since it also disables hw-blinking
322fa15d8c6SHans de Goede 	 * (sw-blink disable is handled in led_set_brightness()).
323fa15d8c6SHans de Goede 	 * To avoid a hw-blink-disable getting lost when a second brightness
324fa15d8c6SHans de Goede 	 * change is done immediately afterwards (before the work runs),
325fa15d8c6SHans de Goede 	 * it uses a separate work_flag.
326fa15d8c6SHans de Goede 	 */
327fa15d8c6SHans de Goede 	if (value) {
32881fe8e5bSJacek Anaszewski 		led_cdev->delayed_set_value = value;
329fa15d8c6SHans de Goede 		set_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
330fa15d8c6SHans de Goede 	} else {
331fa15d8c6SHans de Goede 		clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
33222720a87SHans de Goede 		clear_bit(LED_SET_BLINK, &led_cdev->work_flags);
333fa15d8c6SHans de Goede 		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
334fa15d8c6SHans de Goede 	}
335fa15d8c6SHans de Goede 
33681fe8e5bSJacek Anaszewski 	schedule_work(&led_cdev->set_brightness_work);
33781fe8e5bSJacek Anaszewski }
33881fe8e5bSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
33981fe8e5bSJacek Anaszewski 
340af0bfab9SAbanoub Sameh void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value)
34181fe8e5bSJacek Anaszewski {
34281fe8e5bSJacek Anaszewski 	led_cdev->brightness = min(value, led_cdev->max_brightness);
34381fe8e5bSJacek Anaszewski 
34481fe8e5bSJacek Anaszewski 	if (led_cdev->flags & LED_SUSPENDED)
34581fe8e5bSJacek Anaszewski 		return;
34681fe8e5bSJacek Anaszewski 
34781fe8e5bSJacek Anaszewski 	led_set_brightness_nopm(led_cdev, led_cdev->brightness);
34881fe8e5bSJacek Anaszewski }
34981fe8e5bSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
35081fe8e5bSJacek Anaszewski 
351af0bfab9SAbanoub Sameh int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
35213ae79bbSJacek Anaszewski {
35313ae79bbSJacek Anaszewski 	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
35413ae79bbSJacek Anaszewski 		return -EBUSY;
35513ae79bbSJacek Anaszewski 
35613ae79bbSJacek Anaszewski 	led_cdev->brightness = min(value, led_cdev->max_brightness);
35713ae79bbSJacek Anaszewski 
35813ae79bbSJacek Anaszewski 	if (led_cdev->flags & LED_SUSPENDED)
35913ae79bbSJacek Anaszewski 		return 0;
36013ae79bbSJacek Anaszewski 
361d4887af9SHeiner Kallweit 	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
36213ae79bbSJacek Anaszewski }
36313ae79bbSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_sync);
36413ae79bbSJacek Anaszewski 
3653ef7de53SJacek Anaszewski int led_update_brightness(struct led_classdev *led_cdev)
3663ef7de53SJacek Anaszewski {
3673ef7de53SJacek Anaszewski 	int ret = 0;
3683ef7de53SJacek Anaszewski 
3693ef7de53SJacek Anaszewski 	if (led_cdev->brightness_get) {
3703ef7de53SJacek Anaszewski 		ret = led_cdev->brightness_get(led_cdev);
3713ef7de53SJacek Anaszewski 		if (ret >= 0) {
3723ef7de53SJacek Anaszewski 			led_cdev->brightness = ret;
3733ef7de53SJacek Anaszewski 			return 0;
3743ef7de53SJacek Anaszewski 		}
3753ef7de53SJacek Anaszewski 	}
3763ef7de53SJacek Anaszewski 
3773ef7de53SJacek Anaszewski 	return ret;
3783ef7de53SJacek Anaszewski }
3792806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_update_brightness);
380acd899e4SJacek Anaszewski 
3818e1f4561SKrzysztof Kozlowski u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
3828e1f4561SKrzysztof Kozlowski {
383fd81d7e9SAndy Shevchenko 	struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
3848e1f4561SKrzysztof Kozlowski 	u32 *pattern;
3858e1f4561SKrzysztof Kozlowski 	int count;
3868e1f4561SKrzysztof Kozlowski 
387fd81d7e9SAndy Shevchenko 	count = fwnode_property_count_u32(fwnode, "led-pattern");
3888e1f4561SKrzysztof Kozlowski 	if (count < 0)
3898e1f4561SKrzysztof Kozlowski 		return NULL;
3908e1f4561SKrzysztof Kozlowski 
3918e1f4561SKrzysztof Kozlowski 	pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
3928e1f4561SKrzysztof Kozlowski 	if (!pattern)
3938e1f4561SKrzysztof Kozlowski 		return NULL;
3948e1f4561SKrzysztof Kozlowski 
395fd81d7e9SAndy Shevchenko 	if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
3968e1f4561SKrzysztof Kozlowski 		kfree(pattern);
3978e1f4561SKrzysztof Kozlowski 		return NULL;
3988e1f4561SKrzysztof Kozlowski 	}
3998e1f4561SKrzysztof Kozlowski 
4008e1f4561SKrzysztof Kozlowski 	*size = count;
4018e1f4561SKrzysztof Kozlowski 
4028e1f4561SKrzysztof Kozlowski 	return pattern;
4038e1f4561SKrzysztof Kozlowski }
4048e1f4561SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(led_get_default_pattern);
4058e1f4561SKrzysztof Kozlowski 
406acd899e4SJacek Anaszewski /* Caller must ensure led_cdev->led_access held */
407acd899e4SJacek Anaszewski void led_sysfs_disable(struct led_classdev *led_cdev)
408acd899e4SJacek Anaszewski {
409acd899e4SJacek Anaszewski 	lockdep_assert_held(&led_cdev->led_access);
410acd899e4SJacek Anaszewski 
411acd899e4SJacek Anaszewski 	led_cdev->flags |= LED_SYSFS_DISABLE;
412acd899e4SJacek Anaszewski }
413acd899e4SJacek Anaszewski EXPORT_SYMBOL_GPL(led_sysfs_disable);
414acd899e4SJacek Anaszewski 
415acd899e4SJacek Anaszewski /* Caller must ensure led_cdev->led_access held */
416acd899e4SJacek Anaszewski void led_sysfs_enable(struct led_classdev *led_cdev)
417acd899e4SJacek Anaszewski {
418acd899e4SJacek Anaszewski 	lockdep_assert_held(&led_cdev->led_access);
419acd899e4SJacek Anaszewski 
420acd899e4SJacek Anaszewski 	led_cdev->flags &= ~LED_SYSFS_DISABLE;
421acd899e4SJacek Anaszewski }
422acd899e4SJacek Anaszewski EXPORT_SYMBOL_GPL(led_sysfs_enable);
423bb4e9af0SJacek Anaszewski 
424bb4e9af0SJacek Anaszewski static void led_parse_fwnode_props(struct device *dev,
425bb4e9af0SJacek Anaszewski 				   struct fwnode_handle *fwnode,
426bb4e9af0SJacek Anaszewski 				   struct led_properties *props)
427bb4e9af0SJacek Anaszewski {
428bb4e9af0SJacek Anaszewski 	int ret;
429bb4e9af0SJacek Anaszewski 
430bb4e9af0SJacek Anaszewski 	if (!fwnode)
431bb4e9af0SJacek Anaszewski 		return;
432bb4e9af0SJacek Anaszewski 
433bb4e9af0SJacek Anaszewski 	if (fwnode_property_present(fwnode, "label")) {
434bb4e9af0SJacek Anaszewski 		ret = fwnode_property_read_string(fwnode, "label", &props->label);
435bb4e9af0SJacek Anaszewski 		if (ret)
436bb4e9af0SJacek Anaszewski 			dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
437bb4e9af0SJacek Anaszewski 		return;
438bb4e9af0SJacek Anaszewski 	}
439bb4e9af0SJacek Anaszewski 
440bb4e9af0SJacek Anaszewski 	if (fwnode_property_present(fwnode, "color")) {
441bb4e9af0SJacek Anaszewski 		ret = fwnode_property_read_u32(fwnode, "color", &props->color);
442bb4e9af0SJacek Anaszewski 		if (ret)
443bb4e9af0SJacek Anaszewski 			dev_err(dev, "Error parsing 'color' property (%d)\n", ret);
444bb4e9af0SJacek Anaszewski 		else if (props->color >= LED_COLOR_ID_MAX)
445bb4e9af0SJacek Anaszewski 			dev_err(dev, "LED color identifier out of range\n");
446bb4e9af0SJacek Anaszewski 		else
447bb4e9af0SJacek Anaszewski 			props->color_present = true;
448bb4e9af0SJacek Anaszewski 	}
449bb4e9af0SJacek Anaszewski 
450bb4e9af0SJacek Anaszewski 
451bb4e9af0SJacek Anaszewski 	if (!fwnode_property_present(fwnode, "function"))
452bb4e9af0SJacek Anaszewski 		return;
453bb4e9af0SJacek Anaszewski 
454bb4e9af0SJacek Anaszewski 	ret = fwnode_property_read_string(fwnode, "function", &props->function);
455bb4e9af0SJacek Anaszewski 	if (ret) {
456bb4e9af0SJacek Anaszewski 		dev_err(dev,
457bb4e9af0SJacek Anaszewski 			"Error parsing 'function' property (%d)\n",
458bb4e9af0SJacek Anaszewski 			ret);
459bb4e9af0SJacek Anaszewski 	}
460bb4e9af0SJacek Anaszewski 
461bb4e9af0SJacek Anaszewski 	if (!fwnode_property_present(fwnode, "function-enumerator"))
462bb4e9af0SJacek Anaszewski 		return;
463bb4e9af0SJacek Anaszewski 
464bb4e9af0SJacek Anaszewski 	ret = fwnode_property_read_u32(fwnode, "function-enumerator",
465bb4e9af0SJacek Anaszewski 				       &props->func_enum);
466bb4e9af0SJacek Anaszewski 	if (ret) {
467bb4e9af0SJacek Anaszewski 		dev_err(dev,
468bb4e9af0SJacek Anaszewski 			"Error parsing 'function-enumerator' property (%d)\n",
469bb4e9af0SJacek Anaszewski 			ret);
470bb4e9af0SJacek Anaszewski 	} else {
471bb4e9af0SJacek Anaszewski 		props->func_enum_present = true;
472bb4e9af0SJacek Anaszewski 	}
473bb4e9af0SJacek Anaszewski }
474bb4e9af0SJacek Anaszewski 
475bb4e9af0SJacek Anaszewski int led_compose_name(struct device *dev, struct led_init_data *init_data,
476bb4e9af0SJacek Anaszewski 		     char *led_classdev_name)
477bb4e9af0SJacek Anaszewski {
478bb4e9af0SJacek Anaszewski 	struct led_properties props = {};
479bb4e9af0SJacek Anaszewski 	struct fwnode_handle *fwnode = init_data->fwnode;
480bb4e9af0SJacek Anaszewski 	const char *devicename = init_data->devicename;
481bb4e9af0SJacek Anaszewski 
482bb4e9af0SJacek Anaszewski 	if (!led_classdev_name)
483bb4e9af0SJacek Anaszewski 		return -EINVAL;
484bb4e9af0SJacek Anaszewski 
485bb4e9af0SJacek Anaszewski 	led_parse_fwnode_props(dev, fwnode, &props);
486bb4e9af0SJacek Anaszewski 
487c3f85318SMarek Behún 	/* We want to label LEDs that can produce full range of colors
488c3f85318SMarek Behún 	 * as RGB, not multicolor */
489c3f85318SMarek Behún 	BUG_ON(props.color == LED_COLOR_ID_MULTI);
490c3f85318SMarek Behún 
491bb4e9af0SJacek Anaszewski 	if (props.label) {
492bb4e9af0SJacek Anaszewski 		/*
493bb4e9af0SJacek Anaszewski 		 * If init_data.devicename is NULL, then it indicates that
494bb4e9af0SJacek Anaszewski 		 * DT label should be used as-is for LED class device name.
495bb4e9af0SJacek Anaszewski 		 * Otherwise the label is prepended with devicename to compose
496bb4e9af0SJacek Anaszewski 		 * the final LED class device name.
497bb4e9af0SJacek Anaszewski 		 */
498bb4e9af0SJacek Anaszewski 		if (!devicename) {
499bb4e9af0SJacek Anaszewski 			strscpy(led_classdev_name, props.label,
500bb4e9af0SJacek Anaszewski 				LED_MAX_NAME_SIZE);
501bb4e9af0SJacek Anaszewski 		} else {
502bb4e9af0SJacek Anaszewski 			snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
503bb4e9af0SJacek Anaszewski 				 devicename, props.label);
504bb4e9af0SJacek Anaszewski 		}
505bb4e9af0SJacek Anaszewski 	} else if (props.function || props.color_present) {
506bb4e9af0SJacek Anaszewski 		char tmp_buf[LED_MAX_NAME_SIZE];
507bb4e9af0SJacek Anaszewski 
508bb4e9af0SJacek Anaszewski 		if (props.func_enum_present) {
509bb4e9af0SJacek Anaszewski 			snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d",
510bb4e9af0SJacek Anaszewski 				 props.color_present ? led_colors[props.color] : "",
511bb4e9af0SJacek Anaszewski 				 props.function ?: "", props.func_enum);
512bb4e9af0SJacek Anaszewski 		} else {
513bb4e9af0SJacek Anaszewski 			snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s",
514bb4e9af0SJacek Anaszewski 				 props.color_present ? led_colors[props.color] : "",
515bb4e9af0SJacek Anaszewski 				 props.function ?: "");
516bb4e9af0SJacek Anaszewski 		}
517bb4e9af0SJacek Anaszewski 		if (init_data->devname_mandatory) {
518bb4e9af0SJacek Anaszewski 			snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
519bb4e9af0SJacek Anaszewski 				 devicename, tmp_buf);
520bb4e9af0SJacek Anaszewski 		} else {
521bb4e9af0SJacek Anaszewski 			strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE);
522bb4e9af0SJacek Anaszewski 
523bb4e9af0SJacek Anaszewski 		}
524bb4e9af0SJacek Anaszewski 	} else if (init_data->default_label) {
525bb4e9af0SJacek Anaszewski 		if (!devicename) {
526bb4e9af0SJacek Anaszewski 			dev_err(dev, "Legacy LED naming requires devicename segment");
527bb4e9af0SJacek Anaszewski 			return -EINVAL;
528bb4e9af0SJacek Anaszewski 		}
529bb4e9af0SJacek Anaszewski 		snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
530bb4e9af0SJacek Anaszewski 			 devicename, init_data->default_label);
531bb4e9af0SJacek Anaszewski 	} else if (is_of_node(fwnode)) {
532bb4e9af0SJacek Anaszewski 		strscpy(led_classdev_name, to_of_node(fwnode)->name,
533bb4e9af0SJacek Anaszewski 			LED_MAX_NAME_SIZE);
534bb4e9af0SJacek Anaszewski 	} else
535bb4e9af0SJacek Anaszewski 		return -EINVAL;
536bb4e9af0SJacek Anaszewski 
537bb4e9af0SJacek Anaszewski 	return 0;
538bb4e9af0SJacek Anaszewski }
539bb4e9af0SJacek Anaszewski EXPORT_SYMBOL_GPL(led_compose_name);
540791bc411SDenis Osterland-Heim 
541791bc411SDenis Osterland-Heim enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode)
542791bc411SDenis Osterland-Heim {
543791bc411SDenis Osterland-Heim 	const char *state = NULL;
544791bc411SDenis Osterland-Heim 
545791bc411SDenis Osterland-Heim 	if (!fwnode_property_read_string(fwnode, "default-state", &state)) {
546791bc411SDenis Osterland-Heim 		if (!strcmp(state, "keep"))
547791bc411SDenis Osterland-Heim 			return LEDS_DEFSTATE_KEEP;
548791bc411SDenis Osterland-Heim 		if (!strcmp(state, "on"))
549791bc411SDenis Osterland-Heim 			return LEDS_DEFSTATE_ON;
550791bc411SDenis Osterland-Heim 	}
551791bc411SDenis Osterland-Heim 
552791bc411SDenis Osterland-Heim 	return LEDS_DEFSTATE_OFF;
553791bc411SDenis Osterland-Heim }
554791bc411SDenis Osterland-Heim EXPORT_SYMBOL_GPL(led_init_default_state_get);
555