Lines Matching +full:a +full:- +full:gpio
1 // SPDX-License-Identifier: GPL-2.0
13 #include <linux/gpio.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/machine.h>
23 #include <uapi/linux/gpio.h>
26 #include "gpiolib-of.h"
27 #include "gpiolib-acpi.h"
28 #include "gpiolib-cdev.h"
29 #include "gpiolib-sysfs.h"
32 #include <trace/events/gpio.h>
36 /* Implementation infrastructure for GPIO interfaces.
38 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
56 /* Device and char device-related information */
59 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
62 .name = "gpio",
72 * While any GPIO is requested, its gpio_chip is not removable;
73 * each GPIO's "requested" flag serves as a lock and refcount.
97 d->label = label; in desc_set_label()
101 * gpio_to_desc - Convert a GPIO number to its descriptor
102 * @gpio: global GPIO number
105 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
108 struct gpio_desc *gpio_to_desc(unsigned gpio) in gpio_to_desc() argument
116 if (gdev->base <= gpio && in gpio_to_desc()
117 gdev->base + gdev->ngpio > gpio) { in gpio_to_desc()
119 return &gdev->descs[gpio - gdev->base]; in gpio_to_desc()
125 if (!gpio_is_valid(gpio)) in gpio_to_desc()
126 WARN(1, "invalid GPIO %d\n", gpio); in gpio_to_desc()
133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
135 * @gc: GPIO chip
136 * @hwnum: hardware number of the GPIO for this chip
139 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
145 struct gpio_device *gdev = gc->gpiodev; in gpiochip_get_desc()
147 if (hwnum >= gdev->ngpio) in gpiochip_get_desc()
148 return ERR_PTR(-EINVAL); in gpiochip_get_desc()
150 return &gdev->descs[hwnum]; in gpiochip_get_desc()
155 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
156 * @desc: GPIO descriptor
159 * use GPIO numbers for error messages and sysfs nodes.
162 * The global GPIO number for the GPIO specified by its descriptor.
166 return desc->gdev->base + (desc - &desc->gdev->descs[0]); in desc_to_gpio()
172 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
177 if (!desc || !desc->gdev) in gpiod_to_chip()
179 return desc->gdev->chip; in gpiod_to_chip()
183 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
187 int base = ARCH_NR_GPIOS - ngpio; in gpiochip_find_base()
190 /* found a free space? */ in gpiochip_find_base()
191 if (gdev->base + gdev->ngpio <= base) in gpiochip_find_base()
195 base = gdev->base - ngpio; in gpiochip_find_base()
203 return -ENOSPC; in gpiochip_find_base()
208 * gpiod_get_direction - return the current direction of a GPIO
209 * @desc: GPIO to get the direction of
228 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && in gpiod_get_direction()
229 test_bit(FLAG_IS_OUT, &desc->flags)) in gpiod_get_direction()
232 if (!gc->get_direction) in gpiod_get_direction()
233 return -ENOTSUPP; in gpiod_get_direction()
235 ret = gc->get_direction(gc, offset); in gpiod_get_direction()
243 assign_bit(FLAG_IS_OUT, &desc->flags, !ret); in gpiod_get_direction()
250 * Add a new chip to the global chips list, keeping the list of chips sorted
251 * by range(means [base, base + ngpio - 1]) order.
253 * Return -EBUSY if the new chip overlaps with some other chip's integer
262 list_add_tail(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
267 if (gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list()
269 list_add(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
274 if (prev->base + prev->ngpio <= gdev->base) { in gpiodev_add_to_list()
276 list_add_tail(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
282 if (&next->list == &gpio_devices) in gpiodev_add_to_list()
286 if (prev->base + prev->ngpio <= gdev->base in gpiodev_add_to_list()
287 && gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list()
288 list_add(&gdev->list, &prev->list); in gpiodev_add_to_list()
293 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n"); in gpiodev_add_to_list()
294 return -EBUSY; in gpiodev_add_to_list()
298 * Convert a GPIO name to its descriptor
299 * Note that there is no guarantee that GPIO names are globally unique!
300 * Hence this function will return, if it exists, a reference to the first GPIO
316 for (i = 0; i != gdev->ngpio; ++i) { in gpio_name_to_desc()
317 struct gpio_desc *desc = &gdev->descs[i]; in gpio_name_to_desc()
319 if (!desc->name) in gpio_name_to_desc()
322 if (!strcmp(desc->name, name)) { in gpio_name_to_desc()
335 * Take the names from gc->names and assign them to their GPIO descriptors.
336 * Warn if a name is already used for a GPIO line on a different GPIO chip.
339 * 1. Non-unique names are still accepted,
340 * 2. Name collisions within the same GPIO chip are not reported.
344 struct gpio_device *gdev = gc->gpiodev; in gpiochip_set_desc_names()
348 for (i = 0; i != gc->ngpio; ++i) { in gpiochip_set_desc_names()
349 struct gpio_desc *gpio; in gpiochip_set_desc_names() local
351 gpio = gpio_name_to_desc(gc->names[i]); in gpiochip_set_desc_names()
352 if (gpio) in gpiochip_set_desc_names()
353 dev_warn(&gdev->dev, in gpiochip_set_desc_names()
354 "Detected name collision for GPIO name '%s'\n", in gpiochip_set_desc_names()
355 gc->names[i]); in gpiochip_set_desc_names()
358 /* Then add all names to the GPIO descriptors */ in gpiochip_set_desc_names()
359 for (i = 0; i != gc->ngpio; ++i) in gpiochip_set_desc_names()
360 gdev->descs[i].name = gc->names[i]; in gpiochip_set_desc_names()
366 * devprop_gpiochip_set_names - Set GPIO line names using device properties
367 * @chip: GPIO chip whose lines should be named, if possible
369 * Looks for device property "gpio-line-names" and if it exists assigns
370 * GPIO line names for the chip. The memory allocated for the assigned
376 struct gpio_device *gdev = chip->gpiodev; in devprop_gpiochip_set_names()
377 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev); in devprop_gpiochip_set_names()
382 count = fwnode_property_string_array_count(fwnode, "gpio-line-names"); in devprop_gpiochip_set_names()
386 if (count > gdev->ngpio) { in devprop_gpiochip_set_names()
387 dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d", in devprop_gpiochip_set_names()
388 count, gdev->ngpio); in devprop_gpiochip_set_names()
389 count = gdev->ngpio; in devprop_gpiochip_set_names()
394 return -ENOMEM; in devprop_gpiochip_set_names()
396 ret = fwnode_property_read_string_array(fwnode, "gpio-line-names", in devprop_gpiochip_set_names()
399 dev_warn(&gdev->dev, "failed to read GPIO line names\n"); in devprop_gpiochip_set_names()
405 gdev->descs[i].name = names[i]; in devprop_gpiochip_set_names()
416 p = bitmap_alloc(gc->ngpio, GFP_KERNEL); in gpiochip_allocate_mask()
421 bitmap_fill(p, gc->ngpio); in gpiochip_allocate_mask()
428 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask)) in gpiochip_alloc_valid_mask()
431 gc->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_alloc_valid_mask()
432 if (!gc->valid_mask) in gpiochip_alloc_valid_mask()
433 return -ENOMEM; in gpiochip_alloc_valid_mask()
440 if (gc->init_valid_mask) in gpiochip_init_valid_mask()
441 return gc->init_valid_mask(gc, in gpiochip_init_valid_mask()
442 gc->valid_mask, in gpiochip_init_valid_mask()
443 gc->ngpio); in gpiochip_init_valid_mask()
450 bitmap_free(gc->valid_mask); in gpiochip_free_valid_mask()
451 gc->valid_mask = NULL; in gpiochip_free_valid_mask()
456 if (gc->add_pin_ranges) in gpiochip_add_pin_ranges()
457 return gc->add_pin_ranges(gc); in gpiochip_add_pin_ranges()
466 if (likely(!gc->valid_mask)) in gpiochip_line_is_valid()
468 return test_bit(offset, gc->valid_mask); in gpiochip_line_is_valid()
478 list_del(&gdev->list); in gpiodevice_release()
481 ida_free(&gpio_ida, gdev->id); in gpiodevice_release()
482 kfree_const(gdev->label); in gpiodevice_release()
483 kfree(gdev->descs); in gpiodevice_release()
495 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
496 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
512 gdev->dev.release = gpiodevice_release; in gpiochip_setup_dev()
513 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base, in gpiochip_setup_dev()
514 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic"); in gpiochip_setup_dev()
528 desc = gpiochip_get_desc(gc, hog->chip_hwnum); in gpiochip_machine_hog()
530 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__, in gpiochip_machine_hog()
535 if (test_bit(FLAG_IS_HOGGED, &desc->flags)) in gpiochip_machine_hog()
538 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); in gpiochip_machine_hog()
540 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n", in gpiochip_machine_hog()
541 __func__, gc->label, hog->chip_hwnum, rv); in gpiochip_machine_hog()
551 if (!strcmp(gc->label, hog->chip_label)) in machine_gpiochip_add()
566 dev_err(&gdev->dev, in gpiochip_setup_devs()
567 "Failed to initialize gpio device (%d)\n", ret); in gpiochip_setup_devs()
575 struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL; in gpiochip_add_data_with_key()
579 int base = gc->base; in gpiochip_add_data_with_key()
589 return -ENOMEM; in gpiochip_add_data_with_key()
590 gdev->dev.bus = &gpio_bus_type; in gpiochip_add_data_with_key()
591 gdev->chip = gc; in gpiochip_add_data_with_key()
592 gc->gpiodev = gdev; in gpiochip_add_data_with_key()
593 if (gc->parent) { in gpiochip_add_data_with_key()
594 gdev->dev.parent = gc->parent; in gpiochip_add_data_with_key()
595 gdev->dev.of_node = gc->parent->of_node; in gpiochip_add_data_with_key()
604 gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode; in gpiochip_add_data_with_key()
606 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL); in gpiochip_add_data_with_key()
607 if (gdev->id < 0) { in gpiochip_add_data_with_key()
608 ret = gdev->id; in gpiochip_add_data_with_key()
612 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); in gpiochip_add_data_with_key()
616 device_initialize(&gdev->dev); in gpiochip_add_data_with_key()
617 if (gc->parent && gc->parent->driver) in gpiochip_add_data_with_key()
618 gdev->owner = gc->parent->driver->owner; in gpiochip_add_data_with_key()
619 else if (gc->owner) in gpiochip_add_data_with_key()
620 /* TODO: remove chip->owner */ in gpiochip_add_data_with_key()
621 gdev->owner = gc->owner; in gpiochip_add_data_with_key()
623 gdev->owner = THIS_MODULE; in gpiochip_add_data_with_key()
625 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); in gpiochip_add_data_with_key()
626 if (!gdev->descs) { in gpiochip_add_data_with_key()
627 ret = -ENOMEM; in gpiochip_add_data_with_key()
631 if (gc->ngpio == 0) { in gpiochip_add_data_with_key()
632 chip_err(gc, "tried to insert a GPIO chip with zero lines\n"); in gpiochip_add_data_with_key()
633 ret = -EINVAL; in gpiochip_add_data_with_key()
637 if (gc->ngpio > FASTPATH_NGPIO) in gpiochip_add_data_with_key()
639 gc->ngpio, FASTPATH_NGPIO); in gpiochip_add_data_with_key()
641 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL); in gpiochip_add_data_with_key()
642 if (!gdev->label) { in gpiochip_add_data_with_key()
643 ret = -ENOMEM; in gpiochip_add_data_with_key()
647 gdev->ngpio = gc->ngpio; in gpiochip_add_data_with_key()
648 gdev->data = data; in gpiochip_add_data_with_key()
653 * TODO: this allocates a Linux GPIO number base in the global in gpiochip_add_data_with_key()
654 * GPIO numberspace for this chip. In the long run we want to in gpiochip_add_data_with_key()
656 * it may be a pipe dream. It will not happen before we get rid in gpiochip_add_data_with_key()
660 base = gpiochip_find_base(gc->ngpio); in gpiochip_add_data_with_key()
668 * base outside of the GPIO subsystem. Go over drivers and in gpiochip_add_data_with_key()
670 * a poison instead. in gpiochip_add_data_with_key()
672 gc->base = base; in gpiochip_add_data_with_key()
674 gdev->base = base; in gpiochip_add_data_with_key()
682 for (i = 0; i < gc->ngpio; i++) in gpiochip_add_data_with_key()
683 gdev->descs[i].gdev = gdev; in gpiochip_add_data_with_key()
687 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier); in gpiochip_add_data_with_key()
690 INIT_LIST_HEAD(&gdev->pin_ranges); in gpiochip_add_data_with_key()
693 if (gc->names) in gpiochip_add_data_with_key()
714 for (i = 0; i < gc->ngpio; i++) { in gpiochip_add_data_with_key()
715 struct gpio_desc *desc = &gdev->descs[i]; in gpiochip_add_data_with_key()
717 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { in gpiochip_add_data_with_key()
719 &desc->flags, !gc->get_direction(gc, i)); in gpiochip_add_data_with_key()
722 &desc->flags, !gc->direction_input); in gpiochip_add_data_with_key()
749 * we get a device node entry in sysfs under in gpiochip_add_data_with_key()
750 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for in gpiochip_add_data_with_key()
776 list_del(&gdev->list); in gpiochip_add_data_with_key()
779 kfree_const(gdev->label); in gpiochip_add_data_with_key()
781 kfree(gdev->descs); in gpiochip_add_data_with_key()
783 kfree(dev_name(&gdev->dev)); in gpiochip_add_data_with_key()
785 ida_free(&gpio_ida, gdev->id); in gpiochip_add_data_with_key()
789 gdev->base, gdev->base + gdev->ngpio - 1, in gpiochip_add_data_with_key()
790 gc->label ? : "generic", ret); in gpiochip_add_data_with_key()
797 * gpiochip_get_data() - get per-subdriver data for the chip
798 * @gc: GPIO chip
801 * The per-subdriver data for the chip.
805 return gc->gpiodev->data; in gpiochip_get_data()
810 * gpiochip_remove() - unregister a gpio_chip
813 * A gpio_chip with any GPIOs still requested may not be removed.
817 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove()
825 gdev->chip = NULL; in gpiochip_remove()
835 gdev->data = NULL; in gpiochip_remove()
838 for (i = 0; i < gdev->ngpio; i++) { in gpiochip_remove()
844 if (i != gdev->ngpio) in gpiochip_remove()
845 dev_crit(&gdev->dev, in gpiochip_remove()
855 put_device(&gdev->dev); in gpiochip_remove()
860 * gpiochip_find() - iterator for locating a specific gpio_chip
864 * Similar to bus_find_device. It returns a reference to a gpio_chip as
865 * determined by a user supplied @match callback. The callback should return
866 * 0 if the device doesn't match and non-zero if it does. If the callback is
867 * non-zero, this function will return to the caller and not iterate over any
880 if (gdev->chip && match(gdev->chip, data)) { in gpiochip_find()
881 gc = gdev->chip; in gpiochip_find()
895 return !strcmp(gc->label, name); in gpiochip_match_name()
911 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_hw()
913 if (!girq->init_hw) in gpiochip_irqchip_init_hw()
916 return girq->init_hw(gc); in gpiochip_irqchip_init_hw()
921 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_valid_mask()
923 if (!girq->init_valid_mask) in gpiochip_irqchip_init_valid_mask()
926 girq->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_irqchip_init_valid_mask()
927 if (!girq->valid_mask) in gpiochip_irqchip_init_valid_mask()
928 return -ENOMEM; in gpiochip_irqchip_init_valid_mask()
930 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); in gpiochip_irqchip_init_valid_mask()
937 bitmap_free(gc->irq.valid_mask); in gpiochip_irqchip_free_valid_mask()
938 gc->irq.valid_mask = NULL; in gpiochip_irqchip_free_valid_mask()
947 if (likely(!gc->irq.valid_mask)) in gpiochip_irqchip_irq_valid()
949 return test_bit(offset, gc->irq.valid_mask); in gpiochip_irqchip_irq_valid()
954 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
966 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_set_cascaded_irqchip()
967 struct device *dev = &gc->gpiodev->dev; in gpiochip_set_cascaded_irqchip()
969 if (!girq->domain) { in gpiochip_set_cascaded_irqchip()
976 if (gc->can_sleep) { in gpiochip_set_cascaded_irqchip()
978 "you cannot have chained interrupts on a chip that may sleep\n"); in gpiochip_set_cascaded_irqchip()
981 girq->parents = devm_kcalloc(dev, 1, in gpiochip_set_cascaded_irqchip()
982 sizeof(*girq->parents), in gpiochip_set_cascaded_irqchip()
984 if (!girq->parents) { in gpiochip_set_cascaded_irqchip()
988 girq->parents[0] = parent_irq; in gpiochip_set_cascaded_irqchip()
989 girq->num_parents = 1; in gpiochip_set_cascaded_irqchip()
1000 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1017 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1018 * to a gpiochip
1027 if (is_of_node(gc->irq.fwnode)) in gpiochip_set_hierarchical_irqchip()
1034 * provide. Once all GPIO drivers using board files are gone from in gpiochip_set_hierarchical_irqchip()
1035 * the kernel we can delete this code, but for a transitional period in gpiochip_set_hierarchical_irqchip()
1038 if (is_fwnode_irqchip(gc->irq.fwnode)) { in gpiochip_set_hierarchical_irqchip()
1042 for (i = 0; i < gc->ngpio; i++) { in gpiochip_set_hierarchical_irqchip()
1046 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_set_hierarchical_irqchip()
1054 ret = girq->child_to_parent_hwirq(gc, i, in gpiochip_set_hierarchical_irqchip()
1059 chip_err(gc, "skip set-up on hwirq %d\n", in gpiochip_set_hierarchical_irqchip()
1064 fwspec.fwnode = gc->irq.fwnode; in gpiochip_set_hierarchical_irqchip()
1065 /* This is the hwirq for the GPIO line side of things */ in gpiochip_set_hierarchical_irqchip()
1066 fwspec.param[0] = girq->child_offset_to_irq(gc, i); in gpiochip_set_hierarchical_irqchip()
1070 ret = __irq_domain_alloc_irqs(gc->irq.domain, in gpiochip_set_hierarchical_irqchip()
1072 -1, in gpiochip_set_hierarchical_irqchip()
1080 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n", in gpiochip_set_hierarchical_irqchip()
1098 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { in gpiochip_hierarchy_irq_domain_translate()
1103 if (is_fwnode_irqchip(fwspec->fwnode)) { in gpiochip_hierarchy_irq_domain_translate()
1112 return -EINVAL; in gpiochip_hierarchy_irq_domain_translate()
1120 struct gpio_chip *gc = d->host_data; in gpiochip_hierarchy_irq_domain_alloc()
1127 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_hierarchy_irq_domain_alloc()
1131 * The nr_irqs parameter is always one except for PCI multi-MSI in gpiochip_hierarchy_irq_domain_alloc()
1136 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); in gpiochip_hierarchy_irq_domain_alloc()
1142 ret = girq->child_to_parent_hwirq(gc, hwirq, type, in gpiochip_hierarchy_irq_domain_alloc()
1157 gc->irq.chip, in gpiochip_hierarchy_irq_domain_alloc()
1159 girq->handler, in gpiochip_hierarchy_irq_domain_alloc()
1164 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type); in gpiochip_hierarchy_irq_domain_alloc()
1166 return -ENOMEM; in gpiochip_hierarchy_irq_domain_alloc()
1170 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_hierarchy_irq_domain_alloc()
1176 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST)) in gpiochip_hierarchy_irq_domain_alloc()
1195 ops->activate = gpiochip_irq_domain_activate; in gpiochip_hierarchy_setup_domain_ops()
1196 ops->deactivate = gpiochip_irq_domain_deactivate; in gpiochip_hierarchy_setup_domain_ops()
1197 ops->alloc = gpiochip_hierarchy_irq_domain_alloc; in gpiochip_hierarchy_setup_domain_ops()
1198 ops->free = irq_domain_free_irqs_common; in gpiochip_hierarchy_setup_domain_ops()
1205 if (!ops->translate) in gpiochip_hierarchy_setup_domain_ops()
1206 ops->translate = gpiochip_hierarchy_irq_domain_translate; in gpiochip_hierarchy_setup_domain_ops()
1211 if (!gc->irq.child_to_parent_hwirq || in gpiochip_hierarchy_add_domain()
1212 !gc->irq.fwnode) { in gpiochip_hierarchy_add_domain()
1214 return -EINVAL; in gpiochip_hierarchy_add_domain()
1217 if (!gc->irq.child_offset_to_irq) in gpiochip_hierarchy_add_domain()
1218 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; in gpiochip_hierarchy_add_domain()
1220 if (!gc->irq.populate_parent_alloc_arg) in gpiochip_hierarchy_add_domain()
1221 gc->irq.populate_parent_alloc_arg = in gpiochip_hierarchy_add_domain()
1224 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); in gpiochip_hierarchy_add_domain()
1226 gc->irq.domain = irq_domain_create_hierarchy( in gpiochip_hierarchy_add_domain()
1227 gc->irq.parent_domain, in gpiochip_hierarchy_add_domain()
1229 gc->ngpio, in gpiochip_hierarchy_add_domain()
1230 gc->irq.fwnode, in gpiochip_hierarchy_add_domain()
1231 &gc->irq.child_irq_domain_ops, in gpiochip_hierarchy_add_domain()
1234 if (!gc->irq.domain) in gpiochip_hierarchy_add_domain()
1235 return -ENOMEM; in gpiochip_hierarchy_add_domain()
1237 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); in gpiochip_hierarchy_add_domain()
1244 return !!gc->irq.parent_domain; in gpiochip_hierarchy_is_hierarchical()
1257 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_twocell()
1258 fwspec->param_count = 2; in gpiochip_populate_parent_fwspec_twocell()
1259 fwspec->param[0] = parent_hwirq; in gpiochip_populate_parent_fwspec_twocell()
1260 fwspec->param[1] = parent_type; in gpiochip_populate_parent_fwspec_twocell()
1276 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_fourcell()
1277 fwspec->param_count = 4; in gpiochip_populate_parent_fwspec_fourcell()
1278 fwspec->param[0] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1279 fwspec->param[1] = parent_hwirq; in gpiochip_populate_parent_fwspec_fourcell()
1280 fwspec->param[2] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1281 fwspec->param[3] = parent_type; in gpiochip_populate_parent_fwspec_fourcell()
1291 return -EINVAL; in gpiochip_hierarchy_add_domain()
1302 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1304 * @irq: the global irq number used by this GPIO irqchip irq
1305 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1307 * This function will set up the mapping for a certain IRQ line on a
1314 struct gpio_chip *gc = d->host_data; in gpiochip_irq_map()
1318 return -ENXIO; in gpiochip_irq_map()
1322 * This lock class tells lockdep that GPIO irqs are in a different in gpiochip_irq_map()
1325 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_irq_map()
1326 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler); in gpiochip_irq_map()
1328 if (gc->irq.threaded) in gpiochip_irq_map()
1332 if (gc->irq.num_parents == 1) in gpiochip_irq_map()
1333 ret = irq_set_parent(irq, gc->irq.parents[0]); in gpiochip_irq_map()
1334 else if (gc->irq.map) in gpiochip_irq_map()
1335 ret = irq_set_parent(irq, gc->irq.map[hwirq]); in gpiochip_irq_map()
1341 * No set-up of the hardware will happen if IRQ_TYPE_NONE in gpiochip_irq_map()
1344 if (gc->irq.default_type != IRQ_TYPE_NONE) in gpiochip_irq_map()
1345 irq_set_irq_type(irq, gc->irq.default_type); in gpiochip_irq_map()
1353 struct gpio_chip *gc = d->host_data; in gpiochip_irq_unmap()
1355 if (gc->irq.threaded) in gpiochip_irq_unmap()
1365 /* Virtually all GPIO irqchips are twocell:ed */
1375 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1380 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1387 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_activate()
1389 return gpiochip_lock_as_irq(gc, data->hwirq); in gpiochip_irq_domain_activate()
1394 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1398 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1405 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_deactivate()
1407 return gpiochip_unlock_as_irq(gc, data->hwirq); in gpiochip_irq_domain_deactivate()
1413 struct irq_domain *domain = gc->irq.domain; in gpiochip_to_irq()
1421 if (!gc->irq.initialized) in gpiochip_to_irq()
1422 return -EPROBE_DEFER; in gpiochip_to_irq()
1426 return -ENXIO; in gpiochip_to_irq()
1432 spec.fwnode = domain->fwnode; in gpiochip_to_irq()
1434 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset); in gpiochip_to_irq()
1448 return gpiochip_reqres_irq(gc, d->hwirq); in gpiochip_irq_reqres()
1455 gpiochip_relres_irq(gc, d->hwirq); in gpiochip_irq_relres()
1462 if (gc->irq.irq_mask) in gpiochip_irq_mask()
1463 gc->irq.irq_mask(d); in gpiochip_irq_mask()
1464 gpiochip_disable_irq(gc, d->hwirq); in gpiochip_irq_mask()
1471 gpiochip_enable_irq(gc, d->hwirq); in gpiochip_irq_unmask()
1472 if (gc->irq.irq_unmask) in gpiochip_irq_unmask()
1473 gc->irq.irq_unmask(d); in gpiochip_irq_unmask()
1480 gpiochip_enable_irq(gc, d->hwirq); in gpiochip_irq_enable()
1481 gc->irq.irq_enable(d); in gpiochip_irq_enable()
1488 gc->irq.irq_disable(d); in gpiochip_irq_disable()
1489 gpiochip_disable_irq(gc, d->hwirq); in gpiochip_irq_disable()
1494 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_set_irq_hooks()
1496 if (!irqchip->irq_request_resources && in gpiochip_set_irq_hooks()
1497 !irqchip->irq_release_resources) { in gpiochip_set_irq_hooks()
1498 irqchip->irq_request_resources = gpiochip_irq_reqres; in gpiochip_set_irq_hooks()
1499 irqchip->irq_release_resources = gpiochip_irq_relres; in gpiochip_set_irq_hooks()
1501 if (WARN_ON(gc->irq.irq_enable)) in gpiochip_set_irq_hooks()
1504 if (irqchip->irq_enable == gpiochip_irq_enable || in gpiochip_set_irq_hooks()
1505 irqchip->irq_mask == gpiochip_irq_mask) { in gpiochip_set_irq_hooks()
1507 * ...and if so, give a gentle warning that this is bad in gpiochip_set_irq_hooks()
1515 if (irqchip->irq_disable) { in gpiochip_set_irq_hooks()
1516 gc->irq.irq_disable = irqchip->irq_disable; in gpiochip_set_irq_hooks()
1517 irqchip->irq_disable = gpiochip_irq_disable; in gpiochip_set_irq_hooks()
1519 gc->irq.irq_mask = irqchip->irq_mask; in gpiochip_set_irq_hooks()
1520 irqchip->irq_mask = gpiochip_irq_mask; in gpiochip_set_irq_hooks()
1523 if (irqchip->irq_enable) { in gpiochip_set_irq_hooks()
1524 gc->irq.irq_enable = irqchip->irq_enable; in gpiochip_set_irq_hooks()
1525 irqchip->irq_enable = gpiochip_irq_enable; in gpiochip_set_irq_hooks()
1527 gc->irq.irq_unmask = irqchip->irq_unmask; in gpiochip_set_irq_hooks()
1528 irqchip->irq_unmask = gpiochip_irq_unmask; in gpiochip_set_irq_hooks()
1533 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1534 * @gc: the GPIO chip to add the IRQ chip to
1542 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_add_irqchip()
1551 if (gc->irq.parent_handler && gc->can_sleep) { in gpiochip_add_irqchip()
1552 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n"); in gpiochip_add_irqchip()
1553 return -EINVAL; in gpiochip_add_irqchip()
1556 np = gc->gpiodev->dev.of_node; in gpiochip_add_irqchip()
1557 type = gc->irq.default_type; in gpiochip_add_irqchip()
1560 * Specifying a default trigger is a terrible idea if DT or ACPI is in gpiochip_add_irqchip()
1565 "%s: Ignoring %u default trigger\n", np->full_name, type)) in gpiochip_add_irqchip()
1568 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) { in gpiochip_add_irqchip()
1569 acpi_handle_warn(ACPI_HANDLE(gc->parent), in gpiochip_add_irqchip()
1574 gc->to_irq = gpiochip_to_irq; in gpiochip_add_irqchip()
1575 gc->irq.default_type = type; in gpiochip_add_irqchip()
1576 gc->irq.lock_key = lock_key; in gpiochip_add_irqchip()
1577 gc->irq.request_key = request_key; in gpiochip_add_irqchip()
1579 /* If a parent irqdomain is provided, let's build a hierarchy */ in gpiochip_add_irqchip()
1586 if (gc->irq.domain_ops) in gpiochip_add_irqchip()
1587 ops = gc->irq.domain_ops; in gpiochip_add_irqchip()
1591 gc->irq.domain = irq_domain_add_simple(np, in gpiochip_add_irqchip()
1592 gc->ngpio, in gpiochip_add_irqchip()
1593 gc->irq.first, in gpiochip_add_irqchip()
1595 if (!gc->irq.domain) in gpiochip_add_irqchip()
1596 return -EINVAL; in gpiochip_add_irqchip()
1599 if (gc->irq.parent_handler) { in gpiochip_add_irqchip()
1600 void *data = gc->irq.parent_handler_data ?: gc; in gpiochip_add_irqchip()
1602 for (i = 0; i < gc->irq.num_parents; i++) { in gpiochip_add_irqchip()
1608 irq_set_chained_handler_and_data(gc->irq.parents[i], in gpiochip_add_irqchip()
1609 gc->irq.parent_handler, in gpiochip_add_irqchip()
1618 * gc->irq.initialized before initialization of above in gpiochip_add_irqchip()
1619 * GPIO chip irq members. in gpiochip_add_irqchip()
1623 gc->irq.initialized = true; in gpiochip_add_irqchip()
1631 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1638 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_irqchip_remove()
1643 if (irqchip && gc->irq.parent_handler) { in gpiochip_irqchip_remove()
1644 struct gpio_irq_chip *irq = &gc->irq; in gpiochip_irqchip_remove()
1647 for (i = 0; i < irq->num_parents; i++) in gpiochip_irqchip_remove()
1648 irq_set_chained_handler_and_data(irq->parents[i], in gpiochip_irqchip_remove()
1653 if (gc->irq.domain) { in gpiochip_irqchip_remove()
1656 for (offset = 0; offset < gc->ngpio; offset++) { in gpiochip_irqchip_remove()
1660 irq = irq_find_mapping(gc->irq.domain, offset); in gpiochip_irqchip_remove()
1664 irq_domain_remove(gc->irq.domain); in gpiochip_irqchip_remove()
1668 if (irqchip->irq_request_resources == gpiochip_irq_reqres) { in gpiochip_irqchip_remove()
1669 irqchip->irq_request_resources = NULL; in gpiochip_irqchip_remove()
1670 irqchip->irq_release_resources = NULL; in gpiochip_irqchip_remove()
1672 if (irqchip->irq_enable == gpiochip_irq_enable) { in gpiochip_irqchip_remove()
1673 irqchip->irq_enable = gc->irq.irq_enable; in gpiochip_irqchip_remove()
1674 irqchip->irq_disable = gc->irq.irq_disable; in gpiochip_irqchip_remove()
1677 gc->irq.irq_enable = NULL; in gpiochip_irqchip_remove()
1678 gc->irq.irq_disable = NULL; in gpiochip_irqchip_remove()
1679 gc->irq.chip = NULL; in gpiochip_irqchip_remove()
1685 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1690 * @handler: the irq handler to use (often a predefined irq core function)
1693 * @threaded: whether this irqchip uses a nested thread handler
1697 * This function closely associates a certain irqchip with a certain
1708 * the pins on the gpiochip can generate a unique IRQ. Everything else
1723 return -EINVAL; in gpiochip_irqchip_add_key()
1725 if (!gc->parent) { in gpiochip_irqchip_add_key()
1727 return -EINVAL; in gpiochip_irqchip_add_key()
1729 gc->irq.threaded = threaded; in gpiochip_irqchip_add_key()
1730 of_node = gc->parent->of_node; in gpiochip_irqchip_add_key()
1734 * FIXME: get rid of this and use gc->parent->of_node in gpiochip_irqchip_add_key()
1737 if (gc->of_node) in gpiochip_irqchip_add_key()
1738 of_node = gc->of_node; in gpiochip_irqchip_add_key()
1741 * Specifying a default trigger is a terrible idea if DT or ACPI is in gpiochip_irqchip_add_key()
1742 * used to configure the interrupts, as you may end-up with in gpiochip_irqchip_add_key()
1748 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) { in gpiochip_irqchip_add_key()
1749 acpi_handle_warn(ACPI_HANDLE(gc->parent), in gpiochip_irqchip_add_key()
1754 gc->irq.chip = irqchip; in gpiochip_irqchip_add_key()
1755 gc->irq.handler = handler; in gpiochip_irqchip_add_key()
1756 gc->irq.default_type = type; in gpiochip_irqchip_add_key()
1757 gc->to_irq = gpiochip_to_irq; in gpiochip_irqchip_add_key()
1758 gc->irq.lock_key = lock_key; in gpiochip_irqchip_add_key()
1759 gc->irq.request_key = request_key; in gpiochip_irqchip_add_key()
1760 gc->irq.domain = irq_domain_add_simple(of_node, in gpiochip_irqchip_add_key()
1761 gc->ngpio, first_irq, in gpiochip_irqchip_add_key()
1763 if (!gc->irq.domain) { in gpiochip_irqchip_add_key()
1764 gc->irq.chip = NULL; in gpiochip_irqchip_add_key()
1765 return -EINVAL; in gpiochip_irqchip_add_key()
1777 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1787 return -EINVAL; in gpiochip_irqchip_add_domain()
1789 gc->to_irq = gpiochip_to_irq; in gpiochip_irqchip_add_domain()
1790 gc->irq.domain = domain; in gpiochip_irqchip_add_domain()
1821 * gpiochip_generic_request() - request the gpio function for a pin
1822 * @gc: the gpiochip owning the GPIO
1823 * @offset: the offset of the GPIO to request for GPIO function
1828 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_request()
1832 return pinctrl_gpio_request(gc->gpiodev->base + offset); in gpiochip_generic_request()
1837 * gpiochip_generic_free() - free the gpio function from a pin
1838 * @gc: the gpiochip to request the gpio function for
1839 * @offset: the offset of the GPIO to free from GPIO function
1844 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_free()
1848 pinctrl_gpio_free(gc->gpiodev->base + offset); in gpiochip_generic_free()
1853 * gpiochip_generic_config() - apply configuration for a pin
1854 * @gc: the gpiochip owning the GPIO
1855 * @offset: the offset of the GPIO to apply the configuration
1861 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config); in gpiochip_generic_config()
1868 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1874 * Calling this function directly from a DeviceTree-supported
1876 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1877 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1884 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pingroup_range()
1890 return -ENOMEM; in gpiochip_add_pingroup_range()
1894 pin_range->range.id = gpio_offset; in gpiochip_add_pingroup_range()
1895 pin_range->range.gc = gc; in gpiochip_add_pingroup_range()
1896 pin_range->range.name = gc->label; in gpiochip_add_pingroup_range()
1897 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pingroup_range()
1898 pin_range->pctldev = pctldev; in gpiochip_add_pingroup_range()
1901 &pin_range->range.pins, in gpiochip_add_pingroup_range()
1902 &pin_range->range.npins); in gpiochip_add_pingroup_range()
1908 pinctrl_add_gpio_range(pctldev, &pin_range->range); in gpiochip_add_pingroup_range()
1910 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n", in gpiochip_add_pingroup_range()
1911 gpio_offset, gpio_offset + pin_range->range.npins - 1, in gpiochip_add_pingroup_range()
1914 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pingroup_range()
1921 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1926 * @npins: the number of pins from the offset of each pin space (GPIO and
1930 * 0 on success, or a negative error-code on failure.
1932 * Calling this function directly from a DeviceTree-supported
1934 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1935 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1942 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pin_range()
1948 return -ENOMEM; in gpiochip_add_pin_range()
1952 pin_range->range.id = gpio_offset; in gpiochip_add_pin_range()
1953 pin_range->range.gc = gc; in gpiochip_add_pin_range()
1954 pin_range->range.name = gc->label; in gpiochip_add_pin_range()
1955 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pin_range()
1956 pin_range->range.pin_base = pin_offset; in gpiochip_add_pin_range()
1957 pin_range->range.npins = npins; in gpiochip_add_pin_range()
1958 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, in gpiochip_add_pin_range()
1959 &pin_range->range); in gpiochip_add_pin_range()
1960 if (IS_ERR(pin_range->pctldev)) { in gpiochip_add_pin_range()
1961 ret = PTR_ERR(pin_range->pctldev); in gpiochip_add_pin_range()
1966 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n", in gpiochip_add_pin_range()
1967 gpio_offset, gpio_offset + npins - 1, in gpiochip_add_pin_range()
1969 pin_offset, pin_offset + npins - 1); in gpiochip_add_pin_range()
1971 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pin_range()
1978 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1984 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove_pin_ranges()
1986 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { in gpiochip_remove_pin_ranges()
1987 list_del(&pin_range->node); in gpiochip_remove_pin_ranges()
1988 pinctrl_remove_gpio_range(pin_range->pctldev, in gpiochip_remove_pin_ranges()
1989 &pin_range->range); in gpiochip_remove_pin_ranges()
2003 struct gpio_chip *gc = desc->gdev->chip; in gpiod_request_commit()
2011 return -ENOMEM; in gpiod_request_commit()
2017 * before IRQs are enabled, for non-sleeping (SOC) GPIOs. in gpiod_request_commit()
2020 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { in gpiod_request_commit()
2025 ret = -EBUSY; in gpiod_request_commit()
2029 if (gc->request) { in gpiod_request_commit()
2030 /* gc->request may sleep */ in gpiod_request_commit()
2034 ret = gc->request(gc, offset); in gpiod_request_commit()
2036 ret = -EINVAL; in gpiod_request_commit()
2042 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_request_commit()
2046 if (gc->get_direction) { in gpiod_request_commit()
2047 /* gc->get_direction may sleep */ in gpiod_request_commit()
2059 * function taking a descriptor, so we need to use a preprocessor
2061 * optional GPIO and calls should just bail out.
2068 pr_warn("%s: invalid GPIO (errorpointer)\n", func); in validate_desc()
2071 if (!desc->gdev) { in validate_desc()
2072 pr_warn("%s: invalid GPIO (no device)\n", func); in validate_desc()
2073 return -EINVAL; in validate_desc()
2075 if (!desc->gdev->chip) { in validate_desc()
2076 dev_warn(&desc->gdev->dev, in validate_desc()
2097 int ret = -EPROBE_DEFER; in gpiod_request()
2101 gdev = desc->gdev; in gpiod_request()
2103 if (try_module_get(gdev->owner)) { in gpiod_request()
2106 module_put(gdev->owner); in gpiod_request()
2108 get_device(&gdev->dev); in gpiod_request()
2129 gc = desc->gdev->chip; in gpiod_free_commit()
2130 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) { in gpiod_free_commit()
2131 if (gc->free) { in gpiod_free_commit()
2133 might_sleep_if(gc->can_sleep); in gpiod_free_commit()
2134 gc->free(gc, gpio_chip_hwgpio(desc)); in gpiod_free_commit()
2137 kfree_const(desc->label); in gpiod_free_commit()
2139 clear_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_free_commit()
2140 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_free_commit()
2141 clear_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_free_commit()
2142 clear_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_free_commit()
2143 clear_bit(FLAG_PULL_UP, &desc->flags); in gpiod_free_commit()
2144 clear_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_free_commit()
2145 clear_bit(FLAG_BIAS_DISABLE, &desc->flags); in gpiod_free_commit()
2146 clear_bit(FLAG_EDGE_RISING, &desc->flags); in gpiod_free_commit()
2147 clear_bit(FLAG_EDGE_FALLING, &desc->flags); in gpiod_free_commit()
2148 clear_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_free_commit()
2150 desc->hog = NULL; in gpiod_free_commit()
2153 WRITE_ONCE(desc->debounce_period_us, 0); in gpiod_free_commit()
2159 blocking_notifier_call_chain(&desc->gdev->notifier, in gpiod_free_commit()
2167 if (desc && desc->gdev && gpiod_free_commit(desc)) { in gpiod_free()
2168 module_put(desc->gdev->owner); in gpiod_free()
2169 put_device(&desc->gdev->dev); in gpiod_free()
2176 * gpiochip_is_requested - return string iff signal was requested
2178 * @offset: of signal within controller's 0..(ngpio - 1) range
2180 * Returns NULL if the GPIO is not currently requested, else a string.
2182 * passed it is a meaningless, non-NULL constant.
2184 * This function is for use by GPIO controller drivers. The label can
2185 * help with diagnostics, and knowing that the signal is used as a GPIO
2192 if (offset >= gc->ngpio) in gpiochip_is_requested()
2199 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) in gpiochip_is_requested()
2201 return desc->label; in gpiochip_is_requested()
2206 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2207 * @gc: GPIO chip
2208 * @hwnum: hardware number of the GPIO for which to request the descriptor
2209 * @label: label for the GPIO
2210 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2213 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2216 * Function allows GPIO chip drivers to request and use their own GPIO
2218 * function will not increase reference count of the GPIO chip module. This
2219 * allows the GPIO chip module to be unloaded as needed (we assume that the
2220 * GPIO chip driver handles freeing the GPIOs it has requested).
2223 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2236 chip_err(gc, "failed to get GPIO descriptor\n"); in gpiochip_request_own_desc()
2246 chip_err(gc, "setup of own GPIO %s failed\n", label); in gpiochip_request_own_desc()
2256 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2257 * @desc: GPIO descriptor to free
2259 * Function frees the given GPIO requested previously with
2270 * Drivers MUST set GPIO direction before making get/set calls. In
2273 * As a rule these aren't called more than once (except for drivers
2274 * using the open-drain emulation idiom) so these are natural places
2282 if (!gc->set_config) in gpio_do_set_config()
2283 return -ENOTSUPP; in gpio_do_set_config()
2285 return gc->set_config(gc, offset, config); in gpio_do_set_config()
2290 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_config()
2313 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) in gpio_set_bias()
2315 else if (test_bit(FLAG_PULL_UP, &desc->flags)) in gpio_set_bias()
2317 else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) in gpio_set_bias()
2322 if (ret != -ENOTSUPP) in gpio_set_bias()
2329 * gpiod_direction_input - set the GPIO direction to input
2330 * @desc: GPIO to set to input
2332 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2343 gc = desc->gdev->chip; in gpiod_direction_input()
2347 * the chip is output-only, but you can't specify .direction_input() in gpiod_direction_input()
2350 if (!gc->get && gc->direction_input) { in gpiod_direction_input()
2354 return -EIO; in gpiod_direction_input()
2358 * If we have a .direction_input() callback, things are simple, in gpiod_direction_input()
2359 * just call it. Else we are some input-only chip so try to check the in gpiod_direction_input()
2363 if (gc->direction_input) { in gpiod_direction_input()
2364 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc)); in gpiod_direction_input()
2365 } else if (gc->get_direction && in gpiod_direction_input()
2366 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) { in gpiod_direction_input()
2370 return -EIO; in gpiod_direction_input()
2373 clear_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_input()
2385 struct gpio_chip *gc = desc->gdev->chip; in gpiod_direction_output_raw_commit()
2391 * output-only, but if there is then not even a .set() operation it in gpiod_direction_output_raw_commit()
2394 if (!gc->set && !gc->direction_output) { in gpiod_direction_output_raw_commit()
2398 return -EIO; in gpiod_direction_output_raw_commit()
2401 if (gc->direction_output) { in gpiod_direction_output_raw_commit()
2402 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); in gpiod_direction_output_raw_commit()
2405 if (gc->get_direction && in gpiod_direction_output_raw_commit()
2406 gc->get_direction(gc, gpio_chip_hwgpio(desc))) { in gpiod_direction_output_raw_commit()
2410 return -EIO; in gpiod_direction_output_raw_commit()
2414 * output-only chip, so just drive the output as desired. in gpiod_direction_output_raw_commit()
2416 gc->set(gc, gpio_chip_hwgpio(desc), val); in gpiod_direction_output_raw_commit()
2420 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output_raw_commit()
2427 * gpiod_direction_output_raw - set the GPIO direction to output
2428 * @desc: GPIO to set to output
2429 * @value: initial output value of the GPIO
2431 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2445 * gpiod_direction_output - set the GPIO direction to output
2446 * @desc: GPIO to set to output
2447 * @value: initial output value of the GPIO
2449 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2451 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2461 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_direction_output()
2467 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && in gpiod_direction_output()
2468 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { in gpiod_direction_output()
2470 "%s: tried to set a GPIO tied to an IRQ as output\n", in gpiod_direction_output()
2472 return -EIO; in gpiod_direction_output()
2475 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiod_direction_output()
2486 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { in gpiod_direction_output()
2507 * When emulating open-source or open-drain functionalities by not in gpiod_direction_output()
2513 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output()
2519 * gpiod_set_config - sets @config for a GPIO
2520 * @desc: descriptor of the GPIO for which to set the configuration
2524 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2532 gc = desc->gdev->chip; in gpiod_set_config()
2539 * gpiod_set_debounce - sets @debounce time for a GPIO
2540 * @desc: descriptor of the GPIO for which to set debounce time
2544 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2557 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2558 * @desc: descriptor of the GPIO for which to configure persistence
2562 * 0 on success, otherwise a negative error code.
2568 int gpio; in gpiod_set_transitory() local
2576 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); in gpiod_set_transitory()
2579 gc = desc->gdev->chip; in gpiod_set_transitory()
2580 if (!gc->set_config) in gpiod_set_transitory()
2585 gpio = gpio_chip_hwgpio(desc); in gpiod_set_transitory()
2586 rc = gpio_do_set_config(gc, gpio, packed); in gpiod_set_transitory()
2587 if (rc == -ENOTSUPP) { in gpiod_set_transitory()
2588 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n", in gpiod_set_transitory()
2589 gpio); in gpiod_set_transitory()
2598 * gpiod_is_active_low - test whether a GPIO is active-low or not
2599 * @desc: the gpio descriptor to test
2601 * Returns 1 if the GPIO is active-low, 0 otherwise.
2606 return test_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_is_active_low()
2611 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2612 * @desc: the gpio descriptor to change
2617 change_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_toggle_active_low()
2622 * "is this a valid GPIO" error checks should already have been done.
2624 * "Get" operations are often inlinable as reading a pin value register,
2628 * one register to set a low value, or a different register to set it high.
2631 *------------------------------------------------------------------------
2633 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2634 * have requested the GPIO. That can include implicit requesting by
2635 * a direction setting call. Marking a gpio as requested locks its chip
2640 * that the GPIO was actually requested.
2649 gc = desc->gdev->chip; in gpiod_get_raw_value_commit()
2651 value = gc->get ? gc->get(gc, offset) : -EIO; in gpiod_get_raw_value_commit()
2660 if (gc->get_multiple) { in gpio_chip_get_multiple()
2661 return gc->get_multiple(gc, mask, bits); in gpio_chip_get_multiple()
2662 } else if (gc->get) { in gpio_chip_get_multiple()
2665 for_each_set_bit(i, mask, gc->ngpio) { in gpio_chip_get_multiple()
2666 value = gc->get(gc, i); in gpio_chip_get_multiple()
2673 return -EIO; in gpio_chip_get_multiple()
2689 if (array_info && array_info->desc == desc_array && in gpiod_get_array_value_complex()
2690 array_size <= array_info->size && in gpiod_get_array_value_complex()
2691 (void *)array_info == desc_array + array_info->size) { in gpiod_get_array_value_complex()
2693 WARN_ON(array_info->chip->can_sleep); in gpiod_get_array_value_complex()
2695 ret = gpio_chip_get_multiple(array_info->chip, in gpiod_get_array_value_complex()
2696 array_info->get_mask, in gpiod_get_array_value_complex()
2701 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_get_array_value_complex()
2703 array_info->invert_mask, array_size); in gpiod_get_array_value_complex()
2705 i = find_first_zero_bit(array_info->get_mask, array_size); in gpiod_get_array_value_complex()
2713 struct gpio_chip *gc = desc_array[i]->gdev->chip; in gpiod_get_array_value_complex()
2718 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_get_array_value_complex()
2721 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio), in gpiod_get_array_value_complex()
2725 return -ENOMEM; in gpiod_get_array_value_complex()
2728 bits = mask + BITS_TO_LONGS(gc->ngpio); in gpiod_get_array_value_complex()
2729 bitmap_zero(mask, gc->ngpio); in gpiod_get_array_value_complex()
2732 WARN_ON(gc->can_sleep); in gpiod_get_array_value_complex()
2744 i = find_next_zero_bit(array_info->get_mask, in gpiod_get_array_value_complex()
2747 (desc_array[i]->gdev->chip == gc)); in gpiod_get_array_value_complex()
2761 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_array_value_complex()
2768 j = find_next_zero_bit(array_info->get_mask, i, in gpiod_get_array_value_complex()
2779 * gpiod_get_raw_value() - return a gpio's raw value
2780 * @desc: gpio whose value will be returned
2782 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2786 * complain if the GPIO chip functions potentially sleep.
2792 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_raw_value()
2798 * gpiod_get_value() - return a gpio's value
2799 * @desc: gpio whose value will be returned
2801 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2805 * complain if the GPIO chip functions potentially sleep.
2813 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_value()
2819 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value()
2827 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2829 * @desc_array: array of GPIO descriptors whose values will be read
2838 * and it will complain if the GPIO chip functions potentially sleep.
2846 return -EINVAL; in gpiod_get_raw_array_value()
2854 * gpiod_get_array_value() - read values from an array of GPIOs
2856 * @desc_array: array of GPIO descriptors whose values will be read
2864 * and it will complain if the GPIO chip functions potentially sleep.
2872 return -EINVAL; in gpiod_get_array_value()
2880 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2881 * @desc: gpio descriptor whose state need to be set.
2882 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2887 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_open_drain_value_commit()
2891 ret = gc->direction_input(gc, offset); in gpio_set_open_drain_value_commit()
2893 ret = gc->direction_output(gc, offset, 0); in gpio_set_open_drain_value_commit()
2895 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_drain_value_commit()
2905 * _gpio_set_open_source_value() - Set the open source gpio's value.
2906 * @desc: gpio descriptor whose state need to be set.
2907 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2912 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_open_source_value_commit()
2916 ret = gc->direction_output(gc, offset, 1); in gpio_set_open_source_value_commit()
2918 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_source_value_commit()
2920 ret = gc->direction_input(gc, offset); in gpio_set_open_source_value_commit()
2933 gc = desc->gdev->chip; in gpiod_set_raw_value_commit()
2935 gc->set(gc, gpio_chip_hwgpio(desc), value); in gpiod_set_raw_value_commit()
2942 * @chip: the GPIO chip we operate on
2951 if (gc->set_multiple) { in gpio_chip_set_multiple()
2952 gc->set_multiple(gc, mask, bits); in gpio_chip_set_multiple()
2957 for_each_set_bit(i, mask, gc->ngpio) in gpio_chip_set_multiple()
2958 gc->set(gc, i, test_bit(i, bits)); in gpio_chip_set_multiple()
2975 if (array_info && array_info->desc == desc_array && in gpiod_set_array_value_complex()
2976 array_size <= array_info->size && in gpiod_set_array_value_complex()
2977 (void *)array_info == desc_array + array_info->size) { in gpiod_set_array_value_complex()
2979 WARN_ON(array_info->chip->can_sleep); in gpiod_set_array_value_complex()
2981 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_set_array_value_complex()
2983 array_info->invert_mask, array_size); in gpiod_set_array_value_complex()
2985 gpio_chip_set_multiple(array_info->chip, array_info->set_mask, in gpiod_set_array_value_complex()
2988 i = find_first_zero_bit(array_info->set_mask, array_size); in gpiod_set_array_value_complex()
2996 struct gpio_chip *gc = desc_array[i]->gdev->chip; in gpiod_set_array_value_complex()
3001 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_set_array_value_complex()
3004 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio), in gpiod_set_array_value_complex()
3008 return -ENOMEM; in gpiod_set_array_value_complex()
3011 bits = mask + BITS_TO_LONGS(gc->ngpio); in gpiod_set_array_value_complex()
3012 bitmap_zero(mask, gc->ngpio); in gpiod_set_array_value_complex()
3015 WARN_ON(gc->can_sleep); in gpiod_set_array_value_complex()
3028 test_bit(i, array_info->invert_mask)) && in gpiod_set_array_value_complex()
3029 test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_array_value_complex()
3036 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3038 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3048 i = find_next_zero_bit(array_info->set_mask, in gpiod_set_array_value_complex()
3051 (desc_array[i]->gdev->chip == gc)); in gpiod_set_array_value_complex()
3063 * gpiod_set_raw_value() - assign a gpio's raw value
3064 * @desc: gpio whose value will be assigned
3067 * Set the raw value of the GPIO, i.e. the value of its physical line without
3071 * complain if the GPIO chip functions potentially sleep.
3077 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_set_raw_value()
3083 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3087 * This sets the value of a GPIO line backing a descriptor, applying
3093 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_value_nocheck()
3095 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) in gpiod_set_value_nocheck()
3097 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) in gpiod_set_value_nocheck()
3104 * gpiod_set_value() - assign a gpio's value
3105 * @desc: gpio whose value will be assigned
3108 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3112 * complain if the GPIO chip functions potentially sleep.
3118 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_set_value()
3124 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3126 * @desc_array: array of GPIO descriptors whose values will be assigned
3134 * complain if the GPIO chip functions potentially sleep.
3142 return -EINVAL; in gpiod_set_raw_array_value()
3149 * gpiod_set_array_value() - assign values to an array of GPIOs
3151 * @desc_array: array of GPIO descriptors whose values will be assigned
3159 * complain if the GPIO chip functions potentially sleep.
3167 return -EINVAL; in gpiod_set_array_value()
3175 * gpiod_cansleep() - report whether gpio value access may sleep
3176 * @desc: gpio to check
3182 return desc->gdev->chip->can_sleep; in gpiod_cansleep()
3187 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3188 * @desc: gpio to set the consumer name on
3197 return -ENOMEM; in gpiod_set_consumer_name()
3200 kfree_const(desc->label); in gpiod_set_consumer_name()
3208 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3209 * @desc: gpio whose IRQ will be returned (already requested)
3211 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3222 * but rather a negative error number. in gpiod_to_irq()
3224 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) in gpiod_to_irq()
3225 return -EINVAL; in gpiod_to_irq()
3227 gc = desc->gdev->chip; in gpiod_to_irq()
3229 if (gc->to_irq) { in gpiod_to_irq()
3230 int retirq = gc->to_irq(gc, offset); in gpiod_to_irq()
3234 return -ENXIO; in gpiod_to_irq()
3239 if (gc->irq.chip) { in gpiod_to_irq()
3245 return -EPROBE_DEFER; in gpiod_to_irq()
3248 return -ENXIO; in gpiod_to_irq()
3253 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3254 * @gc: the chip the GPIO to lock belongs to
3255 * @offset: the offset of the GPIO to lock as IRQ
3257 * This is used directly by GPIO drivers that want to lock down
3258 * a certain GPIO line to be used for IRQs.
3272 if (!gc->can_sleep && gc->get_direction) { in gpiochip_lock_as_irq()
3276 chip_err(gc, "%s: cannot get GPIO direction\n", in gpiochip_lock_as_irq()
3283 if (test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_lock_as_irq()
3284 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiochip_lock_as_irq()
3286 "%s: tried to flag a GPIO set as output for IRQ\n", in gpiochip_lock_as_irq()
3288 return -EIO; in gpiochip_lock_as_irq()
3291 set_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_lock_as_irq()
3292 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_lock_as_irq()
3295 * If the consumer has not set up a label (such as when the in gpiochip_lock_as_irq()
3296 * IRQ is referenced from .to_irq()) we set up a label here in gpiochip_lock_as_irq()
3299 if (!desc->label) in gpiochip_lock_as_irq()
3307 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3308 * @gc: the chip the GPIO to lock belongs to
3309 * @offset: the offset of the GPIO to lock as IRQ
3311 * This is used directly by GPIO drivers that want to indicate
3312 * that a certain GPIO is no longer used exclusively for IRQ.
3322 clear_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_unlock_as_irq()
3323 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_unlock_as_irq()
3326 if (desc->label && !strcmp(desc->label, "interrupt")) in gpiochip_unlock_as_irq()
3336 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) in gpiochip_disable_irq()
3337 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_disable_irq()
3346 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { in gpiochip_enable_irq()
3351 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_enable_irq()
3352 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); in gpiochip_enable_irq()
3353 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_enable_irq()
3360 if (offset >= gc->ngpio) in gpiochip_line_is_irq()
3363 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_irq()
3371 if (!try_module_get(gc->gpiodev->owner)) in gpiochip_reqres_irq()
3372 return -ENODEV; in gpiochip_reqres_irq()
3377 module_put(gc->gpiodev->owner); in gpiochip_reqres_irq()
3387 module_put(gc->gpiodev->owner); in gpiochip_relres_irq()
3393 if (offset >= gc->ngpio) in gpiochip_line_is_open_drain()
3396 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_drain()
3402 if (offset >= gc->ngpio) in gpiochip_line_is_open_source()
3405 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_source()
3411 if (offset >= gc->ngpio) in gpiochip_line_is_persistent()
3414 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_persistent()
3419 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3420 * @desc: gpio whose value will be returned
3422 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3436 * gpiod_get_value_cansleep() - return a gpio's value
3437 * @desc: gpio whose value will be returned
3439 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3454 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value_cansleep()
3462 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3464 * @desc_array: array of GPIO descriptors whose values will be read
3481 return -EINVAL; in gpiod_get_raw_array_value_cansleep()
3489 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3491 * @desc_array: array of GPIO descriptors whose values will be read
3507 return -EINVAL; in gpiod_get_array_value_cansleep()
3515 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3516 * @desc: gpio whose value will be assigned
3519 * Set the raw value of the GPIO, i.e. the value of its physical line without
3533 * gpiod_set_value_cansleep() - assign a gpio's value
3534 * @desc: gpio whose value will be assigned
3537 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3551 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3553 * @desc_array: array of GPIO descriptors whose values will be assigned
3569 return -EINVAL; in gpiod_set_raw_array_value_cansleep()
3576 * gpiod_add_lookup_tables() - register GPIO device consumers
3587 list_add_tail(&tables[i]->list, &gpio_lookup_list); in gpiod_add_lookup_tables()
3593 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3595 * @desc_array: array of GPIO descriptors whose values will be assigned
3611 return -EINVAL; in gpiod_set_array_value_cansleep()
3619 * gpiod_add_lookup_table() - register GPIO device consumers
3626 list_add_tail(&table->list, &gpio_lookup_list); in gpiod_add_lookup_table()
3633 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3640 list_del(&table->list); in gpiod_remove_lookup_table()
3647 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3648 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3657 for (hog = &hogs[0]; hog->chip_label; hog++) { in gpiod_add_hogs()
3658 list_add_tail(&hog->list, &gpio_machine_hogs); in gpiod_add_hogs()
3664 gc = find_chip_by_name(hog->chip_label); in gpiod_add_hogs()
3681 if (table->dev_id && dev_id) { in gpiod_find_lookup_table()
3684 * a match in gpiod_find_lookup_table()
3686 if (!strcmp(table->dev_id, dev_id)) in gpiod_find_lookup_table()
3691 * a match in gpiod_find_lookup_table()
3693 if (dev_id == table->dev_id) in gpiod_find_lookup_table()
3707 struct gpio_desc *desc = ERR_PTR(-ENOENT); in gpiod_find()
3715 for (p = &table->table[0]; p->key; p++) { in gpiod_find()
3719 if (p->idx != idx) in gpiod_find()
3722 /* If the lookup entry has a con_id, require exact match */ in gpiod_find()
3723 if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) in gpiod_find()
3726 if (p->chip_hwnum == U16_MAX) { in gpiod_find()
3727 desc = gpio_name_to_desc(p->key); in gpiod_find()
3729 *flags = p->flags; in gpiod_find()
3733 dev_warn(dev, "cannot find GPIO line %s, deferring\n", in gpiod_find()
3734 p->key); in gpiod_find()
3735 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
3738 gc = find_chip_by_name(p->key); in gpiod_find()
3742 * As the lookup table indicates a chip with in gpiod_find()
3743 * p->key should exist, assume it may in gpiod_find()
3748 dev_warn(dev, "cannot find GPIO chip %s, deferring\n", in gpiod_find()
3749 p->key); in gpiod_find()
3750 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
3753 if (gc->ngpio <= p->chip_hwnum) { in gpiod_find()
3755 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n", in gpiod_find()
3756 idx, p->chip_hwnum, gc->ngpio - 1, in gpiod_find()
3757 gc->label); in gpiod_find()
3758 return ERR_PTR(-EINVAL); in gpiod_find()
3761 desc = gpiochip_get_desc(gc, p->chip_hwnum); in gpiod_find()
3762 *flags = p->flags; in gpiod_find()
3778 return -ENOENT; in platform_gpio_count()
3780 for (p = &table->table[0]; p->key; p++) { in platform_gpio_count()
3781 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || in platform_gpio_count()
3782 (!con_id && !p->con_id)) in platform_gpio_count()
3786 return -ENOENT; in platform_gpio_count()
3792 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3794 * @con_id: function within the GPIO consumer
3795 * @index: index of the GPIO to obtain for the consumer
3796 * @flags: GPIO initialization flags
3797 * @label: label to attach to the requested GPIO
3802 * The function properly finds the corresponding GPIO using whatever is the
3803 * underlying firmware interface and then makes sure that the GPIO
3807 * On successful request the GPIO pin is configured in accordance with
3823 snprintf(prop_name, sizeof(prop_name), "%s-%s", in fwnode_gpiod_get_index()
3831 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) in fwnode_gpiod_get_index()
3840 * gpiod_count - return the number of GPIOs associated with a device / function
3841 * or -ENOENT if no GPIO has been assigned to the requested function
3842 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3843 * @con_id: function within the GPIO consumer
3847 int count = -ENOENT; in gpiod_count()
3849 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) in gpiod_count()
3862 * gpiod_get - obtain a GPIO for a given GPIO function
3863 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3864 * @con_id: function within the GPIO consumer
3865 * @flags: optional GPIO initialization flags
3867 * Return the GPIO descriptor corresponding to the function con_id of device
3868 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3869 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3879 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3880 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3881 * @con_id: function within the GPIO consumer
3882 * @flags: optional GPIO initialization flags
3884 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3898 * gpiod_configure_flags - helper function to configure a given GPIO
3899 * @desc: gpio whose value will be assigned
3900 * @con_id: function within the GPIO consumer
3901 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3903 * @dflags: gpiod_flags - optional GPIO initialization flags
3905 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3907 * occurred while trying to acquire the GPIO.
3915 set_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_configure_flags()
3918 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
3924 * first place, so print a little warning here. in gpiod_configure_flags()
3926 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
3932 set_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_configure_flags()
3936 "both pull-up and pull-down enabled, invalid configuration\n"); in gpiod_configure_flags()
3937 return -EINVAL; in gpiod_configure_flags()
3941 set_bit(FLAG_PULL_UP, &desc->flags); in gpiod_configure_flags()
3943 set_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_configure_flags()
3966 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3967 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3968 * @con_id: function within the GPIO consumer
3969 * @idx: index of the GPIO to obtain in the consumer
3970 * @flags: optional GPIO initialization flags
3975 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3977 * occurred while trying to acquire the GPIO.
3987 /* Maybe we have a device name, maybe not */ in gpiod_get_index()
3990 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); in gpiod_get_index()
3994 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { in gpiod_get_index()
3995 dev_dbg(dev, "using device tree for GPIO lookup\n"); in gpiod_get_index()
3998 dev_dbg(dev, "using ACPI for GPIO lookup\n"); in gpiod_get_index()
4005 * a result. In that case, use platform lookup as a fallback. in gpiod_get_index()
4007 if (!desc || desc == ERR_PTR(-ENOENT)) { in gpiod_get_index()
4008 dev_dbg(dev, "using lookup tables for GPIO lookup\n"); in gpiod_get_index()
4013 dev_dbg(dev, "No GPIO consumer %s found\n", con_id); in gpiod_get_index()
4018 * If a connection label was passed use that, else attempt to use in gpiod_get_index()
4023 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { in gpiod_get_index()
4026 * the same GPIO line: we just return here without in gpiod_get_index()
4027 * further initialization. It is a bit if a hack. in gpiod_get_index()
4032 dev_info(dev, "nonexclusive access to GPIO for %s\n", in gpiod_get_index()
4042 dev_dbg(dev, "setup of GPIO %s failed\n", con_id); in gpiod_get_index()
4047 blocking_notifier_call_chain(&desc->gdev->notifier, in gpiod_get_index()
4055 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4057 * @propname: name of the firmware property representing the GPIO
4058 * @index: index of the GPIO to obtain for the consumer
4059 * @dflags: GPIO initialization flags
4060 * @label: label to attach to the requested GPIO
4065 * The function properly finds the corresponding GPIO using whatever is the
4066 * underlying firmware interface and then makes sure that the GPIO
4070 * On successful request the GPIO pin is configured in accordance with
4081 struct gpio_desc *desc = ERR_PTR(-ENODEV); in fwnode_get_named_gpiod()
4085 return ERR_PTR(-EINVAL); in fwnode_get_named_gpiod()
4115 blocking_notifier_call_chain(&desc->gdev->notifier, in fwnode_get_named_gpiod()
4123 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4125 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4126 * @con_id: function within the GPIO consumer
4127 * @index: index of the GPIO to obtain in the consumer
4128 * @flags: optional GPIO initialization flags
4130 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4143 if (PTR_ERR(desc) == -ENOENT) in gpiod_get_index_optional()
4152 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4153 * @desc: gpio whose value will be assigned
4154 * @name: gpio line name
4155 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4157 * @dflags: gpiod_flags - optional GPIO initialization flags
4174 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", in gpiod_hog()
4175 name, gc->label, hwnum, ret); in gpiod_hog()
4179 /* Mark GPIO as hogged so it can be identified and removed later */ in gpiod_hog()
4180 set_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_hog()
4191 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4192 * @gc: gpio chip to act on
4198 for (id = 0; id < gc->ngpio; id++) { in gpiochip_free_hogs()
4199 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags)) in gpiochip_free_hogs()
4200 gpiochip_free_own_desc(&gc->gpiodev->descs[id]); in gpiochip_free_hogs()
4205 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4206 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4207 * @con_id: function within the GPIO consumer
4208 * @flags: optional GPIO initialization flags
4210 * This function acquires all the GPIOs defined under a given function.
4212 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4213 * no GPIO has been assigned to the requested function, or another IS_ERR()
4232 return ERR_PTR(-ENOMEM); in gpiod_get_array()
4234 for (descs->ndescs = 0; descs->ndescs < count; ) { in gpiod_get_array()
4235 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); in gpiod_get_array()
4241 descs->desc[descs->ndescs] = desc; in gpiod_get_array()
4246 * its chip as a candidate for fast bitmap processing path. in gpiod_get_array()
4248 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { in gpiod_get_array()
4251 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ? in gpiod_get_array()
4252 gc->ngpio : count); in gpiod_get_array()
4259 return ERR_PTR(-ENOMEM); in gpiod_get_array()
4263 struct_size(descs, desc, descs->ndescs + 1)); in gpiod_get_array()
4267 array_info = (void *)(descs->desc + count); in gpiod_get_array()
4268 array_info->get_mask = array_info->invert_mask + in gpiod_get_array()
4270 array_info->set_mask = array_info->get_mask + in gpiod_get_array()
4273 array_info->desc = descs->desc; in gpiod_get_array()
4274 array_info->size = count; in gpiod_get_array()
4275 array_info->chip = gc; in gpiod_get_array()
4276 bitmap_set(array_info->get_mask, descs->ndescs, in gpiod_get_array()
4277 count - descs->ndescs); in gpiod_get_array()
4278 bitmap_set(array_info->set_mask, descs->ndescs, in gpiod_get_array()
4279 count - descs->ndescs); in gpiod_get_array()
4280 descs->info = array_info; in gpiod_get_array()
4283 if (array_info && array_info->chip != gc) { in gpiod_get_array()
4284 __clear_bit(descs->ndescs, array_info->get_mask); in gpiod_get_array()
4285 __clear_bit(descs->ndescs, array_info->set_mask); in gpiod_get_array()
4292 gpio_chip_hwgpio(desc) != descs->ndescs) { in gpiod_get_array()
4298 if (bitmap_full(array_info->get_mask, descs->ndescs)) { in gpiod_get_array()
4301 __clear_bit(descs->ndescs, in gpiod_get_array()
4302 array_info->get_mask); in gpiod_get_array()
4303 __clear_bit(descs->ndescs, in gpiod_get_array()
4304 array_info->set_mask); in gpiod_get_array()
4308 if (gpiochip_line_is_open_drain(gc, descs->ndescs) || in gpiod_get_array()
4309 gpiochip_line_is_open_source(gc, descs->ndescs)) in gpiod_get_array()
4310 __clear_bit(descs->ndescs, in gpiod_get_array()
4311 array_info->set_mask); in gpiod_get_array()
4314 __set_bit(descs->ndescs, in gpiod_get_array()
4315 array_info->invert_mask); in gpiod_get_array()
4318 descs->ndescs++; in gpiod_get_array()
4322 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n", in gpiod_get_array()
4323 array_info->chip->label, array_info->size, in gpiod_get_array()
4324 *array_info->get_mask, *array_info->set_mask, in gpiod_get_array()
4325 *array_info->invert_mask); in gpiod_get_array()
4331 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4333 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4334 * @con_id: function within the GPIO consumer
4335 * @flags: optional GPIO initialization flags
4337 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4347 if (PTR_ERR(descs) == -ENOENT) in gpiod_get_array_optional()
4355 * gpiod_put - dispose of a GPIO descriptor
4356 * @desc: GPIO descriptor to dispose of
4368 * gpiod_put_array - dispose of multiple GPIO descriptors
4375 for (i = 0; i < descs->ndescs; i++) in gpiod_put_array()
4376 gpiod_put(descs->desc[i]); in gpiod_put_array()
4386 * Only match if the fwnode doesn't already have a proper struct device in gpio_bus_match()
4389 if (dev->fwnode && dev->fwnode->dev != dev) in gpio_bus_match()
4397 * The DT node of some GPIO chips have a "compatible" property, but in gpio_stub_drv_probe()
4398 * never have a struct device added and probed by a driver to register in gpio_stub_drv_probe()
4399 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause in gpio_stub_drv_probe()
4400 * the consumers of the GPIO chip to get probe deferred forever because in gpio_stub_drv_probe()
4401 * they will be waiting for a device associated with the GPIO chip in gpio_stub_drv_probe()
4402 * firmware node to get added and bound to a driver. in gpio_stub_drv_probe()
4405 * gpio_device of the GPIO chip with the firmware node and then simply in gpio_stub_drv_probe()
4421 /* Register GPIO sysfs bus */ in gpiolib_dev_init()
4424 pr_err("gpiolib: could not register GPIO bus type\n"); in gpiolib_dev_init()
4430 pr_err("gpiolib: could not register GPIO stub driver\n"); in gpiolib_dev_init()
4459 struct gpio_chip *gc = gdev->chip; in gpiolib_dbg_show()
4460 unsigned gpio = gdev->base; in gpiolib_dbg_show() local
4461 struct gpio_desc *gdesc = &gdev->descs[0]; in gpiolib_dbg_show()
4466 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) { in gpiolib_dbg_show()
4467 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { in gpiolib_dbg_show()
4468 if (gdesc->name) { in gpiolib_dbg_show()
4469 seq_printf(s, " gpio-%-3d (%-20.20s)\n", in gpiolib_dbg_show()
4470 gpio, gdesc->name); in gpiolib_dbg_show()
4476 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); in gpiolib_dbg_show()
4477 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); in gpiolib_dbg_show()
4478 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags); in gpiolib_dbg_show()
4479 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s", in gpiolib_dbg_show()
4480 gpio, gdesc->name ? gdesc->name : "", gdesc->label, in gpiolib_dbg_show()
4482 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ", in gpiolib_dbg_show()
4495 s->private = ""; in gpiolib_seq_start()
4499 if (index-- == 0) { in gpiolib_seq_start()
4515 if (list_is_last(&gdev->list, &gpio_devices)) in gpiolib_seq_next()
4518 ret = list_entry(gdev->list.next, struct gpio_device, list); in gpiolib_seq_next()
4521 s->private = "\n"; in gpiolib_seq_next()
4534 struct gpio_chip *gc = gdev->chip; in gpiolib_seq_show()
4538 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, in gpiolib_seq_show()
4539 dev_name(&gdev->dev)); in gpiolib_seq_show()
4543 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, in gpiolib_seq_show()
4544 dev_name(&gdev->dev), in gpiolib_seq_show()
4545 gdev->base, gdev->base + gdev->ngpio - 1); in gpiolib_seq_show()
4546 parent = gc->parent; in gpiolib_seq_show()
4549 parent->bus ? parent->bus->name : "no-bus", in gpiolib_seq_show()
4551 if (gc->label) in gpiolib_seq_show()
4552 seq_printf(s, ", %s", gc->label); in gpiolib_seq_show()
4553 if (gc->can_sleep) in gpiolib_seq_show()
4557 if (gc->dbg_show) in gpiolib_seq_show()
4558 gc->dbg_show(s, gc); in gpiolib_seq_show()
4575 /* /sys/kernel/debug/gpio */ in gpiolib_debugfs_init()
4576 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops); in gpiolib_debugfs_init()