1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/acpi.h>
4 #include <linux/bitmap.h>
5 #include <linux/cleanup.h>
6 #include <linux/compat.h>
7 #include <linux/debugfs.h>
8 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/file.h>
13 #include <linux/idr.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/lockdep.h>
19 #include <linux/module.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/srcu.h>
26 #include <linux/string.h>
28 #include <linux/gpio.h>
29 #include <linux/gpio/driver.h>
30 #include <linux/gpio/machine.h>
32 #include <uapi/linux/gpio.h>
34 #include "gpiolib-acpi.h"
35 #include "gpiolib-cdev.h"
36 #include "gpiolib-of.h"
37 #include "gpiolib-swnode.h"
38 #include "gpiolib-sysfs.h"
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/gpio.h>
44 /* Implementation infrastructure for GPIO interfaces.
46 * The GPIO programming interface allows for inlining speed-critical
47 * get/set operations for common cases, so that access to SOC-integrated
48 * GPIOs can sometimes cost only an instruction or two per bit.
51 /* Device and char device-related information */
52 static DEFINE_IDA(gpio_ida);
53 static dev_t gpio_devt;
54 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
56 static int gpio_bus_match(struct device *dev, struct device_driver *drv)
58 struct fwnode_handle *fwnode = dev_fwnode(dev);
61 * Only match if the fwnode doesn't already have a proper struct device
64 if (fwnode && fwnode->dev != dev)
69 static const struct bus_type gpio_bus_type = {
71 .match = gpio_bus_match,
75 * Number of GPIOs to use for the fast path in set array
77 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
79 static DEFINE_MUTEX(gpio_lookup_lock);
80 static LIST_HEAD(gpio_lookup_list);
82 static LIST_HEAD(gpio_devices);
83 /* Protects the GPIO device list against concurrent modifications. */
84 static DEFINE_MUTEX(gpio_devices_lock);
85 /* Ensures coherence during read-only accesses to the list of GPIO devices. */
86 DEFINE_STATIC_SRCU(gpio_devices_srcu);
88 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
89 static LIST_HEAD(gpio_machine_hogs);
91 static void gpiochip_free_hogs(struct gpio_chip *gc);
92 static int gpiochip_add_irqchip(struct gpio_chip *gc,
93 struct lock_class_key *lock_key,
94 struct lock_class_key *request_key);
95 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
96 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
97 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
98 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
100 static bool gpiolib_initialized;
102 const char *gpiod_get_label(struct gpio_desc *desc)
106 flags = READ_ONCE(desc->flags);
107 if (test_bit(FLAG_USED_AS_IRQ, &flags) &&
108 !test_bit(FLAG_REQUESTED, &flags))
111 return test_bit(FLAG_REQUESTED, &flags) ?
112 rcu_dereference(desc->label) : NULL;
115 static int desc_set_label(struct gpio_desc *desc, const char *label)
117 const char *new = NULL, *old;
120 new = kstrdup_const(label, GFP_KERNEL);
125 old = rcu_replace_pointer(desc->label, new, 1);
126 synchronize_srcu(&desc->srcu);
133 * gpio_to_desc - Convert a GPIO number to its descriptor
134 * @gpio: global GPIO number
137 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
138 * with the given number exists in the system.
140 struct gpio_desc *gpio_to_desc(unsigned gpio)
142 struct gpio_device *gdev;
144 scoped_guard(srcu, &gpio_devices_srcu) {
145 list_for_each_entry_srcu(gdev, &gpio_devices, list,
146 srcu_read_lock_held(&gpio_devices_srcu)) {
147 if (gdev->base <= gpio &&
148 gdev->base + gdev->ngpio > gpio)
149 return &gdev->descs[gpio - gdev->base];
153 if (!gpio_is_valid(gpio))
154 pr_warn("invalid GPIO %d\n", gpio);
158 EXPORT_SYMBOL_GPL(gpio_to_desc);
160 /* This function is deprecated and will be removed soon, don't use. */
161 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
164 return gpio_device_get_desc(gc->gpiodev, hwnum);
166 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
169 * gpio_device_get_desc() - get the GPIO descriptor corresponding to the given
170 * hardware number for this GPIO device
171 * @gdev: GPIO device to get the descriptor from
172 * @hwnum: hardware number of the GPIO for this chip
175 * A pointer to the GPIO descriptor or %EINVAL if no GPIO exists in the given
176 * chip for the specified hardware number or %ENODEV if the underlying chip
179 * The reference count of struct gpio_device is *NOT* increased like when the
180 * GPIO is being requested for exclusive usage. It's up to the caller to make
181 * sure the GPIO device will stay alive together with the descriptor returned
185 gpio_device_get_desc(struct gpio_device *gdev, unsigned int hwnum)
187 struct gpio_chip *gc;
190 * FIXME: This will be locked once we protect gdev->chip everywhere
195 return ERR_PTR(-ENODEV);
197 if (hwnum >= gdev->ngpio)
198 return ERR_PTR(-EINVAL);
200 return &gdev->descs[hwnum];
202 EXPORT_SYMBOL_GPL(gpio_device_get_desc);
205 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
206 * @desc: GPIO descriptor
208 * This should disappear in the future but is needed since we still
209 * use GPIO numbers for error messages and sysfs nodes.
212 * The global GPIO number for the GPIO specified by its descriptor.
214 int desc_to_gpio(const struct gpio_desc *desc)
216 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
218 EXPORT_SYMBOL_GPL(desc_to_gpio);
222 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
223 * @desc: descriptor to return the chip of
225 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
227 if (!desc || !desc->gdev)
229 return desc->gdev->chip;
231 EXPORT_SYMBOL_GPL(gpiod_to_chip);
234 * gpiod_to_gpio_device() - Return the GPIO device to which this descriptor
236 * @desc: Descriptor for which to return the GPIO device.
238 * This *DOES NOT* increase the reference count of the GPIO device as it's
239 * expected that the descriptor is requested and the users already holds a
240 * reference to the device.
243 * Address of the GPIO device owning this descriptor.
245 struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
252 EXPORT_SYMBOL_GPL(gpiod_to_gpio_device);
255 * gpio_device_get_base() - Get the base GPIO number allocated by this device
259 * First GPIO number in the global GPIO numberspace for this device.
261 int gpio_device_get_base(struct gpio_device *gdev)
265 EXPORT_SYMBOL_GPL(gpio_device_get_base);
268 * gpio_device_get_label() - Get the label of this GPIO device
272 * Pointer to the string containing the GPIO device label. The string's
273 * lifetime is tied to that of the underlying GPIO device.
275 const char *gpio_device_get_label(struct gpio_device *gdev)
279 EXPORT_SYMBOL(gpio_device_get_label);
282 * gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device
286 * Address of the GPIO chip backing this device.
288 * Until we can get rid of all non-driver users of struct gpio_chip, we must
289 * provide a way of retrieving the pointer to it from struct gpio_device. This
290 * is *NOT* safe as the GPIO API is considered to be hot-unpluggable and the
291 * chip can dissapear at any moment (unlike reference-counted struct
294 * Use at your own risk.
296 struct gpio_chip *gpio_device_get_chip(struct gpio_device *gdev)
300 EXPORT_SYMBOL_GPL(gpio_device_get_chip);
302 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
303 static int gpiochip_find_base_unlocked(int ngpio)
305 struct gpio_device *gdev;
306 int base = GPIO_DYNAMIC_BASE;
308 list_for_each_entry_srcu(gdev, &gpio_devices, list,
309 lockdep_is_held(&gpio_devices_lock)) {
310 /* found a free space? */
311 if (gdev->base >= base + ngpio)
313 /* nope, check the space right after the chip */
314 base = gdev->base + gdev->ngpio;
315 if (base < GPIO_DYNAMIC_BASE)
316 base = GPIO_DYNAMIC_BASE;
319 if (gpio_is_valid(base)) {
320 pr_debug("%s: found new base at %d\n", __func__, base);
323 pr_err("%s: cannot find free range\n", __func__);
329 * gpiod_get_direction - return the current direction of a GPIO
330 * @desc: GPIO to get the direction of
332 * Returns 0 for output, 1 for input, or an error code in case of error.
334 * This function may sleep if gpiod_cansleep() is true.
336 int gpiod_get_direction(struct gpio_desc *desc)
338 struct gpio_chip *gc;
342 gc = gpiod_to_chip(desc);
343 offset = gpio_chip_hwgpio(desc);
346 * Open drain emulation using input mode may incorrectly report
347 * input here, fix that up.
349 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
350 test_bit(FLAG_IS_OUT, &desc->flags))
353 if (!gc->get_direction)
356 ret = gc->get_direction(gc, offset);
360 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
364 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
368 EXPORT_SYMBOL_GPL(gpiod_get_direction);
371 * Add a new chip to the global chips list, keeping the list of chips sorted
372 * by range(means [base, base + ngpio - 1]) order.
374 * Return -EBUSY if the new chip overlaps with some other chip's integer
377 static int gpiodev_add_to_list_unlocked(struct gpio_device *gdev)
379 struct gpio_device *prev, *next;
381 lockdep_assert_held(&gpio_devices_lock);
383 if (list_empty(&gpio_devices)) {
384 /* initial entry in list */
385 list_add_tail_rcu(&gdev->list, &gpio_devices);
389 next = list_first_entry(&gpio_devices, struct gpio_device, list);
390 if (gdev->base + gdev->ngpio <= next->base) {
391 /* add before first entry */
392 list_add_rcu(&gdev->list, &gpio_devices);
396 prev = list_last_entry(&gpio_devices, struct gpio_device, list);
397 if (prev->base + prev->ngpio <= gdev->base) {
398 /* add behind last entry */
399 list_add_tail_rcu(&gdev->list, &gpio_devices);
403 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
404 /* at the end of the list */
405 if (&next->list == &gpio_devices)
408 /* add between prev and next */
409 if (prev->base + prev->ngpio <= gdev->base
410 && gdev->base + gdev->ngpio <= next->base) {
411 list_add_rcu(&gdev->list, &prev->list);
416 synchronize_srcu(&gpio_devices_srcu);
422 * Convert a GPIO name to its descriptor
423 * Note that there is no guarantee that GPIO names are globally unique!
424 * Hence this function will return, if it exists, a reference to the first GPIO
425 * line found that matches the given name.
427 static struct gpio_desc *gpio_name_to_desc(const char * const name)
429 struct gpio_device *gdev;
430 struct gpio_desc *desc;
435 guard(srcu)(&gpio_devices_srcu);
437 list_for_each_entry_srcu(gdev, &gpio_devices, list,
438 srcu_read_lock_held(&gpio_devices_srcu)) {
439 for_each_gpio_desc(gdev->chip, desc) {
440 if (desc->name && !strcmp(desc->name, name))
449 * Take the names from gc->names and assign them to their GPIO descriptors.
450 * Warn if a name is already used for a GPIO line on a different GPIO chip.
453 * 1. Non-unique names are still accepted,
454 * 2. Name collisions within the same GPIO chip are not reported.
456 static int gpiochip_set_desc_names(struct gpio_chip *gc)
458 struct gpio_device *gdev = gc->gpiodev;
461 /* First check all names if they are unique */
462 for (i = 0; i != gc->ngpio; ++i) {
463 struct gpio_desc *gpio;
465 gpio = gpio_name_to_desc(gc->names[i]);
468 "Detected name collision for GPIO name '%s'\n",
472 /* Then add all names to the GPIO descriptors */
473 for (i = 0; i != gc->ngpio; ++i)
474 gdev->descs[i].name = gc->names[i];
480 * gpiochip_set_names - Set GPIO line names using device properties
481 * @chip: GPIO chip whose lines should be named, if possible
483 * Looks for device property "gpio-line-names" and if it exists assigns
484 * GPIO line names for the chip. The memory allocated for the assigned
485 * names belong to the underlying firmware node and should not be released
488 static int gpiochip_set_names(struct gpio_chip *chip)
490 struct gpio_device *gdev = chip->gpiodev;
491 struct device *dev = &gdev->dev;
496 count = device_property_string_array_count(dev, "gpio-line-names");
501 * When offset is set in the driver side we assume the driver internally
502 * is using more than one gpiochip per the same device. We have to stop
503 * setting friendly names if the specified ones with 'gpio-line-names'
504 * are less than the offset in the device itself. This means all the
505 * lines are not present for every single pin within all the internal
508 if (count <= chip->offset) {
509 dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
510 count, chip->offset);
514 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
518 ret = device_property_read_string_array(dev, "gpio-line-names",
521 dev_warn(dev, "failed to read GPIO line names\n");
527 * When more that one gpiochip per device is used, 'count' can
528 * contain at most number gpiochips x chip->ngpio. We have to
529 * correctly distribute all defined lines taking into account
530 * chip->offset as starting point from where we will assign
531 * the names to pins from the 'names' array. Since property
532 * 'gpio-line-names' cannot contains gaps, we have to be sure
533 * we only assign those pins that really exists since chip->ngpio
534 * can be different of the chip->offset.
536 count = (count > chip->offset) ? count - chip->offset : count;
537 if (count > chip->ngpio)
540 for (i = 0; i < count; i++) {
542 * Allow overriding "fixed" names provided by the GPIO
543 * provider. The "fixed" names are more often than not
544 * generic and less informative than the names given in
547 if (names[chip->offset + i] && names[chip->offset + i][0])
548 gdev->descs[i].name = names[chip->offset + i];
556 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
560 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
564 /* Assume by default all GPIOs are valid */
565 bitmap_fill(p, gc->ngpio);
570 static void gpiochip_free_mask(unsigned long **p)
576 static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
578 struct device *dev = &gc->gpiodev->dev;
581 /* Format is "start, count, ..." */
582 size = device_property_count_u32(dev, "gpio-reserved-ranges");
583 if (size > 0 && size % 2 == 0)
589 static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc)
591 struct device *dev = &gc->gpiodev->dev;
596 size = gpiochip_count_reserved_ranges(gc);
600 ranges = kmalloc_array(size, sizeof(*ranges), GFP_KERNEL);
604 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges",
612 u32 count = ranges[--size];
613 u32 start = ranges[--size];
615 if (start >= gc->ngpio || start + count > gc->ngpio)
618 bitmap_clear(gc->valid_mask, start, count);
625 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
629 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
632 gc->valid_mask = gpiochip_allocate_mask(gc);
636 ret = gpiochip_apply_reserved_ranges(gc);
640 if (gc->init_valid_mask)
641 return gc->init_valid_mask(gc,
648 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
650 gpiochip_free_mask(&gc->valid_mask);
653 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
656 * Device Tree platforms are supposed to use "gpio-ranges"
657 * property. This check ensures that the ->add_pin_ranges()
658 * won't be called for them.
660 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges"))
663 if (gc->add_pin_ranges)
664 return gc->add_pin_ranges(gc);
669 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
672 /* No mask means all valid */
673 if (likely(!gc->valid_mask))
675 return test_bit(offset, gc->valid_mask);
677 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
679 static void gpiodev_release(struct device *dev)
681 struct gpio_device *gdev = to_gpio_device(dev);
684 for (i = 0; i < gdev->ngpio; i++)
685 cleanup_srcu_struct(&gdev->descs[i].srcu);
687 ida_free(&gpio_ida, gdev->id);
688 kfree_const(gdev->label);
693 static const struct device_type gpio_dev_type = {
695 .release = gpiodev_release,
698 #ifdef CONFIG_GPIO_CDEV
699 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
700 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
703 * gpiolib_cdev_register() indirectly calls device_add(), which is still
704 * required even when cdev is not selected.
706 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
707 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
710 static int gpiochip_setup_dev(struct gpio_device *gdev)
712 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
715 device_initialize(&gdev->dev);
718 * If fwnode doesn't belong to another device, it's safe to clear its
721 if (fwnode && !fwnode->dev)
722 fwnode_dev_initialized(fwnode, false);
724 ret = gcdev_register(gdev, gpio_devt);
728 ret = gpiochip_sysfs_register(gdev);
730 goto err_remove_device;
732 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
733 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
738 gcdev_unregister(gdev);
742 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
744 struct gpio_desc *desc;
747 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
749 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
754 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
757 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
759 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
760 __func__, gc->label, hog->chip_hwnum, rv);
763 static void machine_gpiochip_add(struct gpio_chip *gc)
765 struct gpiod_hog *hog;
767 mutex_lock(&gpio_machine_hogs_mutex);
769 list_for_each_entry(hog, &gpio_machine_hogs, list) {
770 if (!strcmp(gc->label, hog->chip_label))
771 gpiochip_machine_hog(gc, hog);
774 mutex_unlock(&gpio_machine_hogs_mutex);
777 static void gpiochip_setup_devs(void)
779 struct gpio_device *gdev;
782 guard(srcu)(&gpio_devices_srcu);
784 list_for_each_entry_srcu(gdev, &gpio_devices, list,
785 srcu_read_lock_held(&gpio_devices_srcu)) {
786 ret = gpiochip_setup_dev(gdev);
789 "Failed to initialize gpio device (%d)\n", ret);
793 static void gpiochip_set_data(struct gpio_chip *gc, void *data)
795 gc->gpiodev->data = data;
799 * gpiochip_get_data() - get per-subdriver data for the chip
803 * The per-subdriver data for the chip.
805 void *gpiochip_get_data(struct gpio_chip *gc)
807 return gc->gpiodev->data;
809 EXPORT_SYMBOL_GPL(gpiochip_get_data);
811 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev)
813 u32 ngpios = gc->ngpio;
817 ret = device_property_read_u32(dev, "ngpios", &ngpios);
820 * -ENODATA means that there is no property found and
821 * we want to issue the error message to the user.
822 * Besides that, we want to return different error code
823 * to state that supplied value is not valid.
832 if (gc->ngpio == 0) {
833 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
837 if (gc->ngpio > FASTPATH_NGPIO)
838 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
839 gc->ngpio, FASTPATH_NGPIO);
843 EXPORT_SYMBOL_GPL(gpiochip_get_ngpios);
845 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
846 struct lock_class_key *lock_key,
847 struct lock_class_key *request_key)
849 struct gpio_device *gdev;
855 * First: allocate and populate the internal stat container, and
856 * set up the struct device.
858 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
862 gdev->dev.type = &gpio_dev_type;
863 gdev->dev.bus = &gpio_bus_type;
864 gdev->dev.parent = gc->parent;
868 gpiochip_set_data(gc, data);
871 * If the calling driver did not initialize firmware node,
872 * do it here using the parent device, if any.
875 device_set_node(&gdev->dev, gc->fwnode);
877 device_set_node(&gdev->dev, dev_fwnode(gc->parent));
879 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
885 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
889 if (gc->parent && gc->parent->driver)
890 gdev->owner = gc->parent->driver->owner;
892 /* TODO: remove chip->owner */
893 gdev->owner = gc->owner;
895 gdev->owner = THIS_MODULE;
897 ret = gpiochip_get_ngpios(gc, &gdev->dev);
899 goto err_free_dev_name;
901 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
904 goto err_free_dev_name;
907 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
913 gdev->ngpio = gc->ngpio;
915 scoped_guard(mutex, &gpio_devices_lock) {
917 * TODO: this allocates a Linux GPIO number base in the global
918 * GPIO numberspace for this chip. In the long run we want to
919 * get *rid* of this numberspace and use only descriptors, but
920 * it may be a pipe dream. It will not happen before we get rid
921 * of the sysfs interface anyways.
925 base = gpiochip_find_base_unlocked(gc->ngpio);
933 * TODO: it should not be necessary to reflect the
934 * assigned base outside of the GPIO subsystem. Go over
935 * drivers and see if anyone makes use of this, else
936 * drop this and assign a poison instead.
941 "Static allocation of GPIO base is deprecated, use dynamic allocation.\n");
946 ret = gpiodev_add_to_list_unlocked(gdev);
948 chip_err(gc, "GPIO integer space overlap, cannot add chip\n");
953 for (i = 0; i < gc->ngpio; i++)
954 gdev->descs[i].gdev = gdev;
956 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier);
957 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier);
958 init_rwsem(&gdev->sem);
960 #ifdef CONFIG_PINCTRL
961 INIT_LIST_HEAD(&gdev->pin_ranges);
965 ret = gpiochip_set_desc_names(gc);
967 goto err_remove_from_list;
969 ret = gpiochip_set_names(gc);
971 goto err_remove_from_list;
973 ret = gpiochip_init_valid_mask(gc);
975 goto err_remove_from_list;
977 ret = of_gpiochip_add(gc);
979 goto err_free_gpiochip_mask;
981 for (i = 0; i < gc->ngpio; i++) {
982 struct gpio_desc *desc = &gdev->descs[i];
984 ret = init_srcu_struct(&desc->srcu);
986 for (j = 0; j < i; j++)
987 cleanup_srcu_struct(&gdev->descs[j].srcu);
988 goto err_remove_of_chip;
991 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
992 assign_bit(FLAG_IS_OUT,
993 &desc->flags, !gc->get_direction(gc, i));
995 assign_bit(FLAG_IS_OUT,
996 &desc->flags, !gc->direction_input);
1000 ret = gpiochip_add_pin_ranges(gc);
1002 goto err_cleanup_desc_srcu;
1004 acpi_gpiochip_add(gc);
1006 machine_gpiochip_add(gc);
1008 ret = gpiochip_irqchip_init_valid_mask(gc);
1010 goto err_remove_acpi_chip;
1012 ret = gpiochip_irqchip_init_hw(gc);
1014 goto err_remove_acpi_chip;
1016 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
1018 goto err_remove_irqchip_mask;
1021 * By first adding the chardev, and then adding the device,
1022 * we get a device node entry in sysfs under
1023 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1024 * coldplug of device nodes and other udev business.
1025 * We can do this only if gpiolib has been initialized.
1026 * Otherwise, defer until later.
1028 if (gpiolib_initialized) {
1029 ret = gpiochip_setup_dev(gdev);
1031 goto err_remove_irqchip;
1036 gpiochip_irqchip_remove(gc);
1037 err_remove_irqchip_mask:
1038 gpiochip_irqchip_free_valid_mask(gc);
1039 err_remove_acpi_chip:
1040 acpi_gpiochip_remove(gc);
1041 err_cleanup_desc_srcu:
1042 for (i = 0; i < gdev->ngpio; i++)
1043 cleanup_srcu_struct(&gdev->descs[i].srcu);
1045 gpiochip_free_hogs(gc);
1046 of_gpiochip_remove(gc);
1047 err_free_gpiochip_mask:
1048 gpiochip_remove_pin_ranges(gc);
1049 gpiochip_free_valid_mask(gc);
1050 err_remove_from_list:
1051 scoped_guard(mutex, &gpio_devices_lock)
1052 list_del_rcu(&gdev->list);
1053 synchronize_srcu(&gpio_devices_srcu);
1054 if (gdev->dev.release) {
1055 /* release() has been registered by gpiochip_setup_dev() */
1056 gpio_device_put(gdev);
1057 goto err_print_message;
1060 kfree_const(gdev->label);
1064 kfree(dev_name(&gdev->dev));
1066 ida_free(&gpio_ida, gdev->id);
1070 /* failures here can mean systems won't boot... */
1071 if (ret != -EPROBE_DEFER) {
1072 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
1073 base, base + (int)gc->ngpio - 1,
1074 gc->label ? : "generic", ret);
1078 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1081 * gpiochip_remove() - unregister a gpio_chip
1082 * @gc: the chip to unregister
1084 * A gpio_chip with any GPIOs still requested may not be removed.
1086 void gpiochip_remove(struct gpio_chip *gc)
1088 struct gpio_device *gdev = gc->gpiodev;
1091 down_write(&gdev->sem);
1093 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1094 gpiochip_sysfs_unregister(gdev);
1095 gpiochip_free_hogs(gc);
1097 scoped_guard(mutex, &gpio_devices_lock)
1098 list_del_rcu(&gdev->list);
1099 synchronize_srcu(&gpio_devices_srcu);
1101 /* Numb the device, cancelling all outstanding operations */
1103 gpiochip_irqchip_remove(gc);
1104 acpi_gpiochip_remove(gc);
1105 of_gpiochip_remove(gc);
1106 gpiochip_remove_pin_ranges(gc);
1107 gpiochip_free_valid_mask(gc);
1109 * We accept no more calls into the driver from this point, so
1110 * NULL the driver data pointer.
1112 gpiochip_set_data(gc, NULL);
1114 for (i = 0; i < gdev->ngpio; i++) {
1115 if (test_bit(FLAG_REQUESTED, &gdev->descs[i].flags))
1119 if (i != gdev->ngpio)
1120 dev_crit(&gdev->dev,
1121 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1124 * The gpiochip side puts its use of the device to rest here:
1125 * if there are no userspace clients, the chardev and device will
1126 * be removed, else it will be dangling until the last user is
1129 gcdev_unregister(gdev);
1130 up_write(&gdev->sem);
1131 gpio_device_put(gdev);
1133 EXPORT_SYMBOL_GPL(gpiochip_remove);
1136 * gpio_device_find() - find a specific GPIO device
1137 * @data: data to pass to match function
1138 * @match: Callback function to check gpio_chip
1141 * New reference to struct gpio_device.
1143 * Similar to bus_find_device(). It returns a reference to a gpio_device as
1144 * determined by a user supplied @match callback. The callback should return
1145 * 0 if the device doesn't match and non-zero if it does. If the callback
1146 * returns non-zero, this function will return to the caller and not iterate
1147 * over any more gpio_devices.
1149 * The callback takes the GPIO chip structure as argument. During the execution
1150 * of the callback function the chip is protected from being freed. TODO: This
1151 * actually has yet to be implemented.
1153 * If the function returns non-NULL, the returned reference must be freed by
1154 * the caller using gpio_device_put().
1156 struct gpio_device *gpio_device_find(void *data,
1157 int (*match)(struct gpio_chip *gc,
1160 struct gpio_device *gdev;
1163 * Not yet but in the future the spinlock below will become a mutex.
1164 * Annotate this function before anyone tries to use it in interrupt
1165 * context like it happened with gpiochip_find().
1169 guard(srcu)(&gpio_devices_srcu);
1171 list_for_each_entry(gdev, &gpio_devices, list) {
1172 if (gdev->chip && match(gdev->chip, data))
1173 return gpio_device_get(gdev);
1178 EXPORT_SYMBOL_GPL(gpio_device_find);
1180 static int gpio_chip_match_by_label(struct gpio_chip *gc, const void *label)
1182 return gc->label && !strcmp(gc->label, label);
1186 * gpio_device_find_by_label() - wrapper around gpio_device_find() finding the
1187 * GPIO device by its backing chip's label
1188 * @label: Label to lookup
1191 * Reference to the GPIO device or NULL. Reference must be released with
1192 * gpio_device_put().
1194 struct gpio_device *gpio_device_find_by_label(const char *label)
1196 return gpio_device_find((void *)label, gpio_chip_match_by_label);
1198 EXPORT_SYMBOL_GPL(gpio_device_find_by_label);
1200 static int gpio_chip_match_by_fwnode(struct gpio_chip *gc, const void *fwnode)
1202 return device_match_fwnode(&gc->gpiodev->dev, fwnode);
1206 * gpio_device_find_by_fwnode() - wrapper around gpio_device_find() finding
1207 * the GPIO device by its fwnode
1208 * @fwnode: Firmware node to lookup
1211 * Reference to the GPIO device or NULL. Reference must be released with
1212 * gpio_device_put().
1214 struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnode)
1216 return gpio_device_find((void *)fwnode, gpio_chip_match_by_fwnode);
1218 EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode);
1221 * gpio_device_get() - Increase the reference count of this GPIO device
1222 * @gdev: GPIO device to increase the refcount for
1227 struct gpio_device *gpio_device_get(struct gpio_device *gdev)
1229 return to_gpio_device(get_device(&gdev->dev));
1231 EXPORT_SYMBOL_GPL(gpio_device_get);
1234 * gpio_device_put() - Decrease the reference count of this GPIO device and
1235 * possibly free all resources associated with it.
1236 * @gdev: GPIO device to decrease the reference count for
1238 void gpio_device_put(struct gpio_device *gdev)
1240 put_device(&gdev->dev);
1242 EXPORT_SYMBOL_GPL(gpio_device_put);
1245 * gpio_device_to_device() - Retrieve the address of the underlying struct
1247 * @gdev: GPIO device for which to return the address.
1249 * This does not increase the reference count of the GPIO device nor the
1250 * underlying struct device.
1253 * Address of struct device backing this GPIO device.
1255 struct device *gpio_device_to_device(struct gpio_device *gdev)
1259 EXPORT_SYMBOL_GPL(gpio_device_to_device);
1261 #ifdef CONFIG_GPIOLIB_IRQCHIP
1264 * The following is irqchip helper code for gpiochips.
1267 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1269 struct gpio_irq_chip *girq = &gc->irq;
1274 return girq->init_hw(gc);
1277 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1279 struct gpio_irq_chip *girq = &gc->irq;
1281 if (!girq->init_valid_mask)
1284 girq->valid_mask = gpiochip_allocate_mask(gc);
1285 if (!girq->valid_mask)
1288 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1293 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1295 gpiochip_free_mask(&gc->irq.valid_mask);
1298 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1299 unsigned int offset)
1301 if (!gpiochip_line_is_valid(gc, offset))
1303 /* No mask means all valid */
1304 if (likely(!gc->irq.valid_mask))
1306 return test_bit(offset, gc->irq.valid_mask);
1309 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1312 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1314 * @gc: the gpiochip to set the irqchip hierarchical handler to
1315 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1316 * will then percolate up to the parent
1318 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1319 struct irq_chip *irqchip)
1321 /* DT will deal with mapping each IRQ as we go along */
1322 if (is_of_node(gc->irq.fwnode))
1326 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1327 * irqs upfront instead of dynamically since we don't have the
1328 * dynamic type of allocation that hardware description languages
1329 * provide. Once all GPIO drivers using board files are gone from
1330 * the kernel we can delete this code, but for a transitional period
1331 * it is necessary to keep this around.
1333 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1337 for (i = 0; i < gc->ngpio; i++) {
1338 struct irq_fwspec fwspec;
1339 unsigned int parent_hwirq;
1340 unsigned int parent_type;
1341 struct gpio_irq_chip *girq = &gc->irq;
1344 * We call the child to parent translation function
1345 * only to check if the child IRQ is valid or not.
1346 * Just pick the rising edge type here as that is what
1347 * we likely need to support.
1349 ret = girq->child_to_parent_hwirq(gc, i,
1350 IRQ_TYPE_EDGE_RISING,
1354 chip_err(gc, "skip set-up on hwirq %d\n",
1359 fwspec.fwnode = gc->irq.fwnode;
1360 /* This is the hwirq for the GPIO line side of things */
1361 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1362 /* Just pick something */
1363 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1364 fwspec.param_count = 2;
1365 ret = irq_domain_alloc_irqs(gc->irq.domain, 1,
1366 NUMA_NO_NODE, &fwspec);
1369 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1376 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1381 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1382 struct irq_fwspec *fwspec,
1383 unsigned long *hwirq,
1386 /* We support standard DT translation */
1387 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1388 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1391 /* This is for board files and others not using DT */
1392 if (is_fwnode_irqchip(fwspec->fwnode)) {
1395 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1398 WARN_ON(*type == IRQ_TYPE_NONE);
1404 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1406 unsigned int nr_irqs,
1409 struct gpio_chip *gc = d->host_data;
1410 irq_hw_number_t hwirq;
1411 unsigned int type = IRQ_TYPE_NONE;
1412 struct irq_fwspec *fwspec = data;
1413 union gpio_irq_fwspec gpio_parent_fwspec = {};
1414 unsigned int parent_hwirq;
1415 unsigned int parent_type;
1416 struct gpio_irq_chip *girq = &gc->irq;
1420 * The nr_irqs parameter is always one except for PCI multi-MSI
1421 * so this should not happen.
1423 WARN_ON(nr_irqs != 1);
1425 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1429 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1431 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1432 &parent_hwirq, &parent_type);
1434 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1437 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1440 * We set handle_bad_irq because the .set_type() should
1441 * always be invoked and set the right type of handler.
1443 irq_domain_set_info(d,
1452 /* This parent only handles asserted level IRQs */
1453 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
1454 parent_hwirq, parent_type);
1458 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1460 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1461 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
1463 * If the parent irqdomain is msi, the interrupts have already
1464 * been allocated, so the EEXIST is good.
1466 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1470 "failed to allocate parent hwirq %d for hwirq %lu\n",
1471 parent_hwirq, hwirq);
1476 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1477 unsigned int offset)
1483 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1484 * @domain: The IRQ domain used by this IRQ chip
1485 * @data: Outermost irq_data associated with the IRQ
1486 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1488 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1489 * used as the activate function for the &struct irq_domain_ops. The host_data
1490 * for the IRQ domain must be the &struct gpio_chip.
1492 static int gpiochip_irq_domain_activate(struct irq_domain *domain,
1493 struct irq_data *data, bool reserve)
1495 struct gpio_chip *gc = domain->host_data;
1496 unsigned int hwirq = irqd_to_hwirq(data);
1498 return gpiochip_lock_as_irq(gc, hwirq);
1502 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1503 * @domain: The IRQ domain used by this IRQ chip
1504 * @data: Outermost irq_data associated with the IRQ
1506 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1507 * be used as the deactivate function for the &struct irq_domain_ops. The
1508 * host_data for the IRQ domain must be the &struct gpio_chip.
1510 static void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1511 struct irq_data *data)
1513 struct gpio_chip *gc = domain->host_data;
1514 unsigned int hwirq = irqd_to_hwirq(data);
1516 return gpiochip_unlock_as_irq(gc, hwirq);
1519 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1521 ops->activate = gpiochip_irq_domain_activate;
1522 ops->deactivate = gpiochip_irq_domain_deactivate;
1523 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1526 * We only allow overriding the translate() and free() functions for
1527 * hierarchical chips, and this should only be done if the user
1528 * really need something other than 1:1 translation for translate()
1529 * callback and free if user wants to free up any resources which
1530 * were allocated during callbacks, for example populate_parent_alloc_arg.
1532 if (!ops->translate)
1533 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1535 ops->free = irq_domain_free_irqs_common;
1538 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1540 struct irq_domain *domain;
1542 if (!gc->irq.child_to_parent_hwirq ||
1544 chip_err(gc, "missing irqdomain vital data\n");
1545 return ERR_PTR(-EINVAL);
1548 if (!gc->irq.child_offset_to_irq)
1549 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1551 if (!gc->irq.populate_parent_alloc_arg)
1552 gc->irq.populate_parent_alloc_arg =
1553 gpiochip_populate_parent_fwspec_twocell;
1555 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1557 domain = irq_domain_create_hierarchy(
1558 gc->irq.parent_domain,
1562 &gc->irq.child_irq_domain_ops,
1566 return ERR_PTR(-ENOMEM);
1568 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1573 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1575 return !!gc->irq.parent_domain;
1578 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1579 union gpio_irq_fwspec *gfwspec,
1580 unsigned int parent_hwirq,
1581 unsigned int parent_type)
1583 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1585 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1586 fwspec->param_count = 2;
1587 fwspec->param[0] = parent_hwirq;
1588 fwspec->param[1] = parent_type;
1592 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1594 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1595 union gpio_irq_fwspec *gfwspec,
1596 unsigned int parent_hwirq,
1597 unsigned int parent_type)
1599 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1601 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1602 fwspec->param_count = 4;
1603 fwspec->param[0] = 0;
1604 fwspec->param[1] = parent_hwirq;
1605 fwspec->param[2] = 0;
1606 fwspec->param[3] = parent_type;
1610 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1614 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1616 return ERR_PTR(-EINVAL);
1619 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1624 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1627 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1628 * @d: the irqdomain used by this irqchip
1629 * @irq: the global irq number used by this GPIO irqchip irq
1630 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1632 * This function will set up the mapping for a certain IRQ line on a
1633 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1634 * stored inside the gpiochip.
1636 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1637 irq_hw_number_t hwirq)
1639 struct gpio_chip *gc = d->host_data;
1642 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1645 irq_set_chip_data(irq, gc);
1647 * This lock class tells lockdep that GPIO irqs are in a different
1648 * category than their parents, so it won't report false recursion.
1650 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1651 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1652 /* Chips that use nested thread handlers have them marked */
1653 if (gc->irq.threaded)
1654 irq_set_nested_thread(irq, 1);
1655 irq_set_noprobe(irq);
1657 if (gc->irq.num_parents == 1)
1658 ret = irq_set_parent(irq, gc->irq.parents[0]);
1659 else if (gc->irq.map)
1660 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1666 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1667 * is passed as default type.
1669 if (gc->irq.default_type != IRQ_TYPE_NONE)
1670 irq_set_irq_type(irq, gc->irq.default_type);
1675 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1677 struct gpio_chip *gc = d->host_data;
1679 if (gc->irq.threaded)
1680 irq_set_nested_thread(irq, 0);
1681 irq_set_chip_and_handler(irq, NULL, NULL);
1682 irq_set_chip_data(irq, NULL);
1685 static const struct irq_domain_ops gpiochip_domain_ops = {
1686 .map = gpiochip_irq_map,
1687 .unmap = gpiochip_irq_unmap,
1688 /* Virtually all GPIO irqchips are twocell:ed */
1689 .xlate = irq_domain_xlate_twocell,
1692 static struct irq_domain *gpiochip_simple_create_domain(struct gpio_chip *gc)
1694 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1695 struct irq_domain *domain;
1697 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first,
1698 &gpiochip_domain_ops, gc);
1700 return ERR_PTR(-EINVAL);
1705 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1707 struct irq_domain *domain = gc->irq.domain;
1709 #ifdef CONFIG_GPIOLIB_IRQCHIP
1711 * Avoid race condition with other code, which tries to lookup
1712 * an IRQ before the irqchip has been properly registered,
1713 * i.e. while gpiochip is still being brought up.
1715 if (!gc->irq.initialized)
1716 return -EPROBE_DEFER;
1719 if (!gpiochip_irqchip_irq_valid(gc, offset))
1722 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1723 if (irq_domain_is_hierarchy(domain)) {
1724 struct irq_fwspec spec;
1726 spec.fwnode = domain->fwnode;
1727 spec.param_count = 2;
1728 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1729 spec.param[1] = IRQ_TYPE_NONE;
1731 return irq_create_fwspec_mapping(&spec);
1735 return irq_create_mapping(domain, offset);
1738 int gpiochip_irq_reqres(struct irq_data *d)
1740 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1741 unsigned int hwirq = irqd_to_hwirq(d);
1743 return gpiochip_reqres_irq(gc, hwirq);
1745 EXPORT_SYMBOL(gpiochip_irq_reqres);
1747 void gpiochip_irq_relres(struct irq_data *d)
1749 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1750 unsigned int hwirq = irqd_to_hwirq(d);
1752 gpiochip_relres_irq(gc, hwirq);
1754 EXPORT_SYMBOL(gpiochip_irq_relres);
1756 static void gpiochip_irq_mask(struct irq_data *d)
1758 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1759 unsigned int hwirq = irqd_to_hwirq(d);
1761 if (gc->irq.irq_mask)
1762 gc->irq.irq_mask(d);
1763 gpiochip_disable_irq(gc, hwirq);
1766 static void gpiochip_irq_unmask(struct irq_data *d)
1768 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1769 unsigned int hwirq = irqd_to_hwirq(d);
1771 gpiochip_enable_irq(gc, hwirq);
1772 if (gc->irq.irq_unmask)
1773 gc->irq.irq_unmask(d);
1776 static void gpiochip_irq_enable(struct irq_data *d)
1778 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1779 unsigned int hwirq = irqd_to_hwirq(d);
1781 gpiochip_enable_irq(gc, hwirq);
1782 gc->irq.irq_enable(d);
1785 static void gpiochip_irq_disable(struct irq_data *d)
1787 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1788 unsigned int hwirq = irqd_to_hwirq(d);
1790 gc->irq.irq_disable(d);
1791 gpiochip_disable_irq(gc, hwirq);
1794 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1796 struct irq_chip *irqchip = gc->irq.chip;
1798 if (irqchip->flags & IRQCHIP_IMMUTABLE)
1801 chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
1803 if (!irqchip->irq_request_resources &&
1804 !irqchip->irq_release_resources) {
1805 irqchip->irq_request_resources = gpiochip_irq_reqres;
1806 irqchip->irq_release_resources = gpiochip_irq_relres;
1808 if (WARN_ON(gc->irq.irq_enable))
1810 /* Check if the irqchip already has this hook... */
1811 if (irqchip->irq_enable == gpiochip_irq_enable ||
1812 irqchip->irq_mask == gpiochip_irq_mask) {
1814 * ...and if so, give a gentle warning that this is bad
1818 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1822 if (irqchip->irq_disable) {
1823 gc->irq.irq_disable = irqchip->irq_disable;
1824 irqchip->irq_disable = gpiochip_irq_disable;
1826 gc->irq.irq_mask = irqchip->irq_mask;
1827 irqchip->irq_mask = gpiochip_irq_mask;
1830 if (irqchip->irq_enable) {
1831 gc->irq.irq_enable = irqchip->irq_enable;
1832 irqchip->irq_enable = gpiochip_irq_enable;
1834 gc->irq.irq_unmask = irqchip->irq_unmask;
1835 irqchip->irq_unmask = gpiochip_irq_unmask;
1839 static int gpiochip_irqchip_add_allocated_domain(struct gpio_chip *gc,
1840 struct irq_domain *domain,
1841 bool allocated_externally)
1847 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1849 gc->to_irq = gpiochip_to_irq;
1850 gc->irq.domain = domain;
1851 gc->irq.domain_is_allocated_externally = allocated_externally;
1854 * Using barrier() here to prevent compiler from reordering
1855 * gc->irq.initialized before adding irqdomain.
1859 gc->irq.initialized = true;
1865 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1866 * @gc: the GPIO chip to add the IRQ chip to
1867 * @lock_key: lockdep class for IRQ lock
1868 * @request_key: lockdep class for IRQ request
1870 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1871 struct lock_class_key *lock_key,
1872 struct lock_class_key *request_key)
1874 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1875 struct irq_chip *irqchip = gc->irq.chip;
1876 struct irq_domain *domain;
1884 if (gc->irq.parent_handler && gc->can_sleep) {
1885 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1889 type = gc->irq.default_type;
1892 * Specifying a default trigger is a terrible idea if DT or ACPI is
1893 * used to configure the interrupts, as you may end up with
1894 * conflicting triggers. Tell the user, and reset to NONE.
1896 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1897 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1898 type = IRQ_TYPE_NONE;
1900 gc->irq.default_type = type;
1901 gc->irq.lock_key = lock_key;
1902 gc->irq.request_key = request_key;
1904 /* If a parent irqdomain is provided, let's build a hierarchy */
1905 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1906 domain = gpiochip_hierarchy_create_domain(gc);
1908 domain = gpiochip_simple_create_domain(gc);
1911 return PTR_ERR(domain);
1913 if (gc->irq.parent_handler) {
1914 for (i = 0; i < gc->irq.num_parents; i++) {
1917 if (gc->irq.per_parent_data)
1918 data = gc->irq.parent_handler_data_array[i];
1920 data = gc->irq.parent_handler_data ?: gc;
1923 * The parent IRQ chip is already using the chip_data
1924 * for this IRQ chip, so our callbacks simply use the
1927 irq_set_chained_handler_and_data(gc->irq.parents[i],
1928 gc->irq.parent_handler,
1933 gpiochip_set_irq_hooks(gc);
1935 ret = gpiochip_irqchip_add_allocated_domain(gc, domain, false);
1939 acpi_gpiochip_request_interrupts(gc);
1945 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1946 * @gc: the gpiochip to remove the irqchip from
1948 * This is called only from gpiochip_remove()
1950 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1952 struct irq_chip *irqchip = gc->irq.chip;
1953 unsigned int offset;
1955 acpi_gpiochip_free_interrupts(gc);
1957 if (irqchip && gc->irq.parent_handler) {
1958 struct gpio_irq_chip *irq = &gc->irq;
1961 for (i = 0; i < irq->num_parents; i++)
1962 irq_set_chained_handler_and_data(irq->parents[i],
1966 /* Remove all IRQ mappings and delete the domain */
1967 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) {
1970 for (offset = 0; offset < gc->ngpio; offset++) {
1971 if (!gpiochip_irqchip_irq_valid(gc, offset))
1974 irq = irq_find_mapping(gc->irq.domain, offset);
1975 irq_dispose_mapping(irq);
1978 irq_domain_remove(gc->irq.domain);
1981 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
1982 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1983 irqchip->irq_request_resources = NULL;
1984 irqchip->irq_release_resources = NULL;
1986 if (irqchip->irq_enable == gpiochip_irq_enable) {
1987 irqchip->irq_enable = gc->irq.irq_enable;
1988 irqchip->irq_disable = gc->irq.irq_disable;
1991 gc->irq.irq_enable = NULL;
1992 gc->irq.irq_disable = NULL;
1993 gc->irq.chip = NULL;
1995 gpiochip_irqchip_free_valid_mask(gc);
1999 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
2000 * @gc: the gpiochip to add the irqchip to
2001 * @domain: the irqdomain to add to the gpiochip
2003 * This function adds an IRQ domain to the gpiochip.
2005 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
2006 struct irq_domain *domain)
2008 return gpiochip_irqchip_add_allocated_domain(gc, domain, true);
2010 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
2012 #else /* CONFIG_GPIOLIB_IRQCHIP */
2014 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
2015 struct lock_class_key *lock_key,
2016 struct lock_class_key *request_key)
2020 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
2022 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
2027 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
2031 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
2034 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2037 * gpiochip_generic_request() - request the gpio function for a pin
2038 * @gc: the gpiochip owning the GPIO
2039 * @offset: the offset of the GPIO to request for GPIO function
2041 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
2043 #ifdef CONFIG_PINCTRL
2044 if (list_empty(&gc->gpiodev->pin_ranges))
2048 return pinctrl_gpio_request(gc, offset);
2050 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2053 * gpiochip_generic_free() - free the gpio function from a pin
2054 * @gc: the gpiochip to request the gpio function for
2055 * @offset: the offset of the GPIO to free from GPIO function
2057 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
2059 #ifdef CONFIG_PINCTRL
2060 if (list_empty(&gc->gpiodev->pin_ranges))
2064 pinctrl_gpio_free(gc, offset);
2066 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2069 * gpiochip_generic_config() - apply configuration for a pin
2070 * @gc: the gpiochip owning the GPIO
2071 * @offset: the offset of the GPIO to apply the configuration
2072 * @config: the configuration to be applied
2074 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
2075 unsigned long config)
2077 return pinctrl_gpio_set_config(gc, offset, config);
2079 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2081 #ifdef CONFIG_PINCTRL
2084 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2085 * @gc: the gpiochip to add the range for
2086 * @pctldev: the pin controller to map to
2087 * @gpio_offset: the start offset in the current gpio_chip number space
2088 * @pin_group: name of the pin group inside the pin controller
2090 * Calling this function directly from a DeviceTree-supported
2091 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2092 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2093 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2095 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
2096 struct pinctrl_dev *pctldev,
2097 unsigned int gpio_offset, const char *pin_group)
2099 struct gpio_pin_range *pin_range;
2100 struct gpio_device *gdev = gc->gpiodev;
2103 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2105 chip_err(gc, "failed to allocate pin ranges\n");
2109 /* Use local offset as range ID */
2110 pin_range->range.id = gpio_offset;
2111 pin_range->range.gc = gc;
2112 pin_range->range.name = gc->label;
2113 pin_range->range.base = gdev->base + gpio_offset;
2114 pin_range->pctldev = pctldev;
2116 ret = pinctrl_get_group_pins(pctldev, pin_group,
2117 &pin_range->range.pins,
2118 &pin_range->range.npins);
2124 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2126 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2127 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2128 pinctrl_dev_get_devname(pctldev), pin_group);
2130 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2134 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2137 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2138 * @gc: the gpiochip to add the range for
2139 * @pinctl_name: the dev_name() of the pin controller to map to
2140 * @gpio_offset: the start offset in the current gpio_chip number space
2141 * @pin_offset: the start offset in the pin controller number space
2142 * @npins: the number of pins from the offset of each pin space (GPIO and
2143 * pin controller) to accumulate in this range
2146 * 0 on success, or a negative error-code on failure.
2148 * Calling this function directly from a DeviceTree-supported
2149 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2150 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2151 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2153 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
2154 unsigned int gpio_offset, unsigned int pin_offset,
2157 struct gpio_pin_range *pin_range;
2158 struct gpio_device *gdev = gc->gpiodev;
2161 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2163 chip_err(gc, "failed to allocate pin ranges\n");
2167 /* Use local offset as range ID */
2168 pin_range->range.id = gpio_offset;
2169 pin_range->range.gc = gc;
2170 pin_range->range.name = gc->label;
2171 pin_range->range.base = gdev->base + gpio_offset;
2172 pin_range->range.pin_base = pin_offset;
2173 pin_range->range.npins = npins;
2174 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2176 if (IS_ERR(pin_range->pctldev)) {
2177 ret = PTR_ERR(pin_range->pctldev);
2178 chip_err(gc, "could not create pin range\n");
2182 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2183 gpio_offset, gpio_offset + npins - 1,
2185 pin_offset, pin_offset + npins - 1);
2187 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2191 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2194 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2195 * @gc: the chip to remove all the mappings for
2197 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
2199 struct gpio_pin_range *pin_range, *tmp;
2200 struct gpio_device *gdev = gc->gpiodev;
2202 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2203 list_del(&pin_range->node);
2204 pinctrl_remove_gpio_range(pin_range->pctldev,
2209 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2211 #endif /* CONFIG_PINCTRL */
2213 /* These "optional" allocation calls help prevent drivers from stomping
2214 * on each other, and help provide better diagnostics in debugfs.
2215 * They're called even less than the "set direction" calls.
2217 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2219 struct gpio_chip *gc = desc->gdev->chip;
2220 unsigned int offset;
2223 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags))
2227 label = kstrdup_const(label, GFP_KERNEL);
2232 /* NOTE: gpio_request() can be called in early boot,
2233 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2237 offset = gpio_chip_hwgpio(desc);
2238 if (gpiochip_line_is_valid(gc, offset))
2239 ret = gc->request(gc, offset);
2246 if (gc->get_direction)
2247 gpiod_get_direction(desc);
2249 ret = desc_set_label(desc, label ? : "?");
2256 clear_bit(FLAG_REQUESTED, &desc->flags);
2261 * This descriptor validation needs to be inserted verbatim into each
2262 * function taking a descriptor, so we need to use a preprocessor
2263 * macro to avoid endless duplication. If the desc is NULL it is an
2264 * optional GPIO and calls should just bail out.
2266 static int validate_desc(const struct gpio_desc *desc, const char *func)
2271 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2272 return PTR_ERR(desc);
2275 pr_warn("%s: invalid GPIO (no device)\n", func);
2278 if (!desc->gdev->chip) {
2279 dev_warn(&desc->gdev->dev,
2280 "%s: backing chip is gone\n", func);
2286 #define VALIDATE_DESC(desc) do { \
2287 int __valid = validate_desc(desc, __func__); \
2292 #define VALIDATE_DESC_VOID(desc) do { \
2293 int __valid = validate_desc(desc, __func__); \
2298 int gpiod_request(struct gpio_desc *desc, const char *label)
2300 int ret = -EPROBE_DEFER;
2302 VALIDATE_DESC(desc);
2304 if (try_module_get(desc->gdev->owner)) {
2305 ret = gpiod_request_commit(desc, label);
2307 module_put(desc->gdev->owner);
2309 gpio_device_get(desc->gdev);
2313 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2318 static bool gpiod_free_commit(struct gpio_desc *desc)
2320 struct gpio_chip *gc;
2321 unsigned long flags;
2326 gc = desc->gdev->chip;
2327 flags = READ_ONCE(desc->flags);
2329 if (gc && test_bit(FLAG_REQUESTED, &flags)) {
2331 gc->free(gc, gpio_chip_hwgpio(desc));
2333 clear_bit(FLAG_ACTIVE_LOW, &flags);
2334 clear_bit(FLAG_REQUESTED, &flags);
2335 clear_bit(FLAG_OPEN_DRAIN, &flags);
2336 clear_bit(FLAG_OPEN_SOURCE, &flags);
2337 clear_bit(FLAG_PULL_UP, &flags);
2338 clear_bit(FLAG_PULL_DOWN, &flags);
2339 clear_bit(FLAG_BIAS_DISABLE, &flags);
2340 clear_bit(FLAG_EDGE_RISING, &flags);
2341 clear_bit(FLAG_EDGE_FALLING, &flags);
2342 clear_bit(FLAG_IS_HOGGED, &flags);
2343 #ifdef CONFIG_OF_DYNAMIC
2344 WRITE_ONCE(desc->hog, NULL);
2347 desc_set_label(desc, NULL);
2348 WRITE_ONCE(desc->flags, flags);
2350 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED);
2356 void gpiod_free(struct gpio_desc *desc)
2359 * We must not use VALIDATE_DESC_VOID() as the underlying gdev->chip
2360 * may already be NULL but we still want to put the references.
2365 if (!gpiod_free_commit(desc))
2368 module_put(desc->gdev->owner);
2369 gpio_device_put(desc->gdev);
2373 * gpiochip_dup_line_label - Get a copy of the consumer label.
2374 * @gc: GPIO chip controlling this line.
2375 * @offset: Hardware offset of the line.
2378 * Pointer to a copy of the consumer label if the line is requested or NULL
2379 * if it's not. If a valid pointer was returned, it must be freed using
2380 * kfree(). In case of a memory allocation error, the function returns %ENOMEM.
2382 * Must not be called from atomic context.
2384 char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
2386 struct gpio_desc *desc;
2389 desc = gpiochip_get_desc(gc, offset);
2393 if (!test_bit(FLAG_REQUESTED, &desc->flags))
2396 guard(srcu)(&desc->srcu);
2398 label = kstrdup(gpiod_get_label(desc), GFP_KERNEL);
2400 return ERR_PTR(-ENOMEM);
2404 EXPORT_SYMBOL_GPL(gpiochip_dup_line_label);
2407 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2409 * @hwnum: hardware number of the GPIO for which to request the descriptor
2410 * @label: label for the GPIO
2411 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2412 * specify things like line inversion semantics with the machine flags
2413 * such as GPIO_OUT_LOW
2414 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2415 * can be used to specify consumer semantics such as open drain
2417 * Function allows GPIO chip drivers to request and use their own GPIO
2418 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2419 * function will not increase reference count of the GPIO chip module. This
2420 * allows the GPIO chip module to be unloaded as needed (we assume that the
2421 * GPIO chip driver handles freeing the GPIOs it has requested).
2424 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2427 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2430 enum gpio_lookup_flags lflags,
2431 enum gpiod_flags dflags)
2433 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2437 chip_err(gc, "failed to get GPIO descriptor\n");
2441 ret = gpiod_request_commit(desc, label);
2443 return ERR_PTR(ret);
2445 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2447 chip_err(gc, "setup of own GPIO %s failed\n", label);
2448 gpiod_free_commit(desc);
2449 return ERR_PTR(ret);
2454 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2457 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2458 * @desc: GPIO descriptor to free
2460 * Function frees the given GPIO requested previously with
2461 * gpiochip_request_own_desc().
2463 void gpiochip_free_own_desc(struct gpio_desc *desc)
2466 gpiod_free_commit(desc);
2468 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2471 * Drivers MUST set GPIO direction before making get/set calls. In
2472 * some cases this is done in early boot, before IRQs are enabled.
2474 * As a rule these aren't called more than once (except for drivers
2475 * using the open-drain emulation idiom) so these are natural places
2476 * to accumulate extra debugging checks. Note that we can't (yet)
2477 * rely on gpio_request() having been called beforehand.
2480 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2481 unsigned long config)
2483 if (!gc->set_config)
2486 return gc->set_config(gc, offset, config);
2489 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2490 enum pin_config_param mode,
2493 struct gpio_chip *gc = desc->gdev->chip;
2494 unsigned long config;
2496 config = pinconf_to_config_packed(mode, argument);
2497 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2500 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2501 enum pin_config_param mode,
2504 struct device *dev = &desc->gdev->dev;
2505 int gpio = gpio_chip_hwgpio(desc);
2508 ret = gpio_set_config_with_argument(desc, mode, argument);
2509 if (ret != -ENOTSUPP)
2513 case PIN_CONFIG_PERSIST_STATE:
2514 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2523 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2525 return gpio_set_config_with_argument(desc, mode, 0);
2528 static int gpio_set_bias(struct gpio_desc *desc)
2530 enum pin_config_param bias;
2533 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2534 bias = PIN_CONFIG_BIAS_DISABLE;
2535 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2536 bias = PIN_CONFIG_BIAS_PULL_UP;
2537 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2538 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2543 case PIN_CONFIG_BIAS_PULL_DOWN:
2544 case PIN_CONFIG_BIAS_PULL_UP:
2553 return gpio_set_config_with_argument_optional(desc, bias, arg);
2557 * gpio_set_debounce_timeout() - Set debounce timeout
2558 * @desc: GPIO descriptor to set the debounce timeout
2559 * @debounce: Debounce timeout in microseconds
2561 * The function calls the certain GPIO driver to set debounce timeout
2564 * Returns 0 on success, or negative error code otherwise.
2566 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2568 return gpio_set_config_with_argument_optional(desc,
2569 PIN_CONFIG_INPUT_DEBOUNCE,
2574 * gpiod_direction_input - set the GPIO direction to input
2575 * @desc: GPIO to set to input
2577 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2578 * be called safely on it.
2580 * Return 0 in case of success, else an error code.
2582 int gpiod_direction_input(struct gpio_desc *desc)
2584 struct gpio_chip *gc;
2587 VALIDATE_DESC(desc);
2588 gc = desc->gdev->chip;
2591 * It is legal to have no .get() and .direction_input() specified if
2592 * the chip is output-only, but you can't specify .direction_input()
2593 * and not support the .get() operation, that doesn't make sense.
2595 if (!gc->get && gc->direction_input) {
2597 "%s: missing get() but have direction_input()\n",
2603 * If we have a .direction_input() callback, things are simple,
2604 * just call it. Else we are some input-only chip so try to check the
2605 * direction (if .get_direction() is supported) else we silently
2606 * assume we are in input mode after this.
2608 if (gc->direction_input) {
2609 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2610 } else if (gc->get_direction &&
2611 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2613 "%s: missing direction_input() operation and line is output\n",
2618 clear_bit(FLAG_IS_OUT, &desc->flags);
2619 ret = gpio_set_bias(desc);
2622 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2626 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2628 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2630 struct gpio_chip *gc = desc->gdev->chip;
2635 * It's OK not to specify .direction_output() if the gpiochip is
2636 * output-only, but if there is then not even a .set() operation it
2637 * is pretty tricky to drive the output line.
2639 if (!gc->set && !gc->direction_output) {
2641 "%s: missing set() and direction_output() operations\n",
2646 if (gc->direction_output) {
2647 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2649 /* Check that we are in output mode if we can */
2650 if (gc->get_direction &&
2651 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2653 "%s: missing direction_output() operation\n",
2658 * If we can't actively set the direction, we are some
2659 * output-only chip, so just drive the output as desired.
2661 gc->set(gc, gpio_chip_hwgpio(desc), val);
2665 set_bit(FLAG_IS_OUT, &desc->flags);
2666 trace_gpio_value(desc_to_gpio(desc), 0, val);
2667 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2672 * gpiod_direction_output_raw - set the GPIO direction to output
2673 * @desc: GPIO to set to output
2674 * @value: initial output value of the GPIO
2676 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2677 * be called safely on it. The initial value of the output must be specified
2678 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2680 * Return 0 in case of success, else an error code.
2682 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2684 VALIDATE_DESC(desc);
2685 return gpiod_direction_output_raw_commit(desc, value);
2687 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2690 * gpiod_direction_output - set the GPIO direction to output
2691 * @desc: GPIO to set to output
2692 * @value: initial output value of the GPIO
2694 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2695 * be called safely on it. The initial value of the output must be specified
2696 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2699 * Return 0 in case of success, else an error code.
2701 int gpiod_direction_output(struct gpio_desc *desc, int value)
2705 VALIDATE_DESC(desc);
2706 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2711 /* GPIOs used for enabled IRQs shall not be set as output */
2712 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2713 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2715 "%s: tried to set a GPIO tied to an IRQ as output\n",
2720 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2721 /* First see if we can enable open drain in hardware */
2722 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2724 goto set_output_value;
2725 /* Emulate open drain by not actively driving the line high */
2727 ret = gpiod_direction_input(desc);
2728 goto set_output_flag;
2730 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2731 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2733 goto set_output_value;
2734 /* Emulate open source by not actively driving the line low */
2736 ret = gpiod_direction_input(desc);
2737 goto set_output_flag;
2740 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2744 ret = gpio_set_bias(desc);
2747 return gpiod_direction_output_raw_commit(desc, value);
2751 * When emulating open-source or open-drain functionalities by not
2752 * actively driving the line (setting mode to input) we still need to
2753 * set the IS_OUT flag or otherwise we won't be able to set the line
2757 set_bit(FLAG_IS_OUT, &desc->flags);
2760 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2763 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2765 * @desc: GPIO to enable.
2766 * @flags: Flags related to GPIO edge.
2768 * Return 0 in case of success, else negative error code.
2770 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2773 struct gpio_chip *gc;
2775 VALIDATE_DESC(desc);
2777 gc = desc->gdev->chip;
2778 if (!gc->en_hw_timestamp) {
2779 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2783 ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2785 gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
2789 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
2792 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2794 * @desc: GPIO to disable.
2795 * @flags: Flags related to GPIO edge, same value as used during enable call.
2797 * Return 0 in case of success, else negative error code.
2799 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2802 struct gpio_chip *gc;
2804 VALIDATE_DESC(desc);
2806 gc = desc->gdev->chip;
2807 if (!gc->dis_hw_timestamp) {
2808 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2812 ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
2814 gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
2818 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
2821 * gpiod_set_config - sets @config for a GPIO
2822 * @desc: descriptor of the GPIO for which to set the configuration
2823 * @config: Same packed config format as generic pinconf
2826 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2829 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2831 struct gpio_chip *gc;
2833 VALIDATE_DESC(desc);
2834 gc = desc->gdev->chip;
2836 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2838 EXPORT_SYMBOL_GPL(gpiod_set_config);
2841 * gpiod_set_debounce - sets @debounce time for a GPIO
2842 * @desc: descriptor of the GPIO for which to set debounce time
2843 * @debounce: debounce time in microseconds
2846 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2849 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2851 unsigned long config;
2853 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2854 return gpiod_set_config(desc, config);
2856 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2859 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2860 * @desc: descriptor of the GPIO for which to configure persistence
2861 * @transitory: True to lose state on suspend or reset, false for persistence
2864 * 0 on success, otherwise a negative error code.
2866 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2868 VALIDATE_DESC(desc);
2870 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2871 * persistence state.
2873 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2875 /* If the driver supports it, set the persistence state now */
2876 return gpio_set_config_with_argument_optional(desc,
2877 PIN_CONFIG_PERSIST_STATE,
2882 * gpiod_is_active_low - test whether a GPIO is active-low or not
2883 * @desc: the gpio descriptor to test
2885 * Returns 1 if the GPIO is active-low, 0 otherwise.
2887 int gpiod_is_active_low(const struct gpio_desc *desc)
2889 VALIDATE_DESC(desc);
2890 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2892 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2895 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2896 * @desc: the gpio descriptor to change
2898 void gpiod_toggle_active_low(struct gpio_desc *desc)
2900 VALIDATE_DESC_VOID(desc);
2901 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2903 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2905 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
2907 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
2910 /* I/O calls are only valid after configuration completed; the relevant
2911 * "is this a valid GPIO" error checks should already have been done.
2913 * "Get" operations are often inlinable as reading a pin value register,
2914 * and masking the relevant bit in that register.
2916 * When "set" operations are inlinable, they involve writing that mask to
2917 * one register to set a low value, or a different register to set it high.
2918 * Otherwise locking is needed, so there may be little value to inlining.
2920 *------------------------------------------------------------------------
2922 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2923 * have requested the GPIO. That can include implicit requesting by
2924 * a direction setting call. Marking a gpio as requested locks its chip
2925 * in memory, guaranteeing that these table lookups need no more locking
2926 * and that gpiochip_remove() will fail.
2928 * REVISIT when debugging, consider adding some instrumentation to ensure
2929 * that the GPIO was actually requested.
2932 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2934 struct gpio_chip *gc;
2937 gc = desc->gdev->chip;
2938 value = gpio_chip_get_value(gc, desc);
2939 value = value < 0 ? value : !!value;
2940 trace_gpio_value(desc_to_gpio(desc), 1, value);
2944 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2945 unsigned long *mask, unsigned long *bits)
2947 if (gc->get_multiple)
2948 return gc->get_multiple(gc, mask, bits);
2952 for_each_set_bit(i, mask, gc->ngpio) {
2953 value = gc->get(gc, i);
2956 __assign_bit(i, bits, value);
2963 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2964 unsigned int array_size,
2965 struct gpio_desc **desc_array,
2966 struct gpio_array *array_info,
2967 unsigned long *value_bitmap)
2972 * Validate array_info against desc_array and its size.
2973 * It should immediately follow desc_array if both
2974 * have been obtained from the same gpiod_get_array() call.
2976 if (array_info && array_info->desc == desc_array &&
2977 array_size <= array_info->size &&
2978 (void *)array_info == desc_array + array_info->size) {
2980 WARN_ON(array_info->chip->can_sleep);
2982 ret = gpio_chip_get_multiple(array_info->chip,
2983 array_info->get_mask,
2988 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2989 bitmap_xor(value_bitmap, value_bitmap,
2990 array_info->invert_mask, array_size);
2992 i = find_first_zero_bit(array_info->get_mask, array_size);
2993 if (i == array_size)
2999 while (i < array_size) {
3000 struct gpio_chip *gc = desc_array[i]->gdev->chip;
3001 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3002 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3003 unsigned long *mask, *bits;
3006 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
3007 mask = fastpath_mask;
3008 bits = fastpath_bits;
3010 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3012 mask = bitmap_alloc(gc->ngpio, flags);
3016 bits = bitmap_alloc(gc->ngpio, flags);
3023 bitmap_zero(mask, gc->ngpio);
3026 WARN_ON(gc->can_sleep);
3028 /* collect all inputs belonging to the same chip */
3031 const struct gpio_desc *desc = desc_array[i];
3032 int hwgpio = gpio_chip_hwgpio(desc);
3034 __set_bit(hwgpio, mask);
3038 i = find_next_zero_bit(array_info->get_mask,
3040 } while ((i < array_size) &&
3041 (desc_array[i]->gdev->chip == gc));
3043 ret = gpio_chip_get_multiple(gc, mask, bits);
3045 if (mask != fastpath_mask)
3047 if (bits != fastpath_bits)
3052 for (j = first; j < i; ) {
3053 const struct gpio_desc *desc = desc_array[j];
3054 int hwgpio = gpio_chip_hwgpio(desc);
3055 int value = test_bit(hwgpio, bits);
3057 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3059 __assign_bit(j, value_bitmap, value);
3060 trace_gpio_value(desc_to_gpio(desc), 1, value);
3064 j = find_next_zero_bit(array_info->get_mask, i,
3068 if (mask != fastpath_mask)
3070 if (bits != fastpath_bits)
3077 * gpiod_get_raw_value() - return a gpio's raw value
3078 * @desc: gpio whose value will be returned
3080 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3081 * its ACTIVE_LOW status, or negative errno on failure.
3083 * This function can be called from contexts where we cannot sleep, and will
3084 * complain if the GPIO chip functions potentially sleep.
3086 int gpiod_get_raw_value(const struct gpio_desc *desc)
3088 VALIDATE_DESC(desc);
3089 /* Should be using gpiod_get_raw_value_cansleep() */
3090 WARN_ON(desc->gdev->chip->can_sleep);
3091 return gpiod_get_raw_value_commit(desc);
3093 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
3096 * gpiod_get_value() - return a gpio's value
3097 * @desc: gpio whose value will be returned
3099 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3100 * account, or negative errno on failure.
3102 * This function can be called from contexts where we cannot sleep, and will
3103 * complain if the GPIO chip functions potentially sleep.
3105 int gpiod_get_value(const struct gpio_desc *desc)
3109 VALIDATE_DESC(desc);
3110 /* Should be using gpiod_get_value_cansleep() */
3111 WARN_ON(desc->gdev->chip->can_sleep);
3113 value = gpiod_get_raw_value_commit(desc);
3117 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3122 EXPORT_SYMBOL_GPL(gpiod_get_value);
3125 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3126 * @array_size: number of elements in the descriptor array / value bitmap
3127 * @desc_array: array of GPIO descriptors whose values will be read
3128 * @array_info: information on applicability of fast bitmap processing path
3129 * @value_bitmap: bitmap to store the read values
3131 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3132 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3133 * else an error code.
3135 * This function can be called from contexts where we cannot sleep,
3136 * and it will complain if the GPIO chip functions potentially sleep.
3138 int gpiod_get_raw_array_value(unsigned int array_size,
3139 struct gpio_desc **desc_array,
3140 struct gpio_array *array_info,
3141 unsigned long *value_bitmap)
3145 return gpiod_get_array_value_complex(true, false, array_size,
3146 desc_array, array_info,
3149 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3152 * gpiod_get_array_value() - read values from an array of GPIOs
3153 * @array_size: number of elements in the descriptor array / value bitmap
3154 * @desc_array: array of GPIO descriptors whose values will be read
3155 * @array_info: information on applicability of fast bitmap processing path
3156 * @value_bitmap: bitmap to store the read values
3158 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3159 * into account. Return 0 in case of success, else an error code.
3161 * This function can be called from contexts where we cannot sleep,
3162 * and it will complain if the GPIO chip functions potentially sleep.
3164 int gpiod_get_array_value(unsigned int array_size,
3165 struct gpio_desc **desc_array,
3166 struct gpio_array *array_info,
3167 unsigned long *value_bitmap)
3171 return gpiod_get_array_value_complex(false, false, array_size,
3172 desc_array, array_info,
3175 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3178 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3179 * @desc: gpio descriptor whose state need to be set.
3180 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3182 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
3185 struct gpio_chip *gc = desc->gdev->chip;
3186 int offset = gpio_chip_hwgpio(desc);
3189 ret = gc->direction_input(gc, offset);
3191 ret = gc->direction_output(gc, offset, 0);
3193 set_bit(FLAG_IS_OUT, &desc->flags);
3195 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3198 "%s: Error in set_value for open drain err %d\n",
3203 * _gpio_set_open_source_value() - Set the open source gpio's value.
3204 * @desc: gpio descriptor whose state need to be set.
3205 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3207 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3210 struct gpio_chip *gc = desc->gdev->chip;
3211 int offset = gpio_chip_hwgpio(desc);
3214 ret = gc->direction_output(gc, offset, 1);
3216 set_bit(FLAG_IS_OUT, &desc->flags);
3218 ret = gc->direction_input(gc, offset);
3220 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3223 "%s: Error in set_value for open source err %d\n",
3227 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3229 struct gpio_chip *gc;
3231 gc = desc->gdev->chip;
3232 trace_gpio_value(desc_to_gpio(desc), 0, value);
3233 gc->set(gc, gpio_chip_hwgpio(desc), value);
3237 * set multiple outputs on the same chip;
3238 * use the chip's set_multiple function if available;
3239 * otherwise set the outputs sequentially;
3240 * @chip: the GPIO chip we operate on
3241 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3242 * defines which outputs are to be changed
3243 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3244 * defines the values the outputs specified by mask are to be set to
3246 static void gpio_chip_set_multiple(struct gpio_chip *gc,
3247 unsigned long *mask, unsigned long *bits)
3249 if (gc->set_multiple) {
3250 gc->set_multiple(gc, mask, bits);
3254 /* set outputs if the corresponding mask bit is set */
3255 for_each_set_bit(i, mask, gc->ngpio)
3256 gc->set(gc, i, test_bit(i, bits));
3260 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3261 unsigned int array_size,
3262 struct gpio_desc **desc_array,
3263 struct gpio_array *array_info,
3264 unsigned long *value_bitmap)
3269 * Validate array_info against desc_array and its size.
3270 * It should immediately follow desc_array if both
3271 * have been obtained from the same gpiod_get_array() call.
3273 if (array_info && array_info->desc == desc_array &&
3274 array_size <= array_info->size &&
3275 (void *)array_info == desc_array + array_info->size) {
3277 WARN_ON(array_info->chip->can_sleep);
3279 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3280 bitmap_xor(value_bitmap, value_bitmap,
3281 array_info->invert_mask, array_size);
3283 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3286 i = find_first_zero_bit(array_info->set_mask, array_size);
3287 if (i == array_size)
3293 while (i < array_size) {
3294 struct gpio_chip *gc = desc_array[i]->gdev->chip;
3295 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3296 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3297 unsigned long *mask, *bits;
3300 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
3301 mask = fastpath_mask;
3302 bits = fastpath_bits;
3304 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3306 mask = bitmap_alloc(gc->ngpio, flags);
3310 bits = bitmap_alloc(gc->ngpio, flags);
3317 bitmap_zero(mask, gc->ngpio);
3320 WARN_ON(gc->can_sleep);
3323 struct gpio_desc *desc = desc_array[i];
3324 int hwgpio = gpio_chip_hwgpio(desc);
3325 int value = test_bit(i, value_bitmap);
3328 * Pins applicable for fast input but not for
3329 * fast output processing may have been already
3330 * inverted inside the fast path, skip them.
3332 if (!raw && !(array_info &&
3333 test_bit(i, array_info->invert_mask)) &&
3334 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3336 trace_gpio_value(desc_to_gpio(desc), 0, value);
3338 * collect all normal outputs belonging to the same chip
3339 * open drain and open source outputs are set individually
3341 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3342 gpio_set_open_drain_value_commit(desc, value);
3343 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3344 gpio_set_open_source_value_commit(desc, value);
3346 __set_bit(hwgpio, mask);
3347 __assign_bit(hwgpio, bits, value);
3353 i = find_next_zero_bit(array_info->set_mask,
3355 } while ((i < array_size) &&
3356 (desc_array[i]->gdev->chip == gc));
3357 /* push collected bits to outputs */
3359 gpio_chip_set_multiple(gc, mask, bits);
3361 if (mask != fastpath_mask)
3363 if (bits != fastpath_bits)
3370 * gpiod_set_raw_value() - assign a gpio's raw value
3371 * @desc: gpio whose value will be assigned
3372 * @value: value to assign
3374 * Set the raw value of the GPIO, i.e. the value of its physical line without
3375 * regard for its ACTIVE_LOW status.
3377 * This function can be called from contexts where we cannot sleep, and will
3378 * complain if the GPIO chip functions potentially sleep.
3380 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3382 VALIDATE_DESC_VOID(desc);
3383 /* Should be using gpiod_set_raw_value_cansleep() */
3384 WARN_ON(desc->gdev->chip->can_sleep);
3385 gpiod_set_raw_value_commit(desc, value);
3387 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3390 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3391 * @desc: the descriptor to set the value on
3392 * @value: value to set
3394 * This sets the value of a GPIO line backing a descriptor, applying
3395 * different semantic quirks like active low and open drain/source
3398 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3400 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3402 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3403 gpio_set_open_drain_value_commit(desc, value);
3404 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3405 gpio_set_open_source_value_commit(desc, value);
3407 gpiod_set_raw_value_commit(desc, value);
3411 * gpiod_set_value() - assign a gpio's value
3412 * @desc: gpio whose value will be assigned
3413 * @value: value to assign
3415 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3416 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3418 * This function can be called from contexts where we cannot sleep, and will
3419 * complain if the GPIO chip functions potentially sleep.
3421 void gpiod_set_value(struct gpio_desc *desc, int value)
3423 VALIDATE_DESC_VOID(desc);
3424 /* Should be using gpiod_set_value_cansleep() */
3425 WARN_ON(desc->gdev->chip->can_sleep);
3426 gpiod_set_value_nocheck(desc, value);
3428 EXPORT_SYMBOL_GPL(gpiod_set_value);
3431 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3432 * @array_size: number of elements in the descriptor array / value bitmap
3433 * @desc_array: array of GPIO descriptors whose values will be assigned
3434 * @array_info: information on applicability of fast bitmap processing path
3435 * @value_bitmap: bitmap of values to assign
3437 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3438 * without regard for their ACTIVE_LOW status.
3440 * This function can be called from contexts where we cannot sleep, and will
3441 * complain if the GPIO chip functions potentially sleep.
3443 int gpiod_set_raw_array_value(unsigned int array_size,
3444 struct gpio_desc **desc_array,
3445 struct gpio_array *array_info,
3446 unsigned long *value_bitmap)
3450 return gpiod_set_array_value_complex(true, false, array_size,
3451 desc_array, array_info, value_bitmap);
3453 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3456 * gpiod_set_array_value() - assign values to an array of GPIOs
3457 * @array_size: number of elements in the descriptor array / value bitmap
3458 * @desc_array: array of GPIO descriptors whose values will be assigned
3459 * @array_info: information on applicability of fast bitmap processing path
3460 * @value_bitmap: bitmap of values to assign
3462 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3465 * This function can be called from contexts where we cannot sleep, and will
3466 * complain if the GPIO chip functions potentially sleep.
3468 int gpiod_set_array_value(unsigned int array_size,
3469 struct gpio_desc **desc_array,
3470 struct gpio_array *array_info,
3471 unsigned long *value_bitmap)
3475 return gpiod_set_array_value_complex(false, false, array_size,
3476 desc_array, array_info,
3479 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3482 * gpiod_cansleep() - report whether gpio value access may sleep
3483 * @desc: gpio to check
3486 int gpiod_cansleep(const struct gpio_desc *desc)
3488 VALIDATE_DESC(desc);
3489 return desc->gdev->chip->can_sleep;
3491 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3494 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3495 * @desc: gpio to set the consumer name on
3496 * @name: the new consumer name
3498 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3500 VALIDATE_DESC(desc);
3502 return desc_set_label(desc, name);
3504 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3507 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3508 * @desc: gpio whose IRQ will be returned (already requested)
3510 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3513 int gpiod_to_irq(const struct gpio_desc *desc)
3515 struct gpio_chip *gc;
3519 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3520 * requires this function to not return zero on an invalid descriptor
3521 * but rather a negative error number.
3523 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3526 gc = desc->gdev->chip;
3527 offset = gpio_chip_hwgpio(desc);
3529 int retirq = gc->to_irq(gc, offset);
3531 /* Zero means NO_IRQ */
3537 #ifdef CONFIG_GPIOLIB_IRQCHIP
3540 * Avoid race condition with other code, which tries to lookup
3541 * an IRQ before the irqchip has been properly registered,
3542 * i.e. while gpiochip is still being brought up.
3544 return -EPROBE_DEFER;
3549 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3552 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3553 * @gc: the chip the GPIO to lock belongs to
3554 * @offset: the offset of the GPIO to lock as IRQ
3556 * This is used directly by GPIO drivers that want to lock down
3557 * a certain GPIO line to be used for IRQs.
3559 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3561 struct gpio_desc *desc;
3563 desc = gpiochip_get_desc(gc, offset);
3565 return PTR_ERR(desc);
3568 * If it's fast: flush the direction setting if something changed
3571 if (!gc->can_sleep && gc->get_direction) {
3572 int dir = gpiod_get_direction(desc);
3575 chip_err(gc, "%s: cannot get GPIO direction\n",
3581 /* To be valid for IRQ the line needs to be input or open drain */
3582 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3583 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3585 "%s: tried to flag a GPIO set as output for IRQ\n",
3590 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3591 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3595 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3598 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3599 * @gc: the chip the GPIO to lock belongs to
3600 * @offset: the offset of the GPIO to lock as IRQ
3602 * This is used directly by GPIO drivers that want to indicate
3603 * that a certain GPIO is no longer used exclusively for IRQ.
3605 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3607 struct gpio_desc *desc;
3609 desc = gpiochip_get_desc(gc, offset);
3613 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3614 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3616 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3618 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3620 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3622 if (!IS_ERR(desc) &&
3623 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3624 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3626 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3628 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3630 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3632 if (!IS_ERR(desc) &&
3633 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3635 * We must not be output when using IRQ UNLESS we are
3638 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3639 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3640 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3643 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3645 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3647 if (offset >= gc->ngpio)
3650 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3652 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3654 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3658 if (!try_module_get(gc->gpiodev->owner))
3661 ret = gpiochip_lock_as_irq(gc, offset);
3663 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3664 module_put(gc->gpiodev->owner);
3669 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3671 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3673 gpiochip_unlock_as_irq(gc, offset);
3674 module_put(gc->gpiodev->owner);
3676 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3678 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3680 if (offset >= gc->ngpio)
3683 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3685 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3687 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3689 if (offset >= gc->ngpio)
3692 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3694 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3696 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3698 if (offset >= gc->ngpio)
3701 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3703 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3706 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3707 * @desc: gpio whose value will be returned
3709 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3710 * its ACTIVE_LOW status, or negative errno on failure.
3712 * This function is to be called from contexts that can sleep.
3714 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3717 VALIDATE_DESC(desc);
3718 return gpiod_get_raw_value_commit(desc);
3720 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3723 * gpiod_get_value_cansleep() - return a gpio's value
3724 * @desc: gpio whose value will be returned
3726 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3727 * account, or negative errno on failure.
3729 * This function is to be called from contexts that can sleep.
3731 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3736 VALIDATE_DESC(desc);
3737 value = gpiod_get_raw_value_commit(desc);
3741 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3746 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3749 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3750 * @array_size: number of elements in the descriptor array / value bitmap
3751 * @desc_array: array of GPIO descriptors whose values will be read
3752 * @array_info: information on applicability of fast bitmap processing path
3753 * @value_bitmap: bitmap to store the read values
3755 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3756 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3757 * else an error code.
3759 * This function is to be called from contexts that can sleep.
3761 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3762 struct gpio_desc **desc_array,
3763 struct gpio_array *array_info,
3764 unsigned long *value_bitmap)
3769 return gpiod_get_array_value_complex(true, true, array_size,
3770 desc_array, array_info,
3773 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3776 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3777 * @array_size: number of elements in the descriptor array / value bitmap
3778 * @desc_array: array of GPIO descriptors whose values will be read
3779 * @array_info: information on applicability of fast bitmap processing path
3780 * @value_bitmap: bitmap to store the read values
3782 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3783 * into account. Return 0 in case of success, else an error code.
3785 * This function is to be called from contexts that can sleep.
3787 int gpiod_get_array_value_cansleep(unsigned int array_size,
3788 struct gpio_desc **desc_array,
3789 struct gpio_array *array_info,
3790 unsigned long *value_bitmap)
3795 return gpiod_get_array_value_complex(false, true, array_size,
3796 desc_array, array_info,
3799 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3802 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3803 * @desc: gpio whose value will be assigned
3804 * @value: value to assign
3806 * Set the raw value of the GPIO, i.e. the value of its physical line without
3807 * regard for its ACTIVE_LOW status.
3809 * This function is to be called from contexts that can sleep.
3811 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3814 VALIDATE_DESC_VOID(desc);
3815 gpiod_set_raw_value_commit(desc, value);
3817 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3820 * gpiod_set_value_cansleep() - assign a gpio's value
3821 * @desc: gpio whose value will be assigned
3822 * @value: value to assign
3824 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3827 * This function is to be called from contexts that can sleep.
3829 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3832 VALIDATE_DESC_VOID(desc);
3833 gpiod_set_value_nocheck(desc, value);
3835 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3838 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3839 * @array_size: number of elements in the descriptor array / value bitmap
3840 * @desc_array: array of GPIO descriptors whose values will be assigned
3841 * @array_info: information on applicability of fast bitmap processing path
3842 * @value_bitmap: bitmap of values to assign
3844 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3845 * without regard for their ACTIVE_LOW status.
3847 * This function is to be called from contexts that can sleep.
3849 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3850 struct gpio_desc **desc_array,
3851 struct gpio_array *array_info,
3852 unsigned long *value_bitmap)
3857 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3858 array_info, value_bitmap);
3860 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3863 * gpiod_add_lookup_tables() - register GPIO device consumers
3864 * @tables: list of tables of consumers to register
3865 * @n: number of tables in the list
3867 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3871 mutex_lock(&gpio_lookup_lock);
3873 for (i = 0; i < n; i++)
3874 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3876 mutex_unlock(&gpio_lookup_lock);
3880 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3881 * @array_size: number of elements in the descriptor array / value bitmap
3882 * @desc_array: array of GPIO descriptors whose values will be assigned
3883 * @array_info: information on applicability of fast bitmap processing path
3884 * @value_bitmap: bitmap of values to assign
3886 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3889 * This function is to be called from contexts that can sleep.
3891 int gpiod_set_array_value_cansleep(unsigned int array_size,
3892 struct gpio_desc **desc_array,
3893 struct gpio_array *array_info,
3894 unsigned long *value_bitmap)
3899 return gpiod_set_array_value_complex(false, true, array_size,
3900 desc_array, array_info,
3903 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3905 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action)
3907 blocking_notifier_call_chain(&desc->gdev->line_state_notifier,
3912 * gpiod_add_lookup_table() - register GPIO device consumers
3913 * @table: table of consumers to register
3915 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3917 gpiod_add_lookup_tables(&table, 1);
3919 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3922 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3923 * @table: table of consumers to unregister
3925 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3927 /* Nothing to remove */
3931 mutex_lock(&gpio_lookup_lock);
3933 list_del(&table->list);
3935 mutex_unlock(&gpio_lookup_lock);
3937 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3940 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3941 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3943 void gpiod_add_hogs(struct gpiod_hog *hogs)
3945 struct gpiod_hog *hog;
3947 mutex_lock(&gpio_machine_hogs_mutex);
3949 for (hog = &hogs[0]; hog->chip_label; hog++) {
3950 list_add_tail(&hog->list, &gpio_machine_hogs);
3953 * The chip may have been registered earlier, so check if it
3954 * exists and, if so, try to hog the line now.
3956 struct gpio_device *gdev __free(gpio_device_put) =
3957 gpio_device_find_by_label(hog->chip_label);
3959 gpiochip_machine_hog(gpio_device_get_chip(gdev), hog);
3962 mutex_unlock(&gpio_machine_hogs_mutex);
3964 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3966 void gpiod_remove_hogs(struct gpiod_hog *hogs)
3968 struct gpiod_hog *hog;
3970 mutex_lock(&gpio_machine_hogs_mutex);
3971 for (hog = &hogs[0]; hog->chip_label; hog++)
3972 list_del(&hog->list);
3973 mutex_unlock(&gpio_machine_hogs_mutex);
3975 EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
3977 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3979 const char *dev_id = dev ? dev_name(dev) : NULL;
3980 struct gpiod_lookup_table *table;
3982 list_for_each_entry(table, &gpio_lookup_list, list) {
3983 if (table->dev_id && dev_id) {
3985 * Valid strings on both ends, must be identical to have
3988 if (!strcmp(table->dev_id, dev_id))
3992 * One of the pointers is NULL, so both must be to have
3995 if (dev_id == table->dev_id)
4003 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
4004 unsigned int idx, unsigned long *flags)
4006 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4007 struct gpiod_lookup_table *table;
4008 struct gpiod_lookup *p;
4009 struct gpio_chip *gc;
4011 guard(mutex)(&gpio_lookup_lock);
4013 table = gpiod_find_lookup_table(dev);
4017 for (p = &table->table[0]; p->key; p++) {
4018 /* idx must always match exactly */
4022 /* If the lookup entry has a con_id, require exact match */
4023 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4026 if (p->chip_hwnum == U16_MAX) {
4027 desc = gpio_name_to_desc(p->key);
4033 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
4035 return ERR_PTR(-EPROBE_DEFER);
4038 struct gpio_device *gdev __free(gpio_device_put) =
4039 gpio_device_find_by_label(p->key);
4042 * As the lookup table indicates a chip with
4043 * p->key should exist, assume it may
4044 * still appear later and let the interested
4045 * consumer be probed again or let the Deferred
4046 * Probe infrastructure handle the error.
4048 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4050 return ERR_PTR(-EPROBE_DEFER);
4053 gc = gpio_device_get_chip(gdev);
4055 if (gc->ngpio <= p->chip_hwnum) {
4057 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4058 idx, p->chip_hwnum, gc->ngpio - 1,
4060 return ERR_PTR(-EINVAL);
4063 desc = gpio_device_get_desc(gdev, p->chip_hwnum);
4072 static int platform_gpio_count(struct device *dev, const char *con_id)
4074 struct gpiod_lookup_table *table;
4075 struct gpiod_lookup *p;
4076 unsigned int count = 0;
4078 scoped_guard(mutex, &gpio_lookup_lock) {
4079 table = gpiod_find_lookup_table(dev);
4083 for (p = &table->table[0]; p->key; p++) {
4084 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4085 (!con_id && !p->con_id))
4096 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
4097 struct device *consumer,
4100 enum gpiod_flags *flags,
4101 unsigned long *lookupflags)
4103 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4105 if (is_of_node(fwnode)) {
4106 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n",
4108 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags);
4109 } else if (is_acpi_node(fwnode)) {
4110 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n",
4112 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
4113 } else if (is_software_node(fwnode)) {
4114 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n",
4116 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
4122 static struct gpio_desc *gpiod_find_and_request(struct device *consumer,
4123 struct fwnode_handle *fwnode,
4126 enum gpiod_flags flags,
4128 bool platform_lookup_allowed)
4130 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4132 * scoped_guard() is implemented as a for loop, meaning static
4133 * analyzers will complain about these two not being initialized.
4135 struct gpio_desc *desc = NULL;
4138 scoped_guard(srcu, &gpio_devices_srcu) {
4139 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx,
4140 &flags, &lookupflags);
4141 if (gpiod_not_found(desc) && platform_lookup_allowed) {
4143 * Either we are not using DT or ACPI, or their lookup
4144 * did not return a result. In that case, use platform
4145 * lookup as a fallback.
4148 "using lookup tables for GPIO lookup\n");
4149 desc = gpiod_find(consumer, con_id, idx, &lookupflags);
4153 dev_dbg(consumer, "No GPIO consumer %s found\n",
4159 * If a connection label was passed use that, else attempt to use
4160 * the device name as label
4162 ret = gpiod_request(desc, label);
4165 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
4166 return ERR_PTR(ret);
4169 * This happens when there are several consumers for
4170 * the same GPIO line: we just return here without
4171 * further initialization. It is a bit of a hack.
4172 * This is necessary to support fixed regulators.
4174 * FIXME: Make this more sane and safe.
4177 "nonexclusive access to GPIO for %s\n", con_id);
4181 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4183 dev_dbg(consumer, "setup of GPIO %s failed\n", con_id);
4185 return ERR_PTR(ret);
4188 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_REQUESTED);
4194 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4195 * @fwnode: handle of the firmware node
4196 * @con_id: function within the GPIO consumer
4197 * @index: index of the GPIO to obtain for the consumer
4198 * @flags: GPIO initialization flags
4199 * @label: label to attach to the requested GPIO
4201 * This function can be used for drivers that get their configuration
4202 * from opaque firmware.
4204 * The function properly finds the corresponding GPIO using whatever is the
4205 * underlying firmware interface and then makes sure that the GPIO
4206 * descriptor is requested before it is returned to the caller.
4209 * On successful request the GPIO pin is configured in accordance with
4212 * In case of error an ERR_PTR() is returned.
4214 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4217 enum gpiod_flags flags,
4220 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false);
4222 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4225 * gpiod_count - return the number of GPIOs associated with a device / function
4226 * or -ENOENT if no GPIO has been assigned to the requested function
4227 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4228 * @con_id: function within the GPIO consumer
4230 int gpiod_count(struct device *dev, const char *con_id)
4232 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4233 int count = -ENOENT;
4235 if (is_of_node(fwnode))
4236 count = of_gpio_get_count(dev, con_id);
4237 else if (is_acpi_node(fwnode))
4238 count = acpi_gpio_count(dev, con_id);
4239 else if (is_software_node(fwnode))
4240 count = swnode_gpio_count(fwnode, con_id);
4243 count = platform_gpio_count(dev, con_id);
4247 EXPORT_SYMBOL_GPL(gpiod_count);
4250 * gpiod_get - obtain a GPIO for a given GPIO function
4251 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4252 * @con_id: function within the GPIO consumer
4253 * @flags: optional GPIO initialization flags
4255 * Return the GPIO descriptor corresponding to the function con_id of device
4256 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4257 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4259 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4260 enum gpiod_flags flags)
4262 return gpiod_get_index(dev, con_id, 0, flags);
4264 EXPORT_SYMBOL_GPL(gpiod_get);
4267 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4268 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4269 * @con_id: function within the GPIO consumer
4270 * @flags: optional GPIO initialization flags
4272 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4273 * the requested function it will return NULL. This is convenient for drivers
4274 * that need to handle optional GPIOs.
4276 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4278 enum gpiod_flags flags)
4280 return gpiod_get_index_optional(dev, con_id, 0, flags);
4282 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4286 * gpiod_configure_flags - helper function to configure a given GPIO
4287 * @desc: gpio whose value will be assigned
4288 * @con_id: function within the GPIO consumer
4289 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4290 * of_find_gpio() or of_get_gpio_hog()
4291 * @dflags: gpiod_flags - optional GPIO initialization flags
4293 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4294 * requested function and/or index, or another IS_ERR() code if an error
4295 * occurred while trying to acquire the GPIO.
4297 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4298 unsigned long lflags, enum gpiod_flags dflags)
4302 if (lflags & GPIO_ACTIVE_LOW)
4303 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4305 if (lflags & GPIO_OPEN_DRAIN)
4306 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4307 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4309 * This enforces open drain mode from the consumer side.
4310 * This is necessary for some busses like I2C, but the lookup
4311 * should *REALLY* have specified them as open drain in the
4312 * first place, so print a little warning here.
4314 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4316 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4319 if (lflags & GPIO_OPEN_SOURCE)
4320 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4322 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
4323 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
4324 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
4326 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4330 if (lflags & GPIO_PULL_UP)
4331 set_bit(FLAG_PULL_UP, &desc->flags);
4332 else if (lflags & GPIO_PULL_DOWN)
4333 set_bit(FLAG_PULL_DOWN, &desc->flags);
4334 else if (lflags & GPIO_PULL_DISABLE)
4335 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
4337 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4341 /* No particular flag request, return here... */
4342 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4343 gpiod_dbg(desc, "no flags found for %s\n", con_id);
4348 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4349 ret = gpiod_direction_output(desc,
4350 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4352 ret = gpiod_direction_input(desc);
4358 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4359 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4360 * @con_id: function within the GPIO consumer
4361 * @idx: index of the GPIO to obtain in the consumer
4362 * @flags: optional GPIO initialization flags
4364 * This variant of gpiod_get() allows to access GPIOs other than the first
4365 * defined one for functions that define several GPIOs.
4367 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4368 * requested function and/or index, or another IS_ERR() code if an error
4369 * occurred while trying to acquire the GPIO.
4371 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4374 enum gpiod_flags flags)
4376 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4377 const char *devname = dev ? dev_name(dev) : "?";
4378 const char *label = con_id ?: devname;
4380 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true);
4382 EXPORT_SYMBOL_GPL(gpiod_get_index);
4385 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4387 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4388 * @con_id: function within the GPIO consumer
4389 * @index: index of the GPIO to obtain in the consumer
4390 * @flags: optional GPIO initialization flags
4392 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4393 * specified index was assigned to the requested function it will return NULL.
4394 * This is convenient for drivers that need to handle optional GPIOs.
4396 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4399 enum gpiod_flags flags)
4401 struct gpio_desc *desc;
4403 desc = gpiod_get_index(dev, con_id, index, flags);
4404 if (gpiod_not_found(desc))
4409 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4412 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4413 * @desc: gpio whose value will be assigned
4414 * @name: gpio line name
4415 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4416 * of_find_gpio() or of_get_gpio_hog()
4417 * @dflags: gpiod_flags - optional GPIO initialization flags
4419 int gpiod_hog(struct gpio_desc *desc, const char *name,
4420 unsigned long lflags, enum gpiod_flags dflags)
4422 struct gpio_chip *gc;
4423 struct gpio_desc *local_desc;
4427 gc = gpiod_to_chip(desc);
4428 hwnum = gpio_chip_hwgpio(desc);
4430 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4432 if (IS_ERR(local_desc)) {
4433 ret = PTR_ERR(local_desc);
4434 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4435 name, gc->label, hwnum, ret);
4439 /* Mark GPIO as hogged so it can be identified and removed later */
4440 set_bit(FLAG_IS_HOGGED, &desc->flags);
4442 gpiod_dbg(desc, "hogged as %s%s\n",
4443 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4444 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4445 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4451 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4452 * @gc: gpio chip to act on
4454 static void gpiochip_free_hogs(struct gpio_chip *gc)
4456 struct gpio_desc *desc;
4458 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
4459 gpiochip_free_own_desc(desc);
4463 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4464 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4465 * @con_id: function within the GPIO consumer
4466 * @flags: optional GPIO initialization flags
4468 * This function acquires all the GPIOs defined under a given function.
4470 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4471 * no GPIO has been assigned to the requested function, or another IS_ERR()
4472 * code if an error occurred while trying to acquire the GPIOs.
4474 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4476 enum gpiod_flags flags)
4478 struct gpio_desc *desc;
4479 struct gpio_descs *descs;
4480 struct gpio_array *array_info = NULL;
4481 struct gpio_chip *gc;
4482 int count, bitmap_size;
4485 count = gpiod_count(dev, con_id);
4487 return ERR_PTR(count);
4489 descs_size = struct_size(descs, desc, count);
4490 descs = kzalloc(descs_size, GFP_KERNEL);
4492 return ERR_PTR(-ENOMEM);
4494 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) {
4495 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4497 gpiod_put_array(descs);
4498 return ERR_CAST(desc);
4501 descs->desc[descs->ndescs] = desc;
4503 gc = gpiod_to_chip(desc);
4505 * If pin hardware number of array member 0 is also 0, select
4506 * its chip as a candidate for fast bitmap processing path.
4508 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4509 struct gpio_descs *array;
4511 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4514 array = krealloc(descs, descs_size +
4515 struct_size(array_info, invert_mask, 3 * bitmap_size),
4516 GFP_KERNEL | __GFP_ZERO);
4518 gpiod_put_array(descs);
4519 return ERR_PTR(-ENOMEM);
4524 array_info = (void *)descs + descs_size;
4525 array_info->get_mask = array_info->invert_mask +
4527 array_info->set_mask = array_info->get_mask +
4530 array_info->desc = descs->desc;
4531 array_info->size = count;
4532 array_info->chip = gc;
4533 bitmap_set(array_info->get_mask, descs->ndescs,
4534 count - descs->ndescs);
4535 bitmap_set(array_info->set_mask, descs->ndescs,
4536 count - descs->ndescs);
4537 descs->info = array_info;
4540 /* If there is no cache for fast bitmap processing path, continue */
4544 /* Unmark array members which don't belong to the 'fast' chip */
4545 if (array_info->chip != gc) {
4546 __clear_bit(descs->ndescs, array_info->get_mask);
4547 __clear_bit(descs->ndescs, array_info->set_mask);
4550 * Detect array members which belong to the 'fast' chip
4551 * but their pins are not in hardware order.
4553 else if (gpio_chip_hwgpio(desc) != descs->ndescs) {
4555 * Don't use fast path if all array members processed so
4556 * far belong to the same chip as this one but its pin
4557 * hardware number is different from its array index.
4559 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4562 __clear_bit(descs->ndescs,
4563 array_info->get_mask);
4564 __clear_bit(descs->ndescs,
4565 array_info->set_mask);
4568 /* Exclude open drain or open source from fast output */
4569 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4570 gpiochip_line_is_open_source(gc, descs->ndescs))
4571 __clear_bit(descs->ndescs,
4572 array_info->set_mask);
4573 /* Identify 'fast' pins which require invertion */
4574 if (gpiod_is_active_low(desc))
4575 __set_bit(descs->ndescs,
4576 array_info->invert_mask);
4581 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4582 array_info->chip->label, array_info->size,
4583 *array_info->get_mask, *array_info->set_mask,
4584 *array_info->invert_mask);
4587 EXPORT_SYMBOL_GPL(gpiod_get_array);
4590 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4592 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4593 * @con_id: function within the GPIO consumer
4594 * @flags: optional GPIO initialization flags
4596 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4597 * assigned to the requested function it will return NULL.
4599 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4601 enum gpiod_flags flags)
4603 struct gpio_descs *descs;
4605 descs = gpiod_get_array(dev, con_id, flags);
4606 if (gpiod_not_found(descs))
4611 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4614 * gpiod_put - dispose of a GPIO descriptor
4615 * @desc: GPIO descriptor to dispose of
4617 * No descriptor can be used after gpiod_put() has been called on it.
4619 void gpiod_put(struct gpio_desc *desc)
4624 EXPORT_SYMBOL_GPL(gpiod_put);
4627 * gpiod_put_array - dispose of multiple GPIO descriptors
4628 * @descs: struct gpio_descs containing an array of descriptors
4630 void gpiod_put_array(struct gpio_descs *descs)
4634 for (i = 0; i < descs->ndescs; i++)
4635 gpiod_put(descs->desc[i]);
4639 EXPORT_SYMBOL_GPL(gpiod_put_array);
4641 static int gpio_stub_drv_probe(struct device *dev)
4644 * The DT node of some GPIO chips have a "compatible" property, but
4645 * never have a struct device added and probed by a driver to register
4646 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4647 * the consumers of the GPIO chip to get probe deferred forever because
4648 * they will be waiting for a device associated with the GPIO chip
4649 * firmware node to get added and bound to a driver.
4651 * To allow these consumers to probe, we associate the struct
4652 * gpio_device of the GPIO chip with the firmware node and then simply
4653 * bind it to this stub driver.
4658 static struct device_driver gpio_stub_drv = {
4659 .name = "gpio_stub_drv",
4660 .bus = &gpio_bus_type,
4661 .probe = gpio_stub_drv_probe,
4664 static int __init gpiolib_dev_init(void)
4668 /* Register GPIO sysfs bus */
4669 ret = bus_register(&gpio_bus_type);
4671 pr_err("gpiolib: could not register GPIO bus type\n");
4675 ret = driver_register(&gpio_stub_drv);
4677 pr_err("gpiolib: could not register GPIO stub driver\n");
4678 bus_unregister(&gpio_bus_type);
4682 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4684 pr_err("gpiolib: failed to allocate char dev region\n");
4685 driver_unregister(&gpio_stub_drv);
4686 bus_unregister(&gpio_bus_type);
4690 gpiolib_initialized = true;
4691 gpiochip_setup_devs();
4693 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4694 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4695 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4699 core_initcall(gpiolib_dev_init);
4701 #ifdef CONFIG_DEBUG_FS
4703 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4705 struct gpio_chip *gc = gdev->chip;
4706 bool active_low, is_irq, is_out;
4707 unsigned int gpio = gdev->base;
4708 struct gpio_desc *desc;
4711 for_each_gpio_desc(gc, desc) {
4712 guard(srcu)(&desc->srcu);
4713 if (test_bit(FLAG_REQUESTED, &desc->flags)) {
4714 gpiod_get_direction(desc);
4715 is_out = test_bit(FLAG_IS_OUT, &desc->flags);
4716 value = gpio_chip_get_value(gc, desc);
4717 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
4718 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4719 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
4720 gpio, desc->name ?: "", gpiod_get_label(desc),
4721 is_out ? "out" : "in ",
4722 value >= 0 ? (value ? "hi" : "lo") : "? ",
4723 is_irq ? "IRQ " : "",
4724 active_low ? "ACTIVE LOW" : "");
4725 } else if (desc->name) {
4726 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
4733 struct gpiolib_seq_priv {
4738 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4740 struct gpiolib_seq_priv *priv;
4741 struct gpio_device *gdev;
4742 loff_t index = *pos;
4744 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
4749 priv->idx = srcu_read_lock(&gpio_devices_srcu);
4751 list_for_each_entry_srcu(gdev, &gpio_devices, list,
4752 srcu_read_lock_held(&gpio_devices_srcu)) {
4760 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4762 struct gpiolib_seq_priv *priv = s->private;
4763 struct gpio_device *gdev = v, *next;
4765 next = list_entry_rcu(gdev->list.next, struct gpio_device, list);
4766 gdev = &next->list == &gpio_devices ? NULL : next;
4767 priv->newline = true;
4773 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4775 struct gpiolib_seq_priv *priv = s->private;
4777 srcu_read_unlock(&gpio_devices_srcu, priv->idx);
4781 static int gpiolib_seq_show(struct seq_file *s, void *v)
4783 struct gpiolib_seq_priv *priv = s->private;
4784 struct gpio_device *gdev = v;
4785 struct gpio_chip *gc = gdev->chip;
4786 struct device *parent;
4789 seq_printf(s, "%s%s: (dangling chip)",
4790 priv->newline ? "\n" : "",
4791 dev_name(&gdev->dev));
4795 seq_printf(s, "%s%s: GPIOs %d-%d", priv->newline ? "\n" : "",
4796 dev_name(&gdev->dev),
4797 gdev->base, gdev->base + gdev->ngpio - 1);
4798 parent = gc->parent;
4800 seq_printf(s, ", parent: %s/%s",
4801 parent->bus ? parent->bus->name : "no-bus",
4804 seq_printf(s, ", %s", gc->label);
4806 seq_printf(s, ", can sleep");
4807 seq_printf(s, ":\n");
4810 gc->dbg_show(s, gc);
4812 gpiolib_dbg_show(s, gdev);
4817 static const struct seq_operations gpiolib_sops = {
4818 .start = gpiolib_seq_start,
4819 .next = gpiolib_seq_next,
4820 .stop = gpiolib_seq_stop,
4821 .show = gpiolib_seq_show,
4823 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4825 static int __init gpiolib_debugfs_init(void)
4827 /* /sys/kernel/debug/gpio */
4828 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4831 subsys_initcall(gpiolib_debugfs_init);
4833 #endif /* DEBUG_FS */