1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitmap.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/interrupt.h>
8 #include <linux/spinlock.h>
9 #include <linux/list.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/debugfs.h>
13 #include <linux/seq_file.h>
14 #include <linux/gpio.h>
15 #include <linux/idr.h>
16 #include <linux/slab.h>
17 #include <linux/acpi.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/gpio/machine.h>
20 #include <linux/pinctrl/consumer.h>
22 #include <linux/compat.h>
23 #include <linux/file.h>
24 #include <uapi/linux/gpio.h>
27 #include "gpiolib-of.h"
28 #include "gpiolib-acpi.h"
29 #include "gpiolib-cdev.h"
30 #include "gpiolib-sysfs.h"
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/gpio.h>
35 /* Implementation infrastructure for GPIO interfaces.
37 * The GPIO programming interface allows for inlining speed-critical
38 * get/set operations for common cases, so that access to SOC-integrated
39 * GPIOs can sometimes cost only an instruction or two per bit.
43 /* When debugging, extend minimal trust to callers and platform code.
44 * Also emit diagnostic messages that may help initial bringup, when
45 * board setup or driver bugs are most common.
47 * Otherwise, minimize overhead in what may be bitbanging codepaths.
50 #define extra_checks 1
52 #define extra_checks 0
55 #define dont_test_bit(b,d) (0)
57 /* Device and char device-related information */
58 static DEFINE_IDA(gpio_ida);
59 static dev_t gpio_devt;
60 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
61 static int gpio_bus_match(struct device *dev, struct device_driver *drv);
62 static struct bus_type gpio_bus_type = {
64 .match = gpio_bus_match,
68 * Number of GPIOs to use for the fast path in set array
70 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
72 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
73 * While any GPIO is requested, its gpio_chip is not removable;
74 * each GPIO's "requested" flag serves as a lock and refcount.
76 DEFINE_SPINLOCK(gpio_lock);
78 static DEFINE_MUTEX(gpio_lookup_lock);
79 static LIST_HEAD(gpio_lookup_list);
80 LIST_HEAD(gpio_devices);
82 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
83 static LIST_HEAD(gpio_machine_hogs);
85 static void gpiochip_free_hogs(struct gpio_chip *gc);
86 static int gpiochip_add_irqchip(struct gpio_chip *gc,
87 struct lock_class_key *lock_key,
88 struct lock_class_key *request_key);
89 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
90 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
91 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
92 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
94 static bool gpiolib_initialized;
96 static inline void desc_set_label(struct gpio_desc *d, const char *label)
102 * gpio_to_desc - Convert a GPIO number to its descriptor
103 * @gpio: global GPIO number
106 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
107 * with the given number exists in the system.
109 struct gpio_desc *gpio_to_desc(unsigned gpio)
111 struct gpio_device *gdev;
114 spin_lock_irqsave(&gpio_lock, flags);
116 list_for_each_entry(gdev, &gpio_devices, list) {
117 if (gdev->base <= gpio &&
118 gdev->base + gdev->ngpio > gpio) {
119 spin_unlock_irqrestore(&gpio_lock, flags);
120 return &gdev->descs[gpio - gdev->base];
124 spin_unlock_irqrestore(&gpio_lock, flags);
126 if (!gpio_is_valid(gpio))
127 pr_warn("invalid GPIO %d\n", gpio);
131 EXPORT_SYMBOL_GPL(gpio_to_desc);
134 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
135 * hardware number for this chip
137 * @hwnum: hardware number of the GPIO for this chip
140 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
141 * in the given chip for the specified hardware number.
143 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
146 struct gpio_device *gdev = gc->gpiodev;
148 if (hwnum >= gdev->ngpio)
149 return ERR_PTR(-EINVAL);
151 return &gdev->descs[hwnum];
153 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
156 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
157 * @desc: GPIO descriptor
159 * This should disappear in the future but is needed since we still
160 * use GPIO numbers for error messages and sysfs nodes.
163 * The global GPIO number for the GPIO specified by its descriptor.
165 int desc_to_gpio(const struct gpio_desc *desc)
167 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
169 EXPORT_SYMBOL_GPL(desc_to_gpio);
173 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
174 * @desc: descriptor to return the chip of
176 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
178 if (!desc || !desc->gdev)
180 return desc->gdev->chip;
182 EXPORT_SYMBOL_GPL(gpiod_to_chip);
184 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
185 static int gpiochip_find_base(int ngpio)
187 struct gpio_device *gdev;
188 int base = ARCH_NR_GPIOS - ngpio;
190 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
191 /* found a free space? */
192 if (gdev->base + gdev->ngpio <= base)
194 /* nope, check the space right before the chip */
195 base = gdev->base - ngpio;
198 if (gpio_is_valid(base)) {
199 pr_debug("%s: found new base at %d\n", __func__, base);
202 pr_err("%s: cannot find free range\n", __func__);
208 * gpiod_get_direction - return the current direction of a GPIO
209 * @desc: GPIO to get the direction of
211 * Returns 0 for output, 1 for input, or an error code in case of error.
213 * This function may sleep if gpiod_cansleep() is true.
215 int gpiod_get_direction(struct gpio_desc *desc)
217 struct gpio_chip *gc;
221 gc = gpiod_to_chip(desc);
222 offset = gpio_chip_hwgpio(desc);
225 * Open drain emulation using input mode may incorrectly report
226 * input here, fix that up.
228 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
229 test_bit(FLAG_IS_OUT, &desc->flags))
232 if (!gc->get_direction)
235 ret = gc->get_direction(gc, offset);
239 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
243 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
247 EXPORT_SYMBOL_GPL(gpiod_get_direction);
250 * Add a new chip to the global chips list, keeping the list of chips sorted
251 * by range(means [base, base + ngpio - 1]) order.
253 * Return -EBUSY if the new chip overlaps with some other chip's integer
256 static int gpiodev_add_to_list(struct gpio_device *gdev)
258 struct gpio_device *prev, *next;
260 if (list_empty(&gpio_devices)) {
261 /* initial entry in list */
262 list_add_tail(&gdev->list, &gpio_devices);
266 next = list_entry(gpio_devices.next, struct gpio_device, list);
267 if (gdev->base + gdev->ngpio <= next->base) {
268 /* add before first entry */
269 list_add(&gdev->list, &gpio_devices);
273 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
274 if (prev->base + prev->ngpio <= gdev->base) {
275 /* add behind last entry */
276 list_add_tail(&gdev->list, &gpio_devices);
280 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
281 /* at the end of the list */
282 if (&next->list == &gpio_devices)
285 /* add between prev and next */
286 if (prev->base + prev->ngpio <= gdev->base
287 && gdev->base + gdev->ngpio <= next->base) {
288 list_add(&gdev->list, &prev->list);
293 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
298 * Convert a GPIO name to its descriptor
299 * Note that there is no guarantee that GPIO names are globally unique!
300 * Hence this function will return, if it exists, a reference to the first GPIO
301 * line found that matches the given name.
303 static struct gpio_desc *gpio_name_to_desc(const char * const name)
305 struct gpio_device *gdev;
311 spin_lock_irqsave(&gpio_lock, flags);
313 list_for_each_entry(gdev, &gpio_devices, list) {
316 for (i = 0; i != gdev->ngpio; ++i) {
317 struct gpio_desc *desc = &gdev->descs[i];
322 if (!strcmp(desc->name, name)) {
323 spin_unlock_irqrestore(&gpio_lock, flags);
329 spin_unlock_irqrestore(&gpio_lock, flags);
335 * Take the names from gc->names and assign them to their GPIO descriptors.
336 * Warn if a name is already used for a GPIO line on a different GPIO chip.
339 * 1. Non-unique names are still accepted,
340 * 2. Name collisions within the same GPIO chip are not reported.
342 static int gpiochip_set_desc_names(struct gpio_chip *gc)
344 struct gpio_device *gdev = gc->gpiodev;
347 /* First check all names if they are unique */
348 for (i = 0; i != gc->ngpio; ++i) {
349 struct gpio_desc *gpio;
351 gpio = gpio_name_to_desc(gc->names[i]);
354 "Detected name collision for GPIO name '%s'\n",
358 /* Then add all names to the GPIO descriptors */
359 for (i = 0; i != gc->ngpio; ++i)
360 gdev->descs[i].name = gc->names[i];
366 * devprop_gpiochip_set_names - Set GPIO line names using device properties
367 * @chip: GPIO chip whose lines should be named, if possible
369 * Looks for device property "gpio-line-names" and if it exists assigns
370 * GPIO line names for the chip. The memory allocated for the assigned
371 * names belong to the underlying firmware node and should not be released
374 static int devprop_gpiochip_set_names(struct gpio_chip *chip)
376 struct gpio_device *gdev = chip->gpiodev;
377 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
382 count = fwnode_property_string_array_count(fwnode, "gpio-line-names");
387 * When offset is set in the driver side we assume the driver internally
388 * is using more than one gpiochip per the same device. We have to stop
389 * setting friendly names if the specified ones with 'gpio-line-names'
390 * are less than the offset in the device itself. This means all the
391 * lines are not present for every single pin within all the internal
394 if (count <= chip->offset) {
395 dev_warn(&gdev->dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
396 count, chip->offset);
400 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
404 ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
407 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
413 * When more that one gpiochip per device is used, 'count' can
414 * contain at most number gpiochips x chip->ngpio. We have to
415 * correctly distribute all defined lines taking into account
416 * chip->offset as starting point from where we will assign
417 * the names to pins from the 'names' array. Since property
418 * 'gpio-line-names' cannot contains gaps, we have to be sure
419 * we only assign those pins that really exists since chip->ngpio
420 * can be different of the chip->offset.
422 count = (count > chip->offset) ? count - chip->offset : count;
423 if (count > chip->ngpio)
426 for (i = 0; i < count; i++)
427 gdev->descs[i].name = names[chip->offset + i];
434 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
438 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
442 /* Assume by default all GPIOs are valid */
443 bitmap_fill(p, gc->ngpio);
448 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
450 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
453 gc->valid_mask = gpiochip_allocate_mask(gc);
460 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
462 if (gc->init_valid_mask)
463 return gc->init_valid_mask(gc,
470 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
472 bitmap_free(gc->valid_mask);
473 gc->valid_mask = NULL;
476 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
478 if (gc->add_pin_ranges)
479 return gc->add_pin_ranges(gc);
484 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
487 /* No mask means all valid */
488 if (likely(!gc->valid_mask))
490 return test_bit(offset, gc->valid_mask);
492 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
494 static void gpiodevice_release(struct device *dev)
496 struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
499 spin_lock_irqsave(&gpio_lock, flags);
500 list_del(&gdev->list);
501 spin_unlock_irqrestore(&gpio_lock, flags);
503 ida_free(&gpio_ida, gdev->id);
504 kfree_const(gdev->label);
509 #ifdef CONFIG_GPIO_CDEV
510 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
511 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
514 * gpiolib_cdev_register() indirectly calls device_add(), which is still
515 * required even when cdev is not selected.
517 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
518 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
521 static int gpiochip_setup_dev(struct gpio_device *gdev)
525 ret = gcdev_register(gdev, gpio_devt);
529 /* From this point, the .release() function cleans up gpio_device */
530 gdev->dev.release = gpiodevice_release;
532 ret = gpiochip_sysfs_register(gdev);
534 goto err_remove_device;
536 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
537 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
542 gcdev_unregister(gdev);
546 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
548 struct gpio_desc *desc;
551 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
553 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
558 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
561 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
563 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
564 __func__, gc->label, hog->chip_hwnum, rv);
567 static void machine_gpiochip_add(struct gpio_chip *gc)
569 struct gpiod_hog *hog;
571 mutex_lock(&gpio_machine_hogs_mutex);
573 list_for_each_entry(hog, &gpio_machine_hogs, list) {
574 if (!strcmp(gc->label, hog->chip_label))
575 gpiochip_machine_hog(gc, hog);
578 mutex_unlock(&gpio_machine_hogs_mutex);
581 static void gpiochip_setup_devs(void)
583 struct gpio_device *gdev;
586 list_for_each_entry(gdev, &gpio_devices, list) {
587 ret = gpiochip_setup_dev(gdev);
590 "Failed to initialize gpio device (%d)\n", ret);
594 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
595 struct lock_class_key *lock_key,
596 struct lock_class_key *request_key)
598 struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
599 struct gpio_device *gdev;
607 * First: allocate and populate the internal stat container, and
608 * set up the struct device.
610 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
613 gdev->dev.bus = &gpio_bus_type;
614 gdev->dev.parent = gc->parent;
618 of_gpio_dev_init(gc, gdev);
619 acpi_gpio_dev_init(gc, gdev);
622 * Assign fwnode depending on the result of the previous calls,
623 * if none of them succeed, assign it to the parent's one.
625 gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode;
627 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
633 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
637 device_initialize(&gdev->dev);
638 if (gc->parent && gc->parent->driver)
639 gdev->owner = gc->parent->driver->owner;
641 /* TODO: remove chip->owner */
642 gdev->owner = gc->owner;
644 gdev->owner = THIS_MODULE;
647 * Try the device properties if the driver didn't supply the number
652 ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
655 * -ENODATA means that there is no property found and
656 * we want to issue the error message to the user.
657 * Besides that, we want to return different error code
658 * to state that supplied value is not valid.
662 goto err_free_dev_name;
667 if (gc->ngpio == 0) {
668 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
670 goto err_free_dev_name;
673 if (gc->ngpio > FASTPATH_NGPIO)
674 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
675 gc->ngpio, FASTPATH_NGPIO);
677 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
680 goto err_free_dev_name;
683 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
689 gdev->ngpio = gc->ngpio;
692 spin_lock_irqsave(&gpio_lock, flags);
695 * TODO: this allocates a Linux GPIO number base in the global
696 * GPIO numberspace for this chip. In the long run we want to
697 * get *rid* of this numberspace and use only descriptors, but
698 * it may be a pipe dream. It will not happen before we get rid
699 * of the sysfs interface anyways.
703 base = gpiochip_find_base(gc->ngpio);
705 spin_unlock_irqrestore(&gpio_lock, flags);
711 * TODO: it should not be necessary to reflect the assigned
712 * base outside of the GPIO subsystem. Go over drivers and
713 * see if anyone makes use of this, else drop this and assign
720 ret = gpiodev_add_to_list(gdev);
722 spin_unlock_irqrestore(&gpio_lock, flags);
726 for (i = 0; i < gc->ngpio; i++)
727 gdev->descs[i].gdev = gdev;
729 spin_unlock_irqrestore(&gpio_lock, flags);
731 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
732 init_rwsem(&gdev->sem);
734 #ifdef CONFIG_PINCTRL
735 INIT_LIST_HEAD(&gdev->pin_ranges);
739 ret = gpiochip_set_desc_names(gc);
741 ret = devprop_gpiochip_set_names(gc);
743 goto err_remove_from_list;
745 ret = gpiochip_alloc_valid_mask(gc);
747 goto err_remove_from_list;
749 ret = of_gpiochip_add(gc);
751 goto err_free_gpiochip_mask;
753 ret = gpiochip_init_valid_mask(gc);
755 goto err_remove_of_chip;
757 for (i = 0; i < gc->ngpio; i++) {
758 struct gpio_desc *desc = &gdev->descs[i];
760 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
761 assign_bit(FLAG_IS_OUT,
762 &desc->flags, !gc->get_direction(gc, i));
764 assign_bit(FLAG_IS_OUT,
765 &desc->flags, !gc->direction_input);
769 ret = gpiochip_add_pin_ranges(gc);
771 goto err_remove_of_chip;
773 acpi_gpiochip_add(gc);
775 machine_gpiochip_add(gc);
777 ret = gpiochip_irqchip_init_valid_mask(gc);
779 goto err_remove_acpi_chip;
781 ret = gpiochip_irqchip_init_hw(gc);
783 goto err_remove_acpi_chip;
785 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
787 goto err_remove_irqchip_mask;
790 * By first adding the chardev, and then adding the device,
791 * we get a device node entry in sysfs under
792 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
793 * coldplug of device nodes and other udev business.
794 * We can do this only if gpiolib has been initialized.
795 * Otherwise, defer until later.
797 if (gpiolib_initialized) {
798 ret = gpiochip_setup_dev(gdev);
800 goto err_remove_irqchip;
805 gpiochip_irqchip_remove(gc);
806 err_remove_irqchip_mask:
807 gpiochip_irqchip_free_valid_mask(gc);
808 err_remove_acpi_chip:
809 acpi_gpiochip_remove(gc);
811 gpiochip_free_hogs(gc);
812 of_gpiochip_remove(gc);
813 err_free_gpiochip_mask:
814 gpiochip_remove_pin_ranges(gc);
815 gpiochip_free_valid_mask(gc);
816 if (gdev->dev.release) {
817 /* release() has been registered by gpiochip_setup_dev() */
818 put_device(&gdev->dev);
819 goto err_print_message;
821 err_remove_from_list:
822 spin_lock_irqsave(&gpio_lock, flags);
823 list_del(&gdev->list);
824 spin_unlock_irqrestore(&gpio_lock, flags);
826 kfree_const(gdev->label);
830 kfree(dev_name(&gdev->dev));
832 ida_free(&gpio_ida, gdev->id);
836 /* failures here can mean systems won't boot... */
837 if (ret != -EPROBE_DEFER) {
838 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
839 base, base + (int)ngpios - 1,
840 gc->label ? : "generic", ret);
844 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
847 * gpiochip_get_data() - get per-subdriver data for the chip
851 * The per-subdriver data for the chip.
853 void *gpiochip_get_data(struct gpio_chip *gc)
855 return gc->gpiodev->data;
857 EXPORT_SYMBOL_GPL(gpiochip_get_data);
860 * gpiochip_remove() - unregister a gpio_chip
861 * @gc: the chip to unregister
863 * A gpio_chip with any GPIOs still requested may not be removed.
865 void gpiochip_remove(struct gpio_chip *gc)
867 struct gpio_device *gdev = gc->gpiodev;
871 down_write(&gdev->sem);
873 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
874 gpiochip_sysfs_unregister(gdev);
875 gpiochip_free_hogs(gc);
876 /* Numb the device, cancelling all outstanding operations */
878 gpiochip_irqchip_remove(gc);
879 acpi_gpiochip_remove(gc);
880 of_gpiochip_remove(gc);
881 gpiochip_remove_pin_ranges(gc);
882 gpiochip_free_valid_mask(gc);
884 * We accept no more calls into the driver from this point, so
885 * NULL the driver data pointer
889 spin_lock_irqsave(&gpio_lock, flags);
890 for (i = 0; i < gdev->ngpio; i++) {
891 if (gpiochip_is_requested(gc, i))
894 spin_unlock_irqrestore(&gpio_lock, flags);
896 if (i != gdev->ngpio)
898 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
901 * The gpiochip side puts its use of the device to rest here:
902 * if there are no userspace clients, the chardev and device will
903 * be removed, else it will be dangling until the last user is
906 gcdev_unregister(gdev);
907 up_write(&gdev->sem);
908 put_device(&gdev->dev);
910 EXPORT_SYMBOL_GPL(gpiochip_remove);
913 * gpiochip_find() - iterator for locating a specific gpio_chip
914 * @data: data to pass to match function
915 * @match: Callback function to check gpio_chip
917 * Similar to bus_find_device. It returns a reference to a gpio_chip as
918 * determined by a user supplied @match callback. The callback should return
919 * 0 if the device doesn't match and non-zero if it does. If the callback is
920 * non-zero, this function will return to the caller and not iterate over any
923 struct gpio_chip *gpiochip_find(void *data,
924 int (*match)(struct gpio_chip *gc,
927 struct gpio_device *gdev;
928 struct gpio_chip *gc = NULL;
931 spin_lock_irqsave(&gpio_lock, flags);
932 list_for_each_entry(gdev, &gpio_devices, list)
933 if (gdev->chip && match(gdev->chip, data)) {
938 spin_unlock_irqrestore(&gpio_lock, flags);
942 EXPORT_SYMBOL_GPL(gpiochip_find);
944 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
946 const char *name = data;
948 return !strcmp(gc->label, name);
951 static struct gpio_chip *find_chip_by_name(const char *name)
953 return gpiochip_find((void *)name, gpiochip_match_name);
956 #ifdef CONFIG_GPIOLIB_IRQCHIP
959 * The following is irqchip helper code for gpiochips.
962 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
964 struct gpio_irq_chip *girq = &gc->irq;
969 return girq->init_hw(gc);
972 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
974 struct gpio_irq_chip *girq = &gc->irq;
976 if (!girq->init_valid_mask)
979 girq->valid_mask = gpiochip_allocate_mask(gc);
980 if (!girq->valid_mask)
983 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
988 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
990 bitmap_free(gc->irq.valid_mask);
991 gc->irq.valid_mask = NULL;
994 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
997 if (!gpiochip_line_is_valid(gc, offset))
999 /* No mask means all valid */
1000 if (likely(!gc->irq.valid_mask))
1002 return test_bit(offset, gc->irq.valid_mask);
1004 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1006 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1009 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1011 * @gc: the gpiochip to set the irqchip hierarchical handler to
1012 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1013 * will then percolate up to the parent
1015 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1016 struct irq_chip *irqchip)
1018 /* DT will deal with mapping each IRQ as we go along */
1019 if (is_of_node(gc->irq.fwnode))
1023 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1024 * irqs upfront instead of dynamically since we don't have the
1025 * dynamic type of allocation that hardware description languages
1026 * provide. Once all GPIO drivers using board files are gone from
1027 * the kernel we can delete this code, but for a transitional period
1028 * it is necessary to keep this around.
1030 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1034 for (i = 0; i < gc->ngpio; i++) {
1035 struct irq_fwspec fwspec;
1036 unsigned int parent_hwirq;
1037 unsigned int parent_type;
1038 struct gpio_irq_chip *girq = &gc->irq;
1041 * We call the child to parent translation function
1042 * only to check if the child IRQ is valid or not.
1043 * Just pick the rising edge type here as that is what
1044 * we likely need to support.
1046 ret = girq->child_to_parent_hwirq(gc, i,
1047 IRQ_TYPE_EDGE_RISING,
1051 chip_err(gc, "skip set-up on hwirq %d\n",
1056 fwspec.fwnode = gc->irq.fwnode;
1057 /* This is the hwirq for the GPIO line side of things */
1058 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1059 /* Just pick something */
1060 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1061 fwspec.param_count = 2;
1062 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1063 /* just pick something */
1072 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1079 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1084 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1085 struct irq_fwspec *fwspec,
1086 unsigned long *hwirq,
1089 /* We support standard DT translation */
1090 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1091 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1094 /* This is for board files and others not using DT */
1095 if (is_fwnode_irqchip(fwspec->fwnode)) {
1098 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1101 WARN_ON(*type == IRQ_TYPE_NONE);
1107 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1109 unsigned int nr_irqs,
1112 struct gpio_chip *gc = d->host_data;
1113 irq_hw_number_t hwirq;
1114 unsigned int type = IRQ_TYPE_NONE;
1115 struct irq_fwspec *fwspec = data;
1117 unsigned int parent_hwirq;
1118 unsigned int parent_type;
1119 struct gpio_irq_chip *girq = &gc->irq;
1123 * The nr_irqs parameter is always one except for PCI multi-MSI
1124 * so this should not happen.
1126 WARN_ON(nr_irqs != 1);
1128 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1132 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1134 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1135 &parent_hwirq, &parent_type);
1137 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1140 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1143 * We set handle_bad_irq because the .set_type() should
1144 * always be invoked and set the right type of handler.
1146 irq_domain_set_info(d,
1155 /* This parent only handles asserted level IRQs */
1156 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1160 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1162 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1163 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1165 * If the parent irqdomain is msi, the interrupts have already
1166 * been allocated, so the EEXIST is good.
1168 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1172 "failed to allocate parent hwirq %d for hwirq %lu\n",
1173 parent_hwirq, hwirq);
1179 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1180 unsigned int offset)
1185 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1187 ops->activate = gpiochip_irq_domain_activate;
1188 ops->deactivate = gpiochip_irq_domain_deactivate;
1189 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1190 ops->free = irq_domain_free_irqs_common;
1193 * We only allow overriding the translate() function for
1194 * hierarchical chips, and this should only be done if the user
1195 * really need something other than 1:1 translation.
1197 if (!ops->translate)
1198 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1201 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1203 if (!gc->irq.child_to_parent_hwirq ||
1205 chip_err(gc, "missing irqdomain vital data\n");
1209 if (!gc->irq.child_offset_to_irq)
1210 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1212 if (!gc->irq.populate_parent_alloc_arg)
1213 gc->irq.populate_parent_alloc_arg =
1214 gpiochip_populate_parent_fwspec_twocell;
1216 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1218 gc->irq.domain = irq_domain_create_hierarchy(
1219 gc->irq.parent_domain,
1223 &gc->irq.child_irq_domain_ops,
1226 if (!gc->irq.domain)
1229 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1234 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1236 return !!gc->irq.parent_domain;
1239 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1240 unsigned int parent_hwirq,
1241 unsigned int parent_type)
1243 struct irq_fwspec *fwspec;
1245 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1249 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1250 fwspec->param_count = 2;
1251 fwspec->param[0] = parent_hwirq;
1252 fwspec->param[1] = parent_type;
1256 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1258 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1259 unsigned int parent_hwirq,
1260 unsigned int parent_type)
1262 struct irq_fwspec *fwspec;
1264 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1268 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1269 fwspec->param_count = 4;
1270 fwspec->param[0] = 0;
1271 fwspec->param[1] = parent_hwirq;
1272 fwspec->param[2] = 0;
1273 fwspec->param[3] = parent_type;
1277 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1281 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1286 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1291 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1294 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1295 * @d: the irqdomain used by this irqchip
1296 * @irq: the global irq number used by this GPIO irqchip irq
1297 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1299 * This function will set up the mapping for a certain IRQ line on a
1300 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1301 * stored inside the gpiochip.
1303 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1304 irq_hw_number_t hwirq)
1306 struct gpio_chip *gc = d->host_data;
1309 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1312 irq_set_chip_data(irq, gc);
1314 * This lock class tells lockdep that GPIO irqs are in a different
1315 * category than their parents, so it won't report false recursion.
1317 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1318 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1319 /* Chips that use nested thread handlers have them marked */
1320 if (gc->irq.threaded)
1321 irq_set_nested_thread(irq, 1);
1322 irq_set_noprobe(irq);
1324 if (gc->irq.num_parents == 1)
1325 ret = irq_set_parent(irq, gc->irq.parents[0]);
1326 else if (gc->irq.map)
1327 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1333 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1334 * is passed as default type.
1336 if (gc->irq.default_type != IRQ_TYPE_NONE)
1337 irq_set_irq_type(irq, gc->irq.default_type);
1341 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1343 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1345 struct gpio_chip *gc = d->host_data;
1347 if (gc->irq.threaded)
1348 irq_set_nested_thread(irq, 0);
1349 irq_set_chip_and_handler(irq, NULL, NULL);
1350 irq_set_chip_data(irq, NULL);
1352 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1354 static const struct irq_domain_ops gpiochip_domain_ops = {
1355 .map = gpiochip_irq_map,
1356 .unmap = gpiochip_irq_unmap,
1357 /* Virtually all GPIO irqchips are twocell:ed */
1358 .xlate = irq_domain_xlate_twocell,
1362 * TODO: move these activate/deactivate in under the hierarchicial
1363 * irqchip implementation as static once SPMI and SSBI (all external
1364 * users) are phased over.
1367 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1368 * @domain: The IRQ domain used by this IRQ chip
1369 * @data: Outermost irq_data associated with the IRQ
1370 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1372 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1373 * used as the activate function for the &struct irq_domain_ops. The host_data
1374 * for the IRQ domain must be the &struct gpio_chip.
1376 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1377 struct irq_data *data, bool reserve)
1379 struct gpio_chip *gc = domain->host_data;
1381 return gpiochip_lock_as_irq(gc, data->hwirq);
1383 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1386 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1387 * @domain: The IRQ domain used by this IRQ chip
1388 * @data: Outermost irq_data associated with the IRQ
1390 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1391 * be used as the deactivate function for the &struct irq_domain_ops. The
1392 * host_data for the IRQ domain must be the &struct gpio_chip.
1394 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1395 struct irq_data *data)
1397 struct gpio_chip *gc = domain->host_data;
1399 return gpiochip_unlock_as_irq(gc, data->hwirq);
1401 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1403 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1405 struct irq_domain *domain = gc->irq.domain;
1407 #ifdef CONFIG_GPIOLIB_IRQCHIP
1409 * Avoid race condition with other code, which tries to lookup
1410 * an IRQ before the irqchip has been properly registered,
1411 * i.e. while gpiochip is still being brought up.
1413 if (!gc->irq.initialized)
1414 return -EPROBE_DEFER;
1417 if (!gpiochip_irqchip_irq_valid(gc, offset))
1420 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1421 if (irq_domain_is_hierarchy(domain)) {
1422 struct irq_fwspec spec;
1424 spec.fwnode = domain->fwnode;
1425 spec.param_count = 2;
1426 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1427 spec.param[1] = IRQ_TYPE_NONE;
1429 return irq_create_fwspec_mapping(&spec);
1433 return irq_create_mapping(domain, offset);
1436 static int gpiochip_irq_reqres(struct irq_data *d)
1438 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1440 return gpiochip_reqres_irq(gc, d->hwirq);
1443 static void gpiochip_irq_relres(struct irq_data *d)
1445 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1447 gpiochip_relres_irq(gc, d->hwirq);
1450 static void gpiochip_irq_mask(struct irq_data *d)
1452 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1454 if (gc->irq.irq_mask)
1455 gc->irq.irq_mask(d);
1456 gpiochip_disable_irq(gc, d->hwirq);
1459 static void gpiochip_irq_unmask(struct irq_data *d)
1461 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1463 gpiochip_enable_irq(gc, d->hwirq);
1464 if (gc->irq.irq_unmask)
1465 gc->irq.irq_unmask(d);
1468 static void gpiochip_irq_enable(struct irq_data *d)
1470 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1472 gpiochip_enable_irq(gc, d->hwirq);
1473 gc->irq.irq_enable(d);
1476 static void gpiochip_irq_disable(struct irq_data *d)
1478 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1480 gc->irq.irq_disable(d);
1481 gpiochip_disable_irq(gc, d->hwirq);
1484 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1486 struct irq_chip *irqchip = gc->irq.chip;
1488 if (!irqchip->irq_request_resources &&
1489 !irqchip->irq_release_resources) {
1490 irqchip->irq_request_resources = gpiochip_irq_reqres;
1491 irqchip->irq_release_resources = gpiochip_irq_relres;
1493 if (WARN_ON(gc->irq.irq_enable))
1495 /* Check if the irqchip already has this hook... */
1496 if (irqchip->irq_enable == gpiochip_irq_enable ||
1497 irqchip->irq_mask == gpiochip_irq_mask) {
1499 * ...and if so, give a gentle warning that this is bad
1503 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1507 if (irqchip->irq_disable) {
1508 gc->irq.irq_disable = irqchip->irq_disable;
1509 irqchip->irq_disable = gpiochip_irq_disable;
1511 gc->irq.irq_mask = irqchip->irq_mask;
1512 irqchip->irq_mask = gpiochip_irq_mask;
1515 if (irqchip->irq_enable) {
1516 gc->irq.irq_enable = irqchip->irq_enable;
1517 irqchip->irq_enable = gpiochip_irq_enable;
1519 gc->irq.irq_unmask = irqchip->irq_unmask;
1520 irqchip->irq_unmask = gpiochip_irq_unmask;
1525 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1526 * @gc: the GPIO chip to add the IRQ chip to
1527 * @lock_key: lockdep class for IRQ lock
1528 * @request_key: lockdep class for IRQ request
1530 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1531 struct lock_class_key *lock_key,
1532 struct lock_class_key *request_key)
1534 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1535 struct irq_chip *irqchip = gc->irq.chip;
1542 if (gc->irq.parent_handler && gc->can_sleep) {
1543 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1547 type = gc->irq.default_type;
1550 * Specifying a default trigger is a terrible idea if DT or ACPI is
1551 * used to configure the interrupts, as you may end up with
1552 * conflicting triggers. Tell the user, and reset to NONE.
1554 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1555 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1556 type = IRQ_TYPE_NONE;
1559 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1561 gc->to_irq = gpiochip_to_irq;
1562 gc->irq.default_type = type;
1563 gc->irq.lock_key = lock_key;
1564 gc->irq.request_key = request_key;
1566 /* If a parent irqdomain is provided, let's build a hierarchy */
1567 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1568 int ret = gpiochip_hierarchy_add_domain(gc);
1572 /* Some drivers provide custom irqdomain ops */
1573 gc->irq.domain = irq_domain_create_simple(fwnode,
1576 gc->irq.domain_ops ?: &gpiochip_domain_ops,
1578 if (!gc->irq.domain)
1582 if (gc->irq.parent_handler) {
1583 void *data = gc->irq.parent_handler_data ?: gc;
1585 for (i = 0; i < gc->irq.num_parents; i++) {
1587 * The parent IRQ chip is already using the chip_data
1588 * for this IRQ chip, so our callbacks simply use the
1591 irq_set_chained_handler_and_data(gc->irq.parents[i],
1592 gc->irq.parent_handler,
1597 gpiochip_set_irq_hooks(gc);
1600 * Using barrier() here to prevent compiler from reordering
1601 * gc->irq.initialized before initialization of above
1602 * GPIO chip irq members.
1606 gc->irq.initialized = true;
1608 acpi_gpiochip_request_interrupts(gc);
1614 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1615 * @gc: the gpiochip to remove the irqchip from
1617 * This is called only from gpiochip_remove()
1619 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1621 struct irq_chip *irqchip = gc->irq.chip;
1622 unsigned int offset;
1624 acpi_gpiochip_free_interrupts(gc);
1626 if (irqchip && gc->irq.parent_handler) {
1627 struct gpio_irq_chip *irq = &gc->irq;
1630 for (i = 0; i < irq->num_parents; i++)
1631 irq_set_chained_handler_and_data(irq->parents[i],
1635 /* Remove all IRQ mappings and delete the domain */
1636 if (gc->irq.domain) {
1639 for (offset = 0; offset < gc->ngpio; offset++) {
1640 if (!gpiochip_irqchip_irq_valid(gc, offset))
1643 irq = irq_find_mapping(gc->irq.domain, offset);
1644 irq_dispose_mapping(irq);
1647 irq_domain_remove(gc->irq.domain);
1651 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1652 irqchip->irq_request_resources = NULL;
1653 irqchip->irq_release_resources = NULL;
1655 if (irqchip->irq_enable == gpiochip_irq_enable) {
1656 irqchip->irq_enable = gc->irq.irq_enable;
1657 irqchip->irq_disable = gc->irq.irq_disable;
1660 gc->irq.irq_enable = NULL;
1661 gc->irq.irq_disable = NULL;
1662 gc->irq.chip = NULL;
1664 gpiochip_irqchip_free_valid_mask(gc);
1668 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1669 * @gc: the gpiochip to add the irqchip to
1670 * @domain: the irqdomain to add to the gpiochip
1672 * This function adds an IRQ domain to the gpiochip.
1674 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1675 struct irq_domain *domain)
1680 gc->to_irq = gpiochip_to_irq;
1681 gc->irq.domain = domain;
1685 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1687 #else /* CONFIG_GPIOLIB_IRQCHIP */
1689 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1690 struct lock_class_key *lock_key,
1691 struct lock_class_key *request_key)
1695 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1697 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1702 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1706 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1709 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1712 * gpiochip_generic_request() - request the gpio function for a pin
1713 * @gc: the gpiochip owning the GPIO
1714 * @offset: the offset of the GPIO to request for GPIO function
1716 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
1718 #ifdef CONFIG_PINCTRL
1719 if (list_empty(&gc->gpiodev->pin_ranges))
1723 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1725 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1728 * gpiochip_generic_free() - free the gpio function from a pin
1729 * @gc: the gpiochip to request the gpio function for
1730 * @offset: the offset of the GPIO to free from GPIO function
1732 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
1734 #ifdef CONFIG_PINCTRL
1735 if (list_empty(&gc->gpiodev->pin_ranges))
1739 pinctrl_gpio_free(gc->gpiodev->base + offset);
1741 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1744 * gpiochip_generic_config() - apply configuration for a pin
1745 * @gc: the gpiochip owning the GPIO
1746 * @offset: the offset of the GPIO to apply the configuration
1747 * @config: the configuration to be applied
1749 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
1750 unsigned long config)
1752 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1754 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1756 #ifdef CONFIG_PINCTRL
1759 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1760 * @gc: the gpiochip to add the range for
1761 * @pctldev: the pin controller to map to
1762 * @gpio_offset: the start offset in the current gpio_chip number space
1763 * @pin_group: name of the pin group inside the pin controller
1765 * Calling this function directly from a DeviceTree-supported
1766 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1767 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1768 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1770 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1771 struct pinctrl_dev *pctldev,
1772 unsigned int gpio_offset, const char *pin_group)
1774 struct gpio_pin_range *pin_range;
1775 struct gpio_device *gdev = gc->gpiodev;
1778 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1780 chip_err(gc, "failed to allocate pin ranges\n");
1784 /* Use local offset as range ID */
1785 pin_range->range.id = gpio_offset;
1786 pin_range->range.gc = gc;
1787 pin_range->range.name = gc->label;
1788 pin_range->range.base = gdev->base + gpio_offset;
1789 pin_range->pctldev = pctldev;
1791 ret = pinctrl_get_group_pins(pctldev, pin_group,
1792 &pin_range->range.pins,
1793 &pin_range->range.npins);
1799 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1801 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1802 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1803 pinctrl_dev_get_devname(pctldev), pin_group);
1805 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1809 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1812 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1813 * @gc: the gpiochip to add the range for
1814 * @pinctl_name: the dev_name() of the pin controller to map to
1815 * @gpio_offset: the start offset in the current gpio_chip number space
1816 * @pin_offset: the start offset in the pin controller number space
1817 * @npins: the number of pins from the offset of each pin space (GPIO and
1818 * pin controller) to accumulate in this range
1821 * 0 on success, or a negative error-code on failure.
1823 * Calling this function directly from a DeviceTree-supported
1824 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1825 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1826 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1828 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1829 unsigned int gpio_offset, unsigned int pin_offset,
1832 struct gpio_pin_range *pin_range;
1833 struct gpio_device *gdev = gc->gpiodev;
1836 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1838 chip_err(gc, "failed to allocate pin ranges\n");
1842 /* Use local offset as range ID */
1843 pin_range->range.id = gpio_offset;
1844 pin_range->range.gc = gc;
1845 pin_range->range.name = gc->label;
1846 pin_range->range.base = gdev->base + gpio_offset;
1847 pin_range->range.pin_base = pin_offset;
1848 pin_range->range.npins = npins;
1849 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1851 if (IS_ERR(pin_range->pctldev)) {
1852 ret = PTR_ERR(pin_range->pctldev);
1853 chip_err(gc, "could not create pin range\n");
1857 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1858 gpio_offset, gpio_offset + npins - 1,
1860 pin_offset, pin_offset + npins - 1);
1862 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1866 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1869 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1870 * @gc: the chip to remove all the mappings for
1872 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1874 struct gpio_pin_range *pin_range, *tmp;
1875 struct gpio_device *gdev = gc->gpiodev;
1877 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1878 list_del(&pin_range->node);
1879 pinctrl_remove_gpio_range(pin_range->pctldev,
1884 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1886 #endif /* CONFIG_PINCTRL */
1888 /* These "optional" allocation calls help prevent drivers from stomping
1889 * on each other, and help provide better diagnostics in debugfs.
1890 * They're called even less than the "set direction" calls.
1892 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
1894 struct gpio_chip *gc = desc->gdev->chip;
1896 unsigned long flags;
1900 label = kstrdup_const(label, GFP_KERNEL);
1905 spin_lock_irqsave(&gpio_lock, flags);
1907 /* NOTE: gpio_request() can be called in early boot,
1908 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1911 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1912 desc_set_label(desc, label ? : "?");
1915 goto out_free_unlock;
1919 /* gc->request may sleep */
1920 spin_unlock_irqrestore(&gpio_lock, flags);
1921 offset = gpio_chip_hwgpio(desc);
1922 if (gpiochip_line_is_valid(gc, offset))
1923 ret = gc->request(gc, offset);
1926 spin_lock_irqsave(&gpio_lock, flags);
1929 desc_set_label(desc, NULL);
1930 clear_bit(FLAG_REQUESTED, &desc->flags);
1931 goto out_free_unlock;
1934 if (gc->get_direction) {
1935 /* gc->get_direction may sleep */
1936 spin_unlock_irqrestore(&gpio_lock, flags);
1937 gpiod_get_direction(desc);
1938 spin_lock_irqsave(&gpio_lock, flags);
1940 spin_unlock_irqrestore(&gpio_lock, flags);
1944 spin_unlock_irqrestore(&gpio_lock, flags);
1950 * This descriptor validation needs to be inserted verbatim into each
1951 * function taking a descriptor, so we need to use a preprocessor
1952 * macro to avoid endless duplication. If the desc is NULL it is an
1953 * optional GPIO and calls should just bail out.
1955 static int validate_desc(const struct gpio_desc *desc, const char *func)
1960 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
1961 return PTR_ERR(desc);
1964 pr_warn("%s: invalid GPIO (no device)\n", func);
1967 if (!desc->gdev->chip) {
1968 dev_warn(&desc->gdev->dev,
1969 "%s: backing chip is gone\n", func);
1975 #define VALIDATE_DESC(desc) do { \
1976 int __valid = validate_desc(desc, __func__); \
1981 #define VALIDATE_DESC_VOID(desc) do { \
1982 int __valid = validate_desc(desc, __func__); \
1987 int gpiod_request(struct gpio_desc *desc, const char *label)
1989 int ret = -EPROBE_DEFER;
1990 struct gpio_device *gdev;
1992 VALIDATE_DESC(desc);
1995 if (try_module_get(gdev->owner)) {
1996 ret = gpiod_request_commit(desc, label);
1998 module_put(gdev->owner);
2000 get_device(&gdev->dev);
2004 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2009 static bool gpiod_free_commit(struct gpio_desc *desc)
2012 unsigned long flags;
2013 struct gpio_chip *gc;
2017 gpiod_unexport(desc);
2019 spin_lock_irqsave(&gpio_lock, flags);
2021 gc = desc->gdev->chip;
2022 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2024 spin_unlock_irqrestore(&gpio_lock, flags);
2025 might_sleep_if(gc->can_sleep);
2026 gc->free(gc, gpio_chip_hwgpio(desc));
2027 spin_lock_irqsave(&gpio_lock, flags);
2029 kfree_const(desc->label);
2030 desc_set_label(desc, NULL);
2031 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2032 clear_bit(FLAG_REQUESTED, &desc->flags);
2033 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2034 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2035 clear_bit(FLAG_PULL_UP, &desc->flags);
2036 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2037 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2038 clear_bit(FLAG_EDGE_RISING, &desc->flags);
2039 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
2040 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2041 #ifdef CONFIG_OF_DYNAMIC
2044 #ifdef CONFIG_GPIO_CDEV
2045 WRITE_ONCE(desc->debounce_period_us, 0);
2050 spin_unlock_irqrestore(&gpio_lock, flags);
2051 blocking_notifier_call_chain(&desc->gdev->notifier,
2052 GPIOLINE_CHANGED_RELEASED, desc);
2057 void gpiod_free(struct gpio_desc *desc)
2059 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2060 module_put(desc->gdev->owner);
2061 put_device(&desc->gdev->dev);
2063 WARN_ON(extra_checks);
2068 * gpiochip_is_requested - return string iff signal was requested
2069 * @gc: controller managing the signal
2070 * @offset: of signal within controller's 0..(ngpio - 1) range
2072 * Returns NULL if the GPIO is not currently requested, else a string.
2073 * The string returned is the label passed to gpio_request(); if none has been
2074 * passed it is a meaningless, non-NULL constant.
2076 * This function is for use by GPIO controller drivers. The label can
2077 * help with diagnostics, and knowing that the signal is used as a GPIO
2078 * can help avoid accidentally multiplexing it to another controller.
2080 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
2082 struct gpio_desc *desc;
2084 desc = gpiochip_get_desc(gc, offset);
2088 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2092 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2095 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2097 * @hwnum: hardware number of the GPIO for which to request the descriptor
2098 * @label: label for the GPIO
2099 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2100 * specify things like line inversion semantics with the machine flags
2101 * such as GPIO_OUT_LOW
2102 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2103 * can be used to specify consumer semantics such as open drain
2105 * Function allows GPIO chip drivers to request and use their own GPIO
2106 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2107 * function will not increase reference count of the GPIO chip module. This
2108 * allows the GPIO chip module to be unloaded as needed (we assume that the
2109 * GPIO chip driver handles freeing the GPIOs it has requested).
2112 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2115 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2118 enum gpio_lookup_flags lflags,
2119 enum gpiod_flags dflags)
2121 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2125 chip_err(gc, "failed to get GPIO descriptor\n");
2129 ret = gpiod_request_commit(desc, label);
2131 return ERR_PTR(ret);
2133 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2135 chip_err(gc, "setup of own GPIO %s failed\n", label);
2136 gpiod_free_commit(desc);
2137 return ERR_PTR(ret);
2142 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2145 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2146 * @desc: GPIO descriptor to free
2148 * Function frees the given GPIO requested previously with
2149 * gpiochip_request_own_desc().
2151 void gpiochip_free_own_desc(struct gpio_desc *desc)
2154 gpiod_free_commit(desc);
2156 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2159 * Drivers MUST set GPIO direction before making get/set calls. In
2160 * some cases this is done in early boot, before IRQs are enabled.
2162 * As a rule these aren't called more than once (except for drivers
2163 * using the open-drain emulation idiom) so these are natural places
2164 * to accumulate extra debugging checks. Note that we can't (yet)
2165 * rely on gpio_request() having been called beforehand.
2168 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2169 unsigned long config)
2171 if (!gc->set_config)
2174 return gc->set_config(gc, offset, config);
2177 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2178 enum pin_config_param mode,
2181 struct gpio_chip *gc = desc->gdev->chip;
2182 unsigned long config;
2184 config = pinconf_to_config_packed(mode, argument);
2185 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2188 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2189 enum pin_config_param mode,
2192 struct device *dev = &desc->gdev->dev;
2193 int gpio = gpio_chip_hwgpio(desc);
2196 ret = gpio_set_config_with_argument(desc, mode, argument);
2197 if (ret != -ENOTSUPP)
2201 case PIN_CONFIG_PERSIST_STATE:
2202 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2211 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2213 return gpio_set_config_with_argument(desc, mode, 0);
2216 static int gpio_set_bias(struct gpio_desc *desc)
2218 enum pin_config_param bias;
2221 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2222 bias = PIN_CONFIG_BIAS_DISABLE;
2223 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2224 bias = PIN_CONFIG_BIAS_PULL_UP;
2225 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2226 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2231 case PIN_CONFIG_BIAS_PULL_DOWN:
2232 case PIN_CONFIG_BIAS_PULL_UP:
2241 return gpio_set_config_with_argument_optional(desc, bias, arg);
2245 * gpio_set_debounce_timeout() - Set debounce timeout
2246 * @desc: GPIO descriptor to set the debounce timeout
2247 * @debounce: Debounce timeout in microseconds
2249 * The function calls the certain GPIO driver to set debounce timeout
2252 * Returns 0 on success, or negative error code otherwise.
2254 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2256 return gpio_set_config_with_argument_optional(desc,
2257 PIN_CONFIG_INPUT_DEBOUNCE,
2262 * gpiod_direction_input - set the GPIO direction to input
2263 * @desc: GPIO to set to input
2265 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2266 * be called safely on it.
2268 * Return 0 in case of success, else an error code.
2270 int gpiod_direction_input(struct gpio_desc *desc)
2272 struct gpio_chip *gc;
2275 VALIDATE_DESC(desc);
2276 gc = desc->gdev->chip;
2279 * It is legal to have no .get() and .direction_input() specified if
2280 * the chip is output-only, but you can't specify .direction_input()
2281 * and not support the .get() operation, that doesn't make sense.
2283 if (!gc->get && gc->direction_input) {
2285 "%s: missing get() but have direction_input()\n",
2291 * If we have a .direction_input() callback, things are simple,
2292 * just call it. Else we are some input-only chip so try to check the
2293 * direction (if .get_direction() is supported) else we silently
2294 * assume we are in input mode after this.
2296 if (gc->direction_input) {
2297 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2298 } else if (gc->get_direction &&
2299 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2301 "%s: missing direction_input() operation and line is output\n",
2306 clear_bit(FLAG_IS_OUT, &desc->flags);
2307 ret = gpio_set_bias(desc);
2310 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2314 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2316 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2318 struct gpio_chip *gc = desc->gdev->chip;
2323 * It's OK not to specify .direction_output() if the gpiochip is
2324 * output-only, but if there is then not even a .set() operation it
2325 * is pretty tricky to drive the output line.
2327 if (!gc->set && !gc->direction_output) {
2329 "%s: missing set() and direction_output() operations\n",
2334 if (gc->direction_output) {
2335 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2337 /* Check that we are in output mode if we can */
2338 if (gc->get_direction &&
2339 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2341 "%s: missing direction_output() operation\n",
2346 * If we can't actively set the direction, we are some
2347 * output-only chip, so just drive the output as desired.
2349 gc->set(gc, gpio_chip_hwgpio(desc), val);
2353 set_bit(FLAG_IS_OUT, &desc->flags);
2354 trace_gpio_value(desc_to_gpio(desc), 0, val);
2355 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2360 * gpiod_direction_output_raw - set the GPIO direction to output
2361 * @desc: GPIO to set to output
2362 * @value: initial output value of the GPIO
2364 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2365 * be called safely on it. The initial value of the output must be specified
2366 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2368 * Return 0 in case of success, else an error code.
2370 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2372 VALIDATE_DESC(desc);
2373 return gpiod_direction_output_raw_commit(desc, value);
2375 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2378 * gpiod_direction_output - set the GPIO direction to output
2379 * @desc: GPIO to set to output
2380 * @value: initial output value of the GPIO
2382 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2383 * be called safely on it. The initial value of the output must be specified
2384 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2387 * Return 0 in case of success, else an error code.
2389 int gpiod_direction_output(struct gpio_desc *desc, int value)
2393 VALIDATE_DESC(desc);
2394 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2399 /* GPIOs used for enabled IRQs shall not be set as output */
2400 if (dont_test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2401 dont_test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2403 "%s: tried to set a GPIO tied to an IRQ as output\n",
2408 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2409 /* First see if we can enable open drain in hardware */
2410 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2412 goto set_output_value;
2413 /* Emulate open drain by not actively driving the line high */
2415 ret = gpiod_direction_input(desc);
2416 goto set_output_flag;
2418 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2419 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2421 goto set_output_value;
2422 /* Emulate open source by not actively driving the line low */
2424 ret = gpiod_direction_input(desc);
2425 goto set_output_flag;
2428 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2432 ret = gpio_set_bias(desc);
2435 return gpiod_direction_output_raw_commit(desc, value);
2439 * When emulating open-source or open-drain functionalities by not
2440 * actively driving the line (setting mode to input) we still need to
2441 * set the IS_OUT flag or otherwise we won't be able to set the line
2445 set_bit(FLAG_IS_OUT, &desc->flags);
2448 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2451 * gpiod_set_config - sets @config for a GPIO
2452 * @desc: descriptor of the GPIO for which to set the configuration
2453 * @config: Same packed config format as generic pinconf
2456 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2459 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2461 struct gpio_chip *gc;
2463 VALIDATE_DESC(desc);
2464 gc = desc->gdev->chip;
2466 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2468 EXPORT_SYMBOL_GPL(gpiod_set_config);
2471 * gpiod_set_debounce - sets @debounce time for a GPIO
2472 * @desc: descriptor of the GPIO for which to set debounce time
2473 * @debounce: debounce time in microseconds
2476 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2479 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2481 unsigned long config;
2483 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2484 return gpiod_set_config(desc, config);
2486 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2489 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2490 * @desc: descriptor of the GPIO for which to configure persistence
2491 * @transitory: True to lose state on suspend or reset, false for persistence
2494 * 0 on success, otherwise a negative error code.
2496 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2498 VALIDATE_DESC(desc);
2500 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2501 * persistence state.
2503 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2505 /* If the driver supports it, set the persistence state now */
2506 return gpio_set_config_with_argument_optional(desc,
2507 PIN_CONFIG_PERSIST_STATE,
2510 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2513 * gpiod_is_active_low - test whether a GPIO is active-low or not
2514 * @desc: the gpio descriptor to test
2516 * Returns 1 if the GPIO is active-low, 0 otherwise.
2518 int gpiod_is_active_low(const struct gpio_desc *desc)
2520 VALIDATE_DESC(desc);
2521 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2523 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2526 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2527 * @desc: the gpio descriptor to change
2529 void gpiod_toggle_active_low(struct gpio_desc *desc)
2531 VALIDATE_DESC_VOID(desc);
2532 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2534 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2536 /* I/O calls are only valid after configuration completed; the relevant
2537 * "is this a valid GPIO" error checks should already have been done.
2539 * "Get" operations are often inlinable as reading a pin value register,
2540 * and masking the relevant bit in that register.
2542 * When "set" operations are inlinable, they involve writing that mask to
2543 * one register to set a low value, or a different register to set it high.
2544 * Otherwise locking is needed, so there may be little value to inlining.
2546 *------------------------------------------------------------------------
2548 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2549 * have requested the GPIO. That can include implicit requesting by
2550 * a direction setting call. Marking a gpio as requested locks its chip
2551 * in memory, guaranteeing that these table lookups need no more locking
2552 * and that gpiochip_remove() will fail.
2554 * REVISIT when debugging, consider adding some instrumentation to ensure
2555 * that the GPIO was actually requested.
2558 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2560 struct gpio_chip *gc;
2564 gc = desc->gdev->chip;
2565 offset = gpio_chip_hwgpio(desc);
2566 value = gc->get ? gc->get(gc, offset) : -EIO;
2567 value = value < 0 ? value : !!value;
2568 trace_gpio_value(desc_to_gpio(desc), 1, value);
2572 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2573 unsigned long *mask, unsigned long *bits)
2575 if (gc->get_multiple)
2576 return gc->get_multiple(gc, mask, bits);
2580 for_each_set_bit(i, mask, gc->ngpio) {
2581 value = gc->get(gc, i);
2584 __assign_bit(i, bits, value);
2591 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2592 unsigned int array_size,
2593 struct gpio_desc **desc_array,
2594 struct gpio_array *array_info,
2595 unsigned long *value_bitmap)
2600 * Validate array_info against desc_array and its size.
2601 * It should immediately follow desc_array if both
2602 * have been obtained from the same gpiod_get_array() call.
2604 if (array_info && array_info->desc == desc_array &&
2605 array_size <= array_info->size &&
2606 (void *)array_info == desc_array + array_info->size) {
2608 WARN_ON(array_info->chip->can_sleep);
2610 ret = gpio_chip_get_multiple(array_info->chip,
2611 array_info->get_mask,
2616 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2617 bitmap_xor(value_bitmap, value_bitmap,
2618 array_info->invert_mask, array_size);
2620 i = find_first_zero_bit(array_info->get_mask, array_size);
2621 if (i == array_size)
2627 while (i < array_size) {
2628 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2629 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
2630 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
2631 unsigned long *mask, *bits;
2634 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2635 mask = fastpath_mask;
2636 bits = fastpath_bits;
2638 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
2640 mask = bitmap_alloc(gc->ngpio, flags);
2644 bits = bitmap_alloc(gc->ngpio, flags);
2651 bitmap_zero(mask, gc->ngpio);
2654 WARN_ON(gc->can_sleep);
2656 /* collect all inputs belonging to the same chip */
2659 const struct gpio_desc *desc = desc_array[i];
2660 int hwgpio = gpio_chip_hwgpio(desc);
2662 __set_bit(hwgpio, mask);
2666 i = find_next_zero_bit(array_info->get_mask,
2668 } while ((i < array_size) &&
2669 (desc_array[i]->gdev->chip == gc));
2671 ret = gpio_chip_get_multiple(gc, mask, bits);
2673 if (mask != fastpath_mask)
2675 if (bits != fastpath_bits)
2680 for (j = first; j < i; ) {
2681 const struct gpio_desc *desc = desc_array[j];
2682 int hwgpio = gpio_chip_hwgpio(desc);
2683 int value = test_bit(hwgpio, bits);
2685 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2687 __assign_bit(j, value_bitmap, value);
2688 trace_gpio_value(desc_to_gpio(desc), 1, value);
2692 j = find_next_zero_bit(array_info->get_mask, i,
2696 if (mask != fastpath_mask)
2698 if (bits != fastpath_bits)
2705 * gpiod_get_raw_value() - return a gpio's raw value
2706 * @desc: gpio whose value will be returned
2708 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2709 * its ACTIVE_LOW status, or negative errno on failure.
2711 * This function can be called from contexts where we cannot sleep, and will
2712 * complain if the GPIO chip functions potentially sleep.
2714 int gpiod_get_raw_value(const struct gpio_desc *desc)
2716 VALIDATE_DESC(desc);
2717 /* Should be using gpiod_get_raw_value_cansleep() */
2718 WARN_ON(desc->gdev->chip->can_sleep);
2719 return gpiod_get_raw_value_commit(desc);
2721 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2724 * gpiod_get_value() - return a gpio's value
2725 * @desc: gpio whose value will be returned
2727 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2728 * account, or negative errno on failure.
2730 * This function can be called from contexts where we cannot sleep, and will
2731 * complain if the GPIO chip functions potentially sleep.
2733 int gpiod_get_value(const struct gpio_desc *desc)
2737 VALIDATE_DESC(desc);
2738 /* Should be using gpiod_get_value_cansleep() */
2739 WARN_ON(desc->gdev->chip->can_sleep);
2741 value = gpiod_get_raw_value_commit(desc);
2745 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2750 EXPORT_SYMBOL_GPL(gpiod_get_value);
2753 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2754 * @array_size: number of elements in the descriptor array / value bitmap
2755 * @desc_array: array of GPIO descriptors whose values will be read
2756 * @array_info: information on applicability of fast bitmap processing path
2757 * @value_bitmap: bitmap to store the read values
2759 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2760 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2761 * else an error code.
2763 * This function can be called from contexts where we cannot sleep,
2764 * and it will complain if the GPIO chip functions potentially sleep.
2766 int gpiod_get_raw_array_value(unsigned int array_size,
2767 struct gpio_desc **desc_array,
2768 struct gpio_array *array_info,
2769 unsigned long *value_bitmap)
2773 return gpiod_get_array_value_complex(true, false, array_size,
2774 desc_array, array_info,
2777 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2780 * gpiod_get_array_value() - read values from an array of GPIOs
2781 * @array_size: number of elements in the descriptor array / value bitmap
2782 * @desc_array: array of GPIO descriptors whose values will be read
2783 * @array_info: information on applicability of fast bitmap processing path
2784 * @value_bitmap: bitmap to store the read values
2786 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2787 * into account. Return 0 in case of success, else an error code.
2789 * This function can be called from contexts where we cannot sleep,
2790 * and it will complain if the GPIO chip functions potentially sleep.
2792 int gpiod_get_array_value(unsigned int array_size,
2793 struct gpio_desc **desc_array,
2794 struct gpio_array *array_info,
2795 unsigned long *value_bitmap)
2799 return gpiod_get_array_value_complex(false, false, array_size,
2800 desc_array, array_info,
2803 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2806 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2807 * @desc: gpio descriptor whose state need to be set.
2808 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2810 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2813 struct gpio_chip *gc = desc->gdev->chip;
2814 int offset = gpio_chip_hwgpio(desc);
2817 ret = gc->direction_input(gc, offset);
2819 ret = gc->direction_output(gc, offset, 0);
2821 set_bit(FLAG_IS_OUT, &desc->flags);
2823 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2826 "%s: Error in set_value for open drain err %d\n",
2831 * _gpio_set_open_source_value() - Set the open source gpio's value.
2832 * @desc: gpio descriptor whose state need to be set.
2833 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2835 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2838 struct gpio_chip *gc = desc->gdev->chip;
2839 int offset = gpio_chip_hwgpio(desc);
2842 ret = gc->direction_output(gc, offset, 1);
2844 set_bit(FLAG_IS_OUT, &desc->flags);
2846 ret = gc->direction_input(gc, offset);
2848 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2851 "%s: Error in set_value for open source err %d\n",
2855 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2857 struct gpio_chip *gc;
2859 gc = desc->gdev->chip;
2860 trace_gpio_value(desc_to_gpio(desc), 0, value);
2861 gc->set(gc, gpio_chip_hwgpio(desc), value);
2865 * set multiple outputs on the same chip;
2866 * use the chip's set_multiple function if available;
2867 * otherwise set the outputs sequentially;
2868 * @chip: the GPIO chip we operate on
2869 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2870 * defines which outputs are to be changed
2871 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2872 * defines the values the outputs specified by mask are to be set to
2874 static void gpio_chip_set_multiple(struct gpio_chip *gc,
2875 unsigned long *mask, unsigned long *bits)
2877 if (gc->set_multiple) {
2878 gc->set_multiple(gc, mask, bits);
2882 /* set outputs if the corresponding mask bit is set */
2883 for_each_set_bit(i, mask, gc->ngpio)
2884 gc->set(gc, i, test_bit(i, bits));
2888 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
2889 unsigned int array_size,
2890 struct gpio_desc **desc_array,
2891 struct gpio_array *array_info,
2892 unsigned long *value_bitmap)
2897 * Validate array_info against desc_array and its size.
2898 * It should immediately follow desc_array if both
2899 * have been obtained from the same gpiod_get_array() call.
2901 if (array_info && array_info->desc == desc_array &&
2902 array_size <= array_info->size &&
2903 (void *)array_info == desc_array + array_info->size) {
2905 WARN_ON(array_info->chip->can_sleep);
2907 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2908 bitmap_xor(value_bitmap, value_bitmap,
2909 array_info->invert_mask, array_size);
2911 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2914 i = find_first_zero_bit(array_info->set_mask, array_size);
2915 if (i == array_size)
2921 while (i < array_size) {
2922 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2923 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
2924 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
2925 unsigned long *mask, *bits;
2928 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2929 mask = fastpath_mask;
2930 bits = fastpath_bits;
2932 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
2934 mask = bitmap_alloc(gc->ngpio, flags);
2938 bits = bitmap_alloc(gc->ngpio, flags);
2945 bitmap_zero(mask, gc->ngpio);
2948 WARN_ON(gc->can_sleep);
2951 struct gpio_desc *desc = desc_array[i];
2952 int hwgpio = gpio_chip_hwgpio(desc);
2953 int value = test_bit(i, value_bitmap);
2956 * Pins applicable for fast input but not for
2957 * fast output processing may have been already
2958 * inverted inside the fast path, skip them.
2960 if (!raw && !(array_info &&
2961 test_bit(i, array_info->invert_mask)) &&
2962 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2964 trace_gpio_value(desc_to_gpio(desc), 0, value);
2966 * collect all normal outputs belonging to the same chip
2967 * open drain and open source outputs are set individually
2969 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2970 gpio_set_open_drain_value_commit(desc, value);
2971 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2972 gpio_set_open_source_value_commit(desc, value);
2974 __set_bit(hwgpio, mask);
2975 __assign_bit(hwgpio, bits, value);
2981 i = find_next_zero_bit(array_info->set_mask,
2983 } while ((i < array_size) &&
2984 (desc_array[i]->gdev->chip == gc));
2985 /* push collected bits to outputs */
2987 gpio_chip_set_multiple(gc, mask, bits);
2989 if (mask != fastpath_mask)
2991 if (bits != fastpath_bits)
2998 * gpiod_set_raw_value() - assign a gpio's raw value
2999 * @desc: gpio whose value will be assigned
3000 * @value: value to assign
3002 * Set the raw value of the GPIO, i.e. the value of its physical line without
3003 * regard for its ACTIVE_LOW status.
3005 * This function can be called from contexts where we cannot sleep, and will
3006 * complain if the GPIO chip functions potentially sleep.
3008 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3010 VALIDATE_DESC_VOID(desc);
3011 /* Should be using gpiod_set_raw_value_cansleep() */
3012 WARN_ON(desc->gdev->chip->can_sleep);
3013 gpiod_set_raw_value_commit(desc, value);
3015 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3018 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3019 * @desc: the descriptor to set the value on
3020 * @value: value to set
3022 * This sets the value of a GPIO line backing a descriptor, applying
3023 * different semantic quirks like active low and open drain/source
3026 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3028 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3030 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3031 gpio_set_open_drain_value_commit(desc, value);
3032 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3033 gpio_set_open_source_value_commit(desc, value);
3035 gpiod_set_raw_value_commit(desc, value);
3039 * gpiod_set_value() - assign a gpio's value
3040 * @desc: gpio whose value will be assigned
3041 * @value: value to assign
3043 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3044 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3046 * This function can be called from contexts where we cannot sleep, and will
3047 * complain if the GPIO chip functions potentially sleep.
3049 void gpiod_set_value(struct gpio_desc *desc, int value)
3051 VALIDATE_DESC_VOID(desc);
3052 /* Should be using gpiod_set_value_cansleep() */
3053 WARN_ON(desc->gdev->chip->can_sleep);
3054 gpiod_set_value_nocheck(desc, value);
3056 EXPORT_SYMBOL_GPL(gpiod_set_value);
3059 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3060 * @array_size: number of elements in the descriptor array / value bitmap
3061 * @desc_array: array of GPIO descriptors whose values will be assigned
3062 * @array_info: information on applicability of fast bitmap processing path
3063 * @value_bitmap: bitmap of values to assign
3065 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3066 * without regard for their ACTIVE_LOW status.
3068 * This function can be called from contexts where we cannot sleep, and will
3069 * complain if the GPIO chip functions potentially sleep.
3071 int gpiod_set_raw_array_value(unsigned int array_size,
3072 struct gpio_desc **desc_array,
3073 struct gpio_array *array_info,
3074 unsigned long *value_bitmap)
3078 return gpiod_set_array_value_complex(true, false, array_size,
3079 desc_array, array_info, value_bitmap);
3081 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3084 * gpiod_set_array_value() - assign values to an array of GPIOs
3085 * @array_size: number of elements in the descriptor array / value bitmap
3086 * @desc_array: array of GPIO descriptors whose values will be assigned
3087 * @array_info: information on applicability of fast bitmap processing path
3088 * @value_bitmap: bitmap of values to assign
3090 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3093 * This function can be called from contexts where we cannot sleep, and will
3094 * complain if the GPIO chip functions potentially sleep.
3096 int gpiod_set_array_value(unsigned int array_size,
3097 struct gpio_desc **desc_array,
3098 struct gpio_array *array_info,
3099 unsigned long *value_bitmap)
3103 return gpiod_set_array_value_complex(false, false, array_size,
3104 desc_array, array_info,
3107 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3110 * gpiod_cansleep() - report whether gpio value access may sleep
3111 * @desc: gpio to check
3114 int gpiod_cansleep(const struct gpio_desc *desc)
3116 VALIDATE_DESC(desc);
3117 return desc->gdev->chip->can_sleep;
3119 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3122 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3123 * @desc: gpio to set the consumer name on
3124 * @name: the new consumer name
3126 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3128 VALIDATE_DESC(desc);
3130 name = kstrdup_const(name, GFP_KERNEL);
3135 kfree_const(desc->label);
3136 desc_set_label(desc, name);
3140 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3143 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3144 * @desc: gpio whose IRQ will be returned (already requested)
3146 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3149 int gpiod_to_irq(const struct gpio_desc *desc)
3151 struct gpio_chip *gc;
3155 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3156 * requires this function to not return zero on an invalid descriptor
3157 * but rather a negative error number.
3159 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3162 gc = desc->gdev->chip;
3163 offset = gpio_chip_hwgpio(desc);
3165 int retirq = gc->to_irq(gc, offset);
3167 /* Zero means NO_IRQ */
3173 #ifdef CONFIG_GPIOLIB_IRQCHIP
3176 * Avoid race condition with other code, which tries to lookup
3177 * an IRQ before the irqchip has been properly registered,
3178 * i.e. while gpiochip is still being brought up.
3180 return -EPROBE_DEFER;
3185 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3188 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3189 * @gc: the chip the GPIO to lock belongs to
3190 * @offset: the offset of the GPIO to lock as IRQ
3192 * This is used directly by GPIO drivers that want to lock down
3193 * a certain GPIO line to be used for IRQs.
3195 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3197 struct gpio_desc *desc;
3199 desc = gpiochip_get_desc(gc, offset);
3201 return PTR_ERR(desc);
3204 * If it's fast: flush the direction setting if something changed
3207 if (!gc->can_sleep && gc->get_direction) {
3208 int dir = gpiod_get_direction(desc);
3211 chip_err(gc, "%s: cannot get GPIO direction\n",
3217 /* To be valid for IRQ the line needs to be input or open drain */
3218 if (dont_test_bit(FLAG_IS_OUT, &desc->flags) &&
3219 !dont_test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3221 "%s: tried to flag a GPIO set as output for IRQ\n",
3226 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3227 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3230 * If the consumer has not set up a label (such as when the
3231 * IRQ is referenced from .to_irq()) we set up a label here
3232 * so it is clear this is used as an interrupt.
3235 desc_set_label(desc, "interrupt");
3239 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3242 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3243 * @gc: the chip the GPIO to lock belongs to
3244 * @offset: the offset of the GPIO to lock as IRQ
3246 * This is used directly by GPIO drivers that want to indicate
3247 * that a certain GPIO is no longer used exclusively for IRQ.
3249 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3251 struct gpio_desc *desc;
3253 desc = gpiochip_get_desc(gc, offset);
3257 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3258 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3260 /* If we only had this marking, erase it */
3261 if (desc->label && !strcmp(desc->label, "interrupt"))
3262 desc_set_label(desc, NULL);
3264 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3266 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3268 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3270 if (!IS_ERR(desc) &&
3271 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3272 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3274 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3276 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3278 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3280 if (!IS_ERR(desc) &&
3281 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3283 * We must not be output when using IRQ UNLESS we are
3286 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3287 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3288 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3291 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3293 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3295 if (offset >= gc->ngpio)
3298 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3300 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3302 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3306 if (!try_module_get(gc->gpiodev->owner))
3309 ret = gpiochip_lock_as_irq(gc, offset);
3311 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3312 module_put(gc->gpiodev->owner);
3317 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3319 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3321 gpiochip_unlock_as_irq(gc, offset);
3322 module_put(gc->gpiodev->owner);
3324 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3326 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3328 if (offset >= gc->ngpio)
3331 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3333 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3335 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3337 if (offset >= gc->ngpio)
3340 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3342 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3344 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3346 if (offset >= gc->ngpio)
3349 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3351 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3354 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3355 * @desc: gpio whose value will be returned
3357 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3358 * its ACTIVE_LOW status, or negative errno on failure.
3360 * This function is to be called from contexts that can sleep.
3362 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3364 might_sleep_if(extra_checks);
3365 VALIDATE_DESC(desc);
3366 return gpiod_get_raw_value_commit(desc);
3368 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3371 * gpiod_get_value_cansleep() - return a gpio's value
3372 * @desc: gpio whose value will be returned
3374 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3375 * account, or negative errno on failure.
3377 * This function is to be called from contexts that can sleep.
3379 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3383 might_sleep_if(extra_checks);
3384 VALIDATE_DESC(desc);
3385 value = gpiod_get_raw_value_commit(desc);
3389 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3394 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3397 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3398 * @array_size: number of elements in the descriptor array / value bitmap
3399 * @desc_array: array of GPIO descriptors whose values will be read
3400 * @array_info: information on applicability of fast bitmap processing path
3401 * @value_bitmap: bitmap to store the read values
3403 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3404 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3405 * else an error code.
3407 * This function is to be called from contexts that can sleep.
3409 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3410 struct gpio_desc **desc_array,
3411 struct gpio_array *array_info,
3412 unsigned long *value_bitmap)
3414 might_sleep_if(extra_checks);
3417 return gpiod_get_array_value_complex(true, true, array_size,
3418 desc_array, array_info,
3421 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3424 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3425 * @array_size: number of elements in the descriptor array / value bitmap
3426 * @desc_array: array of GPIO descriptors whose values will be read
3427 * @array_info: information on applicability of fast bitmap processing path
3428 * @value_bitmap: bitmap to store the read values
3430 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3431 * into account. Return 0 in case of success, else an error code.
3433 * This function is to be called from contexts that can sleep.
3435 int gpiod_get_array_value_cansleep(unsigned int array_size,
3436 struct gpio_desc **desc_array,
3437 struct gpio_array *array_info,
3438 unsigned long *value_bitmap)
3440 might_sleep_if(extra_checks);
3443 return gpiod_get_array_value_complex(false, true, array_size,
3444 desc_array, array_info,
3447 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3450 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3451 * @desc: gpio whose value will be assigned
3452 * @value: value to assign
3454 * Set the raw value of the GPIO, i.e. the value of its physical line without
3455 * regard for its ACTIVE_LOW status.
3457 * This function is to be called from contexts that can sleep.
3459 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3461 might_sleep_if(extra_checks);
3462 VALIDATE_DESC_VOID(desc);
3463 gpiod_set_raw_value_commit(desc, value);
3465 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3468 * gpiod_set_value_cansleep() - assign a gpio's value
3469 * @desc: gpio whose value will be assigned
3470 * @value: value to assign
3472 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3475 * This function is to be called from contexts that can sleep.
3477 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3479 might_sleep_if(extra_checks);
3480 VALIDATE_DESC_VOID(desc);
3481 gpiod_set_value_nocheck(desc, value);
3483 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3486 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3487 * @array_size: number of elements in the descriptor array / value bitmap
3488 * @desc_array: array of GPIO descriptors whose values will be assigned
3489 * @array_info: information on applicability of fast bitmap processing path
3490 * @value_bitmap: bitmap of values to assign
3492 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3493 * without regard for their ACTIVE_LOW status.
3495 * This function is to be called from contexts that can sleep.
3497 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3498 struct gpio_desc **desc_array,
3499 struct gpio_array *array_info,
3500 unsigned long *value_bitmap)
3502 might_sleep_if(extra_checks);
3505 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3506 array_info, value_bitmap);
3508 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3511 * gpiod_add_lookup_tables() - register GPIO device consumers
3512 * @tables: list of tables of consumers to register
3513 * @n: number of tables in the list
3515 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3519 mutex_lock(&gpio_lookup_lock);
3521 for (i = 0; i < n; i++)
3522 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3524 mutex_unlock(&gpio_lookup_lock);
3528 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3529 * @array_size: number of elements in the descriptor array / value bitmap
3530 * @desc_array: array of GPIO descriptors whose values will be assigned
3531 * @array_info: information on applicability of fast bitmap processing path
3532 * @value_bitmap: bitmap of values to assign
3534 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3537 * This function is to be called from contexts that can sleep.
3539 int gpiod_set_array_value_cansleep(unsigned int array_size,
3540 struct gpio_desc **desc_array,
3541 struct gpio_array *array_info,
3542 unsigned long *value_bitmap)
3544 might_sleep_if(extra_checks);
3547 return gpiod_set_array_value_complex(false, true, array_size,
3548 desc_array, array_info,
3551 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3554 * gpiod_add_lookup_table() - register GPIO device consumers
3555 * @table: table of consumers to register
3557 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3559 mutex_lock(&gpio_lookup_lock);
3561 list_add_tail(&table->list, &gpio_lookup_list);
3563 mutex_unlock(&gpio_lookup_lock);
3565 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3568 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3569 * @table: table of consumers to unregister
3571 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3573 /* Nothing to remove */
3577 mutex_lock(&gpio_lookup_lock);
3579 list_del(&table->list);
3581 mutex_unlock(&gpio_lookup_lock);
3583 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3586 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3587 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3589 void gpiod_add_hogs(struct gpiod_hog *hogs)
3591 struct gpio_chip *gc;
3592 struct gpiod_hog *hog;
3594 mutex_lock(&gpio_machine_hogs_mutex);
3596 for (hog = &hogs[0]; hog->chip_label; hog++) {
3597 list_add_tail(&hog->list, &gpio_machine_hogs);
3600 * The chip may have been registered earlier, so check if it
3601 * exists and, if so, try to hog the line now.
3603 gc = find_chip_by_name(hog->chip_label);
3605 gpiochip_machine_hog(gc, hog);
3608 mutex_unlock(&gpio_machine_hogs_mutex);
3610 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3612 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3614 const char *dev_id = dev ? dev_name(dev) : NULL;
3615 struct gpiod_lookup_table *table;
3617 mutex_lock(&gpio_lookup_lock);
3619 list_for_each_entry(table, &gpio_lookup_list, list) {
3620 if (table->dev_id && dev_id) {
3622 * Valid strings on both ends, must be identical to have
3625 if (!strcmp(table->dev_id, dev_id))
3629 * One of the pointers is NULL, so both must be to have
3632 if (dev_id == table->dev_id)
3639 mutex_unlock(&gpio_lookup_lock);
3643 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3644 unsigned int idx, unsigned long *flags)
3646 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3647 struct gpiod_lookup_table *table;
3648 struct gpiod_lookup *p;
3650 table = gpiod_find_lookup_table(dev);
3654 for (p = &table->table[0]; p->key; p++) {
3655 struct gpio_chip *gc;
3657 /* idx must always match exactly */
3661 /* If the lookup entry has a con_id, require exact match */
3662 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3665 if (p->chip_hwnum == U16_MAX) {
3666 desc = gpio_name_to_desc(p->key);
3672 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3674 return ERR_PTR(-EPROBE_DEFER);
3677 gc = find_chip_by_name(p->key);
3681 * As the lookup table indicates a chip with
3682 * p->key should exist, assume it may
3683 * still appear later and let the interested
3684 * consumer be probed again or let the Deferred
3685 * Probe infrastructure handle the error.
3687 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3689 return ERR_PTR(-EPROBE_DEFER);
3692 if (gc->ngpio <= p->chip_hwnum) {
3694 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3695 idx, p->chip_hwnum, gc->ngpio - 1,
3697 return ERR_PTR(-EINVAL);
3700 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3709 static int platform_gpio_count(struct device *dev, const char *con_id)
3711 struct gpiod_lookup_table *table;
3712 struct gpiod_lookup *p;
3713 unsigned int count = 0;
3715 table = gpiod_find_lookup_table(dev);
3719 for (p = &table->table[0]; p->key; p++) {
3720 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3721 (!con_id && !p->con_id))
3731 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3732 * @fwnode: handle of the firmware node
3733 * @con_id: function within the GPIO consumer
3734 * @index: index of the GPIO to obtain for the consumer
3735 * @flags: GPIO initialization flags
3736 * @label: label to attach to the requested GPIO
3738 * This function can be used for drivers that get their configuration
3739 * from opaque firmware.
3741 * The function properly finds the corresponding GPIO using whatever is the
3742 * underlying firmware interface and then makes sure that the GPIO
3743 * descriptor is requested before it is returned to the caller.
3746 * On successful request the GPIO pin is configured in accordance with
3749 * In case of error an ERR_PTR() is returned.
3751 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3752 const char *con_id, int index,
3753 enum gpiod_flags flags,
3756 struct gpio_desc *desc;
3757 char prop_name[32]; /* 32 is max size of property name */
3760 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3762 snprintf(prop_name, sizeof(prop_name), "%s-%s",
3763 con_id, gpio_suffixes[i]);
3765 snprintf(prop_name, sizeof(prop_name), "%s",
3768 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3770 if (!gpiod_not_found(desc))
3776 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
3779 * gpiod_count - return the number of GPIOs associated with a device / function
3780 * or -ENOENT if no GPIO has been assigned to the requested function
3781 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3782 * @con_id: function within the GPIO consumer
3784 int gpiod_count(struct device *dev, const char *con_id)
3786 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
3787 int count = -ENOENT;
3789 if (is_of_node(fwnode))
3790 count = of_gpio_get_count(dev, con_id);
3791 else if (is_acpi_node(fwnode))
3792 count = acpi_gpio_count(dev, con_id);
3795 count = platform_gpio_count(dev, con_id);
3799 EXPORT_SYMBOL_GPL(gpiod_count);
3802 * gpiod_get - obtain a GPIO for a given GPIO function
3803 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3804 * @con_id: function within the GPIO consumer
3805 * @flags: optional GPIO initialization flags
3807 * Return the GPIO descriptor corresponding to the function con_id of device
3808 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3809 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3811 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3812 enum gpiod_flags flags)
3814 return gpiod_get_index(dev, con_id, 0, flags);
3816 EXPORT_SYMBOL_GPL(gpiod_get);
3819 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3820 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3821 * @con_id: function within the GPIO consumer
3822 * @flags: optional GPIO initialization flags
3824 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3825 * the requested function it will return NULL. This is convenient for drivers
3826 * that need to handle optional GPIOs.
3828 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3830 enum gpiod_flags flags)
3832 return gpiod_get_index_optional(dev, con_id, 0, flags);
3834 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3838 * gpiod_configure_flags - helper function to configure a given GPIO
3839 * @desc: gpio whose value will be assigned
3840 * @con_id: function within the GPIO consumer
3841 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3842 * of_find_gpio() or of_get_gpio_hog()
3843 * @dflags: gpiod_flags - optional GPIO initialization flags
3845 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3846 * requested function and/or index, or another IS_ERR() code if an error
3847 * occurred while trying to acquire the GPIO.
3849 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3850 unsigned long lflags, enum gpiod_flags dflags)
3854 if (lflags & GPIO_ACTIVE_LOW)
3855 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3857 if (lflags & GPIO_OPEN_DRAIN)
3858 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3859 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3861 * This enforces open drain mode from the consumer side.
3862 * This is necessary for some busses like I2C, but the lookup
3863 * should *REALLY* have specified them as open drain in the
3864 * first place, so print a little warning here.
3866 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3868 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3871 if (lflags & GPIO_OPEN_SOURCE)
3872 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3874 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3876 "both pull-up and pull-down enabled, invalid configuration\n");
3880 if (lflags & GPIO_PULL_UP)
3881 set_bit(FLAG_PULL_UP, &desc->flags);
3882 else if (lflags & GPIO_PULL_DOWN)
3883 set_bit(FLAG_PULL_DOWN, &desc->flags);
3885 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3889 /* No particular flag request, return here... */
3890 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3891 gpiod_dbg(desc, "no flags found for %s\n", con_id);
3896 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3897 ret = gpiod_direction_output(desc,
3898 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3900 ret = gpiod_direction_input(desc);
3906 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3907 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3908 * @con_id: function within the GPIO consumer
3909 * @idx: index of the GPIO to obtain in the consumer
3910 * @flags: optional GPIO initialization flags
3912 * This variant of gpiod_get() allows to access GPIOs other than the first
3913 * defined one for functions that define several GPIOs.
3915 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3916 * requested function and/or index, or another IS_ERR() code if an error
3917 * occurred while trying to acquire the GPIO.
3919 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3922 enum gpiod_flags flags)
3924 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3925 struct gpio_desc *desc = NULL;
3927 /* Maybe we have a device name, maybe not */
3928 const char *devname = dev ? dev_name(dev) : "?";
3929 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
3931 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3933 /* Using device tree? */
3934 if (is_of_node(fwnode)) {
3935 dev_dbg(dev, "using device tree for GPIO lookup\n");
3936 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3937 } else if (is_acpi_node(fwnode)) {
3938 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3939 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3943 * Either we are not using DT or ACPI, or their lookup did not return
3944 * a result. In that case, use platform lookup as a fallback.
3946 if (!desc || gpiod_not_found(desc)) {
3947 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3948 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3952 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3957 * If a connection label was passed use that, else attempt to use
3958 * the device name as label
3960 ret = gpiod_request(desc, con_id ? con_id : devname);
3962 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
3964 * This happens when there are several consumers for
3965 * the same GPIO line: we just return here without
3966 * further initialization. It is a bit if a hack.
3967 * This is necessary to support fixed regulators.
3969 * FIXME: Make this more sane and safe.
3971 dev_info(dev, "nonexclusive access to GPIO for %s\n",
3972 con_id ? con_id : devname);
3975 return ERR_PTR(ret);
3979 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3981 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3983 return ERR_PTR(ret);
3986 blocking_notifier_call_chain(&desc->gdev->notifier,
3987 GPIOLINE_CHANGED_REQUESTED, desc);
3991 EXPORT_SYMBOL_GPL(gpiod_get_index);
3994 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3995 * @fwnode: handle of the firmware node
3996 * @propname: name of the firmware property representing the GPIO
3997 * @index: index of the GPIO to obtain for the consumer
3998 * @dflags: GPIO initialization flags
3999 * @label: label to attach to the requested GPIO
4001 * This function can be used for drivers that get their configuration
4002 * from opaque firmware.
4004 * The function properly finds the corresponding GPIO using whatever is the
4005 * underlying firmware interface and then makes sure that the GPIO
4006 * descriptor is requested before it is returned to the caller.
4009 * On successful request the GPIO pin is configured in accordance with
4012 * In case of error an ERR_PTR() is returned.
4014 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
4015 const char *propname, int index,
4016 enum gpiod_flags dflags,
4019 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4020 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4023 if (is_of_node(fwnode)) {
4024 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4029 } else if (is_acpi_node(fwnode)) {
4030 struct acpi_gpio_info info;
4032 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
4036 acpi_gpio_update_gpiod_flags(&dflags, &info);
4037 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
4039 return ERR_PTR(-EINVAL);
4041 /* Currently only ACPI takes this path */
4042 ret = gpiod_request(desc, label);
4044 return ERR_PTR(ret);
4046 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4049 return ERR_PTR(ret);
4052 blocking_notifier_call_chain(&desc->gdev->notifier,
4053 GPIOLINE_CHANGED_REQUESTED, desc);
4057 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4060 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4062 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4063 * @con_id: function within the GPIO consumer
4064 * @index: index of the GPIO to obtain in the consumer
4065 * @flags: optional GPIO initialization flags
4067 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4068 * specified index was assigned to the requested function it will return NULL.
4069 * This is convenient for drivers that need to handle optional GPIOs.
4071 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4074 enum gpiod_flags flags)
4076 struct gpio_desc *desc;
4078 desc = gpiod_get_index(dev, con_id, index, flags);
4079 if (gpiod_not_found(desc))
4084 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4087 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4088 * @desc: gpio whose value will be assigned
4089 * @name: gpio line name
4090 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4091 * of_find_gpio() or of_get_gpio_hog()
4092 * @dflags: gpiod_flags - optional GPIO initialization flags
4094 int gpiod_hog(struct gpio_desc *desc, const char *name,
4095 unsigned long lflags, enum gpiod_flags dflags)
4097 struct gpio_chip *gc;
4098 struct gpio_desc *local_desc;
4102 gc = gpiod_to_chip(desc);
4103 hwnum = gpio_chip_hwgpio(desc);
4105 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4107 if (IS_ERR(local_desc)) {
4108 ret = PTR_ERR(local_desc);
4109 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4110 name, gc->label, hwnum, ret);
4114 /* Mark GPIO as hogged so it can be identified and removed later */
4115 set_bit(FLAG_IS_HOGGED, &desc->flags);
4117 gpiod_info(desc, "hogged as %s%s\n",
4118 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4119 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4120 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4126 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4127 * @gc: gpio chip to act on
4129 static void gpiochip_free_hogs(struct gpio_chip *gc)
4133 for (id = 0; id < gc->ngpio; id++) {
4134 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4135 gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
4140 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4141 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4142 * @con_id: function within the GPIO consumer
4143 * @flags: optional GPIO initialization flags
4145 * This function acquires all the GPIOs defined under a given function.
4147 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4148 * no GPIO has been assigned to the requested function, or another IS_ERR()
4149 * code if an error occurred while trying to acquire the GPIOs.
4151 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4153 enum gpiod_flags flags)
4155 struct gpio_desc *desc;
4156 struct gpio_descs *descs;
4157 struct gpio_array *array_info = NULL;
4158 struct gpio_chip *gc;
4159 int count, bitmap_size;
4161 count = gpiod_count(dev, con_id);
4163 return ERR_PTR(count);
4165 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4167 return ERR_PTR(-ENOMEM);
4169 for (descs->ndescs = 0; descs->ndescs < count; ) {
4170 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4172 gpiod_put_array(descs);
4173 return ERR_CAST(desc);
4176 descs->desc[descs->ndescs] = desc;
4178 gc = gpiod_to_chip(desc);
4180 * If pin hardware number of array member 0 is also 0, select
4181 * its chip as a candidate for fast bitmap processing path.
4183 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4184 struct gpio_descs *array;
4186 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4189 array = kzalloc(struct_size(descs, desc, count) +
4190 struct_size(array_info, invert_mask,
4191 3 * bitmap_size), GFP_KERNEL);
4193 gpiod_put_array(descs);
4194 return ERR_PTR(-ENOMEM);
4197 memcpy(array, descs,
4198 struct_size(descs, desc, descs->ndescs + 1));
4202 array_info = (void *)(descs->desc + count);
4203 array_info->get_mask = array_info->invert_mask +
4205 array_info->set_mask = array_info->get_mask +
4208 array_info->desc = descs->desc;
4209 array_info->size = count;
4210 array_info->chip = gc;
4211 bitmap_set(array_info->get_mask, descs->ndescs,
4212 count - descs->ndescs);
4213 bitmap_set(array_info->set_mask, descs->ndescs,
4214 count - descs->ndescs);
4215 descs->info = array_info;
4217 /* Unmark array members which don't belong to the 'fast' chip */
4218 if (array_info && array_info->chip != gc) {
4219 __clear_bit(descs->ndescs, array_info->get_mask);
4220 __clear_bit(descs->ndescs, array_info->set_mask);
4223 * Detect array members which belong to the 'fast' chip
4224 * but their pins are not in hardware order.
4226 else if (array_info &&
4227 gpio_chip_hwgpio(desc) != descs->ndescs) {
4229 * Don't use fast path if all array members processed so
4230 * far belong to the same chip as this one but its pin
4231 * hardware number is different from its array index.
4233 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4236 __clear_bit(descs->ndescs,
4237 array_info->get_mask);
4238 __clear_bit(descs->ndescs,
4239 array_info->set_mask);
4241 } else if (array_info) {
4242 /* Exclude open drain or open source from fast output */
4243 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4244 gpiochip_line_is_open_source(gc, descs->ndescs))
4245 __clear_bit(descs->ndescs,
4246 array_info->set_mask);
4247 /* Identify 'fast' pins which require invertion */
4248 if (gpiod_is_active_low(desc))
4249 __set_bit(descs->ndescs,
4250 array_info->invert_mask);
4257 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4258 array_info->chip->label, array_info->size,
4259 *array_info->get_mask, *array_info->set_mask,
4260 *array_info->invert_mask);
4263 EXPORT_SYMBOL_GPL(gpiod_get_array);
4266 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4268 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4269 * @con_id: function within the GPIO consumer
4270 * @flags: optional GPIO initialization flags
4272 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4273 * assigned to the requested function it will return NULL.
4275 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4277 enum gpiod_flags flags)
4279 struct gpio_descs *descs;
4281 descs = gpiod_get_array(dev, con_id, flags);
4282 if (gpiod_not_found(descs))
4287 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4290 * gpiod_put - dispose of a GPIO descriptor
4291 * @desc: GPIO descriptor to dispose of
4293 * No descriptor can be used after gpiod_put() has been called on it.
4295 void gpiod_put(struct gpio_desc *desc)
4300 EXPORT_SYMBOL_GPL(gpiod_put);
4303 * gpiod_put_array - dispose of multiple GPIO descriptors
4304 * @descs: struct gpio_descs containing an array of descriptors
4306 void gpiod_put_array(struct gpio_descs *descs)
4310 for (i = 0; i < descs->ndescs; i++)
4311 gpiod_put(descs->desc[i]);
4315 EXPORT_SYMBOL_GPL(gpiod_put_array);
4318 static int gpio_bus_match(struct device *dev, struct device_driver *drv)
4320 struct fwnode_handle *fwnode = dev_fwnode(dev);
4323 * Only match if the fwnode doesn't already have a proper struct device
4326 if (fwnode && fwnode->dev != dev)
4331 static int gpio_stub_drv_probe(struct device *dev)
4334 * The DT node of some GPIO chips have a "compatible" property, but
4335 * never have a struct device added and probed by a driver to register
4336 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4337 * the consumers of the GPIO chip to get probe deferred forever because
4338 * they will be waiting for a device associated with the GPIO chip
4339 * firmware node to get added and bound to a driver.
4341 * To allow these consumers to probe, we associate the struct
4342 * gpio_device of the GPIO chip with the firmware node and then simply
4343 * bind it to this stub driver.
4348 static struct device_driver gpio_stub_drv = {
4349 .name = "gpio_stub_drv",
4350 .bus = &gpio_bus_type,
4351 .probe = gpio_stub_drv_probe,
4354 static int __init gpiolib_dev_init(void)
4358 /* Register GPIO sysfs bus */
4359 ret = bus_register(&gpio_bus_type);
4361 pr_err("gpiolib: could not register GPIO bus type\n");
4365 ret = driver_register(&gpio_stub_drv);
4367 pr_err("gpiolib: could not register GPIO stub driver\n");
4368 bus_unregister(&gpio_bus_type);
4372 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4374 pr_err("gpiolib: failed to allocate char dev region\n");
4375 driver_unregister(&gpio_stub_drv);
4376 bus_unregister(&gpio_bus_type);
4380 gpiolib_initialized = true;
4381 gpiochip_setup_devs();
4383 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4384 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4385 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4389 core_initcall(gpiolib_dev_init);
4391 #ifdef CONFIG_DEBUG_FS
4393 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4396 struct gpio_chip *gc = gdev->chip;
4397 unsigned gpio = gdev->base;
4398 struct gpio_desc *gdesc = &gdev->descs[0];
4403 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4404 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4406 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4412 gpiod_get_direction(gdesc);
4413 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4414 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4415 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4416 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4417 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4418 is_out ? "out" : "in ",
4419 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
4420 is_irq ? "IRQ " : "",
4421 active_low ? "ACTIVE LOW" : "");
4422 seq_printf(s, "\n");
4426 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4428 unsigned long flags;
4429 struct gpio_device *gdev = NULL;
4430 loff_t index = *pos;
4434 spin_lock_irqsave(&gpio_lock, flags);
4435 list_for_each_entry(gdev, &gpio_devices, list)
4437 spin_unlock_irqrestore(&gpio_lock, flags);
4440 spin_unlock_irqrestore(&gpio_lock, flags);
4445 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4447 unsigned long flags;
4448 struct gpio_device *gdev = v;
4451 spin_lock_irqsave(&gpio_lock, flags);
4452 if (list_is_last(&gdev->list, &gpio_devices))
4455 ret = list_entry(gdev->list.next, struct gpio_device, list);
4456 spin_unlock_irqrestore(&gpio_lock, flags);
4464 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4468 static int gpiolib_seq_show(struct seq_file *s, void *v)
4470 struct gpio_device *gdev = v;
4471 struct gpio_chip *gc = gdev->chip;
4472 struct device *parent;
4475 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4476 dev_name(&gdev->dev));
4480 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4481 dev_name(&gdev->dev),
4482 gdev->base, gdev->base + gdev->ngpio - 1);
4483 parent = gc->parent;
4485 seq_printf(s, ", parent: %s/%s",
4486 parent->bus ? parent->bus->name : "no-bus",
4489 seq_printf(s, ", %s", gc->label);
4491 seq_printf(s, ", can sleep");
4492 seq_printf(s, ":\n");
4495 gc->dbg_show(s, gc);
4497 gpiolib_dbg_show(s, gdev);
4502 static const struct seq_operations gpiolib_sops = {
4503 .start = gpiolib_seq_start,
4504 .next = gpiolib_seq_next,
4505 .stop = gpiolib_seq_stop,
4506 .show = gpiolib_seq_show,
4508 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4510 static int __init gpiolib_debugfs_init(void)
4512 /* /sys/kernel/debug/gpio */
4513 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4516 subsys_initcall(gpiolib_debugfs_init);
4518 #endif /* DEBUG_FS */