blob: 93d6b82e64372a55cd0eb065b93cdcab20588adb [file] [log] [blame]
Felipe Balbi17354bf2009-02-17 13:18:11 +02001/*
2 * ledtrig-gio.c - LED Trigger Based on GPIO events
3 *
4 * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/gpio.h>
16#include <linux/interrupt.h>
17#include <linux/workqueue.h>
18#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Kim, Milof07fb522013-02-20 00:36:01 -080020#include "../leds.h"
Felipe Balbi17354bf2009-02-17 13:18:11 +020021
22struct gpio_trig_data {
23 struct led_classdev *led;
24 struct work_struct work;
25
26 unsigned desired_brightness; /* desired brightness when led is on */
27 unsigned inverted; /* true when gpio is inverted */
28 unsigned gpio; /* gpio that triggers the leds */
29};
30
31static irqreturn_t gpio_trig_irq(int irq, void *_led)
32{
33 struct led_classdev *led = _led;
34 struct gpio_trig_data *gpio_data = led->trigger_data;
35
36 /* just schedule_work since gpio_get_value can sleep */
37 schedule_work(&gpio_data->work);
38
39 return IRQ_HANDLED;
40};
41
42static void gpio_trig_work(struct work_struct *work)
43{
44 struct gpio_trig_data *gpio_data = container_of(work,
45 struct gpio_trig_data, work);
46 int tmp;
47
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070048 if (!gpio_data->gpio)
49 return;
Felipe Balbi17354bf2009-02-17 13:18:11 +020050
Lothar Waßmann914ae252014-09-09 00:40:32 -070051 tmp = gpio_get_value_cansleep(gpio_data->gpio);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070052 if (gpio_data->inverted)
53 tmp = !tmp;
Felipe Balbi17354bf2009-02-17 13:18:11 +020054
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070055 if (tmp) {
56 if (gpio_data->desired_brightness)
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020057 led_set_brightness_nosleep(gpio_data->led,
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070058 gpio_data->desired_brightness);
59 else
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020060 led_set_brightness_nosleep(gpio_data->led, LED_FULL);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070061 } else {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020062 led_set_brightness_nosleep(gpio_data->led, LED_OFF);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070063 }
Felipe Balbi17354bf2009-02-17 13:18:11 +020064}
65
66static ssize_t gpio_trig_brightness_show(struct device *dev,
67 struct device_attribute *attr, char *buf)
68{
69 struct led_classdev *led = dev_get_drvdata(dev);
70 struct gpio_trig_data *gpio_data = led->trigger_data;
71
72 return sprintf(buf, "%u\n", gpio_data->desired_brightness);
73}
74
75static ssize_t gpio_trig_brightness_store(struct device *dev,
76 struct device_attribute *attr, const char *buf, size_t n)
77{
78 struct led_classdev *led = dev_get_drvdata(dev);
79 struct gpio_trig_data *gpio_data = led->trigger_data;
80 unsigned desired_brightness;
81 int ret;
82
83 ret = sscanf(buf, "%u", &desired_brightness);
84 if (ret < 1 || desired_brightness > 255) {
85 dev_err(dev, "invalid value\n");
86 return -EINVAL;
87 }
88
89 gpio_data->desired_brightness = desired_brightness;
90
91 return n;
92}
93static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
94 gpio_trig_brightness_store);
95
96static ssize_t gpio_trig_inverted_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
98{
99 struct led_classdev *led = dev_get_drvdata(dev);
100 struct gpio_trig_data *gpio_data = led->trigger_data;
101
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -0800102 return sprintf(buf, "%u\n", gpio_data->inverted);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200103}
104
105static ssize_t gpio_trig_inverted_store(struct device *dev,
106 struct device_attribute *attr, const char *buf, size_t n)
107{
108 struct led_classdev *led = dev_get_drvdata(dev);
109 struct gpio_trig_data *gpio_data = led->trigger_data;
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -0800110 unsigned long inverted;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200111 int ret;
112
Jingoo Hane8941922012-10-23 05:27:52 -0700113 ret = kstrtoul(buf, 10, &inverted);
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -0800114 if (ret < 0)
115 return ret;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200116
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -0800117 if (inverted > 1)
118 return -EINVAL;
119
120 gpio_data->inverted = inverted;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200121
Thadeu Lima de Souza Cascardocc674c82009-08-26 14:29:32 -0700122 /* After inverting, we need to update the LED. */
123 schedule_work(&gpio_data->work);
124
Felipe Balbi17354bf2009-02-17 13:18:11 +0200125 return n;
126}
127static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
128 gpio_trig_inverted_store);
129
130static ssize_t gpio_trig_gpio_show(struct device *dev,
131 struct device_attribute *attr, char *buf)
132{
133 struct led_classdev *led = dev_get_drvdata(dev);
134 struct gpio_trig_data *gpio_data = led->trigger_data;
135
136 return sprintf(buf, "%u\n", gpio_data->gpio);
137}
138
139static ssize_t gpio_trig_gpio_store(struct device *dev,
140 struct device_attribute *attr, const char *buf, size_t n)
141{
142 struct led_classdev *led = dev_get_drvdata(dev);
143 struct gpio_trig_data *gpio_data = led->trigger_data;
144 unsigned gpio;
145 int ret;
146
147 ret = sscanf(buf, "%u", &gpio);
148 if (ret < 1) {
149 dev_err(dev, "couldn't read gpio number\n");
150 flush_work(&gpio_data->work);
151 return -EINVAL;
152 }
153
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700154 if (gpio_data->gpio == gpio)
155 return n;
156
Felipe Balbi17354bf2009-02-17 13:18:11 +0200157 if (!gpio) {
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700158 if (gpio_data->gpio != 0)
159 free_irq(gpio_to_irq(gpio_data->gpio), led);
160 gpio_data->gpio = 0;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200161 return n;
162 }
163
Felipe Balbi17354bf2009-02-17 13:18:11 +0200164 ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq,
165 IRQF_SHARED | IRQF_TRIGGER_RISING
166 | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700167 if (ret) {
Felipe Balbi17354bf2009-02-17 13:18:11 +0200168 dev_err(dev, "request_irq failed with error %d\n", ret);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700169 } else {
170 if (gpio_data->gpio != 0)
171 free_irq(gpio_to_irq(gpio_data->gpio), led);
172 gpio_data->gpio = gpio;
Jan Kiszka71afe3c2017-05-26 15:17:46 +0200173 /* After changing the GPIO, we need to update the LED. */
174 schedule_work(&gpio_data->work);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700175 }
Felipe Balbi17354bf2009-02-17 13:18:11 +0200176
177 return ret ? ret : n;
178}
179static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
180
181static void gpio_trig_activate(struct led_classdev *led)
182{
183 struct gpio_trig_data *gpio_data;
184 int ret;
185
186 gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
187 if (!gpio_data)
188 return;
189
190 ret = device_create_file(led->dev, &dev_attr_gpio);
191 if (ret)
192 goto err_gpio;
193
194 ret = device_create_file(led->dev, &dev_attr_inverted);
195 if (ret)
196 goto err_inverted;
197
198 ret = device_create_file(led->dev, &dev_attr_desired_brightness);
199 if (ret)
200 goto err_brightness;
201
202 gpio_data->led = led;
203 led->trigger_data = gpio_data;
204 INIT_WORK(&gpio_data->work, gpio_trig_work);
Shuah Khan03c091e2012-05-29 15:07:28 -0700205 led->activated = true;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200206
207 return;
208
209err_brightness:
210 device_remove_file(led->dev, &dev_attr_inverted);
211
212err_inverted:
213 device_remove_file(led->dev, &dev_attr_gpio);
214
215err_gpio:
216 kfree(gpio_data);
217}
218
219static void gpio_trig_deactivate(struct led_classdev *led)
220{
221 struct gpio_trig_data *gpio_data = led->trigger_data;
222
Shuah Khan03c091e2012-05-29 15:07:28 -0700223 if (led->activated) {
Felipe Balbi17354bf2009-02-17 13:18:11 +0200224 device_remove_file(led->dev, &dev_attr_gpio);
225 device_remove_file(led->dev, &dev_attr_inverted);
226 device_remove_file(led->dev, &dev_attr_desired_brightness);
227 flush_work(&gpio_data->work);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700228 if (gpio_data->gpio != 0)
229 free_irq(gpio_to_irq(gpio_data->gpio), led);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200230 kfree(gpio_data);
Shuah Khan03c091e2012-05-29 15:07:28 -0700231 led->activated = false;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200232 }
233}
234
235static struct led_trigger gpio_led_trigger = {
236 .name = "gpio",
237 .activate = gpio_trig_activate,
238 .deactivate = gpio_trig_deactivate,
239};
240
241static int __init gpio_trig_init(void)
242{
243 return led_trigger_register(&gpio_led_trigger);
244}
245module_init(gpio_trig_init);
246
247static void __exit gpio_trig_exit(void)
248{
249 led_trigger_unregister(&gpio_led_trigger);
250}
251module_exit(gpio_trig_exit);
252
253MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
254MODULE_DESCRIPTION("GPIO LED trigger");
255MODULE_LICENSE("GPL");