1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/acpi.h>
4 #include <linux/bitmap.h>
5 #include <linux/cleanup.h>
6 #include <linux/compat.h>
7 #include <linux/debugfs.h>
8 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/file.h>
13 #include <linux/idr.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/lockdep.h>
19 #include <linux/module.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/srcu.h>
26 #include <linux/string.h>
28 #include <linux/gpio.h>
29 #include <linux/gpio/driver.h>
30 #include <linux/gpio/machine.h>
32 #include <uapi/linux/gpio.h>
34 #include "gpiolib-acpi.h"
35 #include "gpiolib-cdev.h"
36 #include "gpiolib-of.h"
37 #include "gpiolib-swnode.h"
38 #include "gpiolib-sysfs.h"
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/gpio.h>
44 /* Implementation infrastructure for GPIO interfaces.
46 * The GPIO programming interface allows for inlining speed-critical
47 * get/set operations for common cases, so that access to SOC-integrated
48 * GPIOs can sometimes cost only an instruction or two per bit.
51 /* Device and char device-related information */
52 static DEFINE_IDA(gpio_ida);
53 static dev_t gpio_devt;
54 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
56 static int gpio_bus_match(struct device *dev, struct device_driver *drv)
58 struct fwnode_handle *fwnode = dev_fwnode(dev);
61 * Only match if the fwnode doesn't already have a proper struct device
64 if (fwnode && fwnode->dev != dev)
69 static const struct bus_type gpio_bus_type = {
71 .match = gpio_bus_match,
75 * Number of GPIOs to use for the fast path in set array
77 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
79 static DEFINE_MUTEX(gpio_lookup_lock);
80 static LIST_HEAD(gpio_lookup_list);
82 static LIST_HEAD(gpio_devices);
83 /* Protects the GPIO device list against concurrent modifications. */
84 static DEFINE_MUTEX(gpio_devices_lock);
85 /* Ensures coherence during read-only accesses to the list of GPIO devices. */
86 DEFINE_STATIC_SRCU(gpio_devices_srcu);
88 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
89 static LIST_HEAD(gpio_machine_hogs);
91 static void gpiochip_free_hogs(struct gpio_chip *gc);
92 static int gpiochip_add_irqchip(struct gpio_chip *gc,
93 struct lock_class_key *lock_key,
94 struct lock_class_key *request_key);
95 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
96 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
97 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
98 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
100 static bool gpiolib_initialized;
102 const char *gpiod_get_label(struct gpio_desc *desc)
106 flags = READ_ONCE(desc->flags);
107 if (test_bit(FLAG_USED_AS_IRQ, &flags) &&
108 !test_bit(FLAG_REQUESTED, &flags))
111 return test_bit(FLAG_REQUESTED, &flags) ?
112 srcu_dereference(desc->label, &desc->srcu) : NULL;
115 static int desc_set_label(struct gpio_desc *desc, const char *label)
117 const char *new = NULL, *old;
120 new = kstrdup_const(label, GFP_KERNEL);
125 old = rcu_replace_pointer(desc->label, new, 1);
126 synchronize_srcu(&desc->srcu);
133 * gpio_to_desc - Convert a GPIO number to its descriptor
134 * @gpio: global GPIO number
137 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
138 * with the given number exists in the system.
140 struct gpio_desc *gpio_to_desc(unsigned gpio)
142 struct gpio_device *gdev;
144 scoped_guard(srcu, &gpio_devices_srcu) {
145 list_for_each_entry_srcu(gdev, &gpio_devices, list,
146 srcu_read_lock_held(&gpio_devices_srcu)) {
147 if (gdev->base <= gpio &&
148 gdev->base + gdev->ngpio > gpio)
149 return &gdev->descs[gpio - gdev->base];
153 if (!gpio_is_valid(gpio))
154 pr_warn("invalid GPIO %d\n", gpio);
158 EXPORT_SYMBOL_GPL(gpio_to_desc);
160 /* This function is deprecated and will be removed soon, don't use. */
161 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
164 return gpio_device_get_desc(gc->gpiodev, hwnum);
166 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
169 * gpio_device_get_desc() - get the GPIO descriptor corresponding to the given
170 * hardware number for this GPIO device
171 * @gdev: GPIO device to get the descriptor from
172 * @hwnum: hardware number of the GPIO for this chip
175 * A pointer to the GPIO descriptor or %EINVAL if no GPIO exists in the given
176 * chip for the specified hardware number or %ENODEV if the underlying chip
179 * The reference count of struct gpio_device is *NOT* increased like when the
180 * GPIO is being requested for exclusive usage. It's up to the caller to make
181 * sure the GPIO device will stay alive together with the descriptor returned
185 gpio_device_get_desc(struct gpio_device *gdev, unsigned int hwnum)
187 if (hwnum >= gdev->ngpio)
188 return ERR_PTR(-EINVAL);
190 return &gdev->descs[hwnum];
192 EXPORT_SYMBOL_GPL(gpio_device_get_desc);
195 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
196 * @desc: GPIO descriptor
198 * This should disappear in the future but is needed since we still
199 * use GPIO numbers for error messages and sysfs nodes.
202 * The global GPIO number for the GPIO specified by its descriptor.
204 int desc_to_gpio(const struct gpio_desc *desc)
206 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
208 EXPORT_SYMBOL_GPL(desc_to_gpio);
212 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
213 * @desc: descriptor to return the chip of
216 * This function is unsafe and should not be used. Using the chip address
217 * without taking the SRCU read lock may result in dereferencing a dangling
220 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
225 return gpio_device_get_chip(desc->gdev);
227 EXPORT_SYMBOL_GPL(gpiod_to_chip);
230 * gpiod_to_gpio_device() - Return the GPIO device to which this descriptor
232 * @desc: Descriptor for which to return the GPIO device.
234 * This *DOES NOT* increase the reference count of the GPIO device as it's
235 * expected that the descriptor is requested and the users already holds a
236 * reference to the device.
239 * Address of the GPIO device owning this descriptor.
241 struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
248 EXPORT_SYMBOL_GPL(gpiod_to_gpio_device);
251 * gpio_device_get_base() - Get the base GPIO number allocated by this device
255 * First GPIO number in the global GPIO numberspace for this device.
257 int gpio_device_get_base(struct gpio_device *gdev)
261 EXPORT_SYMBOL_GPL(gpio_device_get_base);
264 * gpio_device_get_label() - Get the label of this GPIO device
268 * Pointer to the string containing the GPIO device label. The string's
269 * lifetime is tied to that of the underlying GPIO device.
271 const char *gpio_device_get_label(struct gpio_device *gdev)
275 EXPORT_SYMBOL(gpio_device_get_label);
278 * gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device
282 * Address of the GPIO chip backing this device.
285 * Until we can get rid of all non-driver users of struct gpio_chip, we must
286 * provide a way of retrieving the pointer to it from struct gpio_device. This
287 * is *NOT* safe as the GPIO API is considered to be hot-unpluggable and the
288 * chip can dissapear at any moment (unlike reference-counted struct
291 * Use at your own risk.
293 struct gpio_chip *gpio_device_get_chip(struct gpio_device *gdev)
295 return rcu_dereference_check(gdev->chip, 1);
297 EXPORT_SYMBOL_GPL(gpio_device_get_chip);
299 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
300 static int gpiochip_find_base_unlocked(int ngpio)
302 struct gpio_device *gdev;
303 int base = GPIO_DYNAMIC_BASE;
305 list_for_each_entry_srcu(gdev, &gpio_devices, list,
306 lockdep_is_held(&gpio_devices_lock)) {
307 /* found a free space? */
308 if (gdev->base >= base + ngpio)
310 /* nope, check the space right after the chip */
311 base = gdev->base + gdev->ngpio;
312 if (base < GPIO_DYNAMIC_BASE)
313 base = GPIO_DYNAMIC_BASE;
316 if (gpio_is_valid(base)) {
317 pr_debug("%s: found new base at %d\n", __func__, base);
320 pr_err("%s: cannot find free range\n", __func__);
326 * gpiod_get_direction - return the current direction of a GPIO
327 * @desc: GPIO to get the direction of
329 * Returns 0 for output, 1 for input, or an error code in case of error.
331 * This function may sleep if gpiod_cansleep() is true.
333 int gpiod_get_direction(struct gpio_desc *desc)
340 * We cannot use VALIDATE_DESC() as we must not return 0 for a NULL
341 * descriptor like we usually do.
343 if (!desc || IS_ERR(desc))
346 CLASS(gpio_chip_guard, guard)(desc);
350 offset = gpio_chip_hwgpio(desc);
351 flags = READ_ONCE(desc->flags);
354 * Open drain emulation using input mode may incorrectly report
355 * input here, fix that up.
357 if (test_bit(FLAG_OPEN_DRAIN, &flags) &&
358 test_bit(FLAG_IS_OUT, &flags))
361 if (!guard.gc->get_direction)
364 ret = guard.gc->get_direction(guard.gc, offset);
368 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
372 assign_bit(FLAG_IS_OUT, &flags, !ret);
373 WRITE_ONCE(desc->flags, flags);
377 EXPORT_SYMBOL_GPL(gpiod_get_direction);
380 * Add a new chip to the global chips list, keeping the list of chips sorted
381 * by range(means [base, base + ngpio - 1]) order.
383 * Return -EBUSY if the new chip overlaps with some other chip's integer
386 static int gpiodev_add_to_list_unlocked(struct gpio_device *gdev)
388 struct gpio_device *prev, *next;
390 lockdep_assert_held(&gpio_devices_lock);
392 if (list_empty(&gpio_devices)) {
393 /* initial entry in list */
394 list_add_tail_rcu(&gdev->list, &gpio_devices);
398 next = list_first_entry(&gpio_devices, struct gpio_device, list);
399 if (gdev->base + gdev->ngpio <= next->base) {
400 /* add before first entry */
401 list_add_rcu(&gdev->list, &gpio_devices);
405 prev = list_last_entry(&gpio_devices, struct gpio_device, list);
406 if (prev->base + prev->ngpio <= gdev->base) {
407 /* add behind last entry */
408 list_add_tail_rcu(&gdev->list, &gpio_devices);
412 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
413 /* at the end of the list */
414 if (&next->list == &gpio_devices)
417 /* add between prev and next */
418 if (prev->base + prev->ngpio <= gdev->base
419 && gdev->base + gdev->ngpio <= next->base) {
420 list_add_rcu(&gdev->list, &prev->list);
425 synchronize_srcu(&gpio_devices_srcu);
431 * Convert a GPIO name to its descriptor
432 * Note that there is no guarantee that GPIO names are globally unique!
433 * Hence this function will return, if it exists, a reference to the first GPIO
434 * line found that matches the given name.
436 static struct gpio_desc *gpio_name_to_desc(const char * const name)
438 struct gpio_device *gdev;
439 struct gpio_desc *desc;
440 struct gpio_chip *gc;
445 guard(srcu)(&gpio_devices_srcu);
447 list_for_each_entry_srcu(gdev, &gpio_devices, list,
448 srcu_read_lock_held(&gpio_devices_srcu)) {
449 guard(srcu)(&gdev->srcu);
451 gc = srcu_dereference(gdev->chip, &gdev->srcu);
455 for_each_gpio_desc(gc, desc) {
456 if (desc->name && !strcmp(desc->name, name))
465 * Take the names from gc->names and assign them to their GPIO descriptors.
466 * Warn if a name is already used for a GPIO line on a different GPIO chip.
469 * 1. Non-unique names are still accepted,
470 * 2. Name collisions within the same GPIO chip are not reported.
472 static int gpiochip_set_desc_names(struct gpio_chip *gc)
474 struct gpio_device *gdev = gc->gpiodev;
477 /* First check all names if they are unique */
478 for (i = 0; i != gc->ngpio; ++i) {
479 struct gpio_desc *gpio;
481 gpio = gpio_name_to_desc(gc->names[i]);
484 "Detected name collision for GPIO name '%s'\n",
488 /* Then add all names to the GPIO descriptors */
489 for (i = 0; i != gc->ngpio; ++i)
490 gdev->descs[i].name = gc->names[i];
496 * gpiochip_set_names - Set GPIO line names using device properties
497 * @chip: GPIO chip whose lines should be named, if possible
499 * Looks for device property "gpio-line-names" and if it exists assigns
500 * GPIO line names for the chip. The memory allocated for the assigned
501 * names belong to the underlying firmware node and should not be released
504 static int gpiochip_set_names(struct gpio_chip *chip)
506 struct gpio_device *gdev = chip->gpiodev;
507 struct device *dev = &gdev->dev;
512 count = device_property_string_array_count(dev, "gpio-line-names");
517 * When offset is set in the driver side we assume the driver internally
518 * is using more than one gpiochip per the same device. We have to stop
519 * setting friendly names if the specified ones with 'gpio-line-names'
520 * are less than the offset in the device itself. This means all the
521 * lines are not present for every single pin within all the internal
524 if (count <= chip->offset) {
525 dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
526 count, chip->offset);
530 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
534 ret = device_property_read_string_array(dev, "gpio-line-names",
537 dev_warn(dev, "failed to read GPIO line names\n");
543 * When more that one gpiochip per device is used, 'count' can
544 * contain at most number gpiochips x chip->ngpio. We have to
545 * correctly distribute all defined lines taking into account
546 * chip->offset as starting point from where we will assign
547 * the names to pins from the 'names' array. Since property
548 * 'gpio-line-names' cannot contains gaps, we have to be sure
549 * we only assign those pins that really exists since chip->ngpio
550 * can be different of the chip->offset.
552 count = (count > chip->offset) ? count - chip->offset : count;
553 if (count > chip->ngpio)
556 for (i = 0; i < count; i++) {
558 * Allow overriding "fixed" names provided by the GPIO
559 * provider. The "fixed" names are more often than not
560 * generic and less informative than the names given in
563 if (names[chip->offset + i] && names[chip->offset + i][0])
564 gdev->descs[i].name = names[chip->offset + i];
572 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
576 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
580 /* Assume by default all GPIOs are valid */
581 bitmap_fill(p, gc->ngpio);
586 static void gpiochip_free_mask(unsigned long **p)
592 static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
594 struct device *dev = &gc->gpiodev->dev;
597 /* Format is "start, count, ..." */
598 size = device_property_count_u32(dev, "gpio-reserved-ranges");
599 if (size > 0 && size % 2 == 0)
605 static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc)
607 struct device *dev = &gc->gpiodev->dev;
612 size = gpiochip_count_reserved_ranges(gc);
616 ranges = kmalloc_array(size, sizeof(*ranges), GFP_KERNEL);
620 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges",
628 u32 count = ranges[--size];
629 u32 start = ranges[--size];
631 if (start >= gc->ngpio || start + count > gc->ngpio)
634 bitmap_clear(gc->valid_mask, start, count);
641 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
645 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
648 gc->valid_mask = gpiochip_allocate_mask(gc);
652 ret = gpiochip_apply_reserved_ranges(gc);
656 if (gc->init_valid_mask)
657 return gc->init_valid_mask(gc,
664 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
666 gpiochip_free_mask(&gc->valid_mask);
669 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
672 * Device Tree platforms are supposed to use "gpio-ranges"
673 * property. This check ensures that the ->add_pin_ranges()
674 * won't be called for them.
676 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges"))
679 if (gc->add_pin_ranges)
680 return gc->add_pin_ranges(gc);
685 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
688 /* No mask means all valid */
689 if (likely(!gc->valid_mask))
691 return test_bit(offset, gc->valid_mask);
693 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
695 static void gpiodev_release(struct device *dev)
697 struct gpio_device *gdev = to_gpio_device(dev);
700 for (i = 0; i < gdev->ngpio; i++)
701 cleanup_srcu_struct(&gdev->descs[i].srcu);
703 ida_free(&gpio_ida, gdev->id);
704 kfree_const(gdev->label);
706 cleanup_srcu_struct(&gdev->srcu);
710 static const struct device_type gpio_dev_type = {
712 .release = gpiodev_release,
715 #ifdef CONFIG_GPIO_CDEV
716 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
717 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
720 * gpiolib_cdev_register() indirectly calls device_add(), which is still
721 * required even when cdev is not selected.
723 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
724 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
727 static int gpiochip_setup_dev(struct gpio_device *gdev)
729 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
732 device_initialize(&gdev->dev);
735 * If fwnode doesn't belong to another device, it's safe to clear its
738 if (fwnode && !fwnode->dev)
739 fwnode_dev_initialized(fwnode, false);
741 ret = gcdev_register(gdev, gpio_devt);
745 ret = gpiochip_sysfs_register(gdev);
747 goto err_remove_device;
749 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
750 gdev->base + gdev->ngpio - 1, gdev->label);
755 gcdev_unregister(gdev);
759 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
761 struct gpio_desc *desc;
764 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
766 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
771 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
773 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
774 __func__, gc->label, hog->chip_hwnum, rv);
777 static void machine_gpiochip_add(struct gpio_chip *gc)
779 struct gpiod_hog *hog;
781 mutex_lock(&gpio_machine_hogs_mutex);
783 list_for_each_entry(hog, &gpio_machine_hogs, list) {
784 if (!strcmp(gc->label, hog->chip_label))
785 gpiochip_machine_hog(gc, hog);
788 mutex_unlock(&gpio_machine_hogs_mutex);
791 static void gpiochip_setup_devs(void)
793 struct gpio_device *gdev;
796 guard(srcu)(&gpio_devices_srcu);
798 list_for_each_entry_srcu(gdev, &gpio_devices, list,
799 srcu_read_lock_held(&gpio_devices_srcu)) {
800 ret = gpiochip_setup_dev(gdev);
803 "Failed to initialize gpio device (%d)\n", ret);
807 static void gpiochip_set_data(struct gpio_chip *gc, void *data)
809 gc->gpiodev->data = data;
813 * gpiochip_get_data() - get per-subdriver data for the chip
817 * The per-subdriver data for the chip.
819 void *gpiochip_get_data(struct gpio_chip *gc)
821 return gc->gpiodev->data;
823 EXPORT_SYMBOL_GPL(gpiochip_get_data);
825 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev)
827 u32 ngpios = gc->ngpio;
831 ret = device_property_read_u32(dev, "ngpios", &ngpios);
834 * -ENODATA means that there is no property found and
835 * we want to issue the error message to the user.
836 * Besides that, we want to return different error code
837 * to state that supplied value is not valid.
846 if (gc->ngpio == 0) {
847 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
851 if (gc->ngpio > FASTPATH_NGPIO)
852 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
853 gc->ngpio, FASTPATH_NGPIO);
857 EXPORT_SYMBOL_GPL(gpiochip_get_ngpios);
859 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
860 struct lock_class_key *lock_key,
861 struct lock_class_key *request_key)
863 struct gpio_device *gdev;
869 * First: allocate and populate the internal stat container, and
870 * set up the struct device.
872 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
876 gdev->dev.type = &gpio_dev_type;
877 gdev->dev.bus = &gpio_bus_type;
878 gdev->dev.parent = gc->parent;
879 rcu_assign_pointer(gdev->chip, gc);
882 gpiochip_set_data(gc, data);
885 * If the calling driver did not initialize firmware node,
886 * do it here using the parent device, if any.
889 device_set_node(&gdev->dev, gc->fwnode);
891 device_set_node(&gdev->dev, dev_fwnode(gc->parent));
893 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
899 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
903 if (gc->parent && gc->parent->driver)
904 gdev->owner = gc->parent->driver->owner;
906 /* TODO: remove chip->owner */
907 gdev->owner = gc->owner;
909 gdev->owner = THIS_MODULE;
911 ret = gpiochip_get_ngpios(gc, &gdev->dev);
913 goto err_free_dev_name;
915 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
918 goto err_free_dev_name;
921 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
927 gdev->ngpio = gc->ngpio;
928 gdev->can_sleep = gc->can_sleep;
930 scoped_guard(mutex, &gpio_devices_lock) {
932 * TODO: this allocates a Linux GPIO number base in the global
933 * GPIO numberspace for this chip. In the long run we want to
934 * get *rid* of this numberspace and use only descriptors, but
935 * it may be a pipe dream. It will not happen before we get rid
936 * of the sysfs interface anyways.
940 base = gpiochip_find_base_unlocked(gc->ngpio);
948 * TODO: it should not be necessary to reflect the
949 * assigned base outside of the GPIO subsystem. Go over
950 * drivers and see if anyone makes use of this, else
951 * drop this and assign a poison instead.
956 "Static allocation of GPIO base is deprecated, use dynamic allocation.\n");
961 ret = gpiodev_add_to_list_unlocked(gdev);
963 chip_err(gc, "GPIO integer space overlap, cannot add chip\n");
968 for (i = 0; i < gc->ngpio; i++)
969 gdev->descs[i].gdev = gdev;
971 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier);
972 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier);
974 ret = init_srcu_struct(&gdev->srcu);
976 goto err_remove_from_list;
978 #ifdef CONFIG_PINCTRL
979 INIT_LIST_HEAD(&gdev->pin_ranges);
983 ret = gpiochip_set_desc_names(gc);
985 goto err_cleanup_gdev_srcu;
987 ret = gpiochip_set_names(gc);
989 goto err_cleanup_gdev_srcu;
991 ret = gpiochip_init_valid_mask(gc);
993 goto err_cleanup_gdev_srcu;
995 for (i = 0; i < gc->ngpio; i++) {
996 struct gpio_desc *desc = &gdev->descs[i];
998 ret = init_srcu_struct(&desc->srcu);
1000 for (j = 0; j < i; j++)
1001 cleanup_srcu_struct(&gdev->descs[j].srcu);
1002 goto err_free_gpiochip_mask;
1005 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
1006 assign_bit(FLAG_IS_OUT,
1007 &desc->flags, !gc->get_direction(gc, i));
1009 assign_bit(FLAG_IS_OUT,
1010 &desc->flags, !gc->direction_input);
1014 ret = of_gpiochip_add(gc);
1016 goto err_cleanup_desc_srcu;
1018 ret = gpiochip_add_pin_ranges(gc);
1020 goto err_remove_of_chip;
1022 acpi_gpiochip_add(gc);
1024 machine_gpiochip_add(gc);
1026 ret = gpiochip_irqchip_init_valid_mask(gc);
1028 goto err_remove_acpi_chip;
1030 ret = gpiochip_irqchip_init_hw(gc);
1032 goto err_remove_acpi_chip;
1034 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
1036 goto err_remove_irqchip_mask;
1039 * By first adding the chardev, and then adding the device,
1040 * we get a device node entry in sysfs under
1041 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1042 * coldplug of device nodes and other udev business.
1043 * We can do this only if gpiolib has been initialized.
1044 * Otherwise, defer until later.
1046 if (gpiolib_initialized) {
1047 ret = gpiochip_setup_dev(gdev);
1049 goto err_remove_irqchip;
1054 gpiochip_irqchip_remove(gc);
1055 err_remove_irqchip_mask:
1056 gpiochip_irqchip_free_valid_mask(gc);
1057 err_remove_acpi_chip:
1058 acpi_gpiochip_remove(gc);
1060 gpiochip_free_hogs(gc);
1061 of_gpiochip_remove(gc);
1062 err_cleanup_desc_srcu:
1063 for (i = 0; i < gdev->ngpio; i++)
1064 cleanup_srcu_struct(&gdev->descs[i].srcu);
1065 err_free_gpiochip_mask:
1066 gpiochip_remove_pin_ranges(gc);
1067 gpiochip_free_valid_mask(gc);
1068 err_cleanup_gdev_srcu:
1069 cleanup_srcu_struct(&gdev->srcu);
1070 err_remove_from_list:
1071 scoped_guard(mutex, &gpio_devices_lock)
1072 list_del_rcu(&gdev->list);
1073 synchronize_srcu(&gpio_devices_srcu);
1074 if (gdev->dev.release) {
1075 /* release() has been registered by gpiochip_setup_dev() */
1076 gpio_device_put(gdev);
1077 goto err_print_message;
1080 kfree_const(gdev->label);
1084 kfree(dev_name(&gdev->dev));
1086 ida_free(&gpio_ida, gdev->id);
1090 /* failures here can mean systems won't boot... */
1091 if (ret != -EPROBE_DEFER) {
1092 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
1093 base, base + (int)gc->ngpio - 1,
1094 gc->label ? : "generic", ret);
1098 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1101 * gpiochip_remove() - unregister a gpio_chip
1102 * @gc: the chip to unregister
1104 * A gpio_chip with any GPIOs still requested may not be removed.
1106 void gpiochip_remove(struct gpio_chip *gc)
1108 struct gpio_device *gdev = gc->gpiodev;
1110 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1111 gpiochip_sysfs_unregister(gdev);
1112 gpiochip_free_hogs(gc);
1114 scoped_guard(mutex, &gpio_devices_lock)
1115 list_del_rcu(&gdev->list);
1116 synchronize_srcu(&gpio_devices_srcu);
1118 /* Numb the device, cancelling all outstanding operations */
1119 rcu_assign_pointer(gdev->chip, NULL);
1120 synchronize_srcu(&gdev->srcu);
1121 gpiochip_irqchip_remove(gc);
1122 acpi_gpiochip_remove(gc);
1123 of_gpiochip_remove(gc);
1124 gpiochip_remove_pin_ranges(gc);
1125 gpiochip_free_valid_mask(gc);
1127 * We accept no more calls into the driver from this point, so
1128 * NULL the driver data pointer.
1130 gpiochip_set_data(gc, NULL);
1133 * The gpiochip side puts its use of the device to rest here:
1134 * if there are no userspace clients, the chardev and device will
1135 * be removed, else it will be dangling until the last user is
1138 gcdev_unregister(gdev);
1139 gpio_device_put(gdev);
1141 EXPORT_SYMBOL_GPL(gpiochip_remove);
1144 * gpio_device_find() - find a specific GPIO device
1145 * @data: data to pass to match function
1146 * @match: Callback function to check gpio_chip
1149 * New reference to struct gpio_device.
1151 * Similar to bus_find_device(). It returns a reference to a gpio_device as
1152 * determined by a user supplied @match callback. The callback should return
1153 * 0 if the device doesn't match and non-zero if it does. If the callback
1154 * returns non-zero, this function will return to the caller and not iterate
1155 * over any more gpio_devices.
1157 * The callback takes the GPIO chip structure as argument. During the execution
1158 * of the callback function the chip is protected from being freed. TODO: This
1159 * actually has yet to be implemented.
1161 * If the function returns non-NULL, the returned reference must be freed by
1162 * the caller using gpio_device_put().
1164 struct gpio_device *gpio_device_find(const void *data,
1165 int (*match)(struct gpio_chip *gc,
1168 struct gpio_device *gdev;
1169 struct gpio_chip *gc;
1172 * Not yet but in the future the spinlock below will become a mutex.
1173 * Annotate this function before anyone tries to use it in interrupt
1174 * context like it happened with gpiochip_find().
1178 guard(srcu)(&gpio_devices_srcu);
1180 list_for_each_entry_srcu(gdev, &gpio_devices, list,
1181 srcu_read_lock_held(&gpio_devices_srcu)) {
1182 guard(srcu)(&gdev->srcu);
1184 gc = srcu_dereference(gdev->chip, &gdev->srcu);
1186 if (gc && match(gc, data))
1187 return gpio_device_get(gdev);
1192 EXPORT_SYMBOL_GPL(gpio_device_find);
1194 static int gpio_chip_match_by_label(struct gpio_chip *gc, const void *label)
1196 return gc->label && !strcmp(gc->label, label);
1200 * gpio_device_find_by_label() - wrapper around gpio_device_find() finding the
1201 * GPIO device by its backing chip's label
1202 * @label: Label to lookup
1205 * Reference to the GPIO device or NULL. Reference must be released with
1206 * gpio_device_put().
1208 struct gpio_device *gpio_device_find_by_label(const char *label)
1210 return gpio_device_find((void *)label, gpio_chip_match_by_label);
1212 EXPORT_SYMBOL_GPL(gpio_device_find_by_label);
1214 static int gpio_chip_match_by_fwnode(struct gpio_chip *gc, const void *fwnode)
1216 return device_match_fwnode(&gc->gpiodev->dev, fwnode);
1220 * gpio_device_find_by_fwnode() - wrapper around gpio_device_find() finding
1221 * the GPIO device by its fwnode
1222 * @fwnode: Firmware node to lookup
1225 * Reference to the GPIO device or NULL. Reference must be released with
1226 * gpio_device_put().
1228 struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnode)
1230 return gpio_device_find((void *)fwnode, gpio_chip_match_by_fwnode);
1232 EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode);
1235 * gpio_device_get() - Increase the reference count of this GPIO device
1236 * @gdev: GPIO device to increase the refcount for
1241 struct gpio_device *gpio_device_get(struct gpio_device *gdev)
1243 return to_gpio_device(get_device(&gdev->dev));
1245 EXPORT_SYMBOL_GPL(gpio_device_get);
1248 * gpio_device_put() - Decrease the reference count of this GPIO device and
1249 * possibly free all resources associated with it.
1250 * @gdev: GPIO device to decrease the reference count for
1252 void gpio_device_put(struct gpio_device *gdev)
1254 put_device(&gdev->dev);
1256 EXPORT_SYMBOL_GPL(gpio_device_put);
1259 * gpio_device_to_device() - Retrieve the address of the underlying struct
1261 * @gdev: GPIO device for which to return the address.
1263 * This does not increase the reference count of the GPIO device nor the
1264 * underlying struct device.
1267 * Address of struct device backing this GPIO device.
1269 struct device *gpio_device_to_device(struct gpio_device *gdev)
1273 EXPORT_SYMBOL_GPL(gpio_device_to_device);
1275 #ifdef CONFIG_GPIOLIB_IRQCHIP
1278 * The following is irqchip helper code for gpiochips.
1281 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1283 struct gpio_irq_chip *girq = &gc->irq;
1288 return girq->init_hw(gc);
1291 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1293 struct gpio_irq_chip *girq = &gc->irq;
1295 if (!girq->init_valid_mask)
1298 girq->valid_mask = gpiochip_allocate_mask(gc);
1299 if (!girq->valid_mask)
1302 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1307 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1309 gpiochip_free_mask(&gc->irq.valid_mask);
1312 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1313 unsigned int offset)
1315 if (!gpiochip_line_is_valid(gc, offset))
1317 /* No mask means all valid */
1318 if (likely(!gc->irq.valid_mask))
1320 return test_bit(offset, gc->irq.valid_mask);
1323 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1326 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1328 * @gc: the gpiochip to set the irqchip hierarchical handler to
1329 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1330 * will then percolate up to the parent
1332 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1333 struct irq_chip *irqchip)
1335 /* DT will deal with mapping each IRQ as we go along */
1336 if (is_of_node(gc->irq.fwnode))
1340 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1341 * irqs upfront instead of dynamically since we don't have the
1342 * dynamic type of allocation that hardware description languages
1343 * provide. Once all GPIO drivers using board files are gone from
1344 * the kernel we can delete this code, but for a transitional period
1345 * it is necessary to keep this around.
1347 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1351 for (i = 0; i < gc->ngpio; i++) {
1352 struct irq_fwspec fwspec;
1353 unsigned int parent_hwirq;
1354 unsigned int parent_type;
1355 struct gpio_irq_chip *girq = &gc->irq;
1358 * We call the child to parent translation function
1359 * only to check if the child IRQ is valid or not.
1360 * Just pick the rising edge type here as that is what
1361 * we likely need to support.
1363 ret = girq->child_to_parent_hwirq(gc, i,
1364 IRQ_TYPE_EDGE_RISING,
1368 chip_err(gc, "skip set-up on hwirq %d\n",
1373 fwspec.fwnode = gc->irq.fwnode;
1374 /* This is the hwirq for the GPIO line side of things */
1375 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1376 /* Just pick something */
1377 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1378 fwspec.param_count = 2;
1379 ret = irq_domain_alloc_irqs(gc->irq.domain, 1,
1380 NUMA_NO_NODE, &fwspec);
1383 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1390 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1395 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1396 struct irq_fwspec *fwspec,
1397 unsigned long *hwirq,
1400 /* We support standard DT translation */
1401 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1402 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1405 /* This is for board files and others not using DT */
1406 if (is_fwnode_irqchip(fwspec->fwnode)) {
1409 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1412 WARN_ON(*type == IRQ_TYPE_NONE);
1418 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1420 unsigned int nr_irqs,
1423 struct gpio_chip *gc = d->host_data;
1424 irq_hw_number_t hwirq;
1425 unsigned int type = IRQ_TYPE_NONE;
1426 struct irq_fwspec *fwspec = data;
1427 union gpio_irq_fwspec gpio_parent_fwspec = {};
1428 unsigned int parent_hwirq;
1429 unsigned int parent_type;
1430 struct gpio_irq_chip *girq = &gc->irq;
1434 * The nr_irqs parameter is always one except for PCI multi-MSI
1435 * so this should not happen.
1437 WARN_ON(nr_irqs != 1);
1439 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1443 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1445 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1446 &parent_hwirq, &parent_type);
1448 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1451 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1454 * We set handle_bad_irq because the .set_type() should
1455 * always be invoked and set the right type of handler.
1457 irq_domain_set_info(d,
1466 /* This parent only handles asserted level IRQs */
1467 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
1468 parent_hwirq, parent_type);
1472 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1474 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1475 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
1477 * If the parent irqdomain is msi, the interrupts have already
1478 * been allocated, so the EEXIST is good.
1480 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1484 "failed to allocate parent hwirq %d for hwirq %lu\n",
1485 parent_hwirq, hwirq);
1490 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1491 unsigned int offset)
1497 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1498 * @domain: The IRQ domain used by this IRQ chip
1499 * @data: Outermost irq_data associated with the IRQ
1500 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1502 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1503 * used as the activate function for the &struct irq_domain_ops. The host_data
1504 * for the IRQ domain must be the &struct gpio_chip.
1506 static int gpiochip_irq_domain_activate(struct irq_domain *domain,
1507 struct irq_data *data, bool reserve)
1509 struct gpio_chip *gc = domain->host_data;
1510 unsigned int hwirq = irqd_to_hwirq(data);
1512 return gpiochip_lock_as_irq(gc, hwirq);
1516 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1517 * @domain: The IRQ domain used by this IRQ chip
1518 * @data: Outermost irq_data associated with the IRQ
1520 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1521 * be used as the deactivate function for the &struct irq_domain_ops. The
1522 * host_data for the IRQ domain must be the &struct gpio_chip.
1524 static void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1525 struct irq_data *data)
1527 struct gpio_chip *gc = domain->host_data;
1528 unsigned int hwirq = irqd_to_hwirq(data);
1530 return gpiochip_unlock_as_irq(gc, hwirq);
1533 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1535 ops->activate = gpiochip_irq_domain_activate;
1536 ops->deactivate = gpiochip_irq_domain_deactivate;
1537 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1540 * We only allow overriding the translate() and free() functions for
1541 * hierarchical chips, and this should only be done if the user
1542 * really need something other than 1:1 translation for translate()
1543 * callback and free if user wants to free up any resources which
1544 * were allocated during callbacks, for example populate_parent_alloc_arg.
1546 if (!ops->translate)
1547 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1549 ops->free = irq_domain_free_irqs_common;
1552 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1554 struct irq_domain *domain;
1556 if (!gc->irq.child_to_parent_hwirq ||
1558 chip_err(gc, "missing irqdomain vital data\n");
1559 return ERR_PTR(-EINVAL);
1562 if (!gc->irq.child_offset_to_irq)
1563 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1565 if (!gc->irq.populate_parent_alloc_arg)
1566 gc->irq.populate_parent_alloc_arg =
1567 gpiochip_populate_parent_fwspec_twocell;
1569 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1571 domain = irq_domain_create_hierarchy(
1572 gc->irq.parent_domain,
1576 &gc->irq.child_irq_domain_ops,
1580 return ERR_PTR(-ENOMEM);
1582 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1587 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1589 return !!gc->irq.parent_domain;
1592 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1593 union gpio_irq_fwspec *gfwspec,
1594 unsigned int parent_hwirq,
1595 unsigned int parent_type)
1597 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1599 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1600 fwspec->param_count = 2;
1601 fwspec->param[0] = parent_hwirq;
1602 fwspec->param[1] = parent_type;
1606 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1608 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1609 union gpio_irq_fwspec *gfwspec,
1610 unsigned int parent_hwirq,
1611 unsigned int parent_type)
1613 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1615 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1616 fwspec->param_count = 4;
1617 fwspec->param[0] = 0;
1618 fwspec->param[1] = parent_hwirq;
1619 fwspec->param[2] = 0;
1620 fwspec->param[3] = parent_type;
1624 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1628 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1630 return ERR_PTR(-EINVAL);
1633 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1638 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1641 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1642 * @d: the irqdomain used by this irqchip
1643 * @irq: the global irq number used by this GPIO irqchip irq
1644 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1646 * This function will set up the mapping for a certain IRQ line on a
1647 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1648 * stored inside the gpiochip.
1650 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1651 irq_hw_number_t hwirq)
1653 struct gpio_chip *gc = d->host_data;
1656 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1659 irq_set_chip_data(irq, gc);
1661 * This lock class tells lockdep that GPIO irqs are in a different
1662 * category than their parents, so it won't report false recursion.
1664 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1665 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1666 /* Chips that use nested thread handlers have them marked */
1667 if (gc->irq.threaded)
1668 irq_set_nested_thread(irq, 1);
1669 irq_set_noprobe(irq);
1671 if (gc->irq.num_parents == 1)
1672 ret = irq_set_parent(irq, gc->irq.parents[0]);
1673 else if (gc->irq.map)
1674 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1680 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1681 * is passed as default type.
1683 if (gc->irq.default_type != IRQ_TYPE_NONE)
1684 irq_set_irq_type(irq, gc->irq.default_type);
1689 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1691 struct gpio_chip *gc = d->host_data;
1693 if (gc->irq.threaded)
1694 irq_set_nested_thread(irq, 0);
1695 irq_set_chip_and_handler(irq, NULL, NULL);
1696 irq_set_chip_data(irq, NULL);
1699 static const struct irq_domain_ops gpiochip_domain_ops = {
1700 .map = gpiochip_irq_map,
1701 .unmap = gpiochip_irq_unmap,
1702 /* Virtually all GPIO irqchips are twocell:ed */
1703 .xlate = irq_domain_xlate_twocell,
1706 static struct irq_domain *gpiochip_simple_create_domain(struct gpio_chip *gc)
1708 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1709 struct irq_domain *domain;
1711 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first,
1712 &gpiochip_domain_ops, gc);
1714 return ERR_PTR(-EINVAL);
1719 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1721 struct irq_domain *domain = gc->irq.domain;
1723 #ifdef CONFIG_GPIOLIB_IRQCHIP
1725 * Avoid race condition with other code, which tries to lookup
1726 * an IRQ before the irqchip has been properly registered,
1727 * i.e. while gpiochip is still being brought up.
1729 if (!gc->irq.initialized)
1730 return -EPROBE_DEFER;
1733 if (!gpiochip_irqchip_irq_valid(gc, offset))
1736 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1737 if (irq_domain_is_hierarchy(domain)) {
1738 struct irq_fwspec spec;
1740 spec.fwnode = domain->fwnode;
1741 spec.param_count = 2;
1742 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1743 spec.param[1] = IRQ_TYPE_NONE;
1745 return irq_create_fwspec_mapping(&spec);
1749 return irq_create_mapping(domain, offset);
1752 int gpiochip_irq_reqres(struct irq_data *d)
1754 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1755 unsigned int hwirq = irqd_to_hwirq(d);
1757 return gpiochip_reqres_irq(gc, hwirq);
1759 EXPORT_SYMBOL(gpiochip_irq_reqres);
1761 void gpiochip_irq_relres(struct irq_data *d)
1763 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1764 unsigned int hwirq = irqd_to_hwirq(d);
1766 gpiochip_relres_irq(gc, hwirq);
1768 EXPORT_SYMBOL(gpiochip_irq_relres);
1770 static void gpiochip_irq_mask(struct irq_data *d)
1772 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1773 unsigned int hwirq = irqd_to_hwirq(d);
1775 if (gc->irq.irq_mask)
1776 gc->irq.irq_mask(d);
1777 gpiochip_disable_irq(gc, hwirq);
1780 static void gpiochip_irq_unmask(struct irq_data *d)
1782 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1783 unsigned int hwirq = irqd_to_hwirq(d);
1785 gpiochip_enable_irq(gc, hwirq);
1786 if (gc->irq.irq_unmask)
1787 gc->irq.irq_unmask(d);
1790 static void gpiochip_irq_enable(struct irq_data *d)
1792 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1793 unsigned int hwirq = irqd_to_hwirq(d);
1795 gpiochip_enable_irq(gc, hwirq);
1796 gc->irq.irq_enable(d);
1799 static void gpiochip_irq_disable(struct irq_data *d)
1801 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1802 unsigned int hwirq = irqd_to_hwirq(d);
1804 gc->irq.irq_disable(d);
1805 gpiochip_disable_irq(gc, hwirq);
1808 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1810 struct irq_chip *irqchip = gc->irq.chip;
1812 if (irqchip->flags & IRQCHIP_IMMUTABLE)
1815 chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
1817 if (!irqchip->irq_request_resources &&
1818 !irqchip->irq_release_resources) {
1819 irqchip->irq_request_resources = gpiochip_irq_reqres;
1820 irqchip->irq_release_resources = gpiochip_irq_relres;
1822 if (WARN_ON(gc->irq.irq_enable))
1824 /* Check if the irqchip already has this hook... */
1825 if (irqchip->irq_enable == gpiochip_irq_enable ||
1826 irqchip->irq_mask == gpiochip_irq_mask) {
1828 * ...and if so, give a gentle warning that this is bad
1832 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1836 if (irqchip->irq_disable) {
1837 gc->irq.irq_disable = irqchip->irq_disable;
1838 irqchip->irq_disable = gpiochip_irq_disable;
1840 gc->irq.irq_mask = irqchip->irq_mask;
1841 irqchip->irq_mask = gpiochip_irq_mask;
1844 if (irqchip->irq_enable) {
1845 gc->irq.irq_enable = irqchip->irq_enable;
1846 irqchip->irq_enable = gpiochip_irq_enable;
1848 gc->irq.irq_unmask = irqchip->irq_unmask;
1849 irqchip->irq_unmask = gpiochip_irq_unmask;
1853 static int gpiochip_irqchip_add_allocated_domain(struct gpio_chip *gc,
1854 struct irq_domain *domain,
1855 bool allocated_externally)
1861 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1863 gc->to_irq = gpiochip_to_irq;
1864 gc->irq.domain = domain;
1865 gc->irq.domain_is_allocated_externally = allocated_externally;
1868 * Using barrier() here to prevent compiler from reordering
1869 * gc->irq.initialized before adding irqdomain.
1873 gc->irq.initialized = true;
1879 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1880 * @gc: the GPIO chip to add the IRQ chip to
1881 * @lock_key: lockdep class for IRQ lock
1882 * @request_key: lockdep class for IRQ request
1884 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1885 struct lock_class_key *lock_key,
1886 struct lock_class_key *request_key)
1888 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1889 struct irq_chip *irqchip = gc->irq.chip;
1890 struct irq_domain *domain;
1898 if (gc->irq.parent_handler && gc->can_sleep) {
1899 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1903 type = gc->irq.default_type;
1906 * Specifying a default trigger is a terrible idea if DT or ACPI is
1907 * used to configure the interrupts, as you may end up with
1908 * conflicting triggers. Tell the user, and reset to NONE.
1910 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1911 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1912 type = IRQ_TYPE_NONE;
1914 gc->irq.default_type = type;
1915 gc->irq.lock_key = lock_key;
1916 gc->irq.request_key = request_key;
1918 /* If a parent irqdomain is provided, let's build a hierarchy */
1919 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1920 domain = gpiochip_hierarchy_create_domain(gc);
1922 domain = gpiochip_simple_create_domain(gc);
1925 return PTR_ERR(domain);
1927 if (gc->irq.parent_handler) {
1928 for (i = 0; i < gc->irq.num_parents; i++) {
1931 if (gc->irq.per_parent_data)
1932 data = gc->irq.parent_handler_data_array[i];
1934 data = gc->irq.parent_handler_data ?: gc;
1937 * The parent IRQ chip is already using the chip_data
1938 * for this IRQ chip, so our callbacks simply use the
1941 irq_set_chained_handler_and_data(gc->irq.parents[i],
1942 gc->irq.parent_handler,
1947 gpiochip_set_irq_hooks(gc);
1949 ret = gpiochip_irqchip_add_allocated_domain(gc, domain, false);
1953 acpi_gpiochip_request_interrupts(gc);
1959 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1960 * @gc: the gpiochip to remove the irqchip from
1962 * This is called only from gpiochip_remove()
1964 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1966 struct irq_chip *irqchip = gc->irq.chip;
1967 unsigned int offset;
1969 acpi_gpiochip_free_interrupts(gc);
1971 if (irqchip && gc->irq.parent_handler) {
1972 struct gpio_irq_chip *irq = &gc->irq;
1975 for (i = 0; i < irq->num_parents; i++)
1976 irq_set_chained_handler_and_data(irq->parents[i],
1980 /* Remove all IRQ mappings and delete the domain */
1981 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) {
1984 for (offset = 0; offset < gc->ngpio; offset++) {
1985 if (!gpiochip_irqchip_irq_valid(gc, offset))
1988 irq = irq_find_mapping(gc->irq.domain, offset);
1989 irq_dispose_mapping(irq);
1992 irq_domain_remove(gc->irq.domain);
1995 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
1996 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1997 irqchip->irq_request_resources = NULL;
1998 irqchip->irq_release_resources = NULL;
2000 if (irqchip->irq_enable == gpiochip_irq_enable) {
2001 irqchip->irq_enable = gc->irq.irq_enable;
2002 irqchip->irq_disable = gc->irq.irq_disable;
2005 gc->irq.irq_enable = NULL;
2006 gc->irq.irq_disable = NULL;
2007 gc->irq.chip = NULL;
2009 gpiochip_irqchip_free_valid_mask(gc);
2013 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
2014 * @gc: the gpiochip to add the irqchip to
2015 * @domain: the irqdomain to add to the gpiochip
2017 * This function adds an IRQ domain to the gpiochip.
2019 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
2020 struct irq_domain *domain)
2022 return gpiochip_irqchip_add_allocated_domain(gc, domain, true);
2024 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
2026 #else /* CONFIG_GPIOLIB_IRQCHIP */
2028 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
2029 struct lock_class_key *lock_key,
2030 struct lock_class_key *request_key)
2034 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
2036 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
2041 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
2045 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
2048 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2051 * gpiochip_generic_request() - request the gpio function for a pin
2052 * @gc: the gpiochip owning the GPIO
2053 * @offset: the offset of the GPIO to request for GPIO function
2055 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
2057 #ifdef CONFIG_PINCTRL
2058 if (list_empty(&gc->gpiodev->pin_ranges))
2062 return pinctrl_gpio_request(gc, offset);
2064 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2067 * gpiochip_generic_free() - free the gpio function from a pin
2068 * @gc: the gpiochip to request the gpio function for
2069 * @offset: the offset of the GPIO to free from GPIO function
2071 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
2073 #ifdef CONFIG_PINCTRL
2074 if (list_empty(&gc->gpiodev->pin_ranges))
2078 pinctrl_gpio_free(gc, offset);
2080 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2083 * gpiochip_generic_config() - apply configuration for a pin
2084 * @gc: the gpiochip owning the GPIO
2085 * @offset: the offset of the GPIO to apply the configuration
2086 * @config: the configuration to be applied
2088 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
2089 unsigned long config)
2091 return pinctrl_gpio_set_config(gc, offset, config);
2093 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2095 #ifdef CONFIG_PINCTRL
2098 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2099 * @gc: the gpiochip to add the range for
2100 * @pctldev: the pin controller to map to
2101 * @gpio_offset: the start offset in the current gpio_chip number space
2102 * @pin_group: name of the pin group inside the pin controller
2104 * Calling this function directly from a DeviceTree-supported
2105 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2106 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2107 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2109 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
2110 struct pinctrl_dev *pctldev,
2111 unsigned int gpio_offset, const char *pin_group)
2113 struct gpio_pin_range *pin_range;
2114 struct gpio_device *gdev = gc->gpiodev;
2117 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2119 chip_err(gc, "failed to allocate pin ranges\n");
2123 /* Use local offset as range ID */
2124 pin_range->range.id = gpio_offset;
2125 pin_range->range.gc = gc;
2126 pin_range->range.name = gc->label;
2127 pin_range->range.base = gdev->base + gpio_offset;
2128 pin_range->pctldev = pctldev;
2130 ret = pinctrl_get_group_pins(pctldev, pin_group,
2131 &pin_range->range.pins,
2132 &pin_range->range.npins);
2138 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2140 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2141 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2142 pinctrl_dev_get_devname(pctldev), pin_group);
2144 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2148 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2151 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2152 * @gc: the gpiochip to add the range for
2153 * @pinctl_name: the dev_name() of the pin controller to map to
2154 * @gpio_offset: the start offset in the current gpio_chip number space
2155 * @pin_offset: the start offset in the pin controller number space
2156 * @npins: the number of pins from the offset of each pin space (GPIO and
2157 * pin controller) to accumulate in this range
2160 * 0 on success, or a negative error-code on failure.
2162 * Calling this function directly from a DeviceTree-supported
2163 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2164 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2165 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2167 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
2168 unsigned int gpio_offset, unsigned int pin_offset,
2171 struct gpio_pin_range *pin_range;
2172 struct gpio_device *gdev = gc->gpiodev;
2175 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2177 chip_err(gc, "failed to allocate pin ranges\n");
2181 /* Use local offset as range ID */
2182 pin_range->range.id = gpio_offset;
2183 pin_range->range.gc = gc;
2184 pin_range->range.name = gc->label;
2185 pin_range->range.base = gdev->base + gpio_offset;
2186 pin_range->range.pin_base = pin_offset;
2187 pin_range->range.npins = npins;
2188 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2190 if (IS_ERR(pin_range->pctldev)) {
2191 ret = PTR_ERR(pin_range->pctldev);
2192 chip_err(gc, "could not create pin range\n");
2196 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2197 gpio_offset, gpio_offset + npins - 1,
2199 pin_offset, pin_offset + npins - 1);
2201 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2205 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2208 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2209 * @gc: the chip to remove all the mappings for
2211 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
2213 struct gpio_pin_range *pin_range, *tmp;
2214 struct gpio_device *gdev = gc->gpiodev;
2216 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2217 list_del(&pin_range->node);
2218 pinctrl_remove_gpio_range(pin_range->pctldev,
2223 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2225 #endif /* CONFIG_PINCTRL */
2227 /* These "optional" allocation calls help prevent drivers from stomping
2228 * on each other, and help provide better diagnostics in debugfs.
2229 * They're called even less than the "set direction" calls.
2231 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2233 unsigned int offset;
2236 CLASS(gpio_chip_guard, guard)(desc);
2240 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags))
2243 /* NOTE: gpio_request() can be called in early boot,
2244 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2247 if (guard.gc->request) {
2248 offset = gpio_chip_hwgpio(desc);
2249 if (gpiochip_line_is_valid(guard.gc, offset))
2250 ret = guard.gc->request(guard.gc, offset);
2257 if (guard.gc->get_direction)
2258 gpiod_get_direction(desc);
2260 ret = desc_set_label(desc, label ? : "?");
2267 clear_bit(FLAG_REQUESTED, &desc->flags);
2272 * This descriptor validation needs to be inserted verbatim into each
2273 * function taking a descriptor, so we need to use a preprocessor
2274 * macro to avoid endless duplication. If the desc is NULL it is an
2275 * optional GPIO and calls should just bail out.
2277 static int validate_desc(const struct gpio_desc *desc, const char *func)
2283 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2284 return PTR_ERR(desc);
2290 #define VALIDATE_DESC(desc) do { \
2291 int __valid = validate_desc(desc, __func__); \
2296 #define VALIDATE_DESC_VOID(desc) do { \
2297 int __valid = validate_desc(desc, __func__); \
2302 int gpiod_request(struct gpio_desc *desc, const char *label)
2304 int ret = -EPROBE_DEFER;
2306 VALIDATE_DESC(desc);
2308 if (try_module_get(desc->gdev->owner)) {
2309 ret = gpiod_request_commit(desc, label);
2311 module_put(desc->gdev->owner);
2313 gpio_device_get(desc->gdev);
2317 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2322 static void gpiod_free_commit(struct gpio_desc *desc)
2324 unsigned long flags;
2328 CLASS(gpio_chip_guard, guard)(desc);
2330 flags = READ_ONCE(desc->flags);
2332 if (guard.gc && test_bit(FLAG_REQUESTED, &flags)) {
2334 guard.gc->free(guard.gc, gpio_chip_hwgpio(desc));
2336 clear_bit(FLAG_ACTIVE_LOW, &flags);
2337 clear_bit(FLAG_REQUESTED, &flags);
2338 clear_bit(FLAG_OPEN_DRAIN, &flags);
2339 clear_bit(FLAG_OPEN_SOURCE, &flags);
2340 clear_bit(FLAG_PULL_UP, &flags);
2341 clear_bit(FLAG_PULL_DOWN, &flags);
2342 clear_bit(FLAG_BIAS_DISABLE, &flags);
2343 clear_bit(FLAG_EDGE_RISING, &flags);
2344 clear_bit(FLAG_EDGE_FALLING, &flags);
2345 clear_bit(FLAG_IS_HOGGED, &flags);
2346 #ifdef CONFIG_OF_DYNAMIC
2347 WRITE_ONCE(desc->hog, NULL);
2349 desc_set_label(desc, NULL);
2350 WRITE_ONCE(desc->flags, flags);
2352 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED);
2356 void gpiod_free(struct gpio_desc *desc)
2358 VALIDATE_DESC_VOID(desc);
2360 gpiod_free_commit(desc);
2361 module_put(desc->gdev->owner);
2362 gpio_device_put(desc->gdev);
2366 * gpiochip_dup_line_label - Get a copy of the consumer label.
2367 * @gc: GPIO chip controlling this line.
2368 * @offset: Hardware offset of the line.
2371 * Pointer to a copy of the consumer label if the line is requested or NULL
2372 * if it's not. If a valid pointer was returned, it must be freed using
2373 * kfree(). In case of a memory allocation error, the function returns %ENOMEM.
2375 * Must not be called from atomic context.
2377 char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
2379 struct gpio_desc *desc;
2382 desc = gpiochip_get_desc(gc, offset);
2386 if (!test_bit(FLAG_REQUESTED, &desc->flags))
2389 guard(srcu)(&desc->srcu);
2391 label = kstrdup(gpiod_get_label(desc), GFP_KERNEL);
2393 return ERR_PTR(-ENOMEM);
2397 EXPORT_SYMBOL_GPL(gpiochip_dup_line_label);
2400 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2402 * @hwnum: hardware number of the GPIO for which to request the descriptor
2403 * @label: label for the GPIO
2404 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2405 * specify things like line inversion semantics with the machine flags
2406 * such as GPIO_OUT_LOW
2407 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2408 * can be used to specify consumer semantics such as open drain
2410 * Function allows GPIO chip drivers to request and use their own GPIO
2411 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2412 * function will not increase reference count of the GPIO chip module. This
2413 * allows the GPIO chip module to be unloaded as needed (we assume that the
2414 * GPIO chip driver handles freeing the GPIOs it has requested).
2417 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2420 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2423 enum gpio_lookup_flags lflags,
2424 enum gpiod_flags dflags)
2426 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2430 chip_err(gc, "failed to get GPIO descriptor\n");
2434 ret = gpiod_request_commit(desc, label);
2436 return ERR_PTR(ret);
2438 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2440 chip_err(gc, "setup of own GPIO %s failed\n", label);
2441 gpiod_free_commit(desc);
2442 return ERR_PTR(ret);
2447 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2450 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2451 * @desc: GPIO descriptor to free
2453 * Function frees the given GPIO requested previously with
2454 * gpiochip_request_own_desc().
2456 void gpiochip_free_own_desc(struct gpio_desc *desc)
2459 gpiod_free_commit(desc);
2461 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2464 * Drivers MUST set GPIO direction before making get/set calls. In
2465 * some cases this is done in early boot, before IRQs are enabled.
2467 * As a rule these aren't called more than once (except for drivers
2468 * using the open-drain emulation idiom) so these are natural places
2469 * to accumulate extra debugging checks. Note that we can't (yet)
2470 * rely on gpio_request() having been called beforehand.
2473 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2474 unsigned long config)
2476 if (!gc->set_config)
2479 return gc->set_config(gc, offset, config);
2482 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2483 enum pin_config_param mode,
2486 unsigned long config;
2488 CLASS(gpio_chip_guard, guard)(desc);
2492 config = pinconf_to_config_packed(mode, argument);
2493 return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config);
2496 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2497 enum pin_config_param mode,
2500 struct device *dev = &desc->gdev->dev;
2501 int gpio = gpio_chip_hwgpio(desc);
2504 ret = gpio_set_config_with_argument(desc, mode, argument);
2505 if (ret != -ENOTSUPP)
2509 case PIN_CONFIG_PERSIST_STATE:
2510 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2519 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2521 return gpio_set_config_with_argument(desc, mode, 0);
2524 static int gpio_set_bias(struct gpio_desc *desc)
2526 enum pin_config_param bias;
2527 unsigned long flags;
2530 flags = READ_ONCE(desc->flags);
2532 if (test_bit(FLAG_BIAS_DISABLE, &flags))
2533 bias = PIN_CONFIG_BIAS_DISABLE;
2534 else if (test_bit(FLAG_PULL_UP, &flags))
2535 bias = PIN_CONFIG_BIAS_PULL_UP;
2536 else if (test_bit(FLAG_PULL_DOWN, &flags))
2537 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2542 case PIN_CONFIG_BIAS_PULL_DOWN:
2543 case PIN_CONFIG_BIAS_PULL_UP:
2552 return gpio_set_config_with_argument_optional(desc, bias, arg);
2556 * gpio_set_debounce_timeout() - Set debounce timeout
2557 * @desc: GPIO descriptor to set the debounce timeout
2558 * @debounce: Debounce timeout in microseconds
2560 * The function calls the certain GPIO driver to set debounce timeout
2563 * Returns 0 on success, or negative error code otherwise.
2565 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2567 return gpio_set_config_with_argument_optional(desc,
2568 PIN_CONFIG_INPUT_DEBOUNCE,
2573 * gpiod_direction_input - set the GPIO direction to input
2574 * @desc: GPIO to set to input
2576 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2577 * be called safely on it.
2579 * Return 0 in case of success, else an error code.
2581 int gpiod_direction_input(struct gpio_desc *desc)
2585 VALIDATE_DESC(desc);
2587 CLASS(gpio_chip_guard, guard)(desc);
2592 * It is legal to have no .get() and .direction_input() specified if
2593 * the chip is output-only, but you can't specify .direction_input()
2594 * and not support the .get() operation, that doesn't make sense.
2596 if (!guard.gc->get && guard.gc->direction_input) {
2598 "%s: missing get() but have direction_input()\n",
2604 * If we have a .direction_input() callback, things are simple,
2605 * just call it. Else we are some input-only chip so try to check the
2606 * direction (if .get_direction() is supported) else we silently
2607 * assume we are in input mode after this.
2609 if (guard.gc->direction_input) {
2610 ret = guard.gc->direction_input(guard.gc,
2611 gpio_chip_hwgpio(desc));
2612 } else if (guard.gc->get_direction &&
2613 (guard.gc->get_direction(guard.gc,
2614 gpio_chip_hwgpio(desc)) != 1)) {
2616 "%s: missing direction_input() operation and line is output\n",
2621 clear_bit(FLAG_IS_OUT, &desc->flags);
2622 ret = gpio_set_bias(desc);
2625 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2629 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2631 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2633 int val = !!value, ret = 0;
2635 CLASS(gpio_chip_guard, guard)(desc);
2640 * It's OK not to specify .direction_output() if the gpiochip is
2641 * output-only, but if there is then not even a .set() operation it
2642 * is pretty tricky to drive the output line.
2644 if (!guard.gc->set && !guard.gc->direction_output) {
2646 "%s: missing set() and direction_output() operations\n",
2651 if (guard.gc->direction_output) {
2652 ret = guard.gc->direction_output(guard.gc,
2653 gpio_chip_hwgpio(desc), val);
2655 /* Check that we are in output mode if we can */
2656 if (guard.gc->get_direction &&
2657 guard.gc->get_direction(guard.gc, gpio_chip_hwgpio(desc))) {
2659 "%s: missing direction_output() operation\n",
2664 * If we can't actively set the direction, we are some
2665 * output-only chip, so just drive the output as desired.
2667 guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), val);
2671 set_bit(FLAG_IS_OUT, &desc->flags);
2672 trace_gpio_value(desc_to_gpio(desc), 0, val);
2673 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2678 * gpiod_direction_output_raw - set the GPIO direction to output
2679 * @desc: GPIO to set to output
2680 * @value: initial output value of the GPIO
2682 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2683 * be called safely on it. The initial value of the output must be specified
2684 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2686 * Return 0 in case of success, else an error code.
2688 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2690 VALIDATE_DESC(desc);
2691 return gpiod_direction_output_raw_commit(desc, value);
2693 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2696 * gpiod_direction_output - set the GPIO direction to output
2697 * @desc: GPIO to set to output
2698 * @value: initial output value of the GPIO
2700 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2701 * be called safely on it. The initial value of the output must be specified
2702 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2705 * Return 0 in case of success, else an error code.
2707 int gpiod_direction_output(struct gpio_desc *desc, int value)
2709 unsigned long flags;
2712 VALIDATE_DESC(desc);
2714 flags = READ_ONCE(desc->flags);
2716 if (test_bit(FLAG_ACTIVE_LOW, &flags))
2721 /* GPIOs used for enabled IRQs shall not be set as output */
2722 if (test_bit(FLAG_USED_AS_IRQ, &flags) &&
2723 test_bit(FLAG_IRQ_IS_ENABLED, &flags)) {
2725 "%s: tried to set a GPIO tied to an IRQ as output\n",
2730 if (test_bit(FLAG_OPEN_DRAIN, &flags)) {
2731 /* First see if we can enable open drain in hardware */
2732 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2734 goto set_output_value;
2735 /* Emulate open drain by not actively driving the line high */
2737 ret = gpiod_direction_input(desc);
2738 goto set_output_flag;
2740 } else if (test_bit(FLAG_OPEN_SOURCE, &flags)) {
2741 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2743 goto set_output_value;
2744 /* Emulate open source by not actively driving the line low */
2746 ret = gpiod_direction_input(desc);
2747 goto set_output_flag;
2750 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2754 ret = gpio_set_bias(desc);
2757 return gpiod_direction_output_raw_commit(desc, value);
2761 * When emulating open-source or open-drain functionalities by not
2762 * actively driving the line (setting mode to input) we still need to
2763 * set the IS_OUT flag or otherwise we won't be able to set the line
2767 set_bit(FLAG_IS_OUT, &desc->flags);
2770 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2773 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2775 * @desc: GPIO to enable.
2776 * @flags: Flags related to GPIO edge.
2778 * Return 0 in case of success, else negative error code.
2780 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2784 VALIDATE_DESC(desc);
2786 CLASS(gpio_chip_guard, guard)(desc);
2790 if (!guard.gc->en_hw_timestamp) {
2791 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2795 ret = guard.gc->en_hw_timestamp(guard.gc,
2796 gpio_chip_hwgpio(desc), flags);
2798 gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
2802 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
2805 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2807 * @desc: GPIO to disable.
2808 * @flags: Flags related to GPIO edge, same value as used during enable call.
2810 * Return 0 in case of success, else negative error code.
2812 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2816 VALIDATE_DESC(desc);
2818 CLASS(gpio_chip_guard, guard)(desc);
2822 if (!guard.gc->dis_hw_timestamp) {
2823 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2827 ret = guard.gc->dis_hw_timestamp(guard.gc, gpio_chip_hwgpio(desc),
2830 gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
2834 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
2837 * gpiod_set_config - sets @config for a GPIO
2838 * @desc: descriptor of the GPIO for which to set the configuration
2839 * @config: Same packed config format as generic pinconf
2842 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2845 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2847 VALIDATE_DESC(desc);
2849 CLASS(gpio_chip_guard, guard)(desc);
2853 return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config);
2855 EXPORT_SYMBOL_GPL(gpiod_set_config);
2858 * gpiod_set_debounce - sets @debounce time for a GPIO
2859 * @desc: descriptor of the GPIO for which to set debounce time
2860 * @debounce: debounce time in microseconds
2863 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2866 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2868 unsigned long config;
2870 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2871 return gpiod_set_config(desc, config);
2873 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2876 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2877 * @desc: descriptor of the GPIO for which to configure persistence
2878 * @transitory: True to lose state on suspend or reset, false for persistence
2881 * 0 on success, otherwise a negative error code.
2883 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2885 VALIDATE_DESC(desc);
2887 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2888 * persistence state.
2890 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2892 /* If the driver supports it, set the persistence state now */
2893 return gpio_set_config_with_argument_optional(desc,
2894 PIN_CONFIG_PERSIST_STATE,
2899 * gpiod_is_active_low - test whether a GPIO is active-low or not
2900 * @desc: the gpio descriptor to test
2902 * Returns 1 if the GPIO is active-low, 0 otherwise.
2904 int gpiod_is_active_low(const struct gpio_desc *desc)
2906 VALIDATE_DESC(desc);
2907 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2909 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2912 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2913 * @desc: the gpio descriptor to change
2915 void gpiod_toggle_active_low(struct gpio_desc *desc)
2917 VALIDATE_DESC_VOID(desc);
2918 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2920 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2922 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
2924 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
2927 /* I/O calls are only valid after configuration completed; the relevant
2928 * "is this a valid GPIO" error checks should already have been done.
2930 * "Get" operations are often inlinable as reading a pin value register,
2931 * and masking the relevant bit in that register.
2933 * When "set" operations are inlinable, they involve writing that mask to
2934 * one register to set a low value, or a different register to set it high.
2935 * Otherwise locking is needed, so there may be little value to inlining.
2937 *------------------------------------------------------------------------
2939 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2940 * have requested the GPIO. That can include implicit requesting by
2941 * a direction setting call. Marking a gpio as requested locks its chip
2942 * in memory, guaranteeing that these table lookups need no more locking
2943 * and that gpiochip_remove() will fail.
2945 * REVISIT when debugging, consider adding some instrumentation to ensure
2946 * that the GPIO was actually requested.
2949 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2951 struct gpio_device *gdev;
2952 struct gpio_chip *gc;
2955 /* FIXME Unable to use gpio_chip_guard due to const desc. */
2958 guard(srcu)(&gdev->srcu);
2960 gc = srcu_dereference(gdev->chip, &gdev->srcu);
2964 value = gpio_chip_get_value(gc, desc);
2965 value = value < 0 ? value : !!value;
2966 trace_gpio_value(desc_to_gpio(desc), 1, value);
2970 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2971 unsigned long *mask, unsigned long *bits)
2973 if (gc->get_multiple)
2974 return gc->get_multiple(gc, mask, bits);
2978 for_each_set_bit(i, mask, gc->ngpio) {
2979 value = gc->get(gc, i);
2982 __assign_bit(i, bits, value);
2989 /* The 'other' chip must be protected with its GPIO device's SRCU. */
2990 static bool gpio_device_chip_cmp(struct gpio_device *gdev, struct gpio_chip *gc)
2992 guard(srcu)(&gdev->srcu);
2994 return gc == srcu_dereference(gdev->chip, &gdev->srcu);
2997 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2998 unsigned int array_size,
2999 struct gpio_desc **desc_array,
3000 struct gpio_array *array_info,
3001 unsigned long *value_bitmap)
3006 * Validate array_info against desc_array and its size.
3007 * It should immediately follow desc_array if both
3008 * have been obtained from the same gpiod_get_array() call.
3010 if (array_info && array_info->desc == desc_array &&
3011 array_size <= array_info->size &&
3012 (void *)array_info == desc_array + array_info->size) {
3014 WARN_ON(array_info->chip->can_sleep);
3016 ret = gpio_chip_get_multiple(array_info->chip,
3017 array_info->get_mask,
3022 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3023 bitmap_xor(value_bitmap, value_bitmap,
3024 array_info->invert_mask, array_size);
3026 i = find_first_zero_bit(array_info->get_mask, array_size);
3027 if (i == array_size)
3033 while (i < array_size) {
3034 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3035 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3036 unsigned long *mask, *bits;
3039 CLASS(gpio_chip_guard, guard)(desc_array[i]);
3043 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) {
3044 mask = fastpath_mask;
3045 bits = fastpath_bits;
3047 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3049 mask = bitmap_alloc(guard.gc->ngpio, flags);
3053 bits = bitmap_alloc(guard.gc->ngpio, flags);
3060 bitmap_zero(mask, guard.gc->ngpio);
3063 WARN_ON(guard.gc->can_sleep);
3065 /* collect all inputs belonging to the same chip */
3068 const struct gpio_desc *desc = desc_array[i];
3069 int hwgpio = gpio_chip_hwgpio(desc);
3071 __set_bit(hwgpio, mask);
3075 i = find_next_zero_bit(array_info->get_mask,
3077 } while ((i < array_size) &&
3078 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc));
3080 ret = gpio_chip_get_multiple(guard.gc, mask, bits);
3082 if (mask != fastpath_mask)
3084 if (bits != fastpath_bits)
3089 for (j = first; j < i; ) {
3090 const struct gpio_desc *desc = desc_array[j];
3091 int hwgpio = gpio_chip_hwgpio(desc);
3092 int value = test_bit(hwgpio, bits);
3094 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3096 __assign_bit(j, value_bitmap, value);
3097 trace_gpio_value(desc_to_gpio(desc), 1, value);
3101 j = find_next_zero_bit(array_info->get_mask, i,
3105 if (mask != fastpath_mask)
3107 if (bits != fastpath_bits)
3114 * gpiod_get_raw_value() - return a gpio's raw value
3115 * @desc: gpio whose value will be returned
3117 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3118 * its ACTIVE_LOW status, or negative errno on failure.
3120 * This function can be called from contexts where we cannot sleep, and will
3121 * complain if the GPIO chip functions potentially sleep.
3123 int gpiod_get_raw_value(const struct gpio_desc *desc)
3125 VALIDATE_DESC(desc);
3126 /* Should be using gpiod_get_raw_value_cansleep() */
3127 WARN_ON(desc->gdev->can_sleep);
3128 return gpiod_get_raw_value_commit(desc);
3130 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
3133 * gpiod_get_value() - return a gpio's value
3134 * @desc: gpio whose value will be returned
3136 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3137 * account, or negative errno on failure.
3139 * This function can be called from contexts where we cannot sleep, and will
3140 * complain if the GPIO chip functions potentially sleep.
3142 int gpiod_get_value(const struct gpio_desc *desc)
3146 VALIDATE_DESC(desc);
3147 /* Should be using gpiod_get_value_cansleep() */
3148 WARN_ON(desc->gdev->can_sleep);
3150 value = gpiod_get_raw_value_commit(desc);
3154 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3159 EXPORT_SYMBOL_GPL(gpiod_get_value);
3162 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3163 * @array_size: number of elements in the descriptor array / value bitmap
3164 * @desc_array: array of GPIO descriptors whose values will be read
3165 * @array_info: information on applicability of fast bitmap processing path
3166 * @value_bitmap: bitmap to store the read values
3168 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3169 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3170 * else an error code.
3172 * This function can be called from contexts where we cannot sleep,
3173 * and it will complain if the GPIO chip functions potentially sleep.
3175 int gpiod_get_raw_array_value(unsigned int array_size,
3176 struct gpio_desc **desc_array,
3177 struct gpio_array *array_info,
3178 unsigned long *value_bitmap)
3182 return gpiod_get_array_value_complex(true, false, array_size,
3183 desc_array, array_info,
3186 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3189 * gpiod_get_array_value() - read values from an array of GPIOs
3190 * @array_size: number of elements in the descriptor array / value bitmap
3191 * @desc_array: array of GPIO descriptors whose values will be read
3192 * @array_info: information on applicability of fast bitmap processing path
3193 * @value_bitmap: bitmap to store the read values
3195 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3196 * into account. Return 0 in case of success, else an error code.
3198 * This function can be called from contexts where we cannot sleep,
3199 * and it will complain if the GPIO chip functions potentially sleep.
3201 int gpiod_get_array_value(unsigned int array_size,
3202 struct gpio_desc **desc_array,
3203 struct gpio_array *array_info,
3204 unsigned long *value_bitmap)
3208 return gpiod_get_array_value_complex(false, false, array_size,
3209 desc_array, array_info,
3212 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3215 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3216 * @desc: gpio descriptor whose state need to be set.
3217 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3219 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
3221 int ret = 0, offset = gpio_chip_hwgpio(desc);
3223 CLASS(gpio_chip_guard, guard)(desc);
3228 ret = guard.gc->direction_input(guard.gc, offset);
3230 ret = guard.gc->direction_output(guard.gc, offset, 0);
3232 set_bit(FLAG_IS_OUT, &desc->flags);
3234 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3237 "%s: Error in set_value for open drain err %d\n",
3242 * _gpio_set_open_source_value() - Set the open source gpio's value.
3243 * @desc: gpio descriptor whose state need to be set.
3244 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3246 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3248 int ret = 0, offset = gpio_chip_hwgpio(desc);
3250 CLASS(gpio_chip_guard, guard)(desc);
3255 ret = guard.gc->direction_output(guard.gc, offset, 1);
3257 set_bit(FLAG_IS_OUT, &desc->flags);
3259 ret = guard.gc->direction_input(guard.gc, offset);
3261 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3264 "%s: Error in set_value for open source err %d\n",
3268 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3270 CLASS(gpio_chip_guard, guard)(desc);
3274 trace_gpio_value(desc_to_gpio(desc), 0, value);
3275 guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), value);
3279 * set multiple outputs on the same chip;
3280 * use the chip's set_multiple function if available;
3281 * otherwise set the outputs sequentially;
3282 * @chip: the GPIO chip we operate on
3283 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3284 * defines which outputs are to be changed
3285 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3286 * defines the values the outputs specified by mask are to be set to
3288 static void gpio_chip_set_multiple(struct gpio_chip *gc,
3289 unsigned long *mask, unsigned long *bits)
3291 if (gc->set_multiple) {
3292 gc->set_multiple(gc, mask, bits);
3296 /* set outputs if the corresponding mask bit is set */
3297 for_each_set_bit(i, mask, gc->ngpio)
3298 gc->set(gc, i, test_bit(i, bits));
3302 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3303 unsigned int array_size,
3304 struct gpio_desc **desc_array,
3305 struct gpio_array *array_info,
3306 unsigned long *value_bitmap)
3311 * Validate array_info against desc_array and its size.
3312 * It should immediately follow desc_array if both
3313 * have been obtained from the same gpiod_get_array() call.
3315 if (array_info && array_info->desc == desc_array &&
3316 array_size <= array_info->size &&
3317 (void *)array_info == desc_array + array_info->size) {
3319 WARN_ON(array_info->chip->can_sleep);
3321 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3322 bitmap_xor(value_bitmap, value_bitmap,
3323 array_info->invert_mask, array_size);
3325 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3328 i = find_first_zero_bit(array_info->set_mask, array_size);
3329 if (i == array_size)
3335 while (i < array_size) {
3336 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3337 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3338 unsigned long *mask, *bits;
3341 CLASS(gpio_chip_guard, guard)(desc_array[i]);
3345 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) {
3346 mask = fastpath_mask;
3347 bits = fastpath_bits;
3349 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3351 mask = bitmap_alloc(guard.gc->ngpio, flags);
3355 bits = bitmap_alloc(guard.gc->ngpio, flags);
3362 bitmap_zero(mask, guard.gc->ngpio);
3365 WARN_ON(guard.gc->can_sleep);
3368 struct gpio_desc *desc = desc_array[i];
3369 int hwgpio = gpio_chip_hwgpio(desc);
3370 int value = test_bit(i, value_bitmap);
3373 * Pins applicable for fast input but not for
3374 * fast output processing may have been already
3375 * inverted inside the fast path, skip them.
3377 if (!raw && !(array_info &&
3378 test_bit(i, array_info->invert_mask)) &&
3379 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3381 trace_gpio_value(desc_to_gpio(desc), 0, value);
3383 * collect all normal outputs belonging to the same chip
3384 * open drain and open source outputs are set individually
3386 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3387 gpio_set_open_drain_value_commit(desc, value);
3388 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3389 gpio_set_open_source_value_commit(desc, value);
3391 __set_bit(hwgpio, mask);
3392 __assign_bit(hwgpio, bits, value);
3398 i = find_next_zero_bit(array_info->set_mask,
3400 } while ((i < array_size) &&
3401 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc));
3402 /* push collected bits to outputs */
3404 gpio_chip_set_multiple(guard.gc, mask, bits);
3406 if (mask != fastpath_mask)
3408 if (bits != fastpath_bits)
3415 * gpiod_set_raw_value() - assign a gpio's raw value
3416 * @desc: gpio whose value will be assigned
3417 * @value: value to assign
3419 * Set the raw value of the GPIO, i.e. the value of its physical line without
3420 * regard for its ACTIVE_LOW status.
3422 * This function can be called from contexts where we cannot sleep, and will
3423 * complain if the GPIO chip functions potentially sleep.
3425 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3427 VALIDATE_DESC_VOID(desc);
3428 /* Should be using gpiod_set_raw_value_cansleep() */
3429 WARN_ON(desc->gdev->can_sleep);
3430 gpiod_set_raw_value_commit(desc, value);
3432 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3435 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3436 * @desc: the descriptor to set the value on
3437 * @value: value to set
3439 * This sets the value of a GPIO line backing a descriptor, applying
3440 * different semantic quirks like active low and open drain/source
3443 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3445 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3447 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3448 gpio_set_open_drain_value_commit(desc, value);
3449 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3450 gpio_set_open_source_value_commit(desc, value);
3452 gpiod_set_raw_value_commit(desc, value);
3456 * gpiod_set_value() - assign a gpio's value
3457 * @desc: gpio whose value will be assigned
3458 * @value: value to assign
3460 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3461 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3463 * This function can be called from contexts where we cannot sleep, and will
3464 * complain if the GPIO chip functions potentially sleep.
3466 void gpiod_set_value(struct gpio_desc *desc, int value)
3468 VALIDATE_DESC_VOID(desc);
3469 /* Should be using gpiod_set_value_cansleep() */
3470 WARN_ON(desc->gdev->can_sleep);
3471 gpiod_set_value_nocheck(desc, value);
3473 EXPORT_SYMBOL_GPL(gpiod_set_value);
3476 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3477 * @array_size: number of elements in the descriptor array / value bitmap
3478 * @desc_array: array of GPIO descriptors whose values will be assigned
3479 * @array_info: information on applicability of fast bitmap processing path
3480 * @value_bitmap: bitmap of values to assign
3482 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3483 * without regard for their ACTIVE_LOW status.
3485 * This function can be called from contexts where we cannot sleep, and will
3486 * complain if the GPIO chip functions potentially sleep.
3488 int gpiod_set_raw_array_value(unsigned int array_size,
3489 struct gpio_desc **desc_array,
3490 struct gpio_array *array_info,
3491 unsigned long *value_bitmap)
3495 return gpiod_set_array_value_complex(true, false, array_size,
3496 desc_array, array_info, value_bitmap);
3498 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3501 * gpiod_set_array_value() - assign values to an array of GPIOs
3502 * @array_size: number of elements in the descriptor array / value bitmap
3503 * @desc_array: array of GPIO descriptors whose values will be assigned
3504 * @array_info: information on applicability of fast bitmap processing path
3505 * @value_bitmap: bitmap of values to assign
3507 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3510 * This function can be called from contexts where we cannot sleep, and will
3511 * complain if the GPIO chip functions potentially sleep.
3513 int gpiod_set_array_value(unsigned int array_size,
3514 struct gpio_desc **desc_array,
3515 struct gpio_array *array_info,
3516 unsigned long *value_bitmap)
3520 return gpiod_set_array_value_complex(false, false, array_size,
3521 desc_array, array_info,
3524 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3527 * gpiod_cansleep() - report whether gpio value access may sleep
3528 * @desc: gpio to check
3531 int gpiod_cansleep(const struct gpio_desc *desc)
3533 VALIDATE_DESC(desc);
3534 return desc->gdev->can_sleep;
3536 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3539 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3540 * @desc: gpio to set the consumer name on
3541 * @name: the new consumer name
3543 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3545 VALIDATE_DESC(desc);
3547 return desc_set_label(desc, name);
3549 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3552 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3553 * @desc: gpio whose IRQ will be returned (already requested)
3555 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3558 int gpiod_to_irq(const struct gpio_desc *desc)
3560 struct gpio_device *gdev;
3561 struct gpio_chip *gc;
3565 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3566 * requires this function to not return zero on an invalid descriptor
3567 * but rather a negative error number.
3569 if (!desc || IS_ERR(desc))
3573 /* FIXME Cannot use gpio_chip_guard due to const desc. */
3574 guard(srcu)(&gdev->srcu);
3575 gc = srcu_dereference(gdev->chip, &gdev->srcu);
3579 offset = gpio_chip_hwgpio(desc);
3581 int retirq = gc->to_irq(gc, offset);
3583 /* Zero means NO_IRQ */
3589 #ifdef CONFIG_GPIOLIB_IRQCHIP
3592 * Avoid race condition with other code, which tries to lookup
3593 * an IRQ before the irqchip has been properly registered,
3594 * i.e. while gpiochip is still being brought up.
3596 return -EPROBE_DEFER;
3601 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3604 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3605 * @gc: the chip the GPIO to lock belongs to
3606 * @offset: the offset of the GPIO to lock as IRQ
3608 * This is used directly by GPIO drivers that want to lock down
3609 * a certain GPIO line to be used for IRQs.
3611 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3613 struct gpio_desc *desc;
3615 desc = gpiochip_get_desc(gc, offset);
3617 return PTR_ERR(desc);
3620 * If it's fast: flush the direction setting if something changed
3623 if (!gc->can_sleep && gc->get_direction) {
3624 int dir = gpiod_get_direction(desc);
3627 chip_err(gc, "%s: cannot get GPIO direction\n",
3633 /* To be valid for IRQ the line needs to be input or open drain */
3634 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3635 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3637 "%s: tried to flag a GPIO set as output for IRQ\n",
3642 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3643 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3647 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3650 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3651 * @gc: the chip the GPIO to lock belongs to
3652 * @offset: the offset of the GPIO to lock as IRQ
3654 * This is used directly by GPIO drivers that want to indicate
3655 * that a certain GPIO is no longer used exclusively for IRQ.
3657 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3659 struct gpio_desc *desc;
3661 desc = gpiochip_get_desc(gc, offset);
3665 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3666 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3668 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3670 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3672 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3674 if (!IS_ERR(desc) &&
3675 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3676 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3678 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3680 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3682 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3684 if (!IS_ERR(desc) &&
3685 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3687 * We must not be output when using IRQ UNLESS we are
3690 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3691 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3692 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3695 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3697 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3699 if (offset >= gc->ngpio)
3702 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3704 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3706 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3710 if (!try_module_get(gc->gpiodev->owner))
3713 ret = gpiochip_lock_as_irq(gc, offset);
3715 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3716 module_put(gc->gpiodev->owner);
3721 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3723 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3725 gpiochip_unlock_as_irq(gc, offset);
3726 module_put(gc->gpiodev->owner);
3728 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3730 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3732 if (offset >= gc->ngpio)
3735 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3737 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3739 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3741 if (offset >= gc->ngpio)
3744 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3746 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3748 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3750 if (offset >= gc->ngpio)
3753 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3755 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3758 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3759 * @desc: gpio whose value will be returned
3761 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3762 * its ACTIVE_LOW status, or negative errno on failure.
3764 * This function is to be called from contexts that can sleep.
3766 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3769 VALIDATE_DESC(desc);
3770 return gpiod_get_raw_value_commit(desc);
3772 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3775 * gpiod_get_value_cansleep() - return a gpio's value
3776 * @desc: gpio whose value will be returned
3778 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3779 * account, or negative errno on failure.
3781 * This function is to be called from contexts that can sleep.
3783 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3788 VALIDATE_DESC(desc);
3789 value = gpiod_get_raw_value_commit(desc);
3793 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3798 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3801 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3802 * @array_size: number of elements in the descriptor array / value bitmap
3803 * @desc_array: array of GPIO descriptors whose values will be read
3804 * @array_info: information on applicability of fast bitmap processing path
3805 * @value_bitmap: bitmap to store the read values
3807 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3808 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3809 * else an error code.
3811 * This function is to be called from contexts that can sleep.
3813 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3814 struct gpio_desc **desc_array,
3815 struct gpio_array *array_info,
3816 unsigned long *value_bitmap)
3821 return gpiod_get_array_value_complex(true, true, array_size,
3822 desc_array, array_info,
3825 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3828 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3829 * @array_size: number of elements in the descriptor array / value bitmap
3830 * @desc_array: array of GPIO descriptors whose values will be read
3831 * @array_info: information on applicability of fast bitmap processing path
3832 * @value_bitmap: bitmap to store the read values
3834 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3835 * into account. Return 0 in case of success, else an error code.
3837 * This function is to be called from contexts that can sleep.
3839 int gpiod_get_array_value_cansleep(unsigned int array_size,
3840 struct gpio_desc **desc_array,
3841 struct gpio_array *array_info,
3842 unsigned long *value_bitmap)
3847 return gpiod_get_array_value_complex(false, true, array_size,
3848 desc_array, array_info,
3851 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3854 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3855 * @desc: gpio whose value will be assigned
3856 * @value: value to assign
3858 * Set the raw value of the GPIO, i.e. the value of its physical line without
3859 * regard for its ACTIVE_LOW status.
3861 * This function is to be called from contexts that can sleep.
3863 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3866 VALIDATE_DESC_VOID(desc);
3867 gpiod_set_raw_value_commit(desc, value);
3869 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3872 * gpiod_set_value_cansleep() - assign a gpio's value
3873 * @desc: gpio whose value will be assigned
3874 * @value: value to assign
3876 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3879 * This function is to be called from contexts that can sleep.
3881 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3884 VALIDATE_DESC_VOID(desc);
3885 gpiod_set_value_nocheck(desc, value);
3887 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3890 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3891 * @array_size: number of elements in the descriptor array / value bitmap
3892 * @desc_array: array of GPIO descriptors whose values will be assigned
3893 * @array_info: information on applicability of fast bitmap processing path
3894 * @value_bitmap: bitmap of values to assign
3896 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3897 * without regard for their ACTIVE_LOW status.
3899 * This function is to be called from contexts that can sleep.
3901 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3902 struct gpio_desc **desc_array,
3903 struct gpio_array *array_info,
3904 unsigned long *value_bitmap)
3909 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3910 array_info, value_bitmap);
3912 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3915 * gpiod_add_lookup_tables() - register GPIO device consumers
3916 * @tables: list of tables of consumers to register
3917 * @n: number of tables in the list
3919 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3923 mutex_lock(&gpio_lookup_lock);
3925 for (i = 0; i < n; i++)
3926 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3928 mutex_unlock(&gpio_lookup_lock);
3932 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3933 * @array_size: number of elements in the descriptor array / value bitmap
3934 * @desc_array: array of GPIO descriptors whose values will be assigned
3935 * @array_info: information on applicability of fast bitmap processing path
3936 * @value_bitmap: bitmap of values to assign
3938 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3941 * This function is to be called from contexts that can sleep.
3943 int gpiod_set_array_value_cansleep(unsigned int array_size,
3944 struct gpio_desc **desc_array,
3945 struct gpio_array *array_info,
3946 unsigned long *value_bitmap)
3951 return gpiod_set_array_value_complex(false, true, array_size,
3952 desc_array, array_info,
3955 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3957 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action)
3959 blocking_notifier_call_chain(&desc->gdev->line_state_notifier,
3964 * gpiod_add_lookup_table() - register GPIO device consumers
3965 * @table: table of consumers to register
3967 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3969 gpiod_add_lookup_tables(&table, 1);
3971 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3974 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3975 * @table: table of consumers to unregister
3977 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3979 /* Nothing to remove */
3983 mutex_lock(&gpio_lookup_lock);
3985 list_del(&table->list);
3987 mutex_unlock(&gpio_lookup_lock);
3989 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3992 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3993 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3995 void gpiod_add_hogs(struct gpiod_hog *hogs)
3997 struct gpiod_hog *hog;
3999 mutex_lock(&gpio_machine_hogs_mutex);
4001 for (hog = &hogs[0]; hog->chip_label; hog++) {
4002 list_add_tail(&hog->list, &gpio_machine_hogs);
4005 * The chip may have been registered earlier, so check if it
4006 * exists and, if so, try to hog the line now.
4008 struct gpio_device *gdev __free(gpio_device_put) =
4009 gpio_device_find_by_label(hog->chip_label);
4011 gpiochip_machine_hog(gpio_device_get_chip(gdev), hog);
4014 mutex_unlock(&gpio_machine_hogs_mutex);
4016 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4018 void gpiod_remove_hogs(struct gpiod_hog *hogs)
4020 struct gpiod_hog *hog;
4022 mutex_lock(&gpio_machine_hogs_mutex);
4023 for (hog = &hogs[0]; hog->chip_label; hog++)
4024 list_del(&hog->list);
4025 mutex_unlock(&gpio_machine_hogs_mutex);
4027 EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
4029 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4031 const char *dev_id = dev ? dev_name(dev) : NULL;
4032 struct gpiod_lookup_table *table;
4034 list_for_each_entry(table, &gpio_lookup_list, list) {
4035 if (table->dev_id && dev_id) {
4037 * Valid strings on both ends, must be identical to have
4040 if (!strcmp(table->dev_id, dev_id))
4044 * One of the pointers is NULL, so both must be to have
4047 if (dev_id == table->dev_id)
4055 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
4056 unsigned int idx, unsigned long *flags)
4058 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4059 struct gpiod_lookup_table *table;
4060 struct gpiod_lookup *p;
4061 struct gpio_chip *gc;
4063 guard(mutex)(&gpio_lookup_lock);
4065 table = gpiod_find_lookup_table(dev);
4069 for (p = &table->table[0]; p->key; p++) {
4070 /* idx must always match exactly */
4074 /* If the lookup entry has a con_id, require exact match */
4075 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4078 if (p->chip_hwnum == U16_MAX) {
4079 desc = gpio_name_to_desc(p->key);
4085 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
4087 return ERR_PTR(-EPROBE_DEFER);
4090 struct gpio_device *gdev __free(gpio_device_put) =
4091 gpio_device_find_by_label(p->key);
4094 * As the lookup table indicates a chip with
4095 * p->key should exist, assume it may
4096 * still appear later and let the interested
4097 * consumer be probed again or let the Deferred
4098 * Probe infrastructure handle the error.
4100 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4102 return ERR_PTR(-EPROBE_DEFER);
4105 gc = gpio_device_get_chip(gdev);
4107 if (gc->ngpio <= p->chip_hwnum) {
4109 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4110 idx, p->chip_hwnum, gc->ngpio - 1,
4112 return ERR_PTR(-EINVAL);
4115 desc = gpio_device_get_desc(gdev, p->chip_hwnum);
4124 static int platform_gpio_count(struct device *dev, const char *con_id)
4126 struct gpiod_lookup_table *table;
4127 struct gpiod_lookup *p;
4128 unsigned int count = 0;
4130 scoped_guard(mutex, &gpio_lookup_lock) {
4131 table = gpiod_find_lookup_table(dev);
4135 for (p = &table->table[0]; p->key; p++) {
4136 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4137 (!con_id && !p->con_id))
4148 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
4149 struct device *consumer,
4152 enum gpiod_flags *flags,
4153 unsigned long *lookupflags)
4155 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4157 if (is_of_node(fwnode)) {
4158 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n",
4160 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags);
4161 } else if (is_acpi_node(fwnode)) {
4162 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n",
4164 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
4165 } else if (is_software_node(fwnode)) {
4166 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n",
4168 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
4174 struct gpio_desc *gpiod_find_and_request(struct device *consumer,
4175 struct fwnode_handle *fwnode,
4178 enum gpiod_flags flags,
4180 bool platform_lookup_allowed)
4182 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4184 * scoped_guard() is implemented as a for loop, meaning static
4185 * analyzers will complain about these two not being initialized.
4187 struct gpio_desc *desc = NULL;
4190 scoped_guard(srcu, &gpio_devices_srcu) {
4191 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx,
4192 &flags, &lookupflags);
4193 if (gpiod_not_found(desc) && platform_lookup_allowed) {
4195 * Either we are not using DT or ACPI, or their lookup
4196 * did not return a result. In that case, use platform
4197 * lookup as a fallback.
4200 "using lookup tables for GPIO lookup\n");
4201 desc = gpiod_find(consumer, con_id, idx, &lookupflags);
4205 dev_dbg(consumer, "No GPIO consumer %s found\n",
4211 * If a connection label was passed use that, else attempt to use
4212 * the device name as label
4214 ret = gpiod_request(desc, label);
4217 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
4218 return ERR_PTR(ret);
4221 * This happens when there are several consumers for
4222 * the same GPIO line: we just return here without
4223 * further initialization. It is a bit of a hack.
4224 * This is necessary to support fixed regulators.
4226 * FIXME: Make this more sane and safe.
4229 "nonexclusive access to GPIO for %s\n", con_id);
4233 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4235 dev_dbg(consumer, "setup of GPIO %s failed\n", con_id);
4237 return ERR_PTR(ret);
4240 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_REQUESTED);
4246 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4247 * @fwnode: handle of the firmware node
4248 * @con_id: function within the GPIO consumer
4249 * @index: index of the GPIO to obtain for the consumer
4250 * @flags: GPIO initialization flags
4251 * @label: label to attach to the requested GPIO
4253 * This function can be used for drivers that get their configuration
4254 * from opaque firmware.
4256 * The function properly finds the corresponding GPIO using whatever is the
4257 * underlying firmware interface and then makes sure that the GPIO
4258 * descriptor is requested before it is returned to the caller.
4261 * On successful request the GPIO pin is configured in accordance with
4264 * In case of error an ERR_PTR() is returned.
4266 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4269 enum gpiod_flags flags,
4272 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false);
4274 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4277 * gpiod_count - return the number of GPIOs associated with a device / function
4278 * or -ENOENT if no GPIO has been assigned to the requested function
4279 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4280 * @con_id: function within the GPIO consumer
4282 int gpiod_count(struct device *dev, const char *con_id)
4284 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4285 int count = -ENOENT;
4287 if (is_of_node(fwnode))
4288 count = of_gpio_get_count(dev, con_id);
4289 else if (is_acpi_node(fwnode))
4290 count = acpi_gpio_count(dev, con_id);
4291 else if (is_software_node(fwnode))
4292 count = swnode_gpio_count(fwnode, con_id);
4295 count = platform_gpio_count(dev, con_id);
4299 EXPORT_SYMBOL_GPL(gpiod_count);
4302 * gpiod_get - obtain a GPIO for a given GPIO function
4303 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4304 * @con_id: function within the GPIO consumer
4305 * @flags: optional GPIO initialization flags
4307 * Return the GPIO descriptor corresponding to the function con_id of device
4308 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4309 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4311 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4312 enum gpiod_flags flags)
4314 return gpiod_get_index(dev, con_id, 0, flags);
4316 EXPORT_SYMBOL_GPL(gpiod_get);
4319 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4320 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4321 * @con_id: function within the GPIO consumer
4322 * @flags: optional GPIO initialization flags
4324 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4325 * the requested function it will return NULL. This is convenient for drivers
4326 * that need to handle optional GPIOs.
4328 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4330 enum gpiod_flags flags)
4332 return gpiod_get_index_optional(dev, con_id, 0, flags);
4334 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4338 * gpiod_configure_flags - helper function to configure a given GPIO
4339 * @desc: gpio whose value will be assigned
4340 * @con_id: function within the GPIO consumer
4341 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4342 * of_find_gpio() or of_get_gpio_hog()
4343 * @dflags: gpiod_flags - optional GPIO initialization flags
4345 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4346 * requested function and/or index, or another IS_ERR() code if an error
4347 * occurred while trying to acquire the GPIO.
4349 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4350 unsigned long lflags, enum gpiod_flags dflags)
4354 if (lflags & GPIO_ACTIVE_LOW)
4355 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4357 if (lflags & GPIO_OPEN_DRAIN)
4358 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4359 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4361 * This enforces open drain mode from the consumer side.
4362 * This is necessary for some busses like I2C, but the lookup
4363 * should *REALLY* have specified them as open drain in the
4364 * first place, so print a little warning here.
4366 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4368 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4371 if (lflags & GPIO_OPEN_SOURCE)
4372 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4374 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
4375 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
4376 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
4378 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4382 if (lflags & GPIO_PULL_UP)
4383 set_bit(FLAG_PULL_UP, &desc->flags);
4384 else if (lflags & GPIO_PULL_DOWN)
4385 set_bit(FLAG_PULL_DOWN, &desc->flags);
4386 else if (lflags & GPIO_PULL_DISABLE)
4387 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
4389 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4393 /* No particular flag request, return here... */
4394 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4395 gpiod_dbg(desc, "no flags found for %s\n", con_id);
4400 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4401 ret = gpiod_direction_output(desc,
4402 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4404 ret = gpiod_direction_input(desc);
4410 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4411 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4412 * @con_id: function within the GPIO consumer
4413 * @idx: index of the GPIO to obtain in the consumer
4414 * @flags: optional GPIO initialization flags
4416 * This variant of gpiod_get() allows to access GPIOs other than the first
4417 * defined one for functions that define several GPIOs.
4419 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4420 * requested function and/or index, or another IS_ERR() code if an error
4421 * occurred while trying to acquire the GPIO.
4423 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4426 enum gpiod_flags flags)
4428 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4429 const char *devname = dev ? dev_name(dev) : "?";
4430 const char *label = con_id ?: devname;
4432 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true);
4434 EXPORT_SYMBOL_GPL(gpiod_get_index);
4437 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4439 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4440 * @con_id: function within the GPIO consumer
4441 * @index: index of the GPIO to obtain in the consumer
4442 * @flags: optional GPIO initialization flags
4444 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4445 * specified index was assigned to the requested function it will return NULL.
4446 * This is convenient for drivers that need to handle optional GPIOs.
4448 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4451 enum gpiod_flags flags)
4453 struct gpio_desc *desc;
4455 desc = gpiod_get_index(dev, con_id, index, flags);
4456 if (gpiod_not_found(desc))
4461 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4464 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4465 * @desc: gpio whose value will be assigned
4466 * @name: gpio line name
4467 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4468 * of_find_gpio() or of_get_gpio_hog()
4469 * @dflags: gpiod_flags - optional GPIO initialization flags
4471 int gpiod_hog(struct gpio_desc *desc, const char *name,
4472 unsigned long lflags, enum gpiod_flags dflags)
4474 struct gpio_device *gdev = desc->gdev;
4475 struct gpio_desc *local_desc;
4479 CLASS(gpio_chip_guard, guard)(desc);
4483 if (test_and_set_bit(FLAG_IS_HOGGED, &desc->flags))
4486 hwnum = gpio_chip_hwgpio(desc);
4488 local_desc = gpiochip_request_own_desc(guard.gc, hwnum, name,
4490 if (IS_ERR(local_desc)) {
4491 clear_bit(FLAG_IS_HOGGED, &desc->flags);
4492 ret = PTR_ERR(local_desc);
4493 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4494 name, gdev->label, hwnum, ret);
4498 gpiod_dbg(desc, "hogged as %s%s\n",
4499 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4500 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4501 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4507 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4508 * @gc: gpio chip to act on
4510 static void gpiochip_free_hogs(struct gpio_chip *gc)
4512 struct gpio_desc *desc;
4514 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
4515 gpiochip_free_own_desc(desc);
4519 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4520 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4521 * @con_id: function within the GPIO consumer
4522 * @flags: optional GPIO initialization flags
4524 * This function acquires all the GPIOs defined under a given function.
4526 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4527 * no GPIO has been assigned to the requested function, or another IS_ERR()
4528 * code if an error occurred while trying to acquire the GPIOs.
4530 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4532 enum gpiod_flags flags)
4534 struct gpio_desc *desc;
4535 struct gpio_descs *descs;
4536 struct gpio_array *array_info = NULL;
4537 struct gpio_chip *gc;
4538 int count, bitmap_size;
4541 count = gpiod_count(dev, con_id);
4543 return ERR_PTR(count);
4545 descs_size = struct_size(descs, desc, count);
4546 descs = kzalloc(descs_size, GFP_KERNEL);
4548 return ERR_PTR(-ENOMEM);
4550 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) {
4551 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4553 gpiod_put_array(descs);
4554 return ERR_CAST(desc);
4557 descs->desc[descs->ndescs] = desc;
4559 gc = gpiod_to_chip(desc);
4561 * If pin hardware number of array member 0 is also 0, select
4562 * its chip as a candidate for fast bitmap processing path.
4564 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4565 struct gpio_descs *array;
4567 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4570 array = krealloc(descs, descs_size +
4571 struct_size(array_info, invert_mask, 3 * bitmap_size),
4572 GFP_KERNEL | __GFP_ZERO);
4574 gpiod_put_array(descs);
4575 return ERR_PTR(-ENOMEM);
4580 array_info = (void *)descs + descs_size;
4581 array_info->get_mask = array_info->invert_mask +
4583 array_info->set_mask = array_info->get_mask +
4586 array_info->desc = descs->desc;
4587 array_info->size = count;
4588 array_info->chip = gc;
4589 bitmap_set(array_info->get_mask, descs->ndescs,
4590 count - descs->ndescs);
4591 bitmap_set(array_info->set_mask, descs->ndescs,
4592 count - descs->ndescs);
4593 descs->info = array_info;
4596 /* If there is no cache for fast bitmap processing path, continue */
4600 /* Unmark array members which don't belong to the 'fast' chip */
4601 if (array_info->chip != gc) {
4602 __clear_bit(descs->ndescs, array_info->get_mask);
4603 __clear_bit(descs->ndescs, array_info->set_mask);
4606 * Detect array members which belong to the 'fast' chip
4607 * but their pins are not in hardware order.
4609 else if (gpio_chip_hwgpio(desc) != descs->ndescs) {
4611 * Don't use fast path if all array members processed so
4612 * far belong to the same chip as this one but its pin
4613 * hardware number is different from its array index.
4615 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4618 __clear_bit(descs->ndescs,
4619 array_info->get_mask);
4620 __clear_bit(descs->ndescs,
4621 array_info->set_mask);
4624 /* Exclude open drain or open source from fast output */
4625 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4626 gpiochip_line_is_open_source(gc, descs->ndescs))
4627 __clear_bit(descs->ndescs,
4628 array_info->set_mask);
4629 /* Identify 'fast' pins which require invertion */
4630 if (gpiod_is_active_low(desc))
4631 __set_bit(descs->ndescs,
4632 array_info->invert_mask);
4637 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4638 array_info->chip->label, array_info->size,
4639 *array_info->get_mask, *array_info->set_mask,
4640 *array_info->invert_mask);
4643 EXPORT_SYMBOL_GPL(gpiod_get_array);
4646 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4648 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4649 * @con_id: function within the GPIO consumer
4650 * @flags: optional GPIO initialization flags
4652 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4653 * assigned to the requested function it will return NULL.
4655 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4657 enum gpiod_flags flags)
4659 struct gpio_descs *descs;
4661 descs = gpiod_get_array(dev, con_id, flags);
4662 if (gpiod_not_found(descs))
4667 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4670 * gpiod_put - dispose of a GPIO descriptor
4671 * @desc: GPIO descriptor to dispose of
4673 * No descriptor can be used after gpiod_put() has been called on it.
4675 void gpiod_put(struct gpio_desc *desc)
4680 EXPORT_SYMBOL_GPL(gpiod_put);
4683 * gpiod_put_array - dispose of multiple GPIO descriptors
4684 * @descs: struct gpio_descs containing an array of descriptors
4686 void gpiod_put_array(struct gpio_descs *descs)
4690 for (i = 0; i < descs->ndescs; i++)
4691 gpiod_put(descs->desc[i]);
4695 EXPORT_SYMBOL_GPL(gpiod_put_array);
4697 static int gpio_stub_drv_probe(struct device *dev)
4700 * The DT node of some GPIO chips have a "compatible" property, but
4701 * never have a struct device added and probed by a driver to register
4702 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4703 * the consumers of the GPIO chip to get probe deferred forever because
4704 * they will be waiting for a device associated with the GPIO chip
4705 * firmware node to get added and bound to a driver.
4707 * To allow these consumers to probe, we associate the struct
4708 * gpio_device of the GPIO chip with the firmware node and then simply
4709 * bind it to this stub driver.
4714 static struct device_driver gpio_stub_drv = {
4715 .name = "gpio_stub_drv",
4716 .bus = &gpio_bus_type,
4717 .probe = gpio_stub_drv_probe,
4720 static int __init gpiolib_dev_init(void)
4724 /* Register GPIO sysfs bus */
4725 ret = bus_register(&gpio_bus_type);
4727 pr_err("gpiolib: could not register GPIO bus type\n");
4731 ret = driver_register(&gpio_stub_drv);
4733 pr_err("gpiolib: could not register GPIO stub driver\n");
4734 bus_unregister(&gpio_bus_type);
4738 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4740 pr_err("gpiolib: failed to allocate char dev region\n");
4741 driver_unregister(&gpio_stub_drv);
4742 bus_unregister(&gpio_bus_type);
4746 gpiolib_initialized = true;
4747 gpiochip_setup_devs();
4749 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4750 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4751 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4755 core_initcall(gpiolib_dev_init);
4757 #ifdef CONFIG_DEBUG_FS
4759 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4761 bool active_low, is_irq, is_out;
4762 unsigned int gpio = gdev->base;
4763 struct gpio_desc *desc;
4764 struct gpio_chip *gc;
4767 guard(srcu)(&gdev->srcu);
4769 gc = srcu_dereference(gdev->chip, &gdev->srcu);
4771 seq_puts(s, "Underlying GPIO chip is gone\n");
4775 for_each_gpio_desc(gc, desc) {
4776 guard(srcu)(&desc->srcu);
4777 if (test_bit(FLAG_REQUESTED, &desc->flags)) {
4778 gpiod_get_direction(desc);
4779 is_out = test_bit(FLAG_IS_OUT, &desc->flags);
4780 value = gpio_chip_get_value(gc, desc);
4781 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
4782 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4783 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
4784 gpio, desc->name ?: "", gpiod_get_label(desc),
4785 is_out ? "out" : "in ",
4786 value >= 0 ? (value ? "hi" : "lo") : "? ",
4787 is_irq ? "IRQ " : "",
4788 active_low ? "ACTIVE LOW" : "");
4789 } else if (desc->name) {
4790 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
4797 struct gpiolib_seq_priv {
4802 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4804 struct gpiolib_seq_priv *priv;
4805 struct gpio_device *gdev;
4806 loff_t index = *pos;
4808 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
4813 priv->idx = srcu_read_lock(&gpio_devices_srcu);
4815 list_for_each_entry_srcu(gdev, &gpio_devices, list,
4816 srcu_read_lock_held(&gpio_devices_srcu)) {
4824 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4826 struct gpiolib_seq_priv *priv = s->private;
4827 struct gpio_device *gdev = v, *next;
4829 next = list_entry_rcu(gdev->list.next, struct gpio_device, list);
4830 gdev = &next->list == &gpio_devices ? NULL : next;
4831 priv->newline = true;
4837 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4839 struct gpiolib_seq_priv *priv = s->private;
4841 srcu_read_unlock(&gpio_devices_srcu, priv->idx);
4845 static int gpiolib_seq_show(struct seq_file *s, void *v)
4847 struct gpiolib_seq_priv *priv = s->private;
4848 struct gpio_device *gdev = v;
4849 struct gpio_chip *gc;
4850 struct device *parent;
4852 guard(srcu)(&gdev->srcu);
4854 gc = srcu_dereference(gdev->chip, &gdev->srcu);
4856 seq_printf(s, "%s%s: (dangling chip)",
4857 priv->newline ? "\n" : "",
4858 dev_name(&gdev->dev));
4862 seq_printf(s, "%s%s: GPIOs %d-%d", priv->newline ? "\n" : "",
4863 dev_name(&gdev->dev),
4864 gdev->base, gdev->base + gdev->ngpio - 1);
4865 parent = gc->parent;
4867 seq_printf(s, ", parent: %s/%s",
4868 parent->bus ? parent->bus->name : "no-bus",
4871 seq_printf(s, ", %s", gc->label);
4873 seq_printf(s, ", can sleep");
4874 seq_printf(s, ":\n");
4877 gc->dbg_show(s, gc);
4879 gpiolib_dbg_show(s, gdev);
4884 static const struct seq_operations gpiolib_sops = {
4885 .start = gpiolib_seq_start,
4886 .next = gpiolib_seq_next,
4887 .stop = gpiolib_seq_stop,
4888 .show = gpiolib_seq_show,
4890 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4892 static int __init gpiolib_debugfs_init(void)
4894 /* /sys/kernel/debug/gpio */
4895 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4898 subsys_initcall(gpiolib_debugfs_init);
4900 #endif /* DEBUG_FS */