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