1 From 282f1ca84b35f3be68abc4fd8b52e229f3cb6bb7 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 8 Mar 2012 13:23:53 +0100
4 Subject: [PATCH 01/70] GPIO: add bindings for managed devices
6 This patch adds 2 functions that allow managed devices to request GPIOs.
7 These GPIOs will then be managed by drivers/base/devres.c.
9 Signed-off-by: John Crispin <blogic@openwrt.org>
10 Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
12 drivers/gpio/Makefile | 2 +-
13 drivers/gpio/devres.c | 90 ++++++++++++++++++++++++++++++++++++++++++++
14 include/asm-generic/gpio.h | 4 ++
15 3 files changed, 95 insertions(+), 1 deletions(-)
16 create mode 100644 drivers/gpio/devres.c
18 --- a/drivers/gpio/Makefile
19 +++ b/drivers/gpio/Makefile
22 ccflags-$(CONFIG_DEBUG_GPIO) += -DDEBUG
24 -obj-$(CONFIG_GPIOLIB) += gpiolib.o
25 +obj-$(CONFIG_GPIOLIB) += gpiolib.o devres.o
27 # Device drivers. Generally keep list sorted alphabetically
28 obj-$(CONFIG_GPIO_GENERIC) += gpio-generic.o
30 +++ b/drivers/gpio/devres.c
33 + * drivers/gpio/devres.c - managed gpio resources
35 + * This program is free software; you can redistribute it and/or modify
36 + * it under the terms of the GNU General Public License version 2
37 + * as published by the Free Software Foundation.
39 + * You should have received a copy of the GNU General Public License
40 + * along with this program; if not, write to the Free Software
41 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
43 + * This file is based on kernel/irq/devres.c
45 + * Copyright (c) 2011 John Crispin <blogic@openwrt.org>
48 +#include <linux/module.h>
49 +#include <linux/gpio.h>
50 +#include <linux/device.h>
51 +#include <linux/gfp.h>
53 +static void devm_gpio_release(struct device *dev, void *res)
55 + unsigned *gpio = res;
60 +static int devm_gpio_match(struct device *dev, void *res, void *data)
62 + unsigned *this = res, *gpio = data;
64 + return *this == *gpio;
68 + * devm_gpio_request - request a gpio for a managed device
69 + * @dev: device to request the gpio for
70 + * @gpio: gpio to allocate
71 + * @label: the name of the requested gpio
73 + * Except for the extra @dev argument, this function takes the
74 + * same arguments and performs the same function as
75 + * gpio_request(). GPIOs requested with this function will be
76 + * automatically freed on driver detach.
78 + * If an GPIO allocated with this function needs to be freed
79 + * separately, devm_gpio_free() must be used.
82 +int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
87 + dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
91 + rc = gpio_request(gpio, label);
98 + devres_add(dev, dr);
102 +EXPORT_SYMBOL(devm_gpio_request);
105 + * devm_gpio_free - free an interrupt
106 + * @dev: device to free gpio for
107 + * @gpio: gpio to free
109 + * Except for the extra @dev argument, this function takes the
110 + * same arguments and performs the same function as gpio_free().
111 + * This function instead of gpio_free() should be used to manually
112 + * free GPIOs allocated with devm_gpio_request().
114 +void devm_gpio_free(struct device *dev, unsigned int gpio)
117 + WARN_ON(devres_destroy(dev, devm_gpio_release, devm_gpio_match,
121 +EXPORT_SYMBOL(devm_gpio_free);
122 --- a/include/asm-generic/gpio.h
123 +++ b/include/asm-generic/gpio.h
124 @@ -175,6 +175,10 @@ extern int gpio_request_one(unsigned gpi
125 extern int gpio_request_array(const struct gpio *array, size_t num);
126 extern void gpio_free_array(const struct gpio *array, size_t num);
128 +/* bindings for managed devices that want to request gpios */
129 +int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
130 +void devm_gpio_free(struct device *dev, unsigned int gpio);
132 #ifdef CONFIG_GPIO_SYSFS