leds: Fix set_brightness_delayed() race
[platform/kernel/linux-starfive.git] / drivers / leds / led-core.c
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_set_brightness(struct led_classdev *led_cdev,
118                                                   unsigned int value)
119 {
120         int ret = 0;
121
122         ret = __led_set_brightness(led_cdev, value);
123         if (ret == -ENOTSUPP)
124                 ret = __led_set_brightness_blocking(led_cdev, value);
125         if (ret < 0 &&
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);
131 }
132
133 static void set_brightness_delayed(struct work_struct *ws)
134 {
135         struct led_classdev *led_cdev =
136                 container_of(ws, struct led_classdev, set_brightness_work);
137
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);
141         }
142
143         /*
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.
149          */
150         if (test_and_clear_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags))
151                 set_brightness_delayed_set_brightness(led_cdev, LED_OFF);
152
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);
155 }
156
157 static void led_set_software_blink(struct led_classdev *led_cdev,
158                                    unsigned long delay_on,
159                                    unsigned long delay_off)
160 {
161         int current_brightness;
162
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;
168
169         led_cdev->blink_delay_on = delay_on;
170         led_cdev->blink_delay_off = delay_off;
171
172         /* never on - just set to off */
173         if (!delay_on) {
174                 led_set_brightness_nosleep(led_cdev, LED_OFF);
175                 return;
176         }
177
178         /* never off - just set to brightness */
179         if (!delay_off) {
180                 led_set_brightness_nosleep(led_cdev,
181                                            led_cdev->blink_brightness);
182                 return;
183         }
184
185         set_bit(LED_BLINK_SW, &led_cdev->work_flags);
186         mod_timer(&led_cdev->blink_timer, jiffies + 1);
187 }
188
189
190 static void led_blink_setup(struct led_classdev *led_cdev,
191                      unsigned long *delay_on,
192                      unsigned long *delay_off)
193 {
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))
197                 return;
198
199         /* blink with 1 Hz as default if nothing specified */
200         if (!*delay_on && !*delay_off)
201                 *delay_on = *delay_off = 500;
202
203         led_set_software_blink(led_cdev, *delay_on, *delay_off);
204 }
205
206 void led_init_core(struct led_classdev *led_cdev)
207 {
208         INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
209
210         timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
211 }
212 EXPORT_SYMBOL_GPL(led_init_core);
213
214 void led_blink_set(struct led_classdev *led_cdev,
215                    unsigned long *delay_on,
216                    unsigned long *delay_off)
217 {
218         del_timer_sync(&led_cdev->blink_timer);
219
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);
223
224         led_blink_setup(led_cdev, delay_on, delay_off);
225 }
226 EXPORT_SYMBOL_GPL(led_blink_set);
227
228 void led_blink_set_oneshot(struct led_classdev *led_cdev,
229                            unsigned long *delay_on,
230                            unsigned long *delay_off,
231                            int invert)
232 {
233         if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
234              timer_pending(&led_cdev->blink_timer))
235                 return;
236
237         set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
238         clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
239
240         if (invert)
241                 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
242         else
243                 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
244
245         led_blink_setup(led_cdev, delay_on, delay_off);
246 }
247 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
248
249 void led_stop_software_blink(struct led_classdev *led_cdev)
250 {
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);
255 }
256 EXPORT_SYMBOL_GPL(led_stop_software_blink);
257
258 void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
259 {
260         /*
261          * If software blink is active, delay brightness setting
262          * until the next timer tick.
263          */
264         if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
265                 /*
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.
269                  */
270                 if (!brightness) {
271                         set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
272                         schedule_work(&led_cdev->set_brightness_work);
273                 } else {
274                         set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
275                                 &led_cdev->work_flags);
276                         led_cdev->new_blink_brightness = brightness;
277                 }
278                 return;
279         }
280
281         led_set_brightness_nosleep(led_cdev, brightness);
282 }
283 EXPORT_SYMBOL_GPL(led_set_brightness);
284
285 void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
286 {
287         /* Use brightness_set op if available, it is guaranteed not to sleep */
288         if (!__led_set_brightness(led_cdev, value))
289                 return;
290
291         /*
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.
298          */
299         if (value) {
300                 led_cdev->delayed_set_value = value;
301                 set_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
302         } else {
303                 clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
304                 set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
305         }
306
307         schedule_work(&led_cdev->set_brightness_work);
308 }
309 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
310
311 void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value)
312 {
313         led_cdev->brightness = min(value, led_cdev->max_brightness);
314
315         if (led_cdev->flags & LED_SUSPENDED)
316                 return;
317
318         led_set_brightness_nopm(led_cdev, led_cdev->brightness);
319 }
320 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
321
322 int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
323 {
324         if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
325                 return -EBUSY;
326
327         led_cdev->brightness = min(value, led_cdev->max_brightness);
328
329         if (led_cdev->flags & LED_SUSPENDED)
330                 return 0;
331
332         return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
333 }
334 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
335
336 int led_update_brightness(struct led_classdev *led_cdev)
337 {
338         int ret = 0;
339
340         if (led_cdev->brightness_get) {
341                 ret = led_cdev->brightness_get(led_cdev);
342                 if (ret >= 0) {
343                         led_cdev->brightness = ret;
344                         return 0;
345                 }
346         }
347
348         return ret;
349 }
350 EXPORT_SYMBOL_GPL(led_update_brightness);
351
352 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
353 {
354         struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
355         u32 *pattern;
356         int count;
357
358         count = fwnode_property_count_u32(fwnode, "led-pattern");
359         if (count < 0)
360                 return NULL;
361
362         pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
363         if (!pattern)
364                 return NULL;
365
366         if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
367                 kfree(pattern);
368                 return NULL;
369         }
370
371         *size = count;
372
373         return pattern;
374 }
375 EXPORT_SYMBOL_GPL(led_get_default_pattern);
376
377 /* Caller must ensure led_cdev->led_access held */
378 void led_sysfs_disable(struct led_classdev *led_cdev)
379 {
380         lockdep_assert_held(&led_cdev->led_access);
381
382         led_cdev->flags |= LED_SYSFS_DISABLE;
383 }
384 EXPORT_SYMBOL_GPL(led_sysfs_disable);
385
386 /* Caller must ensure led_cdev->led_access held */
387 void led_sysfs_enable(struct led_classdev *led_cdev)
388 {
389         lockdep_assert_held(&led_cdev->led_access);
390
391         led_cdev->flags &= ~LED_SYSFS_DISABLE;
392 }
393 EXPORT_SYMBOL_GPL(led_sysfs_enable);
394
395 static void led_parse_fwnode_props(struct device *dev,
396                                    struct fwnode_handle *fwnode,
397                                    struct led_properties *props)
398 {
399         int ret;
400
401         if (!fwnode)
402                 return;
403
404         if (fwnode_property_present(fwnode, "label")) {
405                 ret = fwnode_property_read_string(fwnode, "label", &props->label);
406                 if (ret)
407                         dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
408                 return;
409         }
410
411         if (fwnode_property_present(fwnode, "color")) {
412                 ret = fwnode_property_read_u32(fwnode, "color", &props->color);
413                 if (ret)
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");
417                 else
418                         props->color_present = true;
419         }
420
421
422         if (!fwnode_property_present(fwnode, "function"))
423                 return;
424
425         ret = fwnode_property_read_string(fwnode, "function", &props->function);
426         if (ret) {
427                 dev_err(dev,
428                         "Error parsing 'function' property (%d)\n",
429                         ret);
430         }
431
432         if (!fwnode_property_present(fwnode, "function-enumerator"))
433                 return;
434
435         ret = fwnode_property_read_u32(fwnode, "function-enumerator",
436                                        &props->func_enum);
437         if (ret) {
438                 dev_err(dev,
439                         "Error parsing 'function-enumerator' property (%d)\n",
440                         ret);
441         } else {
442                 props->func_enum_present = true;
443         }
444 }
445
446 int led_compose_name(struct device *dev, struct led_init_data *init_data,
447                      char *led_classdev_name)
448 {
449         struct led_properties props = {};
450         struct fwnode_handle *fwnode = init_data->fwnode;
451         const char *devicename = init_data->devicename;
452
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);
456
457         if (!led_classdev_name)
458                 return -EINVAL;
459
460         led_parse_fwnode_props(dev, fwnode, &props);
461
462         if (props.label) {
463                 /*
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.
468                  */
469                 if (!devicename) {
470                         strscpy(led_classdev_name, props.label,
471                                 LED_MAX_NAME_SIZE);
472                 } else {
473                         snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
474                                  devicename, props.label);
475                 }
476         } else if (props.function || props.color_present) {
477                 char tmp_buf[LED_MAX_NAME_SIZE];
478
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);
483                 } else {
484                         snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s",
485                                  props.color_present ? led_colors[props.color] : "",
486                                  props.function ?: "");
487                 }
488                 if (init_data->devname_mandatory) {
489                         snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
490                                  devicename, tmp_buf);
491                 } else {
492                         strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE);
493
494                 }
495         } else if (init_data->default_label) {
496                 if (!devicename) {
497                         dev_err(dev, "Legacy LED naming requires devicename segment");
498                         return -EINVAL;
499                 }
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,
504                         LED_MAX_NAME_SIZE);
505         } else
506                 return -EINVAL;
507
508         return 0;
509 }
510 EXPORT_SYMBOL_GPL(led_compose_name);
511
512 enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode)
513 {
514         const char *state = NULL;
515
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;
521         }
522
523         return LEDS_DEFSTATE_OFF;
524 }
525 EXPORT_SYMBOL_GPL(led_init_default_state_get);