xref: /openbmc/linux/drivers/leds/led-core.c (revision 2634682f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LED Class Core
4  *
5  * Copyright 2005-2006 Openedhand Ltd.
6  *
7  * Author: Richard Purdie <rpurdie@openedhand.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/leds.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/of.h>
16 #include <linux/property.h>
17 #include <linux/rwsem.h>
18 #include <linux/slab.h>
19 #include <uapi/linux/uleds.h>
20 #include "leds.h"
21 
22 DECLARE_RWSEM(leds_list_lock);
23 EXPORT_SYMBOL_GPL(leds_list_lock);
24 
25 LIST_HEAD(leds_list);
26 EXPORT_SYMBOL_GPL(leds_list);
27 
28 const char * const led_colors[LED_COLOR_ID_MAX] = {
29 	[LED_COLOR_ID_WHITE] = "white",
30 	[LED_COLOR_ID_RED] = "red",
31 	[LED_COLOR_ID_GREEN] = "green",
32 	[LED_COLOR_ID_BLUE] = "blue",
33 	[LED_COLOR_ID_AMBER] = "amber",
34 	[LED_COLOR_ID_VIOLET] = "violet",
35 	[LED_COLOR_ID_YELLOW] = "yellow",
36 	[LED_COLOR_ID_IR] = "ir",
37 	[LED_COLOR_ID_MULTI] = "multicolor",
38 	[LED_COLOR_ID_RGB] = "rgb",
39 };
40 EXPORT_SYMBOL_GPL(led_colors);
41 
42 static int __led_set_brightness(struct led_classdev *led_cdev,
43 				enum led_brightness value)
44 {
45 	if (!led_cdev->brightness_set)
46 		return -ENOTSUPP;
47 
48 	led_cdev->brightness_set(led_cdev, value);
49 
50 	return 0;
51 }
52 
53 static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
54 					 enum led_brightness value)
55 {
56 	if (!led_cdev->brightness_set_blocking)
57 		return -ENOTSUPP;
58 
59 	return led_cdev->brightness_set_blocking(led_cdev, value);
60 }
61 
62 static void led_timer_function(struct timer_list *t)
63 {
64 	struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
65 	unsigned long brightness;
66 	unsigned long delay;
67 
68 	if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
69 		led_set_brightness_nosleep(led_cdev, LED_OFF);
70 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
71 		return;
72 	}
73 
74 	if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
75 			       &led_cdev->work_flags)) {
76 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
77 		return;
78 	}
79 
80 	brightness = led_get_brightness(led_cdev);
81 	if (!brightness) {
82 		/* Time to switch the LED on. */
83 		if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE,
84 					&led_cdev->work_flags))
85 			brightness = led_cdev->new_blink_brightness;
86 		else
87 			brightness = led_cdev->blink_brightness;
88 		delay = led_cdev->blink_delay_on;
89 	} else {
90 		/* Store the current brightness value to be able
91 		 * to restore it when the delay_off period is over.
92 		 */
93 		led_cdev->blink_brightness = brightness;
94 		brightness = LED_OFF;
95 		delay = led_cdev->blink_delay_off;
96 	}
97 
98 	led_set_brightness_nosleep(led_cdev, brightness);
99 
100 	/* Return in next iteration if led is in one-shot mode and we are in
101 	 * the final blink state so that the led is toggled each delay_on +
102 	 * delay_off milliseconds in worst case.
103 	 */
104 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
105 		if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
106 			if (brightness)
107 				set_bit(LED_BLINK_ONESHOT_STOP,
108 					&led_cdev->work_flags);
109 		} else {
110 			if (!brightness)
111 				set_bit(LED_BLINK_ONESHOT_STOP,
112 					&led_cdev->work_flags);
113 		}
114 	}
115 
116 	mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
117 }
118 
119 static void set_brightness_delayed(struct work_struct *ws)
120 {
121 	struct led_classdev *led_cdev =
122 		container_of(ws, struct led_classdev, set_brightness_work);
123 	int ret = 0;
124 
125 	if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
126 		led_cdev->delayed_set_value = LED_OFF;
127 		led_stop_software_blink(led_cdev);
128 	}
129 
130 	ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
131 	if (ret == -ENOTSUPP)
132 		ret = __led_set_brightness_blocking(led_cdev,
133 					led_cdev->delayed_set_value);
134 	if (ret < 0 &&
135 	    /* LED HW might have been unplugged, therefore don't warn */
136 	    !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
137 	    (led_cdev->flags & LED_HW_PLUGGABLE)))
138 		dev_err(led_cdev->dev,
139 			"Setting an LED's brightness failed (%d)\n", ret);
140 }
141 
142 static void led_set_software_blink(struct led_classdev *led_cdev,
143 				   unsigned long delay_on,
144 				   unsigned long delay_off)
145 {
146 	int current_brightness;
147 
148 	current_brightness = led_get_brightness(led_cdev);
149 	if (current_brightness)
150 		led_cdev->blink_brightness = current_brightness;
151 	if (!led_cdev->blink_brightness)
152 		led_cdev->blink_brightness = led_cdev->max_brightness;
153 
154 	led_cdev->blink_delay_on = delay_on;
155 	led_cdev->blink_delay_off = delay_off;
156 
157 	/* never on - just set to off */
158 	if (!delay_on) {
159 		led_set_brightness_nosleep(led_cdev, LED_OFF);
160 		return;
161 	}
162 
163 	/* never off - just set to brightness */
164 	if (!delay_off) {
165 		led_set_brightness_nosleep(led_cdev,
166 					   led_cdev->blink_brightness);
167 		return;
168 	}
169 
170 	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
171 	mod_timer(&led_cdev->blink_timer, jiffies + 1);
172 }
173 
174 
175 static void led_blink_setup(struct led_classdev *led_cdev,
176 		     unsigned long *delay_on,
177 		     unsigned long *delay_off)
178 {
179 	if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
180 	    led_cdev->blink_set &&
181 	    !led_cdev->blink_set(led_cdev, delay_on, delay_off))
182 		return;
183 
184 	/* blink with 1 Hz as default if nothing specified */
185 	if (!*delay_on && !*delay_off)
186 		*delay_on = *delay_off = 500;
187 
188 	led_set_software_blink(led_cdev, *delay_on, *delay_off);
189 }
190 
191 void led_init_core(struct led_classdev *led_cdev)
192 {
193 	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
194 
195 	timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
196 }
197 EXPORT_SYMBOL_GPL(led_init_core);
198 
199 void led_blink_set(struct led_classdev *led_cdev,
200 		   unsigned long *delay_on,
201 		   unsigned long *delay_off)
202 {
203 	del_timer_sync(&led_cdev->blink_timer);
204 
205 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
206 	clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
207 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
208 
209 	led_blink_setup(led_cdev, delay_on, delay_off);
210 }
211 EXPORT_SYMBOL_GPL(led_blink_set);
212 
213 void led_blink_set_oneshot(struct led_classdev *led_cdev,
214 			   unsigned long *delay_on,
215 			   unsigned long *delay_off,
216 			   int invert)
217 {
218 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
219 	     timer_pending(&led_cdev->blink_timer))
220 		return;
221 
222 	set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
223 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
224 
225 	if (invert)
226 		set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
227 	else
228 		clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
229 
230 	led_blink_setup(led_cdev, delay_on, delay_off);
231 }
232 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
233 
234 void led_stop_software_blink(struct led_classdev *led_cdev)
235 {
236 	del_timer_sync(&led_cdev->blink_timer);
237 	led_cdev->blink_delay_on = 0;
238 	led_cdev->blink_delay_off = 0;
239 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
240 }
241 EXPORT_SYMBOL_GPL(led_stop_software_blink);
242 
243 void led_set_brightness(struct led_classdev *led_cdev,
244 			enum led_brightness brightness)
245 {
246 	/*
247 	 * If software blink is active, delay brightness setting
248 	 * until the next timer tick.
249 	 */
250 	if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
251 		/*
252 		 * If we need to disable soft blinking delegate this to the
253 		 * work queue task to avoid problems in case we are called
254 		 * from hard irq context.
255 		 */
256 		if (brightness == LED_OFF) {
257 			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
258 			schedule_work(&led_cdev->set_brightness_work);
259 		} else {
260 			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
261 				&led_cdev->work_flags);
262 			led_cdev->new_blink_brightness = brightness;
263 		}
264 		return;
265 	}
266 
267 	led_set_brightness_nosleep(led_cdev, brightness);
268 }
269 EXPORT_SYMBOL_GPL(led_set_brightness);
270 
271 void led_set_brightness_nopm(struct led_classdev *led_cdev,
272 			      enum led_brightness value)
273 {
274 	/* Use brightness_set op if available, it is guaranteed not to sleep */
275 	if (!__led_set_brightness(led_cdev, value))
276 		return;
277 
278 	/* If brightness setting can sleep, delegate it to a work queue task */
279 	led_cdev->delayed_set_value = value;
280 	schedule_work(&led_cdev->set_brightness_work);
281 }
282 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
283 
284 void led_set_brightness_nosleep(struct led_classdev *led_cdev,
285 				enum led_brightness value)
286 {
287 	led_cdev->brightness = min(value, led_cdev->max_brightness);
288 
289 	if (led_cdev->flags & LED_SUSPENDED)
290 		return;
291 
292 	led_set_brightness_nopm(led_cdev, led_cdev->brightness);
293 }
294 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
295 
296 int led_set_brightness_sync(struct led_classdev *led_cdev,
297 			    enum led_brightness value)
298 {
299 	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
300 		return -EBUSY;
301 
302 	led_cdev->brightness = min(value, led_cdev->max_brightness);
303 
304 	if (led_cdev->flags & LED_SUSPENDED)
305 		return 0;
306 
307 	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
308 }
309 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
310 
311 int led_update_brightness(struct led_classdev *led_cdev)
312 {
313 	int ret = 0;
314 
315 	if (led_cdev->brightness_get) {
316 		ret = led_cdev->brightness_get(led_cdev);
317 		if (ret >= 0) {
318 			led_cdev->brightness = ret;
319 			return 0;
320 		}
321 	}
322 
323 	return ret;
324 }
325 EXPORT_SYMBOL_GPL(led_update_brightness);
326 
327 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
328 {
329 	struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
330 	u32 *pattern;
331 	int count;
332 
333 	count = fwnode_property_count_u32(fwnode, "led-pattern");
334 	if (count < 0)
335 		return NULL;
336 
337 	pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
338 	if (!pattern)
339 		return NULL;
340 
341 	if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
342 		kfree(pattern);
343 		return NULL;
344 	}
345 
346 	*size = count;
347 
348 	return pattern;
349 }
350 EXPORT_SYMBOL_GPL(led_get_default_pattern);
351 
352 /* Caller must ensure led_cdev->led_access held */
353 void led_sysfs_disable(struct led_classdev *led_cdev)
354 {
355 	lockdep_assert_held(&led_cdev->led_access);
356 
357 	led_cdev->flags |= LED_SYSFS_DISABLE;
358 }
359 EXPORT_SYMBOL_GPL(led_sysfs_disable);
360 
361 /* Caller must ensure led_cdev->led_access held */
362 void led_sysfs_enable(struct led_classdev *led_cdev)
363 {
364 	lockdep_assert_held(&led_cdev->led_access);
365 
366 	led_cdev->flags &= ~LED_SYSFS_DISABLE;
367 }
368 EXPORT_SYMBOL_GPL(led_sysfs_enable);
369 
370 static void led_parse_fwnode_props(struct device *dev,
371 				   struct fwnode_handle *fwnode,
372 				   struct led_properties *props)
373 {
374 	int ret;
375 
376 	if (!fwnode)
377 		return;
378 
379 	if (fwnode_property_present(fwnode, "label")) {
380 		ret = fwnode_property_read_string(fwnode, "label", &props->label);
381 		if (ret)
382 			dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
383 		return;
384 	}
385 
386 	if (fwnode_property_present(fwnode, "color")) {
387 		ret = fwnode_property_read_u32(fwnode, "color", &props->color);
388 		if (ret)
389 			dev_err(dev, "Error parsing 'color' property (%d)\n", ret);
390 		else if (props->color >= LED_COLOR_ID_MAX)
391 			dev_err(dev, "LED color identifier out of range\n");
392 		else
393 			props->color_present = true;
394 	}
395 
396 
397 	if (!fwnode_property_present(fwnode, "function"))
398 		return;
399 
400 	ret = fwnode_property_read_string(fwnode, "function", &props->function);
401 	if (ret) {
402 		dev_err(dev,
403 			"Error parsing 'function' property (%d)\n",
404 			ret);
405 	}
406 
407 	if (!fwnode_property_present(fwnode, "function-enumerator"))
408 		return;
409 
410 	ret = fwnode_property_read_u32(fwnode, "function-enumerator",
411 				       &props->func_enum);
412 	if (ret) {
413 		dev_err(dev,
414 			"Error parsing 'function-enumerator' property (%d)\n",
415 			ret);
416 	} else {
417 		props->func_enum_present = true;
418 	}
419 }
420 
421 int led_compose_name(struct device *dev, struct led_init_data *init_data,
422 		     char *led_classdev_name)
423 {
424 	struct led_properties props = {};
425 	struct fwnode_handle *fwnode = init_data->fwnode;
426 	const char *devicename = init_data->devicename;
427 
428 	/* We want to label LEDs that can produce full range of colors
429 	 * as RGB, not multicolor */
430 	BUG_ON(props.color == LED_COLOR_ID_MULTI);
431 
432 	if (!led_classdev_name)
433 		return -EINVAL;
434 
435 	led_parse_fwnode_props(dev, fwnode, &props);
436 
437 	if (props.label) {
438 		/*
439 		 * If init_data.devicename is NULL, then it indicates that
440 		 * DT label should be used as-is for LED class device name.
441 		 * Otherwise the label is prepended with devicename to compose
442 		 * the final LED class device name.
443 		 */
444 		if (!devicename) {
445 			strscpy(led_classdev_name, props.label,
446 				LED_MAX_NAME_SIZE);
447 		} else {
448 			snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
449 				 devicename, props.label);
450 		}
451 	} else if (props.function || props.color_present) {
452 		char tmp_buf[LED_MAX_NAME_SIZE];
453 
454 		if (props.func_enum_present) {
455 			snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d",
456 				 props.color_present ? led_colors[props.color] : "",
457 				 props.function ?: "", props.func_enum);
458 		} else {
459 			snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s",
460 				 props.color_present ? led_colors[props.color] : "",
461 				 props.function ?: "");
462 		}
463 		if (init_data->devname_mandatory) {
464 			snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
465 				 devicename, tmp_buf);
466 		} else {
467 			strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE);
468 
469 		}
470 	} else if (init_data->default_label) {
471 		if (!devicename) {
472 			dev_err(dev, "Legacy LED naming requires devicename segment");
473 			return -EINVAL;
474 		}
475 		snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
476 			 devicename, init_data->default_label);
477 	} else if (is_of_node(fwnode)) {
478 		strscpy(led_classdev_name, to_of_node(fwnode)->name,
479 			LED_MAX_NAME_SIZE);
480 	} else
481 		return -EINVAL;
482 
483 	return 0;
484 }
485 EXPORT_SYMBOL_GPL(led_compose_name);
486