pinctrl: bcm2712 pinctrl/pinconf driver
[platform/kernel/linux-rpi.git] / drivers / pinctrl / bcm / pinctrl-bcm2712.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Broadcom BCM2712 GPIO units (pinctrl only)
4  *
5  * Copyright (C) 2021-3 Raspberry Pi Ltd.
6  * Copyright (C) 2012 Chris Boot, Simon Arlott, Stephen Warren
7  *
8  * Based heavily on the BCM2835 GPIO & pinctrl driver, which was inspired by:
9  * pinctrl-nomadik.c, please see original file for copyright information
10  * pinctrl-tegra.c, please see original file for copyright information
11  */
12
13 #include <linux/bitmap.h>
14 #include <linux/bug.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/io.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/of_address.h>
22 #include <linux/of.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pinctrl/machine.h>
25 #include <linux/pinctrl/pinconf.h>
26 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/pinctrl/pinmux.h>
28 #include <linux/pinctrl/pinconf-generic.h>
29 #include <linux/platform_device.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/types.h>
34
35 #define MODULE_NAME "pinctrl-bcm2712"
36
37 /* Register offsets */
38
39 #define BCM2712_PULL_NONE       0
40 #define BCM2712_PULL_DOWN       1
41 #define BCM2712_PULL_UP         2
42 #define BCM2712_PULL_MASK       0x3
43
44 #define BCM2712_FSEL_COUNT 9
45 #define BCM2712_FSEL_MASK  0xf
46
47 #define FUNC(f) \
48         [func_##f] = #f
49 #define PIN(i, f1, f2, f3, f4, f5, f6, f7, f8) \
50         [i] = { \
51                 .funcs = { \
52                         func_##f1, \
53                         func_##f2, \
54                         func_##f3, \
55                         func_##f4, \
56                         func_##f5, \
57                         func_##f6, \
58                         func_##f7, \
59                         func_##f8, \
60                 }, \
61         }
62
63 #define REG_BIT_INVALID 0xffff
64
65 #define BIT_TO_REG(b) (((b) >> 5) << 2)
66 #define BIT_TO_SHIFT(b) ((b) & 0x1f)
67
68 #define GPIO_REGS(n, mr, mb, pr, pb) \
69         [n] = { ((mr)*4)*8 + (mb)*4, ((pr)*4)*8 + (pb)*2 }
70
71 #define EMMC_REGS(n, r, b) \
72         [n] = { 0, ((r)*4)*8 + (b)*2 }
73
74 #define AGPIO_REGS(n, mr, mb, pr, pb) \
75         [n] = { ((mr)*4)*8 + (mb)*4, ((pr)*4)*8 + (pb)*2 }
76
77 #define SGPIO_REGS(n, mr, mb) \
78         [n+32] = { ((mr)*4)*8 + (mb)*4, REG_BIT_INVALID }
79
80 #define GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
81 #define AGPIO_PIN(a) PINCTRL_PIN(a, "aon_gpio" #a)
82 #define SGPIO_PIN(a) PINCTRL_PIN(a+32, "aon_sgpio" #a)
83
84 struct pin_regs {
85         u16 mux_bit;
86         u16 pad_bit;
87 };
88
89 struct bcm2712_pinctrl {
90         struct device *dev;
91         void __iomem *base;
92         struct pinctrl_dev *pctl_dev;
93         struct pinctrl_desc pctl_desc;
94         const struct pin_regs *pin_regs;
95         const struct bcm2712_pin_funcs *pin_funcs;
96         const char *const *gpio_groups;
97         struct pinctrl_gpio_range gpio_range;
98         spinlock_t lock;
99 };
100
101 struct bcm_plat_data {
102         const struct pinctrl_desc *pctl_desc;
103         const struct pinctrl_gpio_range *gpio_range;
104         const struct pin_regs *pin_regs;
105         const struct bcm2712_pin_funcs *pin_funcs;
106 };
107
108 struct bcm2712_pin_funcs {
109         u8 funcs[BCM2712_FSEL_COUNT - 1];
110 };
111
112 enum bcm2712_funcs {
113         func_gpio,
114         func_alt1,
115         func_alt2,
116         func_alt3,
117         func_alt4,
118         func_alt5,
119         func_alt6,
120         func_alt7,
121         func_alt8,
122         func_aon_cpu_standbyb,
123         func_aon_fp_4sec_resetb,
124         func_aon_gpclk,
125         func_aon_pwm,
126         func_arm_jtag,
127         func_aud_fs_clk0,
128         func_avs_pmu_bsc,
129         func_bsc_m0,
130         func_bsc_m1,
131         func_bsc_m2,
132         func_bsc_m3,
133         func_clk_observe,
134         func_ctl_hdmi_5v,
135         func_enet0,
136         func_enet0_mii,
137         func_enet0_rgmii,
138         func_ext_sc_clk,
139         func_fl0,
140         func_fl1,
141         func_gpclk0,
142         func_gpclk1,
143         func_gpclk2,
144         func_hdmi_tx0_auto_i2c,
145         func_hdmi_tx0_bsc,
146         func_hdmi_tx1_auto_i2c,
147         func_hdmi_tx1_bsc,
148         func_i2s_in,
149         func_i2s_out,
150         func_ir_in,
151         func_mtsif,
152         func_mtsif_alt,
153         func_mtsif_alt1,
154         func_pdm,
155         func_pkt,
156         func_pm_led_out,
157         func_sc0,
158         func_sd0,
159         func_sd2,
160         func_sd_card_a,
161         func_sd_card_b,
162         func_sd_card_c,
163         func_sd_card_d,
164         func_sd_card_e,
165         func_sd_card_f,
166         func_sd_card_g,
167         func_spdif_out,
168         func_spi_m,
169         func_spi_s,
170         func_sr_edm_sense,
171         func_te0,
172         func_te1,
173         func_tsio,
174         func_uart0,
175         func_uart1,
176         func_uart2,
177         func_usb_pwr,
178         func_usb_vbus,
179         func_uui,
180         func_vc_i2c0,
181         func_vc_i2c3,
182         func_vc_i2c4,
183         func_vc_i2c5,
184         func_vc_i2csl,
185         func_vc_pcm,
186         func_vc_pwm0,
187         func_vc_pwm1,
188         func_vc_spi0,
189         func_vc_spi3,
190         func_vc_spi4,
191         func_vc_spi5,
192         func_vc_uart0,
193         func_vc_uart2,
194         func_vc_uart3,
195         func_vc_uart4,
196         func__,
197         func_count = func__
198 };
199
200 static const struct pin_regs bcm2712_c0_gpio_pin_regs[] = {
201         GPIO_REGS(0, 0, 0, 7, 7),
202         GPIO_REGS(1, 0, 1, 7, 8),
203         GPIO_REGS(2, 0, 2, 7, 9),
204         GPIO_REGS(3, 0, 3, 7, 10),
205         GPIO_REGS(4, 0, 4, 7, 11),
206         GPIO_REGS(5, 0, 5, 7, 12),
207         GPIO_REGS(6, 0, 6, 7, 13),
208         GPIO_REGS(7, 0, 7, 7, 14),
209         GPIO_REGS(8, 1, 0, 8, 0),
210         GPIO_REGS(9, 1, 1, 8, 1),
211         GPIO_REGS(10, 1, 2, 8, 2),
212         GPIO_REGS(11, 1, 3, 8, 3),
213         GPIO_REGS(12, 1, 4, 8, 4),
214         GPIO_REGS(13, 1, 5, 8, 5),
215         GPIO_REGS(14, 1, 6, 8, 6),
216         GPIO_REGS(15, 1, 7, 8, 7),
217         GPIO_REGS(16, 2, 0, 8, 8),
218         GPIO_REGS(17, 2, 1, 8, 9),
219         GPIO_REGS(18, 2, 2, 8, 10),
220         GPIO_REGS(19, 2, 3, 8, 11),
221         GPIO_REGS(20, 2, 4, 8, 12),
222         GPIO_REGS(21, 2, 5, 8, 13),
223         GPIO_REGS(22, 2, 6, 8, 14),
224         GPIO_REGS(23, 2, 7, 9, 0),
225         GPIO_REGS(24, 3, 0, 9, 1),
226         GPIO_REGS(25, 3, 1, 9, 2),
227         GPIO_REGS(26, 3, 2, 9, 3),
228         GPIO_REGS(27, 3, 3, 9, 4),
229         GPIO_REGS(28, 3, 4, 9, 5),
230         GPIO_REGS(29, 3, 5, 9, 6),
231         GPIO_REGS(30, 3, 6, 9, 7),
232         GPIO_REGS(31, 3, 7, 9, 8),
233         GPIO_REGS(32, 4, 0, 9, 9),
234         GPIO_REGS(33, 4, 1, 9, 10),
235         GPIO_REGS(34, 4, 2, 9, 11),
236         GPIO_REGS(35, 4, 3, 9, 12),
237         GPIO_REGS(36, 4, 4, 9, 13),
238         GPIO_REGS(37, 4, 5, 9, 14),
239         GPIO_REGS(38, 4, 6, 10, 0),
240         GPIO_REGS(39, 4, 7, 10, 1),
241         GPIO_REGS(40, 5, 0, 10, 2),
242         GPIO_REGS(41, 5, 1, 10, 3),
243         GPIO_REGS(42, 5, 2, 10, 4),
244         GPIO_REGS(43, 5, 3, 10, 5),
245         GPIO_REGS(44, 5, 4, 10, 6),
246         GPIO_REGS(45, 5, 5, 10, 7),
247         GPIO_REGS(46, 5, 6, 10, 8),
248         GPIO_REGS(47, 5, 7, 10, 9),
249         GPIO_REGS(48, 6, 0, 10, 10),
250         GPIO_REGS(49, 6, 1, 10, 11),
251         GPIO_REGS(50, 6, 2, 10, 12),
252         GPIO_REGS(51, 6, 3, 10, 13),
253         GPIO_REGS(52, 6, 4, 10, 14),
254         GPIO_REGS(53, 6, 5, 11, 0),
255         EMMC_REGS(54, 11, 1), /* EMMC_CMD */
256         EMMC_REGS(55, 11, 2), /* EMMC_DS */
257         EMMC_REGS(56, 11, 3), /* EMMC_CLK */
258         EMMC_REGS(57, 11, 4), /* EMMC_DAT0 */
259         EMMC_REGS(58, 11, 5), /* EMMC_DAT1 */
260         EMMC_REGS(59, 11, 6), /* EMMC_DAT2 */
261         EMMC_REGS(60, 11, 7), /* EMMC_DAT3 */
262         EMMC_REGS(61, 11, 8), /* EMMC_DAT4 */
263         EMMC_REGS(62, 11, 9), /* EMMC_DAT5 */
264         EMMC_REGS(63, 11, 10), /* EMMC_DAT6 */
265         EMMC_REGS(64, 11, 11), /* EMMC_DAT7 */
266 };
267
268 static struct pin_regs bcm2712_c0_aon_gpio_pin_regs[] = {
269         AGPIO_REGS(0, 3, 0, 6, 10),
270         AGPIO_REGS(1, 3, 1, 6, 11),
271         AGPIO_REGS(2, 3, 2, 6, 12),
272         AGPIO_REGS(3, 3, 3, 6, 13),
273         AGPIO_REGS(4, 3, 4, 6, 14),
274         AGPIO_REGS(5, 3, 5, 7, 0),
275         AGPIO_REGS(6, 3, 6, 7, 1),
276         AGPIO_REGS(7, 3, 7, 7, 2),
277         AGPIO_REGS(8, 4, 0, 7, 3),
278         AGPIO_REGS(9, 4, 1, 7, 4),
279         AGPIO_REGS(10, 4, 2, 7, 5),
280         AGPIO_REGS(11, 4, 3, 7, 6),
281         AGPIO_REGS(12, 4, 4, 7, 7),
282         AGPIO_REGS(13, 4, 5, 7, 8),
283         AGPIO_REGS(14, 4, 6, 7, 9),
284         AGPIO_REGS(15, 4, 7, 7, 10),
285         AGPIO_REGS(16, 5, 0, 7, 11),
286         SGPIO_REGS(0, 0, 0),
287         SGPIO_REGS(1, 0, 1),
288         SGPIO_REGS(2, 0, 2),
289         SGPIO_REGS(3, 0, 3),
290         SGPIO_REGS(4, 1, 0),
291         SGPIO_REGS(5, 2, 0),
292 };
293
294 static const struct pinctrl_pin_desc bcm2712_c0_gpio_pins[] = {
295         GPIO_PIN(0),
296         GPIO_PIN(1),
297         GPIO_PIN(2),
298         GPIO_PIN(3),
299         GPIO_PIN(4),
300         GPIO_PIN(5),
301         GPIO_PIN(6),
302         GPIO_PIN(7),
303         GPIO_PIN(8),
304         GPIO_PIN(9),
305         GPIO_PIN(10),
306         GPIO_PIN(11),
307         GPIO_PIN(12),
308         GPIO_PIN(13),
309         GPIO_PIN(14),
310         GPIO_PIN(15),
311         GPIO_PIN(16),
312         GPIO_PIN(17),
313         GPIO_PIN(18),
314         GPIO_PIN(19),
315         GPIO_PIN(20),
316         GPIO_PIN(21),
317         GPIO_PIN(22),
318         GPIO_PIN(23),
319         GPIO_PIN(24),
320         GPIO_PIN(25),
321         GPIO_PIN(26),
322         GPIO_PIN(27),
323         GPIO_PIN(28),
324         GPIO_PIN(29),
325         GPIO_PIN(30),
326         GPIO_PIN(31),
327         GPIO_PIN(32),
328         GPIO_PIN(33),
329         GPIO_PIN(34),
330         GPIO_PIN(35),
331         GPIO_PIN(36),
332         GPIO_PIN(37),
333         GPIO_PIN(38),
334         GPIO_PIN(39),
335         GPIO_PIN(40),
336         GPIO_PIN(41),
337         GPIO_PIN(42),
338         GPIO_PIN(43),
339         GPIO_PIN(44),
340         GPIO_PIN(45),
341         GPIO_PIN(46),
342         GPIO_PIN(47),
343         GPIO_PIN(48),
344         GPIO_PIN(49),
345         GPIO_PIN(50),
346         GPIO_PIN(51),
347         GPIO_PIN(52),
348         GPIO_PIN(53),
349         PINCTRL_PIN(54, "emmc_cmd"),
350         PINCTRL_PIN(55, "emmc_ds"),
351         PINCTRL_PIN(56, "emmc_clk"),
352         PINCTRL_PIN(57, "emmc_dat0"),
353         PINCTRL_PIN(58, "emmc_dat1"),
354         PINCTRL_PIN(59, "emmc_dat2"),
355         PINCTRL_PIN(60, "emmc_dat3"),
356         PINCTRL_PIN(61, "emmc_dat4"),
357         PINCTRL_PIN(62, "emmc_dat5"),
358         PINCTRL_PIN(63, "emmc_dat6"),
359         PINCTRL_PIN(64, "emmc_dat7"),
360 };
361
362 static struct pinctrl_pin_desc bcm2712_c0_aon_gpio_pins[] = {
363         AGPIO_PIN(0),
364         AGPIO_PIN(1),
365         AGPIO_PIN(2),
366         AGPIO_PIN(3),
367         AGPIO_PIN(4),
368         AGPIO_PIN(5),
369         AGPIO_PIN(6),
370         AGPIO_PIN(7),
371         AGPIO_PIN(8),
372         AGPIO_PIN(9),
373         AGPIO_PIN(10),
374         AGPIO_PIN(11),
375         AGPIO_PIN(12),
376         AGPIO_PIN(13),
377         AGPIO_PIN(14),
378         AGPIO_PIN(15),
379         AGPIO_PIN(16),
380         SGPIO_PIN(0),
381         SGPIO_PIN(1),
382         SGPIO_PIN(2),
383         SGPIO_PIN(3),
384         SGPIO_PIN(4),
385         SGPIO_PIN(5),
386 };
387
388 static const struct pin_regs bcm2712_d0_gpio_pin_regs[] = {
389         GPIO_REGS(1, 0, 0, 4, 5),
390         GPIO_REGS(2, 0, 1, 4, 6),
391         GPIO_REGS(3, 0, 2, 4, 7),
392         GPIO_REGS(4, 0, 3, 4, 8),
393         GPIO_REGS(10, 0, 4, 4, 9),
394         GPIO_REGS(11, 0, 5, 4, 10),
395         GPIO_REGS(12, 0, 6, 4, 11),
396         GPIO_REGS(13, 0, 7, 4, 12),
397         GPIO_REGS(14, 1, 0, 4, 13),
398         GPIO_REGS(15, 1, 1, 4, 14),
399         GPIO_REGS(18, 1, 2, 5, 0),
400         GPIO_REGS(19, 1, 3, 5, 1),
401         GPIO_REGS(20, 1, 4, 5, 2),
402         GPIO_REGS(21, 1, 5, 5, 3),
403         GPIO_REGS(22, 1, 6, 5, 4),
404         GPIO_REGS(23, 1, 7, 5, 5),
405         GPIO_REGS(24, 2, 0, 5, 6),
406         GPIO_REGS(25, 2, 1, 5, 7),
407         GPIO_REGS(26, 2, 2, 5, 8),
408         GPIO_REGS(27, 2, 3, 5, 9),
409         GPIO_REGS(28, 2, 4, 5, 10),
410         GPIO_REGS(29, 2, 5, 5, 11),
411         GPIO_REGS(30, 2, 6, 5, 12),
412         GPIO_REGS(31, 2, 7, 5, 13),
413         GPIO_REGS(32, 3, 0, 5, 14),
414         GPIO_REGS(33, 3, 1, 6, 0),
415         GPIO_REGS(34, 3, 2, 6, 1),
416         GPIO_REGS(35, 3, 3, 6, 2),
417 };
418
419 static struct pin_regs bcm2712_d0_aon_gpio_pin_regs[] = {
420         AGPIO_REGS(0, 3, 0, 5, 9),
421         AGPIO_REGS(1, 3, 1, 5, 10),
422         AGPIO_REGS(2, 3, 2, 5, 11),
423         AGPIO_REGS(3, 3, 3, 5, 12),
424         AGPIO_REGS(4, 3, 4, 5, 13),
425         AGPIO_REGS(5, 3, 5, 5, 14),
426         AGPIO_REGS(6, 3, 6, 6, 0),
427         AGPIO_REGS(8, 3, 7, 6, 1),
428         AGPIO_REGS(9, 4, 0, 6, 2),
429         AGPIO_REGS(12, 4, 1, 6, 3),
430         AGPIO_REGS(13, 4, 2, 6, 4),
431         AGPIO_REGS(14, 4, 3, 6, 5),
432         SGPIO_REGS(0, 0, 0),
433         SGPIO_REGS(1, 0, 1),
434         SGPIO_REGS(2, 0, 2),
435         SGPIO_REGS(3, 0, 3),
436         SGPIO_REGS(4, 1, 0),
437         SGPIO_REGS(5, 2, 0),
438 };
439
440 static const struct pinctrl_pin_desc bcm2712_d0_gpio_pins[] = {
441         GPIO_PIN(1),
442         GPIO_PIN(2),
443         GPIO_PIN(3),
444         GPIO_PIN(4),
445         GPIO_PIN(10),
446         GPIO_PIN(11),
447         GPIO_PIN(12),
448         GPIO_PIN(13),
449         GPIO_PIN(14),
450         GPIO_PIN(15),
451         GPIO_PIN(18),
452         GPIO_PIN(19),
453         GPIO_PIN(20),
454         GPIO_PIN(21),
455         GPIO_PIN(22),
456         GPIO_PIN(23),
457         GPIO_PIN(24),
458         GPIO_PIN(25),
459         GPIO_PIN(26),
460         GPIO_PIN(27),
461         GPIO_PIN(28),
462         GPIO_PIN(29),
463         GPIO_PIN(30),
464         GPIO_PIN(31),
465         GPIO_PIN(32),
466         GPIO_PIN(33),
467         GPIO_PIN(34),
468         GPIO_PIN(35),
469 };
470
471 static struct pinctrl_pin_desc bcm2712_d0_aon_gpio_pins[] = {
472         AGPIO_PIN(0),
473         AGPIO_PIN(1),
474         AGPIO_PIN(2),
475         AGPIO_PIN(3),
476         AGPIO_PIN(4),
477         AGPIO_PIN(5),
478         AGPIO_PIN(6),
479         AGPIO_PIN(8),
480         AGPIO_PIN(9),
481         AGPIO_PIN(12),
482         AGPIO_PIN(13),
483         AGPIO_PIN(14),
484         SGPIO_PIN(0),
485         SGPIO_PIN(1),
486         SGPIO_PIN(2),
487         SGPIO_PIN(3),
488         SGPIO_PIN(4),
489         SGPIO_PIN(5),
490 };
491
492 static const char * const bcm2712_func_names[] = {
493         FUNC(gpio),
494         FUNC(alt1),
495         FUNC(alt2),
496         FUNC(alt3),
497         FUNC(alt4),
498         FUNC(alt5),
499         FUNC(alt6),
500         FUNC(alt7),
501         FUNC(alt8),
502         FUNC(aon_cpu_standbyb),
503         FUNC(aon_fp_4sec_resetb),
504         FUNC(aon_gpclk),
505         FUNC(aon_pwm),
506         FUNC(arm_jtag),
507         FUNC(aud_fs_clk0),
508         FUNC(avs_pmu_bsc),
509         FUNC(bsc_m0),
510         FUNC(bsc_m1),
511         FUNC(bsc_m2),
512         FUNC(bsc_m3),
513         FUNC(clk_observe),
514         FUNC(ctl_hdmi_5v),
515         FUNC(enet0),
516         FUNC(enet0_mii),
517         FUNC(enet0_rgmii),
518         FUNC(ext_sc_clk),
519         FUNC(fl0),
520         FUNC(fl1),
521         FUNC(gpclk0),
522         FUNC(gpclk1),
523         FUNC(gpclk2),
524         FUNC(hdmi_tx0_auto_i2c),
525         FUNC(hdmi_tx0_bsc),
526         FUNC(hdmi_tx1_auto_i2c),
527         FUNC(hdmi_tx1_bsc),
528         FUNC(i2s_in),
529         FUNC(i2s_out),
530         FUNC(ir_in),
531         FUNC(mtsif),
532         FUNC(mtsif_alt),
533         FUNC(mtsif_alt1),
534         FUNC(pdm),
535         FUNC(pkt),
536         FUNC(pm_led_out),
537         FUNC(sc0),
538         FUNC(sd0),
539         FUNC(sd2),
540         FUNC(sd_card_a),
541         FUNC(sd_card_b),
542         FUNC(sd_card_c),
543         FUNC(sd_card_d),
544         FUNC(sd_card_e),
545         FUNC(sd_card_f),
546         FUNC(sd_card_g),
547         FUNC(spdif_out),
548         FUNC(spi_m),
549         FUNC(spi_s),
550         FUNC(sr_edm_sense),
551         FUNC(te0),
552         FUNC(te1),
553         FUNC(tsio),
554         FUNC(uart0),
555         FUNC(uart1),
556         FUNC(uart2),
557         FUNC(usb_pwr),
558         FUNC(usb_vbus),
559         FUNC(uui),
560         FUNC(vc_i2c0),
561         FUNC(vc_i2c3),
562         FUNC(vc_i2c4),
563         FUNC(vc_i2c5),
564         FUNC(vc_i2csl),
565         FUNC(vc_pcm),
566         FUNC(vc_pwm0),
567         FUNC(vc_pwm1),
568         FUNC(vc_spi0),
569         FUNC(vc_spi3),
570         FUNC(vc_spi4),
571         FUNC(vc_spi5),
572         FUNC(vc_uart0),
573         FUNC(vc_uart2),
574         FUNC(vc_uart3),
575         FUNC(vc_uart4),
576 };
577
578 static const struct bcm2712_pin_funcs bcm2712_c0_aon_gpio_pin_funcs[] = {
579         PIN(0, ir_in, vc_spi0, vc_uart3, vc_i2c3, te0, vc_i2c0, _, _),
580         PIN(1, vc_pwm0, vc_spi0, vc_uart3, vc_i2c3, te1, aon_pwm, vc_i2c0, vc_pwm1),
581         PIN(2, vc_pwm0, vc_spi0, vc_uart3, ctl_hdmi_5v, fl0, aon_pwm, ir_in, vc_pwm1),
582         PIN(3, ir_in, vc_spi0, vc_uart3, aon_fp_4sec_resetb, fl1, sd_card_g, aon_gpclk, _),
583         PIN(4, gpclk0, vc_spi0, vc_i2csl, aon_gpclk, pm_led_out, aon_pwm, sd_card_g, vc_pwm0),
584         PIN(5, gpclk1, ir_in, vc_i2csl, clk_observe, aon_pwm, sd_card_g, vc_pwm0, _),
585         PIN(6, uart1, vc_uart4, gpclk2, ctl_hdmi_5v, vc_uart0, vc_spi3, _, _),
586         PIN(7, uart1, vc_uart4, gpclk0, aon_pwm, vc_uart0, vc_spi3, _, _),
587         PIN(8, uart1, vc_uart4, vc_i2csl, ctl_hdmi_5v, vc_uart0, vc_spi3, _, _),
588         PIN(9, uart1, vc_uart4, vc_i2csl, aon_pwm, vc_uart0, vc_spi3, _, _),
589         PIN(10, tsio, ctl_hdmi_5v, sc0, spdif_out, vc_spi5, usb_pwr, aon_gpclk, sd_card_f),
590         PIN(11, tsio, uart0, sc0, aud_fs_clk0, vc_spi5, usb_vbus, vc_uart2, sd_card_f),
591         PIN(12, tsio, uart0, vc_uart0, tsio, vc_spi5, usb_pwr, vc_uart2, sd_card_f),
592         PIN(13, bsc_m1, uart0, vc_uart0, uui, vc_spi5, arm_jtag, vc_uart2, vc_i2c3),
593         PIN(14, bsc_m1, uart0, vc_uart0, uui, vc_spi5, arm_jtag, vc_uart2, vc_i2c3),
594         PIN(15, ir_in, aon_fp_4sec_resetb, vc_uart0, pm_led_out, ctl_hdmi_5v, aon_pwm, aon_gpclk, _),
595         PIN(16, aon_cpu_standbyb, gpclk0, pm_led_out, ctl_hdmi_5v, vc_pwm0, usb_pwr, aud_fs_clk0, _),
596 };
597
598 static const struct bcm2712_pin_funcs bcm2712_c0_aon_sgpio_pin_funcs[] = {
599         PIN(0, hdmi_tx0_bsc, hdmi_tx0_auto_i2c, bsc_m0, vc_i2c0, _, _, _, _),
600         PIN(1, hdmi_tx0_bsc, hdmi_tx0_auto_i2c, bsc_m0, vc_i2c0, _, _, _, _),
601         PIN(2, hdmi_tx1_bsc, hdmi_tx1_auto_i2c, bsc_m1, vc_i2c4, ctl_hdmi_5v, _, _, _),
602         PIN(3, hdmi_tx1_bsc, hdmi_tx1_auto_i2c, bsc_m1, vc_i2c4, _, _, _, _),
603         PIN(4, avs_pmu_bsc, bsc_m2, vc_i2c5, ctl_hdmi_5v, _, _, _, _),
604         PIN(5, avs_pmu_bsc, bsc_m2, vc_i2c5, _, _, _, _, _),
605 };
606
607 static const struct bcm2712_pin_funcs bcm2712_c0_gpio_pin_funcs[] = {
608         PIN(0, bsc_m3, vc_i2c0, gpclk0, enet0, vc_pwm1, vc_spi0, ir_in, _),
609         PIN(1, bsc_m3, vc_i2c0, gpclk1, enet0, vc_pwm1, sr_edm_sense, vc_spi0, vc_uart3),
610         PIN(2, pdm, i2s_in, gpclk2, vc_spi4, pkt, vc_spi0, vc_uart3, _),
611         PIN(3, pdm, i2s_in, vc_spi4, pkt, vc_spi0, vc_uart3, _, _),
612         PIN(4, pdm, i2s_in, arm_jtag, vc_spi4, pkt, vc_spi0, vc_uart3, _),
613         PIN(5, pdm, vc_i2c3, arm_jtag, sd_card_e, vc_spi4, pkt, vc_pcm, vc_i2c5),
614         PIN(6, pdm, vc_i2c3, arm_jtag, sd_card_e, vc_spi4, pkt, vc_pcm, vc_i2c5),
615         PIN(7, i2s_out, spdif_out, arm_jtag, sd_card_e, vc_i2c3, enet0_rgmii, vc_pcm, vc_spi4),
616         PIN(8, i2s_out, aud_fs_clk0, arm_jtag, sd_card_e, vc_i2c3, enet0_mii, vc_pcm, vc_spi4),
617         PIN(9, i2s_out, aud_fs_clk0, arm_jtag, sd_card_e, enet0_mii, sd_card_c, vc_spi4, _),
618         PIN(10, bsc_m3, mtsif_alt1, i2s_in, i2s_out, vc_spi5, enet0_mii, sd_card_c, vc_spi4),
619         PIN(11, bsc_m3, mtsif_alt1, i2s_in, i2s_out, vc_spi5, enet0_mii, sd_card_c, vc_spi4),
620         PIN(12, spi_s, mtsif_alt1, i2s_in, i2s_out, vc_spi5, vc_i2csl, sd0, sd_card_d),
621         PIN(13, spi_s, mtsif_alt1, i2s_out, usb_vbus, vc_spi5, vc_i2csl, sd0, sd_card_d),
622         PIN(14, spi_s, vc_i2csl, enet0_rgmii, arm_jtag, vc_spi5, vc_pwm0, vc_i2c4, sd_card_d),
623         PIN(15, spi_s, vc_i2csl, vc_spi3, arm_jtag, vc_pwm0, vc_i2c4, gpclk0, _),
624         PIN(16, sd_card_b, i2s_out, vc_spi3, i2s_in, sd0, enet0_rgmii, gpclk1, _),
625         PIN(17, sd_card_b, i2s_out, vc_spi3, i2s_in, ext_sc_clk, sd0, enet0_rgmii, gpclk2),
626         PIN(18, sd_card_b, i2s_out, vc_spi3, i2s_in, sd0, enet0_rgmii, vc_pwm1, _),
627         PIN(19, sd_card_b, usb_pwr, vc_spi3, pkt, spdif_out, sd0, ir_in, vc_pwm1),
628         PIN(20, sd_card_b, uui, vc_uart0, arm_jtag, uart2, usb_pwr, vc_pcm, vc_uart4),
629         PIN(21, usb_pwr, uui, vc_uart0, arm_jtag, uart2, sd_card_b, vc_pcm, vc_uart4),
630         PIN(22, usb_pwr, enet0, vc_uart0, mtsif, uart2, usb_vbus, vc_pcm, vc_i2c5),
631         PIN(23, usb_vbus, enet0, vc_uart0, mtsif, uart2, i2s_out, vc_pcm, vc_i2c5),
632         PIN(24, mtsif, pkt, uart0, enet0_rgmii, enet0_rgmii, vc_i2c4, vc_uart3, _),
633         PIN(25, mtsif, pkt, sc0, uart0, enet0_rgmii, enet0_rgmii, vc_i2c4, vc_uart3),
634         PIN(26, mtsif, pkt, sc0, uart0, enet0_rgmii, vc_uart4, vc_spi5, _),
635         PIN(27, mtsif, pkt, sc0, uart0, enet0_rgmii, vc_uart4, vc_spi5, _),
636         PIN(28, mtsif, pkt, sc0, enet0_rgmii, vc_uart4, vc_spi5, _, _),
637         PIN(29, mtsif, pkt, sc0, enet0_rgmii, vc_uart4, vc_spi5, _, _),
638         PIN(30, mtsif, pkt, sc0, sd2, enet0_rgmii, gpclk0, vc_pwm0, _),
639         PIN(31, mtsif, pkt, sc0, sd2, enet0_rgmii, vc_spi3, vc_pwm0, _),
640         PIN(32, mtsif, pkt, sc0, sd2, enet0_rgmii, vc_spi3, vc_uart3, _),
641         PIN(33, mtsif, pkt, sd2, enet0_rgmii, vc_spi3, vc_uart3, _, _),
642         PIN(34, mtsif, pkt, ext_sc_clk, sd2, enet0_rgmii, vc_spi3, vc_i2c5, _),
643         PIN(35, mtsif, pkt, sd2, enet0_rgmii, vc_spi3, vc_i2c5, _, _),
644         PIN(36, sd0, mtsif, sc0, i2s_in, vc_uart3, vc_uart2, _, _),
645         PIN(37, sd0, mtsif, sc0, vc_spi0, i2s_in, vc_uart3, vc_uart2, _),
646         PIN(38, sd0, mtsif_alt, sc0, vc_spi0, i2s_in, vc_uart3, vc_uart2, _),
647         PIN(39, sd0, mtsif_alt, sc0, vc_spi0, vc_uart3, vc_uart2, _, _),
648         PIN(40, sd0, mtsif_alt, sc0, vc_spi0, bsc_m3, _, _, _),
649         PIN(41, sd0, mtsif_alt, sc0, vc_spi0, bsc_m3, _, _, _),
650         PIN(42, vc_spi0, mtsif_alt, vc_i2c0, sd_card_a, mtsif_alt1, arm_jtag, pdm, spi_m),
651         PIN(43, vc_spi0, mtsif_alt, vc_i2c0, sd_card_a, mtsif_alt1, arm_jtag, pdm, spi_m),
652         PIN(44, vc_spi0, mtsif_alt, enet0, sd_card_a, mtsif_alt1, arm_jtag, pdm, spi_m),
653         PIN(45, vc_spi0, mtsif_alt, enet0, sd_card_a, mtsif_alt1, arm_jtag, pdm, spi_m),
654         PIN(46, vc_spi0, mtsif_alt, sd_card_a, mtsif_alt1, arm_jtag, pdm, spi_m, _),
655         PIN(47, enet0, mtsif_alt, i2s_out, mtsif_alt1, arm_jtag, _, _, _),
656         PIN(48, sc0, usb_pwr, spdif_out, mtsif, _, _, _, _),
657         PIN(49, sc0, usb_pwr, aud_fs_clk0, mtsif, _, _, _, _),
658         PIN(50, sc0, usb_vbus, sc0, _, _, _, _, _),
659         PIN(51, sc0, enet0, sc0, sr_edm_sense, _, _, _, _),
660         PIN(52, sc0, enet0, vc_pwm1, _, _, _, _, _),
661         PIN(53, sc0, enet0_rgmii, ext_sc_clk, _, _, _, _, _),
662 };
663
664 static const struct bcm2712_pin_funcs bcm2712_d0_aon_gpio_pin_funcs[] = {
665         PIN(0, ir_in, vc_spi0, vc_uart0, vc_i2c3, uart0, vc_i2c0, _, _),
666         PIN(1, vc_pwm0, vc_spi0, vc_uart0, vc_i2c3, uart0, aon_pwm, vc_i2c0, vc_pwm1),
667         PIN(2, vc_pwm0, vc_spi0, vc_uart0, ctl_hdmi_5v, uart0, aon_pwm, ir_in, vc_pwm1),
668         PIN(3, ir_in, vc_spi0, vc_uart0, uart0, sd_card_g, aon_gpclk, _, _),
669         PIN(4, gpclk0, vc_spi0, pm_led_out, aon_pwm, sd_card_g, vc_pwm0, _, _),
670         PIN(5, gpclk1, ir_in, aon_pwm, sd_card_g, vc_pwm0, _, _, _),
671         PIN(6, uart1, vc_uart2, ctl_hdmi_5v, gpclk2, vc_spi3, _, _, _),
672         PIN(7, _, _, _, _, _, _, _, _),
673         PIN(8, uart1, vc_uart2, ctl_hdmi_5v, vc_spi0, vc_spi3, _, _, _),
674         PIN(9, uart1, vc_uart2, vc_uart0, aon_pwm, vc_spi0, vc_uart2, vc_spi3, _),
675         PIN(10, _, _, _, _, _, _, _, _),
676         PIN(11, _, _, _, _, _, _, _, _),
677         PIN(12, uart1, vc_uart2, vc_uart0, vc_spi0, usb_pwr, vc_uart2, vc_spi3, _),
678         PIN(13, bsc_m1, vc_uart0, uui, vc_spi0, arm_jtag, vc_uart2, vc_i2c3, _),
679         PIN(14, bsc_m1, aon_gpclk, vc_uart0, uui, vc_spi0, arm_jtag, vc_uart2, vc_i2c3),
680 };
681
682 static const struct bcm2712_pin_funcs bcm2712_d0_aon_sgpio_pin_funcs[] = {
683         PIN(0, hdmi_tx0_bsc, hdmi_tx0_auto_i2c, bsc_m0, vc_i2c0, _, _, _, _),
684         PIN(1, hdmi_tx0_bsc, hdmi_tx0_auto_i2c, bsc_m0, vc_i2c0, _, _, _, _),
685         PIN(2, hdmi_tx1_bsc, hdmi_tx1_auto_i2c, bsc_m1, vc_i2c0, ctl_hdmi_5v, _, _, _),
686         PIN(3, hdmi_tx1_bsc, hdmi_tx1_auto_i2c, bsc_m1, vc_i2c0, _, _, _, _),
687         PIN(4, avs_pmu_bsc, bsc_m2, vc_i2c3, ctl_hdmi_5v, _, _, _, _),
688         PIN(5, avs_pmu_bsc, bsc_m2, vc_i2c3, _, _, _, _, _),
689 };
690
691 static const struct bcm2712_pin_funcs bcm2712_d0_gpio_pin_funcs[] = {
692         PIN(1, vc_i2c0, usb_pwr, gpclk0, sd_card_e, vc_spi3, sr_edm_sense, vc_spi0, vc_uart0),
693         PIN(2, vc_i2c0, usb_pwr, gpclk1, sd_card_e, vc_spi3, clk_observe, vc_spi0, vc_uart0),
694         PIN(3, vc_i2c3, usb_vbus, gpclk2, sd_card_e, vc_spi3, vc_spi0, vc_uart0, _),
695         PIN(4, vc_i2c3, vc_pwm1, vc_spi3, sd_card_e, vc_spi3, vc_spi0, vc_uart0, _),
696         PIN(10, bsc_m3, vc_pwm1, vc_spi3, sd_card_e, vc_spi3, gpclk0, _, _),
697         PIN(11, bsc_m3, vc_spi3, clk_observe, sd_card_c, gpclk1, _, _, _),
698         PIN(12, spi_s, vc_spi3, sd_card_c, sd_card_d, _, _, _, _),
699         PIN(13, spi_s, vc_spi3, sd_card_c, sd_card_d, _, _, _, _),
700         PIN(14, spi_s, uui, arm_jtag, vc_pwm0, vc_i2c0, sd_card_d, _, _),
701         PIN(15, spi_s, uui, arm_jtag, vc_pwm0, vc_i2c0, gpclk0, _, _),
702         PIN(18, sd_card_f, vc_pwm1, _, _, _, _, _, _),
703         PIN(19, sd_card_f, usb_pwr, vc_pwm1, _, _, _, _, _),
704         PIN(20, vc_i2c3, uui, vc_uart0, arm_jtag, vc_uart2, _, _, _),
705         PIN(21, vc_i2c3, uui, vc_uart0, arm_jtag, vc_uart2, _, _, _),
706         PIN(22, sd_card_f, vc_uart0, vc_i2c3, _, _, _, _, _),
707         PIN(23, vc_uart0, vc_i2c3, _, _, _, _, _, _),
708         PIN(24, sd_card_b, vc_spi0, arm_jtag, uart0, usb_pwr, vc_uart2, vc_uart0, _),
709         PIN(25, sd_card_b, vc_spi0, arm_jtag, uart0, usb_pwr, vc_uart2, vc_uart0, _),
710         PIN(26, sd_card_b, vc_spi0, arm_jtag, uart0, usb_vbus, vc_uart2, vc_spi0, _),
711         PIN(27, sd_card_b, vc_spi0, arm_jtag, uart0, vc_uart2, vc_spi0, _, _),
712         PIN(28, sd_card_b, vc_spi0, arm_jtag, vc_i2c0, vc_spi0, _, _, _),
713         PIN(29, arm_jtag, vc_i2c0, vc_spi0, _, _, _, _, _),
714         PIN(30, sd2, gpclk0, vc_pwm0, _, _, _, _, _),
715         PIN(31, sd2, vc_spi3, vc_pwm0, _, _, _, _, _),
716         PIN(32, sd2, vc_spi3, vc_uart3, _, _, _, _, _),
717         PIN(33, sd2, vc_spi3, vc_uart3, _, _, _, _, _),
718         PIN(34, sd2, vc_spi3, vc_i2c5, _, _, _, _, _),
719         PIN(35, sd2, vc_spi3, vc_i2c5, _, _, _, _, _),
720 };
721
722 static inline u32 bcm2712_reg_rd(struct bcm2712_pinctrl *pc, unsigned reg)
723 {
724         return readl(pc->base + reg);
725 }
726
727 static inline void bcm2712_reg_wr(struct bcm2712_pinctrl *pc, unsigned reg,
728                 u32 val)
729 {
730         writel(val, pc->base + reg);
731 }
732
733 static enum bcm2712_funcs bcm2712_pinctrl_fsel_get(
734         struct bcm2712_pinctrl *pc, unsigned pin)
735 {
736         u32 bit = pc->pin_regs[pin].mux_bit;
737         enum bcm2712_funcs func;
738         int fsel;
739         u32 val;
740
741         if (!bit)
742                 return func_gpio;
743
744         val = bcm2712_reg_rd(pc, BIT_TO_REG(bit));
745         fsel = (val >> BIT_TO_SHIFT(bit)) & BCM2712_FSEL_MASK;
746         func = pc->pin_funcs[pin].funcs[fsel];
747         if (func >= func_count)
748                 func = (enum bcm2712_funcs)fsel;
749
750         dev_dbg(pc->dev, "get %04x: %08x (%u => %s)\n",
751                 BIT_TO_REG(bit), val, pin,
752                 bcm2712_func_names[func]);
753
754         return func;
755 }
756
757 static void bcm2712_pinctrl_fsel_set(
758         struct bcm2712_pinctrl *pc, unsigned pin,
759         enum bcm2712_funcs func)
760 {
761         u32 bit = pc->pin_regs[pin].mux_bit, val;
762         const u8 *pin_funcs;
763         unsigned long flags;
764         int fsel;
765         int cur;
766         int i;
767
768         if (!bit || func >= func_count)
769                 return;
770
771         fsel = BCM2712_FSEL_COUNT;
772
773         if (func >= BCM2712_FSEL_COUNT) {
774                 /* Convert to an fsel number */
775                 pin_funcs = pc->pin_funcs[pin].funcs;
776                 for (i = 1; i < BCM2712_FSEL_COUNT; i++) {
777                         if (pin_funcs[i - 1] == func) {
778                                 fsel = i;
779                                 break;
780                         }
781                 }
782         } else {
783                 fsel = (enum bcm2712_funcs)func;
784         }
785         if (fsel >= BCM2712_FSEL_COUNT)
786                 return;
787
788         spin_lock_irqsave(&pc->lock, flags);
789
790         val = bcm2712_reg_rd(pc, BIT_TO_REG(bit));
791         cur = (val >> BIT_TO_SHIFT(bit)) & BCM2712_FSEL_MASK;
792
793         dev_dbg(pc->dev, "read %04x: %08x (%u => %s)\n",
794                 BIT_TO_REG(bit), val, pin,
795                 bcm2712_func_names[cur]);
796
797         if (cur != fsel) {
798                 val &= ~(BCM2712_FSEL_MASK << BIT_TO_SHIFT(bit));
799                 val |= fsel << BIT_TO_SHIFT(bit);
800
801                 dev_dbg(pc->dev, "write %04x: %08x (%u <= %s)\n",
802                         BIT_TO_REG(bit), val, pin,
803                         bcm2712_func_names[fsel]);
804                 bcm2712_reg_wr(pc, BIT_TO_REG(bit), val);
805         }
806
807         spin_unlock_irqrestore(&pc->lock, flags);
808 }
809
810 static int bcm2712_pctl_get_groups_count(struct pinctrl_dev *pctldev)
811 {
812         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
813
814         return pc->pctl_desc.npins;
815 }
816
817 static const char *bcm2712_pctl_get_group_name(struct pinctrl_dev *pctldev,
818                 unsigned selector)
819 {
820         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
821
822         return pc->gpio_groups[selector];
823 }
824
825 static int bcm2712_pctl_get_group_pins(struct pinctrl_dev *pctldev,
826                 unsigned selector,
827                 const unsigned **pins,
828                 unsigned *num_pins)
829 {
830         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
831
832         *pins = &pc->pctl_desc.pins[selector].number;
833         *num_pins = 1;
834
835         return 0;
836 }
837
838 static void bcm2712_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
839                 struct seq_file *s,
840                 unsigned offset)
841 {
842         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
843         enum bcm2712_funcs fsel = bcm2712_pinctrl_fsel_get(pc, offset);
844         const char *fname = bcm2712_func_names[fsel];
845
846         seq_printf(s, "function %s", fname);
847 }
848
849 static void bcm2712_pctl_dt_free_map(struct pinctrl_dev *pctldev,
850                 struct pinctrl_map *maps, unsigned num_maps)
851 {
852         int i;
853
854         for (i = 0; i < num_maps; i++)
855                 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
856                         kfree(maps[i].data.configs.configs);
857
858         kfree(maps);
859 }
860
861 static const struct pinctrl_ops bcm2712_pctl_ops = {
862         .get_groups_count = bcm2712_pctl_get_groups_count,
863         .get_group_name = bcm2712_pctl_get_group_name,
864         .get_group_pins = bcm2712_pctl_get_group_pins,
865         .pin_dbg_show = bcm2712_pctl_pin_dbg_show,
866         .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
867         .dt_free_map = bcm2712_pctl_dt_free_map,
868 };
869
870 static int bcm2712_pmx_free(struct pinctrl_dev *pctldev,
871                 unsigned offset)
872 {
873         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
874
875         /* disable by setting to GPIO */
876         bcm2712_pinctrl_fsel_set(pc, offset, func_gpio);
877         return 0;
878 }
879
880 static int bcm2712_pmx_get_functions_count(struct pinctrl_dev *pctldev)
881 {
882         return func_count;
883 }
884
885 static const char *bcm2712_pmx_get_function_name(struct pinctrl_dev *pctldev,
886                 unsigned selector)
887 {
888         return (selector < func_count) ? bcm2712_func_names[selector] : NULL;
889 }
890
891 static int bcm2712_pmx_get_function_groups(struct pinctrl_dev *pctldev,
892                 unsigned selector,
893                 const char * const **groups,
894                 unsigned * const num_groups)
895 {
896         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
897         /* every pin can do every function */
898         *groups = pc->gpio_groups;
899         *num_groups = pc->pctl_desc.npins;
900
901         return 0;
902 }
903
904 static int bcm2712_pmx_set(struct pinctrl_dev *pctldev,
905                 unsigned func_selector,
906                 unsigned group_selector)
907 {
908         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
909
910         bcm2712_pinctrl_fsel_set(pc, group_selector, func_selector);
911
912         return 0;
913 }
914 static int bcm2712_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
915                                            struct pinctrl_gpio_range *range,
916                                            unsigned pin)
917 {
918         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
919
920         bcm2712_pinctrl_fsel_set(pc, pin, func_gpio);
921
922         return 0;
923 }
924
925 static void bcm2712_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
926                 struct pinctrl_gpio_range *range,
927                 unsigned offset)
928 {
929         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
930
931         /* disable by setting to GPIO */
932         bcm2712_pinctrl_fsel_set(pc, offset, func_gpio);
933 }
934
935 static const struct pinmux_ops bcm2712_pmx_ops = {
936         .free = bcm2712_pmx_free,
937         .get_functions_count = bcm2712_pmx_get_functions_count,
938         .get_function_name = bcm2712_pmx_get_function_name,
939         .get_function_groups = bcm2712_pmx_get_function_groups,
940         .set_mux = bcm2712_pmx_set,
941         .gpio_request_enable = bcm2712_pmx_gpio_request_enable,
942         .gpio_disable_free = bcm2712_pmx_gpio_disable_free,
943 };
944
945 static unsigned int bcm2712_pull_config_get(struct bcm2712_pinctrl *pc,
946                                             unsigned int pin)
947 {
948         u32 bit = pc->pin_regs[pin].pad_bit, val;
949
950         if (unlikely(bit == REG_BIT_INVALID))
951             return BCM2712_PULL_NONE;
952
953         val = bcm2712_reg_rd(pc, BIT_TO_REG(bit));
954         return (val >> BIT_TO_SHIFT(bit)) & BCM2712_PULL_MASK;
955 }
956
957 static void bcm2712_pull_config_set(struct bcm2712_pinctrl *pc,
958                                     unsigned int pin, unsigned int arg)
959 {
960         u32 bit = pc->pin_regs[pin].pad_bit, val;
961         unsigned long flags;
962
963         if (unlikely(bit == REG_BIT_INVALID)) {
964             dev_warn(pc->dev, "can't set pulls for %s\n", pc->gpio_groups[pin]);
965             return;
966         }
967
968         spin_lock_irqsave(&pc->lock, flags);
969
970         val = bcm2712_reg_rd(pc, BIT_TO_REG(bit));
971         val &= ~(BCM2712_PULL_MASK << BIT_TO_SHIFT(bit));
972         val |= (arg << BIT_TO_SHIFT(bit));
973         bcm2712_reg_wr(pc, BIT_TO_REG(bit), val);
974
975         spin_unlock_irqrestore(&pc->lock, flags);
976 }
977
978 static int bcm2712_pinconf_get(struct pinctrl_dev *pctldev,
979                         unsigned pin, unsigned long *config)
980 {
981         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
982         enum pin_config_param param = pinconf_to_config_param(*config);
983         u32 arg;
984
985         switch (param) {
986         case PIN_CONFIG_BIAS_DISABLE:
987                 arg = (bcm2712_pull_config_get(pc, pin) == BCM2712_PULL_NONE);
988                 break;
989         case PIN_CONFIG_BIAS_PULL_DOWN:
990                 arg = (bcm2712_pull_config_get(pc, pin) == BCM2712_PULL_DOWN);
991                 break;
992         case PIN_CONFIG_BIAS_PULL_UP:
993                 arg = (bcm2712_pull_config_get(pc, pin) == BCM2712_PULL_UP);
994                 break;
995         default:
996                 return -ENOTSUPP;
997         }
998
999         *config = pinconf_to_config_packed(param, arg);
1000
1001         return -ENOTSUPP;
1002 }
1003
1004 static int bcm2712_pinconf_set(struct pinctrl_dev *pctldev,
1005                                unsigned int pin, unsigned long *configs,
1006                                unsigned int num_configs)
1007 {
1008         struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
1009         u32 param, arg;
1010         int i;
1011
1012         for (i = 0; i < num_configs; i++) {
1013                 param = pinconf_to_config_param(configs[i]);
1014                 arg = pinconf_to_config_argument(configs[i]);
1015
1016                 switch (param) {
1017                 case PIN_CONFIG_BIAS_DISABLE:
1018                         bcm2712_pull_config_set(pc, pin, BCM2712_PULL_NONE);
1019                         break;
1020                 case PIN_CONFIG_BIAS_PULL_DOWN:
1021                         bcm2712_pull_config_set(pc, pin, BCM2712_PULL_DOWN);
1022                         break;
1023                 case PIN_CONFIG_BIAS_PULL_UP:
1024                         bcm2712_pull_config_set(pc, pin, BCM2712_PULL_UP);
1025                         break;
1026                 default:
1027                         return -ENOTSUPP;
1028                 }
1029         } /* for each config */
1030
1031         return 0;
1032 }
1033
1034 static const struct pinconf_ops bcm2712_pinconf_ops = {
1035         .is_generic = true,
1036         .pin_config_get = bcm2712_pinconf_get,
1037         .pin_config_set = bcm2712_pinconf_set,
1038 };
1039
1040 static const struct pinctrl_desc bcm2712_c0_pinctrl_desc = {
1041         .name = "pinctrl-bcm2712",
1042         .pins = bcm2712_c0_gpio_pins,
1043         .npins = ARRAY_SIZE(bcm2712_c0_gpio_pins),
1044         .pctlops = &bcm2712_pctl_ops,
1045         .pmxops = &bcm2712_pmx_ops,
1046         .confops = &bcm2712_pinconf_ops,
1047         .owner = THIS_MODULE,
1048 };
1049
1050 static const struct pinctrl_desc bcm2712_c0_aon_pinctrl_desc = {
1051         .name = "aon-pinctrl-bcm2712",
1052         .pins = bcm2712_c0_aon_gpio_pins,
1053         .npins = ARRAY_SIZE(bcm2712_c0_aon_gpio_pins),
1054         .pctlops = &bcm2712_pctl_ops,
1055         .pmxops = &bcm2712_pmx_ops,
1056         .confops = &bcm2712_pinconf_ops,
1057         .owner = THIS_MODULE,
1058 };
1059
1060 static const struct pinctrl_desc bcm2712_d0_pinctrl_desc = {
1061         .name = "pinctrl-bcm2712",
1062         .pins = bcm2712_d0_gpio_pins,
1063         .npins = ARRAY_SIZE(bcm2712_d0_gpio_pins),
1064         .pctlops = &bcm2712_pctl_ops,
1065         .pmxops = &bcm2712_pmx_ops,
1066         .confops = &bcm2712_pinconf_ops,
1067         .owner = THIS_MODULE,
1068 };
1069
1070 static const struct pinctrl_desc bcm2712_d0_aon_pinctrl_desc = {
1071         .name = "aon-pinctrl-bcm2712",
1072         .pins = bcm2712_d0_aon_gpio_pins,
1073         .npins = ARRAY_SIZE(bcm2712_d0_aon_gpio_pins),
1074         .pctlops = &bcm2712_pctl_ops,
1075         .pmxops = &bcm2712_pmx_ops,
1076         .confops = &bcm2712_pinconf_ops,
1077         .owner = THIS_MODULE,
1078 };
1079
1080 static const struct pinctrl_gpio_range bcm2712_c0_pinctrl_gpio_range = {
1081         .name = "pinctrl-bcm2712",
1082         .npins = ARRAY_SIZE(bcm2712_c0_gpio_pins),
1083 };
1084
1085 static const struct pinctrl_gpio_range bcm2712_c0_aon_pinctrl_gpio_range = {
1086         .name = "aon-pinctrl-bcm2712",
1087         .npins = ARRAY_SIZE(bcm2712_c0_aon_gpio_pins),
1088 };
1089
1090 static const struct pinctrl_gpio_range bcm2712_d0_pinctrl_gpio_range = {
1091         .name = "pinctrl-bcm2712",
1092         .npins = ARRAY_SIZE(bcm2712_d0_gpio_pins),
1093 };
1094
1095 static const struct pinctrl_gpio_range bcm2712_d0_aon_pinctrl_gpio_range = {
1096         .name = "aon-pinctrl-bcm2712",
1097         .npins = ARRAY_SIZE(bcm2712_d0_aon_gpio_pins),
1098 };
1099
1100 static const struct bcm_plat_data bcm2712_c0_plat_data = {
1101         .pctl_desc = &bcm2712_c0_pinctrl_desc,
1102         .gpio_range = &bcm2712_c0_pinctrl_gpio_range,
1103         .pin_regs = bcm2712_c0_gpio_pin_regs,
1104         .pin_funcs = bcm2712_c0_gpio_pin_funcs,
1105 };
1106
1107 static const struct bcm_plat_data bcm2712_c0_aon_plat_data = {
1108         .pctl_desc = &bcm2712_c0_aon_pinctrl_desc,
1109         .gpio_range = &bcm2712_c0_aon_pinctrl_gpio_range,
1110         .pin_regs = bcm2712_c0_aon_gpio_pin_regs,
1111         .pin_funcs = bcm2712_c0_aon_gpio_pin_funcs,
1112 };
1113
1114 static const struct bcm_plat_data bcm2712_d0_plat_data = {
1115         .pctl_desc = &bcm2712_d0_pinctrl_desc,
1116         .gpio_range = &bcm2712_d0_pinctrl_gpio_range,
1117         .pin_regs = bcm2712_d0_gpio_pin_regs,
1118         .pin_funcs = bcm2712_d0_gpio_pin_funcs,
1119 };
1120
1121 static const struct bcm_plat_data bcm2712_d0_aon_plat_data = {
1122         .pctl_desc = &bcm2712_d0_aon_pinctrl_desc,
1123         .gpio_range = &bcm2712_d0_aon_pinctrl_gpio_range,
1124         .pin_regs = bcm2712_d0_aon_gpio_pin_regs,
1125         .pin_funcs = bcm2712_d0_aon_gpio_pin_funcs,
1126 };
1127
1128 static const struct of_device_id bcm2712_pinctrl_match[] = {
1129         {
1130                 .compatible = "brcm,bcm2712-pinctrl",
1131                 .data = &bcm2712_c0_plat_data,
1132         },
1133         {
1134                 .compatible = "brcm,bcm2712-aon-pinctrl",
1135                 .data = &bcm2712_c0_aon_plat_data,
1136         },
1137
1138         {
1139                 .compatible = "brcm,bcm2712c0-pinctrl",
1140                 .data = &bcm2712_c0_plat_data,
1141         },
1142         {
1143                 .compatible = "brcm,bcm2712c0-aon-pinctrl",
1144                 .data = &bcm2712_c0_aon_plat_data,
1145         },
1146
1147         {
1148                 .compatible = "brcm,bcm2712d0-pinctrl",
1149                 .data = &bcm2712_d0_plat_data,
1150         },
1151         {
1152                 .compatible = "brcm,bcm2712d0-aon-pinctrl",
1153                 .data = &bcm2712_d0_aon_plat_data,
1154         },
1155         {}
1156 };
1157
1158 static int bcm2712_pinctrl_probe(struct platform_device *pdev)
1159 {
1160         struct device *dev = &pdev->dev;
1161         struct device_node *np = dev->of_node;
1162         const struct bcm_plat_data *pdata;
1163         const struct of_device_id *match;
1164         struct bcm2712_pinctrl *pc;
1165         const char **names;
1166         int num_pins, i;
1167
1168         match = of_match_node(bcm2712_pinctrl_match, np);
1169         if (!match)
1170                 return -EINVAL;
1171         pdata = match->data;
1172
1173         pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
1174         if (!pc)
1175                 return -ENOMEM;
1176
1177         platform_set_drvdata(pdev, pc);
1178         pc->dev = dev;
1179         spin_lock_init(&pc->lock);
1180
1181         pc->base = devm_of_iomap(dev, np, 0, NULL);
1182         if (IS_ERR(pc->base)) {
1183                 dev_err(dev, "could not get IO memory\n");
1184                 return PTR_ERR(pc->base);
1185         }
1186
1187         pc->pctl_desc = *pdata->pctl_desc;
1188         num_pins = pc->pctl_desc.npins;
1189         names = devm_kmalloc_array(dev, num_pins, sizeof(const char *),
1190                                    GFP_KERNEL);
1191         if (!names)
1192                 return -ENOMEM;
1193         for (i = 0; i < num_pins; i++)
1194                 names[i] = pc->pctl_desc.pins[i].name;
1195         pc->gpio_groups = names;
1196         pc->pin_regs = pdata->pin_regs;
1197         pc->pin_funcs = pdata->pin_funcs;
1198         pc->pctl_dev = devm_pinctrl_register(dev, &pc->pctl_desc, pc);
1199         if (IS_ERR(pc->pctl_dev))
1200                 return PTR_ERR(pc->pctl_dev);
1201
1202         pc->gpio_range = *pdata->gpio_range;
1203         pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
1204
1205         return 0;
1206 }
1207
1208 static struct platform_driver bcm2712_pinctrl_driver = {
1209         .probe = bcm2712_pinctrl_probe,
1210         .driver = {
1211                 .name = MODULE_NAME,
1212                 .of_match_table = bcm2712_pinctrl_match,
1213                 .suppress_bind_attrs = true,
1214         },
1215 };
1216 builtin_platform_driver(bcm2712_pinctrl_driver);