1 From 8f3ed1fffa35d18c2b20ebb866c71a22cc0589ff 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 29/33] 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 diff --git a/Documentation/devicetree/bindings/gpio/gpio.txt b/Documentation/devicetree/bindings/gpio/gpio.txt
18 index d933af3..c264748 100644
19 --- a/Documentation/devicetree/bindings/gpio/gpio.txt
20 +++ b/Documentation/devicetree/bindings/gpio/gpio.txt
21 @@ -112,3 +112,63 @@ where,
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 diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
86 index 665f953..15ec5e5 100644
87 --- a/drivers/gpio/gpiolib-of.c
88 +++ b/drivers/gpio/gpiolib-of.c
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 */
98 @@ -242,3 +244,69 @@ void of_gpiochip_remove(struct gpio_chip *chip)
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 diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
169 index c2534d6..8697c82 100644
170 --- a/drivers/gpio/gpiolib.c
171 +++ b/drivers/gpio/gpiolib.c
172 @@ -96,7 +96,7 @@ static int gpiod_get_value(const struct gpio_desc *desc);
173 static void gpiod_set_value(struct gpio_desc *desc, int value);
174 static int gpiod_cansleep(const struct gpio_desc *desc);
175 static int gpiod_to_irq(const struct gpio_desc *desc);
176 -static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
177 +static int gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
178 static int gpiod_export_link(struct device *dev, const char *name,
179 struct gpio_desc *desc);
180 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
181 @@ -674,7 +674,7 @@ static ssize_t export_store(struct class *class,
185 - status = gpiod_export(desc, true);
186 + status = gpiod_export(desc, true, NULL);
190 @@ -736,9 +736,10 @@ static struct class gpio_class = {
194 - * gpio_export - export a GPIO through sysfs
195 + * gpio_export_with_name - export a GPIO through sysfs
196 * @gpio: gpio to make available, already requested
197 * @direction_may_change: true if userspace may change gpio direction
199 * Context: arch_initcall or later
201 * When drivers want to make a GPIO accessible to userspace after they
202 @@ -750,7 +751,7 @@ static struct class gpio_class = {
204 * Returns zero on success, else an error.
206 -static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
207 +static int gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
211 @@ -783,6 +784,8 @@ static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
217 if (!desc->chip->direction_input || !desc->chip->direction_output)
218 direction_may_change = false;
219 spin_unlock_irqrestore(&gpio_lock, flags);
220 @@ -829,11 +832,11 @@ fail_unlock:
224 -int gpio_export(unsigned gpio, bool direction_may_change)
225 +int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
227 - return gpiod_export(gpio_to_desc(gpio), direction_may_change);
228 + return gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
230 -EXPORT_SYMBOL_GPL(gpio_export);
231 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
233 static int match_export(struct device *dev, const void *data)
235 @@ -1092,7 +1095,7 @@ static inline void gpiochip_unexport(struct gpio_chip *chip)
238 static inline int gpiod_export(struct gpio_desc *desc,
239 - bool direction_may_change)
240 + bool direction_may_change, const char *name)
244 @@ -1521,6 +1524,9 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
245 if (flags & GPIOF_OPEN_SOURCE)
246 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
248 + if (flags & GPIOF_ACTIVE_LOW)
249 + set_bit(FLAG_ACTIVE_LOW, &gpio_desc[gpio].flags);
251 if (flags & GPIOF_DIR_IN)
252 err = gpiod_direction_input(desc);
254 @@ -1531,7 +1537,7 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
257 if (flags & GPIOF_EXPORT) {
258 - err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
259 + err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE, NULL);
263 diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h
264 index bde6469..3290572 100644
265 --- a/include/asm-generic/gpio.h
266 +++ b/include/asm-generic/gpio.h
267 @@ -202,7 +202,8 @@ extern void gpio_free_array(const struct gpio *array, size_t num);
268 * A sysfs interface can be exported by individual drivers if they want,
269 * but more typically is configured entirely from userspace.
271 -extern int gpio_export(unsigned gpio, bool direction_may_change);
272 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
274 extern int gpio_export_link(struct device *dev, const char *name,
276 extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
277 @@ -284,7 +285,8 @@ struct device;
279 /* sysfs support is only available with gpiolib, where it's optional */
281 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
282 +static inline int gpio_export_with_name(unsigned gpio,
283 + bool direction_may_change, const char *name)
287 diff --git a/include/linux/gpio.h b/include/linux/gpio.h
288 index 552e3f4..07bbbcc7 100644
289 --- a/include/linux/gpio.h
290 +++ b/include/linux/gpio.h
292 #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT)
293 #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
295 +#define GPIOF_ACTIVE_LOW (1 << 6)
299 * struct gpio - a structure describing a GPIO with configuration
300 * @gpio: the GPIO number
301 @@ -169,7 +172,8 @@ static inline void gpio_set_value_cansleep(unsigned gpio, int value)
305 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
306 +static inline int gpio_export_with_name(unsigned gpio,
307 + bool direction_may_change, const char *name)
309 /* GPIO can never have been requested or set as {in,out}put */
311 @@ -236,4 +240,24 @@ int devm_gpio_request_one(struct device *dev, unsigned gpio,
312 unsigned long flags, const char *label);
313 void devm_gpio_free(struct device *dev, unsigned int gpio);
316 + * gpio_export - export a GPIO through sysfs
317 + * @gpio: gpio to make available, already requested
318 + * @direction_may_change: true if userspace may change gpio direction
319 + * Context: arch_initcall or later
321 + * When drivers want to make a GPIO accessible to userspace after they
322 + * have requested it -- perhaps while debugging, or as part of their
323 + * public interface -- they may use this routine. If the GPIO can
324 + * change direction (some can't) and the caller allows it, userspace
325 + * will see "direction" sysfs attribute which may be used to change
326 + * the gpio's direction. A "value" attribute will always be provided.
328 + * Returns zero on success, else an error.
330 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
332 + return gpio_export_with_name(gpio, direction_may_change, NULL);
335 #endif /* __LINUX_GPIO_H */