mac80211: ath9k: enable GPIO buttons
[openwrt.git] / package / kernel / mac80211 / patches / 550-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 ---
10  drivers/net/wireless/ath/ath9k/ath9k.h |   16 ++++++
11  drivers/net/wireless/ath/ath9k/gpio.c  |   77 +++++++++++++++++++++++++++++++++
12  drivers/net/wireless/ath/ath9k/init.c  |    2 
13  include/linux/ath9k_platform.h         |    4 +
14  4 files changed, 99 insertions(+)
15
16 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
17 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
18 @@ -825,6 +825,13 @@ int ath_create_gpio_led(struct ath_softc
19  void ath9k_register_gpio_chip(struct ath_softc *sc);
20  void ath9k_unregister_gpio_chip(struct ath_softc *sc);
21  
22 +/******************/
23 +/*  GPIO Buttons  */
24 +/******************/
25 +
26 +void ath9k_init_buttons(struct ath_softc *sc);
27 +void ath9k_deinit_buttons(struct ath_softc *sc);
28 +
29  #else
30  static inline void ath_init_leds(struct ath_softc *sc)
31  {
32 @@ -844,6 +851,14 @@ static inline void ath9k_register_gpio_c
33  static inline void ath9k_unregister_gpio_chip(struct ath_softc *sc)
34  {
35  }
36 +
37 +static inline void ath9k_init_buttons(struct ath_softc *sc)
38 +{
39 +}
40 +
41 +static inline void ath9k_deinit_buttons(struct ath_softc *sc)
42 +{
43 +}
44  #endif
45  
46  /************************/
47 @@ -1040,6 +1055,7 @@ struct ath_softc {
48         const char *led_default_trigger;
49         struct list_head leds;
50         struct ath9k_gpio_chip *gpiochip;
51 +       struct platform_device *btnpdev;        /* gpio-keys-polled */
52  #endif
53  
54  #ifdef CPTCFG_ATH9K_DEBUGFS
55 --- a/drivers/net/wireless/ath/ath9k/gpio.c
56 +++ b/drivers/net/wireless/ath/ath9k/gpio.c
57 @@ -24,6 +24,8 @@
58  #ifdef CPTCFG_MAC80211_LEDS
59  
60  #include <asm-generic/gpio.h>
61 +#include <linux/platform_device.h>
62 +#include <linux/gpio_keys.h>
63  
64  static void ath_led_brightness(struct led_classdev *led_cdev,
65                                enum led_brightness brightness)
66 @@ -159,7 +161,7 @@ void ath_init_leds(struct ath_softc *sc)
67         ath_create_gpio_led(sc, sc->sc_ah->led_pin, led_name, trigger,
68                             !sc->sc_ah->config.led_active_high);
69  
70 -       if (!pdata)
71 +       if (!pdata || !pdata->leds || !pdata->num_leds)
72                 return;
73  
74         for (i = 0; i < pdata->num_leds; i++)
75 @@ -307,6 +309,63 @@ void ath9k_unregister_gpio_chip(struct a
76         sc->gpiochip = NULL;
77  }
78  
79 +/******************/
80 +/*  GPIO Buttons  */
81 +/******************/
82 +
83 +/* add GPIO buttons */
84 +void ath9k_init_buttons(struct ath_softc *sc)
85 +{
86 +       struct ath9k_platform_data *pdata = sc->dev->platform_data;
87 +       struct platform_device *pdev;
88 +       struct gpio_keys_platform_data gkpdata;
89 +       struct gpio_keys_button *bt;
90 +       int i;
91 +
92 +       if (!sc->gpiochip)
93 +               return;
94 +
95 +       if (!pdata || !pdata->btns || !pdata->num_btns)
96 +               return;
97 +
98 +       bt = devm_kmemdup(sc->dev, pdata->btns,
99 +                         pdata->num_btns * sizeof(struct gpio_keys_button),
100 +                         GFP_KERNEL);
101 +       if (!bt)
102 +               return;
103 +
104 +       for (i = 0; i < pdata->num_btns; i++) {
105 +               ath9k_hw_cfg_gpio_input(sc->sc_ah, pdata->btns[i].gpio);
106 +               bt[i].gpio = sc->gpiochip->gchip.base + pdata->btns[i].gpio;
107 +       }
108 +
109 +       memset(&gkpdata, 0, sizeof(struct gpio_keys_platform_data));
110 +       gkpdata.buttons = bt;
111 +       gkpdata.nbuttons = pdata->num_btns;
112 +       gkpdata.poll_interval = pdata->btn_poll_interval;
113 +
114 +       pdev = platform_device_register_data(sc->dev, "gpio-keys-polled",
115 +                                            PLATFORM_DEVID_AUTO, &gkpdata,
116 +                                            sizeof(gkpdata));
117 +       if (!IS_ERR_OR_NULL(pdev))
118 +               sc->btnpdev = pdev;
119 +       else {
120 +               sc->btnpdev = NULL;
121 +               devm_kfree(sc->dev, bt);
122 +       }
123 +}
124 +
125 +/* remove GPIO buttons */
126 +void ath9k_deinit_buttons(struct ath_softc *sc)
127 +{
128 +       if (!sc->gpiochip || !sc->btnpdev)
129 +               return;
130 +
131 +       platform_device_unregister(sc->btnpdev);
132 +
133 +       sc->btnpdev = NULL;
134 +}
135 +
136  #endif
137  
138  /*******************/
139 --- a/drivers/net/wireless/ath/ath9k/init.c
140 +++ b/drivers/net/wireless/ath/ath9k/init.c
141 @@ -977,6 +977,7 @@ int ath9k_init_device(u16 devid, struct
142  
143         ath9k_register_gpio_chip(sc);
144         ath_init_leds(sc);
145 +       ath9k_init_buttons(sc);
146         ath_start_rfkill_poll(sc);
147  
148         return 0;
149 @@ -1022,6 +1023,7 @@ void ath9k_deinit_device(struct ath_soft
150         ath9k_ps_wakeup(sc);
151  
152         wiphy_rfkill_stop_polling(sc->hw->wiphy);
153 +       ath9k_deinit_buttons(sc);
154         ath_deinit_leds(sc);
155         ath9k_unregister_gpio_chip(sc);
156  
157 --- a/include/linux/ath9k_platform.h
158 +++ b/include/linux/ath9k_platform.h
159 @@ -46,6 +46,10 @@ struct ath9k_platform_data {
160         int num_leds;
161         const struct gpio_led *leds;
162         const char *led_name;
163 +
164 +       unsigned num_btns;
165 +       const struct gpio_keys_button *btns;
166 +       unsigned btn_poll_interval;
167  };
168  
169  #endif /* _LINUX_ATH9K_PLATFORM_H */