1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/gpio.h>
24 /* Implementation infrastructure for GPIO interfaces.
26 * The GPIO programming interface allows for inlining speed-critical
27 * get/set operations for common cases, so that access to SOC-integrated
28 * GPIOs can sometimes cost only an instruction or two per bit.
32 /* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
39 #define extra_checks 1
41 #define extra_checks 0
44 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
48 DEFINE_SPINLOCK(gpio_lock);
50 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
52 static DEFINE_MUTEX(gpio_lookup_lock);
53 static LIST_HEAD(gpio_lookup_list);
54 LIST_HEAD(gpio_chips);
57 static void gpiochip_free_hogs(struct gpio_chip *chip);
58 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
61 static inline void desc_set_label(struct gpio_desc *d, const char *label)
67 * Convert a GPIO number to its descriptor
69 struct gpio_desc *gpio_to_desc(unsigned gpio)
71 struct gpio_chip *chip;
74 spin_lock_irqsave(&gpio_lock, flags);
76 list_for_each_entry(chip, &gpio_chips, list) {
77 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
78 spin_unlock_irqrestore(&gpio_lock, flags);
79 return &chip->desc[gpio - chip->base];
83 spin_unlock_irqrestore(&gpio_lock, flags);
85 if (!gpio_is_valid(gpio))
86 WARN(1, "invalid GPIO %d\n", gpio);
90 EXPORT_SYMBOL_GPL(gpio_to_desc);
93 * Get the GPIO descriptor corresponding to the given hw number for this chip.
95 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
98 if (hwnum >= chip->ngpio)
99 return ERR_PTR(-EINVAL);
101 return &chip->desc[hwnum];
105 * Convert a GPIO descriptor to the integer namespace.
106 * This should disappear in the future but is needed since we still
107 * use GPIO numbers for error messages and sysfs nodes
109 int desc_to_gpio(const struct gpio_desc *desc)
111 return desc->chip->base + (desc - &desc->chip->desc[0]);
113 EXPORT_SYMBOL_GPL(desc_to_gpio);
117 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
118 * @desc: descriptor to return the chip of
120 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
122 return desc ? desc->chip : NULL;
124 EXPORT_SYMBOL_GPL(gpiod_to_chip);
126 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
127 static int gpiochip_find_base(int ngpio)
129 struct gpio_chip *chip;
130 int base = ARCH_NR_GPIOS - ngpio;
132 list_for_each_entry_reverse(chip, &gpio_chips, list) {
133 /* found a free space? */
134 if (chip->base + chip->ngpio <= base)
137 /* nope, check the space right before the chip */
138 base = chip->base - ngpio;
141 if (gpio_is_valid(base)) {
142 pr_debug("%s: found new base at %d\n", __func__, base);
145 pr_err("%s: cannot find free range\n", __func__);
151 * gpiod_get_direction - return the current direction of a GPIO
152 * @desc: GPIO to get the direction of
154 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
156 * This function may sleep if gpiod_cansleep() is true.
158 int gpiod_get_direction(struct gpio_desc *desc)
160 struct gpio_chip *chip;
162 int status = -EINVAL;
164 chip = gpiod_to_chip(desc);
165 offset = gpio_chip_hwgpio(desc);
167 if (!chip->get_direction)
170 status = chip->get_direction(chip, offset);
172 /* GPIOF_DIR_IN, or other positive */
174 clear_bit(FLAG_IS_OUT, &desc->flags);
178 set_bit(FLAG_IS_OUT, &desc->flags);
182 EXPORT_SYMBOL_GPL(gpiod_get_direction);
185 * Add a new chip to the global chips list, keeping the list of chips sorted
188 * Return -EBUSY if the new chip overlaps with some other chip's integer
191 static int gpiochip_add_to_list(struct gpio_chip *chip)
193 struct list_head *pos;
194 struct gpio_chip *_chip;
197 /* find where to insert our chip */
198 list_for_each(pos, &gpio_chips) {
199 _chip = list_entry(pos, struct gpio_chip, list);
200 /* shall we insert before _chip? */
201 if (_chip->base >= chip->base + chip->ngpio)
205 /* are we stepping on the chip right before? */
206 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
207 _chip = list_entry(pos->prev, struct gpio_chip, list);
208 if (_chip->base + _chip->ngpio > chip->base) {
210 "GPIO integer space overlap, cannot add chip\n");
216 list_add_tail(&chip->list, pos);
222 * Convert a GPIO name to its descriptor
224 static struct gpio_desc *gpio_name_to_desc(const char * const name)
226 struct gpio_chip *chip;
229 spin_lock_irqsave(&gpio_lock, flags);
231 list_for_each_entry(chip, &gpio_chips, list) {
234 for (i = 0; i != chip->ngpio; ++i) {
235 struct gpio_desc *gpio = &chip->desc[i];
240 if (!strcmp(gpio->name, name)) {
241 spin_unlock_irqrestore(&gpio_lock, flags);
247 spin_unlock_irqrestore(&gpio_lock, flags);
253 * Takes the names from gc->names and checks if they are all unique. If they
254 * are, they are assigned to their gpio descriptors.
256 * Returns -EEXIST if one of the names is already used for a different GPIO.
258 static int gpiochip_set_desc_names(struct gpio_chip *gc)
265 /* First check all names if they are unique */
266 for (i = 0; i != gc->ngpio; ++i) {
267 struct gpio_desc *gpio;
269 gpio = gpio_name_to_desc(gc->names[i]);
271 dev_warn(gc->dev, "Detected name collision for "
276 /* Then add all names to the GPIO descriptors */
277 for (i = 0; i != gc->ngpio; ++i)
278 gc->desc[i].name = gc->names[i];
284 * gpiochip_add() - register a gpio_chip
285 * @chip: the chip to register, with chip->base initialized
286 * Context: potentially before irqs will work
288 * Returns a negative errno if the chip can't be registered, such as
289 * because the chip->base is invalid or already associated with a
290 * different chip. Otherwise it returns zero as a success code.
292 * When gpiochip_add() is called very early during boot, so that GPIOs
293 * can be freely used, the chip->dev device must be registered before
294 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
295 * for GPIOs will fail rudely.
297 * If chip->base is negative, this requests dynamic assignment of
298 * a range of valid GPIOs.
300 int gpiochip_add(struct gpio_chip *chip)
305 int base = chip->base;
306 struct gpio_desc *descs;
308 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
312 spin_lock_irqsave(&gpio_lock, flags);
315 base = gpiochip_find_base(chip->ngpio);
318 spin_unlock_irqrestore(&gpio_lock, flags);
324 status = gpiochip_add_to_list(chip);
326 spin_unlock_irqrestore(&gpio_lock, flags);
330 for (id = 0; id < chip->ngpio; id++) {
331 struct gpio_desc *desc = &descs[id];
335 /* REVISIT: most hardware initializes GPIOs as inputs (often
336 * with pullups enabled) so power usage is minimized. Linux
337 * code should set the gpio direction first thing; but until
338 * it does, and in case chip->get_direction is not set, we may
339 * expose the wrong direction in sysfs.
341 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
346 spin_unlock_irqrestore(&gpio_lock, flags);
348 #ifdef CONFIG_PINCTRL
349 INIT_LIST_HEAD(&chip->pin_ranges);
352 if (!chip->owner && chip->dev && chip->dev->driver)
353 chip->owner = chip->dev->driver->owner;
355 status = gpiochip_set_desc_names(chip);
357 goto err_remove_from_list;
359 status = of_gpiochip_add(chip);
361 goto err_remove_chip;
363 acpi_gpiochip_add(chip);
365 status = gpiochip_sysfs_register(chip);
367 goto err_remove_chip;
369 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
370 chip->base, chip->base + chip->ngpio - 1,
371 chip->label ? : "generic");
376 acpi_gpiochip_remove(chip);
377 gpiochip_free_hogs(chip);
378 of_gpiochip_remove(chip);
379 err_remove_from_list:
380 spin_lock_irqsave(&gpio_lock, flags);
381 list_del(&chip->list);
382 spin_unlock_irqrestore(&gpio_lock, flags);
387 /* failures here can mean systems won't boot... */
388 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
389 chip->base, chip->base + chip->ngpio - 1,
390 chip->label ? : "generic");
393 EXPORT_SYMBOL_GPL(gpiochip_add);
396 * gpiochip_remove() - unregister a gpio_chip
397 * @chip: the chip to unregister
399 * A gpio_chip with any GPIOs still requested may not be removed.
401 void gpiochip_remove(struct gpio_chip *chip)
403 struct gpio_desc *desc;
406 bool requested = false;
408 gpiochip_sysfs_unregister(chip);
410 gpiochip_irqchip_remove(chip);
412 acpi_gpiochip_remove(chip);
413 gpiochip_remove_pin_ranges(chip);
414 gpiochip_free_hogs(chip);
415 of_gpiochip_remove(chip);
417 spin_lock_irqsave(&gpio_lock, flags);
418 for (id = 0; id < chip->ngpio; id++) {
419 desc = &chip->desc[id];
421 if (test_bit(FLAG_REQUESTED, &desc->flags))
424 list_del(&chip->list);
425 spin_unlock_irqrestore(&gpio_lock, flags);
428 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
433 EXPORT_SYMBOL_GPL(gpiochip_remove);
436 * gpiochip_find() - iterator for locating a specific gpio_chip
437 * @data: data to pass to match function
438 * @callback: Callback function to check gpio_chip
440 * Similar to bus_find_device. It returns a reference to a gpio_chip as
441 * determined by a user supplied @match callback. The callback should return
442 * 0 if the device doesn't match and non-zero if it does. If the callback is
443 * non-zero, this function will return to the caller and not iterate over any
446 struct gpio_chip *gpiochip_find(void *data,
447 int (*match)(struct gpio_chip *chip,
450 struct gpio_chip *chip;
453 spin_lock_irqsave(&gpio_lock, flags);
454 list_for_each_entry(chip, &gpio_chips, list)
455 if (match(chip, data))
459 if (&chip->list == &gpio_chips)
461 spin_unlock_irqrestore(&gpio_lock, flags);
465 EXPORT_SYMBOL_GPL(gpiochip_find);
467 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
469 const char *name = data;
471 return !strcmp(chip->label, name);
474 static struct gpio_chip *find_chip_by_name(const char *name)
476 return gpiochip_find((void *)name, gpiochip_match_name);
479 #ifdef CONFIG_GPIOLIB_IRQCHIP
482 * The following is irqchip helper code for gpiochips.
486 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
487 * @gpiochip: the gpiochip to set the irqchip chain to
488 * @irqchip: the irqchip to chain to the gpiochip
489 * @parent_irq: the irq number corresponding to the parent IRQ for this
491 * @parent_handler: the parent interrupt handler for the accumulated IRQ
492 * coming out of the gpiochip. If the interrupt is nested rather than
493 * cascaded, pass NULL in this handler argument
495 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
496 struct irq_chip *irqchip,
498 irq_flow_handler_t parent_handler)
502 if (!gpiochip->irqdomain) {
503 chip_err(gpiochip, "called %s before setting up irqchip\n",
508 if (parent_handler) {
509 if (gpiochip->can_sleep) {
511 "you cannot have chained interrupts on a "
512 "chip that may sleep\n");
516 * The parent irqchip is already using the chip_data for this
517 * irqchip, so our callbacks simply use the handler_data.
519 irq_set_chained_handler_and_data(parent_irq, parent_handler,
522 gpiochip->irq_parent = parent_irq;
525 /* Set the parent IRQ for all affected IRQs */
526 for (offset = 0; offset < gpiochip->ngpio; offset++)
527 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
530 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
533 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
534 * @d: the irqdomain used by this irqchip
535 * @irq: the global irq number used by this GPIO irqchip irq
536 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
538 * This function will set up the mapping for a certain IRQ line on a
539 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
540 * stored inside the gpiochip.
542 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
543 irq_hw_number_t hwirq)
545 struct gpio_chip *chip = d->host_data;
547 irq_set_chip_data(irq, chip);
549 * This lock class tells lockdep that GPIO irqs are in a different
550 * category than their parents, so it won't report false recursion.
552 irq_set_lockdep_class(irq, chip->lock_key);
553 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
554 /* Chips that can sleep need nested thread handlers */
555 if (chip->can_sleep && !chip->irq_not_threaded)
556 irq_set_nested_thread(irq, 1);
557 irq_set_noprobe(irq);
560 * No set-up of the hardware will happen if IRQ_TYPE_NONE
561 * is passed as default type.
563 if (chip->irq_default_type != IRQ_TYPE_NONE)
564 irq_set_irq_type(irq, chip->irq_default_type);
569 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
571 struct gpio_chip *chip = d->host_data;
574 irq_set_nested_thread(irq, 0);
575 irq_set_chip_and_handler(irq, NULL, NULL);
576 irq_set_chip_data(irq, NULL);
579 static const struct irq_domain_ops gpiochip_domain_ops = {
580 .map = gpiochip_irq_map,
581 .unmap = gpiochip_irq_unmap,
582 /* Virtually all GPIO irqchips are twocell:ed */
583 .xlate = irq_domain_xlate_twocell,
586 static int gpiochip_irq_reqres(struct irq_data *d)
588 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
590 if (!try_module_get(chip->owner))
593 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
595 "unable to lock HW IRQ %lu for IRQ\n",
597 module_put(chip->owner);
603 static void gpiochip_irq_relres(struct irq_data *d)
605 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
607 gpiochip_unlock_as_irq(chip, d->hwirq);
608 module_put(chip->owner);
611 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
613 return irq_find_mapping(chip->irqdomain, offset);
617 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
618 * @gpiochip: the gpiochip to remove the irqchip from
620 * This is called only from gpiochip_remove()
622 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
626 acpi_gpiochip_free_interrupts(gpiochip);
628 if (gpiochip->irq_parent) {
629 irq_set_chained_handler(gpiochip->irq_parent, NULL);
630 irq_set_handler_data(gpiochip->irq_parent, NULL);
633 /* Remove all IRQ mappings and delete the domain */
634 if (gpiochip->irqdomain) {
635 for (offset = 0; offset < gpiochip->ngpio; offset++)
637 irq_find_mapping(gpiochip->irqdomain, offset));
638 irq_domain_remove(gpiochip->irqdomain);
641 if (gpiochip->irqchip) {
642 gpiochip->irqchip->irq_request_resources = NULL;
643 gpiochip->irqchip->irq_release_resources = NULL;
644 gpiochip->irqchip = NULL;
649 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
650 * @gpiochip: the gpiochip to add the irqchip to
651 * @irqchip: the irqchip to add to the gpiochip
652 * @first_irq: if not dynamically assigned, the base (first) IRQ to
653 * allocate gpiochip irqs from
654 * @handler: the irq handler to use (often a predefined irq core function)
655 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
656 * to have the core avoid setting up any default type in the hardware.
657 * @lock_key: lockdep class
659 * This function closely associates a certain irqchip with a certain
660 * gpiochip, providing an irq domain to translate the local IRQs to
661 * global irqs in the gpiolib core, and making sure that the gpiochip
662 * is passed as chip data to all related functions. Driver callbacks
663 * need to use container_of() to get their local state containers back
664 * from the gpiochip passed as chip data. An irqdomain will be stored
665 * in the gpiochip that shall be used by the driver to handle IRQ number
666 * translation. The gpiochip will need to be initialized and registered
667 * before calling this function.
669 * This function will handle two cell:ed simple IRQs and assumes all
670 * the pins on the gpiochip can generate a unique IRQ. Everything else
671 * need to be open coded.
673 int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
674 struct irq_chip *irqchip,
675 unsigned int first_irq,
676 irq_flow_handler_t handler,
678 struct lock_class_key *lock_key)
680 struct device_node *of_node;
682 unsigned irq_base = 0;
684 if (!gpiochip || !irqchip)
687 if (!gpiochip->dev) {
688 pr_err("missing gpiochip .dev parent pointer\n");
691 of_node = gpiochip->dev->of_node;
692 #ifdef CONFIG_OF_GPIO
694 * If the gpiochip has an assigned OF node this takes precedence
695 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
697 if (gpiochip->of_node)
698 of_node = gpiochip->of_node;
700 gpiochip->irqchip = irqchip;
701 gpiochip->irq_handler = handler;
702 gpiochip->irq_default_type = type;
703 gpiochip->to_irq = gpiochip_to_irq;
704 gpiochip->lock_key = lock_key;
705 gpiochip->irqdomain = irq_domain_add_simple(of_node,
706 gpiochip->ngpio, first_irq,
707 &gpiochip_domain_ops, gpiochip);
708 if (!gpiochip->irqdomain) {
709 gpiochip->irqchip = NULL;
714 * It is possible for a driver to override this, but only if the
715 * alternative functions are both implemented.
717 if (!irqchip->irq_request_resources &&
718 !irqchip->irq_release_resources) {
719 irqchip->irq_request_resources = gpiochip_irq_reqres;
720 irqchip->irq_release_resources = gpiochip_irq_relres;
724 * Prepare the mapping since the irqchip shall be orthogonal to
725 * any gpiochip calls. If the first_irq was zero, this is
726 * necessary to allocate descriptors for all IRQs.
728 for (offset = 0; offset < gpiochip->ngpio; offset++) {
729 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
732 * Store the base into the gpiochip to be used when
733 * unmapping the irqs.
735 gpiochip->irq_base = irq_base;
738 acpi_gpiochip_request_interrupts(gpiochip);
742 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
744 #else /* CONFIG_GPIOLIB_IRQCHIP */
746 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
748 #endif /* CONFIG_GPIOLIB_IRQCHIP */
750 #ifdef CONFIG_PINCTRL
753 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
754 * @chip: the gpiochip to add the range for
755 * @pctldev: the pin controller to map to
756 * @gpio_offset: the start offset in the current gpio_chip number space
757 * @pin_group: name of the pin group inside the pin controller
759 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
760 struct pinctrl_dev *pctldev,
761 unsigned int gpio_offset, const char *pin_group)
763 struct gpio_pin_range *pin_range;
766 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
768 chip_err(chip, "failed to allocate pin ranges\n");
772 /* Use local offset as range ID */
773 pin_range->range.id = gpio_offset;
774 pin_range->range.gc = chip;
775 pin_range->range.name = chip->label;
776 pin_range->range.base = chip->base + gpio_offset;
777 pin_range->pctldev = pctldev;
779 ret = pinctrl_get_group_pins(pctldev, pin_group,
780 &pin_range->range.pins,
781 &pin_range->range.npins);
787 pinctrl_add_gpio_range(pctldev, &pin_range->range);
789 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
790 gpio_offset, gpio_offset + pin_range->range.npins - 1,
791 pinctrl_dev_get_devname(pctldev), pin_group);
793 list_add_tail(&pin_range->node, &chip->pin_ranges);
797 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
800 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
801 * @chip: the gpiochip to add the range for
802 * @pinctrl_name: the dev_name() of the pin controller to map to
803 * @gpio_offset: the start offset in the current gpio_chip number space
804 * @pin_offset: the start offset in the pin controller number space
805 * @npins: the number of pins from the offset of each pin space (GPIO and
806 * pin controller) to accumulate in this range
808 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
809 unsigned int gpio_offset, unsigned int pin_offset,
812 struct gpio_pin_range *pin_range;
815 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
817 chip_err(chip, "failed to allocate pin ranges\n");
821 /* Use local offset as range ID */
822 pin_range->range.id = gpio_offset;
823 pin_range->range.gc = chip;
824 pin_range->range.name = chip->label;
825 pin_range->range.base = chip->base + gpio_offset;
826 pin_range->range.pin_base = pin_offset;
827 pin_range->range.npins = npins;
828 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
830 if (IS_ERR(pin_range->pctldev)) {
831 ret = PTR_ERR(pin_range->pctldev);
832 chip_err(chip, "could not create pin range\n");
836 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
837 gpio_offset, gpio_offset + npins - 1,
839 pin_offset, pin_offset + npins - 1);
841 list_add_tail(&pin_range->node, &chip->pin_ranges);
845 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
848 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
849 * @chip: the chip to remove all the mappings for
851 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
853 struct gpio_pin_range *pin_range, *tmp;
855 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
856 list_del(&pin_range->node);
857 pinctrl_remove_gpio_range(pin_range->pctldev,
862 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
864 #endif /* CONFIG_PINCTRL */
866 /* These "optional" allocation calls help prevent drivers from stomping
867 * on each other, and help provide better diagnostics in debugfs.
868 * They're called even less than the "set direction" calls.
870 static int __gpiod_request(struct gpio_desc *desc, const char *label)
872 struct gpio_chip *chip = desc->chip;
876 spin_lock_irqsave(&gpio_lock, flags);
878 /* NOTE: gpio_request() can be called in early boot,
879 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
882 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
883 desc_set_label(desc, label ? : "?");
891 /* chip->request may sleep */
892 spin_unlock_irqrestore(&gpio_lock, flags);
893 status = chip->request(chip, gpio_chip_hwgpio(desc));
894 spin_lock_irqsave(&gpio_lock, flags);
897 desc_set_label(desc, NULL);
898 clear_bit(FLAG_REQUESTED, &desc->flags);
902 if (chip->get_direction) {
903 /* chip->get_direction may sleep */
904 spin_unlock_irqrestore(&gpio_lock, flags);
905 gpiod_get_direction(desc);
906 spin_lock_irqsave(&gpio_lock, flags);
909 spin_unlock_irqrestore(&gpio_lock, flags);
913 int gpiod_request(struct gpio_desc *desc, const char *label)
915 int status = -EPROBE_DEFER;
916 struct gpio_chip *chip;
919 pr_warn("%s: invalid GPIO\n", __func__);
927 if (try_module_get(chip->owner)) {
928 status = __gpiod_request(desc, label);
930 module_put(chip->owner);
935 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
940 static bool __gpiod_free(struct gpio_desc *desc)
944 struct gpio_chip *chip;
948 gpiod_unexport(desc);
950 spin_lock_irqsave(&gpio_lock, flags);
953 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
955 spin_unlock_irqrestore(&gpio_lock, flags);
956 might_sleep_if(chip->can_sleep);
957 chip->free(chip, gpio_chip_hwgpio(desc));
958 spin_lock_irqsave(&gpio_lock, flags);
960 desc_set_label(desc, NULL);
961 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
962 clear_bit(FLAG_REQUESTED, &desc->flags);
963 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
964 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
965 clear_bit(FLAG_IS_HOGGED, &desc->flags);
969 spin_unlock_irqrestore(&gpio_lock, flags);
973 void gpiod_free(struct gpio_desc *desc)
975 if (desc && __gpiod_free(desc))
976 module_put(desc->chip->owner);
978 WARN_ON(extra_checks);
982 * gpiochip_is_requested - return string iff signal was requested
983 * @chip: controller managing the signal
984 * @offset: of signal within controller's 0..(ngpio - 1) range
986 * Returns NULL if the GPIO is not currently requested, else a string.
987 * The string returned is the label passed to gpio_request(); if none has been
988 * passed it is a meaningless, non-NULL constant.
990 * This function is for use by GPIO controller drivers. The label can
991 * help with diagnostics, and knowing that the signal is used as a GPIO
992 * can help avoid accidentally multiplexing it to another controller.
994 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
996 struct gpio_desc *desc;
998 if (!GPIO_OFFSET_VALID(chip, offset))
1001 desc = &chip->desc[offset];
1003 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1007 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1010 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1011 * @desc: GPIO descriptor to request
1012 * @label: label for the GPIO
1014 * Function allows GPIO chip drivers to request and use their own GPIO
1015 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1016 * function will not increase reference count of the GPIO chip module. This
1017 * allows the GPIO chip module to be unloaded as needed (we assume that the
1018 * GPIO chip driver handles freeing the GPIOs it has requested).
1020 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1023 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1027 chip_err(chip, "failed to get GPIO descriptor\n");
1031 err = __gpiod_request(desc, label);
1033 return ERR_PTR(err);
1037 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
1040 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1041 * @desc: GPIO descriptor to free
1043 * Function frees the given GPIO requested previously with
1044 * gpiochip_request_own_desc().
1046 void gpiochip_free_own_desc(struct gpio_desc *desc)
1051 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
1053 /* Drivers MUST set GPIO direction before making get/set calls. In
1054 * some cases this is done in early boot, before IRQs are enabled.
1056 * As a rule these aren't called more than once (except for drivers
1057 * using the open-drain emulation idiom) so these are natural places
1058 * to accumulate extra debugging checks. Note that we can't (yet)
1059 * rely on gpio_request() having been called beforehand.
1063 * gpiod_direction_input - set the GPIO direction to input
1064 * @desc: GPIO to set to input
1066 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1067 * be called safely on it.
1069 * Return 0 in case of success, else an error code.
1071 int gpiod_direction_input(struct gpio_desc *desc)
1073 struct gpio_chip *chip;
1074 int status = -EINVAL;
1076 if (!desc || !desc->chip) {
1077 pr_warn("%s: invalid GPIO\n", __func__);
1082 if (!chip->get || !chip->direction_input) {
1084 "%s: missing get() or direction_input() operations\n",
1089 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
1091 clear_bit(FLAG_IS_OUT, &desc->flags);
1093 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1097 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1099 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1101 struct gpio_chip *chip;
1102 int status = -EINVAL;
1104 /* GPIOs used for IRQs shall not be set as output */
1105 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1107 "%s: tried to set a GPIO tied to an IRQ as output\n",
1112 /* Open drain pin should not be driven to 1 */
1113 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1114 return gpiod_direction_input(desc);
1116 /* Open source pin should not be driven to 0 */
1117 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1118 return gpiod_direction_input(desc);
1121 if (!chip->set || !chip->direction_output) {
1123 "%s: missing set() or direction_output() operations\n",
1128 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1130 set_bit(FLAG_IS_OUT, &desc->flags);
1131 trace_gpio_value(desc_to_gpio(desc), 0, value);
1132 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1137 * gpiod_direction_output_raw - set the GPIO direction to output
1138 * @desc: GPIO to set to output
1139 * @value: initial output value of the GPIO
1141 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1142 * be called safely on it. The initial value of the output must be specified
1143 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1145 * Return 0 in case of success, else an error code.
1147 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1149 if (!desc || !desc->chip) {
1150 pr_warn("%s: invalid GPIO\n", __func__);
1153 return _gpiod_direction_output_raw(desc, value);
1155 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1158 * gpiod_direction_output - set the GPIO direction to output
1159 * @desc: GPIO to set to output
1160 * @value: initial output value of the GPIO
1162 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1163 * be called safely on it. The initial value of the output must be specified
1164 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1167 * Return 0 in case of success, else an error code.
1169 int gpiod_direction_output(struct gpio_desc *desc, int value)
1171 if (!desc || !desc->chip) {
1172 pr_warn("%s: invalid GPIO\n", __func__);
1175 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1177 return _gpiod_direction_output_raw(desc, value);
1179 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1182 * gpiod_set_debounce - sets @debounce time for a @gpio
1183 * @gpio: the gpio to set debounce time
1184 * @debounce: debounce time is microseconds
1186 * returns -ENOTSUPP if the controller does not support setting
1189 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1191 struct gpio_chip *chip;
1193 if (!desc || !desc->chip) {
1194 pr_warn("%s: invalid GPIO\n", __func__);
1199 if (!chip->set || !chip->set_debounce) {
1201 "%s: missing set() or set_debounce() operations\n",
1206 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1208 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1211 * gpiod_is_active_low - test whether a GPIO is active-low or not
1212 * @desc: the gpio descriptor to test
1214 * Returns 1 if the GPIO is active-low, 0 otherwise.
1216 int gpiod_is_active_low(const struct gpio_desc *desc)
1218 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1220 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1222 /* I/O calls are only valid after configuration completed; the relevant
1223 * "is this a valid GPIO" error checks should already have been done.
1225 * "Get" operations are often inlinable as reading a pin value register,
1226 * and masking the relevant bit in that register.
1228 * When "set" operations are inlinable, they involve writing that mask to
1229 * one register to set a low value, or a different register to set it high.
1230 * Otherwise locking is needed, so there may be little value to inlining.
1232 *------------------------------------------------------------------------
1234 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1235 * have requested the GPIO. That can include implicit requesting by
1236 * a direction setting call. Marking a gpio as requested locks its chip
1237 * in memory, guaranteeing that these table lookups need no more locking
1238 * and that gpiochip_remove() will fail.
1240 * REVISIT when debugging, consider adding some instrumentation to ensure
1241 * that the GPIO was actually requested.
1244 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
1246 struct gpio_chip *chip;
1251 offset = gpio_chip_hwgpio(desc);
1252 value = chip->get ? chip->get(chip, offset) : -EIO;
1253 value = value < 0 ? value : !!value;
1254 trace_gpio_value(desc_to_gpio(desc), 1, value);
1259 * gpiod_get_raw_value() - return a gpio's raw value
1260 * @desc: gpio whose value will be returned
1262 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1263 * its ACTIVE_LOW status, or negative errno on failure.
1265 * This function should be called from contexts where we cannot sleep, and will
1266 * complain if the GPIO chip functions potentially sleep.
1268 int gpiod_get_raw_value(const struct gpio_desc *desc)
1272 /* Should be using gpio_get_value_cansleep() */
1273 WARN_ON(desc->chip->can_sleep);
1274 return _gpiod_get_raw_value(desc);
1276 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1279 * gpiod_get_value() - return a gpio's value
1280 * @desc: gpio whose value will be returned
1282 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1283 * account, or negative errno on failure.
1285 * This function should be called from contexts where we cannot sleep, and will
1286 * complain if the GPIO chip functions potentially sleep.
1288 int gpiod_get_value(const struct gpio_desc *desc)
1293 /* Should be using gpio_get_value_cansleep() */
1294 WARN_ON(desc->chip->can_sleep);
1296 value = _gpiod_get_raw_value(desc);
1300 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1305 EXPORT_SYMBOL_GPL(gpiod_get_value);
1308 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1309 * @desc: gpio descriptor whose state need to be set.
1310 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1312 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1315 struct gpio_chip *chip = desc->chip;
1316 int offset = gpio_chip_hwgpio(desc);
1319 err = chip->direction_input(chip, offset);
1321 clear_bit(FLAG_IS_OUT, &desc->flags);
1323 err = chip->direction_output(chip, offset, 0);
1325 set_bit(FLAG_IS_OUT, &desc->flags);
1327 trace_gpio_direction(desc_to_gpio(desc), value, err);
1330 "%s: Error in set_value for open drain err %d\n",
1335 * _gpio_set_open_source_value() - Set the open source gpio's value.
1336 * @desc: gpio descriptor whose state need to be set.
1337 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1339 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1342 struct gpio_chip *chip = desc->chip;
1343 int offset = gpio_chip_hwgpio(desc);
1346 err = chip->direction_output(chip, offset, 1);
1348 set_bit(FLAG_IS_OUT, &desc->flags);
1350 err = chip->direction_input(chip, offset);
1352 clear_bit(FLAG_IS_OUT, &desc->flags);
1354 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1357 "%s: Error in set_value for open source err %d\n",
1361 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1363 struct gpio_chip *chip;
1366 trace_gpio_value(desc_to_gpio(desc), 0, value);
1367 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1368 _gpio_set_open_drain_value(desc, value);
1369 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1370 _gpio_set_open_source_value(desc, value);
1372 chip->set(chip, gpio_chip_hwgpio(desc), value);
1376 * set multiple outputs on the same chip;
1377 * use the chip's set_multiple function if available;
1378 * otherwise set the outputs sequentially;
1379 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1380 * defines which outputs are to be changed
1381 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1382 * defines the values the outputs specified by mask are to be set to
1384 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1385 unsigned long *mask, unsigned long *bits)
1387 if (chip->set_multiple) {
1388 chip->set_multiple(chip, mask, bits);
1391 for (i = 0; i < chip->ngpio; i++) {
1392 if (mask[BIT_WORD(i)] == 0) {
1393 /* no more set bits in this mask word;
1394 * skip ahead to the next word */
1395 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1398 /* set outputs if the corresponding mask bit is set */
1399 if (__test_and_clear_bit(i, mask))
1400 chip->set(chip, i, test_bit(i, bits));
1405 static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1406 unsigned int array_size,
1407 struct gpio_desc **desc_array,
1412 while (i < array_size) {
1413 struct gpio_chip *chip = desc_array[i]->chip;
1414 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1415 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1419 WARN_ON(chip->can_sleep);
1421 memset(mask, 0, sizeof(mask));
1423 struct gpio_desc *desc = desc_array[i];
1424 int hwgpio = gpio_chip_hwgpio(desc);
1425 int value = value_array[i];
1427 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1429 trace_gpio_value(desc_to_gpio(desc), 0, value);
1431 * collect all normal outputs belonging to the same chip
1432 * open drain and open source outputs are set individually
1434 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1435 _gpio_set_open_drain_value(desc, value);
1436 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1437 _gpio_set_open_source_value(desc, value);
1439 __set_bit(hwgpio, mask);
1441 __set_bit(hwgpio, bits);
1443 __clear_bit(hwgpio, bits);
1447 } while ((i < array_size) && (desc_array[i]->chip == chip));
1448 /* push collected bits to outputs */
1450 gpio_chip_set_multiple(chip, mask, bits);
1455 * gpiod_set_raw_value() - assign a gpio's raw value
1456 * @desc: gpio whose value will be assigned
1457 * @value: value to assign
1459 * Set the raw value of the GPIO, i.e. the value of its physical line without
1460 * regard for its ACTIVE_LOW status.
1462 * This function should be called from contexts where we cannot sleep, and will
1463 * complain if the GPIO chip functions potentially sleep.
1465 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1469 /* Should be using gpio_set_value_cansleep() */
1470 WARN_ON(desc->chip->can_sleep);
1471 _gpiod_set_raw_value(desc, value);
1473 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1476 * gpiod_set_value() - assign a gpio's value
1477 * @desc: gpio whose value will be assigned
1478 * @value: value to assign
1480 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1483 * This function should be called from contexts where we cannot sleep, and will
1484 * complain if the GPIO chip functions potentially sleep.
1486 void gpiod_set_value(struct gpio_desc *desc, int value)
1490 /* Should be using gpio_set_value_cansleep() */
1491 WARN_ON(desc->chip->can_sleep);
1492 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1494 _gpiod_set_raw_value(desc, value);
1496 EXPORT_SYMBOL_GPL(gpiod_set_value);
1499 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1500 * @array_size: number of elements in the descriptor / value arrays
1501 * @desc_array: array of GPIO descriptors whose values will be assigned
1502 * @value_array: array of values to assign
1504 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1505 * without regard for their ACTIVE_LOW status.
1507 * This function should be called from contexts where we cannot sleep, and will
1508 * complain if the GPIO chip functions potentially sleep.
1510 void gpiod_set_raw_array_value(unsigned int array_size,
1511 struct gpio_desc **desc_array, int *value_array)
1515 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1518 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
1521 * gpiod_set_array_value() - assign values to an array of GPIOs
1522 * @array_size: number of elements in the descriptor / value arrays
1523 * @desc_array: array of GPIO descriptors whose values will be assigned
1524 * @value_array: array of values to assign
1526 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1529 * This function should be called from contexts where we cannot sleep, and will
1530 * complain if the GPIO chip functions potentially sleep.
1532 void gpiod_set_array_value(unsigned int array_size,
1533 struct gpio_desc **desc_array, int *value_array)
1537 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1540 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
1543 * gpiod_cansleep() - report whether gpio value access may sleep
1544 * @desc: gpio to check
1547 int gpiod_cansleep(const struct gpio_desc *desc)
1551 return desc->chip->can_sleep;
1553 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1556 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1557 * @desc: gpio whose IRQ will be returned (already requested)
1559 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1562 int gpiod_to_irq(const struct gpio_desc *desc)
1564 struct gpio_chip *chip;
1570 offset = gpio_chip_hwgpio(desc);
1571 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1573 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1576 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1577 * @chip: the chip the GPIO to lock belongs to
1578 * @offset: the offset of the GPIO to lock as IRQ
1580 * This is used directly by GPIO drivers that want to lock down
1581 * a certain GPIO line to be used for IRQs.
1583 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1585 if (offset >= chip->ngpio)
1588 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1590 "%s: tried to flag a GPIO set as output for IRQ\n",
1595 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1598 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1601 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1602 * @chip: the chip the GPIO to lock belongs to
1603 * @offset: the offset of the GPIO to lock as IRQ
1605 * This is used directly by GPIO drivers that want to indicate
1606 * that a certain GPIO is no longer used exclusively for IRQ.
1608 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1610 if (offset >= chip->ngpio)
1613 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1615 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1618 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1619 * @desc: gpio whose value will be returned
1621 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1622 * its ACTIVE_LOW status, or negative errno on failure.
1624 * This function is to be called from contexts that can sleep.
1626 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1628 might_sleep_if(extra_checks);
1631 return _gpiod_get_raw_value(desc);
1633 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1636 * gpiod_get_value_cansleep() - return a gpio's value
1637 * @desc: gpio whose value will be returned
1639 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1640 * account, or negative errno on failure.
1642 * This function is to be called from contexts that can sleep.
1644 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1648 might_sleep_if(extra_checks);
1652 value = _gpiod_get_raw_value(desc);
1656 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1661 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
1664 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1665 * @desc: gpio whose value will be assigned
1666 * @value: value to assign
1668 * Set the raw value of the GPIO, i.e. the value of its physical line without
1669 * regard for its ACTIVE_LOW status.
1671 * This function is to be called from contexts that can sleep.
1673 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
1675 might_sleep_if(extra_checks);
1678 _gpiod_set_raw_value(desc, value);
1680 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
1683 * gpiod_set_value_cansleep() - assign a gpio's value
1684 * @desc: gpio whose value will be assigned
1685 * @value: value to assign
1687 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1690 * This function is to be called from contexts that can sleep.
1692 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1694 might_sleep_if(extra_checks);
1698 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1700 _gpiod_set_raw_value(desc, value);
1702 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1705 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
1706 * @array_size: number of elements in the descriptor / value arrays
1707 * @desc_array: array of GPIO descriptors whose values will be assigned
1708 * @value_array: array of values to assign
1710 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1711 * without regard for their ACTIVE_LOW status.
1713 * This function is to be called from contexts that can sleep.
1715 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1716 struct gpio_desc **desc_array,
1719 might_sleep_if(extra_checks);
1722 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1725 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
1728 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
1729 * @array_size: number of elements in the descriptor / value arrays
1730 * @desc_array: array of GPIO descriptors whose values will be assigned
1731 * @value_array: array of values to assign
1733 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1736 * This function is to be called from contexts that can sleep.
1738 void gpiod_set_array_value_cansleep(unsigned int array_size,
1739 struct gpio_desc **desc_array,
1742 might_sleep_if(extra_checks);
1745 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1748 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
1751 * gpiod_add_lookup_table() - register GPIO device consumers
1752 * @table: table of consumers to register
1754 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
1756 mutex_lock(&gpio_lookup_lock);
1758 list_add_tail(&table->list, &gpio_lookup_list);
1760 mutex_unlock(&gpio_lookup_lock);
1764 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1765 * @table: table of consumers to unregister
1767 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1769 mutex_lock(&gpio_lookup_lock);
1771 list_del(&table->list);
1773 mutex_unlock(&gpio_lookup_lock);
1776 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
1778 enum gpio_lookup_flags *flags)
1780 char prop_name[32]; /* 32 is max size of property name */
1781 enum of_gpio_flags of_flags;
1782 struct gpio_desc *desc;
1785 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1787 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
1790 snprintf(prop_name, sizeof(prop_name), "%s",
1793 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1795 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1802 if (of_flags & OF_GPIO_ACTIVE_LOW)
1803 *flags |= GPIO_ACTIVE_LOW;
1808 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1810 enum gpio_lookup_flags *flags)
1812 struct acpi_device *adev = ACPI_COMPANION(dev);
1813 struct acpi_gpio_info info;
1814 struct gpio_desc *desc;
1818 /* Try first from _DSD */
1819 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1820 if (con_id && strcmp(con_id, "gpios")) {
1821 snprintf(propname, sizeof(propname), "%s-%s",
1822 con_id, gpio_suffixes[i]);
1824 snprintf(propname, sizeof(propname), "%s",
1828 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1829 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1833 /* Then from plain _CRS GPIOs */
1835 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1840 if (info.active_low)
1841 *flags |= GPIO_ACTIVE_LOW;
1846 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1848 const char *dev_id = dev ? dev_name(dev) : NULL;
1849 struct gpiod_lookup_table *table;
1851 mutex_lock(&gpio_lookup_lock);
1853 list_for_each_entry(table, &gpio_lookup_list, list) {
1854 if (table->dev_id && dev_id) {
1856 * Valid strings on both ends, must be identical to have
1859 if (!strcmp(table->dev_id, dev_id))
1863 * One of the pointers is NULL, so both must be to have
1866 if (dev_id == table->dev_id)
1873 mutex_unlock(&gpio_lookup_lock);
1877 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1879 enum gpio_lookup_flags *flags)
1881 struct gpio_desc *desc = ERR_PTR(-ENOENT);
1882 struct gpiod_lookup_table *table;
1883 struct gpiod_lookup *p;
1885 table = gpiod_find_lookup_table(dev);
1889 for (p = &table->table[0]; p->chip_label; p++) {
1890 struct gpio_chip *chip;
1892 /* idx must always match exactly */
1896 /* If the lookup entry has a con_id, require exact match */
1897 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1900 chip = find_chip_by_name(p->chip_label);
1903 dev_err(dev, "cannot find GPIO chip %s\n",
1905 return ERR_PTR(-ENODEV);
1908 if (chip->ngpio <= p->chip_hwnum) {
1910 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1911 idx, chip->ngpio, chip->label);
1912 return ERR_PTR(-EINVAL);
1915 desc = gpiochip_get_desc(chip, p->chip_hwnum);
1924 static int dt_gpio_count(struct device *dev, const char *con_id)
1930 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1932 snprintf(propname, sizeof(propname), "%s-%s",
1933 con_id, gpio_suffixes[i]);
1935 snprintf(propname, sizeof(propname), "%s",
1938 ret = of_gpio_named_count(dev->of_node, propname);
1945 static int platform_gpio_count(struct device *dev, const char *con_id)
1947 struct gpiod_lookup_table *table;
1948 struct gpiod_lookup *p;
1949 unsigned int count = 0;
1951 table = gpiod_find_lookup_table(dev);
1955 for (p = &table->table[0]; p->chip_label; p++) {
1956 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1957 (!con_id && !p->con_id))
1967 * gpiod_count - return the number of GPIOs associated with a device / function
1968 * or -ENOENT if no GPIO has been assigned to the requested function
1969 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1970 * @con_id: function within the GPIO consumer
1972 int gpiod_count(struct device *dev, const char *con_id)
1974 int count = -ENOENT;
1976 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
1977 count = dt_gpio_count(dev, con_id);
1978 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
1979 count = acpi_gpio_count(dev, con_id);
1982 count = platform_gpio_count(dev, con_id);
1986 EXPORT_SYMBOL_GPL(gpiod_count);
1989 * gpiod_get - obtain a GPIO for a given GPIO function
1990 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1991 * @con_id: function within the GPIO consumer
1992 * @flags: optional GPIO initialization flags
1994 * Return the GPIO descriptor corresponding to the function con_id of device
1995 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1996 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
1998 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
1999 enum gpiod_flags flags)
2001 return gpiod_get_index(dev, con_id, 0, flags);
2003 EXPORT_SYMBOL_GPL(gpiod_get);
2006 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2007 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2008 * @con_id: function within the GPIO consumer
2009 * @flags: optional GPIO initialization flags
2011 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2012 * the requested function it will return NULL. This is convenient for drivers
2013 * that need to handle optional GPIOs.
2015 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
2017 enum gpiod_flags flags)
2019 return gpiod_get_index_optional(dev, con_id, 0, flags);
2021 EXPORT_SYMBOL_GPL(gpiod_get_optional);
2025 * gpiod_configure_flags - helper function to configure a given GPIO
2026 * @desc: gpio whose value will be assigned
2027 * @con_id: function within the GPIO consumer
2028 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2030 * @dflags: gpiod_flags - optional GPIO initialization flags
2032 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2033 * requested function and/or index, or another IS_ERR() code if an error
2034 * occurred while trying to acquire the GPIO.
2036 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2037 unsigned long lflags, enum gpiod_flags dflags)
2041 if (lflags & GPIO_ACTIVE_LOW)
2042 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2043 if (lflags & GPIO_OPEN_DRAIN)
2044 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2045 if (lflags & GPIO_OPEN_SOURCE)
2046 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2048 /* No particular flag request, return here... */
2049 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2050 pr_debug("no flags found for %s\n", con_id);
2055 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2056 status = gpiod_direction_output(desc,
2057 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2059 status = gpiod_direction_input(desc);
2065 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2066 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2067 * @con_id: function within the GPIO consumer
2068 * @idx: index of the GPIO to obtain in the consumer
2069 * @flags: optional GPIO initialization flags
2071 * This variant of gpiod_get() allows to access GPIOs other than the first
2072 * defined one for functions that define several GPIOs.
2074 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2075 * requested function and/or index, or another IS_ERR() code if an error
2076 * occurred while trying to acquire the GPIO.
2078 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2081 enum gpiod_flags flags)
2083 struct gpio_desc *desc = NULL;
2085 enum gpio_lookup_flags lookupflags = 0;
2087 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2090 /* Using device tree? */
2091 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2092 dev_dbg(dev, "using device tree for GPIO lookup\n");
2093 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2094 } else if (ACPI_COMPANION(dev)) {
2095 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2096 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2101 * Either we are not using DT or ACPI, or their lookup did not return
2102 * a result. In that case, use platform lookup as a fallback.
2104 if (!desc || desc == ERR_PTR(-ENOENT)) {
2105 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
2106 desc = gpiod_find(dev, con_id, idx, &lookupflags);
2110 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
2114 status = gpiod_request(desc, con_id);
2116 return ERR_PTR(status);
2118 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
2120 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2122 return ERR_PTR(status);
2127 EXPORT_SYMBOL_GPL(gpiod_get_index);
2130 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2131 * @fwnode: handle of the firmware node
2132 * @propname: name of the firmware property representing the GPIO
2134 * This function can be used for drivers that get their configuration
2137 * Function properly finds the corresponding GPIO using whatever is the
2138 * underlying firmware interface and then makes sure that the GPIO
2139 * descriptor is requested before it is returned to the caller.
2141 * In case of error an ERR_PTR() is returned.
2143 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2144 const char *propname)
2146 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2147 bool active_low = false;
2151 return ERR_PTR(-EINVAL);
2153 if (is_of_node(fwnode)) {
2154 enum of_gpio_flags flags;
2156 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
2159 active_low = flags & OF_GPIO_ACTIVE_LOW;
2160 } else if (is_acpi_node(fwnode)) {
2161 struct acpi_gpio_info info;
2163 desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0,
2166 active_low = info.active_low;
2172 ret = gpiod_request(desc, NULL);
2174 return ERR_PTR(ret);
2176 /* Only value flag can be set from both DT and ACPI is active_low */
2178 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2182 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2185 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2187 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2188 * @con_id: function within the GPIO consumer
2189 * @index: index of the GPIO to obtain in the consumer
2190 * @flags: optional GPIO initialization flags
2192 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2193 * specified index was assigned to the requested function it will return NULL.
2194 * This is convenient for drivers that need to handle optional GPIOs.
2196 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
2199 enum gpiod_flags flags)
2201 struct gpio_desc *desc;
2203 desc = gpiod_get_index(dev, con_id, index, flags);
2205 if (PTR_ERR(desc) == -ENOENT)
2211 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
2214 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2215 * @desc: gpio whose value will be assigned
2216 * @name: gpio line name
2217 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2219 * @dflags: gpiod_flags - optional GPIO initialization flags
2221 int gpiod_hog(struct gpio_desc *desc, const char *name,
2222 unsigned long lflags, enum gpiod_flags dflags)
2224 struct gpio_chip *chip;
2225 struct gpio_desc *local_desc;
2229 chip = gpiod_to_chip(desc);
2230 hwnum = gpio_chip_hwgpio(desc);
2232 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2233 if (IS_ERR(local_desc)) {
2234 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2235 name, chip->label, hwnum);
2236 return PTR_ERR(local_desc);
2239 status = gpiod_configure_flags(desc, name, lflags, dflags);
2241 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2242 name, chip->label, hwnum);
2243 gpiochip_free_own_desc(desc);
2247 /* Mark GPIO as hogged so it can be identified and removed later */
2248 set_bit(FLAG_IS_HOGGED, &desc->flags);
2250 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2251 desc_to_gpio(desc), name,
2252 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2253 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2254 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2260 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2261 * @chip: gpio chip to act on
2263 * This is only used by of_gpiochip_remove to free hogged gpios
2265 static void gpiochip_free_hogs(struct gpio_chip *chip)
2269 for (id = 0; id < chip->ngpio; id++) {
2270 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2271 gpiochip_free_own_desc(&chip->desc[id]);
2276 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2277 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2278 * @con_id: function within the GPIO consumer
2279 * @flags: optional GPIO initialization flags
2281 * This function acquires all the GPIOs defined under a given function.
2283 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2284 * no GPIO has been assigned to the requested function, or another IS_ERR()
2285 * code if an error occurred while trying to acquire the GPIOs.
2287 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2289 enum gpiod_flags flags)
2291 struct gpio_desc *desc;
2292 struct gpio_descs *descs;
2295 count = gpiod_count(dev, con_id);
2297 return ERR_PTR(count);
2299 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2302 return ERR_PTR(-ENOMEM);
2304 for (descs->ndescs = 0; descs->ndescs < count; ) {
2305 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2307 gpiod_put_array(descs);
2308 return ERR_CAST(desc);
2310 descs->desc[descs->ndescs] = desc;
2315 EXPORT_SYMBOL_GPL(gpiod_get_array);
2318 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2320 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2321 * @con_id: function within the GPIO consumer
2322 * @flags: optional GPIO initialization flags
2324 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2325 * assigned to the requested function it will return NULL.
2327 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2329 enum gpiod_flags flags)
2331 struct gpio_descs *descs;
2333 descs = gpiod_get_array(dev, con_id, flags);
2334 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2339 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2342 * gpiod_put - dispose of a GPIO descriptor
2343 * @desc: GPIO descriptor to dispose of
2345 * No descriptor can be used after gpiod_put() has been called on it.
2347 void gpiod_put(struct gpio_desc *desc)
2351 EXPORT_SYMBOL_GPL(gpiod_put);
2354 * gpiod_put_array - dispose of multiple GPIO descriptors
2355 * @descs: struct gpio_descs containing an array of descriptors
2357 void gpiod_put_array(struct gpio_descs *descs)
2361 for (i = 0; i < descs->ndescs; i++)
2362 gpiod_put(descs->desc[i]);
2366 EXPORT_SYMBOL_GPL(gpiod_put_array);
2368 #ifdef CONFIG_DEBUG_FS
2370 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2373 unsigned gpio = chip->base;
2374 struct gpio_desc *gdesc = &chip->desc[0];
2378 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2379 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2381 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2387 gpiod_get_direction(gdesc);
2388 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2389 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2390 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2391 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
2392 is_out ? "out" : "in ",
2394 ? (chip->get(chip, i) ? "hi" : "lo")
2396 is_irq ? "IRQ" : " ");
2397 seq_printf(s, "\n");
2401 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2403 unsigned long flags;
2404 struct gpio_chip *chip = NULL;
2405 loff_t index = *pos;
2409 spin_lock_irqsave(&gpio_lock, flags);
2410 list_for_each_entry(chip, &gpio_chips, list)
2412 spin_unlock_irqrestore(&gpio_lock, flags);
2415 spin_unlock_irqrestore(&gpio_lock, flags);
2420 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2422 unsigned long flags;
2423 struct gpio_chip *chip = v;
2426 spin_lock_irqsave(&gpio_lock, flags);
2427 if (list_is_last(&chip->list, &gpio_chips))
2430 ret = list_entry(chip->list.next, struct gpio_chip, list);
2431 spin_unlock_irqrestore(&gpio_lock, flags);
2439 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2443 static int gpiolib_seq_show(struct seq_file *s, void *v)
2445 struct gpio_chip *chip = v;
2448 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2449 chip->base, chip->base + chip->ngpio - 1);
2452 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2455 seq_printf(s, ", %s", chip->label);
2456 if (chip->can_sleep)
2457 seq_printf(s, ", can sleep");
2458 seq_printf(s, ":\n");
2461 chip->dbg_show(s, chip);
2463 gpiolib_dbg_show(s, chip);
2468 static const struct seq_operations gpiolib_seq_ops = {
2469 .start = gpiolib_seq_start,
2470 .next = gpiolib_seq_next,
2471 .stop = gpiolib_seq_stop,
2472 .show = gpiolib_seq_show,
2475 static int gpiolib_open(struct inode *inode, struct file *file)
2477 return seq_open(file, &gpiolib_seq_ops);
2480 static const struct file_operations gpiolib_operations = {
2481 .owner = THIS_MODULE,
2482 .open = gpiolib_open,
2484 .llseek = seq_lseek,
2485 .release = seq_release,
2488 static int __init gpiolib_debugfs_init(void)
2490 /* /sys/kernel/debug/gpio */
2491 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2492 NULL, NULL, &gpiolib_operations);
2495 subsys_initcall(gpiolib_debugfs_init);
2497 #endif /* DEBUG_FS */