librecmc : Bump to v1.5.15
[librecmc/librecmc.git] / package / kernel / mac80211 / patches / ath / 548-ath9k_enable_gpio_chip.patch
1 From: Michal Cieslakiewicz <michal.cieslakiewicz@wp.pl>
2 Date: Sun, 31 Jan 2016 21:01:31 +0100
3 Subject: [PATCH v4 4/8] mac80211: ath9k: enable access to GPIO
4
5 Enable access to GPIO chip and its pins for Atheros AR92xx
6 wireless devices. For now AR9285 and AR9287 are supported.
7
8 Signed-off-by: Michal Cieslakiewicz <michal.cieslakiewicz@wp.pl>
9 Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 ---
11 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
12 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
13 @@ -24,6 +24,7 @@
14  #include <linux/completion.h>
15  #include <linux/time.h>
16  #include <linux/hw_random.h>
17 +#include <linux/gpio/driver.h>
18  
19  #include "common.h"
20  #include "debug.h"
21 @@ -1002,6 +1003,14 @@ struct ath_led {
22         struct led_classdev cdev;
23  };
24  
25 +#ifdef CONFIG_GPIOLIB
26 +struct ath9k_gpio_chip {
27 +       struct ath_softc *sc;
28 +       char label[32];
29 +       struct gpio_chip gchip;
30 +};
31 +#endif
32 +
33  struct ath_softc {
34         struct ieee80211_hw *hw;
35         struct device *dev;
36 @@ -1059,6 +1068,9 @@ struct ath_softc {
37  #ifdef CPTCFG_MAC80211_LEDS
38         const char *led_default_trigger;
39         struct list_head leds;
40 +#ifdef CONFIG_GPIOLIB
41 +       struct ath9k_gpio_chip *gpiochip;
42 +#endif
43  #endif
44  
45  #ifdef CPTCFG_ATH9K_DEBUGFS
46 --- a/drivers/net/wireless/ath/ath9k/gpio.c
47 +++ b/drivers/net/wireless/ath/ath9k/gpio.c
48 @@ -16,13 +16,139 @@
49  
50  #include "ath9k.h"
51  #include <linux/ath9k_platform.h>
52 +#include <linux/gpio.h>
53 +
54 +#ifdef CPTCFG_MAC80211_LEDS
55 +
56 +#ifdef CONFIG_GPIOLIB
57 +
58 +/***************/
59 +/*  GPIO Chip  */
60 +/***************/
61 +
62 +/* gpio_chip handler : set GPIO to input */
63 +static int ath9k_gpio_pin_cfg_input(struct gpio_chip *chip, unsigned offset)
64 +{
65 +       struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
66 +                                                 gchip);
67 +
68 +       ath9k_hw_gpio_request_in(gc->sc->sc_ah, offset, "ath9k-gpio");
69 +
70 +       return 0;
71 +}
72 +
73 +/* gpio_chip handler : set GPIO to output */
74 +static int ath9k_gpio_pin_cfg_output(struct gpio_chip *chip, unsigned offset,
75 +                                    int value)
76 +{
77 +       struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
78 +                                                 gchip);
79 +
80 +       ath9k_hw_gpio_request_out(gc->sc->sc_ah, offset, "ath9k-gpio",
81 +                                 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
82 +       ath9k_hw_set_gpio(gc->sc->sc_ah, offset, value);
83 +
84 +       return 0;
85 +}
86 +
87 +/* gpio_chip handler : query GPIO direction (0=out, 1=in) */
88 +static int ath9k_gpio_pin_get_dir(struct gpio_chip *chip, unsigned offset)
89 +{
90 +       struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
91 +                                                 gchip);
92 +       struct ath_hw *ah = gc->sc->sc_ah;
93 +
94 +       return !((REG_READ(ah, AR_GPIO_OE_OUT) >> (offset * 2)) & 3);
95 +}
96 +
97 +/* gpio_chip handler : get GPIO pin value */
98 +static int ath9k_gpio_pin_get(struct gpio_chip *chip, unsigned offset)
99 +{
100 +       struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
101 +                                                 gchip);
102 +
103 +       return ath9k_hw_gpio_get(gc->sc->sc_ah, offset);
104 +}
105 +
106 +/* gpio_chip handler : set GPIO pin to value */
107 +static void ath9k_gpio_pin_set(struct gpio_chip *chip, unsigned offset,
108 +                              int value)
109 +{
110 +       struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
111 +                                                 gchip);
112 +
113 +       ath9k_hw_set_gpio(gc->sc->sc_ah, offset, value);
114 +}
115 +
116 +/* register GPIO chip */
117 +static void ath9k_register_gpio_chip(struct ath_softc *sc)
118 +{
119 +       struct ath9k_gpio_chip *gc;
120 +       struct ath_hw *ah = sc->sc_ah;
121 +
122 +       gc = kzalloc(sizeof(struct ath9k_gpio_chip), GFP_KERNEL);
123 +       if (!gc)
124 +               return;
125 +
126 +       gc->sc = sc;
127 +       snprintf(gc->label, sizeof(gc->label), "ath9k-%s",
128 +                wiphy_name(sc->hw->wiphy));
129 +#ifdef CONFIG_OF
130 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,5,0)
131 +       gc->gchip.parent = sc->dev;
132 +#else
133 +       gc->gchip.dev = sc->dev;
134 +#endif
135 +#endif
136 +       gc->gchip.label = gc->label;
137 +       gc->gchip.base = -1;    /* determine base automatically */
138 +       gc->gchip.ngpio = ah->caps.num_gpio_pins;
139 +       gc->gchip.direction_input = ath9k_gpio_pin_cfg_input;
140 +       gc->gchip.direction_output = ath9k_gpio_pin_cfg_output;
141 +       gc->gchip.get_direction = ath9k_gpio_pin_get_dir;
142 +       gc->gchip.get = ath9k_gpio_pin_get;
143 +       gc->gchip.set = ath9k_gpio_pin_set;
144 +
145 +       if (gpiochip_add(&gc->gchip)) {
146 +               kfree(gc);
147 +               return;
148 +       }
149 +
150 +#ifdef CONFIG_OF
151 +       gc->gchip.owner = NULL;
152 +#endif
153 +       sc->gpiochip = gc;
154 +}
155 +
156 +/* remove GPIO chip */
157 +static void ath9k_unregister_gpio_chip(struct ath_softc *sc)
158 +{
159 +       struct ath9k_gpio_chip *gc = sc->gpiochip;
160 +
161 +       if (!gc)
162 +               return;
163 +
164 +       gpiochip_remove(&gc->gchip);
165 +       kfree(gc);
166 +       sc->gpiochip = NULL;
167 +}
168 +
169 +#else /* CONFIG_GPIOLIB */
170 +
171 +static inline void ath9k_register_gpio_chip(struct ath_softc *sc)
172 +{
173 +}
174 +
175 +static inline void ath9k_unregister_gpio_chip(struct ath_softc *sc)
176 +{
177 +}
178 +
179 +#endif /* CONFIG_GPIOLIB */
180  
181  /********************************/
182  /*      LED functions          */
183  /********************************/
184  
185 -#ifdef CPTCFG_MAC80211_LEDS
186 -
187  static void ath_fill_led_pin(struct ath_softc *sc)
188  {
189         struct ath_hw *ah = sc->sc_ah;
190 @@ -80,6 +206,12 @@ static int ath_add_led(struct ath_softc
191         else
192                 ath9k_hw_set_gpio(sc->sc_ah, gpio->gpio, gpio->active_low);
193  
194 +#ifdef CONFIG_GPIOLIB
195 +       /* If there is GPIO chip configured, reserve LED pin */
196 +       if (sc->gpiochip)
197 +               gpio_request(sc->gpiochip->gchip.base + gpio->gpio, gpio->name);
198 +#endif
199 +
200         return 0;
201  }
202  
203 @@ -136,17 +268,24 @@ void ath_deinit_leds(struct ath_softc *s
204  
205         while (!list_empty(&sc->leds)) {
206                 led = list_first_entry(&sc->leds, struct ath_led, list);
207 +#ifdef CONFIG_GPIOLIB
208 +               /* If there is GPIO chip configured, free LED pin */
209 +               if (sc->gpiochip)
210 +                       gpio_free(sc->gpiochip->gchip.base + led->gpio->gpio);
211 +#endif
212                 list_del(&led->list);
213                 ath_led_brightness(&led->cdev, LED_OFF);
214                 led_classdev_unregister(&led->cdev);
215                 ath9k_hw_gpio_free(sc->sc_ah, led->gpio->gpio);
216                 kfree(led);
217         }
218 +       ath9k_unregister_gpio_chip(sc);
219  }
220  
221  void ath_init_leds(struct ath_softc *sc)
222  {
223         struct ath9k_platform_data *pdata = sc->dev->platform_data;
224 +       struct device_node *np = sc->dev->of_node;
225         char led_name[32];
226         const char *trigger;
227         int i;
228 @@ -156,6 +295,15 @@ void ath_init_leds(struct ath_softc *sc)
229         if (AR_SREV_9100(sc->sc_ah))
230                 return;
231  
232 +       if (!np)
233 +               ath9k_register_gpio_chip(sc);
234 +
235 +       /* setup gpio controller only if requested and skip the led_pin setup */
236 +       if (of_property_read_bool(np, "gpio-controller")) {
237 +               ath9k_register_gpio_chip(sc);
238 +               return;
239 +       }
240 +
241         ath_fill_led_pin(sc);
242  
243         if (pdata && pdata->leds && pdata->num_leds)
244 @@ -180,6 +328,7 @@ void ath_init_leds(struct ath_softc *sc)
245         ath_create_gpio_led(sc, sc->sc_ah->led_pin, led_name, trigger,
246                            !sc->sc_ah->config.led_active_high);
247  }
248 +
249  #endif
250  
251  /*******************/