ramips: update v3.10 patches
[openwrt/svn-archive/archive.git] / target / linux / ramips / patches-3.10 / 0200-owrt-GPIO-add-gpio_export_with_name.patch
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
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 --- 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 @@ -242,3 +244,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 @@ -96,7 +96,7 @@ static int gpiod_get_value(const struct
167 static void gpiod_set_value(struct gpio_desc *desc, int value);
168 static int gpiod_cansleep(const struct gpio_desc *desc);
169 static int gpiod_to_irq(const struct gpio_desc *desc);
170 -static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
171 +static int gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
172 static int gpiod_export_link(struct device *dev, const char *name,
173 struct gpio_desc *desc);
174 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
175 @@ -674,7 +674,7 @@ static ssize_t export_store(struct class
176 status = -ENODEV;
177 goto done;
178 }
179 - status = gpiod_export(desc, true);
180 + status = gpiod_export(desc, true, NULL);
181 if (status < 0)
182 gpiod_free(desc);
183 else
184 @@ -736,9 +736,10 @@ static struct class gpio_class = {
185
186
187 /**
188 - * gpio_export - export a GPIO through sysfs
189 + * gpio_export_with_name - export a GPIO through sysfs
190 * @gpio: gpio to make available, already requested
191 * @direction_may_change: true if userspace may change gpio direction
192 + * @name: gpio name
193 * Context: arch_initcall or later
194 *
195 * When drivers want to make a GPIO accessible to userspace after they
196 @@ -750,7 +751,7 @@ static struct class gpio_class = {
197 *
198 * Returns zero on success, else an error.
199 */
200 -static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
201 +static int gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
202 {
203 unsigned long flags;
204 int status;
205 @@ -783,6 +784,8 @@ static int gpiod_export(struct gpio_desc
206 goto fail_unlock;
207 }
208
209 + if (name)
210 + ioname = name;
211 if (!desc->chip->direction_input || !desc->chip->direction_output)
212 direction_may_change = false;
213 spin_unlock_irqrestore(&gpio_lock, flags);
214 @@ -829,11 +832,11 @@ fail_unlock:
215 return status;
216 }
217
218 -int gpio_export(unsigned gpio, bool direction_may_change)
219 +int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
220 {
221 - return gpiod_export(gpio_to_desc(gpio), direction_may_change);
222 + return gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
223 }
224 -EXPORT_SYMBOL_GPL(gpio_export);
225 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
226
227 static int match_export(struct device *dev, const void *data)
228 {
229 @@ -1092,7 +1095,7 @@ static inline void gpiochip_unexport(str
230 }
231
232 static inline int gpiod_export(struct gpio_desc *desc,
233 - bool direction_may_change)
234 + bool direction_may_change, const char *name)
235 {
236 return -ENOSYS;
237 }
238 @@ -1521,6 +1524,9 @@ int gpio_request_one(unsigned gpio, unsi
239 if (flags & GPIOF_OPEN_SOURCE)
240 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
241
242 + if (flags & GPIOF_ACTIVE_LOW)
243 + set_bit(FLAG_ACTIVE_LOW, &gpio_desc[gpio].flags);
244 +
245 if (flags & GPIOF_DIR_IN)
246 err = gpiod_direction_input(desc);
247 else
248 @@ -1531,7 +1537,7 @@ int gpio_request_one(unsigned gpio, unsi
249 goto free_gpio;
250
251 if (flags & GPIOF_EXPORT) {
252 - err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
253 + err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE, NULL);
254 if (err)
255 goto free_gpio;
256 }
257 --- a/include/asm-generic/gpio.h
258 +++ b/include/asm-generic/gpio.h
259 @@ -202,7 +202,8 @@ extern void gpio_free_array(const struct
260 * A sysfs interface can be exported by individual drivers if they want,
261 * but more typically is configured entirely from userspace.
262 */
263 -extern int gpio_export(unsigned gpio, bool direction_may_change);
264 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
265 + const char *name);
266 extern int gpio_export_link(struct device *dev, const char *name,
267 unsigned gpio);
268 extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
269 @@ -284,7 +285,8 @@ struct device;
270
271 /* sysfs support is only available with gpiolib, where it's optional */
272
273 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
274 +static inline int gpio_export_with_name(unsigned gpio,
275 + bool direction_may_change, const char *name)
276 {
277 return -ENOSYS;
278 }
279 --- a/include/linux/gpio.h
280 +++ b/include/linux/gpio.h
281 @@ -27,6 +27,9 @@
282 #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT)
283 #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
284
285 +#define GPIOF_ACTIVE_LOW (1 << 6)
286 +
287 +
288 /**
289 * struct gpio - a structure describing a GPIO with configuration
290 * @gpio: the GPIO number
291 @@ -169,7 +172,8 @@ static inline void gpio_set_value_cansle
292 WARN_ON(1);
293 }
294
295 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
296 +static inline int gpio_export_with_name(unsigned gpio,
297 + bool direction_may_change, const char *name)
298 {
299 /* GPIO can never have been requested or set as {in,out}put */
300 WARN_ON(1);
301 @@ -236,4 +240,24 @@ int devm_gpio_request_one(struct device
302 unsigned long flags, const char *label);
303 void devm_gpio_free(struct device *dev, unsigned int gpio);
304
305 +/**
306 + * gpio_export - export a GPIO through sysfs
307 + * @gpio: gpio to make available, already requested
308 + * @direction_may_change: true if userspace may change gpio direction
309 + * Context: arch_initcall or later
310 + *
311 + * When drivers want to make a GPIO accessible to userspace after they
312 + * have requested it -- perhaps while debugging, or as part of their
313 + * public interface -- they may use this routine. If the GPIO can
314 + * change direction (some can't) and the caller allows it, userspace
315 + * will see "direction" sysfs attribute which may be used to change
316 + * the gpio's direction. A "value" attribute will always be provided.
317 + *
318 + * Returns zero on success, else an error.
319 + */
320 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
321 +{
322 + return gpio_export_with_name(gpio, direction_may_change, NULL);
323 +}
324 +
325 #endif /* __LINUX_GPIO_H */