xref: /linux/drivers/leds/led-core.c (revision fa15d8c69238b352cc143cb9d8f2ca4594b94022)
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",
39bb4e9af0SJacek Anaszewski };
40bb4e9af0SJacek Anaszewski EXPORT_SYMBOL_GPL(led_colors);
41bb4e9af0SJacek Anaszewski 
42af0bfab9SAbanoub Sameh static int __led_set_brightness(struct led_classdev *led_cdev, unsigned int value)
43d4887af9SHeiner Kallweit {
44d4887af9SHeiner Kallweit 	if (!led_cdev->brightness_set)
45d4887af9SHeiner Kallweit 		return -ENOTSUPP;
46d4887af9SHeiner Kallweit 
47d4887af9SHeiner Kallweit 	led_cdev->brightness_set(led_cdev, value);
48d4887af9SHeiner Kallweit 
49d4887af9SHeiner Kallweit 	return 0;
50d4887af9SHeiner Kallweit }
51d4887af9SHeiner Kallweit 
52af0bfab9SAbanoub Sameh static int __led_set_brightness_blocking(struct led_classdev *led_cdev, unsigned int value)
53d4887af9SHeiner Kallweit {
54d4887af9SHeiner Kallweit 	if (!led_cdev->brightness_set_blocking)
55d4887af9SHeiner Kallweit 		return -ENOTSUPP;
56d4887af9SHeiner Kallweit 
57d4887af9SHeiner Kallweit 	return led_cdev->brightness_set_blocking(led_cdev, value);
58d4887af9SHeiner Kallweit }
59d4887af9SHeiner Kallweit 
6049404665SKees Cook static void led_timer_function(struct timer_list *t)
61757b06aeSJacek Anaszewski {
6249404665SKees Cook 	struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
63757b06aeSJacek Anaszewski 	unsigned long brightness;
64757b06aeSJacek Anaszewski 	unsigned long delay;
65757b06aeSJacek Anaszewski 
66757b06aeSJacek Anaszewski 	if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
6781fe8e5bSJacek Anaszewski 		led_set_brightness_nosleep(led_cdev, LED_OFF);
68a9c6ce57SHans de Goede 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
69757b06aeSJacek Anaszewski 		return;
70757b06aeSJacek Anaszewski 	}
71757b06aeSJacek Anaszewski 
72a9c6ce57SHans de Goede 	if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
73a9c6ce57SHans de Goede 			       &led_cdev->work_flags)) {
74a9c6ce57SHans de Goede 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
75757b06aeSJacek Anaszewski 		return;
76757b06aeSJacek Anaszewski 	}
77757b06aeSJacek Anaszewski 
78757b06aeSJacek Anaszewski 	brightness = led_get_brightness(led_cdev);
79757b06aeSJacek Anaszewski 	if (!brightness) {
80757b06aeSJacek Anaszewski 		/* Time to switch the LED on. */
81eb1610b4SHans de Goede 		if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE,
82eb1610b4SHans de Goede 					&led_cdev->work_flags))
83eb1610b4SHans de Goede 			brightness = led_cdev->new_blink_brightness;
84eb1610b4SHans de Goede 		else
85757b06aeSJacek Anaszewski 			brightness = led_cdev->blink_brightness;
86757b06aeSJacek Anaszewski 		delay = led_cdev->blink_delay_on;
87757b06aeSJacek Anaszewski 	} else {
88757b06aeSJacek Anaszewski 		/* Store the current brightness value to be able
89757b06aeSJacek Anaszewski 		 * to restore it when the delay_off period is over.
90757b06aeSJacek Anaszewski 		 */
91757b06aeSJacek Anaszewski 		led_cdev->blink_brightness = brightness;
92757b06aeSJacek Anaszewski 		brightness = LED_OFF;
93757b06aeSJacek Anaszewski 		delay = led_cdev->blink_delay_off;
94757b06aeSJacek Anaszewski 	}
95757b06aeSJacek Anaszewski 
9681fe8e5bSJacek Anaszewski 	led_set_brightness_nosleep(led_cdev, brightness);
97757b06aeSJacek Anaszewski 
98757b06aeSJacek Anaszewski 	/* Return in next iteration if led is in one-shot mode and we are in
99757b06aeSJacek Anaszewski 	 * the final blink state so that the led is toggled each delay_on +
100757b06aeSJacek Anaszewski 	 * delay_off milliseconds in worst case.
101757b06aeSJacek Anaszewski 	 */
102a9c6ce57SHans de Goede 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
103a9c6ce57SHans de Goede 		if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
104757b06aeSJacek Anaszewski 			if (brightness)
105a9c6ce57SHans de Goede 				set_bit(LED_BLINK_ONESHOT_STOP,
106a9c6ce57SHans de Goede 					&led_cdev->work_flags);
107757b06aeSJacek Anaszewski 		} else {
108757b06aeSJacek Anaszewski 			if (!brightness)
109a9c6ce57SHans de Goede 				set_bit(LED_BLINK_ONESHOT_STOP,
110a9c6ce57SHans de Goede 					&led_cdev->work_flags);
111757b06aeSJacek Anaszewski 		}
112757b06aeSJacek Anaszewski 	}
113757b06aeSJacek Anaszewski 
114757b06aeSJacek Anaszewski 	mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
115757b06aeSJacek Anaszewski }
116757b06aeSJacek Anaszewski 
117*fa15d8c6SHans de Goede static void set_brightness_delayed_set_brightness(struct led_classdev *led_cdev,
118*fa15d8c6SHans de Goede 						  unsigned int value)
119757b06aeSJacek Anaszewski {
1201afcadfcSJacek Anaszewski 	int ret = 0;
121757b06aeSJacek Anaszewski 
122*fa15d8c6SHans de Goede 	ret = __led_set_brightness(led_cdev, value);
123d4887af9SHeiner Kallweit 	if (ret == -ENOTSUPP)
124*fa15d8c6SHans de Goede 		ret = __led_set_brightness_blocking(led_cdev, value);
125d84d80f3SHeiner Kallweit 	if (ret < 0 &&
126d84d80f3SHeiner Kallweit 	    /* LED HW might have been unplugged, therefore don't warn */
127d84d80f3SHeiner Kallweit 	    !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
128d84d80f3SHeiner Kallweit 	    (led_cdev->flags & LED_HW_PLUGGABLE)))
1291afcadfcSJacek Anaszewski 		dev_err(led_cdev->dev,
1301afcadfcSJacek Anaszewski 			"Setting an LED's brightness failed (%d)\n", ret);
131757b06aeSJacek Anaszewski }
132757b06aeSJacek Anaszewski 
133*fa15d8c6SHans de Goede static void set_brightness_delayed(struct work_struct *ws)
134*fa15d8c6SHans de Goede {
135*fa15d8c6SHans de Goede 	struct led_classdev *led_cdev =
136*fa15d8c6SHans de Goede 		container_of(ws, struct led_classdev, set_brightness_work);
137*fa15d8c6SHans de Goede 
138*fa15d8c6SHans de Goede 	if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
139*fa15d8c6SHans de Goede 		led_stop_software_blink(led_cdev);
140*fa15d8c6SHans de Goede 		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
141*fa15d8c6SHans de Goede 	}
142*fa15d8c6SHans de Goede 
143*fa15d8c6SHans de Goede 	/*
144*fa15d8c6SHans de Goede 	 * Triggers may call led_set_brightness(LED_OFF),
145*fa15d8c6SHans de Goede 	 * led_set_brightness(LED_FULL) in quick succession to disable blinking
146*fa15d8c6SHans de Goede 	 * and turn the LED on. Both actions may have been scheduled to run
147*fa15d8c6SHans de Goede 	 * before this work item runs once. To make sure this works properly
148*fa15d8c6SHans de Goede 	 * handle LED_SET_BRIGHTNESS_OFF first.
149*fa15d8c6SHans de Goede 	 */
150*fa15d8c6SHans de Goede 	if (test_and_clear_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags))
151*fa15d8c6SHans de Goede 		set_brightness_delayed_set_brightness(led_cdev, LED_OFF);
152*fa15d8c6SHans de Goede 
153*fa15d8c6SHans de Goede 	if (test_and_clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags))
154*fa15d8c6SHans de Goede 		set_brightness_delayed_set_brightness(led_cdev, led_cdev->delayed_set_value);
155*fa15d8c6SHans de Goede }
156*fa15d8c6SHans de Goede 
157a403d930SBryan Wu static void led_set_software_blink(struct led_classdev *led_cdev,
158a403d930SBryan Wu 				   unsigned long delay_on,
159a403d930SBryan Wu 				   unsigned long delay_off)
160a403d930SBryan Wu {
161a403d930SBryan Wu 	int current_brightness;
162a403d930SBryan Wu 
163a403d930SBryan Wu 	current_brightness = led_get_brightness(led_cdev);
164a403d930SBryan Wu 	if (current_brightness)
165a403d930SBryan Wu 		led_cdev->blink_brightness = current_brightness;
166a403d930SBryan Wu 	if (!led_cdev->blink_brightness)
167a403d930SBryan Wu 		led_cdev->blink_brightness = led_cdev->max_brightness;
168a403d930SBryan Wu 
169a403d930SBryan Wu 	led_cdev->blink_delay_on = delay_on;
170a403d930SBryan Wu 	led_cdev->blink_delay_off = delay_off;
171a403d930SBryan Wu 
1728d82fef8SStefan Sørensen 	/* never on - just set to off */
1738d82fef8SStefan Sørensen 	if (!delay_on) {
17481fe8e5bSJacek Anaszewski 		led_set_brightness_nosleep(led_cdev, LED_OFF);
175a403d930SBryan Wu 		return;
1768d82fef8SStefan Sørensen 	}
177a403d930SBryan Wu 
178a403d930SBryan Wu 	/* never off - just set to brightness */
179a403d930SBryan Wu 	if (!delay_off) {
18081fe8e5bSJacek Anaszewski 		led_set_brightness_nosleep(led_cdev,
18181fe8e5bSJacek Anaszewski 					   led_cdev->blink_brightness);
182a403d930SBryan Wu 		return;
183a403d930SBryan Wu 	}
184a403d930SBryan Wu 
185a9c6ce57SHans de Goede 	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
1869067359fSJiri Kosina 	mod_timer(&led_cdev->blink_timer, jiffies + 1);
187a403d930SBryan Wu }
188a403d930SBryan Wu 
189a403d930SBryan Wu 
19020c0e6b8SBryan Wu static void led_blink_setup(struct led_classdev *led_cdev,
191a403d930SBryan Wu 		     unsigned long *delay_on,
192a403d930SBryan Wu 		     unsigned long *delay_off)
193a403d930SBryan Wu {
194a9c6ce57SHans de Goede 	if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
1955bb629c5SFabio Baltieri 	    led_cdev->blink_set &&
196a403d930SBryan Wu 	    !led_cdev->blink_set(led_cdev, delay_on, delay_off))
197a403d930SBryan Wu 		return;
198a403d930SBryan Wu 
199a403d930SBryan Wu 	/* blink with 1 Hz as default if nothing specified */
200a403d930SBryan Wu 	if (!*delay_on && !*delay_off)
201a403d930SBryan Wu 		*delay_on = *delay_off = 500;
202a403d930SBryan Wu 
203a403d930SBryan Wu 	led_set_software_blink(led_cdev, *delay_on, *delay_off);
204a403d930SBryan Wu }
2055bb629c5SFabio Baltieri 
206757b06aeSJacek Anaszewski void led_init_core(struct led_classdev *led_cdev)
207757b06aeSJacek Anaszewski {
208757b06aeSJacek Anaszewski 	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
209757b06aeSJacek Anaszewski 
21049404665SKees Cook 	timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
211757b06aeSJacek Anaszewski }
212757b06aeSJacek Anaszewski EXPORT_SYMBOL_GPL(led_init_core);
213757b06aeSJacek Anaszewski 
2145bb629c5SFabio Baltieri void led_blink_set(struct led_classdev *led_cdev,
2155bb629c5SFabio Baltieri 		   unsigned long *delay_on,
2165bb629c5SFabio Baltieri 		   unsigned long *delay_off)
2175bb629c5SFabio Baltieri {
2187b6af2c5SJacek Anaszewski 	del_timer_sync(&led_cdev->blink_timer);
2195bb629c5SFabio Baltieri 
2207b6af2c5SJacek Anaszewski 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
221a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
222a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
2235bb629c5SFabio Baltieri 
2245bb629c5SFabio Baltieri 	led_blink_setup(led_cdev, delay_on, delay_off);
2255bb629c5SFabio Baltieri }
2262806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_blink_set);
227a403d930SBryan Wu 
2285bb629c5SFabio Baltieri void led_blink_set_oneshot(struct led_classdev *led_cdev,
2295bb629c5SFabio Baltieri 			   unsigned long *delay_on,
2305bb629c5SFabio Baltieri 			   unsigned long *delay_off,
2315bb629c5SFabio Baltieri 			   int invert)
2325bb629c5SFabio Baltieri {
233a9c6ce57SHans de Goede 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
2349067359fSJiri Kosina 	     timer_pending(&led_cdev->blink_timer))
2355bb629c5SFabio Baltieri 		return;
2365bb629c5SFabio Baltieri 
237a9c6ce57SHans de Goede 	set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
238a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
2395bb629c5SFabio Baltieri 
2405bb629c5SFabio Baltieri 	if (invert)
241a9c6ce57SHans de Goede 		set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
2425bb629c5SFabio Baltieri 	else
243a9c6ce57SHans de Goede 		clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
2445bb629c5SFabio Baltieri 
2455bb629c5SFabio Baltieri 	led_blink_setup(led_cdev, delay_on, delay_off);
2465bb629c5SFabio Baltieri }
2472806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
2485bb629c5SFabio Baltieri 
249d23a22a7SFabio Baltieri void led_stop_software_blink(struct led_classdev *led_cdev)
250a403d930SBryan Wu {
2519067359fSJiri Kosina 	del_timer_sync(&led_cdev->blink_timer);
25243786482SFabio Baltieri 	led_cdev->blink_delay_on = 0;
25343786482SFabio Baltieri 	led_cdev->blink_delay_off = 0;
254a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
255d23a22a7SFabio Baltieri }
256d23a22a7SFabio Baltieri EXPORT_SYMBOL_GPL(led_stop_software_blink);
257d23a22a7SFabio Baltieri 
258af0bfab9SAbanoub Sameh void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
259d23a22a7SFabio Baltieri {
260f1e80c07SJacek Anaszewski 	/*
2617cfe749fSTony Makkiel 	 * If software blink is active, delay brightness setting
262f1e80c07SJacek Anaszewski 	 * until the next timer tick.
263f1e80c07SJacek Anaszewski 	 */
264a9c6ce57SHans de Goede 	if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
265f1e80c07SJacek Anaszewski 		/*
266f1e80c07SJacek Anaszewski 		 * If we need to disable soft blinking delegate this to the
267f1e80c07SJacek Anaszewski 		 * work queue task to avoid problems in case we are called
268f1e80c07SJacek Anaszewski 		 * from hard irq context.
269f1e80c07SJacek Anaszewski 		 */
270af0bfab9SAbanoub Sameh 		if (!brightness) {
271a9c6ce57SHans de Goede 			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
272d23a22a7SFabio Baltieri 			schedule_work(&led_cdev->set_brightness_work);
273f1e80c07SJacek Anaszewski 		} else {
274a9c6ce57SHans de Goede 			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
275a9c6ce57SHans de Goede 				&led_cdev->work_flags);
276eb1610b4SHans de Goede 			led_cdev->new_blink_brightness = brightness;
277f1e80c07SJacek Anaszewski 		}
278d23a22a7SFabio Baltieri 		return;
279d23a22a7SFabio Baltieri 	}
28043786482SFabio Baltieri 
28181fe8e5bSJacek Anaszewski 	led_set_brightness_nosleep(led_cdev, brightness);
282a403d930SBryan Wu }
2832806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness);
2843ef7de53SJacek Anaszewski 
285af0bfab9SAbanoub Sameh void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
28681fe8e5bSJacek Anaszewski {
28781fe8e5bSJacek Anaszewski 	/* Use brightness_set op if available, it is guaranteed not to sleep */
288d4887af9SHeiner Kallweit 	if (!__led_set_brightness(led_cdev, value))
28981fe8e5bSJacek Anaszewski 		return;
29081fe8e5bSJacek Anaszewski 
291*fa15d8c6SHans de Goede 	/*
292*fa15d8c6SHans de Goede 	 * Brightness setting can sleep, delegate it to a work queue task.
293*fa15d8c6SHans de Goede 	 * value 0 / LED_OFF is special, since it also disables hw-blinking
294*fa15d8c6SHans de Goede 	 * (sw-blink disable is handled in led_set_brightness()).
295*fa15d8c6SHans de Goede 	 * To avoid a hw-blink-disable getting lost when a second brightness
296*fa15d8c6SHans de Goede 	 * change is done immediately afterwards (before the work runs),
297*fa15d8c6SHans de Goede 	 * it uses a separate work_flag.
298*fa15d8c6SHans de Goede 	 */
299*fa15d8c6SHans de Goede 	if (value) {
30081fe8e5bSJacek Anaszewski 		led_cdev->delayed_set_value = value;
301*fa15d8c6SHans de Goede 		set_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
302*fa15d8c6SHans de Goede 	} else {
303*fa15d8c6SHans de Goede 		clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
304*fa15d8c6SHans de Goede 		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
305*fa15d8c6SHans de Goede 	}
306*fa15d8c6SHans de Goede 
30781fe8e5bSJacek Anaszewski 	schedule_work(&led_cdev->set_brightness_work);
30881fe8e5bSJacek Anaszewski }
30981fe8e5bSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
31081fe8e5bSJacek Anaszewski 
311af0bfab9SAbanoub Sameh void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value)
31281fe8e5bSJacek Anaszewski {
31381fe8e5bSJacek Anaszewski 	led_cdev->brightness = min(value, led_cdev->max_brightness);
31481fe8e5bSJacek Anaszewski 
31581fe8e5bSJacek Anaszewski 	if (led_cdev->flags & LED_SUSPENDED)
31681fe8e5bSJacek Anaszewski 		return;
31781fe8e5bSJacek Anaszewski 
31881fe8e5bSJacek Anaszewski 	led_set_brightness_nopm(led_cdev, led_cdev->brightness);
31981fe8e5bSJacek Anaszewski }
32081fe8e5bSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
32181fe8e5bSJacek Anaszewski 
322af0bfab9SAbanoub Sameh int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
32313ae79bbSJacek Anaszewski {
32413ae79bbSJacek Anaszewski 	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
32513ae79bbSJacek Anaszewski 		return -EBUSY;
32613ae79bbSJacek Anaszewski 
32713ae79bbSJacek Anaszewski 	led_cdev->brightness = min(value, led_cdev->max_brightness);
32813ae79bbSJacek Anaszewski 
32913ae79bbSJacek Anaszewski 	if (led_cdev->flags & LED_SUSPENDED)
33013ae79bbSJacek Anaszewski 		return 0;
33113ae79bbSJacek Anaszewski 
332d4887af9SHeiner Kallweit 	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
33313ae79bbSJacek Anaszewski }
33413ae79bbSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_sync);
33513ae79bbSJacek Anaszewski 
3363ef7de53SJacek Anaszewski int led_update_brightness(struct led_classdev *led_cdev)
3373ef7de53SJacek Anaszewski {
3383ef7de53SJacek Anaszewski 	int ret = 0;
3393ef7de53SJacek Anaszewski 
3403ef7de53SJacek Anaszewski 	if (led_cdev->brightness_get) {
3413ef7de53SJacek Anaszewski 		ret = led_cdev->brightness_get(led_cdev);
3423ef7de53SJacek Anaszewski 		if (ret >= 0) {
3433ef7de53SJacek Anaszewski 			led_cdev->brightness = ret;
3443ef7de53SJacek Anaszewski 			return 0;
3453ef7de53SJacek Anaszewski 		}
3463ef7de53SJacek Anaszewski 	}
3473ef7de53SJacek Anaszewski 
3483ef7de53SJacek Anaszewski 	return ret;
3493ef7de53SJacek Anaszewski }
3502806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_update_brightness);
351acd899e4SJacek Anaszewski 
3528e1f4561SKrzysztof Kozlowski u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
3538e1f4561SKrzysztof Kozlowski {
354fd81d7e9SAndy Shevchenko 	struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
3558e1f4561SKrzysztof Kozlowski 	u32 *pattern;
3568e1f4561SKrzysztof Kozlowski 	int count;
3578e1f4561SKrzysztof Kozlowski 
358fd81d7e9SAndy Shevchenko 	count = fwnode_property_count_u32(fwnode, "led-pattern");
3598e1f4561SKrzysztof Kozlowski 	if (count < 0)
3608e1f4561SKrzysztof Kozlowski 		return NULL;
3618e1f4561SKrzysztof Kozlowski 
3628e1f4561SKrzysztof Kozlowski 	pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
3638e1f4561SKrzysztof Kozlowski 	if (!pattern)
3648e1f4561SKrzysztof Kozlowski 		return NULL;
3658e1f4561SKrzysztof Kozlowski 
366fd81d7e9SAndy Shevchenko 	if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
3678e1f4561SKrzysztof Kozlowski 		kfree(pattern);
3688e1f4561SKrzysztof Kozlowski 		return NULL;
3698e1f4561SKrzysztof Kozlowski 	}
3708e1f4561SKrzysztof Kozlowski 
3718e1f4561SKrzysztof Kozlowski 	*size = count;
3728e1f4561SKrzysztof Kozlowski 
3738e1f4561SKrzysztof Kozlowski 	return pattern;
3748e1f4561SKrzysztof Kozlowski }
3758e1f4561SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(led_get_default_pattern);
3768e1f4561SKrzysztof Kozlowski 
377acd899e4SJacek Anaszewski /* Caller must ensure led_cdev->led_access held */
378acd899e4SJacek Anaszewski void led_sysfs_disable(struct led_classdev *led_cdev)
379acd899e4SJacek Anaszewski {
380acd899e4SJacek Anaszewski 	lockdep_assert_held(&led_cdev->led_access);
381acd899e4SJacek Anaszewski 
382acd899e4SJacek Anaszewski 	led_cdev->flags |= LED_SYSFS_DISABLE;
383acd899e4SJacek Anaszewski }
384acd899e4SJacek Anaszewski EXPORT_SYMBOL_GPL(led_sysfs_disable);
385acd899e4SJacek Anaszewski 
386acd899e4SJacek Anaszewski /* Caller must ensure led_cdev->led_access held */
387acd899e4SJacek Anaszewski void led_sysfs_enable(struct led_classdev *led_cdev)
388acd899e4SJacek Anaszewski {
389acd899e4SJacek Anaszewski 	lockdep_assert_held(&led_cdev->led_access);
390acd899e4SJacek Anaszewski 
391acd899e4SJacek Anaszewski 	led_cdev->flags &= ~LED_SYSFS_DISABLE;
392acd899e4SJacek Anaszewski }
393acd899e4SJacek Anaszewski EXPORT_SYMBOL_GPL(led_sysfs_enable);
394bb4e9af0SJacek Anaszewski 
395bb4e9af0SJacek Anaszewski static void led_parse_fwnode_props(struct device *dev,
396bb4e9af0SJacek Anaszewski 				   struct fwnode_handle *fwnode,
397bb4e9af0SJacek Anaszewski 				   struct led_properties *props)
398bb4e9af0SJacek Anaszewski {
399bb4e9af0SJacek Anaszewski 	int ret;
400bb4e9af0SJacek Anaszewski 
401bb4e9af0SJacek Anaszewski 	if (!fwnode)
402bb4e9af0SJacek Anaszewski 		return;
403bb4e9af0SJacek Anaszewski 
404bb4e9af0SJacek Anaszewski 	if (fwnode_property_present(fwnode, "label")) {
405bb4e9af0SJacek Anaszewski 		ret = fwnode_property_read_string(fwnode, "label", &props->label);
406bb4e9af0SJacek Anaszewski 		if (ret)
407bb4e9af0SJacek Anaszewski 			dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
408bb4e9af0SJacek Anaszewski 		return;
409bb4e9af0SJacek Anaszewski 	}
410bb4e9af0SJacek Anaszewski 
411bb4e9af0SJacek Anaszewski 	if (fwnode_property_present(fwnode, "color")) {
412bb4e9af0SJacek Anaszewski 		ret = fwnode_property_read_u32(fwnode, "color", &props->color);
413bb4e9af0SJacek Anaszewski 		if (ret)
414bb4e9af0SJacek Anaszewski 			dev_err(dev, "Error parsing 'color' property (%d)\n", ret);
415bb4e9af0SJacek Anaszewski 		else if (props->color >= LED_COLOR_ID_MAX)
416bb4e9af0SJacek Anaszewski 			dev_err(dev, "LED color identifier out of range\n");
417bb4e9af0SJacek Anaszewski 		else
418bb4e9af0SJacek Anaszewski 			props->color_present = true;
419bb4e9af0SJacek Anaszewski 	}
420bb4e9af0SJacek Anaszewski 
421bb4e9af0SJacek Anaszewski 
422bb4e9af0SJacek Anaszewski 	if (!fwnode_property_present(fwnode, "function"))
423bb4e9af0SJacek Anaszewski 		return;
424bb4e9af0SJacek Anaszewski 
425bb4e9af0SJacek Anaszewski 	ret = fwnode_property_read_string(fwnode, "function", &props->function);
426bb4e9af0SJacek Anaszewski 	if (ret) {
427bb4e9af0SJacek Anaszewski 		dev_err(dev,
428bb4e9af0SJacek Anaszewski 			"Error parsing 'function' property (%d)\n",
429bb4e9af0SJacek Anaszewski 			ret);
430bb4e9af0SJacek Anaszewski 	}
431bb4e9af0SJacek Anaszewski 
432bb4e9af0SJacek Anaszewski 	if (!fwnode_property_present(fwnode, "function-enumerator"))
433bb4e9af0SJacek Anaszewski 		return;
434bb4e9af0SJacek Anaszewski 
435bb4e9af0SJacek Anaszewski 	ret = fwnode_property_read_u32(fwnode, "function-enumerator",
436bb4e9af0SJacek Anaszewski 				       &props->func_enum);
437bb4e9af0SJacek Anaszewski 	if (ret) {
438bb4e9af0SJacek Anaszewski 		dev_err(dev,
439bb4e9af0SJacek Anaszewski 			"Error parsing 'function-enumerator' property (%d)\n",
440bb4e9af0SJacek Anaszewski 			ret);
441bb4e9af0SJacek Anaszewski 	} else {
442bb4e9af0SJacek Anaszewski 		props->func_enum_present = true;
443bb4e9af0SJacek Anaszewski 	}
444bb4e9af0SJacek Anaszewski }
445bb4e9af0SJacek Anaszewski 
446bb4e9af0SJacek Anaszewski int led_compose_name(struct device *dev, struct led_init_data *init_data,
447bb4e9af0SJacek Anaszewski 		     char *led_classdev_name)
448bb4e9af0SJacek Anaszewski {
449bb4e9af0SJacek Anaszewski 	struct led_properties props = {};
450bb4e9af0SJacek Anaszewski 	struct fwnode_handle *fwnode = init_data->fwnode;
451bb4e9af0SJacek Anaszewski 	const char *devicename = init_data->devicename;
452bb4e9af0SJacek Anaszewski 
45377dce3a2SPavel Machek 	/* We want to label LEDs that can produce full range of colors
45477dce3a2SPavel Machek 	 * as RGB, not multicolor */
45577dce3a2SPavel Machek 	BUG_ON(props.color == LED_COLOR_ID_MULTI);
45677dce3a2SPavel Machek 
457bb4e9af0SJacek Anaszewski 	if (!led_classdev_name)
458bb4e9af0SJacek Anaszewski 		return -EINVAL;
459bb4e9af0SJacek Anaszewski 
460bb4e9af0SJacek Anaszewski 	led_parse_fwnode_props(dev, fwnode, &props);
461bb4e9af0SJacek Anaszewski 
462bb4e9af0SJacek Anaszewski 	if (props.label) {
463bb4e9af0SJacek Anaszewski 		/*
464bb4e9af0SJacek Anaszewski 		 * If init_data.devicename is NULL, then it indicates that
465bb4e9af0SJacek Anaszewski 		 * DT label should be used as-is for LED class device name.
466bb4e9af0SJacek Anaszewski 		 * Otherwise the label is prepended with devicename to compose
467bb4e9af0SJacek Anaszewski 		 * the final LED class device name.
468bb4e9af0SJacek Anaszewski 		 */
469bb4e9af0SJacek Anaszewski 		if (!devicename) {
470bb4e9af0SJacek Anaszewski 			strscpy(led_classdev_name, props.label,
471bb4e9af0SJacek Anaszewski 				LED_MAX_NAME_SIZE);
472bb4e9af0SJacek Anaszewski 		} else {
473bb4e9af0SJacek Anaszewski 			snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
474bb4e9af0SJacek Anaszewski 				 devicename, props.label);
475bb4e9af0SJacek Anaszewski 		}
476bb4e9af0SJacek Anaszewski 	} else if (props.function || props.color_present) {
477bb4e9af0SJacek Anaszewski 		char tmp_buf[LED_MAX_NAME_SIZE];
478bb4e9af0SJacek Anaszewski 
479bb4e9af0SJacek Anaszewski 		if (props.func_enum_present) {
480bb4e9af0SJacek Anaszewski 			snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d",
481bb4e9af0SJacek Anaszewski 				 props.color_present ? led_colors[props.color] : "",
482bb4e9af0SJacek Anaszewski 				 props.function ?: "", props.func_enum);
483bb4e9af0SJacek Anaszewski 		} else {
484bb4e9af0SJacek Anaszewski 			snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s",
485bb4e9af0SJacek Anaszewski 				 props.color_present ? led_colors[props.color] : "",
486bb4e9af0SJacek Anaszewski 				 props.function ?: "");
487bb4e9af0SJacek Anaszewski 		}
488bb4e9af0SJacek Anaszewski 		if (init_data->devname_mandatory) {
489bb4e9af0SJacek Anaszewski 			snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
490bb4e9af0SJacek Anaszewski 				 devicename, tmp_buf);
491bb4e9af0SJacek Anaszewski 		} else {
492bb4e9af0SJacek Anaszewski 			strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE);
493bb4e9af0SJacek Anaszewski 
494bb4e9af0SJacek Anaszewski 		}
495bb4e9af0SJacek Anaszewski 	} else if (init_data->default_label) {
496bb4e9af0SJacek Anaszewski 		if (!devicename) {
497bb4e9af0SJacek Anaszewski 			dev_err(dev, "Legacy LED naming requires devicename segment");
498bb4e9af0SJacek Anaszewski 			return -EINVAL;
499bb4e9af0SJacek Anaszewski 		}
500bb4e9af0SJacek Anaszewski 		snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
501bb4e9af0SJacek Anaszewski 			 devicename, init_data->default_label);
502bb4e9af0SJacek Anaszewski 	} else if (is_of_node(fwnode)) {
503bb4e9af0SJacek Anaszewski 		strscpy(led_classdev_name, to_of_node(fwnode)->name,
504bb4e9af0SJacek Anaszewski 			LED_MAX_NAME_SIZE);
505bb4e9af0SJacek Anaszewski 	} else
506bb4e9af0SJacek Anaszewski 		return -EINVAL;
507bb4e9af0SJacek Anaszewski 
508bb4e9af0SJacek Anaszewski 	return 0;
509bb4e9af0SJacek Anaszewski }
510bb4e9af0SJacek Anaszewski EXPORT_SYMBOL_GPL(led_compose_name);
511791bc411SDenis Osterland-Heim 
512791bc411SDenis Osterland-Heim enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode)
513791bc411SDenis Osterland-Heim {
514791bc411SDenis Osterland-Heim 	const char *state = NULL;
515791bc411SDenis Osterland-Heim 
516791bc411SDenis Osterland-Heim 	if (!fwnode_property_read_string(fwnode, "default-state", &state)) {
517791bc411SDenis Osterland-Heim 		if (!strcmp(state, "keep"))
518791bc411SDenis Osterland-Heim 			return LEDS_DEFSTATE_KEEP;
519791bc411SDenis Osterland-Heim 		if (!strcmp(state, "on"))
520791bc411SDenis Osterland-Heim 			return LEDS_DEFSTATE_ON;
521791bc411SDenis Osterland-Heim 	}
522791bc411SDenis Osterland-Heim 
523791bc411SDenis Osterland-Heim 	return LEDS_DEFSTATE_OFF;
524791bc411SDenis Osterland-Heim }
525791bc411SDenis Osterland-Heim EXPORT_SYMBOL_GPL(led_init_default_state_get);
526