2 * drivers/gpio/devres.c - managed gpio resources
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * You should have received a copy of the GNU General Public License
9 * along with this program; if not, write to the Free Software
10 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
12 * This file is based on kernel/irq/devres.c
14 * Copyright (c) 2011 John Crispin <john@phrozen.org>
17 #include <linux/module.h>
18 #include <linux/err.h>
19 #include <linux/gpio.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/device.h>
22 #include <linux/gfp.h>
26 static void devm_gpiod_release(struct device *dev, void *res)
28 struct gpio_desc **desc = res;
33 static int devm_gpiod_match(struct device *dev, void *res, void *data)
35 struct gpio_desc **this = res, **gpio = data;
37 return *this == *gpio;
40 static void devm_gpiod_release_array(struct device *dev, void *res)
42 struct gpio_descs **descs = res;
44 gpiod_put_array(*descs);
47 static int devm_gpiod_match_array(struct device *dev, void *res, void *data)
49 struct gpio_descs **this = res, **gpios = data;
51 return *this == *gpios;
55 * devm_gpiod_get - Resource-managed gpiod_get()
57 * @con_id: function within the GPIO consumer
58 * @flags: optional GPIO initialization flags
60 * Managed gpiod_get(). GPIO descriptors returned from this function are
61 * automatically disposed on driver detach. See gpiod_get() for detailed
62 * information about behavior and return values.
64 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
66 enum gpiod_flags flags)
68 return devm_gpiod_get_index(dev, con_id, 0, flags);
70 EXPORT_SYMBOL(devm_gpiod_get);
73 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
75 * @con_id: function within the GPIO consumer
76 * @flags: optional GPIO initialization flags
78 * Managed gpiod_get_optional(). GPIO descriptors returned from this function
79 * are automatically disposed on driver detach. See gpiod_get_optional() for
80 * detailed information about behavior and return values.
82 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
84 enum gpiod_flags flags)
86 return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
88 EXPORT_SYMBOL(devm_gpiod_get_optional);
91 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
93 * @con_id: function within the GPIO consumer
94 * @idx: index of the GPIO to obtain in the consumer
95 * @flags: optional GPIO initialization flags
97 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
98 * automatically disposed on driver detach. See gpiod_get_index() for detailed
99 * information about behavior and return values.
101 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
104 enum gpiod_flags flags)
106 struct gpio_desc **dr;
107 struct gpio_desc *desc;
109 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
112 return ERR_PTR(-ENOMEM);
114 desc = gpiod_get_index(dev, con_id, idx, flags);
125 EXPORT_SYMBOL(devm_gpiod_get_index);
128 * devm_gpiod_get_from_of_node() - obtain a GPIO from an OF node
129 * @dev: device for lifecycle management
130 * @node: handle of the OF node
131 * @propname: name of the DT property representing the GPIO
132 * @index: index of the GPIO to obtain for the consumer
133 * @dflags: GPIO initialization flags
134 * @label: label to attach to the requested GPIO
137 * On successful request the GPIO pin is configured in accordance with
140 * In case of error an ERR_PTR() is returned.
142 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
143 struct device_node *node,
144 const char *propname, int index,
145 enum gpiod_flags dflags,
148 struct gpio_desc **dr;
149 struct gpio_desc *desc;
151 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
154 return ERR_PTR(-ENOMEM);
156 desc = gpiod_get_from_of_node(node, propname, index, dflags, label);
167 EXPORT_SYMBOL(devm_gpiod_get_from_of_node);
170 * devm_fwnode_get_index_gpiod_from_child - get a GPIO descriptor from a
171 * device's child node
172 * @dev: GPIO consumer
173 * @con_id: function within the GPIO consumer
174 * @index: index of the GPIO to obtain in the consumer
175 * @child: firmware node (child of @dev)
176 * @flags: GPIO initialization flags
177 * @label: label to attach to the requested GPIO
179 * GPIO descriptors returned from this function are automatically disposed on
182 * On successful request the GPIO pin is configured in accordance with
185 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
186 const char *con_id, int index,
187 struct fwnode_handle *child,
188 enum gpiod_flags flags,
191 char prop_name[32]; /* 32 is max size of property name */
192 struct gpio_desc **dr;
193 struct gpio_desc *desc;
196 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
199 return ERR_PTR(-ENOMEM);
201 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
203 snprintf(prop_name, sizeof(prop_name), "%s-%s",
204 con_id, gpio_suffixes[i]);
206 snprintf(prop_name, sizeof(prop_name), "%s",
209 desc = fwnode_get_named_gpiod(child, prop_name, index, flags,
211 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
224 EXPORT_SYMBOL(devm_fwnode_get_index_gpiod_from_child);
227 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
228 * @dev: GPIO consumer
229 * @con_id: function within the GPIO consumer
230 * @index: index of the GPIO to obtain in the consumer
231 * @flags: optional GPIO initialization flags
233 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
234 * function are automatically disposed on driver detach. See
235 * gpiod_get_index_optional() for detailed information about behavior and
238 struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
241 enum gpiod_flags flags)
243 struct gpio_desc *desc;
245 desc = devm_gpiod_get_index(dev, con_id, index, flags);
247 if (PTR_ERR(desc) == -ENOENT)
253 EXPORT_SYMBOL(devm_gpiod_get_index_optional);
256 * devm_gpiod_get_array - Resource-managed gpiod_get_array()
257 * @dev: GPIO consumer
258 * @con_id: function within the GPIO consumer
259 * @flags: optional GPIO initialization flags
261 * Managed gpiod_get_array(). GPIO descriptors returned from this function are
262 * automatically disposed on driver detach. See gpiod_get_array() for detailed
263 * information about behavior and return values.
265 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
267 enum gpiod_flags flags)
269 struct gpio_descs **dr;
270 struct gpio_descs *descs;
272 dr = devres_alloc(devm_gpiod_release_array,
273 sizeof(struct gpio_descs *), GFP_KERNEL);
275 return ERR_PTR(-ENOMEM);
277 descs = gpiod_get_array(dev, con_id, flags);
288 EXPORT_SYMBOL(devm_gpiod_get_array);
291 * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
292 * @dev: GPIO consumer
293 * @con_id: function within the GPIO consumer
294 * @flags: optional GPIO initialization flags
296 * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
297 * function are automatically disposed on driver detach.
298 * See gpiod_get_array_optional() for detailed information about behavior and
301 struct gpio_descs *__must_check
302 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
303 enum gpiod_flags flags)
305 struct gpio_descs *descs;
307 descs = devm_gpiod_get_array(dev, con_id, flags);
308 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
313 EXPORT_SYMBOL(devm_gpiod_get_array_optional);
316 * devm_gpiod_put - Resource-managed gpiod_put()
317 * @dev: GPIO consumer
318 * @desc: GPIO descriptor to dispose of
320 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
321 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
322 * will be disposed of by the resource management code.
324 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
326 WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
329 EXPORT_SYMBOL(devm_gpiod_put);
332 * devm_gpiod_put_array - Resource-managed gpiod_put_array()
333 * @dev: GPIO consumer
334 * @descs: GPIO descriptor array to dispose of
336 * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
337 * Normally this function will not be called as the GPIOs will be disposed of
338 * by the resource management code.
340 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
342 WARN_ON(devres_release(dev, devm_gpiod_release_array,
343 devm_gpiod_match_array, &descs));
345 EXPORT_SYMBOL(devm_gpiod_put_array);
350 static void devm_gpio_release(struct device *dev, void *res)
352 unsigned *gpio = res;
357 static int devm_gpio_match(struct device *dev, void *res, void *data)
359 unsigned *this = res, *gpio = data;
361 return *this == *gpio;
365 * devm_gpio_request - request a GPIO for a managed device
366 * @dev: device to request the GPIO for
367 * @gpio: GPIO to allocate
368 * @label: the name of the requested GPIO
370 * Except for the extra @dev argument, this function takes the
371 * same arguments and performs the same function as
372 * gpio_request(). GPIOs requested with this function will be
373 * automatically freed on driver detach.
375 * If an GPIO allocated with this function needs to be freed
376 * separately, devm_gpio_free() must be used.
379 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
384 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
388 rc = gpio_request(gpio, label);
399 EXPORT_SYMBOL(devm_gpio_request);
402 * devm_gpio_request_one - request a single GPIO with initial setup
403 * @dev: device to request for
404 * @gpio: the GPIO number
405 * @flags: GPIO configuration as specified by GPIOF_*
406 * @label: a literal description string of this GPIO
408 int devm_gpio_request_one(struct device *dev, unsigned gpio,
409 unsigned long flags, const char *label)
414 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
418 rc = gpio_request_one(gpio, flags, label);
429 EXPORT_SYMBOL(devm_gpio_request_one);
432 * devm_gpio_free - free a GPIO
433 * @dev: device to free GPIO for
434 * @gpio: GPIO to free
436 * Except for the extra @dev argument, this function takes the
437 * same arguments and performs the same function as gpio_free().
438 * This function instead of gpio_free() should be used to manually
439 * free GPIOs allocated with devm_gpio_request().
441 void devm_gpio_free(struct device *dev, unsigned int gpio)
444 WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
447 EXPORT_SYMBOL(devm_gpio_free);