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",
38 EXPORT_SYMBOL_GPL(led_colors
);
40 static int __led_set_brightness(struct led_classdev
*led_cdev
,
41 enum led_brightness value
)
43 if (!led_cdev
->brightness_set
)
46 led_cdev
->brightness_set(led_cdev
, value
);
51 static int __led_set_brightness_blocking(struct led_classdev
*led_cdev
,
52 enum led_brightness 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(struct work_struct
*ws
)
119 struct led_classdev
*led_cdev
=
120 container_of(ws
, struct led_classdev
, set_brightness_work
);
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
);
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
);
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
);
140 static void led_set_software_blink(struct led_classdev
*led_cdev
,
141 unsigned long delay_on
,
142 unsigned long delay_off
)
144 int current_brightness
;
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
;
152 led_cdev
->blink_delay_on
= delay_on
;
153 led_cdev
->blink_delay_off
= delay_off
;
155 /* never on - just set to off */
157 led_set_brightness_nosleep(led_cdev
, LED_OFF
);
161 /* never off - just set to brightness */
163 led_set_brightness_nosleep(led_cdev
,
164 led_cdev
->blink_brightness
);
168 set_bit(LED_BLINK_SW
, &led_cdev
->work_flags
);
169 mod_timer(&led_cdev
->blink_timer
, jiffies
+ 1);
173 static void led_blink_setup(struct led_classdev
*led_cdev
,
174 unsigned long *delay_on
,
175 unsigned long *delay_off
)
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
))
182 /* blink with 1 Hz as default if nothing specified */
183 if (!*delay_on
&& !*delay_off
)
184 *delay_on
= *delay_off
= 500;
186 led_set_software_blink(led_cdev
, *delay_on
, *delay_off
);
189 void led_init_core(struct led_classdev
*led_cdev
)
191 INIT_WORK(&led_cdev
->set_brightness_work
, set_brightness_delayed
);
193 timer_setup(&led_cdev
->blink_timer
, led_timer_function
, 0);
195 EXPORT_SYMBOL_GPL(led_init_core
);
197 void led_blink_set(struct led_classdev
*led_cdev
,
198 unsigned long *delay_on
,
199 unsigned long *delay_off
)
201 del_timer_sync(&led_cdev
->blink_timer
);
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
);
207 led_blink_setup(led_cdev
, delay_on
, delay_off
);
209 EXPORT_SYMBOL_GPL(led_blink_set
);
211 void led_blink_set_oneshot(struct led_classdev
*led_cdev
,
212 unsigned long *delay_on
,
213 unsigned long *delay_off
,
216 if (test_bit(LED_BLINK_ONESHOT
, &led_cdev
->work_flags
) &&
217 timer_pending(&led_cdev
->blink_timer
))
220 set_bit(LED_BLINK_ONESHOT
, &led_cdev
->work_flags
);
221 clear_bit(LED_BLINK_ONESHOT_STOP
, &led_cdev
->work_flags
);
224 set_bit(LED_BLINK_INVERT
, &led_cdev
->work_flags
);
226 clear_bit(LED_BLINK_INVERT
, &led_cdev
->work_flags
);
228 led_blink_setup(led_cdev
, delay_on
, delay_off
);
230 EXPORT_SYMBOL_GPL(led_blink_set_oneshot
);
232 void led_stop_software_blink(struct led_classdev
*led_cdev
)
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
);
239 EXPORT_SYMBOL_GPL(led_stop_software_blink
);
241 void led_set_brightness(struct led_classdev
*led_cdev
,
242 enum led_brightness brightness
)
245 * If software blink is active, delay brightness setting
246 * until the next timer tick.
248 if (test_bit(LED_BLINK_SW
, &led_cdev
->work_flags
)) {
250 * If we need to disable soft blinking delegate this to the
251 * work queue task to avoid problems in case we are called
252 * from hard irq context.
254 if (brightness
== LED_OFF
) {
255 set_bit(LED_BLINK_DISABLE
, &led_cdev
->work_flags
);
256 schedule_work(&led_cdev
->set_brightness_work
);
258 set_bit(LED_BLINK_BRIGHTNESS_CHANGE
,
259 &led_cdev
->work_flags
);
260 led_cdev
->new_blink_brightness
= brightness
;
265 led_set_brightness_nosleep(led_cdev
, brightness
);
267 EXPORT_SYMBOL_GPL(led_set_brightness
);
269 void led_set_brightness_nopm(struct led_classdev
*led_cdev
,
270 enum led_brightness value
)
272 /* Use brightness_set op if available, it is guaranteed not to sleep */
273 if (!__led_set_brightness(led_cdev
, value
))
276 /* If brightness setting can sleep, delegate it to a work queue task */
277 led_cdev
->delayed_set_value
= value
;
278 schedule_work(&led_cdev
->set_brightness_work
);
280 EXPORT_SYMBOL_GPL(led_set_brightness_nopm
);
282 void led_set_brightness_nosleep(struct led_classdev
*led_cdev
,
283 enum led_brightness value
)
285 led_cdev
->brightness
= min(value
, led_cdev
->max_brightness
);
287 if (led_cdev
->flags
& LED_SUSPENDED
)
290 led_set_brightness_nopm(led_cdev
, led_cdev
->brightness
);
292 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep
);
294 int led_set_brightness_sync(struct led_classdev
*led_cdev
,
295 enum led_brightness value
)
297 if (led_cdev
->blink_delay_on
|| led_cdev
->blink_delay_off
)
300 led_cdev
->brightness
= min(value
, led_cdev
->max_brightness
);
302 if (led_cdev
->flags
& LED_SUSPENDED
)
305 return __led_set_brightness_blocking(led_cdev
, led_cdev
->brightness
);
307 EXPORT_SYMBOL_GPL(led_set_brightness_sync
);
309 int led_update_brightness(struct led_classdev
*led_cdev
)
313 if (led_cdev
->brightness_get
) {
314 ret
= led_cdev
->brightness_get(led_cdev
);
316 led_cdev
->brightness
= ret
;
323 EXPORT_SYMBOL_GPL(led_update_brightness
);
325 u32
*led_get_default_pattern(struct led_classdev
*led_cdev
, unsigned int *size
)
327 struct fwnode_handle
*fwnode
= led_cdev
->dev
->fwnode
;
331 count
= fwnode_property_count_u32(fwnode
, "led-pattern");
335 pattern
= kcalloc(count
, sizeof(*pattern
), GFP_KERNEL
);
339 if (fwnode_property_read_u32_array(fwnode
, "led-pattern", pattern
, count
)) {
348 EXPORT_SYMBOL_GPL(led_get_default_pattern
);
350 /* Caller must ensure led_cdev->led_access held */
351 void led_sysfs_disable(struct led_classdev
*led_cdev
)
353 lockdep_assert_held(&led_cdev
->led_access
);
355 led_cdev
->flags
|= LED_SYSFS_DISABLE
;
357 EXPORT_SYMBOL_GPL(led_sysfs_disable
);
359 /* Caller must ensure led_cdev->led_access held */
360 void led_sysfs_enable(struct led_classdev
*led_cdev
)
362 lockdep_assert_held(&led_cdev
->led_access
);
364 led_cdev
->flags
&= ~LED_SYSFS_DISABLE
;
366 EXPORT_SYMBOL_GPL(led_sysfs_enable
);
368 static void led_parse_fwnode_props(struct device
*dev
,
369 struct fwnode_handle
*fwnode
,
370 struct led_properties
*props
)
377 if (fwnode_property_present(fwnode
, "label")) {
378 ret
= fwnode_property_read_string(fwnode
, "label", &props
->label
);
380 dev_err(dev
, "Error parsing 'label' property (%d)\n", ret
);
384 if (fwnode_property_present(fwnode
, "color")) {
385 ret
= fwnode_property_read_u32(fwnode
, "color", &props
->color
);
387 dev_err(dev
, "Error parsing 'color' property (%d)\n", ret
);
388 else if (props
->color
>= LED_COLOR_ID_MAX
)
389 dev_err(dev
, "LED color identifier out of range\n");
391 props
->color_present
= true;
395 if (!fwnode_property_present(fwnode
, "function"))
398 ret
= fwnode_property_read_string(fwnode
, "function", &props
->function
);
401 "Error parsing 'function' property (%d)\n",
405 if (!fwnode_property_present(fwnode
, "function-enumerator"))
408 ret
= fwnode_property_read_u32(fwnode
, "function-enumerator",
412 "Error parsing 'function-enumerator' property (%d)\n",
415 props
->func_enum_present
= true;
419 int led_compose_name(struct device
*dev
, struct led_init_data
*init_data
,
420 char *led_classdev_name
)
422 struct led_properties props
= {};
423 struct fwnode_handle
*fwnode
= init_data
->fwnode
;
424 const char *devicename
= init_data
->devicename
;
426 if (!led_classdev_name
)
429 led_parse_fwnode_props(dev
, fwnode
, &props
);
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.
439 strscpy(led_classdev_name
, props
.label
,
442 snprintf(led_classdev_name
, LED_MAX_NAME_SIZE
, "%s:%s",
443 devicename
, props
.label
);
445 } else if (props
.function
|| props
.color_present
) {
446 char tmp_buf
[LED_MAX_NAME_SIZE
];
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
);
453 snprintf(tmp_buf
, LED_MAX_NAME_SIZE
, "%s:%s",
454 props
.color_present
? led_colors
[props
.color
] : "",
455 props
.function
?: "");
457 if (init_data
->devname_mandatory
) {
458 snprintf(led_classdev_name
, LED_MAX_NAME_SIZE
, "%s:%s",
459 devicename
, tmp_buf
);
461 strscpy(led_classdev_name
, tmp_buf
, LED_MAX_NAME_SIZE
);
464 } else if (init_data
->default_label
) {
466 dev_err(dev
, "Legacy LED naming requires devicename segment");
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
,
479 EXPORT_SYMBOL_GPL(led_compose_name
);