blob: 82b77bd482ffdb4f952f0785c197db6efe1e8736 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Richard Purdie6655c6f2006-03-31 02:31:07 -080026#include "leds.h"
27
28struct timer_trig_data {
Németh Márton0013b232008-03-09 20:54:37 +000029 int brightness_on; /* LED brightness during "on" period.
30 * (LED_OFF < brightness_on <= LED_FULL)
31 */
Richard Purdie6655c6f2006-03-31 02:31:07 -080032 unsigned long delay_on; /* milliseconds on */
33 unsigned long delay_off; /* milliseconds off */
34 struct timer_list timer;
35};
36
37static void led_timer_function(unsigned long data)
38{
39 struct led_classdev *led_cdev = (struct led_classdev *) data;
40 struct timer_trig_data *timer_data = led_cdev->trigger_data;
Németh Márton0013b232008-03-09 20:54:37 +000041 unsigned long brightness;
42 unsigned long delay;
Richard Purdie6655c6f2006-03-31 02:31:07 -080043
44 if (!timer_data->delay_on || !timer_data->delay_off) {
45 led_set_brightness(led_cdev, LED_OFF);
46 return;
47 }
48
Németh Márton0013b232008-03-09 20:54:37 +000049 brightness = led_get_brightness(led_cdev);
50 if (!brightness) {
51 /* Time to switch the LED on. */
52 brightness = timer_data->brightness_on;
Richard Purdie6655c6f2006-03-31 02:31:07 -080053 delay = timer_data->delay_on;
Németh Márton0013b232008-03-09 20:54:37 +000054 } else {
55 /* Store the current brightness value to be able
56 * to restore it when the delay_off period is over.
57 */
58 timer_data->brightness_on = brightness;
59 brightness = LED_OFF;
60 delay = timer_data->delay_off;
Richard Purdie6655c6f2006-03-31 02:31:07 -080061 }
62
63 led_set_brightness(led_cdev, brightness);
64
65 mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay));
66}
67
Németh Márton4d404fd2008-03-09 20:59:57 +000068static ssize_t led_delay_on_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010069 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080070{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010071 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080072 struct timer_trig_data *timer_data = led_cdev->trigger_data;
73
Sven Wegenerdd8e5a22008-10-13 10:41:39 +010074 return sprintf(buf, "%lu\n", timer_data->delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -080075}
76
Németh Márton4d404fd2008-03-09 20:59:57 +000077static ssize_t led_delay_on_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010078 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080079{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010080 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080081 struct timer_trig_data *timer_data = led_cdev->trigger_data;
82 int ret = -EINVAL;
83 char *after;
84 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -070085 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -080086
André Goddard Rosae7d28602009-12-14 18:01:06 -080087 if (isspace(*after))
Richard Purdie3dc7b822006-05-15 09:44:17 -070088 count++;
89
90 if (count == size) {
Márton Németh4c791412007-10-31 15:07:12 +010091 if (timer_data->delay_on != state) {
92 /* the new value differs from the previous */
93 timer_data->delay_on = state;
94
95 /* deactivate previous settings */
96 del_timer_sync(&timer_data->timer);
97
98 /* try to activate hardware acceleration, if any */
99 if (!led_cdev->blink_set ||
100 led_cdev->blink_set(led_cdev,
Németh Márton4d404fd2008-03-09 20:59:57 +0000101 &timer_data->delay_on, &timer_data->delay_off)) {
Márton Németh4c791412007-10-31 15:07:12 +0100102 /* no hardware acceleration, blink via timer */
103 mod_timer(&timer_data->timer, jiffies + 1);
104 }
105 }
Richard Purdie3dc7b822006-05-15 09:44:17 -0700106 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800107 }
108
109 return ret;
110}
111
Németh Márton4d404fd2008-03-09 20:59:57 +0000112static ssize_t led_delay_off_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100113 struct device_attribute *attr, char *buf)
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
Sven Wegenerdd8e5a22008-10-13 10:41:39 +0100118 return sprintf(buf, "%lu\n", timer_data->delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800119}
120
Németh Márton4d404fd2008-03-09 20:59:57 +0000121static ssize_t led_delay_off_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100122 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -0800123{
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100124 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800125 struct timer_trig_data *timer_data = led_cdev->trigger_data;
126 int ret = -EINVAL;
127 char *after;
128 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -0700129 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800130
André Goddard Rosae7d28602009-12-14 18:01:06 -0800131 if (isspace(*after))
Richard Purdie3dc7b822006-05-15 09:44:17 -0700132 count++;
133
134 if (count == size) {
Márton Németh4c791412007-10-31 15:07:12 +0100135 if (timer_data->delay_off != state) {
136 /* the new value differs from the previous */
137 timer_data->delay_off = state;
138
139 /* deactivate previous settings */
140 del_timer_sync(&timer_data->timer);
141
142 /* try to activate hardware acceleration, if any */
143 if (!led_cdev->blink_set ||
144 led_cdev->blink_set(led_cdev,
Németh Márton4d404fd2008-03-09 20:59:57 +0000145 &timer_data->delay_on, &timer_data->delay_off)) {
Márton Németh4c791412007-10-31 15:07:12 +0100146 /* no hardware acceleration, blink via timer */
147 mod_timer(&timer_data->timer, jiffies + 1);
148 }
149 }
Richard Purdie3dc7b822006-05-15 09:44:17 -0700150 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800151 }
152
153 return ret;
154}
155
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100156static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
157static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800158
159static void timer_trig_activate(struct led_classdev *led_cdev)
160{
161 struct timer_trig_data *timer_data;
Jeff Garzik12fda162006-10-17 00:10:20 -0700162 int rc;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800163
164 timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL);
165 if (!timer_data)
166 return;
167
Németh Márton0013b232008-03-09 20:54:37 +0000168 timer_data->brightness_on = led_get_brightness(led_cdev);
169 if (timer_data->brightness_on == LED_OFF)
Guennadi Liakhovetski1bd465e2009-01-10 18:54:39 +0000170 timer_data->brightness_on = led_cdev->max_brightness;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800171 led_cdev->trigger_data = timer_data;
172
173 init_timer(&timer_data->timer);
174 timer_data->timer.function = led_timer_function;
175 timer_data->timer.data = (unsigned long) led_cdev;
176
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100177 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
178 if (rc)
179 goto err_out;
180 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
181 if (rc)
182 goto err_out_delayon;
Jeff Garzik12fda162006-10-17 00:10:20 -0700183
Márton Németh4c791412007-10-31 15:07:12 +0100184 /* If there is hardware support for blinking, start one
185 * user friendly blink rate chosen by the driver.
186 */
187 if (led_cdev->blink_set)
188 led_cdev->blink_set(led_cdev,
189 &timer_data->delay_on, &timer_data->delay_off);
190
Jeff Garzik12fda162006-10-17 00:10:20 -0700191 return;
192
193err_out_delayon:
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100194 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
Jeff Garzik12fda162006-10-17 00:10:20 -0700195err_out:
196 led_cdev->trigger_data = NULL;
197 kfree(timer_data);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800198}
199
200static void timer_trig_deactivate(struct led_classdev *led_cdev)
201{
202 struct timer_trig_data *timer_data = led_cdev->trigger_data;
Rodolfo Giomettice70a242009-01-08 19:50:15 +0000203 unsigned long on = 0, off = 0;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800204
205 if (timer_data) {
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100206 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
207 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800208 del_timer_sync(&timer_data->timer);
209 kfree(timer_data);
210 }
Rodolfo Giomettice70a242009-01-08 19:50:15 +0000211
212 /* If there is hardware support for blinking, stop it */
213 if (led_cdev->blink_set)
214 led_cdev->blink_set(led_cdev, &on, &off);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800215}
216
217static struct led_trigger timer_led_trigger = {
218 .name = "timer",
219 .activate = timer_trig_activate,
220 .deactivate = timer_trig_deactivate,
221};
222
223static int __init timer_trig_init(void)
224{
225 return led_trigger_register(&timer_led_trigger);
226}
227
228static void __exit timer_trig_exit(void)
229{
230 led_trigger_unregister(&timer_led_trigger);
231}
232
233module_init(timer_trig_init);
234module_exit(timer_trig_exit);
235
236MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
237MODULE_DESCRIPTION("Timer LED trigger");
238MODULE_LICENSE("GPL");