ath25: switch default kernel to 5.15
[openwrt/staging/ldir.git] / target / linux / generic / hack-5.10 / 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 @@ -1046,3 +1048,72 @@ void of_gpiochip_remove(struct gpio_chip
19 {
20 of_node_put(chip->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 + unsigned flags = 0;
51 + enum of_gpio_flags of_flags;
52 +
53 + gpio = of_get_gpio_flags(cnp, i, &of_flags);
54 + if (!gpio_is_valid(gpio))
55 + return gpio;
56 +
57 + if (of_flags == OF_GPIO_ACTIVE_LOW)
58 + flags |= GPIOF_ACTIVE_LOW;
59 +
60 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
61 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
62 + else
63 + flags |= GPIOF_IN;
64 +
65 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
66 + continue;
67 +
68 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
69 + gpio_export_with_name(gpio, dmc, name);
70 + nb++;
71 + }
72 + }
73 +
74 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
75 +
76 + return 0;
77 +}
78 +
79 +static struct platform_driver gpio_export_driver = {
80 + .driver = {
81 + .name = "gpio-export",
82 + .owner = THIS_MODULE,
83 + .of_match_table = of_match_ptr(gpio_export_ids),
84 + },
85 + .probe = of_gpio_export_probe,
86 +};
87 +
88 +module_platform_driver(gpio_export_driver);
89 +
90 +#endif
91 --- a/include/asm-generic/gpio.h
92 +++ b/include/asm-generic/gpio.h
93 @@ -125,6 +125,12 @@ static inline int gpio_export(unsigned g
94 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
95 }
96
97 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
98 +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
99 +{
100 + return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
101 +}
102 +
103 static inline int gpio_export_link(struct device *dev, const char *name,
104 unsigned gpio)
105 {
106 --- a/include/linux/gpio/consumer.h
107 +++ b/include/linux/gpio/consumer.h
108 @@ -715,6 +715,7 @@ static inline void devm_acpi_dev_remove_
109
110 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
111
112 +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
113 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
114 int gpiod_export_link(struct device *dev, const char *name,
115 struct gpio_desc *desc);
116 @@ -722,6 +723,13 @@ void gpiod_unexport(struct gpio_desc *de
117
118 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
119
120 +static inline int _gpiod_export(struct gpio_desc *desc,
121 + bool direction_may_change,
122 + const char *name)
123 +{
124 + return -ENOSYS;
125 +}
126 +
127 static inline int gpiod_export(struct gpio_desc *desc,
128 bool direction_may_change)
129 {
130 --- a/drivers/gpio/gpiolib-sysfs.c
131 +++ b/drivers/gpio/gpiolib-sysfs.c
132 @@ -572,7 +572,7 @@ static struct class gpio_class = {
133 *
134 * Returns zero on success, else an error.
135 */
136 -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
137 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
138 {
139 struct gpio_chip *chip;
140 struct gpio_device *gdev;
141 @@ -634,6 +634,8 @@ int gpiod_export(struct gpio_desc *desc,
142 offset = gpio_chip_hwgpio(desc);
143 if (chip->names && chip->names[offset])
144 ioname = chip->names[offset];
145 + if (name)
146 + ioname = name;
147
148 dev = device_create_with_groups(&gpio_class, &gdev->dev,
149 MKDEV(0, 0), data, gpio_groups,
150 @@ -655,6 +657,12 @@ err_unlock:
151 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
152 return status;
153 }
154 +EXPORT_SYMBOL_GPL(__gpiod_export);
155 +
156 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
157 +{
158 + return __gpiod_export(desc, direction_may_change, NULL);
159 +}
160 EXPORT_SYMBOL_GPL(gpiod_export);
161
162 static int match_export(struct device *dev, const void *desc)