1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/gpio.h>
24 /* Implementation infrastructure for GPIO interfaces.
26 * The GPIO programming interface allows for inlining speed-critical
27 * get/set operations for common cases, so that access to SOC-integrated
28 * GPIOs can sometimes cost only an instruction or two per bit.
32 /* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
39 #define extra_checks 1
41 #define extra_checks 0
44 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
48 DEFINE_SPINLOCK(gpio_lock);
50 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
52 static DEFINE_MUTEX(gpio_lookup_lock);
53 static LIST_HEAD(gpio_lookup_list);
54 LIST_HEAD(gpio_chips);
57 static void gpiochip_free_hogs(struct gpio_chip *chip);
58 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
61 static inline void desc_set_label(struct gpio_desc *d, const char *label)
67 * Convert a GPIO number to its descriptor
69 struct gpio_desc *gpio_to_desc(unsigned gpio)
71 struct gpio_chip *chip;
74 spin_lock_irqsave(&gpio_lock, flags);
76 list_for_each_entry(chip, &gpio_chips, list) {
77 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
78 spin_unlock_irqrestore(&gpio_lock, flags);
79 return &chip->desc[gpio - chip->base];
83 spin_unlock_irqrestore(&gpio_lock, flags);
85 if (!gpio_is_valid(gpio))
86 WARN(1, "invalid GPIO %d\n", gpio);
90 EXPORT_SYMBOL_GPL(gpio_to_desc);
93 * Convert a GPIO name to its descriptor
95 struct gpio_desc *gpio_name_to_desc(const char * const name)
97 struct gpio_chip *chip;
100 spin_lock_irqsave(&gpio_lock, flags);
102 list_for_each_entry(chip, &gpio_chips, list) {
105 for (i = 0; i != chip->ngpio; ++i) {
106 struct gpio_desc *gpio = &chip->desc[i];
111 if (!strcmp(gpio->name, name)) {
112 spin_unlock_irqrestore(&gpio_lock, flags);
118 spin_unlock_irqrestore(&gpio_lock, flags);
122 EXPORT_SYMBOL_GPL(gpio_name_to_desc);
125 * Get the GPIO descriptor corresponding to the given hw number for this chip.
127 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
130 if (hwnum >= chip->ngpio)
131 return ERR_PTR(-EINVAL);
133 return &chip->desc[hwnum];
137 * Convert a GPIO descriptor to the integer namespace.
138 * This should disappear in the future but is needed since we still
139 * use GPIO numbers for error messages and sysfs nodes
141 int desc_to_gpio(const struct gpio_desc *desc)
143 return desc->chip->base + (desc - &desc->chip->desc[0]);
145 EXPORT_SYMBOL_GPL(desc_to_gpio);
149 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
150 * @desc: descriptor to return the chip of
152 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
154 return desc ? desc->chip : NULL;
156 EXPORT_SYMBOL_GPL(gpiod_to_chip);
158 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
159 static int gpiochip_find_base(int ngpio)
161 struct gpio_chip *chip;
162 int base = ARCH_NR_GPIOS - ngpio;
164 list_for_each_entry_reverse(chip, &gpio_chips, list) {
165 /* found a free space? */
166 if (chip->base + chip->ngpio <= base)
169 /* nope, check the space right before the chip */
170 base = chip->base - ngpio;
173 if (gpio_is_valid(base)) {
174 pr_debug("%s: found new base at %d\n", __func__, base);
177 pr_err("%s: cannot find free range\n", __func__);
183 * gpiod_get_direction - return the current direction of a GPIO
184 * @desc: GPIO to get the direction of
186 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
188 * This function may sleep if gpiod_cansleep() is true.
190 int gpiod_get_direction(struct gpio_desc *desc)
192 struct gpio_chip *chip;
194 int status = -EINVAL;
196 chip = gpiod_to_chip(desc);
197 offset = gpio_chip_hwgpio(desc);
199 if (!chip->get_direction)
202 status = chip->get_direction(chip, offset);
204 /* GPIOF_DIR_IN, or other positive */
206 clear_bit(FLAG_IS_OUT, &desc->flags);
210 set_bit(FLAG_IS_OUT, &desc->flags);
214 EXPORT_SYMBOL_GPL(gpiod_get_direction);
217 * Add a new chip to the global chips list, keeping the list of chips sorted
220 * Return -EBUSY if the new chip overlaps with some other chip's integer
223 static int gpiochip_add_to_list(struct gpio_chip *chip)
225 struct list_head *pos;
226 struct gpio_chip *_chip;
229 /* find where to insert our chip */
230 list_for_each(pos, &gpio_chips) {
231 _chip = list_entry(pos, struct gpio_chip, list);
232 /* shall we insert before _chip? */
233 if (_chip->base >= chip->base + chip->ngpio)
237 /* are we stepping on the chip right before? */
238 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
239 _chip = list_entry(pos->prev, struct gpio_chip, list);
240 if (_chip->base + _chip->ngpio > chip->base) {
242 "GPIO integer space overlap, cannot add chip\n");
248 list_add_tail(&chip->list, pos);
254 * Takes the names from gc->names and checks if they are all unique. If they
255 * are, they are assigned to their gpio descriptors.
257 * Returns -EEXIST if one of the names is already used for a different GPIO.
259 static int gpiochip_set_desc_names(struct gpio_chip *gc)
266 /* First check all names if they are unique */
267 for (i = 0; i != gc->ngpio; ++i) {
268 struct gpio_desc *gpio;
270 gpio = gpio_name_to_desc(gc->names[i]);
272 dev_err(gc->dev, "Detected name collision for GPIO name '%s'\n",
278 /* Then add all names to the GPIO descriptors */
279 for (i = 0; i != gc->ngpio; ++i)
280 gc->desc[i].name = gc->names[i];
286 * gpiochip_add() - register a gpio_chip
287 * @chip: the chip to register, with chip->base initialized
288 * Context: potentially before irqs will work
290 * Returns a negative errno if the chip can't be registered, such as
291 * because the chip->base is invalid or already associated with a
292 * different chip. Otherwise it returns zero as a success code.
294 * When gpiochip_add() is called very early during boot, so that GPIOs
295 * can be freely used, the chip->dev device must be registered before
296 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
297 * for GPIOs will fail rudely.
299 * If chip->base is negative, this requests dynamic assignment of
300 * a range of valid GPIOs.
302 int gpiochip_add(struct gpio_chip *chip)
307 int base = chip->base;
308 struct gpio_desc *descs;
310 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
314 spin_lock_irqsave(&gpio_lock, flags);
317 base = gpiochip_find_base(chip->ngpio);
320 spin_unlock_irqrestore(&gpio_lock, flags);
326 status = gpiochip_add_to_list(chip);
328 spin_unlock_irqrestore(&gpio_lock, flags);
332 for (id = 0; id < chip->ngpio; id++) {
333 struct gpio_desc *desc = &descs[id];
337 /* REVISIT: most hardware initializes GPIOs as inputs (often
338 * with pullups enabled) so power usage is minimized. Linux
339 * code should set the gpio direction first thing; but until
340 * it does, and in case chip->get_direction is not set, we may
341 * expose the wrong direction in sysfs.
343 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
348 spin_unlock_irqrestore(&gpio_lock, flags);
350 #ifdef CONFIG_PINCTRL
351 INIT_LIST_HEAD(&chip->pin_ranges);
354 if (!chip->owner && chip->dev && chip->dev->driver)
355 chip->owner = chip->dev->driver->owner;
357 status = gpiochip_set_desc_names(chip);
359 goto err_remove_from_list;
361 status = of_gpiochip_add(chip);
363 goto err_remove_chip;
365 acpi_gpiochip_add(chip);
367 status = gpiochip_sysfs_register(chip);
369 goto err_remove_chip;
371 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
372 chip->base, chip->base + chip->ngpio - 1,
373 chip->label ? : "generic");
378 acpi_gpiochip_remove(chip);
379 gpiochip_free_hogs(chip);
380 of_gpiochip_remove(chip);
381 err_remove_from_list:
382 spin_lock_irqsave(&gpio_lock, flags);
383 list_del(&chip->list);
384 spin_unlock_irqrestore(&gpio_lock, flags);
389 /* failures here can mean systems won't boot... */
390 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
391 chip->base, chip->base + chip->ngpio - 1,
392 chip->label ? : "generic");
395 EXPORT_SYMBOL_GPL(gpiochip_add);
398 * gpiochip_remove() - unregister a gpio_chip
399 * @chip: the chip to unregister
401 * A gpio_chip with any GPIOs still requested may not be removed.
403 void gpiochip_remove(struct gpio_chip *chip)
405 struct gpio_desc *desc;
408 bool requested = false;
410 gpiochip_sysfs_unregister(chip);
412 gpiochip_irqchip_remove(chip);
414 acpi_gpiochip_remove(chip);
415 gpiochip_remove_pin_ranges(chip);
416 gpiochip_free_hogs(chip);
417 of_gpiochip_remove(chip);
419 spin_lock_irqsave(&gpio_lock, flags);
420 for (id = 0; id < chip->ngpio; id++) {
421 desc = &chip->desc[id];
423 if (test_bit(FLAG_REQUESTED, &desc->flags))
426 list_del(&chip->list);
427 spin_unlock_irqrestore(&gpio_lock, flags);
430 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
435 EXPORT_SYMBOL_GPL(gpiochip_remove);
438 * gpiochip_find() - iterator for locating a specific gpio_chip
439 * @data: data to pass to match function
440 * @callback: Callback function to check gpio_chip
442 * Similar to bus_find_device. It returns a reference to a gpio_chip as
443 * determined by a user supplied @match callback. The callback should return
444 * 0 if the device doesn't match and non-zero if it does. If the callback is
445 * non-zero, this function will return to the caller and not iterate over any
448 struct gpio_chip *gpiochip_find(void *data,
449 int (*match)(struct gpio_chip *chip,
452 struct gpio_chip *chip;
455 spin_lock_irqsave(&gpio_lock, flags);
456 list_for_each_entry(chip, &gpio_chips, list)
457 if (match(chip, data))
461 if (&chip->list == &gpio_chips)
463 spin_unlock_irqrestore(&gpio_lock, flags);
467 EXPORT_SYMBOL_GPL(gpiochip_find);
469 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
471 const char *name = data;
473 return !strcmp(chip->label, name);
476 static struct gpio_chip *find_chip_by_name(const char *name)
478 return gpiochip_find((void *)name, gpiochip_match_name);
481 #ifdef CONFIG_GPIOLIB_IRQCHIP
484 * The following is irqchip helper code for gpiochips.
488 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
489 * @gpiochip: the gpiochip to set the irqchip chain to
490 * @irqchip: the irqchip to chain to the gpiochip
491 * @parent_irq: the irq number corresponding to the parent IRQ for this
493 * @parent_handler: the parent interrupt handler for the accumulated IRQ
494 * coming out of the gpiochip. If the interrupt is nested rather than
495 * cascaded, pass NULL in this handler argument
497 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
498 struct irq_chip *irqchip,
500 irq_flow_handler_t parent_handler)
504 if (!gpiochip->irqdomain) {
505 chip_err(gpiochip, "called %s before setting up irqchip\n",
510 if (parent_handler) {
511 if (gpiochip->can_sleep) {
513 "you cannot have chained interrupts on a "
514 "chip that may sleep\n");
518 * The parent irqchip is already using the chip_data for this
519 * irqchip, so our callbacks simply use the handler_data.
521 irq_set_chained_handler_and_data(parent_irq, parent_handler,
524 gpiochip->irq_parent = parent_irq;
527 /* Set the parent IRQ for all affected IRQs */
528 for (offset = 0; offset < gpiochip->ngpio; offset++)
529 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
532 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
535 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
536 * @d: the irqdomain used by this irqchip
537 * @irq: the global irq number used by this GPIO irqchip irq
538 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
540 * This function will set up the mapping for a certain IRQ line on a
541 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
542 * stored inside the gpiochip.
544 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
545 irq_hw_number_t hwirq)
547 struct gpio_chip *chip = d->host_data;
549 irq_set_chip_data(irq, chip);
551 * This lock class tells lockdep that GPIO irqs are in a different
552 * category than their parents, so it won't report false recursion.
554 irq_set_lockdep_class(irq, chip->lock_key);
555 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
556 /* Chips that can sleep need nested thread handlers */
557 if (chip->can_sleep && !chip->irq_not_threaded)
558 irq_set_nested_thread(irq, 1);
559 irq_set_noprobe(irq);
562 * No set-up of the hardware will happen if IRQ_TYPE_NONE
563 * is passed as default type.
565 if (chip->irq_default_type != IRQ_TYPE_NONE)
566 irq_set_irq_type(irq, chip->irq_default_type);
571 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
573 struct gpio_chip *chip = d->host_data;
576 irq_set_nested_thread(irq, 0);
577 irq_set_chip_and_handler(irq, NULL, NULL);
578 irq_set_chip_data(irq, NULL);
581 static const struct irq_domain_ops gpiochip_domain_ops = {
582 .map = gpiochip_irq_map,
583 .unmap = gpiochip_irq_unmap,
584 /* Virtually all GPIO irqchips are twocell:ed */
585 .xlate = irq_domain_xlate_twocell,
588 static int gpiochip_irq_reqres(struct irq_data *d)
590 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
592 if (!try_module_get(chip->owner))
595 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
597 "unable to lock HW IRQ %lu for IRQ\n",
599 module_put(chip->owner);
605 static void gpiochip_irq_relres(struct irq_data *d)
607 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
609 gpiochip_unlock_as_irq(chip, d->hwirq);
610 module_put(chip->owner);
613 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
615 return irq_find_mapping(chip->irqdomain, offset);
619 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
620 * @gpiochip: the gpiochip to remove the irqchip from
622 * This is called only from gpiochip_remove()
624 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
628 acpi_gpiochip_free_interrupts(gpiochip);
630 if (gpiochip->irq_parent) {
631 irq_set_chained_handler(gpiochip->irq_parent, NULL);
632 irq_set_handler_data(gpiochip->irq_parent, NULL);
635 /* Remove all IRQ mappings and delete the domain */
636 if (gpiochip->irqdomain) {
637 for (offset = 0; offset < gpiochip->ngpio; offset++)
639 irq_find_mapping(gpiochip->irqdomain, offset));
640 irq_domain_remove(gpiochip->irqdomain);
643 if (gpiochip->irqchip) {
644 gpiochip->irqchip->irq_request_resources = NULL;
645 gpiochip->irqchip->irq_release_resources = NULL;
646 gpiochip->irqchip = NULL;
651 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
652 * @gpiochip: the gpiochip to add the irqchip to
653 * @irqchip: the irqchip to add to the gpiochip
654 * @first_irq: if not dynamically assigned, the base (first) IRQ to
655 * allocate gpiochip irqs from
656 * @handler: the irq handler to use (often a predefined irq core function)
657 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
658 * to have the core avoid setting up any default type in the hardware.
659 * @lock_key: lockdep class
661 * This function closely associates a certain irqchip with a certain
662 * gpiochip, providing an irq domain to translate the local IRQs to
663 * global irqs in the gpiolib core, and making sure that the gpiochip
664 * is passed as chip data to all related functions. Driver callbacks
665 * need to use container_of() to get their local state containers back
666 * from the gpiochip passed as chip data. An irqdomain will be stored
667 * in the gpiochip that shall be used by the driver to handle IRQ number
668 * translation. The gpiochip will need to be initialized and registered
669 * before calling this function.
671 * This function will handle two cell:ed simple IRQs and assumes all
672 * the pins on the gpiochip can generate a unique IRQ. Everything else
673 * need to be open coded.
675 int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
676 struct irq_chip *irqchip,
677 unsigned int first_irq,
678 irq_flow_handler_t handler,
680 struct lock_class_key *lock_key)
682 struct device_node *of_node;
684 unsigned irq_base = 0;
686 if (!gpiochip || !irqchip)
689 if (!gpiochip->dev) {
690 pr_err("missing gpiochip .dev parent pointer\n");
693 of_node = gpiochip->dev->of_node;
694 #ifdef CONFIG_OF_GPIO
696 * If the gpiochip has an assigned OF node this takes precedence
697 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
699 if (gpiochip->of_node)
700 of_node = gpiochip->of_node;
702 gpiochip->irqchip = irqchip;
703 gpiochip->irq_handler = handler;
704 gpiochip->irq_default_type = type;
705 gpiochip->to_irq = gpiochip_to_irq;
706 gpiochip->lock_key = lock_key;
707 gpiochip->irqdomain = irq_domain_add_simple(of_node,
708 gpiochip->ngpio, first_irq,
709 &gpiochip_domain_ops, gpiochip);
710 if (!gpiochip->irqdomain) {
711 gpiochip->irqchip = NULL;
716 * It is possible for a driver to override this, but only if the
717 * alternative functions are both implemented.
719 if (!irqchip->irq_request_resources &&
720 !irqchip->irq_release_resources) {
721 irqchip->irq_request_resources = gpiochip_irq_reqres;
722 irqchip->irq_release_resources = gpiochip_irq_relres;
726 * Prepare the mapping since the irqchip shall be orthogonal to
727 * any gpiochip calls. If the first_irq was zero, this is
728 * necessary to allocate descriptors for all IRQs.
730 for (offset = 0; offset < gpiochip->ngpio; offset++) {
731 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
734 * Store the base into the gpiochip to be used when
735 * unmapping the irqs.
737 gpiochip->irq_base = irq_base;
740 acpi_gpiochip_request_interrupts(gpiochip);
744 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
746 #else /* CONFIG_GPIOLIB_IRQCHIP */
748 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
750 #endif /* CONFIG_GPIOLIB_IRQCHIP */
752 #ifdef CONFIG_PINCTRL
755 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
756 * @chip: the gpiochip to add the range for
757 * @pctldev: the pin controller to map to
758 * @gpio_offset: the start offset in the current gpio_chip number space
759 * @pin_group: name of the pin group inside the pin controller
761 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
762 struct pinctrl_dev *pctldev,
763 unsigned int gpio_offset, const char *pin_group)
765 struct gpio_pin_range *pin_range;
768 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
770 chip_err(chip, "failed to allocate pin ranges\n");
774 /* Use local offset as range ID */
775 pin_range->range.id = gpio_offset;
776 pin_range->range.gc = chip;
777 pin_range->range.name = chip->label;
778 pin_range->range.base = chip->base + gpio_offset;
779 pin_range->pctldev = pctldev;
781 ret = pinctrl_get_group_pins(pctldev, pin_group,
782 &pin_range->range.pins,
783 &pin_range->range.npins);
789 pinctrl_add_gpio_range(pctldev, &pin_range->range);
791 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
792 gpio_offset, gpio_offset + pin_range->range.npins - 1,
793 pinctrl_dev_get_devname(pctldev), pin_group);
795 list_add_tail(&pin_range->node, &chip->pin_ranges);
799 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
802 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
803 * @chip: the gpiochip to add the range for
804 * @pinctrl_name: the dev_name() of the pin controller to map to
805 * @gpio_offset: the start offset in the current gpio_chip number space
806 * @pin_offset: the start offset in the pin controller number space
807 * @npins: the number of pins from the offset of each pin space (GPIO and
808 * pin controller) to accumulate in this range
810 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
811 unsigned int gpio_offset, unsigned int pin_offset,
814 struct gpio_pin_range *pin_range;
817 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
819 chip_err(chip, "failed to allocate pin ranges\n");
823 /* Use local offset as range ID */
824 pin_range->range.id = gpio_offset;
825 pin_range->range.gc = chip;
826 pin_range->range.name = chip->label;
827 pin_range->range.base = chip->base + gpio_offset;
828 pin_range->range.pin_base = pin_offset;
829 pin_range->range.npins = npins;
830 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
832 if (IS_ERR(pin_range->pctldev)) {
833 ret = PTR_ERR(pin_range->pctldev);
834 chip_err(chip, "could not create pin range\n");
838 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
839 gpio_offset, gpio_offset + npins - 1,
841 pin_offset, pin_offset + npins - 1);
843 list_add_tail(&pin_range->node, &chip->pin_ranges);
847 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
850 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
851 * @chip: the chip to remove all the mappings for
853 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
855 struct gpio_pin_range *pin_range, *tmp;
857 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
858 list_del(&pin_range->node);
859 pinctrl_remove_gpio_range(pin_range->pctldev,
864 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
866 #endif /* CONFIG_PINCTRL */
868 /* These "optional" allocation calls help prevent drivers from stomping
869 * on each other, and help provide better diagnostics in debugfs.
870 * They're called even less than the "set direction" calls.
872 static int __gpiod_request(struct gpio_desc *desc, const char *label)
874 struct gpio_chip *chip = desc->chip;
878 spin_lock_irqsave(&gpio_lock, flags);
880 /* NOTE: gpio_request() can be called in early boot,
881 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
884 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
885 desc_set_label(desc, label ? : "?");
893 /* chip->request may sleep */
894 spin_unlock_irqrestore(&gpio_lock, flags);
895 status = chip->request(chip, gpio_chip_hwgpio(desc));
896 spin_lock_irqsave(&gpio_lock, flags);
899 desc_set_label(desc, NULL);
900 clear_bit(FLAG_REQUESTED, &desc->flags);
904 if (chip->get_direction) {
905 /* chip->get_direction may sleep */
906 spin_unlock_irqrestore(&gpio_lock, flags);
907 gpiod_get_direction(desc);
908 spin_lock_irqsave(&gpio_lock, flags);
911 spin_unlock_irqrestore(&gpio_lock, flags);
915 int gpiod_request(struct gpio_desc *desc, const char *label)
917 int status = -EPROBE_DEFER;
918 struct gpio_chip *chip;
921 pr_warn("%s: invalid GPIO\n", __func__);
929 if (try_module_get(chip->owner)) {
930 status = __gpiod_request(desc, label);
932 module_put(chip->owner);
937 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
942 static bool __gpiod_free(struct gpio_desc *desc)
946 struct gpio_chip *chip;
950 gpiod_unexport(desc);
952 spin_lock_irqsave(&gpio_lock, flags);
955 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
957 spin_unlock_irqrestore(&gpio_lock, flags);
958 might_sleep_if(chip->can_sleep);
959 chip->free(chip, gpio_chip_hwgpio(desc));
960 spin_lock_irqsave(&gpio_lock, flags);
962 desc_set_label(desc, NULL);
963 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
964 clear_bit(FLAG_REQUESTED, &desc->flags);
965 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
966 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
967 clear_bit(FLAG_IS_HOGGED, &desc->flags);
971 spin_unlock_irqrestore(&gpio_lock, flags);
975 void gpiod_free(struct gpio_desc *desc)
977 if (desc && __gpiod_free(desc))
978 module_put(desc->chip->owner);
980 WARN_ON(extra_checks);
984 * gpiochip_is_requested - return string iff signal was requested
985 * @chip: controller managing the signal
986 * @offset: of signal within controller's 0..(ngpio - 1) range
988 * Returns NULL if the GPIO is not currently requested, else a string.
989 * The string returned is the label passed to gpio_request(); if none has been
990 * passed it is a meaningless, non-NULL constant.
992 * This function is for use by GPIO controller drivers. The label can
993 * help with diagnostics, and knowing that the signal is used as a GPIO
994 * can help avoid accidentally multiplexing it to another controller.
996 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
998 struct gpio_desc *desc;
1000 if (!GPIO_OFFSET_VALID(chip, offset))
1003 desc = &chip->desc[offset];
1005 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1009 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1012 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1013 * @desc: GPIO descriptor to request
1014 * @label: label for the GPIO
1016 * Function allows GPIO chip drivers to request and use their own GPIO
1017 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1018 * function will not increase reference count of the GPIO chip module. This
1019 * allows the GPIO chip module to be unloaded as needed (we assume that the
1020 * GPIO chip driver handles freeing the GPIOs it has requested).
1022 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1025 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1029 chip_err(chip, "failed to get GPIO descriptor\n");
1033 err = __gpiod_request(desc, label);
1035 return ERR_PTR(err);
1039 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
1042 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1043 * @desc: GPIO descriptor to free
1045 * Function frees the given GPIO requested previously with
1046 * gpiochip_request_own_desc().
1048 void gpiochip_free_own_desc(struct gpio_desc *desc)
1053 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
1055 /* Drivers MUST set GPIO direction before making get/set calls. In
1056 * some cases this is done in early boot, before IRQs are enabled.
1058 * As a rule these aren't called more than once (except for drivers
1059 * using the open-drain emulation idiom) so these are natural places
1060 * to accumulate extra debugging checks. Note that we can't (yet)
1061 * rely on gpio_request() having been called beforehand.
1065 * gpiod_direction_input - set the GPIO direction to input
1066 * @desc: GPIO to set to input
1068 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1069 * be called safely on it.
1071 * Return 0 in case of success, else an error code.
1073 int gpiod_direction_input(struct gpio_desc *desc)
1075 struct gpio_chip *chip;
1076 int status = -EINVAL;
1078 if (!desc || !desc->chip) {
1079 pr_warn("%s: invalid GPIO\n", __func__);
1084 if (!chip->get || !chip->direction_input) {
1086 "%s: missing get() or direction_input() operations\n",
1091 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
1093 clear_bit(FLAG_IS_OUT, &desc->flags);
1095 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1099 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1101 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1103 struct gpio_chip *chip;
1104 int status = -EINVAL;
1106 /* GPIOs used for IRQs shall not be set as output */
1107 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1109 "%s: tried to set a GPIO tied to an IRQ as output\n",
1114 /* Open drain pin should not be driven to 1 */
1115 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1116 return gpiod_direction_input(desc);
1118 /* Open source pin should not be driven to 0 */
1119 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1120 return gpiod_direction_input(desc);
1123 if (!chip->set || !chip->direction_output) {
1125 "%s: missing set() or direction_output() operations\n",
1130 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1132 set_bit(FLAG_IS_OUT, &desc->flags);
1133 trace_gpio_value(desc_to_gpio(desc), 0, value);
1134 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1139 * gpiod_direction_output_raw - set the GPIO direction to output
1140 * @desc: GPIO to set to output
1141 * @value: initial output value of the GPIO
1143 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1144 * be called safely on it. The initial value of the output must be specified
1145 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1147 * Return 0 in case of success, else an error code.
1149 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1151 if (!desc || !desc->chip) {
1152 pr_warn("%s: invalid GPIO\n", __func__);
1155 return _gpiod_direction_output_raw(desc, value);
1157 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1160 * gpiod_direction_output - set the GPIO direction to output
1161 * @desc: GPIO to set to output
1162 * @value: initial output value of the GPIO
1164 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1165 * be called safely on it. The initial value of the output must be specified
1166 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1169 * Return 0 in case of success, else an error code.
1171 int gpiod_direction_output(struct gpio_desc *desc, int value)
1173 if (!desc || !desc->chip) {
1174 pr_warn("%s: invalid GPIO\n", __func__);
1177 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1179 return _gpiod_direction_output_raw(desc, value);
1181 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1184 * gpiod_set_debounce - sets @debounce time for a @gpio
1185 * @gpio: the gpio to set debounce time
1186 * @debounce: debounce time is microseconds
1188 * returns -ENOTSUPP if the controller does not support setting
1191 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1193 struct gpio_chip *chip;
1195 if (!desc || !desc->chip) {
1196 pr_warn("%s: invalid GPIO\n", __func__);
1201 if (!chip->set || !chip->set_debounce) {
1203 "%s: missing set() or set_debounce() operations\n",
1208 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1210 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1213 * gpiod_is_active_low - test whether a GPIO is active-low or not
1214 * @desc: the gpio descriptor to test
1216 * Returns 1 if the GPIO is active-low, 0 otherwise.
1218 int gpiod_is_active_low(const struct gpio_desc *desc)
1220 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1222 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1224 /* I/O calls are only valid after configuration completed; the relevant
1225 * "is this a valid GPIO" error checks should already have been done.
1227 * "Get" operations are often inlinable as reading a pin value register,
1228 * and masking the relevant bit in that register.
1230 * When "set" operations are inlinable, they involve writing that mask to
1231 * one register to set a low value, or a different register to set it high.
1232 * Otherwise locking is needed, so there may be little value to inlining.
1234 *------------------------------------------------------------------------
1236 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1237 * have requested the GPIO. That can include implicit requesting by
1238 * a direction setting call. Marking a gpio as requested locks its chip
1239 * in memory, guaranteeing that these table lookups need no more locking
1240 * and that gpiochip_remove() will fail.
1242 * REVISIT when debugging, consider adding some instrumentation to ensure
1243 * that the GPIO was actually requested.
1246 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
1248 struct gpio_chip *chip;
1253 offset = gpio_chip_hwgpio(desc);
1254 value = chip->get ? chip->get(chip, offset) : -EIO;
1255 value = value < 0 ? value : !!value;
1256 trace_gpio_value(desc_to_gpio(desc), 1, value);
1261 * gpiod_get_raw_value() - return a gpio's raw value
1262 * @desc: gpio whose value will be returned
1264 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1265 * its ACTIVE_LOW status, or negative errno on failure.
1267 * This function should be called from contexts where we cannot sleep, and will
1268 * complain if the GPIO chip functions potentially sleep.
1270 int gpiod_get_raw_value(const struct gpio_desc *desc)
1274 /* Should be using gpio_get_value_cansleep() */
1275 WARN_ON(desc->chip->can_sleep);
1276 return _gpiod_get_raw_value(desc);
1278 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1281 * gpiod_get_value() - return a gpio's value
1282 * @desc: gpio whose value will be returned
1284 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1285 * account, or negative errno on failure.
1287 * This function should be called from contexts where we cannot sleep, and will
1288 * complain if the GPIO chip functions potentially sleep.
1290 int gpiod_get_value(const struct gpio_desc *desc)
1295 /* Should be using gpio_get_value_cansleep() */
1296 WARN_ON(desc->chip->can_sleep);
1298 value = _gpiod_get_raw_value(desc);
1302 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1307 EXPORT_SYMBOL_GPL(gpiod_get_value);
1310 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1311 * @desc: gpio descriptor whose state need to be set.
1312 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1314 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1317 struct gpio_chip *chip = desc->chip;
1318 int offset = gpio_chip_hwgpio(desc);
1321 err = chip->direction_input(chip, offset);
1323 clear_bit(FLAG_IS_OUT, &desc->flags);
1325 err = chip->direction_output(chip, offset, 0);
1327 set_bit(FLAG_IS_OUT, &desc->flags);
1329 trace_gpio_direction(desc_to_gpio(desc), value, err);
1332 "%s: Error in set_value for open drain err %d\n",
1337 * _gpio_set_open_source_value() - Set the open source 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_source_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_output(chip, offset, 1);
1350 set_bit(FLAG_IS_OUT, &desc->flags);
1352 err = chip->direction_input(chip, offset);
1354 clear_bit(FLAG_IS_OUT, &desc->flags);
1356 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1359 "%s: Error in set_value for open source err %d\n",
1363 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1365 struct gpio_chip *chip;
1368 trace_gpio_value(desc_to_gpio(desc), 0, value);
1369 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1370 _gpio_set_open_drain_value(desc, value);
1371 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1372 _gpio_set_open_source_value(desc, value);
1374 chip->set(chip, gpio_chip_hwgpio(desc), value);
1378 * set multiple outputs on the same chip;
1379 * use the chip's set_multiple function if available;
1380 * otherwise set the outputs sequentially;
1381 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1382 * defines which outputs are to be changed
1383 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1384 * defines the values the outputs specified by mask are to be set to
1386 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1387 unsigned long *mask, unsigned long *bits)
1389 if (chip->set_multiple) {
1390 chip->set_multiple(chip, mask, bits);
1393 for (i = 0; i < chip->ngpio; i++) {
1394 if (mask[BIT_WORD(i)] == 0) {
1395 /* no more set bits in this mask word;
1396 * skip ahead to the next word */
1397 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1400 /* set outputs if the corresponding mask bit is set */
1401 if (__test_and_clear_bit(i, mask))
1402 chip->set(chip, i, test_bit(i, bits));
1407 static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1408 unsigned int array_size,
1409 struct gpio_desc **desc_array,
1414 while (i < array_size) {
1415 struct gpio_chip *chip = desc_array[i]->chip;
1416 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1417 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1421 WARN_ON(chip->can_sleep);
1423 memset(mask, 0, sizeof(mask));
1425 struct gpio_desc *desc = desc_array[i];
1426 int hwgpio = gpio_chip_hwgpio(desc);
1427 int value = value_array[i];
1429 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1431 trace_gpio_value(desc_to_gpio(desc), 0, value);
1433 * collect all normal outputs belonging to the same chip
1434 * open drain and open source outputs are set individually
1436 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1437 _gpio_set_open_drain_value(desc, value);
1438 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1439 _gpio_set_open_source_value(desc, value);
1441 __set_bit(hwgpio, mask);
1443 __set_bit(hwgpio, bits);
1445 __clear_bit(hwgpio, bits);
1449 } while ((i < array_size) && (desc_array[i]->chip == chip));
1450 /* push collected bits to outputs */
1452 gpio_chip_set_multiple(chip, mask, bits);
1457 * gpiod_set_raw_value() - assign a gpio's raw value
1458 * @desc: gpio whose value will be assigned
1459 * @value: value to assign
1461 * Set the raw value of the GPIO, i.e. the value of its physical line without
1462 * regard for its ACTIVE_LOW status.
1464 * This function should be called from contexts where we cannot sleep, and will
1465 * complain if the GPIO chip functions potentially sleep.
1467 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1471 /* Should be using gpio_set_value_cansleep() */
1472 WARN_ON(desc->chip->can_sleep);
1473 _gpiod_set_raw_value(desc, value);
1475 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1478 * gpiod_set_value() - assign a gpio's value
1479 * @desc: gpio whose value will be assigned
1480 * @value: value to assign
1482 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1485 * This function should be called from contexts where we cannot sleep, and will
1486 * complain if the GPIO chip functions potentially sleep.
1488 void gpiod_set_value(struct gpio_desc *desc, int value)
1492 /* Should be using gpio_set_value_cansleep() */
1493 WARN_ON(desc->chip->can_sleep);
1494 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1496 _gpiod_set_raw_value(desc, value);
1498 EXPORT_SYMBOL_GPL(gpiod_set_value);
1501 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1502 * @array_size: number of elements in the descriptor / value arrays
1503 * @desc_array: array of GPIO descriptors whose values will be assigned
1504 * @value_array: array of values to assign
1506 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1507 * without regard for their ACTIVE_LOW status.
1509 * This function should be called from contexts where we cannot sleep, and will
1510 * complain if the GPIO chip functions potentially sleep.
1512 void gpiod_set_raw_array_value(unsigned int array_size,
1513 struct gpio_desc **desc_array, int *value_array)
1517 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1520 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
1523 * gpiod_set_array_value() - assign values to an array of GPIOs
1524 * @array_size: number of elements in the descriptor / value arrays
1525 * @desc_array: array of GPIO descriptors whose values will be assigned
1526 * @value_array: array of values to assign
1528 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1531 * This function should be called from contexts where we cannot sleep, and will
1532 * complain if the GPIO chip functions potentially sleep.
1534 void gpiod_set_array_value(unsigned int array_size,
1535 struct gpio_desc **desc_array, int *value_array)
1539 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1542 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
1545 * gpiod_cansleep() - report whether gpio value access may sleep
1546 * @desc: gpio to check
1549 int gpiod_cansleep(const struct gpio_desc *desc)
1553 return desc->chip->can_sleep;
1555 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1558 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1559 * @desc: gpio whose IRQ will be returned (already requested)
1561 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1564 int gpiod_to_irq(const struct gpio_desc *desc)
1566 struct gpio_chip *chip;
1572 offset = gpio_chip_hwgpio(desc);
1573 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1575 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1578 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1579 * @chip: the chip the GPIO to lock belongs to
1580 * @offset: the offset of the GPIO to lock as IRQ
1582 * This is used directly by GPIO drivers that want to lock down
1583 * a certain GPIO line to be used for IRQs.
1585 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1587 if (offset >= chip->ngpio)
1590 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1592 "%s: tried to flag a GPIO set as output for IRQ\n",
1597 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1600 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1603 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1604 * @chip: the chip the GPIO to lock belongs to
1605 * @offset: the offset of the GPIO to lock as IRQ
1607 * This is used directly by GPIO drivers that want to indicate
1608 * that a certain GPIO is no longer used exclusively for IRQ.
1610 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1612 if (offset >= chip->ngpio)
1615 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1617 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1620 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1621 * @desc: gpio whose value will be returned
1623 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1624 * its ACTIVE_LOW status, or negative errno on failure.
1626 * This function is to be called from contexts that can sleep.
1628 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1630 might_sleep_if(extra_checks);
1633 return _gpiod_get_raw_value(desc);
1635 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1638 * gpiod_get_value_cansleep() - return a gpio's value
1639 * @desc: gpio whose value will be returned
1641 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1642 * account, or negative errno on failure.
1644 * This function is to be called from contexts that can sleep.
1646 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1650 might_sleep_if(extra_checks);
1654 value = _gpiod_get_raw_value(desc);
1658 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1663 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
1666 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1667 * @desc: gpio whose value will be assigned
1668 * @value: value to assign
1670 * Set the raw value of the GPIO, i.e. the value of its physical line without
1671 * regard for its ACTIVE_LOW status.
1673 * This function is to be called from contexts that can sleep.
1675 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
1677 might_sleep_if(extra_checks);
1680 _gpiod_set_raw_value(desc, value);
1682 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
1685 * gpiod_set_value_cansleep() - assign a gpio's value
1686 * @desc: gpio whose value will be assigned
1687 * @value: value to assign
1689 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1692 * This function is to be called from contexts that can sleep.
1694 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1696 might_sleep_if(extra_checks);
1700 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1702 _gpiod_set_raw_value(desc, value);
1704 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1707 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
1708 * @array_size: number of elements in the descriptor / value arrays
1709 * @desc_array: array of GPIO descriptors whose values will be assigned
1710 * @value_array: array of values to assign
1712 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1713 * without regard for their ACTIVE_LOW status.
1715 * This function is to be called from contexts that can sleep.
1717 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1718 struct gpio_desc **desc_array,
1721 might_sleep_if(extra_checks);
1724 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1727 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
1730 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
1731 * @array_size: number of elements in the descriptor / value arrays
1732 * @desc_array: array of GPIO descriptors whose values will be assigned
1733 * @value_array: array of values to assign
1735 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1738 * This function is to be called from contexts that can sleep.
1740 void gpiod_set_array_value_cansleep(unsigned int array_size,
1741 struct gpio_desc **desc_array,
1744 might_sleep_if(extra_checks);
1747 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1750 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
1753 * gpiod_add_lookup_table() - register GPIO device consumers
1754 * @table: table of consumers to register
1756 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
1758 mutex_lock(&gpio_lookup_lock);
1760 list_add_tail(&table->list, &gpio_lookup_list);
1762 mutex_unlock(&gpio_lookup_lock);
1766 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1767 * @table: table of consumers to unregister
1769 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1771 mutex_lock(&gpio_lookup_lock);
1773 list_del(&table->list);
1775 mutex_unlock(&gpio_lookup_lock);
1778 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
1780 enum gpio_lookup_flags *flags)
1782 char prop_name[32]; /* 32 is max size of property name */
1783 enum of_gpio_flags of_flags;
1784 struct gpio_desc *desc;
1787 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1789 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
1792 snprintf(prop_name, sizeof(prop_name), "%s",
1795 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1797 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1804 if (of_flags & OF_GPIO_ACTIVE_LOW)
1805 *flags |= GPIO_ACTIVE_LOW;
1810 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1812 enum gpio_lookup_flags *flags)
1814 struct acpi_device *adev = ACPI_COMPANION(dev);
1815 struct acpi_gpio_info info;
1816 struct gpio_desc *desc;
1820 /* Try first from _DSD */
1821 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1822 if (con_id && strcmp(con_id, "gpios")) {
1823 snprintf(propname, sizeof(propname), "%s-%s",
1824 con_id, gpio_suffixes[i]);
1826 snprintf(propname, sizeof(propname), "%s",
1830 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1831 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1835 /* Then from plain _CRS GPIOs */
1837 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1842 if (info.active_low)
1843 *flags |= GPIO_ACTIVE_LOW;
1848 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1850 const char *dev_id = dev ? dev_name(dev) : NULL;
1851 struct gpiod_lookup_table *table;
1853 mutex_lock(&gpio_lookup_lock);
1855 list_for_each_entry(table, &gpio_lookup_list, list) {
1856 if (table->dev_id && dev_id) {
1858 * Valid strings on both ends, must be identical to have
1861 if (!strcmp(table->dev_id, dev_id))
1865 * One of the pointers is NULL, so both must be to have
1868 if (dev_id == table->dev_id)
1875 mutex_unlock(&gpio_lookup_lock);
1879 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1881 enum gpio_lookup_flags *flags)
1883 struct gpio_desc *desc = ERR_PTR(-ENOENT);
1884 struct gpiod_lookup_table *table;
1885 struct gpiod_lookup *p;
1887 table = gpiod_find_lookup_table(dev);
1891 for (p = &table->table[0]; p->chip_label; p++) {
1892 struct gpio_chip *chip;
1894 /* idx must always match exactly */
1898 /* If the lookup entry has a con_id, require exact match */
1899 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1902 chip = find_chip_by_name(p->chip_label);
1905 dev_err(dev, "cannot find GPIO chip %s\n",
1907 return ERR_PTR(-ENODEV);
1910 if (chip->ngpio <= p->chip_hwnum) {
1912 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1913 idx, chip->ngpio, chip->label);
1914 return ERR_PTR(-EINVAL);
1917 desc = gpiochip_get_desc(chip, p->chip_hwnum);
1926 static int dt_gpio_count(struct device *dev, const char *con_id)
1932 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1934 snprintf(propname, sizeof(propname), "%s-%s",
1935 con_id, gpio_suffixes[i]);
1937 snprintf(propname, sizeof(propname), "%s",
1940 ret = of_gpio_named_count(dev->of_node, propname);
1947 static int platform_gpio_count(struct device *dev, const char *con_id)
1949 struct gpiod_lookup_table *table;
1950 struct gpiod_lookup *p;
1951 unsigned int count = 0;
1953 table = gpiod_find_lookup_table(dev);
1957 for (p = &table->table[0]; p->chip_label; p++) {
1958 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1959 (!con_id && !p->con_id))
1969 * gpiod_count - return the number of GPIOs associated with a device / function
1970 * or -ENOENT if no GPIO has been assigned to the requested function
1971 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1972 * @con_id: function within the GPIO consumer
1974 int gpiod_count(struct device *dev, const char *con_id)
1976 int count = -ENOENT;
1978 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
1979 count = dt_gpio_count(dev, con_id);
1980 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
1981 count = acpi_gpio_count(dev, con_id);
1984 count = platform_gpio_count(dev, con_id);
1988 EXPORT_SYMBOL_GPL(gpiod_count);
1991 * gpiod_get - obtain a GPIO for a given GPIO function
1992 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1993 * @con_id: function within the GPIO consumer
1994 * @flags: optional GPIO initialization flags
1996 * Return the GPIO descriptor corresponding to the function con_id of device
1997 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1998 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
2000 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
2001 enum gpiod_flags flags)
2003 return gpiod_get_index(dev, con_id, 0, flags);
2005 EXPORT_SYMBOL_GPL(gpiod_get);
2008 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2009 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2010 * @con_id: function within the GPIO consumer
2011 * @flags: optional GPIO initialization flags
2013 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2014 * the requested function it will return NULL. This is convenient for drivers
2015 * that need to handle optional GPIOs.
2017 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
2019 enum gpiod_flags flags)
2021 return gpiod_get_index_optional(dev, con_id, 0, flags);
2023 EXPORT_SYMBOL_GPL(gpiod_get_optional);
2027 * gpiod_configure_flags - helper function to configure a given GPIO
2028 * @desc: gpio whose value will be assigned
2029 * @con_id: function within the GPIO consumer
2030 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2032 * @dflags: gpiod_flags - optional GPIO initialization flags
2034 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2035 * requested function and/or index, or another IS_ERR() code if an error
2036 * occurred while trying to acquire the GPIO.
2038 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2039 unsigned long lflags, enum gpiod_flags dflags)
2043 if (lflags & GPIO_ACTIVE_LOW)
2044 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2045 if (lflags & GPIO_OPEN_DRAIN)
2046 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2047 if (lflags & GPIO_OPEN_SOURCE)
2048 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2050 /* No particular flag request, return here... */
2051 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2052 pr_debug("no flags found for %s\n", con_id);
2057 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2058 status = gpiod_direction_output(desc,
2059 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2061 status = gpiod_direction_input(desc);
2067 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2068 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2069 * @con_id: function within the GPIO consumer
2070 * @idx: index of the GPIO to obtain in the consumer
2071 * @flags: optional GPIO initialization flags
2073 * This variant of gpiod_get() allows to access GPIOs other than the first
2074 * defined one for functions that define several GPIOs.
2076 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2077 * requested function and/or index, or another IS_ERR() code if an error
2078 * occurred while trying to acquire the GPIO.
2080 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2083 enum gpiod_flags flags)
2085 struct gpio_desc *desc = NULL;
2087 enum gpio_lookup_flags lookupflags = 0;
2089 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2092 /* Using device tree? */
2093 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2094 dev_dbg(dev, "using device tree for GPIO lookup\n");
2095 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2096 } else if (ACPI_COMPANION(dev)) {
2097 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2098 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2103 * Either we are not using DT or ACPI, or their lookup did not return
2104 * a result. In that case, use platform lookup as a fallback.
2106 if (!desc || desc == ERR_PTR(-ENOENT)) {
2107 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
2108 desc = gpiod_find(dev, con_id, idx, &lookupflags);
2112 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
2116 status = gpiod_request(desc, con_id);
2118 return ERR_PTR(status);
2120 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
2122 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2124 return ERR_PTR(status);
2129 EXPORT_SYMBOL_GPL(gpiod_get_index);
2132 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2133 * @fwnode: handle of the firmware node
2134 * @propname: name of the firmware property representing the GPIO
2136 * This function can be used for drivers that get their configuration
2139 * Function properly finds the corresponding GPIO using whatever is the
2140 * underlying firmware interface and then makes sure that the GPIO
2141 * descriptor is requested before it is returned to the caller.
2143 * In case of error an ERR_PTR() is returned.
2145 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2146 const char *propname)
2148 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2149 bool active_low = false;
2153 return ERR_PTR(-EINVAL);
2155 if (is_of_node(fwnode)) {
2156 enum of_gpio_flags flags;
2158 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
2161 active_low = flags & OF_GPIO_ACTIVE_LOW;
2162 } else if (is_acpi_node(fwnode)) {
2163 struct acpi_gpio_info info;
2165 desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0,
2168 active_low = info.active_low;
2174 ret = gpiod_request(desc, NULL);
2176 return ERR_PTR(ret);
2178 /* Only value flag can be set from both DT and ACPI is active_low */
2180 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2184 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2187 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2189 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2190 * @con_id: function within the GPIO consumer
2191 * @index: index of the GPIO to obtain in the consumer
2192 * @flags: optional GPIO initialization flags
2194 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2195 * specified index was assigned to the requested function it will return NULL.
2196 * This is convenient for drivers that need to handle optional GPIOs.
2198 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
2201 enum gpiod_flags flags)
2203 struct gpio_desc *desc;
2205 desc = gpiod_get_index(dev, con_id, index, flags);
2207 if (PTR_ERR(desc) == -ENOENT)
2213 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
2216 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2217 * @desc: gpio whose value will be assigned
2218 * @name: gpio line name
2219 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2221 * @dflags: gpiod_flags - optional GPIO initialization flags
2223 int gpiod_hog(struct gpio_desc *desc, const char *name,
2224 unsigned long lflags, enum gpiod_flags dflags)
2226 struct gpio_chip *chip;
2227 struct gpio_desc *local_desc;
2231 chip = gpiod_to_chip(desc);
2232 hwnum = gpio_chip_hwgpio(desc);
2234 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2235 if (IS_ERR(local_desc)) {
2236 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2237 name, chip->label, hwnum);
2238 return PTR_ERR(local_desc);
2241 status = gpiod_configure_flags(desc, name, lflags, dflags);
2243 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2244 name, chip->label, hwnum);
2245 gpiochip_free_own_desc(desc);
2249 /* Mark GPIO as hogged so it can be identified and removed later */
2250 set_bit(FLAG_IS_HOGGED, &desc->flags);
2252 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2253 desc_to_gpio(desc), name,
2254 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2255 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2256 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2262 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2263 * @chip: gpio chip to act on
2265 * This is only used by of_gpiochip_remove to free hogged gpios
2267 static void gpiochip_free_hogs(struct gpio_chip *chip)
2271 for (id = 0; id < chip->ngpio; id++) {
2272 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2273 gpiochip_free_own_desc(&chip->desc[id]);
2278 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2279 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2280 * @con_id: function within the GPIO consumer
2281 * @flags: optional GPIO initialization flags
2283 * This function acquires all the GPIOs defined under a given function.
2285 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2286 * no GPIO has been assigned to the requested function, or another IS_ERR()
2287 * code if an error occurred while trying to acquire the GPIOs.
2289 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2291 enum gpiod_flags flags)
2293 struct gpio_desc *desc;
2294 struct gpio_descs *descs;
2297 count = gpiod_count(dev, con_id);
2299 return ERR_PTR(count);
2301 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2304 return ERR_PTR(-ENOMEM);
2306 for (descs->ndescs = 0; descs->ndescs < count; ) {
2307 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2309 gpiod_put_array(descs);
2310 return ERR_CAST(desc);
2312 descs->desc[descs->ndescs] = desc;
2317 EXPORT_SYMBOL_GPL(gpiod_get_array);
2320 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2322 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2323 * @con_id: function within the GPIO consumer
2324 * @flags: optional GPIO initialization flags
2326 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2327 * assigned to the requested function it will return NULL.
2329 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2331 enum gpiod_flags flags)
2333 struct gpio_descs *descs;
2335 descs = gpiod_get_array(dev, con_id, flags);
2336 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2341 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2344 * gpiod_put - dispose of a GPIO descriptor
2345 * @desc: GPIO descriptor to dispose of
2347 * No descriptor can be used after gpiod_put() has been called on it.
2349 void gpiod_put(struct gpio_desc *desc)
2353 EXPORT_SYMBOL_GPL(gpiod_put);
2356 * gpiod_put_array - dispose of multiple GPIO descriptors
2357 * @descs: struct gpio_descs containing an array of descriptors
2359 void gpiod_put_array(struct gpio_descs *descs)
2363 for (i = 0; i < descs->ndescs; i++)
2364 gpiod_put(descs->desc[i]);
2368 EXPORT_SYMBOL_GPL(gpiod_put_array);
2370 #ifdef CONFIG_DEBUG_FS
2372 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2375 unsigned gpio = chip->base;
2376 struct gpio_desc *gdesc = &chip->desc[0];
2380 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2381 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2383 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2389 gpiod_get_direction(gdesc);
2390 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2391 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2392 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2393 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
2394 is_out ? "out" : "in ",
2396 ? (chip->get(chip, i) ? "hi" : "lo")
2398 is_irq ? "IRQ" : " ");
2399 seq_printf(s, "\n");
2403 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2405 unsigned long flags;
2406 struct gpio_chip *chip = NULL;
2407 loff_t index = *pos;
2411 spin_lock_irqsave(&gpio_lock, flags);
2412 list_for_each_entry(chip, &gpio_chips, list)
2414 spin_unlock_irqrestore(&gpio_lock, flags);
2417 spin_unlock_irqrestore(&gpio_lock, flags);
2422 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2424 unsigned long flags;
2425 struct gpio_chip *chip = v;
2428 spin_lock_irqsave(&gpio_lock, flags);
2429 if (list_is_last(&chip->list, &gpio_chips))
2432 ret = list_entry(chip->list.next, struct gpio_chip, list);
2433 spin_unlock_irqrestore(&gpio_lock, flags);
2441 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2445 static int gpiolib_seq_show(struct seq_file *s, void *v)
2447 struct gpio_chip *chip = v;
2450 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2451 chip->base, chip->base + chip->ngpio - 1);
2454 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2457 seq_printf(s, ", %s", chip->label);
2458 if (chip->can_sleep)
2459 seq_printf(s, ", can sleep");
2460 seq_printf(s, ":\n");
2463 chip->dbg_show(s, chip);
2465 gpiolib_dbg_show(s, chip);
2470 static const struct seq_operations gpiolib_seq_ops = {
2471 .start = gpiolib_seq_start,
2472 .next = gpiolib_seq_next,
2473 .stop = gpiolib_seq_stop,
2474 .show = gpiolib_seq_show,
2477 static int gpiolib_open(struct inode *inode, struct file *file)
2479 return seq_open(file, &gpiolib_seq_ops);
2482 static const struct file_operations gpiolib_operations = {
2483 .owner = THIS_MODULE,
2484 .open = gpiolib_open,
2486 .llseek = seq_lseek,
2487 .release = seq_release,
2490 static int __init gpiolib_debugfs_init(void)
2492 /* /sys/kernel/debug/gpio */
2493 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2494 NULL, NULL, &gpiolib_operations);
2497 subsys_initcall(gpiolib_debugfs_init);
2499 #endif /* DEBUG_FS */