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/leds.h>
18 #include <linux/slab.h>
21 struct gpio_trig_data
{
22 struct led_classdev
*led
;
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 */
29 static irqreturn_t
gpio_trig_irq(int irq
, void *_led
)
31 struct led_classdev
*led
= _led
;
32 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
35 tmp
= gpio_get_value_cansleep(gpio_data
->gpio
);
36 if (gpio_data
->inverted
)
40 if (gpio_data
->desired_brightness
)
41 led_set_brightness_nosleep(gpio_data
->led
,
42 gpio_data
->desired_brightness
);
44 led_set_brightness_nosleep(gpio_data
->led
, LED_FULL
);
46 led_set_brightness_nosleep(gpio_data
->led
, LED_OFF
);
52 static ssize_t
gpio_trig_brightness_show(struct device
*dev
,
53 struct device_attribute
*attr
, char *buf
)
55 struct led_classdev
*led
= dev_get_drvdata(dev
);
56 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
58 return sprintf(buf
, "%u\n", gpio_data
->desired_brightness
);
61 static ssize_t
gpio_trig_brightness_store(struct device
*dev
,
62 struct device_attribute
*attr
, const char *buf
, size_t n
)
64 struct led_classdev
*led
= dev_get_drvdata(dev
);
65 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
66 unsigned desired_brightness
;
69 ret
= sscanf(buf
, "%u", &desired_brightness
);
70 if (ret
< 1 || desired_brightness
> 255) {
71 dev_err(dev
, "invalid value\n");
75 gpio_data
->desired_brightness
= desired_brightness
;
79 static DEVICE_ATTR(desired_brightness
, 0644, gpio_trig_brightness_show
,
80 gpio_trig_brightness_store
);
82 static ssize_t
gpio_trig_inverted_show(struct device
*dev
,
83 struct device_attribute
*attr
, char *buf
)
85 struct led_classdev
*led
= dev_get_drvdata(dev
);
86 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
88 return sprintf(buf
, "%u\n", gpio_data
->inverted
);
91 static ssize_t
gpio_trig_inverted_store(struct device
*dev
,
92 struct device_attribute
*attr
, const char *buf
, size_t n
)
94 struct led_classdev
*led
= dev_get_drvdata(dev
);
95 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
96 unsigned long inverted
;
99 ret
= kstrtoul(buf
, 10, &inverted
);
106 gpio_data
->inverted
= inverted
;
108 /* After inverting, we need to update the LED. */
109 gpio_trig_irq(0, led
);
113 static DEVICE_ATTR(inverted
, 0644, gpio_trig_inverted_show
,
114 gpio_trig_inverted_store
);
116 static ssize_t
gpio_trig_gpio_show(struct device
*dev
,
117 struct device_attribute
*attr
, char *buf
)
119 struct led_classdev
*led
= dev_get_drvdata(dev
);
120 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
122 return sprintf(buf
, "%u\n", gpio_data
->gpio
);
125 static ssize_t
gpio_trig_gpio_store(struct device
*dev
,
126 struct device_attribute
*attr
, const char *buf
, size_t n
)
128 struct led_classdev
*led
= dev_get_drvdata(dev
);
129 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
133 ret
= sscanf(buf
, "%u", &gpio
);
135 dev_err(dev
, "couldn't read gpio number\n");
139 if (gpio_data
->gpio
== gpio
)
143 if (gpio_data
->gpio
!= 0)
144 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
149 ret
= request_threaded_irq(gpio_to_irq(gpio
), NULL
, gpio_trig_irq
,
150 IRQF_ONESHOT
| IRQF_SHARED
| IRQF_TRIGGER_RISING
151 | IRQF_TRIGGER_FALLING
, "ledtrig-gpio", led
);
153 dev_err(dev
, "request_irq failed with error %d\n", ret
);
155 if (gpio_data
->gpio
!= 0)
156 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
157 gpio_data
->gpio
= gpio
;
158 /* After changing the GPIO, we need to update the LED. */
159 gpio_trig_irq(0, led
);
162 return ret
? ret
: n
;
164 static DEVICE_ATTR(gpio
, 0644, gpio_trig_gpio_show
, gpio_trig_gpio_store
);
166 static void gpio_trig_activate(struct led_classdev
*led
)
168 struct gpio_trig_data
*gpio_data
;
171 gpio_data
= kzalloc(sizeof(*gpio_data
), GFP_KERNEL
);
175 ret
= device_create_file(led
->dev
, &dev_attr_gpio
);
179 ret
= device_create_file(led
->dev
, &dev_attr_inverted
);
183 ret
= device_create_file(led
->dev
, &dev_attr_desired_brightness
);
187 gpio_data
->led
= led
;
188 led
->trigger_data
= gpio_data
;
189 led
->activated
= true;
194 device_remove_file(led
->dev
, &dev_attr_inverted
);
197 device_remove_file(led
->dev
, &dev_attr_gpio
);
203 static void gpio_trig_deactivate(struct led_classdev
*led
)
205 struct gpio_trig_data
*gpio_data
= led
->trigger_data
;
207 if (led
->activated
) {
208 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
);
211 if (gpio_data
->gpio
!= 0)
212 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
214 led
->activated
= false;
218 static struct led_trigger gpio_led_trigger
= {
220 .activate
= gpio_trig_activate
,
221 .deactivate
= gpio_trig_deactivate
,
224 static int __init
gpio_trig_init(void)
226 return led_trigger_register(&gpio_led_trigger
);
228 module_init(gpio_trig_init
);
230 static void __exit
gpio_trig_exit(void)
232 led_trigger_unregister(&gpio_led_trigger
);
234 module_exit(gpio_trig_exit
);
236 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
237 MODULE_DESCRIPTION("GPIO LED trigger");
238 MODULE_LICENSE("GPL");