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
;
39 * devm_gpiod_get - Resource-managed gpiod_get()
41 * @con_id: function within the GPIO consumer
43 * Managed gpiod_get(). GPIO descriptors returned from this function are
44 * automatically disposed on driver detach. See gpiod_get() for detailed
45 * information about behavior and return values.
47 struct gpio_desc
*__must_check
devm_gpiod_get(struct device
*dev
,
50 return devm_gpiod_get_index(dev
, con_id
, 0);
52 EXPORT_SYMBOL(devm_gpiod_get
);
55 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
57 * @con_id: function within the GPIO consumer
58 * @idx: index of the GPIO to obtain in the consumer
60 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
61 * automatically disposed on driver detach. See gpiod_get_index() for detailed
62 * information about behavior and return values.
64 struct gpio_desc
*__must_check
devm_gpiod_get_index(struct device
*dev
,
68 struct gpio_desc
**dr
;
69 struct gpio_desc
*desc
;
71 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpiod_desc
*),
74 return ERR_PTR(-ENOMEM
);
76 desc
= gpiod_get_index(dev
, con_id
, idx
);
87 EXPORT_SYMBOL(devm_gpiod_get_index
);
90 * devm_gpiod_put - Resource-managed gpiod_put()
91 * @desc: GPIO descriptor to dispose of
93 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
94 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
95 * will be disposed of by the resource management code.
97 void devm_gpiod_put(struct device
*dev
, struct gpio_desc
*desc
)
99 WARN_ON(devres_release(dev
, devm_gpiod_release
, devm_gpiod_match
,
102 EXPORT_SYMBOL(devm_gpiod_put
);
107 static void devm_gpio_release(struct device
*dev
, void *res
)
109 unsigned *gpio
= res
;
114 static int devm_gpio_match(struct device
*dev
, void *res
, void *data
)
116 unsigned *this = res
, *gpio
= data
;
118 return *this == *gpio
;
122 * devm_gpio_request - request a GPIO for a managed device
123 * @dev: device to request the GPIO for
124 * @gpio: GPIO to allocate
125 * @label: the name of the requested GPIO
127 * Except for the extra @dev argument, this function takes the
128 * same arguments and performs the same function as
129 * gpio_request(). GPIOs requested with this function will be
130 * automatically freed on driver detach.
132 * If an GPIO allocated with this function needs to be freed
133 * separately, devm_gpio_free() must be used.
136 int devm_gpio_request(struct device
*dev
, unsigned gpio
, const char *label
)
141 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
145 rc
= gpio_request(gpio
, label
);
156 EXPORT_SYMBOL(devm_gpio_request
);
159 * devm_gpio_request_one - request a single GPIO with initial setup
160 * @dev: device to request for
161 * @gpio: the GPIO number
162 * @flags: GPIO configuration as specified by GPIOF_*
163 * @label: a literal description string of this GPIO
165 int devm_gpio_request_one(struct device
*dev
, unsigned gpio
,
166 unsigned long flags
, const char *label
)
171 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
175 rc
= gpio_request_one(gpio
, flags
, label
);
186 EXPORT_SYMBOL(devm_gpio_request_one
);
189 * devm_gpio_free - free a GPIO
190 * @dev: device to free GPIO for
191 * @gpio: GPIO to free
193 * Except for the extra @dev argument, this function takes the
194 * same arguments and performs the same function as gpio_free().
195 * This function instead of gpio_free() should be used to manually
196 * free GPIOs allocated with devm_gpio_request().
198 void devm_gpio_free(struct device
*dev
, unsigned int gpio
)
201 WARN_ON(devres_release(dev
, devm_gpio_release
, devm_gpio_match
,
204 EXPORT_SYMBOL(devm_gpio_free
);