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 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
104 return ERR_PTR(-ENOMEM
);
106 desc
= gpiod_get_index(dev
, con_id
, idx
, flags
);
117 EXPORT_SYMBOL(devm_gpiod_get_index
);
120 * devm_gpiod_get_from_of_node() - obtain a GPIO from an OF node
121 * @dev: device for lifecycle management
122 * @node: handle of the OF node
123 * @propname: name of the DT property representing the GPIO
124 * @index: index of the GPIO to obtain for the consumer
125 * @dflags: GPIO initialization flags
126 * @label: label to attach to the requested GPIO
129 * On successful request the GPIO pin is configured in accordance with
132 * In case of error an ERR_PTR() is returned.
134 struct gpio_desc
*devm_gpiod_get_from_of_node(struct device
*dev
,
135 struct device_node
*node
,
136 const char *propname
, int index
,
137 enum gpiod_flags dflags
,
140 struct gpio_desc
**dr
;
141 struct gpio_desc
*desc
;
143 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
146 return ERR_PTR(-ENOMEM
);
148 desc
= gpiod_get_from_of_node(node
, propname
, index
, dflags
, label
);
159 EXPORT_SYMBOL(devm_gpiod_get_from_of_node
);
162 * devm_fwnode_get_index_gpiod_from_child - get a GPIO descriptor from a
163 * device's child node
164 * @dev: GPIO consumer
165 * @con_id: function within the GPIO consumer
166 * @index: index of the GPIO to obtain in the consumer
167 * @child: firmware node (child of @dev)
168 * @flags: GPIO initialization flags
169 * @label: label to attach to the requested GPIO
171 * GPIO descriptors returned from this function are automatically disposed on
174 * On successful request the GPIO pin is configured in accordance with
177 struct gpio_desc
*devm_fwnode_get_index_gpiod_from_child(struct device
*dev
,
178 const char *con_id
, int index
,
179 struct fwnode_handle
*child
,
180 enum gpiod_flags flags
,
183 char prop_name
[32]; /* 32 is max size of property name */
184 struct gpio_desc
**dr
;
185 struct gpio_desc
*desc
;
188 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
191 return ERR_PTR(-ENOMEM
);
193 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
195 snprintf(prop_name
, sizeof(prop_name
), "%s-%s",
196 con_id
, gpio_suffixes
[i
]);
198 snprintf(prop_name
, sizeof(prop_name
), "%s",
201 desc
= fwnode_get_named_gpiod(child
, prop_name
, index
, flags
,
203 if (!IS_ERR(desc
) || (PTR_ERR(desc
) != -ENOENT
))
216 EXPORT_SYMBOL(devm_fwnode_get_index_gpiod_from_child
);
219 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
220 * @dev: GPIO consumer
221 * @con_id: function within the GPIO consumer
222 * @index: index of the GPIO to obtain in the consumer
223 * @flags: optional GPIO initialization flags
225 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
226 * function are automatically disposed on driver detach. See
227 * gpiod_get_index_optional() for detailed information about behavior and
230 struct gpio_desc
*__must_check
devm_gpiod_get_index_optional(struct device
*dev
,
233 enum gpiod_flags flags
)
235 struct gpio_desc
*desc
;
237 desc
= devm_gpiod_get_index(dev
, con_id
, index
, flags
);
239 if (PTR_ERR(desc
) == -ENOENT
)
245 EXPORT_SYMBOL(devm_gpiod_get_index_optional
);
248 * devm_gpiod_get_array - Resource-managed gpiod_get_array()
249 * @dev: GPIO consumer
250 * @con_id: function within the GPIO consumer
251 * @flags: optional GPIO initialization flags
253 * Managed gpiod_get_array(). GPIO descriptors returned from this function are
254 * automatically disposed on driver detach. See gpiod_get_array() for detailed
255 * information about behavior and return values.
257 struct gpio_descs
*__must_check
devm_gpiod_get_array(struct device
*dev
,
259 enum gpiod_flags flags
)
261 struct gpio_descs
**dr
;
262 struct gpio_descs
*descs
;
264 dr
= devres_alloc(devm_gpiod_release_array
,
265 sizeof(struct gpio_descs
*), GFP_KERNEL
);
267 return ERR_PTR(-ENOMEM
);
269 descs
= gpiod_get_array(dev
, con_id
, flags
);
280 EXPORT_SYMBOL(devm_gpiod_get_array
);
283 * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
284 * @dev: GPIO consumer
285 * @con_id: function within the GPIO consumer
286 * @flags: optional GPIO initialization flags
288 * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
289 * function are automatically disposed on driver detach.
290 * See gpiod_get_array_optional() for detailed information about behavior and
293 struct gpio_descs
*__must_check
294 devm_gpiod_get_array_optional(struct device
*dev
, const char *con_id
,
295 enum gpiod_flags flags
)
297 struct gpio_descs
*descs
;
299 descs
= devm_gpiod_get_array(dev
, con_id
, flags
);
300 if (IS_ERR(descs
) && (PTR_ERR(descs
) == -ENOENT
))
305 EXPORT_SYMBOL(devm_gpiod_get_array_optional
);
308 * devm_gpiod_put - Resource-managed gpiod_put()
309 * @dev: GPIO consumer
310 * @desc: GPIO descriptor to dispose of
312 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
313 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
314 * will be disposed of by the resource management code.
316 void devm_gpiod_put(struct device
*dev
, struct gpio_desc
*desc
)
318 WARN_ON(devres_release(dev
, devm_gpiod_release
, devm_gpiod_match
,
321 EXPORT_SYMBOL(devm_gpiod_put
);
324 * devm_gpiod_put_array - Resource-managed gpiod_put_array()
325 * @dev: GPIO consumer
326 * @descs: GPIO descriptor array to dispose of
328 * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
329 * Normally this function will not be called as the GPIOs will be disposed of
330 * by the resource management code.
332 void devm_gpiod_put_array(struct device
*dev
, struct gpio_descs
*descs
)
334 WARN_ON(devres_release(dev
, devm_gpiod_release_array
,
335 devm_gpiod_match_array
, &descs
));
337 EXPORT_SYMBOL(devm_gpiod_put_array
);
342 static void devm_gpio_release(struct device
*dev
, void *res
)
344 unsigned *gpio
= res
;
349 static int devm_gpio_match(struct device
*dev
, void *res
, void *data
)
351 unsigned *this = res
, *gpio
= data
;
353 return *this == *gpio
;
357 * devm_gpio_request - request a GPIO for a managed device
358 * @dev: device to request the GPIO for
359 * @gpio: GPIO to allocate
360 * @label: the name of the requested GPIO
362 * Except for the extra @dev argument, this function takes the
363 * same arguments and performs the same function as
364 * gpio_request(). GPIOs requested with this function will be
365 * automatically freed on driver detach.
367 * If an GPIO allocated with this function needs to be freed
368 * separately, devm_gpio_free() must be used.
371 int devm_gpio_request(struct device
*dev
, unsigned gpio
, const char *label
)
376 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
380 rc
= gpio_request(gpio
, label
);
391 EXPORT_SYMBOL(devm_gpio_request
);
394 * devm_gpio_request_one - request a single GPIO with initial setup
395 * @dev: device to request for
396 * @gpio: the GPIO number
397 * @flags: GPIO configuration as specified by GPIOF_*
398 * @label: a literal description string of this GPIO
400 int devm_gpio_request_one(struct device
*dev
, unsigned gpio
,
401 unsigned long flags
, const char *label
)
406 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
410 rc
= gpio_request_one(gpio
, flags
, label
);
421 EXPORT_SYMBOL(devm_gpio_request_one
);
424 * devm_gpio_free - free a GPIO
425 * @dev: device to free GPIO for
426 * @gpio: GPIO to free
428 * Except for the extra @dev argument, this function takes the
429 * same arguments and performs the same function as gpio_free().
430 * This function instead of gpio_free() should be used to manually
431 * free GPIOs allocated with devm_gpio_request().
433 void devm_gpio_free(struct device
*dev
, unsigned int gpio
)
436 WARN_ON(devres_release(dev
, devm_gpio_release
, devm_gpio_match
,
439 EXPORT_SYMBOL(devm_gpio_free
);