add patches for v3.8
[oweals/openwrt.git] / target / linux / ramips / patches-3.8 / 0209-owrt-GPIO-add-gpio_export_with_name.patch
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
5
6 http://lists.infradead.org/pipermail/linux-arm-kernel/2012-November/133856.html
7
8 Signed-off-by: John Crispin <blogic@openwrt.org>
9 ---
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(-)
16
17 Index: linux-3.8.3/Documentation/devicetree/bindings/gpio/gpio.txt
18 ===================================================================
19 --- linux-3.8.3.orig/Documentation/devicetree/bindings/gpio/gpio.txt    2013-03-14 19:27:14.000000000 +0100
20 +++ linux-3.8.3/Documentation/devicetree/bindings/gpio/gpio.txt 2013-04-01 11:11:42.151167467 +0200
21 @@ -112,3 +112,63 @@
22  
23  The pinctrl node must have "#gpio-range-cells" property to show number of
24  arguments to pass with phandle from gpio controllers node.
25 +
26 +3) gpio-export
27 +--------------
28 +
29 +gpio-export will allow you to automatically export gpio
30 +
31 +required properties:
32 +- compatible: Should be "gpio-export"
33 +
34 +in each child node will reprensent a gpio or if no name is specified
35 +a list of gpio to export
36 +
37 +required properties:
38 +- gpios: gpio to export
39 +
40 +optional properties:
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
45 +
46 +Example:
47 +
48 +
49 +gpio_export {
50 +       compatible = "gpio-export";
51 +       #size-cells = <0>;
52 +
53 +       in {
54 +               gpio-export,name = "in";
55 +               gpios = <&pioC 20 0>;
56 +       };
57 +
58 +       out {
59 +               gpio-export,name = "out";
60 +               gpio-export,output = <1>;
61 +               gpio-export,direction_may_change;
62 +               gpios = <&pioC 21 0>;
63 +       };
64 +
65 +       in_out {
66 +               gpio-export,name = "in_out";
67 +               gpio-export,direction_may_change;
68 +               gpios = <&pioC 21 0>;
69 +       };
70 +
71 +       gpios_in {
72 +               gpios = <&pioB 0 0
73 +                        &pioB 3 0
74 +                        &pioC 4 0>;
75 +               gpio-export,direction_may_change;
76 +       };
77 +
78 +       gpios_out {
79 +               gpios = <&pioB 1 0
80 +                        &pioB 2 0
81 +                        &pioC 3 0>;
82 +               gpio-export,output = <1>;
83 +       };
84 +};
85 Index: linux-3.8.3/drivers/gpio/gpiolib-of.c
86 ===================================================================
87 --- linux-3.8.3.orig/drivers/gpio/gpiolib-of.c  2013-03-14 19:27:14.000000000 +0100
88 +++ linux-3.8.3/drivers/gpio/gpiolib-of.c       2013-04-01 11:11:42.151167467 +0200
89 @@ -21,6 +21,8 @@
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>
95  
96  /* Private data structure for of_gpiochip_find_and_xlate */
97  struct gg_data {
98 @@ -289,3 +291,62 @@
99         if (chip->of_node)
100                 of_node_put(chip->of_node);
101  }
102 +
103 +static struct of_device_id gpio_export_ids[] = {
104 +       { .compatible = "gpio-export" },
105 +       { /* sentinel */ }
106 +};
107 +
108 +static int __init of_gpio_export_probe(struct platform_device *pdev)
109 +{
110 +       struct device_node *np = pdev->dev.of_node;
111 +       struct device_node *cnp;
112 +       u32 val;
113 +       int nb = 0;
114 +
115 +       for_each_child_of_node(np, cnp) {
116 +               const char *name = NULL;
117 +               int gpio;
118 +               bool dmc;
119 +               int max_gpio = 1;
120 +               int i;
121 +
122 +               of_property_read_string(cnp, "gpio-export,name", &name);
123 +
124 +               if (!name)
125 +                       max_gpio = of_gpio_count(cnp);
126 +
127 +               for (i = 0; i < max_gpio; i++) {
128 +                       gpio = of_get_gpio(cnp, i);
129 +                       if (devm_gpio_request(&pdev->dev, gpio, name ? name : of_node_full_name(np)))
130 +                               continue;
131 +
132 +                       if (!of_property_read_u32(cnp, "gpio-export,output", &val))
133 +                               gpio_direction_output(gpio, val);
134 +                       else
135 +                               gpio_direction_input(gpio);
136 +
137 +                       dmc = of_property_read_bool(np, "gpio-export,direction_may_change");
138 +                       gpio_export_with_name(gpio, dmc, name);
139 +                       nb++;
140 +               }
141 +       }
142 +
143 +       dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
144 +
145 +       return 0;
146 +}
147 +
148 +static struct platform_driver gpio_export_driver = {
149 +       .driver         = {
150 +               .name           = "gpio-export",
151 +               .owner  = THIS_MODULE,
152 +               .of_match_table = of_match_ptr(gpio_export_ids),
153 +       },
154 +};
155 +
156 +static int __init of_gpio_export_init(void)
157 +{
158 +       return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
159 +}
160 +device_initcall(of_gpio_export_init);
161 Index: linux-3.8.3/drivers/gpio/gpiolib.c
162 ===================================================================
163 --- linux-3.8.3.orig/drivers/gpio/gpiolib.c     2013-03-14 19:27:14.000000000 +0100
164 +++ linux-3.8.3/drivers/gpio/gpiolib.c  2013-04-01 11:12:29.263168590 +0200
165 @@ -714,9 +714,10 @@
166  
167  
168  /**
169 - * gpio_export - export a GPIO through sysfs
170 + * gpio_export_with_name - export a GPIO through sysfs
171   * @gpio: gpio to make available, already requested
172   * @direction_may_change: true if userspace may change gpio direction
173 + * @name: gpio name
174   * Context: arch_initcall or later
175   *
176   * When drivers want to make a GPIO accessible to userspace after they
177 @@ -728,7 +729,7 @@
178   *
179   * Returns zero on success, else an error.
180   */
181 -int gpio_export(unsigned gpio, bool direction_may_change)
182 +int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
183  {
184         unsigned long           flags;
185         struct gpio_desc        *desc;
186 @@ -762,6 +763,8 @@
187                 goto fail_unlock;
188         }
189  
190 +       if (name)
191 +               ioname = name;
192         if (!desc->chip->direction_input || !desc->chip->direction_output)
193                 direction_may_change = false;
194         spin_unlock_irqrestore(&gpio_lock, flags);
195 @@ -804,7 +807,7 @@
196         pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
197         return status;
198  }
199 -EXPORT_SYMBOL_GPL(gpio_export);
200 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
201  
202  static int match_export(struct device *dev, void *data)
203  {
204 Index: linux-3.8.3/include/asm-generic/gpio.h
205 ===================================================================
206 --- linux-3.8.3.orig/include/asm-generic/gpio.h 2013-03-14 19:27:14.000000000 +0100
207 +++ linux-3.8.3/include/asm-generic/gpio.h      2013-04-01 11:11:42.155167467 +0200
208 @@ -204,7 +204,8 @@
209   * A sysfs interface can be exported by individual drivers if they want,
210   * but more typically is configured entirely from userspace.
211   */
212 -extern int gpio_export(unsigned gpio, bool direction_may_change);
213 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
214 +                       const char *name);
215  extern int gpio_export_link(struct device *dev, const char *name,
216                         unsigned gpio);
217  extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
218 @@ -249,7 +250,8 @@
219  
220  /* sysfs support is only available with gpiolib, where it's optional */
221  
222 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
223 +static inline int gpio_export_with_name(unsigned gpio,
224 +       bool direction_may_change, const char *name)
225  {
226         return -ENOSYS;
227  }
228 Index: linux-3.8.3/include/linux/gpio.h
229 ===================================================================
230 --- linux-3.8.3.orig/include/linux/gpio.h       2013-03-14 19:27:14.000000000 +0100
231 +++ linux-3.8.3/include/linux/gpio.h    2013-04-01 11:11:42.159167467 +0200
232 @@ -189,7 +189,8 @@
233         WARN_ON(1);
234  }
235  
236 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
237 +static inline int gpio_export_with_name(unsigned gpio,
238 +       bool direction_may_change, const char *name)
239  {
240         /* GPIO can never have been requested or set as {in,out}put */
241         WARN_ON(1);
242 @@ -248,4 +249,24 @@
243  
244  #endif /* ! CONFIG_GENERIC_GPIO */
245  
246 +/**
247 + * gpio_export - export a GPIO through sysfs
248 + * @gpio: gpio to make available, already requested
249 + * @direction_may_change: true if userspace may change gpio direction
250 + * Context: arch_initcall or later
251 + *
252 + * When drivers want to make a GPIO accessible to userspace after they
253 + * have requested it -- perhaps while debugging, or as part of their
254 + * public interface -- they may use this routine.  If the GPIO can
255 + * change direction (some can't) and the caller allows it, userspace
256 + * will see "direction" sysfs attribute which may be used to change
257 + * the gpio's direction.  A "value" attribute will always be provided.
258 + *
259 + * Returns zero on success, else an error.
260 + */
261 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
262 +{
263 +       return gpio_export_with_name(gpio, direction_may_change, NULL);
264 +}
265 +
266  #endif /* __LINUX_GPIO_H */