Merge tag 'backlight-next-5.4' of git://git.kernel.org/pub/scm/linux/kernel/git/lee...
[linux-2.6-microblaze.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 };
38 EXPORT_SYMBOL_GPL(led_colors);
39
40 static int __led_set_brightness(struct led_classdev *led_cdev,
41                                 enum led_brightness value)
42 {
43         if (!led_cdev->brightness_set)
44                 return -ENOTSUPP;
45
46         led_cdev->brightness_set(led_cdev, value);
47
48         return 0;
49 }
50
51 static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
52                                          enum led_brightness 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(struct work_struct *ws)
118 {
119         struct led_classdev *led_cdev =
120                 container_of(ws, struct led_classdev, set_brightness_work);
121         int ret = 0;
122
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);
126         }
127
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);
132         if (ret < 0 &&
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);
138 }
139
140 static void led_set_software_blink(struct led_classdev *led_cdev,
141                                    unsigned long delay_on,
142                                    unsigned long delay_off)
143 {
144         int current_brightness;
145
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;
151
152         led_cdev->blink_delay_on = delay_on;
153         led_cdev->blink_delay_off = delay_off;
154
155         /* never on - just set to off */
156         if (!delay_on) {
157                 led_set_brightness_nosleep(led_cdev, LED_OFF);
158                 return;
159         }
160
161         /* never off - just set to brightness */
162         if (!delay_off) {
163                 led_set_brightness_nosleep(led_cdev,
164                                            led_cdev->blink_brightness);
165                 return;
166         }
167
168         set_bit(LED_BLINK_SW, &led_cdev->work_flags);
169         mod_timer(&led_cdev->blink_timer, jiffies + 1);
170 }
171
172
173 static void led_blink_setup(struct led_classdev *led_cdev,
174                      unsigned long *delay_on,
175                      unsigned long *delay_off)
176 {
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))
180                 return;
181
182         /* blink with 1 Hz as default if nothing specified */
183         if (!*delay_on && !*delay_off)
184                 *delay_on = *delay_off = 500;
185
186         led_set_software_blink(led_cdev, *delay_on, *delay_off);
187 }
188
189 void led_init_core(struct led_classdev *led_cdev)
190 {
191         INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
192
193         timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
194 }
195 EXPORT_SYMBOL_GPL(led_init_core);
196
197 void led_blink_set(struct led_classdev *led_cdev,
198                    unsigned long *delay_on,
199                    unsigned long *delay_off)
200 {
201         del_timer_sync(&led_cdev->blink_timer);
202
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);
206
207         led_blink_setup(led_cdev, delay_on, delay_off);
208 }
209 EXPORT_SYMBOL_GPL(led_blink_set);
210
211 void led_blink_set_oneshot(struct led_classdev *led_cdev,
212                            unsigned long *delay_on,
213                            unsigned long *delay_off,
214                            int invert)
215 {
216         if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
217              timer_pending(&led_cdev->blink_timer))
218                 return;
219
220         set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
221         clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
222
223         if (invert)
224                 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
225         else
226                 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
227
228         led_blink_setup(led_cdev, delay_on, delay_off);
229 }
230 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
231
232 void led_stop_software_blink(struct led_classdev *led_cdev)
233 {
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);
238 }
239 EXPORT_SYMBOL_GPL(led_stop_software_blink);
240
241 void led_set_brightness(struct led_classdev *led_cdev,
242                         enum led_brightness brightness)
243 {
244         /*
245          * If software blink is active, delay brightness setting
246          * until the next timer tick.
247          */
248         if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
249                 /*
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.
253                  */
254                 if (brightness == LED_OFF) {
255                         set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
256                         schedule_work(&led_cdev->set_brightness_work);
257                 } else {
258                         set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
259                                 &led_cdev->work_flags);
260                         led_cdev->new_blink_brightness = brightness;
261                 }
262                 return;
263         }
264
265         led_set_brightness_nosleep(led_cdev, brightness);
266 }
267 EXPORT_SYMBOL_GPL(led_set_brightness);
268
269 void led_set_brightness_nopm(struct led_classdev *led_cdev,
270                               enum led_brightness value)
271 {
272         /* Use brightness_set op if available, it is guaranteed not to sleep */
273         if (!__led_set_brightness(led_cdev, value))
274                 return;
275
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);
279 }
280 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
281
282 void led_set_brightness_nosleep(struct led_classdev *led_cdev,
283                                 enum led_brightness value)
284 {
285         led_cdev->brightness = min(value, led_cdev->max_brightness);
286
287         if (led_cdev->flags & LED_SUSPENDED)
288                 return;
289
290         led_set_brightness_nopm(led_cdev, led_cdev->brightness);
291 }
292 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
293
294 int led_set_brightness_sync(struct led_classdev *led_cdev,
295                             enum led_brightness value)
296 {
297         if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
298                 return -EBUSY;
299
300         led_cdev->brightness = min(value, led_cdev->max_brightness);
301
302         if (led_cdev->flags & LED_SUSPENDED)
303                 return 0;
304
305         return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
306 }
307 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
308
309 int led_update_brightness(struct led_classdev *led_cdev)
310 {
311         int ret = 0;
312
313         if (led_cdev->brightness_get) {
314                 ret = led_cdev->brightness_get(led_cdev);
315                 if (ret >= 0) {
316                         led_cdev->brightness = ret;
317                         return 0;
318                 }
319         }
320
321         return ret;
322 }
323 EXPORT_SYMBOL_GPL(led_update_brightness);
324
325 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
326 {
327         struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
328         u32 *pattern;
329         int count;
330
331         count = fwnode_property_count_u32(fwnode, "led-pattern");
332         if (count < 0)
333                 return NULL;
334
335         pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
336         if (!pattern)
337                 return NULL;
338
339         if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
340                 kfree(pattern);
341                 return NULL;
342         }
343
344         *size = count;
345
346         return pattern;
347 }
348 EXPORT_SYMBOL_GPL(led_get_default_pattern);
349
350 /* Caller must ensure led_cdev->led_access held */
351 void led_sysfs_disable(struct led_classdev *led_cdev)
352 {
353         lockdep_assert_held(&led_cdev->led_access);
354
355         led_cdev->flags |= LED_SYSFS_DISABLE;
356 }
357 EXPORT_SYMBOL_GPL(led_sysfs_disable);
358
359 /* Caller must ensure led_cdev->led_access held */
360 void led_sysfs_enable(struct led_classdev *led_cdev)
361 {
362         lockdep_assert_held(&led_cdev->led_access);
363
364         led_cdev->flags &= ~LED_SYSFS_DISABLE;
365 }
366 EXPORT_SYMBOL_GPL(led_sysfs_enable);
367
368 static void led_parse_fwnode_props(struct device *dev,
369                                    struct fwnode_handle *fwnode,
370                                    struct led_properties *props)
371 {
372         int ret;
373
374         if (!fwnode)
375                 return;
376
377         if (fwnode_property_present(fwnode, "label")) {
378                 ret = fwnode_property_read_string(fwnode, "label", &props->label);
379                 if (ret)
380                         dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
381                 return;
382         }
383
384         if (fwnode_property_present(fwnode, "color")) {
385                 ret = fwnode_property_read_u32(fwnode, "color", &props->color);
386                 if (ret)
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");
390                 else
391                         props->color_present = true;
392         }
393
394
395         if (!fwnode_property_present(fwnode, "function"))
396                 return;
397
398         ret = fwnode_property_read_string(fwnode, "function", &props->function);
399         if (ret) {
400                 dev_err(dev,
401                         "Error parsing 'function' property (%d)\n",
402                         ret);
403         }
404
405         if (!fwnode_property_present(fwnode, "function-enumerator"))
406                 return;
407
408         ret = fwnode_property_read_u32(fwnode, "function-enumerator",
409                                        &props->func_enum);
410         if (ret) {
411                 dev_err(dev,
412                         "Error parsing 'function-enumerator' property (%d)\n",
413                         ret);
414         } else {
415                 props->func_enum_present = true;
416         }
417 }
418
419 int led_compose_name(struct device *dev, struct led_init_data *init_data,
420                      char *led_classdev_name)
421 {
422         struct led_properties props = {};
423         struct fwnode_handle *fwnode = init_data->fwnode;
424         const char *devicename = init_data->devicename;
425
426         if (!led_classdev_name)
427                 return -EINVAL;
428
429         led_parse_fwnode_props(dev, fwnode, &props);
430
431         if (props.label) {
432                 /*
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.
437                  */
438                 if (!devicename) {
439                         strscpy(led_classdev_name, props.label,
440                                 LED_MAX_NAME_SIZE);
441                 } else {
442                         snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
443                                  devicename, props.label);
444                 }
445         } else if (props.function || props.color_present) {
446                 char tmp_buf[LED_MAX_NAME_SIZE];
447
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);
452                 } else {
453                         snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s",
454                                  props.color_present ? led_colors[props.color] : "",
455                                  props.function ?: "");
456                 }
457                 if (init_data->devname_mandatory) {
458                         snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s",
459                                  devicename, tmp_buf);
460                 } else {
461                         strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE);
462
463                 }
464         } else if (init_data->default_label) {
465                 if (!devicename) {
466                         dev_err(dev, "Legacy LED naming requires devicename segment");
467                         return -EINVAL;
468                 }
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,
473                         LED_MAX_NAME_SIZE);
474         } else
475                 return -EINVAL;
476
477         return 0;
478 }
479 EXPORT_SYMBOL_GPL(led_compose_name);