blob: 954b9f6b0ef82eb74437e0efce4781b53ba58e0f [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
38/**
39 * devm_gpiod_get - Resource-managed gpiod_get()
40 * @dev: GPIO consumer
41 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090042 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -070043 *
44 * Managed gpiod_get(). GPIO descriptors returned from this function are
45 * automatically disposed on driver detach. See gpiod_get() for detailed
46 * information about behavior and return values.
47 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090048struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
49 const char *con_id,
50 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -070051{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090052 return devm_gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070053}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090054EXPORT_SYMBOL(__devm_gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070055
56/**
Thierry Reding29a1f2332014-04-25 17:10:06 +020057 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
58 * @dev: GPIO consumer
59 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090060 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +020061 *
62 * Managed gpiod_get_optional(). GPIO descriptors returned from this function
63 * are automatically disposed on driver detach. See gpiod_get_optional() for
64 * detailed information about behavior and return values.
65 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090066struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
67 const char *con_id,
68 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +020069{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090070 return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +020071}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090072EXPORT_SYMBOL(__devm_gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +020073
74/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -070075 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
76 * @dev: GPIO consumer
77 * @con_id: function within the GPIO consumer
78 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090079 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -070080 *
81 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
82 * automatically disposed on driver detach. See gpiod_get_index() for detailed
83 * information about behavior and return values.
84 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090085struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -070086 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090087 unsigned int idx,
88 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -070089{
90 struct gpio_desc **dr;
91 struct gpio_desc *desc;
92
Julia Lawall0f05a3a2014-07-29 17:16:48 +020093 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
Alexandre Courbotbae48da2013-10-17 10:21:38 -070094 GFP_KERNEL);
95 if (!dr)
96 return ERR_PTR(-ENOMEM);
97
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090098 desc = gpiod_get_index(dev, con_id, idx, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070099 if (IS_ERR(desc)) {
100 devres_free(dr);
101 return desc;
102 }
103
104 *dr = desc;
105 devres_add(dev, dr);
106
Alexandre Courbot5fcdb9d2013-10-20 15:14:57 -0700107 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700108}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900109EXPORT_SYMBOL(__devm_gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700110
111/**
Thierry Reding29a1f2332014-04-25 17:10:06 +0200112 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
113 * @dev: GPIO consumer
114 * @con_id: function within the GPIO consumer
115 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900116 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +0200117 *
118 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
119 * function are automatically disposed on driver detach. See
120 * gpiod_get_index_optional() for detailed information about behavior and
121 * return values.
122 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900123struct gpio_desc *__must_check __devm_gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +0200124 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900125 unsigned int index,
126 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200127{
128 struct gpio_desc *desc;
129
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900130 desc = devm_gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +0200131 if (IS_ERR(desc)) {
132 if (PTR_ERR(desc) == -ENOENT)
133 return NULL;
134 }
135
136 return desc;
137}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +0900138EXPORT_SYMBOL(__devm_gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +0200139
140/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700141 * devm_gpiod_put - Resource-managed gpiod_put()
142 * @desc: GPIO descriptor to dispose of
143 *
144 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
145 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
146 * will be disposed of by the resource management code.
147 */
148void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
149{
150 WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
151 &desc));
152}
153EXPORT_SYMBOL(devm_gpiod_put);
154
155
156
157
John Crispin1a0703e2011-12-20 21:40:21 +0100158static void devm_gpio_release(struct device *dev, void *res)
159{
160 unsigned *gpio = res;
161
162 gpio_free(*gpio);
163}
164
165static int devm_gpio_match(struct device *dev, void *res, void *data)
166{
167 unsigned *this = res, *gpio = data;
168
169 return *this == *gpio;
170}
171
172/**
Wolfram Sang713b7ef2013-06-04 17:48:36 +0200173 * devm_gpio_request - request a GPIO for a managed device
174 * @dev: device to request the GPIO for
175 * @gpio: GPIO to allocate
176 * @label: the name of the requested GPIO
John Crispin1a0703e2011-12-20 21:40:21 +0100177 *
178 * Except for the extra @dev argument, this function takes the
179 * same arguments and performs the same function as
180 * gpio_request(). GPIOs requested with this function will be
181 * automatically freed on driver detach.
182 *
183 * If an GPIO allocated with this function needs to be freed
184 * separately, devm_gpio_free() must be used.
185 */
186
187int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
188{
189 unsigned *dr;
190 int rc;
191
192 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
193 if (!dr)
194 return -ENOMEM;
195
196 rc = gpio_request(gpio, label);
197 if (rc) {
198 devres_free(dr);
199 return rc;
200 }
201
202 *dr = gpio;
203 devres_add(dev, dr);
204
205 return 0;
206}
207EXPORT_SYMBOL(devm_gpio_request);
208
209/**
Mark Brown09d71ff2012-05-02 12:46:46 +0100210 * devm_gpio_request_one - request a single GPIO with initial setup
211 * @dev: device to request for
212 * @gpio: the GPIO number
213 * @flags: GPIO configuration as specified by GPIOF_*
214 * @label: a literal description string of this GPIO
215 */
216int devm_gpio_request_one(struct device *dev, unsigned gpio,
217 unsigned long flags, const char *label)
218{
219 unsigned *dr;
220 int rc;
221
222 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
223 if (!dr)
224 return -ENOMEM;
225
226 rc = gpio_request_one(gpio, flags, label);
227 if (rc) {
228 devres_free(dr);
229 return rc;
230 }
231
232 *dr = gpio;
233 devres_add(dev, dr);
234
235 return 0;
236}
Stephen Warren3996bfc2012-06-07 17:56:09 -0600237EXPORT_SYMBOL(devm_gpio_request_one);
Mark Brown09d71ff2012-05-02 12:46:46 +0100238
239/**
Wolfram Sang713b7ef2013-06-04 17:48:36 +0200240 * devm_gpio_free - free a GPIO
241 * @dev: device to free GPIO for
242 * @gpio: GPIO to free
John Crispin1a0703e2011-12-20 21:40:21 +0100243 *
244 * Except for the extra @dev argument, this function takes the
245 * same arguments and performs the same function as gpio_free().
246 * This function instead of gpio_free() should be used to manually
247 * free GPIOs allocated with devm_gpio_request().
248 */
249void devm_gpio_free(struct device *dev, unsigned int gpio)
250{
251
Mark Browna85990b2012-05-03 18:15:14 +0100252 WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
John Crispin1a0703e2011-12-20 21:40:21 +0100253 &gpio));
John Crispin1a0703e2011-12-20 21:40:21 +0100254}
255EXPORT_SYMBOL(devm_gpio_free);