24a1a643525246fba3ba7e4cf8fa666229bdbfa4
[oweals/openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-tl-wr841n-v9.c
1 /*
2  *  TP-LINK TL-WR840N v2/v3 / TL-WR841N/ND v9/v11 / TL-WR842N/ND v3
3  *
4  *  Copyright (C) 2014 Matthias Schiffer <mschiffer@universe-factory.net>
5  *  Copyright (C) 2016 Cezary Jackiewicz <cezary@eko.one.pl>
6  *  Copyright (C) 2016 Stijn Segers <francesco.borromini@gmail.com>
7  *  Copyright (C) 2017 Vaclav Svoboda <svoboda@neng.cz>
8  *
9  *  This program is free software; you can redistribute it and/or modify it
10  *  under the terms of the GNU General Public License version 2 as published
11  *  by the Free Software Foundation.
12  */
13
14 #include <linux/gpio.h>
15 #include <linux/platform_device.h>
16
17 #include <asm/mach-ath79/ath79.h>
18 #include <asm/mach-ath79/ar71xx_regs.h>
19
20 #include "common.h"
21 #include "dev-eth.h"
22 #include "dev-gpio-buttons.h"
23 #include "dev-leds-gpio.h"
24 #include "dev-m25p80.h"
25 #include "dev-usb.h"
26 #include "dev-wmac.h"
27 #include "machtypes.h"
28
29 #define TL_WR840NV2_GPIO_LED_SYSTEM     15
30 #define TL_WR840NV2_GPIO_LED_WLAN       13
31 #define TL_WR840NV2_GPIO_LED_WPS        3
32 #define TL_WR840NV2_GPIO_LED_WAN        4
33 #define TL_WR840NV2_GPIO_LED_LAN        16
34
35 #define TL_WR840NV2_GPIO_BTN_RESET      12
36
37 #define TL_WR841NV9_GPIO_LED_WLAN       13
38 #define TL_WR841NV9_GPIO_LED_QSS        3
39 #define TL_WR841NV9_GPIO_LED_WAN        4
40 #define TL_WR841NV9_GPIO_LED_LAN1       16
41 #define TL_WR841NV9_GPIO_LED_LAN2       15
42 #define TL_WR841NV9_GPIO_LED_LAN3       14
43 #define TL_WR841NV9_GPIO_LED_LAN4       11
44
45 #define TL_WR841NV9_GPIO_BTN_RESET      12
46 #define TL_WR841NV9_GPIO_BTN_WIFI       17
47
48 #define TL_WR841NV11_GPIO_LED_SYSTEM    1
49 #define TL_WR841NV11_GPIO_LED_QSS       3
50 #define TL_WR841NV11_GPIO_LED_WAN       4
51 #define TL_WR841NV11_GPIO_LED_WAN_STATUS        2
52 #define TL_WR841NV11_GPIO_LED_WLAN      13
53 #define TL_WR841NV11_GPIO_LED_LAN1      16
54 #define TL_WR841NV11_GPIO_LED_LAN2      15
55 #define TL_WR841NV11_GPIO_LED_LAN3      14
56 #define TL_WR841NV11_GPIO_LED_LAN4      11
57
58 #define TL_WR841NV11_GPIO_BTN_RESET     12
59 #define TL_WR841NV11_GPIO_BTN_WIFI      17
60
61 #define TL_WR842NV3_GPIO_LED_SYSTEM     2
62 #define TL_WR842NV3_GPIO_LED_WLAN       3
63 #define TL_WR842NV3_GPIO_LED_WAN_RED    4
64 #define TL_WR842NV3_GPIO_LED_WAN_GREEN  11
65 #define TL_WR842NV3_GPIO_LED_LAN1       12
66 #define TL_WR842NV3_GPIO_LED_LAN2       13
67 #define TL_WR842NV3_GPIO_LED_LAN3       14
68 #define TL_WR842NV3_GPIO_LED_LAN4       15
69 #define TL_WR842NV3_GPIO_LED_3G         16
70 #define TL_WR842NV3_GPIO_LED_WPS        17
71
72 #define TL_WR842NV3_GPIO_BTN_RESET      1
73 #define TL_WR842NV3_GPIO_BTN_WIFI       0
74
75 #define TL_WR841NV9_KEYS_POLL_INTERVAL  20      /* msecs */
76 #define TL_WR841NV9_KEYS_DEBOUNCE_INTERVAL (3 * TL_WR841NV9_KEYS_POLL_INTERVAL)
77
78 static const char *tl_wr841n_v9_part_probes[] = {
79         "tp-link",
80         NULL,
81 };
82
83 static struct flash_platform_data tl_wr841n_v9_flash_data = {
84         .part_probes    = tl_wr841n_v9_part_probes,
85 };
86
87 static struct gpio_led tl_wr840n_v2_leds_gpio[] __initdata = {
88         {
89                 .name           = "tp-link:green:system",
90                 .gpio           = TL_WR840NV2_GPIO_LED_SYSTEM,
91                 .active_low     = 1,
92         }, {
93                 .name           = "tp-link:green:lan",
94                 .gpio           = TL_WR840NV2_GPIO_LED_LAN,
95                 .active_low     = 1,
96         }, {
97                 .name           = "tp-link:green:wps",
98                 .gpio           = TL_WR840NV2_GPIO_LED_WPS,
99                 .active_low     = 1,
100         }, {
101                 .name           = "tp-link:green:wan",
102                 .gpio           = TL_WR840NV2_GPIO_LED_WAN,
103                 .active_low     = 1,
104         }, {
105                 .name           = "tp-link:green:wlan",
106                 .gpio           = TL_WR840NV2_GPIO_LED_WLAN,
107                 .active_low     = 1,
108         },
109 };
110
111 static struct gpio_keys_button tl_wr840n_v2_gpio_keys[] __initdata = {
112         {
113                 .desc           = "Reset button",
114                 .type           = EV_KEY,
115                 .code           = KEY_RESTART,
116                 .debounce_interval = TL_WR841NV9_KEYS_DEBOUNCE_INTERVAL,
117                 .gpio           = TL_WR840NV2_GPIO_BTN_RESET,
118                 .active_low     = 1,
119         }
120 };
121
122 static struct gpio_led tl_wr841n_v9_leds_gpio[] __initdata = {
123         {
124                 .name           = "tp-link:green:lan1",
125                 .gpio           = TL_WR841NV9_GPIO_LED_LAN1,
126                 .active_low     = 1,
127         }, {
128                 .name           = "tp-link:green:lan2",
129                 .gpio           = TL_WR841NV9_GPIO_LED_LAN2,
130                 .active_low     = 1,
131         }, {
132                 .name           = "tp-link:green:lan3",
133                 .gpio           = TL_WR841NV9_GPIO_LED_LAN3,
134                 .active_low     = 1,
135         }, {
136                 .name           = "tp-link:green:lan4",
137                 .gpio           = TL_WR841NV9_GPIO_LED_LAN4,
138                 .active_low     = 1,
139         }, {
140                 .name           = "tp-link:green:qss",
141                 .gpio           = TL_WR841NV9_GPIO_LED_QSS,
142                 .active_low     = 1,
143         }, {
144                 .name           = "tp-link:green:wan",
145                 .gpio           = TL_WR841NV9_GPIO_LED_WAN,
146                 .active_low     = 1,
147         }, {
148                 .name           = "tp-link:green:wlan",
149                 .gpio           = TL_WR841NV9_GPIO_LED_WLAN,
150                 .active_low     = 1,
151         },
152 };
153
154 static struct gpio_keys_button tl_wr841n_v9_gpio_keys[] __initdata = {
155         {
156                 .desc           = "Reset button",
157                 .type           = EV_KEY,
158                 .code           = KEY_RESTART,
159                 .debounce_interval = TL_WR841NV9_KEYS_DEBOUNCE_INTERVAL,
160                 .gpio           = TL_WR841NV9_GPIO_BTN_RESET,
161                 .active_low     = 1,
162         }, {
163                 .desc           = "WIFI button",
164                 .type           = EV_KEY,
165                 .code           = KEY_RFKILL,
166                 .debounce_interval = TL_WR841NV9_KEYS_DEBOUNCE_INTERVAL,
167                 .gpio           = TL_WR841NV9_GPIO_BTN_WIFI,
168                 .active_low     = 1,
169         }
170 };
171
172 static struct gpio_led tl_wr841n_v11_leds_gpio[] __initdata = {
173         {
174                 .name           = "tp-link:green:lan1",
175                 .gpio           = TL_WR841NV9_GPIO_LED_LAN1,
176                 .active_low     = 1,
177         }, {
178                 .name           = "tp-link:green:lan2",
179                 .gpio           = TL_WR841NV9_GPIO_LED_LAN2,
180                 .active_low     = 1,
181         }, {
182                 .name           = "tp-link:green:lan3",
183                 .gpio           = TL_WR841NV9_GPIO_LED_LAN3,
184                 .active_low     = 1,
185         }, {
186                 .name           = "tp-link:green:lan4",
187                 .gpio           = TL_WR841NV9_GPIO_LED_LAN4,
188                 .active_low     = 1,
189         }, {
190                 .name           = "tp-link:green:qss",
191                 .gpio           = TL_WR841NV9_GPIO_LED_QSS,
192                 .active_low     = 1,
193         }, {
194                 .name           = "tp-link:green:system",
195                 .gpio           = TL_WR841NV11_GPIO_LED_SYSTEM,
196                 .active_low     = 1,
197         }, {
198                 .name           = "tp-link:green:wan",
199                 .gpio           = TL_WR841NV9_GPIO_LED_WAN,
200                 .active_low     = 1,
201         }, {
202                 .name           = "tp-link:green:wan_status",
203                 .gpio           = TL_WR841NV11_GPIO_LED_WAN_STATUS,
204                 .active_low     = 1,
205         }, {
206                 .name           = "tp-link:green:wlan",
207                 .gpio           = TL_WR841NV9_GPIO_LED_WLAN,
208                 .active_low     = 1,
209         },
210 };
211
212 static struct gpio_led tl_wr842n_v3_leds_gpio[] __initdata = {
213         {
214                 .name           = "tp-link:green:lan1",
215                 .gpio           = TL_WR842NV3_GPIO_LED_LAN1,
216                 .active_low     = 1,
217         }, {
218                 .name           = "tp-link:green:lan2",
219                 .gpio           = TL_WR842NV3_GPIO_LED_LAN2,
220                 .active_low     = 1,
221         }, {
222                 .name           = "tp-link:green:lan3",
223                 .gpio           = TL_WR842NV3_GPIO_LED_LAN3,
224                 .active_low     = 1,
225         }, {
226                 .name           = "tp-link:green:lan4",
227                 .gpio           = TL_WR842NV3_GPIO_LED_LAN4,
228                 .active_low     = 1,
229         }, {
230                 .name           = "tp-link:green:wan",
231                 .gpio           = TL_WR842NV3_GPIO_LED_WAN_GREEN,
232                 .active_low     = 1,
233         }, {
234                 .name           = "tp-link:red:wan",
235                 .gpio           = TL_WR842NV3_GPIO_LED_WAN_RED,
236                 .active_low     = 1,
237         }, {
238                 .name           = "tp-link:green:wlan",
239                 .gpio           = TL_WR842NV3_GPIO_LED_WLAN,
240                 .active_low     = 1,
241         }, {
242                 .name           = "tp-link:green:system",
243                 .gpio           = TL_WR842NV3_GPIO_LED_SYSTEM,
244                 .active_low     = 1,
245         }, {
246                 .name           = "tp-link:green:3g",
247                 .gpio           = TL_WR842NV3_GPIO_LED_3G,
248                 .active_low     = 1,
249         }, {
250                 .name           = "tp-link:green:wps",
251                 .gpio           = TL_WR842NV3_GPIO_LED_WPS,
252                 .active_low     = 1,
253         },
254 };
255
256 static struct gpio_keys_button tl_wr842n_v3_gpio_keys[] __initdata = {
257         {
258                 .desc           = "Reset button",
259                 .type           = EV_KEY,
260                 .code           = KEY_RESTART,
261                 .debounce_interval = TL_WR841NV9_KEYS_DEBOUNCE_INTERVAL,
262                 .gpio           = TL_WR842NV3_GPIO_BTN_RESET,
263                 .active_low     = 1,
264         }, {
265                 .desc           = "WIFI button",
266                 .type           = EV_KEY,
267                 .code           = KEY_RFKILL,
268                 .debounce_interval = TL_WR841NV9_KEYS_DEBOUNCE_INTERVAL,
269                 .gpio           = TL_WR842NV3_GPIO_BTN_WIFI,
270                 .active_low     = 1,
271         }
272 };
273
274
275 static void __init tl_ap143_setup(void)
276 {
277         u8 *mac = (u8 *) KSEG1ADDR(0x1f01fc00);
278         u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);
279         u8 tmpmac[ETH_ALEN];
280
281         ath79_register_m25p80(&tl_wr841n_v9_flash_data);
282
283         ath79_setup_ar933x_phy4_switch(false, false);
284
285         ath79_register_mdio(0, 0x0);
286
287         /* LAN */
288         ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
289         ath79_eth1_data.duplex = DUPLEX_FULL;
290         ath79_switch_data.phy_poll_mask |= BIT(4);
291         ath79_init_mac(ath79_eth1_data.mac_addr, mac, 0);
292         ath79_register_eth(1);
293
294         /* WAN */
295         ath79_switch_data.phy4_mii_en = 1;
296         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
297         ath79_eth0_data.duplex = DUPLEX_FULL;
298         ath79_eth0_data.speed = SPEED_100;
299         ath79_eth0_data.phy_mask = BIT(4);
300         ath79_init_mac(ath79_eth0_data.mac_addr, mac, 1);
301         ath79_register_eth(0);
302
303         ath79_init_mac(tmpmac, mac, 0);
304         ath79_register_wmac(ee, tmpmac);
305 }
306
307
308 static void __init tl_wr840n_v2_setup(void)
309 {
310         tl_ap143_setup();
311
312         ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr840n_v2_leds_gpio),
313                                  tl_wr840n_v2_leds_gpio);
314
315         ath79_register_gpio_keys_polled(1, TL_WR841NV9_KEYS_POLL_INTERVAL,
316                                         ARRAY_SIZE(tl_wr840n_v2_gpio_keys),
317                                         tl_wr840n_v2_gpio_keys);
318 }
319
320 MIPS_MACHINE(ATH79_MACH_TL_WR840N_V2, "TL-WR840N-v2", "TP-LINK TL-WR840N v2",
321              tl_wr840n_v2_setup);
322
323 MIPS_MACHINE(ATH79_MACH_TL_WR840N_V3, "TL-WR840N-v3", "TP-LINK TL-WR840N v3",
324              tl_wr840n_v2_setup);
325
326 static void __init tl_wr841n_v9_setup(void)
327 {
328         tl_ap143_setup();
329
330         ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr841n_v9_leds_gpio),
331                                  tl_wr841n_v9_leds_gpio);
332
333         ath79_register_gpio_keys_polled(1, TL_WR841NV9_KEYS_POLL_INTERVAL,
334                                         ARRAY_SIZE(tl_wr841n_v9_gpio_keys),
335                                         tl_wr841n_v9_gpio_keys);
336 }
337
338 MIPS_MACHINE(ATH79_MACH_TL_WR841N_V9, "TL-WR841N-v9", "TP-LINK TL-WR841N/ND v9",
339              tl_wr841n_v9_setup);
340
341 static void __init tl_wr841n_v11_setup(void)
342 {
343         tl_ap143_setup();
344
345         ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr841n_v11_leds_gpio),
346                                  tl_wr841n_v11_leds_gpio);
347
348         ath79_register_gpio_keys_polled(1, TL_WR841NV9_KEYS_POLL_INTERVAL,
349                                         ARRAY_SIZE(tl_wr841n_v9_gpio_keys),
350                                         tl_wr841n_v9_gpio_keys);
351 }
352
353 MIPS_MACHINE(ATH79_MACH_TL_WR841N_V11, "TL-WR841N-v11", "TP-LINK TL-WR841N/ND v11",
354              tl_wr841n_v11_setup);
355
356 static void __init tl_wr842n_v3_setup(void)
357 {
358         tl_ap143_setup();
359
360         ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr842n_v3_leds_gpio),
361                                  tl_wr842n_v3_leds_gpio);
362
363         ath79_register_gpio_keys_polled(1, TL_WR841NV9_KEYS_POLL_INTERVAL,
364                                         ARRAY_SIZE(tl_wr842n_v3_gpio_keys),
365                                         tl_wr842n_v3_gpio_keys);
366
367         ath79_register_usb();
368 }
369
370 MIPS_MACHINE(ATH79_MACH_TL_WR842N_V3, "TL-WR842N-v3", "TP-LINK TL-WR842N/ND v3",
371              tl_wr842n_v3_setup);