xref: /linux/drivers/leds/trigger/ledtrig-timer.c (revision fcc8487d477a3452a1d0ccbdd4c5e0e1e3cb8bed)
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 
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/ctype.h>
19 #include <linux/leds.h>
20 
21 static ssize_t led_delay_on_show(struct device *dev,
22 		struct device_attribute *attr, char *buf)
23 {
24 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
25 
26 	return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
27 }
28 
29 static ssize_t led_delay_on_store(struct device *dev,
30 		struct device_attribute *attr, const char *buf, size_t size)
31 {
32 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
33 	unsigned long state;
34 	ssize_t ret = -EINVAL;
35 
36 	ret = kstrtoul(buf, 10, &state);
37 	if (ret)
38 		return ret;
39 
40 	led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
41 	led_cdev->blink_delay_on = state;
42 
43 	return size;
44 }
45 
46 static ssize_t led_delay_off_show(struct device *dev,
47 		struct device_attribute *attr, char *buf)
48 {
49 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
50 
51 	return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
52 }
53 
54 static ssize_t led_delay_off_store(struct device *dev,
55 		struct device_attribute *attr, const char *buf, size_t size)
56 {
57 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
58 	unsigned long state;
59 	ssize_t ret = -EINVAL;
60 
61 	ret = kstrtoul(buf, 10, &state);
62 	if (ret)
63 		return ret;
64 
65 	led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
66 	led_cdev->blink_delay_off = state;
67 
68 	return size;
69 }
70 
71 static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
72 static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
73 
74 static void timer_trig_activate(struct led_classdev *led_cdev)
75 {
76 	int rc;
77 
78 	led_cdev->trigger_data = NULL;
79 
80 	rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
81 	if (rc)
82 		return;
83 	rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
84 	if (rc)
85 		goto err_out_delayon;
86 
87 	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
88 		      &led_cdev->blink_delay_off);
89 	led_cdev->activated = true;
90 
91 	return;
92 
93 err_out_delayon:
94 	device_remove_file(led_cdev->dev, &dev_attr_delay_on);
95 }
96 
97 static void timer_trig_deactivate(struct led_classdev *led_cdev)
98 {
99 	if (led_cdev->activated) {
100 		device_remove_file(led_cdev->dev, &dev_attr_delay_on);
101 		device_remove_file(led_cdev->dev, &dev_attr_delay_off);
102 		led_cdev->activated = false;
103 	}
104 
105 	/* Stop blinking */
106 	led_set_brightness(led_cdev, LED_OFF);
107 }
108 
109 static struct led_trigger timer_led_trigger = {
110 	.name     = "timer",
111 	.activate = timer_trig_activate,
112 	.deactivate = timer_trig_deactivate,
113 };
114 
115 static int __init timer_trig_init(void)
116 {
117 	return led_trigger_register(&timer_led_trigger);
118 }
119 
120 static void __exit timer_trig_exit(void)
121 {
122 	led_trigger_unregister(&timer_led_trigger);
123 }
124 
125 module_init(timer_trig_init);
126 module_exit(timer_trig_exit);
127 
128 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
129 MODULE_DESCRIPTION("Timer LED trigger");
130 MODULE_LICENSE("GPL");
131