^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * devres.c - managed gpio resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This file is based on kernel/irq/devres.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2011 John Crispin <john@phrozen.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "gpiolib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static void devm_gpiod_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct gpio_desc **desc = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) gpiod_put(*desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static int devm_gpiod_match(struct device *dev, void *res, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct gpio_desc **this = res, **gpio = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return *this == *gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static void devm_gpiod_release_array(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct gpio_descs **descs = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) gpiod_put_array(*descs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int devm_gpiod_match_array(struct device *dev, void *res, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct gpio_descs **this = res, **gpios = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return *this == *gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * devm_gpiod_get - Resource-managed gpiod_get()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @con_id: function within the GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @flags: optional GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Managed gpiod_get(). GPIO descriptors returned from this function are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * automatically disposed on driver detach. See gpiod_get() for detailed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * information about behavior and return values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) enum gpiod_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return devm_gpiod_get_index(dev, con_id, 0, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) EXPORT_SYMBOL_GPL(devm_gpiod_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @con_id: function within the GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * @flags: optional GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Managed gpiod_get_optional(). GPIO descriptors returned from this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * are automatically disposed on driver detach. See gpiod_get_optional() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * detailed information about behavior and return values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) enum gpiod_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) EXPORT_SYMBOL_GPL(devm_gpiod_get_optional);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * devm_gpiod_get_index - Resource-managed gpiod_get_index()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @con_id: function within the GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * @idx: index of the GPIO to obtain in the consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @flags: optional GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Managed gpiod_get_index(). GPIO descriptors returned from this function are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * automatically disposed on driver detach. See gpiod_get_index() for detailed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * information about behavior and return values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) unsigned int idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) enum gpiod_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct gpio_desc **dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) desc = gpiod_get_index(dev, con_id, idx, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * For non-exclusive GPIO descriptors, check if this descriptor is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * already under resource management by this device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct devres *dres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dres = devres_find(dev, devm_gpiod_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) devm_gpiod_match, &desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (dres)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!dr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) gpiod_put(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *dr = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) devres_add(dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) EXPORT_SYMBOL_GPL(devm_gpiod_get_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * devm_gpiod_get_from_of_node() - obtain a GPIO from an OF node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * @dev: device for lifecycle management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @node: handle of the OF node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * @propname: name of the DT property representing the GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * @index: index of the GPIO to obtain for the consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * @dflags: GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * @label: label to attach to the requested GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * On successful request the GPIO pin is configured in accordance with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * provided @dflags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * In case of error an ERR_PTR() is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) const char *propname, int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) enum gpiod_flags dflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct gpio_desc **dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) desc = gpiod_get_from_of_node(node, propname, index, dflags, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * For non-exclusive GPIO descriptors, check if this descriptor is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * already under resource management by this device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct devres *dres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) dres = devres_find(dev, devm_gpiod_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) devm_gpiod_match, &desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (dres)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (!dr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) gpiod_put(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) *dr = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) devres_add(dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) EXPORT_SYMBOL_GPL(devm_gpiod_get_from_of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * devm_fwnode_gpiod_get_index - get a GPIO descriptor from a given node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * @fwnode: firmware node containing GPIO reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * @con_id: function within the GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * @index: index of the GPIO to obtain in the consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * @flags: GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * @label: label to attach to the requested GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * GPIO descriptors returned from this function are automatically disposed on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * driver detach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * On successful request the GPIO pin is configured in accordance with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * provided @flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct fwnode_handle *fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) const char *con_id, int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) enum gpiod_flags flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct gpio_desc **dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) desc = fwnode_gpiod_get_index(fwnode, con_id, index, flags, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (IS_ERR(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) devres_free(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) *dr = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) devres_add(dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) EXPORT_SYMBOL_GPL(devm_fwnode_gpiod_get_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * @con_id: function within the GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * @index: index of the GPIO to obtain in the consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * @flags: optional GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * function are automatically disposed on driver detach. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * gpiod_get_index_optional() for detailed information about behavior and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * return values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) unsigned int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) enum gpiod_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) desc = devm_gpiod_get_index(dev, con_id, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (IS_ERR(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (PTR_ERR(desc) == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) EXPORT_SYMBOL_GPL(devm_gpiod_get_index_optional);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * devm_gpiod_get_array - Resource-managed gpiod_get_array()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @con_id: function within the GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @flags: optional GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * Managed gpiod_get_array(). GPIO descriptors returned from this function are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * automatically disposed on driver detach. See gpiod_get_array() for detailed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * information about behavior and return values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) enum gpiod_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct gpio_descs **dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct gpio_descs *descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) dr = devres_alloc(devm_gpiod_release_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) sizeof(struct gpio_descs *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) descs = gpiod_get_array(dev, con_id, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (IS_ERR(descs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) devres_free(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) *dr = descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) devres_add(dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) EXPORT_SYMBOL_GPL(devm_gpiod_get_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * @con_id: function within the GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * @flags: optional GPIO initialization flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * function are automatically disposed on driver detach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * See gpiod_get_array_optional() for detailed information about behavior and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * return values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct gpio_descs *__must_check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) enum gpiod_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct gpio_descs *descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) descs = devm_gpiod_get_array(dev, con_id, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (PTR_ERR(descs) == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) EXPORT_SYMBOL_GPL(devm_gpiod_get_array_optional);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * devm_gpiod_put - Resource-managed gpiod_put()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * @desc: GPIO descriptor to dispose of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * will be disposed of by the resource management code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) &desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) EXPORT_SYMBOL_GPL(devm_gpiod_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * devm_gpiod_unhinge - Remove resource management from a gpio descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * @desc: GPIO descriptor to remove resource management from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * Remove resource management from a GPIO descriptor. This is needed when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * you want to hand over lifecycle management of a descriptor to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * mechanism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (IS_ERR_OR_NULL(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) ret = devres_destroy(dev, devm_gpiod_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) devm_gpiod_match, &desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * If the GPIO descriptor is requested as nonexclusive, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * may call this function several times on the same descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * so it is OK if devres_destroy() returns -ENOENT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /* Anything else we should warn about */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) WARN_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) EXPORT_SYMBOL_GPL(devm_gpiod_unhinge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * devm_gpiod_put_array - Resource-managed gpiod_put_array()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * @dev: GPIO consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * @descs: GPIO descriptor array to dispose of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * Normally this function will not be called as the GPIOs will be disposed of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * by the resource management code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) WARN_ON(devres_release(dev, devm_gpiod_release_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) devm_gpiod_match_array, &descs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) EXPORT_SYMBOL_GPL(devm_gpiod_put_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static void devm_gpio_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) unsigned *gpio = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) gpio_free(*gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static int devm_gpio_match(struct device *dev, void *res, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) unsigned *this = res, *gpio = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return *this == *gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * devm_gpio_request - request a GPIO for a managed device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * @dev: device to request the GPIO for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * @gpio: GPIO to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * @label: the name of the requested GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * Except for the extra @dev argument, this function takes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * same arguments and performs the same function as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * gpio_request(). GPIOs requested with this function will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * automatically freed on driver detach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * If an GPIO allocated with this function needs to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * separately, devm_gpio_free() must be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) unsigned *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) rc = gpio_request(gpio, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) devres_free(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) *dr = gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) devres_add(dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) EXPORT_SYMBOL_GPL(devm_gpio_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * devm_gpio_request_one - request a single GPIO with initial setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * @dev: device to request for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * @gpio: the GPIO number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * @flags: GPIO configuration as specified by GPIOF_*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * @label: a literal description string of this GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int devm_gpio_request_one(struct device *dev, unsigned gpio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) unsigned long flags, const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) unsigned *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) rc = gpio_request_one(gpio, flags, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) devres_free(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) *dr = gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) devres_add(dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) EXPORT_SYMBOL_GPL(devm_gpio_request_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * devm_gpio_free - free a GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * @dev: device to free GPIO for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * @gpio: GPIO to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * Except for the extra @dev argument, this function takes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * same arguments and performs the same function as gpio_free().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * This function instead of gpio_free() should be used to manually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * free GPIOs allocated with devm_gpio_request().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) void devm_gpio_free(struct device *dev, unsigned int gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) &gpio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) EXPORT_SYMBOL_GPL(devm_gpio_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static void devm_gpio_chip_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct gpio_chip *gc = *(struct gpio_chip **)res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) gpiochip_remove(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * devm_gpiochip_add_data_with_key() - Resource managed gpiochip_add_data_with_key()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * @dev: pointer to the device that gpio_chip belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * @gc: the GPIO chip to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * @data: driver-private data associated with this chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * @lock_key: lockdep class for IRQ lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * @request_key: lockdep class for IRQ request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * Context: potentially before irqs will work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * The gpio chip automatically be released when the device is unbound.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * A negative errno if the chip can't be registered, such as because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * gc->base is invalid or already associated with a different chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * Otherwise it returns zero as a success code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) struct lock_class_key *lock_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct lock_class_key *request_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct gpio_chip **ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) ret = gpiochip_add_data_with_key(gc, data, lock_key, request_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) *ptr = gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) EXPORT_SYMBOL_GPL(devm_gpiochip_add_data_with_key);