1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/idr.h>
20 #include <linux/cdev.h>
22 #include <linux/uaccess.h>
23 #include <uapi/linux/gpio.h>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/gpio.h>
30 /* Implementation infrastructure for GPIO interfaces.
32 * The GPIO programming interface allows for inlining speed-critical
33 * get/set operations for common cases, so that access to SOC-integrated
34 * GPIOs can sometimes cost only an instruction or two per bit.
38 /* When debugging, extend minimal trust to callers and platform code.
39 * Also emit diagnostic messages that may help initial bringup, when
40 * board setup or driver bugs are most common.
42 * Otherwise, minimize overhead in what may be bitbanging codepaths.
45 #define extra_checks 1
47 #define extra_checks 0
50 /* Device and char device-related information */
51 static DEFINE_IDA(gpio_ida);
52 static dev_t gpio_devt;
53 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
54 static struct bus_type gpio_bus_type = {
58 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
59 * While any GPIO is requested, its gpio_chip is not removable;
60 * each GPIO's "requested" flag serves as a lock and refcount.
62 DEFINE_SPINLOCK(gpio_lock);
64 static DEFINE_MUTEX(gpio_lookup_lock);
65 static LIST_HEAD(gpio_lookup_list);
66 LIST_HEAD(gpio_devices);
68 static void gpiochip_free_hogs(struct gpio_chip *chip);
69 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
72 static inline void desc_set_label(struct gpio_desc *d, const char *label)
78 * Convert a GPIO number to its descriptor
80 struct gpio_desc *gpio_to_desc(unsigned gpio)
82 struct gpio_device *gdev;
85 spin_lock_irqsave(&gpio_lock, flags);
87 list_for_each_entry(gdev, &gpio_devices, list) {
88 if (gdev->base <= gpio &&
89 gdev->base + gdev->ngpio > gpio) {
90 spin_unlock_irqrestore(&gpio_lock, flags);
91 return &gdev->descs[gpio - gdev->base];
95 spin_unlock_irqrestore(&gpio_lock, flags);
97 if (!gpio_is_valid(gpio))
98 WARN(1, "invalid GPIO %d\n", gpio);
102 EXPORT_SYMBOL_GPL(gpio_to_desc);
105 * Get the GPIO descriptor corresponding to the given hw number for this chip.
107 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
110 struct gpio_device *gdev = chip->gpiodev;
112 if (hwnum >= gdev->ngpio)
113 return ERR_PTR(-EINVAL);
115 return &gdev->descs[hwnum];
119 * Convert a GPIO descriptor to the integer namespace.
120 * This should disappear in the future but is needed since we still
121 * use GPIO numbers for error messages and sysfs nodes
123 int desc_to_gpio(const struct gpio_desc *desc)
125 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
127 EXPORT_SYMBOL_GPL(desc_to_gpio);
131 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
132 * @desc: descriptor to return the chip of
134 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
136 if (!desc || !desc->gdev || !desc->gdev->chip)
138 return desc->gdev->chip;
140 EXPORT_SYMBOL_GPL(gpiod_to_chip);
142 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
143 static int gpiochip_find_base(int ngpio)
145 struct gpio_device *gdev;
146 int base = ARCH_NR_GPIOS - ngpio;
148 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
149 /* found a free space? */
150 if (gdev->base + gdev->ngpio <= base)
153 /* nope, check the space right before the chip */
154 base = gdev->base - ngpio;
157 if (gpio_is_valid(base)) {
158 pr_debug("%s: found new base at %d\n", __func__, base);
161 pr_err("%s: cannot find free range\n", __func__);
167 * gpiod_get_direction - return the current direction of a GPIO
168 * @desc: GPIO to get the direction of
170 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
172 * This function may sleep if gpiod_cansleep() is true.
174 int gpiod_get_direction(struct gpio_desc *desc)
176 struct gpio_chip *chip;
178 int status = -EINVAL;
180 chip = gpiod_to_chip(desc);
181 offset = gpio_chip_hwgpio(desc);
183 if (!chip->get_direction)
186 status = chip->get_direction(chip, offset);
188 /* GPIOF_DIR_IN, or other positive */
190 clear_bit(FLAG_IS_OUT, &desc->flags);
194 set_bit(FLAG_IS_OUT, &desc->flags);
198 EXPORT_SYMBOL_GPL(gpiod_get_direction);
201 * Add a new chip to the global chips list, keeping the list of chips sorted
202 * by range(means [base, base + ngpio - 1]) order.
204 * Return -EBUSY if the new chip overlaps with some other chip's integer
207 static int gpiodev_add_to_list(struct gpio_device *gdev)
209 struct gpio_device *iterator;
210 struct gpio_device *previous = NULL;
215 if (list_empty(&gpio_devices)) {
216 list_add_tail(&gdev->list, &gpio_devices);
220 list_for_each_entry(iterator, &gpio_devices, list) {
221 if (iterator->base >= gdev->base + gdev->ngpio) {
223 * Iterator is the first GPIO chip so there is no
230 * We found a valid range(means
231 * [base, base + ngpio - 1]) between previous
234 if (previous->base + previous->ngpio
243 * We are beyond the last chip in the list and iterator now
244 * points to the head.
245 * Let iterator point to the last chip in the list.
248 iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
249 if (iterator->base + iterator->ngpio <= gdev->base) {
250 list_add(&gdev->list, &iterator->list);
255 "GPIO integer space overlap, cannot add chip\n");
259 list_add_tail(&gdev->list, &iterator->list);
264 * Convert a GPIO name to its descriptor
266 static struct gpio_desc *gpio_name_to_desc(const char * const name)
268 struct gpio_device *gdev;
271 spin_lock_irqsave(&gpio_lock, flags);
273 list_for_each_entry(gdev, &gpio_devices, list) {
276 for (i = 0; i != gdev->ngpio; ++i) {
277 struct gpio_desc *desc = &gdev->descs[i];
279 if (!desc->name || !name)
282 if (!strcmp(desc->name, name)) {
283 spin_unlock_irqrestore(&gpio_lock, flags);
289 spin_unlock_irqrestore(&gpio_lock, flags);
295 * Takes the names from gc->names and checks if they are all unique. If they
296 * are, they are assigned to their gpio descriptors.
298 * Warning if one of the names is already used for a different GPIO.
300 static int gpiochip_set_desc_names(struct gpio_chip *gc)
302 struct gpio_device *gdev = gc->gpiodev;
308 /* First check all names if they are unique */
309 for (i = 0; i != gc->ngpio; ++i) {
310 struct gpio_desc *gpio;
312 gpio = gpio_name_to_desc(gc->names[i]);
315 "Detected name collision for GPIO name '%s'\n",
319 /* Then add all names to the GPIO descriptors */
320 for (i = 0; i != gc->ngpio; ++i)
321 gdev->descs[i].name = gc->names[i];
327 * gpio_ioctl() - ioctl handler for the GPIO chardev
329 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
331 struct gpio_device *gdev = filp->private_data;
332 struct gpio_chip *chip = gdev->chip;
333 int __user *ip = (int __user *)arg;
335 /* We fail any subsequent ioctl():s when the chip is gone */
339 /* Fill in the struct and pass to userspace */
340 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
341 struct gpiochip_info chipinfo;
343 strncpy(chipinfo.name, dev_name(&gdev->dev),
344 sizeof(chipinfo.name));
345 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
346 strncpy(chipinfo.label, gdev->label,
347 sizeof(chipinfo.label));
348 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
349 chipinfo.lines = gdev->ngpio;
350 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
353 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
354 struct gpioline_info lineinfo;
355 struct gpio_desc *desc;
357 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
359 if (lineinfo.line_offset > gdev->ngpio)
362 desc = &gdev->descs[lineinfo.line_offset];
364 strncpy(lineinfo.name, desc->name,
365 sizeof(lineinfo.name));
366 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
368 lineinfo.name[0] = '\0';
371 strncpy(lineinfo.label, desc->label,
372 sizeof(lineinfo.label));
373 lineinfo.label[sizeof(lineinfo.label)-1] = '\0';
375 lineinfo.label[0] = '\0';
379 * Userspace only need to know that the kernel is using
380 * this GPIO so it can't use it.
383 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
384 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
385 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
386 test_bit(FLAG_EXPORT, &desc->flags) ||
387 test_bit(FLAG_SYSFS, &desc->flags))
388 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
389 if (test_bit(FLAG_IS_OUT, &desc->flags))
390 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
391 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
392 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
393 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
394 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
395 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
396 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
398 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
406 * gpio_chrdev_open() - open the chardev for ioctl operations
407 * @inode: inode for this chardev
408 * @filp: file struct for storing private data
409 * Returns 0 on success
411 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
413 struct gpio_device *gdev = container_of(inode->i_cdev,
414 struct gpio_device, chrdev);
416 /* Fail on open if the backing gpiochip is gone */
417 if (!gdev || !gdev->chip)
419 get_device(&gdev->dev);
420 filp->private_data = gdev;
425 * gpio_chrdev_release() - close chardev after ioctl operations
426 * @inode: inode for this chardev
427 * @filp: file struct for storing private data
428 * Returns 0 on success
430 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
432 struct gpio_device *gdev = container_of(inode->i_cdev,
433 struct gpio_device, chrdev);
437 put_device(&gdev->dev);
442 static const struct file_operations gpio_fileops = {
443 .release = gpio_chrdev_release,
444 .open = gpio_chrdev_open,
445 .owner = THIS_MODULE,
446 .llseek = noop_llseek,
447 .unlocked_ioctl = gpio_ioctl,
448 .compat_ioctl = gpio_ioctl,
451 static void gpiodevice_release(struct device *dev)
453 struct gpio_device *gdev = dev_get_drvdata(dev);
455 cdev_del(&gdev->chrdev);
456 list_del(&gdev->list);
457 ida_simple_remove(&gpio_ida, gdev->id);
462 * gpiochip_add_data() - register a gpio_chip
463 * @chip: the chip to register, with chip->base initialized
464 * Context: potentially before irqs will work
466 * Returns a negative errno if the chip can't be registered, such as
467 * because the chip->base is invalid or already associated with a
468 * different chip. Otherwise it returns zero as a success code.
470 * When gpiochip_add_data() is called very early during boot, so that GPIOs
471 * can be freely used, the chip->parent device must be registered before
472 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
473 * for GPIOs will fail rudely.
475 * If chip->base is negative, this requests dynamic assignment of
476 * a range of valid GPIOs.
478 int gpiochip_add_data(struct gpio_chip *chip, void *data)
483 int base = chip->base;
484 struct gpio_device *gdev;
487 * First: allocate and populate the internal stat container, and
488 * set up the struct device.
490 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
493 gdev->dev.bus = &gpio_bus_type;
495 chip->gpiodev = gdev;
497 gdev->dev.parent = chip->parent;
498 gdev->dev.of_node = chip->parent->of_node;
500 #ifdef CONFIG_OF_GPIO
501 /* If the gpiochip has an assigned OF node this takes precedence */
503 gdev->dev.of_node = chip->of_node;
506 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
511 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
512 device_initialize(&gdev->dev);
513 dev_set_drvdata(&gdev->dev, gdev);
514 if (chip->parent && chip->parent->driver)
515 gdev->owner = chip->parent->driver->owner;
516 else if (chip->owner)
517 /* TODO: remove chip->owner */
518 gdev->owner = chip->owner;
520 gdev->owner = THIS_MODULE;
522 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
523 sizeof(gdev->descs[0]), GFP_KERNEL);
529 if (chip->ngpio == 0) {
530 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
536 gdev->label = devm_kstrdup(&gdev->dev, chip->label, GFP_KERNEL);
538 gdev->label = devm_kstrdup(&gdev->dev, "unknown", GFP_KERNEL);
544 gdev->ngpio = chip->ngpio;
547 spin_lock_irqsave(&gpio_lock, flags);
550 * TODO: this allocates a Linux GPIO number base in the global
551 * GPIO numberspace for this chip. In the long run we want to
552 * get *rid* of this numberspace and use only descriptors, but
553 * it may be a pipe dream. It will not happen before we get rid
554 * of the sysfs interface anyways.
557 base = gpiochip_find_base(chip->ngpio);
560 spin_unlock_irqrestore(&gpio_lock, flags);
564 * TODO: it should not be necessary to reflect the assigned
565 * base outside of the GPIO subsystem. Go over drivers and
566 * see if anyone makes use of this, else drop this and assign
573 status = gpiodev_add_to_list(gdev);
575 spin_unlock_irqrestore(&gpio_lock, flags);
579 for (i = 0; i < chip->ngpio; i++) {
580 struct gpio_desc *desc = &gdev->descs[i];
584 /* REVISIT: most hardware initializes GPIOs as inputs (often
585 * with pullups enabled) so power usage is minimized. Linux
586 * code should set the gpio direction first thing; but until
587 * it does, and in case chip->get_direction is not set, we may
588 * expose the wrong direction in sysfs.
590 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
593 spin_unlock_irqrestore(&gpio_lock, flags);
595 #ifdef CONFIG_PINCTRL
596 INIT_LIST_HEAD(&gdev->pin_ranges);
599 status = gpiochip_set_desc_names(chip);
601 goto err_remove_from_list;
603 status = of_gpiochip_add(chip);
605 goto err_remove_chip;
607 acpi_gpiochip_add(chip);
610 * By first adding the chardev, and then adding the device,
611 * we get a device node entry in sysfs under
612 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
613 * coldplug of device nodes and other udev business.
615 cdev_init(&gdev->chrdev, &gpio_fileops);
616 gdev->chrdev.owner = THIS_MODULE;
617 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
618 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
619 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
621 chip_warn(chip, "failed to add char device %d:%d\n",
622 MAJOR(gpio_devt), gdev->id);
624 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
625 MAJOR(gpio_devt), gdev->id);
626 status = device_add(&gdev->dev);
628 goto err_remove_chardev;
630 status = gpiochip_sysfs_register(gdev);
632 goto err_remove_device;
634 /* From this point, the .release() function cleans up gpio_device */
635 gdev->dev.release = gpiodevice_release;
636 get_device(&gdev->dev);
637 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
638 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
639 dev_name(&gdev->dev), chip->label ? : "generic");
644 device_del(&gdev->dev);
646 cdev_del(&gdev->chrdev);
648 acpi_gpiochip_remove(chip);
649 gpiochip_free_hogs(chip);
650 of_gpiochip_remove(chip);
651 err_remove_from_list:
652 spin_lock_irqsave(&gpio_lock, flags);
653 list_del(&gdev->list);
654 spin_unlock_irqrestore(&gpio_lock, flags);
656 ida_simple_remove(&gpio_ida, gdev->id);
657 /* failures here can mean systems won't boot... */
658 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
659 gdev->base, gdev->base + gdev->ngpio - 1,
660 chip->label ? : "generic");
664 EXPORT_SYMBOL_GPL(gpiochip_add_data);
667 * gpiochip_get_data() - get per-subdriver data for the chip
669 void *gpiochip_get_data(struct gpio_chip *chip)
671 return chip->gpiodev->data;
673 EXPORT_SYMBOL_GPL(gpiochip_get_data);
676 * gpiochip_remove() - unregister a gpio_chip
677 * @chip: the chip to unregister
679 * A gpio_chip with any GPIOs still requested may not be removed.
681 void gpiochip_remove(struct gpio_chip *chip)
683 struct gpio_device *gdev = chip->gpiodev;
684 struct gpio_desc *desc;
687 bool requested = false;
689 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
690 gpiochip_sysfs_unregister(gdev);
691 /* Numb the device, cancelling all outstanding operations */
693 gpiochip_irqchip_remove(chip);
694 acpi_gpiochip_remove(chip);
695 gpiochip_remove_pin_ranges(chip);
696 gpiochip_free_hogs(chip);
697 of_gpiochip_remove(chip);
699 * We accept no more calls into the driver from this point, so
700 * NULL the driver data pointer
704 spin_lock_irqsave(&gpio_lock, flags);
705 for (i = 0; i < gdev->ngpio; i++) {
706 desc = &gdev->descs[i];
707 if (test_bit(FLAG_REQUESTED, &desc->flags))
710 spin_unlock_irqrestore(&gpio_lock, flags);
714 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
717 * The gpiochip side puts its use of the device to rest here:
718 * if there are no userspace clients, the chardev and device will
719 * be removed, else it will be dangling until the last user is
722 put_device(&gdev->dev);
724 EXPORT_SYMBOL_GPL(gpiochip_remove);
726 static void devm_gpio_chip_release(struct device *dev, void *res)
728 struct gpio_chip *chip = *(struct gpio_chip **)res;
730 gpiochip_remove(chip);
733 static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
736 struct gpio_chip **r = res;
747 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
748 * @dev: the device pointer on which irq_chip belongs to.
749 * @chip: the chip to register, with chip->base initialized
750 * Context: potentially before irqs will work
752 * Returns a negative errno if the chip can't be registered, such as
753 * because the chip->base is invalid or already associated with a
754 * different chip. Otherwise it returns zero as a success code.
756 * The gpio chip automatically be released when the device is unbound.
758 int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
761 struct gpio_chip **ptr;
764 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
769 ret = gpiochip_add_data(chip, data);
776 devres_add(dev, ptr);
780 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
783 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
784 * @dev: device for which which resource was allocated
785 * @chip: the chip to remove
787 * A gpio_chip with any GPIOs still requested may not be removed.
789 void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
793 ret = devres_release(dev, devm_gpio_chip_release,
794 devm_gpio_chip_match, chip);
798 EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
801 * gpiochip_find() - iterator for locating a specific gpio_chip
802 * @data: data to pass to match function
803 * @callback: Callback function to check gpio_chip
805 * Similar to bus_find_device. It returns a reference to a gpio_chip as
806 * determined by a user supplied @match callback. The callback should return
807 * 0 if the device doesn't match and non-zero if it does. If the callback is
808 * non-zero, this function will return to the caller and not iterate over any
811 struct gpio_chip *gpiochip_find(void *data,
812 int (*match)(struct gpio_chip *chip,
815 struct gpio_device *gdev;
816 struct gpio_chip *chip;
819 spin_lock_irqsave(&gpio_lock, flags);
820 list_for_each_entry(gdev, &gpio_devices, list)
821 if (match(gdev->chip, data))
825 if (&gdev->list == &gpio_devices)
830 spin_unlock_irqrestore(&gpio_lock, flags);
834 EXPORT_SYMBOL_GPL(gpiochip_find);
836 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
838 const char *name = data;
840 return !strcmp(chip->label, name);
843 static struct gpio_chip *find_chip_by_name(const char *name)
845 return gpiochip_find((void *)name, gpiochip_match_name);
848 #ifdef CONFIG_GPIOLIB_IRQCHIP
851 * The following is irqchip helper code for gpiochips.
855 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
856 * @gpiochip: the gpiochip to set the irqchip chain to
857 * @irqchip: the irqchip to chain to the gpiochip
858 * @parent_irq: the irq number corresponding to the parent IRQ for this
860 * @parent_handler: the parent interrupt handler for the accumulated IRQ
861 * coming out of the gpiochip. If the interrupt is nested rather than
862 * cascaded, pass NULL in this handler argument
864 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
865 struct irq_chip *irqchip,
867 irq_flow_handler_t parent_handler)
871 if (!gpiochip->irqdomain) {
872 chip_err(gpiochip, "called %s before setting up irqchip\n",
877 if (parent_handler) {
878 if (gpiochip->can_sleep) {
880 "you cannot have chained interrupts on a "
881 "chip that may sleep\n");
885 * The parent irqchip is already using the chip_data for this
886 * irqchip, so our callbacks simply use the handler_data.
888 irq_set_chained_handler_and_data(parent_irq, parent_handler,
891 gpiochip->irq_parent = parent_irq;
894 /* Set the parent IRQ for all affected IRQs */
895 for (offset = 0; offset < gpiochip->ngpio; offset++)
896 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
899 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
902 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
903 * @d: the irqdomain used by this irqchip
904 * @irq: the global irq number used by this GPIO irqchip irq
905 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
907 * This function will set up the mapping for a certain IRQ line on a
908 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
909 * stored inside the gpiochip.
911 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
912 irq_hw_number_t hwirq)
914 struct gpio_chip *chip = d->host_data;
916 irq_set_chip_data(irq, chip);
918 * This lock class tells lockdep that GPIO irqs are in a different
919 * category than their parents, so it won't report false recursion.
921 irq_set_lockdep_class(irq, chip->lock_key);
922 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
923 /* Chips that can sleep need nested thread handlers */
924 if (chip->can_sleep && !chip->irq_not_threaded)
925 irq_set_nested_thread(irq, 1);
926 irq_set_noprobe(irq);
929 * No set-up of the hardware will happen if IRQ_TYPE_NONE
930 * is passed as default type.
932 if (chip->irq_default_type != IRQ_TYPE_NONE)
933 irq_set_irq_type(irq, chip->irq_default_type);
938 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
940 struct gpio_chip *chip = d->host_data;
943 irq_set_nested_thread(irq, 0);
944 irq_set_chip_and_handler(irq, NULL, NULL);
945 irq_set_chip_data(irq, NULL);
948 static const struct irq_domain_ops gpiochip_domain_ops = {
949 .map = gpiochip_irq_map,
950 .unmap = gpiochip_irq_unmap,
951 /* Virtually all GPIO irqchips are twocell:ed */
952 .xlate = irq_domain_xlate_twocell,
955 static int gpiochip_irq_reqres(struct irq_data *d)
957 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
959 if (!try_module_get(chip->gpiodev->owner))
962 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
964 "unable to lock HW IRQ %lu for IRQ\n",
966 module_put(chip->gpiodev->owner);
972 static void gpiochip_irq_relres(struct irq_data *d)
974 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
976 gpiochip_unlock_as_irq(chip, d->hwirq);
977 module_put(chip->gpiodev->owner);
980 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
982 return irq_find_mapping(chip->irqdomain, offset);
986 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
987 * @gpiochip: the gpiochip to remove the irqchip from
989 * This is called only from gpiochip_remove()
991 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
995 acpi_gpiochip_free_interrupts(gpiochip);
997 if (gpiochip->irq_parent) {
998 irq_set_chained_handler(gpiochip->irq_parent, NULL);
999 irq_set_handler_data(gpiochip->irq_parent, NULL);
1002 /* Remove all IRQ mappings and delete the domain */
1003 if (gpiochip->irqdomain) {
1004 for (offset = 0; offset < gpiochip->ngpio; offset++)
1005 irq_dispose_mapping(
1006 irq_find_mapping(gpiochip->irqdomain, offset));
1007 irq_domain_remove(gpiochip->irqdomain);
1010 if (gpiochip->irqchip) {
1011 gpiochip->irqchip->irq_request_resources = NULL;
1012 gpiochip->irqchip->irq_release_resources = NULL;
1013 gpiochip->irqchip = NULL;
1018 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1019 * @gpiochip: the gpiochip to add the irqchip to
1020 * @irqchip: the irqchip to add to the gpiochip
1021 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1022 * allocate gpiochip irqs from
1023 * @handler: the irq handler to use (often a predefined irq core function)
1024 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1025 * to have the core avoid setting up any default type in the hardware.
1026 * @lock_key: lockdep class
1028 * This function closely associates a certain irqchip with a certain
1029 * gpiochip, providing an irq domain to translate the local IRQs to
1030 * global irqs in the gpiolib core, and making sure that the gpiochip
1031 * is passed as chip data to all related functions. Driver callbacks
1032 * need to use gpiochip_get_data() to get their local state containers back
1033 * from the gpiochip passed as chip data. An irqdomain will be stored
1034 * in the gpiochip that shall be used by the driver to handle IRQ number
1035 * translation. The gpiochip will need to be initialized and registered
1036 * before calling this function.
1038 * This function will handle two cell:ed simple IRQs and assumes all
1039 * the pins on the gpiochip can generate a unique IRQ. Everything else
1040 * need to be open coded.
1042 int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1043 struct irq_chip *irqchip,
1044 unsigned int first_irq,
1045 irq_flow_handler_t handler,
1047 struct lock_class_key *lock_key)
1049 struct device_node *of_node;
1050 unsigned int offset;
1051 unsigned irq_base = 0;
1053 if (!gpiochip || !irqchip)
1056 if (!gpiochip->parent) {
1057 pr_err("missing gpiochip .dev parent pointer\n");
1060 of_node = gpiochip->parent->of_node;
1061 #ifdef CONFIG_OF_GPIO
1063 * If the gpiochip has an assigned OF node this takes precedence
1064 * FIXME: get rid of this and use gpiochip->parent->of_node
1067 if (gpiochip->of_node)
1068 of_node = gpiochip->of_node;
1070 gpiochip->irqchip = irqchip;
1071 gpiochip->irq_handler = handler;
1072 gpiochip->irq_default_type = type;
1073 gpiochip->to_irq = gpiochip_to_irq;
1074 gpiochip->lock_key = lock_key;
1075 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1076 gpiochip->ngpio, first_irq,
1077 &gpiochip_domain_ops, gpiochip);
1078 if (!gpiochip->irqdomain) {
1079 gpiochip->irqchip = NULL;
1084 * It is possible for a driver to override this, but only if the
1085 * alternative functions are both implemented.
1087 if (!irqchip->irq_request_resources &&
1088 !irqchip->irq_release_resources) {
1089 irqchip->irq_request_resources = gpiochip_irq_reqres;
1090 irqchip->irq_release_resources = gpiochip_irq_relres;
1094 * Prepare the mapping since the irqchip shall be orthogonal to
1095 * any gpiochip calls. If the first_irq was zero, this is
1096 * necessary to allocate descriptors for all IRQs.
1098 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1099 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1102 * Store the base into the gpiochip to be used when
1103 * unmapping the irqs.
1105 gpiochip->irq_base = irq_base;
1108 acpi_gpiochip_request_interrupts(gpiochip);
1112 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
1114 #else /* CONFIG_GPIOLIB_IRQCHIP */
1116 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1118 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1121 * gpiochip_generic_request() - request the gpio function for a pin
1122 * @chip: the gpiochip owning the GPIO
1123 * @offset: the offset of the GPIO to request for GPIO function
1125 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1127 return pinctrl_request_gpio(chip->gpiodev->base + offset);
1129 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1132 * gpiochip_generic_free() - free the gpio function from a pin
1133 * @chip: the gpiochip to request the gpio function for
1134 * @offset: the offset of the GPIO to free from GPIO function
1136 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1138 pinctrl_free_gpio(chip->gpiodev->base + offset);
1140 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1142 #ifdef CONFIG_PINCTRL
1145 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1146 * @chip: the gpiochip to add the range for
1147 * @pctldev: the pin controller to map to
1148 * @gpio_offset: the start offset in the current gpio_chip number space
1149 * @pin_group: name of the pin group inside the pin controller
1151 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1152 struct pinctrl_dev *pctldev,
1153 unsigned int gpio_offset, const char *pin_group)
1155 struct gpio_pin_range *pin_range;
1156 struct gpio_device *gdev = chip->gpiodev;
1159 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1161 chip_err(chip, "failed to allocate pin ranges\n");
1165 /* Use local offset as range ID */
1166 pin_range->range.id = gpio_offset;
1167 pin_range->range.gc = chip;
1168 pin_range->range.name = chip->label;
1169 pin_range->range.base = gdev->base + gpio_offset;
1170 pin_range->pctldev = pctldev;
1172 ret = pinctrl_get_group_pins(pctldev, pin_group,
1173 &pin_range->range.pins,
1174 &pin_range->range.npins);
1180 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1182 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1183 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1184 pinctrl_dev_get_devname(pctldev), pin_group);
1186 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1190 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1193 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1194 * @chip: the gpiochip to add the range for
1195 * @pinctrl_name: the dev_name() of the pin controller to map to
1196 * @gpio_offset: the start offset in the current gpio_chip number space
1197 * @pin_offset: the start offset in the pin controller number space
1198 * @npins: the number of pins from the offset of each pin space (GPIO and
1199 * pin controller) to accumulate in this range
1201 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1202 unsigned int gpio_offset, unsigned int pin_offset,
1205 struct gpio_pin_range *pin_range;
1206 struct gpio_device *gdev = chip->gpiodev;
1209 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1211 chip_err(chip, "failed to allocate pin ranges\n");
1215 /* Use local offset as range ID */
1216 pin_range->range.id = gpio_offset;
1217 pin_range->range.gc = chip;
1218 pin_range->range.name = chip->label;
1219 pin_range->range.base = gdev->base + gpio_offset;
1220 pin_range->range.pin_base = pin_offset;
1221 pin_range->range.npins = npins;
1222 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1224 if (IS_ERR(pin_range->pctldev)) {
1225 ret = PTR_ERR(pin_range->pctldev);
1226 chip_err(chip, "could not create pin range\n");
1230 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1231 gpio_offset, gpio_offset + npins - 1,
1233 pin_offset, pin_offset + npins - 1);
1235 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1239 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1242 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1243 * @chip: the chip to remove all the mappings for
1245 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1247 struct gpio_pin_range *pin_range, *tmp;
1248 struct gpio_device *gdev = chip->gpiodev;
1250 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1251 list_del(&pin_range->node);
1252 pinctrl_remove_gpio_range(pin_range->pctldev,
1257 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1259 #endif /* CONFIG_PINCTRL */
1261 /* These "optional" allocation calls help prevent drivers from stomping
1262 * on each other, and help provide better diagnostics in debugfs.
1263 * They're called even less than the "set direction" calls.
1265 static int __gpiod_request(struct gpio_desc *desc, const char *label)
1267 struct gpio_chip *chip = desc->gdev->chip;
1269 unsigned long flags;
1271 spin_lock_irqsave(&gpio_lock, flags);
1273 /* NOTE: gpio_request() can be called in early boot,
1274 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1277 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1278 desc_set_label(desc, label ? : "?");
1285 if (chip->request) {
1286 /* chip->request may sleep */
1287 spin_unlock_irqrestore(&gpio_lock, flags);
1288 status = chip->request(chip, gpio_chip_hwgpio(desc));
1289 spin_lock_irqsave(&gpio_lock, flags);
1292 desc_set_label(desc, NULL);
1293 clear_bit(FLAG_REQUESTED, &desc->flags);
1297 if (chip->get_direction) {
1298 /* chip->get_direction may sleep */
1299 spin_unlock_irqrestore(&gpio_lock, flags);
1300 gpiod_get_direction(desc);
1301 spin_lock_irqsave(&gpio_lock, flags);
1305 /* Clear flags that might have been set by the caller before
1306 * requesting the GPIO.
1308 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1309 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1310 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1312 spin_unlock_irqrestore(&gpio_lock, flags);
1317 * This descriptor validation needs to be inserted verbatim into each
1318 * function taking a descriptor, so we need to use a preprocessor
1319 * macro to avoid endless duplication.
1321 #define VALIDATE_DESC(desc) do { \
1322 if (!desc || !desc->gdev) { \
1323 pr_warn("%s: invalid GPIO\n", __func__); \
1326 if ( !desc->gdev->chip ) { \
1327 dev_warn(&desc->gdev->dev, \
1328 "%s: backing chip is gone\n", __func__); \
1332 #define VALIDATE_DESC_VOID(desc) do { \
1333 if (!desc || !desc->gdev) { \
1334 pr_warn("%s: invalid GPIO\n", __func__); \
1337 if (!desc->gdev->chip) { \
1338 dev_warn(&desc->gdev->dev, \
1339 "%s: backing chip is gone\n", __func__); \
1344 int gpiod_request(struct gpio_desc *desc, const char *label)
1346 int status = -EPROBE_DEFER;
1347 struct gpio_device *gdev;
1349 VALIDATE_DESC(desc);
1352 if (try_module_get(gdev->owner)) {
1353 status = __gpiod_request(desc, label);
1355 module_put(gdev->owner);
1357 get_device(&gdev->dev);
1361 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1366 static bool __gpiod_free(struct gpio_desc *desc)
1369 unsigned long flags;
1370 struct gpio_chip *chip;
1374 gpiod_unexport(desc);
1376 spin_lock_irqsave(&gpio_lock, flags);
1378 chip = desc->gdev->chip;
1379 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1381 spin_unlock_irqrestore(&gpio_lock, flags);
1382 might_sleep_if(chip->can_sleep);
1383 chip->free(chip, gpio_chip_hwgpio(desc));
1384 spin_lock_irqsave(&gpio_lock, flags);
1386 desc_set_label(desc, NULL);
1387 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1388 clear_bit(FLAG_REQUESTED, &desc->flags);
1389 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1390 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1391 clear_bit(FLAG_IS_HOGGED, &desc->flags);
1395 spin_unlock_irqrestore(&gpio_lock, flags);
1399 void gpiod_free(struct gpio_desc *desc)
1401 if (desc && desc->gdev && __gpiod_free(desc)) {
1402 module_put(desc->gdev->owner);
1403 put_device(&desc->gdev->dev);
1405 WARN_ON(extra_checks);
1410 * gpiochip_is_requested - return string iff signal was requested
1411 * @chip: controller managing the signal
1412 * @offset: of signal within controller's 0..(ngpio - 1) range
1414 * Returns NULL if the GPIO is not currently requested, else a string.
1415 * The string returned is the label passed to gpio_request(); if none has been
1416 * passed it is a meaningless, non-NULL constant.
1418 * This function is for use by GPIO controller drivers. The label can
1419 * help with diagnostics, and knowing that the signal is used as a GPIO
1420 * can help avoid accidentally multiplexing it to another controller.
1422 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1424 struct gpio_desc *desc;
1426 if (offset >= chip->ngpio)
1429 desc = &chip->gpiodev->descs[offset];
1431 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1435 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1438 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1439 * @desc: GPIO descriptor to request
1440 * @label: label for the GPIO
1442 * Function allows GPIO chip drivers to request and use their own GPIO
1443 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1444 * function will not increase reference count of the GPIO chip module. This
1445 * allows the GPIO chip module to be unloaded as needed (we assume that the
1446 * GPIO chip driver handles freeing the GPIOs it has requested).
1448 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1451 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1455 chip_err(chip, "failed to get GPIO descriptor\n");
1459 err = __gpiod_request(desc, label);
1461 return ERR_PTR(err);
1465 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
1468 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1469 * @desc: GPIO descriptor to free
1471 * Function frees the given GPIO requested previously with
1472 * gpiochip_request_own_desc().
1474 void gpiochip_free_own_desc(struct gpio_desc *desc)
1479 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
1482 * Drivers MUST set GPIO direction before making get/set calls. In
1483 * some cases this is done in early boot, before IRQs are enabled.
1485 * As a rule these aren't called more than once (except for drivers
1486 * using the open-drain emulation idiom) so these are natural places
1487 * to accumulate extra debugging checks. Note that we can't (yet)
1488 * rely on gpio_request() having been called beforehand.
1492 * gpiod_direction_input - set the GPIO direction to input
1493 * @desc: GPIO to set to input
1495 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1496 * be called safely on it.
1498 * Return 0 in case of success, else an error code.
1500 int gpiod_direction_input(struct gpio_desc *desc)
1502 struct gpio_chip *chip;
1503 int status = -EINVAL;
1505 VALIDATE_DESC(desc);
1506 chip = desc->gdev->chip;
1508 if (!chip->get || !chip->direction_input) {
1510 "%s: missing get() or direction_input() operations\n",
1515 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
1517 clear_bit(FLAG_IS_OUT, &desc->flags);
1519 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1523 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1525 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1527 struct gpio_chip *chip;
1528 int status = -EINVAL;
1530 /* GPIOs used for IRQs shall not be set as output */
1531 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1533 "%s: tried to set a GPIO tied to an IRQ as output\n",
1538 /* Open drain pin should not be driven to 1 */
1539 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1540 return gpiod_direction_input(desc);
1542 /* Open source pin should not be driven to 0 */
1543 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1544 return gpiod_direction_input(desc);
1546 chip = desc->gdev->chip;
1547 if (!chip->set || !chip->direction_output) {
1549 "%s: missing set() or direction_output() operations\n",
1554 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1556 set_bit(FLAG_IS_OUT, &desc->flags);
1557 trace_gpio_value(desc_to_gpio(desc), 0, value);
1558 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1563 * gpiod_direction_output_raw - set the GPIO direction to output
1564 * @desc: GPIO to set to output
1565 * @value: initial output value of the GPIO
1567 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1568 * be called safely on it. The initial value of the output must be specified
1569 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1571 * Return 0 in case of success, else an error code.
1573 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1575 VALIDATE_DESC(desc);
1576 return _gpiod_direction_output_raw(desc, value);
1578 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1581 * gpiod_direction_output - set the GPIO direction to output
1582 * @desc: GPIO to set to output
1583 * @value: initial output value of the GPIO
1585 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1586 * be called safely on it. The initial value of the output must be specified
1587 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1590 * Return 0 in case of success, else an error code.
1592 int gpiod_direction_output(struct gpio_desc *desc, int value)
1594 VALIDATE_DESC(desc);
1595 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1597 return _gpiod_direction_output_raw(desc, value);
1599 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1602 * gpiod_set_debounce - sets @debounce time for a @gpio
1603 * @gpio: the gpio to set debounce time
1604 * @debounce: debounce time is microseconds
1606 * returns -ENOTSUPP if the controller does not support setting
1609 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1611 struct gpio_chip *chip;
1613 VALIDATE_DESC(desc);
1614 chip = desc->gdev->chip;
1615 if (!chip->set || !chip->set_debounce) {
1617 "%s: missing set() or set_debounce() operations\n",
1622 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1624 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1627 * gpiod_is_active_low - test whether a GPIO is active-low or not
1628 * @desc: the gpio descriptor to test
1630 * Returns 1 if the GPIO is active-low, 0 otherwise.
1632 int gpiod_is_active_low(const struct gpio_desc *desc)
1634 VALIDATE_DESC(desc);
1635 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1637 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1639 /* I/O calls are only valid after configuration completed; the relevant
1640 * "is this a valid GPIO" error checks should already have been done.
1642 * "Get" operations are often inlinable as reading a pin value register,
1643 * and masking the relevant bit in that register.
1645 * When "set" operations are inlinable, they involve writing that mask to
1646 * one register to set a low value, or a different register to set it high.
1647 * Otherwise locking is needed, so there may be little value to inlining.
1649 *------------------------------------------------------------------------
1651 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1652 * have requested the GPIO. That can include implicit requesting by
1653 * a direction setting call. Marking a gpio as requested locks its chip
1654 * in memory, guaranteeing that these table lookups need no more locking
1655 * and that gpiochip_remove() will fail.
1657 * REVISIT when debugging, consider adding some instrumentation to ensure
1658 * that the GPIO was actually requested.
1661 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
1663 struct gpio_chip *chip;
1667 chip = desc->gdev->chip;
1668 offset = gpio_chip_hwgpio(desc);
1669 value = chip->get ? chip->get(chip, offset) : -EIO;
1670 value = value < 0 ? value : !!value;
1671 trace_gpio_value(desc_to_gpio(desc), 1, value);
1676 * gpiod_get_raw_value() - return a gpio's raw value
1677 * @desc: gpio whose value will be returned
1679 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1680 * its ACTIVE_LOW status, or negative errno on failure.
1682 * This function should be called from contexts where we cannot sleep, and will
1683 * complain if the GPIO chip functions potentially sleep.
1685 int gpiod_get_raw_value(const struct gpio_desc *desc)
1687 VALIDATE_DESC(desc);
1688 /* Should be using gpio_get_value_cansleep() */
1689 WARN_ON(desc->gdev->chip->can_sleep);
1690 return _gpiod_get_raw_value(desc);
1692 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1695 * gpiod_get_value() - return a gpio's value
1696 * @desc: gpio whose value will be returned
1698 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1699 * account, or negative errno on failure.
1701 * This function should be called from contexts where we cannot sleep, and will
1702 * complain if the GPIO chip functions potentially sleep.
1704 int gpiod_get_value(const struct gpio_desc *desc)
1708 VALIDATE_DESC(desc);
1709 /* Should be using gpio_get_value_cansleep() */
1710 WARN_ON(desc->gdev->chip->can_sleep);
1712 value = _gpiod_get_raw_value(desc);
1716 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1721 EXPORT_SYMBOL_GPL(gpiod_get_value);
1724 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1725 * @desc: gpio descriptor whose state need to be set.
1726 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1728 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1731 struct gpio_chip *chip = desc->gdev->chip;
1732 int offset = gpio_chip_hwgpio(desc);
1735 err = chip->direction_input(chip, offset);
1737 clear_bit(FLAG_IS_OUT, &desc->flags);
1739 err = chip->direction_output(chip, offset, 0);
1741 set_bit(FLAG_IS_OUT, &desc->flags);
1743 trace_gpio_direction(desc_to_gpio(desc), value, err);
1746 "%s: Error in set_value for open drain err %d\n",
1751 * _gpio_set_open_source_value() - Set the open source gpio's value.
1752 * @desc: gpio descriptor whose state need to be set.
1753 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1755 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1758 struct gpio_chip *chip = desc->gdev->chip;
1759 int offset = gpio_chip_hwgpio(desc);
1762 err = chip->direction_output(chip, offset, 1);
1764 set_bit(FLAG_IS_OUT, &desc->flags);
1766 err = chip->direction_input(chip, offset);
1768 clear_bit(FLAG_IS_OUT, &desc->flags);
1770 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1773 "%s: Error in set_value for open source err %d\n",
1777 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1779 struct gpio_chip *chip;
1781 chip = desc->gdev->chip;
1782 trace_gpio_value(desc_to_gpio(desc), 0, value);
1783 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1784 _gpio_set_open_drain_value(desc, value);
1785 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1786 _gpio_set_open_source_value(desc, value);
1788 chip->set(chip, gpio_chip_hwgpio(desc), value);
1792 * set multiple outputs on the same chip;
1793 * use the chip's set_multiple function if available;
1794 * otherwise set the outputs sequentially;
1795 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1796 * defines which outputs are to be changed
1797 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1798 * defines the values the outputs specified by mask are to be set to
1800 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1801 unsigned long *mask, unsigned long *bits)
1803 if (chip->set_multiple) {
1804 chip->set_multiple(chip, mask, bits);
1807 for (i = 0; i < chip->ngpio; i++) {
1808 if (mask[BIT_WORD(i)] == 0) {
1809 /* no more set bits in this mask word;
1810 * skip ahead to the next word */
1811 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1814 /* set outputs if the corresponding mask bit is set */
1815 if (__test_and_clear_bit(i, mask))
1816 chip->set(chip, i, test_bit(i, bits));
1821 static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1822 unsigned int array_size,
1823 struct gpio_desc **desc_array,
1828 while (i < array_size) {
1829 struct gpio_chip *chip = desc_array[i]->gdev->chip;
1830 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1831 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1835 WARN_ON(chip->can_sleep);
1837 memset(mask, 0, sizeof(mask));
1839 struct gpio_desc *desc = desc_array[i];
1840 int hwgpio = gpio_chip_hwgpio(desc);
1841 int value = value_array[i];
1843 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1845 trace_gpio_value(desc_to_gpio(desc), 0, value);
1847 * collect all normal outputs belonging to the same chip
1848 * open drain and open source outputs are set individually
1850 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1851 _gpio_set_open_drain_value(desc, value);
1852 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1853 _gpio_set_open_source_value(desc, value);
1855 __set_bit(hwgpio, mask);
1857 __set_bit(hwgpio, bits);
1859 __clear_bit(hwgpio, bits);
1863 } while ((i < array_size) &&
1864 (desc_array[i]->gdev->chip == chip));
1865 /* push collected bits to outputs */
1867 gpio_chip_set_multiple(chip, mask, bits);
1872 * gpiod_set_raw_value() - assign a gpio's raw value
1873 * @desc: gpio whose value will be assigned
1874 * @value: value to assign
1876 * Set the raw value of the GPIO, i.e. the value of its physical line without
1877 * regard for its ACTIVE_LOW status.
1879 * This function should be called from contexts where we cannot sleep, and will
1880 * complain if the GPIO chip functions potentially sleep.
1882 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1884 VALIDATE_DESC_VOID(desc);
1885 /* Should be using gpio_set_value_cansleep() */
1886 WARN_ON(desc->gdev->chip->can_sleep);
1887 _gpiod_set_raw_value(desc, value);
1889 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1892 * gpiod_set_value() - assign a gpio's value
1893 * @desc: gpio whose value will be assigned
1894 * @value: value to assign
1896 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1899 * This function should be called from contexts where we cannot sleep, and will
1900 * complain if the GPIO chip functions potentially sleep.
1902 void gpiod_set_value(struct gpio_desc *desc, int value)
1904 VALIDATE_DESC_VOID(desc);
1905 /* Should be using gpio_set_value_cansleep() */
1906 WARN_ON(desc->gdev->chip->can_sleep);
1907 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1909 _gpiod_set_raw_value(desc, value);
1911 EXPORT_SYMBOL_GPL(gpiod_set_value);
1914 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1915 * @array_size: number of elements in the descriptor / value arrays
1916 * @desc_array: array of GPIO descriptors whose values will be assigned
1917 * @value_array: array of values to assign
1919 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1920 * without regard for their ACTIVE_LOW status.
1922 * This function should be called from contexts where we cannot sleep, and will
1923 * complain if the GPIO chip functions potentially sleep.
1925 void gpiod_set_raw_array_value(unsigned int array_size,
1926 struct gpio_desc **desc_array, int *value_array)
1930 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1933 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
1936 * gpiod_set_array_value() - assign values to an array of GPIOs
1937 * @array_size: number of elements in the descriptor / value arrays
1938 * @desc_array: array of GPIO descriptors whose values will be assigned
1939 * @value_array: array of values to assign
1941 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1944 * This function should be called from contexts where we cannot sleep, and will
1945 * complain if the GPIO chip functions potentially sleep.
1947 void gpiod_set_array_value(unsigned int array_size,
1948 struct gpio_desc **desc_array, int *value_array)
1952 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1955 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
1958 * gpiod_cansleep() - report whether gpio value access may sleep
1959 * @desc: gpio to check
1962 int gpiod_cansleep(const struct gpio_desc *desc)
1964 VALIDATE_DESC(desc);
1965 return desc->gdev->chip->can_sleep;
1967 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1970 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1971 * @desc: gpio whose IRQ will be returned (already requested)
1973 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1976 int gpiod_to_irq(const struct gpio_desc *desc)
1978 struct gpio_chip *chip;
1981 VALIDATE_DESC(desc);
1982 chip = desc->gdev->chip;
1983 offset = gpio_chip_hwgpio(desc);
1984 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1986 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1989 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1990 * @chip: the chip the GPIO to lock belongs to
1991 * @offset: the offset of the GPIO to lock as IRQ
1993 * This is used directly by GPIO drivers that want to lock down
1994 * a certain GPIO line to be used for IRQs.
1996 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1998 if (offset >= chip->ngpio)
2001 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
2003 "%s: tried to flag a GPIO set as output for IRQ\n",
2008 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2011 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
2014 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
2015 * @chip: the chip the GPIO to lock belongs to
2016 * @offset: the offset of the GPIO to lock as IRQ
2018 * This is used directly by GPIO drivers that want to indicate
2019 * that a certain GPIO is no longer used exclusively for IRQ.
2021 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2023 if (offset >= chip->ngpio)
2026 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2028 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
2030 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2032 if (offset >= chip->ngpio)
2035 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2037 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2039 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2041 if (offset >= chip->ngpio)
2044 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2046 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2048 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2050 if (offset >= chip->ngpio)
2053 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2055 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2058 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2059 * @desc: gpio whose value will be returned
2061 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2062 * its ACTIVE_LOW status, or negative errno on failure.
2064 * This function is to be called from contexts that can sleep.
2066 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2068 might_sleep_if(extra_checks);
2069 VALIDATE_DESC(desc);
2070 return _gpiod_get_raw_value(desc);
2072 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2075 * gpiod_get_value_cansleep() - return a gpio's value
2076 * @desc: gpio whose value will be returned
2078 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2079 * account, or negative errno on failure.
2081 * This function is to be called from contexts that can sleep.
2083 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2087 might_sleep_if(extra_checks);
2088 VALIDATE_DESC(desc);
2089 value = _gpiod_get_raw_value(desc);
2093 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2098 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2101 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2102 * @desc: gpio whose value will be assigned
2103 * @value: value to assign
2105 * Set the raw value of the GPIO, i.e. the value of its physical line without
2106 * regard for its ACTIVE_LOW status.
2108 * This function is to be called from contexts that can sleep.
2110 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2112 might_sleep_if(extra_checks);
2113 VALIDATE_DESC_VOID(desc);
2114 _gpiod_set_raw_value(desc, value);
2116 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2119 * gpiod_set_value_cansleep() - assign a gpio's value
2120 * @desc: gpio whose value will be assigned
2121 * @value: value to assign
2123 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2126 * This function is to be called from contexts that can sleep.
2128 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2130 might_sleep_if(extra_checks);
2131 VALIDATE_DESC_VOID(desc);
2132 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2134 _gpiod_set_raw_value(desc, value);
2136 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2139 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
2140 * @array_size: number of elements in the descriptor / value arrays
2141 * @desc_array: array of GPIO descriptors whose values will be assigned
2142 * @value_array: array of values to assign
2144 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2145 * without regard for their ACTIVE_LOW status.
2147 * This function is to be called from contexts that can sleep.
2149 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2150 struct gpio_desc **desc_array,
2153 might_sleep_if(extra_checks);
2156 gpiod_set_array_value_priv(true, true, array_size, desc_array,
2159 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
2162 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
2163 * @array_size: number of elements in the descriptor / value arrays
2164 * @desc_array: array of GPIO descriptors whose values will be assigned
2165 * @value_array: array of values to assign
2167 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2170 * This function is to be called from contexts that can sleep.
2172 void gpiod_set_array_value_cansleep(unsigned int array_size,
2173 struct gpio_desc **desc_array,
2176 might_sleep_if(extra_checks);
2179 gpiod_set_array_value_priv(false, true, array_size, desc_array,
2182 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
2185 * gpiod_add_lookup_table() - register GPIO device consumers
2186 * @table: table of consumers to register
2188 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
2190 mutex_lock(&gpio_lookup_lock);
2192 list_add_tail(&table->list, &gpio_lookup_list);
2194 mutex_unlock(&gpio_lookup_lock);
2198 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2199 * @table: table of consumers to unregister
2201 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2203 mutex_lock(&gpio_lookup_lock);
2205 list_del(&table->list);
2207 mutex_unlock(&gpio_lookup_lock);
2210 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2212 enum gpio_lookup_flags *flags)
2214 char prop_name[32]; /* 32 is max size of property name */
2215 enum of_gpio_flags of_flags;
2216 struct gpio_desc *desc;
2219 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2221 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
2224 snprintf(prop_name, sizeof(prop_name), "%s",
2227 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2229 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2236 if (of_flags & OF_GPIO_ACTIVE_LOW)
2237 *flags |= GPIO_ACTIVE_LOW;
2239 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2240 if (of_flags & OF_GPIO_ACTIVE_LOW)
2241 *flags |= GPIO_OPEN_DRAIN;
2243 *flags |= GPIO_OPEN_SOURCE;
2249 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2251 enum gpio_lookup_flags *flags)
2253 struct acpi_device *adev = ACPI_COMPANION(dev);
2254 struct acpi_gpio_info info;
2255 struct gpio_desc *desc;
2259 /* Try first from _DSD */
2260 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2261 if (con_id && strcmp(con_id, "gpios")) {
2262 snprintf(propname, sizeof(propname), "%s-%s",
2263 con_id, gpio_suffixes[i]);
2265 snprintf(propname, sizeof(propname), "%s",
2269 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2270 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2274 /* Then from plain _CRS GPIOs */
2276 if (!acpi_can_fallback_to_crs(adev, con_id))
2277 return ERR_PTR(-ENOENT);
2279 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2284 if (info.polarity == GPIO_ACTIVE_LOW)
2285 *flags |= GPIO_ACTIVE_LOW;
2290 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2292 const char *dev_id = dev ? dev_name(dev) : NULL;
2293 struct gpiod_lookup_table *table;
2295 mutex_lock(&gpio_lookup_lock);
2297 list_for_each_entry(table, &gpio_lookup_list, list) {
2298 if (table->dev_id && dev_id) {
2300 * Valid strings on both ends, must be identical to have
2303 if (!strcmp(table->dev_id, dev_id))
2307 * One of the pointers is NULL, so both must be to have
2310 if (dev_id == table->dev_id)
2317 mutex_unlock(&gpio_lookup_lock);
2321 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2323 enum gpio_lookup_flags *flags)
2325 struct gpio_desc *desc = ERR_PTR(-ENOENT);
2326 struct gpiod_lookup_table *table;
2327 struct gpiod_lookup *p;
2329 table = gpiod_find_lookup_table(dev);
2333 for (p = &table->table[0]; p->chip_label; p++) {
2334 struct gpio_chip *chip;
2336 /* idx must always match exactly */
2340 /* If the lookup entry has a con_id, require exact match */
2341 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2344 chip = find_chip_by_name(p->chip_label);
2347 dev_err(dev, "cannot find GPIO chip %s\n",
2349 return ERR_PTR(-ENODEV);
2352 if (chip->ngpio <= p->chip_hwnum) {
2354 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2355 idx, chip->ngpio, chip->label);
2356 return ERR_PTR(-EINVAL);
2359 desc = gpiochip_get_desc(chip, p->chip_hwnum);
2368 static int dt_gpio_count(struct device *dev, const char *con_id)
2374 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2376 snprintf(propname, sizeof(propname), "%s-%s",
2377 con_id, gpio_suffixes[i]);
2379 snprintf(propname, sizeof(propname), "%s",
2382 ret = of_gpio_named_count(dev->of_node, propname);
2389 static int platform_gpio_count(struct device *dev, const char *con_id)
2391 struct gpiod_lookup_table *table;
2392 struct gpiod_lookup *p;
2393 unsigned int count = 0;
2395 table = gpiod_find_lookup_table(dev);
2399 for (p = &table->table[0]; p->chip_label; p++) {
2400 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2401 (!con_id && !p->con_id))
2411 * gpiod_count - return the number of GPIOs associated with a device / function
2412 * or -ENOENT if no GPIO has been assigned to the requested function
2413 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2414 * @con_id: function within the GPIO consumer
2416 int gpiod_count(struct device *dev, const char *con_id)
2418 int count = -ENOENT;
2420 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2421 count = dt_gpio_count(dev, con_id);
2422 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2423 count = acpi_gpio_count(dev, con_id);
2426 count = platform_gpio_count(dev, con_id);
2430 EXPORT_SYMBOL_GPL(gpiod_count);
2433 * gpiod_get - obtain a GPIO for a given GPIO function
2434 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2435 * @con_id: function within the GPIO consumer
2436 * @flags: optional GPIO initialization flags
2438 * Return the GPIO descriptor corresponding to the function con_id of device
2439 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2440 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
2442 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
2443 enum gpiod_flags flags)
2445 return gpiod_get_index(dev, con_id, 0, flags);
2447 EXPORT_SYMBOL_GPL(gpiod_get);
2450 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2451 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2452 * @con_id: function within the GPIO consumer
2453 * @flags: optional GPIO initialization flags
2455 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2456 * the requested function it will return NULL. This is convenient for drivers
2457 * that need to handle optional GPIOs.
2459 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
2461 enum gpiod_flags flags)
2463 return gpiod_get_index_optional(dev, con_id, 0, flags);
2465 EXPORT_SYMBOL_GPL(gpiod_get_optional);
2468 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2469 * @desc: gpio to be setup
2470 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2473 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2475 static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2477 if (lflags & GPIO_ACTIVE_LOW)
2478 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2479 if (lflags & GPIO_OPEN_DRAIN)
2480 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2481 if (lflags & GPIO_OPEN_SOURCE)
2482 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2486 * gpiod_configure_flags - helper function to configure a given GPIO
2487 * @desc: gpio whose value will be assigned
2488 * @con_id: function within the GPIO consumer
2489 * @dflags: gpiod_flags - optional GPIO initialization flags
2491 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2492 * requested function and/or index, or another IS_ERR() code if an error
2493 * occurred while trying to acquire the GPIO.
2495 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2496 enum gpiod_flags dflags)
2500 /* No particular flag request, return here... */
2501 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2502 pr_debug("no flags found for %s\n", con_id);
2507 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2508 status = gpiod_direction_output(desc,
2509 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2511 status = gpiod_direction_input(desc);
2517 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2518 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2519 * @con_id: function within the GPIO consumer
2520 * @idx: index of the GPIO to obtain in the consumer
2521 * @flags: optional GPIO initialization flags
2523 * This variant of gpiod_get() allows to access GPIOs other than the first
2524 * defined one for functions that define several GPIOs.
2526 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2527 * requested function and/or index, or another IS_ERR() code if an error
2528 * occurred while trying to acquire the GPIO.
2530 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2533 enum gpiod_flags flags)
2535 struct gpio_desc *desc = NULL;
2537 enum gpio_lookup_flags lookupflags = 0;
2539 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2542 /* Using device tree? */
2543 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2544 dev_dbg(dev, "using device tree for GPIO lookup\n");
2545 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2546 } else if (ACPI_COMPANION(dev)) {
2547 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2548 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2553 * Either we are not using DT or ACPI, or their lookup did not return
2554 * a result. In that case, use platform lookup as a fallback.
2556 if (!desc || desc == ERR_PTR(-ENOENT)) {
2557 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
2558 desc = gpiod_find(dev, con_id, idx, &lookupflags);
2562 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
2566 gpiod_parse_flags(desc, lookupflags);
2568 status = gpiod_request(desc, con_id);
2570 return ERR_PTR(status);
2572 status = gpiod_configure_flags(desc, con_id, flags);
2574 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2576 return ERR_PTR(status);
2581 EXPORT_SYMBOL_GPL(gpiod_get_index);
2584 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2585 * @fwnode: handle of the firmware node
2586 * @propname: name of the firmware property representing the GPIO
2588 * This function can be used for drivers that get their configuration
2591 * Function properly finds the corresponding GPIO using whatever is the
2592 * underlying firmware interface and then makes sure that the GPIO
2593 * descriptor is requested before it is returned to the caller.
2595 * In case of error an ERR_PTR() is returned.
2597 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2598 const char *propname)
2600 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2601 bool active_low = false;
2602 bool single_ended = false;
2606 return ERR_PTR(-EINVAL);
2608 if (is_of_node(fwnode)) {
2609 enum of_gpio_flags flags;
2611 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
2613 if (!IS_ERR(desc)) {
2614 active_low = flags & OF_GPIO_ACTIVE_LOW;
2615 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2617 } else if (is_acpi_node(fwnode)) {
2618 struct acpi_gpio_info info;
2620 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
2622 active_low = info.polarity == GPIO_ACTIVE_LOW;
2629 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2633 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2635 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2638 ret = gpiod_request(desc, NULL);
2640 return ERR_PTR(ret);
2644 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2647 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2649 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2650 * @con_id: function within the GPIO consumer
2651 * @index: index of the GPIO to obtain in the consumer
2652 * @flags: optional GPIO initialization flags
2654 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2655 * specified index was assigned to the requested function it will return NULL.
2656 * This is convenient for drivers that need to handle optional GPIOs.
2658 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
2661 enum gpiod_flags flags)
2663 struct gpio_desc *desc;
2665 desc = gpiod_get_index(dev, con_id, index, flags);
2667 if (PTR_ERR(desc) == -ENOENT)
2673 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
2676 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2677 * @desc: gpio whose value will be assigned
2678 * @name: gpio line name
2679 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2681 * @dflags: gpiod_flags - optional GPIO initialization flags
2683 int gpiod_hog(struct gpio_desc *desc, const char *name,
2684 unsigned long lflags, enum gpiod_flags dflags)
2686 struct gpio_chip *chip;
2687 struct gpio_desc *local_desc;
2691 chip = gpiod_to_chip(desc);
2692 hwnum = gpio_chip_hwgpio(desc);
2694 gpiod_parse_flags(desc, lflags);
2696 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2697 if (IS_ERR(local_desc)) {
2698 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2699 name, chip->label, hwnum);
2700 return PTR_ERR(local_desc);
2703 status = gpiod_configure_flags(desc, name, dflags);
2705 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2706 name, chip->label, hwnum);
2707 gpiochip_free_own_desc(desc);
2711 /* Mark GPIO as hogged so it can be identified and removed later */
2712 set_bit(FLAG_IS_HOGGED, &desc->flags);
2714 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2715 desc_to_gpio(desc), name,
2716 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2717 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2718 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2724 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2725 * @chip: gpio chip to act on
2727 * This is only used by of_gpiochip_remove to free hogged gpios
2729 static void gpiochip_free_hogs(struct gpio_chip *chip)
2733 for (id = 0; id < chip->ngpio; id++) {
2734 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2735 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
2740 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2741 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2742 * @con_id: function within the GPIO consumer
2743 * @flags: optional GPIO initialization flags
2745 * This function acquires all the GPIOs defined under a given function.
2747 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2748 * no GPIO has been assigned to the requested function, or another IS_ERR()
2749 * code if an error occurred while trying to acquire the GPIOs.
2751 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2753 enum gpiod_flags flags)
2755 struct gpio_desc *desc;
2756 struct gpio_descs *descs;
2759 count = gpiod_count(dev, con_id);
2761 return ERR_PTR(count);
2763 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2766 return ERR_PTR(-ENOMEM);
2768 for (descs->ndescs = 0; descs->ndescs < count; ) {
2769 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2771 gpiod_put_array(descs);
2772 return ERR_CAST(desc);
2774 descs->desc[descs->ndescs] = desc;
2779 EXPORT_SYMBOL_GPL(gpiod_get_array);
2782 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2784 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2785 * @con_id: function within the GPIO consumer
2786 * @flags: optional GPIO initialization flags
2788 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2789 * assigned to the requested function it will return NULL.
2791 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2793 enum gpiod_flags flags)
2795 struct gpio_descs *descs;
2797 descs = gpiod_get_array(dev, con_id, flags);
2798 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2803 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2806 * gpiod_put - dispose of a GPIO descriptor
2807 * @desc: GPIO descriptor to dispose of
2809 * No descriptor can be used after gpiod_put() has been called on it.
2811 void gpiod_put(struct gpio_desc *desc)
2815 EXPORT_SYMBOL_GPL(gpiod_put);
2818 * gpiod_put_array - dispose of multiple GPIO descriptors
2819 * @descs: struct gpio_descs containing an array of descriptors
2821 void gpiod_put_array(struct gpio_descs *descs)
2825 for (i = 0; i < descs->ndescs; i++)
2826 gpiod_put(descs->desc[i]);
2830 EXPORT_SYMBOL_GPL(gpiod_put_array);
2832 static int __init gpiolib_dev_init(void)
2836 /* Register GPIO sysfs bus */
2837 ret = bus_register(&gpio_bus_type);
2839 pr_err("gpiolib: could not register GPIO bus type\n");
2843 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2845 pr_err("gpiolib: failed to allocate char dev region\n");
2846 bus_unregister(&gpio_bus_type);
2850 core_initcall(gpiolib_dev_init);
2852 #ifdef CONFIG_DEBUG_FS
2854 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
2857 struct gpio_chip *chip = gdev->chip;
2858 unsigned gpio = gdev->base;
2859 struct gpio_desc *gdesc = &gdev->descs[0];
2863 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
2864 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2866 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2872 gpiod_get_direction(gdesc);
2873 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2874 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2875 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2876 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
2877 is_out ? "out" : "in ",
2879 ? (chip->get(chip, i) ? "hi" : "lo")
2881 is_irq ? "IRQ" : " ");
2882 seq_printf(s, "\n");
2886 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2888 unsigned long flags;
2889 struct gpio_device *gdev = NULL;
2890 loff_t index = *pos;
2894 spin_lock_irqsave(&gpio_lock, flags);
2895 list_for_each_entry(gdev, &gpio_devices, list)
2897 spin_unlock_irqrestore(&gpio_lock, flags);
2900 spin_unlock_irqrestore(&gpio_lock, flags);
2905 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2907 unsigned long flags;
2908 struct gpio_device *gdev = v;
2911 spin_lock_irqsave(&gpio_lock, flags);
2912 if (list_is_last(&gdev->list, &gpio_devices))
2915 ret = list_entry(gdev->list.next, struct gpio_device, list);
2916 spin_unlock_irqrestore(&gpio_lock, flags);
2924 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2928 static int gpiolib_seq_show(struct seq_file *s, void *v)
2930 struct gpio_device *gdev = v;
2931 struct gpio_chip *chip = gdev->chip;
2932 struct device *parent;
2935 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2936 dev_name(&gdev->dev));
2940 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2941 dev_name(&gdev->dev),
2942 gdev->base, gdev->base + gdev->ngpio - 1);
2943 parent = chip->parent;
2945 seq_printf(s, ", parent: %s/%s",
2946 parent->bus ? parent->bus->name : "no-bus",
2949 seq_printf(s, ", %s", chip->label);
2950 if (chip->can_sleep)
2951 seq_printf(s, ", can sleep");
2952 seq_printf(s, ":\n");
2955 chip->dbg_show(s, chip);
2957 gpiolib_dbg_show(s, gdev);
2962 static const struct seq_operations gpiolib_seq_ops = {
2963 .start = gpiolib_seq_start,
2964 .next = gpiolib_seq_next,
2965 .stop = gpiolib_seq_stop,
2966 .show = gpiolib_seq_show,
2969 static int gpiolib_open(struct inode *inode, struct file *file)
2971 return seq_open(file, &gpiolib_seq_ops);
2974 static const struct file_operations gpiolib_operations = {
2975 .owner = THIS_MODULE,
2976 .open = gpiolib_open,
2978 .llseek = seq_lseek,
2979 .release = seq_release,
2982 static int __init gpiolib_debugfs_init(void)
2984 /* /sys/kernel/debug/gpio */
2985 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2986 NULL, NULL, &gpiolib_operations);
2989 subsys_initcall(gpiolib_debugfs_init);
2991 #endif /* DEBUG_FS */