kernel: update linux 3.8 to 3.8.6
[openwrt/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 @@ -289,3 +291,62 @@ 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 + gpio = of_get_gpio(cnp, i);
125 + if (devm_gpio_request(&pdev->dev, gpio, name ? name : of_node_full_name(np)))
126 + continue;
127 +
128 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
129 + gpio_direction_output(gpio, val);
130 + else
131 + gpio_direction_input(gpio);
132 +
133 + dmc = of_property_read_bool(np, "gpio-export,direction_may_change");
134 + gpio_export_with_name(gpio, dmc, name);
135 + nb++;
136 + }
137 + }
138 +
139 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
140 +
141 + return 0;
142 +}
143 +
144 +static struct platform_driver gpio_export_driver = {
145 + .driver = {
146 + .name = "gpio-export",
147 + .owner = THIS_MODULE,
148 + .of_match_table = of_match_ptr(gpio_export_ids),
149 + },
150 +};
151 +
152 +static int __init of_gpio_export_init(void)
153 +{
154 + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
155 +}
156 +device_initcall(of_gpio_export_init);
157 --- a/drivers/gpio/gpiolib.c
158 +++ b/drivers/gpio/gpiolib.c
159 @@ -714,9 +714,10 @@ static struct class gpio_class = {
160
161
162 /**
163 - * gpio_export - export a GPIO through sysfs
164 + * gpio_export_with_name - export a GPIO through sysfs
165 * @gpio: gpio to make available, already requested
166 * @direction_may_change: true if userspace may change gpio direction
167 + * @name: gpio name
168 * Context: arch_initcall or later
169 *
170 * When drivers want to make a GPIO accessible to userspace after they
171 @@ -728,7 +729,7 @@ static struct class gpio_class = {
172 *
173 * Returns zero on success, else an error.
174 */
175 -int gpio_export(unsigned gpio, bool direction_may_change)
176 +int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
177 {
178 unsigned long flags;
179 struct gpio_desc *desc;
180 @@ -762,6 +763,8 @@ int gpio_export(unsigned gpio, bool dire
181 goto fail_unlock;
182 }
183
184 + if (name)
185 + ioname = name;
186 if (!desc->chip->direction_input || !desc->chip->direction_output)
187 direction_may_change = false;
188 spin_unlock_irqrestore(&gpio_lock, flags);
189 @@ -804,7 +807,7 @@ fail_unlock:
190 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
191 return status;
192 }
193 -EXPORT_SYMBOL_GPL(gpio_export);
194 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
195
196 static int match_export(struct device *dev, void *data)
197 {
198 --- a/include/asm-generic/gpio.h
199 +++ b/include/asm-generic/gpio.h
200 @@ -204,7 +204,8 @@ void devm_gpio_free(struct device *dev,
201 * A sysfs interface can be exported by individual drivers if they want,
202 * but more typically is configured entirely from userspace.
203 */
204 -extern int gpio_export(unsigned gpio, bool direction_may_change);
205 +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
206 + const char *name);
207 extern int gpio_export_link(struct device *dev, const char *name,
208 unsigned gpio);
209 extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
210 @@ -249,7 +250,8 @@ struct device;
211
212 /* sysfs support is only available with gpiolib, where it's optional */
213
214 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
215 +static inline int gpio_export_with_name(unsigned gpio,
216 + bool direction_may_change, const char *name)
217 {
218 return -ENOSYS;
219 }
220 --- a/include/linux/gpio.h
221 +++ b/include/linux/gpio.h
222 @@ -189,7 +189,8 @@ static inline void gpio_set_value_cansle
223 WARN_ON(1);
224 }
225
226 -static inline int gpio_export(unsigned gpio, bool direction_may_change)
227 +static inline int gpio_export_with_name(unsigned gpio,
228 + bool direction_may_change, const char *name)
229 {
230 /* GPIO can never have been requested or set as {in,out}put */
231 WARN_ON(1);
232 @@ -248,4 +249,24 @@ gpiochip_remove_pin_ranges(struct gpio_c
233
234 #endif /* ! CONFIG_GENERIC_GPIO */
235
236 +/**
237 + * gpio_export - export a GPIO through sysfs
238 + * @gpio: gpio to make available, already requested
239 + * @direction_may_change: true if userspace may change gpio direction
240 + * Context: arch_initcall or later
241 + *
242 + * When drivers want to make a GPIO accessible to userspace after they
243 + * have requested it -- perhaps while debugging, or as part of their
244 + * public interface -- they may use this routine. If the GPIO can
245 + * change direction (some can't) and the caller allows it, userspace
246 + * will see "direction" sysfs attribute which may be used to change
247 + * the gpio's direction. A "value" attribute will always be provided.
248 + *
249 + * Returns zero on success, else an error.
250 + */
251 +static inline int gpio_export(unsigned gpio,bool direction_may_change)
252 +{
253 + return gpio_export_with_name(gpio, direction_may_change, NULL);
254 +}
255 +
256 #endif /* __LINUX_GPIO_H */