Linux 4.16.11
[linux/fpc-iii.git] / drivers / leds / trigger / ledtrig-transient.c
blob9d1769073562fdfd480d7ba51d9e861d3e439476
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // LED Kernel Transient Trigger
4 //
5 // Transient trigger allows one shot timer activation. Please refer to
6 // Documentation/leds/ledtrig-transient.txt for details
7 // Copyright (C) 2012 Shuah Khan <shuahkhan@gmail.com>
8 //
9 // Based on Richard Purdie's ledtrig-timer.c and Atsushi Nemoto's
10 // ledtrig-heartbeat.c
11 // Design and use-case input from Jonas Bonn <jonas@southpole.se> and
12 // Neil Brown <neilb@suse.de>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/slab.h>
19 #include <linux/timer.h>
20 #include <linux/leds.h>
21 #include "../leds.h"
23 struct transient_trig_data {
24 int activate;
25 int state;
26 int restore_state;
27 unsigned long duration;
28 struct timer_list timer;
29 struct led_classdev *led_cdev;
32 static void transient_timer_function(struct timer_list *t)
34 struct transient_trig_data *transient_data =
35 from_timer(transient_data, t, timer);
36 struct led_classdev *led_cdev = transient_data->led_cdev;
38 transient_data->activate = 0;
39 led_set_brightness_nosleep(led_cdev, transient_data->restore_state);
42 static ssize_t transient_activate_show(struct device *dev,
43 struct device_attribute *attr, char *buf)
45 struct led_classdev *led_cdev = dev_get_drvdata(dev);
46 struct transient_trig_data *transient_data = led_cdev->trigger_data;
48 return sprintf(buf, "%d\n", transient_data->activate);
51 static ssize_t transient_activate_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 transient_trig_data *transient_data = led_cdev->trigger_data;
56 unsigned long state;
57 ssize_t ret;
59 ret = kstrtoul(buf, 10, &state);
60 if (ret)
61 return ret;
63 if (state != 1 && state != 0)
64 return -EINVAL;
66 /* cancel the running timer */
67 if (state == 0 && transient_data->activate == 1) {
68 del_timer(&transient_data->timer);
69 transient_data->activate = state;
70 led_set_brightness_nosleep(led_cdev,
71 transient_data->restore_state);
72 return size;
75 /* start timer if there is no active timer */
76 if (state == 1 && transient_data->activate == 0 &&
77 transient_data->duration != 0) {
78 transient_data->activate = state;
79 led_set_brightness_nosleep(led_cdev, transient_data->state);
80 transient_data->restore_state =
81 (transient_data->state == LED_FULL) ? LED_OFF : LED_FULL;
82 mod_timer(&transient_data->timer,
83 jiffies + msecs_to_jiffies(transient_data->duration));
86 /* state == 0 && transient_data->activate == 0
87 timer is not active - just return */
88 /* state == 1 && transient_data->activate == 1
89 timer is already active - just return */
91 return size;
94 static ssize_t transient_duration_show(struct device *dev,
95 struct device_attribute *attr, char *buf)
97 struct led_classdev *led_cdev = dev_get_drvdata(dev);
98 struct transient_trig_data *transient_data = led_cdev->trigger_data;
100 return sprintf(buf, "%lu\n", transient_data->duration);
103 static ssize_t transient_duration_store(struct device *dev,
104 struct device_attribute *attr, const char *buf, size_t size)
106 struct led_classdev *led_cdev = dev_get_drvdata(dev);
107 struct transient_trig_data *transient_data = led_cdev->trigger_data;
108 unsigned long state;
109 ssize_t ret;
111 ret = kstrtoul(buf, 10, &state);
112 if (ret)
113 return ret;
115 transient_data->duration = state;
116 return size;
119 static ssize_t transient_state_show(struct device *dev,
120 struct device_attribute *attr, char *buf)
122 struct led_classdev *led_cdev = dev_get_drvdata(dev);
123 struct transient_trig_data *transient_data = led_cdev->trigger_data;
124 int state;
126 state = (transient_data->state == LED_FULL) ? 1 : 0;
127 return sprintf(buf, "%d\n", state);
130 static ssize_t transient_state_store(struct device *dev,
131 struct device_attribute *attr, const char *buf, size_t size)
133 struct led_classdev *led_cdev = dev_get_drvdata(dev);
134 struct transient_trig_data *transient_data = led_cdev->trigger_data;
135 unsigned long state;
136 ssize_t ret;
138 ret = kstrtoul(buf, 10, &state);
139 if (ret)
140 return ret;
142 if (state != 1 && state != 0)
143 return -EINVAL;
145 transient_data->state = (state == 1) ? LED_FULL : LED_OFF;
146 return size;
149 static DEVICE_ATTR(activate, 0644, transient_activate_show,
150 transient_activate_store);
151 static DEVICE_ATTR(duration, 0644, transient_duration_show,
152 transient_duration_store);
153 static DEVICE_ATTR(state, 0644, transient_state_show, transient_state_store);
155 static void transient_trig_activate(struct led_classdev *led_cdev)
157 int rc;
158 struct transient_trig_data *tdata;
160 tdata = kzalloc(sizeof(struct transient_trig_data), GFP_KERNEL);
161 if (!tdata) {
162 dev_err(led_cdev->dev,
163 "unable to allocate transient trigger\n");
164 return;
166 led_cdev->trigger_data = tdata;
167 tdata->led_cdev = led_cdev;
169 rc = device_create_file(led_cdev->dev, &dev_attr_activate);
170 if (rc)
171 goto err_out;
173 rc = device_create_file(led_cdev->dev, &dev_attr_duration);
174 if (rc)
175 goto err_out_duration;
177 rc = device_create_file(led_cdev->dev, &dev_attr_state);
178 if (rc)
179 goto err_out_state;
181 timer_setup(&tdata->timer, transient_timer_function, 0);
182 led_cdev->activated = true;
184 return;
186 err_out_state:
187 device_remove_file(led_cdev->dev, &dev_attr_duration);
188 err_out_duration:
189 device_remove_file(led_cdev->dev, &dev_attr_activate);
190 err_out:
191 dev_err(led_cdev->dev, "unable to register transient trigger\n");
192 led_cdev->trigger_data = NULL;
193 kfree(tdata);
196 static void transient_trig_deactivate(struct led_classdev *led_cdev)
198 struct transient_trig_data *transient_data = led_cdev->trigger_data;
200 if (led_cdev->activated) {
201 del_timer_sync(&transient_data->timer);
202 led_set_brightness_nosleep(led_cdev,
203 transient_data->restore_state);
204 device_remove_file(led_cdev->dev, &dev_attr_activate);
205 device_remove_file(led_cdev->dev, &dev_attr_duration);
206 device_remove_file(led_cdev->dev, &dev_attr_state);
207 led_cdev->trigger_data = NULL;
208 led_cdev->activated = false;
209 kfree(transient_data);
213 static struct led_trigger transient_trigger = {
214 .name = "transient",
215 .activate = transient_trig_activate,
216 .deactivate = transient_trig_deactivate,
219 static int __init transient_trig_init(void)
221 return led_trigger_register(&transient_trigger);
224 static void __exit transient_trig_exit(void)
226 led_trigger_unregister(&transient_trigger);
229 module_init(transient_trig_init);
230 module_exit(transient_trig_exit);
232 MODULE_AUTHOR("Shuah Khan <shuahkhan@gmail.com>");
233 MODULE_DESCRIPTION("Transient LED trigger");
234 MODULE_LICENSE("GPL v2");