lantiq: add v3.10 patches
[openwrt.git] / target / linux / lantiq / patches-3.10 / 0025-GPIO-add-gpio_export_with_name.patch
1 From 4bf583c17735a759064bc6177718f05fd990a7ab 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 25/34] 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                       |   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(-)
16
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,
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 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
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 @@ -242,3 +244,69 @@ void of_gpiochip_remove(struct gpio_chip *chip)
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 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,
182                         status = -ENODEV;
183                 goto done;
184         }
185 -       status = gpiod_export(desc, true);
186 +       status = gpiod_export(desc, true, NULL);
187         if (status < 0)
188                 gpiod_free(desc);
189         else
190 @@ -736,9 +736,10 @@ static struct class gpio_class = {
191  
192  
193  /**
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
198 + * @name: gpio name
199   * Context: arch_initcall or later
200   *
201   * When drivers want to make a GPIO accessible to userspace after they
202 @@ -750,7 +751,7 @@ static struct class gpio_class = {
203   *
204   * Returns zero on success, else an error.
205   */
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)
208  {
209         unsigned long           flags;
210         int                     status;
211 @@ -783,6 +784,8 @@ static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
212                 goto fail_unlock;
213         }
214  
215 +       if (name)
216 +               ioname = name;
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:
221         return status;
222  }
223  
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)
226  {
227 -       return gpiod_export(gpio_to_desc(gpio), direction_may_change);
228 +       return gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
229  }
230 -EXPORT_SYMBOL_GPL(gpio_export);
231 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
232  
233  static int match_export(struct device *dev, const void *data)
234  {
235 @@ -1092,7 +1095,7 @@ static inline void gpiochip_unexport(struct gpio_chip *chip)
236  }
237  
238  static inline int gpiod_export(struct gpio_desc *desc,
239 -                              bool direction_may_change)
240 +                              bool direction_may_change, const char *name)
241  {
242         return -ENOSYS;
243  }
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);
247  
248 +       if (flags & GPIOF_ACTIVE_LOW)
249 +               set_bit(FLAG_ACTIVE_LOW, &gpio_desc[gpio].flags);
250 +
251         if (flags & GPIOF_DIR_IN)
252                 err = gpiod_direction_input(desc);
253         else
254 @@ -1531,7 +1537,7 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
255                 goto free_gpio;
256  
257         if (flags & GPIOF_EXPORT) {
258 -               err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
259 +               err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE, NULL);
260                 if (err)
261                         goto free_gpio;
262         }
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.
270   */
271 -extern int gpio_export(unsigned gpio, bool direction_may_change);
272 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
273 +                       const char *name);
274  extern int gpio_export_link(struct device *dev, const char *name,
275                         unsigned gpio);
276  extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
277 @@ -284,7 +285,8 @@ struct device;
278  
279  /* sysfs support is only available with gpiolib, where it's optional */
280  
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)
284  {
285         return -ENOSYS;
286  }
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
291 @@ -27,6 +27,9 @@
292  #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT)
293  #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
294  
295 +#define GPIOF_ACTIVE_LOW       (1 << 6)
296 +
297 +
298  /**
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)
302         WARN_ON(1);
303  }
304  
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)
308  {
309         /* GPIO can never have been requested or set as {in,out}put */
310         WARN_ON(1);
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);
314  
315 +/**
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
320 + *
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.
327 + *
328 + * Returns zero on success, else an error.
329 + */
330 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
331 +{
332 +       return gpio_export_with_name(gpio, direction_may_change, NULL);
333 +}
334 +
335  #endif /* __LINUX_GPIO_H */
336 -- 
337 1.7.10.4
338