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