ramips: Support setting active_low in devicetree gpio-export
[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.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
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.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
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 @@ -288,3 +290,69 @@
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 +                       unsigned flags = 0;
129 +                       enum of_gpio_flags of_flags;
130 +
131 +                       gpio = of_get_gpio_flags(cnp, i, &of_flags);
132 +
133 +                       if (of_flags == OF_GPIO_ACTIVE_LOW)
134 +                               flags |= GPIOF_ACTIVE_LOW;
135 +
136 +                       if (!of_property_read_u32(cnp, "gpio-export,output", &val))
137 +                               flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
138 +                       else
139 +                               flags |= GPIOF_IN;
140 +
141 +                       if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
142 +                               continue;
143 +
144 +                       dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
145 +                       gpio_export_with_name(gpio, dmc, name);
146 +                       nb++;
147 +               }
148 +       }
149 +
150 +       dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
151 +
152 +       return 0;
153 +}
154 +
155 +static struct platform_driver gpio_export_driver = {
156 +       .driver         = {
157 +               .name           = "gpio-export",
158 +               .owner  = THIS_MODULE,
159 +               .of_match_table = of_match_ptr(gpio_export_ids),
160 +       },
161 +};
162 +
163 +static int __init of_gpio_export_init(void)
164 +{
165 +       return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
166 +}
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
172 @@ -714,9 +714,10 @@
173  
174  
175  /**
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
180 + * @name: gpio name
181   * Context: arch_initcall or later
182   *
183   * When drivers want to make a GPIO accessible to userspace after they
184 @@ -728,7 +729,7 @@
185   *
186   * Returns zero on success, else an error.
187   */
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)
190  {
191         unsigned long           flags;
192         struct gpio_desc        *desc;
193 @@ -762,6 +763,8 @@
194                 goto fail_unlock;
195         }
196  
197 +       if (name)
198 +               ioname = name;
199         if (!desc->chip->direction_input || !desc->chip->direction_output)
200                 direction_may_change = false;
201         spin_unlock_irqrestore(&gpio_lock, flags);
202 @@ -804,7 +807,7 @@
203         pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
204         return status;
205  }
206 -EXPORT_SYMBOL_GPL(gpio_export);
207 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
208  
209  static int match_export(struct device *dev, void *data)
210  {
211 @@ -1418,6 +1421,9 @@
212         if (flags & GPIOF_OPEN_SOURCE)
213                 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
214  
215 +       if (flags & GPIOF_ACTIVE_LOW)
216 +               set_bit(FLAG_ACTIVE_LOW, &gpio_desc[gpio].flags);
217 +
218         if (flags & GPIOF_DIR_IN)
219                 err = gpio_direction_input(gpio);
220         else
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
225 @@ -204,7 +204,8 @@
226   * A sysfs interface can be exported by individual drivers if they want,
227   * but more typically is configured entirely from userspace.
228   */
229 -extern int gpio_export(unsigned gpio, bool direction_may_change);
230 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
231 +                       const char *name);
232  extern int gpio_export_link(struct device *dev, const char *name,
233                         unsigned gpio);
234  extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
235 @@ -249,7 +250,8 @@
236  
237  /* sysfs support is only available with gpiolib, where it's optional */
238  
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)
242  {
243         return -ENOSYS;
244  }
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
249 @@ -27,6 +27,9 @@
250  #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT)
251  #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
252  
253 +#define GPIOF_ACTIVE_LOW       (1 << 6)
254 +
255 +
256  /**
257   * struct gpio - a structure describing a GPIO with configuration
258   * @gpio:      the GPIO number
259 @@ -189,7 +192,8 @@
260         WARN_ON(1);
261  }
262  
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)
266  {
267         /* GPIO can never have been requested or set as {in,out}put */
268         WARN_ON(1);
269 @@ -248,4 +252,24 @@
270  
271  #endif /* ! CONFIG_GENERIC_GPIO */
272  
273 +/**
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
278 + *
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.
285 + *
286 + * Returns zero on success, else an error.
287 + */
288 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
289 +{
290 +       return gpio_export_with_name(gpio, direction_may_change, NULL);
291 +}
292 +
293  #endif /* __LINUX_GPIO_H */