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;
214 if (gpio_is_valid(base)) {
215 pr_debug("%s: found new base at %d\n", __func__, base);
218 pr_err("%s: cannot find free range\n", __func__);
224 * gpiod_get_direction - return the current direction of a GPIO
225 * @desc: GPIO to get the direction of
227 * Returns 0 for output, 1 for input, or an error code in case of error.
229 * This function may sleep if gpiod_cansleep() is true.
231 int gpiod_get_direction(struct gpio_desc *desc)
233 struct gpio_chip *gc;
237 gc = gpiod_to_chip(desc);
238 offset = gpio_chip_hwgpio(desc);
241 * Open drain emulation using input mode may incorrectly report
242 * input here, fix that up.
244 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
245 test_bit(FLAG_IS_OUT, &desc->flags))
248 if (!gc->get_direction)
251 ret = gc->get_direction(gc, offset);
255 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
259 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
263 EXPORT_SYMBOL_GPL(gpiod_get_direction);
266 * Add a new chip to the global chips list, keeping the list of chips sorted
267 * by range(means [base, base + ngpio - 1]) order.
269 * Return -EBUSY if the new chip overlaps with some other chip's integer
272 static int gpiodev_add_to_list(struct gpio_device *gdev)
274 struct gpio_device *prev, *next;
276 if (list_empty(&gpio_devices)) {
277 /* initial entry in list */
278 list_add_tail(&gdev->list, &gpio_devices);
282 next = list_first_entry(&gpio_devices, struct gpio_device, list);
283 if (gdev->base + gdev->ngpio <= next->base) {
284 /* add before first entry */
285 list_add(&gdev->list, &gpio_devices);
289 prev = list_last_entry(&gpio_devices, struct gpio_device, list);
290 if (prev->base + prev->ngpio <= gdev->base) {
291 /* add behind last entry */
292 list_add_tail(&gdev->list, &gpio_devices);
296 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
297 /* at the end of the list */
298 if (&next->list == &gpio_devices)
301 /* add between prev and next */
302 if (prev->base + prev->ngpio <= gdev->base
303 && gdev->base + gdev->ngpio <= next->base) {
304 list_add(&gdev->list, &prev->list);
313 * Convert a GPIO name to its descriptor
314 * Note that there is no guarantee that GPIO names are globally unique!
315 * Hence this function will return, if it exists, a reference to the first GPIO
316 * line found that matches the given name.
318 static struct gpio_desc *gpio_name_to_desc(const char * const name)
320 struct gpio_device *gdev;
326 spin_lock_irqsave(&gpio_lock, flags);
328 list_for_each_entry(gdev, &gpio_devices, list) {
329 struct gpio_desc *desc;
331 for_each_gpio_desc(gdev->chip, desc) {
332 if (desc->name && !strcmp(desc->name, name)) {
333 spin_unlock_irqrestore(&gpio_lock, flags);
339 spin_unlock_irqrestore(&gpio_lock, flags);
345 * Take the names from gc->names and assign them to their GPIO descriptors.
346 * Warn if a name is already used for a GPIO line on a different GPIO chip.
349 * 1. Non-unique names are still accepted,
350 * 2. Name collisions within the same GPIO chip are not reported.
352 static int gpiochip_set_desc_names(struct gpio_chip *gc)
354 struct gpio_device *gdev = gc->gpiodev;
357 /* First check all names if they are unique */
358 for (i = 0; i != gc->ngpio; ++i) {
359 struct gpio_desc *gpio;
361 gpio = gpio_name_to_desc(gc->names[i]);
364 "Detected name collision for GPIO name '%s'\n",
368 /* Then add all names to the GPIO descriptors */
369 for (i = 0; i != gc->ngpio; ++i)
370 gdev->descs[i].name = gc->names[i];
376 * gpiochip_set_names - Set GPIO line names using device properties
377 * @chip: GPIO chip whose lines should be named, if possible
379 * Looks for device property "gpio-line-names" and if it exists assigns
380 * GPIO line names for the chip. The memory allocated for the assigned
381 * names belong to the underlying firmware node and should not be released
384 static int gpiochip_set_names(struct gpio_chip *chip)
386 struct gpio_device *gdev = chip->gpiodev;
387 struct device *dev = &gdev->dev;
392 count = device_property_string_array_count(dev, "gpio-line-names");
397 * When offset is set in the driver side we assume the driver internally
398 * is using more than one gpiochip per the same device. We have to stop
399 * setting friendly names if the specified ones with 'gpio-line-names'
400 * are less than the offset in the device itself. This means all the
401 * lines are not present for every single pin within all the internal
404 if (count <= chip->offset) {
405 dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
406 count, chip->offset);
410 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
414 ret = device_property_read_string_array(dev, "gpio-line-names",
417 dev_warn(dev, "failed to read GPIO line names\n");
423 * When more that one gpiochip per device is used, 'count' can
424 * contain at most number gpiochips x chip->ngpio. We have to
425 * correctly distribute all defined lines taking into account
426 * chip->offset as starting point from where we will assign
427 * the names to pins from the 'names' array. Since property
428 * 'gpio-line-names' cannot contains gaps, we have to be sure
429 * we only assign those pins that really exists since chip->ngpio
430 * can be different of the chip->offset.
432 count = (count > chip->offset) ? count - chip->offset : count;
433 if (count > chip->ngpio)
436 for (i = 0; i < count; i++) {
438 * Allow overriding "fixed" names provided by the GPIO
439 * provider. The "fixed" names are more often than not
440 * generic and less informative than the names given in
443 if (names[chip->offset + i] && names[chip->offset + i][0])
444 gdev->descs[i].name = names[chip->offset + i];
452 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
456 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
460 /* Assume by default all GPIOs are valid */
461 bitmap_fill(p, gc->ngpio);
466 static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
468 struct device *dev = &gc->gpiodev->dev;
471 /* Format is "start, count, ..." */
472 size = device_property_count_u32(dev, "gpio-reserved-ranges");
473 if (size > 0 && size % 2 == 0)
479 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
481 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
484 gc->valid_mask = gpiochip_allocate_mask(gc);
491 static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc)
493 struct device *dev = &gc->gpiodev->dev;
498 size = gpiochip_count_reserved_ranges(gc);
502 ranges = kmalloc_array(size, sizeof(*ranges), GFP_KERNEL);
506 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges",
514 u32 count = ranges[--size];
515 u32 start = ranges[--size];
517 if (start >= gc->ngpio || start + count > gc->ngpio)
520 bitmap_clear(gc->valid_mask, start, count);
527 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
531 ret = gpiochip_apply_reserved_ranges(gc);
535 if (gc->init_valid_mask)
536 return gc->init_valid_mask(gc,
543 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
545 bitmap_free(gc->valid_mask);
546 gc->valid_mask = NULL;
549 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
552 * Device Tree platforms are supposed to use "gpio-ranges"
553 * property. This check ensures that the ->add_pin_ranges()
554 * won't be called for them.
556 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges"))
559 if (gc->add_pin_ranges)
560 return gc->add_pin_ranges(gc);
565 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
568 /* No mask means all valid */
569 if (likely(!gc->valid_mask))
571 return test_bit(offset, gc->valid_mask);
573 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
575 static void gpiodev_release(struct device *dev)
577 struct gpio_device *gdev = to_gpio_device(dev);
580 spin_lock_irqsave(&gpio_lock, flags);
581 list_del(&gdev->list);
582 spin_unlock_irqrestore(&gpio_lock, flags);
584 ida_free(&gpio_ida, gdev->id);
585 kfree_const(gdev->label);
590 #ifdef CONFIG_GPIO_CDEV
591 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
592 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
595 * gpiolib_cdev_register() indirectly calls device_add(), which is still
596 * required even when cdev is not selected.
598 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
599 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
602 static int gpiochip_setup_dev(struct gpio_device *gdev)
604 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
608 * If fwnode doesn't belong to another device, it's safe to clear its
611 if (fwnode && !fwnode->dev)
612 fwnode_dev_initialized(fwnode, false);
614 ret = gcdev_register(gdev, gpio_devt);
618 /* From this point, the .release() function cleans up gpio_device */
619 gdev->dev.release = gpiodev_release;
621 ret = gpiochip_sysfs_register(gdev);
623 goto err_remove_device;
625 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
626 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
631 gcdev_unregister(gdev);
635 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
637 struct gpio_desc *desc;
640 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
642 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
647 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
650 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
652 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
653 __func__, gc->label, hog->chip_hwnum, rv);
656 static void machine_gpiochip_add(struct gpio_chip *gc)
658 struct gpiod_hog *hog;
660 mutex_lock(&gpio_machine_hogs_mutex);
662 list_for_each_entry(hog, &gpio_machine_hogs, list) {
663 if (!strcmp(gc->label, hog->chip_label))
664 gpiochip_machine_hog(gc, hog);
667 mutex_unlock(&gpio_machine_hogs_mutex);
670 static void gpiochip_setup_devs(void)
672 struct gpio_device *gdev;
675 list_for_each_entry(gdev, &gpio_devices, list) {
676 ret = gpiochip_setup_dev(gdev);
679 "Failed to initialize gpio device (%d)\n", ret);
683 static void gpiochip_set_data(struct gpio_chip *gc, void *data)
685 gc->gpiodev->data = data;
689 * gpiochip_get_data() - get per-subdriver data for the chip
693 * The per-subdriver data for the chip.
695 void *gpiochip_get_data(struct gpio_chip *gc)
697 return gc->gpiodev->data;
699 EXPORT_SYMBOL_GPL(gpiochip_get_data);
701 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
702 struct lock_class_key *lock_key,
703 struct lock_class_key *request_key)
705 struct gpio_device *gdev;
713 * If the calling driver did not initialize firmware node, do it here
714 * using the parent device, if any.
716 if (!gc->fwnode && gc->parent)
717 gc->fwnode = dev_fwnode(gc->parent);
720 * First: allocate and populate the internal stat container, and
721 * set up the struct device.
723 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
726 gdev->dev.bus = &gpio_bus_type;
727 gdev->dev.parent = gc->parent;
731 gpiochip_set_data(gc, data);
733 device_set_node(&gdev->dev, gc->fwnode);
735 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
741 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
745 device_initialize(&gdev->dev);
746 if (gc->parent && gc->parent->driver)
747 gdev->owner = gc->parent->driver->owner;
749 /* TODO: remove chip->owner */
750 gdev->owner = gc->owner;
752 gdev->owner = THIS_MODULE;
755 * Try the device properties if the driver didn't supply the number
760 ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
763 * -ENODATA means that there is no property found and
764 * we want to issue the error message to the user.
765 * Besides that, we want to return different error code
766 * to state that supplied value is not valid.
770 goto err_free_dev_name;
775 if (gc->ngpio == 0) {
776 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
778 goto err_free_dev_name;
781 if (gc->ngpio > FASTPATH_NGPIO)
782 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
783 gc->ngpio, FASTPATH_NGPIO);
785 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
788 goto err_free_dev_name;
791 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
797 gdev->ngpio = gc->ngpio;
799 spin_lock_irqsave(&gpio_lock, flags);
802 * TODO: this allocates a Linux GPIO number base in the global
803 * GPIO numberspace for this chip. In the long run we want to
804 * get *rid* of this numberspace and use only descriptors, but
805 * it may be a pipe dream. It will not happen before we get rid
806 * of the sysfs interface anyways.
810 base = gpiochip_find_base(gc->ngpio);
812 spin_unlock_irqrestore(&gpio_lock, flags);
818 * TODO: it should not be necessary to reflect the assigned
819 * base outside of the GPIO subsystem. Go over drivers and
820 * see if anyone makes use of this, else drop this and assign
826 "Static allocation of GPIO base is deprecated, use dynamic allocation.\n");
830 ret = gpiodev_add_to_list(gdev);
832 spin_unlock_irqrestore(&gpio_lock, flags);
833 chip_err(gc, "GPIO integer space overlap, cannot add chip\n");
837 for (i = 0; i < gc->ngpio; i++)
838 gdev->descs[i].gdev = gdev;
840 spin_unlock_irqrestore(&gpio_lock, flags);
842 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
843 init_rwsem(&gdev->sem);
845 #ifdef CONFIG_PINCTRL
846 INIT_LIST_HEAD(&gdev->pin_ranges);
850 ret = gpiochip_set_desc_names(gc);
852 goto err_remove_from_list;
854 ret = gpiochip_set_names(gc);
856 goto err_remove_from_list;
858 ret = gpiochip_alloc_valid_mask(gc);
860 goto err_remove_from_list;
862 ret = of_gpiochip_add(gc);
864 goto err_free_gpiochip_mask;
866 ret = gpiochip_init_valid_mask(gc);
868 goto err_remove_of_chip;
870 for (i = 0; i < gc->ngpio; i++) {
871 struct gpio_desc *desc = &gdev->descs[i];
873 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
874 assign_bit(FLAG_IS_OUT,
875 &desc->flags, !gc->get_direction(gc, i));
877 assign_bit(FLAG_IS_OUT,
878 &desc->flags, !gc->direction_input);
882 ret = gpiochip_add_pin_ranges(gc);
884 goto err_remove_of_chip;
886 acpi_gpiochip_add(gc);
888 machine_gpiochip_add(gc);
890 ret = gpiochip_irqchip_init_valid_mask(gc);
892 goto err_remove_acpi_chip;
894 ret = gpiochip_irqchip_init_hw(gc);
896 goto err_remove_acpi_chip;
898 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
900 goto err_remove_irqchip_mask;
903 * By first adding the chardev, and then adding the device,
904 * we get a device node entry in sysfs under
905 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
906 * coldplug of device nodes and other udev business.
907 * We can do this only if gpiolib has been initialized.
908 * Otherwise, defer until later.
910 if (gpiolib_initialized) {
911 ret = gpiochip_setup_dev(gdev);
913 goto err_remove_irqchip;
918 gpiochip_irqchip_remove(gc);
919 err_remove_irqchip_mask:
920 gpiochip_irqchip_free_valid_mask(gc);
921 err_remove_acpi_chip:
922 acpi_gpiochip_remove(gc);
924 gpiochip_free_hogs(gc);
925 of_gpiochip_remove(gc);
926 err_free_gpiochip_mask:
927 gpiochip_remove_pin_ranges(gc);
928 gpiochip_free_valid_mask(gc);
929 if (gdev->dev.release) {
930 /* release() has been registered by gpiochip_setup_dev() */
931 gpio_device_put(gdev);
932 goto err_print_message;
934 err_remove_from_list:
935 spin_lock_irqsave(&gpio_lock, flags);
936 list_del(&gdev->list);
937 spin_unlock_irqrestore(&gpio_lock, flags);
939 kfree_const(gdev->label);
943 kfree(dev_name(&gdev->dev));
945 ida_free(&gpio_ida, gdev->id);
949 /* failures here can mean systems won't boot... */
950 if (ret != -EPROBE_DEFER) {
951 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
952 base, base + (int)ngpios - 1,
953 gc->label ? : "generic", ret);
957 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
960 * gpiochip_remove() - unregister a gpio_chip
961 * @gc: the chip to unregister
963 * A gpio_chip with any GPIOs still requested may not be removed.
965 void gpiochip_remove(struct gpio_chip *gc)
967 struct gpio_device *gdev = gc->gpiodev;
971 down_write(&gdev->sem);
973 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
974 gpiochip_sysfs_unregister(gdev);
975 gpiochip_free_hogs(gc);
976 /* Numb the device, cancelling all outstanding operations */
978 gpiochip_irqchip_remove(gc);
979 acpi_gpiochip_remove(gc);
980 of_gpiochip_remove(gc);
981 gpiochip_remove_pin_ranges(gc);
982 gpiochip_free_valid_mask(gc);
984 * We accept no more calls into the driver from this point, so
985 * NULL the driver data pointer.
987 gpiochip_set_data(gc, NULL);
989 spin_lock_irqsave(&gpio_lock, flags);
990 for (i = 0; i < gdev->ngpio; i++) {
991 if (gpiochip_is_requested(gc, i))
994 spin_unlock_irqrestore(&gpio_lock, flags);
996 if (i != gdev->ngpio)
998 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1001 * The gpiochip side puts its use of the device to rest here:
1002 * if there are no userspace clients, the chardev and device will
1003 * be removed, else it will be dangling until the last user is
1006 gcdev_unregister(gdev);
1007 up_write(&gdev->sem);
1008 gpio_device_put(gdev);
1010 EXPORT_SYMBOL_GPL(gpiochip_remove);
1013 * gpiochip_find() - iterator for locating a specific gpio_chip
1014 * @data: data to pass to match function
1015 * @match: Callback function to check gpio_chip
1017 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1018 * determined by a user supplied @match callback. The callback should return
1019 * 0 if the device doesn't match and non-zero if it does. If the callback is
1020 * non-zero, this function will return to the caller and not iterate over any
1023 struct gpio_chip *gpiochip_find(void *data,
1024 int (*match)(struct gpio_chip *gc,
1027 struct gpio_device *gdev;
1028 struct gpio_chip *gc = NULL;
1029 unsigned long flags;
1031 spin_lock_irqsave(&gpio_lock, flags);
1032 list_for_each_entry(gdev, &gpio_devices, list)
1033 if (gdev->chip && match(gdev->chip, data)) {
1038 spin_unlock_irqrestore(&gpio_lock, flags);
1042 EXPORT_SYMBOL_GPL(gpiochip_find);
1044 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
1046 const char *name = data;
1048 return !strcmp(gc->label, name);
1051 static struct gpio_chip *find_chip_by_name(const char *name)
1053 return gpiochip_find((void *)name, gpiochip_match_name);
1056 #ifdef CONFIG_GPIOLIB_IRQCHIP
1059 * The following is irqchip helper code for gpiochips.
1062 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1064 struct gpio_irq_chip *girq = &gc->irq;
1069 return girq->init_hw(gc);
1072 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1074 struct gpio_irq_chip *girq = &gc->irq;
1076 if (!girq->init_valid_mask)
1079 girq->valid_mask = gpiochip_allocate_mask(gc);
1080 if (!girq->valid_mask)
1083 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1088 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1090 bitmap_free(gc->irq.valid_mask);
1091 gc->irq.valid_mask = NULL;
1094 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1095 unsigned int offset)
1097 if (!gpiochip_line_is_valid(gc, offset))
1099 /* No mask means all valid */
1100 if (likely(!gc->irq.valid_mask))
1102 return test_bit(offset, gc->irq.valid_mask);
1104 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1106 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1109 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1111 * @gc: the gpiochip to set the irqchip hierarchical handler to
1112 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1113 * will then percolate up to the parent
1115 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1116 struct irq_chip *irqchip)
1118 /* DT will deal with mapping each IRQ as we go along */
1119 if (is_of_node(gc->irq.fwnode))
1123 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1124 * irqs upfront instead of dynamically since we don't have the
1125 * dynamic type of allocation that hardware description languages
1126 * provide. Once all GPIO drivers using board files are gone from
1127 * the kernel we can delete this code, but for a transitional period
1128 * it is necessary to keep this around.
1130 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1134 for (i = 0; i < gc->ngpio; i++) {
1135 struct irq_fwspec fwspec;
1136 unsigned int parent_hwirq;
1137 unsigned int parent_type;
1138 struct gpio_irq_chip *girq = &gc->irq;
1141 * We call the child to parent translation function
1142 * only to check if the child IRQ is valid or not.
1143 * Just pick the rising edge type here as that is what
1144 * we likely need to support.
1146 ret = girq->child_to_parent_hwirq(gc, i,
1147 IRQ_TYPE_EDGE_RISING,
1151 chip_err(gc, "skip set-up on hwirq %d\n",
1156 fwspec.fwnode = gc->irq.fwnode;
1157 /* This is the hwirq for the GPIO line side of things */
1158 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1159 /* Just pick something */
1160 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1161 fwspec.param_count = 2;
1162 ret = irq_domain_alloc_irqs(gc->irq.domain, 1,
1163 NUMA_NO_NODE, &fwspec);
1166 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1173 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1178 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1179 struct irq_fwspec *fwspec,
1180 unsigned long *hwirq,
1183 /* We support standard DT translation */
1184 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1185 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1188 /* This is for board files and others not using DT */
1189 if (is_fwnode_irqchip(fwspec->fwnode)) {
1192 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1195 WARN_ON(*type == IRQ_TYPE_NONE);
1201 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1203 unsigned int nr_irqs,
1206 struct gpio_chip *gc = d->host_data;
1207 irq_hw_number_t hwirq;
1208 unsigned int type = IRQ_TYPE_NONE;
1209 struct irq_fwspec *fwspec = data;
1210 union gpio_irq_fwspec gpio_parent_fwspec = {};
1211 unsigned int parent_hwirq;
1212 unsigned int parent_type;
1213 struct gpio_irq_chip *girq = &gc->irq;
1217 * The nr_irqs parameter is always one except for PCI multi-MSI
1218 * so this should not happen.
1220 WARN_ON(nr_irqs != 1);
1222 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1226 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1228 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1229 &parent_hwirq, &parent_type);
1231 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1234 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1237 * We set handle_bad_irq because the .set_type() should
1238 * always be invoked and set the right type of handler.
1240 irq_domain_set_info(d,
1249 /* This parent only handles asserted level IRQs */
1250 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
1251 parent_hwirq, parent_type);
1255 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1257 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1258 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
1260 * If the parent irqdomain is msi, the interrupts have already
1261 * been allocated, so the EEXIST is good.
1263 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1267 "failed to allocate parent hwirq %d for hwirq %lu\n",
1268 parent_hwirq, hwirq);
1273 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1274 unsigned int offset)
1279 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1281 ops->activate = gpiochip_irq_domain_activate;
1282 ops->deactivate = gpiochip_irq_domain_deactivate;
1283 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1286 * We only allow overriding the translate() and free() functions for
1287 * hierarchical chips, and this should only be done if the user
1288 * really need something other than 1:1 translation for translate()
1289 * callback and free if user wants to free up any resources which
1290 * were allocated during callbacks, for example populate_parent_alloc_arg.
1292 if (!ops->translate)
1293 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1295 ops->free = irq_domain_free_irqs_common;
1298 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1300 if (!gc->irq.child_to_parent_hwirq ||
1302 chip_err(gc, "missing irqdomain vital data\n");
1306 if (!gc->irq.child_offset_to_irq)
1307 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1309 if (!gc->irq.populate_parent_alloc_arg)
1310 gc->irq.populate_parent_alloc_arg =
1311 gpiochip_populate_parent_fwspec_twocell;
1313 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1315 gc->irq.domain = irq_domain_create_hierarchy(
1316 gc->irq.parent_domain,
1320 &gc->irq.child_irq_domain_ops,
1323 if (!gc->irq.domain)
1326 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1331 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1333 return !!gc->irq.parent_domain;
1336 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1337 union gpio_irq_fwspec *gfwspec,
1338 unsigned int parent_hwirq,
1339 unsigned int parent_type)
1341 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1343 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1344 fwspec->param_count = 2;
1345 fwspec->param[0] = parent_hwirq;
1346 fwspec->param[1] = parent_type;
1350 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1352 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1353 union gpio_irq_fwspec *gfwspec,
1354 unsigned int parent_hwirq,
1355 unsigned int parent_type)
1357 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1359 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1360 fwspec->param_count = 4;
1361 fwspec->param[0] = 0;
1362 fwspec->param[1] = parent_hwirq;
1363 fwspec->param[2] = 0;
1364 fwspec->param[3] = parent_type;
1368 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1372 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1377 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1382 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1385 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1386 * @d: the irqdomain used by this irqchip
1387 * @irq: the global irq number used by this GPIO irqchip irq
1388 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1390 * This function will set up the mapping for a certain IRQ line on a
1391 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1392 * stored inside the gpiochip.
1394 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hwirq)
1396 struct gpio_chip *gc = d->host_data;
1399 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1402 irq_set_chip_data(irq, gc);
1404 * This lock class tells lockdep that GPIO irqs are in a different
1405 * category than their parents, so it won't report false recursion.
1407 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1408 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1409 /* Chips that use nested thread handlers have them marked */
1410 if (gc->irq.threaded)
1411 irq_set_nested_thread(irq, 1);
1412 irq_set_noprobe(irq);
1414 if (gc->irq.num_parents == 1)
1415 ret = irq_set_parent(irq, gc->irq.parents[0]);
1416 else if (gc->irq.map)
1417 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1423 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1424 * is passed as default type.
1426 if (gc->irq.default_type != IRQ_TYPE_NONE)
1427 irq_set_irq_type(irq, gc->irq.default_type);
1431 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1433 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1435 struct gpio_chip *gc = d->host_data;
1437 if (gc->irq.threaded)
1438 irq_set_nested_thread(irq, 0);
1439 irq_set_chip_and_handler(irq, NULL, NULL);
1440 irq_set_chip_data(irq, NULL);
1442 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1444 static const struct irq_domain_ops gpiochip_domain_ops = {
1445 .map = gpiochip_irq_map,
1446 .unmap = gpiochip_irq_unmap,
1447 /* Virtually all GPIO irqchips are twocell:ed */
1448 .xlate = irq_domain_xlate_twocell,
1452 * TODO: move these activate/deactivate in under the hierarchicial
1453 * irqchip implementation as static once SPMI and SSBI (all external
1454 * users) are phased over.
1457 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1458 * @domain: The IRQ domain used by this IRQ chip
1459 * @data: Outermost irq_data associated with the IRQ
1460 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1462 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1463 * used as the activate function for the &struct irq_domain_ops. The host_data
1464 * for the IRQ domain must be the &struct gpio_chip.
1466 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1467 struct irq_data *data, bool reserve)
1469 struct gpio_chip *gc = domain->host_data;
1470 unsigned int hwirq = irqd_to_hwirq(data);
1472 return gpiochip_lock_as_irq(gc, hwirq);
1474 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1477 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1478 * @domain: The IRQ domain used by this IRQ chip
1479 * @data: Outermost irq_data associated with the IRQ
1481 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1482 * be used as the deactivate function for the &struct irq_domain_ops. The
1483 * host_data for the IRQ domain must be the &struct gpio_chip.
1485 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1486 struct irq_data *data)
1488 struct gpio_chip *gc = domain->host_data;
1489 unsigned int hwirq = irqd_to_hwirq(data);
1491 return gpiochip_unlock_as_irq(gc, hwirq);
1493 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1495 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1497 struct irq_domain *domain = gc->irq.domain;
1499 #ifdef CONFIG_GPIOLIB_IRQCHIP
1501 * Avoid race condition with other code, which tries to lookup
1502 * an IRQ before the irqchip has been properly registered,
1503 * i.e. while gpiochip is still being brought up.
1505 if (!gc->irq.initialized)
1506 return -EPROBE_DEFER;
1509 if (!gpiochip_irqchip_irq_valid(gc, offset))
1512 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1513 if (irq_domain_is_hierarchy(domain)) {
1514 struct irq_fwspec spec;
1516 spec.fwnode = domain->fwnode;
1517 spec.param_count = 2;
1518 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1519 spec.param[1] = IRQ_TYPE_NONE;
1521 return irq_create_fwspec_mapping(&spec);
1525 return irq_create_mapping(domain, offset);
1528 int gpiochip_irq_reqres(struct irq_data *d)
1530 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1531 unsigned int hwirq = irqd_to_hwirq(d);
1533 return gpiochip_reqres_irq(gc, hwirq);
1535 EXPORT_SYMBOL(gpiochip_irq_reqres);
1537 void gpiochip_irq_relres(struct irq_data *d)
1539 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1540 unsigned int hwirq = irqd_to_hwirq(d);
1542 gpiochip_relres_irq(gc, hwirq);
1544 EXPORT_SYMBOL(gpiochip_irq_relres);
1546 static void gpiochip_irq_mask(struct irq_data *d)
1548 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1549 unsigned int hwirq = irqd_to_hwirq(d);
1551 if (gc->irq.irq_mask)
1552 gc->irq.irq_mask(d);
1553 gpiochip_disable_irq(gc, hwirq);
1556 static void gpiochip_irq_unmask(struct irq_data *d)
1558 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1559 unsigned int hwirq = irqd_to_hwirq(d);
1561 gpiochip_enable_irq(gc, hwirq);
1562 if (gc->irq.irq_unmask)
1563 gc->irq.irq_unmask(d);
1566 static void gpiochip_irq_enable(struct irq_data *d)
1568 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1569 unsigned int hwirq = irqd_to_hwirq(d);
1571 gpiochip_enable_irq(gc, hwirq);
1572 gc->irq.irq_enable(d);
1575 static void gpiochip_irq_disable(struct irq_data *d)
1577 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1578 unsigned int hwirq = irqd_to_hwirq(d);
1580 gc->irq.irq_disable(d);
1581 gpiochip_disable_irq(gc, hwirq);
1584 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1586 struct irq_chip *irqchip = gc->irq.chip;
1588 if (irqchip->flags & IRQCHIP_IMMUTABLE)
1591 chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
1593 if (!irqchip->irq_request_resources &&
1594 !irqchip->irq_release_resources) {
1595 irqchip->irq_request_resources = gpiochip_irq_reqres;
1596 irqchip->irq_release_resources = gpiochip_irq_relres;
1598 if (WARN_ON(gc->irq.irq_enable))
1600 /* Check if the irqchip already has this hook... */
1601 if (irqchip->irq_enable == gpiochip_irq_enable ||
1602 irqchip->irq_mask == gpiochip_irq_mask) {
1604 * ...and if so, give a gentle warning that this is bad
1608 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1612 if (irqchip->irq_disable) {
1613 gc->irq.irq_disable = irqchip->irq_disable;
1614 irqchip->irq_disable = gpiochip_irq_disable;
1616 gc->irq.irq_mask = irqchip->irq_mask;
1617 irqchip->irq_mask = gpiochip_irq_mask;
1620 if (irqchip->irq_enable) {
1621 gc->irq.irq_enable = irqchip->irq_enable;
1622 irqchip->irq_enable = gpiochip_irq_enable;
1624 gc->irq.irq_unmask = irqchip->irq_unmask;
1625 irqchip->irq_unmask = gpiochip_irq_unmask;
1630 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1631 * @gc: the GPIO chip to add the IRQ chip to
1632 * @lock_key: lockdep class for IRQ lock
1633 * @request_key: lockdep class for IRQ request
1635 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1636 struct lock_class_key *lock_key,
1637 struct lock_class_key *request_key)
1639 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1640 struct irq_chip *irqchip = gc->irq.chip;
1647 if (gc->irq.parent_handler && gc->can_sleep) {
1648 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1652 type = gc->irq.default_type;
1655 * Specifying a default trigger is a terrible idea if DT or ACPI is
1656 * used to configure the interrupts, as you may end up with
1657 * conflicting triggers. Tell the user, and reset to NONE.
1659 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1660 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1661 type = IRQ_TYPE_NONE;
1664 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1666 gc->to_irq = gpiochip_to_irq;
1667 gc->irq.default_type = type;
1668 gc->irq.lock_key = lock_key;
1669 gc->irq.request_key = request_key;
1671 /* If a parent irqdomain is provided, let's build a hierarchy */
1672 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1673 int ret = gpiochip_hierarchy_add_domain(gc);
1677 /* Some drivers provide custom irqdomain ops */
1678 gc->irq.domain = irq_domain_create_simple(fwnode,
1681 gc->irq.domain_ops ?: &gpiochip_domain_ops,
1683 if (!gc->irq.domain)
1687 if (gc->irq.parent_handler) {
1688 for (i = 0; i < gc->irq.num_parents; i++) {
1691 if (gc->irq.per_parent_data)
1692 data = gc->irq.parent_handler_data_array[i];
1694 data = gc->irq.parent_handler_data ?: gc;
1697 * The parent IRQ chip is already using the chip_data
1698 * for this IRQ chip, so our callbacks simply use the
1701 irq_set_chained_handler_and_data(gc->irq.parents[i],
1702 gc->irq.parent_handler,
1707 gpiochip_set_irq_hooks(gc);
1710 * Using barrier() here to prevent compiler from reordering
1711 * gc->irq.initialized before initialization of above
1712 * GPIO chip irq members.
1716 gc->irq.initialized = true;
1718 acpi_gpiochip_request_interrupts(gc);
1724 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1725 * @gc: the gpiochip to remove the irqchip from
1727 * This is called only from gpiochip_remove()
1729 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1731 struct irq_chip *irqchip = gc->irq.chip;
1732 unsigned int offset;
1734 acpi_gpiochip_free_interrupts(gc);
1736 if (irqchip && gc->irq.parent_handler) {
1737 struct gpio_irq_chip *irq = &gc->irq;
1740 for (i = 0; i < irq->num_parents; i++)
1741 irq_set_chained_handler_and_data(irq->parents[i],
1745 /* Remove all IRQ mappings and delete the domain */
1746 if (gc->irq.domain) {
1749 for (offset = 0; offset < gc->ngpio; offset++) {
1750 if (!gpiochip_irqchip_irq_valid(gc, offset))
1753 irq = irq_find_mapping(gc->irq.domain, offset);
1754 irq_dispose_mapping(irq);
1757 irq_domain_remove(gc->irq.domain);
1760 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
1761 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1762 irqchip->irq_request_resources = NULL;
1763 irqchip->irq_release_resources = NULL;
1765 if (irqchip->irq_enable == gpiochip_irq_enable) {
1766 irqchip->irq_enable = gc->irq.irq_enable;
1767 irqchip->irq_disable = gc->irq.irq_disable;
1770 gc->irq.irq_enable = NULL;
1771 gc->irq.irq_disable = NULL;
1772 gc->irq.chip = NULL;
1774 gpiochip_irqchip_free_valid_mask(gc);
1778 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1779 * @gc: the gpiochip to add the irqchip to
1780 * @domain: the irqdomain to add to the gpiochip
1782 * This function adds an IRQ domain to the gpiochip.
1784 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1785 struct irq_domain *domain)
1790 gc->to_irq = gpiochip_to_irq;
1791 gc->irq.domain = domain;
1795 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1797 #else /* CONFIG_GPIOLIB_IRQCHIP */
1799 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1800 struct lock_class_key *lock_key,
1801 struct lock_class_key *request_key)
1805 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1807 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1812 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1816 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1819 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1822 * gpiochip_generic_request() - request the gpio function for a pin
1823 * @gc: the gpiochip owning the GPIO
1824 * @offset: the offset of the GPIO to request for GPIO function
1826 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
1828 #ifdef CONFIG_PINCTRL
1829 if (list_empty(&gc->gpiodev->pin_ranges))
1833 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1835 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1838 * gpiochip_generic_free() - free the gpio function from a pin
1839 * @gc: the gpiochip to request the gpio function for
1840 * @offset: the offset of the GPIO to free from GPIO function
1842 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
1844 #ifdef CONFIG_PINCTRL
1845 if (list_empty(&gc->gpiodev->pin_ranges))
1849 pinctrl_gpio_free(gc->gpiodev->base + offset);
1851 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1854 * gpiochip_generic_config() - apply configuration for a pin
1855 * @gc: the gpiochip owning the GPIO
1856 * @offset: the offset of the GPIO to apply the configuration
1857 * @config: the configuration to be applied
1859 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
1860 unsigned long config)
1862 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1864 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1866 #ifdef CONFIG_PINCTRL
1869 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1870 * @gc: the gpiochip to add the range for
1871 * @pctldev: the pin controller to map to
1872 * @gpio_offset: the start offset in the current gpio_chip number space
1873 * @pin_group: name of the pin group inside the pin controller
1875 * Calling this function directly from a DeviceTree-supported
1876 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1877 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1878 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1880 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1881 struct pinctrl_dev *pctldev,
1882 unsigned int gpio_offset, const char *pin_group)
1884 struct gpio_pin_range *pin_range;
1885 struct gpio_device *gdev = gc->gpiodev;
1888 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1890 chip_err(gc, "failed to allocate pin ranges\n");
1894 /* Use local offset as range ID */
1895 pin_range->range.id = gpio_offset;
1896 pin_range->range.gc = gc;
1897 pin_range->range.name = gc->label;
1898 pin_range->range.base = gdev->base + gpio_offset;
1899 pin_range->pctldev = pctldev;
1901 ret = pinctrl_get_group_pins(pctldev, pin_group,
1902 &pin_range->range.pins,
1903 &pin_range->range.npins);
1909 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1911 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1912 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1913 pinctrl_dev_get_devname(pctldev), pin_group);
1915 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1919 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1922 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1923 * @gc: the gpiochip to add the range for
1924 * @pinctl_name: the dev_name() of the pin controller to map to
1925 * @gpio_offset: the start offset in the current gpio_chip number space
1926 * @pin_offset: the start offset in the pin controller number space
1927 * @npins: the number of pins from the offset of each pin space (GPIO and
1928 * pin controller) to accumulate in this range
1931 * 0 on success, or a negative error-code on failure.
1933 * Calling this function directly from a DeviceTree-supported
1934 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1935 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1936 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1938 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1939 unsigned int gpio_offset, unsigned int pin_offset,
1942 struct gpio_pin_range *pin_range;
1943 struct gpio_device *gdev = gc->gpiodev;
1946 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1948 chip_err(gc, "failed to allocate pin ranges\n");
1952 /* Use local offset as range ID */
1953 pin_range->range.id = gpio_offset;
1954 pin_range->range.gc = gc;
1955 pin_range->range.name = gc->label;
1956 pin_range->range.base = gdev->base + gpio_offset;
1957 pin_range->range.pin_base = pin_offset;
1958 pin_range->range.npins = npins;
1959 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1961 if (IS_ERR(pin_range->pctldev)) {
1962 ret = PTR_ERR(pin_range->pctldev);
1963 chip_err(gc, "could not create pin range\n");
1967 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1968 gpio_offset, gpio_offset + npins - 1,
1970 pin_offset, pin_offset + npins - 1);
1972 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1976 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1979 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1980 * @gc: the chip to remove all the mappings for
1982 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1984 struct gpio_pin_range *pin_range, *tmp;
1985 struct gpio_device *gdev = gc->gpiodev;
1987 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1988 list_del(&pin_range->node);
1989 pinctrl_remove_gpio_range(pin_range->pctldev,
1994 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1996 #endif /* CONFIG_PINCTRL */
1998 /* These "optional" allocation calls help prevent drivers from stomping
1999 * on each other, and help provide better diagnostics in debugfs.
2000 * They're called even less than the "set direction" calls.
2002 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2004 struct gpio_chip *gc = desc->gdev->chip;
2006 unsigned long flags;
2010 label = kstrdup_const(label, GFP_KERNEL);
2015 spin_lock_irqsave(&gpio_lock, flags);
2017 /* NOTE: gpio_request() can be called in early boot,
2018 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2021 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2022 desc_set_label(desc, label ? : "?");
2025 goto out_free_unlock;
2029 /* gc->request may sleep */
2030 spin_unlock_irqrestore(&gpio_lock, flags);
2031 offset = gpio_chip_hwgpio(desc);
2032 if (gpiochip_line_is_valid(gc, offset))
2033 ret = gc->request(gc, offset);
2036 spin_lock_irqsave(&gpio_lock, flags);
2039 desc_set_label(desc, NULL);
2040 clear_bit(FLAG_REQUESTED, &desc->flags);
2041 goto out_free_unlock;
2044 if (gc->get_direction) {
2045 /* gc->get_direction may sleep */
2046 spin_unlock_irqrestore(&gpio_lock, flags);
2047 gpiod_get_direction(desc);
2048 spin_lock_irqsave(&gpio_lock, flags);
2050 spin_unlock_irqrestore(&gpio_lock, flags);
2054 spin_unlock_irqrestore(&gpio_lock, flags);
2060 * This descriptor validation needs to be inserted verbatim into each
2061 * function taking a descriptor, so we need to use a preprocessor
2062 * macro to avoid endless duplication. If the desc is NULL it is an
2063 * optional GPIO and calls should just bail out.
2065 static int validate_desc(const struct gpio_desc *desc, const char *func)
2070 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2071 return PTR_ERR(desc);
2074 pr_warn("%s: invalid GPIO (no device)\n", func);
2077 if (!desc->gdev->chip) {
2078 dev_warn(&desc->gdev->dev,
2079 "%s: backing chip is gone\n", func);
2085 #define VALIDATE_DESC(desc) do { \
2086 int __valid = validate_desc(desc, __func__); \
2091 #define VALIDATE_DESC_VOID(desc) do { \
2092 int __valid = validate_desc(desc, __func__); \
2097 int gpiod_request(struct gpio_desc *desc, const char *label)
2099 int ret = -EPROBE_DEFER;
2101 VALIDATE_DESC(desc);
2103 if (try_module_get(desc->gdev->owner)) {
2104 ret = gpiod_request_commit(desc, label);
2106 module_put(desc->gdev->owner);
2108 gpio_device_get(desc->gdev);
2112 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2117 static bool gpiod_free_commit(struct gpio_desc *desc)
2120 unsigned long flags;
2121 struct gpio_chip *gc;
2125 gpiod_unexport(desc);
2127 spin_lock_irqsave(&gpio_lock, flags);
2129 gc = desc->gdev->chip;
2130 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2132 spin_unlock_irqrestore(&gpio_lock, flags);
2133 might_sleep_if(gc->can_sleep);
2134 gc->free(gc, gpio_chip_hwgpio(desc));
2135 spin_lock_irqsave(&gpio_lock, flags);
2137 kfree_const(desc->label);
2138 desc_set_label(desc, NULL);
2139 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2140 clear_bit(FLAG_REQUESTED, &desc->flags);
2141 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2142 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2143 clear_bit(FLAG_PULL_UP, &desc->flags);
2144 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2145 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2146 clear_bit(FLAG_EDGE_RISING, &desc->flags);
2147 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
2148 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2149 #ifdef CONFIG_OF_DYNAMIC
2152 #ifdef CONFIG_GPIO_CDEV
2153 WRITE_ONCE(desc->debounce_period_us, 0);
2158 spin_unlock_irqrestore(&gpio_lock, flags);
2159 blocking_notifier_call_chain(&desc->gdev->notifier,
2160 GPIOLINE_CHANGED_RELEASED, desc);
2165 void gpiod_free(struct gpio_desc *desc)
2167 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2168 module_put(desc->gdev->owner);
2169 gpio_device_put(desc->gdev);
2171 WARN_ON(extra_checks);
2176 * gpiochip_is_requested - return string iff signal was requested
2177 * @gc: controller managing the signal
2178 * @offset: of signal within controller's 0..(ngpio - 1) range
2180 * Returns NULL if the GPIO is not currently requested, else a string.
2181 * The string returned is the label passed to gpio_request(); if none has been
2182 * passed it is a meaningless, non-NULL constant.
2184 * This function is for use by GPIO controller drivers. The label can
2185 * help with diagnostics, and knowing that the signal is used as a GPIO
2186 * can help avoid accidentally multiplexing it to another controller.
2188 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
2190 struct gpio_desc *desc;
2192 desc = gpiochip_get_desc(gc, offset);
2196 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2200 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2203 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2205 * @hwnum: hardware number of the GPIO for which to request the descriptor
2206 * @label: label for the GPIO
2207 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2208 * specify things like line inversion semantics with the machine flags
2209 * such as GPIO_OUT_LOW
2210 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2211 * can be used to specify consumer semantics such as open drain
2213 * Function allows GPIO chip drivers to request and use their own GPIO
2214 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2215 * function will not increase reference count of the GPIO chip module. This
2216 * allows the GPIO chip module to be unloaded as needed (we assume that the
2217 * GPIO chip driver handles freeing the GPIOs it has requested).
2220 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2223 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2226 enum gpio_lookup_flags lflags,
2227 enum gpiod_flags dflags)
2229 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2233 chip_err(gc, "failed to get GPIO descriptor\n");
2237 ret = gpiod_request_commit(desc, label);
2239 return ERR_PTR(ret);
2241 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2243 chip_err(gc, "setup of own GPIO %s failed\n", label);
2244 gpiod_free_commit(desc);
2245 return ERR_PTR(ret);
2250 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2253 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2254 * @desc: GPIO descriptor to free
2256 * Function frees the given GPIO requested previously with
2257 * gpiochip_request_own_desc().
2259 void gpiochip_free_own_desc(struct gpio_desc *desc)
2262 gpiod_free_commit(desc);
2264 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2267 * Drivers MUST set GPIO direction before making get/set calls. In
2268 * some cases this is done in early boot, before IRQs are enabled.
2270 * As a rule these aren't called more than once (except for drivers
2271 * using the open-drain emulation idiom) so these are natural places
2272 * to accumulate extra debugging checks. Note that we can't (yet)
2273 * rely on gpio_request() having been called beforehand.
2276 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2277 unsigned long config)
2279 if (!gc->set_config)
2282 return gc->set_config(gc, offset, config);
2285 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2286 enum pin_config_param mode,
2289 struct gpio_chip *gc = desc->gdev->chip;
2290 unsigned long config;
2292 config = pinconf_to_config_packed(mode, argument);
2293 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2296 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2297 enum pin_config_param mode,
2300 struct device *dev = &desc->gdev->dev;
2301 int gpio = gpio_chip_hwgpio(desc);
2304 ret = gpio_set_config_with_argument(desc, mode, argument);
2305 if (ret != -ENOTSUPP)
2309 case PIN_CONFIG_PERSIST_STATE:
2310 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2319 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2321 return gpio_set_config_with_argument(desc, mode, 0);
2324 static int gpio_set_bias(struct gpio_desc *desc)
2326 enum pin_config_param bias;
2329 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2330 bias = PIN_CONFIG_BIAS_DISABLE;
2331 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2332 bias = PIN_CONFIG_BIAS_PULL_UP;
2333 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2334 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2339 case PIN_CONFIG_BIAS_PULL_DOWN:
2340 case PIN_CONFIG_BIAS_PULL_UP:
2349 return gpio_set_config_with_argument_optional(desc, bias, arg);
2353 * gpio_set_debounce_timeout() - Set debounce timeout
2354 * @desc: GPIO descriptor to set the debounce timeout
2355 * @debounce: Debounce timeout in microseconds
2357 * The function calls the certain GPIO driver to set debounce timeout
2360 * Returns 0 on success, or negative error code otherwise.
2362 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2364 return gpio_set_config_with_argument_optional(desc,
2365 PIN_CONFIG_INPUT_DEBOUNCE,
2370 * gpiod_direction_input - set the GPIO direction to input
2371 * @desc: GPIO to set to input
2373 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2374 * be called safely on it.
2376 * Return 0 in case of success, else an error code.
2378 int gpiod_direction_input(struct gpio_desc *desc)
2380 struct gpio_chip *gc;
2383 VALIDATE_DESC(desc);
2384 gc = desc->gdev->chip;
2387 * It is legal to have no .get() and .direction_input() specified if
2388 * the chip is output-only, but you can't specify .direction_input()
2389 * and not support the .get() operation, that doesn't make sense.
2391 if (!gc->get && gc->direction_input) {
2393 "%s: missing get() but have direction_input()\n",
2399 * If we have a .direction_input() callback, things are simple,
2400 * just call it. Else we are some input-only chip so try to check the
2401 * direction (if .get_direction() is supported) else we silently
2402 * assume we are in input mode after this.
2404 if (gc->direction_input) {
2405 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2406 } else if (gc->get_direction &&
2407 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2409 "%s: missing direction_input() operation and line is output\n",
2414 clear_bit(FLAG_IS_OUT, &desc->flags);
2415 ret = gpio_set_bias(desc);
2418 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2422 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2424 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2426 struct gpio_chip *gc = desc->gdev->chip;
2431 * It's OK not to specify .direction_output() if the gpiochip is
2432 * output-only, but if there is then not even a .set() operation it
2433 * is pretty tricky to drive the output line.
2435 if (!gc->set && !gc->direction_output) {
2437 "%s: missing set() and direction_output() operations\n",
2442 if (gc->direction_output) {
2443 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2445 /* Check that we are in output mode if we can */
2446 if (gc->get_direction &&
2447 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2449 "%s: missing direction_output() operation\n",
2454 * If we can't actively set the direction, we are some
2455 * output-only chip, so just drive the output as desired.
2457 gc->set(gc, gpio_chip_hwgpio(desc), val);
2461 set_bit(FLAG_IS_OUT, &desc->flags);
2462 trace_gpio_value(desc_to_gpio(desc), 0, val);
2463 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2468 * gpiod_direction_output_raw - set the GPIO direction to output
2469 * @desc: GPIO to set to output
2470 * @value: initial output value of the GPIO
2472 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2473 * be called safely on it. The initial value of the output must be specified
2474 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2476 * Return 0 in case of success, else an error code.
2478 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2480 VALIDATE_DESC(desc);
2481 return gpiod_direction_output_raw_commit(desc, value);
2483 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2486 * gpiod_direction_output - set the GPIO direction to output
2487 * @desc: GPIO to set to output
2488 * @value: initial output value of the GPIO
2490 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2491 * be called safely on it. The initial value of the output must be specified
2492 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2495 * Return 0 in case of success, else an error code.
2497 int gpiod_direction_output(struct gpio_desc *desc, int value)
2501 VALIDATE_DESC(desc);
2502 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2507 /* GPIOs used for enabled IRQs shall not be set as output */
2508 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2509 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2511 "%s: tried to set a GPIO tied to an IRQ as output\n",
2516 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2517 /* First see if we can enable open drain in hardware */
2518 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2520 goto set_output_value;
2521 /* Emulate open drain by not actively driving the line high */
2523 ret = gpiod_direction_input(desc);
2524 goto set_output_flag;
2526 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2527 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2529 goto set_output_value;
2530 /* Emulate open source by not actively driving the line low */
2532 ret = gpiod_direction_input(desc);
2533 goto set_output_flag;
2536 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2540 ret = gpio_set_bias(desc);
2543 return gpiod_direction_output_raw_commit(desc, value);
2547 * When emulating open-source or open-drain functionalities by not
2548 * actively driving the line (setting mode to input) we still need to
2549 * set the IS_OUT flag or otherwise we won't be able to set the line
2553 set_bit(FLAG_IS_OUT, &desc->flags);
2556 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2559 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2561 * @desc: GPIO to enable.
2562 * @flags: Flags related to GPIO edge.
2564 * Return 0 in case of success, else negative error code.
2566 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2569 struct gpio_chip *gc;
2571 VALIDATE_DESC(desc);
2573 gc = desc->gdev->chip;
2574 if (!gc->en_hw_timestamp) {
2575 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2579 ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2581 gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
2585 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
2588 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2590 * @desc: GPIO to disable.
2591 * @flags: Flags related to GPIO edge, same value as used during enable call.
2593 * Return 0 in case of success, else negative error code.
2595 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2598 struct gpio_chip *gc;
2600 VALIDATE_DESC(desc);
2602 gc = desc->gdev->chip;
2603 if (!gc->dis_hw_timestamp) {
2604 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2608 ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2610 gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
2614 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
2617 * gpiod_set_config - sets @config for a GPIO
2618 * @desc: descriptor of the GPIO for which to set the configuration
2619 * @config: Same packed config format as generic pinconf
2622 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2625 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2627 struct gpio_chip *gc;
2629 VALIDATE_DESC(desc);
2630 gc = desc->gdev->chip;
2632 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2634 EXPORT_SYMBOL_GPL(gpiod_set_config);
2637 * gpiod_set_debounce - sets @debounce time for a GPIO
2638 * @desc: descriptor of the GPIO for which to set debounce time
2639 * @debounce: debounce time in microseconds
2642 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2645 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2647 unsigned long config;
2649 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2650 return gpiod_set_config(desc, config);
2652 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2655 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2656 * @desc: descriptor of the GPIO for which to configure persistence
2657 * @transitory: True to lose state on suspend or reset, false for persistence
2660 * 0 on success, otherwise a negative error code.
2662 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2664 VALIDATE_DESC(desc);
2666 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2667 * persistence state.
2669 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2671 /* If the driver supports it, set the persistence state now */
2672 return gpio_set_config_with_argument_optional(desc,
2673 PIN_CONFIG_PERSIST_STATE,
2676 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2679 * gpiod_is_active_low - test whether a GPIO is active-low or not
2680 * @desc: the gpio descriptor to test
2682 * Returns 1 if the GPIO is active-low, 0 otherwise.
2684 int gpiod_is_active_low(const struct gpio_desc *desc)
2686 VALIDATE_DESC(desc);
2687 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2689 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2692 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2693 * @desc: the gpio descriptor to change
2695 void gpiod_toggle_active_low(struct gpio_desc *desc)
2697 VALIDATE_DESC_VOID(desc);
2698 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2700 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2702 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
2704 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
2707 /* I/O calls are only valid after configuration completed; the relevant
2708 * "is this a valid GPIO" error checks should already have been done.
2710 * "Get" operations are often inlinable as reading a pin value register,
2711 * and masking the relevant bit in that register.
2713 * When "set" operations are inlinable, they involve writing that mask to
2714 * one register to set a low value, or a different register to set it high.
2715 * Otherwise locking is needed, so there may be little value to inlining.
2717 *------------------------------------------------------------------------
2719 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2720 * have requested the GPIO. That can include implicit requesting by
2721 * a direction setting call. Marking a gpio as requested locks its chip
2722 * in memory, guaranteeing that these table lookups need no more locking
2723 * and that gpiochip_remove() will fail.
2725 * REVISIT when debugging, consider adding some instrumentation to ensure
2726 * that the GPIO was actually requested.
2729 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2731 struct gpio_chip *gc;
2734 gc = desc->gdev->chip;
2735 value = gpio_chip_get_value(gc, desc);
2736 value = value < 0 ? value : !!value;
2737 trace_gpio_value(desc_to_gpio(desc), 1, value);
2741 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2742 unsigned long *mask, unsigned long *bits)
2744 if (gc->get_multiple)
2745 return gc->get_multiple(gc, mask, bits);
2749 for_each_set_bit(i, mask, gc->ngpio) {
2750 value = gc->get(gc, i);
2753 __assign_bit(i, bits, value);
2760 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2761 unsigned int array_size,
2762 struct gpio_desc **desc_array,
2763 struct gpio_array *array_info,
2764 unsigned long *value_bitmap)
2769 * Validate array_info against desc_array and its size.
2770 * It should immediately follow desc_array if both
2771 * have been obtained from the same gpiod_get_array() call.
2773 if (array_info && array_info->desc == desc_array &&
2774 array_size <= array_info->size &&
2775 (void *)array_info == desc_array + array_info->size) {
2777 WARN_ON(array_info->chip->can_sleep);
2779 ret = gpio_chip_get_multiple(array_info->chip,
2780 array_info->get_mask,
2785 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2786 bitmap_xor(value_bitmap, value_bitmap,
2787 array_info->invert_mask, array_size);
2789 i = find_first_zero_bit(array_info->get_mask, array_size);
2790 if (i == array_size)
2796 while (i < array_size) {
2797 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2798 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
2799 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
2800 unsigned long *mask, *bits;
2803 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2804 mask = fastpath_mask;
2805 bits = fastpath_bits;
2807 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
2809 mask = bitmap_alloc(gc->ngpio, flags);
2813 bits = bitmap_alloc(gc->ngpio, flags);
2820 bitmap_zero(mask, gc->ngpio);
2823 WARN_ON(gc->can_sleep);
2825 /* collect all inputs belonging to the same chip */
2828 const struct gpio_desc *desc = desc_array[i];
2829 int hwgpio = gpio_chip_hwgpio(desc);
2831 __set_bit(hwgpio, mask);
2835 i = find_next_zero_bit(array_info->get_mask,
2837 } while ((i < array_size) &&
2838 (desc_array[i]->gdev->chip == gc));
2840 ret = gpio_chip_get_multiple(gc, mask, bits);
2842 if (mask != fastpath_mask)
2844 if (bits != fastpath_bits)
2849 for (j = first; j < i; ) {
2850 const struct gpio_desc *desc = desc_array[j];
2851 int hwgpio = gpio_chip_hwgpio(desc);
2852 int value = test_bit(hwgpio, bits);
2854 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2856 __assign_bit(j, value_bitmap, value);
2857 trace_gpio_value(desc_to_gpio(desc), 1, value);
2861 j = find_next_zero_bit(array_info->get_mask, i,
2865 if (mask != fastpath_mask)
2867 if (bits != fastpath_bits)
2874 * gpiod_get_raw_value() - return a gpio's raw value
2875 * @desc: gpio whose value will be returned
2877 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2878 * its ACTIVE_LOW status, or negative errno on failure.
2880 * This function can be called from contexts where we cannot sleep, and will
2881 * complain if the GPIO chip functions potentially sleep.
2883 int gpiod_get_raw_value(const struct gpio_desc *desc)
2885 VALIDATE_DESC(desc);
2886 /* Should be using gpiod_get_raw_value_cansleep() */
2887 WARN_ON(desc->gdev->chip->can_sleep);
2888 return gpiod_get_raw_value_commit(desc);
2890 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2893 * gpiod_get_value() - return a gpio's value
2894 * @desc: gpio whose value will be returned
2896 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2897 * account, or negative errno on failure.
2899 * This function can be called from contexts where we cannot sleep, and will
2900 * complain if the GPIO chip functions potentially sleep.
2902 int gpiod_get_value(const struct gpio_desc *desc)
2906 VALIDATE_DESC(desc);
2907 /* Should be using gpiod_get_value_cansleep() */
2908 WARN_ON(desc->gdev->chip->can_sleep);
2910 value = gpiod_get_raw_value_commit(desc);
2914 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2919 EXPORT_SYMBOL_GPL(gpiod_get_value);
2922 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2923 * @array_size: number of elements in the descriptor array / value bitmap
2924 * @desc_array: array of GPIO descriptors whose values will be read
2925 * @array_info: information on applicability of fast bitmap processing path
2926 * @value_bitmap: bitmap to store the read values
2928 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2929 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2930 * else an error code.
2932 * This function can be called from contexts where we cannot sleep,
2933 * and it will complain if the GPIO chip functions potentially sleep.
2935 int gpiod_get_raw_array_value(unsigned int array_size,
2936 struct gpio_desc **desc_array,
2937 struct gpio_array *array_info,
2938 unsigned long *value_bitmap)
2942 return gpiod_get_array_value_complex(true, false, array_size,
2943 desc_array, array_info,
2946 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2949 * gpiod_get_array_value() - read values from an array of GPIOs
2950 * @array_size: number of elements in the descriptor array / value bitmap
2951 * @desc_array: array of GPIO descriptors whose values will be read
2952 * @array_info: information on applicability of fast bitmap processing path
2953 * @value_bitmap: bitmap to store the read values
2955 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2956 * into account. Return 0 in case of success, else an error code.
2958 * This function can be called from contexts where we cannot sleep,
2959 * and it will complain if the GPIO chip functions potentially sleep.
2961 int gpiod_get_array_value(unsigned int array_size,
2962 struct gpio_desc **desc_array,
2963 struct gpio_array *array_info,
2964 unsigned long *value_bitmap)
2968 return gpiod_get_array_value_complex(false, false, array_size,
2969 desc_array, array_info,
2972 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2975 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2976 * @desc: gpio descriptor whose state need to be set.
2977 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2979 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2982 struct gpio_chip *gc = desc->gdev->chip;
2983 int offset = gpio_chip_hwgpio(desc);
2986 ret = gc->direction_input(gc, offset);
2988 ret = gc->direction_output(gc, offset, 0);
2990 set_bit(FLAG_IS_OUT, &desc->flags);
2992 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2995 "%s: Error in set_value for open drain err %d\n",
3000 * _gpio_set_open_source_value() - Set the open source gpio's value.
3001 * @desc: gpio descriptor whose state need to be set.
3002 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3004 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3007 struct gpio_chip *gc = desc->gdev->chip;
3008 int offset = gpio_chip_hwgpio(desc);
3011 ret = gc->direction_output(gc, offset, 1);
3013 set_bit(FLAG_IS_OUT, &desc->flags);
3015 ret = gc->direction_input(gc, offset);
3017 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3020 "%s: Error in set_value for open source err %d\n",
3024 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3026 struct gpio_chip *gc;
3028 gc = desc->gdev->chip;
3029 trace_gpio_value(desc_to_gpio(desc), 0, value);
3030 gc->set(gc, gpio_chip_hwgpio(desc), value);
3034 * set multiple outputs on the same chip;
3035 * use the chip's set_multiple function if available;
3036 * otherwise set the outputs sequentially;
3037 * @chip: the GPIO chip we operate on
3038 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3039 * defines which outputs are to be changed
3040 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3041 * defines the values the outputs specified by mask are to be set to
3043 static void gpio_chip_set_multiple(struct gpio_chip *gc,
3044 unsigned long *mask, unsigned long *bits)
3046 if (gc->set_multiple) {
3047 gc->set_multiple(gc, mask, bits);
3051 /* set outputs if the corresponding mask bit is set */
3052 for_each_set_bit(i, mask, gc->ngpio)
3053 gc->set(gc, i, test_bit(i, bits));
3057 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3058 unsigned int array_size,
3059 struct gpio_desc **desc_array,
3060 struct gpio_array *array_info,
3061 unsigned long *value_bitmap)
3066 * Validate array_info against desc_array and its size.
3067 * It should immediately follow desc_array if both
3068 * have been obtained from the same gpiod_get_array() call.
3070 if (array_info && array_info->desc == desc_array &&
3071 array_size <= array_info->size &&
3072 (void *)array_info == desc_array + array_info->size) {
3074 WARN_ON(array_info->chip->can_sleep);
3076 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3077 bitmap_xor(value_bitmap, value_bitmap,
3078 array_info->invert_mask, array_size);
3080 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3083 i = find_first_zero_bit(array_info->set_mask, array_size);
3084 if (i == array_size)
3090 while (i < array_size) {
3091 struct gpio_chip *gc = desc_array[i]->gdev->chip;
3092 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3093 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3094 unsigned long *mask, *bits;
3097 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
3098 mask = fastpath_mask;
3099 bits = fastpath_bits;
3101 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3103 mask = bitmap_alloc(gc->ngpio, flags);
3107 bits = bitmap_alloc(gc->ngpio, flags);
3114 bitmap_zero(mask, gc->ngpio);
3117 WARN_ON(gc->can_sleep);
3120 struct gpio_desc *desc = desc_array[i];
3121 int hwgpio = gpio_chip_hwgpio(desc);
3122 int value = test_bit(i, value_bitmap);
3125 * Pins applicable for fast input but not for
3126 * fast output processing may have been already
3127 * inverted inside the fast path, skip them.
3129 if (!raw && !(array_info &&
3130 test_bit(i, array_info->invert_mask)) &&
3131 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3133 trace_gpio_value(desc_to_gpio(desc), 0, value);
3135 * collect all normal outputs belonging to the same chip
3136 * open drain and open source outputs are set individually
3138 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3139 gpio_set_open_drain_value_commit(desc, value);
3140 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3141 gpio_set_open_source_value_commit(desc, value);
3143 __set_bit(hwgpio, mask);
3144 __assign_bit(hwgpio, bits, value);
3150 i = find_next_zero_bit(array_info->set_mask,
3152 } while ((i < array_size) &&
3153 (desc_array[i]->gdev->chip == gc));
3154 /* push collected bits to outputs */
3156 gpio_chip_set_multiple(gc, mask, bits);
3158 if (mask != fastpath_mask)
3160 if (bits != fastpath_bits)
3167 * gpiod_set_raw_value() - assign a gpio's raw value
3168 * @desc: gpio whose value will be assigned
3169 * @value: value to assign
3171 * Set the raw value of the GPIO, i.e. the value of its physical line without
3172 * regard for its ACTIVE_LOW status.
3174 * This function can be called from contexts where we cannot sleep, and will
3175 * complain if the GPIO chip functions potentially sleep.
3177 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3179 VALIDATE_DESC_VOID(desc);
3180 /* Should be using gpiod_set_raw_value_cansleep() */
3181 WARN_ON(desc->gdev->chip->can_sleep);
3182 gpiod_set_raw_value_commit(desc, value);
3184 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3187 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3188 * @desc: the descriptor to set the value on
3189 * @value: value to set
3191 * This sets the value of a GPIO line backing a descriptor, applying
3192 * different semantic quirks like active low and open drain/source
3195 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3197 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3199 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3200 gpio_set_open_drain_value_commit(desc, value);
3201 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3202 gpio_set_open_source_value_commit(desc, value);
3204 gpiod_set_raw_value_commit(desc, value);
3208 * gpiod_set_value() - assign a gpio's value
3209 * @desc: gpio whose value will be assigned
3210 * @value: value to assign
3212 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3213 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3215 * This function can be called from contexts where we cannot sleep, and will
3216 * complain if the GPIO chip functions potentially sleep.
3218 void gpiod_set_value(struct gpio_desc *desc, int value)
3220 VALIDATE_DESC_VOID(desc);
3221 /* Should be using gpiod_set_value_cansleep() */
3222 WARN_ON(desc->gdev->chip->can_sleep);
3223 gpiod_set_value_nocheck(desc, value);
3225 EXPORT_SYMBOL_GPL(gpiod_set_value);
3228 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3229 * @array_size: number of elements in the descriptor array / value bitmap
3230 * @desc_array: array of GPIO descriptors whose values will be assigned
3231 * @array_info: information on applicability of fast bitmap processing path
3232 * @value_bitmap: bitmap of values to assign
3234 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3235 * without regard for their ACTIVE_LOW status.
3237 * This function can be called from contexts where we cannot sleep, and will
3238 * complain if the GPIO chip functions potentially sleep.
3240 int gpiod_set_raw_array_value(unsigned int array_size,
3241 struct gpio_desc **desc_array,
3242 struct gpio_array *array_info,
3243 unsigned long *value_bitmap)
3247 return gpiod_set_array_value_complex(true, false, array_size,
3248 desc_array, array_info, value_bitmap);
3250 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3253 * gpiod_set_array_value() - assign values to an array of GPIOs
3254 * @array_size: number of elements in the descriptor array / value bitmap
3255 * @desc_array: array of GPIO descriptors whose values will be assigned
3256 * @array_info: information on applicability of fast bitmap processing path
3257 * @value_bitmap: bitmap of values to assign
3259 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3262 * This function can be called from contexts where we cannot sleep, and will
3263 * complain if the GPIO chip functions potentially sleep.
3265 int gpiod_set_array_value(unsigned int array_size,
3266 struct gpio_desc **desc_array,
3267 struct gpio_array *array_info,
3268 unsigned long *value_bitmap)
3272 return gpiod_set_array_value_complex(false, false, array_size,
3273 desc_array, array_info,
3276 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3279 * gpiod_cansleep() - report whether gpio value access may sleep
3280 * @desc: gpio to check
3283 int gpiod_cansleep(const struct gpio_desc *desc)
3285 VALIDATE_DESC(desc);
3286 return desc->gdev->chip->can_sleep;
3288 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3291 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3292 * @desc: gpio to set the consumer name on
3293 * @name: the new consumer name
3295 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3297 VALIDATE_DESC(desc);
3299 name = kstrdup_const(name, GFP_KERNEL);
3304 kfree_const(desc->label);
3305 desc_set_label(desc, name);
3309 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3312 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3313 * @desc: gpio whose IRQ will be returned (already requested)
3315 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3318 int gpiod_to_irq(const struct gpio_desc *desc)
3320 struct gpio_chip *gc;
3324 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3325 * requires this function to not return zero on an invalid descriptor
3326 * but rather a negative error number.
3328 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3331 gc = desc->gdev->chip;
3332 offset = gpio_chip_hwgpio(desc);
3334 int retirq = gc->to_irq(gc, offset);
3336 /* Zero means NO_IRQ */
3342 #ifdef CONFIG_GPIOLIB_IRQCHIP
3345 * Avoid race condition with other code, which tries to lookup
3346 * an IRQ before the irqchip has been properly registered,
3347 * i.e. while gpiochip is still being brought up.
3349 return -EPROBE_DEFER;
3354 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3357 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3358 * @gc: the chip the GPIO to lock belongs to
3359 * @offset: the offset of the GPIO to lock as IRQ
3361 * This is used directly by GPIO drivers that want to lock down
3362 * a certain GPIO line to be used for IRQs.
3364 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3366 struct gpio_desc *desc;
3368 desc = gpiochip_get_desc(gc, offset);
3370 return PTR_ERR(desc);
3373 * If it's fast: flush the direction setting if something changed
3376 if (!gc->can_sleep && gc->get_direction) {
3377 int dir = gpiod_get_direction(desc);
3380 chip_err(gc, "%s: cannot get GPIO direction\n",
3386 /* To be valid for IRQ the line needs to be input or open drain */
3387 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3388 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3390 "%s: tried to flag a GPIO set as output for IRQ\n",
3395 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3396 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3399 * If the consumer has not set up a label (such as when the
3400 * IRQ is referenced from .to_irq()) we set up a label here
3401 * so it is clear this is used as an interrupt.
3404 desc_set_label(desc, "interrupt");
3408 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3411 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3412 * @gc: the chip the GPIO to lock belongs to
3413 * @offset: the offset of the GPIO to lock as IRQ
3415 * This is used directly by GPIO drivers that want to indicate
3416 * that a certain GPIO is no longer used exclusively for IRQ.
3418 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3420 struct gpio_desc *desc;
3422 desc = gpiochip_get_desc(gc, offset);
3426 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3427 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3429 /* If we only had this marking, erase it */
3430 if (desc->label && !strcmp(desc->label, "interrupt"))
3431 desc_set_label(desc, NULL);
3433 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3435 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3437 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3439 if (!IS_ERR(desc) &&
3440 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3441 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3443 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3445 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3447 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3449 if (!IS_ERR(desc) &&
3450 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3452 * We must not be output when using IRQ UNLESS we are
3455 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3456 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3457 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3460 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3462 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3464 if (offset >= gc->ngpio)
3467 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3469 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3471 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3475 if (!try_module_get(gc->gpiodev->owner))
3478 ret = gpiochip_lock_as_irq(gc, offset);
3480 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3481 module_put(gc->gpiodev->owner);
3486 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3488 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3490 gpiochip_unlock_as_irq(gc, offset);
3491 module_put(gc->gpiodev->owner);
3493 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3495 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3497 if (offset >= gc->ngpio)
3500 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3502 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3504 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3506 if (offset >= gc->ngpio)
3509 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3511 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3513 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3515 if (offset >= gc->ngpio)
3518 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3520 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3523 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3524 * @desc: gpio whose value will be returned
3526 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3527 * its ACTIVE_LOW status, or negative errno on failure.
3529 * This function is to be called from contexts that can sleep.
3531 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3533 might_sleep_if(extra_checks);
3534 VALIDATE_DESC(desc);
3535 return gpiod_get_raw_value_commit(desc);
3537 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3540 * gpiod_get_value_cansleep() - return a gpio's value
3541 * @desc: gpio whose value will be returned
3543 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3544 * account, or negative errno on failure.
3546 * This function is to be called from contexts that can sleep.
3548 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3552 might_sleep_if(extra_checks);
3553 VALIDATE_DESC(desc);
3554 value = gpiod_get_raw_value_commit(desc);
3558 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3563 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3566 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3567 * @array_size: number of elements in the descriptor array / value bitmap
3568 * @desc_array: array of GPIO descriptors whose values will be read
3569 * @array_info: information on applicability of fast bitmap processing path
3570 * @value_bitmap: bitmap to store the read values
3572 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3573 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3574 * else an error code.
3576 * This function is to be called from contexts that can sleep.
3578 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3579 struct gpio_desc **desc_array,
3580 struct gpio_array *array_info,
3581 unsigned long *value_bitmap)
3583 might_sleep_if(extra_checks);
3586 return gpiod_get_array_value_complex(true, true, array_size,
3587 desc_array, array_info,
3590 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3593 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3594 * @array_size: number of elements in the descriptor array / value bitmap
3595 * @desc_array: array of GPIO descriptors whose values will be read
3596 * @array_info: information on applicability of fast bitmap processing path
3597 * @value_bitmap: bitmap to store the read values
3599 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3600 * into account. Return 0 in case of success, else an error code.
3602 * This function is to be called from contexts that can sleep.
3604 int gpiod_get_array_value_cansleep(unsigned int array_size,
3605 struct gpio_desc **desc_array,
3606 struct gpio_array *array_info,
3607 unsigned long *value_bitmap)
3609 might_sleep_if(extra_checks);
3612 return gpiod_get_array_value_complex(false, true, array_size,
3613 desc_array, array_info,
3616 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3619 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3620 * @desc: gpio whose value will be assigned
3621 * @value: value to assign
3623 * Set the raw value of the GPIO, i.e. the value of its physical line without
3624 * regard for its ACTIVE_LOW status.
3626 * This function is to be called from contexts that can sleep.
3628 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3630 might_sleep_if(extra_checks);
3631 VALIDATE_DESC_VOID(desc);
3632 gpiod_set_raw_value_commit(desc, value);
3634 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3637 * gpiod_set_value_cansleep() - assign a gpio's value
3638 * @desc: gpio whose value will be assigned
3639 * @value: value to assign
3641 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3644 * This function is to be called from contexts that can sleep.
3646 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3648 might_sleep_if(extra_checks);
3649 VALIDATE_DESC_VOID(desc);
3650 gpiod_set_value_nocheck(desc, value);
3652 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3655 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3656 * @array_size: number of elements in the descriptor array / value bitmap
3657 * @desc_array: array of GPIO descriptors whose values will be assigned
3658 * @array_info: information on applicability of fast bitmap processing path
3659 * @value_bitmap: bitmap of values to assign
3661 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3662 * without regard for their ACTIVE_LOW status.
3664 * This function is to be called from contexts that can sleep.
3666 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3667 struct gpio_desc **desc_array,
3668 struct gpio_array *array_info,
3669 unsigned long *value_bitmap)
3671 might_sleep_if(extra_checks);
3674 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3675 array_info, value_bitmap);
3677 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3680 * gpiod_add_lookup_tables() - register GPIO device consumers
3681 * @tables: list of tables of consumers to register
3682 * @n: number of tables in the list
3684 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3688 mutex_lock(&gpio_lookup_lock);
3690 for (i = 0; i < n; i++)
3691 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3693 mutex_unlock(&gpio_lookup_lock);
3697 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3698 * @array_size: number of elements in the descriptor array / value bitmap
3699 * @desc_array: array of GPIO descriptors whose values will be assigned
3700 * @array_info: information on applicability of fast bitmap processing path
3701 * @value_bitmap: bitmap of values to assign
3703 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3706 * This function is to be called from contexts that can sleep.
3708 int gpiod_set_array_value_cansleep(unsigned int array_size,
3709 struct gpio_desc **desc_array,
3710 struct gpio_array *array_info,
3711 unsigned long *value_bitmap)
3713 might_sleep_if(extra_checks);
3716 return gpiod_set_array_value_complex(false, true, array_size,
3717 desc_array, array_info,
3720 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3723 * gpiod_add_lookup_table() - register GPIO device consumers
3724 * @table: table of consumers to register
3726 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3728 gpiod_add_lookup_tables(&table, 1);
3730 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3733 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3734 * @table: table of consumers to unregister
3736 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3738 /* Nothing to remove */
3742 mutex_lock(&gpio_lookup_lock);
3744 list_del(&table->list);
3746 mutex_unlock(&gpio_lookup_lock);
3748 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3751 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3752 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3754 void gpiod_add_hogs(struct gpiod_hog *hogs)
3756 struct gpio_chip *gc;
3757 struct gpiod_hog *hog;
3759 mutex_lock(&gpio_machine_hogs_mutex);
3761 for (hog = &hogs[0]; hog->chip_label; hog++) {
3762 list_add_tail(&hog->list, &gpio_machine_hogs);
3765 * The chip may have been registered earlier, so check if it
3766 * exists and, if so, try to hog the line now.
3768 gc = find_chip_by_name(hog->chip_label);
3770 gpiochip_machine_hog(gc, hog);
3773 mutex_unlock(&gpio_machine_hogs_mutex);
3775 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3777 void gpiod_remove_hogs(struct gpiod_hog *hogs)
3779 struct gpiod_hog *hog;
3781 mutex_lock(&gpio_machine_hogs_mutex);
3782 for (hog = &hogs[0]; hog->chip_label; hog++)
3783 list_del(&hog->list);
3784 mutex_unlock(&gpio_machine_hogs_mutex);
3786 EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
3788 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3790 const char *dev_id = dev ? dev_name(dev) : NULL;
3791 struct gpiod_lookup_table *table;
3793 mutex_lock(&gpio_lookup_lock);
3795 list_for_each_entry(table, &gpio_lookup_list, list) {
3796 if (table->dev_id && dev_id) {
3798 * Valid strings on both ends, must be identical to have
3801 if (!strcmp(table->dev_id, dev_id))
3805 * One of the pointers is NULL, so both must be to have
3808 if (dev_id == table->dev_id)
3815 mutex_unlock(&gpio_lookup_lock);
3819 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3820 unsigned int idx, unsigned long *flags)
3822 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3823 struct gpiod_lookup_table *table;
3824 struct gpiod_lookup *p;
3826 table = gpiod_find_lookup_table(dev);
3830 for (p = &table->table[0]; p->key; p++) {
3831 struct gpio_chip *gc;
3833 /* idx must always match exactly */
3837 /* If the lookup entry has a con_id, require exact match */
3838 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3841 if (p->chip_hwnum == U16_MAX) {
3842 desc = gpio_name_to_desc(p->key);
3848 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3850 return ERR_PTR(-EPROBE_DEFER);
3853 gc = find_chip_by_name(p->key);
3857 * As the lookup table indicates a chip with
3858 * p->key should exist, assume it may
3859 * still appear later and let the interested
3860 * consumer be probed again or let the Deferred
3861 * Probe infrastructure handle the error.
3863 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3865 return ERR_PTR(-EPROBE_DEFER);
3868 if (gc->ngpio <= p->chip_hwnum) {
3870 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3871 idx, p->chip_hwnum, gc->ngpio - 1,
3873 return ERR_PTR(-EINVAL);
3876 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3885 static int platform_gpio_count(struct device *dev, const char *con_id)
3887 struct gpiod_lookup_table *table;
3888 struct gpiod_lookup *p;
3889 unsigned int count = 0;
3891 table = gpiod_find_lookup_table(dev);
3895 for (p = &table->table[0]; p->key; p++) {
3896 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3897 (!con_id && !p->con_id))
3906 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
3907 struct device *consumer,
3910 enum gpiod_flags *flags,
3911 unsigned long *lookupflags)
3913 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3915 if (is_of_node(fwnode)) {
3916 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n",
3918 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags);
3919 } else if (is_acpi_node(fwnode)) {
3920 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n",
3922 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
3923 } else if (is_software_node(fwnode)) {
3924 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n",
3926 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
3932 static struct gpio_desc *gpiod_find_and_request(struct device *consumer,
3933 struct fwnode_handle *fwnode,
3936 enum gpiod_flags flags,
3938 bool platform_lookup_allowed)
3940 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3941 struct gpio_desc *desc;
3944 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, &flags, &lookupflags);
3945 if (gpiod_not_found(desc) && platform_lookup_allowed) {
3947 * Either we are not using DT or ACPI, or their lookup did not
3948 * return a result. In that case, use platform lookup as a
3951 dev_dbg(consumer, "using lookup tables for GPIO lookup\n");
3952 desc = gpiod_find(consumer, con_id, idx, &lookupflags);
3956 dev_dbg(consumer, "No GPIO consumer %s found\n", con_id);
3961 * If a connection label was passed use that, else attempt to use
3962 * the device name as label
3964 ret = gpiod_request(desc, label);
3966 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
3967 return ERR_PTR(ret);
3970 * This happens when there are several consumers for
3971 * the same GPIO line: we just return here without
3972 * further initialization. It is a bit of a hack.
3973 * This is necessary to support fixed regulators.
3975 * FIXME: Make this more sane and safe.
3978 "nonexclusive access to GPIO for %s\n", con_id);
3982 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3984 dev_dbg(consumer, "setup of GPIO %s failed\n", con_id);
3986 return ERR_PTR(ret);
3989 blocking_notifier_call_chain(&desc->gdev->notifier,
3990 GPIOLINE_CHANGED_REQUESTED, desc);
3996 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3997 * @fwnode: handle of the firmware node
3998 * @con_id: function within the GPIO consumer
3999 * @index: index of the GPIO to obtain for the consumer
4000 * @flags: GPIO initialization flags
4001 * @label: label to attach to the requested GPIO
4003 * This function can be used for drivers that get their configuration
4004 * from opaque firmware.
4006 * The function properly finds the corresponding GPIO using whatever is the
4007 * underlying firmware interface and then makes sure that the GPIO
4008 * descriptor is requested before it is returned to the caller.
4011 * On successful request the GPIO pin is configured in accordance with
4014 * In case of error an ERR_PTR() is returned.
4016 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4019 enum gpiod_flags flags,
4022 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false);
4024 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4027 * gpiod_count - return the number of GPIOs associated with a device / function
4028 * or -ENOENT if no GPIO has been assigned to the requested function
4029 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4030 * @con_id: function within the GPIO consumer
4032 int gpiod_count(struct device *dev, const char *con_id)
4034 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4035 int count = -ENOENT;
4037 if (is_of_node(fwnode))
4038 count = of_gpio_get_count(dev, con_id);
4039 else if (is_acpi_node(fwnode))
4040 count = acpi_gpio_count(dev, con_id);
4041 else if (is_software_node(fwnode))
4042 count = swnode_gpio_count(fwnode, con_id);
4045 count = platform_gpio_count(dev, con_id);
4049 EXPORT_SYMBOL_GPL(gpiod_count);
4052 * gpiod_get - obtain a GPIO for a given GPIO function
4053 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4054 * @con_id: function within the GPIO consumer
4055 * @flags: optional GPIO initialization flags
4057 * Return the GPIO descriptor corresponding to the function con_id of device
4058 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4059 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4061 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4062 enum gpiod_flags flags)
4064 return gpiod_get_index(dev, con_id, 0, flags);
4066 EXPORT_SYMBOL_GPL(gpiod_get);
4069 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4070 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4071 * @con_id: function within the GPIO consumer
4072 * @flags: optional GPIO initialization flags
4074 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4075 * the requested function it will return NULL. This is convenient for drivers
4076 * that need to handle optional GPIOs.
4078 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4080 enum gpiod_flags flags)
4082 return gpiod_get_index_optional(dev, con_id, 0, flags);
4084 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4088 * gpiod_configure_flags - helper function to configure a given GPIO
4089 * @desc: gpio whose value will be assigned
4090 * @con_id: function within the GPIO consumer
4091 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4092 * of_find_gpio() or of_get_gpio_hog()
4093 * @dflags: gpiod_flags - optional GPIO initialization flags
4095 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4096 * requested function and/or index, or another IS_ERR() code if an error
4097 * occurred while trying to acquire the GPIO.
4099 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4100 unsigned long lflags, enum gpiod_flags dflags)
4104 if (lflags & GPIO_ACTIVE_LOW)
4105 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4107 if (lflags & GPIO_OPEN_DRAIN)
4108 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4109 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4111 * This enforces open drain mode from the consumer side.
4112 * This is necessary for some busses like I2C, but the lookup
4113 * should *REALLY* have specified them as open drain in the
4114 * first place, so print a little warning here.
4116 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4118 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4121 if (lflags & GPIO_OPEN_SOURCE)
4122 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4124 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
4125 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
4126 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
4128 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4132 if (lflags & GPIO_PULL_UP)
4133 set_bit(FLAG_PULL_UP, &desc->flags);
4134 else if (lflags & GPIO_PULL_DOWN)
4135 set_bit(FLAG_PULL_DOWN, &desc->flags);
4136 else if (lflags & GPIO_PULL_DISABLE)
4137 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
4139 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4143 /* No particular flag request, return here... */
4144 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4145 gpiod_dbg(desc, "no flags found for %s\n", con_id);
4150 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4151 ret = gpiod_direction_output(desc,
4152 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4154 ret = gpiod_direction_input(desc);
4160 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4161 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4162 * @con_id: function within the GPIO consumer
4163 * @idx: index of the GPIO to obtain in the consumer
4164 * @flags: optional GPIO initialization flags
4166 * This variant of gpiod_get() allows to access GPIOs other than the first
4167 * defined one for functions that define several GPIOs.
4169 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4170 * requested function and/or index, or another IS_ERR() code if an error
4171 * occurred while trying to acquire the GPIO.
4173 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4176 enum gpiod_flags flags)
4178 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4179 const char *devname = dev ? dev_name(dev) : "?";
4180 const char *label = con_id ?: devname;
4182 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true);
4184 EXPORT_SYMBOL_GPL(gpiod_get_index);
4187 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4189 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4190 * @con_id: function within the GPIO consumer
4191 * @index: index of the GPIO to obtain in the consumer
4192 * @flags: optional GPIO initialization flags
4194 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4195 * specified index was assigned to the requested function it will return NULL.
4196 * This is convenient for drivers that need to handle optional GPIOs.
4198 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4201 enum gpiod_flags flags)
4203 struct gpio_desc *desc;
4205 desc = gpiod_get_index(dev, con_id, index, flags);
4206 if (gpiod_not_found(desc))
4211 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4214 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4215 * @desc: gpio whose value will be assigned
4216 * @name: gpio line name
4217 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4218 * of_find_gpio() or of_get_gpio_hog()
4219 * @dflags: gpiod_flags - optional GPIO initialization flags
4221 int gpiod_hog(struct gpio_desc *desc, const char *name,
4222 unsigned long lflags, enum gpiod_flags dflags)
4224 struct gpio_chip *gc;
4225 struct gpio_desc *local_desc;
4229 gc = gpiod_to_chip(desc);
4230 hwnum = gpio_chip_hwgpio(desc);
4232 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4234 if (IS_ERR(local_desc)) {
4235 ret = PTR_ERR(local_desc);
4236 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4237 name, gc->label, hwnum, ret);
4241 /* Mark GPIO as hogged so it can be identified and removed later */
4242 set_bit(FLAG_IS_HOGGED, &desc->flags);
4244 gpiod_info(desc, "hogged as %s%s\n",
4245 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4246 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4247 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4253 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4254 * @gc: gpio chip to act on
4256 static void gpiochip_free_hogs(struct gpio_chip *gc)
4258 struct gpio_desc *desc;
4260 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
4261 gpiochip_free_own_desc(desc);
4265 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4266 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4267 * @con_id: function within the GPIO consumer
4268 * @flags: optional GPIO initialization flags
4270 * This function acquires all the GPIOs defined under a given function.
4272 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4273 * no GPIO has been assigned to the requested function, or another IS_ERR()
4274 * code if an error occurred while trying to acquire the GPIOs.
4276 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4278 enum gpiod_flags flags)
4280 struct gpio_desc *desc;
4281 struct gpio_descs *descs;
4282 struct gpio_array *array_info = NULL;
4283 struct gpio_chip *gc;
4284 int count, bitmap_size;
4287 count = gpiod_count(dev, con_id);
4289 return ERR_PTR(count);
4291 descs_size = struct_size(descs, desc, count);
4292 descs = kzalloc(descs_size, GFP_KERNEL);
4294 return ERR_PTR(-ENOMEM);
4296 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) {
4297 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4299 gpiod_put_array(descs);
4300 return ERR_CAST(desc);
4303 descs->desc[descs->ndescs] = desc;
4305 gc = gpiod_to_chip(desc);
4307 * If pin hardware number of array member 0 is also 0, select
4308 * its chip as a candidate for fast bitmap processing path.
4310 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4311 struct gpio_descs *array;
4313 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4316 array = krealloc(descs, descs_size +
4317 struct_size(array_info, invert_mask, 3 * bitmap_size),
4318 GFP_KERNEL | __GFP_ZERO);
4320 gpiod_put_array(descs);
4321 return ERR_PTR(-ENOMEM);
4326 array_info = (void *)descs + descs_size;
4327 array_info->get_mask = array_info->invert_mask +
4329 array_info->set_mask = array_info->get_mask +
4332 array_info->desc = descs->desc;
4333 array_info->size = count;
4334 array_info->chip = gc;
4335 bitmap_set(array_info->get_mask, descs->ndescs,
4336 count - descs->ndescs);
4337 bitmap_set(array_info->set_mask, descs->ndescs,
4338 count - descs->ndescs);
4339 descs->info = array_info;
4342 /* If there is no cache for fast bitmap processing path, continue */
4346 /* Unmark array members which don't belong to the 'fast' chip */
4347 if (array_info->chip != gc) {
4348 __clear_bit(descs->ndescs, array_info->get_mask);
4349 __clear_bit(descs->ndescs, array_info->set_mask);
4352 * Detect array members which belong to the 'fast' chip
4353 * but their pins are not in hardware order.
4355 else if (gpio_chip_hwgpio(desc) != descs->ndescs) {
4357 * Don't use fast path if all array members processed so
4358 * far belong to the same chip as this one but its pin
4359 * hardware number is different from its array index.
4361 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4364 __clear_bit(descs->ndescs,
4365 array_info->get_mask);
4366 __clear_bit(descs->ndescs,
4367 array_info->set_mask);
4370 /* Exclude open drain or open source from fast output */
4371 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4372 gpiochip_line_is_open_source(gc, descs->ndescs))
4373 __clear_bit(descs->ndescs,
4374 array_info->set_mask);
4375 /* Identify 'fast' pins which require invertion */
4376 if (gpiod_is_active_low(desc))
4377 __set_bit(descs->ndescs,
4378 array_info->invert_mask);
4383 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4384 array_info->chip->label, array_info->size,
4385 *array_info->get_mask, *array_info->set_mask,
4386 *array_info->invert_mask);
4389 EXPORT_SYMBOL_GPL(gpiod_get_array);
4392 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4394 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4395 * @con_id: function within the GPIO consumer
4396 * @flags: optional GPIO initialization flags
4398 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4399 * assigned to the requested function it will return NULL.
4401 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4403 enum gpiod_flags flags)
4405 struct gpio_descs *descs;
4407 descs = gpiod_get_array(dev, con_id, flags);
4408 if (gpiod_not_found(descs))
4413 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4416 * gpiod_put - dispose of a GPIO descriptor
4417 * @desc: GPIO descriptor to dispose of
4419 * No descriptor can be used after gpiod_put() has been called on it.
4421 void gpiod_put(struct gpio_desc *desc)
4426 EXPORT_SYMBOL_GPL(gpiod_put);
4429 * gpiod_put_array - dispose of multiple GPIO descriptors
4430 * @descs: struct gpio_descs containing an array of descriptors
4432 void gpiod_put_array(struct gpio_descs *descs)
4436 for (i = 0; i < descs->ndescs; i++)
4437 gpiod_put(descs->desc[i]);
4441 EXPORT_SYMBOL_GPL(gpiod_put_array);
4443 static int gpio_stub_drv_probe(struct device *dev)
4446 * The DT node of some GPIO chips have a "compatible" property, but
4447 * never have a struct device added and probed by a driver to register
4448 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4449 * the consumers of the GPIO chip to get probe deferred forever because
4450 * they will be waiting for a device associated with the GPIO chip
4451 * firmware node to get added and bound to a driver.
4453 * To allow these consumers to probe, we associate the struct
4454 * gpio_device of the GPIO chip with the firmware node and then simply
4455 * bind it to this stub driver.
4460 static struct device_driver gpio_stub_drv = {
4461 .name = "gpio_stub_drv",
4462 .bus = &gpio_bus_type,
4463 .probe = gpio_stub_drv_probe,
4466 static int __init gpiolib_dev_init(void)
4470 /* Register GPIO sysfs bus */
4471 ret = bus_register(&gpio_bus_type);
4473 pr_err("gpiolib: could not register GPIO bus type\n");
4477 ret = driver_register(&gpio_stub_drv);
4479 pr_err("gpiolib: could not register GPIO stub driver\n");
4480 bus_unregister(&gpio_bus_type);
4484 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4486 pr_err("gpiolib: failed to allocate char dev region\n");
4487 driver_unregister(&gpio_stub_drv);
4488 bus_unregister(&gpio_bus_type);
4492 gpiolib_initialized = true;
4493 gpiochip_setup_devs();
4495 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4496 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4497 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4501 core_initcall(gpiolib_dev_init);
4503 #ifdef CONFIG_DEBUG_FS
4505 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4507 struct gpio_chip *gc = gdev->chip;
4508 struct gpio_desc *desc;
4509 unsigned gpio = gdev->base;
4515 for_each_gpio_desc(gc, desc) {
4516 if (test_bit(FLAG_REQUESTED, &desc->flags)) {
4517 gpiod_get_direction(desc);
4518 is_out = test_bit(FLAG_IS_OUT, &desc->flags);
4519 value = gpio_chip_get_value(gc, desc);
4520 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
4521 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4522 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
4523 gpio, desc->name ?: "", desc->label,
4524 is_out ? "out" : "in ",
4525 value >= 0 ? (value ? "hi" : "lo") : "? ",
4526 is_irq ? "IRQ " : "",
4527 active_low ? "ACTIVE LOW" : "");
4528 } else if (desc->name) {
4529 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
4536 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4538 unsigned long flags;
4539 struct gpio_device *gdev = NULL;
4540 loff_t index = *pos;
4544 spin_lock_irqsave(&gpio_lock, flags);
4545 list_for_each_entry(gdev, &gpio_devices, list)
4547 spin_unlock_irqrestore(&gpio_lock, flags);
4550 spin_unlock_irqrestore(&gpio_lock, flags);
4555 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4557 unsigned long flags;
4558 struct gpio_device *gdev = v;
4561 spin_lock_irqsave(&gpio_lock, flags);
4562 if (list_is_last(&gdev->list, &gpio_devices))
4565 ret = list_first_entry(&gdev->list, struct gpio_device, list);
4566 spin_unlock_irqrestore(&gpio_lock, flags);
4574 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4578 static int gpiolib_seq_show(struct seq_file *s, void *v)
4580 struct gpio_device *gdev = v;
4581 struct gpio_chip *gc = gdev->chip;
4582 struct device *parent;
4585 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4586 dev_name(&gdev->dev));
4590 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4591 dev_name(&gdev->dev),
4592 gdev->base, gdev->base + gdev->ngpio - 1);
4593 parent = gc->parent;
4595 seq_printf(s, ", parent: %s/%s",
4596 parent->bus ? parent->bus->name : "no-bus",
4599 seq_printf(s, ", %s", gc->label);
4601 seq_printf(s, ", can sleep");
4602 seq_printf(s, ":\n");
4605 gc->dbg_show(s, gc);
4607 gpiolib_dbg_show(s, gdev);
4612 static const struct seq_operations gpiolib_sops = {
4613 .start = gpiolib_seq_start,
4614 .next = gpiolib_seq_next,
4615 .stop = gpiolib_seq_stop,
4616 .show = gpiolib_seq_show,
4618 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4620 static int __init gpiolib_debugfs_init(void)
4622 /* /sys/kernel/debug/gpio */
4623 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4626 subsys_initcall(gpiolib_debugfs_init);
4628 #endif /* DEBUG_FS */