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
6 Signed-off-by: John Crispin <blogic@openwrt.org>
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(-)
14 --- a/drivers/gpio/gpiolib-of.c
15 +++ b/drivers/gpio/gpiolib-of.c
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>
25 @@ -538,3 +540,73 @@ void of_gpiochip_remove(struct gpio_chip
26 gpiochip_remove_pin_ranges(chip);
27 of_node_put(chip->of_node);
30 +#ifdef CONFIG_GPIO_SYSFS
32 +static struct of_device_id gpio_export_ids[] = {
33 + { .compatible = "gpio-export" },
37 +static int __init of_gpio_export_probe(struct platform_device *pdev)
39 + struct device_node *np = pdev->dev.of_node;
40 + struct device_node *cnp;
44 + for_each_child_of_node(np, cnp) {
45 + const char *name = NULL;
51 + of_property_read_string(cnp, "gpio-export,name", &name);
54 + max_gpio = of_gpio_count(cnp);
56 + for (i = 0; i < max_gpio; i++) {
58 + enum of_gpio_flags of_flags;
60 + gpio = of_get_gpio_flags(cnp, i, &of_flags);
62 + if (of_flags == OF_GPIO_ACTIVE_LOW)
63 + flags |= GPIOF_ACTIVE_LOW;
65 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
66 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
70 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
73 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
74 + gpio_export_with_name(gpio, dmc, name);
79 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
84 +static struct platform_driver gpio_export_driver = {
86 + .name = "gpio-export",
87 + .owner = THIS_MODULE,
88 + .of_match_table = of_match_ptr(gpio_export_ids),
92 +static int __init of_gpio_export_init(void)
94 + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
96 +device_initcall(of_gpio_export_init);
99 --- a/include/asm-generic/gpio.h
100 +++ b/include/asm-generic/gpio.h
101 @@ -126,6 +126,12 @@ static inline int gpio_export(unsigned g
102 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
105 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
106 +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
108 + return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
111 static inline int gpio_export_link(struct device *dev, const char *name,
114 --- a/include/linux/gpio/consumer.h
115 +++ b/include/linux/gpio/consumer.h
116 @@ -427,6 +427,7 @@ static inline struct gpio_desc *devm_get
118 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
120 +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
121 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
122 int gpiod_export_link(struct device *dev, const char *name,
123 struct gpio_desc *desc);
124 @@ -434,6 +435,13 @@ void gpiod_unexport(struct gpio_desc *de
126 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
128 +static inline int _gpiod_export(struct gpio_desc *desc,
129 + bool direction_may_change,
135 static inline int gpiod_export(struct gpio_desc *desc,
136 bool direction_may_change)
138 --- a/drivers/gpio/gpiolib-sysfs.c
139 +++ b/drivers/gpio/gpiolib-sysfs.c
140 @@ -544,7 +544,7 @@ static struct class gpio_class = {
142 * Returns zero on success, else an error.
144 -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
145 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
147 struct gpio_chip *chip;
148 struct gpio_device *gdev;
149 @@ -606,6 +606,8 @@ int gpiod_export(struct gpio_desc *desc,
150 offset = gpio_chip_hwgpio(desc);
151 if (chip->names && chip->names[offset])
152 ioname = chip->names[offset];
156 dev = device_create_with_groups(&gpio_class, &gdev->dev,
157 MKDEV(0, 0), data, gpio_groups,
158 @@ -627,6 +629,12 @@ err_unlock:
159 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
162 +EXPORT_SYMBOL_GPL(__gpiod_export);
164 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
166 + return __gpiod_export(desc, direction_may_change, NULL);
168 EXPORT_SYMBOL_GPL(gpiod_export);
170 static int match_export(struct device *dev, const void *desc)