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/of_gpio.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/slab.h>
20 +#include <linux/init.h>
21 +#include <linux/platform_device.h>
25 @@ -296,3 +298,69 @@ void of_gpiochip_remove(struct gpio_chip
27 of_node_put(chip->of_node);
30 +static struct of_device_id gpio_export_ids[] = {
31 + { .compatible = "gpio-export" },
35 +static int __init of_gpio_export_probe(struct platform_device *pdev)
37 + struct device_node *np = pdev->dev.of_node;
38 + struct device_node *cnp;
42 + for_each_child_of_node(np, cnp) {
43 + const char *name = NULL;
49 + of_property_read_string(cnp, "gpio-export,name", &name);
52 + max_gpio = of_gpio_count(cnp);
54 + for (i = 0; i < max_gpio; i++) {
56 + enum of_gpio_flags of_flags;
58 + gpio = of_get_gpio_flags(cnp, i, &of_flags);
60 + if (of_flags == OF_GPIO_ACTIVE_LOW)
61 + flags |= GPIOF_ACTIVE_LOW;
63 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
64 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
68 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
71 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
72 + gpio_export_with_name(gpio, dmc, name);
77 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
82 +static struct platform_driver gpio_export_driver = {
84 + .name = "gpio-export",
85 + .owner = THIS_MODULE,
86 + .of_match_table = of_match_ptr(gpio_export_ids),
90 +static int __init of_gpio_export_init(void)
92 + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
94 +device_initcall(of_gpio_export_init);
95 --- a/drivers/gpio/gpiolib.c
96 +++ b/drivers/gpio/gpiolib.c
97 @@ -803,7 +803,7 @@ static struct class gpio_class = {
99 * Returns zero on success, else an error.
101 -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
102 +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
106 @@ -843,7 +843,8 @@ int gpiod_export(struct gpio_desc *desc,
107 offset = gpio_chip_hwgpio(desc);
108 if (desc->chip->names && desc->chip->names[offset])
109 ioname = desc->chip->names[offset];
113 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
114 desc, ioname ? ioname : "gpio%u",
116 @@ -880,6 +881,12 @@ fail_unlock:
117 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
120 +EXPORT_SYMBOL_GPL(_gpiod_export);
122 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
124 + return _gpiod_export(desc, direction_may_change, NULL);
126 EXPORT_SYMBOL_GPL(gpiod_export);
128 static int match_export(struct device *dev, const void *data)
129 --- a/include/asm-generic/gpio.h
130 +++ b/include/asm-generic/gpio.h
131 @@ -126,6 +126,11 @@ static inline int gpio_export(unsigned g
132 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
135 +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
137 + return _gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
140 static inline int gpio_export_link(struct device *dev, const char *name,
143 --- a/include/linux/gpio/consumer.h
144 +++ b/include/linux/gpio/consumer.h
145 @@ -219,6 +219,7 @@ static inline struct gpio_chip *gpiod_to
147 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
149 +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
150 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
151 int gpiod_export_link(struct device *dev, const char *name,
152 struct gpio_desc *desc);
153 @@ -227,6 +228,13 @@ void gpiod_unexport(struct gpio_desc *de
155 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
157 +static inline int _gpiod_export(struct gpio_desc *desc,
158 + bool direction_may_change,
164 static inline int gpiod_export(struct gpio_desc *desc,
165 bool direction_may_change)