ralink: bump to the target to v4.3
[openwrt.git] / target / linux / ramips / patches-4.3 / 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 diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
15 index fa6e3c8..c3f34c0 100644
16 --- a/drivers/gpio/gpiolib-of.c
17 +++ b/drivers/gpio/gpiolib-of.c
18 @@ -23,6 +23,8 @@
19  #include <linux/pinctrl/pinctrl.h>
20  #include <linux/slab.h>
21  #include <linux/gpio/machine.h>
22 +#include <linux/init.h>
23 +#include <linux/platform_device.h>
24  
25  #include "gpiolib.h"
26  
27 @@ -450,3 +452,69 @@ void of_gpiochip_remove(struct gpio_chip *chip)
28         gpiochip_remove_pin_ranges(chip);
29         of_node_put(chip->of_node);
30  }
31 +
32 +static struct of_device_id gpio_export_ids[] = {
33 +       { .compatible = "gpio-export" },
34 +       { /* sentinel */ }
35 +};
36 +
37 +static int __init 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 +
62 +                       if (of_flags == OF_GPIO_ACTIVE_LOW)
63 +                               flags |= GPIOF_ACTIVE_LOW;
64 +
65 +                       if (!of_property_read_u32(cnp, "gpio-export,output", &val))
66 +                               flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
67 +                       else
68 +                               flags |= GPIOF_IN;
69 +
70 +                       if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
71 +                               continue;
72 +
73 +                       dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
74 +                       gpio_export_with_name(gpio, dmc, name);
75 +                       nb++;
76 +               }
77 +       }
78 +
79 +       dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
80 +
81 +       return 0;
82 +}
83 +
84 +static struct platform_driver gpio_export_driver = {
85 +       .driver         = {
86 +               .name           = "gpio-export",
87 +               .owner  = THIS_MODULE,
88 +               .of_match_table = of_match_ptr(gpio_export_ids),
89 +       },
90 +};
91 +
92 +static int __init of_gpio_export_init(void)
93 +{
94 +       return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
95 +}
96 +device_initcall(of_gpio_export_init);
97 diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
98 index b57ed8e..0df781d 100644
99 --- a/drivers/gpio/gpiolib-sysfs.c
100 +++ b/drivers/gpio/gpiolib-sysfs.c
101 @@ -544,7 +544,7 @@ static struct class gpio_class = {
102   *
103   * Returns zero on success, else an error.
104   */
105 -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
106 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
107  {
108         struct gpio_chip        *chip;
109         struct gpiod_data       *data;
110 @@ -604,6 +604,8 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
111         offset = gpio_chip_hwgpio(desc);
112         if (chip->names && chip->names[offset])
113                 ioname = chip->names[offset];
114 +       if (name)
115 +               ioname = name;
116  
117         dev = device_create_with_groups(&gpio_class, chip->dev,
118                                         MKDEV(0, 0), data, gpio_groups,
119 @@ -625,6 +627,12 @@ err_unlock:
120         gpiod_dbg(desc, "%s: status %d\n", __func__, status);
121         return status;
122  }
123 +EXPORT_SYMBOL_GPL(__gpiod_export);
124 +
125 +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
126 +{
127 +       return __gpiod_export(desc, direction_may_change, NULL);
128 +}
129  EXPORT_SYMBOL_GPL(gpiod_export);
130  
131  static int match_export(struct device *dev, const void *desc)
132 diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h
133 index 40ec143..dcb07ab 100644
134 --- a/include/asm-generic/gpio.h
135 +++ b/include/asm-generic/gpio.h
136 @@ -122,6 +122,12 @@ static inline int gpio_export(unsigned gpio, bool direction_may_change)
137         return gpiod_export(gpio_to_desc(gpio), direction_may_change);
138  }
139  
140 +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
141 +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
142 +{
143 +       return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
144 +}
145 +
146  static inline int gpio_export_link(struct device *dev, const char *name,
147                                    unsigned gpio)
148  {
149 diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
150 index 14cac67..8097374 100644
151 --- a/include/linux/gpio/consumer.h
152 +++ b/include/linux/gpio/consumer.h
153 @@ -426,6 +426,7 @@ static inline struct gpio_desc *devm_get_gpiod_from_child(
154  
155  #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
156  
157 +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
158  int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
159  int gpiod_export_link(struct device *dev, const char *name,
160                       struct gpio_desc *desc);
161 @@ -433,6 +434,13 @@ void gpiod_unexport(struct gpio_desc *desc);
162  
163  #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
164  
165 +static inline int _gpiod_export(struct gpio_desc *desc,
166 +                              bool direction_may_change,
167 +                              const char *name)
168 +{
169 +       return -ENOSYS;
170 +}
171 +
172  static inline int gpiod_export(struct gpio_desc *desc,
173                                bool direction_may_change)
174  {
175 -- 
176 1.7.10.4
177