1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2005-2006 Openedhand Ltd.
7 * Author: Richard Purdie <rpurdie@openedhand.com>
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>
16 #include <linux/property.h>
17 #include <linux/rwsem.h>
18 #include <linux/slab.h>
19 #include <uapi/linux/uleds.h>
22 DECLARE_RWSEM(leds_list_lock);
23 EXPORT_SYMBOL_GPL(leds_list_lock);
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, unsigned int value)
44 if (!led_cdev->brightness_set)
47 led_cdev->brightness_set(led_cdev, value);
52 static int __led_set_brightness_blocking(struct led_classdev *led_cdev, unsigned int value)
54 if (!led_cdev->brightness_set_blocking)
57 return led_cdev->brightness_set_blocking(led_cdev, value);
60 static void led_timer_function(struct timer_list *t)
62 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
63 unsigned long brightness;
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);
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);
78 brightness = led_get_brightness(led_cdev);
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;
85 brightness = led_cdev->blink_brightness;
86 delay = led_cdev->blink_delay_on;
88 /* Store the current brightness value to be able
89 * to restore it when the delay_off period is over.
91 led_cdev->blink_brightness = brightness;
93 delay = led_cdev->blink_delay_off;
96 led_set_brightness_nosleep(led_cdev, brightness);
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.
102 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
103 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
105 set_bit(LED_BLINK_ONESHOT_STOP,
106 &led_cdev->work_flags);
109 set_bit(LED_BLINK_ONESHOT_STOP,
110 &led_cdev->work_flags);
114 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
117 static void set_brightness_delayed_set_brightness(struct led_classdev *led_cdev,
122 ret = __led_set_brightness(led_cdev, value);
123 if (ret == -ENOTSUPP)
124 ret = __led_set_brightness_blocking(led_cdev, value);
126 /* LED HW might have been unplugged, therefore don't warn */
127 !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
128 (led_cdev->flags & LED_HW_PLUGGABLE)))
129 dev_err(led_cdev->dev,
130 "Setting an LED's brightness failed (%d)\n", ret);
133 static void set_brightness_delayed(struct work_struct *ws)
135 struct led_classdev *led_cdev =
136 container_of(ws, struct led_classdev, set_brightness_work);
138 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
139 led_stop_software_blink(led_cdev);
140 set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
144 * Triggers may call led_set_brightness(LED_OFF),
145 * led_set_brightness(LED_FULL) in quick succession to disable blinking
146 * and turn the LED on. Both actions may have been scheduled to run
147 * before this work item runs once. To make sure this works properly
148 * handle LED_SET_BRIGHTNESS_OFF first.
150 if (test_and_clear_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags))
151 set_brightness_delayed_set_brightness(led_cdev, LED_OFF);
153 if (test_and_clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags))
154 set_brightness_delayed_set_brightness(led_cdev, led_cdev->delayed_set_value);
157 static void led_set_software_blink(struct led_classdev *led_cdev,
158 unsigned long delay_on,
159 unsigned long delay_off)
161 int current_brightness;
163 current_brightness = led_get_brightness(led_cdev);
164 if (current_brightness)
165 led_cdev->blink_brightness = current_brightness;
166 if (!led_cdev->blink_brightness)
167 led_cdev->blink_brightness = led_cdev->max_brightness;
169 led_cdev->blink_delay_on = delay_on;
170 led_cdev->blink_delay_off = delay_off;
172 /* never on - just set to off */
174 led_set_brightness_nosleep(led_cdev, LED_OFF);
178 /* never off - just set to brightness */
180 led_set_brightness_nosleep(led_cdev,
181 led_cdev->blink_brightness);
185 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
186 mod_timer(&led_cdev->blink_timer, jiffies + 1);
190 static void led_blink_setup(struct led_classdev *led_cdev,
191 unsigned long *delay_on,
192 unsigned long *delay_off)
194 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
195 led_cdev->blink_set &&
196 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
199 /* blink with 1 Hz as default if nothing specified */
200 if (!*delay_on && !*delay_off)
201 *delay_on = *delay_off = 500;
203 led_set_software_blink(led_cdev, *delay_on, *delay_off);
206 void led_init_core(struct led_classdev *led_cdev)
208 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
210 timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
212 EXPORT_SYMBOL_GPL(led_init_core);
214 void led_blink_set(struct led_classdev *led_cdev,
215 unsigned long *delay_on,
216 unsigned long *delay_off)
218 del_timer_sync(&led_cdev->blink_timer);
220 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
221 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
222 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
224 led_blink_setup(led_cdev, delay_on, delay_off);
226 EXPORT_SYMBOL_GPL(led_blink_set);
228 void led_blink_set_oneshot(struct led_classdev *led_cdev,
229 unsigned long *delay_on,
230 unsigned long *delay_off,
233 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
234 timer_pending(&led_cdev->blink_timer))
237 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
238 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
241 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
243 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
245 led_blink_setup(led_cdev, delay_on, delay_off);
247 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
249 void led_stop_software_blink(struct led_classdev *led_cdev)
251 del_timer_sync(&led_cdev->blink_timer);
252 led_cdev->blink_delay_on = 0;
253 led_cdev->blink_delay_off = 0;
254 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
256 EXPORT_SYMBOL_GPL(led_stop_software_blink);
258 void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
261 * If software blink is active, delay brightness setting
262 * until the next timer tick.
264 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
266 * If we need to disable soft blinking delegate this to the
267 * work queue task to avoid problems in case we are called
268 * from hard irq context.
271 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
272 schedule_work(&led_cdev->set_brightness_work);
274 set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
275 &led_cdev->work_flags);
276 led_cdev->new_blink_brightness = brightness;
281 led_set_brightness_nosleep(led_cdev, brightness);
283 EXPORT_SYMBOL_GPL(led_set_brightness);
285 void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
287 /* Use brightness_set op if available, it is guaranteed not to sleep */
288 if (!__led_set_brightness(led_cdev, value))
292 * Brightness setting can sleep, delegate it to a work queue task.
293 * value 0 / LED_OFF is special, since it also disables hw-blinking
294 * (sw-blink disable is handled in led_set_brightness()).
295 * To avoid a hw-blink-disable getting lost when a second brightness
296 * change is done immediately afterwards (before the work runs),
297 * it uses a separate work_flag.
300 led_cdev->delayed_set_value = value;
301 set_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
303 clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
304 set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
307 schedule_work(&led_cdev->set_brightness_work);
309 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
311 void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value)
313 led_cdev->brightness = min(value, led_cdev->max_brightness);
315 if (led_cdev->flags & LED_SUSPENDED)
318 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
320 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
322 int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
324 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
327 led_cdev->brightness = min(value, led_cdev->max_brightness);
329 if (led_cdev->flags & LED_SUSPENDED)
332 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
334 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
336 int led_update_brightness(struct led_classdev *led_cdev)
340 if (led_cdev->brightness_get) {
341 ret = led_cdev->brightness_get(led_cdev);
343 led_cdev->brightness = ret;
350 EXPORT_SYMBOL_GPL(led_update_brightness);
352 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
354 struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
358 count = fwnode_property_count_u32(fwnode, "led-pattern");
362 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
366 if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
375 EXPORT_SYMBOL_GPL(led_get_default_pattern);
377 /* Caller must ensure led_cdev->led_access held */
378 void led_sysfs_disable(struct led_classdev *led_cdev)
380 lockdep_assert_held(&led_cdev->led_access);
382 led_cdev->flags |= LED_SYSFS_DISABLE;
384 EXPORT_SYMBOL_GPL(led_sysfs_disable);
386 /* Caller must ensure led_cdev->led_access held */
387 void led_sysfs_enable(struct led_classdev *led_cdev)
389 lockdep_assert_held(&led_cdev->led_access);
391 led_cdev->flags &= ~LED_SYSFS_DISABLE;
393 EXPORT_SYMBOL_GPL(led_sysfs_enable);
395 static void led_parse_fwnode_props(struct device *dev,
396 struct fwnode_handle *fwnode,
397 struct led_properties *props)
404 if (fwnode_property_present(fwnode, "label")) {
405 ret = fwnode_property_read_string(fwnode, "label", &props->label);
407 dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
411 if (fwnode_property_present(fwnode, "color")) {
412 ret = fwnode_property_read_u32(fwnode, "color", &props->color);
414 dev_err(dev, "Error parsing 'color' property (%d)\n", ret);
415 else if (props->color >= LED_COLOR_ID_MAX)
416 dev_err(dev, "LED color identifier out of range\n");
418 props->color_present = true;
422 if (!fwnode_property_present(fwnode, "function"))
425 ret = fwnode_property_read_string(fwnode, "function", &props->function);
428 "Error parsing 'function' property (%d)\n",
432 if (!fwnode_property_present(fwnode, "function-enumerator"))
435 ret = fwnode_property_read_u32(fwnode, "function-enumerator",
439 "Error parsing 'function-enumerator' property (%d)\n",
442 props->func_enum_present = true;
446 int led_compose_name(struct device *dev, struct led_init_data *init_data,
447 char *led_classdev_name)
449 struct led_properties props = {};
450 struct fwnode_handle *fwnode = init_data->fwnode;
451 const char *devicename = init_data->devicename;
453 /* We want to label LEDs that can produce full range of colors
454 * as RGB, not multicolor */
455 BUG_ON(props.color == LED_COLOR_ID_MULTI);
457 if (!led_classdev_name)
460 led_parse_fwnode_props(dev, fwnode, &props);
464 * If init_data.devicename is NULL, then it indicates that
465 * DT label should be used as-is for LED class device name.
466 * Otherwise the label is prepended with devicename to compose
467 * the final LED class device name.
470 strscpy(led_classdev_name, props.label,
473 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
474 devicename, props.label);
476 } else if (props.function || props.color_present) {
477 char tmp_buf[LED_MAX_NAME_SIZE];
479 if (props.func_enum_present) {
480 snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d",
481 props.color_present ? led_colors[props.color] : "",
482 props.function ?: "", props.func_enum);
484 snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s",
485 props.color_present ? led_colors[props.color] : "",
486 props.function ?: "");
488 if (init_data->devname_mandatory) {
489 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
490 devicename, tmp_buf);
492 strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE);
495 } else if (init_data->default_label) {
497 dev_err(dev, "Legacy LED naming requires devicename segment");
500 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
501 devicename, init_data->default_label);
502 } else if (is_of_node(fwnode)) {
503 strscpy(led_classdev_name, to_of_node(fwnode)->name,
510 EXPORT_SYMBOL_GPL(led_compose_name);
512 enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode)
514 const char *state = NULL;
516 if (!fwnode_property_read_string(fwnode, "default-state", &state)) {
517 if (!strcmp(state, "keep"))
518 return LEDS_DEFSTATE_KEEP;
519 if (!strcmp(state, "on"))
520 return LEDS_DEFSTATE_ON;
523 return LEDS_DEFSTATE_OFF;
525 EXPORT_SYMBOL_GPL(led_init_default_state_get);