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>
7 --- a/drivers/gpio/gpiolib-of.c
8 +++ b/drivers/gpio/gpiolib-of.c
11 #include <linux/gpio/consumer.h>
12 #include <linux/gpio/machine.h>
13 +#include <linux/init.h>
14 +#include <linux/platform_device.h>
17 #include "gpiolib-of.h"
18 @@ -1111,3 +1113,74 @@ void of_gpiochip_remove(struct gpio_chip
20 of_node_put(dev_of_node(&chip->gpiodev->dev));
23 +#ifdef CONFIG_GPIO_SYSFS
25 +static struct of_device_id gpio_export_ids[] = {
26 + { .compatible = "gpio-export" },
30 +static int of_gpio_export_probe(struct platform_device *pdev)
32 + struct device_node *np = pdev->dev.of_node;
33 + struct device_node *cnp;
37 + for_each_child_of_node(np, cnp) {
38 + const char *name = NULL;
44 + of_property_read_string(cnp, "gpio-export,name", &name);
47 + max_gpio = of_gpio_named_count(cnp, "gpios");
49 + for (i = 0; i < max_gpio; i++) {
50 + struct gpio_desc *desc;
52 + enum of_gpio_flags of_flags;
54 + desc = of_get_named_gpiod_flags(cnp, "gpios", i, &of_flags);
56 + return PTR_ERR(desc);
57 + gpio = desc_to_gpio(desc);
59 + if (of_flags & OF_GPIO_ACTIVE_LOW)
60 + flags |= GPIOF_ACTIVE_LOW;
62 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
63 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
67 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
70 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
71 + gpio_export_with_name(gpio_to_desc(gpio), dmc, name);
76 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
81 +static struct platform_driver gpio_export_driver = {
83 + .name = "gpio-export",
84 + .owner = THIS_MODULE,
85 + .of_match_table = of_match_ptr(gpio_export_ids),
87 + .probe = of_gpio_export_probe,
90 +module_platform_driver(gpio_export_driver);
93 --- a/include/linux/gpio/consumer.h
94 +++ b/include/linux/gpio/consumer.h
95 @@ -644,7 +644,10 @@ static inline struct gpio_desc *acpi_get
97 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
99 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
100 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
101 +int gpio_export_with_name(struct gpio_desc *desc, bool direction_may_change,
103 int gpiod_export_link(struct device *dev, const char *name,
104 struct gpio_desc *desc);
105 void gpiod_unexport(struct gpio_desc *desc);
106 @@ -653,12 +656,26 @@ void gpiod_unexport(struct gpio_desc *de
108 #include <asm/errno.h>
110 +static inline int __gpiod_export(struct gpio_desc *desc,
111 + bool direction_may_change,
117 static inline int gpiod_export(struct gpio_desc *desc,
118 bool direction_may_change)
123 +static inline int gpio_export_with_name(struct gpio_desc *desc,
124 + bool direction_may_change,
130 static inline int gpiod_export_link(struct device *dev, const char *name,
131 struct gpio_desc *desc)
133 --- a/drivers/gpio/gpiolib-sysfs.c
134 +++ b/drivers/gpio/gpiolib-sysfs.c
135 @@ -557,7 +557,7 @@ static struct class gpio_class = {
137 * Returns zero on success, else an error.
139 -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
140 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
142 struct gpio_chip *chip;
143 struct gpio_device *gdev;
144 @@ -619,6 +619,8 @@ int gpiod_export(struct gpio_desc *desc,
145 offset = gpio_chip_hwgpio(desc);
146 if (chip->names && chip->names[offset])
147 ioname = chip->names[offset];
151 dev = device_create_with_groups(&gpio_class, &gdev->dev,
152 MKDEV(0, 0), data, gpio_groups,
153 @@ -640,8 +642,21 @@ int gpiod_export(struct gpio_desc *desc,
154 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
157 +EXPORT_SYMBOL_GPL(__gpiod_export);
159 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
161 + return __gpiod_export(desc, direction_may_change, NULL);
163 EXPORT_SYMBOL_GPL(gpiod_export);
165 +int gpio_export_with_name(struct gpio_desc *desc, bool direction_may_change,
168 + return __gpiod_export(desc, direction_may_change, name);
170 +EXPORT_SYMBOL_GPL(gpio_export_with_name);
172 static int match_export(struct device *dev, const void *desc)
174 struct gpiod_data *data = dev_get_drvdata(dev);