Linux 4.1.16
[linux/fpc-iii.git] / drivers / leds / trigger / ledtrig-oneshot.c
blobfbd02cdc3ad7d29a83cac5565117eb60f4cf9960
1 /*
2 * One-shot LED Trigger
4 * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
6 * Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com>
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.
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/slab.h>
20 #include <linux/leds.h>
21 #include "../leds.h"
23 #define DEFAULT_DELAY 100
25 struct oneshot_trig_data {
26 unsigned int invert;
29 static ssize_t led_shot(struct device *dev,
30 struct device_attribute *attr, const char *buf, size_t size)
32 struct led_classdev *led_cdev = dev_get_drvdata(dev);
33 struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
35 led_blink_set_oneshot(led_cdev,
36 &led_cdev->blink_delay_on, &led_cdev->blink_delay_off,
37 oneshot_data->invert);
39 /* content is ignored */
40 return size;
42 static ssize_t led_invert_show(struct device *dev,
43 struct device_attribute *attr, char *buf)
45 struct led_classdev *led_cdev = dev_get_drvdata(dev);
46 struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
48 return sprintf(buf, "%u\n", oneshot_data->invert);
51 static ssize_t led_invert_store(struct device *dev,
52 struct device_attribute *attr, const char *buf, size_t size)
54 struct led_classdev *led_cdev = dev_get_drvdata(dev);
55 struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
56 unsigned long state;
57 int ret;
59 ret = kstrtoul(buf, 0, &state);
60 if (ret)
61 return ret;
63 oneshot_data->invert = !!state;
65 if (oneshot_data->invert)
66 led_set_brightness_async(led_cdev, LED_FULL);
67 else
68 led_set_brightness_async(led_cdev, LED_OFF);
70 return size;
73 static ssize_t led_delay_on_show(struct device *dev,
74 struct device_attribute *attr, char *buf)
76 struct led_classdev *led_cdev = dev_get_drvdata(dev);
78 return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
81 static ssize_t led_delay_on_store(struct device *dev,
82 struct device_attribute *attr, const char *buf, size_t size)
84 struct led_classdev *led_cdev = dev_get_drvdata(dev);
85 unsigned long state;
86 int ret;
88 ret = kstrtoul(buf, 0, &state);
89 if (ret)
90 return ret;
92 led_cdev->blink_delay_on = state;
94 return size;
96 static ssize_t led_delay_off_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
99 struct led_classdev *led_cdev = dev_get_drvdata(dev);
101 return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
104 static ssize_t led_delay_off_store(struct device *dev,
105 struct device_attribute *attr, const char *buf, size_t size)
107 struct led_classdev *led_cdev = dev_get_drvdata(dev);
108 unsigned long state;
109 int ret;
111 ret = kstrtoul(buf, 0, &state);
112 if (ret)
113 return ret;
115 led_cdev->blink_delay_off = state;
117 return size;
120 static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
121 static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
122 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
123 static DEVICE_ATTR(shot, 0200, NULL, led_shot);
125 static void oneshot_trig_activate(struct led_classdev *led_cdev)
127 struct oneshot_trig_data *oneshot_data;
128 int rc;
130 oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL);
131 if (!oneshot_data)
132 return;
134 led_cdev->trigger_data = oneshot_data;
136 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
137 if (rc)
138 goto err_out_trig_data;
139 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
140 if (rc)
141 goto err_out_delayon;
142 rc = device_create_file(led_cdev->dev, &dev_attr_invert);
143 if (rc)
144 goto err_out_delayoff;
145 rc = device_create_file(led_cdev->dev, &dev_attr_shot);
146 if (rc)
147 goto err_out_invert;
149 led_cdev->blink_delay_on = DEFAULT_DELAY;
150 led_cdev->blink_delay_off = DEFAULT_DELAY;
152 led_cdev->activated = true;
154 return;
156 err_out_invert:
157 device_remove_file(led_cdev->dev, &dev_attr_invert);
158 err_out_delayoff:
159 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
160 err_out_delayon:
161 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
162 err_out_trig_data:
163 kfree(led_cdev->trigger_data);
166 static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
168 struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
170 if (led_cdev->activated) {
171 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
172 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
173 device_remove_file(led_cdev->dev, &dev_attr_invert);
174 device_remove_file(led_cdev->dev, &dev_attr_shot);
175 kfree(oneshot_data);
176 led_cdev->activated = false;
179 /* Stop blinking */
180 led_set_brightness(led_cdev, LED_OFF);
183 static struct led_trigger oneshot_led_trigger = {
184 .name = "oneshot",
185 .activate = oneshot_trig_activate,
186 .deactivate = oneshot_trig_deactivate,
189 static int __init oneshot_trig_init(void)
191 return led_trigger_register(&oneshot_led_trigger);
194 static void __exit oneshot_trig_exit(void)
196 led_trigger_unregister(&oneshot_led_trigger);
199 module_init(oneshot_trig_init);
200 module_exit(oneshot_trig_exit);
202 MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>");
203 MODULE_DESCRIPTION("One-shot LED trigger");
204 MODULE_LICENSE("GPL");