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 <blogic@openwrt.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>
24 static void devm_gpiod_release(struct device
*dev
, void *res
)
26 struct gpio_desc
**desc
= res
;
31 static int devm_gpiod_match(struct device
*dev
, void *res
, void *data
)
33 struct gpio_desc
**this = res
, **gpio
= data
;
35 return *this == *gpio
;
38 static void devm_gpiod_release_array(struct device
*dev
, void *res
)
40 struct gpio_descs
**descs
= res
;
42 gpiod_put_array(*descs
);
45 static int devm_gpiod_match_array(struct device
*dev
, void *res
, void *data
)
47 struct gpio_descs
**this = res
, **gpios
= data
;
49 return *this == *gpios
;
53 * devm_gpiod_get - Resource-managed gpiod_get()
55 * @con_id: function within the GPIO consumer
56 * @flags: optional GPIO initialization flags
58 * Managed gpiod_get(). GPIO descriptors returned from this function are
59 * automatically disposed on driver detach. See gpiod_get() for detailed
60 * information about behavior and return values.
62 struct gpio_desc
*__must_check
devm_gpiod_get(struct device
*dev
,
64 enum gpiod_flags flags
)
66 return devm_gpiod_get_index(dev
, con_id
, 0, flags
);
68 EXPORT_SYMBOL(devm_gpiod_get
);
71 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
73 * @con_id: function within the GPIO consumer
74 * @flags: optional GPIO initialization flags
76 * Managed gpiod_get_optional(). GPIO descriptors returned from this function
77 * are automatically disposed on driver detach. See gpiod_get_optional() for
78 * detailed information about behavior and return values.
80 struct gpio_desc
*__must_check
devm_gpiod_get_optional(struct device
*dev
,
82 enum gpiod_flags flags
)
84 return devm_gpiod_get_index_optional(dev
, con_id
, 0, flags
);
86 EXPORT_SYMBOL(devm_gpiod_get_optional
);
89 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
91 * @con_id: function within the GPIO consumer
92 * @idx: index of the GPIO to obtain in the consumer
93 * @flags: optional GPIO initialization flags
95 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
96 * automatically disposed on driver detach. See gpiod_get_index() for detailed
97 * information about behavior and return values.
99 struct gpio_desc
*__must_check
devm_gpiod_get_index(struct device
*dev
,
102 enum gpiod_flags flags
)
104 struct gpio_desc
**dr
;
105 struct gpio_desc
*desc
;
107 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
110 return ERR_PTR(-ENOMEM
);
112 desc
= gpiod_get_index(dev
, con_id
, idx
, flags
);
123 EXPORT_SYMBOL(devm_gpiod_get_index
);
126 * devm_get_gpiod_from_child - get a GPIO descriptor from a device's child node
127 * @dev: GPIO consumer
128 * @con_id: function within the GPIO consumer
129 * @child: firmware node (child of @dev)
131 * GPIO descriptors returned from this function are automatically disposed on
134 struct gpio_desc
*devm_get_gpiod_from_child(struct device
*dev
,
136 struct fwnode_handle
*child
)
138 static const char * const suffixes
[] = { "gpios", "gpio" };
139 char prop_name
[32]; /* 32 is max size of property name */
140 struct gpio_desc
**dr
;
141 struct gpio_desc
*desc
;
144 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
147 return ERR_PTR(-ENOMEM
);
149 for (i
= 0; i
< ARRAY_SIZE(suffixes
); i
++) {
151 snprintf(prop_name
, sizeof(prop_name
), "%s-%s",
152 con_id
, suffixes
[i
]);
154 snprintf(prop_name
, sizeof(prop_name
), "%s",
157 desc
= fwnode_get_named_gpiod(child
, prop_name
);
158 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
171 EXPORT_SYMBOL(devm_get_gpiod_from_child
);
174 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
175 * @dev: GPIO consumer
176 * @con_id: function within the GPIO consumer
177 * @index: index of the GPIO to obtain in the consumer
178 * @flags: optional GPIO initialization flags
180 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
181 * function are automatically disposed on driver detach. See
182 * gpiod_get_index_optional() for detailed information about behavior and
185 struct gpio_desc
*__must_check
devm_gpiod_get_index_optional(struct device
*dev
,
188 enum gpiod_flags flags
)
190 struct gpio_desc
*desc
;
192 desc
= devm_gpiod_get_index(dev
, con_id
, index
, flags
);
194 if (PTR_ERR(desc
) == -ENOENT
)
200 EXPORT_SYMBOL(devm_gpiod_get_index_optional
);
203 * devm_gpiod_get_array - Resource-managed gpiod_get_array()
204 * @dev: GPIO consumer
205 * @con_id: function within the GPIO consumer
206 * @flags: optional GPIO initialization flags
208 * Managed gpiod_get_array(). GPIO descriptors returned from this function are
209 * automatically disposed on driver detach. See gpiod_get_array() for detailed
210 * information about behavior and return values.
212 struct gpio_descs
*__must_check
devm_gpiod_get_array(struct device
*dev
,
214 enum gpiod_flags flags
)
216 struct gpio_descs
**dr
;
217 struct gpio_descs
*descs
;
219 dr
= devres_alloc(devm_gpiod_release_array
,
220 sizeof(struct gpio_descs
*), GFP_KERNEL
);
222 return ERR_PTR(-ENOMEM
);
224 descs
= gpiod_get_array(dev
, con_id
, flags
);
235 EXPORT_SYMBOL(devm_gpiod_get_array
);
238 * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
239 * @dev: GPIO consumer
240 * @con_id: function within the GPIO consumer
241 * @flags: optional GPIO initialization flags
243 * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
244 * function are automatically disposed on driver detach.
245 * See gpiod_get_array_optional() for detailed information about behavior and
248 struct gpio_descs
*__must_check
249 devm_gpiod_get_array_optional(struct device
*dev
, const char *con_id
,
250 enum gpiod_flags flags
)
252 struct gpio_descs
*descs
;
254 descs
= devm_gpiod_get_array(dev
, con_id
, flags
);
255 if (IS_ERR(descs
) && (PTR_ERR(descs
) == -ENOENT
))
260 EXPORT_SYMBOL(devm_gpiod_get_array_optional
);
263 * devm_gpiod_put - Resource-managed gpiod_put()
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(devm_gpiod_put
);
278 * devm_gpiod_put_array - Resource-managed gpiod_put_array()
279 * @descs: GPIO descriptor array to dispose of
281 * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
282 * Normally this function will not be called as the GPIOs will be disposed of
283 * by the resource management code.
285 void devm_gpiod_put_array(struct device
*dev
, struct gpio_descs
*descs
)
287 WARN_ON(devres_release(dev
, devm_gpiod_release_array
,
288 devm_gpiod_match_array
, &descs
));
290 EXPORT_SYMBOL(devm_gpiod_put_array
);
295 static void devm_gpio_release(struct device
*dev
, void *res
)
297 unsigned *gpio
= res
;
302 static int devm_gpio_match(struct device
*dev
, void *res
, void *data
)
304 unsigned *this = res
, *gpio
= data
;
306 return *this == *gpio
;
310 * devm_gpio_request - request a GPIO for a managed device
311 * @dev: device to request the GPIO for
312 * @gpio: GPIO to allocate
313 * @label: the name of the requested GPIO
315 * Except for the extra @dev argument, this function takes the
316 * same arguments and performs the same function as
317 * gpio_request(). GPIOs requested with this function will be
318 * automatically freed on driver detach.
320 * If an GPIO allocated with this function needs to be freed
321 * separately, devm_gpio_free() must be used.
324 int devm_gpio_request(struct device
*dev
, unsigned gpio
, const char *label
)
329 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
333 rc
= gpio_request(gpio
, label
);
344 EXPORT_SYMBOL(devm_gpio_request
);
347 * devm_gpio_request_one - request a single GPIO with initial setup
348 * @dev: device to request for
349 * @gpio: the GPIO number
350 * @flags: GPIO configuration as specified by GPIOF_*
351 * @label: a literal description string of this GPIO
353 int devm_gpio_request_one(struct device
*dev
, unsigned gpio
,
354 unsigned long flags
, const char *label
)
359 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
363 rc
= gpio_request_one(gpio
, flags
, label
);
374 EXPORT_SYMBOL(devm_gpio_request_one
);
377 * devm_gpio_free - free a GPIO
378 * @dev: device to free GPIO for
379 * @gpio: GPIO to free
381 * Except for the extra @dev argument, this function takes the
382 * same arguments and performs the same function as gpio_free().
383 * This function instead of gpio_free() should be used to manually
384 * free GPIOs allocated with devm_gpio_request().
386 void devm_gpio_free(struct device
*dev
, unsigned int gpio
)
389 WARN_ON(devres_release(dev
, devm_gpio_release
, devm_gpio_match
,
392 EXPORT_SYMBOL(devm_gpio_free
);