ramips: gpio: fix compilation if CONFIG_GPIO_SYSFS=n
[openwrt/openwrt.git] / target / linux / ramips / patches-4.9 / 0024-GPIO-add-named-gpio-exports.patch
1 From 4267880319bc1a2270d352e0ded6d6386242a7ef 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 24/53] GPIO: add named gpio exports
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8 drivers/gpio/gpiolib-of.c | 68 +++++++++++++++++++++++++++++++++++++++++
9 drivers/gpio/gpiolib-sysfs.c | 10 +++++-
10 include/asm-generic/gpio.h | 6 ++++
11 include/linux/gpio/consumer.h | 8 +++++
12 4 files changed, 91 insertions(+), 1 deletion(-)
13
14 --- a/drivers/gpio/gpiolib-of.c
15 +++ b/drivers/gpio/gpiolib-of.c
16 @@ -23,6 +23,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
25 @@ -538,3 +540,69 @@ void of_gpiochip_remove(struct gpio_chip
26 gpiochip_remove_pin_ranges(chip);
27 of_node_put(chip->of_node);
28 }
29 +
30 +static struct of_device_id gpio_export_ids[] = {
31 + { .compatible = "gpio-export" },
32 + { /* sentinel */ }
33 +};
34 +
35 +static int __init of_gpio_export_probe(struct platform_device *pdev)
36 +{
37 + struct device_node *np = pdev->dev.of_node;
38 + struct device_node *cnp;
39 + u32 val;
40 + int nb = 0;
41 +
42 + for_each_child_of_node(np, cnp) {
43 + const char *name = NULL;
44 + int gpio;
45 + bool dmc;
46 + int max_gpio = 1;
47 + int i;
48 +
49 + of_property_read_string(cnp, "gpio-export,name", &name);
50 +
51 + if (!name)
52 + max_gpio = of_gpio_count(cnp);
53 +
54 + for (i = 0; i < max_gpio; i++) {
55 + unsigned flags = 0;
56 + enum of_gpio_flags of_flags;
57 +
58 + gpio = of_get_gpio_flags(cnp, i, &of_flags);
59 +
60 + if (of_flags == OF_GPIO_ACTIVE_LOW)
61 + flags |= GPIOF_ACTIVE_LOW;
62 +
63 + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
64 + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
65 + else
66 + flags |= GPIOF_IN;
67 +
68 + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
69 + continue;
70 +
71 + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
72 + gpio_export_with_name(gpio, dmc, name);
73 + nb++;
74 + }
75 + }
76 +
77 + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
78 +
79 + return 0;
80 +}
81 +
82 +static struct platform_driver gpio_export_driver = {
83 + .driver = {
84 + .name = "gpio-export",
85 + .owner = THIS_MODULE,
86 + .of_match_table = of_match_ptr(gpio_export_ids),
87 + },
88 +};
89 +
90 +static int __init of_gpio_export_init(void)
91 +{
92 + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
93 +}
94 +device_initcall(of_gpio_export_init);
95 --- a/drivers/gpio/gpiolib-sysfs.c
96 +++ b/drivers/gpio/gpiolib-sysfs.c
97 @@ -544,7 +544,7 @@ static struct class gpio_class = {
98 *
99 * Returns zero on success, else an error.
100 */
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)
103 {
104 struct gpio_chip *chip;
105 struct gpio_device *gdev;
106 @@ -606,6 +606,8 @@ int gpiod_export(struct gpio_desc *desc,
107 offset = gpio_chip_hwgpio(desc);
108 if (chip->names && chip->names[offset])
109 ioname = chip->names[offset];
110 + if (name)
111 + ioname = name;
112
113 dev = device_create_with_groups(&gpio_class, &gdev->dev,
114 MKDEV(0, 0), data, gpio_groups,
115 @@ -627,6 +629,12 @@ err_unlock:
116 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
117 return status;
118 }
119 +EXPORT_SYMBOL_GPL(__gpiod_export);
120 +
121 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
122 +{
123 + return __gpiod_export(desc, direction_may_change, NULL);
124 +}
125 EXPORT_SYMBOL_GPL(gpiod_export);
126
127 static int match_export(struct device *dev, const void *desc)
128 --- a/include/asm-generic/gpio.h
129 +++ b/include/asm-generic/gpio.h
130 @@ -126,6 +126,11 @@ static inline int gpio_export(unsigned g
131 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
132 }
133
134 +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
135 +{
136 + return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
137 +}
138 +
139 static inline int gpio_export_link(struct device *dev, const char *name,
140 unsigned gpio)
141 {
142 --- a/include/linux/gpio/consumer.h
143 +++ b/include/linux/gpio/consumer.h
144 @@ -434,6 +435,13 @@ void gpiod_unexport(struct gpio_desc *de
145
146 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
147
148 +static inline int _gpiod_export(struct gpio_desc *desc,
149 + bool direction_may_change,
150 + const char *name)
151 +{
152 + return -ENOSYS;
153 +}
154 +
155 static inline int gpiod_export(struct gpio_desc *desc,
156 bool direction_may_change)
157 {