Lines Matching +full:irq +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/irq.h>
26 #include "gpiolib-of.h"
27 #include "gpiolib-acpi.h"
28 #include "gpiolib-cdev.h"
29 #include "gpiolib-sysfs.h"
38 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
56 /* Device and char device-related information */
67 * Number of GPIOs to use for the fast path in set array
97 d->label = label; in desc_set_label()
101 * gpio_to_desc - Convert a GPIO number to its descriptor
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()
133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
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
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()
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
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()
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()
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.
339 * 1. Non-unique names are still accepted,
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()
351 gpio = gpio_name_to_desc(gc->names[i]); in gpiochip_set_desc_names()
353 dev_warn(&gdev->dev, in gpiochip_set_desc_names()
355 gc->names[i]); 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
369 * Looks for device property "gpio-line-names" and if it exists assigns
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()
420 /* Assume by default all GPIOs are valid */ 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()
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()
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()
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()
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()
660 base = gpiochip_find_base(gc->ngpio); 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()
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()
788 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__, 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
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()
846 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); in gpiochip_remove()
855 put_device(&gdev->dev); in gpiochip_remove()
860 * gpiochip_find() - iterator for locating a specific gpio_chip
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
956 * @parent_irq: the irq number corresponding to the parent IRQ for this
958 * @parent_handler: the parent interrupt handler for the accumulated IRQ
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()
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()
985 chip_err(gc, "out of memory allocating parent IRQ\n"); 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
1003 * @parent_irq: the irq number corresponding to the parent IRQ for this
1017 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1026 /* DT will deal with mapping each IRQ as we go along */ in gpiochip_set_hierarchical_irqchip()
1027 if (is_of_node(gc->irq.fwnode)) 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()
1050 * only to check if the child IRQ is valid or not. 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()
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()
1116 unsigned int irq, in gpiochip_hierarchy_irq_domain_alloc() argument
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()
1140 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq); in gpiochip_hierarchy_irq_domain_alloc()
1142 ret = girq->child_to_parent_hwirq(gc, hwirq, type, in gpiochip_hierarchy_irq_domain_alloc()
1155 irq, 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()
1161 irq_set_probe(irq); 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()
1169 irq, parent_hwirq); 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()
1171 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg); 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
1311 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, in gpiochip_irq_map() argument
1314 struct gpio_chip *gc = d->host_data; in gpiochip_irq_map()
1318 return -ENXIO; in gpiochip_irq_map()
1320 irq_set_chip_data(irq, gc); 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()
1329 irq_set_nested_thread(irq, 1); in gpiochip_irq_map()
1330 irq_set_noprobe(irq); 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()
1351 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) in gpiochip_irq_unmap() argument
1353 struct gpio_chip *gc = d->host_data; in gpiochip_irq_unmap()
1355 if (gc->irq.threaded) in gpiochip_irq_unmap()
1356 irq_set_nested_thread(irq, 0); in gpiochip_irq_unmap()
1357 irq_set_chip_and_handler(irq, NULL, NULL); in gpiochip_irq_unmap()
1358 irq_set_chip_data(irq, NULL); in gpiochip_irq_unmap()
1375 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1376 * @domain: The IRQ domain used by this IRQ chip
1377 * @data: Outermost irq_data associated with the IRQ
1382 * for the IRQ domain must be the &struct gpio_chip.
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
1395 * @domain: The IRQ domain used by this IRQ chip
1396 * @data: Outermost irq_data associated with the IRQ
1400 * host_data for the IRQ domain must be the &struct gpio_chip.
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()
1418 * an IRQ before the irqchip has been properly registered, 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()
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
1535 * @lock_key: lockdep class for IRQ lock
1536 * @request_key: lockdep class for IRQ request
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()
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()
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()
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()
1604 * The parent IRQ chip is already using the chip_data in gpiochip_add_irqchip()
1605 * for this IRQ chip, so our callbacks simply use the 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() local
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()
1652 /* Remove all IRQ mappings and delete the domain */ in gpiochip_irqchip_remove()
1653 if (gc->irq.domain) { in gpiochip_irqchip_remove()
1654 unsigned int irq; in gpiochip_irqchip_remove() local
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()
1661 irq_dispose_mapping(irq); 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
1688 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1690 * @handler: the irq handler to use (often a predefined irq core function)
1694 * @lock_key: lockdep class for IRQ lock
1695 * @request_key: lockdep class for IRQ request
1698 * gpiochip, providing an irq domain to translate the local IRQs to
1703 * in the gpiochip that shall be used by the driver to handle IRQ number
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()
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
1781 * This function adds an IRQ domain to the 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
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
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
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
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
1930 * 0 on success, or a negative error-code on failure.
1932 * Calling this function directly from a DeviceTree-supported
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()
2071 if (!desc->gdev) { 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
2182 * passed it is a meaningless, non-NULL constant.
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
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
2256 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
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
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()
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
2445 * gpiod_direction_output - set the GPIO direction to output
2461 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_direction_output()
2466 /* GPIOs used for enabled IRQs shall not be set as output */ 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
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
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
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()
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()
2598 * gpiod_is_active_low - test whether a GPIO is active-low or not
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
2617 change_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_toggle_active_low()
2631 *------------------------------------------------------------------------
2633 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
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
2792 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_raw_value()
2798 * gpiod_get_value() - return a gpio's value
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
2833 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2846 return -EINVAL; in gpiod_get_raw_array_value()
2854 * gpiod_get_array_value() - read values from an array of GPIOs
2860 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2872 return -EINVAL; in gpiod_get_array_value()
2880 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
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.
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()
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
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
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
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
3130 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3142 return -EINVAL; in gpiod_set_raw_array_value()
3149 * gpiod_set_array_value() - assign values to an array of GPIOs
3155 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3167 return -EINVAL; in gpiod_set_array_value()
3175 * gpiod_cansleep() - report whether gpio value access may sleep
3182 return desc->gdev->chip->can_sleep; in gpiod_cansleep()
3187 * gpiod_set_consumer_name() - set the consumer name for the descriptor
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
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()
3242 * an IRQ before the irqchip has been properly registered, 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
3255 * @offset: the offset of the GPIO to lock as IRQ
3272 if (!gc->can_sleep && gc->get_direction) { in gpiochip_lock_as_irq()
3282 /* To be valid for IRQ the line needs to be input or open drain */ 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()
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
3309 * @offset: the offset of the GPIO to lock as IRQ
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()
3348 * We must not be output when using IRQ UNLESS we are 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()
3376 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset); 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
3436 * gpiod_get_value_cansleep() - return a gpio's value
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
3468 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3481 return -EINVAL; in gpiod_get_raw_array_value_cansleep()
3489 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3495 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3507 return -EINVAL; in gpiod_get_array_value_cansleep()
3515 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3533 * gpiod_set_value_cansleep() - assign a gpio's value
3551 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3557 * Set the raw values of the GPIOs, i.e. the values of the physical lines
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
3599 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
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
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()
3686 if (!strcmp(table->dev_id, dev_id)) 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()
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()
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()
3743 * p->key should exist, assume it may 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()
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
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
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
3868 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3879 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3880 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3886 * that need to handle optional GPIOs.
3898 * gpiod_configure_flags - helper function to configure a given GPIO
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
3915 set_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_configure_flags()
3918 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 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
3972 * This variant of gpiod_get() allows to access GPIOs other than the first
3973 * defined one for functions that define several GPIOs.
3975 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3994 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { in gpiod_get_index()
4007 if (!desc || desc == ERR_PTR(-ENOENT)) { in gpiod_get_index()
4023 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { 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
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
4132 * This is convenient for drivers that need to handle optional GPIOs.
4143 if (PTR_ERR(desc) == -ENOENT) in gpiod_get_index_optional()
4152 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4155 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4157 * @dflags: gpiod_flags - optional GPIO initialization flags
4175 name, gc->label, hwnum, ret); 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
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
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
4214 * code if an error occurred while trying to acquire the GPIOs.
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()
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()
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
4347 if (PTR_ERR(descs) == -ENOENT) in gpiod_get_array_optional()
4355 * gpiod_put - dispose of a GPIO descriptor
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()
4389 if (dev->fwnode && dev->fwnode->dev != dev) in gpio_bus_match()
4459 struct gpio_chip *gc = gdev->chip; in gpiolib_dbg_show()
4460 unsigned gpio = gdev->base; in gpiolib_dbg_show()
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()
4483 is_irq ? "IRQ " : "", 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()