blob: d756bdb01c59ab33eeb261d251bddecd45367f89 [file] [log] [blame]
Richard Purdie6655c6f2006-03-31 02:31:07 -08001/*
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
Richard Purdie6655c6f2006-03-31 02:31:07 -080014#include <linux/module.h>
Al Virof6a57032006-10-18 01:47:25 -040015#include <linux/jiffies.h>
Richard Purdie6655c6f2006-03-31 02:31:07 -080016#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>
Richard Purdie3dc7b822006-05-15 09:44:17 -070023#include <linux/ctype.h>
Richard Purdie6655c6f2006-03-31 02:31:07 -080024#include <linux/leds.h>
25#include "leds.h"
26
27struct timer_trig_data {
28 unsigned long delay_on; /* milliseconds on */
29 unsigned long delay_off; /* milliseconds off */
30 struct timer_list timer;
31};
32
33static void led_timer_function(unsigned long data)
34{
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;
39
40 if (!timer_data->delay_on || !timer_data->delay_off) {
41 led_set_brightness(led_cdev, LED_OFF);
42 return;
43 }
44
45 if (!led_cdev->brightness) {
46 brightness = LED_FULL;
47 delay = timer_data->delay_on;
48 }
49
50 led_set_brightness(led_cdev, brightness);
51
52 mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay));
53}
54
55static ssize_t led_delay_on_show(struct class_device *dev, char *buf)
56{
57 struct led_classdev *led_cdev = class_get_devdata(dev);
58 struct timer_trig_data *timer_data = led_cdev->trigger_data;
59
60 sprintf(buf, "%lu\n", timer_data->delay_on);
61
62 return strlen(buf) + 1;
63}
64
65static ssize_t led_delay_on_store(struct class_device *dev, const char *buf,
66 size_t size)
67{
68 struct led_classdev *led_cdev = class_get_devdata(dev);
69 struct timer_trig_data *timer_data = led_cdev->trigger_data;
70 int ret = -EINVAL;
71 char *after;
72 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -070073 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -080074
Richard Purdie3dc7b822006-05-15 09:44:17 -070075 if (*after && isspace(*after))
76 count++;
77
78 if (count == size) {
Richard Purdie6655c6f2006-03-31 02:31:07 -080079 timer_data->delay_on = state;
80 mod_timer(&timer_data->timer, jiffies + 1);
Richard Purdie3dc7b822006-05-15 09:44:17 -070081 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -080082 }
83
84 return ret;
85}
86
87static ssize_t led_delay_off_show(struct class_device *dev, char *buf)
88{
89 struct led_classdev *led_cdev = class_get_devdata(dev);
90 struct timer_trig_data *timer_data = led_cdev->trigger_data;
91
92 sprintf(buf, "%lu\n", timer_data->delay_off);
93
94 return strlen(buf) + 1;
95}
96
97static ssize_t led_delay_off_store(struct class_device *dev, const char *buf,
98 size_t size)
99{
100 struct led_classdev *led_cdev = class_get_devdata(dev);
101 struct timer_trig_data *timer_data = led_cdev->trigger_data;
102 int ret = -EINVAL;
103 char *after;
104 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -0700105 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800106
Richard Purdie3dc7b822006-05-15 09:44:17 -0700107 if (*after && isspace(*after))
108 count++;
109
110 if (count == size) {
Richard Purdie6655c6f2006-03-31 02:31:07 -0800111 timer_data->delay_off = state;
112 mod_timer(&timer_data->timer, jiffies + 1);
Richard Purdie3dc7b822006-05-15 09:44:17 -0700113 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800114 }
115
116 return ret;
117}
118
119static CLASS_DEVICE_ATTR(delay_on, 0644, led_delay_on_show,
120 led_delay_on_store);
121static CLASS_DEVICE_ATTR(delay_off, 0644, led_delay_off_show,
122 led_delay_off_store);
123
124static void timer_trig_activate(struct led_classdev *led_cdev)
125{
126 struct timer_trig_data *timer_data;
Jeff Garzik12fda162006-10-17 00:10:20 -0700127 int rc;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800128
129 timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL);
130 if (!timer_data)
131 return;
132
133 led_cdev->trigger_data = timer_data;
134
135 init_timer(&timer_data->timer);
136 timer_data->timer.function = led_timer_function;
137 timer_data->timer.data = (unsigned long) led_cdev;
138
Jeff Garzik12fda162006-10-17 00:10:20 -0700139 rc = class_device_create_file(led_cdev->class_dev,
Richard Purdie6655c6f2006-03-31 02:31:07 -0800140 &class_device_attr_delay_on);
Jeff Garzik12fda162006-10-17 00:10:20 -0700141 if (rc) goto err_out;
142 rc = class_device_create_file(led_cdev->class_dev,
Richard Purdie6655c6f2006-03-31 02:31:07 -0800143 &class_device_attr_delay_off);
Jeff Garzik12fda162006-10-17 00:10:20 -0700144 if (rc) goto err_out_delayon;
145
146 return;
147
148err_out_delayon:
149 class_device_remove_file(led_cdev->class_dev,
150 &class_device_attr_delay_on);
151err_out:
152 led_cdev->trigger_data = NULL;
153 kfree(timer_data);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800154}
155
156static void timer_trig_deactivate(struct led_classdev *led_cdev)
157{
158 struct timer_trig_data *timer_data = led_cdev->trigger_data;
159
160 if (timer_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);
166 kfree(timer_data);
167 }
168}
169
170static struct led_trigger timer_led_trigger = {
171 .name = "timer",
172 .activate = timer_trig_activate,
173 .deactivate = timer_trig_deactivate,
174};
175
176static int __init timer_trig_init(void)
177{
178 return led_trigger_register(&timer_led_trigger);
179}
180
181static void __exit timer_trig_exit(void)
182{
183 led_trigger_unregister(&timer_led_trigger);
184}
185
186module_init(timer_trig_init);
187module_exit(timer_trig_exit);
188
189MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
190MODULE_DESCRIPTION("Timer LED trigger");
191MODULE_LICENSE("GPL");