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);
156 if (test_and_clear_bit(LED_SET_BLINK, &led_cdev->work_flags)) {
157 unsigned long delay_on = led_cdev->delayed_delay_on;
158 unsigned long delay_off = led_cdev->delayed_delay_off;
160 led_blink_set(led_cdev, &delay_on, &delay_off);
164 static void led_set_software_blink(struct led_classdev *led_cdev,
165 unsigned long delay_on,
166 unsigned long delay_off)
168 int current_brightness;
170 current_brightness = led_get_brightness(led_cdev);
171 if (current_brightness)
172 led_cdev->blink_brightness = current_brightness;
173 if (!led_cdev->blink_brightness)
174 led_cdev->blink_brightness = led_cdev->max_brightness;
176 led_cdev->blink_delay_on = delay_on;
177 led_cdev->blink_delay_off = delay_off;
179 /* never on - just set to off */
181 led_set_brightness_nosleep(led_cdev, LED_OFF);
185 /* never off - just set to brightness */
187 led_set_brightness_nosleep(led_cdev,
188 led_cdev->blink_brightness);
192 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
193 mod_timer(&led_cdev->blink_timer, jiffies + 1);
197 static void led_blink_setup(struct led_classdev *led_cdev,
198 unsigned long *delay_on,
199 unsigned long *delay_off)
201 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
202 led_cdev->blink_set &&
203 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
206 /* blink with 1 Hz as default if nothing specified */
207 if (!*delay_on && !*delay_off)
208 *delay_on = *delay_off = 500;
210 led_set_software_blink(led_cdev, *delay_on, *delay_off);
213 void led_init_core(struct led_classdev *led_cdev)
215 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
217 timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
219 EXPORT_SYMBOL_GPL(led_init_core);
221 void led_blink_set(struct led_classdev *led_cdev,
222 unsigned long *delay_on,
223 unsigned long *delay_off)
225 del_timer_sync(&led_cdev->blink_timer);
227 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
228 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
229 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
231 led_blink_setup(led_cdev, delay_on, delay_off);
233 EXPORT_SYMBOL_GPL(led_blink_set);
235 void led_blink_set_oneshot(struct led_classdev *led_cdev,
236 unsigned long *delay_on,
237 unsigned long *delay_off,
240 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
241 timer_pending(&led_cdev->blink_timer))
244 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
245 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
248 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
250 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
252 led_blink_setup(led_cdev, delay_on, delay_off);
254 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
256 void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on,
257 unsigned long delay_off)
259 /* If necessary delegate to a work queue task. */
260 if (led_cdev->blink_set && led_cdev->brightness_set_blocking) {
261 led_cdev->delayed_delay_on = delay_on;
262 led_cdev->delayed_delay_off = delay_off;
263 set_bit(LED_SET_BLINK, &led_cdev->work_flags);
264 schedule_work(&led_cdev->set_brightness_work);
268 led_blink_set(led_cdev, &delay_on, &delay_off);
270 EXPORT_SYMBOL_GPL(led_blink_set_nosleep);
272 void led_stop_software_blink(struct led_classdev *led_cdev)
274 del_timer_sync(&led_cdev->blink_timer);
275 led_cdev->blink_delay_on = 0;
276 led_cdev->blink_delay_off = 0;
277 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
279 EXPORT_SYMBOL_GPL(led_stop_software_blink);
281 void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
284 * If software blink is active, delay brightness setting
285 * until the next timer tick.
287 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
289 * If we need to disable soft blinking delegate this to the
290 * work queue task to avoid problems in case we are called
291 * from hard irq context.
294 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
295 schedule_work(&led_cdev->set_brightness_work);
297 set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
298 &led_cdev->work_flags);
299 led_cdev->new_blink_brightness = brightness;
304 led_set_brightness_nosleep(led_cdev, brightness);
306 EXPORT_SYMBOL_GPL(led_set_brightness);
308 void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
310 /* Use brightness_set op if available, it is guaranteed not to sleep */
311 if (!__led_set_brightness(led_cdev, value))
315 * Brightness setting can sleep, delegate it to a work queue task.
316 * value 0 / LED_OFF is special, since it also disables hw-blinking
317 * (sw-blink disable is handled in led_set_brightness()).
318 * To avoid a hw-blink-disable getting lost when a second brightness
319 * change is done immediately afterwards (before the work runs),
320 * it uses a separate work_flag.
323 led_cdev->delayed_set_value = value;
324 set_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
326 clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
327 clear_bit(LED_SET_BLINK, &led_cdev->work_flags);
328 set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
331 schedule_work(&led_cdev->set_brightness_work);
333 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
335 void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value)
337 led_cdev->brightness = min(value, led_cdev->max_brightness);
339 if (led_cdev->flags & LED_SUSPENDED)
342 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
344 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
346 int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
348 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
351 led_cdev->brightness = min(value, led_cdev->max_brightness);
353 if (led_cdev->flags & LED_SUSPENDED)
356 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
358 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
360 int led_update_brightness(struct led_classdev *led_cdev)
364 if (led_cdev->brightness_get) {
365 ret = led_cdev->brightness_get(led_cdev);
367 led_cdev->brightness = ret;
374 EXPORT_SYMBOL_GPL(led_update_brightness);
376 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
378 struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
382 count = fwnode_property_count_u32(fwnode, "led-pattern");
386 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
390 if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
399 EXPORT_SYMBOL_GPL(led_get_default_pattern);
401 /* Caller must ensure led_cdev->led_access held */
402 void led_sysfs_disable(struct led_classdev *led_cdev)
404 lockdep_assert_held(&led_cdev->led_access);
406 led_cdev->flags |= LED_SYSFS_DISABLE;
408 EXPORT_SYMBOL_GPL(led_sysfs_disable);
410 /* Caller must ensure led_cdev->led_access held */
411 void led_sysfs_enable(struct led_classdev *led_cdev)
413 lockdep_assert_held(&led_cdev->led_access);
415 led_cdev->flags &= ~LED_SYSFS_DISABLE;
417 EXPORT_SYMBOL_GPL(led_sysfs_enable);
419 static void led_parse_fwnode_props(struct device *dev,
420 struct fwnode_handle *fwnode,
421 struct led_properties *props)
428 if (fwnode_property_present(fwnode, "label")) {
429 ret = fwnode_property_read_string(fwnode, "label", &props->label);
431 dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
435 if (fwnode_property_present(fwnode, "color")) {
436 ret = fwnode_property_read_u32(fwnode, "color", &props->color);
438 dev_err(dev, "Error parsing 'color' property (%d)\n", ret);
439 else if (props->color >= LED_COLOR_ID_MAX)
440 dev_err(dev, "LED color identifier out of range\n");
442 props->color_present = true;
446 if (!fwnode_property_present(fwnode, "function"))
449 ret = fwnode_property_read_string(fwnode, "function", &props->function);
452 "Error parsing 'function' property (%d)\n",
456 if (!fwnode_property_present(fwnode, "function-enumerator"))
459 ret = fwnode_property_read_u32(fwnode, "function-enumerator",
463 "Error parsing 'function-enumerator' property (%d)\n",
466 props->func_enum_present = true;
470 int led_compose_name(struct device *dev, struct led_init_data *init_data,
471 char *led_classdev_name)
473 struct led_properties props = {};
474 struct fwnode_handle *fwnode = init_data->fwnode;
475 const char *devicename = init_data->devicename;
477 /* We want to label LEDs that can produce full range of colors
478 * as RGB, not multicolor */
479 BUG_ON(props.color == LED_COLOR_ID_MULTI);
481 if (!led_classdev_name)
484 led_parse_fwnode_props(dev, fwnode, &props);
488 * If init_data.devicename is NULL, then it indicates that
489 * DT label should be used as-is for LED class device name.
490 * Otherwise the label is prepended with devicename to compose
491 * the final LED class device name.
494 strscpy(led_classdev_name, props.label,
497 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
498 devicename, props.label);
500 } else if (props.function || props.color_present) {
501 char tmp_buf[LED_MAX_NAME_SIZE];
503 if (props.func_enum_present) {
504 snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d",
505 props.color_present ? led_colors[props.color] : "",
506 props.function ?: "", props.func_enum);
508 snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s",
509 props.color_present ? led_colors[props.color] : "",
510 props.function ?: "");
512 if (init_data->devname_mandatory) {
513 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
514 devicename, tmp_buf);
516 strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE);
519 } else if (init_data->default_label) {
521 dev_err(dev, "Legacy LED naming requires devicename segment");
524 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
525 devicename, init_data->default_label);
526 } else if (is_of_node(fwnode)) {
527 strscpy(led_classdev_name, to_of_node(fwnode)->name,
534 EXPORT_SYMBOL_GPL(led_compose_name);
536 enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode)
538 const char *state = NULL;
540 if (!fwnode_property_read_string(fwnode, "default-state", &state)) {
541 if (!strcmp(state, "keep"))
542 return LEDS_DEFSTATE_KEEP;
543 if (!strcmp(state, "on"))
544 return LEDS_DEFSTATE_ON;
547 return LEDS_DEFSTATE_OFF;
549 EXPORT_SYMBOL_GPL(led_init_default_state_get);