Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
17354bfe FB |
2 | /* |
3 | * ledtrig-gio.c - LED Trigger Based on GPIO events | |
4 | * | |
5 | * Copyright 2009 Felipe Balbi <me@felipebalbi.com> | |
17354bfe FB |
6 | */ |
7 | ||
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> | |
17354bfe | 13 | #include <linux/leds.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
f07fb521 | 15 | #include "../leds.h" |
17354bfe FB |
16 | |
17 | struct gpio_trig_data { | |
18 | struct led_classdev *led; | |
17354bfe FB |
19 | |
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 */ | |
23 | }; | |
24 | ||
25 | static irqreturn_t gpio_trig_irq(int irq, void *_led) | |
26 | { | |
27 | struct led_classdev *led = _led; | |
9bfd7d9e | 28 | struct gpio_trig_data *gpio_data = led_get_trigger_data(led); |
17354bfe FB |
29 | int tmp; |
30 | ||
914ae25a | 31 | tmp = gpio_get_value_cansleep(gpio_data->gpio); |
74cbe202 TLSC |
32 | if (gpio_data->inverted) |
33 | tmp = !tmp; | |
17354bfe | 34 | |
74cbe202 TLSC |
35 | if (tmp) { |
36 | if (gpio_data->desired_brightness) | |
81fe8e5b | 37 | led_set_brightness_nosleep(gpio_data->led, |
74cbe202 TLSC |
38 | gpio_data->desired_brightness); |
39 | else | |
81fe8e5b | 40 | led_set_brightness_nosleep(gpio_data->led, LED_FULL); |
74cbe202 | 41 | } else { |
81fe8e5b | 42 | led_set_brightness_nosleep(gpio_data->led, LED_OFF); |
74cbe202 | 43 | } |
71c17b06 JK |
44 | |
45 | return IRQ_HANDLED; | |
17354bfe FB |
46 | } |
47 | ||
48 | static ssize_t gpio_trig_brightness_show(struct device *dev, | |
49 | struct device_attribute *attr, char *buf) | |
50 | { | |
9bfd7d9e | 51 | struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev); |
17354bfe FB |
52 | |
53 | return sprintf(buf, "%u\n", gpio_data->desired_brightness); | |
54 | } | |
55 | ||
56 | static ssize_t gpio_trig_brightness_store(struct device *dev, | |
57 | struct device_attribute *attr, const char *buf, size_t n) | |
58 | { | |
9bfd7d9e | 59 | struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev); |
17354bfe FB |
60 | unsigned desired_brightness; |
61 | int ret; | |
62 | ||
63 | ret = sscanf(buf, "%u", &desired_brightness); | |
64 | if (ret < 1 || desired_brightness > 255) { | |
65 | dev_err(dev, "invalid value\n"); | |
66 | return -EINVAL; | |
67 | } | |
68 | ||
69 | gpio_data->desired_brightness = desired_brightness; | |
70 | ||
71 | return n; | |
72 | } | |
73 | static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show, | |
74 | gpio_trig_brightness_store); | |
75 | ||
76 | static ssize_t gpio_trig_inverted_show(struct device *dev, | |
77 | struct device_attribute *attr, char *buf) | |
78 | { | |
9bfd7d9e | 79 | struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev); |
17354bfe | 80 | |
cc587ece | 81 | return sprintf(buf, "%u\n", gpio_data->inverted); |
17354bfe FB |
82 | } |
83 | ||
84 | static ssize_t gpio_trig_inverted_store(struct device *dev, | |
85 | struct device_attribute *attr, const char *buf, size_t n) | |
86 | { | |
9bfd7d9e UKK |
87 | struct led_classdev *led = led_trigger_get_led(dev); |
88 | struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev); | |
cc587ece | 89 | unsigned long inverted; |
17354bfe FB |
90 | int ret; |
91 | ||
e8941928 | 92 | ret = kstrtoul(buf, 10, &inverted); |
cc587ece JK |
93 | if (ret < 0) |
94 | return ret; | |
95 | ||
96 | if (inverted > 1) | |
17354bfe | 97 | return -EINVAL; |
17354bfe | 98 | |
cc587ece | 99 | gpio_data->inverted = inverted; |
17354bfe | 100 | |
cc674c81 | 101 | /* After inverting, we need to update the LED. */ |
71c17b06 | 102 | gpio_trig_irq(0, led); |
cc674c81 | 103 | |
17354bfe FB |
104 | return n; |
105 | } | |
106 | static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show, | |
107 | gpio_trig_inverted_store); | |
108 | ||
109 | static ssize_t gpio_trig_gpio_show(struct device *dev, | |
110 | struct device_attribute *attr, char *buf) | |
111 | { | |
9bfd7d9e | 112 | struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev); |
17354bfe FB |
113 | |
114 | return sprintf(buf, "%u\n", gpio_data->gpio); | |
115 | } | |
116 | ||
117 | static ssize_t gpio_trig_gpio_store(struct device *dev, | |
118 | struct device_attribute *attr, const char *buf, size_t n) | |
119 | { | |
9bfd7d9e UKK |
120 | struct led_classdev *led = led_trigger_get_led(dev); |
121 | struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev); | |
17354bfe FB |
122 | unsigned gpio; |
123 | int ret; | |
124 | ||
125 | ret = sscanf(buf, "%u", &gpio); | |
126 | if (ret < 1) { | |
127 | dev_err(dev, "couldn't read gpio number\n"); | |
17354bfe FB |
128 | return -EINVAL; |
129 | } | |
130 | ||
48cccd26 TLSC |
131 | if (gpio_data->gpio == gpio) |
132 | return n; | |
133 | ||
17354bfe | 134 | if (!gpio) { |
48cccd26 TLSC |
135 | if (gpio_data->gpio != 0) |
136 | free_irq(gpio_to_irq(gpio_data->gpio), led); | |
137 | gpio_data->gpio = 0; | |
17354bfe FB |
138 | return n; |
139 | } | |
140 | ||
71c17b06 JK |
141 | ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq, |
142 | IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING | |
17354bfe | 143 | | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led); |
48cccd26 | 144 | if (ret) { |
17354bfe | 145 | dev_err(dev, "request_irq failed with error %d\n", ret); |
48cccd26 TLSC |
146 | } else { |
147 | if (gpio_data->gpio != 0) | |
148 | free_irq(gpio_to_irq(gpio_data->gpio), led); | |
149 | gpio_data->gpio = gpio; | |
71afe3cb | 150 | /* After changing the GPIO, we need to update the LED. */ |
71c17b06 | 151 | gpio_trig_irq(0, led); |
48cccd26 | 152 | } |
17354bfe FB |
153 | |
154 | return ret ? ret : n; | |
155 | } | |
156 | static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store); | |
157 | ||
9bfd7d9e UKK |
158 | static struct attribute *gpio_trig_attrs[] = { |
159 | &dev_attr_desired_brightness.attr, | |
160 | &dev_attr_inverted.attr, | |
161 | &dev_attr_gpio.attr, | |
162 | NULL | |
163 | }; | |
164 | ATTRIBUTE_GROUPS(gpio_trig); | |
165 | ||
2282e125 | 166 | static int gpio_trig_activate(struct led_classdev *led) |
17354bfe FB |
167 | { |
168 | struct gpio_trig_data *gpio_data; | |
17354bfe FB |
169 | |
170 | gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL); | |
171 | if (!gpio_data) | |
9bfd7d9e | 172 | return -ENOMEM; |
17354bfe FB |
173 | |
174 | gpio_data->led = led; | |
9bfd7d9e | 175 | led_set_trigger_data(led, gpio_data); |
2282e125 UKK |
176 | |
177 | return 0; | |
17354bfe FB |
178 | } |
179 | ||
180 | static void gpio_trig_deactivate(struct led_classdev *led) | |
181 | { | |
9bfd7d9e | 182 | struct gpio_trig_data *gpio_data = led_get_trigger_data(led); |
17354bfe | 183 | |
9bfd7d9e UKK |
184 | if (gpio_data->gpio != 0) |
185 | free_irq(gpio_to_irq(gpio_data->gpio), led); | |
186 | kfree(gpio_data); | |
17354bfe FB |
187 | } |
188 | ||
189 | static struct led_trigger gpio_led_trigger = { | |
190 | .name = "gpio", | |
191 | .activate = gpio_trig_activate, | |
192 | .deactivate = gpio_trig_deactivate, | |
9bfd7d9e | 193 | .groups = gpio_trig_groups, |
17354bfe | 194 | }; |
9bfd7d9e | 195 | module_led_trigger(gpio_led_trigger); |
17354bfe FB |
196 | |
197 | MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>"); | |
198 | MODULE_DESCRIPTION("GPIO LED trigger"); | |
033692eb | 199 | MODULE_LICENSE("GPL v2"); |