Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / leds / trigger / ledtrig-gpio.c
blob7f6a2352b0acdd911ef18402575b80b163a2084a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
7 */
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>
16 #include "../leds.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);
28 int tmp;
30 tmp = gpiod_get_value_cansleep(gpio_data->gpiod);
31 if (tmp) {
32 if (gpio_data->desired_brightness)
33 led_set_brightness_nosleep(gpio_data->led,
34 gpio_data->desired_brightness);
35 else
36 led_set_brightness_nosleep(gpio_data->led, LED_FULL);
37 } else {
38 led_set_brightness_nosleep(gpio_data->led, LED_OFF);
41 return IRQ_HANDLED;
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;
57 int ret;
59 ret = kstrtou8(buf, 10, &desired_brightness);
60 if (ret)
61 return ret;
63 gpio_data->desired_brightness = desired_brightness;
65 return n;
67 static DEVICE_ATTR_RW(desired_brightness);
69 static struct attribute *gpio_trig_attrs[] = {
70 &dev_attr_desired_brightness.attr,
71 NULL
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;
79 int ret;
81 gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
82 if (!gpio_data)
83 return -ENOMEM;
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);
92 kfree(gpio_data);
93 return ret;
95 if (!gpio_data->gpiod) {
96 dev_err(dev, "no valid GPIO for the trigger\n");
97 kfree(gpio_data);
98 return -EINVAL;
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);
109 if (ret) {
110 dev_err(dev, "request_irq failed with error %d\n", ret);
111 gpiod_put(gpio_data->gpiod);
112 kfree(gpio_data);
113 return ret;
116 /* Finally update the LED to initial status */
117 gpio_trig_irq(0, led);
119 return 0;
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);
128 kfree(gpio_data);
131 static struct led_trigger gpio_led_trigger = {
132 .name = "gpio",
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");