r8152: fix tx packets accounting
[linux/fpc-iii.git] / drivers / leds / led-core.c
blob454ed4dc6ec1d2b93f2c0142c24b696e87f386d0
1 /*
2 * LED Class Core
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>
20 #include "leds.h"
22 DECLARE_RWSEM(leds_list_lock);
23 EXPORT_SYMBOL_GPL(leds_list_lock);
25 LIST_HEAD(leds_list);
26 EXPORT_SYMBOL_GPL(leds_list);
28 static int __led_set_brightness(struct led_classdev *led_cdev,
29 enum led_brightness value)
31 if (!led_cdev->brightness_set)
32 return -ENOTSUPP;
34 led_cdev->brightness_set(led_cdev, value);
36 return 0;
39 static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
40 enum led_brightness value)
42 if (!led_cdev->brightness_set_blocking)
43 return -ENOTSUPP;
45 return led_cdev->brightness_set_blocking(led_cdev, value);
48 static void led_timer_function(unsigned long data)
50 struct led_classdev *led_cdev = (void *)data;
51 unsigned long brightness;
52 unsigned long delay;
54 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
55 led_set_brightness_nosleep(led_cdev, LED_OFF);
56 led_cdev->flags &= ~LED_BLINK_SW;
57 return;
60 if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
61 led_cdev->flags &= ~(LED_BLINK_ONESHOT_STOP | LED_BLINK_SW);
62 return;
65 brightness = led_get_brightness(led_cdev);
66 if (!brightness) {
67 /* Time to switch the LED on. */
68 brightness = led_cdev->blink_brightness;
69 delay = led_cdev->blink_delay_on;
70 } else {
71 /* Store the current brightness value to be able
72 * to restore it when the delay_off period is over.
73 * Do it only if there is no pending blink brightness
74 * change, to avoid overwriting the new value.
76 if (!(led_cdev->flags & LED_BLINK_BRIGHTNESS_CHANGE))
77 led_cdev->blink_brightness = brightness;
78 else
79 led_cdev->flags &= ~LED_BLINK_BRIGHTNESS_CHANGE;
80 brightness = LED_OFF;
81 delay = led_cdev->blink_delay_off;
84 led_set_brightness_nosleep(led_cdev, brightness);
86 /* Return in next iteration if led is in one-shot mode and we are in
87 * the final blink state so that the led is toggled each delay_on +
88 * delay_off milliseconds in worst case.
90 if (led_cdev->flags & LED_BLINK_ONESHOT) {
91 if (led_cdev->flags & LED_BLINK_INVERT) {
92 if (brightness)
93 led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
94 } else {
95 if (!brightness)
96 led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
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 (led_cdev->flags & LED_BLINK_DISABLE) {
110 led_cdev->delayed_set_value = LED_OFF;
111 led_stop_software_blink(led_cdev);
112 led_cdev->flags &= ~LED_BLINK_DISABLE;
115 ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
116 if (ret == -ENOTSUPP)
117 ret = __led_set_brightness_blocking(led_cdev,
118 led_cdev->delayed_set_value);
119 if (ret < 0 &&
120 /* LED HW might have been unplugged, therefore don't warn */
121 !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
122 (led_cdev->flags & LED_HW_PLUGGABLE)))
123 dev_err(led_cdev->dev,
124 "Setting an LED's brightness failed (%d)\n", ret);
127 static void led_set_software_blink(struct led_classdev *led_cdev,
128 unsigned long delay_on,
129 unsigned long delay_off)
131 int current_brightness;
133 current_brightness = led_get_brightness(led_cdev);
134 if (current_brightness)
135 led_cdev->blink_brightness = current_brightness;
136 if (!led_cdev->blink_brightness)
137 led_cdev->blink_brightness = led_cdev->max_brightness;
139 led_cdev->blink_delay_on = delay_on;
140 led_cdev->blink_delay_off = delay_off;
142 /* never on - just set to off */
143 if (!delay_on) {
144 led_set_brightness_nosleep(led_cdev, LED_OFF);
145 return;
148 /* never off - just set to brightness */
149 if (!delay_off) {
150 led_set_brightness_nosleep(led_cdev,
151 led_cdev->blink_brightness);
152 return;
155 led_cdev->flags |= LED_BLINK_SW;
156 mod_timer(&led_cdev->blink_timer, jiffies + 1);
160 static void led_blink_setup(struct led_classdev *led_cdev,
161 unsigned long *delay_on,
162 unsigned long *delay_off)
164 if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
165 led_cdev->blink_set &&
166 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
167 return;
169 /* blink with 1 Hz as default if nothing specified */
170 if (!*delay_on && !*delay_off)
171 *delay_on = *delay_off = 500;
173 led_set_software_blink(led_cdev, *delay_on, *delay_off);
176 void led_init_core(struct led_classdev *led_cdev)
178 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
180 setup_timer(&led_cdev->blink_timer, led_timer_function,
181 (unsigned long)led_cdev);
183 EXPORT_SYMBOL_GPL(led_init_core);
185 void led_blink_set(struct led_classdev *led_cdev,
186 unsigned long *delay_on,
187 unsigned long *delay_off)
189 del_timer_sync(&led_cdev->blink_timer);
191 led_cdev->flags &= ~LED_BLINK_SW;
192 led_cdev->flags &= ~LED_BLINK_ONESHOT;
193 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
195 led_blink_setup(led_cdev, delay_on, delay_off);
197 EXPORT_SYMBOL_GPL(led_blink_set);
199 void led_blink_set_oneshot(struct led_classdev *led_cdev,
200 unsigned long *delay_on,
201 unsigned long *delay_off,
202 int invert)
204 if ((led_cdev->flags & LED_BLINK_ONESHOT) &&
205 timer_pending(&led_cdev->blink_timer))
206 return;
208 led_cdev->flags |= LED_BLINK_ONESHOT;
209 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
211 if (invert)
212 led_cdev->flags |= LED_BLINK_INVERT;
213 else
214 led_cdev->flags &= ~LED_BLINK_INVERT;
216 led_blink_setup(led_cdev, delay_on, delay_off);
218 EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
220 void led_stop_software_blink(struct led_classdev *led_cdev)
222 del_timer_sync(&led_cdev->blink_timer);
223 led_cdev->blink_delay_on = 0;
224 led_cdev->blink_delay_off = 0;
225 led_cdev->flags &= ~LED_BLINK_SW;
227 EXPORT_SYMBOL_GPL(led_stop_software_blink);
229 void led_set_brightness(struct led_classdev *led_cdev,
230 enum led_brightness brightness)
233 * If software blink is active, delay brightness setting
234 * until the next timer tick.
236 if (led_cdev->flags & LED_BLINK_SW) {
238 * If we need to disable soft blinking delegate this to the
239 * work queue task to avoid problems in case we are called
240 * from hard irq context.
242 if (brightness == LED_OFF) {
243 led_cdev->flags |= LED_BLINK_DISABLE;
244 schedule_work(&led_cdev->set_brightness_work);
245 } else {
246 led_cdev->flags |= LED_BLINK_BRIGHTNESS_CHANGE;
247 led_cdev->blink_brightness = brightness;
249 return;
252 led_set_brightness_nosleep(led_cdev, brightness);
254 EXPORT_SYMBOL_GPL(led_set_brightness);
256 void led_set_brightness_nopm(struct led_classdev *led_cdev,
257 enum led_brightness value)
259 /* Use brightness_set op if available, it is guaranteed not to sleep */
260 if (!__led_set_brightness(led_cdev, value))
261 return;
263 /* If brightness setting can sleep, delegate it to a work queue task */
264 led_cdev->delayed_set_value = value;
265 schedule_work(&led_cdev->set_brightness_work);
267 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
269 void led_set_brightness_nosleep(struct led_classdev *led_cdev,
270 enum led_brightness value)
272 led_cdev->brightness = min(value, led_cdev->max_brightness);
274 if (led_cdev->flags & LED_SUSPENDED)
275 return;
277 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
279 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
281 int led_set_brightness_sync(struct led_classdev *led_cdev,
282 enum led_brightness value)
284 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
285 return -EBUSY;
287 led_cdev->brightness = min(value, led_cdev->max_brightness);
289 if (led_cdev->flags & LED_SUSPENDED)
290 return 0;
292 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
294 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
296 int led_update_brightness(struct led_classdev *led_cdev)
298 int ret = 0;
300 if (led_cdev->brightness_get) {
301 ret = led_cdev->brightness_get(led_cdev);
302 if (ret >= 0) {
303 led_cdev->brightness = ret;
304 return 0;
308 return ret;
310 EXPORT_SYMBOL_GPL(led_update_brightness);
312 /* Caller must ensure led_cdev->led_access held */
313 void led_sysfs_disable(struct led_classdev *led_cdev)
315 lockdep_assert_held(&led_cdev->led_access);
317 led_cdev->flags |= LED_SYSFS_DISABLE;
319 EXPORT_SYMBOL_GPL(led_sysfs_disable);
321 /* Caller must ensure led_cdev->led_access held */
322 void led_sysfs_enable(struct led_classdev *led_cdev)
324 lockdep_assert_held(&led_cdev->led_access);
326 led_cdev->flags &= ~LED_SYSFS_DISABLE;
328 EXPORT_SYMBOL_GPL(led_sysfs_enable);