blob: 706297765d93ae4006b1c7ac8df0802ec9a99847 [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 {
Németh Márton0013b232008-03-09 20:54:37 +000028 int brightness_on; /* LED brightness during "on" period.
29 * (LED_OFF < brightness_on <= LED_FULL)
30 */
Richard Purdie6655c6f2006-03-31 02:31:07 -080031 unsigned long delay_on; /* milliseconds on */
32 unsigned long delay_off; /* milliseconds off */
33 struct timer_list timer;
34};
35
36static void led_timer_function(unsigned long data)
37{
38 struct led_classdev *led_cdev = (struct led_classdev *) data;
39 struct timer_trig_data *timer_data = led_cdev->trigger_data;
Németh Márton0013b232008-03-09 20:54:37 +000040 unsigned long brightness;
41 unsigned long delay;
Richard Purdie6655c6f2006-03-31 02:31:07 -080042
43 if (!timer_data->delay_on || !timer_data->delay_off) {
44 led_set_brightness(led_cdev, LED_OFF);
45 return;
46 }
47
Németh Márton0013b232008-03-09 20:54:37 +000048 brightness = led_get_brightness(led_cdev);
49 if (!brightness) {
50 /* Time to switch the LED on. */
51 brightness = timer_data->brightness_on;
Richard Purdie6655c6f2006-03-31 02:31:07 -080052 delay = timer_data->delay_on;
Németh Márton0013b232008-03-09 20:54:37 +000053 } else {
54 /* Store the current brightness value to be able
55 * to restore it when the delay_off period is over.
56 */
57 timer_data->brightness_on = brightness;
58 brightness = LED_OFF;
59 delay = timer_data->delay_off;
Richard Purdie6655c6f2006-03-31 02:31:07 -080060 }
61
62 led_set_brightness(led_cdev, brightness);
63
64 mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay));
65}
66
Richard Purdief8a7c6f2007-07-08 23:19:31 +010067static ssize_t led_delay_on_show(struct device *dev,
68 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080069{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010070 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080071 struct timer_trig_data *timer_data = led_cdev->trigger_data;
72
73 sprintf(buf, "%lu\n", timer_data->delay_on);
74
75 return strlen(buf) + 1;
76}
77
Richard Purdief8a7c6f2007-07-08 23:19:31 +010078static ssize_t led_delay_on_store(struct device *dev,
79 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080080{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010081 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080082 struct timer_trig_data *timer_data = led_cdev->trigger_data;
83 int ret = -EINVAL;
84 char *after;
85 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -070086 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -080087
Richard Purdie3dc7b822006-05-15 09:44:17 -070088 if (*after && isspace(*after))
89 count++;
90
91 if (count == size) {
Márton Németh4c791412007-10-31 15:07:12 +010092 if (timer_data->delay_on != state) {
93 /* the new value differs from the previous */
94 timer_data->delay_on = state;
95
96 /* deactivate previous settings */
97 del_timer_sync(&timer_data->timer);
98
99 /* try to activate hardware acceleration, if any */
100 if (!led_cdev->blink_set ||
101 led_cdev->blink_set(led_cdev,
102 &timer_data->delay_on, &timer_data->delay_off)) {
103 /* no hardware acceleration, blink via timer */
104 mod_timer(&timer_data->timer, jiffies + 1);
105 }
106 }
Richard Purdie3dc7b822006-05-15 09:44:17 -0700107 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800108 }
109
110 return ret;
111}
112
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100113static ssize_t led_delay_off_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -0800115{
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100116 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800117 struct timer_trig_data *timer_data = led_cdev->trigger_data;
118
119 sprintf(buf, "%lu\n", timer_data->delay_off);
120
121 return strlen(buf) + 1;
122}
123
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100124static ssize_t led_delay_off_store(struct device *dev,
125 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -0800126{
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100127 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800128 struct timer_trig_data *timer_data = led_cdev->trigger_data;
129 int ret = -EINVAL;
130 char *after;
131 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -0700132 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800133
Richard Purdie3dc7b822006-05-15 09:44:17 -0700134 if (*after && isspace(*after))
135 count++;
136
137 if (count == size) {
Márton Németh4c791412007-10-31 15:07:12 +0100138 if (timer_data->delay_off != state) {
139 /* the new value differs from the previous */
140 timer_data->delay_off = state;
141
142 /* deactivate previous settings */
143 del_timer_sync(&timer_data->timer);
144
145 /* try to activate hardware acceleration, if any */
146 if (!led_cdev->blink_set ||
147 led_cdev->blink_set(led_cdev,
148 &timer_data->delay_on, &timer_data->delay_off)) {
149 /* no hardware acceleration, blink via timer */
150 mod_timer(&timer_data->timer, jiffies + 1);
151 }
152 }
Richard Purdie3dc7b822006-05-15 09:44:17 -0700153 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800154 }
155
156 return ret;
157}
158
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100159static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
160static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800161
162static void timer_trig_activate(struct led_classdev *led_cdev)
163{
164 struct timer_trig_data *timer_data;
Jeff Garzik12fda162006-10-17 00:10:20 -0700165 int rc;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800166
167 timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL);
168 if (!timer_data)
169 return;
170
Németh Márton0013b232008-03-09 20:54:37 +0000171 timer_data->brightness_on = led_get_brightness(led_cdev);
172 if (timer_data->brightness_on == LED_OFF)
173 timer_data->brightness_on = LED_FULL;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800174 led_cdev->trigger_data = timer_data;
175
176 init_timer(&timer_data->timer);
177 timer_data->timer.function = led_timer_function;
178 timer_data->timer.data = (unsigned long) led_cdev;
179
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100180 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
181 if (rc)
182 goto err_out;
183 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
184 if (rc)
185 goto err_out_delayon;
Jeff Garzik12fda162006-10-17 00:10:20 -0700186
Márton Németh4c791412007-10-31 15:07:12 +0100187 /* If there is hardware support for blinking, start one
188 * user friendly blink rate chosen by the driver.
189 */
190 if (led_cdev->blink_set)
191 led_cdev->blink_set(led_cdev,
192 &timer_data->delay_on, &timer_data->delay_off);
193
Jeff Garzik12fda162006-10-17 00:10:20 -0700194 return;
195
196err_out_delayon:
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100197 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
Jeff Garzik12fda162006-10-17 00:10:20 -0700198err_out:
199 led_cdev->trigger_data = NULL;
200 kfree(timer_data);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800201}
202
203static void timer_trig_deactivate(struct led_classdev *led_cdev)
204{
205 struct timer_trig_data *timer_data = led_cdev->trigger_data;
206
207 if (timer_data) {
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100208 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
209 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800210 del_timer_sync(&timer_data->timer);
211 kfree(timer_data);
212 }
213}
214
215static struct led_trigger timer_led_trigger = {
216 .name = "timer",
217 .activate = timer_trig_activate,
218 .deactivate = timer_trig_deactivate,
219};
220
221static int __init timer_trig_init(void)
222{
223 return led_trigger_register(&timer_led_trigger);
224}
225
226static void __exit timer_trig_exit(void)
227{
228 led_trigger_unregister(&timer_led_trigger);
229}
230
231module_init(timer_trig_init);
232module_exit(timer_trig_exit);
233
234MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
235MODULE_DESCRIPTION("Timer LED trigger");
236MODULE_LICENSE("GPL");