Linux 4.2.1
[linux/fpc-iii.git] / drivers / leds / led-core.c
blob549de7e24cfdf445f27180ab7c776da21af50a94
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 void led_set_software_blink(struct led_classdev *led_cdev,
29 unsigned long delay_on,
30 unsigned long delay_off)
32 int current_brightness;
34 current_brightness = led_get_brightness(led_cdev);
35 if (current_brightness)
36 led_cdev->blink_brightness = current_brightness;
37 if (!led_cdev->blink_brightness)
38 led_cdev->blink_brightness = led_cdev->max_brightness;
40 led_cdev->blink_delay_on = delay_on;
41 led_cdev->blink_delay_off = delay_off;
43 /* never on - just set to off */
44 if (!delay_on) {
45 led_set_brightness_async(led_cdev, LED_OFF);
46 return;
49 /* never off - just set to brightness */
50 if (!delay_off) {
51 led_set_brightness_async(led_cdev, led_cdev->blink_brightness);
52 return;
55 mod_timer(&led_cdev->blink_timer, jiffies + 1);
59 static void led_blink_setup(struct led_classdev *led_cdev,
60 unsigned long *delay_on,
61 unsigned long *delay_off)
63 if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
64 led_cdev->blink_set &&
65 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
66 return;
68 /* blink with 1 Hz as default if nothing specified */
69 if (!*delay_on && !*delay_off)
70 *delay_on = *delay_off = 500;
72 led_set_software_blink(led_cdev, *delay_on, *delay_off);
75 void led_blink_set(struct led_classdev *led_cdev,
76 unsigned long *delay_on,
77 unsigned long *delay_off)
79 del_timer_sync(&led_cdev->blink_timer);
81 led_cdev->flags &= ~LED_BLINK_ONESHOT;
82 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
84 led_blink_setup(led_cdev, delay_on, delay_off);
86 EXPORT_SYMBOL(led_blink_set);
88 void led_blink_set_oneshot(struct led_classdev *led_cdev,
89 unsigned long *delay_on,
90 unsigned long *delay_off,
91 int invert)
93 if ((led_cdev->flags & LED_BLINK_ONESHOT) &&
94 timer_pending(&led_cdev->blink_timer))
95 return;
97 led_cdev->flags |= LED_BLINK_ONESHOT;
98 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
100 if (invert)
101 led_cdev->flags |= LED_BLINK_INVERT;
102 else
103 led_cdev->flags &= ~LED_BLINK_INVERT;
105 led_blink_setup(led_cdev, delay_on, delay_off);
107 EXPORT_SYMBOL(led_blink_set_oneshot);
109 void led_stop_software_blink(struct led_classdev *led_cdev)
111 del_timer_sync(&led_cdev->blink_timer);
112 led_cdev->blink_delay_on = 0;
113 led_cdev->blink_delay_off = 0;
115 EXPORT_SYMBOL_GPL(led_stop_software_blink);
117 void led_set_brightness(struct led_classdev *led_cdev,
118 enum led_brightness brightness)
120 int ret = 0;
122 /* delay brightness if soft-blink is active */
123 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) {
124 led_cdev->delayed_set_value = brightness;
125 if (brightness == LED_OFF)
126 schedule_work(&led_cdev->set_brightness_work);
127 return;
130 if (led_cdev->flags & SET_BRIGHTNESS_ASYNC) {
131 led_set_brightness_async(led_cdev, brightness);
132 return;
133 } else if (led_cdev->flags & SET_BRIGHTNESS_SYNC)
134 ret = led_set_brightness_sync(led_cdev, brightness);
135 else
136 ret = -EINVAL;
138 if (ret < 0)
139 dev_dbg(led_cdev->dev, "Setting LED brightness failed (%d)\n",
140 ret);
142 EXPORT_SYMBOL(led_set_brightness);
144 int led_update_brightness(struct led_classdev *led_cdev)
146 int ret = 0;
148 if (led_cdev->brightness_get) {
149 ret = led_cdev->brightness_get(led_cdev);
150 if (ret >= 0) {
151 led_cdev->brightness = ret;
152 return 0;
156 return ret;
158 EXPORT_SYMBOL(led_update_brightness);
160 /* Caller must ensure led_cdev->led_access held */
161 void led_sysfs_disable(struct led_classdev *led_cdev)
163 lockdep_assert_held(&led_cdev->led_access);
165 led_cdev->flags |= LED_SYSFS_DISABLE;
167 EXPORT_SYMBOL_GPL(led_sysfs_disable);
169 /* Caller must ensure led_cdev->led_access held */
170 void led_sysfs_enable(struct led_classdev *led_cdev)
172 lockdep_assert_held(&led_cdev->led_access);
174 led_cdev->flags &= ~LED_SYSFS_DISABLE;
176 EXPORT_SYMBOL_GPL(led_sysfs_enable);