1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <linux/cleanup.h>
5 #include <linux/device.h>
6 #include <linux/init.h>
7 #include <linux/interrupt.h>
8 #include <linux/kdev_t.h>
9 #include <linux/kstrtox.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/printk.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/srcu.h>
16 #include <linux/sysfs.h>
17 #include <linux/types.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/gpio/driver.h>
22 #include <uapi/linux/gpio.h>
25 #include "gpiolib-sysfs.h"
27 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
31 #define GPIO_IRQF_TRIGGER_NONE 0
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)
38 GPIO_SYSFS_LINE_CLASS_ATTR_DIRECTION = 0,
39 GPIO_SYSFS_LINE_CLASS_ATTR_VALUE,
40 GPIO_SYSFS_LINE_CLASS_ATTR_EDGE,
41 GPIO_SYSFS_LINE_CLASS_ATTR_ACTIVE_LOW,
42 GPIO_SYSFS_LINE_CLASS_ATTR_SENTINEL,
43 GPIO_SYSFS_LINE_CLASS_ATTR_SIZE,
46 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
49 GPIO_SYSFS_LINE_CHIP_ATTR_DIRECTION = 0,
50 GPIO_SYSFS_LINE_CHIP_ATTR_VALUE,
51 GPIO_SYSFS_LINE_CHIP_ATTR_SENTINEL,
52 GPIO_SYSFS_LINE_CHIP_ATTR_SIZE,
56 struct list_head list;
58 struct gpio_desc *desc;
62 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
63 struct kernfs_node *value_kn;
65 unsigned char irq_flags;
66 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
68 bool direction_can_change;
70 struct kobject *parent;
71 struct device_attribute dir_attr;
72 struct device_attribute val_attr;
74 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
75 struct device_attribute edge_attr;
76 struct device_attribute active_low_attr;
78 struct attribute *class_attrs[GPIO_SYSFS_LINE_CLASS_ATTR_SIZE];
79 struct attribute_group class_attr_group;
80 const struct attribute_group *class_attr_groups[2];
81 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
83 struct attribute *chip_attrs[GPIO_SYSFS_LINE_CHIP_ATTR_SIZE];
84 struct attribute_group chip_attr_group;
85 const struct attribute_group *chip_attr_groups[2];
89 struct list_head exported_lines;
90 struct gpio_device *gdev;
91 struct device *cdev_id; /* Class device by GPIO device ID */
92 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
93 struct device *cdev_base; /* Class device by GPIO base */
94 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
98 * Lock to serialise gpiod export and unexport, and prevent re-export of
99 * gpiod whose chip is being unregistered.
101 static DEFINE_MUTEX(sysfs_lock);
104 * /sys/class/gpio/gpioN... only for GPIOs that are exported
106 * * MAY BE OMITTED if kernel won't allow direction changes
107 * * is read/write as "in" or "out"
108 * * may also be written as "high" or "low", initializing
109 * output value as specified ("out" implies "low")
111 * * always readable, subject to hardware behavior
112 * * may be writable, as zero/nonzero
114 * * configures behavior of poll(2) on /value
115 * * available only if pin can generate IRQs on input
116 * * is read/write as "none", "falling", "rising", or "both"
118 * * configures polarity of /value
119 * * is read/write as zero/nonzero
120 * * also affects existing and subsequent "falling" and "rising"
121 * /edge configuration
124 static ssize_t direction_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
127 struct gpiod_data *data = container_of(attr, struct gpiod_data,
129 struct gpio_desc *desc = data->desc;
132 scoped_guard(mutex, &data->mutex) {
133 gpiod_get_direction(desc);
134 value = !!test_bit(FLAG_IS_OUT, &desc->flags);
137 return sysfs_emit(buf, "%s\n", value ? "out" : "in");
140 static ssize_t direction_store(struct device *dev,
141 struct device_attribute *attr, const char *buf,
144 struct gpiod_data *data = container_of(attr, struct gpiod_data,
146 struct gpio_desc *desc = data->desc;
149 guard(mutex)(&data->mutex);
151 if (sysfs_streq(buf, "high"))
152 status = gpiod_direction_output_raw(desc, 1);
153 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
154 status = gpiod_direction_output_raw(desc, 0);
155 else if (sysfs_streq(buf, "in"))
156 status = gpiod_direction_input(desc);
160 return status ? : size;
163 static ssize_t value_show(struct device *dev, struct device_attribute *attr,
166 struct gpiod_data *data = container_of(attr, struct gpiod_data,
168 struct gpio_desc *desc = data->desc;
171 scoped_guard(mutex, &data->mutex)
172 status = gpiod_get_value_cansleep(desc);
177 return sysfs_emit(buf, "%zd\n", status);
180 static ssize_t value_store(struct device *dev, struct device_attribute *attr,
181 const char *buf, size_t size)
183 struct gpiod_data *data = container_of(attr, struct gpiod_data,
185 struct gpio_desc *desc = data->desc;
189 status = kstrtol(buf, 0, &value);
193 guard(mutex)(&data->mutex);
195 status = gpiod_set_value_cansleep(desc, value);
202 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
203 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
205 struct gpiod_data *data = priv;
207 sysfs_notify_dirent(data->value_kn);
212 /* Caller holds gpiod-data mutex. */
213 static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)
215 struct gpio_desc *desc = data->desc;
216 unsigned long irq_flags;
219 CLASS(gpio_chip_guard, guard)(desc);
223 data->irq = gpiod_to_irq(desc);
227 irq_flags = IRQF_SHARED;
228 if (flags & GPIO_IRQF_TRIGGER_FALLING) {
229 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
230 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
231 set_bit(FLAG_EDGE_FALLING, &desc->flags);
233 if (flags & GPIO_IRQF_TRIGGER_RISING) {
234 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
235 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
236 set_bit(FLAG_EDGE_RISING, &desc->flags);
240 * FIXME: This should be done in the irq_request_resources callback
241 * when the irq is requested, but a few drivers currently fail to do
244 * Remove this redundant call (along with the corresponding unlock)
245 * when those drivers have been fixed.
247 ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
251 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
256 data->irq_flags = flags;
261 gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
263 clear_bit(FLAG_EDGE_RISING, &desc->flags);
264 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
270 * Caller holds gpiod-data mutex (unless called after class-device
273 static void gpio_sysfs_free_irq(struct gpiod_data *data)
275 struct gpio_desc *desc = data->desc;
277 CLASS(gpio_chip_guard, guard)(desc);
282 free_irq(data->irq, data);
283 gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
284 clear_bit(FLAG_EDGE_RISING, &desc->flags);
285 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
288 static const char *const trigger_names[] = {
289 [GPIO_IRQF_TRIGGER_NONE] = "none",
290 [GPIO_IRQF_TRIGGER_FALLING] = "falling",
291 [GPIO_IRQF_TRIGGER_RISING] = "rising",
292 [GPIO_IRQF_TRIGGER_BOTH] = "both",
295 static ssize_t edge_show(struct device *dev, struct device_attribute *attr,
298 struct gpiod_data *data = container_of(attr, struct gpiod_data,
302 scoped_guard(mutex, &data->mutex)
303 flags = data->irq_flags;
305 if (flags >= ARRAY_SIZE(trigger_names))
308 return sysfs_emit(buf, "%s\n", trigger_names[flags]);
311 static ssize_t edge_store(struct device *dev, struct device_attribute *attr,
312 const char *buf, size_t size)
314 struct gpiod_data *data = container_of(attr, struct gpiod_data,
316 ssize_t status = size;
319 flags = sysfs_match_string(trigger_names, buf);
323 guard(mutex)(&data->mutex);
325 if (flags == data->irq_flags)
329 gpio_sysfs_free_irq(data);
334 status = gpio_sysfs_request_irq(data, flags);
338 gpiod_line_state_notify(data->desc, GPIO_V2_LINE_CHANGED_CONFIG);
343 /* Caller holds gpiod-data mutex. */
344 static int gpio_sysfs_set_active_low(struct gpiod_data *data, int value)
346 unsigned int flags = data->irq_flags;
347 struct gpio_desc *desc = data->desc;
350 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
353 assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
355 /* reconfigure poll(2) support if enabled on one edge only */
356 if (flags == GPIO_IRQF_TRIGGER_FALLING ||
357 flags == GPIO_IRQF_TRIGGER_RISING) {
358 gpio_sysfs_free_irq(data);
359 status = gpio_sysfs_request_irq(data, flags);
362 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
367 static ssize_t active_low_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
370 struct gpiod_data *data = container_of(attr, struct gpiod_data,
372 struct gpio_desc *desc = data->desc;
375 scoped_guard(mutex, &data->mutex)
376 value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags);
378 return sysfs_emit(buf, "%d\n", value);
381 static ssize_t active_low_store(struct device *dev,
382 struct device_attribute *attr,
383 const char *buf, size_t size)
385 struct gpiod_data *data = container_of(attr, struct gpiod_data,
390 status = kstrtol(buf, 0, &value);
394 guard(mutex)(&data->mutex);
396 return gpio_sysfs_set_active_low(data, value) ?: size;
398 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
400 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
403 struct device_attribute *dev_attr = container_of(attr,
404 struct device_attribute, attr);
405 umode_t mode = attr->mode;
406 struct gpiod_data *data;
408 if (strcmp(attr->name, "direction") == 0) {
409 data = container_of(dev_attr, struct gpiod_data, dir_attr);
411 if (!data->direction_can_change)
413 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
414 } else if (strcmp(attr->name, "edge") == 0) {
415 data = container_of(dev_attr, struct gpiod_data, edge_attr);
417 if (gpiod_to_irq(data->desc) < 0)
420 if (!data->direction_can_change &&
421 test_bit(FLAG_IS_OUT, &data->desc->flags))
423 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
430 * /sys/class/gpio/gpiochipN/
431 * /base ... matching gpio_chip.base (N)
432 * /label ... matching gpio_chip.label
433 * /ngpio ... matching gpio_chip.ngpio
437 * /sys/class/gpio/chipX/
438 * /export ... export GPIO at given offset
439 * /unexport ... unexport GPIO at given offset
440 * /label ... matching gpio_chip.label
441 * /ngpio ... matching gpio_chip.ngpio
444 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
445 static ssize_t base_show(struct device *dev, struct device_attribute *attr,
448 const struct gpiodev_data *data = dev_get_drvdata(dev);
450 return sysfs_emit(buf, "%u\n", data->gdev->base);
452 static DEVICE_ATTR_RO(base);
453 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
455 static ssize_t label_show(struct device *dev, struct device_attribute *attr,
458 const struct gpiodev_data *data = dev_get_drvdata(dev);
460 return sysfs_emit(buf, "%s\n", data->gdev->label);
462 static DEVICE_ATTR_RO(label);
464 static ssize_t ngpio_show(struct device *dev, struct device_attribute *attr,
467 const struct gpiodev_data *data = dev_get_drvdata(dev);
469 return sysfs_emit(buf, "%u\n", data->gdev->ngpio);
471 static DEVICE_ATTR_RO(ngpio);
473 static int export_gpio_desc(struct gpio_desc *desc)
477 CLASS(gpio_chip_guard, guard)(desc);
481 offset = gpio_chip_hwgpio(desc);
482 if (!gpiochip_line_is_valid(guard.gc, offset)) {
483 pr_debug_ratelimited("%s: GPIO %d masked\n", __func__,
484 gpio_chip_hwgpio(desc));
489 * No extra locking here; FLAG_SYSFS just signifies that the
490 * request and export were done by on behalf of userspace, so
491 * they may be undone on its behalf too.
494 ret = gpiod_request_user(desc, "sysfs");
498 ret = gpiod_set_transitory(desc, false);
504 ret = gpiod_export(desc, true);
508 set_bit(FLAG_SYSFS, &desc->flags);
509 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
515 static int unexport_gpio_desc(struct gpio_desc *desc)
518 * No extra locking here; FLAG_SYSFS just signifies that the
519 * request and export were done by on behalf of userspace, so
520 * they may be undone on its behalf too.
522 if (!test_and_clear_bit(FLAG_SYSFS, &desc->flags))
525 gpiod_unexport(desc);
531 static ssize_t do_chip_export_store(struct device *dev,
532 struct device_attribute *attr,
533 const char *buf, ssize_t size,
534 int (*handler)(struct gpio_desc *desc))
536 struct gpiodev_data *data = dev_get_drvdata(dev);
537 struct gpio_device *gdev = data->gdev;
538 struct gpio_desc *desc;
542 ret = kstrtouint(buf, 0, &gpio);
546 desc = gpio_device_get_desc(gdev, gpio);
548 return PTR_ERR(desc);
557 static ssize_t chip_export_store(struct device *dev,
558 struct device_attribute *attr,
559 const char *buf, size_t size)
561 return do_chip_export_store(dev, attr, buf, size, export_gpio_desc);
564 static struct device_attribute dev_attr_export = __ATTR(export, 0200, NULL,
567 static ssize_t chip_unexport_store(struct device *dev,
568 struct device_attribute *attr,
569 const char *buf, size_t size)
571 return do_chip_export_store(dev, attr, buf, size, unexport_gpio_desc);
574 static struct device_attribute dev_attr_unexport = __ATTR(unexport, 0200,
576 chip_unexport_store);
578 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
579 static struct attribute *gpiochip_attrs[] = {
581 &dev_attr_label.attr,
582 &dev_attr_ngpio.attr,
585 ATTRIBUTE_GROUPS(gpiochip);
586 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
588 static struct attribute *gpiochip_ext_attrs[] = {
589 &dev_attr_label.attr,
590 &dev_attr_ngpio.attr,
591 &dev_attr_export.attr,
592 &dev_attr_unexport.attr,
595 ATTRIBUTE_GROUPS(gpiochip_ext);
597 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
599 * /sys/class/gpio/export ... write-only
600 * integer N ... number of GPIO to export (full access)
601 * /sys/class/gpio/unexport ... write-only
602 * integer N ... number of GPIO to unexport
604 static ssize_t export_store(const struct class *class,
605 const struct class_attribute *attr,
606 const char *buf, size_t len)
608 struct gpio_desc *desc;
612 status = kstrtol(buf, 0, &gpio);
616 desc = gpio_to_desc(gpio);
617 /* reject invalid GPIOs */
619 pr_debug_ratelimited("%s: invalid GPIO %ld\n", __func__, gpio);
623 status = export_gpio_desc(desc);
625 pr_debug("%s: status %d\n", __func__, status);
626 return status ? : len;
628 static CLASS_ATTR_WO(export);
630 static ssize_t unexport_store(const struct class *class,
631 const struct class_attribute *attr,
632 const char *buf, size_t len)
634 struct gpio_desc *desc;
638 status = kstrtol(buf, 0, &gpio);
642 desc = gpio_to_desc(gpio);
643 /* reject bogus commands (gpiod_unexport() ignores them) */
645 pr_debug_ratelimited("%s: invalid GPIO %ld\n", __func__, gpio);
649 status = unexport_gpio_desc(desc);
651 pr_debug("%s: status %d\n", __func__, status);
652 return status ? : len;
654 static CLASS_ATTR_WO(unexport);
656 static struct attribute *gpio_class_attrs[] = {
657 &class_attr_export.attr,
658 &class_attr_unexport.attr,
661 ATTRIBUTE_GROUPS(gpio_class);
662 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
664 static const struct class gpio_class = {
666 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
667 .class_groups = gpio_class_groups,
668 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
671 static int match_gdev(struct device *dev, const void *desc)
673 struct gpiodev_data *data = dev_get_drvdata(dev);
674 const struct gpio_device *gdev = desc;
676 return data && data->gdev == gdev;
679 static struct gpiodev_data *
680 gdev_get_data(struct gpio_device *gdev) __must_hold(&sysfs_lock)
683 * Find the first device in GPIO class that matches. Whether that's
684 * the one indexed by GPIO base or device ID doesn't matter, it has
685 * the same address set as driver data.
687 struct device *cdev __free(put_device) = class_find_device(&gpio_class,
693 return dev_get_drvdata(cdev);
696 static void gpiod_attr_init(struct device_attribute *dev_attr, const char *name,
697 ssize_t (*show)(struct device *dev,
698 struct device_attribute *attr,
700 ssize_t (*store)(struct device *dev,
701 struct device_attribute *attr,
702 const char *buf, size_t count))
704 sysfs_attr_init(&dev_attr->attr);
705 dev_attr->attr.name = name;
706 dev_attr->attr.mode = 0644;
707 dev_attr->show = show;
708 dev_attr->store = store;
712 * gpiod_export - export a GPIO through sysfs
713 * @desc: GPIO to make available, already requested
714 * @direction_may_change: true if userspace may change GPIO direction
715 * Context: arch_initcall or later
717 * When drivers want to make a GPIO accessible to userspace after they
718 * have requested it -- perhaps while debugging, or as part of their
719 * public interface -- they may use this routine. If the GPIO can
720 * change direction (some can't) and the caller allows it, userspace
721 * will see "direction" sysfs attribute which may be used to change
722 * the gpio's direction. A "value" attribute will always be provided.
725 * 0 on success, or negative errno on failure.
727 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
729 char *path __free(kfree) = NULL;
730 struct gpiodev_data *gdev_data;
731 struct gpiod_data *desc_data;
732 struct gpio_device *gdev;
733 struct attribute **attrs;
736 /* can't export until sysfs is available ... */
737 if (!class_is_registered(&gpio_class)) {
738 pr_debug("%s: called too early!\n", __func__);
743 pr_debug("%s: invalid gpio descriptor\n", __func__);
747 CLASS(gpio_chip_guard, guard)(desc);
751 if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
756 guard(mutex)(&sysfs_lock);
758 if (!test_bit(FLAG_REQUESTED, &desc->flags)) {
759 gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__);
764 desc_data = kzalloc(sizeof(*desc_data), GFP_KERNEL);
770 desc_data->desc = desc;
771 mutex_init(&desc_data->mutex);
772 if (guard.gc->direction_input && guard.gc->direction_output)
773 desc_data->direction_can_change = direction_may_change;
775 desc_data->direction_can_change = false;
777 gpiod_attr_init(&desc_data->dir_attr, "direction",
778 direction_show, direction_store);
779 gpiod_attr_init(&desc_data->val_attr, "value", value_show, value_store);
781 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
782 gpiod_attr_init(&desc_data->edge_attr, "edge", edge_show, edge_store);
783 gpiod_attr_init(&desc_data->active_low_attr, "active_low",
784 active_low_show, active_low_store);
786 attrs = desc_data->class_attrs;
787 desc_data->class_attr_group.is_visible = gpio_is_visible;
788 attrs[GPIO_SYSFS_LINE_CLASS_ATTR_DIRECTION] = &desc_data->dir_attr.attr;
789 attrs[GPIO_SYSFS_LINE_CLASS_ATTR_VALUE] = &desc_data->val_attr.attr;
790 attrs[GPIO_SYSFS_LINE_CLASS_ATTR_EDGE] = &desc_data->edge_attr.attr;
791 attrs[GPIO_SYSFS_LINE_CLASS_ATTR_ACTIVE_LOW] = &desc_data->active_low_attr.attr;
793 desc_data->class_attr_group.attrs = desc_data->class_attrs;
794 desc_data->class_attr_groups[0] = &desc_data->class_attr_group;
797 * Note: we need to continue passing desc_data here as there's still
798 * at least one known user of gpiod_export_link() in the tree. This
799 * function still uses class_find_device() internally.
801 desc_data->dev = device_create_with_groups(&gpio_class, &gdev->dev,
802 MKDEV(0, 0), desc_data,
803 desc_data->class_attr_groups,
806 if (IS_ERR(desc_data->dev)) {
807 status = PTR_ERR(desc_data->dev);
811 desc_data->value_kn = sysfs_get_dirent(desc_data->dev->kobj.sd,
813 if (!desc_data->value_kn) {
815 goto err_unregister_device;
817 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
819 gdev_data = gdev_get_data(gdev);
825 desc_data->chip_attr_group.name = kasprintf(GFP_KERNEL, "gpio%u",
826 gpio_chip_hwgpio(desc));
827 if (!desc_data->chip_attr_group.name) {
832 attrs = desc_data->chip_attrs;
833 desc_data->chip_attr_group.is_visible = gpio_is_visible;
834 attrs[GPIO_SYSFS_LINE_CHIP_ATTR_DIRECTION] = &desc_data->dir_attr.attr;
835 attrs[GPIO_SYSFS_LINE_CHIP_ATTR_VALUE] = &desc_data->val_attr.attr;
837 desc_data->chip_attr_group.attrs = attrs;
838 desc_data->chip_attr_groups[0] = &desc_data->chip_attr_group;
840 desc_data->parent = &gdev_data->cdev_id->kobj;
841 status = sysfs_create_groups(desc_data->parent,
842 desc_data->chip_attr_groups);
846 path = kasprintf(GFP_KERNEL, "gpio%u/value", gpio_chip_hwgpio(desc));
849 goto err_remove_groups;
852 list_add(&desc_data->list, &gdev_data->exported_lines);
857 sysfs_remove_groups(desc_data->parent, desc_data->chip_attr_groups);
859 kfree(desc_data->chip_attr_group.name);
861 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
862 sysfs_put(desc_data->value_kn);
863 err_unregister_device:
864 device_unregister(desc_data->dev);
866 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
869 clear_bit(FLAG_EXPORT, &desc->flags);
870 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
873 EXPORT_SYMBOL_GPL(gpiod_export);
875 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
876 static int match_export(struct device *dev, const void *desc)
878 struct gpiod_data *data = dev_get_drvdata(dev);
880 return gpiod_is_equal(data->desc, desc);
882 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
885 * gpiod_export_link - create a sysfs link to an exported GPIO node
886 * @dev: device under which to create symlink
887 * @name: name of the symlink
888 * @desc: GPIO to create symlink to, already exported
890 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
891 * node. Caller is responsible for unlinking.
894 * 0 on success, or negative errno on failure.
896 int gpiod_export_link(struct device *dev, const char *name,
897 struct gpio_desc *desc)
899 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
904 pr_warn("%s: invalid GPIO\n", __func__);
908 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
912 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
918 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
920 EXPORT_SYMBOL_GPL(gpiod_export_link);
923 * gpiod_unexport - reverse effect of gpiod_export()
924 * @desc: GPIO to make unavailable
926 * This is implicit on gpiod_free().
928 void gpiod_unexport(struct gpio_desc *desc)
930 struct gpiod_data *tmp, *desc_data = NULL;
931 struct gpiodev_data *gdev_data;
932 struct gpio_device *gdev;
935 pr_warn("%s: invalid GPIO\n", __func__);
939 scoped_guard(mutex, &sysfs_lock) {
940 if (!test_bit(FLAG_EXPORT, &desc->flags))
943 gdev = gpiod_to_gpio_device(desc);
944 gdev_data = gdev_get_data(gdev);
948 list_for_each_entry(tmp, &gdev_data->exported_lines, list) {
949 if (gpiod_is_equal(desc, tmp->desc)) {
958 list_del(&desc_data->list);
959 clear_bit(FLAG_EXPORT, &desc->flags);
960 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
961 sysfs_put(desc_data->value_kn);
962 device_unregister(desc_data->dev);
965 * Release irq after deregistration to prevent race with
968 if (desc_data->irq_flags)
969 gpio_sysfs_free_irq(desc_data);
970 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
972 sysfs_remove_groups(desc_data->parent,
973 desc_data->chip_attr_groups);
976 mutex_destroy(&desc_data->mutex);
979 EXPORT_SYMBOL_GPL(gpiod_unexport);
981 int gpiochip_sysfs_register(struct gpio_device *gdev)
983 struct gpiodev_data *data;
984 struct gpio_chip *chip;
985 struct device *parent;
989 * Many systems add gpio chips for SOC support very early,
990 * before driver model support is available. In those cases we
991 * register later, in gpiolib_sysfs_init() ... here we just
992 * verify that _some_ field of gpio_class got initialized.
994 if (!class_is_registered(&gpio_class))
997 guard(srcu)(&gdev->srcu);
999 chip = srcu_dereference(gdev->chip, &gdev->srcu);
1004 * For sysfs backward compatibility we need to preserve this
1005 * preferred parenting to the gpio_chip parent field, if set.
1008 parent = chip->parent;
1010 parent = &gdev->dev;
1012 data = kmalloc(sizeof(*data), GFP_KERNEL);
1017 INIT_LIST_HEAD(&data->exported_lines);
1019 guard(mutex)(&sysfs_lock);
1021 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
1022 /* use chip->base for the ID; it's already known to be unique */
1023 data->cdev_base = device_create_with_groups(&gpio_class, parent,
1028 if (IS_ERR(data->cdev_base)) {
1029 err = PTR_ERR(data->cdev_base);
1033 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
1035 data->cdev_id = device_create_with_groups(&gpio_class, parent,
1037 gpiochip_ext_groups,
1038 "chip%d", gdev->id);
1039 if (IS_ERR(data->cdev_id)) {
1040 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
1041 device_unregister(data->cdev_base);
1042 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
1043 err = PTR_ERR(data->cdev_id);
1051 void gpiochip_sysfs_unregister(struct gpio_device *gdev)
1053 struct gpiodev_data *data;
1054 struct gpio_desc *desc;
1055 struct gpio_chip *chip;
1057 scoped_guard(mutex, &sysfs_lock) {
1058 data = gdev_get_data(gdev);
1062 #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY)
1063 device_unregister(data->cdev_base);
1064 #endif /* CONFIG_GPIO_SYSFS_LEGACY */
1065 device_unregister(data->cdev_id);
1069 guard(srcu)(&gdev->srcu);
1071 chip = srcu_dereference(gdev->chip, &gdev->srcu);
1075 /* unregister gpiod class devices owned by sysfs */
1076 for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) {
1077 gpiod_unexport(desc);
1083 * We're not really looking for a device - we just want to iterate over the
1084 * list and call this callback for each GPIO device. This is why this function
1087 static int gpiofind_sysfs_register(struct gpio_chip *gc, const void *data)
1089 struct gpio_device *gdev = gc->gpiodev;
1092 ret = gpiochip_sysfs_register(gdev);
1094 chip_err(gc, "failed to register the sysfs entry: %d\n", ret);
1099 static int __init gpiolib_sysfs_init(void)
1103 status = class_register(&gpio_class);
1107 /* Scan and register the gpio_chips which registered very
1108 * early (e.g. before the class_register above was called).
1110 * We run before arch_initcall() so chip->dev nodes can have
1111 * registered, and so arch_initcall() can always gpiod_export().
1113 (void)gpio_device_find(NULL, gpiofind_sysfs_register);
1117 postcore_initcall(gpiolib_sysfs_init);