2 * LED Kernel Timer Trigger
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/module.h>
15 #include <linux/jiffies.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/ctype.h>
24 #include <linux/leds.h>
25 #include <linux/slab.h>
28 struct timer_trig_data
{
29 int brightness_on
; /* LED brightness during "on" period.
30 * (LED_OFF < brightness_on <= LED_FULL)
32 unsigned long delay_on
; /* milliseconds on */
33 unsigned long delay_off
; /* milliseconds off */
34 struct timer_list timer
;
37 static void led_timer_function(unsigned long data
)
39 struct led_classdev
*led_cdev
= (struct led_classdev
*) data
;
40 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
41 unsigned long brightness
;
44 if (!timer_data
->delay_on
|| !timer_data
->delay_off
) {
45 led_set_brightness(led_cdev
, LED_OFF
);
49 brightness
= led_get_brightness(led_cdev
);
51 /* Time to switch the LED on. */
52 brightness
= timer_data
->brightness_on
;
53 delay
= timer_data
->delay_on
;
55 /* Store the current brightness value to be able
56 * to restore it when the delay_off period is over.
58 timer_data
->brightness_on
= brightness
;
60 delay
= timer_data
->delay_off
;
63 led_set_brightness(led_cdev
, brightness
);
65 mod_timer(&timer_data
->timer
, jiffies
+ msecs_to_jiffies(delay
));
68 static ssize_t
led_delay_on_show(struct device
*dev
,
69 struct device_attribute
*attr
, char *buf
)
71 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
72 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
74 return sprintf(buf
, "%lu\n", timer_data
->delay_on
);
77 static ssize_t
led_delay_on_store(struct device
*dev
,
78 struct device_attribute
*attr
, const char *buf
, size_t size
)
80 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
81 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
84 unsigned long state
= simple_strtoul(buf
, &after
, 10);
85 size_t count
= after
- buf
;
91 if (timer_data
->delay_on
!= state
) {
92 /* the new value differs from the previous */
93 timer_data
->delay_on
= state
;
95 /* deactivate previous settings */
96 del_timer_sync(&timer_data
->timer
);
98 /* try to activate hardware acceleration, if any */
99 if (!led_cdev
->blink_set
||
100 led_cdev
->blink_set(led_cdev
,
101 &timer_data
->delay_on
, &timer_data
->delay_off
)) {
102 /* no hardware acceleration, blink via timer */
103 mod_timer(&timer_data
->timer
, jiffies
+ 1);
112 static ssize_t
led_delay_off_show(struct device
*dev
,
113 struct device_attribute
*attr
, char *buf
)
115 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
116 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
118 return sprintf(buf
, "%lu\n", timer_data
->delay_off
);
121 static ssize_t
led_delay_off_store(struct device
*dev
,
122 struct device_attribute
*attr
, const char *buf
, size_t size
)
124 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
125 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
128 unsigned long state
= simple_strtoul(buf
, &after
, 10);
129 size_t count
= after
- buf
;
135 if (timer_data
->delay_off
!= state
) {
136 /* the new value differs from the previous */
137 timer_data
->delay_off
= state
;
139 /* deactivate previous settings */
140 del_timer_sync(&timer_data
->timer
);
142 /* try to activate hardware acceleration, if any */
143 if (!led_cdev
->blink_set
||
144 led_cdev
->blink_set(led_cdev
,
145 &timer_data
->delay_on
, &timer_data
->delay_off
)) {
146 /* no hardware acceleration, blink via timer */
147 mod_timer(&timer_data
->timer
, jiffies
+ 1);
156 static DEVICE_ATTR(delay_on
, 0644, led_delay_on_show
, led_delay_on_store
);
157 static DEVICE_ATTR(delay_off
, 0644, led_delay_off_show
, led_delay_off_store
);
159 static void timer_trig_activate(struct led_classdev
*led_cdev
)
161 struct timer_trig_data
*timer_data
;
164 timer_data
= kzalloc(sizeof(struct timer_trig_data
), GFP_KERNEL
);
168 timer_data
->brightness_on
= led_get_brightness(led_cdev
);
169 if (timer_data
->brightness_on
== LED_OFF
)
170 timer_data
->brightness_on
= led_cdev
->max_brightness
;
171 led_cdev
->trigger_data
= timer_data
;
173 init_timer(&timer_data
->timer
);
174 timer_data
->timer
.function
= led_timer_function
;
175 timer_data
->timer
.data
= (unsigned long) led_cdev
;
177 rc
= device_create_file(led_cdev
->dev
, &dev_attr_delay_on
);
180 rc
= device_create_file(led_cdev
->dev
, &dev_attr_delay_off
);
182 goto err_out_delayon
;
184 /* If there is hardware support for blinking, start one
185 * user friendly blink rate chosen by the driver.
187 if (led_cdev
->blink_set
)
188 led_cdev
->blink_set(led_cdev
,
189 &timer_data
->delay_on
, &timer_data
->delay_off
);
194 device_remove_file(led_cdev
->dev
, &dev_attr_delay_on
);
196 led_cdev
->trigger_data
= NULL
;
200 static void timer_trig_deactivate(struct led_classdev
*led_cdev
)
202 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
203 unsigned long on
= 0, off
= 0;
206 device_remove_file(led_cdev
->dev
, &dev_attr_delay_on
);
207 device_remove_file(led_cdev
->dev
, &dev_attr_delay_off
);
208 del_timer_sync(&timer_data
->timer
);
212 /* If there is hardware support for blinking, stop it */
213 if (led_cdev
->blink_set
)
214 led_cdev
->blink_set(led_cdev
, &on
, &off
);
217 static struct led_trigger timer_led_trigger
= {
219 .activate
= timer_trig_activate
,
220 .deactivate
= timer_trig_deactivate
,
223 static int __init
timer_trig_init(void)
225 return led_trigger_register(&timer_led_trigger
);
228 static void __exit
timer_trig_exit(void)
230 led_trigger_unregister(&timer_led_trigger
);
233 module_init(timer_trig_init
);
234 module_exit(timer_trig_exit
);
236 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
237 MODULE_DESCRIPTION("Timer LED trigger");
238 MODULE_LICENSE("GPL");