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 device
*dev
,
56 struct device_attribute
*attr
, char *buf
)
58 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
59 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
61 sprintf(buf
, "%lu\n", timer_data
->delay_on
);
63 return strlen(buf
) + 1;
66 static ssize_t
led_delay_on_store(struct device
*dev
,
67 struct device_attribute
*attr
, const char *buf
, size_t size
)
69 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
70 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
73 unsigned long state
= simple_strtoul(buf
, &after
, 10);
74 size_t count
= after
- buf
;
76 if (*after
&& isspace(*after
))
80 if (timer_data
->delay_on
!= state
) {
81 /* the new value differs from the previous */
82 timer_data
->delay_on
= state
;
84 /* deactivate previous settings */
85 del_timer_sync(&timer_data
->timer
);
87 /* try to activate hardware acceleration, if any */
88 if (!led_cdev
->blink_set
||
89 led_cdev
->blink_set(led_cdev
,
90 &timer_data
->delay_on
, &timer_data
->delay_off
)) {
91 /* no hardware acceleration, blink via timer */
92 mod_timer(&timer_data
->timer
, jiffies
+ 1);
101 static ssize_t
led_delay_off_show(struct device
*dev
,
102 struct device_attribute
*attr
, char *buf
)
104 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
105 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
107 sprintf(buf
, "%lu\n", timer_data
->delay_off
);
109 return strlen(buf
) + 1;
112 static ssize_t
led_delay_off_store(struct device
*dev
,
113 struct device_attribute
*attr
, const char *buf
, size_t size
)
115 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
116 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
119 unsigned long state
= simple_strtoul(buf
, &after
, 10);
120 size_t count
= after
- buf
;
122 if (*after
&& isspace(*after
))
126 if (timer_data
->delay_off
!= state
) {
127 /* the new value differs from the previous */
128 timer_data
->delay_off
= state
;
130 /* deactivate previous settings */
131 del_timer_sync(&timer_data
->timer
);
133 /* try to activate hardware acceleration, if any */
134 if (!led_cdev
->blink_set
||
135 led_cdev
->blink_set(led_cdev
,
136 &timer_data
->delay_on
, &timer_data
->delay_off
)) {
137 /* no hardware acceleration, blink via timer */
138 mod_timer(&timer_data
->timer
, jiffies
+ 1);
147 static DEVICE_ATTR(delay_on
, 0644, led_delay_on_show
, led_delay_on_store
);
148 static DEVICE_ATTR(delay_off
, 0644, led_delay_off_show
, led_delay_off_store
);
150 static void timer_trig_activate(struct led_classdev
*led_cdev
)
152 struct timer_trig_data
*timer_data
;
155 timer_data
= kzalloc(sizeof(struct timer_trig_data
), GFP_KERNEL
);
159 led_cdev
->trigger_data
= timer_data
;
161 init_timer(&timer_data
->timer
);
162 timer_data
->timer
.function
= led_timer_function
;
163 timer_data
->timer
.data
= (unsigned long) led_cdev
;
165 rc
= device_create_file(led_cdev
->dev
, &dev_attr_delay_on
);
168 rc
= device_create_file(led_cdev
->dev
, &dev_attr_delay_off
);
170 goto err_out_delayon
;
172 /* If there is hardware support for blinking, start one
173 * user friendly blink rate chosen by the driver.
175 if (led_cdev
->blink_set
)
176 led_cdev
->blink_set(led_cdev
,
177 &timer_data
->delay_on
, &timer_data
->delay_off
);
182 device_remove_file(led_cdev
->dev
, &dev_attr_delay_on
);
184 led_cdev
->trigger_data
= NULL
;
188 static void timer_trig_deactivate(struct led_classdev
*led_cdev
)
190 struct timer_trig_data
*timer_data
= led_cdev
->trigger_data
;
193 device_remove_file(led_cdev
->dev
, &dev_attr_delay_on
);
194 device_remove_file(led_cdev
->dev
, &dev_attr_delay_off
);
195 del_timer_sync(&timer_data
->timer
);
200 static struct led_trigger timer_led_trigger
= {
202 .activate
= timer_trig_activate
,
203 .deactivate
= timer_trig_deactivate
,
206 static int __init
timer_trig_init(void)
208 return led_trigger_register(&timer_led_trigger
);
211 static void __exit
timer_trig_exit(void)
213 led_trigger_unregister(&timer_led_trigger
);
216 module_init(timer_trig_init
);
217 module_exit(timer_trig_exit
);
219 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
220 MODULE_DESCRIPTION("Timer LED trigger");
221 MODULE_LICENSE("GPL");