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>
6 * Copyright 2023 Linus Walleij <linus.walleij@linaro.org>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/interrupt.h>
14 #include <linux/leds.h>
15 #include <linux/slab.h>
18 struct gpio_trig_data
{
19 struct led_classdev
*led
;
20 unsigned desired_brightness
; /* desired brightness when led is on */
21 struct gpio_desc
*gpiod
; /* gpio that triggers the led */
24 static irqreturn_t
gpio_trig_irq(int irq
, void *_led
)
26 struct led_classdev
*led
= _led
;
27 struct gpio_trig_data
*gpio_data
= led_get_trigger_data(led
);
30 tmp
= gpiod_get_value_cansleep(gpio_data
->gpiod
);
32 if (gpio_data
->desired_brightness
)
33 led_set_brightness_nosleep(gpio_data
->led
,
34 gpio_data
->desired_brightness
);
36 led_set_brightness_nosleep(gpio_data
->led
, LED_FULL
);
38 led_set_brightness_nosleep(gpio_data
->led
, LED_OFF
);
44 static ssize_t
desired_brightness_show(struct device
*dev
,
45 struct device_attribute
*attr
, char *buf
)
47 struct gpio_trig_data
*gpio_data
= led_trigger_get_drvdata(dev
);
49 return sysfs_emit(buf
, "%u\n", gpio_data
->desired_brightness
);
52 static ssize_t
desired_brightness_store(struct device
*dev
,
53 struct device_attribute
*attr
, const char *buf
, size_t n
)
55 struct gpio_trig_data
*gpio_data
= led_trigger_get_drvdata(dev
);
56 u8 desired_brightness
;
59 ret
= kstrtou8(buf
, 10, &desired_brightness
);
63 gpio_data
->desired_brightness
= desired_brightness
;
67 static DEVICE_ATTR_RW(desired_brightness
);
69 static struct attribute
*gpio_trig_attrs
[] = {
70 &dev_attr_desired_brightness
.attr
,
73 ATTRIBUTE_GROUPS(gpio_trig
);
75 static int gpio_trig_activate(struct led_classdev
*led
)
77 struct gpio_trig_data
*gpio_data
;
78 struct device
*dev
= led
->dev
;
81 gpio_data
= kzalloc(sizeof(*gpio_data
), GFP_KERNEL
);
86 * The generic property "trigger-sources" is followed,
87 * and we hope that this is a GPIO.
89 gpio_data
->gpiod
= gpiod_get_optional(dev
, "trigger-sources", GPIOD_IN
);
90 if (IS_ERR(gpio_data
->gpiod
)) {
91 ret
= PTR_ERR(gpio_data
->gpiod
);
95 if (!gpio_data
->gpiod
) {
96 dev_err(dev
, "no valid GPIO for the trigger\n");
101 gpiod_set_consumer_name(gpio_data
->gpiod
, "led-trigger");
103 gpio_data
->led
= led
;
104 led_set_trigger_data(led
, gpio_data
);
106 ret
= request_threaded_irq(gpiod_to_irq(gpio_data
->gpiod
), NULL
, gpio_trig_irq
,
107 IRQF_ONESHOT
| IRQF_SHARED
| IRQF_TRIGGER_RISING
108 | IRQF_TRIGGER_FALLING
, "ledtrig-gpio", led
);
110 dev_err(dev
, "request_irq failed with error %d\n", ret
);
111 gpiod_put(gpio_data
->gpiod
);
116 /* Finally update the LED to initial status */
117 gpio_trig_irq(0, led
);
122 static void gpio_trig_deactivate(struct led_classdev
*led
)
124 struct gpio_trig_data
*gpio_data
= led_get_trigger_data(led
);
126 free_irq(gpiod_to_irq(gpio_data
->gpiod
), led
);
127 gpiod_put(gpio_data
->gpiod
);
131 static struct led_trigger gpio_led_trigger
= {
133 .activate
= gpio_trig_activate
,
134 .deactivate
= gpio_trig_deactivate
,
135 .groups
= gpio_trig_groups
,
137 module_led_trigger(gpio_led_trigger
);
139 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
140 MODULE_DESCRIPTION("GPIO LED trigger");
141 MODULE_LICENSE("GPL v2");