Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / leds / led-core.c
blob7107cd7e87cfde458fb0f24d0183c6de1566fa82
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LED Class Core
5 * Copyright 2005-2006 Openedhand Ltd.
7 * Author: Richard Purdie <rpurdie@openedhand.com>
8 */
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/rwsem.h>
17 #include <linux/slab.h>
18 #include "leds.h"
20 DECLARE_RWSEM(leds_list_lock);
21 EXPORT_SYMBOL_GPL(leds_list_lock);
23 LIST_HEAD(leds_list);
24 EXPORT_SYMBOL_GPL(leds_list);
26 static int __led_set_brightness(struct led_classdev *led_cdev,
27 enum led_brightness value)
29 if (!led_cdev->brightness_set)
30 return -ENOTSUPP;
32 led_cdev->brightness_set(led_cdev, value);
34 return 0;
37 static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
38 enum led_brightness value)
40 if (!led_cdev->brightness_set_blocking)
41 return -ENOTSUPP;
43 return led_cdev->brightness_set_blocking(led_cdev, value);
46 static void led_timer_function(struct timer_list *t)
48 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
49 unsigned long brightness;
50 unsigned long delay;
52 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
53 led_set_brightness_nosleep(led_cdev, LED_OFF);
54 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
55 return;
58 if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
59 &led_cdev->work_flags)) {
60 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
61 return;
64 brightness = led_get_brightness(led_cdev);
65 if (!brightness) {
66 /* Time to switch the LED on. */
67 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;
72 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.
77 led_cdev->blink_brightness = brightness;
78 brightness = LED_OFF;
79 delay = led_cdev->blink_delay_off;
82 led_set_brightness_nosleep(led_cdev, brightness);
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.
88 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
89 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
90 if (brightness)
91 set_bit(LED_BLINK_ONESHOT_STOP,
92 &led_cdev->work_flags);
93 } else {
94 if (!brightness)
95 set_bit(LED_BLINK_ONESHOT_STOP,
96 &led_cdev->work_flags);
100 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
103 static void set_brightness_delayed(struct work_struct *ws)
105 struct led_classdev *led_cdev =
106 container_of(ws, struct led_classdev, set_brightness_work);
107 int ret = 0;
109 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
110 led_cdev->delayed_set_value = LED_OFF;
111 led_stop_software_blink(led_cdev);
114 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);
118 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)))
122 dev_err(led_cdev->dev,
123 "Setting an LED's brightness failed (%d)\n", ret);
126 static void led_set_software_blink(struct led_classdev *led_cdev,
127 unsigned long delay_on,
128 unsigned long delay_off)
130 int current_brightness;
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;
138 led_cdev->blink_delay_on = delay_on;
139 led_cdev->blink_delay_off = delay_off;
141 /* never on - just set to off */
142 if (!delay_on) {
143 led_set_brightness_nosleep(led_cdev, LED_OFF);
144 return;
147 /* never off - just set to brightness */
148 if (!delay_off) {
149 led_set_brightness_nosleep(led_cdev,
150 led_cdev->blink_brightness);
151 return;
154 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
155 mod_timer(&led_cdev->blink_timer, jiffies + 1);
159 static void led_blink_setup(struct led_classdev *led_cdev,
160 unsigned long *delay_on,
161 unsigned long *delay_off)
163 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
164 led_cdev->blink_set &&
165 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
166 return;
168 /* blink with 1 Hz as default if nothing specified */
169 if (!*delay_on && !*delay_off)
170 *delay_on = *delay_off = 500;
172 led_set_software_blink(led_cdev, *delay_on, *delay_off);
175 void led_init_core(struct led_classdev *led_cdev)
177 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
179 timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
181 EXPORT_SYMBOL_GPL(led_init_core);
183 void led_blink_set(struct led_classdev *led_cdev,
184 unsigned long *delay_on,
185 unsigned long *delay_off)
187 del_timer_sync(&led_cdev->blink_timer);
189 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
190 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
191 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
193 led_blink_setup(led_cdev, delay_on, delay_off);
195 EXPORT_SYMBOL_GPL(led_blink_set);
197 void led_blink_set_oneshot(struct led_classdev *led_cdev,
198 unsigned long *delay_on,
199 unsigned long *delay_off,
200 int invert)
202 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
203 timer_pending(&led_cdev->blink_timer))
204 return;
206 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
207 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
209 if (invert)
210 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
211 else
212 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
214 led_blink_setup(led_cdev, delay_on, delay_off);
216 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
218 void led_stop_software_blink(struct led_classdev *led_cdev)
220 del_timer_sync(&led_cdev->blink_timer);
221 led_cdev->blink_delay_on = 0;
222 led_cdev->blink_delay_off = 0;
223 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
225 EXPORT_SYMBOL_GPL(led_stop_software_blink);
227 void led_set_brightness(struct led_classdev *led_cdev,
228 enum led_brightness brightness)
231 * If software blink is active, delay brightness setting
232 * until the next timer tick.
234 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
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.
240 if (brightness == LED_OFF) {
241 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
242 schedule_work(&led_cdev->set_brightness_work);
243 } else {
244 set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
245 &led_cdev->work_flags);
246 led_cdev->new_blink_brightness = brightness;
248 return;
251 led_set_brightness_nosleep(led_cdev, brightness);
253 EXPORT_SYMBOL_GPL(led_set_brightness);
255 void led_set_brightness_nopm(struct led_classdev *led_cdev,
256 enum led_brightness value)
258 /* Use brightness_set op if available, it is guaranteed not to sleep */
259 if (!__led_set_brightness(led_cdev, value))
260 return;
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);
266 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
268 void led_set_brightness_nosleep(struct led_classdev *led_cdev,
269 enum led_brightness value)
271 led_cdev->brightness = min(value, led_cdev->max_brightness);
273 if (led_cdev->flags & LED_SUSPENDED)
274 return;
276 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
278 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
280 int led_set_brightness_sync(struct led_classdev *led_cdev,
281 enum led_brightness value)
283 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
284 return -EBUSY;
286 led_cdev->brightness = min(value, led_cdev->max_brightness);
288 if (led_cdev->flags & LED_SUSPENDED)
289 return 0;
291 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
293 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
295 int led_update_brightness(struct led_classdev *led_cdev)
297 int ret = 0;
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;
307 return ret;
309 EXPORT_SYMBOL_GPL(led_update_brightness);
311 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
313 struct device_node *np = dev_of_node(led_cdev->dev);
314 u32 *pattern;
315 int count;
317 if (!np)
318 return NULL;
320 count = of_property_count_u32_elems(np, "led-pattern");
321 if (count < 0)
322 return NULL;
324 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
325 if (!pattern)
326 return NULL;
328 if (of_property_read_u32_array(np, "led-pattern", pattern, count)) {
329 kfree(pattern);
330 return NULL;
333 *size = count;
335 return pattern;
337 EXPORT_SYMBOL_GPL(led_get_default_pattern);
339 /* Caller must ensure led_cdev->led_access held */
340 void led_sysfs_disable(struct led_classdev *led_cdev)
342 lockdep_assert_held(&led_cdev->led_access);
344 led_cdev->flags |= LED_SYSFS_DISABLE;
346 EXPORT_SYMBOL_GPL(led_sysfs_disable);
348 /* Caller must ensure led_cdev->led_access held */
349 void led_sysfs_enable(struct led_classdev *led_cdev)
351 lockdep_assert_held(&led_cdev->led_access);
353 led_cdev->flags &= ~LED_SYSFS_DISABLE;
355 EXPORT_SYMBOL_GPL(led_sysfs_enable);