linux/named-gpio-export: add support for OPEN_DRAIN and OPEN_SOURCE flag
[openwrt/staging/pepe2k.git] / target / linux / generic / hack-6.1 / 800-GPIO-add-named-gpio-exports.patch
1 From cc809a441d8f2924f785eb863dfa6aef47a25b0b Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Tue, 12 Aug 2014 20:49:27 +0200
4 Subject: [PATCH 30/36] GPIO: add named gpio exports
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 --- a/drivers/gpio/gpiolib-of.c
8 +++ b/drivers/gpio/gpiolib-of.c
9 @@ -19,6 +19,8 @@
10 #include <linux/pinctrl/pinctrl.h>
11 #include <linux/slab.h>
12 #include <linux/gpio/machine.h>
13 +#include <linux/init.h>
14 +#include <linux/platform_device.h>
15
16 #include "gpiolib.h"
17 #include "gpiolib-of.h"
18 @@ -1030,3 +1032,100 @@ void of_gpio_dev_init(struct gpio_chip *
19 else
20 gc->of_node = gdev->dev.of_node;
21 }
22 +
23 +#ifdef CONFIG_GPIO_SYSFS
24 +
25 +static struct of_device_id gpio_export_ids[] = {
26 + { .compatible = "gpio-export" },
27 + { /* sentinel */ }
28 +};
29 +
30 +static int of_gpio_export_probe(struct platform_device *pdev)
31 +{
32 + struct device_node *np = pdev->dev.of_node;
33 + struct device_node *cnp;
34 + u32 val;
35 + int nb = 0;
36 +
37 + for_each_child_of_node(np, cnp) {
38 + const char *name = NULL;
39 + int gpio;
40 + bool dmc;
41 + int max_gpio = 1;
42 + int i;
43 +
44 + of_property_read_string(cnp, "gpio-export,name", &name);
45 +
46 + if (!name)
47 + max_gpio = of_gpio_count(cnp);
48 +
49 + for (i = 0; i < max_gpio; i++) {
50 + struct gpio_desc *desc;
51 + unsigned flags = 0;
52 + enum of_gpio_flags of_flags;
53 +
54 + gpio = of_get_gpio_flags(cnp, i, &of_flags);
55 + if (!gpio_is_valid(gpio))
56 + return gpio;
57 +
58 + if (of_flags & OF_GPIO_ACTIVE_LOW)
59 + flags |= GPIOF_ACTIVE_LOW;
60 +
61 + if (!of_property_read_u32(cnp, "gpio-export,output", &val)) {
62 + if (of_flags & OF_GPIO_SINGLE_ENDED) {
63 + /*
64 + * As gpiod_direction_output_raw() is used, we
65 + * need to emulate open drain or open source here.
66 + */
67 + if (of_flags & OF_GPIO_OPEN_DRAIN) {
68 + flags |= GPIOF_OPEN_DRAIN;
69 + flags |= val ? GPIOF_IN : GPIOF_OUT_INIT_LOW;
70 + } else {
71 + flags |= GPIOF_OPEN_SOURCE;
72 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_IN;
73 + }
74 + } else {
75 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
76 + }
77 + } else {
78 + flags |= GPIOF_IN;
79 + }
80 +
81 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
82 + continue;
83 +
84 + /*
85 + * When emulating open-source or open-drain functionalities by not
86 + * actively driving the line (setting mode to input) we still need to
87 + * set the IS_OUT flag or otherwise we won't be able to set the line
88 + * value anymore.
89 + */
90 + if ((flags & GPIOF_IN) &&
91 + ((flags & GPIOF_OPEN_DRAIN) || (flags & GPIOF_OPEN_SOURCE))) {
92 + desc = gpio_to_desc(gpio);
93 + set_bit(FLAG_IS_OUT, &desc->flags);
94 + }
95 +
96 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
97 + gpio_export_with_name(gpio, dmc, name);
98 + nb++;
99 + }
100 + }
101 +
102 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
103 +
104 + return 0;
105 +}
106 +
107 +static struct platform_driver gpio_export_driver = {
108 + .driver = {
109 + .name = "gpio-export",
110 + .owner = THIS_MODULE,
111 + .of_match_table = of_match_ptr(gpio_export_ids),
112 + },
113 + .probe = of_gpio_export_probe,
114 +};
115 +
116 +module_platform_driver(gpio_export_driver);
117 +
118 +#endif
119 --- a/include/asm-generic/gpio.h
120 +++ b/include/asm-generic/gpio.h
121 @@ -125,6 +125,12 @@ static inline int gpio_export(unsigned g
122 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
123 }
124
125 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
126 +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
127 +{
128 + return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
129 +}
130 +
131 static inline int gpio_export_link(struct device *dev, const char *name,
132 unsigned gpio)
133 {
134 --- a/include/linux/gpio/consumer.h
135 +++ b/include/linux/gpio/consumer.h
136 @@ -715,6 +715,7 @@ static inline struct gpio_desc *acpi_get
137
138 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
139
140 +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
141 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
142 int gpiod_export_link(struct device *dev, const char *name,
143 struct gpio_desc *desc);
144 @@ -722,6 +723,13 @@ void gpiod_unexport(struct gpio_desc *de
145
146 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
147
148 +static inline int _gpiod_export(struct gpio_desc *desc,
149 + bool direction_may_change,
150 + const char *name)
151 +{
152 + return -ENOSYS;
153 +}
154 +
155 static inline int gpiod_export(struct gpio_desc *desc,
156 bool direction_may_change)
157 {
158 --- a/drivers/gpio/gpiolib-sysfs.c
159 +++ b/drivers/gpio/gpiolib-sysfs.c
160 @@ -544,7 +544,7 @@ static struct class gpio_class = {
161 *
162 * Returns zero on success, else an error.
163 */
164 -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
165 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
166 {
167 struct gpio_chip *chip;
168 struct gpio_device *gdev;
169 @@ -606,6 +606,8 @@ int gpiod_export(struct gpio_desc *desc,
170 offset = gpio_chip_hwgpio(desc);
171 if (chip->names && chip->names[offset])
172 ioname = chip->names[offset];
173 + if (name)
174 + ioname = name;
175
176 dev = device_create_with_groups(&gpio_class, &gdev->dev,
177 MKDEV(0, 0), data, gpio_groups,
178 @@ -627,6 +629,12 @@ err_unlock:
179 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
180 return status;
181 }
182 +EXPORT_SYMBOL_GPL(__gpiod_export);
183 +
184 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
185 +{
186 + return __gpiod_export(desc, direction_may_change, NULL);
187 +}
188 EXPORT_SYMBOL_GPL(gpiod_export);
189
190 static int match_export(struct device *dev, const void *desc)