aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/leds/Kconfig7
-rw-r--r--drivers/leds/Makefile3
-rw-r--r--drivers/leds/ledtrig-timer.c170
3 files changed, 180 insertions, 0 deletions
diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index a4d12ecaf6a5..fda44df5921f 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -22,5 +22,12 @@ config LEDS_TRIGGERS
22 These triggers allow kernel events to drive the LEDs and can 22 These triggers allow kernel events to drive the LEDs and can
23 be configured via sysfs. If unsure, say Y. 23 be configured via sysfs. If unsure, say Y.
24 24
25config LEDS_TRIGGER_TIMER
26 tristate "LED Timer Trigger"
27 depends LEDS_TRIGGERS
28 help
29 This allows LEDs to be controlled by a programmable timer
30 via sysfs. If unsure, say Y.
31
25endmenu 32endmenu
26 33
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index a9d8becd6189..81e4abb9dcb0 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -3,3 +3,6 @@
3obj-$(CONFIG_NEW_LEDS) += led-core.o 3obj-$(CONFIG_NEW_LEDS) += led-core.o
4obj-$(CONFIG_LEDS_CLASS) += led-class.o 4obj-$(CONFIG_LEDS_CLASS) += led-class.o
5obj-$(CONFIG_LEDS_TRIGGERS) += led-triggers.o 5obj-$(CONFIG_LEDS_TRIGGERS) += led-triggers.o
6
7# LED Triggers
8obj-$(CONFIG_LEDS_TRIGGER_TIMER) += ledtrig-timer.o
diff --git a/drivers/leds/ledtrig-timer.c b/drivers/leds/ledtrig-timer.c
new file mode 100644
index 000000000000..f484b5d6dbf8
--- /dev/null
+++ b/drivers/leds/ledtrig-timer.c
@@ -0,0 +1,170 @@
1/*
2 * LED Kernel Timer Trigger
3 *
4 * Copyright 2005-2006 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <rpurdie@openedhand.com>
7 *
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.
11 *
12 */
13
14#include <linux/config.h>
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/list.h>
19#include <linux/spinlock.h>
20#include <linux/device.h>
21#include <linux/sysdev.h>
22#include <linux/timer.h>
23#include <linux/leds.h>
24#include "leds.h"
25
26struct timer_trig_data {
27 unsigned long delay_on; /* milliseconds on */
28 unsigned long delay_off; /* milliseconds off */
29 struct timer_list timer;
30};
31
32static void led_timer_function(unsigned long data)
33{
34 struct led_classdev *led_cdev = (struct led_classdev *) data;
35 struct timer_trig_data *timer_data = led_cdev->trigger_data;
36 unsigned long brightness = LED_OFF;
37 unsigned long delay = timer_data->delay_off;
38
39 if (!timer_data->delay_on || !timer_data->delay_off) {
40 led_set_brightness(led_cdev, LED_OFF);
41 return;
42 }
43
44 if (!led_cdev->brightness) {
45 brightness = LED_FULL;
46 delay = timer_data->delay_on;
47 }
48
49 led_set_brightness(led_cdev, brightness);
50
51 mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay));
52}
53
54static ssize_t led_delay_on_show(struct class_device *dev, char *buf)
55{
56 struct led_classdev *led_cdev = class_get_devdata(dev);
57 struct timer_trig_data *timer_data = led_cdev->trigger_data;
58
59 sprintf(buf, "%lu\n", timer_data->delay_on);
60
61 return strlen(buf) + 1;
62}
63
64static ssize_t led_delay_on_store(struct class_device *dev, const char *buf,
65 size_t size)
66{
67 struct led_classdev *led_cdev = class_get_devdata(dev);
68 struct timer_trig_data *timer_data = led_cdev->trigger_data;
69 int ret = -EINVAL;
70 char *after;
71 unsigned long state = simple_strtoul(buf, &after, 10);
72
73 if (after - buf > 0) {
74 timer_data->delay_on = state;
75 mod_timer(&timer_data->timer, jiffies + 1);
76 ret = after - buf;
77 }
78
79 return ret;
80}
81
82static ssize_t led_delay_off_show(struct class_device *dev, char *buf)
83{
84 struct led_classdev *led_cdev = class_get_devdata(dev);
85 struct timer_trig_data *timer_data = led_cdev->trigger_data;
86
87 sprintf(buf, "%lu\n", timer_data->delay_off);
88
89 return strlen(buf) + 1;
90}
91
92static ssize_t led_delay_off_store(struct class_device *dev, const char *buf,
93 size_t size)
94{
95 struct led_classdev *led_cdev = class_get_devdata(dev);
96 struct timer_trig_data *timer_data = led_cdev->trigger_data;
97 int ret = -EINVAL;
98 char *after;
99 unsigned long state = simple_strtoul(buf, &after, 10);
100
101 if (after - buf > 0) {
102 timer_data->delay_off = state;
103 mod_timer(&timer_data->timer, jiffies + 1);
104 ret = after - buf;
105 }
106
107 return ret;
108}
109
110static CLASS_DEVICE_ATTR(delay_on, 0644, led_delay_on_show,
111 led_delay_on_store);
112static CLASS_DEVICE_ATTR(delay_off, 0644, led_delay_off_show,
113 led_delay_off_store);
114
115static void timer_trig_activate(struct led_classdev *led_cdev)
116{
117 struct timer_trig_data *timer_data;
118
119 timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL);
120 if (!timer_data)
121 return;
122
123 led_cdev->trigger_data = timer_data;
124
125 init_timer(&timer_data->timer);
126 timer_data->timer.function = led_timer_function;
127 timer_data->timer.data = (unsigned long) led_cdev;
128
129 class_device_create_file(led_cdev->class_dev,
130 &class_device_attr_delay_on);
131 class_device_create_file(led_cdev->class_dev,
132 &class_device_attr_delay_off);
133}
134
135static void timer_trig_deactivate(struct led_classdev *led_cdev)
136{
137 struct timer_trig_data *timer_data = led_cdev->trigger_data;
138
139 if (timer_data) {
140 class_device_remove_file(led_cdev->class_dev,
141 &class_device_attr_delay_on);
142 class_device_remove_file(led_cdev->class_dev,
143 &class_device_attr_delay_off);
144 del_timer_sync(&timer_data->timer);
145 kfree(timer_data);
146 }
147}
148
149static struct led_trigger timer_led_trigger = {
150 .name = "timer",
151 .activate = timer_trig_activate,
152 .deactivate = timer_trig_deactivate,
153};
154
155static int __init timer_trig_init(void)
156{
157 return led_trigger_register(&timer_led_trigger);
158}
159
160static void __exit timer_trig_exit(void)
161{
162 led_trigger_unregister(&timer_led_trigger);
163}
164
165module_init(timer_trig_init);
166module_exit(timer_trig_exit);
167
168MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
169MODULE_DESCRIPTION("Timer LED trigger");
170MODULE_LICENSE("GPL");