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>
19 #include <linux/slab.h>
22 struct gpio_trig_data
{
23 struct led_classdev
*led
;
24 struct work_struct work
;
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 */
31 static irqreturn_t
gpio_trig_irq(int irq
, void *_led
)
33 struct led_classdev
*led
= _led
;
34 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
36 /* just schedule_work since gpio_get_value can sleep */
37 schedule_work(&gpio_data
->work
);
42 static void gpio_trig_work(struct work_struct
*work
)
44 struct gpio_trig_data
*gpio_data
= container_of(work
,
45 struct gpio_trig_data
, work
);
51 tmp
= gpio_get_value(gpio_data
->gpio
);
52 if (gpio_data
->inverted
)
56 if (gpio_data
->desired_brightness
)
57 led_set_brightness(gpio_data
->led
,
58 gpio_data
->desired_brightness
);
60 led_set_brightness(gpio_data
->led
, LED_FULL
);
62 led_set_brightness(gpio_data
->led
, LED_OFF
);
66 static ssize_t
gpio_trig_brightness_show(struct device
*dev
,
67 struct device_attribute
*attr
, char *buf
)
69 struct led_classdev
*led
= dev_get_drvdata(dev
);
70 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
72 return sprintf(buf
, "%u\n", gpio_data
->desired_brightness
);
75 static ssize_t
gpio_trig_brightness_store(struct device
*dev
,
76 struct device_attribute
*attr
, const char *buf
, size_t n
)
78 struct led_classdev
*led
= dev_get_drvdata(dev
);
79 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
80 unsigned desired_brightness
;
83 ret
= sscanf(buf
, "%u", &desired_brightness
);
84 if (ret
< 1 || desired_brightness
> 255) {
85 dev_err(dev
, "invalid value\n");
89 gpio_data
->desired_brightness
= desired_brightness
;
93 static DEVICE_ATTR(desired_brightness
, 0644, gpio_trig_brightness_show
,
94 gpio_trig_brightness_store
);
96 static ssize_t
gpio_trig_inverted_show(struct device
*dev
,
97 struct device_attribute
*attr
, char *buf
)
99 struct led_classdev
*led
= dev_get_drvdata(dev
);
100 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
102 return sprintf(buf
, "%u\n", gpio_data
->inverted
);
105 static ssize_t
gpio_trig_inverted_store(struct device
*dev
,
106 struct device_attribute
*attr
, const char *buf
, size_t n
)
108 struct led_classdev
*led
= dev_get_drvdata(dev
);
109 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
110 unsigned long inverted
;
113 ret
= strict_strtoul(buf
, 10, &inverted
);
120 gpio_data
->inverted
= inverted
;
122 /* After inverting, we need to update the LED. */
123 schedule_work(&gpio_data
->work
);
127 static DEVICE_ATTR(inverted
, 0644, gpio_trig_inverted_show
,
128 gpio_trig_inverted_store
);
130 static ssize_t
gpio_trig_gpio_show(struct device
*dev
,
131 struct device_attribute
*attr
, char *buf
)
133 struct led_classdev
*led
= dev_get_drvdata(dev
);
134 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
136 return sprintf(buf
, "%u\n", gpio_data
->gpio
);
139 static ssize_t
gpio_trig_gpio_store(struct device
*dev
,
140 struct device_attribute
*attr
, const char *buf
, size_t n
)
142 struct led_classdev
*led
= dev_get_drvdata(dev
);
143 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
147 ret
= sscanf(buf
, "%u", &gpio
);
149 dev_err(dev
, "couldn't read gpio number\n");
150 flush_work(&gpio_data
->work
);
154 if (gpio_data
->gpio
== gpio
)
158 if (gpio_data
->gpio
!= 0)
159 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
164 ret
= request_irq(gpio_to_irq(gpio
), gpio_trig_irq
,
165 IRQF_SHARED
| IRQF_TRIGGER_RISING
166 | IRQF_TRIGGER_FALLING
, "ledtrig-gpio", led
);
168 dev_err(dev
, "request_irq failed with error %d\n", ret
);
170 if (gpio_data
->gpio
!= 0)
171 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
172 gpio_data
->gpio
= gpio
;
175 return ret
? ret
: n
;
177 static DEVICE_ATTR(gpio
, 0644, gpio_trig_gpio_show
, gpio_trig_gpio_store
);
179 static void gpio_trig_activate(struct led_classdev
*led
)
181 struct gpio_trig_data
*gpio_data
;
184 gpio_data
= kzalloc(sizeof(*gpio_data
), GFP_KERNEL
);
188 ret
= device_create_file(led
->dev
, &dev_attr_gpio
);
192 ret
= device_create_file(led
->dev
, &dev_attr_inverted
);
196 ret
= device_create_file(led
->dev
, &dev_attr_desired_brightness
);
200 gpio_data
->led
= led
;
201 led
->trigger_data
= gpio_data
;
202 INIT_WORK(&gpio_data
->work
, gpio_trig_work
);
207 device_remove_file(led
->dev
, &dev_attr_inverted
);
210 device_remove_file(led
->dev
, &dev_attr_gpio
);
216 static void gpio_trig_deactivate(struct led_classdev
*led
)
218 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
221 device_remove_file(led
->dev
, &dev_attr_gpio
);
222 device_remove_file(led
->dev
, &dev_attr_inverted
);
223 device_remove_file(led
->dev
, &dev_attr_desired_brightness
);
224 flush_work(&gpio_data
->work
);
225 if (gpio_data
->gpio
!= 0)
226 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
231 static struct led_trigger gpio_led_trigger
= {
233 .activate
= gpio_trig_activate
,
234 .deactivate
= gpio_trig_deactivate
,
237 static int __init
gpio_trig_init(void)
239 return led_trigger_register(&gpio_led_trigger
);
241 module_init(gpio_trig_init
);
243 static void __exit
gpio_trig_exit(void)
245 led_trigger_unregister(&gpio_led_trigger
);
247 module_exit(gpio_trig_exit
);
249 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
250 MODULE_DESCRIPTION("GPIO LED trigger");
251 MODULE_LICENSE("GPL");