diff options
author | John Crispin <blogic@openwrt.org> | 2011-12-20 15:40:21 -0500 |
---|---|---|
committer | Grant Likely <grant.likely@secretlab.ca> | 2012-01-04 13:37:42 -0500 |
commit | 1a0703ede4493bd74f9c6b53f782b749e175a88e (patch) | |
tree | ee86f6148f53af7ddcc329a28f45d7679642780d /drivers/gpio | |
parent | e198a8de14d4bb122b821432fadb28eedd4b4507 (diff) |
GPIO: add bindings for managed devices
This patch adds 2 functions that allow managed devices to request GPIOs.
These GPIOs will then be managed by drivers/base/devres.c.
Signed-off-by: John Crispin <blogic@openwrt.org>
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
Diffstat (limited to 'drivers/gpio')
-rw-r--r-- | drivers/gpio/Makefile | 2 | ||||
-rw-r--r-- | drivers/gpio/devres.c | 90 |
2 files changed, 91 insertions, 1 deletions
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 4e018d6a7639..76dbd3f55ef5 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | ccflags-$(CONFIG_DEBUG_GPIO) += -DDEBUG | 3 | ccflags-$(CONFIG_DEBUG_GPIO) += -DDEBUG |
4 | 4 | ||
5 | obj-$(CONFIG_GPIOLIB) += gpiolib.o | 5 | obj-$(CONFIG_GPIOLIB) += gpiolib.o devres.o |
6 | 6 | ||
7 | # Device drivers. Generally keep list sorted alphabetically | 7 | # Device drivers. Generally keep list sorted alphabetically |
8 | obj-$(CONFIG_GPIO_GENERIC) += gpio-generic.o | 8 | obj-$(CONFIG_GPIO_GENERIC) += gpio-generic.o |
diff --git a/drivers/gpio/devres.c b/drivers/gpio/devres.c new file mode 100644 index 000000000000..3dd29399cef5 --- /dev/null +++ b/drivers/gpio/devres.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
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> | ||
18 | #include <linux/gpio.h> | ||
19 | #include <linux/device.h> | ||
20 | #include <linux/gfp.h> | ||
21 | |||
22 | static void devm_gpio_release(struct device *dev, void *res) | ||
23 | { | ||
24 | unsigned *gpio = res; | ||
25 | |||
26 | gpio_free(*gpio); | ||
27 | } | ||
28 | |||
29 | static int devm_gpio_match(struct device *dev, void *res, void *data) | ||
30 | { | ||
31 | unsigned *this = res, *gpio = data; | ||
32 | |||
33 | return *this == *gpio; | ||
34 | } | ||
35 | |||
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 | ||
41 | * | ||
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. | ||
46 | * | ||
47 | * If an GPIO allocated with this function needs to be freed | ||
48 | * separately, devm_gpio_free() must be used. | ||
49 | */ | ||
50 | |||
51 | int devm_gpio_request(struct device *dev, unsigned gpio, const char *label) | ||
52 | { | ||
53 | unsigned *dr; | ||
54 | int rc; | ||
55 | |||
56 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); | ||
57 | if (!dr) | ||
58 | return -ENOMEM; | ||
59 | |||
60 | rc = gpio_request(gpio, label); | ||
61 | if (rc) { | ||
62 | devres_free(dr); | ||
63 | return rc; | ||
64 | } | ||
65 | |||
66 | *dr = gpio; | ||
67 | devres_add(dev, dr); | ||
68 | |||
69 | return 0; | ||
70 | } | ||
71 | EXPORT_SYMBOL(devm_gpio_request); | ||
72 | |||
73 | /** | ||
74 | * devm_gpio_free - free an interrupt | ||
75 | * @dev: device to free gpio for | ||
76 | * @gpio: gpio to free | ||
77 | * | ||
78 | * Except for the extra @dev argument, this function takes the | ||
79 | * same arguments and performs the same function as gpio_free(). | ||
80 | * This function instead of gpio_free() should be used to manually | ||
81 | * free GPIOs allocated with devm_gpio_request(). | ||
82 | */ | ||
83 | void devm_gpio_free(struct device *dev, unsigned int gpio) | ||
84 | { | ||
85 | |||
86 | WARN_ON(devres_destroy(dev, devm_gpio_release, devm_gpio_match, | ||
87 | &gpio)); | ||
88 | gpio_free(gpio); | ||
89 | } | ||
90 | EXPORT_SYMBOL(devm_gpio_free); | ||