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/gpio.h>
19 #include <linux/device.h>
20 #include <linux/gfp.h>
22 static void devm_gpiod_release(struct device *dev, void *res)
24 struct gpio_desc **desc = res;
29 static int devm_gpiod_match(struct device *dev, void *res, void *data)
31 struct gpio_desc **this = res, **gpio = data;
33 return *this == *gpio;
37 * devm_gpiod_get - Resource-managed gpiod_get()
39 * @con_id: function within the GPIO consumer
41 * Managed gpiod_get(). GPIO descriptors returned from this function are
42 * automatically disposed on driver detach. See gpiod_get() for detailed
43 * information about behavior and return values.
45 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
48 return devm_gpiod_get_index(dev, con_id, 0);
50 EXPORT_SYMBOL(devm_gpiod_get);
53 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
55 * @con_id: function within the GPIO consumer
56 * @idx: index of the GPIO to obtain in the consumer
58 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
59 * automatically disposed on driver detach. See gpiod_get_index() for detailed
60 * information about behavior and return values.
62 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
66 struct gpio_desc **dr;
67 struct gpio_desc *desc;
69 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpiod_desc *),
72 return ERR_PTR(-ENOMEM);
74 desc = gpiod_get_index(dev, con_id, idx);
85 EXPORT_SYMBOL(devm_gpiod_get_index);
88 * devm_gpiod_put - Resource-managed gpiod_put()
89 * @desc: GPIO descriptor to dispose of
91 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
92 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
93 * will be disposed of by the resource management code.
95 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
97 WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
100 EXPORT_SYMBOL(devm_gpiod_put);
105 static void devm_gpio_release(struct device *dev, void *res)
107 unsigned *gpio = res;
112 static int devm_gpio_match(struct device *dev, void *res, void *data)
114 unsigned *this = res, *gpio = data;
116 return *this == *gpio;
120 * devm_gpio_request - request a GPIO for a managed device
121 * @dev: device to request the GPIO for
122 * @gpio: GPIO to allocate
123 * @label: the name of the requested GPIO
125 * Except for the extra @dev argument, this function takes the
126 * same arguments and performs the same function as
127 * gpio_request(). GPIOs requested with this function will be
128 * automatically freed on driver detach.
130 * If an GPIO allocated with this function needs to be freed
131 * separately, devm_gpio_free() must be used.
134 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
139 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
143 rc = gpio_request(gpio, label);
154 EXPORT_SYMBOL(devm_gpio_request);
157 * devm_gpio_request_one - request a single GPIO with initial setup
158 * @dev: device to request for
159 * @gpio: the GPIO number
160 * @flags: GPIO configuration as specified by GPIOF_*
161 * @label: a literal description string of this GPIO
163 int devm_gpio_request_one(struct device *dev, unsigned gpio,
164 unsigned long flags, const char *label)
169 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
173 rc = gpio_request_one(gpio, flags, label);
184 EXPORT_SYMBOL(devm_gpio_request_one);
187 * devm_gpio_free - free a GPIO
188 * @dev: device to free GPIO for
189 * @gpio: GPIO to free
191 * Except for the extra @dev argument, this function takes the
192 * same arguments and performs the same function as gpio_free().
193 * This function instead of gpio_free() should be used to manually
194 * free GPIOs allocated with devm_gpio_request().
196 void devm_gpio_free(struct device *dev, unsigned int gpio)
199 WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
202 EXPORT_SYMBOL(devm_gpio_free);