HID: input: avoid polling stylus battery on Chromebook Pompom
[sfrench/cifs-2.6.git] / drivers / leds / trigger / ledtrig-gpio.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ledtrig-gio.c - LED Trigger Based on GPIO events
4  *
5  * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
6  * Copyright 2023 Linus Walleij <linus.walleij@linaro.org>
7  */
8
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"
17
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 */
22 };
23
24 static irqreturn_t gpio_trig_irq(int irq, void *_led)
25 {
26         struct led_classdev *led = _led;
27         struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
28         int tmp;
29
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);
39         }
40
41         return IRQ_HANDLED;
42 }
43
44 static ssize_t gpio_trig_brightness_show(struct device *dev,
45                 struct device_attribute *attr, char *buf)
46 {
47         struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
48
49         return sprintf(buf, "%u\n", gpio_data->desired_brightness);
50 }
51
52 static ssize_t gpio_trig_brightness_store(struct device *dev,
53                 struct device_attribute *attr, const char *buf, size_t n)
54 {
55         struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
56         unsigned desired_brightness;
57         int ret;
58
59         ret = sscanf(buf, "%u", &desired_brightness);
60         if (ret < 1 || desired_brightness > 255) {
61                 dev_err(dev, "invalid value\n");
62                 return -EINVAL;
63         }
64
65         gpio_data->desired_brightness = desired_brightness;
66
67         return n;
68 }
69 static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
70                 gpio_trig_brightness_store);
71
72 static struct attribute *gpio_trig_attrs[] = {
73         &dev_attr_desired_brightness.attr,
74         NULL
75 };
76 ATTRIBUTE_GROUPS(gpio_trig);
77
78 static int gpio_trig_activate(struct led_classdev *led)
79 {
80         struct gpio_trig_data *gpio_data;
81         struct device *dev = led->dev;
82         int ret;
83
84         gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
85         if (!gpio_data)
86                 return -ENOMEM;
87
88         /*
89          * The generic property "trigger-sources" is followed,
90          * and we hope that this is a GPIO.
91          */
92         gpio_data->gpiod = fwnode_gpiod_get_index(dev->fwnode,
93                                                   "trigger-sources",
94                                                   0, GPIOD_IN,
95                                                   "led-trigger");
96         if (IS_ERR(gpio_data->gpiod)) {
97                 ret = PTR_ERR(gpio_data->gpiod);
98                 kfree(gpio_data);
99                 return ret;
100         }
101         if (!gpio_data->gpiod) {
102                 dev_err(dev, "no valid GPIO for the trigger\n");
103                 kfree(gpio_data);
104                 return -EINVAL;
105         }
106
107         gpio_data->led = led;
108         led_set_trigger_data(led, gpio_data);
109
110         ret = request_threaded_irq(gpiod_to_irq(gpio_data->gpiod), NULL, gpio_trig_irq,
111                         IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
112                         | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
113         if (ret) {
114                 dev_err(dev, "request_irq failed with error %d\n", ret);
115                 gpiod_put(gpio_data->gpiod);
116                 kfree(gpio_data);
117                 return ret;
118         }
119
120         /* Finally update the LED to initial status */
121         gpio_trig_irq(0, led);
122
123         return 0;
124 }
125
126 static void gpio_trig_deactivate(struct led_classdev *led)
127 {
128         struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
129
130         free_irq(gpiod_to_irq(gpio_data->gpiod), led);
131         gpiod_put(gpio_data->gpiod);
132         kfree(gpio_data);
133 }
134
135 static struct led_trigger gpio_led_trigger = {
136         .name           = "gpio",
137         .activate       = gpio_trig_activate,
138         .deactivate     = gpio_trig_deactivate,
139         .groups         = gpio_trig_groups,
140 };
141 module_led_trigger(gpio_led_trigger);
142
143 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
144 MODULE_DESCRIPTION("GPIO LED trigger");
145 MODULE_LICENSE("GPL v2");