1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
18 #include <linux/pinctrl/consumer.h>
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/gpio.h>
25 /* Implementation infrastructure for GPIO interfaces.
27 * The GPIO programming interface allows for inlining speed-critical
28 * get/set operations for common cases, so that access to SOC-integrated
29 * GPIOs can sometimes cost only an instruction or two per bit.
33 /* When debugging, extend minimal trust to callers and platform code.
34 * Also emit diagnostic messages that may help initial bringup, when
35 * board setup or driver bugs are most common.
37 * Otherwise, minimize overhead in what may be bitbanging codepaths.
40 #define extra_checks 1
42 #define extra_checks 0
45 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
46 * While any GPIO is requested, its gpio_chip is not removable;
47 * each GPIO's "requested" flag serves as a lock and refcount.
49 DEFINE_SPINLOCK(gpio_lock
);
51 static DEFINE_MUTEX(gpio_lookup_lock
);
52 static LIST_HEAD(gpio_lookup_list
);
53 LIST_HEAD(gpio_chips
);
56 static void gpiochip_free_hogs(struct gpio_chip
*chip
);
57 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
);
60 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
66 * Convert a GPIO number to its descriptor
68 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
70 struct gpio_chip
*chip
;
73 spin_lock_irqsave(&gpio_lock
, flags
);
75 list_for_each_entry(chip
, &gpio_chips
, list
) {
76 if (chip
->base
<= gpio
&& chip
->base
+ chip
->ngpio
> gpio
) {
77 spin_unlock_irqrestore(&gpio_lock
, flags
);
78 return &chip
->desc
[gpio
- chip
->base
];
82 spin_unlock_irqrestore(&gpio_lock
, flags
);
84 if (!gpio_is_valid(gpio
))
85 WARN(1, "invalid GPIO %d\n", gpio
);
89 EXPORT_SYMBOL_GPL(gpio_to_desc
);
92 * Get the GPIO descriptor corresponding to the given hw number for this chip.
94 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*chip
,
97 if (hwnum
>= chip
->ngpio
)
98 return ERR_PTR(-EINVAL
);
100 return &chip
->desc
[hwnum
];
104 * Convert a GPIO descriptor to the integer namespace.
105 * This should disappear in the future but is needed since we still
106 * use GPIO numbers for error messages and sysfs nodes
108 int desc_to_gpio(const struct gpio_desc
*desc
)
110 return desc
->chip
->base
+ (desc
- &desc
->chip
->desc
[0]);
112 EXPORT_SYMBOL_GPL(desc_to_gpio
);
116 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
117 * @desc: descriptor to return the chip of
119 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
121 return desc
? desc
->chip
: NULL
;
123 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
125 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
126 static int gpiochip_find_base(int ngpio
)
128 struct gpio_chip
*chip
;
129 int base
= ARCH_NR_GPIOS
- ngpio
;
131 list_for_each_entry_reverse(chip
, &gpio_chips
, list
) {
132 /* found a free space? */
133 if (chip
->base
+ chip
->ngpio
<= base
)
136 /* nope, check the space right before the chip */
137 base
= chip
->base
- ngpio
;
140 if (gpio_is_valid(base
)) {
141 pr_debug("%s: found new base at %d\n", __func__
, base
);
144 pr_err("%s: cannot find free range\n", __func__
);
150 * gpiod_get_direction - return the current direction of a GPIO
151 * @desc: GPIO to get the direction of
153 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
155 * This function may sleep if gpiod_cansleep() is true.
157 int gpiod_get_direction(struct gpio_desc
*desc
)
159 struct gpio_chip
*chip
;
161 int status
= -EINVAL
;
163 chip
= gpiod_to_chip(desc
);
164 offset
= gpio_chip_hwgpio(desc
);
166 if (!chip
->get_direction
)
169 status
= chip
->get_direction(chip
, offset
);
171 /* GPIOF_DIR_IN, or other positive */
173 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
177 set_bit(FLAG_IS_OUT
, &desc
->flags
);
181 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
184 * Add a new chip to the global chips list, keeping the list of chips sorted
185 * by range(means [base, base + ngpio - 1]) order.
187 * Return -EBUSY if the new chip overlaps with some other chip's integer
190 static int gpiochip_add_to_list(struct gpio_chip
*chip
)
192 struct gpio_chip
*iterator
;
193 struct gpio_chip
*previous
= NULL
;
195 if (list_empty(&gpio_chips
)) {
196 list_add_tail(&chip
->list
, &gpio_chips
);
200 list_for_each_entry(iterator
, &gpio_chips
, list
) {
201 if (iterator
->base
>= chip
->base
+ chip
->ngpio
) {
203 * Iterator is the first GPIO chip so there is no
210 * We found a valid range(means
211 * [base, base + ngpio - 1]) between previous
214 if (previous
->base
+ previous
->ngpio
223 * We are beyond the last chip in the list and iterator now
224 * points to the head.
225 * Let iterator point to the last chip in the list.
228 iterator
= list_last_entry(&gpio_chips
, struct gpio_chip
, list
);
229 if (iterator
->base
+ iterator
->ngpio
<= chip
->base
) {
230 list_add(&chip
->list
, &iterator
->list
);
234 dev_err(chip
->parent
,
235 "GPIO integer space overlap, cannot add chip\n");
239 list_add_tail(&chip
->list
, &iterator
->list
);
244 * Convert a GPIO name to its descriptor
246 static struct gpio_desc
*gpio_name_to_desc(const char * const name
)
248 struct gpio_chip
*chip
;
251 spin_lock_irqsave(&gpio_lock
, flags
);
253 list_for_each_entry(chip
, &gpio_chips
, list
) {
256 for (i
= 0; i
!= chip
->ngpio
; ++i
) {
257 struct gpio_desc
*gpio
= &chip
->desc
[i
];
259 if (!gpio
->name
|| !name
)
262 if (!strcmp(gpio
->name
, name
)) {
263 spin_unlock_irqrestore(&gpio_lock
, flags
);
269 spin_unlock_irqrestore(&gpio_lock
, flags
);
275 * Takes the names from gc->names and checks if they are all unique. If they
276 * are, they are assigned to their gpio descriptors.
278 * Warning if one of the names is already used for a different GPIO.
280 static int gpiochip_set_desc_names(struct gpio_chip
*gc
)
287 /* First check all names if they are unique */
288 for (i
= 0; i
!= gc
->ngpio
; ++i
) {
289 struct gpio_desc
*gpio
;
291 gpio
= gpio_name_to_desc(gc
->names
[i
]);
293 dev_warn(gc
->parent
, "Detected name collision for "
298 /* Then add all names to the GPIO descriptors */
299 for (i
= 0; i
!= gc
->ngpio
; ++i
)
300 gc
->desc
[i
].name
= gc
->names
[i
];
306 * gpiochip_add_data() - register a gpio_chip
307 * @chip: the chip to register, with chip->base initialized
308 * Context: potentially before irqs will work
310 * Returns a negative errno if the chip can't be registered, such as
311 * because the chip->base is invalid or already associated with a
312 * different chip. Otherwise it returns zero as a success code.
314 * When gpiochip_add_data() is called very early during boot, so that GPIOs
315 * can be freely used, the chip->parent device must be registered before
316 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
317 * for GPIOs will fail rudely.
319 * If chip->base is negative, this requests dynamic assignment of
320 * a range of valid GPIOs.
322 int gpiochip_add_data(struct gpio_chip
*chip
, void *data
)
327 int base
= chip
->base
;
328 struct gpio_desc
*descs
;
330 descs
= kcalloc(chip
->ngpio
, sizeof(descs
[0]), GFP_KERNEL
);
336 if (chip
->ngpio
== 0) {
337 chip_err(chip
, "tried to insert a GPIO chip with zero lines\n");
341 spin_lock_irqsave(&gpio_lock
, flags
);
344 base
= gpiochip_find_base(chip
->ngpio
);
347 spin_unlock_irqrestore(&gpio_lock
, flags
);
353 status
= gpiochip_add_to_list(chip
);
355 spin_unlock_irqrestore(&gpio_lock
, flags
);
359 for (id
= 0; id
< chip
->ngpio
; id
++) {
360 struct gpio_desc
*desc
= &descs
[id
];
364 /* REVISIT: most hardware initializes GPIOs as inputs (often
365 * with pullups enabled) so power usage is minimized. Linux
366 * code should set the gpio direction first thing; but until
367 * it does, and in case chip->get_direction is not set, we may
368 * expose the wrong direction in sysfs.
370 desc
->flags
= !chip
->direction_input
? (1 << FLAG_IS_OUT
) : 0;
375 spin_unlock_irqrestore(&gpio_lock
, flags
);
377 #ifdef CONFIG_PINCTRL
378 INIT_LIST_HEAD(&chip
->pin_ranges
);
381 if (!chip
->owner
&& chip
->parent
&& chip
->parent
->driver
)
382 chip
->owner
= chip
->parent
->driver
->owner
;
384 status
= gpiochip_set_desc_names(chip
);
386 goto err_remove_from_list
;
388 status
= of_gpiochip_add(chip
);
390 goto err_remove_chip
;
392 acpi_gpiochip_add(chip
);
394 status
= gpiochip_sysfs_register(chip
);
396 goto err_remove_chip
;
398 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__
,
399 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
400 chip
->label
? : "generic");
405 acpi_gpiochip_remove(chip
);
406 gpiochip_free_hogs(chip
);
407 of_gpiochip_remove(chip
);
408 err_remove_from_list
:
409 spin_lock_irqsave(&gpio_lock
, flags
);
410 list_del(&chip
->list
);
411 spin_unlock_irqrestore(&gpio_lock
, flags
);
416 /* failures here can mean systems won't boot... */
417 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__
,
418 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
419 chip
->label
? : "generic");
422 EXPORT_SYMBOL_GPL(gpiochip_add_data
);
425 * gpiochip_remove() - unregister a gpio_chip
426 * @chip: the chip to unregister
428 * A gpio_chip with any GPIOs still requested may not be removed.
430 void gpiochip_remove(struct gpio_chip
*chip
)
432 struct gpio_desc
*desc
;
435 bool requested
= false;
437 gpiochip_sysfs_unregister(chip
);
439 gpiochip_irqchip_remove(chip
);
441 acpi_gpiochip_remove(chip
);
442 gpiochip_remove_pin_ranges(chip
);
443 gpiochip_free_hogs(chip
);
444 of_gpiochip_remove(chip
);
446 spin_lock_irqsave(&gpio_lock
, flags
);
447 for (id
= 0; id
< chip
->ngpio
; id
++) {
448 desc
= &chip
->desc
[id
];
450 if (test_bit(FLAG_REQUESTED
, &desc
->flags
))
453 list_del(&chip
->list
);
454 spin_unlock_irqrestore(&gpio_lock
, flags
);
457 dev_crit(chip
->parent
,
458 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
463 EXPORT_SYMBOL_GPL(gpiochip_remove
);
466 * gpiochip_find() - iterator for locating a specific gpio_chip
467 * @data: data to pass to match function
468 * @callback: Callback function to check gpio_chip
470 * Similar to bus_find_device. It returns a reference to a gpio_chip as
471 * determined by a user supplied @match callback. The callback should return
472 * 0 if the device doesn't match and non-zero if it does. If the callback is
473 * non-zero, this function will return to the caller and not iterate over any
476 struct gpio_chip
*gpiochip_find(void *data
,
477 int (*match
)(struct gpio_chip
*chip
,
480 struct gpio_chip
*chip
;
483 spin_lock_irqsave(&gpio_lock
, flags
);
484 list_for_each_entry(chip
, &gpio_chips
, list
)
485 if (match(chip
, data
))
489 if (&chip
->list
== &gpio_chips
)
491 spin_unlock_irqrestore(&gpio_lock
, flags
);
495 EXPORT_SYMBOL_GPL(gpiochip_find
);
497 static int gpiochip_match_name(struct gpio_chip
*chip
, void *data
)
499 const char *name
= data
;
501 return !strcmp(chip
->label
, name
);
504 static struct gpio_chip
*find_chip_by_name(const char *name
)
506 return gpiochip_find((void *)name
, gpiochip_match_name
);
509 #ifdef CONFIG_GPIOLIB_IRQCHIP
512 * The following is irqchip helper code for gpiochips.
516 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
517 * @gpiochip: the gpiochip to set the irqchip chain to
518 * @irqchip: the irqchip to chain to the gpiochip
519 * @parent_irq: the irq number corresponding to the parent IRQ for this
521 * @parent_handler: the parent interrupt handler for the accumulated IRQ
522 * coming out of the gpiochip. If the interrupt is nested rather than
523 * cascaded, pass NULL in this handler argument
525 void gpiochip_set_chained_irqchip(struct gpio_chip
*gpiochip
,
526 struct irq_chip
*irqchip
,
528 irq_flow_handler_t parent_handler
)
532 if (!gpiochip
->irqdomain
) {
533 chip_err(gpiochip
, "called %s before setting up irqchip\n",
538 if (parent_handler
) {
539 if (gpiochip
->can_sleep
) {
541 "you cannot have chained interrupts on a "
542 "chip that may sleep\n");
546 * The parent irqchip is already using the chip_data for this
547 * irqchip, so our callbacks simply use the handler_data.
549 irq_set_chained_handler_and_data(parent_irq
, parent_handler
,
552 gpiochip
->irq_parent
= parent_irq
;
555 /* Set the parent IRQ for all affected IRQs */
556 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
557 irq_set_parent(irq_find_mapping(gpiochip
->irqdomain
, offset
),
560 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip
);
563 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
564 * @d: the irqdomain used by this irqchip
565 * @irq: the global irq number used by this GPIO irqchip irq
566 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
568 * This function will set up the mapping for a certain IRQ line on a
569 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
570 * stored inside the gpiochip.
572 static int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
573 irq_hw_number_t hwirq
)
575 struct gpio_chip
*chip
= d
->host_data
;
577 irq_set_chip_data(irq
, chip
);
579 * This lock class tells lockdep that GPIO irqs are in a different
580 * category than their parents, so it won't report false recursion.
582 irq_set_lockdep_class(irq
, chip
->lock_key
);
583 irq_set_chip_and_handler(irq
, chip
->irqchip
, chip
->irq_handler
);
584 /* Chips that can sleep need nested thread handlers */
585 if (chip
->can_sleep
&& !chip
->irq_not_threaded
)
586 irq_set_nested_thread(irq
, 1);
587 irq_set_noprobe(irq
);
590 * No set-up of the hardware will happen if IRQ_TYPE_NONE
591 * is passed as default type.
593 if (chip
->irq_default_type
!= IRQ_TYPE_NONE
)
594 irq_set_irq_type(irq
, chip
->irq_default_type
);
599 static void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
601 struct gpio_chip
*chip
= d
->host_data
;
604 irq_set_nested_thread(irq
, 0);
605 irq_set_chip_and_handler(irq
, NULL
, NULL
);
606 irq_set_chip_data(irq
, NULL
);
609 static const struct irq_domain_ops gpiochip_domain_ops
= {
610 .map
= gpiochip_irq_map
,
611 .unmap
= gpiochip_irq_unmap
,
612 /* Virtually all GPIO irqchips are twocell:ed */
613 .xlate
= irq_domain_xlate_twocell
,
616 static int gpiochip_irq_reqres(struct irq_data
*d
)
618 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
620 if (!try_module_get(chip
->owner
))
623 if (gpiochip_lock_as_irq(chip
, d
->hwirq
)) {
625 "unable to lock HW IRQ %lu for IRQ\n",
627 module_put(chip
->owner
);
633 static void gpiochip_irq_relres(struct irq_data
*d
)
635 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
637 gpiochip_unlock_as_irq(chip
, d
->hwirq
);
638 module_put(chip
->owner
);
641 static int gpiochip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
643 return irq_find_mapping(chip
->irqdomain
, offset
);
647 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
648 * @gpiochip: the gpiochip to remove the irqchip from
650 * This is called only from gpiochip_remove()
652 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
)
656 acpi_gpiochip_free_interrupts(gpiochip
);
658 if (gpiochip
->irq_parent
) {
659 irq_set_chained_handler(gpiochip
->irq_parent
, NULL
);
660 irq_set_handler_data(gpiochip
->irq_parent
, NULL
);
663 /* Remove all IRQ mappings and delete the domain */
664 if (gpiochip
->irqdomain
) {
665 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
667 irq_find_mapping(gpiochip
->irqdomain
, offset
));
668 irq_domain_remove(gpiochip
->irqdomain
);
671 if (gpiochip
->irqchip
) {
672 gpiochip
->irqchip
->irq_request_resources
= NULL
;
673 gpiochip
->irqchip
->irq_release_resources
= NULL
;
674 gpiochip
->irqchip
= NULL
;
679 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
680 * @gpiochip: the gpiochip to add the irqchip to
681 * @irqchip: the irqchip to add to the gpiochip
682 * @first_irq: if not dynamically assigned, the base (first) IRQ to
683 * allocate gpiochip irqs from
684 * @handler: the irq handler to use (often a predefined irq core function)
685 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
686 * to have the core avoid setting up any default type in the hardware.
687 * @lock_key: lockdep class
689 * This function closely associates a certain irqchip with a certain
690 * gpiochip, providing an irq domain to translate the local IRQs to
691 * global irqs in the gpiolib core, and making sure that the gpiochip
692 * is passed as chip data to all related functions. Driver callbacks
693 * need to use gpiochip_get_data() to get their local state containers back
694 * from the gpiochip passed as chip data. An irqdomain will be stored
695 * in the gpiochip that shall be used by the driver to handle IRQ number
696 * translation. The gpiochip will need to be initialized and registered
697 * before calling this function.
699 * This function will handle two cell:ed simple IRQs and assumes all
700 * the pins on the gpiochip can generate a unique IRQ. Everything else
701 * need to be open coded.
703 int _gpiochip_irqchip_add(struct gpio_chip
*gpiochip
,
704 struct irq_chip
*irqchip
,
705 unsigned int first_irq
,
706 irq_flow_handler_t handler
,
708 struct lock_class_key
*lock_key
)
710 struct device_node
*of_node
;
712 unsigned irq_base
= 0;
714 if (!gpiochip
|| !irqchip
)
717 if (!gpiochip
->parent
) {
718 pr_err("missing gpiochip .dev parent pointer\n");
721 of_node
= gpiochip
->parent
->of_node
;
722 #ifdef CONFIG_OF_GPIO
724 * If the gpiochip has an assigned OF node this takes precedence
725 * FIXME: get rid of this and use gpiochip->parent->of_node
728 if (gpiochip
->of_node
)
729 of_node
= gpiochip
->of_node
;
731 gpiochip
->irqchip
= irqchip
;
732 gpiochip
->irq_handler
= handler
;
733 gpiochip
->irq_default_type
= type
;
734 gpiochip
->to_irq
= gpiochip_to_irq
;
735 gpiochip
->lock_key
= lock_key
;
736 gpiochip
->irqdomain
= irq_domain_add_simple(of_node
,
737 gpiochip
->ngpio
, first_irq
,
738 &gpiochip_domain_ops
, gpiochip
);
739 if (!gpiochip
->irqdomain
) {
740 gpiochip
->irqchip
= NULL
;
745 * It is possible for a driver to override this, but only if the
746 * alternative functions are both implemented.
748 if (!irqchip
->irq_request_resources
&&
749 !irqchip
->irq_release_resources
) {
750 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
751 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
755 * Prepare the mapping since the irqchip shall be orthogonal to
756 * any gpiochip calls. If the first_irq was zero, this is
757 * necessary to allocate descriptors for all IRQs.
759 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++) {
760 irq_base
= irq_create_mapping(gpiochip
->irqdomain
, offset
);
763 * Store the base into the gpiochip to be used when
764 * unmapping the irqs.
766 gpiochip
->irq_base
= irq_base
;
769 acpi_gpiochip_request_interrupts(gpiochip
);
773 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add
);
775 #else /* CONFIG_GPIOLIB_IRQCHIP */
777 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
) {}
779 #endif /* CONFIG_GPIOLIB_IRQCHIP */
782 * gpiochip_generic_request() - request the gpio function for a pin
783 * @chip: the gpiochip owning the GPIO
784 * @offset: the offset of the GPIO to request for GPIO function
786 int gpiochip_generic_request(struct gpio_chip
*chip
, unsigned offset
)
788 return pinctrl_request_gpio(chip
->base
+ offset
);
790 EXPORT_SYMBOL_GPL(gpiochip_generic_request
);
793 * gpiochip_generic_free() - free the gpio function from a pin
794 * @chip: the gpiochip to request the gpio function for
795 * @offset: the offset of the GPIO to free from GPIO function
797 void gpiochip_generic_free(struct gpio_chip
*chip
, unsigned offset
)
799 pinctrl_free_gpio(chip
->base
+ offset
);
801 EXPORT_SYMBOL_GPL(gpiochip_generic_free
);
803 #ifdef CONFIG_PINCTRL
806 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
807 * @chip: the gpiochip to add the range for
808 * @pctldev: the pin controller to map to
809 * @gpio_offset: the start offset in the current gpio_chip number space
810 * @pin_group: name of the pin group inside the pin controller
812 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
813 struct pinctrl_dev
*pctldev
,
814 unsigned int gpio_offset
, const char *pin_group
)
816 struct gpio_pin_range
*pin_range
;
819 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
821 chip_err(chip
, "failed to allocate pin ranges\n");
825 /* Use local offset as range ID */
826 pin_range
->range
.id
= gpio_offset
;
827 pin_range
->range
.gc
= chip
;
828 pin_range
->range
.name
= chip
->label
;
829 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
830 pin_range
->pctldev
= pctldev
;
832 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
833 &pin_range
->range
.pins
,
834 &pin_range
->range
.npins
);
840 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
842 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
843 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
844 pinctrl_dev_get_devname(pctldev
), pin_group
);
846 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
850 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
853 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
854 * @chip: the gpiochip to add the range for
855 * @pinctrl_name: the dev_name() of the pin controller to map to
856 * @gpio_offset: the start offset in the current gpio_chip number space
857 * @pin_offset: the start offset in the pin controller number space
858 * @npins: the number of pins from the offset of each pin space (GPIO and
859 * pin controller) to accumulate in this range
861 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
862 unsigned int gpio_offset
, unsigned int pin_offset
,
865 struct gpio_pin_range
*pin_range
;
868 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
870 chip_err(chip
, "failed to allocate pin ranges\n");
874 /* Use local offset as range ID */
875 pin_range
->range
.id
= gpio_offset
;
876 pin_range
->range
.gc
= chip
;
877 pin_range
->range
.name
= chip
->label
;
878 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
879 pin_range
->range
.pin_base
= pin_offset
;
880 pin_range
->range
.npins
= npins
;
881 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
883 if (IS_ERR(pin_range
->pctldev
)) {
884 ret
= PTR_ERR(pin_range
->pctldev
);
885 chip_err(chip
, "could not create pin range\n");
889 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
890 gpio_offset
, gpio_offset
+ npins
- 1,
892 pin_offset
, pin_offset
+ npins
- 1);
894 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
898 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
901 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
902 * @chip: the chip to remove all the mappings for
904 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
906 struct gpio_pin_range
*pin_range
, *tmp
;
908 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
909 list_del(&pin_range
->node
);
910 pinctrl_remove_gpio_range(pin_range
->pctldev
,
915 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
917 #endif /* CONFIG_PINCTRL */
919 /* These "optional" allocation calls help prevent drivers from stomping
920 * on each other, and help provide better diagnostics in debugfs.
921 * They're called even less than the "set direction" calls.
923 static int __gpiod_request(struct gpio_desc
*desc
, const char *label
)
925 struct gpio_chip
*chip
= desc
->chip
;
929 spin_lock_irqsave(&gpio_lock
, flags
);
931 /* NOTE: gpio_request() can be called in early boot,
932 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
935 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
936 desc_set_label(desc
, label
? : "?");
944 /* chip->request may sleep */
945 spin_unlock_irqrestore(&gpio_lock
, flags
);
946 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
947 spin_lock_irqsave(&gpio_lock
, flags
);
950 desc_set_label(desc
, NULL
);
951 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
955 if (chip
->get_direction
) {
956 /* chip->get_direction may sleep */
957 spin_unlock_irqrestore(&gpio_lock
, flags
);
958 gpiod_get_direction(desc
);
959 spin_lock_irqsave(&gpio_lock
, flags
);
963 /* Clear flags that might have been set by the caller before
964 * requesting the GPIO.
966 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
967 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
968 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
970 spin_unlock_irqrestore(&gpio_lock
, flags
);
974 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
976 int status
= -EPROBE_DEFER
;
977 struct gpio_chip
*chip
;
980 pr_warn("%s: invalid GPIO\n", __func__
);
988 if (try_module_get(chip
->owner
)) {
989 status
= __gpiod_request(desc
, label
);
991 module_put(chip
->owner
);
996 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1001 static bool __gpiod_free(struct gpio_desc
*desc
)
1004 unsigned long flags
;
1005 struct gpio_chip
*chip
;
1009 gpiod_unexport(desc
);
1011 spin_lock_irqsave(&gpio_lock
, flags
);
1014 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1016 spin_unlock_irqrestore(&gpio_lock
, flags
);
1017 might_sleep_if(chip
->can_sleep
);
1018 chip
->free(chip
, gpio_chip_hwgpio(desc
));
1019 spin_lock_irqsave(&gpio_lock
, flags
);
1021 desc_set_label(desc
, NULL
);
1022 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1023 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1024 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1025 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1026 clear_bit(FLAG_IS_HOGGED
, &desc
->flags
);
1030 spin_unlock_irqrestore(&gpio_lock
, flags
);
1034 void gpiod_free(struct gpio_desc
*desc
)
1036 if (desc
&& __gpiod_free(desc
))
1037 module_put(desc
->chip
->owner
);
1039 WARN_ON(extra_checks
);
1043 * gpiochip_is_requested - return string iff signal was requested
1044 * @chip: controller managing the signal
1045 * @offset: of signal within controller's 0..(ngpio - 1) range
1047 * Returns NULL if the GPIO is not currently requested, else a string.
1048 * The string returned is the label passed to gpio_request(); if none has been
1049 * passed it is a meaningless, non-NULL constant.
1051 * This function is for use by GPIO controller drivers. The label can
1052 * help with diagnostics, and knowing that the signal is used as a GPIO
1053 * can help avoid accidentally multiplexing it to another controller.
1055 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1057 struct gpio_desc
*desc
;
1059 if (offset
>= chip
->ngpio
)
1062 desc
= &chip
->desc
[offset
];
1064 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
1068 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1071 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1072 * @desc: GPIO descriptor to request
1073 * @label: label for the GPIO
1075 * Function allows GPIO chip drivers to request and use their own GPIO
1076 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1077 * function will not increase reference count of the GPIO chip module. This
1078 * allows the GPIO chip module to be unloaded as needed (we assume that the
1079 * GPIO chip driver handles freeing the GPIOs it has requested).
1081 struct gpio_desc
*gpiochip_request_own_desc(struct gpio_chip
*chip
, u16 hwnum
,
1084 struct gpio_desc
*desc
= gpiochip_get_desc(chip
, hwnum
);
1088 chip_err(chip
, "failed to get GPIO descriptor\n");
1092 err
= __gpiod_request(desc
, label
);
1094 return ERR_PTR(err
);
1098 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc
);
1101 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1102 * @desc: GPIO descriptor to free
1104 * Function frees the given GPIO requested previously with
1105 * gpiochip_request_own_desc().
1107 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
1112 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc
);
1114 /* Drivers MUST set GPIO direction before making get/set calls. In
1115 * some cases this is done in early boot, before IRQs are enabled.
1117 * As a rule these aren't called more than once (except for drivers
1118 * using the open-drain emulation idiom) so these are natural places
1119 * to accumulate extra debugging checks. Note that we can't (yet)
1120 * rely on gpio_request() having been called beforehand.
1124 * gpiod_direction_input - set the GPIO direction to input
1125 * @desc: GPIO to set to input
1127 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1128 * be called safely on it.
1130 * Return 0 in case of success, else an error code.
1132 int gpiod_direction_input(struct gpio_desc
*desc
)
1134 struct gpio_chip
*chip
;
1135 int status
= -EINVAL
;
1137 if (!desc
|| !desc
->chip
) {
1138 pr_warn("%s: invalid GPIO\n", __func__
);
1143 if (!chip
->get
|| !chip
->direction_input
) {
1145 "%s: missing get() or direction_input() operations\n",
1150 status
= chip
->direction_input(chip
, gpio_chip_hwgpio(desc
));
1152 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1154 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1158 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
1160 static int _gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1162 struct gpio_chip
*chip
;
1163 int status
= -EINVAL
;
1165 /* GPIOs used for IRQs shall not be set as output */
1166 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
1168 "%s: tried to set a GPIO tied to an IRQ as output\n",
1173 /* Open drain pin should not be driven to 1 */
1174 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1175 return gpiod_direction_input(desc
);
1177 /* Open source pin should not be driven to 0 */
1178 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1179 return gpiod_direction_input(desc
);
1182 if (!chip
->set
|| !chip
->direction_output
) {
1184 "%s: missing set() or direction_output() operations\n",
1189 status
= chip
->direction_output(chip
, gpio_chip_hwgpio(desc
), value
);
1191 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1192 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1193 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1198 * gpiod_direction_output_raw - set the GPIO direction to output
1199 * @desc: GPIO to set to output
1200 * @value: initial output value of the GPIO
1202 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1203 * be called safely on it. The initial value of the output must be specified
1204 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1206 * Return 0 in case of success, else an error code.
1208 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1210 if (!desc
|| !desc
->chip
) {
1211 pr_warn("%s: invalid GPIO\n", __func__
);
1214 return _gpiod_direction_output_raw(desc
, value
);
1216 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
1219 * gpiod_direction_output - set the GPIO direction to output
1220 * @desc: GPIO to set to output
1221 * @value: initial output value of the GPIO
1223 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1224 * be called safely on it. The initial value of the output must be specified
1225 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1228 * Return 0 in case of success, else an error code.
1230 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1232 if (!desc
|| !desc
->chip
) {
1233 pr_warn("%s: invalid GPIO\n", __func__
);
1236 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1238 return _gpiod_direction_output_raw(desc
, value
);
1240 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1243 * gpiod_set_debounce - sets @debounce time for a @gpio
1244 * @gpio: the gpio to set debounce time
1245 * @debounce: debounce time is microseconds
1247 * returns -ENOTSUPP if the controller does not support setting
1250 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1252 struct gpio_chip
*chip
;
1254 if (!desc
|| !desc
->chip
) {
1255 pr_warn("%s: invalid GPIO\n", __func__
);
1260 if (!chip
->set
|| !chip
->set_debounce
) {
1262 "%s: missing set() or set_debounce() operations\n",
1267 return chip
->set_debounce(chip
, gpio_chip_hwgpio(desc
), debounce
);
1269 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1272 * gpiod_is_active_low - test whether a GPIO is active-low or not
1273 * @desc: the gpio descriptor to test
1275 * Returns 1 if the GPIO is active-low, 0 otherwise.
1277 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1279 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1281 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1283 /* I/O calls are only valid after configuration completed; the relevant
1284 * "is this a valid GPIO" error checks should already have been done.
1286 * "Get" operations are often inlinable as reading a pin value register,
1287 * and masking the relevant bit in that register.
1289 * When "set" operations are inlinable, they involve writing that mask to
1290 * one register to set a low value, or a different register to set it high.
1291 * Otherwise locking is needed, so there may be little value to inlining.
1293 *------------------------------------------------------------------------
1295 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1296 * have requested the GPIO. That can include implicit requesting by
1297 * a direction setting call. Marking a gpio as requested locks its chip
1298 * in memory, guaranteeing that these table lookups need no more locking
1299 * and that gpiochip_remove() will fail.
1301 * REVISIT when debugging, consider adding some instrumentation to ensure
1302 * that the GPIO was actually requested.
1305 static int _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1307 struct gpio_chip
*chip
;
1312 offset
= gpio_chip_hwgpio(desc
);
1313 value
= chip
->get
? chip
->get(chip
, offset
) : -EIO
;
1314 value
= value
< 0 ? value
: !!value
;
1315 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1320 * gpiod_get_raw_value() - return a gpio's raw value
1321 * @desc: gpio whose value will be returned
1323 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1324 * its ACTIVE_LOW status, or negative errno on failure.
1326 * This function should be called from contexts where we cannot sleep, and will
1327 * complain if the GPIO chip functions potentially sleep.
1329 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
1333 /* Should be using gpio_get_value_cansleep() */
1334 WARN_ON(desc
->chip
->can_sleep
);
1335 return _gpiod_get_raw_value(desc
);
1337 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1340 * gpiod_get_value() - return a gpio's value
1341 * @desc: gpio whose value will be returned
1343 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1344 * account, or negative errno on failure.
1346 * This function should be called from contexts where we cannot sleep, and will
1347 * complain if the GPIO chip functions potentially sleep.
1349 int gpiod_get_value(const struct gpio_desc
*desc
)
1354 /* Should be using gpio_get_value_cansleep() */
1355 WARN_ON(desc
->chip
->can_sleep
);
1357 value
= _gpiod_get_raw_value(desc
);
1361 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1366 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1369 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1370 * @desc: gpio descriptor whose state need to be set.
1371 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1373 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, bool value
)
1376 struct gpio_chip
*chip
= desc
->chip
;
1377 int offset
= gpio_chip_hwgpio(desc
);
1380 err
= chip
->direction_input(chip
, offset
);
1382 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1384 err
= chip
->direction_output(chip
, offset
, 0);
1386 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1388 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1391 "%s: Error in set_value for open drain err %d\n",
1396 * _gpio_set_open_source_value() - Set the open source gpio's value.
1397 * @desc: gpio descriptor whose state need to be set.
1398 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1400 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, bool value
)
1403 struct gpio_chip
*chip
= desc
->chip
;
1404 int offset
= gpio_chip_hwgpio(desc
);
1407 err
= chip
->direction_output(chip
, offset
, 1);
1409 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1411 err
= chip
->direction_input(chip
, offset
);
1413 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1415 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
1418 "%s: Error in set_value for open source err %d\n",
1422 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, bool value
)
1424 struct gpio_chip
*chip
;
1427 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1428 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1429 _gpio_set_open_drain_value(desc
, value
);
1430 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1431 _gpio_set_open_source_value(desc
, value
);
1433 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
1437 * set multiple outputs on the same chip;
1438 * use the chip's set_multiple function if available;
1439 * otherwise set the outputs sequentially;
1440 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1441 * defines which outputs are to be changed
1442 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1443 * defines the values the outputs specified by mask are to be set to
1445 static void gpio_chip_set_multiple(struct gpio_chip
*chip
,
1446 unsigned long *mask
, unsigned long *bits
)
1448 if (chip
->set_multiple
) {
1449 chip
->set_multiple(chip
, mask
, bits
);
1452 for (i
= 0; i
< chip
->ngpio
; i
++) {
1453 if (mask
[BIT_WORD(i
)] == 0) {
1454 /* no more set bits in this mask word;
1455 * skip ahead to the next word */
1456 i
= (BIT_WORD(i
) + 1) * BITS_PER_LONG
- 1;
1459 /* set outputs if the corresponding mask bit is set */
1460 if (__test_and_clear_bit(i
, mask
))
1461 chip
->set(chip
, i
, test_bit(i
, bits
));
1466 static void gpiod_set_array_value_priv(bool raw
, bool can_sleep
,
1467 unsigned int array_size
,
1468 struct gpio_desc
**desc_array
,
1473 while (i
< array_size
) {
1474 struct gpio_chip
*chip
= desc_array
[i
]->chip
;
1475 unsigned long mask
[BITS_TO_LONGS(chip
->ngpio
)];
1476 unsigned long bits
[BITS_TO_LONGS(chip
->ngpio
)];
1480 WARN_ON(chip
->can_sleep
);
1482 memset(mask
, 0, sizeof(mask
));
1484 struct gpio_desc
*desc
= desc_array
[i
];
1485 int hwgpio
= gpio_chip_hwgpio(desc
);
1486 int value
= value_array
[i
];
1488 if (!raw
&& test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1490 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1492 * collect all normal outputs belonging to the same chip
1493 * open drain and open source outputs are set individually
1495 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
)) {
1496 _gpio_set_open_drain_value(desc
, value
);
1497 } else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
)) {
1498 _gpio_set_open_source_value(desc
, value
);
1500 __set_bit(hwgpio
, mask
);
1502 __set_bit(hwgpio
, bits
);
1504 __clear_bit(hwgpio
, bits
);
1508 } while ((i
< array_size
) && (desc_array
[i
]->chip
== chip
));
1509 /* push collected bits to outputs */
1511 gpio_chip_set_multiple(chip
, mask
, bits
);
1516 * gpiod_set_raw_value() - assign a gpio's raw value
1517 * @desc: gpio whose value will be assigned
1518 * @value: value to assign
1520 * Set the raw value of the GPIO, i.e. the value of its physical line without
1521 * regard for its ACTIVE_LOW status.
1523 * This function should be called from contexts where we cannot sleep, and will
1524 * complain if the GPIO chip functions potentially sleep.
1526 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
1530 /* Should be using gpio_set_value_cansleep() */
1531 WARN_ON(desc
->chip
->can_sleep
);
1532 _gpiod_set_raw_value(desc
, value
);
1534 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
1537 * gpiod_set_value() - assign a gpio's value
1538 * @desc: gpio whose value will be assigned
1539 * @value: value to assign
1541 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1544 * This function should be called from contexts where we cannot sleep, and will
1545 * complain if the GPIO chip functions potentially sleep.
1547 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
1551 /* Should be using gpio_set_value_cansleep() */
1552 WARN_ON(desc
->chip
->can_sleep
);
1553 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1555 _gpiod_set_raw_value(desc
, value
);
1557 EXPORT_SYMBOL_GPL(gpiod_set_value
);
1560 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1561 * @array_size: number of elements in the descriptor / value arrays
1562 * @desc_array: array of GPIO descriptors whose values will be assigned
1563 * @value_array: array of values to assign
1565 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1566 * without regard for their ACTIVE_LOW status.
1568 * This function should be called from contexts where we cannot sleep, and will
1569 * complain if the GPIO chip functions potentially sleep.
1571 void gpiod_set_raw_array_value(unsigned int array_size
,
1572 struct gpio_desc
**desc_array
, int *value_array
)
1576 gpiod_set_array_value_priv(true, false, array_size
, desc_array
,
1579 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value
);
1582 * gpiod_set_array_value() - assign values to an array of GPIOs
1583 * @array_size: number of elements in the descriptor / value arrays
1584 * @desc_array: array of GPIO descriptors whose values will be assigned
1585 * @value_array: array of values to assign
1587 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1590 * This function should be called from contexts where we cannot sleep, and will
1591 * complain if the GPIO chip functions potentially sleep.
1593 void gpiod_set_array_value(unsigned int array_size
,
1594 struct gpio_desc
**desc_array
, int *value_array
)
1598 gpiod_set_array_value_priv(false, false, array_size
, desc_array
,
1601 EXPORT_SYMBOL_GPL(gpiod_set_array_value
);
1604 * gpiod_cansleep() - report whether gpio value access may sleep
1605 * @desc: gpio to check
1608 int gpiod_cansleep(const struct gpio_desc
*desc
)
1612 return desc
->chip
->can_sleep
;
1614 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
1617 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1618 * @desc: gpio whose IRQ will be returned (already requested)
1620 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1623 int gpiod_to_irq(const struct gpio_desc
*desc
)
1625 struct gpio_chip
*chip
;
1631 offset
= gpio_chip_hwgpio(desc
);
1632 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
1634 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
1637 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1638 * @chip: the chip the GPIO to lock belongs to
1639 * @offset: the offset of the GPIO to lock as IRQ
1641 * This is used directly by GPIO drivers that want to lock down
1642 * a certain GPIO line to be used for IRQs.
1644 int gpiochip_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
1646 if (offset
>= chip
->ngpio
)
1649 if (test_bit(FLAG_IS_OUT
, &chip
->desc
[offset
].flags
)) {
1651 "%s: tried to flag a GPIO set as output for IRQ\n",
1656 set_bit(FLAG_USED_AS_IRQ
, &chip
->desc
[offset
].flags
);
1659 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq
);
1662 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1663 * @chip: the chip the GPIO to lock belongs to
1664 * @offset: the offset of the GPIO to lock as IRQ
1666 * This is used directly by GPIO drivers that want to indicate
1667 * that a certain GPIO is no longer used exclusively for IRQ.
1669 void gpiochip_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
1671 if (offset
>= chip
->ngpio
)
1674 clear_bit(FLAG_USED_AS_IRQ
, &chip
->desc
[offset
].flags
);
1676 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq
);
1679 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1680 * @desc: gpio whose value will be returned
1682 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1683 * its ACTIVE_LOW status, or negative errno on failure.
1685 * This function is to be called from contexts that can sleep.
1687 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
1689 might_sleep_if(extra_checks
);
1692 return _gpiod_get_raw_value(desc
);
1694 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
1697 * gpiod_get_value_cansleep() - return a gpio's value
1698 * @desc: gpio whose value will be returned
1700 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1701 * account, or negative errno on failure.
1703 * This function is to be called from contexts that can sleep.
1705 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
1709 might_sleep_if(extra_checks
);
1713 value
= _gpiod_get_raw_value(desc
);
1717 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1722 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
1725 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1726 * @desc: gpio whose value will be assigned
1727 * @value: value to assign
1729 * Set the raw value of the GPIO, i.e. the value of its physical line without
1730 * regard for its ACTIVE_LOW status.
1732 * This function is to be called from contexts that can sleep.
1734 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
1736 might_sleep_if(extra_checks
);
1739 _gpiod_set_raw_value(desc
, value
);
1741 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
1744 * gpiod_set_value_cansleep() - assign a gpio's value
1745 * @desc: gpio whose value will be assigned
1746 * @value: value to assign
1748 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1751 * This function is to be called from contexts that can sleep.
1753 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
1755 might_sleep_if(extra_checks
);
1759 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1761 _gpiod_set_raw_value(desc
, value
);
1763 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
1766 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
1767 * @array_size: number of elements in the descriptor / value arrays
1768 * @desc_array: array of GPIO descriptors whose values will be assigned
1769 * @value_array: array of values to assign
1771 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1772 * without regard for their ACTIVE_LOW status.
1774 * This function is to be called from contexts that can sleep.
1776 void gpiod_set_raw_array_value_cansleep(unsigned int array_size
,
1777 struct gpio_desc
**desc_array
,
1780 might_sleep_if(extra_checks
);
1783 gpiod_set_array_value_priv(true, true, array_size
, desc_array
,
1786 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep
);
1789 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
1790 * @array_size: number of elements in the descriptor / value arrays
1791 * @desc_array: array of GPIO descriptors whose values will be assigned
1792 * @value_array: array of values to assign
1794 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1797 * This function is to be called from contexts that can sleep.
1799 void gpiod_set_array_value_cansleep(unsigned int array_size
,
1800 struct gpio_desc
**desc_array
,
1803 might_sleep_if(extra_checks
);
1806 gpiod_set_array_value_priv(false, true, array_size
, desc_array
,
1809 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep
);
1812 * gpiod_add_lookup_table() - register GPIO device consumers
1813 * @table: table of consumers to register
1815 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
1817 mutex_lock(&gpio_lookup_lock
);
1819 list_add_tail(&table
->list
, &gpio_lookup_list
);
1821 mutex_unlock(&gpio_lookup_lock
);
1825 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1826 * @table: table of consumers to unregister
1828 void gpiod_remove_lookup_table(struct gpiod_lookup_table
*table
)
1830 mutex_lock(&gpio_lookup_lock
);
1832 list_del(&table
->list
);
1834 mutex_unlock(&gpio_lookup_lock
);
1837 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
1839 enum gpio_lookup_flags
*flags
)
1841 char prop_name
[32]; /* 32 is max size of property name */
1842 enum of_gpio_flags of_flags
;
1843 struct gpio_desc
*desc
;
1846 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
1848 snprintf(prop_name
, sizeof(prop_name
), "%s-%s", con_id
,
1851 snprintf(prop_name
, sizeof(prop_name
), "%s",
1854 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
1856 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
1863 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
1864 *flags
|= GPIO_ACTIVE_LOW
;
1866 if (of_flags
& OF_GPIO_SINGLE_ENDED
) {
1867 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
1868 *flags
|= GPIO_OPEN_DRAIN
;
1870 *flags
|= GPIO_OPEN_SOURCE
;
1876 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
1878 enum gpio_lookup_flags
*flags
)
1880 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
1881 struct acpi_gpio_info info
;
1882 struct gpio_desc
*desc
;
1886 /* Try first from _DSD */
1887 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
1888 if (con_id
&& strcmp(con_id
, "gpios")) {
1889 snprintf(propname
, sizeof(propname
), "%s-%s",
1890 con_id
, gpio_suffixes
[i
]);
1892 snprintf(propname
, sizeof(propname
), "%s",
1896 desc
= acpi_get_gpiod_by_index(adev
, propname
, idx
, &info
);
1897 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
1901 /* Then from plain _CRS GPIOs */
1903 if (!acpi_can_fallback_to_crs(adev
, con_id
))
1904 return ERR_PTR(-ENOENT
);
1906 desc
= acpi_get_gpiod_by_index(adev
, NULL
, idx
, &info
);
1911 if (info
.polarity
== GPIO_ACTIVE_LOW
)
1912 *flags
|= GPIO_ACTIVE_LOW
;
1917 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
1919 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
1920 struct gpiod_lookup_table
*table
;
1922 mutex_lock(&gpio_lookup_lock
);
1924 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
1925 if (table
->dev_id
&& dev_id
) {
1927 * Valid strings on both ends, must be identical to have
1930 if (!strcmp(table
->dev_id
, dev_id
))
1934 * One of the pointers is NULL, so both must be to have
1937 if (dev_id
== table
->dev_id
)
1944 mutex_unlock(&gpio_lookup_lock
);
1948 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
1950 enum gpio_lookup_flags
*flags
)
1952 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
1953 struct gpiod_lookup_table
*table
;
1954 struct gpiod_lookup
*p
;
1956 table
= gpiod_find_lookup_table(dev
);
1960 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
1961 struct gpio_chip
*chip
;
1963 /* idx must always match exactly */
1967 /* If the lookup entry has a con_id, require exact match */
1968 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
1971 chip
= find_chip_by_name(p
->chip_label
);
1974 dev_err(dev
, "cannot find GPIO chip %s\n",
1976 return ERR_PTR(-ENODEV
);
1979 if (chip
->ngpio
<= p
->chip_hwnum
) {
1981 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1982 idx
, chip
->ngpio
, chip
->label
);
1983 return ERR_PTR(-EINVAL
);
1986 desc
= gpiochip_get_desc(chip
, p
->chip_hwnum
);
1995 static int dt_gpio_count(struct device
*dev
, const char *con_id
)
2001 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
2003 snprintf(propname
, sizeof(propname
), "%s-%s",
2004 con_id
, gpio_suffixes
[i
]);
2006 snprintf(propname
, sizeof(propname
), "%s",
2009 ret
= of_gpio_named_count(dev
->of_node
, propname
);
2016 static int platform_gpio_count(struct device
*dev
, const char *con_id
)
2018 struct gpiod_lookup_table
*table
;
2019 struct gpiod_lookup
*p
;
2020 unsigned int count
= 0;
2022 table
= gpiod_find_lookup_table(dev
);
2026 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
2027 if ((con_id
&& p
->con_id
&& !strcmp(con_id
, p
->con_id
)) ||
2028 (!con_id
&& !p
->con_id
))
2038 * gpiod_count - return the number of GPIOs associated with a device / function
2039 * or -ENOENT if no GPIO has been assigned to the requested function
2040 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2041 * @con_id: function within the GPIO consumer
2043 int gpiod_count(struct device
*dev
, const char *con_id
)
2045 int count
= -ENOENT
;
2047 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
)
2048 count
= dt_gpio_count(dev
, con_id
);
2049 else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
))
2050 count
= acpi_gpio_count(dev
, con_id
);
2053 count
= platform_gpio_count(dev
, con_id
);
2057 EXPORT_SYMBOL_GPL(gpiod_count
);
2060 * gpiod_get - obtain a GPIO for a given GPIO function
2061 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2062 * @con_id: function within the GPIO consumer
2063 * @flags: optional GPIO initialization flags
2065 * Return the GPIO descriptor corresponding to the function con_id of device
2066 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2067 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
2069 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
,
2070 enum gpiod_flags flags
)
2072 return gpiod_get_index(dev
, con_id
, 0, flags
);
2074 EXPORT_SYMBOL_GPL(gpiod_get
);
2077 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2078 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2079 * @con_id: function within the GPIO consumer
2080 * @flags: optional GPIO initialization flags
2082 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2083 * the requested function it will return NULL. This is convenient for drivers
2084 * that need to handle optional GPIOs.
2086 struct gpio_desc
*__must_check
gpiod_get_optional(struct device
*dev
,
2088 enum gpiod_flags flags
)
2090 return gpiod_get_index_optional(dev
, con_id
, 0, flags
);
2092 EXPORT_SYMBOL_GPL(gpiod_get_optional
);
2095 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2096 * @desc: gpio to be setup
2097 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2100 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2102 static void gpiod_parse_flags(struct gpio_desc
*desc
, unsigned long lflags
)
2104 if (lflags
& GPIO_ACTIVE_LOW
)
2105 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2106 if (lflags
& GPIO_OPEN_DRAIN
)
2107 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
2108 if (lflags
& GPIO_OPEN_SOURCE
)
2109 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
2113 * gpiod_configure_flags - helper function to configure a given GPIO
2114 * @desc: gpio whose value will be assigned
2115 * @con_id: function within the GPIO consumer
2116 * @dflags: gpiod_flags - optional GPIO initialization flags
2118 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2119 * requested function and/or index, or another IS_ERR() code if an error
2120 * occurred while trying to acquire the GPIO.
2122 static int gpiod_configure_flags(struct gpio_desc
*desc
, const char *con_id
,
2123 enum gpiod_flags dflags
)
2127 /* No particular flag request, return here... */
2128 if (!(dflags
& GPIOD_FLAGS_BIT_DIR_SET
)) {
2129 pr_debug("no flags found for %s\n", con_id
);
2134 if (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
)
2135 status
= gpiod_direction_output(desc
,
2136 dflags
& GPIOD_FLAGS_BIT_DIR_VAL
);
2138 status
= gpiod_direction_input(desc
);
2144 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2145 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2146 * @con_id: function within the GPIO consumer
2147 * @idx: index of the GPIO to obtain in the consumer
2148 * @flags: optional GPIO initialization flags
2150 * This variant of gpiod_get() allows to access GPIOs other than the first
2151 * defined one for functions that define several GPIOs.
2153 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2154 * requested function and/or index, or another IS_ERR() code if an error
2155 * occurred while trying to acquire the GPIO.
2157 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
2160 enum gpiod_flags flags
)
2162 struct gpio_desc
*desc
= NULL
;
2164 enum gpio_lookup_flags lookupflags
= 0;
2166 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
2169 /* Using device tree? */
2170 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
2171 dev_dbg(dev
, "using device tree for GPIO lookup\n");
2172 desc
= of_find_gpio(dev
, con_id
, idx
, &lookupflags
);
2173 } else if (ACPI_COMPANION(dev
)) {
2174 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
2175 desc
= acpi_find_gpio(dev
, con_id
, idx
, &lookupflags
);
2180 * Either we are not using DT or ACPI, or their lookup did not return
2181 * a result. In that case, use platform lookup as a fallback.
2183 if (!desc
|| desc
== ERR_PTR(-ENOENT
)) {
2184 dev_dbg(dev
, "using lookup tables for GPIO lookup\n");
2185 desc
= gpiod_find(dev
, con_id
, idx
, &lookupflags
);
2189 dev_dbg(dev
, "lookup for GPIO %s failed\n", con_id
);
2193 gpiod_parse_flags(desc
, lookupflags
);
2195 status
= gpiod_request(desc
, con_id
);
2197 return ERR_PTR(status
);
2199 status
= gpiod_configure_flags(desc
, con_id
, flags
);
2201 dev_dbg(dev
, "setup of GPIO %s failed\n", con_id
);
2203 return ERR_PTR(status
);
2208 EXPORT_SYMBOL_GPL(gpiod_get_index
);
2211 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2212 * @fwnode: handle of the firmware node
2213 * @propname: name of the firmware property representing the GPIO
2215 * This function can be used for drivers that get their configuration
2218 * Function properly finds the corresponding GPIO using whatever is the
2219 * underlying firmware interface and then makes sure that the GPIO
2220 * descriptor is requested before it is returned to the caller.
2222 * In case of error an ERR_PTR() is returned.
2224 struct gpio_desc
*fwnode_get_named_gpiod(struct fwnode_handle
*fwnode
,
2225 const char *propname
)
2227 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
2228 bool active_low
= false;
2229 bool single_ended
= false;
2233 return ERR_PTR(-EINVAL
);
2235 if (is_of_node(fwnode
)) {
2236 enum of_gpio_flags flags
;
2238 desc
= of_get_named_gpiod_flags(to_of_node(fwnode
), propname
, 0,
2240 if (!IS_ERR(desc
)) {
2241 active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
2242 single_ended
= flags
& OF_GPIO_SINGLE_ENDED
;
2244 } else if (is_acpi_node(fwnode
)) {
2245 struct acpi_gpio_info info
;
2247 desc
= acpi_node_get_gpiod(fwnode
, propname
, 0, &info
);
2249 active_low
= info
.polarity
== GPIO_ACTIVE_LOW
;
2256 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2260 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
2262 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
2265 ret
= gpiod_request(desc
, NULL
);
2267 return ERR_PTR(ret
);
2271 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod
);
2274 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2276 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2277 * @con_id: function within the GPIO consumer
2278 * @index: index of the GPIO to obtain in the consumer
2279 * @flags: optional GPIO initialization flags
2281 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2282 * specified index was assigned to the requested function it will return NULL.
2283 * This is convenient for drivers that need to handle optional GPIOs.
2285 struct gpio_desc
*__must_check
gpiod_get_index_optional(struct device
*dev
,
2288 enum gpiod_flags flags
)
2290 struct gpio_desc
*desc
;
2292 desc
= gpiod_get_index(dev
, con_id
, index
, flags
);
2294 if (PTR_ERR(desc
) == -ENOENT
)
2300 EXPORT_SYMBOL_GPL(gpiod_get_index_optional
);
2303 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2304 * @desc: gpio whose value will be assigned
2305 * @name: gpio line name
2306 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2308 * @dflags: gpiod_flags - optional GPIO initialization flags
2310 int gpiod_hog(struct gpio_desc
*desc
, const char *name
,
2311 unsigned long lflags
, enum gpiod_flags dflags
)
2313 struct gpio_chip
*chip
;
2314 struct gpio_desc
*local_desc
;
2318 chip
= gpiod_to_chip(desc
);
2319 hwnum
= gpio_chip_hwgpio(desc
);
2321 gpiod_parse_flags(desc
, lflags
);
2323 local_desc
= gpiochip_request_own_desc(chip
, hwnum
, name
);
2324 if (IS_ERR(local_desc
)) {
2325 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2326 name
, chip
->label
, hwnum
);
2327 return PTR_ERR(local_desc
);
2330 status
= gpiod_configure_flags(desc
, name
, dflags
);
2332 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2333 name
, chip
->label
, hwnum
);
2334 gpiochip_free_own_desc(desc
);
2338 /* Mark GPIO as hogged so it can be identified and removed later */
2339 set_bit(FLAG_IS_HOGGED
, &desc
->flags
);
2341 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2342 desc_to_gpio(desc
), name
,
2343 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ? "output" : "input",
2344 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ?
2345 (dflags
&GPIOD_FLAGS_BIT_DIR_VAL
) ? "/high" : "/low":"");
2351 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2352 * @chip: gpio chip to act on
2354 * This is only used by of_gpiochip_remove to free hogged gpios
2356 static void gpiochip_free_hogs(struct gpio_chip
*chip
)
2360 for (id
= 0; id
< chip
->ngpio
; id
++) {
2361 if (test_bit(FLAG_IS_HOGGED
, &chip
->desc
[id
].flags
))
2362 gpiochip_free_own_desc(&chip
->desc
[id
]);
2367 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2368 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2369 * @con_id: function within the GPIO consumer
2370 * @flags: optional GPIO initialization flags
2372 * This function acquires all the GPIOs defined under a given function.
2374 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2375 * no GPIO has been assigned to the requested function, or another IS_ERR()
2376 * code if an error occurred while trying to acquire the GPIOs.
2378 struct gpio_descs
*__must_check
gpiod_get_array(struct device
*dev
,
2380 enum gpiod_flags flags
)
2382 struct gpio_desc
*desc
;
2383 struct gpio_descs
*descs
;
2386 count
= gpiod_count(dev
, con_id
);
2388 return ERR_PTR(count
);
2390 descs
= kzalloc(sizeof(*descs
) + sizeof(descs
->desc
[0]) * count
,
2393 return ERR_PTR(-ENOMEM
);
2395 for (descs
->ndescs
= 0; descs
->ndescs
< count
; ) {
2396 desc
= gpiod_get_index(dev
, con_id
, descs
->ndescs
, flags
);
2398 gpiod_put_array(descs
);
2399 return ERR_CAST(desc
);
2401 descs
->desc
[descs
->ndescs
] = desc
;
2406 EXPORT_SYMBOL_GPL(gpiod_get_array
);
2409 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2411 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2412 * @con_id: function within the GPIO consumer
2413 * @flags: optional GPIO initialization flags
2415 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2416 * assigned to the requested function it will return NULL.
2418 struct gpio_descs
*__must_check
gpiod_get_array_optional(struct device
*dev
,
2420 enum gpiod_flags flags
)
2422 struct gpio_descs
*descs
;
2424 descs
= gpiod_get_array(dev
, con_id
, flags
);
2425 if (IS_ERR(descs
) && (PTR_ERR(descs
) == -ENOENT
))
2430 EXPORT_SYMBOL_GPL(gpiod_get_array_optional
);
2433 * gpiod_put - dispose of a GPIO descriptor
2434 * @desc: GPIO descriptor to dispose of
2436 * No descriptor can be used after gpiod_put() has been called on it.
2438 void gpiod_put(struct gpio_desc
*desc
)
2442 EXPORT_SYMBOL_GPL(gpiod_put
);
2445 * gpiod_put_array - dispose of multiple GPIO descriptors
2446 * @descs: struct gpio_descs containing an array of descriptors
2448 void gpiod_put_array(struct gpio_descs
*descs
)
2452 for (i
= 0; i
< descs
->ndescs
; i
++)
2453 gpiod_put(descs
->desc
[i
]);
2457 EXPORT_SYMBOL_GPL(gpiod_put_array
);
2459 #ifdef CONFIG_DEBUG_FS
2461 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
2464 unsigned gpio
= chip
->base
;
2465 struct gpio_desc
*gdesc
= &chip
->desc
[0];
2469 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
2470 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
)) {
2472 seq_printf(s
, " gpio-%-3d (%-20.20s)\n",
2478 gpiod_get_direction(gdesc
);
2479 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
2480 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
2481 seq_printf(s
, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2482 gpio
, gdesc
->name
? gdesc
->name
: "", gdesc
->label
,
2483 is_out
? "out" : "in ",
2485 ? (chip
->get(chip
, i
) ? "hi" : "lo")
2487 is_irq
? "IRQ" : " ");
2488 seq_printf(s
, "\n");
2492 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
2494 unsigned long flags
;
2495 struct gpio_chip
*chip
= NULL
;
2496 loff_t index
= *pos
;
2500 spin_lock_irqsave(&gpio_lock
, flags
);
2501 list_for_each_entry(chip
, &gpio_chips
, list
)
2503 spin_unlock_irqrestore(&gpio_lock
, flags
);
2506 spin_unlock_irqrestore(&gpio_lock
, flags
);
2511 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2513 unsigned long flags
;
2514 struct gpio_chip
*chip
= v
;
2517 spin_lock_irqsave(&gpio_lock
, flags
);
2518 if (list_is_last(&chip
->list
, &gpio_chips
))
2521 ret
= list_entry(chip
->list
.next
, struct gpio_chip
, list
);
2522 spin_unlock_irqrestore(&gpio_lock
, flags
);
2530 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
2534 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
2536 struct gpio_chip
*chip
= v
;
2539 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
2540 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
2543 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
2546 seq_printf(s
, ", %s", chip
->label
);
2547 if (chip
->can_sleep
)
2548 seq_printf(s
, ", can sleep");
2549 seq_printf(s
, ":\n");
2552 chip
->dbg_show(s
, chip
);
2554 gpiolib_dbg_show(s
, chip
);
2559 static const struct seq_operations gpiolib_seq_ops
= {
2560 .start
= gpiolib_seq_start
,
2561 .next
= gpiolib_seq_next
,
2562 .stop
= gpiolib_seq_stop
,
2563 .show
= gpiolib_seq_show
,
2566 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
2568 return seq_open(file
, &gpiolib_seq_ops
);
2571 static const struct file_operations gpiolib_operations
= {
2572 .owner
= THIS_MODULE
,
2573 .open
= gpiolib_open
,
2575 .llseek
= seq_lseek
,
2576 .release
= seq_release
,
2579 static int __init
gpiolib_debugfs_init(void)
2581 /* /sys/kernel/debug/gpio */
2582 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
2583 NULL
, NULL
, &gpiolib_operations
);
2586 subsys_initcall(gpiolib_debugfs_init
);
2588 #endif /* DEBUG_FS */