blob: e9ae7f87ab900b37b9109e5dffd69db60a378b68 [file] [log] [blame]
Richard Purdiec72a1d62006-03-31 02:31:04 -08001/*
2 * LED Class Core
3 *
4 * Copyright 2005-2006 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <rpurdie@openedhand.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/kernel.h>
Jacek Anaszewski04713302014-08-07 05:10:22 -070015#include <linux/leds.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080016#include <linux/list.h>
17#include <linux/module.h>
Jacek Anaszewski04713302014-08-07 05:10:22 -070018#include <linux/mutex.h>
Krzysztof Kozlowski8e1f4562019-01-09 15:44:46 +010019#include <linux/of.h>
Richard Purdie72f8da32007-12-31 23:09:44 +000020#include <linux/rwsem.h>
Krzysztof Kozlowski8e1f4562019-01-09 15:44:46 +010021#include <linux/slab.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080022#include "leds.h"
23
Richard Purdie72f8da32007-12-31 23:09:44 +000024DECLARE_RWSEM(leds_list_lock);
Richard Purdiec72a1d62006-03-31 02:31:04 -080025EXPORT_SYMBOL_GPL(leds_list_lock);
Németh Márton4d404fd2008-03-09 20:59:57 +000026
27LIST_HEAD(leds_list);
28EXPORT_SYMBOL_GPL(leds_list);
Bryan Wua403d932012-03-23 15:02:14 -070029
Heiner Kallweitd4887af2016-02-16 20:21:56 +010030static int __led_set_brightness(struct led_classdev *led_cdev,
31 enum led_brightness value)
32{
33 if (!led_cdev->brightness_set)
34 return -ENOTSUPP;
35
36 led_cdev->brightness_set(led_cdev, value);
37
38 return 0;
39}
40
41static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
42 enum led_brightness value)
43{
44 if (!led_cdev->brightness_set_blocking)
45 return -ENOTSUPP;
46
47 return led_cdev->brightness_set_blocking(led_cdev, value);
48}
49
Kees Cook49404662017-10-25 03:30:01 -070050static void led_timer_function(struct timer_list *t)
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020051{
Kees Cook49404662017-10-25 03:30:01 -070052 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020053 unsigned long brightness;
54 unsigned long delay;
55
56 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020057 led_set_brightness_nosleep(led_cdev, LED_OFF);
Hans de Goedea9c6ce52016-11-08 14:38:46 +010058 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020059 return;
60 }
61
Hans de Goedea9c6ce52016-11-08 14:38:46 +010062 if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
63 &led_cdev->work_flags)) {
64 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020065 return;
66 }
67
68 brightness = led_get_brightness(led_cdev);
69 if (!brightness) {
70 /* Time to switch the LED on. */
Hans de Goedeeb1610b2016-10-23 21:47:26 +020071 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE,
72 &led_cdev->work_flags))
73 brightness = led_cdev->new_blink_brightness;
74 else
75 brightness = led_cdev->blink_brightness;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020076 delay = led_cdev->blink_delay_on;
77 } else {
78 /* Store the current brightness value to be able
79 * to restore it when the delay_off period is over.
80 */
Hans de Goedeeb1610b2016-10-23 21:47:26 +020081 led_cdev->blink_brightness = brightness;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020082 brightness = LED_OFF;
83 delay = led_cdev->blink_delay_off;
84 }
85
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020086 led_set_brightness_nosleep(led_cdev, brightness);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020087
88 /* Return in next iteration if led is in one-shot mode and we are in
89 * the final blink state so that the led is toggled each delay_on +
90 * delay_off milliseconds in worst case.
91 */
Hans de Goedea9c6ce52016-11-08 14:38:46 +010092 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
93 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020094 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 } else {
98 if (!brightness)
Hans de Goedea9c6ce52016-11-08 14:38:46 +010099 set_bit(LED_BLINK_ONESHOT_STOP,
100 &led_cdev->work_flags);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200101 }
102 }
103
104 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
105}
106
107static void set_brightness_delayed(struct work_struct *ws)
108{
109 struct led_classdev *led_cdev =
110 container_of(ws, struct led_classdev, set_brightness_work);
Jacek Anaszewski1afcadf2015-10-19 09:04:01 +0200111 int ret = 0;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200112
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100113 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200114 led_cdev->delayed_set_value = LED_OFF;
115 led_stop_software_blink(led_cdev);
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200116 }
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200117
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100118 ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
119 if (ret == -ENOTSUPP)
120 ret = __led_set_brightness_blocking(led_cdev,
121 led_cdev->delayed_set_value);
Heiner Kallweitd84d80f2016-01-22 21:43:48 +0100122 if (ret < 0 &&
123 /* LED HW might have been unplugged, therefore don't warn */
124 !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
125 (led_cdev->flags & LED_HW_PLUGGABLE)))
Jacek Anaszewski1afcadf2015-10-19 09:04:01 +0200126 dev_err(led_cdev->dev,
127 "Setting an LED's brightness failed (%d)\n", ret);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200128}
129
Bryan Wua403d932012-03-23 15:02:14 -0700130static void led_set_software_blink(struct led_classdev *led_cdev,
131 unsigned long delay_on,
132 unsigned long delay_off)
133{
134 int current_brightness;
135
136 current_brightness = led_get_brightness(led_cdev);
137 if (current_brightness)
138 led_cdev->blink_brightness = current_brightness;
139 if (!led_cdev->blink_brightness)
140 led_cdev->blink_brightness = led_cdev->max_brightness;
141
Bryan Wua403d932012-03-23 15:02:14 -0700142 led_cdev->blink_delay_on = delay_on;
143 led_cdev->blink_delay_off = delay_off;
144
Stefan Sørensen8d82fef2014-02-04 00:11:42 -0800145 /* never on - just set to off */
146 if (!delay_on) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200147 led_set_brightness_nosleep(led_cdev, LED_OFF);
Bryan Wua403d932012-03-23 15:02:14 -0700148 return;
Stefan Sørensen8d82fef2014-02-04 00:11:42 -0800149 }
Bryan Wua403d932012-03-23 15:02:14 -0700150
151 /* never off - just set to brightness */
152 if (!delay_off) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200153 led_set_brightness_nosleep(led_cdev,
154 led_cdev->blink_brightness);
Bryan Wua403d932012-03-23 15:02:14 -0700155 return;
156 }
157
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100158 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
Jiri Kosina90673592014-09-02 02:03:12 -0700159 mod_timer(&led_cdev->blink_timer, jiffies + 1);
Bryan Wua403d932012-03-23 15:02:14 -0700160}
161
162
Bryan Wu20c0e6b2012-06-15 22:15:27 +0800163static void led_blink_setup(struct led_classdev *led_cdev,
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800164 unsigned long *delay_on,
165 unsigned long *delay_off)
Bryan Wua403d932012-03-23 15:02:14 -0700166{
Pavel Machek0db37912019-04-29 17:22:59 +0200167 /*
168 * If "set brightness to 0" is pending in workqueue, we don't
169 * want that to be reordered after blink_set()
170 */
171 flush_work(&led_cdev->set_brightness_work);
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100172 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800173 led_cdev->blink_set &&
Bryan Wua403d932012-03-23 15:02:14 -0700174 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
175 return;
176
177 /* blink with 1 Hz as default if nothing specified */
178 if (!*delay_on && !*delay_off)
179 *delay_on = *delay_off = 500;
180
181 led_set_software_blink(led_cdev, *delay_on, *delay_off);
182}
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800183
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200184void led_init_core(struct led_classdev *led_cdev)
185{
186 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
187
Kees Cook49404662017-10-25 03:30:01 -0700188 timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200189}
190EXPORT_SYMBOL_GPL(led_init_core);
191
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800192void led_blink_set(struct led_classdev *led_cdev,
193 unsigned long *delay_on,
194 unsigned long *delay_off)
195{
Jacek Anaszewski7b6af2c2018-01-03 21:13:45 +0100196 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800197
Jacek Anaszewski7b6af2c2018-01-03 21:13:45 +0100198 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100199 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
200 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800201
202 led_blink_setup(led_cdev, delay_on, delay_off);
203}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200204EXPORT_SYMBOL_GPL(led_blink_set);
Bryan Wua403d932012-03-23 15:02:14 -0700205
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800206void led_blink_set_oneshot(struct led_classdev *led_cdev,
207 unsigned long *delay_on,
208 unsigned long *delay_off,
209 int invert)
210{
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100211 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
Jiri Kosina90673592014-09-02 02:03:12 -0700212 timer_pending(&led_cdev->blink_timer))
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800213 return;
214
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100215 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
216 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800217
218 if (invert)
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100219 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800220 else
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100221 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800222
223 led_blink_setup(led_cdev, delay_on, delay_off);
224}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200225EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800226
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800227void led_stop_software_blink(struct led_classdev *led_cdev)
Bryan Wua403d932012-03-23 15:02:14 -0700228{
Jiri Kosina90673592014-09-02 02:03:12 -0700229 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri43786482012-06-07 03:12:34 +0800230 led_cdev->blink_delay_on = 0;
231 led_cdev->blink_delay_off = 0;
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100232 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800233}
234EXPORT_SYMBOL_GPL(led_stop_software_blink);
235
236void led_set_brightness(struct led_classdev *led_cdev,
237 enum led_brightness brightness)
238{
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200239 /*
Tony Makkiel7cfe7492016-05-18 17:22:45 +0100240 * If software blink is active, delay brightness setting
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200241 * until the next timer tick.
242 */
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100243 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200244 /*
245 * If we need to disable soft blinking delegate this to the
246 * work queue task to avoid problems in case we are called
247 * from hard irq context.
248 */
249 if (brightness == LED_OFF) {
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100250 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
Stas Sergeev76931ed2015-05-14 08:24:02 -0700251 schedule_work(&led_cdev->set_brightness_work);
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200252 } else {
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100253 set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
254 &led_cdev->work_flags);
Hans de Goedeeb1610b2016-10-23 21:47:26 +0200255 led_cdev->new_blink_brightness = brightness;
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200256 }
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800257 return;
258 }
Fabio Baltieri43786482012-06-07 03:12:34 +0800259
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200260 led_set_brightness_nosleep(led_cdev, brightness);
Bryan Wua403d932012-03-23 15:02:14 -0700261}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200262EXPORT_SYMBOL_GPL(led_set_brightness);
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700263
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200264void led_set_brightness_nopm(struct led_classdev *led_cdev,
265 enum led_brightness value)
266{
267 /* Use brightness_set op if available, it is guaranteed not to sleep */
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100268 if (!__led_set_brightness(led_cdev, value))
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200269 return;
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200270
271 /* If brightness setting can sleep, delegate it to a work queue task */
272 led_cdev->delayed_set_value = value;
273 schedule_work(&led_cdev->set_brightness_work);
274}
275EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
276
277void led_set_brightness_nosleep(struct led_classdev *led_cdev,
278 enum led_brightness value)
279{
280 led_cdev->brightness = min(value, led_cdev->max_brightness);
281
282 if (led_cdev->flags & LED_SUSPENDED)
283 return;
284
285 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
286}
287EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
288
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200289int led_set_brightness_sync(struct led_classdev *led_cdev,
290 enum led_brightness value)
291{
292 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
293 return -EBUSY;
294
295 led_cdev->brightness = min(value, led_cdev->max_brightness);
296
297 if (led_cdev->flags & LED_SUSPENDED)
298 return 0;
299
Heiner Kallweitd4887af2016-02-16 20:21:56 +0100300 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200301}
302EXPORT_SYMBOL_GPL(led_set_brightness_sync);
303
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700304int led_update_brightness(struct led_classdev *led_cdev)
305{
306 int ret = 0;
307
308 if (led_cdev->brightness_get) {
309 ret = led_cdev->brightness_get(led_cdev);
310 if (ret >= 0) {
311 led_cdev->brightness = ret;
312 return 0;
313 }
314 }
315
316 return ret;
317}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200318EXPORT_SYMBOL_GPL(led_update_brightness);
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700319
Krzysztof Kozlowski8e1f4562019-01-09 15:44:46 +0100320u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
321{
322 struct device_node *np = dev_of_node(led_cdev->dev);
323 u32 *pattern;
324 int count;
325
326 if (!np)
327 return NULL;
328
329 count = of_property_count_u32_elems(np, "led-pattern");
330 if (count < 0)
331 return NULL;
332
333 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
334 if (!pattern)
335 return NULL;
336
337 if (of_property_read_u32_array(np, "led-pattern", pattern, count)) {
338 kfree(pattern);
339 return NULL;
340 }
341
342 *size = count;
343
344 return pattern;
345}
346EXPORT_SYMBOL_GPL(led_get_default_pattern);
347
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700348/* Caller must ensure led_cdev->led_access held */
349void led_sysfs_disable(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_disable);
356
357/* Caller must ensure led_cdev->led_access held */
358void led_sysfs_enable(struct led_classdev *led_cdev)
359{
360 lockdep_assert_held(&led_cdev->led_access);
361
362 led_cdev->flags &= ~LED_SYSFS_DISABLE;
363}
364EXPORT_SYMBOL_GPL(led_sysfs_enable);