xref: /openbmc/linux/drivers/leds/led-core.c (revision 2127c01b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LED Class Core
4  *
5  * Copyright 2005-2006 Openedhand Ltd.
6  *
7  * Author: Richard Purdie <rpurdie@openedhand.com>
8  */
9 
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"
19 
20 DECLARE_RWSEM(leds_list_lock);
21 EXPORT_SYMBOL_GPL(leds_list_lock);
22 
23 LIST_HEAD(leds_list);
24 EXPORT_SYMBOL_GPL(leds_list);
25 
26 static 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 
37 static 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 
46 static void led_timer_function(struct timer_list *t)
47 {
48 	struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
49 	unsigned long brightness;
50 	unsigned long delay;
51 
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;
56 	}
57 
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;
62 	}
63 
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.
76 		 */
77 		led_cdev->blink_brightness = brightness;
78 		brightness = LED_OFF;
79 		delay = led_cdev->blink_delay_off;
80 	}
81 
82 	led_set_brightness_nosleep(led_cdev, brightness);
83 
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 	 */
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);
97 		}
98 	}
99 
100 	mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
101 }
102 
103 static 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);
107 	int ret = 0;
108 
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);
112 	}
113 
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);
124 }
125 
126 static 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 
138 	led_cdev->blink_delay_on = delay_on;
139 	led_cdev->blink_delay_off = delay_off;
140 
141 	/* never on - just set to off */
142 	if (!delay_on) {
143 		led_set_brightness_nosleep(led_cdev, LED_OFF);
144 		return;
145 	}
146 
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;
152 	}
153 
154 	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
155 	mod_timer(&led_cdev->blink_timer, jiffies + 1);
156 }
157 
158 
159 static void led_blink_setup(struct led_classdev *led_cdev,
160 		     unsigned long *delay_on,
161 		     unsigned long *delay_off)
162 {
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;
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 }
174 
175 void led_init_core(struct led_classdev *led_cdev)
176 {
177 	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
178 
179 	timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
180 }
181 EXPORT_SYMBOL_GPL(led_init_core);
182 
183 void led_blink_set(struct led_classdev *led_cdev,
184 		   unsigned long *delay_on,
185 		   unsigned long *delay_off)
186 {
187 	del_timer_sync(&led_cdev->blink_timer);
188 
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);
192 
193 	led_blink_setup(led_cdev, delay_on, delay_off);
194 }
195 EXPORT_SYMBOL_GPL(led_blink_set);
196 
197 void led_blink_set_oneshot(struct led_classdev *led_cdev,
198 			   unsigned long *delay_on,
199 			   unsigned long *delay_off,
200 			   int invert)
201 {
202 	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
203 	     timer_pending(&led_cdev->blink_timer))
204 		return;
205 
206 	set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
207 	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
208 
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);
213 
214 	led_blink_setup(led_cdev, delay_on, delay_off);
215 }
216 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
217 
218 void led_stop_software_blink(struct led_classdev *led_cdev)
219 {
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);
224 }
225 EXPORT_SYMBOL_GPL(led_stop_software_blink);
226 
227 void led_set_brightness(struct led_classdev *led_cdev,
228 			enum led_brightness brightness)
229 {
230 	/*
231 	 * If software blink is active, delay brightness setting
232 	 * until the next timer tick.
233 	 */
234 	if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
235 		/*
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) {
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;
247 		}
248 		return;
249 	}
250 
251 	led_set_brightness_nosleep(led_cdev, brightness);
252 }
253 EXPORT_SYMBOL_GPL(led_set_brightness);
254 
255 void 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 */
259 	if (!__led_set_brightness(led_cdev, value))
260 		return;
261 
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 }
266 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
267 
268 void 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 }
278 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
279 
280 int 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 
291 	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
292 }
293 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
294 
295 int 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 }
309 EXPORT_SYMBOL_GPL(led_update_brightness);
310 
311 u32 *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 }
337 EXPORT_SYMBOL_GPL(led_get_default_pattern);
338 
339 /* Caller must ensure led_cdev->led_access held */
340 void 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 }
346 EXPORT_SYMBOL_GPL(led_sysfs_disable);
347 
348 /* Caller must ensure led_cdev->led_access held */
349 void 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 }
355 EXPORT_SYMBOL_GPL(led_sysfs_enable);
356