05274065c70fbad293b184ca90586258916cdb11
[openwrt.git] / package / kernel / mac80211 / patches / 549-ath9k_enable_gpio_buttons.patch
1 From: Michal Cieslakiewicz <michal.cieslakiewicz@wp.pl>
2 Subject: [PATCH v5 5/8] mac80211: ath9k: enable GPIO buttons
3
4 Enable platform-defined GPIO button support for ath9k device.
5 Key poller is activated for attached platform buttons.
6 Requires ath9k GPIO chip access.
7
8 Signed-off-by: Michal Cieslakiewicz <michal.cieslakiewicz@wp.pl>
9 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
10 ---
11 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
12 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
13 @@ -1028,6 +1028,7 @@ struct ath_softc {
14         struct list_head leds;
15  #ifdef CONFIG_GPIOLIB
16         struct ath9k_gpio_chip *gpiochip;
17 +       struct platform_device *btnpdev;        /* gpio-keys-polled */
18  #endif
19  #endif
20  
21 --- a/drivers/net/wireless/ath/ath9k/gpio.c
22 +++ b/drivers/net/wireless/ath/ath9k/gpio.c
23 @@ -17,6 +17,8 @@
24  #include "ath9k.h"
25  #include <linux/ath9k_platform.h>
26  #include <linux/gpio.h>
27 +#include <linux/platform_device.h>
28 +#include <linux/gpio_keys.h>
29  
30  #ifdef CPTCFG_MAC80211_LEDS
31  
32 @@ -132,6 +134,63 @@ static void ath9k_unregister_gpio_chip(s
33         sc->gpiochip = NULL;
34  }
35  
36 +/******************/
37 +/*  GPIO Buttons  */
38 +/******************/
39 +
40 +/* add GPIO buttons */
41 +static void ath9k_init_buttons(struct ath_softc *sc)
42 +{
43 +       struct ath9k_platform_data *pdata = sc->dev->platform_data;
44 +       struct platform_device *pdev;
45 +       struct gpio_keys_platform_data gkpdata;
46 +       struct gpio_keys_button *bt;
47 +       int i;
48 +
49 +       if (!sc->gpiochip)
50 +               return;
51 +
52 +       if (!pdata || !pdata->btns || !pdata->num_btns)
53 +               return;
54 +
55 +       bt = devm_kmemdup(sc->dev, pdata->btns,
56 +                         pdata->num_btns * sizeof(struct gpio_keys_button),
57 +                         GFP_KERNEL);
58 +       if (!bt)
59 +               return;
60 +
61 +       for (i = 0; i < pdata->num_btns; i++) {
62 +               ath9k_hw_cfg_gpio_input(sc->sc_ah, pdata->btns[i].gpio);
63 +               bt[i].gpio = sc->gpiochip->gchip.base + pdata->btns[i].gpio;
64 +       }
65 +
66 +       memset(&gkpdata, 0, sizeof(struct gpio_keys_platform_data));
67 +       gkpdata.buttons = bt;
68 +       gkpdata.nbuttons = pdata->num_btns;
69 +       gkpdata.poll_interval = pdata->btn_poll_interval;
70 +
71 +       pdev = platform_device_register_data(sc->dev, "gpio-keys-polled",
72 +                                            PLATFORM_DEVID_AUTO, &gkpdata,
73 +                                            sizeof(gkpdata));
74 +       if (!IS_ERR_OR_NULL(pdev))
75 +               sc->btnpdev = pdev;
76 +       else {
77 +               sc->btnpdev = NULL;
78 +               devm_kfree(sc->dev, bt);
79 +       }
80 +}
81 +
82 +/* remove GPIO buttons */
83 +static void ath9k_deinit_buttons(struct ath_softc *sc)
84 +{
85 +       if (!sc->gpiochip || !sc->btnpdev)
86 +               return;
87 +
88 +       platform_device_unregister(sc->btnpdev);
89 +
90 +       sc->btnpdev = NULL;
91 +}
92 +
93  #else /* CONFIG_GPIOLIB */
94  
95  static inline void ath9k_register_gpio_chip(struct ath_softc *sc)
96 @@ -142,6 +201,14 @@ static inline void ath9k_unregister_gpio
97  {
98  }
99  
100 +static inline void ath9k_init_buttons(struct ath_softc *sc)
101 +{
102 +}
103 +
104 +static inline void ath9k_deinit_buttons(struct ath_softc *sc)
105 +{
106 +}
107 +
108  #endif /* CONFIG_GPIOLIB */
109  
110  /********************************/
111 @@ -246,6 +313,7 @@ void ath_deinit_leds(struct ath_softc *s
112  {
113         struct ath_led *led;
114  
115 +       ath9k_deinit_buttons(sc);
116         while (!list_empty(&sc->leds)) {
117                 led = list_first_entry(&sc->leds, struct ath_led, list);
118  #ifdef CONFIG_GPIOLIB
119 @@ -274,6 +342,7 @@ void ath_init_leds(struct ath_softc *sc)
120                 return;
121  
122         ath9k_register_gpio_chip(sc);
123 +       ath9k_init_buttons(sc);
124  
125         if (pdata && pdata->led_name)
126                 strncpy(led_name, pdata->led_name, sizeof(led_name));
127 @@ -289,7 +358,7 @@ void ath_init_leds(struct ath_softc *sc)
128         ath_create_gpio_led(sc, sc->sc_ah->led_pin, led_name, trigger,
129                             !sc->sc_ah->config.led_active_high);
130  
131 -       if (!pdata)
132 +       if (!pdata || !pdata->leds || !pdata->num_leds)
133                 return;
134  
135         for (i = 0; i < pdata->num_leds; i++)
136 --- a/include/linux/ath9k_platform.h
137 +++ b/include/linux/ath9k_platform.h
138 @@ -46,6 +46,10 @@ struct ath9k_platform_data {
139         int num_leds;
140         const struct gpio_led *leds;
141         const char *led_name;
142 +
143 +       unsigned num_btns;
144 +       const struct gpio_keys_button *btns;
145 +       unsigned btn_poll_interval;
146  };
147  
148  #endif /* _LINUX_ATH9K_PLATFORM_H */