xref: /linux/drivers/leds/trigger/ledtrig-timer.c (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
1 /*
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 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/ctype.h>
18 #include <linux/leds.h>
19 
20 static ssize_t led_delay_on_show(struct device *dev,
21 		struct device_attribute *attr, char *buf)
22 {
23 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
24 
25 	return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
26 }
27 
28 static ssize_t led_delay_on_store(struct device *dev,
29 		struct device_attribute *attr, const char *buf, size_t size)
30 {
31 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
32 	unsigned long state;
33 	ssize_t ret = -EINVAL;
34 
35 	ret = kstrtoul(buf, 10, &state);
36 	if (ret)
37 		return ret;
38 
39 	led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
40 	led_cdev->blink_delay_on = state;
41 
42 	return size;
43 }
44 
45 static ssize_t led_delay_off_show(struct device *dev,
46 		struct device_attribute *attr, char *buf)
47 {
48 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
49 
50 	return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
51 }
52 
53 static ssize_t led_delay_off_store(struct device *dev,
54 		struct device_attribute *attr, const char *buf, size_t size)
55 {
56 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
57 	unsigned long state;
58 	ssize_t ret = -EINVAL;
59 
60 	ret = kstrtoul(buf, 10, &state);
61 	if (ret)
62 		return ret;
63 
64 	led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
65 	led_cdev->blink_delay_off = state;
66 
67 	return size;
68 }
69 
70 static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
71 static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
72 
73 static struct attribute *timer_trig_attrs[] = {
74 	&dev_attr_delay_on.attr,
75 	&dev_attr_delay_off.attr,
76 	NULL
77 };
78 ATTRIBUTE_GROUPS(timer_trig);
79 
80 static int timer_trig_activate(struct led_classdev *led_cdev)
81 {
82 	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
83 		      &led_cdev->blink_delay_off);
84 
85 	return 0;
86 }
87 
88 static void timer_trig_deactivate(struct led_classdev *led_cdev)
89 {
90 	/* Stop blinking */
91 	led_set_brightness(led_cdev, LED_OFF);
92 }
93 
94 static struct led_trigger timer_led_trigger = {
95 	.name     = "timer",
96 	.activate = timer_trig_activate,
97 	.deactivate = timer_trig_deactivate,
98 	.groups = timer_trig_groups,
99 };
100 module_led_trigger(timer_led_trigger);
101 
102 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
103 MODULE_DESCRIPTION("Timer LED trigger");
104 MODULE_LICENSE("GPL v2");
105