4 * Copyright 2005-2006 Openedhand Ltd.
6 * Author: Richard Purdie <rpurdie@openedhand.com>
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.
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/rwsem.h>
22 DECLARE_RWSEM(leds_list_lock
);
23 EXPORT_SYMBOL_GPL(leds_list_lock
);
26 EXPORT_SYMBOL_GPL(leds_list
);
28 static void led_timer_function(unsigned long data
)
30 struct led_classdev
*led_cdev
= (void *)data
;
31 unsigned long brightness
;
34 if (!led_cdev
->blink_delay_on
|| !led_cdev
->blink_delay_off
) {
35 led_set_brightness_async(led_cdev
, LED_OFF
);
39 if (led_cdev
->flags
& LED_BLINK_ONESHOT_STOP
) {
40 led_cdev
->flags
&= ~LED_BLINK_ONESHOT_STOP
;
44 brightness
= led_get_brightness(led_cdev
);
46 /* Time to switch the LED on. */
47 if (led_cdev
->delayed_set_value
) {
48 led_cdev
->blink_brightness
=
49 led_cdev
->delayed_set_value
;
50 led_cdev
->delayed_set_value
= 0;
52 brightness
= led_cdev
->blink_brightness
;
53 delay
= led_cdev
->blink_delay_on
;
55 /* Store the current brightness value to be able
56 * to restore it when the delay_off period is over.
58 led_cdev
->blink_brightness
= brightness
;
60 delay
= led_cdev
->blink_delay_off
;
63 led_set_brightness_async(led_cdev
, brightness
);
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.
69 if (led_cdev
->flags
& LED_BLINK_ONESHOT
) {
70 if (led_cdev
->flags
& LED_BLINK_INVERT
) {
72 led_cdev
->flags
|= LED_BLINK_ONESHOT_STOP
;
75 led_cdev
->flags
|= LED_BLINK_ONESHOT_STOP
;
79 mod_timer(&led_cdev
->blink_timer
, jiffies
+ msecs_to_jiffies(delay
));
82 static void set_brightness_delayed(struct work_struct
*ws
)
84 struct led_classdev
*led_cdev
=
85 container_of(ws
, struct led_classdev
, set_brightness_work
);
87 led_stop_software_blink(led_cdev
);
89 led_set_brightness_async(led_cdev
, led_cdev
->delayed_set_value
);
92 static void led_set_software_blink(struct led_classdev
*led_cdev
,
93 unsigned long delay_on
,
94 unsigned long delay_off
)
96 int current_brightness
;
98 current_brightness
= led_get_brightness(led_cdev
);
99 if (current_brightness
)
100 led_cdev
->blink_brightness
= current_brightness
;
101 if (!led_cdev
->blink_brightness
)
102 led_cdev
->blink_brightness
= led_cdev
->max_brightness
;
104 led_cdev
->blink_delay_on
= delay_on
;
105 led_cdev
->blink_delay_off
= delay_off
;
107 /* never on - just set to off */
109 led_set_brightness_async(led_cdev
, LED_OFF
);
113 /* never off - just set to brightness */
115 led_set_brightness_async(led_cdev
, led_cdev
->blink_brightness
);
119 mod_timer(&led_cdev
->blink_timer
, jiffies
+ 1);
123 static void led_blink_setup(struct led_classdev
*led_cdev
,
124 unsigned long *delay_on
,
125 unsigned long *delay_off
)
127 if (!(led_cdev
->flags
& LED_BLINK_ONESHOT
) &&
128 led_cdev
->blink_set
&&
129 !led_cdev
->blink_set(led_cdev
, delay_on
, delay_off
))
132 /* blink with 1 Hz as default if nothing specified */
133 if (!*delay_on
&& !*delay_off
)
134 *delay_on
= *delay_off
= 500;
136 led_set_software_blink(led_cdev
, *delay_on
, *delay_off
);
139 void led_init_core(struct led_classdev
*led_cdev
)
141 INIT_WORK(&led_cdev
->set_brightness_work
, set_brightness_delayed
);
143 setup_timer(&led_cdev
->blink_timer
, led_timer_function
,
144 (unsigned long)led_cdev
);
146 EXPORT_SYMBOL_GPL(led_init_core
);
148 void led_blink_set(struct led_classdev
*led_cdev
,
149 unsigned long *delay_on
,
150 unsigned long *delay_off
)
152 del_timer_sync(&led_cdev
->blink_timer
);
154 led_cdev
->flags
&= ~LED_BLINK_ONESHOT
;
155 led_cdev
->flags
&= ~LED_BLINK_ONESHOT_STOP
;
157 led_blink_setup(led_cdev
, delay_on
, delay_off
);
159 EXPORT_SYMBOL(led_blink_set
);
161 void led_blink_set_oneshot(struct led_classdev
*led_cdev
,
162 unsigned long *delay_on
,
163 unsigned long *delay_off
,
166 if ((led_cdev
->flags
& LED_BLINK_ONESHOT
) &&
167 timer_pending(&led_cdev
->blink_timer
))
170 led_cdev
->flags
|= LED_BLINK_ONESHOT
;
171 led_cdev
->flags
&= ~LED_BLINK_ONESHOT_STOP
;
174 led_cdev
->flags
|= LED_BLINK_INVERT
;
176 led_cdev
->flags
&= ~LED_BLINK_INVERT
;
178 led_blink_setup(led_cdev
, delay_on
, delay_off
);
180 EXPORT_SYMBOL(led_blink_set_oneshot
);
182 void led_stop_software_blink(struct led_classdev
*led_cdev
)
184 del_timer_sync(&led_cdev
->blink_timer
);
185 led_cdev
->blink_delay_on
= 0;
186 led_cdev
->blink_delay_off
= 0;
188 EXPORT_SYMBOL_GPL(led_stop_software_blink
);
190 void led_set_brightness(struct led_classdev
*led_cdev
,
191 enum led_brightness brightness
)
195 /* delay brightness if soft-blink is active */
196 if (led_cdev
->blink_delay_on
|| led_cdev
->blink_delay_off
) {
197 led_cdev
->delayed_set_value
= brightness
;
198 if (brightness
== LED_OFF
)
199 schedule_work(&led_cdev
->set_brightness_work
);
203 if (led_cdev
->flags
& SET_BRIGHTNESS_ASYNC
) {
204 led_set_brightness_async(led_cdev
, brightness
);
206 } else if (led_cdev
->flags
& SET_BRIGHTNESS_SYNC
)
207 ret
= led_set_brightness_sync(led_cdev
, brightness
);
212 dev_dbg(led_cdev
->dev
, "Setting LED brightness failed (%d)\n",
215 EXPORT_SYMBOL(led_set_brightness
);
217 int led_update_brightness(struct led_classdev
*led_cdev
)
221 if (led_cdev
->brightness_get
) {
222 ret
= led_cdev
->brightness_get(led_cdev
);
224 led_cdev
->brightness
= ret
;
231 EXPORT_SYMBOL(led_update_brightness
);
233 /* Caller must ensure led_cdev->led_access held */
234 void led_sysfs_disable(struct led_classdev
*led_cdev
)
236 lockdep_assert_held(&led_cdev
->led_access
);
238 led_cdev
->flags
|= LED_SYSFS_DISABLE
;
240 EXPORT_SYMBOL_GPL(led_sysfs_disable
);
242 /* Caller must ensure led_cdev->led_access held */
243 void led_sysfs_enable(struct led_classdev
*led_cdev
)
245 lockdep_assert_held(&led_cdev
->led_access
);
247 led_cdev
->flags
&= ~LED_SYSFS_DISABLE
;
249 EXPORT_SYMBOL_GPL(led_sysfs_enable
);