1 /* SPDX-License-Identifier: GPL-2.0 */
3 * devres.c - managed gpio resources
4 * This file is based on kernel/irq/devres.c
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/export.h>
12 #include <linux/gfp.h>
13 #include <linux/types.h>
15 #include <linux/gpio/consumer.h>
20 struct lock_class_key;
22 static void devm_gpiod_release(struct device *dev, void *res)
24 struct gpio_desc **desc = res;
29 static int devm_gpiod_match(struct device *dev, void *res, void *data)
31 struct gpio_desc **this = res, **gpio = data;
33 return *this == *gpio;
36 static void devm_gpiod_release_array(struct device *dev, void *res)
38 struct gpio_descs **descs = res;
40 gpiod_put_array(*descs);
43 static int devm_gpiod_match_array(struct device *dev, void *res, void *data)
45 struct gpio_descs **this = res, **gpios = data;
47 return *this == *gpios;
51 * devm_gpiod_get - Resource-managed gpiod_get()
53 * @con_id: function within the GPIO consumer
54 * @flags: optional GPIO initialization flags
56 * Managed gpiod_get(). GPIO descriptors returned from this function are
57 * automatically disposed on driver detach. See gpiod_get() for detailed
58 * information about behavior and return values.
61 * The GPIO descriptor corresponding to the function @con_id of device
62 * dev, %-ENOENT if no GPIO has been assigned to the requested function, or
63 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
65 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
67 enum gpiod_flags flags)
69 return devm_gpiod_get_index(dev, con_id, 0, flags);
71 EXPORT_SYMBOL_GPL(devm_gpiod_get);
74 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
76 * @con_id: function within the GPIO consumer
77 * @flags: optional GPIO initialization flags
79 * Managed gpiod_get_optional(). GPIO descriptors returned from this function
80 * are automatically disposed on driver detach. See gpiod_get_optional() for
81 * detailed information about behavior and return values.
84 * The GPIO descriptor corresponding to the function @con_id of device
85 * dev, NULL if no GPIO has been assigned to the requested function, or
86 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
88 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
90 enum gpiod_flags flags)
92 return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
94 EXPORT_SYMBOL_GPL(devm_gpiod_get_optional);
97 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
99 * @con_id: function within the GPIO consumer
100 * @idx: index of the GPIO to obtain in the consumer
101 * @flags: optional GPIO initialization flags
103 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
104 * automatically disposed on driver detach. See gpiod_get_index() for detailed
105 * information about behavior and return values.
108 * The GPIO descriptor corresponding to the function @con_id of device
109 * dev, %-ENOENT if no GPIO has been assigned to the requested function, or
110 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
112 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
115 enum gpiod_flags flags)
117 struct gpio_desc **dr;
118 struct gpio_desc *desc;
120 desc = gpiod_get_index(dev, con_id, idx, flags);
125 * For non-exclusive GPIO descriptors, check if this descriptor is
126 * already under resource management by this device.
128 if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
131 dres = devres_find(dev, devm_gpiod_release,
132 devm_gpiod_match, &desc);
137 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
141 return ERR_PTR(-ENOMEM);
149 EXPORT_SYMBOL_GPL(devm_gpiod_get_index);
152 * devm_fwnode_gpiod_get_index - get a GPIO descriptor from a given node
153 * @dev: GPIO consumer
154 * @fwnode: firmware node containing GPIO reference
155 * @con_id: function within the GPIO consumer
156 * @index: index of the GPIO to obtain in the consumer
157 * @flags: GPIO initialization flags
158 * @label: label to attach to the requested GPIO
160 * GPIO descriptors returned from this function are automatically disposed on
164 * The GPIO descriptor corresponding to the function @con_id of device
165 * dev, %-ENOENT if no GPIO has been assigned to the requested function, or
166 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
168 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
169 struct fwnode_handle *fwnode,
170 const char *con_id, int index,
171 enum gpiod_flags flags,
174 struct gpio_desc **dr;
175 struct gpio_desc *desc;
177 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
180 return ERR_PTR(-ENOMEM);
182 desc = gpiod_find_and_request(dev, fwnode, con_id, index, flags, label, false);
193 EXPORT_SYMBOL_GPL(devm_fwnode_gpiod_get_index);
196 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
197 * @dev: GPIO consumer
198 * @con_id: function within the GPIO consumer
199 * @index: index of the GPIO to obtain in the consumer
200 * @flags: optional GPIO initialization flags
202 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
203 * function are automatically disposed on driver detach. See
204 * gpiod_get_index_optional() for detailed information about behavior and
208 * The GPIO descriptor corresponding to the function @con_id of device
209 * dev, %NULL if no GPIO has been assigned to the requested function, or
210 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
212 struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
215 enum gpiod_flags flags)
217 struct gpio_desc *desc;
219 desc = devm_gpiod_get_index(dev, con_id, index, flags);
220 if (gpiod_not_found(desc))
225 EXPORT_SYMBOL_GPL(devm_gpiod_get_index_optional);
228 * devm_gpiod_get_array - Resource-managed gpiod_get_array()
229 * @dev: GPIO consumer
230 * @con_id: function within the GPIO consumer
231 * @flags: optional GPIO initialization flags
233 * Managed gpiod_get_array(). GPIO descriptors returned from this function are
234 * automatically disposed on driver detach. See gpiod_get_array() for detailed
235 * information about behavior and return values.
238 * The GPIO descriptors corresponding to the function @con_id of device
239 * dev, %-ENOENT if no GPIO has been assigned to the requested function,
240 * or another IS_ERR() code if an error occurred while trying to acquire
243 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
245 enum gpiod_flags flags)
247 struct gpio_descs **dr;
248 struct gpio_descs *descs;
250 dr = devres_alloc(devm_gpiod_release_array,
251 sizeof(struct gpio_descs *), GFP_KERNEL);
253 return ERR_PTR(-ENOMEM);
255 descs = gpiod_get_array(dev, con_id, flags);
266 EXPORT_SYMBOL_GPL(devm_gpiod_get_array);
269 * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
270 * @dev: GPIO consumer
271 * @con_id: function within the GPIO consumer
272 * @flags: optional GPIO initialization flags
274 * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
275 * function are automatically disposed on driver detach.
276 * See gpiod_get_array_optional() for detailed information about behavior and
280 * The GPIO descriptors corresponding to the function @con_id of device
281 * dev, %NULL if no GPIO has been assigned to the requested function,
282 * or another IS_ERR() code if an error occurred while trying to acquire
285 struct gpio_descs *__must_check
286 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
287 enum gpiod_flags flags)
289 struct gpio_descs *descs;
291 descs = devm_gpiod_get_array(dev, con_id, flags);
292 if (gpiod_not_found(descs))
297 EXPORT_SYMBOL_GPL(devm_gpiod_get_array_optional);
300 * devm_gpiod_put - Resource-managed gpiod_put()
301 * @dev: GPIO consumer
302 * @desc: GPIO descriptor to dispose of
304 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
305 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
306 * will be disposed of by the resource management code.
308 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
310 WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
313 EXPORT_SYMBOL_GPL(devm_gpiod_put);
316 * devm_gpiod_unhinge - Remove resource management from a gpio descriptor
317 * @dev: GPIO consumer
318 * @desc: GPIO descriptor to remove resource management from
320 * Remove resource management from a GPIO descriptor. This is needed when
321 * you want to hand over lifecycle management of a descriptor to another
325 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc)
329 if (IS_ERR_OR_NULL(desc))
331 ret = devres_destroy(dev, devm_gpiod_release,
332 devm_gpiod_match, &desc);
334 * If the GPIO descriptor is requested as nonexclusive, we
335 * may call this function several times on the same descriptor
336 * so it is OK if devres_destroy() returns -ENOENT.
340 /* Anything else we should warn about */
343 EXPORT_SYMBOL_GPL(devm_gpiod_unhinge);
346 * devm_gpiod_put_array - Resource-managed gpiod_put_array()
347 * @dev: GPIO consumer
348 * @descs: GPIO descriptor array to dispose of
350 * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
351 * Normally this function will not be called as the GPIOs will be disposed of
352 * by the resource management code.
354 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
356 WARN_ON(devres_release(dev, devm_gpiod_release_array,
357 devm_gpiod_match_array, &descs));
359 EXPORT_SYMBOL_GPL(devm_gpiod_put_array);
361 static void devm_gpio_chip_release(void *data)
363 struct gpio_chip *gc = data;
369 * devm_gpiochip_add_data_with_key() - Resource managed gpiochip_add_data_with_key()
370 * @dev: pointer to the device that gpio_chip belongs to.
371 * @gc: the GPIO chip to register
372 * @data: driver-private data associated with this chip
373 * @lock_key: lockdep class for IRQ lock
374 * @request_key: lockdep class for IRQ request
376 * Context: potentially before irqs will work
378 * The gpio chip automatically be released when the device is unbound.
381 * A negative errno if the chip can't be registered, such as because the
382 * gc->base is invalid or already associated with a different chip.
383 * Otherwise it returns zero as a success code.
385 int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
386 struct lock_class_key *lock_key,
387 struct lock_class_key *request_key)
391 ret = gpiochip_add_data_with_key(gc, data, lock_key, request_key);
395 return devm_add_action_or_reset(dev, devm_gpio_chip_release, gc);
397 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data_with_key);