1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitmap.h>
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/interrupt.h>
7 #include <linux/spinlock.h>
8 #include <linux/list.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/debugfs.h>
12 #include <linux/seq_file.h>
13 #include <linux/gpio.h>
14 #include <linux/idr.h>
15 #include <linux/slab.h>
16 #include <linux/acpi.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/machine.h>
19 #include <linux/pinctrl/consumer.h>
21 #include <linux/compat.h>
22 #include <linux/file.h>
23 #include <uapi/linux/gpio.h>
26 #include "gpiolib-of.h"
27 #include "gpiolib-acpi.h"
28 #include "gpiolib-cdev.h"
29 #include "gpiolib-sysfs.h"
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/gpio.h>
34 /* Implementation infrastructure for GPIO interfaces.
36 * The GPIO programming interface allows for inlining speed-critical
37 * get/set operations for common cases, so that access to SOC-integrated
38 * GPIOs can sometimes cost only an instruction or two per bit.
42 /* When debugging, extend minimal trust to callers and platform code.
43 * Also emit diagnostic messages that may help initial bringup, when
44 * board setup or driver bugs are most common.
46 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 #define extra_checks 1
51 #define extra_checks 0
54 /* Device and char device-related information */
55 static DEFINE_IDA(gpio_ida);
56 static dev_t gpio_devt;
57 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
58 static struct bus_type gpio_bus_type = {
63 * Number of GPIOs to use for the fast path in set array
65 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
67 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
68 * While any GPIO is requested, its gpio_chip is not removable;
69 * each GPIO's "requested" flag serves as a lock and refcount.
71 DEFINE_SPINLOCK(gpio_lock);
73 static DEFINE_MUTEX(gpio_lookup_lock);
74 static LIST_HEAD(gpio_lookup_list);
75 LIST_HEAD(gpio_devices);
77 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
78 static LIST_HEAD(gpio_machine_hogs);
80 static void gpiochip_free_hogs(struct gpio_chip *gc);
81 static int gpiochip_add_irqchip(struct gpio_chip *gc,
82 struct lock_class_key *lock_key,
83 struct lock_class_key *request_key);
84 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
85 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
86 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
87 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
89 static bool gpiolib_initialized;
91 static inline void desc_set_label(struct gpio_desc *d, const char *label)
97 * gpio_to_desc - Convert a GPIO number to its descriptor
98 * @gpio: global GPIO number
101 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
102 * with the given number exists in the system.
104 struct gpio_desc *gpio_to_desc(unsigned gpio)
106 struct gpio_device *gdev;
109 spin_lock_irqsave(&gpio_lock, flags);
111 list_for_each_entry(gdev, &gpio_devices, list) {
112 if (gdev->base <= gpio &&
113 gdev->base + gdev->ngpio > gpio) {
114 spin_unlock_irqrestore(&gpio_lock, flags);
115 return &gdev->descs[gpio - gdev->base];
119 spin_unlock_irqrestore(&gpio_lock, flags);
121 if (!gpio_is_valid(gpio))
122 WARN(1, "invalid GPIO %d\n", gpio);
126 EXPORT_SYMBOL_GPL(gpio_to_desc);
129 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
130 * hardware number for this chip
132 * @hwnum: hardware number of the GPIO for this chip
135 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
136 * in the given chip for the specified hardware number.
138 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
141 struct gpio_device *gdev = gc->gpiodev;
143 if (hwnum >= gdev->ngpio)
144 return ERR_PTR(-EINVAL);
146 return &gdev->descs[hwnum];
148 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
151 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
152 * @desc: GPIO descriptor
154 * This should disappear in the future but is needed since we still
155 * use GPIO numbers for error messages and sysfs nodes.
158 * The global GPIO number for the GPIO specified by its descriptor.
160 int desc_to_gpio(const struct gpio_desc *desc)
162 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
164 EXPORT_SYMBOL_GPL(desc_to_gpio);
168 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
169 * @desc: descriptor to return the chip of
171 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
173 if (!desc || !desc->gdev)
175 return desc->gdev->chip;
177 EXPORT_SYMBOL_GPL(gpiod_to_chip);
179 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
180 static int gpiochip_find_base(int ngpio)
182 struct gpio_device *gdev;
183 int base = ARCH_NR_GPIOS - ngpio;
185 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
186 /* found a free space? */
187 if (gdev->base + gdev->ngpio <= base)
190 /* nope, check the space right before the chip */
191 base = gdev->base - ngpio;
194 if (gpio_is_valid(base)) {
195 pr_debug("%s: found new base at %d\n", __func__, base);
198 pr_err("%s: cannot find free range\n", __func__);
204 * gpiod_get_direction - return the current direction of a GPIO
205 * @desc: GPIO to get the direction of
207 * Returns 0 for output, 1 for input, or an error code in case of error.
209 * This function may sleep if gpiod_cansleep() is true.
211 int gpiod_get_direction(struct gpio_desc *desc)
213 struct gpio_chip *gc;
217 gc = gpiod_to_chip(desc);
218 offset = gpio_chip_hwgpio(desc);
221 * Open drain emulation using input mode may incorrectly report
222 * input here, fix that up.
224 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
225 test_bit(FLAG_IS_OUT, &desc->flags))
228 if (!gc->get_direction)
231 ret = gc->get_direction(gc, offset);
235 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
239 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
243 EXPORT_SYMBOL_GPL(gpiod_get_direction);
246 * Add a new chip to the global chips list, keeping the list of chips sorted
247 * by range(means [base, base + ngpio - 1]) order.
249 * Return -EBUSY if the new chip overlaps with some other chip's integer
252 static int gpiodev_add_to_list(struct gpio_device *gdev)
254 struct gpio_device *prev, *next;
256 if (list_empty(&gpio_devices)) {
257 /* initial entry in list */
258 list_add_tail(&gdev->list, &gpio_devices);
262 next = list_entry(gpio_devices.next, struct gpio_device, list);
263 if (gdev->base + gdev->ngpio <= next->base) {
264 /* add before first entry */
265 list_add(&gdev->list, &gpio_devices);
269 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
270 if (prev->base + prev->ngpio <= gdev->base) {
271 /* add behind last entry */
272 list_add_tail(&gdev->list, &gpio_devices);
276 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
277 /* at the end of the list */
278 if (&next->list == &gpio_devices)
281 /* add between prev and next */
282 if (prev->base + prev->ngpio <= gdev->base
283 && gdev->base + gdev->ngpio <= next->base) {
284 list_add(&gdev->list, &prev->list);
289 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
294 * Convert a GPIO name to its descriptor
295 * Note that there is no guarantee that GPIO names are globally unique!
296 * Hence this function will return, if it exists, a reference to the first GPIO
297 * line found that matches the given name.
299 static struct gpio_desc *gpio_name_to_desc(const char * const name)
301 struct gpio_device *gdev;
307 spin_lock_irqsave(&gpio_lock, flags);
309 list_for_each_entry(gdev, &gpio_devices, list) {
312 for (i = 0; i != gdev->ngpio; ++i) {
313 struct gpio_desc *desc = &gdev->descs[i];
318 if (!strcmp(desc->name, name)) {
319 spin_unlock_irqrestore(&gpio_lock, flags);
325 spin_unlock_irqrestore(&gpio_lock, flags);
331 * Take the names from gc->names and assign them to their GPIO descriptors.
332 * Warn if a name is already used for a GPIO line on a different GPIO chip.
335 * 1. Non-unique names are still accepted,
336 * 2. Name collisions within the same GPIO chip are not reported.
338 static int gpiochip_set_desc_names(struct gpio_chip *gc)
340 struct gpio_device *gdev = gc->gpiodev;
343 /* First check all names if they are unique */
344 for (i = 0; i != gc->ngpio; ++i) {
345 struct gpio_desc *gpio;
347 gpio = gpio_name_to_desc(gc->names[i]);
350 "Detected name collision for GPIO name '%s'\n",
354 /* Then add all names to the GPIO descriptors */
355 for (i = 0; i != gc->ngpio; ++i)
356 gdev->descs[i].name = gc->names[i];
362 * devprop_gpiochip_set_names - Set GPIO line names using device properties
363 * @chip: GPIO chip whose lines should be named, if possible
365 * Looks for device property "gpio-line-names" and if it exists assigns
366 * GPIO line names for the chip. The memory allocated for the assigned
367 * names belong to the underlying software node and should not be released
370 static int devprop_gpiochip_set_names(struct gpio_chip *chip)
372 struct gpio_device *gdev = chip->gpiodev;
373 struct device *dev = chip->parent;
378 /* GPIO chip may not have a parent device whose properties we inspect. */
382 count = device_property_string_array_count(dev, "gpio-line-names");
386 if (count > gdev->ngpio) {
387 dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
392 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
396 ret = device_property_read_string_array(dev, "gpio-line-names",
399 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
404 for (i = 0; i < count; i++)
405 gdev->descs[i].name = names[i];
412 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
416 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
420 /* Assume by default all GPIOs are valid */
421 bitmap_fill(p, gc->ngpio);
426 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
428 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
431 gc->valid_mask = gpiochip_allocate_mask(gc);
438 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
440 if (gc->init_valid_mask)
441 return gc->init_valid_mask(gc,
448 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
450 bitmap_free(gc->valid_mask);
451 gc->valid_mask = NULL;
454 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
456 if (gc->add_pin_ranges)
457 return gc->add_pin_ranges(gc);
462 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
465 /* No mask means all valid */
466 if (likely(!gc->valid_mask))
468 return test_bit(offset, gc->valid_mask);
470 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
472 static void gpiodevice_release(struct device *dev)
474 struct gpio_device *gdev = dev_get_drvdata(dev);
476 list_del(&gdev->list);
477 ida_free(&gpio_ida, gdev->id);
478 kfree_const(gdev->label);
483 #ifdef CONFIG_GPIO_CDEV
484 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
485 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
488 * gpiolib_cdev_register() indirectly calls device_add(), which is still
489 * required even when cdev is not selected.
491 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
492 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
495 static int gpiochip_setup_dev(struct gpio_device *gdev)
499 ret = gcdev_register(gdev, gpio_devt);
503 ret = gpiochip_sysfs_register(gdev);
505 goto err_remove_device;
507 /* From this point, the .release() function cleans up gpio_device */
508 gdev->dev.release = gpiodevice_release;
509 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
510 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
515 gcdev_unregister(gdev);
519 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
521 struct gpio_desc *desc;
524 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
526 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
531 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
534 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
536 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
537 __func__, gc->label, hog->chip_hwnum, rv);
540 static void machine_gpiochip_add(struct gpio_chip *gc)
542 struct gpiod_hog *hog;
544 mutex_lock(&gpio_machine_hogs_mutex);
546 list_for_each_entry(hog, &gpio_machine_hogs, list) {
547 if (!strcmp(gc->label, hog->chip_label))
548 gpiochip_machine_hog(gc, hog);
551 mutex_unlock(&gpio_machine_hogs_mutex);
554 static void gpiochip_setup_devs(void)
556 struct gpio_device *gdev;
559 list_for_each_entry(gdev, &gpio_devices, list) {
560 ret = gpiochip_setup_dev(gdev);
563 "Failed to initialize gpio device (%d)\n", ret);
567 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
568 struct lock_class_key *lock_key,
569 struct lock_class_key *request_key)
575 struct gpio_device *gdev;
578 * First: allocate and populate the internal stat container, and
579 * set up the struct device.
581 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
584 gdev->dev.bus = &gpio_bus_type;
588 gdev->dev.parent = gc->parent;
589 gdev->dev.of_node = gc->parent->of_node;
592 #ifdef CONFIG_OF_GPIO
593 /* If the gpiochip has an assigned OF node this takes precedence */
595 gdev->dev.of_node = gc->of_node;
597 gc->of_node = gdev->dev.of_node;
600 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
605 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
606 device_initialize(&gdev->dev);
607 dev_set_drvdata(&gdev->dev, gdev);
608 if (gc->parent && gc->parent->driver)
609 gdev->owner = gc->parent->driver->owner;
611 /* TODO: remove chip->owner */
612 gdev->owner = gc->owner;
614 gdev->owner = THIS_MODULE;
616 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
622 if (gc->ngpio == 0) {
623 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
628 if (gc->ngpio > FASTPATH_NGPIO)
629 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
630 gc->ngpio, FASTPATH_NGPIO);
632 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
638 gdev->ngpio = gc->ngpio;
641 spin_lock_irqsave(&gpio_lock, flags);
644 * TODO: this allocates a Linux GPIO number base in the global
645 * GPIO numberspace for this chip. In the long run we want to
646 * get *rid* of this numberspace and use only descriptors, but
647 * it may be a pipe dream. It will not happen before we get rid
648 * of the sysfs interface anyways.
651 base = gpiochip_find_base(gc->ngpio);
654 spin_unlock_irqrestore(&gpio_lock, flags);
658 * TODO: it should not be necessary to reflect the assigned
659 * base outside of the GPIO subsystem. Go over drivers and
660 * see if anyone makes use of this, else drop this and assign
667 ret = gpiodev_add_to_list(gdev);
669 spin_unlock_irqrestore(&gpio_lock, flags);
673 for (i = 0; i < gc->ngpio; i++)
674 gdev->descs[i].gdev = gdev;
676 spin_unlock_irqrestore(&gpio_lock, flags);
678 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
680 #ifdef CONFIG_PINCTRL
681 INIT_LIST_HEAD(&gdev->pin_ranges);
685 ret = gpiochip_set_desc_names(gc);
687 ret = devprop_gpiochip_set_names(gc);
689 goto err_remove_from_list;
691 ret = gpiochip_alloc_valid_mask(gc);
693 goto err_remove_from_list;
695 ret = of_gpiochip_add(gc);
697 goto err_free_gpiochip_mask;
699 ret = gpiochip_init_valid_mask(gc);
701 goto err_remove_of_chip;
703 for (i = 0; i < gc->ngpio; i++) {
704 struct gpio_desc *desc = &gdev->descs[i];
706 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
707 assign_bit(FLAG_IS_OUT,
708 &desc->flags, !gc->get_direction(gc, i));
710 assign_bit(FLAG_IS_OUT,
711 &desc->flags, !gc->direction_input);
715 ret = gpiochip_add_pin_ranges(gc);
717 goto err_remove_of_chip;
719 acpi_gpiochip_add(gc);
721 machine_gpiochip_add(gc);
723 ret = gpiochip_irqchip_init_valid_mask(gc);
725 goto err_remove_acpi_chip;
727 ret = gpiochip_irqchip_init_hw(gc);
729 goto err_remove_acpi_chip;
731 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
733 goto err_remove_irqchip_mask;
736 * By first adding the chardev, and then adding the device,
737 * we get a device node entry in sysfs under
738 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
739 * coldplug of device nodes and other udev business.
740 * We can do this only if gpiolib has been initialized.
741 * Otherwise, defer until later.
743 if (gpiolib_initialized) {
744 ret = gpiochip_setup_dev(gdev);
746 goto err_remove_irqchip;
751 gpiochip_irqchip_remove(gc);
752 err_remove_irqchip_mask:
753 gpiochip_irqchip_free_valid_mask(gc);
754 err_remove_acpi_chip:
755 acpi_gpiochip_remove(gc);
757 gpiochip_free_hogs(gc);
758 of_gpiochip_remove(gc);
759 err_free_gpiochip_mask:
760 gpiochip_remove_pin_ranges(gc);
761 gpiochip_free_valid_mask(gc);
762 err_remove_from_list:
763 spin_lock_irqsave(&gpio_lock, flags);
764 list_del(&gdev->list);
765 spin_unlock_irqrestore(&gpio_lock, flags);
767 kfree_const(gdev->label);
771 ida_free(&gpio_ida, gdev->id);
773 /* failures here can mean systems won't boot... */
774 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
775 gdev->base, gdev->base + gdev->ngpio - 1,
776 gc->label ? : "generic", ret);
780 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
783 * gpiochip_get_data() - get per-subdriver data for the chip
787 * The per-subdriver data for the chip.
789 void *gpiochip_get_data(struct gpio_chip *gc)
791 return gc->gpiodev->data;
793 EXPORT_SYMBOL_GPL(gpiochip_get_data);
796 * gpiochip_remove() - unregister a gpio_chip
797 * @gc: the chip to unregister
799 * A gpio_chip with any GPIOs still requested may not be removed.
801 void gpiochip_remove(struct gpio_chip *gc)
803 struct gpio_device *gdev = gc->gpiodev;
807 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
808 gpiochip_sysfs_unregister(gdev);
809 gpiochip_free_hogs(gc);
810 /* Numb the device, cancelling all outstanding operations */
812 gpiochip_irqchip_remove(gc);
813 acpi_gpiochip_remove(gc);
814 of_gpiochip_remove(gc);
815 gpiochip_remove_pin_ranges(gc);
816 gpiochip_free_valid_mask(gc);
818 * We accept no more calls into the driver from this point, so
819 * NULL the driver data pointer
823 spin_lock_irqsave(&gpio_lock, flags);
824 for (i = 0; i < gdev->ngpio; i++) {
825 if (gpiochip_is_requested(gc, i))
828 spin_unlock_irqrestore(&gpio_lock, flags);
830 if (i != gdev->ngpio)
832 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
835 * The gpiochip side puts its use of the device to rest here:
836 * if there are no userspace clients, the chardev and device will
837 * be removed, else it will be dangling until the last user is
840 gcdev_unregister(gdev);
841 put_device(&gdev->dev);
843 EXPORT_SYMBOL_GPL(gpiochip_remove);
846 * gpiochip_find() - iterator for locating a specific gpio_chip
847 * @data: data to pass to match function
848 * @match: Callback function to check gpio_chip
850 * Similar to bus_find_device. It returns a reference to a gpio_chip as
851 * determined by a user supplied @match callback. The callback should return
852 * 0 if the device doesn't match and non-zero if it does. If the callback is
853 * non-zero, this function will return to the caller and not iterate over any
856 struct gpio_chip *gpiochip_find(void *data,
857 int (*match)(struct gpio_chip *gc,
860 struct gpio_device *gdev;
861 struct gpio_chip *gc = NULL;
864 spin_lock_irqsave(&gpio_lock, flags);
865 list_for_each_entry(gdev, &gpio_devices, list)
866 if (gdev->chip && match(gdev->chip, data)) {
871 spin_unlock_irqrestore(&gpio_lock, flags);
875 EXPORT_SYMBOL_GPL(gpiochip_find);
877 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
879 const char *name = data;
881 return !strcmp(gc->label, name);
884 static struct gpio_chip *find_chip_by_name(const char *name)
886 return gpiochip_find((void *)name, gpiochip_match_name);
889 #ifdef CONFIG_GPIOLIB_IRQCHIP
892 * The following is irqchip helper code for gpiochips.
895 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
897 struct gpio_irq_chip *girq = &gc->irq;
902 return girq->init_hw(gc);
905 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
907 struct gpio_irq_chip *girq = &gc->irq;
909 if (!girq->init_valid_mask)
912 girq->valid_mask = gpiochip_allocate_mask(gc);
913 if (!girq->valid_mask)
916 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
921 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
923 bitmap_free(gc->irq.valid_mask);
924 gc->irq.valid_mask = NULL;
927 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
930 if (!gpiochip_line_is_valid(gc, offset))
932 /* No mask means all valid */
933 if (likely(!gc->irq.valid_mask))
935 return test_bit(offset, gc->irq.valid_mask);
937 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
940 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
941 * @gc: the gpiochip to set the irqchip chain to
942 * @parent_irq: the irq number corresponding to the parent IRQ for this
944 * @parent_handler: the parent interrupt handler for the accumulated IRQ
945 * coming out of the gpiochip. If the interrupt is nested rather than
946 * cascaded, pass NULL in this handler argument
948 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
949 unsigned int parent_irq,
950 irq_flow_handler_t parent_handler)
952 struct gpio_irq_chip *girq = &gc->irq;
953 struct device *dev = &gc->gpiodev->dev;
956 chip_err(gc, "called %s before setting up irqchip\n",
961 if (parent_handler) {
964 "you cannot have chained interrupts on a chip that may sleep\n");
967 girq->parents = devm_kcalloc(dev, 1,
968 sizeof(*girq->parents),
970 if (!girq->parents) {
971 chip_err(gc, "out of memory allocating parent IRQ\n");
974 girq->parents[0] = parent_irq;
975 girq->num_parents = 1;
977 * The parent irqchip is already using the chip_data for this
978 * irqchip, so our callbacks simply use the handler_data.
980 irq_set_chained_handler_and_data(parent_irq, parent_handler,
986 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
987 * @gc: the gpiochip to set the irqchip nested handler to
988 * @irqchip: the irqchip to nest to the gpiochip
989 * @parent_irq: the irq number corresponding to the parent IRQ for this
992 void gpiochip_set_nested_irqchip(struct gpio_chip *gc,
993 struct irq_chip *irqchip,
994 unsigned int parent_irq)
996 gpiochip_set_cascaded_irqchip(gc, parent_irq, NULL);
998 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1000 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1003 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1005 * @gc: the gpiochip to set the irqchip hierarchical handler to
1006 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1007 * will then percolate up to the parent
1009 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1010 struct irq_chip *irqchip)
1012 /* DT will deal with mapping each IRQ as we go along */
1013 if (is_of_node(gc->irq.fwnode))
1017 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1018 * irqs upfront instead of dynamically since we don't have the
1019 * dynamic type of allocation that hardware description languages
1020 * provide. Once all GPIO drivers using board files are gone from
1021 * the kernel we can delete this code, but for a transitional period
1022 * it is necessary to keep this around.
1024 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1028 for (i = 0; i < gc->ngpio; i++) {
1029 struct irq_fwspec fwspec;
1030 unsigned int parent_hwirq;
1031 unsigned int parent_type;
1032 struct gpio_irq_chip *girq = &gc->irq;
1035 * We call the child to parent translation function
1036 * only to check if the child IRQ is valid or not.
1037 * Just pick the rising edge type here as that is what
1038 * we likely need to support.
1040 ret = girq->child_to_parent_hwirq(gc, i,
1041 IRQ_TYPE_EDGE_RISING,
1045 chip_err(gc, "skip set-up on hwirq %d\n",
1050 fwspec.fwnode = gc->irq.fwnode;
1051 /* This is the hwirq for the GPIO line side of things */
1052 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1053 /* Just pick something */
1054 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1055 fwspec.param_count = 2;
1056 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1057 /* just pick something */
1066 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1073 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1078 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1079 struct irq_fwspec *fwspec,
1080 unsigned long *hwirq,
1083 /* We support standard DT translation */
1084 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1085 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1088 /* This is for board files and others not using DT */
1089 if (is_fwnode_irqchip(fwspec->fwnode)) {
1092 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1095 WARN_ON(*type == IRQ_TYPE_NONE);
1101 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1103 unsigned int nr_irqs,
1106 struct gpio_chip *gc = d->host_data;
1107 irq_hw_number_t hwirq;
1108 unsigned int type = IRQ_TYPE_NONE;
1109 struct irq_fwspec *fwspec = data;
1111 unsigned int parent_hwirq;
1112 unsigned int parent_type;
1113 struct gpio_irq_chip *girq = &gc->irq;
1117 * The nr_irqs parameter is always one except for PCI multi-MSI
1118 * so this should not happen.
1120 WARN_ON(nr_irqs != 1);
1122 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1126 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1128 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1129 &parent_hwirq, &parent_type);
1131 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1134 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1137 * We set handle_bad_irq because the .set_type() should
1138 * always be invoked and set the right type of handler.
1140 irq_domain_set_info(d,
1149 /* This parent only handles asserted level IRQs */
1150 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1154 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1156 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1157 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1159 * If the parent irqdomain is msi, the interrupts have already
1160 * been allocated, so the EEXIST is good.
1162 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1166 "failed to allocate parent hwirq %d for hwirq %lu\n",
1167 parent_hwirq, hwirq);
1173 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1174 unsigned int offset)
1179 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1181 ops->activate = gpiochip_irq_domain_activate;
1182 ops->deactivate = gpiochip_irq_domain_deactivate;
1183 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1184 ops->free = irq_domain_free_irqs_common;
1187 * We only allow overriding the translate() function for
1188 * hierarchical chips, and this should only be done if the user
1189 * really need something other than 1:1 translation.
1191 if (!ops->translate)
1192 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1195 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1197 if (!gc->irq.child_to_parent_hwirq ||
1199 chip_err(gc, "missing irqdomain vital data\n");
1203 if (!gc->irq.child_offset_to_irq)
1204 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1206 if (!gc->irq.populate_parent_alloc_arg)
1207 gc->irq.populate_parent_alloc_arg =
1208 gpiochip_populate_parent_fwspec_twocell;
1210 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1212 gc->irq.domain = irq_domain_create_hierarchy(
1213 gc->irq.parent_domain,
1217 &gc->irq.child_irq_domain_ops,
1220 if (!gc->irq.domain)
1223 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1228 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1230 return !!gc->irq.parent_domain;
1233 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1234 unsigned int parent_hwirq,
1235 unsigned int parent_type)
1237 struct irq_fwspec *fwspec;
1239 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1243 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1244 fwspec->param_count = 2;
1245 fwspec->param[0] = parent_hwirq;
1246 fwspec->param[1] = parent_type;
1250 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1252 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1253 unsigned int parent_hwirq,
1254 unsigned int parent_type)
1256 struct irq_fwspec *fwspec;
1258 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1262 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1263 fwspec->param_count = 4;
1264 fwspec->param[0] = 0;
1265 fwspec->param[1] = parent_hwirq;
1266 fwspec->param[2] = 0;
1267 fwspec->param[3] = parent_type;
1271 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1275 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1280 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1285 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1288 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1289 * @d: the irqdomain used by this irqchip
1290 * @irq: the global irq number used by this GPIO irqchip irq
1291 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1293 * This function will set up the mapping for a certain IRQ line on a
1294 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1295 * stored inside the gpiochip.
1297 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1298 irq_hw_number_t hwirq)
1300 struct gpio_chip *gc = d->host_data;
1303 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1306 irq_set_chip_data(irq, gc);
1308 * This lock class tells lockdep that GPIO irqs are in a different
1309 * category than their parents, so it won't report false recursion.
1311 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1312 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1313 /* Chips that use nested thread handlers have them marked */
1314 if (gc->irq.threaded)
1315 irq_set_nested_thread(irq, 1);
1316 irq_set_noprobe(irq);
1318 if (gc->irq.num_parents == 1)
1319 ret = irq_set_parent(irq, gc->irq.parents[0]);
1320 else if (gc->irq.map)
1321 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1327 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1328 * is passed as default type.
1330 if (gc->irq.default_type != IRQ_TYPE_NONE)
1331 irq_set_irq_type(irq, gc->irq.default_type);
1335 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1337 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1339 struct gpio_chip *gc = d->host_data;
1341 if (gc->irq.threaded)
1342 irq_set_nested_thread(irq, 0);
1343 irq_set_chip_and_handler(irq, NULL, NULL);
1344 irq_set_chip_data(irq, NULL);
1346 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1348 static const struct irq_domain_ops gpiochip_domain_ops = {
1349 .map = gpiochip_irq_map,
1350 .unmap = gpiochip_irq_unmap,
1351 /* Virtually all GPIO irqchips are twocell:ed */
1352 .xlate = irq_domain_xlate_twocell,
1356 * TODO: move these activate/deactivate in under the hierarchicial
1357 * irqchip implementation as static once SPMI and SSBI (all external
1358 * users) are phased over.
1361 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1362 * @domain: The IRQ domain used by this IRQ chip
1363 * @data: Outermost irq_data associated with the IRQ
1364 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1366 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1367 * used as the activate function for the &struct irq_domain_ops. The host_data
1368 * for the IRQ domain must be the &struct gpio_chip.
1370 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1371 struct irq_data *data, bool reserve)
1373 struct gpio_chip *gc = domain->host_data;
1375 return gpiochip_lock_as_irq(gc, data->hwirq);
1377 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1380 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1381 * @domain: The IRQ domain used by this IRQ chip
1382 * @data: Outermost irq_data associated with the IRQ
1384 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1385 * be used as the deactivate function for the &struct irq_domain_ops. The
1386 * host_data for the IRQ domain must be the &struct gpio_chip.
1388 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1389 struct irq_data *data)
1391 struct gpio_chip *gc = domain->host_data;
1393 return gpiochip_unlock_as_irq(gc, data->hwirq);
1395 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1397 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned offset)
1399 struct irq_domain *domain = gc->irq.domain;
1401 if (!gpiochip_irqchip_irq_valid(gc, offset))
1404 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1405 if (irq_domain_is_hierarchy(domain)) {
1406 struct irq_fwspec spec;
1408 spec.fwnode = domain->fwnode;
1409 spec.param_count = 2;
1410 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1411 spec.param[1] = IRQ_TYPE_NONE;
1413 return irq_create_fwspec_mapping(&spec);
1417 return irq_create_mapping(domain, offset);
1420 static int gpiochip_irq_reqres(struct irq_data *d)
1422 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1424 return gpiochip_reqres_irq(gc, d->hwirq);
1427 static void gpiochip_irq_relres(struct irq_data *d)
1429 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1431 gpiochip_relres_irq(gc, d->hwirq);
1434 static void gpiochip_irq_mask(struct irq_data *d)
1436 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1438 if (gc->irq.irq_mask)
1439 gc->irq.irq_mask(d);
1440 gpiochip_disable_irq(gc, d->hwirq);
1443 static void gpiochip_irq_unmask(struct irq_data *d)
1445 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1447 gpiochip_enable_irq(gc, d->hwirq);
1448 if (gc->irq.irq_unmask)
1449 gc->irq.irq_unmask(d);
1452 static void gpiochip_irq_enable(struct irq_data *d)
1454 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1456 gpiochip_enable_irq(gc, d->hwirq);
1457 gc->irq.irq_enable(d);
1460 static void gpiochip_irq_disable(struct irq_data *d)
1462 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1464 gc->irq.irq_disable(d);
1465 gpiochip_disable_irq(gc, d->hwirq);
1468 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1470 struct irq_chip *irqchip = gc->irq.chip;
1472 if (!irqchip->irq_request_resources &&
1473 !irqchip->irq_release_resources) {
1474 irqchip->irq_request_resources = gpiochip_irq_reqres;
1475 irqchip->irq_release_resources = gpiochip_irq_relres;
1477 if (WARN_ON(gc->irq.irq_enable))
1479 /* Check if the irqchip already has this hook... */
1480 if (irqchip->irq_enable == gpiochip_irq_enable) {
1482 * ...and if so, give a gentle warning that this is bad
1486 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1490 if (irqchip->irq_disable) {
1491 gc->irq.irq_disable = irqchip->irq_disable;
1492 irqchip->irq_disable = gpiochip_irq_disable;
1494 gc->irq.irq_mask = irqchip->irq_mask;
1495 irqchip->irq_mask = gpiochip_irq_mask;
1498 if (irqchip->irq_enable) {
1499 gc->irq.irq_enable = irqchip->irq_enable;
1500 irqchip->irq_enable = gpiochip_irq_enable;
1502 gc->irq.irq_unmask = irqchip->irq_unmask;
1503 irqchip->irq_unmask = gpiochip_irq_unmask;
1508 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1509 * @gc: the GPIO chip to add the IRQ chip to
1510 * @lock_key: lockdep class for IRQ lock
1511 * @request_key: lockdep class for IRQ request
1513 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1514 struct lock_class_key *lock_key,
1515 struct lock_class_key *request_key)
1517 struct irq_chip *irqchip = gc->irq.chip;
1518 const struct irq_domain_ops *ops = NULL;
1519 struct device_node *np;
1526 if (gc->irq.parent_handler && gc->can_sleep) {
1527 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1531 np = gc->gpiodev->dev.of_node;
1532 type = gc->irq.default_type;
1535 * Specifying a default trigger is a terrible idea if DT or ACPI is
1536 * used to configure the interrupts, as you may end up with
1537 * conflicting triggers. Tell the user, and reset to NONE.
1539 if (WARN(np && type != IRQ_TYPE_NONE,
1540 "%s: Ignoring %u default trigger\n", np->full_name, type))
1541 type = IRQ_TYPE_NONE;
1543 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1544 acpi_handle_warn(ACPI_HANDLE(gc->parent),
1545 "Ignoring %u default trigger\n", type);
1546 type = IRQ_TYPE_NONE;
1549 gc->to_irq = gpiochip_to_irq;
1550 gc->irq.default_type = type;
1551 gc->irq.lock_key = lock_key;
1552 gc->irq.request_key = request_key;
1554 /* If a parent irqdomain is provided, let's build a hierarchy */
1555 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1556 int ret = gpiochip_hierarchy_add_domain(gc);
1560 /* Some drivers provide custom irqdomain ops */
1561 if (gc->irq.domain_ops)
1562 ops = gc->irq.domain_ops;
1565 ops = &gpiochip_domain_ops;
1566 gc->irq.domain = irq_domain_add_simple(np,
1570 if (!gc->irq.domain)
1574 if (gc->irq.parent_handler) {
1575 void *data = gc->irq.parent_handler_data ?: gc;
1577 for (i = 0; i < gc->irq.num_parents; i++) {
1579 * The parent IRQ chip is already using the chip_data
1580 * for this IRQ chip, so our callbacks simply use the
1583 irq_set_chained_handler_and_data(gc->irq.parents[i],
1584 gc->irq.parent_handler,
1589 gpiochip_set_irq_hooks(gc);
1591 acpi_gpiochip_request_interrupts(gc);
1597 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1598 * @gc: the gpiochip to remove the irqchip from
1600 * This is called only from gpiochip_remove()
1602 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1604 struct irq_chip *irqchip = gc->irq.chip;
1605 unsigned int offset;
1607 acpi_gpiochip_free_interrupts(gc);
1609 if (irqchip && gc->irq.parent_handler) {
1610 struct gpio_irq_chip *irq = &gc->irq;
1613 for (i = 0; i < irq->num_parents; i++)
1614 irq_set_chained_handler_and_data(irq->parents[i],
1618 /* Remove all IRQ mappings and delete the domain */
1619 if (gc->irq.domain) {
1622 for (offset = 0; offset < gc->ngpio; offset++) {
1623 if (!gpiochip_irqchip_irq_valid(gc, offset))
1626 irq = irq_find_mapping(gc->irq.domain, offset);
1627 irq_dispose_mapping(irq);
1630 irq_domain_remove(gc->irq.domain);
1634 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1635 irqchip->irq_request_resources = NULL;
1636 irqchip->irq_release_resources = NULL;
1638 if (irqchip->irq_enable == gpiochip_irq_enable) {
1639 irqchip->irq_enable = gc->irq.irq_enable;
1640 irqchip->irq_disable = gc->irq.irq_disable;
1643 gc->irq.irq_enable = NULL;
1644 gc->irq.irq_disable = NULL;
1645 gc->irq.chip = NULL;
1647 gpiochip_irqchip_free_valid_mask(gc);
1651 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1652 * @gc: the gpiochip to add the irqchip to
1653 * @irqchip: the irqchip to add to the gpiochip
1654 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1655 * allocate gpiochip irqs from
1656 * @handler: the irq handler to use (often a predefined irq core function)
1657 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1658 * to have the core avoid setting up any default type in the hardware.
1659 * @threaded: whether this irqchip uses a nested thread handler
1660 * @lock_key: lockdep class for IRQ lock
1661 * @request_key: lockdep class for IRQ request
1663 * This function closely associates a certain irqchip with a certain
1664 * gpiochip, providing an irq domain to translate the local IRQs to
1665 * global irqs in the gpiolib core, and making sure that the gpiochip
1666 * is passed as chip data to all related functions. Driver callbacks
1667 * need to use gpiochip_get_data() to get their local state containers back
1668 * from the gpiochip passed as chip data. An irqdomain will be stored
1669 * in the gpiochip that shall be used by the driver to handle IRQ number
1670 * translation. The gpiochip will need to be initialized and registered
1671 * before calling this function.
1673 * This function will handle two cell:ed simple IRQs and assumes all
1674 * the pins on the gpiochip can generate a unique IRQ. Everything else
1675 * need to be open coded.
1677 int gpiochip_irqchip_add_key(struct gpio_chip *gc,
1678 struct irq_chip *irqchip,
1679 unsigned int first_irq,
1680 irq_flow_handler_t handler,
1683 struct lock_class_key *lock_key,
1684 struct lock_class_key *request_key)
1686 struct device_node *of_node;
1688 if (!gc || !irqchip)
1692 chip_err(gc, "missing gpiochip .dev parent pointer\n");
1695 gc->irq.threaded = threaded;
1696 of_node = gc->parent->of_node;
1697 #ifdef CONFIG_OF_GPIO
1699 * If the gpiochip has an assigned OF node this takes precedence
1700 * FIXME: get rid of this and use gc->parent->of_node
1704 of_node = gc->of_node;
1707 * Specifying a default trigger is a terrible idea if DT or ACPI is
1708 * used to configure the interrupts, as you may end-up with
1709 * conflicting triggers. Tell the user, and reset to NONE.
1711 if (WARN(of_node && type != IRQ_TYPE_NONE,
1712 "%pOF: Ignoring %d default trigger\n", of_node, type))
1713 type = IRQ_TYPE_NONE;
1714 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1715 acpi_handle_warn(ACPI_HANDLE(gc->parent),
1716 "Ignoring %d default trigger\n", type);
1717 type = IRQ_TYPE_NONE;
1720 gc->irq.chip = irqchip;
1721 gc->irq.handler = handler;
1722 gc->irq.default_type = type;
1723 gc->to_irq = gpiochip_to_irq;
1724 gc->irq.lock_key = lock_key;
1725 gc->irq.request_key = request_key;
1726 gc->irq.domain = irq_domain_add_simple(of_node,
1727 gc->ngpio, first_irq,
1728 &gpiochip_domain_ops, gc);
1729 if (!gc->irq.domain) {
1730 gc->irq.chip = NULL;
1734 gpiochip_set_irq_hooks(gc);
1736 acpi_gpiochip_request_interrupts(gc);
1740 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
1743 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1744 * @gc: the gpiochip to add the irqchip to
1745 * @domain: the irqdomain to add to the gpiochip
1747 * This function adds an IRQ domain to the gpiochip.
1749 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1750 struct irq_domain *domain)
1755 gc->to_irq = gpiochip_to_irq;
1756 gc->irq.domain = domain;
1760 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1762 #else /* CONFIG_GPIOLIB_IRQCHIP */
1764 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1765 struct lock_class_key *lock_key,
1766 struct lock_class_key *request_key)
1770 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1772 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1777 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1781 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1784 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1787 * gpiochip_generic_request() - request the gpio function for a pin
1788 * @gc: the gpiochip owning the GPIO
1789 * @offset: the offset of the GPIO to request for GPIO function
1791 int gpiochip_generic_request(struct gpio_chip *gc, unsigned offset)
1793 #ifdef CONFIG_PINCTRL
1794 if (list_empty(&gc->gpiodev->pin_ranges))
1798 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1800 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1803 * gpiochip_generic_free() - free the gpio function from a pin
1804 * @gc: the gpiochip to request the gpio function for
1805 * @offset: the offset of the GPIO to free from GPIO function
1807 void gpiochip_generic_free(struct gpio_chip *gc, unsigned offset)
1809 pinctrl_gpio_free(gc->gpiodev->base + offset);
1811 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1814 * gpiochip_generic_config() - apply configuration for a pin
1815 * @gc: the gpiochip owning the GPIO
1816 * @offset: the offset of the GPIO to apply the configuration
1817 * @config: the configuration to be applied
1819 int gpiochip_generic_config(struct gpio_chip *gc, unsigned offset,
1820 unsigned long config)
1822 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1824 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1826 #ifdef CONFIG_PINCTRL
1829 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1830 * @gc: the gpiochip to add the range for
1831 * @pctldev: the pin controller to map to
1832 * @gpio_offset: the start offset in the current gpio_chip number space
1833 * @pin_group: name of the pin group inside the pin controller
1835 * Calling this function directly from a DeviceTree-supported
1836 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1837 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1838 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1840 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1841 struct pinctrl_dev *pctldev,
1842 unsigned int gpio_offset, const char *pin_group)
1844 struct gpio_pin_range *pin_range;
1845 struct gpio_device *gdev = gc->gpiodev;
1848 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1850 chip_err(gc, "failed to allocate pin ranges\n");
1854 /* Use local offset as range ID */
1855 pin_range->range.id = gpio_offset;
1856 pin_range->range.gc = gc;
1857 pin_range->range.name = gc->label;
1858 pin_range->range.base = gdev->base + gpio_offset;
1859 pin_range->pctldev = pctldev;
1861 ret = pinctrl_get_group_pins(pctldev, pin_group,
1862 &pin_range->range.pins,
1863 &pin_range->range.npins);
1869 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1871 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1872 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1873 pinctrl_dev_get_devname(pctldev), pin_group);
1875 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1879 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1882 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1883 * @gc: the gpiochip to add the range for
1884 * @pinctl_name: the dev_name() of the pin controller to map to
1885 * @gpio_offset: the start offset in the current gpio_chip number space
1886 * @pin_offset: the start offset in the pin controller number space
1887 * @npins: the number of pins from the offset of each pin space (GPIO and
1888 * pin controller) to accumulate in this range
1891 * 0 on success, or a negative error-code on failure.
1893 * Calling this function directly from a DeviceTree-supported
1894 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1895 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1896 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1898 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1899 unsigned int gpio_offset, unsigned int pin_offset,
1902 struct gpio_pin_range *pin_range;
1903 struct gpio_device *gdev = gc->gpiodev;
1906 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1908 chip_err(gc, "failed to allocate pin ranges\n");
1912 /* Use local offset as range ID */
1913 pin_range->range.id = gpio_offset;
1914 pin_range->range.gc = gc;
1915 pin_range->range.name = gc->label;
1916 pin_range->range.base = gdev->base + gpio_offset;
1917 pin_range->range.pin_base = pin_offset;
1918 pin_range->range.npins = npins;
1919 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1921 if (IS_ERR(pin_range->pctldev)) {
1922 ret = PTR_ERR(pin_range->pctldev);
1923 chip_err(gc, "could not create pin range\n");
1927 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1928 gpio_offset, gpio_offset + npins - 1,
1930 pin_offset, pin_offset + npins - 1);
1932 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1936 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1939 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1940 * @gc: the chip to remove all the mappings for
1942 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1944 struct gpio_pin_range *pin_range, *tmp;
1945 struct gpio_device *gdev = gc->gpiodev;
1947 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1948 list_del(&pin_range->node);
1949 pinctrl_remove_gpio_range(pin_range->pctldev,
1954 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1956 #endif /* CONFIG_PINCTRL */
1958 /* These "optional" allocation calls help prevent drivers from stomping
1959 * on each other, and help provide better diagnostics in debugfs.
1960 * They're called even less than the "set direction" calls.
1962 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
1964 struct gpio_chip *gc = desc->gdev->chip;
1966 unsigned long flags;
1970 label = kstrdup_const(label, GFP_KERNEL);
1975 spin_lock_irqsave(&gpio_lock, flags);
1977 /* NOTE: gpio_request() can be called in early boot,
1978 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1981 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1982 desc_set_label(desc, label ? : "?");
1991 /* gc->request may sleep */
1992 spin_unlock_irqrestore(&gpio_lock, flags);
1993 offset = gpio_chip_hwgpio(desc);
1994 if (gpiochip_line_is_valid(gc, offset))
1995 ret = gc->request(gc, offset);
1998 spin_lock_irqsave(&gpio_lock, flags);
2001 desc_set_label(desc, NULL);
2003 clear_bit(FLAG_REQUESTED, &desc->flags);
2007 if (gc->get_direction) {
2008 /* gc->get_direction may sleep */
2009 spin_unlock_irqrestore(&gpio_lock, flags);
2010 gpiod_get_direction(desc);
2011 spin_lock_irqsave(&gpio_lock, flags);
2014 spin_unlock_irqrestore(&gpio_lock, flags);
2019 * This descriptor validation needs to be inserted verbatim into each
2020 * function taking a descriptor, so we need to use a preprocessor
2021 * macro to avoid endless duplication. If the desc is NULL it is an
2022 * optional GPIO and calls should just bail out.
2024 static int validate_desc(const struct gpio_desc *desc, const char *func)
2029 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2030 return PTR_ERR(desc);
2033 pr_warn("%s: invalid GPIO (no device)\n", func);
2036 if (!desc->gdev->chip) {
2037 dev_warn(&desc->gdev->dev,
2038 "%s: backing chip is gone\n", func);
2044 #define VALIDATE_DESC(desc) do { \
2045 int __valid = validate_desc(desc, __func__); \
2050 #define VALIDATE_DESC_VOID(desc) do { \
2051 int __valid = validate_desc(desc, __func__); \
2056 int gpiod_request(struct gpio_desc *desc, const char *label)
2058 int ret = -EPROBE_DEFER;
2059 struct gpio_device *gdev;
2061 VALIDATE_DESC(desc);
2064 if (try_module_get(gdev->owner)) {
2065 ret = gpiod_request_commit(desc, label);
2067 module_put(gdev->owner);
2069 get_device(&gdev->dev);
2073 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2078 static bool gpiod_free_commit(struct gpio_desc *desc)
2081 unsigned long flags;
2082 struct gpio_chip *gc;
2086 gpiod_unexport(desc);
2088 spin_lock_irqsave(&gpio_lock, flags);
2090 gc = desc->gdev->chip;
2091 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2093 spin_unlock_irqrestore(&gpio_lock, flags);
2094 might_sleep_if(gc->can_sleep);
2095 gc->free(gc, gpio_chip_hwgpio(desc));
2096 spin_lock_irqsave(&gpio_lock, flags);
2098 kfree_const(desc->label);
2099 desc_set_label(desc, NULL);
2100 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2101 clear_bit(FLAG_REQUESTED, &desc->flags);
2102 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2103 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2104 clear_bit(FLAG_PULL_UP, &desc->flags);
2105 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2106 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2107 clear_bit(FLAG_EDGE_RISING, &desc->flags);
2108 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
2109 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2110 #ifdef CONFIG_OF_DYNAMIC
2113 #ifdef CONFIG_GPIO_CDEV
2114 WRITE_ONCE(desc->debounce_period_us, 0);
2119 spin_unlock_irqrestore(&gpio_lock, flags);
2120 blocking_notifier_call_chain(&desc->gdev->notifier,
2121 GPIOLINE_CHANGED_RELEASED, desc);
2126 void gpiod_free(struct gpio_desc *desc)
2128 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2129 module_put(desc->gdev->owner);
2130 put_device(&desc->gdev->dev);
2132 WARN_ON(extra_checks);
2137 * gpiochip_is_requested - return string iff signal was requested
2138 * @gc: controller managing the signal
2139 * @offset: of signal within controller's 0..(ngpio - 1) range
2141 * Returns NULL if the GPIO is not currently requested, else a string.
2142 * The string returned is the label passed to gpio_request(); if none has been
2143 * passed it is a meaningless, non-NULL constant.
2145 * This function is for use by GPIO controller drivers. The label can
2146 * help with diagnostics, and knowing that the signal is used as a GPIO
2147 * can help avoid accidentally multiplexing it to another controller.
2149 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned offset)
2151 struct gpio_desc *desc;
2153 if (offset >= gc->ngpio)
2156 desc = gpiochip_get_desc(gc, offset);
2160 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2164 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2167 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2169 * @hwnum: hardware number of the GPIO for which to request the descriptor
2170 * @label: label for the GPIO
2171 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2172 * specify things like line inversion semantics with the machine flags
2173 * such as GPIO_OUT_LOW
2174 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2175 * can be used to specify consumer semantics such as open drain
2177 * Function allows GPIO chip drivers to request and use their own GPIO
2178 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2179 * function will not increase reference count of the GPIO chip module. This
2180 * allows the GPIO chip module to be unloaded as needed (we assume that the
2181 * GPIO chip driver handles freeing the GPIOs it has requested).
2184 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2187 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2190 enum gpio_lookup_flags lflags,
2191 enum gpiod_flags dflags)
2193 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2197 chip_err(gc, "failed to get GPIO descriptor\n");
2201 ret = gpiod_request_commit(desc, label);
2203 return ERR_PTR(ret);
2205 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2207 chip_err(gc, "setup of own GPIO %s failed\n", label);
2208 gpiod_free_commit(desc);
2209 return ERR_PTR(ret);
2214 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2217 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2218 * @desc: GPIO descriptor to free
2220 * Function frees the given GPIO requested previously with
2221 * gpiochip_request_own_desc().
2223 void gpiochip_free_own_desc(struct gpio_desc *desc)
2226 gpiod_free_commit(desc);
2228 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2231 * Drivers MUST set GPIO direction before making get/set calls. In
2232 * some cases this is done in early boot, before IRQs are enabled.
2234 * As a rule these aren't called more than once (except for drivers
2235 * using the open-drain emulation idiom) so these are natural places
2236 * to accumulate extra debugging checks. Note that we can't (yet)
2237 * rely on gpio_request() having been called beforehand.
2240 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2241 unsigned long config)
2243 if (!gc->set_config)
2246 return gc->set_config(gc, offset, config);
2249 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2251 struct gpio_chip *gc = desc->gdev->chip;
2252 unsigned long config;
2256 case PIN_CONFIG_BIAS_PULL_DOWN:
2257 case PIN_CONFIG_BIAS_PULL_UP:
2265 config = PIN_CONF_PACKED(mode, arg);
2266 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2269 static int gpio_set_bias(struct gpio_desc *desc)
2274 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2275 bias = PIN_CONFIG_BIAS_DISABLE;
2276 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2277 bias = PIN_CONFIG_BIAS_PULL_UP;
2278 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2279 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2282 ret = gpio_set_config(desc, bias);
2283 if (ret != -ENOTSUPP)
2290 * gpiod_direction_input - set the GPIO direction to input
2291 * @desc: GPIO to set to input
2293 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2294 * be called safely on it.
2296 * Return 0 in case of success, else an error code.
2298 int gpiod_direction_input(struct gpio_desc *desc)
2300 struct gpio_chip *gc;
2303 VALIDATE_DESC(desc);
2304 gc = desc->gdev->chip;
2307 * It is legal to have no .get() and .direction_input() specified if
2308 * the chip is output-only, but you can't specify .direction_input()
2309 * and not support the .get() operation, that doesn't make sense.
2311 if (!gc->get && gc->direction_input) {
2313 "%s: missing get() but have direction_input()\n",
2319 * If we have a .direction_input() callback, things are simple,
2320 * just call it. Else we are some input-only chip so try to check the
2321 * direction (if .get_direction() is supported) else we silently
2322 * assume we are in input mode after this.
2324 if (gc->direction_input) {
2325 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2326 } else if (gc->get_direction &&
2327 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2329 "%s: missing direction_input() operation and line is output\n",
2334 clear_bit(FLAG_IS_OUT, &desc->flags);
2335 ret = gpio_set_bias(desc);
2338 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2342 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2344 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2346 struct gpio_chip *gc = desc->gdev->chip;
2351 * It's OK not to specify .direction_output() if the gpiochip is
2352 * output-only, but if there is then not even a .set() operation it
2353 * is pretty tricky to drive the output line.
2355 if (!gc->set && !gc->direction_output) {
2357 "%s: missing set() and direction_output() operations\n",
2362 if (gc->direction_output) {
2363 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2365 /* Check that we are in output mode if we can */
2366 if (gc->get_direction &&
2367 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2369 "%s: missing direction_output() operation\n",
2374 * If we can't actively set the direction, we are some
2375 * output-only chip, so just drive the output as desired.
2377 gc->set(gc, gpio_chip_hwgpio(desc), val);
2381 set_bit(FLAG_IS_OUT, &desc->flags);
2382 trace_gpio_value(desc_to_gpio(desc), 0, val);
2383 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2388 * gpiod_direction_output_raw - set the GPIO direction to output
2389 * @desc: GPIO to set to output
2390 * @value: initial output value of the GPIO
2392 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2393 * be called safely on it. The initial value of the output must be specified
2394 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2396 * Return 0 in case of success, else an error code.
2398 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2400 VALIDATE_DESC(desc);
2401 return gpiod_direction_output_raw_commit(desc, value);
2403 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2406 * gpiod_direction_output - set the GPIO direction to output
2407 * @desc: GPIO to set to output
2408 * @value: initial output value of the GPIO
2410 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2411 * be called safely on it. The initial value of the output must be specified
2412 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2415 * Return 0 in case of success, else an error code.
2417 int gpiod_direction_output(struct gpio_desc *desc, int value)
2421 VALIDATE_DESC(desc);
2422 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2427 /* GPIOs used for enabled IRQs shall not be set as output */
2428 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2429 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2431 "%s: tried to set a GPIO tied to an IRQ as output\n",
2436 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2437 /* First see if we can enable open drain in hardware */
2438 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2440 goto set_output_value;
2441 /* Emulate open drain by not actively driving the line high */
2443 ret = gpiod_direction_input(desc);
2444 goto set_output_flag;
2447 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2448 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2450 goto set_output_value;
2451 /* Emulate open source by not actively driving the line low */
2453 ret = gpiod_direction_input(desc);
2454 goto set_output_flag;
2457 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2461 ret = gpio_set_bias(desc);
2464 return gpiod_direction_output_raw_commit(desc, value);
2468 * When emulating open-source or open-drain functionalities by not
2469 * actively driving the line (setting mode to input) we still need to
2470 * set the IS_OUT flag or otherwise we won't be able to set the line
2474 set_bit(FLAG_IS_OUT, &desc->flags);
2477 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2480 * gpiod_set_config - sets @config for a GPIO
2481 * @desc: descriptor of the GPIO for which to set the configuration
2482 * @config: Same packed config format as generic pinconf
2485 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2488 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2490 struct gpio_chip *gc;
2492 VALIDATE_DESC(desc);
2493 gc = desc->gdev->chip;
2495 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2497 EXPORT_SYMBOL_GPL(gpiod_set_config);
2500 * gpiod_set_debounce - sets @debounce time for a GPIO
2501 * @desc: descriptor of the GPIO for which to set debounce time
2502 * @debounce: debounce time in microseconds
2505 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2508 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2510 unsigned long config;
2512 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2513 return gpiod_set_config(desc, config);
2515 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2518 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2519 * @desc: descriptor of the GPIO for which to configure persistence
2520 * @transitory: True to lose state on suspend or reset, false for persistence
2523 * 0 on success, otherwise a negative error code.
2525 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2527 struct gpio_chip *gc;
2528 unsigned long packed;
2532 VALIDATE_DESC(desc);
2534 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2535 * persistence state.
2537 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2539 /* If the driver supports it, set the persistence state now */
2540 gc = desc->gdev->chip;
2541 if (!gc->set_config)
2544 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2546 gpio = gpio_chip_hwgpio(desc);
2547 rc = gpio_do_set_config(gc, gpio, packed);
2548 if (rc == -ENOTSUPP) {
2549 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2556 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2559 * gpiod_is_active_low - test whether a GPIO is active-low or not
2560 * @desc: the gpio descriptor to test
2562 * Returns 1 if the GPIO is active-low, 0 otherwise.
2564 int gpiod_is_active_low(const struct gpio_desc *desc)
2566 VALIDATE_DESC(desc);
2567 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2569 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2572 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2573 * @desc: the gpio descriptor to change
2575 void gpiod_toggle_active_low(struct gpio_desc *desc)
2577 VALIDATE_DESC_VOID(desc);
2578 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2580 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2582 /* I/O calls are only valid after configuration completed; the relevant
2583 * "is this a valid GPIO" error checks should already have been done.
2585 * "Get" operations are often inlinable as reading a pin value register,
2586 * and masking the relevant bit in that register.
2588 * When "set" operations are inlinable, they involve writing that mask to
2589 * one register to set a low value, or a different register to set it high.
2590 * Otherwise locking is needed, so there may be little value to inlining.
2592 *------------------------------------------------------------------------
2594 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2595 * have requested the GPIO. That can include implicit requesting by
2596 * a direction setting call. Marking a gpio as requested locks its chip
2597 * in memory, guaranteeing that these table lookups need no more locking
2598 * and that gpiochip_remove() will fail.
2600 * REVISIT when debugging, consider adding some instrumentation to ensure
2601 * that the GPIO was actually requested.
2604 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2606 struct gpio_chip *gc;
2610 gc = desc->gdev->chip;
2611 offset = gpio_chip_hwgpio(desc);
2612 value = gc->get ? gc->get(gc, offset) : -EIO;
2613 value = value < 0 ? value : !!value;
2614 trace_gpio_value(desc_to_gpio(desc), 1, value);
2618 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2619 unsigned long *mask, unsigned long *bits)
2621 if (gc->get_multiple) {
2622 return gc->get_multiple(gc, mask, bits);
2623 } else if (gc->get) {
2626 for_each_set_bit(i, mask, gc->ngpio) {
2627 value = gc->get(gc, i);
2630 __assign_bit(i, bits, value);
2637 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2638 unsigned int array_size,
2639 struct gpio_desc **desc_array,
2640 struct gpio_array *array_info,
2641 unsigned long *value_bitmap)
2646 * Validate array_info against desc_array and its size.
2647 * It should immediately follow desc_array if both
2648 * have been obtained from the same gpiod_get_array() call.
2650 if (array_info && array_info->desc == desc_array &&
2651 array_size <= array_info->size &&
2652 (void *)array_info == desc_array + array_info->size) {
2654 WARN_ON(array_info->chip->can_sleep);
2656 ret = gpio_chip_get_multiple(array_info->chip,
2657 array_info->get_mask,
2662 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2663 bitmap_xor(value_bitmap, value_bitmap,
2664 array_info->invert_mask, array_size);
2666 i = find_first_zero_bit(array_info->get_mask, array_size);
2667 if (i == array_size)
2673 while (i < array_size) {
2674 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2675 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2676 unsigned long *mask, *bits;
2679 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2682 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2684 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2689 bits = mask + BITS_TO_LONGS(gc->ngpio);
2690 bitmap_zero(mask, gc->ngpio);
2693 WARN_ON(gc->can_sleep);
2695 /* collect all inputs belonging to the same chip */
2698 const struct gpio_desc *desc = desc_array[i];
2699 int hwgpio = gpio_chip_hwgpio(desc);
2701 __set_bit(hwgpio, mask);
2705 i = find_next_zero_bit(array_info->get_mask,
2707 } while ((i < array_size) &&
2708 (desc_array[i]->gdev->chip == gc));
2710 ret = gpio_chip_get_multiple(gc, mask, bits);
2712 if (mask != fastpath)
2717 for (j = first; j < i; ) {
2718 const struct gpio_desc *desc = desc_array[j];
2719 int hwgpio = gpio_chip_hwgpio(desc);
2720 int value = test_bit(hwgpio, bits);
2722 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2724 __assign_bit(j, value_bitmap, value);
2725 trace_gpio_value(desc_to_gpio(desc), 1, value);
2729 j = find_next_zero_bit(array_info->get_mask, i,
2733 if (mask != fastpath)
2740 * gpiod_get_raw_value() - return a gpio's raw value
2741 * @desc: gpio whose value will be returned
2743 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2744 * its ACTIVE_LOW status, or negative errno on failure.
2746 * This function can be called from contexts where we cannot sleep, and will
2747 * complain if the GPIO chip functions potentially sleep.
2749 int gpiod_get_raw_value(const struct gpio_desc *desc)
2751 VALIDATE_DESC(desc);
2752 /* Should be using gpiod_get_raw_value_cansleep() */
2753 WARN_ON(desc->gdev->chip->can_sleep);
2754 return gpiod_get_raw_value_commit(desc);
2756 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2759 * gpiod_get_value() - return a gpio's value
2760 * @desc: gpio whose value will be returned
2762 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2763 * account, or negative errno on failure.
2765 * This function can be called from contexts where we cannot sleep, and will
2766 * complain if the GPIO chip functions potentially sleep.
2768 int gpiod_get_value(const struct gpio_desc *desc)
2772 VALIDATE_DESC(desc);
2773 /* Should be using gpiod_get_value_cansleep() */
2774 WARN_ON(desc->gdev->chip->can_sleep);
2776 value = gpiod_get_raw_value_commit(desc);
2780 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2785 EXPORT_SYMBOL_GPL(gpiod_get_value);
2788 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2789 * @array_size: number of elements in the descriptor array / value bitmap
2790 * @desc_array: array of GPIO descriptors whose values will be read
2791 * @array_info: information on applicability of fast bitmap processing path
2792 * @value_bitmap: bitmap to store the read values
2794 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2795 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2796 * else an error code.
2798 * This function can be called from contexts where we cannot sleep,
2799 * and it will complain if the GPIO chip functions potentially sleep.
2801 int gpiod_get_raw_array_value(unsigned int array_size,
2802 struct gpio_desc **desc_array,
2803 struct gpio_array *array_info,
2804 unsigned long *value_bitmap)
2808 return gpiod_get_array_value_complex(true, false, array_size,
2809 desc_array, array_info,
2812 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2815 * gpiod_get_array_value() - read values from an array of GPIOs
2816 * @array_size: number of elements in the descriptor array / value bitmap
2817 * @desc_array: array of GPIO descriptors whose values will be read
2818 * @array_info: information on applicability of fast bitmap processing path
2819 * @value_bitmap: bitmap to store the read values
2821 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2822 * into account. Return 0 in case of success, else an error code.
2824 * This function can be called from contexts where we cannot sleep,
2825 * and it will complain if the GPIO chip functions potentially sleep.
2827 int gpiod_get_array_value(unsigned int array_size,
2828 struct gpio_desc **desc_array,
2829 struct gpio_array *array_info,
2830 unsigned long *value_bitmap)
2834 return gpiod_get_array_value_complex(false, false, array_size,
2835 desc_array, array_info,
2838 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2841 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2842 * @desc: gpio descriptor whose state need to be set.
2843 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2845 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2848 struct gpio_chip *gc = desc->gdev->chip;
2849 int offset = gpio_chip_hwgpio(desc);
2852 ret = gc->direction_input(gc, offset);
2854 ret = gc->direction_output(gc, offset, 0);
2856 set_bit(FLAG_IS_OUT, &desc->flags);
2858 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2861 "%s: Error in set_value for open drain err %d\n",
2866 * _gpio_set_open_source_value() - Set the open source gpio's value.
2867 * @desc: gpio descriptor whose state need to be set.
2868 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2870 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2873 struct gpio_chip *gc = desc->gdev->chip;
2874 int offset = gpio_chip_hwgpio(desc);
2877 ret = gc->direction_output(gc, offset, 1);
2879 set_bit(FLAG_IS_OUT, &desc->flags);
2881 ret = gc->direction_input(gc, offset);
2883 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2886 "%s: Error in set_value for open source err %d\n",
2890 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2892 struct gpio_chip *gc;
2894 gc = desc->gdev->chip;
2895 trace_gpio_value(desc_to_gpio(desc), 0, value);
2896 gc->set(gc, gpio_chip_hwgpio(desc), value);
2900 * set multiple outputs on the same chip;
2901 * use the chip's set_multiple function if available;
2902 * otherwise set the outputs sequentially;
2903 * @chip: the GPIO chip we operate on
2904 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2905 * defines which outputs are to be changed
2906 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2907 * defines the values the outputs specified by mask are to be set to
2909 static void gpio_chip_set_multiple(struct gpio_chip *gc,
2910 unsigned long *mask, unsigned long *bits)
2912 if (gc->set_multiple) {
2913 gc->set_multiple(gc, mask, bits);
2917 /* set outputs if the corresponding mask bit is set */
2918 for_each_set_bit(i, mask, gc->ngpio)
2919 gc->set(gc, i, test_bit(i, bits));
2923 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
2924 unsigned int array_size,
2925 struct gpio_desc **desc_array,
2926 struct gpio_array *array_info,
2927 unsigned long *value_bitmap)
2932 * Validate array_info against desc_array and its size.
2933 * It should immediately follow desc_array if both
2934 * have been obtained from the same gpiod_get_array() call.
2936 if (array_info && array_info->desc == desc_array &&
2937 array_size <= array_info->size &&
2938 (void *)array_info == desc_array + array_info->size) {
2940 WARN_ON(array_info->chip->can_sleep);
2942 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2943 bitmap_xor(value_bitmap, value_bitmap,
2944 array_info->invert_mask, array_size);
2946 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2949 i = find_first_zero_bit(array_info->set_mask, array_size);
2950 if (i == array_size)
2956 while (i < array_size) {
2957 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2958 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2959 unsigned long *mask, *bits;
2962 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2965 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2967 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2972 bits = mask + BITS_TO_LONGS(gc->ngpio);
2973 bitmap_zero(mask, gc->ngpio);
2976 WARN_ON(gc->can_sleep);
2979 struct gpio_desc *desc = desc_array[i];
2980 int hwgpio = gpio_chip_hwgpio(desc);
2981 int value = test_bit(i, value_bitmap);
2984 * Pins applicable for fast input but not for
2985 * fast output processing may have been already
2986 * inverted inside the fast path, skip them.
2988 if (!raw && !(array_info &&
2989 test_bit(i, array_info->invert_mask)) &&
2990 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2992 trace_gpio_value(desc_to_gpio(desc), 0, value);
2994 * collect all normal outputs belonging to the same chip
2995 * open drain and open source outputs are set individually
2997 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2998 gpio_set_open_drain_value_commit(desc, value);
2999 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3000 gpio_set_open_source_value_commit(desc, value);
3002 __set_bit(hwgpio, mask);
3003 __assign_bit(hwgpio, bits, value);
3009 i = find_next_zero_bit(array_info->set_mask,
3011 } while ((i < array_size) &&
3012 (desc_array[i]->gdev->chip == gc));
3013 /* push collected bits to outputs */
3015 gpio_chip_set_multiple(gc, mask, bits);
3017 if (mask != fastpath)
3024 * gpiod_set_raw_value() - assign a gpio's raw value
3025 * @desc: gpio whose value will be assigned
3026 * @value: value to assign
3028 * Set the raw value of the GPIO, i.e. the value of its physical line without
3029 * regard for its ACTIVE_LOW status.
3031 * This function can be called from contexts where we cannot sleep, and will
3032 * complain if the GPIO chip functions potentially sleep.
3034 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3036 VALIDATE_DESC_VOID(desc);
3037 /* Should be using gpiod_set_raw_value_cansleep() */
3038 WARN_ON(desc->gdev->chip->can_sleep);
3039 gpiod_set_raw_value_commit(desc, value);
3041 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3044 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3045 * @desc: the descriptor to set the value on
3046 * @value: value to set
3048 * This sets the value of a GPIO line backing a descriptor, applying
3049 * different semantic quirks like active low and open drain/source
3052 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3054 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3056 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3057 gpio_set_open_drain_value_commit(desc, value);
3058 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3059 gpio_set_open_source_value_commit(desc, value);
3061 gpiod_set_raw_value_commit(desc, value);
3065 * gpiod_set_value() - assign a gpio's value
3066 * @desc: gpio whose value will be assigned
3067 * @value: value to assign
3069 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3070 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3072 * This function can be called from contexts where we cannot sleep, and will
3073 * complain if the GPIO chip functions potentially sleep.
3075 void gpiod_set_value(struct gpio_desc *desc, int value)
3077 VALIDATE_DESC_VOID(desc);
3078 /* Should be using gpiod_set_value_cansleep() */
3079 WARN_ON(desc->gdev->chip->can_sleep);
3080 gpiod_set_value_nocheck(desc, value);
3082 EXPORT_SYMBOL_GPL(gpiod_set_value);
3085 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3086 * @array_size: number of elements in the descriptor array / value bitmap
3087 * @desc_array: array of GPIO descriptors whose values will be assigned
3088 * @array_info: information on applicability of fast bitmap processing path
3089 * @value_bitmap: bitmap of values to assign
3091 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3092 * without regard for their ACTIVE_LOW status.
3094 * This function can be called from contexts where we cannot sleep, and will
3095 * complain if the GPIO chip functions potentially sleep.
3097 int gpiod_set_raw_array_value(unsigned int array_size,
3098 struct gpio_desc **desc_array,
3099 struct gpio_array *array_info,
3100 unsigned long *value_bitmap)
3104 return gpiod_set_array_value_complex(true, false, array_size,
3105 desc_array, array_info, value_bitmap);
3107 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3110 * gpiod_set_array_value() - assign values to an array of GPIOs
3111 * @array_size: number of elements in the descriptor array / value bitmap
3112 * @desc_array: array of GPIO descriptors whose values will be assigned
3113 * @array_info: information on applicability of fast bitmap processing path
3114 * @value_bitmap: bitmap of values to assign
3116 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3119 * This function can be called from contexts where we cannot sleep, and will
3120 * complain if the GPIO chip functions potentially sleep.
3122 int gpiod_set_array_value(unsigned int array_size,
3123 struct gpio_desc **desc_array,
3124 struct gpio_array *array_info,
3125 unsigned long *value_bitmap)
3129 return gpiod_set_array_value_complex(false, false, array_size,
3130 desc_array, array_info,
3133 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3136 * gpiod_cansleep() - report whether gpio value access may sleep
3137 * @desc: gpio to check
3140 int gpiod_cansleep(const struct gpio_desc *desc)
3142 VALIDATE_DESC(desc);
3143 return desc->gdev->chip->can_sleep;
3145 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3148 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3149 * @desc: gpio to set the consumer name on
3150 * @name: the new consumer name
3152 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3154 VALIDATE_DESC(desc);
3156 name = kstrdup_const(name, GFP_KERNEL);
3161 kfree_const(desc->label);
3162 desc_set_label(desc, name);
3166 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3169 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3170 * @desc: gpio whose IRQ will be returned (already requested)
3172 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3175 int gpiod_to_irq(const struct gpio_desc *desc)
3177 struct gpio_chip *gc;
3181 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3182 * requires this function to not return zero on an invalid descriptor
3183 * but rather a negative error number.
3185 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3188 gc = desc->gdev->chip;
3189 offset = gpio_chip_hwgpio(desc);
3191 int retirq = gc->to_irq(gc, offset);
3193 /* Zero means NO_IRQ */
3201 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3204 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3205 * @gc: the chip the GPIO to lock belongs to
3206 * @offset: the offset of the GPIO to lock as IRQ
3208 * This is used directly by GPIO drivers that want to lock down
3209 * a certain GPIO line to be used for IRQs.
3211 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3213 struct gpio_desc *desc;
3215 desc = gpiochip_get_desc(gc, offset);
3217 return PTR_ERR(desc);
3220 * If it's fast: flush the direction setting if something changed
3223 if (!gc->can_sleep && gc->get_direction) {
3224 int dir = gpiod_get_direction(desc);
3227 chip_err(gc, "%s: cannot get GPIO direction\n",
3233 /* To be valid for IRQ the line needs to be input or open drain */
3234 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3235 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3237 "%s: tried to flag a GPIO set as output for IRQ\n",
3242 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3243 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3246 * If the consumer has not set up a label (such as when the
3247 * IRQ is referenced from .to_irq()) we set up a label here
3248 * so it is clear this is used as an interrupt.
3251 desc_set_label(desc, "interrupt");
3255 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3258 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3259 * @gc: the chip the GPIO to lock belongs to
3260 * @offset: the offset of the GPIO to lock as IRQ
3262 * This is used directly by GPIO drivers that want to indicate
3263 * that a certain GPIO is no longer used exclusively for IRQ.
3265 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3267 struct gpio_desc *desc;
3269 desc = gpiochip_get_desc(gc, offset);
3273 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3274 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3276 /* If we only had this marking, erase it */
3277 if (desc->label && !strcmp(desc->label, "interrupt"))
3278 desc_set_label(desc, NULL);
3280 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3282 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3284 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3286 if (!IS_ERR(desc) &&
3287 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3288 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3290 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3292 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3294 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3296 if (!IS_ERR(desc) &&
3297 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3299 * We must not be output when using IRQ UNLESS we are
3302 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3303 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3304 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3307 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3309 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3311 if (offset >= gc->ngpio)
3314 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3316 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3318 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3322 if (!try_module_get(gc->gpiodev->owner))
3325 ret = gpiochip_lock_as_irq(gc, offset);
3327 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3328 module_put(gc->gpiodev->owner);
3333 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3335 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3337 gpiochip_unlock_as_irq(gc, offset);
3338 module_put(gc->gpiodev->owner);
3340 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3342 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3344 if (offset >= gc->ngpio)
3347 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3349 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3351 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3353 if (offset >= gc->ngpio)
3356 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3358 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3360 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3362 if (offset >= gc->ngpio)
3365 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3367 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3370 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3371 * @desc: gpio whose value will be returned
3373 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3374 * its ACTIVE_LOW status, or negative errno on failure.
3376 * This function is to be called from contexts that can sleep.
3378 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3380 might_sleep_if(extra_checks);
3381 VALIDATE_DESC(desc);
3382 return gpiod_get_raw_value_commit(desc);
3384 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3387 * gpiod_get_value_cansleep() - return a gpio's value
3388 * @desc: gpio whose value will be returned
3390 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3391 * account, or negative errno on failure.
3393 * This function is to be called from contexts that can sleep.
3395 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3399 might_sleep_if(extra_checks);
3400 VALIDATE_DESC(desc);
3401 value = gpiod_get_raw_value_commit(desc);
3405 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3410 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3413 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3414 * @array_size: number of elements in the descriptor array / value bitmap
3415 * @desc_array: array of GPIO descriptors whose values will be read
3416 * @array_info: information on applicability of fast bitmap processing path
3417 * @value_bitmap: bitmap to store the read values
3419 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3420 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3421 * else an error code.
3423 * This function is to be called from contexts that can sleep.
3425 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3426 struct gpio_desc **desc_array,
3427 struct gpio_array *array_info,
3428 unsigned long *value_bitmap)
3430 might_sleep_if(extra_checks);
3433 return gpiod_get_array_value_complex(true, true, array_size,
3434 desc_array, array_info,
3437 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3440 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3441 * @array_size: number of elements in the descriptor array / value bitmap
3442 * @desc_array: array of GPIO descriptors whose values will be read
3443 * @array_info: information on applicability of fast bitmap processing path
3444 * @value_bitmap: bitmap to store the read values
3446 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3447 * into account. Return 0 in case of success, else an error code.
3449 * This function is to be called from contexts that can sleep.
3451 int gpiod_get_array_value_cansleep(unsigned int array_size,
3452 struct gpio_desc **desc_array,
3453 struct gpio_array *array_info,
3454 unsigned long *value_bitmap)
3456 might_sleep_if(extra_checks);
3459 return gpiod_get_array_value_complex(false, true, array_size,
3460 desc_array, array_info,
3463 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3466 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3467 * @desc: gpio whose value will be assigned
3468 * @value: value to assign
3470 * Set the raw value of the GPIO, i.e. the value of its physical line without
3471 * regard for its ACTIVE_LOW status.
3473 * This function is to be called from contexts that can sleep.
3475 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3477 might_sleep_if(extra_checks);
3478 VALIDATE_DESC_VOID(desc);
3479 gpiod_set_raw_value_commit(desc, value);
3481 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3484 * gpiod_set_value_cansleep() - assign a gpio's value
3485 * @desc: gpio whose value will be assigned
3486 * @value: value to assign
3488 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3491 * This function is to be called from contexts that can sleep.
3493 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3495 might_sleep_if(extra_checks);
3496 VALIDATE_DESC_VOID(desc);
3497 gpiod_set_value_nocheck(desc, value);
3499 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3502 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3503 * @array_size: number of elements in the descriptor array / value bitmap
3504 * @desc_array: array of GPIO descriptors whose values will be assigned
3505 * @array_info: information on applicability of fast bitmap processing path
3506 * @value_bitmap: bitmap of values to assign
3508 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3509 * without regard for their ACTIVE_LOW status.
3511 * This function is to be called from contexts that can sleep.
3513 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3514 struct gpio_desc **desc_array,
3515 struct gpio_array *array_info,
3516 unsigned long *value_bitmap)
3518 might_sleep_if(extra_checks);
3521 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3522 array_info, value_bitmap);
3524 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3527 * gpiod_add_lookup_tables() - register GPIO device consumers
3528 * @tables: list of tables of consumers to register
3529 * @n: number of tables in the list
3531 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3535 mutex_lock(&gpio_lookup_lock);
3537 for (i = 0; i < n; i++)
3538 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3540 mutex_unlock(&gpio_lookup_lock);
3544 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3545 * @array_size: number of elements in the descriptor array / value bitmap
3546 * @desc_array: array of GPIO descriptors whose values will be assigned
3547 * @array_info: information on applicability of fast bitmap processing path
3548 * @value_bitmap: bitmap of values to assign
3550 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3553 * This function is to be called from contexts that can sleep.
3555 int gpiod_set_array_value_cansleep(unsigned int array_size,
3556 struct gpio_desc **desc_array,
3557 struct gpio_array *array_info,
3558 unsigned long *value_bitmap)
3560 might_sleep_if(extra_checks);
3563 return gpiod_set_array_value_complex(false, true, array_size,
3564 desc_array, array_info,
3567 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3570 * gpiod_add_lookup_table() - register GPIO device consumers
3571 * @table: table of consumers to register
3573 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3575 mutex_lock(&gpio_lookup_lock);
3577 list_add_tail(&table->list, &gpio_lookup_list);
3579 mutex_unlock(&gpio_lookup_lock);
3581 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3584 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3585 * @table: table of consumers to unregister
3587 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3589 mutex_lock(&gpio_lookup_lock);
3591 list_del(&table->list);
3593 mutex_unlock(&gpio_lookup_lock);
3595 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3598 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3599 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3601 void gpiod_add_hogs(struct gpiod_hog *hogs)
3603 struct gpio_chip *gc;
3604 struct gpiod_hog *hog;
3606 mutex_lock(&gpio_machine_hogs_mutex);
3608 for (hog = &hogs[0]; hog->chip_label; hog++) {
3609 list_add_tail(&hog->list, &gpio_machine_hogs);
3612 * The chip may have been registered earlier, so check if it
3613 * exists and, if so, try to hog the line now.
3615 gc = find_chip_by_name(hog->chip_label);
3617 gpiochip_machine_hog(gc, hog);
3620 mutex_unlock(&gpio_machine_hogs_mutex);
3622 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3624 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3626 const char *dev_id = dev ? dev_name(dev) : NULL;
3627 struct gpiod_lookup_table *table;
3629 mutex_lock(&gpio_lookup_lock);
3631 list_for_each_entry(table, &gpio_lookup_list, list) {
3632 if (table->dev_id && dev_id) {
3634 * Valid strings on both ends, must be identical to have
3637 if (!strcmp(table->dev_id, dev_id))
3641 * One of the pointers is NULL, so both must be to have
3644 if (dev_id == table->dev_id)
3651 mutex_unlock(&gpio_lookup_lock);
3655 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3656 unsigned int idx, unsigned long *flags)
3658 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3659 struct gpiod_lookup_table *table;
3660 struct gpiod_lookup *p;
3662 table = gpiod_find_lookup_table(dev);
3666 for (p = &table->table[0]; p->key; p++) {
3667 struct gpio_chip *gc;
3669 /* idx must always match exactly */
3673 /* If the lookup entry has a con_id, require exact match */
3674 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3677 if (p->chip_hwnum == U16_MAX) {
3678 desc = gpio_name_to_desc(p->key);
3684 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3686 return ERR_PTR(-EPROBE_DEFER);
3689 gc = find_chip_by_name(p->key);
3693 * As the lookup table indicates a chip with
3694 * p->key should exist, assume it may
3695 * still appear later and let the interested
3696 * consumer be probed again or let the Deferred
3697 * Probe infrastructure handle the error.
3699 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3701 return ERR_PTR(-EPROBE_DEFER);
3704 if (gc->ngpio <= p->chip_hwnum) {
3706 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3707 idx, p->chip_hwnum, gc->ngpio - 1,
3709 return ERR_PTR(-EINVAL);
3712 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3721 static int platform_gpio_count(struct device *dev, const char *con_id)
3723 struct gpiod_lookup_table *table;
3724 struct gpiod_lookup *p;
3725 unsigned int count = 0;
3727 table = gpiod_find_lookup_table(dev);
3731 for (p = &table->table[0]; p->key; p++) {
3732 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3733 (!con_id && !p->con_id))
3743 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3744 * @fwnode: handle of the firmware node
3745 * @con_id: function within the GPIO consumer
3746 * @index: index of the GPIO to obtain for the consumer
3747 * @flags: GPIO initialization flags
3748 * @label: label to attach to the requested GPIO
3750 * This function can be used for drivers that get their configuration
3751 * from opaque firmware.
3753 * The function properly finds the corresponding GPIO using whatever is the
3754 * underlying firmware interface and then makes sure that the GPIO
3755 * descriptor is requested before it is returned to the caller.
3758 * On successful request the GPIO pin is configured in accordance with
3761 * In case of error an ERR_PTR() is returned.
3763 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3764 const char *con_id, int index,
3765 enum gpiod_flags flags,
3768 struct gpio_desc *desc;
3769 char prop_name[32]; /* 32 is max size of property name */
3772 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3774 snprintf(prop_name, sizeof(prop_name), "%s-%s",
3775 con_id, gpio_suffixes[i]);
3777 snprintf(prop_name, sizeof(prop_name), "%s",
3780 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3782 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
3788 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
3791 * gpiod_count - return the number of GPIOs associated with a device / function
3792 * or -ENOENT if no GPIO has been assigned to the requested function
3793 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3794 * @con_id: function within the GPIO consumer
3796 int gpiod_count(struct device *dev, const char *con_id)
3798 int count = -ENOENT;
3800 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3801 count = of_gpio_get_count(dev, con_id);
3802 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3803 count = acpi_gpio_count(dev, con_id);
3806 count = platform_gpio_count(dev, con_id);
3810 EXPORT_SYMBOL_GPL(gpiod_count);
3813 * gpiod_get - obtain a GPIO for a given GPIO function
3814 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3815 * @con_id: function within the GPIO consumer
3816 * @flags: optional GPIO initialization flags
3818 * Return the GPIO descriptor corresponding to the function con_id of device
3819 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3820 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3822 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3823 enum gpiod_flags flags)
3825 return gpiod_get_index(dev, con_id, 0, flags);
3827 EXPORT_SYMBOL_GPL(gpiod_get);
3830 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3831 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3832 * @con_id: function within the GPIO consumer
3833 * @flags: optional GPIO initialization flags
3835 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3836 * the requested function it will return NULL. This is convenient for drivers
3837 * that need to handle optional GPIOs.
3839 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3841 enum gpiod_flags flags)
3843 return gpiod_get_index_optional(dev, con_id, 0, flags);
3845 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3849 * gpiod_configure_flags - helper function to configure a given GPIO
3850 * @desc: gpio whose value will be assigned
3851 * @con_id: function within the GPIO consumer
3852 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3853 * of_find_gpio() or of_get_gpio_hog()
3854 * @dflags: gpiod_flags - optional GPIO initialization flags
3856 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3857 * requested function and/or index, or another IS_ERR() code if an error
3858 * occurred while trying to acquire the GPIO.
3860 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3861 unsigned long lflags, enum gpiod_flags dflags)
3865 if (lflags & GPIO_ACTIVE_LOW)
3866 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3868 if (lflags & GPIO_OPEN_DRAIN)
3869 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3870 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3872 * This enforces open drain mode from the consumer side.
3873 * This is necessary for some busses like I2C, but the lookup
3874 * should *REALLY* have specified them as open drain in the
3875 * first place, so print a little warning here.
3877 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3879 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3882 if (lflags & GPIO_OPEN_SOURCE)
3883 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3885 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3887 "both pull-up and pull-down enabled, invalid configuration\n");
3891 if (lflags & GPIO_PULL_UP)
3892 set_bit(FLAG_PULL_UP, &desc->flags);
3893 else if (lflags & GPIO_PULL_DOWN)
3894 set_bit(FLAG_PULL_DOWN, &desc->flags);
3896 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3900 /* No particular flag request, return here... */
3901 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3902 gpiod_dbg(desc, "no flags found for %s\n", con_id);
3907 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3908 ret = gpiod_direction_output(desc,
3909 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3911 ret = gpiod_direction_input(desc);
3917 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3918 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3919 * @con_id: function within the GPIO consumer
3920 * @idx: index of the GPIO to obtain in the consumer
3921 * @flags: optional GPIO initialization flags
3923 * This variant of gpiod_get() allows to access GPIOs other than the first
3924 * defined one for functions that define several GPIOs.
3926 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3927 * requested function and/or index, or another IS_ERR() code if an error
3928 * occurred while trying to acquire the GPIO.
3930 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3933 enum gpiod_flags flags)
3935 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3936 struct gpio_desc *desc = NULL;
3938 /* Maybe we have a device name, maybe not */
3939 const char *devname = dev ? dev_name(dev) : "?";
3941 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3944 /* Using device tree? */
3945 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3946 dev_dbg(dev, "using device tree for GPIO lookup\n");
3947 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3948 } else if (ACPI_COMPANION(dev)) {
3949 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3950 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3955 * Either we are not using DT or ACPI, or their lookup did not return
3956 * a result. In that case, use platform lookup as a fallback.
3958 if (!desc || desc == ERR_PTR(-ENOENT)) {
3959 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3960 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3964 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3969 * If a connection label was passed use that, else attempt to use
3970 * the device name as label
3972 ret = gpiod_request(desc, con_id ? con_id : devname);
3974 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
3976 * This happens when there are several consumers for
3977 * the same GPIO line: we just return here without
3978 * further initialization. It is a bit if a hack.
3979 * This is necessary to support fixed regulators.
3981 * FIXME: Make this more sane and safe.
3983 dev_info(dev, "nonexclusive access to GPIO for %s\n",
3984 con_id ? con_id : devname);
3987 return ERR_PTR(ret);
3991 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3993 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3995 return ERR_PTR(ret);
3998 blocking_notifier_call_chain(&desc->gdev->notifier,
3999 GPIOLINE_CHANGED_REQUESTED, desc);
4003 EXPORT_SYMBOL_GPL(gpiod_get_index);
4006 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4007 * @fwnode: handle of the firmware node
4008 * @propname: name of the firmware property representing the GPIO
4009 * @index: index of the GPIO to obtain for the consumer
4010 * @dflags: GPIO initialization flags
4011 * @label: label to attach to the requested GPIO
4013 * This function can be used for drivers that get their configuration
4014 * from opaque firmware.
4016 * The function properly finds the corresponding GPIO using whatever is the
4017 * underlying firmware interface and then makes sure that the GPIO
4018 * descriptor is requested before it is returned to the caller.
4021 * On successful request the GPIO pin is configured in accordance with
4024 * In case of error an ERR_PTR() is returned.
4026 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
4027 const char *propname, int index,
4028 enum gpiod_flags dflags,
4031 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4032 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4036 return ERR_PTR(-EINVAL);
4038 if (is_of_node(fwnode)) {
4039 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4044 } else if (is_acpi_node(fwnode)) {
4045 struct acpi_gpio_info info;
4047 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
4051 acpi_gpio_update_gpiod_flags(&dflags, &info);
4052 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
4055 /* Currently only ACPI takes this path */
4056 ret = gpiod_request(desc, label);
4058 return ERR_PTR(ret);
4060 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4063 return ERR_PTR(ret);
4066 blocking_notifier_call_chain(&desc->gdev->notifier,
4067 GPIOLINE_CHANGED_REQUESTED, desc);
4071 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4074 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4076 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4077 * @con_id: function within the GPIO consumer
4078 * @index: index of the GPIO to obtain in the consumer
4079 * @flags: optional GPIO initialization flags
4081 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4082 * specified index was assigned to the requested function it will return NULL.
4083 * This is convenient for drivers that need to handle optional GPIOs.
4085 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4088 enum gpiod_flags flags)
4090 struct gpio_desc *desc;
4092 desc = gpiod_get_index(dev, con_id, index, flags);
4094 if (PTR_ERR(desc) == -ENOENT)
4100 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4103 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4104 * @desc: gpio whose value will be assigned
4105 * @name: gpio line name
4106 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4107 * of_find_gpio() or of_get_gpio_hog()
4108 * @dflags: gpiod_flags - optional GPIO initialization flags
4110 int gpiod_hog(struct gpio_desc *desc, const char *name,
4111 unsigned long lflags, enum gpiod_flags dflags)
4113 struct gpio_chip *gc;
4114 struct gpio_desc *local_desc;
4118 gc = gpiod_to_chip(desc);
4119 hwnum = gpio_chip_hwgpio(desc);
4121 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4123 if (IS_ERR(local_desc)) {
4124 ret = PTR_ERR(local_desc);
4125 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4126 name, gc->label, hwnum, ret);
4130 /* Mark GPIO as hogged so it can be identified and removed later */
4131 set_bit(FLAG_IS_HOGGED, &desc->flags);
4133 gpiod_info(desc, "hogged as %s%s\n",
4134 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4135 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4136 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4142 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4143 * @gc: gpio chip to act on
4145 static void gpiochip_free_hogs(struct gpio_chip *gc)
4149 for (id = 0; id < gc->ngpio; id++) {
4150 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4151 gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
4156 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4157 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4158 * @con_id: function within the GPIO consumer
4159 * @flags: optional GPIO initialization flags
4161 * This function acquires all the GPIOs defined under a given function.
4163 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4164 * no GPIO has been assigned to the requested function, or another IS_ERR()
4165 * code if an error occurred while trying to acquire the GPIOs.
4167 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4169 enum gpiod_flags flags)
4171 struct gpio_desc *desc;
4172 struct gpio_descs *descs;
4173 struct gpio_array *array_info = NULL;
4174 struct gpio_chip *gc;
4175 int count, bitmap_size;
4177 count = gpiod_count(dev, con_id);
4179 return ERR_PTR(count);
4181 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4183 return ERR_PTR(-ENOMEM);
4185 for (descs->ndescs = 0; descs->ndescs < count; ) {
4186 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4188 gpiod_put_array(descs);
4189 return ERR_CAST(desc);
4192 descs->desc[descs->ndescs] = desc;
4194 gc = gpiod_to_chip(desc);
4196 * If pin hardware number of array member 0 is also 0, select
4197 * its chip as a candidate for fast bitmap processing path.
4199 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4200 struct gpio_descs *array;
4202 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4205 array = kzalloc(struct_size(descs, desc, count) +
4206 struct_size(array_info, invert_mask,
4207 3 * bitmap_size), GFP_KERNEL);
4209 gpiod_put_array(descs);
4210 return ERR_PTR(-ENOMEM);
4213 memcpy(array, descs,
4214 struct_size(descs, desc, descs->ndescs + 1));
4218 array_info = (void *)(descs->desc + count);
4219 array_info->get_mask = array_info->invert_mask +
4221 array_info->set_mask = array_info->get_mask +
4224 array_info->desc = descs->desc;
4225 array_info->size = count;
4226 array_info->chip = gc;
4227 bitmap_set(array_info->get_mask, descs->ndescs,
4228 count - descs->ndescs);
4229 bitmap_set(array_info->set_mask, descs->ndescs,
4230 count - descs->ndescs);
4231 descs->info = array_info;
4233 /* Unmark array members which don't belong to the 'fast' chip */
4234 if (array_info && array_info->chip != gc) {
4235 __clear_bit(descs->ndescs, array_info->get_mask);
4236 __clear_bit(descs->ndescs, array_info->set_mask);
4239 * Detect array members which belong to the 'fast' chip
4240 * but their pins are not in hardware order.
4242 else if (array_info &&
4243 gpio_chip_hwgpio(desc) != descs->ndescs) {
4245 * Don't use fast path if all array members processed so
4246 * far belong to the same chip as this one but its pin
4247 * hardware number is different from its array index.
4249 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4252 __clear_bit(descs->ndescs,
4253 array_info->get_mask);
4254 __clear_bit(descs->ndescs,
4255 array_info->set_mask);
4257 } else if (array_info) {
4258 /* Exclude open drain or open source from fast output */
4259 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4260 gpiochip_line_is_open_source(gc, descs->ndescs))
4261 __clear_bit(descs->ndescs,
4262 array_info->set_mask);
4263 /* Identify 'fast' pins which require invertion */
4264 if (gpiod_is_active_low(desc))
4265 __set_bit(descs->ndescs,
4266 array_info->invert_mask);
4273 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4274 array_info->chip->label, array_info->size,
4275 *array_info->get_mask, *array_info->set_mask,
4276 *array_info->invert_mask);
4279 EXPORT_SYMBOL_GPL(gpiod_get_array);
4282 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4284 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4285 * @con_id: function within the GPIO consumer
4286 * @flags: optional GPIO initialization flags
4288 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4289 * assigned to the requested function it will return NULL.
4291 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4293 enum gpiod_flags flags)
4295 struct gpio_descs *descs;
4297 descs = gpiod_get_array(dev, con_id, flags);
4298 if (PTR_ERR(descs) == -ENOENT)
4303 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4306 * gpiod_put - dispose of a GPIO descriptor
4307 * @desc: GPIO descriptor to dispose of
4309 * No descriptor can be used after gpiod_put() has been called on it.
4311 void gpiod_put(struct gpio_desc *desc)
4316 EXPORT_SYMBOL_GPL(gpiod_put);
4319 * gpiod_put_array - dispose of multiple GPIO descriptors
4320 * @descs: struct gpio_descs containing an array of descriptors
4322 void gpiod_put_array(struct gpio_descs *descs)
4326 for (i = 0; i < descs->ndescs; i++)
4327 gpiod_put(descs->desc[i]);
4331 EXPORT_SYMBOL_GPL(gpiod_put_array);
4333 static int __init gpiolib_dev_init(void)
4337 /* Register GPIO sysfs bus */
4338 ret = bus_register(&gpio_bus_type);
4340 pr_err("gpiolib: could not register GPIO bus type\n");
4344 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4346 pr_err("gpiolib: failed to allocate char dev region\n");
4347 bus_unregister(&gpio_bus_type);
4351 gpiolib_initialized = true;
4352 gpiochip_setup_devs();
4354 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4355 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4356 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4360 core_initcall(gpiolib_dev_init);
4362 #ifdef CONFIG_DEBUG_FS
4364 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4367 struct gpio_chip *gc = gdev->chip;
4368 unsigned gpio = gdev->base;
4369 struct gpio_desc *gdesc = &gdev->descs[0];
4374 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4375 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4377 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4383 gpiod_get_direction(gdesc);
4384 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4385 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4386 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4387 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4388 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4389 is_out ? "out" : "in ",
4390 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
4391 is_irq ? "IRQ " : "",
4392 active_low ? "ACTIVE LOW" : "");
4393 seq_printf(s, "\n");
4397 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4399 unsigned long flags;
4400 struct gpio_device *gdev = NULL;
4401 loff_t index = *pos;
4405 spin_lock_irqsave(&gpio_lock, flags);
4406 list_for_each_entry(gdev, &gpio_devices, list)
4408 spin_unlock_irqrestore(&gpio_lock, flags);
4411 spin_unlock_irqrestore(&gpio_lock, flags);
4416 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4418 unsigned long flags;
4419 struct gpio_device *gdev = v;
4422 spin_lock_irqsave(&gpio_lock, flags);
4423 if (list_is_last(&gdev->list, &gpio_devices))
4426 ret = list_entry(gdev->list.next, struct gpio_device, list);
4427 spin_unlock_irqrestore(&gpio_lock, flags);
4435 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4439 static int gpiolib_seq_show(struct seq_file *s, void *v)
4441 struct gpio_device *gdev = v;
4442 struct gpio_chip *gc = gdev->chip;
4443 struct device *parent;
4446 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4447 dev_name(&gdev->dev));
4451 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4452 dev_name(&gdev->dev),
4453 gdev->base, gdev->base + gdev->ngpio - 1);
4454 parent = gc->parent;
4456 seq_printf(s, ", parent: %s/%s",
4457 parent->bus ? parent->bus->name : "no-bus",
4460 seq_printf(s, ", %s", gc->label);
4462 seq_printf(s, ", can sleep");
4463 seq_printf(s, ":\n");
4466 gc->dbg_show(s, gc);
4468 gpiolib_dbg_show(s, gdev);
4473 static const struct seq_operations gpiolib_sops = {
4474 .start = gpiolib_seq_start,
4475 .next = gpiolib_seq_next,
4476 .stop = gpiolib_seq_stop,
4477 .show = gpiolib_seq_show,
4479 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4481 static int __init gpiolib_debugfs_init(void)
4483 /* /sys/kernel/debug/gpio */
4484 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4487 subsys_initcall(gpiolib_debugfs_init);
4489 #endif /* DEBUG_FS */