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
5 Enable access to GPIO chip and its pins for Atheros AR92xx
6 wireless devices. For now AR9285 and AR9287 are supported.
8 Signed-off-by: Michal Cieslakiewicz <michal.cieslakiewicz@wp.pl>
9 Signed-off-by: Felix Fietkau <nbd@nbd.name>
11 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
12 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
14 #include <linux/completion.h>
15 #include <linux/time.h>
16 #include <linux/hw_random.h>
17 +#include <linux/gpio/driver.h>
21 @@ -959,6 +960,14 @@ struct ath_led {
22 struct led_classdev cdev;
25 +#ifdef CONFIG_GPIOLIB
26 +struct ath9k_gpio_chip {
27 + struct ath_softc *sc;
29 + struct gpio_chip gchip;
34 struct ieee80211_hw *hw;
36 @@ -1013,6 +1022,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;
45 #ifdef CPTCFG_ATH9K_DEBUGFS
46 --- a/drivers/net/wireless/ath/ath9k/gpio.c
47 +++ b/drivers/net/wireless/ath/ath9k/gpio.c
51 #include <linux/ath9k_platform.h>
52 +#include <linux/gpio.h>
54 +#ifdef CPTCFG_MAC80211_LEDS
56 +#ifdef CONFIG_GPIOLIB
62 +/* gpio_chip handler : set GPIO to input */
63 +static int ath9k_gpio_pin_cfg_input(struct gpio_chip *chip, unsigned offset)
65 + struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
68 + ath9k_hw_gpio_request_in(gc->sc->sc_ah, offset, "ath9k-gpio");
73 +/* gpio_chip handler : set GPIO to output */
74 +static int ath9k_gpio_pin_cfg_output(struct gpio_chip *chip, unsigned offset,
77 + struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
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);
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)
90 + struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
92 + struct ath_hw *ah = gc->sc->sc_ah;
94 + return !((REG_READ(ah, AR_GPIO_OE_OUT) >> (offset * 2)) & 3);
97 +/* gpio_chip handler : get GPIO pin value */
98 +static int ath9k_gpio_pin_get(struct gpio_chip *chip, unsigned offset)
100 + struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
103 + return ath9k_hw_gpio_get(gc->sc->sc_ah, offset);
106 +/* gpio_chip handler : set GPIO pin to value */
107 +static void ath9k_gpio_pin_set(struct gpio_chip *chip, unsigned offset,
110 + struct ath9k_gpio_chip *gc = container_of(chip, struct ath9k_gpio_chip,
113 + ath9k_hw_set_gpio(gc->sc->sc_ah, offset, value);
116 +/* register GPIO chip */
117 +static void ath9k_register_gpio_chip(struct ath_softc *sc)
119 + struct ath9k_gpio_chip *gc;
120 + struct ath_hw *ah = sc->sc_ah;
122 + gc = kzalloc(sizeof(struct ath9k_gpio_chip), GFP_KERNEL);
126 + snprintf(gc->label, sizeof(gc->label), "ath9k-%s",
127 + wiphy_name(sc->hw->wiphy));
128 + gc->gchip.label = gc->label;
129 + gc->gchip.base = -1; /* determine base automatically */
130 + gc->gchip.ngpio = ah->caps.num_gpio_pins;
131 + gc->gchip.direction_input = ath9k_gpio_pin_cfg_input;
132 + gc->gchip.direction_output = ath9k_gpio_pin_cfg_output;
133 + gc->gchip.get_direction = ath9k_gpio_pin_get_dir;
134 + gc->gchip.get = ath9k_gpio_pin_get;
135 + gc->gchip.set = ath9k_gpio_pin_set;
136 + gc->gchip.owner = THIS_MODULE;
138 + if (gpiochip_add(&gc->gchip)) {
147 +/* remove GPIO chip */
148 +static void ath9k_unregister_gpio_chip(struct ath_softc *sc)
150 + struct ath9k_gpio_chip *gc = sc->gpiochip;
155 + gpiochip_remove(&gc->gchip);
157 + sc->gpiochip = NULL;
160 +#else /* CONFIG_GPIOLIB */
162 +static inline void ath9k_register_gpio_chip(struct ath_softc *sc)
166 +static inline void ath9k_unregister_gpio_chip(struct ath_softc *sc)
170 +#endif /* CONFIG_GPIOLIB */
172 /********************************/
174 /********************************/
176 -#ifdef CPTCFG_MAC80211_LEDS
178 static void ath_fill_led_pin(struct ath_softc *sc)
180 struct ath_hw *ah = sc->sc_ah;
181 @@ -80,6 +197,12 @@ static int ath_add_led(struct ath_softc
183 ath9k_hw_set_gpio(sc->sc_ah, gpio->gpio, gpio->active_low);
185 +#ifdef CONFIG_GPIOLIB
186 + /* If there is GPIO chip configured, reserve LED pin */
188 + gpio_request(sc->gpiochip->gchip.base + gpio->gpio, gpio->name);
194 @@ -136,12 +259,18 @@ void ath_deinit_leds(struct ath_softc *s
196 while (!list_empty(&sc->leds)) {
197 led = list_first_entry(&sc->leds, struct ath_led, list);
198 +#ifdef CONFIG_GPIOLIB
199 + /* If there is GPIO chip configured, free LED pin */
201 + gpio_free(sc->gpiochip->gchip.base + led->gpio->gpio);
203 list_del(&led->list);
204 ath_led_brightness(&led->cdev, LED_OFF);
205 led_classdev_unregister(&led->cdev);
206 ath9k_hw_gpio_free(sc->sc_ah, led->gpio->gpio);
209 + ath9k_unregister_gpio_chip(sc);
212 void ath_init_leds(struct ath_softc *sc)
213 @@ -158,6 +287,8 @@ void ath_init_leds(struct ath_softc *sc)
215 ath_fill_led_pin(sc);
217 + ath9k_register_gpio_chip(sc);
219 if (pdata && pdata->led_name)
220 strncpy(led_name, pdata->led_name, sizeof(led_name));
222 @@ -178,6 +309,7 @@ void ath_init_leds(struct ath_softc *sc)
223 for (i = 0; i < pdata->num_leds; i++)
224 ath_create_platform_led(sc, &pdata->leds[i]);
229 /*******************/