1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/acpi.h>
4 #include <linux/bitmap.h>
5 #include <linux/compat.h>
6 #include <linux/debugfs.h>
7 #include <linux/device.h>
9 #include <linux/errno.h>
10 #include <linux/file.h>
12 #include <linux/idr.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/seq_file.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
24 #include <linux/gpio.h>
25 #include <linux/gpio/driver.h>
26 #include <linux/gpio/machine.h>
28 #include <uapi/linux/gpio.h>
30 #include "gpiolib-acpi.h"
31 #include "gpiolib-cdev.h"
32 #include "gpiolib-of.h"
33 #include "gpiolib-swnode.h"
34 #include "gpiolib-sysfs.h"
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/gpio.h>
40 /* Implementation infrastructure for GPIO interfaces.
42 * The GPIO programming interface allows for inlining speed-critical
43 * get/set operations for common cases, so that access to SOC-integrated
44 * GPIOs can sometimes cost only an instruction or two per bit.
48 /* When debugging, extend minimal trust to callers and platform code.
49 * Also emit diagnostic messages that may help initial bringup, when
50 * board setup or driver bugs are most common.
52 * Otherwise, minimize overhead in what may be bitbanging codepaths.
55 #define extra_checks 1
57 #define extra_checks 0
60 /* Device and char device-related information */
61 static DEFINE_IDA(gpio_ida);
62 static dev_t gpio_devt;
63 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
65 static int gpio_bus_match(struct device *dev, struct device_driver *drv)
67 struct fwnode_handle *fwnode = dev_fwnode(dev);
70 * Only match if the fwnode doesn't already have a proper struct device
73 if (fwnode && fwnode->dev != dev)
78 static struct bus_type gpio_bus_type = {
80 .match = gpio_bus_match,
84 * Number of GPIOs to use for the fast path in set array
86 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
88 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
89 * While any GPIO is requested, its gpio_chip is not removable;
90 * each GPIO's "requested" flag serves as a lock and refcount.
92 DEFINE_SPINLOCK(gpio_lock);
94 static DEFINE_MUTEX(gpio_lookup_lock);
95 static LIST_HEAD(gpio_lookup_list);
96 LIST_HEAD(gpio_devices);
98 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
99 static LIST_HEAD(gpio_machine_hogs);
101 static void gpiochip_free_hogs(struct gpio_chip *gc);
102 static int gpiochip_add_irqchip(struct gpio_chip *gc,
103 struct lock_class_key *lock_key,
104 struct lock_class_key *request_key);
105 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
106 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
107 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
108 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
110 static bool gpiolib_initialized;
112 static inline void desc_set_label(struct gpio_desc *d, const char *label)
118 * gpio_to_desc - Convert a GPIO number to its descriptor
119 * @gpio: global GPIO number
122 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
123 * with the given number exists in the system.
125 struct gpio_desc *gpio_to_desc(unsigned gpio)
127 struct gpio_device *gdev;
130 spin_lock_irqsave(&gpio_lock, flags);
132 list_for_each_entry(gdev, &gpio_devices, list) {
133 if (gdev->base <= gpio &&
134 gdev->base + gdev->ngpio > gpio) {
135 spin_unlock_irqrestore(&gpio_lock, flags);
136 return &gdev->descs[gpio - gdev->base];
140 spin_unlock_irqrestore(&gpio_lock, flags);
142 if (!gpio_is_valid(gpio))
143 pr_warn("invalid GPIO %d\n", gpio);
147 EXPORT_SYMBOL_GPL(gpio_to_desc);
150 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
151 * hardware number for this chip
153 * @hwnum: hardware number of the GPIO for this chip
156 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
157 * in the given chip for the specified hardware number.
159 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
162 struct gpio_device *gdev = gc->gpiodev;
164 if (hwnum >= gdev->ngpio)
165 return ERR_PTR(-EINVAL);
167 return &gdev->descs[hwnum];
169 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
172 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
173 * @desc: GPIO descriptor
175 * This should disappear in the future but is needed since we still
176 * use GPIO numbers for error messages and sysfs nodes.
179 * The global GPIO number for the GPIO specified by its descriptor.
181 int desc_to_gpio(const struct gpio_desc *desc)
183 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
185 EXPORT_SYMBOL_GPL(desc_to_gpio);
189 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
190 * @desc: descriptor to return the chip of
192 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
194 if (!desc || !desc->gdev)
196 return desc->gdev->chip;
198 EXPORT_SYMBOL_GPL(gpiod_to_chip);
200 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
201 static int gpiochip_find_base(int ngpio)
203 struct gpio_device *gdev;
204 int base = GPIO_DYNAMIC_BASE;
206 list_for_each_entry(gdev, &gpio_devices, list) {
207 /* found a free space? */
208 if (gdev->base >= base + ngpio)
210 /* nope, check the space right after the chip */
211 base = gdev->base + gdev->ngpio;
212 if (base < GPIO_DYNAMIC_BASE)
213 base = GPIO_DYNAMIC_BASE;
216 if (gpio_is_valid(base)) {
217 pr_debug("%s: found new base at %d\n", __func__, base);
220 pr_err("%s: cannot find free range\n", __func__);
226 * gpiod_get_direction - return the current direction of a GPIO
227 * @desc: GPIO to get the direction of
229 * Returns 0 for output, 1 for input, or an error code in case of error.
231 * This function may sleep if gpiod_cansleep() is true.
233 int gpiod_get_direction(struct gpio_desc *desc)
235 struct gpio_chip *gc;
239 gc = gpiod_to_chip(desc);
240 offset = gpio_chip_hwgpio(desc);
243 * Open drain emulation using input mode may incorrectly report
244 * input here, fix that up.
246 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
247 test_bit(FLAG_IS_OUT, &desc->flags))
250 if (!gc->get_direction)
253 ret = gc->get_direction(gc, offset);
257 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
261 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
265 EXPORT_SYMBOL_GPL(gpiod_get_direction);
268 * Add a new chip to the global chips list, keeping the list of chips sorted
269 * by range(means [base, base + ngpio - 1]) order.
271 * Return -EBUSY if the new chip overlaps with some other chip's integer
274 static int gpiodev_add_to_list(struct gpio_device *gdev)
276 struct gpio_device *prev, *next;
278 if (list_empty(&gpio_devices)) {
279 /* initial entry in list */
280 list_add_tail(&gdev->list, &gpio_devices);
284 next = list_first_entry(&gpio_devices, struct gpio_device, list);
285 if (gdev->base + gdev->ngpio <= next->base) {
286 /* add before first entry */
287 list_add(&gdev->list, &gpio_devices);
291 prev = list_last_entry(&gpio_devices, struct gpio_device, list);
292 if (prev->base + prev->ngpio <= gdev->base) {
293 /* add behind last entry */
294 list_add_tail(&gdev->list, &gpio_devices);
298 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
299 /* at the end of the list */
300 if (&next->list == &gpio_devices)
303 /* add between prev and next */
304 if (prev->base + prev->ngpio <= gdev->base
305 && gdev->base + gdev->ngpio <= next->base) {
306 list_add(&gdev->list, &prev->list);
315 * Convert a GPIO name to its descriptor
316 * Note that there is no guarantee that GPIO names are globally unique!
317 * Hence this function will return, if it exists, a reference to the first GPIO
318 * line found that matches the given name.
320 static struct gpio_desc *gpio_name_to_desc(const char * const name)
322 struct gpio_device *gdev;
328 spin_lock_irqsave(&gpio_lock, flags);
330 list_for_each_entry(gdev, &gpio_devices, list) {
331 struct gpio_desc *desc;
333 for_each_gpio_desc(gdev->chip, desc) {
334 if (desc->name && !strcmp(desc->name, name)) {
335 spin_unlock_irqrestore(&gpio_lock, flags);
341 spin_unlock_irqrestore(&gpio_lock, flags);
347 * Take the names from gc->names and assign them to their GPIO descriptors.
348 * Warn if a name is already used for a GPIO line on a different GPIO chip.
351 * 1. Non-unique names are still accepted,
352 * 2. Name collisions within the same GPIO chip are not reported.
354 static int gpiochip_set_desc_names(struct gpio_chip *gc)
356 struct gpio_device *gdev = gc->gpiodev;
359 /* First check all names if they are unique */
360 for (i = 0; i != gc->ngpio; ++i) {
361 struct gpio_desc *gpio;
363 gpio = gpio_name_to_desc(gc->names[i]);
366 "Detected name collision for GPIO name '%s'\n",
370 /* Then add all names to the GPIO descriptors */
371 for (i = 0; i != gc->ngpio; ++i)
372 gdev->descs[i].name = gc->names[i];
378 * gpiochip_set_names - Set GPIO line names using device properties
379 * @chip: GPIO chip whose lines should be named, if possible
381 * Looks for device property "gpio-line-names" and if it exists assigns
382 * GPIO line names for the chip. The memory allocated for the assigned
383 * names belong to the underlying firmware node and should not be released
386 static int gpiochip_set_names(struct gpio_chip *chip)
388 struct gpio_device *gdev = chip->gpiodev;
389 struct device *dev = &gdev->dev;
394 count = device_property_string_array_count(dev, "gpio-line-names");
399 * When offset is set in the driver side we assume the driver internally
400 * is using more than one gpiochip per the same device. We have to stop
401 * setting friendly names if the specified ones with 'gpio-line-names'
402 * are less than the offset in the device itself. This means all the
403 * lines are not present for every single pin within all the internal
406 if (count <= chip->offset) {
407 dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
408 count, chip->offset);
412 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
416 ret = device_property_read_string_array(dev, "gpio-line-names",
419 dev_warn(dev, "failed to read GPIO line names\n");
425 * When more that one gpiochip per device is used, 'count' can
426 * contain at most number gpiochips x chip->ngpio. We have to
427 * correctly distribute all defined lines taking into account
428 * chip->offset as starting point from where we will assign
429 * the names to pins from the 'names' array. Since property
430 * 'gpio-line-names' cannot contains gaps, we have to be sure
431 * we only assign those pins that really exists since chip->ngpio
432 * can be different of the chip->offset.
434 count = (count > chip->offset) ? count - chip->offset : count;
435 if (count > chip->ngpio)
438 for (i = 0; i < count; i++) {
440 * Allow overriding "fixed" names provided by the GPIO
441 * provider. The "fixed" names are more often than not
442 * generic and less informative than the names given in
445 if (names[chip->offset + i] && names[chip->offset + i][0])
446 gdev->descs[i].name = names[chip->offset + i];
454 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
458 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
462 /* Assume by default all GPIOs are valid */
463 bitmap_fill(p, gc->ngpio);
468 static void gpiochip_free_mask(unsigned long **p)
474 static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
476 struct device *dev = &gc->gpiodev->dev;
479 /* Format is "start, count, ..." */
480 size = device_property_count_u32(dev, "gpio-reserved-ranges");
481 if (size > 0 && size % 2 == 0)
487 static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc)
489 struct device *dev = &gc->gpiodev->dev;
494 size = gpiochip_count_reserved_ranges(gc);
498 ranges = kmalloc_array(size, sizeof(*ranges), GFP_KERNEL);
502 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges",
510 u32 count = ranges[--size];
511 u32 start = ranges[--size];
513 if (start >= gc->ngpio || start + count > gc->ngpio)
516 bitmap_clear(gc->valid_mask, start, count);
523 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
527 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
530 gc->valid_mask = gpiochip_allocate_mask(gc);
534 ret = gpiochip_apply_reserved_ranges(gc);
538 if (gc->init_valid_mask)
539 return gc->init_valid_mask(gc,
546 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
548 gpiochip_free_mask(&gc->valid_mask);
551 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
554 * Device Tree platforms are supposed to use "gpio-ranges"
555 * property. This check ensures that the ->add_pin_ranges()
556 * won't be called for them.
558 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges"))
561 if (gc->add_pin_ranges)
562 return gc->add_pin_ranges(gc);
567 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
570 /* No mask means all valid */
571 if (likely(!gc->valid_mask))
573 return test_bit(offset, gc->valid_mask);
575 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
577 static void gpiodev_release(struct device *dev)
579 struct gpio_device *gdev = to_gpio_device(dev);
582 spin_lock_irqsave(&gpio_lock, flags);
583 list_del(&gdev->list);
584 spin_unlock_irqrestore(&gpio_lock, flags);
586 ida_free(&gpio_ida, gdev->id);
587 kfree_const(gdev->label);
592 #ifdef CONFIG_GPIO_CDEV
593 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
594 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
597 * gpiolib_cdev_register() indirectly calls device_add(), which is still
598 * required even when cdev is not selected.
600 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
601 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
604 static int gpiochip_setup_dev(struct gpio_device *gdev)
606 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
610 * If fwnode doesn't belong to another device, it's safe to clear its
613 if (fwnode && !fwnode->dev)
614 fwnode_dev_initialized(fwnode, false);
616 ret = gcdev_register(gdev, gpio_devt);
620 /* From this point, the .release() function cleans up gpio_device */
621 gdev->dev.release = gpiodev_release;
623 ret = gpiochip_sysfs_register(gdev);
625 goto err_remove_device;
627 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
628 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
633 gcdev_unregister(gdev);
637 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
639 struct gpio_desc *desc;
642 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
644 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
649 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
652 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
654 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
655 __func__, gc->label, hog->chip_hwnum, rv);
658 static void machine_gpiochip_add(struct gpio_chip *gc)
660 struct gpiod_hog *hog;
662 mutex_lock(&gpio_machine_hogs_mutex);
664 list_for_each_entry(hog, &gpio_machine_hogs, list) {
665 if (!strcmp(gc->label, hog->chip_label))
666 gpiochip_machine_hog(gc, hog);
669 mutex_unlock(&gpio_machine_hogs_mutex);
672 static void gpiochip_setup_devs(void)
674 struct gpio_device *gdev;
677 list_for_each_entry(gdev, &gpio_devices, list) {
678 ret = gpiochip_setup_dev(gdev);
681 "Failed to initialize gpio device (%d)\n", ret);
685 static void gpiochip_set_data(struct gpio_chip *gc, void *data)
687 gc->gpiodev->data = data;
691 * gpiochip_get_data() - get per-subdriver data for the chip
695 * The per-subdriver data for the chip.
697 void *gpiochip_get_data(struct gpio_chip *gc)
699 return gc->gpiodev->data;
701 EXPORT_SYMBOL_GPL(gpiochip_get_data);
703 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
704 struct lock_class_key *lock_key,
705 struct lock_class_key *request_key)
707 struct gpio_device *gdev;
715 * If the calling driver did not initialize firmware node, do it here
716 * using the parent device, if any.
718 if (!gc->fwnode && gc->parent)
719 gc->fwnode = dev_fwnode(gc->parent);
722 * First: allocate and populate the internal stat container, and
723 * set up the struct device.
725 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
728 gdev->dev.bus = &gpio_bus_type;
729 gdev->dev.parent = gc->parent;
733 gpiochip_set_data(gc, data);
735 device_set_node(&gdev->dev, gc->fwnode);
737 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
743 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
747 device_initialize(&gdev->dev);
748 if (gc->parent && gc->parent->driver)
749 gdev->owner = gc->parent->driver->owner;
751 /* TODO: remove chip->owner */
752 gdev->owner = gc->owner;
754 gdev->owner = THIS_MODULE;
757 * Try the device properties if the driver didn't supply the number
762 ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
765 * -ENODATA means that there is no property found and
766 * we want to issue the error message to the user.
767 * Besides that, we want to return different error code
768 * to state that supplied value is not valid.
772 goto err_free_dev_name;
777 if (gc->ngpio == 0) {
778 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
780 goto err_free_dev_name;
783 if (gc->ngpio > FASTPATH_NGPIO)
784 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
785 gc->ngpio, FASTPATH_NGPIO);
787 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
790 goto err_free_dev_name;
793 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
799 gdev->ngpio = gc->ngpio;
801 spin_lock_irqsave(&gpio_lock, flags);
804 * TODO: this allocates a Linux GPIO number base in the global
805 * GPIO numberspace for this chip. In the long run we want to
806 * get *rid* of this numberspace and use only descriptors, but
807 * it may be a pipe dream. It will not happen before we get rid
808 * of the sysfs interface anyways.
812 base = gpiochip_find_base(gc->ngpio);
814 spin_unlock_irqrestore(&gpio_lock, flags);
820 * TODO: it should not be necessary to reflect the assigned
821 * base outside of the GPIO subsystem. Go over drivers and
822 * see if anyone makes use of this, else drop this and assign
828 "Static allocation of GPIO base is deprecated, use dynamic allocation.\n");
832 ret = gpiodev_add_to_list(gdev);
834 spin_unlock_irqrestore(&gpio_lock, flags);
835 chip_err(gc, "GPIO integer space overlap, cannot add chip\n");
839 for (i = 0; i < gc->ngpio; i++)
840 gdev->descs[i].gdev = gdev;
842 spin_unlock_irqrestore(&gpio_lock, flags);
844 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
845 init_rwsem(&gdev->sem);
847 #ifdef CONFIG_PINCTRL
848 INIT_LIST_HEAD(&gdev->pin_ranges);
852 ret = gpiochip_set_desc_names(gc);
854 goto err_remove_from_list;
856 ret = gpiochip_set_names(gc);
858 goto err_remove_from_list;
860 ret = gpiochip_init_valid_mask(gc);
862 goto err_remove_from_list;
864 ret = of_gpiochip_add(gc);
866 goto err_free_gpiochip_mask;
868 for (i = 0; i < gc->ngpio; i++) {
869 struct gpio_desc *desc = &gdev->descs[i];
871 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
872 assign_bit(FLAG_IS_OUT,
873 &desc->flags, !gc->get_direction(gc, i));
875 assign_bit(FLAG_IS_OUT,
876 &desc->flags, !gc->direction_input);
880 ret = gpiochip_add_pin_ranges(gc);
882 goto err_remove_of_chip;
884 acpi_gpiochip_add(gc);
886 machine_gpiochip_add(gc);
888 ret = gpiochip_irqchip_init_valid_mask(gc);
890 goto err_remove_acpi_chip;
892 ret = gpiochip_irqchip_init_hw(gc);
894 goto err_remove_acpi_chip;
896 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
898 goto err_remove_irqchip_mask;
901 * By first adding the chardev, and then adding the device,
902 * we get a device node entry in sysfs under
903 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
904 * coldplug of device nodes and other udev business.
905 * We can do this only if gpiolib has been initialized.
906 * Otherwise, defer until later.
908 if (gpiolib_initialized) {
909 ret = gpiochip_setup_dev(gdev);
911 goto err_remove_irqchip;
916 gpiochip_irqchip_remove(gc);
917 err_remove_irqchip_mask:
918 gpiochip_irqchip_free_valid_mask(gc);
919 err_remove_acpi_chip:
920 acpi_gpiochip_remove(gc);
922 gpiochip_free_hogs(gc);
923 of_gpiochip_remove(gc);
924 err_free_gpiochip_mask:
925 gpiochip_remove_pin_ranges(gc);
926 gpiochip_free_valid_mask(gc);
927 if (gdev->dev.release) {
928 /* release() has been registered by gpiochip_setup_dev() */
929 gpio_device_put(gdev);
930 goto err_print_message;
932 err_remove_from_list:
933 spin_lock_irqsave(&gpio_lock, flags);
934 list_del(&gdev->list);
935 spin_unlock_irqrestore(&gpio_lock, flags);
937 kfree_const(gdev->label);
941 kfree(dev_name(&gdev->dev));
943 ida_free(&gpio_ida, gdev->id);
947 /* failures here can mean systems won't boot... */
948 if (ret != -EPROBE_DEFER) {
949 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
950 base, base + (int)ngpios - 1,
951 gc->label ? : "generic", ret);
955 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
958 * gpiochip_remove() - unregister a gpio_chip
959 * @gc: the chip to unregister
961 * A gpio_chip with any GPIOs still requested may not be removed.
963 void gpiochip_remove(struct gpio_chip *gc)
965 struct gpio_device *gdev = gc->gpiodev;
969 down_write(&gdev->sem);
971 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
972 gpiochip_sysfs_unregister(gdev);
973 gpiochip_free_hogs(gc);
974 /* Numb the device, cancelling all outstanding operations */
976 gpiochip_irqchip_remove(gc);
977 acpi_gpiochip_remove(gc);
978 of_gpiochip_remove(gc);
979 gpiochip_remove_pin_ranges(gc);
980 gpiochip_free_valid_mask(gc);
982 * We accept no more calls into the driver from this point, so
983 * NULL the driver data pointer.
985 gpiochip_set_data(gc, NULL);
987 spin_lock_irqsave(&gpio_lock, flags);
988 for (i = 0; i < gdev->ngpio; i++) {
989 if (gpiochip_is_requested(gc, i))
992 spin_unlock_irqrestore(&gpio_lock, flags);
994 if (i != gdev->ngpio)
996 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
999 * The gpiochip side puts its use of the device to rest here:
1000 * if there are no userspace clients, the chardev and device will
1001 * be removed, else it will be dangling until the last user is
1004 gcdev_unregister(gdev);
1005 up_write(&gdev->sem);
1006 gpio_device_put(gdev);
1008 EXPORT_SYMBOL_GPL(gpiochip_remove);
1011 * gpiochip_find() - iterator for locating a specific gpio_chip
1012 * @data: data to pass to match function
1013 * @match: Callback function to check gpio_chip
1015 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1016 * determined by a user supplied @match callback. The callback should return
1017 * 0 if the device doesn't match and non-zero if it does. If the callback is
1018 * non-zero, this function will return to the caller and not iterate over any
1021 struct gpio_chip *gpiochip_find(void *data,
1022 int (*match)(struct gpio_chip *gc,
1025 struct gpio_device *gdev;
1026 struct gpio_chip *gc = NULL;
1027 unsigned long flags;
1029 spin_lock_irqsave(&gpio_lock, flags);
1030 list_for_each_entry(gdev, &gpio_devices, list)
1031 if (gdev->chip && match(gdev->chip, data)) {
1036 spin_unlock_irqrestore(&gpio_lock, flags);
1040 EXPORT_SYMBOL_GPL(gpiochip_find);
1042 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
1044 const char *name = data;
1046 return !strcmp(gc->label, name);
1049 static struct gpio_chip *find_chip_by_name(const char *name)
1051 return gpiochip_find((void *)name, gpiochip_match_name);
1054 #ifdef CONFIG_GPIOLIB_IRQCHIP
1057 * The following is irqchip helper code for gpiochips.
1060 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1062 struct gpio_irq_chip *girq = &gc->irq;
1067 return girq->init_hw(gc);
1070 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1072 struct gpio_irq_chip *girq = &gc->irq;
1074 if (!girq->init_valid_mask)
1077 girq->valid_mask = gpiochip_allocate_mask(gc);
1078 if (!girq->valid_mask)
1081 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1086 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1088 gpiochip_free_mask(&gc->irq.valid_mask);
1091 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1092 unsigned int offset)
1094 if (!gpiochip_line_is_valid(gc, offset))
1096 /* No mask means all valid */
1097 if (likely(!gc->irq.valid_mask))
1099 return test_bit(offset, gc->irq.valid_mask);
1101 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1103 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1106 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1108 * @gc: the gpiochip to set the irqchip hierarchical handler to
1109 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1110 * will then percolate up to the parent
1112 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1113 struct irq_chip *irqchip)
1115 /* DT will deal with mapping each IRQ as we go along */
1116 if (is_of_node(gc->irq.fwnode))
1120 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1121 * irqs upfront instead of dynamically since we don't have the
1122 * dynamic type of allocation that hardware description languages
1123 * provide. Once all GPIO drivers using board files are gone from
1124 * the kernel we can delete this code, but for a transitional period
1125 * it is necessary to keep this around.
1127 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1131 for (i = 0; i < gc->ngpio; i++) {
1132 struct irq_fwspec fwspec;
1133 unsigned int parent_hwirq;
1134 unsigned int parent_type;
1135 struct gpio_irq_chip *girq = &gc->irq;
1138 * We call the child to parent translation function
1139 * only to check if the child IRQ is valid or not.
1140 * Just pick the rising edge type here as that is what
1141 * we likely need to support.
1143 ret = girq->child_to_parent_hwirq(gc, i,
1144 IRQ_TYPE_EDGE_RISING,
1148 chip_err(gc, "skip set-up on hwirq %d\n",
1153 fwspec.fwnode = gc->irq.fwnode;
1154 /* This is the hwirq for the GPIO line side of things */
1155 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1156 /* Just pick something */
1157 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1158 fwspec.param_count = 2;
1159 ret = irq_domain_alloc_irqs(gc->irq.domain, 1,
1160 NUMA_NO_NODE, &fwspec);
1163 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1170 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1175 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1176 struct irq_fwspec *fwspec,
1177 unsigned long *hwirq,
1180 /* We support standard DT translation */
1181 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1182 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1185 /* This is for board files and others not using DT */
1186 if (is_fwnode_irqchip(fwspec->fwnode)) {
1189 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1192 WARN_ON(*type == IRQ_TYPE_NONE);
1198 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1200 unsigned int nr_irqs,
1203 struct gpio_chip *gc = d->host_data;
1204 irq_hw_number_t hwirq;
1205 unsigned int type = IRQ_TYPE_NONE;
1206 struct irq_fwspec *fwspec = data;
1207 union gpio_irq_fwspec gpio_parent_fwspec = {};
1208 unsigned int parent_hwirq;
1209 unsigned int parent_type;
1210 struct gpio_irq_chip *girq = &gc->irq;
1214 * The nr_irqs parameter is always one except for PCI multi-MSI
1215 * so this should not happen.
1217 WARN_ON(nr_irqs != 1);
1219 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1223 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1225 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1226 &parent_hwirq, &parent_type);
1228 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1231 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1234 * We set handle_bad_irq because the .set_type() should
1235 * always be invoked and set the right type of handler.
1237 irq_domain_set_info(d,
1246 /* This parent only handles asserted level IRQs */
1247 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
1248 parent_hwirq, parent_type);
1252 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1254 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1255 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
1257 * If the parent irqdomain is msi, the interrupts have already
1258 * been allocated, so the EEXIST is good.
1260 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1264 "failed to allocate parent hwirq %d for hwirq %lu\n",
1265 parent_hwirq, hwirq);
1270 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1271 unsigned int offset)
1276 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1278 ops->activate = gpiochip_irq_domain_activate;
1279 ops->deactivate = gpiochip_irq_domain_deactivate;
1280 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1283 * We only allow overriding the translate() and free() functions for
1284 * hierarchical chips, and this should only be done if the user
1285 * really need something other than 1:1 translation for translate()
1286 * callback and free if user wants to free up any resources which
1287 * were allocated during callbacks, for example populate_parent_alloc_arg.
1289 if (!ops->translate)
1290 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1292 ops->free = irq_domain_free_irqs_common;
1295 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1297 if (!gc->irq.child_to_parent_hwirq ||
1299 chip_err(gc, "missing irqdomain vital data\n");
1303 if (!gc->irq.child_offset_to_irq)
1304 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1306 if (!gc->irq.populate_parent_alloc_arg)
1307 gc->irq.populate_parent_alloc_arg =
1308 gpiochip_populate_parent_fwspec_twocell;
1310 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1312 gc->irq.domain = irq_domain_create_hierarchy(
1313 gc->irq.parent_domain,
1317 &gc->irq.child_irq_domain_ops,
1320 if (!gc->irq.domain)
1323 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1328 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1330 return !!gc->irq.parent_domain;
1333 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1334 union gpio_irq_fwspec *gfwspec,
1335 unsigned int parent_hwirq,
1336 unsigned int parent_type)
1338 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1340 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1341 fwspec->param_count = 2;
1342 fwspec->param[0] = parent_hwirq;
1343 fwspec->param[1] = parent_type;
1347 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1349 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1350 union gpio_irq_fwspec *gfwspec,
1351 unsigned int parent_hwirq,
1352 unsigned int parent_type)
1354 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1356 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1357 fwspec->param_count = 4;
1358 fwspec->param[0] = 0;
1359 fwspec->param[1] = parent_hwirq;
1360 fwspec->param[2] = 0;
1361 fwspec->param[3] = parent_type;
1365 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1369 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1374 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1379 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1382 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1383 * @d: the irqdomain used by this irqchip
1384 * @irq: the global irq number used by this GPIO irqchip irq
1385 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1387 * This function will set up the mapping for a certain IRQ line on a
1388 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1389 * stored inside the gpiochip.
1391 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hwirq)
1393 struct gpio_chip *gc = d->host_data;
1396 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1399 irq_set_chip_data(irq, gc);
1401 * This lock class tells lockdep that GPIO irqs are in a different
1402 * category than their parents, so it won't report false recursion.
1404 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1405 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1406 /* Chips that use nested thread handlers have them marked */
1407 if (gc->irq.threaded)
1408 irq_set_nested_thread(irq, 1);
1409 irq_set_noprobe(irq);
1411 if (gc->irq.num_parents == 1)
1412 ret = irq_set_parent(irq, gc->irq.parents[0]);
1413 else if (gc->irq.map)
1414 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1420 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1421 * is passed as default type.
1423 if (gc->irq.default_type != IRQ_TYPE_NONE)
1424 irq_set_irq_type(irq, gc->irq.default_type);
1428 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1430 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1432 struct gpio_chip *gc = d->host_data;
1434 if (gc->irq.threaded)
1435 irq_set_nested_thread(irq, 0);
1436 irq_set_chip_and_handler(irq, NULL, NULL);
1437 irq_set_chip_data(irq, NULL);
1439 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1441 static const struct irq_domain_ops gpiochip_domain_ops = {
1442 .map = gpiochip_irq_map,
1443 .unmap = gpiochip_irq_unmap,
1444 /* Virtually all GPIO irqchips are twocell:ed */
1445 .xlate = irq_domain_xlate_twocell,
1449 * TODO: move these activate/deactivate in under the hierarchicial
1450 * irqchip implementation as static once SPMI and SSBI (all external
1451 * users) are phased over.
1454 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1455 * @domain: The IRQ domain used by this IRQ chip
1456 * @data: Outermost irq_data associated with the IRQ
1457 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1459 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1460 * used as the activate function for the &struct irq_domain_ops. The host_data
1461 * for the IRQ domain must be the &struct gpio_chip.
1463 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1464 struct irq_data *data, bool reserve)
1466 struct gpio_chip *gc = domain->host_data;
1467 unsigned int hwirq = irqd_to_hwirq(data);
1469 return gpiochip_lock_as_irq(gc, hwirq);
1471 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1474 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1475 * @domain: The IRQ domain used by this IRQ chip
1476 * @data: Outermost irq_data associated with the IRQ
1478 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1479 * be used as the deactivate function for the &struct irq_domain_ops. The
1480 * host_data for the IRQ domain must be the &struct gpio_chip.
1482 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1483 struct irq_data *data)
1485 struct gpio_chip *gc = domain->host_data;
1486 unsigned int hwirq = irqd_to_hwirq(data);
1488 return gpiochip_unlock_as_irq(gc, hwirq);
1490 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1492 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1494 struct irq_domain *domain = gc->irq.domain;
1496 #ifdef CONFIG_GPIOLIB_IRQCHIP
1498 * Avoid race condition with other code, which tries to lookup
1499 * an IRQ before the irqchip has been properly registered,
1500 * i.e. while gpiochip is still being brought up.
1502 if (!gc->irq.initialized)
1503 return -EPROBE_DEFER;
1506 if (!gpiochip_irqchip_irq_valid(gc, offset))
1509 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1510 if (irq_domain_is_hierarchy(domain)) {
1511 struct irq_fwspec spec;
1513 spec.fwnode = domain->fwnode;
1514 spec.param_count = 2;
1515 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1516 spec.param[1] = IRQ_TYPE_NONE;
1518 return irq_create_fwspec_mapping(&spec);
1522 return irq_create_mapping(domain, offset);
1525 int gpiochip_irq_reqres(struct irq_data *d)
1527 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1528 unsigned int hwirq = irqd_to_hwirq(d);
1530 return gpiochip_reqres_irq(gc, hwirq);
1532 EXPORT_SYMBOL(gpiochip_irq_reqres);
1534 void gpiochip_irq_relres(struct irq_data *d)
1536 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1537 unsigned int hwirq = irqd_to_hwirq(d);
1539 gpiochip_relres_irq(gc, hwirq);
1541 EXPORT_SYMBOL(gpiochip_irq_relres);
1543 static void gpiochip_irq_mask(struct irq_data *d)
1545 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1546 unsigned int hwirq = irqd_to_hwirq(d);
1548 if (gc->irq.irq_mask)
1549 gc->irq.irq_mask(d);
1550 gpiochip_disable_irq(gc, hwirq);
1553 static void gpiochip_irq_unmask(struct irq_data *d)
1555 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1556 unsigned int hwirq = irqd_to_hwirq(d);
1558 gpiochip_enable_irq(gc, hwirq);
1559 if (gc->irq.irq_unmask)
1560 gc->irq.irq_unmask(d);
1563 static void gpiochip_irq_enable(struct irq_data *d)
1565 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1566 unsigned int hwirq = irqd_to_hwirq(d);
1568 gpiochip_enable_irq(gc, hwirq);
1569 gc->irq.irq_enable(d);
1572 static void gpiochip_irq_disable(struct irq_data *d)
1574 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1575 unsigned int hwirq = irqd_to_hwirq(d);
1577 gc->irq.irq_disable(d);
1578 gpiochip_disable_irq(gc, hwirq);
1581 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1583 struct irq_chip *irqchip = gc->irq.chip;
1585 if (irqchip->flags & IRQCHIP_IMMUTABLE)
1588 chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
1590 if (!irqchip->irq_request_resources &&
1591 !irqchip->irq_release_resources) {
1592 irqchip->irq_request_resources = gpiochip_irq_reqres;
1593 irqchip->irq_release_resources = gpiochip_irq_relres;
1595 if (WARN_ON(gc->irq.irq_enable))
1597 /* Check if the irqchip already has this hook... */
1598 if (irqchip->irq_enable == gpiochip_irq_enable ||
1599 irqchip->irq_mask == gpiochip_irq_mask) {
1601 * ...and if so, give a gentle warning that this is bad
1605 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1609 if (irqchip->irq_disable) {
1610 gc->irq.irq_disable = irqchip->irq_disable;
1611 irqchip->irq_disable = gpiochip_irq_disable;
1613 gc->irq.irq_mask = irqchip->irq_mask;
1614 irqchip->irq_mask = gpiochip_irq_mask;
1617 if (irqchip->irq_enable) {
1618 gc->irq.irq_enable = irqchip->irq_enable;
1619 irqchip->irq_enable = gpiochip_irq_enable;
1621 gc->irq.irq_unmask = irqchip->irq_unmask;
1622 irqchip->irq_unmask = gpiochip_irq_unmask;
1627 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1628 * @gc: the GPIO chip to add the IRQ chip to
1629 * @lock_key: lockdep class for IRQ lock
1630 * @request_key: lockdep class for IRQ request
1632 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1633 struct lock_class_key *lock_key,
1634 struct lock_class_key *request_key)
1636 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1637 struct irq_chip *irqchip = gc->irq.chip;
1644 if (gc->irq.parent_handler && gc->can_sleep) {
1645 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1649 type = gc->irq.default_type;
1652 * Specifying a default trigger is a terrible idea if DT or ACPI is
1653 * used to configure the interrupts, as you may end up with
1654 * conflicting triggers. Tell the user, and reset to NONE.
1656 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1657 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1658 type = IRQ_TYPE_NONE;
1661 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1663 gc->to_irq = gpiochip_to_irq;
1664 gc->irq.default_type = type;
1665 gc->irq.lock_key = lock_key;
1666 gc->irq.request_key = request_key;
1668 /* If a parent irqdomain is provided, let's build a hierarchy */
1669 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1670 int ret = gpiochip_hierarchy_add_domain(gc);
1674 gc->irq.domain = irq_domain_create_simple(fwnode,
1677 &gpiochip_domain_ops,
1679 if (!gc->irq.domain)
1683 if (gc->irq.parent_handler) {
1684 for (i = 0; i < gc->irq.num_parents; i++) {
1687 if (gc->irq.per_parent_data)
1688 data = gc->irq.parent_handler_data_array[i];
1690 data = gc->irq.parent_handler_data ?: gc;
1693 * The parent IRQ chip is already using the chip_data
1694 * for this IRQ chip, so our callbacks simply use the
1697 irq_set_chained_handler_and_data(gc->irq.parents[i],
1698 gc->irq.parent_handler,
1703 gpiochip_set_irq_hooks(gc);
1706 * Using barrier() here to prevent compiler from reordering
1707 * gc->irq.initialized before initialization of above
1708 * GPIO chip irq members.
1712 gc->irq.initialized = true;
1714 acpi_gpiochip_request_interrupts(gc);
1720 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1721 * @gc: the gpiochip to remove the irqchip from
1723 * This is called only from gpiochip_remove()
1725 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1727 struct irq_chip *irqchip = gc->irq.chip;
1728 unsigned int offset;
1730 acpi_gpiochip_free_interrupts(gc);
1732 if (irqchip && gc->irq.parent_handler) {
1733 struct gpio_irq_chip *irq = &gc->irq;
1736 for (i = 0; i < irq->num_parents; i++)
1737 irq_set_chained_handler_and_data(irq->parents[i],
1741 /* Remove all IRQ mappings and delete the domain */
1742 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) {
1745 for (offset = 0; offset < gc->ngpio; offset++) {
1746 if (!gpiochip_irqchip_irq_valid(gc, offset))
1749 irq = irq_find_mapping(gc->irq.domain, offset);
1750 irq_dispose_mapping(irq);
1753 irq_domain_remove(gc->irq.domain);
1756 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
1757 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1758 irqchip->irq_request_resources = NULL;
1759 irqchip->irq_release_resources = NULL;
1761 if (irqchip->irq_enable == gpiochip_irq_enable) {
1762 irqchip->irq_enable = gc->irq.irq_enable;
1763 irqchip->irq_disable = gc->irq.irq_disable;
1766 gc->irq.irq_enable = NULL;
1767 gc->irq.irq_disable = NULL;
1768 gc->irq.chip = NULL;
1770 gpiochip_irqchip_free_valid_mask(gc);
1774 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1775 * @gc: the gpiochip to add the irqchip to
1776 * @domain: the irqdomain to add to the gpiochip
1778 * This function adds an IRQ domain to the gpiochip.
1780 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1781 struct irq_domain *domain)
1786 gc->to_irq = gpiochip_to_irq;
1787 gc->irq.domain = domain;
1788 gc->irq.domain_is_allocated_externally = true;
1791 * Using barrier() here to prevent compiler from reordering
1792 * gc->irq.initialized before adding irqdomain.
1796 gc->irq.initialized = true;
1800 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1802 #else /* CONFIG_GPIOLIB_IRQCHIP */
1804 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1805 struct lock_class_key *lock_key,
1806 struct lock_class_key *request_key)
1810 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1812 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1817 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1821 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1824 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1827 * gpiochip_generic_request() - request the gpio function for a pin
1828 * @gc: the gpiochip owning the GPIO
1829 * @offset: the offset of the GPIO to request for GPIO function
1831 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
1833 #ifdef CONFIG_PINCTRL
1834 if (list_empty(&gc->gpiodev->pin_ranges))
1838 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1840 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1843 * gpiochip_generic_free() - free the gpio function from a pin
1844 * @gc: the gpiochip to request the gpio function for
1845 * @offset: the offset of the GPIO to free from GPIO function
1847 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
1849 #ifdef CONFIG_PINCTRL
1850 if (list_empty(&gc->gpiodev->pin_ranges))
1854 pinctrl_gpio_free(gc->gpiodev->base + offset);
1856 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1859 * gpiochip_generic_config() - apply configuration for a pin
1860 * @gc: the gpiochip owning the GPIO
1861 * @offset: the offset of the GPIO to apply the configuration
1862 * @config: the configuration to be applied
1864 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
1865 unsigned long config)
1867 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1869 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1871 #ifdef CONFIG_PINCTRL
1874 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1875 * @gc: the gpiochip to add the range for
1876 * @pctldev: the pin controller to map to
1877 * @gpio_offset: the start offset in the current gpio_chip number space
1878 * @pin_group: name of the pin group inside the pin controller
1880 * Calling this function directly from a DeviceTree-supported
1881 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1882 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1883 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1885 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1886 struct pinctrl_dev *pctldev,
1887 unsigned int gpio_offset, const char *pin_group)
1889 struct gpio_pin_range *pin_range;
1890 struct gpio_device *gdev = gc->gpiodev;
1893 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1895 chip_err(gc, "failed to allocate pin ranges\n");
1899 /* Use local offset as range ID */
1900 pin_range->range.id = gpio_offset;
1901 pin_range->range.gc = gc;
1902 pin_range->range.name = gc->label;
1903 pin_range->range.base = gdev->base + gpio_offset;
1904 pin_range->pctldev = pctldev;
1906 ret = pinctrl_get_group_pins(pctldev, pin_group,
1907 &pin_range->range.pins,
1908 &pin_range->range.npins);
1914 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1916 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1917 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1918 pinctrl_dev_get_devname(pctldev), pin_group);
1920 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1924 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1927 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1928 * @gc: the gpiochip to add the range for
1929 * @pinctl_name: the dev_name() of the pin controller to map to
1930 * @gpio_offset: the start offset in the current gpio_chip number space
1931 * @pin_offset: the start offset in the pin controller number space
1932 * @npins: the number of pins from the offset of each pin space (GPIO and
1933 * pin controller) to accumulate in this range
1936 * 0 on success, or a negative error-code on failure.
1938 * Calling this function directly from a DeviceTree-supported
1939 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1940 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1941 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1943 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1944 unsigned int gpio_offset, unsigned int pin_offset,
1947 struct gpio_pin_range *pin_range;
1948 struct gpio_device *gdev = gc->gpiodev;
1951 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1953 chip_err(gc, "failed to allocate pin ranges\n");
1957 /* Use local offset as range ID */
1958 pin_range->range.id = gpio_offset;
1959 pin_range->range.gc = gc;
1960 pin_range->range.name = gc->label;
1961 pin_range->range.base = gdev->base + gpio_offset;
1962 pin_range->range.pin_base = pin_offset;
1963 pin_range->range.npins = npins;
1964 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1966 if (IS_ERR(pin_range->pctldev)) {
1967 ret = PTR_ERR(pin_range->pctldev);
1968 chip_err(gc, "could not create pin range\n");
1972 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1973 gpio_offset, gpio_offset + npins - 1,
1975 pin_offset, pin_offset + npins - 1);
1977 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1981 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1984 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1985 * @gc: the chip to remove all the mappings for
1987 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1989 struct gpio_pin_range *pin_range, *tmp;
1990 struct gpio_device *gdev = gc->gpiodev;
1992 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1993 list_del(&pin_range->node);
1994 pinctrl_remove_gpio_range(pin_range->pctldev,
1999 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2001 #endif /* CONFIG_PINCTRL */
2003 /* These "optional" allocation calls help prevent drivers from stomping
2004 * on each other, and help provide better diagnostics in debugfs.
2005 * They're called even less than the "set direction" calls.
2007 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2009 struct gpio_chip *gc = desc->gdev->chip;
2011 unsigned long flags;
2015 label = kstrdup_const(label, GFP_KERNEL);
2020 spin_lock_irqsave(&gpio_lock, flags);
2022 /* NOTE: gpio_request() can be called in early boot,
2023 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2026 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2027 desc_set_label(desc, label ? : "?");
2030 goto out_free_unlock;
2034 /* gc->request may sleep */
2035 spin_unlock_irqrestore(&gpio_lock, flags);
2036 offset = gpio_chip_hwgpio(desc);
2037 if (gpiochip_line_is_valid(gc, offset))
2038 ret = gc->request(gc, offset);
2041 spin_lock_irqsave(&gpio_lock, flags);
2044 desc_set_label(desc, NULL);
2045 clear_bit(FLAG_REQUESTED, &desc->flags);
2046 goto out_free_unlock;
2049 if (gc->get_direction) {
2050 /* gc->get_direction may sleep */
2051 spin_unlock_irqrestore(&gpio_lock, flags);
2052 gpiod_get_direction(desc);
2053 spin_lock_irqsave(&gpio_lock, flags);
2055 spin_unlock_irqrestore(&gpio_lock, flags);
2059 spin_unlock_irqrestore(&gpio_lock, flags);
2065 * This descriptor validation needs to be inserted verbatim into each
2066 * function taking a descriptor, so we need to use a preprocessor
2067 * macro to avoid endless duplication. If the desc is NULL it is an
2068 * optional GPIO and calls should just bail out.
2070 static int validate_desc(const struct gpio_desc *desc, const char *func)
2075 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2076 return PTR_ERR(desc);
2079 pr_warn("%s: invalid GPIO (no device)\n", func);
2082 if (!desc->gdev->chip) {
2083 dev_warn(&desc->gdev->dev,
2084 "%s: backing chip is gone\n", func);
2090 #define VALIDATE_DESC(desc) do { \
2091 int __valid = validate_desc(desc, __func__); \
2096 #define VALIDATE_DESC_VOID(desc) do { \
2097 int __valid = validate_desc(desc, __func__); \
2102 int gpiod_request(struct gpio_desc *desc, const char *label)
2104 int ret = -EPROBE_DEFER;
2106 VALIDATE_DESC(desc);
2108 if (try_module_get(desc->gdev->owner)) {
2109 ret = gpiod_request_commit(desc, label);
2111 module_put(desc->gdev->owner);
2113 gpio_device_get(desc->gdev);
2117 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2122 static bool gpiod_free_commit(struct gpio_desc *desc)
2125 unsigned long flags;
2126 struct gpio_chip *gc;
2130 spin_lock_irqsave(&gpio_lock, flags);
2132 gc = desc->gdev->chip;
2133 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2135 spin_unlock_irqrestore(&gpio_lock, flags);
2136 might_sleep_if(gc->can_sleep);
2137 gc->free(gc, gpio_chip_hwgpio(desc));
2138 spin_lock_irqsave(&gpio_lock, flags);
2140 kfree_const(desc->label);
2141 desc_set_label(desc, NULL);
2142 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2143 clear_bit(FLAG_REQUESTED, &desc->flags);
2144 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2145 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2146 clear_bit(FLAG_PULL_UP, &desc->flags);
2147 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2148 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2149 clear_bit(FLAG_EDGE_RISING, &desc->flags);
2150 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
2151 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2152 #ifdef CONFIG_OF_DYNAMIC
2155 #ifdef CONFIG_GPIO_CDEV
2156 WRITE_ONCE(desc->debounce_period_us, 0);
2161 spin_unlock_irqrestore(&gpio_lock, flags);
2162 blocking_notifier_call_chain(&desc->gdev->notifier,
2163 GPIOLINE_CHANGED_RELEASED, desc);
2168 void gpiod_free(struct gpio_desc *desc)
2170 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2171 module_put(desc->gdev->owner);
2172 gpio_device_put(desc->gdev);
2174 WARN_ON(extra_checks);
2179 * gpiochip_is_requested - return string iff signal was requested
2180 * @gc: controller managing the signal
2181 * @offset: of signal within controller's 0..(ngpio - 1) range
2183 * Returns NULL if the GPIO is not currently requested, else a string.
2184 * The string returned is the label passed to gpio_request(); if none has been
2185 * passed it is a meaningless, non-NULL constant.
2187 * This function is for use by GPIO controller drivers. The label can
2188 * help with diagnostics, and knowing that the signal is used as a GPIO
2189 * can help avoid accidentally multiplexing it to another controller.
2191 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
2193 struct gpio_desc *desc;
2195 desc = gpiochip_get_desc(gc, offset);
2199 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2203 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2206 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2208 * @hwnum: hardware number of the GPIO for which to request the descriptor
2209 * @label: label for the GPIO
2210 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2211 * specify things like line inversion semantics with the machine flags
2212 * such as GPIO_OUT_LOW
2213 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2214 * can be used to specify consumer semantics such as open drain
2216 * Function allows GPIO chip drivers to request and use their own GPIO
2217 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2218 * function will not increase reference count of the GPIO chip module. This
2219 * allows the GPIO chip module to be unloaded as needed (we assume that the
2220 * GPIO chip driver handles freeing the GPIOs it has requested).
2223 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2226 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2229 enum gpio_lookup_flags lflags,
2230 enum gpiod_flags dflags)
2232 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2236 chip_err(gc, "failed to get GPIO descriptor\n");
2240 ret = gpiod_request_commit(desc, label);
2242 return ERR_PTR(ret);
2244 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2246 chip_err(gc, "setup of own GPIO %s failed\n", label);
2247 gpiod_free_commit(desc);
2248 return ERR_PTR(ret);
2253 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2256 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2257 * @desc: GPIO descriptor to free
2259 * Function frees the given GPIO requested previously with
2260 * gpiochip_request_own_desc().
2262 void gpiochip_free_own_desc(struct gpio_desc *desc)
2265 gpiod_free_commit(desc);
2267 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2270 * Drivers MUST set GPIO direction before making get/set calls. In
2271 * some cases this is done in early boot, before IRQs are enabled.
2273 * As a rule these aren't called more than once (except for drivers
2274 * using the open-drain emulation idiom) so these are natural places
2275 * to accumulate extra debugging checks. Note that we can't (yet)
2276 * rely on gpio_request() having been called beforehand.
2279 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2280 unsigned long config)
2282 if (!gc->set_config)
2285 return gc->set_config(gc, offset, config);
2288 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2289 enum pin_config_param mode,
2292 struct gpio_chip *gc = desc->gdev->chip;
2293 unsigned long config;
2295 config = pinconf_to_config_packed(mode, argument);
2296 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2299 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2300 enum pin_config_param mode,
2303 struct device *dev = &desc->gdev->dev;
2304 int gpio = gpio_chip_hwgpio(desc);
2307 ret = gpio_set_config_with_argument(desc, mode, argument);
2308 if (ret != -ENOTSUPP)
2312 case PIN_CONFIG_PERSIST_STATE:
2313 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2322 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2324 return gpio_set_config_with_argument(desc, mode, 0);
2327 static int gpio_set_bias(struct gpio_desc *desc)
2329 enum pin_config_param bias;
2332 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2333 bias = PIN_CONFIG_BIAS_DISABLE;
2334 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2335 bias = PIN_CONFIG_BIAS_PULL_UP;
2336 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2337 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2342 case PIN_CONFIG_BIAS_PULL_DOWN:
2343 case PIN_CONFIG_BIAS_PULL_UP:
2352 return gpio_set_config_with_argument_optional(desc, bias, arg);
2356 * gpio_set_debounce_timeout() - Set debounce timeout
2357 * @desc: GPIO descriptor to set the debounce timeout
2358 * @debounce: Debounce timeout in microseconds
2360 * The function calls the certain GPIO driver to set debounce timeout
2363 * Returns 0 on success, or negative error code otherwise.
2365 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2367 return gpio_set_config_with_argument_optional(desc,
2368 PIN_CONFIG_INPUT_DEBOUNCE,
2373 * gpiod_direction_input - set the GPIO direction to input
2374 * @desc: GPIO to set to input
2376 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2377 * be called safely on it.
2379 * Return 0 in case of success, else an error code.
2381 int gpiod_direction_input(struct gpio_desc *desc)
2383 struct gpio_chip *gc;
2386 VALIDATE_DESC(desc);
2387 gc = desc->gdev->chip;
2390 * It is legal to have no .get() and .direction_input() specified if
2391 * the chip is output-only, but you can't specify .direction_input()
2392 * and not support the .get() operation, that doesn't make sense.
2394 if (!gc->get && gc->direction_input) {
2396 "%s: missing get() but have direction_input()\n",
2402 * If we have a .direction_input() callback, things are simple,
2403 * just call it. Else we are some input-only chip so try to check the
2404 * direction (if .get_direction() is supported) else we silently
2405 * assume we are in input mode after this.
2407 if (gc->direction_input) {
2408 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2409 } else if (gc->get_direction &&
2410 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2412 "%s: missing direction_input() operation and line is output\n",
2417 clear_bit(FLAG_IS_OUT, &desc->flags);
2418 ret = gpio_set_bias(desc);
2421 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2425 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2427 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2429 struct gpio_chip *gc = desc->gdev->chip;
2434 * It's OK not to specify .direction_output() if the gpiochip is
2435 * output-only, but if there is then not even a .set() operation it
2436 * is pretty tricky to drive the output line.
2438 if (!gc->set && !gc->direction_output) {
2440 "%s: missing set() and direction_output() operations\n",
2445 if (gc->direction_output) {
2446 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2448 /* Check that we are in output mode if we can */
2449 if (gc->get_direction &&
2450 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2452 "%s: missing direction_output() operation\n",
2457 * If we can't actively set the direction, we are some
2458 * output-only chip, so just drive the output as desired.
2460 gc->set(gc, gpio_chip_hwgpio(desc), val);
2464 set_bit(FLAG_IS_OUT, &desc->flags);
2465 trace_gpio_value(desc_to_gpio(desc), 0, val);
2466 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2471 * gpiod_direction_output_raw - set the GPIO direction to output
2472 * @desc: GPIO to set to output
2473 * @value: initial output value of the GPIO
2475 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2476 * be called safely on it. The initial value of the output must be specified
2477 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2479 * Return 0 in case of success, else an error code.
2481 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2483 VALIDATE_DESC(desc);
2484 return gpiod_direction_output_raw_commit(desc, value);
2486 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2489 * gpiod_direction_output - set the GPIO direction to output
2490 * @desc: GPIO to set to output
2491 * @value: initial output value of the GPIO
2493 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2494 * be called safely on it. The initial value of the output must be specified
2495 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2498 * Return 0 in case of success, else an error code.
2500 int gpiod_direction_output(struct gpio_desc *desc, int value)
2504 VALIDATE_DESC(desc);
2505 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2510 /* GPIOs used for enabled IRQs shall not be set as output */
2511 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2512 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2514 "%s: tried to set a GPIO tied to an IRQ as output\n",
2519 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2520 /* First see if we can enable open drain in hardware */
2521 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2523 goto set_output_value;
2524 /* Emulate open drain by not actively driving the line high */
2526 ret = gpiod_direction_input(desc);
2527 goto set_output_flag;
2529 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2530 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2532 goto set_output_value;
2533 /* Emulate open source by not actively driving the line low */
2535 ret = gpiod_direction_input(desc);
2536 goto set_output_flag;
2539 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2543 ret = gpio_set_bias(desc);
2546 return gpiod_direction_output_raw_commit(desc, value);
2550 * When emulating open-source or open-drain functionalities by not
2551 * actively driving the line (setting mode to input) we still need to
2552 * set the IS_OUT flag or otherwise we won't be able to set the line
2556 set_bit(FLAG_IS_OUT, &desc->flags);
2559 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2562 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2564 * @desc: GPIO to enable.
2565 * @flags: Flags related to GPIO edge.
2567 * Return 0 in case of success, else negative error code.
2569 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2572 struct gpio_chip *gc;
2574 VALIDATE_DESC(desc);
2576 gc = desc->gdev->chip;
2577 if (!gc->en_hw_timestamp) {
2578 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2582 ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2584 gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
2588 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
2591 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2593 * @desc: GPIO to disable.
2594 * @flags: Flags related to GPIO edge, same value as used during enable call.
2596 * Return 0 in case of success, else negative error code.
2598 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2601 struct gpio_chip *gc;
2603 VALIDATE_DESC(desc);
2605 gc = desc->gdev->chip;
2606 if (!gc->dis_hw_timestamp) {
2607 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2611 ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2613 gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
2617 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
2620 * gpiod_set_config - sets @config for a GPIO
2621 * @desc: descriptor of the GPIO for which to set the configuration
2622 * @config: Same packed config format as generic pinconf
2625 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2628 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2630 struct gpio_chip *gc;
2632 VALIDATE_DESC(desc);
2633 gc = desc->gdev->chip;
2635 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2637 EXPORT_SYMBOL_GPL(gpiod_set_config);
2640 * gpiod_set_debounce - sets @debounce time for a GPIO
2641 * @desc: descriptor of the GPIO for which to set debounce time
2642 * @debounce: debounce time in microseconds
2645 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2648 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2650 unsigned long config;
2652 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2653 return gpiod_set_config(desc, config);
2655 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2658 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2659 * @desc: descriptor of the GPIO for which to configure persistence
2660 * @transitory: True to lose state on suspend or reset, false for persistence
2663 * 0 on success, otherwise a negative error code.
2665 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2667 VALIDATE_DESC(desc);
2669 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2670 * persistence state.
2672 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2674 /* If the driver supports it, set the persistence state now */
2675 return gpio_set_config_with_argument_optional(desc,
2676 PIN_CONFIG_PERSIST_STATE,
2679 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2682 * gpiod_is_active_low - test whether a GPIO is active-low or not
2683 * @desc: the gpio descriptor to test
2685 * Returns 1 if the GPIO is active-low, 0 otherwise.
2687 int gpiod_is_active_low(const struct gpio_desc *desc)
2689 VALIDATE_DESC(desc);
2690 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2692 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2695 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2696 * @desc: the gpio descriptor to change
2698 void gpiod_toggle_active_low(struct gpio_desc *desc)
2700 VALIDATE_DESC_VOID(desc);
2701 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2703 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2705 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
2707 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
2710 /* I/O calls are only valid after configuration completed; the relevant
2711 * "is this a valid GPIO" error checks should already have been done.
2713 * "Get" operations are often inlinable as reading a pin value register,
2714 * and masking the relevant bit in that register.
2716 * When "set" operations are inlinable, they involve writing that mask to
2717 * one register to set a low value, or a different register to set it high.
2718 * Otherwise locking is needed, so there may be little value to inlining.
2720 *------------------------------------------------------------------------
2722 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2723 * have requested the GPIO. That can include implicit requesting by
2724 * a direction setting call. Marking a gpio as requested locks its chip
2725 * in memory, guaranteeing that these table lookups need no more locking
2726 * and that gpiochip_remove() will fail.
2728 * REVISIT when debugging, consider adding some instrumentation to ensure
2729 * that the GPIO was actually requested.
2732 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2734 struct gpio_chip *gc;
2737 gc = desc->gdev->chip;
2738 value = gpio_chip_get_value(gc, desc);
2739 value = value < 0 ? value : !!value;
2740 trace_gpio_value(desc_to_gpio(desc), 1, value);
2744 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2745 unsigned long *mask, unsigned long *bits)
2747 if (gc->get_multiple)
2748 return gc->get_multiple(gc, mask, bits);
2752 for_each_set_bit(i, mask, gc->ngpio) {
2753 value = gc->get(gc, i);
2756 __assign_bit(i, bits, value);
2763 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2764 unsigned int array_size,
2765 struct gpio_desc **desc_array,
2766 struct gpio_array *array_info,
2767 unsigned long *value_bitmap)
2772 * Validate array_info against desc_array and its size.
2773 * It should immediately follow desc_array if both
2774 * have been obtained from the same gpiod_get_array() call.
2776 if (array_info && array_info->desc == desc_array &&
2777 array_size <= array_info->size &&
2778 (void *)array_info == desc_array + array_info->size) {
2780 WARN_ON(array_info->chip->can_sleep);
2782 ret = gpio_chip_get_multiple(array_info->chip,
2783 array_info->get_mask,
2788 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2789 bitmap_xor(value_bitmap, value_bitmap,
2790 array_info->invert_mask, array_size);
2792 i = find_first_zero_bit(array_info->get_mask, array_size);
2793 if (i == array_size)
2799 while (i < array_size) {
2800 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2801 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
2802 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
2803 unsigned long *mask, *bits;
2806 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2807 mask = fastpath_mask;
2808 bits = fastpath_bits;
2810 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
2812 mask = bitmap_alloc(gc->ngpio, flags);
2816 bits = bitmap_alloc(gc->ngpio, flags);
2823 bitmap_zero(mask, gc->ngpio);
2826 WARN_ON(gc->can_sleep);
2828 /* collect all inputs belonging to the same chip */
2831 const struct gpio_desc *desc = desc_array[i];
2832 int hwgpio = gpio_chip_hwgpio(desc);
2834 __set_bit(hwgpio, mask);
2838 i = find_next_zero_bit(array_info->get_mask,
2840 } while ((i < array_size) &&
2841 (desc_array[i]->gdev->chip == gc));
2843 ret = gpio_chip_get_multiple(gc, mask, bits);
2845 if (mask != fastpath_mask)
2847 if (bits != fastpath_bits)
2852 for (j = first; j < i; ) {
2853 const struct gpio_desc *desc = desc_array[j];
2854 int hwgpio = gpio_chip_hwgpio(desc);
2855 int value = test_bit(hwgpio, bits);
2857 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2859 __assign_bit(j, value_bitmap, value);
2860 trace_gpio_value(desc_to_gpio(desc), 1, value);
2864 j = find_next_zero_bit(array_info->get_mask, i,
2868 if (mask != fastpath_mask)
2870 if (bits != fastpath_bits)
2877 * gpiod_get_raw_value() - return a gpio's raw value
2878 * @desc: gpio whose value will be returned
2880 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2881 * its ACTIVE_LOW status, or negative errno on failure.
2883 * This function can be called from contexts where we cannot sleep, and will
2884 * complain if the GPIO chip functions potentially sleep.
2886 int gpiod_get_raw_value(const struct gpio_desc *desc)
2888 VALIDATE_DESC(desc);
2889 /* Should be using gpiod_get_raw_value_cansleep() */
2890 WARN_ON(desc->gdev->chip->can_sleep);
2891 return gpiod_get_raw_value_commit(desc);
2893 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2896 * gpiod_get_value() - return a gpio's value
2897 * @desc: gpio whose value will be returned
2899 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2900 * account, or negative errno on failure.
2902 * This function can be called from contexts where we cannot sleep, and will
2903 * complain if the GPIO chip functions potentially sleep.
2905 int gpiod_get_value(const struct gpio_desc *desc)
2909 VALIDATE_DESC(desc);
2910 /* Should be using gpiod_get_value_cansleep() */
2911 WARN_ON(desc->gdev->chip->can_sleep);
2913 value = gpiod_get_raw_value_commit(desc);
2917 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2922 EXPORT_SYMBOL_GPL(gpiod_get_value);
2925 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2926 * @array_size: number of elements in the descriptor array / value bitmap
2927 * @desc_array: array of GPIO descriptors whose values will be read
2928 * @array_info: information on applicability of fast bitmap processing path
2929 * @value_bitmap: bitmap to store the read values
2931 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2932 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2933 * else an error code.
2935 * This function can be called from contexts where we cannot sleep,
2936 * and it will complain if the GPIO chip functions potentially sleep.
2938 int gpiod_get_raw_array_value(unsigned int array_size,
2939 struct gpio_desc **desc_array,
2940 struct gpio_array *array_info,
2941 unsigned long *value_bitmap)
2945 return gpiod_get_array_value_complex(true, false, array_size,
2946 desc_array, array_info,
2949 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2952 * gpiod_get_array_value() - read values from an array of GPIOs
2953 * @array_size: number of elements in the descriptor array / value bitmap
2954 * @desc_array: array of GPIO descriptors whose values will be read
2955 * @array_info: information on applicability of fast bitmap processing path
2956 * @value_bitmap: bitmap to store the read values
2958 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2959 * into account. Return 0 in case of success, else an error code.
2961 * This function can be called from contexts where we cannot sleep,
2962 * and it will complain if the GPIO chip functions potentially sleep.
2964 int gpiod_get_array_value(unsigned int array_size,
2965 struct gpio_desc **desc_array,
2966 struct gpio_array *array_info,
2967 unsigned long *value_bitmap)
2971 return gpiod_get_array_value_complex(false, false, array_size,
2972 desc_array, array_info,
2975 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2978 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2979 * @desc: gpio descriptor whose state need to be set.
2980 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2982 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2985 struct gpio_chip *gc = desc->gdev->chip;
2986 int offset = gpio_chip_hwgpio(desc);
2989 ret = gc->direction_input(gc, offset);
2991 ret = gc->direction_output(gc, offset, 0);
2993 set_bit(FLAG_IS_OUT, &desc->flags);
2995 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2998 "%s: Error in set_value for open drain err %d\n",
3003 * _gpio_set_open_source_value() - Set the open source gpio's value.
3004 * @desc: gpio descriptor whose state need to be set.
3005 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3007 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3010 struct gpio_chip *gc = desc->gdev->chip;
3011 int offset = gpio_chip_hwgpio(desc);
3014 ret = gc->direction_output(gc, offset, 1);
3016 set_bit(FLAG_IS_OUT, &desc->flags);
3018 ret = gc->direction_input(gc, offset);
3020 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3023 "%s: Error in set_value for open source err %d\n",
3027 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3029 struct gpio_chip *gc;
3031 gc = desc->gdev->chip;
3032 trace_gpio_value(desc_to_gpio(desc), 0, value);
3033 gc->set(gc, gpio_chip_hwgpio(desc), value);
3037 * set multiple outputs on the same chip;
3038 * use the chip's set_multiple function if available;
3039 * otherwise set the outputs sequentially;
3040 * @chip: the GPIO chip we operate on
3041 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3042 * defines which outputs are to be changed
3043 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3044 * defines the values the outputs specified by mask are to be set to
3046 static void gpio_chip_set_multiple(struct gpio_chip *gc,
3047 unsigned long *mask, unsigned long *bits)
3049 if (gc->set_multiple) {
3050 gc->set_multiple(gc, mask, bits);
3054 /* set outputs if the corresponding mask bit is set */
3055 for_each_set_bit(i, mask, gc->ngpio)
3056 gc->set(gc, i, test_bit(i, bits));
3060 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3061 unsigned int array_size,
3062 struct gpio_desc **desc_array,
3063 struct gpio_array *array_info,
3064 unsigned long *value_bitmap)
3069 * Validate array_info against desc_array and its size.
3070 * It should immediately follow desc_array if both
3071 * have been obtained from the same gpiod_get_array() call.
3073 if (array_info && array_info->desc == desc_array &&
3074 array_size <= array_info->size &&
3075 (void *)array_info == desc_array + array_info->size) {
3077 WARN_ON(array_info->chip->can_sleep);
3079 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3080 bitmap_xor(value_bitmap, value_bitmap,
3081 array_info->invert_mask, array_size);
3083 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3086 i = find_first_zero_bit(array_info->set_mask, array_size);
3087 if (i == array_size)
3093 while (i < array_size) {
3094 struct gpio_chip *gc = desc_array[i]->gdev->chip;
3095 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3096 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3097 unsigned long *mask, *bits;
3100 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
3101 mask = fastpath_mask;
3102 bits = fastpath_bits;
3104 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3106 mask = bitmap_alloc(gc->ngpio, flags);
3110 bits = bitmap_alloc(gc->ngpio, flags);
3117 bitmap_zero(mask, gc->ngpio);
3120 WARN_ON(gc->can_sleep);
3123 struct gpio_desc *desc = desc_array[i];
3124 int hwgpio = gpio_chip_hwgpio(desc);
3125 int value = test_bit(i, value_bitmap);
3128 * Pins applicable for fast input but not for
3129 * fast output processing may have been already
3130 * inverted inside the fast path, skip them.
3132 if (!raw && !(array_info &&
3133 test_bit(i, array_info->invert_mask)) &&
3134 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3136 trace_gpio_value(desc_to_gpio(desc), 0, value);
3138 * collect all normal outputs belonging to the same chip
3139 * open drain and open source outputs are set individually
3141 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3142 gpio_set_open_drain_value_commit(desc, value);
3143 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3144 gpio_set_open_source_value_commit(desc, value);
3146 __set_bit(hwgpio, mask);
3147 __assign_bit(hwgpio, bits, value);
3153 i = find_next_zero_bit(array_info->set_mask,
3155 } while ((i < array_size) &&
3156 (desc_array[i]->gdev->chip == gc));
3157 /* push collected bits to outputs */
3159 gpio_chip_set_multiple(gc, mask, bits);
3161 if (mask != fastpath_mask)
3163 if (bits != fastpath_bits)
3170 * gpiod_set_raw_value() - assign a gpio's raw value
3171 * @desc: gpio whose value will be assigned
3172 * @value: value to assign
3174 * Set the raw value of the GPIO, i.e. the value of its physical line without
3175 * regard for its ACTIVE_LOW status.
3177 * This function can be called from contexts where we cannot sleep, and will
3178 * complain if the GPIO chip functions potentially sleep.
3180 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3182 VALIDATE_DESC_VOID(desc);
3183 /* Should be using gpiod_set_raw_value_cansleep() */
3184 WARN_ON(desc->gdev->chip->can_sleep);
3185 gpiod_set_raw_value_commit(desc, value);
3187 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3190 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3191 * @desc: the descriptor to set the value on
3192 * @value: value to set
3194 * This sets the value of a GPIO line backing a descriptor, applying
3195 * different semantic quirks like active low and open drain/source
3198 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3200 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3202 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3203 gpio_set_open_drain_value_commit(desc, value);
3204 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3205 gpio_set_open_source_value_commit(desc, value);
3207 gpiod_set_raw_value_commit(desc, value);
3211 * gpiod_set_value() - assign a gpio's value
3212 * @desc: gpio whose value will be assigned
3213 * @value: value to assign
3215 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3216 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3218 * This function can be called from contexts where we cannot sleep, and will
3219 * complain if the GPIO chip functions potentially sleep.
3221 void gpiod_set_value(struct gpio_desc *desc, int value)
3223 VALIDATE_DESC_VOID(desc);
3224 /* Should be using gpiod_set_value_cansleep() */
3225 WARN_ON(desc->gdev->chip->can_sleep);
3226 gpiod_set_value_nocheck(desc, value);
3228 EXPORT_SYMBOL_GPL(gpiod_set_value);
3231 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3232 * @array_size: number of elements in the descriptor array / value bitmap
3233 * @desc_array: array of GPIO descriptors whose values will be assigned
3234 * @array_info: information on applicability of fast bitmap processing path
3235 * @value_bitmap: bitmap of values to assign
3237 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3238 * without regard for their ACTIVE_LOW status.
3240 * This function can be called from contexts where we cannot sleep, and will
3241 * complain if the GPIO chip functions potentially sleep.
3243 int gpiod_set_raw_array_value(unsigned int array_size,
3244 struct gpio_desc **desc_array,
3245 struct gpio_array *array_info,
3246 unsigned long *value_bitmap)
3250 return gpiod_set_array_value_complex(true, false, array_size,
3251 desc_array, array_info, value_bitmap);
3253 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3256 * gpiod_set_array_value() - assign values to an array of GPIOs
3257 * @array_size: number of elements in the descriptor array / value bitmap
3258 * @desc_array: array of GPIO descriptors whose values will be assigned
3259 * @array_info: information on applicability of fast bitmap processing path
3260 * @value_bitmap: bitmap of values to assign
3262 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3265 * This function can be called from contexts where we cannot sleep, and will
3266 * complain if the GPIO chip functions potentially sleep.
3268 int gpiod_set_array_value(unsigned int array_size,
3269 struct gpio_desc **desc_array,
3270 struct gpio_array *array_info,
3271 unsigned long *value_bitmap)
3275 return gpiod_set_array_value_complex(false, false, array_size,
3276 desc_array, array_info,
3279 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3282 * gpiod_cansleep() - report whether gpio value access may sleep
3283 * @desc: gpio to check
3286 int gpiod_cansleep(const struct gpio_desc *desc)
3288 VALIDATE_DESC(desc);
3289 return desc->gdev->chip->can_sleep;
3291 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3294 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3295 * @desc: gpio to set the consumer name on
3296 * @name: the new consumer name
3298 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3300 VALIDATE_DESC(desc);
3302 name = kstrdup_const(name, GFP_KERNEL);
3307 kfree_const(desc->label);
3308 desc_set_label(desc, name);
3312 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3315 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3316 * @desc: gpio whose IRQ will be returned (already requested)
3318 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3321 int gpiod_to_irq(const struct gpio_desc *desc)
3323 struct gpio_chip *gc;
3327 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3328 * requires this function to not return zero on an invalid descriptor
3329 * but rather a negative error number.
3331 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3334 gc = desc->gdev->chip;
3335 offset = gpio_chip_hwgpio(desc);
3337 int retirq = gc->to_irq(gc, offset);
3339 /* Zero means NO_IRQ */
3345 #ifdef CONFIG_GPIOLIB_IRQCHIP
3348 * Avoid race condition with other code, which tries to lookup
3349 * an IRQ before the irqchip has been properly registered,
3350 * i.e. while gpiochip is still being brought up.
3352 return -EPROBE_DEFER;
3357 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3360 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3361 * @gc: the chip the GPIO to lock belongs to
3362 * @offset: the offset of the GPIO to lock as IRQ
3364 * This is used directly by GPIO drivers that want to lock down
3365 * a certain GPIO line to be used for IRQs.
3367 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3369 struct gpio_desc *desc;
3371 desc = gpiochip_get_desc(gc, offset);
3373 return PTR_ERR(desc);
3376 * If it's fast: flush the direction setting if something changed
3379 if (!gc->can_sleep && gc->get_direction) {
3380 int dir = gpiod_get_direction(desc);
3383 chip_err(gc, "%s: cannot get GPIO direction\n",
3389 /* To be valid for IRQ the line needs to be input or open drain */
3390 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3391 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3393 "%s: tried to flag a GPIO set as output for IRQ\n",
3398 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3399 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3402 * If the consumer has not set up a label (such as when the
3403 * IRQ is referenced from .to_irq()) we set up a label here
3404 * so it is clear this is used as an interrupt.
3407 desc_set_label(desc, "interrupt");
3411 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3414 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3415 * @gc: the chip the GPIO to lock belongs to
3416 * @offset: the offset of the GPIO to lock as IRQ
3418 * This is used directly by GPIO drivers that want to indicate
3419 * that a certain GPIO is no longer used exclusively for IRQ.
3421 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3423 struct gpio_desc *desc;
3425 desc = gpiochip_get_desc(gc, offset);
3429 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3430 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3432 /* If we only had this marking, erase it */
3433 if (desc->label && !strcmp(desc->label, "interrupt"))
3434 desc_set_label(desc, NULL);
3436 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3438 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3440 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3442 if (!IS_ERR(desc) &&
3443 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3444 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3446 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3448 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3450 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3452 if (!IS_ERR(desc) &&
3453 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3455 * We must not be output when using IRQ UNLESS we are
3458 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3459 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3460 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3463 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3465 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3467 if (offset >= gc->ngpio)
3470 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3472 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3474 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3478 if (!try_module_get(gc->gpiodev->owner))
3481 ret = gpiochip_lock_as_irq(gc, offset);
3483 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3484 module_put(gc->gpiodev->owner);
3489 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3491 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3493 gpiochip_unlock_as_irq(gc, offset);
3494 module_put(gc->gpiodev->owner);
3496 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3498 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3500 if (offset >= gc->ngpio)
3503 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3505 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3507 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3509 if (offset >= gc->ngpio)
3512 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3514 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3516 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3518 if (offset >= gc->ngpio)
3521 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3523 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3526 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3527 * @desc: gpio whose value will be returned
3529 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3530 * its ACTIVE_LOW status, or negative errno on failure.
3532 * This function is to be called from contexts that can sleep.
3534 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3536 might_sleep_if(extra_checks);
3537 VALIDATE_DESC(desc);
3538 return gpiod_get_raw_value_commit(desc);
3540 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3543 * gpiod_get_value_cansleep() - return a gpio's value
3544 * @desc: gpio whose value will be returned
3546 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3547 * account, or negative errno on failure.
3549 * This function is to be called from contexts that can sleep.
3551 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3555 might_sleep_if(extra_checks);
3556 VALIDATE_DESC(desc);
3557 value = gpiod_get_raw_value_commit(desc);
3561 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3566 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3569 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3570 * @array_size: number of elements in the descriptor array / value bitmap
3571 * @desc_array: array of GPIO descriptors whose values will be read
3572 * @array_info: information on applicability of fast bitmap processing path
3573 * @value_bitmap: bitmap to store the read values
3575 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3576 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3577 * else an error code.
3579 * This function is to be called from contexts that can sleep.
3581 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3582 struct gpio_desc **desc_array,
3583 struct gpio_array *array_info,
3584 unsigned long *value_bitmap)
3586 might_sleep_if(extra_checks);
3589 return gpiod_get_array_value_complex(true, true, array_size,
3590 desc_array, array_info,
3593 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3596 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3597 * @array_size: number of elements in the descriptor array / value bitmap
3598 * @desc_array: array of GPIO descriptors whose values will be read
3599 * @array_info: information on applicability of fast bitmap processing path
3600 * @value_bitmap: bitmap to store the read values
3602 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3603 * into account. Return 0 in case of success, else an error code.
3605 * This function is to be called from contexts that can sleep.
3607 int gpiod_get_array_value_cansleep(unsigned int array_size,
3608 struct gpio_desc **desc_array,
3609 struct gpio_array *array_info,
3610 unsigned long *value_bitmap)
3612 might_sleep_if(extra_checks);
3615 return gpiod_get_array_value_complex(false, true, array_size,
3616 desc_array, array_info,
3619 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3622 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3623 * @desc: gpio whose value will be assigned
3624 * @value: value to assign
3626 * Set the raw value of the GPIO, i.e. the value of its physical line without
3627 * regard for its ACTIVE_LOW status.
3629 * This function is to be called from contexts that can sleep.
3631 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3633 might_sleep_if(extra_checks);
3634 VALIDATE_DESC_VOID(desc);
3635 gpiod_set_raw_value_commit(desc, value);
3637 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3640 * gpiod_set_value_cansleep() - assign a gpio's value
3641 * @desc: gpio whose value will be assigned
3642 * @value: value to assign
3644 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3647 * This function is to be called from contexts that can sleep.
3649 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3651 might_sleep_if(extra_checks);
3652 VALIDATE_DESC_VOID(desc);
3653 gpiod_set_value_nocheck(desc, value);
3655 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3658 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3659 * @array_size: number of elements in the descriptor array / value bitmap
3660 * @desc_array: array of GPIO descriptors whose values will be assigned
3661 * @array_info: information on applicability of fast bitmap processing path
3662 * @value_bitmap: bitmap of values to assign
3664 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3665 * without regard for their ACTIVE_LOW status.
3667 * This function is to be called from contexts that can sleep.
3669 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3670 struct gpio_desc **desc_array,
3671 struct gpio_array *array_info,
3672 unsigned long *value_bitmap)
3674 might_sleep_if(extra_checks);
3677 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3678 array_info, value_bitmap);
3680 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3683 * gpiod_add_lookup_tables() - register GPIO device consumers
3684 * @tables: list of tables of consumers to register
3685 * @n: number of tables in the list
3687 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3691 mutex_lock(&gpio_lookup_lock);
3693 for (i = 0; i < n; i++)
3694 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3696 mutex_unlock(&gpio_lookup_lock);
3700 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3701 * @array_size: number of elements in the descriptor array / value bitmap
3702 * @desc_array: array of GPIO descriptors whose values will be assigned
3703 * @array_info: information on applicability of fast bitmap processing path
3704 * @value_bitmap: bitmap of values to assign
3706 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3709 * This function is to be called from contexts that can sleep.
3711 int gpiod_set_array_value_cansleep(unsigned int array_size,
3712 struct gpio_desc **desc_array,
3713 struct gpio_array *array_info,
3714 unsigned long *value_bitmap)
3716 might_sleep_if(extra_checks);
3719 return gpiod_set_array_value_complex(false, true, array_size,
3720 desc_array, array_info,
3723 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3726 * gpiod_add_lookup_table() - register GPIO device consumers
3727 * @table: table of consumers to register
3729 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3731 gpiod_add_lookup_tables(&table, 1);
3733 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3736 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3737 * @table: table of consumers to unregister
3739 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3741 /* Nothing to remove */
3745 mutex_lock(&gpio_lookup_lock);
3747 list_del(&table->list);
3749 mutex_unlock(&gpio_lookup_lock);
3751 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3754 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3755 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3757 void gpiod_add_hogs(struct gpiod_hog *hogs)
3759 struct gpio_chip *gc;
3760 struct gpiod_hog *hog;
3762 mutex_lock(&gpio_machine_hogs_mutex);
3764 for (hog = &hogs[0]; hog->chip_label; hog++) {
3765 list_add_tail(&hog->list, &gpio_machine_hogs);
3768 * The chip may have been registered earlier, so check if it
3769 * exists and, if so, try to hog the line now.
3771 gc = find_chip_by_name(hog->chip_label);
3773 gpiochip_machine_hog(gc, hog);
3776 mutex_unlock(&gpio_machine_hogs_mutex);
3778 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3780 void gpiod_remove_hogs(struct gpiod_hog *hogs)
3782 struct gpiod_hog *hog;
3784 mutex_lock(&gpio_machine_hogs_mutex);
3785 for (hog = &hogs[0]; hog->chip_label; hog++)
3786 list_del(&hog->list);
3787 mutex_unlock(&gpio_machine_hogs_mutex);
3789 EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
3791 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3793 const char *dev_id = dev ? dev_name(dev) : NULL;
3794 struct gpiod_lookup_table *table;
3796 mutex_lock(&gpio_lookup_lock);
3798 list_for_each_entry(table, &gpio_lookup_list, list) {
3799 if (table->dev_id && dev_id) {
3801 * Valid strings on both ends, must be identical to have
3804 if (!strcmp(table->dev_id, dev_id))
3808 * One of the pointers is NULL, so both must be to have
3811 if (dev_id == table->dev_id)
3818 mutex_unlock(&gpio_lookup_lock);
3822 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3823 unsigned int idx, unsigned long *flags)
3825 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3826 struct gpiod_lookup_table *table;
3827 struct gpiod_lookup *p;
3829 table = gpiod_find_lookup_table(dev);
3833 for (p = &table->table[0]; p->key; p++) {
3834 struct gpio_chip *gc;
3836 /* idx must always match exactly */
3840 /* If the lookup entry has a con_id, require exact match */
3841 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3844 if (p->chip_hwnum == U16_MAX) {
3845 desc = gpio_name_to_desc(p->key);
3851 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3853 return ERR_PTR(-EPROBE_DEFER);
3856 gc = find_chip_by_name(p->key);
3860 * As the lookup table indicates a chip with
3861 * p->key should exist, assume it may
3862 * still appear later and let the interested
3863 * consumer be probed again or let the Deferred
3864 * Probe infrastructure handle the error.
3866 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3868 return ERR_PTR(-EPROBE_DEFER);
3871 if (gc->ngpio <= p->chip_hwnum) {
3873 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3874 idx, p->chip_hwnum, gc->ngpio - 1,
3876 return ERR_PTR(-EINVAL);
3879 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3888 static int platform_gpio_count(struct device *dev, const char *con_id)
3890 struct gpiod_lookup_table *table;
3891 struct gpiod_lookup *p;
3892 unsigned int count = 0;
3894 table = gpiod_find_lookup_table(dev);
3898 for (p = &table->table[0]; p->key; p++) {
3899 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3900 (!con_id && !p->con_id))
3909 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
3910 struct device *consumer,
3913 enum gpiod_flags *flags,
3914 unsigned long *lookupflags)
3916 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3918 if (is_of_node(fwnode)) {
3919 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n",
3921 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags);
3922 } else if (is_acpi_node(fwnode)) {
3923 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n",
3925 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
3926 } else if (is_software_node(fwnode)) {
3927 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n",
3929 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
3935 static struct gpio_desc *gpiod_find_and_request(struct device *consumer,
3936 struct fwnode_handle *fwnode,
3939 enum gpiod_flags flags,
3941 bool platform_lookup_allowed)
3943 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3944 struct gpio_desc *desc;
3947 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, &flags, &lookupflags);
3948 if (gpiod_not_found(desc) && platform_lookup_allowed) {
3950 * Either we are not using DT or ACPI, or their lookup did not
3951 * return a result. In that case, use platform lookup as a
3954 dev_dbg(consumer, "using lookup tables for GPIO lookup\n");
3955 desc = gpiod_find(consumer, con_id, idx, &lookupflags);
3959 dev_dbg(consumer, "No GPIO consumer %s found\n", con_id);
3964 * If a connection label was passed use that, else attempt to use
3965 * the device name as label
3967 ret = gpiod_request(desc, label);
3969 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
3970 return ERR_PTR(ret);
3973 * This happens when there are several consumers for
3974 * the same GPIO line: we just return here without
3975 * further initialization. It is a bit of a hack.
3976 * This is necessary to support fixed regulators.
3978 * FIXME: Make this more sane and safe.
3981 "nonexclusive access to GPIO for %s\n", con_id);
3985 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3987 dev_dbg(consumer, "setup of GPIO %s failed\n", con_id);
3989 return ERR_PTR(ret);
3992 blocking_notifier_call_chain(&desc->gdev->notifier,
3993 GPIOLINE_CHANGED_REQUESTED, desc);
3999 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4000 * @fwnode: handle of the firmware node
4001 * @con_id: function within the GPIO consumer
4002 * @index: index of the GPIO to obtain for the consumer
4003 * @flags: GPIO initialization flags
4004 * @label: label to attach to the requested GPIO
4006 * This function can be used for drivers that get their configuration
4007 * from opaque firmware.
4009 * The function properly finds the corresponding GPIO using whatever is the
4010 * underlying firmware interface and then makes sure that the GPIO
4011 * descriptor is requested before it is returned to the caller.
4014 * On successful request the GPIO pin is configured in accordance with
4017 * In case of error an ERR_PTR() is returned.
4019 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4022 enum gpiod_flags flags,
4025 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false);
4027 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4030 * gpiod_count - return the number of GPIOs associated with a device / function
4031 * or -ENOENT if no GPIO has been assigned to the requested function
4032 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4033 * @con_id: function within the GPIO consumer
4035 int gpiod_count(struct device *dev, const char *con_id)
4037 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4038 int count = -ENOENT;
4040 if (is_of_node(fwnode))
4041 count = of_gpio_get_count(dev, con_id);
4042 else if (is_acpi_node(fwnode))
4043 count = acpi_gpio_count(dev, con_id);
4044 else if (is_software_node(fwnode))
4045 count = swnode_gpio_count(fwnode, con_id);
4048 count = platform_gpio_count(dev, con_id);
4052 EXPORT_SYMBOL_GPL(gpiod_count);
4055 * gpiod_get - obtain a GPIO for a given GPIO function
4056 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4057 * @con_id: function within the GPIO consumer
4058 * @flags: optional GPIO initialization flags
4060 * Return the GPIO descriptor corresponding to the function con_id of device
4061 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4062 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4064 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4065 enum gpiod_flags flags)
4067 return gpiod_get_index(dev, con_id, 0, flags);
4069 EXPORT_SYMBOL_GPL(gpiod_get);
4072 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4073 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4074 * @con_id: function within the GPIO consumer
4075 * @flags: optional GPIO initialization flags
4077 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4078 * the requested function it will return NULL. This is convenient for drivers
4079 * that need to handle optional GPIOs.
4081 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4083 enum gpiod_flags flags)
4085 return gpiod_get_index_optional(dev, con_id, 0, flags);
4087 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4091 * gpiod_configure_flags - helper function to configure a given GPIO
4092 * @desc: gpio whose value will be assigned
4093 * @con_id: function within the GPIO consumer
4094 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4095 * of_find_gpio() or of_get_gpio_hog()
4096 * @dflags: gpiod_flags - optional GPIO initialization flags
4098 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4099 * requested function and/or index, or another IS_ERR() code if an error
4100 * occurred while trying to acquire the GPIO.
4102 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4103 unsigned long lflags, enum gpiod_flags dflags)
4107 if (lflags & GPIO_ACTIVE_LOW)
4108 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4110 if (lflags & GPIO_OPEN_DRAIN)
4111 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4112 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4114 * This enforces open drain mode from the consumer side.
4115 * This is necessary for some busses like I2C, but the lookup
4116 * should *REALLY* have specified them as open drain in the
4117 * first place, so print a little warning here.
4119 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4121 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4124 if (lflags & GPIO_OPEN_SOURCE)
4125 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4127 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
4128 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
4129 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
4131 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4135 if (lflags & GPIO_PULL_UP)
4136 set_bit(FLAG_PULL_UP, &desc->flags);
4137 else if (lflags & GPIO_PULL_DOWN)
4138 set_bit(FLAG_PULL_DOWN, &desc->flags);
4139 else if (lflags & GPIO_PULL_DISABLE)
4140 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
4142 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4146 /* No particular flag request, return here... */
4147 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4148 gpiod_dbg(desc, "no flags found for %s\n", con_id);
4153 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4154 ret = gpiod_direction_output(desc,
4155 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4157 ret = gpiod_direction_input(desc);
4163 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4164 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4165 * @con_id: function within the GPIO consumer
4166 * @idx: index of the GPIO to obtain in the consumer
4167 * @flags: optional GPIO initialization flags
4169 * This variant of gpiod_get() allows to access GPIOs other than the first
4170 * defined one for functions that define several GPIOs.
4172 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4173 * requested function and/or index, or another IS_ERR() code if an error
4174 * occurred while trying to acquire the GPIO.
4176 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4179 enum gpiod_flags flags)
4181 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4182 const char *devname = dev ? dev_name(dev) : "?";
4183 const char *label = con_id ?: devname;
4185 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true);
4187 EXPORT_SYMBOL_GPL(gpiod_get_index);
4190 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4192 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4193 * @con_id: function within the GPIO consumer
4194 * @index: index of the GPIO to obtain in the consumer
4195 * @flags: optional GPIO initialization flags
4197 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4198 * specified index was assigned to the requested function it will return NULL.
4199 * This is convenient for drivers that need to handle optional GPIOs.
4201 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4204 enum gpiod_flags flags)
4206 struct gpio_desc *desc;
4208 desc = gpiod_get_index(dev, con_id, index, flags);
4209 if (gpiod_not_found(desc))
4214 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4217 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4218 * @desc: gpio whose value will be assigned
4219 * @name: gpio line name
4220 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4221 * of_find_gpio() or of_get_gpio_hog()
4222 * @dflags: gpiod_flags - optional GPIO initialization flags
4224 int gpiod_hog(struct gpio_desc *desc, const char *name,
4225 unsigned long lflags, enum gpiod_flags dflags)
4227 struct gpio_chip *gc;
4228 struct gpio_desc *local_desc;
4232 gc = gpiod_to_chip(desc);
4233 hwnum = gpio_chip_hwgpio(desc);
4235 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4237 if (IS_ERR(local_desc)) {
4238 ret = PTR_ERR(local_desc);
4239 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4240 name, gc->label, hwnum, ret);
4244 /* Mark GPIO as hogged so it can be identified and removed later */
4245 set_bit(FLAG_IS_HOGGED, &desc->flags);
4247 gpiod_dbg(desc, "hogged as %s%s\n",
4248 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4249 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4250 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4256 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4257 * @gc: gpio chip to act on
4259 static void gpiochip_free_hogs(struct gpio_chip *gc)
4261 struct gpio_desc *desc;
4263 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
4264 gpiochip_free_own_desc(desc);
4268 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4269 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4270 * @con_id: function within the GPIO consumer
4271 * @flags: optional GPIO initialization flags
4273 * This function acquires all the GPIOs defined under a given function.
4275 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4276 * no GPIO has been assigned to the requested function, or another IS_ERR()
4277 * code if an error occurred while trying to acquire the GPIOs.
4279 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4281 enum gpiod_flags flags)
4283 struct gpio_desc *desc;
4284 struct gpio_descs *descs;
4285 struct gpio_array *array_info = NULL;
4286 struct gpio_chip *gc;
4287 int count, bitmap_size;
4290 count = gpiod_count(dev, con_id);
4292 return ERR_PTR(count);
4294 descs_size = struct_size(descs, desc, count);
4295 descs = kzalloc(descs_size, GFP_KERNEL);
4297 return ERR_PTR(-ENOMEM);
4299 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) {
4300 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4302 gpiod_put_array(descs);
4303 return ERR_CAST(desc);
4306 descs->desc[descs->ndescs] = desc;
4308 gc = gpiod_to_chip(desc);
4310 * If pin hardware number of array member 0 is also 0, select
4311 * its chip as a candidate for fast bitmap processing path.
4313 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4314 struct gpio_descs *array;
4316 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4319 array = krealloc(descs, descs_size +
4320 struct_size(array_info, invert_mask, 3 * bitmap_size),
4321 GFP_KERNEL | __GFP_ZERO);
4323 gpiod_put_array(descs);
4324 return ERR_PTR(-ENOMEM);
4329 array_info = (void *)descs + descs_size;
4330 array_info->get_mask = array_info->invert_mask +
4332 array_info->set_mask = array_info->get_mask +
4335 array_info->desc = descs->desc;
4336 array_info->size = count;
4337 array_info->chip = gc;
4338 bitmap_set(array_info->get_mask, descs->ndescs,
4339 count - descs->ndescs);
4340 bitmap_set(array_info->set_mask, descs->ndescs,
4341 count - descs->ndescs);
4342 descs->info = array_info;
4345 /* If there is no cache for fast bitmap processing path, continue */
4349 /* Unmark array members which don't belong to the 'fast' chip */
4350 if (array_info->chip != gc) {
4351 __clear_bit(descs->ndescs, array_info->get_mask);
4352 __clear_bit(descs->ndescs, array_info->set_mask);
4355 * Detect array members which belong to the 'fast' chip
4356 * but their pins are not in hardware order.
4358 else if (gpio_chip_hwgpio(desc) != descs->ndescs) {
4360 * Don't use fast path if all array members processed so
4361 * far belong to the same chip as this one but its pin
4362 * hardware number is different from its array index.
4364 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4367 __clear_bit(descs->ndescs,
4368 array_info->get_mask);
4369 __clear_bit(descs->ndescs,
4370 array_info->set_mask);
4373 /* Exclude open drain or open source from fast output */
4374 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4375 gpiochip_line_is_open_source(gc, descs->ndescs))
4376 __clear_bit(descs->ndescs,
4377 array_info->set_mask);
4378 /* Identify 'fast' pins which require invertion */
4379 if (gpiod_is_active_low(desc))
4380 __set_bit(descs->ndescs,
4381 array_info->invert_mask);
4386 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4387 array_info->chip->label, array_info->size,
4388 *array_info->get_mask, *array_info->set_mask,
4389 *array_info->invert_mask);
4392 EXPORT_SYMBOL_GPL(gpiod_get_array);
4395 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4397 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4398 * @con_id: function within the GPIO consumer
4399 * @flags: optional GPIO initialization flags
4401 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4402 * assigned to the requested function it will return NULL.
4404 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4406 enum gpiod_flags flags)
4408 struct gpio_descs *descs;
4410 descs = gpiod_get_array(dev, con_id, flags);
4411 if (gpiod_not_found(descs))
4416 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4419 * gpiod_put - dispose of a GPIO descriptor
4420 * @desc: GPIO descriptor to dispose of
4422 * No descriptor can be used after gpiod_put() has been called on it.
4424 void gpiod_put(struct gpio_desc *desc)
4429 EXPORT_SYMBOL_GPL(gpiod_put);
4432 * gpiod_put_array - dispose of multiple GPIO descriptors
4433 * @descs: struct gpio_descs containing an array of descriptors
4435 void gpiod_put_array(struct gpio_descs *descs)
4439 for (i = 0; i < descs->ndescs; i++)
4440 gpiod_put(descs->desc[i]);
4444 EXPORT_SYMBOL_GPL(gpiod_put_array);
4446 static int gpio_stub_drv_probe(struct device *dev)
4449 * The DT node of some GPIO chips have a "compatible" property, but
4450 * never have a struct device added and probed by a driver to register
4451 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4452 * the consumers of the GPIO chip to get probe deferred forever because
4453 * they will be waiting for a device associated with the GPIO chip
4454 * firmware node to get added and bound to a driver.
4456 * To allow these consumers to probe, we associate the struct
4457 * gpio_device of the GPIO chip with the firmware node and then simply
4458 * bind it to this stub driver.
4463 static struct device_driver gpio_stub_drv = {
4464 .name = "gpio_stub_drv",
4465 .bus = &gpio_bus_type,
4466 .probe = gpio_stub_drv_probe,
4469 static int __init gpiolib_dev_init(void)
4473 /* Register GPIO sysfs bus */
4474 ret = bus_register(&gpio_bus_type);
4476 pr_err("gpiolib: could not register GPIO bus type\n");
4480 ret = driver_register(&gpio_stub_drv);
4482 pr_err("gpiolib: could not register GPIO stub driver\n");
4483 bus_unregister(&gpio_bus_type);
4487 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4489 pr_err("gpiolib: failed to allocate char dev region\n");
4490 driver_unregister(&gpio_stub_drv);
4491 bus_unregister(&gpio_bus_type);
4495 gpiolib_initialized = true;
4496 gpiochip_setup_devs();
4498 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4499 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4500 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4504 core_initcall(gpiolib_dev_init);
4506 #ifdef CONFIG_DEBUG_FS
4508 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4510 struct gpio_chip *gc = gdev->chip;
4511 struct gpio_desc *desc;
4512 unsigned gpio = gdev->base;
4518 for_each_gpio_desc(gc, desc) {
4519 if (test_bit(FLAG_REQUESTED, &desc->flags)) {
4520 gpiod_get_direction(desc);
4521 is_out = test_bit(FLAG_IS_OUT, &desc->flags);
4522 value = gpio_chip_get_value(gc, desc);
4523 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
4524 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4525 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
4526 gpio, desc->name ?: "", desc->label,
4527 is_out ? "out" : "in ",
4528 value >= 0 ? (value ? "hi" : "lo") : "? ",
4529 is_irq ? "IRQ " : "",
4530 active_low ? "ACTIVE LOW" : "");
4531 } else if (desc->name) {
4532 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
4539 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4541 unsigned long flags;
4542 struct gpio_device *gdev = NULL;
4543 loff_t index = *pos;
4547 spin_lock_irqsave(&gpio_lock, flags);
4548 list_for_each_entry(gdev, &gpio_devices, list)
4550 spin_unlock_irqrestore(&gpio_lock, flags);
4553 spin_unlock_irqrestore(&gpio_lock, flags);
4558 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4560 unsigned long flags;
4561 struct gpio_device *gdev = v;
4564 spin_lock_irqsave(&gpio_lock, flags);
4565 if (list_is_last(&gdev->list, &gpio_devices))
4568 ret = list_first_entry(&gdev->list, struct gpio_device, list);
4569 spin_unlock_irqrestore(&gpio_lock, flags);
4577 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4581 static int gpiolib_seq_show(struct seq_file *s, void *v)
4583 struct gpio_device *gdev = v;
4584 struct gpio_chip *gc = gdev->chip;
4585 struct device *parent;
4588 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4589 dev_name(&gdev->dev));
4593 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4594 dev_name(&gdev->dev),
4595 gdev->base, gdev->base + gdev->ngpio - 1);
4596 parent = gc->parent;
4598 seq_printf(s, ", parent: %s/%s",
4599 parent->bus ? parent->bus->name : "no-bus",
4602 seq_printf(s, ", %s", gc->label);
4604 seq_printf(s, ", can sleep");
4605 seq_printf(s, ":\n");
4608 gc->dbg_show(s, gc);
4610 gpiolib_dbg_show(s, gdev);
4615 static const struct seq_operations gpiolib_sops = {
4616 .start = gpiolib_seq_start,
4617 .next = gpiolib_seq_next,
4618 .stop = gpiolib_seq_stop,
4619 .show = gpiolib_seq_show,
4621 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4623 static int __init gpiolib_debugfs_init(void)
4625 /* /sys/kernel/debug/gpio */
4626 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4629 subsys_initcall(gpiolib_debugfs_init);
4631 #endif /* DEBUG_FS */