ARM: omap2: remove unnecessary boot_lock
[linux-2.6/linux-2.6-arm.git] / drivers / gpio / gpiolib-devres.c
blob01959369360bd761cdb6a30c06980363e78ef40d
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
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>
7 */
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>
16 #include "gpiolib.h"
18 static void devm_gpiod_release(struct device *dev, void *res)
20 struct gpio_desc **desc = res;
22 gpiod_put(*desc);
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;
46 /**
47 * devm_gpiod_get - Resource-managed gpiod_get()
48 * @dev: GPIO consumer
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,
57 const char *con_id,
58 enum gpiod_flags flags)
60 return devm_gpiod_get_index(dev, con_id, 0, flags);
62 EXPORT_SYMBOL(devm_gpiod_get);
64 /**
65 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
66 * @dev: GPIO consumer
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,
75 const char *con_id,
76 enum gpiod_flags flags)
78 return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
80 EXPORT_SYMBOL(devm_gpiod_get_optional);
82 /**
83 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
84 * @dev: GPIO consumer
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,
94 const char *con_id,
95 unsigned int idx,
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 *),
102 GFP_KERNEL);
103 if (!dr)
104 return ERR_PTR(-ENOMEM);
106 desc = gpiod_get_index(dev, con_id, idx, flags);
107 if (IS_ERR(desc)) {
108 devres_free(dr);
109 return desc;
112 *dr = desc;
113 devres_add(dev, dr);
115 return desc;
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
128 * Returns:
129 * On successful request the GPIO pin is configured in accordance with
130 * provided @dflags.
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,
138 const char *label)
140 struct gpio_desc **dr;
141 struct gpio_desc *desc;
143 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
144 GFP_KERNEL);
145 if (!dr)
146 return ERR_PTR(-ENOMEM);
148 desc = gpiod_get_from_of_node(node, propname, index, dflags, label);
149 if (IS_ERR(desc)) {
150 devres_free(dr);
151 return desc;
154 *dr = desc;
155 devres_add(dev, dr);
157 return desc;
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
172 * driver detach.
174 * On successful request the GPIO pin is configured in accordance with
175 * provided @flags.
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,
181 const char *label)
183 char prop_name[32]; /* 32 is max size of property name */
184 struct gpio_desc **dr;
185 struct gpio_desc *desc;
186 unsigned int i;
188 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
189 GFP_KERNEL);
190 if (!dr)
191 return ERR_PTR(-ENOMEM);
193 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
194 if (con_id)
195 snprintf(prop_name, sizeof(prop_name), "%s-%s",
196 con_id, gpio_suffixes[i]);
197 else
198 snprintf(prop_name, sizeof(prop_name), "%s",
199 gpio_suffixes[i]);
201 desc = fwnode_get_named_gpiod(child, prop_name, index, flags,
202 label);
203 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
204 break;
206 if (IS_ERR(desc)) {
207 devres_free(dr);
208 return desc;
211 *dr = desc;
212 devres_add(dev, dr);
214 return desc;
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
228 * return values.
230 struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
231 const char *con_id,
232 unsigned int index,
233 enum gpiod_flags flags)
235 struct gpio_desc *desc;
237 desc = devm_gpiod_get_index(dev, con_id, index, flags);
238 if (IS_ERR(desc)) {
239 if (PTR_ERR(desc) == -ENOENT)
240 return NULL;
243 return desc;
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,
258 const char *con_id,
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);
266 if (!dr)
267 return ERR_PTR(-ENOMEM);
269 descs = gpiod_get_array(dev, con_id, flags);
270 if (IS_ERR(descs)) {
271 devres_free(dr);
272 return descs;
275 *dr = descs;
276 devres_add(dev, dr);
278 return descs;
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
291 * return values.
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))
301 return NULL;
303 return descs;
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,
319 &desc));
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;
346 gpio_free(*gpio);
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)
373 unsigned *dr;
374 int rc;
376 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
377 if (!dr)
378 return -ENOMEM;
380 rc = gpio_request(gpio, label);
381 if (rc) {
382 devres_free(dr);
383 return rc;
386 *dr = gpio;
387 devres_add(dev, dr);
389 return 0;
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)
403 unsigned *dr;
404 int rc;
406 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
407 if (!dr)
408 return -ENOMEM;
410 rc = gpio_request_one(gpio, flags, label);
411 if (rc) {
412 devres_free(dr);
413 return rc;
416 *dr = gpio;
417 devres_add(dev, dr);
419 return 0;
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,
437 &gpio));
439 EXPORT_SYMBOL(devm_gpio_free);