blob: 19e1e60dfaa354585ca01e2ffdc2a47f29c49c3b [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>
Richard Purdie72f8da32007-12-31 23:09:44 +000019#include <linux/rwsem.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 Anaszewski757b06a2015-09-28 14:38:04 +020028static void led_timer_function(unsigned long data)
29{
30 struct led_classdev *led_cdev = (void *)data;
31 unsigned long brightness;
32 unsigned long delay;
33
34 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020035 led_set_brightness_nosleep(led_cdev, LED_OFF);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020036 return;
37 }
38
39 if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
40 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
41 return;
42 }
43
44 brightness = led_get_brightness(led_cdev);
45 if (!brightness) {
46 /* Time to switch the LED on. */
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020047 brightness = led_cdev->blink_brightness;
48 delay = led_cdev->blink_delay_on;
49 } else {
50 /* Store the current brightness value to be able
51 * to restore it when the delay_off period is over.
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +020052 * Do it only if there is no pending blink brightness
53 * change, to avoid overwriting the new value.
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020054 */
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +020055 if (!(led_cdev->flags & LED_BLINK_BRIGHTNESS_CHANGE))
56 led_cdev->blink_brightness = brightness;
57 else
58 led_cdev->flags &= ~LED_BLINK_BRIGHTNESS_CHANGE;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020059 brightness = LED_OFF;
60 delay = led_cdev->blink_delay_off;
61 }
62
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020063 led_set_brightness_nosleep(led_cdev, brightness);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020064
65 /* Return in next iteration if led is in one-shot mode and we are in
66 * the final blink state so that the led is toggled each delay_on +
67 * delay_off milliseconds in worst case.
68 */
69 if (led_cdev->flags & LED_BLINK_ONESHOT) {
70 if (led_cdev->flags & LED_BLINK_INVERT) {
71 if (brightness)
72 led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
73 } else {
74 if (!brightness)
75 led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
76 }
77 }
78
79 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
80}
81
82static void set_brightness_delayed(struct work_struct *ws)
83{
84 struct led_classdev *led_cdev =
85 container_of(ws, struct led_classdev, set_brightness_work);
Jacek Anaszewski1afcadf2015-10-19 09:04:01 +020086 int ret = 0;
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020087
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +020088 if (led_cdev->flags & LED_BLINK_DISABLE) {
89 led_cdev->delayed_set_value = LED_OFF;
90 led_stop_software_blink(led_cdev);
91 led_cdev->flags &= ~LED_BLINK_DISABLE;
92 }
Jacek Anaszewski757b06a2015-09-28 14:38:04 +020093
Jacek Anaszewski1afcadf2015-10-19 09:04:01 +020094 if (led_cdev->brightness_set)
95 led_cdev->brightness_set(led_cdev, led_cdev->delayed_set_value);
96 else if (led_cdev->brightness_set_blocking)
97 ret = led_cdev->brightness_set_blocking(led_cdev,
98 led_cdev->delayed_set_value);
99 else
100 ret = -ENOTSUPP;
101 if (ret < 0)
102 dev_err(led_cdev->dev,
103 "Setting an LED's brightness failed (%d)\n", ret);
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200104}
105
Bryan Wua403d932012-03-23 15:02:14 -0700106static void led_set_software_blink(struct led_classdev *led_cdev,
107 unsigned long delay_on,
108 unsigned long delay_off)
109{
110 int current_brightness;
111
112 current_brightness = led_get_brightness(led_cdev);
113 if (current_brightness)
114 led_cdev->blink_brightness = current_brightness;
115 if (!led_cdev->blink_brightness)
116 led_cdev->blink_brightness = led_cdev->max_brightness;
117
Bryan Wua403d932012-03-23 15:02:14 -0700118 led_cdev->blink_delay_on = delay_on;
119 led_cdev->blink_delay_off = delay_off;
120
Stefan Sørensen8d82fef2014-02-04 00:11:42 -0800121 /* never on - just set to off */
122 if (!delay_on) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200123 led_set_brightness_nosleep(led_cdev, LED_OFF);
Bryan Wua403d932012-03-23 15:02:14 -0700124 return;
Stefan Sørensen8d82fef2014-02-04 00:11:42 -0800125 }
Bryan Wua403d932012-03-23 15:02:14 -0700126
127 /* never off - just set to brightness */
128 if (!delay_off) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200129 led_set_brightness_nosleep(led_cdev,
130 led_cdev->blink_brightness);
Bryan Wua403d932012-03-23 15:02:14 -0700131 return;
132 }
133
Jiri Kosina90673592014-09-02 02:03:12 -0700134 mod_timer(&led_cdev->blink_timer, jiffies + 1);
Bryan Wua403d932012-03-23 15:02:14 -0700135}
136
137
Bryan Wu20c0e6b2012-06-15 22:15:27 +0800138static void led_blink_setup(struct led_classdev *led_cdev,
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800139 unsigned long *delay_on,
140 unsigned long *delay_off)
Bryan Wua403d932012-03-23 15:02:14 -0700141{
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800142 if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
143 led_cdev->blink_set &&
Bryan Wua403d932012-03-23 15:02:14 -0700144 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
145 return;
146
147 /* blink with 1 Hz as default if nothing specified */
148 if (!*delay_on && !*delay_off)
149 *delay_on = *delay_off = 500;
150
151 led_set_software_blink(led_cdev, *delay_on, *delay_off);
152}
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800153
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200154void led_init_core(struct led_classdev *led_cdev)
155{
156 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
157
158 setup_timer(&led_cdev->blink_timer, led_timer_function,
159 (unsigned long)led_cdev);
160}
161EXPORT_SYMBOL_GPL(led_init_core);
162
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800163void led_blink_set(struct led_classdev *led_cdev,
164 unsigned long *delay_on,
165 unsigned long *delay_off)
166{
Jiri Kosina90673592014-09-02 02:03:12 -0700167 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800168
169 led_cdev->flags &= ~LED_BLINK_ONESHOT;
170 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
171
172 led_blink_setup(led_cdev, delay_on, delay_off);
173}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200174EXPORT_SYMBOL_GPL(led_blink_set);
Bryan Wua403d932012-03-23 15:02:14 -0700175
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800176void led_blink_set_oneshot(struct led_classdev *led_cdev,
177 unsigned long *delay_on,
178 unsigned long *delay_off,
179 int invert)
180{
181 if ((led_cdev->flags & LED_BLINK_ONESHOT) &&
Jiri Kosina90673592014-09-02 02:03:12 -0700182 timer_pending(&led_cdev->blink_timer))
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800183 return;
184
185 led_cdev->flags |= LED_BLINK_ONESHOT;
186 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
187
188 if (invert)
189 led_cdev->flags |= LED_BLINK_INVERT;
190 else
191 led_cdev->flags &= ~LED_BLINK_INVERT;
192
193 led_blink_setup(led_cdev, delay_on, delay_off);
194}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200195EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800196
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800197void led_stop_software_blink(struct led_classdev *led_cdev)
Bryan Wua403d932012-03-23 15:02:14 -0700198{
Jiri Kosina90673592014-09-02 02:03:12 -0700199 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri43786482012-06-07 03:12:34 +0800200 led_cdev->blink_delay_on = 0;
201 led_cdev->blink_delay_off = 0;
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800202}
203EXPORT_SYMBOL_GPL(led_stop_software_blink);
204
205void led_set_brightness(struct led_classdev *led_cdev,
206 enum led_brightness brightness)
207{
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200208 /*
209 * In case blinking is on delay brightness setting
210 * until the next timer tick.
211 */
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800212 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) {
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200213 /*
214 * If we need to disable soft blinking delegate this to the
215 * work queue task to avoid problems in case we are called
216 * from hard irq context.
217 */
218 if (brightness == LED_OFF) {
219 led_cdev->flags |= LED_BLINK_DISABLE;
Stas Sergeev76931ed2015-05-14 08:24:02 -0700220 schedule_work(&led_cdev->set_brightness_work);
Jacek Anaszewskif1e80c02015-10-07 11:10:39 +0200221 } else {
222 led_cdev->flags |= LED_BLINK_BRIGHTNESS_CHANGE;
223 led_cdev->blink_brightness = brightness;
224 }
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800225 return;
226 }
Fabio Baltieri43786482012-06-07 03:12:34 +0800227
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200228 led_set_brightness_nosleep(led_cdev, brightness);
Bryan Wua403d932012-03-23 15:02:14 -0700229}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200230EXPORT_SYMBOL_GPL(led_set_brightness);
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700231
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200232void led_set_brightness_nopm(struct led_classdev *led_cdev,
233 enum led_brightness value)
234{
235 /* Use brightness_set op if available, it is guaranteed not to sleep */
236 if (led_cdev->brightness_set) {
237 led_cdev->brightness_set(led_cdev, value);
238 return;
239 }
240
241 /* If brightness setting can sleep, delegate it to a work queue task */
242 led_cdev->delayed_set_value = value;
243 schedule_work(&led_cdev->set_brightness_work);
244}
245EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
246
247void led_set_brightness_nosleep(struct led_classdev *led_cdev,
248 enum led_brightness value)
249{
250 led_cdev->brightness = min(value, led_cdev->max_brightness);
251
252 if (led_cdev->flags & LED_SUSPENDED)
253 return;
254
255 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
256}
257EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
258
Jacek Anaszewski13ae79b2015-10-07 11:10:43 +0200259int led_set_brightness_sync(struct led_classdev *led_cdev,
260 enum led_brightness value)
261{
262 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
263 return -EBUSY;
264
265 led_cdev->brightness = min(value, led_cdev->max_brightness);
266
267 if (led_cdev->flags & LED_SUSPENDED)
268 return 0;
269
270 if (led_cdev->brightness_set_blocking)
271 return led_cdev->brightness_set_blocking(led_cdev,
272 led_cdev->brightness);
273 return -ENOTSUPP;
274}
275EXPORT_SYMBOL_GPL(led_set_brightness_sync);
276
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700277int led_update_brightness(struct led_classdev *led_cdev)
278{
279 int ret = 0;
280
281 if (led_cdev->brightness_get) {
282 ret = led_cdev->brightness_get(led_cdev);
283 if (ret >= 0) {
284 led_cdev->brightness = ret;
285 return 0;
286 }
287 }
288
289 return ret;
290}
Jacek Anaszewski2806e2f2015-09-28 15:07:10 +0200291EXPORT_SYMBOL_GPL(led_update_brightness);
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700292
293/* Caller must ensure led_cdev->led_access held */
294void led_sysfs_disable(struct led_classdev *led_cdev)
295{
296 lockdep_assert_held(&led_cdev->led_access);
297
298 led_cdev->flags |= LED_SYSFS_DISABLE;
299}
300EXPORT_SYMBOL_GPL(led_sysfs_disable);
301
302/* Caller must ensure led_cdev->led_access held */
303void led_sysfs_enable(struct led_classdev *led_cdev)
304{
305 lockdep_assert_held(&led_cdev->led_access);
306
307 led_cdev->flags &= ~LED_SYSFS_DISABLE;
308}
309EXPORT_SYMBOL_GPL(led_sysfs_enable);