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);
56 static inline void desc_set_label(struct gpio_desc *d, const char *label)
62 * Convert a GPIO number to its descriptor
64 struct gpio_desc *gpio_to_desc(unsigned gpio)
66 struct gpio_chip *chip;
69 spin_lock_irqsave(&gpio_lock, flags);
71 list_for_each_entry(chip, &gpio_chips, list) {
72 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
73 spin_unlock_irqrestore(&gpio_lock, flags);
74 return &chip->desc[gpio - chip->base];
78 spin_unlock_irqrestore(&gpio_lock, flags);
80 WARN(1, "invalid GPIO %d\n", gpio);
83 EXPORT_SYMBOL_GPL(gpio_to_desc);
86 * Get the GPIO descriptor corresponding to the given hw number for this chip.
88 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
91 if (hwnum >= chip->ngpio)
92 return ERR_PTR(-EINVAL);
94 return &chip->desc[hwnum];
98 * Convert a GPIO descriptor to the integer namespace.
99 * This should disappear in the future but is needed since we still
100 * use GPIO numbers for error messages and sysfs nodes
102 int desc_to_gpio(const struct gpio_desc *desc)
104 return desc->chip->base + (desc - &desc->chip->desc[0]);
106 EXPORT_SYMBOL_GPL(desc_to_gpio);
110 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
111 * @desc: descriptor to return the chip of
113 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
115 return desc ? desc->chip : NULL;
117 EXPORT_SYMBOL_GPL(gpiod_to_chip);
119 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
120 static int gpiochip_find_base(int ngpio)
122 struct gpio_chip *chip;
123 int base = ARCH_NR_GPIOS - ngpio;
125 list_for_each_entry_reverse(chip, &gpio_chips, list) {
126 /* found a free space? */
127 if (chip->base + chip->ngpio <= base)
130 /* nope, check the space right before the chip */
131 base = chip->base - ngpio;
134 if (gpio_is_valid(base)) {
135 pr_debug("%s: found new base at %d\n", __func__, base);
138 pr_err("%s: cannot find free range\n", __func__);
144 * gpiod_get_direction - return the current direction of a GPIO
145 * @desc: GPIO to get the direction of
147 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
149 * This function may sleep if gpiod_cansleep() is true.
151 int gpiod_get_direction(struct gpio_desc *desc)
153 struct gpio_chip *chip;
155 int status = -EINVAL;
157 chip = gpiod_to_chip(desc);
158 offset = gpio_chip_hwgpio(desc);
160 if (!chip->get_direction)
163 status = chip->get_direction(chip, offset);
165 /* GPIOF_DIR_IN, or other positive */
167 clear_bit(FLAG_IS_OUT, &desc->flags);
171 set_bit(FLAG_IS_OUT, &desc->flags);
175 EXPORT_SYMBOL_GPL(gpiod_get_direction);
178 * Add a new chip to the global chips list, keeping the list of chips sorted
181 * Return -EBUSY if the new chip overlaps with some other chip's integer
184 static int gpiochip_add_to_list(struct gpio_chip *chip)
186 struct list_head *pos = &gpio_chips;
187 struct gpio_chip *_chip;
190 /* find where to insert our chip */
191 list_for_each(pos, &gpio_chips) {
192 _chip = list_entry(pos, struct gpio_chip, list);
193 /* shall we insert before _chip? */
194 if (_chip->base >= chip->base + chip->ngpio)
198 /* are we stepping on the chip right before? */
199 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
200 _chip = list_entry(pos->prev, struct gpio_chip, list);
201 if (_chip->base + _chip->ngpio > chip->base) {
203 "GPIO integer space overlap, cannot add chip\n");
209 list_add_tail(&chip->list, pos);
215 * gpiochip_add() - register a gpio_chip
216 * @chip: the chip to register, with chip->base initialized
217 * Context: potentially before irqs will work
219 * Returns a negative errno if the chip can't be registered, such as
220 * because the chip->base is invalid or already associated with a
221 * different chip. Otherwise it returns zero as a success code.
223 * When gpiochip_add() is called very early during boot, so that GPIOs
224 * can be freely used, the chip->dev device must be registered before
225 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
226 * for GPIOs will fail rudely.
228 * If chip->base is negative, this requests dynamic assignment of
229 * a range of valid GPIOs.
231 int gpiochip_add(struct gpio_chip *chip)
236 int base = chip->base;
237 struct gpio_desc *descs;
239 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
243 spin_lock_irqsave(&gpio_lock, flags);
246 base = gpiochip_find_base(chip->ngpio);
254 status = gpiochip_add_to_list(chip);
257 for (id = 0; id < chip->ngpio; id++) {
258 struct gpio_desc *desc = &descs[id];
261 /* REVISIT: most hardware initializes GPIOs as
262 * inputs (often with pullups enabled) so power
263 * usage is minimized. Linux code should set the
264 * gpio direction first thing; but until it does,
265 * and in case chip->get_direction is not set,
266 * we may expose the wrong direction in sysfs.
268 desc->flags = !chip->direction_input
276 spin_unlock_irqrestore(&gpio_lock, flags);
278 #ifdef CONFIG_PINCTRL
279 INIT_LIST_HEAD(&chip->pin_ranges);
282 of_gpiochip_add(chip);
283 acpi_gpiochip_add(chip);
288 status = gpiochip_export(chip);
292 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
293 chip->base, chip->base + chip->ngpio - 1,
294 chip->label ? : "generic");
299 spin_unlock_irqrestore(&gpio_lock, flags);
304 /* failures here can mean systems won't boot... */
305 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
306 chip->base, chip->base + chip->ngpio - 1,
307 chip->label ? : "generic");
310 EXPORT_SYMBOL_GPL(gpiochip_add);
312 /* Forward-declaration */
313 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
316 * gpiochip_remove() - unregister a gpio_chip
317 * @chip: the chip to unregister
319 * A gpio_chip with any GPIOs still requested may not be removed.
321 void gpiochip_remove(struct gpio_chip *chip)
326 acpi_gpiochip_remove(chip);
328 spin_lock_irqsave(&gpio_lock, flags);
330 gpiochip_irqchip_remove(chip);
331 gpiochip_remove_pin_ranges(chip);
332 of_gpiochip_remove(chip);
334 for (id = 0; id < chip->ngpio; id++) {
335 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
336 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
338 for (id = 0; id < chip->ngpio; id++)
339 chip->desc[id].chip = NULL;
341 list_del(&chip->list);
342 spin_unlock_irqrestore(&gpio_lock, flags);
343 gpiochip_unexport(chip);
348 EXPORT_SYMBOL_GPL(gpiochip_remove);
351 * gpiochip_find() - iterator for locating a specific gpio_chip
352 * @data: data to pass to match function
353 * @callback: Callback function to check gpio_chip
355 * Similar to bus_find_device. It returns a reference to a gpio_chip as
356 * determined by a user supplied @match callback. The callback should return
357 * 0 if the device doesn't match and non-zero if it does. If the callback is
358 * non-zero, this function will return to the caller and not iterate over any
361 struct gpio_chip *gpiochip_find(void *data,
362 int (*match)(struct gpio_chip *chip,
365 struct gpio_chip *chip;
368 spin_lock_irqsave(&gpio_lock, flags);
369 list_for_each_entry(chip, &gpio_chips, list)
370 if (match(chip, data))
374 if (&chip->list == &gpio_chips)
376 spin_unlock_irqrestore(&gpio_lock, flags);
380 EXPORT_SYMBOL_GPL(gpiochip_find);
382 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
384 const char *name = data;
386 return !strcmp(chip->label, name);
389 static struct gpio_chip *find_chip_by_name(const char *name)
391 return gpiochip_find((void *)name, gpiochip_match_name);
394 #ifdef CONFIG_GPIOLIB_IRQCHIP
397 * The following is irqchip helper code for gpiochips.
401 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
402 * @gpiochip: the gpiochip to set the irqchip chain to
403 * @irqchip: the irqchip to chain to the gpiochip
404 * @parent_irq: the irq number corresponding to the parent IRQ for this
406 * @parent_handler: the parent interrupt handler for the accumulated IRQ
407 * coming out of the gpiochip. If the interrupt is nested rather than
408 * cascaded, pass NULL in this handler argument
410 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
411 struct irq_chip *irqchip,
413 irq_flow_handler_t parent_handler)
417 if (!gpiochip->irqdomain) {
418 chip_err(gpiochip, "called %s before setting up irqchip\n",
423 if (parent_handler) {
424 if (gpiochip->can_sleep) {
426 "you cannot have chained interrupts on a "
427 "chip that may sleep\n");
431 * The parent irqchip is already using the chip_data for this
432 * irqchip, so our callbacks simply use the handler_data.
434 irq_set_handler_data(parent_irq, gpiochip);
435 irq_set_chained_handler(parent_irq, parent_handler);
438 /* Set the parent IRQ for all affected IRQs */
439 for (offset = 0; offset < gpiochip->ngpio; offset++)
440 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
443 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
446 * This lock class tells lockdep that GPIO irqs are in a different
447 * category than their parents, so it won't report false recursion.
449 static struct lock_class_key gpiochip_irq_lock_class;
452 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
453 * @d: the irqdomain used by this irqchip
454 * @irq: the global irq number used by this GPIO irqchip irq
455 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
457 * This function will set up the mapping for a certain IRQ line on a
458 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
459 * stored inside the gpiochip.
461 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
462 irq_hw_number_t hwirq)
464 struct gpio_chip *chip = d->host_data;
466 irq_set_chip_data(irq, chip);
467 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
468 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
469 /* Chips that can sleep need nested thread handlers */
470 if (chip->can_sleep && !chip->irq_not_threaded)
471 irq_set_nested_thread(irq, 1);
473 set_irq_flags(irq, IRQF_VALID);
475 irq_set_noprobe(irq);
478 * No set-up of the hardware will happen if IRQ_TYPE_NONE
479 * is passed as default type.
481 if (chip->irq_default_type != IRQ_TYPE_NONE)
482 irq_set_irq_type(irq, chip->irq_default_type);
487 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
489 struct gpio_chip *chip = d->host_data;
492 set_irq_flags(irq, 0);
495 irq_set_nested_thread(irq, 0);
496 irq_set_chip_and_handler(irq, NULL, NULL);
497 irq_set_chip_data(irq, NULL);
500 static const struct irq_domain_ops gpiochip_domain_ops = {
501 .map = gpiochip_irq_map,
502 .unmap = gpiochip_irq_unmap,
503 /* Virtually all GPIO irqchips are twocell:ed */
504 .xlate = irq_domain_xlate_twocell,
507 static int gpiochip_irq_reqres(struct irq_data *d)
509 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
511 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
513 "unable to lock HW IRQ %lu for IRQ\n",
520 static void gpiochip_irq_relres(struct irq_data *d)
522 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
524 gpiochip_unlock_as_irq(chip, d->hwirq);
527 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
529 return irq_find_mapping(chip->irqdomain, offset);
533 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
534 * @gpiochip: the gpiochip to remove the irqchip from
536 * This is called only from gpiochip_remove()
538 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
542 acpi_gpiochip_free_interrupts(gpiochip);
544 /* Remove all IRQ mappings and delete the domain */
545 if (gpiochip->irqdomain) {
546 for (offset = 0; offset < gpiochip->ngpio; offset++)
548 irq_find_mapping(gpiochip->irqdomain, offset));
549 irq_domain_remove(gpiochip->irqdomain);
552 if (gpiochip->irqchip) {
553 gpiochip->irqchip->irq_request_resources = NULL;
554 gpiochip->irqchip->irq_release_resources = NULL;
555 gpiochip->irqchip = NULL;
560 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
561 * @gpiochip: the gpiochip to add the irqchip to
562 * @irqchip: the irqchip to add to the gpiochip
563 * @first_irq: if not dynamically assigned, the base (first) IRQ to
564 * allocate gpiochip irqs from
565 * @handler: the irq handler to use (often a predefined irq core function)
566 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
567 * to have the core avoid setting up any default type in the hardware.
569 * This function closely associates a certain irqchip with a certain
570 * gpiochip, providing an irq domain to translate the local IRQs to
571 * global irqs in the gpiolib core, and making sure that the gpiochip
572 * is passed as chip data to all related functions. Driver callbacks
573 * need to use container_of() to get their local state containers back
574 * from the gpiochip passed as chip data. An irqdomain will be stored
575 * in the gpiochip that shall be used by the driver to handle IRQ number
576 * translation. The gpiochip will need to be initialized and registered
577 * before calling this function.
579 * This function will handle two cell:ed simple IRQs and assumes all
580 * the pins on the gpiochip can generate a unique IRQ. Everything else
581 * need to be open coded.
583 int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
584 struct irq_chip *irqchip,
585 unsigned int first_irq,
586 irq_flow_handler_t handler,
589 struct device_node *of_node;
591 unsigned irq_base = 0;
593 if (!gpiochip || !irqchip)
596 if (!gpiochip->dev) {
597 pr_err("missing gpiochip .dev parent pointer\n");
600 of_node = gpiochip->dev->of_node;
601 #ifdef CONFIG_OF_GPIO
603 * If the gpiochip has an assigned OF node this takes precendence
604 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
606 if (gpiochip->of_node)
607 of_node = gpiochip->of_node;
609 gpiochip->irqchip = irqchip;
610 gpiochip->irq_handler = handler;
611 gpiochip->irq_default_type = type;
612 gpiochip->to_irq = gpiochip_to_irq;
613 gpiochip->irqdomain = irq_domain_add_simple(of_node,
614 gpiochip->ngpio, first_irq,
615 &gpiochip_domain_ops, gpiochip);
616 if (!gpiochip->irqdomain) {
617 gpiochip->irqchip = NULL;
620 irqchip->irq_request_resources = gpiochip_irq_reqres;
621 irqchip->irq_release_resources = gpiochip_irq_relres;
624 * Prepare the mapping since the irqchip shall be orthogonal to
625 * any gpiochip calls. If the first_irq was zero, this is
626 * necessary to allocate descriptors for all IRQs.
628 for (offset = 0; offset < gpiochip->ngpio; offset++) {
629 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
632 * Store the base into the gpiochip to be used when
633 * unmapping the irqs.
635 gpiochip->irq_base = irq_base;
638 acpi_gpiochip_request_interrupts(gpiochip);
642 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
644 #else /* CONFIG_GPIOLIB_IRQCHIP */
646 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
648 #endif /* CONFIG_GPIOLIB_IRQCHIP */
650 #ifdef CONFIG_PINCTRL
653 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
654 * @chip: the gpiochip to add the range for
655 * @pinctrl: the dev_name() of the pin controller to map to
656 * @gpio_offset: the start offset in the current gpio_chip number space
657 * @pin_group: name of the pin group inside the pin controller
659 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
660 struct pinctrl_dev *pctldev,
661 unsigned int gpio_offset, const char *pin_group)
663 struct gpio_pin_range *pin_range;
666 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
668 chip_err(chip, "failed to allocate pin ranges\n");
672 /* Use local offset as range ID */
673 pin_range->range.id = gpio_offset;
674 pin_range->range.gc = chip;
675 pin_range->range.name = chip->label;
676 pin_range->range.base = chip->base + gpio_offset;
677 pin_range->pctldev = pctldev;
679 ret = pinctrl_get_group_pins(pctldev, pin_group,
680 &pin_range->range.pins,
681 &pin_range->range.npins);
687 pinctrl_add_gpio_range(pctldev, &pin_range->range);
689 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
690 gpio_offset, gpio_offset + pin_range->range.npins - 1,
691 pinctrl_dev_get_devname(pctldev), pin_group);
693 list_add_tail(&pin_range->node, &chip->pin_ranges);
697 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
700 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
701 * @chip: the gpiochip to add the range for
702 * @pinctrl_name: the dev_name() of the pin controller to map to
703 * @gpio_offset: the start offset in the current gpio_chip number space
704 * @pin_offset: the start offset in the pin controller number space
705 * @npins: the number of pins from the offset of each pin space (GPIO and
706 * pin controller) to accumulate in this range
708 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
709 unsigned int gpio_offset, unsigned int pin_offset,
712 struct gpio_pin_range *pin_range;
715 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
717 chip_err(chip, "failed to allocate pin ranges\n");
721 /* Use local offset as range ID */
722 pin_range->range.id = gpio_offset;
723 pin_range->range.gc = chip;
724 pin_range->range.name = chip->label;
725 pin_range->range.base = chip->base + gpio_offset;
726 pin_range->range.pin_base = pin_offset;
727 pin_range->range.npins = npins;
728 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
730 if (IS_ERR(pin_range->pctldev)) {
731 ret = PTR_ERR(pin_range->pctldev);
732 chip_err(chip, "could not create pin range\n");
736 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
737 gpio_offset, gpio_offset + npins - 1,
739 pin_offset, pin_offset + npins - 1);
741 list_add_tail(&pin_range->node, &chip->pin_ranges);
745 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
748 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
749 * @chip: the chip to remove all the mappings for
751 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
753 struct gpio_pin_range *pin_range, *tmp;
755 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
756 list_del(&pin_range->node);
757 pinctrl_remove_gpio_range(pin_range->pctldev,
762 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
764 #endif /* CONFIG_PINCTRL */
766 /* These "optional" allocation calls help prevent drivers from stomping
767 * on each other, and help provide better diagnostics in debugfs.
768 * They're called even less than the "set direction" calls.
770 static int __gpiod_request(struct gpio_desc *desc, const char *label)
772 struct gpio_chip *chip = desc->chip;
776 spin_lock_irqsave(&gpio_lock, flags);
778 /* NOTE: gpio_request() can be called in early boot,
779 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
782 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
783 desc_set_label(desc, label ? : "?");
791 /* chip->request may sleep */
792 spin_unlock_irqrestore(&gpio_lock, flags);
793 status = chip->request(chip, gpio_chip_hwgpio(desc));
794 spin_lock_irqsave(&gpio_lock, flags);
797 desc_set_label(desc, NULL);
798 clear_bit(FLAG_REQUESTED, &desc->flags);
802 if (chip->get_direction) {
803 /* chip->get_direction may sleep */
804 spin_unlock_irqrestore(&gpio_lock, flags);
805 gpiod_get_direction(desc);
806 spin_lock_irqsave(&gpio_lock, flags);
809 spin_unlock_irqrestore(&gpio_lock, flags);
813 int gpiod_request(struct gpio_desc *desc, const char *label)
815 int status = -EPROBE_DEFER;
816 struct gpio_chip *chip;
819 pr_warn("%s: invalid GPIO\n", __func__);
827 if (try_module_get(chip->owner)) {
828 status = __gpiod_request(desc, label);
830 module_put(chip->owner);
835 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
840 static bool __gpiod_free(struct gpio_desc *desc)
844 struct gpio_chip *chip;
848 gpiod_unexport(desc);
850 spin_lock_irqsave(&gpio_lock, flags);
853 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
855 spin_unlock_irqrestore(&gpio_lock, flags);
856 might_sleep_if(chip->can_sleep);
857 chip->free(chip, gpio_chip_hwgpio(desc));
858 spin_lock_irqsave(&gpio_lock, flags);
860 desc_set_label(desc, NULL);
861 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
862 clear_bit(FLAG_REQUESTED, &desc->flags);
863 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
864 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
868 spin_unlock_irqrestore(&gpio_lock, flags);
872 void gpiod_free(struct gpio_desc *desc)
874 if (desc && __gpiod_free(desc))
875 module_put(desc->chip->owner);
877 WARN_ON(extra_checks);
881 * gpiochip_is_requested - return string iff signal was requested
882 * @chip: controller managing the signal
883 * @offset: of signal within controller's 0..(ngpio - 1) range
885 * Returns NULL if the GPIO is not currently requested, else a string.
886 * The string returned is the label passed to gpio_request(); if none has been
887 * passed it is a meaningless, non-NULL constant.
889 * This function is for use by GPIO controller drivers. The label can
890 * help with diagnostics, and knowing that the signal is used as a GPIO
891 * can help avoid accidentally multiplexing it to another controller.
893 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
895 struct gpio_desc *desc;
897 if (!GPIO_OFFSET_VALID(chip, offset))
900 desc = &chip->desc[offset];
902 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
906 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
909 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
910 * @desc: GPIO descriptor to request
911 * @label: label for the GPIO
913 * Function allows GPIO chip drivers to request and use their own GPIO
914 * descriptors via gpiolib API. Difference to gpiod_request() is that this
915 * function will not increase reference count of the GPIO chip module. This
916 * allows the GPIO chip module to be unloaded as needed (we assume that the
917 * GPIO chip driver handles freeing the GPIOs it has requested).
919 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
922 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
926 chip_err(chip, "failed to get GPIO descriptor\n");
930 err = __gpiod_request(desc, label);
936 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
939 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
940 * @desc: GPIO descriptor to free
942 * Function frees the given GPIO requested previously with
943 * gpiochip_request_own_desc().
945 void gpiochip_free_own_desc(struct gpio_desc *desc)
950 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
952 /* Drivers MUST set GPIO direction before making get/set calls. In
953 * some cases this is done in early boot, before IRQs are enabled.
955 * As a rule these aren't called more than once (except for drivers
956 * using the open-drain emulation idiom) so these are natural places
957 * to accumulate extra debugging checks. Note that we can't (yet)
958 * rely on gpio_request() having been called beforehand.
962 * gpiod_direction_input - set the GPIO direction to input
963 * @desc: GPIO to set to input
965 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
966 * be called safely on it.
968 * Return 0 in case of success, else an error code.
970 int gpiod_direction_input(struct gpio_desc *desc)
972 struct gpio_chip *chip;
973 int status = -EINVAL;
975 if (!desc || !desc->chip) {
976 pr_warn("%s: invalid GPIO\n", __func__);
981 if (!chip->get || !chip->direction_input) {
983 "%s: missing get() or direction_input() operations\n",
988 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
990 clear_bit(FLAG_IS_OUT, &desc->flags);
992 trace_gpio_direction(desc_to_gpio(desc), 1, status);
996 EXPORT_SYMBOL_GPL(gpiod_direction_input);
998 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1000 struct gpio_chip *chip;
1001 int status = -EINVAL;
1003 /* GPIOs used for IRQs shall not be set as output */
1004 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1006 "%s: tried to set a GPIO tied to an IRQ as output\n",
1011 /* Open drain pin should not be driven to 1 */
1012 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1013 return gpiod_direction_input(desc);
1015 /* Open source pin should not be driven to 0 */
1016 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1017 return gpiod_direction_input(desc);
1020 if (!chip->set || !chip->direction_output) {
1022 "%s: missing set() or direction_output() operations\n",
1027 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1029 set_bit(FLAG_IS_OUT, &desc->flags);
1030 trace_gpio_value(desc_to_gpio(desc), 0, value);
1031 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1036 * gpiod_direction_output_raw - set the GPIO direction to output
1037 * @desc: GPIO to set to output
1038 * @value: initial output value of the GPIO
1040 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1041 * be called safely on it. The initial value of the output must be specified
1042 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1044 * Return 0 in case of success, else an error code.
1046 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1048 if (!desc || !desc->chip) {
1049 pr_warn("%s: invalid GPIO\n", __func__);
1052 return _gpiod_direction_output_raw(desc, value);
1054 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1057 * gpiod_direction_output - set the GPIO direction to output
1058 * @desc: GPIO to set to output
1059 * @value: initial output value of the GPIO
1061 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1062 * be called safely on it. The initial value of the output must be specified
1063 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1066 * Return 0 in case of success, else an error code.
1068 int gpiod_direction_output(struct gpio_desc *desc, int value)
1070 if (!desc || !desc->chip) {
1071 pr_warn("%s: invalid GPIO\n", __func__);
1074 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1076 return _gpiod_direction_output_raw(desc, value);
1078 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1081 * gpiod_set_debounce - sets @debounce time for a @gpio
1082 * @gpio: the gpio to set debounce time
1083 * @debounce: debounce time is microseconds
1085 * returns -ENOTSUPP if the controller does not support setting
1088 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1090 struct gpio_chip *chip;
1092 if (!desc || !desc->chip) {
1093 pr_warn("%s: invalid GPIO\n", __func__);
1098 if (!chip->set || !chip->set_debounce) {
1100 "%s: missing set() or set_debounce() operations\n",
1105 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1107 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1110 * gpiod_is_active_low - test whether a GPIO is active-low or not
1111 * @desc: the gpio descriptor to test
1113 * Returns 1 if the GPIO is active-low, 0 otherwise.
1115 int gpiod_is_active_low(const struct gpio_desc *desc)
1117 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1119 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1121 /* I/O calls are only valid after configuration completed; the relevant
1122 * "is this a valid GPIO" error checks should already have been done.
1124 * "Get" operations are often inlinable as reading a pin value register,
1125 * and masking the relevant bit in that register.
1127 * When "set" operations are inlinable, they involve writing that mask to
1128 * one register to set a low value, or a different register to set it high.
1129 * Otherwise locking is needed, so there may be little value to inlining.
1131 *------------------------------------------------------------------------
1133 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1134 * have requested the GPIO. That can include implicit requesting by
1135 * a direction setting call. Marking a gpio as requested locks its chip
1136 * in memory, guaranteeing that these table lookups need no more locking
1137 * and that gpiochip_remove() will fail.
1139 * REVISIT when debugging, consider adding some instrumentation to ensure
1140 * that the GPIO was actually requested.
1143 static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
1145 struct gpio_chip *chip;
1150 offset = gpio_chip_hwgpio(desc);
1151 value = chip->get ? chip->get(chip, offset) : false;
1152 trace_gpio_value(desc_to_gpio(desc), 1, value);
1157 * gpiod_get_raw_value() - return a gpio's raw value
1158 * @desc: gpio whose value will be returned
1160 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1161 * its ACTIVE_LOW status.
1163 * This function should be called from contexts where we cannot sleep, and will
1164 * complain if the GPIO chip functions potentially sleep.
1166 int gpiod_get_raw_value(const struct gpio_desc *desc)
1170 /* Should be using gpio_get_value_cansleep() */
1171 WARN_ON(desc->chip->can_sleep);
1172 return _gpiod_get_raw_value(desc);
1174 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1177 * gpiod_get_value() - return a gpio's value
1178 * @desc: gpio whose value will be returned
1180 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1183 * This function should be called from contexts where we cannot sleep, and will
1184 * complain if the GPIO chip functions potentially sleep.
1186 int gpiod_get_value(const struct gpio_desc *desc)
1191 /* Should be using gpio_get_value_cansleep() */
1192 WARN_ON(desc->chip->can_sleep);
1194 value = _gpiod_get_raw_value(desc);
1195 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1200 EXPORT_SYMBOL_GPL(gpiod_get_value);
1203 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1204 * @desc: gpio descriptor whose state need to be set.
1205 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1207 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1210 struct gpio_chip *chip = desc->chip;
1211 int offset = gpio_chip_hwgpio(desc);
1214 err = chip->direction_input(chip, offset);
1216 clear_bit(FLAG_IS_OUT, &desc->flags);
1218 err = chip->direction_output(chip, offset, 0);
1220 set_bit(FLAG_IS_OUT, &desc->flags);
1222 trace_gpio_direction(desc_to_gpio(desc), value, err);
1225 "%s: Error in set_value for open drain err %d\n",
1230 * _gpio_set_open_source_value() - Set the open source gpio's value.
1231 * @desc: gpio descriptor whose state need to be set.
1232 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1234 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1237 struct gpio_chip *chip = desc->chip;
1238 int offset = gpio_chip_hwgpio(desc);
1241 err = chip->direction_output(chip, offset, 1);
1243 set_bit(FLAG_IS_OUT, &desc->flags);
1245 err = chip->direction_input(chip, offset);
1247 clear_bit(FLAG_IS_OUT, &desc->flags);
1249 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1252 "%s: Error in set_value for open source err %d\n",
1256 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1258 struct gpio_chip *chip;
1261 trace_gpio_value(desc_to_gpio(desc), 0, value);
1262 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1263 _gpio_set_open_drain_value(desc, value);
1264 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1265 _gpio_set_open_source_value(desc, value);
1267 chip->set(chip, gpio_chip_hwgpio(desc), value);
1271 * set multiple outputs on the same chip;
1272 * use the chip's set_multiple function if available;
1273 * otherwise set the outputs sequentially;
1274 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1275 * defines which outputs are to be changed
1276 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1277 * defines the values the outputs specified by mask are to be set to
1279 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1280 unsigned long *mask, unsigned long *bits)
1282 if (chip->set_multiple) {
1283 chip->set_multiple(chip, mask, bits);
1286 for (i = 0; i < chip->ngpio; i++) {
1287 if (mask[BIT_WORD(i)] == 0) {
1288 /* no more set bits in this mask word;
1289 * skip ahead to the next word */
1290 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1293 /* set outputs if the corresponding mask bit is set */
1294 if (__test_and_clear_bit(i, mask)) {
1295 chip->set(chip, i, test_bit(i, bits));
1301 static void gpiod_set_array_priv(bool raw, bool can_sleep,
1302 unsigned int array_size,
1303 struct gpio_desc **desc_array,
1308 while (i < array_size) {
1309 struct gpio_chip *chip = desc_array[i]->chip;
1310 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1311 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1315 WARN_ON(chip->can_sleep);
1317 memset(mask, 0, sizeof(mask));
1319 struct gpio_desc *desc = desc_array[i];
1320 int hwgpio = gpio_chip_hwgpio(desc);
1321 int value = value_array[i];
1323 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1325 trace_gpio_value(desc_to_gpio(desc), 0, value);
1327 * collect all normal outputs belonging to the same chip
1328 * open drain and open source outputs are set individually
1330 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1331 _gpio_set_open_drain_value(desc,value);
1332 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1333 _gpio_set_open_source_value(desc, value);
1335 __set_bit(hwgpio, mask);
1337 __set_bit(hwgpio, bits);
1339 __clear_bit(hwgpio, bits);
1344 } while ((i < array_size) && (desc_array[i]->chip == chip));
1345 /* push collected bits to outputs */
1347 gpio_chip_set_multiple(chip, mask, bits);
1353 * gpiod_set_raw_value() - assign a gpio's raw value
1354 * @desc: gpio whose value will be assigned
1355 * @value: value to assign
1357 * Set the raw value of the GPIO, i.e. the value of its physical line without
1358 * regard for its ACTIVE_LOW status.
1360 * This function should be called from contexts where we cannot sleep, and will
1361 * complain if the GPIO chip functions potentially sleep.
1363 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1367 /* Should be using gpio_set_value_cansleep() */
1368 WARN_ON(desc->chip->can_sleep);
1369 _gpiod_set_raw_value(desc, value);
1371 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1374 * gpiod_set_value() - assign a gpio's value
1375 * @desc: gpio whose value will be assigned
1376 * @value: value to assign
1378 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1381 * This function should be called from contexts where we cannot sleep, and will
1382 * complain if the GPIO chip functions potentially sleep.
1384 void gpiod_set_value(struct gpio_desc *desc, int value)
1388 /* Should be using gpio_set_value_cansleep() */
1389 WARN_ON(desc->chip->can_sleep);
1390 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1392 _gpiod_set_raw_value(desc, value);
1394 EXPORT_SYMBOL_GPL(gpiod_set_value);
1397 * gpiod_set_raw_array() - assign values to an array of GPIOs
1398 * @array_size: number of elements in the descriptor / value arrays
1399 * @desc_array: array of GPIO descriptors whose values will be assigned
1400 * @value_array: array of values to assign
1402 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1403 * without regard for their ACTIVE_LOW status.
1405 * This function should be called from contexts where we cannot sleep, and will
1406 * complain if the GPIO chip functions potentially sleep.
1408 void gpiod_set_raw_array(unsigned int array_size,
1409 struct gpio_desc **desc_array, int *value_array)
1413 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1415 EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1418 * gpiod_set_array() - assign values to an array of GPIOs
1419 * @array_size: number of elements in the descriptor / value arrays
1420 * @desc_array: array of GPIO descriptors whose values will be assigned
1421 * @value_array: array of values to assign
1423 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1426 * This function should be called from contexts where we cannot sleep, and will
1427 * complain if the GPIO chip functions potentially sleep.
1429 void gpiod_set_array(unsigned int array_size,
1430 struct gpio_desc **desc_array, int *value_array)
1434 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1436 EXPORT_SYMBOL_GPL(gpiod_set_array);
1439 * gpiod_cansleep() - report whether gpio value access may sleep
1440 * @desc: gpio to check
1443 int gpiod_cansleep(const struct gpio_desc *desc)
1447 return desc->chip->can_sleep;
1449 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1452 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1453 * @desc: gpio whose IRQ will be returned (already requested)
1455 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1458 int gpiod_to_irq(const struct gpio_desc *desc)
1460 struct gpio_chip *chip;
1466 offset = gpio_chip_hwgpio(desc);
1467 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1469 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1472 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1473 * @chip: the chip the GPIO to lock belongs to
1474 * @offset: the offset of the GPIO to lock as IRQ
1476 * This is used directly by GPIO drivers that want to lock down
1477 * a certain GPIO line to be used for IRQs.
1479 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1481 if (offset >= chip->ngpio)
1484 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1486 "%s: tried to flag a GPIO set as output for IRQ\n",
1491 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1494 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1497 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1498 * @chip: the chip the GPIO to lock belongs to
1499 * @offset: the offset of the GPIO to lock as IRQ
1501 * This is used directly by GPIO drivers that want to indicate
1502 * that a certain GPIO is no longer used exclusively for IRQ.
1504 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1506 if (offset >= chip->ngpio)
1509 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1511 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1514 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1515 * @desc: gpio whose value will be returned
1517 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1518 * its ACTIVE_LOW status.
1520 * This function is to be called from contexts that can sleep.
1522 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1524 might_sleep_if(extra_checks);
1527 return _gpiod_get_raw_value(desc);
1529 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1532 * gpiod_get_value_cansleep() - return a gpio's value
1533 * @desc: gpio whose value will be returned
1535 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1538 * This function is to be called from contexts that can sleep.
1540 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1544 might_sleep_if(extra_checks);
1548 value = _gpiod_get_raw_value(desc);
1549 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1554 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
1557 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1558 * @desc: gpio whose value will be assigned
1559 * @value: value to assign
1561 * Set the raw value of the GPIO, i.e. the value of its physical line without
1562 * regard for its ACTIVE_LOW status.
1564 * This function is to be called from contexts that can sleep.
1566 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
1568 might_sleep_if(extra_checks);
1571 _gpiod_set_raw_value(desc, value);
1573 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
1576 * gpiod_set_value_cansleep() - assign a gpio's value
1577 * @desc: gpio whose value will be assigned
1578 * @value: value to assign
1580 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1583 * This function is to be called from contexts that can sleep.
1585 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1587 might_sleep_if(extra_checks);
1591 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1593 _gpiod_set_raw_value(desc, value);
1595 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1598 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1599 * @array_size: number of elements in the descriptor / value arrays
1600 * @desc_array: array of GPIO descriptors whose values will be assigned
1601 * @value_array: array of values to assign
1603 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1604 * without regard for their ACTIVE_LOW status.
1606 * This function is to be called from contexts that can sleep.
1608 void gpiod_set_raw_array_cansleep(unsigned int array_size,
1609 struct gpio_desc **desc_array,
1612 might_sleep_if(extra_checks);
1615 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1617 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1620 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1621 * @array_size: number of elements in the descriptor / value arrays
1622 * @desc_array: array of GPIO descriptors whose values will be assigned
1623 * @value_array: array of values to assign
1625 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1628 * This function is to be called from contexts that can sleep.
1630 void gpiod_set_array_cansleep(unsigned int array_size,
1631 struct gpio_desc **desc_array,
1634 might_sleep_if(extra_checks);
1637 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1639 EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1642 * gpiod_add_lookup_table() - register GPIO device consumers
1643 * @table: table of consumers to register
1645 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
1647 mutex_lock(&gpio_lookup_lock);
1649 list_add_tail(&table->list, &gpio_lookup_list);
1651 mutex_unlock(&gpio_lookup_lock);
1654 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
1656 enum gpio_lookup_flags *flags)
1658 static const char *suffixes[] = { "gpios", "gpio" };
1659 char prop_name[32]; /* 32 is max size of property name */
1660 enum of_gpio_flags of_flags;
1661 struct gpio_desc *desc;
1664 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1666 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1668 snprintf(prop_name, 32, "%s", suffixes[i]);
1670 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1672 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1679 if (of_flags & OF_GPIO_ACTIVE_LOW)
1680 *flags |= GPIO_ACTIVE_LOW;
1685 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1687 enum gpio_lookup_flags *flags)
1689 struct acpi_gpio_info info;
1690 struct gpio_desc *desc;
1692 desc = acpi_get_gpiod_by_index(dev, idx, &info);
1696 if (info.gpioint && info.active_low)
1697 *flags |= GPIO_ACTIVE_LOW;
1702 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1704 const char *dev_id = dev ? dev_name(dev) : NULL;
1705 struct gpiod_lookup_table *table;
1707 mutex_lock(&gpio_lookup_lock);
1709 list_for_each_entry(table, &gpio_lookup_list, list) {
1710 if (table->dev_id && dev_id) {
1712 * Valid strings on both ends, must be identical to have
1715 if (!strcmp(table->dev_id, dev_id))
1719 * One of the pointers is NULL, so both must be to have
1722 if (dev_id == table->dev_id)
1729 mutex_unlock(&gpio_lookup_lock);
1733 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1735 enum gpio_lookup_flags *flags)
1737 struct gpio_desc *desc = ERR_PTR(-ENOENT);
1738 struct gpiod_lookup_table *table;
1739 struct gpiod_lookup *p;
1741 table = gpiod_find_lookup_table(dev);
1745 for (p = &table->table[0]; p->chip_label; p++) {
1746 struct gpio_chip *chip;
1748 /* idx must always match exactly */
1752 /* If the lookup entry has a con_id, require exact match */
1753 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1756 chip = find_chip_by_name(p->chip_label);
1759 dev_err(dev, "cannot find GPIO chip %s\n",
1761 return ERR_PTR(-ENODEV);
1764 if (chip->ngpio <= p->chip_hwnum) {
1766 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1767 idx, chip->ngpio, chip->label);
1768 return ERR_PTR(-EINVAL);
1771 desc = gpiochip_get_desc(chip, p->chip_hwnum);
1781 * gpiod_get - obtain a GPIO for a given GPIO function
1782 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1783 * @con_id: function within the GPIO consumer
1784 * @flags: optional GPIO initialization flags
1786 * Return the GPIO descriptor corresponding to the function con_id of device
1787 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1788 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
1790 struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1791 enum gpiod_flags flags)
1793 return gpiod_get_index(dev, con_id, 0, flags);
1795 EXPORT_SYMBOL_GPL(__gpiod_get);
1798 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1799 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1800 * @con_id: function within the GPIO consumer
1801 * @flags: optional GPIO initialization flags
1803 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1804 * the requested function it will return NULL. This is convenient for drivers
1805 * that need to handle optional GPIOs.
1807 struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1809 enum gpiod_flags flags)
1811 return gpiod_get_index_optional(dev, con_id, 0, flags);
1813 EXPORT_SYMBOL_GPL(__gpiod_get_optional);
1816 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
1817 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1818 * @con_id: function within the GPIO consumer
1819 * @idx: index of the GPIO to obtain in the consumer
1820 * @flags: optional GPIO initialization flags
1822 * This variant of gpiod_get() allows to access GPIOs other than the first
1823 * defined one for functions that define several GPIOs.
1825 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1826 * requested function and/or index, or another IS_ERR() code if an error
1827 * occured while trying to acquire the GPIO.
1829 struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
1832 enum gpiod_flags flags)
1834 struct gpio_desc *desc = NULL;
1836 enum gpio_lookup_flags lookupflags = 0;
1838 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1840 /* Using device tree? */
1841 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1842 dev_dbg(dev, "using device tree for GPIO lookup\n");
1843 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
1844 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1845 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1846 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
1850 * Either we are not using DT or ACPI, or their lookup did not return
1851 * a result. In that case, use platform lookup as a fallback.
1853 if (!desc || desc == ERR_PTR(-ENOENT)) {
1854 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
1855 desc = gpiod_find(dev, con_id, idx, &lookupflags);
1859 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
1863 status = gpiod_request(desc, con_id);
1866 return ERR_PTR(status);
1868 if (lookupflags & GPIO_ACTIVE_LOW)
1869 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1870 if (lookupflags & GPIO_OPEN_DRAIN)
1871 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1872 if (lookupflags & GPIO_OPEN_SOURCE)
1873 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1875 /* No particular flag request, return here... */
1876 if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
1880 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1881 status = gpiod_direction_output(desc,
1882 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1884 status = gpiod_direction_input(desc);
1887 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1889 return ERR_PTR(status);
1894 EXPORT_SYMBOL_GPL(__gpiod_get_index);
1897 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1899 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1900 * @con_id: function within the GPIO consumer
1901 * @index: index of the GPIO to obtain in the consumer
1902 * @flags: optional GPIO initialization flags
1904 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1905 * specified index was assigned to the requested function it will return NULL.
1906 * This is convenient for drivers that need to handle optional GPIOs.
1908 struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
1911 enum gpiod_flags flags)
1913 struct gpio_desc *desc;
1915 desc = gpiod_get_index(dev, con_id, index, flags);
1917 if (PTR_ERR(desc) == -ENOENT)
1923 EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
1926 * gpiod_put - dispose of a GPIO descriptor
1927 * @desc: GPIO descriptor to dispose of
1929 * No descriptor can be used after gpiod_put() has been called on it.
1931 void gpiod_put(struct gpio_desc *desc)
1935 EXPORT_SYMBOL_GPL(gpiod_put);
1937 #ifdef CONFIG_DEBUG_FS
1939 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1942 unsigned gpio = chip->base;
1943 struct gpio_desc *gdesc = &chip->desc[0];
1947 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1948 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1951 gpiod_get_direction(gdesc);
1952 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1953 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
1954 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
1956 is_out ? "out" : "in ",
1958 ? (chip->get(chip, i) ? "hi" : "lo")
1960 is_irq ? "IRQ" : " ");
1961 seq_printf(s, "\n");
1965 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
1967 unsigned long flags;
1968 struct gpio_chip *chip = NULL;
1969 loff_t index = *pos;
1973 spin_lock_irqsave(&gpio_lock, flags);
1974 list_for_each_entry(chip, &gpio_chips, list)
1976 spin_unlock_irqrestore(&gpio_lock, flags);
1979 spin_unlock_irqrestore(&gpio_lock, flags);
1984 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1986 unsigned long flags;
1987 struct gpio_chip *chip = v;
1990 spin_lock_irqsave(&gpio_lock, flags);
1991 if (list_is_last(&chip->list, &gpio_chips))
1994 ret = list_entry(chip->list.next, struct gpio_chip, list);
1995 spin_unlock_irqrestore(&gpio_lock, flags);
2003 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2007 static int gpiolib_seq_show(struct seq_file *s, void *v)
2009 struct gpio_chip *chip = v;
2012 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2013 chip->base, chip->base + chip->ngpio - 1);
2016 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2019 seq_printf(s, ", %s", chip->label);
2020 if (chip->can_sleep)
2021 seq_printf(s, ", can sleep");
2022 seq_printf(s, ":\n");
2025 chip->dbg_show(s, chip);
2027 gpiolib_dbg_show(s, chip);
2032 static const struct seq_operations gpiolib_seq_ops = {
2033 .start = gpiolib_seq_start,
2034 .next = gpiolib_seq_next,
2035 .stop = gpiolib_seq_stop,
2036 .show = gpiolib_seq_show,
2039 static int gpiolib_open(struct inode *inode, struct file *file)
2041 return seq_open(file, &gpiolib_seq_ops);
2044 static const struct file_operations gpiolib_operations = {
2045 .owner = THIS_MODULE,
2046 .open = gpiolib_open,
2048 .llseek = seq_lseek,
2049 .release = seq_release,
2052 static int __init gpiolib_debugfs_init(void)
2054 /* /sys/kernel/debug/gpio */
2055 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2056 NULL, NULL, &gpiolib_operations);
2059 subsys_initcall(gpiolib_debugfs_init);
2061 #endif /* DEBUG_FS */