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 Index: linux-3.8.11/Documentation/devicetree/bindings/gpio/gpio.txt
18 ===================================================================
19 --- linux-3.8.11.orig/Documentation/devicetree/bindings/gpio/gpio.txt 2013-05-01 18:56:10.000000000 +0200
20 +++ linux-3.8.11/Documentation/devicetree/bindings/gpio/gpio.txt 2013-05-06 12:43:38.252652605 +0200
23 The pinctrl node must have "#gpio-range-cells" property to show number of
24 arguments to pass with phandle from gpio controllers node.
29 +gpio-export will allow you to automatically export gpio
32 +- compatible: Should be "gpio-export"
34 +in each child node will reprensent a gpio or if no name is specified
35 +a list of gpio to export
38 +- gpios: gpio to export
41 + - gpio-export,name: export name
42 + - gpio-export,output: to set the as output with default value
43 + if no present gpio as input
44 + - pio-export,direction_may_change: boolean to allow the direction to be controllable
50 + compatible = "gpio-export";
54 + gpio-export,name = "in";
55 + gpios = <&pioC 20 0>;
59 + gpio-export,name = "out";
60 + gpio-export,output = <1>;
61 + gpio-export,direction_may_change;
62 + gpios = <&pioC 21 0>;
66 + gpio-export,name = "in_out";
67 + gpio-export,direction_may_change;
68 + gpios = <&pioC 21 0>;
75 + gpio-export,direction_may_change;
82 + gpio-export,output = <1>;
85 Index: linux-3.8.11/drivers/gpio/gpiolib-of.c
86 ===================================================================
87 --- linux-3.8.11.orig/drivers/gpio/gpiolib-of.c 2013-05-01 18:56:10.000000000 +0200
88 +++ linux-3.8.11/drivers/gpio/gpiolib-of.c 2013-05-06 13:35:56.452788709 +0200
90 #include <linux/of_gpio.h>
91 #include <linux/pinctrl/pinctrl.h>
92 #include <linux/slab.h>
93 +#include <linux/init.h>
94 +#include <linux/platform_device.h>
96 /* Private data structure for of_gpiochip_find_and_xlate */
100 of_node_put(chip->of_node);
103 +static struct of_device_id gpio_export_ids[] = {
104 + { .compatible = "gpio-export" },
108 +static int __init of_gpio_export_probe(struct platform_device *pdev)
110 + struct device_node *np = pdev->dev.of_node;
111 + struct device_node *cnp;
115 + for_each_child_of_node(np, cnp) {
116 + const char *name = NULL;
122 + of_property_read_string(cnp, "gpio-export,name", &name);
125 + max_gpio = of_gpio_count(cnp);
127 + for (i = 0; i < max_gpio; i++) {
128 + unsigned flags = 0;
129 + enum of_gpio_flags of_flags;
131 + gpio = of_get_gpio_flags(cnp, i, &of_flags);
133 + if (of_flags == OF_GPIO_ACTIVE_LOW)
134 + flags |= GPIOF_ACTIVE_LOW;
136 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
137 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
141 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
144 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
145 + gpio_export_with_name(gpio, dmc, name);
150 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
155 +static struct platform_driver gpio_export_driver = {
157 + .name = "gpio-export",
158 + .owner = THIS_MODULE,
159 + .of_match_table = of_match_ptr(gpio_export_ids),
163 +static int __init of_gpio_export_init(void)
165 + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
167 +device_initcall(of_gpio_export_init);
168 Index: linux-3.8.11/drivers/gpio/gpiolib.c
169 ===================================================================
170 --- linux-3.8.11.orig/drivers/gpio/gpiolib.c 2013-05-01 18:56:10.000000000 +0200
171 +++ linux-3.8.11/drivers/gpio/gpiolib.c 2013-05-06 13:31:35.060777376 +0200
176 - * gpio_export - export a GPIO through sysfs
177 + * gpio_export_with_name - export a GPIO through sysfs
178 * @gpio: gpio to make available, already requested
179 * @direction_may_change: true if userspace may change gpio direction
181 * Context: arch_initcall or later
183 * When drivers want to make a GPIO accessible to userspace after they
186 * Returns zero on success, else an error.
188 -int gpio_export(unsigned gpio, bool direction_may_change)
189 +int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
192 struct gpio_desc *desc;
199 if (!desc->chip->direction_input || !desc->chip->direction_output)
200 direction_may_change = false;
201 spin_unlock_irqrestore(&gpio_lock, flags);
203 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
206 -EXPORT_SYMBOL_GPL(gpio_export);
207 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
209 static int match_export(struct device *dev, void *data)
211 @@ -1418,6 +1421,9 @@
212 if (flags & GPIOF_OPEN_SOURCE)
213 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
215 + if (flags & GPIOF_ACTIVE_LOW)
216 + set_bit(FLAG_ACTIVE_LOW, &gpio_desc[gpio].flags);
218 if (flags & GPIOF_DIR_IN)
219 err = gpio_direction_input(gpio);
221 Index: linux-3.8.11/include/asm-generic/gpio.h
222 ===================================================================
223 --- linux-3.8.11.orig/include/asm-generic/gpio.h 2013-05-01 18:56:10.000000000 +0200
224 +++ linux-3.8.11/include/asm-generic/gpio.h 2013-05-06 12:43:38.252652605 +0200
226 * A sysfs interface can be exported by individual drivers if they want,
227 * but more typically is configured entirely from userspace.
229 -extern int gpio_export(unsigned gpio, bool direction_may_change);
230 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
232 extern int gpio_export_link(struct device *dev, const char *name,
234 extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
237 /* sysfs support is only available with gpiolib, where it's optional */
239 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
240 +static inline int gpio_export_with_name(unsigned gpio,
241 + bool direction_may_change, const char *name)
245 Index: linux-3.8.11/include/linux/gpio.h
246 ===================================================================
247 --- linux-3.8.11.orig/include/linux/gpio.h 2013-05-01 18:56:10.000000000 +0200
248 +++ linux-3.8.11/include/linux/gpio.h 2013-05-06 13:32:59.796781050 +0200
250 #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT)
251 #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
253 +#define GPIOF_ACTIVE_LOW (1 << 6)
257 * struct gpio - a structure describing a GPIO with configuration
258 * @gpio: the GPIO number
263 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
264 +static inline int gpio_export_with_name(unsigned gpio,
265 + bool direction_may_change, const char *name)
267 /* GPIO can never have been requested or set as {in,out}put */
271 #endif /* ! CONFIG_GENERIC_GPIO */
274 + * gpio_export - export a GPIO through sysfs
275 + * @gpio: gpio to make available, already requested
276 + * @direction_may_change: true if userspace may change gpio direction
277 + * Context: arch_initcall or later
279 + * When drivers want to make a GPIO accessible to userspace after they
280 + * have requested it -- perhaps while debugging, or as part of their
281 + * public interface -- they may use this routine. If the GPIO can
282 + * change direction (some can't) and the caller allows it, userspace
283 + * will see "direction" sysfs attribute which may be used to change
284 + * the gpio's direction. A "value" attribute will always be provided.
286 + * Returns zero on success, else an error.
288 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
290 + return gpio_export_with_name(gpio, direction_may_change, NULL);
293 #endif /* __LINUX_GPIO_H */