blob: 7107cd7e87cfde458fb0f24d0183c6de1566fa82 [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>
Richard Purdie72f8da32007-12-31 23:09:44 +000016#include <linux/rwsem.h>
Krzysztof Kozlowski8e1f45612019-01-09 15:44:46 +010017#include <linux/slab.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080018#include "leds.h"
19
Richard Purdie72f8da32007-12-31 23:09:44 +000020DECLARE_RWSEM(leds_list_lock);
Richard Purdiec72a1d62006-03-31 02:31:04 -080021EXPORT_SYMBOL_GPL(leds_list_lock);
Németh Márton4d404fd2008-03-09 20:59:57 +000022
23LIST_HEAD(leds_list);
24EXPORT_SYMBOL_GPL(leds_list);
Bryan Wua403d932012-03-23 15:02:14 -070025
Heiner Kallweitd4887af2016-02-16 20:21:56 +010026static int __led_set_brightness(struct led_classdev *led_cdev,
27 enum led_brightness value)
28{
29 if (!led_cdev->brightness_set)
30 return -ENOTSUPP;
31
32 led_cdev->brightness_set(led_cdev, value);
33
34 return 0;
35}
36
37static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
38 enum led_brightness value)
39{
40 if (!led_cdev->brightness_set_blocking)
41 return -ENOTSUPP;
42
43 return led_cdev->brightness_set_blocking(led_cdev, value);
44}
45
Kees Cook49404662017-10-25 03:30:01 -070046static void led_timer_function(struct timer_list *t)
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020047{
Kees Cook49404662017-10-25 03:30:01 -070048 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020049 unsigned long brightness;
50 unsigned long delay;
51
52 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020053 led_set_brightness_nosleep(led_cdev, LED_OFF);
Hans de Goedea9c6ce52016-11-08 14:38:46 +010054 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020055 return;
56 }
57
Hans de Goedea9c6ce52016-11-08 14:38:46 +010058 if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
59 &led_cdev->work_flags)) {
60 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020061 return;
62 }
63
64 brightness = led_get_brightness(led_cdev);
65 if (!brightness) {
66 /* Time to switch the LED on. */
Hans de Goedeeb1610b2016-10-23 21:47:26 +020067 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE,
68 &led_cdev->work_flags))
69 brightness = led_cdev->new_blink_brightness;
70 else
71 brightness = led_cdev->blink_brightness;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020072 delay = led_cdev->blink_delay_on;
73 } else {
74 /* Store the current brightness value to be able
75 * to restore it when the delay_off period is over.
76 */
Hans de Goedeeb1610b2016-10-23 21:47:26 +020077 led_cdev->blink_brightness = brightness;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020078 brightness = LED_OFF;
79 delay = led_cdev->blink_delay_off;
80 }
81
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020082 led_set_brightness_nosleep(led_cdev, brightness);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020083
84 /* Return in next iteration if led is in one-shot mode and we are in
85 * the final blink state so that the led is toggled each delay_on +
86 * delay_off milliseconds in worst case.
87 */
Hans de Goedea9c6ce52016-11-08 14:38:46 +010088 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
89 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020090 if (brightness)
Hans de Goedea9c6ce52016-11-08 14:38:46 +010091 set_bit(LED_BLINK_ONESHOT_STOP,
92 &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020093 } else {
94 if (!brightness)
Hans de Goedea9c6ce52016-11-08 14:38:46 +010095 set_bit(LED_BLINK_ONESHOT_STOP,
96 &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020097 }
98 }
99
100 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
101}
102
103static void set_brightness_delayed(struct work_struct *ws)
104{
105 struct led_classdev *led_cdev =
106 container_of(ws, struct led_classdev, set_brightness_work);
Jacek Anaszewski1afcadf2015-10-19 09:04:01 +0200107 int ret = 0;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200108
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100109 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200110 led_cdev->delayed_set_value = LED_OFF;
111 led_stop_software_blink(led_cdev);
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200112 }
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200113
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100114 ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
115 if (ret == -ENOTSUPP)
116 ret = __led_set_brightness_blocking(led_cdev,
117 led_cdev->delayed_set_value);
Heiner Kallweitd84d80f2016-01-22 21:43:48 +0100118 if (ret < 0 &&
119 /* LED HW might have been unplugged, therefore don't warn */
120 !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
121 (led_cdev->flags & LED_HW_PLUGGABLE)))
Jacek Anaszewski1afcadf2015-10-19 09:04:01 +0200122 dev_err(led_cdev->dev,
123 "Setting an LED's brightness failed (%d)\n", ret);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200124}
125
Bryan Wua403d932012-03-23 15:02:14 -0700126static void led_set_software_blink(struct led_classdev *led_cdev,
127 unsigned long delay_on,
128 unsigned long delay_off)
129{
130 int current_brightness;
131
132 current_brightness = led_get_brightness(led_cdev);
133 if (current_brightness)
134 led_cdev->blink_brightness = current_brightness;
135 if (!led_cdev->blink_brightness)
136 led_cdev->blink_brightness = led_cdev->max_brightness;
137
Bryan Wua403d932012-03-23 15:02:14 -0700138 led_cdev->blink_delay_on = delay_on;
139 led_cdev->blink_delay_off = delay_off;
140
Stefan Sørensen8d82fef2014-02-04 00:11:42 -0800141 /* never on - just set to off */
142 if (!delay_on) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200143 led_set_brightness_nosleep(led_cdev, LED_OFF);
Bryan Wua403d932012-03-23 15:02:14 -0700144 return;
Stefan Sørensen8d82fef2014-02-04 00:11:42 -0800145 }
Bryan Wua403d932012-03-23 15:02:14 -0700146
147 /* never off - just set to brightness */
148 if (!delay_off) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200149 led_set_brightness_nosleep(led_cdev,
150 led_cdev->blink_brightness);
Bryan Wua403d932012-03-23 15:02:14 -0700151 return;
152 }
153
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100154 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jiri Kosina90673592014-09-02 02:03:12 -0700155 mod_timer(&led_cdev->blink_timer, jiffies + 1);
Bryan Wua403d932012-03-23 15:02:14 -0700156}
157
158
Bryan Wu20c0e6b2012-06-15 22:15:27 +0800159static void led_blink_setup(struct led_classdev *led_cdev,
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800160 unsigned long *delay_on,
161 unsigned long *delay_off)
Bryan Wua403d932012-03-23 15:02:14 -0700162{
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100163 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800164 led_cdev->blink_set &&
Bryan Wua403d932012-03-23 15:02:14 -0700165 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
166 return;
167
168 /* blink with 1 Hz as default if nothing specified */
169 if (!*delay_on && !*delay_off)
170 *delay_on = *delay_off = 500;
171
172 led_set_software_blink(led_cdev, *delay_on, *delay_off);
173}
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800174
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200175void led_init_core(struct led_classdev *led_cdev)
176{
177 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
178
Kees Cook49404662017-10-25 03:30:01 -0700179 timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200180}
181EXPORT_SYMBOL_GPL(led_init_core);
182
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800183void led_blink_set(struct led_classdev *led_cdev,
184 unsigned long *delay_on,
185 unsigned long *delay_off)
186{
Jacek Anaszewski7b6af2c2018-01-03 21:13:45 +0100187 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800188
Jacek Anaszewski7b6af2c2018-01-03 21:13:45 +0100189 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100190 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
191 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800192
193 led_blink_setup(led_cdev, delay_on, delay_off);
194}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200195EXPORT_SYMBOL_GPL(led_blink_set);
Bryan Wua403d932012-03-23 15:02:14 -0700196
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800197void led_blink_set_oneshot(struct led_classdev *led_cdev,
198 unsigned long *delay_on,
199 unsigned long *delay_off,
200 int invert)
201{
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100202 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
Jiri Kosina90673592014-09-02 02:03:12 -0700203 timer_pending(&led_cdev->blink_timer))
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800204 return;
205
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100206 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
207 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800208
209 if (invert)
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100210 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800211 else
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100212 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800213
214 led_blink_setup(led_cdev, delay_on, delay_off);
215}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200216EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800217
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800218void led_stop_software_blink(struct led_classdev *led_cdev)
Bryan Wua403d932012-03-23 15:02:14 -0700219{
Jiri Kosina90673592014-09-02 02:03:12 -0700220 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri43786482012-06-07 03:12:34 +0800221 led_cdev->blink_delay_on = 0;
222 led_cdev->blink_delay_off = 0;
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100223 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800224}
225EXPORT_SYMBOL_GPL(led_stop_software_blink);
226
227void led_set_brightness(struct led_classdev *led_cdev,
228 enum led_brightness brightness)
229{
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200230 /*
Tony Makkiel7cfe7492016-05-18 17:22:45 +0100231 * If software blink is active, delay brightness setting
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200232 * until the next timer tick.
233 */
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100234 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200235 /*
236 * If we need to disable soft blinking delegate this to the
237 * work queue task to avoid problems in case we are called
238 * from hard irq context.
239 */
240 if (brightness == LED_OFF) {
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100241 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
Stas Sergeev76931ed2015-05-14 08:24:02 -0700242 schedule_work(&led_cdev->set_brightness_work);
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200243 } else {
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100244 set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
245 &led_cdev->work_flags);
Hans de Goedeeb1610b2016-10-23 21:47:26 +0200246 led_cdev->new_blink_brightness = brightness;
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200247 }
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800248 return;
249 }
Fabio Baltieri43786482012-06-07 03:12:34 +0800250
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200251 led_set_brightness_nosleep(led_cdev, brightness);
Bryan Wua403d932012-03-23 15:02:14 -0700252}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200253EXPORT_SYMBOL_GPL(led_set_brightness);
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700254
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200255void led_set_brightness_nopm(struct led_classdev *led_cdev,
256 enum led_brightness value)
257{
258 /* Use brightness_set op if available, it is guaranteed not to sleep */
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100259 if (!__led_set_brightness(led_cdev, value))
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200260 return;
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200261
262 /* If brightness setting can sleep, delegate it to a work queue task */
263 led_cdev->delayed_set_value = value;
264 schedule_work(&led_cdev->set_brightness_work);
265}
266EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
267
268void led_set_brightness_nosleep(struct led_classdev *led_cdev,
269 enum led_brightness value)
270{
271 led_cdev->brightness = min(value, led_cdev->max_brightness);
272
273 if (led_cdev->flags & LED_SUSPENDED)
274 return;
275
276 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
277}
278EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
279
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200280int led_set_brightness_sync(struct led_classdev *led_cdev,
281 enum led_brightness value)
282{
283 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
284 return -EBUSY;
285
286 led_cdev->brightness = min(value, led_cdev->max_brightness);
287
288 if (led_cdev->flags & LED_SUSPENDED)
289 return 0;
290
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100291 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200292}
293EXPORT_SYMBOL_GPL(led_set_brightness_sync);
294
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700295int led_update_brightness(struct led_classdev *led_cdev)
296{
297 int ret = 0;
298
299 if (led_cdev->brightness_get) {
300 ret = led_cdev->brightness_get(led_cdev);
301 if (ret >= 0) {
302 led_cdev->brightness = ret;
303 return 0;
304 }
305 }
306
307 return ret;
308}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200309EXPORT_SYMBOL_GPL(led_update_brightness);
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700310
Krzysztof Kozlowski8e1f45612019-01-09 15:44:46 +0100311u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
312{
313 struct device_node *np = dev_of_node(led_cdev->dev);
314 u32 *pattern;
315 int count;
316
317 if (!np)
318 return NULL;
319
320 count = of_property_count_u32_elems(np, "led-pattern");
321 if (count < 0)
322 return NULL;
323
324 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
325 if (!pattern)
326 return NULL;
327
328 if (of_property_read_u32_array(np, "led-pattern", pattern, count)) {
329 kfree(pattern);
330 return NULL;
331 }
332
333 *size = count;
334
335 return pattern;
336}
337EXPORT_SYMBOL_GPL(led_get_default_pattern);
338
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700339/* Caller must ensure led_cdev->led_access held */
340void led_sysfs_disable(struct led_classdev *led_cdev)
341{
342 lockdep_assert_held(&led_cdev->led_access);
343
344 led_cdev->flags |= LED_SYSFS_DISABLE;
345}
346EXPORT_SYMBOL_GPL(led_sysfs_disable);
347
348/* Caller must ensure led_cdev->led_access held */
349void led_sysfs_enable(struct led_classdev *led_cdev)
350{
351 lockdep_assert_held(&led_cdev->led_access);
352
353 led_cdev->flags &= ~LED_SYSFS_DISABLE;
354}
355EXPORT_SYMBOL_GPL(led_sysfs_enable);