blob: 5e0b41b5e5541c47c1b5cbc9050b9dfe48bf989c [file] [log] [blame]
John Crispin1a0703e2011-12-20 21:40:21 +01001/*
2 * drivers/gpio/devres.c - managed gpio resources
3 *
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.
7 *
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
11 *
12 * This file is based on kernel/irq/devres.c
13 *
14 * Copyright (c) 2011 John Crispin <blogic@openwrt.org>
15 */
16
17#include <linux/module.h>
Alexandre Courbot3c2c6282013-10-20 15:14:58 -070018#include <linux/err.h>
John Crispin1a0703e2011-12-20 21:40:21 +010019#include <linux/gpio.h>
Alexandre Courbot3c2c6282013-10-20 15:14:58 -070020#include <linux/gpio/consumer.h>
John Crispin1a0703e2011-12-20 21:40:21 +010021#include <linux/device.h>
22#include <linux/gfp.h>
23
Alexandre Courbotbae48da2013-10-17 10:21:38 -070024static void devm_gpiod_release(struct device *dev, void *res)
25{
26 struct gpio_desc **desc = res;
27
28 gpiod_put(*desc);
29}
30
31static int devm_gpiod_match(struct device *dev, void *res, void *data)
32{
33 struct gpio_desc **this = res, **gpio = data;
34
35 return *this == *gpio;
36}
37
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +010038static void devm_gpiod_release_array(struct device *dev, void *res)
39{
40 struct gpio_descs **descs = res;
41
42 gpiod_put_array(*descs);
43}
44
45static int devm_gpiod_match_array(struct device *dev, void *res, void *data)
46{
47 struct gpio_descs **this = res, **gpios = data;
48
49 return *this == *gpios;
50}
51
Alexandre Courbotbae48da2013-10-17 10:21:38 -070052/**
53 * devm_gpiod_get - Resource-managed gpiod_get()
54 * @dev: GPIO consumer
55 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090056 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -070057 *
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.
61 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010062struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090063 const char *con_id,
64 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -070065{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090066 return devm_gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070067}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010068EXPORT_SYMBOL(devm_gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070069
70/**
Thierry Reding29a1f2332014-04-25 17:10:06 +020071 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
72 * @dev: GPIO consumer
73 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090074 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +020075 *
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.
79 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010080struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090081 const char *con_id,
82 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +020083{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090084 return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +020085}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010086EXPORT_SYMBOL(devm_gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +020087
88/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -070089 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
90 * @dev: GPIO consumer
91 * @con_id: function within the GPIO consumer
92 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090093 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -070094 *
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.
98 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010099struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700100 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900101 unsigned int idx,
102 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700103{
104 struct gpio_desc **dr;
105 struct gpio_desc *desc;
106
Julia Lawall0f05a3a2014-07-29 17:16:48 +0200107 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700108 GFP_KERNEL);
109 if (!dr)
110 return ERR_PTR(-ENOMEM);
111
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900112 desc = gpiod_get_index(dev, con_id, idx, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700113 if (IS_ERR(desc)) {
114 devres_free(dr);
115 return desc;
116 }
117
118 *dr = desc;
119 devres_add(dev, dr);
120
Alexandre Courbot5fcdb9d2013-10-20 15:14:57 -0700121 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700122}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100123EXPORT_SYMBOL(devm_gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700124
125/**
Mika Westerberg40b73182014-10-21 13:33:59 +0200126 * devm_get_gpiod_from_child - get a GPIO descriptor from a device's child node
127 * @dev: GPIO consumer
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100128 * @con_id: function within the GPIO consumer
Mika Westerberg40b73182014-10-21 13:33:59 +0200129 * @child: firmware node (child of @dev)
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200130 * @flags: GPIO initialization flags
Mika Westerberg40b73182014-10-21 13:33:59 +0200131 *
132 * GPIO descriptors returned from this function are automatically disposed on
133 * driver detach.
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200134 *
135 * On successfull request the GPIO pin is configured in accordance with
136 * provided @flags.
Mika Westerberg40b73182014-10-21 13:33:59 +0200137 */
138struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100139 const char *con_id,
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200140 struct fwnode_handle *child,
141 enum gpiod_flags flags)
Mika Westerberg40b73182014-10-21 13:33:59 +0200142{
Linus Walleijb6202852015-03-11 12:21:17 +0100143 static const char * const suffixes[] = { "gpios", "gpio" };
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100144 char prop_name[32]; /* 32 is max size of property name */
Mika Westerberg40b73182014-10-21 13:33:59 +0200145 struct gpio_desc **dr;
146 struct gpio_desc *desc;
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100147 unsigned int i;
Mika Westerberg40b73182014-10-21 13:33:59 +0200148
149 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
150 GFP_KERNEL);
151 if (!dr)
152 return ERR_PTR(-ENOMEM);
153
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100154 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
155 if (con_id)
156 snprintf(prop_name, sizeof(prop_name), "%s-%s",
157 con_id, suffixes[i]);
158 else
159 snprintf(prop_name, sizeof(prop_name), "%s",
160 suffixes[i]);
161
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200162 desc = fwnode_get_named_gpiod(child, prop_name, flags);
Geert Uytterhoeven40c8eab2016-02-19 11:00:50 +0100163 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100164 break;
165 }
Mika Westerberg40b73182014-10-21 13:33:59 +0200166 if (IS_ERR(desc)) {
167 devres_free(dr);
168 return desc;
169 }
170
171 *dr = desc;
172 devres_add(dev, dr);
173
174 return desc;
175}
176EXPORT_SYMBOL(devm_get_gpiod_from_child);
177
178/**
Thierry Reding29a1f2332014-04-25 17:10:06 +0200179 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
180 * @dev: GPIO consumer
181 * @con_id: function within the GPIO consumer
182 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900183 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +0200184 *
185 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
186 * function are automatically disposed on driver detach. See
187 * gpiod_get_index_optional() for detailed information about behavior and
188 * return values.
189 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100190struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +0200191 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900192 unsigned int index,
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100193 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200194{
195 struct gpio_desc *desc;
196
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900197 desc = devm_gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +0200198 if (IS_ERR(desc)) {
199 if (PTR_ERR(desc) == -ENOENT)
200 return NULL;
201 }
202
203 return desc;
204}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100205EXPORT_SYMBOL(devm_gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +0200206
207/**
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +0100208 * devm_gpiod_get_array - Resource-managed gpiod_get_array()
209 * @dev: GPIO consumer
210 * @con_id: function within the GPIO consumer
211 * @flags: optional GPIO initialization flags
212 *
213 * Managed gpiod_get_array(). GPIO descriptors returned from this function are
214 * automatically disposed on driver detach. See gpiod_get_array() for detailed
215 * information about behavior and return values.
216 */
217struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
218 const char *con_id,
219 enum gpiod_flags flags)
220{
221 struct gpio_descs **dr;
222 struct gpio_descs *descs;
223
224 dr = devres_alloc(devm_gpiod_release_array,
225 sizeof(struct gpio_descs *), GFP_KERNEL);
226 if (!dr)
227 return ERR_PTR(-ENOMEM);
228
229 descs = gpiod_get_array(dev, con_id, flags);
230 if (IS_ERR(descs)) {
231 devres_free(dr);
232 return descs;
233 }
234
235 *dr = descs;
236 devres_add(dev, dr);
237
238 return descs;
239}
240EXPORT_SYMBOL(devm_gpiod_get_array);
241
242/**
243 * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
244 * @dev: GPIO consumer
245 * @con_id: function within the GPIO consumer
246 * @flags: optional GPIO initialization flags
247 *
248 * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
249 * function are automatically disposed on driver detach.
250 * See gpiod_get_array_optional() for detailed information about behavior and
251 * return values.
252 */
253struct gpio_descs *__must_check
254devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
255 enum gpiod_flags flags)
256{
257 struct gpio_descs *descs;
258
259 descs = devm_gpiod_get_array(dev, con_id, flags);
260 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
261 return NULL;
262
263 return descs;
264}
265EXPORT_SYMBOL(devm_gpiod_get_array_optional);
266
267/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700268 * devm_gpiod_put - Resource-managed gpiod_put()
269 * @desc: GPIO descriptor to dispose of
270 *
271 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
272 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
273 * will be disposed of by the resource management code.
274 */
275void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
276{
277 WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
278 &desc));
279}
280EXPORT_SYMBOL(devm_gpiod_put);
281
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +0100282/**
283 * devm_gpiod_put_array - Resource-managed gpiod_put_array()
284 * @descs: GPIO descriptor array to dispose of
285 *
286 * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
287 * Normally this function will not be called as the GPIOs will be disposed of
288 * by the resource management code.
289 */
290void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
291{
292 WARN_ON(devres_release(dev, devm_gpiod_release_array,
293 devm_gpiod_match_array, &descs));
294}
295EXPORT_SYMBOL(devm_gpiod_put_array);
296
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700297
298
299
John Crispin1a0703e2011-12-20 21:40:21 +0100300static void devm_gpio_release(struct device *dev, void *res)
301{
302 unsigned *gpio = res;
303
304 gpio_free(*gpio);
305}
306
307static int devm_gpio_match(struct device *dev, void *res, void *data)
308{
309 unsigned *this = res, *gpio = data;
310
311 return *this == *gpio;
312}
313
314/**
Wolfram Sang713b7ef2013-06-04 17:48:36 +0200315 * devm_gpio_request - request a GPIO for a managed device
316 * @dev: device to request the GPIO for
317 * @gpio: GPIO to allocate
318 * @label: the name of the requested GPIO
John Crispin1a0703e2011-12-20 21:40:21 +0100319 *
320 * Except for the extra @dev argument, this function takes the
321 * same arguments and performs the same function as
322 * gpio_request(). GPIOs requested with this function will be
323 * automatically freed on driver detach.
324 *
325 * If an GPIO allocated with this function needs to be freed
326 * separately, devm_gpio_free() must be used.
327 */
328
329int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
330{
331 unsigned *dr;
332 int rc;
333
334 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
335 if (!dr)
336 return -ENOMEM;
337
338 rc = gpio_request(gpio, label);
339 if (rc) {
340 devres_free(dr);
341 return rc;
342 }
343
344 *dr = gpio;
345 devres_add(dev, dr);
346
347 return 0;
348}
349EXPORT_SYMBOL(devm_gpio_request);
350
351/**
Mark Brown09d71ff2012-05-02 12:46:46 +0100352 * devm_gpio_request_one - request a single GPIO with initial setup
353 * @dev: device to request for
354 * @gpio: the GPIO number
355 * @flags: GPIO configuration as specified by GPIOF_*
356 * @label: a literal description string of this GPIO
357 */
358int devm_gpio_request_one(struct device *dev, unsigned gpio,
359 unsigned long flags, const char *label)
360{
361 unsigned *dr;
362 int rc;
363
364 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
365 if (!dr)
366 return -ENOMEM;
367
368 rc = gpio_request_one(gpio, flags, label);
369 if (rc) {
370 devres_free(dr);
371 return rc;
372 }
373
374 *dr = gpio;
375 devres_add(dev, dr);
376
377 return 0;
378}
Stephen Warren3996bfc2012-06-07 17:56:09 -0600379EXPORT_SYMBOL(devm_gpio_request_one);
Mark Brown09d71ff2012-05-02 12:46:46 +0100380
381/**
Wolfram Sang713b7ef2013-06-04 17:48:36 +0200382 * devm_gpio_free - free a GPIO
383 * @dev: device to free GPIO for
384 * @gpio: GPIO to free
John Crispin1a0703e2011-12-20 21:40:21 +0100385 *
386 * Except for the extra @dev argument, this function takes the
387 * same arguments and performs the same function as gpio_free().
388 * This function instead of gpio_free() should be used to manually
389 * free GPIOs allocated with devm_gpio_request().
390 */
391void devm_gpio_free(struct device *dev, unsigned int gpio)
392{
393
Mark Browna85990b2012-05-03 18:15:14 +0100394 WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
John Crispin1a0703e2011-12-20 21:40:21 +0100395 &gpio));
John Crispin1a0703e2011-12-20 21:40:21 +0100396}
397EXPORT_SYMBOL(devm_gpio_free);