1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <linux/cleanup.h>
5 #include <linux/device.h>
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>
12 #include <linux/mutex.h>
13 #include <linux/printk.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/string.h>
17 #include <linux/srcu.h>
18 #include <linux/sysfs.h>
19 #include <linux/types.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio/driver.h>
25 #include "gpiolib-sysfs.h"
29 #define GPIO_IRQF_TRIGGER_NONE 0
30 #define GPIO_IRQF_TRIGGER_FALLING BIT(0)
31 #define GPIO_IRQF_TRIGGER_RISING BIT(1)
32 #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
33 GPIO_IRQF_TRIGGER_RISING)
36 struct gpio_desc *desc;
39 struct kernfs_node *value_kn;
41 unsigned char irq_flags;
43 bool direction_can_change;
47 * Lock to serialise gpiod export and unexport, and prevent re-export of
48 * gpiod whose chip is being unregistered.
50 static DEFINE_MUTEX(sysfs_lock);
53 * /sys/class/gpio/gpioN... only for GPIOs that are exported
55 * * MAY BE OMITTED if kernel won't allow direction changes
56 * * is read/write as "in" or "out"
57 * * may also be written as "high" or "low", initializing
58 * output value as specified ("out" implies "low")
60 * * always readable, subject to hardware behavior
61 * * may be writable, as zero/nonzero
63 * * configures behavior of poll(2) on /value
64 * * available only if pin can generate IRQs on input
65 * * is read/write as "none", "falling", "rising", or "both"
67 * * configures polarity of /value
68 * * is read/write as zero/nonzero
69 * * also affects existing and subsequent "falling" and "rising"
73 static ssize_t direction_show(struct device *dev,
74 struct device_attribute *attr, char *buf)
76 struct gpiod_data *data = dev_get_drvdata(dev);
77 struct gpio_desc *desc = data->desc;
80 mutex_lock(&data->mutex);
82 gpiod_get_direction(desc);
83 value = !!test_bit(FLAG_IS_OUT, &desc->flags);
85 mutex_unlock(&data->mutex);
87 return sysfs_emit(buf, "%s\n", value ? "out" : "in");
90 static ssize_t direction_store(struct device *dev,
91 struct device_attribute *attr, const char *buf, size_t size)
93 struct gpiod_data *data = dev_get_drvdata(dev);
94 struct gpio_desc *desc = data->desc;
97 mutex_lock(&data->mutex);
99 if (sysfs_streq(buf, "high"))
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);
108 mutex_unlock(&data->mutex);
110 return status ? : size;
112 static DEVICE_ATTR_RW(direction);
114 static ssize_t value_show(struct device *dev,
115 struct device_attribute *attr, char *buf)
117 struct gpiod_data *data = dev_get_drvdata(dev);
118 struct gpio_desc *desc = data->desc;
121 mutex_lock(&data->mutex);
123 status = gpiod_get_value_cansleep(desc);
125 mutex_unlock(&data->mutex);
130 return sysfs_emit(buf, "%zd\n", status);
133 static ssize_t value_store(struct device *dev,
134 struct device_attribute *attr, const char *buf, size_t size)
136 struct gpiod_data *data = dev_get_drvdata(dev);
137 struct gpio_desc *desc = data->desc;
141 status = kstrtol(buf, 0, &value);
143 mutex_lock(&data->mutex);
145 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
147 } else if (status == 0) {
148 gpiod_set_value_cansleep(desc, value);
152 mutex_unlock(&data->mutex);
156 static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
158 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
160 struct gpiod_data *data = priv;
162 sysfs_notify_dirent(data->value_kn);
167 /* Caller holds gpiod-data mutex. */
168 static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
170 struct gpiod_data *data = dev_get_drvdata(dev);
171 struct gpio_desc *desc = data->desc;
172 unsigned long irq_flags;
175 CLASS(gpio_chip_guard, guard)(desc);
179 data->irq = gpiod_to_irq(desc);
183 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
187 irq_flags = IRQF_SHARED;
188 if (flags & GPIO_IRQF_TRIGGER_FALLING)
189 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
190 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
191 if (flags & GPIO_IRQF_TRIGGER_RISING)
192 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
193 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
196 * FIXME: This should be done in the irq_request_resources callback
197 * when the irq is requested, but a few drivers currently fail
200 * Remove this redundant call (along with the corresponding
201 * unlock) when those drivers have been fixed.
203 ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
207 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
212 data->irq_flags = flags;
217 gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
219 sysfs_put(data->value_kn);
225 * Caller holds gpiod-data mutex (unless called after class-device
228 static void gpio_sysfs_free_irq(struct device *dev)
230 struct gpiod_data *data = dev_get_drvdata(dev);
231 struct gpio_desc *desc = data->desc;
233 CLASS(gpio_chip_guard, guard)(desc);
238 free_irq(data->irq, data);
239 gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
240 sysfs_put(data->value_kn);
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",
250 static ssize_t edge_show(struct device *dev,
251 struct device_attribute *attr, char *buf)
253 struct gpiod_data *data = dev_get_drvdata(dev);
256 mutex_lock(&data->mutex);
258 flags = data->irq_flags;
260 mutex_unlock(&data->mutex);
262 if (flags >= ARRAY_SIZE(trigger_names))
265 return sysfs_emit(buf, "%s\n", trigger_names[flags]);
268 static ssize_t edge_store(struct device *dev,
269 struct device_attribute *attr, const char *buf, size_t size)
271 struct gpiod_data *data = dev_get_drvdata(dev);
272 ssize_t status = size;
275 flags = sysfs_match_string(trigger_names, buf);
279 mutex_lock(&data->mutex);
281 if (flags == data->irq_flags) {
287 gpio_sysfs_free_irq(dev);
290 status = gpio_sysfs_request_irq(dev, flags);
296 mutex_unlock(&data->mutex);
300 static DEVICE_ATTR_RW(edge);
302 /* Caller holds gpiod-data mutex. */
303 static int gpio_sysfs_set_active_low(struct device *dev, int value)
305 struct gpiod_data *data = dev_get_drvdata(dev);
306 unsigned int flags = data->irq_flags;
307 struct gpio_desc *desc = data->desc;
311 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
314 assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
316 /* reconfigure poll(2) support if enabled on one edge only */
317 if (flags == GPIO_IRQF_TRIGGER_FALLING ||
318 flags == GPIO_IRQF_TRIGGER_RISING) {
319 gpio_sysfs_free_irq(dev);
320 status = gpio_sysfs_request_irq(dev, flags);
326 static ssize_t active_low_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 struct gpiod_data *data = dev_get_drvdata(dev);
330 struct gpio_desc *desc = data->desc;
333 mutex_lock(&data->mutex);
335 value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags);
337 mutex_unlock(&data->mutex);
339 return sysfs_emit(buf, "%d\n", value);
342 static ssize_t active_low_store(struct device *dev,
343 struct device_attribute *attr, const char *buf, size_t size)
345 struct gpiod_data *data = dev_get_drvdata(dev);
349 status = kstrtol(buf, 0, &value);
353 mutex_lock(&data->mutex);
355 status = gpio_sysfs_set_active_low(dev, value);
357 mutex_unlock(&data->mutex);
359 return status ? : size;
361 static DEVICE_ATTR_RW(active_low);
363 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
366 struct device *dev = kobj_to_dev(kobj);
367 struct gpiod_data *data = dev_get_drvdata(dev);
368 struct gpio_desc *desc = data->desc;
369 umode_t mode = attr->mode;
370 bool show_direction = data->direction_can_change;
372 if (attr == &dev_attr_direction.attr) {
375 } else if (attr == &dev_attr_edge.attr) {
376 if (gpiod_to_irq(desc) < 0)
378 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
385 static struct attribute *gpio_attrs[] = {
386 &dev_attr_direction.attr,
388 &dev_attr_value.attr,
389 &dev_attr_active_low.attr,
393 static const struct attribute_group gpio_group = {
395 .is_visible = gpio_is_visible,
398 static const struct attribute_group *gpio_groups[] = {
404 * /sys/class/gpio/gpiochipN/
405 * /base ... matching gpio_chip.base (N)
406 * /label ... matching gpio_chip.label
407 * /ngpio ... matching gpio_chip.ngpio
410 static ssize_t base_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 const struct gpio_device *gdev = dev_get_drvdata(dev);
415 return sysfs_emit(buf, "%u\n", gdev->base);
417 static DEVICE_ATTR_RO(base);
419 static ssize_t label_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 const struct gpio_device *gdev = dev_get_drvdata(dev);
424 return sysfs_emit(buf, "%s\n", gdev->label);
426 static DEVICE_ATTR_RO(label);
428 static ssize_t ngpio_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 const struct gpio_device *gdev = dev_get_drvdata(dev);
433 return sysfs_emit(buf, "%u\n", gdev->ngpio);
435 static DEVICE_ATTR_RO(ngpio);
437 static struct attribute *gpiochip_attrs[] = {
439 &dev_attr_label.attr,
440 &dev_attr_ngpio.attr,
443 ATTRIBUTE_GROUPS(gpiochip);
446 * /sys/class/gpio/export ... write-only
447 * integer N ... number of GPIO to export (full access)
448 * /sys/class/gpio/unexport ... write-only
449 * integer N ... number of GPIO to unexport
451 static ssize_t export_store(const struct class *class,
452 const struct class_attribute *attr,
453 const char *buf, size_t len)
455 struct gpio_desc *desc;
459 status = kstrtol(buf, 0, &gpio);
463 desc = gpio_to_desc(gpio);
464 /* reject invalid GPIOs */
466 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
470 CLASS(gpio_chip_guard, guard)(desc);
474 offset = gpio_chip_hwgpio(desc);
475 if (!gpiochip_line_is_valid(guard.gc, offset)) {
476 pr_warn("%s: GPIO %ld masked\n", __func__, gpio);
480 /* No extra locking here; FLAG_SYSFS just signifies that the
481 * request and export were done by on behalf of userspace, so
482 * they may be undone on its behalf too.
485 status = gpiod_request_user(desc, "sysfs");
489 status = gpiod_set_transitory(desc, false);
495 status = gpiod_export(desc, true);
499 set_bit(FLAG_SYSFS, &desc->flags);
503 pr_debug("%s: status %d\n", __func__, status);
504 return status ? : len;
506 static CLASS_ATTR_WO(export);
508 static ssize_t unexport_store(const struct class *class,
509 const struct class_attribute *attr,
510 const char *buf, size_t len)
512 struct gpio_desc *desc;
516 status = kstrtol(buf, 0, &gpio);
520 desc = gpio_to_desc(gpio);
521 /* reject bogus commands (gpiod_unexport() ignores them) */
523 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
529 /* No extra locking here; FLAG_SYSFS just signifies that the
530 * request and export were done by on behalf of userspace, so
531 * they may be undone on its behalf too.
533 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
534 gpiod_unexport(desc);
540 pr_debug("%s: status %d\n", __func__, status);
541 return status ? : len;
543 static CLASS_ATTR_WO(unexport);
545 static struct attribute *gpio_class_attrs[] = {
546 &class_attr_export.attr,
547 &class_attr_unexport.attr,
550 ATTRIBUTE_GROUPS(gpio_class);
552 static struct class gpio_class = {
554 .class_groups = gpio_class_groups,
559 * gpiod_export - export a GPIO through sysfs
560 * @desc: GPIO to make available, already requested
561 * @direction_may_change: true if userspace may change GPIO direction
562 * Context: arch_initcall or later
564 * When drivers want to make a GPIO accessible to userspace after they
565 * have requested it -- perhaps while debugging, or as part of their
566 * public interface -- they may use this routine. If the GPIO can
567 * change direction (some can't) and the caller allows it, userspace
568 * will see "direction" sysfs attribute which may be used to change
569 * the gpio's direction. A "value" attribute will always be provided.
571 * Returns zero on success, else an error.
573 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
575 const char *ioname = NULL;
576 struct gpio_device *gdev;
577 struct gpiod_data *data;
581 /* can't export until sysfs is available ... */
582 if (!class_is_registered(&gpio_class)) {
583 pr_debug("%s: called too early!\n", __func__);
588 pr_debug("%s: invalid gpio descriptor\n", __func__);
592 CLASS(gpio_chip_guard, guard)(desc);
596 if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
601 mutex_lock(&sysfs_lock);
603 /* check if chip is being removed */
604 if (!gdev->mockdev) {
609 if (!test_bit(FLAG_REQUESTED, &desc->flags)) {
610 gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__);
615 data = kzalloc(sizeof(*data), GFP_KERNEL);
622 mutex_init(&data->mutex);
623 if (guard.gc->direction_input && guard.gc->direction_output)
624 data->direction_can_change = direction_may_change;
626 data->direction_can_change = false;
628 offset = gpio_chip_hwgpio(desc);
629 if (guard.gc->names && guard.gc->names[offset])
630 ioname = guard.gc->names[offset];
632 dev = device_create_with_groups(&gpio_class, &gdev->dev,
633 MKDEV(0, 0), data, gpio_groups,
634 ioname ? ioname : "gpio%u",
637 status = PTR_ERR(dev);
641 mutex_unlock(&sysfs_lock);
647 mutex_unlock(&sysfs_lock);
648 clear_bit(FLAG_EXPORT, &desc->flags);
649 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
652 EXPORT_SYMBOL_GPL(gpiod_export);
654 static int match_export(struct device *dev, const void *desc)
656 struct gpiod_data *data = dev_get_drvdata(dev);
658 return data->desc == desc;
662 * gpiod_export_link - create a sysfs link to an exported GPIO node
663 * @dev: device under which to create symlink
664 * @name: name of the symlink
665 * @desc: GPIO to create symlink to, already exported
667 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
668 * node. Caller is responsible for unlinking.
670 * Returns zero on success, else an error.
672 int gpiod_export_link(struct device *dev, const char *name,
673 struct gpio_desc *desc)
679 pr_warn("%s: invalid GPIO\n", __func__);
683 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
687 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
692 EXPORT_SYMBOL_GPL(gpiod_export_link);
695 * gpiod_unexport - reverse effect of gpiod_export()
696 * @desc: GPIO to make unavailable
698 * This is implicit on gpiod_free().
700 void gpiod_unexport(struct gpio_desc *desc)
702 struct gpiod_data *data;
706 pr_warn("%s: invalid GPIO\n", __func__);
710 mutex_lock(&sysfs_lock);
712 if (!test_bit(FLAG_EXPORT, &desc->flags))
715 dev = class_find_device(&gpio_class, NULL, desc, match_export);
719 data = dev_get_drvdata(dev);
721 clear_bit(FLAG_EXPORT, &desc->flags);
723 device_unregister(dev);
726 * Release irq after deregistration to prevent race with edge_store.
729 gpio_sysfs_free_irq(dev);
731 mutex_unlock(&sysfs_lock);
739 mutex_unlock(&sysfs_lock);
741 EXPORT_SYMBOL_GPL(gpiod_unexport);
743 int gpiochip_sysfs_register(struct gpio_device *gdev)
745 struct gpio_chip *chip;
746 struct device *parent;
750 * Many systems add gpio chips for SOC support very early,
751 * before driver model support is available. In those cases we
752 * register later, in gpiolib_sysfs_init() ... here we just
753 * verify that _some_ field of gpio_class got initialized.
755 if (!class_is_registered(&gpio_class))
758 guard(srcu)(&gdev->srcu);
760 chip = srcu_dereference(gdev->chip, &gdev->srcu);
765 * For sysfs backward compatibility we need to preserve this
766 * preferred parenting to the gpio_chip parent field, if set.
769 parent = chip->parent;
773 /* use chip->base for the ID; it's already known to be unique */
774 dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), gdev,
775 gpiochip_groups, GPIOCHIP_NAME "%d",
780 mutex_lock(&sysfs_lock);
782 mutex_unlock(&sysfs_lock);
787 void gpiochip_sysfs_unregister(struct gpio_device *gdev)
789 struct gpio_desc *desc;
790 struct gpio_chip *chip;
792 scoped_guard(mutex, &sysfs_lock) {
796 device_unregister(gdev->mockdev);
798 /* prevent further gpiod exports */
799 gdev->mockdev = NULL;
802 guard(srcu)(&gdev->srcu);
804 chip = srcu_dereference(gdev->chip, &gdev->srcu);
808 /* unregister gpiod class devices owned by sysfs */
809 for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) {
810 gpiod_unexport(desc);
816 * We're not really looking for a device - we just want to iterate over the
817 * list and call this callback for each GPIO device. This is why this function
820 static int gpiofind_sysfs_register(struct gpio_chip *gc, const void *data)
822 struct gpio_device *gdev = gc->gpiodev;
828 ret = gpiochip_sysfs_register(gdev);
830 chip_err(gc, "failed to register the sysfs entry: %d\n", ret);
835 static int __init gpiolib_sysfs_init(void)
839 status = class_register(&gpio_class);
843 /* Scan and register the gpio_chips which registered very
844 * early (e.g. before the class_register above was called).
846 * We run before arch_initcall() so chip->dev nodes can have
847 * registered, and so arch_initcall() can always gpiod_export().
849 (void)gpio_device_find(NULL, gpiofind_sysfs_register);
853 postcore_initcall(gpiolib_sysfs_init);