blob: 991d93be0f447313945b6925a0a196c19a9586e2 [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>
Felipe Balbi17354bf2009-02-17 13:18:11 +020020#include "leds.h"
21
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
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070051 tmp = gpio_get_value(gpio_data->gpio);
52 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)
57 led_set_brightness(gpio_data->led,
58 gpio_data->desired_brightness);
59 else
60 led_set_brightness(gpio_data->led, LED_FULL);
61 } else {
62 led_set_brightness(gpio_data->led, LED_OFF);
63 }
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
102 return sprintf(buf, "%s\n", gpio_data->inverted ? "yes" : "no");
103}
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;
110 unsigned inverted;
111 int ret;
112
113 ret = sscanf(buf, "%u", &inverted);
114 if (ret < 1) {
115 dev_err(dev, "invalid value\n");
116 return -EINVAL;
117 }
118
119 gpio_data->inverted = !!inverted;
120
Thadeu Lima de Souza Cascardocc674c82009-08-26 14:29:32 -0700121 /* After inverting, we need to update the LED. */
122 schedule_work(&gpio_data->work);
123
Felipe Balbi17354bf2009-02-17 13:18:11 +0200124 return n;
125}
126static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
127 gpio_trig_inverted_store);
128
129static ssize_t gpio_trig_gpio_show(struct device *dev,
130 struct device_attribute *attr, char *buf)
131{
132 struct led_classdev *led = dev_get_drvdata(dev);
133 struct gpio_trig_data *gpio_data = led->trigger_data;
134
135 return sprintf(buf, "%u\n", gpio_data->gpio);
136}
137
138static ssize_t gpio_trig_gpio_store(struct device *dev,
139 struct device_attribute *attr, const char *buf, size_t n)
140{
141 struct led_classdev *led = dev_get_drvdata(dev);
142 struct gpio_trig_data *gpio_data = led->trigger_data;
143 unsigned gpio;
144 int ret;
145
146 ret = sscanf(buf, "%u", &gpio);
147 if (ret < 1) {
148 dev_err(dev, "couldn't read gpio number\n");
149 flush_work(&gpio_data->work);
150 return -EINVAL;
151 }
152
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700153 if (gpio_data->gpio == gpio)
154 return n;
155
Felipe Balbi17354bf2009-02-17 13:18:11 +0200156 if (!gpio) {
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700157 if (gpio_data->gpio != 0)
158 free_irq(gpio_to_irq(gpio_data->gpio), led);
159 gpio_data->gpio = 0;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200160 return n;
161 }
162
Felipe Balbi17354bf2009-02-17 13:18:11 +0200163 ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq,
164 IRQF_SHARED | IRQF_TRIGGER_RISING
165 | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700166 if (ret) {
Felipe Balbi17354bf2009-02-17 13:18:11 +0200167 dev_err(dev, "request_irq failed with error %d\n", ret);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700168 } else {
169 if (gpio_data->gpio != 0)
170 free_irq(gpio_to_irq(gpio_data->gpio), led);
171 gpio_data->gpio = gpio;
172 }
Felipe Balbi17354bf2009-02-17 13:18:11 +0200173
174 return ret ? ret : n;
175}
176static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
177
178static void gpio_trig_activate(struct led_classdev *led)
179{
180 struct gpio_trig_data *gpio_data;
181 int ret;
182
183 gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
184 if (!gpio_data)
185 return;
186
187 ret = device_create_file(led->dev, &dev_attr_gpio);
188 if (ret)
189 goto err_gpio;
190
191 ret = device_create_file(led->dev, &dev_attr_inverted);
192 if (ret)
193 goto err_inverted;
194
195 ret = device_create_file(led->dev, &dev_attr_desired_brightness);
196 if (ret)
197 goto err_brightness;
198
199 gpio_data->led = led;
200 led->trigger_data = gpio_data;
201 INIT_WORK(&gpio_data->work, gpio_trig_work);
202
203 return;
204
205err_brightness:
206 device_remove_file(led->dev, &dev_attr_inverted);
207
208err_inverted:
209 device_remove_file(led->dev, &dev_attr_gpio);
210
211err_gpio:
212 kfree(gpio_data);
213}
214
215static void gpio_trig_deactivate(struct led_classdev *led)
216{
217 struct gpio_trig_data *gpio_data = led->trigger_data;
218
219 if (gpio_data) {
220 device_remove_file(led->dev, &dev_attr_gpio);
221 device_remove_file(led->dev, &dev_attr_inverted);
222 device_remove_file(led->dev, &dev_attr_desired_brightness);
223 flush_work(&gpio_data->work);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700224 if (gpio_data->gpio != 0)
225 free_irq(gpio_to_irq(gpio_data->gpio), led);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200226 kfree(gpio_data);
227 }
228}
229
230static struct led_trigger gpio_led_trigger = {
231 .name = "gpio",
232 .activate = gpio_trig_activate,
233 .deactivate = gpio_trig_deactivate,
234};
235
236static int __init gpio_trig_init(void)
237{
238 return led_trigger_register(&gpio_led_trigger);
239}
240module_init(gpio_trig_init);
241
242static void __exit gpio_trig_exit(void)
243{
244 led_trigger_unregister(&gpio_led_trigger);
245}
246module_exit(gpio_trig_exit);
247
248MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
249MODULE_DESCRIPTION("GPIO LED trigger");
250MODULE_LICENSE("GPL");