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"
36 * The GPIO programming interface allows for inlining speed-critical
37 * get/set operations for common cases, so that access to SOC-integrated
38 * GPIOs can sometimes cost only an instruction or two per bit.
54 /* Device and char device-related information */
63 * Number of GPIOs to use for the fast path in set array
93 d->label = label; in desc_set_label()
97 * gpio_to_desc - Convert a GPIO number to its descriptor
112 if (gdev->base <= gpio && in gpio_to_desc()
113 gdev->base + gdev->ngpio > gpio) { in gpio_to_desc()
115 return &gdev->descs[gpio - gdev->base]; in gpio_to_desc()
129 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
135 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
141 struct gpio_device *gdev = gc->gpiodev; in gpiochip_get_desc()
143 if (hwnum >= gdev->ngpio) in gpiochip_get_desc()
144 return ERR_PTR(-EINVAL); in gpiochip_get_desc()
146 return &gdev->descs[hwnum]; in gpiochip_get_desc()
151 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
162 return desc->gdev->base + (desc - &desc->gdev->descs[0]); in desc_to_gpio()
168 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
173 if (!desc || !desc->gdev) in gpiod_to_chip()
175 return desc->gdev->chip; in gpiod_to_chip()
179 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
183 int base = ARCH_NR_GPIOS - ngpio; in gpiochip_find_base()
187 if (gdev->base + gdev->ngpio <= base) in gpiochip_find_base()
191 base = gdev->base - ngpio; in gpiochip_find_base()
199 return -ENOSPC; in gpiochip_find_base()
204 * gpiod_get_direction - return the current direction of a GPIO
224 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && in gpiod_get_direction()
225 test_bit(FLAG_IS_OUT, &desc->flags)) in gpiod_get_direction()
228 if (!gc->get_direction) in gpiod_get_direction()
229 return -ENOTSUPP; in gpiod_get_direction()
231 ret = gc->get_direction(gc, offset); in gpiod_get_direction()
239 assign_bit(FLAG_IS_OUT, &desc->flags, !ret); in gpiod_get_direction()
247 * by range(means [base, base + ngpio - 1]) order.
249 * Return -EBUSY if the new chip overlaps with some other chip's integer
258 list_add_tail(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
263 if (gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list()
265 list_add(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
270 if (prev->base + prev->ngpio <= gdev->base) { in gpiodev_add_to_list()
272 list_add_tail(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
278 if (&next->list == &gpio_devices) in gpiodev_add_to_list()
282 if (prev->base + prev->ngpio <= gdev->base in gpiodev_add_to_list()
283 && gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list()
284 list_add(&gdev->list, &prev->list); in gpiodev_add_to_list()
289 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n"); in gpiodev_add_to_list()
290 return -EBUSY; in gpiodev_add_to_list()
312 for (i = 0; i != gdev->ngpio; ++i) { in gpio_name_to_desc()
313 struct gpio_desc *desc = &gdev->descs[i]; in gpio_name_to_desc()
315 if (!desc->name) in gpio_name_to_desc()
318 if (!strcmp(desc->name, name)) { in gpio_name_to_desc()
331 * Take the names from gc->names and assign them to their GPIO descriptors.
335 * 1. Non-unique names are still accepted,
340 struct gpio_device *gdev = gc->gpiodev; in gpiochip_set_desc_names()
344 for (i = 0; i != gc->ngpio; ++i) { in gpiochip_set_desc_names()
347 gpio = gpio_name_to_desc(gc->names[i]); in gpiochip_set_desc_names()
349 dev_warn(&gdev->dev, in gpiochip_set_desc_names()
351 gc->names[i]); in gpiochip_set_desc_names()
355 for (i = 0; i != gc->ngpio; ++i) in gpiochip_set_desc_names()
356 gdev->descs[i].name = gc->names[i]; in gpiochip_set_desc_names()
362 * devprop_gpiochip_set_names - Set GPIO line names using device properties
365 * Looks for device property "gpio-line-names" and if it exists assigns
372 struct gpio_device *gdev = chip->gpiodev; in devprop_gpiochip_set_names()
373 struct device *dev = chip->parent; in devprop_gpiochip_set_names()
382 count = device_property_string_array_count(dev, "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 = device_property_read_string_array(dev, "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()
476 list_del(&gdev->list); in gpiodevice_release()
477 ida_free(&gpio_ida, gdev->id); in gpiodevice_release()
478 kfree_const(gdev->label); in gpiodevice_release()
479 kfree(gdev->descs); in gpiodevice_release()
491 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
492 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
508 gdev->dev.release = gpiodevice_release; in gpiochip_setup_dev()
509 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base, in gpiochip_setup_dev()
510 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic"); in gpiochip_setup_dev()
524 desc = gpiochip_get_desc(gc, hog->chip_hwnum); in gpiochip_machine_hog()
531 if (test_bit(FLAG_IS_HOGGED, &desc->flags)) in gpiochip_machine_hog()
534 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); in gpiochip_machine_hog()
537 __func__, gc->label, hog->chip_hwnum, rv); in gpiochip_machine_hog()
547 if (!strcmp(gc->label, hog->chip_label)) in machine_gpiochip_add()
562 dev_err(&gdev->dev, in gpiochip_setup_devs()
574 int base = gc->base; in gpiochip_add_data_with_key()
583 return -ENOMEM; in gpiochip_add_data_with_key()
584 gdev->dev.bus = &gpio_bus_type; in gpiochip_add_data_with_key()
585 gdev->chip = gc; in gpiochip_add_data_with_key()
586 gc->gpiodev = gdev; in gpiochip_add_data_with_key()
587 if (gc->parent) { in gpiochip_add_data_with_key()
588 gdev->dev.parent = gc->parent; in gpiochip_add_data_with_key()
589 gdev->dev.of_node = gc->parent->of_node; in gpiochip_add_data_with_key()
594 if (gc->of_node) in gpiochip_add_data_with_key()
595 gdev->dev.of_node = gc->of_node; in gpiochip_add_data_with_key()
597 gc->of_node = gdev->dev.of_node; in gpiochip_add_data_with_key()
600 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL); in gpiochip_add_data_with_key()
601 if (gdev->id < 0) { in gpiochip_add_data_with_key()
602 ret = gdev->id; in gpiochip_add_data_with_key()
605 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); in gpiochip_add_data_with_key()
606 device_initialize(&gdev->dev); in gpiochip_add_data_with_key()
607 dev_set_drvdata(&gdev->dev, gdev); in gpiochip_add_data_with_key()
608 if (gc->parent && gc->parent->driver) in gpiochip_add_data_with_key()
609 gdev->owner = gc->parent->driver->owner; in gpiochip_add_data_with_key()
610 else if (gc->owner) in gpiochip_add_data_with_key()
611 /* TODO: remove chip->owner */ in gpiochip_add_data_with_key()
612 gdev->owner = gc->owner; in gpiochip_add_data_with_key()
614 gdev->owner = THIS_MODULE; in gpiochip_add_data_with_key()
616 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); in gpiochip_add_data_with_key()
617 if (!gdev->descs) { in gpiochip_add_data_with_key()
618 ret = -ENOMEM; in gpiochip_add_data_with_key()
622 if (gc->ngpio == 0) { in gpiochip_add_data_with_key()
624 ret = -EINVAL; in gpiochip_add_data_with_key()
628 if (gc->ngpio > FASTPATH_NGPIO) in gpiochip_add_data_with_key()
630 gc->ngpio, FASTPATH_NGPIO); in gpiochip_add_data_with_key()
632 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL); in gpiochip_add_data_with_key()
633 if (!gdev->label) { in gpiochip_add_data_with_key()
634 ret = -ENOMEM; in gpiochip_add_data_with_key()
638 gdev->ngpio = gc->ngpio; in gpiochip_add_data_with_key()
639 gdev->data = data; in gpiochip_add_data_with_key()
651 base = gpiochip_find_base(gc->ngpio); in gpiochip_add_data_with_key()
663 gc->base = base; in gpiochip_add_data_with_key()
665 gdev->base = base; in gpiochip_add_data_with_key()
673 for (i = 0; i < gc->ngpio; i++) in gpiochip_add_data_with_key()
674 gdev->descs[i].gdev = gdev; in gpiochip_add_data_with_key()
678 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier); in gpiochip_add_data_with_key()
681 INIT_LIST_HEAD(&gdev->pin_ranges); in gpiochip_add_data_with_key()
684 if (gc->names) in gpiochip_add_data_with_key()
703 for (i = 0; i < gc->ngpio; i++) { in gpiochip_add_data_with_key()
704 struct gpio_desc *desc = &gdev->descs[i]; in gpiochip_add_data_with_key()
706 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { in gpiochip_add_data_with_key()
708 &desc->flags, !gc->get_direction(gc, i)); in gpiochip_add_data_with_key()
711 &desc->flags, !gc->direction_input); in gpiochip_add_data_with_key()
764 list_del(&gdev->list); in gpiochip_add_data_with_key()
767 kfree_const(gdev->label); in gpiochip_add_data_with_key()
769 kfree(gdev->descs); in gpiochip_add_data_with_key()
771 ida_free(&gpio_ida, gdev->id); in gpiochip_add_data_with_key()
774 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__, in gpiochip_add_data_with_key()
775 gdev->base, gdev->base + gdev->ngpio - 1, in gpiochip_add_data_with_key()
776 gc->label ? : "generic", ret); in gpiochip_add_data_with_key()
783 * gpiochip_get_data() - get per-subdriver data for the chip
787 * The per-subdriver data for the chip.
791 return gc->gpiodev->data; in gpiochip_get_data()
796 * gpiochip_remove() - unregister a gpio_chip
799 * A gpio_chip with any GPIOs still requested may not be removed.
803 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove()
811 gdev->chip = NULL; in gpiochip_remove()
821 gdev->data = NULL; in gpiochip_remove()
824 for (i = 0; i < gdev->ngpio; i++) { in gpiochip_remove()
830 if (i != gdev->ngpio) in gpiochip_remove()
831 dev_crit(&gdev->dev, in gpiochip_remove()
832 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); in gpiochip_remove()
841 put_device(&gdev->dev); in gpiochip_remove()
846 * gpiochip_find() - iterator for locating a specific gpio_chip
852 * 0 if the device doesn't match and non-zero if it does. If the callback is
853 * non-zero, this function will return to the caller and not iterate over any
866 if (gdev->chip && match(gdev->chip, data)) { in gpiochip_find()
867 gc = gdev->chip; in gpiochip_find()
881 return !strcmp(gc->label, name); in gpiochip_match_name()
897 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_hw()
899 if (!girq->init_hw) in gpiochip_irqchip_init_hw()
902 return girq->init_hw(gc); in gpiochip_irqchip_init_hw()
907 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_valid_mask()
909 if (!girq->init_valid_mask) in gpiochip_irqchip_init_valid_mask()
912 girq->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_irqchip_init_valid_mask()
913 if (!girq->valid_mask) in gpiochip_irqchip_init_valid_mask()
914 return -ENOMEM; in gpiochip_irqchip_init_valid_mask()
916 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); in gpiochip_irqchip_init_valid_mask()
923 bitmap_free(gc->irq.valid_mask); in gpiochip_irqchip_free_valid_mask()
924 gc->irq.valid_mask = NULL; in gpiochip_irqchip_free_valid_mask()
933 if (likely(!gc->irq.valid_mask)) in gpiochip_irqchip_irq_valid()
935 return test_bit(offset, gc->irq.valid_mask); in gpiochip_irqchip_irq_valid()
940 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
942 * @parent_irq: the irq number corresponding to the parent IRQ for this
944 * @parent_handler: the parent interrupt handler for the accumulated IRQ
952 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_set_cascaded_irqchip()
953 struct device *dev = &gc->gpiodev->dev; in gpiochip_set_cascaded_irqchip()
955 if (!girq->domain) { in gpiochip_set_cascaded_irqchip()
962 if (gc->can_sleep) { in gpiochip_set_cascaded_irqchip()
967 girq->parents = devm_kcalloc(dev, 1, in gpiochip_set_cascaded_irqchip()
968 sizeof(*girq->parents), in gpiochip_set_cascaded_irqchip()
970 if (!girq->parents) { in gpiochip_set_cascaded_irqchip()
971 chip_err(gc, "out of memory allocating parent IRQ\n"); in gpiochip_set_cascaded_irqchip()
974 girq->parents[0] = parent_irq; in gpiochip_set_cascaded_irqchip()
975 girq->num_parents = 1; in gpiochip_set_cascaded_irqchip()
986 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
989 * @parent_irq: the irq number corresponding to the parent IRQ for this
1003 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1012 /* DT will deal with mapping each IRQ as we go along */ in gpiochip_set_hierarchical_irqchip()
1013 if (is_of_node(gc->irq.fwnode)) in gpiochip_set_hierarchical_irqchip()
1024 if (is_fwnode_irqchip(gc->irq.fwnode)) { in gpiochip_set_hierarchical_irqchip()
1028 for (i = 0; i < gc->ngpio; i++) { in gpiochip_set_hierarchical_irqchip()
1032 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_set_hierarchical_irqchip()
1036 * only to check if the child IRQ is valid or not. in gpiochip_set_hierarchical_irqchip()
1040 ret = girq->child_to_parent_hwirq(gc, i, in gpiochip_set_hierarchical_irqchip()
1045 chip_err(gc, "skip set-up on hwirq %d\n", in gpiochip_set_hierarchical_irqchip()
1050 fwspec.fwnode = gc->irq.fwnode; in gpiochip_set_hierarchical_irqchip()
1052 fwspec.param[0] = girq->child_offset_to_irq(gc, i); in gpiochip_set_hierarchical_irqchip()
1056 ret = __irq_domain_alloc_irqs(gc->irq.domain, in gpiochip_set_hierarchical_irqchip()
1058 -1, in gpiochip_set_hierarchical_irqchip()
1066 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n", in gpiochip_set_hierarchical_irqchip()
1084 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { in gpiochip_hierarchy_irq_domain_translate()
1089 if (is_fwnode_irqchip(fwspec->fwnode)) { in gpiochip_hierarchy_irq_domain_translate()
1098 return -EINVAL; in gpiochip_hierarchy_irq_domain_translate()
1102 unsigned int irq, in gpiochip_hierarchy_irq_domain_alloc() argument
1106 struct gpio_chip *gc = d->host_data; in gpiochip_hierarchy_irq_domain_alloc()
1113 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_hierarchy_irq_domain_alloc()
1117 * The nr_irqs parameter is always one except for PCI multi-MSI in gpiochip_hierarchy_irq_domain_alloc()
1122 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); in gpiochip_hierarchy_irq_domain_alloc()
1126 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq); in gpiochip_hierarchy_irq_domain_alloc()
1128 ret = girq->child_to_parent_hwirq(gc, hwirq, type, in gpiochip_hierarchy_irq_domain_alloc()
1141 irq, in gpiochip_hierarchy_irq_domain_alloc()
1143 gc->irq.chip, in gpiochip_hierarchy_irq_domain_alloc()
1145 girq->handler, in gpiochip_hierarchy_irq_domain_alloc()
1147 irq_set_probe(irq); in gpiochip_hierarchy_irq_domain_alloc()
1150 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type); in gpiochip_hierarchy_irq_domain_alloc()
1152 return -ENOMEM; in gpiochip_hierarchy_irq_domain_alloc()
1155 irq, parent_hwirq); in gpiochip_hierarchy_irq_domain_alloc()
1156 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_hierarchy_irq_domain_alloc()
1157 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg); in gpiochip_hierarchy_irq_domain_alloc()
1162 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST)) in gpiochip_hierarchy_irq_domain_alloc()
1181 ops->activate = gpiochip_irq_domain_activate; in gpiochip_hierarchy_setup_domain_ops()
1182 ops->deactivate = gpiochip_irq_domain_deactivate; in gpiochip_hierarchy_setup_domain_ops()
1183 ops->alloc = gpiochip_hierarchy_irq_domain_alloc; in gpiochip_hierarchy_setup_domain_ops()
1184 ops->free = irq_domain_free_irqs_common; in gpiochip_hierarchy_setup_domain_ops()
1191 if (!ops->translate) in gpiochip_hierarchy_setup_domain_ops()
1192 ops->translate = gpiochip_hierarchy_irq_domain_translate; in gpiochip_hierarchy_setup_domain_ops()
1197 if (!gc->irq.child_to_parent_hwirq || in gpiochip_hierarchy_add_domain()
1198 !gc->irq.fwnode) { in gpiochip_hierarchy_add_domain()
1200 return -EINVAL; in gpiochip_hierarchy_add_domain()
1203 if (!gc->irq.child_offset_to_irq) in gpiochip_hierarchy_add_domain()
1204 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; in gpiochip_hierarchy_add_domain()
1206 if (!gc->irq.populate_parent_alloc_arg) in gpiochip_hierarchy_add_domain()
1207 gc->irq.populate_parent_alloc_arg = in gpiochip_hierarchy_add_domain()
1210 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); in gpiochip_hierarchy_add_domain()
1212 gc->irq.domain = irq_domain_create_hierarchy( in gpiochip_hierarchy_add_domain()
1213 gc->irq.parent_domain, in gpiochip_hierarchy_add_domain()
1215 gc->ngpio, in gpiochip_hierarchy_add_domain()
1216 gc->irq.fwnode, in gpiochip_hierarchy_add_domain()
1217 &gc->irq.child_irq_domain_ops, in gpiochip_hierarchy_add_domain()
1220 if (!gc->irq.domain) in gpiochip_hierarchy_add_domain()
1221 return -ENOMEM; in gpiochip_hierarchy_add_domain()
1223 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); in gpiochip_hierarchy_add_domain()
1230 return !!gc->irq.parent_domain; in gpiochip_hierarchy_is_hierarchical()
1243 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_twocell()
1244 fwspec->param_count = 2; in gpiochip_populate_parent_fwspec_twocell()
1245 fwspec->param[0] = parent_hwirq; in gpiochip_populate_parent_fwspec_twocell()
1246 fwspec->param[1] = parent_type; in gpiochip_populate_parent_fwspec_twocell()
1262 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_fourcell()
1263 fwspec->param_count = 4; in gpiochip_populate_parent_fwspec_fourcell()
1264 fwspec->param[0] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1265 fwspec->param[1] = parent_hwirq; in gpiochip_populate_parent_fwspec_fourcell()
1266 fwspec->param[2] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1267 fwspec->param[3] = parent_type; in gpiochip_populate_parent_fwspec_fourcell()
1277 return -EINVAL; in gpiochip_hierarchy_add_domain()
1288 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1290 * @irq: the global irq number used by this GPIO irqchip irq
1291 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1293 * This function will set up the mapping for a certain IRQ line on a
1297 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, in gpiochip_irq_map() argument
1300 struct gpio_chip *gc = d->host_data; in gpiochip_irq_map()
1304 return -ENXIO; in gpiochip_irq_map()
1306 irq_set_chip_data(irq, gc); in gpiochip_irq_map()
1311 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_irq_map()
1312 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler); in gpiochip_irq_map()
1314 if (gc->irq.threaded) in gpiochip_irq_map()
1315 irq_set_nested_thread(irq, 1); in gpiochip_irq_map()
1316 irq_set_noprobe(irq); in gpiochip_irq_map()
1318 if (gc->irq.num_parents == 1) in gpiochip_irq_map()
1319 ret = irq_set_parent(irq, gc->irq.parents[0]); in gpiochip_irq_map()
1320 else if (gc->irq.map) in gpiochip_irq_map()
1321 ret = irq_set_parent(irq, gc->irq.map[hwirq]); in gpiochip_irq_map()
1327 * No set-up of the hardware will happen if IRQ_TYPE_NONE in gpiochip_irq_map()
1330 if (gc->irq.default_type != IRQ_TYPE_NONE) in gpiochip_irq_map()
1331 irq_set_irq_type(irq, gc->irq.default_type); in gpiochip_irq_map()
1337 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) in gpiochip_irq_unmap() argument
1339 struct gpio_chip *gc = d->host_data; in gpiochip_irq_unmap()
1341 if (gc->irq.threaded) in gpiochip_irq_unmap()
1342 irq_set_nested_thread(irq, 0); in gpiochip_irq_unmap()
1343 irq_set_chip_and_handler(irq, NULL, NULL); in gpiochip_irq_unmap()
1344 irq_set_chip_data(irq, NULL); in gpiochip_irq_unmap()
1361 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1362 * @domain: The IRQ domain used by this IRQ chip
1363 * @data: Outermost irq_data associated with the IRQ
1368 * for the IRQ domain must be the &struct gpio_chip.
1373 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_activate()
1375 return gpiochip_lock_as_irq(gc, data->hwirq); in gpiochip_irq_domain_activate()
1380 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1381 * @domain: The IRQ domain used by this IRQ chip
1382 * @data: Outermost irq_data associated with the IRQ
1386 * host_data for the IRQ domain must be the &struct gpio_chip.
1391 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_deactivate()
1393 return gpiochip_unlock_as_irq(gc, data->hwirq); in gpiochip_irq_domain_deactivate()
1399 struct irq_domain *domain = gc->irq.domain; in gpiochip_to_irq()
1402 return -ENXIO; in gpiochip_to_irq()
1408 spec.fwnode = domain->fwnode; in gpiochip_to_irq()
1410 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset); in gpiochip_to_irq()
1424 return gpiochip_reqres_irq(gc, d->hwirq); in gpiochip_irq_reqres()
1431 gpiochip_relres_irq(gc, d->hwirq); in gpiochip_irq_relres()
1438 if (gc->irq.irq_mask) in gpiochip_irq_mask()
1439 gc->irq.irq_mask(d); in gpiochip_irq_mask()
1440 gpiochip_disable_irq(gc, d->hwirq); in gpiochip_irq_mask()
1447 gpiochip_enable_irq(gc, d->hwirq); in gpiochip_irq_unmask()
1448 if (gc->irq.irq_unmask) in gpiochip_irq_unmask()
1449 gc->irq.irq_unmask(d); in gpiochip_irq_unmask()
1456 gpiochip_enable_irq(gc, d->hwirq); in gpiochip_irq_enable()
1457 gc->irq.irq_enable(d); in gpiochip_irq_enable()
1464 gc->irq.irq_disable(d); in gpiochip_irq_disable()
1465 gpiochip_disable_irq(gc, d->hwirq); in gpiochip_irq_disable()
1470 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_set_irq_hooks()
1472 if (!irqchip->irq_request_resources && in gpiochip_set_irq_hooks()
1473 !irqchip->irq_release_resources) { in gpiochip_set_irq_hooks()
1474 irqchip->irq_request_resources = gpiochip_irq_reqres; in gpiochip_set_irq_hooks()
1475 irqchip->irq_release_resources = gpiochip_irq_relres; in gpiochip_set_irq_hooks()
1477 if (WARN_ON(gc->irq.irq_enable)) in gpiochip_set_irq_hooks()
1480 if (irqchip->irq_enable == gpiochip_irq_enable) { in gpiochip_set_irq_hooks()
1490 if (irqchip->irq_disable) { in gpiochip_set_irq_hooks()
1491 gc->irq.irq_disable = irqchip->irq_disable; in gpiochip_set_irq_hooks()
1492 irqchip->irq_disable = gpiochip_irq_disable; in gpiochip_set_irq_hooks()
1494 gc->irq.irq_mask = irqchip->irq_mask; in gpiochip_set_irq_hooks()
1495 irqchip->irq_mask = gpiochip_irq_mask; in gpiochip_set_irq_hooks()
1498 if (irqchip->irq_enable) { in gpiochip_set_irq_hooks()
1499 gc->irq.irq_enable = irqchip->irq_enable; in gpiochip_set_irq_hooks()
1500 irqchip->irq_enable = gpiochip_irq_enable; in gpiochip_set_irq_hooks()
1502 gc->irq.irq_unmask = irqchip->irq_unmask; in gpiochip_set_irq_hooks()
1503 irqchip->irq_unmask = gpiochip_irq_unmask; in gpiochip_set_irq_hooks()
1508 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1509 * @gc: the GPIO chip to add the IRQ chip to
1510 * @lock_key: lockdep class for IRQ lock
1511 * @request_key: lockdep class for IRQ request
1517 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_add_irqchip()
1526 if (gc->irq.parent_handler && gc->can_sleep) { in gpiochip_add_irqchip()
1528 return -EINVAL; in gpiochip_add_irqchip()
1531 np = gc->gpiodev->dev.of_node; in gpiochip_add_irqchip()
1532 type = gc->irq.default_type; in gpiochip_add_irqchip()
1540 "%s: Ignoring %u default trigger\n", np->full_name, type)) in gpiochip_add_irqchip()
1543 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) { in gpiochip_add_irqchip()
1544 acpi_handle_warn(ACPI_HANDLE(gc->parent), in gpiochip_add_irqchip()
1549 gc->to_irq = gpiochip_to_irq; in gpiochip_add_irqchip()
1550 gc->irq.default_type = type; in gpiochip_add_irqchip()
1551 gc->irq.lock_key = lock_key; in gpiochip_add_irqchip()
1552 gc->irq.request_key = request_key; in gpiochip_add_irqchip()
1561 if (gc->irq.domain_ops) in gpiochip_add_irqchip()
1562 ops = gc->irq.domain_ops; in gpiochip_add_irqchip()
1566 gc->irq.domain = irq_domain_add_simple(np, in gpiochip_add_irqchip()
1567 gc->ngpio, in gpiochip_add_irqchip()
1568 gc->irq.first, in gpiochip_add_irqchip()
1570 if (!gc->irq.domain) in gpiochip_add_irqchip()
1571 return -EINVAL; in gpiochip_add_irqchip()
1574 if (gc->irq.parent_handler) { in gpiochip_add_irqchip()
1575 void *data = gc->irq.parent_handler_data ?: gc; in gpiochip_add_irqchip()
1577 for (i = 0; i < gc->irq.num_parents; i++) { in gpiochip_add_irqchip()
1579 * The parent IRQ chip is already using the chip_data in gpiochip_add_irqchip()
1580 * for this IRQ chip, so our callbacks simply use the in gpiochip_add_irqchip()
1583 irq_set_chained_handler_and_data(gc->irq.parents[i], in gpiochip_add_irqchip()
1584 gc->irq.parent_handler, in gpiochip_add_irqchip()
1597 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1604 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_irqchip_remove()
1609 if (irqchip && gc->irq.parent_handler) { in gpiochip_irqchip_remove()
1610 struct gpio_irq_chip *irq = &gc->irq; in gpiochip_irqchip_remove() local
1613 for (i = 0; i < irq->num_parents; i++) in gpiochip_irqchip_remove()
1614 irq_set_chained_handler_and_data(irq->parents[i], in gpiochip_irqchip_remove()
1618 /* Remove all IRQ mappings and delete the domain */ in gpiochip_irqchip_remove()
1619 if (gc->irq.domain) { in gpiochip_irqchip_remove()
1620 unsigned int irq; in gpiochip_irqchip_remove() local
1622 for (offset = 0; offset < gc->ngpio; offset++) { in gpiochip_irqchip_remove()
1626 irq = irq_find_mapping(gc->irq.domain, offset); in gpiochip_irqchip_remove()
1627 irq_dispose_mapping(irq); in gpiochip_irqchip_remove()
1630 irq_domain_remove(gc->irq.domain); in gpiochip_irqchip_remove()
1634 if (irqchip->irq_request_resources == gpiochip_irq_reqres) { in gpiochip_irqchip_remove()
1635 irqchip->irq_request_resources = NULL; in gpiochip_irqchip_remove()
1636 irqchip->irq_release_resources = NULL; in gpiochip_irqchip_remove()
1638 if (irqchip->irq_enable == gpiochip_irq_enable) { in gpiochip_irqchip_remove()
1639 irqchip->irq_enable = gc->irq.irq_enable; in gpiochip_irqchip_remove()
1640 irqchip->irq_disable = gc->irq.irq_disable; in gpiochip_irqchip_remove()
1643 gc->irq.irq_enable = NULL; in gpiochip_irqchip_remove()
1644 gc->irq.irq_disable = NULL; in gpiochip_irqchip_remove()
1645 gc->irq.chip = NULL; in gpiochip_irqchip_remove()
1651 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1654 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1656 * @handler: the irq handler to use (often a predefined irq core function)
1660 * @lock_key: lockdep class for IRQ lock
1661 * @request_key: lockdep class for IRQ request
1664 * gpiochip, providing an irq domain to translate the local IRQs to
1669 * in the gpiochip that shall be used by the driver to handle IRQ number
1674 * the pins on the gpiochip can generate a unique IRQ. Everything else
1689 return -EINVAL; in gpiochip_irqchip_add_key()
1691 if (!gc->parent) { in gpiochip_irqchip_add_key()
1693 return -EINVAL; in gpiochip_irqchip_add_key()
1695 gc->irq.threaded = threaded; in gpiochip_irqchip_add_key()
1696 of_node = gc->parent->of_node; in gpiochip_irqchip_add_key()
1700 * FIXME: get rid of this and use gc->parent->of_node in gpiochip_irqchip_add_key()
1703 if (gc->of_node) in gpiochip_irqchip_add_key()
1704 of_node = gc->of_node; in gpiochip_irqchip_add_key()
1708 * used to configure the interrupts, as you may end-up with in gpiochip_irqchip_add_key()
1714 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) { in gpiochip_irqchip_add_key()
1715 acpi_handle_warn(ACPI_HANDLE(gc->parent), in gpiochip_irqchip_add_key()
1720 gc->irq.chip = irqchip; in gpiochip_irqchip_add_key()
1721 gc->irq.handler = handler; in gpiochip_irqchip_add_key()
1722 gc->irq.default_type = type; in gpiochip_irqchip_add_key()
1723 gc->to_irq = gpiochip_to_irq; in gpiochip_irqchip_add_key()
1724 gc->irq.lock_key = lock_key; in gpiochip_irqchip_add_key()
1725 gc->irq.request_key = request_key; in gpiochip_irqchip_add_key()
1726 gc->irq.domain = irq_domain_add_simple(of_node, in gpiochip_irqchip_add_key()
1727 gc->ngpio, first_irq, in gpiochip_irqchip_add_key()
1729 if (!gc->irq.domain) { in gpiochip_irqchip_add_key()
1730 gc->irq.chip = NULL; in gpiochip_irqchip_add_key()
1731 return -EINVAL; in gpiochip_irqchip_add_key()
1743 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1747 * This function adds an IRQ domain to the gpiochip.
1753 return -EINVAL; in gpiochip_irqchip_add_domain()
1755 gc->to_irq = gpiochip_to_irq; in gpiochip_irqchip_add_domain()
1756 gc->irq.domain = domain; in gpiochip_irqchip_add_domain()
1787 * gpiochip_generic_request() - request the gpio function for a pin
1794 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_request()
1798 return pinctrl_gpio_request(gc->gpiodev->base + offset); in gpiochip_generic_request()
1803 * gpiochip_generic_free() - free the gpio function from a pin
1810 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_free()
1814 pinctrl_gpio_free(gc->gpiodev->base + offset); in gpiochip_generic_free()
1819 * gpiochip_generic_config() - apply configuration for a pin
1827 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config); in gpiochip_generic_config()
1834 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1840 * Calling this function directly from a DeviceTree-supported
1843 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1850 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pingroup_range()
1856 return -ENOMEM; in gpiochip_add_pingroup_range()
1860 pin_range->range.id = gpio_offset; in gpiochip_add_pingroup_range()
1861 pin_range->range.gc = gc; in gpiochip_add_pingroup_range()
1862 pin_range->range.name = gc->label; in gpiochip_add_pingroup_range()
1863 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pingroup_range()
1864 pin_range->pctldev = pctldev; in gpiochip_add_pingroup_range()
1867 &pin_range->range.pins, in gpiochip_add_pingroup_range()
1868 &pin_range->range.npins); in gpiochip_add_pingroup_range()
1874 pinctrl_add_gpio_range(pctldev, &pin_range->range); in gpiochip_add_pingroup_range()
1876 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n", in gpiochip_add_pingroup_range()
1877 gpio_offset, gpio_offset + pin_range->range.npins - 1, in gpiochip_add_pingroup_range()
1880 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pingroup_range()
1887 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1896 * 0 on success, or a negative error-code on failure.
1898 * Calling this function directly from a DeviceTree-supported
1901 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1908 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pin_range()
1914 return -ENOMEM; in gpiochip_add_pin_range()
1918 pin_range->range.id = gpio_offset; in gpiochip_add_pin_range()
1919 pin_range->range.gc = gc; in gpiochip_add_pin_range()
1920 pin_range->range.name = gc->label; in gpiochip_add_pin_range()
1921 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pin_range()
1922 pin_range->range.pin_base = pin_offset; in gpiochip_add_pin_range()
1923 pin_range->range.npins = npins; in gpiochip_add_pin_range()
1924 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, in gpiochip_add_pin_range()
1925 &pin_range->range); in gpiochip_add_pin_range()
1926 if (IS_ERR(pin_range->pctldev)) { in gpiochip_add_pin_range()
1927 ret = PTR_ERR(pin_range->pctldev); in gpiochip_add_pin_range()
1932 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n", in gpiochip_add_pin_range()
1933 gpio_offset, gpio_offset + npins - 1, in gpiochip_add_pin_range()
1935 pin_offset, pin_offset + npins - 1); in gpiochip_add_pin_range()
1937 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pin_range()
1944 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1950 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove_pin_ranges()
1952 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { in gpiochip_remove_pin_ranges()
1953 list_del(&pin_range->node); in gpiochip_remove_pin_ranges()
1954 pinctrl_remove_gpio_range(pin_range->pctldev, in gpiochip_remove_pin_ranges()
1955 &pin_range->range); in gpiochip_remove_pin_ranges()
1969 struct gpio_chip *gc = desc->gdev->chip; in gpiod_request_commit()
1977 return -ENOMEM; in gpiod_request_commit()
1983 * before IRQs are enabled, for non-sleeping (SOC) GPIOs. in gpiod_request_commit()
1986 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { in gpiod_request_commit()
1991 ret = -EBUSY; in gpiod_request_commit()
1995 if (gc->request) { in gpiod_request_commit()
1996 /* gc->request may sleep */ in gpiod_request_commit()
2000 ret = gc->request(gc, offset); in gpiod_request_commit()
2002 ret = -EINVAL; in gpiod_request_commit()
2008 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_request_commit()
2012 if (gc->get_direction) { in gpiod_request_commit()
2013 /* gc->get_direction may sleep */ in gpiod_request_commit()
2037 if (!desc->gdev) { in validate_desc()
2039 return -EINVAL; in validate_desc()
2041 if (!desc->gdev->chip) { in validate_desc()
2042 dev_warn(&desc->gdev->dev, in validate_desc()
2063 int ret = -EPROBE_DEFER; in gpiod_request()
2067 gdev = desc->gdev; in gpiod_request()
2069 if (try_module_get(gdev->owner)) { in gpiod_request()
2072 module_put(gdev->owner); in gpiod_request()
2074 get_device(&gdev->dev); in gpiod_request()
2095 gc = desc->gdev->chip; in gpiod_free_commit()
2096 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) { in gpiod_free_commit()
2097 if (gc->free) { in gpiod_free_commit()
2099 might_sleep_if(gc->can_sleep); in gpiod_free_commit()
2100 gc->free(gc, gpio_chip_hwgpio(desc)); in gpiod_free_commit()
2103 kfree_const(desc->label); in gpiod_free_commit()
2105 clear_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_free_commit()
2106 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_free_commit()
2107 clear_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_free_commit()
2108 clear_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_free_commit()
2109 clear_bit(FLAG_PULL_UP, &desc->flags); in gpiod_free_commit()
2110 clear_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_free_commit()
2111 clear_bit(FLAG_BIAS_DISABLE, &desc->flags); in gpiod_free_commit()
2112 clear_bit(FLAG_EDGE_RISING, &desc->flags); in gpiod_free_commit()
2113 clear_bit(FLAG_EDGE_FALLING, &desc->flags); in gpiod_free_commit()
2114 clear_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_free_commit()
2116 desc->hog = NULL; in gpiod_free_commit()
2119 WRITE_ONCE(desc->debounce_period_us, 0); in gpiod_free_commit()
2125 blocking_notifier_call_chain(&desc->gdev->notifier, in gpiod_free_commit()
2133 if (desc && desc->gdev && gpiod_free_commit(desc)) { in gpiod_free()
2134 module_put(desc->gdev->owner); in gpiod_free()
2135 put_device(&desc->gdev->dev); in gpiod_free()
2142 * gpiochip_is_requested - return string iff signal was requested
2144 * @offset: of signal within controller's 0..(ngpio - 1) range
2148 * passed it is a meaningless, non-NULL constant.
2158 if (offset >= gc->ngpio) in gpiochip_is_requested()
2165 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) in gpiochip_is_requested()
2167 return desc->label; in gpiochip_is_requested()
2172 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2186 * GPIO chip driver handles freeing the GPIOs it has requested).
2189 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2222 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2240 * using the open-drain emulation idiom) so these are natural places
2248 if (!gc->set_config) in gpio_do_set_config()
2249 return -ENOTSUPP; in gpio_do_set_config()
2251 return gc->set_config(gc, offset, config); in gpio_do_set_config()
2256 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_config()
2279 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) in gpio_set_bias()
2281 else if (test_bit(FLAG_PULL_UP, &desc->flags)) in gpio_set_bias()
2283 else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) in gpio_set_bias()
2288 if (ret != -ENOTSUPP) in gpio_set_bias()
2295 * gpiod_direction_input - set the GPIO direction to input
2309 gc = desc->gdev->chip; in gpiod_direction_input()
2313 * the chip is output-only, but you can't specify .direction_input() in gpiod_direction_input()
2316 if (!gc->get && gc->direction_input) { in gpiod_direction_input()
2320 return -EIO; in gpiod_direction_input()
2325 * just call it. Else we are some input-only chip so try to check the in gpiod_direction_input()
2329 if (gc->direction_input) { in gpiod_direction_input()
2330 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc)); in gpiod_direction_input()
2331 } else if (gc->get_direction && in gpiod_direction_input()
2332 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) { in gpiod_direction_input()
2336 return -EIO; in gpiod_direction_input()
2339 clear_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_input()
2351 struct gpio_chip *gc = desc->gdev->chip; in gpiod_direction_output_raw_commit()
2357 * output-only, but if there is then not even a .set() operation it in gpiod_direction_output_raw_commit()
2360 if (!gc->set && !gc->direction_output) { in gpiod_direction_output_raw_commit()
2364 return -EIO; in gpiod_direction_output_raw_commit()
2367 if (gc->direction_output) { in gpiod_direction_output_raw_commit()
2368 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); in gpiod_direction_output_raw_commit()
2371 if (gc->get_direction && in gpiod_direction_output_raw_commit()
2372 gc->get_direction(gc, gpio_chip_hwgpio(desc))) { in gpiod_direction_output_raw_commit()
2376 return -EIO; in gpiod_direction_output_raw_commit()
2380 * output-only chip, so just drive the output as desired. in gpiod_direction_output_raw_commit()
2382 gc->set(gc, gpio_chip_hwgpio(desc), val); in gpiod_direction_output_raw_commit()
2386 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output_raw_commit()
2393 * gpiod_direction_output_raw - set the GPIO direction to output
2411 * gpiod_direction_output - set the GPIO direction to output
2427 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_direction_output()
2432 /* GPIOs used for enabled IRQs shall not be set as output */ in gpiod_direction_output()
2433 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && in gpiod_direction_output()
2434 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { in gpiod_direction_output()
2436 "%s: tried to set a GPIO tied to an IRQ as output\n", in gpiod_direction_output()
2438 return -EIO; in gpiod_direction_output()
2441 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiod_direction_output()
2452 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { in gpiod_direction_output()
2473 * When emulating open-source or open-drain functionalities by not in gpiod_direction_output()
2479 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output()
2485 * gpiod_set_config - sets @config for a GPIO
2490 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2498 gc = desc->gdev->chip; in gpiod_set_config()
2505 * gpiod_set_debounce - sets @debounce time for a GPIO
2510 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2523 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2542 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); in gpiod_set_transitory()
2545 gc = desc->gdev->chip; in gpiod_set_transitory()
2546 if (!gc->set_config) in gpiod_set_transitory()
2553 if (rc == -ENOTSUPP) { in gpiod_set_transitory()
2554 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n", in gpiod_set_transitory()
2564 * gpiod_is_active_low - test whether a GPIO is active-low or not
2567 * Returns 1 if the GPIO is active-low, 0 otherwise.
2572 return test_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_is_active_low()
2577 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2583 change_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_toggle_active_low()
2597 *------------------------------------------------------------------------
2599 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2615 gc = desc->gdev->chip; in gpiod_get_raw_value_commit()
2617 value = gc->get ? gc->get(gc, offset) : -EIO; in gpiod_get_raw_value_commit()
2626 if (gc->get_multiple) { in gpio_chip_get_multiple()
2627 return gc->get_multiple(gc, mask, bits); in gpio_chip_get_multiple()
2628 } else if (gc->get) { in gpio_chip_get_multiple()
2631 for_each_set_bit(i, mask, gc->ngpio) { in gpio_chip_get_multiple()
2632 value = gc->get(gc, i); in gpio_chip_get_multiple()
2639 return -EIO; in gpio_chip_get_multiple()
2655 if (array_info && array_info->desc == desc_array && in gpiod_get_array_value_complex()
2656 array_size <= array_info->size && in gpiod_get_array_value_complex()
2657 (void *)array_info == desc_array + array_info->size) { in gpiod_get_array_value_complex()
2659 WARN_ON(array_info->chip->can_sleep); in gpiod_get_array_value_complex()
2661 ret = gpio_chip_get_multiple(array_info->chip, in gpiod_get_array_value_complex()
2662 array_info->get_mask, in gpiod_get_array_value_complex()
2667 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_get_array_value_complex()
2669 array_info->invert_mask, array_size); in gpiod_get_array_value_complex()
2671 i = find_first_zero_bit(array_info->get_mask, array_size); in gpiod_get_array_value_complex()
2679 struct gpio_chip *gc = desc_array[i]->gdev->chip; in gpiod_get_array_value_complex()
2684 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_get_array_value_complex()
2687 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio), in gpiod_get_array_value_complex()
2691 return -ENOMEM; in gpiod_get_array_value_complex()
2694 bits = mask + BITS_TO_LONGS(gc->ngpio); in gpiod_get_array_value_complex()
2695 bitmap_zero(mask, gc->ngpio); in gpiod_get_array_value_complex()
2698 WARN_ON(gc->can_sleep); in gpiod_get_array_value_complex()
2710 i = find_next_zero_bit(array_info->get_mask, in gpiod_get_array_value_complex()
2713 (desc_array[i]->gdev->chip == gc)); in gpiod_get_array_value_complex()
2727 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_array_value_complex()
2734 j = find_next_zero_bit(array_info->get_mask, i, in gpiod_get_array_value_complex()
2745 * gpiod_get_raw_value() - return a gpio's raw value
2758 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_raw_value()
2764 * gpiod_get_value() - return a gpio's value
2779 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_value()
2785 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value()
2793 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2799 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2812 return -EINVAL; in gpiod_get_raw_array_value()
2820 * gpiod_get_array_value() - read values from an array of GPIOs
2826 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2838 return -EINVAL; in gpiod_get_array_value()
2846 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2848 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2853 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_open_drain_value_commit()
2857 ret = gc->direction_input(gc, offset); in gpio_set_open_drain_value_commit()
2859 ret = gc->direction_output(gc, offset, 0); in gpio_set_open_drain_value_commit()
2861 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_drain_value_commit()
2871 * _gpio_set_open_source_value() - Set the open source gpio's value.
2873 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2878 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_open_source_value_commit()
2882 ret = gc->direction_output(gc, offset, 1); in gpio_set_open_source_value_commit()
2884 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_source_value_commit()
2886 ret = gc->direction_input(gc, offset); in gpio_set_open_source_value_commit()
2899 gc = desc->gdev->chip; in gpiod_set_raw_value_commit()
2901 gc->set(gc, gpio_chip_hwgpio(desc), value); in gpiod_set_raw_value_commit()
2917 if (gc->set_multiple) { in gpio_chip_set_multiple()
2918 gc->set_multiple(gc, mask, bits); in gpio_chip_set_multiple()
2923 for_each_set_bit(i, mask, gc->ngpio) in gpio_chip_set_multiple()
2924 gc->set(gc, i, test_bit(i, bits)); in gpio_chip_set_multiple()
2941 if (array_info && array_info->desc == desc_array && in gpiod_set_array_value_complex()
2942 array_size <= array_info->size && in gpiod_set_array_value_complex()
2943 (void *)array_info == desc_array + array_info->size) { in gpiod_set_array_value_complex()
2945 WARN_ON(array_info->chip->can_sleep); in gpiod_set_array_value_complex()
2947 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_set_array_value_complex()
2949 array_info->invert_mask, array_size); in gpiod_set_array_value_complex()
2951 gpio_chip_set_multiple(array_info->chip, array_info->set_mask, in gpiod_set_array_value_complex()
2954 i = find_first_zero_bit(array_info->set_mask, array_size); in gpiod_set_array_value_complex()
2962 struct gpio_chip *gc = desc_array[i]->gdev->chip; in gpiod_set_array_value_complex()
2967 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_set_array_value_complex()
2970 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio), in gpiod_set_array_value_complex()
2974 return -ENOMEM; in gpiod_set_array_value_complex()
2977 bits = mask + BITS_TO_LONGS(gc->ngpio); in gpiod_set_array_value_complex()
2978 bitmap_zero(mask, gc->ngpio); in gpiod_set_array_value_complex()
2981 WARN_ON(gc->can_sleep); in gpiod_set_array_value_complex()
2994 test_bit(i, array_info->invert_mask)) && in gpiod_set_array_value_complex()
2995 test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_array_value_complex()
3002 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3004 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3014 i = find_next_zero_bit(array_info->set_mask, in gpiod_set_array_value_complex()
3017 (desc_array[i]->gdev->chip == gc)); in gpiod_set_array_value_complex()
3029 * gpiod_set_raw_value() - assign a gpio's raw value
3043 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_set_raw_value()
3049 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3059 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_value_nocheck()
3061 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) in gpiod_set_value_nocheck()
3063 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) in gpiod_set_value_nocheck()
3070 * gpiod_set_value() - assign a gpio's value
3084 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_set_value()
3090 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3096 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3108 return -EINVAL; in gpiod_set_raw_array_value()
3115 * gpiod_set_array_value() - assign values to an array of GPIOs
3121 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3133 return -EINVAL; in gpiod_set_array_value()
3141 * gpiod_cansleep() - report whether gpio value access may sleep
3148 return desc->gdev->chip->can_sleep; in gpiod_cansleep()
3153 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3163 return -ENOMEM; in gpiod_set_consumer_name()
3166 kfree_const(desc->label); in gpiod_set_consumer_name()
3174 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3175 * @desc: gpio whose IRQ will be returned (already requested)
3177 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3190 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) in gpiod_to_irq()
3191 return -EINVAL; in gpiod_to_irq()
3193 gc = desc->gdev->chip; in gpiod_to_irq()
3195 if (gc->to_irq) { in gpiod_to_irq()
3196 int retirq = gc->to_irq(gc, offset); in gpiod_to_irq()
3200 return -ENXIO; in gpiod_to_irq()
3204 return -ENXIO; in gpiod_to_irq()
3209 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3211 * @offset: the offset of the GPIO to lock as IRQ
3228 if (!gc->can_sleep && gc->get_direction) { in gpiochip_lock_as_irq()
3238 /* To be valid for IRQ the line needs to be input or open drain */ in gpiochip_lock_as_irq()
3239 if (test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_lock_as_irq()
3240 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiochip_lock_as_irq()
3242 "%s: tried to flag a GPIO set as output for IRQ\n", in gpiochip_lock_as_irq()
3244 return -EIO; in gpiochip_lock_as_irq()
3247 set_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_lock_as_irq()
3248 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_lock_as_irq()
3252 * IRQ is referenced from .to_irq()) we set up a label here in gpiochip_lock_as_irq()
3255 if (!desc->label) in gpiochip_lock_as_irq()
3263 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3265 * @offset: the offset of the GPIO to lock as IRQ
3268 * that a certain GPIO is no longer used exclusively for IRQ.
3278 clear_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_unlock_as_irq()
3279 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_unlock_as_irq()
3282 if (desc->label && !strcmp(desc->label, "interrupt")) in gpiochip_unlock_as_irq()
3292 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) in gpiochip_disable_irq()
3293 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_disable_irq()
3302 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { in gpiochip_enable_irq()
3304 * We must not be output when using IRQ UNLESS we are in gpiochip_enable_irq()
3307 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_enable_irq()
3308 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); in gpiochip_enable_irq()
3309 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_enable_irq()
3316 if (offset >= gc->ngpio) in gpiochip_line_is_irq()
3319 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_irq()
3327 if (!try_module_get(gc->gpiodev->owner)) in gpiochip_reqres_irq()
3328 return -ENODEV; in gpiochip_reqres_irq()
3332 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset); in gpiochip_reqres_irq()
3333 module_put(gc->gpiodev->owner); in gpiochip_reqres_irq()
3343 module_put(gc->gpiodev->owner); in gpiochip_relres_irq()
3349 if (offset >= gc->ngpio) in gpiochip_line_is_open_drain()
3352 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_drain()
3358 if (offset >= gc->ngpio) in gpiochip_line_is_open_source()
3361 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_source()
3367 if (offset >= gc->ngpio) in gpiochip_line_is_persistent()
3370 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_persistent()
3375 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3392 * gpiod_get_value_cansleep() - return a gpio's value
3410 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value_cansleep()
3418 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3424 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3437 return -EINVAL; in gpiod_get_raw_array_value_cansleep()
3445 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3451 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3463 return -EINVAL; in gpiod_get_array_value_cansleep()
3471 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3489 * gpiod_set_value_cansleep() - assign a gpio's value
3507 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3513 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3525 return -EINVAL; in gpiod_set_raw_array_value_cansleep()
3532 * gpiod_add_lookup_tables() - register GPIO device consumers
3543 list_add_tail(&tables[i]->list, &gpio_lookup_list); in gpiod_add_lookup_tables()
3549 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3555 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3567 return -EINVAL; in gpiod_set_array_value_cansleep()
3575 * gpiod_add_lookup_table() - register GPIO device consumers
3582 list_add_tail(&table->list, &gpio_lookup_list); in gpiod_add_lookup_table()
3589 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3596 list_del(&table->list); in gpiod_remove_lookup_table()
3603 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3613 for (hog = &hogs[0]; hog->chip_label; hog++) { in gpiod_add_hogs()
3614 list_add_tail(&hog->list, &gpio_machine_hogs); in gpiod_add_hogs()
3620 gc = find_chip_by_name(hog->chip_label); in gpiod_add_hogs()
3637 if (table->dev_id && dev_id) { in gpiod_find_lookup_table()
3642 if (!strcmp(table->dev_id, dev_id)) in gpiod_find_lookup_table()
3649 if (dev_id == table->dev_id) in gpiod_find_lookup_table()
3663 struct gpio_desc *desc = ERR_PTR(-ENOENT); in gpiod_find()
3671 for (p = &table->table[0]; p->key; p++) { in gpiod_find()
3675 if (p->idx != idx) in gpiod_find()
3679 if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) in gpiod_find()
3682 if (p->chip_hwnum == U16_MAX) { in gpiod_find()
3683 desc = gpio_name_to_desc(p->key); in gpiod_find()
3685 *flags = p->flags; in gpiod_find()
3690 p->key); in gpiod_find()
3691 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
3694 gc = find_chip_by_name(p->key); in gpiod_find()
3699 * p->key should exist, assume it may in gpiod_find()
3705 p->key); in gpiod_find()
3706 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
3709 if (gc->ngpio <= p->chip_hwnum) { in gpiod_find()
3712 idx, p->chip_hwnum, gc->ngpio - 1, in gpiod_find()
3713 gc->label); in gpiod_find()
3714 return ERR_PTR(-EINVAL); in gpiod_find()
3717 desc = gpiochip_get_desc(gc, p->chip_hwnum); in gpiod_find()
3718 *flags = p->flags; in gpiod_find()
3734 return -ENOENT; in platform_gpio_count()
3736 for (p = &table->table[0]; p->key; p++) { in platform_gpio_count()
3737 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || in platform_gpio_count()
3738 (!con_id && !p->con_id)) in platform_gpio_count()
3742 return -ENOENT; in platform_gpio_count()
3748 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3779 snprintf(prop_name, sizeof(prop_name), "%s-%s", in fwnode_gpiod_get_index()
3787 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) in fwnode_gpiod_get_index()
3796 * gpiod_count - return the number of GPIOs associated with a device / function
3797 * or -ENOENT if no GPIO has been assigned to the requested function
3798 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3803 int count = -ENOENT; in gpiod_count()
3805 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) in gpiod_count()
3818 * gpiod_get - obtain a GPIO for a given GPIO function
3819 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3824 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3835 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3836 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3842 * that need to handle optional GPIOs.
3854 * gpiod_configure_flags - helper function to configure a given GPIO
3857 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3859 * @dflags: gpiod_flags - optional GPIO initialization flags
3861 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3871 set_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_configure_flags()
3874 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
3882 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
3888 set_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_configure_flags()
3892 "both pull-up and pull-down enabled, invalid configuration\n"); in gpiod_configure_flags()
3893 return -EINVAL; in gpiod_configure_flags()
3897 set_bit(FLAG_PULL_UP, &desc->flags); in gpiod_configure_flags()
3899 set_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_configure_flags()
3922 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3923 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3928 * This variant of gpiod_get() allows to access GPIOs other than the first
3929 * defined one for functions that define several GPIOs.
3931 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3950 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { in gpiod_get_index()
3963 if (!desc || desc == ERR_PTR(-ENOENT)) { in gpiod_get_index()
3979 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { in gpiod_get_index()
4003 blocking_notifier_call_chain(&desc->gdev->notifier, in gpiod_get_index()
4011 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4037 struct gpio_desc *desc = ERR_PTR(-ENODEV); in fwnode_get_named_gpiod()
4041 return ERR_PTR(-EINVAL); in fwnode_get_named_gpiod()
4071 blocking_notifier_call_chain(&desc->gdev->notifier, in fwnode_get_named_gpiod()
4079 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4081 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4088 * This is convenient for drivers that need to handle optional GPIOs.
4099 if (PTR_ERR(desc) == -ENOENT) in gpiod_get_index_optional()
4108 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4111 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4113 * @dflags: gpiod_flags - optional GPIO initialization flags
4131 name, gc->label, hwnum, ret); in gpiod_hog()
4136 set_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_hog()
4147 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4154 for (id = 0; id < gc->ngpio; id++) { in gpiochip_free_hogs()
4155 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags)) in gpiochip_free_hogs()
4156 gpiochip_free_own_desc(&gc->gpiodev->descs[id]); in gpiochip_free_hogs()
4161 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4162 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4166 * This function acquires all the GPIOs defined under a given function.
4168 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4170 * code if an error occurred while trying to acquire the GPIOs.
4188 return ERR_PTR(-ENOMEM); in gpiod_get_array()
4190 for (descs->ndescs = 0; descs->ndescs < count; ) { in gpiod_get_array()
4191 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); in gpiod_get_array()
4197 descs->desc[descs->ndescs] = desc; in gpiod_get_array()
4204 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { in gpiod_get_array()
4207 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ? in gpiod_get_array()
4208 gc->ngpio : count); in gpiod_get_array()
4215 return ERR_PTR(-ENOMEM); in gpiod_get_array()
4219 struct_size(descs, desc, descs->ndescs + 1)); in gpiod_get_array()
4223 array_info = (void *)(descs->desc + count); in gpiod_get_array()
4224 array_info->get_mask = array_info->invert_mask + in gpiod_get_array()
4226 array_info->set_mask = array_info->get_mask + in gpiod_get_array()
4229 array_info->desc = descs->desc; in gpiod_get_array()
4230 array_info->size = count; in gpiod_get_array()
4231 array_info->chip = gc; in gpiod_get_array()
4232 bitmap_set(array_info->get_mask, descs->ndescs, in gpiod_get_array()
4233 count - descs->ndescs); in gpiod_get_array()
4234 bitmap_set(array_info->set_mask, descs->ndescs, in gpiod_get_array()
4235 count - descs->ndescs); in gpiod_get_array()
4236 descs->info = array_info; in gpiod_get_array()
4239 if (array_info && array_info->chip != gc) { in gpiod_get_array()
4240 __clear_bit(descs->ndescs, array_info->get_mask); in gpiod_get_array()
4241 __clear_bit(descs->ndescs, array_info->set_mask); in gpiod_get_array()
4248 gpio_chip_hwgpio(desc) != descs->ndescs) { in gpiod_get_array()
4254 if (bitmap_full(array_info->get_mask, descs->ndescs)) { in gpiod_get_array()
4257 __clear_bit(descs->ndescs, in gpiod_get_array()
4258 array_info->get_mask); in gpiod_get_array()
4259 __clear_bit(descs->ndescs, in gpiod_get_array()
4260 array_info->set_mask); in gpiod_get_array()
4264 if (gpiochip_line_is_open_drain(gc, descs->ndescs) || in gpiod_get_array()
4265 gpiochip_line_is_open_source(gc, descs->ndescs)) in gpiod_get_array()
4266 __clear_bit(descs->ndescs, in gpiod_get_array()
4267 array_info->set_mask); in gpiod_get_array()
4270 __set_bit(descs->ndescs, in gpiod_get_array()
4271 array_info->invert_mask); in gpiod_get_array()
4274 descs->ndescs++; in gpiod_get_array()
4279 array_info->chip->label, array_info->size, in gpiod_get_array()
4280 *array_info->get_mask, *array_info->set_mask, in gpiod_get_array()
4281 *array_info->invert_mask); in gpiod_get_array()
4287 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4289 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4303 if (PTR_ERR(descs) == -ENOENT) in gpiod_get_array_optional()
4311 * gpiod_put - dispose of a GPIO descriptor
4324 * gpiod_put_array - dispose of multiple GPIO descriptors
4331 for (i = 0; i < descs->ndescs; i++) in gpiod_put_array()
4332 gpiod_put(descs->desc[i]); in gpiod_put_array()
4372 struct gpio_chip *gc = gdev->chip; in gpiolib_dbg_show()
4373 unsigned gpio = gdev->base; in gpiolib_dbg_show()
4374 struct gpio_desc *gdesc = &gdev->descs[0]; in gpiolib_dbg_show()
4379 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) { in gpiolib_dbg_show()
4380 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { in gpiolib_dbg_show()
4381 if (gdesc->name) { in gpiolib_dbg_show()
4382 seq_printf(s, " gpio-%-3d (%-20.20s)\n", in gpiolib_dbg_show()
4383 gpio, gdesc->name); in gpiolib_dbg_show()
4389 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); in gpiolib_dbg_show()
4390 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); in gpiolib_dbg_show()
4391 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags); in gpiolib_dbg_show()
4392 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s", in gpiolib_dbg_show()
4393 gpio, gdesc->name ? gdesc->name : "", gdesc->label, in gpiolib_dbg_show()
4395 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ", in gpiolib_dbg_show()
4396 is_irq ? "IRQ " : "", in gpiolib_dbg_show()
4408 s->private = ""; in gpiolib_seq_start()
4412 if (index-- == 0) { in gpiolib_seq_start()
4428 if (list_is_last(&gdev->list, &gpio_devices)) in gpiolib_seq_next()
4431 ret = list_entry(gdev->list.next, struct gpio_device, list); in gpiolib_seq_next()
4434 s->private = "\n"; in gpiolib_seq_next()
4447 struct gpio_chip *gc = gdev->chip; in gpiolib_seq_show()
4451 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, in gpiolib_seq_show()
4452 dev_name(&gdev->dev)); in gpiolib_seq_show()
4456 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, in gpiolib_seq_show()
4457 dev_name(&gdev->dev), in gpiolib_seq_show()
4458 gdev->base, gdev->base + gdev->ngpio - 1); in gpiolib_seq_show()
4459 parent = gc->parent; in gpiolib_seq_show()
4462 parent->bus ? parent->bus->name : "no-bus", in gpiolib_seq_show()
4464 if (gc->label) in gpiolib_seq_show()
4465 seq_printf(s, ", %s", gc->label); in gpiolib_seq_show()
4466 if (gc->can_sleep) in gpiolib_seq_show()
4470 if (gc->dbg_show) in gpiolib_seq_show()
4471 gc->dbg_show(s, gc); in gpiolib_seq_show()