Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / leds / led-core.c
blobc4e780bdb38523b2cc18b74348a10158abae654b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LED Class Core
5 * Copyright 2005-2006 Openedhand Ltd.
7 * Author: Richard Purdie <rpurdie@openedhand.com>
8 */
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"
22 DECLARE_RWSEM(leds_list_lock);
23 EXPORT_SYMBOL_GPL(leds_list_lock);
25 LIST_HEAD(leds_list);
26 EXPORT_SYMBOL_GPL(leds_list);
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",
40 EXPORT_SYMBOL_GPL(led_colors);
42 static int __led_set_brightness(struct led_classdev *led_cdev,
43 enum led_brightness value)
45 if (!led_cdev->brightness_set)
46 return -ENOTSUPP;
48 led_cdev->brightness_set(led_cdev, value);
50 return 0;
53 static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
54 enum led_brightness value)
56 if (!led_cdev->brightness_set_blocking)
57 return -ENOTSUPP;
59 return led_cdev->brightness_set_blocking(led_cdev, value);
62 static void led_timer_function(struct timer_list *t)
64 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
65 unsigned long brightness;
66 unsigned long delay;
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;
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;
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.
93 led_cdev->blink_brightness = brightness;
94 brightness = LED_OFF;
95 delay = led_cdev->blink_delay_off;
98 led_set_brightness_nosleep(led_cdev, brightness);
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.
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);
116 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
119 static void set_brightness_delayed(struct work_struct *ws)
121 struct led_classdev *led_cdev =
122 container_of(ws, struct led_classdev, set_brightness_work);
123 int ret = 0;
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);
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);
142 static void led_set_software_blink(struct led_classdev *led_cdev,
143 unsigned long delay_on,
144 unsigned long delay_off)
146 int current_brightness;
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;
154 led_cdev->blink_delay_on = delay_on;
155 led_cdev->blink_delay_off = delay_off;
157 /* never on - just set to off */
158 if (!delay_on) {
159 led_set_brightness_nosleep(led_cdev, LED_OFF);
160 return;
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;
170 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
171 mod_timer(&led_cdev->blink_timer, jiffies + 1);
175 static void led_blink_setup(struct led_classdev *led_cdev,
176 unsigned long *delay_on,
177 unsigned long *delay_off)
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;
184 /* blink with 1 Hz as default if nothing specified */
185 if (!*delay_on && !*delay_off)
186 *delay_on = *delay_off = 500;
188 led_set_software_blink(led_cdev, *delay_on, *delay_off);
191 void led_init_core(struct led_classdev *led_cdev)
193 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
195 timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
197 EXPORT_SYMBOL_GPL(led_init_core);
199 void led_blink_set(struct led_classdev *led_cdev,
200 unsigned long *delay_on,
201 unsigned long *delay_off)
203 del_timer_sync(&led_cdev->blink_timer);
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);
209 led_blink_setup(led_cdev, delay_on, delay_off);
211 EXPORT_SYMBOL_GPL(led_blink_set);
213 void led_blink_set_oneshot(struct led_classdev *led_cdev,
214 unsigned long *delay_on,
215 unsigned long *delay_off,
216 int invert)
218 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
219 timer_pending(&led_cdev->blink_timer))
220 return;
222 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
223 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
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);
230 led_blink_setup(led_cdev, delay_on, delay_off);
232 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
234 void led_stop_software_blink(struct led_classdev *led_cdev)
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);
241 EXPORT_SYMBOL_GPL(led_stop_software_blink);
243 void led_set_brightness(struct led_classdev *led_cdev,
244 enum led_brightness brightness)
247 * If software blink is active, delay brightness setting
248 * until the next timer tick.
250 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
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.
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;
264 return;
267 led_set_brightness_nosleep(led_cdev, brightness);
269 EXPORT_SYMBOL_GPL(led_set_brightness);
271 void led_set_brightness_nopm(struct led_classdev *led_cdev,
272 enum led_brightness value)
274 /* Use brightness_set op if available, it is guaranteed not to sleep */
275 if (!__led_set_brightness(led_cdev, value))
276 return;
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);
282 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
284 void led_set_brightness_nosleep(struct led_classdev *led_cdev,
285 enum led_brightness value)
287 led_cdev->brightness = min(value, led_cdev->max_brightness);
289 if (led_cdev->flags & LED_SUSPENDED)
290 return;
292 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
294 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
296 int led_set_brightness_sync(struct led_classdev *led_cdev,
297 enum led_brightness value)
299 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
300 return -EBUSY;
302 led_cdev->brightness = min(value, led_cdev->max_brightness);
304 if (led_cdev->flags & LED_SUSPENDED)
305 return 0;
307 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
309 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
311 int led_update_brightness(struct led_classdev *led_cdev)
313 int ret = 0;
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;
323 return ret;
325 EXPORT_SYMBOL_GPL(led_update_brightness);
327 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
329 struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
330 u32 *pattern;
331 int count;
333 count = fwnode_property_count_u32(fwnode, "led-pattern");
334 if (count < 0)
335 return NULL;
337 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
338 if (!pattern)
339 return NULL;
341 if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
342 kfree(pattern);
343 return NULL;
346 *size = count;
348 return pattern;
350 EXPORT_SYMBOL_GPL(led_get_default_pattern);
352 /* Caller must ensure led_cdev->led_access held */
353 void led_sysfs_disable(struct led_classdev *led_cdev)
355 lockdep_assert_held(&led_cdev->led_access);
357 led_cdev->flags |= LED_SYSFS_DISABLE;
359 EXPORT_SYMBOL_GPL(led_sysfs_disable);
361 /* Caller must ensure led_cdev->led_access held */
362 void led_sysfs_enable(struct led_classdev *led_cdev)
364 lockdep_assert_held(&led_cdev->led_access);
366 led_cdev->flags &= ~LED_SYSFS_DISABLE;
368 EXPORT_SYMBOL_GPL(led_sysfs_enable);
370 static void led_parse_fwnode_props(struct device *dev,
371 struct fwnode_handle *fwnode,
372 struct led_properties *props)
374 int ret;
376 if (!fwnode)
377 return;
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;
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;
397 if (!fwnode_property_present(fwnode, "function"))
398 return;
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);
407 if (!fwnode_property_present(fwnode, "function-enumerator"))
408 return;
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;
421 int led_compose_name(struct device *dev, struct led_init_data *init_data,
422 char *led_classdev_name)
424 struct led_properties props = {};
425 struct fwnode_handle *fwnode = init_data->fwnode;
426 const char *devicename = init_data->devicename;
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);
432 if (!led_classdev_name)
433 return -EINVAL;
435 led_parse_fwnode_props(dev, fwnode, &props);
437 if (props.label) {
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.
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);
451 } else if (props.function || props.color_present) {
452 char tmp_buf[LED_MAX_NAME_SIZE];
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 ?: "");
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);
470 } else if (init_data->default_label) {
471 if (!devicename) {
472 dev_err(dev, "Legacy LED naming requires devicename segment");
473 return -EINVAL;
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;
483 return 0;
485 EXPORT_SYMBOL_GPL(led_compose_name);