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 if (!gpio_is_valid(gpio))
81 WARN(1, "invalid GPIO %d\n", gpio);
85 EXPORT_SYMBOL_GPL(gpio_to_desc);
88 * Get the GPIO descriptor corresponding to the given hw number for this chip.
90 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
93 if (hwnum >= chip->ngpio)
94 return ERR_PTR(-EINVAL);
96 return &chip->desc[hwnum];
100 * Convert a GPIO descriptor to the integer namespace.
101 * This should disappear in the future but is needed since we still
102 * use GPIO numbers for error messages and sysfs nodes
104 int desc_to_gpio(const struct gpio_desc *desc)
106 return desc->chip->base + (desc - &desc->chip->desc[0]);
108 EXPORT_SYMBOL_GPL(desc_to_gpio);
112 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
113 * @desc: descriptor to return the chip of
115 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
117 return desc ? desc->chip : NULL;
119 EXPORT_SYMBOL_GPL(gpiod_to_chip);
121 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
122 static int gpiochip_find_base(int ngpio)
124 struct gpio_chip *chip;
125 int base = ARCH_NR_GPIOS - ngpio;
127 list_for_each_entry_reverse(chip, &gpio_chips, list) {
128 /* found a free space? */
129 if (chip->base + chip->ngpio <= base)
132 /* nope, check the space right before the chip */
133 base = chip->base - ngpio;
136 if (gpio_is_valid(base)) {
137 pr_debug("%s: found new base at %d\n", __func__, base);
140 pr_err("%s: cannot find free range\n", __func__);
146 * gpiod_get_direction - return the current direction of a GPIO
147 * @desc: GPIO to get the direction of
149 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
151 * This function may sleep if gpiod_cansleep() is true.
153 int gpiod_get_direction(struct gpio_desc *desc)
155 struct gpio_chip *chip;
157 int status = -EINVAL;
159 chip = gpiod_to_chip(desc);
160 offset = gpio_chip_hwgpio(desc);
162 if (!chip->get_direction)
165 status = chip->get_direction(chip, offset);
167 /* GPIOF_DIR_IN, or other positive */
169 clear_bit(FLAG_IS_OUT, &desc->flags);
173 set_bit(FLAG_IS_OUT, &desc->flags);
177 EXPORT_SYMBOL_GPL(gpiod_get_direction);
180 * Add a new chip to the global chips list, keeping the list of chips sorted
183 * Return -EBUSY if the new chip overlaps with some other chip's integer
186 static int gpiochip_add_to_list(struct gpio_chip *chip)
188 struct list_head *pos = &gpio_chips;
189 struct gpio_chip *_chip;
192 /* find where to insert our chip */
193 list_for_each(pos, &gpio_chips) {
194 _chip = list_entry(pos, struct gpio_chip, list);
195 /* shall we insert before _chip? */
196 if (_chip->base >= chip->base + chip->ngpio)
200 /* are we stepping on the chip right before? */
201 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
202 _chip = list_entry(pos->prev, struct gpio_chip, list);
203 if (_chip->base + _chip->ngpio > chip->base) {
205 "GPIO integer space overlap, cannot add chip\n");
211 list_add_tail(&chip->list, pos);
217 * gpiochip_add() - register a gpio_chip
218 * @chip: the chip to register, with chip->base initialized
219 * Context: potentially before irqs will work
221 * Returns a negative errno if the chip can't be registered, such as
222 * because the chip->base is invalid or already associated with a
223 * different chip. Otherwise it returns zero as a success code.
225 * When gpiochip_add() is called very early during boot, so that GPIOs
226 * can be freely used, the chip->dev device must be registered before
227 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
228 * for GPIOs will fail rudely.
230 * If chip->base is negative, this requests dynamic assignment of
231 * a range of valid GPIOs.
233 int gpiochip_add(struct gpio_chip *chip)
238 int base = chip->base;
239 struct gpio_desc *descs;
241 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
245 spin_lock_irqsave(&gpio_lock, flags);
248 base = gpiochip_find_base(chip->ngpio);
256 status = gpiochip_add_to_list(chip);
259 for (id = 0; id < chip->ngpio; id++) {
260 struct gpio_desc *desc = &descs[id];
263 /* REVISIT: most hardware initializes GPIOs as
264 * inputs (often with pullups enabled) so power
265 * usage is minimized. Linux code should set the
266 * gpio direction first thing; but until it does,
267 * and in case chip->get_direction is not set,
268 * we may expose the wrong direction in sysfs.
270 desc->flags = !chip->direction_input
278 spin_unlock_irqrestore(&gpio_lock, flags);
280 #ifdef CONFIG_PINCTRL
281 INIT_LIST_HEAD(&chip->pin_ranges);
284 of_gpiochip_add(chip);
285 acpi_gpiochip_add(chip);
290 status = gpiochip_export(chip);
294 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
295 chip->base, chip->base + chip->ngpio - 1,
296 chip->label ? : "generic");
301 spin_unlock_irqrestore(&gpio_lock, flags);
306 /* failures here can mean systems won't boot... */
307 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
308 chip->base, chip->base + chip->ngpio - 1,
309 chip->label ? : "generic");
312 EXPORT_SYMBOL_GPL(gpiochip_add);
314 /* Forward-declaration */
315 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
318 * gpiochip_remove() - unregister a gpio_chip
319 * @chip: the chip to unregister
321 * A gpio_chip with any GPIOs still requested may not be removed.
323 void gpiochip_remove(struct gpio_chip *chip)
328 acpi_gpiochip_remove(chip);
330 spin_lock_irqsave(&gpio_lock, flags);
332 gpiochip_irqchip_remove(chip);
333 gpiochip_remove_pin_ranges(chip);
334 of_gpiochip_remove(chip);
336 for (id = 0; id < chip->ngpio; id++) {
337 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
338 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
340 for (id = 0; id < chip->ngpio; id++)
341 chip->desc[id].chip = NULL;
343 list_del(&chip->list);
344 spin_unlock_irqrestore(&gpio_lock, flags);
345 gpiochip_unexport(chip);
350 EXPORT_SYMBOL_GPL(gpiochip_remove);
353 * gpiochip_find() - iterator for locating a specific gpio_chip
354 * @data: data to pass to match function
355 * @callback: Callback function to check gpio_chip
357 * Similar to bus_find_device. It returns a reference to a gpio_chip as
358 * determined by a user supplied @match callback. The callback should return
359 * 0 if the device doesn't match and non-zero if it does. If the callback is
360 * non-zero, this function will return to the caller and not iterate over any
363 struct gpio_chip *gpiochip_find(void *data,
364 int (*match)(struct gpio_chip *chip,
367 struct gpio_chip *chip;
370 spin_lock_irqsave(&gpio_lock, flags);
371 list_for_each_entry(chip, &gpio_chips, list)
372 if (match(chip, data))
376 if (&chip->list == &gpio_chips)
378 spin_unlock_irqrestore(&gpio_lock, flags);
382 EXPORT_SYMBOL_GPL(gpiochip_find);
384 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
386 const char *name = data;
388 return !strcmp(chip->label, name);
391 static struct gpio_chip *find_chip_by_name(const char *name)
393 return gpiochip_find((void *)name, gpiochip_match_name);
396 #ifdef CONFIG_GPIOLIB_IRQCHIP
399 * The following is irqchip helper code for gpiochips.
403 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
404 * @gpiochip: the gpiochip to set the irqchip chain to
405 * @irqchip: the irqchip to chain to the gpiochip
406 * @parent_irq: the irq number corresponding to the parent IRQ for this
408 * @parent_handler: the parent interrupt handler for the accumulated IRQ
409 * coming out of the gpiochip. If the interrupt is nested rather than
410 * cascaded, pass NULL in this handler argument
412 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
413 struct irq_chip *irqchip,
415 irq_flow_handler_t parent_handler)
419 if (!gpiochip->irqdomain) {
420 chip_err(gpiochip, "called %s before setting up irqchip\n",
425 if (parent_handler) {
426 if (gpiochip->can_sleep) {
428 "you cannot have chained interrupts on a "
429 "chip that may sleep\n");
433 * The parent irqchip is already using the chip_data for this
434 * irqchip, so our callbacks simply use the handler_data.
436 irq_set_handler_data(parent_irq, gpiochip);
437 irq_set_chained_handler(parent_irq, parent_handler);
440 /* Set the parent IRQ for all affected IRQs */
441 for (offset = 0; offset < gpiochip->ngpio; offset++)
442 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
445 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
448 * This lock class tells lockdep that GPIO irqs are in a different
449 * category than their parents, so it won't report false recursion.
451 static struct lock_class_key gpiochip_irq_lock_class;
454 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
455 * @d: the irqdomain used by this irqchip
456 * @irq: the global irq number used by this GPIO irqchip irq
457 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
459 * This function will set up the mapping for a certain IRQ line on a
460 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
461 * stored inside the gpiochip.
463 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
464 irq_hw_number_t hwirq)
466 struct gpio_chip *chip = d->host_data;
468 irq_set_chip_data(irq, chip);
469 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
470 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
471 /* Chips that can sleep need nested thread handlers */
472 if (chip->can_sleep && !chip->irq_not_threaded)
473 irq_set_nested_thread(irq, 1);
475 set_irq_flags(irq, IRQF_VALID);
477 irq_set_noprobe(irq);
480 * No set-up of the hardware will happen if IRQ_TYPE_NONE
481 * is passed as default type.
483 if (chip->irq_default_type != IRQ_TYPE_NONE)
484 irq_set_irq_type(irq, chip->irq_default_type);
489 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
491 struct gpio_chip *chip = d->host_data;
494 set_irq_flags(irq, 0);
497 irq_set_nested_thread(irq, 0);
498 irq_set_chip_and_handler(irq, NULL, NULL);
499 irq_set_chip_data(irq, NULL);
502 static const struct irq_domain_ops gpiochip_domain_ops = {
503 .map = gpiochip_irq_map,
504 .unmap = gpiochip_irq_unmap,
505 /* Virtually all GPIO irqchips are twocell:ed */
506 .xlate = irq_domain_xlate_twocell,
509 static int gpiochip_irq_reqres(struct irq_data *d)
511 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
513 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
515 "unable to lock HW IRQ %lu for IRQ\n",
522 static void gpiochip_irq_relres(struct irq_data *d)
524 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
526 gpiochip_unlock_as_irq(chip, d->hwirq);
529 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
531 return irq_find_mapping(chip->irqdomain, offset);
535 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
536 * @gpiochip: the gpiochip to remove the irqchip from
538 * This is called only from gpiochip_remove()
540 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
544 acpi_gpiochip_free_interrupts(gpiochip);
546 /* Remove all IRQ mappings and delete the domain */
547 if (gpiochip->irqdomain) {
548 for (offset = 0; offset < gpiochip->ngpio; offset++)
550 irq_find_mapping(gpiochip->irqdomain, offset));
551 irq_domain_remove(gpiochip->irqdomain);
554 if (gpiochip->irqchip) {
555 gpiochip->irqchip->irq_request_resources = NULL;
556 gpiochip->irqchip->irq_release_resources = NULL;
557 gpiochip->irqchip = NULL;
562 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
563 * @gpiochip: the gpiochip to add the irqchip to
564 * @irqchip: the irqchip to add to the gpiochip
565 * @first_irq: if not dynamically assigned, the base (first) IRQ to
566 * allocate gpiochip irqs from
567 * @handler: the irq handler to use (often a predefined irq core function)
568 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
569 * to have the core avoid setting up any default type in the hardware.
571 * This function closely associates a certain irqchip with a certain
572 * gpiochip, providing an irq domain to translate the local IRQs to
573 * global irqs in the gpiolib core, and making sure that the gpiochip
574 * is passed as chip data to all related functions. Driver callbacks
575 * need to use container_of() to get their local state containers back
576 * from the gpiochip passed as chip data. An irqdomain will be stored
577 * in the gpiochip that shall be used by the driver to handle IRQ number
578 * translation. The gpiochip will need to be initialized and registered
579 * before calling this function.
581 * This function will handle two cell:ed simple IRQs and assumes all
582 * the pins on the gpiochip can generate a unique IRQ. Everything else
583 * need to be open coded.
585 int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
586 struct irq_chip *irqchip,
587 unsigned int first_irq,
588 irq_flow_handler_t handler,
591 struct device_node *of_node;
593 unsigned irq_base = 0;
595 if (!gpiochip || !irqchip)
598 if (!gpiochip->dev) {
599 pr_err("missing gpiochip .dev parent pointer\n");
602 of_node = gpiochip->dev->of_node;
603 #ifdef CONFIG_OF_GPIO
605 * If the gpiochip has an assigned OF node this takes precendence
606 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
608 if (gpiochip->of_node)
609 of_node = gpiochip->of_node;
611 gpiochip->irqchip = irqchip;
612 gpiochip->irq_handler = handler;
613 gpiochip->irq_default_type = type;
614 gpiochip->to_irq = gpiochip_to_irq;
615 gpiochip->irqdomain = irq_domain_add_simple(of_node,
616 gpiochip->ngpio, first_irq,
617 &gpiochip_domain_ops, gpiochip);
618 if (!gpiochip->irqdomain) {
619 gpiochip->irqchip = NULL;
622 irqchip->irq_request_resources = gpiochip_irq_reqres;
623 irqchip->irq_release_resources = gpiochip_irq_relres;
626 * Prepare the mapping since the irqchip shall be orthogonal to
627 * any gpiochip calls. If the first_irq was zero, this is
628 * necessary to allocate descriptors for all IRQs.
630 for (offset = 0; offset < gpiochip->ngpio; offset++) {
631 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
634 * Store the base into the gpiochip to be used when
635 * unmapping the irqs.
637 gpiochip->irq_base = irq_base;
640 acpi_gpiochip_request_interrupts(gpiochip);
644 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
646 #else /* CONFIG_GPIOLIB_IRQCHIP */
648 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
650 #endif /* CONFIG_GPIOLIB_IRQCHIP */
652 #ifdef CONFIG_PINCTRL
655 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
656 * @chip: the gpiochip to add the range for
657 * @pinctrl: the dev_name() of the pin controller to map to
658 * @gpio_offset: the start offset in the current gpio_chip number space
659 * @pin_group: name of the pin group inside the pin controller
661 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
662 struct pinctrl_dev *pctldev,
663 unsigned int gpio_offset, const char *pin_group)
665 struct gpio_pin_range *pin_range;
668 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
670 chip_err(chip, "failed to allocate pin ranges\n");
674 /* Use local offset as range ID */
675 pin_range->range.id = gpio_offset;
676 pin_range->range.gc = chip;
677 pin_range->range.name = chip->label;
678 pin_range->range.base = chip->base + gpio_offset;
679 pin_range->pctldev = pctldev;
681 ret = pinctrl_get_group_pins(pctldev, pin_group,
682 &pin_range->range.pins,
683 &pin_range->range.npins);
689 pinctrl_add_gpio_range(pctldev, &pin_range->range);
691 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
692 gpio_offset, gpio_offset + pin_range->range.npins - 1,
693 pinctrl_dev_get_devname(pctldev), pin_group);
695 list_add_tail(&pin_range->node, &chip->pin_ranges);
699 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
702 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
703 * @chip: the gpiochip to add the range for
704 * @pinctrl_name: the dev_name() of the pin controller to map to
705 * @gpio_offset: the start offset in the current gpio_chip number space
706 * @pin_offset: the start offset in the pin controller number space
707 * @npins: the number of pins from the offset of each pin space (GPIO and
708 * pin controller) to accumulate in this range
710 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
711 unsigned int gpio_offset, unsigned int pin_offset,
714 struct gpio_pin_range *pin_range;
717 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
719 chip_err(chip, "failed to allocate pin ranges\n");
723 /* Use local offset as range ID */
724 pin_range->range.id = gpio_offset;
725 pin_range->range.gc = chip;
726 pin_range->range.name = chip->label;
727 pin_range->range.base = chip->base + gpio_offset;
728 pin_range->range.pin_base = pin_offset;
729 pin_range->range.npins = npins;
730 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
732 if (IS_ERR(pin_range->pctldev)) {
733 ret = PTR_ERR(pin_range->pctldev);
734 chip_err(chip, "could not create pin range\n");
738 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
739 gpio_offset, gpio_offset + npins - 1,
741 pin_offset, pin_offset + npins - 1);
743 list_add_tail(&pin_range->node, &chip->pin_ranges);
747 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
750 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
751 * @chip: the chip to remove all the mappings for
753 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
755 struct gpio_pin_range *pin_range, *tmp;
757 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
758 list_del(&pin_range->node);
759 pinctrl_remove_gpio_range(pin_range->pctldev,
764 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
766 #endif /* CONFIG_PINCTRL */
768 /* These "optional" allocation calls help prevent drivers from stomping
769 * on each other, and help provide better diagnostics in debugfs.
770 * They're called even less than the "set direction" calls.
772 static int __gpiod_request(struct gpio_desc *desc, const char *label)
774 struct gpio_chip *chip = desc->chip;
778 spin_lock_irqsave(&gpio_lock, flags);
780 /* NOTE: gpio_request() can be called in early boot,
781 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
784 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
785 desc_set_label(desc, label ? : "?");
793 /* chip->request may sleep */
794 spin_unlock_irqrestore(&gpio_lock, flags);
795 status = chip->request(chip, gpio_chip_hwgpio(desc));
796 spin_lock_irqsave(&gpio_lock, flags);
799 desc_set_label(desc, NULL);
800 clear_bit(FLAG_REQUESTED, &desc->flags);
804 if (chip->get_direction) {
805 /* chip->get_direction may sleep */
806 spin_unlock_irqrestore(&gpio_lock, flags);
807 gpiod_get_direction(desc);
808 spin_lock_irqsave(&gpio_lock, flags);
811 spin_unlock_irqrestore(&gpio_lock, flags);
815 int gpiod_request(struct gpio_desc *desc, const char *label)
817 int status = -EPROBE_DEFER;
818 struct gpio_chip *chip;
821 pr_warn("%s: invalid GPIO\n", __func__);
829 if (try_module_get(chip->owner)) {
830 status = __gpiod_request(desc, label);
832 module_put(chip->owner);
837 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
842 static bool __gpiod_free(struct gpio_desc *desc)
846 struct gpio_chip *chip;
850 gpiod_unexport(desc);
852 spin_lock_irqsave(&gpio_lock, flags);
855 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
857 spin_unlock_irqrestore(&gpio_lock, flags);
858 might_sleep_if(chip->can_sleep);
859 chip->free(chip, gpio_chip_hwgpio(desc));
860 spin_lock_irqsave(&gpio_lock, flags);
862 desc_set_label(desc, NULL);
863 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
864 clear_bit(FLAG_REQUESTED, &desc->flags);
865 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
866 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
870 spin_unlock_irqrestore(&gpio_lock, flags);
874 void gpiod_free(struct gpio_desc *desc)
876 if (desc && __gpiod_free(desc))
877 module_put(desc->chip->owner);
879 WARN_ON(extra_checks);
883 * gpiochip_is_requested - return string iff signal was requested
884 * @chip: controller managing the signal
885 * @offset: of signal within controller's 0..(ngpio - 1) range
887 * Returns NULL if the GPIO is not currently requested, else a string.
888 * The string returned is the label passed to gpio_request(); if none has been
889 * passed it is a meaningless, non-NULL constant.
891 * This function is for use by GPIO controller drivers. The label can
892 * help with diagnostics, and knowing that the signal is used as a GPIO
893 * can help avoid accidentally multiplexing it to another controller.
895 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
897 struct gpio_desc *desc;
899 if (!GPIO_OFFSET_VALID(chip, offset))
902 desc = &chip->desc[offset];
904 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
908 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
911 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
912 * @desc: GPIO descriptor to request
913 * @label: label for the GPIO
915 * Function allows GPIO chip drivers to request and use their own GPIO
916 * descriptors via gpiolib API. Difference to gpiod_request() is that this
917 * function will not increase reference count of the GPIO chip module. This
918 * allows the GPIO chip module to be unloaded as needed (we assume that the
919 * GPIO chip driver handles freeing the GPIOs it has requested).
921 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
924 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
928 chip_err(chip, "failed to get GPIO descriptor\n");
932 err = __gpiod_request(desc, label);
938 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
941 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
942 * @desc: GPIO descriptor to free
944 * Function frees the given GPIO requested previously with
945 * gpiochip_request_own_desc().
947 void gpiochip_free_own_desc(struct gpio_desc *desc)
952 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
954 /* Drivers MUST set GPIO direction before making get/set calls. In
955 * some cases this is done in early boot, before IRQs are enabled.
957 * As a rule these aren't called more than once (except for drivers
958 * using the open-drain emulation idiom) so these are natural places
959 * to accumulate extra debugging checks. Note that we can't (yet)
960 * rely on gpio_request() having been called beforehand.
964 * gpiod_direction_input - set the GPIO direction to input
965 * @desc: GPIO to set to input
967 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
968 * be called safely on it.
970 * Return 0 in case of success, else an error code.
972 int gpiod_direction_input(struct gpio_desc *desc)
974 struct gpio_chip *chip;
975 int status = -EINVAL;
977 if (!desc || !desc->chip) {
978 pr_warn("%s: invalid GPIO\n", __func__);
983 if (!chip->get || !chip->direction_input) {
985 "%s: missing get() or direction_input() operations\n",
990 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
992 clear_bit(FLAG_IS_OUT, &desc->flags);
994 trace_gpio_direction(desc_to_gpio(desc), 1, status);
998 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1000 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1002 struct gpio_chip *chip;
1003 int status = -EINVAL;
1005 /* GPIOs used for IRQs shall not be set as output */
1006 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1008 "%s: tried to set a GPIO tied to an IRQ as output\n",
1013 /* Open drain pin should not be driven to 1 */
1014 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1015 return gpiod_direction_input(desc);
1017 /* Open source pin should not be driven to 0 */
1018 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1019 return gpiod_direction_input(desc);
1022 if (!chip->set || !chip->direction_output) {
1024 "%s: missing set() or direction_output() operations\n",
1029 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1031 set_bit(FLAG_IS_OUT, &desc->flags);
1032 trace_gpio_value(desc_to_gpio(desc), 0, value);
1033 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1038 * gpiod_direction_output_raw - set the GPIO direction to output
1039 * @desc: GPIO to set to output
1040 * @value: initial output value of the GPIO
1042 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1043 * be called safely on it. The initial value of the output must be specified
1044 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1046 * Return 0 in case of success, else an error code.
1048 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1050 if (!desc || !desc->chip) {
1051 pr_warn("%s: invalid GPIO\n", __func__);
1054 return _gpiod_direction_output_raw(desc, value);
1056 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1059 * gpiod_direction_output - set the GPIO direction to output
1060 * @desc: GPIO to set to output
1061 * @value: initial output value of the GPIO
1063 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1064 * be called safely on it. The initial value of the output must be specified
1065 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1068 * Return 0 in case of success, else an error code.
1070 int gpiod_direction_output(struct gpio_desc *desc, int value)
1072 if (!desc || !desc->chip) {
1073 pr_warn("%s: invalid GPIO\n", __func__);
1076 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1078 return _gpiod_direction_output_raw(desc, value);
1080 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1083 * gpiod_set_debounce - sets @debounce time for a @gpio
1084 * @gpio: the gpio to set debounce time
1085 * @debounce: debounce time is microseconds
1087 * returns -ENOTSUPP if the controller does not support setting
1090 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1092 struct gpio_chip *chip;
1094 if (!desc || !desc->chip) {
1095 pr_warn("%s: invalid GPIO\n", __func__);
1100 if (!chip->set || !chip->set_debounce) {
1102 "%s: missing set() or set_debounce() operations\n",
1107 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1109 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1112 * gpiod_is_active_low - test whether a GPIO is active-low or not
1113 * @desc: the gpio descriptor to test
1115 * Returns 1 if the GPIO is active-low, 0 otherwise.
1117 int gpiod_is_active_low(const struct gpio_desc *desc)
1119 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1121 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1123 /* I/O calls are only valid after configuration completed; the relevant
1124 * "is this a valid GPIO" error checks should already have been done.
1126 * "Get" operations are often inlinable as reading a pin value register,
1127 * and masking the relevant bit in that register.
1129 * When "set" operations are inlinable, they involve writing that mask to
1130 * one register to set a low value, or a different register to set it high.
1131 * Otherwise locking is needed, so there may be little value to inlining.
1133 *------------------------------------------------------------------------
1135 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1136 * have requested the GPIO. That can include implicit requesting by
1137 * a direction setting call. Marking a gpio as requested locks its chip
1138 * in memory, guaranteeing that these table lookups need no more locking
1139 * and that gpiochip_remove() will fail.
1141 * REVISIT when debugging, consider adding some instrumentation to ensure
1142 * that the GPIO was actually requested.
1145 static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
1147 struct gpio_chip *chip;
1152 offset = gpio_chip_hwgpio(desc);
1153 value = chip->get ? chip->get(chip, offset) : false;
1154 trace_gpio_value(desc_to_gpio(desc), 1, value);
1159 * gpiod_get_raw_value() - return a gpio's raw value
1160 * @desc: gpio whose value will be returned
1162 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1163 * its ACTIVE_LOW status.
1165 * This function should be called from contexts where we cannot sleep, and will
1166 * complain if the GPIO chip functions potentially sleep.
1168 int gpiod_get_raw_value(const struct gpio_desc *desc)
1172 /* Should be using gpio_get_value_cansleep() */
1173 WARN_ON(desc->chip->can_sleep);
1174 return _gpiod_get_raw_value(desc);
1176 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1179 * gpiod_get_value() - return a gpio's value
1180 * @desc: gpio whose value will be returned
1182 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1185 * This function should be called from contexts where we cannot sleep, and will
1186 * complain if the GPIO chip functions potentially sleep.
1188 int gpiod_get_value(const struct gpio_desc *desc)
1193 /* Should be using gpio_get_value_cansleep() */
1194 WARN_ON(desc->chip->can_sleep);
1196 value = _gpiod_get_raw_value(desc);
1197 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1202 EXPORT_SYMBOL_GPL(gpiod_get_value);
1205 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1206 * @desc: gpio descriptor whose state need to be set.
1207 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1209 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1212 struct gpio_chip *chip = desc->chip;
1213 int offset = gpio_chip_hwgpio(desc);
1216 err = chip->direction_input(chip, offset);
1218 clear_bit(FLAG_IS_OUT, &desc->flags);
1220 err = chip->direction_output(chip, offset, 0);
1222 set_bit(FLAG_IS_OUT, &desc->flags);
1224 trace_gpio_direction(desc_to_gpio(desc), value, err);
1227 "%s: Error in set_value for open drain err %d\n",
1232 * _gpio_set_open_source_value() - Set the open source gpio's value.
1233 * @desc: gpio descriptor whose state need to be set.
1234 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1236 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1239 struct gpio_chip *chip = desc->chip;
1240 int offset = gpio_chip_hwgpio(desc);
1243 err = chip->direction_output(chip, offset, 1);
1245 set_bit(FLAG_IS_OUT, &desc->flags);
1247 err = chip->direction_input(chip, offset);
1249 clear_bit(FLAG_IS_OUT, &desc->flags);
1251 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1254 "%s: Error in set_value for open source err %d\n",
1258 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1260 struct gpio_chip *chip;
1263 trace_gpio_value(desc_to_gpio(desc), 0, value);
1264 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1265 _gpio_set_open_drain_value(desc, value);
1266 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1267 _gpio_set_open_source_value(desc, value);
1269 chip->set(chip, gpio_chip_hwgpio(desc), value);
1273 * set multiple outputs on the same chip;
1274 * use the chip's set_multiple function if available;
1275 * otherwise set the outputs sequentially;
1276 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1277 * defines which outputs are to be changed
1278 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1279 * defines the values the outputs specified by mask are to be set to
1281 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1282 unsigned long *mask, unsigned long *bits)
1284 if (chip->set_multiple) {
1285 chip->set_multiple(chip, mask, bits);
1288 for (i = 0; i < chip->ngpio; i++) {
1289 if (mask[BIT_WORD(i)] == 0) {
1290 /* no more set bits in this mask word;
1291 * skip ahead to the next word */
1292 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1295 /* set outputs if the corresponding mask bit is set */
1296 if (__test_and_clear_bit(i, mask)) {
1297 chip->set(chip, i, test_bit(i, bits));
1303 static void gpiod_set_array_priv(bool raw, bool can_sleep,
1304 unsigned int array_size,
1305 struct gpio_desc **desc_array,
1310 while (i < array_size) {
1311 struct gpio_chip *chip = desc_array[i]->chip;
1312 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1313 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1317 WARN_ON(chip->can_sleep);
1319 memset(mask, 0, sizeof(mask));
1321 struct gpio_desc *desc = desc_array[i];
1322 int hwgpio = gpio_chip_hwgpio(desc);
1323 int value = value_array[i];
1325 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1327 trace_gpio_value(desc_to_gpio(desc), 0, value);
1329 * collect all normal outputs belonging to the same chip
1330 * open drain and open source outputs are set individually
1332 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1333 _gpio_set_open_drain_value(desc,value);
1334 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1335 _gpio_set_open_source_value(desc, value);
1337 __set_bit(hwgpio, mask);
1339 __set_bit(hwgpio, bits);
1341 __clear_bit(hwgpio, bits);
1346 } while ((i < array_size) && (desc_array[i]->chip == chip));
1347 /* push collected bits to outputs */
1349 gpio_chip_set_multiple(chip, mask, bits);
1355 * gpiod_set_raw_value() - assign a gpio's raw value
1356 * @desc: gpio whose value will be assigned
1357 * @value: value to assign
1359 * Set the raw value of the GPIO, i.e. the value of its physical line without
1360 * regard for its ACTIVE_LOW status.
1362 * This function should be called from contexts where we cannot sleep, and will
1363 * complain if the GPIO chip functions potentially sleep.
1365 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1369 /* Should be using gpio_set_value_cansleep() */
1370 WARN_ON(desc->chip->can_sleep);
1371 _gpiod_set_raw_value(desc, value);
1373 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1376 * gpiod_set_value() - assign a gpio's value
1377 * @desc: gpio whose value will be assigned
1378 * @value: value to assign
1380 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1383 * This function should be called from contexts where we cannot sleep, and will
1384 * complain if the GPIO chip functions potentially sleep.
1386 void gpiod_set_value(struct gpio_desc *desc, int value)
1390 /* Should be using gpio_set_value_cansleep() */
1391 WARN_ON(desc->chip->can_sleep);
1392 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1394 _gpiod_set_raw_value(desc, value);
1396 EXPORT_SYMBOL_GPL(gpiod_set_value);
1399 * gpiod_set_raw_array() - assign values to an array of GPIOs
1400 * @array_size: number of elements in the descriptor / value arrays
1401 * @desc_array: array of GPIO descriptors whose values will be assigned
1402 * @value_array: array of values to assign
1404 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1405 * without regard for their ACTIVE_LOW status.
1407 * This function should be called from contexts where we cannot sleep, and will
1408 * complain if the GPIO chip functions potentially sleep.
1410 void gpiod_set_raw_array(unsigned int array_size,
1411 struct gpio_desc **desc_array, int *value_array)
1415 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1417 EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1420 * gpiod_set_array() - assign values to an array of GPIOs
1421 * @array_size: number of elements in the descriptor / value arrays
1422 * @desc_array: array of GPIO descriptors whose values will be assigned
1423 * @value_array: array of values to assign
1425 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1428 * This function should be called from contexts where we cannot sleep, and will
1429 * complain if the GPIO chip functions potentially sleep.
1431 void gpiod_set_array(unsigned int array_size,
1432 struct gpio_desc **desc_array, int *value_array)
1436 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1438 EXPORT_SYMBOL_GPL(gpiod_set_array);
1441 * gpiod_cansleep() - report whether gpio value access may sleep
1442 * @desc: gpio to check
1445 int gpiod_cansleep(const struct gpio_desc *desc)
1449 return desc->chip->can_sleep;
1451 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1454 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1455 * @desc: gpio whose IRQ will be returned (already requested)
1457 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1460 int gpiod_to_irq(const struct gpio_desc *desc)
1462 struct gpio_chip *chip;
1468 offset = gpio_chip_hwgpio(desc);
1469 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1471 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1474 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1475 * @chip: the chip the GPIO to lock belongs to
1476 * @offset: the offset of the GPIO to lock as IRQ
1478 * This is used directly by GPIO drivers that want to lock down
1479 * a certain GPIO line to be used for IRQs.
1481 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1483 if (offset >= chip->ngpio)
1486 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1488 "%s: tried to flag a GPIO set as output for IRQ\n",
1493 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1496 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1499 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1500 * @chip: the chip the GPIO to lock belongs to
1501 * @offset: the offset of the GPIO to lock as IRQ
1503 * This is used directly by GPIO drivers that want to indicate
1504 * that a certain GPIO is no longer used exclusively for IRQ.
1506 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1508 if (offset >= chip->ngpio)
1511 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1513 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1516 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1517 * @desc: gpio whose value will be returned
1519 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1520 * its ACTIVE_LOW status.
1522 * This function is to be called from contexts that can sleep.
1524 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1526 might_sleep_if(extra_checks);
1529 return _gpiod_get_raw_value(desc);
1531 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1534 * gpiod_get_value_cansleep() - return a gpio's value
1535 * @desc: gpio whose value will be returned
1537 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1540 * This function is to be called from contexts that can sleep.
1542 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1546 might_sleep_if(extra_checks);
1550 value = _gpiod_get_raw_value(desc);
1551 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1556 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
1559 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1560 * @desc: gpio whose value will be assigned
1561 * @value: value to assign
1563 * Set the raw value of the GPIO, i.e. the value of its physical line without
1564 * regard for its ACTIVE_LOW status.
1566 * This function is to be called from contexts that can sleep.
1568 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
1570 might_sleep_if(extra_checks);
1573 _gpiod_set_raw_value(desc, value);
1575 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
1578 * gpiod_set_value_cansleep() - assign a gpio's value
1579 * @desc: gpio whose value will be assigned
1580 * @value: value to assign
1582 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1585 * This function is to be called from contexts that can sleep.
1587 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1589 might_sleep_if(extra_checks);
1593 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1595 _gpiod_set_raw_value(desc, value);
1597 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1600 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1601 * @array_size: number of elements in the descriptor / value arrays
1602 * @desc_array: array of GPIO descriptors whose values will be assigned
1603 * @value_array: array of values to assign
1605 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1606 * without regard for their ACTIVE_LOW status.
1608 * This function is to be called from contexts that can sleep.
1610 void gpiod_set_raw_array_cansleep(unsigned int array_size,
1611 struct gpio_desc **desc_array,
1614 might_sleep_if(extra_checks);
1617 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1619 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1622 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1623 * @array_size: number of elements in the descriptor / value arrays
1624 * @desc_array: array of GPIO descriptors whose values will be assigned
1625 * @value_array: array of values to assign
1627 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1630 * This function is to be called from contexts that can sleep.
1632 void gpiod_set_array_cansleep(unsigned int array_size,
1633 struct gpio_desc **desc_array,
1636 might_sleep_if(extra_checks);
1639 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1641 EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1644 * gpiod_add_lookup_table() - register GPIO device consumers
1645 * @table: table of consumers to register
1647 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
1649 mutex_lock(&gpio_lookup_lock);
1651 list_add_tail(&table->list, &gpio_lookup_list);
1653 mutex_unlock(&gpio_lookup_lock);
1656 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
1658 enum gpio_lookup_flags *flags)
1660 static const char *suffixes[] = { "gpios", "gpio" };
1661 char prop_name[32]; /* 32 is max size of property name */
1662 enum of_gpio_flags of_flags;
1663 struct gpio_desc *desc;
1666 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1668 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1670 snprintf(prop_name, 32, "%s", suffixes[i]);
1672 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1674 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1681 if (of_flags & OF_GPIO_ACTIVE_LOW)
1682 *flags |= GPIO_ACTIVE_LOW;
1687 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1689 enum gpio_lookup_flags *flags)
1691 static const char * const suffixes[] = { "gpios", "gpio" };
1692 struct acpi_device *adev = ACPI_COMPANION(dev);
1693 struct acpi_gpio_info info;
1694 struct gpio_desc *desc;
1698 /* Try first from _DSD */
1699 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1700 if (con_id && strcmp(con_id, "gpios")) {
1701 snprintf(propname, sizeof(propname), "%s-%s",
1702 con_id, suffixes[i]);
1704 snprintf(propname, sizeof(propname), "%s",
1708 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1709 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1713 /* Then from plain _CRS GPIOs */
1715 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1720 if (info.active_low)
1721 *flags |= GPIO_ACTIVE_LOW;
1726 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1728 const char *dev_id = dev ? dev_name(dev) : NULL;
1729 struct gpiod_lookup_table *table;
1731 mutex_lock(&gpio_lookup_lock);
1733 list_for_each_entry(table, &gpio_lookup_list, list) {
1734 if (table->dev_id && dev_id) {
1736 * Valid strings on both ends, must be identical to have
1739 if (!strcmp(table->dev_id, dev_id))
1743 * One of the pointers is NULL, so both must be to have
1746 if (dev_id == table->dev_id)
1753 mutex_unlock(&gpio_lookup_lock);
1757 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1759 enum gpio_lookup_flags *flags)
1761 struct gpio_desc *desc = ERR_PTR(-ENOENT);
1762 struct gpiod_lookup_table *table;
1763 struct gpiod_lookup *p;
1765 table = gpiod_find_lookup_table(dev);
1769 for (p = &table->table[0]; p->chip_label; p++) {
1770 struct gpio_chip *chip;
1772 /* idx must always match exactly */
1776 /* If the lookup entry has a con_id, require exact match */
1777 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1780 chip = find_chip_by_name(p->chip_label);
1783 dev_err(dev, "cannot find GPIO chip %s\n",
1785 return ERR_PTR(-ENODEV);
1788 if (chip->ngpio <= p->chip_hwnum) {
1790 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1791 idx, chip->ngpio, chip->label);
1792 return ERR_PTR(-EINVAL);
1795 desc = gpiochip_get_desc(chip, p->chip_hwnum);
1805 * gpiod_get - obtain a GPIO for a given GPIO function
1806 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1807 * @con_id: function within the GPIO consumer
1808 * @flags: optional GPIO initialization flags
1810 * Return the GPIO descriptor corresponding to the function con_id of device
1811 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1812 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
1814 struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1815 enum gpiod_flags flags)
1817 return gpiod_get_index(dev, con_id, 0, flags);
1819 EXPORT_SYMBOL_GPL(__gpiod_get);
1822 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1823 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1824 * @con_id: function within the GPIO consumer
1825 * @flags: optional GPIO initialization flags
1827 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1828 * the requested function it will return NULL. This is convenient for drivers
1829 * that need to handle optional GPIOs.
1831 struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1833 enum gpiod_flags flags)
1835 return gpiod_get_index_optional(dev, con_id, 0, flags);
1837 EXPORT_SYMBOL_GPL(__gpiod_get_optional);
1840 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
1841 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1842 * @con_id: function within the GPIO consumer
1843 * @idx: index of the GPIO to obtain in the consumer
1844 * @flags: optional GPIO initialization flags
1846 * This variant of gpiod_get() allows to access GPIOs other than the first
1847 * defined one for functions that define several GPIOs.
1849 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1850 * requested function and/or index, or another IS_ERR() code if an error
1851 * occured while trying to acquire the GPIO.
1853 struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
1856 enum gpiod_flags flags)
1858 struct gpio_desc *desc = NULL;
1860 enum gpio_lookup_flags lookupflags = 0;
1862 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1864 /* Using device tree? */
1865 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1866 dev_dbg(dev, "using device tree for GPIO lookup\n");
1867 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
1868 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1869 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1870 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
1874 * Either we are not using DT or ACPI, or their lookup did not return
1875 * a result. In that case, use platform lookup as a fallback.
1877 if (!desc || desc == ERR_PTR(-ENOENT)) {
1878 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
1879 desc = gpiod_find(dev, con_id, idx, &lookupflags);
1883 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
1887 status = gpiod_request(desc, con_id);
1890 return ERR_PTR(status);
1892 if (lookupflags & GPIO_ACTIVE_LOW)
1893 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1894 if (lookupflags & GPIO_OPEN_DRAIN)
1895 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1896 if (lookupflags & GPIO_OPEN_SOURCE)
1897 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1899 /* No particular flag request, return here... */
1900 if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
1904 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1905 status = gpiod_direction_output(desc,
1906 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1908 status = gpiod_direction_input(desc);
1911 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1913 return ERR_PTR(status);
1918 EXPORT_SYMBOL_GPL(__gpiod_get_index);
1921 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
1922 * @fwnode: handle of the firmware node
1923 * @propname: name of the firmware property representing the GPIO
1925 * This function can be used for drivers that get their configuration
1928 * Function properly finds the corresponding GPIO using whatever is the
1929 * underlying firmware interface and then makes sure that the GPIO
1930 * descriptor is requested before it is returned to the caller.
1932 * In case of error an ERR_PTR() is returned.
1934 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
1935 const char *propname)
1937 struct gpio_desc *desc = ERR_PTR(-ENODEV);
1938 bool active_low = false;
1942 return ERR_PTR(-EINVAL);
1944 if (is_of_node(fwnode)) {
1945 enum of_gpio_flags flags;
1947 desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
1950 active_low = flags & OF_GPIO_ACTIVE_LOW;
1951 } else if (is_acpi_node(fwnode)) {
1952 struct acpi_gpio_info info;
1954 desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
1957 active_low = info.active_low;
1963 ret = gpiod_request(desc, NULL);
1965 return ERR_PTR(ret);
1967 /* Only value flag can be set from both DT and ACPI is active_low */
1969 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1973 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
1976 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1978 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1979 * @con_id: function within the GPIO consumer
1980 * @index: index of the GPIO to obtain in the consumer
1981 * @flags: optional GPIO initialization flags
1983 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1984 * specified index was assigned to the requested function it will return NULL.
1985 * This is convenient for drivers that need to handle optional GPIOs.
1987 struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
1990 enum gpiod_flags flags)
1992 struct gpio_desc *desc;
1994 desc = gpiod_get_index(dev, con_id, index, flags);
1996 if (PTR_ERR(desc) == -ENOENT)
2002 EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
2005 * gpiod_put - dispose of a GPIO descriptor
2006 * @desc: GPIO descriptor to dispose of
2008 * No descriptor can be used after gpiod_put() has been called on it.
2010 void gpiod_put(struct gpio_desc *desc)
2014 EXPORT_SYMBOL_GPL(gpiod_put);
2016 #ifdef CONFIG_DEBUG_FS
2018 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2021 unsigned gpio = chip->base;
2022 struct gpio_desc *gdesc = &chip->desc[0];
2026 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2027 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2030 gpiod_get_direction(gdesc);
2031 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2032 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2033 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
2035 is_out ? "out" : "in ",
2037 ? (chip->get(chip, i) ? "hi" : "lo")
2039 is_irq ? "IRQ" : " ");
2040 seq_printf(s, "\n");
2044 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2046 unsigned long flags;
2047 struct gpio_chip *chip = NULL;
2048 loff_t index = *pos;
2052 spin_lock_irqsave(&gpio_lock, flags);
2053 list_for_each_entry(chip, &gpio_chips, list)
2055 spin_unlock_irqrestore(&gpio_lock, flags);
2058 spin_unlock_irqrestore(&gpio_lock, flags);
2063 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2065 unsigned long flags;
2066 struct gpio_chip *chip = v;
2069 spin_lock_irqsave(&gpio_lock, flags);
2070 if (list_is_last(&chip->list, &gpio_chips))
2073 ret = list_entry(chip->list.next, struct gpio_chip, list);
2074 spin_unlock_irqrestore(&gpio_lock, flags);
2082 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2086 static int gpiolib_seq_show(struct seq_file *s, void *v)
2088 struct gpio_chip *chip = v;
2091 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2092 chip->base, chip->base + chip->ngpio - 1);
2095 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2098 seq_printf(s, ", %s", chip->label);
2099 if (chip->can_sleep)
2100 seq_printf(s, ", can sleep");
2101 seq_printf(s, ":\n");
2104 chip->dbg_show(s, chip);
2106 gpiolib_dbg_show(s, chip);
2111 static const struct seq_operations gpiolib_seq_ops = {
2112 .start = gpiolib_seq_start,
2113 .next = gpiolib_seq_next,
2114 .stop = gpiolib_seq_stop,
2115 .show = gpiolib_seq_show,
2118 static int gpiolib_open(struct inode *inode, struct file *file)
2120 return seq_open(file, &gpiolib_seq_ops);
2123 static const struct file_operations gpiolib_operations = {
2124 .owner = THIS_MODULE,
2125 .open = gpiolib_open,
2127 .llseek = seq_lseek,
2128 .release = seq_release,
2131 static int __init gpiolib_debugfs_init(void)
2133 /* /sys/kernel/debug/gpio */
2134 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2135 NULL, NULL, &gpiolib_operations);
2138 subsys_initcall(gpiolib_debugfs_init);
2140 #endif /* DEBUG_FS */