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 static int gpiochip_setup_dev(struct gpio_device *gdev)
487 ret = gpiolib_cdev_register(gdev, gpio_devt);
491 ret = gpiochip_sysfs_register(gdev);
493 goto err_remove_device;
495 /* From this point, the .release() function cleans up gpio_device */
496 gdev->dev.release = gpiodevice_release;
497 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
498 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
503 gpiolib_cdev_unregister(gdev);
507 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
509 struct gpio_desc *desc;
512 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
514 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
519 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
522 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
524 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
525 __func__, gc->label, hog->chip_hwnum, rv);
528 static void machine_gpiochip_add(struct gpio_chip *gc)
530 struct gpiod_hog *hog;
532 mutex_lock(&gpio_machine_hogs_mutex);
534 list_for_each_entry(hog, &gpio_machine_hogs, list) {
535 if (!strcmp(gc->label, hog->chip_label))
536 gpiochip_machine_hog(gc, hog);
539 mutex_unlock(&gpio_machine_hogs_mutex);
542 static void gpiochip_setup_devs(void)
544 struct gpio_device *gdev;
547 list_for_each_entry(gdev, &gpio_devices, list) {
548 ret = gpiochip_setup_dev(gdev);
551 "Failed to initialize gpio device (%d)\n", ret);
555 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
556 struct lock_class_key *lock_key,
557 struct lock_class_key *request_key)
563 struct gpio_device *gdev;
566 * First: allocate and populate the internal stat container, and
567 * set up the struct device.
569 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
572 gdev->dev.bus = &gpio_bus_type;
576 gdev->dev.parent = gc->parent;
577 gdev->dev.of_node = gc->parent->of_node;
580 #ifdef CONFIG_OF_GPIO
581 /* If the gpiochip has an assigned OF node this takes precedence */
583 gdev->dev.of_node = gc->of_node;
585 gc->of_node = gdev->dev.of_node;
588 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
593 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
594 device_initialize(&gdev->dev);
595 dev_set_drvdata(&gdev->dev, gdev);
596 if (gc->parent && gc->parent->driver)
597 gdev->owner = gc->parent->driver->owner;
599 /* TODO: remove chip->owner */
600 gdev->owner = gc->owner;
602 gdev->owner = THIS_MODULE;
604 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
610 if (gc->ngpio == 0) {
611 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
616 if (gc->ngpio > FASTPATH_NGPIO)
617 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
618 gc->ngpio, FASTPATH_NGPIO);
620 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
626 gdev->ngpio = gc->ngpio;
629 spin_lock_irqsave(&gpio_lock, flags);
632 * TODO: this allocates a Linux GPIO number base in the global
633 * GPIO numberspace for this chip. In the long run we want to
634 * get *rid* of this numberspace and use only descriptors, but
635 * it may be a pipe dream. It will not happen before we get rid
636 * of the sysfs interface anyways.
639 base = gpiochip_find_base(gc->ngpio);
642 spin_unlock_irqrestore(&gpio_lock, flags);
646 * TODO: it should not be necessary to reflect the assigned
647 * base outside of the GPIO subsystem. Go over drivers and
648 * see if anyone makes use of this, else drop this and assign
655 ret = gpiodev_add_to_list(gdev);
657 spin_unlock_irqrestore(&gpio_lock, flags);
661 for (i = 0; i < gc->ngpio; i++)
662 gdev->descs[i].gdev = gdev;
664 spin_unlock_irqrestore(&gpio_lock, flags);
666 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
668 #ifdef CONFIG_PINCTRL
669 INIT_LIST_HEAD(&gdev->pin_ranges);
673 ret = gpiochip_set_desc_names(gc);
675 ret = devprop_gpiochip_set_names(gc);
677 goto err_remove_from_list;
679 ret = gpiochip_alloc_valid_mask(gc);
681 goto err_remove_from_list;
683 ret = of_gpiochip_add(gc);
685 goto err_free_gpiochip_mask;
687 ret = gpiochip_init_valid_mask(gc);
689 goto err_remove_of_chip;
691 for (i = 0; i < gc->ngpio; i++) {
692 struct gpio_desc *desc = &gdev->descs[i];
694 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
695 assign_bit(FLAG_IS_OUT,
696 &desc->flags, !gc->get_direction(gc, i));
698 assign_bit(FLAG_IS_OUT,
699 &desc->flags, !gc->direction_input);
703 ret = gpiochip_add_pin_ranges(gc);
705 goto err_remove_of_chip;
707 acpi_gpiochip_add(gc);
709 machine_gpiochip_add(gc);
711 ret = gpiochip_irqchip_init_valid_mask(gc);
713 goto err_remove_acpi_chip;
715 ret = gpiochip_irqchip_init_hw(gc);
717 goto err_remove_acpi_chip;
719 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
721 goto err_remove_irqchip_mask;
724 * By first adding the chardev, and then adding the device,
725 * we get a device node entry in sysfs under
726 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
727 * coldplug of device nodes and other udev business.
728 * We can do this only if gpiolib has been initialized.
729 * Otherwise, defer until later.
731 if (gpiolib_initialized) {
732 ret = gpiochip_setup_dev(gdev);
734 goto err_remove_irqchip;
739 gpiochip_irqchip_remove(gc);
740 err_remove_irqchip_mask:
741 gpiochip_irqchip_free_valid_mask(gc);
742 err_remove_acpi_chip:
743 acpi_gpiochip_remove(gc);
745 gpiochip_free_hogs(gc);
746 of_gpiochip_remove(gc);
747 err_free_gpiochip_mask:
748 gpiochip_remove_pin_ranges(gc);
749 gpiochip_free_valid_mask(gc);
750 err_remove_from_list:
751 spin_lock_irqsave(&gpio_lock, flags);
752 list_del(&gdev->list);
753 spin_unlock_irqrestore(&gpio_lock, flags);
755 kfree_const(gdev->label);
759 ida_free(&gpio_ida, gdev->id);
761 /* failures here can mean systems won't boot... */
762 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
763 gdev->base, gdev->base + gdev->ngpio - 1,
764 gc->label ? : "generic", ret);
768 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
771 * gpiochip_get_data() - get per-subdriver data for the chip
775 * The per-subdriver data for the chip.
777 void *gpiochip_get_data(struct gpio_chip *gc)
779 return gc->gpiodev->data;
781 EXPORT_SYMBOL_GPL(gpiochip_get_data);
784 * gpiochip_remove() - unregister a gpio_chip
785 * @gc: the chip to unregister
787 * A gpio_chip with any GPIOs still requested may not be removed.
789 void gpiochip_remove(struct gpio_chip *gc)
791 struct gpio_device *gdev = gc->gpiodev;
795 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
796 gpiochip_sysfs_unregister(gdev);
797 gpiochip_free_hogs(gc);
798 /* Numb the device, cancelling all outstanding operations */
800 gpiochip_irqchip_remove(gc);
801 acpi_gpiochip_remove(gc);
802 of_gpiochip_remove(gc);
803 gpiochip_remove_pin_ranges(gc);
804 gpiochip_free_valid_mask(gc);
806 * We accept no more calls into the driver from this point, so
807 * NULL the driver data pointer
811 spin_lock_irqsave(&gpio_lock, flags);
812 for (i = 0; i < gdev->ngpio; i++) {
813 if (gpiochip_is_requested(gc, i))
816 spin_unlock_irqrestore(&gpio_lock, flags);
818 if (i != gdev->ngpio)
820 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
823 * The gpiochip side puts its use of the device to rest here:
824 * if there are no userspace clients, the chardev and device will
825 * be removed, else it will be dangling until the last user is
828 gpiolib_cdev_unregister(gdev);
829 put_device(&gdev->dev);
831 EXPORT_SYMBOL_GPL(gpiochip_remove);
834 * gpiochip_find() - iterator for locating a specific gpio_chip
835 * @data: data to pass to match function
836 * @match: Callback function to check gpio_chip
838 * Similar to bus_find_device. It returns a reference to a gpio_chip as
839 * determined by a user supplied @match callback. The callback should return
840 * 0 if the device doesn't match and non-zero if it does. If the callback is
841 * non-zero, this function will return to the caller and not iterate over any
844 struct gpio_chip *gpiochip_find(void *data,
845 int (*match)(struct gpio_chip *gc,
848 struct gpio_device *gdev;
849 struct gpio_chip *gc = NULL;
852 spin_lock_irqsave(&gpio_lock, flags);
853 list_for_each_entry(gdev, &gpio_devices, list)
854 if (gdev->chip && match(gdev->chip, data)) {
859 spin_unlock_irqrestore(&gpio_lock, flags);
863 EXPORT_SYMBOL_GPL(gpiochip_find);
865 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
867 const char *name = data;
869 return !strcmp(gc->label, name);
872 static struct gpio_chip *find_chip_by_name(const char *name)
874 return gpiochip_find((void *)name, gpiochip_match_name);
877 #ifdef CONFIG_GPIOLIB_IRQCHIP
880 * The following is irqchip helper code for gpiochips.
883 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
885 struct gpio_irq_chip *girq = &gc->irq;
890 return girq->init_hw(gc);
893 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
895 struct gpio_irq_chip *girq = &gc->irq;
897 if (!girq->init_valid_mask)
900 girq->valid_mask = gpiochip_allocate_mask(gc);
901 if (!girq->valid_mask)
904 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
909 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
911 bitmap_free(gc->irq.valid_mask);
912 gc->irq.valid_mask = NULL;
915 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
918 if (!gpiochip_line_is_valid(gc, offset))
920 /* No mask means all valid */
921 if (likely(!gc->irq.valid_mask))
923 return test_bit(offset, gc->irq.valid_mask);
925 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
928 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
929 * @gc: the gpiochip to set the irqchip chain to
930 * @parent_irq: the irq number corresponding to the parent IRQ for this
932 * @parent_handler: the parent interrupt handler for the accumulated IRQ
933 * coming out of the gpiochip. If the interrupt is nested rather than
934 * cascaded, pass NULL in this handler argument
936 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
937 unsigned int parent_irq,
938 irq_flow_handler_t parent_handler)
940 struct gpio_irq_chip *girq = &gc->irq;
941 struct device *dev = &gc->gpiodev->dev;
944 chip_err(gc, "called %s before setting up irqchip\n",
949 if (parent_handler) {
952 "you cannot have chained interrupts on a chip that may sleep\n");
955 girq->parents = devm_kcalloc(dev, 1,
956 sizeof(*girq->parents),
958 if (!girq->parents) {
959 chip_err(gc, "out of memory allocating parent IRQ\n");
962 girq->parents[0] = parent_irq;
963 girq->num_parents = 1;
965 * The parent irqchip is already using the chip_data for this
966 * irqchip, so our callbacks simply use the handler_data.
968 irq_set_chained_handler_and_data(parent_irq, parent_handler,
974 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
975 * @gc: the gpiochip to set the irqchip nested handler to
976 * @irqchip: the irqchip to nest to the gpiochip
977 * @parent_irq: the irq number corresponding to the parent IRQ for this
980 void gpiochip_set_nested_irqchip(struct gpio_chip *gc,
981 struct irq_chip *irqchip,
982 unsigned int parent_irq)
984 gpiochip_set_cascaded_irqchip(gc, parent_irq, NULL);
986 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
988 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
991 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
993 * @gc: the gpiochip to set the irqchip hierarchical handler to
994 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
995 * will then percolate up to the parent
997 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
998 struct irq_chip *irqchip)
1000 /* DT will deal with mapping each IRQ as we go along */
1001 if (is_of_node(gc->irq.fwnode))
1005 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1006 * irqs upfront instead of dynamically since we don't have the
1007 * dynamic type of allocation that hardware description languages
1008 * provide. Once all GPIO drivers using board files are gone from
1009 * the kernel we can delete this code, but for a transitional period
1010 * it is necessary to keep this around.
1012 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1016 for (i = 0; i < gc->ngpio; i++) {
1017 struct irq_fwspec fwspec;
1018 unsigned int parent_hwirq;
1019 unsigned int parent_type;
1020 struct gpio_irq_chip *girq = &gc->irq;
1023 * We call the child to parent translation function
1024 * only to check if the child IRQ is valid or not.
1025 * Just pick the rising edge type here as that is what
1026 * we likely need to support.
1028 ret = girq->child_to_parent_hwirq(gc, i,
1029 IRQ_TYPE_EDGE_RISING,
1033 chip_err(gc, "skip set-up on hwirq %d\n",
1038 fwspec.fwnode = gc->irq.fwnode;
1039 /* This is the hwirq for the GPIO line side of things */
1040 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1041 /* Just pick something */
1042 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1043 fwspec.param_count = 2;
1044 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1045 /* just pick something */
1054 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1061 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1066 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1067 struct irq_fwspec *fwspec,
1068 unsigned long *hwirq,
1071 /* We support standard DT translation */
1072 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1073 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1076 /* This is for board files and others not using DT */
1077 if (is_fwnode_irqchip(fwspec->fwnode)) {
1080 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1083 WARN_ON(*type == IRQ_TYPE_NONE);
1089 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1091 unsigned int nr_irqs,
1094 struct gpio_chip *gc = d->host_data;
1095 irq_hw_number_t hwirq;
1096 unsigned int type = IRQ_TYPE_NONE;
1097 struct irq_fwspec *fwspec = data;
1099 unsigned int parent_hwirq;
1100 unsigned int parent_type;
1101 struct gpio_irq_chip *girq = &gc->irq;
1105 * The nr_irqs parameter is always one except for PCI multi-MSI
1106 * so this should not happen.
1108 WARN_ON(nr_irqs != 1);
1110 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1114 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1116 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1117 &parent_hwirq, &parent_type);
1119 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1122 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1125 * We set handle_bad_irq because the .set_type() should
1126 * always be invoked and set the right type of handler.
1128 irq_domain_set_info(d,
1137 /* This parent only handles asserted level IRQs */
1138 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1142 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1144 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1145 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1147 * If the parent irqdomain is msi, the interrupts have already
1148 * been allocated, so the EEXIST is good.
1150 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1154 "failed to allocate parent hwirq %d for hwirq %lu\n",
1155 parent_hwirq, hwirq);
1161 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1162 unsigned int offset)
1167 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1169 ops->activate = gpiochip_irq_domain_activate;
1170 ops->deactivate = gpiochip_irq_domain_deactivate;
1171 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1172 ops->free = irq_domain_free_irqs_common;
1175 * We only allow overriding the translate() function for
1176 * hierarchical chips, and this should only be done if the user
1177 * really need something other than 1:1 translation.
1179 if (!ops->translate)
1180 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1183 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1185 if (!gc->irq.child_to_parent_hwirq ||
1187 chip_err(gc, "missing irqdomain vital data\n");
1191 if (!gc->irq.child_offset_to_irq)
1192 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1194 if (!gc->irq.populate_parent_alloc_arg)
1195 gc->irq.populate_parent_alloc_arg =
1196 gpiochip_populate_parent_fwspec_twocell;
1198 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1200 gc->irq.domain = irq_domain_create_hierarchy(
1201 gc->irq.parent_domain,
1205 &gc->irq.child_irq_domain_ops,
1208 if (!gc->irq.domain)
1211 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1216 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1218 return !!gc->irq.parent_domain;
1221 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1222 unsigned int parent_hwirq,
1223 unsigned int parent_type)
1225 struct irq_fwspec *fwspec;
1227 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1231 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1232 fwspec->param_count = 2;
1233 fwspec->param[0] = parent_hwirq;
1234 fwspec->param[1] = parent_type;
1238 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1240 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1241 unsigned int parent_hwirq,
1242 unsigned int parent_type)
1244 struct irq_fwspec *fwspec;
1246 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1250 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1251 fwspec->param_count = 4;
1252 fwspec->param[0] = 0;
1253 fwspec->param[1] = parent_hwirq;
1254 fwspec->param[2] = 0;
1255 fwspec->param[3] = parent_type;
1259 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1263 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1268 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1273 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1276 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1277 * @d: the irqdomain used by this irqchip
1278 * @irq: the global irq number used by this GPIO irqchip irq
1279 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1281 * This function will set up the mapping for a certain IRQ line on a
1282 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1283 * stored inside the gpiochip.
1285 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1286 irq_hw_number_t hwirq)
1288 struct gpio_chip *gc = d->host_data;
1291 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1294 irq_set_chip_data(irq, gc);
1296 * This lock class tells lockdep that GPIO irqs are in a different
1297 * category than their parents, so it won't report false recursion.
1299 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1300 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1301 /* Chips that use nested thread handlers have them marked */
1302 if (gc->irq.threaded)
1303 irq_set_nested_thread(irq, 1);
1304 irq_set_noprobe(irq);
1306 if (gc->irq.num_parents == 1)
1307 ret = irq_set_parent(irq, gc->irq.parents[0]);
1308 else if (gc->irq.map)
1309 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1315 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1316 * is passed as default type.
1318 if (gc->irq.default_type != IRQ_TYPE_NONE)
1319 irq_set_irq_type(irq, gc->irq.default_type);
1323 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1325 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1327 struct gpio_chip *gc = d->host_data;
1329 if (gc->irq.threaded)
1330 irq_set_nested_thread(irq, 0);
1331 irq_set_chip_and_handler(irq, NULL, NULL);
1332 irq_set_chip_data(irq, NULL);
1334 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1336 static const struct irq_domain_ops gpiochip_domain_ops = {
1337 .map = gpiochip_irq_map,
1338 .unmap = gpiochip_irq_unmap,
1339 /* Virtually all GPIO irqchips are twocell:ed */
1340 .xlate = irq_domain_xlate_twocell,
1344 * TODO: move these activate/deactivate in under the hierarchicial
1345 * irqchip implementation as static once SPMI and SSBI (all external
1346 * users) are phased over.
1349 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1350 * @domain: The IRQ domain used by this IRQ chip
1351 * @data: Outermost irq_data associated with the IRQ
1352 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1354 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1355 * used as the activate function for the &struct irq_domain_ops. The host_data
1356 * for the IRQ domain must be the &struct gpio_chip.
1358 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1359 struct irq_data *data, bool reserve)
1361 struct gpio_chip *gc = domain->host_data;
1363 return gpiochip_lock_as_irq(gc, data->hwirq);
1365 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1368 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1369 * @domain: The IRQ domain used by this IRQ chip
1370 * @data: Outermost irq_data associated with the IRQ
1372 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1373 * be used as the deactivate function for the &struct irq_domain_ops. The
1374 * host_data for the IRQ domain must be the &struct gpio_chip.
1376 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1377 struct irq_data *data)
1379 struct gpio_chip *gc = domain->host_data;
1381 return gpiochip_unlock_as_irq(gc, data->hwirq);
1383 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1385 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned offset)
1387 struct irq_domain *domain = gc->irq.domain;
1389 if (!gpiochip_irqchip_irq_valid(gc, offset))
1392 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1393 if (irq_domain_is_hierarchy(domain)) {
1394 struct irq_fwspec spec;
1396 spec.fwnode = domain->fwnode;
1397 spec.param_count = 2;
1398 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1399 spec.param[1] = IRQ_TYPE_NONE;
1401 return irq_create_fwspec_mapping(&spec);
1405 return irq_create_mapping(domain, offset);
1408 static int gpiochip_irq_reqres(struct irq_data *d)
1410 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1412 return gpiochip_reqres_irq(gc, d->hwirq);
1415 static void gpiochip_irq_relres(struct irq_data *d)
1417 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1419 gpiochip_relres_irq(gc, d->hwirq);
1422 static void gpiochip_irq_mask(struct irq_data *d)
1424 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1426 if (gc->irq.irq_mask)
1427 gc->irq.irq_mask(d);
1428 gpiochip_disable_irq(gc, d->hwirq);
1431 static void gpiochip_irq_unmask(struct irq_data *d)
1433 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1435 gpiochip_enable_irq(gc, d->hwirq);
1436 if (gc->irq.irq_unmask)
1437 gc->irq.irq_unmask(d);
1440 static void gpiochip_irq_enable(struct irq_data *d)
1442 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1444 gpiochip_enable_irq(gc, d->hwirq);
1445 gc->irq.irq_enable(d);
1448 static void gpiochip_irq_disable(struct irq_data *d)
1450 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1452 gc->irq.irq_disable(d);
1453 gpiochip_disable_irq(gc, d->hwirq);
1456 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1458 struct irq_chip *irqchip = gc->irq.chip;
1460 if (!irqchip->irq_request_resources &&
1461 !irqchip->irq_release_resources) {
1462 irqchip->irq_request_resources = gpiochip_irq_reqres;
1463 irqchip->irq_release_resources = gpiochip_irq_relres;
1465 if (WARN_ON(gc->irq.irq_enable))
1467 /* Check if the irqchip already has this hook... */
1468 if (irqchip->irq_enable == gpiochip_irq_enable) {
1470 * ...and if so, give a gentle warning that this is bad
1474 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1478 if (irqchip->irq_disable) {
1479 gc->irq.irq_disable = irqchip->irq_disable;
1480 irqchip->irq_disable = gpiochip_irq_disable;
1482 gc->irq.irq_mask = irqchip->irq_mask;
1483 irqchip->irq_mask = gpiochip_irq_mask;
1486 if (irqchip->irq_enable) {
1487 gc->irq.irq_enable = irqchip->irq_enable;
1488 irqchip->irq_enable = gpiochip_irq_enable;
1490 gc->irq.irq_unmask = irqchip->irq_unmask;
1491 irqchip->irq_unmask = gpiochip_irq_unmask;
1496 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1497 * @gc: the GPIO chip to add the IRQ chip to
1498 * @lock_key: lockdep class for IRQ lock
1499 * @request_key: lockdep class for IRQ request
1501 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1502 struct lock_class_key *lock_key,
1503 struct lock_class_key *request_key)
1505 struct irq_chip *irqchip = gc->irq.chip;
1506 const struct irq_domain_ops *ops = NULL;
1507 struct device_node *np;
1514 if (gc->irq.parent_handler && gc->can_sleep) {
1515 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1519 np = gc->gpiodev->dev.of_node;
1520 type = gc->irq.default_type;
1523 * Specifying a default trigger is a terrible idea if DT or ACPI is
1524 * used to configure the interrupts, as you may end up with
1525 * conflicting triggers. Tell the user, and reset to NONE.
1527 if (WARN(np && type != IRQ_TYPE_NONE,
1528 "%s: Ignoring %u default trigger\n", np->full_name, type))
1529 type = IRQ_TYPE_NONE;
1531 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1532 acpi_handle_warn(ACPI_HANDLE(gc->parent),
1533 "Ignoring %u default trigger\n", type);
1534 type = IRQ_TYPE_NONE;
1537 gc->to_irq = gpiochip_to_irq;
1538 gc->irq.default_type = type;
1539 gc->irq.lock_key = lock_key;
1540 gc->irq.request_key = request_key;
1542 /* If a parent irqdomain is provided, let's build a hierarchy */
1543 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1544 int ret = gpiochip_hierarchy_add_domain(gc);
1548 /* Some drivers provide custom irqdomain ops */
1549 if (gc->irq.domain_ops)
1550 ops = gc->irq.domain_ops;
1553 ops = &gpiochip_domain_ops;
1554 gc->irq.domain = irq_domain_add_simple(np,
1558 if (!gc->irq.domain)
1562 if (gc->irq.parent_handler) {
1563 void *data = gc->irq.parent_handler_data ?: gc;
1565 for (i = 0; i < gc->irq.num_parents; i++) {
1567 * The parent IRQ chip is already using the chip_data
1568 * for this IRQ chip, so our callbacks simply use the
1571 irq_set_chained_handler_and_data(gc->irq.parents[i],
1572 gc->irq.parent_handler,
1577 gpiochip_set_irq_hooks(gc);
1579 acpi_gpiochip_request_interrupts(gc);
1585 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1586 * @gc: the gpiochip to remove the irqchip from
1588 * This is called only from gpiochip_remove()
1590 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1592 struct irq_chip *irqchip = gc->irq.chip;
1593 unsigned int offset;
1595 acpi_gpiochip_free_interrupts(gc);
1597 if (irqchip && gc->irq.parent_handler) {
1598 struct gpio_irq_chip *irq = &gc->irq;
1601 for (i = 0; i < irq->num_parents; i++)
1602 irq_set_chained_handler_and_data(irq->parents[i],
1606 /* Remove all IRQ mappings and delete the domain */
1607 if (gc->irq.domain) {
1610 for (offset = 0; offset < gc->ngpio; offset++) {
1611 if (!gpiochip_irqchip_irq_valid(gc, offset))
1614 irq = irq_find_mapping(gc->irq.domain, offset);
1615 irq_dispose_mapping(irq);
1618 irq_domain_remove(gc->irq.domain);
1622 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1623 irqchip->irq_request_resources = NULL;
1624 irqchip->irq_release_resources = NULL;
1626 if (irqchip->irq_enable == gpiochip_irq_enable) {
1627 irqchip->irq_enable = gc->irq.irq_enable;
1628 irqchip->irq_disable = gc->irq.irq_disable;
1631 gc->irq.irq_enable = NULL;
1632 gc->irq.irq_disable = NULL;
1633 gc->irq.chip = NULL;
1635 gpiochip_irqchip_free_valid_mask(gc);
1639 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1640 * @gc: the gpiochip to add the irqchip to
1641 * @irqchip: the irqchip to add to the gpiochip
1642 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1643 * allocate gpiochip irqs from
1644 * @handler: the irq handler to use (often a predefined irq core function)
1645 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1646 * to have the core avoid setting up any default type in the hardware.
1647 * @threaded: whether this irqchip uses a nested thread handler
1648 * @lock_key: lockdep class for IRQ lock
1649 * @request_key: lockdep class for IRQ request
1651 * This function closely associates a certain irqchip with a certain
1652 * gpiochip, providing an irq domain to translate the local IRQs to
1653 * global irqs in the gpiolib core, and making sure that the gpiochip
1654 * is passed as chip data to all related functions. Driver callbacks
1655 * need to use gpiochip_get_data() to get their local state containers back
1656 * from the gpiochip passed as chip data. An irqdomain will be stored
1657 * in the gpiochip that shall be used by the driver to handle IRQ number
1658 * translation. The gpiochip will need to be initialized and registered
1659 * before calling this function.
1661 * This function will handle two cell:ed simple IRQs and assumes all
1662 * the pins on the gpiochip can generate a unique IRQ. Everything else
1663 * need to be open coded.
1665 int gpiochip_irqchip_add_key(struct gpio_chip *gc,
1666 struct irq_chip *irqchip,
1667 unsigned int first_irq,
1668 irq_flow_handler_t handler,
1671 struct lock_class_key *lock_key,
1672 struct lock_class_key *request_key)
1674 struct device_node *of_node;
1676 if (!gc || !irqchip)
1680 chip_err(gc, "missing gpiochip .dev parent pointer\n");
1683 gc->irq.threaded = threaded;
1684 of_node = gc->parent->of_node;
1685 #ifdef CONFIG_OF_GPIO
1687 * If the gpiochip has an assigned OF node this takes precedence
1688 * FIXME: get rid of this and use gc->parent->of_node
1692 of_node = gc->of_node;
1695 * Specifying a default trigger is a terrible idea if DT or ACPI is
1696 * used to configure the interrupts, as you may end-up with
1697 * conflicting triggers. Tell the user, and reset to NONE.
1699 if (WARN(of_node && type != IRQ_TYPE_NONE,
1700 "%pOF: Ignoring %d default trigger\n", of_node, type))
1701 type = IRQ_TYPE_NONE;
1702 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1703 acpi_handle_warn(ACPI_HANDLE(gc->parent),
1704 "Ignoring %d default trigger\n", type);
1705 type = IRQ_TYPE_NONE;
1708 gc->irq.chip = irqchip;
1709 gc->irq.handler = handler;
1710 gc->irq.default_type = type;
1711 gc->to_irq = gpiochip_to_irq;
1712 gc->irq.lock_key = lock_key;
1713 gc->irq.request_key = request_key;
1714 gc->irq.domain = irq_domain_add_simple(of_node,
1715 gc->ngpio, first_irq,
1716 &gpiochip_domain_ops, gc);
1717 if (!gc->irq.domain) {
1718 gc->irq.chip = NULL;
1722 gpiochip_set_irq_hooks(gc);
1724 acpi_gpiochip_request_interrupts(gc);
1728 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
1731 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1732 * @gc: the gpiochip to add the irqchip to
1733 * @domain: the irqdomain to add to the gpiochip
1735 * This function adds an IRQ domain to the gpiochip.
1737 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1738 struct irq_domain *domain)
1743 gc->to_irq = gpiochip_to_irq;
1744 gc->irq.domain = domain;
1748 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1750 #else /* CONFIG_GPIOLIB_IRQCHIP */
1752 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1753 struct lock_class_key *lock_key,
1754 struct lock_class_key *request_key)
1758 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1760 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1765 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1769 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1772 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1775 * gpiochip_generic_request() - request the gpio function for a pin
1776 * @gc: the gpiochip owning the GPIO
1777 * @offset: the offset of the GPIO to request for GPIO function
1779 int gpiochip_generic_request(struct gpio_chip *gc, unsigned offset)
1781 #ifdef CONFIG_PINCTRL
1782 if (list_empty(&gc->gpiodev->pin_ranges))
1786 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1788 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1791 * gpiochip_generic_free() - free the gpio function from a pin
1792 * @gc: the gpiochip to request the gpio function for
1793 * @offset: the offset of the GPIO to free from GPIO function
1795 void gpiochip_generic_free(struct gpio_chip *gc, unsigned offset)
1797 pinctrl_gpio_free(gc->gpiodev->base + offset);
1799 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1802 * gpiochip_generic_config() - apply configuration for a pin
1803 * @gc: the gpiochip owning the GPIO
1804 * @offset: the offset of the GPIO to apply the configuration
1805 * @config: the configuration to be applied
1807 int gpiochip_generic_config(struct gpio_chip *gc, unsigned offset,
1808 unsigned long config)
1810 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1812 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1814 #ifdef CONFIG_PINCTRL
1817 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1818 * @gc: the gpiochip to add the range for
1819 * @pctldev: the pin controller to map to
1820 * @gpio_offset: the start offset in the current gpio_chip number space
1821 * @pin_group: name of the pin group inside the pin controller
1823 * Calling this function directly from a DeviceTree-supported
1824 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1825 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1826 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1828 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1829 struct pinctrl_dev *pctldev,
1830 unsigned int gpio_offset, const char *pin_group)
1832 struct gpio_pin_range *pin_range;
1833 struct gpio_device *gdev = gc->gpiodev;
1836 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1838 chip_err(gc, "failed to allocate pin ranges\n");
1842 /* Use local offset as range ID */
1843 pin_range->range.id = gpio_offset;
1844 pin_range->range.gc = gc;
1845 pin_range->range.name = gc->label;
1846 pin_range->range.base = gdev->base + gpio_offset;
1847 pin_range->pctldev = pctldev;
1849 ret = pinctrl_get_group_pins(pctldev, pin_group,
1850 &pin_range->range.pins,
1851 &pin_range->range.npins);
1857 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1859 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1860 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1861 pinctrl_dev_get_devname(pctldev), pin_group);
1863 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1867 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1870 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1871 * @gc: the gpiochip to add the range for
1872 * @pinctl_name: the dev_name() of the pin controller to map to
1873 * @gpio_offset: the start offset in the current gpio_chip number space
1874 * @pin_offset: the start offset in the pin controller number space
1875 * @npins: the number of pins from the offset of each pin space (GPIO and
1876 * pin controller) to accumulate in this range
1879 * 0 on success, or a negative error-code on failure.
1881 * Calling this function directly from a DeviceTree-supported
1882 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1883 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1884 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1886 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1887 unsigned int gpio_offset, unsigned int pin_offset,
1890 struct gpio_pin_range *pin_range;
1891 struct gpio_device *gdev = gc->gpiodev;
1894 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1896 chip_err(gc, "failed to allocate pin ranges\n");
1900 /* Use local offset as range ID */
1901 pin_range->range.id = gpio_offset;
1902 pin_range->range.gc = gc;
1903 pin_range->range.name = gc->label;
1904 pin_range->range.base = gdev->base + gpio_offset;
1905 pin_range->range.pin_base = pin_offset;
1906 pin_range->range.npins = npins;
1907 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1909 if (IS_ERR(pin_range->pctldev)) {
1910 ret = PTR_ERR(pin_range->pctldev);
1911 chip_err(gc, "could not create pin range\n");
1915 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1916 gpio_offset, gpio_offset + npins - 1,
1918 pin_offset, pin_offset + npins - 1);
1920 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1924 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1927 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1928 * @gc: the chip to remove all the mappings for
1930 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1932 struct gpio_pin_range *pin_range, *tmp;
1933 struct gpio_device *gdev = gc->gpiodev;
1935 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1936 list_del(&pin_range->node);
1937 pinctrl_remove_gpio_range(pin_range->pctldev,
1942 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1944 #endif /* CONFIG_PINCTRL */
1946 /* These "optional" allocation calls help prevent drivers from stomping
1947 * on each other, and help provide better diagnostics in debugfs.
1948 * They're called even less than the "set direction" calls.
1950 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
1952 struct gpio_chip *gc = desc->gdev->chip;
1954 unsigned long flags;
1958 label = kstrdup_const(label, GFP_KERNEL);
1963 spin_lock_irqsave(&gpio_lock, flags);
1965 /* NOTE: gpio_request() can be called in early boot,
1966 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1969 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1970 desc_set_label(desc, label ? : "?");
1979 /* gc->request may sleep */
1980 spin_unlock_irqrestore(&gpio_lock, flags);
1981 offset = gpio_chip_hwgpio(desc);
1982 if (gpiochip_line_is_valid(gc, offset))
1983 ret = gc->request(gc, offset);
1986 spin_lock_irqsave(&gpio_lock, flags);
1989 desc_set_label(desc, NULL);
1991 clear_bit(FLAG_REQUESTED, &desc->flags);
1995 if (gc->get_direction) {
1996 /* gc->get_direction may sleep */
1997 spin_unlock_irqrestore(&gpio_lock, flags);
1998 gpiod_get_direction(desc);
1999 spin_lock_irqsave(&gpio_lock, flags);
2002 spin_unlock_irqrestore(&gpio_lock, flags);
2007 * This descriptor validation needs to be inserted verbatim into each
2008 * function taking a descriptor, so we need to use a preprocessor
2009 * macro to avoid endless duplication. If the desc is NULL it is an
2010 * optional GPIO and calls should just bail out.
2012 static int validate_desc(const struct gpio_desc *desc, const char *func)
2017 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2018 return PTR_ERR(desc);
2021 pr_warn("%s: invalid GPIO (no device)\n", func);
2024 if (!desc->gdev->chip) {
2025 dev_warn(&desc->gdev->dev,
2026 "%s: backing chip is gone\n", func);
2032 #define VALIDATE_DESC(desc) do { \
2033 int __valid = validate_desc(desc, __func__); \
2038 #define VALIDATE_DESC_VOID(desc) do { \
2039 int __valid = validate_desc(desc, __func__); \
2044 int gpiod_request(struct gpio_desc *desc, const char *label)
2046 int ret = -EPROBE_DEFER;
2047 struct gpio_device *gdev;
2049 VALIDATE_DESC(desc);
2052 if (try_module_get(gdev->owner)) {
2053 ret = gpiod_request_commit(desc, label);
2055 module_put(gdev->owner);
2057 get_device(&gdev->dev);
2061 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2066 static bool gpiod_free_commit(struct gpio_desc *desc)
2069 unsigned long flags;
2070 struct gpio_chip *gc;
2074 gpiod_unexport(desc);
2076 spin_lock_irqsave(&gpio_lock, flags);
2078 gc = desc->gdev->chip;
2079 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2081 spin_unlock_irqrestore(&gpio_lock, flags);
2082 might_sleep_if(gc->can_sleep);
2083 gc->free(gc, gpio_chip_hwgpio(desc));
2084 spin_lock_irqsave(&gpio_lock, flags);
2086 kfree_const(desc->label);
2087 desc_set_label(desc, NULL);
2088 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2089 clear_bit(FLAG_REQUESTED, &desc->flags);
2090 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2091 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2092 clear_bit(FLAG_PULL_UP, &desc->flags);
2093 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2094 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2095 clear_bit(FLAG_EDGE_RISING, &desc->flags);
2096 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
2097 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2098 #ifdef CONFIG_OF_DYNAMIC
2101 #ifdef CONFIG_GPIO_CDEV
2102 WRITE_ONCE(desc->debounce_period_us, 0);
2107 spin_unlock_irqrestore(&gpio_lock, flags);
2108 blocking_notifier_call_chain(&desc->gdev->notifier,
2109 GPIOLINE_CHANGED_RELEASED, desc);
2114 void gpiod_free(struct gpio_desc *desc)
2116 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2117 module_put(desc->gdev->owner);
2118 put_device(&desc->gdev->dev);
2120 WARN_ON(extra_checks);
2125 * gpiochip_is_requested - return string iff signal was requested
2126 * @gc: controller managing the signal
2127 * @offset: of signal within controller's 0..(ngpio - 1) range
2129 * Returns NULL if the GPIO is not currently requested, else a string.
2130 * The string returned is the label passed to gpio_request(); if none has been
2131 * passed it is a meaningless, non-NULL constant.
2133 * This function is for use by GPIO controller drivers. The label can
2134 * help with diagnostics, and knowing that the signal is used as a GPIO
2135 * can help avoid accidentally multiplexing it to another controller.
2137 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned offset)
2139 struct gpio_desc *desc;
2141 if (offset >= gc->ngpio)
2144 desc = gpiochip_get_desc(gc, offset);
2148 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2152 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2155 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2157 * @hwnum: hardware number of the GPIO for which to request the descriptor
2158 * @label: label for the GPIO
2159 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2160 * specify things like line inversion semantics with the machine flags
2161 * such as GPIO_OUT_LOW
2162 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2163 * can be used to specify consumer semantics such as open drain
2165 * Function allows GPIO chip drivers to request and use their own GPIO
2166 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2167 * function will not increase reference count of the GPIO chip module. This
2168 * allows the GPIO chip module to be unloaded as needed (we assume that the
2169 * GPIO chip driver handles freeing the GPIOs it has requested).
2172 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2175 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2178 enum gpio_lookup_flags lflags,
2179 enum gpiod_flags dflags)
2181 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2185 chip_err(gc, "failed to get GPIO descriptor\n");
2189 ret = gpiod_request_commit(desc, label);
2191 return ERR_PTR(ret);
2193 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2195 chip_err(gc, "setup of own GPIO %s failed\n", label);
2196 gpiod_free_commit(desc);
2197 return ERR_PTR(ret);
2202 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2205 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2206 * @desc: GPIO descriptor to free
2208 * Function frees the given GPIO requested previously with
2209 * gpiochip_request_own_desc().
2211 void gpiochip_free_own_desc(struct gpio_desc *desc)
2214 gpiod_free_commit(desc);
2216 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2219 * Drivers MUST set GPIO direction before making get/set calls. In
2220 * some cases this is done in early boot, before IRQs are enabled.
2222 * As a rule these aren't called more than once (except for drivers
2223 * using the open-drain emulation idiom) so these are natural places
2224 * to accumulate extra debugging checks. Note that we can't (yet)
2225 * rely on gpio_request() having been called beforehand.
2228 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2229 unsigned long config)
2231 if (!gc->set_config)
2234 return gc->set_config(gc, offset, config);
2237 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2239 struct gpio_chip *gc = desc->gdev->chip;
2240 unsigned long config;
2244 case PIN_CONFIG_BIAS_PULL_DOWN:
2245 case PIN_CONFIG_BIAS_PULL_UP:
2253 config = PIN_CONF_PACKED(mode, arg);
2254 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2257 static int gpio_set_bias(struct gpio_desc *desc)
2262 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2263 bias = PIN_CONFIG_BIAS_DISABLE;
2264 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2265 bias = PIN_CONFIG_BIAS_PULL_UP;
2266 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2267 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2270 ret = gpio_set_config(desc, bias);
2271 if (ret != -ENOTSUPP)
2278 * gpiod_direction_input - set the GPIO direction to input
2279 * @desc: GPIO to set to input
2281 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2282 * be called safely on it.
2284 * Return 0 in case of success, else an error code.
2286 int gpiod_direction_input(struct gpio_desc *desc)
2288 struct gpio_chip *gc;
2291 VALIDATE_DESC(desc);
2292 gc = desc->gdev->chip;
2295 * It is legal to have no .get() and .direction_input() specified if
2296 * the chip is output-only, but you can't specify .direction_input()
2297 * and not support the .get() operation, that doesn't make sense.
2299 if (!gc->get && gc->direction_input) {
2301 "%s: missing get() but have direction_input()\n",
2307 * If we have a .direction_input() callback, things are simple,
2308 * just call it. Else we are some input-only chip so try to check the
2309 * direction (if .get_direction() is supported) else we silently
2310 * assume we are in input mode after this.
2312 if (gc->direction_input) {
2313 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2314 } else if (gc->get_direction &&
2315 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2317 "%s: missing direction_input() operation and line is output\n",
2322 clear_bit(FLAG_IS_OUT, &desc->flags);
2323 ret = gpio_set_bias(desc);
2326 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2330 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2332 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2334 struct gpio_chip *gc = desc->gdev->chip;
2339 * It's OK not to specify .direction_output() if the gpiochip is
2340 * output-only, but if there is then not even a .set() operation it
2341 * is pretty tricky to drive the output line.
2343 if (!gc->set && !gc->direction_output) {
2345 "%s: missing set() and direction_output() operations\n",
2350 if (gc->direction_output) {
2351 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2353 /* Check that we are in output mode if we can */
2354 if (gc->get_direction &&
2355 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2357 "%s: missing direction_output() operation\n",
2362 * If we can't actively set the direction, we are some
2363 * output-only chip, so just drive the output as desired.
2365 gc->set(gc, gpio_chip_hwgpio(desc), val);
2369 set_bit(FLAG_IS_OUT, &desc->flags);
2370 trace_gpio_value(desc_to_gpio(desc), 0, val);
2371 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2376 * gpiod_direction_output_raw - set the GPIO direction to output
2377 * @desc: GPIO to set to output
2378 * @value: initial output value of the GPIO
2380 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2381 * be called safely on it. The initial value of the output must be specified
2382 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2384 * Return 0 in case of success, else an error code.
2386 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2388 VALIDATE_DESC(desc);
2389 return gpiod_direction_output_raw_commit(desc, value);
2391 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2394 * gpiod_direction_output - set the GPIO direction to output
2395 * @desc: GPIO to set to output
2396 * @value: initial output value of the GPIO
2398 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2399 * be called safely on it. The initial value of the output must be specified
2400 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2403 * Return 0 in case of success, else an error code.
2405 int gpiod_direction_output(struct gpio_desc *desc, int value)
2409 VALIDATE_DESC(desc);
2410 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2415 /* GPIOs used for enabled IRQs shall not be set as output */
2416 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2417 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2419 "%s: tried to set a GPIO tied to an IRQ as output\n",
2424 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2425 /* First see if we can enable open drain in hardware */
2426 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2428 goto set_output_value;
2429 /* Emulate open drain by not actively driving the line high */
2431 ret = gpiod_direction_input(desc);
2432 goto set_output_flag;
2435 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2436 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2438 goto set_output_value;
2439 /* Emulate open source by not actively driving the line low */
2441 ret = gpiod_direction_input(desc);
2442 goto set_output_flag;
2445 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2449 ret = gpio_set_bias(desc);
2452 return gpiod_direction_output_raw_commit(desc, value);
2456 * When emulating open-source or open-drain functionalities by not
2457 * actively driving the line (setting mode to input) we still need to
2458 * set the IS_OUT flag or otherwise we won't be able to set the line
2462 set_bit(FLAG_IS_OUT, &desc->flags);
2465 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2468 * gpiod_set_config - sets @config for a GPIO
2469 * @desc: descriptor of the GPIO for which to set the configuration
2470 * @config: Same packed config format as generic pinconf
2473 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2476 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2478 struct gpio_chip *gc;
2480 VALIDATE_DESC(desc);
2481 gc = desc->gdev->chip;
2483 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2485 EXPORT_SYMBOL_GPL(gpiod_set_config);
2488 * gpiod_set_debounce - sets @debounce time for a GPIO
2489 * @desc: descriptor of the GPIO for which to set debounce time
2490 * @debounce: debounce time in microseconds
2493 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2496 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2498 unsigned long config;
2500 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2501 return gpiod_set_config(desc, config);
2503 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2506 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2507 * @desc: descriptor of the GPIO for which to configure persistence
2508 * @transitory: True to lose state on suspend or reset, false for persistence
2511 * 0 on success, otherwise a negative error code.
2513 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2515 struct gpio_chip *gc;
2516 unsigned long packed;
2520 VALIDATE_DESC(desc);
2522 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2523 * persistence state.
2525 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2527 /* If the driver supports it, set the persistence state now */
2528 gc = desc->gdev->chip;
2529 if (!gc->set_config)
2532 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2534 gpio = gpio_chip_hwgpio(desc);
2535 rc = gpio_do_set_config(gc, gpio, packed);
2536 if (rc == -ENOTSUPP) {
2537 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2544 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2547 * gpiod_is_active_low - test whether a GPIO is active-low or not
2548 * @desc: the gpio descriptor to test
2550 * Returns 1 if the GPIO is active-low, 0 otherwise.
2552 int gpiod_is_active_low(const struct gpio_desc *desc)
2554 VALIDATE_DESC(desc);
2555 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2557 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2560 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2561 * @desc: the gpio descriptor to change
2563 void gpiod_toggle_active_low(struct gpio_desc *desc)
2565 VALIDATE_DESC_VOID(desc);
2566 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2568 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2570 /* I/O calls are only valid after configuration completed; the relevant
2571 * "is this a valid GPIO" error checks should already have been done.
2573 * "Get" operations are often inlinable as reading a pin value register,
2574 * and masking the relevant bit in that register.
2576 * When "set" operations are inlinable, they involve writing that mask to
2577 * one register to set a low value, or a different register to set it high.
2578 * Otherwise locking is needed, so there may be little value to inlining.
2580 *------------------------------------------------------------------------
2582 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2583 * have requested the GPIO. That can include implicit requesting by
2584 * a direction setting call. Marking a gpio as requested locks its chip
2585 * in memory, guaranteeing that these table lookups need no more locking
2586 * and that gpiochip_remove() will fail.
2588 * REVISIT when debugging, consider adding some instrumentation to ensure
2589 * that the GPIO was actually requested.
2592 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2594 struct gpio_chip *gc;
2598 gc = desc->gdev->chip;
2599 offset = gpio_chip_hwgpio(desc);
2600 value = gc->get ? gc->get(gc, offset) : -EIO;
2601 value = value < 0 ? value : !!value;
2602 trace_gpio_value(desc_to_gpio(desc), 1, value);
2606 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2607 unsigned long *mask, unsigned long *bits)
2609 if (gc->get_multiple) {
2610 return gc->get_multiple(gc, mask, bits);
2611 } else if (gc->get) {
2614 for_each_set_bit(i, mask, gc->ngpio) {
2615 value = gc->get(gc, i);
2618 __assign_bit(i, bits, value);
2625 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2626 unsigned int array_size,
2627 struct gpio_desc **desc_array,
2628 struct gpio_array *array_info,
2629 unsigned long *value_bitmap)
2634 * Validate array_info against desc_array and its size.
2635 * It should immediately follow desc_array if both
2636 * have been obtained from the same gpiod_get_array() call.
2638 if (array_info && array_info->desc == desc_array &&
2639 array_size <= array_info->size &&
2640 (void *)array_info == desc_array + array_info->size) {
2642 WARN_ON(array_info->chip->can_sleep);
2644 ret = gpio_chip_get_multiple(array_info->chip,
2645 array_info->get_mask,
2650 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2651 bitmap_xor(value_bitmap, value_bitmap,
2652 array_info->invert_mask, array_size);
2654 i = find_first_zero_bit(array_info->get_mask, array_size);
2655 if (i == array_size)
2661 while (i < array_size) {
2662 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2663 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2664 unsigned long *mask, *bits;
2667 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2670 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2672 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2677 bits = mask + BITS_TO_LONGS(gc->ngpio);
2678 bitmap_zero(mask, gc->ngpio);
2681 WARN_ON(gc->can_sleep);
2683 /* collect all inputs belonging to the same chip */
2686 const struct gpio_desc *desc = desc_array[i];
2687 int hwgpio = gpio_chip_hwgpio(desc);
2689 __set_bit(hwgpio, mask);
2693 i = find_next_zero_bit(array_info->get_mask,
2695 } while ((i < array_size) &&
2696 (desc_array[i]->gdev->chip == gc));
2698 ret = gpio_chip_get_multiple(gc, mask, bits);
2700 if (mask != fastpath)
2705 for (j = first; j < i; ) {
2706 const struct gpio_desc *desc = desc_array[j];
2707 int hwgpio = gpio_chip_hwgpio(desc);
2708 int value = test_bit(hwgpio, bits);
2710 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2712 __assign_bit(j, value_bitmap, value);
2713 trace_gpio_value(desc_to_gpio(desc), 1, value);
2717 j = find_next_zero_bit(array_info->get_mask, i,
2721 if (mask != fastpath)
2728 * gpiod_get_raw_value() - return a gpio's raw value
2729 * @desc: gpio whose value will be returned
2731 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2732 * its ACTIVE_LOW status, or negative errno on failure.
2734 * This function can be called from contexts where we cannot sleep, and will
2735 * complain if the GPIO chip functions potentially sleep.
2737 int gpiod_get_raw_value(const struct gpio_desc *desc)
2739 VALIDATE_DESC(desc);
2740 /* Should be using gpiod_get_raw_value_cansleep() */
2741 WARN_ON(desc->gdev->chip->can_sleep);
2742 return gpiod_get_raw_value_commit(desc);
2744 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2747 * gpiod_get_value() - return a gpio's value
2748 * @desc: gpio whose value will be returned
2750 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2751 * account, or negative errno on failure.
2753 * This function can be called from contexts where we cannot sleep, and will
2754 * complain if the GPIO chip functions potentially sleep.
2756 int gpiod_get_value(const struct gpio_desc *desc)
2760 VALIDATE_DESC(desc);
2761 /* Should be using gpiod_get_value_cansleep() */
2762 WARN_ON(desc->gdev->chip->can_sleep);
2764 value = gpiod_get_raw_value_commit(desc);
2768 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2773 EXPORT_SYMBOL_GPL(gpiod_get_value);
2776 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2777 * @array_size: number of elements in the descriptor array / value bitmap
2778 * @desc_array: array of GPIO descriptors whose values will be read
2779 * @array_info: information on applicability of fast bitmap processing path
2780 * @value_bitmap: bitmap to store the read values
2782 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2783 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2784 * else an error code.
2786 * This function can be called from contexts where we cannot sleep,
2787 * and it will complain if the GPIO chip functions potentially sleep.
2789 int gpiod_get_raw_array_value(unsigned int array_size,
2790 struct gpio_desc **desc_array,
2791 struct gpio_array *array_info,
2792 unsigned long *value_bitmap)
2796 return gpiod_get_array_value_complex(true, false, array_size,
2797 desc_array, array_info,
2800 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2803 * gpiod_get_array_value() - read values from an array of GPIOs
2804 * @array_size: number of elements in the descriptor array / value bitmap
2805 * @desc_array: array of GPIO descriptors whose values will be read
2806 * @array_info: information on applicability of fast bitmap processing path
2807 * @value_bitmap: bitmap to store the read values
2809 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2810 * into account. Return 0 in case of success, else an error code.
2812 * This function can be called from contexts where we cannot sleep,
2813 * and it will complain if the GPIO chip functions potentially sleep.
2815 int gpiod_get_array_value(unsigned int array_size,
2816 struct gpio_desc **desc_array,
2817 struct gpio_array *array_info,
2818 unsigned long *value_bitmap)
2822 return gpiod_get_array_value_complex(false, false, array_size,
2823 desc_array, array_info,
2826 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2829 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2830 * @desc: gpio descriptor whose state need to be set.
2831 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2833 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2836 struct gpio_chip *gc = desc->gdev->chip;
2837 int offset = gpio_chip_hwgpio(desc);
2840 ret = gc->direction_input(gc, offset);
2842 ret = gc->direction_output(gc, offset, 0);
2844 set_bit(FLAG_IS_OUT, &desc->flags);
2846 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2849 "%s: Error in set_value for open drain err %d\n",
2854 * _gpio_set_open_source_value() - Set the open source gpio's value.
2855 * @desc: gpio descriptor whose state need to be set.
2856 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2858 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2861 struct gpio_chip *gc = desc->gdev->chip;
2862 int offset = gpio_chip_hwgpio(desc);
2865 ret = gc->direction_output(gc, offset, 1);
2867 set_bit(FLAG_IS_OUT, &desc->flags);
2869 ret = gc->direction_input(gc, offset);
2871 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2874 "%s: Error in set_value for open source err %d\n",
2878 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2880 struct gpio_chip *gc;
2882 gc = desc->gdev->chip;
2883 trace_gpio_value(desc_to_gpio(desc), 0, value);
2884 gc->set(gc, gpio_chip_hwgpio(desc), value);
2888 * set multiple outputs on the same chip;
2889 * use the chip's set_multiple function if available;
2890 * otherwise set the outputs sequentially;
2891 * @chip: the GPIO chip we operate on
2892 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2893 * defines which outputs are to be changed
2894 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2895 * defines the values the outputs specified by mask are to be set to
2897 static void gpio_chip_set_multiple(struct gpio_chip *gc,
2898 unsigned long *mask, unsigned long *bits)
2900 if (gc->set_multiple) {
2901 gc->set_multiple(gc, mask, bits);
2905 /* set outputs if the corresponding mask bit is set */
2906 for_each_set_bit(i, mask, gc->ngpio)
2907 gc->set(gc, i, test_bit(i, bits));
2911 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
2912 unsigned int array_size,
2913 struct gpio_desc **desc_array,
2914 struct gpio_array *array_info,
2915 unsigned long *value_bitmap)
2920 * Validate array_info against desc_array and its size.
2921 * It should immediately follow desc_array if both
2922 * have been obtained from the same gpiod_get_array() call.
2924 if (array_info && array_info->desc == desc_array &&
2925 array_size <= array_info->size &&
2926 (void *)array_info == desc_array + array_info->size) {
2928 WARN_ON(array_info->chip->can_sleep);
2930 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2931 bitmap_xor(value_bitmap, value_bitmap,
2932 array_info->invert_mask, array_size);
2934 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2937 i = find_first_zero_bit(array_info->set_mask, array_size);
2938 if (i == array_size)
2944 while (i < array_size) {
2945 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2946 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2947 unsigned long *mask, *bits;
2950 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2953 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2955 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2960 bits = mask + BITS_TO_LONGS(gc->ngpio);
2961 bitmap_zero(mask, gc->ngpio);
2964 WARN_ON(gc->can_sleep);
2967 struct gpio_desc *desc = desc_array[i];
2968 int hwgpio = gpio_chip_hwgpio(desc);
2969 int value = test_bit(i, value_bitmap);
2972 * Pins applicable for fast input but not for
2973 * fast output processing may have been already
2974 * inverted inside the fast path, skip them.
2976 if (!raw && !(array_info &&
2977 test_bit(i, array_info->invert_mask)) &&
2978 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2980 trace_gpio_value(desc_to_gpio(desc), 0, value);
2982 * collect all normal outputs belonging to the same chip
2983 * open drain and open source outputs are set individually
2985 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2986 gpio_set_open_drain_value_commit(desc, value);
2987 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2988 gpio_set_open_source_value_commit(desc, value);
2990 __set_bit(hwgpio, mask);
2991 __assign_bit(hwgpio, bits, value);
2997 i = find_next_zero_bit(array_info->set_mask,
2999 } while ((i < array_size) &&
3000 (desc_array[i]->gdev->chip == gc));
3001 /* push collected bits to outputs */
3003 gpio_chip_set_multiple(gc, mask, bits);
3005 if (mask != fastpath)
3012 * gpiod_set_raw_value() - assign a gpio's raw value
3013 * @desc: gpio whose value will be assigned
3014 * @value: value to assign
3016 * Set the raw value of the GPIO, i.e. the value of its physical line without
3017 * regard for its ACTIVE_LOW status.
3019 * This function can be called from contexts where we cannot sleep, and will
3020 * complain if the GPIO chip functions potentially sleep.
3022 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3024 VALIDATE_DESC_VOID(desc);
3025 /* Should be using gpiod_set_raw_value_cansleep() */
3026 WARN_ON(desc->gdev->chip->can_sleep);
3027 gpiod_set_raw_value_commit(desc, value);
3029 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3032 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3033 * @desc: the descriptor to set the value on
3034 * @value: value to set
3036 * This sets the value of a GPIO line backing a descriptor, applying
3037 * different semantic quirks like active low and open drain/source
3040 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3042 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3044 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3045 gpio_set_open_drain_value_commit(desc, value);
3046 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3047 gpio_set_open_source_value_commit(desc, value);
3049 gpiod_set_raw_value_commit(desc, value);
3053 * gpiod_set_value() - assign a gpio's value
3054 * @desc: gpio whose value will be assigned
3055 * @value: value to assign
3057 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3058 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3060 * This function can be called from contexts where we cannot sleep, and will
3061 * complain if the GPIO chip functions potentially sleep.
3063 void gpiod_set_value(struct gpio_desc *desc, int value)
3065 VALIDATE_DESC_VOID(desc);
3066 /* Should be using gpiod_set_value_cansleep() */
3067 WARN_ON(desc->gdev->chip->can_sleep);
3068 gpiod_set_value_nocheck(desc, value);
3070 EXPORT_SYMBOL_GPL(gpiod_set_value);
3073 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3074 * @array_size: number of elements in the descriptor array / value bitmap
3075 * @desc_array: array of GPIO descriptors whose values will be assigned
3076 * @array_info: information on applicability of fast bitmap processing path
3077 * @value_bitmap: bitmap of values to assign
3079 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3080 * without regard for their ACTIVE_LOW status.
3082 * This function can be called from contexts where we cannot sleep, and will
3083 * complain if the GPIO chip functions potentially sleep.
3085 int gpiod_set_raw_array_value(unsigned int array_size,
3086 struct gpio_desc **desc_array,
3087 struct gpio_array *array_info,
3088 unsigned long *value_bitmap)
3092 return gpiod_set_array_value_complex(true, false, array_size,
3093 desc_array, array_info, value_bitmap);
3095 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3098 * gpiod_set_array_value() - assign values to an array of GPIOs
3099 * @array_size: number of elements in the descriptor array / value bitmap
3100 * @desc_array: array of GPIO descriptors whose values will be assigned
3101 * @array_info: information on applicability of fast bitmap processing path
3102 * @value_bitmap: bitmap of values to assign
3104 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3107 * This function can be called from contexts where we cannot sleep, and will
3108 * complain if the GPIO chip functions potentially sleep.
3110 int gpiod_set_array_value(unsigned int array_size,
3111 struct gpio_desc **desc_array,
3112 struct gpio_array *array_info,
3113 unsigned long *value_bitmap)
3117 return gpiod_set_array_value_complex(false, false, array_size,
3118 desc_array, array_info,
3121 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3124 * gpiod_cansleep() - report whether gpio value access may sleep
3125 * @desc: gpio to check
3128 int gpiod_cansleep(const struct gpio_desc *desc)
3130 VALIDATE_DESC(desc);
3131 return desc->gdev->chip->can_sleep;
3133 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3136 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3137 * @desc: gpio to set the consumer name on
3138 * @name: the new consumer name
3140 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3142 VALIDATE_DESC(desc);
3144 name = kstrdup_const(name, GFP_KERNEL);
3149 kfree_const(desc->label);
3150 desc_set_label(desc, name);
3154 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3157 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3158 * @desc: gpio whose IRQ will be returned (already requested)
3160 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3163 int gpiod_to_irq(const struct gpio_desc *desc)
3165 struct gpio_chip *gc;
3169 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3170 * requires this function to not return zero on an invalid descriptor
3171 * but rather a negative error number.
3173 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3176 gc = desc->gdev->chip;
3177 offset = gpio_chip_hwgpio(desc);
3179 int retirq = gc->to_irq(gc, offset);
3181 /* Zero means NO_IRQ */
3189 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3192 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3193 * @gc: the chip the GPIO to lock belongs to
3194 * @offset: the offset of the GPIO to lock as IRQ
3196 * This is used directly by GPIO drivers that want to lock down
3197 * a certain GPIO line to be used for IRQs.
3199 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3201 struct gpio_desc *desc;
3203 desc = gpiochip_get_desc(gc, offset);
3205 return PTR_ERR(desc);
3208 * If it's fast: flush the direction setting if something changed
3211 if (!gc->can_sleep && gc->get_direction) {
3212 int dir = gpiod_get_direction(desc);
3215 chip_err(gc, "%s: cannot get GPIO direction\n",
3221 /* To be valid for IRQ the line needs to be input or open drain */
3222 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3223 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3225 "%s: tried to flag a GPIO set as output for IRQ\n",
3230 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3231 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3234 * If the consumer has not set up a label (such as when the
3235 * IRQ is referenced from .to_irq()) we set up a label here
3236 * so it is clear this is used as an interrupt.
3239 desc_set_label(desc, "interrupt");
3243 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3246 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3247 * @gc: the chip the GPIO to lock belongs to
3248 * @offset: the offset of the GPIO to lock as IRQ
3250 * This is used directly by GPIO drivers that want to indicate
3251 * that a certain GPIO is no longer used exclusively for IRQ.
3253 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3255 struct gpio_desc *desc;
3257 desc = gpiochip_get_desc(gc, offset);
3261 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3262 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3264 /* If we only had this marking, erase it */
3265 if (desc->label && !strcmp(desc->label, "interrupt"))
3266 desc_set_label(desc, NULL);
3268 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3270 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3272 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3274 if (!IS_ERR(desc) &&
3275 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3276 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3278 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3280 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3282 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3284 if (!IS_ERR(desc) &&
3285 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3287 * We must not be output when using IRQ UNLESS we are
3290 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3291 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3292 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3295 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3297 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3299 if (offset >= gc->ngpio)
3302 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3304 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3306 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3310 if (!try_module_get(gc->gpiodev->owner))
3313 ret = gpiochip_lock_as_irq(gc, offset);
3315 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3316 module_put(gc->gpiodev->owner);
3321 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3323 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3325 gpiochip_unlock_as_irq(gc, offset);
3326 module_put(gc->gpiodev->owner);
3328 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3330 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3332 if (offset >= gc->ngpio)
3335 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3337 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3339 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3341 if (offset >= gc->ngpio)
3344 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3346 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3348 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3350 if (offset >= gc->ngpio)
3353 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3355 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3358 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3359 * @desc: gpio whose value will be returned
3361 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3362 * its ACTIVE_LOW status, or negative errno on failure.
3364 * This function is to be called from contexts that can sleep.
3366 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3368 might_sleep_if(extra_checks);
3369 VALIDATE_DESC(desc);
3370 return gpiod_get_raw_value_commit(desc);
3372 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3375 * gpiod_get_value_cansleep() - return a gpio's value
3376 * @desc: gpio whose value will be returned
3378 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3379 * account, or negative errno on failure.
3381 * This function is to be called from contexts that can sleep.
3383 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3387 might_sleep_if(extra_checks);
3388 VALIDATE_DESC(desc);
3389 value = gpiod_get_raw_value_commit(desc);
3393 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3398 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3401 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3402 * @array_size: number of elements in the descriptor array / value bitmap
3403 * @desc_array: array of GPIO descriptors whose values will be read
3404 * @array_info: information on applicability of fast bitmap processing path
3405 * @value_bitmap: bitmap to store the read values
3407 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3408 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3409 * else an error code.
3411 * This function is to be called from contexts that can sleep.
3413 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3414 struct gpio_desc **desc_array,
3415 struct gpio_array *array_info,
3416 unsigned long *value_bitmap)
3418 might_sleep_if(extra_checks);
3421 return gpiod_get_array_value_complex(true, true, array_size,
3422 desc_array, array_info,
3425 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3428 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3429 * @array_size: number of elements in the descriptor array / value bitmap
3430 * @desc_array: array of GPIO descriptors whose values will be read
3431 * @array_info: information on applicability of fast bitmap processing path
3432 * @value_bitmap: bitmap to store the read values
3434 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3435 * into account. Return 0 in case of success, else an error code.
3437 * This function is to be called from contexts that can sleep.
3439 int gpiod_get_array_value_cansleep(unsigned int array_size,
3440 struct gpio_desc **desc_array,
3441 struct gpio_array *array_info,
3442 unsigned long *value_bitmap)
3444 might_sleep_if(extra_checks);
3447 return gpiod_get_array_value_complex(false, true, array_size,
3448 desc_array, array_info,
3451 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3454 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3455 * @desc: gpio whose value will be assigned
3456 * @value: value to assign
3458 * Set the raw value of the GPIO, i.e. the value of its physical line without
3459 * regard for its ACTIVE_LOW status.
3461 * This function is to be called from contexts that can sleep.
3463 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3465 might_sleep_if(extra_checks);
3466 VALIDATE_DESC_VOID(desc);
3467 gpiod_set_raw_value_commit(desc, value);
3469 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3472 * gpiod_set_value_cansleep() - assign a gpio's value
3473 * @desc: gpio whose value will be assigned
3474 * @value: value to assign
3476 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3479 * This function is to be called from contexts that can sleep.
3481 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3483 might_sleep_if(extra_checks);
3484 VALIDATE_DESC_VOID(desc);
3485 gpiod_set_value_nocheck(desc, value);
3487 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3490 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3491 * @array_size: number of elements in the descriptor array / value bitmap
3492 * @desc_array: array of GPIO descriptors whose values will be assigned
3493 * @array_info: information on applicability of fast bitmap processing path
3494 * @value_bitmap: bitmap of values to assign
3496 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3497 * without regard for their ACTIVE_LOW status.
3499 * This function is to be called from contexts that can sleep.
3501 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3502 struct gpio_desc **desc_array,
3503 struct gpio_array *array_info,
3504 unsigned long *value_bitmap)
3506 might_sleep_if(extra_checks);
3509 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3510 array_info, value_bitmap);
3512 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3515 * gpiod_add_lookup_tables() - register GPIO device consumers
3516 * @tables: list of tables of consumers to register
3517 * @n: number of tables in the list
3519 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3523 mutex_lock(&gpio_lookup_lock);
3525 for (i = 0; i < n; i++)
3526 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3528 mutex_unlock(&gpio_lookup_lock);
3532 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3533 * @array_size: number of elements in the descriptor array / value bitmap
3534 * @desc_array: array of GPIO descriptors whose values will be assigned
3535 * @array_info: information on applicability of fast bitmap processing path
3536 * @value_bitmap: bitmap of values to assign
3538 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3541 * This function is to be called from contexts that can sleep.
3543 int gpiod_set_array_value_cansleep(unsigned int array_size,
3544 struct gpio_desc **desc_array,
3545 struct gpio_array *array_info,
3546 unsigned long *value_bitmap)
3548 might_sleep_if(extra_checks);
3551 return gpiod_set_array_value_complex(false, true, array_size,
3552 desc_array, array_info,
3555 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3558 * gpiod_add_lookup_table() - register GPIO device consumers
3559 * @table: table of consumers to register
3561 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3563 mutex_lock(&gpio_lookup_lock);
3565 list_add_tail(&table->list, &gpio_lookup_list);
3567 mutex_unlock(&gpio_lookup_lock);
3569 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3572 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3573 * @table: table of consumers to unregister
3575 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3577 mutex_lock(&gpio_lookup_lock);
3579 list_del(&table->list);
3581 mutex_unlock(&gpio_lookup_lock);
3583 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3586 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3587 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3589 void gpiod_add_hogs(struct gpiod_hog *hogs)
3591 struct gpio_chip *gc;
3592 struct gpiod_hog *hog;
3594 mutex_lock(&gpio_machine_hogs_mutex);
3596 for (hog = &hogs[0]; hog->chip_label; hog++) {
3597 list_add_tail(&hog->list, &gpio_machine_hogs);
3600 * The chip may have been registered earlier, so check if it
3601 * exists and, if so, try to hog the line now.
3603 gc = find_chip_by_name(hog->chip_label);
3605 gpiochip_machine_hog(gc, hog);
3608 mutex_unlock(&gpio_machine_hogs_mutex);
3610 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3612 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3614 const char *dev_id = dev ? dev_name(dev) : NULL;
3615 struct gpiod_lookup_table *table;
3617 mutex_lock(&gpio_lookup_lock);
3619 list_for_each_entry(table, &gpio_lookup_list, list) {
3620 if (table->dev_id && dev_id) {
3622 * Valid strings on both ends, must be identical to have
3625 if (!strcmp(table->dev_id, dev_id))
3629 * One of the pointers is NULL, so both must be to have
3632 if (dev_id == table->dev_id)
3639 mutex_unlock(&gpio_lookup_lock);
3643 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3644 unsigned int idx, unsigned long *flags)
3646 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3647 struct gpiod_lookup_table *table;
3648 struct gpiod_lookup *p;
3650 table = gpiod_find_lookup_table(dev);
3654 for (p = &table->table[0]; p->key; p++) {
3655 struct gpio_chip *gc;
3657 /* idx must always match exactly */
3661 /* If the lookup entry has a con_id, require exact match */
3662 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3665 if (p->chip_hwnum == U16_MAX) {
3666 desc = gpio_name_to_desc(p->key);
3672 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3674 return ERR_PTR(-EPROBE_DEFER);
3677 gc = find_chip_by_name(p->key);
3681 * As the lookup table indicates a chip with
3682 * p->key should exist, assume it may
3683 * still appear later and let the interested
3684 * consumer be probed again or let the Deferred
3685 * Probe infrastructure handle the error.
3687 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3689 return ERR_PTR(-EPROBE_DEFER);
3692 if (gc->ngpio <= p->chip_hwnum) {
3694 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3695 idx, p->chip_hwnum, gc->ngpio - 1,
3697 return ERR_PTR(-EINVAL);
3700 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3709 static int platform_gpio_count(struct device *dev, const char *con_id)
3711 struct gpiod_lookup_table *table;
3712 struct gpiod_lookup *p;
3713 unsigned int count = 0;
3715 table = gpiod_find_lookup_table(dev);
3719 for (p = &table->table[0]; p->key; p++) {
3720 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3721 (!con_id && !p->con_id))
3731 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3732 * @fwnode: handle of the firmware node
3733 * @con_id: function within the GPIO consumer
3734 * @index: index of the GPIO to obtain for the consumer
3735 * @flags: GPIO initialization flags
3736 * @label: label to attach to the requested GPIO
3738 * This function can be used for drivers that get their configuration
3739 * from opaque firmware.
3741 * The function properly finds the corresponding GPIO using whatever is the
3742 * underlying firmware interface and then makes sure that the GPIO
3743 * descriptor is requested before it is returned to the caller.
3746 * On successful request the GPIO pin is configured in accordance with
3749 * In case of error an ERR_PTR() is returned.
3751 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3752 const char *con_id, int index,
3753 enum gpiod_flags flags,
3756 struct gpio_desc *desc;
3757 char prop_name[32]; /* 32 is max size of property name */
3760 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3762 snprintf(prop_name, sizeof(prop_name), "%s-%s",
3763 con_id, gpio_suffixes[i]);
3765 snprintf(prop_name, sizeof(prop_name), "%s",
3768 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3770 if (!gpiod_not_found(desc))
3776 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
3779 * gpiod_count - return the number of GPIOs associated with a device / function
3780 * or -ENOENT if no GPIO has been assigned to the requested function
3781 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3782 * @con_id: function within the GPIO consumer
3784 int gpiod_count(struct device *dev, const char *con_id)
3786 int count = -ENOENT;
3788 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3789 count = of_gpio_get_count(dev, con_id);
3790 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3791 count = acpi_gpio_count(dev, con_id);
3794 count = platform_gpio_count(dev, con_id);
3798 EXPORT_SYMBOL_GPL(gpiod_count);
3801 * gpiod_get - obtain a GPIO for a given GPIO function
3802 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3803 * @con_id: function within the GPIO consumer
3804 * @flags: optional GPIO initialization flags
3806 * Return the GPIO descriptor corresponding to the function con_id of device
3807 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3808 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3810 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3811 enum gpiod_flags flags)
3813 return gpiod_get_index(dev, con_id, 0, flags);
3815 EXPORT_SYMBOL_GPL(gpiod_get);
3818 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3819 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3820 * @con_id: function within the GPIO consumer
3821 * @flags: optional GPIO initialization flags
3823 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3824 * the requested function it will return NULL. This is convenient for drivers
3825 * that need to handle optional GPIOs.
3827 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3829 enum gpiod_flags flags)
3831 return gpiod_get_index_optional(dev, con_id, 0, flags);
3833 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3837 * gpiod_configure_flags - helper function to configure a given GPIO
3838 * @desc: gpio whose value will be assigned
3839 * @con_id: function within the GPIO consumer
3840 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3841 * of_find_gpio() or of_get_gpio_hog()
3842 * @dflags: gpiod_flags - optional GPIO initialization flags
3844 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3845 * requested function and/or index, or another IS_ERR() code if an error
3846 * occurred while trying to acquire the GPIO.
3848 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3849 unsigned long lflags, enum gpiod_flags dflags)
3853 if (lflags & GPIO_ACTIVE_LOW)
3854 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3856 if (lflags & GPIO_OPEN_DRAIN)
3857 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3858 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3860 * This enforces open drain mode from the consumer side.
3861 * This is necessary for some busses like I2C, but the lookup
3862 * should *REALLY* have specified them as open drain in the
3863 * first place, so print a little warning here.
3865 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3867 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3870 if (lflags & GPIO_OPEN_SOURCE)
3871 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3873 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3875 "both pull-up and pull-down enabled, invalid configuration\n");
3879 if (lflags & GPIO_PULL_UP)
3880 set_bit(FLAG_PULL_UP, &desc->flags);
3881 else if (lflags & GPIO_PULL_DOWN)
3882 set_bit(FLAG_PULL_DOWN, &desc->flags);
3884 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3888 /* No particular flag request, return here... */
3889 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3890 gpiod_dbg(desc, "no flags found for %s\n", con_id);
3895 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3896 ret = gpiod_direction_output(desc,
3897 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3899 ret = gpiod_direction_input(desc);
3905 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3906 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3907 * @con_id: function within the GPIO consumer
3908 * @idx: index of the GPIO to obtain in the consumer
3909 * @flags: optional GPIO initialization flags
3911 * This variant of gpiod_get() allows to access GPIOs other than the first
3912 * defined one for functions that define several GPIOs.
3914 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3915 * requested function and/or index, or another IS_ERR() code if an error
3916 * occurred while trying to acquire the GPIO.
3918 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3921 enum gpiod_flags flags)
3923 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3924 struct gpio_desc *desc = NULL;
3926 /* Maybe we have a device name, maybe not */
3927 const char *devname = dev ? dev_name(dev) : "?";
3929 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3932 /* Using device tree? */
3933 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3934 dev_dbg(dev, "using device tree for GPIO lookup\n");
3935 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3936 } else if (ACPI_COMPANION(dev)) {
3937 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3938 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3943 * Either we are not using DT or ACPI, or their lookup did not return
3944 * a result. In that case, use platform lookup as a fallback.
3946 if (!desc || gpiod_not_found(desc)) {
3947 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3948 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3952 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3957 * If a connection label was passed use that, else attempt to use
3958 * the device name as label
3960 ret = gpiod_request(desc, con_id ? con_id : devname);
3962 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
3964 * This happens when there are several consumers for
3965 * the same GPIO line: we just return here without
3966 * further initialization. It is a bit if a hack.
3967 * This is necessary to support fixed regulators.
3969 * FIXME: Make this more sane and safe.
3971 dev_info(dev, "nonexclusive access to GPIO for %s\n",
3972 con_id ? con_id : devname);
3975 return ERR_PTR(ret);
3979 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3981 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3983 return ERR_PTR(ret);
3986 blocking_notifier_call_chain(&desc->gdev->notifier,
3987 GPIOLINE_CHANGED_REQUESTED, desc);
3991 EXPORT_SYMBOL_GPL(gpiod_get_index);
3994 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3995 * @fwnode: handle of the firmware node
3996 * @propname: name of the firmware property representing the GPIO
3997 * @index: index of the GPIO to obtain for the consumer
3998 * @dflags: GPIO initialization flags
3999 * @label: label to attach to the requested GPIO
4001 * This function can be used for drivers that get their configuration
4002 * from opaque firmware.
4004 * The function properly finds the corresponding GPIO using whatever is the
4005 * underlying firmware interface and then makes sure that the GPIO
4006 * descriptor is requested before it is returned to the caller.
4009 * On successful request the GPIO pin is configured in accordance with
4012 * In case of error an ERR_PTR() is returned.
4014 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
4015 const char *propname, int index,
4016 enum gpiod_flags dflags,
4019 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4020 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4024 return ERR_PTR(-EINVAL);
4026 if (is_of_node(fwnode)) {
4027 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4032 } else if (is_acpi_node(fwnode)) {
4033 struct acpi_gpio_info info;
4035 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
4039 acpi_gpio_update_gpiod_flags(&dflags, &info);
4040 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
4043 /* Currently only ACPI takes this path */
4044 ret = gpiod_request(desc, label);
4046 return ERR_PTR(ret);
4048 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4051 return ERR_PTR(ret);
4054 blocking_notifier_call_chain(&desc->gdev->notifier,
4055 GPIOLINE_CHANGED_REQUESTED, desc);
4059 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4062 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4064 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4065 * @con_id: function within the GPIO consumer
4066 * @index: index of the GPIO to obtain in the consumer
4067 * @flags: optional GPIO initialization flags
4069 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4070 * specified index was assigned to the requested function it will return NULL.
4071 * This is convenient for drivers that need to handle optional GPIOs.
4073 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4076 enum gpiod_flags flags)
4078 struct gpio_desc *desc;
4080 desc = gpiod_get_index(dev, con_id, index, flags);
4081 if (gpiod_not_found(desc))
4086 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4089 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4090 * @desc: gpio whose value will be assigned
4091 * @name: gpio line name
4092 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4093 * of_find_gpio() or of_get_gpio_hog()
4094 * @dflags: gpiod_flags - optional GPIO initialization flags
4096 int gpiod_hog(struct gpio_desc *desc, const char *name,
4097 unsigned long lflags, enum gpiod_flags dflags)
4099 struct gpio_chip *gc;
4100 struct gpio_desc *local_desc;
4104 gc = gpiod_to_chip(desc);
4105 hwnum = gpio_chip_hwgpio(desc);
4107 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4109 if (IS_ERR(local_desc)) {
4110 ret = PTR_ERR(local_desc);
4111 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4112 name, gc->label, hwnum, ret);
4116 /* Mark GPIO as hogged so it can be identified and removed later */
4117 set_bit(FLAG_IS_HOGGED, &desc->flags);
4119 gpiod_info(desc, "hogged as %s%s\n",
4120 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4121 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4122 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4128 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4129 * @gc: gpio chip to act on
4131 static void gpiochip_free_hogs(struct gpio_chip *gc)
4135 for (id = 0; id < gc->ngpio; id++) {
4136 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4137 gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
4142 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4143 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4144 * @con_id: function within the GPIO consumer
4145 * @flags: optional GPIO initialization flags
4147 * This function acquires all the GPIOs defined under a given function.
4149 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4150 * no GPIO has been assigned to the requested function, or another IS_ERR()
4151 * code if an error occurred while trying to acquire the GPIOs.
4153 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4155 enum gpiod_flags flags)
4157 struct gpio_desc *desc;
4158 struct gpio_descs *descs;
4159 struct gpio_array *array_info = NULL;
4160 struct gpio_chip *gc;
4161 int count, bitmap_size;
4163 count = gpiod_count(dev, con_id);
4165 return ERR_PTR(count);
4167 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4169 return ERR_PTR(-ENOMEM);
4171 for (descs->ndescs = 0; descs->ndescs < count; ) {
4172 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4174 gpiod_put_array(descs);
4175 return ERR_CAST(desc);
4178 descs->desc[descs->ndescs] = desc;
4180 gc = gpiod_to_chip(desc);
4182 * If pin hardware number of array member 0 is also 0, select
4183 * its chip as a candidate for fast bitmap processing path.
4185 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4186 struct gpio_descs *array;
4188 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4191 array = kzalloc(struct_size(descs, desc, count) +
4192 struct_size(array_info, invert_mask,
4193 3 * bitmap_size), GFP_KERNEL);
4195 gpiod_put_array(descs);
4196 return ERR_PTR(-ENOMEM);
4199 memcpy(array, descs,
4200 struct_size(descs, desc, descs->ndescs + 1));
4204 array_info = (void *)(descs->desc + count);
4205 array_info->get_mask = array_info->invert_mask +
4207 array_info->set_mask = array_info->get_mask +
4210 array_info->desc = descs->desc;
4211 array_info->size = count;
4212 array_info->chip = gc;
4213 bitmap_set(array_info->get_mask, descs->ndescs,
4214 count - descs->ndescs);
4215 bitmap_set(array_info->set_mask, descs->ndescs,
4216 count - descs->ndescs);
4217 descs->info = array_info;
4219 /* Unmark array members which don't belong to the 'fast' chip */
4220 if (array_info && array_info->chip != gc) {
4221 __clear_bit(descs->ndescs, array_info->get_mask);
4222 __clear_bit(descs->ndescs, array_info->set_mask);
4225 * Detect array members which belong to the 'fast' chip
4226 * but their pins are not in hardware order.
4228 else if (array_info &&
4229 gpio_chip_hwgpio(desc) != descs->ndescs) {
4231 * Don't use fast path if all array members processed so
4232 * far belong to the same chip as this one but its pin
4233 * hardware number is different from its array index.
4235 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4238 __clear_bit(descs->ndescs,
4239 array_info->get_mask);
4240 __clear_bit(descs->ndescs,
4241 array_info->set_mask);
4243 } else if (array_info) {
4244 /* Exclude open drain or open source from fast output */
4245 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4246 gpiochip_line_is_open_source(gc, descs->ndescs))
4247 __clear_bit(descs->ndescs,
4248 array_info->set_mask);
4249 /* Identify 'fast' pins which require invertion */
4250 if (gpiod_is_active_low(desc))
4251 __set_bit(descs->ndescs,
4252 array_info->invert_mask);
4259 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4260 array_info->chip->label, array_info->size,
4261 *array_info->get_mask, *array_info->set_mask,
4262 *array_info->invert_mask);
4265 EXPORT_SYMBOL_GPL(gpiod_get_array);
4268 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4270 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4271 * @con_id: function within the GPIO consumer
4272 * @flags: optional GPIO initialization flags
4274 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4275 * assigned to the requested function it will return NULL.
4277 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4279 enum gpiod_flags flags)
4281 struct gpio_descs *descs;
4283 descs = gpiod_get_array(dev, con_id, flags);
4284 if (gpiod_not_found(descs))
4289 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4292 * gpiod_put - dispose of a GPIO descriptor
4293 * @desc: GPIO descriptor to dispose of
4295 * No descriptor can be used after gpiod_put() has been called on it.
4297 void gpiod_put(struct gpio_desc *desc)
4302 EXPORT_SYMBOL_GPL(gpiod_put);
4305 * gpiod_put_array - dispose of multiple GPIO descriptors
4306 * @descs: struct gpio_descs containing an array of descriptors
4308 void gpiod_put_array(struct gpio_descs *descs)
4312 for (i = 0; i < descs->ndescs; i++)
4313 gpiod_put(descs->desc[i]);
4317 EXPORT_SYMBOL_GPL(gpiod_put_array);
4319 static int __init gpiolib_dev_init(void)
4323 /* Register GPIO sysfs bus */
4324 ret = bus_register(&gpio_bus_type);
4326 pr_err("gpiolib: could not register GPIO bus type\n");
4330 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4332 pr_err("gpiolib: failed to allocate char dev region\n");
4333 bus_unregister(&gpio_bus_type);
4337 gpiolib_initialized = true;
4338 gpiochip_setup_devs();
4340 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4341 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4342 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4346 core_initcall(gpiolib_dev_init);
4348 #ifdef CONFIG_DEBUG_FS
4350 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4353 struct gpio_chip *gc = gdev->chip;
4354 unsigned gpio = gdev->base;
4355 struct gpio_desc *gdesc = &gdev->descs[0];
4360 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4361 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4363 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4369 gpiod_get_direction(gdesc);
4370 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4371 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4372 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4373 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4374 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4375 is_out ? "out" : "in ",
4376 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
4377 is_irq ? "IRQ " : "",
4378 active_low ? "ACTIVE LOW" : "");
4379 seq_printf(s, "\n");
4383 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4385 unsigned long flags;
4386 struct gpio_device *gdev = NULL;
4387 loff_t index = *pos;
4391 spin_lock_irqsave(&gpio_lock, flags);
4392 list_for_each_entry(gdev, &gpio_devices, list)
4394 spin_unlock_irqrestore(&gpio_lock, flags);
4397 spin_unlock_irqrestore(&gpio_lock, flags);
4402 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4404 unsigned long flags;
4405 struct gpio_device *gdev = v;
4408 spin_lock_irqsave(&gpio_lock, flags);
4409 if (list_is_last(&gdev->list, &gpio_devices))
4412 ret = list_entry(gdev->list.next, struct gpio_device, list);
4413 spin_unlock_irqrestore(&gpio_lock, flags);
4421 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4425 static int gpiolib_seq_show(struct seq_file *s, void *v)
4427 struct gpio_device *gdev = v;
4428 struct gpio_chip *gc = gdev->chip;
4429 struct device *parent;
4432 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4433 dev_name(&gdev->dev));
4437 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4438 dev_name(&gdev->dev),
4439 gdev->base, gdev->base + gdev->ngpio - 1);
4440 parent = gc->parent;
4442 seq_printf(s, ", parent: %s/%s",
4443 parent->bus ? parent->bus->name : "no-bus",
4446 seq_printf(s, ", %s", gc->label);
4448 seq_printf(s, ", can sleep");
4449 seq_printf(s, ":\n");
4452 gc->dbg_show(s, gc);
4454 gpiolib_dbg_show(s, gdev);
4459 static const struct seq_operations gpiolib_sops = {
4460 .start = gpiolib_seq_start,
4461 .next = gpiolib_seq_next,
4462 .stop = gpiolib_seq_stop,
4463 .show = gpiolib_seq_show,
4465 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4467 static int __init gpiolib_debugfs_init(void)
4469 /* /sys/kernel/debug/gpio */
4470 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4473 subsys_initcall(gpiolib_debugfs_init);
4475 #endif /* DEBUG_FS */