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(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(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(devm_gpiod_get_index
);
133 * devm_gpiod_get_from_of_node() - obtain a GPIO from an OF node
134 * @dev: device for lifecycle management
135 * @node: handle of the OF node
136 * @propname: name of the DT property representing the GPIO
137 * @index: index of the GPIO to obtain for the consumer
138 * @dflags: GPIO initialization flags
139 * @label: label to attach to the requested GPIO
142 * On successful request the GPIO pin is configured in accordance with
145 * In case of error an ERR_PTR() is returned.
147 struct gpio_desc
*devm_gpiod_get_from_of_node(struct device
*dev
,
148 struct device_node
*node
,
149 const char *propname
, int index
,
150 enum gpiod_flags dflags
,
153 struct gpio_desc
**dr
;
154 struct gpio_desc
*desc
;
156 desc
= gpiod_get_from_of_node(node
, propname
, index
, dflags
, label
);
161 * For non-exclusive GPIO descriptors, check if this descriptor is
162 * already under resource management by this device.
164 if (dflags
& GPIOD_FLAGS_BIT_NONEXCLUSIVE
) {
167 dres
= devres_find(dev
, devm_gpiod_release
,
168 devm_gpiod_match
, &desc
);
173 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
177 return ERR_PTR(-ENOMEM
);
185 EXPORT_SYMBOL(devm_gpiod_get_from_of_node
);
188 * devm_fwnode_get_index_gpiod_from_child - get a GPIO descriptor from a
189 * device's child node
190 * @dev: GPIO consumer
191 * @con_id: function within the GPIO consumer
192 * @index: index of the GPIO to obtain in the consumer
193 * @child: firmware node (child of @dev)
194 * @flags: GPIO initialization flags
195 * @label: label to attach to the requested GPIO
197 * GPIO descriptors returned from this function are automatically disposed on
200 * On successful request the GPIO pin is configured in accordance with
203 struct gpio_desc
*devm_fwnode_get_index_gpiod_from_child(struct device
*dev
,
204 const char *con_id
, int index
,
205 struct fwnode_handle
*child
,
206 enum gpiod_flags flags
,
209 char prop_name
[32]; /* 32 is max size of property name */
210 struct gpio_desc
**dr
;
211 struct gpio_desc
*desc
;
214 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
217 return ERR_PTR(-ENOMEM
);
219 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
221 snprintf(prop_name
, sizeof(prop_name
), "%s-%s",
222 con_id
, gpio_suffixes
[i
]);
224 snprintf(prop_name
, sizeof(prop_name
), "%s",
227 desc
= fwnode_get_named_gpiod(child
, prop_name
, index
, flags
,
229 if (!IS_ERR(desc
) || (PTR_ERR(desc
) != -ENOENT
))
242 EXPORT_SYMBOL(devm_fwnode_get_index_gpiod_from_child
);
245 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
246 * @dev: GPIO consumer
247 * @con_id: function within the GPIO consumer
248 * @index: index of the GPIO to obtain in the consumer
249 * @flags: optional GPIO initialization flags
251 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
252 * function are automatically disposed on driver detach. See
253 * gpiod_get_index_optional() for detailed information about behavior and
256 struct gpio_desc
*__must_check
devm_gpiod_get_index_optional(struct device
*dev
,
259 enum gpiod_flags flags
)
261 struct gpio_desc
*desc
;
263 desc
= devm_gpiod_get_index(dev
, con_id
, index
, flags
);
265 if (PTR_ERR(desc
) == -ENOENT
)
271 EXPORT_SYMBOL(devm_gpiod_get_index_optional
);
274 * devm_gpiod_get_array - Resource-managed gpiod_get_array()
275 * @dev: GPIO consumer
276 * @con_id: function within the GPIO consumer
277 * @flags: optional GPIO initialization flags
279 * Managed gpiod_get_array(). GPIO descriptors returned from this function are
280 * automatically disposed on driver detach. See gpiod_get_array() for detailed
281 * information about behavior and return values.
283 struct gpio_descs
*__must_check
devm_gpiod_get_array(struct device
*dev
,
285 enum gpiod_flags flags
)
287 struct gpio_descs
**dr
;
288 struct gpio_descs
*descs
;
290 dr
= devres_alloc(devm_gpiod_release_array
,
291 sizeof(struct gpio_descs
*), GFP_KERNEL
);
293 return ERR_PTR(-ENOMEM
);
295 descs
= gpiod_get_array(dev
, con_id
, flags
);
306 EXPORT_SYMBOL(devm_gpiod_get_array
);
309 * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
310 * @dev: GPIO consumer
311 * @con_id: function within the GPIO consumer
312 * @flags: optional GPIO initialization flags
314 * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
315 * function are automatically disposed on driver detach.
316 * See gpiod_get_array_optional() for detailed information about behavior and
319 struct gpio_descs
*__must_check
320 devm_gpiod_get_array_optional(struct device
*dev
, const char *con_id
,
321 enum gpiod_flags flags
)
323 struct gpio_descs
*descs
;
325 descs
= devm_gpiod_get_array(dev
, con_id
, flags
);
326 if (IS_ERR(descs
) && (PTR_ERR(descs
) == -ENOENT
))
331 EXPORT_SYMBOL(devm_gpiod_get_array_optional
);
334 * devm_gpiod_put - Resource-managed gpiod_put()
335 * @dev: GPIO consumer
336 * @desc: GPIO descriptor to dispose of
338 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
339 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
340 * will be disposed of by the resource management code.
342 void devm_gpiod_put(struct device
*dev
, struct gpio_desc
*desc
)
344 WARN_ON(devres_release(dev
, devm_gpiod_release
, devm_gpiod_match
,
347 EXPORT_SYMBOL(devm_gpiod_put
);
350 * devm_gpiod_unhinge - Remove resource management from a gpio descriptor
351 * @dev: GPIO consumer
352 * @desc: GPIO descriptor to remove resource management from
354 * Remove resource management from a GPIO descriptor. This is needed when
355 * you want to hand over lifecycle management of a descriptor to another
359 void devm_gpiod_unhinge(struct device
*dev
, struct gpio_desc
*desc
)
363 if (IS_ERR_OR_NULL(desc
))
365 ret
= devres_destroy(dev
, devm_gpiod_release
,
366 devm_gpiod_match
, &desc
);
368 * If the GPIO descriptor is requested as nonexclusive, we
369 * may call this function several times on the same descriptor
370 * so it is OK if devres_destroy() returns -ENOENT.
374 /* Anything else we should warn about */
377 EXPORT_SYMBOL(devm_gpiod_unhinge
);
380 * devm_gpiod_put_array - Resource-managed gpiod_put_array()
381 * @dev: GPIO consumer
382 * @descs: GPIO descriptor array to dispose of
384 * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
385 * Normally this function will not be called as the GPIOs will be disposed of
386 * by the resource management code.
388 void devm_gpiod_put_array(struct device
*dev
, struct gpio_descs
*descs
)
390 WARN_ON(devres_release(dev
, devm_gpiod_release_array
,
391 devm_gpiod_match_array
, &descs
));
393 EXPORT_SYMBOL(devm_gpiod_put_array
);
398 static void devm_gpio_release(struct device
*dev
, void *res
)
400 unsigned *gpio
= res
;
405 static int devm_gpio_match(struct device
*dev
, void *res
, void *data
)
407 unsigned *this = res
, *gpio
= data
;
409 return *this == *gpio
;
413 * devm_gpio_request - request a GPIO for a managed device
414 * @dev: device to request the GPIO for
415 * @gpio: GPIO to allocate
416 * @label: the name of the requested GPIO
418 * Except for the extra @dev argument, this function takes the
419 * same arguments and performs the same function as
420 * gpio_request(). GPIOs requested with this function will be
421 * automatically freed on driver detach.
423 * If an GPIO allocated with this function needs to be freed
424 * separately, devm_gpio_free() must be used.
427 int devm_gpio_request(struct device
*dev
, unsigned gpio
, const char *label
)
432 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
436 rc
= gpio_request(gpio
, label
);
447 EXPORT_SYMBOL(devm_gpio_request
);
450 * devm_gpio_request_one - request a single GPIO with initial setup
451 * @dev: device to request for
452 * @gpio: the GPIO number
453 * @flags: GPIO configuration as specified by GPIOF_*
454 * @label: a literal description string of this GPIO
456 int devm_gpio_request_one(struct device
*dev
, unsigned gpio
,
457 unsigned long flags
, const char *label
)
462 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
466 rc
= gpio_request_one(gpio
, flags
, label
);
477 EXPORT_SYMBOL(devm_gpio_request_one
);
480 * devm_gpio_free - free a GPIO
481 * @dev: device to free GPIO for
482 * @gpio: GPIO to free
484 * Except for the extra @dev argument, this function takes the
485 * same arguments and performs the same function as gpio_free().
486 * This function instead of gpio_free() should be used to manually
487 * free GPIOs allocated with devm_gpio_request().
489 void devm_gpio_free(struct device
*dev
, unsigned int gpio
)
492 WARN_ON(devres_release(dev
, devm_gpio_release
, devm_gpio_match
,
495 EXPORT_SYMBOL(devm_gpio_free
);