1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitmap.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/interrupt.h>
8 #include <linux/spinlock.h>
9 #include <linux/list.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/debugfs.h>
13 #include <linux/seq_file.h>
14 #include <linux/gpio.h>
15 #include <linux/idr.h>
16 #include <linux/slab.h>
17 #include <linux/acpi.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/gpio/machine.h>
20 #include <linux/pinctrl/consumer.h>
22 #include <linux/compat.h>
23 #include <linux/file.h>
24 #include <uapi/linux/gpio.h>
27 #include "gpiolib-of.h"
28 #include "gpiolib-acpi.h"
29 #include "gpiolib-cdev.h"
30 #include "gpiolib-sysfs.h"
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/gpio.h>
35 /* Implementation infrastructure for GPIO interfaces.
37 * The GPIO programming interface allows for inlining speed-critical
38 * get/set operations for common cases, so that access to SOC-integrated
39 * GPIOs can sometimes cost only an instruction or two per bit.
43 /* When debugging, extend minimal trust to callers and platform code.
44 * Also emit diagnostic messages that may help initial bringup, when
45 * board setup or driver bugs are most common.
47 * Otherwise, minimize overhead in what may be bitbanging codepaths.
50 #define extra_checks 1
52 #define extra_checks 0
55 /* Device and char device-related information */
56 static DEFINE_IDA(gpio_ida);
57 static dev_t gpio_devt;
58 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
59 static int gpio_bus_match(struct device *dev, struct device_driver *drv);
60 static struct bus_type gpio_bus_type = {
62 .match = gpio_bus_match,
66 * Number of GPIOs to use for the fast path in set array
68 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
70 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
71 * While any GPIO is requested, its gpio_chip is not removable;
72 * each GPIO's "requested" flag serves as a lock and refcount.
74 DEFINE_SPINLOCK(gpio_lock);
76 static DEFINE_MUTEX(gpio_lookup_lock);
77 static LIST_HEAD(gpio_lookup_list);
78 LIST_HEAD(gpio_devices);
80 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
81 static LIST_HEAD(gpio_machine_hogs);
83 static void gpiochip_free_hogs(struct gpio_chip *gc);
84 static int gpiochip_add_irqchip(struct gpio_chip *gc,
85 struct lock_class_key *lock_key,
86 struct lock_class_key *request_key);
87 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
88 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
89 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
90 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
92 static bool gpiolib_initialized;
94 static inline void desc_set_label(struct gpio_desc *d, const char *label)
100 * gpio_to_desc - Convert a GPIO number to its descriptor
101 * @gpio: global GPIO number
104 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
105 * with the given number exists in the system.
107 struct gpio_desc *gpio_to_desc(unsigned gpio)
109 struct gpio_device *gdev;
112 spin_lock_irqsave(&gpio_lock, flags);
114 list_for_each_entry(gdev, &gpio_devices, list) {
115 if (gdev->base <= gpio &&
116 gdev->base + gdev->ngpio > gpio) {
117 spin_unlock_irqrestore(&gpio_lock, flags);
118 return &gdev->descs[gpio - gdev->base];
122 spin_unlock_irqrestore(&gpio_lock, flags);
124 if (!gpio_is_valid(gpio))
125 pr_warn("invalid GPIO %d\n", gpio);
129 EXPORT_SYMBOL_GPL(gpio_to_desc);
132 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
133 * hardware number for this chip
135 * @hwnum: hardware number of the GPIO for this chip
138 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
139 * in the given chip for the specified hardware number.
141 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
144 struct gpio_device *gdev = gc->gpiodev;
146 if (hwnum >= gdev->ngpio)
147 return ERR_PTR(-EINVAL);
149 return &gdev->descs[hwnum];
151 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
154 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
155 * @desc: GPIO descriptor
157 * This should disappear in the future but is needed since we still
158 * use GPIO numbers for error messages and sysfs nodes.
161 * The global GPIO number for the GPIO specified by its descriptor.
163 int desc_to_gpio(const struct gpio_desc *desc)
165 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
167 EXPORT_SYMBOL_GPL(desc_to_gpio);
171 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
172 * @desc: descriptor to return the chip of
174 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
176 if (!desc || !desc->gdev)
178 return desc->gdev->chip;
180 EXPORT_SYMBOL_GPL(gpiod_to_chip);
182 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
183 static int gpiochip_find_base(int ngpio)
185 struct gpio_device *gdev;
186 int base = ARCH_NR_GPIOS - ngpio;
188 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
189 /* found a free space? */
190 if (gdev->base + gdev->ngpio <= base)
193 /* nope, check the space right before the chip */
194 base = gdev->base - ngpio;
197 if (gpio_is_valid(base)) {
198 pr_debug("%s: found new base at %d\n", __func__, base);
201 pr_err("%s: cannot find free range\n", __func__);
207 * gpiod_get_direction - return the current direction of a GPIO
208 * @desc: GPIO to get the direction of
210 * Returns 0 for output, 1 for input, or an error code in case of error.
212 * This function may sleep if gpiod_cansleep() is true.
214 int gpiod_get_direction(struct gpio_desc *desc)
216 struct gpio_chip *gc;
220 gc = gpiod_to_chip(desc);
221 offset = gpio_chip_hwgpio(desc);
224 * Open drain emulation using input mode may incorrectly report
225 * input here, fix that up.
227 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
228 test_bit(FLAG_IS_OUT, &desc->flags))
231 if (!gc->get_direction)
234 ret = gc->get_direction(gc, offset);
238 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
242 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
246 EXPORT_SYMBOL_GPL(gpiod_get_direction);
249 * Add a new chip to the global chips list, keeping the list of chips sorted
250 * by range(means [base, base + ngpio - 1]) order.
252 * Return -EBUSY if the new chip overlaps with some other chip's integer
255 static int gpiodev_add_to_list(struct gpio_device *gdev)
257 struct gpio_device *prev, *next;
259 if (list_empty(&gpio_devices)) {
260 /* initial entry in list */
261 list_add_tail(&gdev->list, &gpio_devices);
265 next = list_entry(gpio_devices.next, struct gpio_device, list);
266 if (gdev->base + gdev->ngpio <= next->base) {
267 /* add before first entry */
268 list_add(&gdev->list, &gpio_devices);
272 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
273 if (prev->base + prev->ngpio <= gdev->base) {
274 /* add behind last entry */
275 list_add_tail(&gdev->list, &gpio_devices);
279 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
280 /* at the end of the list */
281 if (&next->list == &gpio_devices)
284 /* add between prev and next */
285 if (prev->base + prev->ngpio <= gdev->base
286 && gdev->base + gdev->ngpio <= next->base) {
287 list_add(&gdev->list, &prev->list);
292 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
297 * Convert a GPIO name to its descriptor
298 * Note that there is no guarantee that GPIO names are globally unique!
299 * Hence this function will return, if it exists, a reference to the first GPIO
300 * line found that matches the given name.
302 static struct gpio_desc *gpio_name_to_desc(const char * const name)
304 struct gpio_device *gdev;
310 spin_lock_irqsave(&gpio_lock, flags);
312 list_for_each_entry(gdev, &gpio_devices, list) {
315 for (i = 0; i != gdev->ngpio; ++i) {
316 struct gpio_desc *desc = &gdev->descs[i];
321 if (!strcmp(desc->name, name)) {
322 spin_unlock_irqrestore(&gpio_lock, flags);
328 spin_unlock_irqrestore(&gpio_lock, flags);
334 * Take the names from gc->names and assign them to their GPIO descriptors.
335 * Warn if a name is already used for a GPIO line on a different GPIO chip.
338 * 1. Non-unique names are still accepted,
339 * 2. Name collisions within the same GPIO chip are not reported.
341 static int gpiochip_set_desc_names(struct gpio_chip *gc)
343 struct gpio_device *gdev = gc->gpiodev;
346 /* First check all names if they are unique */
347 for (i = 0; i != gc->ngpio; ++i) {
348 struct gpio_desc *gpio;
350 gpio = gpio_name_to_desc(gc->names[i]);
353 "Detected name collision for GPIO name '%s'\n",
357 /* Then add all names to the GPIO descriptors */
358 for (i = 0; i != gc->ngpio; ++i)
359 gdev->descs[i].name = gc->names[i];
365 * devprop_gpiochip_set_names - Set GPIO line names using device properties
366 * @chip: GPIO chip whose lines should be named, if possible
368 * Looks for device property "gpio-line-names" and if it exists assigns
369 * GPIO line names for the chip. The memory allocated for the assigned
370 * names belong to the underlying firmware node and should not be released
373 static int devprop_gpiochip_set_names(struct gpio_chip *chip)
375 struct gpio_device *gdev = chip->gpiodev;
376 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
381 count = fwnode_property_string_array_count(fwnode, "gpio-line-names");
385 if (count > gdev->ngpio) {
386 dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
391 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
395 ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
398 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
403 for (i = 0; i < count; i++)
404 gdev->descs[i].name = names[i];
411 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
415 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
419 /* Assume by default all GPIOs are valid */
420 bitmap_fill(p, gc->ngpio);
425 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
427 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
430 gc->valid_mask = gpiochip_allocate_mask(gc);
437 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
439 if (gc->init_valid_mask)
440 return gc->init_valid_mask(gc,
447 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
449 bitmap_free(gc->valid_mask);
450 gc->valid_mask = NULL;
453 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
455 if (gc->add_pin_ranges)
456 return gc->add_pin_ranges(gc);
461 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
464 /* No mask means all valid */
465 if (likely(!gc->valid_mask))
467 return test_bit(offset, gc->valid_mask);
469 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
471 static void gpiodevice_release(struct device *dev)
473 struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
476 spin_lock_irqsave(&gpio_lock, flags);
477 list_del(&gdev->list);
478 spin_unlock_irqrestore(&gpio_lock, flags);
480 ida_free(&gpio_ida, gdev->id);
481 kfree_const(gdev->label);
486 #ifdef CONFIG_GPIO_CDEV
487 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
488 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
491 * gpiolib_cdev_register() indirectly calls device_add(), which is still
492 * required even when cdev is not selected.
494 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
495 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
498 static int gpiochip_setup_dev(struct gpio_device *gdev)
502 ret = gcdev_register(gdev, gpio_devt);
506 ret = gpiochip_sysfs_register(gdev);
508 goto err_remove_device;
510 /* From this point, the .release() function cleans up gpio_device */
511 gdev->dev.release = gpiodevice_release;
512 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
513 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
518 gcdev_unregister(gdev);
522 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
524 struct gpio_desc *desc;
527 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
529 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
534 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
537 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
539 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
540 __func__, gc->label, hog->chip_hwnum, rv);
543 static void machine_gpiochip_add(struct gpio_chip *gc)
545 struct gpiod_hog *hog;
547 mutex_lock(&gpio_machine_hogs_mutex);
549 list_for_each_entry(hog, &gpio_machine_hogs, list) {
550 if (!strcmp(gc->label, hog->chip_label))
551 gpiochip_machine_hog(gc, hog);
554 mutex_unlock(&gpio_machine_hogs_mutex);
557 static void gpiochip_setup_devs(void)
559 struct gpio_device *gdev;
562 list_for_each_entry(gdev, &gpio_devices, list) {
563 ret = gpiochip_setup_dev(gdev);
566 "Failed to initialize gpio device (%d)\n", ret);
570 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
571 struct lock_class_key *lock_key,
572 struct lock_class_key *request_key)
574 struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
579 struct gpio_device *gdev;
582 * First: allocate and populate the internal stat container, and
583 * set up the struct device.
585 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
588 gdev->dev.bus = &gpio_bus_type;
592 gdev->dev.parent = gc->parent;
593 gdev->dev.of_node = gc->parent->of_node;
596 of_gpio_dev_init(gc, gdev);
599 * Assign fwnode depending on the result of the previous calls,
600 * if none of them succeed, assign it to the parent's one.
602 gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode;
604 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
610 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
614 device_initialize(&gdev->dev);
615 if (gc->parent && gc->parent->driver)
616 gdev->owner = gc->parent->driver->owner;
618 /* TODO: remove chip->owner */
619 gdev->owner = gc->owner;
621 gdev->owner = THIS_MODULE;
623 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
626 goto err_free_dev_name;
629 if (gc->ngpio == 0) {
630 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
635 if (gc->ngpio > FASTPATH_NGPIO)
636 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
637 gc->ngpio, FASTPATH_NGPIO);
639 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
645 gdev->ngpio = gc->ngpio;
648 spin_lock_irqsave(&gpio_lock, flags);
651 * TODO: this allocates a Linux GPIO number base in the global
652 * GPIO numberspace for this chip. In the long run we want to
653 * get *rid* of this numberspace and use only descriptors, but
654 * it may be a pipe dream. It will not happen before we get rid
655 * of the sysfs interface anyways.
658 base = gpiochip_find_base(gc->ngpio);
661 spin_unlock_irqrestore(&gpio_lock, flags);
665 * TODO: it should not be necessary to reflect the assigned
666 * base outside of the GPIO subsystem. Go over drivers and
667 * see if anyone makes use of this, else drop this and assign
674 ret = gpiodev_add_to_list(gdev);
676 spin_unlock_irqrestore(&gpio_lock, flags);
680 for (i = 0; i < gc->ngpio; i++)
681 gdev->descs[i].gdev = gdev;
683 spin_unlock_irqrestore(&gpio_lock, flags);
685 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
687 #ifdef CONFIG_PINCTRL
688 INIT_LIST_HEAD(&gdev->pin_ranges);
692 ret = gpiochip_set_desc_names(gc);
694 ret = devprop_gpiochip_set_names(gc);
696 goto err_remove_from_list;
698 ret = gpiochip_alloc_valid_mask(gc);
700 goto err_remove_from_list;
702 ret = of_gpiochip_add(gc);
704 goto err_free_gpiochip_mask;
706 ret = gpiochip_init_valid_mask(gc);
708 goto err_remove_of_chip;
710 for (i = 0; i < gc->ngpio; i++) {
711 struct gpio_desc *desc = &gdev->descs[i];
713 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
714 assign_bit(FLAG_IS_OUT,
715 &desc->flags, !gc->get_direction(gc, i));
717 assign_bit(FLAG_IS_OUT,
718 &desc->flags, !gc->direction_input);
722 ret = gpiochip_add_pin_ranges(gc);
724 goto err_remove_of_chip;
726 acpi_gpiochip_add(gc);
728 machine_gpiochip_add(gc);
730 ret = gpiochip_irqchip_init_valid_mask(gc);
732 goto err_remove_acpi_chip;
734 ret = gpiochip_irqchip_init_hw(gc);
736 goto err_remove_acpi_chip;
738 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
740 goto err_remove_irqchip_mask;
743 * By first adding the chardev, and then adding the device,
744 * we get a device node entry in sysfs under
745 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
746 * coldplug of device nodes and other udev business.
747 * We can do this only if gpiolib has been initialized.
748 * Otherwise, defer until later.
750 if (gpiolib_initialized) {
751 ret = gpiochip_setup_dev(gdev);
753 goto err_remove_irqchip;
758 gpiochip_irqchip_remove(gc);
759 err_remove_irqchip_mask:
760 gpiochip_irqchip_free_valid_mask(gc);
761 err_remove_acpi_chip:
762 acpi_gpiochip_remove(gc);
764 gpiochip_free_hogs(gc);
765 of_gpiochip_remove(gc);
766 err_free_gpiochip_mask:
767 gpiochip_remove_pin_ranges(gc);
768 gpiochip_free_valid_mask(gc);
769 err_remove_from_list:
770 spin_lock_irqsave(&gpio_lock, flags);
771 list_del(&gdev->list);
772 spin_unlock_irqrestore(&gpio_lock, flags);
774 kfree_const(gdev->label);
778 kfree(dev_name(&gdev->dev));
780 ida_free(&gpio_ida, gdev->id);
782 /* failures here can mean systems won't boot... */
783 if (ret != -EPROBE_DEFER) {
784 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
785 gdev->base, gdev->base + gdev->ngpio - 1,
786 gc->label ? : "generic", ret);
791 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
794 * gpiochip_get_data() - get per-subdriver data for the chip
798 * The per-subdriver data for the chip.
800 void *gpiochip_get_data(struct gpio_chip *gc)
802 return gc->gpiodev->data;
804 EXPORT_SYMBOL_GPL(gpiochip_get_data);
807 * gpiochip_remove() - unregister a gpio_chip
808 * @gc: the chip to unregister
810 * A gpio_chip with any GPIOs still requested may not be removed.
812 void gpiochip_remove(struct gpio_chip *gc)
814 struct gpio_device *gdev = gc->gpiodev;
818 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
819 gpiochip_sysfs_unregister(gdev);
820 gpiochip_free_hogs(gc);
821 /* Numb the device, cancelling all outstanding operations */
823 gpiochip_irqchip_remove(gc);
824 acpi_gpiochip_remove(gc);
825 of_gpiochip_remove(gc);
826 gpiochip_remove_pin_ranges(gc);
827 gpiochip_free_valid_mask(gc);
829 * We accept no more calls into the driver from this point, so
830 * NULL the driver data pointer
834 spin_lock_irqsave(&gpio_lock, flags);
835 for (i = 0; i < gdev->ngpio; i++) {
836 if (gpiochip_is_requested(gc, i))
839 spin_unlock_irqrestore(&gpio_lock, flags);
841 if (i != gdev->ngpio)
843 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
846 * The gpiochip side puts its use of the device to rest here:
847 * if there are no userspace clients, the chardev and device will
848 * be removed, else it will be dangling until the last user is
851 gcdev_unregister(gdev);
852 put_device(&gdev->dev);
854 EXPORT_SYMBOL_GPL(gpiochip_remove);
857 * gpiochip_find() - iterator for locating a specific gpio_chip
858 * @data: data to pass to match function
859 * @match: Callback function to check gpio_chip
861 * Similar to bus_find_device. It returns a reference to a gpio_chip as
862 * determined by a user supplied @match callback. The callback should return
863 * 0 if the device doesn't match and non-zero if it does. If the callback is
864 * non-zero, this function will return to the caller and not iterate over any
867 struct gpio_chip *gpiochip_find(void *data,
868 int (*match)(struct gpio_chip *gc,
871 struct gpio_device *gdev;
872 struct gpio_chip *gc = NULL;
875 spin_lock_irqsave(&gpio_lock, flags);
876 list_for_each_entry(gdev, &gpio_devices, list)
877 if (gdev->chip && match(gdev->chip, data)) {
882 spin_unlock_irqrestore(&gpio_lock, flags);
886 EXPORT_SYMBOL_GPL(gpiochip_find);
888 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
890 const char *name = data;
892 return !strcmp(gc->label, name);
895 static struct gpio_chip *find_chip_by_name(const char *name)
897 return gpiochip_find((void *)name, gpiochip_match_name);
900 #ifdef CONFIG_GPIOLIB_IRQCHIP
903 * The following is irqchip helper code for gpiochips.
906 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
908 struct gpio_irq_chip *girq = &gc->irq;
913 return girq->init_hw(gc);
916 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
918 struct gpio_irq_chip *girq = &gc->irq;
920 if (!girq->init_valid_mask)
923 girq->valid_mask = gpiochip_allocate_mask(gc);
924 if (!girq->valid_mask)
927 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
932 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
934 bitmap_free(gc->irq.valid_mask);
935 gc->irq.valid_mask = NULL;
938 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
941 if (!gpiochip_line_is_valid(gc, offset))
943 /* No mask means all valid */
944 if (likely(!gc->irq.valid_mask))
946 return test_bit(offset, gc->irq.valid_mask);
948 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
950 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
953 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
955 * @gc: the gpiochip to set the irqchip hierarchical handler to
956 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
957 * will then percolate up to the parent
959 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
960 struct irq_chip *irqchip)
962 /* DT will deal with mapping each IRQ as we go along */
963 if (is_of_node(gc->irq.fwnode))
967 * This is for legacy and boardfile "irqchip" fwnodes: allocate
968 * irqs upfront instead of dynamically since we don't have the
969 * dynamic type of allocation that hardware description languages
970 * provide. Once all GPIO drivers using board files are gone from
971 * the kernel we can delete this code, but for a transitional period
972 * it is necessary to keep this around.
974 if (is_fwnode_irqchip(gc->irq.fwnode)) {
978 for (i = 0; i < gc->ngpio; i++) {
979 struct irq_fwspec fwspec;
980 unsigned int parent_hwirq;
981 unsigned int parent_type;
982 struct gpio_irq_chip *girq = &gc->irq;
985 * We call the child to parent translation function
986 * only to check if the child IRQ is valid or not.
987 * Just pick the rising edge type here as that is what
988 * we likely need to support.
990 ret = girq->child_to_parent_hwirq(gc, i,
991 IRQ_TYPE_EDGE_RISING,
995 chip_err(gc, "skip set-up on hwirq %d\n",
1000 fwspec.fwnode = gc->irq.fwnode;
1001 /* This is the hwirq for the GPIO line side of things */
1002 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1003 /* Just pick something */
1004 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1005 fwspec.param_count = 2;
1006 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1007 /* just pick something */
1016 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1023 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1028 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1029 struct irq_fwspec *fwspec,
1030 unsigned long *hwirq,
1033 /* We support standard DT translation */
1034 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1035 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1038 /* This is for board files and others not using DT */
1039 if (is_fwnode_irqchip(fwspec->fwnode)) {
1042 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1045 WARN_ON(*type == IRQ_TYPE_NONE);
1051 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1053 unsigned int nr_irqs,
1056 struct gpio_chip *gc = d->host_data;
1057 irq_hw_number_t hwirq;
1058 unsigned int type = IRQ_TYPE_NONE;
1059 struct irq_fwspec *fwspec = data;
1061 unsigned int parent_hwirq;
1062 unsigned int parent_type;
1063 struct gpio_irq_chip *girq = &gc->irq;
1067 * The nr_irqs parameter is always one except for PCI multi-MSI
1068 * so this should not happen.
1070 WARN_ON(nr_irqs != 1);
1072 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1076 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1078 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1079 &parent_hwirq, &parent_type);
1081 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1084 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1087 * We set handle_bad_irq because the .set_type() should
1088 * always be invoked and set the right type of handler.
1090 irq_domain_set_info(d,
1099 /* This parent only handles asserted level IRQs */
1100 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1104 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1106 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1107 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1109 * If the parent irqdomain is msi, the interrupts have already
1110 * been allocated, so the EEXIST is good.
1112 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1116 "failed to allocate parent hwirq %d for hwirq %lu\n",
1117 parent_hwirq, hwirq);
1123 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1124 unsigned int offset)
1129 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1131 ops->activate = gpiochip_irq_domain_activate;
1132 ops->deactivate = gpiochip_irq_domain_deactivate;
1133 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1134 ops->free = irq_domain_free_irqs_common;
1137 * We only allow overriding the translate() function for
1138 * hierarchical chips, and this should only be done if the user
1139 * really need something other than 1:1 translation.
1141 if (!ops->translate)
1142 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1145 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1147 if (!gc->irq.child_to_parent_hwirq ||
1149 chip_err(gc, "missing irqdomain vital data\n");
1153 if (!gc->irq.child_offset_to_irq)
1154 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1156 if (!gc->irq.populate_parent_alloc_arg)
1157 gc->irq.populate_parent_alloc_arg =
1158 gpiochip_populate_parent_fwspec_twocell;
1160 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1162 gc->irq.domain = irq_domain_create_hierarchy(
1163 gc->irq.parent_domain,
1167 &gc->irq.child_irq_domain_ops,
1170 if (!gc->irq.domain)
1173 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1178 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1180 return !!gc->irq.parent_domain;
1183 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1184 unsigned int parent_hwirq,
1185 unsigned int parent_type)
1187 struct irq_fwspec *fwspec;
1189 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1193 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1194 fwspec->param_count = 2;
1195 fwspec->param[0] = parent_hwirq;
1196 fwspec->param[1] = parent_type;
1200 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1202 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1203 unsigned int parent_hwirq,
1204 unsigned int parent_type)
1206 struct irq_fwspec *fwspec;
1208 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1212 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1213 fwspec->param_count = 4;
1214 fwspec->param[0] = 0;
1215 fwspec->param[1] = parent_hwirq;
1216 fwspec->param[2] = 0;
1217 fwspec->param[3] = parent_type;
1221 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1225 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1230 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1235 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1238 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1239 * @d: the irqdomain used by this irqchip
1240 * @irq: the global irq number used by this GPIO irqchip irq
1241 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1243 * This function will set up the mapping for a certain IRQ line on a
1244 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1245 * stored inside the gpiochip.
1247 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1248 irq_hw_number_t hwirq)
1250 struct gpio_chip *gc = d->host_data;
1253 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1256 irq_set_chip_data(irq, gc);
1258 * This lock class tells lockdep that GPIO irqs are in a different
1259 * category than their parents, so it won't report false recursion.
1261 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1262 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1263 /* Chips that use nested thread handlers have them marked */
1264 if (gc->irq.threaded)
1265 irq_set_nested_thread(irq, 1);
1266 irq_set_noprobe(irq);
1268 if (gc->irq.num_parents == 1)
1269 ret = irq_set_parent(irq, gc->irq.parents[0]);
1270 else if (gc->irq.map)
1271 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1277 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1278 * is passed as default type.
1280 if (gc->irq.default_type != IRQ_TYPE_NONE)
1281 irq_set_irq_type(irq, gc->irq.default_type);
1285 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1287 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1289 struct gpio_chip *gc = d->host_data;
1291 if (gc->irq.threaded)
1292 irq_set_nested_thread(irq, 0);
1293 irq_set_chip_and_handler(irq, NULL, NULL);
1294 irq_set_chip_data(irq, NULL);
1296 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1298 static const struct irq_domain_ops gpiochip_domain_ops = {
1299 .map = gpiochip_irq_map,
1300 .unmap = gpiochip_irq_unmap,
1301 /* Virtually all GPIO irqchips are twocell:ed */
1302 .xlate = irq_domain_xlate_twocell,
1306 * TODO: move these activate/deactivate in under the hierarchicial
1307 * irqchip implementation as static once SPMI and SSBI (all external
1308 * users) are phased over.
1311 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1312 * @domain: The IRQ domain used by this IRQ chip
1313 * @data: Outermost irq_data associated with the IRQ
1314 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1316 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1317 * used as the activate function for the &struct irq_domain_ops. The host_data
1318 * for the IRQ domain must be the &struct gpio_chip.
1320 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1321 struct irq_data *data, bool reserve)
1323 struct gpio_chip *gc = domain->host_data;
1325 return gpiochip_lock_as_irq(gc, data->hwirq);
1327 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1330 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1331 * @domain: The IRQ domain used by this IRQ chip
1332 * @data: Outermost irq_data associated with the IRQ
1334 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1335 * be used as the deactivate function for the &struct irq_domain_ops. The
1336 * host_data for the IRQ domain must be the &struct gpio_chip.
1338 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1339 struct irq_data *data)
1341 struct gpio_chip *gc = domain->host_data;
1343 return gpiochip_unlock_as_irq(gc, data->hwirq);
1345 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1347 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1349 struct irq_domain *domain = gc->irq.domain;
1351 if (!gpiochip_irqchip_irq_valid(gc, offset))
1354 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1355 if (irq_domain_is_hierarchy(domain)) {
1356 struct irq_fwspec spec;
1358 spec.fwnode = domain->fwnode;
1359 spec.param_count = 2;
1360 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1361 spec.param[1] = IRQ_TYPE_NONE;
1363 return irq_create_fwspec_mapping(&spec);
1367 return irq_create_mapping(domain, offset);
1370 static int gpiochip_irq_reqres(struct irq_data *d)
1372 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1374 return gpiochip_reqres_irq(gc, d->hwirq);
1377 static void gpiochip_irq_relres(struct irq_data *d)
1379 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1381 gpiochip_relres_irq(gc, d->hwirq);
1384 static void gpiochip_irq_mask(struct irq_data *d)
1386 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1388 if (gc->irq.irq_mask)
1389 gc->irq.irq_mask(d);
1390 gpiochip_disable_irq(gc, d->hwirq);
1393 static void gpiochip_irq_unmask(struct irq_data *d)
1395 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1397 gpiochip_enable_irq(gc, d->hwirq);
1398 if (gc->irq.irq_unmask)
1399 gc->irq.irq_unmask(d);
1402 static void gpiochip_irq_enable(struct irq_data *d)
1404 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1406 gpiochip_enable_irq(gc, d->hwirq);
1407 gc->irq.irq_enable(d);
1410 static void gpiochip_irq_disable(struct irq_data *d)
1412 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1414 gc->irq.irq_disable(d);
1415 gpiochip_disable_irq(gc, d->hwirq);
1418 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1420 struct irq_chip *irqchip = gc->irq.chip;
1422 if (!irqchip->irq_request_resources &&
1423 !irqchip->irq_release_resources) {
1424 irqchip->irq_request_resources = gpiochip_irq_reqres;
1425 irqchip->irq_release_resources = gpiochip_irq_relres;
1427 if (WARN_ON(gc->irq.irq_enable))
1429 /* Check if the irqchip already has this hook... */
1430 if (irqchip->irq_enable == gpiochip_irq_enable ||
1431 irqchip->irq_mask == gpiochip_irq_mask) {
1433 * ...and if so, give a gentle warning that this is bad
1437 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1441 if (irqchip->irq_disable) {
1442 gc->irq.irq_disable = irqchip->irq_disable;
1443 irqchip->irq_disable = gpiochip_irq_disable;
1445 gc->irq.irq_mask = irqchip->irq_mask;
1446 irqchip->irq_mask = gpiochip_irq_mask;
1449 if (irqchip->irq_enable) {
1450 gc->irq.irq_enable = irqchip->irq_enable;
1451 irqchip->irq_enable = gpiochip_irq_enable;
1453 gc->irq.irq_unmask = irqchip->irq_unmask;
1454 irqchip->irq_unmask = gpiochip_irq_unmask;
1459 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1460 * @gc: the GPIO chip to add the IRQ chip to
1461 * @lock_key: lockdep class for IRQ lock
1462 * @request_key: lockdep class for IRQ request
1464 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1465 struct lock_class_key *lock_key,
1466 struct lock_class_key *request_key)
1468 struct irq_chip *irqchip = gc->irq.chip;
1469 const struct irq_domain_ops *ops = NULL;
1470 struct device_node *np;
1477 if (gc->irq.parent_handler && gc->can_sleep) {
1478 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1482 np = gc->gpiodev->dev.of_node;
1483 type = gc->irq.default_type;
1486 * Specifying a default trigger is a terrible idea if DT or ACPI is
1487 * used to configure the interrupts, as you may end up with
1488 * conflicting triggers. Tell the user, and reset to NONE.
1490 if (WARN(np && type != IRQ_TYPE_NONE,
1491 "%s: Ignoring %u default trigger\n", np->full_name, type))
1492 type = IRQ_TYPE_NONE;
1494 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1495 acpi_handle_warn(ACPI_HANDLE(gc->parent),
1496 "Ignoring %u default trigger\n", type);
1497 type = IRQ_TYPE_NONE;
1501 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1503 gc->to_irq = gpiochip_to_irq;
1504 gc->irq.default_type = type;
1505 gc->irq.lock_key = lock_key;
1506 gc->irq.request_key = request_key;
1508 /* If a parent irqdomain is provided, let's build a hierarchy */
1509 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1510 int ret = gpiochip_hierarchy_add_domain(gc);
1514 /* Some drivers provide custom irqdomain ops */
1515 if (gc->irq.domain_ops)
1516 ops = gc->irq.domain_ops;
1519 ops = &gpiochip_domain_ops;
1520 gc->irq.domain = irq_domain_add_simple(np,
1524 if (!gc->irq.domain)
1528 if (gc->irq.parent_handler) {
1529 void *data = gc->irq.parent_handler_data ?: gc;
1531 for (i = 0; i < gc->irq.num_parents; i++) {
1533 * The parent IRQ chip is already using the chip_data
1534 * for this IRQ chip, so our callbacks simply use the
1537 irq_set_chained_handler_and_data(gc->irq.parents[i],
1538 gc->irq.parent_handler,
1543 gpiochip_set_irq_hooks(gc);
1545 acpi_gpiochip_request_interrupts(gc);
1551 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1552 * @gc: the gpiochip to remove the irqchip from
1554 * This is called only from gpiochip_remove()
1556 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1558 struct irq_chip *irqchip = gc->irq.chip;
1559 unsigned int offset;
1561 acpi_gpiochip_free_interrupts(gc);
1563 if (irqchip && gc->irq.parent_handler) {
1564 struct gpio_irq_chip *irq = &gc->irq;
1567 for (i = 0; i < irq->num_parents; i++)
1568 irq_set_chained_handler_and_data(irq->parents[i],
1572 /* Remove all IRQ mappings and delete the domain */
1573 if (gc->irq.domain) {
1576 for (offset = 0; offset < gc->ngpio; offset++) {
1577 if (!gpiochip_irqchip_irq_valid(gc, offset))
1580 irq = irq_find_mapping(gc->irq.domain, offset);
1581 irq_dispose_mapping(irq);
1584 irq_domain_remove(gc->irq.domain);
1588 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1589 irqchip->irq_request_resources = NULL;
1590 irqchip->irq_release_resources = NULL;
1592 if (irqchip->irq_enable == gpiochip_irq_enable) {
1593 irqchip->irq_enable = gc->irq.irq_enable;
1594 irqchip->irq_disable = gc->irq.irq_disable;
1597 gc->irq.irq_enable = NULL;
1598 gc->irq.irq_disable = NULL;
1599 gc->irq.chip = NULL;
1601 gpiochip_irqchip_free_valid_mask(gc);
1605 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1606 * @gc: the gpiochip to add the irqchip to
1607 * @domain: the irqdomain to add to the gpiochip
1609 * This function adds an IRQ domain to the gpiochip.
1611 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1612 struct irq_domain *domain)
1617 gc->to_irq = gpiochip_to_irq;
1618 gc->irq.domain = domain;
1622 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1624 #else /* CONFIG_GPIOLIB_IRQCHIP */
1626 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1627 struct lock_class_key *lock_key,
1628 struct lock_class_key *request_key)
1632 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1634 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1639 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1643 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1646 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1649 * gpiochip_generic_request() - request the gpio function for a pin
1650 * @gc: the gpiochip owning the GPIO
1651 * @offset: the offset of the GPIO to request for GPIO function
1653 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
1655 #ifdef CONFIG_PINCTRL
1656 if (list_empty(&gc->gpiodev->pin_ranges))
1660 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1662 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1665 * gpiochip_generic_free() - free the gpio function from a pin
1666 * @gc: the gpiochip to request the gpio function for
1667 * @offset: the offset of the GPIO to free from GPIO function
1669 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
1671 #ifdef CONFIG_PINCTRL
1672 if (list_empty(&gc->gpiodev->pin_ranges))
1676 pinctrl_gpio_free(gc->gpiodev->base + offset);
1678 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1681 * gpiochip_generic_config() - apply configuration for a pin
1682 * @gc: the gpiochip owning the GPIO
1683 * @offset: the offset of the GPIO to apply the configuration
1684 * @config: the configuration to be applied
1686 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
1687 unsigned long config)
1689 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1691 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1693 #ifdef CONFIG_PINCTRL
1696 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1697 * @gc: the gpiochip to add the range for
1698 * @pctldev: the pin controller to map to
1699 * @gpio_offset: the start offset in the current gpio_chip number space
1700 * @pin_group: name of the pin group inside the pin controller
1702 * Calling this function directly from a DeviceTree-supported
1703 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1704 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1705 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1707 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1708 struct pinctrl_dev *pctldev,
1709 unsigned int gpio_offset, const char *pin_group)
1711 struct gpio_pin_range *pin_range;
1712 struct gpio_device *gdev = gc->gpiodev;
1715 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1717 chip_err(gc, "failed to allocate pin ranges\n");
1721 /* Use local offset as range ID */
1722 pin_range->range.id = gpio_offset;
1723 pin_range->range.gc = gc;
1724 pin_range->range.name = gc->label;
1725 pin_range->range.base = gdev->base + gpio_offset;
1726 pin_range->pctldev = pctldev;
1728 ret = pinctrl_get_group_pins(pctldev, pin_group,
1729 &pin_range->range.pins,
1730 &pin_range->range.npins);
1736 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1738 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1739 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1740 pinctrl_dev_get_devname(pctldev), pin_group);
1742 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1746 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1749 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1750 * @gc: the gpiochip to add the range for
1751 * @pinctl_name: the dev_name() of the pin controller to map to
1752 * @gpio_offset: the start offset in the current gpio_chip number space
1753 * @pin_offset: the start offset in the pin controller number space
1754 * @npins: the number of pins from the offset of each pin space (GPIO and
1755 * pin controller) to accumulate in this range
1758 * 0 on success, or a negative error-code on failure.
1760 * Calling this function directly from a DeviceTree-supported
1761 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1762 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1763 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1765 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1766 unsigned int gpio_offset, unsigned int pin_offset,
1769 struct gpio_pin_range *pin_range;
1770 struct gpio_device *gdev = gc->gpiodev;
1773 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1775 chip_err(gc, "failed to allocate pin ranges\n");
1779 /* Use local offset as range ID */
1780 pin_range->range.id = gpio_offset;
1781 pin_range->range.gc = gc;
1782 pin_range->range.name = gc->label;
1783 pin_range->range.base = gdev->base + gpio_offset;
1784 pin_range->range.pin_base = pin_offset;
1785 pin_range->range.npins = npins;
1786 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1788 if (IS_ERR(pin_range->pctldev)) {
1789 ret = PTR_ERR(pin_range->pctldev);
1790 chip_err(gc, "could not create pin range\n");
1794 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1795 gpio_offset, gpio_offset + npins - 1,
1797 pin_offset, pin_offset + npins - 1);
1799 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1803 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1806 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1807 * @gc: the chip to remove all the mappings for
1809 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1811 struct gpio_pin_range *pin_range, *tmp;
1812 struct gpio_device *gdev = gc->gpiodev;
1814 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1815 list_del(&pin_range->node);
1816 pinctrl_remove_gpio_range(pin_range->pctldev,
1821 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1823 #endif /* CONFIG_PINCTRL */
1825 /* These "optional" allocation calls help prevent drivers from stomping
1826 * on each other, and help provide better diagnostics in debugfs.
1827 * They're called even less than the "set direction" calls.
1829 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
1831 struct gpio_chip *gc = desc->gdev->chip;
1833 unsigned long flags;
1837 label = kstrdup_const(label, GFP_KERNEL);
1842 spin_lock_irqsave(&gpio_lock, flags);
1844 /* NOTE: gpio_request() can be called in early boot,
1845 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1848 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1849 desc_set_label(desc, label ? : "?");
1852 goto out_free_unlock;
1856 /* gc->request may sleep */
1857 spin_unlock_irqrestore(&gpio_lock, flags);
1858 offset = gpio_chip_hwgpio(desc);
1859 if (gpiochip_line_is_valid(gc, offset))
1860 ret = gc->request(gc, offset);
1863 spin_lock_irqsave(&gpio_lock, flags);
1866 desc_set_label(desc, NULL);
1867 clear_bit(FLAG_REQUESTED, &desc->flags);
1868 goto out_free_unlock;
1871 if (gc->get_direction) {
1872 /* gc->get_direction may sleep */
1873 spin_unlock_irqrestore(&gpio_lock, flags);
1874 gpiod_get_direction(desc);
1875 spin_lock_irqsave(&gpio_lock, flags);
1877 spin_unlock_irqrestore(&gpio_lock, flags);
1881 spin_unlock_irqrestore(&gpio_lock, flags);
1887 * This descriptor validation needs to be inserted verbatim into each
1888 * function taking a descriptor, so we need to use a preprocessor
1889 * macro to avoid endless duplication. If the desc is NULL it is an
1890 * optional GPIO and calls should just bail out.
1892 static int validate_desc(const struct gpio_desc *desc, const char *func)
1897 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
1898 return PTR_ERR(desc);
1901 pr_warn("%s: invalid GPIO (no device)\n", func);
1904 if (!desc->gdev->chip) {
1905 dev_warn(&desc->gdev->dev,
1906 "%s: backing chip is gone\n", func);
1912 #define VALIDATE_DESC(desc) do { \
1913 int __valid = validate_desc(desc, __func__); \
1918 #define VALIDATE_DESC_VOID(desc) do { \
1919 int __valid = validate_desc(desc, __func__); \
1924 int gpiod_request(struct gpio_desc *desc, const char *label)
1926 int ret = -EPROBE_DEFER;
1927 struct gpio_device *gdev;
1929 VALIDATE_DESC(desc);
1932 if (try_module_get(gdev->owner)) {
1933 ret = gpiod_request_commit(desc, label);
1935 module_put(gdev->owner);
1937 get_device(&gdev->dev);
1941 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
1946 static bool gpiod_free_commit(struct gpio_desc *desc)
1949 unsigned long flags;
1950 struct gpio_chip *gc;
1954 gpiod_unexport(desc);
1956 spin_lock_irqsave(&gpio_lock, flags);
1958 gc = desc->gdev->chip;
1959 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
1961 spin_unlock_irqrestore(&gpio_lock, flags);
1962 might_sleep_if(gc->can_sleep);
1963 gc->free(gc, gpio_chip_hwgpio(desc));
1964 spin_lock_irqsave(&gpio_lock, flags);
1966 kfree_const(desc->label);
1967 desc_set_label(desc, NULL);
1968 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1969 clear_bit(FLAG_REQUESTED, &desc->flags);
1970 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1971 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1972 clear_bit(FLAG_PULL_UP, &desc->flags);
1973 clear_bit(FLAG_PULL_DOWN, &desc->flags);
1974 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
1975 clear_bit(FLAG_EDGE_RISING, &desc->flags);
1976 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
1977 clear_bit(FLAG_IS_HOGGED, &desc->flags);
1978 #ifdef CONFIG_OF_DYNAMIC
1981 #ifdef CONFIG_GPIO_CDEV
1982 WRITE_ONCE(desc->debounce_period_us, 0);
1987 spin_unlock_irqrestore(&gpio_lock, flags);
1988 blocking_notifier_call_chain(&desc->gdev->notifier,
1989 GPIOLINE_CHANGED_RELEASED, desc);
1994 void gpiod_free(struct gpio_desc *desc)
1996 if (desc && desc->gdev && gpiod_free_commit(desc)) {
1997 module_put(desc->gdev->owner);
1998 put_device(&desc->gdev->dev);
2000 WARN_ON(extra_checks);
2005 * gpiochip_is_requested - return string iff signal was requested
2006 * @gc: controller managing the signal
2007 * @offset: of signal within controller's 0..(ngpio - 1) range
2009 * Returns NULL if the GPIO is not currently requested, else a string.
2010 * The string returned is the label passed to gpio_request(); if none has been
2011 * passed it is a meaningless, non-NULL constant.
2013 * This function is for use by GPIO controller drivers. The label can
2014 * help with diagnostics, and knowing that the signal is used as a GPIO
2015 * can help avoid accidentally multiplexing it to another controller.
2017 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
2019 struct gpio_desc *desc;
2021 if (offset >= gc->ngpio)
2024 desc = gpiochip_get_desc(gc, offset);
2028 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2032 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2035 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2037 * @hwnum: hardware number of the GPIO for which to request the descriptor
2038 * @label: label for the GPIO
2039 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2040 * specify things like line inversion semantics with the machine flags
2041 * such as GPIO_OUT_LOW
2042 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2043 * can be used to specify consumer semantics such as open drain
2045 * Function allows GPIO chip drivers to request and use their own GPIO
2046 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2047 * function will not increase reference count of the GPIO chip module. This
2048 * allows the GPIO chip module to be unloaded as needed (we assume that the
2049 * GPIO chip driver handles freeing the GPIOs it has requested).
2052 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2055 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2058 enum gpio_lookup_flags lflags,
2059 enum gpiod_flags dflags)
2061 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2065 chip_err(gc, "failed to get GPIO descriptor\n");
2069 ret = gpiod_request_commit(desc, label);
2071 return ERR_PTR(ret);
2073 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2075 chip_err(gc, "setup of own GPIO %s failed\n", label);
2076 gpiod_free_commit(desc);
2077 return ERR_PTR(ret);
2082 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2085 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2086 * @desc: GPIO descriptor to free
2088 * Function frees the given GPIO requested previously with
2089 * gpiochip_request_own_desc().
2091 void gpiochip_free_own_desc(struct gpio_desc *desc)
2094 gpiod_free_commit(desc);
2096 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2099 * Drivers MUST set GPIO direction before making get/set calls. In
2100 * some cases this is done in early boot, before IRQs are enabled.
2102 * As a rule these aren't called more than once (except for drivers
2103 * using the open-drain emulation idiom) so these are natural places
2104 * to accumulate extra debugging checks. Note that we can't (yet)
2105 * rely on gpio_request() having been called beforehand.
2108 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2109 unsigned long config)
2111 if (!gc->set_config)
2114 return gc->set_config(gc, offset, config);
2117 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2118 enum pin_config_param mode,
2121 struct gpio_chip *gc = desc->gdev->chip;
2122 unsigned long config;
2124 config = pinconf_to_config_packed(mode, argument);
2125 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2128 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2129 enum pin_config_param mode,
2132 struct device *dev = &desc->gdev->dev;
2133 int gpio = gpio_chip_hwgpio(desc);
2136 ret = gpio_set_config_with_argument(desc, mode, argument);
2137 if (ret != -ENOTSUPP)
2141 case PIN_CONFIG_PERSIST_STATE:
2142 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2151 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2153 return gpio_set_config_with_argument(desc, mode, 0);
2156 static int gpio_set_bias(struct gpio_desc *desc)
2158 enum pin_config_param bias;
2161 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2162 bias = PIN_CONFIG_BIAS_DISABLE;
2163 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2164 bias = PIN_CONFIG_BIAS_PULL_UP;
2165 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2166 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2171 case PIN_CONFIG_BIAS_PULL_DOWN:
2172 case PIN_CONFIG_BIAS_PULL_UP:
2181 return gpio_set_config_with_argument_optional(desc, bias, arg);
2184 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2186 return gpio_set_config_with_argument_optional(desc,
2187 PIN_CONFIG_INPUT_DEBOUNCE,
2192 * gpiod_direction_input - set the GPIO direction to input
2193 * @desc: GPIO to set to input
2195 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2196 * be called safely on it.
2198 * Return 0 in case of success, else an error code.
2200 int gpiod_direction_input(struct gpio_desc *desc)
2202 struct gpio_chip *gc;
2205 VALIDATE_DESC(desc);
2206 gc = desc->gdev->chip;
2209 * It is legal to have no .get() and .direction_input() specified if
2210 * the chip is output-only, but you can't specify .direction_input()
2211 * and not support the .get() operation, that doesn't make sense.
2213 if (!gc->get && gc->direction_input) {
2215 "%s: missing get() but have direction_input()\n",
2221 * If we have a .direction_input() callback, things are simple,
2222 * just call it. Else we are some input-only chip so try to check the
2223 * direction (if .get_direction() is supported) else we silently
2224 * assume we are in input mode after this.
2226 if (gc->direction_input) {
2227 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2228 } else if (gc->get_direction &&
2229 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2231 "%s: missing direction_input() operation and line is output\n",
2236 clear_bit(FLAG_IS_OUT, &desc->flags);
2237 ret = gpio_set_bias(desc);
2240 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2244 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2246 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2248 struct gpio_chip *gc = desc->gdev->chip;
2253 * It's OK not to specify .direction_output() if the gpiochip is
2254 * output-only, but if there is then not even a .set() operation it
2255 * is pretty tricky to drive the output line.
2257 if (!gc->set && !gc->direction_output) {
2259 "%s: missing set() and direction_output() operations\n",
2264 if (gc->direction_output) {
2265 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2267 /* Check that we are in output mode if we can */
2268 if (gc->get_direction &&
2269 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2271 "%s: missing direction_output() operation\n",
2276 * If we can't actively set the direction, we are some
2277 * output-only chip, so just drive the output as desired.
2279 gc->set(gc, gpio_chip_hwgpio(desc), val);
2283 set_bit(FLAG_IS_OUT, &desc->flags);
2284 trace_gpio_value(desc_to_gpio(desc), 0, val);
2285 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2290 * gpiod_direction_output_raw - set the GPIO direction to output
2291 * @desc: GPIO to set to output
2292 * @value: initial output value of the GPIO
2294 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2295 * be called safely on it. The initial value of the output must be specified
2296 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2298 * Return 0 in case of success, else an error code.
2300 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2302 VALIDATE_DESC(desc);
2303 return gpiod_direction_output_raw_commit(desc, value);
2305 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2308 * gpiod_direction_output - set the GPIO direction to output
2309 * @desc: GPIO to set to output
2310 * @value: initial output value of the GPIO
2312 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2313 * be called safely on it. The initial value of the output must be specified
2314 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2317 * Return 0 in case of success, else an error code.
2319 int gpiod_direction_output(struct gpio_desc *desc, int value)
2323 VALIDATE_DESC(desc);
2324 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2329 /* GPIOs used for enabled IRQs shall not be set as output */
2330 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2331 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2333 "%s: tried to set a GPIO tied to an IRQ as output\n",
2338 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2339 /* First see if we can enable open drain in hardware */
2340 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2342 goto set_output_value;
2343 /* Emulate open drain by not actively driving the line high */
2345 ret = gpiod_direction_input(desc);
2346 goto set_output_flag;
2349 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2350 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2352 goto set_output_value;
2353 /* Emulate open source by not actively driving the line low */
2355 ret = gpiod_direction_input(desc);
2356 goto set_output_flag;
2359 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2363 ret = gpio_set_bias(desc);
2366 return gpiod_direction_output_raw_commit(desc, value);
2370 * When emulating open-source or open-drain functionalities by not
2371 * actively driving the line (setting mode to input) we still need to
2372 * set the IS_OUT flag or otherwise we won't be able to set the line
2376 set_bit(FLAG_IS_OUT, &desc->flags);
2379 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2382 * gpiod_set_config - sets @config for a GPIO
2383 * @desc: descriptor of the GPIO for which to set the configuration
2384 * @config: Same packed config format as generic pinconf
2387 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2390 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2392 struct gpio_chip *gc;
2394 VALIDATE_DESC(desc);
2395 gc = desc->gdev->chip;
2397 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2399 EXPORT_SYMBOL_GPL(gpiod_set_config);
2402 * gpiod_set_debounce - sets @debounce time for a GPIO
2403 * @desc: descriptor of the GPIO for which to set debounce time
2404 * @debounce: debounce time in microseconds
2407 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2410 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2412 unsigned long config;
2414 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2415 return gpiod_set_config(desc, config);
2417 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2420 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2421 * @desc: descriptor of the GPIO for which to configure persistence
2422 * @transitory: True to lose state on suspend or reset, false for persistence
2425 * 0 on success, otherwise a negative error code.
2427 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2429 VALIDATE_DESC(desc);
2431 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2432 * persistence state.
2434 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2436 /* If the driver supports it, set the persistence state now */
2437 return gpio_set_config_with_argument_optional(desc,
2438 PIN_CONFIG_PERSIST_STATE,
2441 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2444 * gpiod_is_active_low - test whether a GPIO is active-low or not
2445 * @desc: the gpio descriptor to test
2447 * Returns 1 if the GPIO is active-low, 0 otherwise.
2449 int gpiod_is_active_low(const struct gpio_desc *desc)
2451 VALIDATE_DESC(desc);
2452 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2454 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2457 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2458 * @desc: the gpio descriptor to change
2460 void gpiod_toggle_active_low(struct gpio_desc *desc)
2462 VALIDATE_DESC_VOID(desc);
2463 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2465 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2467 /* I/O calls are only valid after configuration completed; the relevant
2468 * "is this a valid GPIO" error checks should already have been done.
2470 * "Get" operations are often inlinable as reading a pin value register,
2471 * and masking the relevant bit in that register.
2473 * When "set" operations are inlinable, they involve writing that mask to
2474 * one register to set a low value, or a different register to set it high.
2475 * Otherwise locking is needed, so there may be little value to inlining.
2477 *------------------------------------------------------------------------
2479 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2480 * have requested the GPIO. That can include implicit requesting by
2481 * a direction setting call. Marking a gpio as requested locks its chip
2482 * in memory, guaranteeing that these table lookups need no more locking
2483 * and that gpiochip_remove() will fail.
2485 * REVISIT when debugging, consider adding some instrumentation to ensure
2486 * that the GPIO was actually requested.
2489 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2491 struct gpio_chip *gc;
2495 gc = desc->gdev->chip;
2496 offset = gpio_chip_hwgpio(desc);
2497 value = gc->get ? gc->get(gc, offset) : -EIO;
2498 value = value < 0 ? value : !!value;
2499 trace_gpio_value(desc_to_gpio(desc), 1, value);
2503 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2504 unsigned long *mask, unsigned long *bits)
2506 if (gc->get_multiple) {
2507 return gc->get_multiple(gc, mask, bits);
2508 } else if (gc->get) {
2511 for_each_set_bit(i, mask, gc->ngpio) {
2512 value = gc->get(gc, i);
2515 __assign_bit(i, bits, value);
2522 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2523 unsigned int array_size,
2524 struct gpio_desc **desc_array,
2525 struct gpio_array *array_info,
2526 unsigned long *value_bitmap)
2531 * Validate array_info against desc_array and its size.
2532 * It should immediately follow desc_array if both
2533 * have been obtained from the same gpiod_get_array() call.
2535 if (array_info && array_info->desc == desc_array &&
2536 array_size <= array_info->size &&
2537 (void *)array_info == desc_array + array_info->size) {
2539 WARN_ON(array_info->chip->can_sleep);
2541 ret = gpio_chip_get_multiple(array_info->chip,
2542 array_info->get_mask,
2547 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2548 bitmap_xor(value_bitmap, value_bitmap,
2549 array_info->invert_mask, array_size);
2551 i = find_first_zero_bit(array_info->get_mask, array_size);
2552 if (i == array_size)
2558 while (i < array_size) {
2559 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2560 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2561 unsigned long *mask, *bits;
2564 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2567 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2569 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2574 bits = mask + BITS_TO_LONGS(gc->ngpio);
2575 bitmap_zero(mask, gc->ngpio);
2578 WARN_ON(gc->can_sleep);
2580 /* collect all inputs belonging to the same chip */
2583 const struct gpio_desc *desc = desc_array[i];
2584 int hwgpio = gpio_chip_hwgpio(desc);
2586 __set_bit(hwgpio, mask);
2590 i = find_next_zero_bit(array_info->get_mask,
2592 } while ((i < array_size) &&
2593 (desc_array[i]->gdev->chip == gc));
2595 ret = gpio_chip_get_multiple(gc, mask, bits);
2597 if (mask != fastpath)
2602 for (j = first; j < i; ) {
2603 const struct gpio_desc *desc = desc_array[j];
2604 int hwgpio = gpio_chip_hwgpio(desc);
2605 int value = test_bit(hwgpio, bits);
2607 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2609 __assign_bit(j, value_bitmap, value);
2610 trace_gpio_value(desc_to_gpio(desc), 1, value);
2614 j = find_next_zero_bit(array_info->get_mask, i,
2618 if (mask != fastpath)
2625 * gpiod_get_raw_value() - return a gpio's raw value
2626 * @desc: gpio whose value will be returned
2628 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2629 * its ACTIVE_LOW status, or negative errno on failure.
2631 * This function can be called from contexts where we cannot sleep, and will
2632 * complain if the GPIO chip functions potentially sleep.
2634 int gpiod_get_raw_value(const struct gpio_desc *desc)
2636 VALIDATE_DESC(desc);
2637 /* Should be using gpiod_get_raw_value_cansleep() */
2638 WARN_ON(desc->gdev->chip->can_sleep);
2639 return gpiod_get_raw_value_commit(desc);
2641 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2644 * gpiod_get_value() - return a gpio's value
2645 * @desc: gpio whose value will be returned
2647 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2648 * account, or negative errno on failure.
2650 * This function can be called from contexts where we cannot sleep, and will
2651 * complain if the GPIO chip functions potentially sleep.
2653 int gpiod_get_value(const struct gpio_desc *desc)
2657 VALIDATE_DESC(desc);
2658 /* Should be using gpiod_get_value_cansleep() */
2659 WARN_ON(desc->gdev->chip->can_sleep);
2661 value = gpiod_get_raw_value_commit(desc);
2665 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2670 EXPORT_SYMBOL_GPL(gpiod_get_value);
2673 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2674 * @array_size: number of elements in the descriptor array / value bitmap
2675 * @desc_array: array of GPIO descriptors whose values will be read
2676 * @array_info: information on applicability of fast bitmap processing path
2677 * @value_bitmap: bitmap to store the read values
2679 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2680 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2681 * else an error code.
2683 * This function can be called from contexts where we cannot sleep,
2684 * and it will complain if the GPIO chip functions potentially sleep.
2686 int gpiod_get_raw_array_value(unsigned int array_size,
2687 struct gpio_desc **desc_array,
2688 struct gpio_array *array_info,
2689 unsigned long *value_bitmap)
2693 return gpiod_get_array_value_complex(true, false, array_size,
2694 desc_array, array_info,
2697 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2700 * gpiod_get_array_value() - read values from an array of GPIOs
2701 * @array_size: number of elements in the descriptor array / value bitmap
2702 * @desc_array: array of GPIO descriptors whose values will be read
2703 * @array_info: information on applicability of fast bitmap processing path
2704 * @value_bitmap: bitmap to store the read values
2706 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2707 * into account. Return 0 in case of success, else an error code.
2709 * This function can be called from contexts where we cannot sleep,
2710 * and it will complain if the GPIO chip functions potentially sleep.
2712 int gpiod_get_array_value(unsigned int array_size,
2713 struct gpio_desc **desc_array,
2714 struct gpio_array *array_info,
2715 unsigned long *value_bitmap)
2719 return gpiod_get_array_value_complex(false, false, array_size,
2720 desc_array, array_info,
2723 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2726 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2727 * @desc: gpio descriptor whose state need to be set.
2728 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2730 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2733 struct gpio_chip *gc = desc->gdev->chip;
2734 int offset = gpio_chip_hwgpio(desc);
2737 ret = gc->direction_input(gc, offset);
2739 ret = gc->direction_output(gc, offset, 0);
2741 set_bit(FLAG_IS_OUT, &desc->flags);
2743 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2746 "%s: Error in set_value for open drain err %d\n",
2751 * _gpio_set_open_source_value() - Set the open source gpio's value.
2752 * @desc: gpio descriptor whose state need to be set.
2753 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2755 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2758 struct gpio_chip *gc = desc->gdev->chip;
2759 int offset = gpio_chip_hwgpio(desc);
2762 ret = gc->direction_output(gc, offset, 1);
2764 set_bit(FLAG_IS_OUT, &desc->flags);
2766 ret = gc->direction_input(gc, offset);
2768 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2771 "%s: Error in set_value for open source err %d\n",
2775 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2777 struct gpio_chip *gc;
2779 gc = desc->gdev->chip;
2780 trace_gpio_value(desc_to_gpio(desc), 0, value);
2781 gc->set(gc, gpio_chip_hwgpio(desc), value);
2785 * set multiple outputs on the same chip;
2786 * use the chip's set_multiple function if available;
2787 * otherwise set the outputs sequentially;
2788 * @chip: the GPIO chip we operate on
2789 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2790 * defines which outputs are to be changed
2791 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2792 * defines the values the outputs specified by mask are to be set to
2794 static void gpio_chip_set_multiple(struct gpio_chip *gc,
2795 unsigned long *mask, unsigned long *bits)
2797 if (gc->set_multiple) {
2798 gc->set_multiple(gc, mask, bits);
2802 /* set outputs if the corresponding mask bit is set */
2803 for_each_set_bit(i, mask, gc->ngpio)
2804 gc->set(gc, i, test_bit(i, bits));
2808 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
2809 unsigned int array_size,
2810 struct gpio_desc **desc_array,
2811 struct gpio_array *array_info,
2812 unsigned long *value_bitmap)
2817 * Validate array_info against desc_array and its size.
2818 * It should immediately follow desc_array if both
2819 * have been obtained from the same gpiod_get_array() call.
2821 if (array_info && array_info->desc == desc_array &&
2822 array_size <= array_info->size &&
2823 (void *)array_info == desc_array + array_info->size) {
2825 WARN_ON(array_info->chip->can_sleep);
2827 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2828 bitmap_xor(value_bitmap, value_bitmap,
2829 array_info->invert_mask, array_size);
2831 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2834 i = find_first_zero_bit(array_info->set_mask, array_size);
2835 if (i == array_size)
2841 while (i < array_size) {
2842 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2843 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2844 unsigned long *mask, *bits;
2847 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2850 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2852 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2857 bits = mask + BITS_TO_LONGS(gc->ngpio);
2858 bitmap_zero(mask, gc->ngpio);
2861 WARN_ON(gc->can_sleep);
2864 struct gpio_desc *desc = desc_array[i];
2865 int hwgpio = gpio_chip_hwgpio(desc);
2866 int value = test_bit(i, value_bitmap);
2869 * Pins applicable for fast input but not for
2870 * fast output processing may have been already
2871 * inverted inside the fast path, skip them.
2873 if (!raw && !(array_info &&
2874 test_bit(i, array_info->invert_mask)) &&
2875 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2877 trace_gpio_value(desc_to_gpio(desc), 0, value);
2879 * collect all normal outputs belonging to the same chip
2880 * open drain and open source outputs are set individually
2882 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2883 gpio_set_open_drain_value_commit(desc, value);
2884 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2885 gpio_set_open_source_value_commit(desc, value);
2887 __set_bit(hwgpio, mask);
2888 __assign_bit(hwgpio, bits, value);
2894 i = find_next_zero_bit(array_info->set_mask,
2896 } while ((i < array_size) &&
2897 (desc_array[i]->gdev->chip == gc));
2898 /* push collected bits to outputs */
2900 gpio_chip_set_multiple(gc, mask, bits);
2902 if (mask != fastpath)
2909 * gpiod_set_raw_value() - assign a gpio's raw value
2910 * @desc: gpio whose value will be assigned
2911 * @value: value to assign
2913 * Set the raw value of the GPIO, i.e. the value of its physical line without
2914 * regard for its ACTIVE_LOW status.
2916 * This function can be called from contexts where we cannot sleep, and will
2917 * complain if the GPIO chip functions potentially sleep.
2919 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2921 VALIDATE_DESC_VOID(desc);
2922 /* Should be using gpiod_set_raw_value_cansleep() */
2923 WARN_ON(desc->gdev->chip->can_sleep);
2924 gpiod_set_raw_value_commit(desc, value);
2926 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2929 * gpiod_set_value_nocheck() - set a GPIO line value without checking
2930 * @desc: the descriptor to set the value on
2931 * @value: value to set
2933 * This sets the value of a GPIO line backing a descriptor, applying
2934 * different semantic quirks like active low and open drain/source
2937 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
2939 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2941 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2942 gpio_set_open_drain_value_commit(desc, value);
2943 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2944 gpio_set_open_source_value_commit(desc, value);
2946 gpiod_set_raw_value_commit(desc, value);
2950 * gpiod_set_value() - assign a gpio's value
2951 * @desc: gpio whose value will be assigned
2952 * @value: value to assign
2954 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2955 * OPEN_DRAIN and OPEN_SOURCE flags into account.
2957 * This function can be called from contexts where we cannot sleep, and will
2958 * complain if the GPIO chip functions potentially sleep.
2960 void gpiod_set_value(struct gpio_desc *desc, int value)
2962 VALIDATE_DESC_VOID(desc);
2963 /* Should be using gpiod_set_value_cansleep() */
2964 WARN_ON(desc->gdev->chip->can_sleep);
2965 gpiod_set_value_nocheck(desc, value);
2967 EXPORT_SYMBOL_GPL(gpiod_set_value);
2970 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
2971 * @array_size: number of elements in the descriptor array / value bitmap
2972 * @desc_array: array of GPIO descriptors whose values will be assigned
2973 * @array_info: information on applicability of fast bitmap processing path
2974 * @value_bitmap: bitmap of values to assign
2976 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2977 * without regard for their ACTIVE_LOW status.
2979 * This function can be called from contexts where we cannot sleep, and will
2980 * complain if the GPIO chip functions potentially sleep.
2982 int gpiod_set_raw_array_value(unsigned int array_size,
2983 struct gpio_desc **desc_array,
2984 struct gpio_array *array_info,
2985 unsigned long *value_bitmap)
2989 return gpiod_set_array_value_complex(true, false, array_size,
2990 desc_array, array_info, value_bitmap);
2992 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
2995 * gpiod_set_array_value() - assign values to an array of GPIOs
2996 * @array_size: number of elements in the descriptor array / value bitmap
2997 * @desc_array: array of GPIO descriptors whose values will be assigned
2998 * @array_info: information on applicability of fast bitmap processing path
2999 * @value_bitmap: bitmap of values to assign
3001 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3004 * This function can be called from contexts where we cannot sleep, and will
3005 * complain if the GPIO chip functions potentially sleep.
3007 int gpiod_set_array_value(unsigned int array_size,
3008 struct gpio_desc **desc_array,
3009 struct gpio_array *array_info,
3010 unsigned long *value_bitmap)
3014 return gpiod_set_array_value_complex(false, false, array_size,
3015 desc_array, array_info,
3018 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3021 * gpiod_cansleep() - report whether gpio value access may sleep
3022 * @desc: gpio to check
3025 int gpiod_cansleep(const struct gpio_desc *desc)
3027 VALIDATE_DESC(desc);
3028 return desc->gdev->chip->can_sleep;
3030 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3033 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3034 * @desc: gpio to set the consumer name on
3035 * @name: the new consumer name
3037 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3039 VALIDATE_DESC(desc);
3041 name = kstrdup_const(name, GFP_KERNEL);
3046 kfree_const(desc->label);
3047 desc_set_label(desc, name);
3051 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3054 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3055 * @desc: gpio whose IRQ will be returned (already requested)
3057 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3060 int gpiod_to_irq(const struct gpio_desc *desc)
3062 struct gpio_chip *gc;
3066 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3067 * requires this function to not return zero on an invalid descriptor
3068 * but rather a negative error number.
3070 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3073 gc = desc->gdev->chip;
3074 offset = gpio_chip_hwgpio(desc);
3076 int retirq = gc->to_irq(gc, offset);
3078 /* Zero means NO_IRQ */
3086 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3089 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3090 * @gc: the chip the GPIO to lock belongs to
3091 * @offset: the offset of the GPIO to lock as IRQ
3093 * This is used directly by GPIO drivers that want to lock down
3094 * a certain GPIO line to be used for IRQs.
3096 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3098 struct gpio_desc *desc;
3100 desc = gpiochip_get_desc(gc, offset);
3102 return PTR_ERR(desc);
3105 * If it's fast: flush the direction setting if something changed
3108 if (!gc->can_sleep && gc->get_direction) {
3109 int dir = gpiod_get_direction(desc);
3112 chip_err(gc, "%s: cannot get GPIO direction\n",
3118 /* To be valid for IRQ the line needs to be input or open drain */
3119 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3120 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3122 "%s: tried to flag a GPIO set as output for IRQ\n",
3127 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3128 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3131 * If the consumer has not set up a label (such as when the
3132 * IRQ is referenced from .to_irq()) we set up a label here
3133 * so it is clear this is used as an interrupt.
3136 desc_set_label(desc, "interrupt");
3140 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3143 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3144 * @gc: the chip the GPIO to lock belongs to
3145 * @offset: the offset of the GPIO to lock as IRQ
3147 * This is used directly by GPIO drivers that want to indicate
3148 * that a certain GPIO is no longer used exclusively for IRQ.
3150 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3152 struct gpio_desc *desc;
3154 desc = gpiochip_get_desc(gc, offset);
3158 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3159 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3161 /* If we only had this marking, erase it */
3162 if (desc->label && !strcmp(desc->label, "interrupt"))
3163 desc_set_label(desc, NULL);
3165 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3167 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3169 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3171 if (!IS_ERR(desc) &&
3172 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3173 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3175 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3177 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3179 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3181 if (!IS_ERR(desc) &&
3182 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3184 * We must not be output when using IRQ UNLESS we are
3187 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3188 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3189 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3192 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3194 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3196 if (offset >= gc->ngpio)
3199 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3201 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3203 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3207 if (!try_module_get(gc->gpiodev->owner))
3210 ret = gpiochip_lock_as_irq(gc, offset);
3212 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3213 module_put(gc->gpiodev->owner);
3218 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3220 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3222 gpiochip_unlock_as_irq(gc, offset);
3223 module_put(gc->gpiodev->owner);
3225 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3227 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3229 if (offset >= gc->ngpio)
3232 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3234 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3236 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3238 if (offset >= gc->ngpio)
3241 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3243 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3245 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3247 if (offset >= gc->ngpio)
3250 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3252 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3255 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3256 * @desc: gpio whose value will be returned
3258 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3259 * its ACTIVE_LOW status, or negative errno on failure.
3261 * This function is to be called from contexts that can sleep.
3263 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3265 might_sleep_if(extra_checks);
3266 VALIDATE_DESC(desc);
3267 return gpiod_get_raw_value_commit(desc);
3269 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3272 * gpiod_get_value_cansleep() - return a gpio's value
3273 * @desc: gpio whose value will be returned
3275 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3276 * account, or negative errno on failure.
3278 * This function is to be called from contexts that can sleep.
3280 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3284 might_sleep_if(extra_checks);
3285 VALIDATE_DESC(desc);
3286 value = gpiod_get_raw_value_commit(desc);
3290 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3295 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3298 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3299 * @array_size: number of elements in the descriptor array / value bitmap
3300 * @desc_array: array of GPIO descriptors whose values will be read
3301 * @array_info: information on applicability of fast bitmap processing path
3302 * @value_bitmap: bitmap to store the read values
3304 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3305 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3306 * else an error code.
3308 * This function is to be called from contexts that can sleep.
3310 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3311 struct gpio_desc **desc_array,
3312 struct gpio_array *array_info,
3313 unsigned long *value_bitmap)
3315 might_sleep_if(extra_checks);
3318 return gpiod_get_array_value_complex(true, true, array_size,
3319 desc_array, array_info,
3322 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3325 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3326 * @array_size: number of elements in the descriptor array / value bitmap
3327 * @desc_array: array of GPIO descriptors whose values will be read
3328 * @array_info: information on applicability of fast bitmap processing path
3329 * @value_bitmap: bitmap to store the read values
3331 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3332 * into account. Return 0 in case of success, else an error code.
3334 * This function is to be called from contexts that can sleep.
3336 int gpiod_get_array_value_cansleep(unsigned int array_size,
3337 struct gpio_desc **desc_array,
3338 struct gpio_array *array_info,
3339 unsigned long *value_bitmap)
3341 might_sleep_if(extra_checks);
3344 return gpiod_get_array_value_complex(false, true, array_size,
3345 desc_array, array_info,
3348 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3351 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3352 * @desc: gpio whose value will be assigned
3353 * @value: value to assign
3355 * Set the raw value of the GPIO, i.e. the value of its physical line without
3356 * regard for its ACTIVE_LOW status.
3358 * This function is to be called from contexts that can sleep.
3360 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3362 might_sleep_if(extra_checks);
3363 VALIDATE_DESC_VOID(desc);
3364 gpiod_set_raw_value_commit(desc, value);
3366 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3369 * gpiod_set_value_cansleep() - assign a gpio's value
3370 * @desc: gpio whose value will be assigned
3371 * @value: value to assign
3373 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3376 * This function is to be called from contexts that can sleep.
3378 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3380 might_sleep_if(extra_checks);
3381 VALIDATE_DESC_VOID(desc);
3382 gpiod_set_value_nocheck(desc, value);
3384 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3387 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3388 * @array_size: number of elements in the descriptor array / value bitmap
3389 * @desc_array: array of GPIO descriptors whose values will be assigned
3390 * @array_info: information on applicability of fast bitmap processing path
3391 * @value_bitmap: bitmap of values to assign
3393 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3394 * without regard for their ACTIVE_LOW status.
3396 * This function is to be called from contexts that can sleep.
3398 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3399 struct gpio_desc **desc_array,
3400 struct gpio_array *array_info,
3401 unsigned long *value_bitmap)
3403 might_sleep_if(extra_checks);
3406 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3407 array_info, value_bitmap);
3409 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3412 * gpiod_add_lookup_tables() - register GPIO device consumers
3413 * @tables: list of tables of consumers to register
3414 * @n: number of tables in the list
3416 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3420 mutex_lock(&gpio_lookup_lock);
3422 for (i = 0; i < n; i++)
3423 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3425 mutex_unlock(&gpio_lookup_lock);
3429 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3430 * @array_size: number of elements in the descriptor array / value bitmap
3431 * @desc_array: array of GPIO descriptors whose values will be assigned
3432 * @array_info: information on applicability of fast bitmap processing path
3433 * @value_bitmap: bitmap of values to assign
3435 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3438 * This function is to be called from contexts that can sleep.
3440 int gpiod_set_array_value_cansleep(unsigned int array_size,
3441 struct gpio_desc **desc_array,
3442 struct gpio_array *array_info,
3443 unsigned long *value_bitmap)
3445 might_sleep_if(extra_checks);
3448 return gpiod_set_array_value_complex(false, true, array_size,
3449 desc_array, array_info,
3452 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3455 * gpiod_add_lookup_table() - register GPIO device consumers
3456 * @table: table of consumers to register
3458 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3460 mutex_lock(&gpio_lookup_lock);
3462 list_add_tail(&table->list, &gpio_lookup_list);
3464 mutex_unlock(&gpio_lookup_lock);
3466 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3469 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3470 * @table: table of consumers to unregister
3472 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3474 /* Nothing to remove */
3478 mutex_lock(&gpio_lookup_lock);
3480 list_del(&table->list);
3482 mutex_unlock(&gpio_lookup_lock);
3484 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3487 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3488 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3490 void gpiod_add_hogs(struct gpiod_hog *hogs)
3492 struct gpio_chip *gc;
3493 struct gpiod_hog *hog;
3495 mutex_lock(&gpio_machine_hogs_mutex);
3497 for (hog = &hogs[0]; hog->chip_label; hog++) {
3498 list_add_tail(&hog->list, &gpio_machine_hogs);
3501 * The chip may have been registered earlier, so check if it
3502 * exists and, if so, try to hog the line now.
3504 gc = find_chip_by_name(hog->chip_label);
3506 gpiochip_machine_hog(gc, hog);
3509 mutex_unlock(&gpio_machine_hogs_mutex);
3511 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3513 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3515 const char *dev_id = dev ? dev_name(dev) : NULL;
3516 struct gpiod_lookup_table *table;
3518 mutex_lock(&gpio_lookup_lock);
3520 list_for_each_entry(table, &gpio_lookup_list, list) {
3521 if (table->dev_id && dev_id) {
3523 * Valid strings on both ends, must be identical to have
3526 if (!strcmp(table->dev_id, dev_id))
3530 * One of the pointers is NULL, so both must be to have
3533 if (dev_id == table->dev_id)
3540 mutex_unlock(&gpio_lookup_lock);
3544 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3545 unsigned int idx, unsigned long *flags)
3547 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3548 struct gpiod_lookup_table *table;
3549 struct gpiod_lookup *p;
3551 table = gpiod_find_lookup_table(dev);
3555 for (p = &table->table[0]; p->key; p++) {
3556 struct gpio_chip *gc;
3558 /* idx must always match exactly */
3562 /* If the lookup entry has a con_id, require exact match */
3563 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3566 if (p->chip_hwnum == U16_MAX) {
3567 desc = gpio_name_to_desc(p->key);
3573 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3575 return ERR_PTR(-EPROBE_DEFER);
3578 gc = find_chip_by_name(p->key);
3582 * As the lookup table indicates a chip with
3583 * p->key should exist, assume it may
3584 * still appear later and let the interested
3585 * consumer be probed again or let the Deferred
3586 * Probe infrastructure handle the error.
3588 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3590 return ERR_PTR(-EPROBE_DEFER);
3593 if (gc->ngpio <= p->chip_hwnum) {
3595 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3596 idx, p->chip_hwnum, gc->ngpio - 1,
3598 return ERR_PTR(-EINVAL);
3601 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3610 static int platform_gpio_count(struct device *dev, const char *con_id)
3612 struct gpiod_lookup_table *table;
3613 struct gpiod_lookup *p;
3614 unsigned int count = 0;
3616 table = gpiod_find_lookup_table(dev);
3620 for (p = &table->table[0]; p->key; p++) {
3621 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3622 (!con_id && !p->con_id))
3632 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3633 * @fwnode: handle of the firmware node
3634 * @con_id: function within the GPIO consumer
3635 * @index: index of the GPIO to obtain for the consumer
3636 * @flags: GPIO initialization flags
3637 * @label: label to attach to the requested GPIO
3639 * This function can be used for drivers that get their configuration
3640 * from opaque firmware.
3642 * The function properly finds the corresponding GPIO using whatever is the
3643 * underlying firmware interface and then makes sure that the GPIO
3644 * descriptor is requested before it is returned to the caller.
3647 * On successful request the GPIO pin is configured in accordance with
3650 * In case of error an ERR_PTR() is returned.
3652 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3653 const char *con_id, int index,
3654 enum gpiod_flags flags,
3657 struct gpio_desc *desc;
3658 char prop_name[32]; /* 32 is max size of property name */
3661 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3663 snprintf(prop_name, sizeof(prop_name), "%s-%s",
3664 con_id, gpio_suffixes[i]);
3666 snprintf(prop_name, sizeof(prop_name), "%s",
3669 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3671 if (!gpiod_not_found(desc))
3677 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
3680 * gpiod_count - return the number of GPIOs associated with a device / function
3681 * or -ENOENT if no GPIO has been assigned to the requested function
3682 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3683 * @con_id: function within the GPIO consumer
3685 int gpiod_count(struct device *dev, const char *con_id)
3687 int count = -ENOENT;
3689 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3690 count = of_gpio_get_count(dev, con_id);
3691 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3692 count = acpi_gpio_count(dev, con_id);
3695 count = platform_gpio_count(dev, con_id);
3699 EXPORT_SYMBOL_GPL(gpiod_count);
3702 * gpiod_get - obtain a GPIO for a given GPIO function
3703 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3704 * @con_id: function within the GPIO consumer
3705 * @flags: optional GPIO initialization flags
3707 * Return the GPIO descriptor corresponding to the function con_id of device
3708 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3709 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3711 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3712 enum gpiod_flags flags)
3714 return gpiod_get_index(dev, con_id, 0, flags);
3716 EXPORT_SYMBOL_GPL(gpiod_get);
3719 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3720 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3721 * @con_id: function within the GPIO consumer
3722 * @flags: optional GPIO initialization flags
3724 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3725 * the requested function it will return NULL. This is convenient for drivers
3726 * that need to handle optional GPIOs.
3728 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3730 enum gpiod_flags flags)
3732 return gpiod_get_index_optional(dev, con_id, 0, flags);
3734 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3738 * gpiod_configure_flags - helper function to configure a given GPIO
3739 * @desc: gpio whose value will be assigned
3740 * @con_id: function within the GPIO consumer
3741 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3742 * of_find_gpio() or of_get_gpio_hog()
3743 * @dflags: gpiod_flags - optional GPIO initialization flags
3745 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3746 * requested function and/or index, or another IS_ERR() code if an error
3747 * occurred while trying to acquire the GPIO.
3749 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3750 unsigned long lflags, enum gpiod_flags dflags)
3754 if (lflags & GPIO_ACTIVE_LOW)
3755 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3757 if (lflags & GPIO_OPEN_DRAIN)
3758 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3759 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3761 * This enforces open drain mode from the consumer side.
3762 * This is necessary for some busses like I2C, but the lookup
3763 * should *REALLY* have specified them as open drain in the
3764 * first place, so print a little warning here.
3766 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3768 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3771 if (lflags & GPIO_OPEN_SOURCE)
3772 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3774 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3776 "both pull-up and pull-down enabled, invalid configuration\n");
3780 if (lflags & GPIO_PULL_UP)
3781 set_bit(FLAG_PULL_UP, &desc->flags);
3782 else if (lflags & GPIO_PULL_DOWN)
3783 set_bit(FLAG_PULL_DOWN, &desc->flags);
3785 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3789 /* No particular flag request, return here... */
3790 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3791 gpiod_dbg(desc, "no flags found for %s\n", con_id);
3796 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3797 ret = gpiod_direction_output(desc,
3798 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3800 ret = gpiod_direction_input(desc);
3806 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3807 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3808 * @con_id: function within the GPIO consumer
3809 * @idx: index of the GPIO to obtain in the consumer
3810 * @flags: optional GPIO initialization flags
3812 * This variant of gpiod_get() allows to access GPIOs other than the first
3813 * defined one for functions that define several GPIOs.
3815 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3816 * requested function and/or index, or another IS_ERR() code if an error
3817 * occurred while trying to acquire the GPIO.
3819 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3822 enum gpiod_flags flags)
3824 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3825 struct gpio_desc *desc = NULL;
3827 /* Maybe we have a device name, maybe not */
3828 const char *devname = dev ? dev_name(dev) : "?";
3830 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3833 /* Using device tree? */
3834 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3835 dev_dbg(dev, "using device tree for GPIO lookup\n");
3836 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3837 } else if (ACPI_COMPANION(dev)) {
3838 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3839 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3844 * Either we are not using DT or ACPI, or their lookup did not return
3845 * a result. In that case, use platform lookup as a fallback.
3847 if (!desc || gpiod_not_found(desc)) {
3848 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3849 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3853 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3858 * If a connection label was passed use that, else attempt to use
3859 * the device name as label
3861 ret = gpiod_request(desc, con_id ? con_id : devname);
3863 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
3865 * This happens when there are several consumers for
3866 * the same GPIO line: we just return here without
3867 * further initialization. It is a bit if a hack.
3868 * This is necessary to support fixed regulators.
3870 * FIXME: Make this more sane and safe.
3872 dev_info(dev, "nonexclusive access to GPIO for %s\n",
3873 con_id ? con_id : devname);
3876 return ERR_PTR(ret);
3880 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3882 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3884 return ERR_PTR(ret);
3887 blocking_notifier_call_chain(&desc->gdev->notifier,
3888 GPIOLINE_CHANGED_REQUESTED, desc);
3892 EXPORT_SYMBOL_GPL(gpiod_get_index);
3895 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3896 * @fwnode: handle of the firmware node
3897 * @propname: name of the firmware property representing the GPIO
3898 * @index: index of the GPIO to obtain for the consumer
3899 * @dflags: GPIO initialization flags
3900 * @label: label to attach to the requested GPIO
3902 * This function can be used for drivers that get their configuration
3903 * from opaque firmware.
3905 * The function properly finds the corresponding GPIO using whatever is the
3906 * underlying firmware interface and then makes sure that the GPIO
3907 * descriptor is requested before it is returned to the caller.
3910 * On successful request the GPIO pin is configured in accordance with
3913 * In case of error an ERR_PTR() is returned.
3915 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3916 const char *propname, int index,
3917 enum gpiod_flags dflags,
3920 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3921 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3925 return ERR_PTR(-EINVAL);
3927 if (is_of_node(fwnode)) {
3928 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3933 } else if (is_acpi_node(fwnode)) {
3934 struct acpi_gpio_info info;
3936 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3940 acpi_gpio_update_gpiod_flags(&dflags, &info);
3941 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
3944 /* Currently only ACPI takes this path */
3945 ret = gpiod_request(desc, label);
3947 return ERR_PTR(ret);
3949 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3952 return ERR_PTR(ret);
3955 blocking_notifier_call_chain(&desc->gdev->notifier,
3956 GPIOLINE_CHANGED_REQUESTED, desc);
3960 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3963 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3965 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3966 * @con_id: function within the GPIO consumer
3967 * @index: index of the GPIO to obtain in the consumer
3968 * @flags: optional GPIO initialization flags
3970 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3971 * specified index was assigned to the requested function it will return NULL.
3972 * This is convenient for drivers that need to handle optional GPIOs.
3974 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
3977 enum gpiod_flags flags)
3979 struct gpio_desc *desc;
3981 desc = gpiod_get_index(dev, con_id, index, flags);
3982 if (gpiod_not_found(desc))
3987 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3990 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3991 * @desc: gpio whose value will be assigned
3992 * @name: gpio line name
3993 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3994 * of_find_gpio() or of_get_gpio_hog()
3995 * @dflags: gpiod_flags - optional GPIO initialization flags
3997 int gpiod_hog(struct gpio_desc *desc, const char *name,
3998 unsigned long lflags, enum gpiod_flags dflags)
4000 struct gpio_chip *gc;
4001 struct gpio_desc *local_desc;
4005 gc = gpiod_to_chip(desc);
4006 hwnum = gpio_chip_hwgpio(desc);
4008 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4010 if (IS_ERR(local_desc)) {
4011 ret = PTR_ERR(local_desc);
4012 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4013 name, gc->label, hwnum, ret);
4017 /* Mark GPIO as hogged so it can be identified and removed later */
4018 set_bit(FLAG_IS_HOGGED, &desc->flags);
4020 gpiod_info(desc, "hogged as %s%s\n",
4021 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4022 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4023 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4029 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4030 * @gc: gpio chip to act on
4032 static void gpiochip_free_hogs(struct gpio_chip *gc)
4036 for (id = 0; id < gc->ngpio; id++) {
4037 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4038 gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
4043 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4044 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4045 * @con_id: function within the GPIO consumer
4046 * @flags: optional GPIO initialization flags
4048 * This function acquires all the GPIOs defined under a given function.
4050 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4051 * no GPIO has been assigned to the requested function, or another IS_ERR()
4052 * code if an error occurred while trying to acquire the GPIOs.
4054 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4056 enum gpiod_flags flags)
4058 struct gpio_desc *desc;
4059 struct gpio_descs *descs;
4060 struct gpio_array *array_info = NULL;
4061 struct gpio_chip *gc;
4062 int count, bitmap_size;
4064 count = gpiod_count(dev, con_id);
4066 return ERR_PTR(count);
4068 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4070 return ERR_PTR(-ENOMEM);
4072 for (descs->ndescs = 0; descs->ndescs < count; ) {
4073 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4075 gpiod_put_array(descs);
4076 return ERR_CAST(desc);
4079 descs->desc[descs->ndescs] = desc;
4081 gc = gpiod_to_chip(desc);
4083 * If pin hardware number of array member 0 is also 0, select
4084 * its chip as a candidate for fast bitmap processing path.
4086 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4087 struct gpio_descs *array;
4089 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4092 array = kzalloc(struct_size(descs, desc, count) +
4093 struct_size(array_info, invert_mask,
4094 3 * bitmap_size), GFP_KERNEL);
4096 gpiod_put_array(descs);
4097 return ERR_PTR(-ENOMEM);
4100 memcpy(array, descs,
4101 struct_size(descs, desc, descs->ndescs + 1));
4105 array_info = (void *)(descs->desc + count);
4106 array_info->get_mask = array_info->invert_mask +
4108 array_info->set_mask = array_info->get_mask +
4111 array_info->desc = descs->desc;
4112 array_info->size = count;
4113 array_info->chip = gc;
4114 bitmap_set(array_info->get_mask, descs->ndescs,
4115 count - descs->ndescs);
4116 bitmap_set(array_info->set_mask, descs->ndescs,
4117 count - descs->ndescs);
4118 descs->info = array_info;
4120 /* Unmark array members which don't belong to the 'fast' chip */
4121 if (array_info && array_info->chip != gc) {
4122 __clear_bit(descs->ndescs, array_info->get_mask);
4123 __clear_bit(descs->ndescs, array_info->set_mask);
4126 * Detect array members which belong to the 'fast' chip
4127 * but their pins are not in hardware order.
4129 else if (array_info &&
4130 gpio_chip_hwgpio(desc) != descs->ndescs) {
4132 * Don't use fast path if all array members processed so
4133 * far belong to the same chip as this one but its pin
4134 * hardware number is different from its array index.
4136 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4139 __clear_bit(descs->ndescs,
4140 array_info->get_mask);
4141 __clear_bit(descs->ndescs,
4142 array_info->set_mask);
4144 } else if (array_info) {
4145 /* Exclude open drain or open source from fast output */
4146 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4147 gpiochip_line_is_open_source(gc, descs->ndescs))
4148 __clear_bit(descs->ndescs,
4149 array_info->set_mask);
4150 /* Identify 'fast' pins which require invertion */
4151 if (gpiod_is_active_low(desc))
4152 __set_bit(descs->ndescs,
4153 array_info->invert_mask);
4160 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4161 array_info->chip->label, array_info->size,
4162 *array_info->get_mask, *array_info->set_mask,
4163 *array_info->invert_mask);
4166 EXPORT_SYMBOL_GPL(gpiod_get_array);
4169 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4171 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4172 * @con_id: function within the GPIO consumer
4173 * @flags: optional GPIO initialization flags
4175 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4176 * assigned to the requested function it will return NULL.
4178 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4180 enum gpiod_flags flags)
4182 struct gpio_descs *descs;
4184 descs = gpiod_get_array(dev, con_id, flags);
4185 if (gpiod_not_found(descs))
4190 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4193 * gpiod_put - dispose of a GPIO descriptor
4194 * @desc: GPIO descriptor to dispose of
4196 * No descriptor can be used after gpiod_put() has been called on it.
4198 void gpiod_put(struct gpio_desc *desc)
4203 EXPORT_SYMBOL_GPL(gpiod_put);
4206 * gpiod_put_array - dispose of multiple GPIO descriptors
4207 * @descs: struct gpio_descs containing an array of descriptors
4209 void gpiod_put_array(struct gpio_descs *descs)
4213 for (i = 0; i < descs->ndescs; i++)
4214 gpiod_put(descs->desc[i]);
4218 EXPORT_SYMBOL_GPL(gpiod_put_array);
4221 static int gpio_bus_match(struct device *dev, struct device_driver *drv)
4224 * Only match if the fwnode doesn't already have a proper struct device
4227 if (dev->fwnode && dev->fwnode->dev != dev)
4232 static int gpio_stub_drv_probe(struct device *dev)
4235 * The DT node of some GPIO chips have a "compatible" property, but
4236 * never have a struct device added and probed by a driver to register
4237 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4238 * the consumers of the GPIO chip to get probe deferred forever because
4239 * they will be waiting for a device associated with the GPIO chip
4240 * firmware node to get added and bound to a driver.
4242 * To allow these consumers to probe, we associate the struct
4243 * gpio_device of the GPIO chip with the firmware node and then simply
4244 * bind it to this stub driver.
4249 static struct device_driver gpio_stub_drv = {
4250 .name = "gpio_stub_drv",
4251 .bus = &gpio_bus_type,
4252 .probe = gpio_stub_drv_probe,
4255 static int __init gpiolib_dev_init(void)
4259 /* Register GPIO sysfs bus */
4260 ret = bus_register(&gpio_bus_type);
4262 pr_err("gpiolib: could not register GPIO bus type\n");
4266 ret = driver_register(&gpio_stub_drv);
4268 pr_err("gpiolib: could not register GPIO stub driver\n");
4269 bus_unregister(&gpio_bus_type);
4273 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4275 pr_err("gpiolib: failed to allocate char dev region\n");
4276 driver_unregister(&gpio_stub_drv);
4277 bus_unregister(&gpio_bus_type);
4281 gpiolib_initialized = true;
4282 gpiochip_setup_devs();
4284 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4285 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4286 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4290 core_initcall(gpiolib_dev_init);
4292 #ifdef CONFIG_DEBUG_FS
4294 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4297 struct gpio_chip *gc = gdev->chip;
4298 unsigned gpio = gdev->base;
4299 struct gpio_desc *gdesc = &gdev->descs[0];
4304 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4305 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4307 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4313 gpiod_get_direction(gdesc);
4314 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4315 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4316 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4317 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4318 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4319 is_out ? "out" : "in ",
4320 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
4321 is_irq ? "IRQ " : "",
4322 active_low ? "ACTIVE LOW" : "");
4323 seq_printf(s, "\n");
4327 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4329 unsigned long flags;
4330 struct gpio_device *gdev = NULL;
4331 loff_t index = *pos;
4335 spin_lock_irqsave(&gpio_lock, flags);
4336 list_for_each_entry(gdev, &gpio_devices, list)
4338 spin_unlock_irqrestore(&gpio_lock, flags);
4341 spin_unlock_irqrestore(&gpio_lock, flags);
4346 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4348 unsigned long flags;
4349 struct gpio_device *gdev = v;
4352 spin_lock_irqsave(&gpio_lock, flags);
4353 if (list_is_last(&gdev->list, &gpio_devices))
4356 ret = list_entry(gdev->list.next, struct gpio_device, list);
4357 spin_unlock_irqrestore(&gpio_lock, flags);
4365 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4369 static int gpiolib_seq_show(struct seq_file *s, void *v)
4371 struct gpio_device *gdev = v;
4372 struct gpio_chip *gc = gdev->chip;
4373 struct device *parent;
4376 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4377 dev_name(&gdev->dev));
4381 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4382 dev_name(&gdev->dev),
4383 gdev->base, gdev->base + gdev->ngpio - 1);
4384 parent = gc->parent;
4386 seq_printf(s, ", parent: %s/%s",
4387 parent->bus ? parent->bus->name : "no-bus",
4390 seq_printf(s, ", %s", gc->label);
4392 seq_printf(s, ", can sleep");
4393 seq_printf(s, ":\n");
4396 gc->dbg_show(s, gc);
4398 gpiolib_dbg_show(s, gdev);
4403 static const struct seq_operations gpiolib_sops = {
4404 .start = gpiolib_seq_start,
4405 .next = gpiolib_seq_next,
4406 .stop = gpiolib_seq_stop,
4407 .show = gpiolib_seq_show,
4409 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4411 static int __init gpiolib_debugfs_init(void)
4413 /* /sys/kernel/debug/gpio */
4414 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4417 subsys_initcall(gpiolib_debugfs_init);
4419 #endif /* DEBUG_FS */