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 * FIXME: This will be removed soon.
1020 * This function is depracated, don't use.
1022 struct gpio_chip *gpiochip_find(void *data,
1023 int (*match)(struct gpio_chip *gc,
1026 struct gpio_device *gdev;
1027 struct gpio_chip *gc = NULL;
1029 gdev = gpio_device_find(data, match);
1032 gpio_device_put(gdev);
1037 EXPORT_SYMBOL_GPL(gpiochip_find);
1040 * gpio_device_find() - find a specific GPIO device
1041 * @data: data to pass to match function
1042 * @match: Callback function to check gpio_chip
1045 * New reference to struct gpio_device.
1047 * Similar to bus_find_device(). It returns a reference to a gpio_device as
1048 * determined by a user supplied @match callback. The callback should return
1049 * 0 if the device doesn't match and non-zero if it does. If the callback
1050 * returns non-zero, this function will return to the caller and not iterate
1051 * over any more gpio_devices.
1053 * The callback takes the GPIO chip structure as argument. During the execution
1054 * of the callback function the chip is protected from being freed. TODO: This
1055 * actually has yet to be implemented.
1057 * If the function returns non-NULL, the returned reference must be freed by
1058 * the caller using gpio_device_put().
1060 struct gpio_device *gpio_device_find(void *data,
1061 int (*match)(struct gpio_chip *gc,
1064 struct gpio_device *gdev;
1067 * Not yet but in the future the spinlock below will become a mutex.
1068 * Annotate this function before anyone tries to use it in interrupt
1069 * context like it happened with gpiochip_find().
1073 guard(spinlock_irqsave)(&gpio_lock);
1075 list_for_each_entry(gdev, &gpio_devices, list) {
1076 if (gdev->chip && match(gdev->chip, data))
1077 return gpio_device_get(gdev);
1082 EXPORT_SYMBOL_GPL(gpio_device_find);
1084 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
1086 const char *name = data;
1088 return !strcmp(gc->label, name);
1091 static struct gpio_chip *find_chip_by_name(const char *name)
1093 return gpiochip_find((void *)name, gpiochip_match_name);
1097 * gpio_device_get() - Increase the reference count of this GPIO device
1098 * @gdev: GPIO device to increase the refcount for
1103 struct gpio_device *gpio_device_get(struct gpio_device *gdev)
1105 return to_gpio_device(get_device(&gdev->dev));
1107 EXPORT_SYMBOL_GPL(gpio_device_get);
1110 * gpio_device_put() - Decrease the reference count of this GPIO device and
1111 * possibly free all resources associated with it.
1112 * @gdev: GPIO device to decrease the reference count for
1114 void gpio_device_put(struct gpio_device *gdev)
1116 put_device(&gdev->dev);
1118 EXPORT_SYMBOL_GPL(gpio_device_put);
1120 #ifdef CONFIG_GPIOLIB_IRQCHIP
1123 * The following is irqchip helper code for gpiochips.
1126 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1128 struct gpio_irq_chip *girq = &gc->irq;
1133 return girq->init_hw(gc);
1136 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1138 struct gpio_irq_chip *girq = &gc->irq;
1140 if (!girq->init_valid_mask)
1143 girq->valid_mask = gpiochip_allocate_mask(gc);
1144 if (!girq->valid_mask)
1147 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1152 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1154 gpiochip_free_mask(&gc->irq.valid_mask);
1157 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1158 unsigned int offset)
1160 if (!gpiochip_line_is_valid(gc, offset))
1162 /* No mask means all valid */
1163 if (likely(!gc->irq.valid_mask))
1165 return test_bit(offset, gc->irq.valid_mask);
1167 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1169 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1172 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1174 * @gc: the gpiochip to set the irqchip hierarchical handler to
1175 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1176 * will then percolate up to the parent
1178 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1179 struct irq_chip *irqchip)
1181 /* DT will deal with mapping each IRQ as we go along */
1182 if (is_of_node(gc->irq.fwnode))
1186 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1187 * irqs upfront instead of dynamically since we don't have the
1188 * dynamic type of allocation that hardware description languages
1189 * provide. Once all GPIO drivers using board files are gone from
1190 * the kernel we can delete this code, but for a transitional period
1191 * it is necessary to keep this around.
1193 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1197 for (i = 0; i < gc->ngpio; i++) {
1198 struct irq_fwspec fwspec;
1199 unsigned int parent_hwirq;
1200 unsigned int parent_type;
1201 struct gpio_irq_chip *girq = &gc->irq;
1204 * We call the child to parent translation function
1205 * only to check if the child IRQ is valid or not.
1206 * Just pick the rising edge type here as that is what
1207 * we likely need to support.
1209 ret = girq->child_to_parent_hwirq(gc, i,
1210 IRQ_TYPE_EDGE_RISING,
1214 chip_err(gc, "skip set-up on hwirq %d\n",
1219 fwspec.fwnode = gc->irq.fwnode;
1220 /* This is the hwirq for the GPIO line side of things */
1221 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1222 /* Just pick something */
1223 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1224 fwspec.param_count = 2;
1225 ret = irq_domain_alloc_irqs(gc->irq.domain, 1,
1226 NUMA_NO_NODE, &fwspec);
1229 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1236 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1241 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1242 struct irq_fwspec *fwspec,
1243 unsigned long *hwirq,
1246 /* We support standard DT translation */
1247 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1248 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1251 /* This is for board files and others not using DT */
1252 if (is_fwnode_irqchip(fwspec->fwnode)) {
1255 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1258 WARN_ON(*type == IRQ_TYPE_NONE);
1264 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1266 unsigned int nr_irqs,
1269 struct gpio_chip *gc = d->host_data;
1270 irq_hw_number_t hwirq;
1271 unsigned int type = IRQ_TYPE_NONE;
1272 struct irq_fwspec *fwspec = data;
1273 union gpio_irq_fwspec gpio_parent_fwspec = {};
1274 unsigned int parent_hwirq;
1275 unsigned int parent_type;
1276 struct gpio_irq_chip *girq = &gc->irq;
1280 * The nr_irqs parameter is always one except for PCI multi-MSI
1281 * so this should not happen.
1283 WARN_ON(nr_irqs != 1);
1285 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1289 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1291 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1292 &parent_hwirq, &parent_type);
1294 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1297 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1300 * We set handle_bad_irq because the .set_type() should
1301 * always be invoked and set the right type of handler.
1303 irq_domain_set_info(d,
1312 /* This parent only handles asserted level IRQs */
1313 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
1314 parent_hwirq, parent_type);
1318 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1320 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1321 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
1323 * If the parent irqdomain is msi, the interrupts have already
1324 * been allocated, so the EEXIST is good.
1326 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1330 "failed to allocate parent hwirq %d for hwirq %lu\n",
1331 parent_hwirq, hwirq);
1336 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1337 unsigned int offset)
1342 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1344 ops->activate = gpiochip_irq_domain_activate;
1345 ops->deactivate = gpiochip_irq_domain_deactivate;
1346 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1349 * We only allow overriding the translate() and free() functions for
1350 * hierarchical chips, and this should only be done if the user
1351 * really need something other than 1:1 translation for translate()
1352 * callback and free if user wants to free up any resources which
1353 * were allocated during callbacks, for example populate_parent_alloc_arg.
1355 if (!ops->translate)
1356 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1358 ops->free = irq_domain_free_irqs_common;
1361 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1363 struct irq_domain *domain;
1365 if (!gc->irq.child_to_parent_hwirq ||
1367 chip_err(gc, "missing irqdomain vital data\n");
1368 return ERR_PTR(-EINVAL);
1371 if (!gc->irq.child_offset_to_irq)
1372 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1374 if (!gc->irq.populate_parent_alloc_arg)
1375 gc->irq.populate_parent_alloc_arg =
1376 gpiochip_populate_parent_fwspec_twocell;
1378 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1380 domain = irq_domain_create_hierarchy(
1381 gc->irq.parent_domain,
1385 &gc->irq.child_irq_domain_ops,
1389 return ERR_PTR(-ENOMEM);
1391 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1396 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1398 return !!gc->irq.parent_domain;
1401 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1402 union gpio_irq_fwspec *gfwspec,
1403 unsigned int parent_hwirq,
1404 unsigned int parent_type)
1406 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1408 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1409 fwspec->param_count = 2;
1410 fwspec->param[0] = parent_hwirq;
1411 fwspec->param[1] = parent_type;
1415 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1417 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1418 union gpio_irq_fwspec *gfwspec,
1419 unsigned int parent_hwirq,
1420 unsigned int parent_type)
1422 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1424 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1425 fwspec->param_count = 4;
1426 fwspec->param[0] = 0;
1427 fwspec->param[1] = parent_hwirq;
1428 fwspec->param[2] = 0;
1429 fwspec->param[3] = parent_type;
1433 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1437 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1439 return ERR_PTR(-EINVAL);
1442 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1447 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1450 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1451 * @d: the irqdomain used by this irqchip
1452 * @irq: the global irq number used by this GPIO irqchip irq
1453 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1455 * This function will set up the mapping for a certain IRQ line on a
1456 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1457 * stored inside the gpiochip.
1459 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hwirq)
1461 struct gpio_chip *gc = d->host_data;
1464 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1467 irq_set_chip_data(irq, gc);
1469 * This lock class tells lockdep that GPIO irqs are in a different
1470 * category than their parents, so it won't report false recursion.
1472 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1473 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1474 /* Chips that use nested thread handlers have them marked */
1475 if (gc->irq.threaded)
1476 irq_set_nested_thread(irq, 1);
1477 irq_set_noprobe(irq);
1479 if (gc->irq.num_parents == 1)
1480 ret = irq_set_parent(irq, gc->irq.parents[0]);
1481 else if (gc->irq.map)
1482 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1488 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1489 * is passed as default type.
1491 if (gc->irq.default_type != IRQ_TYPE_NONE)
1492 irq_set_irq_type(irq, gc->irq.default_type);
1496 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1498 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1500 struct gpio_chip *gc = d->host_data;
1502 if (gc->irq.threaded)
1503 irq_set_nested_thread(irq, 0);
1504 irq_set_chip_and_handler(irq, NULL, NULL);
1505 irq_set_chip_data(irq, NULL);
1507 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1509 static const struct irq_domain_ops gpiochip_domain_ops = {
1510 .map = gpiochip_irq_map,
1511 .unmap = gpiochip_irq_unmap,
1512 /* Virtually all GPIO irqchips are twocell:ed */
1513 .xlate = irq_domain_xlate_twocell,
1516 static struct irq_domain *gpiochip_simple_create_domain(struct gpio_chip *gc)
1518 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1519 struct irq_domain *domain;
1521 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first,
1522 &gpiochip_domain_ops, gc);
1524 return ERR_PTR(-EINVAL);
1530 * TODO: move these activate/deactivate in under the hierarchicial
1531 * irqchip implementation as static once SPMI and SSBI (all external
1532 * users) are phased over.
1535 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1536 * @domain: The IRQ domain used by this IRQ chip
1537 * @data: Outermost irq_data associated with the IRQ
1538 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1540 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1541 * used as the activate function for the &struct irq_domain_ops. The host_data
1542 * for the IRQ domain must be the &struct gpio_chip.
1544 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1545 struct irq_data *data, bool reserve)
1547 struct gpio_chip *gc = domain->host_data;
1548 unsigned int hwirq = irqd_to_hwirq(data);
1550 return gpiochip_lock_as_irq(gc, hwirq);
1552 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1555 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1556 * @domain: The IRQ domain used by this IRQ chip
1557 * @data: Outermost irq_data associated with the IRQ
1559 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1560 * be used as the deactivate function for the &struct irq_domain_ops. The
1561 * host_data for the IRQ domain must be the &struct gpio_chip.
1563 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1564 struct irq_data *data)
1566 struct gpio_chip *gc = domain->host_data;
1567 unsigned int hwirq = irqd_to_hwirq(data);
1569 return gpiochip_unlock_as_irq(gc, hwirq);
1571 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1573 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1575 struct irq_domain *domain = gc->irq.domain;
1577 #ifdef CONFIG_GPIOLIB_IRQCHIP
1579 * Avoid race condition with other code, which tries to lookup
1580 * an IRQ before the irqchip has been properly registered,
1581 * i.e. while gpiochip is still being brought up.
1583 if (!gc->irq.initialized)
1584 return -EPROBE_DEFER;
1587 if (!gpiochip_irqchip_irq_valid(gc, offset))
1590 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1591 if (irq_domain_is_hierarchy(domain)) {
1592 struct irq_fwspec spec;
1594 spec.fwnode = domain->fwnode;
1595 spec.param_count = 2;
1596 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1597 spec.param[1] = IRQ_TYPE_NONE;
1599 return irq_create_fwspec_mapping(&spec);
1603 return irq_create_mapping(domain, offset);
1606 int gpiochip_irq_reqres(struct irq_data *d)
1608 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1609 unsigned int hwirq = irqd_to_hwirq(d);
1611 return gpiochip_reqres_irq(gc, hwirq);
1613 EXPORT_SYMBOL(gpiochip_irq_reqres);
1615 void gpiochip_irq_relres(struct irq_data *d)
1617 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1618 unsigned int hwirq = irqd_to_hwirq(d);
1620 gpiochip_relres_irq(gc, hwirq);
1622 EXPORT_SYMBOL(gpiochip_irq_relres);
1624 static void gpiochip_irq_mask(struct irq_data *d)
1626 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1627 unsigned int hwirq = irqd_to_hwirq(d);
1629 if (gc->irq.irq_mask)
1630 gc->irq.irq_mask(d);
1631 gpiochip_disable_irq(gc, hwirq);
1634 static void gpiochip_irq_unmask(struct irq_data *d)
1636 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1637 unsigned int hwirq = irqd_to_hwirq(d);
1639 gpiochip_enable_irq(gc, hwirq);
1640 if (gc->irq.irq_unmask)
1641 gc->irq.irq_unmask(d);
1644 static void gpiochip_irq_enable(struct irq_data *d)
1646 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1647 unsigned int hwirq = irqd_to_hwirq(d);
1649 gpiochip_enable_irq(gc, hwirq);
1650 gc->irq.irq_enable(d);
1653 static void gpiochip_irq_disable(struct irq_data *d)
1655 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1656 unsigned int hwirq = irqd_to_hwirq(d);
1658 gc->irq.irq_disable(d);
1659 gpiochip_disable_irq(gc, hwirq);
1662 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1664 struct irq_chip *irqchip = gc->irq.chip;
1666 if (irqchip->flags & IRQCHIP_IMMUTABLE)
1669 chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
1671 if (!irqchip->irq_request_resources &&
1672 !irqchip->irq_release_resources) {
1673 irqchip->irq_request_resources = gpiochip_irq_reqres;
1674 irqchip->irq_release_resources = gpiochip_irq_relres;
1676 if (WARN_ON(gc->irq.irq_enable))
1678 /* Check if the irqchip already has this hook... */
1679 if (irqchip->irq_enable == gpiochip_irq_enable ||
1680 irqchip->irq_mask == gpiochip_irq_mask) {
1682 * ...and if so, give a gentle warning that this is bad
1686 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1690 if (irqchip->irq_disable) {
1691 gc->irq.irq_disable = irqchip->irq_disable;
1692 irqchip->irq_disable = gpiochip_irq_disable;
1694 gc->irq.irq_mask = irqchip->irq_mask;
1695 irqchip->irq_mask = gpiochip_irq_mask;
1698 if (irqchip->irq_enable) {
1699 gc->irq.irq_enable = irqchip->irq_enable;
1700 irqchip->irq_enable = gpiochip_irq_enable;
1702 gc->irq.irq_unmask = irqchip->irq_unmask;
1703 irqchip->irq_unmask = gpiochip_irq_unmask;
1707 static int gpiochip_irqchip_add_allocated_domain(struct gpio_chip *gc,
1708 struct irq_domain *domain,
1709 bool allocated_externally)
1715 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1717 gc->to_irq = gpiochip_to_irq;
1718 gc->irq.domain = domain;
1719 gc->irq.domain_is_allocated_externally = allocated_externally;
1722 * Using barrier() here to prevent compiler from reordering
1723 * gc->irq.initialized before adding irqdomain.
1727 gc->irq.initialized = true;
1733 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1734 * @gc: the GPIO chip to add the IRQ chip to
1735 * @lock_key: lockdep class for IRQ lock
1736 * @request_key: lockdep class for IRQ request
1738 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1739 struct lock_class_key *lock_key,
1740 struct lock_class_key *request_key)
1742 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1743 struct irq_chip *irqchip = gc->irq.chip;
1744 struct irq_domain *domain;
1752 if (gc->irq.parent_handler && gc->can_sleep) {
1753 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1757 type = gc->irq.default_type;
1760 * Specifying a default trigger is a terrible idea if DT or ACPI is
1761 * used to configure the interrupts, as you may end up with
1762 * conflicting triggers. Tell the user, and reset to NONE.
1764 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1765 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1766 type = IRQ_TYPE_NONE;
1768 gc->irq.default_type = type;
1769 gc->irq.lock_key = lock_key;
1770 gc->irq.request_key = request_key;
1772 /* If a parent irqdomain is provided, let's build a hierarchy */
1773 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1774 domain = gpiochip_hierarchy_create_domain(gc);
1776 domain = gpiochip_simple_create_domain(gc);
1779 return PTR_ERR(domain);
1781 if (gc->irq.parent_handler) {
1782 for (i = 0; i < gc->irq.num_parents; i++) {
1785 if (gc->irq.per_parent_data)
1786 data = gc->irq.parent_handler_data_array[i];
1788 data = gc->irq.parent_handler_data ?: gc;
1791 * The parent IRQ chip is already using the chip_data
1792 * for this IRQ chip, so our callbacks simply use the
1795 irq_set_chained_handler_and_data(gc->irq.parents[i],
1796 gc->irq.parent_handler,
1801 gpiochip_set_irq_hooks(gc);
1803 ret = gpiochip_irqchip_add_allocated_domain(gc, domain, false);
1807 acpi_gpiochip_request_interrupts(gc);
1813 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1814 * @gc: the gpiochip to remove the irqchip from
1816 * This is called only from gpiochip_remove()
1818 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1820 struct irq_chip *irqchip = gc->irq.chip;
1821 unsigned int offset;
1823 acpi_gpiochip_free_interrupts(gc);
1825 if (irqchip && gc->irq.parent_handler) {
1826 struct gpio_irq_chip *irq = &gc->irq;
1829 for (i = 0; i < irq->num_parents; i++)
1830 irq_set_chained_handler_and_data(irq->parents[i],
1834 /* Remove all IRQ mappings and delete the domain */
1835 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) {
1838 for (offset = 0; offset < gc->ngpio; offset++) {
1839 if (!gpiochip_irqchip_irq_valid(gc, offset))
1842 irq = irq_find_mapping(gc->irq.domain, offset);
1843 irq_dispose_mapping(irq);
1846 irq_domain_remove(gc->irq.domain);
1849 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
1850 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1851 irqchip->irq_request_resources = NULL;
1852 irqchip->irq_release_resources = NULL;
1854 if (irqchip->irq_enable == gpiochip_irq_enable) {
1855 irqchip->irq_enable = gc->irq.irq_enable;
1856 irqchip->irq_disable = gc->irq.irq_disable;
1859 gc->irq.irq_enable = NULL;
1860 gc->irq.irq_disable = NULL;
1861 gc->irq.chip = NULL;
1863 gpiochip_irqchip_free_valid_mask(gc);
1867 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1868 * @gc: the gpiochip to add the irqchip to
1869 * @domain: the irqdomain to add to the gpiochip
1871 * This function adds an IRQ domain to the gpiochip.
1873 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1874 struct irq_domain *domain)
1876 return gpiochip_irqchip_add_allocated_domain(gc, domain, true);
1878 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1880 #else /* CONFIG_GPIOLIB_IRQCHIP */
1882 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1883 struct lock_class_key *lock_key,
1884 struct lock_class_key *request_key)
1888 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1890 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1895 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1899 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1902 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1905 * gpiochip_generic_request() - request the gpio function for a pin
1906 * @gc: the gpiochip owning the GPIO
1907 * @offset: the offset of the GPIO to request for GPIO function
1909 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
1911 #ifdef CONFIG_PINCTRL
1912 if (list_empty(&gc->gpiodev->pin_ranges))
1916 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1918 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1921 * gpiochip_generic_free() - free the gpio function from a pin
1922 * @gc: the gpiochip to request the gpio function for
1923 * @offset: the offset of the GPIO to free from GPIO function
1925 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
1927 #ifdef CONFIG_PINCTRL
1928 if (list_empty(&gc->gpiodev->pin_ranges))
1932 pinctrl_gpio_free(gc->gpiodev->base + offset);
1934 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1937 * gpiochip_generic_config() - apply configuration for a pin
1938 * @gc: the gpiochip owning the GPIO
1939 * @offset: the offset of the GPIO to apply the configuration
1940 * @config: the configuration to be applied
1942 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
1943 unsigned long config)
1945 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1947 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1949 #ifdef CONFIG_PINCTRL
1952 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1953 * @gc: the gpiochip to add the range for
1954 * @pctldev: the pin controller to map to
1955 * @gpio_offset: the start offset in the current gpio_chip number space
1956 * @pin_group: name of the pin group inside the pin controller
1958 * Calling this function directly from a DeviceTree-supported
1959 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1960 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1961 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1963 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1964 struct pinctrl_dev *pctldev,
1965 unsigned int gpio_offset, const char *pin_group)
1967 struct gpio_pin_range *pin_range;
1968 struct gpio_device *gdev = gc->gpiodev;
1971 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1973 chip_err(gc, "failed to allocate pin ranges\n");
1977 /* Use local offset as range ID */
1978 pin_range->range.id = gpio_offset;
1979 pin_range->range.gc = gc;
1980 pin_range->range.name = gc->label;
1981 pin_range->range.base = gdev->base + gpio_offset;
1982 pin_range->pctldev = pctldev;
1984 ret = pinctrl_get_group_pins(pctldev, pin_group,
1985 &pin_range->range.pins,
1986 &pin_range->range.npins);
1992 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1994 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1995 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1996 pinctrl_dev_get_devname(pctldev), pin_group);
1998 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2002 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2005 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2006 * @gc: the gpiochip to add the range for
2007 * @pinctl_name: the dev_name() of the pin controller to map to
2008 * @gpio_offset: the start offset in the current gpio_chip number space
2009 * @pin_offset: the start offset in the pin controller number space
2010 * @npins: the number of pins from the offset of each pin space (GPIO and
2011 * pin controller) to accumulate in this range
2014 * 0 on success, or a negative error-code on failure.
2016 * Calling this function directly from a DeviceTree-supported
2017 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2018 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2019 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2021 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
2022 unsigned int gpio_offset, unsigned int pin_offset,
2025 struct gpio_pin_range *pin_range;
2026 struct gpio_device *gdev = gc->gpiodev;
2029 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2031 chip_err(gc, "failed to allocate pin ranges\n");
2035 /* Use local offset as range ID */
2036 pin_range->range.id = gpio_offset;
2037 pin_range->range.gc = gc;
2038 pin_range->range.name = gc->label;
2039 pin_range->range.base = gdev->base + gpio_offset;
2040 pin_range->range.pin_base = pin_offset;
2041 pin_range->range.npins = npins;
2042 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2044 if (IS_ERR(pin_range->pctldev)) {
2045 ret = PTR_ERR(pin_range->pctldev);
2046 chip_err(gc, "could not create pin range\n");
2050 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2051 gpio_offset, gpio_offset + npins - 1,
2053 pin_offset, pin_offset + npins - 1);
2055 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2059 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2062 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2063 * @gc: the chip to remove all the mappings for
2065 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
2067 struct gpio_pin_range *pin_range, *tmp;
2068 struct gpio_device *gdev = gc->gpiodev;
2070 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2071 list_del(&pin_range->node);
2072 pinctrl_remove_gpio_range(pin_range->pctldev,
2077 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2079 #endif /* CONFIG_PINCTRL */
2081 /* These "optional" allocation calls help prevent drivers from stomping
2082 * on each other, and help provide better diagnostics in debugfs.
2083 * They're called even less than the "set direction" calls.
2085 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2087 struct gpio_chip *gc = desc->gdev->chip;
2089 unsigned long flags;
2093 label = kstrdup_const(label, GFP_KERNEL);
2098 spin_lock_irqsave(&gpio_lock, flags);
2100 /* NOTE: gpio_request() can be called in early boot,
2101 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2104 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2105 desc_set_label(desc, label ? : "?");
2108 goto out_free_unlock;
2112 /* gc->request may sleep */
2113 spin_unlock_irqrestore(&gpio_lock, flags);
2114 offset = gpio_chip_hwgpio(desc);
2115 if (gpiochip_line_is_valid(gc, offset))
2116 ret = gc->request(gc, offset);
2119 spin_lock_irqsave(&gpio_lock, flags);
2122 desc_set_label(desc, NULL);
2123 clear_bit(FLAG_REQUESTED, &desc->flags);
2124 goto out_free_unlock;
2127 if (gc->get_direction) {
2128 /* gc->get_direction may sleep */
2129 spin_unlock_irqrestore(&gpio_lock, flags);
2130 gpiod_get_direction(desc);
2131 spin_lock_irqsave(&gpio_lock, flags);
2133 spin_unlock_irqrestore(&gpio_lock, flags);
2137 spin_unlock_irqrestore(&gpio_lock, flags);
2143 * This descriptor validation needs to be inserted verbatim into each
2144 * function taking a descriptor, so we need to use a preprocessor
2145 * macro to avoid endless duplication. If the desc is NULL it is an
2146 * optional GPIO and calls should just bail out.
2148 static int validate_desc(const struct gpio_desc *desc, const char *func)
2153 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2154 return PTR_ERR(desc);
2157 pr_warn("%s: invalid GPIO (no device)\n", func);
2160 if (!desc->gdev->chip) {
2161 dev_warn(&desc->gdev->dev,
2162 "%s: backing chip is gone\n", func);
2168 #define VALIDATE_DESC(desc) do { \
2169 int __valid = validate_desc(desc, __func__); \
2174 #define VALIDATE_DESC_VOID(desc) do { \
2175 int __valid = validate_desc(desc, __func__); \
2180 int gpiod_request(struct gpio_desc *desc, const char *label)
2182 int ret = -EPROBE_DEFER;
2184 VALIDATE_DESC(desc);
2186 if (try_module_get(desc->gdev->owner)) {
2187 ret = gpiod_request_commit(desc, label);
2189 module_put(desc->gdev->owner);
2191 gpio_device_get(desc->gdev);
2195 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2200 static bool gpiod_free_commit(struct gpio_desc *desc)
2203 unsigned long flags;
2204 struct gpio_chip *gc;
2208 spin_lock_irqsave(&gpio_lock, flags);
2210 gc = desc->gdev->chip;
2211 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2213 spin_unlock_irqrestore(&gpio_lock, flags);
2214 might_sleep_if(gc->can_sleep);
2215 gc->free(gc, gpio_chip_hwgpio(desc));
2216 spin_lock_irqsave(&gpio_lock, flags);
2218 kfree_const(desc->label);
2219 desc_set_label(desc, NULL);
2220 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2221 clear_bit(FLAG_REQUESTED, &desc->flags);
2222 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2223 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2224 clear_bit(FLAG_PULL_UP, &desc->flags);
2225 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2226 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2227 clear_bit(FLAG_EDGE_RISING, &desc->flags);
2228 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
2229 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2230 #ifdef CONFIG_OF_DYNAMIC
2233 #ifdef CONFIG_GPIO_CDEV
2234 WRITE_ONCE(desc->debounce_period_us, 0);
2239 spin_unlock_irqrestore(&gpio_lock, flags);
2240 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED);
2245 void gpiod_free(struct gpio_desc *desc)
2248 * We must not use VALIDATE_DESC_VOID() as the underlying gdev->chip
2249 * may already be NULL but we still want to put the references.
2254 if (!gpiod_free_commit(desc))
2255 WARN_ON(extra_checks);
2257 module_put(desc->gdev->owner);
2258 gpio_device_put(desc->gdev);
2262 * gpiochip_is_requested - return string iff signal was requested
2263 * @gc: controller managing the signal
2264 * @offset: of signal within controller's 0..(ngpio - 1) range
2266 * Returns NULL if the GPIO is not currently requested, else a string.
2267 * The string returned is the label passed to gpio_request(); if none has been
2268 * passed it is a meaningless, non-NULL constant.
2270 * This function is for use by GPIO controller drivers. The label can
2271 * help with diagnostics, and knowing that the signal is used as a GPIO
2272 * can help avoid accidentally multiplexing it to another controller.
2274 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
2276 struct gpio_desc *desc;
2278 desc = gpiochip_get_desc(gc, offset);
2282 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2286 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2289 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2291 * @hwnum: hardware number of the GPIO for which to request the descriptor
2292 * @label: label for the GPIO
2293 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2294 * specify things like line inversion semantics with the machine flags
2295 * such as GPIO_OUT_LOW
2296 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2297 * can be used to specify consumer semantics such as open drain
2299 * Function allows GPIO chip drivers to request and use their own GPIO
2300 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2301 * function will not increase reference count of the GPIO chip module. This
2302 * allows the GPIO chip module to be unloaded as needed (we assume that the
2303 * GPIO chip driver handles freeing the GPIOs it has requested).
2306 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2309 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2312 enum gpio_lookup_flags lflags,
2313 enum gpiod_flags dflags)
2315 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2319 chip_err(gc, "failed to get GPIO descriptor\n");
2323 ret = gpiod_request_commit(desc, label);
2325 return ERR_PTR(ret);
2327 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2329 chip_err(gc, "setup of own GPIO %s failed\n", label);
2330 gpiod_free_commit(desc);
2331 return ERR_PTR(ret);
2336 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2339 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2340 * @desc: GPIO descriptor to free
2342 * Function frees the given GPIO requested previously with
2343 * gpiochip_request_own_desc().
2345 void gpiochip_free_own_desc(struct gpio_desc *desc)
2348 gpiod_free_commit(desc);
2350 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2353 * Drivers MUST set GPIO direction before making get/set calls. In
2354 * some cases this is done in early boot, before IRQs are enabled.
2356 * As a rule these aren't called more than once (except for drivers
2357 * using the open-drain emulation idiom) so these are natural places
2358 * to accumulate extra debugging checks. Note that we can't (yet)
2359 * rely on gpio_request() having been called beforehand.
2362 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2363 unsigned long config)
2365 if (!gc->set_config)
2368 return gc->set_config(gc, offset, config);
2371 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2372 enum pin_config_param mode,
2375 struct gpio_chip *gc = desc->gdev->chip;
2376 unsigned long config;
2378 config = pinconf_to_config_packed(mode, argument);
2379 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2382 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2383 enum pin_config_param mode,
2386 struct device *dev = &desc->gdev->dev;
2387 int gpio = gpio_chip_hwgpio(desc);
2390 ret = gpio_set_config_with_argument(desc, mode, argument);
2391 if (ret != -ENOTSUPP)
2395 case PIN_CONFIG_PERSIST_STATE:
2396 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2405 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2407 return gpio_set_config_with_argument(desc, mode, 0);
2410 static int gpio_set_bias(struct gpio_desc *desc)
2412 enum pin_config_param bias;
2415 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2416 bias = PIN_CONFIG_BIAS_DISABLE;
2417 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2418 bias = PIN_CONFIG_BIAS_PULL_UP;
2419 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2420 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2425 case PIN_CONFIG_BIAS_PULL_DOWN:
2426 case PIN_CONFIG_BIAS_PULL_UP:
2435 return gpio_set_config_with_argument_optional(desc, bias, arg);
2439 * gpio_set_debounce_timeout() - Set debounce timeout
2440 * @desc: GPIO descriptor to set the debounce timeout
2441 * @debounce: Debounce timeout in microseconds
2443 * The function calls the certain GPIO driver to set debounce timeout
2446 * Returns 0 on success, or negative error code otherwise.
2448 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2450 return gpio_set_config_with_argument_optional(desc,
2451 PIN_CONFIG_INPUT_DEBOUNCE,
2456 * gpiod_direction_input - set the GPIO direction to input
2457 * @desc: GPIO to set to input
2459 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2460 * be called safely on it.
2462 * Return 0 in case of success, else an error code.
2464 int gpiod_direction_input(struct gpio_desc *desc)
2466 struct gpio_chip *gc;
2469 VALIDATE_DESC(desc);
2470 gc = desc->gdev->chip;
2473 * It is legal to have no .get() and .direction_input() specified if
2474 * the chip is output-only, but you can't specify .direction_input()
2475 * and not support the .get() operation, that doesn't make sense.
2477 if (!gc->get && gc->direction_input) {
2479 "%s: missing get() but have direction_input()\n",
2485 * If we have a .direction_input() callback, things are simple,
2486 * just call it. Else we are some input-only chip so try to check the
2487 * direction (if .get_direction() is supported) else we silently
2488 * assume we are in input mode after this.
2490 if (gc->direction_input) {
2491 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2492 } else if (gc->get_direction &&
2493 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2495 "%s: missing direction_input() operation and line is output\n",
2500 clear_bit(FLAG_IS_OUT, &desc->flags);
2501 ret = gpio_set_bias(desc);
2504 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2508 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2510 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2512 struct gpio_chip *gc = desc->gdev->chip;
2517 * It's OK not to specify .direction_output() if the gpiochip is
2518 * output-only, but if there is then not even a .set() operation it
2519 * is pretty tricky to drive the output line.
2521 if (!gc->set && !gc->direction_output) {
2523 "%s: missing set() and direction_output() operations\n",
2528 if (gc->direction_output) {
2529 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2531 /* Check that we are in output mode if we can */
2532 if (gc->get_direction &&
2533 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2535 "%s: missing direction_output() operation\n",
2540 * If we can't actively set the direction, we are some
2541 * output-only chip, so just drive the output as desired.
2543 gc->set(gc, gpio_chip_hwgpio(desc), val);
2547 set_bit(FLAG_IS_OUT, &desc->flags);
2548 trace_gpio_value(desc_to_gpio(desc), 0, val);
2549 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2554 * gpiod_direction_output_raw - set the GPIO direction to output
2555 * @desc: GPIO to set to output
2556 * @value: initial output value of the GPIO
2558 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2559 * be called safely on it. The initial value of the output must be specified
2560 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2562 * Return 0 in case of success, else an error code.
2564 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2566 VALIDATE_DESC(desc);
2567 return gpiod_direction_output_raw_commit(desc, value);
2569 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2572 * gpiod_direction_output - set the GPIO direction to output
2573 * @desc: GPIO to set to output
2574 * @value: initial output value of the GPIO
2576 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2577 * be called safely on it. The initial value of the output must be specified
2578 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2581 * Return 0 in case of success, else an error code.
2583 int gpiod_direction_output(struct gpio_desc *desc, int value)
2587 VALIDATE_DESC(desc);
2588 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2593 /* GPIOs used for enabled IRQs shall not be set as output */
2594 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2595 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2597 "%s: tried to set a GPIO tied to an IRQ as output\n",
2602 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2603 /* First see if we can enable open drain in hardware */
2604 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2606 goto set_output_value;
2607 /* Emulate open drain by not actively driving the line high */
2609 ret = gpiod_direction_input(desc);
2610 goto set_output_flag;
2612 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2613 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2615 goto set_output_value;
2616 /* Emulate open source by not actively driving the line low */
2618 ret = gpiod_direction_input(desc);
2619 goto set_output_flag;
2622 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2626 ret = gpio_set_bias(desc);
2629 return gpiod_direction_output_raw_commit(desc, value);
2633 * When emulating open-source or open-drain functionalities by not
2634 * actively driving the line (setting mode to input) we still need to
2635 * set the IS_OUT flag or otherwise we won't be able to set the line
2639 set_bit(FLAG_IS_OUT, &desc->flags);
2642 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2645 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2647 * @desc: GPIO to enable.
2648 * @flags: Flags related to GPIO edge.
2650 * Return 0 in case of success, else negative error code.
2652 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2655 struct gpio_chip *gc;
2657 VALIDATE_DESC(desc);
2659 gc = desc->gdev->chip;
2660 if (!gc->en_hw_timestamp) {
2661 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2665 ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2667 gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
2671 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
2674 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2676 * @desc: GPIO to disable.
2677 * @flags: Flags related to GPIO edge, same value as used during enable call.
2679 * Return 0 in case of success, else negative error code.
2681 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2684 struct gpio_chip *gc;
2686 VALIDATE_DESC(desc);
2688 gc = desc->gdev->chip;
2689 if (!gc->dis_hw_timestamp) {
2690 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2694 ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2696 gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
2700 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
2703 * gpiod_set_config - sets @config for a GPIO
2704 * @desc: descriptor of the GPIO for which to set the configuration
2705 * @config: Same packed config format as generic pinconf
2708 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2711 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2713 struct gpio_chip *gc;
2715 VALIDATE_DESC(desc);
2716 gc = desc->gdev->chip;
2718 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2720 EXPORT_SYMBOL_GPL(gpiod_set_config);
2723 * gpiod_set_debounce - sets @debounce time for a GPIO
2724 * @desc: descriptor of the GPIO for which to set debounce time
2725 * @debounce: debounce time in microseconds
2728 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2731 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2733 unsigned long config;
2735 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2736 return gpiod_set_config(desc, config);
2738 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2741 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2742 * @desc: descriptor of the GPIO for which to configure persistence
2743 * @transitory: True to lose state on suspend or reset, false for persistence
2746 * 0 on success, otherwise a negative error code.
2748 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2750 VALIDATE_DESC(desc);
2752 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2753 * persistence state.
2755 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2757 /* If the driver supports it, set the persistence state now */
2758 return gpio_set_config_with_argument_optional(desc,
2759 PIN_CONFIG_PERSIST_STATE,
2762 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2765 * gpiod_is_active_low - test whether a GPIO is active-low or not
2766 * @desc: the gpio descriptor to test
2768 * Returns 1 if the GPIO is active-low, 0 otherwise.
2770 int gpiod_is_active_low(const struct gpio_desc *desc)
2772 VALIDATE_DESC(desc);
2773 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2775 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2778 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2779 * @desc: the gpio descriptor to change
2781 void gpiod_toggle_active_low(struct gpio_desc *desc)
2783 VALIDATE_DESC_VOID(desc);
2784 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2786 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2788 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
2790 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
2793 /* I/O calls are only valid after configuration completed; the relevant
2794 * "is this a valid GPIO" error checks should already have been done.
2796 * "Get" operations are often inlinable as reading a pin value register,
2797 * and masking the relevant bit in that register.
2799 * When "set" operations are inlinable, they involve writing that mask to
2800 * one register to set a low value, or a different register to set it high.
2801 * Otherwise locking is needed, so there may be little value to inlining.
2803 *------------------------------------------------------------------------
2805 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2806 * have requested the GPIO. That can include implicit requesting by
2807 * a direction setting call. Marking a gpio as requested locks its chip
2808 * in memory, guaranteeing that these table lookups need no more locking
2809 * and that gpiochip_remove() will fail.
2811 * REVISIT when debugging, consider adding some instrumentation to ensure
2812 * that the GPIO was actually requested.
2815 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2817 struct gpio_chip *gc;
2820 gc = desc->gdev->chip;
2821 value = gpio_chip_get_value(gc, desc);
2822 value = value < 0 ? value : !!value;
2823 trace_gpio_value(desc_to_gpio(desc), 1, value);
2827 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2828 unsigned long *mask, unsigned long *bits)
2830 if (gc->get_multiple)
2831 return gc->get_multiple(gc, mask, bits);
2835 for_each_set_bit(i, mask, gc->ngpio) {
2836 value = gc->get(gc, i);
2839 __assign_bit(i, bits, value);
2846 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2847 unsigned int array_size,
2848 struct gpio_desc **desc_array,
2849 struct gpio_array *array_info,
2850 unsigned long *value_bitmap)
2855 * Validate array_info against desc_array and its size.
2856 * It should immediately follow desc_array if both
2857 * have been obtained from the same gpiod_get_array() call.
2859 if (array_info && array_info->desc == desc_array &&
2860 array_size <= array_info->size &&
2861 (void *)array_info == desc_array + array_info->size) {
2863 WARN_ON(array_info->chip->can_sleep);
2865 ret = gpio_chip_get_multiple(array_info->chip,
2866 array_info->get_mask,
2871 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2872 bitmap_xor(value_bitmap, value_bitmap,
2873 array_info->invert_mask, array_size);
2875 i = find_first_zero_bit(array_info->get_mask, array_size);
2876 if (i == array_size)
2882 while (i < array_size) {
2883 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2884 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
2885 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
2886 unsigned long *mask, *bits;
2889 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2890 mask = fastpath_mask;
2891 bits = fastpath_bits;
2893 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
2895 mask = bitmap_alloc(gc->ngpio, flags);
2899 bits = bitmap_alloc(gc->ngpio, flags);
2906 bitmap_zero(mask, gc->ngpio);
2909 WARN_ON(gc->can_sleep);
2911 /* collect all inputs belonging to the same chip */
2914 const struct gpio_desc *desc = desc_array[i];
2915 int hwgpio = gpio_chip_hwgpio(desc);
2917 __set_bit(hwgpio, mask);
2921 i = find_next_zero_bit(array_info->get_mask,
2923 } while ((i < array_size) &&
2924 (desc_array[i]->gdev->chip == gc));
2926 ret = gpio_chip_get_multiple(gc, mask, bits);
2928 if (mask != fastpath_mask)
2930 if (bits != fastpath_bits)
2935 for (j = first; j < i; ) {
2936 const struct gpio_desc *desc = desc_array[j];
2937 int hwgpio = gpio_chip_hwgpio(desc);
2938 int value = test_bit(hwgpio, bits);
2940 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2942 __assign_bit(j, value_bitmap, value);
2943 trace_gpio_value(desc_to_gpio(desc), 1, value);
2947 j = find_next_zero_bit(array_info->get_mask, i,
2951 if (mask != fastpath_mask)
2953 if (bits != fastpath_bits)
2960 * gpiod_get_raw_value() - return a gpio's raw value
2961 * @desc: gpio whose value will be returned
2963 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2964 * its ACTIVE_LOW status, or negative errno on failure.
2966 * This function can be called from contexts where we cannot sleep, and will
2967 * complain if the GPIO chip functions potentially sleep.
2969 int gpiod_get_raw_value(const struct gpio_desc *desc)
2971 VALIDATE_DESC(desc);
2972 /* Should be using gpiod_get_raw_value_cansleep() */
2973 WARN_ON(desc->gdev->chip->can_sleep);
2974 return gpiod_get_raw_value_commit(desc);
2976 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2979 * gpiod_get_value() - return a gpio's value
2980 * @desc: gpio whose value will be returned
2982 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2983 * account, or negative errno on failure.
2985 * This function can be called from contexts where we cannot sleep, and will
2986 * complain if the GPIO chip functions potentially sleep.
2988 int gpiod_get_value(const struct gpio_desc *desc)
2992 VALIDATE_DESC(desc);
2993 /* Should be using gpiod_get_value_cansleep() */
2994 WARN_ON(desc->gdev->chip->can_sleep);
2996 value = gpiod_get_raw_value_commit(desc);
3000 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3005 EXPORT_SYMBOL_GPL(gpiod_get_value);
3008 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3009 * @array_size: number of elements in the descriptor array / value bitmap
3010 * @desc_array: array of GPIO descriptors whose values will be read
3011 * @array_info: information on applicability of fast bitmap processing path
3012 * @value_bitmap: bitmap to store the read values
3014 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3015 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3016 * else an error code.
3018 * This function can be called from contexts where we cannot sleep,
3019 * and it will complain if the GPIO chip functions potentially sleep.
3021 int gpiod_get_raw_array_value(unsigned int array_size,
3022 struct gpio_desc **desc_array,
3023 struct gpio_array *array_info,
3024 unsigned long *value_bitmap)
3028 return gpiod_get_array_value_complex(true, false, array_size,
3029 desc_array, array_info,
3032 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3035 * gpiod_get_array_value() - read values from an array of GPIOs
3036 * @array_size: number of elements in the descriptor array / value bitmap
3037 * @desc_array: array of GPIO descriptors whose values will be read
3038 * @array_info: information on applicability of fast bitmap processing path
3039 * @value_bitmap: bitmap to store the read values
3041 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3042 * into account. Return 0 in case of success, else an error code.
3044 * This function can be called from contexts where we cannot sleep,
3045 * and it will complain if the GPIO chip functions potentially sleep.
3047 int gpiod_get_array_value(unsigned int array_size,
3048 struct gpio_desc **desc_array,
3049 struct gpio_array *array_info,
3050 unsigned long *value_bitmap)
3054 return gpiod_get_array_value_complex(false, false, array_size,
3055 desc_array, array_info,
3058 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3061 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3062 * @desc: gpio descriptor whose state need to be set.
3063 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3065 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
3068 struct gpio_chip *gc = desc->gdev->chip;
3069 int offset = gpio_chip_hwgpio(desc);
3072 ret = gc->direction_input(gc, offset);
3074 ret = gc->direction_output(gc, offset, 0);
3076 set_bit(FLAG_IS_OUT, &desc->flags);
3078 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3081 "%s: Error in set_value for open drain err %d\n",
3086 * _gpio_set_open_source_value() - Set the open source gpio's value.
3087 * @desc: gpio descriptor whose state need to be set.
3088 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3090 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3093 struct gpio_chip *gc = desc->gdev->chip;
3094 int offset = gpio_chip_hwgpio(desc);
3097 ret = gc->direction_output(gc, offset, 1);
3099 set_bit(FLAG_IS_OUT, &desc->flags);
3101 ret = gc->direction_input(gc, offset);
3103 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3106 "%s: Error in set_value for open source err %d\n",
3110 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3112 struct gpio_chip *gc;
3114 gc = desc->gdev->chip;
3115 trace_gpio_value(desc_to_gpio(desc), 0, value);
3116 gc->set(gc, gpio_chip_hwgpio(desc), value);
3120 * set multiple outputs on the same chip;
3121 * use the chip's set_multiple function if available;
3122 * otherwise set the outputs sequentially;
3123 * @chip: the GPIO chip we operate on
3124 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3125 * defines which outputs are to be changed
3126 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3127 * defines the values the outputs specified by mask are to be set to
3129 static void gpio_chip_set_multiple(struct gpio_chip *gc,
3130 unsigned long *mask, unsigned long *bits)
3132 if (gc->set_multiple) {
3133 gc->set_multiple(gc, mask, bits);
3137 /* set outputs if the corresponding mask bit is set */
3138 for_each_set_bit(i, mask, gc->ngpio)
3139 gc->set(gc, i, test_bit(i, bits));
3143 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3144 unsigned int array_size,
3145 struct gpio_desc **desc_array,
3146 struct gpio_array *array_info,
3147 unsigned long *value_bitmap)
3152 * Validate array_info against desc_array and its size.
3153 * It should immediately follow desc_array if both
3154 * have been obtained from the same gpiod_get_array() call.
3156 if (array_info && array_info->desc == desc_array &&
3157 array_size <= array_info->size &&
3158 (void *)array_info == desc_array + array_info->size) {
3160 WARN_ON(array_info->chip->can_sleep);
3162 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3163 bitmap_xor(value_bitmap, value_bitmap,
3164 array_info->invert_mask, array_size);
3166 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3169 i = find_first_zero_bit(array_info->set_mask, array_size);
3170 if (i == array_size)
3176 while (i < array_size) {
3177 struct gpio_chip *gc = desc_array[i]->gdev->chip;
3178 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3179 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3180 unsigned long *mask, *bits;
3183 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
3184 mask = fastpath_mask;
3185 bits = fastpath_bits;
3187 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3189 mask = bitmap_alloc(gc->ngpio, flags);
3193 bits = bitmap_alloc(gc->ngpio, flags);
3200 bitmap_zero(mask, gc->ngpio);
3203 WARN_ON(gc->can_sleep);
3206 struct gpio_desc *desc = desc_array[i];
3207 int hwgpio = gpio_chip_hwgpio(desc);
3208 int value = test_bit(i, value_bitmap);
3211 * Pins applicable for fast input but not for
3212 * fast output processing may have been already
3213 * inverted inside the fast path, skip them.
3215 if (!raw && !(array_info &&
3216 test_bit(i, array_info->invert_mask)) &&
3217 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3219 trace_gpio_value(desc_to_gpio(desc), 0, value);
3221 * collect all normal outputs belonging to the same chip
3222 * open drain and open source outputs are set individually
3224 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3225 gpio_set_open_drain_value_commit(desc, value);
3226 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3227 gpio_set_open_source_value_commit(desc, value);
3229 __set_bit(hwgpio, mask);
3230 __assign_bit(hwgpio, bits, value);
3236 i = find_next_zero_bit(array_info->set_mask,
3238 } while ((i < array_size) &&
3239 (desc_array[i]->gdev->chip == gc));
3240 /* push collected bits to outputs */
3242 gpio_chip_set_multiple(gc, mask, bits);
3244 if (mask != fastpath_mask)
3246 if (bits != fastpath_bits)
3253 * gpiod_set_raw_value() - assign a gpio's raw value
3254 * @desc: gpio whose value will be assigned
3255 * @value: value to assign
3257 * Set the raw value of the GPIO, i.e. the value of its physical line without
3258 * regard for its ACTIVE_LOW status.
3260 * This function can be called from contexts where we cannot sleep, and will
3261 * complain if the GPIO chip functions potentially sleep.
3263 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3265 VALIDATE_DESC_VOID(desc);
3266 /* Should be using gpiod_set_raw_value_cansleep() */
3267 WARN_ON(desc->gdev->chip->can_sleep);
3268 gpiod_set_raw_value_commit(desc, value);
3270 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3273 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3274 * @desc: the descriptor to set the value on
3275 * @value: value to set
3277 * This sets the value of a GPIO line backing a descriptor, applying
3278 * different semantic quirks like active low and open drain/source
3281 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3283 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3285 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3286 gpio_set_open_drain_value_commit(desc, value);
3287 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3288 gpio_set_open_source_value_commit(desc, value);
3290 gpiod_set_raw_value_commit(desc, value);
3294 * gpiod_set_value() - assign a gpio's value
3295 * @desc: gpio whose value will be assigned
3296 * @value: value to assign
3298 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3299 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3301 * This function can be called from contexts where we cannot sleep, and will
3302 * complain if the GPIO chip functions potentially sleep.
3304 void gpiod_set_value(struct gpio_desc *desc, int value)
3306 VALIDATE_DESC_VOID(desc);
3307 /* Should be using gpiod_set_value_cansleep() */
3308 WARN_ON(desc->gdev->chip->can_sleep);
3309 gpiod_set_value_nocheck(desc, value);
3311 EXPORT_SYMBOL_GPL(gpiod_set_value);
3314 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3315 * @array_size: number of elements in the descriptor array / value bitmap
3316 * @desc_array: array of GPIO descriptors whose values will be assigned
3317 * @array_info: information on applicability of fast bitmap processing path
3318 * @value_bitmap: bitmap of values to assign
3320 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3321 * without regard for their ACTIVE_LOW status.
3323 * This function can be called from contexts where we cannot sleep, and will
3324 * complain if the GPIO chip functions potentially sleep.
3326 int gpiod_set_raw_array_value(unsigned int array_size,
3327 struct gpio_desc **desc_array,
3328 struct gpio_array *array_info,
3329 unsigned long *value_bitmap)
3333 return gpiod_set_array_value_complex(true, false, array_size,
3334 desc_array, array_info, value_bitmap);
3336 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3339 * gpiod_set_array_value() - assign values to an array of GPIOs
3340 * @array_size: number of elements in the descriptor array / value bitmap
3341 * @desc_array: array of GPIO descriptors whose values will be assigned
3342 * @array_info: information on applicability of fast bitmap processing path
3343 * @value_bitmap: bitmap of values to assign
3345 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3348 * This function can be called from contexts where we cannot sleep, and will
3349 * complain if the GPIO chip functions potentially sleep.
3351 int gpiod_set_array_value(unsigned int array_size,
3352 struct gpio_desc **desc_array,
3353 struct gpio_array *array_info,
3354 unsigned long *value_bitmap)
3358 return gpiod_set_array_value_complex(false, false, array_size,
3359 desc_array, array_info,
3362 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3365 * gpiod_cansleep() - report whether gpio value access may sleep
3366 * @desc: gpio to check
3369 int gpiod_cansleep(const struct gpio_desc *desc)
3371 VALIDATE_DESC(desc);
3372 return desc->gdev->chip->can_sleep;
3374 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3377 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3378 * @desc: gpio to set the consumer name on
3379 * @name: the new consumer name
3381 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3383 VALIDATE_DESC(desc);
3385 name = kstrdup_const(name, GFP_KERNEL);
3390 kfree_const(desc->label);
3391 desc_set_label(desc, name);
3395 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3398 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3399 * @desc: gpio whose IRQ will be returned (already requested)
3401 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3404 int gpiod_to_irq(const struct gpio_desc *desc)
3406 struct gpio_chip *gc;
3410 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3411 * requires this function to not return zero on an invalid descriptor
3412 * but rather a negative error number.
3414 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3417 gc = desc->gdev->chip;
3418 offset = gpio_chip_hwgpio(desc);
3420 int retirq = gc->to_irq(gc, offset);
3422 /* Zero means NO_IRQ */
3428 #ifdef CONFIG_GPIOLIB_IRQCHIP
3431 * Avoid race condition with other code, which tries to lookup
3432 * an IRQ before the irqchip has been properly registered,
3433 * i.e. while gpiochip is still being brought up.
3435 return -EPROBE_DEFER;
3440 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3443 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3444 * @gc: the chip the GPIO to lock belongs to
3445 * @offset: the offset of the GPIO to lock as IRQ
3447 * This is used directly by GPIO drivers that want to lock down
3448 * a certain GPIO line to be used for IRQs.
3450 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3452 struct gpio_desc *desc;
3454 desc = gpiochip_get_desc(gc, offset);
3456 return PTR_ERR(desc);
3459 * If it's fast: flush the direction setting if something changed
3462 if (!gc->can_sleep && gc->get_direction) {
3463 int dir = gpiod_get_direction(desc);
3466 chip_err(gc, "%s: cannot get GPIO direction\n",
3472 /* To be valid for IRQ the line needs to be input or open drain */
3473 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3474 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3476 "%s: tried to flag a GPIO set as output for IRQ\n",
3481 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3482 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3485 * If the consumer has not set up a label (such as when the
3486 * IRQ is referenced from .to_irq()) we set up a label here
3487 * so it is clear this is used as an interrupt.
3490 desc_set_label(desc, "interrupt");
3494 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3497 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3498 * @gc: the chip the GPIO to lock belongs to
3499 * @offset: the offset of the GPIO to lock as IRQ
3501 * This is used directly by GPIO drivers that want to indicate
3502 * that a certain GPIO is no longer used exclusively for IRQ.
3504 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3506 struct gpio_desc *desc;
3508 desc = gpiochip_get_desc(gc, offset);
3512 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3513 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3515 /* If we only had this marking, erase it */
3516 if (desc->label && !strcmp(desc->label, "interrupt"))
3517 desc_set_label(desc, NULL);
3519 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3521 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3523 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3525 if (!IS_ERR(desc) &&
3526 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3527 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3529 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3531 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3533 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3535 if (!IS_ERR(desc) &&
3536 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3538 * We must not be output when using IRQ UNLESS we are
3541 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3542 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3543 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3546 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3548 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3550 if (offset >= gc->ngpio)
3553 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3555 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3557 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3561 if (!try_module_get(gc->gpiodev->owner))
3564 ret = gpiochip_lock_as_irq(gc, offset);
3566 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3567 module_put(gc->gpiodev->owner);
3572 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3574 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3576 gpiochip_unlock_as_irq(gc, offset);
3577 module_put(gc->gpiodev->owner);
3579 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3581 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3583 if (offset >= gc->ngpio)
3586 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3588 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3590 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3592 if (offset >= gc->ngpio)
3595 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3597 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3599 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3601 if (offset >= gc->ngpio)
3604 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3606 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3609 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3610 * @desc: gpio whose value will be returned
3612 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3613 * its ACTIVE_LOW status, or negative errno on failure.
3615 * This function is to be called from contexts that can sleep.
3617 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3619 might_sleep_if(extra_checks);
3620 VALIDATE_DESC(desc);
3621 return gpiod_get_raw_value_commit(desc);
3623 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3626 * gpiod_get_value_cansleep() - return a gpio's value
3627 * @desc: gpio whose value will be returned
3629 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3630 * account, or negative errno on failure.
3632 * This function is to be called from contexts that can sleep.
3634 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3638 might_sleep_if(extra_checks);
3639 VALIDATE_DESC(desc);
3640 value = gpiod_get_raw_value_commit(desc);
3644 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3649 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3652 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3653 * @array_size: number of elements in the descriptor array / value bitmap
3654 * @desc_array: array of GPIO descriptors whose values will be read
3655 * @array_info: information on applicability of fast bitmap processing path
3656 * @value_bitmap: bitmap to store the read values
3658 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3659 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3660 * else an error code.
3662 * This function is to be called from contexts that can sleep.
3664 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3665 struct gpio_desc **desc_array,
3666 struct gpio_array *array_info,
3667 unsigned long *value_bitmap)
3669 might_sleep_if(extra_checks);
3672 return gpiod_get_array_value_complex(true, true, array_size,
3673 desc_array, array_info,
3676 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3679 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3680 * @array_size: number of elements in the descriptor array / value bitmap
3681 * @desc_array: array of GPIO descriptors whose values will be read
3682 * @array_info: information on applicability of fast bitmap processing path
3683 * @value_bitmap: bitmap to store the read values
3685 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3686 * into account. Return 0 in case of success, else an error code.
3688 * This function is to be called from contexts that can sleep.
3690 int gpiod_get_array_value_cansleep(unsigned int array_size,
3691 struct gpio_desc **desc_array,
3692 struct gpio_array *array_info,
3693 unsigned long *value_bitmap)
3695 might_sleep_if(extra_checks);
3698 return gpiod_get_array_value_complex(false, true, array_size,
3699 desc_array, array_info,
3702 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3705 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3706 * @desc: gpio whose value will be assigned
3707 * @value: value to assign
3709 * Set the raw value of the GPIO, i.e. the value of its physical line without
3710 * regard for its ACTIVE_LOW status.
3712 * This function is to be called from contexts that can sleep.
3714 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3716 might_sleep_if(extra_checks);
3717 VALIDATE_DESC_VOID(desc);
3718 gpiod_set_raw_value_commit(desc, value);
3720 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3723 * gpiod_set_value_cansleep() - assign a gpio's value
3724 * @desc: gpio whose value will be assigned
3725 * @value: value to assign
3727 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3730 * This function is to be called from contexts that can sleep.
3732 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3734 might_sleep_if(extra_checks);
3735 VALIDATE_DESC_VOID(desc);
3736 gpiod_set_value_nocheck(desc, value);
3738 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3741 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3742 * @array_size: number of elements in the descriptor array / value bitmap
3743 * @desc_array: array of GPIO descriptors whose values will be assigned
3744 * @array_info: information on applicability of fast bitmap processing path
3745 * @value_bitmap: bitmap of values to assign
3747 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3748 * without regard for their ACTIVE_LOW status.
3750 * This function is to be called from contexts that can sleep.
3752 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3753 struct gpio_desc **desc_array,
3754 struct gpio_array *array_info,
3755 unsigned long *value_bitmap)
3757 might_sleep_if(extra_checks);
3760 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3761 array_info, value_bitmap);
3763 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3766 * gpiod_add_lookup_tables() - register GPIO device consumers
3767 * @tables: list of tables of consumers to register
3768 * @n: number of tables in the list
3770 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3774 mutex_lock(&gpio_lookup_lock);
3776 for (i = 0; i < n; i++)
3777 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3779 mutex_unlock(&gpio_lookup_lock);
3783 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3784 * @array_size: number of elements in the descriptor array / value bitmap
3785 * @desc_array: array of GPIO descriptors whose values will be assigned
3786 * @array_info: information on applicability of fast bitmap processing path
3787 * @value_bitmap: bitmap of values to assign
3789 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3792 * This function is to be called from contexts that can sleep.
3794 int gpiod_set_array_value_cansleep(unsigned int array_size,
3795 struct gpio_desc **desc_array,
3796 struct gpio_array *array_info,
3797 unsigned long *value_bitmap)
3799 might_sleep_if(extra_checks);
3802 return gpiod_set_array_value_complex(false, true, array_size,
3803 desc_array, array_info,
3806 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3808 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action)
3810 blocking_notifier_call_chain(&desc->gdev->line_state_notifier,
3815 * gpiod_add_lookup_table() - register GPIO device consumers
3816 * @table: table of consumers to register
3818 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3820 gpiod_add_lookup_tables(&table, 1);
3822 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3825 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3826 * @table: table of consumers to unregister
3828 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3830 /* Nothing to remove */
3834 mutex_lock(&gpio_lookup_lock);
3836 list_del(&table->list);
3838 mutex_unlock(&gpio_lookup_lock);
3840 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3843 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3844 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3846 void gpiod_add_hogs(struct gpiod_hog *hogs)
3848 struct gpio_chip *gc;
3849 struct gpiod_hog *hog;
3851 mutex_lock(&gpio_machine_hogs_mutex);
3853 for (hog = &hogs[0]; hog->chip_label; hog++) {
3854 list_add_tail(&hog->list, &gpio_machine_hogs);
3857 * The chip may have been registered earlier, so check if it
3858 * exists and, if so, try to hog the line now.
3860 gc = find_chip_by_name(hog->chip_label);
3862 gpiochip_machine_hog(gc, hog);
3865 mutex_unlock(&gpio_machine_hogs_mutex);
3867 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3869 void gpiod_remove_hogs(struct gpiod_hog *hogs)
3871 struct gpiod_hog *hog;
3873 mutex_lock(&gpio_machine_hogs_mutex);
3874 for (hog = &hogs[0]; hog->chip_label; hog++)
3875 list_del(&hog->list);
3876 mutex_unlock(&gpio_machine_hogs_mutex);
3878 EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
3880 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3882 const char *dev_id = dev ? dev_name(dev) : NULL;
3883 struct gpiod_lookup_table *table;
3885 mutex_lock(&gpio_lookup_lock);
3887 list_for_each_entry(table, &gpio_lookup_list, list) {
3888 if (table->dev_id && dev_id) {
3890 * Valid strings on both ends, must be identical to have
3893 if (!strcmp(table->dev_id, dev_id))
3897 * One of the pointers is NULL, so both must be to have
3900 if (dev_id == table->dev_id)
3907 mutex_unlock(&gpio_lookup_lock);
3911 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3912 unsigned int idx, unsigned long *flags)
3914 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3915 struct gpiod_lookup_table *table;
3916 struct gpiod_lookup *p;
3918 table = gpiod_find_lookup_table(dev);
3922 for (p = &table->table[0]; p->key; p++) {
3923 struct gpio_chip *gc;
3925 /* idx must always match exactly */
3929 /* If the lookup entry has a con_id, require exact match */
3930 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3933 if (p->chip_hwnum == U16_MAX) {
3934 desc = gpio_name_to_desc(p->key);
3940 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3942 return ERR_PTR(-EPROBE_DEFER);
3945 gc = find_chip_by_name(p->key);
3949 * As the lookup table indicates a chip with
3950 * p->key should exist, assume it may
3951 * still appear later and let the interested
3952 * consumer be probed again or let the Deferred
3953 * Probe infrastructure handle the error.
3955 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3957 return ERR_PTR(-EPROBE_DEFER);
3960 if (gc->ngpio <= p->chip_hwnum) {
3962 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3963 idx, p->chip_hwnum, gc->ngpio - 1,
3965 return ERR_PTR(-EINVAL);
3968 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3977 static int platform_gpio_count(struct device *dev, const char *con_id)
3979 struct gpiod_lookup_table *table;
3980 struct gpiod_lookup *p;
3981 unsigned int count = 0;
3983 table = gpiod_find_lookup_table(dev);
3987 for (p = &table->table[0]; p->key; p++) {
3988 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3989 (!con_id && !p->con_id))
3998 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
3999 struct device *consumer,
4002 enum gpiod_flags *flags,
4003 unsigned long *lookupflags)
4005 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4007 if (is_of_node(fwnode)) {
4008 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n",
4010 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags);
4011 } else if (is_acpi_node(fwnode)) {
4012 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n",
4014 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
4015 } else if (is_software_node(fwnode)) {
4016 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n",
4018 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
4024 static struct gpio_desc *gpiod_find_and_request(struct device *consumer,
4025 struct fwnode_handle *fwnode,
4028 enum gpiod_flags flags,
4030 bool platform_lookup_allowed)
4032 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4033 struct gpio_desc *desc;
4036 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, &flags, &lookupflags);
4037 if (gpiod_not_found(desc) && platform_lookup_allowed) {
4039 * Either we are not using DT or ACPI, or their lookup did not
4040 * return a result. In that case, use platform lookup as a
4043 dev_dbg(consumer, "using lookup tables for GPIO lookup\n");
4044 desc = gpiod_find(consumer, con_id, idx, &lookupflags);
4048 dev_dbg(consumer, "No GPIO consumer %s found\n", con_id);
4053 * If a connection label was passed use that, else attempt to use
4054 * the device name as label
4056 ret = gpiod_request(desc, label);
4058 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
4059 return ERR_PTR(ret);
4062 * This happens when there are several consumers for
4063 * the same GPIO line: we just return here without
4064 * further initialization. It is a bit of a hack.
4065 * This is necessary to support fixed regulators.
4067 * FIXME: Make this more sane and safe.
4070 "nonexclusive access to GPIO for %s\n", con_id);
4074 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4076 dev_dbg(consumer, "setup of GPIO %s failed\n", con_id);
4078 return ERR_PTR(ret);
4081 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_REQUESTED);
4087 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4088 * @fwnode: handle of the firmware node
4089 * @con_id: function within the GPIO consumer
4090 * @index: index of the GPIO to obtain for the consumer
4091 * @flags: GPIO initialization flags
4092 * @label: label to attach to the requested GPIO
4094 * This function can be used for drivers that get their configuration
4095 * from opaque firmware.
4097 * The function properly finds the corresponding GPIO using whatever is the
4098 * underlying firmware interface and then makes sure that the GPIO
4099 * descriptor is requested before it is returned to the caller.
4102 * On successful request the GPIO pin is configured in accordance with
4105 * In case of error an ERR_PTR() is returned.
4107 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4110 enum gpiod_flags flags,
4113 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false);
4115 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4118 * gpiod_count - return the number of GPIOs associated with a device / function
4119 * or -ENOENT if no GPIO has been assigned to the requested function
4120 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4121 * @con_id: function within the GPIO consumer
4123 int gpiod_count(struct device *dev, const char *con_id)
4125 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4126 int count = -ENOENT;
4128 if (is_of_node(fwnode))
4129 count = of_gpio_get_count(dev, con_id);
4130 else if (is_acpi_node(fwnode))
4131 count = acpi_gpio_count(dev, con_id);
4132 else if (is_software_node(fwnode))
4133 count = swnode_gpio_count(fwnode, con_id);
4136 count = platform_gpio_count(dev, con_id);
4140 EXPORT_SYMBOL_GPL(gpiod_count);
4143 * gpiod_get - obtain a GPIO for a given GPIO function
4144 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4145 * @con_id: function within the GPIO consumer
4146 * @flags: optional GPIO initialization flags
4148 * Return the GPIO descriptor corresponding to the function con_id of device
4149 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4150 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4152 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4153 enum gpiod_flags flags)
4155 return gpiod_get_index(dev, con_id, 0, flags);
4157 EXPORT_SYMBOL_GPL(gpiod_get);
4160 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4161 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4162 * @con_id: function within the GPIO consumer
4163 * @flags: optional GPIO initialization flags
4165 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4166 * the requested function it will return NULL. This is convenient for drivers
4167 * that need to handle optional GPIOs.
4169 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4171 enum gpiod_flags flags)
4173 return gpiod_get_index_optional(dev, con_id, 0, flags);
4175 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4179 * gpiod_configure_flags - helper function to configure a given GPIO
4180 * @desc: gpio whose value will be assigned
4181 * @con_id: function within the GPIO consumer
4182 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4183 * of_find_gpio() or of_get_gpio_hog()
4184 * @dflags: gpiod_flags - optional GPIO initialization flags
4186 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4187 * requested function and/or index, or another IS_ERR() code if an error
4188 * occurred while trying to acquire the GPIO.
4190 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4191 unsigned long lflags, enum gpiod_flags dflags)
4195 if (lflags & GPIO_ACTIVE_LOW)
4196 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4198 if (lflags & GPIO_OPEN_DRAIN)
4199 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4200 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4202 * This enforces open drain mode from the consumer side.
4203 * This is necessary for some busses like I2C, but the lookup
4204 * should *REALLY* have specified them as open drain in the
4205 * first place, so print a little warning here.
4207 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4209 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4212 if (lflags & GPIO_OPEN_SOURCE)
4213 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4215 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
4216 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
4217 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
4219 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4223 if (lflags & GPIO_PULL_UP)
4224 set_bit(FLAG_PULL_UP, &desc->flags);
4225 else if (lflags & GPIO_PULL_DOWN)
4226 set_bit(FLAG_PULL_DOWN, &desc->flags);
4227 else if (lflags & GPIO_PULL_DISABLE)
4228 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
4230 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4234 /* No particular flag request, return here... */
4235 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4236 gpiod_dbg(desc, "no flags found for %s\n", con_id);
4241 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4242 ret = gpiod_direction_output(desc,
4243 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4245 ret = gpiod_direction_input(desc);
4251 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4252 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4253 * @con_id: function within the GPIO consumer
4254 * @idx: index of the GPIO to obtain in the consumer
4255 * @flags: optional GPIO initialization flags
4257 * This variant of gpiod_get() allows to access GPIOs other than the first
4258 * defined one for functions that define several GPIOs.
4260 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4261 * requested function and/or index, or another IS_ERR() code if an error
4262 * occurred while trying to acquire the GPIO.
4264 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4267 enum gpiod_flags flags)
4269 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4270 const char *devname = dev ? dev_name(dev) : "?";
4271 const char *label = con_id ?: devname;
4273 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true);
4275 EXPORT_SYMBOL_GPL(gpiod_get_index);
4278 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4280 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4281 * @con_id: function within the GPIO consumer
4282 * @index: index of the GPIO to obtain in the consumer
4283 * @flags: optional GPIO initialization flags
4285 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4286 * specified index was assigned to the requested function it will return NULL.
4287 * This is convenient for drivers that need to handle optional GPIOs.
4289 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4292 enum gpiod_flags flags)
4294 struct gpio_desc *desc;
4296 desc = gpiod_get_index(dev, con_id, index, flags);
4297 if (gpiod_not_found(desc))
4302 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4305 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4306 * @desc: gpio whose value will be assigned
4307 * @name: gpio line name
4308 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4309 * of_find_gpio() or of_get_gpio_hog()
4310 * @dflags: gpiod_flags - optional GPIO initialization flags
4312 int gpiod_hog(struct gpio_desc *desc, const char *name,
4313 unsigned long lflags, enum gpiod_flags dflags)
4315 struct gpio_chip *gc;
4316 struct gpio_desc *local_desc;
4320 gc = gpiod_to_chip(desc);
4321 hwnum = gpio_chip_hwgpio(desc);
4323 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4325 if (IS_ERR(local_desc)) {
4326 ret = PTR_ERR(local_desc);
4327 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4328 name, gc->label, hwnum, ret);
4332 /* Mark GPIO as hogged so it can be identified and removed later */
4333 set_bit(FLAG_IS_HOGGED, &desc->flags);
4335 gpiod_dbg(desc, "hogged as %s%s\n",
4336 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4337 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4338 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4344 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4345 * @gc: gpio chip to act on
4347 static void gpiochip_free_hogs(struct gpio_chip *gc)
4349 struct gpio_desc *desc;
4351 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
4352 gpiochip_free_own_desc(desc);
4356 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4357 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4358 * @con_id: function within the GPIO consumer
4359 * @flags: optional GPIO initialization flags
4361 * This function acquires all the GPIOs defined under a given function.
4363 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4364 * no GPIO has been assigned to the requested function, or another IS_ERR()
4365 * code if an error occurred while trying to acquire the GPIOs.
4367 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4369 enum gpiod_flags flags)
4371 struct gpio_desc *desc;
4372 struct gpio_descs *descs;
4373 struct gpio_array *array_info = NULL;
4374 struct gpio_chip *gc;
4375 int count, bitmap_size;
4378 count = gpiod_count(dev, con_id);
4380 return ERR_PTR(count);
4382 descs_size = struct_size(descs, desc, count);
4383 descs = kzalloc(descs_size, GFP_KERNEL);
4385 return ERR_PTR(-ENOMEM);
4387 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) {
4388 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4390 gpiod_put_array(descs);
4391 return ERR_CAST(desc);
4394 descs->desc[descs->ndescs] = desc;
4396 gc = gpiod_to_chip(desc);
4398 * If pin hardware number of array member 0 is also 0, select
4399 * its chip as a candidate for fast bitmap processing path.
4401 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4402 struct gpio_descs *array;
4404 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4407 array = krealloc(descs, descs_size +
4408 struct_size(array_info, invert_mask, 3 * bitmap_size),
4409 GFP_KERNEL | __GFP_ZERO);
4411 gpiod_put_array(descs);
4412 return ERR_PTR(-ENOMEM);
4417 array_info = (void *)descs + descs_size;
4418 array_info->get_mask = array_info->invert_mask +
4420 array_info->set_mask = array_info->get_mask +
4423 array_info->desc = descs->desc;
4424 array_info->size = count;
4425 array_info->chip = gc;
4426 bitmap_set(array_info->get_mask, descs->ndescs,
4427 count - descs->ndescs);
4428 bitmap_set(array_info->set_mask, descs->ndescs,
4429 count - descs->ndescs);
4430 descs->info = array_info;
4433 /* If there is no cache for fast bitmap processing path, continue */
4437 /* Unmark array members which don't belong to the 'fast' chip */
4438 if (array_info->chip != gc) {
4439 __clear_bit(descs->ndescs, array_info->get_mask);
4440 __clear_bit(descs->ndescs, array_info->set_mask);
4443 * Detect array members which belong to the 'fast' chip
4444 * but their pins are not in hardware order.
4446 else if (gpio_chip_hwgpio(desc) != descs->ndescs) {
4448 * Don't use fast path if all array members processed so
4449 * far belong to the same chip as this one but its pin
4450 * hardware number is different from its array index.
4452 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4455 __clear_bit(descs->ndescs,
4456 array_info->get_mask);
4457 __clear_bit(descs->ndescs,
4458 array_info->set_mask);
4461 /* Exclude open drain or open source from fast output */
4462 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4463 gpiochip_line_is_open_source(gc, descs->ndescs))
4464 __clear_bit(descs->ndescs,
4465 array_info->set_mask);
4466 /* Identify 'fast' pins which require invertion */
4467 if (gpiod_is_active_low(desc))
4468 __set_bit(descs->ndescs,
4469 array_info->invert_mask);
4474 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4475 array_info->chip->label, array_info->size,
4476 *array_info->get_mask, *array_info->set_mask,
4477 *array_info->invert_mask);
4480 EXPORT_SYMBOL_GPL(gpiod_get_array);
4483 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4485 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4486 * @con_id: function within the GPIO consumer
4487 * @flags: optional GPIO initialization flags
4489 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4490 * assigned to the requested function it will return NULL.
4492 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4494 enum gpiod_flags flags)
4496 struct gpio_descs *descs;
4498 descs = gpiod_get_array(dev, con_id, flags);
4499 if (gpiod_not_found(descs))
4504 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4507 * gpiod_put - dispose of a GPIO descriptor
4508 * @desc: GPIO descriptor to dispose of
4510 * No descriptor can be used after gpiod_put() has been called on it.
4512 void gpiod_put(struct gpio_desc *desc)
4517 EXPORT_SYMBOL_GPL(gpiod_put);
4520 * gpiod_put_array - dispose of multiple GPIO descriptors
4521 * @descs: struct gpio_descs containing an array of descriptors
4523 void gpiod_put_array(struct gpio_descs *descs)
4527 for (i = 0; i < descs->ndescs; i++)
4528 gpiod_put(descs->desc[i]);
4532 EXPORT_SYMBOL_GPL(gpiod_put_array);
4534 static int gpio_stub_drv_probe(struct device *dev)
4537 * The DT node of some GPIO chips have a "compatible" property, but
4538 * never have a struct device added and probed by a driver to register
4539 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4540 * the consumers of the GPIO chip to get probe deferred forever because
4541 * they will be waiting for a device associated with the GPIO chip
4542 * firmware node to get added and bound to a driver.
4544 * To allow these consumers to probe, we associate the struct
4545 * gpio_device of the GPIO chip with the firmware node and then simply
4546 * bind it to this stub driver.
4551 static struct device_driver gpio_stub_drv = {
4552 .name = "gpio_stub_drv",
4553 .bus = &gpio_bus_type,
4554 .probe = gpio_stub_drv_probe,
4557 static int __init gpiolib_dev_init(void)
4561 /* Register GPIO sysfs bus */
4562 ret = bus_register(&gpio_bus_type);
4564 pr_err("gpiolib: could not register GPIO bus type\n");
4568 ret = driver_register(&gpio_stub_drv);
4570 pr_err("gpiolib: could not register GPIO stub driver\n");
4571 bus_unregister(&gpio_bus_type);
4575 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4577 pr_err("gpiolib: failed to allocate char dev region\n");
4578 driver_unregister(&gpio_stub_drv);
4579 bus_unregister(&gpio_bus_type);
4583 gpiolib_initialized = true;
4584 gpiochip_setup_devs();
4586 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4587 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4588 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4592 core_initcall(gpiolib_dev_init);
4594 #ifdef CONFIG_DEBUG_FS
4596 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4598 struct gpio_chip *gc = gdev->chip;
4599 struct gpio_desc *desc;
4600 unsigned gpio = gdev->base;
4606 for_each_gpio_desc(gc, desc) {
4607 if (test_bit(FLAG_REQUESTED, &desc->flags)) {
4608 gpiod_get_direction(desc);
4609 is_out = test_bit(FLAG_IS_OUT, &desc->flags);
4610 value = gpio_chip_get_value(gc, desc);
4611 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
4612 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4613 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
4614 gpio, desc->name ?: "", desc->label,
4615 is_out ? "out" : "in ",
4616 value >= 0 ? (value ? "hi" : "lo") : "? ",
4617 is_irq ? "IRQ " : "",
4618 active_low ? "ACTIVE LOW" : "");
4619 } else if (desc->name) {
4620 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
4627 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4629 unsigned long flags;
4630 struct gpio_device *gdev = NULL;
4631 loff_t index = *pos;
4635 spin_lock_irqsave(&gpio_lock, flags);
4636 list_for_each_entry(gdev, &gpio_devices, list)
4638 spin_unlock_irqrestore(&gpio_lock, flags);
4641 spin_unlock_irqrestore(&gpio_lock, flags);
4646 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4648 unsigned long flags;
4649 struct gpio_device *gdev = v;
4652 spin_lock_irqsave(&gpio_lock, flags);
4653 if (list_is_last(&gdev->list, &gpio_devices))
4656 ret = list_first_entry(&gdev->list, struct gpio_device, list);
4657 spin_unlock_irqrestore(&gpio_lock, flags);
4665 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4669 static int gpiolib_seq_show(struct seq_file *s, void *v)
4671 struct gpio_device *gdev = v;
4672 struct gpio_chip *gc = gdev->chip;
4673 struct device *parent;
4676 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4677 dev_name(&gdev->dev));
4681 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4682 dev_name(&gdev->dev),
4683 gdev->base, gdev->base + gdev->ngpio - 1);
4684 parent = gc->parent;
4686 seq_printf(s, ", parent: %s/%s",
4687 parent->bus ? parent->bus->name : "no-bus",
4690 seq_printf(s, ", %s", gc->label);
4692 seq_printf(s, ", can sleep");
4693 seq_printf(s, ":\n");
4696 gc->dbg_show(s, gc);
4698 gpiolib_dbg_show(s, gdev);
4703 static const struct seq_operations gpiolib_sops = {
4704 .start = gpiolib_seq_start,
4705 .next = gpiolib_seq_next,
4706 .stop = gpiolib_seq_stop,
4707 .show = gpiolib_seq_show,
4709 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4711 static int __init gpiolib_debugfs_init(void)
4713 /* /sys/kernel/debug/gpio */
4714 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4717 subsys_initcall(gpiolib_debugfs_init);
4719 #endif /* DEBUG_FS */