1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/gpio.h>
24 /* Implementation infrastructure for GPIO interfaces.
26 * The GPIO programming interface allows for inlining speed-critical
27 * get/set operations for common cases, so that access to SOC-integrated
28 * GPIOs can sometimes cost only an instruction or two per bit.
32 /* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
39 #define extra_checks 1
41 #define extra_checks 0
44 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
48 DEFINE_SPINLOCK(gpio_lock
);
50 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
52 static DEFINE_MUTEX(gpio_lookup_lock
);
53 static LIST_HEAD(gpio_lookup_list
);
54 LIST_HEAD(gpio_chips
);
56 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
62 * Convert a GPIO number to its descriptor
64 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
66 struct gpio_chip
*chip
;
69 spin_lock_irqsave(&gpio_lock
, flags
);
71 list_for_each_entry(chip
, &gpio_chips
, list
) {
72 if (chip
->base
<= gpio
&& chip
->base
+ chip
->ngpio
> gpio
) {
73 spin_unlock_irqrestore(&gpio_lock
, flags
);
74 return &chip
->desc
[gpio
- chip
->base
];
78 spin_unlock_irqrestore(&gpio_lock
, flags
);
80 if (!gpio_is_valid(gpio
))
81 WARN(1, "invalid GPIO %d\n", gpio
);
85 EXPORT_SYMBOL_GPL(gpio_to_desc
);
88 * Get the GPIO descriptor corresponding to the given hw number for this chip.
90 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*chip
,
93 if (hwnum
>= chip
->ngpio
)
94 return ERR_PTR(-EINVAL
);
96 return &chip
->desc
[hwnum
];
100 * Convert a GPIO descriptor to the integer namespace.
101 * This should disappear in the future but is needed since we still
102 * use GPIO numbers for error messages and sysfs nodes
104 int desc_to_gpio(const struct gpio_desc
*desc
)
106 return desc
->chip
->base
+ (desc
- &desc
->chip
->desc
[0]);
108 EXPORT_SYMBOL_GPL(desc_to_gpio
);
112 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
113 * @desc: descriptor to return the chip of
115 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
117 return desc
? desc
->chip
: NULL
;
119 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
121 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
122 static int gpiochip_find_base(int ngpio
)
124 struct gpio_chip
*chip
;
125 int base
= ARCH_NR_GPIOS
- ngpio
;
127 list_for_each_entry_reverse(chip
, &gpio_chips
, list
) {
128 /* found a free space? */
129 if (chip
->base
+ chip
->ngpio
<= base
)
132 /* nope, check the space right before the chip */
133 base
= chip
->base
- ngpio
;
136 if (gpio_is_valid(base
)) {
137 pr_debug("%s: found new base at %d\n", __func__
, base
);
140 pr_err("%s: cannot find free range\n", __func__
);
146 * gpiod_get_direction - return the current direction of a GPIO
147 * @desc: GPIO to get the direction of
149 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
151 * This function may sleep if gpiod_cansleep() is true.
153 int gpiod_get_direction(struct gpio_desc
*desc
)
155 struct gpio_chip
*chip
;
157 int status
= -EINVAL
;
159 chip
= gpiod_to_chip(desc
);
160 offset
= gpio_chip_hwgpio(desc
);
162 if (!chip
->get_direction
)
165 status
= chip
->get_direction(chip
, offset
);
167 /* GPIOF_DIR_IN, or other positive */
169 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
173 set_bit(FLAG_IS_OUT
, &desc
->flags
);
177 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
180 * Add a new chip to the global chips list, keeping the list of chips sorted
183 * Return -EBUSY if the new chip overlaps with some other chip's integer
186 static int gpiochip_add_to_list(struct gpio_chip
*chip
)
188 struct list_head
*pos
= &gpio_chips
;
189 struct gpio_chip
*_chip
;
192 /* find where to insert our chip */
193 list_for_each(pos
, &gpio_chips
) {
194 _chip
= list_entry(pos
, struct gpio_chip
, list
);
195 /* shall we insert before _chip? */
196 if (_chip
->base
>= chip
->base
+ chip
->ngpio
)
200 /* are we stepping on the chip right before? */
201 if (pos
!= &gpio_chips
&& pos
->prev
!= &gpio_chips
) {
202 _chip
= list_entry(pos
->prev
, struct gpio_chip
, list
);
203 if (_chip
->base
+ _chip
->ngpio
> chip
->base
) {
205 "GPIO integer space overlap, cannot add chip\n");
211 list_add_tail(&chip
->list
, pos
);
217 * gpiochip_add() - register a gpio_chip
218 * @chip: the chip to register, with chip->base initialized
219 * Context: potentially before irqs will work
221 * Returns a negative errno if the chip can't be registered, such as
222 * because the chip->base is invalid or already associated with a
223 * different chip. Otherwise it returns zero as a success code.
225 * When gpiochip_add() is called very early during boot, so that GPIOs
226 * can be freely used, the chip->dev device must be registered before
227 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
228 * for GPIOs will fail rudely.
230 * If chip->base is negative, this requests dynamic assignment of
231 * a range of valid GPIOs.
233 int gpiochip_add(struct gpio_chip
*chip
)
238 int base
= chip
->base
;
239 struct gpio_desc
*descs
;
241 descs
= kcalloc(chip
->ngpio
, sizeof(descs
[0]), GFP_KERNEL
);
245 spin_lock_irqsave(&gpio_lock
, flags
);
248 base
= gpiochip_find_base(chip
->ngpio
);
251 spin_unlock_irqrestore(&gpio_lock
, flags
);
257 status
= gpiochip_add_to_list(chip
);
259 spin_unlock_irqrestore(&gpio_lock
, flags
);
263 for (id
= 0; id
< chip
->ngpio
; id
++) {
264 struct gpio_desc
*desc
= &descs
[id
];
268 /* REVISIT: most hardware initializes GPIOs as inputs (often
269 * with pullups enabled) so power usage is minimized. Linux
270 * code should set the gpio direction first thing; but until
271 * it does, and in case chip->get_direction is not set, we may
272 * expose the wrong direction in sysfs.
274 desc
->flags
= !chip
->direction_input
? (1 << FLAG_IS_OUT
) : 0;
279 spin_unlock_irqrestore(&gpio_lock
, flags
);
281 #ifdef CONFIG_PINCTRL
282 INIT_LIST_HEAD(&chip
->pin_ranges
);
285 of_gpiochip_add(chip
);
286 acpi_gpiochip_add(chip
);
288 status
= gpiochip_export(chip
);
290 goto err_remove_chip
;
292 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__
,
293 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
294 chip
->label
? : "generic");
299 acpi_gpiochip_remove(chip
);
300 of_gpiochip_remove(chip
);
301 spin_lock_irqsave(&gpio_lock
, flags
);
302 list_del(&chip
->list
);
303 spin_unlock_irqrestore(&gpio_lock
, flags
);
308 /* failures here can mean systems won't boot... */
309 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__
,
310 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
311 chip
->label
? : "generic");
314 EXPORT_SYMBOL_GPL(gpiochip_add
);
316 /* Forward-declaration */
317 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
);
318 static void gpiochip_free_hogs(struct gpio_chip
*chip
);
321 * gpiochip_remove() - unregister a gpio_chip
322 * @chip: the chip to unregister
324 * A gpio_chip with any GPIOs still requested may not be removed.
326 void gpiochip_remove(struct gpio_chip
*chip
)
331 gpiochip_unexport(chip
);
333 gpiochip_irqchip_remove(chip
);
335 acpi_gpiochip_remove(chip
);
336 gpiochip_remove_pin_ranges(chip
);
337 gpiochip_free_hogs(chip
);
338 of_gpiochip_remove(chip
);
340 spin_lock_irqsave(&gpio_lock
, flags
);
341 for (id
= 0; id
< chip
->ngpio
; id
++) {
342 if (test_bit(FLAG_REQUESTED
, &chip
->desc
[id
].flags
))
343 dev_crit(chip
->dev
, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
345 for (id
= 0; id
< chip
->ngpio
; id
++)
346 chip
->desc
[id
].chip
= NULL
;
348 list_del(&chip
->list
);
349 spin_unlock_irqrestore(&gpio_lock
, flags
);
354 EXPORT_SYMBOL_GPL(gpiochip_remove
);
357 * gpiochip_find() - iterator for locating a specific gpio_chip
358 * @data: data to pass to match function
359 * @callback: Callback function to check gpio_chip
361 * Similar to bus_find_device. It returns a reference to a gpio_chip as
362 * determined by a user supplied @match callback. The callback should return
363 * 0 if the device doesn't match and non-zero if it does. If the callback is
364 * non-zero, this function will return to the caller and not iterate over any
367 struct gpio_chip
*gpiochip_find(void *data
,
368 int (*match
)(struct gpio_chip
*chip
,
371 struct gpio_chip
*chip
;
374 spin_lock_irqsave(&gpio_lock
, flags
);
375 list_for_each_entry(chip
, &gpio_chips
, list
)
376 if (match(chip
, data
))
380 if (&chip
->list
== &gpio_chips
)
382 spin_unlock_irqrestore(&gpio_lock
, flags
);
386 EXPORT_SYMBOL_GPL(gpiochip_find
);
388 static int gpiochip_match_name(struct gpio_chip
*chip
, void *data
)
390 const char *name
= data
;
392 return !strcmp(chip
->label
, name
);
395 static struct gpio_chip
*find_chip_by_name(const char *name
)
397 return gpiochip_find((void *)name
, gpiochip_match_name
);
400 #ifdef CONFIG_GPIOLIB_IRQCHIP
403 * The following is irqchip helper code for gpiochips.
407 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
408 * @gpiochip: the gpiochip to set the irqchip chain to
409 * @irqchip: the irqchip to chain to the gpiochip
410 * @parent_irq: the irq number corresponding to the parent IRQ for this
412 * @parent_handler: the parent interrupt handler for the accumulated IRQ
413 * coming out of the gpiochip. If the interrupt is nested rather than
414 * cascaded, pass NULL in this handler argument
416 void gpiochip_set_chained_irqchip(struct gpio_chip
*gpiochip
,
417 struct irq_chip
*irqchip
,
419 irq_flow_handler_t parent_handler
)
423 if (!gpiochip
->irqdomain
) {
424 chip_err(gpiochip
, "called %s before setting up irqchip\n",
429 if (parent_handler
) {
430 if (gpiochip
->can_sleep
) {
432 "you cannot have chained interrupts on a "
433 "chip that may sleep\n");
437 * The parent irqchip is already using the chip_data for this
438 * irqchip, so our callbacks simply use the handler_data.
440 irq_set_handler_data(parent_irq
, gpiochip
);
441 irq_set_chained_handler(parent_irq
, parent_handler
);
444 /* Set the parent IRQ for all affected IRQs */
445 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
446 irq_set_parent(irq_find_mapping(gpiochip
->irqdomain
, offset
),
449 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip
);
452 * This lock class tells lockdep that GPIO irqs are in a different
453 * category than their parents, so it won't report false recursion.
455 static struct lock_class_key gpiochip_irq_lock_class
;
458 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
459 * @d: the irqdomain used by this irqchip
460 * @irq: the global irq number used by this GPIO irqchip irq
461 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
463 * This function will set up the mapping for a certain IRQ line on a
464 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
465 * stored inside the gpiochip.
467 static int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
468 irq_hw_number_t hwirq
)
470 struct gpio_chip
*chip
= d
->host_data
;
472 irq_set_chip_data(irq
, chip
);
473 irq_set_lockdep_class(irq
, &gpiochip_irq_lock_class
);
474 irq_set_chip_and_handler(irq
, chip
->irqchip
, chip
->irq_handler
);
475 /* Chips that can sleep need nested thread handlers */
476 if (chip
->can_sleep
&& !chip
->irq_not_threaded
)
477 irq_set_nested_thread(irq
, 1);
479 set_irq_flags(irq
, IRQF_VALID
);
481 irq_set_noprobe(irq
);
484 * No set-up of the hardware will happen if IRQ_TYPE_NONE
485 * is passed as default type.
487 if (chip
->irq_default_type
!= IRQ_TYPE_NONE
)
488 irq_set_irq_type(irq
, chip
->irq_default_type
);
493 static void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
495 struct gpio_chip
*chip
= d
->host_data
;
498 set_irq_flags(irq
, 0);
501 irq_set_nested_thread(irq
, 0);
502 irq_set_chip_and_handler(irq
, NULL
, NULL
);
503 irq_set_chip_data(irq
, NULL
);
506 static const struct irq_domain_ops gpiochip_domain_ops
= {
507 .map
= gpiochip_irq_map
,
508 .unmap
= gpiochip_irq_unmap
,
509 /* Virtually all GPIO irqchips are twocell:ed */
510 .xlate
= irq_domain_xlate_twocell
,
513 static int gpiochip_irq_reqres(struct irq_data
*d
)
515 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
517 if (gpiochip_lock_as_irq(chip
, d
->hwirq
)) {
519 "unable to lock HW IRQ %lu for IRQ\n",
526 static void gpiochip_irq_relres(struct irq_data
*d
)
528 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
530 gpiochip_unlock_as_irq(chip
, d
->hwirq
);
533 static int gpiochip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
535 return irq_find_mapping(chip
->irqdomain
, offset
);
539 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
540 * @gpiochip: the gpiochip to remove the irqchip from
542 * This is called only from gpiochip_remove()
544 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
)
548 acpi_gpiochip_free_interrupts(gpiochip
);
550 /* Remove all IRQ mappings and delete the domain */
551 if (gpiochip
->irqdomain
) {
552 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
554 irq_find_mapping(gpiochip
->irqdomain
, offset
));
555 irq_domain_remove(gpiochip
->irqdomain
);
558 if (gpiochip
->irqchip
) {
559 gpiochip
->irqchip
->irq_request_resources
= NULL
;
560 gpiochip
->irqchip
->irq_release_resources
= NULL
;
561 gpiochip
->irqchip
= NULL
;
566 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
567 * @gpiochip: the gpiochip to add the irqchip to
568 * @irqchip: the irqchip to add to the gpiochip
569 * @first_irq: if not dynamically assigned, the base (first) IRQ to
570 * allocate gpiochip irqs from
571 * @handler: the irq handler to use (often a predefined irq core function)
572 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
573 * to have the core avoid setting up any default type in the hardware.
575 * This function closely associates a certain irqchip with a certain
576 * gpiochip, providing an irq domain to translate the local IRQs to
577 * global irqs in the gpiolib core, and making sure that the gpiochip
578 * is passed as chip data to all related functions. Driver callbacks
579 * need to use container_of() to get their local state containers back
580 * from the gpiochip passed as chip data. An irqdomain will be stored
581 * in the gpiochip that shall be used by the driver to handle IRQ number
582 * translation. The gpiochip will need to be initialized and registered
583 * before calling this function.
585 * This function will handle two cell:ed simple IRQs and assumes all
586 * the pins on the gpiochip can generate a unique IRQ. Everything else
587 * need to be open coded.
589 int gpiochip_irqchip_add(struct gpio_chip
*gpiochip
,
590 struct irq_chip
*irqchip
,
591 unsigned int first_irq
,
592 irq_flow_handler_t handler
,
595 struct device_node
*of_node
;
597 unsigned irq_base
= 0;
599 if (!gpiochip
|| !irqchip
)
602 if (!gpiochip
->dev
) {
603 pr_err("missing gpiochip .dev parent pointer\n");
606 of_node
= gpiochip
->dev
->of_node
;
607 #ifdef CONFIG_OF_GPIO
609 * If the gpiochip has an assigned OF node this takes precendence
610 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
612 if (gpiochip
->of_node
)
613 of_node
= gpiochip
->of_node
;
615 gpiochip
->irqchip
= irqchip
;
616 gpiochip
->irq_handler
= handler
;
617 gpiochip
->irq_default_type
= type
;
618 gpiochip
->to_irq
= gpiochip_to_irq
;
619 gpiochip
->irqdomain
= irq_domain_add_simple(of_node
,
620 gpiochip
->ngpio
, first_irq
,
621 &gpiochip_domain_ops
, gpiochip
);
622 if (!gpiochip
->irqdomain
) {
623 gpiochip
->irqchip
= NULL
;
626 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
627 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
630 * Prepare the mapping since the irqchip shall be orthogonal to
631 * any gpiochip calls. If the first_irq was zero, this is
632 * necessary to allocate descriptors for all IRQs.
634 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++) {
635 irq_base
= irq_create_mapping(gpiochip
->irqdomain
, offset
);
638 * Store the base into the gpiochip to be used when
639 * unmapping the irqs.
641 gpiochip
->irq_base
= irq_base
;
644 acpi_gpiochip_request_interrupts(gpiochip
);
648 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add
);
650 #else /* CONFIG_GPIOLIB_IRQCHIP */
652 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
) {}
654 #endif /* CONFIG_GPIOLIB_IRQCHIP */
656 #ifdef CONFIG_PINCTRL
659 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
660 * @chip: the gpiochip to add the range for
661 * @pinctrl: the dev_name() of the pin controller to map to
662 * @gpio_offset: the start offset in the current gpio_chip number space
663 * @pin_group: name of the pin group inside the pin controller
665 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
666 struct pinctrl_dev
*pctldev
,
667 unsigned int gpio_offset
, const char *pin_group
)
669 struct gpio_pin_range
*pin_range
;
672 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
674 chip_err(chip
, "failed to allocate pin ranges\n");
678 /* Use local offset as range ID */
679 pin_range
->range
.id
= gpio_offset
;
680 pin_range
->range
.gc
= chip
;
681 pin_range
->range
.name
= chip
->label
;
682 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
683 pin_range
->pctldev
= pctldev
;
685 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
686 &pin_range
->range
.pins
,
687 &pin_range
->range
.npins
);
693 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
695 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
696 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
697 pinctrl_dev_get_devname(pctldev
), pin_group
);
699 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
703 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
706 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
707 * @chip: the gpiochip to add the range for
708 * @pinctrl_name: the dev_name() of the pin controller to map to
709 * @gpio_offset: the start offset in the current gpio_chip number space
710 * @pin_offset: the start offset in the pin controller number space
711 * @npins: the number of pins from the offset of each pin space (GPIO and
712 * pin controller) to accumulate in this range
714 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
715 unsigned int gpio_offset
, unsigned int pin_offset
,
718 struct gpio_pin_range
*pin_range
;
721 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
723 chip_err(chip
, "failed to allocate pin ranges\n");
727 /* Use local offset as range ID */
728 pin_range
->range
.id
= gpio_offset
;
729 pin_range
->range
.gc
= chip
;
730 pin_range
->range
.name
= chip
->label
;
731 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
732 pin_range
->range
.pin_base
= pin_offset
;
733 pin_range
->range
.npins
= npins
;
734 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
736 if (IS_ERR(pin_range
->pctldev
)) {
737 ret
= PTR_ERR(pin_range
->pctldev
);
738 chip_err(chip
, "could not create pin range\n");
742 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
743 gpio_offset
, gpio_offset
+ npins
- 1,
745 pin_offset
, pin_offset
+ npins
- 1);
747 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
751 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
754 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
755 * @chip: the chip to remove all the mappings for
757 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
759 struct gpio_pin_range
*pin_range
, *tmp
;
761 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
762 list_del(&pin_range
->node
);
763 pinctrl_remove_gpio_range(pin_range
->pctldev
,
768 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
770 #endif /* CONFIG_PINCTRL */
772 /* These "optional" allocation calls help prevent drivers from stomping
773 * on each other, and help provide better diagnostics in debugfs.
774 * They're called even less than the "set direction" calls.
776 static int __gpiod_request(struct gpio_desc
*desc
, const char *label
)
778 struct gpio_chip
*chip
= desc
->chip
;
782 spin_lock_irqsave(&gpio_lock
, flags
);
784 /* NOTE: gpio_request() can be called in early boot,
785 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
788 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
789 desc_set_label(desc
, label
? : "?");
797 /* chip->request may sleep */
798 spin_unlock_irqrestore(&gpio_lock
, flags
);
799 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
800 spin_lock_irqsave(&gpio_lock
, flags
);
803 desc_set_label(desc
, NULL
);
804 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
808 if (chip
->get_direction
) {
809 /* chip->get_direction may sleep */
810 spin_unlock_irqrestore(&gpio_lock
, flags
);
811 gpiod_get_direction(desc
);
812 spin_lock_irqsave(&gpio_lock
, flags
);
815 spin_unlock_irqrestore(&gpio_lock
, flags
);
819 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
821 int status
= -EPROBE_DEFER
;
822 struct gpio_chip
*chip
;
825 pr_warn("%s: invalid GPIO\n", __func__
);
833 if (try_module_get(chip
->owner
)) {
834 status
= __gpiod_request(desc
, label
);
836 module_put(chip
->owner
);
841 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
846 static bool __gpiod_free(struct gpio_desc
*desc
)
850 struct gpio_chip
*chip
;
854 gpiod_unexport(desc
);
856 spin_lock_irqsave(&gpio_lock
, flags
);
859 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
861 spin_unlock_irqrestore(&gpio_lock
, flags
);
862 might_sleep_if(chip
->can_sleep
);
863 chip
->free(chip
, gpio_chip_hwgpio(desc
));
864 spin_lock_irqsave(&gpio_lock
, flags
);
866 desc_set_label(desc
, NULL
);
867 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
868 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
869 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
870 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
871 clear_bit(FLAG_IS_HOGGED
, &desc
->flags
);
875 spin_unlock_irqrestore(&gpio_lock
, flags
);
879 void gpiod_free(struct gpio_desc
*desc
)
881 if (desc
&& __gpiod_free(desc
))
882 module_put(desc
->chip
->owner
);
884 WARN_ON(extra_checks
);
888 * gpiochip_is_requested - return string iff signal was requested
889 * @chip: controller managing the signal
890 * @offset: of signal within controller's 0..(ngpio - 1) range
892 * Returns NULL if the GPIO is not currently requested, else a string.
893 * The string returned is the label passed to gpio_request(); if none has been
894 * passed it is a meaningless, non-NULL constant.
896 * This function is for use by GPIO controller drivers. The label can
897 * help with diagnostics, and knowing that the signal is used as a GPIO
898 * can help avoid accidentally multiplexing it to another controller.
900 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
902 struct gpio_desc
*desc
;
904 if (!GPIO_OFFSET_VALID(chip
, offset
))
907 desc
= &chip
->desc
[offset
];
909 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
913 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
916 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
917 * @desc: GPIO descriptor to request
918 * @label: label for the GPIO
920 * Function allows GPIO chip drivers to request and use their own GPIO
921 * descriptors via gpiolib API. Difference to gpiod_request() is that this
922 * function will not increase reference count of the GPIO chip module. This
923 * allows the GPIO chip module to be unloaded as needed (we assume that the
924 * GPIO chip driver handles freeing the GPIOs it has requested).
926 struct gpio_desc
*gpiochip_request_own_desc(struct gpio_chip
*chip
, u16 hwnum
,
929 struct gpio_desc
*desc
= gpiochip_get_desc(chip
, hwnum
);
933 chip_err(chip
, "failed to get GPIO descriptor\n");
937 err
= __gpiod_request(desc
, label
);
943 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc
);
946 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
947 * @desc: GPIO descriptor to free
949 * Function frees the given GPIO requested previously with
950 * gpiochip_request_own_desc().
952 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
957 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc
);
959 /* Drivers MUST set GPIO direction before making get/set calls. In
960 * some cases this is done in early boot, before IRQs are enabled.
962 * As a rule these aren't called more than once (except for drivers
963 * using the open-drain emulation idiom) so these are natural places
964 * to accumulate extra debugging checks. Note that we can't (yet)
965 * rely on gpio_request() having been called beforehand.
969 * gpiod_direction_input - set the GPIO direction to input
970 * @desc: GPIO to set to input
972 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
973 * be called safely on it.
975 * Return 0 in case of success, else an error code.
977 int gpiod_direction_input(struct gpio_desc
*desc
)
979 struct gpio_chip
*chip
;
980 int status
= -EINVAL
;
982 if (!desc
|| !desc
->chip
) {
983 pr_warn("%s: invalid GPIO\n", __func__
);
988 if (!chip
->get
|| !chip
->direction_input
) {
990 "%s: missing get() or direction_input() operations\n",
995 status
= chip
->direction_input(chip
, gpio_chip_hwgpio(desc
));
997 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
999 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1003 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
1005 static int _gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1007 struct gpio_chip
*chip
;
1008 int status
= -EINVAL
;
1010 /* GPIOs used for IRQs shall not be set as output */
1011 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
1013 "%s: tried to set a GPIO tied to an IRQ as output\n",
1018 /* Open drain pin should not be driven to 1 */
1019 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1020 return gpiod_direction_input(desc
);
1022 /* Open source pin should not be driven to 0 */
1023 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1024 return gpiod_direction_input(desc
);
1027 if (!chip
->set
|| !chip
->direction_output
) {
1029 "%s: missing set() or direction_output() operations\n",
1034 status
= chip
->direction_output(chip
, gpio_chip_hwgpio(desc
), value
);
1036 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1037 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1038 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1043 * gpiod_direction_output_raw - set the GPIO direction to output
1044 * @desc: GPIO to set to output
1045 * @value: initial output value of the GPIO
1047 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1048 * be called safely on it. The initial value of the output must be specified
1049 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1051 * Return 0 in case of success, else an error code.
1053 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1055 if (!desc
|| !desc
->chip
) {
1056 pr_warn("%s: invalid GPIO\n", __func__
);
1059 return _gpiod_direction_output_raw(desc
, value
);
1061 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
1064 * gpiod_direction_output - set the GPIO direction to output
1065 * @desc: GPIO to set to output
1066 * @value: initial output value of the GPIO
1068 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1069 * be called safely on it. The initial value of the output must be specified
1070 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1073 * Return 0 in case of success, else an error code.
1075 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1077 if (!desc
|| !desc
->chip
) {
1078 pr_warn("%s: invalid GPIO\n", __func__
);
1081 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1083 return _gpiod_direction_output_raw(desc
, value
);
1085 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1088 * gpiod_set_debounce - sets @debounce time for a @gpio
1089 * @gpio: the gpio to set debounce time
1090 * @debounce: debounce time is microseconds
1092 * returns -ENOTSUPP if the controller does not support setting
1095 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1097 struct gpio_chip
*chip
;
1099 if (!desc
|| !desc
->chip
) {
1100 pr_warn("%s: invalid GPIO\n", __func__
);
1105 if (!chip
->set
|| !chip
->set_debounce
) {
1107 "%s: missing set() or set_debounce() operations\n",
1112 return chip
->set_debounce(chip
, gpio_chip_hwgpio(desc
), debounce
);
1114 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1117 * gpiod_is_active_low - test whether a GPIO is active-low or not
1118 * @desc: the gpio descriptor to test
1120 * Returns 1 if the GPIO is active-low, 0 otherwise.
1122 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1124 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1126 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1128 /* I/O calls are only valid after configuration completed; the relevant
1129 * "is this a valid GPIO" error checks should already have been done.
1131 * "Get" operations are often inlinable as reading a pin value register,
1132 * and masking the relevant bit in that register.
1134 * When "set" operations are inlinable, they involve writing that mask to
1135 * one register to set a low value, or a different register to set it high.
1136 * Otherwise locking is needed, so there may be little value to inlining.
1138 *------------------------------------------------------------------------
1140 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1141 * have requested the GPIO. That can include implicit requesting by
1142 * a direction setting call. Marking a gpio as requested locks its chip
1143 * in memory, guaranteeing that these table lookups need no more locking
1144 * and that gpiochip_remove() will fail.
1146 * REVISIT when debugging, consider adding some instrumentation to ensure
1147 * that the GPIO was actually requested.
1150 static bool _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1152 struct gpio_chip
*chip
;
1157 offset
= gpio_chip_hwgpio(desc
);
1158 value
= chip
->get
? chip
->get(chip
, offset
) : false;
1159 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1164 * gpiod_get_raw_value() - return a gpio's raw value
1165 * @desc: gpio whose value will be returned
1167 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1168 * its ACTIVE_LOW status.
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_raw_value(const struct gpio_desc
*desc
)
1177 /* Should be using gpio_get_value_cansleep() */
1178 WARN_ON(desc
->chip
->can_sleep
);
1179 return _gpiod_get_raw_value(desc
);
1181 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1184 * gpiod_get_value() - return a gpio's value
1185 * @desc: gpio whose value will be returned
1187 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1190 * This function should be called from contexts where we cannot sleep, and will
1191 * complain if the GPIO chip functions potentially sleep.
1193 int gpiod_get_value(const struct gpio_desc
*desc
)
1198 /* Should be using gpio_get_value_cansleep() */
1199 WARN_ON(desc
->chip
->can_sleep
);
1201 value
= _gpiod_get_raw_value(desc
);
1202 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1207 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1210 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1211 * @desc: gpio descriptor whose state need to be set.
1212 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1214 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, bool value
)
1217 struct gpio_chip
*chip
= desc
->chip
;
1218 int offset
= gpio_chip_hwgpio(desc
);
1221 err
= chip
->direction_input(chip
, offset
);
1223 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1225 err
= chip
->direction_output(chip
, offset
, 0);
1227 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1229 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1232 "%s: Error in set_value for open drain err %d\n",
1237 * _gpio_set_open_source_value() - Set the open source gpio's value.
1238 * @desc: gpio descriptor whose state need to be set.
1239 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1241 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, bool value
)
1244 struct gpio_chip
*chip
= desc
->chip
;
1245 int offset
= gpio_chip_hwgpio(desc
);
1248 err
= chip
->direction_output(chip
, offset
, 1);
1250 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1252 err
= chip
->direction_input(chip
, offset
);
1254 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1256 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
1259 "%s: Error in set_value for open source err %d\n",
1263 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, bool value
)
1265 struct gpio_chip
*chip
;
1268 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1269 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1270 _gpio_set_open_drain_value(desc
, value
);
1271 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1272 _gpio_set_open_source_value(desc
, value
);
1274 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
1278 * set multiple outputs on the same chip;
1279 * use the chip's set_multiple function if available;
1280 * otherwise set the outputs sequentially;
1281 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1282 * defines which outputs are to be changed
1283 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1284 * defines the values the outputs specified by mask are to be set to
1286 static void gpio_chip_set_multiple(struct gpio_chip
*chip
,
1287 unsigned long *mask
, unsigned long *bits
)
1289 if (chip
->set_multiple
) {
1290 chip
->set_multiple(chip
, mask
, bits
);
1293 for (i
= 0; i
< chip
->ngpio
; i
++) {
1294 if (mask
[BIT_WORD(i
)] == 0) {
1295 /* no more set bits in this mask word;
1296 * skip ahead to the next word */
1297 i
= (BIT_WORD(i
) + 1) * BITS_PER_LONG
- 1;
1300 /* set outputs if the corresponding mask bit is set */
1301 if (__test_and_clear_bit(i
, mask
)) {
1302 chip
->set(chip
, i
, test_bit(i
, bits
));
1308 static void gpiod_set_array_priv(bool raw
, bool can_sleep
,
1309 unsigned int array_size
,
1310 struct gpio_desc
**desc_array
,
1315 while (i
< array_size
) {
1316 struct gpio_chip
*chip
= desc_array
[i
]->chip
;
1317 unsigned long mask
[BITS_TO_LONGS(chip
->ngpio
)];
1318 unsigned long bits
[BITS_TO_LONGS(chip
->ngpio
)];
1322 WARN_ON(chip
->can_sleep
);
1324 memset(mask
, 0, sizeof(mask
));
1326 struct gpio_desc
*desc
= desc_array
[i
];
1327 int hwgpio
= gpio_chip_hwgpio(desc
);
1328 int value
= value_array
[i
];
1330 if (!raw
&& test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1332 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1334 * collect all normal outputs belonging to the same chip
1335 * open drain and open source outputs are set individually
1337 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
)) {
1338 _gpio_set_open_drain_value(desc
,value
);
1339 } else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
)) {
1340 _gpio_set_open_source_value(desc
, value
);
1342 __set_bit(hwgpio
, mask
);
1344 __set_bit(hwgpio
, bits
);
1346 __clear_bit(hwgpio
, bits
);
1351 } while ((i
< array_size
) && (desc_array
[i
]->chip
== chip
));
1352 /* push collected bits to outputs */
1354 gpio_chip_set_multiple(chip
, mask
, bits
);
1360 * gpiod_set_raw_value() - assign a gpio's raw value
1361 * @desc: gpio whose value will be assigned
1362 * @value: value to assign
1364 * Set the raw value of the GPIO, i.e. the value of its physical line without
1365 * regard for its ACTIVE_LOW status.
1367 * This function should be called from contexts where we cannot sleep, and will
1368 * complain if the GPIO chip functions potentially sleep.
1370 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
1374 /* Should be using gpio_set_value_cansleep() */
1375 WARN_ON(desc
->chip
->can_sleep
);
1376 _gpiod_set_raw_value(desc
, value
);
1378 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
1381 * gpiod_set_value() - assign a gpio's value
1382 * @desc: gpio whose value will be assigned
1383 * @value: value to assign
1385 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1388 * This function should be called from contexts where we cannot sleep, and will
1389 * complain if the GPIO chip functions potentially sleep.
1391 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
1395 /* Should be using gpio_set_value_cansleep() */
1396 WARN_ON(desc
->chip
->can_sleep
);
1397 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1399 _gpiod_set_raw_value(desc
, value
);
1401 EXPORT_SYMBOL_GPL(gpiod_set_value
);
1404 * gpiod_set_raw_array() - assign values to an array of GPIOs
1405 * @array_size: number of elements in the descriptor / value arrays
1406 * @desc_array: array of GPIO descriptors whose values will be assigned
1407 * @value_array: array of values to assign
1409 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1410 * without regard for their ACTIVE_LOW status.
1412 * This function should be called from contexts where we cannot sleep, and will
1413 * complain if the GPIO chip functions potentially sleep.
1415 void gpiod_set_raw_array(unsigned int array_size
,
1416 struct gpio_desc
**desc_array
, int *value_array
)
1420 gpiod_set_array_priv(true, false, array_size
, desc_array
, value_array
);
1422 EXPORT_SYMBOL_GPL(gpiod_set_raw_array
);
1425 * gpiod_set_array() - assign values to an array of GPIOs
1426 * @array_size: number of elements in the descriptor / value arrays
1427 * @desc_array: array of GPIO descriptors whose values will be assigned
1428 * @value_array: array of values to assign
1430 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1433 * This function should be called from contexts where we cannot sleep, and will
1434 * complain if the GPIO chip functions potentially sleep.
1436 void gpiod_set_array(unsigned int array_size
,
1437 struct gpio_desc
**desc_array
, int *value_array
)
1441 gpiod_set_array_priv(false, false, array_size
, desc_array
, value_array
);
1443 EXPORT_SYMBOL_GPL(gpiod_set_array
);
1446 * gpiod_cansleep() - report whether gpio value access may sleep
1447 * @desc: gpio to check
1450 int gpiod_cansleep(const struct gpio_desc
*desc
)
1454 return desc
->chip
->can_sleep
;
1456 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
1459 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1460 * @desc: gpio whose IRQ will be returned (already requested)
1462 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1465 int gpiod_to_irq(const struct gpio_desc
*desc
)
1467 struct gpio_chip
*chip
;
1473 offset
= gpio_chip_hwgpio(desc
);
1474 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
1476 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
1479 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1480 * @chip: the chip the GPIO to lock belongs to
1481 * @offset: the offset of the GPIO to lock as IRQ
1483 * This is used directly by GPIO drivers that want to lock down
1484 * a certain GPIO line to be used for IRQs.
1486 int gpiochip_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
1488 if (offset
>= chip
->ngpio
)
1491 if (test_bit(FLAG_IS_OUT
, &chip
->desc
[offset
].flags
)) {
1493 "%s: tried to flag a GPIO set as output for IRQ\n",
1498 set_bit(FLAG_USED_AS_IRQ
, &chip
->desc
[offset
].flags
);
1501 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq
);
1504 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1505 * @chip: the chip the GPIO to lock belongs to
1506 * @offset: the offset of the GPIO to lock as IRQ
1508 * This is used directly by GPIO drivers that want to indicate
1509 * that a certain GPIO is no longer used exclusively for IRQ.
1511 void gpiochip_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
1513 if (offset
>= chip
->ngpio
)
1516 clear_bit(FLAG_USED_AS_IRQ
, &chip
->desc
[offset
].flags
);
1518 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq
);
1521 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1522 * @desc: gpio whose value will be returned
1524 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1525 * its ACTIVE_LOW status.
1527 * This function is to be called from contexts that can sleep.
1529 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
1531 might_sleep_if(extra_checks
);
1534 return _gpiod_get_raw_value(desc
);
1536 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
1539 * gpiod_get_value_cansleep() - return a gpio's value
1540 * @desc: gpio whose value will be returned
1542 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1545 * This function is to be called from contexts that can sleep.
1547 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
1551 might_sleep_if(extra_checks
);
1555 value
= _gpiod_get_raw_value(desc
);
1556 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1561 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
1564 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1565 * @desc: gpio whose value will be assigned
1566 * @value: value to assign
1568 * Set the raw value of the GPIO, i.e. the value of its physical line without
1569 * regard for its ACTIVE_LOW status.
1571 * This function is to be called from contexts that can sleep.
1573 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
1575 might_sleep_if(extra_checks
);
1578 _gpiod_set_raw_value(desc
, value
);
1580 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
1583 * gpiod_set_value_cansleep() - assign a gpio's value
1584 * @desc: gpio whose value will be assigned
1585 * @value: value to assign
1587 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1590 * This function is to be called from contexts that can sleep.
1592 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
1594 might_sleep_if(extra_checks
);
1598 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1600 _gpiod_set_raw_value(desc
, value
);
1602 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
1605 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1606 * @array_size: number of elements in the descriptor / value arrays
1607 * @desc_array: array of GPIO descriptors whose values will be assigned
1608 * @value_array: array of values to assign
1610 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1611 * without regard for their ACTIVE_LOW status.
1613 * This function is to be called from contexts that can sleep.
1615 void gpiod_set_raw_array_cansleep(unsigned int array_size
,
1616 struct gpio_desc
**desc_array
,
1619 might_sleep_if(extra_checks
);
1622 gpiod_set_array_priv(true, true, array_size
, desc_array
, value_array
);
1624 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep
);
1627 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1628 * @array_size: number of elements in the descriptor / value arrays
1629 * @desc_array: array of GPIO descriptors whose values will be assigned
1630 * @value_array: array of values to assign
1632 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1635 * This function is to be called from contexts that can sleep.
1637 void gpiod_set_array_cansleep(unsigned int array_size
,
1638 struct gpio_desc
**desc_array
,
1641 might_sleep_if(extra_checks
);
1644 gpiod_set_array_priv(false, true, array_size
, desc_array
, value_array
);
1646 EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep
);
1649 * gpiod_add_lookup_table() - register GPIO device consumers
1650 * @table: table of consumers to register
1652 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
1654 mutex_lock(&gpio_lookup_lock
);
1656 list_add_tail(&table
->list
, &gpio_lookup_list
);
1658 mutex_unlock(&gpio_lookup_lock
);
1661 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
1663 enum gpio_lookup_flags
*flags
)
1665 char prop_name
[32]; /* 32 is max size of property name */
1666 enum of_gpio_flags of_flags
;
1667 struct gpio_desc
*desc
;
1670 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
1672 snprintf(prop_name
, sizeof(prop_name
), "%s-%s", con_id
,
1675 snprintf(prop_name
, sizeof(prop_name
), "%s",
1678 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
1680 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
1687 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
1688 *flags
|= GPIO_ACTIVE_LOW
;
1693 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
1695 enum gpio_lookup_flags
*flags
)
1697 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
1698 struct acpi_gpio_info info
;
1699 struct gpio_desc
*desc
;
1703 /* Try first from _DSD */
1704 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
1705 if (con_id
&& strcmp(con_id
, "gpios")) {
1706 snprintf(propname
, sizeof(propname
), "%s-%s",
1707 con_id
, gpio_suffixes
[i
]);
1709 snprintf(propname
, sizeof(propname
), "%s",
1713 desc
= acpi_get_gpiod_by_index(adev
, propname
, idx
, &info
);
1714 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
1718 /* Then from plain _CRS GPIOs */
1720 desc
= acpi_get_gpiod_by_index(adev
, NULL
, idx
, &info
);
1725 if (info
.active_low
)
1726 *flags
|= GPIO_ACTIVE_LOW
;
1731 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
1733 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
1734 struct gpiod_lookup_table
*table
;
1736 mutex_lock(&gpio_lookup_lock
);
1738 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
1739 if (table
->dev_id
&& dev_id
) {
1741 * Valid strings on both ends, must be identical to have
1744 if (!strcmp(table
->dev_id
, dev_id
))
1748 * One of the pointers is NULL, so both must be to have
1751 if (dev_id
== table
->dev_id
)
1758 mutex_unlock(&gpio_lookup_lock
);
1762 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
1764 enum gpio_lookup_flags
*flags
)
1766 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
1767 struct gpiod_lookup_table
*table
;
1768 struct gpiod_lookup
*p
;
1770 table
= gpiod_find_lookup_table(dev
);
1774 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
1775 struct gpio_chip
*chip
;
1777 /* idx must always match exactly */
1781 /* If the lookup entry has a con_id, require exact match */
1782 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
1785 chip
= find_chip_by_name(p
->chip_label
);
1788 dev_err(dev
, "cannot find GPIO chip %s\n",
1790 return ERR_PTR(-ENODEV
);
1793 if (chip
->ngpio
<= p
->chip_hwnum
) {
1795 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1796 idx
, chip
->ngpio
, chip
->label
);
1797 return ERR_PTR(-EINVAL
);
1800 desc
= gpiochip_get_desc(chip
, p
->chip_hwnum
);
1809 static int dt_gpio_count(struct device
*dev
, const char *con_id
)
1815 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
1817 snprintf(propname
, sizeof(propname
), "%s-%s",
1818 con_id
, gpio_suffixes
[i
]);
1820 snprintf(propname
, sizeof(propname
), "%s",
1823 ret
= of_gpio_named_count(dev
->of_node
, propname
);
1830 static int platform_gpio_count(struct device
*dev
, const char *con_id
)
1832 struct gpiod_lookup_table
*table
;
1833 struct gpiod_lookup
*p
;
1834 unsigned int count
= 0;
1836 table
= gpiod_find_lookup_table(dev
);
1840 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
1841 if ((con_id
&& p
->con_id
&& !strcmp(con_id
, p
->con_id
)) ||
1842 (!con_id
&& !p
->con_id
))
1852 * gpiod_count - return the number of GPIOs associated with a device / function
1853 * or -ENOENT if no GPIO has been assigned to the requested function
1854 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1855 * @con_id: function within the GPIO consumer
1857 int gpiod_count(struct device
*dev
, const char *con_id
)
1859 int count
= -ENOENT
;
1861 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
)
1862 count
= dt_gpio_count(dev
, con_id
);
1863 else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
))
1864 count
= acpi_gpio_count(dev
, con_id
);
1867 count
= platform_gpio_count(dev
, con_id
);
1871 EXPORT_SYMBOL_GPL(gpiod_count
);
1874 * gpiod_get - obtain a GPIO for a given GPIO function
1875 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1876 * @con_id: function within the GPIO consumer
1877 * @flags: optional GPIO initialization flags
1879 * Return the GPIO descriptor corresponding to the function con_id of device
1880 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1881 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
1883 struct gpio_desc
*__must_check
__gpiod_get(struct device
*dev
, const char *con_id
,
1884 enum gpiod_flags flags
)
1886 return gpiod_get_index(dev
, con_id
, 0, flags
);
1888 EXPORT_SYMBOL_GPL(__gpiod_get
);
1891 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1892 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1893 * @con_id: function within the GPIO consumer
1894 * @flags: optional GPIO initialization flags
1896 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1897 * the requested function it will return NULL. This is convenient for drivers
1898 * that need to handle optional GPIOs.
1900 struct gpio_desc
*__must_check
__gpiod_get_optional(struct device
*dev
,
1902 enum gpiod_flags flags
)
1904 return gpiod_get_index_optional(dev
, con_id
, 0, flags
);
1906 EXPORT_SYMBOL_GPL(__gpiod_get_optional
);
1910 * gpiod_configure_flags - helper function to configure a given GPIO
1911 * @desc: gpio whose value will be assigned
1912 * @con_id: function within the GPIO consumer
1913 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
1915 * @dflags: gpiod_flags - optional GPIO initialization flags
1917 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
1918 * requested function and/or index, or another IS_ERR() code if an error
1919 * occurred while trying to acquire the GPIO.
1921 static int gpiod_configure_flags(struct gpio_desc
*desc
, const char *con_id
,
1922 unsigned long lflags
, enum gpiod_flags dflags
)
1926 if (lflags
& GPIO_ACTIVE_LOW
)
1927 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1928 if (lflags
& GPIO_OPEN_DRAIN
)
1929 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1930 if (lflags
& GPIO_OPEN_SOURCE
)
1931 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1933 /* No particular flag request, return here... */
1934 if (!(dflags
& GPIOD_FLAGS_BIT_DIR_SET
)) {
1935 pr_debug("no flags found for %s\n", con_id
);
1940 if (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
)
1941 status
= gpiod_direction_output(desc
,
1942 dflags
& GPIOD_FLAGS_BIT_DIR_VAL
);
1944 status
= gpiod_direction_input(desc
);
1950 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
1951 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1952 * @con_id: function within the GPIO consumer
1953 * @idx: index of the GPIO to obtain in the consumer
1954 * @flags: optional GPIO initialization flags
1956 * This variant of gpiod_get() allows to access GPIOs other than the first
1957 * defined one for functions that define several GPIOs.
1959 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1960 * requested function and/or index, or another IS_ERR() code if an error
1961 * occured while trying to acquire the GPIO.
1963 struct gpio_desc
*__must_check
__gpiod_get_index(struct device
*dev
,
1966 enum gpiod_flags flags
)
1968 struct gpio_desc
*desc
= NULL
;
1970 enum gpio_lookup_flags lookupflags
= 0;
1972 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
1975 /* Using device tree? */
1976 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
1977 dev_dbg(dev
, "using device tree for GPIO lookup\n");
1978 desc
= of_find_gpio(dev
, con_id
, idx
, &lookupflags
);
1979 } else if (ACPI_COMPANION(dev
)) {
1980 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
1981 desc
= acpi_find_gpio(dev
, con_id
, idx
, &lookupflags
);
1986 * Either we are not using DT or ACPI, or their lookup did not return
1987 * a result. In that case, use platform lookup as a fallback.
1989 if (!desc
|| desc
== ERR_PTR(-ENOENT
)) {
1990 dev_dbg(dev
, "using lookup tables for GPIO lookup\n");
1991 desc
= gpiod_find(dev
, con_id
, idx
, &lookupflags
);
1995 dev_dbg(dev
, "lookup for GPIO %s failed\n", con_id
);
1999 status
= gpiod_request(desc
, con_id
);
2001 return ERR_PTR(status
);
2003 status
= gpiod_configure_flags(desc
, con_id
, lookupflags
, flags
);
2005 dev_dbg(dev
, "setup of GPIO %s failed\n", con_id
);
2007 return ERR_PTR(status
);
2012 EXPORT_SYMBOL_GPL(__gpiod_get_index
);
2015 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2016 * @fwnode: handle of the firmware node
2017 * @propname: name of the firmware property representing the GPIO
2019 * This function can be used for drivers that get their configuration
2022 * Function properly finds the corresponding GPIO using whatever is the
2023 * underlying firmware interface and then makes sure that the GPIO
2024 * descriptor is requested before it is returned to the caller.
2026 * In case of error an ERR_PTR() is returned.
2028 struct gpio_desc
*fwnode_get_named_gpiod(struct fwnode_handle
*fwnode
,
2029 const char *propname
)
2031 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
2032 bool active_low
= false;
2036 return ERR_PTR(-EINVAL
);
2038 if (is_of_node(fwnode
)) {
2039 enum of_gpio_flags flags
;
2041 desc
= of_get_named_gpiod_flags(of_node(fwnode
), propname
, 0,
2044 active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
2045 } else if (is_acpi_node(fwnode
)) {
2046 struct acpi_gpio_info info
;
2048 desc
= acpi_get_gpiod_by_index(acpi_node(fwnode
), propname
, 0,
2051 active_low
= info
.active_low
;
2057 ret
= gpiod_request(desc
, NULL
);
2059 return ERR_PTR(ret
);
2061 /* Only value flag can be set from both DT and ACPI is active_low */
2063 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2067 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod
);
2070 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2072 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2073 * @con_id: function within the GPIO consumer
2074 * @index: index of the GPIO to obtain in the consumer
2075 * @flags: optional GPIO initialization flags
2077 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2078 * specified index was assigned to the requested function it will return NULL.
2079 * This is convenient for drivers that need to handle optional GPIOs.
2081 struct gpio_desc
*__must_check
__gpiod_get_index_optional(struct device
*dev
,
2084 enum gpiod_flags flags
)
2086 struct gpio_desc
*desc
;
2088 desc
= gpiod_get_index(dev
, con_id
, index
, flags
);
2090 if (PTR_ERR(desc
) == -ENOENT
)
2096 EXPORT_SYMBOL_GPL(__gpiod_get_index_optional
);
2099 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2100 * @desc: gpio whose value will be assigned
2101 * @name: gpio line name
2102 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2104 * @dflags: gpiod_flags - optional GPIO initialization flags
2106 int gpiod_hog(struct gpio_desc
*desc
, const char *name
,
2107 unsigned long lflags
, enum gpiod_flags dflags
)
2109 struct gpio_chip
*chip
;
2110 struct gpio_desc
*local_desc
;
2114 chip
= gpiod_to_chip(desc
);
2115 hwnum
= gpio_chip_hwgpio(desc
);
2117 local_desc
= gpiochip_request_own_desc(chip
, hwnum
, name
);
2118 if (IS_ERR(local_desc
)) {
2119 pr_debug("requesting own GPIO %s failed\n", name
);
2120 return PTR_ERR(local_desc
);
2123 status
= gpiod_configure_flags(desc
, name
, lflags
, dflags
);
2125 pr_debug("setup of GPIO %s failed\n", name
);
2126 gpiochip_free_own_desc(desc
);
2130 /* Mark GPIO as hogged so it can be identified and removed later */
2131 set_bit(FLAG_IS_HOGGED
, &desc
->flags
);
2133 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2134 desc_to_gpio(desc
), name
,
2135 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ? "output" : "input",
2136 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ?
2137 (dflags
&GPIOD_FLAGS_BIT_DIR_VAL
) ? "/high" : "/low":"");
2143 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2144 * @chip: gpio chip to act on
2146 * This is only used by of_gpiochip_remove to free hogged gpios
2148 static void gpiochip_free_hogs(struct gpio_chip
*chip
)
2152 for (id
= 0; id
< chip
->ngpio
; id
++) {
2153 if (test_bit(FLAG_IS_HOGGED
, &chip
->desc
[id
].flags
))
2154 gpiochip_free_own_desc(&chip
->desc
[id
]);
2159 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2160 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2161 * @con_id: function within the GPIO consumer
2162 * @flags: optional GPIO initialization flags
2164 * This function acquires all the GPIOs defined under a given function.
2166 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2167 * no GPIO has been assigned to the requested function, or another IS_ERR()
2168 * code if an error occurred while trying to acquire the GPIOs.
2170 struct gpio_descs
*__must_check
gpiod_get_array(struct device
*dev
,
2172 enum gpiod_flags flags
)
2174 struct gpio_desc
*desc
;
2175 struct gpio_descs
*descs
;
2178 count
= gpiod_count(dev
, con_id
);
2180 return ERR_PTR(count
);
2182 descs
= kzalloc(sizeof(*descs
) + sizeof(descs
->desc
[0]) * count
,
2185 return ERR_PTR(-ENOMEM
);
2187 for (descs
->ndescs
= 0; descs
->ndescs
< count
; ) {
2188 desc
= gpiod_get_index(dev
, con_id
, descs
->ndescs
, flags
);
2190 gpiod_put_array(descs
);
2191 return ERR_CAST(desc
);
2193 descs
->desc
[descs
->ndescs
] = desc
;
2198 EXPORT_SYMBOL_GPL(gpiod_get_array
);
2201 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2203 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2204 * @con_id: function within the GPIO consumer
2205 * @flags: optional GPIO initialization flags
2207 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2208 * assigned to the requested function it will return NULL.
2210 struct gpio_descs
*__must_check
gpiod_get_array_optional(struct device
*dev
,
2212 enum gpiod_flags flags
)
2214 struct gpio_descs
*descs
;
2216 descs
= gpiod_get_array(dev
, con_id
, flags
);
2217 if (IS_ERR(descs
) && (PTR_ERR(descs
) == -ENOENT
))
2222 EXPORT_SYMBOL_GPL(gpiod_get_array_optional
);
2225 * gpiod_put - dispose of a GPIO descriptor
2226 * @desc: GPIO descriptor to dispose of
2228 * No descriptor can be used after gpiod_put() has been called on it.
2230 void gpiod_put(struct gpio_desc
*desc
)
2234 EXPORT_SYMBOL_GPL(gpiod_put
);
2237 * gpiod_put_array - dispose of multiple GPIO descriptors
2238 * @descs: struct gpio_descs containing an array of descriptors
2240 void gpiod_put_array(struct gpio_descs
*descs
)
2244 for (i
= 0; i
< descs
->ndescs
; i
++)
2245 gpiod_put(descs
->desc
[i
]);
2249 EXPORT_SYMBOL_GPL(gpiod_put_array
);
2251 #ifdef CONFIG_DEBUG_FS
2253 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
2256 unsigned gpio
= chip
->base
;
2257 struct gpio_desc
*gdesc
= &chip
->desc
[0];
2261 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
2262 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
2265 gpiod_get_direction(gdesc
);
2266 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
2267 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
2268 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s %s",
2270 is_out
? "out" : "in ",
2272 ? (chip
->get(chip
, i
) ? "hi" : "lo")
2274 is_irq
? "IRQ" : " ");
2275 seq_printf(s
, "\n");
2279 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
2281 unsigned long flags
;
2282 struct gpio_chip
*chip
= NULL
;
2283 loff_t index
= *pos
;
2287 spin_lock_irqsave(&gpio_lock
, flags
);
2288 list_for_each_entry(chip
, &gpio_chips
, list
)
2290 spin_unlock_irqrestore(&gpio_lock
, flags
);
2293 spin_unlock_irqrestore(&gpio_lock
, flags
);
2298 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2300 unsigned long flags
;
2301 struct gpio_chip
*chip
= v
;
2304 spin_lock_irqsave(&gpio_lock
, flags
);
2305 if (list_is_last(&chip
->list
, &gpio_chips
))
2308 ret
= list_entry(chip
->list
.next
, struct gpio_chip
, list
);
2309 spin_unlock_irqrestore(&gpio_lock
, flags
);
2317 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
2321 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
2323 struct gpio_chip
*chip
= v
;
2326 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
2327 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
2330 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
2333 seq_printf(s
, ", %s", chip
->label
);
2334 if (chip
->can_sleep
)
2335 seq_printf(s
, ", can sleep");
2336 seq_printf(s
, ":\n");
2339 chip
->dbg_show(s
, chip
);
2341 gpiolib_dbg_show(s
, chip
);
2346 static const struct seq_operations gpiolib_seq_ops
= {
2347 .start
= gpiolib_seq_start
,
2348 .next
= gpiolib_seq_next
,
2349 .stop
= gpiolib_seq_stop
,
2350 .show
= gpiolib_seq_show
,
2353 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
2355 return seq_open(file
, &gpiolib_seq_ops
);
2358 static const struct file_operations gpiolib_operations
= {
2359 .owner
= THIS_MODULE
,
2360 .open
= gpiolib_open
,
2362 .llseek
= seq_lseek
,
2363 .release
= seq_release
,
2366 static int __init
gpiolib_debugfs_init(void)
2368 /* /sys/kernel/debug/gpio */
2369 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
2370 NULL
, NULL
, &gpiolib_operations
);
2373 subsys_initcall(gpiolib_debugfs_init
);
2375 #endif /* DEBUG_FS */