1 /* SPDX-License-Identifier: GPL-2.0 */
3 * devres.c - managed gpio resources
4 * This file is based on kernel/irq/devres.c
6 * Copyright (c) 2011 John Crispin <john@phrozen.org>
9 #include <linux/module.h>
10 #include <linux/err.h>
11 #include <linux/gpio.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/device.h>
14 #include <linux/gfp.h>
18 static void devm_gpiod_release(struct device *dev, void *res)
20 struct gpio_desc **desc = res;
25 static int devm_gpiod_match(struct device *dev, void *res, void *data)
27 struct gpio_desc **this = res, **gpio = data;
29 return *this == *gpio;
32 static void devm_gpiod_release_array(struct device *dev, void *res)
34 struct gpio_descs **descs = res;
36 gpiod_put_array(*descs);
39 static int devm_gpiod_match_array(struct device *dev, void *res, void *data)
41 struct gpio_descs **this = res, **gpios = data;
43 return *this == *gpios;
47 * devm_gpiod_get - Resource-managed gpiod_get()
49 * @con_id: function within the GPIO consumer
50 * @flags: optional GPIO initialization flags
52 * Managed gpiod_get(). GPIO descriptors returned from this function are
53 * automatically disposed on driver detach. See gpiod_get() for detailed
54 * information about behavior and return values.
56 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
58 enum gpiod_flags flags)
60 return devm_gpiod_get_index(dev, con_id, 0, flags);
62 EXPORT_SYMBOL_GPL(devm_gpiod_get);
65 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
67 * @con_id: function within the GPIO consumer
68 * @flags: optional GPIO initialization flags
70 * Managed gpiod_get_optional(). GPIO descriptors returned from this function
71 * are automatically disposed on driver detach. See gpiod_get_optional() for
72 * detailed information about behavior and return values.
74 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
76 enum gpiod_flags flags)
78 return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
80 EXPORT_SYMBOL_GPL(devm_gpiod_get_optional);
83 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
85 * @con_id: function within the GPIO consumer
86 * @idx: index of the GPIO to obtain in the consumer
87 * @flags: optional GPIO initialization flags
89 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
90 * automatically disposed on driver detach. See gpiod_get_index() for detailed
91 * information about behavior and return values.
93 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
96 enum gpiod_flags flags)
98 struct gpio_desc **dr;
99 struct gpio_desc *desc;
101 desc = gpiod_get_index(dev, con_id, idx, flags);
106 * For non-exclusive GPIO descriptors, check if this descriptor is
107 * already under resource management by this device.
109 if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
112 dres = devres_find(dev, devm_gpiod_release,
113 devm_gpiod_match, &desc);
118 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
122 return ERR_PTR(-ENOMEM);
130 EXPORT_SYMBOL_GPL(devm_gpiod_get_index);
133 * devm_fwnode_gpiod_get_index - get a GPIO descriptor from a given node
134 * @dev: GPIO consumer
135 * @fwnode: firmware node containing GPIO reference
136 * @con_id: function within the GPIO consumer
137 * @index: index of the GPIO to obtain in the consumer
138 * @flags: GPIO initialization flags
139 * @label: label to attach to the requested GPIO
141 * GPIO descriptors returned from this function are automatically disposed on
144 * On successful request the GPIO pin is configured in accordance with
147 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
148 struct fwnode_handle *fwnode,
149 const char *con_id, int index,
150 enum gpiod_flags flags,
153 struct gpio_desc **dr;
154 struct gpio_desc *desc;
156 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
159 return ERR_PTR(-ENOMEM);
161 desc = fwnode_gpiod_get_index(fwnode, con_id, index, flags, label);
172 EXPORT_SYMBOL_GPL(devm_fwnode_gpiod_get_index);
175 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
176 * @dev: GPIO consumer
177 * @con_id: function within the GPIO consumer
178 * @index: index of the GPIO to obtain in the consumer
179 * @flags: optional GPIO initialization flags
181 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
182 * function are automatically disposed on driver detach. See
183 * gpiod_get_index_optional() for detailed information about behavior and
186 struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
189 enum gpiod_flags flags)
191 struct gpio_desc *desc;
193 desc = devm_gpiod_get_index(dev, con_id, index, flags);
194 if (gpiod_not_found(desc))
199 EXPORT_SYMBOL_GPL(devm_gpiod_get_index_optional);
202 * devm_gpiod_get_array - Resource-managed gpiod_get_array()
203 * @dev: GPIO consumer
204 * @con_id: function within the GPIO consumer
205 * @flags: optional GPIO initialization flags
207 * Managed gpiod_get_array(). GPIO descriptors returned from this function are
208 * automatically disposed on driver detach. See gpiod_get_array() for detailed
209 * information about behavior and return values.
211 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
213 enum gpiod_flags flags)
215 struct gpio_descs **dr;
216 struct gpio_descs *descs;
218 dr = devres_alloc(devm_gpiod_release_array,
219 sizeof(struct gpio_descs *), GFP_KERNEL);
221 return ERR_PTR(-ENOMEM);
223 descs = gpiod_get_array(dev, con_id, flags);
234 EXPORT_SYMBOL_GPL(devm_gpiod_get_array);
237 * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
238 * @dev: GPIO consumer
239 * @con_id: function within the GPIO consumer
240 * @flags: optional GPIO initialization flags
242 * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
243 * function are automatically disposed on driver detach.
244 * See gpiod_get_array_optional() for detailed information about behavior and
247 struct gpio_descs *__must_check
248 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
249 enum gpiod_flags flags)
251 struct gpio_descs *descs;
253 descs = devm_gpiod_get_array(dev, con_id, flags);
254 if (gpiod_not_found(descs))
259 EXPORT_SYMBOL_GPL(devm_gpiod_get_array_optional);
262 * devm_gpiod_put - Resource-managed gpiod_put()
263 * @dev: GPIO consumer
264 * @desc: GPIO descriptor to dispose of
266 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
267 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
268 * will be disposed of by the resource management code.
270 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
272 WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
275 EXPORT_SYMBOL_GPL(devm_gpiod_put);
278 * devm_gpiod_unhinge - Remove resource management from a gpio descriptor
279 * @dev: GPIO consumer
280 * @desc: GPIO descriptor to remove resource management from
282 * Remove resource management from a GPIO descriptor. This is needed when
283 * you want to hand over lifecycle management of a descriptor to another
287 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc)
291 if (IS_ERR_OR_NULL(desc))
293 ret = devres_destroy(dev, devm_gpiod_release,
294 devm_gpiod_match, &desc);
296 * If the GPIO descriptor is requested as nonexclusive, we
297 * may call this function several times on the same descriptor
298 * so it is OK if devres_destroy() returns -ENOENT.
302 /* Anything else we should warn about */
305 EXPORT_SYMBOL_GPL(devm_gpiod_unhinge);
308 * devm_gpiod_put_array - Resource-managed gpiod_put_array()
309 * @dev: GPIO consumer
310 * @descs: GPIO descriptor array to dispose of
312 * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
313 * Normally this function will not be called as the GPIOs will be disposed of
314 * by the resource management code.
316 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
318 WARN_ON(devres_release(dev, devm_gpiod_release_array,
319 devm_gpiod_match_array, &descs));
321 EXPORT_SYMBOL_GPL(devm_gpiod_put_array);
323 static void devm_gpio_release(struct device *dev, void *res)
325 unsigned *gpio = res;
331 * devm_gpio_request - request a GPIO for a managed device
332 * @dev: device to request the GPIO for
333 * @gpio: GPIO to allocate
334 * @label: the name of the requested GPIO
336 * Except for the extra @dev argument, this function takes the
337 * same arguments and performs the same function as
338 * gpio_request(). GPIOs requested with this function will be
339 * automatically freed on driver detach.
341 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
346 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
350 rc = gpio_request(gpio, label);
361 EXPORT_SYMBOL_GPL(devm_gpio_request);
364 * devm_gpio_request_one - request a single GPIO with initial setup
365 * @dev: device to request for
366 * @gpio: the GPIO number
367 * @flags: GPIO configuration as specified by GPIOF_*
368 * @label: a literal description string of this GPIO
370 int devm_gpio_request_one(struct device *dev, unsigned gpio,
371 unsigned long flags, const char *label)
376 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
380 rc = gpio_request_one(gpio, flags, label);
391 EXPORT_SYMBOL_GPL(devm_gpio_request_one);
393 static void devm_gpio_chip_release(void *data)
395 struct gpio_chip *gc = data;
401 * devm_gpiochip_add_data_with_key() - Resource managed gpiochip_add_data_with_key()
402 * @dev: pointer to the device that gpio_chip belongs to.
403 * @gc: the GPIO chip to register
404 * @data: driver-private data associated with this chip
405 * @lock_key: lockdep class for IRQ lock
406 * @request_key: lockdep class for IRQ request
408 * Context: potentially before irqs will work
410 * The gpio chip automatically be released when the device is unbound.
413 * A negative errno if the chip can't be registered, such as because the
414 * gc->base is invalid or already associated with a different chip.
415 * Otherwise it returns zero as a success code.
417 int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
418 struct lock_class_key *lock_key,
419 struct lock_class_key *request_key)
423 ret = gpiochip_add_data_with_key(gc, data, lock_key, request_key);
427 return devm_add_action_or_reset(dev, devm_gpio_chip_release, gc);
429 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data_with_key);