blob: 4a97cb7457888c6dd3c265985548163209f8d647 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Richard Purdiec72a1d62006-03-31 02:31:04 -08002/*
3 * LED Class Core
4 *
5 * Copyright 2005-2006 Openedhand Ltd.
6 *
7 * Author: Richard Purdie <rpurdie@openedhand.com>
Richard Purdiec72a1d62006-03-31 02:31:04 -08008 */
9
10#include <linux/kernel.h>
Jacek Anaszewski04713302014-08-07 05:10:22 -070011#include <linux/leds.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080012#include <linux/list.h>
13#include <linux/module.h>
Jacek Anaszewski04713302014-08-07 05:10:22 -070014#include <linux/mutex.h>
Krzysztof Kozlowski8e1f45612019-01-09 15:44:46 +010015#include <linux/of.h>
Jacek Anaszewskibb4e9af2019-06-09 20:19:04 +020016#include <linux/property.h>
Richard Purdie72f8da32007-12-31 23:09:44 +000017#include <linux/rwsem.h>
Krzysztof Kozlowski8e1f45612019-01-09 15:44:46 +010018#include <linux/slab.h>
Jacek Anaszewskibb4e9af2019-06-09 20:19:04 +020019#include <uapi/linux/uleds.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080020#include "leds.h"
21
Richard Purdie72f8da32007-12-31 23:09:44 +000022DECLARE_RWSEM(leds_list_lock);
Richard Purdiec72a1d62006-03-31 02:31:04 -080023EXPORT_SYMBOL_GPL(leds_list_lock);
Németh Márton4d404fd2008-03-09 20:59:57 +000024
25LIST_HEAD(leds_list);
26EXPORT_SYMBOL_GPL(leds_list);
Bryan Wua403d932012-03-23 15:02:14 -070027
Jacek Anaszewskibb4e9af2019-06-09 20:19:04 +020028const 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",
Dan Murphy10d3e0d2020-07-13 10:45:32 -050037 [LED_COLOR_ID_MULTI] = "multicolor",
Pavel Machek54212f52020-08-03 13:20:06 +020038 [LED_COLOR_ID_RGB] = "rgb",
Jacek Anaszewskibb4e9af2019-06-09 20:19:04 +020039};
40EXPORT_SYMBOL_GPL(led_colors);
41
Abanoub Samehaf0bfab2020-12-11 22:42:08 +020042static int __led_set_brightness(struct led_classdev *led_cdev, unsigned int value)
Heiner Kallweitd4887af2016-02-16 20:21:56 +010043{
44 if (!led_cdev->brightness_set)
45 return -ENOTSUPP;
46
47 led_cdev->brightness_set(led_cdev, value);
48
49 return 0;
50}
51
Abanoub Samehaf0bfab2020-12-11 22:42:08 +020052static int __led_set_brightness_blocking(struct led_classdev *led_cdev, unsigned int value)
Heiner Kallweitd4887af2016-02-16 20:21:56 +010053{
54 if (!led_cdev->brightness_set_blocking)
55 return -ENOTSUPP;
56
57 return led_cdev->brightness_set_blocking(led_cdev, value);
58}
59
Kees Cook49404662017-10-25 03:30:01 -070060static void led_timer_function(struct timer_list *t)
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020061{
Kees Cook49404662017-10-25 03:30:01 -070062 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020063 unsigned long brightness;
64 unsigned long delay;
65
66 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020067 led_set_brightness_nosleep(led_cdev, LED_OFF);
Hans de Goedea9c6ce52016-11-08 14:38:46 +010068 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020069 return;
70 }
71
Hans de Goedea9c6ce52016-11-08 14:38:46 +010072 if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
73 &led_cdev->work_flags)) {
74 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020075 return;
76 }
77
78 brightness = led_get_brightness(led_cdev);
79 if (!brightness) {
80 /* Time to switch the LED on. */
Hans de Goedeeb1610b2016-10-23 21:47:26 +020081 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;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020086 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 */
Hans de Goedeeb1610b2016-10-23 21:47:26 +020091 led_cdev->blink_brightness = brightness;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020092 brightness = LED_OFF;
93 delay = led_cdev->blink_delay_off;
94 }
95
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020096 led_set_brightness_nosleep(led_cdev, brightness);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020097
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 */
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100102 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
103 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200104 if (brightness)
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100105 set_bit(LED_BLINK_ONESHOT_STOP,
106 &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200107 } else {
108 if (!brightness)
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100109 set_bit(LED_BLINK_ONESHOT_STOP,
110 &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200111 }
112 }
113
114 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
115}
116
117static 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);
Jacek Anaszewski1afcadf2015-10-19 09:04:01 +0200121 int ret = 0;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200122
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100123 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200124 led_cdev->delayed_set_value = LED_OFF;
125 led_stop_software_blink(led_cdev);
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200126 }
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200127
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100128 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);
Heiner Kallweitd84d80f2016-01-22 21:43:48 +0100132 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)))
Jacek Anaszewski1afcadf2015-10-19 09:04:01 +0200136 dev_err(led_cdev->dev,
137 "Setting an LED's brightness failed (%d)\n", ret);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200138}
139
Bryan Wua403d932012-03-23 15:02:14 -0700140static 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
Bryan Wua403d932012-03-23 15:02:14 -0700152 led_cdev->blink_delay_on = delay_on;
153 led_cdev->blink_delay_off = delay_off;
154
Stefan Sørensen8d82fef2014-02-04 00:11:42 -0800155 /* never on - just set to off */
156 if (!delay_on) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200157 led_set_brightness_nosleep(led_cdev, LED_OFF);
Bryan Wua403d932012-03-23 15:02:14 -0700158 return;
Stefan Sørensen8d82fef2014-02-04 00:11:42 -0800159 }
Bryan Wua403d932012-03-23 15:02:14 -0700160
161 /* never off - just set to brightness */
162 if (!delay_off) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200163 led_set_brightness_nosleep(led_cdev,
164 led_cdev->blink_brightness);
Bryan Wua403d932012-03-23 15:02:14 -0700165 return;
166 }
167
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100168 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jiri Kosina90673592014-09-02 02:03:12 -0700169 mod_timer(&led_cdev->blink_timer, jiffies + 1);
Bryan Wua403d932012-03-23 15:02:14 -0700170}
171
172
Bryan Wu20c0e6b2012-06-15 22:15:27 +0800173static void led_blink_setup(struct led_classdev *led_cdev,
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800174 unsigned long *delay_on,
175 unsigned long *delay_off)
Bryan Wua403d932012-03-23 15:02:14 -0700176{
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100177 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800178 led_cdev->blink_set &&
Bryan Wua403d932012-03-23 15:02:14 -0700179 !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}
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800188
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200189void led_init_core(struct led_classdev *led_cdev)
190{
191 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
192
Kees Cook49404662017-10-25 03:30:01 -0700193 timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200194}
195EXPORT_SYMBOL_GPL(led_init_core);
196
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800197void led_blink_set(struct led_classdev *led_cdev,
198 unsigned long *delay_on,
199 unsigned long *delay_off)
200{
Jacek Anaszewski7b6af2c2018-01-03 21:13:45 +0100201 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800202
Jacek Anaszewski7b6af2c2018-01-03 21:13:45 +0100203 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100204 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
205 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800206
207 led_blink_setup(led_cdev, delay_on, delay_off);
208}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200209EXPORT_SYMBOL_GPL(led_blink_set);
Bryan Wua403d932012-03-23 15:02:14 -0700210
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800211void led_blink_set_oneshot(struct led_classdev *led_cdev,
212 unsigned long *delay_on,
213 unsigned long *delay_off,
214 int invert)
215{
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100216 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
Jiri Kosina90673592014-09-02 02:03:12 -0700217 timer_pending(&led_cdev->blink_timer))
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800218 return;
219
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100220 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
221 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800222
223 if (invert)
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100224 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800225 else
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100226 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800227
228 led_blink_setup(led_cdev, delay_on, delay_off);
229}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200230EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800231
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800232void led_stop_software_blink(struct led_classdev *led_cdev)
Bryan Wua403d932012-03-23 15:02:14 -0700233{
Jiri Kosina90673592014-09-02 02:03:12 -0700234 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri43786482012-06-07 03:12:34 +0800235 led_cdev->blink_delay_on = 0;
236 led_cdev->blink_delay_off = 0;
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100237 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800238}
239EXPORT_SYMBOL_GPL(led_stop_software_blink);
240
Abanoub Samehaf0bfab2020-12-11 22:42:08 +0200241void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800242{
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200243 /*
Tony Makkiel7cfe7492016-05-18 17:22:45 +0100244 * If software blink is active, delay brightness setting
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200245 * until the next timer tick.
246 */
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100247 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200248 /*
249 * If we need to disable soft blinking delegate this to the
250 * work queue task to avoid problems in case we are called
251 * from hard irq context.
252 */
Abanoub Samehaf0bfab2020-12-11 22:42:08 +0200253 if (!brightness) {
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100254 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
Stas Sergeev76931ed2015-05-14 08:24:02 -0700255 schedule_work(&led_cdev->set_brightness_work);
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200256 } else {
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100257 set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
258 &led_cdev->work_flags);
Hans de Goedeeb1610b2016-10-23 21:47:26 +0200259 led_cdev->new_blink_brightness = brightness;
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200260 }
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800261 return;
262 }
Fabio Baltieri43786482012-06-07 03:12:34 +0800263
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200264 led_set_brightness_nosleep(led_cdev, brightness);
Bryan Wua403d932012-03-23 15:02:14 -0700265}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200266EXPORT_SYMBOL_GPL(led_set_brightness);
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700267
Abanoub Samehaf0bfab2020-12-11 22:42:08 +0200268void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200269{
270 /* Use brightness_set op if available, it is guaranteed not to sleep */
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100271 if (!__led_set_brightness(led_cdev, value))
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200272 return;
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200273
274 /* If brightness setting can sleep, delegate it to a work queue task */
275 led_cdev->delayed_set_value = value;
276 schedule_work(&led_cdev->set_brightness_work);
277}
278EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
279
Abanoub Samehaf0bfab2020-12-11 22:42:08 +0200280void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value)
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200281{
282 led_cdev->brightness = min(value, led_cdev->max_brightness);
283
284 if (led_cdev->flags & LED_SUSPENDED)
285 return;
286
287 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
288}
289EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
290
Abanoub Samehaf0bfab2020-12-11 22:42:08 +0200291int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200292{
293 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
294 return -EBUSY;
295
296 led_cdev->brightness = min(value, led_cdev->max_brightness);
297
298 if (led_cdev->flags & LED_SUSPENDED)
299 return 0;
300
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100301 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200302}
303EXPORT_SYMBOL_GPL(led_set_brightness_sync);
304
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700305int led_update_brightness(struct led_classdev *led_cdev)
306{
307 int ret = 0;
308
309 if (led_cdev->brightness_get) {
310 ret = led_cdev->brightness_get(led_cdev);
311 if (ret >= 0) {
312 led_cdev->brightness = ret;
313 return 0;
314 }
315 }
316
317 return ret;
318}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200319EXPORT_SYMBOL_GPL(led_update_brightness);
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700320
Krzysztof Kozlowski8e1f45612019-01-09 15:44:46 +0100321u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
322{
Andy Shevchenkofd81d7e2019-08-22 18:19:28 +0300323 struct fwnode_handle *fwnode = led_cdev->dev->fwnode;
Krzysztof Kozlowski8e1f45612019-01-09 15:44:46 +0100324 u32 *pattern;
325 int count;
326
Andy Shevchenkofd81d7e2019-08-22 18:19:28 +0300327 count = fwnode_property_count_u32(fwnode, "led-pattern");
Krzysztof Kozlowski8e1f45612019-01-09 15:44:46 +0100328 if (count < 0)
329 return NULL;
330
331 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
332 if (!pattern)
333 return NULL;
334
Andy Shevchenkofd81d7e2019-08-22 18:19:28 +0300335 if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) {
Krzysztof Kozlowski8e1f45612019-01-09 15:44:46 +0100336 kfree(pattern);
337 return NULL;
338 }
339
340 *size = count;
341
342 return pattern;
343}
344EXPORT_SYMBOL_GPL(led_get_default_pattern);
345
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700346/* Caller must ensure led_cdev->led_access held */
347void led_sysfs_disable(struct led_classdev *led_cdev)
348{
349 lockdep_assert_held(&led_cdev->led_access);
350
351 led_cdev->flags |= LED_SYSFS_DISABLE;
352}
353EXPORT_SYMBOL_GPL(led_sysfs_disable);
354
355/* Caller must ensure led_cdev->led_access held */
356void led_sysfs_enable(struct led_classdev *led_cdev)
357{
358 lockdep_assert_held(&led_cdev->led_access);
359
360 led_cdev->flags &= ~LED_SYSFS_DISABLE;
361}
362EXPORT_SYMBOL_GPL(led_sysfs_enable);
Jacek Anaszewskibb4e9af2019-06-09 20:19:04 +0200363
364static void led_parse_fwnode_props(struct device *dev,
365 struct fwnode_handle *fwnode,
366 struct led_properties *props)
367{
368 int ret;
369
370 if (!fwnode)
371 return;
372
373 if (fwnode_property_present(fwnode, "label")) {
374 ret = fwnode_property_read_string(fwnode, "label", &props->label);
375 if (ret)
376 dev_err(dev, "Error parsing 'label' property (%d)\n", ret);
377 return;
378 }
379
380 if (fwnode_property_present(fwnode, "color")) {
381 ret = fwnode_property_read_u32(fwnode, "color", &props->color);
382 if (ret)
383 dev_err(dev, "Error parsing 'color' property (%d)\n", ret);
384 else if (props->color >= LED_COLOR_ID_MAX)
385 dev_err(dev, "LED color identifier out of range\n");
386 else
387 props->color_present = true;
388 }
389
390
391 if (!fwnode_property_present(fwnode, "function"))
392 return;
393
394 ret = fwnode_property_read_string(fwnode, "function", &props->function);
395 if (ret) {
396 dev_err(dev,
397 "Error parsing 'function' property (%d)\n",
398 ret);
399 }
400
401 if (!fwnode_property_present(fwnode, "function-enumerator"))
402 return;
403
404 ret = fwnode_property_read_u32(fwnode, "function-enumerator",
405 &props->func_enum);
406 if (ret) {
407 dev_err(dev,
408 "Error parsing 'function-enumerator' property (%d)\n",
409 ret);
410 } else {
411 props->func_enum_present = true;
412 }
413}
414
415int led_compose_name(struct device *dev, struct led_init_data *init_data,
416 char *led_classdev_name)
417{
418 struct led_properties props = {};
419 struct fwnode_handle *fwnode = init_data->fwnode;
420 const char *devicename = init_data->devicename;
421
Pavel Machek77dce3a2020-08-03 14:00:06 +0200422 /* We want to label LEDs that can produce full range of colors
423 * as RGB, not multicolor */
424 BUG_ON(props.color == LED_COLOR_ID_MULTI);
425
Jacek Anaszewskibb4e9af2019-06-09 20:19:04 +0200426 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}
479EXPORT_SYMBOL_GPL(led_compose_name);
Denis Osterland-Heim791bc412021-06-08 08:35:53 +0200480
481enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode)
482{
483 const char *state = NULL;
484
485 if (!fwnode_property_read_string(fwnode, "default-state", &state)) {
486 if (!strcmp(state, "keep"))
487 return LEDS_DEFSTATE_KEEP;
488 if (!strcmp(state, "on"))
489 return LEDS_DEFSTATE_ON;
490 }
491
492 return LEDS_DEFSTATE_OFF;
493}
494EXPORT_SYMBOL_GPL(led_init_default_state_get);