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 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
52 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
54 static DEFINE_MUTEX(gpio_lookup_lock
);
55 static LIST_HEAD(gpio_lookup_list
);
56 LIST_HEAD(gpio_chips
);
58 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
64 * Convert a GPIO number to its descriptor
66 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
68 if (WARN(!gpio_is_valid(gpio
), "invalid GPIO %d\n", gpio
))
71 return &gpio_desc
[gpio
];
73 EXPORT_SYMBOL_GPL(gpio_to_desc
);
76 * Get the GPIO descriptor corresponding to the given hw number for this chip.
78 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*chip
,
81 if (hwnum
>= chip
->ngpio
)
82 return ERR_PTR(-EINVAL
);
84 return &chip
->desc
[hwnum
];
88 * Convert a GPIO descriptor to the integer namespace.
89 * This should disappear in the future but is needed since we still
90 * use GPIO numbers for error messages and sysfs nodes
92 int desc_to_gpio(const struct gpio_desc
*desc
)
94 return desc
- &gpio_desc
[0];
96 EXPORT_SYMBOL_GPL(desc_to_gpio
);
100 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
101 * @desc: descriptor to return the chip of
103 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
105 return desc
? desc
->chip
: NULL
;
107 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
109 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
110 static int gpiochip_find_base(int ngpio
)
112 struct gpio_chip
*chip
;
113 int base
= ARCH_NR_GPIOS
- ngpio
;
115 list_for_each_entry_reverse(chip
, &gpio_chips
, list
) {
116 /* found a free space? */
117 if (chip
->base
+ chip
->ngpio
<= base
)
120 /* nope, check the space right before the chip */
121 base
= chip
->base
- ngpio
;
124 if (gpio_is_valid(base
)) {
125 pr_debug("%s: found new base at %d\n", __func__
, base
);
128 pr_err("%s: cannot find free range\n", __func__
);
134 * gpiod_get_direction - return the current direction of a GPIO
135 * @desc: GPIO to get the direction of
137 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
139 * This function may sleep if gpiod_cansleep() is true.
141 int gpiod_get_direction(const struct gpio_desc
*desc
)
143 struct gpio_chip
*chip
;
145 int status
= -EINVAL
;
147 chip
= gpiod_to_chip(desc
);
148 offset
= gpio_chip_hwgpio(desc
);
150 if (!chip
->get_direction
)
153 status
= chip
->get_direction(chip
, offset
);
155 /* GPIOF_DIR_IN, or other positive */
157 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
158 * so it does not affect constness per se */
159 clear_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
163 set_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
167 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
170 * Add a new chip to the global chips list, keeping the list of chips sorted
173 * Return -EBUSY if the new chip overlaps with some other chip's integer
176 static int gpiochip_add_to_list(struct gpio_chip
*chip
)
178 struct list_head
*pos
= &gpio_chips
;
179 struct gpio_chip
*_chip
;
182 /* find where to insert our chip */
183 list_for_each(pos
, &gpio_chips
) {
184 _chip
= list_entry(pos
, struct gpio_chip
, list
);
185 /* shall we insert before _chip? */
186 if (_chip
->base
>= chip
->base
+ chip
->ngpio
)
190 /* are we stepping on the chip right before? */
191 if (pos
!= &gpio_chips
&& pos
->prev
!= &gpio_chips
) {
192 _chip
= list_entry(pos
->prev
, struct gpio_chip
, list
);
193 if (_chip
->base
+ _chip
->ngpio
> chip
->base
) {
195 "GPIO integer space overlap, cannot add chip\n");
201 list_add_tail(&chip
->list
, pos
);
207 * gpiochip_add() - register a gpio_chip
208 * @chip: the chip to register, with chip->base initialized
209 * Context: potentially before irqs or kmalloc will work
211 * Returns a negative errno if the chip can't be registered, such as
212 * because the chip->base is invalid or already associated with a
213 * different chip. Otherwise it returns zero as a success code.
215 * When gpiochip_add() is called very early during boot, so that GPIOs
216 * can be freely used, the chip->dev device must be registered before
217 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
218 * for GPIOs will fail rudely.
220 * If chip->base is negative, this requests dynamic assignment of
221 * a range of valid GPIOs.
223 int gpiochip_add(struct gpio_chip
*chip
)
228 int base
= chip
->base
;
230 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
236 spin_lock_irqsave(&gpio_lock
, flags
);
239 base
= gpiochip_find_base(chip
->ngpio
);
247 status
= gpiochip_add_to_list(chip
);
250 chip
->desc
= &gpio_desc
[chip
->base
];
252 for (id
= 0; id
< chip
->ngpio
; id
++) {
253 struct gpio_desc
*desc
= &chip
->desc
[id
];
256 /* REVISIT: most hardware initializes GPIOs as
257 * inputs (often with pullups enabled) so power
258 * usage is minimized. Linux code should set the
259 * gpio direction first thing; but until it does,
260 * and in case chip->get_direction is not set,
261 * we may expose the wrong direction in sysfs.
263 desc
->flags
= !chip
->direction_input
269 spin_unlock_irqrestore(&gpio_lock
, flags
);
271 #ifdef CONFIG_PINCTRL
272 INIT_LIST_HEAD(&chip
->pin_ranges
);
275 of_gpiochip_add(chip
);
276 acpi_gpiochip_add(chip
);
281 status
= gpiochip_export(chip
);
285 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__
,
286 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
287 chip
->label
? : "generic");
292 spin_unlock_irqrestore(&gpio_lock
, flags
);
294 /* failures here can mean systems won't boot... */
295 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__
,
296 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
297 chip
->label
? : "generic");
300 EXPORT_SYMBOL_GPL(gpiochip_add
);
302 /* Forward-declaration */
303 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
);
306 * gpiochip_remove() - unregister a gpio_chip
307 * @chip: the chip to unregister
309 * A gpio_chip with any GPIOs still requested may not be removed.
311 void gpiochip_remove(struct gpio_chip
*chip
)
316 acpi_gpiochip_remove(chip
);
318 spin_lock_irqsave(&gpio_lock
, flags
);
320 gpiochip_irqchip_remove(chip
);
321 gpiochip_remove_pin_ranges(chip
);
322 of_gpiochip_remove(chip
);
324 for (id
= 0; id
< chip
->ngpio
; id
++) {
325 if (test_bit(FLAG_REQUESTED
, &chip
->desc
[id
].flags
))
326 dev_crit(chip
->dev
, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
328 for (id
= 0; id
< chip
->ngpio
; id
++)
329 chip
->desc
[id
].chip
= NULL
;
331 list_del(&chip
->list
);
332 spin_unlock_irqrestore(&gpio_lock
, flags
);
333 gpiochip_unexport(chip
);
335 EXPORT_SYMBOL_GPL(gpiochip_remove
);
338 * gpiochip_find() - iterator for locating a specific gpio_chip
339 * @data: data to pass to match function
340 * @callback: Callback function to check gpio_chip
342 * Similar to bus_find_device. It returns a reference to a gpio_chip as
343 * determined by a user supplied @match callback. The callback should return
344 * 0 if the device doesn't match and non-zero if it does. If the callback is
345 * non-zero, this function will return to the caller and not iterate over any
348 struct gpio_chip
*gpiochip_find(void *data
,
349 int (*match
)(struct gpio_chip
*chip
,
352 struct gpio_chip
*chip
;
355 spin_lock_irqsave(&gpio_lock
, flags
);
356 list_for_each_entry(chip
, &gpio_chips
, list
)
357 if (match(chip
, data
))
361 if (&chip
->list
== &gpio_chips
)
363 spin_unlock_irqrestore(&gpio_lock
, flags
);
367 EXPORT_SYMBOL_GPL(gpiochip_find
);
369 static int gpiochip_match_name(struct gpio_chip
*chip
, void *data
)
371 const char *name
= data
;
373 return !strcmp(chip
->label
, name
);
376 static struct gpio_chip
*find_chip_by_name(const char *name
)
378 return gpiochip_find((void *)name
, gpiochip_match_name
);
381 #ifdef CONFIG_GPIOLIB_IRQCHIP
384 * The following is irqchip helper code for gpiochips.
388 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
389 * @gpiochip: the gpiochip to set the irqchip chain to
390 * @irqchip: the irqchip to chain to the gpiochip
391 * @parent_irq: the irq number corresponding to the parent IRQ for this
393 * @parent_handler: the parent interrupt handler for the accumulated IRQ
394 * coming out of the gpiochip. If the interrupt is nested rather than
395 * cascaded, pass NULL in this handler argument
397 void gpiochip_set_chained_irqchip(struct gpio_chip
*gpiochip
,
398 struct irq_chip
*irqchip
,
400 irq_flow_handler_t parent_handler
)
404 if (!gpiochip
->irqdomain
) {
405 chip_err(gpiochip
, "called %s before setting up irqchip\n",
410 if (parent_handler
) {
411 if (gpiochip
->can_sleep
) {
413 "you cannot have chained interrupts on a "
414 "chip that may sleep\n");
418 * The parent irqchip is already using the chip_data for this
419 * irqchip, so our callbacks simply use the handler_data.
421 irq_set_handler_data(parent_irq
, gpiochip
);
422 irq_set_chained_handler(parent_irq
, parent_handler
);
425 /* Set the parent IRQ for all affected IRQs */
426 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
427 irq_set_parent(irq_find_mapping(gpiochip
->irqdomain
, offset
),
430 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip
);
433 * This lock class tells lockdep that GPIO irqs are in a different
434 * category than their parents, so it won't report false recursion.
436 static struct lock_class_key gpiochip_irq_lock_class
;
439 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
440 * @d: the irqdomain used by this irqchip
441 * @irq: the global irq number used by this GPIO irqchip irq
442 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
444 * This function will set up the mapping for a certain IRQ line on a
445 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
446 * stored inside the gpiochip.
448 static int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
449 irq_hw_number_t hwirq
)
451 struct gpio_chip
*chip
= d
->host_data
;
453 irq_set_chip_data(irq
, chip
);
454 irq_set_lockdep_class(irq
, &gpiochip_irq_lock_class
);
455 irq_set_chip_and_handler(irq
, chip
->irqchip
, chip
->irq_handler
);
456 /* Chips that can sleep need nested thread handlers */
457 if (chip
->can_sleep
&& !chip
->irq_not_threaded
)
458 irq_set_nested_thread(irq
, 1);
460 set_irq_flags(irq
, IRQF_VALID
);
462 irq_set_noprobe(irq
);
465 * No set-up of the hardware will happen if IRQ_TYPE_NONE
466 * is passed as default type.
468 if (chip
->irq_default_type
!= IRQ_TYPE_NONE
)
469 irq_set_irq_type(irq
, chip
->irq_default_type
);
474 static void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
476 struct gpio_chip
*chip
= d
->host_data
;
479 set_irq_flags(irq
, 0);
482 irq_set_nested_thread(irq
, 0);
483 irq_set_chip_and_handler(irq
, NULL
, NULL
);
484 irq_set_chip_data(irq
, NULL
);
487 static const struct irq_domain_ops gpiochip_domain_ops
= {
488 .map
= gpiochip_irq_map
,
489 .unmap
= gpiochip_irq_unmap
,
490 /* Virtually all GPIO irqchips are twocell:ed */
491 .xlate
= irq_domain_xlate_twocell
,
494 static int gpiochip_irq_reqres(struct irq_data
*d
)
496 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
498 if (gpio_lock_as_irq(chip
, d
->hwirq
)) {
500 "unable to lock HW IRQ %lu for IRQ\n",
507 static void gpiochip_irq_relres(struct irq_data
*d
)
509 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
511 gpio_unlock_as_irq(chip
, d
->hwirq
);
514 static int gpiochip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
516 return irq_find_mapping(chip
->irqdomain
, offset
);
520 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
521 * @gpiochip: the gpiochip to remove the irqchip from
523 * This is called only from gpiochip_remove()
525 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
)
529 acpi_gpiochip_free_interrupts(gpiochip
);
531 /* Remove all IRQ mappings and delete the domain */
532 if (gpiochip
->irqdomain
) {
533 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
535 irq_find_mapping(gpiochip
->irqdomain
, offset
));
536 irq_domain_remove(gpiochip
->irqdomain
);
539 if (gpiochip
->irqchip
) {
540 gpiochip
->irqchip
->irq_request_resources
= NULL
;
541 gpiochip
->irqchip
->irq_release_resources
= NULL
;
542 gpiochip
->irqchip
= NULL
;
547 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
548 * @gpiochip: the gpiochip to add the irqchip to
549 * @irqchip: the irqchip to add to the gpiochip
550 * @first_irq: if not dynamically assigned, the base (first) IRQ to
551 * allocate gpiochip irqs from
552 * @handler: the irq handler to use (often a predefined irq core function)
553 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
554 * to have the core avoid setting up any default type in the hardware.
556 * This function closely associates a certain irqchip with a certain
557 * gpiochip, providing an irq domain to translate the local IRQs to
558 * global irqs in the gpiolib core, and making sure that the gpiochip
559 * is passed as chip data to all related functions. Driver callbacks
560 * need to use container_of() to get their local state containers back
561 * from the gpiochip passed as chip data. An irqdomain will be stored
562 * in the gpiochip that shall be used by the driver to handle IRQ number
563 * translation. The gpiochip will need to be initialized and registered
564 * before calling this function.
566 * This function will handle two cell:ed simple IRQs and assumes all
567 * the pins on the gpiochip can generate a unique IRQ. Everything else
568 * need to be open coded.
570 int gpiochip_irqchip_add(struct gpio_chip
*gpiochip
,
571 struct irq_chip
*irqchip
,
572 unsigned int first_irq
,
573 irq_flow_handler_t handler
,
576 struct device_node
*of_node
;
578 unsigned irq_base
= 0;
580 if (!gpiochip
|| !irqchip
)
583 if (!gpiochip
->dev
) {
584 pr_err("missing gpiochip .dev parent pointer\n");
587 of_node
= gpiochip
->dev
->of_node
;
588 #ifdef CONFIG_OF_GPIO
590 * If the gpiochip has an assigned OF node this takes precendence
591 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
593 if (gpiochip
->of_node
)
594 of_node
= gpiochip
->of_node
;
596 gpiochip
->irqchip
= irqchip
;
597 gpiochip
->irq_handler
= handler
;
598 gpiochip
->irq_default_type
= type
;
599 gpiochip
->to_irq
= gpiochip_to_irq
;
600 gpiochip
->irqdomain
= irq_domain_add_simple(of_node
,
601 gpiochip
->ngpio
, first_irq
,
602 &gpiochip_domain_ops
, gpiochip
);
603 if (!gpiochip
->irqdomain
) {
604 gpiochip
->irqchip
= NULL
;
607 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
608 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
611 * Prepare the mapping since the irqchip shall be orthogonal to
612 * any gpiochip calls. If the first_irq was zero, this is
613 * necessary to allocate descriptors for all IRQs.
615 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++) {
616 irq_base
= irq_create_mapping(gpiochip
->irqdomain
, offset
);
619 * Store the base into the gpiochip to be used when
620 * unmapping the irqs.
622 gpiochip
->irq_base
= irq_base
;
625 acpi_gpiochip_request_interrupts(gpiochip
);
629 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add
);
631 #else /* CONFIG_GPIOLIB_IRQCHIP */
633 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
) {}
635 #endif /* CONFIG_GPIOLIB_IRQCHIP */
637 #ifdef CONFIG_PINCTRL
640 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
641 * @chip: the gpiochip to add the range for
642 * @pinctrl: the dev_name() of the pin controller to map to
643 * @gpio_offset: the start offset in the current gpio_chip number space
644 * @pin_group: name of the pin group inside the pin controller
646 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
647 struct pinctrl_dev
*pctldev
,
648 unsigned int gpio_offset
, const char *pin_group
)
650 struct gpio_pin_range
*pin_range
;
653 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
655 chip_err(chip
, "failed to allocate pin ranges\n");
659 /* Use local offset as range ID */
660 pin_range
->range
.id
= gpio_offset
;
661 pin_range
->range
.gc
= chip
;
662 pin_range
->range
.name
= chip
->label
;
663 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
664 pin_range
->pctldev
= pctldev
;
666 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
667 &pin_range
->range
.pins
,
668 &pin_range
->range
.npins
);
674 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
676 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
677 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
678 pinctrl_dev_get_devname(pctldev
), pin_group
);
680 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
684 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
687 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
688 * @chip: the gpiochip to add the range for
689 * @pinctrl_name: the dev_name() of the pin controller to map to
690 * @gpio_offset: the start offset in the current gpio_chip number space
691 * @pin_offset: the start offset in the pin controller number space
692 * @npins: the number of pins from the offset of each pin space (GPIO and
693 * pin controller) to accumulate in this range
695 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
696 unsigned int gpio_offset
, unsigned int pin_offset
,
699 struct gpio_pin_range
*pin_range
;
702 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
704 chip_err(chip
, "failed to allocate pin ranges\n");
708 /* Use local offset as range ID */
709 pin_range
->range
.id
= gpio_offset
;
710 pin_range
->range
.gc
= chip
;
711 pin_range
->range
.name
= chip
->label
;
712 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
713 pin_range
->range
.pin_base
= pin_offset
;
714 pin_range
->range
.npins
= npins
;
715 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
717 if (IS_ERR(pin_range
->pctldev
)) {
718 ret
= PTR_ERR(pin_range
->pctldev
);
719 chip_err(chip
, "could not create pin range\n");
723 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
724 gpio_offset
, gpio_offset
+ npins
- 1,
726 pin_offset
, pin_offset
+ npins
- 1);
728 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
732 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
735 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
736 * @chip: the chip to remove all the mappings for
738 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
740 struct gpio_pin_range
*pin_range
, *tmp
;
742 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
743 list_del(&pin_range
->node
);
744 pinctrl_remove_gpio_range(pin_range
->pctldev
,
749 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
751 #endif /* CONFIG_PINCTRL */
753 /* These "optional" allocation calls help prevent drivers from stomping
754 * on each other, and help provide better diagnostics in debugfs.
755 * They're called even less than the "set direction" calls.
757 static int __gpiod_request(struct gpio_desc
*desc
, const char *label
)
759 struct gpio_chip
*chip
= desc
->chip
;
763 spin_lock_irqsave(&gpio_lock
, flags
);
765 /* NOTE: gpio_request() can be called in early boot,
766 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
769 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
770 desc_set_label(desc
, label
? : "?");
778 /* chip->request may sleep */
779 spin_unlock_irqrestore(&gpio_lock
, flags
);
780 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
781 spin_lock_irqsave(&gpio_lock
, flags
);
784 desc_set_label(desc
, NULL
);
785 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
789 if (chip
->get_direction
) {
790 /* chip->get_direction may sleep */
791 spin_unlock_irqrestore(&gpio_lock
, flags
);
792 gpiod_get_direction(desc
);
793 spin_lock_irqsave(&gpio_lock
, flags
);
796 spin_unlock_irqrestore(&gpio_lock
, flags
);
800 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
802 int status
= -EPROBE_DEFER
;
803 struct gpio_chip
*chip
;
806 pr_warn("%s: invalid GPIO\n", __func__
);
814 if (try_module_get(chip
->owner
)) {
815 status
= __gpiod_request(desc
, label
);
817 module_put(chip
->owner
);
822 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
827 static bool __gpiod_free(struct gpio_desc
*desc
)
831 struct gpio_chip
*chip
;
835 gpiod_unexport(desc
);
837 spin_lock_irqsave(&gpio_lock
, flags
);
840 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
842 spin_unlock_irqrestore(&gpio_lock
, flags
);
843 might_sleep_if(chip
->can_sleep
);
844 chip
->free(chip
, gpio_chip_hwgpio(desc
));
845 spin_lock_irqsave(&gpio_lock
, flags
);
847 desc_set_label(desc
, NULL
);
848 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
849 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
850 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
851 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
855 spin_unlock_irqrestore(&gpio_lock
, flags
);
859 void gpiod_free(struct gpio_desc
*desc
)
861 if (desc
&& __gpiod_free(desc
))
862 module_put(desc
->chip
->owner
);
864 WARN_ON(extra_checks
);
868 * gpiochip_is_requested - return string iff signal was requested
869 * @chip: controller managing the signal
870 * @offset: of signal within controller's 0..(ngpio - 1) range
872 * Returns NULL if the GPIO is not currently requested, else a string.
873 * The string returned is the label passed to gpio_request(); if none has been
874 * passed it is a meaningless, non-NULL constant.
876 * This function is for use by GPIO controller drivers. The label can
877 * help with diagnostics, and knowing that the signal is used as a GPIO
878 * can help avoid accidentally multiplexing it to another controller.
880 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
882 struct gpio_desc
*desc
;
884 if (!GPIO_OFFSET_VALID(chip
, offset
))
887 desc
= &chip
->desc
[offset
];
889 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
893 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
896 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
897 * @desc: GPIO descriptor to request
898 * @label: label for the GPIO
900 * Function allows GPIO chip drivers to request and use their own GPIO
901 * descriptors via gpiolib API. Difference to gpiod_request() is that this
902 * function will not increase reference count of the GPIO chip module. This
903 * allows the GPIO chip module to be unloaded as needed (we assume that the
904 * GPIO chip driver handles freeing the GPIOs it has requested).
906 struct gpio_desc
*gpiochip_request_own_desc(struct gpio_chip
*chip
, u16 hwnum
,
909 struct gpio_desc
*desc
= gpiochip_get_desc(chip
, hwnum
);
913 chip_err(chip
, "failed to get GPIO descriptor\n");
917 err
= __gpiod_request(desc
, label
);
923 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc
);
926 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
927 * @desc: GPIO descriptor to free
929 * Function frees the given GPIO requested previously with
930 * gpiochip_request_own_desc().
932 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
937 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc
);
939 /* Drivers MUST set GPIO direction before making get/set calls. In
940 * some cases this is done in early boot, before IRQs are enabled.
942 * As a rule these aren't called more than once (except for drivers
943 * using the open-drain emulation idiom) so these are natural places
944 * to accumulate extra debugging checks. Note that we can't (yet)
945 * rely on gpio_request() having been called beforehand.
949 * gpiod_direction_input - set the GPIO direction to input
950 * @desc: GPIO to set to input
952 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
953 * be called safely on it.
955 * Return 0 in case of success, else an error code.
957 int gpiod_direction_input(struct gpio_desc
*desc
)
959 struct gpio_chip
*chip
;
960 int status
= -EINVAL
;
962 if (!desc
|| !desc
->chip
) {
963 pr_warn("%s: invalid GPIO\n", __func__
);
968 if (!chip
->get
|| !chip
->direction_input
) {
970 "%s: missing get() or direction_input() operations\n",
975 status
= chip
->direction_input(chip
, gpio_chip_hwgpio(desc
));
977 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
979 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
983 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
985 static int _gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
987 struct gpio_chip
*chip
;
988 int status
= -EINVAL
;
990 /* GPIOs used for IRQs shall not be set as output */
991 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
993 "%s: tried to set a GPIO tied to an IRQ as output\n",
998 /* Open drain pin should not be driven to 1 */
999 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1000 return gpiod_direction_input(desc
);
1002 /* Open source pin should not be driven to 0 */
1003 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1004 return gpiod_direction_input(desc
);
1007 if (!chip
->set
|| !chip
->direction_output
) {
1009 "%s: missing set() or direction_output() operations\n",
1014 status
= chip
->direction_output(chip
, gpio_chip_hwgpio(desc
), value
);
1016 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1017 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1018 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1023 * gpiod_direction_output_raw - set the GPIO direction to output
1024 * @desc: GPIO to set to output
1025 * @value: initial output value of the GPIO
1027 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1028 * be called safely on it. The initial value of the output must be specified
1029 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1031 * Return 0 in case of success, else an error code.
1033 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1035 if (!desc
|| !desc
->chip
) {
1036 pr_warn("%s: invalid GPIO\n", __func__
);
1039 return _gpiod_direction_output_raw(desc
, value
);
1041 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
1044 * gpiod_direction_output - set the GPIO direction to output
1045 * @desc: GPIO to set to output
1046 * @value: initial output value of the GPIO
1048 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1049 * be called safely on it. The initial value of the output must be specified
1050 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1053 * Return 0 in case of success, else an error code.
1055 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1057 if (!desc
|| !desc
->chip
) {
1058 pr_warn("%s: invalid GPIO\n", __func__
);
1061 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1063 return _gpiod_direction_output_raw(desc
, value
);
1065 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1068 * gpiod_set_debounce - sets @debounce time for a @gpio
1069 * @gpio: the gpio to set debounce time
1070 * @debounce: debounce time is microseconds
1072 * returns -ENOTSUPP if the controller does not support setting
1075 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1077 struct gpio_chip
*chip
;
1079 if (!desc
|| !desc
->chip
) {
1080 pr_warn("%s: invalid GPIO\n", __func__
);
1085 if (!chip
->set
|| !chip
->set_debounce
) {
1087 "%s: missing set() or set_debounce() operations\n",
1092 return chip
->set_debounce(chip
, gpio_chip_hwgpio(desc
), debounce
);
1094 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1097 * gpiod_is_active_low - test whether a GPIO is active-low or not
1098 * @desc: the gpio descriptor to test
1100 * Returns 1 if the GPIO is active-low, 0 otherwise.
1102 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1104 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1106 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1108 /* I/O calls are only valid after configuration completed; the relevant
1109 * "is this a valid GPIO" error checks should already have been done.
1111 * "Get" operations are often inlinable as reading a pin value register,
1112 * and masking the relevant bit in that register.
1114 * When "set" operations are inlinable, they involve writing that mask to
1115 * one register to set a low value, or a different register to set it high.
1116 * Otherwise locking is needed, so there may be little value to inlining.
1118 *------------------------------------------------------------------------
1120 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1121 * have requested the GPIO. That can include implicit requesting by
1122 * a direction setting call. Marking a gpio as requested locks its chip
1123 * in memory, guaranteeing that these table lookups need no more locking
1124 * and that gpiochip_remove() will fail.
1126 * REVISIT when debugging, consider adding some instrumentation to ensure
1127 * that the GPIO was actually requested.
1130 static bool _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1132 struct gpio_chip
*chip
;
1137 offset
= gpio_chip_hwgpio(desc
);
1138 value
= chip
->get
? chip
->get(chip
, offset
) : false;
1139 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1144 * gpiod_get_raw_value() - return a gpio's raw value
1145 * @desc: gpio whose value will be returned
1147 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1148 * its ACTIVE_LOW status.
1150 * This function should be called from contexts where we cannot sleep, and will
1151 * complain if the GPIO chip functions potentially sleep.
1153 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
1157 /* Should be using gpio_get_value_cansleep() */
1158 WARN_ON(desc
->chip
->can_sleep
);
1159 return _gpiod_get_raw_value(desc
);
1161 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1164 * gpiod_get_value() - return a gpio's value
1165 * @desc: gpio whose value will be returned
1167 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1170 * This function should be called from contexts where we cannot sleep, and will
1171 * complain if the GPIO chip functions potentially sleep.
1173 int gpiod_get_value(const struct gpio_desc
*desc
)
1178 /* Should be using gpio_get_value_cansleep() */
1179 WARN_ON(desc
->chip
->can_sleep
);
1181 value
= _gpiod_get_raw_value(desc
);
1182 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1187 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1190 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1191 * @desc: gpio descriptor whose state need to be set.
1192 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1194 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, bool value
)
1197 struct gpio_chip
*chip
= desc
->chip
;
1198 int offset
= gpio_chip_hwgpio(desc
);
1201 err
= chip
->direction_input(chip
, offset
);
1203 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1205 err
= chip
->direction_output(chip
, offset
, 0);
1207 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1209 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1212 "%s: Error in set_value for open drain err %d\n",
1217 * _gpio_set_open_source_value() - Set the open source gpio's value.
1218 * @desc: gpio descriptor whose state need to be set.
1219 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1221 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, bool value
)
1224 struct gpio_chip
*chip
= desc
->chip
;
1225 int offset
= gpio_chip_hwgpio(desc
);
1228 err
= chip
->direction_output(chip
, offset
, 1);
1230 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1232 err
= chip
->direction_input(chip
, offset
);
1234 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1236 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
1239 "%s: Error in set_value for open source err %d\n",
1243 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, bool value
)
1245 struct gpio_chip
*chip
;
1248 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1249 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1250 _gpio_set_open_drain_value(desc
, value
);
1251 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1252 _gpio_set_open_source_value(desc
, value
);
1254 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
1258 * gpiod_set_raw_value() - assign a gpio's raw value
1259 * @desc: gpio whose value will be assigned
1260 * @value: value to assign
1262 * Set the raw value of the GPIO, i.e. the value of its physical line without
1263 * regard for its ACTIVE_LOW status.
1265 * This function should be called from contexts where we cannot sleep, and will
1266 * complain if the GPIO chip functions potentially sleep.
1268 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
1272 /* Should be using gpio_set_value_cansleep() */
1273 WARN_ON(desc
->chip
->can_sleep
);
1274 _gpiod_set_raw_value(desc
, value
);
1276 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
1279 * gpiod_set_value() - assign a gpio's value
1280 * @desc: gpio whose value will be assigned
1281 * @value: value to assign
1283 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1286 * This function should be called from contexts where we cannot sleep, and will
1287 * complain if the GPIO chip functions potentially sleep.
1289 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
1293 /* Should be using gpio_set_value_cansleep() */
1294 WARN_ON(desc
->chip
->can_sleep
);
1295 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1297 _gpiod_set_raw_value(desc
, value
);
1299 EXPORT_SYMBOL_GPL(gpiod_set_value
);
1302 * gpiod_cansleep() - report whether gpio value access may sleep
1303 * @desc: gpio to check
1306 int gpiod_cansleep(const struct gpio_desc
*desc
)
1310 return desc
->chip
->can_sleep
;
1312 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
1315 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1316 * @desc: gpio whose IRQ will be returned (already requested)
1318 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1321 int gpiod_to_irq(const struct gpio_desc
*desc
)
1323 struct gpio_chip
*chip
;
1329 offset
= gpio_chip_hwgpio(desc
);
1330 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
1332 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
1335 * gpio_lock_as_irq() - lock a GPIO to be used as IRQ
1336 * @chip: the chip the GPIO to lock belongs to
1337 * @offset: the offset of the GPIO to lock as IRQ
1339 * This is used directly by GPIO drivers that want to lock down
1340 * a certain GPIO line to be used for IRQs.
1342 int gpio_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
1344 if (offset
>= chip
->ngpio
)
1347 if (test_bit(FLAG_IS_OUT
, &chip
->desc
[offset
].flags
)) {
1349 "%s: tried to flag a GPIO set as output for IRQ\n",
1354 set_bit(FLAG_USED_AS_IRQ
, &chip
->desc
[offset
].flags
);
1357 EXPORT_SYMBOL_GPL(gpio_lock_as_irq
);
1360 * gpio_unlock_as_irq() - unlock a GPIO used as IRQ
1361 * @chip: the chip the GPIO to lock belongs to
1362 * @offset: the offset of the GPIO to lock as IRQ
1364 * This is used directly by GPIO drivers that want to indicate
1365 * that a certain GPIO is no longer used exclusively for IRQ.
1367 void gpio_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
1369 if (offset
>= chip
->ngpio
)
1372 clear_bit(FLAG_USED_AS_IRQ
, &chip
->desc
[offset
].flags
);
1374 EXPORT_SYMBOL_GPL(gpio_unlock_as_irq
);
1377 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1378 * @desc: gpio whose value will be returned
1380 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1381 * its ACTIVE_LOW status.
1383 * This function is to be called from contexts that can sleep.
1385 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
1387 might_sleep_if(extra_checks
);
1390 return _gpiod_get_raw_value(desc
);
1392 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
1395 * gpiod_get_value_cansleep() - return a gpio's value
1396 * @desc: gpio whose value will be returned
1398 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1401 * This function is to be called from contexts that can sleep.
1403 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
1407 might_sleep_if(extra_checks
);
1411 value
= _gpiod_get_raw_value(desc
);
1412 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1417 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
1420 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1421 * @desc: gpio whose value will be assigned
1422 * @value: value to assign
1424 * Set the raw value of the GPIO, i.e. the value of its physical line without
1425 * regard for its ACTIVE_LOW status.
1427 * This function is to be called from contexts that can sleep.
1429 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
1431 might_sleep_if(extra_checks
);
1434 _gpiod_set_raw_value(desc
, value
);
1436 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
1439 * gpiod_set_value_cansleep() - assign a gpio's value
1440 * @desc: gpio whose value will be assigned
1441 * @value: value to assign
1443 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1446 * This function is to be called from contexts that can sleep.
1448 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
1450 might_sleep_if(extra_checks
);
1454 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1456 _gpiod_set_raw_value(desc
, value
);
1458 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
1461 * gpiod_add_lookup_table() - register GPIO device consumers
1462 * @table: table of consumers to register
1464 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
1466 mutex_lock(&gpio_lookup_lock
);
1468 list_add_tail(&table
->list
, &gpio_lookup_list
);
1470 mutex_unlock(&gpio_lookup_lock
);
1473 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
1475 enum gpio_lookup_flags
*flags
)
1477 static const char *suffixes
[] = { "gpios", "gpio" };
1478 char prop_name
[32]; /* 32 is max size of property name */
1479 enum of_gpio_flags of_flags
;
1480 struct gpio_desc
*desc
;
1483 for (i
= 0; i
< ARRAY_SIZE(suffixes
); i
++) {
1485 snprintf(prop_name
, 32, "%s-%s", con_id
, suffixes
[i
]);
1487 snprintf(prop_name
, 32, "%s", suffixes
[i
]);
1489 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
1491 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
1498 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
1499 *flags
|= GPIO_ACTIVE_LOW
;
1504 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
1506 enum gpio_lookup_flags
*flags
)
1508 struct acpi_gpio_info info
;
1509 struct gpio_desc
*desc
;
1511 desc
= acpi_get_gpiod_by_index(dev
, idx
, &info
);
1515 if (info
.gpioint
&& info
.active_low
)
1516 *flags
|= GPIO_ACTIVE_LOW
;
1521 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
1523 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
1524 struct gpiod_lookup_table
*table
;
1526 mutex_lock(&gpio_lookup_lock
);
1528 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
1529 if (table
->dev_id
&& dev_id
) {
1531 * Valid strings on both ends, must be identical to have
1534 if (!strcmp(table
->dev_id
, dev_id
))
1538 * One of the pointers is NULL, so both must be to have
1541 if (dev_id
== table
->dev_id
)
1548 mutex_unlock(&gpio_lookup_lock
);
1552 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
1554 enum gpio_lookup_flags
*flags
)
1556 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
1557 struct gpiod_lookup_table
*table
;
1558 struct gpiod_lookup
*p
;
1560 table
= gpiod_find_lookup_table(dev
);
1564 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
1565 struct gpio_chip
*chip
;
1567 /* idx must always match exactly */
1571 /* If the lookup entry has a con_id, require exact match */
1572 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
1575 chip
= find_chip_by_name(p
->chip_label
);
1578 dev_err(dev
, "cannot find GPIO chip %s\n",
1580 return ERR_PTR(-ENODEV
);
1583 if (chip
->ngpio
<= p
->chip_hwnum
) {
1585 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1586 idx
, chip
->ngpio
, chip
->label
);
1587 return ERR_PTR(-EINVAL
);
1590 desc
= gpiochip_get_desc(chip
, p
->chip_hwnum
);
1600 * gpiod_get - obtain a GPIO for a given GPIO function
1601 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1602 * @con_id: function within the GPIO consumer
1603 * @flags: optional GPIO initialization flags
1605 * Return the GPIO descriptor corresponding to the function con_id of device
1606 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1607 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
1609 struct gpio_desc
*__must_check
__gpiod_get(struct device
*dev
, const char *con_id
,
1610 enum gpiod_flags flags
)
1612 return gpiod_get_index(dev
, con_id
, 0, flags
);
1614 EXPORT_SYMBOL_GPL(__gpiod_get
);
1617 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1618 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1619 * @con_id: function within the GPIO consumer
1620 * @flags: optional GPIO initialization flags
1622 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1623 * the requested function it will return NULL. This is convenient for drivers
1624 * that need to handle optional GPIOs.
1626 struct gpio_desc
*__must_check
__gpiod_get_optional(struct device
*dev
,
1628 enum gpiod_flags flags
)
1630 return gpiod_get_index_optional(dev
, con_id
, 0, flags
);
1632 EXPORT_SYMBOL_GPL(__gpiod_get_optional
);
1635 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
1636 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1637 * @con_id: function within the GPIO consumer
1638 * @idx: index of the GPIO to obtain in the consumer
1639 * @flags: optional GPIO initialization flags
1641 * This variant of gpiod_get() allows to access GPIOs other than the first
1642 * defined one for functions that define several GPIOs.
1644 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1645 * requested function and/or index, or another IS_ERR() code if an error
1646 * occured while trying to acquire the GPIO.
1648 struct gpio_desc
*__must_check
__gpiod_get_index(struct device
*dev
,
1651 enum gpiod_flags flags
)
1653 struct gpio_desc
*desc
= NULL
;
1655 enum gpio_lookup_flags lookupflags
= 0;
1657 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
1659 /* Using device tree? */
1660 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
) {
1661 dev_dbg(dev
, "using device tree for GPIO lookup\n");
1662 desc
= of_find_gpio(dev
, con_id
, idx
, &lookupflags
);
1663 } else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
)) {
1664 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
1665 desc
= acpi_find_gpio(dev
, con_id
, idx
, &lookupflags
);
1669 * Either we are not using DT or ACPI, or their lookup did not return
1670 * a result. In that case, use platform lookup as a fallback.
1672 if (!desc
|| desc
== ERR_PTR(-ENOENT
)) {
1673 dev_dbg(dev
, "using lookup tables for GPIO lookup\n");
1674 desc
= gpiod_find(dev
, con_id
, idx
, &lookupflags
);
1678 dev_dbg(dev
, "lookup for GPIO %s failed\n", con_id
);
1682 status
= gpiod_request(desc
, con_id
);
1685 return ERR_PTR(status
);
1687 if (lookupflags
& GPIO_ACTIVE_LOW
)
1688 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1689 if (lookupflags
& GPIO_OPEN_DRAIN
)
1690 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1691 if (lookupflags
& GPIO_OPEN_SOURCE
)
1692 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1694 /* No particular flag request, return here... */
1695 if (!(flags
& GPIOD_FLAGS_BIT_DIR_SET
))
1699 if (flags
& GPIOD_FLAGS_BIT_DIR_OUT
)
1700 status
= gpiod_direction_output(desc
,
1701 flags
& GPIOD_FLAGS_BIT_DIR_VAL
);
1703 status
= gpiod_direction_input(desc
);
1706 dev_dbg(dev
, "setup of GPIO %s failed\n", con_id
);
1708 return ERR_PTR(status
);
1713 EXPORT_SYMBOL_GPL(__gpiod_get_index
);
1716 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1718 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1719 * @con_id: function within the GPIO consumer
1720 * @index: index of the GPIO to obtain in the consumer
1721 * @flags: optional GPIO initialization flags
1723 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1724 * specified index was assigned to the requested function it will return NULL.
1725 * This is convenient for drivers that need to handle optional GPIOs.
1727 struct gpio_desc
*__must_check
__gpiod_get_index_optional(struct device
*dev
,
1730 enum gpiod_flags flags
)
1732 struct gpio_desc
*desc
;
1734 desc
= gpiod_get_index(dev
, con_id
, index
, flags
);
1736 if (PTR_ERR(desc
) == -ENOENT
)
1742 EXPORT_SYMBOL_GPL(__gpiod_get_index_optional
);
1745 * gpiod_put - dispose of a GPIO descriptor
1746 * @desc: GPIO descriptor to dispose of
1748 * No descriptor can be used after gpiod_put() has been called on it.
1750 void gpiod_put(struct gpio_desc
*desc
)
1754 EXPORT_SYMBOL_GPL(gpiod_put
);
1756 #ifdef CONFIG_DEBUG_FS
1758 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1761 unsigned gpio
= chip
->base
;
1762 struct gpio_desc
*gdesc
= &chip
->desc
[0];
1766 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
1767 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
1770 gpiod_get_direction(gdesc
);
1771 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
1772 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
1773 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s %s",
1775 is_out
? "out" : "in ",
1777 ? (chip
->get(chip
, i
) ? "hi" : "lo")
1779 is_irq
? "IRQ" : " ");
1780 seq_printf(s
, "\n");
1784 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
1786 unsigned long flags
;
1787 struct gpio_chip
*chip
= NULL
;
1788 loff_t index
= *pos
;
1792 spin_lock_irqsave(&gpio_lock
, flags
);
1793 list_for_each_entry(chip
, &gpio_chips
, list
)
1795 spin_unlock_irqrestore(&gpio_lock
, flags
);
1798 spin_unlock_irqrestore(&gpio_lock
, flags
);
1803 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1805 unsigned long flags
;
1806 struct gpio_chip
*chip
= v
;
1809 spin_lock_irqsave(&gpio_lock
, flags
);
1810 if (list_is_last(&chip
->list
, &gpio_chips
))
1813 ret
= list_entry(chip
->list
.next
, struct gpio_chip
, list
);
1814 spin_unlock_irqrestore(&gpio_lock
, flags
);
1822 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
1826 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
1828 struct gpio_chip
*chip
= v
;
1831 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
1832 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
1835 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
1838 seq_printf(s
, ", %s", chip
->label
);
1839 if (chip
->can_sleep
)
1840 seq_printf(s
, ", can sleep");
1841 seq_printf(s
, ":\n");
1844 chip
->dbg_show(s
, chip
);
1846 gpiolib_dbg_show(s
, chip
);
1851 static const struct seq_operations gpiolib_seq_ops
= {
1852 .start
= gpiolib_seq_start
,
1853 .next
= gpiolib_seq_next
,
1854 .stop
= gpiolib_seq_stop
,
1855 .show
= gpiolib_seq_show
,
1858 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
1860 return seq_open(file
, &gpiolib_seq_ops
);
1863 static const struct file_operations gpiolib_operations
= {
1864 .owner
= THIS_MODULE
,
1865 .open
= gpiolib_open
,
1867 .llseek
= seq_lseek
,
1868 .release
= seq_release
,
1871 static int __init
gpiolib_debugfs_init(void)
1873 /* /sys/kernel/debug/gpio */
1874 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
1875 NULL
, NULL
, &gpiolib_operations
);
1878 subsys_initcall(gpiolib_debugfs_init
);
1880 #endif /* DEBUG_FS */