Linux 4.1.16
[linux/fpc-iii.git] / drivers / gpio / gpiolib.c
blob6bc612b8a49fcf859261173e00d0e7389d7d2b05
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
4 #include <linux/irq.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
8 #include <linux/err.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>
19 #include "gpiolib.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.
38 #ifdef DEBUG
39 #define extra_checks 1
40 #else
41 #define extra_checks 0
42 #endif
44 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
48 DEFINE_SPINLOCK(gpio_lock);
50 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
52 static DEFINE_MUTEX(gpio_lookup_lock);
53 static LIST_HEAD(gpio_lookup_list);
54 LIST_HEAD(gpio_chips);
57 static void gpiochip_free_hogs(struct gpio_chip *chip);
58 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
61 static inline void desc_set_label(struct gpio_desc *d, const char *label)
63 d->label = label;
66 /**
67 * Convert a GPIO number to its descriptor
69 struct gpio_desc *gpio_to_desc(unsigned gpio)
71 struct gpio_chip *chip;
72 unsigned long flags;
74 spin_lock_irqsave(&gpio_lock, flags);
76 list_for_each_entry(chip, &gpio_chips, list) {
77 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
78 spin_unlock_irqrestore(&gpio_lock, flags);
79 return &chip->desc[gpio - chip->base];
83 spin_unlock_irqrestore(&gpio_lock, flags);
85 if (!gpio_is_valid(gpio))
86 WARN(1, "invalid GPIO %d\n", gpio);
88 return NULL;
90 EXPORT_SYMBOL_GPL(gpio_to_desc);
92 /**
93 * Get the GPIO descriptor corresponding to the given hw number for this chip.
95 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
96 u16 hwnum)
98 if (hwnum >= chip->ngpio)
99 return ERR_PTR(-EINVAL);
101 return &chip->desc[hwnum];
105 * Convert a GPIO descriptor to the integer namespace.
106 * This should disappear in the future but is needed since we still
107 * use GPIO numbers for error messages and sysfs nodes
109 int desc_to_gpio(const struct gpio_desc *desc)
111 return desc->chip->base + (desc - &desc->chip->desc[0]);
113 EXPORT_SYMBOL_GPL(desc_to_gpio);
117 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
118 * @desc: descriptor to return the chip of
120 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
122 return desc ? desc->chip : NULL;
124 EXPORT_SYMBOL_GPL(gpiod_to_chip);
126 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
127 static int gpiochip_find_base(int ngpio)
129 struct gpio_chip *chip;
130 int base = ARCH_NR_GPIOS - ngpio;
132 list_for_each_entry_reverse(chip, &gpio_chips, list) {
133 /* found a free space? */
134 if (chip->base + chip->ngpio <= base)
135 break;
136 else
137 /* nope, check the space right before the chip */
138 base = chip->base - ngpio;
141 if (gpio_is_valid(base)) {
142 pr_debug("%s: found new base at %d\n", __func__, base);
143 return base;
144 } else {
145 pr_err("%s: cannot find free range\n", __func__);
146 return -ENOSPC;
151 * gpiod_get_direction - return the current direction of a GPIO
152 * @desc: GPIO to get the direction of
154 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
156 * This function may sleep if gpiod_cansleep() is true.
158 int gpiod_get_direction(struct gpio_desc *desc)
160 struct gpio_chip *chip;
161 unsigned offset;
162 int status = -EINVAL;
164 chip = gpiod_to_chip(desc);
165 offset = gpio_chip_hwgpio(desc);
167 if (!chip->get_direction)
168 return status;
170 status = chip->get_direction(chip, offset);
171 if (status > 0) {
172 /* GPIOF_DIR_IN, or other positive */
173 status = 1;
174 clear_bit(FLAG_IS_OUT, &desc->flags);
176 if (status == 0) {
177 /* GPIOF_DIR_OUT */
178 set_bit(FLAG_IS_OUT, &desc->flags);
180 return status;
182 EXPORT_SYMBOL_GPL(gpiod_get_direction);
185 * Add a new chip to the global chips list, keeping the list of chips sorted
186 * by base order.
188 * Return -EBUSY if the new chip overlaps with some other chip's integer
189 * space.
191 static int gpiochip_add_to_list(struct gpio_chip *chip)
193 struct list_head *pos = &gpio_chips;
194 struct gpio_chip *_chip;
195 int err = 0;
197 /* find where to insert our chip */
198 list_for_each(pos, &gpio_chips) {
199 _chip = list_entry(pos, struct gpio_chip, list);
200 /* shall we insert before _chip? */
201 if (_chip->base >= chip->base + chip->ngpio)
202 break;
205 /* are we stepping on the chip right before? */
206 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
207 _chip = list_entry(pos->prev, struct gpio_chip, list);
208 if (_chip->base + _chip->ngpio > chip->base) {
209 dev_err(chip->dev,
210 "GPIO integer space overlap, cannot add chip\n");
211 err = -EBUSY;
215 if (!err)
216 list_add_tail(&chip->list, pos);
218 return err;
222 * gpiochip_add() - register a gpio_chip
223 * @chip: the chip to register, with chip->base initialized
224 * Context: potentially before irqs will work
226 * Returns a negative errno if the chip can't be registered, such as
227 * because the chip->base is invalid or already associated with a
228 * different chip. Otherwise it returns zero as a success code.
230 * When gpiochip_add() is called very early during boot, so that GPIOs
231 * can be freely used, the chip->dev device must be registered before
232 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
233 * for GPIOs will fail rudely.
235 * If chip->base is negative, this requests dynamic assignment of
236 * a range of valid GPIOs.
238 int gpiochip_add(struct gpio_chip *chip)
240 unsigned long flags;
241 int status = 0;
242 unsigned id;
243 int base = chip->base;
244 struct gpio_desc *descs;
246 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
247 if (!descs)
248 return -ENOMEM;
250 spin_lock_irqsave(&gpio_lock, flags);
252 if (base < 0) {
253 base = gpiochip_find_base(chip->ngpio);
254 if (base < 0) {
255 status = base;
256 spin_unlock_irqrestore(&gpio_lock, flags);
257 goto err_free_descs;
259 chip->base = base;
262 status = gpiochip_add_to_list(chip);
263 if (status) {
264 spin_unlock_irqrestore(&gpio_lock, flags);
265 goto err_free_descs;
268 for (id = 0; id < chip->ngpio; id++) {
269 struct gpio_desc *desc = &descs[id];
271 desc->chip = chip;
273 /* REVISIT: most hardware initializes GPIOs as inputs (often
274 * with pullups enabled) so power usage is minimized. Linux
275 * code should set the gpio direction first thing; but until
276 * it does, and in case chip->get_direction is not set, we may
277 * expose the wrong direction in sysfs.
279 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
282 chip->desc = descs;
284 spin_unlock_irqrestore(&gpio_lock, flags);
286 #ifdef CONFIG_PINCTRL
287 INIT_LIST_HEAD(&chip->pin_ranges);
288 #endif
290 of_gpiochip_add(chip);
291 acpi_gpiochip_add(chip);
293 status = gpiochip_export(chip);
294 if (status)
295 goto err_remove_chip;
297 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
298 chip->base, chip->base + chip->ngpio - 1,
299 chip->label ? : "generic");
301 return 0;
303 err_remove_chip:
304 acpi_gpiochip_remove(chip);
305 gpiochip_free_hogs(chip);
306 of_gpiochip_remove(chip);
307 spin_lock_irqsave(&gpio_lock, flags);
308 list_del(&chip->list);
309 spin_unlock_irqrestore(&gpio_lock, flags);
310 chip->desc = NULL;
311 err_free_descs:
312 kfree(descs);
314 /* failures here can mean systems won't boot... */
315 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
316 chip->base, chip->base + chip->ngpio - 1,
317 chip->label ? : "generic");
318 return status;
320 EXPORT_SYMBOL_GPL(gpiochip_add);
323 * gpiochip_remove() - unregister a gpio_chip
324 * @chip: the chip to unregister
326 * A gpio_chip with any GPIOs still requested may not be removed.
328 void gpiochip_remove(struct gpio_chip *chip)
330 unsigned long flags;
331 unsigned id;
333 gpiochip_unexport(chip);
335 gpiochip_irqchip_remove(chip);
337 acpi_gpiochip_remove(chip);
338 gpiochip_remove_pin_ranges(chip);
339 gpiochip_free_hogs(chip);
340 of_gpiochip_remove(chip);
342 spin_lock_irqsave(&gpio_lock, flags);
343 for (id = 0; id < chip->ngpio; id++) {
344 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
345 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
347 for (id = 0; id < chip->ngpio; id++)
348 chip->desc[id].chip = NULL;
350 list_del(&chip->list);
351 spin_unlock_irqrestore(&gpio_lock, flags);
353 kfree(chip->desc);
354 chip->desc = NULL;
356 EXPORT_SYMBOL_GPL(gpiochip_remove);
359 * gpiochip_find() - iterator for locating a specific gpio_chip
360 * @data: data to pass to match function
361 * @callback: Callback function to check gpio_chip
363 * Similar to bus_find_device. It returns a reference to a gpio_chip as
364 * determined by a user supplied @match callback. The callback should return
365 * 0 if the device doesn't match and non-zero if it does. If the callback is
366 * non-zero, this function will return to the caller and not iterate over any
367 * more gpio_chips.
369 struct gpio_chip *gpiochip_find(void *data,
370 int (*match)(struct gpio_chip *chip,
371 void *data))
373 struct gpio_chip *chip;
374 unsigned long flags;
376 spin_lock_irqsave(&gpio_lock, flags);
377 list_for_each_entry(chip, &gpio_chips, list)
378 if (match(chip, data))
379 break;
381 /* No match? */
382 if (&chip->list == &gpio_chips)
383 chip = NULL;
384 spin_unlock_irqrestore(&gpio_lock, flags);
386 return chip;
388 EXPORT_SYMBOL_GPL(gpiochip_find);
390 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
392 const char *name = data;
394 return !strcmp(chip->label, name);
397 static struct gpio_chip *find_chip_by_name(const char *name)
399 return gpiochip_find((void *)name, gpiochip_match_name);
402 #ifdef CONFIG_GPIOLIB_IRQCHIP
405 * The following is irqchip helper code for gpiochips.
409 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
410 * @gpiochip: the gpiochip to set the irqchip chain to
411 * @irqchip: the irqchip to chain to the gpiochip
412 * @parent_irq: the irq number corresponding to the parent IRQ for this
413 * chained irqchip
414 * @parent_handler: the parent interrupt handler for the accumulated IRQ
415 * coming out of the gpiochip. If the interrupt is nested rather than
416 * cascaded, pass NULL in this handler argument
418 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
419 struct irq_chip *irqchip,
420 int parent_irq,
421 irq_flow_handler_t parent_handler)
423 unsigned int offset;
425 if (!gpiochip->irqdomain) {
426 chip_err(gpiochip, "called %s before setting up irqchip\n",
427 __func__);
428 return;
431 if (parent_handler) {
432 if (gpiochip->can_sleep) {
433 chip_err(gpiochip,
434 "you cannot have chained interrupts on a "
435 "chip that may sleep\n");
436 return;
439 * The parent irqchip is already using the chip_data for this
440 * irqchip, so our callbacks simply use the handler_data.
442 irq_set_handler_data(parent_irq, gpiochip);
443 irq_set_chained_handler(parent_irq, parent_handler);
446 /* Set the parent IRQ for all affected IRQs */
447 for (offset = 0; offset < gpiochip->ngpio; offset++)
448 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
449 parent_irq);
451 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
454 * This lock class tells lockdep that GPIO irqs are in a different
455 * category than their parents, so it won't report false recursion.
457 static struct lock_class_key gpiochip_irq_lock_class;
460 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
461 * @d: the irqdomain used by this irqchip
462 * @irq: the global irq number used by this GPIO irqchip irq
463 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
465 * This function will set up the mapping for a certain IRQ line on a
466 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
467 * stored inside the gpiochip.
469 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
470 irq_hw_number_t hwirq)
472 struct gpio_chip *chip = d->host_data;
474 irq_set_chip_data(irq, chip);
475 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
476 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
477 /* Chips that can sleep need nested thread handlers */
478 if (chip->can_sleep && !chip->irq_not_threaded)
479 irq_set_nested_thread(irq, 1);
480 #ifdef CONFIG_ARM
481 set_irq_flags(irq, IRQF_VALID);
482 #else
483 irq_set_noprobe(irq);
484 #endif
486 * No set-up of the hardware will happen if IRQ_TYPE_NONE
487 * is passed as default type.
489 if (chip->irq_default_type != IRQ_TYPE_NONE)
490 irq_set_irq_type(irq, chip->irq_default_type);
492 return 0;
495 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
497 struct gpio_chip *chip = d->host_data;
499 #ifdef CONFIG_ARM
500 set_irq_flags(irq, 0);
501 #endif
502 if (chip->can_sleep)
503 irq_set_nested_thread(irq, 0);
504 irq_set_chip_and_handler(irq, NULL, NULL);
505 irq_set_chip_data(irq, NULL);
508 static const struct irq_domain_ops gpiochip_domain_ops = {
509 .map = gpiochip_irq_map,
510 .unmap = gpiochip_irq_unmap,
511 /* Virtually all GPIO irqchips are twocell:ed */
512 .xlate = irq_domain_xlate_twocell,
515 static int gpiochip_irq_reqres(struct irq_data *d)
517 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
519 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
520 chip_err(chip,
521 "unable to lock HW IRQ %lu for IRQ\n",
522 d->hwirq);
523 return -EINVAL;
525 return 0;
528 static void gpiochip_irq_relres(struct irq_data *d)
530 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
532 gpiochip_unlock_as_irq(chip, d->hwirq);
535 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
537 return irq_find_mapping(chip->irqdomain, offset);
541 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
542 * @gpiochip: the gpiochip to remove the irqchip from
544 * This is called only from gpiochip_remove()
546 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
548 unsigned int offset;
550 acpi_gpiochip_free_interrupts(gpiochip);
552 /* Remove all IRQ mappings and delete the domain */
553 if (gpiochip->irqdomain) {
554 for (offset = 0; offset < gpiochip->ngpio; offset++)
555 irq_dispose_mapping(
556 irq_find_mapping(gpiochip->irqdomain, offset));
557 irq_domain_remove(gpiochip->irqdomain);
560 if (gpiochip->irqchip) {
561 gpiochip->irqchip->irq_request_resources = NULL;
562 gpiochip->irqchip->irq_release_resources = NULL;
563 gpiochip->irqchip = NULL;
568 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
569 * @gpiochip: the gpiochip to add the irqchip to
570 * @irqchip: the irqchip to add to the gpiochip
571 * @first_irq: if not dynamically assigned, the base (first) IRQ to
572 * allocate gpiochip irqs from
573 * @handler: the irq handler to use (often a predefined irq core function)
574 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
575 * to have the core avoid setting up any default type in the hardware.
577 * This function closely associates a certain irqchip with a certain
578 * gpiochip, providing an irq domain to translate the local IRQs to
579 * global irqs in the gpiolib core, and making sure that the gpiochip
580 * is passed as chip data to all related functions. Driver callbacks
581 * need to use container_of() to get their local state containers back
582 * from the gpiochip passed as chip data. An irqdomain will be stored
583 * in the gpiochip that shall be used by the driver to handle IRQ number
584 * translation. The gpiochip will need to be initialized and registered
585 * before calling this function.
587 * This function will handle two cell:ed simple IRQs and assumes all
588 * the pins on the gpiochip can generate a unique IRQ. Everything else
589 * need to be open coded.
591 int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
592 struct irq_chip *irqchip,
593 unsigned int first_irq,
594 irq_flow_handler_t handler,
595 unsigned int type)
597 struct device_node *of_node;
598 unsigned int offset;
599 unsigned irq_base = 0;
601 if (!gpiochip || !irqchip)
602 return -EINVAL;
604 if (!gpiochip->dev) {
605 pr_err("missing gpiochip .dev parent pointer\n");
606 return -EINVAL;
608 of_node = gpiochip->dev->of_node;
609 #ifdef CONFIG_OF_GPIO
611 * If the gpiochip has an assigned OF node this takes precendence
612 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
614 if (gpiochip->of_node)
615 of_node = gpiochip->of_node;
616 #endif
617 gpiochip->irqchip = irqchip;
618 gpiochip->irq_handler = handler;
619 gpiochip->irq_default_type = type;
620 gpiochip->to_irq = gpiochip_to_irq;
621 gpiochip->irqdomain = irq_domain_add_simple(of_node,
622 gpiochip->ngpio, first_irq,
623 &gpiochip_domain_ops, gpiochip);
624 if (!gpiochip->irqdomain) {
625 gpiochip->irqchip = NULL;
626 return -EINVAL;
628 irqchip->irq_request_resources = gpiochip_irq_reqres;
629 irqchip->irq_release_resources = gpiochip_irq_relres;
632 * Prepare the mapping since the irqchip shall be orthogonal to
633 * any gpiochip calls. If the first_irq was zero, this is
634 * necessary to allocate descriptors for all IRQs.
636 for (offset = 0; offset < gpiochip->ngpio; offset++) {
637 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
638 if (offset == 0)
640 * Store the base into the gpiochip to be used when
641 * unmapping the irqs.
643 gpiochip->irq_base = irq_base;
646 acpi_gpiochip_request_interrupts(gpiochip);
648 return 0;
650 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
652 #else /* CONFIG_GPIOLIB_IRQCHIP */
654 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
656 #endif /* CONFIG_GPIOLIB_IRQCHIP */
658 #ifdef CONFIG_PINCTRL
661 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
662 * @chip: the gpiochip to add the range for
663 * @pinctrl: the dev_name() of the pin controller to map to
664 * @gpio_offset: the start offset in the current gpio_chip number space
665 * @pin_group: name of the pin group inside the pin controller
667 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
668 struct pinctrl_dev *pctldev,
669 unsigned int gpio_offset, const char *pin_group)
671 struct gpio_pin_range *pin_range;
672 int ret;
674 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
675 if (!pin_range) {
676 chip_err(chip, "failed to allocate pin ranges\n");
677 return -ENOMEM;
680 /* Use local offset as range ID */
681 pin_range->range.id = gpio_offset;
682 pin_range->range.gc = chip;
683 pin_range->range.name = chip->label;
684 pin_range->range.base = chip->base + gpio_offset;
685 pin_range->pctldev = pctldev;
687 ret = pinctrl_get_group_pins(pctldev, pin_group,
688 &pin_range->range.pins,
689 &pin_range->range.npins);
690 if (ret < 0) {
691 kfree(pin_range);
692 return ret;
695 pinctrl_add_gpio_range(pctldev, &pin_range->range);
697 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
698 gpio_offset, gpio_offset + pin_range->range.npins - 1,
699 pinctrl_dev_get_devname(pctldev), pin_group);
701 list_add_tail(&pin_range->node, &chip->pin_ranges);
703 return 0;
705 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
708 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
709 * @chip: the gpiochip to add the range for
710 * @pinctrl_name: the dev_name() of the pin controller to map to
711 * @gpio_offset: the start offset in the current gpio_chip number space
712 * @pin_offset: the start offset in the pin controller number space
713 * @npins: the number of pins from the offset of each pin space (GPIO and
714 * pin controller) to accumulate in this range
716 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
717 unsigned int gpio_offset, unsigned int pin_offset,
718 unsigned int npins)
720 struct gpio_pin_range *pin_range;
721 int ret;
723 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
724 if (!pin_range) {
725 chip_err(chip, "failed to allocate pin ranges\n");
726 return -ENOMEM;
729 /* Use local offset as range ID */
730 pin_range->range.id = gpio_offset;
731 pin_range->range.gc = chip;
732 pin_range->range.name = chip->label;
733 pin_range->range.base = chip->base + gpio_offset;
734 pin_range->range.pin_base = pin_offset;
735 pin_range->range.npins = npins;
736 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
737 &pin_range->range);
738 if (IS_ERR(pin_range->pctldev)) {
739 ret = PTR_ERR(pin_range->pctldev);
740 chip_err(chip, "could not create pin range\n");
741 kfree(pin_range);
742 return ret;
744 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
745 gpio_offset, gpio_offset + npins - 1,
746 pinctl_name,
747 pin_offset, pin_offset + npins - 1);
749 list_add_tail(&pin_range->node, &chip->pin_ranges);
751 return 0;
753 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
756 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
757 * @chip: the chip to remove all the mappings for
759 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
761 struct gpio_pin_range *pin_range, *tmp;
763 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
764 list_del(&pin_range->node);
765 pinctrl_remove_gpio_range(pin_range->pctldev,
766 &pin_range->range);
767 kfree(pin_range);
770 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
772 #endif /* CONFIG_PINCTRL */
774 /* These "optional" allocation calls help prevent drivers from stomping
775 * on each other, and help provide better diagnostics in debugfs.
776 * They're called even less than the "set direction" calls.
778 static int __gpiod_request(struct gpio_desc *desc, const char *label)
780 struct gpio_chip *chip = desc->chip;
781 int status;
782 unsigned long flags;
784 spin_lock_irqsave(&gpio_lock, flags);
786 /* NOTE: gpio_request() can be called in early boot,
787 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
790 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
791 desc_set_label(desc, label ? : "?");
792 status = 0;
793 } else {
794 status = -EBUSY;
795 goto done;
798 if (chip->request) {
799 /* chip->request may sleep */
800 spin_unlock_irqrestore(&gpio_lock, flags);
801 status = chip->request(chip, gpio_chip_hwgpio(desc));
802 spin_lock_irqsave(&gpio_lock, flags);
804 if (status < 0) {
805 desc_set_label(desc, NULL);
806 clear_bit(FLAG_REQUESTED, &desc->flags);
807 goto done;
810 if (chip->get_direction) {
811 /* chip->get_direction may sleep */
812 spin_unlock_irqrestore(&gpio_lock, flags);
813 gpiod_get_direction(desc);
814 spin_lock_irqsave(&gpio_lock, flags);
816 done:
817 spin_unlock_irqrestore(&gpio_lock, flags);
818 return status;
821 int gpiod_request(struct gpio_desc *desc, const char *label)
823 int status = -EPROBE_DEFER;
824 struct gpio_chip *chip;
826 if (!desc) {
827 pr_warn("%s: invalid GPIO\n", __func__);
828 return -EINVAL;
831 chip = desc->chip;
832 if (!chip)
833 goto done;
835 if (try_module_get(chip->owner)) {
836 status = __gpiod_request(desc, label);
837 if (status < 0)
838 module_put(chip->owner);
841 done:
842 if (status)
843 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
845 return status;
848 static bool __gpiod_free(struct gpio_desc *desc)
850 bool ret = false;
851 unsigned long flags;
852 struct gpio_chip *chip;
854 might_sleep();
856 gpiod_unexport(desc);
858 spin_lock_irqsave(&gpio_lock, flags);
860 chip = desc->chip;
861 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
862 if (chip->free) {
863 spin_unlock_irqrestore(&gpio_lock, flags);
864 might_sleep_if(chip->can_sleep);
865 chip->free(chip, gpio_chip_hwgpio(desc));
866 spin_lock_irqsave(&gpio_lock, flags);
868 desc_set_label(desc, NULL);
869 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
870 clear_bit(FLAG_REQUESTED, &desc->flags);
871 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
872 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
873 clear_bit(FLAG_IS_HOGGED, &desc->flags);
874 ret = true;
877 spin_unlock_irqrestore(&gpio_lock, flags);
878 return ret;
881 void gpiod_free(struct gpio_desc *desc)
883 if (desc && __gpiod_free(desc))
884 module_put(desc->chip->owner);
885 else
886 WARN_ON(extra_checks);
890 * gpiochip_is_requested - return string iff signal was requested
891 * @chip: controller managing the signal
892 * @offset: of signal within controller's 0..(ngpio - 1) range
894 * Returns NULL if the GPIO is not currently requested, else a string.
895 * The string returned is the label passed to gpio_request(); if none has been
896 * passed it is a meaningless, non-NULL constant.
898 * This function is for use by GPIO controller drivers. The label can
899 * help with diagnostics, and knowing that the signal is used as a GPIO
900 * can help avoid accidentally multiplexing it to another controller.
902 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
904 struct gpio_desc *desc;
906 if (!GPIO_OFFSET_VALID(chip, offset))
907 return NULL;
909 desc = &chip->desc[offset];
911 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
912 return NULL;
913 return desc->label;
915 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
918 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
919 * @desc: GPIO descriptor to request
920 * @label: label for the GPIO
922 * Function allows GPIO chip drivers to request and use their own GPIO
923 * descriptors via gpiolib API. Difference to gpiod_request() is that this
924 * function will not increase reference count of the GPIO chip module. This
925 * allows the GPIO chip module to be unloaded as needed (we assume that the
926 * GPIO chip driver handles freeing the GPIOs it has requested).
928 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
929 const char *label)
931 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
932 int err;
934 if (IS_ERR(desc)) {
935 chip_err(chip, "failed to get GPIO descriptor\n");
936 return desc;
939 err = __gpiod_request(desc, label);
940 if (err < 0)
941 return ERR_PTR(err);
943 return desc;
945 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
948 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
949 * @desc: GPIO descriptor to free
951 * Function frees the given GPIO requested previously with
952 * gpiochip_request_own_desc().
954 void gpiochip_free_own_desc(struct gpio_desc *desc)
956 if (desc)
957 __gpiod_free(desc);
959 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
961 /* Drivers MUST set GPIO direction before making get/set calls. In
962 * some cases this is done in early boot, before IRQs are enabled.
964 * As a rule these aren't called more than once (except for drivers
965 * using the open-drain emulation idiom) so these are natural places
966 * to accumulate extra debugging checks. Note that we can't (yet)
967 * rely on gpio_request() having been called beforehand.
971 * gpiod_direction_input - set the GPIO direction to input
972 * @desc: GPIO to set to input
974 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
975 * be called safely on it.
977 * Return 0 in case of success, else an error code.
979 int gpiod_direction_input(struct gpio_desc *desc)
981 struct gpio_chip *chip;
982 int status = -EINVAL;
984 if (!desc || !desc->chip) {
985 pr_warn("%s: invalid GPIO\n", __func__);
986 return -EINVAL;
989 chip = desc->chip;
990 if (!chip->get || !chip->direction_input) {
991 gpiod_warn(desc,
992 "%s: missing get() or direction_input() operations\n",
993 __func__);
994 return -EIO;
997 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
998 if (status == 0)
999 clear_bit(FLAG_IS_OUT, &desc->flags);
1001 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1003 return status;
1005 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1007 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1009 struct gpio_chip *chip;
1010 int status = -EINVAL;
1012 /* GPIOs used for IRQs shall not be set as output */
1013 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1014 gpiod_err(desc,
1015 "%s: tried to set a GPIO tied to an IRQ as output\n",
1016 __func__);
1017 return -EIO;
1020 /* Open drain pin should not be driven to 1 */
1021 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1022 return gpiod_direction_input(desc);
1024 /* Open source pin should not be driven to 0 */
1025 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1026 return gpiod_direction_input(desc);
1028 chip = desc->chip;
1029 if (!chip->set || !chip->direction_output) {
1030 gpiod_warn(desc,
1031 "%s: missing set() or direction_output() operations\n",
1032 __func__);
1033 return -EIO;
1036 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1037 if (status == 0)
1038 set_bit(FLAG_IS_OUT, &desc->flags);
1039 trace_gpio_value(desc_to_gpio(desc), 0, value);
1040 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1041 return status;
1045 * gpiod_direction_output_raw - set the GPIO direction to output
1046 * @desc: GPIO to set to output
1047 * @value: initial output value of the GPIO
1049 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1050 * be called safely on it. The initial value of the output must be specified
1051 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1053 * Return 0 in case of success, else an error code.
1055 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1057 if (!desc || !desc->chip) {
1058 pr_warn("%s: invalid GPIO\n", __func__);
1059 return -EINVAL;
1061 return _gpiod_direction_output_raw(desc, value);
1063 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1066 * gpiod_direction_output - set the GPIO direction to output
1067 * @desc: GPIO to set to output
1068 * @value: initial output value of the GPIO
1070 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1071 * be called safely on it. The initial value of the output must be specified
1072 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1073 * account.
1075 * Return 0 in case of success, else an error code.
1077 int gpiod_direction_output(struct gpio_desc *desc, int value)
1079 if (!desc || !desc->chip) {
1080 pr_warn("%s: invalid GPIO\n", __func__);
1081 return -EINVAL;
1083 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1084 value = !value;
1085 return _gpiod_direction_output_raw(desc, value);
1087 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1090 * gpiod_set_debounce - sets @debounce time for a @gpio
1091 * @gpio: the gpio to set debounce time
1092 * @debounce: debounce time is microseconds
1094 * returns -ENOTSUPP if the controller does not support setting
1095 * debounce.
1097 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1099 struct gpio_chip *chip;
1101 if (!desc || !desc->chip) {
1102 pr_warn("%s: invalid GPIO\n", __func__);
1103 return -EINVAL;
1106 chip = desc->chip;
1107 if (!chip->set || !chip->set_debounce) {
1108 gpiod_dbg(desc,
1109 "%s: missing set() or set_debounce() operations\n",
1110 __func__);
1111 return -ENOTSUPP;
1114 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1116 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1119 * gpiod_is_active_low - test whether a GPIO is active-low or not
1120 * @desc: the gpio descriptor to test
1122 * Returns 1 if the GPIO is active-low, 0 otherwise.
1124 int gpiod_is_active_low(const struct gpio_desc *desc)
1126 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1128 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1130 /* I/O calls are only valid after configuration completed; the relevant
1131 * "is this a valid GPIO" error checks should already have been done.
1133 * "Get" operations are often inlinable as reading a pin value register,
1134 * and masking the relevant bit in that register.
1136 * When "set" operations are inlinable, they involve writing that mask to
1137 * one register to set a low value, or a different register to set it high.
1138 * Otherwise locking is needed, so there may be little value to inlining.
1140 *------------------------------------------------------------------------
1142 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1143 * have requested the GPIO. That can include implicit requesting by
1144 * a direction setting call. Marking a gpio as requested locks its chip
1145 * in memory, guaranteeing that these table lookups need no more locking
1146 * and that gpiochip_remove() will fail.
1148 * REVISIT when debugging, consider adding some instrumentation to ensure
1149 * that the GPIO was actually requested.
1152 static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
1154 struct gpio_chip *chip;
1155 bool value;
1156 int offset;
1158 chip = desc->chip;
1159 offset = gpio_chip_hwgpio(desc);
1160 value = chip->get ? chip->get(chip, offset) : false;
1161 trace_gpio_value(desc_to_gpio(desc), 1, value);
1162 return value;
1166 * gpiod_get_raw_value() - return a gpio's raw value
1167 * @desc: gpio whose value will be returned
1169 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1170 * its ACTIVE_LOW status.
1172 * This function should be called from contexts where we cannot sleep, and will
1173 * complain if the GPIO chip functions potentially sleep.
1175 int gpiod_get_raw_value(const struct gpio_desc *desc)
1177 if (!desc)
1178 return 0;
1179 /* Should be using gpio_get_value_cansleep() */
1180 WARN_ON(desc->chip->can_sleep);
1181 return _gpiod_get_raw_value(desc);
1183 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1186 * gpiod_get_value() - return a gpio's value
1187 * @desc: gpio whose value will be returned
1189 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1190 * account.
1192 * This function should be called from contexts where we cannot sleep, and will
1193 * complain if the GPIO chip functions potentially sleep.
1195 int gpiod_get_value(const struct gpio_desc *desc)
1197 int value;
1198 if (!desc)
1199 return 0;
1200 /* Should be using gpio_get_value_cansleep() */
1201 WARN_ON(desc->chip->can_sleep);
1203 value = _gpiod_get_raw_value(desc);
1204 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1205 value = !value;
1207 return value;
1209 EXPORT_SYMBOL_GPL(gpiod_get_value);
1212 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1213 * @desc: gpio descriptor whose state need to be set.
1214 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1216 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1218 int err = 0;
1219 struct gpio_chip *chip = desc->chip;
1220 int offset = gpio_chip_hwgpio(desc);
1222 if (value) {
1223 err = chip->direction_input(chip, offset);
1224 if (!err)
1225 clear_bit(FLAG_IS_OUT, &desc->flags);
1226 } else {
1227 err = chip->direction_output(chip, offset, 0);
1228 if (!err)
1229 set_bit(FLAG_IS_OUT, &desc->flags);
1231 trace_gpio_direction(desc_to_gpio(desc), value, err);
1232 if (err < 0)
1233 gpiod_err(desc,
1234 "%s: Error in set_value for open drain err %d\n",
1235 __func__, err);
1239 * _gpio_set_open_source_value() - Set the open source gpio's value.
1240 * @desc: gpio descriptor whose state need to be set.
1241 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1243 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1245 int err = 0;
1246 struct gpio_chip *chip = desc->chip;
1247 int offset = gpio_chip_hwgpio(desc);
1249 if (value) {
1250 err = chip->direction_output(chip, offset, 1);
1251 if (!err)
1252 set_bit(FLAG_IS_OUT, &desc->flags);
1253 } else {
1254 err = chip->direction_input(chip, offset);
1255 if (!err)
1256 clear_bit(FLAG_IS_OUT, &desc->flags);
1258 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1259 if (err < 0)
1260 gpiod_err(desc,
1261 "%s: Error in set_value for open source err %d\n",
1262 __func__, err);
1265 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1267 struct gpio_chip *chip;
1269 chip = desc->chip;
1270 trace_gpio_value(desc_to_gpio(desc), 0, value);
1271 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1272 _gpio_set_open_drain_value(desc, value);
1273 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1274 _gpio_set_open_source_value(desc, value);
1275 else
1276 chip->set(chip, gpio_chip_hwgpio(desc), value);
1280 * set multiple outputs on the same chip;
1281 * use the chip's set_multiple function if available;
1282 * otherwise set the outputs sequentially;
1283 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1284 * defines which outputs are to be changed
1285 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1286 * defines the values the outputs specified by mask are to be set to
1288 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1289 unsigned long *mask, unsigned long *bits)
1291 if (chip->set_multiple) {
1292 chip->set_multiple(chip, mask, bits);
1293 } else {
1294 int i;
1295 for (i = 0; i < chip->ngpio; i++) {
1296 if (mask[BIT_WORD(i)] == 0) {
1297 /* no more set bits in this mask word;
1298 * skip ahead to the next word */
1299 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1300 continue;
1302 /* set outputs if the corresponding mask bit is set */
1303 if (__test_and_clear_bit(i, mask)) {
1304 chip->set(chip, i, test_bit(i, bits));
1310 static void gpiod_set_array_priv(bool raw, bool can_sleep,
1311 unsigned int array_size,
1312 struct gpio_desc **desc_array,
1313 int *value_array)
1315 int i = 0;
1317 while (i < array_size) {
1318 struct gpio_chip *chip = desc_array[i]->chip;
1319 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1320 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1321 int count = 0;
1323 if (!can_sleep) {
1324 WARN_ON(chip->can_sleep);
1326 memset(mask, 0, sizeof(mask));
1327 do {
1328 struct gpio_desc *desc = desc_array[i];
1329 int hwgpio = gpio_chip_hwgpio(desc);
1330 int value = value_array[i];
1332 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1333 value = !value;
1334 trace_gpio_value(desc_to_gpio(desc), 0, value);
1336 * collect all normal outputs belonging to the same chip
1337 * open drain and open source outputs are set individually
1339 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1340 _gpio_set_open_drain_value(desc,value);
1341 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1342 _gpio_set_open_source_value(desc, value);
1343 } else {
1344 __set_bit(hwgpio, mask);
1345 if (value) {
1346 __set_bit(hwgpio, bits);
1347 } else {
1348 __clear_bit(hwgpio, bits);
1350 count++;
1352 i++;
1353 } while ((i < array_size) && (desc_array[i]->chip == chip));
1354 /* push collected bits to outputs */
1355 if (count != 0) {
1356 gpio_chip_set_multiple(chip, mask, bits);
1362 * gpiod_set_raw_value() - assign a gpio's raw value
1363 * @desc: gpio whose value will be assigned
1364 * @value: value to assign
1366 * Set the raw value of the GPIO, i.e. the value of its physical line without
1367 * regard for its ACTIVE_LOW status.
1369 * This function should be called from contexts where we cannot sleep, and will
1370 * complain if the GPIO chip functions potentially sleep.
1372 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1374 if (!desc)
1375 return;
1376 /* Should be using gpio_set_value_cansleep() */
1377 WARN_ON(desc->chip->can_sleep);
1378 _gpiod_set_raw_value(desc, value);
1380 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1383 * gpiod_set_value() - assign a gpio's value
1384 * @desc: gpio whose value will be assigned
1385 * @value: value to assign
1387 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1388 * account
1390 * This function should be called from contexts where we cannot sleep, and will
1391 * complain if the GPIO chip functions potentially sleep.
1393 void gpiod_set_value(struct gpio_desc *desc, int value)
1395 if (!desc)
1396 return;
1397 /* Should be using gpio_set_value_cansleep() */
1398 WARN_ON(desc->chip->can_sleep);
1399 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1400 value = !value;
1401 _gpiod_set_raw_value(desc, value);
1403 EXPORT_SYMBOL_GPL(gpiod_set_value);
1406 * gpiod_set_raw_array() - assign values to an array of GPIOs
1407 * @array_size: number of elements in the descriptor / value arrays
1408 * @desc_array: array of GPIO descriptors whose values will be assigned
1409 * @value_array: array of values to assign
1411 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1412 * without regard for their ACTIVE_LOW status.
1414 * This function should be called from contexts where we cannot sleep, and will
1415 * complain if the GPIO chip functions potentially sleep.
1417 void gpiod_set_raw_array(unsigned int array_size,
1418 struct gpio_desc **desc_array, int *value_array)
1420 if (!desc_array)
1421 return;
1422 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1424 EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1427 * gpiod_set_array() - assign values to an array of GPIOs
1428 * @array_size: number of elements in the descriptor / value arrays
1429 * @desc_array: array of GPIO descriptors whose values will be assigned
1430 * @value_array: array of values to assign
1432 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1433 * into account.
1435 * This function should be called from contexts where we cannot sleep, and will
1436 * complain if the GPIO chip functions potentially sleep.
1438 void gpiod_set_array(unsigned int array_size,
1439 struct gpio_desc **desc_array, int *value_array)
1441 if (!desc_array)
1442 return;
1443 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1445 EXPORT_SYMBOL_GPL(gpiod_set_array);
1448 * gpiod_cansleep() - report whether gpio value access may sleep
1449 * @desc: gpio to check
1452 int gpiod_cansleep(const struct gpio_desc *desc)
1454 if (!desc)
1455 return 0;
1456 return desc->chip->can_sleep;
1458 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1461 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1462 * @desc: gpio whose IRQ will be returned (already requested)
1464 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1465 * error.
1467 int gpiod_to_irq(const struct gpio_desc *desc)
1469 struct gpio_chip *chip;
1470 int offset;
1472 if (!desc)
1473 return -EINVAL;
1474 chip = desc->chip;
1475 offset = gpio_chip_hwgpio(desc);
1476 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1478 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1481 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1482 * @chip: the chip the GPIO to lock belongs to
1483 * @offset: the offset of the GPIO to lock as IRQ
1485 * This is used directly by GPIO drivers that want to lock down
1486 * a certain GPIO line to be used for IRQs.
1488 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1490 if (offset >= chip->ngpio)
1491 return -EINVAL;
1493 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1494 chip_err(chip,
1495 "%s: tried to flag a GPIO set as output for IRQ\n",
1496 __func__);
1497 return -EIO;
1500 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1501 return 0;
1503 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1506 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1507 * @chip: the chip the GPIO to lock belongs to
1508 * @offset: the offset of the GPIO to lock as IRQ
1510 * This is used directly by GPIO drivers that want to indicate
1511 * that a certain GPIO is no longer used exclusively for IRQ.
1513 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1515 if (offset >= chip->ngpio)
1516 return;
1518 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1520 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1523 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1524 * @desc: gpio whose value will be returned
1526 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1527 * its ACTIVE_LOW status.
1529 * This function is to be called from contexts that can sleep.
1531 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1533 might_sleep_if(extra_checks);
1534 if (!desc)
1535 return 0;
1536 return _gpiod_get_raw_value(desc);
1538 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1541 * gpiod_get_value_cansleep() - return a gpio's value
1542 * @desc: gpio whose value will be returned
1544 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1545 * account.
1547 * This function is to be called from contexts that can sleep.
1549 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1551 int value;
1553 might_sleep_if(extra_checks);
1554 if (!desc)
1555 return 0;
1557 value = _gpiod_get_raw_value(desc);
1558 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1559 value = !value;
1561 return value;
1563 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
1566 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1567 * @desc: gpio whose value will be assigned
1568 * @value: value to assign
1570 * Set the raw value of the GPIO, i.e. the value of its physical line without
1571 * regard for its ACTIVE_LOW status.
1573 * This function is to be called from contexts that can sleep.
1575 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
1577 might_sleep_if(extra_checks);
1578 if (!desc)
1579 return;
1580 _gpiod_set_raw_value(desc, value);
1582 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
1585 * gpiod_set_value_cansleep() - assign a gpio's value
1586 * @desc: gpio whose value will be assigned
1587 * @value: value to assign
1589 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1590 * account
1592 * This function is to be called from contexts that can sleep.
1594 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1596 might_sleep_if(extra_checks);
1597 if (!desc)
1598 return;
1600 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1601 value = !value;
1602 _gpiod_set_raw_value(desc, value);
1604 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1607 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1608 * @array_size: number of elements in the descriptor / value arrays
1609 * @desc_array: array of GPIO descriptors whose values will be assigned
1610 * @value_array: array of values to assign
1612 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1613 * without regard for their ACTIVE_LOW status.
1615 * This function is to be called from contexts that can sleep.
1617 void gpiod_set_raw_array_cansleep(unsigned int array_size,
1618 struct gpio_desc **desc_array,
1619 int *value_array)
1621 might_sleep_if(extra_checks);
1622 if (!desc_array)
1623 return;
1624 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1626 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1629 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1630 * @array_size: number of elements in the descriptor / value arrays
1631 * @desc_array: array of GPIO descriptors whose values will be assigned
1632 * @value_array: array of values to assign
1634 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1635 * into account.
1637 * This function is to be called from contexts that can sleep.
1639 void gpiod_set_array_cansleep(unsigned int array_size,
1640 struct gpio_desc **desc_array,
1641 int *value_array)
1643 might_sleep_if(extra_checks);
1644 if (!desc_array)
1645 return;
1646 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1648 EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1651 * gpiod_add_lookup_table() - register GPIO device consumers
1652 * @table: table of consumers to register
1654 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
1656 mutex_lock(&gpio_lookup_lock);
1658 list_add_tail(&table->list, &gpio_lookup_list);
1660 mutex_unlock(&gpio_lookup_lock);
1663 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
1664 unsigned int idx,
1665 enum gpio_lookup_flags *flags)
1667 char prop_name[32]; /* 32 is max size of property name */
1668 enum of_gpio_flags of_flags;
1669 struct gpio_desc *desc;
1670 unsigned int i;
1672 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1673 if (con_id)
1674 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
1675 gpio_suffixes[i]);
1676 else
1677 snprintf(prop_name, sizeof(prop_name), "%s",
1678 gpio_suffixes[i]);
1680 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1681 &of_flags);
1682 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1683 break;
1686 if (IS_ERR(desc))
1687 return desc;
1689 if (of_flags & OF_GPIO_ACTIVE_LOW)
1690 *flags |= GPIO_ACTIVE_LOW;
1692 return desc;
1695 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1696 unsigned int idx,
1697 enum gpio_lookup_flags *flags)
1699 struct acpi_device *adev = ACPI_COMPANION(dev);
1700 struct acpi_gpio_info info;
1701 struct gpio_desc *desc;
1702 char propname[32];
1703 int i;
1705 /* Try first from _DSD */
1706 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1707 if (con_id && strcmp(con_id, "gpios")) {
1708 snprintf(propname, sizeof(propname), "%s-%s",
1709 con_id, gpio_suffixes[i]);
1710 } else {
1711 snprintf(propname, sizeof(propname), "%s",
1712 gpio_suffixes[i]);
1715 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1716 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1717 break;
1720 /* Then from plain _CRS GPIOs */
1721 if (IS_ERR(desc)) {
1722 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1723 if (IS_ERR(desc))
1724 return desc;
1727 if (info.active_low)
1728 *flags |= GPIO_ACTIVE_LOW;
1730 return desc;
1733 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1735 const char *dev_id = dev ? dev_name(dev) : NULL;
1736 struct gpiod_lookup_table *table;
1738 mutex_lock(&gpio_lookup_lock);
1740 list_for_each_entry(table, &gpio_lookup_list, list) {
1741 if (table->dev_id && dev_id) {
1743 * Valid strings on both ends, must be identical to have
1744 * a match
1746 if (!strcmp(table->dev_id, dev_id))
1747 goto found;
1748 } else {
1750 * One of the pointers is NULL, so both must be to have
1751 * a match
1753 if (dev_id == table->dev_id)
1754 goto found;
1757 table = NULL;
1759 found:
1760 mutex_unlock(&gpio_lookup_lock);
1761 return table;
1764 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1765 unsigned int idx,
1766 enum gpio_lookup_flags *flags)
1768 struct gpio_desc *desc = ERR_PTR(-ENOENT);
1769 struct gpiod_lookup_table *table;
1770 struct gpiod_lookup *p;
1772 table = gpiod_find_lookup_table(dev);
1773 if (!table)
1774 return desc;
1776 for (p = &table->table[0]; p->chip_label; p++) {
1777 struct gpio_chip *chip;
1779 /* idx must always match exactly */
1780 if (p->idx != idx)
1781 continue;
1783 /* If the lookup entry has a con_id, require exact match */
1784 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1785 continue;
1787 chip = find_chip_by_name(p->chip_label);
1789 if (!chip) {
1790 dev_err(dev, "cannot find GPIO chip %s\n",
1791 p->chip_label);
1792 return ERR_PTR(-ENODEV);
1795 if (chip->ngpio <= p->chip_hwnum) {
1796 dev_err(dev,
1797 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1798 idx, chip->ngpio, chip->label);
1799 return ERR_PTR(-EINVAL);
1802 desc = gpiochip_get_desc(chip, p->chip_hwnum);
1803 *flags = p->flags;
1805 return desc;
1808 return desc;
1811 static int dt_gpio_count(struct device *dev, const char *con_id)
1813 int ret;
1814 char propname[32];
1815 unsigned int i;
1817 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1818 if (con_id)
1819 snprintf(propname, sizeof(propname), "%s-%s",
1820 con_id, gpio_suffixes[i]);
1821 else
1822 snprintf(propname, sizeof(propname), "%s",
1823 gpio_suffixes[i]);
1825 ret = of_gpio_named_count(dev->of_node, propname);
1826 if (ret >= 0)
1827 break;
1829 return ret;
1832 static int platform_gpio_count(struct device *dev, const char *con_id)
1834 struct gpiod_lookup_table *table;
1835 struct gpiod_lookup *p;
1836 unsigned int count = 0;
1838 table = gpiod_find_lookup_table(dev);
1839 if (!table)
1840 return -ENOENT;
1842 for (p = &table->table[0]; p->chip_label; p++) {
1843 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1844 (!con_id && !p->con_id))
1845 count++;
1847 if (!count)
1848 return -ENOENT;
1850 return count;
1854 * gpiod_count - return the number of GPIOs associated with a device / function
1855 * or -ENOENT if no GPIO has been assigned to the requested function
1856 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1857 * @con_id: function within the GPIO consumer
1859 int gpiod_count(struct device *dev, const char *con_id)
1861 int count = -ENOENT;
1863 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
1864 count = dt_gpio_count(dev, con_id);
1865 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
1866 count = acpi_gpio_count(dev, con_id);
1868 if (count < 0)
1869 count = platform_gpio_count(dev, con_id);
1871 return count;
1873 EXPORT_SYMBOL_GPL(gpiod_count);
1876 * gpiod_get - obtain a GPIO for a given GPIO function
1877 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1878 * @con_id: function within the GPIO consumer
1879 * @flags: optional GPIO initialization flags
1881 * Return the GPIO descriptor corresponding to the function con_id of device
1882 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1883 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
1885 struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1886 enum gpiod_flags flags)
1888 return gpiod_get_index(dev, con_id, 0, flags);
1890 EXPORT_SYMBOL_GPL(__gpiod_get);
1893 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1894 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1895 * @con_id: function within the GPIO consumer
1896 * @flags: optional GPIO initialization flags
1898 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1899 * the requested function it will return NULL. This is convenient for drivers
1900 * that need to handle optional GPIOs.
1902 struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1903 const char *con_id,
1904 enum gpiod_flags flags)
1906 return gpiod_get_index_optional(dev, con_id, 0, flags);
1908 EXPORT_SYMBOL_GPL(__gpiod_get_optional);
1912 * gpiod_configure_flags - helper function to configure a given GPIO
1913 * @desc: gpio whose value will be assigned
1914 * @con_id: function within the GPIO consumer
1915 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
1916 * of_get_gpio_hog()
1917 * @dflags: gpiod_flags - optional GPIO initialization flags
1919 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
1920 * requested function and/or index, or another IS_ERR() code if an error
1921 * occurred while trying to acquire the GPIO.
1923 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
1924 unsigned long lflags, enum gpiod_flags dflags)
1926 int status;
1928 if (lflags & GPIO_ACTIVE_LOW)
1929 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1930 if (lflags & GPIO_OPEN_DRAIN)
1931 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1932 if (lflags & GPIO_OPEN_SOURCE)
1933 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1935 /* No particular flag request, return here... */
1936 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
1937 pr_debug("no flags found for %s\n", con_id);
1938 return 0;
1941 /* Process flags */
1942 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
1943 status = gpiod_direction_output(desc,
1944 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
1945 else
1946 status = gpiod_direction_input(desc);
1948 return status;
1952 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
1953 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1954 * @con_id: function within the GPIO consumer
1955 * @idx: index of the GPIO to obtain in the consumer
1956 * @flags: optional GPIO initialization flags
1958 * This variant of gpiod_get() allows to access GPIOs other than the first
1959 * defined one for functions that define several GPIOs.
1961 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1962 * requested function and/or index, or another IS_ERR() code if an error
1963 * occured while trying to acquire the GPIO.
1965 struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
1966 const char *con_id,
1967 unsigned int idx,
1968 enum gpiod_flags flags)
1970 struct gpio_desc *desc = NULL;
1971 int status;
1972 enum gpio_lookup_flags lookupflags = 0;
1974 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1976 if (dev) {
1977 /* Using device tree? */
1978 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1979 dev_dbg(dev, "using device tree for GPIO lookup\n");
1980 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
1981 } else if (ACPI_COMPANION(dev)) {
1982 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1983 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
1988 * Either we are not using DT or ACPI, or their lookup did not return
1989 * a result. In that case, use platform lookup as a fallback.
1991 if (!desc || desc == ERR_PTR(-ENOENT)) {
1992 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
1993 desc = gpiod_find(dev, con_id, idx, &lookupflags);
1996 if (IS_ERR(desc)) {
1997 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
1998 return desc;
2001 status = gpiod_request(desc, con_id);
2002 if (status < 0)
2003 return ERR_PTR(status);
2005 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
2006 if (status < 0) {
2007 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2008 gpiod_put(desc);
2009 return ERR_PTR(status);
2012 return desc;
2014 EXPORT_SYMBOL_GPL(__gpiod_get_index);
2017 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2018 * @fwnode: handle of the firmware node
2019 * @propname: name of the firmware property representing the GPIO
2021 * This function can be used for drivers that get their configuration
2022 * from firmware.
2024 * Function properly finds the corresponding GPIO using whatever is the
2025 * underlying firmware interface and then makes sure that the GPIO
2026 * descriptor is requested before it is returned to the caller.
2028 * In case of error an ERR_PTR() is returned.
2030 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2031 const char *propname)
2033 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2034 bool active_low = false;
2035 int ret;
2037 if (!fwnode)
2038 return ERR_PTR(-EINVAL);
2040 if (is_of_node(fwnode)) {
2041 enum of_gpio_flags flags;
2043 desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
2044 &flags);
2045 if (!IS_ERR(desc))
2046 active_low = flags & OF_GPIO_ACTIVE_LOW;
2047 } else if (is_acpi_node(fwnode)) {
2048 struct acpi_gpio_info info;
2050 desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
2051 &info);
2052 if (!IS_ERR(desc))
2053 active_low = info.active_low;
2056 if (IS_ERR(desc))
2057 return desc;
2059 ret = gpiod_request(desc, NULL);
2060 if (ret)
2061 return ERR_PTR(ret);
2063 /* Only value flag can be set from both DT and ACPI is active_low */
2064 if (active_low)
2065 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2067 return desc;
2069 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2072 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2073 * function
2074 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2075 * @con_id: function within the GPIO consumer
2076 * @index: index of the GPIO to obtain in the consumer
2077 * @flags: optional GPIO initialization flags
2079 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2080 * specified index was assigned to the requested function it will return NULL.
2081 * This is convenient for drivers that need to handle optional GPIOs.
2083 struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
2084 const char *con_id,
2085 unsigned int index,
2086 enum gpiod_flags flags)
2088 struct gpio_desc *desc;
2090 desc = gpiod_get_index(dev, con_id, index, flags);
2091 if (IS_ERR(desc)) {
2092 if (PTR_ERR(desc) == -ENOENT)
2093 return NULL;
2096 return desc;
2098 EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
2101 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2102 * @desc: gpio whose value will be assigned
2103 * @name: gpio line name
2104 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2105 * of_get_gpio_hog()
2106 * @dflags: gpiod_flags - optional GPIO initialization flags
2108 int gpiod_hog(struct gpio_desc *desc, const char *name,
2109 unsigned long lflags, enum gpiod_flags dflags)
2111 struct gpio_chip *chip;
2112 struct gpio_desc *local_desc;
2113 int hwnum;
2114 int status;
2116 chip = gpiod_to_chip(desc);
2117 hwnum = gpio_chip_hwgpio(desc);
2119 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2120 if (IS_ERR(local_desc)) {
2121 pr_debug("requesting own GPIO %s failed\n", name);
2122 return PTR_ERR(local_desc);
2125 status = gpiod_configure_flags(desc, name, lflags, dflags);
2126 if (status < 0) {
2127 pr_debug("setup of GPIO %s failed\n", name);
2128 gpiochip_free_own_desc(desc);
2129 return status;
2132 /* Mark GPIO as hogged so it can be identified and removed later */
2133 set_bit(FLAG_IS_HOGGED, &desc->flags);
2135 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2136 desc_to_gpio(desc), name,
2137 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2138 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2139 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2141 return 0;
2145 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2146 * @chip: gpio chip to act on
2148 * This is only used by of_gpiochip_remove to free hogged gpios
2150 static void gpiochip_free_hogs(struct gpio_chip *chip)
2152 int id;
2154 for (id = 0; id < chip->ngpio; id++) {
2155 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2156 gpiochip_free_own_desc(&chip->desc[id]);
2161 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2162 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2163 * @con_id: function within the GPIO consumer
2164 * @flags: optional GPIO initialization flags
2166 * This function acquires all the GPIOs defined under a given function.
2168 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2169 * no GPIO has been assigned to the requested function, or another IS_ERR()
2170 * code if an error occurred while trying to acquire the GPIOs.
2172 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2173 const char *con_id,
2174 enum gpiod_flags flags)
2176 struct gpio_desc *desc;
2177 struct gpio_descs *descs;
2178 int count;
2180 count = gpiod_count(dev, con_id);
2181 if (count < 0)
2182 return ERR_PTR(count);
2184 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2185 GFP_KERNEL);
2186 if (!descs)
2187 return ERR_PTR(-ENOMEM);
2189 for (descs->ndescs = 0; descs->ndescs < count; ) {
2190 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2191 if (IS_ERR(desc)) {
2192 gpiod_put_array(descs);
2193 return ERR_CAST(desc);
2195 descs->desc[descs->ndescs] = desc;
2196 descs->ndescs++;
2198 return descs;
2200 EXPORT_SYMBOL_GPL(gpiod_get_array);
2203 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2204 * function
2205 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2206 * @con_id: function within the GPIO consumer
2207 * @flags: optional GPIO initialization flags
2209 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2210 * assigned to the requested function it will return NULL.
2212 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2213 const char *con_id,
2214 enum gpiod_flags flags)
2216 struct gpio_descs *descs;
2218 descs = gpiod_get_array(dev, con_id, flags);
2219 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2220 return NULL;
2222 return descs;
2224 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2227 * gpiod_put - dispose of a GPIO descriptor
2228 * @desc: GPIO descriptor to dispose of
2230 * No descriptor can be used after gpiod_put() has been called on it.
2232 void gpiod_put(struct gpio_desc *desc)
2234 gpiod_free(desc);
2236 EXPORT_SYMBOL_GPL(gpiod_put);
2239 * gpiod_put_array - dispose of multiple GPIO descriptors
2240 * @descs: struct gpio_descs containing an array of descriptors
2242 void gpiod_put_array(struct gpio_descs *descs)
2244 unsigned int i;
2246 for (i = 0; i < descs->ndescs; i++)
2247 gpiod_put(descs->desc[i]);
2249 kfree(descs);
2251 EXPORT_SYMBOL_GPL(gpiod_put_array);
2253 #ifdef CONFIG_DEBUG_FS
2255 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2257 unsigned i;
2258 unsigned gpio = chip->base;
2259 struct gpio_desc *gdesc = &chip->desc[0];
2260 int is_out;
2261 int is_irq;
2263 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2264 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2265 continue;
2267 gpiod_get_direction(gdesc);
2268 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2269 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2270 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
2271 gpio, gdesc->label,
2272 is_out ? "out" : "in ",
2273 chip->get
2274 ? (chip->get(chip, i) ? "hi" : "lo")
2275 : "? ",
2276 is_irq ? "IRQ" : " ");
2277 seq_printf(s, "\n");
2281 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2283 unsigned long flags;
2284 struct gpio_chip *chip = NULL;
2285 loff_t index = *pos;
2287 s->private = "";
2289 spin_lock_irqsave(&gpio_lock, flags);
2290 list_for_each_entry(chip, &gpio_chips, list)
2291 if (index-- == 0) {
2292 spin_unlock_irqrestore(&gpio_lock, flags);
2293 return chip;
2295 spin_unlock_irqrestore(&gpio_lock, flags);
2297 return NULL;
2300 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2302 unsigned long flags;
2303 struct gpio_chip *chip = v;
2304 void *ret = NULL;
2306 spin_lock_irqsave(&gpio_lock, flags);
2307 if (list_is_last(&chip->list, &gpio_chips))
2308 ret = NULL;
2309 else
2310 ret = list_entry(chip->list.next, struct gpio_chip, list);
2311 spin_unlock_irqrestore(&gpio_lock, flags);
2313 s->private = "\n";
2314 ++*pos;
2316 return ret;
2319 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2323 static int gpiolib_seq_show(struct seq_file *s, void *v)
2325 struct gpio_chip *chip = v;
2326 struct device *dev;
2328 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2329 chip->base, chip->base + chip->ngpio - 1);
2330 dev = chip->dev;
2331 if (dev)
2332 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2333 dev_name(dev));
2334 if (chip->label)
2335 seq_printf(s, ", %s", chip->label);
2336 if (chip->can_sleep)
2337 seq_printf(s, ", can sleep");
2338 seq_printf(s, ":\n");
2340 if (chip->dbg_show)
2341 chip->dbg_show(s, chip);
2342 else
2343 gpiolib_dbg_show(s, chip);
2345 return 0;
2348 static const struct seq_operations gpiolib_seq_ops = {
2349 .start = gpiolib_seq_start,
2350 .next = gpiolib_seq_next,
2351 .stop = gpiolib_seq_stop,
2352 .show = gpiolib_seq_show,
2355 static int gpiolib_open(struct inode *inode, struct file *file)
2357 return seq_open(file, &gpiolib_seq_ops);
2360 static const struct file_operations gpiolib_operations = {
2361 .owner = THIS_MODULE,
2362 .open = gpiolib_open,
2363 .read = seq_read,
2364 .llseek = seq_lseek,
2365 .release = seq_release,
2368 static int __init gpiolib_debugfs_init(void)
2370 /* /sys/kernel/debug/gpio */
2371 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2372 NULL, NULL, &gpiolib_operations);
2373 return 0;
2375 subsys_initcall(gpiolib_debugfs_init);
2377 #endif /* DEBUG_FS */