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;
864 unsigned int desc_index;
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 (desc_index = 0; desc_index < gc->ngpio; desc_index++)
969 gdev->descs[desc_index].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 (desc_index = 0; desc_index < gc->ngpio; desc_index++) {
996 struct gpio_desc *desc = &gdev->descs[desc_index];
998 ret = init_srcu_struct(&desc->srcu);
1000 goto err_cleanup_desc_srcu;
1002 if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) {
1003 assign_bit(FLAG_IS_OUT,
1004 &desc->flags, !gc->get_direction(gc, desc_index));
1006 assign_bit(FLAG_IS_OUT,
1007 &desc->flags, !gc->direction_input);
1011 ret = of_gpiochip_add(gc);
1013 goto err_cleanup_desc_srcu;
1015 ret = gpiochip_add_pin_ranges(gc);
1017 goto err_remove_of_chip;
1019 acpi_gpiochip_add(gc);
1021 machine_gpiochip_add(gc);
1023 ret = gpiochip_irqchip_init_valid_mask(gc);
1027 ret = gpiochip_irqchip_init_hw(gc);
1029 goto err_remove_irqchip_mask;
1031 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
1033 goto err_remove_irqchip_mask;
1036 * By first adding the chardev, and then adding the device,
1037 * we get a device node entry in sysfs under
1038 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1039 * coldplug of device nodes and other udev business.
1040 * We can do this only if gpiolib has been initialized.
1041 * Otherwise, defer until later.
1043 if (gpiolib_initialized) {
1044 ret = gpiochip_setup_dev(gdev);
1046 goto err_remove_irqchip;
1051 gpiochip_irqchip_remove(gc);
1052 err_remove_irqchip_mask:
1053 gpiochip_irqchip_free_valid_mask(gc);
1055 gpiochip_free_hogs(gc);
1056 acpi_gpiochip_remove(gc);
1057 gpiochip_remove_pin_ranges(gc);
1059 of_gpiochip_remove(gc);
1060 err_cleanup_desc_srcu:
1061 while (desc_index--)
1062 cleanup_srcu_struct(&gdev->descs[desc_index].srcu);
1063 gpiochip_free_valid_mask(gc);
1064 err_cleanup_gdev_srcu:
1065 cleanup_srcu_struct(&gdev->srcu);
1066 err_remove_from_list:
1067 scoped_guard(mutex, &gpio_devices_lock)
1068 list_del_rcu(&gdev->list);
1069 synchronize_srcu(&gpio_devices_srcu);
1070 if (gdev->dev.release) {
1071 /* release() has been registered by gpiochip_setup_dev() */
1072 gpio_device_put(gdev);
1073 goto err_print_message;
1076 kfree_const(gdev->label);
1080 kfree(dev_name(&gdev->dev));
1082 ida_free(&gpio_ida, gdev->id);
1086 /* failures here can mean systems won't boot... */
1087 if (ret != -EPROBE_DEFER) {
1088 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
1089 base, base + (int)gc->ngpio - 1,
1090 gc->label ? : "generic", ret);
1094 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1097 * gpiochip_remove() - unregister a gpio_chip
1098 * @gc: the chip to unregister
1100 * A gpio_chip with any GPIOs still requested may not be removed.
1102 void gpiochip_remove(struct gpio_chip *gc)
1104 struct gpio_device *gdev = gc->gpiodev;
1106 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1107 gpiochip_sysfs_unregister(gdev);
1108 gpiochip_free_hogs(gc);
1110 scoped_guard(mutex, &gpio_devices_lock)
1111 list_del_rcu(&gdev->list);
1112 synchronize_srcu(&gpio_devices_srcu);
1114 /* Numb the device, cancelling all outstanding operations */
1115 rcu_assign_pointer(gdev->chip, NULL);
1116 synchronize_srcu(&gdev->srcu);
1117 gpiochip_irqchip_remove(gc);
1118 acpi_gpiochip_remove(gc);
1119 of_gpiochip_remove(gc);
1120 gpiochip_remove_pin_ranges(gc);
1121 gpiochip_free_valid_mask(gc);
1123 * We accept no more calls into the driver from this point, so
1124 * NULL the driver data pointer.
1126 gpiochip_set_data(gc, NULL);
1129 * The gpiochip side puts its use of the device to rest here:
1130 * if there are no userspace clients, the chardev and device will
1131 * be removed, else it will be dangling until the last user is
1134 gcdev_unregister(gdev);
1135 gpio_device_put(gdev);
1137 EXPORT_SYMBOL_GPL(gpiochip_remove);
1140 * gpio_device_find() - find a specific GPIO device
1141 * @data: data to pass to match function
1142 * @match: Callback function to check gpio_chip
1145 * New reference to struct gpio_device.
1147 * Similar to bus_find_device(). It returns a reference to a gpio_device as
1148 * determined by a user supplied @match callback. The callback should return
1149 * 0 if the device doesn't match and non-zero if it does. If the callback
1150 * returns non-zero, this function will return to the caller and not iterate
1151 * over any more gpio_devices.
1153 * The callback takes the GPIO chip structure as argument. During the execution
1154 * of the callback function the chip is protected from being freed. TODO: This
1155 * actually has yet to be implemented.
1157 * If the function returns non-NULL, the returned reference must be freed by
1158 * the caller using gpio_device_put().
1160 struct gpio_device *gpio_device_find(const void *data,
1161 int (*match)(struct gpio_chip *gc,
1164 struct gpio_device *gdev;
1165 struct gpio_chip *gc;
1168 * Not yet but in the future the spinlock below will become a mutex.
1169 * Annotate this function before anyone tries to use it in interrupt
1170 * context like it happened with gpiochip_find().
1174 guard(srcu)(&gpio_devices_srcu);
1176 list_for_each_entry_srcu(gdev, &gpio_devices, list,
1177 srcu_read_lock_held(&gpio_devices_srcu)) {
1178 guard(srcu)(&gdev->srcu);
1180 gc = srcu_dereference(gdev->chip, &gdev->srcu);
1182 if (gc && match(gc, data))
1183 return gpio_device_get(gdev);
1188 EXPORT_SYMBOL_GPL(gpio_device_find);
1190 static int gpio_chip_match_by_label(struct gpio_chip *gc, const void *label)
1192 return gc->label && !strcmp(gc->label, label);
1196 * gpio_device_find_by_label() - wrapper around gpio_device_find() finding the
1197 * GPIO device by its backing chip's label
1198 * @label: Label to lookup
1201 * Reference to the GPIO device or NULL. Reference must be released with
1202 * gpio_device_put().
1204 struct gpio_device *gpio_device_find_by_label(const char *label)
1206 return gpio_device_find((void *)label, gpio_chip_match_by_label);
1208 EXPORT_SYMBOL_GPL(gpio_device_find_by_label);
1210 static int gpio_chip_match_by_fwnode(struct gpio_chip *gc, const void *fwnode)
1212 return device_match_fwnode(&gc->gpiodev->dev, fwnode);
1216 * gpio_device_find_by_fwnode() - wrapper around gpio_device_find() finding
1217 * the GPIO device by its fwnode
1218 * @fwnode: Firmware node to lookup
1221 * Reference to the GPIO device or NULL. Reference must be released with
1222 * gpio_device_put().
1224 struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnode)
1226 return gpio_device_find((void *)fwnode, gpio_chip_match_by_fwnode);
1228 EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode);
1231 * gpio_device_get() - Increase the reference count of this GPIO device
1232 * @gdev: GPIO device to increase the refcount for
1237 struct gpio_device *gpio_device_get(struct gpio_device *gdev)
1239 return to_gpio_device(get_device(&gdev->dev));
1241 EXPORT_SYMBOL_GPL(gpio_device_get);
1244 * gpio_device_put() - Decrease the reference count of this GPIO device and
1245 * possibly free all resources associated with it.
1246 * @gdev: GPIO device to decrease the reference count for
1248 void gpio_device_put(struct gpio_device *gdev)
1250 put_device(&gdev->dev);
1252 EXPORT_SYMBOL_GPL(gpio_device_put);
1255 * gpio_device_to_device() - Retrieve the address of the underlying struct
1257 * @gdev: GPIO device for which to return the address.
1259 * This does not increase the reference count of the GPIO device nor the
1260 * underlying struct device.
1263 * Address of struct device backing this GPIO device.
1265 struct device *gpio_device_to_device(struct gpio_device *gdev)
1269 EXPORT_SYMBOL_GPL(gpio_device_to_device);
1271 #ifdef CONFIG_GPIOLIB_IRQCHIP
1274 * The following is irqchip helper code for gpiochips.
1277 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1279 struct gpio_irq_chip *girq = &gc->irq;
1284 return girq->init_hw(gc);
1287 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1289 struct gpio_irq_chip *girq = &gc->irq;
1291 if (!girq->init_valid_mask)
1294 girq->valid_mask = gpiochip_allocate_mask(gc);
1295 if (!girq->valid_mask)
1298 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1303 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1305 gpiochip_free_mask(&gc->irq.valid_mask);
1308 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1309 unsigned int offset)
1311 if (!gpiochip_line_is_valid(gc, offset))
1313 /* No mask means all valid */
1314 if (likely(!gc->irq.valid_mask))
1316 return test_bit(offset, gc->irq.valid_mask);
1319 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1322 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1324 * @gc: the gpiochip to set the irqchip hierarchical handler to
1325 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1326 * will then percolate up to the parent
1328 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1329 struct irq_chip *irqchip)
1331 /* DT will deal with mapping each IRQ as we go along */
1332 if (is_of_node(gc->irq.fwnode))
1336 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1337 * irqs upfront instead of dynamically since we don't have the
1338 * dynamic type of allocation that hardware description languages
1339 * provide. Once all GPIO drivers using board files are gone from
1340 * the kernel we can delete this code, but for a transitional period
1341 * it is necessary to keep this around.
1343 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1347 for (i = 0; i < gc->ngpio; i++) {
1348 struct irq_fwspec fwspec;
1349 unsigned int parent_hwirq;
1350 unsigned int parent_type;
1351 struct gpio_irq_chip *girq = &gc->irq;
1354 * We call the child to parent translation function
1355 * only to check if the child IRQ is valid or not.
1356 * Just pick the rising edge type here as that is what
1357 * we likely need to support.
1359 ret = girq->child_to_parent_hwirq(gc, i,
1360 IRQ_TYPE_EDGE_RISING,
1364 chip_err(gc, "skip set-up on hwirq %d\n",
1369 fwspec.fwnode = gc->irq.fwnode;
1370 /* This is the hwirq for the GPIO line side of things */
1371 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1372 /* Just pick something */
1373 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1374 fwspec.param_count = 2;
1375 ret = irq_domain_alloc_irqs(gc->irq.domain, 1,
1376 NUMA_NO_NODE, &fwspec);
1379 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1386 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1391 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1392 struct irq_fwspec *fwspec,
1393 unsigned long *hwirq,
1396 /* We support standard DT translation */
1397 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1398 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1401 /* This is for board files and others not using DT */
1402 if (is_fwnode_irqchip(fwspec->fwnode)) {
1405 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1408 WARN_ON(*type == IRQ_TYPE_NONE);
1414 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1416 unsigned int nr_irqs,
1419 struct gpio_chip *gc = d->host_data;
1420 irq_hw_number_t hwirq;
1421 unsigned int type = IRQ_TYPE_NONE;
1422 struct irq_fwspec *fwspec = data;
1423 union gpio_irq_fwspec gpio_parent_fwspec = {};
1424 unsigned int parent_hwirq;
1425 unsigned int parent_type;
1426 struct gpio_irq_chip *girq = &gc->irq;
1430 * The nr_irqs parameter is always one except for PCI multi-MSI
1431 * so this should not happen.
1433 WARN_ON(nr_irqs != 1);
1435 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1439 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1441 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1442 &parent_hwirq, &parent_type);
1444 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1447 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1450 * We set handle_bad_irq because the .set_type() should
1451 * always be invoked and set the right type of handler.
1453 irq_domain_set_info(d,
1462 /* This parent only handles asserted level IRQs */
1463 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
1464 parent_hwirq, parent_type);
1468 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1470 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1471 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
1473 * If the parent irqdomain is msi, the interrupts have already
1474 * been allocated, so the EEXIST is good.
1476 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1480 "failed to allocate parent hwirq %d for hwirq %lu\n",
1481 parent_hwirq, hwirq);
1486 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1487 unsigned int offset)
1493 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1494 * @domain: The IRQ domain used by this IRQ chip
1495 * @data: Outermost irq_data associated with the IRQ
1496 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1498 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1499 * used as the activate function for the &struct irq_domain_ops. The host_data
1500 * for the IRQ domain must be the &struct gpio_chip.
1502 static int gpiochip_irq_domain_activate(struct irq_domain *domain,
1503 struct irq_data *data, bool reserve)
1505 struct gpio_chip *gc = domain->host_data;
1506 unsigned int hwirq = irqd_to_hwirq(data);
1508 return gpiochip_lock_as_irq(gc, hwirq);
1512 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1513 * @domain: The IRQ domain used by this IRQ chip
1514 * @data: Outermost irq_data associated with the IRQ
1516 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1517 * be used as the deactivate function for the &struct irq_domain_ops. The
1518 * host_data for the IRQ domain must be the &struct gpio_chip.
1520 static void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1521 struct irq_data *data)
1523 struct gpio_chip *gc = domain->host_data;
1524 unsigned int hwirq = irqd_to_hwirq(data);
1526 return gpiochip_unlock_as_irq(gc, hwirq);
1529 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1531 ops->activate = gpiochip_irq_domain_activate;
1532 ops->deactivate = gpiochip_irq_domain_deactivate;
1533 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1536 * We only allow overriding the translate() and free() functions for
1537 * hierarchical chips, and this should only be done if the user
1538 * really need something other than 1:1 translation for translate()
1539 * callback and free if user wants to free up any resources which
1540 * were allocated during callbacks, for example populate_parent_alloc_arg.
1542 if (!ops->translate)
1543 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1545 ops->free = irq_domain_free_irqs_common;
1548 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1550 struct irq_domain *domain;
1552 if (!gc->irq.child_to_parent_hwirq ||
1554 chip_err(gc, "missing irqdomain vital data\n");
1555 return ERR_PTR(-EINVAL);
1558 if (!gc->irq.child_offset_to_irq)
1559 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1561 if (!gc->irq.populate_parent_alloc_arg)
1562 gc->irq.populate_parent_alloc_arg =
1563 gpiochip_populate_parent_fwspec_twocell;
1565 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1567 domain = irq_domain_create_hierarchy(
1568 gc->irq.parent_domain,
1572 &gc->irq.child_irq_domain_ops,
1576 return ERR_PTR(-ENOMEM);
1578 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1583 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1585 return !!gc->irq.parent_domain;
1588 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1589 union gpio_irq_fwspec *gfwspec,
1590 unsigned int parent_hwirq,
1591 unsigned int parent_type)
1593 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1595 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1596 fwspec->param_count = 2;
1597 fwspec->param[0] = parent_hwirq;
1598 fwspec->param[1] = parent_type;
1602 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1604 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1605 union gpio_irq_fwspec *gfwspec,
1606 unsigned int parent_hwirq,
1607 unsigned int parent_type)
1609 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1611 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1612 fwspec->param_count = 4;
1613 fwspec->param[0] = 0;
1614 fwspec->param[1] = parent_hwirq;
1615 fwspec->param[2] = 0;
1616 fwspec->param[3] = parent_type;
1620 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1624 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1626 return ERR_PTR(-EINVAL);
1629 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1634 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1637 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1638 * @d: the irqdomain used by this irqchip
1639 * @irq: the global irq number used by this GPIO irqchip irq
1640 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1642 * This function will set up the mapping for a certain IRQ line on a
1643 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1644 * stored inside the gpiochip.
1646 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1647 irq_hw_number_t hwirq)
1649 struct gpio_chip *gc = d->host_data;
1652 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1655 irq_set_chip_data(irq, gc);
1657 * This lock class tells lockdep that GPIO irqs are in a different
1658 * category than their parents, so it won't report false recursion.
1660 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1661 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1662 /* Chips that use nested thread handlers have them marked */
1663 if (gc->irq.threaded)
1664 irq_set_nested_thread(irq, 1);
1665 irq_set_noprobe(irq);
1667 if (gc->irq.num_parents == 1)
1668 ret = irq_set_parent(irq, gc->irq.parents[0]);
1669 else if (gc->irq.map)
1670 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1676 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1677 * is passed as default type.
1679 if (gc->irq.default_type != IRQ_TYPE_NONE)
1680 irq_set_irq_type(irq, gc->irq.default_type);
1685 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1687 struct gpio_chip *gc = d->host_data;
1689 if (gc->irq.threaded)
1690 irq_set_nested_thread(irq, 0);
1691 irq_set_chip_and_handler(irq, NULL, NULL);
1692 irq_set_chip_data(irq, NULL);
1695 static const struct irq_domain_ops gpiochip_domain_ops = {
1696 .map = gpiochip_irq_map,
1697 .unmap = gpiochip_irq_unmap,
1698 /* Virtually all GPIO irqchips are twocell:ed */
1699 .xlate = irq_domain_xlate_twocell,
1702 static struct irq_domain *gpiochip_simple_create_domain(struct gpio_chip *gc)
1704 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1705 struct irq_domain *domain;
1707 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first,
1708 &gpiochip_domain_ops, gc);
1710 return ERR_PTR(-EINVAL);
1715 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1717 struct irq_domain *domain = gc->irq.domain;
1719 #ifdef CONFIG_GPIOLIB_IRQCHIP
1721 * Avoid race condition with other code, which tries to lookup
1722 * an IRQ before the irqchip has been properly registered,
1723 * i.e. while gpiochip is still being brought up.
1725 if (!gc->irq.initialized)
1726 return -EPROBE_DEFER;
1729 if (!gpiochip_irqchip_irq_valid(gc, offset))
1732 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1733 if (irq_domain_is_hierarchy(domain)) {
1734 struct irq_fwspec spec;
1736 spec.fwnode = domain->fwnode;
1737 spec.param_count = 2;
1738 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1739 spec.param[1] = IRQ_TYPE_NONE;
1741 return irq_create_fwspec_mapping(&spec);
1745 return irq_create_mapping(domain, offset);
1748 int gpiochip_irq_reqres(struct irq_data *d)
1750 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1751 unsigned int hwirq = irqd_to_hwirq(d);
1753 return gpiochip_reqres_irq(gc, hwirq);
1755 EXPORT_SYMBOL(gpiochip_irq_reqres);
1757 void gpiochip_irq_relres(struct irq_data *d)
1759 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1760 unsigned int hwirq = irqd_to_hwirq(d);
1762 gpiochip_relres_irq(gc, hwirq);
1764 EXPORT_SYMBOL(gpiochip_irq_relres);
1766 static void gpiochip_irq_mask(struct irq_data *d)
1768 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1769 unsigned int hwirq = irqd_to_hwirq(d);
1771 if (gc->irq.irq_mask)
1772 gc->irq.irq_mask(d);
1773 gpiochip_disable_irq(gc, hwirq);
1776 static void gpiochip_irq_unmask(struct irq_data *d)
1778 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1779 unsigned int hwirq = irqd_to_hwirq(d);
1781 gpiochip_enable_irq(gc, hwirq);
1782 if (gc->irq.irq_unmask)
1783 gc->irq.irq_unmask(d);
1786 static void gpiochip_irq_enable(struct irq_data *d)
1788 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1789 unsigned int hwirq = irqd_to_hwirq(d);
1791 gpiochip_enable_irq(gc, hwirq);
1792 gc->irq.irq_enable(d);
1795 static void gpiochip_irq_disable(struct irq_data *d)
1797 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1798 unsigned int hwirq = irqd_to_hwirq(d);
1800 gc->irq.irq_disable(d);
1801 gpiochip_disable_irq(gc, hwirq);
1804 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1806 struct irq_chip *irqchip = gc->irq.chip;
1808 if (irqchip->flags & IRQCHIP_IMMUTABLE)
1811 chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
1813 if (!irqchip->irq_request_resources &&
1814 !irqchip->irq_release_resources) {
1815 irqchip->irq_request_resources = gpiochip_irq_reqres;
1816 irqchip->irq_release_resources = gpiochip_irq_relres;
1818 if (WARN_ON(gc->irq.irq_enable))
1820 /* Check if the irqchip already has this hook... */
1821 if (irqchip->irq_enable == gpiochip_irq_enable ||
1822 irqchip->irq_mask == gpiochip_irq_mask) {
1824 * ...and if so, give a gentle warning that this is bad
1828 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1832 if (irqchip->irq_disable) {
1833 gc->irq.irq_disable = irqchip->irq_disable;
1834 irqchip->irq_disable = gpiochip_irq_disable;
1836 gc->irq.irq_mask = irqchip->irq_mask;
1837 irqchip->irq_mask = gpiochip_irq_mask;
1840 if (irqchip->irq_enable) {
1841 gc->irq.irq_enable = irqchip->irq_enable;
1842 irqchip->irq_enable = gpiochip_irq_enable;
1844 gc->irq.irq_unmask = irqchip->irq_unmask;
1845 irqchip->irq_unmask = gpiochip_irq_unmask;
1849 static int gpiochip_irqchip_add_allocated_domain(struct gpio_chip *gc,
1850 struct irq_domain *domain,
1851 bool allocated_externally)
1857 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1859 gc->to_irq = gpiochip_to_irq;
1860 gc->irq.domain = domain;
1861 gc->irq.domain_is_allocated_externally = allocated_externally;
1864 * Using barrier() here to prevent compiler from reordering
1865 * gc->irq.initialized before adding irqdomain.
1869 gc->irq.initialized = true;
1875 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1876 * @gc: the GPIO chip to add the IRQ chip to
1877 * @lock_key: lockdep class for IRQ lock
1878 * @request_key: lockdep class for IRQ request
1880 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1881 struct lock_class_key *lock_key,
1882 struct lock_class_key *request_key)
1884 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1885 struct irq_chip *irqchip = gc->irq.chip;
1886 struct irq_domain *domain;
1894 if (gc->irq.parent_handler && gc->can_sleep) {
1895 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1899 type = gc->irq.default_type;
1902 * Specifying a default trigger is a terrible idea if DT or ACPI is
1903 * used to configure the interrupts, as you may end up with
1904 * conflicting triggers. Tell the user, and reset to NONE.
1906 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1907 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1908 type = IRQ_TYPE_NONE;
1910 gc->irq.default_type = type;
1911 gc->irq.lock_key = lock_key;
1912 gc->irq.request_key = request_key;
1914 /* If a parent irqdomain is provided, let's build a hierarchy */
1915 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1916 domain = gpiochip_hierarchy_create_domain(gc);
1918 domain = gpiochip_simple_create_domain(gc);
1921 return PTR_ERR(domain);
1923 if (gc->irq.parent_handler) {
1924 for (i = 0; i < gc->irq.num_parents; i++) {
1927 if (gc->irq.per_parent_data)
1928 data = gc->irq.parent_handler_data_array[i];
1930 data = gc->irq.parent_handler_data ?: gc;
1933 * The parent IRQ chip is already using the chip_data
1934 * for this IRQ chip, so our callbacks simply use the
1937 irq_set_chained_handler_and_data(gc->irq.parents[i],
1938 gc->irq.parent_handler,
1943 gpiochip_set_irq_hooks(gc);
1945 ret = gpiochip_irqchip_add_allocated_domain(gc, domain, false);
1949 acpi_gpiochip_request_interrupts(gc);
1955 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1956 * @gc: the gpiochip to remove the irqchip from
1958 * This is called only from gpiochip_remove()
1960 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1962 struct irq_chip *irqchip = gc->irq.chip;
1963 unsigned int offset;
1965 acpi_gpiochip_free_interrupts(gc);
1967 if (irqchip && gc->irq.parent_handler) {
1968 struct gpio_irq_chip *irq = &gc->irq;
1971 for (i = 0; i < irq->num_parents; i++)
1972 irq_set_chained_handler_and_data(irq->parents[i],
1976 /* Remove all IRQ mappings and delete the domain */
1977 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) {
1980 for (offset = 0; offset < gc->ngpio; offset++) {
1981 if (!gpiochip_irqchip_irq_valid(gc, offset))
1984 irq = irq_find_mapping(gc->irq.domain, offset);
1985 irq_dispose_mapping(irq);
1988 irq_domain_remove(gc->irq.domain);
1991 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
1992 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1993 irqchip->irq_request_resources = NULL;
1994 irqchip->irq_release_resources = NULL;
1996 if (irqchip->irq_enable == gpiochip_irq_enable) {
1997 irqchip->irq_enable = gc->irq.irq_enable;
1998 irqchip->irq_disable = gc->irq.irq_disable;
2001 gc->irq.irq_enable = NULL;
2002 gc->irq.irq_disable = NULL;
2003 gc->irq.chip = NULL;
2005 gpiochip_irqchip_free_valid_mask(gc);
2009 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
2010 * @gc: the gpiochip to add the irqchip to
2011 * @domain: the irqdomain to add to the gpiochip
2013 * This function adds an IRQ domain to the gpiochip.
2015 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
2016 struct irq_domain *domain)
2018 return gpiochip_irqchip_add_allocated_domain(gc, domain, true);
2020 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
2022 #else /* CONFIG_GPIOLIB_IRQCHIP */
2024 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
2025 struct lock_class_key *lock_key,
2026 struct lock_class_key *request_key)
2030 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
2032 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
2037 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
2041 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
2044 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2047 * gpiochip_generic_request() - request the gpio function for a pin
2048 * @gc: the gpiochip owning the GPIO
2049 * @offset: the offset of the GPIO to request for GPIO function
2051 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
2053 #ifdef CONFIG_PINCTRL
2054 if (list_empty(&gc->gpiodev->pin_ranges))
2058 return pinctrl_gpio_request(gc, offset);
2060 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2063 * gpiochip_generic_free() - free the gpio function from a pin
2064 * @gc: the gpiochip to request the gpio function for
2065 * @offset: the offset of the GPIO to free from GPIO function
2067 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
2069 #ifdef CONFIG_PINCTRL
2070 if (list_empty(&gc->gpiodev->pin_ranges))
2074 pinctrl_gpio_free(gc, offset);
2076 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2079 * gpiochip_generic_config() - apply configuration for a pin
2080 * @gc: the gpiochip owning the GPIO
2081 * @offset: the offset of the GPIO to apply the configuration
2082 * @config: the configuration to be applied
2084 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
2085 unsigned long config)
2087 #ifdef CONFIG_PINCTRL
2088 if (list_empty(&gc->gpiodev->pin_ranges))
2092 return pinctrl_gpio_set_config(gc, offset, config);
2094 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2096 #ifdef CONFIG_PINCTRL
2099 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2100 * @gc: the gpiochip to add the range for
2101 * @pctldev: the pin controller to map to
2102 * @gpio_offset: the start offset in the current gpio_chip number space
2103 * @pin_group: name of the pin group inside the pin controller
2105 * Calling this function directly from a DeviceTree-supported
2106 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2107 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2108 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2110 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
2111 struct pinctrl_dev *pctldev,
2112 unsigned int gpio_offset, const char *pin_group)
2114 struct gpio_pin_range *pin_range;
2115 struct gpio_device *gdev = gc->gpiodev;
2118 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2120 chip_err(gc, "failed to allocate pin ranges\n");
2124 /* Use local offset as range ID */
2125 pin_range->range.id = gpio_offset;
2126 pin_range->range.gc = gc;
2127 pin_range->range.name = gc->label;
2128 pin_range->range.base = gdev->base + gpio_offset;
2129 pin_range->pctldev = pctldev;
2131 ret = pinctrl_get_group_pins(pctldev, pin_group,
2132 &pin_range->range.pins,
2133 &pin_range->range.npins);
2139 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2141 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2142 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2143 pinctrl_dev_get_devname(pctldev), pin_group);
2145 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2149 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2152 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2153 * @gc: the gpiochip to add the range for
2154 * @pinctl_name: the dev_name() of the pin controller to map to
2155 * @gpio_offset: the start offset in the current gpio_chip number space
2156 * @pin_offset: the start offset in the pin controller number space
2157 * @npins: the number of pins from the offset of each pin space (GPIO and
2158 * pin controller) to accumulate in this range
2161 * 0 on success, or a negative error-code on failure.
2163 * Calling this function directly from a DeviceTree-supported
2164 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2165 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2166 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2168 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
2169 unsigned int gpio_offset, unsigned int pin_offset,
2172 struct gpio_pin_range *pin_range;
2173 struct gpio_device *gdev = gc->gpiodev;
2176 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2178 chip_err(gc, "failed to allocate pin ranges\n");
2182 /* Use local offset as range ID */
2183 pin_range->range.id = gpio_offset;
2184 pin_range->range.gc = gc;
2185 pin_range->range.name = gc->label;
2186 pin_range->range.base = gdev->base + gpio_offset;
2187 pin_range->range.pin_base = pin_offset;
2188 pin_range->range.npins = npins;
2189 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2191 if (IS_ERR(pin_range->pctldev)) {
2192 ret = PTR_ERR(pin_range->pctldev);
2193 chip_err(gc, "could not create pin range\n");
2197 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2198 gpio_offset, gpio_offset + npins - 1,
2200 pin_offset, pin_offset + npins - 1);
2202 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2206 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2209 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2210 * @gc: the chip to remove all the mappings for
2212 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
2214 struct gpio_pin_range *pin_range, *tmp;
2215 struct gpio_device *gdev = gc->gpiodev;
2217 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2218 list_del(&pin_range->node);
2219 pinctrl_remove_gpio_range(pin_range->pctldev,
2224 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2226 #endif /* CONFIG_PINCTRL */
2228 /* These "optional" allocation calls help prevent drivers from stomping
2229 * on each other, and help provide better diagnostics in debugfs.
2230 * They're called even less than the "set direction" calls.
2232 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2234 unsigned int offset;
2237 CLASS(gpio_chip_guard, guard)(desc);
2241 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags))
2244 /* NOTE: gpio_request() can be called in early boot,
2245 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2248 if (guard.gc->request) {
2249 offset = gpio_chip_hwgpio(desc);
2250 if (gpiochip_line_is_valid(guard.gc, offset))
2251 ret = guard.gc->request(guard.gc, offset);
2258 if (guard.gc->get_direction)
2259 gpiod_get_direction(desc);
2261 ret = desc_set_label(desc, label ? : "?");
2268 clear_bit(FLAG_REQUESTED, &desc->flags);
2273 * This descriptor validation needs to be inserted verbatim into each
2274 * function taking a descriptor, so we need to use a preprocessor
2275 * macro to avoid endless duplication. If the desc is NULL it is an
2276 * optional GPIO and calls should just bail out.
2278 static int validate_desc(const struct gpio_desc *desc, const char *func)
2284 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2285 return PTR_ERR(desc);
2291 #define VALIDATE_DESC(desc) do { \
2292 int __valid = validate_desc(desc, __func__); \
2297 #define VALIDATE_DESC_VOID(desc) do { \
2298 int __valid = validate_desc(desc, __func__); \
2303 int gpiod_request(struct gpio_desc *desc, const char *label)
2305 int ret = -EPROBE_DEFER;
2307 VALIDATE_DESC(desc);
2309 if (try_module_get(desc->gdev->owner)) {
2310 ret = gpiod_request_commit(desc, label);
2312 module_put(desc->gdev->owner);
2314 gpio_device_get(desc->gdev);
2318 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2323 static void gpiod_free_commit(struct gpio_desc *desc)
2325 unsigned long flags;
2329 CLASS(gpio_chip_guard, guard)(desc);
2331 flags = READ_ONCE(desc->flags);
2333 if (guard.gc && test_bit(FLAG_REQUESTED, &flags)) {
2335 guard.gc->free(guard.gc, gpio_chip_hwgpio(desc));
2337 clear_bit(FLAG_ACTIVE_LOW, &flags);
2338 clear_bit(FLAG_REQUESTED, &flags);
2339 clear_bit(FLAG_OPEN_DRAIN, &flags);
2340 clear_bit(FLAG_OPEN_SOURCE, &flags);
2341 clear_bit(FLAG_PULL_UP, &flags);
2342 clear_bit(FLAG_PULL_DOWN, &flags);
2343 clear_bit(FLAG_BIAS_DISABLE, &flags);
2344 clear_bit(FLAG_EDGE_RISING, &flags);
2345 clear_bit(FLAG_EDGE_FALLING, &flags);
2346 clear_bit(FLAG_IS_HOGGED, &flags);
2347 #ifdef CONFIG_OF_DYNAMIC
2348 WRITE_ONCE(desc->hog, NULL);
2350 desc_set_label(desc, NULL);
2351 WRITE_ONCE(desc->flags, flags);
2353 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED);
2357 void gpiod_free(struct gpio_desc *desc)
2359 VALIDATE_DESC_VOID(desc);
2361 gpiod_free_commit(desc);
2362 module_put(desc->gdev->owner);
2363 gpio_device_put(desc->gdev);
2367 * gpiochip_dup_line_label - Get a copy of the consumer label.
2368 * @gc: GPIO chip controlling this line.
2369 * @offset: Hardware offset of the line.
2372 * Pointer to a copy of the consumer label if the line is requested or NULL
2373 * if it's not. If a valid pointer was returned, it must be freed using
2374 * kfree(). In case of a memory allocation error, the function returns %ENOMEM.
2376 * Must not be called from atomic context.
2378 char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
2380 struct gpio_desc *desc;
2383 desc = gpiochip_get_desc(gc, offset);
2387 if (!test_bit(FLAG_REQUESTED, &desc->flags))
2390 guard(srcu)(&desc->srcu);
2392 label = kstrdup(gpiod_get_label(desc), GFP_KERNEL);
2394 return ERR_PTR(-ENOMEM);
2398 EXPORT_SYMBOL_GPL(gpiochip_dup_line_label);
2401 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2403 * @hwnum: hardware number of the GPIO for which to request the descriptor
2404 * @label: label for the GPIO
2405 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2406 * specify things like line inversion semantics with the machine flags
2407 * such as GPIO_OUT_LOW
2408 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2409 * can be used to specify consumer semantics such as open drain
2411 * Function allows GPIO chip drivers to request and use their own GPIO
2412 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2413 * function will not increase reference count of the GPIO chip module. This
2414 * allows the GPIO chip module to be unloaded as needed (we assume that the
2415 * GPIO chip driver handles freeing the GPIOs it has requested).
2418 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2421 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2424 enum gpio_lookup_flags lflags,
2425 enum gpiod_flags dflags)
2427 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2431 chip_err(gc, "failed to get GPIO descriptor\n");
2435 ret = gpiod_request_commit(desc, label);
2437 return ERR_PTR(ret);
2439 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2441 chip_err(gc, "setup of own GPIO %s failed\n", label);
2442 gpiod_free_commit(desc);
2443 return ERR_PTR(ret);
2448 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2451 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2452 * @desc: GPIO descriptor to free
2454 * Function frees the given GPIO requested previously with
2455 * gpiochip_request_own_desc().
2457 void gpiochip_free_own_desc(struct gpio_desc *desc)
2460 gpiod_free_commit(desc);
2462 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2465 * Drivers MUST set GPIO direction before making get/set calls. In
2466 * some cases this is done in early boot, before IRQs are enabled.
2468 * As a rule these aren't called more than once (except for drivers
2469 * using the open-drain emulation idiom) so these are natural places
2470 * to accumulate extra debugging checks. Note that we can't (yet)
2471 * rely on gpio_request() having been called beforehand.
2474 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2475 unsigned long config)
2477 if (!gc->set_config)
2480 return gc->set_config(gc, offset, config);
2483 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2484 enum pin_config_param mode,
2487 unsigned long config;
2489 CLASS(gpio_chip_guard, guard)(desc);
2493 config = pinconf_to_config_packed(mode, argument);
2494 return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config);
2497 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2498 enum pin_config_param mode,
2501 struct device *dev = &desc->gdev->dev;
2502 int gpio = gpio_chip_hwgpio(desc);
2505 ret = gpio_set_config_with_argument(desc, mode, argument);
2506 if (ret != -ENOTSUPP)
2510 case PIN_CONFIG_PERSIST_STATE:
2511 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2520 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2522 return gpio_set_config_with_argument(desc, mode, 0);
2525 static int gpio_set_bias(struct gpio_desc *desc)
2527 enum pin_config_param bias;
2528 unsigned long flags;
2531 flags = READ_ONCE(desc->flags);
2533 if (test_bit(FLAG_BIAS_DISABLE, &flags))
2534 bias = PIN_CONFIG_BIAS_DISABLE;
2535 else if (test_bit(FLAG_PULL_UP, &flags))
2536 bias = PIN_CONFIG_BIAS_PULL_UP;
2537 else if (test_bit(FLAG_PULL_DOWN, &flags))
2538 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2543 case PIN_CONFIG_BIAS_PULL_DOWN:
2544 case PIN_CONFIG_BIAS_PULL_UP:
2553 return gpio_set_config_with_argument_optional(desc, bias, arg);
2557 * gpio_set_debounce_timeout() - Set debounce timeout
2558 * @desc: GPIO descriptor to set the debounce timeout
2559 * @debounce: Debounce timeout in microseconds
2561 * The function calls the certain GPIO driver to set debounce timeout
2564 * Returns 0 on success, or negative error code otherwise.
2566 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2568 return gpio_set_config_with_argument_optional(desc,
2569 PIN_CONFIG_INPUT_DEBOUNCE,
2574 * gpiod_direction_input - set the GPIO direction to input
2575 * @desc: GPIO to set to input
2577 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2578 * be called safely on it.
2580 * Return 0 in case of success, else an error code.
2582 int gpiod_direction_input(struct gpio_desc *desc)
2586 VALIDATE_DESC(desc);
2588 CLASS(gpio_chip_guard, guard)(desc);
2593 * It is legal to have no .get() and .direction_input() specified if
2594 * the chip is output-only, but you can't specify .direction_input()
2595 * and not support the .get() operation, that doesn't make sense.
2597 if (!guard.gc->get && guard.gc->direction_input) {
2599 "%s: missing get() but have direction_input()\n",
2605 * If we have a .direction_input() callback, things are simple,
2606 * just call it. Else we are some input-only chip so try to check the
2607 * direction (if .get_direction() is supported) else we silently
2608 * assume we are in input mode after this.
2610 if (guard.gc->direction_input) {
2611 ret = guard.gc->direction_input(guard.gc,
2612 gpio_chip_hwgpio(desc));
2613 } else if (guard.gc->get_direction &&
2614 (guard.gc->get_direction(guard.gc,
2615 gpio_chip_hwgpio(desc)) != 1)) {
2617 "%s: missing direction_input() operation and line is output\n",
2622 clear_bit(FLAG_IS_OUT, &desc->flags);
2623 ret = gpio_set_bias(desc);
2626 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2630 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2632 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2634 int val = !!value, ret = 0;
2636 CLASS(gpio_chip_guard, guard)(desc);
2641 * It's OK not to specify .direction_output() if the gpiochip is
2642 * output-only, but if there is then not even a .set() operation it
2643 * is pretty tricky to drive the output line.
2645 if (!guard.gc->set && !guard.gc->direction_output) {
2647 "%s: missing set() and direction_output() operations\n",
2652 if (guard.gc->direction_output) {
2653 ret = guard.gc->direction_output(guard.gc,
2654 gpio_chip_hwgpio(desc), val);
2656 /* Check that we are in output mode if we can */
2657 if (guard.gc->get_direction &&
2658 guard.gc->get_direction(guard.gc, gpio_chip_hwgpio(desc))) {
2660 "%s: missing direction_output() operation\n",
2665 * If we can't actively set the direction, we are some
2666 * output-only chip, so just drive the output as desired.
2668 guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), val);
2672 set_bit(FLAG_IS_OUT, &desc->flags);
2673 trace_gpio_value(desc_to_gpio(desc), 0, val);
2674 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2679 * gpiod_direction_output_raw - set the GPIO direction to output
2680 * @desc: GPIO to set to output
2681 * @value: initial output value of the GPIO
2683 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2684 * be called safely on it. The initial value of the output must be specified
2685 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2687 * Return 0 in case of success, else an error code.
2689 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2691 VALIDATE_DESC(desc);
2692 return gpiod_direction_output_raw_commit(desc, value);
2694 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2697 * gpiod_direction_output - set the GPIO direction to output
2698 * @desc: GPIO to set to output
2699 * @value: initial output value of the GPIO
2701 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2702 * be called safely on it. The initial value of the output must be specified
2703 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2706 * Return 0 in case of success, else an error code.
2708 int gpiod_direction_output(struct gpio_desc *desc, int value)
2710 unsigned long flags;
2713 VALIDATE_DESC(desc);
2715 flags = READ_ONCE(desc->flags);
2717 if (test_bit(FLAG_ACTIVE_LOW, &flags))
2722 /* GPIOs used for enabled IRQs shall not be set as output */
2723 if (test_bit(FLAG_USED_AS_IRQ, &flags) &&
2724 test_bit(FLAG_IRQ_IS_ENABLED, &flags)) {
2726 "%s: tried to set a GPIO tied to an IRQ as output\n",
2731 if (test_bit(FLAG_OPEN_DRAIN, &flags)) {
2732 /* First see if we can enable open drain in hardware */
2733 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2735 goto set_output_value;
2736 /* Emulate open drain by not actively driving the line high */
2738 ret = gpiod_direction_input(desc);
2739 goto set_output_flag;
2741 } else if (test_bit(FLAG_OPEN_SOURCE, &flags)) {
2742 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2744 goto set_output_value;
2745 /* Emulate open source by not actively driving the line low */
2747 ret = gpiod_direction_input(desc);
2748 goto set_output_flag;
2751 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2755 ret = gpio_set_bias(desc);
2758 return gpiod_direction_output_raw_commit(desc, value);
2762 * When emulating open-source or open-drain functionalities by not
2763 * actively driving the line (setting mode to input) we still need to
2764 * set the IS_OUT flag or otherwise we won't be able to set the line
2768 set_bit(FLAG_IS_OUT, &desc->flags);
2771 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2774 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2776 * @desc: GPIO to enable.
2777 * @flags: Flags related to GPIO edge.
2779 * Return 0 in case of success, else negative error code.
2781 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2785 VALIDATE_DESC(desc);
2787 CLASS(gpio_chip_guard, guard)(desc);
2791 if (!guard.gc->en_hw_timestamp) {
2792 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2796 ret = guard.gc->en_hw_timestamp(guard.gc,
2797 gpio_chip_hwgpio(desc), flags);
2799 gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
2803 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
2806 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2808 * @desc: GPIO to disable.
2809 * @flags: Flags related to GPIO edge, same value as used during enable call.
2811 * Return 0 in case of success, else negative error code.
2813 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2817 VALIDATE_DESC(desc);
2819 CLASS(gpio_chip_guard, guard)(desc);
2823 if (!guard.gc->dis_hw_timestamp) {
2824 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2828 ret = guard.gc->dis_hw_timestamp(guard.gc, gpio_chip_hwgpio(desc),
2831 gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
2835 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
2838 * gpiod_set_config - sets @config for a GPIO
2839 * @desc: descriptor of the GPIO for which to set the configuration
2840 * @config: Same packed config format as generic pinconf
2843 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2846 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2848 VALIDATE_DESC(desc);
2850 CLASS(gpio_chip_guard, guard)(desc);
2854 return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config);
2856 EXPORT_SYMBOL_GPL(gpiod_set_config);
2859 * gpiod_set_debounce - sets @debounce time for a GPIO
2860 * @desc: descriptor of the GPIO for which to set debounce time
2861 * @debounce: debounce time in microseconds
2864 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2867 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2869 unsigned long config;
2871 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2872 return gpiod_set_config(desc, config);
2874 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2877 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2878 * @desc: descriptor of the GPIO for which to configure persistence
2879 * @transitory: True to lose state on suspend or reset, false for persistence
2882 * 0 on success, otherwise a negative error code.
2884 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2886 VALIDATE_DESC(desc);
2888 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2889 * persistence state.
2891 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2893 /* If the driver supports it, set the persistence state now */
2894 return gpio_set_config_with_argument_optional(desc,
2895 PIN_CONFIG_PERSIST_STATE,
2900 * gpiod_is_active_low - test whether a GPIO is active-low or not
2901 * @desc: the gpio descriptor to test
2903 * Returns 1 if the GPIO is active-low, 0 otherwise.
2905 int gpiod_is_active_low(const struct gpio_desc *desc)
2907 VALIDATE_DESC(desc);
2908 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2910 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2913 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2914 * @desc: the gpio descriptor to change
2916 void gpiod_toggle_active_low(struct gpio_desc *desc)
2918 VALIDATE_DESC_VOID(desc);
2919 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2921 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2923 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
2925 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
2928 /* I/O calls are only valid after configuration completed; the relevant
2929 * "is this a valid GPIO" error checks should already have been done.
2931 * "Get" operations are often inlinable as reading a pin value register,
2932 * and masking the relevant bit in that register.
2934 * When "set" operations are inlinable, they involve writing that mask to
2935 * one register to set a low value, or a different register to set it high.
2936 * Otherwise locking is needed, so there may be little value to inlining.
2938 *------------------------------------------------------------------------
2940 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2941 * have requested the GPIO. That can include implicit requesting by
2942 * a direction setting call. Marking a gpio as requested locks its chip
2943 * in memory, guaranteeing that these table lookups need no more locking
2944 * and that gpiochip_remove() will fail.
2946 * REVISIT when debugging, consider adding some instrumentation to ensure
2947 * that the GPIO was actually requested.
2950 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2952 struct gpio_device *gdev;
2953 struct gpio_chip *gc;
2956 /* FIXME Unable to use gpio_chip_guard due to const desc. */
2959 guard(srcu)(&gdev->srcu);
2961 gc = srcu_dereference(gdev->chip, &gdev->srcu);
2965 value = gpio_chip_get_value(gc, desc);
2966 value = value < 0 ? value : !!value;
2967 trace_gpio_value(desc_to_gpio(desc), 1, value);
2971 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2972 unsigned long *mask, unsigned long *bits)
2974 if (gc->get_multiple)
2975 return gc->get_multiple(gc, mask, bits);
2979 for_each_set_bit(i, mask, gc->ngpio) {
2980 value = gc->get(gc, i);
2983 __assign_bit(i, bits, value);
2990 /* The 'other' chip must be protected with its GPIO device's SRCU. */
2991 static bool gpio_device_chip_cmp(struct gpio_device *gdev, struct gpio_chip *gc)
2993 guard(srcu)(&gdev->srcu);
2995 return gc == srcu_dereference(gdev->chip, &gdev->srcu);
2998 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2999 unsigned int array_size,
3000 struct gpio_desc **desc_array,
3001 struct gpio_array *array_info,
3002 unsigned long *value_bitmap)
3007 * Validate array_info against desc_array and its size.
3008 * It should immediately follow desc_array if both
3009 * have been obtained from the same gpiod_get_array() call.
3011 if (array_info && array_info->desc == desc_array &&
3012 array_size <= array_info->size &&
3013 (void *)array_info == desc_array + array_info->size) {
3015 WARN_ON(array_info->chip->can_sleep);
3017 ret = gpio_chip_get_multiple(array_info->chip,
3018 array_info->get_mask,
3023 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3024 bitmap_xor(value_bitmap, value_bitmap,
3025 array_info->invert_mask, array_size);
3027 i = find_first_zero_bit(array_info->get_mask, array_size);
3028 if (i == array_size)
3034 while (i < array_size) {
3035 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3036 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3037 unsigned long *mask, *bits;
3040 CLASS(gpio_chip_guard, guard)(desc_array[i]);
3044 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) {
3045 mask = fastpath_mask;
3046 bits = fastpath_bits;
3048 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3050 mask = bitmap_alloc(guard.gc->ngpio, flags);
3054 bits = bitmap_alloc(guard.gc->ngpio, flags);
3061 bitmap_zero(mask, guard.gc->ngpio);
3064 WARN_ON(guard.gc->can_sleep);
3066 /* collect all inputs belonging to the same chip */
3069 const struct gpio_desc *desc = desc_array[i];
3070 int hwgpio = gpio_chip_hwgpio(desc);
3072 __set_bit(hwgpio, mask);
3076 i = find_next_zero_bit(array_info->get_mask,
3078 } while ((i < array_size) &&
3079 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc));
3081 ret = gpio_chip_get_multiple(guard.gc, mask, bits);
3083 if (mask != fastpath_mask)
3085 if (bits != fastpath_bits)
3090 for (j = first; j < i; ) {
3091 const struct gpio_desc *desc = desc_array[j];
3092 int hwgpio = gpio_chip_hwgpio(desc);
3093 int value = test_bit(hwgpio, bits);
3095 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3097 __assign_bit(j, value_bitmap, value);
3098 trace_gpio_value(desc_to_gpio(desc), 1, value);
3102 j = find_next_zero_bit(array_info->get_mask, i,
3106 if (mask != fastpath_mask)
3108 if (bits != fastpath_bits)
3115 * gpiod_get_raw_value() - return a gpio's raw value
3116 * @desc: gpio whose value will be returned
3118 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3119 * its ACTIVE_LOW status, or negative errno on failure.
3121 * This function can be called from contexts where we cannot sleep, and will
3122 * complain if the GPIO chip functions potentially sleep.
3124 int gpiod_get_raw_value(const struct gpio_desc *desc)
3126 VALIDATE_DESC(desc);
3127 /* Should be using gpiod_get_raw_value_cansleep() */
3128 WARN_ON(desc->gdev->can_sleep);
3129 return gpiod_get_raw_value_commit(desc);
3131 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
3134 * gpiod_get_value() - return a gpio's value
3135 * @desc: gpio whose value will be returned
3137 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3138 * account, or negative errno on failure.
3140 * This function can be called from contexts where we cannot sleep, and will
3141 * complain if the GPIO chip functions potentially sleep.
3143 int gpiod_get_value(const struct gpio_desc *desc)
3147 VALIDATE_DESC(desc);
3148 /* Should be using gpiod_get_value_cansleep() */
3149 WARN_ON(desc->gdev->can_sleep);
3151 value = gpiod_get_raw_value_commit(desc);
3155 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3160 EXPORT_SYMBOL_GPL(gpiod_get_value);
3163 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3164 * @array_size: number of elements in the descriptor array / value bitmap
3165 * @desc_array: array of GPIO descriptors whose values will be read
3166 * @array_info: information on applicability of fast bitmap processing path
3167 * @value_bitmap: bitmap to store the read values
3169 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3170 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3171 * else an error code.
3173 * This function can be called from contexts where we cannot sleep,
3174 * and it will complain if the GPIO chip functions potentially sleep.
3176 int gpiod_get_raw_array_value(unsigned int array_size,
3177 struct gpio_desc **desc_array,
3178 struct gpio_array *array_info,
3179 unsigned long *value_bitmap)
3183 return gpiod_get_array_value_complex(true, false, array_size,
3184 desc_array, array_info,
3187 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3190 * gpiod_get_array_value() - read values from an array of GPIOs
3191 * @array_size: number of elements in the descriptor array / value bitmap
3192 * @desc_array: array of GPIO descriptors whose values will be read
3193 * @array_info: information on applicability of fast bitmap processing path
3194 * @value_bitmap: bitmap to store the read values
3196 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3197 * into account. Return 0 in case of success, else an error code.
3199 * This function can be called from contexts where we cannot sleep,
3200 * and it will complain if the GPIO chip functions potentially sleep.
3202 int gpiod_get_array_value(unsigned int array_size,
3203 struct gpio_desc **desc_array,
3204 struct gpio_array *array_info,
3205 unsigned long *value_bitmap)
3209 return gpiod_get_array_value_complex(false, false, array_size,
3210 desc_array, array_info,
3213 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3216 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3217 * @desc: gpio descriptor whose state need to be set.
3218 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3220 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
3222 int ret = 0, offset = gpio_chip_hwgpio(desc);
3224 CLASS(gpio_chip_guard, guard)(desc);
3229 ret = guard.gc->direction_input(guard.gc, offset);
3231 ret = guard.gc->direction_output(guard.gc, offset, 0);
3233 set_bit(FLAG_IS_OUT, &desc->flags);
3235 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3238 "%s: Error in set_value for open drain err %d\n",
3243 * _gpio_set_open_source_value() - Set the open source gpio's value.
3244 * @desc: gpio descriptor whose state need to be set.
3245 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3247 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3249 int ret = 0, offset = gpio_chip_hwgpio(desc);
3251 CLASS(gpio_chip_guard, guard)(desc);
3256 ret = guard.gc->direction_output(guard.gc, offset, 1);
3258 set_bit(FLAG_IS_OUT, &desc->flags);
3260 ret = guard.gc->direction_input(guard.gc, offset);
3262 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3265 "%s: Error in set_value for open source err %d\n",
3269 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3271 CLASS(gpio_chip_guard, guard)(desc);
3275 trace_gpio_value(desc_to_gpio(desc), 0, value);
3276 guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), value);
3280 * set multiple outputs on the same chip;
3281 * use the chip's set_multiple function if available;
3282 * otherwise set the outputs sequentially;
3283 * @chip: the GPIO chip we operate on
3284 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3285 * defines which outputs are to be changed
3286 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3287 * defines the values the outputs specified by mask are to be set to
3289 static void gpio_chip_set_multiple(struct gpio_chip *gc,
3290 unsigned long *mask, unsigned long *bits)
3292 if (gc->set_multiple) {
3293 gc->set_multiple(gc, mask, bits);
3297 /* set outputs if the corresponding mask bit is set */
3298 for_each_set_bit(i, mask, gc->ngpio)
3299 gc->set(gc, i, test_bit(i, bits));
3303 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3304 unsigned int array_size,
3305 struct gpio_desc **desc_array,
3306 struct gpio_array *array_info,
3307 unsigned long *value_bitmap)
3312 * Validate array_info against desc_array and its size.
3313 * It should immediately follow desc_array if both
3314 * have been obtained from the same gpiod_get_array() call.
3316 if (array_info && array_info->desc == desc_array &&
3317 array_size <= array_info->size &&
3318 (void *)array_info == desc_array + array_info->size) {
3320 WARN_ON(array_info->chip->can_sleep);
3322 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3323 bitmap_xor(value_bitmap, value_bitmap,
3324 array_info->invert_mask, array_size);
3326 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3329 i = find_first_zero_bit(array_info->set_mask, array_size);
3330 if (i == array_size)
3336 while (i < array_size) {
3337 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3338 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3339 unsigned long *mask, *bits;
3342 CLASS(gpio_chip_guard, guard)(desc_array[i]);
3346 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) {
3347 mask = fastpath_mask;
3348 bits = fastpath_bits;
3350 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3352 mask = bitmap_alloc(guard.gc->ngpio, flags);
3356 bits = bitmap_alloc(guard.gc->ngpio, flags);
3363 bitmap_zero(mask, guard.gc->ngpio);
3366 WARN_ON(guard.gc->can_sleep);
3369 struct gpio_desc *desc = desc_array[i];
3370 int hwgpio = gpio_chip_hwgpio(desc);
3371 int value = test_bit(i, value_bitmap);
3374 * Pins applicable for fast input but not for
3375 * fast output processing may have been already
3376 * inverted inside the fast path, skip them.
3378 if (!raw && !(array_info &&
3379 test_bit(i, array_info->invert_mask)) &&
3380 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3382 trace_gpio_value(desc_to_gpio(desc), 0, value);
3384 * collect all normal outputs belonging to the same chip
3385 * open drain and open source outputs are set individually
3387 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3388 gpio_set_open_drain_value_commit(desc, value);
3389 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3390 gpio_set_open_source_value_commit(desc, value);
3392 __set_bit(hwgpio, mask);
3393 __assign_bit(hwgpio, bits, value);
3399 i = find_next_zero_bit(array_info->set_mask,
3401 } while ((i < array_size) &&
3402 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc));
3403 /* push collected bits to outputs */
3405 gpio_chip_set_multiple(guard.gc, mask, bits);
3407 if (mask != fastpath_mask)
3409 if (bits != fastpath_bits)
3416 * gpiod_set_raw_value() - assign a gpio's raw value
3417 * @desc: gpio whose value will be assigned
3418 * @value: value to assign
3420 * Set the raw value of the GPIO, i.e. the value of its physical line without
3421 * regard for its ACTIVE_LOW status.
3423 * This function can be called from contexts where we cannot sleep, and will
3424 * complain if the GPIO chip functions potentially sleep.
3426 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3428 VALIDATE_DESC_VOID(desc);
3429 /* Should be using gpiod_set_raw_value_cansleep() */
3430 WARN_ON(desc->gdev->can_sleep);
3431 gpiod_set_raw_value_commit(desc, value);
3433 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3436 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3437 * @desc: the descriptor to set the value on
3438 * @value: value to set
3440 * This sets the value of a GPIO line backing a descriptor, applying
3441 * different semantic quirks like active low and open drain/source
3444 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3446 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3448 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3449 gpio_set_open_drain_value_commit(desc, value);
3450 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3451 gpio_set_open_source_value_commit(desc, value);
3453 gpiod_set_raw_value_commit(desc, value);
3457 * gpiod_set_value() - assign a gpio's value
3458 * @desc: gpio whose value will be assigned
3459 * @value: value to assign
3461 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3462 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3464 * This function can be called from contexts where we cannot sleep, and will
3465 * complain if the GPIO chip functions potentially sleep.
3467 void gpiod_set_value(struct gpio_desc *desc, int value)
3469 VALIDATE_DESC_VOID(desc);
3470 /* Should be using gpiod_set_value_cansleep() */
3471 WARN_ON(desc->gdev->can_sleep);
3472 gpiod_set_value_nocheck(desc, value);
3474 EXPORT_SYMBOL_GPL(gpiod_set_value);
3477 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3478 * @array_size: number of elements in the descriptor array / value bitmap
3479 * @desc_array: array of GPIO descriptors whose values will be assigned
3480 * @array_info: information on applicability of fast bitmap processing path
3481 * @value_bitmap: bitmap of values to assign
3483 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3484 * without regard for their ACTIVE_LOW status.
3486 * This function can be called from contexts where we cannot sleep, and will
3487 * complain if the GPIO chip functions potentially sleep.
3489 int gpiod_set_raw_array_value(unsigned int array_size,
3490 struct gpio_desc **desc_array,
3491 struct gpio_array *array_info,
3492 unsigned long *value_bitmap)
3496 return gpiod_set_array_value_complex(true, false, array_size,
3497 desc_array, array_info, value_bitmap);
3499 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3502 * gpiod_set_array_value() - assign values to an array of GPIOs
3503 * @array_size: number of elements in the descriptor array / value bitmap
3504 * @desc_array: array of GPIO descriptors whose values will be assigned
3505 * @array_info: information on applicability of fast bitmap processing path
3506 * @value_bitmap: bitmap of values to assign
3508 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3511 * This function can be called from contexts where we cannot sleep, and will
3512 * complain if the GPIO chip functions potentially sleep.
3514 int gpiod_set_array_value(unsigned int array_size,
3515 struct gpio_desc **desc_array,
3516 struct gpio_array *array_info,
3517 unsigned long *value_bitmap)
3521 return gpiod_set_array_value_complex(false, false, array_size,
3522 desc_array, array_info,
3525 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3528 * gpiod_cansleep() - report whether gpio value access may sleep
3529 * @desc: gpio to check
3532 int gpiod_cansleep(const struct gpio_desc *desc)
3534 VALIDATE_DESC(desc);
3535 return desc->gdev->can_sleep;
3537 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3540 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3541 * @desc: gpio to set the consumer name on
3542 * @name: the new consumer name
3544 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3546 VALIDATE_DESC(desc);
3548 return desc_set_label(desc, name);
3550 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3553 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3554 * @desc: gpio whose IRQ will be returned (already requested)
3556 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3559 int gpiod_to_irq(const struct gpio_desc *desc)
3561 struct gpio_device *gdev;
3562 struct gpio_chip *gc;
3566 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3567 * requires this function to not return zero on an invalid descriptor
3568 * but rather a negative error number.
3570 if (!desc || IS_ERR(desc))
3574 /* FIXME Cannot use gpio_chip_guard due to const desc. */
3575 guard(srcu)(&gdev->srcu);
3576 gc = srcu_dereference(gdev->chip, &gdev->srcu);
3580 offset = gpio_chip_hwgpio(desc);
3582 int retirq = gc->to_irq(gc, offset);
3584 /* Zero means NO_IRQ */
3590 #ifdef CONFIG_GPIOLIB_IRQCHIP
3593 * Avoid race condition with other code, which tries to lookup
3594 * an IRQ before the irqchip has been properly registered,
3595 * i.e. while gpiochip is still being brought up.
3597 return -EPROBE_DEFER;
3602 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3605 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3606 * @gc: the chip the GPIO to lock belongs to
3607 * @offset: the offset of the GPIO to lock as IRQ
3609 * This is used directly by GPIO drivers that want to lock down
3610 * a certain GPIO line to be used for IRQs.
3612 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3614 struct gpio_desc *desc;
3616 desc = gpiochip_get_desc(gc, offset);
3618 return PTR_ERR(desc);
3621 * If it's fast: flush the direction setting if something changed
3624 if (!gc->can_sleep && gc->get_direction) {
3625 int dir = gpiod_get_direction(desc);
3628 chip_err(gc, "%s: cannot get GPIO direction\n",
3634 /* To be valid for IRQ the line needs to be input or open drain */
3635 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3636 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3638 "%s: tried to flag a GPIO set as output for IRQ\n",
3643 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3644 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3648 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3651 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3652 * @gc: the chip the GPIO to lock belongs to
3653 * @offset: the offset of the GPIO to lock as IRQ
3655 * This is used directly by GPIO drivers that want to indicate
3656 * that a certain GPIO is no longer used exclusively for IRQ.
3658 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3660 struct gpio_desc *desc;
3662 desc = gpiochip_get_desc(gc, offset);
3666 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3667 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3669 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3671 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3673 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3675 if (!IS_ERR(desc) &&
3676 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3677 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3679 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3681 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3683 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3685 if (!IS_ERR(desc) &&
3686 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3688 * We must not be output when using IRQ UNLESS we are
3691 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3692 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3693 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3696 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3698 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3700 if (offset >= gc->ngpio)
3703 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3705 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3707 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3711 if (!try_module_get(gc->gpiodev->owner))
3714 ret = gpiochip_lock_as_irq(gc, offset);
3716 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3717 module_put(gc->gpiodev->owner);
3722 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3724 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3726 gpiochip_unlock_as_irq(gc, offset);
3727 module_put(gc->gpiodev->owner);
3729 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3731 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3733 if (offset >= gc->ngpio)
3736 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3738 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3740 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3742 if (offset >= gc->ngpio)
3745 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3747 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3749 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3751 if (offset >= gc->ngpio)
3754 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3756 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3759 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3760 * @desc: gpio whose value will be returned
3762 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3763 * its ACTIVE_LOW status, or negative errno on failure.
3765 * This function is to be called from contexts that can sleep.
3767 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3770 VALIDATE_DESC(desc);
3771 return gpiod_get_raw_value_commit(desc);
3773 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3776 * gpiod_get_value_cansleep() - return a gpio's value
3777 * @desc: gpio whose value will be returned
3779 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3780 * account, or negative errno on failure.
3782 * This function is to be called from contexts that can sleep.
3784 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3789 VALIDATE_DESC(desc);
3790 value = gpiod_get_raw_value_commit(desc);
3794 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3799 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3802 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3803 * @array_size: number of elements in the descriptor array / value bitmap
3804 * @desc_array: array of GPIO descriptors whose values will be read
3805 * @array_info: information on applicability of fast bitmap processing path
3806 * @value_bitmap: bitmap to store the read values
3808 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3809 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3810 * else an error code.
3812 * This function is to be called from contexts that can sleep.
3814 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3815 struct gpio_desc **desc_array,
3816 struct gpio_array *array_info,
3817 unsigned long *value_bitmap)
3822 return gpiod_get_array_value_complex(true, true, array_size,
3823 desc_array, array_info,
3826 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3829 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3830 * @array_size: number of elements in the descriptor array / value bitmap
3831 * @desc_array: array of GPIO descriptors whose values will be read
3832 * @array_info: information on applicability of fast bitmap processing path
3833 * @value_bitmap: bitmap to store the read values
3835 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3836 * into account. Return 0 in case of success, else an error code.
3838 * This function is to be called from contexts that can sleep.
3840 int gpiod_get_array_value_cansleep(unsigned int array_size,
3841 struct gpio_desc **desc_array,
3842 struct gpio_array *array_info,
3843 unsigned long *value_bitmap)
3848 return gpiod_get_array_value_complex(false, true, array_size,
3849 desc_array, array_info,
3852 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3855 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3856 * @desc: gpio whose value will be assigned
3857 * @value: value to assign
3859 * Set the raw value of the GPIO, i.e. the value of its physical line without
3860 * regard for its ACTIVE_LOW status.
3862 * This function is to be called from contexts that can sleep.
3864 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3867 VALIDATE_DESC_VOID(desc);
3868 gpiod_set_raw_value_commit(desc, value);
3870 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3873 * gpiod_set_value_cansleep() - assign a gpio's value
3874 * @desc: gpio whose value will be assigned
3875 * @value: value to assign
3877 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3880 * This function is to be called from contexts that can sleep.
3882 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3885 VALIDATE_DESC_VOID(desc);
3886 gpiod_set_value_nocheck(desc, value);
3888 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3891 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3892 * @array_size: number of elements in the descriptor array / value bitmap
3893 * @desc_array: array of GPIO descriptors whose values will be assigned
3894 * @array_info: information on applicability of fast bitmap processing path
3895 * @value_bitmap: bitmap of values to assign
3897 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3898 * without regard for their ACTIVE_LOW status.
3900 * This function is to be called from contexts that can sleep.
3902 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3903 struct gpio_desc **desc_array,
3904 struct gpio_array *array_info,
3905 unsigned long *value_bitmap)
3910 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3911 array_info, value_bitmap);
3913 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3916 * gpiod_add_lookup_tables() - register GPIO device consumers
3917 * @tables: list of tables of consumers to register
3918 * @n: number of tables in the list
3920 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3924 mutex_lock(&gpio_lookup_lock);
3926 for (i = 0; i < n; i++)
3927 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3929 mutex_unlock(&gpio_lookup_lock);
3933 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3934 * @array_size: number of elements in the descriptor array / value bitmap
3935 * @desc_array: array of GPIO descriptors whose values will be assigned
3936 * @array_info: information on applicability of fast bitmap processing path
3937 * @value_bitmap: bitmap of values to assign
3939 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3942 * This function is to be called from contexts that can sleep.
3944 int gpiod_set_array_value_cansleep(unsigned int array_size,
3945 struct gpio_desc **desc_array,
3946 struct gpio_array *array_info,
3947 unsigned long *value_bitmap)
3952 return gpiod_set_array_value_complex(false, true, array_size,
3953 desc_array, array_info,
3956 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3958 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action)
3960 blocking_notifier_call_chain(&desc->gdev->line_state_notifier,
3965 * gpiod_add_lookup_table() - register GPIO device consumers
3966 * @table: table of consumers to register
3968 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3970 gpiod_add_lookup_tables(&table, 1);
3972 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3975 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3976 * @table: table of consumers to unregister
3978 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3980 /* Nothing to remove */
3984 mutex_lock(&gpio_lookup_lock);
3986 list_del(&table->list);
3988 mutex_unlock(&gpio_lookup_lock);
3990 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3993 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3994 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3996 void gpiod_add_hogs(struct gpiod_hog *hogs)
3998 struct gpiod_hog *hog;
4000 mutex_lock(&gpio_machine_hogs_mutex);
4002 for (hog = &hogs[0]; hog->chip_label; hog++) {
4003 list_add_tail(&hog->list, &gpio_machine_hogs);
4006 * The chip may have been registered earlier, so check if it
4007 * exists and, if so, try to hog the line now.
4009 struct gpio_device *gdev __free(gpio_device_put) =
4010 gpio_device_find_by_label(hog->chip_label);
4012 gpiochip_machine_hog(gpio_device_get_chip(gdev), hog);
4015 mutex_unlock(&gpio_machine_hogs_mutex);
4017 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4019 void gpiod_remove_hogs(struct gpiod_hog *hogs)
4021 struct gpiod_hog *hog;
4023 mutex_lock(&gpio_machine_hogs_mutex);
4024 for (hog = &hogs[0]; hog->chip_label; hog++)
4025 list_del(&hog->list);
4026 mutex_unlock(&gpio_machine_hogs_mutex);
4028 EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
4030 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4032 const char *dev_id = dev ? dev_name(dev) : NULL;
4033 struct gpiod_lookup_table *table;
4035 list_for_each_entry(table, &gpio_lookup_list, list) {
4036 if (table->dev_id && dev_id) {
4038 * Valid strings on both ends, must be identical to have
4041 if (!strcmp(table->dev_id, dev_id))
4045 * One of the pointers is NULL, so both must be to have
4048 if (dev_id == table->dev_id)
4056 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
4057 unsigned int idx, unsigned long *flags)
4059 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4060 struct gpiod_lookup_table *table;
4061 struct gpiod_lookup *p;
4062 struct gpio_chip *gc;
4064 guard(mutex)(&gpio_lookup_lock);
4066 table = gpiod_find_lookup_table(dev);
4070 for (p = &table->table[0]; p->key; p++) {
4071 /* idx must always match exactly */
4075 /* If the lookup entry has a con_id, require exact match */
4076 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4079 if (p->chip_hwnum == U16_MAX) {
4080 desc = gpio_name_to_desc(p->key);
4086 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
4088 return ERR_PTR(-EPROBE_DEFER);
4091 struct gpio_device *gdev __free(gpio_device_put) =
4092 gpio_device_find_by_label(p->key);
4095 * As the lookup table indicates a chip with
4096 * p->key should exist, assume it may
4097 * still appear later and let the interested
4098 * consumer be probed again or let the Deferred
4099 * Probe infrastructure handle the error.
4101 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4103 return ERR_PTR(-EPROBE_DEFER);
4106 gc = gpio_device_get_chip(gdev);
4108 if (gc->ngpio <= p->chip_hwnum) {
4110 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4111 idx, p->chip_hwnum, gc->ngpio - 1,
4113 return ERR_PTR(-EINVAL);
4116 desc = gpio_device_get_desc(gdev, p->chip_hwnum);
4125 static int platform_gpio_count(struct device *dev, const char *con_id)
4127 struct gpiod_lookup_table *table;
4128 struct gpiod_lookup *p;
4129 unsigned int count = 0;
4131 scoped_guard(mutex, &gpio_lookup_lock) {
4132 table = gpiod_find_lookup_table(dev);
4136 for (p = &table->table[0]; p->key; p++) {
4137 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4138 (!con_id && !p->con_id))
4149 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
4150 struct device *consumer,
4153 enum gpiod_flags *flags,
4154 unsigned long *lookupflags)
4156 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4158 if (is_of_node(fwnode)) {
4159 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n",
4161 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags);
4162 } else if (is_acpi_node(fwnode)) {
4163 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n",
4165 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
4166 } else if (is_software_node(fwnode)) {
4167 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n",
4169 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
4175 struct gpio_desc *gpiod_find_and_request(struct device *consumer,
4176 struct fwnode_handle *fwnode,
4179 enum gpiod_flags flags,
4181 bool platform_lookup_allowed)
4183 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4185 * scoped_guard() is implemented as a for loop, meaning static
4186 * analyzers will complain about these two not being initialized.
4188 struct gpio_desc *desc = NULL;
4191 scoped_guard(srcu, &gpio_devices_srcu) {
4192 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx,
4193 &flags, &lookupflags);
4194 if (gpiod_not_found(desc) && platform_lookup_allowed) {
4196 * Either we are not using DT or ACPI, or their lookup
4197 * did not return a result. In that case, use platform
4198 * lookup as a fallback.
4201 "using lookup tables for GPIO lookup\n");
4202 desc = gpiod_find(consumer, con_id, idx, &lookupflags);
4206 dev_dbg(consumer, "No GPIO consumer %s found\n",
4212 * If a connection label was passed use that, else attempt to use
4213 * the device name as label
4215 ret = gpiod_request(desc, label);
4218 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
4219 return ERR_PTR(ret);
4222 * This happens when there are several consumers for
4223 * the same GPIO line: we just return here without
4224 * further initialization. It is a bit of a hack.
4225 * This is necessary to support fixed regulators.
4227 * FIXME: Make this more sane and safe.
4230 "nonexclusive access to GPIO for %s\n", con_id);
4234 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4236 dev_dbg(consumer, "setup of GPIO %s failed\n", con_id);
4238 return ERR_PTR(ret);
4241 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_REQUESTED);
4247 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4248 * @fwnode: handle of the firmware node
4249 * @con_id: function within the GPIO consumer
4250 * @index: index of the GPIO to obtain for the consumer
4251 * @flags: GPIO initialization flags
4252 * @label: label to attach to the requested GPIO
4254 * This function can be used for drivers that get their configuration
4255 * from opaque firmware.
4257 * The function properly finds the corresponding GPIO using whatever is the
4258 * underlying firmware interface and then makes sure that the GPIO
4259 * descriptor is requested before it is returned to the caller.
4262 * On successful request the GPIO pin is configured in accordance with
4265 * In case of error an ERR_PTR() is returned.
4267 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4270 enum gpiod_flags flags,
4273 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false);
4275 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4278 * gpiod_count - return the number of GPIOs associated with a device / function
4279 * or -ENOENT if no GPIO has been assigned to the requested function
4280 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4281 * @con_id: function within the GPIO consumer
4283 int gpiod_count(struct device *dev, const char *con_id)
4285 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4286 int count = -ENOENT;
4288 if (is_of_node(fwnode))
4289 count = of_gpio_count(fwnode, con_id);
4290 else if (is_acpi_node(fwnode))
4291 count = acpi_gpio_count(fwnode, con_id);
4292 else if (is_software_node(fwnode))
4293 count = swnode_gpio_count(fwnode, con_id);
4296 count = platform_gpio_count(dev, con_id);
4300 EXPORT_SYMBOL_GPL(gpiod_count);
4303 * gpiod_get - obtain a GPIO for a given GPIO function
4304 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4305 * @con_id: function within the GPIO consumer
4306 * @flags: optional GPIO initialization flags
4308 * Return the GPIO descriptor corresponding to the function con_id of device
4309 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4310 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4312 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4313 enum gpiod_flags flags)
4315 return gpiod_get_index(dev, con_id, 0, flags);
4317 EXPORT_SYMBOL_GPL(gpiod_get);
4320 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4321 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4322 * @con_id: function within the GPIO consumer
4323 * @flags: optional GPIO initialization flags
4325 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4326 * the requested function it will return NULL. This is convenient for drivers
4327 * that need to handle optional GPIOs.
4329 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4331 enum gpiod_flags flags)
4333 return gpiod_get_index_optional(dev, con_id, 0, flags);
4335 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4339 * gpiod_configure_flags - helper function to configure a given GPIO
4340 * @desc: gpio whose value will be assigned
4341 * @con_id: function within the GPIO consumer
4342 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4343 * of_find_gpio() or of_get_gpio_hog()
4344 * @dflags: gpiod_flags - optional GPIO initialization flags
4346 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4347 * requested function and/or index, or another IS_ERR() code if an error
4348 * occurred while trying to acquire the GPIO.
4350 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4351 unsigned long lflags, enum gpiod_flags dflags)
4355 if (lflags & GPIO_ACTIVE_LOW)
4356 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4358 if (lflags & GPIO_OPEN_DRAIN)
4359 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4360 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4362 * This enforces open drain mode from the consumer side.
4363 * This is necessary for some busses like I2C, but the lookup
4364 * should *REALLY* have specified them as open drain in the
4365 * first place, so print a little warning here.
4367 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4369 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4372 if (lflags & GPIO_OPEN_SOURCE)
4373 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4375 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
4376 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
4377 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
4379 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4383 if (lflags & GPIO_PULL_UP)
4384 set_bit(FLAG_PULL_UP, &desc->flags);
4385 else if (lflags & GPIO_PULL_DOWN)
4386 set_bit(FLAG_PULL_DOWN, &desc->flags);
4387 else if (lflags & GPIO_PULL_DISABLE)
4388 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
4390 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4394 /* No particular flag request, return here... */
4395 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4396 gpiod_dbg(desc, "no flags found for %s\n", con_id);
4401 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4402 ret = gpiod_direction_output(desc,
4403 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4405 ret = gpiod_direction_input(desc);
4411 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4412 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4413 * @con_id: function within the GPIO consumer
4414 * @idx: index of the GPIO to obtain in the consumer
4415 * @flags: optional GPIO initialization flags
4417 * This variant of gpiod_get() allows to access GPIOs other than the first
4418 * defined one for functions that define several GPIOs.
4420 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4421 * requested function and/or index, or another IS_ERR() code if an error
4422 * occurred while trying to acquire the GPIO.
4424 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4427 enum gpiod_flags flags)
4429 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4430 const char *devname = dev ? dev_name(dev) : "?";
4431 const char *label = con_id ?: devname;
4433 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true);
4435 EXPORT_SYMBOL_GPL(gpiod_get_index);
4438 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4440 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4441 * @con_id: function within the GPIO consumer
4442 * @index: index of the GPIO to obtain in the consumer
4443 * @flags: optional GPIO initialization flags
4445 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4446 * specified index was assigned to the requested function it will return NULL.
4447 * This is convenient for drivers that need to handle optional GPIOs.
4449 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4452 enum gpiod_flags flags)
4454 struct gpio_desc *desc;
4456 desc = gpiod_get_index(dev, con_id, index, flags);
4457 if (gpiod_not_found(desc))
4462 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4465 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4466 * @desc: gpio whose value will be assigned
4467 * @name: gpio line name
4468 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4469 * of_find_gpio() or of_get_gpio_hog()
4470 * @dflags: gpiod_flags - optional GPIO initialization flags
4472 int gpiod_hog(struct gpio_desc *desc, const char *name,
4473 unsigned long lflags, enum gpiod_flags dflags)
4475 struct gpio_device *gdev = desc->gdev;
4476 struct gpio_desc *local_desc;
4480 CLASS(gpio_chip_guard, guard)(desc);
4484 if (test_and_set_bit(FLAG_IS_HOGGED, &desc->flags))
4487 hwnum = gpio_chip_hwgpio(desc);
4489 local_desc = gpiochip_request_own_desc(guard.gc, hwnum, name,
4491 if (IS_ERR(local_desc)) {
4492 clear_bit(FLAG_IS_HOGGED, &desc->flags);
4493 ret = PTR_ERR(local_desc);
4494 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4495 name, gdev->label, hwnum, ret);
4499 gpiod_dbg(desc, "hogged as %s%s\n",
4500 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4501 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4502 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4508 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4509 * @gc: gpio chip to act on
4511 static void gpiochip_free_hogs(struct gpio_chip *gc)
4513 struct gpio_desc *desc;
4515 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
4516 gpiochip_free_own_desc(desc);
4520 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4521 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4522 * @con_id: function within the GPIO consumer
4523 * @flags: optional GPIO initialization flags
4525 * This function acquires all the GPIOs defined under a given function.
4527 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4528 * no GPIO has been assigned to the requested function, or another IS_ERR()
4529 * code if an error occurred while trying to acquire the GPIOs.
4531 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4533 enum gpiod_flags flags)
4535 struct gpio_desc *desc;
4536 struct gpio_descs *descs;
4537 struct gpio_array *array_info = NULL;
4538 struct gpio_chip *gc;
4539 int count, bitmap_size;
4542 count = gpiod_count(dev, con_id);
4544 return ERR_PTR(count);
4546 descs_size = struct_size(descs, desc, count);
4547 descs = kzalloc(descs_size, GFP_KERNEL);
4549 return ERR_PTR(-ENOMEM);
4551 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) {
4552 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4554 gpiod_put_array(descs);
4555 return ERR_CAST(desc);
4558 descs->desc[descs->ndescs] = desc;
4560 gc = gpiod_to_chip(desc);
4562 * If pin hardware number of array member 0 is also 0, select
4563 * its chip as a candidate for fast bitmap processing path.
4565 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4566 struct gpio_descs *array;
4568 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4571 array = krealloc(descs, descs_size +
4572 struct_size(array_info, invert_mask, 3 * bitmap_size),
4573 GFP_KERNEL | __GFP_ZERO);
4575 gpiod_put_array(descs);
4576 return ERR_PTR(-ENOMEM);
4581 array_info = (void *)descs + descs_size;
4582 array_info->get_mask = array_info->invert_mask +
4584 array_info->set_mask = array_info->get_mask +
4587 array_info->desc = descs->desc;
4588 array_info->size = count;
4589 array_info->chip = gc;
4590 bitmap_set(array_info->get_mask, descs->ndescs,
4591 count - descs->ndescs);
4592 bitmap_set(array_info->set_mask, descs->ndescs,
4593 count - descs->ndescs);
4594 descs->info = array_info;
4597 /* If there is no cache for fast bitmap processing path, continue */
4601 /* Unmark array members which don't belong to the 'fast' chip */
4602 if (array_info->chip != gc) {
4603 __clear_bit(descs->ndescs, array_info->get_mask);
4604 __clear_bit(descs->ndescs, array_info->set_mask);
4607 * Detect array members which belong to the 'fast' chip
4608 * but their pins are not in hardware order.
4610 else if (gpio_chip_hwgpio(desc) != descs->ndescs) {
4612 * Don't use fast path if all array members processed so
4613 * far belong to the same chip as this one but its pin
4614 * hardware number is different from its array index.
4616 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4619 __clear_bit(descs->ndescs,
4620 array_info->get_mask);
4621 __clear_bit(descs->ndescs,
4622 array_info->set_mask);
4625 /* Exclude open drain or open source from fast output */
4626 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4627 gpiochip_line_is_open_source(gc, descs->ndescs))
4628 __clear_bit(descs->ndescs,
4629 array_info->set_mask);
4630 /* Identify 'fast' pins which require invertion */
4631 if (gpiod_is_active_low(desc))
4632 __set_bit(descs->ndescs,
4633 array_info->invert_mask);
4638 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4639 array_info->chip->label, array_info->size,
4640 *array_info->get_mask, *array_info->set_mask,
4641 *array_info->invert_mask);
4644 EXPORT_SYMBOL_GPL(gpiod_get_array);
4647 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4649 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4650 * @con_id: function within the GPIO consumer
4651 * @flags: optional GPIO initialization flags
4653 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4654 * assigned to the requested function it will return NULL.
4656 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4658 enum gpiod_flags flags)
4660 struct gpio_descs *descs;
4662 descs = gpiod_get_array(dev, con_id, flags);
4663 if (gpiod_not_found(descs))
4668 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4671 * gpiod_put - dispose of a GPIO descriptor
4672 * @desc: GPIO descriptor to dispose of
4674 * No descriptor can be used after gpiod_put() has been called on it.
4676 void gpiod_put(struct gpio_desc *desc)
4681 EXPORT_SYMBOL_GPL(gpiod_put);
4684 * gpiod_put_array - dispose of multiple GPIO descriptors
4685 * @descs: struct gpio_descs containing an array of descriptors
4687 void gpiod_put_array(struct gpio_descs *descs)
4691 for (i = 0; i < descs->ndescs; i++)
4692 gpiod_put(descs->desc[i]);
4696 EXPORT_SYMBOL_GPL(gpiod_put_array);
4698 static int gpio_stub_drv_probe(struct device *dev)
4701 * The DT node of some GPIO chips have a "compatible" property, but
4702 * never have a struct device added and probed by a driver to register
4703 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4704 * the consumers of the GPIO chip to get probe deferred forever because
4705 * they will be waiting for a device associated with the GPIO chip
4706 * firmware node to get added and bound to a driver.
4708 * To allow these consumers to probe, we associate the struct
4709 * gpio_device of the GPIO chip with the firmware node and then simply
4710 * bind it to this stub driver.
4715 static struct device_driver gpio_stub_drv = {
4716 .name = "gpio_stub_drv",
4717 .bus = &gpio_bus_type,
4718 .probe = gpio_stub_drv_probe,
4721 static int __init gpiolib_dev_init(void)
4725 /* Register GPIO sysfs bus */
4726 ret = bus_register(&gpio_bus_type);
4728 pr_err("gpiolib: could not register GPIO bus type\n");
4732 ret = driver_register(&gpio_stub_drv);
4734 pr_err("gpiolib: could not register GPIO stub driver\n");
4735 bus_unregister(&gpio_bus_type);
4739 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4741 pr_err("gpiolib: failed to allocate char dev region\n");
4742 driver_unregister(&gpio_stub_drv);
4743 bus_unregister(&gpio_bus_type);
4747 gpiolib_initialized = true;
4748 gpiochip_setup_devs();
4750 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4751 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4752 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4756 core_initcall(gpiolib_dev_init);
4758 #ifdef CONFIG_DEBUG_FS
4760 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4762 bool active_low, is_irq, is_out;
4763 unsigned int gpio = gdev->base;
4764 struct gpio_desc *desc;
4765 struct gpio_chip *gc;
4768 guard(srcu)(&gdev->srcu);
4770 gc = srcu_dereference(gdev->chip, &gdev->srcu);
4772 seq_puts(s, "Underlying GPIO chip is gone\n");
4776 for_each_gpio_desc(gc, desc) {
4777 guard(srcu)(&desc->srcu);
4778 if (test_bit(FLAG_REQUESTED, &desc->flags)) {
4779 gpiod_get_direction(desc);
4780 is_out = test_bit(FLAG_IS_OUT, &desc->flags);
4781 value = gpio_chip_get_value(gc, desc);
4782 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
4783 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4784 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
4785 gpio, desc->name ?: "", gpiod_get_label(desc),
4786 is_out ? "out" : "in ",
4787 value >= 0 ? (value ? "hi" : "lo") : "? ",
4788 is_irq ? "IRQ " : "",
4789 active_low ? "ACTIVE LOW" : "");
4790 } else if (desc->name) {
4791 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
4798 struct gpiolib_seq_priv {
4803 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4805 struct gpiolib_seq_priv *priv;
4806 struct gpio_device *gdev;
4807 loff_t index = *pos;
4809 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
4814 priv->idx = srcu_read_lock(&gpio_devices_srcu);
4816 list_for_each_entry_srcu(gdev, &gpio_devices, list,
4817 srcu_read_lock_held(&gpio_devices_srcu)) {
4825 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4827 struct gpiolib_seq_priv *priv = s->private;
4828 struct gpio_device *gdev = v, *next;
4830 next = list_entry_rcu(gdev->list.next, struct gpio_device, list);
4831 gdev = &next->list == &gpio_devices ? NULL : next;
4832 priv->newline = true;
4838 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4840 struct gpiolib_seq_priv *priv = s->private;
4842 srcu_read_unlock(&gpio_devices_srcu, priv->idx);
4846 static int gpiolib_seq_show(struct seq_file *s, void *v)
4848 struct gpiolib_seq_priv *priv = s->private;
4849 struct gpio_device *gdev = v;
4850 struct gpio_chip *gc;
4851 struct device *parent;
4853 guard(srcu)(&gdev->srcu);
4855 gc = srcu_dereference(gdev->chip, &gdev->srcu);
4857 seq_printf(s, "%s%s: (dangling chip)",
4858 priv->newline ? "\n" : "",
4859 dev_name(&gdev->dev));
4863 seq_printf(s, "%s%s: GPIOs %d-%d", priv->newline ? "\n" : "",
4864 dev_name(&gdev->dev),
4865 gdev->base, gdev->base + gdev->ngpio - 1);
4866 parent = gc->parent;
4868 seq_printf(s, ", parent: %s/%s",
4869 parent->bus ? parent->bus->name : "no-bus",
4872 seq_printf(s, ", %s", gc->label);
4874 seq_printf(s, ", can sleep");
4875 seq_printf(s, ":\n");
4878 gc->dbg_show(s, gc);
4880 gpiolib_dbg_show(s, gdev);
4885 static const struct seq_operations gpiolib_sops = {
4886 .start = gpiolib_seq_start,
4887 .next = gpiolib_seq_next,
4888 .stop = gpiolib_seq_stop,
4889 .show = gpiolib_seq_show,
4891 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4893 static int __init gpiolib_debugfs_init(void)
4895 /* /sys/kernel/debug/gpio */
4896 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4899 subsys_initcall(gpiolib_debugfs_init);
4901 #endif /* DEBUG_FS */