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
42 * @flags: optional GPIO initialization flags
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.
48 struct gpio_desc
*__must_check
__devm_gpiod_get(struct device
*dev
,
50 enum gpiod_flags flags
)
52 return devm_gpiod_get_index(dev
, con_id
, 0, flags
);
54 EXPORT_SYMBOL(__devm_gpiod_get
);
57 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
59 * @con_id: function within the GPIO consumer
60 * @flags: optional GPIO initialization flags
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.
66 struct gpio_desc
*__must_check
__devm_gpiod_get_optional(struct device
*dev
,
68 enum gpiod_flags flags
)
70 return devm_gpiod_get_index_optional(dev
, con_id
, 0, flags
);
72 EXPORT_SYMBOL(__devm_gpiod_get_optional
);
75 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
77 * @con_id: function within the GPIO consumer
78 * @idx: index of the GPIO to obtain in the consumer
79 * @flags: optional GPIO initialization flags
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.
85 struct gpio_desc
*__must_check
__devm_gpiod_get_index(struct device
*dev
,
88 enum gpiod_flags flags
)
90 struct gpio_desc
**dr
;
91 struct gpio_desc
*desc
;
93 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
96 return ERR_PTR(-ENOMEM
);
98 desc
= gpiod_get_index(dev
, con_id
, idx
, flags
);
109 EXPORT_SYMBOL(__devm_gpiod_get_index
);
112 * devm_get_gpiod_from_child - get a GPIO descriptor from a device's child node
113 * @dev: GPIO consumer
114 * @child: firmware node (child of @dev)
116 * GPIO descriptors returned from this function are automatically disposed on
119 struct gpio_desc
*devm_get_gpiod_from_child(struct device
*dev
,
120 struct fwnode_handle
*child
)
122 struct gpio_desc
**dr
;
123 struct gpio_desc
*desc
;
125 dr
= devres_alloc(devm_gpiod_release
, sizeof(struct gpio_desc
*),
128 return ERR_PTR(-ENOMEM
);
130 desc
= fwnode_get_named_gpiod(child
, "gpios");
141 EXPORT_SYMBOL(devm_get_gpiod_from_child
);
144 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
145 * @dev: GPIO consumer
146 * @con_id: function within the GPIO consumer
147 * @index: index of the GPIO to obtain in the consumer
148 * @flags: optional GPIO initialization flags
150 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
151 * function are automatically disposed on driver detach. See
152 * gpiod_get_index_optional() for detailed information about behavior and
155 struct gpio_desc
*__must_check
__devm_gpiod_get_index_optional(struct device
*dev
,
158 enum gpiod_flags flags
)
160 struct gpio_desc
*desc
;
162 desc
= devm_gpiod_get_index(dev
, con_id
, index
, flags
);
164 if (PTR_ERR(desc
) == -ENOENT
)
170 EXPORT_SYMBOL(__devm_gpiod_get_index_optional
);
173 * devm_gpiod_put - Resource-managed gpiod_put()
174 * @desc: GPIO descriptor to dispose of
176 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
177 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
178 * will be disposed of by the resource management code.
180 void devm_gpiod_put(struct device
*dev
, struct gpio_desc
*desc
)
182 WARN_ON(devres_release(dev
, devm_gpiod_release
, devm_gpiod_match
,
185 EXPORT_SYMBOL(devm_gpiod_put
);
190 static void devm_gpio_release(struct device
*dev
, void *res
)
192 unsigned *gpio
= res
;
197 static int devm_gpio_match(struct device
*dev
, void *res
, void *data
)
199 unsigned *this = res
, *gpio
= data
;
201 return *this == *gpio
;
205 * devm_gpio_request - request a GPIO for a managed device
206 * @dev: device to request the GPIO for
207 * @gpio: GPIO to allocate
208 * @label: the name of the requested GPIO
210 * Except for the extra @dev argument, this function takes the
211 * same arguments and performs the same function as
212 * gpio_request(). GPIOs requested with this function will be
213 * automatically freed on driver detach.
215 * If an GPIO allocated with this function needs to be freed
216 * separately, devm_gpio_free() must be used.
219 int devm_gpio_request(struct device
*dev
, unsigned gpio
, const char *label
)
224 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
228 rc
= gpio_request(gpio
, label
);
239 EXPORT_SYMBOL(devm_gpio_request
);
242 * devm_gpio_request_one - request a single GPIO with initial setup
243 * @dev: device to request for
244 * @gpio: the GPIO number
245 * @flags: GPIO configuration as specified by GPIOF_*
246 * @label: a literal description string of this GPIO
248 int devm_gpio_request_one(struct device
*dev
, unsigned gpio
,
249 unsigned long flags
, const char *label
)
254 dr
= devres_alloc(devm_gpio_release
, sizeof(unsigned), GFP_KERNEL
);
258 rc
= gpio_request_one(gpio
, flags
, label
);
269 EXPORT_SYMBOL(devm_gpio_request_one
);
272 * devm_gpio_free - free a GPIO
273 * @dev: device to free GPIO for
274 * @gpio: GPIO to free
276 * Except for the extra @dev argument, this function takes the
277 * same arguments and performs the same function as gpio_free().
278 * This function instead of gpio_free() should be used to manually
279 * free GPIOs allocated with devm_gpio_request().
281 void devm_gpio_free(struct device
*dev
, unsigned int gpio
)
284 WARN_ON(devres_release(dev
, devm_gpio_release
, devm_gpio_match
,
287 EXPORT_SYMBOL(devm_gpio_free
);