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"
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/gpio.h>
33 /* Implementation infrastructure for GPIO interfaces.
35 * The GPIO programming interface allows for inlining speed-critical
36 * get/set operations for common cases, so that access to SOC-integrated
37 * GPIOs can sometimes cost only an instruction or two per bit.
41 /* When debugging, extend minimal trust to callers and platform code.
42 * Also emit diagnostic messages that may help initial bringup, when
43 * board setup or driver bugs are most common.
45 * Otherwise, minimize overhead in what may be bitbanging codepaths.
48 #define extra_checks 1
50 #define extra_checks 0
53 /* Device and char device-related information */
54 static DEFINE_IDA(gpio_ida);
55 static dev_t gpio_devt;
56 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
57 static struct bus_type gpio_bus_type = {
62 * Number of GPIOs to use for the fast path in set array
64 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
66 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
67 * While any GPIO is requested, its gpio_chip is not removable;
68 * each GPIO's "requested" flag serves as a lock and refcount.
70 DEFINE_SPINLOCK(gpio_lock);
72 static DEFINE_MUTEX(gpio_lookup_lock);
73 static LIST_HEAD(gpio_lookup_list);
74 LIST_HEAD(gpio_devices);
76 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
77 static LIST_HEAD(gpio_machine_hogs);
79 static void gpiochip_free_hogs(struct gpio_chip *gc);
80 static int gpiochip_add_irqchip(struct gpio_chip *gc,
81 struct lock_class_key *lock_key,
82 struct lock_class_key *request_key);
83 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
84 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
85 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
86 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
88 static bool gpiolib_initialized;
90 static inline void desc_set_label(struct gpio_desc *d, const char *label)
96 * gpio_to_desc - Convert a GPIO number to its descriptor
97 * @gpio: global GPIO number
100 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
101 * with the given number exists in the system.
103 struct gpio_desc *gpio_to_desc(unsigned gpio)
105 struct gpio_device *gdev;
108 spin_lock_irqsave(&gpio_lock, flags);
110 list_for_each_entry(gdev, &gpio_devices, list) {
111 if (gdev->base <= gpio &&
112 gdev->base + gdev->ngpio > gpio) {
113 spin_unlock_irqrestore(&gpio_lock, flags);
114 return &gdev->descs[gpio - gdev->base];
118 spin_unlock_irqrestore(&gpio_lock, flags);
120 if (!gpio_is_valid(gpio))
121 WARN(1, "invalid GPIO %d\n", gpio);
125 EXPORT_SYMBOL_GPL(gpio_to_desc);
128 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
129 * hardware number for this chip
131 * @hwnum: hardware number of the GPIO for this chip
134 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
135 * in the given chip for the specified hardware number.
137 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
140 struct gpio_device *gdev = gc->gpiodev;
142 if (hwnum >= gdev->ngpio)
143 return ERR_PTR(-EINVAL);
145 return &gdev->descs[hwnum];
147 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
150 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
151 * @desc: GPIO descriptor
153 * This should disappear in the future but is needed since we still
154 * use GPIO numbers for error messages and sysfs nodes.
157 * The global GPIO number for the GPIO specified by its descriptor.
159 int desc_to_gpio(const struct gpio_desc *desc)
161 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
163 EXPORT_SYMBOL_GPL(desc_to_gpio);
167 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
168 * @desc: descriptor to return the chip of
170 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
172 if (!desc || !desc->gdev)
174 return desc->gdev->chip;
176 EXPORT_SYMBOL_GPL(gpiod_to_chip);
178 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
179 static int gpiochip_find_base(int ngpio)
181 struct gpio_device *gdev;
182 int base = ARCH_NR_GPIOS - ngpio;
184 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
185 /* found a free space? */
186 if (gdev->base + gdev->ngpio <= base)
189 /* nope, check the space right before the chip */
190 base = gdev->base - ngpio;
193 if (gpio_is_valid(base)) {
194 pr_debug("%s: found new base at %d\n", __func__, base);
197 pr_err("%s: cannot find free range\n", __func__);
203 * gpiod_get_direction - return the current direction of a GPIO
204 * @desc: GPIO to get the direction of
206 * Returns 0 for output, 1 for input, or an error code in case of error.
208 * This function may sleep if gpiod_cansleep() is true.
210 int gpiod_get_direction(struct gpio_desc *desc)
212 struct gpio_chip *gc;
216 gc = gpiod_to_chip(desc);
217 offset = gpio_chip_hwgpio(desc);
220 * Open drain emulation using input mode may incorrectly report
221 * input here, fix that up.
223 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
224 test_bit(FLAG_IS_OUT, &desc->flags))
227 if (!gc->get_direction)
230 ret = gc->get_direction(gc, offset);
234 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
238 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
242 EXPORT_SYMBOL_GPL(gpiod_get_direction);
245 * Add a new chip to the global chips list, keeping the list of chips sorted
246 * by range(means [base, base + ngpio - 1]) order.
248 * Return -EBUSY if the new chip overlaps with some other chip's integer
251 static int gpiodev_add_to_list(struct gpio_device *gdev)
253 struct gpio_device *prev, *next;
255 if (list_empty(&gpio_devices)) {
256 /* initial entry in list */
257 list_add_tail(&gdev->list, &gpio_devices);
261 next = list_entry(gpio_devices.next, struct gpio_device, list);
262 if (gdev->base + gdev->ngpio <= next->base) {
263 /* add before first entry */
264 list_add(&gdev->list, &gpio_devices);
268 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
269 if (prev->base + prev->ngpio <= gdev->base) {
270 /* add behind last entry */
271 list_add_tail(&gdev->list, &gpio_devices);
275 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
276 /* at the end of the list */
277 if (&next->list == &gpio_devices)
280 /* add between prev and next */
281 if (prev->base + prev->ngpio <= gdev->base
282 && gdev->base + gdev->ngpio <= next->base) {
283 list_add(&gdev->list, &prev->list);
288 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
293 * Convert a GPIO name to its descriptor
294 * Note that there is no guarantee that GPIO names are globally unique!
295 * Hence this function will return, if it exists, a reference to the first GPIO
296 * line found that matches the given name.
298 static struct gpio_desc *gpio_name_to_desc(const char * const name)
300 struct gpio_device *gdev;
306 spin_lock_irqsave(&gpio_lock, flags);
308 list_for_each_entry(gdev, &gpio_devices, list) {
311 for (i = 0; i != gdev->ngpio; ++i) {
312 struct gpio_desc *desc = &gdev->descs[i];
317 if (!strcmp(desc->name, name)) {
318 spin_unlock_irqrestore(&gpio_lock, flags);
324 spin_unlock_irqrestore(&gpio_lock, flags);
330 * Take the names from gc->names and assign them to their GPIO descriptors.
331 * Warn if a name is already used for a GPIO line on a different GPIO chip.
334 * 1. Non-unique names are still accepted,
335 * 2. Name collisions within the same GPIO chip are not reported.
337 static int gpiochip_set_desc_names(struct gpio_chip *gc)
339 struct gpio_device *gdev = gc->gpiodev;
345 /* First check all names if they are unique */
346 for (i = 0; i != gc->ngpio; ++i) {
347 struct gpio_desc *gpio;
349 gpio = gpio_name_to_desc(gc->names[i]);
352 "Detected name collision for GPIO name '%s'\n",
356 /* Then add all names to the GPIO descriptors */
357 for (i = 0; i != gc->ngpio; ++i)
358 gdev->descs[i].name = gc->names[i];
363 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
367 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
371 /* Assume by default all GPIOs are valid */
372 bitmap_fill(p, gc->ngpio);
377 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
379 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
382 gc->valid_mask = gpiochip_allocate_mask(gc);
389 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
391 if (gc->init_valid_mask)
392 return gc->init_valid_mask(gc,
399 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
401 bitmap_free(gc->valid_mask);
402 gc->valid_mask = NULL;
405 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
407 if (gc->add_pin_ranges)
408 return gc->add_pin_ranges(gc);
413 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
416 /* No mask means all valid */
417 if (likely(!gc->valid_mask))
419 return test_bit(offset, gc->valid_mask);
421 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
423 static void gpiodevice_release(struct device *dev)
425 struct gpio_device *gdev = dev_get_drvdata(dev);
427 list_del(&gdev->list);
428 ida_simple_remove(&gpio_ida, gdev->id);
429 kfree_const(gdev->label);
434 static int gpiochip_setup_dev(struct gpio_device *gdev)
438 ret = gpiolib_cdev_register(gdev, gpio_devt);
442 ret = gpiochip_sysfs_register(gdev);
444 goto err_remove_device;
446 /* From this point, the .release() function cleans up gpio_device */
447 gdev->dev.release = gpiodevice_release;
448 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
449 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
454 gpiolib_cdev_unregister(gdev);
458 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
460 struct gpio_desc *desc;
463 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
465 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
470 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
473 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
475 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
476 __func__, gc->label, hog->chip_hwnum, rv);
479 static void machine_gpiochip_add(struct gpio_chip *gc)
481 struct gpiod_hog *hog;
483 mutex_lock(&gpio_machine_hogs_mutex);
485 list_for_each_entry(hog, &gpio_machine_hogs, list) {
486 if (!strcmp(gc->label, hog->chip_label))
487 gpiochip_machine_hog(gc, hog);
490 mutex_unlock(&gpio_machine_hogs_mutex);
493 static void gpiochip_setup_devs(void)
495 struct gpio_device *gdev;
498 list_for_each_entry(gdev, &gpio_devices, list) {
499 ret = gpiochip_setup_dev(gdev);
502 "Failed to initialize gpio device (%d)\n", ret);
506 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
507 struct lock_class_key *lock_key,
508 struct lock_class_key *request_key)
514 struct gpio_device *gdev;
517 * First: allocate and populate the internal stat container, and
518 * set up the struct device.
520 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
523 gdev->dev.bus = &gpio_bus_type;
527 gdev->dev.parent = gc->parent;
528 gdev->dev.of_node = gc->parent->of_node;
531 #ifdef CONFIG_OF_GPIO
532 /* If the gpiochip has an assigned OF node this takes precedence */
534 gdev->dev.of_node = gc->of_node;
536 gc->of_node = gdev->dev.of_node;
539 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
544 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
545 device_initialize(&gdev->dev);
546 dev_set_drvdata(&gdev->dev, gdev);
547 if (gc->parent && gc->parent->driver)
548 gdev->owner = gc->parent->driver->owner;
550 /* TODO: remove chip->owner */
551 gdev->owner = gc->owner;
553 gdev->owner = THIS_MODULE;
555 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
561 if (gc->ngpio == 0) {
562 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
567 if (gc->ngpio > FASTPATH_NGPIO)
568 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
569 gc->ngpio, FASTPATH_NGPIO);
571 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
577 gdev->ngpio = gc->ngpio;
580 spin_lock_irqsave(&gpio_lock, flags);
583 * TODO: this allocates a Linux GPIO number base in the global
584 * GPIO numberspace for this chip. In the long run we want to
585 * get *rid* of this numberspace and use only descriptors, but
586 * it may be a pipe dream. It will not happen before we get rid
587 * of the sysfs interface anyways.
590 base = gpiochip_find_base(gc->ngpio);
593 spin_unlock_irqrestore(&gpio_lock, flags);
597 * TODO: it should not be necessary to reflect the assigned
598 * base outside of the GPIO subsystem. Go over drivers and
599 * see if anyone makes use of this, else drop this and assign
606 ret = gpiodev_add_to_list(gdev);
608 spin_unlock_irqrestore(&gpio_lock, flags);
612 for (i = 0; i < gc->ngpio; i++)
613 gdev->descs[i].gdev = gdev;
615 spin_unlock_irqrestore(&gpio_lock, flags);
617 ATOMIC_INIT_NOTIFIER_HEAD(&gdev->notifier);
619 #ifdef CONFIG_PINCTRL
620 INIT_LIST_HEAD(&gdev->pin_ranges);
623 ret = gpiochip_set_desc_names(gc);
625 goto err_remove_from_list;
627 ret = gpiochip_alloc_valid_mask(gc);
629 goto err_remove_from_list;
631 ret = of_gpiochip_add(gc);
633 goto err_free_gpiochip_mask;
635 ret = gpiochip_init_valid_mask(gc);
637 goto err_remove_of_chip;
639 for (i = 0; i < gc->ngpio; i++) {
640 struct gpio_desc *desc = &gdev->descs[i];
642 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
643 assign_bit(FLAG_IS_OUT,
644 &desc->flags, !gc->get_direction(gc, i));
646 assign_bit(FLAG_IS_OUT,
647 &desc->flags, !gc->direction_input);
651 ret = gpiochip_add_pin_ranges(gc);
653 goto err_remove_of_chip;
655 acpi_gpiochip_add(gc);
657 machine_gpiochip_add(gc);
659 ret = gpiochip_irqchip_init_valid_mask(gc);
661 goto err_remove_acpi_chip;
663 ret = gpiochip_irqchip_init_hw(gc);
665 goto err_remove_acpi_chip;
667 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
669 goto err_remove_irqchip_mask;
672 * By first adding the chardev, and then adding the device,
673 * we get a device node entry in sysfs under
674 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
675 * coldplug of device nodes and other udev business.
676 * We can do this only if gpiolib has been initialized.
677 * Otherwise, defer until later.
679 if (gpiolib_initialized) {
680 ret = gpiochip_setup_dev(gdev);
682 goto err_remove_irqchip;
687 gpiochip_irqchip_remove(gc);
688 err_remove_irqchip_mask:
689 gpiochip_irqchip_free_valid_mask(gc);
690 err_remove_acpi_chip:
691 acpi_gpiochip_remove(gc);
693 gpiochip_free_hogs(gc);
694 of_gpiochip_remove(gc);
695 err_free_gpiochip_mask:
696 gpiochip_remove_pin_ranges(gc);
697 gpiochip_free_valid_mask(gc);
698 err_remove_from_list:
699 spin_lock_irqsave(&gpio_lock, flags);
700 list_del(&gdev->list);
701 spin_unlock_irqrestore(&gpio_lock, flags);
703 kfree_const(gdev->label);
707 ida_simple_remove(&gpio_ida, gdev->id);
709 /* failures here can mean systems won't boot... */
710 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
711 gdev->base, gdev->base + gdev->ngpio - 1,
712 gc->label ? : "generic", ret);
716 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
719 * gpiochip_get_data() - get per-subdriver data for the chip
723 * The per-subdriver data for the chip.
725 void *gpiochip_get_data(struct gpio_chip *gc)
727 return gc->gpiodev->data;
729 EXPORT_SYMBOL_GPL(gpiochip_get_data);
732 * gpiochip_remove() - unregister a gpio_chip
733 * @gc: the chip to unregister
735 * A gpio_chip with any GPIOs still requested may not be removed.
737 void gpiochip_remove(struct gpio_chip *gc)
739 struct gpio_device *gdev = gc->gpiodev;
743 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
744 gpiochip_sysfs_unregister(gdev);
745 gpiochip_free_hogs(gc);
746 /* Numb the device, cancelling all outstanding operations */
748 gpiochip_irqchip_remove(gc);
749 acpi_gpiochip_remove(gc);
750 of_gpiochip_remove(gc);
751 gpiochip_remove_pin_ranges(gc);
752 gpiochip_free_valid_mask(gc);
754 * We accept no more calls into the driver from this point, so
755 * NULL the driver data pointer
759 spin_lock_irqsave(&gpio_lock, flags);
760 for (i = 0; i < gdev->ngpio; i++) {
761 if (gpiochip_is_requested(gc, i))
764 spin_unlock_irqrestore(&gpio_lock, flags);
766 if (i != gdev->ngpio)
768 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
771 * The gpiochip side puts its use of the device to rest here:
772 * if there are no userspace clients, the chardev and device will
773 * be removed, else it will be dangling until the last user is
776 gpiolib_cdev_unregister(gdev);
777 put_device(&gdev->dev);
779 EXPORT_SYMBOL_GPL(gpiochip_remove);
782 * gpiochip_find() - iterator for locating a specific gpio_chip
783 * @data: data to pass to match function
784 * @match: Callback function to check gpio_chip
786 * Similar to bus_find_device. It returns a reference to a gpio_chip as
787 * determined by a user supplied @match callback. The callback should return
788 * 0 if the device doesn't match and non-zero if it does. If the callback is
789 * non-zero, this function will return to the caller and not iterate over any
792 struct gpio_chip *gpiochip_find(void *data,
793 int (*match)(struct gpio_chip *gc,
796 struct gpio_device *gdev;
797 struct gpio_chip *gc = NULL;
800 spin_lock_irqsave(&gpio_lock, flags);
801 list_for_each_entry(gdev, &gpio_devices, list)
802 if (gdev->chip && match(gdev->chip, data)) {
807 spin_unlock_irqrestore(&gpio_lock, flags);
811 EXPORT_SYMBOL_GPL(gpiochip_find);
813 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
815 const char *name = data;
817 return !strcmp(gc->label, name);
820 static struct gpio_chip *find_chip_by_name(const char *name)
822 return gpiochip_find((void *)name, gpiochip_match_name);
825 #ifdef CONFIG_GPIOLIB_IRQCHIP
828 * The following is irqchip helper code for gpiochips.
831 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
833 struct gpio_irq_chip *girq = &gc->irq;
838 return girq->init_hw(gc);
841 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
843 struct gpio_irq_chip *girq = &gc->irq;
845 if (!girq->init_valid_mask)
848 girq->valid_mask = gpiochip_allocate_mask(gc);
849 if (!girq->valid_mask)
852 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
857 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
859 bitmap_free(gc->irq.valid_mask);
860 gc->irq.valid_mask = NULL;
863 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
866 if (!gpiochip_line_is_valid(gc, offset))
868 /* No mask means all valid */
869 if (likely(!gc->irq.valid_mask))
871 return test_bit(offset, gc->irq.valid_mask);
873 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
876 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
877 * @gc: the gpiochip to set the irqchip chain to
878 * @parent_irq: the irq number corresponding to the parent IRQ for this
880 * @parent_handler: the parent interrupt handler for the accumulated IRQ
881 * coming out of the gpiochip. If the interrupt is nested rather than
882 * cascaded, pass NULL in this handler argument
884 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
885 unsigned int parent_irq,
886 irq_flow_handler_t parent_handler)
888 struct gpio_irq_chip *girq = &gc->irq;
889 struct device *dev = &gc->gpiodev->dev;
892 chip_err(gc, "called %s before setting up irqchip\n",
897 if (parent_handler) {
900 "you cannot have chained interrupts on a chip that may sleep\n");
903 girq->parents = devm_kcalloc(dev, 1,
904 sizeof(*girq->parents),
906 if (!girq->parents) {
907 chip_err(gc, "out of memory allocating parent IRQ\n");
910 girq->parents[0] = parent_irq;
911 girq->num_parents = 1;
913 * The parent irqchip is already using the chip_data for this
914 * irqchip, so our callbacks simply use the handler_data.
916 irq_set_chained_handler_and_data(parent_irq, parent_handler,
922 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
923 * @gc: the gpiochip to set the irqchip nested handler to
924 * @irqchip: the irqchip to nest to the gpiochip
925 * @parent_irq: the irq number corresponding to the parent IRQ for this
928 void gpiochip_set_nested_irqchip(struct gpio_chip *gc,
929 struct irq_chip *irqchip,
930 unsigned int parent_irq)
932 gpiochip_set_cascaded_irqchip(gc, parent_irq, NULL);
934 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
936 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
939 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
941 * @gc: the gpiochip to set the irqchip hierarchical handler to
942 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
943 * will then percolate up to the parent
945 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
946 struct irq_chip *irqchip)
948 /* DT will deal with mapping each IRQ as we go along */
949 if (is_of_node(gc->irq.fwnode))
953 * This is for legacy and boardfile "irqchip" fwnodes: allocate
954 * irqs upfront instead of dynamically since we don't have the
955 * dynamic type of allocation that hardware description languages
956 * provide. Once all GPIO drivers using board files are gone from
957 * the kernel we can delete this code, but for a transitional period
958 * it is necessary to keep this around.
960 if (is_fwnode_irqchip(gc->irq.fwnode)) {
964 for (i = 0; i < gc->ngpio; i++) {
965 struct irq_fwspec fwspec;
966 unsigned int parent_hwirq;
967 unsigned int parent_type;
968 struct gpio_irq_chip *girq = &gc->irq;
971 * We call the child to parent translation function
972 * only to check if the child IRQ is valid or not.
973 * Just pick the rising edge type here as that is what
974 * we likely need to support.
976 ret = girq->child_to_parent_hwirq(gc, i,
977 IRQ_TYPE_EDGE_RISING,
981 chip_err(gc, "skip set-up on hwirq %d\n",
986 fwspec.fwnode = gc->irq.fwnode;
987 /* This is the hwirq for the GPIO line side of things */
988 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
989 /* Just pick something */
990 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
991 fwspec.param_count = 2;
992 ret = __irq_domain_alloc_irqs(gc->irq.domain,
993 /* just pick something */
1002 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1009 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1014 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1015 struct irq_fwspec *fwspec,
1016 unsigned long *hwirq,
1019 /* We support standard DT translation */
1020 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1021 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1024 /* This is for board files and others not using DT */
1025 if (is_fwnode_irqchip(fwspec->fwnode)) {
1028 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1031 WARN_ON(*type == IRQ_TYPE_NONE);
1037 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1039 unsigned int nr_irqs,
1042 struct gpio_chip *gc = d->host_data;
1043 irq_hw_number_t hwirq;
1044 unsigned int type = IRQ_TYPE_NONE;
1045 struct irq_fwspec *fwspec = data;
1047 unsigned int parent_hwirq;
1048 unsigned int parent_type;
1049 struct gpio_irq_chip *girq = &gc->irq;
1053 * The nr_irqs parameter is always one except for PCI multi-MSI
1054 * so this should not happen.
1056 WARN_ON(nr_irqs != 1);
1058 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1062 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1064 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1065 &parent_hwirq, &parent_type);
1067 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1070 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1073 * We set handle_bad_irq because the .set_type() should
1074 * always be invoked and set the right type of handler.
1076 irq_domain_set_info(d,
1085 /* This parent only handles asserted level IRQs */
1086 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1090 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1092 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1093 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1095 * If the parent irqdomain is msi, the interrupts have already
1096 * been allocated, so the EEXIST is good.
1098 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1102 "failed to allocate parent hwirq %d for hwirq %lu\n",
1103 parent_hwirq, hwirq);
1109 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1110 unsigned int offset)
1115 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1117 ops->activate = gpiochip_irq_domain_activate;
1118 ops->deactivate = gpiochip_irq_domain_deactivate;
1119 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1120 ops->free = irq_domain_free_irqs_common;
1123 * We only allow overriding the translate() function for
1124 * hierarchical chips, and this should only be done if the user
1125 * really need something other than 1:1 translation.
1127 if (!ops->translate)
1128 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1131 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1133 if (!gc->irq.child_to_parent_hwirq ||
1135 chip_err(gc, "missing irqdomain vital data\n");
1139 if (!gc->irq.child_offset_to_irq)
1140 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1142 if (!gc->irq.populate_parent_alloc_arg)
1143 gc->irq.populate_parent_alloc_arg =
1144 gpiochip_populate_parent_fwspec_twocell;
1146 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1148 gc->irq.domain = irq_domain_create_hierarchy(
1149 gc->irq.parent_domain,
1153 &gc->irq.child_irq_domain_ops,
1156 if (!gc->irq.domain)
1159 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1164 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1166 return !!gc->irq.parent_domain;
1169 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1170 unsigned int parent_hwirq,
1171 unsigned int parent_type)
1173 struct irq_fwspec *fwspec;
1175 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1179 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1180 fwspec->param_count = 2;
1181 fwspec->param[0] = parent_hwirq;
1182 fwspec->param[1] = parent_type;
1186 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1188 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1189 unsigned int parent_hwirq,
1190 unsigned int parent_type)
1192 struct irq_fwspec *fwspec;
1194 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1198 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1199 fwspec->param_count = 4;
1200 fwspec->param[0] = 0;
1201 fwspec->param[1] = parent_hwirq;
1202 fwspec->param[2] = 0;
1203 fwspec->param[3] = parent_type;
1207 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1211 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1216 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1221 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1224 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1225 * @d: the irqdomain used by this irqchip
1226 * @irq: the global irq number used by this GPIO irqchip irq
1227 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1229 * This function will set up the mapping for a certain IRQ line on a
1230 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1231 * stored inside the gpiochip.
1233 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1234 irq_hw_number_t hwirq)
1236 struct gpio_chip *gc = d->host_data;
1239 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1242 irq_set_chip_data(irq, gc);
1244 * This lock class tells lockdep that GPIO irqs are in a different
1245 * category than their parents, so it won't report false recursion.
1247 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1248 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1249 /* Chips that use nested thread handlers have them marked */
1250 if (gc->irq.threaded)
1251 irq_set_nested_thread(irq, 1);
1252 irq_set_noprobe(irq);
1254 if (gc->irq.num_parents == 1)
1255 ret = irq_set_parent(irq, gc->irq.parents[0]);
1256 else if (gc->irq.map)
1257 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1263 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1264 * is passed as default type.
1266 if (gc->irq.default_type != IRQ_TYPE_NONE)
1267 irq_set_irq_type(irq, gc->irq.default_type);
1271 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1273 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1275 struct gpio_chip *gc = d->host_data;
1277 if (gc->irq.threaded)
1278 irq_set_nested_thread(irq, 0);
1279 irq_set_chip_and_handler(irq, NULL, NULL);
1280 irq_set_chip_data(irq, NULL);
1282 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1284 static const struct irq_domain_ops gpiochip_domain_ops = {
1285 .map = gpiochip_irq_map,
1286 .unmap = gpiochip_irq_unmap,
1287 /* Virtually all GPIO irqchips are twocell:ed */
1288 .xlate = irq_domain_xlate_twocell,
1292 * TODO: move these activate/deactivate in under the hierarchicial
1293 * irqchip implementation as static once SPMI and SSBI (all external
1294 * users) are phased over.
1297 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1298 * @domain: The IRQ domain used by this IRQ chip
1299 * @data: Outermost irq_data associated with the IRQ
1300 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1302 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1303 * used as the activate function for the &struct irq_domain_ops. The host_data
1304 * for the IRQ domain must be the &struct gpio_chip.
1306 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1307 struct irq_data *data, bool reserve)
1309 struct gpio_chip *gc = domain->host_data;
1311 return gpiochip_lock_as_irq(gc, data->hwirq);
1313 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1316 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1317 * @domain: The IRQ domain used by this IRQ chip
1318 * @data: Outermost irq_data associated with the IRQ
1320 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1321 * be used as the deactivate function for the &struct irq_domain_ops. The
1322 * host_data for the IRQ domain must be the &struct gpio_chip.
1324 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1325 struct irq_data *data)
1327 struct gpio_chip *gc = domain->host_data;
1329 return gpiochip_unlock_as_irq(gc, data->hwirq);
1331 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1333 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned offset)
1335 struct irq_domain *domain = gc->irq.domain;
1337 if (!gpiochip_irqchip_irq_valid(gc, offset))
1340 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1341 if (irq_domain_is_hierarchy(domain)) {
1342 struct irq_fwspec spec;
1344 spec.fwnode = domain->fwnode;
1345 spec.param_count = 2;
1346 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1347 spec.param[1] = IRQ_TYPE_NONE;
1349 return irq_create_fwspec_mapping(&spec);
1353 return irq_create_mapping(domain, offset);
1356 static int gpiochip_irq_reqres(struct irq_data *d)
1358 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1360 return gpiochip_reqres_irq(gc, d->hwirq);
1363 static void gpiochip_irq_relres(struct irq_data *d)
1365 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1367 gpiochip_relres_irq(gc, d->hwirq);
1370 static void gpiochip_irq_mask(struct irq_data *d)
1372 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1374 if (gc->irq.irq_mask)
1375 gc->irq.irq_mask(d);
1376 gpiochip_disable_irq(gc, d->hwirq);
1379 static void gpiochip_irq_unmask(struct irq_data *d)
1381 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1383 gpiochip_enable_irq(gc, d->hwirq);
1384 if (gc->irq.irq_unmask)
1385 gc->irq.irq_unmask(d);
1388 static void gpiochip_irq_enable(struct irq_data *d)
1390 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1392 gpiochip_enable_irq(gc, d->hwirq);
1393 gc->irq.irq_enable(d);
1396 static void gpiochip_irq_disable(struct irq_data *d)
1398 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1400 gc->irq.irq_disable(d);
1401 gpiochip_disable_irq(gc, d->hwirq);
1404 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1406 struct irq_chip *irqchip = gc->irq.chip;
1408 if (!irqchip->irq_request_resources &&
1409 !irqchip->irq_release_resources) {
1410 irqchip->irq_request_resources = gpiochip_irq_reqres;
1411 irqchip->irq_release_resources = gpiochip_irq_relres;
1413 if (WARN_ON(gc->irq.irq_enable))
1415 /* Check if the irqchip already has this hook... */
1416 if (irqchip->irq_enable == gpiochip_irq_enable) {
1418 * ...and if so, give a gentle warning that this is bad
1422 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1426 if (irqchip->irq_disable) {
1427 gc->irq.irq_disable = irqchip->irq_disable;
1428 irqchip->irq_disable = gpiochip_irq_disable;
1430 gc->irq.irq_mask = irqchip->irq_mask;
1431 irqchip->irq_mask = gpiochip_irq_mask;
1434 if (irqchip->irq_enable) {
1435 gc->irq.irq_enable = irqchip->irq_enable;
1436 irqchip->irq_enable = gpiochip_irq_enable;
1438 gc->irq.irq_unmask = irqchip->irq_unmask;
1439 irqchip->irq_unmask = gpiochip_irq_unmask;
1444 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1445 * @gc: the GPIO chip to add the IRQ chip to
1446 * @lock_key: lockdep class for IRQ lock
1447 * @request_key: lockdep class for IRQ request
1449 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1450 struct lock_class_key *lock_key,
1451 struct lock_class_key *request_key)
1453 struct irq_chip *irqchip = gc->irq.chip;
1454 const struct irq_domain_ops *ops = NULL;
1455 struct device_node *np;
1462 if (gc->irq.parent_handler && gc->can_sleep) {
1463 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1467 np = gc->gpiodev->dev.of_node;
1468 type = gc->irq.default_type;
1471 * Specifying a default trigger is a terrible idea if DT or ACPI is
1472 * used to configure the interrupts, as you may end up with
1473 * conflicting triggers. Tell the user, and reset to NONE.
1475 if (WARN(np && type != IRQ_TYPE_NONE,
1476 "%s: Ignoring %u default trigger\n", np->full_name, type))
1477 type = IRQ_TYPE_NONE;
1479 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1480 acpi_handle_warn(ACPI_HANDLE(gc->parent),
1481 "Ignoring %u default trigger\n", type);
1482 type = IRQ_TYPE_NONE;
1485 gc->to_irq = gpiochip_to_irq;
1486 gc->irq.default_type = type;
1487 gc->irq.lock_key = lock_key;
1488 gc->irq.request_key = request_key;
1490 /* If a parent irqdomain is provided, let's build a hierarchy */
1491 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1492 int ret = gpiochip_hierarchy_add_domain(gc);
1496 /* Some drivers provide custom irqdomain ops */
1497 if (gc->irq.domain_ops)
1498 ops = gc->irq.domain_ops;
1501 ops = &gpiochip_domain_ops;
1502 gc->irq.domain = irq_domain_add_simple(np,
1506 if (!gc->irq.domain)
1510 if (gc->irq.parent_handler) {
1511 void *data = gc->irq.parent_handler_data ?: gc;
1513 for (i = 0; i < gc->irq.num_parents; i++) {
1515 * The parent IRQ chip is already using the chip_data
1516 * for this IRQ chip, so our callbacks simply use the
1519 irq_set_chained_handler_and_data(gc->irq.parents[i],
1520 gc->irq.parent_handler,
1525 gpiochip_set_irq_hooks(gc);
1527 acpi_gpiochip_request_interrupts(gc);
1533 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1534 * @gc: the gpiochip to remove the irqchip from
1536 * This is called only from gpiochip_remove()
1538 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1540 struct irq_chip *irqchip = gc->irq.chip;
1541 unsigned int offset;
1543 acpi_gpiochip_free_interrupts(gc);
1545 if (irqchip && gc->irq.parent_handler) {
1546 struct gpio_irq_chip *irq = &gc->irq;
1549 for (i = 0; i < irq->num_parents; i++)
1550 irq_set_chained_handler_and_data(irq->parents[i],
1554 /* Remove all IRQ mappings and delete the domain */
1555 if (gc->irq.domain) {
1558 for (offset = 0; offset < gc->ngpio; offset++) {
1559 if (!gpiochip_irqchip_irq_valid(gc, offset))
1562 irq = irq_find_mapping(gc->irq.domain, offset);
1563 irq_dispose_mapping(irq);
1566 irq_domain_remove(gc->irq.domain);
1570 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1571 irqchip->irq_request_resources = NULL;
1572 irqchip->irq_release_resources = NULL;
1574 if (irqchip->irq_enable == gpiochip_irq_enable) {
1575 irqchip->irq_enable = gc->irq.irq_enable;
1576 irqchip->irq_disable = gc->irq.irq_disable;
1579 gc->irq.irq_enable = NULL;
1580 gc->irq.irq_disable = NULL;
1581 gc->irq.chip = NULL;
1583 gpiochip_irqchip_free_valid_mask(gc);
1587 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1588 * @gc: the gpiochip to add the irqchip to
1589 * @irqchip: the irqchip to add to the gpiochip
1590 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1591 * allocate gpiochip irqs from
1592 * @handler: the irq handler to use (often a predefined irq core function)
1593 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1594 * to have the core avoid setting up any default type in the hardware.
1595 * @threaded: whether this irqchip uses a nested thread handler
1596 * @lock_key: lockdep class for IRQ lock
1597 * @request_key: lockdep class for IRQ request
1599 * This function closely associates a certain irqchip with a certain
1600 * gpiochip, providing an irq domain to translate the local IRQs to
1601 * global irqs in the gpiolib core, and making sure that the gpiochip
1602 * is passed as chip data to all related functions. Driver callbacks
1603 * need to use gpiochip_get_data() to get their local state containers back
1604 * from the gpiochip passed as chip data. An irqdomain will be stored
1605 * in the gpiochip that shall be used by the driver to handle IRQ number
1606 * translation. The gpiochip will need to be initialized and registered
1607 * before calling this function.
1609 * This function will handle two cell:ed simple IRQs and assumes all
1610 * the pins on the gpiochip can generate a unique IRQ. Everything else
1611 * need to be open coded.
1613 int gpiochip_irqchip_add_key(struct gpio_chip *gc,
1614 struct irq_chip *irqchip,
1615 unsigned int first_irq,
1616 irq_flow_handler_t handler,
1619 struct lock_class_key *lock_key,
1620 struct lock_class_key *request_key)
1622 struct device_node *of_node;
1624 if (!gc || !irqchip)
1628 chip_err(gc, "missing gpiochip .dev parent pointer\n");
1631 gc->irq.threaded = threaded;
1632 of_node = gc->parent->of_node;
1633 #ifdef CONFIG_OF_GPIO
1635 * If the gpiochip has an assigned OF node this takes precedence
1636 * FIXME: get rid of this and use gc->parent->of_node
1640 of_node = gc->of_node;
1643 * Specifying a default trigger is a terrible idea if DT or ACPI is
1644 * used to configure the interrupts, as you may end-up with
1645 * conflicting triggers. Tell the user, and reset to NONE.
1647 if (WARN(of_node && type != IRQ_TYPE_NONE,
1648 "%pOF: Ignoring %d default trigger\n", of_node, type))
1649 type = IRQ_TYPE_NONE;
1650 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1651 acpi_handle_warn(ACPI_HANDLE(gc->parent),
1652 "Ignoring %d default trigger\n", type);
1653 type = IRQ_TYPE_NONE;
1656 gc->irq.chip = irqchip;
1657 gc->irq.handler = handler;
1658 gc->irq.default_type = type;
1659 gc->to_irq = gpiochip_to_irq;
1660 gc->irq.lock_key = lock_key;
1661 gc->irq.request_key = request_key;
1662 gc->irq.domain = irq_domain_add_simple(of_node,
1663 gc->ngpio, first_irq,
1664 &gpiochip_domain_ops, gc);
1665 if (!gc->irq.domain) {
1666 gc->irq.chip = NULL;
1670 gpiochip_set_irq_hooks(gc);
1672 acpi_gpiochip_request_interrupts(gc);
1676 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
1679 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1680 * @gc: the gpiochip to add the irqchip to
1681 * @domain: the irqdomain to add to the gpiochip
1683 * This function adds an IRQ domain to the gpiochip.
1685 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1686 struct irq_domain *domain)
1691 gc->to_irq = gpiochip_to_irq;
1692 gc->irq.domain = domain;
1696 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1698 #else /* CONFIG_GPIOLIB_IRQCHIP */
1700 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1701 struct lock_class_key *lock_key,
1702 struct lock_class_key *request_key)
1706 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1708 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1713 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1717 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1720 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1723 * gpiochip_generic_request() - request the gpio function for a pin
1724 * @gc: the gpiochip owning the GPIO
1725 * @offset: the offset of the GPIO to request for GPIO function
1727 int gpiochip_generic_request(struct gpio_chip *gc, unsigned offset)
1729 #ifdef CONFIG_PINCTRL
1730 if (list_empty(&gc->gpiodev->pin_ranges))
1734 return pinctrl_gpio_request(gc->gpiodev->base + offset);
1736 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1739 * gpiochip_generic_free() - free the gpio function from a pin
1740 * @gc: the gpiochip to request the gpio function for
1741 * @offset: the offset of the GPIO to free from GPIO function
1743 void gpiochip_generic_free(struct gpio_chip *gc, unsigned offset)
1745 pinctrl_gpio_free(gc->gpiodev->base + offset);
1747 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1750 * gpiochip_generic_config() - apply configuration for a pin
1751 * @gc: the gpiochip owning the GPIO
1752 * @offset: the offset of the GPIO to apply the configuration
1753 * @config: the configuration to be applied
1755 int gpiochip_generic_config(struct gpio_chip *gc, unsigned offset,
1756 unsigned long config)
1758 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1760 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1762 #ifdef CONFIG_PINCTRL
1765 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1766 * @gc: the gpiochip to add the range for
1767 * @pctldev: the pin controller to map to
1768 * @gpio_offset: the start offset in the current gpio_chip number space
1769 * @pin_group: name of the pin group inside the pin controller
1771 * Calling this function directly from a DeviceTree-supported
1772 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1773 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1774 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1776 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1777 struct pinctrl_dev *pctldev,
1778 unsigned int gpio_offset, const char *pin_group)
1780 struct gpio_pin_range *pin_range;
1781 struct gpio_device *gdev = gc->gpiodev;
1784 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1786 chip_err(gc, "failed to allocate pin ranges\n");
1790 /* Use local offset as range ID */
1791 pin_range->range.id = gpio_offset;
1792 pin_range->range.gc = gc;
1793 pin_range->range.name = gc->label;
1794 pin_range->range.base = gdev->base + gpio_offset;
1795 pin_range->pctldev = pctldev;
1797 ret = pinctrl_get_group_pins(pctldev, pin_group,
1798 &pin_range->range.pins,
1799 &pin_range->range.npins);
1805 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1807 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1808 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1809 pinctrl_dev_get_devname(pctldev), pin_group);
1811 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1815 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1818 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1819 * @gc: the gpiochip to add the range for
1820 * @pinctl_name: the dev_name() of the pin controller to map to
1821 * @gpio_offset: the start offset in the current gpio_chip number space
1822 * @pin_offset: the start offset in the pin controller number space
1823 * @npins: the number of pins from the offset of each pin space (GPIO and
1824 * pin controller) to accumulate in this range
1827 * 0 on success, or a negative error-code on failure.
1829 * Calling this function directly from a DeviceTree-supported
1830 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1831 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1832 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1834 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1835 unsigned int gpio_offset, unsigned int pin_offset,
1838 struct gpio_pin_range *pin_range;
1839 struct gpio_device *gdev = gc->gpiodev;
1842 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1844 chip_err(gc, "failed to allocate pin ranges\n");
1848 /* Use local offset as range ID */
1849 pin_range->range.id = gpio_offset;
1850 pin_range->range.gc = gc;
1851 pin_range->range.name = gc->label;
1852 pin_range->range.base = gdev->base + gpio_offset;
1853 pin_range->range.pin_base = pin_offset;
1854 pin_range->range.npins = npins;
1855 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1857 if (IS_ERR(pin_range->pctldev)) {
1858 ret = PTR_ERR(pin_range->pctldev);
1859 chip_err(gc, "could not create pin range\n");
1863 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1864 gpio_offset, gpio_offset + npins - 1,
1866 pin_offset, pin_offset + npins - 1);
1868 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1872 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1875 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1876 * @gc: the chip to remove all the mappings for
1878 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1880 struct gpio_pin_range *pin_range, *tmp;
1881 struct gpio_device *gdev = gc->gpiodev;
1883 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1884 list_del(&pin_range->node);
1885 pinctrl_remove_gpio_range(pin_range->pctldev,
1890 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1892 #endif /* CONFIG_PINCTRL */
1894 /* These "optional" allocation calls help prevent drivers from stomping
1895 * on each other, and help provide better diagnostics in debugfs.
1896 * They're called even less than the "set direction" calls.
1898 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
1900 struct gpio_chip *gc = desc->gdev->chip;
1902 unsigned long flags;
1906 label = kstrdup_const(label, GFP_KERNEL);
1911 spin_lock_irqsave(&gpio_lock, flags);
1913 /* NOTE: gpio_request() can be called in early boot,
1914 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1917 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1918 desc_set_label(desc, label ? : "?");
1927 /* gc->request may sleep */
1928 spin_unlock_irqrestore(&gpio_lock, flags);
1929 offset = gpio_chip_hwgpio(desc);
1930 if (gpiochip_line_is_valid(gc, offset))
1931 ret = gc->request(gc, offset);
1934 spin_lock_irqsave(&gpio_lock, flags);
1937 desc_set_label(desc, NULL);
1939 clear_bit(FLAG_REQUESTED, &desc->flags);
1943 if (gc->get_direction) {
1944 /* gc->get_direction may sleep */
1945 spin_unlock_irqrestore(&gpio_lock, flags);
1946 gpiod_get_direction(desc);
1947 spin_lock_irqsave(&gpio_lock, flags);
1950 spin_unlock_irqrestore(&gpio_lock, flags);
1955 * This descriptor validation needs to be inserted verbatim into each
1956 * function taking a descriptor, so we need to use a preprocessor
1957 * macro to avoid endless duplication. If the desc is NULL it is an
1958 * optional GPIO and calls should just bail out.
1960 static int validate_desc(const struct gpio_desc *desc, const char *func)
1965 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
1966 return PTR_ERR(desc);
1969 pr_warn("%s: invalid GPIO (no device)\n", func);
1972 if (!desc->gdev->chip) {
1973 dev_warn(&desc->gdev->dev,
1974 "%s: backing chip is gone\n", func);
1980 #define VALIDATE_DESC(desc) do { \
1981 int __valid = validate_desc(desc, __func__); \
1986 #define VALIDATE_DESC_VOID(desc) do { \
1987 int __valid = validate_desc(desc, __func__); \
1992 int gpiod_request(struct gpio_desc *desc, const char *label)
1994 int ret = -EPROBE_DEFER;
1995 struct gpio_device *gdev;
1997 VALIDATE_DESC(desc);
2000 if (try_module_get(gdev->owner)) {
2001 ret = gpiod_request_commit(desc, label);
2003 module_put(gdev->owner);
2005 get_device(&gdev->dev);
2009 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2014 static bool gpiod_free_commit(struct gpio_desc *desc)
2017 unsigned long flags;
2018 struct gpio_chip *gc;
2022 gpiod_unexport(desc);
2024 spin_lock_irqsave(&gpio_lock, flags);
2026 gc = desc->gdev->chip;
2027 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2029 spin_unlock_irqrestore(&gpio_lock, flags);
2030 might_sleep_if(gc->can_sleep);
2031 gc->free(gc, gpio_chip_hwgpio(desc));
2032 spin_lock_irqsave(&gpio_lock, flags);
2034 kfree_const(desc->label);
2035 desc_set_label(desc, NULL);
2036 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2037 clear_bit(FLAG_REQUESTED, &desc->flags);
2038 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2039 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2040 clear_bit(FLAG_PULL_UP, &desc->flags);
2041 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2042 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2043 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2044 #ifdef CONFIG_OF_DYNAMIC
2050 spin_unlock_irqrestore(&gpio_lock, flags);
2051 atomic_notifier_call_chain(&desc->gdev->notifier,
2052 GPIOLINE_CHANGED_RELEASED, desc);
2057 void gpiod_free(struct gpio_desc *desc)
2059 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2060 module_put(desc->gdev->owner);
2061 put_device(&desc->gdev->dev);
2063 WARN_ON(extra_checks);
2068 * gpiochip_is_requested - return string iff signal was requested
2069 * @gc: controller managing the signal
2070 * @offset: of signal within controller's 0..(ngpio - 1) range
2072 * Returns NULL if the GPIO is not currently requested, else a string.
2073 * The string returned is the label passed to gpio_request(); if none has been
2074 * passed it is a meaningless, non-NULL constant.
2076 * This function is for use by GPIO controller drivers. The label can
2077 * help with diagnostics, and knowing that the signal is used as a GPIO
2078 * can help avoid accidentally multiplexing it to another controller.
2080 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned offset)
2082 struct gpio_desc *desc;
2084 if (offset >= gc->ngpio)
2087 desc = gpiochip_get_desc(gc, offset);
2091 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2095 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2098 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2100 * @hwnum: hardware number of the GPIO for which to request the descriptor
2101 * @label: label for the GPIO
2102 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2103 * specify things like line inversion semantics with the machine flags
2104 * such as GPIO_OUT_LOW
2105 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2106 * can be used to specify consumer semantics such as open drain
2108 * Function allows GPIO chip drivers to request and use their own GPIO
2109 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2110 * function will not increase reference count of the GPIO chip module. This
2111 * allows the GPIO chip module to be unloaded as needed (we assume that the
2112 * GPIO chip driver handles freeing the GPIOs it has requested).
2115 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2118 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2121 enum gpio_lookup_flags lflags,
2122 enum gpiod_flags dflags)
2124 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2128 chip_err(gc, "failed to get GPIO descriptor\n");
2132 ret = gpiod_request_commit(desc, label);
2134 return ERR_PTR(ret);
2136 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2138 chip_err(gc, "setup of own GPIO %s failed\n", label);
2139 gpiod_free_commit(desc);
2140 return ERR_PTR(ret);
2145 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2148 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2149 * @desc: GPIO descriptor to free
2151 * Function frees the given GPIO requested previously with
2152 * gpiochip_request_own_desc().
2154 void gpiochip_free_own_desc(struct gpio_desc *desc)
2157 gpiod_free_commit(desc);
2159 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2162 * Drivers MUST set GPIO direction before making get/set calls. In
2163 * some cases this is done in early boot, before IRQs are enabled.
2165 * As a rule these aren't called more than once (except for drivers
2166 * using the open-drain emulation idiom) so these are natural places
2167 * to accumulate extra debugging checks. Note that we can't (yet)
2168 * rely on gpio_request() having been called beforehand.
2171 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2172 unsigned long config)
2174 if (!gc->set_config)
2177 return gc->set_config(gc, offset, config);
2180 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2182 struct gpio_chip *gc = desc->gdev->chip;
2183 unsigned long config;
2187 case PIN_CONFIG_BIAS_PULL_DOWN:
2188 case PIN_CONFIG_BIAS_PULL_UP:
2196 config = PIN_CONF_PACKED(mode, arg);
2197 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2200 static int gpio_set_bias(struct gpio_desc *desc)
2205 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2206 bias = PIN_CONFIG_BIAS_DISABLE;
2207 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2208 bias = PIN_CONFIG_BIAS_PULL_UP;
2209 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2210 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2213 ret = gpio_set_config(desc, bias);
2214 if (ret != -ENOTSUPP)
2221 * gpiod_direction_input - set the GPIO direction to input
2222 * @desc: GPIO to set to input
2224 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2225 * be called safely on it.
2227 * Return 0 in case of success, else an error code.
2229 int gpiod_direction_input(struct gpio_desc *desc)
2231 struct gpio_chip *gc;
2234 VALIDATE_DESC(desc);
2235 gc = desc->gdev->chip;
2238 * It is legal to have no .get() and .direction_input() specified if
2239 * the chip is output-only, but you can't specify .direction_input()
2240 * and not support the .get() operation, that doesn't make sense.
2242 if (!gc->get && gc->direction_input) {
2244 "%s: missing get() but have direction_input()\n",
2250 * If we have a .direction_input() callback, things are simple,
2251 * just call it. Else we are some input-only chip so try to check the
2252 * direction (if .get_direction() is supported) else we silently
2253 * assume we are in input mode after this.
2255 if (gc->direction_input) {
2256 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2257 } else if (gc->get_direction &&
2258 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2260 "%s: missing direction_input() operation and line is output\n",
2265 clear_bit(FLAG_IS_OUT, &desc->flags);
2266 ret = gpio_set_bias(desc);
2269 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2273 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2275 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2277 struct gpio_chip *gc = desc->gdev->chip;
2282 * It's OK not to specify .direction_output() if the gpiochip is
2283 * output-only, but if there is then not even a .set() operation it
2284 * is pretty tricky to drive the output line.
2286 if (!gc->set && !gc->direction_output) {
2288 "%s: missing set() and direction_output() operations\n",
2293 if (gc->direction_output) {
2294 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2296 /* Check that we are in output mode if we can */
2297 if (gc->get_direction &&
2298 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2300 "%s: missing direction_output() operation\n",
2305 * If we can't actively set the direction, we are some
2306 * output-only chip, so just drive the output as desired.
2308 gc->set(gc, gpio_chip_hwgpio(desc), val);
2312 set_bit(FLAG_IS_OUT, &desc->flags);
2313 trace_gpio_value(desc_to_gpio(desc), 0, val);
2314 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2319 * gpiod_direction_output_raw - set the GPIO direction to output
2320 * @desc: GPIO to set to output
2321 * @value: initial output value of the GPIO
2323 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2324 * be called safely on it. The initial value of the output must be specified
2325 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2327 * Return 0 in case of success, else an error code.
2329 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2331 VALIDATE_DESC(desc);
2332 return gpiod_direction_output_raw_commit(desc, value);
2334 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2337 * gpiod_direction_output - set the GPIO direction to output
2338 * @desc: GPIO to set to output
2339 * @value: initial output value of the GPIO
2341 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2342 * be called safely on it. The initial value of the output must be specified
2343 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2346 * Return 0 in case of success, else an error code.
2348 int gpiod_direction_output(struct gpio_desc *desc, int value)
2352 VALIDATE_DESC(desc);
2353 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2358 /* GPIOs used for enabled IRQs shall not be set as output */
2359 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2360 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2362 "%s: tried to set a GPIO tied to an IRQ as output\n",
2367 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2368 /* First see if we can enable open drain in hardware */
2369 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2371 goto set_output_value;
2372 /* Emulate open drain by not actively driving the line high */
2374 ret = gpiod_direction_input(desc);
2375 goto set_output_flag;
2378 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2379 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2381 goto set_output_value;
2382 /* Emulate open source by not actively driving the line low */
2384 ret = gpiod_direction_input(desc);
2385 goto set_output_flag;
2388 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2392 ret = gpio_set_bias(desc);
2395 return gpiod_direction_output_raw_commit(desc, value);
2399 * When emulating open-source or open-drain functionalities by not
2400 * actively driving the line (setting mode to input) we still need to
2401 * set the IS_OUT flag or otherwise we won't be able to set the line
2405 set_bit(FLAG_IS_OUT, &desc->flags);
2408 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2411 * gpiod_set_config - sets @config for a GPIO
2412 * @desc: descriptor of the GPIO for which to set the configuration
2413 * @config: Same packed config format as generic pinconf
2416 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2419 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2421 struct gpio_chip *gc;
2423 VALIDATE_DESC(desc);
2424 gc = desc->gdev->chip;
2426 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2428 EXPORT_SYMBOL_GPL(gpiod_set_config);
2431 * gpiod_set_debounce - sets @debounce time for a GPIO
2432 * @desc: descriptor of the GPIO for which to set debounce time
2433 * @debounce: debounce time in microseconds
2436 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2439 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2441 unsigned long config;
2443 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2444 return gpiod_set_config(desc, config);
2446 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2449 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2450 * @desc: descriptor of the GPIO for which to configure persistence
2451 * @transitory: True to lose state on suspend or reset, false for persistence
2454 * 0 on success, otherwise a negative error code.
2456 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2458 struct gpio_chip *gc;
2459 unsigned long packed;
2463 VALIDATE_DESC(desc);
2465 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2466 * persistence state.
2468 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2470 /* If the driver supports it, set the persistence state now */
2471 gc = desc->gdev->chip;
2472 if (!gc->set_config)
2475 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2477 gpio = gpio_chip_hwgpio(desc);
2478 rc = gpio_do_set_config(gc, gpio, packed);
2479 if (rc == -ENOTSUPP) {
2480 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2487 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2490 * gpiod_is_active_low - test whether a GPIO is active-low or not
2491 * @desc: the gpio descriptor to test
2493 * Returns 1 if the GPIO is active-low, 0 otherwise.
2495 int gpiod_is_active_low(const struct gpio_desc *desc)
2497 VALIDATE_DESC(desc);
2498 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2500 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2503 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2504 * @desc: the gpio descriptor to change
2506 void gpiod_toggle_active_low(struct gpio_desc *desc)
2508 VALIDATE_DESC_VOID(desc);
2509 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2511 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2513 /* I/O calls are only valid after configuration completed; the relevant
2514 * "is this a valid GPIO" error checks should already have been done.
2516 * "Get" operations are often inlinable as reading a pin value register,
2517 * and masking the relevant bit in that register.
2519 * When "set" operations are inlinable, they involve writing that mask to
2520 * one register to set a low value, or a different register to set it high.
2521 * Otherwise locking is needed, so there may be little value to inlining.
2523 *------------------------------------------------------------------------
2525 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2526 * have requested the GPIO. That can include implicit requesting by
2527 * a direction setting call. Marking a gpio as requested locks its chip
2528 * in memory, guaranteeing that these table lookups need no more locking
2529 * and that gpiochip_remove() will fail.
2531 * REVISIT when debugging, consider adding some instrumentation to ensure
2532 * that the GPIO was actually requested.
2535 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2537 struct gpio_chip *gc;
2541 gc = desc->gdev->chip;
2542 offset = gpio_chip_hwgpio(desc);
2543 value = gc->get ? gc->get(gc, offset) : -EIO;
2544 value = value < 0 ? value : !!value;
2545 trace_gpio_value(desc_to_gpio(desc), 1, value);
2549 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2550 unsigned long *mask, unsigned long *bits)
2552 if (gc->get_multiple) {
2553 return gc->get_multiple(gc, mask, bits);
2554 } else if (gc->get) {
2557 for_each_set_bit(i, mask, gc->ngpio) {
2558 value = gc->get(gc, i);
2561 __assign_bit(i, bits, value);
2568 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2569 unsigned int array_size,
2570 struct gpio_desc **desc_array,
2571 struct gpio_array *array_info,
2572 unsigned long *value_bitmap)
2577 * Validate array_info against desc_array and its size.
2578 * It should immediately follow desc_array if both
2579 * have been obtained from the same gpiod_get_array() call.
2581 if (array_info && array_info->desc == desc_array &&
2582 array_size <= array_info->size &&
2583 (void *)array_info == desc_array + array_info->size) {
2585 WARN_ON(array_info->chip->can_sleep);
2587 ret = gpio_chip_get_multiple(array_info->chip,
2588 array_info->get_mask,
2593 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2594 bitmap_xor(value_bitmap, value_bitmap,
2595 array_info->invert_mask, array_size);
2597 i = find_first_zero_bit(array_info->get_mask, array_size);
2598 if (i == array_size)
2604 while (i < array_size) {
2605 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2606 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2607 unsigned long *mask, *bits;
2610 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2613 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2615 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2620 bits = mask + BITS_TO_LONGS(gc->ngpio);
2621 bitmap_zero(mask, gc->ngpio);
2624 WARN_ON(gc->can_sleep);
2626 /* collect all inputs belonging to the same chip */
2629 const struct gpio_desc *desc = desc_array[i];
2630 int hwgpio = gpio_chip_hwgpio(desc);
2632 __set_bit(hwgpio, mask);
2636 i = find_next_zero_bit(array_info->get_mask,
2638 } while ((i < array_size) &&
2639 (desc_array[i]->gdev->chip == gc));
2641 ret = gpio_chip_get_multiple(gc, mask, bits);
2643 if (mask != fastpath)
2648 for (j = first; j < i; ) {
2649 const struct gpio_desc *desc = desc_array[j];
2650 int hwgpio = gpio_chip_hwgpio(desc);
2651 int value = test_bit(hwgpio, bits);
2653 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2655 __assign_bit(j, value_bitmap, value);
2656 trace_gpio_value(desc_to_gpio(desc), 1, value);
2660 j = find_next_zero_bit(array_info->get_mask, i,
2664 if (mask != fastpath)
2671 * gpiod_get_raw_value() - return a gpio's raw value
2672 * @desc: gpio whose value will be returned
2674 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2675 * its ACTIVE_LOW status, or negative errno on failure.
2677 * This function can be called from contexts where we cannot sleep, and will
2678 * complain if the GPIO chip functions potentially sleep.
2680 int gpiod_get_raw_value(const struct gpio_desc *desc)
2682 VALIDATE_DESC(desc);
2683 /* Should be using gpiod_get_raw_value_cansleep() */
2684 WARN_ON(desc->gdev->chip->can_sleep);
2685 return gpiod_get_raw_value_commit(desc);
2687 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2690 * gpiod_get_value() - return a gpio's value
2691 * @desc: gpio whose value will be returned
2693 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2694 * account, or negative errno on failure.
2696 * This function can be called from contexts where we cannot sleep, and will
2697 * complain if the GPIO chip functions potentially sleep.
2699 int gpiod_get_value(const struct gpio_desc *desc)
2703 VALIDATE_DESC(desc);
2704 /* Should be using gpiod_get_value_cansleep() */
2705 WARN_ON(desc->gdev->chip->can_sleep);
2707 value = gpiod_get_raw_value_commit(desc);
2711 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2716 EXPORT_SYMBOL_GPL(gpiod_get_value);
2719 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2720 * @array_size: number of elements in the descriptor array / value bitmap
2721 * @desc_array: array of GPIO descriptors whose values will be read
2722 * @array_info: information on applicability of fast bitmap processing path
2723 * @value_bitmap: bitmap to store the read values
2725 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2726 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2727 * else an error code.
2729 * This function can be called from contexts where we cannot sleep,
2730 * and it will complain if the GPIO chip functions potentially sleep.
2732 int gpiod_get_raw_array_value(unsigned int array_size,
2733 struct gpio_desc **desc_array,
2734 struct gpio_array *array_info,
2735 unsigned long *value_bitmap)
2739 return gpiod_get_array_value_complex(true, false, array_size,
2740 desc_array, array_info,
2743 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2746 * gpiod_get_array_value() - read values from an array of GPIOs
2747 * @array_size: number of elements in the descriptor array / value bitmap
2748 * @desc_array: array of GPIO descriptors whose values will be read
2749 * @array_info: information on applicability of fast bitmap processing path
2750 * @value_bitmap: bitmap to store the read values
2752 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2753 * into account. Return 0 in case of success, else an error code.
2755 * This function can be called from contexts where we cannot sleep,
2756 * and it will complain if the GPIO chip functions potentially sleep.
2758 int gpiod_get_array_value(unsigned int array_size,
2759 struct gpio_desc **desc_array,
2760 struct gpio_array *array_info,
2761 unsigned long *value_bitmap)
2765 return gpiod_get_array_value_complex(false, false, array_size,
2766 desc_array, array_info,
2769 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2772 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2773 * @desc: gpio descriptor whose state need to be set.
2774 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2776 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2779 struct gpio_chip *gc = desc->gdev->chip;
2780 int offset = gpio_chip_hwgpio(desc);
2783 ret = gc->direction_input(gc, offset);
2785 ret = gc->direction_output(gc, offset, 0);
2787 set_bit(FLAG_IS_OUT, &desc->flags);
2789 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2792 "%s: Error in set_value for open drain err %d\n",
2797 * _gpio_set_open_source_value() - Set the open source gpio's value.
2798 * @desc: gpio descriptor whose state need to be set.
2799 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2801 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2804 struct gpio_chip *gc = desc->gdev->chip;
2805 int offset = gpio_chip_hwgpio(desc);
2808 ret = gc->direction_output(gc, offset, 1);
2810 set_bit(FLAG_IS_OUT, &desc->flags);
2812 ret = gc->direction_input(gc, offset);
2814 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2817 "%s: Error in set_value for open source err %d\n",
2821 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2823 struct gpio_chip *gc;
2825 gc = desc->gdev->chip;
2826 trace_gpio_value(desc_to_gpio(desc), 0, value);
2827 gc->set(gc, gpio_chip_hwgpio(desc), value);
2831 * set multiple outputs on the same chip;
2832 * use the chip's set_multiple function if available;
2833 * otherwise set the outputs sequentially;
2834 * @chip: the GPIO chip we operate on
2835 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2836 * defines which outputs are to be changed
2837 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2838 * defines the values the outputs specified by mask are to be set to
2840 static void gpio_chip_set_multiple(struct gpio_chip *gc,
2841 unsigned long *mask, unsigned long *bits)
2843 if (gc->set_multiple) {
2844 gc->set_multiple(gc, mask, bits);
2848 /* set outputs if the corresponding mask bit is set */
2849 for_each_set_bit(i, mask, gc->ngpio)
2850 gc->set(gc, i, test_bit(i, bits));
2854 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
2855 unsigned int array_size,
2856 struct gpio_desc **desc_array,
2857 struct gpio_array *array_info,
2858 unsigned long *value_bitmap)
2863 * Validate array_info against desc_array and its size.
2864 * It should immediately follow desc_array if both
2865 * have been obtained from the same gpiod_get_array() call.
2867 if (array_info && array_info->desc == desc_array &&
2868 array_size <= array_info->size &&
2869 (void *)array_info == desc_array + array_info->size) {
2871 WARN_ON(array_info->chip->can_sleep);
2873 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2874 bitmap_xor(value_bitmap, value_bitmap,
2875 array_info->invert_mask, array_size);
2877 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2880 i = find_first_zero_bit(array_info->set_mask, array_size);
2881 if (i == array_size)
2887 while (i < array_size) {
2888 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2889 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2890 unsigned long *mask, *bits;
2893 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2896 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
2898 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2903 bits = mask + BITS_TO_LONGS(gc->ngpio);
2904 bitmap_zero(mask, gc->ngpio);
2907 WARN_ON(gc->can_sleep);
2910 struct gpio_desc *desc = desc_array[i];
2911 int hwgpio = gpio_chip_hwgpio(desc);
2912 int value = test_bit(i, value_bitmap);
2915 * Pins applicable for fast input but not for
2916 * fast output processing may have been already
2917 * inverted inside the fast path, skip them.
2919 if (!raw && !(array_info &&
2920 test_bit(i, array_info->invert_mask)) &&
2921 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2923 trace_gpio_value(desc_to_gpio(desc), 0, value);
2925 * collect all normal outputs belonging to the same chip
2926 * open drain and open source outputs are set individually
2928 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2929 gpio_set_open_drain_value_commit(desc, value);
2930 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2931 gpio_set_open_source_value_commit(desc, value);
2933 __set_bit(hwgpio, mask);
2934 __assign_bit(hwgpio, bits, value);
2940 i = find_next_zero_bit(array_info->set_mask,
2942 } while ((i < array_size) &&
2943 (desc_array[i]->gdev->chip == gc));
2944 /* push collected bits to outputs */
2946 gpio_chip_set_multiple(gc, mask, bits);
2948 if (mask != fastpath)
2955 * gpiod_set_raw_value() - assign a gpio's raw value
2956 * @desc: gpio whose value will be assigned
2957 * @value: value to assign
2959 * Set the raw value of the GPIO, i.e. the value of its physical line without
2960 * regard for its ACTIVE_LOW status.
2962 * This function can be called from contexts where we cannot sleep, and will
2963 * complain if the GPIO chip functions potentially sleep.
2965 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2967 VALIDATE_DESC_VOID(desc);
2968 /* Should be using gpiod_set_raw_value_cansleep() */
2969 WARN_ON(desc->gdev->chip->can_sleep);
2970 gpiod_set_raw_value_commit(desc, value);
2972 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2975 * gpiod_set_value_nocheck() - set a GPIO line value without checking
2976 * @desc: the descriptor to set the value on
2977 * @value: value to set
2979 * This sets the value of a GPIO line backing a descriptor, applying
2980 * different semantic quirks like active low and open drain/source
2983 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
2985 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2987 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2988 gpio_set_open_drain_value_commit(desc, value);
2989 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2990 gpio_set_open_source_value_commit(desc, value);
2992 gpiod_set_raw_value_commit(desc, value);
2996 * gpiod_set_value() - assign a gpio's value
2997 * @desc: gpio whose value will be assigned
2998 * @value: value to assign
3000 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3001 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3003 * This function can be called from contexts where we cannot sleep, and will
3004 * complain if the GPIO chip functions potentially sleep.
3006 void gpiod_set_value(struct gpio_desc *desc, int value)
3008 VALIDATE_DESC_VOID(desc);
3009 /* Should be using gpiod_set_value_cansleep() */
3010 WARN_ON(desc->gdev->chip->can_sleep);
3011 gpiod_set_value_nocheck(desc, value);
3013 EXPORT_SYMBOL_GPL(gpiod_set_value);
3016 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3017 * @array_size: number of elements in the descriptor array / value bitmap
3018 * @desc_array: array of GPIO descriptors whose values will be assigned
3019 * @array_info: information on applicability of fast bitmap processing path
3020 * @value_bitmap: bitmap of values to assign
3022 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3023 * without regard for their ACTIVE_LOW status.
3025 * This function can be called from contexts where we cannot sleep, and will
3026 * complain if the GPIO chip functions potentially sleep.
3028 int gpiod_set_raw_array_value(unsigned int array_size,
3029 struct gpio_desc **desc_array,
3030 struct gpio_array *array_info,
3031 unsigned long *value_bitmap)
3035 return gpiod_set_array_value_complex(true, false, array_size,
3036 desc_array, array_info, value_bitmap);
3038 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3041 * gpiod_set_array_value() - assign values to an array of GPIOs
3042 * @array_size: number of elements in the descriptor array / value bitmap
3043 * @desc_array: array of GPIO descriptors whose values will be assigned
3044 * @array_info: information on applicability of fast bitmap processing path
3045 * @value_bitmap: bitmap of values to assign
3047 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3050 * This function can be called from contexts where we cannot sleep, and will
3051 * complain if the GPIO chip functions potentially sleep.
3053 int gpiod_set_array_value(unsigned int array_size,
3054 struct gpio_desc **desc_array,
3055 struct gpio_array *array_info,
3056 unsigned long *value_bitmap)
3060 return gpiod_set_array_value_complex(false, false, array_size,
3061 desc_array, array_info,
3064 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3067 * gpiod_cansleep() - report whether gpio value access may sleep
3068 * @desc: gpio to check
3071 int gpiod_cansleep(const struct gpio_desc *desc)
3073 VALIDATE_DESC(desc);
3074 return desc->gdev->chip->can_sleep;
3076 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3079 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3080 * @desc: gpio to set the consumer name on
3081 * @name: the new consumer name
3083 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3085 VALIDATE_DESC(desc);
3087 name = kstrdup_const(name, GFP_KERNEL);
3092 kfree_const(desc->label);
3093 desc_set_label(desc, name);
3097 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3100 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3101 * @desc: gpio whose IRQ will be returned (already requested)
3103 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3106 int gpiod_to_irq(const struct gpio_desc *desc)
3108 struct gpio_chip *gc;
3112 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3113 * requires this function to not return zero on an invalid descriptor
3114 * but rather a negative error number.
3116 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3119 gc = desc->gdev->chip;
3120 offset = gpio_chip_hwgpio(desc);
3122 int retirq = gc->to_irq(gc, offset);
3124 /* Zero means NO_IRQ */
3132 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3135 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3136 * @gc: the chip the GPIO to lock belongs to
3137 * @offset: the offset of the GPIO to lock as IRQ
3139 * This is used directly by GPIO drivers that want to lock down
3140 * a certain GPIO line to be used for IRQs.
3142 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3144 struct gpio_desc *desc;
3146 desc = gpiochip_get_desc(gc, offset);
3148 return PTR_ERR(desc);
3151 * If it's fast: flush the direction setting if something changed
3154 if (!gc->can_sleep && gc->get_direction) {
3155 int dir = gpiod_get_direction(desc);
3158 chip_err(gc, "%s: cannot get GPIO direction\n",
3164 /* To be valid for IRQ the line needs to be input or open drain */
3165 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3166 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3168 "%s: tried to flag a GPIO set as output for IRQ\n",
3173 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3174 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3177 * If the consumer has not set up a label (such as when the
3178 * IRQ is referenced from .to_irq()) we set up a label here
3179 * so it is clear this is used as an interrupt.
3182 desc_set_label(desc, "interrupt");
3186 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3189 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3190 * @gc: the chip the GPIO to lock belongs to
3191 * @offset: the offset of the GPIO to lock as IRQ
3193 * This is used directly by GPIO drivers that want to indicate
3194 * that a certain GPIO is no longer used exclusively for IRQ.
3196 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3198 struct gpio_desc *desc;
3200 desc = gpiochip_get_desc(gc, offset);
3204 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3205 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3207 /* If we only had this marking, erase it */
3208 if (desc->label && !strcmp(desc->label, "interrupt"))
3209 desc_set_label(desc, NULL);
3211 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3213 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3215 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3217 if (!IS_ERR(desc) &&
3218 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3219 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3221 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3223 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3225 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3227 if (!IS_ERR(desc) &&
3228 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3230 * We must not be output when using IRQ UNLESS we are
3233 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3234 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3235 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3238 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3240 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3242 if (offset >= gc->ngpio)
3245 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3247 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3249 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3253 if (!try_module_get(gc->gpiodev->owner))
3256 ret = gpiochip_lock_as_irq(gc, offset);
3258 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3259 module_put(gc->gpiodev->owner);
3264 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3266 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3268 gpiochip_unlock_as_irq(gc, offset);
3269 module_put(gc->gpiodev->owner);
3271 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3273 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3275 if (offset >= gc->ngpio)
3278 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3280 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3282 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3284 if (offset >= gc->ngpio)
3287 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3289 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3291 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3293 if (offset >= gc->ngpio)
3296 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3298 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3301 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3302 * @desc: gpio whose value will be returned
3304 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3305 * its ACTIVE_LOW status, or negative errno on failure.
3307 * This function is to be called from contexts that can sleep.
3309 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3311 might_sleep_if(extra_checks);
3312 VALIDATE_DESC(desc);
3313 return gpiod_get_raw_value_commit(desc);
3315 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3318 * gpiod_get_value_cansleep() - return a gpio's value
3319 * @desc: gpio whose value will be returned
3321 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3322 * account, or negative errno on failure.
3324 * This function is to be called from contexts that can sleep.
3326 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3330 might_sleep_if(extra_checks);
3331 VALIDATE_DESC(desc);
3332 value = gpiod_get_raw_value_commit(desc);
3336 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3341 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3344 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3345 * @array_size: number of elements in the descriptor array / value bitmap
3346 * @desc_array: array of GPIO descriptors whose values will be read
3347 * @array_info: information on applicability of fast bitmap processing path
3348 * @value_bitmap: bitmap to store the read values
3350 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3351 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3352 * else an error code.
3354 * This function is to be called from contexts that can sleep.
3356 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3357 struct gpio_desc **desc_array,
3358 struct gpio_array *array_info,
3359 unsigned long *value_bitmap)
3361 might_sleep_if(extra_checks);
3364 return gpiod_get_array_value_complex(true, true, array_size,
3365 desc_array, array_info,
3368 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3371 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3372 * @array_size: number of elements in the descriptor array / value bitmap
3373 * @desc_array: array of GPIO descriptors whose values will be read
3374 * @array_info: information on applicability of fast bitmap processing path
3375 * @value_bitmap: bitmap to store the read values
3377 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3378 * into account. Return 0 in case of success, else an error code.
3380 * This function is to be called from contexts that can sleep.
3382 int gpiod_get_array_value_cansleep(unsigned int array_size,
3383 struct gpio_desc **desc_array,
3384 struct gpio_array *array_info,
3385 unsigned long *value_bitmap)
3387 might_sleep_if(extra_checks);
3390 return gpiod_get_array_value_complex(false, true, array_size,
3391 desc_array, array_info,
3394 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3397 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3398 * @desc: gpio whose value will be assigned
3399 * @value: value to assign
3401 * Set the raw value of the GPIO, i.e. the value of its physical line without
3402 * regard for its ACTIVE_LOW status.
3404 * This function is to be called from contexts that can sleep.
3406 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3408 might_sleep_if(extra_checks);
3409 VALIDATE_DESC_VOID(desc);
3410 gpiod_set_raw_value_commit(desc, value);
3412 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3415 * gpiod_set_value_cansleep() - assign a gpio's value
3416 * @desc: gpio whose value will be assigned
3417 * @value: value to assign
3419 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3422 * This function is to be called from contexts that can sleep.
3424 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3426 might_sleep_if(extra_checks);
3427 VALIDATE_DESC_VOID(desc);
3428 gpiod_set_value_nocheck(desc, value);
3430 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3433 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3434 * @array_size: number of elements in the descriptor array / value bitmap
3435 * @desc_array: array of GPIO descriptors whose values will be assigned
3436 * @array_info: information on applicability of fast bitmap processing path
3437 * @value_bitmap: bitmap of values to assign
3439 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3440 * without regard for their ACTIVE_LOW status.
3442 * This function is to be called from contexts that can sleep.
3444 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3445 struct gpio_desc **desc_array,
3446 struct gpio_array *array_info,
3447 unsigned long *value_bitmap)
3449 might_sleep_if(extra_checks);
3452 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3453 array_info, value_bitmap);
3455 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3458 * gpiod_add_lookup_tables() - register GPIO device consumers
3459 * @tables: list of tables of consumers to register
3460 * @n: number of tables in the list
3462 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3466 mutex_lock(&gpio_lookup_lock);
3468 for (i = 0; i < n; i++)
3469 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3471 mutex_unlock(&gpio_lookup_lock);
3475 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3476 * @array_size: number of elements in the descriptor array / value bitmap
3477 * @desc_array: array of GPIO descriptors whose values will be assigned
3478 * @array_info: information on applicability of fast bitmap processing path
3479 * @value_bitmap: bitmap of values to assign
3481 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3484 * This function is to be called from contexts that can sleep.
3486 int gpiod_set_array_value_cansleep(unsigned int array_size,
3487 struct gpio_desc **desc_array,
3488 struct gpio_array *array_info,
3489 unsigned long *value_bitmap)
3491 might_sleep_if(extra_checks);
3494 return gpiod_set_array_value_complex(false, true, array_size,
3495 desc_array, array_info,
3498 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3501 * gpiod_add_lookup_table() - register GPIO device consumers
3502 * @table: table of consumers to register
3504 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3506 mutex_lock(&gpio_lookup_lock);
3508 list_add_tail(&table->list, &gpio_lookup_list);
3510 mutex_unlock(&gpio_lookup_lock);
3512 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3515 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3516 * @table: table of consumers to unregister
3518 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3520 mutex_lock(&gpio_lookup_lock);
3522 list_del(&table->list);
3524 mutex_unlock(&gpio_lookup_lock);
3526 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3529 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3530 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3532 void gpiod_add_hogs(struct gpiod_hog *hogs)
3534 struct gpio_chip *gc;
3535 struct gpiod_hog *hog;
3537 mutex_lock(&gpio_machine_hogs_mutex);
3539 for (hog = &hogs[0]; hog->chip_label; hog++) {
3540 list_add_tail(&hog->list, &gpio_machine_hogs);
3543 * The chip may have been registered earlier, so check if it
3544 * exists and, if so, try to hog the line now.
3546 gc = find_chip_by_name(hog->chip_label);
3548 gpiochip_machine_hog(gc, hog);
3551 mutex_unlock(&gpio_machine_hogs_mutex);
3553 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3555 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3557 const char *dev_id = dev ? dev_name(dev) : NULL;
3558 struct gpiod_lookup_table *table;
3560 mutex_lock(&gpio_lookup_lock);
3562 list_for_each_entry(table, &gpio_lookup_list, list) {
3563 if (table->dev_id && dev_id) {
3565 * Valid strings on both ends, must be identical to have
3568 if (!strcmp(table->dev_id, dev_id))
3572 * One of the pointers is NULL, so both must be to have
3575 if (dev_id == table->dev_id)
3582 mutex_unlock(&gpio_lookup_lock);
3586 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3587 unsigned int idx, unsigned long *flags)
3589 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3590 struct gpiod_lookup_table *table;
3591 struct gpiod_lookup *p;
3593 table = gpiod_find_lookup_table(dev);
3597 for (p = &table->table[0]; p->key; p++) {
3598 struct gpio_chip *gc;
3600 /* idx must always match exactly */
3604 /* If the lookup entry has a con_id, require exact match */
3605 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3608 if (p->chip_hwnum == U16_MAX) {
3609 desc = gpio_name_to_desc(p->key);
3615 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3617 return ERR_PTR(-EPROBE_DEFER);
3620 gc = find_chip_by_name(p->key);
3624 * As the lookup table indicates a chip with
3625 * p->key should exist, assume it may
3626 * still appear later and let the interested
3627 * consumer be probed again or let the Deferred
3628 * Probe infrastructure handle the error.
3630 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3632 return ERR_PTR(-EPROBE_DEFER);
3635 if (gc->ngpio <= p->chip_hwnum) {
3637 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3638 idx, p->chip_hwnum, gc->ngpio - 1,
3640 return ERR_PTR(-EINVAL);
3643 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3652 static int platform_gpio_count(struct device *dev, const char *con_id)
3654 struct gpiod_lookup_table *table;
3655 struct gpiod_lookup *p;
3656 unsigned int count = 0;
3658 table = gpiod_find_lookup_table(dev);
3662 for (p = &table->table[0]; p->key; p++) {
3663 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3664 (!con_id && !p->con_id))
3674 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3675 * @fwnode: handle of the firmware node
3676 * @con_id: function within the GPIO consumer
3677 * @index: index of the GPIO to obtain for the consumer
3678 * @flags: GPIO initialization flags
3679 * @label: label to attach to the requested GPIO
3681 * This function can be used for drivers that get their configuration
3682 * from opaque firmware.
3684 * The function properly finds the corresponding GPIO using whatever is the
3685 * underlying firmware interface and then makes sure that the GPIO
3686 * descriptor is requested before it is returned to the caller.
3689 * On successful request the GPIO pin is configured in accordance with
3692 * In case of error an ERR_PTR() is returned.
3694 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3695 const char *con_id, int index,
3696 enum gpiod_flags flags,
3699 struct gpio_desc *desc;
3700 char prop_name[32]; /* 32 is max size of property name */
3703 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3705 snprintf(prop_name, sizeof(prop_name), "%s-%s",
3706 con_id, gpio_suffixes[i]);
3708 snprintf(prop_name, sizeof(prop_name), "%s",
3711 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3713 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
3719 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
3722 * gpiod_count - return the number of GPIOs associated with a device / function
3723 * or -ENOENT if no GPIO has been assigned to the requested function
3724 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3725 * @con_id: function within the GPIO consumer
3727 int gpiod_count(struct device *dev, const char *con_id)
3729 int count = -ENOENT;
3731 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3732 count = of_gpio_get_count(dev, con_id);
3733 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3734 count = acpi_gpio_count(dev, con_id);
3737 count = platform_gpio_count(dev, con_id);
3741 EXPORT_SYMBOL_GPL(gpiod_count);
3744 * gpiod_get - obtain a GPIO for a given GPIO function
3745 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3746 * @con_id: function within the GPIO consumer
3747 * @flags: optional GPIO initialization flags
3749 * Return the GPIO descriptor corresponding to the function con_id of device
3750 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3751 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3753 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3754 enum gpiod_flags flags)
3756 return gpiod_get_index(dev, con_id, 0, flags);
3758 EXPORT_SYMBOL_GPL(gpiod_get);
3761 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3762 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3763 * @con_id: function within the GPIO consumer
3764 * @flags: optional GPIO initialization flags
3766 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3767 * the requested function it will return NULL. This is convenient for drivers
3768 * that need to handle optional GPIOs.
3770 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3772 enum gpiod_flags flags)
3774 return gpiod_get_index_optional(dev, con_id, 0, flags);
3776 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3780 * gpiod_configure_flags - helper function to configure a given GPIO
3781 * @desc: gpio whose value will be assigned
3782 * @con_id: function within the GPIO consumer
3783 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3784 * of_find_gpio() or of_get_gpio_hog()
3785 * @dflags: gpiod_flags - optional GPIO initialization flags
3787 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3788 * requested function and/or index, or another IS_ERR() code if an error
3789 * occurred while trying to acquire the GPIO.
3791 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3792 unsigned long lflags, enum gpiod_flags dflags)
3796 if (lflags & GPIO_ACTIVE_LOW)
3797 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3799 if (lflags & GPIO_OPEN_DRAIN)
3800 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3801 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3803 * This enforces open drain mode from the consumer side.
3804 * This is necessary for some busses like I2C, but the lookup
3805 * should *REALLY* have specified them as open drain in the
3806 * first place, so print a little warning here.
3808 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3810 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3813 if (lflags & GPIO_OPEN_SOURCE)
3814 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3816 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3818 "both pull-up and pull-down enabled, invalid configuration\n");
3822 if (lflags & GPIO_PULL_UP)
3823 set_bit(FLAG_PULL_UP, &desc->flags);
3824 else if (lflags & GPIO_PULL_DOWN)
3825 set_bit(FLAG_PULL_DOWN, &desc->flags);
3827 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3831 /* No particular flag request, return here... */
3832 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3833 gpiod_dbg(desc, "no flags found for %s\n", con_id);
3838 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3839 ret = gpiod_direction_output(desc,
3840 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3842 ret = gpiod_direction_input(desc);
3848 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3849 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3850 * @con_id: function within the GPIO consumer
3851 * @idx: index of the GPIO to obtain in the consumer
3852 * @flags: optional GPIO initialization flags
3854 * This variant of gpiod_get() allows to access GPIOs other than the first
3855 * defined one for functions that define several GPIOs.
3857 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3858 * requested function and/or index, or another IS_ERR() code if an error
3859 * occurred while trying to acquire the GPIO.
3861 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3864 enum gpiod_flags flags)
3866 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3867 struct gpio_desc *desc = NULL;
3869 /* Maybe we have a device name, maybe not */
3870 const char *devname = dev ? dev_name(dev) : "?";
3872 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3875 /* Using device tree? */
3876 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3877 dev_dbg(dev, "using device tree for GPIO lookup\n");
3878 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3879 } else if (ACPI_COMPANION(dev)) {
3880 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3881 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3886 * Either we are not using DT or ACPI, or their lookup did not return
3887 * a result. In that case, use platform lookup as a fallback.
3889 if (!desc || desc == ERR_PTR(-ENOENT)) {
3890 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3891 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3895 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3900 * If a connection label was passed use that, else attempt to use
3901 * the device name as label
3903 ret = gpiod_request(desc, con_id ? con_id : devname);
3905 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
3907 * This happens when there are several consumers for
3908 * the same GPIO line: we just return here without
3909 * further initialization. It is a bit if a hack.
3910 * This is necessary to support fixed regulators.
3912 * FIXME: Make this more sane and safe.
3914 dev_info(dev, "nonexclusive access to GPIO for %s\n",
3915 con_id ? con_id : devname);
3918 return ERR_PTR(ret);
3922 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3924 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3926 return ERR_PTR(ret);
3929 atomic_notifier_call_chain(&desc->gdev->notifier,
3930 GPIOLINE_CHANGED_REQUESTED, desc);
3934 EXPORT_SYMBOL_GPL(gpiod_get_index);
3937 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3938 * @fwnode: handle of the firmware node
3939 * @propname: name of the firmware property representing the GPIO
3940 * @index: index of the GPIO to obtain for the consumer
3941 * @dflags: GPIO initialization flags
3942 * @label: label to attach to the requested GPIO
3944 * This function can be used for drivers that get their configuration
3945 * from opaque firmware.
3947 * The function properly finds the corresponding GPIO using whatever is the
3948 * underlying firmware interface and then makes sure that the GPIO
3949 * descriptor is requested before it is returned to the caller.
3952 * On successful request the GPIO pin is configured in accordance with
3955 * In case of error an ERR_PTR() is returned.
3957 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3958 const char *propname, int index,
3959 enum gpiod_flags dflags,
3962 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3963 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3967 return ERR_PTR(-EINVAL);
3969 if (is_of_node(fwnode)) {
3970 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3975 } else if (is_acpi_node(fwnode)) {
3976 struct acpi_gpio_info info;
3978 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3982 acpi_gpio_update_gpiod_flags(&dflags, &info);
3983 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
3986 /* Currently only ACPI takes this path */
3987 ret = gpiod_request(desc, label);
3989 return ERR_PTR(ret);
3991 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3994 return ERR_PTR(ret);
3997 atomic_notifier_call_chain(&desc->gdev->notifier,
3998 GPIOLINE_CHANGED_REQUESTED, desc);
4002 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4005 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4007 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4008 * @con_id: function within the GPIO consumer
4009 * @index: index of the GPIO to obtain in the consumer
4010 * @flags: optional GPIO initialization flags
4012 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4013 * specified index was assigned to the requested function it will return NULL.
4014 * This is convenient for drivers that need to handle optional GPIOs.
4016 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4019 enum gpiod_flags flags)
4021 struct gpio_desc *desc;
4023 desc = gpiod_get_index(dev, con_id, index, flags);
4025 if (PTR_ERR(desc) == -ENOENT)
4031 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4034 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4035 * @desc: gpio whose value will be assigned
4036 * @name: gpio line name
4037 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4038 * of_find_gpio() or of_get_gpio_hog()
4039 * @dflags: gpiod_flags - optional GPIO initialization flags
4041 int gpiod_hog(struct gpio_desc *desc, const char *name,
4042 unsigned long lflags, enum gpiod_flags dflags)
4044 struct gpio_chip *gc;
4045 struct gpio_desc *local_desc;
4049 gc = gpiod_to_chip(desc);
4050 hwnum = gpio_chip_hwgpio(desc);
4052 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4054 if (IS_ERR(local_desc)) {
4055 ret = PTR_ERR(local_desc);
4056 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4057 name, gc->label, hwnum, ret);
4061 /* Mark GPIO as hogged so it can be identified and removed later */
4062 set_bit(FLAG_IS_HOGGED, &desc->flags);
4064 gpiod_info(desc, "hogged as %s%s\n",
4065 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4066 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4067 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4073 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4074 * @gc: gpio chip to act on
4076 static void gpiochip_free_hogs(struct gpio_chip *gc)
4080 for (id = 0; id < gc->ngpio; id++) {
4081 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4082 gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
4087 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4088 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4089 * @con_id: function within the GPIO consumer
4090 * @flags: optional GPIO initialization flags
4092 * This function acquires all the GPIOs defined under a given function.
4094 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4095 * no GPIO has been assigned to the requested function, or another IS_ERR()
4096 * code if an error occurred while trying to acquire the GPIOs.
4098 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4100 enum gpiod_flags flags)
4102 struct gpio_desc *desc;
4103 struct gpio_descs *descs;
4104 struct gpio_array *array_info = NULL;
4105 struct gpio_chip *gc;
4106 int count, bitmap_size;
4108 count = gpiod_count(dev, con_id);
4110 return ERR_PTR(count);
4112 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4114 return ERR_PTR(-ENOMEM);
4116 for (descs->ndescs = 0; descs->ndescs < count; ) {
4117 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4119 gpiod_put_array(descs);
4120 return ERR_CAST(desc);
4123 descs->desc[descs->ndescs] = desc;
4125 gc = gpiod_to_chip(desc);
4127 * If pin hardware number of array member 0 is also 0, select
4128 * its chip as a candidate for fast bitmap processing path.
4130 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4131 struct gpio_descs *array;
4133 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4136 array = kzalloc(struct_size(descs, desc, count) +
4137 struct_size(array_info, invert_mask,
4138 3 * bitmap_size), GFP_KERNEL);
4140 gpiod_put_array(descs);
4141 return ERR_PTR(-ENOMEM);
4144 memcpy(array, descs,
4145 struct_size(descs, desc, descs->ndescs + 1));
4149 array_info = (void *)(descs->desc + count);
4150 array_info->get_mask = array_info->invert_mask +
4152 array_info->set_mask = array_info->get_mask +
4155 array_info->desc = descs->desc;
4156 array_info->size = count;
4157 array_info->chip = gc;
4158 bitmap_set(array_info->get_mask, descs->ndescs,
4159 count - descs->ndescs);
4160 bitmap_set(array_info->set_mask, descs->ndescs,
4161 count - descs->ndescs);
4162 descs->info = array_info;
4164 /* Unmark array members which don't belong to the 'fast' chip */
4165 if (array_info && array_info->chip != gc) {
4166 __clear_bit(descs->ndescs, array_info->get_mask);
4167 __clear_bit(descs->ndescs, array_info->set_mask);
4170 * Detect array members which belong to the 'fast' chip
4171 * but their pins are not in hardware order.
4173 else if (array_info &&
4174 gpio_chip_hwgpio(desc) != descs->ndescs) {
4176 * Don't use fast path if all array members processed so
4177 * far belong to the same chip as this one but its pin
4178 * hardware number is different from its array index.
4180 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4183 __clear_bit(descs->ndescs,
4184 array_info->get_mask);
4185 __clear_bit(descs->ndescs,
4186 array_info->set_mask);
4188 } else if (array_info) {
4189 /* Exclude open drain or open source from fast output */
4190 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4191 gpiochip_line_is_open_source(gc, descs->ndescs))
4192 __clear_bit(descs->ndescs,
4193 array_info->set_mask);
4194 /* Identify 'fast' pins which require invertion */
4195 if (gpiod_is_active_low(desc))
4196 __set_bit(descs->ndescs,
4197 array_info->invert_mask);
4204 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4205 array_info->chip->label, array_info->size,
4206 *array_info->get_mask, *array_info->set_mask,
4207 *array_info->invert_mask);
4210 EXPORT_SYMBOL_GPL(gpiod_get_array);
4213 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4215 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4216 * @con_id: function within the GPIO consumer
4217 * @flags: optional GPIO initialization flags
4219 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4220 * assigned to the requested function it will return NULL.
4222 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4224 enum gpiod_flags flags)
4226 struct gpio_descs *descs;
4228 descs = gpiod_get_array(dev, con_id, flags);
4229 if (PTR_ERR(descs) == -ENOENT)
4234 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4237 * gpiod_put - dispose of a GPIO descriptor
4238 * @desc: GPIO descriptor to dispose of
4240 * No descriptor can be used after gpiod_put() has been called on it.
4242 void gpiod_put(struct gpio_desc *desc)
4247 EXPORT_SYMBOL_GPL(gpiod_put);
4250 * gpiod_put_array - dispose of multiple GPIO descriptors
4251 * @descs: struct gpio_descs containing an array of descriptors
4253 void gpiod_put_array(struct gpio_descs *descs)
4257 for (i = 0; i < descs->ndescs; i++)
4258 gpiod_put(descs->desc[i]);
4262 EXPORT_SYMBOL_GPL(gpiod_put_array);
4264 static int __init gpiolib_dev_init(void)
4268 /* Register GPIO sysfs bus */
4269 ret = bus_register(&gpio_bus_type);
4271 pr_err("gpiolib: could not register GPIO bus type\n");
4275 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4277 pr_err("gpiolib: failed to allocate char dev region\n");
4278 bus_unregister(&gpio_bus_type);
4282 gpiolib_initialized = true;
4283 gpiochip_setup_devs();
4285 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4286 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4287 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4291 core_initcall(gpiolib_dev_init);
4293 #ifdef CONFIG_DEBUG_FS
4295 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4298 struct gpio_chip *gc = gdev->chip;
4299 unsigned gpio = gdev->base;
4300 struct gpio_desc *gdesc = &gdev->descs[0];
4305 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4306 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4308 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4314 gpiod_get_direction(gdesc);
4315 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4316 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4317 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4318 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4319 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4320 is_out ? "out" : "in ",
4321 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
4322 is_irq ? "IRQ " : "",
4323 active_low ? "ACTIVE LOW" : "");
4324 seq_printf(s, "\n");
4328 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4330 unsigned long flags;
4331 struct gpio_device *gdev = NULL;
4332 loff_t index = *pos;
4336 spin_lock_irqsave(&gpio_lock, flags);
4337 list_for_each_entry(gdev, &gpio_devices, list)
4339 spin_unlock_irqrestore(&gpio_lock, flags);
4342 spin_unlock_irqrestore(&gpio_lock, flags);
4347 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4349 unsigned long flags;
4350 struct gpio_device *gdev = v;
4353 spin_lock_irqsave(&gpio_lock, flags);
4354 if (list_is_last(&gdev->list, &gpio_devices))
4357 ret = list_entry(gdev->list.next, struct gpio_device, list);
4358 spin_unlock_irqrestore(&gpio_lock, flags);
4366 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4370 static int gpiolib_seq_show(struct seq_file *s, void *v)
4372 struct gpio_device *gdev = v;
4373 struct gpio_chip *gc = gdev->chip;
4374 struct device *parent;
4377 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4378 dev_name(&gdev->dev));
4382 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4383 dev_name(&gdev->dev),
4384 gdev->base, gdev->base + gdev->ngpio - 1);
4385 parent = gc->parent;
4387 seq_printf(s, ", parent: %s/%s",
4388 parent->bus ? parent->bus->name : "no-bus",
4391 seq_printf(s, ", %s", gc->label);
4393 seq_printf(s, ", can sleep");
4394 seq_printf(s, ":\n");
4397 gc->dbg_show(s, gc);
4399 gpiolib_dbg_show(s, gdev);
4404 static const struct seq_operations gpiolib_seq_ops = {
4405 .start = gpiolib_seq_start,
4406 .next = gpiolib_seq_next,
4407 .stop = gpiolib_seq_stop,
4408 .show = gpiolib_seq_show,
4411 static int gpiolib_open(struct inode *inode, struct file *file)
4413 return seq_open(file, &gpiolib_seq_ops);
4416 static const struct file_operations gpiolib_operations = {
4417 .owner = THIS_MODULE,
4418 .open = gpiolib_open,
4420 .llseek = seq_lseek,
4421 .release = seq_release,
4424 static int __init gpiolib_debugfs_init(void)
4426 /* /sys/kernel/debug/gpio */
4427 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
4428 &gpiolib_operations);
4431 subsys_initcall(gpiolib_debugfs_init);
4433 #endif /* DEBUG_FS */