a1d5d9d9ff3a89469d32f9668e4e1955a710021a
[openwrt/openwrt.git] / target / linux / ramips / patches-4.14 / 0024-GPIO-add-named-gpio-exports.patch
1 From 4267880319bc1a2270d352e0ded6d6386242a7ef 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 24/53] GPIO: add named gpio exports
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8 drivers/gpio/gpiolib-of.c | 68 +++++++++++++++++++++++++++++++++++++++++
9 drivers/gpio/gpiolib-sysfs.c | 10 +++++-
10 include/asm-generic/gpio.h | 6 ++++
11 include/linux/gpio/consumer.h | 8 +++++
12 4 files changed, 91 insertions(+), 1 deletion(-)
13
14 --- a/drivers/gpio/gpiolib-of.c
15 +++ b/drivers/gpio/gpiolib-of.c
16 @@ -23,6 +23,8 @@
17 #include <linux/pinctrl/pinctrl.h>
18 #include <linux/slab.h>
19 #include <linux/gpio/machine.h>
20 +#include <linux/init.h>
21 +#include <linux/platform_device.h>
22
23 #include "gpiolib.h"
24
25 @@ -507,3 +509,68 @@ void of_gpiochip_remove(struct gpio_chip
26 gpiochip_remove_pin_ranges(chip);
27 of_node_put(chip->of_node);
28 }
29 +
30 +static struct of_device_id gpio_export_ids[] = {
31 + { .compatible = "gpio-export" },
32 + { /* sentinel */ }
33 +};
34 +
35 +static int of_gpio_export_probe(struct platform_device *pdev)
36 +{
37 + struct device_node *np = pdev->dev.of_node;
38 + struct device_node *cnp;
39 + u32 val;
40 + int nb = 0;
41 +
42 + for_each_child_of_node(np, cnp) {
43 + const char *name = NULL;
44 + int gpio;
45 + bool dmc;
46 + int max_gpio = 1;
47 + int i;
48 +
49 + of_property_read_string(cnp, "gpio-export,name", &name);
50 +
51 + if (!name)
52 + max_gpio = of_gpio_count(cnp);
53 +
54 + for (i = 0; i < max_gpio; i++) {
55 + unsigned flags = 0;
56 + enum of_gpio_flags of_flags;
57 +
58 + gpio = of_get_gpio_flags(cnp, i, &of_flags);
59 + if (!gpio_is_valid(gpio))
60 + return gpio;
61 +
62 + if (of_flags == OF_GPIO_ACTIVE_LOW)
63 + flags |= GPIOF_ACTIVE_LOW;
64 +
65 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
66 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
67 + else
68 + flags |= GPIOF_IN;
69 +
70 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
71 + continue;
72 +
73 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
74 + gpio_export_with_name(gpio, dmc, name);
75 + nb++;
76 + }
77 + }
78 +
79 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
80 +
81 + return 0;
82 +}
83 +
84 +static struct platform_driver gpio_export_driver = {
85 + .driver = {
86 + .name = "gpio-export",
87 + .owner = THIS_MODULE,
88 + .of_match_table = of_match_ptr(gpio_export_ids),
89 + },
90 + .probe = of_gpio_export_probe,
91 +};
92 +
93 +module_platform_driver(gpio_export_driver);
94 --- a/drivers/gpio/gpiolib-sysfs.c
95 +++ b/drivers/gpio/gpiolib-sysfs.c
96 @@ -553,7 +553,7 @@ static struct class gpio_class = {
97 *
98 * Returns zero on success, else an error.
99 */
100 -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
101 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
102 {
103 struct gpio_chip *chip;
104 struct gpio_device *gdev;
105 @@ -615,6 +615,8 @@ int gpiod_export(struct gpio_desc *desc,
106 offset = gpio_chip_hwgpio(desc);
107 if (chip->names && chip->names[offset])
108 ioname = chip->names[offset];
109 + if (name)
110 + ioname = name;
111
112 dev = device_create_with_groups(&gpio_class, &gdev->dev,
113 MKDEV(0, 0), data, gpio_groups,
114 @@ -636,6 +638,12 @@ err_unlock:
115 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
116 return status;
117 }
118 +EXPORT_SYMBOL_GPL(__gpiod_export);
119 +
120 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
121 +{
122 + return __gpiod_export(desc, direction_may_change, NULL);
123 +}
124 EXPORT_SYMBOL_GPL(gpiod_export);
125
126 static int match_export(struct device *dev, const void *desc)
127 --- a/include/asm-generic/gpio.h
128 +++ b/include/asm-generic/gpio.h
129 @@ -127,6 +127,12 @@ static inline int gpio_export(unsigned g
130 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
131 }
132
133 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
134 +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
135 +{
136 + return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
137 +}
138 +
139 static inline int gpio_export_link(struct device *dev, const char *name,
140 unsigned gpio)
141 {
142 --- a/include/linux/gpio/consumer.h
143 +++ b/include/linux/gpio/consumer.h
144 @@ -451,6 +451,7 @@ struct gpio_desc *devm_fwnode_get_gpiod_
145
146 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
147
148 +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
149 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
150 int gpiod_export_link(struct device *dev, const char *name,
151 struct gpio_desc *desc);
152 @@ -458,6 +459,13 @@ void gpiod_unexport(struct gpio_desc *de
153
154 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
155
156 +static inline int _gpiod_export(struct gpio_desc *desc,
157 + bool direction_may_change,
158 + const char *name)
159 +{
160 + return -ENOSYS;
161 +}
162 +
163 static inline int gpiod_export(struct gpio_desc *desc,
164 bool direction_may_change)
165 {