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_fwnode_get_index_gpiod_from_child - get a GPIO descriptor from a
129 * device's child node
130 * @dev: GPIO consumer
131 * @con_id: function within the GPIO consumer
132 * @index: index of the GPIO to obtain in the consumer
133 * @child: firmware node (child of @dev)
134 * @flags: GPIO initialization flags
136 * GPIO descriptors returned from this function are automatically disposed on
139 * On successful request the GPIO pin is configured in accordance with
142 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
143 const char *con_id, int index,
144 struct fwnode_handle *child,
145 enum gpiod_flags flags,
148 char prop_name[32]; /* 32 is max size of property name */
149 struct gpio_desc **dr;
150 struct gpio_desc *desc;
153 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
156 return ERR_PTR(-ENOMEM);
158 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
160 snprintf(prop_name, sizeof(prop_name), "%s-%s",
161 con_id, gpio_suffixes[i]);
163 snprintf(prop_name, sizeof(prop_name), "%s",
166 desc = fwnode_get_named_gpiod(child, prop_name, index, flags,
168 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
181 EXPORT_SYMBOL(devm_fwnode_get_index_gpiod_from_child);
184 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
185 * @dev: GPIO consumer
186 * @con_id: function within the GPIO consumer
187 * @index: index of the GPIO to obtain in the consumer
188 * @flags: optional GPIO initialization flags
190 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
191 * function are automatically disposed on driver detach. See
192 * gpiod_get_index_optional() for detailed information about behavior and
195 struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
198 enum gpiod_flags flags)
200 struct gpio_desc *desc;
202 desc = devm_gpiod_get_index(dev, con_id, index, flags);
204 if (PTR_ERR(desc) == -ENOENT)
210 EXPORT_SYMBOL(devm_gpiod_get_index_optional);
213 * devm_gpiod_get_array - Resource-managed gpiod_get_array()
214 * @dev: GPIO consumer
215 * @con_id: function within the GPIO consumer
216 * @flags: optional GPIO initialization flags
218 * Managed gpiod_get_array(). GPIO descriptors returned from this function are
219 * automatically disposed on driver detach. See gpiod_get_array() for detailed
220 * information about behavior and return values.
222 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
224 enum gpiod_flags flags)
226 struct gpio_descs **dr;
227 struct gpio_descs *descs;
229 dr = devres_alloc(devm_gpiod_release_array,
230 sizeof(struct gpio_descs *), GFP_KERNEL);
232 return ERR_PTR(-ENOMEM);
234 descs = gpiod_get_array(dev, con_id, flags);
245 EXPORT_SYMBOL(devm_gpiod_get_array);
248 * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
249 * @dev: GPIO consumer
250 * @con_id: function within the GPIO consumer
251 * @flags: optional GPIO initialization flags
253 * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
254 * function are automatically disposed on driver detach.
255 * See gpiod_get_array_optional() for detailed information about behavior and
258 struct gpio_descs *__must_check
259 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
260 enum gpiod_flags flags)
262 struct gpio_descs *descs;
264 descs = devm_gpiod_get_array(dev, con_id, flags);
265 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
270 EXPORT_SYMBOL(devm_gpiod_get_array_optional);
273 * devm_gpiod_put - Resource-managed gpiod_put()
274 * @desc: GPIO descriptor to dispose of
276 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
277 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
278 * will be disposed of by the resource management code.
280 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
282 WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
285 EXPORT_SYMBOL(devm_gpiod_put);
288 * devm_gpiod_put_array - Resource-managed gpiod_put_array()
289 * @descs: GPIO descriptor array to dispose of
291 * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
292 * Normally this function will not be called as the GPIOs will be disposed of
293 * by the resource management code.
295 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
297 WARN_ON(devres_release(dev, devm_gpiod_release_array,
298 devm_gpiod_match_array, &descs));
300 EXPORT_SYMBOL(devm_gpiod_put_array);
305 static void devm_gpio_release(struct device *dev, void *res)
307 unsigned *gpio = res;
312 static int devm_gpio_match(struct device *dev, void *res, void *data)
314 unsigned *this = res, *gpio = data;
316 return *this == *gpio;
320 * devm_gpio_request - request a GPIO for a managed device
321 * @dev: device to request the GPIO for
322 * @gpio: GPIO to allocate
323 * @label: the name of the requested GPIO
325 * Except for the extra @dev argument, this function takes the
326 * same arguments and performs the same function as
327 * gpio_request(). GPIOs requested with this function will be
328 * automatically freed on driver detach.
330 * If an GPIO allocated with this function needs to be freed
331 * separately, devm_gpio_free() must be used.
334 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
339 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
343 rc = gpio_request(gpio, label);
354 EXPORT_SYMBOL(devm_gpio_request);
357 * devm_gpio_request_one - request a single GPIO with initial setup
358 * @dev: device to request for
359 * @gpio: the GPIO number
360 * @flags: GPIO configuration as specified by GPIOF_*
361 * @label: a literal description string of this GPIO
363 int devm_gpio_request_one(struct device *dev, unsigned gpio,
364 unsigned long flags, const char *label)
369 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
373 rc = gpio_request_one(gpio, flags, label);
384 EXPORT_SYMBOL(devm_gpio_request_one);
387 * devm_gpio_free - free a GPIO
388 * @dev: device to free GPIO for
389 * @gpio: GPIO to free
391 * Except for the extra @dev argument, this function takes the
392 * same arguments and performs the same function as gpio_free().
393 * This function instead of gpio_free() should be used to manually
394 * free GPIOs allocated with devm_gpio_request().
396 void devm_gpio_free(struct device *dev, unsigned int gpio)
399 WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
402 EXPORT_SYMBOL(devm_gpio_free);