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>
27 struct timer_trig_data
{
28 unsigned long delay_on
; /* milliseconds on */
29 unsigned long delay_off
; /* milliseconds off */
30 struct timer_list timer
;
33 static void led_timer_function(unsigned long data
)
35 struct led_classdev
*led_cdev
= (struct led_classdev
*) data
;
36 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
37 unsigned long brightness
= LED_OFF
;
38 unsigned long delay
= timer_data
->delay_off
;
40 if (!timer_data
->delay_on
|| !timer_data
->delay_off
) {
41 led_set_brightness(led_cdev
, LED_OFF
);
45 if (!led_cdev
->brightness
) {
46 brightness
= LED_FULL
;
47 delay
= timer_data
->delay_on
;
50 led_set_brightness(led_cdev
, brightness
);
52 mod_timer(&timer_data
->timer
, jiffies
+ msecs_to_jiffies(delay
));
55 static ssize_t
led_delay_on_show(struct class_device
*dev
, char *buf
)
57 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
58 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
60 sprintf(buf
, "%lu\n", timer_data
->delay_on
);
62 return strlen(buf
) + 1;
65 static ssize_t
led_delay_on_store(struct class_device
*dev
, const char *buf
,
68 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
69 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
72 unsigned long state
= simple_strtoul(buf
, &after
, 10);
73 size_t count
= after
- buf
;
75 if (*after
&& isspace(*after
))
79 timer_data
->delay_on
= state
;
80 mod_timer(&timer_data
->timer
, jiffies
+ 1);
87 static ssize_t
led_delay_off_show(struct class_device
*dev
, char *buf
)
89 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
90 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
92 sprintf(buf
, "%lu\n", timer_data
->delay_off
);
94 return strlen(buf
) + 1;
97 static ssize_t
led_delay_off_store(struct class_device
*dev
, const char *buf
,
100 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
101 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
104 unsigned long state
= simple_strtoul(buf
, &after
, 10);
105 size_t count
= after
- buf
;
107 if (*after
&& isspace(*after
))
111 timer_data
->delay_off
= state
;
112 mod_timer(&timer_data
->timer
, jiffies
+ 1);
119 static CLASS_DEVICE_ATTR(delay_on
, 0644, led_delay_on_show
,
121 static CLASS_DEVICE_ATTR(delay_off
, 0644, led_delay_off_show
,
122 led_delay_off_store
);
124 static void timer_trig_activate(struct led_classdev
*led_cdev
)
126 struct timer_trig_data
*timer_data
;
129 timer_data
= kzalloc(sizeof(struct timer_trig_data
), GFP_KERNEL
);
133 led_cdev
->trigger_data
= timer_data
;
135 init_timer(&timer_data
->timer
);
136 timer_data
->timer
.function
= led_timer_function
;
137 timer_data
->timer
.data
= (unsigned long) led_cdev
;
139 rc
= class_device_create_file(led_cdev
->class_dev
,
140 &class_device_attr_delay_on
);
141 if (rc
) goto err_out
;
142 rc
= class_device_create_file(led_cdev
->class_dev
,
143 &class_device_attr_delay_off
);
144 if (rc
) goto err_out_delayon
;
149 class_device_remove_file(led_cdev
->class_dev
,
150 &class_device_attr_delay_on
);
152 led_cdev
->trigger_data
= NULL
;
156 static void timer_trig_deactivate(struct led_classdev
*led_cdev
)
158 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
161 class_device_remove_file(led_cdev
->class_dev
,
162 &class_device_attr_delay_on
);
163 class_device_remove_file(led_cdev
->class_dev
,
164 &class_device_attr_delay_off
);
165 del_timer_sync(&timer_data
->timer
);
170 static struct led_trigger timer_led_trigger
= {
172 .activate
= timer_trig_activate
,
173 .deactivate
= timer_trig_deactivate
,
176 static int __init
timer_trig_init(void)
178 return led_trigger_register(&timer_led_trigger
);
181 static void __exit
timer_trig_exit(void)
183 led_trigger_unregister(&timer_led_trigger
);
186 module_init(timer_trig_init
);
187 module_exit(timer_trig_exit
);
189 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
190 MODULE_DESCRIPTION("Timer LED trigger");
191 MODULE_LICENSE("GPL");