blob: 82c55d6e49023ff9fe2866a819a1c5ec7bad7949 [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
Richard Purdief8a7c6f2007-07-08 23:19:31 +010055static ssize_t led_delay_on_show(struct device *dev,
56 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080057{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010058 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080059 struct timer_trig_data *timer_data = led_cdev->trigger_data;
60
61 sprintf(buf, "%lu\n", timer_data->delay_on);
62
63 return strlen(buf) + 1;
64}
65
Richard Purdief8a7c6f2007-07-08 23:19:31 +010066static ssize_t led_delay_on_store(struct device *dev,
67 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080068{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010069 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080070 struct timer_trig_data *timer_data = led_cdev->trigger_data;
71 int ret = -EINVAL;
72 char *after;
73 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -070074 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -080075
Richard Purdie3dc7b822006-05-15 09:44:17 -070076 if (*after && isspace(*after))
77 count++;
78
79 if (count == size) {
Márton Németh4c791412007-10-31 15:07:12 +010080 if (timer_data->delay_on != state) {
81 /* the new value differs from the previous */
82 timer_data->delay_on = state;
83
84 /* deactivate previous settings */
85 del_timer_sync(&timer_data->timer);
86
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);
93 }
94 }
Richard Purdie3dc7b822006-05-15 09:44:17 -070095 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -080096 }
97
98 return ret;
99}
100
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100101static ssize_t led_delay_off_show(struct device *dev,
102 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -0800103{
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100104 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800105 struct timer_trig_data *timer_data = led_cdev->trigger_data;
106
107 sprintf(buf, "%lu\n", timer_data->delay_off);
108
109 return strlen(buf) + 1;
110}
111
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100112static ssize_t led_delay_off_store(struct device *dev,
113 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -0800114{
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100115 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800116 struct timer_trig_data *timer_data = led_cdev->trigger_data;
117 int ret = -EINVAL;
118 char *after;
119 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -0700120 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800121
Richard Purdie3dc7b822006-05-15 09:44:17 -0700122 if (*after && isspace(*after))
123 count++;
124
125 if (count == size) {
Márton Németh4c791412007-10-31 15:07:12 +0100126 if (timer_data->delay_off != state) {
127 /* the new value differs from the previous */
128 timer_data->delay_off = state;
129
130 /* deactivate previous settings */
131 del_timer_sync(&timer_data->timer);
132
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);
139 }
140 }
Richard Purdie3dc7b822006-05-15 09:44:17 -0700141 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800142 }
143
144 return ret;
145}
146
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100147static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
148static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800149
150static void timer_trig_activate(struct led_classdev *led_cdev)
151{
152 struct timer_trig_data *timer_data;
Jeff Garzik12fda162006-10-17 00:10:20 -0700153 int rc;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800154
155 timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL);
156 if (!timer_data)
157 return;
158
159 led_cdev->trigger_data = timer_data;
160
161 init_timer(&timer_data->timer);
162 timer_data->timer.function = led_timer_function;
163 timer_data->timer.data = (unsigned long) led_cdev;
164
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100165 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
166 if (rc)
167 goto err_out;
168 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
169 if (rc)
170 goto err_out_delayon;
Jeff Garzik12fda162006-10-17 00:10:20 -0700171
Márton Németh4c791412007-10-31 15:07:12 +0100172 /* If there is hardware support for blinking, start one
173 * user friendly blink rate chosen by the driver.
174 */
175 if (led_cdev->blink_set)
176 led_cdev->blink_set(led_cdev,
177 &timer_data->delay_on, &timer_data->delay_off);
178
Jeff Garzik12fda162006-10-17 00:10:20 -0700179 return;
180
181err_out_delayon:
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100182 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
Jeff Garzik12fda162006-10-17 00:10:20 -0700183err_out:
184 led_cdev->trigger_data = NULL;
185 kfree(timer_data);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800186}
187
188static void timer_trig_deactivate(struct led_classdev *led_cdev)
189{
190 struct timer_trig_data *timer_data = led_cdev->trigger_data;
191
192 if (timer_data) {
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100193 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
194 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800195 del_timer_sync(&timer_data->timer);
196 kfree(timer_data);
197 }
198}
199
200static struct led_trigger timer_led_trigger = {
201 .name = "timer",
202 .activate = timer_trig_activate,
203 .deactivate = timer_trig_deactivate,
204};
205
206static int __init timer_trig_init(void)
207{
208 return led_trigger_register(&timer_led_trigger);
209}
210
211static void __exit timer_trig_exit(void)
212{
213 led_trigger_unregister(&timer_led_trigger);
214}
215
216module_init(timer_trig_init);
217module_exit(timer_trig_exit);
218
219MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
220MODULE_DESCRIPTION("Timer LED trigger");
221MODULE_LICENSE("GPL");