xref: /openbmc/linux/drivers/leds/led-core.c (revision ae40e94f)
1 /*
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>
15 #include <linux/leds.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/of.h>
20 #include <linux/rwsem.h>
21 #include <linux/slab.h>
22 #include "leds.h"
23 
24 DECLARE_RWSEM(leds_list_lock);
25 EXPORT_SYMBOL_GPL(leds_list_lock);
26 
27 LIST_HEAD(leds_list);
28 EXPORT_SYMBOL_GPL(leds_list);
29 
30 static 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 
41 static 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 
50 static void led_timer_function(struct timer_list *t)
51 {
52 	struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
53 	unsigned long brightness;
54 	unsigned long delay;
55 
56 	if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
57 		led_set_brightness_nosleep(led_cdev, LED_OFF);
58 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
59 		return;
60 	}
61 
62 	if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
63 			       &led_cdev->work_flags)) {
64 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
65 		return;
66 	}
67 
68 	brightness = led_get_brightness(led_cdev);
69 	if (!brightness) {
70 		/* Time to switch the LED on. */
71 		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;
76 		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 		 */
81 		led_cdev->blink_brightness = brightness;
82 		brightness = LED_OFF;
83 		delay = led_cdev->blink_delay_off;
84 	}
85 
86 	led_set_brightness_nosleep(led_cdev, brightness);
87 
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 	 */
92 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
93 		if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
94 			if (brightness)
95 				set_bit(LED_BLINK_ONESHOT_STOP,
96 					&led_cdev->work_flags);
97 		} else {
98 			if (!brightness)
99 				set_bit(LED_BLINK_ONESHOT_STOP,
100 					&led_cdev->work_flags);
101 		}
102 	}
103 
104 	mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
105 }
106 
107 static 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);
111 	int ret = 0;
112 
113 	if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
114 		led_cdev->delayed_set_value = LED_OFF;
115 		led_stop_software_blink(led_cdev);
116 	}
117 
118 	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);
122 	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)))
126 		dev_err(led_cdev->dev,
127 			"Setting an LED's brightness failed (%d)\n", ret);
128 }
129 
130 static 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 
142 	led_cdev->blink_delay_on = delay_on;
143 	led_cdev->blink_delay_off = delay_off;
144 
145 	/* never on - just set to off */
146 	if (!delay_on) {
147 		led_set_brightness_nosleep(led_cdev, LED_OFF);
148 		return;
149 	}
150 
151 	/* never off - just set to brightness */
152 	if (!delay_off) {
153 		led_set_brightness_nosleep(led_cdev,
154 					   led_cdev->blink_brightness);
155 		return;
156 	}
157 
158 	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
159 	mod_timer(&led_cdev->blink_timer, jiffies + 1);
160 }
161 
162 
163 static void led_blink_setup(struct led_classdev *led_cdev,
164 		     unsigned long *delay_on,
165 		     unsigned long *delay_off)
166 {
167 	if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
168 	    led_cdev->blink_set &&
169 	    !led_cdev->blink_set(led_cdev, delay_on, delay_off))
170 		return;
171 
172 	/* blink with 1 Hz as default if nothing specified */
173 	if (!*delay_on && !*delay_off)
174 		*delay_on = *delay_off = 500;
175 
176 	led_set_software_blink(led_cdev, *delay_on, *delay_off);
177 }
178 
179 void led_init_core(struct led_classdev *led_cdev)
180 {
181 	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
182 
183 	timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
184 }
185 EXPORT_SYMBOL_GPL(led_init_core);
186 
187 void led_blink_set(struct led_classdev *led_cdev,
188 		   unsigned long *delay_on,
189 		   unsigned long *delay_off)
190 {
191 	del_timer_sync(&led_cdev->blink_timer);
192 
193 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
194 	clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
195 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
196 
197 	led_blink_setup(led_cdev, delay_on, delay_off);
198 }
199 EXPORT_SYMBOL_GPL(led_blink_set);
200 
201 void led_blink_set_oneshot(struct led_classdev *led_cdev,
202 			   unsigned long *delay_on,
203 			   unsigned long *delay_off,
204 			   int invert)
205 {
206 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
207 	     timer_pending(&led_cdev->blink_timer))
208 		return;
209 
210 	set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
211 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
212 
213 	if (invert)
214 		set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
215 	else
216 		clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
217 
218 	led_blink_setup(led_cdev, delay_on, delay_off);
219 }
220 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
221 
222 void led_stop_software_blink(struct led_classdev *led_cdev)
223 {
224 	del_timer_sync(&led_cdev->blink_timer);
225 	led_cdev->blink_delay_on = 0;
226 	led_cdev->blink_delay_off = 0;
227 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
228 }
229 EXPORT_SYMBOL_GPL(led_stop_software_blink);
230 
231 void led_set_brightness(struct led_classdev *led_cdev,
232 			enum led_brightness brightness)
233 {
234 	/*
235 	 * If software blink is active, delay brightness setting
236 	 * until the next timer tick.
237 	 */
238 	if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
239 		/*
240 		 * If we need to disable soft blinking delegate this to the
241 		 * work queue task to avoid problems in case we are called
242 		 * from hard irq context.
243 		 */
244 		if (brightness == LED_OFF) {
245 			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
246 			schedule_work(&led_cdev->set_brightness_work);
247 		} else {
248 			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
249 				&led_cdev->work_flags);
250 			led_cdev->new_blink_brightness = brightness;
251 		}
252 		return;
253 	}
254 
255 	led_set_brightness_nosleep(led_cdev, brightness);
256 }
257 EXPORT_SYMBOL_GPL(led_set_brightness);
258 
259 void led_set_brightness_nopm(struct led_classdev *led_cdev,
260 			      enum led_brightness value)
261 {
262 	/* Use brightness_set op if available, it is guaranteed not to sleep */
263 	if (!__led_set_brightness(led_cdev, value))
264 		return;
265 
266 	/* If brightness setting can sleep, delegate it to a work queue task */
267 	led_cdev->delayed_set_value = value;
268 	schedule_work(&led_cdev->set_brightness_work);
269 }
270 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
271 
272 void led_set_brightness_nosleep(struct led_classdev *led_cdev,
273 				enum led_brightness value)
274 {
275 	led_cdev->brightness = min(value, led_cdev->max_brightness);
276 
277 	if (led_cdev->flags & LED_SUSPENDED)
278 		return;
279 
280 	led_set_brightness_nopm(led_cdev, led_cdev->brightness);
281 }
282 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
283 
284 int led_set_brightness_sync(struct led_classdev *led_cdev,
285 			    enum led_brightness value)
286 {
287 	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
288 		return -EBUSY;
289 
290 	led_cdev->brightness = min(value, led_cdev->max_brightness);
291 
292 	if (led_cdev->flags & LED_SUSPENDED)
293 		return 0;
294 
295 	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
296 }
297 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
298 
299 int led_update_brightness(struct led_classdev *led_cdev)
300 {
301 	int ret = 0;
302 
303 	if (led_cdev->brightness_get) {
304 		ret = led_cdev->brightness_get(led_cdev);
305 		if (ret >= 0) {
306 			led_cdev->brightness = ret;
307 			return 0;
308 		}
309 	}
310 
311 	return ret;
312 }
313 EXPORT_SYMBOL_GPL(led_update_brightness);
314 
315 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
316 {
317 	struct device_node *np = dev_of_node(led_cdev->dev);
318 	u32 *pattern;
319 	int count;
320 
321 	if (!np)
322 		return NULL;
323 
324 	count = of_property_count_u32_elems(np, "led-pattern");
325 	if (count < 0)
326 		return NULL;
327 
328 	pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
329 	if (!pattern)
330 		return NULL;
331 
332 	if (of_property_read_u32_array(np, "led-pattern", pattern, count)) {
333 		kfree(pattern);
334 		return NULL;
335 	}
336 
337 	*size = count;
338 
339 	return pattern;
340 }
341 EXPORT_SYMBOL_GPL(led_get_default_pattern);
342 
343 /* Caller must ensure led_cdev->led_access held */
344 void led_sysfs_disable(struct led_classdev *led_cdev)
345 {
346 	lockdep_assert_held(&led_cdev->led_access);
347 
348 	led_cdev->flags |= LED_SYSFS_DISABLE;
349 }
350 EXPORT_SYMBOL_GPL(led_sysfs_disable);
351 
352 /* Caller must ensure led_cdev->led_access held */
353 void led_sysfs_enable(struct led_classdev *led_cdev)
354 {
355 	lockdep_assert_held(&led_cdev->led_access);
356 
357 	led_cdev->flags &= ~LED_SYSFS_DISABLE;
358 }
359 EXPORT_SYMBOL_GPL(led_sysfs_enable);
360