perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / leds / led-core.c
blobede4fa0ac2cceb736f69890a8823909a5d162f38
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(struct timer_list *t)
50 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
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 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
57 return;
60 if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
61 &led_cdev->work_flags)) {
62 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
63 return;
66 brightness = led_get_brightness(led_cdev);
67 if (!brightness) {
68 /* Time to switch the LED on. */
69 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE,
70 &led_cdev->work_flags))
71 brightness = led_cdev->new_blink_brightness;
72 else
73 brightness = led_cdev->blink_brightness;
74 delay = led_cdev->blink_delay_on;
75 } else {
76 /* Store the current brightness value to be able
77 * to restore it when the delay_off period is over.
79 led_cdev->blink_brightness = brightness;
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 (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
91 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
92 if (brightness)
93 set_bit(LED_BLINK_ONESHOT_STOP,
94 &led_cdev->work_flags);
95 } else {
96 if (!brightness)
97 set_bit(LED_BLINK_ONESHOT_STOP,
98 &led_cdev->work_flags);
102 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
105 static void set_brightness_delayed(struct work_struct *ws)
107 struct led_classdev *led_cdev =
108 container_of(ws, struct led_classdev, set_brightness_work);
109 int ret = 0;
111 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
112 led_cdev->delayed_set_value = LED_OFF;
113 led_stop_software_blink(led_cdev);
116 ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
117 if (ret == -ENOTSUPP)
118 ret = __led_set_brightness_blocking(led_cdev,
119 led_cdev->delayed_set_value);
120 if (ret < 0 &&
121 /* LED HW might have been unplugged, therefore don't warn */
122 !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
123 (led_cdev->flags & LED_HW_PLUGGABLE)))
124 dev_err(led_cdev->dev,
125 "Setting an LED's brightness failed (%d)\n", ret);
128 static void led_set_software_blink(struct led_classdev *led_cdev,
129 unsigned long delay_on,
130 unsigned long delay_off)
132 int current_brightness;
134 current_brightness = led_get_brightness(led_cdev);
135 if (current_brightness)
136 led_cdev->blink_brightness = current_brightness;
137 if (!led_cdev->blink_brightness)
138 led_cdev->blink_brightness = led_cdev->max_brightness;
140 led_cdev->blink_delay_on = delay_on;
141 led_cdev->blink_delay_off = delay_off;
143 /* never on - just set to off */
144 if (!delay_on) {
145 led_set_brightness_nosleep(led_cdev, LED_OFF);
146 return;
149 /* never off - just set to brightness */
150 if (!delay_off) {
151 led_set_brightness_nosleep(led_cdev,
152 led_cdev->blink_brightness);
153 return;
156 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
157 mod_timer(&led_cdev->blink_timer, jiffies + 1);
161 static void led_blink_setup(struct led_classdev *led_cdev,
162 unsigned long *delay_on,
163 unsigned long *delay_off)
165 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
166 led_cdev->blink_set &&
167 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
168 return;
170 /* blink with 1 Hz as default if nothing specified */
171 if (!*delay_on && !*delay_off)
172 *delay_on = *delay_off = 500;
174 led_set_software_blink(led_cdev, *delay_on, *delay_off);
177 void led_init_core(struct led_classdev *led_cdev)
179 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
181 timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
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 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
192 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
193 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
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 (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
205 timer_pending(&led_cdev->blink_timer))
206 return;
208 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
209 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
211 if (invert)
212 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
213 else
214 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
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 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
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 (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
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 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
244 schedule_work(&led_cdev->set_brightness_work);
245 } else {
246 set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
247 &led_cdev->work_flags);
248 led_cdev->new_blink_brightness = brightness;
250 return;
253 led_set_brightness_nosleep(led_cdev, brightness);
255 EXPORT_SYMBOL_GPL(led_set_brightness);
257 void led_set_brightness_nopm(struct led_classdev *led_cdev,
258 enum led_brightness value)
260 /* Use brightness_set op if available, it is guaranteed not to sleep */
261 if (!__led_set_brightness(led_cdev, value))
262 return;
264 /* If brightness setting can sleep, delegate it to a work queue task */
265 led_cdev->delayed_set_value = value;
266 schedule_work(&led_cdev->set_brightness_work);
268 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
270 void led_set_brightness_nosleep(struct led_classdev *led_cdev,
271 enum led_brightness value)
273 led_cdev->brightness = min(value, led_cdev->max_brightness);
275 if (led_cdev->flags & LED_SUSPENDED)
276 return;
278 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
280 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
282 int led_set_brightness_sync(struct led_classdev *led_cdev,
283 enum led_brightness value)
285 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
286 return -EBUSY;
288 led_cdev->brightness = min(value, led_cdev->max_brightness);
290 if (led_cdev->flags & LED_SUSPENDED)
291 return 0;
293 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
295 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
297 int led_update_brightness(struct led_classdev *led_cdev)
299 int ret = 0;
301 if (led_cdev->brightness_get) {
302 ret = led_cdev->brightness_get(led_cdev);
303 if (ret >= 0) {
304 led_cdev->brightness = ret;
305 return 0;
309 return ret;
311 EXPORT_SYMBOL_GPL(led_update_brightness);
313 /* Caller must ensure led_cdev->led_access held */
314 void led_sysfs_disable(struct led_classdev *led_cdev)
316 lockdep_assert_held(&led_cdev->led_access);
318 led_cdev->flags |= LED_SYSFS_DISABLE;
320 EXPORT_SYMBOL_GPL(led_sysfs_disable);
322 /* Caller must ensure led_cdev->led_access held */
323 void led_sysfs_enable(struct led_classdev *led_cdev)
325 lockdep_assert_held(&led_cdev->led_access);
327 led_cdev->flags &= ~LED_SYSFS_DISABLE;
329 EXPORT_SYMBOL_GPL(led_sysfs_enable);