1 From eda15425bcd2703ea1cfeebd65847305c17e5f0a Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Wed, 27 Mar 2013 18:38:48 +0100
4 Subject: [PATCH] owrt: GPIO: add gpio_export_with_name
6 http://lists.infradead.org/pipermail/linux-arm-kernel/2012-November/133856.html
8 Signed-off-by: John Crispin <blogic@openwrt.org>
10 Documentation/devicetree/bindings/gpio/gpio.txt | 60 ++++++++++++++++++++++
11 drivers/gpio/gpiolib-of.c | 61 +++++++++++++++++++++++
12 drivers/gpio/gpiolib.c | 18 +++++--
13 include/asm-generic/gpio.h | 6 ++-
14 include/linux/gpio.h | 23 ++++++++-
15 5 files changed, 160 insertions(+), 8 deletions(-)
17 --- a/Documentation/devicetree/bindings/gpio/gpio.txt
18 +++ b/Documentation/devicetree/bindings/gpio/gpio.txt
19 @@ -112,3 +112,63 @@ where,
21 The pinctrl node must have "#gpio-range-cells" property to show number of
22 arguments to pass with phandle from gpio controllers node.
27 +gpio-export will allow you to automatically export gpio
30 +- compatible: Should be "gpio-export"
32 +in each child node will reprensent a gpio or if no name is specified
33 +a list of gpio to export
36 +- gpios: gpio to export
39 + - gpio-export,name: export name
40 + - gpio-export,output: to set the as output with default value
41 + if no present gpio as input
42 + - pio-export,direction_may_change: boolean to allow the direction to be controllable
48 + compatible = "gpio-export";
52 + gpio-export,name = "in";
53 + gpios = <&pioC 20 0>;
57 + gpio-export,name = "out";
58 + gpio-export,output = <1>;
59 + gpio-export,direction_may_change;
60 + gpios = <&pioC 21 0>;
64 + gpio-export,name = "in_out";
65 + gpio-export,direction_may_change;
66 + gpios = <&pioC 21 0>;
73 + gpio-export,direction_may_change;
80 + gpio-export,output = <1>;
83 --- a/drivers/gpio/gpiolib-of.c
84 +++ b/drivers/gpio/gpiolib-of.c
86 #include <linux/of_gpio.h>
87 #include <linux/pinctrl/pinctrl.h>
88 #include <linux/slab.h>
89 +#include <linux/init.h>
90 +#include <linux/platform_device.h>
92 /* Private data structure for of_gpiochip_find_and_xlate */
94 @@ -289,3 +291,62 @@ void of_gpiochip_remove(struct gpio_chip
96 of_node_put(chip->of_node);
99 +static struct of_device_id gpio_export_ids[] = {
100 + { .compatible = "gpio-export" },
104 +static int __init of_gpio_export_probe(struct platform_device *pdev)
106 + struct device_node *np = pdev->dev.of_node;
107 + struct device_node *cnp;
111 + for_each_child_of_node(np, cnp) {
112 + const char *name = NULL;
118 + of_property_read_string(cnp, "gpio-export,name", &name);
121 + max_gpio = of_gpio_count(cnp);
123 + for (i = 0; i < max_gpio; i++) {
124 + gpio = of_get_gpio(cnp, i);
125 + if (devm_gpio_request(&pdev->dev, gpio, name ? name : of_node_full_name(np)))
128 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
129 + gpio_direction_output(gpio, val);
131 + gpio_direction_input(gpio);
133 + dmc = of_property_read_bool(np, "gpio-export,direction_may_change");
134 + gpio_export_with_name(gpio, dmc, name);
139 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
144 +static struct platform_driver gpio_export_driver = {
146 + .name = "gpio-export",
147 + .owner = THIS_MODULE,
148 + .of_match_table = of_match_ptr(gpio_export_ids),
152 +static int __init of_gpio_export_init(void)
154 + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
156 +device_initcall(of_gpio_export_init);
157 --- a/drivers/gpio/gpiolib.c
158 +++ b/drivers/gpio/gpiolib.c
159 @@ -714,9 +714,10 @@ static struct class gpio_class = {
163 - * gpio_export - export a GPIO through sysfs
164 + * gpio_export_with_name - export a GPIO through sysfs
165 * @gpio: gpio to make available, already requested
166 * @direction_may_change: true if userspace may change gpio direction
168 * Context: arch_initcall or later
170 * When drivers want to make a GPIO accessible to userspace after they
171 @@ -728,7 +729,7 @@ static struct class gpio_class = {
173 * Returns zero on success, else an error.
175 -int gpio_export(unsigned gpio, bool direction_may_change)
176 +int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
179 struct gpio_desc *desc;
180 @@ -762,6 +763,8 @@ int gpio_export(unsigned gpio, bool dire
186 if (!desc->chip->direction_input || !desc->chip->direction_output)
187 direction_may_change = false;
188 spin_unlock_irqrestore(&gpio_lock, flags);
189 @@ -804,7 +807,7 @@ fail_unlock:
190 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
193 -EXPORT_SYMBOL_GPL(gpio_export);
194 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
196 static int match_export(struct device *dev, void *data)
198 --- a/include/asm-generic/gpio.h
199 +++ b/include/asm-generic/gpio.h
200 @@ -204,7 +204,8 @@ void devm_gpio_free(struct device *dev,
201 * A sysfs interface can be exported by individual drivers if they want,
202 * but more typically is configured entirely from userspace.
204 -extern int gpio_export(unsigned gpio, bool direction_may_change);
205 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
207 extern int gpio_export_link(struct device *dev, const char *name,
209 extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
210 @@ -249,7 +250,8 @@ struct device;
212 /* sysfs support is only available with gpiolib, where it's optional */
214 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
215 +static inline int gpio_export_with_name(unsigned gpio,
216 + bool direction_may_change, const char *name)
220 --- a/include/linux/gpio.h
221 +++ b/include/linux/gpio.h
222 @@ -189,7 +189,8 @@ static inline void gpio_set_value_cansle
226 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
227 +static inline int gpio_export_with_name(unsigned gpio,
228 + bool direction_may_change, const char *name)
230 /* GPIO can never have been requested or set as {in,out}put */
232 @@ -248,4 +249,24 @@ gpiochip_remove_pin_ranges(struct gpio_c
234 #endif /* ! CONFIG_GENERIC_GPIO */
237 + * gpio_export - export a GPIO through sysfs
238 + * @gpio: gpio to make available, already requested
239 + * @direction_may_change: true if userspace may change gpio direction
240 + * Context: arch_initcall or later
242 + * When drivers want to make a GPIO accessible to userspace after they
243 + * have requested it -- perhaps while debugging, or as part of their
244 + * public interface -- they may use this routine. If the GPIO can
245 + * change direction (some can't) and the caller allows it, userspace
246 + * will see "direction" sysfs attribute which may be used to change
247 + * the gpio's direction. A "value" attribute will always be provided.
249 + * Returns zero on success, else an error.
251 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
253 + return gpio_export_with_name(gpio, direction_may_change, NULL);
256 #endif /* __LINUX_GPIO_H */