Commit | Line | Data |
---|---|---|
dae5f0af | 1 | // SPDX-License-Identifier: GPL-2.0 |
380c7ba3 AS |
2 | |
3 | #include <linux/bitops.h> | |
59cba4a0 | 4 | #include <linux/cleanup.h> |
380c7ba3 | 5 | #include <linux/device.h> |
0eb4c6c2 | 6 | #include <linux/idr.h> |
380c7ba3 AS |
7 | #include <linux/init.h> |
8 | #include <linux/interrupt.h> | |
9 | #include <linux/kdev_t.h> | |
10 | #include <linux/kstrtox.h> | |
11 | #include <linux/list.h> | |
0eb4c6c2 | 12 | #include <linux/mutex.h> |
380c7ba3 AS |
13 | #include <linux/printk.h> |
14 | #include <linux/slab.h> | |
15 | #include <linux/spinlock.h> | |
16 | #include <linux/string.h> | |
d82b9e08 | 17 | #include <linux/srcu.h> |
0eb4c6c2 | 18 | #include <linux/sysfs.h> |
380c7ba3 AS |
19 | #include <linux/types.h> |
20 | ||
0eb4c6c2 AC |
21 | #include <linux/gpio/consumer.h> |
22 | #include <linux/gpio/driver.h> | |
0eb4c6c2 | 23 | |
285678c9 BG |
24 | #include <uapi/linux/gpio.h> |
25 | ||
0eb4c6c2 | 26 | #include "gpiolib.h" |
ef087d8e | 27 | #include "gpiolib-sysfs.h" |
0eb4c6c2 | 28 | |
380c7ba3 AS |
29 | struct kernfs_node; |
30 | ||
667630ed | 31 | #define GPIO_IRQF_TRIGGER_NONE 0 |
cef1717b JH |
32 | #define GPIO_IRQF_TRIGGER_FALLING BIT(0) |
33 | #define GPIO_IRQF_TRIGGER_RISING BIT(1) | |
34 | #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \ | |
35 | GPIO_IRQF_TRIGGER_RISING) | |
36 | ||
c43960fb JH |
37 | struct gpiod_data { |
38 | struct gpio_desc *desc; | |
6ffcb797 JH |
39 | |
40 | struct mutex mutex; | |
a08f5c21 | 41 | struct kernfs_node *value_kn; |
2ec74a95 | 42 | int irq; |
cef1717b | 43 | unsigned char irq_flags; |
427fdeef JH |
44 | |
45 | bool direction_can_change; | |
c43960fb JH |
46 | }; |
47 | ||
6ffcb797 JH |
48 | /* |
49 | * Lock to serialise gpiod export and unexport, and prevent re-export of | |
50 | * gpiod whose chip is being unregistered. | |
0eb4c6c2 AC |
51 | */ |
52 | static DEFINE_MUTEX(sysfs_lock); | |
53 | ||
54 | /* | |
55 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | |
56 | * /direction | |
57 | * * MAY BE OMITTED if kernel won't allow direction changes | |
58 | * * is read/write as "in" or "out" | |
59 | * * may also be written as "high" or "low", initializing | |
60 | * output value as specified ("out" implies "low") | |
61 | * /value | |
62 | * * always readable, subject to hardware behavior | |
63 | * * may be writable, as zero/nonzero | |
64 | * /edge | |
65 | * * configures behavior of poll(2) on /value | |
66 | * * available only if pin can generate IRQs on input | |
67 | * * is read/write as "none", "falling", "rising", or "both" | |
68 | * /active_low | |
69 | * * configures polarity of /value | |
70 | * * is read/write as zero/nonzero | |
71 | * * also affects existing and subsequent "falling" and "rising" | |
72 | * /edge configuration | |
73 | */ | |
74 | ||
6beac9d1 | 75 | static ssize_t direction_show(struct device *dev, |
0eb4c6c2 AC |
76 | struct device_attribute *attr, char *buf) |
77 | { | |
c43960fb JH |
78 | struct gpiod_data *data = dev_get_drvdata(dev); |
79 | struct gpio_desc *desc = data->desc; | |
e28747da | 80 | int value; |
0eb4c6c2 | 81 | |
d99c980c BG |
82 | scoped_guard(mutex, &data->mutex) { |
83 | gpiod_get_direction(desc); | |
84 | value = !!test_bit(FLAG_IS_OUT, &desc->flags); | |
85 | } | |
6ffcb797 | 86 | |
e28747da | 87 | return sysfs_emit(buf, "%s\n", value ? "out" : "in"); |
0eb4c6c2 AC |
88 | } |
89 | ||
6beac9d1 | 90 | static ssize_t direction_store(struct device *dev, |
0eb4c6c2 AC |
91 | struct device_attribute *attr, const char *buf, size_t size) |
92 | { | |
c43960fb JH |
93 | struct gpiod_data *data = dev_get_drvdata(dev); |
94 | struct gpio_desc *desc = data->desc; | |
0eb4c6c2 AC |
95 | ssize_t status; |
96 | ||
d99c980c | 97 | guard(mutex)(&data->mutex); |
0eb4c6c2 | 98 | |
f0b7866a | 99 | if (sysfs_streq(buf, "high")) |
0eb4c6c2 AC |
100 | status = gpiod_direction_output_raw(desc, 1); |
101 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) | |
102 | status = gpiod_direction_output_raw(desc, 0); | |
103 | else if (sysfs_streq(buf, "in")) | |
104 | status = gpiod_direction_input(desc); | |
105 | else | |
106 | status = -EINVAL; | |
107 | ||
0eb4c6c2 AC |
108 | return status ? : size; |
109 | } | |
6beac9d1 | 110 | static DEVICE_ATTR_RW(direction); |
0eb4c6c2 | 111 | |
6beac9d1 | 112 | static ssize_t value_show(struct device *dev, |
0eb4c6c2 AC |
113 | struct device_attribute *attr, char *buf) |
114 | { | |
c43960fb JH |
115 | struct gpiod_data *data = dev_get_drvdata(dev); |
116 | struct gpio_desc *desc = data->desc; | |
0eb4c6c2 AC |
117 | ssize_t status; |
118 | ||
d99c980c BG |
119 | scoped_guard(mutex, &data->mutex) |
120 | status = gpiod_get_value_cansleep(desc); | |
6ffcb797 | 121 | |
e28747da AS |
122 | if (status < 0) |
123 | return status; | |
124 | ||
125 | return sysfs_emit(buf, "%zd\n", status); | |
0eb4c6c2 AC |
126 | } |
127 | ||
6beac9d1 | 128 | static ssize_t value_store(struct device *dev, |
0eb4c6c2 AC |
129 | struct device_attribute *attr, const char *buf, size_t size) |
130 | { | |
c43960fb JH |
131 | struct gpiod_data *data = dev_get_drvdata(dev); |
132 | struct gpio_desc *desc = data->desc; | |
6b3c1791 AS |
133 | ssize_t status; |
134 | long value; | |
135 | ||
136 | status = kstrtol(buf, 0, &value); | |
92ac7de3 BG |
137 | if (status) |
138 | return status; | |
0eb4c6c2 | 139 | |
d99c980c | 140 | guard(mutex)(&data->mutex); |
0eb4c6c2 | 141 | |
92ac7de3 | 142 | status = gpiod_set_value_cansleep(desc, value); |
d99c980c BG |
143 | if (status) |
144 | return status; | |
0eb4c6c2 | 145 | |
d99c980c | 146 | return size; |
0eb4c6c2 | 147 | } |
7fda9100 | 148 | static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store); |
0eb4c6c2 AC |
149 | |
150 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) | |
151 | { | |
a08f5c21 JH |
152 | struct gpiod_data *data = priv; |
153 | ||
154 | sysfs_notify_dirent(data->value_kn); | |
0eb4c6c2 | 155 | |
0eb4c6c2 AC |
156 | return IRQ_HANDLED; |
157 | } | |
158 | ||
6ffcb797 | 159 | /* Caller holds gpiod-data mutex. */ |
cef1717b | 160 | static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags) |
0eb4c6c2 | 161 | { |
513246a3 BG |
162 | struct gpiod_data *data = dev_get_drvdata(dev); |
163 | struct gpio_desc *desc = data->desc; | |
164 | unsigned long irq_flags; | |
165 | int ret; | |
0eb4c6c2 | 166 | |
d83cee3d BG |
167 | CLASS(gpio_chip_guard, guard)(desc); |
168 | if (!guard.gc) | |
169 | return -ENODEV; | |
170 | ||
2ec74a95 JH |
171 | data->irq = gpiod_to_irq(desc); |
172 | if (data->irq < 0) | |
0eb4c6c2 AC |
173 | return -EIO; |
174 | ||
2ec74a95 JH |
175 | data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value"); |
176 | if (!data->value_kn) | |
177 | return -ENODEV; | |
0eb4c6c2 AC |
178 | |
179 | irq_flags = IRQF_SHARED; | |
7b925098 | 180 | if (flags & GPIO_IRQF_TRIGGER_FALLING) { |
0eb4c6c2 AC |
181 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
182 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
7b925098 BG |
183 | set_bit(FLAG_EDGE_FALLING, &desc->flags); |
184 | } | |
185 | if (flags & GPIO_IRQF_TRIGGER_RISING) { | |
0eb4c6c2 AC |
186 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
187 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
7b925098 BG |
188 | set_bit(FLAG_EDGE_RISING, &desc->flags); |
189 | } | |
0eb4c6c2 | 190 | |
52176d0d JH |
191 | /* |
192 | * FIXME: This should be done in the irq_request_resources callback | |
193 | * when the irq is requested, but a few drivers currently fail | |
194 | * to do so. | |
195 | * | |
196 | * Remove this redundant call (along with the corresponding | |
197 | * unlock) when those drivers have been fixed. | |
198 | */ | |
d83cee3d | 199 | ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); |
0eb4c6c2 | 200 | if (ret < 0) |
2ec74a95 | 201 | goto err_put_kn; |
0eb4c6c2 | 202 | |
2ec74a95 | 203 | ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags, |
a08f5c21 | 204 | "gpiolib", data); |
52176d0d JH |
205 | if (ret < 0) |
206 | goto err_unlock; | |
0eb4c6c2 | 207 | |
cef1717b | 208 | data->irq_flags = flags; |
2ec74a95 | 209 | |
0eb4c6c2 AC |
210 | return 0; |
211 | ||
52176d0d | 212 | err_unlock: |
d83cee3d | 213 | gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); |
2ec74a95 | 214 | err_put_kn: |
7b925098 BG |
215 | clear_bit(FLAG_EDGE_RISING, &desc->flags); |
216 | clear_bit(FLAG_EDGE_FALLING, &desc->flags); | |
2ec74a95 JH |
217 | sysfs_put(data->value_kn); |
218 | ||
0eb4c6c2 AC |
219 | return ret; |
220 | } | |
221 | ||
6ffcb797 JH |
222 | /* |
223 | * Caller holds gpiod-data mutex (unless called after class-device | |
224 | * deregistration). | |
225 | */ | |
2ec74a95 JH |
226 | static void gpio_sysfs_free_irq(struct device *dev) |
227 | { | |
228 | struct gpiod_data *data = dev_get_drvdata(dev); | |
229 | struct gpio_desc *desc = data->desc; | |
230 | ||
d83cee3d BG |
231 | CLASS(gpio_chip_guard, guard)(desc); |
232 | if (!guard.gc) | |
233 | return; | |
234 | ||
cef1717b | 235 | data->irq_flags = 0; |
2ec74a95 | 236 | free_irq(data->irq, data); |
d83cee3d | 237 | gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); |
7b925098 BG |
238 | clear_bit(FLAG_EDGE_RISING, &desc->flags); |
239 | clear_bit(FLAG_EDGE_FALLING, &desc->flags); | |
2ec74a95 JH |
240 | sysfs_put(data->value_kn); |
241 | } | |
242 | ||
667630ed AS |
243 | static const char * const trigger_names[] = { |
244 | [GPIO_IRQF_TRIGGER_NONE] = "none", | |
245 | [GPIO_IRQF_TRIGGER_FALLING] = "falling", | |
246 | [GPIO_IRQF_TRIGGER_RISING] = "rising", | |
247 | [GPIO_IRQF_TRIGGER_BOTH] = "both", | |
0eb4c6c2 AC |
248 | }; |
249 | ||
6beac9d1 | 250 | static ssize_t edge_show(struct device *dev, |
0eb4c6c2 AC |
251 | struct device_attribute *attr, char *buf) |
252 | { | |
c43960fb | 253 | struct gpiod_data *data = dev_get_drvdata(dev); |
667630ed | 254 | int flags; |
0eb4c6c2 | 255 | |
d99c980c BG |
256 | scoped_guard(mutex, &data->mutex) |
257 | flags = data->irq_flags; | |
6ffcb797 | 258 | |
667630ed | 259 | if (flags >= ARRAY_SIZE(trigger_names)) |
e28747da AS |
260 | return 0; |
261 | ||
667630ed | 262 | return sysfs_emit(buf, "%s\n", trigger_names[flags]); |
0eb4c6c2 AC |
263 | } |
264 | ||
6beac9d1 | 265 | static ssize_t edge_store(struct device *dev, |
0eb4c6c2 AC |
266 | struct device_attribute *attr, const char *buf, size_t size) |
267 | { | |
b91e1807 | 268 | struct gpiod_data *data = dev_get_drvdata(dev); |
513246a3 | 269 | ssize_t status = size; |
667630ed | 270 | int flags; |
e4339ce3 | 271 | |
667630ed AS |
272 | flags = sysfs_match_string(trigger_names, buf); |
273 | if (flags < 0) | |
274 | return flags; | |
b91e1807 | 275 | |
d99c980c | 276 | guard(mutex)(&data->mutex); |
0eb4c6c2 | 277 | |
d99c980c BG |
278 | if (flags == data->irq_flags) |
279 | return size; | |
b91e1807 | 280 | |
cef1717b | 281 | if (data->irq_flags) |
2ec74a95 JH |
282 | gpio_sysfs_free_irq(dev); |
283 | ||
d99c980c BG |
284 | if (!flags) |
285 | return size; | |
0eb4c6c2 | 286 | |
d99c980c BG |
287 | status = gpio_sysfs_request_irq(dev, flags); |
288 | if (status) | |
289 | return status; | |
0eb4c6c2 | 290 | |
7b925098 BG |
291 | gpiod_line_state_notify(data->desc, GPIO_V2_LINE_CHANGED_CONFIG); |
292 | ||
d99c980c | 293 | return size; |
0eb4c6c2 | 294 | } |
6beac9d1 | 295 | static DEVICE_ATTR_RW(edge); |
0eb4c6c2 | 296 | |
6ffcb797 | 297 | /* Caller holds gpiod-data mutex. */ |
2f323b85 | 298 | static int gpio_sysfs_set_active_low(struct device *dev, int value) |
0eb4c6c2 | 299 | { |
513246a3 BG |
300 | struct gpiod_data *data = dev_get_drvdata(dev); |
301 | unsigned int flags = data->irq_flags; | |
302 | struct gpio_desc *desc = data->desc; | |
303 | int status = 0; | |
304 | ||
0eb4c6c2 AC |
305 | |
306 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | |
307 | return 0; | |
308 | ||
fd80b8ba | 309 | assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value); |
0eb4c6c2 AC |
310 | |
311 | /* reconfigure poll(2) support if enabled on one edge only */ | |
cef1717b JH |
312 | if (flags == GPIO_IRQF_TRIGGER_FALLING || |
313 | flags == GPIO_IRQF_TRIGGER_RISING) { | |
2ec74a95 | 314 | gpio_sysfs_free_irq(dev); |
cef1717b | 315 | status = gpio_sysfs_request_irq(dev, flags); |
0eb4c6c2 AC |
316 | } |
317 | ||
5a7119e0 BG |
318 | gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); |
319 | ||
0eb4c6c2 AC |
320 | return status; |
321 | } | |
322 | ||
6beac9d1 | 323 | static ssize_t active_low_show(struct device *dev, |
0eb4c6c2 AC |
324 | struct device_attribute *attr, char *buf) |
325 | { | |
c43960fb JH |
326 | struct gpiod_data *data = dev_get_drvdata(dev); |
327 | struct gpio_desc *desc = data->desc; | |
e28747da | 328 | int value; |
0eb4c6c2 | 329 | |
d99c980c BG |
330 | scoped_guard(mutex, &data->mutex) |
331 | value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
0eb4c6c2 | 332 | |
e28747da | 333 | return sysfs_emit(buf, "%d\n", value); |
0eb4c6c2 AC |
334 | } |
335 | ||
6beac9d1 | 336 | static ssize_t active_low_store(struct device *dev, |
0eb4c6c2 AC |
337 | struct device_attribute *attr, const char *buf, size_t size) |
338 | { | |
513246a3 BG |
339 | struct gpiod_data *data = dev_get_drvdata(dev); |
340 | ssize_t status; | |
341 | long value; | |
0eb4c6c2 | 342 | |
6b3c1791 AS |
343 | status = kstrtol(buf, 0, &value); |
344 | if (status) | |
345 | return status; | |
346 | ||
d99c980c | 347 | guard(mutex)(&data->mutex); |
0eb4c6c2 | 348 | |
d99c980c | 349 | return gpio_sysfs_set_active_low(dev, value) ?: size; |
0eb4c6c2 | 350 | } |
6beac9d1 | 351 | static DEVICE_ATTR_RW(active_low); |
0eb4c6c2 | 352 | |
ebbeba12 JH |
353 | static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr, |
354 | int n) | |
355 | { | |
97cd738c | 356 | struct device *dev = kobj_to_dev(kobj); |
c43960fb JH |
357 | struct gpiod_data *data = dev_get_drvdata(dev); |
358 | struct gpio_desc *desc = data->desc; | |
ebbeba12 | 359 | umode_t mode = attr->mode; |
427fdeef | 360 | bool show_direction = data->direction_can_change; |
ebbeba12 JH |
361 | |
362 | if (attr == &dev_attr_direction.attr) { | |
363 | if (!show_direction) | |
364 | mode = 0; | |
365 | } else if (attr == &dev_attr_edge.attr) { | |
366 | if (gpiod_to_irq(desc) < 0) | |
367 | mode = 0; | |
368 | if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags)) | |
369 | mode = 0; | |
370 | } | |
371 | ||
372 | return mode; | |
373 | } | |
374 | ||
0915e6fe | 375 | static struct attribute *gpio_attrs[] = { |
ebbeba12 JH |
376 | &dev_attr_direction.attr, |
377 | &dev_attr_edge.attr, | |
0eb4c6c2 AC |
378 | &dev_attr_value.attr, |
379 | &dev_attr_active_low.attr, | |
380 | NULL, | |
381 | }; | |
ebbeba12 JH |
382 | |
383 | static const struct attribute_group gpio_group = { | |
384 | .attrs = gpio_attrs, | |
385 | .is_visible = gpio_is_visible, | |
386 | }; | |
387 | ||
388 | static const struct attribute_group *gpio_groups[] = { | |
389 | &gpio_group, | |
390 | NULL | |
391 | }; | |
0eb4c6c2 AC |
392 | |
393 | /* | |
394 | * /sys/class/gpio/gpiochipN/ | |
395 | * /base ... matching gpio_chip.base (N) | |
396 | * /label ... matching gpio_chip.label | |
397 | * /ngpio ... matching gpio_chip.ngpio | |
398 | */ | |
399 | ||
6beac9d1 | 400 | static ssize_t base_show(struct device *dev, |
0eb4c6c2 AC |
401 | struct device_attribute *attr, char *buf) |
402 | { | |
b93bca4b | 403 | const struct gpio_device *gdev = dev_get_drvdata(dev); |
0eb4c6c2 | 404 | |
8a7a6103 | 405 | return sysfs_emit(buf, "%u\n", gdev->base); |
0eb4c6c2 | 406 | } |
6beac9d1 | 407 | static DEVICE_ATTR_RO(base); |
0eb4c6c2 | 408 | |
6beac9d1 | 409 | static ssize_t label_show(struct device *dev, |
0eb4c6c2 AC |
410 | struct device_attribute *attr, char *buf) |
411 | { | |
b93bca4b | 412 | const struct gpio_device *gdev = dev_get_drvdata(dev); |
0eb4c6c2 | 413 | |
5694f274 | 414 | return sysfs_emit(buf, "%s\n", gdev->label); |
0eb4c6c2 | 415 | } |
6beac9d1 | 416 | static DEVICE_ATTR_RO(label); |
0eb4c6c2 | 417 | |
6beac9d1 | 418 | static ssize_t ngpio_show(struct device *dev, |
0eb4c6c2 AC |
419 | struct device_attribute *attr, char *buf) |
420 | { | |
b93bca4b | 421 | const struct gpio_device *gdev = dev_get_drvdata(dev); |
0eb4c6c2 | 422 | |
5694f274 | 423 | return sysfs_emit(buf, "%u\n", gdev->ngpio); |
0eb4c6c2 | 424 | } |
6beac9d1 | 425 | static DEVICE_ATTR_RO(ngpio); |
0eb4c6c2 | 426 | |
121b6a79 | 427 | static struct attribute *gpiochip_attrs[] = { |
0eb4c6c2 AC |
428 | &dev_attr_base.attr, |
429 | &dev_attr_label.attr, | |
430 | &dev_attr_ngpio.attr, | |
431 | NULL, | |
432 | }; | |
121b6a79 | 433 | ATTRIBUTE_GROUPS(gpiochip); |
0eb4c6c2 AC |
434 | |
435 | /* | |
436 | * /sys/class/gpio/export ... write-only | |
437 | * integer N ... number of GPIO to export (full access) | |
438 | * /sys/class/gpio/unexport ... write-only | |
439 | * integer N ... number of GPIO to unexport | |
440 | */ | |
75a2d422 GKH |
441 | static ssize_t export_store(const struct class *class, |
442 | const struct class_attribute *attr, | |
0eb4c6c2 AC |
443 | const char *buf, size_t len) |
444 | { | |
513246a3 | 445 | struct gpio_desc *desc; |
513246a3 BG |
446 | int status, offset; |
447 | long gpio; | |
0eb4c6c2 AC |
448 | |
449 | status = kstrtol(buf, 0, &gpio); | |
d83cee3d BG |
450 | if (status) |
451 | return status; | |
0eb4c6c2 | 452 | |
f13a0b0b | 453 | desc = gpio_to_desc(gpio); |
0eb4c6c2 AC |
454 | /* reject invalid GPIOs */ |
455 | if (!desc) { | |
37d5a6d6 | 456 | pr_debug_ratelimited("%s: invalid GPIO %ld\n", __func__, gpio); |
0eb4c6c2 AC |
457 | return -EINVAL; |
458 | } | |
d83cee3d BG |
459 | |
460 | CLASS(gpio_chip_guard, guard)(desc); | |
461 | if (!guard.gc) | |
462 | return -ENODEV; | |
463 | ||
23cf00dd | 464 | offset = gpio_chip_hwgpio(desc); |
d83cee3d | 465 | if (!gpiochip_line_is_valid(guard.gc, offset)) { |
37d5a6d6 | 466 | pr_debug_ratelimited("%s: GPIO %ld masked\n", __func__, gpio); |
23cf00dd MV |
467 | return -EINVAL; |
468 | } | |
0eb4c6c2 AC |
469 | |
470 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
471 | * request and export were done by on behalf of userspace, so | |
472 | * they may be undone on its behalf too. | |
473 | */ | |
474 | ||
95a4eed7 AS |
475 | status = gpiod_request_user(desc, "sysfs"); |
476 | if (status) | |
0eb4c6c2 | 477 | goto done; |
e10f72bf AJ |
478 | |
479 | status = gpiod_set_transitory(desc, false); | |
95dd1e34 BS |
480 | if (status) { |
481 | gpiod_free(desc); | |
482 | goto done; | |
e10f72bf | 483 | } |
0eb4c6c2 | 484 | |
95dd1e34 | 485 | status = gpiod_export(desc, true); |
285678c9 | 486 | if (status < 0) { |
95dd1e34 | 487 | gpiod_free(desc); |
285678c9 | 488 | } else { |
95dd1e34 | 489 | set_bit(FLAG_SYSFS, &desc->flags); |
285678c9 BG |
490 | gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); |
491 | } | |
95dd1e34 | 492 | |
0eb4c6c2 AC |
493 | done: |
494 | if (status) | |
495 | pr_debug("%s: status %d\n", __func__, status); | |
496 | return status ? : len; | |
497 | } | |
d83bb159 | 498 | static CLASS_ATTR_WO(export); |
0eb4c6c2 | 499 | |
75a2d422 GKH |
500 | static ssize_t unexport_store(const struct class *class, |
501 | const struct class_attribute *attr, | |
0eb4c6c2 AC |
502 | const char *buf, size_t len) |
503 | { | |
513246a3 BG |
504 | struct gpio_desc *desc; |
505 | int status; | |
506 | long gpio; | |
0eb4c6c2 AC |
507 | |
508 | status = kstrtol(buf, 0, &gpio); | |
509 | if (status < 0) | |
510 | goto done; | |
511 | ||
f13a0b0b | 512 | desc = gpio_to_desc(gpio); |
d74e3166 | 513 | /* reject bogus commands (gpiod_unexport() ignores them) */ |
0eb4c6c2 | 514 | if (!desc) { |
37d5a6d6 | 515 | pr_debug_ratelimited("%s: invalid GPIO %ld\n", __func__, gpio); |
0eb4c6c2 AC |
516 | return -EINVAL; |
517 | } | |
518 | ||
519 | status = -EINVAL; | |
520 | ||
521 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
522 | * request and export were done by on behalf of userspace, so | |
523 | * they may be undone on its behalf too. | |
524 | */ | |
525 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { | |
20d9b3b5 | 526 | gpiod_unexport(desc); |
0eb4c6c2 | 527 | gpiod_free(desc); |
20d9b3b5 | 528 | status = 0; |
0eb4c6c2 AC |
529 | } |
530 | done: | |
531 | if (status) | |
532 | pr_debug("%s: status %d\n", __func__, status); | |
533 | return status ? : len; | |
534 | } | |
d83bb159 | 535 | static CLASS_ATTR_WO(unexport); |
0eb4c6c2 | 536 | |
d83bb159 GKH |
537 | static struct attribute *gpio_class_attrs[] = { |
538 | &class_attr_export.attr, | |
539 | &class_attr_unexport.attr, | |
540 | NULL, | |
0eb4c6c2 | 541 | }; |
d83bb159 | 542 | ATTRIBUTE_GROUPS(gpio_class); |
0eb4c6c2 | 543 | |
b6f7aeaf | 544 | static const struct class gpio_class = { |
0eb4c6c2 | 545 | .name = "gpio", |
b6f7aeaf | 546 | .class_groups = gpio_class_groups, |
0eb4c6c2 AC |
547 | }; |
548 | ||
0eb4c6c2 AC |
549 | /** |
550 | * gpiod_export - export a GPIO through sysfs | |
2d9d0519 TR |
551 | * @desc: GPIO to make available, already requested |
552 | * @direction_may_change: true if userspace may change GPIO direction | |
0eb4c6c2 AC |
553 | * Context: arch_initcall or later |
554 | * | |
555 | * When drivers want to make a GPIO accessible to userspace after they | |
556 | * have requested it -- perhaps while debugging, or as part of their | |
557 | * public interface -- they may use this routine. If the GPIO can | |
558 | * change direction (some can't) and the caller allows it, userspace | |
559 | * will see "direction" sysfs attribute which may be used to change | |
560 | * the gpio's direction. A "value" attribute will always be provided. | |
561 | * | |
94bd9ce1 AS |
562 | * Returns: |
563 | * 0 on success, or negative errno on failure. | |
0eb4c6c2 AC |
564 | */ |
565 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) | |
566 | { | |
513246a3 BG |
567 | struct gpio_device *gdev; |
568 | struct gpiod_data *data; | |
513246a3 | 569 | struct device *dev; |
700cdf7e | 570 | int status; |
0eb4c6c2 AC |
571 | |
572 | /* can't export until sysfs is available ... */ | |
6f14c022 | 573 | if (!class_is_registered(&gpio_class)) { |
0eb4c6c2 AC |
574 | pr_debug("%s: called too early!\n", __func__); |
575 | return -ENOENT; | |
576 | } | |
577 | ||
578 | if (!desc) { | |
579 | pr_debug("%s: invalid gpio descriptor\n", __func__); | |
580 | return -EINVAL; | |
581 | } | |
582 | ||
d83cee3d BG |
583 | CLASS(gpio_chip_guard, guard)(desc); |
584 | if (!guard.gc) | |
585 | return -ENODEV; | |
586 | ||
8636f19c | 587 | if (test_and_set_bit(FLAG_EXPORT, &desc->flags)) |
35b54533 BG |
588 | return -EPERM; |
589 | ||
fdeb8e15 | 590 | gdev = desc->gdev; |
483d8211 | 591 | |
f4af1671 | 592 | guard(mutex)(&sysfs_lock); |
0eb4c6c2 | 593 | |
483d8211 | 594 | /* check if chip is being removed */ |
d83cee3d | 595 | if (!gdev->mockdev) { |
483d8211 | 596 | status = -ENODEV; |
f4af1671 | 597 | goto err_clear_bit; |
483d8211 JH |
598 | } |
599 | ||
35b54533 BG |
600 | if (!test_bit(FLAG_REQUESTED, &desc->flags)) { |
601 | gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__); | |
0eb4c6c2 | 602 | status = -EPERM; |
f4af1671 | 603 | goto err_clear_bit; |
0eb4c6c2 | 604 | } |
0eb4c6c2 | 605 | |
c43960fb JH |
606 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
607 | if (!data) { | |
608 | status = -ENOMEM; | |
f4af1671 | 609 | goto err_clear_bit; |
c43960fb JH |
610 | } |
611 | ||
612 | data->desc = desc; | |
6ffcb797 | 613 | mutex_init(&data->mutex); |
d83cee3d | 614 | if (guard.gc->direction_input && guard.gc->direction_output) |
427fdeef JH |
615 | data->direction_can_change = direction_may_change; |
616 | else | |
617 | data->direction_can_change = false; | |
c43960fb | 618 | |
ff2b1359 | 619 | dev = device_create_with_groups(&gpio_class, &gdev->dev, |
c43960fb | 620 | MKDEV(0, 0), data, gpio_groups, |
700cdf7e | 621 | "gpio%u", desc_to_gpio(desc)); |
0eb4c6c2 AC |
622 | if (IS_ERR(dev)) { |
623 | status = PTR_ERR(dev); | |
c43960fb | 624 | goto err_free_data; |
0eb4c6c2 AC |
625 | } |
626 | ||
0eb4c6c2 AC |
627 | return 0; |
628 | ||
c43960fb JH |
629 | err_free_data: |
630 | kfree(data); | |
f4af1671 | 631 | err_clear_bit: |
35b54533 | 632 | clear_bit(FLAG_EXPORT, &desc->flags); |
0eb4c6c2 AC |
633 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
634 | return status; | |
635 | } | |
636 | EXPORT_SYMBOL_GPL(gpiod_export); | |
637 | ||
c43960fb | 638 | static int match_export(struct device *dev, const void *desc) |
0eb4c6c2 | 639 | { |
c43960fb JH |
640 | struct gpiod_data *data = dev_get_drvdata(dev); |
641 | ||
642 | return data->desc == desc; | |
0eb4c6c2 AC |
643 | } |
644 | ||
645 | /** | |
646 | * gpiod_export_link - create a sysfs link to an exported GPIO node | |
647 | * @dev: device under which to create symlink | |
648 | * @name: name of the symlink | |
2d9d0519 | 649 | * @desc: GPIO to create symlink to, already exported |
0eb4c6c2 AC |
650 | * |
651 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | |
652 | * node. Caller is responsible for unlinking. | |
653 | * | |
94bd9ce1 AS |
654 | * Returns: |
655 | * 0 on success, or negative errno on failure. | |
0eb4c6c2 AC |
656 | */ |
657 | int gpiod_export_link(struct device *dev, const char *name, | |
658 | struct gpio_desc *desc) | |
659 | { | |
56d30ec1 JH |
660 | struct device *cdev; |
661 | int ret; | |
0eb4c6c2 AC |
662 | |
663 | if (!desc) { | |
664 | pr_warn("%s: invalid GPIO\n", __func__); | |
665 | return -EINVAL; | |
666 | } | |
667 | ||
56d30ec1 JH |
668 | cdev = class_find_device(&gpio_class, NULL, desc, match_export); |
669 | if (!cdev) | |
670 | return -ENODEV; | |
0eb4c6c2 | 671 | |
56d30ec1 JH |
672 | ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name); |
673 | put_device(cdev); | |
0eb4c6c2 | 674 | |
56d30ec1 | 675 | return ret; |
0eb4c6c2 AC |
676 | } |
677 | EXPORT_SYMBOL_GPL(gpiod_export_link); | |
678 | ||
0eb4c6c2 | 679 | /** |
31963eb0 | 680 | * gpiod_unexport - reverse effect of gpiod_export() |
2d9d0519 | 681 | * @desc: GPIO to make unavailable |
0eb4c6c2 | 682 | * |
31963eb0 | 683 | * This is implicit on gpiod_free(). |
0eb4c6c2 AC |
684 | */ |
685 | void gpiod_unexport(struct gpio_desc *desc) | |
686 | { | |
72eba6f6 JH |
687 | struct gpiod_data *data; |
688 | struct device *dev; | |
0eb4c6c2 AC |
689 | |
690 | if (!desc) { | |
691 | pr_warn("%s: invalid GPIO\n", __func__); | |
692 | return; | |
693 | } | |
694 | ||
f4af1671 BG |
695 | scoped_guard(mutex, &sysfs_lock) { |
696 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
697 | return; | |
72eba6f6 | 698 | |
f4af1671 BG |
699 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
700 | if (!dev) | |
701 | return; | |
72eba6f6 | 702 | |
f4af1671 BG |
703 | data = dev_get_drvdata(dev); |
704 | clear_bit(FLAG_EXPORT, &desc->flags); | |
705 | device_unregister(dev); | |
0eb4c6c2 | 706 | |
f4af1671 BG |
707 | /* |
708 | * Release irq after deregistration to prevent race with | |
709 | * edge_store. | |
710 | */ | |
711 | if (data->irq_flags) | |
712 | gpio_sysfs_free_irq(dev); | |
713 | } | |
0eb4c6c2 | 714 | |
72eba6f6 JH |
715 | put_device(dev); |
716 | kfree(data); | |
0eb4c6c2 AC |
717 | } |
718 | EXPORT_SYMBOL_GPL(gpiod_unexport); | |
719 | ||
afbc4f31 | 720 | int gpiochip_sysfs_register(struct gpio_device *gdev) |
0eb4c6c2 | 721 | { |
d83cee3d | 722 | struct gpio_chip *chip; |
513246a3 BG |
723 | struct device *parent; |
724 | struct device *dev; | |
0eb4c6c2 | 725 | |
426577bd JH |
726 | /* |
727 | * Many systems add gpio chips for SOC support very early, | |
0eb4c6c2 | 728 | * before driver model support is available. In those cases we |
426577bd | 729 | * register later, in gpiolib_sysfs_init() ... here we just |
0eb4c6c2 AC |
730 | * verify that _some_ field of gpio_class got initialized. |
731 | */ | |
6f14c022 | 732 | if (!class_is_registered(&gpio_class)) |
0eb4c6c2 AC |
733 | return 0; |
734 | ||
d83cee3d BG |
735 | guard(srcu)(&gdev->srcu); |
736 | ||
d82b9e08 | 737 | chip = srcu_dereference(gdev->chip, &gdev->srcu); |
d83cee3d BG |
738 | if (!chip) |
739 | return -ENODEV; | |
740 | ||
d27c1728 BJZ |
741 | /* |
742 | * For sysfs backward compatibility we need to preserve this | |
743 | * preferred parenting to the gpio_chip parent field, if set. | |
744 | */ | |
745 | if (chip->parent) | |
746 | parent = chip->parent; | |
747 | else | |
748 | parent = &gdev->dev; | |
749 | ||
0eb4c6c2 | 750 | /* use chip->base for the ID; it's already known to be unique */ |
b93bca4b | 751 | dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), gdev, |
ddd8891e GU |
752 | gpiochip_groups, GPIOCHIP_NAME "%d", |
753 | chip->base); | |
121b6a79 | 754 | if (IS_ERR(dev)) |
6a4b6b0a | 755 | return PTR_ERR(dev); |
3ff74be5 | 756 | |
f4af1671 | 757 | guard(mutex)(&sysfs_lock); |
afbc4f31 | 758 | gdev->mockdev = dev; |
0eb4c6c2 | 759 | |
6a4b6b0a | 760 | return 0; |
0eb4c6c2 AC |
761 | } |
762 | ||
afbc4f31 | 763 | void gpiochip_sysfs_unregister(struct gpio_device *gdev) |
0eb4c6c2 | 764 | { |
483d8211 | 765 | struct gpio_desc *desc; |
d83cee3d | 766 | struct gpio_chip *chip; |
0eb4c6c2 | 767 | |
59cba4a0 BG |
768 | scoped_guard(mutex, &sysfs_lock) { |
769 | if (!gdev->mockdev) | |
770 | return; | |
0eb4c6c2 | 771 | |
59cba4a0 | 772 | device_unregister(gdev->mockdev); |
6a4b6b0a | 773 | |
59cba4a0 BG |
774 | /* prevent further gpiod exports */ |
775 | gdev->mockdev = NULL; | |
776 | } | |
483d8211 | 777 | |
d83cee3d BG |
778 | guard(srcu)(&gdev->srcu); |
779 | ||
d82b9e08 | 780 | chip = srcu_dereference(gdev->chip, &gdev->srcu); |
be91c19e | 781 | if (!chip) |
d83cee3d BG |
782 | return; |
783 | ||
483d8211 | 784 | /* unregister gpiod class devices owned by sysfs */ |
20d9b3b5 AS |
785 | for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) { |
786 | gpiod_unexport(desc); | |
80c78fbe | 787 | gpiod_free(desc); |
20d9b3b5 | 788 | } |
0eb4c6c2 AC |
789 | } |
790 | ||
2a9101e8 BG |
791 | /* |
792 | * We're not really looking for a device - we just want to iterate over the | |
793 | * list and call this callback for each GPIO device. This is why this function | |
794 | * always returns 0. | |
795 | */ | |
796 | static int gpiofind_sysfs_register(struct gpio_chip *gc, const void *data) | |
797 | { | |
798 | struct gpio_device *gdev = gc->gpiodev; | |
799 | int ret; | |
800 | ||
801 | if (gdev->mockdev) | |
802 | return 0; | |
803 | ||
804 | ret = gpiochip_sysfs_register(gdev); | |
805 | if (ret) | |
806 | chip_err(gc, "failed to register the sysfs entry: %d\n", ret); | |
807 | ||
808 | return 0; | |
809 | } | |
810 | ||
0eb4c6c2 AC |
811 | static int __init gpiolib_sysfs_init(void) |
812 | { | |
2a9101e8 | 813 | int status; |
0eb4c6c2 AC |
814 | |
815 | status = class_register(&gpio_class); | |
816 | if (status < 0) | |
817 | return status; | |
818 | ||
819 | /* Scan and register the gpio_chips which registered very | |
820 | * early (e.g. before the class_register above was called). | |
821 | * | |
822 | * We run before arch_initcall() so chip->dev nodes can have | |
d74e3166 | 823 | * registered, and so arch_initcall() can always gpiod_export(). |
0eb4c6c2 | 824 | */ |
2a9101e8 | 825 | (void)gpio_device_find(NULL, gpiofind_sysfs_register); |
efb8235b | 826 | |
2a9101e8 | 827 | return 0; |
0eb4c6c2 AC |
828 | } |
829 | postcore_initcall(gpiolib_sysfs_init); |