1 From 7d8a8b0e46a970fba505d967e93123e9729d97b5 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 23 Jun 2013 00:16:22 +0200
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 | 68 +++++++++++++++++++++++
12 drivers/gpio/gpiolib.c | 24 +++++---
13 include/asm-generic/gpio.h | 6 +-
14 include/linux/gpio.h | 26 ++++++++-
15 5 files changed, 172 insertions(+), 12 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 @@ -253,3 +255,69 @@ 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 + unsigned flags = 0;
125 + enum of_gpio_flags of_flags;
127 + gpio = of_get_gpio_flags(cnp, i, &of_flags);
129 + if (of_flags == OF_GPIO_ACTIVE_LOW)
130 + flags |= GPIOF_ACTIVE_LOW;
132 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
133 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
137 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
140 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
141 + gpio_export_with_name(gpio, dmc, name);
146 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
151 +static struct platform_driver gpio_export_driver = {
153 + .name = "gpio-export",
154 + .owner = THIS_MODULE,
155 + .of_match_table = of_match_ptr(gpio_export_ids),
159 +static int __init of_gpio_export_init(void)
161 + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
163 +device_initcall(of_gpio_export_init);
164 --- a/drivers/gpio/gpiolib.c
165 +++ b/drivers/gpio/gpiolib.c
166 @@ -96,7 +96,7 @@ static int gpiod_get_value(const struct
167 static void gpiod_set_value(struct gpio_desc *desc, int value);
168 static int gpiod_cansleep(const struct gpio_desc *desc);
169 static int gpiod_to_irq(const struct gpio_desc *desc);
170 -static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
171 +static int gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
172 static int gpiod_export_link(struct device *dev, const char *name,
173 struct gpio_desc *desc);
174 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
175 @@ -674,7 +674,7 @@ static ssize_t export_store(struct class
179 - status = gpiod_export(desc, true);
180 + status = gpiod_export(desc, true, NULL);
184 @@ -736,9 +736,10 @@ static struct class gpio_class = {
188 - * gpio_export - export a GPIO through sysfs
189 + * gpio_export_with_name - export a GPIO through sysfs
190 * @gpio: gpio to make available, already requested
191 * @direction_may_change: true if userspace may change gpio direction
193 * Context: arch_initcall or later
195 * When drivers want to make a GPIO accessible to userspace after they
196 @@ -750,7 +751,7 @@ static struct class gpio_class = {
198 * Returns zero on success, else an error.
200 -static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
201 +static int gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
205 @@ -783,6 +784,8 @@ static int gpiod_export(struct gpio_desc
211 if (!desc->chip->direction_input || !desc->chip->direction_output)
212 direction_may_change = false;
213 spin_unlock_irqrestore(&gpio_lock, flags);
214 @@ -829,11 +832,11 @@ fail_unlock:
218 -int gpio_export(unsigned gpio, bool direction_may_change)
219 +int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
221 - return gpiod_export(gpio_to_desc(gpio), direction_may_change);
222 + return gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
224 -EXPORT_SYMBOL_GPL(gpio_export);
225 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
227 static int match_export(struct device *dev, const void *data)
229 @@ -1092,7 +1095,7 @@ static inline void gpiochip_unexport(str
232 static inline int gpiod_export(struct gpio_desc *desc,
233 - bool direction_may_change)
234 + bool direction_may_change, const char *name)
238 @@ -1521,6 +1524,9 @@ int gpio_request_one(unsigned gpio, unsi
239 if (flags & GPIOF_OPEN_SOURCE)
240 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
242 + if (flags & GPIOF_ACTIVE_LOW)
243 + set_bit(FLAG_ACTIVE_LOW, &gpio_desc[gpio].flags);
245 if (flags & GPIOF_DIR_IN)
246 err = gpiod_direction_input(desc);
248 @@ -1531,7 +1537,7 @@ int gpio_request_one(unsigned gpio, unsi
251 if (flags & GPIOF_EXPORT) {
252 - err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
253 + err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE, NULL);
257 --- a/include/asm-generic/gpio.h
258 +++ b/include/asm-generic/gpio.h
259 @@ -202,7 +202,8 @@ extern void gpio_free_array(const struct
260 * A sysfs interface can be exported by individual drivers if they want,
261 * but more typically is configured entirely from userspace.
263 -extern int gpio_export(unsigned gpio, bool direction_may_change);
264 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
266 extern int gpio_export_link(struct device *dev, const char *name,
268 extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
269 @@ -284,7 +285,8 @@ struct device;
271 /* sysfs support is only available with gpiolib, where it's optional */
273 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
274 +static inline int gpio_export_with_name(unsigned gpio,
275 + bool direction_may_change, const char *name)
279 --- a/include/linux/gpio.h
280 +++ b/include/linux/gpio.h
282 #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT)
283 #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
285 +#define GPIOF_ACTIVE_LOW (1 << 6)
289 * struct gpio - a structure describing a GPIO with configuration
290 * @gpio: the GPIO number
291 @@ -169,7 +172,8 @@ static inline void gpio_set_value_cansle
295 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
296 +static inline int gpio_export_with_name(unsigned gpio,
297 + bool direction_may_change, const char *name)
299 /* GPIO can never have been requested or set as {in,out}put */
301 @@ -236,4 +240,24 @@ int devm_gpio_request_one(struct device
302 unsigned long flags, const char *label);
303 void devm_gpio_free(struct device *dev, unsigned int gpio);
306 + * gpio_export - export a GPIO through sysfs
307 + * @gpio: gpio to make available, already requested
308 + * @direction_may_change: true if userspace may change gpio direction
309 + * Context: arch_initcall or later
311 + * When drivers want to make a GPIO accessible to userspace after they
312 + * have requested it -- perhaps while debugging, or as part of their
313 + * public interface -- they may use this routine. If the GPIO can
314 + * change direction (some can't) and the caller allows it, userspace
315 + * will see "direction" sysfs attribute which may be used to change
316 + * the gpio's direction. A "value" attribute will always be provided.
318 + * Returns zero on success, else an error.
320 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
322 + return gpio_export_with_name(gpio, direction_may_change, NULL);
325 #endif /* __LINUX_GPIO_H */