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>
18 #include <linux/pinctrl/consumer.h>
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/gpio.h>
25 /* Implementation infrastructure for GPIO interfaces.
27 * The GPIO programming interface allows for inlining speed-critical
28 * get/set operations for common cases, so that access to SOC-integrated
29 * GPIOs can sometimes cost only an instruction or two per bit.
33 /* When debugging, extend minimal trust to callers and platform code.
34 * Also emit diagnostic messages that may help initial bringup, when
35 * board setup or driver bugs are most common.
37 * Otherwise, minimize overhead in what may be bitbanging codepaths.
40 #define extra_checks 1
42 #define extra_checks 0
45 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
46 * While any GPIO is requested, its gpio_chip is not removable;
47 * each GPIO's "requested" flag serves as a lock and refcount.
49 DEFINE_SPINLOCK(gpio_lock);
51 static DEFINE_MUTEX(gpio_lookup_lock);
52 static LIST_HEAD(gpio_lookup_list);
53 LIST_HEAD(gpio_chips);
56 static void gpiochip_free_hogs(struct gpio_chip *chip);
57 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
60 static inline void desc_set_label(struct gpio_desc *d, const char *label)
66 * Convert a GPIO number to its descriptor
68 struct gpio_desc *gpio_to_desc(unsigned gpio)
70 struct gpio_chip *chip;
73 spin_lock_irqsave(&gpio_lock, flags);
75 list_for_each_entry(chip, &gpio_chips, list) {
76 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
77 spin_unlock_irqrestore(&gpio_lock, flags);
78 return &chip->desc[gpio - chip->base];
82 spin_unlock_irqrestore(&gpio_lock, flags);
84 if (!gpio_is_valid(gpio))
85 WARN(1, "invalid GPIO %d\n", gpio);
89 EXPORT_SYMBOL_GPL(gpio_to_desc);
92 * Get the GPIO descriptor corresponding to the given hw number for this chip.
94 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
97 if (hwnum >= chip->ngpio)
98 return ERR_PTR(-EINVAL);
100 return &chip->desc[hwnum];
104 * Convert a GPIO descriptor to the integer namespace.
105 * This should disappear in the future but is needed since we still
106 * use GPIO numbers for error messages and sysfs nodes
108 int desc_to_gpio(const struct gpio_desc *desc)
110 return desc->chip->base + (desc - &desc->chip->desc[0]);
112 EXPORT_SYMBOL_GPL(desc_to_gpio);
116 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
117 * @desc: descriptor to return the chip of
119 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
121 return desc ? desc->chip : NULL;
123 EXPORT_SYMBOL_GPL(gpiod_to_chip);
125 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
126 static int gpiochip_find_base(int ngpio)
128 struct gpio_chip *chip;
129 int base = ARCH_NR_GPIOS - ngpio;
131 list_for_each_entry_reverse(chip, &gpio_chips, list) {
132 /* found a free space? */
133 if (chip->base + chip->ngpio <= base)
136 /* nope, check the space right before the chip */
137 base = chip->base - ngpio;
140 if (gpio_is_valid(base)) {
141 pr_debug("%s: found new base at %d\n", __func__, base);
144 pr_err("%s: cannot find free range\n", __func__);
150 * gpiod_get_direction - return the current direction of a GPIO
151 * @desc: GPIO to get the direction of
153 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
155 * This function may sleep if gpiod_cansleep() is true.
157 int gpiod_get_direction(struct gpio_desc *desc)
159 struct gpio_chip *chip;
161 int status = -EINVAL;
163 chip = gpiod_to_chip(desc);
164 offset = gpio_chip_hwgpio(desc);
166 if (!chip->get_direction)
169 status = chip->get_direction(chip, offset);
171 /* GPIOF_DIR_IN, or other positive */
173 clear_bit(FLAG_IS_OUT, &desc->flags);
177 set_bit(FLAG_IS_OUT, &desc->flags);
181 EXPORT_SYMBOL_GPL(gpiod_get_direction);
184 * Add a new chip to the global chips list, keeping the list of chips sorted
185 * by range(means [base, base + ngpio - 1]) order.
187 * Return -EBUSY if the new chip overlaps with some other chip's integer
190 static int gpiochip_add_to_list(struct gpio_chip *chip)
192 struct list_head *pos;
193 struct gpio_chip *iterator;
194 struct gpio_chip *previous = NULL;
196 if (list_empty(&gpio_chips)) {
197 pos = gpio_chips.next;
201 list_for_each(pos, &gpio_chips) {
202 iterator = list_entry(pos, struct gpio_chip, list);
203 if (iterator->base >= chip->base + chip->ngpio) {
205 * Iterator is the first GPIO chip so there is no
208 if (previous == NULL) {
212 * We found a valid range(means
213 * [base, base + ngpio - 1]) between previous
216 if (previous->base + previous->ngpio
224 /* We are beyond the last chip in the list */
225 if (iterator->base + iterator->ngpio <= chip->base)
228 dev_err(chip->parent,
229 "GPIO integer space overlap, cannot add chip\n");
233 list_add_tail(&chip->list, pos);
238 * Convert a GPIO name to its descriptor
240 static struct gpio_desc *gpio_name_to_desc(const char * const name)
242 struct gpio_chip *chip;
245 spin_lock_irqsave(&gpio_lock, flags);
247 list_for_each_entry(chip, &gpio_chips, list) {
250 for (i = 0; i != chip->ngpio; ++i) {
251 struct gpio_desc *gpio = &chip->desc[i];
253 if (!gpio->name || !name)
256 if (!strcmp(gpio->name, name)) {
257 spin_unlock_irqrestore(&gpio_lock, flags);
263 spin_unlock_irqrestore(&gpio_lock, flags);
269 * Takes the names from gc->names and checks if they are all unique. If they
270 * are, they are assigned to their gpio descriptors.
272 * Warning if one of the names is already used for a different GPIO.
274 static int gpiochip_set_desc_names(struct gpio_chip *gc)
281 /* First check all names if they are unique */
282 for (i = 0; i != gc->ngpio; ++i) {
283 struct gpio_desc *gpio;
285 gpio = gpio_name_to_desc(gc->names[i]);
287 dev_warn(gc->parent, "Detected name collision for "
292 /* Then add all names to the GPIO descriptors */
293 for (i = 0; i != gc->ngpio; ++i)
294 gc->desc[i].name = gc->names[i];
300 * gpiochip_add() - register a gpio_chip
301 * @chip: the chip to register, with chip->base initialized
302 * Context: potentially before irqs will work
304 * Returns a negative errno if the chip can't be registered, such as
305 * because the chip->base is invalid or already associated with a
306 * different chip. Otherwise it returns zero as a success code.
308 * When gpiochip_add() is called very early during boot, so that GPIOs
309 * can be freely used, the chip->parent device must be registered before
310 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
311 * for GPIOs will fail rudely.
313 * If chip->base is negative, this requests dynamic assignment of
314 * a range of valid GPIOs.
316 int gpiochip_add(struct gpio_chip *chip)
321 int base = chip->base;
322 struct gpio_desc *descs;
324 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
328 if (chip->ngpio == 0) {
329 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
333 spin_lock_irqsave(&gpio_lock, flags);
336 base = gpiochip_find_base(chip->ngpio);
339 spin_unlock_irqrestore(&gpio_lock, flags);
345 status = gpiochip_add_to_list(chip);
347 spin_unlock_irqrestore(&gpio_lock, flags);
351 for (id = 0; id < chip->ngpio; id++) {
352 struct gpio_desc *desc = &descs[id];
356 /* REVISIT: most hardware initializes GPIOs as inputs (often
357 * with pullups enabled) so power usage is minimized. Linux
358 * code should set the gpio direction first thing; but until
359 * it does, and in case chip->get_direction is not set, we may
360 * expose the wrong direction in sysfs.
362 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
367 spin_unlock_irqrestore(&gpio_lock, flags);
369 #ifdef CONFIG_PINCTRL
370 INIT_LIST_HEAD(&chip->pin_ranges);
373 if (!chip->owner && chip->parent && chip->parent->driver)
374 chip->owner = chip->parent->driver->owner;
376 status = gpiochip_set_desc_names(chip);
378 goto err_remove_from_list;
380 status = of_gpiochip_add(chip);
382 goto err_remove_chip;
384 acpi_gpiochip_add(chip);
386 status = gpiochip_sysfs_register(chip);
388 goto err_remove_chip;
390 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
391 chip->base, chip->base + chip->ngpio - 1,
392 chip->label ? : "generic");
397 acpi_gpiochip_remove(chip);
398 gpiochip_free_hogs(chip);
399 of_gpiochip_remove(chip);
400 err_remove_from_list:
401 spin_lock_irqsave(&gpio_lock, flags);
402 list_del(&chip->list);
403 spin_unlock_irqrestore(&gpio_lock, flags);
408 /* failures here can mean systems won't boot... */
409 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
410 chip->base, chip->base + chip->ngpio - 1,
411 chip->label ? : "generic");
414 EXPORT_SYMBOL_GPL(gpiochip_add);
417 * gpiochip_remove() - unregister a gpio_chip
418 * @chip: the chip to unregister
420 * A gpio_chip with any GPIOs still requested may not be removed.
422 void gpiochip_remove(struct gpio_chip *chip)
424 struct gpio_desc *desc;
427 bool requested = false;
429 gpiochip_sysfs_unregister(chip);
431 gpiochip_irqchip_remove(chip);
433 acpi_gpiochip_remove(chip);
434 gpiochip_remove_pin_ranges(chip);
435 gpiochip_free_hogs(chip);
436 of_gpiochip_remove(chip);
438 spin_lock_irqsave(&gpio_lock, flags);
439 for (id = 0; id < chip->ngpio; id++) {
440 desc = &chip->desc[id];
442 if (test_bit(FLAG_REQUESTED, &desc->flags))
445 list_del(&chip->list);
446 spin_unlock_irqrestore(&gpio_lock, flags);
449 dev_crit(chip->parent,
450 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
455 EXPORT_SYMBOL_GPL(gpiochip_remove);
458 * gpiochip_find() - iterator for locating a specific gpio_chip
459 * @data: data to pass to match function
460 * @callback: Callback function to check gpio_chip
462 * Similar to bus_find_device. It returns a reference to a gpio_chip as
463 * determined by a user supplied @match callback. The callback should return
464 * 0 if the device doesn't match and non-zero if it does. If the callback is
465 * non-zero, this function will return to the caller and not iterate over any
468 struct gpio_chip *gpiochip_find(void *data,
469 int (*match)(struct gpio_chip *chip,
472 struct gpio_chip *chip;
475 spin_lock_irqsave(&gpio_lock, flags);
476 list_for_each_entry(chip, &gpio_chips, list)
477 if (match(chip, data))
481 if (&chip->list == &gpio_chips)
483 spin_unlock_irqrestore(&gpio_lock, flags);
487 EXPORT_SYMBOL_GPL(gpiochip_find);
489 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
491 const char *name = data;
493 return !strcmp(chip->label, name);
496 static struct gpio_chip *find_chip_by_name(const char *name)
498 return gpiochip_find((void *)name, gpiochip_match_name);
501 #ifdef CONFIG_GPIOLIB_IRQCHIP
504 * The following is irqchip helper code for gpiochips.
508 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
509 * @gpiochip: the gpiochip to set the irqchip chain to
510 * @irqchip: the irqchip to chain to the gpiochip
511 * @parent_irq: the irq number corresponding to the parent IRQ for this
513 * @parent_handler: the parent interrupt handler for the accumulated IRQ
514 * coming out of the gpiochip. If the interrupt is nested rather than
515 * cascaded, pass NULL in this handler argument
517 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
518 struct irq_chip *irqchip,
520 irq_flow_handler_t parent_handler)
524 if (!gpiochip->irqdomain) {
525 chip_err(gpiochip, "called %s before setting up irqchip\n",
530 if (parent_handler) {
531 if (gpiochip->can_sleep) {
533 "you cannot have chained interrupts on a "
534 "chip that may sleep\n");
538 * The parent irqchip is already using the chip_data for this
539 * irqchip, so our callbacks simply use the handler_data.
541 irq_set_chained_handler_and_data(parent_irq, parent_handler,
544 gpiochip->irq_parent = parent_irq;
547 /* Set the parent IRQ for all affected IRQs */
548 for (offset = 0; offset < gpiochip->ngpio; offset++)
549 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
552 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
555 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
556 * @d: the irqdomain used by this irqchip
557 * @irq: the global irq number used by this GPIO irqchip irq
558 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
560 * This function will set up the mapping for a certain IRQ line on a
561 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
562 * stored inside the gpiochip.
564 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
565 irq_hw_number_t hwirq)
567 struct gpio_chip *chip = d->host_data;
569 irq_set_chip_data(irq, chip);
571 * This lock class tells lockdep that GPIO irqs are in a different
572 * category than their parents, so it won't report false recursion.
574 irq_set_lockdep_class(irq, chip->lock_key);
575 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
576 /* Chips that can sleep need nested thread handlers */
577 if (chip->can_sleep && !chip->irq_not_threaded)
578 irq_set_nested_thread(irq, 1);
579 irq_set_noprobe(irq);
582 * No set-up of the hardware will happen if IRQ_TYPE_NONE
583 * is passed as default type.
585 if (chip->irq_default_type != IRQ_TYPE_NONE)
586 irq_set_irq_type(irq, chip->irq_default_type);
591 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
593 struct gpio_chip *chip = d->host_data;
596 irq_set_nested_thread(irq, 0);
597 irq_set_chip_and_handler(irq, NULL, NULL);
598 irq_set_chip_data(irq, NULL);
601 static const struct irq_domain_ops gpiochip_domain_ops = {
602 .map = gpiochip_irq_map,
603 .unmap = gpiochip_irq_unmap,
604 /* Virtually all GPIO irqchips are twocell:ed */
605 .xlate = irq_domain_xlate_twocell,
608 static int gpiochip_irq_reqres(struct irq_data *d)
610 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
612 if (!try_module_get(chip->owner))
615 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
617 "unable to lock HW IRQ %lu for IRQ\n",
619 module_put(chip->owner);
625 static void gpiochip_irq_relres(struct irq_data *d)
627 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
629 gpiochip_unlock_as_irq(chip, d->hwirq);
630 module_put(chip->owner);
633 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
635 return irq_find_mapping(chip->irqdomain, offset);
639 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
640 * @gpiochip: the gpiochip to remove the irqchip from
642 * This is called only from gpiochip_remove()
644 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
648 acpi_gpiochip_free_interrupts(gpiochip);
650 if (gpiochip->irq_parent) {
651 irq_set_chained_handler(gpiochip->irq_parent, NULL);
652 irq_set_handler_data(gpiochip->irq_parent, NULL);
655 /* Remove all IRQ mappings and delete the domain */
656 if (gpiochip->irqdomain) {
657 for (offset = 0; offset < gpiochip->ngpio; offset++)
659 irq_find_mapping(gpiochip->irqdomain, offset));
660 irq_domain_remove(gpiochip->irqdomain);
663 if (gpiochip->irqchip) {
664 gpiochip->irqchip->irq_request_resources = NULL;
665 gpiochip->irqchip->irq_release_resources = NULL;
666 gpiochip->irqchip = NULL;
671 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
672 * @gpiochip: the gpiochip to add the irqchip to
673 * @irqchip: the irqchip to add to the gpiochip
674 * @first_irq: if not dynamically assigned, the base (first) IRQ to
675 * allocate gpiochip irqs from
676 * @handler: the irq handler to use (often a predefined irq core function)
677 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
678 * to have the core avoid setting up any default type in the hardware.
679 * @lock_key: lockdep class
681 * This function closely associates a certain irqchip with a certain
682 * gpiochip, providing an irq domain to translate the local IRQs to
683 * global irqs in the gpiolib core, and making sure that the gpiochip
684 * is passed as chip data to all related functions. Driver callbacks
685 * need to use container_of() to get their local state containers back
686 * from the gpiochip passed as chip data. An irqdomain will be stored
687 * in the gpiochip that shall be used by the driver to handle IRQ number
688 * translation. The gpiochip will need to be initialized and registered
689 * before calling this function.
691 * This function will handle two cell:ed simple IRQs and assumes all
692 * the pins on the gpiochip can generate a unique IRQ. Everything else
693 * need to be open coded.
695 int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
696 struct irq_chip *irqchip,
697 unsigned int first_irq,
698 irq_flow_handler_t handler,
700 struct lock_class_key *lock_key)
702 struct device_node *of_node;
704 unsigned irq_base = 0;
706 if (!gpiochip || !irqchip)
709 if (!gpiochip->parent) {
710 pr_err("missing gpiochip .dev parent pointer\n");
713 of_node = gpiochip->parent->of_node;
714 #ifdef CONFIG_OF_GPIO
716 * If the gpiochip has an assigned OF node this takes precedence
717 * FIXME: get rid of this and use gpiochip->parent->of_node
720 if (gpiochip->of_node)
721 of_node = gpiochip->of_node;
723 gpiochip->irqchip = irqchip;
724 gpiochip->irq_handler = handler;
725 gpiochip->irq_default_type = type;
726 gpiochip->to_irq = gpiochip_to_irq;
727 gpiochip->lock_key = lock_key;
728 gpiochip->irqdomain = irq_domain_add_simple(of_node,
729 gpiochip->ngpio, first_irq,
730 &gpiochip_domain_ops, gpiochip);
731 if (!gpiochip->irqdomain) {
732 gpiochip->irqchip = NULL;
737 * It is possible for a driver to override this, but only if the
738 * alternative functions are both implemented.
740 if (!irqchip->irq_request_resources &&
741 !irqchip->irq_release_resources) {
742 irqchip->irq_request_resources = gpiochip_irq_reqres;
743 irqchip->irq_release_resources = gpiochip_irq_relres;
747 * Prepare the mapping since the irqchip shall be orthogonal to
748 * any gpiochip calls. If the first_irq was zero, this is
749 * necessary to allocate descriptors for all IRQs.
751 for (offset = 0; offset < gpiochip->ngpio; offset++) {
752 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
755 * Store the base into the gpiochip to be used when
756 * unmapping the irqs.
758 gpiochip->irq_base = irq_base;
761 acpi_gpiochip_request_interrupts(gpiochip);
765 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
767 #else /* CONFIG_GPIOLIB_IRQCHIP */
769 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
771 #endif /* CONFIG_GPIOLIB_IRQCHIP */
774 * gpiochip_generic_request() - request the gpio function for a pin
775 * @chip: the gpiochip owning the GPIO
776 * @offset: the offset of the GPIO to request for GPIO function
778 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
780 return pinctrl_request_gpio(chip->base + offset);
782 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
785 * gpiochip_generic_free() - free the gpio function from a pin
786 * @chip: the gpiochip to request the gpio function for
787 * @offset: the offset of the GPIO to free from GPIO function
789 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
791 pinctrl_free_gpio(chip->base + offset);
793 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
795 #ifdef CONFIG_PINCTRL
798 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
799 * @chip: the gpiochip to add the range for
800 * @pctldev: the pin controller to map to
801 * @gpio_offset: the start offset in the current gpio_chip number space
802 * @pin_group: name of the pin group inside the pin controller
804 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
805 struct pinctrl_dev *pctldev,
806 unsigned int gpio_offset, const char *pin_group)
808 struct gpio_pin_range *pin_range;
811 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
813 chip_err(chip, "failed to allocate pin ranges\n");
817 /* Use local offset as range ID */
818 pin_range->range.id = gpio_offset;
819 pin_range->range.gc = chip;
820 pin_range->range.name = chip->label;
821 pin_range->range.base = chip->base + gpio_offset;
822 pin_range->pctldev = pctldev;
824 ret = pinctrl_get_group_pins(pctldev, pin_group,
825 &pin_range->range.pins,
826 &pin_range->range.npins);
832 pinctrl_add_gpio_range(pctldev, &pin_range->range);
834 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
835 gpio_offset, gpio_offset + pin_range->range.npins - 1,
836 pinctrl_dev_get_devname(pctldev), pin_group);
838 list_add_tail(&pin_range->node, &chip->pin_ranges);
842 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
845 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
846 * @chip: the gpiochip to add the range for
847 * @pinctrl_name: the dev_name() of the pin controller to map to
848 * @gpio_offset: the start offset in the current gpio_chip number space
849 * @pin_offset: the start offset in the pin controller number space
850 * @npins: the number of pins from the offset of each pin space (GPIO and
851 * pin controller) to accumulate in this range
853 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
854 unsigned int gpio_offset, unsigned int pin_offset,
857 struct gpio_pin_range *pin_range;
860 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
862 chip_err(chip, "failed to allocate pin ranges\n");
866 /* Use local offset as range ID */
867 pin_range->range.id = gpio_offset;
868 pin_range->range.gc = chip;
869 pin_range->range.name = chip->label;
870 pin_range->range.base = chip->base + gpio_offset;
871 pin_range->range.pin_base = pin_offset;
872 pin_range->range.npins = npins;
873 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
875 if (IS_ERR(pin_range->pctldev)) {
876 ret = PTR_ERR(pin_range->pctldev);
877 chip_err(chip, "could not create pin range\n");
881 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
882 gpio_offset, gpio_offset + npins - 1,
884 pin_offset, pin_offset + npins - 1);
886 list_add_tail(&pin_range->node, &chip->pin_ranges);
890 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
893 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
894 * @chip: the chip to remove all the mappings for
896 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
898 struct gpio_pin_range *pin_range, *tmp;
900 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
901 list_del(&pin_range->node);
902 pinctrl_remove_gpio_range(pin_range->pctldev,
907 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
909 #endif /* CONFIG_PINCTRL */
911 /* These "optional" allocation calls help prevent drivers from stomping
912 * on each other, and help provide better diagnostics in debugfs.
913 * They're called even less than the "set direction" calls.
915 static int __gpiod_request(struct gpio_desc *desc, const char *label)
917 struct gpio_chip *chip = desc->chip;
921 spin_lock_irqsave(&gpio_lock, flags);
923 /* NOTE: gpio_request() can be called in early boot,
924 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
927 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
928 desc_set_label(desc, label ? : "?");
936 /* chip->request may sleep */
937 spin_unlock_irqrestore(&gpio_lock, flags);
938 status = chip->request(chip, gpio_chip_hwgpio(desc));
939 spin_lock_irqsave(&gpio_lock, flags);
942 desc_set_label(desc, NULL);
943 clear_bit(FLAG_REQUESTED, &desc->flags);
947 if (chip->get_direction) {
948 /* chip->get_direction may sleep */
949 spin_unlock_irqrestore(&gpio_lock, flags);
950 gpiod_get_direction(desc);
951 spin_lock_irqsave(&gpio_lock, flags);
955 /* Clear flags that might have been set by the caller before
956 * requesting the GPIO.
958 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
959 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
960 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
962 spin_unlock_irqrestore(&gpio_lock, flags);
966 int gpiod_request(struct gpio_desc *desc, const char *label)
968 int status = -EPROBE_DEFER;
969 struct gpio_chip *chip;
972 pr_warn("%s: invalid GPIO\n", __func__);
980 if (try_module_get(chip->owner)) {
981 status = __gpiod_request(desc, label);
983 module_put(chip->owner);
988 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
993 static bool __gpiod_free(struct gpio_desc *desc)
997 struct gpio_chip *chip;
1001 gpiod_unexport(desc);
1003 spin_lock_irqsave(&gpio_lock, flags);
1006 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1008 spin_unlock_irqrestore(&gpio_lock, flags);
1009 might_sleep_if(chip->can_sleep);
1010 chip->free(chip, gpio_chip_hwgpio(desc));
1011 spin_lock_irqsave(&gpio_lock, flags);
1013 desc_set_label(desc, NULL);
1014 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1015 clear_bit(FLAG_REQUESTED, &desc->flags);
1016 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1017 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1018 clear_bit(FLAG_IS_HOGGED, &desc->flags);
1022 spin_unlock_irqrestore(&gpio_lock, flags);
1026 void gpiod_free(struct gpio_desc *desc)
1028 if (desc && __gpiod_free(desc))
1029 module_put(desc->chip->owner);
1031 WARN_ON(extra_checks);
1035 * gpiochip_is_requested - return string iff signal was requested
1036 * @chip: controller managing the signal
1037 * @offset: of signal within controller's 0..(ngpio - 1) range
1039 * Returns NULL if the GPIO is not currently requested, else a string.
1040 * The string returned is the label passed to gpio_request(); if none has been
1041 * passed it is a meaningless, non-NULL constant.
1043 * This function is for use by GPIO controller drivers. The label can
1044 * help with diagnostics, and knowing that the signal is used as a GPIO
1045 * can help avoid accidentally multiplexing it to another controller.
1047 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1049 struct gpio_desc *desc;
1051 if (offset >= chip->ngpio)
1054 desc = &chip->desc[offset];
1056 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1060 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1063 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1064 * @desc: GPIO descriptor to request
1065 * @label: label for the GPIO
1067 * Function allows GPIO chip drivers to request and use their own GPIO
1068 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1069 * function will not increase reference count of the GPIO chip module. This
1070 * allows the GPIO chip module to be unloaded as needed (we assume that the
1071 * GPIO chip driver handles freeing the GPIOs it has requested).
1073 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1076 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1080 chip_err(chip, "failed to get GPIO descriptor\n");
1084 err = __gpiod_request(desc, label);
1086 return ERR_PTR(err);
1090 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
1093 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1094 * @desc: GPIO descriptor to free
1096 * Function frees the given GPIO requested previously with
1097 * gpiochip_request_own_desc().
1099 void gpiochip_free_own_desc(struct gpio_desc *desc)
1104 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
1106 /* Drivers MUST set GPIO direction before making get/set calls. In
1107 * some cases this is done in early boot, before IRQs are enabled.
1109 * As a rule these aren't called more than once (except for drivers
1110 * using the open-drain emulation idiom) so these are natural places
1111 * to accumulate extra debugging checks. Note that we can't (yet)
1112 * rely on gpio_request() having been called beforehand.
1116 * gpiod_direction_input - set the GPIO direction to input
1117 * @desc: GPIO to set to input
1119 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1120 * be called safely on it.
1122 * Return 0 in case of success, else an error code.
1124 int gpiod_direction_input(struct gpio_desc *desc)
1126 struct gpio_chip *chip;
1127 int status = -EINVAL;
1129 if (!desc || !desc->chip) {
1130 pr_warn("%s: invalid GPIO\n", __func__);
1135 if (!chip->get || !chip->direction_input) {
1137 "%s: missing get() or direction_input() operations\n",
1142 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
1144 clear_bit(FLAG_IS_OUT, &desc->flags);
1146 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1150 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1152 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1154 struct gpio_chip *chip;
1155 int status = -EINVAL;
1157 /* GPIOs used for IRQs shall not be set as output */
1158 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1160 "%s: tried to set a GPIO tied to an IRQ as output\n",
1165 /* Open drain pin should not be driven to 1 */
1166 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1167 return gpiod_direction_input(desc);
1169 /* Open source pin should not be driven to 0 */
1170 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1171 return gpiod_direction_input(desc);
1174 if (!chip->set || !chip->direction_output) {
1176 "%s: missing set() or direction_output() operations\n",
1181 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1183 set_bit(FLAG_IS_OUT, &desc->flags);
1184 trace_gpio_value(desc_to_gpio(desc), 0, value);
1185 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1190 * gpiod_direction_output_raw - set the GPIO direction to output
1191 * @desc: GPIO to set to output
1192 * @value: initial output value of the GPIO
1194 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1195 * be called safely on it. The initial value of the output must be specified
1196 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1198 * Return 0 in case of success, else an error code.
1200 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1202 if (!desc || !desc->chip) {
1203 pr_warn("%s: invalid GPIO\n", __func__);
1206 return _gpiod_direction_output_raw(desc, value);
1208 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1211 * gpiod_direction_output - set the GPIO direction to output
1212 * @desc: GPIO to set to output
1213 * @value: initial output value of the GPIO
1215 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1216 * be called safely on it. The initial value of the output must be specified
1217 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1220 * Return 0 in case of success, else an error code.
1222 int gpiod_direction_output(struct gpio_desc *desc, int value)
1224 if (!desc || !desc->chip) {
1225 pr_warn("%s: invalid GPIO\n", __func__);
1228 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1230 return _gpiod_direction_output_raw(desc, value);
1232 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1235 * gpiod_set_debounce - sets @debounce time for a @gpio
1236 * @gpio: the gpio to set debounce time
1237 * @debounce: debounce time is microseconds
1239 * returns -ENOTSUPP if the controller does not support setting
1242 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1244 struct gpio_chip *chip;
1246 if (!desc || !desc->chip) {
1247 pr_warn("%s: invalid GPIO\n", __func__);
1252 if (!chip->set || !chip->set_debounce) {
1254 "%s: missing set() or set_debounce() operations\n",
1259 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1261 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1264 * gpiod_is_active_low - test whether a GPIO is active-low or not
1265 * @desc: the gpio descriptor to test
1267 * Returns 1 if the GPIO is active-low, 0 otherwise.
1269 int gpiod_is_active_low(const struct gpio_desc *desc)
1271 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1273 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1275 /* I/O calls are only valid after configuration completed; the relevant
1276 * "is this a valid GPIO" error checks should already have been done.
1278 * "Get" operations are often inlinable as reading a pin value register,
1279 * and masking the relevant bit in that register.
1281 * When "set" operations are inlinable, they involve writing that mask to
1282 * one register to set a low value, or a different register to set it high.
1283 * Otherwise locking is needed, so there may be little value to inlining.
1285 *------------------------------------------------------------------------
1287 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1288 * have requested the GPIO. That can include implicit requesting by
1289 * a direction setting call. Marking a gpio as requested locks its chip
1290 * in memory, guaranteeing that these table lookups need no more locking
1291 * and that gpiochip_remove() will fail.
1293 * REVISIT when debugging, consider adding some instrumentation to ensure
1294 * that the GPIO was actually requested.
1297 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
1299 struct gpio_chip *chip;
1304 offset = gpio_chip_hwgpio(desc);
1305 value = chip->get ? chip->get(chip, offset) : -EIO;
1307 * FIXME: fix all drivers to clamp to [0,1] or return negative,
1308 * then change this to:
1309 * value = value < 0 ? value : !!value;
1310 * so we can properly propagate error codes.
1313 trace_gpio_value(desc_to_gpio(desc), 1, value);
1318 * gpiod_get_raw_value() - return a gpio's raw value
1319 * @desc: gpio whose value will be returned
1321 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1322 * its ACTIVE_LOW status, or negative errno on failure.
1324 * This function should be called from contexts where we cannot sleep, and will
1325 * complain if the GPIO chip functions potentially sleep.
1327 int gpiod_get_raw_value(const struct gpio_desc *desc)
1331 /* Should be using gpio_get_value_cansleep() */
1332 WARN_ON(desc->chip->can_sleep);
1333 return _gpiod_get_raw_value(desc);
1335 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1338 * gpiod_get_value() - return a gpio's value
1339 * @desc: gpio whose value will be returned
1341 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1342 * account, or negative errno on failure.
1344 * This function should be called from contexts where we cannot sleep, and will
1345 * complain if the GPIO chip functions potentially sleep.
1347 int gpiod_get_value(const struct gpio_desc *desc)
1352 /* Should be using gpio_get_value_cansleep() */
1353 WARN_ON(desc->chip->can_sleep);
1355 value = _gpiod_get_raw_value(desc);
1359 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1364 EXPORT_SYMBOL_GPL(gpiod_get_value);
1367 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1368 * @desc: gpio descriptor whose state need to be set.
1369 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1371 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1374 struct gpio_chip *chip = desc->chip;
1375 int offset = gpio_chip_hwgpio(desc);
1378 err = chip->direction_input(chip, offset);
1380 clear_bit(FLAG_IS_OUT, &desc->flags);
1382 err = chip->direction_output(chip, offset, 0);
1384 set_bit(FLAG_IS_OUT, &desc->flags);
1386 trace_gpio_direction(desc_to_gpio(desc), value, err);
1389 "%s: Error in set_value for open drain err %d\n",
1394 * _gpio_set_open_source_value() - Set the open source gpio's value.
1395 * @desc: gpio descriptor whose state need to be set.
1396 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1398 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1401 struct gpio_chip *chip = desc->chip;
1402 int offset = gpio_chip_hwgpio(desc);
1405 err = chip->direction_output(chip, offset, 1);
1407 set_bit(FLAG_IS_OUT, &desc->flags);
1409 err = chip->direction_input(chip, offset);
1411 clear_bit(FLAG_IS_OUT, &desc->flags);
1413 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1416 "%s: Error in set_value for open source err %d\n",
1420 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1422 struct gpio_chip *chip;
1425 trace_gpio_value(desc_to_gpio(desc), 0, value);
1426 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1427 _gpio_set_open_drain_value(desc, value);
1428 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1429 _gpio_set_open_source_value(desc, value);
1431 chip->set(chip, gpio_chip_hwgpio(desc), value);
1435 * set multiple outputs on the same chip;
1436 * use the chip's set_multiple function if available;
1437 * otherwise set the outputs sequentially;
1438 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1439 * defines which outputs are to be changed
1440 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1441 * defines the values the outputs specified by mask are to be set to
1443 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1444 unsigned long *mask, unsigned long *bits)
1446 if (chip->set_multiple) {
1447 chip->set_multiple(chip, mask, bits);
1450 for (i = 0; i < chip->ngpio; i++) {
1451 if (mask[BIT_WORD(i)] == 0) {
1452 /* no more set bits in this mask word;
1453 * skip ahead to the next word */
1454 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1457 /* set outputs if the corresponding mask bit is set */
1458 if (__test_and_clear_bit(i, mask))
1459 chip->set(chip, i, test_bit(i, bits));
1464 static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1465 unsigned int array_size,
1466 struct gpio_desc **desc_array,
1471 while (i < array_size) {
1472 struct gpio_chip *chip = desc_array[i]->chip;
1473 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1474 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1478 WARN_ON(chip->can_sleep);
1480 memset(mask, 0, sizeof(mask));
1482 struct gpio_desc *desc = desc_array[i];
1483 int hwgpio = gpio_chip_hwgpio(desc);
1484 int value = value_array[i];
1486 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1488 trace_gpio_value(desc_to_gpio(desc), 0, value);
1490 * collect all normal outputs belonging to the same chip
1491 * open drain and open source outputs are set individually
1493 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1494 _gpio_set_open_drain_value(desc, value);
1495 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1496 _gpio_set_open_source_value(desc, value);
1498 __set_bit(hwgpio, mask);
1500 __set_bit(hwgpio, bits);
1502 __clear_bit(hwgpio, bits);
1506 } while ((i < array_size) && (desc_array[i]->chip == chip));
1507 /* push collected bits to outputs */
1509 gpio_chip_set_multiple(chip, mask, bits);
1514 * gpiod_set_raw_value() - assign a gpio's raw value
1515 * @desc: gpio whose value will be assigned
1516 * @value: value to assign
1518 * Set the raw value of the GPIO, i.e. the value of its physical line without
1519 * regard for its ACTIVE_LOW status.
1521 * This function should be called from contexts where we cannot sleep, and will
1522 * complain if the GPIO chip functions potentially sleep.
1524 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1528 /* Should be using gpio_set_value_cansleep() */
1529 WARN_ON(desc->chip->can_sleep);
1530 _gpiod_set_raw_value(desc, value);
1532 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1535 * gpiod_set_value() - assign a gpio's value
1536 * @desc: gpio whose value will be assigned
1537 * @value: value to assign
1539 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1542 * This function should be called from contexts where we cannot sleep, and will
1543 * complain if the GPIO chip functions potentially sleep.
1545 void gpiod_set_value(struct gpio_desc *desc, int value)
1549 /* Should be using gpio_set_value_cansleep() */
1550 WARN_ON(desc->chip->can_sleep);
1551 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1553 _gpiod_set_raw_value(desc, value);
1555 EXPORT_SYMBOL_GPL(gpiod_set_value);
1558 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1559 * @array_size: number of elements in the descriptor / value arrays
1560 * @desc_array: array of GPIO descriptors whose values will be assigned
1561 * @value_array: array of values to assign
1563 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1564 * without regard for their ACTIVE_LOW status.
1566 * This function should be called from contexts where we cannot sleep, and will
1567 * complain if the GPIO chip functions potentially sleep.
1569 void gpiod_set_raw_array_value(unsigned int array_size,
1570 struct gpio_desc **desc_array, int *value_array)
1574 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1577 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
1580 * gpiod_set_array_value() - assign values to an array of GPIOs
1581 * @array_size: number of elements in the descriptor / value arrays
1582 * @desc_array: array of GPIO descriptors whose values will be assigned
1583 * @value_array: array of values to assign
1585 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1588 * This function should be called from contexts where we cannot sleep, and will
1589 * complain if the GPIO chip functions potentially sleep.
1591 void gpiod_set_array_value(unsigned int array_size,
1592 struct gpio_desc **desc_array, int *value_array)
1596 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1599 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
1602 * gpiod_cansleep() - report whether gpio value access may sleep
1603 * @desc: gpio to check
1606 int gpiod_cansleep(const struct gpio_desc *desc)
1610 return desc->chip->can_sleep;
1612 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1615 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1616 * @desc: gpio whose IRQ will be returned (already requested)
1618 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1621 int gpiod_to_irq(const struct gpio_desc *desc)
1623 struct gpio_chip *chip;
1629 offset = gpio_chip_hwgpio(desc);
1630 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1632 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1635 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1636 * @chip: the chip the GPIO to lock belongs to
1637 * @offset: the offset of the GPIO to lock as IRQ
1639 * This is used directly by GPIO drivers that want to lock down
1640 * a certain GPIO line to be used for IRQs.
1642 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1644 if (offset >= chip->ngpio)
1647 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1649 "%s: tried to flag a GPIO set as output for IRQ\n",
1654 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1657 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1660 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1661 * @chip: the chip the GPIO to lock belongs to
1662 * @offset: the offset of the GPIO to lock as IRQ
1664 * This is used directly by GPIO drivers that want to indicate
1665 * that a certain GPIO is no longer used exclusively for IRQ.
1667 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1669 if (offset >= chip->ngpio)
1672 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1674 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1677 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1678 * @desc: gpio whose value will be returned
1680 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1681 * its ACTIVE_LOW status, or negative errno on failure.
1683 * This function is to be called from contexts that can sleep.
1685 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1687 might_sleep_if(extra_checks);
1690 return _gpiod_get_raw_value(desc);
1692 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1695 * gpiod_get_value_cansleep() - return a gpio's value
1696 * @desc: gpio whose value will be returned
1698 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1699 * account, or negative errno on failure.
1701 * This function is to be called from contexts that can sleep.
1703 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1707 might_sleep_if(extra_checks);
1711 value = _gpiod_get_raw_value(desc);
1715 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1720 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
1723 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1724 * @desc: gpio whose value will be assigned
1725 * @value: value to assign
1727 * Set the raw value of the GPIO, i.e. the value of its physical line without
1728 * regard for its ACTIVE_LOW status.
1730 * This function is to be called from contexts that can sleep.
1732 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
1734 might_sleep_if(extra_checks);
1737 _gpiod_set_raw_value(desc, value);
1739 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
1742 * gpiod_set_value_cansleep() - assign a gpio's value
1743 * @desc: gpio whose value will be assigned
1744 * @value: value to assign
1746 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1749 * This function is to be called from contexts that can sleep.
1751 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1753 might_sleep_if(extra_checks);
1757 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1759 _gpiod_set_raw_value(desc, value);
1761 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1764 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
1765 * @array_size: number of elements in the descriptor / value arrays
1766 * @desc_array: array of GPIO descriptors whose values will be assigned
1767 * @value_array: array of values to assign
1769 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1770 * without regard for their ACTIVE_LOW status.
1772 * This function is to be called from contexts that can sleep.
1774 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1775 struct gpio_desc **desc_array,
1778 might_sleep_if(extra_checks);
1781 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1784 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
1787 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
1788 * @array_size: number of elements in the descriptor / value arrays
1789 * @desc_array: array of GPIO descriptors whose values will be assigned
1790 * @value_array: array of values to assign
1792 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1795 * This function is to be called from contexts that can sleep.
1797 void gpiod_set_array_value_cansleep(unsigned int array_size,
1798 struct gpio_desc **desc_array,
1801 might_sleep_if(extra_checks);
1804 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1807 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
1810 * gpiod_add_lookup_table() - register GPIO device consumers
1811 * @table: table of consumers to register
1813 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
1815 mutex_lock(&gpio_lookup_lock);
1817 list_add_tail(&table->list, &gpio_lookup_list);
1819 mutex_unlock(&gpio_lookup_lock);
1823 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1824 * @table: table of consumers to unregister
1826 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1828 mutex_lock(&gpio_lookup_lock);
1830 list_del(&table->list);
1832 mutex_unlock(&gpio_lookup_lock);
1835 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
1837 enum gpio_lookup_flags *flags)
1839 char prop_name[32]; /* 32 is max size of property name */
1840 enum of_gpio_flags of_flags;
1841 struct gpio_desc *desc;
1844 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1846 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
1849 snprintf(prop_name, sizeof(prop_name), "%s",
1852 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1854 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1861 if (of_flags & OF_GPIO_ACTIVE_LOW)
1862 *flags |= GPIO_ACTIVE_LOW;
1864 if (of_flags & OF_GPIO_SINGLE_ENDED) {
1865 if (of_flags & OF_GPIO_ACTIVE_LOW)
1866 *flags |= GPIO_OPEN_DRAIN;
1868 *flags |= GPIO_OPEN_SOURCE;
1874 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1876 enum gpio_lookup_flags *flags)
1878 struct acpi_device *adev = ACPI_COMPANION(dev);
1879 struct acpi_gpio_info info;
1880 struct gpio_desc *desc;
1884 /* Try first from _DSD */
1885 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1886 if (con_id && strcmp(con_id, "gpios")) {
1887 snprintf(propname, sizeof(propname), "%s-%s",
1888 con_id, gpio_suffixes[i]);
1890 snprintf(propname, sizeof(propname), "%s",
1894 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1895 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1899 /* Then from plain _CRS GPIOs */
1901 if (!acpi_can_fallback_to_crs(adev, con_id))
1902 return ERR_PTR(-ENOENT);
1904 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1909 if (info.active_low)
1910 *flags |= GPIO_ACTIVE_LOW;
1915 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1917 const char *dev_id = dev ? dev_name(dev) : NULL;
1918 struct gpiod_lookup_table *table;
1920 mutex_lock(&gpio_lookup_lock);
1922 list_for_each_entry(table, &gpio_lookup_list, list) {
1923 if (table->dev_id && dev_id) {
1925 * Valid strings on both ends, must be identical to have
1928 if (!strcmp(table->dev_id, dev_id))
1932 * One of the pointers is NULL, so both must be to have
1935 if (dev_id == table->dev_id)
1942 mutex_unlock(&gpio_lookup_lock);
1946 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1948 enum gpio_lookup_flags *flags)
1950 struct gpio_desc *desc = ERR_PTR(-ENOENT);
1951 struct gpiod_lookup_table *table;
1952 struct gpiod_lookup *p;
1954 table = gpiod_find_lookup_table(dev);
1958 for (p = &table->table[0]; p->chip_label; p++) {
1959 struct gpio_chip *chip;
1961 /* idx must always match exactly */
1965 /* If the lookup entry has a con_id, require exact match */
1966 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1969 chip = find_chip_by_name(p->chip_label);
1972 dev_err(dev, "cannot find GPIO chip %s\n",
1974 return ERR_PTR(-ENODEV);
1977 if (chip->ngpio <= p->chip_hwnum) {
1979 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1980 idx, chip->ngpio, chip->label);
1981 return ERR_PTR(-EINVAL);
1984 desc = gpiochip_get_desc(chip, p->chip_hwnum);
1993 static int dt_gpio_count(struct device *dev, const char *con_id)
1999 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2001 snprintf(propname, sizeof(propname), "%s-%s",
2002 con_id, gpio_suffixes[i]);
2004 snprintf(propname, sizeof(propname), "%s",
2007 ret = of_gpio_named_count(dev->of_node, propname);
2014 static int platform_gpio_count(struct device *dev, const char *con_id)
2016 struct gpiod_lookup_table *table;
2017 struct gpiod_lookup *p;
2018 unsigned int count = 0;
2020 table = gpiod_find_lookup_table(dev);
2024 for (p = &table->table[0]; p->chip_label; p++) {
2025 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2026 (!con_id && !p->con_id))
2036 * gpiod_count - return the number of GPIOs associated with a device / function
2037 * or -ENOENT if no GPIO has been assigned to the requested function
2038 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2039 * @con_id: function within the GPIO consumer
2041 int gpiod_count(struct device *dev, const char *con_id)
2043 int count = -ENOENT;
2045 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2046 count = dt_gpio_count(dev, con_id);
2047 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2048 count = acpi_gpio_count(dev, con_id);
2051 count = platform_gpio_count(dev, con_id);
2055 EXPORT_SYMBOL_GPL(gpiod_count);
2058 * gpiod_get - obtain a GPIO for a given GPIO function
2059 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2060 * @con_id: function within the GPIO consumer
2061 * @flags: optional GPIO initialization flags
2063 * Return the GPIO descriptor corresponding to the function con_id of device
2064 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2065 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
2067 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
2068 enum gpiod_flags flags)
2070 return gpiod_get_index(dev, con_id, 0, flags);
2072 EXPORT_SYMBOL_GPL(gpiod_get);
2075 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2076 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2077 * @con_id: function within the GPIO consumer
2078 * @flags: optional GPIO initialization flags
2080 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2081 * the requested function it will return NULL. This is convenient for drivers
2082 * that need to handle optional GPIOs.
2084 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
2086 enum gpiod_flags flags)
2088 return gpiod_get_index_optional(dev, con_id, 0, flags);
2090 EXPORT_SYMBOL_GPL(gpiod_get_optional);
2093 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2094 * @desc: gpio to be setup
2095 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2098 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2100 static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2102 if (lflags & GPIO_ACTIVE_LOW)
2103 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2104 if (lflags & GPIO_OPEN_DRAIN)
2105 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2106 if (lflags & GPIO_OPEN_SOURCE)
2107 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2111 * gpiod_configure_flags - helper function to configure a given GPIO
2112 * @desc: gpio whose value will be assigned
2113 * @con_id: function within the GPIO consumer
2114 * @dflags: gpiod_flags - optional GPIO initialization flags
2116 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2117 * requested function and/or index, or another IS_ERR() code if an error
2118 * occurred while trying to acquire the GPIO.
2120 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2121 enum gpiod_flags dflags)
2125 /* No particular flag request, return here... */
2126 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2127 pr_debug("no flags found for %s\n", con_id);
2132 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2133 status = gpiod_direction_output(desc,
2134 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2136 status = gpiod_direction_input(desc);
2142 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2143 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2144 * @con_id: function within the GPIO consumer
2145 * @idx: index of the GPIO to obtain in the consumer
2146 * @flags: optional GPIO initialization flags
2148 * This variant of gpiod_get() allows to access GPIOs other than the first
2149 * defined one for functions that define several GPIOs.
2151 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2152 * requested function and/or index, or another IS_ERR() code if an error
2153 * occurred while trying to acquire the GPIO.
2155 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2158 enum gpiod_flags flags)
2160 struct gpio_desc *desc = NULL;
2162 enum gpio_lookup_flags lookupflags = 0;
2164 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2167 /* Using device tree? */
2168 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2169 dev_dbg(dev, "using device tree for GPIO lookup\n");
2170 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2171 } else if (ACPI_COMPANION(dev)) {
2172 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2173 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2178 * Either we are not using DT or ACPI, or their lookup did not return
2179 * a result. In that case, use platform lookup as a fallback.
2181 if (!desc || desc == ERR_PTR(-ENOENT)) {
2182 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
2183 desc = gpiod_find(dev, con_id, idx, &lookupflags);
2187 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
2191 gpiod_parse_flags(desc, lookupflags);
2193 status = gpiod_request(desc, con_id);
2195 return ERR_PTR(status);
2197 status = gpiod_configure_flags(desc, con_id, flags);
2199 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2201 return ERR_PTR(status);
2206 EXPORT_SYMBOL_GPL(gpiod_get_index);
2209 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2210 * @fwnode: handle of the firmware node
2211 * @propname: name of the firmware property representing the GPIO
2213 * This function can be used for drivers that get their configuration
2216 * Function properly finds the corresponding GPIO using whatever is the
2217 * underlying firmware interface and then makes sure that the GPIO
2218 * descriptor is requested before it is returned to the caller.
2220 * In case of error an ERR_PTR() is returned.
2222 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2223 const char *propname)
2225 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2226 bool active_low = false;
2227 bool single_ended = false;
2231 return ERR_PTR(-EINVAL);
2233 if (is_of_node(fwnode)) {
2234 enum of_gpio_flags flags;
2236 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
2238 if (!IS_ERR(desc)) {
2239 active_low = flags & OF_GPIO_ACTIVE_LOW;
2240 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2242 } else if (is_acpi_node(fwnode)) {
2243 struct acpi_gpio_info info;
2245 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
2247 active_low = info.active_low;
2254 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2258 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2260 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2263 ret = gpiod_request(desc, NULL);
2265 return ERR_PTR(ret);
2269 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2272 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2274 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2275 * @con_id: function within the GPIO consumer
2276 * @index: index of the GPIO to obtain in the consumer
2277 * @flags: optional GPIO initialization flags
2279 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2280 * specified index was assigned to the requested function it will return NULL.
2281 * This is convenient for drivers that need to handle optional GPIOs.
2283 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
2286 enum gpiod_flags flags)
2288 struct gpio_desc *desc;
2290 desc = gpiod_get_index(dev, con_id, index, flags);
2292 if (PTR_ERR(desc) == -ENOENT)
2298 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
2301 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2302 * @desc: gpio whose value will be assigned
2303 * @name: gpio line name
2304 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2306 * @dflags: gpiod_flags - optional GPIO initialization flags
2308 int gpiod_hog(struct gpio_desc *desc, const char *name,
2309 unsigned long lflags, enum gpiod_flags dflags)
2311 struct gpio_chip *chip;
2312 struct gpio_desc *local_desc;
2316 chip = gpiod_to_chip(desc);
2317 hwnum = gpio_chip_hwgpio(desc);
2319 gpiod_parse_flags(desc, lflags);
2321 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2322 if (IS_ERR(local_desc)) {
2323 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2324 name, chip->label, hwnum);
2325 return PTR_ERR(local_desc);
2328 status = gpiod_configure_flags(desc, name, dflags);
2330 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2331 name, chip->label, hwnum);
2332 gpiochip_free_own_desc(desc);
2336 /* Mark GPIO as hogged so it can be identified and removed later */
2337 set_bit(FLAG_IS_HOGGED, &desc->flags);
2339 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2340 desc_to_gpio(desc), name,
2341 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2342 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2343 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2349 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2350 * @chip: gpio chip to act on
2352 * This is only used by of_gpiochip_remove to free hogged gpios
2354 static void gpiochip_free_hogs(struct gpio_chip *chip)
2358 for (id = 0; id < chip->ngpio; id++) {
2359 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2360 gpiochip_free_own_desc(&chip->desc[id]);
2365 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2366 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2367 * @con_id: function within the GPIO consumer
2368 * @flags: optional GPIO initialization flags
2370 * This function acquires all the GPIOs defined under a given function.
2372 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2373 * no GPIO has been assigned to the requested function, or another IS_ERR()
2374 * code if an error occurred while trying to acquire the GPIOs.
2376 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2378 enum gpiod_flags flags)
2380 struct gpio_desc *desc;
2381 struct gpio_descs *descs;
2384 count = gpiod_count(dev, con_id);
2386 return ERR_PTR(count);
2388 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2391 return ERR_PTR(-ENOMEM);
2393 for (descs->ndescs = 0; descs->ndescs < count; ) {
2394 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2396 gpiod_put_array(descs);
2397 return ERR_CAST(desc);
2399 descs->desc[descs->ndescs] = desc;
2404 EXPORT_SYMBOL_GPL(gpiod_get_array);
2407 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2409 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2410 * @con_id: function within the GPIO consumer
2411 * @flags: optional GPIO initialization flags
2413 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2414 * assigned to the requested function it will return NULL.
2416 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2418 enum gpiod_flags flags)
2420 struct gpio_descs *descs;
2422 descs = gpiod_get_array(dev, con_id, flags);
2423 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2428 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2431 * gpiod_put - dispose of a GPIO descriptor
2432 * @desc: GPIO descriptor to dispose of
2434 * No descriptor can be used after gpiod_put() has been called on it.
2436 void gpiod_put(struct gpio_desc *desc)
2440 EXPORT_SYMBOL_GPL(gpiod_put);
2443 * gpiod_put_array - dispose of multiple GPIO descriptors
2444 * @descs: struct gpio_descs containing an array of descriptors
2446 void gpiod_put_array(struct gpio_descs *descs)
2450 for (i = 0; i < descs->ndescs; i++)
2451 gpiod_put(descs->desc[i]);
2455 EXPORT_SYMBOL_GPL(gpiod_put_array);
2457 #ifdef CONFIG_DEBUG_FS
2459 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2462 unsigned gpio = chip->base;
2463 struct gpio_desc *gdesc = &chip->desc[0];
2467 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2468 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2470 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2476 gpiod_get_direction(gdesc);
2477 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2478 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2479 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2480 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
2481 is_out ? "out" : "in ",
2483 ? (chip->get(chip, i) ? "hi" : "lo")
2485 is_irq ? "IRQ" : " ");
2486 seq_printf(s, "\n");
2490 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2492 unsigned long flags;
2493 struct gpio_chip *chip = NULL;
2494 loff_t index = *pos;
2498 spin_lock_irqsave(&gpio_lock, flags);
2499 list_for_each_entry(chip, &gpio_chips, list)
2501 spin_unlock_irqrestore(&gpio_lock, flags);
2504 spin_unlock_irqrestore(&gpio_lock, flags);
2509 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2511 unsigned long flags;
2512 struct gpio_chip *chip = v;
2515 spin_lock_irqsave(&gpio_lock, flags);
2516 if (list_is_last(&chip->list, &gpio_chips))
2519 ret = list_entry(chip->list.next, struct gpio_chip, list);
2520 spin_unlock_irqrestore(&gpio_lock, flags);
2528 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2532 static int gpiolib_seq_show(struct seq_file *s, void *v)
2534 struct gpio_chip *chip = v;
2537 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2538 chip->base, chip->base + chip->ngpio - 1);
2541 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2544 seq_printf(s, ", %s", chip->label);
2545 if (chip->can_sleep)
2546 seq_printf(s, ", can sleep");
2547 seq_printf(s, ":\n");
2550 chip->dbg_show(s, chip);
2552 gpiolib_dbg_show(s, chip);
2557 static const struct seq_operations gpiolib_seq_ops = {
2558 .start = gpiolib_seq_start,
2559 .next = gpiolib_seq_next,
2560 .stop = gpiolib_seq_stop,
2561 .show = gpiolib_seq_show,
2564 static int gpiolib_open(struct inode *inode, struct file *file)
2566 return seq_open(file, &gpiolib_seq_ops);
2569 static const struct file_operations gpiolib_operations = {
2570 .owner = THIS_MODULE,
2571 .open = gpiolib_open,
2573 .llseek = seq_lseek,
2574 .release = seq_release,
2577 static int __init gpiolib_debugfs_init(void)
2579 /* /sys/kernel/debug/gpio */
2580 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2581 NULL, NULL, &gpiolib_operations);
2584 subsys_initcall(gpiolib_debugfs_init);
2586 #endif /* DEBUG_FS */