mediatek: backport upstream mediatek patches
[openwrt/openwrt.git] / target / linux / mediatek / patches-4.14 / 0171-pinctrl-mediatek-add-pinctrl-driver-for-MT7622-SoC.patch
1 From 4318e143fe3ac617de20cf72aa32fd9d3ad92ee6 Mon Sep 17 00:00:00 2001
2 From: Sean Wang <sean.wang@mediatek.com>
3 Date: Tue, 12 Dec 2017 14:24:20 +0800
4 Subject: [PATCH 171/224] pinctrl: mediatek: add pinctrl driver for MT7622 SoC
5
6 Add support for pinctrl on MT7622 SoC. The IO core found on the SoC has
7 the registers for pinctrl, pinconf and gpio mixed up in the same register
8 range. However, the IO core for the MT7622 SoC is completely distinct from
9 anyone of previous MediaTek SoCs which already had support, such as
10 the hardware internal, register address map and register detailed
11 definition for each pin.
12
13 Therefore, instead, the driver is being newly implemented by reusing
14 generic methods provided from the core layer with GENERIC_PINCONF,
15 GENERIC_PINCTRL_GROUPS, and GENERIC_PINMUX_FUNCTIONS for the sake of code
16 simplicity and rid of superfluous code. Where the function of pins
17 determined by groups is utilized in this driver which can help developers
18 less confused with what combinations of pins effective on the SoC and even
19 reducing the mistakes during the integration of those relevant boards.
20
21 As the gpio_chip handling is also only a few lines, the driver also
22 implements the gpio functionality directly through GPIOLIB.
23
24 Signed-off-by: Sean Wang <sean.wang@mediatek.com>
25 Reviewed-by: Biao Huang <biao.huang@mediatek.com>
26 Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
27 ---
28 drivers/pinctrl/Makefile | 2 +-
29 drivers/pinctrl/mediatek/Kconfig | 10 +
30 drivers/pinctrl/mediatek/Makefile | 3 +-
31 drivers/pinctrl/mediatek/pinctrl-mt7622.c | 1595 +++++++++++++++++++++++++++++
32 4 files changed, 1608 insertions(+), 2 deletions(-)
33 create mode 100644 drivers/pinctrl/mediatek/pinctrl-mt7622.c
34
35 diff --git a/drivers/pinctrl/Makefile b/drivers/pinctrl/Makefile
36 index c16e27900dbb..3a798d5b0900 100644
37 --- a/drivers/pinctrl/Makefile
38 +++ b/drivers/pinctrl/Makefile
39 @@ -63,5 +63,5 @@ obj-$(CONFIG_PINCTRL_SUNXI) += sunxi/
40 obj-y += ti/
41 obj-$(CONFIG_PINCTRL_UNIPHIER) += uniphier/
42 obj-$(CONFIG_ARCH_VT8500) += vt8500/
43 -obj-$(CONFIG_PINCTRL_MTK) += mediatek/
44 +obj-y += mediatek/
45 obj-$(CONFIG_PINCTRL_ZX) += zte/
46 diff --git a/drivers/pinctrl/mediatek/Kconfig b/drivers/pinctrl/mediatek/Kconfig
47 index 03b3023d5fe5..3e598740b379 100644
48 --- a/drivers/pinctrl/mediatek/Kconfig
49 +++ b/drivers/pinctrl/mediatek/Kconfig
50 @@ -32,6 +32,16 @@ config PINCTRL_MT8127
51 select PINCTRL_MTK
52
53 # For ARMv8 SoCs
54 +config PINCTRL_MT7622
55 + bool "MediaTek MT7622 pin control"
56 + depends on OF
57 + depends on ARM64 || COMPILE_TEST
58 + select GENERIC_PINCONF
59 + select GENERIC_PINCTRL_GROUPS
60 + select GENERIC_PINMUX_FUNCTIONS
61 + select GPIOLIB
62 + select OF_GPIO
63 +
64 config PINCTRL_MT8173
65 bool "Mediatek MT8173 pin control"
66 depends on OF
67 diff --git a/drivers/pinctrl/mediatek/Makefile b/drivers/pinctrl/mediatek/Makefile
68 index e59c613d4ddd..6e3ca6bbda7a 100644
69 --- a/drivers/pinctrl/mediatek/Makefile
70 +++ b/drivers/pinctrl/mediatek/Makefile
71 @@ -1,9 +1,10 @@
72 # Core
73 -obj-y += pinctrl-mtk-common.o
74 +obj-$(CONFIG_PINCTRL_MTK) += pinctrl-mtk-common.o
75
76 # SoC Drivers
77 obj-$(CONFIG_PINCTRL_MT2701) += pinctrl-mt2701.o
78 obj-$(CONFIG_PINCTRL_MT8135) += pinctrl-mt8135.o
79 obj-$(CONFIG_PINCTRL_MT8127) += pinctrl-mt8127.o
80 +obj-$(CONFIG_PINCTRL_MT7622) += pinctrl-mt7622.o
81 obj-$(CONFIG_PINCTRL_MT8173) += pinctrl-mt8173.o
82 obj-$(CONFIG_PINCTRL_MT6397) += pinctrl-mt6397.o
83 diff --git a/drivers/pinctrl/mediatek/pinctrl-mt7622.c b/drivers/pinctrl/mediatek/pinctrl-mt7622.c
84 new file mode 100644
85 index 000000000000..3824d82888ac
86 --- /dev/null
87 +++ b/drivers/pinctrl/mediatek/pinctrl-mt7622.c
88 @@ -0,0 +1,1595 @@
89 +/*
90 + * MediaTek MT7622 Pinctrl Driver
91 + *
92 + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
93 + *
94 + * This program is free software; you can redistribute it and/or modify
95 + * it under the terms of the GNU General Public License version 2 as
96 + * published by the Free Software Foundation.
97 + *
98 + * This program is distributed in the hope that it will be useful,
99 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
100 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
101 + * GNU General Public License for more details.
102 + */
103 +
104 +#include <linux/gpio.h>
105 +#include <linux/gpio/driver.h>
106 +#include <linux/io.h>
107 +#include <linux/init.h>
108 +#include <linux/mfd/syscon.h>
109 +#include <linux/of.h>
110 +#include <linux/of_platform.h>
111 +#include <linux/platform_device.h>
112 +#include <linux/pinctrl/pinctrl.h>
113 +#include <linux/pinctrl/pinmux.h>
114 +#include <linux/pinctrl/pinconf.h>
115 +#include <linux/pinctrl/pinconf-generic.h>
116 +#include <linux/regmap.h>
117 +
118 +#include "../core.h"
119 +#include "../pinconf.h"
120 +#include "../pinmux.h"
121 +
122 +#define PINCTRL_PINCTRL_DEV KBUILD_MODNAME
123 +#define MTK_RANGE(_a) { .range = (_a), .nranges = ARRAY_SIZE(_a), }
124 +#define PINCTRL_PIN_GROUP(name, id) \
125 + { \
126 + name, \
127 + id##_pins, \
128 + ARRAY_SIZE(id##_pins), \
129 + id##_funcs, \
130 + }
131 +
132 +#define MTK_GPIO_MODE 1
133 +#define MTK_INPUT 0
134 +#define MTK_OUTPUT 1
135 +#define MTK_DISABLE 0
136 +#define MTK_ENABLE 1
137 +
138 +/* Custom pinconf parameters */
139 +#define MTK_PIN_CONFIG_TDSEL (PIN_CONFIG_END + 1)
140 +#define MTK_PIN_CONFIG_RDSEL (PIN_CONFIG_END + 2)
141 +
142 +/* List these attributes which could be modified for the pin */
143 +enum {
144 + PINCTRL_PIN_REG_MODE,
145 + PINCTRL_PIN_REG_DIR,
146 + PINCTRL_PIN_REG_DI,
147 + PINCTRL_PIN_REG_DO,
148 + PINCTRL_PIN_REG_SR,
149 + PINCTRL_PIN_REG_SMT,
150 + PINCTRL_PIN_REG_PD,
151 + PINCTRL_PIN_REG_PU,
152 + PINCTRL_PIN_REG_E4,
153 + PINCTRL_PIN_REG_E8,
154 + PINCTRL_PIN_REG_TDSEL,
155 + PINCTRL_PIN_REG_RDSEL,
156 + PINCTRL_PIN_REG_MAX,
157 +};
158 +
159 +/* struct mtk_pin_field - the structure that holds the information of the field
160 + * used to describe the attribute for the pin
161 + * @offset: the register offset relative to the base address
162 + * @mask: the mask used to filter out the field from the register
163 + * @bitpos: the start bit relative to the register
164 + * @next: the indication that the field would be extended to the
165 + next register
166 + */
167 +struct mtk_pin_field {
168 + u32 offset;
169 + u32 mask;
170 + u8 bitpos;
171 + u8 next;
172 +};
173 +
174 +/* struct mtk_pin_field_calc - the structure that holds the range providing
175 + * the guide used to look up the relevant field
176 + * @s_pin: the start pin within the range
177 + * @e_pin: the end pin within the range
178 + * @s_addr: the start address for the range
179 + * @x_addrs: the address distance between two consecutive registers
180 + * within the range
181 + * @s_bit: the start bit for the first register within the range
182 + * @x_bits: the bit distance between two consecutive pins within
183 + * the range
184 + */
185 +struct mtk_pin_field_calc {
186 + u16 s_pin;
187 + u16 e_pin;
188 + u32 s_addr;
189 + u8 x_addrs;
190 + u8 s_bit;
191 + u8 x_bits;
192 +};
193 +
194 +/* struct mtk_pin_reg_calc - the structure that holds all ranges used to
195 + * determine which register the pin would make use of
196 + * for certain pin attribute.
197 + * @range: the start address for the range
198 + * @nranges: the number of items in the range
199 + */
200 +struct mtk_pin_reg_calc {
201 + const struct mtk_pin_field_calc *range;
202 + unsigned int nranges;
203 +};
204 +
205 +/* struct mtk_pin_soc - the structure that holds SoC-specific data */
206 +struct mtk_pin_soc {
207 + const struct mtk_pin_reg_calc *reg_cal;
208 + const struct pinctrl_pin_desc *pins;
209 + unsigned int npins;
210 + const struct group_desc *grps;
211 + unsigned int ngrps;
212 + const struct function_desc *funcs;
213 + unsigned int nfuncs;
214 +};
215 +
216 +struct mtk_pinctrl {
217 + struct pinctrl_dev *pctrl;
218 + void __iomem *base;
219 + struct device *dev;
220 + struct gpio_chip chip;
221 + const struct mtk_pin_soc *soc;
222 +};
223 +
224 +static const struct mtk_pin_field_calc mt7622_pin_mode_range[] = {
225 + {0, 0, 0x320, 0x10, 16, 4},
226 + {1, 4, 0x3a0, 0x10, 16, 4},
227 + {5, 5, 0x320, 0x10, 0, 4},
228 + {6, 6, 0x300, 0x10, 4, 4},
229 + {7, 7, 0x300, 0x10, 4, 4},
230 + {8, 9, 0x350, 0x10, 20, 4},
231 + {10, 10, 0x300, 0x10, 8, 4},
232 + {11, 11, 0x300, 0x10, 8, 4},
233 + {12, 12, 0x300, 0x10, 8, 4},
234 + {13, 13, 0x300, 0x10, 8, 4},
235 + {14, 15, 0x320, 0x10, 4, 4},
236 + {16, 17, 0x320, 0x10, 20, 4},
237 + {18, 21, 0x310, 0x10, 16, 4},
238 + {22, 22, 0x380, 0x10, 16, 4},
239 + {23, 23, 0x300, 0x10, 24, 4},
240 + {24, 24, 0x300, 0x10, 24, 4},
241 + {25, 25, 0x300, 0x10, 12, 4},
242 + {25, 25, 0x300, 0x10, 12, 4},
243 + {26, 26, 0x300, 0x10, 12, 4},
244 + {27, 27, 0x300, 0x10, 12, 4},
245 + {28, 28, 0x300, 0x10, 12, 4},
246 + {29, 29, 0x300, 0x10, 12, 4},
247 + {30, 30, 0x300, 0x10, 12, 4},
248 + {31, 31, 0x300, 0x10, 12, 4},
249 + {32, 32, 0x300, 0x10, 12, 4},
250 + {33, 33, 0x300, 0x10, 12, 4},
251 + {34, 34, 0x300, 0x10, 12, 4},
252 + {35, 35, 0x300, 0x10, 12, 4},
253 + {36, 36, 0x300, 0x10, 12, 4},
254 + {37, 37, 0x300, 0x10, 20, 4},
255 + {38, 38, 0x300, 0x10, 20, 4},
256 + {39, 39, 0x300, 0x10, 20, 4},
257 + {40, 40, 0x300, 0x10, 20, 4},
258 + {41, 41, 0x300, 0x10, 20, 4},
259 + {42, 42, 0x300, 0x10, 20, 4},
260 + {43, 43, 0x300, 0x10, 20, 4},
261 + {44, 44, 0x300, 0x10, 20, 4},
262 + {45, 46, 0x300, 0x10, 20, 4},
263 + {47, 47, 0x300, 0x10, 20, 4},
264 + {48, 48, 0x300, 0x10, 20, 4},
265 + {49, 49, 0x300, 0x10, 20, 4},
266 + {50, 50, 0x300, 0x10, 20, 4},
267 + {51, 70, 0x330, 0x10, 4, 4},
268 + {71, 71, 0x300, 0x10, 16, 4},
269 + {72, 72, 0x300, 0x10, 16, 4},
270 + {73, 76, 0x310, 0x10, 0, 4},
271 + {77, 77, 0x320, 0x10, 28, 4},
272 + {78, 78, 0x320, 0x10, 12, 4},
273 + {79, 82, 0x3a0, 0x10, 0, 4},
274 + {83, 83, 0x350, 0x10, 28, 4},
275 + {84, 84, 0x330, 0x10, 0, 4},
276 + {85, 90, 0x360, 0x10, 4, 4},
277 + {91, 94, 0x390, 0x10, 16, 4},
278 + {95, 97, 0x380, 0x10, 20, 4},
279 + {98, 101, 0x390, 0x10, 0, 4},
280 + {102, 102, 0x360, 0x10, 0, 4},
281 +};
282 +
283 +static const struct mtk_pin_field_calc mt7622_pin_dir_range[] = {
284 + {0, 102, 0x0, 0x10, 0, 1},
285 +};
286 +
287 +static const struct mtk_pin_field_calc mt7622_pin_di_range[] = {
288 + {0, 102, 0x200, 0x10, 0, 1},
289 +};
290 +
291 +static const struct mtk_pin_field_calc mt7622_pin_do_range[] = {
292 + {0, 102, 0x100, 0x10, 0, 1},
293 +};
294 +
295 +static const struct mtk_pin_field_calc mt7622_pin_sr_range[] = {
296 + {0, 31, 0x910, 0x10, 0, 1},
297 + {32, 50, 0xa10, 0x10, 0, 1},
298 + {51, 70, 0x810, 0x10, 0, 1},
299 + {71, 72, 0xb10, 0x10, 0, 1},
300 + {73, 86, 0xb10, 0x10, 4, 1},
301 + {87, 90, 0xc10, 0x10, 0, 1},
302 + {91, 102, 0xb10, 0x10, 18, 1},
303 +};
304 +
305 +static const struct mtk_pin_field_calc mt7622_pin_smt_range[] = {
306 + {0, 31, 0x920, 0x10, 0, 1},
307 + {32, 50, 0xa20, 0x10, 0, 1},
308 + {51, 70, 0x820, 0x10, 0, 1},
309 + {71, 72, 0xb20, 0x10, 0, 1},
310 + {73, 86, 0xb20, 0x10, 4, 1},
311 + {87, 90, 0xc20, 0x10, 0, 1},
312 + {91, 102, 0xb20, 0x10, 18, 1},
313 +};
314 +
315 +static const struct mtk_pin_field_calc mt7622_pin_pu_range[] = {
316 + {0, 31, 0x930, 0x10, 0, 1},
317 + {32, 50, 0xa30, 0x10, 0, 1},
318 + {51, 70, 0x830, 0x10, 0, 1},
319 + {71, 72, 0xb30, 0x10, 0, 1},
320 + {73, 86, 0xb30, 0x10, 4, 1},
321 + {87, 90, 0xc30, 0x10, 0, 1},
322 + {91, 102, 0xb30, 0x10, 18, 1},
323 +};
324 +
325 +static const struct mtk_pin_field_calc mt7622_pin_pd_range[] = {
326 + {0, 31, 0x940, 0x10, 0, 1},
327 + {32, 50, 0xa40, 0x10, 0, 1},
328 + {51, 70, 0x840, 0x10, 0, 1},
329 + {71, 72, 0xb40, 0x10, 0, 1},
330 + {73, 86, 0xb40, 0x10, 4, 1},
331 + {87, 90, 0xc40, 0x10, 0, 1},
332 + {91, 102, 0xb40, 0x10, 18, 1},
333 +};
334 +
335 +static const struct mtk_pin_field_calc mt7622_pin_e4_range[] = {
336 + {0, 31, 0x960, 0x10, 0, 1},
337 + {32, 50, 0xa60, 0x10, 0, 1},
338 + {51, 70, 0x860, 0x10, 0, 1},
339 + {71, 72, 0xb60, 0x10, 0, 1},
340 + {73, 86, 0xb60, 0x10, 4, 1},
341 + {87, 90, 0xc60, 0x10, 0, 1},
342 + {91, 102, 0xb60, 0x10, 18, 1},
343 +};
344 +
345 +static const struct mtk_pin_field_calc mt7622_pin_e8_range[] = {
346 + {0, 31, 0x970, 0x10, 0, 1},
347 + {32, 50, 0xa70, 0x10, 0, 1},
348 + {51, 70, 0x870, 0x10, 0, 1},
349 + {71, 72, 0xb70, 0x10, 0, 1},
350 + {73, 86, 0xb70, 0x10, 4, 1},
351 + {87, 90, 0xc70, 0x10, 0, 1},
352 + {91, 102, 0xb70, 0x10, 18, 1},
353 +};
354 +
355 +static const struct mtk_pin_field_calc mt7622_pin_tdsel_range[] = {
356 + {0, 31, 0x980, 0x4, 0, 4},
357 + {32, 50, 0xa80, 0x4, 0, 4},
358 + {51, 70, 0x880, 0x4, 0, 4},
359 + {71, 72, 0xb80, 0x4, 0, 4},
360 + {73, 86, 0xb80, 0x4, 16, 4},
361 + {87, 90, 0xc80, 0x4, 0, 4},
362 + {91, 102, 0xb88, 0x4, 8, 4},
363 +};
364 +
365 +static const struct mtk_pin_field_calc mt7622_pin_rdsel_range[] = {
366 + {0, 31, 0x990, 0x4, 0, 6},
367 + {32, 50, 0xa90, 0x4, 0, 6},
368 + {51, 58, 0x890, 0x4, 0, 6},
369 + {59, 60, 0x894, 0x4, 28, 6},
370 + {61, 62, 0x894, 0x4, 16, 6},
371 + {63, 66, 0x898, 0x4, 8, 6},
372 + {67, 68, 0x89c, 0x4, 12, 6},
373 + {69, 70, 0x89c, 0x4, 0, 6},
374 + {71, 72, 0xb90, 0x4, 0, 6},
375 + {73, 86, 0xb90, 0x4, 24, 6},
376 + {87, 90, 0xc90, 0x4, 0, 6},
377 + {91, 102, 0xb9c, 0x4, 12, 6},
378 +};
379 +
380 +static const struct mtk_pin_reg_calc mt7622_reg_cals[PINCTRL_PIN_REG_MAX] = {
381 + [PINCTRL_PIN_REG_MODE] = MTK_RANGE(mt7622_pin_mode_range),
382 + [PINCTRL_PIN_REG_DIR] = MTK_RANGE(mt7622_pin_dir_range),
383 + [PINCTRL_PIN_REG_DI] = MTK_RANGE(mt7622_pin_di_range),
384 + [PINCTRL_PIN_REG_DO] = MTK_RANGE(mt7622_pin_do_range),
385 + [PINCTRL_PIN_REG_SR] = MTK_RANGE(mt7622_pin_sr_range),
386 + [PINCTRL_PIN_REG_SMT] = MTK_RANGE(mt7622_pin_smt_range),
387 + [PINCTRL_PIN_REG_PU] = MTK_RANGE(mt7622_pin_pu_range),
388 + [PINCTRL_PIN_REG_PD] = MTK_RANGE(mt7622_pin_pd_range),
389 + [PINCTRL_PIN_REG_E4] = MTK_RANGE(mt7622_pin_e4_range),
390 + [PINCTRL_PIN_REG_E8] = MTK_RANGE(mt7622_pin_e8_range),
391 + [PINCTRL_PIN_REG_TDSEL] = MTK_RANGE(mt7622_pin_tdsel_range),
392 + [PINCTRL_PIN_REG_RDSEL] = MTK_RANGE(mt7622_pin_rdsel_range),
393 +};
394 +
395 +static const struct pinctrl_pin_desc mt7622_pins[] = {
396 + PINCTRL_PIN(0, "GPIO_A"),
397 + PINCTRL_PIN(1, "I2S1_IN"),
398 + PINCTRL_PIN(2, "I2S1_OUT"),
399 + PINCTRL_PIN(3, "I2S_BCLK"),
400 + PINCTRL_PIN(4, "I2S_WS"),
401 + PINCTRL_PIN(5, "I2S_MCLK"),
402 + PINCTRL_PIN(6, "TXD0"),
403 + PINCTRL_PIN(7, "RXD0"),
404 + PINCTRL_PIN(8, "SPI_WP"),
405 + PINCTRL_PIN(9, "SPI_HOLD"),
406 + PINCTRL_PIN(10, "SPI_CLK"),
407 + PINCTRL_PIN(11, "SPI_MOSI"),
408 + PINCTRL_PIN(12, "SPI_MISO"),
409 + PINCTRL_PIN(13, "SPI_CS"),
410 + PINCTRL_PIN(14, "I2C_SDA"),
411 + PINCTRL_PIN(15, "I2C_SCL"),
412 + PINCTRL_PIN(16, "I2S2_IN"),
413 + PINCTRL_PIN(17, "I2S3_IN"),
414 + PINCTRL_PIN(18, "I2S4_IN"),
415 + PINCTRL_PIN(19, "I2S2_OUT"),
416 + PINCTRL_PIN(20, "I2S3_OUT"),
417 + PINCTRL_PIN(21, "I2S4_OUT"),
418 + PINCTRL_PIN(22, "GPIO_B"),
419 + PINCTRL_PIN(23, "MDC"),
420 + PINCTRL_PIN(24, "MDIO"),
421 + PINCTRL_PIN(25, "G2_TXD0"),
422 + PINCTRL_PIN(26, "G2_TXD1"),
423 + PINCTRL_PIN(27, "G2_TXD2"),
424 + PINCTRL_PIN(28, "G2_TXD3"),
425 + PINCTRL_PIN(29, "G2_TXEN"),
426 + PINCTRL_PIN(30, "G2_TXC"),
427 + PINCTRL_PIN(31, "G2_RXD0"),
428 + PINCTRL_PIN(32, "G2_RXD1"),
429 + PINCTRL_PIN(33, "G2_RXD2"),
430 + PINCTRL_PIN(34, "G2_RXD3"),
431 + PINCTRL_PIN(35, "G2_RXDV"),
432 + PINCTRL_PIN(36, "G2_RXC"),
433 + PINCTRL_PIN(37, "NCEB"),
434 + PINCTRL_PIN(38, "NWEB"),
435 + PINCTRL_PIN(39, "NREB"),
436 + PINCTRL_PIN(40, "NDL4"),
437 + PINCTRL_PIN(41, "NDL5"),
438 + PINCTRL_PIN(42, "NDL6"),
439 + PINCTRL_PIN(43, "NDL7"),
440 + PINCTRL_PIN(44, "NRB"),
441 + PINCTRL_PIN(45, "NCLE"),
442 + PINCTRL_PIN(46, "NALE"),
443 + PINCTRL_PIN(47, "NDL0"),
444 + PINCTRL_PIN(48, "NDL1"),
445 + PINCTRL_PIN(49, "NDL2"),
446 + PINCTRL_PIN(50, "NDL3"),
447 + PINCTRL_PIN(51, "MDI_TP_P0"),
448 + PINCTRL_PIN(52, "MDI_TN_P0"),
449 + PINCTRL_PIN(53, "MDI_RP_P0"),
450 + PINCTRL_PIN(54, "MDI_RN_P0"),
451 + PINCTRL_PIN(55, "MDI_TP_P1"),
452 + PINCTRL_PIN(56, "MDI_TN_P1"),
453 + PINCTRL_PIN(57, "MDI_RP_P1"),
454 + PINCTRL_PIN(58, "MDI_RN_P1"),
455 + PINCTRL_PIN(59, "MDI_RP_P2"),
456 + PINCTRL_PIN(60, "MDI_RN_P2"),
457 + PINCTRL_PIN(61, "MDI_TP_P2"),
458 + PINCTRL_PIN(62, "MDI_TN_P2"),
459 + PINCTRL_PIN(63, "MDI_TP_P3"),
460 + PINCTRL_PIN(64, "MDI_TN_P3"),
461 + PINCTRL_PIN(65, "MDI_RP_P3"),
462 + PINCTRL_PIN(66, "MDI_RN_P3"),
463 + PINCTRL_PIN(67, "MDI_RP_P4"),
464 + PINCTRL_PIN(68, "MDI_RN_P4"),
465 + PINCTRL_PIN(69, "MDI_TP_P4"),
466 + PINCTRL_PIN(70, "MDI_TN_P4"),
467 + PINCTRL_PIN(71, "PMIC_SCL"),
468 + PINCTRL_PIN(72, "PMIC_SDA"),
469 + PINCTRL_PIN(73, "SPIC1_CLK"),
470 + PINCTRL_PIN(74, "SPIC1_MOSI"),
471 + PINCTRL_PIN(75, "SPIC1_MISO"),
472 + PINCTRL_PIN(76, "SPIC1_CS"),
473 + PINCTRL_PIN(77, "GPIO_D"),
474 + PINCTRL_PIN(78, "WATCHDOG"),
475 + PINCTRL_PIN(79, "RTS3_N"),
476 + PINCTRL_PIN(80, "CTS3_N"),
477 + PINCTRL_PIN(81, "TXD3"),
478 + PINCTRL_PIN(82, "RXD3"),
479 + PINCTRL_PIN(83, "PERST0_N"),
480 + PINCTRL_PIN(84, "PERST1_N"),
481 + PINCTRL_PIN(85, "WLED_N"),
482 + PINCTRL_PIN(86, "EPHY_LED0_N"),
483 + PINCTRL_PIN(87, "AUXIN0"),
484 + PINCTRL_PIN(88, "AUXIN1"),
485 + PINCTRL_PIN(89, "AUXIN2"),
486 + PINCTRL_PIN(90, "AUXIN3"),
487 + PINCTRL_PIN(91, "TXD4"),
488 + PINCTRL_PIN(92, "RXD4"),
489 + PINCTRL_PIN(93, "RTS4_N"),
490 + PINCTRL_PIN(94, "CTS4_N"),
491 + PINCTRL_PIN(95, "PWM1"),
492 + PINCTRL_PIN(96, "PWM2"),
493 + PINCTRL_PIN(97, "PWM3"),
494 + PINCTRL_PIN(98, "PWM4"),
495 + PINCTRL_PIN(99, "PWM5"),
496 + PINCTRL_PIN(100, "PWM6"),
497 + PINCTRL_PIN(101, "PWM7"),
498 + PINCTRL_PIN(102, "GPIO_E"),
499 +};
500 +
501 +/* List all groups consisting of these pins dedicated to the enablement of
502 + * certain hardware block and the corresponding mode for all of the pins. The
503 + * hardware probably has multiple combinations of these pinouts.
504 + */
505 +
506 +/* EMMC */
507 +static int mt7622_emmc_pins[] = { 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, };
508 +static int mt7622_emmc_funcs[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, };
509 +
510 +static int mt7622_emmc_rst_pins[] = { 37, };
511 +static int mt7622_emmc_rst_funcs[] = { 1, };
512 +
513 +/* LED for EPHY */
514 +static int mt7622_ephy_leds_pins[] = { 86, 91, 92, 93, 94, };
515 +static int mt7622_ephy_leds_funcs[] = { 0, 0, 0, 0, 0, };
516 +static int mt7622_ephy0_led_pins[] = { 86, };
517 +static int mt7622_ephy0_led_funcs[] = { 0, };
518 +static int mt7622_ephy1_led_pins[] = { 91, };
519 +static int mt7622_ephy1_led_funcs[] = { 2, };
520 +static int mt7622_ephy2_led_pins[] = { 92, };
521 +static int mt7622_ephy2_led_funcs[] = { 2, };
522 +static int mt7622_ephy3_led_pins[] = { 93, };
523 +static int mt7622_ephy3_led_funcs[] = { 2, };
524 +static int mt7622_ephy4_led_pins[] = { 94, };
525 +static int mt7622_ephy4_led_funcs[] = { 2, };
526 +
527 +/* Embedded Switch */
528 +static int mt7622_esw_pins[] = { 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
529 + 62, 63, 64, 65, 66, 67, 68, 69, 70, };
530 +static int mt7622_esw_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
531 + 0, 0, 0, 0, 0, 0, 0, 0, 0, };
532 +static int mt7622_esw_p0_p1_pins[] = { 51, 52, 53, 54, 55, 56, 57, 58, };
533 +static int mt7622_esw_p0_p1_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, };
534 +static int mt7622_esw_p2_p3_p4_pins[] = { 59, 60, 61, 62, 63, 64, 65, 66, 67,
535 + 68, 69, 70, };
536 +static int mt7622_esw_p2_p3_p4_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0,
537 + 0, 0, 0, };
538 +/* RGMII via ESW */
539 +static int mt7622_rgmii_via_esw_pins[] = { 59, 60, 61, 62, 63, 64, 65, 66,
540 + 67, 68, 69, 70, };
541 +static int mt7622_rgmii_via_esw_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
542 + 0, };
543 +
544 +/* RGMII via GMAC1 */
545 +static int mt7622_rgmii_via_gmac1_pins[] = { 59, 60, 61, 62, 63, 64, 65, 66,
546 + 67, 68, 69, 70, };
547 +static int mt7622_rgmii_via_gmac1_funcs[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
548 + 2, };
549 +
550 +/* RGMII via GMAC2 */
551 +static int mt7622_rgmii_via_gmac2_pins[] = { 25, 26, 27, 28, 29, 30, 31, 32,
552 + 33, 34, 35, 36, };
553 +static int mt7622_rgmii_via_gmac2_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
554 + 0, };
555 +
556 +/* I2C */
557 +static int mt7622_i2c0_pins[] = { 14, 15, };
558 +static int mt7622_i2c0_funcs[] = { 0, 0, };
559 +static int mt7622_i2c1_0_pins[] = { 55, 56, };
560 +static int mt7622_i2c1_0_funcs[] = { 0, 0, };
561 +static int mt7622_i2c1_1_pins[] = { 73, 74, };
562 +static int mt7622_i2c1_1_funcs[] = { 3, 3, };
563 +static int mt7622_i2c1_2_pins[] = { 87, 88, };
564 +static int mt7622_i2c1_2_funcs[] = { 0, 0, };
565 +static int mt7622_i2c2_0_pins[] = { 57, 58, };
566 +static int mt7622_i2c2_0_funcs[] = { 0, 0, };
567 +static int mt7622_i2c2_1_pins[] = { 75, 76, };
568 +static int mt7622_i2c2_1_funcs[] = { 3, 3, };
569 +static int mt7622_i2c2_2_pins[] = { 89, 90, };
570 +static int mt7622_i2c2_2_funcs[] = { 0, 0, };
571 +
572 +/* I2S */
573 +static int mt7622_i2s_in_mclk_bclk_ws_pins[] = { 3, 4, 5, };
574 +static int mt7622_i2s_in_mclk_bclk_ws_funcs[] = { 3, 3, 0, };
575 +static int mt7622_i2s1_in_data_pins[] = { 1, };
576 +static int mt7622_i2s1_in_data_funcs[] = { 0, };
577 +static int mt7622_i2s2_in_data_pins[] = { 16, };
578 +static int mt7622_i2s2_in_data_funcs[] = { 0, };
579 +static int mt7622_i2s3_in_data_pins[] = { 17, };
580 +static int mt7622_i2s3_in_data_funcs[] = { 0, };
581 +static int mt7622_i2s4_in_data_pins[] = { 18, };
582 +static int mt7622_i2s4_in_data_funcs[] = { 0, };
583 +static int mt7622_i2s_out_mclk_bclk_ws_pins[] = { 3, 4, 5, };
584 +static int mt7622_i2s_out_mclk_bclk_ws_funcs[] = { 0, 0, 0, };
585 +static int mt7622_i2s1_out_data_pins[] = { 2, };
586 +static int mt7622_i2s1_out_data_funcs[] = { 0, };
587 +static int mt7622_i2s2_out_data_pins[] = { 19, };
588 +static int mt7622_i2s2_out_data_funcs[] = { 0, };
589 +static int mt7622_i2s3_out_data_pins[] = { 20, };
590 +static int mt7622_i2s3_out_data_funcs[] = { 0, };
591 +static int mt7622_i2s4_out_data_pins[] = { 21, };
592 +static int mt7622_i2s4_out_data_funcs[] = { 0, };
593 +
594 +/* IR */
595 +static int mt7622_ir_0_tx_pins[] = { 16, };
596 +static int mt7622_ir_0_tx_funcs[] = { 4, };
597 +static int mt7622_ir_1_tx_pins[] = { 59, };
598 +static int mt7622_ir_1_tx_funcs[] = { 5, };
599 +static int mt7622_ir_2_tx_pins[] = { 99, };
600 +static int mt7622_ir_2_tx_funcs[] = { 3, };
601 +static int mt7622_ir_0_rx_pins[] = { 17, };
602 +static int mt7622_ir_0_rx_funcs[] = { 4, };
603 +static int mt7622_ir_1_rx_pins[] = { 60, };
604 +static int mt7622_ir_1_rx_funcs[] = { 5, };
605 +static int mt7622_ir_2_rx_pins[] = { 100, };
606 +static int mt7622_ir_2_rx_funcs[] = { 3, };
607 +
608 +/* MDIO */
609 +static int mt7622_mdc_mdio_pins[] = { 23, 24, };
610 +static int mt7622_mdc_mdio_funcs[] = { 0, 0, };
611 +
612 +/* PCIE */
613 +static int mt7622_pcie0_0_waken_pins[] = { 14, };
614 +static int mt7622_pcie0_0_waken_funcs[] = { 2, };
615 +static int mt7622_pcie0_0_clkreq_pins[] = { 15, };
616 +static int mt7622_pcie0_0_clkreq_funcs[] = { 2, };
617 +static int mt7622_pcie0_1_waken_pins[] = { 79, };
618 +static int mt7622_pcie0_1_waken_funcs[] = { 4, };
619 +static int mt7622_pcie0_1_clkreq_pins[] = { 80, };
620 +static int mt7622_pcie0_1_clkreq_funcs[] = { 4, };
621 +static int mt7622_pcie1_0_waken_pins[] = { 14, };
622 +static int mt7622_pcie1_0_waken_funcs[] = { 3, };
623 +static int mt7622_pcie1_0_clkreq_pins[] = { 15, };
624 +static int mt7622_pcie1_0_clkreq_funcs[] = { 3, };
625 +
626 +static int mt7622_pcie0_pad_perst_pins[] = { 83, };
627 +static int mt7622_pcie0_pad_perst_funcs[] = { 0, };
628 +static int mt7622_pcie1_pad_perst_pins[] = { 84, };
629 +static int mt7622_pcie1_pad_perst_funcs[] = { 0, };
630 +
631 +/* PMIC bus */
632 +static int mt7622_pmic_bus_pins[] = { 71, 72, };
633 +static int mt7622_pmic_bus_funcs[] = { 0, 0, };
634 +
635 +/* Parallel NAND */
636 +static int mt7622_pnand_pins[] = { 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
637 + 48, 49, 50, };
638 +static int mt7622_pnand_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
639 + 0, };
640 +
641 +/* PWM */
642 +static int mt7622_pwm_ch1_0_pins[] = { 51, };
643 +static int mt7622_pwm_ch1_0_funcs[] = { 3, };
644 +static int mt7622_pwm_ch1_1_pins[] = { 73, };
645 +static int mt7622_pwm_ch1_1_funcs[] = { 4, };
646 +static int mt7622_pwm_ch1_2_pins[] = { 95, };
647 +static int mt7622_pwm_ch1_2_funcs[] = { 0, };
648 +static int mt7622_pwm_ch2_0_pins[] = { 52, };
649 +static int mt7622_pwm_ch2_0_funcs[] = { 3, };
650 +static int mt7622_pwm_ch2_1_pins[] = { 74, };
651 +static int mt7622_pwm_ch2_1_funcs[] = { 4, };
652 +static int mt7622_pwm_ch2_2_pins[] = { 96, };
653 +static int mt7622_pwm_ch2_2_funcs[] = { 0, };
654 +static int mt7622_pwm_ch3_0_pins[] = { 53, };
655 +static int mt7622_pwm_ch3_0_funcs[] = { 3, };
656 +static int mt7622_pwm_ch3_1_pins[] = { 75, };
657 +static int mt7622_pwm_ch3_1_funcs[] = { 4, };
658 +static int mt7622_pwm_ch3_2_pins[] = { 97, };
659 +static int mt7622_pwm_ch3_2_funcs[] = { 0, };
660 +static int mt7622_pwm_ch4_0_pins[] = { 54, };
661 +static int mt7622_pwm_ch4_0_funcs[] = { 3, };
662 +static int mt7622_pwm_ch4_1_pins[] = { 67, };
663 +static int mt7622_pwm_ch4_1_funcs[] = { 3, };
664 +static int mt7622_pwm_ch4_2_pins[] = { 76, };
665 +static int mt7622_pwm_ch4_2_funcs[] = { 4, };
666 +static int mt7622_pwm_ch4_3_pins[] = { 98, };
667 +static int mt7622_pwm_ch4_3_funcs[] = { 0, };
668 +static int mt7622_pwm_ch5_0_pins[] = { 68, };
669 +static int mt7622_pwm_ch5_0_funcs[] = { 3, };
670 +static int mt7622_pwm_ch5_1_pins[] = { 77, };
671 +static int mt7622_pwm_ch5_1_funcs[] = { 4, };
672 +static int mt7622_pwm_ch5_2_pins[] = { 99, };
673 +static int mt7622_pwm_ch5_2_funcs[] = { 0, };
674 +static int mt7622_pwm_ch6_0_pins[] = { 69, };
675 +static int mt7622_pwm_ch6_0_funcs[] = { 3, };
676 +static int mt7622_pwm_ch6_1_pins[] = { 78, };
677 +static int mt7622_pwm_ch6_1_funcs[] = { 4, };
678 +static int mt7622_pwm_ch6_2_pins[] = { 81, };
679 +static int mt7622_pwm_ch6_2_funcs[] = { 4, };
680 +static int mt7622_pwm_ch6_3_pins[] = { 100, };
681 +static int mt7622_pwm_ch6_3_funcs[] = { 0, };
682 +static int mt7622_pwm_ch7_0_pins[] = { 70, };
683 +static int mt7622_pwm_ch7_0_funcs[] = { 3, };
684 +static int mt7622_pwm_ch7_1_pins[] = { 82, };
685 +static int mt7622_pwm_ch7_1_funcs[] = { 4, };
686 +static int mt7622_pwm_ch7_2_pins[] = { 101, };
687 +static int mt7622_pwm_ch7_2_funcs[] = { 0, };
688 +
689 +/* SD */
690 +static int mt7622_sd_0_pins[] = { 16, 17, 18, 19, 20, 21, };
691 +static int mt7622_sd_0_funcs[] = { 2, 2, 2, 2, 2, 2, };
692 +static int mt7622_sd_1_pins[] = { 25, 26, 27, 28, 29, 30, };
693 +static int mt7622_sd_1_funcs[] = { 2, 2, 2, 2, 2, 2, };
694 +
695 +/* Serial NAND */
696 +static int mt7622_snfi_pins[] = { 8, 9, 10, 11, 12, 13, };
697 +static int mt7622_snfi_funcs[] = { 2, 2, 2, 2, 2, 2, };
698 +
699 +/* SPI NOR */
700 +static int mt7622_spi_pins[] = { 8, 9, 10, 11, 12, 13 };
701 +static int mt7622_spi_funcs[] = { 0, 0, 0, 0, 0, 0, };
702 +
703 +/* SPIC */
704 +static int mt7622_spic0_0_pins[] = { 63, 64, 65, 66, };
705 +static int mt7622_spic0_0_funcs[] = { 4, 4, 4, 4, };
706 +static int mt7622_spic0_1_pins[] = { 79, 80, 81, 82, };
707 +static int mt7622_spic0_1_funcs[] = { 3, 3, 3, 3, };
708 +static int mt7622_spic1_0_pins[] = { 67, 68, 69, 70, };
709 +static int mt7622_spic1_0_funcs[] = { 4, 4, 4, 4, };
710 +static int mt7622_spic1_1_pins[] = { 73, 74, 75, 76, };
711 +static int mt7622_spic1_1_funcs[] = { 0, 0, 0, 0, };
712 +static int mt7622_spic2_0_pins[] = { 10, 11, 12, 13, };
713 +static int mt7622_spic2_0_funcs[] = { 0, 0, 0, 0, };
714 +static int mt7622_spic2_0_wp_hold_pins[] = { 8, 9, };
715 +static int mt7622_spic2_0_wp_hold_funcs[] = { 0, 0, };
716 +
717 +/* TDM */
718 +static int mt7622_tdm_0_out_mclk_bclk_ws_pins[] = { 8, 9, 10, };
719 +static int mt7622_tdm_0_out_mclk_bclk_ws_funcs[] = { 3, 3, 3, };
720 +static int mt7622_tdm_0_in_mclk_bclk_ws_pins[] = { 11, 12, 13, };
721 +static int mt7622_tdm_0_in_mclk_bclk_ws_funcs[] = { 3, 3, 3, };
722 +static int mt7622_tdm_0_out_data_pins[] = { 20, };
723 +static int mt7622_tdm_0_out_data_funcs[] = { 3, };
724 +static int mt7622_tdm_0_in_data_pins[] = { 21, };
725 +static int mt7622_tdm_0_in_data_funcs[] = { 3, };
726 +static int mt7622_tdm_1_out_mclk_bclk_ws_pins[] = { 57, 58, 59, };
727 +static int mt7622_tdm_1_out_mclk_bclk_ws_funcs[] = { 3, 3, 3, };
728 +static int mt7622_tdm_1_in_mclk_bclk_ws_pins[] = { 60, 61, 62, };
729 +static int mt7622_tdm_1_in_mclk_bclk_ws_funcs[] = { 3, 3, 3, };
730 +static int mt7622_tdm_1_out_data_pins[] = { 55, };
731 +static int mt7622_tdm_1_out_data_funcs[] = { 3, };
732 +static int mt7622_tdm_1_in_data_pins[] = { 56, };
733 +static int mt7622_tdm_1_in_data_funcs[] = { 3, };
734 +
735 +/* UART */
736 +static int mt7622_uart0_0_tx_rx_pins[] = { 6, 7, };
737 +static int mt7622_uart0_0_tx_rx_funcs[] = { 0, 0, };
738 +static int mt7622_uart1_0_tx_rx_pins[] = { 55, 56, };
739 +static int mt7622_uart1_0_tx_rx_funcs[] = { 2, 2, };
740 +static int mt7622_uart1_0_rts_cts_pins[] = { 57, 58, };
741 +static int mt7622_uart1_0_rts_cts_funcs[] = { 2, 2, };
742 +static int mt7622_uart1_1_tx_rx_pins[] = { 73, 74, };
743 +static int mt7622_uart1_1_tx_rx_funcs[] = { 2, 2, };
744 +static int mt7622_uart1_1_rts_cts_pins[] = { 75, 76, };
745 +static int mt7622_uart1_1_rts_cts_funcs[] = { 2, 2, };
746 +static int mt7622_uart2_0_tx_rx_pins[] = { 3, 4, };
747 +static int mt7622_uart2_0_tx_rx_funcs[] = { 2, 2, };
748 +static int mt7622_uart2_0_rts_cts_pins[] = { 1, 2, };
749 +static int mt7622_uart2_0_rts_cts_funcs[] = { 2, 2, };
750 +static int mt7622_uart2_1_tx_rx_pins[] = { 51, 52, };
751 +static int mt7622_uart2_1_tx_rx_funcs[] = { 0, 0, };
752 +static int mt7622_uart2_1_rts_cts_pins[] = { 53, 54, };
753 +static int mt7622_uart2_1_rts_cts_funcs[] = { 0, 0, };
754 +static int mt7622_uart2_2_tx_rx_pins[] = { 59, 60, };
755 +static int mt7622_uart2_2_tx_rx_funcs[] = { 4, 4, };
756 +static int mt7622_uart2_2_rts_cts_pins[] = { 61, 62, };
757 +static int mt7622_uart2_2_rts_cts_funcs[] = { 4, 4, };
758 +static int mt7622_uart2_3_tx_rx_pins[] = { 95, 96, };
759 +static int mt7622_uart2_3_tx_rx_funcs[] = { 3, 3, };
760 +static int mt7622_uart3_0_tx_rx_pins[] = { 57, 58, };
761 +static int mt7622_uart3_0_tx_rx_funcs[] = { 5, 5, };
762 +static int mt7622_uart3_1_tx_rx_pins[] = { 81, 82, };
763 +static int mt7622_uart3_1_tx_rx_funcs[] = { 0, 0, };
764 +static int mt7622_uart3_1_rts_cts_pins[] = { 79, 80, };
765 +static int mt7622_uart3_1_rts_cts_funcs[] = { 0, 0, };
766 +static int mt7622_uart4_0_tx_rx_pins[] = { 61, 62, };
767 +static int mt7622_uart4_0_tx_rx_funcs[] = { 5, 5, };
768 +static int mt7622_uart4_1_tx_rx_pins[] = { 91, 92, };
769 +static int mt7622_uart4_1_tx_rx_funcs[] = { 0, 0, };
770 +static int mt7622_uart4_1_rts_cts_pins[] = { 93, 94 };
771 +static int mt7622_uart4_1_rts_cts_funcs[] = { 0, 0, };
772 +static int mt7622_uart4_2_tx_rx_pins[] = { 97, 98, };
773 +static int mt7622_uart4_2_tx_rx_funcs[] = { 2, 2, };
774 +static int mt7622_uart4_2_rts_cts_pins[] = { 95, 96 };
775 +static int mt7622_uart4_2_rts_cts_funcs[] = { 2, 2, };
776 +
777 +/* Watchdog */
778 +static int mt7622_watchdog_pins[] = { 78, };
779 +static int mt7622_watchdog_funcs[] = { 0, };
780 +
781 +/* WLAN LED */
782 +static int mt7622_wled_pins[] = { 85, };
783 +static int mt7622_wled_funcs[] = { 0, };
784 +
785 +static const struct group_desc mt7622_groups[] = {
786 + PINCTRL_PIN_GROUP("emmc", mt7622_emmc),
787 + PINCTRL_PIN_GROUP("emmc_rst", mt7622_emmc_rst),
788 + PINCTRL_PIN_GROUP("ephy_leds", mt7622_ephy_leds),
789 + PINCTRL_PIN_GROUP("ephy0_led", mt7622_ephy0_led),
790 + PINCTRL_PIN_GROUP("ephy1_led", mt7622_ephy1_led),
791 + PINCTRL_PIN_GROUP("ephy2_led", mt7622_ephy2_led),
792 + PINCTRL_PIN_GROUP("ephy3_led", mt7622_ephy3_led),
793 + PINCTRL_PIN_GROUP("ephy4_led", mt7622_ephy4_led),
794 + PINCTRL_PIN_GROUP("esw", mt7622_esw),
795 + PINCTRL_PIN_GROUP("esw_p0_p1", mt7622_esw_p0_p1),
796 + PINCTRL_PIN_GROUP("esw_p2_p3_p4", mt7622_esw_p2_p3_p4),
797 + PINCTRL_PIN_GROUP("rgmii_via_esw", mt7622_rgmii_via_esw),
798 + PINCTRL_PIN_GROUP("rgmii_via_gmac1", mt7622_rgmii_via_gmac1),
799 + PINCTRL_PIN_GROUP("rgmii_via_gmac2", mt7622_rgmii_via_gmac2),
800 + PINCTRL_PIN_GROUP("i2c0", mt7622_i2c0),
801 + PINCTRL_PIN_GROUP("i2c1_0", mt7622_i2c1_0),
802 + PINCTRL_PIN_GROUP("i2c1_1", mt7622_i2c1_1),
803 + PINCTRL_PIN_GROUP("i2c1_2", mt7622_i2c1_2),
804 + PINCTRL_PIN_GROUP("i2c2_0", mt7622_i2c2_0),
805 + PINCTRL_PIN_GROUP("i2c2_1", mt7622_i2c2_1),
806 + PINCTRL_PIN_GROUP("i2c2_2", mt7622_i2c2_2),
807 + PINCTRL_PIN_GROUP("i2s_out_mclk_bclk_ws", mt7622_i2s_out_mclk_bclk_ws),
808 + PINCTRL_PIN_GROUP("i2s_in_mclk_bclk_ws", mt7622_i2s_in_mclk_bclk_ws),
809 + PINCTRL_PIN_GROUP("i2s1_in_data", mt7622_i2s1_in_data),
810 + PINCTRL_PIN_GROUP("i2s2_in_data", mt7622_i2s2_in_data),
811 + PINCTRL_PIN_GROUP("i2s3_in_data", mt7622_i2s3_in_data),
812 + PINCTRL_PIN_GROUP("i2s4_in_data", mt7622_i2s4_in_data),
813 + PINCTRL_PIN_GROUP("i2s1_out_data", mt7622_i2s1_out_data),
814 + PINCTRL_PIN_GROUP("i2s2_out_data", mt7622_i2s2_out_data),
815 + PINCTRL_PIN_GROUP("i2s3_out_data", mt7622_i2s3_out_data),
816 + PINCTRL_PIN_GROUP("i2s4_out_data", mt7622_i2s4_out_data),
817 + PINCTRL_PIN_GROUP("ir_0_tx", mt7622_ir_0_tx),
818 + PINCTRL_PIN_GROUP("ir_1_tx", mt7622_ir_1_tx),
819 + PINCTRL_PIN_GROUP("ir_2_tx", mt7622_ir_2_tx),
820 + PINCTRL_PIN_GROUP("ir_0_rx", mt7622_ir_0_rx),
821 + PINCTRL_PIN_GROUP("ir_1_rx", mt7622_ir_1_rx),
822 + PINCTRL_PIN_GROUP("ir_2_rx", mt7622_ir_2_rx),
823 + PINCTRL_PIN_GROUP("mdc_mdio", mt7622_mdc_mdio),
824 + PINCTRL_PIN_GROUP("pcie0_0_waken", mt7622_pcie0_0_waken),
825 + PINCTRL_PIN_GROUP("pcie0_0_clkreq", mt7622_pcie0_0_clkreq),
826 + PINCTRL_PIN_GROUP("pcie0_1_waken", mt7622_pcie0_1_waken),
827 + PINCTRL_PIN_GROUP("pcie0_1_clkreq", mt7622_pcie0_1_clkreq),
828 + PINCTRL_PIN_GROUP("pcie1_0_waken", mt7622_pcie1_0_waken),
829 + PINCTRL_PIN_GROUP("pcie1_0_clkreq", mt7622_pcie1_0_clkreq),
830 + PINCTRL_PIN_GROUP("pcie0_pad_perst", mt7622_pcie0_pad_perst),
831 + PINCTRL_PIN_GROUP("pcie1_pad_perst", mt7622_pcie1_pad_perst),
832 + PINCTRL_PIN_GROUP("par_nand", mt7622_pnand),
833 + PINCTRL_PIN_GROUP("pmic_bus", mt7622_pmic_bus),
834 + PINCTRL_PIN_GROUP("pwm_ch1_0", mt7622_pwm_ch1_0),
835 + PINCTRL_PIN_GROUP("pwm_ch1_1", mt7622_pwm_ch1_1),
836 + PINCTRL_PIN_GROUP("pwm_ch1_2", mt7622_pwm_ch1_2),
837 + PINCTRL_PIN_GROUP("pwm_ch2_0", mt7622_pwm_ch2_0),
838 + PINCTRL_PIN_GROUP("pwm_ch2_1", mt7622_pwm_ch2_1),
839 + PINCTRL_PIN_GROUP("pwm_ch2_2", mt7622_pwm_ch2_2),
840 + PINCTRL_PIN_GROUP("pwm_ch3_0", mt7622_pwm_ch3_0),
841 + PINCTRL_PIN_GROUP("pwm_ch3_1", mt7622_pwm_ch3_1),
842 + PINCTRL_PIN_GROUP("pwm_ch3_2", mt7622_pwm_ch3_2),
843 + PINCTRL_PIN_GROUP("pwm_ch4_0", mt7622_pwm_ch4_0),
844 + PINCTRL_PIN_GROUP("pwm_ch4_1", mt7622_pwm_ch4_1),
845 + PINCTRL_PIN_GROUP("pwm_ch4_2", mt7622_pwm_ch4_2),
846 + PINCTRL_PIN_GROUP("pwm_ch4_3", mt7622_pwm_ch4_3),
847 + PINCTRL_PIN_GROUP("pwm_ch5_0", mt7622_pwm_ch5_0),
848 + PINCTRL_PIN_GROUP("pwm_ch5_1", mt7622_pwm_ch5_1),
849 + PINCTRL_PIN_GROUP("pwm_ch5_2", mt7622_pwm_ch5_2),
850 + PINCTRL_PIN_GROUP("pwm_ch6_0", mt7622_pwm_ch6_0),
851 + PINCTRL_PIN_GROUP("pwm_ch6_1", mt7622_pwm_ch6_1),
852 + PINCTRL_PIN_GROUP("pwm_ch6_2", mt7622_pwm_ch6_2),
853 + PINCTRL_PIN_GROUP("pwm_ch6_3", mt7622_pwm_ch6_3),
854 + PINCTRL_PIN_GROUP("pwm_ch7_0", mt7622_pwm_ch7_0),
855 + PINCTRL_PIN_GROUP("pwm_ch7_1", mt7622_pwm_ch7_1),
856 + PINCTRL_PIN_GROUP("pwm_ch7_2", mt7622_pwm_ch7_2),
857 + PINCTRL_PIN_GROUP("sd_0", mt7622_sd_0),
858 + PINCTRL_PIN_GROUP("sd_1", mt7622_sd_1),
859 + PINCTRL_PIN_GROUP("snfi", mt7622_snfi),
860 + PINCTRL_PIN_GROUP("spi_nor", mt7622_spi),
861 + PINCTRL_PIN_GROUP("spic0_0", mt7622_spic0_0),
862 + PINCTRL_PIN_GROUP("spic0_1", mt7622_spic0_1),
863 + PINCTRL_PIN_GROUP("spic1_0", mt7622_spic1_0),
864 + PINCTRL_PIN_GROUP("spic1_1", mt7622_spic1_1),
865 + PINCTRL_PIN_GROUP("spic2_0", mt7622_spic2_0),
866 + PINCTRL_PIN_GROUP("spic2_0_wp_hold", mt7622_spic2_0_wp_hold),
867 + PINCTRL_PIN_GROUP("tdm_0_out_mclk_bclk_ws",
868 + mt7622_tdm_0_out_mclk_bclk_ws),
869 + PINCTRL_PIN_GROUP("tdm_0_in_mclk_bclk_ws",
870 + mt7622_tdm_0_in_mclk_bclk_ws),
871 + PINCTRL_PIN_GROUP("tdm_0_out_data", mt7622_tdm_0_out_data),
872 + PINCTRL_PIN_GROUP("tdm_0_in_data", mt7622_tdm_0_in_data),
873 + PINCTRL_PIN_GROUP("tdm_1_out_mclk_bclk_ws",
874 + mt7622_tdm_1_out_mclk_bclk_ws),
875 + PINCTRL_PIN_GROUP("tdm_1_in_mclk_bclk_ws",
876 + mt7622_tdm_1_in_mclk_bclk_ws),
877 + PINCTRL_PIN_GROUP("tdm_1_out_data", mt7622_tdm_1_out_data),
878 + PINCTRL_PIN_GROUP("tdm_1_in_data", mt7622_tdm_1_in_data),
879 + PINCTRL_PIN_GROUP("uart0_0_tx_rx", mt7622_uart0_0_tx_rx),
880 + PINCTRL_PIN_GROUP("uart1_0_tx_rx", mt7622_uart1_0_tx_rx),
881 + PINCTRL_PIN_GROUP("uart1_0_rts_cts", mt7622_uart1_0_rts_cts),
882 + PINCTRL_PIN_GROUP("uart1_1_tx_rx", mt7622_uart1_1_tx_rx),
883 + PINCTRL_PIN_GROUP("uart1_1_rts_cts", mt7622_uart1_1_rts_cts),
884 + PINCTRL_PIN_GROUP("uart2_0_tx_rx", mt7622_uart2_0_tx_rx),
885 + PINCTRL_PIN_GROUP("uart2_0_rts_cts", mt7622_uart2_0_rts_cts),
886 + PINCTRL_PIN_GROUP("uart2_1_tx_rx", mt7622_uart2_1_tx_rx),
887 + PINCTRL_PIN_GROUP("uart2_1_rts_cts", mt7622_uart2_1_rts_cts),
888 + PINCTRL_PIN_GROUP("uart2_2_tx_rx", mt7622_uart2_2_tx_rx),
889 + PINCTRL_PIN_GROUP("uart2_2_rts_cts", mt7622_uart2_2_rts_cts),
890 + PINCTRL_PIN_GROUP("uart2_3_tx_rx", mt7622_uart2_3_tx_rx),
891 + PINCTRL_PIN_GROUP("uart3_0_tx_rx", mt7622_uart3_0_tx_rx),
892 + PINCTRL_PIN_GROUP("uart3_1_tx_rx", mt7622_uart3_1_tx_rx),
893 + PINCTRL_PIN_GROUP("uart3_1_rts_cts", mt7622_uart3_1_rts_cts),
894 + PINCTRL_PIN_GROUP("uart4_0_tx_rx", mt7622_uart4_0_tx_rx),
895 + PINCTRL_PIN_GROUP("uart4_1_tx_rx", mt7622_uart4_1_tx_rx),
896 + PINCTRL_PIN_GROUP("uart4_1_rts_cts", mt7622_uart4_1_rts_cts),
897 + PINCTRL_PIN_GROUP("uart4_2_tx_rx", mt7622_uart4_2_tx_rx),
898 + PINCTRL_PIN_GROUP("uart4_2_rts_cts", mt7622_uart4_2_rts_cts),
899 + PINCTRL_PIN_GROUP("watchdog", mt7622_watchdog),
900 + PINCTRL_PIN_GROUP("wled", mt7622_wled),
901 +};
902 +
903 +/* Joint those groups owning the same capability in user point of view which
904 + * allows that people tend to use through the device tree.
905 + */
906 +static const char *mt7622_emmc_groups[] = { "emmc", "emmc_rst", };
907 +static const char *mt7622_ethernet_groups[] = { "esw", "esw_p0_p1",
908 + "esw_p2_p3_p4", "mdc_mdio",
909 + "rgmii_via_gmac1",
910 + "rgmii_via_gmac2",
911 + "rgmii_via_esw", };
912 +static const char *mt7622_i2c_groups[] = { "i2c0", "i2c1_0", "i2c1_1",
913 + "i2c1_2", "i2c2_0", "i2c2_1",
914 + "i2c2_2", };
915 +static const char *mt7622_i2s_groups[] = { "i2s_out_mclk_bclk_ws",
916 + "i2s_in_mclk_bclk_ws",
917 + "i2s1_in_data", "i2s2_in_data",
918 + "i2s3_in_data", "i2s4_in_data",
919 + "i2s1_out_data", "i2s2_out_data",
920 + "i2s3_out_data", "i2s4_out_data", };
921 +static const char *mt7622_ir_groups[] = { "ir_0_tx", "ir_1_tx", "ir_2_tx",
922 + "ir_0_rx", "ir_1_rx", "ir_2_rx"};
923 +static const char *mt7622_led_groups[] = { "ephy_leds", "ephy0_led",
924 + "ephy1_led", "ephy2_led",
925 + "ephy3_led", "ephy4_led",
926 + "wled", };
927 +static const char *mt7622_flash_groups[] = { "par_nand", "snfi", "spi_nor"};
928 +static const char *mt7622_pcie_groups[] = { "pcie0_0_waken", "pcie0_0_clkreq",
929 + "pcie0_1_waken", "pcie0_1_clkreq",
930 + "pcie1_0_waken", "pcie1_0_clkreq",
931 + "pcie0_pad_perst",
932 + "pcie1_pad_perst", };
933 +static const char *mt7622_pmic_bus_groups[] = { "pmic_bus", };
934 +static const char *mt7622_pwm_groups[] = { "pwm_ch1_0", "pwm_ch1_1",
935 + "pwm_ch1_2", "pwm_ch2_0",
936 + "pwm_ch2_1", "pwm_ch2_2",
937 + "pwm_ch3_0", "pwm_ch3_1",
938 + "pwm_ch3_2", "pwm_ch4_0",
939 + "pwm_ch4_1", "pwm_ch4_2",
940 + "pwm_ch4_3", "pwm_ch5_0",
941 + "pwm_ch5_1", "pwm_ch5_2",
942 + "pwm_ch6_0", "pwm_ch6_1",
943 + "pwm_ch6_2", "pwm_ch6_3",
944 + "pwm_ch7_0", "pwm_ch7_1",
945 + "pwm_ch7_2", };
946 +static const char *mt7622_sd_groups[] = { "sd_0", "sd_1", };
947 +static const char *mt7622_spic_groups[] = { "spic0_0", "spic0_1", "spic1_0",
948 + "spic1_1", "spic2_0",
949 + "spic2_0_wp_hold", };
950 +static const char *mt7622_tdm_groups[] = { "tdm_0_out_mclk_bclk_ws",
951 + "tdm_0_in_mclk_bclk_ws",
952 + "tdm_0_out_data",
953 + "tdm_0_in_data",
954 + "tdm_1_out_mclk_bclk_ws",
955 + "tdm_1_in_mclk_bclk_ws",
956 + "tdm_1_out_data",
957 + "tdm_1_in_data", };
958 +
959 +static const char *mt7622_uart_groups[] = { "uart0_0_tx_rx",
960 + "uart1_0_tx_rx", "uart1_0_rts_cts",
961 + "uart1_1_tx_rx", "uart1_1_rts_cts",
962 + "uart2_0_tx_rx", "uart2_0_rts_cts",
963 + "uart2_1_tx_rx", "uart2_1_rts_cts",
964 + "uart2_2_tx_rx", "uart2_2_rts_cts",
965 + "uart2_3_tx_rx",
966 + "uart3_0_tx_rx",
967 + "uart3_1_tx_rx", "uart3_1_rts_cts",
968 + "uart4_0_tx_rx",
969 + "uart4_1_tx_rx", "uart4_1_rts_cts",
970 + "uart4_2_tx_rx",
971 + "uart4_2_rts_cts",};
972 +static const char *mt7622_wdt_groups[] = { "watchdog", };
973 +
974 +static const struct function_desc mt7622_functions[] = {
975 + {"emmc", mt7622_emmc_groups, ARRAY_SIZE(mt7622_emmc_groups)},
976 + {"eth", mt7622_ethernet_groups, ARRAY_SIZE(mt7622_ethernet_groups)},
977 + {"i2c", mt7622_i2c_groups, ARRAY_SIZE(mt7622_i2c_groups)},
978 + {"i2s", mt7622_i2s_groups, ARRAY_SIZE(mt7622_i2s_groups)},
979 + {"ir", mt7622_ir_groups, ARRAY_SIZE(mt7622_ir_groups)},
980 + {"led", mt7622_led_groups, ARRAY_SIZE(mt7622_led_groups)},
981 + {"flash", mt7622_flash_groups, ARRAY_SIZE(mt7622_flash_groups)},
982 + {"pcie", mt7622_pcie_groups, ARRAY_SIZE(mt7622_pcie_groups)},
983 + {"pmic", mt7622_pmic_bus_groups, ARRAY_SIZE(mt7622_pmic_bus_groups)},
984 + {"pwm", mt7622_pwm_groups, ARRAY_SIZE(mt7622_pwm_groups)},
985 + {"sd", mt7622_sd_groups, ARRAY_SIZE(mt7622_sd_groups)},
986 + {"spi", mt7622_spic_groups, ARRAY_SIZE(mt7622_spic_groups)},
987 + {"tdm", mt7622_tdm_groups, ARRAY_SIZE(mt7622_tdm_groups)},
988 + {"uart", mt7622_uart_groups, ARRAY_SIZE(mt7622_uart_groups)},
989 + {"watchdog", mt7622_wdt_groups, ARRAY_SIZE(mt7622_wdt_groups)},
990 +};
991 +
992 +static const struct pinconf_generic_params mtk_custom_bindings[] = {
993 + {"mediatek,tdsel", MTK_PIN_CONFIG_TDSEL, 0},
994 + {"mediatek,rdsel", MTK_PIN_CONFIG_RDSEL, 0},
995 +};
996 +
997 +#ifdef CONFIG_DEBUG_FS
998 +static const struct pin_config_item mtk_conf_items[] = {
999 + PCONFDUMP(MTK_PIN_CONFIG_TDSEL, "tdsel", NULL, true),
1000 + PCONFDUMP(MTK_PIN_CONFIG_RDSEL, "rdsel", NULL, true),
1001 +};
1002 +#endif
1003 +
1004 +static const struct mtk_pin_soc mt7622_data = {
1005 + .reg_cal = mt7622_reg_cals,
1006 + .pins = mt7622_pins,
1007 + .npins = ARRAY_SIZE(mt7622_pins),
1008 + .grps = mt7622_groups,
1009 + .ngrps = ARRAY_SIZE(mt7622_groups),
1010 + .funcs = mt7622_functions,
1011 + .nfuncs = ARRAY_SIZE(mt7622_functions),
1012 +};
1013 +
1014 +static void mtk_w32(struct mtk_pinctrl *pctl, u32 reg, u32 val)
1015 +{
1016 + writel_relaxed(val, pctl->base + reg);
1017 +}
1018 +
1019 +static u32 mtk_r32(struct mtk_pinctrl *pctl, u32 reg)
1020 +{
1021 + return readl_relaxed(pctl->base + reg);
1022 +}
1023 +
1024 +static void mtk_rmw(struct mtk_pinctrl *pctl, u32 reg, u32 mask, u32 set)
1025 +{
1026 + u32 val;
1027 +
1028 + val = mtk_r32(pctl, reg);
1029 + val &= ~mask;
1030 + val |= set;
1031 + mtk_w32(pctl, reg, val);
1032 +}
1033 +
1034 +static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw, int pin,
1035 + const struct mtk_pin_reg_calc *rc,
1036 + struct mtk_pin_field *pfd)
1037 +{
1038 + const struct mtk_pin_field_calc *c, *e;
1039 + u32 bits;
1040 +
1041 + c = rc->range;
1042 + e = c + rc->nranges;
1043 +
1044 + while (c < e) {
1045 + if (pin >= c->s_pin && pin <= c->e_pin)
1046 + break;
1047 + c++;
1048 + }
1049 +
1050 + if (c >= e) {
1051 + dev_err(hw->dev, "Out of range for pin = %d\n", pin);
1052 + return -EINVAL;
1053 + }
1054 +
1055 + /* Caculated bits as the overall offset the pin is located at */
1056 + bits = c->s_bit + (pin - c->s_pin) * (c->x_bits);
1057 +
1058 + /* Fill pfd from bits and 32-bit register applied is assumed */
1059 + pfd->offset = c->s_addr + c->x_addrs * (bits / 32);
1060 + pfd->bitpos = bits % 32;
1061 + pfd->mask = (1 << c->x_bits) - 1;
1062 +
1063 + /* pfd->next is used for indicating that bit wrapping-around happens
1064 + * which requires the manipulation for bit 0 starting in the next
1065 + * register to form the complete field read/write.
1066 + */
1067 + pfd->next = pfd->bitpos + c->x_bits - 1 > 31 ? c->x_addrs : 0;
1068 +
1069 + return 0;
1070 +}
1071 +
1072 +static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw, int pin,
1073 + int field, struct mtk_pin_field *pfd)
1074 +{
1075 + const struct mtk_pin_reg_calc *rc;
1076 +
1077 + if (field < 0 || field >= PINCTRL_PIN_REG_MAX) {
1078 + dev_err(hw->dev, "Invalid Field %d\n", field);
1079 + return -EINVAL;
1080 + }
1081 +
1082 + if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) {
1083 + rc = &hw->soc->reg_cal[field];
1084 + } else {
1085 + dev_err(hw->dev, "Undefined range for field %d\n", field);
1086 + return -EINVAL;
1087 + }
1088 +
1089 + return mtk_hw_pin_field_lookup(hw, pin, rc, pfd);
1090 +}
1091 +
1092 +static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
1093 +{
1094 + *l = 32 - pf->bitpos;
1095 + *h = get_count_order(pf->mask) - *l;
1096 +}
1097 +
1098 +static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw,
1099 + struct mtk_pin_field *pf, int value)
1100 +{
1101 + int nbits_l, nbits_h;
1102 +
1103 + mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
1104 +
1105 + mtk_rmw(hw, pf->offset, pf->mask << pf->bitpos,
1106 + (value & pf->mask) << pf->bitpos);
1107 +
1108 + mtk_rmw(hw, pf->offset + pf->next, BIT(nbits_h) - 1,
1109 + (value & pf->mask) >> nbits_l);
1110 +}
1111 +
1112 +static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw,
1113 + struct mtk_pin_field *pf, int *value)
1114 +{
1115 + int nbits_l, nbits_h, h, l;
1116 +
1117 + mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
1118 +
1119 + l = (mtk_r32(hw, pf->offset) >> pf->bitpos) & (BIT(nbits_l) - 1);
1120 + h = (mtk_r32(hw, pf->offset + pf->next)) & (BIT(nbits_h) - 1);
1121 +
1122 + *value = (h << nbits_l) | l;
1123 +}
1124 +
1125 +static int mtk_hw_set_value(struct mtk_pinctrl *hw, int pin, int field,
1126 + int value)
1127 +{
1128 + struct mtk_pin_field pf;
1129 + int err;
1130 +
1131 + err = mtk_hw_pin_field_get(hw, pin, field, &pf);
1132 + if (err)
1133 + return err;
1134 +
1135 + if (!pf.next)
1136 + mtk_rmw(hw, pf.offset, pf.mask << pf.bitpos,
1137 + (value & pf.mask) << pf.bitpos);
1138 + else
1139 + mtk_hw_write_cross_field(hw, &pf, value);
1140 +
1141 + return 0;
1142 +}
1143 +
1144 +static int mtk_hw_get_value(struct mtk_pinctrl *hw, int pin, int field,
1145 + int *value)
1146 +{
1147 + struct mtk_pin_field pf;
1148 + int err;
1149 +
1150 + err = mtk_hw_pin_field_get(hw, pin, field, &pf);
1151 + if (err)
1152 + return err;
1153 +
1154 + if (!pf.next)
1155 + *value = (mtk_r32(hw, pf.offset) >> pf.bitpos) & pf.mask;
1156 + else
1157 + mtk_hw_read_cross_field(hw, &pf, value);
1158 +
1159 + return 0;
1160 +}
1161 +
1162 +static int mtk_pinmux_set_mux(struct pinctrl_dev *pctldev,
1163 + unsigned int selector, unsigned int group)
1164 +{
1165 + struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
1166 + struct function_desc *func;
1167 + struct group_desc *grp;
1168 + int i;
1169 +
1170 + func = pinmux_generic_get_function(pctldev, selector);
1171 + if (!func)
1172 + return -EINVAL;
1173 +
1174 + grp = pinctrl_generic_get_group(pctldev, group);
1175 + if (!grp)
1176 + return -EINVAL;
1177 +
1178 + dev_dbg(pctldev->dev, "enable function %s group %s\n",
1179 + func->name, grp->name);
1180 +
1181 + for (i = 0; i < grp->num_pins; i++) {
1182 + int *pin_modes = grp->data;
1183 +
1184 + mtk_hw_set_value(hw, grp->pins[i], PINCTRL_PIN_REG_MODE,
1185 + pin_modes[i]);
1186 + }
1187 +
1188 + return 0;
1189 +}
1190 +
1191 +static int mtk_pinmux_gpio_request_enable(struct pinctrl_dev *pctldev,
1192 + struct pinctrl_gpio_range *range,
1193 + unsigned int pin)
1194 +{
1195 + struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
1196 +
1197 + return mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_MODE, MTK_GPIO_MODE);
1198 +}
1199 +
1200 +static int mtk_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
1201 + struct pinctrl_gpio_range *range,
1202 + unsigned int pin, bool input)
1203 +{
1204 + struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
1205 +
1206 + /* hardware would take 0 as input direction */
1207 + return mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_DIR, !input);
1208 +}
1209 +
1210 +static int mtk_pinconf_get(struct pinctrl_dev *pctldev,
1211 + unsigned int pin, unsigned long *config)
1212 +{
1213 + struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
1214 + u32 param = pinconf_to_config_param(*config);
1215 + int val, val2, err, reg, ret = 1;
1216 +
1217 + switch (param) {
1218 + case PIN_CONFIG_BIAS_DISABLE:
1219 + err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_PU, &val);
1220 + if (err)
1221 + return err;
1222 +
1223 + err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_PD, &val2);
1224 + if (err)
1225 + return err;
1226 +
1227 + if (val || val2)
1228 + return -EINVAL;
1229 +
1230 + break;
1231 + case PIN_CONFIG_BIAS_PULL_UP:
1232 + case PIN_CONFIG_BIAS_PULL_DOWN:
1233 + case PIN_CONFIG_SLEW_RATE:
1234 + reg = (param == PIN_CONFIG_BIAS_PULL_UP) ?
1235 + PINCTRL_PIN_REG_PU :
1236 + (param == PIN_CONFIG_BIAS_PULL_DOWN) ?
1237 + PINCTRL_PIN_REG_PD : PINCTRL_PIN_REG_SR;
1238 +
1239 + err = mtk_hw_get_value(hw, pin, reg, &val);
1240 + if (err)
1241 + return err;
1242 +
1243 + if (!val)
1244 + return -EINVAL;
1245 +
1246 + break;
1247 + case PIN_CONFIG_INPUT_ENABLE:
1248 + case PIN_CONFIG_OUTPUT_ENABLE:
1249 + err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_DIR, &val);
1250 + if (err)
1251 + return -EINVAL;
1252 +
1253 + /* HW takes input mode as zero; output mode as non-zero */
1254 + if ((val && param == PIN_CONFIG_INPUT_ENABLE) ||
1255 + (!val && param == PIN_CONFIG_OUTPUT_ENABLE))
1256 + return -EINVAL;
1257 +
1258 + break;
1259 + case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1260 + err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_DIR, &val);
1261 + if (err)
1262 + return err;
1263 +
1264 + err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_SMT, &val2);
1265 + if (err)
1266 + return err;
1267 +
1268 + if (val || !val2)
1269 + return -EINVAL;
1270 +
1271 + break;
1272 + case PIN_CONFIG_DRIVE_STRENGTH:
1273 + err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_E4, &val);
1274 + if (err)
1275 + return -EINVAL;
1276 +
1277 + err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_E8, &val2);
1278 + if (err)
1279 + return -EINVAL;
1280 +
1281 + /* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1)
1282 + * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1)
1283 + */
1284 + ret = ((val2 << 1) + val + 1) * 4;
1285 +
1286 + break;
1287 + case MTK_PIN_CONFIG_TDSEL:
1288 + case MTK_PIN_CONFIG_RDSEL:
1289 + reg = (param == MTK_PIN_CONFIG_TDSEL) ?
1290 + PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
1291 +
1292 + err = mtk_hw_get_value(hw, pin, reg, &val);
1293 + if (err)
1294 + return -EINVAL;
1295 +
1296 + ret = val;
1297 +
1298 + break;
1299 + default:
1300 + return -ENOTSUPP;
1301 + }
1302 +
1303 + *config = pinconf_to_config_packed(param, ret);
1304 +
1305 + return 0;
1306 +}
1307 +
1308 +static int mtk_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1309 + unsigned long *configs, unsigned int num_configs)
1310 +{
1311 + struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
1312 + u32 reg, param, arg;
1313 + int cfg, err = 0;
1314 +
1315 + for (cfg = 0; cfg < num_configs; cfg++) {
1316 + param = pinconf_to_config_param(configs[cfg]);
1317 + arg = pinconf_to_config_argument(configs[cfg]);
1318 +
1319 + switch (param) {
1320 + case PIN_CONFIG_BIAS_DISABLE:
1321 + case PIN_CONFIG_BIAS_PULL_UP:
1322 + case PIN_CONFIG_BIAS_PULL_DOWN:
1323 + arg = (param == PIN_CONFIG_BIAS_DISABLE) ? 0 :
1324 + (param == PIN_CONFIG_BIAS_PULL_UP) ? 1 : 2;
1325 +
1326 + err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_PU,
1327 + arg & 1);
1328 + if (err)
1329 + goto err;
1330 +
1331 + err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_PD,
1332 + !!(arg & 2));
1333 + if (err)
1334 + goto err;
1335 + break;
1336 + case PIN_CONFIG_OUTPUT_ENABLE:
1337 + err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_SMT,
1338 + MTK_DISABLE);
1339 + if (err)
1340 + goto err;
1341 + case PIN_CONFIG_INPUT_ENABLE:
1342 + case PIN_CONFIG_SLEW_RATE:
1343 + reg = (param == PIN_CONFIG_SLEW_RATE) ?
1344 + PINCTRL_PIN_REG_SR : PINCTRL_PIN_REG_DIR;
1345 +
1346 + arg = (param == PIN_CONFIG_INPUT_ENABLE) ? 0 :
1347 + (param == PIN_CONFIG_OUTPUT_ENABLE) ? 1 : arg;
1348 + err = mtk_hw_set_value(hw, pin, reg, arg);
1349 + if (err)
1350 + goto err;
1351 +
1352 + break;
1353 + case PIN_CONFIG_OUTPUT:
1354 + err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_DIR,
1355 + MTK_OUTPUT);
1356 + if (err)
1357 + goto err;
1358 +
1359 + err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_DO,
1360 + arg);
1361 + if (err)
1362 + goto err;
1363 + break;
1364 + case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1365 + /* arg = 1: Input mode & SMT enable ;
1366 + * arg = 0: Output mode & SMT disable
1367 + */
1368 + arg = arg ? 2 : 1;
1369 + err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_DIR,
1370 + arg & 1);
1371 + if (err)
1372 + goto err;
1373 +
1374 + err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_SMT,
1375 + !!(arg & 2));
1376 + if (err)
1377 + goto err;
1378 + break;
1379 + case PIN_CONFIG_DRIVE_STRENGTH:
1380 + /* 4mA when (e8, e4) = (0, 0);
1381 + * 8mA when (e8, e4) = (0, 1);
1382 + * 12mA when (e8, e4) = (1, 0);
1383 + * 16mA when (e8, e4) = (1, 1)
1384 + */
1385 + if (!(arg % 4) && (arg >= 4 && arg <= 16)) {
1386 + arg = arg / 4 - 1;
1387 + err = mtk_hw_set_value(hw, pin,
1388 + PINCTRL_PIN_REG_E4,
1389 + arg & 0x1);
1390 + if (err)
1391 + goto err;
1392 +
1393 + err = mtk_hw_set_value(hw, pin,
1394 + PINCTRL_PIN_REG_E8,
1395 + (arg & 0x2) >> 1);
1396 + if (err)
1397 + goto err;
1398 + } else {
1399 + err = -ENOTSUPP;
1400 + }
1401 + break;
1402 + case MTK_PIN_CONFIG_TDSEL:
1403 + case MTK_PIN_CONFIG_RDSEL:
1404 + reg = (param == MTK_PIN_CONFIG_TDSEL) ?
1405 + PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
1406 +
1407 + err = mtk_hw_set_value(hw, pin, reg, arg);
1408 + if (err)
1409 + goto err;
1410 + break;
1411 + default:
1412 + err = -ENOTSUPP;
1413 + }
1414 + }
1415 +err:
1416 + return err;
1417 +}
1418 +
1419 +static int mtk_pinconf_group_get(struct pinctrl_dev *pctldev,
1420 + unsigned int group, unsigned long *config)
1421 +{
1422 + const unsigned int *pins;
1423 + unsigned int i, npins, old = 0;
1424 + int ret;
1425 +
1426 + ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
1427 + if (ret)
1428 + return ret;
1429 +
1430 + for (i = 0; i < npins; i++) {
1431 + if (mtk_pinconf_get(pctldev, pins[i], config))
1432 + return -ENOTSUPP;
1433 +
1434 + /* configs do not match between two pins */
1435 + if (i && old != *config)
1436 + return -ENOTSUPP;
1437 +
1438 + old = *config;
1439 + }
1440 +
1441 + return 0;
1442 +}
1443 +
1444 +static int mtk_pinconf_group_set(struct pinctrl_dev *pctldev,
1445 + unsigned int group, unsigned long *configs,
1446 + unsigned int num_configs)
1447 +{
1448 + const unsigned int *pins;
1449 + unsigned int i, npins;
1450 + int ret;
1451 +
1452 + ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
1453 + if (ret)
1454 + return ret;
1455 +
1456 + for (i = 0; i < npins; i++) {
1457 + ret = mtk_pinconf_set(pctldev, pins[i], configs, num_configs);
1458 + if (ret)
1459 + return ret;
1460 + }
1461 +
1462 + return 0;
1463 +}
1464 +
1465 +static const struct pinctrl_ops mtk_pctlops = {
1466 + .get_groups_count = pinctrl_generic_get_group_count,
1467 + .get_group_name = pinctrl_generic_get_group_name,
1468 + .get_group_pins = pinctrl_generic_get_group_pins,
1469 + .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
1470 + .dt_free_map = pinconf_generic_dt_free_map,
1471 +};
1472 +
1473 +static const struct pinmux_ops mtk_pmxops = {
1474 + .get_functions_count = pinmux_generic_get_function_count,
1475 + .get_function_name = pinmux_generic_get_function_name,
1476 + .get_function_groups = pinmux_generic_get_function_groups,
1477 + .set_mux = mtk_pinmux_set_mux,
1478 + .gpio_request_enable = mtk_pinmux_gpio_request_enable,
1479 + .gpio_set_direction = mtk_pinmux_gpio_set_direction,
1480 + .strict = true,
1481 +};
1482 +
1483 +static const struct pinconf_ops mtk_confops = {
1484 + .is_generic = true,
1485 + .pin_config_get = mtk_pinconf_get,
1486 + .pin_config_set = mtk_pinconf_set,
1487 + .pin_config_group_get = mtk_pinconf_group_get,
1488 + .pin_config_group_set = mtk_pinconf_group_set,
1489 + .pin_config_config_dbg_show = pinconf_generic_dump_config,
1490 +};
1491 +
1492 +static struct pinctrl_desc mtk_desc = {
1493 + .name = PINCTRL_PINCTRL_DEV,
1494 + .pctlops = &mtk_pctlops,
1495 + .pmxops = &mtk_pmxops,
1496 + .confops = &mtk_confops,
1497 + .owner = THIS_MODULE,
1498 +};
1499 +
1500 +static int mtk_gpio_get(struct gpio_chip *chip, unsigned int gpio)
1501 +{
1502 + struct mtk_pinctrl *hw = dev_get_drvdata(chip->parent);
1503 + int value;
1504 +
1505 + mtk_hw_get_value(hw, gpio, PINCTRL_PIN_REG_DI, &value);
1506 +
1507 + return !!value;
1508 +}
1509 +
1510 +static void mtk_gpio_set(struct gpio_chip *chip, unsigned int gpio, int value)
1511 +{
1512 + struct mtk_pinctrl *hw = dev_get_drvdata(chip->parent);
1513 +
1514 + mtk_hw_set_value(hw, gpio, PINCTRL_PIN_REG_DO, !!value);
1515 +}
1516 +
1517 +static int mtk_gpio_direction_input(struct gpio_chip *chip, unsigned int gpio)
1518 +{
1519 + return pinctrl_gpio_direction_input(chip->base + gpio);
1520 +}
1521 +
1522 +static int mtk_gpio_direction_output(struct gpio_chip *chip, unsigned int gpio,
1523 + int value)
1524 +{
1525 + mtk_gpio_set(chip, gpio, value);
1526 +
1527 + return pinctrl_gpio_direction_output(chip->base + gpio);
1528 +}
1529 +
1530 +static int mtk_build_gpiochip(struct mtk_pinctrl *hw, struct device_node *np)
1531 +{
1532 + struct gpio_chip *chip = &hw->chip;
1533 + int ret;
1534 +
1535 + chip->label = PINCTRL_PINCTRL_DEV;
1536 + chip->parent = hw->dev;
1537 + chip->request = gpiochip_generic_request;
1538 + chip->free = gpiochip_generic_free;
1539 + chip->direction_input = mtk_gpio_direction_input;
1540 + chip->direction_output = mtk_gpio_direction_output;
1541 + chip->get = mtk_gpio_get;
1542 + chip->set = mtk_gpio_set;
1543 + chip->base = -1;
1544 + chip->ngpio = hw->soc->npins;
1545 + chip->of_node = np;
1546 + chip->of_gpio_n_cells = 2;
1547 +
1548 + ret = gpiochip_add_data(chip, hw);
1549 + if (ret < 0)
1550 + return ret;
1551 +
1552 + ret = gpiochip_add_pin_range(chip, dev_name(hw->dev), 0, 0,
1553 + chip->ngpio);
1554 + if (ret < 0) {
1555 + gpiochip_remove(chip);
1556 + return ret;
1557 + }
1558 +
1559 + return 0;
1560 +}
1561 +
1562 +static int mtk_build_groups(struct mtk_pinctrl *hw)
1563 +{
1564 + int err, i;
1565 +
1566 + for (i = 0; i < hw->soc->ngrps; i++) {
1567 + const struct group_desc *group = hw->soc->grps + i;
1568 +
1569 + err = pinctrl_generic_add_group(hw->pctrl, group->name,
1570 + group->pins, group->num_pins,
1571 + group->data);
1572 + if (err) {
1573 + dev_err(hw->dev, "Failed to register group %s\n",
1574 + group->name);
1575 + return err;
1576 + }
1577 + }
1578 +
1579 + return 0;
1580 +}
1581 +
1582 +static int mtk_build_functions(struct mtk_pinctrl *hw)
1583 +{
1584 + int i, err;
1585 +
1586 + for (i = 0; i < hw->soc->nfuncs ; i++) {
1587 + const struct function_desc *func = hw->soc->funcs + i;
1588 +
1589 + err = pinmux_generic_add_function(hw->pctrl, func->name,
1590 + func->group_names,
1591 + func->num_group_names,
1592 + func->data);
1593 + if (err) {
1594 + dev_err(hw->dev, "Failed to register function %s\n",
1595 + func->name);
1596 + return err;
1597 + }
1598 + }
1599 +
1600 + return 0;
1601 +}
1602 +
1603 +static const struct of_device_id mtk_pinctrl_of_match[] = {
1604 + { .compatible = "mediatek,mt7622-pinctrl", .data = &mt7622_data},
1605 + { }
1606 +};
1607 +
1608 +static int mtk_pinctrl_probe(struct platform_device *pdev)
1609 +{
1610 + struct resource *res;
1611 + struct mtk_pinctrl *hw;
1612 + const struct of_device_id *of_id =
1613 + of_match_device(mtk_pinctrl_of_match, &pdev->dev);
1614 + int err;
1615 +
1616 + hw = devm_kzalloc(&pdev->dev, sizeof(*hw), GFP_KERNEL);
1617 + if (!hw)
1618 + return -ENOMEM;
1619 +
1620 + hw->soc = of_id->data;
1621 +
1622 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1623 + if (!res) {
1624 + dev_err(&pdev->dev, "missing IO resource\n");
1625 + return -ENXIO;
1626 + }
1627 +
1628 + hw->dev = &pdev->dev;
1629 + hw->base = devm_ioremap_resource(&pdev->dev, res);
1630 + if (IS_ERR(hw->base))
1631 + return PTR_ERR(hw->base);
1632 +
1633 + /* Setup pins descriptions per SoC types */
1634 + mtk_desc.pins = hw->soc->pins;
1635 + mtk_desc.npins = hw->soc->npins;
1636 + mtk_desc.num_custom_params = ARRAY_SIZE(mtk_custom_bindings);
1637 + mtk_desc.custom_params = mtk_custom_bindings;
1638 +#ifdef CONFIG_DEBUG_FS
1639 + mtk_desc.custom_conf_items = mtk_conf_items;
1640 +#endif
1641 +
1642 + hw->pctrl = devm_pinctrl_register(&pdev->dev, &mtk_desc, hw);
1643 + if (IS_ERR(hw->pctrl))
1644 + return PTR_ERR(hw->pctrl);
1645 +
1646 + /* Setup groups descriptions per SoC types */
1647 + err = mtk_build_groups(hw);
1648 + if (err) {
1649 + dev_err(&pdev->dev, "Failed to build groups\n");
1650 + return 0;
1651 + }
1652 +
1653 + /* Setup functions descriptions per SoC types */
1654 + err = mtk_build_functions(hw);
1655 + if (err) {
1656 + dev_err(&pdev->dev, "Failed to build functions\n");
1657 + return err;
1658 + }
1659 +
1660 + err = mtk_build_gpiochip(hw, pdev->dev.of_node);
1661 + if (err) {
1662 + dev_err(&pdev->dev, "Failed to add gpio_chip\n");
1663 + return err;
1664 + }
1665 +
1666 + platform_set_drvdata(pdev, hw);
1667 +
1668 + return 0;
1669 +}
1670 +
1671 +static struct platform_driver mtk_pinctrl_driver = {
1672 + .driver = {
1673 + .name = "mtk-pinctrl",
1674 + .of_match_table = mtk_pinctrl_of_match,
1675 + },
1676 + .probe = mtk_pinctrl_probe,
1677 +};
1678 +
1679 +static int __init mtk_pinctrl_init(void)
1680 +{
1681 + return platform_driver_register(&mtk_pinctrl_driver);
1682 +}
1683 +arch_initcall(mtk_pinctrl_init);
1684 --
1685 2.11.0
1686