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 struct bus_type gpio_bus_type = {
64 * Number of GPIOs to use for the fast path in set array
66 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
68 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
69 * While any GPIO is requested, its gpio_chip is not removable;
70 * each GPIO's "requested" flag serves as a lock and refcount.
72 DEFINE_SPINLOCK(gpio_lock);
74 static DEFINE_MUTEX(gpio_lookup_lock);
75 static LIST_HEAD(gpio_lookup_list);
76 LIST_HEAD(gpio_devices);
78 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
79 static LIST_HEAD(gpio_machine_hogs);
81 static void gpiochip_free_hogs(struct gpio_chip *gc);
82 static int gpiochip_add_irqchip(struct gpio_chip *gc,
83 struct lock_class_key *lock_key,
84 struct lock_class_key *request_key);
85 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
86 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
87 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
88 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
90 static bool gpiolib_initialized;
92 static inline void desc_set_label(struct gpio_desc *d, const char *label)
98 * gpio_to_desc - Convert a GPIO number to its descriptor
99 * @gpio: global GPIO number
102 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
103 * with the given number exists in the system.
105 struct gpio_desc *gpio_to_desc(unsigned gpio)
107 struct gpio_device *gdev;
110 spin_lock_irqsave(&gpio_lock, flags);
112 list_for_each_entry(gdev, &gpio_devices, list) {
113 if (gdev->base <= gpio &&
114 gdev->base + gdev->ngpio > gpio) {
115 spin_unlock_irqrestore(&gpio_lock, flags);
116 return &gdev->descs[gpio - gdev->base];
120 spin_unlock_irqrestore(&gpio_lock, flags);
122 if (!gpio_is_valid(gpio))
123 pr_warn("invalid GPIO %d\n", gpio);
127 EXPORT_SYMBOL_GPL(gpio_to_desc);
130 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
131 * hardware number for this chip
133 * @hwnum: hardware number of the GPIO for this chip
136 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
137 * in the given chip for the specified hardware number.
139 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
142 struct gpio_device *gdev = gc->gpiodev;
144 if (hwnum >= gdev->ngpio)
145 return ERR_PTR(-EINVAL);
147 return &gdev->descs[hwnum];
149 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
152 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
153 * @desc: GPIO descriptor
155 * This should disappear in the future but is needed since we still
156 * use GPIO numbers for error messages and sysfs nodes.
159 * The global GPIO number for the GPIO specified by its descriptor.
161 int desc_to_gpio(const struct gpio_desc *desc)
163 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
165 EXPORT_SYMBOL_GPL(desc_to_gpio);
169 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
170 * @desc: descriptor to return the chip of
172 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
174 if (!desc || !desc->gdev)
176 return desc->gdev->chip;
178 EXPORT_SYMBOL_GPL(gpiod_to_chip);
180 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
181 static int gpiochip_find_base(int ngpio)
183 struct gpio_device *gdev;
184 int base = ARCH_NR_GPIOS - ngpio;
186 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
187 /* found a free space? */
188 if (gdev->base + gdev->ngpio <= base)
191 /* nope, check the space right before the chip */
192 base = gdev->base - ngpio;
195 if (gpio_is_valid(base)) {
196 pr_debug("%s: found new base at %d\n", __func__, base);
199 pr_err("%s: cannot find free range\n", __func__);
205 * gpiod_get_direction - return the current direction of a GPIO
206 * @desc: GPIO to get the direction of
208 * Returns 0 for output, 1 for input, or an error code in case of error.
210 * This function may sleep if gpiod_cansleep() is true.
212 int gpiod_get_direction(struct gpio_desc *desc)
214 struct gpio_chip *gc;
218 gc = gpiod_to_chip(desc);
219 offset = gpio_chip_hwgpio(desc);
222 * Open drain emulation using input mode may incorrectly report
223 * input here, fix that up.
225 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
226 test_bit(FLAG_IS_OUT, &desc->flags))
229 if (!gc->get_direction)
232 ret = gc->get_direction(gc, offset);
236 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
240 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
244 EXPORT_SYMBOL_GPL(gpiod_get_direction);
247 * Add a new chip to the global chips list, keeping the list of chips sorted
248 * by range(means [base, base + ngpio - 1]) order.
250 * Return -EBUSY if the new chip overlaps with some other chip's integer
253 static int gpiodev_add_to_list(struct gpio_device *gdev)
255 struct gpio_device *prev, *next;
257 if (list_empty(&gpio_devices)) {
258 /* initial entry in list */
259 list_add_tail(&gdev->list, &gpio_devices);
263 next = list_entry(gpio_devices.next, struct gpio_device, list);
264 if (gdev->base + gdev->ngpio <= next->base) {
265 /* add before first entry */
266 list_add(&gdev->list, &gpio_devices);
270 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
271 if (prev->base + prev->ngpio <= gdev->base) {
272 /* add behind last entry */
273 list_add_tail(&gdev->list, &gpio_devices);
277 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
278 /* at the end of the list */
279 if (&next->list == &gpio_devices)
282 /* add between prev and next */
283 if (prev->base + prev->ngpio <= gdev->base
284 && gdev->base + gdev->ngpio <= next->base) {
285 list_add(&gdev->list, &prev->list);
290 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
295 * Convert a GPIO name to its descriptor
296 * Note that there is no guarantee that GPIO names are globally unique!
297 * Hence this function will return, if it exists, a reference to the first GPIO
298 * line found that matches the given name.
300 static struct gpio_desc *gpio_name_to_desc(const char * const name)
302 struct gpio_device *gdev;
308 spin_lock_irqsave(&gpio_lock, flags);
310 list_for_each_entry(gdev, &gpio_devices, list) {
313 for (i = 0; i != gdev->ngpio; ++i) {
314 struct gpio_desc *desc = &gdev->descs[i];
319 if (!strcmp(desc->name, name)) {
320 spin_unlock_irqrestore(&gpio_lock, flags);
326 spin_unlock_irqrestore(&gpio_lock, flags);
332 * Take the names from gc->names and assign them to their GPIO descriptors.
333 * Warn if a name is already used for a GPIO line on a different GPIO chip.
336 * 1. Non-unique names are still accepted,
337 * 2. Name collisions within the same GPIO chip are not reported.
339 static int gpiochip_set_desc_names(struct gpio_chip *gc)
341 struct gpio_device *gdev = gc->gpiodev;
344 /* First check all names if they are unique */
345 for (i = 0; i != gc->ngpio; ++i) {
346 struct gpio_desc *gpio;
348 gpio = gpio_name_to_desc(gc->names[i]);
351 "Detected name collision for GPIO name '%s'\n",
355 /* Then add all names to the GPIO descriptors */
356 for (i = 0; i != gc->ngpio; ++i)
357 gdev->descs[i].name = gc->names[i];
363 * devprop_gpiochip_set_names - Set GPIO line names using device properties
364 * @chip: GPIO chip whose lines should be named, if possible
366 * Looks for device property "gpio-line-names" and if it exists assigns
367 * GPIO line names for the chip. The memory allocated for the assigned
368 * names belong to the underlying software node and should not be released
371 static int devprop_gpiochip_set_names(struct gpio_chip *chip)
373 struct gpio_device *gdev = chip->gpiodev;
374 struct device *dev = chip->parent;
379 /* GPIO chip may not have a parent device whose properties we inspect. */
383 count = device_property_string_array_count(dev, "gpio-line-names");
387 if (count > gdev->ngpio) {
388 dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
393 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
397 ret = device_property_read_string_array(dev, "gpio-line-names",
400 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
405 for (i = 0; i < count; i++)
406 gdev->descs[i].name = names[i];
413 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
417 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
421 /* Assume by default all GPIOs are valid */
422 bitmap_fill(p, gc->ngpio);
427 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
429 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
432 gc->valid_mask = gpiochip_allocate_mask(gc);
439 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
441 if (gc->init_valid_mask)
442 return gc->init_valid_mask(gc,
449 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
451 bitmap_free(gc->valid_mask);
452 gc->valid_mask = NULL;
455 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
457 if (gc->add_pin_ranges)
458 return gc->add_pin_ranges(gc);
463 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
466 /* No mask means all valid */
467 if (likely(!gc->valid_mask))
469 return test_bit(offset, gc->valid_mask);
471 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
473 static void gpiodevice_release(struct device *dev)
475 struct gpio_device *gdev = dev_get_drvdata(dev);
477 list_del(&gdev->list);
478 ida_free(&gpio_ida, gdev->id);
479 kfree_const(gdev->label);
484 #ifdef CONFIG_GPIO_CDEV
485 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
486 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
489 * gpiolib_cdev_register() indirectly calls device_add(), which is still
490 * required even when cdev is not selected.
492 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
493 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
496 static int gpiochip_setup_dev(struct gpio_device *gdev)
500 ret = gcdev_register(gdev, gpio_devt);
504 ret = gpiochip_sysfs_register(gdev);
506 goto err_remove_device;
508 /* From this point, the .release() function cleans up gpio_device */
509 gdev->dev.release = gpiodevice_release;
510 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
511 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
516 gcdev_unregister(gdev);
520 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
522 struct gpio_desc *desc;
525 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
527 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
532 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
535 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
537 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
538 __func__, gc->label, hog->chip_hwnum, rv);
541 static void machine_gpiochip_add(struct gpio_chip *gc)
543 struct gpiod_hog *hog;
545 mutex_lock(&gpio_machine_hogs_mutex);
547 list_for_each_entry(hog, &gpio_machine_hogs, list) {
548 if (!strcmp(gc->label, hog->chip_label))
549 gpiochip_machine_hog(gc, hog);
552 mutex_unlock(&gpio_machine_hogs_mutex);
555 static void gpiochip_setup_devs(void)
557 struct gpio_device *gdev;
560 list_for_each_entry(gdev, &gpio_devices, list) {
561 ret = gpiochip_setup_dev(gdev);
564 "Failed to initialize gpio device (%d)\n", ret);
568 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
569 struct lock_class_key *lock_key,
570 struct lock_class_key *request_key)
576 struct gpio_device *gdev;
579 * First: allocate and populate the internal stat container, and
580 * set up the struct device.
582 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
585 gdev->dev.bus = &gpio_bus_type;
589 gdev->dev.parent = gc->parent;
590 gdev->dev.of_node = gc->parent->of_node;
593 #ifdef CONFIG_OF_GPIO
594 /* If the gpiochip has an assigned OF node this takes precedence */
596 gdev->dev.of_node = gc->of_node;
598 gc->of_node = gdev->dev.of_node;
601 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
606 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
607 device_initialize(&gdev->dev);
608 dev_set_drvdata(&gdev->dev, gdev);
609 if (gc->parent && gc->parent->driver)
610 gdev->owner = gc->parent->driver->owner;
612 /* TODO: remove chip->owner */
613 gdev->owner = gc->owner;
615 gdev->owner = THIS_MODULE;
617 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
623 if (gc->ngpio == 0) {
624 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
629 if (gc->ngpio > FASTPATH_NGPIO)
630 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
631 gc->ngpio, FASTPATH_NGPIO);
633 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
639 gdev->ngpio = gc->ngpio;
642 spin_lock_irqsave(&gpio_lock, flags);
645 * TODO: this allocates a Linux GPIO number base in the global
646 * GPIO numberspace for this chip. In the long run we want to
647 * get *rid* of this numberspace and use only descriptors, but
648 * it may be a pipe dream. It will not happen before we get rid
649 * of the sysfs interface anyways.
652 base = gpiochip_find_base(gc->ngpio);
655 spin_unlock_irqrestore(&gpio_lock, flags);
659 * TODO: it should not be necessary to reflect the assigned
660 * base outside of the GPIO subsystem. Go over drivers and
661 * see if anyone makes use of this, else drop this and assign
668 ret = gpiodev_add_to_list(gdev);
670 spin_unlock_irqrestore(&gpio_lock, flags);
674 for (i = 0; i < gc->ngpio; i++)
675 gdev->descs[i].gdev = gdev;
677 spin_unlock_irqrestore(&gpio_lock, flags);
679 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
681 #ifdef CONFIG_PINCTRL
682 INIT_LIST_HEAD(&gdev->pin_ranges);
686 ret = gpiochip_set_desc_names(gc);
688 ret = devprop_gpiochip_set_names(gc);
690 goto err_remove_from_list;
692 ret = gpiochip_alloc_valid_mask(gc);
694 goto err_remove_from_list;
696 ret = of_gpiochip_add(gc);
698 goto err_free_gpiochip_mask;
700 ret = gpiochip_init_valid_mask(gc);
702 goto err_remove_of_chip;
704 for (i = 0; i < gc->ngpio; i++) {
705 struct gpio_desc *desc = &gdev->descs[i];
707 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
708 assign_bit(FLAG_IS_OUT,
709 &desc->flags, !gc->get_direction(gc, i));
711 assign_bit(FLAG_IS_OUT,
712 &desc->flags, !gc->direction_input);
716 ret = gpiochip_add_pin_ranges(gc);
718 goto err_remove_of_chip;
720 acpi_gpiochip_add(gc);
722 machine_gpiochip_add(gc);
724 ret = gpiochip_irqchip_init_valid_mask(gc);
726 goto err_remove_acpi_chip;
728 ret = gpiochip_irqchip_init_hw(gc);
730 goto err_remove_acpi_chip;
732 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
734 goto err_remove_irqchip_mask;
737 * By first adding the chardev, and then adding the device,
738 * we get a device node entry in sysfs under
739 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
740 * coldplug of device nodes and other udev business.
741 * We can do this only if gpiolib has been initialized.
742 * Otherwise, defer until later.
744 if (gpiolib_initialized) {
745 ret = gpiochip_setup_dev(gdev);
747 goto err_remove_irqchip;
752 gpiochip_irqchip_remove(gc);
753 err_remove_irqchip_mask:
754 gpiochip_irqchip_free_valid_mask(gc);
755 err_remove_acpi_chip:
756 acpi_gpiochip_remove(gc);
758 gpiochip_free_hogs(gc);
759 of_gpiochip_remove(gc);
760 err_free_gpiochip_mask:
761 gpiochip_remove_pin_ranges(gc);
762 gpiochip_free_valid_mask(gc);
763 err_remove_from_list:
764 spin_lock_irqsave(&gpio_lock, flags);
765 list_del(&gdev->list);
766 spin_unlock_irqrestore(&gpio_lock, flags);
768 kfree_const(gdev->label);
772 ida_free(&gpio_ida, gdev->id);
774 /* failures here can mean systems won't boot... */
775 if (ret != -EPROBE_DEFER) {
776 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
777 gdev->base, gdev->base + gdev->ngpio - 1,
778 gc->label ? : "generic", ret);
783 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
786 * gpiochip_get_data() - get per-subdriver data for the chip
790 * The per-subdriver data for the chip.
792 void *gpiochip_get_data(struct gpio_chip *gc)
794 return gc->gpiodev->data;
796 EXPORT_SYMBOL_GPL(gpiochip_get_data);
799 * gpiochip_remove() - unregister a gpio_chip
800 * @gc: the chip to unregister
802 * A gpio_chip with any GPIOs still requested may not be removed.
804 void gpiochip_remove(struct gpio_chip *gc)
806 struct gpio_device *gdev = gc->gpiodev;
810 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
811 gpiochip_sysfs_unregister(gdev);
812 gpiochip_free_hogs(gc);
813 /* Numb the device, cancelling all outstanding operations */
815 gpiochip_irqchip_remove(gc);
816 acpi_gpiochip_remove(gc);
817 of_gpiochip_remove(gc);
818 gpiochip_remove_pin_ranges(gc);
819 gpiochip_free_valid_mask(gc);
821 * We accept no more calls into the driver from this point, so
822 * NULL the driver data pointer
826 spin_lock_irqsave(&gpio_lock, flags);
827 for (i = 0; i < gdev->ngpio; i++) {
828 if (gpiochip_is_requested(gc, i))
831 spin_unlock_irqrestore(&gpio_lock, flags);
833 if (i != gdev->ngpio)
835 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
838 * The gpiochip side puts its use of the device to rest here:
839 * if there are no userspace clients, the chardev and device will
840 * be removed, else it will be dangling until the last user is
843 gcdev_unregister(gdev);
844 put_device(&gdev->dev);
846 EXPORT_SYMBOL_GPL(gpiochip_remove);
849 * gpiochip_find() - iterator for locating a specific gpio_chip
850 * @data: data to pass to match function
851 * @match: Callback function to check gpio_chip
853 * Similar to bus_find_device. It returns a reference to a gpio_chip as
854 * determined by a user supplied @match callback. The callback should return
855 * 0 if the device doesn't match and non-zero if it does. If the callback is
856 * non-zero, this function will return to the caller and not iterate over any
859 struct gpio_chip *gpiochip_find(void *data,
860 int (*match)(struct gpio_chip *gc,
863 struct gpio_device *gdev;
864 struct gpio_chip *gc = NULL;
867 spin_lock_irqsave(&gpio_lock, flags);
868 list_for_each_entry(gdev, &gpio_devices, list)
869 if (gdev->chip && match(gdev->chip, data)) {
874 spin_unlock_irqrestore(&gpio_lock, flags);
878 EXPORT_SYMBOL_GPL(gpiochip_find);
880 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
882 const char *name = data;
884 return !strcmp(gc->label, name);
887 static struct gpio_chip *find_chip_by_name(const char *name)
889 return gpiochip_find((void *)name, gpiochip_match_name);
892 #ifdef CONFIG_GPIOLIB_IRQCHIP
895 * The following is irqchip helper code for gpiochips.
898 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
900 struct gpio_irq_chip *girq = &gc->irq;
905 return girq->init_hw(gc);
908 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
910 struct gpio_irq_chip *girq = &gc->irq;
912 if (!girq->init_valid_mask)
915 girq->valid_mask = gpiochip_allocate_mask(gc);
916 if (!girq->valid_mask)
919 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
924 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
926 bitmap_free(gc->irq.valid_mask);
927 gc->irq.valid_mask = NULL;
930 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
933 if (!gpiochip_line_is_valid(gc, offset))
935 /* No mask means all valid */
936 if (likely(!gc->irq.valid_mask))
938 return test_bit(offset, gc->irq.valid_mask);
940 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
942 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
945 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
947 * @gc: the gpiochip to set the irqchip hierarchical handler to
948 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
949 * will then percolate up to the parent
951 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
952 struct irq_chip *irqchip)
954 /* DT will deal with mapping each IRQ as we go along */
955 if (is_of_node(gc->irq.fwnode))
959 * This is for legacy and boardfile "irqchip" fwnodes: allocate
960 * irqs upfront instead of dynamically since we don't have the
961 * dynamic type of allocation that hardware description languages
962 * provide. Once all GPIO drivers using board files are gone from
963 * the kernel we can delete this code, but for a transitional period
964 * it is necessary to keep this around.
966 if (is_fwnode_irqchip(gc->irq.fwnode)) {
970 for (i = 0; i < gc->ngpio; i++) {
971 struct irq_fwspec fwspec;
972 unsigned int parent_hwirq;
973 unsigned int parent_type;
974 struct gpio_irq_chip *girq = &gc->irq;
977 * We call the child to parent translation function
978 * only to check if the child IRQ is valid or not.
979 * Just pick the rising edge type here as that is what
980 * we likely need to support.
982 ret = girq->child_to_parent_hwirq(gc, i,
983 IRQ_TYPE_EDGE_RISING,
987 chip_err(gc, "skip set-up on hwirq %d\n",
992 fwspec.fwnode = gc->irq.fwnode;
993 /* This is the hwirq for the GPIO line side of things */
994 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
995 /* Just pick something */
996 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
997 fwspec.param_count = 2;
998 ret = __irq_domain_alloc_irqs(gc->irq.domain,
999 /* just pick something */
1008 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1015 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1020 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1021 struct irq_fwspec *fwspec,
1022 unsigned long *hwirq,
1025 /* We support standard DT translation */
1026 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1027 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1030 /* This is for board files and others not using DT */
1031 if (is_fwnode_irqchip(fwspec->fwnode)) {
1034 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1037 WARN_ON(*type == IRQ_TYPE_NONE);
1043 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1045 unsigned int nr_irqs,
1048 struct gpio_chip *gc = d->host_data;
1049 irq_hw_number_t hwirq;
1050 unsigned int type = IRQ_TYPE_NONE;
1051 struct irq_fwspec *fwspec = data;
1053 unsigned int parent_hwirq;
1054 unsigned int parent_type;
1055 struct gpio_irq_chip *girq = &gc->irq;
1059 * The nr_irqs parameter is always one except for PCI multi-MSI
1060 * so this should not happen.
1062 WARN_ON(nr_irqs != 1);
1064 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1068 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1070 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1071 &parent_hwirq, &parent_type);
1073 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1076 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1079 * We set handle_bad_irq because the .set_type() should
1080 * always be invoked and set the right type of handler.
1082 irq_domain_set_info(d,
1091 /* This parent only handles asserted level IRQs */
1092 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1096 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1098 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1099 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1101 * If the parent irqdomain is msi, the interrupts have already
1102 * been allocated, so the EEXIST is good.
1104 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1108 "failed to allocate parent hwirq %d for hwirq %lu\n",
1109 parent_hwirq, hwirq);
1115 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1116 unsigned int offset)
1121 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1123 ops->activate = gpiochip_irq_domain_activate;
1124 ops->deactivate = gpiochip_irq_domain_deactivate;
1125 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1126 ops->free = irq_domain_free_irqs_common;
1129 * We only allow overriding the translate() function for
1130 * hierarchical chips, and this should only be done if the user
1131 * really need something other than 1:1 translation.
1133 if (!ops->translate)
1134 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1137 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1139 if (!gc->irq.child_to_parent_hwirq ||
1141 chip_err(gc, "missing irqdomain vital data\n");
1145 if (!gc->irq.child_offset_to_irq)
1146 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1148 if (!gc->irq.populate_parent_alloc_arg)
1149 gc->irq.populate_parent_alloc_arg =
1150 gpiochip_populate_parent_fwspec_twocell;
1152 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1154 gc->irq.domain = irq_domain_create_hierarchy(
1155 gc->irq.parent_domain,
1159 &gc->irq.child_irq_domain_ops,
1162 if (!gc->irq.domain)
1165 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1170 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1172 return !!gc->irq.parent_domain;
1175 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1176 unsigned int parent_hwirq,
1177 unsigned int parent_type)
1179 struct irq_fwspec *fwspec;
1181 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1185 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1186 fwspec->param_count = 2;
1187 fwspec->param[0] = parent_hwirq;
1188 fwspec->param[1] = parent_type;
1192 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1194 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1195 unsigned int parent_hwirq,
1196 unsigned int parent_type)
1198 struct irq_fwspec *fwspec;
1200 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1204 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1205 fwspec->param_count = 4;
1206 fwspec->param[0] = 0;
1207 fwspec->param[1] = parent_hwirq;
1208 fwspec->param[2] = 0;
1209 fwspec->param[3] = parent_type;
1213 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1217 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1222 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1227 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1230 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1231 * @d: the irqdomain used by this irqchip
1232 * @irq: the global irq number used by this GPIO irqchip irq
1233 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1235 * This function will set up the mapping for a certain IRQ line on a
1236 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1237 * stored inside the gpiochip.
1239 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1240 irq_hw_number_t hwirq)
1242 struct gpio_chip *gc = d->host_data;
1245 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1248 irq_set_chip_data(irq, gc);
1250 * This lock class tells lockdep that GPIO irqs are in a different
1251 * category than their parents, so it won't report false recursion.
1253 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1254 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1255 /* Chips that use nested thread handlers have them marked */
1256 if (gc->irq.threaded)
1257 irq_set_nested_thread(irq, 1);
1258 irq_set_noprobe(irq);
1260 if (gc->irq.num_parents == 1)
1261 ret = irq_set_parent(irq, gc->irq.parents[0]);
1262 else if (gc->irq.map)
1263 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1269 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1270 * is passed as default type.
1272 if (gc->irq.default_type != IRQ_TYPE_NONE)
1273 irq_set_irq_type(irq, gc->irq.default_type);
1277 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1279 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1281 struct gpio_chip *gc = d->host_data;
1283 if (gc->irq.threaded)
1284 irq_set_nested_thread(irq, 0);
1285 irq_set_chip_and_handler(irq, NULL, NULL);
1286 irq_set_chip_data(irq, NULL);
1288 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1290 static const struct irq_domain_ops gpiochip_domain_ops = {
1291 .map = gpiochip_irq_map,
1292 .unmap = gpiochip_irq_unmap,
1293 /* Virtually all GPIO irqchips are twocell:ed */
1294 .xlate = irq_domain_xlate_twocell,
1298 * TODO: move these activate/deactivate in under the hierarchicial
1299 * irqchip implementation as static once SPMI and SSBI (all external
1300 * users) are phased over.
1303 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1304 * @domain: The IRQ domain used by this IRQ chip
1305 * @data: Outermost irq_data associated with the IRQ
1306 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1308 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1309 * used as the activate function for the &struct irq_domain_ops. The host_data
1310 * for the IRQ domain must be the &struct gpio_chip.
1312 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1313 struct irq_data *data, bool reserve)
1315 struct gpio_chip *gc = domain->host_data;
1317 return gpiochip_lock_as_irq(gc, data->hwirq);
1319 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1322 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1323 * @domain: The IRQ domain used by this IRQ chip
1324 * @data: Outermost irq_data associated with the IRQ
1326 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1327 * be used as the deactivate function for the &struct irq_domain_ops. The
1328 * host_data for the IRQ domain must be the &struct gpio_chip.
1330 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1331 struct irq_data *data)
1333 struct gpio_chip *gc = domain->host_data;
1335 return gpiochip_unlock_as_irq(gc, data->hwirq);
1337 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1339 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1341 struct irq_domain *domain = gc->irq.domain;
1343 if (!gpiochip_irqchip_irq_valid(gc, offset))
1346 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1347 if (irq_domain_is_hierarchy(domain)) {
1348 struct irq_fwspec spec;
1350 spec.fwnode = domain->fwnode;
1351 spec.param_count = 2;
1352 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1353 spec.param[1] = IRQ_TYPE_NONE;
1355 return irq_create_fwspec_mapping(&spec);
1359 return irq_create_mapping(domain, offset);
1362 static int gpiochip_irq_reqres(struct irq_data *d)
1364 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1366 return gpiochip_reqres_irq(gc, d->hwirq);
1369 static void gpiochip_irq_relres(struct irq_data *d)
1371 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1373 gpiochip_relres_irq(gc, d->hwirq);
1376 static void gpiochip_irq_mask(struct irq_data *d)
1378 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1380 if (gc->irq.irq_mask)
1381 gc->irq.irq_mask(d);
1382 gpiochip_disable_irq(gc, d->hwirq);
1385 static void gpiochip_irq_unmask(struct irq_data *d)
1387 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1389 gpiochip_enable_irq(gc, d->hwirq);
1390 if (gc->irq.irq_unmask)
1391 gc->irq.irq_unmask(d);
1394 static void gpiochip_irq_enable(struct irq_data *d)
1396 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1398 gpiochip_enable_irq(gc, d->hwirq);
1399 gc->irq.irq_enable(d);
1402 static void gpiochip_irq_disable(struct irq_data *d)
1404 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1406 gc->irq.irq_disable(d);
1407 gpiochip_disable_irq(gc, d->hwirq);
1410 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1412 struct irq_chip *irqchip = gc->irq.chip;
1414 if (!irqchip->irq_request_resources &&
1415 !irqchip->irq_release_resources) {
1416 irqchip->irq_request_resources = gpiochip_irq_reqres;
1417 irqchip->irq_release_resources = gpiochip_irq_relres;
1419 if (WARN_ON(gc->irq.irq_enable))
1421 /* Check if the irqchip already has this hook... */
1422 if (irqchip->irq_enable == gpiochip_irq_enable ||
1423 irqchip->irq_mask == gpiochip_irq_mask) {
1425 * ...and if so, give a gentle warning that this is bad
1429 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1433 if (irqchip->irq_disable) {
1434 gc->irq.irq_disable = irqchip->irq_disable;
1435 irqchip->irq_disable = gpiochip_irq_disable;
1437 gc->irq.irq_mask = irqchip->irq_mask;
1438 irqchip->irq_mask = gpiochip_irq_mask;
1441 if (irqchip->irq_enable) {
1442 gc->irq.irq_enable = irqchip->irq_enable;
1443 irqchip->irq_enable = gpiochip_irq_enable;
1445 gc->irq.irq_unmask = irqchip->irq_unmask;
1446 irqchip->irq_unmask = gpiochip_irq_unmask;
1451 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1452 * @gc: the GPIO chip to add the IRQ chip to
1453 * @lock_key: lockdep class for IRQ lock
1454 * @request_key: lockdep class for IRQ request
1456 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1457 struct lock_class_key *lock_key,
1458 struct lock_class_key *request_key)
1460 struct irq_chip *irqchip = gc->irq.chip;
1461 const struct irq_domain_ops *ops = NULL;
1462 struct device_node *np;
1469 if (gc->irq.parent_handler && gc->can_sleep) {
1470 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1474 np = gc->gpiodev->dev.of_node;
1475 type = gc->irq.default_type;
1478 * Specifying a default trigger is a terrible idea if DT or ACPI is
1479 * used to configure the interrupts, as you may end up with
1480 * conflicting triggers. Tell the user, and reset to NONE.
1482 if (WARN(np && type != IRQ_TYPE_NONE,
1483 "%s: Ignoring %u default trigger\n", np->full_name, type))
1484 type = IRQ_TYPE_NONE;
1486 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1487 acpi_handle_warn(ACPI_HANDLE(gc->parent),
1488 "Ignoring %u default trigger\n", type);
1489 type = IRQ_TYPE_NONE;
1493 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1495 gc->to_irq = gpiochip_to_irq;
1496 gc->irq.default_type = type;
1497 gc->irq.lock_key = lock_key;
1498 gc->irq.request_key = request_key;
1500 /* If a parent irqdomain is provided, let's build a hierarchy */
1501 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1502 int ret = gpiochip_hierarchy_add_domain(gc);
1506 /* Some drivers provide custom irqdomain ops */
1507 if (gc->irq.domain_ops)
1508 ops = gc->irq.domain_ops;
1511 ops = &gpiochip_domain_ops;
1512 gc->irq.domain = irq_domain_add_simple(np,
1516 if (!gc->irq.domain)
1520 if (gc->irq.parent_handler) {
1521 void *data = gc->irq.parent_handler_data ?: gc;
1523 for (i = 0; i < gc->irq.num_parents; i++) {
1525 * The parent IRQ chip is already using the chip_data
1526 * for this IRQ chip, so our callbacks simply use the
1529 irq_set_chained_handler_and_data(gc->irq.parents[i],
1530 gc->irq.parent_handler,
1535 gpiochip_set_irq_hooks(gc);
1537 acpi_gpiochip_request_interrupts(gc);
1543 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1544 * @gc: the gpiochip to remove the irqchip from
1546 * This is called only from gpiochip_remove()
1548 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1550 struct irq_chip *irqchip = gc->irq.chip;
1551 unsigned int offset;
1553 acpi_gpiochip_free_interrupts(gc);
1555 if (irqchip && gc->irq.parent_handler) {
1556 struct gpio_irq_chip *irq = &gc->irq;
1559 for (i = 0; i < irq->num_parents; i++)
1560 irq_set_chained_handler_and_data(irq->parents[i],
1564 /* Remove all IRQ mappings and delete the domain */
1565 if (gc->irq.domain) {
1568 for (offset = 0; offset < gc->ngpio; offset++) {
1569 if (!gpiochip_irqchip_irq_valid(gc, offset))
1572 irq = irq_find_mapping(gc->irq.domain, offset);
1573 irq_dispose_mapping(irq);
1576 irq_domain_remove(gc->irq.domain);
1580 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1581 irqchip->irq_request_resources = NULL;
1582 irqchip->irq_release_resources = NULL;
1584 if (irqchip->irq_enable == gpiochip_irq_enable) {
1585 irqchip->irq_enable = gc->irq.irq_enable;
1586 irqchip->irq_disable = gc->irq.irq_disable;
1589 gc->irq.irq_enable = NULL;
1590 gc->irq.irq_disable = NULL;
1591 gc->irq.chip = NULL;
1593 gpiochip_irqchip_free_valid_mask(gc);
1597 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1598 * @gc: the gpiochip to add the irqchip to
1599 * @domain: the irqdomain to add to the gpiochip
1601 * This function adds an IRQ domain to the gpiochip.
1603 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1604 struct irq_domain *domain)
1609 gc->to_irq = gpiochip_to_irq;
1610 gc->irq.domain = domain;
1614 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1616 #else /* CONFIG_GPIOLIB_IRQCHIP */
1618 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1619 struct lock_class_key *lock_key,
1620 struct lock_class_key *request_key)
1624 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1626 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1631 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1635 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1638 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1641 * gpiochip_generic_request() - request the gpio function for a pin
1642 * @gc: the gpiochip owning the GPIO
1643 * @offset: the offset of the GPIO to request for GPIO function
1645 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
1647 #ifdef CONFIG_PINCTRL
1648 if (list_empty(&gc->gpiodev->pin_ranges))
1652 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1654 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1657 * gpiochip_generic_free() - free the gpio function from a pin
1658 * @gc: the gpiochip to request the gpio function for
1659 * @offset: the offset of the GPIO to free from GPIO function
1661 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
1663 #ifdef CONFIG_PINCTRL
1664 if (list_empty(&gc->gpiodev->pin_ranges))
1668 pinctrl_gpio_free(gc->gpiodev->base + offset);
1670 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1673 * gpiochip_generic_config() - apply configuration for a pin
1674 * @gc: the gpiochip owning the GPIO
1675 * @offset: the offset of the GPIO to apply the configuration
1676 * @config: the configuration to be applied
1678 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
1679 unsigned long config)
1681 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1683 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1685 #ifdef CONFIG_PINCTRL
1688 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1689 * @gc: the gpiochip to add the range for
1690 * @pctldev: the pin controller to map to
1691 * @gpio_offset: the start offset in the current gpio_chip number space
1692 * @pin_group: name of the pin group inside the pin controller
1694 * Calling this function directly from a DeviceTree-supported
1695 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1696 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1697 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1699 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1700 struct pinctrl_dev *pctldev,
1701 unsigned int gpio_offset, const char *pin_group)
1703 struct gpio_pin_range *pin_range;
1704 struct gpio_device *gdev = gc->gpiodev;
1707 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1709 chip_err(gc, "failed to allocate pin ranges\n");
1713 /* Use local offset as range ID */
1714 pin_range->range.id = gpio_offset;
1715 pin_range->range.gc = gc;
1716 pin_range->range.name = gc->label;
1717 pin_range->range.base = gdev->base + gpio_offset;
1718 pin_range->pctldev = pctldev;
1720 ret = pinctrl_get_group_pins(pctldev, pin_group,
1721 &pin_range->range.pins,
1722 &pin_range->range.npins);
1728 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1730 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1731 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1732 pinctrl_dev_get_devname(pctldev), pin_group);
1734 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1738 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1741 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1742 * @gc: the gpiochip to add the range for
1743 * @pinctl_name: the dev_name() of the pin controller to map to
1744 * @gpio_offset: the start offset in the current gpio_chip number space
1745 * @pin_offset: the start offset in the pin controller number space
1746 * @npins: the number of pins from the offset of each pin space (GPIO and
1747 * pin controller) to accumulate in this range
1750 * 0 on success, or a negative error-code on failure.
1752 * Calling this function directly from a DeviceTree-supported
1753 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1754 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1755 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1757 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1758 unsigned int gpio_offset, unsigned int pin_offset,
1761 struct gpio_pin_range *pin_range;
1762 struct gpio_device *gdev = gc->gpiodev;
1765 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1767 chip_err(gc, "failed to allocate pin ranges\n");
1771 /* Use local offset as range ID */
1772 pin_range->range.id = gpio_offset;
1773 pin_range->range.gc = gc;
1774 pin_range->range.name = gc->label;
1775 pin_range->range.base = gdev->base + gpio_offset;
1776 pin_range->range.pin_base = pin_offset;
1777 pin_range->range.npins = npins;
1778 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1780 if (IS_ERR(pin_range->pctldev)) {
1781 ret = PTR_ERR(pin_range->pctldev);
1782 chip_err(gc, "could not create pin range\n");
1786 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1787 gpio_offset, gpio_offset + npins - 1,
1789 pin_offset, pin_offset + npins - 1);
1791 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1795 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1798 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1799 * @gc: the chip to remove all the mappings for
1801 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1803 struct gpio_pin_range *pin_range, *tmp;
1804 struct gpio_device *gdev = gc->gpiodev;
1806 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1807 list_del(&pin_range->node);
1808 pinctrl_remove_gpio_range(pin_range->pctldev,
1813 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1815 #endif /* CONFIG_PINCTRL */
1817 /* These "optional" allocation calls help prevent drivers from stomping
1818 * on each other, and help provide better diagnostics in debugfs.
1819 * They're called even less than the "set direction" calls.
1821 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
1823 struct gpio_chip *gc = desc->gdev->chip;
1825 unsigned long flags;
1829 label = kstrdup_const(label, GFP_KERNEL);
1834 spin_lock_irqsave(&gpio_lock, flags);
1836 /* NOTE: gpio_request() can be called in early boot,
1837 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1840 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1841 desc_set_label(desc, label ? : "?");
1844 goto out_free_unlock;
1848 /* gc->request may sleep */
1849 spin_unlock_irqrestore(&gpio_lock, flags);
1850 offset = gpio_chip_hwgpio(desc);
1851 if (gpiochip_line_is_valid(gc, offset))
1852 ret = gc->request(gc, offset);
1855 spin_lock_irqsave(&gpio_lock, flags);
1858 desc_set_label(desc, NULL);
1859 clear_bit(FLAG_REQUESTED, &desc->flags);
1860 goto out_free_unlock;
1863 if (gc->get_direction) {
1864 /* gc->get_direction may sleep */
1865 spin_unlock_irqrestore(&gpio_lock, flags);
1866 gpiod_get_direction(desc);
1867 spin_lock_irqsave(&gpio_lock, flags);
1869 spin_unlock_irqrestore(&gpio_lock, flags);
1873 spin_unlock_irqrestore(&gpio_lock, flags);
1879 * This descriptor validation needs to be inserted verbatim into each
1880 * function taking a descriptor, so we need to use a preprocessor
1881 * macro to avoid endless duplication. If the desc is NULL it is an
1882 * optional GPIO and calls should just bail out.
1884 static int validate_desc(const struct gpio_desc *desc, const char *func)
1889 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
1890 return PTR_ERR(desc);
1893 pr_warn("%s: invalid GPIO (no device)\n", func);
1896 if (!desc->gdev->chip) {
1897 dev_warn(&desc->gdev->dev,
1898 "%s: backing chip is gone\n", func);
1904 #define VALIDATE_DESC(desc) do { \
1905 int __valid = validate_desc(desc, __func__); \
1910 #define VALIDATE_DESC_VOID(desc) do { \
1911 int __valid = validate_desc(desc, __func__); \
1916 int gpiod_request(struct gpio_desc *desc, const char *label)
1918 int ret = -EPROBE_DEFER;
1919 struct gpio_device *gdev;
1921 VALIDATE_DESC(desc);
1924 if (try_module_get(gdev->owner)) {
1925 ret = gpiod_request_commit(desc, label);
1927 module_put(gdev->owner);
1929 get_device(&gdev->dev);
1933 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
1938 static bool gpiod_free_commit(struct gpio_desc *desc)
1941 unsigned long flags;
1942 struct gpio_chip *gc;
1946 gpiod_unexport(desc);
1948 spin_lock_irqsave(&gpio_lock, flags);
1950 gc = desc->gdev->chip;
1951 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
1953 spin_unlock_irqrestore(&gpio_lock, flags);
1954 might_sleep_if(gc->can_sleep);
1955 gc->free(gc, gpio_chip_hwgpio(desc));
1956 spin_lock_irqsave(&gpio_lock, flags);
1958 kfree_const(desc->label);
1959 desc_set_label(desc, NULL);
1960 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1961 clear_bit(FLAG_REQUESTED, &desc->flags);
1962 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1963 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1964 clear_bit(FLAG_PULL_UP, &desc->flags);
1965 clear_bit(FLAG_PULL_DOWN, &desc->flags);
1966 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
1967 clear_bit(FLAG_EDGE_RISING, &desc->flags);
1968 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
1969 clear_bit(FLAG_IS_HOGGED, &desc->flags);
1970 #ifdef CONFIG_OF_DYNAMIC
1973 #ifdef CONFIG_GPIO_CDEV
1974 WRITE_ONCE(desc->debounce_period_us, 0);
1979 spin_unlock_irqrestore(&gpio_lock, flags);
1980 blocking_notifier_call_chain(&desc->gdev->notifier,
1981 GPIOLINE_CHANGED_RELEASED, desc);
1986 void gpiod_free(struct gpio_desc *desc)
1988 if (desc && desc->gdev && gpiod_free_commit(desc)) {
1989 module_put(desc->gdev->owner);
1990 put_device(&desc->gdev->dev);
1992 WARN_ON(extra_checks);
1997 * gpiochip_is_requested - return string iff signal was requested
1998 * @gc: controller managing the signal
1999 * @offset: of signal within controller's 0..(ngpio - 1) range
2001 * Returns NULL if the GPIO is not currently requested, else a string.
2002 * The string returned is the label passed to gpio_request(); if none has been
2003 * passed it is a meaningless, non-NULL constant.
2005 * This function is for use by GPIO controller drivers. The label can
2006 * help with diagnostics, and knowing that the signal is used as a GPIO
2007 * can help avoid accidentally multiplexing it to another controller.
2009 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
2011 struct gpio_desc *desc;
2013 if (offset >= gc->ngpio)
2016 desc = gpiochip_get_desc(gc, offset);
2020 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2024 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2027 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2029 * @hwnum: hardware number of the GPIO for which to request the descriptor
2030 * @label: label for the GPIO
2031 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2032 * specify things like line inversion semantics with the machine flags
2033 * such as GPIO_OUT_LOW
2034 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2035 * can be used to specify consumer semantics such as open drain
2037 * Function allows GPIO chip drivers to request and use their own GPIO
2038 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2039 * function will not increase reference count of the GPIO chip module. This
2040 * allows the GPIO chip module to be unloaded as needed (we assume that the
2041 * GPIO chip driver handles freeing the GPIOs it has requested).
2044 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2047 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2050 enum gpio_lookup_flags lflags,
2051 enum gpiod_flags dflags)
2053 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2057 chip_err(gc, "failed to get GPIO descriptor\n");
2061 ret = gpiod_request_commit(desc, label);
2063 return ERR_PTR(ret);
2065 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2067 chip_err(gc, "setup of own GPIO %s failed\n", label);
2068 gpiod_free_commit(desc);
2069 return ERR_PTR(ret);
2074 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2077 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2078 * @desc: GPIO descriptor to free
2080 * Function frees the given GPIO requested previously with
2081 * gpiochip_request_own_desc().
2083 void gpiochip_free_own_desc(struct gpio_desc *desc)
2086 gpiod_free_commit(desc);
2088 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2091 * Drivers MUST set GPIO direction before making get/set calls. In
2092 * some cases this is done in early boot, before IRQs are enabled.
2094 * As a rule these aren't called more than once (except for drivers
2095 * using the open-drain emulation idiom) so these are natural places
2096 * to accumulate extra debugging checks. Note that we can't (yet)
2097 * rely on gpio_request() having been called beforehand.
2100 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2101 unsigned long config)
2103 if (!gc->set_config)
2106 return gc->set_config(gc, offset, config);
2109 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2110 enum pin_config_param mode,
2113 struct gpio_chip *gc = desc->gdev->chip;
2114 unsigned long config;
2116 config = pinconf_to_config_packed(mode, argument);
2117 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2120 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2121 enum pin_config_param mode,
2124 struct device *dev = &desc->gdev->dev;
2125 int gpio = gpio_chip_hwgpio(desc);
2128 ret = gpio_set_config_with_argument(desc, mode, argument);
2129 if (ret != -ENOTSUPP)
2133 case PIN_CONFIG_PERSIST_STATE:
2134 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2143 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2145 return gpio_set_config_with_argument(desc, mode, 0);
2148 static int gpio_set_bias(struct gpio_desc *desc)
2150 enum pin_config_param bias;
2153 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2154 bias = PIN_CONFIG_BIAS_DISABLE;
2155 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2156 bias = PIN_CONFIG_BIAS_PULL_UP;
2157 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2158 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2163 case PIN_CONFIG_BIAS_PULL_DOWN:
2164 case PIN_CONFIG_BIAS_PULL_UP:
2173 return gpio_set_config_with_argument_optional(desc, bias, arg);
2176 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2178 return gpio_set_config_with_argument_optional(desc,
2179 PIN_CONFIG_INPUT_DEBOUNCE,
2184 * gpiod_direction_input - set the GPIO direction to input
2185 * @desc: GPIO to set to input
2187 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2188 * be called safely on it.
2190 * Return 0 in case of success, else an error code.
2192 int gpiod_direction_input(struct gpio_desc *desc)
2194 struct gpio_chip *gc;
2197 VALIDATE_DESC(desc);
2198 gc = desc->gdev->chip;
2201 * It is legal to have no .get() and .direction_input() specified if
2202 * the chip is output-only, but you can't specify .direction_input()
2203 * and not support the .get() operation, that doesn't make sense.
2205 if (!gc->get && gc->direction_input) {
2207 "%s: missing get() but have direction_input()\n",
2213 * If we have a .direction_input() callback, things are simple,
2214 * just call it. Else we are some input-only chip so try to check the
2215 * direction (if .get_direction() is supported) else we silently
2216 * assume we are in input mode after this.
2218 if (gc->direction_input) {
2219 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2220 } else if (gc->get_direction &&
2221 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2223 "%s: missing direction_input() operation and line is output\n",
2228 clear_bit(FLAG_IS_OUT, &desc->flags);
2229 ret = gpio_set_bias(desc);
2232 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2236 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2238 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2240 struct gpio_chip *gc = desc->gdev->chip;
2245 * It's OK not to specify .direction_output() if the gpiochip is
2246 * output-only, but if there is then not even a .set() operation it
2247 * is pretty tricky to drive the output line.
2249 if (!gc->set && !gc->direction_output) {
2251 "%s: missing set() and direction_output() operations\n",
2256 if (gc->direction_output) {
2257 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2259 /* Check that we are in output mode if we can */
2260 if (gc->get_direction &&
2261 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2263 "%s: missing direction_output() operation\n",
2268 * If we can't actively set the direction, we are some
2269 * output-only chip, so just drive the output as desired.
2271 gc->set(gc, gpio_chip_hwgpio(desc), val);
2275 set_bit(FLAG_IS_OUT, &desc->flags);
2276 trace_gpio_value(desc_to_gpio(desc), 0, val);
2277 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2282 * gpiod_direction_output_raw - set the GPIO direction to output
2283 * @desc: GPIO to set to output
2284 * @value: initial output value of the GPIO
2286 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2287 * be called safely on it. The initial value of the output must be specified
2288 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2290 * Return 0 in case of success, else an error code.
2292 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2294 VALIDATE_DESC(desc);
2295 return gpiod_direction_output_raw_commit(desc, value);
2297 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2300 * gpiod_direction_output - set the GPIO direction to output
2301 * @desc: GPIO to set to output
2302 * @value: initial output value of the GPIO
2304 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2305 * be called safely on it. The initial value of the output must be specified
2306 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2309 * Return 0 in case of success, else an error code.
2311 int gpiod_direction_output(struct gpio_desc *desc, int value)
2315 VALIDATE_DESC(desc);
2316 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2321 /* GPIOs used for enabled IRQs shall not be set as output */
2322 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2323 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2325 "%s: tried to set a GPIO tied to an IRQ as output\n",
2330 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2331 /* First see if we can enable open drain in hardware */
2332 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2334 goto set_output_value;
2335 /* Emulate open drain by not actively driving the line high */
2337 ret = gpiod_direction_input(desc);
2338 goto set_output_flag;
2341 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2342 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2344 goto set_output_value;
2345 /* Emulate open source by not actively driving the line low */
2347 ret = gpiod_direction_input(desc);
2348 goto set_output_flag;
2351 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2355 ret = gpio_set_bias(desc);
2358 return gpiod_direction_output_raw_commit(desc, value);
2362 * When emulating open-source or open-drain functionalities by not
2363 * actively driving the line (setting mode to input) we still need to
2364 * set the IS_OUT flag or otherwise we won't be able to set the line
2368 set_bit(FLAG_IS_OUT, &desc->flags);
2371 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2374 * gpiod_set_config - sets @config for a GPIO
2375 * @desc: descriptor of the GPIO for which to set the configuration
2376 * @config: Same packed config format as generic pinconf
2379 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2382 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2384 struct gpio_chip *gc;
2386 VALIDATE_DESC(desc);
2387 gc = desc->gdev->chip;
2389 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2391 EXPORT_SYMBOL_GPL(gpiod_set_config);
2394 * gpiod_set_debounce - sets @debounce time for a GPIO
2395 * @desc: descriptor of the GPIO for which to set debounce time
2396 * @debounce: debounce time in microseconds
2399 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2402 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2404 unsigned long config;
2406 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2407 return gpiod_set_config(desc, config);
2409 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2412 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2413 * @desc: descriptor of the GPIO for which to configure persistence
2414 * @transitory: True to lose state on suspend or reset, false for persistence
2417 * 0 on success, otherwise a negative error code.
2419 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2421 VALIDATE_DESC(desc);
2423 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2424 * persistence state.
2426 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2428 /* If the driver supports it, set the persistence state now */
2429 return gpio_set_config_with_argument_optional(desc,
2430 PIN_CONFIG_PERSIST_STATE,
2433 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2436 * gpiod_is_active_low - test whether a GPIO is active-low or not
2437 * @desc: the gpio descriptor to test
2439 * Returns 1 if the GPIO is active-low, 0 otherwise.
2441 int gpiod_is_active_low(const struct gpio_desc *desc)
2443 VALIDATE_DESC(desc);
2444 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2446 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2449 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2450 * @desc: the gpio descriptor to change
2452 void gpiod_toggle_active_low(struct gpio_desc *desc)
2454 VALIDATE_DESC_VOID(desc);
2455 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2457 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2459 /* I/O calls are only valid after configuration completed; the relevant
2460 * "is this a valid GPIO" error checks should already have been done.
2462 * "Get" operations are often inlinable as reading a pin value register,
2463 * and masking the relevant bit in that register.
2465 * When "set" operations are inlinable, they involve writing that mask to
2466 * one register to set a low value, or a different register to set it high.
2467 * Otherwise locking is needed, so there may be little value to inlining.
2469 *------------------------------------------------------------------------
2471 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2472 * have requested the GPIO. That can include implicit requesting by
2473 * a direction setting call. Marking a gpio as requested locks its chip
2474 * in memory, guaranteeing that these table lookups need no more locking
2475 * and that gpiochip_remove() will fail.
2477 * REVISIT when debugging, consider adding some instrumentation to ensure
2478 * that the GPIO was actually requested.
2481 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2483 struct gpio_chip *gc;
2487 gc = desc->gdev->chip;
2488 offset = gpio_chip_hwgpio(desc);
2489 value = gc->get ? gc->get(gc, offset) : -EIO;
2490 value = value < 0 ? value : !!value;
2491 trace_gpio_value(desc_to_gpio(desc), 1, value);
2495 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2496 unsigned long *mask, unsigned long *bits)
2498 if (gc->get_multiple) {
2499 return gc->get_multiple(gc, mask, bits);
2500 } else if (gc->get) {
2503 for_each_set_bit(i, mask, gc->ngpio) {
2504 value = gc->get(gc, i);
2507 __assign_bit(i, bits, value);
2514 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2515 unsigned int array_size,
2516 struct gpio_desc **desc_array,
2517 struct gpio_array *array_info,
2518 unsigned long *value_bitmap)
2523 * Validate array_info against desc_array and its size.
2524 * It should immediately follow desc_array if both
2525 * have been obtained from the same gpiod_get_array() call.
2527 if (array_info && array_info->desc == desc_array &&
2528 array_size <= array_info->size &&
2529 (void *)array_info == desc_array + array_info->size) {
2531 WARN_ON(array_info->chip->can_sleep);
2533 ret = gpio_chip_get_multiple(array_info->chip,
2534 array_info->get_mask,
2539 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2540 bitmap_xor(value_bitmap, value_bitmap,
2541 array_info->invert_mask, array_size);
2543 i = find_first_zero_bit(array_info->get_mask, array_size);
2544 if (i == array_size)
2550 while (i < array_size) {
2551 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2552 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2553 unsigned long *mask, *bits;
2556 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2559 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2561 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2566 bits = mask + BITS_TO_LONGS(gc->ngpio);
2567 bitmap_zero(mask, gc->ngpio);
2570 WARN_ON(gc->can_sleep);
2572 /* collect all inputs belonging to the same chip */
2575 const struct gpio_desc *desc = desc_array[i];
2576 int hwgpio = gpio_chip_hwgpio(desc);
2578 __set_bit(hwgpio, mask);
2582 i = find_next_zero_bit(array_info->get_mask,
2584 } while ((i < array_size) &&
2585 (desc_array[i]->gdev->chip == gc));
2587 ret = gpio_chip_get_multiple(gc, mask, bits);
2589 if (mask != fastpath)
2594 for (j = first; j < i; ) {
2595 const struct gpio_desc *desc = desc_array[j];
2596 int hwgpio = gpio_chip_hwgpio(desc);
2597 int value = test_bit(hwgpio, bits);
2599 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2601 __assign_bit(j, value_bitmap, value);
2602 trace_gpio_value(desc_to_gpio(desc), 1, value);
2606 j = find_next_zero_bit(array_info->get_mask, i,
2610 if (mask != fastpath)
2617 * gpiod_get_raw_value() - return a gpio's raw value
2618 * @desc: gpio whose value will be returned
2620 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2621 * its ACTIVE_LOW status, or negative errno on failure.
2623 * This function can be called from contexts where we cannot sleep, and will
2624 * complain if the GPIO chip functions potentially sleep.
2626 int gpiod_get_raw_value(const struct gpio_desc *desc)
2628 VALIDATE_DESC(desc);
2629 /* Should be using gpiod_get_raw_value_cansleep() */
2630 WARN_ON(desc->gdev->chip->can_sleep);
2631 return gpiod_get_raw_value_commit(desc);
2633 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2636 * gpiod_get_value() - return a gpio's value
2637 * @desc: gpio whose value will be returned
2639 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2640 * account, or negative errno on failure.
2642 * This function can be called from contexts where we cannot sleep, and will
2643 * complain if the GPIO chip functions potentially sleep.
2645 int gpiod_get_value(const struct gpio_desc *desc)
2649 VALIDATE_DESC(desc);
2650 /* Should be using gpiod_get_value_cansleep() */
2651 WARN_ON(desc->gdev->chip->can_sleep);
2653 value = gpiod_get_raw_value_commit(desc);
2657 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2662 EXPORT_SYMBOL_GPL(gpiod_get_value);
2665 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2666 * @array_size: number of elements in the descriptor array / value bitmap
2667 * @desc_array: array of GPIO descriptors whose values will be read
2668 * @array_info: information on applicability of fast bitmap processing path
2669 * @value_bitmap: bitmap to store the read values
2671 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2672 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2673 * else an error code.
2675 * This function can be called from contexts where we cannot sleep,
2676 * and it will complain if the GPIO chip functions potentially sleep.
2678 int gpiod_get_raw_array_value(unsigned int array_size,
2679 struct gpio_desc **desc_array,
2680 struct gpio_array *array_info,
2681 unsigned long *value_bitmap)
2685 return gpiod_get_array_value_complex(true, false, array_size,
2686 desc_array, array_info,
2689 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2692 * gpiod_get_array_value() - read values from an array of GPIOs
2693 * @array_size: number of elements in the descriptor array / value bitmap
2694 * @desc_array: array of GPIO descriptors whose values will be read
2695 * @array_info: information on applicability of fast bitmap processing path
2696 * @value_bitmap: bitmap to store the read values
2698 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2699 * into account. Return 0 in case of success, else an error code.
2701 * This function can be called from contexts where we cannot sleep,
2702 * and it will complain if the GPIO chip functions potentially sleep.
2704 int gpiod_get_array_value(unsigned int array_size,
2705 struct gpio_desc **desc_array,
2706 struct gpio_array *array_info,
2707 unsigned long *value_bitmap)
2711 return gpiod_get_array_value_complex(false, false, array_size,
2712 desc_array, array_info,
2715 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2718 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2719 * @desc: gpio descriptor whose state need to be set.
2720 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2722 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2725 struct gpio_chip *gc = desc->gdev->chip;
2726 int offset = gpio_chip_hwgpio(desc);
2729 ret = gc->direction_input(gc, offset);
2731 ret = gc->direction_output(gc, offset, 0);
2733 set_bit(FLAG_IS_OUT, &desc->flags);
2735 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2738 "%s: Error in set_value for open drain err %d\n",
2743 * _gpio_set_open_source_value() - Set the open source gpio's value.
2744 * @desc: gpio descriptor whose state need to be set.
2745 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2747 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2750 struct gpio_chip *gc = desc->gdev->chip;
2751 int offset = gpio_chip_hwgpio(desc);
2754 ret = gc->direction_output(gc, offset, 1);
2756 set_bit(FLAG_IS_OUT, &desc->flags);
2758 ret = gc->direction_input(gc, offset);
2760 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2763 "%s: Error in set_value for open source err %d\n",
2767 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2769 struct gpio_chip *gc;
2771 gc = desc->gdev->chip;
2772 trace_gpio_value(desc_to_gpio(desc), 0, value);
2773 gc->set(gc, gpio_chip_hwgpio(desc), value);
2777 * set multiple outputs on the same chip;
2778 * use the chip's set_multiple function if available;
2779 * otherwise set the outputs sequentially;
2780 * @chip: the GPIO chip we operate on
2781 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2782 * defines which outputs are to be changed
2783 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2784 * defines the values the outputs specified by mask are to be set to
2786 static void gpio_chip_set_multiple(struct gpio_chip *gc,
2787 unsigned long *mask, unsigned long *bits)
2789 if (gc->set_multiple) {
2790 gc->set_multiple(gc, mask, bits);
2794 /* set outputs if the corresponding mask bit is set */
2795 for_each_set_bit(i, mask, gc->ngpio)
2796 gc->set(gc, i, test_bit(i, bits));
2800 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
2801 unsigned int array_size,
2802 struct gpio_desc **desc_array,
2803 struct gpio_array *array_info,
2804 unsigned long *value_bitmap)
2809 * Validate array_info against desc_array and its size.
2810 * It should immediately follow desc_array if both
2811 * have been obtained from the same gpiod_get_array() call.
2813 if (array_info && array_info->desc == desc_array &&
2814 array_size <= array_info->size &&
2815 (void *)array_info == desc_array + array_info->size) {
2817 WARN_ON(array_info->chip->can_sleep);
2819 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2820 bitmap_xor(value_bitmap, value_bitmap,
2821 array_info->invert_mask, array_size);
2823 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2826 i = find_first_zero_bit(array_info->set_mask, array_size);
2827 if (i == array_size)
2833 while (i < array_size) {
2834 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2835 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2836 unsigned long *mask, *bits;
2839 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2842 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2844 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2849 bits = mask + BITS_TO_LONGS(gc->ngpio);
2850 bitmap_zero(mask, gc->ngpio);
2853 WARN_ON(gc->can_sleep);
2856 struct gpio_desc *desc = desc_array[i];
2857 int hwgpio = gpio_chip_hwgpio(desc);
2858 int value = test_bit(i, value_bitmap);
2861 * Pins applicable for fast input but not for
2862 * fast output processing may have been already
2863 * inverted inside the fast path, skip them.
2865 if (!raw && !(array_info &&
2866 test_bit(i, array_info->invert_mask)) &&
2867 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2869 trace_gpio_value(desc_to_gpio(desc), 0, value);
2871 * collect all normal outputs belonging to the same chip
2872 * open drain and open source outputs are set individually
2874 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2875 gpio_set_open_drain_value_commit(desc, value);
2876 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2877 gpio_set_open_source_value_commit(desc, value);
2879 __set_bit(hwgpio, mask);
2880 __assign_bit(hwgpio, bits, value);
2886 i = find_next_zero_bit(array_info->set_mask,
2888 } while ((i < array_size) &&
2889 (desc_array[i]->gdev->chip == gc));
2890 /* push collected bits to outputs */
2892 gpio_chip_set_multiple(gc, mask, bits);
2894 if (mask != fastpath)
2901 * gpiod_set_raw_value() - assign a gpio's raw value
2902 * @desc: gpio whose value will be assigned
2903 * @value: value to assign
2905 * Set the raw value of the GPIO, i.e. the value of its physical line without
2906 * regard for its ACTIVE_LOW status.
2908 * This function can be called from contexts where we cannot sleep, and will
2909 * complain if the GPIO chip functions potentially sleep.
2911 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2913 VALIDATE_DESC_VOID(desc);
2914 /* Should be using gpiod_set_raw_value_cansleep() */
2915 WARN_ON(desc->gdev->chip->can_sleep);
2916 gpiod_set_raw_value_commit(desc, value);
2918 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2921 * gpiod_set_value_nocheck() - set a GPIO line value without checking
2922 * @desc: the descriptor to set the value on
2923 * @value: value to set
2925 * This sets the value of a GPIO line backing a descriptor, applying
2926 * different semantic quirks like active low and open drain/source
2929 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
2931 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2933 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2934 gpio_set_open_drain_value_commit(desc, value);
2935 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2936 gpio_set_open_source_value_commit(desc, value);
2938 gpiod_set_raw_value_commit(desc, value);
2942 * gpiod_set_value() - assign a gpio's value
2943 * @desc: gpio whose value will be assigned
2944 * @value: value to assign
2946 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2947 * OPEN_DRAIN and OPEN_SOURCE flags into account.
2949 * This function can be called from contexts where we cannot sleep, and will
2950 * complain if the GPIO chip functions potentially sleep.
2952 void gpiod_set_value(struct gpio_desc *desc, int value)
2954 VALIDATE_DESC_VOID(desc);
2955 /* Should be using gpiod_set_value_cansleep() */
2956 WARN_ON(desc->gdev->chip->can_sleep);
2957 gpiod_set_value_nocheck(desc, value);
2959 EXPORT_SYMBOL_GPL(gpiod_set_value);
2962 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
2963 * @array_size: number of elements in the descriptor array / value bitmap
2964 * @desc_array: array of GPIO descriptors whose values will be assigned
2965 * @array_info: information on applicability of fast bitmap processing path
2966 * @value_bitmap: bitmap of values to assign
2968 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2969 * without regard for their ACTIVE_LOW status.
2971 * This function can be called from contexts where we cannot sleep, and will
2972 * complain if the GPIO chip functions potentially sleep.
2974 int gpiod_set_raw_array_value(unsigned int array_size,
2975 struct gpio_desc **desc_array,
2976 struct gpio_array *array_info,
2977 unsigned long *value_bitmap)
2981 return gpiod_set_array_value_complex(true, false, array_size,
2982 desc_array, array_info, value_bitmap);
2984 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
2987 * gpiod_set_array_value() - assign values to an array of GPIOs
2988 * @array_size: number of elements in the descriptor array / value bitmap
2989 * @desc_array: array of GPIO descriptors whose values will be assigned
2990 * @array_info: information on applicability of fast bitmap processing path
2991 * @value_bitmap: bitmap of values to assign
2993 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2996 * This function can be called from contexts where we cannot sleep, and will
2997 * complain if the GPIO chip functions potentially sleep.
2999 int gpiod_set_array_value(unsigned int array_size,
3000 struct gpio_desc **desc_array,
3001 struct gpio_array *array_info,
3002 unsigned long *value_bitmap)
3006 return gpiod_set_array_value_complex(false, false, array_size,
3007 desc_array, array_info,
3010 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3013 * gpiod_cansleep() - report whether gpio value access may sleep
3014 * @desc: gpio to check
3017 int gpiod_cansleep(const struct gpio_desc *desc)
3019 VALIDATE_DESC(desc);
3020 return desc->gdev->chip->can_sleep;
3022 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3025 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3026 * @desc: gpio to set the consumer name on
3027 * @name: the new consumer name
3029 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3031 VALIDATE_DESC(desc);
3033 name = kstrdup_const(name, GFP_KERNEL);
3038 kfree_const(desc->label);
3039 desc_set_label(desc, name);
3043 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3046 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3047 * @desc: gpio whose IRQ will be returned (already requested)
3049 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3052 int gpiod_to_irq(const struct gpio_desc *desc)
3054 struct gpio_chip *gc;
3058 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3059 * requires this function to not return zero on an invalid descriptor
3060 * but rather a negative error number.
3062 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3065 gc = desc->gdev->chip;
3066 offset = gpio_chip_hwgpio(desc);
3068 int retirq = gc->to_irq(gc, offset);
3070 /* Zero means NO_IRQ */
3078 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3081 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3082 * @gc: the chip the GPIO to lock belongs to
3083 * @offset: the offset of the GPIO to lock as IRQ
3085 * This is used directly by GPIO drivers that want to lock down
3086 * a certain GPIO line to be used for IRQs.
3088 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3090 struct gpio_desc *desc;
3092 desc = gpiochip_get_desc(gc, offset);
3094 return PTR_ERR(desc);
3097 * If it's fast: flush the direction setting if something changed
3100 if (!gc->can_sleep && gc->get_direction) {
3101 int dir = gpiod_get_direction(desc);
3104 chip_err(gc, "%s: cannot get GPIO direction\n",
3110 /* To be valid for IRQ the line needs to be input or open drain */
3111 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3112 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3114 "%s: tried to flag a GPIO set as output for IRQ\n",
3119 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3120 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3123 * If the consumer has not set up a label (such as when the
3124 * IRQ is referenced from .to_irq()) we set up a label here
3125 * so it is clear this is used as an interrupt.
3128 desc_set_label(desc, "interrupt");
3132 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3135 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3136 * @gc: the chip the GPIO to lock belongs to
3137 * @offset: the offset of the GPIO to lock as IRQ
3139 * This is used directly by GPIO drivers that want to indicate
3140 * that a certain GPIO is no longer used exclusively for IRQ.
3142 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3144 struct gpio_desc *desc;
3146 desc = gpiochip_get_desc(gc, offset);
3150 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3151 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3153 /* If we only had this marking, erase it */
3154 if (desc->label && !strcmp(desc->label, "interrupt"))
3155 desc_set_label(desc, NULL);
3157 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3159 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3161 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3163 if (!IS_ERR(desc) &&
3164 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3165 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3167 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3169 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3171 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3173 if (!IS_ERR(desc) &&
3174 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3176 * We must not be output when using IRQ UNLESS we are
3179 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3180 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3181 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3184 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3186 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3188 if (offset >= gc->ngpio)
3191 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3193 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3195 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3199 if (!try_module_get(gc->gpiodev->owner))
3202 ret = gpiochip_lock_as_irq(gc, offset);
3204 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3205 module_put(gc->gpiodev->owner);
3210 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3212 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3214 gpiochip_unlock_as_irq(gc, offset);
3215 module_put(gc->gpiodev->owner);
3217 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3219 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3221 if (offset >= gc->ngpio)
3224 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3226 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3228 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3230 if (offset >= gc->ngpio)
3233 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3235 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3237 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3239 if (offset >= gc->ngpio)
3242 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3244 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3247 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3248 * @desc: gpio whose value will be returned
3250 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3251 * its ACTIVE_LOW status, or negative errno on failure.
3253 * This function is to be called from contexts that can sleep.
3255 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3257 might_sleep_if(extra_checks);
3258 VALIDATE_DESC(desc);
3259 return gpiod_get_raw_value_commit(desc);
3261 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3264 * gpiod_get_value_cansleep() - return a gpio's value
3265 * @desc: gpio whose value will be returned
3267 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3268 * account, or negative errno on failure.
3270 * This function is to be called from contexts that can sleep.
3272 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3276 might_sleep_if(extra_checks);
3277 VALIDATE_DESC(desc);
3278 value = gpiod_get_raw_value_commit(desc);
3282 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3287 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3290 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3291 * @array_size: number of elements in the descriptor array / value bitmap
3292 * @desc_array: array of GPIO descriptors whose values will be read
3293 * @array_info: information on applicability of fast bitmap processing path
3294 * @value_bitmap: bitmap to store the read values
3296 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3297 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3298 * else an error code.
3300 * This function is to be called from contexts that can sleep.
3302 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3303 struct gpio_desc **desc_array,
3304 struct gpio_array *array_info,
3305 unsigned long *value_bitmap)
3307 might_sleep_if(extra_checks);
3310 return gpiod_get_array_value_complex(true, true, array_size,
3311 desc_array, array_info,
3314 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3317 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3318 * @array_size: number of elements in the descriptor array / value bitmap
3319 * @desc_array: array of GPIO descriptors whose values will be read
3320 * @array_info: information on applicability of fast bitmap processing path
3321 * @value_bitmap: bitmap to store the read values
3323 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3324 * into account. Return 0 in case of success, else an error code.
3326 * This function is to be called from contexts that can sleep.
3328 int gpiod_get_array_value_cansleep(unsigned int array_size,
3329 struct gpio_desc **desc_array,
3330 struct gpio_array *array_info,
3331 unsigned long *value_bitmap)
3333 might_sleep_if(extra_checks);
3336 return gpiod_get_array_value_complex(false, true, array_size,
3337 desc_array, array_info,
3340 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3343 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3344 * @desc: gpio whose value will be assigned
3345 * @value: value to assign
3347 * Set the raw value of the GPIO, i.e. the value of its physical line without
3348 * regard for its ACTIVE_LOW status.
3350 * This function is to be called from contexts that can sleep.
3352 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3354 might_sleep_if(extra_checks);
3355 VALIDATE_DESC_VOID(desc);
3356 gpiod_set_raw_value_commit(desc, value);
3358 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3361 * gpiod_set_value_cansleep() - assign a gpio's value
3362 * @desc: gpio whose value will be assigned
3363 * @value: value to assign
3365 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3368 * This function is to be called from contexts that can sleep.
3370 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3372 might_sleep_if(extra_checks);
3373 VALIDATE_DESC_VOID(desc);
3374 gpiod_set_value_nocheck(desc, value);
3376 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3379 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3380 * @array_size: number of elements in the descriptor array / value bitmap
3381 * @desc_array: array of GPIO descriptors whose values will be assigned
3382 * @array_info: information on applicability of fast bitmap processing path
3383 * @value_bitmap: bitmap of values to assign
3385 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3386 * without regard for their ACTIVE_LOW status.
3388 * This function is to be called from contexts that can sleep.
3390 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3391 struct gpio_desc **desc_array,
3392 struct gpio_array *array_info,
3393 unsigned long *value_bitmap)
3395 might_sleep_if(extra_checks);
3398 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3399 array_info, value_bitmap);
3401 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3404 * gpiod_add_lookup_tables() - register GPIO device consumers
3405 * @tables: list of tables of consumers to register
3406 * @n: number of tables in the list
3408 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3412 mutex_lock(&gpio_lookup_lock);
3414 for (i = 0; i < n; i++)
3415 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3417 mutex_unlock(&gpio_lookup_lock);
3421 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3422 * @array_size: number of elements in the descriptor array / value bitmap
3423 * @desc_array: array of GPIO descriptors whose values will be assigned
3424 * @array_info: information on applicability of fast bitmap processing path
3425 * @value_bitmap: bitmap of values to assign
3427 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3430 * This function is to be called from contexts that can sleep.
3432 int gpiod_set_array_value_cansleep(unsigned int array_size,
3433 struct gpio_desc **desc_array,
3434 struct gpio_array *array_info,
3435 unsigned long *value_bitmap)
3437 might_sleep_if(extra_checks);
3440 return gpiod_set_array_value_complex(false, true, array_size,
3441 desc_array, array_info,
3444 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3447 * gpiod_add_lookup_table() - register GPIO device consumers
3448 * @table: table of consumers to register
3450 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3452 mutex_lock(&gpio_lookup_lock);
3454 list_add_tail(&table->list, &gpio_lookup_list);
3456 mutex_unlock(&gpio_lookup_lock);
3458 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3461 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3462 * @table: table of consumers to unregister
3464 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3466 mutex_lock(&gpio_lookup_lock);
3468 list_del(&table->list);
3470 mutex_unlock(&gpio_lookup_lock);
3472 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3475 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3476 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3478 void gpiod_add_hogs(struct gpiod_hog *hogs)
3480 struct gpio_chip *gc;
3481 struct gpiod_hog *hog;
3483 mutex_lock(&gpio_machine_hogs_mutex);
3485 for (hog = &hogs[0]; hog->chip_label; hog++) {
3486 list_add_tail(&hog->list, &gpio_machine_hogs);
3489 * The chip may have been registered earlier, so check if it
3490 * exists and, if so, try to hog the line now.
3492 gc = find_chip_by_name(hog->chip_label);
3494 gpiochip_machine_hog(gc, hog);
3497 mutex_unlock(&gpio_machine_hogs_mutex);
3499 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3501 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3503 const char *dev_id = dev ? dev_name(dev) : NULL;
3504 struct gpiod_lookup_table *table;
3506 mutex_lock(&gpio_lookup_lock);
3508 list_for_each_entry(table, &gpio_lookup_list, list) {
3509 if (table->dev_id && dev_id) {
3511 * Valid strings on both ends, must be identical to have
3514 if (!strcmp(table->dev_id, dev_id))
3518 * One of the pointers is NULL, so both must be to have
3521 if (dev_id == table->dev_id)
3528 mutex_unlock(&gpio_lookup_lock);
3532 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3533 unsigned int idx, unsigned long *flags)
3535 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3536 struct gpiod_lookup_table *table;
3537 struct gpiod_lookup *p;
3539 table = gpiod_find_lookup_table(dev);
3543 for (p = &table->table[0]; p->key; p++) {
3544 struct gpio_chip *gc;
3546 /* idx must always match exactly */
3550 /* If the lookup entry has a con_id, require exact match */
3551 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3554 if (p->chip_hwnum == U16_MAX) {
3555 desc = gpio_name_to_desc(p->key);
3561 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3563 return ERR_PTR(-EPROBE_DEFER);
3566 gc = find_chip_by_name(p->key);
3570 * As the lookup table indicates a chip with
3571 * p->key should exist, assume it may
3572 * still appear later and let the interested
3573 * consumer be probed again or let the Deferred
3574 * Probe infrastructure handle the error.
3576 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3578 return ERR_PTR(-EPROBE_DEFER);
3581 if (gc->ngpio <= p->chip_hwnum) {
3583 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3584 idx, p->chip_hwnum, gc->ngpio - 1,
3586 return ERR_PTR(-EINVAL);
3589 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3598 static int platform_gpio_count(struct device *dev, const char *con_id)
3600 struct gpiod_lookup_table *table;
3601 struct gpiod_lookup *p;
3602 unsigned int count = 0;
3604 table = gpiod_find_lookup_table(dev);
3608 for (p = &table->table[0]; p->key; p++) {
3609 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3610 (!con_id && !p->con_id))
3620 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3621 * @fwnode: handle of the firmware node
3622 * @con_id: function within the GPIO consumer
3623 * @index: index of the GPIO to obtain for the consumer
3624 * @flags: GPIO initialization flags
3625 * @label: label to attach to the requested GPIO
3627 * This function can be used for drivers that get their configuration
3628 * from opaque firmware.
3630 * The function properly finds the corresponding GPIO using whatever is the
3631 * underlying firmware interface and then makes sure that the GPIO
3632 * descriptor is requested before it is returned to the caller.
3635 * On successful request the GPIO pin is configured in accordance with
3638 * In case of error an ERR_PTR() is returned.
3640 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3641 const char *con_id, int index,
3642 enum gpiod_flags flags,
3645 struct gpio_desc *desc;
3646 char prop_name[32]; /* 32 is max size of property name */
3649 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3651 snprintf(prop_name, sizeof(prop_name), "%s-%s",
3652 con_id, gpio_suffixes[i]);
3654 snprintf(prop_name, sizeof(prop_name), "%s",
3657 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3659 if (!gpiod_not_found(desc))
3665 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
3668 * gpiod_count - return the number of GPIOs associated with a device / function
3669 * or -ENOENT if no GPIO has been assigned to the requested function
3670 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3671 * @con_id: function within the GPIO consumer
3673 int gpiod_count(struct device *dev, const char *con_id)
3675 int count = -ENOENT;
3677 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3678 count = of_gpio_get_count(dev, con_id);
3679 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3680 count = acpi_gpio_count(dev, con_id);
3683 count = platform_gpio_count(dev, con_id);
3687 EXPORT_SYMBOL_GPL(gpiod_count);
3690 * gpiod_get - obtain a GPIO for a given GPIO function
3691 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3692 * @con_id: function within the GPIO consumer
3693 * @flags: optional GPIO initialization flags
3695 * Return the GPIO descriptor corresponding to the function con_id of device
3696 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3697 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3699 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3700 enum gpiod_flags flags)
3702 return gpiod_get_index(dev, con_id, 0, flags);
3704 EXPORT_SYMBOL_GPL(gpiod_get);
3707 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3708 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3709 * @con_id: function within the GPIO consumer
3710 * @flags: optional GPIO initialization flags
3712 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3713 * the requested function it will return NULL. This is convenient for drivers
3714 * that need to handle optional GPIOs.
3716 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3718 enum gpiod_flags flags)
3720 return gpiod_get_index_optional(dev, con_id, 0, flags);
3722 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3726 * gpiod_configure_flags - helper function to configure a given GPIO
3727 * @desc: gpio whose value will be assigned
3728 * @con_id: function within the GPIO consumer
3729 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3730 * of_find_gpio() or of_get_gpio_hog()
3731 * @dflags: gpiod_flags - optional GPIO initialization flags
3733 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3734 * requested function and/or index, or another IS_ERR() code if an error
3735 * occurred while trying to acquire the GPIO.
3737 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3738 unsigned long lflags, enum gpiod_flags dflags)
3742 if (lflags & GPIO_ACTIVE_LOW)
3743 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3745 if (lflags & GPIO_OPEN_DRAIN)
3746 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3747 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3749 * This enforces open drain mode from the consumer side.
3750 * This is necessary for some busses like I2C, but the lookup
3751 * should *REALLY* have specified them as open drain in the
3752 * first place, so print a little warning here.
3754 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3756 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3759 if (lflags & GPIO_OPEN_SOURCE)
3760 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3762 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3764 "both pull-up and pull-down enabled, invalid configuration\n");
3768 if (lflags & GPIO_PULL_UP)
3769 set_bit(FLAG_PULL_UP, &desc->flags);
3770 else if (lflags & GPIO_PULL_DOWN)
3771 set_bit(FLAG_PULL_DOWN, &desc->flags);
3773 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3777 /* No particular flag request, return here... */
3778 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3779 gpiod_dbg(desc, "no flags found for %s\n", con_id);
3784 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3785 ret = gpiod_direction_output(desc,
3786 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3788 ret = gpiod_direction_input(desc);
3794 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3795 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3796 * @con_id: function within the GPIO consumer
3797 * @idx: index of the GPIO to obtain in the consumer
3798 * @flags: optional GPIO initialization flags
3800 * This variant of gpiod_get() allows to access GPIOs other than the first
3801 * defined one for functions that define several GPIOs.
3803 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3804 * requested function and/or index, or another IS_ERR() code if an error
3805 * occurred while trying to acquire the GPIO.
3807 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3810 enum gpiod_flags flags)
3812 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3813 struct gpio_desc *desc = NULL;
3815 /* Maybe we have a device name, maybe not */
3816 const char *devname = dev ? dev_name(dev) : "?";
3818 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3821 /* Using device tree? */
3822 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3823 dev_dbg(dev, "using device tree for GPIO lookup\n");
3824 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3825 } else if (ACPI_COMPANION(dev)) {
3826 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3827 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3832 * Either we are not using DT or ACPI, or their lookup did not return
3833 * a result. In that case, use platform lookup as a fallback.
3835 if (!desc || gpiod_not_found(desc)) {
3836 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3837 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3841 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3846 * If a connection label was passed use that, else attempt to use
3847 * the device name as label
3849 ret = gpiod_request(desc, con_id ? con_id : devname);
3851 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
3853 * This happens when there are several consumers for
3854 * the same GPIO line: we just return here without
3855 * further initialization. It is a bit if a hack.
3856 * This is necessary to support fixed regulators.
3858 * FIXME: Make this more sane and safe.
3860 dev_info(dev, "nonexclusive access to GPIO for %s\n",
3861 con_id ? con_id : devname);
3864 return ERR_PTR(ret);
3868 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3870 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3872 return ERR_PTR(ret);
3875 blocking_notifier_call_chain(&desc->gdev->notifier,
3876 GPIOLINE_CHANGED_REQUESTED, desc);
3880 EXPORT_SYMBOL_GPL(gpiod_get_index);
3883 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3884 * @fwnode: handle of the firmware node
3885 * @propname: name of the firmware property representing the GPIO
3886 * @index: index of the GPIO to obtain for the consumer
3887 * @dflags: GPIO initialization flags
3888 * @label: label to attach to the requested GPIO
3890 * This function can be used for drivers that get their configuration
3891 * from opaque firmware.
3893 * The function properly finds the corresponding GPIO using whatever is the
3894 * underlying firmware interface and then makes sure that the GPIO
3895 * descriptor is requested before it is returned to the caller.
3898 * On successful request the GPIO pin is configured in accordance with
3901 * In case of error an ERR_PTR() is returned.
3903 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3904 const char *propname, int index,
3905 enum gpiod_flags dflags,
3908 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3909 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3913 return ERR_PTR(-EINVAL);
3915 if (is_of_node(fwnode)) {
3916 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3921 } else if (is_acpi_node(fwnode)) {
3922 struct acpi_gpio_info info;
3924 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3928 acpi_gpio_update_gpiod_flags(&dflags, &info);
3929 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
3932 /* Currently only ACPI takes this path */
3933 ret = gpiod_request(desc, label);
3935 return ERR_PTR(ret);
3937 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3940 return ERR_PTR(ret);
3943 blocking_notifier_call_chain(&desc->gdev->notifier,
3944 GPIOLINE_CHANGED_REQUESTED, desc);
3948 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3951 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3953 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3954 * @con_id: function within the GPIO consumer
3955 * @index: index of the GPIO to obtain in the consumer
3956 * @flags: optional GPIO initialization flags
3958 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3959 * specified index was assigned to the requested function it will return NULL.
3960 * This is convenient for drivers that need to handle optional GPIOs.
3962 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
3965 enum gpiod_flags flags)
3967 struct gpio_desc *desc;
3969 desc = gpiod_get_index(dev, con_id, index, flags);
3970 if (gpiod_not_found(desc))
3975 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3978 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3979 * @desc: gpio whose value will be assigned
3980 * @name: gpio line name
3981 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3982 * of_find_gpio() or of_get_gpio_hog()
3983 * @dflags: gpiod_flags - optional GPIO initialization flags
3985 int gpiod_hog(struct gpio_desc *desc, const char *name,
3986 unsigned long lflags, enum gpiod_flags dflags)
3988 struct gpio_chip *gc;
3989 struct gpio_desc *local_desc;
3993 gc = gpiod_to_chip(desc);
3994 hwnum = gpio_chip_hwgpio(desc);
3996 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
3998 if (IS_ERR(local_desc)) {
3999 ret = PTR_ERR(local_desc);
4000 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4001 name, gc->label, hwnum, ret);
4005 /* Mark GPIO as hogged so it can be identified and removed later */
4006 set_bit(FLAG_IS_HOGGED, &desc->flags);
4008 gpiod_info(desc, "hogged as %s%s\n",
4009 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4010 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4011 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4017 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4018 * @gc: gpio chip to act on
4020 static void gpiochip_free_hogs(struct gpio_chip *gc)
4024 for (id = 0; id < gc->ngpio; id++) {
4025 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4026 gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
4031 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4032 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4033 * @con_id: function within the GPIO consumer
4034 * @flags: optional GPIO initialization flags
4036 * This function acquires all the GPIOs defined under a given function.
4038 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4039 * no GPIO has been assigned to the requested function, or another IS_ERR()
4040 * code if an error occurred while trying to acquire the GPIOs.
4042 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4044 enum gpiod_flags flags)
4046 struct gpio_desc *desc;
4047 struct gpio_descs *descs;
4048 struct gpio_array *array_info = NULL;
4049 struct gpio_chip *gc;
4050 int count, bitmap_size;
4052 count = gpiod_count(dev, con_id);
4054 return ERR_PTR(count);
4056 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4058 return ERR_PTR(-ENOMEM);
4060 for (descs->ndescs = 0; descs->ndescs < count; ) {
4061 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4063 gpiod_put_array(descs);
4064 return ERR_CAST(desc);
4067 descs->desc[descs->ndescs] = desc;
4069 gc = gpiod_to_chip(desc);
4071 * If pin hardware number of array member 0 is also 0, select
4072 * its chip as a candidate for fast bitmap processing path.
4074 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4075 struct gpio_descs *array;
4077 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4080 array = kzalloc(struct_size(descs, desc, count) +
4081 struct_size(array_info, invert_mask,
4082 3 * bitmap_size), GFP_KERNEL);
4084 gpiod_put_array(descs);
4085 return ERR_PTR(-ENOMEM);
4088 memcpy(array, descs,
4089 struct_size(descs, desc, descs->ndescs + 1));
4093 array_info = (void *)(descs->desc + count);
4094 array_info->get_mask = array_info->invert_mask +
4096 array_info->set_mask = array_info->get_mask +
4099 array_info->desc = descs->desc;
4100 array_info->size = count;
4101 array_info->chip = gc;
4102 bitmap_set(array_info->get_mask, descs->ndescs,
4103 count - descs->ndescs);
4104 bitmap_set(array_info->set_mask, descs->ndescs,
4105 count - descs->ndescs);
4106 descs->info = array_info;
4108 /* Unmark array members which don't belong to the 'fast' chip */
4109 if (array_info && array_info->chip != gc) {
4110 __clear_bit(descs->ndescs, array_info->get_mask);
4111 __clear_bit(descs->ndescs, array_info->set_mask);
4114 * Detect array members which belong to the 'fast' chip
4115 * but their pins are not in hardware order.
4117 else if (array_info &&
4118 gpio_chip_hwgpio(desc) != descs->ndescs) {
4120 * Don't use fast path if all array members processed so
4121 * far belong to the same chip as this one but its pin
4122 * hardware number is different from its array index.
4124 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4127 __clear_bit(descs->ndescs,
4128 array_info->get_mask);
4129 __clear_bit(descs->ndescs,
4130 array_info->set_mask);
4132 } else if (array_info) {
4133 /* Exclude open drain or open source from fast output */
4134 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4135 gpiochip_line_is_open_source(gc, descs->ndescs))
4136 __clear_bit(descs->ndescs,
4137 array_info->set_mask);
4138 /* Identify 'fast' pins which require invertion */
4139 if (gpiod_is_active_low(desc))
4140 __set_bit(descs->ndescs,
4141 array_info->invert_mask);
4148 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4149 array_info->chip->label, array_info->size,
4150 *array_info->get_mask, *array_info->set_mask,
4151 *array_info->invert_mask);
4154 EXPORT_SYMBOL_GPL(gpiod_get_array);
4157 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4159 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4160 * @con_id: function within the GPIO consumer
4161 * @flags: optional GPIO initialization flags
4163 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4164 * assigned to the requested function it will return NULL.
4166 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4168 enum gpiod_flags flags)
4170 struct gpio_descs *descs;
4172 descs = gpiod_get_array(dev, con_id, flags);
4173 if (gpiod_not_found(descs))
4178 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4181 * gpiod_put - dispose of a GPIO descriptor
4182 * @desc: GPIO descriptor to dispose of
4184 * No descriptor can be used after gpiod_put() has been called on it.
4186 void gpiod_put(struct gpio_desc *desc)
4191 EXPORT_SYMBOL_GPL(gpiod_put);
4194 * gpiod_put_array - dispose of multiple GPIO descriptors
4195 * @descs: struct gpio_descs containing an array of descriptors
4197 void gpiod_put_array(struct gpio_descs *descs)
4201 for (i = 0; i < descs->ndescs; i++)
4202 gpiod_put(descs->desc[i]);
4206 EXPORT_SYMBOL_GPL(gpiod_put_array);
4208 static int __init gpiolib_dev_init(void)
4212 /* Register GPIO sysfs bus */
4213 ret = bus_register(&gpio_bus_type);
4215 pr_err("gpiolib: could not register GPIO bus type\n");
4219 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4221 pr_err("gpiolib: failed to allocate char dev region\n");
4222 bus_unregister(&gpio_bus_type);
4226 gpiolib_initialized = true;
4227 gpiochip_setup_devs();
4229 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4230 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4231 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4235 core_initcall(gpiolib_dev_init);
4237 #ifdef CONFIG_DEBUG_FS
4239 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4242 struct gpio_chip *gc = gdev->chip;
4243 unsigned gpio = gdev->base;
4244 struct gpio_desc *gdesc = &gdev->descs[0];
4249 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4250 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4252 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4258 gpiod_get_direction(gdesc);
4259 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4260 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4261 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4262 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4263 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4264 is_out ? "out" : "in ",
4265 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
4266 is_irq ? "IRQ " : "",
4267 active_low ? "ACTIVE LOW" : "");
4268 seq_printf(s, "\n");
4272 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4274 unsigned long flags;
4275 struct gpio_device *gdev = NULL;
4276 loff_t index = *pos;
4280 spin_lock_irqsave(&gpio_lock, flags);
4281 list_for_each_entry(gdev, &gpio_devices, list)
4283 spin_unlock_irqrestore(&gpio_lock, flags);
4286 spin_unlock_irqrestore(&gpio_lock, flags);
4291 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4293 unsigned long flags;
4294 struct gpio_device *gdev = v;
4297 spin_lock_irqsave(&gpio_lock, flags);
4298 if (list_is_last(&gdev->list, &gpio_devices))
4301 ret = list_entry(gdev->list.next, struct gpio_device, list);
4302 spin_unlock_irqrestore(&gpio_lock, flags);
4310 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4314 static int gpiolib_seq_show(struct seq_file *s, void *v)
4316 struct gpio_device *gdev = v;
4317 struct gpio_chip *gc = gdev->chip;
4318 struct device *parent;
4321 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4322 dev_name(&gdev->dev));
4326 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4327 dev_name(&gdev->dev),
4328 gdev->base, gdev->base + gdev->ngpio - 1);
4329 parent = gc->parent;
4331 seq_printf(s, ", parent: %s/%s",
4332 parent->bus ? parent->bus->name : "no-bus",
4335 seq_printf(s, ", %s", gc->label);
4337 seq_printf(s, ", can sleep");
4338 seq_printf(s, ":\n");
4341 gc->dbg_show(s, gc);
4343 gpiolib_dbg_show(s, gdev);
4348 static const struct seq_operations gpiolib_sops = {
4349 .start = gpiolib_seq_start,
4350 .next = gpiolib_seq_next,
4351 .stop = gpiolib_seq_stop,
4352 .show = gpiolib_seq_show,
4354 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4356 static int __init gpiolib_debugfs_init(void)
4358 /* /sys/kernel/debug/gpio */
4359 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4362 subsys_initcall(gpiolib_debugfs_init);
4364 #endif /* DEBUG_FS */