1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2005-2006 Openedhand Ltd.
7 * Author: Richard Purdie <rpurdie@openedhand.com>
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>
16 #include <linux/rwsem.h>
17 #include <linux/slab.h>
20 DECLARE_RWSEM(leds_list_lock
);
21 EXPORT_SYMBOL_GPL(leds_list_lock
);
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
)
32 led_cdev
->brightness_set(led_cdev
, value
);
37 static int __led_set_brightness_blocking(struct led_classdev
*led_cdev
,
38 enum led_brightness value
)
40 if (!led_cdev
->brightness_set_blocking
)
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
;
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
);
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
);
64 brightness
= led_get_brightness(led_cdev
);
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
;
71 brightness
= led_cdev
->blink_brightness
;
72 delay
= led_cdev
->blink_delay_on
;
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
;
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
)) {
91 set_bit(LED_BLINK_ONESHOT_STOP
,
92 &led_cdev
->work_flags
);
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
);
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
);
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 */
143 led_set_brightness_nosleep(led_cdev
, LED_OFF
);
147 /* never off - just set to brightness */
149 led_set_brightness_nosleep(led_cdev
,
150 led_cdev
->blink_brightness
);
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
))
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
,
202 if (test_bit(LED_BLINK_ONESHOT
, &led_cdev
->work_flags
) &&
203 timer_pending(&led_cdev
->blink_timer
))
206 set_bit(LED_BLINK_ONESHOT
, &led_cdev
->work_flags
);
207 clear_bit(LED_BLINK_ONESHOT_STOP
, &led_cdev
->work_flags
);
210 set_bit(LED_BLINK_INVERT
, &led_cdev
->work_flags
);
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
);
244 set_bit(LED_BLINK_BRIGHTNESS_CHANGE
,
245 &led_cdev
->work_flags
);
246 led_cdev
->new_blink_brightness
= brightness
;
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
))
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
)
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
)
286 led_cdev
->brightness
= min(value
, led_cdev
->max_brightness
);
288 if (led_cdev
->flags
& LED_SUSPENDED
)
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
)
299 if (led_cdev
->brightness_get
) {
300 ret
= led_cdev
->brightness_get(led_cdev
);
302 led_cdev
->brightness
= 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
);
320 count
= of_property_count_u32_elems(np
, "led-pattern");
324 pattern
= kcalloc(count
, sizeof(*pattern
), GFP_KERNEL
);
328 if (of_property_read_u32_array(np
, "led-pattern", pattern
, count
)) {
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
);