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