2 * ledtrig-gio.c - LED Trigger Based on GPIO events
4 * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
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.
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>
21 struct gpio_trig_data
{
22 struct led_classdev
*led
;
23 struct work_struct work
;
25 unsigned desired_brightness
; /* desired brightness when led is on */
26 unsigned inverted
; /* true when gpio is inverted */
27 unsigned gpio
; /* gpio that triggers the leds */
30 static irqreturn_t
gpio_trig_irq(int irq
, void *_led
)
32 struct led_classdev
*led
= _led
;
33 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
35 /* just schedule_work since gpio_get_value can sleep */
36 schedule_work(&gpio_data
->work
);
41 static void gpio_trig_work(struct work_struct
*work
)
43 struct gpio_trig_data
*gpio_data
= container_of(work
,
44 struct gpio_trig_data
, work
);
50 tmp
= gpio_get_value(gpio_data
->gpio
);
51 if (gpio_data
->inverted
)
55 if (gpio_data
->desired_brightness
)
56 led_set_brightness(gpio_data
->led
,
57 gpio_data
->desired_brightness
);
59 led_set_brightness(gpio_data
->led
, LED_FULL
);
61 led_set_brightness(gpio_data
->led
, LED_OFF
);
65 static ssize_t
gpio_trig_brightness_show(struct device
*dev
,
66 struct device_attribute
*attr
, char *buf
)
68 struct led_classdev
*led
= dev_get_drvdata(dev
);
69 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
71 return sprintf(buf
, "%u\n", gpio_data
->desired_brightness
);
74 static ssize_t
gpio_trig_brightness_store(struct device
*dev
,
75 struct device_attribute
*attr
, const char *buf
, size_t n
)
77 struct led_classdev
*led
= dev_get_drvdata(dev
);
78 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
79 unsigned desired_brightness
;
82 ret
= sscanf(buf
, "%u", &desired_brightness
);
83 if (ret
< 1 || desired_brightness
> 255) {
84 dev_err(dev
, "invalid value\n");
88 gpio_data
->desired_brightness
= desired_brightness
;
92 static DEVICE_ATTR(desired_brightness
, 0644, gpio_trig_brightness_show
,
93 gpio_trig_brightness_store
);
95 static ssize_t
gpio_trig_inverted_show(struct device
*dev
,
96 struct device_attribute
*attr
, char *buf
)
98 struct led_classdev
*led
= dev_get_drvdata(dev
);
99 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
101 return sprintf(buf
, "%s\n", gpio_data
->inverted
? "yes" : "no");
104 static ssize_t
gpio_trig_inverted_store(struct device
*dev
,
105 struct device_attribute
*attr
, const char *buf
, size_t n
)
107 struct led_classdev
*led
= dev_get_drvdata(dev
);
108 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
112 ret
= sscanf(buf
, "%u", &inverted
);
114 dev_err(dev
, "invalid value\n");
118 gpio_data
->inverted
= !!inverted
;
120 /* After inverting, we need to update the LED. */
121 schedule_work(&gpio_data
->work
);
125 static DEVICE_ATTR(inverted
, 0644, gpio_trig_inverted_show
,
126 gpio_trig_inverted_store
);
128 static ssize_t
gpio_trig_gpio_show(struct device
*dev
,
129 struct device_attribute
*attr
, char *buf
)
131 struct led_classdev
*led
= dev_get_drvdata(dev
);
132 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
134 return sprintf(buf
, "%u\n", gpio_data
->gpio
);
137 static ssize_t
gpio_trig_gpio_store(struct device
*dev
,
138 struct device_attribute
*attr
, const char *buf
, size_t n
)
140 struct led_classdev
*led
= dev_get_drvdata(dev
);
141 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
145 ret
= sscanf(buf
, "%u", &gpio
);
147 dev_err(dev
, "couldn't read gpio number\n");
148 flush_work(&gpio_data
->work
);
152 if (gpio_data
->gpio
== gpio
)
156 if (gpio_data
->gpio
!= 0)
157 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
162 ret
= request_irq(gpio_to_irq(gpio
), gpio_trig_irq
,
163 IRQF_SHARED
| IRQF_TRIGGER_RISING
164 | IRQF_TRIGGER_FALLING
, "ledtrig-gpio", led
);
166 dev_err(dev
, "request_irq failed with error %d\n", ret
);
168 if (gpio_data
->gpio
!= 0)
169 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
170 gpio_data
->gpio
= gpio
;
173 return ret
? ret
: n
;
175 static DEVICE_ATTR(gpio
, 0644, gpio_trig_gpio_show
, gpio_trig_gpio_store
);
177 static void gpio_trig_activate(struct led_classdev
*led
)
179 struct gpio_trig_data
*gpio_data
;
182 gpio_data
= kzalloc(sizeof(*gpio_data
), GFP_KERNEL
);
186 ret
= device_create_file(led
->dev
, &dev_attr_gpio
);
190 ret
= device_create_file(led
->dev
, &dev_attr_inverted
);
194 ret
= device_create_file(led
->dev
, &dev_attr_desired_brightness
);
198 gpio_data
->led
= led
;
199 led
->trigger_data
= gpio_data
;
200 INIT_WORK(&gpio_data
->work
, gpio_trig_work
);
205 device_remove_file(led
->dev
, &dev_attr_inverted
);
208 device_remove_file(led
->dev
, &dev_attr_gpio
);
214 static void gpio_trig_deactivate(struct led_classdev
*led
)
216 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
219 device_remove_file(led
->dev
, &dev_attr_gpio
);
220 device_remove_file(led
->dev
, &dev_attr_inverted
);
221 device_remove_file(led
->dev
, &dev_attr_desired_brightness
);
222 flush_work(&gpio_data
->work
);
223 if (gpio_data
->gpio
!= 0)
224 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
229 static struct led_trigger gpio_led_trigger
= {
231 .activate
= gpio_trig_activate
,
232 .deactivate
= gpio_trig_deactivate
,
235 static int __init
gpio_trig_init(void)
237 return led_trigger_register(&gpio_led_trigger
);
239 module_init(gpio_trig_init
);
241 static void __exit
gpio_trig_exit(void)
243 led_trigger_unregister(&gpio_led_trigger
);
245 module_exit(gpio_trig_exit
);
247 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
248 MODULE_DESCRIPTION("GPIO LED trigger");
249 MODULE_LICENSE("GPL");