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>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/gpio.h>
23 /* Implementation infrastructure for GPIO interfaces.
25 * The GPIO programming interface allows for inlining speed-critical
26 * get/set operations for common cases, so that access to SOC-integrated
27 * GPIOs can sometimes cost only an instruction or two per bit.
31 /* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
38 #define extra_checks 1
40 #define extra_checks 0
43 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
47 DEFINE_SPINLOCK(gpio_lock
);
49 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
51 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
53 static DEFINE_MUTEX(gpio_lookup_lock
);
54 static LIST_HEAD(gpio_lookup_list
);
55 LIST_HEAD(gpio_chips
);
57 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
63 * Convert a GPIO number to its descriptor
65 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
67 if (WARN(!gpio_is_valid(gpio
), "invalid GPIO %d\n", gpio
))
70 return &gpio_desc
[gpio
];
72 EXPORT_SYMBOL_GPL(gpio_to_desc
);
75 * Get the GPIO descriptor corresponding to the given hw number for this chip.
77 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*chip
,
80 if (hwnum
>= chip
->ngpio
)
81 return ERR_PTR(-EINVAL
);
83 return &chip
->desc
[hwnum
];
87 * Convert a GPIO descriptor to the integer namespace.
88 * This should disappear in the future but is needed since we still
89 * use GPIO numbers for error messages and sysfs nodes
91 int desc_to_gpio(const struct gpio_desc
*desc
)
93 return desc
- &gpio_desc
[0];
95 EXPORT_SYMBOL_GPL(desc_to_gpio
);
98 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
99 * when setting direction, and otherwise illegal. Until board setup code
100 * and drivers use explicit requests everywhere (which won't happen when
101 * those calls have no teeth) we can't avoid autorequesting. This nag
102 * message should motivate switching to explicit requests... so should
103 * the weaker cleanup after faults, compared to gpio_request().
105 * NOTE: the autorequest mechanism is going away; at this point it's
106 * only "legal" in the sense that (old) code using it won't break yet,
107 * but instead only triggers a WARN() stack dump.
109 static int gpio_ensure_requested(struct gpio_desc
*desc
)
111 const struct gpio_chip
*chip
= desc
->chip
;
112 const int gpio
= desc_to_gpio(desc
);
114 if (WARN(test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0,
115 "autorequest GPIO-%d\n", gpio
)) {
116 if (!try_module_get(chip
->owner
)) {
117 gpiod_err(desc
, "%s: module can't be gotten\n",
119 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
123 desc_set_label(desc
, "[auto]");
124 /* caller must chip->request() w/o spinlock */
132 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
133 * @desc: descriptor to return the chip of
135 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
137 return desc
? desc
->chip
: NULL
;
139 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
141 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
142 static int gpiochip_find_base(int ngpio
)
144 struct gpio_chip
*chip
;
145 int base
= ARCH_NR_GPIOS
- ngpio
;
147 list_for_each_entry_reverse(chip
, &gpio_chips
, list
) {
148 /* found a free space? */
149 if (chip
->base
+ chip
->ngpio
<= base
)
152 /* nope, check the space right before the chip */
153 base
= chip
->base
- ngpio
;
156 if (gpio_is_valid(base
)) {
157 pr_debug("%s: found new base at %d\n", __func__
, base
);
160 pr_err("%s: cannot find free range\n", __func__
);
166 * gpiod_get_direction - return the current direction of a GPIO
167 * @desc: GPIO to get the direction of
169 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
171 * This function may sleep if gpiod_cansleep() is true.
173 int gpiod_get_direction(const struct gpio_desc
*desc
)
175 struct gpio_chip
*chip
;
177 int status
= -EINVAL
;
179 chip
= gpiod_to_chip(desc
);
180 offset
= gpio_chip_hwgpio(desc
);
182 if (!chip
->get_direction
)
185 status
= chip
->get_direction(chip
, offset
);
187 /* GPIOF_DIR_IN, or other positive */
189 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
190 * so it does not affect constness per se */
191 clear_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
195 set_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
199 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
202 * Add a new chip to the global chips list, keeping the list of chips sorted
205 * Return -EBUSY if the new chip overlaps with some other chip's integer
208 static int gpiochip_add_to_list(struct gpio_chip
*chip
)
210 struct list_head
*pos
= &gpio_chips
;
211 struct gpio_chip
*_chip
;
214 /* find where to insert our chip */
215 list_for_each(pos
, &gpio_chips
) {
216 _chip
= list_entry(pos
, struct gpio_chip
, list
);
217 /* shall we insert before _chip? */
218 if (_chip
->base
>= chip
->base
+ chip
->ngpio
)
222 /* are we stepping on the chip right before? */
223 if (pos
!= &gpio_chips
&& pos
->prev
!= &gpio_chips
) {
224 _chip
= list_entry(pos
->prev
, struct gpio_chip
, list
);
225 if (_chip
->base
+ _chip
->ngpio
> chip
->base
) {
227 "GPIO integer space overlap, cannot add chip\n");
233 list_add_tail(&chip
->list
, pos
);
239 * gpiochip_add() - register a gpio_chip
240 * @chip: the chip to register, with chip->base initialized
241 * Context: potentially before irqs or kmalloc will work
243 * Returns a negative errno if the chip can't be registered, such as
244 * because the chip->base is invalid or already associated with a
245 * different chip. Otherwise it returns zero as a success code.
247 * When gpiochip_add() is called very early during boot, so that GPIOs
248 * can be freely used, the chip->dev device must be registered before
249 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
250 * for GPIOs will fail rudely.
252 * If chip->base is negative, this requests dynamic assignment of
253 * a range of valid GPIOs.
255 int gpiochip_add(struct gpio_chip
*chip
)
260 int base
= chip
->base
;
262 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
268 spin_lock_irqsave(&gpio_lock
, flags
);
271 base
= gpiochip_find_base(chip
->ngpio
);
279 status
= gpiochip_add_to_list(chip
);
282 chip
->desc
= &gpio_desc
[chip
->base
];
284 for (id
= 0; id
< chip
->ngpio
; id
++) {
285 struct gpio_desc
*desc
= &chip
->desc
[id
];
288 /* REVISIT: most hardware initializes GPIOs as
289 * inputs (often with pullups enabled) so power
290 * usage is minimized. Linux code should set the
291 * gpio direction first thing; but until it does,
292 * and in case chip->get_direction is not set,
293 * we may expose the wrong direction in sysfs.
295 desc
->flags
= !chip
->direction_input
301 spin_unlock_irqrestore(&gpio_lock
, flags
);
303 #ifdef CONFIG_PINCTRL
304 INIT_LIST_HEAD(&chip
->pin_ranges
);
307 of_gpiochip_add(chip
);
308 acpi_gpiochip_add(chip
);
313 status
= gpiochip_export(chip
);
317 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__
,
318 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
319 chip
->label
? : "generic");
324 spin_unlock_irqrestore(&gpio_lock
, flags
);
326 /* failures here can mean systems won't boot... */
327 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__
,
328 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
329 chip
->label
? : "generic");
332 EXPORT_SYMBOL_GPL(gpiochip_add
);
334 /* Forward-declaration */
335 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
);
338 * gpiochip_remove() - unregister a gpio_chip
339 * @chip: the chip to unregister
341 * A gpio_chip with any GPIOs still requested may not be removed.
343 int gpiochip_remove(struct gpio_chip
*chip
)
349 acpi_gpiochip_remove(chip
);
351 spin_lock_irqsave(&gpio_lock
, flags
);
353 gpiochip_irqchip_remove(chip
);
354 gpiochip_remove_pin_ranges(chip
);
355 of_gpiochip_remove(chip
);
357 for (id
= 0; id
< chip
->ngpio
; id
++) {
358 if (test_bit(FLAG_REQUESTED
, &chip
->desc
[id
].flags
)) {
364 for (id
= 0; id
< chip
->ngpio
; id
++)
365 chip
->desc
[id
].chip
= NULL
;
367 list_del(&chip
->list
);
370 spin_unlock_irqrestore(&gpio_lock
, flags
);
373 gpiochip_unexport(chip
);
377 EXPORT_SYMBOL_GPL(gpiochip_remove
);
380 * gpiochip_find() - iterator for locating a specific gpio_chip
381 * @data: data to pass to match function
382 * @callback: Callback function to check gpio_chip
384 * Similar to bus_find_device. It returns a reference to a gpio_chip as
385 * determined by a user supplied @match callback. The callback should return
386 * 0 if the device doesn't match and non-zero if it does. If the callback is
387 * non-zero, this function will return to the caller and not iterate over any
390 struct gpio_chip
*gpiochip_find(void *data
,
391 int (*match
)(struct gpio_chip
*chip
,
394 struct gpio_chip
*chip
;
397 spin_lock_irqsave(&gpio_lock
, flags
);
398 list_for_each_entry(chip
, &gpio_chips
, list
)
399 if (match(chip
, data
))
403 if (&chip
->list
== &gpio_chips
)
405 spin_unlock_irqrestore(&gpio_lock
, flags
);
409 EXPORT_SYMBOL_GPL(gpiochip_find
);
411 static int gpiochip_match_name(struct gpio_chip
*chip
, void *data
)
413 const char *name
= data
;
415 return !strcmp(chip
->label
, name
);
418 static struct gpio_chip
*find_chip_by_name(const char *name
)
420 return gpiochip_find((void *)name
, gpiochip_match_name
);
423 #ifdef CONFIG_GPIOLIB_IRQCHIP
426 * The following is irqchip helper code for gpiochips.
430 * gpiochip_add_chained_irqchip() - adds a chained irqchip to a gpiochip
431 * @gpiochip: the gpiochip to add the irqchip to
432 * @irqchip: the irqchip to add to the gpiochip
433 * @parent_irq: the irq number corresponding to the parent IRQ for this
435 * @parent_handler: the parent interrupt handler for the accumulated IRQ
436 * coming out of the gpiochip
438 void gpiochip_set_chained_irqchip(struct gpio_chip
*gpiochip
,
439 struct irq_chip
*irqchip
,
441 irq_flow_handler_t parent_handler
)
443 if (gpiochip
->can_sleep
) {
444 chip_err(gpiochip
, "you cannot have chained interrupts on a chip that may sleep\n");
448 irq_set_chained_handler(parent_irq
, parent_handler
);
450 * The parent irqchip is already using the chip_data for this
451 * irqchip, so our callbacks simply use the handler_data.
453 irq_set_handler_data(parent_irq
, gpiochip
);
455 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip
);
458 * This lock class tells lockdep that GPIO irqs are in a different
459 * category than their parents, so it won't report false recursion.
461 static struct lock_class_key gpiochip_irq_lock_class
;
464 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
465 * @d: the irqdomain used by this irqchip
466 * @irq: the global irq number used by this GPIO irqchip irq
467 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
469 * This function will set up the mapping for a certain IRQ line on a
470 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
471 * stored inside the gpiochip.
473 static int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
474 irq_hw_number_t hwirq
)
476 struct gpio_chip
*chip
= d
->host_data
;
478 irq_set_chip_data(irq
, chip
);
479 irq_set_lockdep_class(irq
, &gpiochip_irq_lock_class
);
480 irq_set_chip_and_handler(irq
, chip
->irqchip
, chip
->irq_handler
);
481 /* Chips that can sleep need nested thread handlers */
483 irq_set_nested_thread(irq
, 1);
485 set_irq_flags(irq
, IRQF_VALID
);
487 irq_set_noprobe(irq
);
490 * No set-up of the hardware will happen if IRQ_TYPE_NONE
491 * is passed as default type.
493 if (chip
->irq_default_type
!= IRQ_TYPE_NONE
)
494 irq_set_irq_type(irq
, chip
->irq_default_type
);
499 static void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
501 struct gpio_chip
*chip
= d
->host_data
;
504 set_irq_flags(irq
, 0);
507 irq_set_nested_thread(irq
, 0);
508 irq_set_chip_and_handler(irq
, NULL
, NULL
);
509 irq_set_chip_data(irq
, NULL
);
512 static const struct irq_domain_ops gpiochip_domain_ops
= {
513 .map
= gpiochip_irq_map
,
514 .unmap
= gpiochip_irq_unmap
,
515 /* Virtually all GPIO irqchips are twocell:ed */
516 .xlate
= irq_domain_xlate_twocell
,
519 static int gpiochip_irq_reqres(struct irq_data
*d
)
521 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
523 if (gpio_lock_as_irq(chip
, d
->hwirq
)) {
525 "unable to lock HW IRQ %lu for IRQ\n",
532 static void gpiochip_irq_relres(struct irq_data
*d
)
534 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
536 gpio_unlock_as_irq(chip
, d
->hwirq
);
539 static int gpiochip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
541 return irq_find_mapping(chip
->irqdomain
, offset
);
545 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
546 * @gpiochip: the gpiochip to remove the irqchip from
548 * This is called only from gpiochip_remove()
550 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
)
554 /* Remove all IRQ mappings and delete the domain */
555 if (gpiochip
->irqdomain
) {
556 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
557 irq_dispose_mapping(gpiochip
->irq_base
+ offset
);
558 irq_domain_remove(gpiochip
->irqdomain
);
561 if (gpiochip
->irqchip
) {
562 gpiochip
->irqchip
->irq_request_resources
= NULL
;
563 gpiochip
->irqchip
->irq_release_resources
= NULL
;
564 gpiochip
->irqchip
= NULL
;
569 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
570 * @gpiochip: the gpiochip to add the irqchip to
571 * @irqchip: the irqchip to add to the gpiochip
572 * @first_irq: if not dynamically assigned, the base (first) IRQ to
573 * allocate gpiochip irqs from
574 * @handler: the irq handler to use (often a predefined irq core function)
575 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
576 * to have the core avoid setting up any default type in the hardware.
578 * This function closely associates a certain irqchip with a certain
579 * gpiochip, providing an irq domain to translate the local IRQs to
580 * global irqs in the gpiolib core, and making sure that the gpiochip
581 * is passed as chip data to all related functions. Driver callbacks
582 * need to use container_of() to get their local state containers back
583 * from the gpiochip passed as chip data. An irqdomain will be stored
584 * in the gpiochip that shall be used by the driver to handle IRQ number
585 * translation. The gpiochip will need to be initialized and registered
586 * before calling this function.
588 * This function will handle two cell:ed simple IRQs and assumes all
589 * the pins on the gpiochip can generate a unique IRQ. Everything else
590 * need to be open coded.
592 int gpiochip_irqchip_add(struct gpio_chip
*gpiochip
,
593 struct irq_chip
*irqchip
,
594 unsigned int first_irq
,
595 irq_flow_handler_t handler
,
598 struct device_node
*of_node
;
600 unsigned irq_base
= 0;
602 if (!gpiochip
|| !irqchip
)
605 if (!gpiochip
->dev
) {
606 pr_err("missing gpiochip .dev parent pointer\n");
609 of_node
= gpiochip
->dev
->of_node
;
610 #ifdef CONFIG_OF_GPIO
612 * If the gpiochip has an assigned OF node this takes precendence
613 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
615 if (gpiochip
->of_node
)
616 of_node
= gpiochip
->of_node
;
618 gpiochip
->irqchip
= irqchip
;
619 gpiochip
->irq_handler
= handler
;
620 gpiochip
->irq_default_type
= type
;
621 gpiochip
->to_irq
= gpiochip_to_irq
;
622 gpiochip
->irqdomain
= irq_domain_add_simple(of_node
,
623 gpiochip
->ngpio
, first_irq
,
624 &gpiochip_domain_ops
, gpiochip
);
625 if (!gpiochip
->irqdomain
) {
626 gpiochip
->irqchip
= NULL
;
629 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
630 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
633 * Prepare the mapping since the irqchip shall be orthogonal to
634 * any gpiochip calls. If the first_irq was zero, this is
635 * necessary to allocate descriptors for all IRQs.
637 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++) {
638 irq_base
= irq_create_mapping(gpiochip
->irqdomain
, offset
);
641 * Store the base into the gpiochip to be used when
642 * unmapping the irqs.
644 gpiochip
->irq_base
= irq_base
;
649 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add
);
651 #else /* CONFIG_GPIOLIB_IRQCHIP */
653 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
) {}
655 #endif /* CONFIG_GPIOLIB_IRQCHIP */
657 #ifdef CONFIG_PINCTRL
660 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
661 * @chip: the gpiochip to add the range for
662 * @pinctrl: the dev_name() of the pin controller to map to
663 * @gpio_offset: the start offset in the current gpio_chip number space
664 * @pin_group: name of the pin group inside the pin controller
666 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
667 struct pinctrl_dev
*pctldev
,
668 unsigned int gpio_offset
, const char *pin_group
)
670 struct gpio_pin_range
*pin_range
;
673 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
675 chip_err(chip
, "failed to allocate pin ranges\n");
679 /* Use local offset as range ID */
680 pin_range
->range
.id
= gpio_offset
;
681 pin_range
->range
.gc
= chip
;
682 pin_range
->range
.name
= chip
->label
;
683 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
684 pin_range
->pctldev
= pctldev
;
686 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
687 &pin_range
->range
.pins
,
688 &pin_range
->range
.npins
);
694 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
696 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
697 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
698 pinctrl_dev_get_devname(pctldev
), pin_group
);
700 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
704 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
707 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
708 * @chip: the gpiochip to add the range for
709 * @pinctrl_name: the dev_name() of the pin controller to map to
710 * @gpio_offset: the start offset in the current gpio_chip number space
711 * @pin_offset: the start offset in the pin controller number space
712 * @npins: the number of pins from the offset of each pin space (GPIO and
713 * pin controller) to accumulate in this range
715 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
716 unsigned int gpio_offset
, unsigned int pin_offset
,
719 struct gpio_pin_range
*pin_range
;
722 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
724 chip_err(chip
, "failed to allocate pin ranges\n");
728 /* Use local offset as range ID */
729 pin_range
->range
.id
= gpio_offset
;
730 pin_range
->range
.gc
= chip
;
731 pin_range
->range
.name
= chip
->label
;
732 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
733 pin_range
->range
.pin_base
= pin_offset
;
734 pin_range
->range
.npins
= npins
;
735 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
737 if (IS_ERR(pin_range
->pctldev
)) {
738 ret
= PTR_ERR(pin_range
->pctldev
);
739 chip_err(chip
, "could not create pin range\n");
743 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
744 gpio_offset
, gpio_offset
+ npins
- 1,
746 pin_offset
, pin_offset
+ npins
- 1);
748 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
752 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
755 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
756 * @chip: the chip to remove all the mappings for
758 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
760 struct gpio_pin_range
*pin_range
, *tmp
;
762 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
763 list_del(&pin_range
->node
);
764 pinctrl_remove_gpio_range(pin_range
->pctldev
,
769 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
771 #endif /* CONFIG_PINCTRL */
773 /* These "optional" allocation calls help prevent drivers from stomping
774 * on each other, and help provide better diagnostics in debugfs.
775 * They're called even less than the "set direction" calls.
777 static int __gpiod_request(struct gpio_desc
*desc
, const char *label
)
779 struct gpio_chip
*chip
= desc
->chip
;
783 spin_lock_irqsave(&gpio_lock
, flags
);
785 /* NOTE: gpio_request() can be called in early boot,
786 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
789 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
790 desc_set_label(desc
, label
? : "?");
798 /* chip->request may sleep */
799 spin_unlock_irqrestore(&gpio_lock
, flags
);
800 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
801 spin_lock_irqsave(&gpio_lock
, flags
);
804 desc_set_label(desc
, NULL
);
805 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
809 if (chip
->get_direction
) {
810 /* chip->get_direction may sleep */
811 spin_unlock_irqrestore(&gpio_lock
, flags
);
812 gpiod_get_direction(desc
);
813 spin_lock_irqsave(&gpio_lock
, flags
);
816 spin_unlock_irqrestore(&gpio_lock
, flags
);
820 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
822 int status
= -EPROBE_DEFER
;
823 struct gpio_chip
*chip
;
826 pr_warn("%s: invalid GPIO\n", __func__
);
834 if (try_module_get(chip
->owner
)) {
835 status
= __gpiod_request(desc
, label
);
837 module_put(chip
->owner
);
842 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
847 static bool __gpiod_free(struct gpio_desc
*desc
)
851 struct gpio_chip
*chip
;
855 gpiod_unexport(desc
);
857 spin_lock_irqsave(&gpio_lock
, flags
);
860 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
862 spin_unlock_irqrestore(&gpio_lock
, flags
);
863 might_sleep_if(chip
->can_sleep
);
864 chip
->free(chip
, gpio_chip_hwgpio(desc
));
865 spin_lock_irqsave(&gpio_lock
, flags
);
867 desc_set_label(desc
, NULL
);
868 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
869 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
870 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
871 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
875 spin_unlock_irqrestore(&gpio_lock
, flags
);
879 void gpiod_free(struct gpio_desc
*desc
)
881 if (desc
&& __gpiod_free(desc
))
882 module_put(desc
->chip
->owner
);
884 WARN_ON(extra_checks
);
888 * gpiochip_is_requested - return string iff signal was requested
889 * @chip: controller managing the signal
890 * @offset: of signal within controller's 0..(ngpio - 1) range
892 * Returns NULL if the GPIO is not currently requested, else a string.
893 * The string returned is the label passed to gpio_request(); if none has been
894 * passed it is a meaningless, non-NULL constant.
896 * This function is for use by GPIO controller drivers. The label can
897 * help with diagnostics, and knowing that the signal is used as a GPIO
898 * can help avoid accidentally multiplexing it to another controller.
900 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
902 struct gpio_desc
*desc
;
904 if (!GPIO_OFFSET_VALID(chip
, offset
))
907 desc
= &chip
->desc
[offset
];
909 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
913 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
916 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
917 * @desc: GPIO descriptor to request
918 * @label: label for the GPIO
920 * Function allows GPIO chip drivers to request and use their own GPIO
921 * descriptors via gpiolib API. Difference to gpiod_request() is that this
922 * function will not increase reference count of the GPIO chip module. This
923 * allows the GPIO chip module to be unloaded as needed (we assume that the
924 * GPIO chip driver handles freeing the GPIOs it has requested).
926 int gpiochip_request_own_desc(struct gpio_desc
*desc
, const char *label
)
928 if (!desc
|| !desc
->chip
)
931 return __gpiod_request(desc
, label
);
935 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
936 * @desc: GPIO descriptor to free
938 * Function frees the given GPIO requested previously with
939 * gpiochip_request_own_desc().
941 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
947 /* Drivers MUST set GPIO direction before making get/set calls. In
948 * some cases this is done in early boot, before IRQs are enabled.
950 * As a rule these aren't called more than once (except for drivers
951 * using the open-drain emulation idiom) so these are natural places
952 * to accumulate extra debugging checks. Note that we can't (yet)
953 * rely on gpio_request() having been called beforehand.
957 * gpiod_direction_input - set the GPIO direction to input
958 * @desc: GPIO to set to input
960 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
961 * be called safely on it.
963 * Return 0 in case of success, else an error code.
965 int gpiod_direction_input(struct gpio_desc
*desc
)
968 struct gpio_chip
*chip
;
969 int status
= -EINVAL
;
972 if (!desc
|| !desc
->chip
) {
973 pr_warn("%s: invalid GPIO\n", __func__
);
978 if (!chip
->get
|| !chip
->direction_input
) {
980 "%s: missing get() or direction_input() operations\n",
985 spin_lock_irqsave(&gpio_lock
, flags
);
987 status
= gpio_ensure_requested(desc
);
991 /* now we know the gpio is valid and chip won't vanish */
993 spin_unlock_irqrestore(&gpio_lock
, flags
);
995 might_sleep_if(chip
->can_sleep
);
997 offset
= gpio_chip_hwgpio(desc
);
999 status
= chip
->request(chip
, offset
);
1001 gpiod_dbg(desc
, "%s: chip request fail, %d\n",
1003 /* and it's not available to anyone else ...
1004 * gpio_request() is the fully clean solution.
1010 status
= chip
->direction_input(chip
, offset
);
1012 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1014 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1018 spin_unlock_irqrestore(&gpio_lock
, flags
);
1020 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1023 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
1025 static int _gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1027 unsigned long flags
;
1028 struct gpio_chip
*chip
;
1029 int status
= -EINVAL
;
1032 /* GPIOs used for IRQs shall not be set as output */
1033 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
1035 "%s: tried to set a GPIO tied to an IRQ as output\n",
1040 /* Open drain pin should not be driven to 1 */
1041 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1042 return gpiod_direction_input(desc
);
1044 /* Open source pin should not be driven to 0 */
1045 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1046 return gpiod_direction_input(desc
);
1049 if (!chip
->set
|| !chip
->direction_output
) {
1051 "%s: missing set() or direction_output() operations\n",
1056 spin_lock_irqsave(&gpio_lock
, flags
);
1058 status
= gpio_ensure_requested(desc
);
1062 /* now we know the gpio is valid and chip won't vanish */
1064 spin_unlock_irqrestore(&gpio_lock
, flags
);
1066 might_sleep_if(chip
->can_sleep
);
1068 offset
= gpio_chip_hwgpio(desc
);
1070 status
= chip
->request(chip
, offset
);
1072 gpiod_dbg(desc
, "%s: chip request fail, %d\n",
1074 /* and it's not available to anyone else ...
1075 * gpio_request() is the fully clean solution.
1081 status
= chip
->direction_output(chip
, offset
, value
);
1083 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1084 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1085 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1089 spin_unlock_irqrestore(&gpio_lock
, flags
);
1091 gpiod_dbg(desc
, "%s: gpio status %d\n", __func__
, status
);
1096 * gpiod_direction_output_raw - set the GPIO direction to output
1097 * @desc: GPIO to set to output
1098 * @value: initial output value of the GPIO
1100 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1101 * be called safely on it. The initial value of the output must be specified
1102 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1104 * Return 0 in case of success, else an error code.
1106 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1108 if (!desc
|| !desc
->chip
) {
1109 pr_warn("%s: invalid GPIO\n", __func__
);
1112 return _gpiod_direction_output_raw(desc
, value
);
1114 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
1117 * gpiod_direction_output - set the GPIO direction to output
1118 * @desc: GPIO to set to output
1119 * @value: initial output value of the GPIO
1121 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1122 * be called safely on it. The initial value of the output must be specified
1123 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1126 * Return 0 in case of success, else an error code.
1128 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1130 if (!desc
|| !desc
->chip
) {
1131 pr_warn("%s: invalid GPIO\n", __func__
);
1134 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1136 return _gpiod_direction_output_raw(desc
, value
);
1138 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1141 * gpiod_set_debounce - sets @debounce time for a @gpio
1142 * @gpio: the gpio to set debounce time
1143 * @debounce: debounce time is microseconds
1145 * returns -ENOTSUPP if the controller does not support setting
1148 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1150 unsigned long flags
;
1151 struct gpio_chip
*chip
;
1152 int status
= -EINVAL
;
1155 if (!desc
|| !desc
->chip
) {
1156 pr_warn("%s: invalid GPIO\n", __func__
);
1161 if (!chip
->set
|| !chip
->set_debounce
) {
1163 "%s: missing set() or set_debounce() operations\n",
1168 spin_lock_irqsave(&gpio_lock
, flags
);
1170 status
= gpio_ensure_requested(desc
);
1174 /* now we know the gpio is valid and chip won't vanish */
1176 spin_unlock_irqrestore(&gpio_lock
, flags
);
1178 might_sleep_if(chip
->can_sleep
);
1180 offset
= gpio_chip_hwgpio(desc
);
1181 return chip
->set_debounce(chip
, offset
, debounce
);
1184 spin_unlock_irqrestore(&gpio_lock
, flags
);
1186 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1190 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1193 * gpiod_is_active_low - test whether a GPIO is active-low or not
1194 * @desc: the gpio descriptor to test
1196 * Returns 1 if the GPIO is active-low, 0 otherwise.
1198 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1200 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1202 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1204 /* I/O calls are only valid after configuration completed; the relevant
1205 * "is this a valid GPIO" error checks should already have been done.
1207 * "Get" operations are often inlinable as reading a pin value register,
1208 * and masking the relevant bit in that register.
1210 * When "set" operations are inlinable, they involve writing that mask to
1211 * one register to set a low value, or a different register to set it high.
1212 * Otherwise locking is needed, so there may be little value to inlining.
1214 *------------------------------------------------------------------------
1216 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1217 * have requested the GPIO. That can include implicit requesting by
1218 * a direction setting call. Marking a gpio as requested locks its chip
1219 * in memory, guaranteeing that these table lookups need no more locking
1220 * and that gpiochip_remove() will fail.
1222 * REVISIT when debugging, consider adding some instrumentation to ensure
1223 * that the GPIO was actually requested.
1226 static bool _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1228 struct gpio_chip
*chip
;
1233 offset
= gpio_chip_hwgpio(desc
);
1234 value
= chip
->get
? chip
->get(chip
, offset
) : false;
1235 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1240 * gpiod_get_raw_value() - return a gpio's raw value
1241 * @desc: gpio whose value will be returned
1243 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1244 * its ACTIVE_LOW status.
1246 * This function should be called from contexts where we cannot sleep, and will
1247 * complain if the GPIO chip functions potentially sleep.
1249 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
1253 /* Should be using gpio_get_value_cansleep() */
1254 WARN_ON(desc
->chip
->can_sleep
);
1255 return _gpiod_get_raw_value(desc
);
1257 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1260 * gpiod_get_value() - return a gpio's value
1261 * @desc: gpio whose value will be returned
1263 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1266 * This function should be called from contexts where we cannot sleep, and will
1267 * complain if the GPIO chip functions potentially sleep.
1269 int gpiod_get_value(const struct gpio_desc
*desc
)
1274 /* Should be using gpio_get_value_cansleep() */
1275 WARN_ON(desc
->chip
->can_sleep
);
1277 value
= _gpiod_get_raw_value(desc
);
1278 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1283 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1286 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1287 * @desc: gpio descriptor whose state need to be set.
1288 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1290 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, bool value
)
1293 struct gpio_chip
*chip
= desc
->chip
;
1294 int offset
= gpio_chip_hwgpio(desc
);
1297 err
= chip
->direction_input(chip
, offset
);
1299 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1301 err
= chip
->direction_output(chip
, offset
, 0);
1303 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1305 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1308 "%s: Error in set_value for open drain err %d\n",
1313 * _gpio_set_open_source_value() - Set the open source gpio's value.
1314 * @desc: gpio descriptor whose state need to be set.
1315 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1317 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, bool value
)
1320 struct gpio_chip
*chip
= desc
->chip
;
1321 int offset
= gpio_chip_hwgpio(desc
);
1324 err
= chip
->direction_output(chip
, offset
, 1);
1326 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1328 err
= chip
->direction_input(chip
, offset
);
1330 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1332 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
1335 "%s: Error in set_value for open source err %d\n",
1339 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, bool value
)
1341 struct gpio_chip
*chip
;
1344 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1345 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1346 _gpio_set_open_drain_value(desc
, value
);
1347 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1348 _gpio_set_open_source_value(desc
, value
);
1350 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
1354 * gpiod_set_raw_value() - assign a gpio's raw value
1355 * @desc: gpio whose value will be assigned
1356 * @value: value to assign
1358 * Set the raw value of the GPIO, i.e. the value of its physical line without
1359 * regard for its ACTIVE_LOW status.
1361 * This function should be called from contexts where we cannot sleep, and will
1362 * complain if the GPIO chip functions potentially sleep.
1364 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
1368 /* Should be using gpio_set_value_cansleep() */
1369 WARN_ON(desc
->chip
->can_sleep
);
1370 _gpiod_set_raw_value(desc
, value
);
1372 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
1375 * gpiod_set_value() - assign a gpio's value
1376 * @desc: gpio whose value will be assigned
1377 * @value: value to assign
1379 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1382 * This function should be called from contexts where we cannot sleep, and will
1383 * complain if the GPIO chip functions potentially sleep.
1385 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
1389 /* Should be using gpio_set_value_cansleep() */
1390 WARN_ON(desc
->chip
->can_sleep
);
1391 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1393 _gpiod_set_raw_value(desc
, value
);
1395 EXPORT_SYMBOL_GPL(gpiod_set_value
);
1398 * gpiod_cansleep() - report whether gpio value access may sleep
1399 * @desc: gpio to check
1402 int gpiod_cansleep(const struct gpio_desc
*desc
)
1406 return desc
->chip
->can_sleep
;
1408 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
1411 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1412 * @desc: gpio whose IRQ will be returned (already requested)
1414 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1417 int gpiod_to_irq(const struct gpio_desc
*desc
)
1419 struct gpio_chip
*chip
;
1425 offset
= gpio_chip_hwgpio(desc
);
1426 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
1428 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
1431 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
1432 * @gpio: the GPIO line to lock as used for IRQ
1434 * This is used directly by GPIO drivers that want to lock down
1435 * a certain GPIO line to be used for IRQs.
1437 int gpiod_lock_as_irq(struct gpio_desc
*desc
)
1442 if (test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
1444 "%s: tried to flag a GPIO set as output for IRQ\n",
1449 set_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
1452 EXPORT_SYMBOL_GPL(gpiod_lock_as_irq
);
1455 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
1456 * @gpio: the GPIO line to unlock from IRQ usage
1458 * This is used directly by GPIO drivers that want to indicate
1459 * that a certain GPIO is no longer used exclusively for IRQ.
1461 void gpiod_unlock_as_irq(struct gpio_desc
*desc
)
1466 clear_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
1468 EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq
);
1471 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1472 * @desc: gpio whose value will be returned
1474 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1475 * its ACTIVE_LOW status.
1477 * This function is to be called from contexts that can sleep.
1479 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
1481 might_sleep_if(extra_checks
);
1484 return _gpiod_get_raw_value(desc
);
1486 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
1489 * gpiod_get_value_cansleep() - return a gpio's value
1490 * @desc: gpio whose value will be returned
1492 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1495 * This function is to be called from contexts that can sleep.
1497 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
1501 might_sleep_if(extra_checks
);
1505 value
= _gpiod_get_raw_value(desc
);
1506 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1511 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
1514 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1515 * @desc: gpio whose value will be assigned
1516 * @value: value to assign
1518 * Set the raw value of the GPIO, i.e. the value of its physical line without
1519 * regard for its ACTIVE_LOW status.
1521 * This function is to be called from contexts that can sleep.
1523 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
1525 might_sleep_if(extra_checks
);
1528 _gpiod_set_raw_value(desc
, value
);
1530 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
1533 * gpiod_set_value_cansleep() - assign a gpio's value
1534 * @desc: gpio whose value will be assigned
1535 * @value: value to assign
1537 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1540 * This function is to be called from contexts that can sleep.
1542 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
1544 might_sleep_if(extra_checks
);
1548 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1550 _gpiod_set_raw_value(desc
, value
);
1552 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
1555 * gpiod_add_lookup_table() - register GPIO device consumers
1556 * @table: table of consumers to register
1558 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
1560 mutex_lock(&gpio_lookup_lock
);
1562 list_add_tail(&table
->list
, &gpio_lookup_list
);
1564 mutex_unlock(&gpio_lookup_lock
);
1567 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
1569 enum gpio_lookup_flags
*flags
)
1571 static const char *suffixes
[] = { "gpios", "gpio" };
1572 char prop_name
[32]; /* 32 is max size of property name */
1573 enum of_gpio_flags of_flags
;
1574 struct gpio_desc
*desc
;
1577 for (i
= 0; i
< ARRAY_SIZE(suffixes
); i
++) {
1579 snprintf(prop_name
, 32, "%s-%s", con_id
, suffixes
[i
]);
1581 snprintf(prop_name
, 32, "%s", suffixes
[i
]);
1583 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
1585 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
1592 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
1593 *flags
|= GPIO_ACTIVE_LOW
;
1598 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
1600 enum gpio_lookup_flags
*flags
)
1602 struct acpi_gpio_info info
;
1603 struct gpio_desc
*desc
;
1605 desc
= acpi_get_gpiod_by_index(dev
, idx
, &info
);
1609 if (info
.gpioint
&& info
.active_low
)
1610 *flags
|= GPIO_ACTIVE_LOW
;
1615 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
1617 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
1618 struct gpiod_lookup_table
*table
;
1620 mutex_lock(&gpio_lookup_lock
);
1622 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
1623 if (table
->dev_id
&& dev_id
) {
1625 * Valid strings on both ends, must be identical to have
1628 if (!strcmp(table
->dev_id
, dev_id
))
1632 * One of the pointers is NULL, so both must be to have
1635 if (dev_id
== table
->dev_id
)
1642 mutex_unlock(&gpio_lookup_lock
);
1646 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
1648 enum gpio_lookup_flags
*flags
)
1650 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
1651 struct gpiod_lookup_table
*table
;
1652 struct gpiod_lookup
*p
;
1654 table
= gpiod_find_lookup_table(dev
);
1658 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
1659 struct gpio_chip
*chip
;
1661 /* idx must always match exactly */
1665 /* If the lookup entry has a con_id, require exact match */
1666 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
1669 chip
= find_chip_by_name(p
->chip_label
);
1672 dev_err(dev
, "cannot find GPIO chip %s\n",
1674 return ERR_PTR(-ENODEV
);
1677 if (chip
->ngpio
<= p
->chip_hwnum
) {
1679 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1680 idx
, chip
->ngpio
, chip
->label
);
1681 return ERR_PTR(-EINVAL
);
1684 desc
= gpiochip_get_desc(chip
, p
->chip_hwnum
);
1694 * gpiod_get - obtain a GPIO for a given GPIO function
1695 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1696 * @con_id: function within the GPIO consumer
1698 * Return the GPIO descriptor corresponding to the function con_id of device
1699 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1700 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
1702 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
)
1704 return gpiod_get_index(dev
, con_id
, 0);
1706 EXPORT_SYMBOL_GPL(gpiod_get
);
1709 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1710 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1711 * @con_id: function within the GPIO consumer
1713 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1714 * the requested function it will return NULL. This is convenient for drivers
1715 * that need to handle optional GPIOs.
1717 struct gpio_desc
*__must_check
gpiod_get_optional(struct device
*dev
,
1720 return gpiod_get_index_optional(dev
, con_id
, 0);
1722 EXPORT_SYMBOL_GPL(gpiod_get_optional
);
1725 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
1726 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1727 * @con_id: function within the GPIO consumer
1728 * @idx: index of the GPIO to obtain in the consumer
1730 * This variant of gpiod_get() allows to access GPIOs other than the first
1731 * defined one for functions that define several GPIOs.
1733 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1734 * requested function and/or index, or another IS_ERR() code if an error
1735 * occured while trying to acquire the GPIO.
1737 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
1741 struct gpio_desc
*desc
= NULL
;
1743 enum gpio_lookup_flags flags
= 0;
1745 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
1747 /* Using device tree? */
1748 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
) {
1749 dev_dbg(dev
, "using device tree for GPIO lookup\n");
1750 desc
= of_find_gpio(dev
, con_id
, idx
, &flags
);
1751 } else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
)) {
1752 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
1753 desc
= acpi_find_gpio(dev
, con_id
, idx
, &flags
);
1757 * Either we are not using DT or ACPI, or their lookup did not return
1758 * a result. In that case, use platform lookup as a fallback.
1760 if (!desc
|| desc
== ERR_PTR(-ENOENT
)) {
1761 dev_dbg(dev
, "using lookup tables for GPIO lookup");
1762 desc
= gpiod_find(dev
, con_id
, idx
, &flags
);
1766 dev_dbg(dev
, "lookup for GPIO %s failed\n", con_id
);
1770 status
= gpiod_request(desc
, con_id
);
1773 return ERR_PTR(status
);
1775 if (flags
& GPIO_ACTIVE_LOW
)
1776 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1777 if (flags
& GPIO_OPEN_DRAIN
)
1778 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1779 if (flags
& GPIO_OPEN_SOURCE
)
1780 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1784 EXPORT_SYMBOL_GPL(gpiod_get_index
);
1787 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1789 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1790 * @con_id: function within the GPIO consumer
1791 * @index: index of the GPIO to obtain in the consumer
1793 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1794 * specified index was assigned to the requested function it will return NULL.
1795 * This is convenient for drivers that need to handle optional GPIOs.
1797 struct gpio_desc
*__must_check
gpiod_get_index_optional(struct device
*dev
,
1801 struct gpio_desc
*desc
;
1803 desc
= gpiod_get_index(dev
, con_id
, index
);
1805 if (PTR_ERR(desc
) == -ENOENT
)
1811 EXPORT_SYMBOL_GPL(gpiod_get_index_optional
);
1814 * gpiod_put - dispose of a GPIO descriptor
1815 * @desc: GPIO descriptor to dispose of
1817 * No descriptor can be used after gpiod_put() has been called on it.
1819 void gpiod_put(struct gpio_desc
*desc
)
1823 EXPORT_SYMBOL_GPL(gpiod_put
);
1825 #ifdef CONFIG_DEBUG_FS
1827 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1830 unsigned gpio
= chip
->base
;
1831 struct gpio_desc
*gdesc
= &chip
->desc
[0];
1835 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
1836 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
1839 gpiod_get_direction(gdesc
);
1840 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
1841 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
1842 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s %s",
1844 is_out
? "out" : "in ",
1846 ? (chip
->get(chip
, i
) ? "hi" : "lo")
1848 is_irq
? "IRQ" : " ");
1849 seq_printf(s
, "\n");
1853 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
1855 unsigned long flags
;
1856 struct gpio_chip
*chip
= NULL
;
1857 loff_t index
= *pos
;
1861 spin_lock_irqsave(&gpio_lock
, flags
);
1862 list_for_each_entry(chip
, &gpio_chips
, list
)
1864 spin_unlock_irqrestore(&gpio_lock
, flags
);
1867 spin_unlock_irqrestore(&gpio_lock
, flags
);
1872 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1874 unsigned long flags
;
1875 struct gpio_chip
*chip
= v
;
1878 spin_lock_irqsave(&gpio_lock
, flags
);
1879 if (list_is_last(&chip
->list
, &gpio_chips
))
1882 ret
= list_entry(chip
->list
.next
, struct gpio_chip
, list
);
1883 spin_unlock_irqrestore(&gpio_lock
, flags
);
1891 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
1895 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
1897 struct gpio_chip
*chip
= v
;
1900 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
1901 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
1904 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
1907 seq_printf(s
, ", %s", chip
->label
);
1908 if (chip
->can_sleep
)
1909 seq_printf(s
, ", can sleep");
1910 seq_printf(s
, ":\n");
1913 chip
->dbg_show(s
, chip
);
1915 gpiolib_dbg_show(s
, chip
);
1920 static const struct seq_operations gpiolib_seq_ops
= {
1921 .start
= gpiolib_seq_start
,
1922 .next
= gpiolib_seq_next
,
1923 .stop
= gpiolib_seq_stop
,
1924 .show
= gpiolib_seq_show
,
1927 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
1929 return seq_open(file
, &gpiolib_seq_ops
);
1932 static const struct file_operations gpiolib_operations
= {
1933 .owner
= THIS_MODULE
,
1934 .open
= gpiolib_open
,
1936 .llseek
= seq_lseek
,
1937 .release
= seq_release
,
1940 static int __init
gpiolib_debugfs_init(void)
1942 /* /sys/kernel/debug/gpio */
1943 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
1944 NULL
, NULL
, &gpiolib_operations
);
1947 subsys_initcall(gpiolib_debugfs_init
);
1949 #endif /* DEBUG_FS */