• Home
  • Raw
  • Download

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

1 // SPDX-License-Identifier: GPL-2.0
30 #include "gpiolib-acpi.h"
31 #include "gpiolib-cdev.h"
32 #include "gpiolib-of.h"
33 #include "gpiolib-swnode.h"
34 #include "gpiolib-sysfs.h"
42 * The GPIO programming interface allows for inlining speed-critical
43 * get/set operations for common cases, so that access to SOC-integrated
60 /* Device and char device-related information */
65 static int gpio_bus_match(struct device *dev, struct device_driver *drv) in gpio_bus_match() argument
73 if (fwnode && fwnode->dev != dev) in gpio_bus_match()
114 d->label = label; in desc_set_label()
118 * gpio_to_desc - Convert a GPIO number to its descriptor
133 if (gdev->base <= gpio && in gpio_to_desc()
134 gdev->base + gdev->ngpio > gpio) { in gpio_to_desc()
136 return &gdev->descs[gpio - gdev->base]; in gpio_to_desc()
150 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
156 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
162 struct gpio_device *gdev = gc->gpiodev; in gpiochip_get_desc()
164 if (hwnum >= gdev->ngpio) in gpiochip_get_desc()
165 return ERR_PTR(-EINVAL); in gpiochip_get_desc()
167 return &gdev->descs[hwnum]; in gpiochip_get_desc()
172 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
183 return desc->gdev->base + (desc - &desc->gdev->descs[0]); in desc_to_gpio()
189 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
194 if (!desc || !desc->gdev) in gpiod_to_chip()
196 return desc->gdev->chip; in gpiod_to_chip()
208 if (gdev->base >= base + ngpio) in gpiochip_find_base()
211 base = gdev->base + gdev->ngpio; in gpiochip_find_base()
221 return -ENOSPC; in gpiochip_find_base()
226 * gpiod_get_direction - return the current direction of a GPIO
246 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && in gpiod_get_direction()
247 test_bit(FLAG_IS_OUT, &desc->flags)) in gpiod_get_direction()
250 if (!gc->get_direction) in gpiod_get_direction()
251 return -ENOTSUPP; in gpiod_get_direction()
253 ret = gc->get_direction(gc, offset); in gpiod_get_direction()
261 assign_bit(FLAG_IS_OUT, &desc->flags, !ret); in gpiod_get_direction()
269 * by range(means [base, base + ngpio - 1]) order.
271 * Return -EBUSY if the new chip overlaps with some other chip's integer
280 list_add_tail(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
285 if (gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list()
287 list_add(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
292 if (prev->base + prev->ngpio <= gdev->base) { in gpiodev_add_to_list()
294 list_add_tail(&gdev->list, &gpio_devices); in gpiodev_add_to_list()
300 if (&next->list == &gpio_devices) in gpiodev_add_to_list()
304 if (prev->base + prev->ngpio <= gdev->base in gpiodev_add_to_list()
305 && gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list()
306 list_add(&gdev->list, &prev->list); in gpiodev_add_to_list()
311 return -EBUSY; in gpiodev_add_to_list()
333 for_each_gpio_desc(gdev->chip, desc) { in gpio_name_to_desc()
334 if (desc->name && !strcmp(desc->name, name)) { in gpio_name_to_desc()
347 * Take the names from gc->names and assign them to their GPIO descriptors.
351 * 1. Non-unique names are still accepted,
356 struct gpio_device *gdev = gc->gpiodev; in gpiochip_set_desc_names()
360 for (i = 0; i != gc->ngpio; ++i) { in gpiochip_set_desc_names()
363 gpio = gpio_name_to_desc(gc->names[i]); in gpiochip_set_desc_names()
365 dev_warn(&gdev->dev, in gpiochip_set_desc_names()
367 gc->names[i]); in gpiochip_set_desc_names()
371 for (i = 0; i != gc->ngpio; ++i) in gpiochip_set_desc_names()
372 gdev->descs[i].name = gc->names[i]; in gpiochip_set_desc_names()
378 * gpiochip_set_names - Set GPIO line names using device properties
381 * Looks for device property "gpio-line-names" and if it exists assigns
388 struct gpio_device *gdev = chip->gpiodev; in gpiochip_set_names()
389 struct device *dev = &gdev->dev; in gpiochip_set_names()
394 count = device_property_string_array_count(dev, "gpio-line-names"); in gpiochip_set_names()
401 * setting friendly names if the specified ones with 'gpio-line-names' in gpiochip_set_names()
406 if (count <= chip->offset) { in gpiochip_set_names()
407 …dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset … in gpiochip_set_names()
408 count, chip->offset); in gpiochip_set_names()
414 return -ENOMEM; in gpiochip_set_names()
416 ret = device_property_read_string_array(dev, "gpio-line-names", in gpiochip_set_names()
426 * contain at most number gpiochips x chip->ngpio. We have to in gpiochip_set_names()
428 * chip->offset as starting point from where we will assign in gpiochip_set_names()
430 * 'gpio-line-names' cannot contains gaps, we have to be sure in gpiochip_set_names()
431 * we only assign those pins that really exists since chip->ngpio in gpiochip_set_names()
432 * can be different of the chip->offset. in gpiochip_set_names()
434 count = (count > chip->offset) ? count - chip->offset : count; in gpiochip_set_names()
435 if (count > chip->ngpio) in gpiochip_set_names()
436 count = chip->ngpio; in gpiochip_set_names()
445 if (names[chip->offset + i] && names[chip->offset + i][0]) in gpiochip_set_names()
446 gdev->descs[i].name = names[chip->offset + i]; in gpiochip_set_names()
458 p = bitmap_alloc(gc->ngpio, GFP_KERNEL); in gpiochip_allocate_mask()
463 bitmap_fill(p, gc->ngpio); in gpiochip_allocate_mask()
476 struct device *dev = &gc->gpiodev->dev; in gpiochip_count_reserved_ranges()
480 size = device_property_count_u32(dev, "gpio-reserved-ranges"); in gpiochip_count_reserved_ranges()
489 struct device *dev = &gc->gpiodev->dev; in gpiochip_apply_reserved_ranges()
500 return -ENOMEM; in gpiochip_apply_reserved_ranges()
502 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges", in gpiochip_apply_reserved_ranges()
510 u32 count = ranges[--size]; in gpiochip_apply_reserved_ranges()
511 u32 start = ranges[--size]; in gpiochip_apply_reserved_ranges()
513 if (start >= gc->ngpio || start + count > gc->ngpio) in gpiochip_apply_reserved_ranges()
516 bitmap_clear(gc->valid_mask, start, count); in gpiochip_apply_reserved_ranges()
527 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask)) in gpiochip_init_valid_mask()
530 gc->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_init_valid_mask()
531 if (!gc->valid_mask) in gpiochip_init_valid_mask()
532 return -ENOMEM; in gpiochip_init_valid_mask()
538 if (gc->init_valid_mask) in gpiochip_init_valid_mask()
539 return gc->init_valid_mask(gc, in gpiochip_init_valid_mask()
540 gc->valid_mask, in gpiochip_init_valid_mask()
541 gc->ngpio); in gpiochip_init_valid_mask()
548 gpiochip_free_mask(&gc->valid_mask); in gpiochip_free_valid_mask()
554 * Device Tree platforms are supposed to use "gpio-ranges" in gpiochip_add_pin_ranges()
555 * property. This check ensures that the ->add_pin_ranges() in gpiochip_add_pin_ranges()
558 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges")) in gpiochip_add_pin_ranges()
561 if (gc->add_pin_ranges) in gpiochip_add_pin_ranges()
562 return gc->add_pin_ranges(gc); in gpiochip_add_pin_ranges()
571 if (likely(!gc->valid_mask)) in gpiochip_line_is_valid()
573 return test_bit(offset, gc->valid_mask); in gpiochip_line_is_valid()
583 list_del(&gdev->list); in gpiodev_release()
586 ida_free(&gpio_ida, gdev->id); in gpiodev_release()
587 kfree_const(gdev->label); in gpiodev_release()
588 kfree(gdev->descs); in gpiodev_release()
600 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
601 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
606 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev); in gpiochip_setup_dev()
613 if (fwnode && !fwnode->dev) in gpiochip_setup_dev()
621 gdev->dev.release = gpiodev_release; in gpiochip_setup_dev()
627 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base, in gpiochip_setup_dev()
628 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic"); in gpiochip_setup_dev()
642 desc = gpiochip_get_desc(gc, hog->chip_hwnum); in gpiochip_machine_hog()
649 if (test_bit(FLAG_IS_HOGGED, &desc->flags)) in gpiochip_machine_hog()
652 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); in gpiochip_machine_hog()
655 __func__, gc->label, hog->chip_hwnum, rv); in gpiochip_machine_hog()
665 if (!strcmp(gc->label, hog->chip_label)) in machine_gpiochip_add()
680 dev_err(&gdev->dev, in gpiochip_setup_devs()
687 gc->gpiodev->data = data; in gpiochip_set_data()
691 * gpiochip_get_data() - get per-subdriver data for the chip
695 * The per-subdriver data for the chip.
699 return gc->gpiodev->data; in gpiochip_get_data()
705 u32 ngpios = gc->ngpio; in gpiochip_get_ngpios()
710 if (ret == -ENODATA) in gpiochip_get_ngpios()
712 * -ENODATA means that there is no property found and in gpiochip_get_ngpios()
721 gc->ngpio = ngpios; in gpiochip_get_ngpios()
724 if (gc->ngpio == 0) { in gpiochip_get_ngpios()
726 return -EINVAL; in gpiochip_get_ngpios()
729 if (gc->ngpio > FASTPATH_NGPIO) in gpiochip_get_ngpios()
731 gc->ngpio, FASTPATH_NGPIO); in gpiochip_get_ngpios()
753 return -ENOMEM; in gpiochip_add_data_with_key()
754 gdev->dev.bus = &gpio_bus_type; in gpiochip_add_data_with_key()
755 gdev->dev.parent = gc->parent; in gpiochip_add_data_with_key()
756 gdev->chip = gc; in gpiochip_add_data_with_key()
758 gc->gpiodev = gdev; in gpiochip_add_data_with_key()
765 if (gc->fwnode) in gpiochip_add_data_with_key()
766 device_set_node(&gdev->dev, gc->fwnode); in gpiochip_add_data_with_key()
767 else if (gc->parent) in gpiochip_add_data_with_key()
768 device_set_node(&gdev->dev, dev_fwnode(gc->parent)); in gpiochip_add_data_with_key()
770 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL); in gpiochip_add_data_with_key()
771 if (gdev->id < 0) { in gpiochip_add_data_with_key()
772 ret = gdev->id; in gpiochip_add_data_with_key()
776 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); in gpiochip_add_data_with_key()
780 device_initialize(&gdev->dev); in gpiochip_add_data_with_key()
781 if (gc->parent && gc->parent->driver) in gpiochip_add_data_with_key()
782 gdev->owner = gc->parent->driver->owner; in gpiochip_add_data_with_key()
783 else if (gc->owner) in gpiochip_add_data_with_key()
784 /* TODO: remove chip->owner */ in gpiochip_add_data_with_key()
785 gdev->owner = gc->owner; in gpiochip_add_data_with_key()
787 gdev->owner = THIS_MODULE; in gpiochip_add_data_with_key()
789 ret = gpiochip_get_ngpios(gc, &gdev->dev); in gpiochip_add_data_with_key()
793 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL); in gpiochip_add_data_with_key()
794 if (!gdev->descs) { in gpiochip_add_data_with_key()
795 ret = -ENOMEM; in gpiochip_add_data_with_key()
799 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL); in gpiochip_add_data_with_key()
800 if (!gdev->label) { in gpiochip_add_data_with_key()
801 ret = -ENOMEM; in gpiochip_add_data_with_key()
805 gdev->ngpio = gc->ngpio; in gpiochip_add_data_with_key()
816 base = gc->base; in gpiochip_add_data_with_key()
818 base = gpiochip_find_base(gc->ngpio); in gpiochip_add_data_with_key()
831 gc->base = base; in gpiochip_add_data_with_key()
833 dev_warn(&gdev->dev, in gpiochip_add_data_with_key()
836 gdev->base = base; in gpiochip_add_data_with_key()
845 for (i = 0; i < gc->ngpio; i++) in gpiochip_add_data_with_key()
846 gdev->descs[i].gdev = gdev; in gpiochip_add_data_with_key()
850 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); in gpiochip_add_data_with_key()
851 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); in gpiochip_add_data_with_key()
852 init_rwsem(&gdev->sem); in gpiochip_add_data_with_key()
855 INIT_LIST_HEAD(&gdev->pin_ranges); in gpiochip_add_data_with_key()
858 if (gc->names) { in gpiochip_add_data_with_key()
875 for (i = 0; i < gc->ngpio; i++) { in gpiochip_add_data_with_key()
876 struct gpio_desc *desc = &gdev->descs[i]; in gpiochip_add_data_with_key()
878 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { in gpiochip_add_data_with_key()
880 &desc->flags, !gc->get_direction(gc, i)); in gpiochip_add_data_with_key()
883 &desc->flags, !gc->direction_input); in gpiochip_add_data_with_key()
934 if (gdev->dev.release) { in gpiochip_add_data_with_key()
941 list_del(&gdev->list); in gpiochip_add_data_with_key()
944 kfree_const(gdev->label); in gpiochip_add_data_with_key()
946 kfree(gdev->descs); in gpiochip_add_data_with_key()
948 kfree(dev_name(&gdev->dev)); in gpiochip_add_data_with_key()
950 ida_free(&gpio_ida, gdev->id); in gpiochip_add_data_with_key()
955 if (ret != -EPROBE_DEFER) { in gpiochip_add_data_with_key()
957 base, base + (int)gc->ngpio - 1, in gpiochip_add_data_with_key()
958 gc->label ? : "generic", ret); in gpiochip_add_data_with_key()
965 * gpiochip_remove() - unregister a gpio_chip
972 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove()
976 down_write(&gdev->sem); in gpiochip_remove()
982 gdev->chip = NULL; in gpiochip_remove()
995 for (i = 0; i < gdev->ngpio; i++) { in gpiochip_remove()
1001 if (i != gdev->ngpio) in gpiochip_remove()
1002 dev_crit(&gdev->dev, in gpiochip_remove()
1012 up_write(&gdev->sem); in gpiochip_remove()
1031 gc = gdev->chip; in gpiochip_find()
1040 * gpio_device_find() - find a specific GPIO device
1049 * 0 if the device doesn't match and non-zero if it does. If the callback
1050 * returns non-zero, this function will return to the caller and not iterate
1057 * If the function returns non-NULL, the returned reference must be freed by
1076 if (gdev->chip && match(gdev->chip, data)) in gpio_device_find()
1088 return !strcmp(gc->label, name); in gpiochip_match_name()
1097 * gpio_device_get() - Increase the reference count of this GPIO device
1105 return to_gpio_device(get_device(&gdev->dev)); in gpio_device_get()
1110 * gpio_device_put() - Decrease the reference count of this GPIO device and
1116 put_device(&gdev->dev); in gpio_device_put()
1128 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_hw()
1130 if (!girq->init_hw) in gpiochip_irqchip_init_hw()
1133 return girq->init_hw(gc); in gpiochip_irqchip_init_hw()
1138 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_valid_mask()
1140 if (!girq->init_valid_mask) in gpiochip_irqchip_init_valid_mask()
1143 girq->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_irqchip_init_valid_mask()
1144 if (!girq->valid_mask) in gpiochip_irqchip_init_valid_mask()
1145 return -ENOMEM; in gpiochip_irqchip_init_valid_mask()
1147 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); in gpiochip_irqchip_init_valid_mask()
1154 gpiochip_free_mask(&gc->irq.valid_mask); in gpiochip_irqchip_free_valid_mask()
1163 if (likely(!gc->irq.valid_mask)) in gpiochip_irqchip_irq_valid()
1165 return test_bit(offset, gc->irq.valid_mask); in gpiochip_irqchip_irq_valid()
1172 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1182 if (is_of_node(gc->irq.fwnode)) in gpiochip_set_hierarchical_irqchip()
1193 if (is_fwnode_irqchip(gc->irq.fwnode)) { in gpiochip_set_hierarchical_irqchip()
1197 for (i = 0; i < gc->ngpio; i++) { in gpiochip_set_hierarchical_irqchip()
1201 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_set_hierarchical_irqchip()
1209 ret = girq->child_to_parent_hwirq(gc, i, in gpiochip_set_hierarchical_irqchip()
1214 chip_err(gc, "skip set-up on hwirq %d\n", in gpiochip_set_hierarchical_irqchip()
1219 fwspec.fwnode = gc->irq.fwnode; in gpiochip_set_hierarchical_irqchip()
1221 fwspec.param[0] = girq->child_offset_to_irq(gc, i); in gpiochip_set_hierarchical_irqchip()
1225 ret = irq_domain_alloc_irqs(gc->irq.domain, 1, in gpiochip_set_hierarchical_irqchip()
1247 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { in gpiochip_hierarchy_irq_domain_translate()
1252 if (is_fwnode_irqchip(fwspec->fwnode)) { in gpiochip_hierarchy_irq_domain_translate()
1261 return -EINVAL; in gpiochip_hierarchy_irq_domain_translate()
1269 struct gpio_chip *gc = d->host_data; in gpiochip_hierarchy_irq_domain_alloc()
1276 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_hierarchy_irq_domain_alloc()
1280 * The nr_irqs parameter is always one except for PCI multi-MSI in gpiochip_hierarchy_irq_domain_alloc()
1285 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); in gpiochip_hierarchy_irq_domain_alloc()
1291 ret = girq->child_to_parent_hwirq(gc, hwirq, type, in gpiochip_hierarchy_irq_domain_alloc()
1306 gc->irq.chip, in gpiochip_hierarchy_irq_domain_alloc()
1308 girq->handler, in gpiochip_hierarchy_irq_domain_alloc()
1313 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec, in gpiochip_hierarchy_irq_domain_alloc()
1320 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_hierarchy_irq_domain_alloc()
1326 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST)) in gpiochip_hierarchy_irq_domain_alloc()
1344 ops->activate = gpiochip_irq_domain_activate; in gpiochip_hierarchy_setup_domain_ops()
1345 ops->deactivate = gpiochip_irq_domain_deactivate; in gpiochip_hierarchy_setup_domain_ops()
1346 ops->alloc = gpiochip_hierarchy_irq_domain_alloc; in gpiochip_hierarchy_setup_domain_ops()
1355 if (!ops->translate) in gpiochip_hierarchy_setup_domain_ops()
1356 ops->translate = gpiochip_hierarchy_irq_domain_translate; in gpiochip_hierarchy_setup_domain_ops()
1357 if (!ops->free) in gpiochip_hierarchy_setup_domain_ops()
1358 ops->free = irq_domain_free_irqs_common; in gpiochip_hierarchy_setup_domain_ops()
1365 if (!gc->irq.child_to_parent_hwirq || in gpiochip_hierarchy_create_domain()
1366 !gc->irq.fwnode) { in gpiochip_hierarchy_create_domain()
1368 return ERR_PTR(-EINVAL); in gpiochip_hierarchy_create_domain()
1371 if (!gc->irq.child_offset_to_irq) in gpiochip_hierarchy_create_domain()
1372 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; in gpiochip_hierarchy_create_domain()
1374 if (!gc->irq.populate_parent_alloc_arg) in gpiochip_hierarchy_create_domain()
1375 gc->irq.populate_parent_alloc_arg = in gpiochip_hierarchy_create_domain()
1378 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); in gpiochip_hierarchy_create_domain()
1381 gc->irq.parent_domain, in gpiochip_hierarchy_create_domain()
1383 gc->ngpio, in gpiochip_hierarchy_create_domain()
1384 gc->irq.fwnode, in gpiochip_hierarchy_create_domain()
1385 &gc->irq.child_irq_domain_ops, in gpiochip_hierarchy_create_domain()
1389 return ERR_PTR(-ENOMEM); in gpiochip_hierarchy_create_domain()
1391 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); in gpiochip_hierarchy_create_domain()
1398 return !!gc->irq.parent_domain; in gpiochip_hierarchy_is_hierarchical()
1406 struct irq_fwspec *fwspec = &gfwspec->fwspec; in gpiochip_populate_parent_fwspec_twocell()
1408 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_twocell()
1409 fwspec->param_count = 2; in gpiochip_populate_parent_fwspec_twocell()
1410 fwspec->param[0] = parent_hwirq; in gpiochip_populate_parent_fwspec_twocell()
1411 fwspec->param[1] = parent_type; in gpiochip_populate_parent_fwspec_twocell()
1422 struct irq_fwspec *fwspec = &gfwspec->fwspec; in gpiochip_populate_parent_fwspec_fourcell()
1424 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_fourcell()
1425 fwspec->param_count = 4; in gpiochip_populate_parent_fwspec_fourcell()
1426 fwspec->param[0] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1427 fwspec->param[1] = parent_hwirq; in gpiochip_populate_parent_fwspec_fourcell()
1428 fwspec->param[2] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1429 fwspec->param[3] = parent_type; in gpiochip_populate_parent_fwspec_fourcell()
1439 return ERR_PTR(-EINVAL); in gpiochip_hierarchy_create_domain()
1450 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1461 struct gpio_chip *gc = d->host_data; in gpiochip_irq_map()
1465 return -ENXIO; in gpiochip_irq_map()
1472 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_irq_map()
1473 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler); in gpiochip_irq_map()
1475 if (gc->irq.threaded) in gpiochip_irq_map()
1479 if (gc->irq.num_parents == 1) in gpiochip_irq_map()
1480 ret = irq_set_parent(irq, gc->irq.parents[0]); in gpiochip_irq_map()
1481 else if (gc->irq.map) in gpiochip_irq_map()
1482 ret = irq_set_parent(irq, gc->irq.map[hwirq]); in gpiochip_irq_map()
1488 * No set-up of the hardware will happen if IRQ_TYPE_NONE in gpiochip_irq_map()
1491 if (gc->irq.default_type != IRQ_TYPE_NONE) in gpiochip_irq_map()
1492 irq_set_irq_type(irq, gc->irq.default_type); in gpiochip_irq_map()
1500 struct gpio_chip *gc = d->host_data; in gpiochip_irq_unmap()
1502 if (gc->irq.threaded) in gpiochip_irq_unmap()
1518 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev); in gpiochip_simple_create_domain()
1521 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first, in gpiochip_simple_create_domain()
1524 return ERR_PTR(-EINVAL); in gpiochip_simple_create_domain()
1535 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1547 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_activate()
1555 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1566 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_deactivate()
1575 struct irq_domain *domain = gc->irq.domain; in gpiochip_to_irq()
1583 if (!gc->irq.initialized) in gpiochip_to_irq()
1584 return -EPROBE_DEFER; in gpiochip_to_irq()
1588 return -ENXIO; in gpiochip_to_irq()
1594 spec.fwnode = domain->fwnode; in gpiochip_to_irq()
1596 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset); in gpiochip_to_irq()
1629 if (gc->irq.irq_mask) in gpiochip_irq_mask()
1630 gc->irq.irq_mask(d); in gpiochip_irq_mask()
1640 if (gc->irq.irq_unmask) in gpiochip_irq_unmask()
1641 gc->irq.irq_unmask(d); in gpiochip_irq_unmask()
1650 gc->irq.irq_enable(d); in gpiochip_irq_enable()
1658 gc->irq.irq_disable(d); in gpiochip_irq_disable()
1664 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_set_irq_hooks()
1666 if (irqchip->flags & IRQCHIP_IMMUTABLE) in gpiochip_set_irq_hooks()
1671 if (!irqchip->irq_request_resources && in gpiochip_set_irq_hooks()
1672 !irqchip->irq_release_resources) { in gpiochip_set_irq_hooks()
1673 irqchip->irq_request_resources = gpiochip_irq_reqres; in gpiochip_set_irq_hooks()
1674 irqchip->irq_release_resources = gpiochip_irq_relres; in gpiochip_set_irq_hooks()
1676 if (WARN_ON(gc->irq.irq_enable)) in gpiochip_set_irq_hooks()
1679 if (irqchip->irq_enable == gpiochip_irq_enable || in gpiochip_set_irq_hooks()
1680 irqchip->irq_mask == gpiochip_irq_mask) { in gpiochip_set_irq_hooks()
1690 if (irqchip->irq_disable) { in gpiochip_set_irq_hooks()
1691 gc->irq.irq_disable = irqchip->irq_disable; in gpiochip_set_irq_hooks()
1692 irqchip->irq_disable = gpiochip_irq_disable; in gpiochip_set_irq_hooks()
1694 gc->irq.irq_mask = irqchip->irq_mask; in gpiochip_set_irq_hooks()
1695 irqchip->irq_mask = gpiochip_irq_mask; in gpiochip_set_irq_hooks()
1698 if (irqchip->irq_enable) { in gpiochip_set_irq_hooks()
1699 gc->irq.irq_enable = irqchip->irq_enable; in gpiochip_set_irq_hooks()
1700 irqchip->irq_enable = gpiochip_irq_enable; in gpiochip_set_irq_hooks()
1702 gc->irq.irq_unmask = irqchip->irq_unmask; in gpiochip_set_irq_hooks()
1703 irqchip->irq_unmask = gpiochip_irq_unmask; in gpiochip_set_irq_hooks()
1712 return -EINVAL; in gpiochip_irqchip_add_allocated_domain()
1714 if (gc->to_irq) in gpiochip_irqchip_add_allocated_domain()
1717 gc->to_irq = gpiochip_to_irq; in gpiochip_irqchip_add_allocated_domain()
1718 gc->irq.domain = domain; in gpiochip_irqchip_add_allocated_domain()
1719 gc->irq.domain_is_allocated_externally = allocated_externally; in gpiochip_irqchip_add_allocated_domain()
1723 * gc->irq.initialized before adding irqdomain. in gpiochip_irqchip_add_allocated_domain()
1727 gc->irq.initialized = true; in gpiochip_irqchip_add_allocated_domain()
1733 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1742 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev); in gpiochip_add_irqchip()
1743 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_add_irqchip()
1752 if (gc->irq.parent_handler && gc->can_sleep) { in gpiochip_add_irqchip()
1754 return -EINVAL; in gpiochip_add_irqchip()
1757 type = gc->irq.default_type; in gpiochip_add_irqchip()
1768 gc->irq.default_type = type; in gpiochip_add_irqchip()
1769 gc->irq.lock_key = lock_key; in gpiochip_add_irqchip()
1770 gc->irq.request_key = request_key; in gpiochip_add_irqchip()
1781 if (gc->irq.parent_handler) { in gpiochip_add_irqchip()
1782 for (i = 0; i < gc->irq.num_parents; i++) { in gpiochip_add_irqchip()
1785 if (gc->irq.per_parent_data) in gpiochip_add_irqchip()
1786 data = gc->irq.parent_handler_data_array[i]; in gpiochip_add_irqchip()
1788 data = gc->irq.parent_handler_data ?: gc; in gpiochip_add_irqchip()
1795 irq_set_chained_handler_and_data(gc->irq.parents[i], in gpiochip_add_irqchip()
1796 gc->irq.parent_handler, in gpiochip_add_irqchip()
1813 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1820 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_irqchip_remove()
1825 if (irqchip && gc->irq.parent_handler) { in gpiochip_irqchip_remove()
1826 struct gpio_irq_chip *irq = &gc->irq; in gpiochip_irqchip_remove()
1829 for (i = 0; i < irq->num_parents; i++) in gpiochip_irqchip_remove()
1830 irq_set_chained_handler_and_data(irq->parents[i], in gpiochip_irqchip_remove()
1835 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) { in gpiochip_irqchip_remove()
1838 for (offset = 0; offset < gc->ngpio; offset++) { in gpiochip_irqchip_remove()
1842 irq = irq_find_mapping(gc->irq.domain, offset); in gpiochip_irqchip_remove()
1846 irq_domain_remove(gc->irq.domain); in gpiochip_irqchip_remove()
1849 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) { in gpiochip_irqchip_remove()
1850 if (irqchip->irq_request_resources == gpiochip_irq_reqres) { in gpiochip_irqchip_remove()
1851 irqchip->irq_request_resources = NULL; in gpiochip_irqchip_remove()
1852 irqchip->irq_release_resources = NULL; in gpiochip_irqchip_remove()
1854 if (irqchip->irq_enable == gpiochip_irq_enable) { in gpiochip_irqchip_remove()
1855 irqchip->irq_enable = gc->irq.irq_enable; in gpiochip_irqchip_remove()
1856 irqchip->irq_disable = gc->irq.irq_disable; in gpiochip_irqchip_remove()
1859 gc->irq.irq_enable = NULL; in gpiochip_irqchip_remove()
1860 gc->irq.irq_disable = NULL; in gpiochip_irqchip_remove()
1861 gc->irq.chip = NULL; in gpiochip_irqchip_remove()
1867 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1905 * gpiochip_generic_request() - request the gpio function for a pin
1912 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_request()
1916 return pinctrl_gpio_request(gc->gpiodev->base + offset); in gpiochip_generic_request()
1921 * gpiochip_generic_free() - free the gpio function from a pin
1928 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_free()
1932 pinctrl_gpio_free(gc->gpiodev->base + offset); in gpiochip_generic_free()
1937 * gpiochip_generic_config() - apply configuration for a pin
1945 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config); in gpiochip_generic_config()
1952 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1958 * Calling this function directly from a DeviceTree-supported
1961 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1968 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pingroup_range()
1974 return -ENOMEM; in gpiochip_add_pingroup_range()
1978 pin_range->range.id = gpio_offset; in gpiochip_add_pingroup_range()
1979 pin_range->range.gc = gc; in gpiochip_add_pingroup_range()
1980 pin_range->range.name = gc->label; in gpiochip_add_pingroup_range()
1981 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pingroup_range()
1982 pin_range->pctldev = pctldev; in gpiochip_add_pingroup_range()
1985 &pin_range->range.pins, in gpiochip_add_pingroup_range()
1986 &pin_range->range.npins); in gpiochip_add_pingroup_range()
1992 pinctrl_add_gpio_range(pctldev, &pin_range->range); in gpiochip_add_pingroup_range()
1994 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n", in gpiochip_add_pingroup_range()
1995 gpio_offset, gpio_offset + pin_range->range.npins - 1, in gpiochip_add_pingroup_range()
1998 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pingroup_range()
2005 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2014 * 0 on success, or a negative error-code on failure.
2016 * Calling this function directly from a DeviceTree-supported
2019 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2026 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pin_range()
2032 return -ENOMEM; in gpiochip_add_pin_range()
2036 pin_range->range.id = gpio_offset; in gpiochip_add_pin_range()
2037 pin_range->range.gc = gc; in gpiochip_add_pin_range()
2038 pin_range->range.name = gc->label; in gpiochip_add_pin_range()
2039 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pin_range()
2040 pin_range->range.pin_base = pin_offset; in gpiochip_add_pin_range()
2041 pin_range->range.npins = npins; in gpiochip_add_pin_range()
2042 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, in gpiochip_add_pin_range()
2043 &pin_range->range); in gpiochip_add_pin_range()
2044 if (IS_ERR(pin_range->pctldev)) { in gpiochip_add_pin_range()
2045 ret = PTR_ERR(pin_range->pctldev); in gpiochip_add_pin_range()
2050 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n", in gpiochip_add_pin_range()
2051 gpio_offset, gpio_offset + npins - 1, in gpiochip_add_pin_range()
2053 pin_offset, pin_offset + npins - 1); in gpiochip_add_pin_range()
2055 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pin_range()
2062 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2068 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove_pin_ranges()
2070 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { in gpiochip_remove_pin_ranges()
2071 list_del(&pin_range->node); in gpiochip_remove_pin_ranges()
2072 pinctrl_remove_gpio_range(pin_range->pctldev, in gpiochip_remove_pin_ranges()
2073 &pin_range->range); in gpiochip_remove_pin_ranges()
2087 struct gpio_chip *gc = desc->gdev->chip; in gpiod_request_commit()
2095 return -ENOMEM; in gpiod_request_commit()
2101 * before IRQs are enabled, for non-sleeping (SOC) GPIOs. in gpiod_request_commit()
2104 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { in gpiod_request_commit()
2107 ret = -EBUSY; in gpiod_request_commit()
2111 if (gc->request) { in gpiod_request_commit()
2112 /* gc->request may sleep */ in gpiod_request_commit()
2116 ret = gc->request(gc, offset); in gpiod_request_commit()
2118 ret = -EINVAL; in gpiod_request_commit()
2123 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_request_commit()
2127 if (gc->get_direction) { in gpiod_request_commit()
2128 /* gc->get_direction may sleep */ in gpiod_request_commit()
2156 if (!desc->gdev) { in validate_desc()
2158 return -EINVAL; in validate_desc()
2160 if (!desc->gdev->chip) { in validate_desc()
2161 dev_warn(&desc->gdev->dev, in validate_desc()
2182 int ret = -EPROBE_DEFER; in gpiod_request()
2186 if (try_module_get(desc->gdev->owner)) { in gpiod_request()
2189 module_put(desc->gdev->owner); in gpiod_request()
2191 gpio_device_get(desc->gdev); in gpiod_request()
2210 gc = desc->gdev->chip; in gpiod_free_commit()
2211 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) { in gpiod_free_commit()
2212 if (gc->free) { in gpiod_free_commit()
2214 might_sleep_if(gc->can_sleep); in gpiod_free_commit()
2215 gc->free(gc, gpio_chip_hwgpio(desc)); in gpiod_free_commit()
2218 kfree_const(desc->label); in gpiod_free_commit()
2220 clear_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_free_commit()
2221 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_free_commit()
2222 clear_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_free_commit()
2223 clear_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_free_commit()
2224 clear_bit(FLAG_PULL_UP, &desc->flags); in gpiod_free_commit()
2225 clear_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_free_commit()
2226 clear_bit(FLAG_BIAS_DISABLE, &desc->flags); in gpiod_free_commit()
2227 clear_bit(FLAG_EDGE_RISING, &desc->flags); in gpiod_free_commit()
2228 clear_bit(FLAG_EDGE_FALLING, &desc->flags); in gpiod_free_commit()
2229 clear_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_free_commit()
2231 desc->hog = NULL; in gpiod_free_commit()
2234 WRITE_ONCE(desc->debounce_period_us, 0); in gpiod_free_commit()
2248 * We must not use VALIDATE_DESC_VOID() as the underlying gdev->chip in gpiod_free()
2257 module_put(desc->gdev->owner); in gpiod_free()
2258 gpio_device_put(desc->gdev); in gpiod_free()
2262 * gpiochip_is_requested - return string iff signal was requested
2264 * @offset: of signal within controller's 0..(ngpio - 1) range
2268 * passed it is a meaningless, non-NULL constant.
2282 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) in gpiochip_is_requested()
2284 return desc->label; in gpiochip_is_requested()
2289 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2306 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2339 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2357 * using the open-drain emulation idiom) so these are natural places
2365 if (!gc->set_config) in gpio_do_set_config()
2366 return -ENOTSUPP; in gpio_do_set_config()
2368 return gc->set_config(gc, offset, config); in gpio_do_set_config()
2375 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_config_with_argument()
2386 struct device *dev = &desc->gdev->dev; in gpio_set_config_with_argument_optional()
2391 if (ret != -ENOTSUPP) in gpio_set_config_with_argument_optional()
2415 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) in gpio_set_bias()
2417 else if (test_bit(FLAG_PULL_UP, &desc->flags)) in gpio_set_bias()
2419 else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) in gpio_set_bias()
2439 * gpio_set_debounce_timeout() - Set debounce timeout
2456 * gpiod_direction_input - set the GPIO direction to input
2470 gc = desc->gdev->chip; in gpiod_direction_input()
2474 * the chip is output-only, but you can't specify .direction_input() in gpiod_direction_input()
2477 if (!gc->get && gc->direction_input) { in gpiod_direction_input()
2481 return -EIO; in gpiod_direction_input()
2486 * just call it. Else we are some input-only chip so try to check the in gpiod_direction_input()
2490 if (gc->direction_input) { in gpiod_direction_input()
2491 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc)); in gpiod_direction_input()
2492 } else if (gc->get_direction && in gpiod_direction_input()
2493 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) { in gpiod_direction_input()
2497 return -EIO; in gpiod_direction_input()
2500 clear_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_input()
2512 struct gpio_chip *gc = desc->gdev->chip; in gpiod_direction_output_raw_commit()
2518 * output-only, but if there is then not even a .set() operation it in gpiod_direction_output_raw_commit()
2521 if (!gc->set && !gc->direction_output) { in gpiod_direction_output_raw_commit()
2525 return -EIO; in gpiod_direction_output_raw_commit()
2528 if (gc->direction_output) { in gpiod_direction_output_raw_commit()
2529 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); in gpiod_direction_output_raw_commit()
2532 if (gc->get_direction && in gpiod_direction_output_raw_commit()
2533 gc->get_direction(gc, gpio_chip_hwgpio(desc))) { in gpiod_direction_output_raw_commit()
2537 return -EIO; in gpiod_direction_output_raw_commit()
2541 * output-only chip, so just drive the output as desired. in gpiod_direction_output_raw_commit()
2543 gc->set(gc, gpio_chip_hwgpio(desc), val); in gpiod_direction_output_raw_commit()
2547 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output_raw_commit()
2554 * gpiod_direction_output_raw - set the GPIO direction to output
2572 * gpiod_direction_output - set the GPIO direction to output
2588 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_direction_output()
2594 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && in gpiod_direction_output()
2595 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { in gpiod_direction_output()
2599 return -EIO; in gpiod_direction_output()
2602 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiod_direction_output()
2612 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { in gpiod_direction_output()
2633 * When emulating open-source or open-drain functionalities by not in gpiod_direction_output()
2639 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output()
2645 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2659 gc = desc->gdev->chip; in gpiod_enable_hw_timestamp_ns()
2660 if (!gc->en_hw_timestamp) { in gpiod_enable_hw_timestamp_ns()
2662 return -ENOTSUPP; in gpiod_enable_hw_timestamp_ns()
2665 ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags); in gpiod_enable_hw_timestamp_ns()
2674 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2688 gc = desc->gdev->chip; in gpiod_disable_hw_timestamp_ns()
2689 if (!gc->dis_hw_timestamp) { in gpiod_disable_hw_timestamp_ns()
2691 return -ENOTSUPP; in gpiod_disable_hw_timestamp_ns()
2694 ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags); in gpiod_disable_hw_timestamp_ns()
2703 * gpiod_set_config - sets @config for a GPIO
2705 * @config: Same packed config format as generic pinconf
2708 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2716 gc = desc->gdev->chip; in gpiod_set_config()
2723 * gpiod_set_debounce - sets @debounce time for a GPIO
2728 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2741 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2755 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); in gpiod_set_transitory()
2765 * gpiod_is_active_low - test whether a GPIO is active-low or not
2768 * Returns 1 if the GPIO is active-low, 0 otherwise.
2773 return test_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_is_active_low()
2778 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2784 change_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_toggle_active_low()
2790 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO; in gpio_chip_get_value()
2803 *------------------------------------------------------------------------
2805 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2820 gc = desc->gdev->chip; in gpiod_get_raw_value_commit()
2830 if (gc->get_multiple) in gpio_chip_get_multiple()
2831 return gc->get_multiple(gc, mask, bits); in gpio_chip_get_multiple()
2832 if (gc->get) { in gpio_chip_get_multiple()
2835 for_each_set_bit(i, mask, gc->ngpio) { in gpio_chip_get_multiple()
2836 value = gc->get(gc, i); in gpio_chip_get_multiple()
2843 return -EIO; in gpio_chip_get_multiple()
2859 if (array_info && array_info->desc == desc_array && in gpiod_get_array_value_complex()
2860 array_size <= array_info->size && in gpiod_get_array_value_complex()
2861 (void *)array_info == desc_array + array_info->size) { in gpiod_get_array_value_complex()
2863 WARN_ON(array_info->chip->can_sleep); in gpiod_get_array_value_complex()
2865 ret = gpio_chip_get_multiple(array_info->chip, in gpiod_get_array_value_complex()
2866 array_info->get_mask, in gpiod_get_array_value_complex()
2871 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_get_array_value_complex()
2873 array_info->invert_mask, array_size); in gpiod_get_array_value_complex()
2875 i = find_first_zero_bit(array_info->get_mask, array_size); in gpiod_get_array_value_complex()
2883 struct gpio_chip *gc = desc_array[i]->gdev->chip; in gpiod_get_array_value_complex()
2889 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_get_array_value_complex()
2895 mask = bitmap_alloc(gc->ngpio, flags); in gpiod_get_array_value_complex()
2897 return -ENOMEM; in gpiod_get_array_value_complex()
2899 bits = bitmap_alloc(gc->ngpio, flags); in gpiod_get_array_value_complex()
2902 return -ENOMEM; in gpiod_get_array_value_complex()
2906 bitmap_zero(mask, gc->ngpio); in gpiod_get_array_value_complex()
2909 WARN_ON(gc->can_sleep); in gpiod_get_array_value_complex()
2921 i = find_next_zero_bit(array_info->get_mask, in gpiod_get_array_value_complex()
2924 (desc_array[i]->gdev->chip == gc)); in gpiod_get_array_value_complex()
2940 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_array_value_complex()
2947 j = find_next_zero_bit(array_info->get_mask, i, in gpiod_get_array_value_complex()
2960 * gpiod_get_raw_value() - return a gpio's raw value
2973 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_raw_value()
2979 * gpiod_get_value() - return a gpio's value
2994 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_get_value()
3000 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value()
3008 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3027 return -EINVAL; in gpiod_get_raw_array_value()
3035 * gpiod_get_array_value() - read values from an array of GPIOs
3053 return -EINVAL; in gpiod_get_array_value()
3061 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3063 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3068 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_open_drain_value_commit()
3072 ret = gc->direction_input(gc, offset); in gpio_set_open_drain_value_commit()
3074 ret = gc->direction_output(gc, offset, 0); in gpio_set_open_drain_value_commit()
3076 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_drain_value_commit()
3086 * _gpio_set_open_source_value() - Set the open source gpio's value.
3088 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3093 struct gpio_chip *gc = desc->gdev->chip; in gpio_set_open_source_value_commit()
3097 ret = gc->direction_output(gc, offset, 1); in gpio_set_open_source_value_commit()
3099 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_source_value_commit()
3101 ret = gc->direction_input(gc, offset); in gpio_set_open_source_value_commit()
3114 gc = desc->gdev->chip; in gpiod_set_raw_value_commit()
3116 gc->set(gc, gpio_chip_hwgpio(desc), value); in gpiod_set_raw_value_commit()
3132 if (gc->set_multiple) { in gpio_chip_set_multiple()
3133 gc->set_multiple(gc, mask, bits); in gpio_chip_set_multiple()
3138 for_each_set_bit(i, mask, gc->ngpio) in gpio_chip_set_multiple()
3139 gc->set(gc, i, test_bit(i, bits)); in gpio_chip_set_multiple()
3156 if (array_info && array_info->desc == desc_array && in gpiod_set_array_value_complex()
3157 array_size <= array_info->size && in gpiod_set_array_value_complex()
3158 (void *)array_info == desc_array + array_info->size) { in gpiod_set_array_value_complex()
3160 WARN_ON(array_info->chip->can_sleep); in gpiod_set_array_value_complex()
3162 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_set_array_value_complex()
3164 array_info->invert_mask, array_size); in gpiod_set_array_value_complex()
3166 gpio_chip_set_multiple(array_info->chip, array_info->set_mask, in gpiod_set_array_value_complex()
3169 i = find_first_zero_bit(array_info->set_mask, array_size); in gpiod_set_array_value_complex()
3177 struct gpio_chip *gc = desc_array[i]->gdev->chip; in gpiod_set_array_value_complex()
3183 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_set_array_value_complex()
3189 mask = bitmap_alloc(gc->ngpio, flags); in gpiod_set_array_value_complex()
3191 return -ENOMEM; in gpiod_set_array_value_complex()
3193 bits = bitmap_alloc(gc->ngpio, flags); in gpiod_set_array_value_complex()
3196 return -ENOMEM; in gpiod_set_array_value_complex()
3200 bitmap_zero(mask, gc->ngpio); in gpiod_set_array_value_complex()
3203 WARN_ON(gc->can_sleep); in gpiod_set_array_value_complex()
3216 test_bit(i, array_info->invert_mask)) && in gpiod_set_array_value_complex()
3217 test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_array_value_complex()
3224 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3226 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3236 i = find_next_zero_bit(array_info->set_mask, in gpiod_set_array_value_complex()
3239 (desc_array[i]->gdev->chip == gc)); in gpiod_set_array_value_complex()
3253 * gpiod_set_raw_value() - assign a gpio's raw value
3267 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_set_raw_value()
3273 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3283 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_value_nocheck()
3285 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) in gpiod_set_value_nocheck()
3287 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) in gpiod_set_value_nocheck()
3294 * gpiod_set_value() - assign a gpio's value
3308 WARN_ON(desc->gdev->chip->can_sleep); in gpiod_set_value()
3314 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3332 return -EINVAL; in gpiod_set_raw_array_value()
3339 * gpiod_set_array_value() - assign values to an array of GPIOs
3357 return -EINVAL; in gpiod_set_array_value()
3365 * gpiod_cansleep() - report whether gpio value access may sleep
3372 return desc->gdev->chip->can_sleep; in gpiod_cansleep()
3377 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3387 return -ENOMEM; in gpiod_set_consumer_name()
3390 kfree_const(desc->label); in gpiod_set_consumer_name()
3398 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3414 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) in gpiod_to_irq()
3415 return -EINVAL; in gpiod_to_irq()
3417 gc = desc->gdev->chip; in gpiod_to_irq()
3419 if (gc->to_irq) { in gpiod_to_irq()
3420 int retirq = gc->to_irq(gc, offset); in gpiod_to_irq()
3424 return -ENXIO; in gpiod_to_irq()
3429 if (gc->irq.chip) { in gpiod_to_irq()
3435 return -EPROBE_DEFER; in gpiod_to_irq()
3438 return -ENXIO; in gpiod_to_irq()
3443 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3462 if (!gc->can_sleep && gc->get_direction) { in gpiochip_lock_as_irq()
3473 if (test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_lock_as_irq()
3474 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiochip_lock_as_irq()
3478 return -EIO; in gpiochip_lock_as_irq()
3481 set_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_lock_as_irq()
3482 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_lock_as_irq()
3489 if (!desc->label) in gpiochip_lock_as_irq()
3497 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3512 clear_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_unlock_as_irq()
3513 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_unlock_as_irq()
3516 if (desc->label && !strcmp(desc->label, "interrupt")) in gpiochip_unlock_as_irq()
3526 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) in gpiochip_disable_irq()
3527 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_disable_irq()
3536 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { in gpiochip_enable_irq()
3541 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_enable_irq()
3542 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); in gpiochip_enable_irq()
3543 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_enable_irq()
3550 if (offset >= gc->ngpio) in gpiochip_line_is_irq()
3553 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_irq()
3561 if (!try_module_get(gc->gpiodev->owner)) in gpiochip_reqres_irq()
3562 return -ENODEV; in gpiochip_reqres_irq()
3567 module_put(gc->gpiodev->owner); in gpiochip_reqres_irq()
3577 module_put(gc->gpiodev->owner); in gpiochip_relres_irq()
3583 if (offset >= gc->ngpio) in gpiochip_line_is_open_drain()
3586 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_drain()
3592 if (offset >= gc->ngpio) in gpiochip_line_is_open_source()
3595 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_source()
3601 if (offset >= gc->ngpio) in gpiochip_line_is_persistent()
3604 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_persistent()
3609 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3626 * gpiod_get_value_cansleep() - return a gpio's value
3644 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value_cansleep()
3652 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3671 return -EINVAL; in gpiod_get_raw_array_value_cansleep()
3679 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3697 return -EINVAL; in gpiod_get_array_value_cansleep()
3705 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3723 * gpiod_set_value_cansleep() - assign a gpio's value
3741 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3759 return -EINVAL; in gpiod_set_raw_array_value_cansleep()
3766 * gpiod_add_lookup_tables() - register GPIO device consumers
3777 list_add_tail(&tables[i]->list, &gpio_lookup_list); in gpiod_add_lookup_tables()
3783 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3801 return -EINVAL; in gpiod_set_array_value_cansleep()
3810 blocking_notifier_call_chain(&desc->gdev->line_state_notifier, in gpiod_line_state_notify()
3815 * gpiod_add_lookup_table() - register GPIO device consumers
3825 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3836 list_del(&table->list); in gpiod_remove_lookup_table()
3843 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3853 for (hog = &hogs[0]; hog->chip_label; hog++) { in gpiod_add_hogs()
3854 list_add_tail(&hog->list, &gpio_machine_hogs); in gpiod_add_hogs()
3860 gc = find_chip_by_name(hog->chip_label); in gpiod_add_hogs()
3874 for (hog = &hogs[0]; hog->chip_label; hog++) in gpiod_remove_hogs()
3875 list_del(&hog->list); in gpiod_remove_hogs()
3888 if (table->dev_id && dev_id) { in gpiod_find_lookup_table()
3893 if (!strcmp(table->dev_id, dev_id)) in gpiod_find_lookup_table()
3900 if (dev_id == table->dev_id) in gpiod_find_lookup_table()
3914 struct gpio_desc *desc = ERR_PTR(-ENOENT); in gpiod_find()
3922 for (p = &table->table[0]; p->key; p++) { in gpiod_find()
3926 if (p->idx != idx) in gpiod_find()
3930 if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) in gpiod_find()
3933 if (p->chip_hwnum == U16_MAX) { in gpiod_find()
3934 desc = gpio_name_to_desc(p->key); in gpiod_find()
3936 *flags = p->flags; in gpiod_find()
3941 p->key); in gpiod_find()
3942 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
3945 gc = find_chip_by_name(p->key); in gpiod_find()
3950 * p->key should exist, assume it may in gpiod_find()
3956 p->key); in gpiod_find()
3957 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
3960 if (gc->ngpio <= p->chip_hwnum) { in gpiod_find()
3963 idx, p->chip_hwnum, gc->ngpio - 1, in gpiod_find()
3964 gc->label); in gpiod_find()
3965 return ERR_PTR(-EINVAL); in gpiod_find()
3968 desc = gpiochip_get_desc(gc, p->chip_hwnum); in gpiod_find()
3969 *flags = p->flags; in gpiod_find()
3985 return -ENOENT; in platform_gpio_count()
3987 for (p = &table->table[0]; p->key; p++) { in platform_gpio_count()
3988 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || in platform_gpio_count()
3989 (!con_id && !p->con_id)) in platform_gpio_count()
3993 return -ENOENT; in platform_gpio_count()
4005 struct gpio_desc *desc = ERR_PTR(-ENOENT); in gpiod_find_by_fwnode()
4058 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) in gpiod_find_and_request()
4087 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4118 * gpiod_count - return the number of GPIOs associated with a device / function
4119 * or -ENOENT if no GPIO has been assigned to the requested function
4120 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4126 int count = -ENOENT; in gpiod_count()
4143 * gpiod_get - obtain a GPIO for a given GPIO function
4144 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4149 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4160 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4161 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4179 * gpiod_configure_flags - helper function to configure a given GPIO
4182 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4184 * @dflags: gpiod_flags - optional GPIO initialization flags
4186 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4196 set_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_configure_flags()
4199 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
4207 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
4213 set_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_configure_flags()
4219 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n"); in gpiod_configure_flags()
4220 return -EINVAL; in gpiod_configure_flags()
4224 set_bit(FLAG_PULL_UP, &desc->flags); in gpiod_configure_flags()
4226 set_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_configure_flags()
4228 set_bit(FLAG_BIAS_DISABLE, &desc->flags); in gpiod_configure_flags()
4251 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4252 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4260 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4278 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4280 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4305 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4308 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4310 * @dflags: gpiod_flags - optional GPIO initialization flags
4328 name, gc->label, hwnum, ret); in gpiod_hog()
4333 set_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_hog()
4344 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4356 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4357 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4363 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4385 return ERR_PTR(-ENOMEM); in gpiod_get_array()
4387 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) { in gpiod_get_array()
4388 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); in gpiod_get_array()
4394 descs->desc[descs->ndescs] = desc; in gpiod_get_array()
4401 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { in gpiod_get_array()
4404 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ? in gpiod_get_array()
4405 gc->ngpio : count); in gpiod_get_array()
4412 return ERR_PTR(-ENOMEM); in gpiod_get_array()
4418 array_info->get_mask = array_info->invert_mask + in gpiod_get_array()
4420 array_info->set_mask = array_info->get_mask + in gpiod_get_array()
4423 array_info->desc = descs->desc; in gpiod_get_array()
4424 array_info->size = count; in gpiod_get_array()
4425 array_info->chip = gc; in gpiod_get_array()
4426 bitmap_set(array_info->get_mask, descs->ndescs, in gpiod_get_array()
4427 count - descs->ndescs); in gpiod_get_array()
4428 bitmap_set(array_info->set_mask, descs->ndescs, in gpiod_get_array()
4429 count - descs->ndescs); in gpiod_get_array()
4430 descs->info = array_info; in gpiod_get_array()
4438 if (array_info->chip != gc) { in gpiod_get_array()
4439 __clear_bit(descs->ndescs, array_info->get_mask); in gpiod_get_array()
4440 __clear_bit(descs->ndescs, array_info->set_mask); in gpiod_get_array()
4446 else if (gpio_chip_hwgpio(desc) != descs->ndescs) { in gpiod_get_array()
4452 if (bitmap_full(array_info->get_mask, descs->ndescs)) { in gpiod_get_array()
4455 __clear_bit(descs->ndescs, in gpiod_get_array()
4456 array_info->get_mask); in gpiod_get_array()
4457 __clear_bit(descs->ndescs, in gpiod_get_array()
4458 array_info->set_mask); in gpiod_get_array()
4462 if (gpiochip_line_is_open_drain(gc, descs->ndescs) || in gpiod_get_array()
4463 gpiochip_line_is_open_source(gc, descs->ndescs)) in gpiod_get_array()
4464 __clear_bit(descs->ndescs, in gpiod_get_array()
4465 array_info->set_mask); in gpiod_get_array()
4468 __set_bit(descs->ndescs, in gpiod_get_array()
4469 array_info->invert_mask); in gpiod_get_array()
4475 array_info->chip->label, array_info->size, in gpiod_get_array()
4476 *array_info->get_mask, *array_info->set_mask, in gpiod_get_array()
4477 *array_info->invert_mask); in gpiod_get_array()
4483 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4485 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4507 * gpiod_put - dispose of a GPIO descriptor
4520 * gpiod_put_array - dispose of multiple GPIO descriptors
4527 for (i = 0; i < descs->ndescs; i++) in gpiod_put_array()
4528 gpiod_put(descs->desc[i]); in gpiod_put_array()
4598 struct gpio_chip *gc = gdev->chip; in gpiolib_dbg_show()
4600 unsigned gpio = gdev->base; in gpiolib_dbg_show()
4607 if (test_bit(FLAG_REQUESTED, &desc->flags)) { in gpiolib_dbg_show()
4609 is_out = test_bit(FLAG_IS_OUT, &desc->flags); in gpiolib_dbg_show()
4611 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiolib_dbg_show()
4612 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiolib_dbg_show()
4613 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n", in gpiolib_dbg_show()
4614 gpio, desc->name ?: "", desc->label, in gpiolib_dbg_show()
4619 } else if (desc->name) { in gpiolib_dbg_show()
4620 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name); in gpiolib_dbg_show()
4633 s->private = ""; in gpiolib_seq_start()
4637 if (index-- == 0) { in gpiolib_seq_start()
4653 if (list_is_last(&gdev->list, &gpio_devices)) in gpiolib_seq_next()
4656 ret = list_first_entry(&gdev->list, struct gpio_device, list); in gpiolib_seq_next()
4659 s->private = "\n"; in gpiolib_seq_next()
4672 struct gpio_chip *gc = gdev->chip; in gpiolib_seq_show()
4676 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, in gpiolib_seq_show()
4677 dev_name(&gdev->dev)); in gpiolib_seq_show()
4681 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, in gpiolib_seq_show()
4682 dev_name(&gdev->dev), in gpiolib_seq_show()
4683 gdev->base, gdev->base + gdev->ngpio - 1); in gpiolib_seq_show()
4684 parent = gc->parent; in gpiolib_seq_show()
4687 parent->bus ? parent->bus->name : "no-bus", in gpiolib_seq_show()
4689 if (gc->label) in gpiolib_seq_show()
4690 seq_printf(s, ", %s", gc->label); in gpiolib_seq_show()
4691 if (gc->can_sleep) in gpiolib_seq_show()
4695 if (gc->dbg_show) in gpiolib_seq_show()
4696 gc->dbg_show(s, gc); in gpiolib_seq_show()