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
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 *_chip;
196 /* find where to insert our chip */
197 list_for_each(pos, &gpio_chips) {
198 _chip = list_entry(pos, struct gpio_chip, list);
199 /* shall we insert before _chip? */
200 if (_chip->base >= chip->base + chip->ngpio)
204 /* are we stepping on the chip right before? */
205 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
206 _chip = list_entry(pos->prev, struct gpio_chip, list);
207 if (_chip->base + _chip->ngpio > chip->base) {
209 "GPIO integer space overlap, cannot add chip\n");
215 list_add_tail(&chip->list, pos);
221 * Convert a GPIO name to its descriptor
223 static struct gpio_desc *gpio_name_to_desc(const char * const name)
225 struct gpio_chip *chip;
228 spin_lock_irqsave(&gpio_lock, flags);
230 list_for_each_entry(chip, &gpio_chips, list) {
233 for (i = 0; i != chip->ngpio; ++i) {
234 struct gpio_desc *gpio = &chip->desc[i];
239 if (!strcmp(gpio->name, name)) {
240 spin_unlock_irqrestore(&gpio_lock, flags);
246 spin_unlock_irqrestore(&gpio_lock, flags);
252 * Takes the names from gc->names and checks if they are all unique. If they
253 * are, they are assigned to their gpio descriptors.
255 * Returns -EEXIST if one of the names is already used for a different GPIO.
257 static int gpiochip_set_desc_names(struct gpio_chip *gc)
264 /* First check all names if they are unique */
265 for (i = 0; i != gc->ngpio; ++i) {
266 struct gpio_desc *gpio;
268 gpio = gpio_name_to_desc(gc->names[i]);
270 dev_warn(gc->dev, "Detected name collision for "
275 /* Then add all names to the GPIO descriptors */
276 for (i = 0; i != gc->ngpio; ++i)
277 gc->desc[i].name = gc->names[i];
283 * gpiochip_add() - register a gpio_chip
284 * @chip: the chip to register, with chip->base initialized
285 * Context: potentially before irqs will work
287 * Returns a negative errno if the chip can't be registered, such as
288 * because the chip->base is invalid or already associated with a
289 * different chip. Otherwise it returns zero as a success code.
291 * When gpiochip_add() is called very early during boot, so that GPIOs
292 * can be freely used, the chip->dev device must be registered before
293 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
294 * for GPIOs will fail rudely.
296 * If chip->base is negative, this requests dynamic assignment of
297 * a range of valid GPIOs.
299 int gpiochip_add(struct gpio_chip *chip)
304 int base = chip->base;
305 struct gpio_desc *descs;
307 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
311 spin_lock_irqsave(&gpio_lock, flags);
314 base = gpiochip_find_base(chip->ngpio);
317 spin_unlock_irqrestore(&gpio_lock, flags);
323 status = gpiochip_add_to_list(chip);
325 spin_unlock_irqrestore(&gpio_lock, flags);
329 for (id = 0; id < chip->ngpio; id++) {
330 struct gpio_desc *desc = &descs[id];
334 /* REVISIT: most hardware initializes GPIOs as inputs (often
335 * with pullups enabled) so power usage is minimized. Linux
336 * code should set the gpio direction first thing; but until
337 * it does, and in case chip->get_direction is not set, we may
338 * expose the wrong direction in sysfs.
340 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
345 spin_unlock_irqrestore(&gpio_lock, flags);
347 #ifdef CONFIG_PINCTRL
348 INIT_LIST_HEAD(&chip->pin_ranges);
351 if (!chip->owner && chip->dev && chip->dev->driver)
352 chip->owner = chip->dev->driver->owner;
354 status = gpiochip_set_desc_names(chip);
356 goto err_remove_from_list;
358 status = of_gpiochip_add(chip);
360 goto err_remove_chip;
362 acpi_gpiochip_add(chip);
364 status = gpiochip_sysfs_register(chip);
366 goto err_remove_chip;
368 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
369 chip->base, chip->base + chip->ngpio - 1,
370 chip->label ? : "generic");
375 acpi_gpiochip_remove(chip);
376 gpiochip_free_hogs(chip);
377 of_gpiochip_remove(chip);
378 err_remove_from_list:
379 spin_lock_irqsave(&gpio_lock, flags);
380 list_del(&chip->list);
381 spin_unlock_irqrestore(&gpio_lock, flags);
386 /* failures here can mean systems won't boot... */
387 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
388 chip->base, chip->base + chip->ngpio - 1,
389 chip->label ? : "generic");
392 EXPORT_SYMBOL_GPL(gpiochip_add);
395 * gpiochip_remove() - unregister a gpio_chip
396 * @chip: the chip to unregister
398 * A gpio_chip with any GPIOs still requested may not be removed.
400 void gpiochip_remove(struct gpio_chip *chip)
402 struct gpio_desc *desc;
405 bool requested = false;
407 gpiochip_sysfs_unregister(chip);
409 gpiochip_irqchip_remove(chip);
411 acpi_gpiochip_remove(chip);
412 gpiochip_remove_pin_ranges(chip);
413 gpiochip_free_hogs(chip);
414 of_gpiochip_remove(chip);
416 spin_lock_irqsave(&gpio_lock, flags);
417 for (id = 0; id < chip->ngpio; id++) {
418 desc = &chip->desc[id];
420 if (test_bit(FLAG_REQUESTED, &desc->flags))
423 list_del(&chip->list);
424 spin_unlock_irqrestore(&gpio_lock, flags);
427 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
432 EXPORT_SYMBOL_GPL(gpiochip_remove);
435 * gpiochip_find() - iterator for locating a specific gpio_chip
436 * @data: data to pass to match function
437 * @callback: Callback function to check gpio_chip
439 * Similar to bus_find_device. It returns a reference to a gpio_chip as
440 * determined by a user supplied @match callback. The callback should return
441 * 0 if the device doesn't match and non-zero if it does. If the callback is
442 * non-zero, this function will return to the caller and not iterate over any
445 struct gpio_chip *gpiochip_find(void *data,
446 int (*match)(struct gpio_chip *chip,
449 struct gpio_chip *chip;
452 spin_lock_irqsave(&gpio_lock, flags);
453 list_for_each_entry(chip, &gpio_chips, list)
454 if (match(chip, data))
458 if (&chip->list == &gpio_chips)
460 spin_unlock_irqrestore(&gpio_lock, flags);
464 EXPORT_SYMBOL_GPL(gpiochip_find);
466 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
468 const char *name = data;
470 return !strcmp(chip->label, name);
473 static struct gpio_chip *find_chip_by_name(const char *name)
475 return gpiochip_find((void *)name, gpiochip_match_name);
478 #ifdef CONFIG_GPIOLIB_IRQCHIP
481 * The following is irqchip helper code for gpiochips.
485 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
486 * @gpiochip: the gpiochip to set the irqchip chain to
487 * @irqchip: the irqchip to chain to the gpiochip
488 * @parent_irq: the irq number corresponding to the parent IRQ for this
490 * @parent_handler: the parent interrupt handler for the accumulated IRQ
491 * coming out of the gpiochip. If the interrupt is nested rather than
492 * cascaded, pass NULL in this handler argument
494 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
495 struct irq_chip *irqchip,
497 irq_flow_handler_t parent_handler)
501 if (!gpiochip->irqdomain) {
502 chip_err(gpiochip, "called %s before setting up irqchip\n",
507 if (parent_handler) {
508 if (gpiochip->can_sleep) {
510 "you cannot have chained interrupts on a "
511 "chip that may sleep\n");
515 * The parent irqchip is already using the chip_data for this
516 * irqchip, so our callbacks simply use the handler_data.
518 irq_set_chained_handler_and_data(parent_irq, parent_handler,
521 gpiochip->irq_parent = parent_irq;
524 /* Set the parent IRQ for all affected IRQs */
525 for (offset = 0; offset < gpiochip->ngpio; offset++)
526 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
529 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
532 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
533 * @d: the irqdomain used by this irqchip
534 * @irq: the global irq number used by this GPIO irqchip irq
535 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
537 * This function will set up the mapping for a certain IRQ line on a
538 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
539 * stored inside the gpiochip.
541 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
542 irq_hw_number_t hwirq)
544 struct gpio_chip *chip = d->host_data;
546 irq_set_chip_data(irq, chip);
548 * This lock class tells lockdep that GPIO irqs are in a different
549 * category than their parents, so it won't report false recursion.
551 irq_set_lockdep_class(irq, chip->lock_key);
552 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
553 /* Chips that can sleep need nested thread handlers */
554 if (chip->can_sleep && !chip->irq_not_threaded)
555 irq_set_nested_thread(irq, 1);
556 irq_set_noprobe(irq);
559 * No set-up of the hardware will happen if IRQ_TYPE_NONE
560 * is passed as default type.
562 if (chip->irq_default_type != IRQ_TYPE_NONE)
563 irq_set_irq_type(irq, chip->irq_default_type);
568 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
570 struct gpio_chip *chip = d->host_data;
573 irq_set_nested_thread(irq, 0);
574 irq_set_chip_and_handler(irq, NULL, NULL);
575 irq_set_chip_data(irq, NULL);
578 static const struct irq_domain_ops gpiochip_domain_ops = {
579 .map = gpiochip_irq_map,
580 .unmap = gpiochip_irq_unmap,
581 /* Virtually all GPIO irqchips are twocell:ed */
582 .xlate = irq_domain_xlate_twocell,
585 static int gpiochip_irq_reqres(struct irq_data *d)
587 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
589 if (!try_module_get(chip->owner))
592 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
594 "unable to lock HW IRQ %lu for IRQ\n",
596 module_put(chip->owner);
602 static void gpiochip_irq_relres(struct irq_data *d)
604 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
606 gpiochip_unlock_as_irq(chip, d->hwirq);
607 module_put(chip->owner);
610 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
612 return irq_find_mapping(chip->irqdomain, offset);
616 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
617 * @gpiochip: the gpiochip to remove the irqchip from
619 * This is called only from gpiochip_remove()
621 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
625 acpi_gpiochip_free_interrupts(gpiochip);
627 if (gpiochip->irq_parent) {
628 irq_set_chained_handler(gpiochip->irq_parent, NULL);
629 irq_set_handler_data(gpiochip->irq_parent, NULL);
632 /* Remove all IRQ mappings and delete the domain */
633 if (gpiochip->irqdomain) {
634 for (offset = 0; offset < gpiochip->ngpio; offset++)
636 irq_find_mapping(gpiochip->irqdomain, offset));
637 irq_domain_remove(gpiochip->irqdomain);
640 if (gpiochip->irqchip) {
641 gpiochip->irqchip->irq_request_resources = NULL;
642 gpiochip->irqchip->irq_release_resources = NULL;
643 gpiochip->irqchip = NULL;
648 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
649 * @gpiochip: the gpiochip to add the irqchip to
650 * @irqchip: the irqchip to add to the gpiochip
651 * @first_irq: if not dynamically assigned, the base (first) IRQ to
652 * allocate gpiochip irqs from
653 * @handler: the irq handler to use (often a predefined irq core function)
654 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
655 * to have the core avoid setting up any default type in the hardware.
656 * @lock_key: lockdep class
658 * This function closely associates a certain irqchip with a certain
659 * gpiochip, providing an irq domain to translate the local IRQs to
660 * global irqs in the gpiolib core, and making sure that the gpiochip
661 * is passed as chip data to all related functions. Driver callbacks
662 * need to use container_of() to get their local state containers back
663 * from the gpiochip passed as chip data. An irqdomain will be stored
664 * in the gpiochip that shall be used by the driver to handle IRQ number
665 * translation. The gpiochip will need to be initialized and registered
666 * before calling this function.
668 * This function will handle two cell:ed simple IRQs and assumes all
669 * the pins on the gpiochip can generate a unique IRQ. Everything else
670 * need to be open coded.
672 int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
673 struct irq_chip *irqchip,
674 unsigned int first_irq,
675 irq_flow_handler_t handler,
677 struct lock_class_key *lock_key)
679 struct device_node *of_node;
681 unsigned irq_base = 0;
683 if (!gpiochip || !irqchip)
686 if (!gpiochip->dev) {
687 pr_err("missing gpiochip .dev parent pointer\n");
690 of_node = gpiochip->dev->of_node;
691 #ifdef CONFIG_OF_GPIO
693 * If the gpiochip has an assigned OF node this takes precedence
694 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
696 if (gpiochip->of_node)
697 of_node = gpiochip->of_node;
699 gpiochip->irqchip = irqchip;
700 gpiochip->irq_handler = handler;
701 gpiochip->irq_default_type = type;
702 gpiochip->to_irq = gpiochip_to_irq;
703 gpiochip->lock_key = lock_key;
704 gpiochip->irqdomain = irq_domain_add_simple(of_node,
705 gpiochip->ngpio, first_irq,
706 &gpiochip_domain_ops, gpiochip);
707 if (!gpiochip->irqdomain) {
708 gpiochip->irqchip = NULL;
713 * It is possible for a driver to override this, but only if the
714 * alternative functions are both implemented.
716 if (!irqchip->irq_request_resources &&
717 !irqchip->irq_release_resources) {
718 irqchip->irq_request_resources = gpiochip_irq_reqres;
719 irqchip->irq_release_resources = gpiochip_irq_relres;
723 * Prepare the mapping since the irqchip shall be orthogonal to
724 * any gpiochip calls. If the first_irq was zero, this is
725 * necessary to allocate descriptors for all IRQs.
727 for (offset = 0; offset < gpiochip->ngpio; offset++) {
728 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
731 * Store the base into the gpiochip to be used when
732 * unmapping the irqs.
734 gpiochip->irq_base = irq_base;
737 acpi_gpiochip_request_interrupts(gpiochip);
741 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
743 #else /* CONFIG_GPIOLIB_IRQCHIP */
745 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
747 #endif /* CONFIG_GPIOLIB_IRQCHIP */
750 * gpiochip_generic_request() - request the gpio function for a pin
751 * @chip: the gpiochip owning the GPIO
752 * @offset: the offset of the GPIO to request for GPIO function
754 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
756 return pinctrl_request_gpio(chip->base + offset);
758 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
761 * gpiochip_generic_free() - free the gpio function from a pin
762 * @chip: the gpiochip to request the gpio function for
763 * @offset: the offset of the GPIO to free from GPIO function
765 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
767 pinctrl_free_gpio(chip->base + offset);
769 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
771 #ifdef CONFIG_PINCTRL
774 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
775 * @chip: the gpiochip to add the range for
776 * @pctldev: the pin controller to map to
777 * @gpio_offset: the start offset in the current gpio_chip number space
778 * @pin_group: name of the pin group inside the pin controller
780 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
781 struct pinctrl_dev *pctldev,
782 unsigned int gpio_offset, const char *pin_group)
784 struct gpio_pin_range *pin_range;
787 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
789 chip_err(chip, "failed to allocate pin ranges\n");
793 /* Use local offset as range ID */
794 pin_range->range.id = gpio_offset;
795 pin_range->range.gc = chip;
796 pin_range->range.name = chip->label;
797 pin_range->range.base = chip->base + gpio_offset;
798 pin_range->pctldev = pctldev;
800 ret = pinctrl_get_group_pins(pctldev, pin_group,
801 &pin_range->range.pins,
802 &pin_range->range.npins);
808 pinctrl_add_gpio_range(pctldev, &pin_range->range);
810 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
811 gpio_offset, gpio_offset + pin_range->range.npins - 1,
812 pinctrl_dev_get_devname(pctldev), pin_group);
814 list_add_tail(&pin_range->node, &chip->pin_ranges);
818 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
821 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
822 * @chip: the gpiochip to add the range for
823 * @pinctrl_name: the dev_name() of the pin controller to map to
824 * @gpio_offset: the start offset in the current gpio_chip number space
825 * @pin_offset: the start offset in the pin controller number space
826 * @npins: the number of pins from the offset of each pin space (GPIO and
827 * pin controller) to accumulate in this range
829 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
830 unsigned int gpio_offset, unsigned int pin_offset,
833 struct gpio_pin_range *pin_range;
836 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
838 chip_err(chip, "failed to allocate pin ranges\n");
842 /* Use local offset as range ID */
843 pin_range->range.id = gpio_offset;
844 pin_range->range.gc = chip;
845 pin_range->range.name = chip->label;
846 pin_range->range.base = chip->base + gpio_offset;
847 pin_range->range.pin_base = pin_offset;
848 pin_range->range.npins = npins;
849 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
851 if (IS_ERR(pin_range->pctldev)) {
852 ret = PTR_ERR(pin_range->pctldev);
853 chip_err(chip, "could not create pin range\n");
857 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
858 gpio_offset, gpio_offset + npins - 1,
860 pin_offset, pin_offset + npins - 1);
862 list_add_tail(&pin_range->node, &chip->pin_ranges);
866 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
869 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
870 * @chip: the chip to remove all the mappings for
872 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
874 struct gpio_pin_range *pin_range, *tmp;
876 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
877 list_del(&pin_range->node);
878 pinctrl_remove_gpio_range(pin_range->pctldev,
883 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
885 #endif /* CONFIG_PINCTRL */
887 /* These "optional" allocation calls help prevent drivers from stomping
888 * on each other, and help provide better diagnostics in debugfs.
889 * They're called even less than the "set direction" calls.
891 static int __gpiod_request(struct gpio_desc *desc, const char *label)
893 struct gpio_chip *chip = desc->chip;
897 spin_lock_irqsave(&gpio_lock, flags);
899 /* NOTE: gpio_request() can be called in early boot,
900 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
903 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
904 desc_set_label(desc, label ? : "?");
912 /* chip->request may sleep */
913 spin_unlock_irqrestore(&gpio_lock, flags);
914 status = chip->request(chip, gpio_chip_hwgpio(desc));
915 spin_lock_irqsave(&gpio_lock, flags);
918 desc_set_label(desc, NULL);
919 clear_bit(FLAG_REQUESTED, &desc->flags);
923 if (chip->get_direction) {
924 /* chip->get_direction may sleep */
925 spin_unlock_irqrestore(&gpio_lock, flags);
926 gpiod_get_direction(desc);
927 spin_lock_irqsave(&gpio_lock, flags);
931 /* Clear flags that might have been set by the caller before
932 * requesting the GPIO.
934 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
935 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
936 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
938 spin_unlock_irqrestore(&gpio_lock, flags);
942 int gpiod_request(struct gpio_desc *desc, const char *label)
944 int status = -EPROBE_DEFER;
945 struct gpio_chip *chip;
948 pr_warn("%s: invalid GPIO\n", __func__);
956 if (try_module_get(chip->owner)) {
957 status = __gpiod_request(desc, label);
959 module_put(chip->owner);
964 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
969 static bool __gpiod_free(struct gpio_desc *desc)
973 struct gpio_chip *chip;
977 gpiod_unexport(desc);
979 spin_lock_irqsave(&gpio_lock, flags);
982 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
984 spin_unlock_irqrestore(&gpio_lock, flags);
985 might_sleep_if(chip->can_sleep);
986 chip->free(chip, gpio_chip_hwgpio(desc));
987 spin_lock_irqsave(&gpio_lock, flags);
989 desc_set_label(desc, NULL);
990 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
991 clear_bit(FLAG_REQUESTED, &desc->flags);
992 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
993 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
994 clear_bit(FLAG_IS_HOGGED, &desc->flags);
998 spin_unlock_irqrestore(&gpio_lock, flags);
1002 void gpiod_free(struct gpio_desc *desc)
1004 if (desc && __gpiod_free(desc))
1005 module_put(desc->chip->owner);
1007 WARN_ON(extra_checks);
1011 * gpiochip_is_requested - return string iff signal was requested
1012 * @chip: controller managing the signal
1013 * @offset: of signal within controller's 0..(ngpio - 1) range
1015 * Returns NULL if the GPIO is not currently requested, else a string.
1016 * The string returned is the label passed to gpio_request(); if none has been
1017 * passed it is a meaningless, non-NULL constant.
1019 * This function is for use by GPIO controller drivers. The label can
1020 * help with diagnostics, and knowing that the signal is used as a GPIO
1021 * can help avoid accidentally multiplexing it to another controller.
1023 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1025 struct gpio_desc *desc;
1027 if (offset >= chip->ngpio)
1030 desc = &chip->desc[offset];
1032 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1036 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1039 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1040 * @desc: GPIO descriptor to request
1041 * @label: label for the GPIO
1043 * Function allows GPIO chip drivers to request and use their own GPIO
1044 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1045 * function will not increase reference count of the GPIO chip module. This
1046 * allows the GPIO chip module to be unloaded as needed (we assume that the
1047 * GPIO chip driver handles freeing the GPIOs it has requested).
1049 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1052 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1056 chip_err(chip, "failed to get GPIO descriptor\n");
1060 err = __gpiod_request(desc, label);
1062 return ERR_PTR(err);
1066 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
1069 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1070 * @desc: GPIO descriptor to free
1072 * Function frees the given GPIO requested previously with
1073 * gpiochip_request_own_desc().
1075 void gpiochip_free_own_desc(struct gpio_desc *desc)
1080 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
1082 /* Drivers MUST set GPIO direction before making get/set calls. In
1083 * some cases this is done in early boot, before IRQs are enabled.
1085 * As a rule these aren't called more than once (except for drivers
1086 * using the open-drain emulation idiom) so these are natural places
1087 * to accumulate extra debugging checks. Note that we can't (yet)
1088 * rely on gpio_request() having been called beforehand.
1092 * gpiod_direction_input - set the GPIO direction to input
1093 * @desc: GPIO to set to input
1095 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1096 * be called safely on it.
1098 * Return 0 in case of success, else an error code.
1100 int gpiod_direction_input(struct gpio_desc *desc)
1102 struct gpio_chip *chip;
1103 int status = -EINVAL;
1105 if (!desc || !desc->chip) {
1106 pr_warn("%s: invalid GPIO\n", __func__);
1111 if (!chip->get || !chip->direction_input) {
1113 "%s: missing get() or direction_input() operations\n",
1118 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
1120 clear_bit(FLAG_IS_OUT, &desc->flags);
1122 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1126 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1128 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1130 struct gpio_chip *chip;
1131 int status = -EINVAL;
1133 /* GPIOs used for IRQs shall not be set as output */
1134 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1136 "%s: tried to set a GPIO tied to an IRQ as output\n",
1141 /* Open drain pin should not be driven to 1 */
1142 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1143 return gpiod_direction_input(desc);
1145 /* Open source pin should not be driven to 0 */
1146 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1147 return gpiod_direction_input(desc);
1150 if (!chip->set || !chip->direction_output) {
1152 "%s: missing set() or direction_output() operations\n",
1157 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1159 set_bit(FLAG_IS_OUT, &desc->flags);
1160 trace_gpio_value(desc_to_gpio(desc), 0, value);
1161 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1166 * gpiod_direction_output_raw - set the GPIO direction to output
1167 * @desc: GPIO to set to output
1168 * @value: initial output value of the GPIO
1170 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1171 * be called safely on it. The initial value of the output must be specified
1172 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1174 * Return 0 in case of success, else an error code.
1176 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1178 if (!desc || !desc->chip) {
1179 pr_warn("%s: invalid GPIO\n", __func__);
1182 return _gpiod_direction_output_raw(desc, value);
1184 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1187 * gpiod_direction_output - set the GPIO direction to output
1188 * @desc: GPIO to set to output
1189 * @value: initial output value of the GPIO
1191 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1192 * be called safely on it. The initial value of the output must be specified
1193 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1196 * Return 0 in case of success, else an error code.
1198 int gpiod_direction_output(struct gpio_desc *desc, int value)
1200 if (!desc || !desc->chip) {
1201 pr_warn("%s: invalid GPIO\n", __func__);
1204 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1206 return _gpiod_direction_output_raw(desc, value);
1208 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1211 * gpiod_set_debounce - sets @debounce time for a @gpio
1212 * @gpio: the gpio to set debounce time
1213 * @debounce: debounce time is microseconds
1215 * returns -ENOTSUPP if the controller does not support setting
1218 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1220 struct gpio_chip *chip;
1222 if (!desc || !desc->chip) {
1223 pr_warn("%s: invalid GPIO\n", __func__);
1228 if (!chip->set || !chip->set_debounce) {
1230 "%s: missing set() or set_debounce() operations\n",
1235 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1237 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1240 * gpiod_is_active_low - test whether a GPIO is active-low or not
1241 * @desc: the gpio descriptor to test
1243 * Returns 1 if the GPIO is active-low, 0 otherwise.
1245 int gpiod_is_active_low(const struct gpio_desc *desc)
1247 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1249 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1251 /* I/O calls are only valid after configuration completed; the relevant
1252 * "is this a valid GPIO" error checks should already have been done.
1254 * "Get" operations are often inlinable as reading a pin value register,
1255 * and masking the relevant bit in that register.
1257 * When "set" operations are inlinable, they involve writing that mask to
1258 * one register to set a low value, or a different register to set it high.
1259 * Otherwise locking is needed, so there may be little value to inlining.
1261 *------------------------------------------------------------------------
1263 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1264 * have requested the GPIO. That can include implicit requesting by
1265 * a direction setting call. Marking a gpio as requested locks its chip
1266 * in memory, guaranteeing that these table lookups need no more locking
1267 * and that gpiochip_remove() will fail.
1269 * REVISIT when debugging, consider adding some instrumentation to ensure
1270 * that the GPIO was actually requested.
1273 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
1275 struct gpio_chip *chip;
1280 offset = gpio_chip_hwgpio(desc);
1281 value = chip->get ? chip->get(chip, offset) : -EIO;
1282 value = value < 0 ? value : !!value;
1283 trace_gpio_value(desc_to_gpio(desc), 1, value);
1288 * gpiod_get_raw_value() - return a gpio's raw value
1289 * @desc: gpio whose value will be returned
1291 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1292 * its ACTIVE_LOW status, or negative errno on failure.
1294 * This function should be called from contexts where we cannot sleep, and will
1295 * complain if the GPIO chip functions potentially sleep.
1297 int gpiod_get_raw_value(const struct gpio_desc *desc)
1301 /* Should be using gpio_get_value_cansleep() */
1302 WARN_ON(desc->chip->can_sleep);
1303 return _gpiod_get_raw_value(desc);
1305 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1308 * gpiod_get_value() - return a gpio's value
1309 * @desc: gpio whose value will be returned
1311 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1312 * account, or negative errno on failure.
1314 * This function should be called from contexts where we cannot sleep, and will
1315 * complain if the GPIO chip functions potentially sleep.
1317 int gpiod_get_value(const struct gpio_desc *desc)
1322 /* Should be using gpio_get_value_cansleep() */
1323 WARN_ON(desc->chip->can_sleep);
1325 value = _gpiod_get_raw_value(desc);
1329 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1334 EXPORT_SYMBOL_GPL(gpiod_get_value);
1337 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1338 * @desc: gpio descriptor whose state need to be set.
1339 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1341 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1344 struct gpio_chip *chip = desc->chip;
1345 int offset = gpio_chip_hwgpio(desc);
1348 err = chip->direction_input(chip, offset);
1350 clear_bit(FLAG_IS_OUT, &desc->flags);
1352 err = chip->direction_output(chip, offset, 0);
1354 set_bit(FLAG_IS_OUT, &desc->flags);
1356 trace_gpio_direction(desc_to_gpio(desc), value, err);
1359 "%s: Error in set_value for open drain err %d\n",
1364 * _gpio_set_open_source_value() - Set the open source gpio's value.
1365 * @desc: gpio descriptor whose state need to be set.
1366 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1368 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1371 struct gpio_chip *chip = desc->chip;
1372 int offset = gpio_chip_hwgpio(desc);
1375 err = chip->direction_output(chip, offset, 1);
1377 set_bit(FLAG_IS_OUT, &desc->flags);
1379 err = chip->direction_input(chip, offset);
1381 clear_bit(FLAG_IS_OUT, &desc->flags);
1383 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1386 "%s: Error in set_value for open source err %d\n",
1390 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1392 struct gpio_chip *chip;
1395 trace_gpio_value(desc_to_gpio(desc), 0, value);
1396 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1397 _gpio_set_open_drain_value(desc, value);
1398 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1399 _gpio_set_open_source_value(desc, value);
1401 chip->set(chip, gpio_chip_hwgpio(desc), value);
1405 * set multiple outputs on the same chip;
1406 * use the chip's set_multiple function if available;
1407 * otherwise set the outputs sequentially;
1408 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1409 * defines which outputs are to be changed
1410 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1411 * defines the values the outputs specified by mask are to be set to
1413 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1414 unsigned long *mask, unsigned long *bits)
1416 if (chip->set_multiple) {
1417 chip->set_multiple(chip, mask, bits);
1420 for (i = 0; i < chip->ngpio; i++) {
1421 if (mask[BIT_WORD(i)] == 0) {
1422 /* no more set bits in this mask word;
1423 * skip ahead to the next word */
1424 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1427 /* set outputs if the corresponding mask bit is set */
1428 if (__test_and_clear_bit(i, mask))
1429 chip->set(chip, i, test_bit(i, bits));
1434 static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1435 unsigned int array_size,
1436 struct gpio_desc **desc_array,
1441 while (i < array_size) {
1442 struct gpio_chip *chip = desc_array[i]->chip;
1443 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1444 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1448 WARN_ON(chip->can_sleep);
1450 memset(mask, 0, sizeof(mask));
1452 struct gpio_desc *desc = desc_array[i];
1453 int hwgpio = gpio_chip_hwgpio(desc);
1454 int value = value_array[i];
1456 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1458 trace_gpio_value(desc_to_gpio(desc), 0, value);
1460 * collect all normal outputs belonging to the same chip
1461 * open drain and open source outputs are set individually
1463 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1464 _gpio_set_open_drain_value(desc, value);
1465 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1466 _gpio_set_open_source_value(desc, value);
1468 __set_bit(hwgpio, mask);
1470 __set_bit(hwgpio, bits);
1472 __clear_bit(hwgpio, bits);
1476 } while ((i < array_size) && (desc_array[i]->chip == chip));
1477 /* push collected bits to outputs */
1479 gpio_chip_set_multiple(chip, mask, bits);
1484 * gpiod_set_raw_value() - assign a gpio's raw value
1485 * @desc: gpio whose value will be assigned
1486 * @value: value to assign
1488 * Set the raw value of the GPIO, i.e. the value of its physical line without
1489 * regard for its ACTIVE_LOW status.
1491 * This function should be called from contexts where we cannot sleep, and will
1492 * complain if the GPIO chip functions potentially sleep.
1494 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1498 /* Should be using gpio_set_value_cansleep() */
1499 WARN_ON(desc->chip->can_sleep);
1500 _gpiod_set_raw_value(desc, value);
1502 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1505 * gpiod_set_value() - assign a gpio's value
1506 * @desc: gpio whose value will be assigned
1507 * @value: value to assign
1509 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1512 * This function should be called from contexts where we cannot sleep, and will
1513 * complain if the GPIO chip functions potentially sleep.
1515 void gpiod_set_value(struct gpio_desc *desc, int value)
1519 /* Should be using gpio_set_value_cansleep() */
1520 WARN_ON(desc->chip->can_sleep);
1521 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1523 _gpiod_set_raw_value(desc, value);
1525 EXPORT_SYMBOL_GPL(gpiod_set_value);
1528 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1529 * @array_size: number of elements in the descriptor / value arrays
1530 * @desc_array: array of GPIO descriptors whose values will be assigned
1531 * @value_array: array of values to assign
1533 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1534 * without regard for their ACTIVE_LOW status.
1536 * This function should be called from contexts where we cannot sleep, and will
1537 * complain if the GPIO chip functions potentially sleep.
1539 void gpiod_set_raw_array_value(unsigned int array_size,
1540 struct gpio_desc **desc_array, int *value_array)
1544 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1547 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
1550 * gpiod_set_array_value() - assign values to an array of GPIOs
1551 * @array_size: number of elements in the descriptor / value arrays
1552 * @desc_array: array of GPIO descriptors whose values will be assigned
1553 * @value_array: array of values to assign
1555 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1558 * This function should be called from contexts where we cannot sleep, and will
1559 * complain if the GPIO chip functions potentially sleep.
1561 void gpiod_set_array_value(unsigned int array_size,
1562 struct gpio_desc **desc_array, int *value_array)
1566 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1569 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
1572 * gpiod_cansleep() - report whether gpio value access may sleep
1573 * @desc: gpio to check
1576 int gpiod_cansleep(const struct gpio_desc *desc)
1580 return desc->chip->can_sleep;
1582 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1585 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1586 * @desc: gpio whose IRQ will be returned (already requested)
1588 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1591 int gpiod_to_irq(const struct gpio_desc *desc)
1593 struct gpio_chip *chip;
1599 offset = gpio_chip_hwgpio(desc);
1600 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1602 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1605 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1606 * @chip: the chip the GPIO to lock belongs to
1607 * @offset: the offset of the GPIO to lock as IRQ
1609 * This is used directly by GPIO drivers that want to lock down
1610 * a certain GPIO line to be used for IRQs.
1612 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1614 if (offset >= chip->ngpio)
1617 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1619 "%s: tried to flag a GPIO set as output for IRQ\n",
1624 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1627 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1630 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1631 * @chip: the chip the GPIO to lock belongs to
1632 * @offset: the offset of the GPIO to lock as IRQ
1634 * This is used directly by GPIO drivers that want to indicate
1635 * that a certain GPIO is no longer used exclusively for IRQ.
1637 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1639 if (offset >= chip->ngpio)
1642 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1644 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1647 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1648 * @desc: gpio whose value will be returned
1650 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1651 * its ACTIVE_LOW status, or negative errno on failure.
1653 * This function is to be called from contexts that can sleep.
1655 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1657 might_sleep_if(extra_checks);
1660 return _gpiod_get_raw_value(desc);
1662 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1665 * gpiod_get_value_cansleep() - return a gpio's value
1666 * @desc: gpio whose value will be returned
1668 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1669 * account, or negative errno on failure.
1671 * This function is to be called from contexts that can sleep.
1673 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1677 might_sleep_if(extra_checks);
1681 value = _gpiod_get_raw_value(desc);
1685 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1690 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
1693 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1694 * @desc: gpio whose value will be assigned
1695 * @value: value to assign
1697 * Set the raw value of the GPIO, i.e. the value of its physical line without
1698 * regard for its ACTIVE_LOW status.
1700 * This function is to be called from contexts that can sleep.
1702 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
1704 might_sleep_if(extra_checks);
1707 _gpiod_set_raw_value(desc, value);
1709 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
1712 * gpiod_set_value_cansleep() - assign a gpio's value
1713 * @desc: gpio whose value will be assigned
1714 * @value: value to assign
1716 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1719 * This function is to be called from contexts that can sleep.
1721 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1723 might_sleep_if(extra_checks);
1727 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1729 _gpiod_set_raw_value(desc, value);
1731 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1734 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
1735 * @array_size: number of elements in the descriptor / value arrays
1736 * @desc_array: array of GPIO descriptors whose values will be assigned
1737 * @value_array: array of values to assign
1739 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1740 * without regard for their ACTIVE_LOW status.
1742 * This function is to be called from contexts that can sleep.
1744 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1745 struct gpio_desc **desc_array,
1748 might_sleep_if(extra_checks);
1751 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1754 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
1757 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
1758 * @array_size: number of elements in the descriptor / value arrays
1759 * @desc_array: array of GPIO descriptors whose values will be assigned
1760 * @value_array: array of values to assign
1762 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1765 * This function is to be called from contexts that can sleep.
1767 void gpiod_set_array_value_cansleep(unsigned int array_size,
1768 struct gpio_desc **desc_array,
1771 might_sleep_if(extra_checks);
1774 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1777 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
1780 * gpiod_add_lookup_table() - register GPIO device consumers
1781 * @table: table of consumers to register
1783 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
1785 mutex_lock(&gpio_lookup_lock);
1787 list_add_tail(&table->list, &gpio_lookup_list);
1789 mutex_unlock(&gpio_lookup_lock);
1793 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1794 * @table: table of consumers to unregister
1796 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1798 mutex_lock(&gpio_lookup_lock);
1800 list_del(&table->list);
1802 mutex_unlock(&gpio_lookup_lock);
1805 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
1807 enum gpio_lookup_flags *flags)
1809 char prop_name[32]; /* 32 is max size of property name */
1810 enum of_gpio_flags of_flags;
1811 struct gpio_desc *desc;
1814 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1816 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
1819 snprintf(prop_name, sizeof(prop_name), "%s",
1822 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1824 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1831 if (of_flags & OF_GPIO_ACTIVE_LOW)
1832 *flags |= GPIO_ACTIVE_LOW;
1834 if (of_flags & OF_GPIO_SINGLE_ENDED) {
1835 if (of_flags & OF_GPIO_ACTIVE_LOW)
1836 *flags |= GPIO_OPEN_DRAIN;
1838 *flags |= GPIO_OPEN_SOURCE;
1844 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1846 enum gpio_lookup_flags *flags)
1848 struct acpi_device *adev = ACPI_COMPANION(dev);
1849 struct acpi_gpio_info info;
1850 struct gpio_desc *desc;
1854 /* Try first from _DSD */
1855 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1856 if (con_id && strcmp(con_id, "gpios")) {
1857 snprintf(propname, sizeof(propname), "%s-%s",
1858 con_id, gpio_suffixes[i]);
1860 snprintf(propname, sizeof(propname), "%s",
1864 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1865 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1869 /* Then from plain _CRS GPIOs */
1871 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1876 if (info.active_low)
1877 *flags |= GPIO_ACTIVE_LOW;
1882 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1884 const char *dev_id = dev ? dev_name(dev) : NULL;
1885 struct gpiod_lookup_table *table;
1887 mutex_lock(&gpio_lookup_lock);
1889 list_for_each_entry(table, &gpio_lookup_list, list) {
1890 if (table->dev_id && dev_id) {
1892 * Valid strings on both ends, must be identical to have
1895 if (!strcmp(table->dev_id, dev_id))
1899 * One of the pointers is NULL, so both must be to have
1902 if (dev_id == table->dev_id)
1909 mutex_unlock(&gpio_lookup_lock);
1913 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1915 enum gpio_lookup_flags *flags)
1917 struct gpio_desc *desc = ERR_PTR(-ENOENT);
1918 struct gpiod_lookup_table *table;
1919 struct gpiod_lookup *p;
1921 table = gpiod_find_lookup_table(dev);
1925 for (p = &table->table[0]; p->chip_label; p++) {
1926 struct gpio_chip *chip;
1928 /* idx must always match exactly */
1932 /* If the lookup entry has a con_id, require exact match */
1933 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1936 chip = find_chip_by_name(p->chip_label);
1939 dev_err(dev, "cannot find GPIO chip %s\n",
1941 return ERR_PTR(-ENODEV);
1944 if (chip->ngpio <= p->chip_hwnum) {
1946 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1947 idx, chip->ngpio, chip->label);
1948 return ERR_PTR(-EINVAL);
1951 desc = gpiochip_get_desc(chip, p->chip_hwnum);
1960 static int dt_gpio_count(struct device *dev, const char *con_id)
1966 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1968 snprintf(propname, sizeof(propname), "%s-%s",
1969 con_id, gpio_suffixes[i]);
1971 snprintf(propname, sizeof(propname), "%s",
1974 ret = of_gpio_named_count(dev->of_node, propname);
1981 static int platform_gpio_count(struct device *dev, const char *con_id)
1983 struct gpiod_lookup_table *table;
1984 struct gpiod_lookup *p;
1985 unsigned int count = 0;
1987 table = gpiod_find_lookup_table(dev);
1991 for (p = &table->table[0]; p->chip_label; p++) {
1992 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1993 (!con_id && !p->con_id))
2003 * gpiod_count - return the number of GPIOs associated with a device / function
2004 * or -ENOENT if no GPIO has been assigned to the requested function
2005 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2006 * @con_id: function within the GPIO consumer
2008 int gpiod_count(struct device *dev, const char *con_id)
2010 int count = -ENOENT;
2012 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2013 count = dt_gpio_count(dev, con_id);
2014 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2015 count = acpi_gpio_count(dev, con_id);
2018 count = platform_gpio_count(dev, con_id);
2022 EXPORT_SYMBOL_GPL(gpiod_count);
2025 * gpiod_get - obtain a GPIO for a given GPIO function
2026 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2027 * @con_id: function within the GPIO consumer
2028 * @flags: optional GPIO initialization flags
2030 * Return the GPIO descriptor corresponding to the function con_id of device
2031 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2032 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
2034 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
2035 enum gpiod_flags flags)
2037 return gpiod_get_index(dev, con_id, 0, flags);
2039 EXPORT_SYMBOL_GPL(gpiod_get);
2042 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2043 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2044 * @con_id: function within the GPIO consumer
2045 * @flags: optional GPIO initialization flags
2047 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2048 * the requested function it will return NULL. This is convenient for drivers
2049 * that need to handle optional GPIOs.
2051 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
2053 enum gpiod_flags flags)
2055 return gpiod_get_index_optional(dev, con_id, 0, flags);
2057 EXPORT_SYMBOL_GPL(gpiod_get_optional);
2060 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2061 * @desc: gpio to be setup
2062 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2065 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2067 static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2069 if (lflags & GPIO_ACTIVE_LOW)
2070 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2071 if (lflags & GPIO_OPEN_DRAIN)
2072 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2073 if (lflags & GPIO_OPEN_SOURCE)
2074 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2078 * gpiod_configure_flags - helper function to configure a given GPIO
2079 * @desc: gpio whose value will be assigned
2080 * @con_id: function within the GPIO consumer
2081 * @dflags: gpiod_flags - optional GPIO initialization flags
2083 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2084 * requested function and/or index, or another IS_ERR() code if an error
2085 * occurred while trying to acquire the GPIO.
2087 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2088 enum gpiod_flags dflags)
2092 /* No particular flag request, return here... */
2093 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2094 pr_debug("no flags found for %s\n", con_id);
2099 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2100 status = gpiod_direction_output(desc,
2101 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2103 status = gpiod_direction_input(desc);
2109 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2110 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2111 * @con_id: function within the GPIO consumer
2112 * @idx: index of the GPIO to obtain in the consumer
2113 * @flags: optional GPIO initialization flags
2115 * This variant of gpiod_get() allows to access GPIOs other than the first
2116 * defined one for functions that define several GPIOs.
2118 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2119 * requested function and/or index, or another IS_ERR() code if an error
2120 * occurred while trying to acquire the GPIO.
2122 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2125 enum gpiod_flags flags)
2127 struct gpio_desc *desc = NULL;
2129 enum gpio_lookup_flags lookupflags = 0;
2131 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2134 /* Using device tree? */
2135 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2136 dev_dbg(dev, "using device tree for GPIO lookup\n");
2137 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2138 } else if (ACPI_COMPANION(dev)) {
2139 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2140 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2145 * Either we are not using DT or ACPI, or their lookup did not return
2146 * a result. In that case, use platform lookup as a fallback.
2148 if (!desc || desc == ERR_PTR(-ENOENT)) {
2149 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
2150 desc = gpiod_find(dev, con_id, idx, &lookupflags);
2154 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
2158 gpiod_parse_flags(desc, lookupflags);
2160 status = gpiod_request(desc, con_id);
2162 return ERR_PTR(status);
2164 status = gpiod_configure_flags(desc, con_id, flags);
2166 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2168 return ERR_PTR(status);
2173 EXPORT_SYMBOL_GPL(gpiod_get_index);
2176 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2177 * @fwnode: handle of the firmware node
2178 * @propname: name of the firmware property representing the GPIO
2180 * This function can be used for drivers that get their configuration
2183 * Function properly finds the corresponding GPIO using whatever is the
2184 * underlying firmware interface and then makes sure that the GPIO
2185 * descriptor is requested before it is returned to the caller.
2187 * In case of error an ERR_PTR() is returned.
2189 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2190 const char *propname)
2192 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2193 bool active_low = false;
2194 bool single_ended = false;
2198 return ERR_PTR(-EINVAL);
2200 if (is_of_node(fwnode)) {
2201 enum of_gpio_flags flags;
2203 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
2205 if (!IS_ERR(desc)) {
2206 active_low = flags & OF_GPIO_ACTIVE_LOW;
2207 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2209 } else if (is_acpi_node(fwnode)) {
2210 struct acpi_gpio_info info;
2212 desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0,
2215 active_low = info.active_low;
2222 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2226 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2228 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2231 ret = gpiod_request(desc, NULL);
2233 return ERR_PTR(ret);
2237 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2240 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2242 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2243 * @con_id: function within the GPIO consumer
2244 * @index: index of the GPIO to obtain in the consumer
2245 * @flags: optional GPIO initialization flags
2247 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2248 * specified index was assigned to the requested function it will return NULL.
2249 * This is convenient for drivers that need to handle optional GPIOs.
2251 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
2254 enum gpiod_flags flags)
2256 struct gpio_desc *desc;
2258 desc = gpiod_get_index(dev, con_id, index, flags);
2260 if (PTR_ERR(desc) == -ENOENT)
2266 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
2269 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2270 * @desc: gpio whose value will be assigned
2271 * @name: gpio line name
2272 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2274 * @dflags: gpiod_flags - optional GPIO initialization flags
2276 int gpiod_hog(struct gpio_desc *desc, const char *name,
2277 unsigned long lflags, enum gpiod_flags dflags)
2279 struct gpio_chip *chip;
2280 struct gpio_desc *local_desc;
2284 chip = gpiod_to_chip(desc);
2285 hwnum = gpio_chip_hwgpio(desc);
2287 gpiod_parse_flags(desc, lflags);
2289 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2290 if (IS_ERR(local_desc)) {
2291 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2292 name, chip->label, hwnum);
2293 return PTR_ERR(local_desc);
2296 status = gpiod_configure_flags(desc, name, dflags);
2298 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2299 name, chip->label, hwnum);
2300 gpiochip_free_own_desc(desc);
2304 /* Mark GPIO as hogged so it can be identified and removed later */
2305 set_bit(FLAG_IS_HOGGED, &desc->flags);
2307 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2308 desc_to_gpio(desc), name,
2309 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2310 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2311 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2317 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2318 * @chip: gpio chip to act on
2320 * This is only used by of_gpiochip_remove to free hogged gpios
2322 static void gpiochip_free_hogs(struct gpio_chip *chip)
2326 for (id = 0; id < chip->ngpio; id++) {
2327 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2328 gpiochip_free_own_desc(&chip->desc[id]);
2333 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2334 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2335 * @con_id: function within the GPIO consumer
2336 * @flags: optional GPIO initialization flags
2338 * This function acquires all the GPIOs defined under a given function.
2340 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2341 * no GPIO has been assigned to the requested function, or another IS_ERR()
2342 * code if an error occurred while trying to acquire the GPIOs.
2344 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2346 enum gpiod_flags flags)
2348 struct gpio_desc *desc;
2349 struct gpio_descs *descs;
2352 count = gpiod_count(dev, con_id);
2354 return ERR_PTR(count);
2356 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2359 return ERR_PTR(-ENOMEM);
2361 for (descs->ndescs = 0; descs->ndescs < count; ) {
2362 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2364 gpiod_put_array(descs);
2365 return ERR_CAST(desc);
2367 descs->desc[descs->ndescs] = desc;
2372 EXPORT_SYMBOL_GPL(gpiod_get_array);
2375 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2377 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2378 * @con_id: function within the GPIO consumer
2379 * @flags: optional GPIO initialization flags
2381 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2382 * assigned to the requested function it will return NULL.
2384 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2386 enum gpiod_flags flags)
2388 struct gpio_descs *descs;
2390 descs = gpiod_get_array(dev, con_id, flags);
2391 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2396 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2399 * gpiod_put - dispose of a GPIO descriptor
2400 * @desc: GPIO descriptor to dispose of
2402 * No descriptor can be used after gpiod_put() has been called on it.
2404 void gpiod_put(struct gpio_desc *desc)
2408 EXPORT_SYMBOL_GPL(gpiod_put);
2411 * gpiod_put_array - dispose of multiple GPIO descriptors
2412 * @descs: struct gpio_descs containing an array of descriptors
2414 void gpiod_put_array(struct gpio_descs *descs)
2418 for (i = 0; i < descs->ndescs; i++)
2419 gpiod_put(descs->desc[i]);
2423 EXPORT_SYMBOL_GPL(gpiod_put_array);
2425 #ifdef CONFIG_DEBUG_FS
2427 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2430 unsigned gpio = chip->base;
2431 struct gpio_desc *gdesc = &chip->desc[0];
2435 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2436 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2438 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2444 gpiod_get_direction(gdesc);
2445 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2446 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2447 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2448 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
2449 is_out ? "out" : "in ",
2451 ? (chip->get(chip, i) ? "hi" : "lo")
2453 is_irq ? "IRQ" : " ");
2454 seq_printf(s, "\n");
2458 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2460 unsigned long flags;
2461 struct gpio_chip *chip = NULL;
2462 loff_t index = *pos;
2466 spin_lock_irqsave(&gpio_lock, flags);
2467 list_for_each_entry(chip, &gpio_chips, list)
2469 spin_unlock_irqrestore(&gpio_lock, flags);
2472 spin_unlock_irqrestore(&gpio_lock, flags);
2477 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2479 unsigned long flags;
2480 struct gpio_chip *chip = v;
2483 spin_lock_irqsave(&gpio_lock, flags);
2484 if (list_is_last(&chip->list, &gpio_chips))
2487 ret = list_entry(chip->list.next, struct gpio_chip, list);
2488 spin_unlock_irqrestore(&gpio_lock, flags);
2496 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2500 static int gpiolib_seq_show(struct seq_file *s, void *v)
2502 struct gpio_chip *chip = v;
2505 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2506 chip->base, chip->base + chip->ngpio - 1);
2509 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2512 seq_printf(s, ", %s", chip->label);
2513 if (chip->can_sleep)
2514 seq_printf(s, ", can sleep");
2515 seq_printf(s, ":\n");
2518 chip->dbg_show(s, chip);
2520 gpiolib_dbg_show(s, chip);
2525 static const struct seq_operations gpiolib_seq_ops = {
2526 .start = gpiolib_seq_start,
2527 .next = gpiolib_seq_next,
2528 .stop = gpiolib_seq_stop,
2529 .show = gpiolib_seq_show,
2532 static int gpiolib_open(struct inode *inode, struct file *file)
2534 return seq_open(file, &gpiolib_seq_ops);
2537 static const struct file_operations gpiolib_operations = {
2538 .owner = THIS_MODULE,
2539 .open = gpiolib_open,
2541 .llseek = seq_lseek,
2542 .release = seq_release,
2545 static int __init gpiolib_debugfs_init(void)
2547 /* /sys/kernel/debug/gpio */
2548 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2549 NULL, NULL, &gpiolib_operations);
2552 subsys_initcall(gpiolib_debugfs_init);
2554 #endif /* DEBUG_FS */