Lines Matching +full:lock +full:- +full:detect +full:- +full:function +full:- +full:integer +full:- +full:n +full:- +full:enable
1 // SPDX-License-Identifier: GPL-2.0
37 #include "gpiolib-acpi.h"
38 #include "gpiolib-cdev.h"
39 #include "gpiolib-of.h"
40 #include "gpiolib-swnode.h"
41 #include "gpiolib-sysfs.h"
49 * The GPIO programming interface allows for inlining speed-critical
50 * get/set operations for common cases, so that access to SOC-integrated
54 /* Device and char device-related information */
67 if (fwnode && fwnode->dev != dev) in gpio_bus_match()
88 /* Ensures coherence during read-only accesses to the list of GPIO devices. */
112 flags = READ_ONCE(desc->flags); in gpiod_get_label()
114 label = srcu_dereference_check(desc->label, &desc->gdev->desc_srcu, in gpiod_get_label()
115 srcu_read_lock_held(&desc->gdev->desc_srcu)); in gpiod_get_label()
118 return label ? label->str : "interrupt"; in gpiod_get_label()
123 return label ? label->str : NULL; in gpiod_get_label()
139 return -ENOMEM; in desc_set_label()
141 strcpy(new->str, label); in desc_set_label()
144 old = rcu_replace_pointer(desc->label, new, 1); in desc_set_label()
146 call_srcu(&desc->gdev->desc_srcu, &old->rh, desc_free_label); in desc_set_label()
152 * gpio_to_desc - Convert a GPIO number to its descriptor
166 if (gdev->base <= gpio && in gpio_to_desc()
167 gdev->base + gdev->ngpio > gpio) in gpio_to_desc()
168 return &gdev->descs[gpio - gdev->base]; in gpio_to_desc()
176 /* This function is deprecated and will be removed soon, don't use. */
180 return gpio_device_get_desc(gc->gpiodev, hwnum); in gpiochip_get_desc()
184 * gpio_device_get_desc() - get the GPIO descriptor corresponding to the given
197 * by this function.
202 if (hwnum >= gdev->ngpio) in gpio_device_get_desc()
203 return ERR_PTR(-EINVAL); in gpio_device_get_desc()
205 return &gdev->descs[array_index_nospec(hwnum, gdev->ngpio)]; in gpio_device_get_desc()
210 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
221 return desc->gdev->base + (desc - &desc->gdev->descs[0]); in desc_to_gpio()
227 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
231 * This function is unsafe and should not be used. Using the chip address
232 * without taking the SRCU read lock may result in dereferencing a dangling
243 return gpio_device_get_chip(desc->gdev); in gpiod_to_chip()
248 * gpiod_to_gpio_device() - Return the GPIO device to which this descriptor
264 return desc->gdev; in gpiod_to_gpio_device()
269 * gpiod_is_equal() - Check if two GPIO descriptors refer to the same pin.
283 * gpio_device_get_base() - Get the base GPIO number allocated by this device
291 return gdev->base; in gpio_device_get_base()
296 * gpio_device_get_label() - Get the label of this GPIO device
305 return gdev->label; in gpio_device_get_label()
310 * gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device
317 * Until we can get rid of all non-driver users of struct gpio_chip, we must
319 * is *NOT* safe as the GPIO API is considered to be hot-unpluggable and the
320 * chip can dissapear at any moment (unlike reference-counted struct
327 return rcu_dereference_check(gdev->chip, 1); in gpio_device_get_chip()
340 if (gdev->base >= base + ngpio) in gpiochip_find_base_unlocked()
343 base = gdev->base + gdev->ngpio; in gpiochip_find_base_unlocked()
346 if (base > GPIO_DYNAMIC_MAX - ngpio) in gpiochip_find_base_unlocked()
350 if (base <= GPIO_DYNAMIC_MAX - ngpio) { in gpiochip_find_base_unlocked()
351 pr_debug("%s: found new base at %d\n", __func__, base); in gpiochip_find_base_unlocked()
354 pr_err("%s: cannot find free range\n", __func__); in gpiochip_find_base_unlocked()
355 return -ENOSPC; in gpiochip_find_base_unlocked()
361 * function taking a descriptor, so we need to use a preprocessor
371 pr_warn("%s: invalid GPIO (errorpointer: %pe)\n", func, desc); in validate_desc()
394 lockdep_assert_held(&gc->gpiodev->srcu); in gpiochip_get_direction()
396 if (WARN_ON(!gc->get_direction)) in gpiochip_get_direction()
397 return -EOPNOTSUPP; in gpiochip_get_direction()
399 ret = gc->get_direction(gc, offset); in gpiochip_get_direction()
404 ret = -EBADE; in gpiochip_get_direction()
410 * gpiod_get_direction - return the current direction of a GPIO
416 * This function may sleep if gpiod_cansleep() is true.
426 return -EINVAL; in gpiod_get_direction()
430 return -ENODEV; in gpiod_get_direction()
433 flags = READ_ONCE(desc->flags); in gpiod_get_direction()
443 if (!guard.gc->get_direction) in gpiod_get_direction()
444 return -ENOTSUPP; in gpiod_get_direction()
458 WRITE_ONCE(desc->flags, flags); in gpiod_get_direction()
466 * by range(means [base, base + ngpio - 1]) order.
469 * -EBUSY if the new chip overlaps with some other chip's integer space.
479 list_add_tail_rcu(&gdev->list, &gpio_devices); in gpiodev_add_to_list_unlocked()
484 if (gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list_unlocked()
486 list_add_rcu(&gdev->list, &gpio_devices); in gpiodev_add_to_list_unlocked()
491 if (prev->base + prev->ngpio <= gdev->base) { in gpiodev_add_to_list_unlocked()
493 list_add_tail_rcu(&gdev->list, &gpio_devices); in gpiodev_add_to_list_unlocked()
499 if (&next->list == &gpio_devices) in gpiodev_add_to_list_unlocked()
503 if (prev->base + prev->ngpio <= gdev->base in gpiodev_add_to_list_unlocked()
504 && gdev->base + gdev->ngpio <= next->base) { in gpiodev_add_to_list_unlocked()
505 list_add_rcu(&gdev->list, &prev->list); in gpiodev_add_to_list_unlocked()
512 return -EBUSY; in gpiodev_add_to_list_unlocked()
518 * Hence this function will return, if it exists, a reference to the first GPIO
534 guard(srcu)(&gdev->srcu); in gpio_name_to_desc()
536 gc = srcu_dereference(gdev->chip, &gdev->srcu); in gpio_name_to_desc()
541 if (desc->name && !strcmp(desc->name, name)) in gpio_name_to_desc()
550 * Take the names from gc->names and assign them to their GPIO descriptors.
554 * 1. Non-unique names are still accepted,
559 struct gpio_device *gdev = gc->gpiodev; in gpiochip_set_desc_names()
563 for (i = 0; i != gc->ngpio; ++i) { in gpiochip_set_desc_names()
566 gpio = gpio_name_to_desc(gc->names[i]); in gpiochip_set_desc_names()
568 dev_warn(&gdev->dev, in gpiochip_set_desc_names()
569 "Detected name collision for GPIO name '%s'\n", in gpiochip_set_desc_names()
570 gc->names[i]); in gpiochip_set_desc_names()
574 for (i = 0; i != gc->ngpio; ++i) in gpiochip_set_desc_names()
575 gdev->descs[i].name = gc->names[i]; in gpiochip_set_desc_names()
579 * gpiochip_set_names - Set GPIO line names using device properties
582 * Looks for device property "gpio-line-names" and if it exists assigns
589 struct gpio_device *gdev = chip->gpiodev; in gpiochip_set_names()
590 struct device *dev = &gdev->dev; in gpiochip_set_names()
595 count = device_property_string_array_count(dev, "gpio-line-names"); in gpiochip_set_names()
602 * setting friendly names if the specified ones with 'gpio-line-names' in gpiochip_set_names()
607 if (count <= chip->offset) { in gpiochip_set_names()
608 …dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset … in gpiochip_set_names()
609 count, chip->offset); in gpiochip_set_names()
615 return -ENOMEM; in gpiochip_set_names()
617 ret = device_property_read_string_array(dev, "gpio-line-names", in gpiochip_set_names()
620 dev_warn(dev, "failed to read GPIO line names\n"); in gpiochip_set_names()
627 * contain at most number gpiochips x chip->ngpio. We have to in gpiochip_set_names()
629 * chip->offset as starting point from where we will assign in gpiochip_set_names()
631 * 'gpio-line-names' cannot contains gaps, we have to be sure in gpiochip_set_names()
632 * we only assign those pins that really exists since chip->ngpio in gpiochip_set_names()
633 * can be different of the chip->offset. in gpiochip_set_names()
635 count = (count > chip->offset) ? count - chip->offset : count; in gpiochip_set_names()
636 if (count > chip->ngpio) in gpiochip_set_names()
637 count = chip->ngpio; in gpiochip_set_names()
646 if (names[chip->offset + i] && names[chip->offset + i][0]) in gpiochip_set_names()
647 gdev->descs[i].name = names[chip->offset + i]; in gpiochip_set_names()
659 p = bitmap_alloc(gc->ngpio, GFP_KERNEL); in gpiochip_allocate_mask()
664 bitmap_fill(p, gc->ngpio); in gpiochip_allocate_mask()
677 struct device *dev = &gc->gpiodev->dev; in gpiochip_count_reserved_ranges()
681 size = device_property_count_u32(dev, "gpio-reserved-ranges"); in gpiochip_count_reserved_ranges()
690 struct device *dev = &gc->gpiodev->dev; in gpiochip_apply_reserved_ranges()
701 return -ENOMEM; in gpiochip_apply_reserved_ranges()
703 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges", in gpiochip_apply_reserved_ranges()
711 u32 count = ranges[--size]; in gpiochip_apply_reserved_ranges()
712 u32 start = ranges[--size]; in gpiochip_apply_reserved_ranges()
714 if (start >= gc->ngpio || start + count > gc->ngpio) in gpiochip_apply_reserved_ranges()
717 bitmap_clear(gc->gpiodev->valid_mask, start, count); in gpiochip_apply_reserved_ranges()
728 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask)) in gpiochip_init_valid_mask()
731 gc->gpiodev->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_init_valid_mask()
732 if (!gc->gpiodev->valid_mask) in gpiochip_init_valid_mask()
733 return -ENOMEM; in gpiochip_init_valid_mask()
739 if (gc->init_valid_mask) in gpiochip_init_valid_mask()
740 return gc->init_valid_mask(gc, in gpiochip_init_valid_mask()
741 gc->gpiodev->valid_mask, in gpiochip_init_valid_mask()
742 gc->ngpio); in gpiochip_init_valid_mask()
749 gpiochip_free_mask(&gc->gpiodev->valid_mask); in gpiochip_free_valid_mask()
755 * Device Tree platforms are supposed to use "gpio-ranges" in gpiochip_add_pin_ranges()
756 * property. This check ensures that the ->add_pin_ranges() in gpiochip_add_pin_ranges()
759 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges")) in gpiochip_add_pin_ranges()
762 if (gc->add_pin_ranges) in gpiochip_add_pin_ranges()
763 return gc->add_pin_ranges(gc); in gpiochip_add_pin_ranges()
769 * gpiochip_query_valid_mask - return the GPIO validity information
776 * GPIOs. This function can be used to retrieve this information.
780 return gc->gpiodev->valid_mask; in gpiochip_query_valid_mask()
790 if (!gc->gpiodev) in gpiochip_line_is_valid()
794 if (likely(!gc->gpiodev->valid_mask)) in gpiochip_line_is_valid()
796 return test_bit(offset, gc->gpiodev->valid_mask); in gpiochip_line_is_valid()
810 * If the irq is freed after we release the lock, that's in gpiod_free_irqs()
811 * alright - the underlying maple tree lookup will return NULL in gpiod_free_irqs()
814 scoped_guard(mutex, &irqd->request_mutex) { in gpiod_free_irqs()
818 cookie = irqd->action->dev_id; in gpiod_free_irqs()
844 synchronize_srcu(&gdev->desc_srcu); in gpiodev_release()
845 cleanup_srcu_struct(&gdev->desc_srcu); in gpiodev_release()
847 ida_free(&gpio_ida, gdev->id); in gpiodev_release()
848 kfree_const(gdev->label); in gpiodev_release()
849 kfree(gdev->descs); in gpiodev_release()
850 cleanup_srcu_struct(&gdev->srcu); in gpiodev_release()
867 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
868 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
873 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev); in gpiochip_setup_dev()
876 device_initialize(&gdev->dev); in gpiochip_setup_dev()
882 if (fwnode && !fwnode->dev) in gpiochip_setup_dev()
893 dev_dbg(&gdev->dev, "registered GPIOs %u to %u on %s\n", gdev->base, in gpiochip_setup_dev()
894 gdev->base + gdev->ngpio - 1, gdev->label); in gpiochip_setup_dev()
908 desc = gpiochip_get_desc(gc, hog->chip_hwnum); in gpiochip_machine_hog()
910 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__, in gpiochip_machine_hog()
915 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); in gpiochip_machine_hog()
917 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n", in gpiochip_machine_hog()
918 __func__, gc->label, hog->chip_hwnum, rv); in gpiochip_machine_hog()
928 if (!strcmp(gc->label, hog->chip_label)) in machine_gpiochip_add()
944 dev_err(&gdev->dev, in gpiochip_setup_devs()
945 "Failed to initialize gpio device (%d)\n", ret); in gpiochip_setup_devs()
951 gc->gpiodev->data = data; in gpiochip_set_data()
955 * gpiochip_get_data() - get per-subdriver data for the chip
959 * The per-subdriver data for the chip.
963 return gc->gpiodev->data; in gpiochip_get_data()
973 if (gc->fwnode) in gpiochip_choose_fwnode()
974 return gc->fwnode; in gpiochip_choose_fwnode()
976 if (gc->parent) in gpiochip_choose_fwnode()
977 return dev_fwnode(gc->parent); in gpiochip_choose_fwnode()
985 u32 ngpios = gc->ngpio; in gpiochip_get_ngpios()
990 if (ret == -ENODATA) in gpiochip_get_ngpios()
992 * -ENODATA means that there is no property found and in gpiochip_get_ngpios()
1001 gc->ngpio = ngpios; in gpiochip_get_ngpios()
1004 if (gc->ngpio == 0) { in gpiochip_get_ngpios()
1005 dev_err(dev, "tried to insert a GPIO chip with zero lines\n"); in gpiochip_get_ngpios()
1006 return -EINVAL; in gpiochip_get_ngpios()
1009 if (gc->ngpio > FASTPATH_NGPIO) in gpiochip_get_ngpios()
1010 dev_warn(dev, "line cnt %u is greater than fast path cnt %u\n", in gpiochip_get_ngpios()
1011 gc->ngpio, FASTPATH_NGPIO); in gpiochip_get_ngpios()
1027 if ((gc->set && gc->set_rv) || in gpiochip_add_data_with_key()
1028 (gc->set_multiple && gc->set_multiple_rv)) in gpiochip_add_data_with_key()
1029 return -EINVAL; in gpiochip_add_data_with_key()
1037 return -ENOMEM; in gpiochip_add_data_with_key()
1039 gdev->dev.type = &gpio_dev_type; in gpiochip_add_data_with_key()
1040 gdev->dev.bus = &gpio_bus_type; in gpiochip_add_data_with_key()
1041 gdev->dev.parent = gc->parent; in gpiochip_add_data_with_key()
1042 rcu_assign_pointer(gdev->chip, gc); in gpiochip_add_data_with_key()
1044 gc->gpiodev = gdev; in gpiochip_add_data_with_key()
1047 device_set_node(&gdev->dev, gpiochip_choose_fwnode(gc)); in gpiochip_add_data_with_key()
1052 gdev->id = ret; in gpiochip_add_data_with_key()
1054 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); in gpiochip_add_data_with_key()
1058 if (gc->parent && gc->parent->driver) in gpiochip_add_data_with_key()
1059 gdev->owner = gc->parent->driver->owner; in gpiochip_add_data_with_key()
1060 else if (gc->owner) in gpiochip_add_data_with_key()
1061 /* TODO: remove chip->owner */ in gpiochip_add_data_with_key()
1062 gdev->owner = gc->owner; in gpiochip_add_data_with_key()
1064 gdev->owner = THIS_MODULE; in gpiochip_add_data_with_key()
1066 ret = gpiochip_get_ngpios(gc, &gdev->dev); in gpiochip_add_data_with_key()
1070 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL); in gpiochip_add_data_with_key()
1071 if (!gdev->descs) { in gpiochip_add_data_with_key()
1072 ret = -ENOMEM; in gpiochip_add_data_with_key()
1076 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL); in gpiochip_add_data_with_key()
1077 if (!gdev->label) { in gpiochip_add_data_with_key()
1078 ret = -ENOMEM; in gpiochip_add_data_with_key()
1082 gdev->ngpio = gc->ngpio; in gpiochip_add_data_with_key()
1083 gdev->can_sleep = gc->can_sleep; in gpiochip_add_data_with_key()
1093 base = gc->base; in gpiochip_add_data_with_key()
1095 base = gpiochip_find_base_unlocked(gc->ngpio); in gpiochip_add_data_with_key()
1108 gc->base = base; in gpiochip_add_data_with_key()
1110 dev_warn(&gdev->dev, in gpiochip_add_data_with_key()
1111 "Static allocation of GPIO base is deprecated, use dynamic allocation.\n"); in gpiochip_add_data_with_key()
1114 gdev->base = base; in gpiochip_add_data_with_key()
1118 chip_err(gc, "GPIO integer space overlap, cannot add chip\n"); in gpiochip_add_data_with_key()
1123 rwlock_init(&gdev->line_state_lock); in gpiochip_add_data_with_key()
1124 RAW_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); in gpiochip_add_data_with_key()
1125 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); in gpiochip_add_data_with_key()
1127 ret = init_srcu_struct(&gdev->srcu); in gpiochip_add_data_with_key()
1131 ret = init_srcu_struct(&gdev->desc_srcu); in gpiochip_add_data_with_key()
1136 INIT_LIST_HEAD(&gdev->pin_ranges); in gpiochip_add_data_with_key()
1139 if (gc->names) in gpiochip_add_data_with_key()
1150 for (desc_index = 0; desc_index < gc->ngpio; desc_index++) { in gpiochip_add_data_with_key()
1151 struct gpio_desc *desc = &gdev->descs[desc_index]; in gpiochip_add_data_with_key()
1153 desc->gdev = gdev; in gpiochip_add_data_with_key()
1157 * but we must not check the return value and bail-out as pin in gpiochip_add_data_with_key()
1159 * and return -EINVAL. Also: there's no need to take the SRCU in gpiochip_add_data_with_key()
1160 * lock here. in gpiochip_add_data_with_key()
1162 if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) in gpiochip_add_data_with_key()
1163 assign_bit(FLAG_IS_OUT, &desc->flags, in gpiochip_add_data_with_key()
1164 !gc->get_direction(gc, desc_index)); in gpiochip_add_data_with_key()
1167 &desc->flags, !gc->direction_input); in gpiochip_add_data_with_key()
1222 cleanup_srcu_struct(&gdev->desc_srcu); in gpiochip_add_data_with_key()
1224 cleanup_srcu_struct(&gdev->srcu); in gpiochip_add_data_with_key()
1227 list_del_rcu(&gdev->list); in gpiochip_add_data_with_key()
1229 if (gdev->dev.release) { in gpiochip_add_data_with_key()
1235 kfree_const(gdev->label); in gpiochip_add_data_with_key()
1237 kfree(gdev->descs); in gpiochip_add_data_with_key()
1239 kfree(dev_name(&gdev->dev)); in gpiochip_add_data_with_key()
1241 ida_free(&gpio_ida, gdev->id); in gpiochip_add_data_with_key()
1246 if (ret != -EPROBE_DEFER) { in gpiochip_add_data_with_key()
1247 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__, in gpiochip_add_data_with_key()
1248 base, base + (int)gc->ngpio - 1, in gpiochip_add_data_with_key()
1249 gc->label ? : "generic", ret); in gpiochip_add_data_with_key()
1256 * gpiochip_remove() - unregister a gpio_chip
1263 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove()
1271 list_del_rcu(&gdev->list); in gpiochip_remove()
1275 rcu_assign_pointer(gdev->chip, NULL); in gpiochip_remove()
1276 synchronize_srcu(&gdev->srcu); in gpiochip_remove()
1300 * gpio_device_find() - find a specific GPIO device
1301 * @data: data to pass to match function
1302 * @match: Callback function to check gpio_chip
1309 * 0 if the device doesn't match and non-zero if it does. If the callback
1310 * returns non-zero, this function will return to the caller and not iterate
1314 * of the callback function the chip is protected from being freed. TODO: This
1317 * If the function returns non-NULL, the returned reference must be freed by
1333 if (!device_is_registered(&gdev->dev)) in gpio_device_find()
1336 guard(srcu)(&gdev->srcu); in gpio_device_find()
1338 gc = srcu_dereference(gdev->chip, &gdev->srcu); in gpio_device_find()
1350 return gc->label && !strcmp(gc->label, label); in gpio_chip_match_by_label()
1354 * gpio_device_find_by_label() - wrapper around gpio_device_find() finding the
1370 return device_match_fwnode(&gc->gpiodev->dev, fwnode); in gpio_chip_match_by_fwnode()
1374 * gpio_device_find_by_fwnode() - wrapper around gpio_device_find() finding
1389 * gpio_device_get() - Increase the reference count of this GPIO device
1397 return to_gpio_device(get_device(&gdev->dev)); in gpio_device_get()
1402 * gpio_device_put() - Decrease the reference count of this GPIO device and
1408 put_device(&gdev->dev); in gpio_device_put()
1413 * gpio_device_to_device() - Retrieve the address of the underlying struct
1425 return &gdev->dev; in gpio_device_to_device()
1437 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_hw()
1439 if (!girq->init_hw) in gpiochip_irqchip_init_hw()
1442 return girq->init_hw(gc); in gpiochip_irqchip_init_hw()
1447 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_irqchip_init_valid_mask()
1449 if (!girq->init_valid_mask) in gpiochip_irqchip_init_valid_mask()
1452 girq->valid_mask = gpiochip_allocate_mask(gc); in gpiochip_irqchip_init_valid_mask()
1453 if (!girq->valid_mask) in gpiochip_irqchip_init_valid_mask()
1454 return -ENOMEM; in gpiochip_irqchip_init_valid_mask()
1456 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); in gpiochip_irqchip_init_valid_mask()
1463 gpiochip_free_mask(&gc->irq.valid_mask); in gpiochip_irqchip_free_valid_mask()
1472 if (likely(!gc->irq.valid_mask)) in gpiochip_irqchip_irq_valid()
1474 return test_bit(offset, gc->irq.valid_mask); in gpiochip_irqchip_irq_valid()
1480 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1490 if (is_of_node(gc->irq.fwnode)) in gpiochip_set_hierarchical_irqchip()
1501 if (is_fwnode_irqchip(gc->irq.fwnode)) { in gpiochip_set_hierarchical_irqchip()
1505 for (i = 0; i < gc->ngpio; i++) { in gpiochip_set_hierarchical_irqchip()
1509 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_set_hierarchical_irqchip()
1512 * We call the child to parent translation function in gpiochip_set_hierarchical_irqchip()
1517 ret = girq->child_to_parent_hwirq(gc, i, in gpiochip_set_hierarchical_irqchip()
1522 chip_err(gc, "skip set-up on hwirq %d\n", in gpiochip_set_hierarchical_irqchip()
1527 fwspec.fwnode = gc->irq.fwnode; in gpiochip_set_hierarchical_irqchip()
1529 fwspec.param[0] = girq->child_offset_to_irq(gc, i); in gpiochip_set_hierarchical_irqchip()
1533 ret = irq_domain_alloc_irqs(gc->irq.domain, 1, in gpiochip_set_hierarchical_irqchip()
1537 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n", in gpiochip_set_hierarchical_irqchip()
1544 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__); in gpiochip_set_hierarchical_irqchip()
1555 if (is_of_node(fwspec->fwnode)) in gpiochip_hierarchy_irq_domain_translate()
1559 if (is_fwnode_irqchip(fwspec->fwnode)) { in gpiochip_hierarchy_irq_domain_translate()
1568 return -EINVAL; in gpiochip_hierarchy_irq_domain_translate()
1576 struct gpio_chip *gc = d->host_data; in gpiochip_hierarchy_irq_domain_alloc()
1583 struct gpio_irq_chip *girq = &gc->irq; in gpiochip_hierarchy_irq_domain_alloc()
1587 * The nr_irqs parameter is always one except for PCI multi-MSI in gpiochip_hierarchy_irq_domain_alloc()
1592 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); in gpiochip_hierarchy_irq_domain_alloc()
1596 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq); in gpiochip_hierarchy_irq_domain_alloc()
1598 ret = girq->child_to_parent_hwirq(gc, hwirq, type, in gpiochip_hierarchy_irq_domain_alloc()
1601 chip_err(gc, "can't look up hwirq %lu\n", hwirq); in gpiochip_hierarchy_irq_domain_alloc()
1604 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq); in gpiochip_hierarchy_irq_domain_alloc()
1613 gc->irq.chip, in gpiochip_hierarchy_irq_domain_alloc()
1615 girq->handler, in gpiochip_hierarchy_irq_domain_alloc()
1620 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec, in gpiochip_hierarchy_irq_domain_alloc()
1625 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n", in gpiochip_hierarchy_irq_domain_alloc()
1627 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_hierarchy_irq_domain_alloc()
1633 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST)) in gpiochip_hierarchy_irq_domain_alloc()
1637 "failed to allocate parent hwirq %d for hwirq %lu\n", in gpiochip_hierarchy_irq_domain_alloc()
1650 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1655 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1656 * used as the activate function for the &struct irq_domain_ops. The host_data
1665 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_activate()
1672 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1676 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1677 * be used as the deactivate function for the &struct irq_domain_ops. The
1683 struct gpio_chip *gc = domain->host_data; in gpiochip_irq_domain_deactivate()
1691 ops->activate = gpiochip_irq_domain_activate; in gpiochip_hierarchy_setup_domain_ops()
1692 ops->deactivate = gpiochip_irq_domain_deactivate; in gpiochip_hierarchy_setup_domain_ops()
1693 ops->alloc = gpiochip_hierarchy_irq_domain_alloc; in gpiochip_hierarchy_setup_domain_ops()
1702 if (!ops->translate) in gpiochip_hierarchy_setup_domain_ops()
1703 ops->translate = gpiochip_hierarchy_irq_domain_translate; in gpiochip_hierarchy_setup_domain_ops()
1704 if (!ops->free) in gpiochip_hierarchy_setup_domain_ops()
1705 ops->free = irq_domain_free_irqs_common; in gpiochip_hierarchy_setup_domain_ops()
1712 if (!gc->irq.child_to_parent_hwirq || in gpiochip_hierarchy_create_domain()
1713 !gc->irq.fwnode) { in gpiochip_hierarchy_create_domain()
1714 chip_err(gc, "missing irqdomain vital data\n"); in gpiochip_hierarchy_create_domain()
1715 return ERR_PTR(-EINVAL); in gpiochip_hierarchy_create_domain()
1718 if (!gc->irq.child_offset_to_irq) in gpiochip_hierarchy_create_domain()
1719 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; in gpiochip_hierarchy_create_domain()
1721 if (!gc->irq.populate_parent_alloc_arg) in gpiochip_hierarchy_create_domain()
1722 gc->irq.populate_parent_alloc_arg = in gpiochip_hierarchy_create_domain()
1725 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); in gpiochip_hierarchy_create_domain()
1728 gc->irq.parent_domain, in gpiochip_hierarchy_create_domain()
1730 gc->ngpio, in gpiochip_hierarchy_create_domain()
1731 gc->irq.fwnode, in gpiochip_hierarchy_create_domain()
1732 &gc->irq.child_irq_domain_ops, in gpiochip_hierarchy_create_domain()
1736 return ERR_PTR(-ENOMEM); in gpiochip_hierarchy_create_domain()
1738 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); in gpiochip_hierarchy_create_domain()
1745 return !!gc->irq.parent_domain; in gpiochip_hierarchy_is_hierarchical()
1753 struct irq_fwspec *fwspec = &gfwspec->fwspec; in gpiochip_populate_parent_fwspec_twocell()
1755 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_twocell()
1756 fwspec->param_count = 2; in gpiochip_populate_parent_fwspec_twocell()
1757 fwspec->param[0] = parent_hwirq; in gpiochip_populate_parent_fwspec_twocell()
1758 fwspec->param[1] = parent_type; in gpiochip_populate_parent_fwspec_twocell()
1769 struct irq_fwspec *fwspec = &gfwspec->fwspec; in gpiochip_populate_parent_fwspec_fourcell()
1771 fwspec->fwnode = gc->irq.parent_domain->fwnode; in gpiochip_populate_parent_fwspec_fourcell()
1772 fwspec->param_count = 4; in gpiochip_populate_parent_fwspec_fourcell()
1773 fwspec->param[0] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1774 fwspec->param[1] = parent_hwirq; in gpiochip_populate_parent_fwspec_fourcell()
1775 fwspec->param[2] = 0; in gpiochip_populate_parent_fwspec_fourcell()
1776 fwspec->param[3] = parent_type; in gpiochip_populate_parent_fwspec_fourcell()
1786 return ERR_PTR(-EINVAL); in gpiochip_hierarchy_create_domain()
1797 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1802 * This function will set up the mapping for a certain IRQ line on a
1812 struct gpio_chip *gc = d->host_data; in gpiochip_irq_map()
1816 return -ENXIO; in gpiochip_irq_map()
1820 * This lock class tells lockdep that GPIO irqs are in a different in gpiochip_irq_map()
1823 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); in gpiochip_irq_map()
1824 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler); in gpiochip_irq_map()
1826 if (gc->irq.threaded) in gpiochip_irq_map()
1830 if (gc->irq.num_parents == 1) in gpiochip_irq_map()
1831 ret = irq_set_parent(irq, gc->irq.parents[0]); in gpiochip_irq_map()
1832 else if (gc->irq.map) in gpiochip_irq_map()
1833 ret = irq_set_parent(irq, gc->irq.map[hwirq]); in gpiochip_irq_map()
1839 * No set-up of the hardware will happen if IRQ_TYPE_NONE in gpiochip_irq_map()
1842 if (gc->irq.default_type != IRQ_TYPE_NONE) in gpiochip_irq_map()
1843 irq_set_irq_type(irq, gc->irq.default_type); in gpiochip_irq_map()
1850 struct gpio_chip *gc = d->host_data; in gpiochip_irq_unmap()
1852 if (gc->irq.threaded) in gpiochip_irq_unmap()
1861 struct fwnode_handle *fwnode = fwspec->fwnode; in gpiochip_irq_select()
1862 struct gpio_chip *gc = d->host_data; in gpiochip_irq_select()
1863 unsigned int index = fwspec->param[0]; in gpiochip_irq_select()
1865 if (fwspec->param_count == 3 && is_of_node(fwnode)) in gpiochip_irq_select()
1869 return (fwnode && (d->fwnode == fwnode) && (d->bus_token == bus_token)); in gpiochip_irq_select()
1882 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev); in gpiochip_simple_create_domain()
1885 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first, in gpiochip_simple_create_domain()
1888 return ERR_PTR(-EINVAL); in gpiochip_simple_create_domain()
1895 struct irq_domain *domain = gc->irq.domain; in gpiochip_to_irq()
1902 if (!gc->irq.initialized) in gpiochip_to_irq()
1903 return -EPROBE_DEFER; in gpiochip_to_irq()
1906 return -ENXIO; in gpiochip_to_irq()
1912 spec.fwnode = domain->fwnode; in gpiochip_to_irq()
1914 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset); in gpiochip_to_irq()
1947 if (gc->irq.irq_mask) in gpiochip_irq_mask()
1948 gc->irq.irq_mask(d); in gpiochip_irq_mask()
1958 if (gc->irq.irq_unmask) in gpiochip_irq_unmask()
1959 gc->irq.irq_unmask(d); in gpiochip_irq_unmask()
1968 gc->irq.irq_enable(d); in gpiochip_irq_enable()
1976 gc->irq.irq_disable(d); in gpiochip_irq_disable()
1982 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_set_irq_hooks()
1984 if (irqchip->flags & IRQCHIP_IMMUTABLE) in gpiochip_set_irq_hooks()
1987 chip_warn(gc, "not an immutable chip, please consider fixing it!\n"); in gpiochip_set_irq_hooks()
1989 if (!irqchip->irq_request_resources && in gpiochip_set_irq_hooks()
1990 !irqchip->irq_release_resources) { in gpiochip_set_irq_hooks()
1991 irqchip->irq_request_resources = gpiochip_irq_reqres; in gpiochip_set_irq_hooks()
1992 irqchip->irq_release_resources = gpiochip_irq_relres; in gpiochip_set_irq_hooks()
1994 if (WARN_ON(gc->irq.irq_enable)) in gpiochip_set_irq_hooks()
1997 if (irqchip->irq_enable == gpiochip_irq_enable || in gpiochip_set_irq_hooks()
1998 irqchip->irq_mask == gpiochip_irq_mask) { in gpiochip_set_irq_hooks()
2004 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n"); in gpiochip_set_irq_hooks()
2008 if (irqchip->irq_disable) { in gpiochip_set_irq_hooks()
2009 gc->irq.irq_disable = irqchip->irq_disable; in gpiochip_set_irq_hooks()
2010 irqchip->irq_disable = gpiochip_irq_disable; in gpiochip_set_irq_hooks()
2012 gc->irq.irq_mask = irqchip->irq_mask; in gpiochip_set_irq_hooks()
2013 irqchip->irq_mask = gpiochip_irq_mask; in gpiochip_set_irq_hooks()
2016 if (irqchip->irq_enable) { in gpiochip_set_irq_hooks()
2017 gc->irq.irq_enable = irqchip->irq_enable; in gpiochip_set_irq_hooks()
2018 irqchip->irq_enable = gpiochip_irq_enable; in gpiochip_set_irq_hooks()
2020 gc->irq.irq_unmask = irqchip->irq_unmask; in gpiochip_set_irq_hooks()
2021 irqchip->irq_unmask = gpiochip_irq_unmask; in gpiochip_set_irq_hooks()
2030 return -EINVAL; in gpiochip_irqchip_add_allocated_domain()
2032 if (gc->to_irq) in gpiochip_irqchip_add_allocated_domain()
2033 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__); in gpiochip_irqchip_add_allocated_domain()
2035 gc->to_irq = gpiochip_to_irq; in gpiochip_irqchip_add_allocated_domain()
2036 gc->irq.domain = domain; in gpiochip_irqchip_add_allocated_domain()
2037 gc->irq.domain_is_allocated_externally = allocated_externally; in gpiochip_irqchip_add_allocated_domain()
2041 * gc->irq.initialized before adding irqdomain. in gpiochip_irqchip_add_allocated_domain()
2045 gc->irq.initialized = true; in gpiochip_irqchip_add_allocated_domain()
2051 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2053 * @lock_key: lockdep class for IRQ lock
2063 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev); in gpiochip_add_irqchip()
2064 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_add_irqchip()
2073 if (gc->irq.parent_handler && gc->can_sleep) { in gpiochip_add_irqchip()
2074 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n"); in gpiochip_add_irqchip()
2075 return -EINVAL; in gpiochip_add_irqchip()
2078 type = gc->irq.default_type; in gpiochip_add_irqchip()
2086 "%pfw: Ignoring %u default trigger\n", fwnode, type)) in gpiochip_add_irqchip()
2089 gc->irq.default_type = type; in gpiochip_add_irqchip()
2090 gc->irq.lock_key = lock_key; in gpiochip_add_irqchip()
2091 gc->irq.request_key = request_key; in gpiochip_add_irqchip()
2102 if (gc->irq.parent_handler) { in gpiochip_add_irqchip()
2103 for (i = 0; i < gc->irq.num_parents; i++) { in gpiochip_add_irqchip()
2106 if (gc->irq.per_parent_data) in gpiochip_add_irqchip()
2107 data = gc->irq.parent_handler_data_array[i]; in gpiochip_add_irqchip()
2109 data = gc->irq.parent_handler_data ?: gc; in gpiochip_add_irqchip()
2116 irq_set_chained_handler_and_data(gc->irq.parents[i], in gpiochip_add_irqchip()
2117 gc->irq.parent_handler, in gpiochip_add_irqchip()
2134 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2141 struct irq_chip *irqchip = gc->irq.chip; in gpiochip_irqchip_remove()
2146 if (irqchip && gc->irq.parent_handler) { in gpiochip_irqchip_remove()
2147 struct gpio_irq_chip *irq = &gc->irq; in gpiochip_irqchip_remove()
2150 for (i = 0; i < irq->num_parents; i++) in gpiochip_irqchip_remove()
2151 irq_set_chained_handler_and_data(irq->parents[i], in gpiochip_irqchip_remove()
2156 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) { in gpiochip_irqchip_remove()
2159 for (offset = 0; offset < gc->ngpio; offset++) { in gpiochip_irqchip_remove()
2163 irq = irq_find_mapping(gc->irq.domain, offset); in gpiochip_irqchip_remove()
2167 irq_domain_remove(gc->irq.domain); in gpiochip_irqchip_remove()
2170 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) { in gpiochip_irqchip_remove()
2171 if (irqchip->irq_request_resources == gpiochip_irq_reqres) { in gpiochip_irqchip_remove()
2172 irqchip->irq_request_resources = NULL; in gpiochip_irqchip_remove()
2173 irqchip->irq_release_resources = NULL; in gpiochip_irqchip_remove()
2175 if (irqchip->irq_enable == gpiochip_irq_enable) { in gpiochip_irqchip_remove()
2176 irqchip->irq_enable = gc->irq.irq_enable; in gpiochip_irqchip_remove()
2177 irqchip->irq_disable = gc->irq.irq_disable; in gpiochip_irqchip_remove()
2180 gc->irq.irq_enable = NULL; in gpiochip_irqchip_remove()
2181 gc->irq.irq_disable = NULL; in gpiochip_irqchip_remove()
2182 gc->irq.chip = NULL; in gpiochip_irqchip_remove()
2188 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
2192 * This function adds an IRQ domain to the gpiochip.
2229 * gpiochip_generic_request() - request the gpio function for a pin
2231 * @offset: the offset of the GPIO to request for GPIO function
2239 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_request()
2248 * gpiochip_generic_free() - free the gpio function from a pin
2249 * @gc: the gpiochip to request the gpio function for
2250 * @offset: the offset of the GPIO to free from GPIO function
2255 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_free()
2264 * gpiochip_generic_config() - apply configuration for a pin
2276 if (list_empty(&gc->gpiodev->pin_ranges)) in gpiochip_generic_config()
2277 return -ENOTSUPP; in gpiochip_generic_config()
2287 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2293 * Calling this function directly from a DeviceTree-supported
2296 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2306 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pingroup_range()
2311 chip_err(gc, "failed to allocate pin ranges\n"); in gpiochip_add_pingroup_range()
2312 return -ENOMEM; in gpiochip_add_pingroup_range()
2316 pin_range->range.id = gpio_offset; in gpiochip_add_pingroup_range()
2317 pin_range->range.gc = gc; in gpiochip_add_pingroup_range()
2318 pin_range->range.name = gc->label; in gpiochip_add_pingroup_range()
2319 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pingroup_range()
2320 pin_range->pctldev = pctldev; in gpiochip_add_pingroup_range()
2323 &pin_range->range.pins, in gpiochip_add_pingroup_range()
2324 &pin_range->range.npins); in gpiochip_add_pingroup_range()
2330 pinctrl_add_gpio_range(pctldev, &pin_range->range); in gpiochip_add_pingroup_range()
2332 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n", in gpiochip_add_pingroup_range()
2333 gpio_offset, gpio_offset + pin_range->range.npins - 1, in gpiochip_add_pingroup_range()
2336 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pingroup_range()
2343 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2351 * Calling this function directly from a DeviceTree-supported
2354 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2364 struct gpio_device *gdev = gc->gpiodev; in gpiochip_add_pin_range()
2369 chip_err(gc, "failed to allocate pin ranges\n"); in gpiochip_add_pin_range()
2370 return -ENOMEM; in gpiochip_add_pin_range()
2374 pin_range->range.id = gpio_offset; in gpiochip_add_pin_range()
2375 pin_range->range.gc = gc; in gpiochip_add_pin_range()
2376 pin_range->range.name = gc->label; in gpiochip_add_pin_range()
2377 pin_range->range.base = gdev->base + gpio_offset; in gpiochip_add_pin_range()
2378 pin_range->range.pin_base = pin_offset; in gpiochip_add_pin_range()
2379 pin_range->range.npins = npins; in gpiochip_add_pin_range()
2380 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, in gpiochip_add_pin_range()
2381 &pin_range->range); in gpiochip_add_pin_range()
2382 if (IS_ERR(pin_range->pctldev)) { in gpiochip_add_pin_range()
2383 ret = PTR_ERR(pin_range->pctldev); in gpiochip_add_pin_range()
2384 chip_err(gc, "could not create pin range\n"); in gpiochip_add_pin_range()
2388 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n", in gpiochip_add_pin_range()
2389 gpio_offset, gpio_offset + npins - 1, in gpiochip_add_pin_range()
2391 pin_offset, pin_offset + npins - 1); in gpiochip_add_pin_range()
2393 list_add_tail(&pin_range->node, &gdev->pin_ranges); in gpiochip_add_pin_range()
2400 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2406 struct gpio_device *gdev = gc->gpiodev; in gpiochip_remove_pin_ranges()
2408 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { in gpiochip_remove_pin_ranges()
2409 list_del(&pin_range->node); in gpiochip_remove_pin_ranges()
2410 pinctrl_remove_gpio_range(pin_range->pctldev, in gpiochip_remove_pin_ranges()
2411 &pin_range->range); in gpiochip_remove_pin_ranges()
2430 return -ENODEV; in gpiod_request_commit()
2432 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags)) in gpiod_request_commit()
2433 return -EBUSY; in gpiod_request_commit()
2437 return -EINVAL; in gpiod_request_commit()
2440 * before IRQs are enabled, for non-sleeping (SOC) GPIOs. in gpiod_request_commit()
2443 if (guard.gc->request) { in gpiod_request_commit()
2444 ret = guard.gc->request(guard.gc, offset); in gpiod_request_commit()
2446 ret = -EBADE; in gpiod_request_commit()
2451 if (guard.gc->get_direction) in gpiod_request_commit()
2461 clear_bit(FLAG_REQUESTED, &desc->flags); in gpiod_request_commit()
2467 int ret = -EPROBE_DEFER; in gpiod_request()
2471 if (try_module_get(desc->gdev->owner)) { in gpiod_request()
2474 module_put(desc->gdev->owner); in gpiod_request()
2476 gpio_device_get(desc->gdev); in gpiod_request()
2480 gpiod_dbg(desc, "%s: status %d\n", __func__, ret); in gpiod_request()
2493 flags = READ_ONCE(desc->flags); in gpiod_free_commit()
2496 if (guard.gc->free) in gpiod_free_commit()
2497 guard.gc->free(guard.gc, gpio_chip_hwgpio(desc)); in gpiod_free_commit()
2510 WRITE_ONCE(desc->hog, NULL); in gpiod_free_commit()
2513 WRITE_ONCE(desc->flags, flags); in gpiod_free_commit()
2515 WRITE_ONCE(desc->debounce_period_us, 0); in gpiod_free_commit()
2526 module_put(desc->gdev->owner); in gpiod_free()
2527 gpio_device_put(desc->gdev); in gpiod_free()
2531 * gpiochip_dup_line_label - Get a copy of the consumer label.
2538 * kfree(). In case of a memory allocation error, the function returns %ENOMEM.
2551 if (!test_bit(FLAG_REQUESTED, &desc->flags)) in gpiochip_dup_line_label()
2554 guard(srcu)(&desc->gdev->desc_srcu); in gpiochip_dup_line_label()
2558 return ERR_PTR(-ENOMEM); in gpiochip_dup_line_label()
2570 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2580 * Function allows GPIO chip drivers to request and use their own GPIO
2582 * function will not increase reference count of the GPIO chip module. This
2587 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2601 chip_err(gc, "failed to get GPIO %s descriptor\n", name); in gpiochip_request_own_desc()
2612 chip_err(gc, "setup of own GPIO %s failed\n", name); in gpiochip_request_own_desc()
2623 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2626 * Function frees the given GPIO requested previously with
2641 * using the open-drain emulation idiom) so these are natural places
2652 return -ENODEV; in gpio_do_set_config()
2654 if (!guard.gc->set_config) in gpio_do_set_config()
2655 return -ENOTSUPP; in gpio_do_set_config()
2657 ret = guard.gc->set_config(guard.gc, gpio_chip_hwgpio(desc), config); in gpio_do_set_config()
2659 ret = -EBADE; in gpio_do_set_config()
2663 * Special case - if we're setting debounce period, we need to store in gpio_do_set_config()
2664 * it in the descriptor in case user-space wants to know it. in gpio_do_set_config()
2667 WRITE_ONCE(desc->debounce_period_us, in gpio_do_set_config()
2687 struct device *dev = &desc->gdev->dev; in gpio_set_config_with_argument_optional()
2692 if (ret != -ENOTSUPP) in gpio_set_config_with_argument_optional()
2697 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio); in gpio_set_config_with_argument_optional()
2717 flags = READ_ONCE(desc->flags); in gpio_set_bias()
2743 * gpio_set_debounce_timeout() - Set debounce timeout
2747 * The function calls the certain GPIO driver to set debounce timeout
2770 lockdep_assert_held(&gc->gpiodev->srcu); in gpiochip_direction_input()
2772 if (WARN_ON(!gc->direction_input)) in gpiochip_direction_input()
2773 return -EOPNOTSUPP; in gpiochip_direction_input()
2775 ret = gc->direction_input(gc, offset); in gpiochip_direction_input()
2777 ret = -EBADE; in gpiochip_direction_input()
2787 lockdep_assert_held(&gc->gpiodev->srcu); in gpiochip_direction_output()
2789 if (WARN_ON(!gc->direction_output)) in gpiochip_direction_output()
2790 return -EOPNOTSUPP; in gpiochip_direction_output()
2792 ret = gc->direction_output(gc, offset, value); in gpiochip_direction_output()
2794 ret = -EBADE; in gpiochip_direction_output()
2800 * gpiod_direction_input - set the GPIO direction to input
2829 return -ENODEV; in gpiod_direction_input_nonotify()
2833 * the chip is output-only, but you can't specify .direction_input() in gpiod_direction_input_nonotify()
2836 if (!guard.gc->get && guard.gc->direction_input) { in gpiod_direction_input_nonotify()
2838 "%s: missing get() but have direction_input()\n", in gpiod_direction_input_nonotify()
2840 return -EIO; in gpiod_direction_input_nonotify()
2845 * just call it. Else we are some input-only chip so try to check the in gpiod_direction_input_nonotify()
2849 if (guard.gc->direction_input) { in gpiod_direction_input_nonotify()
2852 } else if (guard.gc->get_direction) { in gpiod_direction_input_nonotify()
2859 "%s: missing direction_input() operation and line is output\n", in gpiod_direction_input_nonotify()
2861 return -EIO; in gpiod_direction_input_nonotify()
2865 clear_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_input_nonotify()
2878 lockdep_assert_held(&gc->gpiodev->srcu); in gpiochip_set()
2880 if (WARN_ON(unlikely(!gc->set && !gc->set_rv))) in gpiochip_set()
2881 return -EOPNOTSUPP; in gpiochip_set()
2883 if (gc->set_rv) { in gpiochip_set()
2884 ret = gc->set_rv(gc, offset, value); in gpiochip_set()
2886 ret = -EBADE; in gpiochip_set()
2891 gc->set(gc, offset, value); in gpiochip_set()
2901 return -ENODEV; in gpiod_direction_output_raw_commit()
2905 * output-only, but if there is then not even a .set() operation it in gpiod_direction_output_raw_commit()
2908 if (!guard.gc->set && !guard.gc->set_rv && !guard.gc->direction_output) { in gpiod_direction_output_raw_commit()
2910 "%s: missing set() and direction_output() operations\n", in gpiod_direction_output_raw_commit()
2912 return -EIO; in gpiod_direction_output_raw_commit()
2915 if (guard.gc->direction_output) { in gpiod_direction_output_raw_commit()
2920 if (guard.gc->get_direction) { in gpiod_direction_output_raw_commit()
2928 "%s: missing direction_output() operation\n", in gpiod_direction_output_raw_commit()
2930 return -EIO; in gpiod_direction_output_raw_commit()
2935 * output-only chip, so just drive the output as desired. in gpiod_direction_output_raw_commit()
2943 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output_raw_commit()
2950 * gpiod_direction_output_raw - set the GPIO direction to output
2976 * gpiod_direction_output - set the GPIO direction to output
3007 flags = READ_ONCE(desc->flags); in gpiod_direction_output_nonotify()
3018 "%s: tried to set a GPIO tied to an IRQ as output\n", in gpiod_direction_output_nonotify()
3020 return -EIO; in gpiod_direction_output_nonotify()
3024 /* First see if we can enable open drain in hardware */ in gpiod_direction_output_nonotify()
3053 * When emulating open-source or open-drain functionalities by not in gpiod_direction_output_nonotify()
3058 set_bit(FLAG_IS_OUT, &desc->flags); in gpiod_direction_output_nonotify()
3064 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
3066 * @desc: GPIO to enable.
3080 return -ENODEV; in gpiod_enable_hw_timestamp_ns()
3082 if (!guard.gc->en_hw_timestamp) { in gpiod_enable_hw_timestamp_ns()
3083 gpiod_warn(desc, "%s: hw ts not supported\n", __func__); in gpiod_enable_hw_timestamp_ns()
3084 return -ENOTSUPP; in gpiod_enable_hw_timestamp_ns()
3087 ret = guard.gc->en_hw_timestamp(guard.gc, in gpiod_enable_hw_timestamp_ns()
3090 gpiod_warn(desc, "%s: hw ts request failed\n", __func__); in gpiod_enable_hw_timestamp_ns()
3097 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
3100 * @flags: Flags related to GPIO edge, same value as used during enable call.
3113 return -ENODEV; in gpiod_disable_hw_timestamp_ns()
3115 if (!guard.gc->dis_hw_timestamp) { in gpiod_disable_hw_timestamp_ns()
3116 gpiod_warn(desc, "%s: hw ts not supported\n", __func__); in gpiod_disable_hw_timestamp_ns()
3117 return -ENOTSUPP; in gpiod_disable_hw_timestamp_ns()
3120 ret = guard.gc->dis_hw_timestamp(guard.gc, gpio_chip_hwgpio(desc), in gpiod_disable_hw_timestamp_ns()
3123 gpiod_warn(desc, "%s: hw ts release failed\n", __func__); in gpiod_disable_hw_timestamp_ns()
3131 * gpiod_set_config - sets @config for a GPIO
3136 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3169 * gpiod_set_debounce - sets @debounce time for a GPIO
3174 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3187 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3201 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); in gpiod_set_transitory()
3210 * gpiod_is_active_low - test whether a GPIO is active-low or not
3214 * 1 if the GPIO is active-low, 0 otherwise.
3219 return test_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_is_active_low()
3224 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
3230 change_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_toggle_active_low()
3239 lockdep_assert_held(&gc->gpiodev->srcu); in gpiochip_get()
3241 /* Make sure this is called after checking for gc->get(). */ in gpiochip_get()
3242 ret = gc->get(gc, offset); in gpiochip_get()
3244 ret = -EBADE; in gpiochip_get()
3251 return gc->get ? gpiochip_get(gc, gpio_chip_hwgpio(desc)) : -EIO; in gpio_chip_get_value()
3264 *------------------------------------------------------------------------
3266 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3283 gdev = desc->gdev; in gpiod_get_raw_value_commit()
3285 guard(srcu)(&gdev->srcu); in gpiod_get_raw_value_commit()
3287 gc = srcu_dereference(gdev->chip, &gdev->srcu); in gpiod_get_raw_value_commit()
3289 return -ENODEV; in gpiod_get_raw_value_commit()
3302 lockdep_assert_held(&gc->gpiodev->srcu); in gpio_chip_get_multiple()
3304 if (gc->get_multiple) { in gpio_chip_get_multiple()
3305 ret = gc->get_multiple(gc, mask, bits); in gpio_chip_get_multiple()
3307 return -EBADE; in gpio_chip_get_multiple()
3310 if (gc->get) { in gpio_chip_get_multiple()
3313 for_each_set_bit(i, mask, gc->ngpio) { in gpio_chip_get_multiple()
3321 return -EIO; in gpio_chip_get_multiple()
3327 guard(srcu)(&gdev->srcu); in gpio_device_chip_cmp()
3329 return gc == srcu_dereference(gdev->chip, &gdev->srcu); in gpio_device_chip_cmp()
3346 if (array_info && array_info->desc == desc_array && in gpiod_get_array_value_complex()
3347 array_size <= array_info->size && in gpiod_get_array_value_complex()
3348 (void *)array_info == desc_array + array_info->size) { in gpiod_get_array_value_complex()
3350 WARN_ON(array_info->gdev->can_sleep); in gpiod_get_array_value_complex()
3352 guard(srcu)(&array_info->gdev->srcu); in gpiod_get_array_value_complex()
3353 gc = srcu_dereference(array_info->gdev->chip, in gpiod_get_array_value_complex()
3354 &array_info->gdev->srcu); in gpiod_get_array_value_complex()
3356 return -ENODEV; in gpiod_get_array_value_complex()
3358 ret = gpio_chip_get_multiple(gc, array_info->get_mask, in gpiod_get_array_value_complex()
3363 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_get_array_value_complex()
3365 array_info->invert_mask, array_size); in gpiod_get_array_value_complex()
3367 i = find_first_zero_bit(array_info->get_mask, array_size); in gpiod_get_array_value_complex()
3382 return -ENODEV; in gpiod_get_array_value_complex()
3384 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_get_array_value_complex()
3390 mask = bitmap_alloc(guard.gc->ngpio, flags); in gpiod_get_array_value_complex()
3392 return -ENOMEM; in gpiod_get_array_value_complex()
3394 bits = bitmap_alloc(guard.gc->ngpio, flags); in gpiod_get_array_value_complex()
3397 return -ENOMEM; in gpiod_get_array_value_complex()
3401 bitmap_zero(mask, guard.gc->ngpio); in gpiod_get_array_value_complex()
3404 WARN_ON(guard.gc->can_sleep); in gpiod_get_array_value_complex()
3416 i = find_next_zero_bit(array_info->get_mask, in gpiod_get_array_value_complex()
3419 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); in gpiod_get_array_value_complex()
3435 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_array_value_complex()
3442 j = find_next_zero_bit(array_info->get_mask, i, in gpiod_get_array_value_complex()
3455 * gpiod_get_raw_value() - return a gpio's raw value
3462 * This function can be called from contexts where we cannot sleep, and will
3469 WARN_ON(desc->gdev->can_sleep); in gpiod_get_raw_value()
3475 * gpiod_get_value() - return a gpio's value
3482 * This function can be called from contexts where we cannot sleep, and will
3491 WARN_ON(desc->gdev->can_sleep); in gpiod_get_value()
3497 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value()
3505 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3514 * This function can be called from contexts where we cannot sleep,
3526 return -EINVAL; in gpiod_get_raw_array_value()
3534 * gpiod_get_array_value() - read values from an array of GPIOs
3543 * This function can be called from contexts where we cannot sleep,
3555 return -EINVAL; in gpiod_get_array_value()
3563 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3565 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3573 return -ENODEV; in gpio_set_open_drain_value_commit()
3580 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_drain_value_commit()
3585 "%s: Error in set_value for open drain err %d\n", in gpio_set_open_drain_value_commit()
3592 * _gpio_set_open_source_value() - Set the open source gpio's value.
3594 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3602 return -ENODEV; in gpio_set_open_source_value_commit()
3607 set_bit(FLAG_IS_OUT, &desc->flags); in gpio_set_open_source_value_commit()
3614 "%s: Error in set_value for open source err %d\n", in gpio_set_open_source_value_commit()
3622 if (unlikely(!test_bit(FLAG_IS_OUT, &desc->flags))) in gpiod_set_raw_value_commit()
3623 return -EPERM; in gpiod_set_raw_value_commit()
3627 return -ENODEV; in gpiod_set_raw_value_commit()
3635 * use the chip's set_multiple function if available;
3651 lockdep_assert_held(&gc->gpiodev->srcu); in gpiochip_set_multiple()
3653 if (gc->set_multiple_rv) { in gpiochip_set_multiple()
3654 ret = gc->set_multiple_rv(gc, mask, bits); in gpiochip_set_multiple()
3656 ret = -EBADE; in gpiochip_set_multiple()
3661 if (gc->set_multiple) { in gpiochip_set_multiple()
3662 gc->set_multiple(gc, mask, bits); in gpiochip_set_multiple()
3667 for_each_set_bit(i, mask, gc->ngpio) { in gpiochip_set_multiple()
3690 if (array_info && array_info->desc == desc_array && in gpiod_set_array_value_complex()
3691 array_size <= array_info->size && in gpiod_set_array_value_complex()
3692 (void *)array_info == desc_array + array_info->size) { in gpiod_set_array_value_complex()
3694 WARN_ON(array_info->gdev->can_sleep); in gpiod_set_array_value_complex()
3698 &desc_array[i]->flags))) in gpiod_set_array_value_complex()
3699 return -EPERM; in gpiod_set_array_value_complex()
3702 guard(srcu)(&array_info->gdev->srcu); in gpiod_set_array_value_complex()
3703 gc = srcu_dereference(array_info->gdev->chip, in gpiod_set_array_value_complex()
3704 &array_info->gdev->srcu); in gpiod_set_array_value_complex()
3706 return -ENODEV; in gpiod_set_array_value_complex()
3708 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) in gpiod_set_array_value_complex()
3710 array_info->invert_mask, array_size); in gpiod_set_array_value_complex()
3712 ret = gpiochip_set_multiple(gc, array_info->set_mask, in gpiod_set_array_value_complex()
3717 i = find_first_zero_bit(array_info->set_mask, array_size); in gpiod_set_array_value_complex()
3732 return -ENODEV; in gpiod_set_array_value_complex()
3734 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) { in gpiod_set_array_value_complex()
3740 mask = bitmap_alloc(guard.gc->ngpio, flags); in gpiod_set_array_value_complex()
3742 return -ENOMEM; in gpiod_set_array_value_complex()
3744 bits = bitmap_alloc(guard.gc->ngpio, flags); in gpiod_set_array_value_complex()
3747 return -ENOMEM; in gpiod_set_array_value_complex()
3751 bitmap_zero(mask, guard.gc->ngpio); in gpiod_set_array_value_complex()
3754 WARN_ON(guard.gc->can_sleep); in gpiod_set_array_value_complex()
3761 if (unlikely(!test_bit(FLAG_IS_OUT, &desc->flags))) in gpiod_set_array_value_complex()
3762 return -EPERM; in gpiod_set_array_value_complex()
3770 test_bit(i, array_info->invert_mask)) && in gpiod_set_array_value_complex()
3771 test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_array_value_complex()
3778 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3780 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { in gpiod_set_array_value_complex()
3790 i = find_next_zero_bit(array_info->set_mask, in gpiod_set_array_value_complex()
3793 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); in gpiod_set_array_value_complex()
3810 * gpiod_set_raw_value() - assign a gpio's raw value
3817 * This function can be called from contexts where we cannot sleep, and will
3827 WARN_ON(desc->gdev->can_sleep); in gpiod_set_raw_value()
3833 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3846 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_set_value_nocheck()
3849 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) in gpiod_set_value_nocheck()
3851 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) in gpiod_set_value_nocheck()
3858 * gpiod_set_value() - assign a gpio's value
3865 * This function can be called from contexts where we cannot sleep, and will
3875 WARN_ON(desc->gdev->can_sleep); in gpiod_set_value()
3881 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3890 * This function can be called from contexts where we cannot sleep, and will
3902 return -EINVAL; in gpiod_set_raw_array_value()
3909 * gpiod_set_array_value() - assign values to an array of GPIOs
3918 * This function can be called from contexts where we cannot sleep, and will
3930 return -EINVAL; in gpiod_set_array_value()
3938 * gpiod_cansleep() - report whether gpio value access may sleep
3942 * 0 for non-sleepable, 1 for sleepable, or an error code in case of error.
3947 return desc->gdev->can_sleep; in gpiod_cansleep()
3952 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3974 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3989 return -EINVAL; in gpiod_to_irq()
3991 gdev = desc->gdev; in gpiod_to_irq()
3993 guard(srcu)(&gdev->srcu); in gpiod_to_irq()
3994 gc = srcu_dereference(gdev->chip, &gdev->srcu); in gpiod_to_irq()
3996 return -ENODEV; in gpiod_to_irq()
3999 if (gc->to_irq) { in gpiod_to_irq()
4000 ret = gc->to_irq(gc, offset); in gpiod_to_irq()
4005 return -ENXIO; in gpiod_to_irq()
4008 if (gc->irq.chip) { in gpiod_to_irq()
4014 return -EPROBE_DEFER; in gpiod_to_irq()
4017 return -ENXIO; in gpiod_to_irq()
4022 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
4023 * @gc: the chip the GPIO to lock belongs to
4024 * @offset: the offset of the GPIO to lock as IRQ
4026 * This is used directly by GPIO drivers that want to lock down
4044 if (!gc->can_sleep && gc->get_direction) { in gpiochip_lock_as_irq()
4048 chip_err(gc, "%s: cannot get GPIO direction\n", in gpiochip_lock_as_irq()
4055 if (test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_lock_as_irq()
4056 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { in gpiochip_lock_as_irq()
4058 "%s: tried to flag a GPIO set as output for IRQ\n", in gpiochip_lock_as_irq()
4060 return -EIO; in gpiochip_lock_as_irq()
4063 set_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_lock_as_irq()
4064 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_lock_as_irq()
4071 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
4072 * @gc: the chip the GPIO to lock belongs to
4073 * @offset: the offset of the GPIO to lock as IRQ
4086 clear_bit(FLAG_USED_AS_IRQ, &desc->flags); in gpiochip_unlock_as_irq()
4087 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_unlock_as_irq()
4096 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) in gpiochip_disable_irq()
4097 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_disable_irq()
4106 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { in gpiochip_enable_irq()
4111 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && in gpiochip_enable_irq()
4112 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); in gpiochip_enable_irq()
4113 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); in gpiochip_enable_irq()
4120 if (offset >= gc->ngpio) in gpiochip_line_is_irq()
4123 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_irq()
4131 if (!try_module_get(gc->gpiodev->owner)) in gpiochip_reqres_irq()
4132 return -ENODEV; in gpiochip_reqres_irq()
4136 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset); in gpiochip_reqres_irq()
4137 module_put(gc->gpiodev->owner); in gpiochip_reqres_irq()
4147 module_put(gc->gpiodev->owner); in gpiochip_relres_irq()
4153 if (offset >= gc->ngpio) in gpiochip_line_is_open_drain()
4156 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_drain()
4162 if (offset >= gc->ngpio) in gpiochip_line_is_open_source()
4165 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_open_source()
4171 if (offset >= gc->ngpio) in gpiochip_line_is_persistent()
4174 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); in gpiochip_line_is_persistent()
4179 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4186 * This function is to be called from contexts that can sleep.
4197 * gpiod_get_value_cansleep() - return a gpio's value
4204 * This function is to be called from contexts that can sleep.
4216 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) in gpiod_get_value_cansleep()
4224 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
4233 * This function is to be called from contexts that can sleep.
4245 return -EINVAL; in gpiod_get_raw_array_value_cansleep()
4253 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
4262 * This function is to be called from contexts that can sleep.
4274 return -EINVAL; in gpiod_get_array_value_cansleep()
4282 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4289 * This function is to be called from contexts that can sleep.
4303 * gpiod_set_value_cansleep() - assign a gpio's value
4310 * This function is to be called from contexts that can sleep.
4324 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
4333 * This function is to be called from contexts that can sleep.
4345 return -EINVAL; in gpiod_set_raw_array_value_cansleep()
4352 * gpiod_add_lookup_tables() - register GPIO device consumers
4354 * @n: number of tables in the list
4356 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) in gpiod_add_lookup_tables() argument
4362 for (i = 0; i < n; i++) in gpiod_add_lookup_tables()
4363 list_add_tail(&tables[i]->list, &gpio_lookup_list); in gpiod_add_lookup_tables()
4367 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
4376 * This function is to be called from contexts that can sleep.
4388 return -EINVAL; in gpiod_set_array_value_cansleep()
4397 guard(read_lock_irqsave)(&desc->gdev->line_state_lock); in gpiod_line_state_notify()
4399 raw_notifier_call_chain(&desc->gdev->line_state_notifier, action, desc); in gpiod_line_state_notify()
4403 * gpiod_add_lookup_table() - register GPIO device consumers
4413 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4424 list_del(&table->list); in gpiod_remove_lookup_table()
4429 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4438 for (hog = &hogs[0]; hog->chip_label; hog++) { in gpiod_add_hogs()
4439 list_add_tail(&hog->list, &gpio_machine_hogs); in gpiod_add_hogs()
4446 gpio_device_find_by_label(hog->chip_label); in gpiod_add_hogs()
4459 for (hog = &hogs[0]; hog->chip_label; hog++) in gpiod_remove_hogs()
4460 list_del(&hog->list); in gpiod_remove_hogs()
4470 if (table->dev_id && dev_id) { in gpiod_find_lookup_table()
4475 if (!strcmp(table->dev_id, dev_id)) in gpiod_find_lookup_table()
4482 if (dev_id == table->dev_id) in gpiod_find_lookup_table()
4493 struct gpio_desc *desc = ERR_PTR(-ENOENT); in gpiod_find()
4504 for (p = &table->table[0]; p->key; p++) { in gpiod_find()
4506 if (p->idx != idx) in gpiod_find()
4510 if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) in gpiod_find()
4513 if (p->chip_hwnum == U16_MAX) { in gpiod_find()
4514 desc = gpio_name_to_desc(p->key); in gpiod_find()
4516 *flags = p->flags; in gpiod_find()
4520 dev_warn(dev, "cannot find GPIO line %s, deferring\n", in gpiod_find()
4521 p->key); in gpiod_find()
4522 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
4526 gpio_device_find_by_label(p->key); in gpiod_find()
4530 * p->key should exist, assume it may in gpiod_find()
4535 dev_warn(dev, "cannot find GPIO chip %s, deferring\n", in gpiod_find()
4536 p->key); in gpiod_find()
4537 return ERR_PTR(-EPROBE_DEFER); in gpiod_find()
4542 if (gc->ngpio <= p->chip_hwnum) { in gpiod_find()
4544 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n", in gpiod_find()
4545 idx, p->chip_hwnum, gc->ngpio - 1, in gpiod_find()
4546 gc->label); in gpiod_find()
4547 return ERR_PTR(-EINVAL); in gpiod_find()
4550 desc = gpio_device_get_desc(gdev, p->chip_hwnum); in gpiod_find()
4551 *flags = p->flags; in gpiod_find()
4568 return -ENOENT; in platform_gpio_count()
4570 for (p = &table->table[0]; p->key; p++) { in platform_gpio_count()
4571 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || in platform_gpio_count()
4572 (!con_id && !p->con_id)) in platform_gpio_count()
4578 return -ENOENT; in platform_gpio_count()
4591 struct gpio_desc *desc = ERR_PTR(-ENOENT); in gpiod_find_by_fwnode()
4594 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n", fwnode, name); in gpiod_find_by_fwnode()
4597 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n", fwnode, name); in gpiod_find_by_fwnode()
4600 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n", fwnode, name); in gpiod_find_by_fwnode()
4634 "using lookup tables for GPIO lookup\n"); in gpiod_find_and_request()
4639 dev_dbg(consumer, "No GPIO consumer %s found\n", name); in gpiod_find_and_request()
4650 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) in gpiod_find_and_request()
4661 dev_info(consumer, "nonexclusive access to GPIO for %s\n", name); in gpiod_find_and_request()
4668 dev_err(consumer, "setup of GPIO %s failed: %d\n", name, ret); in gpiod_find_and_request()
4678 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4680 * @con_id: function within the GPIO consumer
4685 * This function can be used for drivers that get their configuration
4688 * The function properly finds the corresponding GPIO using whatever is the
4709 * gpiod_count - return the number of GPIOs associated with a device / function
4710 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4711 * @con_id: function within the GPIO consumer
4714 * The number of GPIOs associated with a device / function or -ENOENT if no
4715 * GPIO has been assigned to the requested function.
4720 int count = -ENOENT; in gpiod_count()
4737 * gpiod_get - obtain a GPIO for a given GPIO function
4738 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4739 * @con_id: function within the GPIO consumer
4743 * The GPIO descriptor corresponding to the function @con_id of device
4744 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4755 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4756 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4757 * @con_id: function within the GPIO consumer
4761 * the requested function it will return NULL. This is convenient for drivers
4765 * The GPIO descriptor corresponding to the function @con_id of device
4766 * dev, NULL if no GPIO has been assigned to the requested function, or
4779 * gpiod_configure_flags - helper function to configure a given GPIO
4781 * @con_id: function within the GPIO consumer
4782 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4784 * @dflags: gpiod_flags - optional GPIO initialization flags
4787 * 0 on success, -ENOENT if no GPIO has been assigned to the
4788 * requested function and/or index, or another IS_ERR() code if an error
4798 set_bit(FLAG_ACTIVE_LOW, &desc->flags); in gpiod_configure_flags()
4801 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
4809 set_bit(FLAG_OPEN_DRAIN, &desc->flags); in gpiod_configure_flags()
4811 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n"); in gpiod_configure_flags()
4815 set_bit(FLAG_OPEN_SOURCE, &desc->flags); in gpiod_configure_flags()
4821 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n"); in gpiod_configure_flags()
4822 return -EINVAL; in gpiod_configure_flags()
4826 set_bit(FLAG_PULL_UP, &desc->flags); in gpiod_configure_flags()
4828 set_bit(FLAG_PULL_DOWN, &desc->flags); in gpiod_configure_flags()
4830 set_bit(FLAG_BIAS_DISABLE, &desc->flags); in gpiod_configure_flags()
4838 gpiod_dbg(desc, "no flags found for GPIO %s\n", name); in gpiod_configure_flags()
4853 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4854 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4855 * @con_id: function within the GPIO consumer
4863 * A valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4864 * requested function and/or index, or another IS_ERR() code if an error
4881 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4882 * function
4883 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4884 * @con_id: function within the GPIO consumer
4889 * specified index was assigned to the requested function it will return NULL.
4894 * requested function and/or index, or another IS_ERR() code if an error
4913 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4916 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4918 * @dflags: gpiod_flags - optional GPIO initialization flags
4926 struct gpio_device *gdev = desc->gdev; in gpiod_hog()
4933 return -ENODEV; in gpiod_hog()
4935 if (test_and_set_bit(FLAG_IS_HOGGED, &desc->flags)) in gpiod_hog()
4943 clear_bit(FLAG_IS_HOGGED, &desc->flags); in gpiod_hog()
4945 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", in gpiod_hog()
4946 name, gdev->label, hwnum, ret); in gpiod_hog()
4950 gpiod_dbg(desc, "hogged as %s/%s\n", in gpiod_hog()
4959 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4971 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4972 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4973 * @con_id: function within the GPIO consumer
4976 * This function acquires all the GPIOs defined under a given function.
4979 * The GPIO descriptors corresponding to the function @con_id of device
4980 * dev, -ENOENT if no GPIO has been assigned to the requested function,
5003 return ERR_PTR(-ENOMEM); in gpiod_get_array()
5005 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) { in gpiod_get_array()
5006 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); in gpiod_get_array()
5012 descs->desc[descs->ndescs] = desc; in gpiod_get_array()
5019 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { in gpiod_get_array()
5022 bitmap_size = BITS_TO_LONGS(gdev->ngpio > count ? in gpiod_get_array()
5023 gdev->ngpio : count); in gpiod_get_array()
5030 return ERR_PTR(-ENOMEM); in gpiod_get_array()
5036 array_info->get_mask = array_info->invert_mask + in gpiod_get_array()
5038 array_info->set_mask = array_info->get_mask + in gpiod_get_array()
5041 array_info->desc = descs->desc; in gpiod_get_array()
5042 array_info->size = count; in gpiod_get_array()
5043 array_info->gdev = gdev; in gpiod_get_array()
5044 bitmap_set(array_info->get_mask, descs->ndescs, in gpiod_get_array()
5045 count - descs->ndescs); in gpiod_get_array()
5046 bitmap_set(array_info->set_mask, descs->ndescs, in gpiod_get_array()
5047 count - descs->ndescs); in gpiod_get_array()
5048 descs->info = array_info; in gpiod_get_array()
5056 if (array_info->gdev != gdev) { in gpiod_get_array()
5057 __clear_bit(descs->ndescs, array_info->get_mask); in gpiod_get_array()
5058 __clear_bit(descs->ndescs, array_info->set_mask); in gpiod_get_array()
5061 * Detect array members which belong to the 'fast' chip in gpiod_get_array()
5064 else if (gpio_chip_hwgpio(desc) != descs->ndescs) { in gpiod_get_array()
5070 if (bitmap_full(array_info->get_mask, descs->ndescs)) { in gpiod_get_array()
5073 __clear_bit(descs->ndescs, in gpiod_get_array()
5074 array_info->get_mask); in gpiod_get_array()
5075 __clear_bit(descs->ndescs, in gpiod_get_array()
5076 array_info->set_mask); in gpiod_get_array()
5079 dflags = READ_ONCE(desc->flags); in gpiod_get_array()
5083 __clear_bit(descs->ndescs, in gpiod_get_array()
5084 array_info->set_mask); in gpiod_get_array()
5087 __set_bit(descs->ndescs, in gpiod_get_array()
5088 array_info->invert_mask); in gpiod_get_array()
5093 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n", in gpiod_get_array()
5094 array_info->gdev->label, array_info->size, in gpiod_get_array()
5095 *array_info->get_mask, *array_info->set_mask, in gpiod_get_array()
5096 *array_info->invert_mask); in gpiod_get_array()
5102 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
5103 * function
5104 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5105 * @con_id: function within the GPIO consumer
5109 * assigned to the requested function it will return NULL.
5112 * The GPIO descriptors corresponding to the function @con_id of device
5113 * dev, NULL if no GPIO has been assigned to the requested function,
5132 * gpiod_put - dispose of a GPIO descriptor
5144 * gpiod_put_array - dispose of multiple GPIO descriptors
5151 for (i = 0; i < descs->ndescs; i++) in gpiod_put_array()
5152 gpiod_put(descs->desc[i]); in gpiod_put_array()
5188 pr_err("gpiolib: could not register GPIO bus type\n"); in gpiolib_dev_init()
5194 pr_err("gpiolib: could not register GPIO stub driver\n"); in gpiolib_dev_init()
5201 pr_err("gpiolib: failed to allocate char dev region\n"); in gpiolib_dev_init()
5223 unsigned int gpio = gdev->base; in gpiolib_dbg_show()
5229 guard(srcu)(&gdev->srcu); in gpiolib_dbg_show()
5231 gc = srcu_dereference(gdev->chip, &gdev->srcu); in gpiolib_dbg_show()
5233 seq_puts(s, "Underlying GPIO chip is gone\n"); in gpiolib_dbg_show()
5238 guard(srcu)(&desc->gdev->desc_srcu); in gpiolib_dbg_show()
5239 flags = READ_ONCE(desc->flags); in gpiolib_dbg_show()
5246 seq_printf(s, " gpio-%-3u (%-20.20s|%-20.20s) %s %s %s%s\n", in gpiolib_dbg_show()
5247 gpio, desc->name ?: "", gpiod_get_label(desc), in gpiolib_dbg_show()
5252 } else if (desc->name) { in gpiolib_dbg_show()
5253 seq_printf(s, " gpio-%-3u (%-20.20s)\n", gpio, desc->name); in gpiolib_dbg_show()
5275 s->private = priv; in gpiolib_seq_start()
5277 priv->newline = true; in gpiolib_seq_start()
5278 priv->idx = srcu_read_lock(&gpio_devices_srcu); in gpiolib_seq_start()
5282 if (index-- == 0) in gpiolib_seq_start()
5291 struct gpiolib_seq_priv *priv = s->private; in gpiolib_seq_next()
5294 next = list_entry_rcu(gdev->list.next, struct gpio_device, list); in gpiolib_seq_next()
5295 gdev = &next->list == &gpio_devices ? NULL : next; in gpiolib_seq_next()
5296 priv->newline = true; in gpiolib_seq_next()
5304 struct gpiolib_seq_priv *priv = s->private; in gpiolib_seq_stop()
5306 srcu_read_unlock(&gpio_devices_srcu, priv->idx); in gpiolib_seq_stop()
5312 struct gpiolib_seq_priv *priv = s->private; in gpiolib_seq_show()
5317 if (priv->newline) in gpiolib_seq_show()
5318 seq_putc(s, '\n'); in gpiolib_seq_show()
5320 guard(srcu)(&gdev->srcu); in gpiolib_seq_show()
5322 gc = srcu_dereference(gdev->chip, &gdev->srcu); in gpiolib_seq_show()
5324 seq_printf(s, "%s: (dangling chip)\n", dev_name(&gdev->dev)); in gpiolib_seq_show()
5328 seq_printf(s, "%s: GPIOs %u-%u", dev_name(&gdev->dev), gdev->base, in gpiolib_seq_show()
5329 gdev->base + gdev->ngpio - 1); in gpiolib_seq_show()
5330 parent = gc->parent; in gpiolib_seq_show()
5333 parent->bus ? parent->bus->name : "no-bus", in gpiolib_seq_show()
5335 if (gc->label) in gpiolib_seq_show()
5336 seq_printf(s, ", %s", gc->label); in gpiolib_seq_show()
5337 if (gc->can_sleep) in gpiolib_seq_show()
5339 seq_printf(s, ":\n"); in gpiolib_seq_show()
5341 if (gc->dbg_show) in gpiolib_seq_show()
5342 gc->dbg_show(s, gc); in gpiolib_seq_show()