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>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/gpio.h>
23 /* Implementation infrastructure for GPIO interfaces.
25 * The GPIO programming interface allows for inlining speed-critical
26 * get/set operations for common cases, so that access to SOC-integrated
27 * GPIOs can sometimes cost only an instruction or two per bit.
31 /* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
38 #define extra_checks 1
40 #define extra_checks 0
43 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
47 DEFINE_SPINLOCK(gpio_lock);
49 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
51 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
53 static DEFINE_MUTEX(gpio_lookup_lock);
54 static LIST_HEAD(gpio_lookup_list);
55 LIST_HEAD(gpio_chips);
57 static inline void desc_set_label(struct gpio_desc *d, const char *label)
63 * Convert a GPIO number to its descriptor
65 struct gpio_desc *gpio_to_desc(unsigned gpio)
67 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
70 return &gpio_desc[gpio];
72 EXPORT_SYMBOL_GPL(gpio_to_desc);
75 * Get the GPIO descriptor corresponding to the given hw number for this chip.
77 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
80 if (hwnum >= chip->ngpio)
81 return ERR_PTR(-EINVAL);
83 return &chip->desc[hwnum];
85 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
88 * Convert a GPIO descriptor to the integer namespace.
89 * This should disappear in the future but is needed since we still
90 * use GPIO numbers for error messages and sysfs nodes
92 int desc_to_gpio(const struct gpio_desc *desc)
94 return desc - &gpio_desc[0];
96 EXPORT_SYMBOL_GPL(desc_to_gpio);
99 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
100 * when setting direction, and otherwise illegal. Until board setup code
101 * and drivers use explicit requests everywhere (which won't happen when
102 * those calls have no teeth) we can't avoid autorequesting. This nag
103 * message should motivate switching to explicit requests... so should
104 * the weaker cleanup after faults, compared to gpio_request().
106 * NOTE: the autorequest mechanism is going away; at this point it's
107 * only "legal" in the sense that (old) code using it won't break yet,
108 * but instead only triggers a WARN() stack dump.
110 static int gpio_ensure_requested(struct gpio_desc *desc)
112 const struct gpio_chip *chip = desc->chip;
113 const int gpio = desc_to_gpio(desc);
115 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
116 "autorequest GPIO-%d\n", gpio)) {
117 if (!try_module_get(chip->owner)) {
118 gpiod_err(desc, "%s: module can't be gotten\n",
120 clear_bit(FLAG_REQUESTED, &desc->flags);
124 desc_set_label(desc, "[auto]");
125 /* caller must chip->request() w/o spinlock */
133 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
134 * @desc: descriptor to return the chip of
136 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
138 return desc ? desc->chip : NULL;
140 EXPORT_SYMBOL_GPL(gpiod_to_chip);
142 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
143 static int gpiochip_find_base(int ngpio)
145 struct gpio_chip *chip;
146 int base = ARCH_NR_GPIOS - ngpio;
148 list_for_each_entry_reverse(chip, &gpio_chips, list) {
149 /* found a free space? */
150 if (chip->base + chip->ngpio <= base)
153 /* nope, check the space right before the chip */
154 base = chip->base - ngpio;
157 if (gpio_is_valid(base)) {
158 pr_debug("%s: found new base at %d\n", __func__, base);
161 pr_err("%s: cannot find free range\n", __func__);
167 * gpiod_get_direction - return the current direction of a GPIO
168 * @desc: GPIO to get the direction of
170 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
172 * This function may sleep if gpiod_cansleep() is true.
174 int gpiod_get_direction(const struct gpio_desc *desc)
176 struct gpio_chip *chip;
178 int status = -EINVAL;
180 chip = gpiod_to_chip(desc);
181 offset = gpio_chip_hwgpio(desc);
183 if (!chip->get_direction)
186 status = chip->get_direction(chip, offset);
188 /* GPIOF_DIR_IN, or other positive */
190 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
191 * so it does not affect constness per se */
192 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
196 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
200 EXPORT_SYMBOL_GPL(gpiod_get_direction);
203 * Add a new chip to the global chips list, keeping the list of chips sorted
206 * Return -EBUSY if the new chip overlaps with some other chip's integer
209 static int gpiochip_add_to_list(struct gpio_chip *chip)
211 struct list_head *pos = &gpio_chips;
212 struct gpio_chip *_chip;
215 /* find where to insert our chip */
216 list_for_each(pos, &gpio_chips) {
217 _chip = list_entry(pos, struct gpio_chip, list);
218 /* shall we insert before _chip? */
219 if (_chip->base >= chip->base + chip->ngpio)
223 /* are we stepping on the chip right before? */
224 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
225 _chip = list_entry(pos->prev, struct gpio_chip, list);
226 if (_chip->base + _chip->ngpio > chip->base) {
228 "GPIO integer space overlap, cannot add chip\n");
234 list_add_tail(&chip->list, pos);
240 * gpiochip_add() - register a gpio_chip
241 * @chip: the chip to register, with chip->base initialized
242 * Context: potentially before irqs or kmalloc will work
244 * Returns a negative errno if the chip can't be registered, such as
245 * because the chip->base is invalid or already associated with a
246 * different chip. Otherwise it returns zero as a success code.
248 * When gpiochip_add() is called very early during boot, so that GPIOs
249 * can be freely used, the chip->dev device must be registered before
250 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
251 * for GPIOs will fail rudely.
253 * If chip->base is negative, this requests dynamic assignment of
254 * a range of valid GPIOs.
256 int gpiochip_add(struct gpio_chip *chip)
261 int base = chip->base;
263 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
269 spin_lock_irqsave(&gpio_lock, flags);
272 base = gpiochip_find_base(chip->ngpio);
280 status = gpiochip_add_to_list(chip);
283 chip->desc = &gpio_desc[chip->base];
285 for (id = 0; id < chip->ngpio; id++) {
286 struct gpio_desc *desc = &chip->desc[id];
289 /* REVISIT: most hardware initializes GPIOs as
290 * inputs (often with pullups enabled) so power
291 * usage is minimized. Linux code should set the
292 * gpio direction first thing; but until it does,
293 * and in case chip->get_direction is not set,
294 * we may expose the wrong direction in sysfs.
296 desc->flags = !chip->direction_input
302 spin_unlock_irqrestore(&gpio_lock, flags);
304 #ifdef CONFIG_PINCTRL
305 INIT_LIST_HEAD(&chip->pin_ranges);
308 of_gpiochip_add(chip);
309 acpi_gpiochip_add(chip);
314 status = gpiochip_export(chip);
318 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
319 chip->base, chip->base + chip->ngpio - 1,
320 chip->label ? : "generic");
325 spin_unlock_irqrestore(&gpio_lock, flags);
327 /* failures here can mean systems won't boot... */
328 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
329 chip->base, chip->base + chip->ngpio - 1,
330 chip->label ? : "generic");
333 EXPORT_SYMBOL_GPL(gpiochip_add);
335 /* Forward-declaration */
336 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
339 * gpiochip_remove() - unregister a gpio_chip
340 * @chip: the chip to unregister
342 * A gpio_chip with any GPIOs still requested may not be removed.
344 int gpiochip_remove(struct gpio_chip *chip)
350 acpi_gpiochip_remove(chip);
352 spin_lock_irqsave(&gpio_lock, flags);
354 gpiochip_irqchip_remove(chip);
355 gpiochip_remove_pin_ranges(chip);
356 of_gpiochip_remove(chip);
358 for (id = 0; id < chip->ngpio; id++) {
359 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
365 for (id = 0; id < chip->ngpio; id++)
366 chip->desc[id].chip = NULL;
368 list_del(&chip->list);
371 spin_unlock_irqrestore(&gpio_lock, flags);
374 gpiochip_unexport(chip);
378 EXPORT_SYMBOL_GPL(gpiochip_remove);
381 * gpiochip_find() - iterator for locating a specific gpio_chip
382 * @data: data to pass to match function
383 * @callback: Callback function to check gpio_chip
385 * Similar to bus_find_device. It returns a reference to a gpio_chip as
386 * determined by a user supplied @match callback. The callback should return
387 * 0 if the device doesn't match and non-zero if it does. If the callback is
388 * non-zero, this function will return to the caller and not iterate over any
391 struct gpio_chip *gpiochip_find(void *data,
392 int (*match)(struct gpio_chip *chip,
395 struct gpio_chip *chip;
398 spin_lock_irqsave(&gpio_lock, flags);
399 list_for_each_entry(chip, &gpio_chips, list)
400 if (match(chip, data))
404 if (&chip->list == &gpio_chips)
406 spin_unlock_irqrestore(&gpio_lock, flags);
410 EXPORT_SYMBOL_GPL(gpiochip_find);
412 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
414 const char *name = data;
416 return !strcmp(chip->label, name);
419 static struct gpio_chip *find_chip_by_name(const char *name)
421 return gpiochip_find((void *)name, gpiochip_match_name);
424 #ifdef CONFIG_GPIOLIB_IRQCHIP
427 * The following is irqchip helper code for gpiochips.
431 * gpiochip_add_chained_irqchip() - adds a chained irqchip to a gpiochip
432 * @gpiochip: the gpiochip to add the irqchip to
433 * @irqchip: the irqchip to add to the gpiochip
434 * @parent_irq: the irq number corresponding to the parent IRQ for this
436 * @parent_handler: the parent interrupt handler for the accumulated IRQ
437 * coming out of the gpiochip
439 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
440 struct irq_chip *irqchip,
442 irq_flow_handler_t parent_handler)
444 if (gpiochip->can_sleep) {
445 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
449 irq_set_chained_handler(parent_irq, parent_handler);
451 * The parent irqchip is already using the chip_data for this
452 * irqchip, so our callbacks simply use the handler_data.
454 irq_set_handler_data(parent_irq, gpiochip);
456 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
459 * This lock class tells lockdep that GPIO irqs are in a different
460 * category than their parents, so it won't report false recursion.
462 static struct lock_class_key gpiochip_irq_lock_class;
465 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
466 * @d: the irqdomain used by this irqchip
467 * @irq: the global irq number used by this GPIO irqchip irq
468 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
470 * This function will set up the mapping for a certain IRQ line on a
471 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
472 * stored inside the gpiochip.
474 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
475 irq_hw_number_t hwirq)
477 struct gpio_chip *chip = d->host_data;
479 irq_set_chip_data(irq, chip);
480 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
481 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
482 /* Chips that can sleep need nested thread handlers */
484 irq_set_nested_thread(irq, 1);
486 set_irq_flags(irq, IRQF_VALID);
488 irq_set_noprobe(irq);
491 * No set-up of the hardware will happen if IRQ_TYPE_NONE
492 * is passed as default type.
494 if (chip->irq_default_type != IRQ_TYPE_NONE)
495 irq_set_irq_type(irq, chip->irq_default_type);
500 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
502 struct gpio_chip *chip = d->host_data;
505 set_irq_flags(irq, 0);
508 irq_set_nested_thread(irq, 0);
509 irq_set_chip_and_handler(irq, NULL, NULL);
510 irq_set_chip_data(irq, NULL);
513 static const struct irq_domain_ops gpiochip_domain_ops = {
514 .map = gpiochip_irq_map,
515 .unmap = gpiochip_irq_unmap,
516 /* Virtually all GPIO irqchips are twocell:ed */
517 .xlate = irq_domain_xlate_twocell,
520 static int gpiochip_irq_reqres(struct irq_data *d)
522 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
524 if (gpio_lock_as_irq(chip, d->hwirq)) {
526 "unable to lock HW IRQ %lu for IRQ\n",
533 static void gpiochip_irq_relres(struct irq_data *d)
535 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
537 gpio_unlock_as_irq(chip, d->hwirq);
540 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
542 return irq_find_mapping(chip->irqdomain, offset);
546 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
547 * @gpiochip: the gpiochip to remove the irqchip from
549 * This is called only from gpiochip_remove()
551 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
555 /* Remove all IRQ mappings and delete the domain */
556 if (gpiochip->irqdomain) {
557 for (offset = 0; offset < gpiochip->ngpio; offset++)
558 irq_dispose_mapping(gpiochip->irq_base + offset);
559 irq_domain_remove(gpiochip->irqdomain);
562 if (gpiochip->irqchip) {
563 gpiochip->irqchip->irq_request_resources = NULL;
564 gpiochip->irqchip->irq_release_resources = NULL;
565 gpiochip->irqchip = NULL;
570 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
571 * @gpiochip: the gpiochip to add the irqchip to
572 * @irqchip: the irqchip to add to the gpiochip
573 * @first_irq: if not dynamically assigned, the base (first) IRQ to
574 * allocate gpiochip irqs from
575 * @handler: the irq handler to use (often a predefined irq core function)
576 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
577 * to have the core avoid setting up any default type in the hardware.
579 * This function closely associates a certain irqchip with a certain
580 * gpiochip, providing an irq domain to translate the local IRQs to
581 * global irqs in the gpiolib core, and making sure that the gpiochip
582 * is passed as chip data to all related functions. Driver callbacks
583 * need to use container_of() to get their local state containers back
584 * from the gpiochip passed as chip data. An irqdomain will be stored
585 * in the gpiochip that shall be used by the driver to handle IRQ number
586 * translation. The gpiochip will need to be initialized and registered
587 * before calling this function.
589 * This function will handle two cell:ed simple IRQs and assumes all
590 * the pins on the gpiochip can generate a unique IRQ. Everything else
591 * need to be open coded.
593 int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
594 struct irq_chip *irqchip,
595 unsigned int first_irq,
596 irq_flow_handler_t handler,
599 struct device_node *of_node;
601 unsigned irq_base = 0;
603 if (!gpiochip || !irqchip)
606 if (!gpiochip->dev) {
607 pr_err("missing gpiochip .dev parent pointer\n");
610 of_node = gpiochip->dev->of_node;
611 #ifdef CONFIG_OF_GPIO
613 * If the gpiochip has an assigned OF node this takes precendence
614 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
616 if (gpiochip->of_node)
617 of_node = gpiochip->of_node;
619 gpiochip->irqchip = irqchip;
620 gpiochip->irq_handler = handler;
621 gpiochip->irq_default_type = type;
622 gpiochip->to_irq = gpiochip_to_irq;
623 gpiochip->irqdomain = irq_domain_add_simple(of_node,
624 gpiochip->ngpio, first_irq,
625 &gpiochip_domain_ops, gpiochip);
626 if (!gpiochip->irqdomain) {
627 gpiochip->irqchip = NULL;
630 irqchip->irq_request_resources = gpiochip_irq_reqres;
631 irqchip->irq_release_resources = gpiochip_irq_relres;
634 * Prepare the mapping since the irqchip shall be orthogonal to
635 * any gpiochip calls. If the first_irq was zero, this is
636 * necessary to allocate descriptors for all IRQs.
638 for (offset = 0; offset < gpiochip->ngpio; offset++) {
639 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
642 * Store the base into the gpiochip to be used when
643 * unmapping the irqs.
645 gpiochip->irq_base = irq_base;
650 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
652 #else /* CONFIG_GPIOLIB_IRQCHIP */
654 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
656 #endif /* CONFIG_GPIOLIB_IRQCHIP */
658 #ifdef CONFIG_PINCTRL
661 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
662 * @chip: the gpiochip to add the range for
663 * @pinctrl: the dev_name() of the pin controller to map to
664 * @gpio_offset: the start offset in the current gpio_chip number space
665 * @pin_group: name of the pin group inside the pin controller
667 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
668 struct pinctrl_dev *pctldev,
669 unsigned int gpio_offset, const char *pin_group)
671 struct gpio_pin_range *pin_range;
674 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
676 chip_err(chip, "failed to allocate pin ranges\n");
680 /* Use local offset as range ID */
681 pin_range->range.id = gpio_offset;
682 pin_range->range.gc = chip;
683 pin_range->range.name = chip->label;
684 pin_range->range.base = chip->base + gpio_offset;
685 pin_range->pctldev = pctldev;
687 ret = pinctrl_get_group_pins(pctldev, pin_group,
688 &pin_range->range.pins,
689 &pin_range->range.npins);
695 pinctrl_add_gpio_range(pctldev, &pin_range->range);
697 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
698 gpio_offset, gpio_offset + pin_range->range.npins - 1,
699 pinctrl_dev_get_devname(pctldev), pin_group);
701 list_add_tail(&pin_range->node, &chip->pin_ranges);
705 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
708 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
709 * @chip: the gpiochip to add the range for
710 * @pinctrl_name: the dev_name() of the pin controller to map to
711 * @gpio_offset: the start offset in the current gpio_chip number space
712 * @pin_offset: the start offset in the pin controller number space
713 * @npins: the number of pins from the offset of each pin space (GPIO and
714 * pin controller) to accumulate in this range
716 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
717 unsigned int gpio_offset, unsigned int pin_offset,
720 struct gpio_pin_range *pin_range;
723 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
725 chip_err(chip, "failed to allocate pin ranges\n");
729 /* Use local offset as range ID */
730 pin_range->range.id = gpio_offset;
731 pin_range->range.gc = chip;
732 pin_range->range.name = chip->label;
733 pin_range->range.base = chip->base + gpio_offset;
734 pin_range->range.pin_base = pin_offset;
735 pin_range->range.npins = npins;
736 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
738 if (IS_ERR(pin_range->pctldev)) {
739 ret = PTR_ERR(pin_range->pctldev);
740 chip_err(chip, "could not create pin range\n");
744 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
745 gpio_offset, gpio_offset + npins - 1,
747 pin_offset, pin_offset + npins - 1);
749 list_add_tail(&pin_range->node, &chip->pin_ranges);
753 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
756 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
757 * @chip: the chip to remove all the mappings for
759 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
761 struct gpio_pin_range *pin_range, *tmp;
763 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
764 list_del(&pin_range->node);
765 pinctrl_remove_gpio_range(pin_range->pctldev,
770 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
772 #endif /* CONFIG_PINCTRL */
774 /* These "optional" allocation calls help prevent drivers from stomping
775 * on each other, and help provide better diagnostics in debugfs.
776 * They're called even less than the "set direction" calls.
778 static int __gpiod_request(struct gpio_desc *desc, const char *label)
780 struct gpio_chip *chip = desc->chip;
784 spin_lock_irqsave(&gpio_lock, flags);
786 /* NOTE: gpio_request() can be called in early boot,
787 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
790 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
791 desc_set_label(desc, label ? : "?");
799 /* chip->request may sleep */
800 spin_unlock_irqrestore(&gpio_lock, flags);
801 status = chip->request(chip, gpio_chip_hwgpio(desc));
802 spin_lock_irqsave(&gpio_lock, flags);
805 desc_set_label(desc, NULL);
806 clear_bit(FLAG_REQUESTED, &desc->flags);
810 if (chip->get_direction) {
811 /* chip->get_direction may sleep */
812 spin_unlock_irqrestore(&gpio_lock, flags);
813 gpiod_get_direction(desc);
814 spin_lock_irqsave(&gpio_lock, flags);
817 spin_unlock_irqrestore(&gpio_lock, flags);
821 int gpiod_request(struct gpio_desc *desc, const char *label)
823 int status = -EPROBE_DEFER;
824 struct gpio_chip *chip;
827 pr_warn("%s: invalid GPIO\n", __func__);
835 if (try_module_get(chip->owner)) {
836 status = __gpiod_request(desc, label);
838 module_put(chip->owner);
843 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
848 int gpio_request(unsigned gpio, const char *label)
850 return gpiod_request(gpio_to_desc(gpio), label);
852 EXPORT_SYMBOL_GPL(gpio_request);
854 static bool __gpiod_free(struct gpio_desc *desc)
858 struct gpio_chip *chip;
862 gpiod_unexport(desc);
864 spin_lock_irqsave(&gpio_lock, flags);
867 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
869 spin_unlock_irqrestore(&gpio_lock, flags);
870 might_sleep_if(chip->can_sleep);
871 chip->free(chip, gpio_chip_hwgpio(desc));
872 spin_lock_irqsave(&gpio_lock, flags);
874 desc_set_label(desc, NULL);
875 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
876 clear_bit(FLAG_REQUESTED, &desc->flags);
877 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
878 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
882 spin_unlock_irqrestore(&gpio_lock, flags);
886 void gpiod_free(struct gpio_desc *desc)
888 if (desc && __gpiod_free(desc))
889 module_put(desc->chip->owner);
891 WARN_ON(extra_checks);
894 void gpio_free(unsigned gpio)
896 gpiod_free(gpio_to_desc(gpio));
898 EXPORT_SYMBOL_GPL(gpio_free);
901 * gpio_request_one - request a single GPIO with initial configuration
902 * @gpio: the GPIO number
903 * @flags: GPIO configuration as specified by GPIOF_*
904 * @label: a literal description string of this GPIO
906 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
908 struct gpio_desc *desc;
911 desc = gpio_to_desc(gpio);
913 err = gpiod_request(desc, label);
917 if (flags & GPIOF_OPEN_DRAIN)
918 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
920 if (flags & GPIOF_OPEN_SOURCE)
921 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
923 if (flags & GPIOF_DIR_IN)
924 err = gpiod_direction_input(desc);
926 err = gpiod_direction_output_raw(desc,
927 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
932 if (flags & GPIOF_EXPORT) {
933 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
944 EXPORT_SYMBOL_GPL(gpio_request_one);
947 * gpio_request_array - request multiple GPIOs in a single call
948 * @array: array of the 'struct gpio'
949 * @num: how many GPIOs in the array
951 int gpio_request_array(const struct gpio *array, size_t num)
955 for (i = 0; i < num; i++, array++) {
956 err = gpio_request_one(array->gpio, array->flags, array->label);
964 gpio_free((--array)->gpio);
967 EXPORT_SYMBOL_GPL(gpio_request_array);
970 * gpio_free_array - release multiple GPIOs in a single call
971 * @array: array of the 'struct gpio'
972 * @num: how many GPIOs in the array
974 void gpio_free_array(const struct gpio *array, size_t num)
977 gpio_free((array++)->gpio);
979 EXPORT_SYMBOL_GPL(gpio_free_array);
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 int gpiochip_request_own_desc(struct gpio_desc *desc, const char *label)
1022 if (!desc || !desc->chip)
1025 return __gpiod_request(desc, label);
1029 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1030 * @desc: GPIO descriptor to free
1032 * Function frees the given GPIO requested previously with
1033 * gpiochip_request_own_desc().
1035 void gpiochip_free_own_desc(struct gpio_desc *desc)
1041 /* Drivers MUST set GPIO direction before making get/set calls. In
1042 * some cases this is done in early boot, before IRQs are enabled.
1044 * As a rule these aren't called more than once (except for drivers
1045 * using the open-drain emulation idiom) so these are natural places
1046 * to accumulate extra debugging checks. Note that we can't (yet)
1047 * rely on gpio_request() having been called beforehand.
1051 * gpiod_direction_input - set the GPIO direction to input
1052 * @desc: GPIO to set to input
1054 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1055 * be called safely on it.
1057 * Return 0 in case of success, else an error code.
1059 int gpiod_direction_input(struct gpio_desc *desc)
1061 unsigned long flags;
1062 struct gpio_chip *chip;
1063 int status = -EINVAL;
1066 if (!desc || !desc->chip) {
1067 pr_warn("%s: invalid GPIO\n", __func__);
1072 if (!chip->get || !chip->direction_input) {
1074 "%s: missing get() or direction_input() operations\n",
1079 spin_lock_irqsave(&gpio_lock, flags);
1081 status = gpio_ensure_requested(desc);
1085 /* now we know the gpio is valid and chip won't vanish */
1087 spin_unlock_irqrestore(&gpio_lock, flags);
1089 might_sleep_if(chip->can_sleep);
1091 offset = gpio_chip_hwgpio(desc);
1093 status = chip->request(chip, offset);
1095 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1097 /* and it's not available to anyone else ...
1098 * gpio_request() is the fully clean solution.
1104 status = chip->direction_input(chip, offset);
1106 clear_bit(FLAG_IS_OUT, &desc->flags);
1108 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1112 spin_unlock_irqrestore(&gpio_lock, flags);
1114 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1117 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1119 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1121 unsigned long flags;
1122 struct gpio_chip *chip;
1123 int status = -EINVAL;
1126 /* GPIOs used for IRQs shall not be set as output */
1127 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1129 "%s: tried to set a GPIO tied to an IRQ as output\n",
1134 /* Open drain pin should not be driven to 1 */
1135 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1136 return gpiod_direction_input(desc);
1138 /* Open source pin should not be driven to 0 */
1139 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1140 return gpiod_direction_input(desc);
1143 if (!chip->set || !chip->direction_output) {
1145 "%s: missing set() or direction_output() operations\n",
1150 spin_lock_irqsave(&gpio_lock, flags);
1152 status = gpio_ensure_requested(desc);
1156 /* now we know the gpio is valid and chip won't vanish */
1158 spin_unlock_irqrestore(&gpio_lock, flags);
1160 might_sleep_if(chip->can_sleep);
1162 offset = gpio_chip_hwgpio(desc);
1164 status = chip->request(chip, offset);
1166 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1168 /* and it's not available to anyone else ...
1169 * gpio_request() is the fully clean solution.
1175 status = chip->direction_output(chip, offset, value);
1177 set_bit(FLAG_IS_OUT, &desc->flags);
1178 trace_gpio_value(desc_to_gpio(desc), 0, value);
1179 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1183 spin_unlock_irqrestore(&gpio_lock, flags);
1185 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
1190 * gpiod_direction_output_raw - set the GPIO direction to output
1191 * @desc: GPIO to set to output
1192 * @value: initial output value of the GPIO
1194 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1195 * be called safely on it. The initial value of the output must be specified
1196 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1198 * Return 0 in case of success, else an error code.
1200 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1202 if (!desc || !desc->chip) {
1203 pr_warn("%s: invalid GPIO\n", __func__);
1206 return _gpiod_direction_output_raw(desc, value);
1208 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1211 * gpiod_direction_output - set the GPIO direction to output
1212 * @desc: GPIO to set to output
1213 * @value: initial output value of the GPIO
1215 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1216 * be called safely on it. The initial value of the output must be specified
1217 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1220 * Return 0 in case of success, else an error code.
1222 int gpiod_direction_output(struct gpio_desc *desc, int value)
1224 if (!desc || !desc->chip) {
1225 pr_warn("%s: invalid GPIO\n", __func__);
1228 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1230 return _gpiod_direction_output_raw(desc, value);
1232 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1235 * gpiod_set_debounce - sets @debounce time for a @gpio
1236 * @gpio: the gpio to set debounce time
1237 * @debounce: debounce time is microseconds
1239 * returns -ENOTSUPP if the controller does not support setting
1242 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1244 unsigned long flags;
1245 struct gpio_chip *chip;
1246 int status = -EINVAL;
1249 if (!desc || !desc->chip) {
1250 pr_warn("%s: invalid GPIO\n", __func__);
1255 if (!chip->set || !chip->set_debounce) {
1257 "%s: missing set() or set_debounce() operations\n",
1262 spin_lock_irqsave(&gpio_lock, flags);
1264 status = gpio_ensure_requested(desc);
1268 /* now we know the gpio is valid and chip won't vanish */
1270 spin_unlock_irqrestore(&gpio_lock, flags);
1272 might_sleep_if(chip->can_sleep);
1274 offset = gpio_chip_hwgpio(desc);
1275 return chip->set_debounce(chip, offset, debounce);
1278 spin_unlock_irqrestore(&gpio_lock, flags);
1280 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1284 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1287 * gpiod_is_active_low - test whether a GPIO is active-low or not
1288 * @desc: the gpio descriptor to test
1290 * Returns 1 if the GPIO is active-low, 0 otherwise.
1292 int gpiod_is_active_low(const struct gpio_desc *desc)
1294 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1296 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1298 /* I/O calls are only valid after configuration completed; the relevant
1299 * "is this a valid GPIO" error checks should already have been done.
1301 * "Get" operations are often inlinable as reading a pin value register,
1302 * and masking the relevant bit in that register.
1304 * When "set" operations are inlinable, they involve writing that mask to
1305 * one register to set a low value, or a different register to set it high.
1306 * Otherwise locking is needed, so there may be little value to inlining.
1308 *------------------------------------------------------------------------
1310 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1311 * have requested the GPIO. That can include implicit requesting by
1312 * a direction setting call. Marking a gpio as requested locks its chip
1313 * in memory, guaranteeing that these table lookups need no more locking
1314 * and that gpiochip_remove() will fail.
1316 * REVISIT when debugging, consider adding some instrumentation to ensure
1317 * that the GPIO was actually requested.
1320 static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
1322 struct gpio_chip *chip;
1327 offset = gpio_chip_hwgpio(desc);
1328 value = chip->get ? chip->get(chip, offset) : false;
1329 trace_gpio_value(desc_to_gpio(desc), 1, value);
1334 * gpiod_get_raw_value() - return a gpio's raw value
1335 * @desc: gpio whose value will be returned
1337 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1338 * its ACTIVE_LOW status.
1340 * This function should be called from contexts where we cannot sleep, and will
1341 * complain if the GPIO chip functions potentially sleep.
1343 int gpiod_get_raw_value(const struct gpio_desc *desc)
1347 /* Should be using gpio_get_value_cansleep() */
1348 WARN_ON(desc->chip->can_sleep);
1349 return _gpiod_get_raw_value(desc);
1351 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1354 * gpiod_get_value() - return a gpio's value
1355 * @desc: gpio whose value will be returned
1357 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1360 * This function should be called from contexts where we cannot sleep, and will
1361 * complain if the GPIO chip functions potentially sleep.
1363 int gpiod_get_value(const struct gpio_desc *desc)
1368 /* Should be using gpio_get_value_cansleep() */
1369 WARN_ON(desc->chip->can_sleep);
1371 value = _gpiod_get_raw_value(desc);
1372 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1377 EXPORT_SYMBOL_GPL(gpiod_get_value);
1380 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1381 * @desc: gpio descriptor whose state need to be set.
1382 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1384 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1387 struct gpio_chip *chip = desc->chip;
1388 int offset = gpio_chip_hwgpio(desc);
1391 err = chip->direction_input(chip, offset);
1393 clear_bit(FLAG_IS_OUT, &desc->flags);
1395 err = chip->direction_output(chip, offset, 0);
1397 set_bit(FLAG_IS_OUT, &desc->flags);
1399 trace_gpio_direction(desc_to_gpio(desc), value, err);
1402 "%s: Error in set_value for open drain err %d\n",
1407 * _gpio_set_open_source_value() - Set the open source gpio's value.
1408 * @desc: gpio descriptor whose state need to be set.
1409 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1411 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1414 struct gpio_chip *chip = desc->chip;
1415 int offset = gpio_chip_hwgpio(desc);
1418 err = chip->direction_output(chip, offset, 1);
1420 set_bit(FLAG_IS_OUT, &desc->flags);
1422 err = chip->direction_input(chip, offset);
1424 clear_bit(FLAG_IS_OUT, &desc->flags);
1426 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1429 "%s: Error in set_value for open source err %d\n",
1433 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1435 struct gpio_chip *chip;
1438 trace_gpio_value(desc_to_gpio(desc), 0, value);
1439 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1440 _gpio_set_open_drain_value(desc, value);
1441 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1442 _gpio_set_open_source_value(desc, value);
1444 chip->set(chip, gpio_chip_hwgpio(desc), value);
1448 * gpiod_set_raw_value() - assign a gpio's raw value
1449 * @desc: gpio whose value will be assigned
1450 * @value: value to assign
1452 * Set the raw value of the GPIO, i.e. the value of its physical line without
1453 * regard for its ACTIVE_LOW status.
1455 * This function should be called from contexts where we cannot sleep, and will
1456 * complain if the GPIO chip functions potentially sleep.
1458 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1462 /* Should be using gpio_set_value_cansleep() */
1463 WARN_ON(desc->chip->can_sleep);
1464 _gpiod_set_raw_value(desc, value);
1466 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1469 * gpiod_set_value() - assign a gpio's value
1470 * @desc: gpio whose value will be assigned
1471 * @value: value to assign
1473 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1476 * This function should be called from contexts where we cannot sleep, and will
1477 * complain if the GPIO chip functions potentially sleep.
1479 void gpiod_set_value(struct gpio_desc *desc, int value)
1483 /* Should be using gpio_set_value_cansleep() */
1484 WARN_ON(desc->chip->can_sleep);
1485 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1487 _gpiod_set_raw_value(desc, value);
1489 EXPORT_SYMBOL_GPL(gpiod_set_value);
1492 * gpiod_cansleep() - report whether gpio value access may sleep
1493 * @desc: gpio to check
1496 int gpiod_cansleep(const struct gpio_desc *desc)
1500 return desc->chip->can_sleep;
1502 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1505 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1506 * @desc: gpio whose IRQ will be returned (already requested)
1508 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1511 int gpiod_to_irq(const struct gpio_desc *desc)
1513 struct gpio_chip *chip;
1519 offset = gpio_chip_hwgpio(desc);
1520 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1522 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1525 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
1526 * @gpio: the GPIO line to lock as used for IRQ
1528 * This is used directly by GPIO drivers that want to lock down
1529 * a certain GPIO line to be used for IRQs.
1531 int gpiod_lock_as_irq(struct gpio_desc *desc)
1536 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
1538 "%s: tried to flag a GPIO set as output for IRQ\n",
1543 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
1546 EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
1548 int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1550 return gpiod_lock_as_irq(gpiochip_get_desc(chip, offset));
1552 EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
1555 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
1556 * @gpio: the GPIO line to unlock from IRQ usage
1558 * This is used directly by GPIO drivers that want to indicate
1559 * that a certain GPIO is no longer used exclusively for IRQ.
1561 void gpiod_unlock_as_irq(struct gpio_desc *desc)
1566 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
1568 EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
1570 void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1572 return gpiod_unlock_as_irq(gpiochip_get_desc(chip, offset));
1574 EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
1577 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1578 * @desc: gpio whose value will be returned
1580 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1581 * its ACTIVE_LOW status.
1583 * This function is to be called from contexts that can sleep.
1585 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1587 might_sleep_if(extra_checks);
1590 return _gpiod_get_raw_value(desc);
1592 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1595 * gpiod_get_value_cansleep() - return a gpio's value
1596 * @desc: gpio whose value will be returned
1598 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1601 * This function is to be called from contexts that can sleep.
1603 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1607 might_sleep_if(extra_checks);
1611 value = _gpiod_get_raw_value(desc);
1612 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1617 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
1620 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1621 * @desc: gpio whose value will be assigned
1622 * @value: value to assign
1624 * Set the raw value of the GPIO, i.e. the value of its physical line without
1625 * regard for its ACTIVE_LOW status.
1627 * This function is to be called from contexts that can sleep.
1629 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
1631 might_sleep_if(extra_checks);
1634 _gpiod_set_raw_value(desc, value);
1636 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
1639 * gpiod_set_value_cansleep() - assign a gpio's value
1640 * @desc: gpio whose value will be assigned
1641 * @value: value to assign
1643 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1646 * This function is to be called from contexts that can sleep.
1648 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1650 might_sleep_if(extra_checks);
1654 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1656 _gpiod_set_raw_value(desc, value);
1658 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1661 * gpiod_add_lookup_table() - register GPIO device consumers
1662 * @table: table of consumers to register
1664 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
1666 mutex_lock(&gpio_lookup_lock);
1668 list_add_tail(&table->list, &gpio_lookup_list);
1670 mutex_unlock(&gpio_lookup_lock);
1673 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
1675 enum gpio_lookup_flags *flags)
1677 static const char *suffixes[] = { "gpios", "gpio" };
1678 char prop_name[32]; /* 32 is max size of property name */
1679 enum of_gpio_flags of_flags;
1680 struct gpio_desc *desc;
1683 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1685 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1687 snprintf(prop_name, 32, "%s", suffixes[i]);
1689 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1691 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1698 if (of_flags & OF_GPIO_ACTIVE_LOW)
1699 *flags |= GPIO_ACTIVE_LOW;
1704 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1706 enum gpio_lookup_flags *flags)
1708 struct acpi_gpio_info info;
1709 struct gpio_desc *desc;
1711 desc = acpi_get_gpiod_by_index(dev, idx, &info);
1715 if (info.gpioint && info.active_low)
1716 *flags |= GPIO_ACTIVE_LOW;
1721 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1723 const char *dev_id = dev ? dev_name(dev) : NULL;
1724 struct gpiod_lookup_table *table;
1726 mutex_lock(&gpio_lookup_lock);
1728 list_for_each_entry(table, &gpio_lookup_list, list) {
1729 if (table->dev_id && dev_id) {
1731 * Valid strings on both ends, must be identical to have
1734 if (!strcmp(table->dev_id, dev_id))
1738 * One of the pointers is NULL, so both must be to have
1741 if (dev_id == table->dev_id)
1748 mutex_unlock(&gpio_lookup_lock);
1752 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1754 enum gpio_lookup_flags *flags)
1756 struct gpio_desc *desc = ERR_PTR(-ENOENT);
1757 struct gpiod_lookup_table *table;
1758 struct gpiod_lookup *p;
1760 table = gpiod_find_lookup_table(dev);
1764 for (p = &table->table[0]; p->chip_label; p++) {
1765 struct gpio_chip *chip;
1767 /* idx must always match exactly */
1771 /* If the lookup entry has a con_id, require exact match */
1772 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1775 chip = find_chip_by_name(p->chip_label);
1778 dev_err(dev, "cannot find GPIO chip %s\n",
1780 return ERR_PTR(-ENODEV);
1783 if (chip->ngpio <= p->chip_hwnum) {
1785 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1786 idx, chip->ngpio, chip->label);
1787 return ERR_PTR(-EINVAL);
1790 desc = gpiochip_get_desc(chip, p->chip_hwnum);
1800 * gpiod_get - obtain a GPIO for a given GPIO function
1801 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1802 * @con_id: function within the GPIO consumer
1804 * Return the GPIO descriptor corresponding to the function con_id of device
1805 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1806 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
1808 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
1810 return gpiod_get_index(dev, con_id, 0);
1812 EXPORT_SYMBOL_GPL(gpiod_get);
1815 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1816 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1817 * @con_id: function within the GPIO consumer
1819 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1820 * the requested function it will return NULL. This is convenient for drivers
1821 * that need to handle optional GPIOs.
1823 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
1826 return gpiod_get_index_optional(dev, con_id, 0);
1828 EXPORT_SYMBOL_GPL(gpiod_get_optional);
1831 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
1832 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1833 * @con_id: function within the GPIO consumer
1834 * @idx: index of the GPIO to obtain in the consumer
1836 * This variant of gpiod_get() allows to access GPIOs other than the first
1837 * defined one for functions that define several GPIOs.
1839 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1840 * requested function and/or index, or another IS_ERR() code if an error
1841 * occured while trying to acquire the GPIO.
1843 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
1847 struct gpio_desc *desc = NULL;
1849 enum gpio_lookup_flags flags = 0;
1851 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1853 /* Using device tree? */
1854 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1855 dev_dbg(dev, "using device tree for GPIO lookup\n");
1856 desc = of_find_gpio(dev, con_id, idx, &flags);
1857 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1858 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1859 desc = acpi_find_gpio(dev, con_id, idx, &flags);
1863 * Either we are not using DT or ACPI, or their lookup did not return
1864 * a result. In that case, use platform lookup as a fallback.
1866 if (!desc || desc == ERR_PTR(-ENOENT)) {
1867 dev_dbg(dev, "using lookup tables for GPIO lookup");
1868 desc = gpiod_find(dev, con_id, idx, &flags);
1872 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
1876 status = gpiod_request(desc, con_id);
1879 return ERR_PTR(status);
1881 if (flags & GPIO_ACTIVE_LOW)
1882 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1883 if (flags & GPIO_OPEN_DRAIN)
1884 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1885 if (flags & GPIO_OPEN_SOURCE)
1886 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1890 EXPORT_SYMBOL_GPL(gpiod_get_index);
1893 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1895 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1896 * @con_id: function within the GPIO consumer
1897 * @index: index of the GPIO to obtain in the consumer
1899 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1900 * specified index was assigned to the requested function it will return NULL.
1901 * This is convenient for drivers that need to handle optional GPIOs.
1903 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
1907 struct gpio_desc *desc;
1909 desc = gpiod_get_index(dev, con_id, index);
1911 if (PTR_ERR(desc) == -ENOENT)
1917 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
1920 * gpiod_put - dispose of a GPIO descriptor
1921 * @desc: GPIO descriptor to dispose of
1923 * No descriptor can be used after gpiod_put() has been called on it.
1925 void gpiod_put(struct gpio_desc *desc)
1929 EXPORT_SYMBOL_GPL(gpiod_put);
1931 #ifdef CONFIG_DEBUG_FS
1933 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1936 unsigned gpio = chip->base;
1937 struct gpio_desc *gdesc = &chip->desc[0];
1941 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1942 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1945 gpiod_get_direction(gdesc);
1946 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1947 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
1948 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
1950 is_out ? "out" : "in ",
1952 ? (chip->get(chip, i) ? "hi" : "lo")
1954 is_irq ? "IRQ" : " ");
1955 seq_printf(s, "\n");
1959 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
1961 unsigned long flags;
1962 struct gpio_chip *chip = NULL;
1963 loff_t index = *pos;
1967 spin_lock_irqsave(&gpio_lock, flags);
1968 list_for_each_entry(chip, &gpio_chips, list)
1970 spin_unlock_irqrestore(&gpio_lock, flags);
1973 spin_unlock_irqrestore(&gpio_lock, flags);
1978 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1980 unsigned long flags;
1981 struct gpio_chip *chip = v;
1984 spin_lock_irqsave(&gpio_lock, flags);
1985 if (list_is_last(&chip->list, &gpio_chips))
1988 ret = list_entry(chip->list.next, struct gpio_chip, list);
1989 spin_unlock_irqrestore(&gpio_lock, flags);
1997 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2001 static int gpiolib_seq_show(struct seq_file *s, void *v)
2003 struct gpio_chip *chip = v;
2006 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2007 chip->base, chip->base + chip->ngpio - 1);
2010 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2013 seq_printf(s, ", %s", chip->label);
2014 if (chip->can_sleep)
2015 seq_printf(s, ", can sleep");
2016 seq_printf(s, ":\n");
2019 chip->dbg_show(s, chip);
2021 gpiolib_dbg_show(s, chip);
2026 static const struct seq_operations gpiolib_seq_ops = {
2027 .start = gpiolib_seq_start,
2028 .next = gpiolib_seq_next,
2029 .stop = gpiolib_seq_stop,
2030 .show = gpiolib_seq_show,
2033 static int gpiolib_open(struct inode *inode, struct file *file)
2035 return seq_open(file, &gpiolib_seq_ops);
2038 static const struct file_operations gpiolib_operations = {
2039 .owner = THIS_MODULE,
2040 .open = gpiolib_open,
2042 .llseek = seq_lseek,
2043 .release = seq_release,
2046 static int __init gpiolib_debugfs_init(void)
2048 /* /sys/kernel/debug/gpio */
2049 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2050 NULL, NULL, &gpiolib_operations);
2053 subsys_initcall(gpiolib_debugfs_init);
2055 #endif /* DEBUG_FS */