xref: /linux/drivers/leds/led-core.c (revision 791bc41163c51f870972d6c6b82d971ce951096c)
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 
117757b06aeSJacek Anaszewski static void set_brightness_delayed(struct work_struct *ws)
118757b06aeSJacek Anaszewski {
119757b06aeSJacek Anaszewski 	struct led_classdev *led_cdev =
120757b06aeSJacek Anaszewski 		container_of(ws, struct led_classdev, set_brightness_work);
1211afcadfcSJacek Anaszewski 	int ret = 0;
122757b06aeSJacek Anaszewski 
123a9c6ce57SHans de Goede 	if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
124f1e80c07SJacek Anaszewski 		led_cdev->delayed_set_value = LED_OFF;
125757b06aeSJacek Anaszewski 		led_stop_software_blink(led_cdev);
126f1e80c07SJacek Anaszewski 	}
127757b06aeSJacek Anaszewski 
128d4887af9SHeiner Kallweit 	ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
129d4887af9SHeiner Kallweit 	if (ret == -ENOTSUPP)
130d4887af9SHeiner Kallweit 		ret = __led_set_brightness_blocking(led_cdev,
1311afcadfcSJacek Anaszewski 					led_cdev->delayed_set_value);
132d84d80f3SHeiner Kallweit 	if (ret < 0 &&
133d84d80f3SHeiner Kallweit 	    /* LED HW might have been unplugged, therefore don't warn */
134d84d80f3SHeiner Kallweit 	    !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
135d84d80f3SHeiner Kallweit 	    (led_cdev->flags & LED_HW_PLUGGABLE)))
1361afcadfcSJacek Anaszewski 		dev_err(led_cdev->dev,
1371afcadfcSJacek Anaszewski 			"Setting an LED's brightness failed (%d)\n", ret);
138757b06aeSJacek Anaszewski }
139757b06aeSJacek Anaszewski 
140a403d930SBryan Wu static void led_set_software_blink(struct led_classdev *led_cdev,
141a403d930SBryan Wu 				   unsigned long delay_on,
142a403d930SBryan Wu 				   unsigned long delay_off)
143a403d930SBryan Wu {
144a403d930SBryan Wu 	int current_brightness;
145a403d930SBryan Wu 
146a403d930SBryan Wu 	current_brightness = led_get_brightness(led_cdev);
147a403d930SBryan Wu 	if (current_brightness)
148a403d930SBryan Wu 		led_cdev->blink_brightness = current_brightness;
149a403d930SBryan Wu 	if (!led_cdev->blink_brightness)
150a403d930SBryan Wu 		led_cdev->blink_brightness = led_cdev->max_brightness;
151a403d930SBryan Wu 
152a403d930SBryan Wu 	led_cdev->blink_delay_on = delay_on;
153a403d930SBryan Wu 	led_cdev->blink_delay_off = delay_off;
154a403d930SBryan Wu 
1558d82fef8SStefan Sørensen 	/* never on - just set to off */
1568d82fef8SStefan Sørensen 	if (!delay_on) {
15781fe8e5bSJacek Anaszewski 		led_set_brightness_nosleep(led_cdev, LED_OFF);
158a403d930SBryan Wu 		return;
1598d82fef8SStefan Sørensen 	}
160a403d930SBryan Wu 
161a403d930SBryan Wu 	/* never off - just set to brightness */
162a403d930SBryan Wu 	if (!delay_off) {
16381fe8e5bSJacek Anaszewski 		led_set_brightness_nosleep(led_cdev,
16481fe8e5bSJacek Anaszewski 					   led_cdev->blink_brightness);
165a403d930SBryan Wu 		return;
166a403d930SBryan Wu 	}
167a403d930SBryan Wu 
168a9c6ce57SHans de Goede 	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
1699067359fSJiri Kosina 	mod_timer(&led_cdev->blink_timer, jiffies + 1);
170a403d930SBryan Wu }
171a403d930SBryan Wu 
172a403d930SBryan Wu 
17320c0e6b8SBryan Wu static void led_blink_setup(struct led_classdev *led_cdev,
174a403d930SBryan Wu 		     unsigned long *delay_on,
175a403d930SBryan Wu 		     unsigned long *delay_off)
176a403d930SBryan Wu {
177a9c6ce57SHans de Goede 	if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
1785bb629c5SFabio Baltieri 	    led_cdev->blink_set &&
179a403d930SBryan Wu 	    !led_cdev->blink_set(led_cdev, delay_on, delay_off))
180a403d930SBryan Wu 		return;
181a403d930SBryan Wu 
182a403d930SBryan Wu 	/* blink with 1 Hz as default if nothing specified */
183a403d930SBryan Wu 	if (!*delay_on && !*delay_off)
184a403d930SBryan Wu 		*delay_on = *delay_off = 500;
185a403d930SBryan Wu 
186a403d930SBryan Wu 	led_set_software_blink(led_cdev, *delay_on, *delay_off);
187a403d930SBryan Wu }
1885bb629c5SFabio Baltieri 
189757b06aeSJacek Anaszewski void led_init_core(struct led_classdev *led_cdev)
190757b06aeSJacek Anaszewski {
191757b06aeSJacek Anaszewski 	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
192757b06aeSJacek Anaszewski 
19349404665SKees Cook 	timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
194757b06aeSJacek Anaszewski }
195757b06aeSJacek Anaszewski EXPORT_SYMBOL_GPL(led_init_core);
196757b06aeSJacek Anaszewski 
1975bb629c5SFabio Baltieri void led_blink_set(struct led_classdev *led_cdev,
1985bb629c5SFabio Baltieri 		   unsigned long *delay_on,
1995bb629c5SFabio Baltieri 		   unsigned long *delay_off)
2005bb629c5SFabio Baltieri {
2017b6af2c5SJacek Anaszewski 	del_timer_sync(&led_cdev->blink_timer);
2025bb629c5SFabio Baltieri 
2037b6af2c5SJacek Anaszewski 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
204a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
205a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
2065bb629c5SFabio Baltieri 
2075bb629c5SFabio Baltieri 	led_blink_setup(led_cdev, delay_on, delay_off);
2085bb629c5SFabio Baltieri }
2092806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_blink_set);
210a403d930SBryan Wu 
2115bb629c5SFabio Baltieri void led_blink_set_oneshot(struct led_classdev *led_cdev,
2125bb629c5SFabio Baltieri 			   unsigned long *delay_on,
2135bb629c5SFabio Baltieri 			   unsigned long *delay_off,
2145bb629c5SFabio Baltieri 			   int invert)
2155bb629c5SFabio Baltieri {
216a9c6ce57SHans de Goede 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
2179067359fSJiri Kosina 	     timer_pending(&led_cdev->blink_timer))
2185bb629c5SFabio Baltieri 		return;
2195bb629c5SFabio Baltieri 
220a9c6ce57SHans de Goede 	set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
221a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
2225bb629c5SFabio Baltieri 
2235bb629c5SFabio Baltieri 	if (invert)
224a9c6ce57SHans de Goede 		set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
2255bb629c5SFabio Baltieri 	else
226a9c6ce57SHans de Goede 		clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
2275bb629c5SFabio Baltieri 
2285bb629c5SFabio Baltieri 	led_blink_setup(led_cdev, delay_on, delay_off);
2295bb629c5SFabio Baltieri }
2302806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
2315bb629c5SFabio Baltieri 
232d23a22a7SFabio Baltieri void led_stop_software_blink(struct led_classdev *led_cdev)
233a403d930SBryan Wu {
2349067359fSJiri Kosina 	del_timer_sync(&led_cdev->blink_timer);
23543786482SFabio Baltieri 	led_cdev->blink_delay_on = 0;
23643786482SFabio Baltieri 	led_cdev->blink_delay_off = 0;
237a9c6ce57SHans de Goede 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
238d23a22a7SFabio Baltieri }
239d23a22a7SFabio Baltieri EXPORT_SYMBOL_GPL(led_stop_software_blink);
240d23a22a7SFabio Baltieri 
241af0bfab9SAbanoub Sameh void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
242d23a22a7SFabio Baltieri {
243f1e80c07SJacek Anaszewski 	/*
2447cfe749fSTony Makkiel 	 * If software blink is active, delay brightness setting
245f1e80c07SJacek Anaszewski 	 * until the next timer tick.
246f1e80c07SJacek Anaszewski 	 */
247a9c6ce57SHans de Goede 	if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
248f1e80c07SJacek Anaszewski 		/*
249f1e80c07SJacek Anaszewski 		 * If we need to disable soft blinking delegate this to the
250f1e80c07SJacek Anaszewski 		 * work queue task to avoid problems in case we are called
251f1e80c07SJacek Anaszewski 		 * from hard irq context.
252f1e80c07SJacek Anaszewski 		 */
253af0bfab9SAbanoub Sameh 		if (!brightness) {
254a9c6ce57SHans de Goede 			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
255d23a22a7SFabio Baltieri 			schedule_work(&led_cdev->set_brightness_work);
256f1e80c07SJacek Anaszewski 		} else {
257a9c6ce57SHans de Goede 			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
258a9c6ce57SHans de Goede 				&led_cdev->work_flags);
259eb1610b4SHans de Goede 			led_cdev->new_blink_brightness = brightness;
260f1e80c07SJacek Anaszewski 		}
261d23a22a7SFabio Baltieri 		return;
262d23a22a7SFabio Baltieri 	}
26343786482SFabio Baltieri 
26481fe8e5bSJacek Anaszewski 	led_set_brightness_nosleep(led_cdev, brightness);
265a403d930SBryan Wu }
2662806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness);
2673ef7de53SJacek Anaszewski 
268af0bfab9SAbanoub Sameh void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
26981fe8e5bSJacek Anaszewski {
27081fe8e5bSJacek Anaszewski 	/* Use brightness_set op if available, it is guaranteed not to sleep */
271d4887af9SHeiner Kallweit 	if (!__led_set_brightness(led_cdev, value))
27281fe8e5bSJacek Anaszewski 		return;
27381fe8e5bSJacek Anaszewski 
27481fe8e5bSJacek Anaszewski 	/* If brightness setting can sleep, delegate it to a work queue task */
27581fe8e5bSJacek Anaszewski 	led_cdev->delayed_set_value = value;
27681fe8e5bSJacek Anaszewski 	schedule_work(&led_cdev->set_brightness_work);
27781fe8e5bSJacek Anaszewski }
27881fe8e5bSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
27981fe8e5bSJacek Anaszewski 
280af0bfab9SAbanoub Sameh void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value)
28181fe8e5bSJacek Anaszewski {
28281fe8e5bSJacek Anaszewski 	led_cdev->brightness = min(value, led_cdev->max_brightness);
28381fe8e5bSJacek Anaszewski 
28481fe8e5bSJacek Anaszewski 	if (led_cdev->flags & LED_SUSPENDED)
28581fe8e5bSJacek Anaszewski 		return;
28681fe8e5bSJacek Anaszewski 
28781fe8e5bSJacek Anaszewski 	led_set_brightness_nopm(led_cdev, led_cdev->brightness);
28881fe8e5bSJacek Anaszewski }
28981fe8e5bSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
29081fe8e5bSJacek Anaszewski 
291af0bfab9SAbanoub Sameh int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
29213ae79bbSJacek Anaszewski {
29313ae79bbSJacek Anaszewski 	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
29413ae79bbSJacek Anaszewski 		return -EBUSY;
29513ae79bbSJacek Anaszewski 
29613ae79bbSJacek Anaszewski 	led_cdev->brightness = min(value, led_cdev->max_brightness);
29713ae79bbSJacek Anaszewski 
29813ae79bbSJacek Anaszewski 	if (led_cdev->flags & LED_SUSPENDED)
29913ae79bbSJacek Anaszewski 		return 0;
30013ae79bbSJacek Anaszewski 
301d4887af9SHeiner Kallweit 	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
30213ae79bbSJacek Anaszewski }
30313ae79bbSJacek Anaszewski EXPORT_SYMBOL_GPL(led_set_brightness_sync);
30413ae79bbSJacek Anaszewski 
3053ef7de53SJacek Anaszewski int led_update_brightness(struct led_classdev *led_cdev)
3063ef7de53SJacek Anaszewski {
3073ef7de53SJacek Anaszewski 	int ret = 0;
3083ef7de53SJacek Anaszewski 
3093ef7de53SJacek Anaszewski 	if (led_cdev->brightness_get) {
3103ef7de53SJacek Anaszewski 		ret = led_cdev->brightness_get(led_cdev);
3113ef7de53SJacek Anaszewski 		if (ret >= 0) {
3123ef7de53SJacek Anaszewski 			led_cdev->brightness = ret;
3133ef7de53SJacek Anaszewski 			return 0;
3143ef7de53SJacek Anaszewski 		}
3153ef7de53SJacek Anaszewski 	}
3163ef7de53SJacek Anaszewski 
3173ef7de53SJacek Anaszewski 	return ret;
3183ef7de53SJacek Anaszewski }
3192806e2ffSJacek Anaszewski EXPORT_SYMBOL_GPL(led_update_brightness);
320acd899e4SJacek Anaszewski 
3218e1f4561SKrzysztof Kozlowski u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
3228e1f4561SKrzysztof Kozlowski {
323fd81d7e9SAndy Shevchenko 	struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
3248e1f4561SKrzysztof Kozlowski 	u32 *pattern;
3258e1f4561SKrzysztof Kozlowski 	int count;
3268e1f4561SKrzysztof Kozlowski 
327fd81d7e9SAndy Shevchenko 	count = fwnode_property_count_u32(fwnode, "led-pattern");
3288e1f4561SKrzysztof Kozlowski 	if (count < 0)
3298e1f4561SKrzysztof Kozlowski 		return NULL;
3308e1f4561SKrzysztof Kozlowski 
3318e1f4561SKrzysztof Kozlowski 	pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
3328e1f4561SKrzysztof Kozlowski 	if (!pattern)
3338e1f4561SKrzysztof Kozlowski 		return NULL;
3348e1f4561SKrzysztof Kozlowski 
335fd81d7e9SAndy Shevchenko 	if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
3368e1f4561SKrzysztof Kozlowski 		kfree(pattern);
3378e1f4561SKrzysztof Kozlowski 		return NULL;
3388e1f4561SKrzysztof Kozlowski 	}
3398e1f4561SKrzysztof Kozlowski 
3408e1f4561SKrzysztof Kozlowski 	*size = count;
3418e1f4561SKrzysztof Kozlowski 
3428e1f4561SKrzysztof Kozlowski 	return pattern;
3438e1f4561SKrzysztof Kozlowski }
3448e1f4561SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(led_get_default_pattern);
3458e1f4561SKrzysztof Kozlowski 
346acd899e4SJacek Anaszewski /* Caller must ensure led_cdev->led_access held */
347acd899e4SJacek Anaszewski void led_sysfs_disable(struct led_classdev *led_cdev)
348acd899e4SJacek Anaszewski {
349acd899e4SJacek Anaszewski 	lockdep_assert_held(&led_cdev->led_access);
350acd899e4SJacek Anaszewski 
351acd899e4SJacek Anaszewski 	led_cdev->flags |= LED_SYSFS_DISABLE;
352acd899e4SJacek Anaszewski }
353acd899e4SJacek Anaszewski EXPORT_SYMBOL_GPL(led_sysfs_disable);
354acd899e4SJacek Anaszewski 
355acd899e4SJacek Anaszewski /* Caller must ensure led_cdev->led_access held */
356acd899e4SJacek Anaszewski void led_sysfs_enable(struct led_classdev *led_cdev)
357acd899e4SJacek Anaszewski {
358acd899e4SJacek Anaszewski 	lockdep_assert_held(&led_cdev->led_access);
359acd899e4SJacek Anaszewski 
360acd899e4SJacek Anaszewski 	led_cdev->flags &= ~LED_SYSFS_DISABLE;
361acd899e4SJacek Anaszewski }
362acd899e4SJacek Anaszewski EXPORT_SYMBOL_GPL(led_sysfs_enable);
363bb4e9af0SJacek Anaszewski 
364bb4e9af0SJacek Anaszewski static void led_parse_fwnode_props(struct device *dev,
365bb4e9af0SJacek Anaszewski 				   struct fwnode_handle *fwnode,
366bb4e9af0SJacek Anaszewski 				   struct led_properties *props)
367bb4e9af0SJacek Anaszewski {
368bb4e9af0SJacek Anaszewski 	int ret;
369bb4e9af0SJacek Anaszewski 
370bb4e9af0SJacek Anaszewski 	if (!fwnode)
371bb4e9af0SJacek Anaszewski 		return;
372bb4e9af0SJacek Anaszewski 
373bb4e9af0SJacek Anaszewski 	if (fwnode_property_present(fwnode, "label")) {
374bb4e9af0SJacek Anaszewski 		ret = fwnode_property_read_string(fwnode, "label", &props->label);
375bb4e9af0SJacek Anaszewski 		if (ret)
376bb4e9af0SJacek Anaszewski 			dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
377bb4e9af0SJacek Anaszewski 		return;
378bb4e9af0SJacek Anaszewski 	}
379bb4e9af0SJacek Anaszewski 
380bb4e9af0SJacek Anaszewski 	if (fwnode_property_present(fwnode, "color")) {
381bb4e9af0SJacek Anaszewski 		ret = fwnode_property_read_u32(fwnode, "color", &props->color);
382bb4e9af0SJacek Anaszewski 		if (ret)
383bb4e9af0SJacek Anaszewski 			dev_err(dev, "Error parsing 'color' property (%d)\n", ret);
384bb4e9af0SJacek Anaszewski 		else if (props->color >= LED_COLOR_ID_MAX)
385bb4e9af0SJacek Anaszewski 			dev_err(dev, "LED color identifier out of range\n");
386bb4e9af0SJacek Anaszewski 		else
387bb4e9af0SJacek Anaszewski 			props->color_present = true;
388bb4e9af0SJacek Anaszewski 	}
389bb4e9af0SJacek Anaszewski 
390bb4e9af0SJacek Anaszewski 
391bb4e9af0SJacek Anaszewski 	if (!fwnode_property_present(fwnode, "function"))
392bb4e9af0SJacek Anaszewski 		return;
393bb4e9af0SJacek Anaszewski 
394bb4e9af0SJacek Anaszewski 	ret = fwnode_property_read_string(fwnode, "function", &props->function);
395bb4e9af0SJacek Anaszewski 	if (ret) {
396bb4e9af0SJacek Anaszewski 		dev_err(dev,
397bb4e9af0SJacek Anaszewski 			"Error parsing 'function' property (%d)\n",
398bb4e9af0SJacek Anaszewski 			ret);
399bb4e9af0SJacek Anaszewski 	}
400bb4e9af0SJacek Anaszewski 
401bb4e9af0SJacek Anaszewski 	if (!fwnode_property_present(fwnode, "function-enumerator"))
402bb4e9af0SJacek Anaszewski 		return;
403bb4e9af0SJacek Anaszewski 
404bb4e9af0SJacek Anaszewski 	ret = fwnode_property_read_u32(fwnode, "function-enumerator",
405bb4e9af0SJacek Anaszewski 				       &props->func_enum);
406bb4e9af0SJacek Anaszewski 	if (ret) {
407bb4e9af0SJacek Anaszewski 		dev_err(dev,
408bb4e9af0SJacek Anaszewski 			"Error parsing 'function-enumerator' property (%d)\n",
409bb4e9af0SJacek Anaszewski 			ret);
410bb4e9af0SJacek Anaszewski 	} else {
411bb4e9af0SJacek Anaszewski 		props->func_enum_present = true;
412bb4e9af0SJacek Anaszewski 	}
413bb4e9af0SJacek Anaszewski }
414bb4e9af0SJacek Anaszewski 
415bb4e9af0SJacek Anaszewski int led_compose_name(struct device *dev, struct led_init_data *init_data,
416bb4e9af0SJacek Anaszewski 		     char *led_classdev_name)
417bb4e9af0SJacek Anaszewski {
418bb4e9af0SJacek Anaszewski 	struct led_properties props = {};
419bb4e9af0SJacek Anaszewski 	struct fwnode_handle *fwnode = init_data->fwnode;
420bb4e9af0SJacek Anaszewski 	const char *devicename = init_data->devicename;
421bb4e9af0SJacek Anaszewski 
42277dce3a2SPavel Machek 	/* We want to label LEDs that can produce full range of colors
42377dce3a2SPavel Machek 	 * as RGB, not multicolor */
42477dce3a2SPavel Machek 	BUG_ON(props.color == LED_COLOR_ID_MULTI);
42577dce3a2SPavel Machek 
426bb4e9af0SJacek Anaszewski 	if (!led_classdev_name)
427bb4e9af0SJacek Anaszewski 		return -EINVAL;
428bb4e9af0SJacek Anaszewski 
429bb4e9af0SJacek Anaszewski 	led_parse_fwnode_props(dev, fwnode, &props);
430bb4e9af0SJacek Anaszewski 
431bb4e9af0SJacek Anaszewski 	if (props.label) {
432bb4e9af0SJacek Anaszewski 		/*
433bb4e9af0SJacek Anaszewski 		 * If init_data.devicename is NULL, then it indicates that
434bb4e9af0SJacek Anaszewski 		 * DT label should be used as-is for LED class device name.
435bb4e9af0SJacek Anaszewski 		 * Otherwise the label is prepended with devicename to compose
436bb4e9af0SJacek Anaszewski 		 * the final LED class device name.
437bb4e9af0SJacek Anaszewski 		 */
438bb4e9af0SJacek Anaszewski 		if (!devicename) {
439bb4e9af0SJacek Anaszewski 			strscpy(led_classdev_name, props.label,
440bb4e9af0SJacek Anaszewski 				LED_MAX_NAME_SIZE);
441bb4e9af0SJacek Anaszewski 		} else {
442bb4e9af0SJacek Anaszewski 			snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
443bb4e9af0SJacek Anaszewski 				 devicename, props.label);
444bb4e9af0SJacek Anaszewski 		}
445bb4e9af0SJacek Anaszewski 	} else if (props.function || props.color_present) {
446bb4e9af0SJacek Anaszewski 		char tmp_buf[LED_MAX_NAME_SIZE];
447bb4e9af0SJacek Anaszewski 
448bb4e9af0SJacek Anaszewski 		if (props.func_enum_present) {
449bb4e9af0SJacek Anaszewski 			snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d",
450bb4e9af0SJacek Anaszewski 				 props.color_present ? led_colors[props.color] : "",
451bb4e9af0SJacek Anaszewski 				 props.function ?: "", props.func_enum);
452bb4e9af0SJacek Anaszewski 		} else {
453bb4e9af0SJacek Anaszewski 			snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s",
454bb4e9af0SJacek Anaszewski 				 props.color_present ? led_colors[props.color] : "",
455bb4e9af0SJacek Anaszewski 				 props.function ?: "");
456bb4e9af0SJacek Anaszewski 		}
457bb4e9af0SJacek Anaszewski 		if (init_data->devname_mandatory) {
458bb4e9af0SJacek Anaszewski 			snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
459bb4e9af0SJacek Anaszewski 				 devicename, tmp_buf);
460bb4e9af0SJacek Anaszewski 		} else {
461bb4e9af0SJacek Anaszewski 			strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE);
462bb4e9af0SJacek Anaszewski 
463bb4e9af0SJacek Anaszewski 		}
464bb4e9af0SJacek Anaszewski 	} else if (init_data->default_label) {
465bb4e9af0SJacek Anaszewski 		if (!devicename) {
466bb4e9af0SJacek Anaszewski 			dev_err(dev, "Legacy LED naming requires devicename segment");
467bb4e9af0SJacek Anaszewski 			return -EINVAL;
468bb4e9af0SJacek Anaszewski 		}
469bb4e9af0SJacek Anaszewski 		snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
470bb4e9af0SJacek Anaszewski 			 devicename, init_data->default_label);
471bb4e9af0SJacek Anaszewski 	} else if (is_of_node(fwnode)) {
472bb4e9af0SJacek Anaszewski 		strscpy(led_classdev_name, to_of_node(fwnode)->name,
473bb4e9af0SJacek Anaszewski 			LED_MAX_NAME_SIZE);
474bb4e9af0SJacek Anaszewski 	} else
475bb4e9af0SJacek Anaszewski 		return -EINVAL;
476bb4e9af0SJacek Anaszewski 
477bb4e9af0SJacek Anaszewski 	return 0;
478bb4e9af0SJacek Anaszewski }
479bb4e9af0SJacek Anaszewski EXPORT_SYMBOL_GPL(led_compose_name);
480*791bc411SDenis Osterland-Heim 
481*791bc411SDenis Osterland-Heim enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode)
482*791bc411SDenis Osterland-Heim {
483*791bc411SDenis Osterland-Heim 	const char *state = NULL;
484*791bc411SDenis Osterland-Heim 
485*791bc411SDenis Osterland-Heim 	if (!fwnode_property_read_string(fwnode, "default-state", &state)) {
486*791bc411SDenis Osterland-Heim 		if (!strcmp(state, "keep"))
487*791bc411SDenis Osterland-Heim 			return LEDS_DEFSTATE_KEEP;
488*791bc411SDenis Osterland-Heim 		if (!strcmp(state, "on"))
489*791bc411SDenis Osterland-Heim 			return LEDS_DEFSTATE_ON;
490*791bc411SDenis Osterland-Heim 	}
491*791bc411SDenis Osterland-Heim 
492*791bc411SDenis Osterland-Heim 	return LEDS_DEFSTATE_OFF;
493*791bc411SDenis Osterland-Heim }
494*791bc411SDenis Osterland-Heim EXPORT_SYMBOL_GPL(led_init_default_state_get);
495