1 // SPDX-License-Identifier: GPL-2.0-only
3 * ledtrig-gio.c - LED Trigger Based on GPIO events
5 * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/gpio.h>
12 #include <linux/interrupt.h>
13 #include <linux/leds.h>
14 #include <linux/slab.h>
17 struct gpio_trig_data
{
18 struct led_classdev
*led
;
20 unsigned desired_brightness
; /* desired brightness when led is on */
21 unsigned inverted
; /* true when gpio is inverted */
22 unsigned gpio
; /* gpio that triggers the leds */
25 static irqreturn_t
gpio_trig_irq(int irq
, void *_led
)
27 struct led_classdev
*led
= _led
;
28 struct gpio_trig_data
*gpio_data
= led_get_trigger_data(led
);
31 tmp
= gpio_get_value_cansleep(gpio_data
->gpio
);
32 if (gpio_data
->inverted
)
36 if (gpio_data
->desired_brightness
)
37 led_set_brightness_nosleep(gpio_data
->led
,
38 gpio_data
->desired_brightness
);
40 led_set_brightness_nosleep(gpio_data
->led
, LED_FULL
);
42 led_set_brightness_nosleep(gpio_data
->led
, LED_OFF
);
48 static ssize_t
gpio_trig_brightness_show(struct device
*dev
,
49 struct device_attribute
*attr
, char *buf
)
51 struct gpio_trig_data
*gpio_data
= led_trigger_get_drvdata(dev
);
53 return sprintf(buf
, "%u\n", gpio_data
->desired_brightness
);
56 static ssize_t
gpio_trig_brightness_store(struct device
*dev
,
57 struct device_attribute
*attr
, const char *buf
, size_t n
)
59 struct gpio_trig_data
*gpio_data
= led_trigger_get_drvdata(dev
);
60 unsigned desired_brightness
;
63 ret
= sscanf(buf
, "%u", &desired_brightness
);
64 if (ret
< 1 || desired_brightness
> 255) {
65 dev_err(dev
, "invalid value\n");
69 gpio_data
->desired_brightness
= desired_brightness
;
73 static DEVICE_ATTR(desired_brightness
, 0644, gpio_trig_brightness_show
,
74 gpio_trig_brightness_store
);
76 static ssize_t
gpio_trig_inverted_show(struct device
*dev
,
77 struct device_attribute
*attr
, char *buf
)
79 struct gpio_trig_data
*gpio_data
= led_trigger_get_drvdata(dev
);
81 return sprintf(buf
, "%u\n", gpio_data
->inverted
);
84 static ssize_t
gpio_trig_inverted_store(struct device
*dev
,
85 struct device_attribute
*attr
, const char *buf
, size_t n
)
87 struct led_classdev
*led
= led_trigger_get_led(dev
);
88 struct gpio_trig_data
*gpio_data
= led_trigger_get_drvdata(dev
);
89 unsigned long inverted
;
92 ret
= kstrtoul(buf
, 10, &inverted
);
99 gpio_data
->inverted
= inverted
;
101 /* After inverting, we need to update the LED. */
102 if (gpio_is_valid(gpio_data
->gpio
))
103 gpio_trig_irq(0, led
);
107 static DEVICE_ATTR(inverted
, 0644, gpio_trig_inverted_show
,
108 gpio_trig_inverted_store
);
110 static ssize_t
gpio_trig_gpio_show(struct device
*dev
,
111 struct device_attribute
*attr
, char *buf
)
113 struct gpio_trig_data
*gpio_data
= led_trigger_get_drvdata(dev
);
115 return sprintf(buf
, "%u\n", gpio_data
->gpio
);
118 static ssize_t
gpio_trig_gpio_store(struct device
*dev
,
119 struct device_attribute
*attr
, const char *buf
, size_t n
)
121 struct led_classdev
*led
= led_trigger_get_led(dev
);
122 struct gpio_trig_data
*gpio_data
= led_trigger_get_drvdata(dev
);
126 ret
= sscanf(buf
, "%u", &gpio
);
128 dev_err(dev
, "couldn't read gpio number\n");
132 if (gpio_data
->gpio
== gpio
)
135 if (!gpio_is_valid(gpio
)) {
136 if (gpio_is_valid(gpio_data
->gpio
))
137 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
138 gpio_data
->gpio
= gpio
;
142 ret
= request_threaded_irq(gpio_to_irq(gpio
), NULL
, gpio_trig_irq
,
143 IRQF_ONESHOT
| IRQF_SHARED
| IRQF_TRIGGER_RISING
144 | IRQF_TRIGGER_FALLING
, "ledtrig-gpio", led
);
146 dev_err(dev
, "request_irq failed with error %d\n", ret
);
148 if (gpio_is_valid(gpio_data
->gpio
))
149 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
150 gpio_data
->gpio
= gpio
;
151 /* After changing the GPIO, we need to update the LED. */
152 gpio_trig_irq(0, led
);
155 return ret
? ret
: n
;
157 static DEVICE_ATTR(gpio
, 0644, gpio_trig_gpio_show
, gpio_trig_gpio_store
);
159 static struct attribute
*gpio_trig_attrs
[] = {
160 &dev_attr_desired_brightness
.attr
,
161 &dev_attr_inverted
.attr
,
165 ATTRIBUTE_GROUPS(gpio_trig
);
167 static int gpio_trig_activate(struct led_classdev
*led
)
169 struct gpio_trig_data
*gpio_data
;
171 gpio_data
= kzalloc(sizeof(*gpio_data
), GFP_KERNEL
);
175 gpio_data
->led
= led
;
176 gpio_data
->gpio
= -ENOENT
;
178 led_set_trigger_data(led
, gpio_data
);
183 static void gpio_trig_deactivate(struct led_classdev
*led
)
185 struct gpio_trig_data
*gpio_data
= led_get_trigger_data(led
);
187 if (gpio_is_valid(gpio_data
->gpio
))
188 free_irq(gpio_to_irq(gpio_data
->gpio
), led
);
192 static struct led_trigger gpio_led_trigger
= {
194 .activate
= gpio_trig_activate
,
195 .deactivate
= gpio_trig_deactivate
,
196 .groups
= gpio_trig_groups
,
198 module_led_trigger(gpio_led_trigger
);
200 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
201 MODULE_DESCRIPTION("GPIO LED trigger");
202 MODULE_LICENSE("GPL v2");