generic: 6.6: manually refresh hack patches
[openwrt/staging/981213.git] / target / linux / generic / hack-6.6 / 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 @@ -21,6 +21,8 @@
10
11 #include <linux/gpio/consumer.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 @@ -1111,3 +1113,74 @@ void of_gpiochip_remove(struct gpio_chip
19 {
20 of_node_put(dev_of_node(&chip->gpiodev->dev));
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_named_count(cnp, "gpios");
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 + desc = of_get_named_gpiod_flags(cnp, "gpios", i, &of_flags);
55 + if (IS_ERR(desc))
56 + return PTR_ERR(desc);
57 + gpio = desc_to_gpio(desc);
58 +
59 + if (of_flags & OF_GPIO_ACTIVE_LOW)
60 + flags |= GPIOF_ACTIVE_LOW;
61 +
62 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
63 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
64 + else
65 + flags |= GPIOF_IN;
66 +
67 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
68 + continue;
69 +
70 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
71 + gpio_export_with_name(gpio_to_desc(gpio), dmc, name);
72 + nb++;
73 + }
74 + }
75 +
76 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
77 +
78 + return 0;
79 +}
80 +
81 +static struct platform_driver gpio_export_driver = {
82 + .driver = {
83 + .name = "gpio-export",
84 + .owner = THIS_MODULE,
85 + .of_match_table = of_match_ptr(gpio_export_ids),
86 + },
87 + .probe = of_gpio_export_probe,
88 +};
89 +
90 +module_platform_driver(gpio_export_driver);
91 +
92 +#endif
93 --- a/include/linux/gpio/consumer.h
94 +++ b/include/linux/gpio/consumer.h
95 @@ -644,7 +644,10 @@ static inline struct gpio_desc *acpi_get
96
97 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
98
99 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
100 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
101 +int gpio_export_with_name(struct gpio_desc *desc, bool direction_may_change,
102 + const char *name);
103 int gpiod_export_link(struct device *dev, const char *name,
104 struct gpio_desc *desc);
105 void gpiod_unexport(struct gpio_desc *desc);
106 @@ -653,12 +656,26 @@ void gpiod_unexport(struct gpio_desc *de
107
108 #include <asm/errno.h>
109
110 +static inline int __gpiod_export(struct gpio_desc *desc,
111 + bool direction_may_change,
112 + const char *name)
113 +{
114 + return -ENOSYS;
115 +}
116 +
117 static inline int gpiod_export(struct gpio_desc *desc,
118 bool direction_may_change)
119 {
120 return -ENOSYS;
121 }
122
123 +static inline int gpio_export_with_name(struct gpio_desc *desc,
124 + bool direction_may_change,
125 + const char *name)
126 +{
127 + return -ENOSYS;
128 +}
129 +
130 static inline int gpiod_export_link(struct device *dev, const char *name,
131 struct gpio_desc *desc)
132 {
133 --- a/drivers/gpio/gpiolib-sysfs.c
134 +++ b/drivers/gpio/gpiolib-sysfs.c
135 @@ -557,7 +557,7 @@ static struct class gpio_class = {
136 *
137 * Returns zero on success, else an error.
138 */
139 -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
140 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
141 {
142 struct gpio_chip *chip;
143 struct gpio_device *gdev;
144 @@ -619,6 +619,8 @@ int gpiod_export(struct gpio_desc *desc,
145 offset = gpio_chip_hwgpio(desc);
146 if (chip->names && chip->names[offset])
147 ioname = chip->names[offset];
148 + if (name)
149 + ioname = name;
150
151 dev = device_create_with_groups(&gpio_class, &gdev->dev,
152 MKDEV(0, 0), data, gpio_groups,
153 @@ -640,8 +642,21 @@ int gpiod_export(struct gpio_desc *desc,
154 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
155 return status;
156 }
157 +EXPORT_SYMBOL_GPL(__gpiod_export);
158 +
159 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
160 +{
161 + return __gpiod_export(desc, direction_may_change, NULL);
162 +}
163 EXPORT_SYMBOL_GPL(gpiod_export);
164
165 +int gpio_export_with_name(struct gpio_desc *desc, bool direction_may_change,
166 + const char *name)
167 +{
168 + return __gpiod_export(desc, direction_may_change, name);
169 +}
170 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
171 +
172 static int match_export(struct device *dev, const void *desc)
173 {
174 struct gpiod_data *data = dev_get_drvdata(dev);