1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/acpi.h>
4 #include <linux/bitmap.h>
5 #include <linux/compat.h>
6 #include <linux/debugfs.h>
7 #include <linux/device.h>
9 #include <linux/errno.h>
10 #include <linux/file.h>
12 #include <linux/idr.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/seq_file.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
24 #include <linux/gpio.h>
25 #include <linux/gpio/driver.h>
26 #include <linux/gpio/machine.h>
28 #include <uapi/linux/gpio.h>
30 #include "gpiolib-acpi.h"
31 #include "gpiolib-cdev.h"
32 #include "gpiolib-of.h"
33 #include "gpiolib-swnode.h"
34 #include "gpiolib-sysfs.h"
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/gpio.h>
40 /* Implementation infrastructure for GPIO interfaces.
42 * The GPIO programming interface allows for inlining speed-critical
43 * get/set operations for common cases, so that access to SOC-integrated
44 * GPIOs can sometimes cost only an instruction or two per bit.
48 /* When debugging, extend minimal trust to callers and platform code.
49 * Also emit diagnostic messages that may help initial bringup, when
50 * board setup or driver bugs are most common.
52 * Otherwise, minimize overhead in what may be bitbanging codepaths.
55 #define extra_checks 1
57 #define extra_checks 0
60 /* Device and char device-related information */
61 static DEFINE_IDA(gpio_ida);
62 static dev_t gpio_devt;
63 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
65 static int gpio_bus_match(struct device *dev, struct device_driver *drv)
67 struct fwnode_handle *fwnode = dev_fwnode(dev);
70 * Only match if the fwnode doesn't already have a proper struct device
73 if (fwnode && fwnode->dev != dev)
78 static struct bus_type gpio_bus_type = {
80 .match = gpio_bus_match,
84 * Number of GPIOs to use for the fast path in set array
86 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
88 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
89 * While any GPIO is requested, its gpio_chip is not removable;
90 * each GPIO's "requested" flag serves as a lock and refcount.
92 DEFINE_SPINLOCK(gpio_lock);
94 static DEFINE_MUTEX(gpio_lookup_lock);
95 static LIST_HEAD(gpio_lookup_list);
96 LIST_HEAD(gpio_devices);
98 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
99 static LIST_HEAD(gpio_machine_hogs);
101 static void gpiochip_free_hogs(struct gpio_chip *gc);
102 static int gpiochip_add_irqchip(struct gpio_chip *gc,
103 struct lock_class_key *lock_key,
104 struct lock_class_key *request_key);
105 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
106 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
107 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
108 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
110 static bool gpiolib_initialized;
112 static inline void desc_set_label(struct gpio_desc *d, const char *label)
118 * gpio_to_desc - Convert a GPIO number to its descriptor
119 * @gpio: global GPIO number
122 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
123 * with the given number exists in the system.
125 struct gpio_desc *gpio_to_desc(unsigned gpio)
127 struct gpio_device *gdev;
130 spin_lock_irqsave(&gpio_lock, flags);
132 list_for_each_entry(gdev, &gpio_devices, list) {
133 if (gdev->base <= gpio &&
134 gdev->base + gdev->ngpio > gpio) {
135 spin_unlock_irqrestore(&gpio_lock, flags);
136 return &gdev->descs[gpio - gdev->base];
140 spin_unlock_irqrestore(&gpio_lock, flags);
142 if (!gpio_is_valid(gpio))
143 pr_warn("invalid GPIO %d\n", gpio);
147 EXPORT_SYMBOL_GPL(gpio_to_desc);
150 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
151 * hardware number for this chip
153 * @hwnum: hardware number of the GPIO for this chip
156 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
157 * in the given chip for the specified hardware number.
159 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
162 struct gpio_device *gdev = gc->gpiodev;
164 if (hwnum >= gdev->ngpio)
165 return ERR_PTR(-EINVAL);
167 return &gdev->descs[hwnum];
169 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
172 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
173 * @desc: GPIO descriptor
175 * This should disappear in the future but is needed since we still
176 * use GPIO numbers for error messages and sysfs nodes.
179 * The global GPIO number for the GPIO specified by its descriptor.
181 int desc_to_gpio(const struct gpio_desc *desc)
183 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
185 EXPORT_SYMBOL_GPL(desc_to_gpio);
189 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
190 * @desc: descriptor to return the chip of
192 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
194 if (!desc || !desc->gdev)
196 return desc->gdev->chip;
198 EXPORT_SYMBOL_GPL(gpiod_to_chip);
200 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
201 static int gpiochip_find_base(int ngpio)
203 struct gpio_device *gdev;
204 int base = GPIO_DYNAMIC_BASE;
206 list_for_each_entry(gdev, &gpio_devices, list) {
207 /* found a free space? */
208 if (gdev->base >= base + ngpio)
210 /* nope, check the space right after the chip */
211 base = gdev->base + gdev->ngpio;
212 if (base < GPIO_DYNAMIC_BASE)
213 base = GPIO_DYNAMIC_BASE;
216 if (gpio_is_valid(base)) {
217 pr_debug("%s: found new base at %d\n", __func__, base);
220 pr_err("%s: cannot find free range\n", __func__);
226 * gpiod_get_direction - return the current direction of a GPIO
227 * @desc: GPIO to get the direction of
229 * Returns 0 for output, 1 for input, or an error code in case of error.
231 * This function may sleep if gpiod_cansleep() is true.
233 int gpiod_get_direction(struct gpio_desc *desc)
235 struct gpio_chip *gc;
239 gc = gpiod_to_chip(desc);
240 offset = gpio_chip_hwgpio(desc);
243 * Open drain emulation using input mode may incorrectly report
244 * input here, fix that up.
246 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
247 test_bit(FLAG_IS_OUT, &desc->flags))
250 if (!gc->get_direction)
253 ret = gc->get_direction(gc, offset);
257 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
261 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
265 EXPORT_SYMBOL_GPL(gpiod_get_direction);
268 * Add a new chip to the global chips list, keeping the list of chips sorted
269 * by range(means [base, base + ngpio - 1]) order.
271 * Return -EBUSY if the new chip overlaps with some other chip's integer
274 static int gpiodev_add_to_list(struct gpio_device *gdev)
276 struct gpio_device *prev, *next;
278 if (list_empty(&gpio_devices)) {
279 /* initial entry in list */
280 list_add_tail(&gdev->list, &gpio_devices);
284 next = list_first_entry(&gpio_devices, struct gpio_device, list);
285 if (gdev->base + gdev->ngpio <= next->base) {
286 /* add before first entry */
287 list_add(&gdev->list, &gpio_devices);
291 prev = list_last_entry(&gpio_devices, struct gpio_device, list);
292 if (prev->base + prev->ngpio <= gdev->base) {
293 /* add behind last entry */
294 list_add_tail(&gdev->list, &gpio_devices);
298 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
299 /* at the end of the list */
300 if (&next->list == &gpio_devices)
303 /* add between prev and next */
304 if (prev->base + prev->ngpio <= gdev->base
305 && gdev->base + gdev->ngpio <= next->base) {
306 list_add(&gdev->list, &prev->list);
315 * Convert a GPIO name to its descriptor
316 * Note that there is no guarantee that GPIO names are globally unique!
317 * Hence this function will return, if it exists, a reference to the first GPIO
318 * line found that matches the given name.
320 static struct gpio_desc *gpio_name_to_desc(const char * const name)
322 struct gpio_device *gdev;
328 spin_lock_irqsave(&gpio_lock, flags);
330 list_for_each_entry(gdev, &gpio_devices, list) {
331 struct gpio_desc *desc;
333 for_each_gpio_desc(gdev->chip, desc) {
334 if (desc->name && !strcmp(desc->name, name)) {
335 spin_unlock_irqrestore(&gpio_lock, flags);
341 spin_unlock_irqrestore(&gpio_lock, flags);
347 * Take the names from gc->names and assign them to their GPIO descriptors.
348 * Warn if a name is already used for a GPIO line on a different GPIO chip.
351 * 1. Non-unique names are still accepted,
352 * 2. Name collisions within the same GPIO chip are not reported.
354 static int gpiochip_set_desc_names(struct gpio_chip *gc)
356 struct gpio_device *gdev = gc->gpiodev;
359 /* First check all names if they are unique */
360 for (i = 0; i != gc->ngpio; ++i) {
361 struct gpio_desc *gpio;
363 gpio = gpio_name_to_desc(gc->names[i]);
366 "Detected name collision for GPIO name '%s'\n",
370 /* Then add all names to the GPIO descriptors */
371 for (i = 0; i != gc->ngpio; ++i)
372 gdev->descs[i].name = gc->names[i];
378 * gpiochip_set_names - Set GPIO line names using device properties
379 * @chip: GPIO chip whose lines should be named, if possible
381 * Looks for device property "gpio-line-names" and if it exists assigns
382 * GPIO line names for the chip. The memory allocated for the assigned
383 * names belong to the underlying firmware node and should not be released
386 static int gpiochip_set_names(struct gpio_chip *chip)
388 struct gpio_device *gdev = chip->gpiodev;
389 struct device *dev = &gdev->dev;
394 count = device_property_string_array_count(dev, "gpio-line-names");
399 * When offset is set in the driver side we assume the driver internally
400 * is using more than one gpiochip per the same device. We have to stop
401 * setting friendly names if the specified ones with 'gpio-line-names'
402 * are less than the offset in the device itself. This means all the
403 * lines are not present for every single pin within all the internal
406 if (count <= chip->offset) {
407 dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
408 count, chip->offset);
412 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
416 ret = device_property_read_string_array(dev, "gpio-line-names",
419 dev_warn(dev, "failed to read GPIO line names\n");
425 * When more that one gpiochip per device is used, 'count' can
426 * contain at most number gpiochips x chip->ngpio. We have to
427 * correctly distribute all defined lines taking into account
428 * chip->offset as starting point from where we will assign
429 * the names to pins from the 'names' array. Since property
430 * 'gpio-line-names' cannot contains gaps, we have to be sure
431 * we only assign those pins that really exists since chip->ngpio
432 * can be different of the chip->offset.
434 count = (count > chip->offset) ? count - chip->offset : count;
435 if (count > chip->ngpio)
438 for (i = 0; i < count; i++) {
440 * Allow overriding "fixed" names provided by the GPIO
441 * provider. The "fixed" names are more often than not
442 * generic and less informative than the names given in
445 if (names[chip->offset + i] && names[chip->offset + i][0])
446 gdev->descs[i].name = names[chip->offset + i];
454 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
458 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
462 /* Assume by default all GPIOs are valid */
463 bitmap_fill(p, gc->ngpio);
468 static void gpiochip_free_mask(unsigned long **p)
474 static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
476 struct device *dev = &gc->gpiodev->dev;
479 /* Format is "start, count, ..." */
480 size = device_property_count_u32(dev, "gpio-reserved-ranges");
481 if (size > 0 && size % 2 == 0)
487 static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc)
489 struct device *dev = &gc->gpiodev->dev;
494 size = gpiochip_count_reserved_ranges(gc);
498 ranges = kmalloc_array(size, sizeof(*ranges), GFP_KERNEL);
502 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges",
510 u32 count = ranges[--size];
511 u32 start = ranges[--size];
513 if (start >= gc->ngpio || start + count > gc->ngpio)
516 bitmap_clear(gc->valid_mask, start, count);
523 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
527 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
530 gc->valid_mask = gpiochip_allocate_mask(gc);
534 ret = gpiochip_apply_reserved_ranges(gc);
538 if (gc->init_valid_mask)
539 return gc->init_valid_mask(gc,
546 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
548 gpiochip_free_mask(&gc->valid_mask);
551 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
554 * Device Tree platforms are supposed to use "gpio-ranges"
555 * property. This check ensures that the ->add_pin_ranges()
556 * won't be called for them.
558 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges"))
561 if (gc->add_pin_ranges)
562 return gc->add_pin_ranges(gc);
567 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
570 /* No mask means all valid */
571 if (likely(!gc->valid_mask))
573 return test_bit(offset, gc->valid_mask);
575 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
577 static void gpiodev_release(struct device *dev)
579 struct gpio_device *gdev = to_gpio_device(dev);
582 spin_lock_irqsave(&gpio_lock, flags);
583 list_del(&gdev->list);
584 spin_unlock_irqrestore(&gpio_lock, flags);
586 ida_free(&gpio_ida, gdev->id);
587 kfree_const(gdev->label);
592 #ifdef CONFIG_GPIO_CDEV
593 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
594 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
597 * gpiolib_cdev_register() indirectly calls device_add(), which is still
598 * required even when cdev is not selected.
600 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
601 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
604 static int gpiochip_setup_dev(struct gpio_device *gdev)
606 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
610 * If fwnode doesn't belong to another device, it's safe to clear its
613 if (fwnode && !fwnode->dev)
614 fwnode_dev_initialized(fwnode, false);
616 ret = gcdev_register(gdev, gpio_devt);
620 /* From this point, the .release() function cleans up gpio_device */
621 gdev->dev.release = gpiodev_release;
623 ret = gpiochip_sysfs_register(gdev);
625 goto err_remove_device;
627 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
628 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
633 gcdev_unregister(gdev);
637 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
639 struct gpio_desc *desc;
642 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
644 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
649 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
652 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
654 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
655 __func__, gc->label, hog->chip_hwnum, rv);
658 static void machine_gpiochip_add(struct gpio_chip *gc)
660 struct gpiod_hog *hog;
662 mutex_lock(&gpio_machine_hogs_mutex);
664 list_for_each_entry(hog, &gpio_machine_hogs, list) {
665 if (!strcmp(gc->label, hog->chip_label))
666 gpiochip_machine_hog(gc, hog);
669 mutex_unlock(&gpio_machine_hogs_mutex);
672 static void gpiochip_setup_devs(void)
674 struct gpio_device *gdev;
677 list_for_each_entry(gdev, &gpio_devices, list) {
678 ret = gpiochip_setup_dev(gdev);
681 "Failed to initialize gpio device (%d)\n", ret);
685 static void gpiochip_set_data(struct gpio_chip *gc, void *data)
687 gc->gpiodev->data = data;
691 * gpiochip_get_data() - get per-subdriver data for the chip
695 * The per-subdriver data for the chip.
697 void *gpiochip_get_data(struct gpio_chip *gc)
699 return gc->gpiodev->data;
701 EXPORT_SYMBOL_GPL(gpiochip_get_data);
703 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev)
705 u32 ngpios = gc->ngpio;
709 ret = device_property_read_u32(dev, "ngpios", &ngpios);
712 * -ENODATA means that there is no property found and
713 * we want to issue the error message to the user.
714 * Besides that, we want to return different error code
715 * to state that supplied value is not valid.
724 if (gc->ngpio == 0) {
725 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
729 if (gc->ngpio > FASTPATH_NGPIO)
730 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
731 gc->ngpio, FASTPATH_NGPIO);
735 EXPORT_SYMBOL_GPL(gpiochip_get_ngpios);
737 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
738 struct lock_class_key *lock_key,
739 struct lock_class_key *request_key)
741 struct gpio_device *gdev;
748 * First: allocate and populate the internal stat container, and
749 * set up the struct device.
751 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
754 gdev->dev.bus = &gpio_bus_type;
755 gdev->dev.parent = gc->parent;
759 gpiochip_set_data(gc, data);
762 * If the calling driver did not initialize firmware node,
763 * do it here using the parent device, if any.
766 device_set_node(&gdev->dev, gc->fwnode);
768 device_set_node(&gdev->dev, dev_fwnode(gc->parent));
770 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
776 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
780 device_initialize(&gdev->dev);
781 if (gc->parent && gc->parent->driver)
782 gdev->owner = gc->parent->driver->owner;
784 /* TODO: remove chip->owner */
785 gdev->owner = gc->owner;
787 gdev->owner = THIS_MODULE;
789 ret = gpiochip_get_ngpios(gc, &gdev->dev);
791 goto err_free_dev_name;
793 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
796 goto err_free_dev_name;
799 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
805 gdev->ngpio = gc->ngpio;
807 spin_lock_irqsave(&gpio_lock, flags);
810 * TODO: this allocates a Linux GPIO number base in the global
811 * GPIO numberspace for this chip. In the long run we want to
812 * get *rid* of this numberspace and use only descriptors, but
813 * it may be a pipe dream. It will not happen before we get rid
814 * of the sysfs interface anyways.
818 base = gpiochip_find_base(gc->ngpio);
820 spin_unlock_irqrestore(&gpio_lock, flags);
826 * TODO: it should not be necessary to reflect the assigned
827 * base outside of the GPIO subsystem. Go over drivers and
828 * see if anyone makes use of this, else drop this and assign
834 "Static allocation of GPIO base is deprecated, use dynamic allocation.\n");
838 ret = gpiodev_add_to_list(gdev);
840 spin_unlock_irqrestore(&gpio_lock, flags);
841 chip_err(gc, "GPIO integer space overlap, cannot add chip\n");
845 for (i = 0; i < gc->ngpio; i++)
846 gdev->descs[i].gdev = gdev;
848 spin_unlock_irqrestore(&gpio_lock, flags);
850 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier);
851 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier);
852 init_rwsem(&gdev->sem);
854 #ifdef CONFIG_PINCTRL
855 INIT_LIST_HEAD(&gdev->pin_ranges);
859 ret = gpiochip_set_desc_names(gc);
861 goto err_remove_from_list;
863 ret = gpiochip_set_names(gc);
865 goto err_remove_from_list;
867 ret = gpiochip_init_valid_mask(gc);
869 goto err_remove_from_list;
871 ret = of_gpiochip_add(gc);
873 goto err_free_gpiochip_mask;
875 for (i = 0; i < gc->ngpio; i++) {
876 struct gpio_desc *desc = &gdev->descs[i];
878 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
879 assign_bit(FLAG_IS_OUT,
880 &desc->flags, !gc->get_direction(gc, i));
882 assign_bit(FLAG_IS_OUT,
883 &desc->flags, !gc->direction_input);
887 ret = gpiochip_add_pin_ranges(gc);
889 goto err_remove_of_chip;
891 acpi_gpiochip_add(gc);
893 machine_gpiochip_add(gc);
895 ret = gpiochip_irqchip_init_valid_mask(gc);
897 goto err_remove_acpi_chip;
899 ret = gpiochip_irqchip_init_hw(gc);
901 goto err_remove_acpi_chip;
903 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
905 goto err_remove_irqchip_mask;
908 * By first adding the chardev, and then adding the device,
909 * we get a device node entry in sysfs under
910 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
911 * coldplug of device nodes and other udev business.
912 * We can do this only if gpiolib has been initialized.
913 * Otherwise, defer until later.
915 if (gpiolib_initialized) {
916 ret = gpiochip_setup_dev(gdev);
918 goto err_remove_irqchip;
923 gpiochip_irqchip_remove(gc);
924 err_remove_irqchip_mask:
925 gpiochip_irqchip_free_valid_mask(gc);
926 err_remove_acpi_chip:
927 acpi_gpiochip_remove(gc);
929 gpiochip_free_hogs(gc);
930 of_gpiochip_remove(gc);
931 err_free_gpiochip_mask:
932 gpiochip_remove_pin_ranges(gc);
933 gpiochip_free_valid_mask(gc);
934 if (gdev->dev.release) {
935 /* release() has been registered by gpiochip_setup_dev() */
936 gpio_device_put(gdev);
937 goto err_print_message;
939 err_remove_from_list:
940 spin_lock_irqsave(&gpio_lock, flags);
941 list_del(&gdev->list);
942 spin_unlock_irqrestore(&gpio_lock, flags);
944 kfree_const(gdev->label);
948 kfree(dev_name(&gdev->dev));
950 ida_free(&gpio_ida, gdev->id);
954 /* failures here can mean systems won't boot... */
955 if (ret != -EPROBE_DEFER) {
956 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
957 base, base + (int)gc->ngpio - 1,
958 gc->label ? : "generic", ret);
962 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
965 * gpiochip_remove() - unregister a gpio_chip
966 * @gc: the chip to unregister
968 * A gpio_chip with any GPIOs still requested may not be removed.
970 void gpiochip_remove(struct gpio_chip *gc)
972 struct gpio_device *gdev = gc->gpiodev;
976 down_write(&gdev->sem);
978 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
979 gpiochip_sysfs_unregister(gdev);
980 gpiochip_free_hogs(gc);
981 /* Numb the device, cancelling all outstanding operations */
983 gpiochip_irqchip_remove(gc);
984 acpi_gpiochip_remove(gc);
985 of_gpiochip_remove(gc);
986 gpiochip_remove_pin_ranges(gc);
987 gpiochip_free_valid_mask(gc);
989 * We accept no more calls into the driver from this point, so
990 * NULL the driver data pointer.
992 gpiochip_set_data(gc, NULL);
994 spin_lock_irqsave(&gpio_lock, flags);
995 for (i = 0; i < gdev->ngpio; i++) {
996 if (gpiochip_is_requested(gc, i))
999 spin_unlock_irqrestore(&gpio_lock, flags);
1001 if (i != gdev->ngpio)
1002 dev_crit(&gdev->dev,
1003 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1006 * The gpiochip side puts its use of the device to rest here:
1007 * if there are no userspace clients, the chardev and device will
1008 * be removed, else it will be dangling until the last user is
1011 gcdev_unregister(gdev);
1012 up_write(&gdev->sem);
1013 gpio_device_put(gdev);
1015 EXPORT_SYMBOL_GPL(gpiochip_remove);
1018 * gpiochip_find() - iterator for locating a specific gpio_chip
1019 * @data: data to pass to match function
1020 * @match: Callback function to check gpio_chip
1022 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1023 * determined by a user supplied @match callback. The callback should return
1024 * 0 if the device doesn't match and non-zero if it does. If the callback is
1025 * non-zero, this function will return to the caller and not iterate over any
1028 struct gpio_chip *gpiochip_find(void *data,
1029 int (*match)(struct gpio_chip *gc,
1032 struct gpio_device *gdev;
1033 struct gpio_chip *gc = NULL;
1034 unsigned long flags;
1036 spin_lock_irqsave(&gpio_lock, flags);
1037 list_for_each_entry(gdev, &gpio_devices, list)
1038 if (gdev->chip && match(gdev->chip, data)) {
1043 spin_unlock_irqrestore(&gpio_lock, flags);
1047 EXPORT_SYMBOL_GPL(gpiochip_find);
1049 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
1051 const char *name = data;
1053 return !strcmp(gc->label, name);
1056 static struct gpio_chip *find_chip_by_name(const char *name)
1058 return gpiochip_find((void *)name, gpiochip_match_name);
1061 #ifdef CONFIG_GPIOLIB_IRQCHIP
1064 * The following is irqchip helper code for gpiochips.
1067 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1069 struct gpio_irq_chip *girq = &gc->irq;
1074 return girq->init_hw(gc);
1077 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1079 struct gpio_irq_chip *girq = &gc->irq;
1081 if (!girq->init_valid_mask)
1084 girq->valid_mask = gpiochip_allocate_mask(gc);
1085 if (!girq->valid_mask)
1088 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1093 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1095 gpiochip_free_mask(&gc->irq.valid_mask);
1098 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1099 unsigned int offset)
1101 if (!gpiochip_line_is_valid(gc, offset))
1103 /* No mask means all valid */
1104 if (likely(!gc->irq.valid_mask))
1106 return test_bit(offset, gc->irq.valid_mask);
1108 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1110 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1113 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1115 * @gc: the gpiochip to set the irqchip hierarchical handler to
1116 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1117 * will then percolate up to the parent
1119 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1120 struct irq_chip *irqchip)
1122 /* DT will deal with mapping each IRQ as we go along */
1123 if (is_of_node(gc->irq.fwnode))
1127 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1128 * irqs upfront instead of dynamically since we don't have the
1129 * dynamic type of allocation that hardware description languages
1130 * provide. Once all GPIO drivers using board files are gone from
1131 * the kernel we can delete this code, but for a transitional period
1132 * it is necessary to keep this around.
1134 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1138 for (i = 0; i < gc->ngpio; i++) {
1139 struct irq_fwspec fwspec;
1140 unsigned int parent_hwirq;
1141 unsigned int parent_type;
1142 struct gpio_irq_chip *girq = &gc->irq;
1145 * We call the child to parent translation function
1146 * only to check if the child IRQ is valid or not.
1147 * Just pick the rising edge type here as that is what
1148 * we likely need to support.
1150 ret = girq->child_to_parent_hwirq(gc, i,
1151 IRQ_TYPE_EDGE_RISING,
1155 chip_err(gc, "skip set-up on hwirq %d\n",
1160 fwspec.fwnode = gc->irq.fwnode;
1161 /* This is the hwirq for the GPIO line side of things */
1162 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1163 /* Just pick something */
1164 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1165 fwspec.param_count = 2;
1166 ret = irq_domain_alloc_irqs(gc->irq.domain, 1,
1167 NUMA_NO_NODE, &fwspec);
1170 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1177 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1182 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1183 struct irq_fwspec *fwspec,
1184 unsigned long *hwirq,
1187 /* We support standard DT translation */
1188 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1189 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1192 /* This is for board files and others not using DT */
1193 if (is_fwnode_irqchip(fwspec->fwnode)) {
1196 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1199 WARN_ON(*type == IRQ_TYPE_NONE);
1205 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1207 unsigned int nr_irqs,
1210 struct gpio_chip *gc = d->host_data;
1211 irq_hw_number_t hwirq;
1212 unsigned int type = IRQ_TYPE_NONE;
1213 struct irq_fwspec *fwspec = data;
1214 union gpio_irq_fwspec gpio_parent_fwspec = {};
1215 unsigned int parent_hwirq;
1216 unsigned int parent_type;
1217 struct gpio_irq_chip *girq = &gc->irq;
1221 * The nr_irqs parameter is always one except for PCI multi-MSI
1222 * so this should not happen.
1224 WARN_ON(nr_irqs != 1);
1226 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1230 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1232 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1233 &parent_hwirq, &parent_type);
1235 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1238 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1241 * We set handle_bad_irq because the .set_type() should
1242 * always be invoked and set the right type of handler.
1244 irq_domain_set_info(d,
1253 /* This parent only handles asserted level IRQs */
1254 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
1255 parent_hwirq, parent_type);
1259 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1261 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1262 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
1264 * If the parent irqdomain is msi, the interrupts have already
1265 * been allocated, so the EEXIST is good.
1267 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1271 "failed to allocate parent hwirq %d for hwirq %lu\n",
1272 parent_hwirq, hwirq);
1277 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1278 unsigned int offset)
1283 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1285 ops->activate = gpiochip_irq_domain_activate;
1286 ops->deactivate = gpiochip_irq_domain_deactivate;
1287 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1290 * We only allow overriding the translate() and free() functions for
1291 * hierarchical chips, and this should only be done if the user
1292 * really need something other than 1:1 translation for translate()
1293 * callback and free if user wants to free up any resources which
1294 * were allocated during callbacks, for example populate_parent_alloc_arg.
1296 if (!ops->translate)
1297 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1299 ops->free = irq_domain_free_irqs_common;
1302 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1304 struct irq_domain *domain;
1306 if (!gc->irq.child_to_parent_hwirq ||
1308 chip_err(gc, "missing irqdomain vital data\n");
1309 return ERR_PTR(-EINVAL);
1312 if (!gc->irq.child_offset_to_irq)
1313 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1315 if (!gc->irq.populate_parent_alloc_arg)
1316 gc->irq.populate_parent_alloc_arg =
1317 gpiochip_populate_parent_fwspec_twocell;
1319 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1321 domain = irq_domain_create_hierarchy(
1322 gc->irq.parent_domain,
1326 &gc->irq.child_irq_domain_ops,
1330 return ERR_PTR(-ENOMEM);
1332 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1337 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1339 return !!gc->irq.parent_domain;
1342 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1343 union gpio_irq_fwspec *gfwspec,
1344 unsigned int parent_hwirq,
1345 unsigned int parent_type)
1347 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1349 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1350 fwspec->param_count = 2;
1351 fwspec->param[0] = parent_hwirq;
1352 fwspec->param[1] = parent_type;
1356 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1358 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1359 union gpio_irq_fwspec *gfwspec,
1360 unsigned int parent_hwirq,
1361 unsigned int parent_type)
1363 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1365 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1366 fwspec->param_count = 4;
1367 fwspec->param[0] = 0;
1368 fwspec->param[1] = parent_hwirq;
1369 fwspec->param[2] = 0;
1370 fwspec->param[3] = parent_type;
1374 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1378 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1380 return ERR_PTR(-EINVAL);
1383 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1388 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1391 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1392 * @d: the irqdomain used by this irqchip
1393 * @irq: the global irq number used by this GPIO irqchip irq
1394 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1396 * This function will set up the mapping for a certain IRQ line on a
1397 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1398 * stored inside the gpiochip.
1400 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hwirq)
1402 struct gpio_chip *gc = d->host_data;
1405 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1408 irq_set_chip_data(irq, gc);
1410 * This lock class tells lockdep that GPIO irqs are in a different
1411 * category than their parents, so it won't report false recursion.
1413 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1414 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1415 /* Chips that use nested thread handlers have them marked */
1416 if (gc->irq.threaded)
1417 irq_set_nested_thread(irq, 1);
1418 irq_set_noprobe(irq);
1420 if (gc->irq.num_parents == 1)
1421 ret = irq_set_parent(irq, gc->irq.parents[0]);
1422 else if (gc->irq.map)
1423 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1429 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1430 * is passed as default type.
1432 if (gc->irq.default_type != IRQ_TYPE_NONE)
1433 irq_set_irq_type(irq, gc->irq.default_type);
1437 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1439 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1441 struct gpio_chip *gc = d->host_data;
1443 if (gc->irq.threaded)
1444 irq_set_nested_thread(irq, 0);
1445 irq_set_chip_and_handler(irq, NULL, NULL);
1446 irq_set_chip_data(irq, NULL);
1448 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1450 static const struct irq_domain_ops gpiochip_domain_ops = {
1451 .map = gpiochip_irq_map,
1452 .unmap = gpiochip_irq_unmap,
1453 /* Virtually all GPIO irqchips are twocell:ed */
1454 .xlate = irq_domain_xlate_twocell,
1457 static struct irq_domain *gpiochip_simple_create_domain(struct gpio_chip *gc)
1459 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1460 struct irq_domain *domain;
1462 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first,
1463 &gpiochip_domain_ops, gc);
1465 return ERR_PTR(-EINVAL);
1471 * TODO: move these activate/deactivate in under the hierarchicial
1472 * irqchip implementation as static once SPMI and SSBI (all external
1473 * users) are phased over.
1476 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1477 * @domain: The IRQ domain used by this IRQ chip
1478 * @data: Outermost irq_data associated with the IRQ
1479 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1481 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1482 * used as the activate function for the &struct irq_domain_ops. The host_data
1483 * for the IRQ domain must be the &struct gpio_chip.
1485 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1486 struct irq_data *data, bool reserve)
1488 struct gpio_chip *gc = domain->host_data;
1489 unsigned int hwirq = irqd_to_hwirq(data);
1491 return gpiochip_lock_as_irq(gc, hwirq);
1493 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1496 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1497 * @domain: The IRQ domain used by this IRQ chip
1498 * @data: Outermost irq_data associated with the IRQ
1500 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1501 * be used as the deactivate function for the &struct irq_domain_ops. The
1502 * host_data for the IRQ domain must be the &struct gpio_chip.
1504 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1505 struct irq_data *data)
1507 struct gpio_chip *gc = domain->host_data;
1508 unsigned int hwirq = irqd_to_hwirq(data);
1510 return gpiochip_unlock_as_irq(gc, hwirq);
1512 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1514 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1516 struct irq_domain *domain = gc->irq.domain;
1518 #ifdef CONFIG_GPIOLIB_IRQCHIP
1520 * Avoid race condition with other code, which tries to lookup
1521 * an IRQ before the irqchip has been properly registered,
1522 * i.e. while gpiochip is still being brought up.
1524 if (!gc->irq.initialized)
1525 return -EPROBE_DEFER;
1528 if (!gpiochip_irqchip_irq_valid(gc, offset))
1531 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1532 if (irq_domain_is_hierarchy(domain)) {
1533 struct irq_fwspec spec;
1535 spec.fwnode = domain->fwnode;
1536 spec.param_count = 2;
1537 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1538 spec.param[1] = IRQ_TYPE_NONE;
1540 return irq_create_fwspec_mapping(&spec);
1544 return irq_create_mapping(domain, offset);
1547 int gpiochip_irq_reqres(struct irq_data *d)
1549 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1550 unsigned int hwirq = irqd_to_hwirq(d);
1552 return gpiochip_reqres_irq(gc, hwirq);
1554 EXPORT_SYMBOL(gpiochip_irq_reqres);
1556 void gpiochip_irq_relres(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_relres_irq(gc, hwirq);
1563 EXPORT_SYMBOL(gpiochip_irq_relres);
1565 static void gpiochip_irq_mask(struct irq_data *d)
1567 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1568 unsigned int hwirq = irqd_to_hwirq(d);
1570 if (gc->irq.irq_mask)
1571 gc->irq.irq_mask(d);
1572 gpiochip_disable_irq(gc, hwirq);
1575 static void gpiochip_irq_unmask(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 gpiochip_enable_irq(gc, hwirq);
1581 if (gc->irq.irq_unmask)
1582 gc->irq.irq_unmask(d);
1585 static void gpiochip_irq_enable(struct irq_data *d)
1587 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1588 unsigned int hwirq = irqd_to_hwirq(d);
1590 gpiochip_enable_irq(gc, hwirq);
1591 gc->irq.irq_enable(d);
1594 static void gpiochip_irq_disable(struct irq_data *d)
1596 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1597 unsigned int hwirq = irqd_to_hwirq(d);
1599 gc->irq.irq_disable(d);
1600 gpiochip_disable_irq(gc, hwirq);
1603 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1605 struct irq_chip *irqchip = gc->irq.chip;
1607 if (irqchip->flags & IRQCHIP_IMMUTABLE)
1610 chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
1612 if (!irqchip->irq_request_resources &&
1613 !irqchip->irq_release_resources) {
1614 irqchip->irq_request_resources = gpiochip_irq_reqres;
1615 irqchip->irq_release_resources = gpiochip_irq_relres;
1617 if (WARN_ON(gc->irq.irq_enable))
1619 /* Check if the irqchip already has this hook... */
1620 if (irqchip->irq_enable == gpiochip_irq_enable ||
1621 irqchip->irq_mask == gpiochip_irq_mask) {
1623 * ...and if so, give a gentle warning that this is bad
1627 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1631 if (irqchip->irq_disable) {
1632 gc->irq.irq_disable = irqchip->irq_disable;
1633 irqchip->irq_disable = gpiochip_irq_disable;
1635 gc->irq.irq_mask = irqchip->irq_mask;
1636 irqchip->irq_mask = gpiochip_irq_mask;
1639 if (irqchip->irq_enable) {
1640 gc->irq.irq_enable = irqchip->irq_enable;
1641 irqchip->irq_enable = gpiochip_irq_enable;
1643 gc->irq.irq_unmask = irqchip->irq_unmask;
1644 irqchip->irq_unmask = gpiochip_irq_unmask;
1648 static int gpiochip_irqchip_add_allocated_domain(struct gpio_chip *gc,
1649 struct irq_domain *domain,
1650 bool allocated_externally)
1656 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1658 gc->to_irq = gpiochip_to_irq;
1659 gc->irq.domain = domain;
1660 gc->irq.domain_is_allocated_externally = allocated_externally;
1663 * Using barrier() here to prevent compiler from reordering
1664 * gc->irq.initialized before adding irqdomain.
1668 gc->irq.initialized = true;
1674 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1675 * @gc: the GPIO chip to add the IRQ chip to
1676 * @lock_key: lockdep class for IRQ lock
1677 * @request_key: lockdep class for IRQ request
1679 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1680 struct lock_class_key *lock_key,
1681 struct lock_class_key *request_key)
1683 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1684 struct irq_chip *irqchip = gc->irq.chip;
1685 struct irq_domain *domain;
1693 if (gc->irq.parent_handler && gc->can_sleep) {
1694 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1698 type = gc->irq.default_type;
1701 * Specifying a default trigger is a terrible idea if DT or ACPI is
1702 * used to configure the interrupts, as you may end up with
1703 * conflicting triggers. Tell the user, and reset to NONE.
1705 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1706 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1707 type = IRQ_TYPE_NONE;
1709 gc->irq.default_type = type;
1710 gc->irq.lock_key = lock_key;
1711 gc->irq.request_key = request_key;
1713 /* If a parent irqdomain is provided, let's build a hierarchy */
1714 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1715 domain = gpiochip_hierarchy_create_domain(gc);
1717 domain = gpiochip_simple_create_domain(gc);
1720 return PTR_ERR(domain);
1722 if (gc->irq.parent_handler) {
1723 for (i = 0; i < gc->irq.num_parents; i++) {
1726 if (gc->irq.per_parent_data)
1727 data = gc->irq.parent_handler_data_array[i];
1729 data = gc->irq.parent_handler_data ?: gc;
1732 * The parent IRQ chip is already using the chip_data
1733 * for this IRQ chip, so our callbacks simply use the
1736 irq_set_chained_handler_and_data(gc->irq.parents[i],
1737 gc->irq.parent_handler,
1742 gpiochip_set_irq_hooks(gc);
1744 ret = gpiochip_irqchip_add_allocated_domain(gc, domain, false);
1748 acpi_gpiochip_request_interrupts(gc);
1754 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1755 * @gc: the gpiochip to remove the irqchip from
1757 * This is called only from gpiochip_remove()
1759 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1761 struct irq_chip *irqchip = gc->irq.chip;
1762 unsigned int offset;
1764 acpi_gpiochip_free_interrupts(gc);
1766 if (irqchip && gc->irq.parent_handler) {
1767 struct gpio_irq_chip *irq = &gc->irq;
1770 for (i = 0; i < irq->num_parents; i++)
1771 irq_set_chained_handler_and_data(irq->parents[i],
1775 /* Remove all IRQ mappings and delete the domain */
1776 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) {
1779 for (offset = 0; offset < gc->ngpio; offset++) {
1780 if (!gpiochip_irqchip_irq_valid(gc, offset))
1783 irq = irq_find_mapping(gc->irq.domain, offset);
1784 irq_dispose_mapping(irq);
1787 irq_domain_remove(gc->irq.domain);
1790 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
1791 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1792 irqchip->irq_request_resources = NULL;
1793 irqchip->irq_release_resources = NULL;
1795 if (irqchip->irq_enable == gpiochip_irq_enable) {
1796 irqchip->irq_enable = gc->irq.irq_enable;
1797 irqchip->irq_disable = gc->irq.irq_disable;
1800 gc->irq.irq_enable = NULL;
1801 gc->irq.irq_disable = NULL;
1802 gc->irq.chip = NULL;
1804 gpiochip_irqchip_free_valid_mask(gc);
1808 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1809 * @gc: the gpiochip to add the irqchip to
1810 * @domain: the irqdomain to add to the gpiochip
1812 * This function adds an IRQ domain to the gpiochip.
1814 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1815 struct irq_domain *domain)
1817 return gpiochip_irqchip_add_allocated_domain(gc, domain, true);
1819 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1821 #else /* CONFIG_GPIOLIB_IRQCHIP */
1823 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1824 struct lock_class_key *lock_key,
1825 struct lock_class_key *request_key)
1829 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1831 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1836 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1840 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1843 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1846 * gpiochip_generic_request() - request the gpio function for a pin
1847 * @gc: the gpiochip owning the GPIO
1848 * @offset: the offset of the GPIO to request for GPIO function
1850 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
1852 #ifdef CONFIG_PINCTRL
1853 if (list_empty(&gc->gpiodev->pin_ranges))
1857 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1859 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1862 * gpiochip_generic_free() - free the gpio function from a pin
1863 * @gc: the gpiochip to request the gpio function for
1864 * @offset: the offset of the GPIO to free from GPIO function
1866 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
1868 #ifdef CONFIG_PINCTRL
1869 if (list_empty(&gc->gpiodev->pin_ranges))
1873 pinctrl_gpio_free(gc->gpiodev->base + offset);
1875 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1878 * gpiochip_generic_config() - apply configuration for a pin
1879 * @gc: the gpiochip owning the GPIO
1880 * @offset: the offset of the GPIO to apply the configuration
1881 * @config: the configuration to be applied
1883 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
1884 unsigned long config)
1886 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1888 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1890 #ifdef CONFIG_PINCTRL
1893 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1894 * @gc: the gpiochip to add the range for
1895 * @pctldev: the pin controller to map to
1896 * @gpio_offset: the start offset in the current gpio_chip number space
1897 * @pin_group: name of the pin group inside the pin controller
1899 * Calling this function directly from a DeviceTree-supported
1900 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1901 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1902 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1904 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1905 struct pinctrl_dev *pctldev,
1906 unsigned int gpio_offset, const char *pin_group)
1908 struct gpio_pin_range *pin_range;
1909 struct gpio_device *gdev = gc->gpiodev;
1912 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1914 chip_err(gc, "failed to allocate pin ranges\n");
1918 /* Use local offset as range ID */
1919 pin_range->range.id = gpio_offset;
1920 pin_range->range.gc = gc;
1921 pin_range->range.name = gc->label;
1922 pin_range->range.base = gdev->base + gpio_offset;
1923 pin_range->pctldev = pctldev;
1925 ret = pinctrl_get_group_pins(pctldev, pin_group,
1926 &pin_range->range.pins,
1927 &pin_range->range.npins);
1933 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1935 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1936 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1937 pinctrl_dev_get_devname(pctldev), pin_group);
1939 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1943 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1946 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1947 * @gc: the gpiochip to add the range for
1948 * @pinctl_name: the dev_name() of the pin controller to map to
1949 * @gpio_offset: the start offset in the current gpio_chip number space
1950 * @pin_offset: the start offset in the pin controller number space
1951 * @npins: the number of pins from the offset of each pin space (GPIO and
1952 * pin controller) to accumulate in this range
1955 * 0 on success, or a negative error-code on failure.
1957 * Calling this function directly from a DeviceTree-supported
1958 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1959 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1960 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1962 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1963 unsigned int gpio_offset, unsigned int pin_offset,
1966 struct gpio_pin_range *pin_range;
1967 struct gpio_device *gdev = gc->gpiodev;
1970 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1972 chip_err(gc, "failed to allocate pin ranges\n");
1976 /* Use local offset as range ID */
1977 pin_range->range.id = gpio_offset;
1978 pin_range->range.gc = gc;
1979 pin_range->range.name = gc->label;
1980 pin_range->range.base = gdev->base + gpio_offset;
1981 pin_range->range.pin_base = pin_offset;
1982 pin_range->range.npins = npins;
1983 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1985 if (IS_ERR(pin_range->pctldev)) {
1986 ret = PTR_ERR(pin_range->pctldev);
1987 chip_err(gc, "could not create pin range\n");
1991 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1992 gpio_offset, gpio_offset + npins - 1,
1994 pin_offset, pin_offset + npins - 1);
1996 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2000 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2003 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2004 * @gc: the chip to remove all the mappings for
2006 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
2008 struct gpio_pin_range *pin_range, *tmp;
2009 struct gpio_device *gdev = gc->gpiodev;
2011 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2012 list_del(&pin_range->node);
2013 pinctrl_remove_gpio_range(pin_range->pctldev,
2018 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2020 #endif /* CONFIG_PINCTRL */
2022 /* These "optional" allocation calls help prevent drivers from stomping
2023 * on each other, and help provide better diagnostics in debugfs.
2024 * They're called even less than the "set direction" calls.
2026 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2028 struct gpio_chip *gc = desc->gdev->chip;
2030 unsigned long flags;
2034 label = kstrdup_const(label, GFP_KERNEL);
2039 spin_lock_irqsave(&gpio_lock, flags);
2041 /* NOTE: gpio_request() can be called in early boot,
2042 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2045 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2046 desc_set_label(desc, label ? : "?");
2049 goto out_free_unlock;
2053 /* gc->request may sleep */
2054 spin_unlock_irqrestore(&gpio_lock, flags);
2055 offset = gpio_chip_hwgpio(desc);
2056 if (gpiochip_line_is_valid(gc, offset))
2057 ret = gc->request(gc, offset);
2060 spin_lock_irqsave(&gpio_lock, flags);
2063 desc_set_label(desc, NULL);
2064 clear_bit(FLAG_REQUESTED, &desc->flags);
2065 goto out_free_unlock;
2068 if (gc->get_direction) {
2069 /* gc->get_direction may sleep */
2070 spin_unlock_irqrestore(&gpio_lock, flags);
2071 gpiod_get_direction(desc);
2072 spin_lock_irqsave(&gpio_lock, flags);
2074 spin_unlock_irqrestore(&gpio_lock, flags);
2078 spin_unlock_irqrestore(&gpio_lock, flags);
2084 * This descriptor validation needs to be inserted verbatim into each
2085 * function taking a descriptor, so we need to use a preprocessor
2086 * macro to avoid endless duplication. If the desc is NULL it is an
2087 * optional GPIO and calls should just bail out.
2089 static int validate_desc(const struct gpio_desc *desc, const char *func)
2094 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2095 return PTR_ERR(desc);
2098 pr_warn("%s: invalid GPIO (no device)\n", func);
2101 if (!desc->gdev->chip) {
2102 dev_warn(&desc->gdev->dev,
2103 "%s: backing chip is gone\n", func);
2109 #define VALIDATE_DESC(desc) do { \
2110 int __valid = validate_desc(desc, __func__); \
2115 #define VALIDATE_DESC_VOID(desc) do { \
2116 int __valid = validate_desc(desc, __func__); \
2121 int gpiod_request(struct gpio_desc *desc, const char *label)
2123 int ret = -EPROBE_DEFER;
2125 VALIDATE_DESC(desc);
2127 if (try_module_get(desc->gdev->owner)) {
2128 ret = gpiod_request_commit(desc, label);
2130 module_put(desc->gdev->owner);
2132 gpio_device_get(desc->gdev);
2136 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2141 static bool gpiod_free_commit(struct gpio_desc *desc)
2144 unsigned long flags;
2145 struct gpio_chip *gc;
2149 spin_lock_irqsave(&gpio_lock, flags);
2151 gc = desc->gdev->chip;
2152 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2154 spin_unlock_irqrestore(&gpio_lock, flags);
2155 might_sleep_if(gc->can_sleep);
2156 gc->free(gc, gpio_chip_hwgpio(desc));
2157 spin_lock_irqsave(&gpio_lock, flags);
2159 kfree_const(desc->label);
2160 desc_set_label(desc, NULL);
2161 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2162 clear_bit(FLAG_REQUESTED, &desc->flags);
2163 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2164 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2165 clear_bit(FLAG_PULL_UP, &desc->flags);
2166 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2167 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2168 clear_bit(FLAG_EDGE_RISING, &desc->flags);
2169 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
2170 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2171 #ifdef CONFIG_OF_DYNAMIC
2174 #ifdef CONFIG_GPIO_CDEV
2175 WRITE_ONCE(desc->debounce_period_us, 0);
2180 spin_unlock_irqrestore(&gpio_lock, flags);
2181 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED);
2186 void gpiod_free(struct gpio_desc *desc)
2189 * We must not use VALIDATE_DESC_VOID() as the underlying gdev->chip
2190 * may already be NULL but we still want to put the references.
2195 if (!gpiod_free_commit(desc))
2196 WARN_ON(extra_checks);
2198 module_put(desc->gdev->owner);
2199 gpio_device_put(desc->gdev);
2203 * gpiochip_is_requested - return string iff signal was requested
2204 * @gc: controller managing the signal
2205 * @offset: of signal within controller's 0..(ngpio - 1) range
2207 * Returns NULL if the GPIO is not currently requested, else a string.
2208 * The string returned is the label passed to gpio_request(); if none has been
2209 * passed it is a meaningless, non-NULL constant.
2211 * This function is for use by GPIO controller drivers. The label can
2212 * help with diagnostics, and knowing that the signal is used as a GPIO
2213 * can help avoid accidentally multiplexing it to another controller.
2215 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
2217 struct gpio_desc *desc;
2219 desc = gpiochip_get_desc(gc, offset);
2223 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2227 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2230 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2232 * @hwnum: hardware number of the GPIO for which to request the descriptor
2233 * @label: label for the GPIO
2234 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2235 * specify things like line inversion semantics with the machine flags
2236 * such as GPIO_OUT_LOW
2237 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2238 * can be used to specify consumer semantics such as open drain
2240 * Function allows GPIO chip drivers to request and use their own GPIO
2241 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2242 * function will not increase reference count of the GPIO chip module. This
2243 * allows the GPIO chip module to be unloaded as needed (we assume that the
2244 * GPIO chip driver handles freeing the GPIOs it has requested).
2247 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2250 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2253 enum gpio_lookup_flags lflags,
2254 enum gpiod_flags dflags)
2256 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2260 chip_err(gc, "failed to get GPIO descriptor\n");
2264 ret = gpiod_request_commit(desc, label);
2266 return ERR_PTR(ret);
2268 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2270 chip_err(gc, "setup of own GPIO %s failed\n", label);
2271 gpiod_free_commit(desc);
2272 return ERR_PTR(ret);
2277 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2280 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2281 * @desc: GPIO descriptor to free
2283 * Function frees the given GPIO requested previously with
2284 * gpiochip_request_own_desc().
2286 void gpiochip_free_own_desc(struct gpio_desc *desc)
2289 gpiod_free_commit(desc);
2291 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2294 * Drivers MUST set GPIO direction before making get/set calls. In
2295 * some cases this is done in early boot, before IRQs are enabled.
2297 * As a rule these aren't called more than once (except for drivers
2298 * using the open-drain emulation idiom) so these are natural places
2299 * to accumulate extra debugging checks. Note that we can't (yet)
2300 * rely on gpio_request() having been called beforehand.
2303 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2304 unsigned long config)
2306 if (!gc->set_config)
2309 return gc->set_config(gc, offset, config);
2312 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2313 enum pin_config_param mode,
2316 struct gpio_chip *gc = desc->gdev->chip;
2317 unsigned long config;
2319 config = pinconf_to_config_packed(mode, argument);
2320 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2323 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2324 enum pin_config_param mode,
2327 struct device *dev = &desc->gdev->dev;
2328 int gpio = gpio_chip_hwgpio(desc);
2331 ret = gpio_set_config_with_argument(desc, mode, argument);
2332 if (ret != -ENOTSUPP)
2336 case PIN_CONFIG_PERSIST_STATE:
2337 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2346 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2348 return gpio_set_config_with_argument(desc, mode, 0);
2351 static int gpio_set_bias(struct gpio_desc *desc)
2353 enum pin_config_param bias;
2356 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2357 bias = PIN_CONFIG_BIAS_DISABLE;
2358 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2359 bias = PIN_CONFIG_BIAS_PULL_UP;
2360 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2361 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2366 case PIN_CONFIG_BIAS_PULL_DOWN:
2367 case PIN_CONFIG_BIAS_PULL_UP:
2376 return gpio_set_config_with_argument_optional(desc, bias, arg);
2380 * gpio_set_debounce_timeout() - Set debounce timeout
2381 * @desc: GPIO descriptor to set the debounce timeout
2382 * @debounce: Debounce timeout in microseconds
2384 * The function calls the certain GPIO driver to set debounce timeout
2387 * Returns 0 on success, or negative error code otherwise.
2389 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2391 return gpio_set_config_with_argument_optional(desc,
2392 PIN_CONFIG_INPUT_DEBOUNCE,
2397 * gpiod_direction_input - set the GPIO direction to input
2398 * @desc: GPIO to set to input
2400 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2401 * be called safely on it.
2403 * Return 0 in case of success, else an error code.
2405 int gpiod_direction_input(struct gpio_desc *desc)
2407 struct gpio_chip *gc;
2410 VALIDATE_DESC(desc);
2411 gc = desc->gdev->chip;
2414 * It is legal to have no .get() and .direction_input() specified if
2415 * the chip is output-only, but you can't specify .direction_input()
2416 * and not support the .get() operation, that doesn't make sense.
2418 if (!gc->get && gc->direction_input) {
2420 "%s: missing get() but have direction_input()\n",
2426 * If we have a .direction_input() callback, things are simple,
2427 * just call it. Else we are some input-only chip so try to check the
2428 * direction (if .get_direction() is supported) else we silently
2429 * assume we are in input mode after this.
2431 if (gc->direction_input) {
2432 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2433 } else if (gc->get_direction &&
2434 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2436 "%s: missing direction_input() operation and line is output\n",
2441 clear_bit(FLAG_IS_OUT, &desc->flags);
2442 ret = gpio_set_bias(desc);
2445 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2449 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2451 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2453 struct gpio_chip *gc = desc->gdev->chip;
2458 * It's OK not to specify .direction_output() if the gpiochip is
2459 * output-only, but if there is then not even a .set() operation it
2460 * is pretty tricky to drive the output line.
2462 if (!gc->set && !gc->direction_output) {
2464 "%s: missing set() and direction_output() operations\n",
2469 if (gc->direction_output) {
2470 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2472 /* Check that we are in output mode if we can */
2473 if (gc->get_direction &&
2474 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2476 "%s: missing direction_output() operation\n",
2481 * If we can't actively set the direction, we are some
2482 * output-only chip, so just drive the output as desired.
2484 gc->set(gc, gpio_chip_hwgpio(desc), val);
2488 set_bit(FLAG_IS_OUT, &desc->flags);
2489 trace_gpio_value(desc_to_gpio(desc), 0, val);
2490 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2495 * gpiod_direction_output_raw - set the GPIO direction to output
2496 * @desc: GPIO to set to output
2497 * @value: initial output value of the GPIO
2499 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2500 * be called safely on it. The initial value of the output must be specified
2501 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2503 * Return 0 in case of success, else an error code.
2505 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2507 VALIDATE_DESC(desc);
2508 return gpiod_direction_output_raw_commit(desc, value);
2510 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2513 * gpiod_direction_output - set the GPIO direction to output
2514 * @desc: GPIO to set to output
2515 * @value: initial output value of the GPIO
2517 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2518 * be called safely on it. The initial value of the output must be specified
2519 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2522 * Return 0 in case of success, else an error code.
2524 int gpiod_direction_output(struct gpio_desc *desc, int value)
2528 VALIDATE_DESC(desc);
2529 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2534 /* GPIOs used for enabled IRQs shall not be set as output */
2535 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2536 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2538 "%s: tried to set a GPIO tied to an IRQ as output\n",
2543 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2544 /* First see if we can enable open drain in hardware */
2545 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2547 goto set_output_value;
2548 /* Emulate open drain by not actively driving the line high */
2550 ret = gpiod_direction_input(desc);
2551 goto set_output_flag;
2553 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2554 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2556 goto set_output_value;
2557 /* Emulate open source by not actively driving the line low */
2559 ret = gpiod_direction_input(desc);
2560 goto set_output_flag;
2563 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2567 ret = gpio_set_bias(desc);
2570 return gpiod_direction_output_raw_commit(desc, value);
2574 * When emulating open-source or open-drain functionalities by not
2575 * actively driving the line (setting mode to input) we still need to
2576 * set the IS_OUT flag or otherwise we won't be able to set the line
2580 set_bit(FLAG_IS_OUT, &desc->flags);
2583 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2586 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2588 * @desc: GPIO to enable.
2589 * @flags: Flags related to GPIO edge.
2591 * Return 0 in case of success, else negative error code.
2593 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2596 struct gpio_chip *gc;
2598 VALIDATE_DESC(desc);
2600 gc = desc->gdev->chip;
2601 if (!gc->en_hw_timestamp) {
2602 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2606 ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2608 gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
2612 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
2615 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2617 * @desc: GPIO to disable.
2618 * @flags: Flags related to GPIO edge, same value as used during enable call.
2620 * Return 0 in case of success, else negative error code.
2622 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2625 struct gpio_chip *gc;
2627 VALIDATE_DESC(desc);
2629 gc = desc->gdev->chip;
2630 if (!gc->dis_hw_timestamp) {
2631 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2635 ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2637 gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
2641 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
2644 * gpiod_set_config - sets @config for a GPIO
2645 * @desc: descriptor of the GPIO for which to set the configuration
2646 * @config: Same packed config format as generic pinconf
2649 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2652 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2654 struct gpio_chip *gc;
2656 VALIDATE_DESC(desc);
2657 gc = desc->gdev->chip;
2659 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2661 EXPORT_SYMBOL_GPL(gpiod_set_config);
2664 * gpiod_set_debounce - sets @debounce time for a GPIO
2665 * @desc: descriptor of the GPIO for which to set debounce time
2666 * @debounce: debounce time in microseconds
2669 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2672 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2674 unsigned long config;
2676 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2677 return gpiod_set_config(desc, config);
2679 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2682 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2683 * @desc: descriptor of the GPIO for which to configure persistence
2684 * @transitory: True to lose state on suspend or reset, false for persistence
2687 * 0 on success, otherwise a negative error code.
2689 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2691 VALIDATE_DESC(desc);
2693 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2694 * persistence state.
2696 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2698 /* If the driver supports it, set the persistence state now */
2699 return gpio_set_config_with_argument_optional(desc,
2700 PIN_CONFIG_PERSIST_STATE,
2703 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2706 * gpiod_is_active_low - test whether a GPIO is active-low or not
2707 * @desc: the gpio descriptor to test
2709 * Returns 1 if the GPIO is active-low, 0 otherwise.
2711 int gpiod_is_active_low(const struct gpio_desc *desc)
2713 VALIDATE_DESC(desc);
2714 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2716 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2719 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2720 * @desc: the gpio descriptor to change
2722 void gpiod_toggle_active_low(struct gpio_desc *desc)
2724 VALIDATE_DESC_VOID(desc);
2725 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2727 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2729 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
2731 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
2734 /* I/O calls are only valid after configuration completed; the relevant
2735 * "is this a valid GPIO" error checks should already have been done.
2737 * "Get" operations are often inlinable as reading a pin value register,
2738 * and masking the relevant bit in that register.
2740 * When "set" operations are inlinable, they involve writing that mask to
2741 * one register to set a low value, or a different register to set it high.
2742 * Otherwise locking is needed, so there may be little value to inlining.
2744 *------------------------------------------------------------------------
2746 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2747 * have requested the GPIO. That can include implicit requesting by
2748 * a direction setting call. Marking a gpio as requested locks its chip
2749 * in memory, guaranteeing that these table lookups need no more locking
2750 * and that gpiochip_remove() will fail.
2752 * REVISIT when debugging, consider adding some instrumentation to ensure
2753 * that the GPIO was actually requested.
2756 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2758 struct gpio_chip *gc;
2761 gc = desc->gdev->chip;
2762 value = gpio_chip_get_value(gc, desc);
2763 value = value < 0 ? value : !!value;
2764 trace_gpio_value(desc_to_gpio(desc), 1, value);
2768 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2769 unsigned long *mask, unsigned long *bits)
2771 if (gc->get_multiple)
2772 return gc->get_multiple(gc, mask, bits);
2776 for_each_set_bit(i, mask, gc->ngpio) {
2777 value = gc->get(gc, i);
2780 __assign_bit(i, bits, value);
2787 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2788 unsigned int array_size,
2789 struct gpio_desc **desc_array,
2790 struct gpio_array *array_info,
2791 unsigned long *value_bitmap)
2796 * Validate array_info against desc_array and its size.
2797 * It should immediately follow desc_array if both
2798 * have been obtained from the same gpiod_get_array() call.
2800 if (array_info && array_info->desc == desc_array &&
2801 array_size <= array_info->size &&
2802 (void *)array_info == desc_array + array_info->size) {
2804 WARN_ON(array_info->chip->can_sleep);
2806 ret = gpio_chip_get_multiple(array_info->chip,
2807 array_info->get_mask,
2812 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2813 bitmap_xor(value_bitmap, value_bitmap,
2814 array_info->invert_mask, array_size);
2816 i = find_first_zero_bit(array_info->get_mask, array_size);
2817 if (i == array_size)
2823 while (i < array_size) {
2824 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2825 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
2826 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
2827 unsigned long *mask, *bits;
2830 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2831 mask = fastpath_mask;
2832 bits = fastpath_bits;
2834 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
2836 mask = bitmap_alloc(gc->ngpio, flags);
2840 bits = bitmap_alloc(gc->ngpio, flags);
2847 bitmap_zero(mask, gc->ngpio);
2850 WARN_ON(gc->can_sleep);
2852 /* collect all inputs belonging to the same chip */
2855 const struct gpio_desc *desc = desc_array[i];
2856 int hwgpio = gpio_chip_hwgpio(desc);
2858 __set_bit(hwgpio, mask);
2862 i = find_next_zero_bit(array_info->get_mask,
2864 } while ((i < array_size) &&
2865 (desc_array[i]->gdev->chip == gc));
2867 ret = gpio_chip_get_multiple(gc, mask, bits);
2869 if (mask != fastpath_mask)
2871 if (bits != fastpath_bits)
2876 for (j = first; j < i; ) {
2877 const struct gpio_desc *desc = desc_array[j];
2878 int hwgpio = gpio_chip_hwgpio(desc);
2879 int value = test_bit(hwgpio, bits);
2881 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2883 __assign_bit(j, value_bitmap, value);
2884 trace_gpio_value(desc_to_gpio(desc), 1, value);
2888 j = find_next_zero_bit(array_info->get_mask, i,
2892 if (mask != fastpath_mask)
2894 if (bits != fastpath_bits)
2901 * gpiod_get_raw_value() - return a gpio's raw value
2902 * @desc: gpio whose value will be returned
2904 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2905 * its ACTIVE_LOW status, or negative errno on failure.
2907 * This function can be called from contexts where we cannot sleep, and will
2908 * complain if the GPIO chip functions potentially sleep.
2910 int gpiod_get_raw_value(const struct gpio_desc *desc)
2912 VALIDATE_DESC(desc);
2913 /* Should be using gpiod_get_raw_value_cansleep() */
2914 WARN_ON(desc->gdev->chip->can_sleep);
2915 return gpiod_get_raw_value_commit(desc);
2917 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2920 * gpiod_get_value() - return a gpio's value
2921 * @desc: gpio whose value will be returned
2923 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2924 * account, or negative errno on failure.
2926 * This function can be called from contexts where we cannot sleep, and will
2927 * complain if the GPIO chip functions potentially sleep.
2929 int gpiod_get_value(const struct gpio_desc *desc)
2933 VALIDATE_DESC(desc);
2934 /* Should be using gpiod_get_value_cansleep() */
2935 WARN_ON(desc->gdev->chip->can_sleep);
2937 value = gpiod_get_raw_value_commit(desc);
2941 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2946 EXPORT_SYMBOL_GPL(gpiod_get_value);
2949 * gpiod_get_raw_array_value() - read raw 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 raw values of the GPIOs, i.e. the values of the physical lines
2956 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2957 * else an error code.
2959 * This function can be called from contexts where we cannot sleep,
2960 * and it will complain if the GPIO chip functions potentially sleep.
2962 int gpiod_get_raw_array_value(unsigned int array_size,
2963 struct gpio_desc **desc_array,
2964 struct gpio_array *array_info,
2965 unsigned long *value_bitmap)
2969 return gpiod_get_array_value_complex(true, false, array_size,
2970 desc_array, array_info,
2973 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2976 * gpiod_get_array_value() - read values from an array of GPIOs
2977 * @array_size: number of elements in the descriptor array / value bitmap
2978 * @desc_array: array of GPIO descriptors whose values will be read
2979 * @array_info: information on applicability of fast bitmap processing path
2980 * @value_bitmap: bitmap to store the read values
2982 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2983 * into account. Return 0 in case of success, else an error code.
2985 * This function can be called from contexts where we cannot sleep,
2986 * and it will complain if the GPIO chip functions potentially sleep.
2988 int gpiod_get_array_value(unsigned int array_size,
2989 struct gpio_desc **desc_array,
2990 struct gpio_array *array_info,
2991 unsigned long *value_bitmap)
2995 return gpiod_get_array_value_complex(false, false, array_size,
2996 desc_array, array_info,
2999 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3002 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3003 * @desc: gpio descriptor whose state need to be set.
3004 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3006 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
3009 struct gpio_chip *gc = desc->gdev->chip;
3010 int offset = gpio_chip_hwgpio(desc);
3013 ret = gc->direction_input(gc, offset);
3015 ret = gc->direction_output(gc, offset, 0);
3017 set_bit(FLAG_IS_OUT, &desc->flags);
3019 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3022 "%s: Error in set_value for open drain err %d\n",
3027 * _gpio_set_open_source_value() - Set the open source gpio's value.
3028 * @desc: gpio descriptor whose state need to be set.
3029 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3031 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3034 struct gpio_chip *gc = desc->gdev->chip;
3035 int offset = gpio_chip_hwgpio(desc);
3038 ret = gc->direction_output(gc, offset, 1);
3040 set_bit(FLAG_IS_OUT, &desc->flags);
3042 ret = gc->direction_input(gc, offset);
3044 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3047 "%s: Error in set_value for open source err %d\n",
3051 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3053 struct gpio_chip *gc;
3055 gc = desc->gdev->chip;
3056 trace_gpio_value(desc_to_gpio(desc), 0, value);
3057 gc->set(gc, gpio_chip_hwgpio(desc), value);
3061 * set multiple outputs on the same chip;
3062 * use the chip's set_multiple function if available;
3063 * otherwise set the outputs sequentially;
3064 * @chip: the GPIO chip we operate on
3065 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3066 * defines which outputs are to be changed
3067 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3068 * defines the values the outputs specified by mask are to be set to
3070 static void gpio_chip_set_multiple(struct gpio_chip *gc,
3071 unsigned long *mask, unsigned long *bits)
3073 if (gc->set_multiple) {
3074 gc->set_multiple(gc, mask, bits);
3078 /* set outputs if the corresponding mask bit is set */
3079 for_each_set_bit(i, mask, gc->ngpio)
3080 gc->set(gc, i, test_bit(i, bits));
3084 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3085 unsigned int array_size,
3086 struct gpio_desc **desc_array,
3087 struct gpio_array *array_info,
3088 unsigned long *value_bitmap)
3093 * Validate array_info against desc_array and its size.
3094 * It should immediately follow desc_array if both
3095 * have been obtained from the same gpiod_get_array() call.
3097 if (array_info && array_info->desc == desc_array &&
3098 array_size <= array_info->size &&
3099 (void *)array_info == desc_array + array_info->size) {
3101 WARN_ON(array_info->chip->can_sleep);
3103 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3104 bitmap_xor(value_bitmap, value_bitmap,
3105 array_info->invert_mask, array_size);
3107 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3110 i = find_first_zero_bit(array_info->set_mask, array_size);
3111 if (i == array_size)
3117 while (i < array_size) {
3118 struct gpio_chip *gc = desc_array[i]->gdev->chip;
3119 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3120 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3121 unsigned long *mask, *bits;
3124 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
3125 mask = fastpath_mask;
3126 bits = fastpath_bits;
3128 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3130 mask = bitmap_alloc(gc->ngpio, flags);
3134 bits = bitmap_alloc(gc->ngpio, flags);
3141 bitmap_zero(mask, gc->ngpio);
3144 WARN_ON(gc->can_sleep);
3147 struct gpio_desc *desc = desc_array[i];
3148 int hwgpio = gpio_chip_hwgpio(desc);
3149 int value = test_bit(i, value_bitmap);
3152 * Pins applicable for fast input but not for
3153 * fast output processing may have been already
3154 * inverted inside the fast path, skip them.
3156 if (!raw && !(array_info &&
3157 test_bit(i, array_info->invert_mask)) &&
3158 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3160 trace_gpio_value(desc_to_gpio(desc), 0, value);
3162 * collect all normal outputs belonging to the same chip
3163 * open drain and open source outputs are set individually
3165 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3166 gpio_set_open_drain_value_commit(desc, value);
3167 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3168 gpio_set_open_source_value_commit(desc, value);
3170 __set_bit(hwgpio, mask);
3171 __assign_bit(hwgpio, bits, value);
3177 i = find_next_zero_bit(array_info->set_mask,
3179 } while ((i < array_size) &&
3180 (desc_array[i]->gdev->chip == gc));
3181 /* push collected bits to outputs */
3183 gpio_chip_set_multiple(gc, mask, bits);
3185 if (mask != fastpath_mask)
3187 if (bits != fastpath_bits)
3194 * gpiod_set_raw_value() - assign a gpio's raw value
3195 * @desc: gpio whose value will be assigned
3196 * @value: value to assign
3198 * Set the raw value of the GPIO, i.e. the value of its physical line without
3199 * regard for its ACTIVE_LOW status.
3201 * This function can be called from contexts where we cannot sleep, and will
3202 * complain if the GPIO chip functions potentially sleep.
3204 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3206 VALIDATE_DESC_VOID(desc);
3207 /* Should be using gpiod_set_raw_value_cansleep() */
3208 WARN_ON(desc->gdev->chip->can_sleep);
3209 gpiod_set_raw_value_commit(desc, value);
3211 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3214 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3215 * @desc: the descriptor to set the value on
3216 * @value: value to set
3218 * This sets the value of a GPIO line backing a descriptor, applying
3219 * different semantic quirks like active low and open drain/source
3222 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3224 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3226 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3227 gpio_set_open_drain_value_commit(desc, value);
3228 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3229 gpio_set_open_source_value_commit(desc, value);
3231 gpiod_set_raw_value_commit(desc, value);
3235 * gpiod_set_value() - assign a gpio's value
3236 * @desc: gpio whose value will be assigned
3237 * @value: value to assign
3239 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3240 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3242 * This function can be called from contexts where we cannot sleep, and will
3243 * complain if the GPIO chip functions potentially sleep.
3245 void gpiod_set_value(struct gpio_desc *desc, int value)
3247 VALIDATE_DESC_VOID(desc);
3248 /* Should be using gpiod_set_value_cansleep() */
3249 WARN_ON(desc->gdev->chip->can_sleep);
3250 gpiod_set_value_nocheck(desc, value);
3252 EXPORT_SYMBOL_GPL(gpiod_set_value);
3255 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3256 * @array_size: number of elements in the descriptor array / value bitmap
3257 * @desc_array: array of GPIO descriptors whose values will be assigned
3258 * @array_info: information on applicability of fast bitmap processing path
3259 * @value_bitmap: bitmap of values to assign
3261 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3262 * without regard for their ACTIVE_LOW status.
3264 * This function can be called from contexts where we cannot sleep, and will
3265 * complain if the GPIO chip functions potentially sleep.
3267 int gpiod_set_raw_array_value(unsigned int array_size,
3268 struct gpio_desc **desc_array,
3269 struct gpio_array *array_info,
3270 unsigned long *value_bitmap)
3274 return gpiod_set_array_value_complex(true, false, array_size,
3275 desc_array, array_info, value_bitmap);
3277 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3280 * gpiod_set_array_value() - assign values to an array of GPIOs
3281 * @array_size: number of elements in the descriptor array / value bitmap
3282 * @desc_array: array of GPIO descriptors whose values will be assigned
3283 * @array_info: information on applicability of fast bitmap processing path
3284 * @value_bitmap: bitmap of values to assign
3286 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3289 * This function can be called from contexts where we cannot sleep, and will
3290 * complain if the GPIO chip functions potentially sleep.
3292 int gpiod_set_array_value(unsigned int array_size,
3293 struct gpio_desc **desc_array,
3294 struct gpio_array *array_info,
3295 unsigned long *value_bitmap)
3299 return gpiod_set_array_value_complex(false, false, array_size,
3300 desc_array, array_info,
3303 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3306 * gpiod_cansleep() - report whether gpio value access may sleep
3307 * @desc: gpio to check
3310 int gpiod_cansleep(const struct gpio_desc *desc)
3312 VALIDATE_DESC(desc);
3313 return desc->gdev->chip->can_sleep;
3315 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3318 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3319 * @desc: gpio to set the consumer name on
3320 * @name: the new consumer name
3322 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3324 VALIDATE_DESC(desc);
3326 name = kstrdup_const(name, GFP_KERNEL);
3331 kfree_const(desc->label);
3332 desc_set_label(desc, name);
3336 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3339 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3340 * @desc: gpio whose IRQ will be returned (already requested)
3342 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3345 int gpiod_to_irq(const struct gpio_desc *desc)
3347 struct gpio_chip *gc;
3351 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3352 * requires this function to not return zero on an invalid descriptor
3353 * but rather a negative error number.
3355 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3358 gc = desc->gdev->chip;
3359 offset = gpio_chip_hwgpio(desc);
3361 int retirq = gc->to_irq(gc, offset);
3363 /* Zero means NO_IRQ */
3369 #ifdef CONFIG_GPIOLIB_IRQCHIP
3372 * Avoid race condition with other code, which tries to lookup
3373 * an IRQ before the irqchip has been properly registered,
3374 * i.e. while gpiochip is still being brought up.
3376 return -EPROBE_DEFER;
3381 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3384 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3385 * @gc: the chip the GPIO to lock belongs to
3386 * @offset: the offset of the GPIO to lock as IRQ
3388 * This is used directly by GPIO drivers that want to lock down
3389 * a certain GPIO line to be used for IRQs.
3391 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3393 struct gpio_desc *desc;
3395 desc = gpiochip_get_desc(gc, offset);
3397 return PTR_ERR(desc);
3400 * If it's fast: flush the direction setting if something changed
3403 if (!gc->can_sleep && gc->get_direction) {
3404 int dir = gpiod_get_direction(desc);
3407 chip_err(gc, "%s: cannot get GPIO direction\n",
3413 /* To be valid for IRQ the line needs to be input or open drain */
3414 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3415 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3417 "%s: tried to flag a GPIO set as output for IRQ\n",
3422 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3423 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3426 * If the consumer has not set up a label (such as when the
3427 * IRQ is referenced from .to_irq()) we set up a label here
3428 * so it is clear this is used as an interrupt.
3431 desc_set_label(desc, "interrupt");
3435 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3438 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3439 * @gc: the chip the GPIO to lock belongs to
3440 * @offset: the offset of the GPIO to lock as IRQ
3442 * This is used directly by GPIO drivers that want to indicate
3443 * that a certain GPIO is no longer used exclusively for IRQ.
3445 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3447 struct gpio_desc *desc;
3449 desc = gpiochip_get_desc(gc, offset);
3453 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3454 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3456 /* If we only had this marking, erase it */
3457 if (desc->label && !strcmp(desc->label, "interrupt"))
3458 desc_set_label(desc, NULL);
3460 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3462 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3464 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3466 if (!IS_ERR(desc) &&
3467 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3468 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3470 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3472 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3474 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3476 if (!IS_ERR(desc) &&
3477 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3479 * We must not be output when using IRQ UNLESS we are
3482 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3483 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3484 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3487 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3489 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3491 if (offset >= gc->ngpio)
3494 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3496 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3498 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3502 if (!try_module_get(gc->gpiodev->owner))
3505 ret = gpiochip_lock_as_irq(gc, offset);
3507 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3508 module_put(gc->gpiodev->owner);
3513 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3515 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3517 gpiochip_unlock_as_irq(gc, offset);
3518 module_put(gc->gpiodev->owner);
3520 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3522 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3524 if (offset >= gc->ngpio)
3527 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3529 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3531 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3533 if (offset >= gc->ngpio)
3536 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3538 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3540 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3542 if (offset >= gc->ngpio)
3545 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3547 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3550 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3551 * @desc: gpio whose value will be returned
3553 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3554 * its ACTIVE_LOW status, or negative errno on failure.
3556 * This function is to be called from contexts that can sleep.
3558 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3560 might_sleep_if(extra_checks);
3561 VALIDATE_DESC(desc);
3562 return gpiod_get_raw_value_commit(desc);
3564 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3567 * gpiod_get_value_cansleep() - return a gpio's value
3568 * @desc: gpio whose value will be returned
3570 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3571 * account, or negative errno on failure.
3573 * This function is to be called from contexts that can sleep.
3575 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3579 might_sleep_if(extra_checks);
3580 VALIDATE_DESC(desc);
3581 value = gpiod_get_raw_value_commit(desc);
3585 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3590 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3593 * gpiod_get_raw_array_value_cansleep() - read raw 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 raw values of the GPIOs, i.e. the values of the physical lines
3600 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3601 * else an error code.
3603 * This function is to be called from contexts that can sleep.
3605 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3606 struct gpio_desc **desc_array,
3607 struct gpio_array *array_info,
3608 unsigned long *value_bitmap)
3610 might_sleep_if(extra_checks);
3613 return gpiod_get_array_value_complex(true, true, array_size,
3614 desc_array, array_info,
3617 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3620 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3621 * @array_size: number of elements in the descriptor array / value bitmap
3622 * @desc_array: array of GPIO descriptors whose values will be read
3623 * @array_info: information on applicability of fast bitmap processing path
3624 * @value_bitmap: bitmap to store the read values
3626 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3627 * into account. Return 0 in case of success, else an error code.
3629 * This function is to be called from contexts that can sleep.
3631 int gpiod_get_array_value_cansleep(unsigned int array_size,
3632 struct gpio_desc **desc_array,
3633 struct gpio_array *array_info,
3634 unsigned long *value_bitmap)
3636 might_sleep_if(extra_checks);
3639 return gpiod_get_array_value_complex(false, true, array_size,
3640 desc_array, array_info,
3643 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3646 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3647 * @desc: gpio whose value will be assigned
3648 * @value: value to assign
3650 * Set the raw value of the GPIO, i.e. the value of its physical line without
3651 * regard for its ACTIVE_LOW status.
3653 * This function is to be called from contexts that can sleep.
3655 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3657 might_sleep_if(extra_checks);
3658 VALIDATE_DESC_VOID(desc);
3659 gpiod_set_raw_value_commit(desc, value);
3661 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3664 * gpiod_set_value_cansleep() - assign a gpio's value
3665 * @desc: gpio whose value will be assigned
3666 * @value: value to assign
3668 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3671 * This function is to be called from contexts that can sleep.
3673 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3675 might_sleep_if(extra_checks);
3676 VALIDATE_DESC_VOID(desc);
3677 gpiod_set_value_nocheck(desc, value);
3679 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3682 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3683 * @array_size: number of elements in the descriptor array / value bitmap
3684 * @desc_array: array of GPIO descriptors whose values will be assigned
3685 * @array_info: information on applicability of fast bitmap processing path
3686 * @value_bitmap: bitmap of values to assign
3688 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3689 * without regard for their ACTIVE_LOW status.
3691 * This function is to be called from contexts that can sleep.
3693 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3694 struct gpio_desc **desc_array,
3695 struct gpio_array *array_info,
3696 unsigned long *value_bitmap)
3698 might_sleep_if(extra_checks);
3701 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3702 array_info, value_bitmap);
3704 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3707 * gpiod_add_lookup_tables() - register GPIO device consumers
3708 * @tables: list of tables of consumers to register
3709 * @n: number of tables in the list
3711 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3715 mutex_lock(&gpio_lookup_lock);
3717 for (i = 0; i < n; i++)
3718 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3720 mutex_unlock(&gpio_lookup_lock);
3724 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3725 * @array_size: number of elements in the descriptor array / value bitmap
3726 * @desc_array: array of GPIO descriptors whose values will be assigned
3727 * @array_info: information on applicability of fast bitmap processing path
3728 * @value_bitmap: bitmap of values to assign
3730 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3733 * This function is to be called from contexts that can sleep.
3735 int gpiod_set_array_value_cansleep(unsigned int array_size,
3736 struct gpio_desc **desc_array,
3737 struct gpio_array *array_info,
3738 unsigned long *value_bitmap)
3740 might_sleep_if(extra_checks);
3743 return gpiod_set_array_value_complex(false, true, array_size,
3744 desc_array, array_info,
3747 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3749 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action)
3751 blocking_notifier_call_chain(&desc->gdev->line_state_notifier,
3756 * gpiod_add_lookup_table() - register GPIO device consumers
3757 * @table: table of consumers to register
3759 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3761 gpiod_add_lookup_tables(&table, 1);
3763 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3766 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3767 * @table: table of consumers to unregister
3769 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3771 /* Nothing to remove */
3775 mutex_lock(&gpio_lookup_lock);
3777 list_del(&table->list);
3779 mutex_unlock(&gpio_lookup_lock);
3781 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3784 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3785 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3787 void gpiod_add_hogs(struct gpiod_hog *hogs)
3789 struct gpio_chip *gc;
3790 struct gpiod_hog *hog;
3792 mutex_lock(&gpio_machine_hogs_mutex);
3794 for (hog = &hogs[0]; hog->chip_label; hog++) {
3795 list_add_tail(&hog->list, &gpio_machine_hogs);
3798 * The chip may have been registered earlier, so check if it
3799 * exists and, if so, try to hog the line now.
3801 gc = find_chip_by_name(hog->chip_label);
3803 gpiochip_machine_hog(gc, hog);
3806 mutex_unlock(&gpio_machine_hogs_mutex);
3808 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3810 void gpiod_remove_hogs(struct gpiod_hog *hogs)
3812 struct gpiod_hog *hog;
3814 mutex_lock(&gpio_machine_hogs_mutex);
3815 for (hog = &hogs[0]; hog->chip_label; hog++)
3816 list_del(&hog->list);
3817 mutex_unlock(&gpio_machine_hogs_mutex);
3819 EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
3821 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3823 const char *dev_id = dev ? dev_name(dev) : NULL;
3824 struct gpiod_lookup_table *table;
3826 mutex_lock(&gpio_lookup_lock);
3828 list_for_each_entry(table, &gpio_lookup_list, list) {
3829 if (table->dev_id && dev_id) {
3831 * Valid strings on both ends, must be identical to have
3834 if (!strcmp(table->dev_id, dev_id))
3838 * One of the pointers is NULL, so both must be to have
3841 if (dev_id == table->dev_id)
3848 mutex_unlock(&gpio_lookup_lock);
3852 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3853 unsigned int idx, unsigned long *flags)
3855 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3856 struct gpiod_lookup_table *table;
3857 struct gpiod_lookup *p;
3859 table = gpiod_find_lookup_table(dev);
3863 for (p = &table->table[0]; p->key; p++) {
3864 struct gpio_chip *gc;
3866 /* idx must always match exactly */
3870 /* If the lookup entry has a con_id, require exact match */
3871 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3874 if (p->chip_hwnum == U16_MAX) {
3875 desc = gpio_name_to_desc(p->key);
3881 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3883 return ERR_PTR(-EPROBE_DEFER);
3886 gc = find_chip_by_name(p->key);
3890 * As the lookup table indicates a chip with
3891 * p->key should exist, assume it may
3892 * still appear later and let the interested
3893 * consumer be probed again or let the Deferred
3894 * Probe infrastructure handle the error.
3896 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3898 return ERR_PTR(-EPROBE_DEFER);
3901 if (gc->ngpio <= p->chip_hwnum) {
3903 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3904 idx, p->chip_hwnum, gc->ngpio - 1,
3906 return ERR_PTR(-EINVAL);
3909 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3918 static int platform_gpio_count(struct device *dev, const char *con_id)
3920 struct gpiod_lookup_table *table;
3921 struct gpiod_lookup *p;
3922 unsigned int count = 0;
3924 table = gpiod_find_lookup_table(dev);
3928 for (p = &table->table[0]; p->key; p++) {
3929 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3930 (!con_id && !p->con_id))
3939 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
3940 struct device *consumer,
3943 enum gpiod_flags *flags,
3944 unsigned long *lookupflags)
3946 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3948 if (is_of_node(fwnode)) {
3949 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n",
3951 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags);
3952 } else if (is_acpi_node(fwnode)) {
3953 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n",
3955 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
3956 } else if (is_software_node(fwnode)) {
3957 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n",
3959 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
3965 static struct gpio_desc *gpiod_find_and_request(struct device *consumer,
3966 struct fwnode_handle *fwnode,
3969 enum gpiod_flags flags,
3971 bool platform_lookup_allowed)
3973 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3974 struct gpio_desc *desc;
3977 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, &flags, &lookupflags);
3978 if (gpiod_not_found(desc) && platform_lookup_allowed) {
3980 * Either we are not using DT or ACPI, or their lookup did not
3981 * return a result. In that case, use platform lookup as a
3984 dev_dbg(consumer, "using lookup tables for GPIO lookup\n");
3985 desc = gpiod_find(consumer, con_id, idx, &lookupflags);
3989 dev_dbg(consumer, "No GPIO consumer %s found\n", con_id);
3994 * If a connection label was passed use that, else attempt to use
3995 * the device name as label
3997 ret = gpiod_request(desc, label);
3999 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
4000 return ERR_PTR(ret);
4003 * This happens when there are several consumers for
4004 * the same GPIO line: we just return here without
4005 * further initialization. It is a bit of a hack.
4006 * This is necessary to support fixed regulators.
4008 * FIXME: Make this more sane and safe.
4011 "nonexclusive access to GPIO for %s\n", con_id);
4015 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4017 dev_dbg(consumer, "setup of GPIO %s failed\n", con_id);
4019 return ERR_PTR(ret);
4022 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_REQUESTED);
4028 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4029 * @fwnode: handle of the firmware node
4030 * @con_id: function within the GPIO consumer
4031 * @index: index of the GPIO to obtain for the consumer
4032 * @flags: GPIO initialization flags
4033 * @label: label to attach to the requested GPIO
4035 * This function can be used for drivers that get their configuration
4036 * from opaque firmware.
4038 * The function properly finds the corresponding GPIO using whatever is the
4039 * underlying firmware interface and then makes sure that the GPIO
4040 * descriptor is requested before it is returned to the caller.
4043 * On successful request the GPIO pin is configured in accordance with
4046 * In case of error an ERR_PTR() is returned.
4048 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4051 enum gpiod_flags flags,
4054 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false);
4056 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4059 * gpiod_count - return the number of GPIOs associated with a device / function
4060 * or -ENOENT if no GPIO has been assigned to the requested function
4061 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4062 * @con_id: function within the GPIO consumer
4064 int gpiod_count(struct device *dev, const char *con_id)
4066 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4067 int count = -ENOENT;
4069 if (is_of_node(fwnode))
4070 count = of_gpio_get_count(dev, con_id);
4071 else if (is_acpi_node(fwnode))
4072 count = acpi_gpio_count(dev, con_id);
4073 else if (is_software_node(fwnode))
4074 count = swnode_gpio_count(fwnode, con_id);
4077 count = platform_gpio_count(dev, con_id);
4081 EXPORT_SYMBOL_GPL(gpiod_count);
4084 * gpiod_get - obtain a GPIO for a given GPIO function
4085 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4086 * @con_id: function within the GPIO consumer
4087 * @flags: optional GPIO initialization flags
4089 * Return the GPIO descriptor corresponding to the function con_id of device
4090 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4091 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4093 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4094 enum gpiod_flags flags)
4096 return gpiod_get_index(dev, con_id, 0, flags);
4098 EXPORT_SYMBOL_GPL(gpiod_get);
4101 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4102 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4103 * @con_id: function within the GPIO consumer
4104 * @flags: optional GPIO initialization flags
4106 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4107 * the requested function it will return NULL. This is convenient for drivers
4108 * that need to handle optional GPIOs.
4110 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4112 enum gpiod_flags flags)
4114 return gpiod_get_index_optional(dev, con_id, 0, flags);
4116 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4120 * gpiod_configure_flags - helper function to configure a given GPIO
4121 * @desc: gpio whose value will be assigned
4122 * @con_id: function within the GPIO consumer
4123 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4124 * of_find_gpio() or of_get_gpio_hog()
4125 * @dflags: gpiod_flags - optional GPIO initialization flags
4127 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4128 * requested function and/or index, or another IS_ERR() code if an error
4129 * occurred while trying to acquire the GPIO.
4131 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4132 unsigned long lflags, enum gpiod_flags dflags)
4136 if (lflags & GPIO_ACTIVE_LOW)
4137 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4139 if (lflags & GPIO_OPEN_DRAIN)
4140 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4141 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4143 * This enforces open drain mode from the consumer side.
4144 * This is necessary for some busses like I2C, but the lookup
4145 * should *REALLY* have specified them as open drain in the
4146 * first place, so print a little warning here.
4148 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4150 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4153 if (lflags & GPIO_OPEN_SOURCE)
4154 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4156 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
4157 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
4158 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
4160 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4164 if (lflags & GPIO_PULL_UP)
4165 set_bit(FLAG_PULL_UP, &desc->flags);
4166 else if (lflags & GPIO_PULL_DOWN)
4167 set_bit(FLAG_PULL_DOWN, &desc->flags);
4168 else if (lflags & GPIO_PULL_DISABLE)
4169 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
4171 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4175 /* No particular flag request, return here... */
4176 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4177 gpiod_dbg(desc, "no flags found for %s\n", con_id);
4182 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4183 ret = gpiod_direction_output(desc,
4184 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4186 ret = gpiod_direction_input(desc);
4192 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4193 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4194 * @con_id: function within the GPIO consumer
4195 * @idx: index of the GPIO to obtain in the consumer
4196 * @flags: optional GPIO initialization flags
4198 * This variant of gpiod_get() allows to access GPIOs other than the first
4199 * defined one for functions that define several GPIOs.
4201 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4202 * requested function and/or index, or another IS_ERR() code if an error
4203 * occurred while trying to acquire the GPIO.
4205 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4208 enum gpiod_flags flags)
4210 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4211 const char *devname = dev ? dev_name(dev) : "?";
4212 const char *label = con_id ?: devname;
4214 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true);
4216 EXPORT_SYMBOL_GPL(gpiod_get_index);
4219 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4221 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4222 * @con_id: function within the GPIO consumer
4223 * @index: index of the GPIO to obtain in the consumer
4224 * @flags: optional GPIO initialization flags
4226 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4227 * specified index was assigned to the requested function it will return NULL.
4228 * This is convenient for drivers that need to handle optional GPIOs.
4230 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4233 enum gpiod_flags flags)
4235 struct gpio_desc *desc;
4237 desc = gpiod_get_index(dev, con_id, index, flags);
4238 if (gpiod_not_found(desc))
4243 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4246 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4247 * @desc: gpio whose value will be assigned
4248 * @name: gpio line name
4249 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4250 * of_find_gpio() or of_get_gpio_hog()
4251 * @dflags: gpiod_flags - optional GPIO initialization flags
4253 int gpiod_hog(struct gpio_desc *desc, const char *name,
4254 unsigned long lflags, enum gpiod_flags dflags)
4256 struct gpio_chip *gc;
4257 struct gpio_desc *local_desc;
4261 gc = gpiod_to_chip(desc);
4262 hwnum = gpio_chip_hwgpio(desc);
4264 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4266 if (IS_ERR(local_desc)) {
4267 ret = PTR_ERR(local_desc);
4268 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4269 name, gc->label, hwnum, ret);
4273 /* Mark GPIO as hogged so it can be identified and removed later */
4274 set_bit(FLAG_IS_HOGGED, &desc->flags);
4276 gpiod_dbg(desc, "hogged as %s%s\n",
4277 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4278 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4279 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4285 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4286 * @gc: gpio chip to act on
4288 static void gpiochip_free_hogs(struct gpio_chip *gc)
4290 struct gpio_desc *desc;
4292 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
4293 gpiochip_free_own_desc(desc);
4297 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4298 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4299 * @con_id: function within the GPIO consumer
4300 * @flags: optional GPIO initialization flags
4302 * This function acquires all the GPIOs defined under a given function.
4304 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4305 * no GPIO has been assigned to the requested function, or another IS_ERR()
4306 * code if an error occurred while trying to acquire the GPIOs.
4308 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4310 enum gpiod_flags flags)
4312 struct gpio_desc *desc;
4313 struct gpio_descs *descs;
4314 struct gpio_array *array_info = NULL;
4315 struct gpio_chip *gc;
4316 int count, bitmap_size;
4319 count = gpiod_count(dev, con_id);
4321 return ERR_PTR(count);
4323 descs_size = struct_size(descs, desc, count);
4324 descs = kzalloc(descs_size, GFP_KERNEL);
4326 return ERR_PTR(-ENOMEM);
4328 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) {
4329 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4331 gpiod_put_array(descs);
4332 return ERR_CAST(desc);
4335 descs->desc[descs->ndescs] = desc;
4337 gc = gpiod_to_chip(desc);
4339 * If pin hardware number of array member 0 is also 0, select
4340 * its chip as a candidate for fast bitmap processing path.
4342 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4343 struct gpio_descs *array;
4345 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4348 array = krealloc(descs, descs_size +
4349 struct_size(array_info, invert_mask, 3 * bitmap_size),
4350 GFP_KERNEL | __GFP_ZERO);
4352 gpiod_put_array(descs);
4353 return ERR_PTR(-ENOMEM);
4358 array_info = (void *)descs + descs_size;
4359 array_info->get_mask = array_info->invert_mask +
4361 array_info->set_mask = array_info->get_mask +
4364 array_info->desc = descs->desc;
4365 array_info->size = count;
4366 array_info->chip = gc;
4367 bitmap_set(array_info->get_mask, descs->ndescs,
4368 count - descs->ndescs);
4369 bitmap_set(array_info->set_mask, descs->ndescs,
4370 count - descs->ndescs);
4371 descs->info = array_info;
4374 /* If there is no cache for fast bitmap processing path, continue */
4378 /* Unmark array members which don't belong to the 'fast' chip */
4379 if (array_info->chip != gc) {
4380 __clear_bit(descs->ndescs, array_info->get_mask);
4381 __clear_bit(descs->ndescs, array_info->set_mask);
4384 * Detect array members which belong to the 'fast' chip
4385 * but their pins are not in hardware order.
4387 else if (gpio_chip_hwgpio(desc) != descs->ndescs) {
4389 * Don't use fast path if all array members processed so
4390 * far belong to the same chip as this one but its pin
4391 * hardware number is different from its array index.
4393 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4396 __clear_bit(descs->ndescs,
4397 array_info->get_mask);
4398 __clear_bit(descs->ndescs,
4399 array_info->set_mask);
4402 /* Exclude open drain or open source from fast output */
4403 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4404 gpiochip_line_is_open_source(gc, descs->ndescs))
4405 __clear_bit(descs->ndescs,
4406 array_info->set_mask);
4407 /* Identify 'fast' pins which require invertion */
4408 if (gpiod_is_active_low(desc))
4409 __set_bit(descs->ndescs,
4410 array_info->invert_mask);
4415 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4416 array_info->chip->label, array_info->size,
4417 *array_info->get_mask, *array_info->set_mask,
4418 *array_info->invert_mask);
4421 EXPORT_SYMBOL_GPL(gpiod_get_array);
4424 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4426 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4427 * @con_id: function within the GPIO consumer
4428 * @flags: optional GPIO initialization flags
4430 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4431 * assigned to the requested function it will return NULL.
4433 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4435 enum gpiod_flags flags)
4437 struct gpio_descs *descs;
4439 descs = gpiod_get_array(dev, con_id, flags);
4440 if (gpiod_not_found(descs))
4445 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4448 * gpiod_put - dispose of a GPIO descriptor
4449 * @desc: GPIO descriptor to dispose of
4451 * No descriptor can be used after gpiod_put() has been called on it.
4453 void gpiod_put(struct gpio_desc *desc)
4458 EXPORT_SYMBOL_GPL(gpiod_put);
4461 * gpiod_put_array - dispose of multiple GPIO descriptors
4462 * @descs: struct gpio_descs containing an array of descriptors
4464 void gpiod_put_array(struct gpio_descs *descs)
4468 for (i = 0; i < descs->ndescs; i++)
4469 gpiod_put(descs->desc[i]);
4473 EXPORT_SYMBOL_GPL(gpiod_put_array);
4475 static int gpio_stub_drv_probe(struct device *dev)
4478 * The DT node of some GPIO chips have a "compatible" property, but
4479 * never have a struct device added and probed by a driver to register
4480 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4481 * the consumers of the GPIO chip to get probe deferred forever because
4482 * they will be waiting for a device associated with the GPIO chip
4483 * firmware node to get added and bound to a driver.
4485 * To allow these consumers to probe, we associate the struct
4486 * gpio_device of the GPIO chip with the firmware node and then simply
4487 * bind it to this stub driver.
4492 static struct device_driver gpio_stub_drv = {
4493 .name = "gpio_stub_drv",
4494 .bus = &gpio_bus_type,
4495 .probe = gpio_stub_drv_probe,
4498 static int __init gpiolib_dev_init(void)
4502 /* Register GPIO sysfs bus */
4503 ret = bus_register(&gpio_bus_type);
4505 pr_err("gpiolib: could not register GPIO bus type\n");
4509 ret = driver_register(&gpio_stub_drv);
4511 pr_err("gpiolib: could not register GPIO stub driver\n");
4512 bus_unregister(&gpio_bus_type);
4516 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4518 pr_err("gpiolib: failed to allocate char dev region\n");
4519 driver_unregister(&gpio_stub_drv);
4520 bus_unregister(&gpio_bus_type);
4524 gpiolib_initialized = true;
4525 gpiochip_setup_devs();
4527 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4528 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4529 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4533 core_initcall(gpiolib_dev_init);
4535 #ifdef CONFIG_DEBUG_FS
4537 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4539 struct gpio_chip *gc = gdev->chip;
4540 struct gpio_desc *desc;
4541 unsigned gpio = gdev->base;
4547 for_each_gpio_desc(gc, desc) {
4548 if (test_bit(FLAG_REQUESTED, &desc->flags)) {
4549 gpiod_get_direction(desc);
4550 is_out = test_bit(FLAG_IS_OUT, &desc->flags);
4551 value = gpio_chip_get_value(gc, desc);
4552 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
4553 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4554 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
4555 gpio, desc->name ?: "", desc->label,
4556 is_out ? "out" : "in ",
4557 value >= 0 ? (value ? "hi" : "lo") : "? ",
4558 is_irq ? "IRQ " : "",
4559 active_low ? "ACTIVE LOW" : "");
4560 } else if (desc->name) {
4561 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
4568 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4570 unsigned long flags;
4571 struct gpio_device *gdev = NULL;
4572 loff_t index = *pos;
4576 spin_lock_irqsave(&gpio_lock, flags);
4577 list_for_each_entry(gdev, &gpio_devices, list)
4579 spin_unlock_irqrestore(&gpio_lock, flags);
4582 spin_unlock_irqrestore(&gpio_lock, flags);
4587 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4589 unsigned long flags;
4590 struct gpio_device *gdev = v;
4593 spin_lock_irqsave(&gpio_lock, flags);
4594 if (list_is_last(&gdev->list, &gpio_devices))
4597 ret = list_first_entry(&gdev->list, struct gpio_device, list);
4598 spin_unlock_irqrestore(&gpio_lock, flags);
4606 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4610 static int gpiolib_seq_show(struct seq_file *s, void *v)
4612 struct gpio_device *gdev = v;
4613 struct gpio_chip *gc = gdev->chip;
4614 struct device *parent;
4617 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4618 dev_name(&gdev->dev));
4622 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4623 dev_name(&gdev->dev),
4624 gdev->base, gdev->base + gdev->ngpio - 1);
4625 parent = gc->parent;
4627 seq_printf(s, ", parent: %s/%s",
4628 parent->bus ? parent->bus->name : "no-bus",
4631 seq_printf(s, ", %s", gc->label);
4633 seq_printf(s, ", can sleep");
4634 seq_printf(s, ":\n");
4637 gc->dbg_show(s, gc);
4639 gpiolib_dbg_show(s, gdev);
4644 static const struct seq_operations gpiolib_sops = {
4645 .start = gpiolib_seq_start,
4646 .next = gpiolib_seq_next,
4647 .stop = gpiolib_seq_stop,
4648 .show = gpiolib_seq_show,
4650 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4652 static int __init gpiolib_debugfs_init(void)
4654 /* /sys/kernel/debug/gpio */
4655 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4658 subsys_initcall(gpiolib_debugfs_init);
4660 #endif /* DEBUG_FS */