blob: 8891e88d54dd0cddda8833eb12012c00ce45fda6 [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>
Felipe Balbi17354bf2009-02-17 13:18:11 +020017#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Kim, Milof07fb522013-02-20 00:36:01 -080019#include "../leds.h"
Felipe Balbi17354bf2009-02-17 13:18:11 +020020
21struct gpio_trig_data {
22 struct led_classdev *led;
Felipe Balbi17354bf2009-02-17 13:18:11 +020023
24 unsigned desired_brightness; /* desired brightness when led is on */
25 unsigned inverted; /* true when gpio is inverted */
26 unsigned gpio; /* gpio that triggers the leds */
27};
28
29static irqreturn_t gpio_trig_irq(int irq, void *_led)
30{
31 struct led_classdev *led = _led;
32 struct gpio_trig_data *gpio_data = led->trigger_data;
Felipe Balbi17354bf2009-02-17 13:18:11 +020033 int tmp;
34
Lothar Waßmann914ae252014-09-09 00:40:32 -070035 tmp = gpio_get_value_cansleep(gpio_data->gpio);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070036 if (gpio_data->inverted)
37 tmp = !tmp;
Felipe Balbi17354bf2009-02-17 13:18:11 +020038
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070039 if (tmp) {
40 if (gpio_data->desired_brightness)
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020041 led_set_brightness_nosleep(gpio_data->led,
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070042 gpio_data->desired_brightness);
43 else
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020044 led_set_brightness_nosleep(gpio_data->led, LED_FULL);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070045 } else {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020046 led_set_brightness_nosleep(gpio_data->led, LED_OFF);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070047 }
Jan Kiszka71c17b02017-05-26 15:17:47 +020048
49 return IRQ_HANDLED;
Felipe Balbi17354bf2009-02-17 13:18:11 +020050}
51
52static ssize_t gpio_trig_brightness_show(struct device *dev,
53 struct device_attribute *attr, char *buf)
54{
55 struct led_classdev *led = dev_get_drvdata(dev);
56 struct gpio_trig_data *gpio_data = led->trigger_data;
57
58 return sprintf(buf, "%u\n", gpio_data->desired_brightness);
59}
60
61static ssize_t gpio_trig_brightness_store(struct device *dev,
62 struct device_attribute *attr, const char *buf, size_t n)
63{
64 struct led_classdev *led = dev_get_drvdata(dev);
65 struct gpio_trig_data *gpio_data = led->trigger_data;
66 unsigned desired_brightness;
67 int ret;
68
69 ret = sscanf(buf, "%u", &desired_brightness);
70 if (ret < 1 || desired_brightness > 255) {
71 dev_err(dev, "invalid value\n");
72 return -EINVAL;
73 }
74
75 gpio_data->desired_brightness = desired_brightness;
76
77 return n;
78}
79static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
80 gpio_trig_brightness_store);
81
82static ssize_t gpio_trig_inverted_show(struct device *dev,
83 struct device_attribute *attr, char *buf)
84{
85 struct led_classdev *led = dev_get_drvdata(dev);
86 struct gpio_trig_data *gpio_data = led->trigger_data;
87
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080088 return sprintf(buf, "%u\n", gpio_data->inverted);
Felipe Balbi17354bf2009-02-17 13:18:11 +020089}
90
91static ssize_t gpio_trig_inverted_store(struct device *dev,
92 struct device_attribute *attr, const char *buf, size_t n)
93{
94 struct led_classdev *led = dev_get_drvdata(dev);
95 struct gpio_trig_data *gpio_data = led->trigger_data;
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080096 unsigned long inverted;
Felipe Balbi17354bf2009-02-17 13:18:11 +020097 int ret;
98
Jingoo Hane8941922012-10-23 05:27:52 -070099 ret = kstrtoul(buf, 10, &inverted);
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -0800100 if (ret < 0)
101 return ret;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200102
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -0800103 if (inverted > 1)
104 return -EINVAL;
105
106 gpio_data->inverted = inverted;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200107
Thadeu Lima de Souza Cascardocc674c82009-08-26 14:29:32 -0700108 /* After inverting, we need to update the LED. */
Jan Kiszka71c17b02017-05-26 15:17:47 +0200109 gpio_trig_irq(0, led);
Thadeu Lima de Souza Cascardocc674c82009-08-26 14:29:32 -0700110
Felipe Balbi17354bf2009-02-17 13:18:11 +0200111 return n;
112}
113static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
114 gpio_trig_inverted_store);
115
116static ssize_t gpio_trig_gpio_show(struct device *dev,
117 struct device_attribute *attr, char *buf)
118{
119 struct led_classdev *led = dev_get_drvdata(dev);
120 struct gpio_trig_data *gpio_data = led->trigger_data;
121
122 return sprintf(buf, "%u\n", gpio_data->gpio);
123}
124
125static ssize_t gpio_trig_gpio_store(struct device *dev,
126 struct device_attribute *attr, const char *buf, size_t n)
127{
128 struct led_classdev *led = dev_get_drvdata(dev);
129 struct gpio_trig_data *gpio_data = led->trigger_data;
130 unsigned gpio;
131 int ret;
132
133 ret = sscanf(buf, "%u", &gpio);
134 if (ret < 1) {
135 dev_err(dev, "couldn't read gpio number\n");
Felipe Balbi17354bf2009-02-17 13:18:11 +0200136 return -EINVAL;
137 }
138
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700139 if (gpio_data->gpio == gpio)
140 return n;
141
Felipe Balbi17354bf2009-02-17 13:18:11 +0200142 if (!gpio) {
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700143 if (gpio_data->gpio != 0)
144 free_irq(gpio_to_irq(gpio_data->gpio), led);
145 gpio_data->gpio = 0;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200146 return n;
147 }
148
Jan Kiszka71c17b02017-05-26 15:17:47 +0200149 ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
150 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
Felipe Balbi17354bf2009-02-17 13:18:11 +0200151 | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700152 if (ret) {
Felipe Balbi17354bf2009-02-17 13:18:11 +0200153 dev_err(dev, "request_irq failed with error %d\n", ret);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700154 } else {
155 if (gpio_data->gpio != 0)
156 free_irq(gpio_to_irq(gpio_data->gpio), led);
157 gpio_data->gpio = gpio;
Jan Kiszka71afe3c2017-05-26 15:17:46 +0200158 /* After changing the GPIO, we need to update the LED. */
Jan Kiszka71c17b02017-05-26 15:17:47 +0200159 gpio_trig_irq(0, led);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700160 }
Felipe Balbi17354bf2009-02-17 13:18:11 +0200161
162 return ret ? ret : n;
163}
164static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
165
166static void gpio_trig_activate(struct led_classdev *led)
167{
168 struct gpio_trig_data *gpio_data;
169 int ret;
170
171 gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
172 if (!gpio_data)
173 return;
174
175 ret = device_create_file(led->dev, &dev_attr_gpio);
176 if (ret)
177 goto err_gpio;
178
179 ret = device_create_file(led->dev, &dev_attr_inverted);
180 if (ret)
181 goto err_inverted;
182
183 ret = device_create_file(led->dev, &dev_attr_desired_brightness);
184 if (ret)
185 goto err_brightness;
186
187 gpio_data->led = led;
188 led->trigger_data = gpio_data;
Shuah Khan03c091e2012-05-29 15:07:28 -0700189 led->activated = true;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200190
191 return;
192
193err_brightness:
194 device_remove_file(led->dev, &dev_attr_inverted);
195
196err_inverted:
197 device_remove_file(led->dev, &dev_attr_gpio);
198
199err_gpio:
200 kfree(gpio_data);
201}
202
203static void gpio_trig_deactivate(struct led_classdev *led)
204{
205 struct gpio_trig_data *gpio_data = led->trigger_data;
206
Shuah Khan03c091e2012-05-29 15:07:28 -0700207 if (led->activated) {
Felipe Balbi17354bf2009-02-17 13:18:11 +0200208 device_remove_file(led->dev, &dev_attr_gpio);
209 device_remove_file(led->dev, &dev_attr_inverted);
210 device_remove_file(led->dev, &dev_attr_desired_brightness);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700211 if (gpio_data->gpio != 0)
212 free_irq(gpio_to_irq(gpio_data->gpio), led);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200213 kfree(gpio_data);
Shuah Khan03c091e2012-05-29 15:07:28 -0700214 led->activated = false;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200215 }
216}
217
218static struct led_trigger gpio_led_trigger = {
219 .name = "gpio",
220 .activate = gpio_trig_activate,
221 .deactivate = gpio_trig_deactivate,
222};
223
224static int __init gpio_trig_init(void)
225{
226 return led_trigger_register(&gpio_led_trigger);
227}
228module_init(gpio_trig_init);
229
230static void __exit gpio_trig_exit(void)
231{
232 led_trigger_unregister(&gpio_led_trigger);
233}
234module_exit(gpio_trig_exit);
235
236MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
237MODULE_DESCRIPTION("GPIO LED trigger");
238MODULE_LICENSE("GPL");