Linux 3.11-rc3
[cris-mirror.git] / drivers / gpio / devres.c
blob3e7812f0405e8d91d507384ec1f2d183afc3ce44
1 /*
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_gpio_release(struct device *dev, void *res)
24 unsigned *gpio = res;
26 gpio_free(*gpio);
29 static int devm_gpio_match(struct device *dev, void *res, void *data)
31 unsigned *this = res, *gpio = data;
33 return *this == *gpio;
36 /**
37 * devm_gpio_request - request a GPIO for a managed device
38 * @dev: device to request the GPIO for
39 * @gpio: GPIO to allocate
40 * @label: the name of the requested GPIO
42 * Except for the extra @dev argument, this function takes the
43 * same arguments and performs the same function as
44 * gpio_request(). GPIOs requested with this function will be
45 * automatically freed on driver detach.
47 * If an GPIO allocated with this function needs to be freed
48 * separately, devm_gpio_free() must be used.
51 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
53 unsigned *dr;
54 int rc;
56 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
57 if (!dr)
58 return -ENOMEM;
60 rc = gpio_request(gpio, label);
61 if (rc) {
62 devres_free(dr);
63 return rc;
66 *dr = gpio;
67 devres_add(dev, dr);
69 return 0;
71 EXPORT_SYMBOL(devm_gpio_request);
73 /**
74 * devm_gpio_request_one - request a single GPIO with initial setup
75 * @dev: device to request for
76 * @gpio: the GPIO number
77 * @flags: GPIO configuration as specified by GPIOF_*
78 * @label: a literal description string of this GPIO
80 int devm_gpio_request_one(struct device *dev, unsigned gpio,
81 unsigned long flags, const char *label)
83 unsigned *dr;
84 int rc;
86 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
87 if (!dr)
88 return -ENOMEM;
90 rc = gpio_request_one(gpio, flags, label);
91 if (rc) {
92 devres_free(dr);
93 return rc;
96 *dr = gpio;
97 devres_add(dev, dr);
99 return 0;
101 EXPORT_SYMBOL(devm_gpio_request_one);
104 * devm_gpio_free - free a GPIO
105 * @dev: device to free GPIO for
106 * @gpio: GPIO to free
108 * Except for the extra @dev argument, this function takes the
109 * same arguments and performs the same function as gpio_free().
110 * This function instead of gpio_free() should be used to manually
111 * free GPIOs allocated with devm_gpio_request().
113 void devm_gpio_free(struct device *dev, unsigned int gpio)
116 WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
117 &gpio));
119 EXPORT_SYMBOL(devm_gpio_free);