Lines Matching +full:drv +full:- +full:pinconf

1 // SPDX-License-Identifier: GPL-2.0
31 #include "gpiolib-acpi.h"
32 #include "gpiolib-cdev.h"
33 #include "gpiolib-of.h"
34 #include "gpiolib-swnode.h"
35 #include "gpiolib-sysfs.h"
43 * The GPIO programming interface allows for inlining speed-critical
44 * get/set operations for common cases, so that access to SOC-integrated
61 /* Device and char device-related information */
66 static int gpio_bus_match(struct device *dev, struct device_driver *drv) in gpio_bus_match() argument
74 if (fwnode && fwnode->dev != dev) in gpio_bus_match()
115 d->label = label; in desc_set_label()
119 * gpio_to_desc - Convert a GPIO number to its descriptor
134 if (gdev->base <= gpio && in gpio_to_desc()
135 gdev->base + gdev->ngpio > gpio) { in gpio_to_desc()
137 return &gdev->descs[gpio - gdev->base]; in gpio_to_desc()
151 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
157 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
163 struct gpio_device *gdev = gc->gpiodev; in gpiochip_get_desc()
165 if (hwnum >= gdev->ngpio) in gpiochip_get_desc()
166 return ERR_PTR(-EINVAL); in gpiochip_get_desc()
168 return &gdev->descs[array_index_nospec(hwnum, gdev->ngpio)]; in gpiochip_get_desc()
173 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
184 return desc->gdev->base + (desc - &desc->gdev->descs[0]); in desc_to_gpio()
190 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
195 if (!desc || !desc->gdev) in gpiod_to_chip()
197 return desc->gdev->chip; in gpiod_to_chip()
209 if (gdev->base >= base + ngpio) in gpiochip_find_base()
212 base = gdev->base + gdev->ngpio; in gpiochip_find_base()
222 return -ENOSPC; in gpiochip_find_base()
227 * gpiod_get_direction - return the current direction of a GPIO
247 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && in gpiod_get_direction()
248 test_bit(FLAG_IS_OUT, &desc->flags)) in gpiod_get_direction()
251 if (!gc->get_direction) in gpiod_get_direction()
252 return -ENOTSUPP; in gpiod_get_direction()
254 ret = gc->get_direction(gc, offset); in gpiod_get_direction()
262 assign_bit(FLAG_IS_OUT, &desc->flags, !ret); in gpiod_get_direction()
270 * by range(means [base, base + ngpio - 1]) order.
272 * Return -EBUSY if the new chip overlaps with some other chip's integer
281 list_add_tail(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
286 if (gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list()
288 list_add(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
293 if (prev->base + prev->ngpio <= gdev->base) { in gpiodev_add_to_list()
295 list_add_tail(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
301 if (&next->list == &gpio_devices) in gpiodev_add_to_list()
305 if (prev->base + prev->ngpio <= gdev->base in gpiodev_add_to_list()
306 && gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list()
307 list_add(&gdev->list, &prev->list); in gpiodev_add_to_list()
312 return -EBUSY; in gpiodev_add_to_list()
334 for_each_gpio_desc(gdev->chip, desc) { in gpio_name_to_desc()
335 if (desc->name && !strcmp(desc->name, name)) { in gpio_name_to_desc()
348 * Take the names from gc->names and assign them to their GPIO descriptors.
352 * 1. Non-unique names are still accepted,
357 struct gpio_device *gdev = gc->gpiodev; in gpiochip_set_desc_names()
361 for (i = 0; i != gc->ngpio; ++i) { in gpiochip_set_desc_names()
364 gpio = gpio_name_to_desc(gc->names[i]); in gpiochip_set_desc_names()
366 dev_warn(&gdev->dev, in gpiochip_set_desc_names()
368 gc->names[i]); in gpiochip_set_desc_names()
372 for (i = 0; i != gc->ngpio; ++i) in gpiochip_set_desc_names()
373 gdev->descs[i].name = gc->names[i]; in gpiochip_set_desc_names()
379 * gpiochip_set_names - Set GPIO line names using device properties
382 * Looks for device property "gpio-line-names" and if it exists assigns
389 struct gpio_device *gdev = chip->gpiodev; in gpiochip_set_names()
390 struct device *dev = &gdev->dev; in gpiochip_set_names()
395 count = device_property_string_array_count(dev, "gpio-line-names"); in gpiochip_set_names()
402 * setting friendly names if the specified ones with 'gpio-line-names' in gpiochip_set_names()
407 if (count <= chip->offset) { in gpiochip_set_names()
408 …dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset … in gpiochip_set_names()
409 count, chip->offset); in gpiochip_set_names()
415 return -ENOMEM; in gpiochip_set_names()
417 ret = device_property_read_string_array(dev, "gpio-line-names", in gpiochip_set_names()
427 * contain at most number gpiochips x chip->ngpio. We have to in gpiochip_set_names()
429 * chip->offset as starting point from where we will assign in gpiochip_set_names()
431 * 'gpio-line-names' cannot contains gaps, we have to be sure in gpiochip_set_names()
432 * we only assign those pins that really exists since chip->ngpio in gpiochip_set_names()
433 * can be different of the chip->offset. in gpiochip_set_names()
435 count = (count > chip->offset) ? count - chip->offset : count; in gpiochip_set_names()
436 if (count > chip->ngpio) in gpiochip_set_names()
437 count = chip->ngpio; in gpiochip_set_names()
446 if (names[chip->offset + i] && names[chip->offset + i][0]) in gpiochip_set_names()
447 gdev->descs[i].name = names[chip->offset + i]; in gpiochip_set_names()
459 p = bitmap_alloc(gc->ngpio, GFP_KERNEL); in gpiochip_allocate_mask()
464 bitmap_fill(p, gc->ngpio); in gpiochip_allocate_mask()
477 struct device *dev = &gc->gpiodev->dev; in gpiochip_count_reserved_ranges()
481 size = device_property_count_u32(dev, "gpio-reserved-ranges"); in gpiochip_count_reserved_ranges()
490 struct device *dev = &gc->gpiodev->dev; in gpiochip_apply_reserved_ranges()
501 return -ENOMEM; in gpiochip_apply_reserved_ranges()
503 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges", in gpiochip_apply_reserved_ranges()
511 u32 count = ranges[--size]; in gpiochip_apply_reserved_ranges()
512 u32 start = ranges[--size]; in gpiochip_apply_reserved_ranges()
514 if (start >= gc->ngpio || start + count > gc->ngpio) in gpiochip_apply_reserved_ranges()
517 bitmap_clear(gc->valid_mask, start, count); in gpiochip_apply_reserved_ranges()
528 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask)) in gpiochip_init_valid_mask()
531 gc->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_init_valid_mask()
532 if (!gc->valid_mask) in gpiochip_init_valid_mask()
533 return -ENOMEM; in gpiochip_init_valid_mask()
539 if (gc->init_valid_mask) in gpiochip_init_valid_mask()
540 return gc->init_valid_mask(gc, in gpiochip_init_valid_mask()
541 gc->valid_mask, in gpiochip_init_valid_mask()
542 gc->ngpio); in gpiochip_init_valid_mask()
549 gpiochip_free_mask(&gc->valid_mask); in gpiochip_free_valid_mask()
555 * Device Tree platforms are supposed to use "gpio-ranges" in gpiochip_add_pin_ranges()
556 * property. This check ensures that the ->add_pin_ranges() in gpiochip_add_pin_ranges()
559 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges")) in gpiochip_add_pin_ranges()
562 if (gc->add_pin_ranges) in gpiochip_add_pin_ranges()
563 return gc->add_pin_ranges(gc); in gpiochip_add_pin_ranges()
572 if (likely(!gc->valid_mask)) in gpiochip_line_is_valid()
574 return test_bit(offset, gc->valid_mask); in gpiochip_line_is_valid()
584 list_del(&gdev->list); in gpiodev_release()
587 ida_free(&gpio_ida, gdev->id); in gpiodev_release()
588 kfree_const(gdev->label); in gpiodev_release()
589 kfree(gdev->descs); in gpiodev_release()
601 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
602 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
607 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev); in gpiochip_setup_dev()
614 if (fwnode && !fwnode->dev) in gpiochip_setup_dev()
622 gdev->dev.release = gpiodev_release; in gpiochip_setup_dev()
628 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base, in gpiochip_setup_dev()
629 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic"); in gpiochip_setup_dev()
643 desc = gpiochip_get_desc(gc, hog->chip_hwnum); in gpiochip_machine_hog()
650 if (test_bit(FLAG_IS_HOGGED, &desc->flags)) in gpiochip_machine_hog()
653 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); in gpiochip_machine_hog()
656 __func__, gc->label, hog->chip_hwnum, rv); in gpiochip_machine_hog()
666 if (!strcmp(gc->label, hog->chip_label)) in machine_gpiochip_add()
681 dev_err(&gdev->dev, in gpiochip_setup_devs()
688 gc->gpiodev->data = data; in gpiochip_set_data()
692 * gpiochip_get_data() - get per-subdriver data for the chip
696 * The per-subdriver data for the chip.
700 return gc->gpiodev->data; in gpiochip_get_data()
706 u32 ngpios = gc->ngpio; in gpiochip_get_ngpios()
711 if (ret == -ENODATA) in gpiochip_get_ngpios()
713 * -ENODATA means that there is no property found and in gpiochip_get_ngpios()
722 gc->ngpio = ngpios; in gpiochip_get_ngpios()
725 if (gc->ngpio == 0) { in gpiochip_get_ngpios()
727 return -EINVAL; in gpiochip_get_ngpios()
730 if (gc->ngpio > FASTPATH_NGPIO) in gpiochip_get_ngpios()
732 gc->ngpio, FASTPATH_NGPIO); in gpiochip_get_ngpios()
754 return -ENOMEM; in gpiochip_add_data_with_key()
755 gdev->dev.bus = &gpio_bus_type; in gpiochip_add_data_with_key()
756 gdev->dev.parent = gc->parent; in gpiochip_add_data_with_key()
757 gdev->chip = gc; in gpiochip_add_data_with_key()
759 gc->gpiodev = gdev; in gpiochip_add_data_with_key()
766 if (gc->fwnode) in gpiochip_add_data_with_key()
767 device_set_node(&gdev->dev, gc->fwnode); in gpiochip_add_data_with_key()
768 else if (gc->parent) in gpiochip_add_data_with_key()
769 device_set_node(&gdev->dev, dev_fwnode(gc->parent)); in gpiochip_add_data_with_key()
771 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL); in gpiochip_add_data_with_key()
772 if (gdev->id < 0) { in gpiochip_add_data_with_key()
773 ret = gdev->id; in gpiochip_add_data_with_key()
777 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); in gpiochip_add_data_with_key()
781 device_initialize(&gdev->dev); in gpiochip_add_data_with_key()
782 if (gc->parent && gc->parent->driver) in gpiochip_add_data_with_key()
783 gdev->owner = gc->parent->driver->owner; in gpiochip_add_data_with_key()
784 else if (gc->owner) in gpiochip_add_data_with_key()
785 /* TODO: remove chip->owner */ in gpiochip_add_data_with_key()
786 gdev->owner = gc->owner; in gpiochip_add_data_with_key()
788 gdev->owner = THIS_MODULE; in gpiochip_add_data_with_key()
790 ret = gpiochip_get_ngpios(gc, &gdev->dev); in gpiochip_add_data_with_key()
794 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL); in gpiochip_add_data_with_key()
795 if (!gdev->descs) { in gpiochip_add_data_with_key()
796 ret = -ENOMEM; in gpiochip_add_data_with_key()
800 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL); in gpiochip_add_data_with_key()
801 if (!gdev->label) { in gpiochip_add_data_with_key()
802 ret = -ENOMEM; in gpiochip_add_data_with_key()
806 gdev->ngpio = gc->ngpio; in gpiochip_add_data_with_key()
817 base = gc->base; in gpiochip_add_data_with_key()
819 base = gpiochip_find_base(gc->ngpio); in gpiochip_add_data_with_key()
832 gc->base = base; in gpiochip_add_data_with_key()
834 dev_warn(&gdev->dev, in gpiochip_add_data_with_key()
837 gdev->base = base; in gpiochip_add_data_with_key()
846 for (i = 0; i < gc->ngpio; i++) in gpiochip_add_data_with_key()
847 gdev->descs[i].gdev = gdev; in gpiochip_add_data_with_key()
851 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); in gpiochip_add_data_with_key()
852 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); in gpiochip_add_data_with_key()
853 init_rwsem(&gdev->sem); in gpiochip_add_data_with_key()
856 INIT_LIST_HEAD(&gdev->pin_ranges); in gpiochip_add_data_with_key()
859 if (gc->names) { in gpiochip_add_data_with_key()
876 for (i = 0; i < gc->ngpio; i++) { in gpiochip_add_data_with_key()
877 struct gpio_desc *desc = &gdev->descs[i]; in gpiochip_add_data_with_key()
879 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { in gpiochip_add_data_with_key()
881 &desc->flags, !gc->get_direction(gc, i)); in gpiochip_add_data_with_key()
884 &desc->flags, !gc->direction_input); in gpiochip_add_data_with_key()
935 if (gdev->dev.release) { in gpiochip_add_data_with_key()
942 list_del(&gdev->list); in gpiochip_add_data_with_key()
945 kfree_const(gdev->label); in gpiochip_add_data_with_key()
947 kfree(gdev->descs); in gpiochip_add_data_with_key()
949 kfree(dev_name(&gdev->dev)); in gpiochip_add_data_with_key()
951 ida_free(&gpio_ida, gdev->id); in gpiochip_add_data_with_key()
956 if (ret != -EPROBE_DEFER) { in gpiochip_add_data_with_key()
958 base, base + (int)gc->ngpio - 1, in gpiochip_add_data_with_key()
959 gc->label ? : "generic", ret); in gpiochip_add_data_with_key()
966 * gpiochip_remove() - unregister a gpio_chip
973 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove()
977 down_write(&gdev->sem); in gpiochip_remove()
983 gdev->chip = NULL; in gpiochip_remove()
996 for (i = 0; i < gdev->ngpio; i++) { in gpiochip_remove()
1002 if (i != gdev->ngpio) in gpiochip_remove()
1003 dev_crit(&gdev->dev, in gpiochip_remove()
1013 up_write(&gdev->sem); in gpiochip_remove()
1032 gc = gdev->chip; in gpiochip_find()
1041 * gpio_device_find() - find a specific GPIO device
1050 * 0 if the device doesn't match and non-zero if it does. If the callback
1051 * returns non-zero, this function will return to the caller and not iterate
1058 * If the function returns non-NULL, the returned reference must be freed by
1077 if (gdev->chip && match(gdev->chip, data)) in gpio_device_find()
1089 return !strcmp(gc->label, name); in gpiochip_match_name()
1098 * gpio_device_get() - Increase the reference count of this GPIO device
1106 return to_gpio_device(get_device(&gdev->dev)); in gpio_device_get()
1111 * gpio_device_put() - Decrease the reference count of this GPIO device and
1117 put_device(&gdev->dev); in gpio_device_put()
1129 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_hw()
1131 if (!girq->init_hw) in gpiochip_irqchip_init_hw()
1134 return girq->init_hw(gc); in gpiochip_irqchip_init_hw()
1139 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_valid_mask()
1141 if (!girq->init_valid_mask) in gpiochip_irqchip_init_valid_mask()
1144 girq->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_irqchip_init_valid_mask()
1145 if (!girq->valid_mask) in gpiochip_irqchip_init_valid_mask()
1146 return -ENOMEM; in gpiochip_irqchip_init_valid_mask()
1148 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); in gpiochip_irqchip_init_valid_mask()
1155 gpiochip_free_mask(&gc->irq.valid_mask); in gpiochip_irqchip_free_valid_mask()
1164 if (likely(!gc->irq.valid_mask)) in gpiochip_irqchip_irq_valid()
1166 return test_bit(offset, gc->irq.valid_mask); in gpiochip_irqchip_irq_valid()
1173 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1183 if (is_of_node(gc->irq.fwnode)) in gpiochip_set_hierarchical_irqchip()
1194 if (is_fwnode_irqchip(gc->irq.fwnode)) { in gpiochip_set_hierarchical_irqchip()
1198 for (i = 0; i < gc->ngpio; i++) { in gpiochip_set_hierarchical_irqchip()
1202 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_set_hierarchical_irqchip()
1210 ret = girq->child_to_parent_hwirq(gc, i, in gpiochip_set_hierarchical_irqchip()
1215 chip_err(gc, "skip set-up on hwirq %d\n", in gpiochip_set_hierarchical_irqchip()
1220 fwspec.fwnode = gc->irq.fwnode; in gpiochip_set_hierarchical_irqchip()
1222 fwspec.param[0] = girq->child_offset_to_irq(gc, i); in gpiochip_set_hierarchical_irqchip()
1226 ret = irq_domain_alloc_irqs(gc->irq.domain, 1, in gpiochip_set_hierarchical_irqchip()
1248 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { in gpiochip_hierarchy_irq_domain_translate()
1253 if (is_fwnode_irqchip(fwspec->fwnode)) { in gpiochip_hierarchy_irq_domain_translate()
1262 return -EINVAL; in gpiochip_hierarchy_irq_domain_translate()
1270 struct gpio_chip *gc = d->host_data; in gpiochip_hierarchy_irq_domain_alloc()
1277 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_hierarchy_irq_domain_alloc()
1281 * The nr_irqs parameter is always one except for PCI multi-MSI in gpiochip_hierarchy_irq_domain_alloc()
1286 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); in gpiochip_hierarchy_irq_domain_alloc()
1292 ret = girq->child_to_parent_hwirq(gc, hwirq, type, in gpiochip_hierarchy_irq_domain_alloc()
1307 gc->irq.chip, in gpiochip_hierarchy_irq_domain_alloc()
1309 girq->handler, in gpiochip_hierarchy_irq_domain_alloc()
1314 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec, in gpiochip_hierarchy_irq_domain_alloc()
1321 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_hierarchy_irq_domain_alloc()
1327 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST)) in gpiochip_hierarchy_irq_domain_alloc()
1345 ops->activate = gpiochip_irq_domain_activate; in gpiochip_hierarchy_setup_domain_ops()
1346 ops->deactivate = gpiochip_irq_domain_deactivate; in gpiochip_hierarchy_setup_domain_ops()
1347 ops->alloc = gpiochip_hierarchy_irq_domain_alloc; in gpiochip_hierarchy_setup_domain_ops()
1356 if (!ops->translate) in gpiochip_hierarchy_setup_domain_ops()
1357 ops->translate = gpiochip_hierarchy_irq_domain_translate; in gpiochip_hierarchy_setup_domain_ops()
1358 if (!ops->free) in gpiochip_hierarchy_setup_domain_ops()
1359 ops->free = irq_domain_free_irqs_common; in gpiochip_hierarchy_setup_domain_ops()
1366 if (!gc->irq.child_to_parent_hwirq || in gpiochip_hierarchy_create_domain()
1367 !gc->irq.fwnode) { in gpiochip_hierarchy_create_domain()
1369 return ERR_PTR(-EINVAL); in gpiochip_hierarchy_create_domain()
1372 if (!gc->irq.child_offset_to_irq) in gpiochip_hierarchy_create_domain()
1373 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; in gpiochip_hierarchy_create_domain()
1375 if (!gc->irq.populate_parent_alloc_arg) in gpiochip_hierarchy_create_domain()
1376 gc->irq.populate_parent_alloc_arg = in gpiochip_hierarchy_create_domain()
1379 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); in gpiochip_hierarchy_create_domain()
1382 gc->irq.parent_domain, in gpiochip_hierarchy_create_domain()
1384 gc->ngpio, in gpiochip_hierarchy_create_domain()
1385 gc->irq.fwnode, in gpiochip_hierarchy_create_domain()
1386 &gc->irq.child_irq_domain_ops, in gpiochip_hierarchy_create_domain()
1390 return ERR_PTR(-ENOMEM); in gpiochip_hierarchy_create_domain()
1392 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); in gpiochip_hierarchy_create_domain()
1399 return !!gc->irq.parent_domain; in gpiochip_hierarchy_is_hierarchical()
1407 struct irq_fwspec *fwspec = &gfwspec->fwspec; in gpiochip_populate_parent_fwspec_twocell()
1409 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_twocell()
1410 fwspec->param_count = 2; in gpiochip_populate_parent_fwspec_twocell()
1411 fwspec->param[0] = parent_hwirq; in gpiochip_populate_parent_fwspec_twocell()
1412 fwspec->param[1] = parent_type; in gpiochip_populate_parent_fwspec_twocell()
1423 struct irq_fwspec *fwspec = &gfwspec->fwspec; in gpiochip_populate_parent_fwspec_fourcell()
1425 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_fourcell()
1426 fwspec->param_count = 4; in gpiochip_populate_parent_fwspec_fourcell()
1427 fwspec->param[0] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1428 fwspec->param[1] = parent_hwirq; in gpiochip_populate_parent_fwspec_fourcell()
1429 fwspec->param[2] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1430 fwspec->param[3] = parent_type; in gpiochip_populate_parent_fwspec_fourcell()
1440 return ERR_PTR(-EINVAL); in gpiochip_hierarchy_create_domain()
1451 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1462 struct gpio_chip *gc = d->host_data; in gpiochip_irq_map()
1466 return -ENXIO; in gpiochip_irq_map()
1473 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_irq_map()
1474 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler); in gpiochip_irq_map()
1476 if (gc->irq.threaded) in gpiochip_irq_map()
1480 if (gc->irq.num_parents == 1) in gpiochip_irq_map()
1481 ret = irq_set_parent(irq, gc->irq.parents[0]); in gpiochip_irq_map()
1482 else if (gc->irq.map) in gpiochip_irq_map()
1483 ret = irq_set_parent(irq, gc->irq.map[hwirq]); in gpiochip_irq_map()
1489 * No set-up of the hardware will happen if IRQ_TYPE_NONE in gpiochip_irq_map()
1492 if (gc->irq.default_type != IRQ_TYPE_NONE) in gpiochip_irq_map()
1493 irq_set_irq_type(irq, gc->irq.default_type); in gpiochip_irq_map()
1501 struct gpio_chip *gc = d->host_data; in gpiochip_irq_unmap()
1503 if (gc->irq.threaded) in gpiochip_irq_unmap()
1519 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev); in gpiochip_simple_create_domain()
1522 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first, in gpiochip_simple_create_domain()
1525 return ERR_PTR(-EINVAL); in gpiochip_simple_create_domain()
1536 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1548 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_activate()
1556 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1567 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_deactivate()
1576 struct irq_domain *domain = gc->irq.domain; in gpiochip_to_irq()
1584 if (!gc->irq.initialized) in gpiochip_to_irq()
1585 return -EPROBE_DEFER; in gpiochip_to_irq()
1589 return -ENXIO; in gpiochip_to_irq()
1595 spec.fwnode = domain->fwnode; in gpiochip_to_irq()
1597 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset); in gpiochip_to_irq()
1630 if (gc->irq.irq_mask) in gpiochip_irq_mask()
1631 gc->irq.irq_mask(d); in gpiochip_irq_mask()
1641 if (gc->irq.irq_unmask) in gpiochip_irq_unmask()
1642 gc->irq.irq_unmask(d); in gpiochip_irq_unmask()
1651 gc->irq.irq_enable(d); in gpiochip_irq_enable()
1659 gc->irq.irq_disable(d); in gpiochip_irq_disable()
1665 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_set_irq_hooks()
1667 if (irqchip->flags & IRQCHIP_IMMUTABLE) in gpiochip_set_irq_hooks()
1672 if (!irqchip->irq_request_resources && in gpiochip_set_irq_hooks()
1673 !irqchip->irq_release_resources) { in gpiochip_set_irq_hooks()
1674 irqchip->irq_request_resources = gpiochip_irq_reqres; in gpiochip_set_irq_hooks()
1675 irqchip->irq_release_resources = gpiochip_irq_relres; in gpiochip_set_irq_hooks()
1677 if (WARN_ON(gc->irq.irq_enable)) in gpiochip_set_irq_hooks()
1680 if (irqchip->irq_enable == gpiochip_irq_enable || in gpiochip_set_irq_hooks()
1681 irqchip->irq_mask == gpiochip_irq_mask) { in gpiochip_set_irq_hooks()
1691 if (irqchip->irq_disable) { in gpiochip_set_irq_hooks()
1692 gc->irq.irq_disable = irqchip->irq_disable; in gpiochip_set_irq_hooks()
1693 irqchip->irq_disable = gpiochip_irq_disable; in gpiochip_set_irq_hooks()
1695 gc->irq.irq_mask = irqchip->irq_mask; in gpiochip_set_irq_hooks()
1696 irqchip->irq_mask = gpiochip_irq_mask; in gpiochip_set_irq_hooks()
1699 if (irqchip->irq_enable) { in gpiochip_set_irq_hooks()
1700 gc->irq.irq_enable = irqchip->irq_enable; in gpiochip_set_irq_hooks()
1701 irqchip->irq_enable = gpiochip_irq_enable; in gpiochip_set_irq_hooks()
1703 gc->irq.irq_unmask = irqchip->irq_unmask; in gpiochip_set_irq_hooks()
1704 irqchip->irq_unmask = gpiochip_irq_unmask; in gpiochip_set_irq_hooks()
1713 return -EINVAL; in gpiochip_irqchip_add_allocated_domain()
1715 if (gc->to_irq) in gpiochip_irqchip_add_allocated_domain()
1718 gc->to_irq = gpiochip_to_irq; in gpiochip_irqchip_add_allocated_domain()
1719 gc->irq.domain = domain; in gpiochip_irqchip_add_allocated_domain()
1720 gc->irq.domain_is_allocated_externally = allocated_externally; in gpiochip_irqchip_add_allocated_domain()
1724 * gc->irq.initialized before adding irqdomain. in gpiochip_irqchip_add_allocated_domain()
1728 gc->irq.initialized = true; in gpiochip_irqchip_add_allocated_domain()
1734 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1743 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev); in gpiochip_add_irqchip()
1744 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_add_irqchip()
1753 if (gc->irq.parent_handler && gc->can_sleep) { in gpiochip_add_irqchip()
1755 return -EINVAL; in gpiochip_add_irqchip()
1758 type = gc->irq.default_type; in gpiochip_add_irqchip()
1769 gc->irq.default_type = type; in gpiochip_add_irqchip()
1770 gc->irq.lock_key = lock_key; in gpiochip_add_irqchip()
1771 gc->irq.request_key = request_key; in gpiochip_add_irqchip()
1782 if (gc->irq.parent_handler) { in gpiochip_add_irqchip()
1783 for (i = 0; i < gc->irq.num_parents; i++) { in gpiochip_add_irqchip()
1786 if (gc->irq.per_parent_data) in gpiochip_add_irqchip()
1787 data = gc->irq.parent_handler_data_array[i]; in gpiochip_add_irqchip()
1789 data = gc->irq.parent_handler_data ?: gc; in gpiochip_add_irqchip()
1796 irq_set_chained_handler_and_data(gc->irq.parents[i], in gpiochip_add_irqchip()
1797 gc->irq.parent_handler, in gpiochip_add_irqchip()
1814 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1821 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_irqchip_remove()
1826 if (irqchip && gc->irq.parent_handler) { in gpiochip_irqchip_remove()
1827 struct gpio_irq_chip *irq = &gc->irq; in gpiochip_irqchip_remove()
1830 for (i = 0; i < irq->num_parents; i++) in gpiochip_irqchip_remove()
1831 irq_set_chained_handler_and_data(irq->parents[i], in gpiochip_irqchip_remove()
1836 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) { in gpiochip_irqchip_remove()
1839 for (offset = 0; offset < gc->ngpio; offset++) { in gpiochip_irqchip_remove()
1843 irq = irq_find_mapping(gc->irq.domain, offset); in gpiochip_irqchip_remove()
1847 irq_domain_remove(gc->irq.domain); in gpiochip_irqchip_remove()
1850 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) { in gpiochip_irqchip_remove()
1851 if (irqchip->irq_request_resources == gpiochip_irq_reqres) { in gpiochip_irqchip_remove()
1852 irqchip->irq_request_resources = NULL; in gpiochip_irqchip_remove()
1853 irqchip->irq_release_resources = NULL; in gpiochip_irqchip_remove()
1855 if (irqchip->irq_enable == gpiochip_irq_enable) { in gpiochip_irqchip_remove()
1856 irqchip->irq_enable = gc->irq.irq_enable; in gpiochip_irqchip_remove()
1857 irqchip->irq_disable = gc->irq.irq_disable; in gpiochip_irqchip_remove()
1860 gc->irq.irq_enable = NULL; in gpiochip_irqchip_remove()
1861 gc->irq.irq_disable = NULL; in gpiochip_irqchip_remove()
1862 gc->irq.chip = NULL; in gpiochip_irqchip_remove()
1868 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1906 * gpiochip_generic_request() - request the gpio function for a pin
1913 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_request()
1917 return pinctrl_gpio_request(gc->gpiodev->base + offset); in gpiochip_generic_request()
1922 * gpiochip_generic_free() - free the gpio function from a pin
1929 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_free()
1933 pinctrl_gpio_free(gc->gpiodev->base + offset); in gpiochip_generic_free()
1938 * gpiochip_generic_config() - apply configuration for a pin
1946 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config); in gpiochip_generic_config()
1953 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1959 * Calling this function directly from a DeviceTree-supported
1962 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1969 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pingroup_range()
1975 return -ENOMEM; in gpiochip_add_pingroup_range()
1979 pin_range->range.id = gpio_offset; in gpiochip_add_pingroup_range()
1980 pin_range->range.gc = gc; in gpiochip_add_pingroup_range()
1981 pin_range->range.name = gc->label; in gpiochip_add_pingroup_range()
1982 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pingroup_range()
1983 pin_range->pctldev = pctldev; in gpiochip_add_pingroup_range()
1986 &pin_range->range.pins, in gpiochip_add_pingroup_range()
1987 &pin_range->range.npins); in gpiochip_add_pingroup_range()
1993 pinctrl_add_gpio_range(pctldev, &pin_range->range); in gpiochip_add_pingroup_range()
1995 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n", in gpiochip_add_pingroup_range()
1996 gpio_offset, gpio_offset + pin_range->range.npins - 1, in gpiochip_add_pingroup_range()
1999 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pingroup_range()
2006 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2015 * 0 on success, or a negative error-code on failure.
2017 * Calling this function directly from a DeviceTree-supported
2020 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2027 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pin_range()
2033 return -ENOMEM; in gpiochip_add_pin_range()
2037 pin_range->range.id = gpio_offset; in gpiochip_add_pin_range()
2038 pin_range->range.gc = gc; in gpiochip_add_pin_range()
2039 pin_range->range.name = gc->label; in gpiochip_add_pin_range()
2040 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pin_range()
2041 pin_range->range.pin_base = pin_offset; in gpiochip_add_pin_range()
2042 pin_range->range.npins = npins; in gpiochip_add_pin_range()
2043 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, in gpiochip_add_pin_range()
2044 &pin_range->range); in gpiochip_add_pin_range()
2045 if (IS_ERR(pin_range->pctldev)) { in gpiochip_add_pin_range()
2046 ret = PTR_ERR(pin_range->pctldev); in gpiochip_add_pin_range()
2051 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n", in gpiochip_add_pin_range()
2052 gpio_offset, gpio_offset + npins - 1, in gpiochip_add_pin_range()
2054 pin_offset, pin_offset + npins - 1); in gpiochip_add_pin_range()
2056 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pin_range()
2063 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2069 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove_pin_ranges()
2071 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { in gpiochip_remove_pin_ranges()
2072 list_del(&pin_range->node); in gpiochip_remove_pin_ranges()
2073 pinctrl_remove_gpio_range(pin_range->pctldev, in gpiochip_remove_pin_ranges()
2074 &pin_range->range); in gpiochip_remove_pin_ranges()
2088 struct gpio_chip *gc = desc->gdev->chip; in gpiod_request_commit()
2096 return -ENOMEM; in gpiod_request_commit()
2102 * before IRQs are enabled, for non-sleeping (SOC) GPIOs. in gpiod_request_commit()
2105 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { in gpiod_request_commit()
2108 ret = -EBUSY; in gpiod_request_commit()
2112 if (gc->request) { in gpiod_request_commit()
2113 /* gc->request may sleep */ in gpiod_request_commit()
2117 ret = gc->request(gc, offset); in gpiod_request_commit()
2119 ret = -EINVAL; in gpiod_request_commit()
2124 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_request_commit()
2128 if (gc->get_direction) { in gpiod_request_commit()
2129 /* gc->get_direction may sleep */ in gpiod_request_commit()
2157 if (!desc->gdev) { in validate_desc()
2159 return -EINVAL; in validate_desc()
2161 if (!desc->gdev->chip) { in validate_desc()
2162 dev_warn(&desc->gdev->dev, in validate_desc()
2183 int ret = -EPROBE_DEFER; in gpiod_request()
2187 if (try_module_get(desc->gdev->owner)) { in gpiod_request()
2190 module_put(desc->gdev->owner); in gpiod_request()
2192 gpio_device_get(desc->gdev); in gpiod_request()
2211 gc = desc->gdev->chip; in gpiod_free_commit()
2212 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) { in gpiod_free_commit()
2213 if (gc->free) { in gpiod_free_commit()
2215 might_sleep_if(gc->can_sleep); in gpiod_free_commit()
2216 gc->free(gc, gpio_chip_hwgpio(desc)); in gpiod_free_commit()
2219 kfree_const(desc->label); in gpiod_free_commit()
2221 clear_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_free_commit()
2222 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_free_commit()
2223 clear_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_free_commit()
2224 clear_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_free_commit()
2225 clear_bit(FLAG_PULL_UP, &desc->flags); in gpiod_free_commit()
2226 clear_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_free_commit()
2227 clear_bit(FLAG_BIAS_DISABLE, &desc->flags); in gpiod_free_commit()
2228 clear_bit(FLAG_EDGE_RISING, &desc->flags); in gpiod_free_commit()
2229 clear_bit(FLAG_EDGE_FALLING, &desc->flags); in gpiod_free_commit()
2230 clear_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_free_commit()
2232 desc->hog = NULL; in gpiod_free_commit()
2235 WRITE_ONCE(desc->debounce_period_us, 0); in gpiod_free_commit()
2249 * We must not use VALIDATE_DESC_VOID() as the underlying gdev->chip in gpiod_free()
2258 module_put(desc->gdev->owner); in gpiod_free()
2259 gpio_device_put(desc->gdev); in gpiod_free()
2263 * gpiochip_is_requested - return string iff signal was requested
2265 * @offset: of signal within controller's 0..(ngpio - 1) range
2269 * passed it is a meaningless, non-NULL constant.
2283 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) in gpiochip_is_requested()
2285 return desc->label; in gpiochip_is_requested()
2290 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2307 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2340 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2358 * using the open-drain emulation idiom) so these are natural places
2366 if (!gc->set_config) in gpio_do_set_config()
2367 return -ENOTSUPP; in gpio_do_set_config()
2369 return gc->set_config(gc, offset, config); in gpio_do_set_config()
2376 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_config_with_argument()
2387 struct device *dev = &desc->gdev->dev; in gpio_set_config_with_argument_optional()
2392 if (ret != -ENOTSUPP) in gpio_set_config_with_argument_optional()
2416 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) in gpio_set_bias()
2418 else if (test_bit(FLAG_PULL_UP, &desc->flags)) in gpio_set_bias()
2420 else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) in gpio_set_bias()
2440 * gpio_set_debounce_timeout() - Set debounce timeout
2457 * gpiod_direction_input - set the GPIO direction to input
2471 gc = desc->gdev->chip; in gpiod_direction_input()
2475 * the chip is output-only, but you can't specify .direction_input() in gpiod_direction_input()
2478 if (!gc->get && gc->direction_input) { in gpiod_direction_input()
2482 return -EIO; in gpiod_direction_input()
2487 * just call it. Else we are some input-only chip so try to check the in gpiod_direction_input()
2491 if (gc->direction_input) { in gpiod_direction_input()
2492 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc)); in gpiod_direction_input()
2493 } else if (gc->get_direction && in gpiod_direction_input()
2494 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) { in gpiod_direction_input()
2498 return -EIO; in gpiod_direction_input()
2501 clear_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_input()
2513 struct gpio_chip *gc = desc->gdev->chip; in gpiod_direction_output_raw_commit()
2519 * output-only, but if there is then not even a .set() operation it in gpiod_direction_output_raw_commit()
2522 if (!gc->set && !gc->direction_output) { in gpiod_direction_output_raw_commit()
2526 return -EIO; in gpiod_direction_output_raw_commit()
2529 if (gc->direction_output) { in gpiod_direction_output_raw_commit()
2530 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); in gpiod_direction_output_raw_commit()
2533 if (gc->get_direction && in gpiod_direction_output_raw_commit()
2534 gc->get_direction(gc, gpio_chip_hwgpio(desc))) { in gpiod_direction_output_raw_commit()
2538 return -EIO; in gpiod_direction_output_raw_commit()
2542 * output-only chip, so just drive the output as desired. in gpiod_direction_output_raw_commit()
2544 gc->set(gc, gpio_chip_hwgpio(desc), val); in gpiod_direction_output_raw_commit()
2548 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output_raw_commit()
2555 * gpiod_direction_output_raw - set the GPIO direction to output
2573 * gpiod_direction_output - set the GPIO direction to output
2589 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_direction_output()
2595 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && in gpiod_direction_output()
2596 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { in gpiod_direction_output()
2600 return -EIO; in gpiod_direction_output()
2603 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiod_direction_output()
2613 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { in gpiod_direction_output()
2634 * When emulating open-source or open-drain functionalities by not in gpiod_direction_output()
2640 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output()
2646 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2660 gc = desc->gdev->chip; in gpiod_enable_hw_timestamp_ns()
2661 if (!gc->en_hw_timestamp) { in gpiod_enable_hw_timestamp_ns()
2663 return -ENOTSUPP; in gpiod_enable_hw_timestamp_ns()
2666 ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags); in gpiod_enable_hw_timestamp_ns()
2675 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2689 gc = desc->gdev->chip; in gpiod_disable_hw_timestamp_ns()
2690 if (!gc->dis_hw_timestamp) { in gpiod_disable_hw_timestamp_ns()
2692 return -ENOTSUPP; in gpiod_disable_hw_timestamp_ns()
2695 ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags); in gpiod_disable_hw_timestamp_ns()
2704 * gpiod_set_config - sets @config for a GPIO
2706 * @config: Same packed config format as generic pinconf
2709 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2717 gc = desc->gdev->chip; in gpiod_set_config()
2724 * gpiod_set_debounce - sets @debounce time for a GPIO
2729 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2742 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2756 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); in gpiod_set_transitory()
2766 * gpiod_is_active_low - test whether a GPIO is active-low or not
2769 * Returns 1 if the GPIO is active-low, 0 otherwise.
2774 return test_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_is_active_low()
2779 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2785 change_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_toggle_active_low()
2791 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO; in gpio_chip_get_value()
2804 *------------------------------------------------------------------------
2806 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2821 gc = desc->gdev->chip; in gpiod_get_raw_value_commit()
2831 if (gc->get_multiple) in gpio_chip_get_multiple()
2832 return gc->get_multiple(gc, mask, bits); in gpio_chip_get_multiple()
2833 if (gc->get) { in gpio_chip_get_multiple()
2836 for_each_set_bit(i, mask, gc->ngpio) { in gpio_chip_get_multiple()
2837 value = gc->get(gc, i); in gpio_chip_get_multiple()
2844 return -EIO; in gpio_chip_get_multiple()
2860 if (array_info && array_info->desc == desc_array && in gpiod_get_array_value_complex()
2861 array_size <= array_info->size && in gpiod_get_array_value_complex()
2862 (void *)array_info == desc_array + array_info->size) { in gpiod_get_array_value_complex()
2864 WARN_ON(array_info->chip->can_sleep); in gpiod_get_array_value_complex()
2866 ret = gpio_chip_get_multiple(array_info->chip, in gpiod_get_array_value_complex()
2867 array_info->get_mask, in gpiod_get_array_value_complex()
2872 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_get_array_value_complex()
2874 array_info->invert_mask, array_size); in gpiod_get_array_value_complex()
2876 i = find_first_zero_bit(array_info->get_mask, array_size); in gpiod_get_array_value_complex()
2884 struct gpio_chip *gc = desc_array[i]->gdev->chip; in gpiod_get_array_value_complex()
2890 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_get_array_value_complex()
2896 mask = bitmap_alloc(gc->ngpio, flags); in gpiod_get_array_value_complex()
2898 return -ENOMEM; in gpiod_get_array_value_complex()
2900 bits = bitmap_alloc(gc->ngpio, flags); in gpiod_get_array_value_complex()
2903 return -ENOMEM; in gpiod_get_array_value_complex()
2907 bitmap_zero(mask, gc->ngpio); in gpiod_get_array_value_complex()
2910 WARN_ON(gc->can_sleep); in gpiod_get_array_value_complex()
2922 i = find_next_zero_bit(array_info->get_mask, in gpiod_get_array_value_complex()
2925 (desc_array[i]->gdev->chip == gc)); in gpiod_get_array_value_complex()
2941 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_array_value_complex()
2948 j = find_next_zero_bit(array_info->get_mask, i, in gpiod_get_array_value_complex()
2961 * gpiod_get_raw_value() - return a gpio's raw value
2974 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_raw_value()
2980 * gpiod_get_value() - return a gpio's value
2995 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_value()
3001 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value()
3009 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3028 return -EINVAL; in gpiod_get_raw_array_value()
3036 * gpiod_get_array_value() - read values from an array of GPIOs
3054 return -EINVAL; in gpiod_get_array_value()
3062 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3064 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3069 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_open_drain_value_commit()
3073 ret = gc->direction_input(gc, offset); in gpio_set_open_drain_value_commit()
3075 ret = gc->direction_output(gc, offset, 0); in gpio_set_open_drain_value_commit()
3077 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_drain_value_commit()
3087 * _gpio_set_open_source_value() - Set the open source gpio's value.
3089 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3094 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_open_source_value_commit()
3098 ret = gc->direction_output(gc, offset, 1); in gpio_set_open_source_value_commit()
3100 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_source_value_commit()
3102 ret = gc->direction_input(gc, offset); in gpio_set_open_source_value_commit()
3115 gc = desc->gdev->chip; in gpiod_set_raw_value_commit()
3117 gc->set(gc, gpio_chip_hwgpio(desc), value); in gpiod_set_raw_value_commit()
3133 if (gc->set_multiple) { in gpio_chip_set_multiple()
3134 gc->set_multiple(gc, mask, bits); in gpio_chip_set_multiple()
3139 for_each_set_bit(i, mask, gc->ngpio) in gpio_chip_set_multiple()
3140 gc->set(gc, i, test_bit(i, bits)); in gpio_chip_set_multiple()
3157 if (array_info && array_info->desc == desc_array && in gpiod_set_array_value_complex()
3158 array_size <= array_info->size && in gpiod_set_array_value_complex()
3159 (void *)array_info == desc_array + array_info->size) { in gpiod_set_array_value_complex()
3161 WARN_ON(array_info->chip->can_sleep); in gpiod_set_array_value_complex()
3163 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_set_array_value_complex()
3165 array_info->invert_mask, array_size); in gpiod_set_array_value_complex()
3167 gpio_chip_set_multiple(array_info->chip, array_info->set_mask, in gpiod_set_array_value_complex()
3170 i = find_first_zero_bit(array_info->set_mask, array_size); in gpiod_set_array_value_complex()
3178 struct gpio_chip *gc = desc_array[i]->gdev->chip; in gpiod_set_array_value_complex()
3184 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_set_array_value_complex()
3190 mask = bitmap_alloc(gc->ngpio, flags); in gpiod_set_array_value_complex()
3192 return -ENOMEM; in gpiod_set_array_value_complex()
3194 bits = bitmap_alloc(gc->ngpio, flags); in gpiod_set_array_value_complex()
3197 return -ENOMEM; in gpiod_set_array_value_complex()
3201 bitmap_zero(mask, gc->ngpio); in gpiod_set_array_value_complex()
3204 WARN_ON(gc->can_sleep); in gpiod_set_array_value_complex()
3217 test_bit(i, array_info->invert_mask)) && in gpiod_set_array_value_complex()
3218 test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_array_value_complex()
3225 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3227 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3237 i = find_next_zero_bit(array_info->set_mask, in gpiod_set_array_value_complex()
3240 (desc_array[i]->gdev->chip == gc)); in gpiod_set_array_value_complex()
3254 * gpiod_set_raw_value() - assign a gpio's raw value
3268 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_set_raw_value()
3274 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3284 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_value_nocheck()
3286 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) in gpiod_set_value_nocheck()
3288 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) in gpiod_set_value_nocheck()
3295 * gpiod_set_value() - assign a gpio's value
3309 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_set_value()
3315 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3333 return -EINVAL; in gpiod_set_raw_array_value()
3340 * gpiod_set_array_value() - assign values to an array of GPIOs
3358 return -EINVAL; in gpiod_set_array_value()
3366 * gpiod_cansleep() - report whether gpio value access may sleep
3373 return desc->gdev->chip->can_sleep; in gpiod_cansleep()
3378 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3388 return -ENOMEM; in gpiod_set_consumer_name()
3391 kfree_const(desc->label); in gpiod_set_consumer_name()
3399 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3415 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) in gpiod_to_irq()
3416 return -EINVAL; in gpiod_to_irq()
3418 gc = desc->gdev->chip; in gpiod_to_irq()
3420 if (gc->to_irq) { in gpiod_to_irq()
3421 int retirq = gc->to_irq(gc, offset); in gpiod_to_irq()
3425 return -ENXIO; in gpiod_to_irq()
3430 if (gc->irq.chip) { in gpiod_to_irq()
3436 return -EPROBE_DEFER; in gpiod_to_irq()
3439 return -ENXIO; in gpiod_to_irq()
3444 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3463 if (!gc->can_sleep && gc->get_direction) { in gpiochip_lock_as_irq()
3474 if (test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_lock_as_irq()
3475 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiochip_lock_as_irq()
3479 return -EIO; in gpiochip_lock_as_irq()
3482 set_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_lock_as_irq()
3483 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_lock_as_irq()
3490 if (!desc->label) in gpiochip_lock_as_irq()
3498 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3513 clear_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_unlock_as_irq()
3514 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_unlock_as_irq()
3517 if (desc->label && !strcmp(desc->label, "interrupt")) in gpiochip_unlock_as_irq()
3527 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) in gpiochip_disable_irq()
3528 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_disable_irq()
3537 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { in gpiochip_enable_irq()
3542 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_enable_irq()
3543 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); in gpiochip_enable_irq()
3544 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_enable_irq()
3551 if (offset >= gc->ngpio) in gpiochip_line_is_irq()
3554 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_irq()
3562 if (!try_module_get(gc->gpiodev->owner)) in gpiochip_reqres_irq()
3563 return -ENODEV; in gpiochip_reqres_irq()
3568 module_put(gc->gpiodev->owner); in gpiochip_reqres_irq()
3578 module_put(gc->gpiodev->owner); in gpiochip_relres_irq()
3584 if (offset >= gc->ngpio) in gpiochip_line_is_open_drain()
3587 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_drain()
3593 if (offset >= gc->ngpio) in gpiochip_line_is_open_source()
3596 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_source()
3602 if (offset >= gc->ngpio) in gpiochip_line_is_persistent()
3605 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_persistent()
3610 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3627 * gpiod_get_value_cansleep() - return a gpio's value
3645 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value_cansleep()
3653 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3672 return -EINVAL; in gpiod_get_raw_array_value_cansleep()
3680 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3698 return -EINVAL; in gpiod_get_array_value_cansleep()
3706 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3724 * gpiod_set_value_cansleep() - assign a gpio's value
3742 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3760 return -EINVAL; in gpiod_set_raw_array_value_cansleep()
3767 * gpiod_add_lookup_tables() - register GPIO device consumers
3778 list_add_tail(&tables[i]->list, &gpio_lookup_list); in gpiod_add_lookup_tables()
3784 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3802 return -EINVAL; in gpiod_set_array_value_cansleep()
3811 blocking_notifier_call_chain(&desc->gdev->line_state_notifier, in gpiod_line_state_notify()
3816 * gpiod_add_lookup_table() - register GPIO device consumers
3826 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3837 list_del(&table->list); in gpiod_remove_lookup_table()
3844 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3854 for (hog = &hogs[0]; hog->chip_label; hog++) { in gpiod_add_hogs()
3855 list_add_tail(&hog->list, &gpio_machine_hogs); in gpiod_add_hogs()
3861 gc = find_chip_by_name(hog->chip_label); in gpiod_add_hogs()
3875 for (hog = &hogs[0]; hog->chip_label; hog++) in gpiod_remove_hogs()
3876 list_del(&hog->list); in gpiod_remove_hogs()
3889 if (table->dev_id && dev_id) { in gpiod_find_lookup_table()
3894 if (!strcmp(table->dev_id, dev_id)) in gpiod_find_lookup_table()
3901 if (dev_id == table->dev_id) in gpiod_find_lookup_table()
3915 struct gpio_desc *desc = ERR_PTR(-ENOENT); in gpiod_find()
3923 for (p = &table->table[0]; p->key; p++) { in gpiod_find()
3927 if (p->idx != idx) in gpiod_find()
3931 if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) in gpiod_find()
3934 if (p->chip_hwnum == U16_MAX) { in gpiod_find()
3935 desc = gpio_name_to_desc(p->key); in gpiod_find()
3937 *flags = p->flags; in gpiod_find()
3942 p->key); in gpiod_find()
3943 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
3946 gc = find_chip_by_name(p->key); in gpiod_find()
3951 * p->key should exist, assume it may in gpiod_find()
3957 p->key); in gpiod_find()
3958 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
3961 if (gc->ngpio <= p->chip_hwnum) { in gpiod_find()
3964 idx, p->chip_hwnum, gc->ngpio - 1, in gpiod_find()
3965 gc->label); in gpiod_find()
3966 return ERR_PTR(-EINVAL); in gpiod_find()
3969 desc = gpiochip_get_desc(gc, p->chip_hwnum); in gpiod_find()
3970 *flags = p->flags; in gpiod_find()
3986 return -ENOENT; in platform_gpio_count()
3988 for (p = &table->table[0]; p->key; p++) { in platform_gpio_count()
3989 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || in platform_gpio_count()
3990 (!con_id && !p->con_id)) in platform_gpio_count()
3994 return -ENOENT; in platform_gpio_count()
4006 struct gpio_desc *desc = ERR_PTR(-ENOENT); in gpiod_find_by_fwnode()
4059 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) in gpiod_find_and_request()
4088 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4119 * gpiod_count - return the number of GPIOs associated with a device / function
4120 * or -ENOENT if no GPIO has been assigned to the requested function
4121 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4127 int count = -ENOENT; in gpiod_count()
4144 * gpiod_get - obtain a GPIO for a given GPIO function
4145 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4150 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4161 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4162 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4180 * gpiod_configure_flags - helper function to configure a given GPIO
4183 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4185 * @dflags: gpiod_flags - optional GPIO initialization flags
4187 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4197 set_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_configure_flags()
4200 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
4208 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
4214 set_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_configure_flags()
4220 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n"); in gpiod_configure_flags()
4221 return -EINVAL; in gpiod_configure_flags()
4225 set_bit(FLAG_PULL_UP, &desc->flags); in gpiod_configure_flags()
4227 set_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_configure_flags()
4229 set_bit(FLAG_BIAS_DISABLE, &desc->flags); in gpiod_configure_flags()
4252 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4253 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4261 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4279 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4281 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4306 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4309 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4311 * @dflags: gpiod_flags - optional GPIO initialization flags
4329 name, gc->label, hwnum, ret); in gpiod_hog()
4334 set_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_hog()
4345 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4357 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4358 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4364 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4386 return ERR_PTR(-ENOMEM); in gpiod_get_array()
4388 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) { in gpiod_get_array()
4389 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); in gpiod_get_array()
4395 descs->desc[descs->ndescs] = desc; in gpiod_get_array()
4402 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { in gpiod_get_array()
4405 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ? in gpiod_get_array()
4406 gc->ngpio : count); in gpiod_get_array()
4413 return ERR_PTR(-ENOMEM); in gpiod_get_array()
4419 array_info->get_mask = array_info->invert_mask + in gpiod_get_array()
4421 array_info->set_mask = array_info->get_mask + in gpiod_get_array()
4424 array_info->desc = descs->desc; in gpiod_get_array()
4425 array_info->size = count; in gpiod_get_array()
4426 array_info->chip = gc; in gpiod_get_array()
4427 bitmap_set(array_info->get_mask, descs->ndescs, in gpiod_get_array()
4428 count - descs->ndescs); in gpiod_get_array()
4429 bitmap_set(array_info->set_mask, descs->ndescs, in gpiod_get_array()
4430 count - descs->ndescs); in gpiod_get_array()
4431 descs->info = array_info; in gpiod_get_array()
4439 if (array_info->chip != gc) { in gpiod_get_array()
4440 __clear_bit(descs->ndescs, array_info->get_mask); in gpiod_get_array()
4441 __clear_bit(descs->ndescs, array_info->set_mask); in gpiod_get_array()
4447 else if (gpio_chip_hwgpio(desc) != descs->ndescs) { in gpiod_get_array()
4453 if (bitmap_full(array_info->get_mask, descs->ndescs)) { in gpiod_get_array()
4456 __clear_bit(descs->ndescs, in gpiod_get_array()
4457 array_info->get_mask); in gpiod_get_array()
4458 __clear_bit(descs->ndescs, in gpiod_get_array()
4459 array_info->set_mask); in gpiod_get_array()
4463 if (gpiochip_line_is_open_drain(gc, descs->ndescs) || in gpiod_get_array()
4464 gpiochip_line_is_open_source(gc, descs->ndescs)) in gpiod_get_array()
4465 __clear_bit(descs->ndescs, in gpiod_get_array()
4466 array_info->set_mask); in gpiod_get_array()
4469 __set_bit(descs->ndescs, in gpiod_get_array()
4470 array_info->invert_mask); in gpiod_get_array()
4476 array_info->chip->label, array_info->size, in gpiod_get_array()
4477 *array_info->get_mask, *array_info->set_mask, in gpiod_get_array()
4478 *array_info->invert_mask); in gpiod_get_array()
4484 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4486 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4508 * gpiod_put - dispose of a GPIO descriptor
4521 * gpiod_put_array - dispose of multiple GPIO descriptors
4528 for (i = 0; i < descs->ndescs; i++) in gpiod_put_array()
4529 gpiod_put(descs->desc[i]); in gpiod_put_array()
4599 struct gpio_chip *gc = gdev->chip; in gpiolib_dbg_show()
4601 unsigned gpio = gdev->base; in gpiolib_dbg_show()
4608 if (test_bit(FLAG_REQUESTED, &desc->flags)) { in gpiolib_dbg_show()
4610 is_out = test_bit(FLAG_IS_OUT, &desc->flags); in gpiolib_dbg_show()
4612 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiolib_dbg_show()
4613 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiolib_dbg_show()
4614 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n", in gpiolib_dbg_show()
4615 gpio, desc->name ?: "", desc->label, in gpiolib_dbg_show()
4620 } else if (desc->name) { in gpiolib_dbg_show()
4621 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name); in gpiolib_dbg_show()
4634 s->private = ""; in gpiolib_seq_start()
4638 if (index-- == 0) { in gpiolib_seq_start()
4654 if (list_is_last(&gdev->list, &gpio_devices)) in gpiolib_seq_next()
4657 ret = list_first_entry(&gdev->list, struct gpio_device, list); in gpiolib_seq_next()
4660 s->private = "\n"; in gpiolib_seq_next()
4673 struct gpio_chip *gc = gdev->chip; in gpiolib_seq_show()
4677 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, in gpiolib_seq_show()
4678 dev_name(&gdev->dev)); in gpiolib_seq_show()
4682 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, in gpiolib_seq_show()
4683 dev_name(&gdev->dev), in gpiolib_seq_show()
4684 gdev->base, gdev->base + gdev->ngpio - 1); in gpiolib_seq_show()
4685 parent = gc->parent; in gpiolib_seq_show()
4688 parent->bus ? parent->bus->name : "no-bus", in gpiolib_seq_show()
4690 if (gc->label) in gpiolib_seq_show()
4691 seq_printf(s, ", %s", gc->label); in gpiolib_seq_show()
4692 if (gc->can_sleep) in gpiolib_seq_show()
4696 if (gc->dbg_show) in gpiolib_seq_show()
4697 gc->dbg_show(s, gc); in gpiolib_seq_show()