page_pool: remove PP_FLAG_PAGE_FRAG
[platform/kernel/linux-rpi.git] / drivers / pinctrl / pinctrl-rp1.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for Raspberry Pi RP1 GPIO unit (pinctrl + GPIO)
4  *
5  * Copyright (C) 2023 Raspberry Pi Ltd.
6  *
7  * This driver is inspired by:
8  * pinctrl-bcm2835.c, please see original file for copyright information
9  */
10
11 #include <linux/bitmap.h>
12 #include <linux/bitops.h>
13 #include <linux/bug.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/io.h>
19 #include <linux/irq.h>
20 #include <linux/irqdesc.h>
21 #include <linux/init.h>
22 #include <linux/of_address.h>
23 #include <linux/of.h>
24 #include <linux/of_irq.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/pinctrl/machine.h>
27 #include <linux/pinctrl/pinconf.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/pinctrl/pinconf-generic.h>
31 #include <linux/platform_device.h>
32 #include <linux/seq_file.h>
33 #include <linux/spinlock.h>
34 #include <linux/types.h>
35 #include "core.h"
36 #include "pinconf.h"
37 #include "pinctrl-utils.h"
38
39 #define MODULE_NAME "pinctrl-rp1"
40 #define RP1_NUM_GPIOS   54
41 #define RP1_NUM_BANKS   3
42
43 #define RP1_RW_OFFSET                   0x0000
44 #define RP1_XOR_OFFSET                  0x1000
45 #define RP1_SET_OFFSET                  0x2000
46 #define RP1_CLR_OFFSET                  0x3000
47
48 #define RP1_GPIO_STATUS                 0x0000
49 #define RP1_GPIO_CTRL                   0x0004
50
51 #define RP1_GPIO_PCIE_INTE              0x011c
52 #define RP1_GPIO_PCIE_INTS              0x0124
53
54 #define RP1_GPIO_EVENTS_SHIFT_RAW       20
55 #define RP1_GPIO_STATUS_FALLING         BIT(20)
56 #define RP1_GPIO_STATUS_RISING          BIT(21)
57 #define RP1_GPIO_STATUS_LOW             BIT(22)
58 #define RP1_GPIO_STATUS_HIGH            BIT(23)
59
60 #define RP1_GPIO_EVENTS_SHIFT_FILTERED  24
61 #define RP1_GPIO_STATUS_F_FALLING       BIT(24)
62 #define RP1_GPIO_STATUS_F_RISING        BIT(25)
63 #define RP1_GPIO_STATUS_F_LOW           BIT(26)
64 #define RP1_GPIO_STATUS_F_HIGH          BIT(27)
65
66 #define RP1_GPIO_CTRL_FUNCSEL_LSB       0
67 #define RP1_GPIO_CTRL_FUNCSEL_MASK      0x0000001f
68 #define RP1_GPIO_CTRL_OUTOVER_LSB       12
69 #define RP1_GPIO_CTRL_OUTOVER_MASK      0x00003000
70 #define RP1_GPIO_CTRL_OEOVER_LSB        14
71 #define RP1_GPIO_CTRL_OEOVER_MASK       0x0000c000
72 #define RP1_GPIO_CTRL_INOVER_LSB        16
73 #define RP1_GPIO_CTRL_INOVER_MASK       0x00030000
74 #define RP1_GPIO_CTRL_IRQEN_FALLING     BIT(20)
75 #define RP1_GPIO_CTRL_IRQEN_RISING      BIT(21)
76 #define RP1_GPIO_CTRL_IRQEN_LOW         BIT(22)
77 #define RP1_GPIO_CTRL_IRQEN_HIGH        BIT(23)
78 #define RP1_GPIO_CTRL_IRQEN_F_FALLING   BIT(24)
79 #define RP1_GPIO_CTRL_IRQEN_F_RISING    BIT(25)
80 #define RP1_GPIO_CTRL_IRQEN_F_LOW       BIT(26)
81 #define RP1_GPIO_CTRL_IRQEN_F_HIGH      BIT(27)
82 #define RP1_GPIO_CTRL_IRQRESET          BIT(28)
83 #define RP1_GPIO_CTRL_IRQOVER_LSB       30
84 #define RP1_GPIO_CTRL_IRQOVER_MASK      0xc0000000
85
86 #define RP1_INT_EDGE_FALLING            BIT(0)
87 #define RP1_INT_EDGE_RISING             BIT(1)
88 #define RP1_INT_LEVEL_LOW               BIT(2)
89 #define RP1_INT_LEVEL_HIGH              BIT(3)
90 #define RP1_INT_MASK                    0xf
91
92 #define RP1_INT_EDGE_BOTH               (RP1_INT_EDGE_FALLING | \
93                                          RP1_INT_EDGE_RISING)
94 #define RP1_PUD_OFF                     0
95 #define RP1_PUD_DOWN                    1
96 #define RP1_PUD_UP                      2
97
98 #define RP1_FSEL_COUNT                  9
99
100 #define RP1_FSEL_ALT0                   0x00
101 #define RP1_FSEL_GPIO                   0x05
102 #define RP1_FSEL_NONE                   0x09
103 #define RP1_FSEL_NONE_HW                0x1f
104
105 #define RP1_DIR_OUTPUT                  0
106 #define RP1_DIR_INPUT                   1
107
108 #define RP1_OUTOVER_PERI                0
109 #define RP1_OUTOVER_INVPERI             1
110 #define RP1_OUTOVER_LOW                 2
111 #define RP1_OUTOVER_HIGH                3
112
113 #define RP1_OEOVER_PERI                 0
114 #define RP1_OEOVER_INVPERI              1
115 #define RP1_OEOVER_DISABLE              2
116 #define RP1_OEOVER_ENABLE               3
117
118 #define RP1_INOVER_PERI                 0
119 #define RP1_INOVER_INVPERI              1
120 #define RP1_INOVER_LOW                  2
121 #define RP1_INOVER_HIGH                 3
122
123 #define RP1_RIO_OUT                     0x00
124 #define RP1_RIO_OE                      0x04
125 #define RP1_RIO_IN                      0x08
126
127 #define RP1_PAD_SLEWFAST_MASK           0x00000001
128 #define RP1_PAD_SLEWFAST_LSB            0
129 #define RP1_PAD_SCHMITT_MASK            0x00000002
130 #define RP1_PAD_SCHMITT_LSB             1
131 #define RP1_PAD_PULL_MASK               0x0000000c
132 #define RP1_PAD_PULL_LSB                2
133 #define RP1_PAD_DRIVE_MASK              0x00000030
134 #define RP1_PAD_DRIVE_LSB               4
135 #define RP1_PAD_IN_ENABLE_MASK          0x00000040
136 #define RP1_PAD_IN_ENABLE_LSB           6
137 #define RP1_PAD_OUT_DISABLE_MASK        0x00000080
138 #define RP1_PAD_OUT_DISABLE_LSB         7
139
140 #define RP1_PAD_DRIVE_2MA               0x00000000
141 #define RP1_PAD_DRIVE_4MA               0x00000010
142 #define RP1_PAD_DRIVE_8MA               0x00000020
143 #define RP1_PAD_DRIVE_12MA              0x00000030
144
145 #define FLD_GET(r, f) (((r) & (f ## _MASK)) >> (f ## _LSB))
146 #define FLD_SET(r, f, v) r = (((r) & ~(f ## _MASK)) | ((v) << (f ## _LSB)))
147
148 #define FUNC(f) \
149         [func_##f] = #f
150 #define RP1_MAX_FSEL 8
151 #define PIN(i, f0, f1, f2, f3, f4, f5, f6, f7, f8) \
152         [i] = { \
153                 .funcs = { \
154                         func_##f0, \
155                         func_##f1, \
156                         func_##f2, \
157                         func_##f3, \
158                         func_##f4, \
159                         func_##f5, \
160                         func_##f6, \
161                         func_##f7, \
162                         func_##f8, \
163                 }, \
164         }
165
166 #define LEGACY_MAP(n, f0, f1, f2, f3, f4, f5) \
167         [n] = { \
168                 func_gpio, \
169                 func_gpio, \
170                 func_##f5, \
171                 func_##f4, \
172                 func_##f0, \
173                 func_##f1, \
174                 func_##f2, \
175                 func_##f3, \
176         }
177
178 struct rp1_iobank_desc {
179         int min_gpio;
180         int num_gpios;
181         int gpio_offset;
182         int inte_offset;
183         int ints_offset;
184         int rio_offset;
185         int pads_offset;
186 };
187
188 struct rp1_pin_info {
189         u8 num;
190         u8 bank;
191         u8 offset;
192         u8 fsel;
193         u8 irq_type;
194
195         void __iomem *gpio;
196         void __iomem *rio;
197         void __iomem *inte;
198         void __iomem *ints;
199         void __iomem *pad;
200 };
201
202 enum funcs {
203         func_alt0,
204         func_alt1,
205         func_alt2,
206         func_alt3,
207         func_alt4,
208         func_gpio,
209         func_alt6,
210         func_alt7,
211         func_alt8,
212         func_none,
213         func_aaud,
214         func_dcd0,
215         func_dpi,
216         func_dsi0_te_ext,
217         func_dsi1_te_ext,
218         func_dsr0,
219         func_dtr0,
220         func_gpclk0,
221         func_gpclk1,
222         func_gpclk2,
223         func_gpclk3,
224         func_gpclk4,
225         func_gpclk5,
226         func_i2c0,
227         func_i2c1,
228         func_i2c2,
229         func_i2c3,
230         func_i2c4,
231         func_i2c5,
232         func_i2c6,
233         func_i2s0,
234         func_i2s1,
235         func_i2s2,
236         func_ir,
237         func_mic,
238         func_pcie_clkreq_n,
239         func_pio,
240         func_proc_rio,
241         func_pwm0,
242         func_pwm1,
243         func_ri0,
244         func_sd0,
245         func_sd1,
246         func_spi0,
247         func_spi1,
248         func_spi2,
249         func_spi3,
250         func_spi4,
251         func_spi5,
252         func_spi6,
253         func_spi7,
254         func_spi8,
255         func_uart0,
256         func_uart1,
257         func_uart2,
258         func_uart3,
259         func_uart4,
260         func_uart5,
261         func_vbus0,
262         func_vbus1,
263         func_vbus2,
264         func_vbus3,
265         func__,
266         func_count = func__,
267         func_invalid = func__,
268 };
269
270 struct rp1_pin_funcs {
271         u8 funcs[RP1_FSEL_COUNT];
272 };
273
274 struct rp1_pinctrl {
275         struct device *dev;
276         void __iomem *gpio_base;
277         void __iomem *rio_base;
278         void __iomem *pads_base;
279         int irq[RP1_NUM_BANKS];
280         struct rp1_pin_info pins[RP1_NUM_GPIOS];
281
282         struct pinctrl_dev *pctl_dev;
283         struct gpio_chip gpio_chip;
284         struct pinctrl_gpio_range gpio_range;
285
286         raw_spinlock_t irq_lock[RP1_NUM_BANKS];
287 };
288
289 const struct rp1_iobank_desc rp1_iobanks[RP1_NUM_BANKS] = {
290         /*         gpio   inte    ints     rio    pads */
291         {  0, 28, 0x0000, 0x011c, 0x0124, 0x0000, 0x0004 },
292         { 28,  6, 0x4000, 0x411c, 0x4124, 0x4000, 0x4004 },
293         { 34, 20, 0x8000, 0x811c, 0x8124, 0x8000, 0x8004 },
294 };
295
296 /* pins are just named GPIO0..GPIO53 */
297 #define RP1_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
298 static struct pinctrl_pin_desc rp1_gpio_pins[] = {
299         RP1_GPIO_PIN(0),
300         RP1_GPIO_PIN(1),
301         RP1_GPIO_PIN(2),
302         RP1_GPIO_PIN(3),
303         RP1_GPIO_PIN(4),
304         RP1_GPIO_PIN(5),
305         RP1_GPIO_PIN(6),
306         RP1_GPIO_PIN(7),
307         RP1_GPIO_PIN(8),
308         RP1_GPIO_PIN(9),
309         RP1_GPIO_PIN(10),
310         RP1_GPIO_PIN(11),
311         RP1_GPIO_PIN(12),
312         RP1_GPIO_PIN(13),
313         RP1_GPIO_PIN(14),
314         RP1_GPIO_PIN(15),
315         RP1_GPIO_PIN(16),
316         RP1_GPIO_PIN(17),
317         RP1_GPIO_PIN(18),
318         RP1_GPIO_PIN(19),
319         RP1_GPIO_PIN(20),
320         RP1_GPIO_PIN(21),
321         RP1_GPIO_PIN(22),
322         RP1_GPIO_PIN(23),
323         RP1_GPIO_PIN(24),
324         RP1_GPIO_PIN(25),
325         RP1_GPIO_PIN(26),
326         RP1_GPIO_PIN(27),
327         RP1_GPIO_PIN(28),
328         RP1_GPIO_PIN(29),
329         RP1_GPIO_PIN(30),
330         RP1_GPIO_PIN(31),
331         RP1_GPIO_PIN(32),
332         RP1_GPIO_PIN(33),
333         RP1_GPIO_PIN(34),
334         RP1_GPIO_PIN(35),
335         RP1_GPIO_PIN(36),
336         RP1_GPIO_PIN(37),
337         RP1_GPIO_PIN(38),
338         RP1_GPIO_PIN(39),
339         RP1_GPIO_PIN(40),
340         RP1_GPIO_PIN(41),
341         RP1_GPIO_PIN(42),
342         RP1_GPIO_PIN(43),
343         RP1_GPIO_PIN(44),
344         RP1_GPIO_PIN(45),
345         RP1_GPIO_PIN(46),
346         RP1_GPIO_PIN(47),
347         RP1_GPIO_PIN(48),
348         RP1_GPIO_PIN(49),
349         RP1_GPIO_PIN(50),
350         RP1_GPIO_PIN(51),
351         RP1_GPIO_PIN(52),
352         RP1_GPIO_PIN(53),
353 };
354
355 /* one pin per group */
356 static const char * const rp1_gpio_groups[] = {
357         "gpio0",
358         "gpio1",
359         "gpio2",
360         "gpio3",
361         "gpio4",
362         "gpio5",
363         "gpio6",
364         "gpio7",
365         "gpio8",
366         "gpio9",
367         "gpio10",
368         "gpio11",
369         "gpio12",
370         "gpio13",
371         "gpio14",
372         "gpio15",
373         "gpio16",
374         "gpio17",
375         "gpio18",
376         "gpio19",
377         "gpio20",
378         "gpio21",
379         "gpio22",
380         "gpio23",
381         "gpio24",
382         "gpio25",
383         "gpio26",
384         "gpio27",
385         "gpio28",
386         "gpio29",
387         "gpio30",
388         "gpio31",
389         "gpio32",
390         "gpio33",
391         "gpio34",
392         "gpio35",
393         "gpio36",
394         "gpio37",
395         "gpio38",
396         "gpio39",
397         "gpio40",
398         "gpio41",
399         "gpio42",
400         "gpio43",
401         "gpio44",
402         "gpio45",
403         "gpio46",
404         "gpio47",
405         "gpio48",
406         "gpio49",
407         "gpio50",
408         "gpio51",
409         "gpio52",
410         "gpio53",
411 };
412
413 static const char * const rp1_func_names[] = {
414         FUNC(alt0),
415         FUNC(alt1),
416         FUNC(alt2),
417         FUNC(alt3),
418         FUNC(alt4),
419         FUNC(gpio),
420         FUNC(alt6),
421         FUNC(alt7),
422         FUNC(alt8),
423         FUNC(none),
424         FUNC(aaud),
425         FUNC(dcd0),
426         FUNC(dpi),
427         FUNC(dsi0_te_ext),
428         FUNC(dsi1_te_ext),
429         FUNC(dsr0),
430         FUNC(dtr0),
431         FUNC(gpclk0),
432         FUNC(gpclk1),
433         FUNC(gpclk2),
434         FUNC(gpclk3),
435         FUNC(gpclk4),
436         FUNC(gpclk5),
437         FUNC(i2c0),
438         FUNC(i2c1),
439         FUNC(i2c2),
440         FUNC(i2c3),
441         FUNC(i2c4),
442         FUNC(i2c5),
443         FUNC(i2c6),
444         FUNC(i2s0),
445         FUNC(i2s1),
446         FUNC(i2s2),
447         FUNC(ir),
448         FUNC(mic),
449         FUNC(pcie_clkreq_n),
450         FUNC(pio),
451         FUNC(proc_rio),
452         FUNC(pwm0),
453         FUNC(pwm1),
454         FUNC(ri0),
455         FUNC(sd0),
456         FUNC(sd1),
457         FUNC(spi0),
458         FUNC(spi1),
459         FUNC(spi2),
460         FUNC(spi3),
461         FUNC(spi4),
462         FUNC(spi5),
463         FUNC(spi6),
464         FUNC(spi7),
465         FUNC(spi8),
466         FUNC(uart0),
467         FUNC(uart1),
468         FUNC(uart2),
469         FUNC(uart3),
470         FUNC(uart4),
471         FUNC(uart5),
472         FUNC(vbus0),
473         FUNC(vbus1),
474         FUNC(vbus2),
475         FUNC(vbus3),
476         [func_invalid] = "?"
477 };
478
479 static const struct rp1_pin_funcs rp1_gpio_pin_funcs[] = {
480         PIN(0, spi0, dpi, uart1, i2c0, _, gpio, proc_rio, pio, spi2),
481         PIN(1, spi0, dpi, uart1, i2c0, _, gpio, proc_rio, pio, spi2),
482         PIN(2, spi0, dpi, uart1, i2c1, ir, gpio, proc_rio, pio, spi2),
483         PIN(3, spi0, dpi, uart1, i2c1, ir, gpio, proc_rio, pio, spi2),
484         PIN(4, gpclk0, dpi, uart2, i2c2, ri0, gpio, proc_rio, pio, spi3),
485         PIN(5, gpclk1, dpi, uart2, i2c2, dtr0, gpio, proc_rio, pio, spi3),
486         PIN(6, gpclk2, dpi, uart2, i2c3, dcd0, gpio, proc_rio, pio, spi3),
487         PIN(7, spi0, dpi, uart2, i2c3, dsr0, gpio, proc_rio, pio, spi3),
488         PIN(8, spi0, dpi, uart3, i2c0, _, gpio, proc_rio, pio, spi4),
489         PIN(9, spi0, dpi, uart3, i2c0, _, gpio, proc_rio, pio, spi4),
490         PIN(10, spi0, dpi, uart3, i2c1, _, gpio, proc_rio, pio, spi4),
491         PIN(11, spi0, dpi, uart3, i2c1, _, gpio, proc_rio, pio, spi4),
492         PIN(12, pwm0, dpi, uart4, i2c2, aaud, gpio, proc_rio, pio, spi5),
493         PIN(13, pwm0, dpi, uart4, i2c2, aaud, gpio, proc_rio, pio, spi5),
494         PIN(14, pwm0, dpi, uart4, i2c3, uart0, gpio, proc_rio, pio, spi5),
495         PIN(15, pwm0, dpi, uart4, i2c3, uart0, gpio, proc_rio, pio, spi5),
496         PIN(16, spi1, dpi, dsi0_te_ext, _, uart0, gpio, proc_rio, pio, _),
497         PIN(17, spi1, dpi, dsi1_te_ext, _, uart0, gpio, proc_rio, pio, _),
498         PIN(18, spi1, dpi, i2s0, pwm0, i2s1, gpio, proc_rio, pio, gpclk1),
499         PIN(19, spi1, dpi, i2s0, pwm0, i2s1, gpio, proc_rio, pio, _),
500         PIN(20, spi1, dpi, i2s0, gpclk0, i2s1, gpio, proc_rio, pio, _),
501         PIN(21, spi1, dpi, i2s0, gpclk1, i2s1, gpio, proc_rio, pio, _),
502         PIN(22, sd0, dpi, i2s0, i2c3, i2s1, gpio, proc_rio, pio, _),
503         PIN(23, sd0, dpi, i2s0, i2c3, i2s1, gpio, proc_rio, pio, _),
504         PIN(24, sd0, dpi, i2s0, _, i2s1, gpio, proc_rio, pio, spi2),
505         PIN(25, sd0, dpi, i2s0, mic, i2s1, gpio, proc_rio, pio, spi3),
506         PIN(26, sd0, dpi, i2s0, mic, i2s1, gpio, proc_rio, pio, spi5),
507         PIN(27, sd0, dpi, i2s0, mic, i2s1, gpio, proc_rio, pio, spi1),
508         PIN(28, sd1, i2c4, i2s2, spi6, vbus0, gpio, proc_rio, _, _),
509         PIN(29, sd1, i2c4, i2s2, spi6, vbus0, gpio, proc_rio, _, _),
510         PIN(30, sd1, i2c5, i2s2, spi6, uart5, gpio, proc_rio, _, _),
511         PIN(31, sd1, i2c5, i2s2, spi6, uart5, gpio, proc_rio, _, _),
512         PIN(32, sd1, gpclk3, i2s2, spi6, uart5, gpio, proc_rio, _, _),
513         PIN(33, sd1, gpclk4, i2s2, spi6, uart5, gpio, proc_rio, _, _),
514         PIN(34, pwm1, gpclk3, vbus0, i2c4, mic, gpio, proc_rio, _, _),
515         PIN(35, spi8, pwm1, vbus0, i2c4, mic, gpio, proc_rio, _, _),
516         PIN(36, spi8, uart5, pcie_clkreq_n, i2c5, mic, gpio, proc_rio, _, _),
517         PIN(37, spi8, uart5, mic, i2c5, pcie_clkreq_n, gpio, proc_rio, _, _),
518         PIN(38, spi8, uart5, mic, i2c6, aaud, gpio, proc_rio, dsi0_te_ext, _),
519         PIN(39, spi8, uart5, mic, i2c6, aaud, gpio, proc_rio, dsi1_te_ext, _),
520         PIN(40, pwm1, uart5, i2c4, spi6, aaud, gpio, proc_rio, _, _),
521         PIN(41, pwm1, uart5, i2c4, spi6, aaud, gpio, proc_rio, _, _),
522         PIN(42, gpclk5, uart5, vbus1, spi6, i2s2, gpio, proc_rio, _, _),
523         PIN(43, gpclk4, uart5, vbus1, spi6, i2s2, gpio, proc_rio, _, _),
524         PIN(44, gpclk5, i2c5, pwm1, spi6, i2s2, gpio, proc_rio, _, _),
525         PIN(45, pwm1, i2c5, spi7, spi6, i2s2, gpio, proc_rio, _, _),
526         PIN(46, gpclk3, i2c4, spi7, mic, i2s2, gpio, proc_rio, dsi0_te_ext, _),
527         PIN(47, gpclk5, i2c4, spi7, mic, i2s2, gpio, proc_rio, dsi1_te_ext, _),
528         PIN(48, pwm1, pcie_clkreq_n, spi7, mic, uart5, gpio, proc_rio, _, _),
529         PIN(49, spi8, spi7, i2c5, aaud, uart5, gpio, proc_rio, _, _),
530         PIN(50, spi8, spi7, i2c5, aaud, vbus2, gpio, proc_rio, _, _),
531         PIN(51, spi8, spi7, i2c6, aaud, vbus2, gpio, proc_rio, _, _),
532         PIN(52, spi8, _, i2c6, aaud, vbus3, gpio, proc_rio, _, _),
533         PIN(53, spi8, spi7, _, pcie_clkreq_n, vbus3, gpio, proc_rio, _, _),
534 };
535
536 static const u8 legacy_fsel_map[][8] = {
537         LEGACY_MAP(0, i2c0, _, dpi, spi2, uart1, _),
538         LEGACY_MAP(1, i2c0, _, dpi, spi2, uart1, _),
539         LEGACY_MAP(2, i2c1, _, dpi, spi2, uart1, _),
540         LEGACY_MAP(3, i2c1, _, dpi, spi2, uart1, _),
541         LEGACY_MAP(4, gpclk0, _, dpi, spi3, uart2, i2c2),
542         LEGACY_MAP(5, gpclk1, _, dpi, spi3, uart2, i2c2),
543         LEGACY_MAP(6, gpclk2, _, dpi, spi3, uart2, i2c3),
544         LEGACY_MAP(7, spi0, _, dpi, spi3, uart2, i2c3),
545         LEGACY_MAP(8, spi0, _, dpi, _, uart3, i2c0),
546         LEGACY_MAP(9, spi0, _, dpi, _, uart3, i2c0),
547         LEGACY_MAP(10, spi0, _, dpi, _, uart3, i2c1),
548         LEGACY_MAP(11, spi0, _, dpi, _, uart3, i2c1),
549         LEGACY_MAP(12, pwm0, _, dpi, spi5, uart4, i2c2),
550         LEGACY_MAP(13, pwm0, _, dpi, spi5, uart4, i2c2),
551         LEGACY_MAP(14, uart0, _, dpi, spi5, uart4, _),
552         LEGACY_MAP(15, uart0, _, dpi, spi5, uart4, _),
553         LEGACY_MAP(16, _, _, dpi, uart0, spi1, _),
554         LEGACY_MAP(17, _, _, dpi, uart0, spi1, _),
555         LEGACY_MAP(18, i2s0, _, dpi, _, spi1, pwm0),
556         LEGACY_MAP(19, i2s0, _, dpi, _, spi1, pwm0),
557         LEGACY_MAP(20, i2s0, _, dpi, _, spi1, gpclk0),
558         LEGACY_MAP(21, i2s0, _, dpi, _, spi1, gpclk1),
559         LEGACY_MAP(22, sd0, _, dpi, _, _, i2c3),
560         LEGACY_MAP(23, sd0, _, dpi, _, _, i2c3),
561         LEGACY_MAP(24, sd0, _, dpi, _, _, spi2),
562         LEGACY_MAP(25, sd0, _, dpi, _, _, spi3),
563         LEGACY_MAP(26, sd0, _, dpi, _, _, spi5),
564         LEGACY_MAP(27, sd0, _, dpi, _, _, _),
565 };
566
567 static const char * const irq_type_names[] = {
568         [IRQ_TYPE_NONE] = "none",
569         [IRQ_TYPE_EDGE_RISING] = "edge-rising",
570         [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
571         [IRQ_TYPE_EDGE_BOTH] = "edge-both",
572         [IRQ_TYPE_LEVEL_HIGH] = "level-high",
573         [IRQ_TYPE_LEVEL_LOW] = "level-low",
574 };
575
576 static int rp1_pinconf_set(struct pinctrl_dev *pctldev,
577                            unsigned int offset, unsigned long *configs,
578                            unsigned int num_configs);
579
580 static struct rp1_pin_info *rp1_get_pin(struct gpio_chip *chip,
581                                         unsigned int offset)
582 {
583         struct rp1_pinctrl *pc = gpiochip_get_data(chip);
584
585         if (pc && offset < RP1_NUM_GPIOS)
586                 return &pc->pins[offset];
587         return NULL;
588 }
589
590 static struct rp1_pin_info *rp1_get_pin_pctl(struct pinctrl_dev *pctldev,
591                                              unsigned int offset)
592 {
593         struct rp1_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
594
595         if (pc && offset < RP1_NUM_GPIOS)
596                 return &pc->pins[offset];
597         return NULL;
598 }
599
600 static void rp1_pad_update(struct rp1_pin_info *pin, u32 clr, u32 set)
601 {
602         u32 padctrl = readl(pin->pad);
603
604         padctrl &= ~clr;
605         padctrl |= set;
606
607         writel(padctrl, pin->pad);
608 }
609
610 static void rp1_input_enable(struct rp1_pin_info *pin, int value)
611 {
612         rp1_pad_update(pin, RP1_PAD_IN_ENABLE_MASK,
613                        value ? RP1_PAD_IN_ENABLE_MASK : 0);
614 }
615
616 static void rp1_output_enable(struct rp1_pin_info *pin, int value)
617 {
618         rp1_pad_update(pin, RP1_PAD_OUT_DISABLE_MASK,
619                        value ? 0 : RP1_PAD_OUT_DISABLE_MASK);
620 }
621
622 static u32 rp1_get_fsel(struct rp1_pin_info *pin)
623 {
624         u32 ctrl = readl(pin->gpio + RP1_GPIO_CTRL);
625         u32 oeover = FLD_GET(ctrl, RP1_GPIO_CTRL_OEOVER);
626         u32 fsel = FLD_GET(ctrl, RP1_GPIO_CTRL_FUNCSEL);
627
628         if (oeover != RP1_OEOVER_PERI || fsel >= RP1_FSEL_COUNT)
629                 fsel = RP1_FSEL_NONE;
630
631         return fsel;
632 }
633
634 static void rp1_set_fsel(struct rp1_pin_info *pin, u32 fsel)
635 {
636         u32 ctrl = readl(pin->gpio + RP1_GPIO_CTRL);
637
638         if (fsel >= RP1_FSEL_COUNT)
639                 fsel = RP1_FSEL_NONE_HW;
640
641         rp1_input_enable(pin, 1);
642         rp1_output_enable(pin, 1);
643
644         if (fsel == RP1_FSEL_NONE) {
645                 FLD_SET(ctrl, RP1_GPIO_CTRL_OEOVER, RP1_OEOVER_DISABLE);
646         } else {
647                 FLD_SET(ctrl, RP1_GPIO_CTRL_OUTOVER, RP1_OUTOVER_PERI);
648                 FLD_SET(ctrl, RP1_GPIO_CTRL_OEOVER, RP1_OEOVER_PERI);
649         }
650         FLD_SET(ctrl, RP1_GPIO_CTRL_FUNCSEL, fsel);
651         writel(ctrl, pin->gpio + RP1_GPIO_CTRL);
652 }
653
654 static int rp1_get_dir(struct rp1_pin_info *pin)
655 {
656         return !(readl(pin->rio + RP1_RIO_OE) & (1 << pin->offset)) ?
657                 RP1_DIR_INPUT : RP1_DIR_OUTPUT;
658 }
659
660 static void rp1_set_dir(struct rp1_pin_info *pin, bool is_input)
661 {
662         int offset = is_input ? RP1_CLR_OFFSET : RP1_SET_OFFSET;
663
664         writel(1 << pin->offset, pin->rio + RP1_RIO_OE + offset);
665 }
666
667 static int rp1_get_value(struct rp1_pin_info *pin)
668 {
669         return !!(readl(pin->rio + RP1_RIO_IN) & (1 << pin->offset));
670 }
671
672 static void rp1_set_value(struct rp1_pin_info *pin, int value)
673 {
674         /* Assume the pin is already an output */
675         writel(1 << pin->offset,
676                pin->rio + RP1_RIO_OUT + (value ? RP1_SET_OFFSET : RP1_CLR_OFFSET));
677 }
678
679 static int rp1_gpio_get(struct gpio_chip *chip, unsigned offset)
680 {
681         struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
682         int ret;
683
684         if (!pin)
685                 return -EINVAL;
686         ret = rp1_get_value(pin);
687         return ret;
688 }
689
690 static void rp1_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
691 {
692         struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
693
694         if (pin)
695                 rp1_set_value(pin, value);
696 }
697
698 static int rp1_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
699 {
700         struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
701         u32 fsel;
702
703         if (!pin)
704                 return -EINVAL;
705         fsel = rp1_get_fsel(pin);
706         if (fsel != RP1_FSEL_GPIO)
707                 return -EINVAL;
708         return (rp1_get_dir(pin) == RP1_DIR_OUTPUT) ?
709                 GPIO_LINE_DIRECTION_OUT :
710                 GPIO_LINE_DIRECTION_IN;
711 }
712
713 static int rp1_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
714 {
715         struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
716
717         if (!pin)
718                 return -EINVAL;
719         rp1_set_dir(pin, RP1_DIR_INPUT);
720         rp1_set_fsel(pin, RP1_FSEL_GPIO);
721         return 0;
722 }
723
724 static int rp1_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
725                                      int value)
726 {
727         struct rp1_pin_info *pin = rp1_get_pin(chip, offset);
728
729         if (!pin)
730                 return -EINVAL;
731         rp1_set_value(pin, value);
732         rp1_set_dir(pin, RP1_DIR_OUTPUT);
733         rp1_set_fsel(pin, RP1_FSEL_GPIO);
734         return 0;
735 }
736
737 static int rp1_gpio_set_config(struct gpio_chip *gc, unsigned offset,
738                                unsigned long config)
739 {
740         struct rp1_pinctrl *pc = gpiochip_get_data(gc);
741         unsigned long configs[] = { config };
742
743         return rp1_pinconf_set(pc->pctl_dev, offset, configs,
744                               ARRAY_SIZE(configs));
745 }
746
747 static const struct gpio_chip rp1_gpio_chip = {
748         .label = MODULE_NAME,
749         .owner = THIS_MODULE,
750         .request = gpiochip_generic_request,
751         .free = gpiochip_generic_free,
752         .direction_input = rp1_gpio_direction_input,
753         .direction_output = rp1_gpio_direction_output,
754         .get_direction = rp1_gpio_get_direction,
755         .get = rp1_gpio_get,
756         .set = rp1_gpio_set,
757         .base = -1,
758         .set_config = rp1_gpio_set_config,
759         .ngpio = RP1_NUM_GPIOS,
760         .can_sleep = false,
761 };
762
763 static void rp1_gpio_irq_handler(struct irq_desc *desc)
764 {
765         struct gpio_chip *chip = irq_desc_get_handler_data(desc);
766         struct rp1_pinctrl *pc = gpiochip_get_data(chip);
767         struct irq_chip *host_chip = irq_desc_get_chip(desc);
768         const struct rp1_iobank_desc *bank;
769         int irq = irq_desc_get_irq(desc);
770         unsigned long ints;
771         int b;
772
773         if (pc->irq[0] == irq)
774                 bank = &rp1_iobanks[0];
775         else if (pc->irq[1] == irq)
776                 bank = &rp1_iobanks[1];
777         else
778                 bank = &rp1_iobanks[2];
779
780         chained_irq_enter(host_chip, desc);
781
782         ints = readl(pc->gpio_base + bank->ints_offset);
783         for_each_set_bit(b, &ints, 32) {
784                 struct rp1_pin_info *pin = rp1_get_pin(chip, b);
785
786                 writel(RP1_GPIO_CTRL_IRQRESET,
787                        pin->gpio + RP1_SET_OFFSET + RP1_GPIO_CTRL);
788                 generic_handle_irq(irq_linear_revmap(pc->gpio_chip.irq.domain,
789                                                      bank->gpio_offset + b));
790         }
791
792         chained_irq_exit(host_chip, desc);
793 }
794
795 static void rp1_gpio_irq_config(struct rp1_pin_info *pin, bool enable)
796 {
797         writel(1 << pin->offset,
798                pin->inte + (enable ? RP1_SET_OFFSET : RP1_CLR_OFFSET));
799         if (!enable)
800                 /* Clear any latched events */
801                 writel(RP1_GPIO_CTRL_IRQRESET,
802                        pin->gpio + RP1_SET_OFFSET + RP1_GPIO_CTRL);
803 }
804
805 static void rp1_gpio_irq_enable(struct irq_data *data)
806 {
807         struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
808         unsigned gpio = irqd_to_hwirq(data);
809         struct rp1_pin_info *pin = rp1_get_pin(chip, gpio);
810
811         rp1_gpio_irq_config(pin, true);
812 }
813
814 static void rp1_gpio_irq_disable(struct irq_data *data)
815 {
816         struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
817         unsigned gpio = irqd_to_hwirq(data);
818         struct rp1_pin_info *pin = rp1_get_pin(chip, gpio);
819
820         rp1_gpio_irq_config(pin, false);
821 }
822
823 static int rp1_irq_set_type(struct rp1_pin_info *pin, unsigned int type)
824 {
825         u32 irq_flags;
826
827         switch (type) {
828         case IRQ_TYPE_NONE:
829                 irq_flags = 0;
830                 break;
831         case IRQ_TYPE_EDGE_RISING:
832                 irq_flags = RP1_INT_EDGE_RISING;
833                 break;
834         case IRQ_TYPE_EDGE_FALLING:
835                 irq_flags = RP1_INT_EDGE_FALLING;
836                 break;
837         case IRQ_TYPE_EDGE_BOTH:
838                 irq_flags = RP1_INT_EDGE_RISING | RP1_INT_EDGE_FALLING;
839                 break;
840         case IRQ_TYPE_LEVEL_HIGH:
841                 irq_flags = RP1_INT_LEVEL_HIGH;
842                 break;
843         case IRQ_TYPE_LEVEL_LOW:
844                 irq_flags = RP1_INT_LEVEL_LOW;
845                 break;
846
847         default:
848                 return -EINVAL;
849         }
850
851         /* Clear them all */
852         writel(RP1_INT_MASK << RP1_GPIO_EVENTS_SHIFT_RAW,
853                pin->gpio + RP1_CLR_OFFSET + RP1_GPIO_CTRL);
854         /* Set those that are needed */
855         writel(irq_flags << RP1_GPIO_EVENTS_SHIFT_RAW,
856                pin->gpio + RP1_SET_OFFSET + RP1_GPIO_CTRL);
857         pin->irq_type = type;
858
859         return 0;
860 }
861
862 static int rp1_gpio_irq_set_type(struct irq_data *data, unsigned int type)
863 {
864         struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
865         struct rp1_pinctrl *pc = gpiochip_get_data(chip);
866         unsigned gpio = irqd_to_hwirq(data);
867         struct rp1_pin_info *pin = rp1_get_pin(chip, gpio);
868         int bank = pin->bank;
869         unsigned long flags;
870         int ret;
871
872         raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
873
874         ret = rp1_irq_set_type(pin, type);
875         if (!ret) {
876                 if (type & IRQ_TYPE_EDGE_BOTH)
877                         irq_set_handler_locked(data, handle_edge_irq);
878                 else
879                         irq_set_handler_locked(data, handle_level_irq);
880         }
881
882         raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
883
884         return ret;
885 }
886
887 static void rp1_gpio_irq_ack(struct irq_data *data)
888 {
889         struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
890         unsigned gpio = irqd_to_hwirq(data);
891         struct rp1_pin_info *pin = rp1_get_pin(chip, gpio);
892
893         /* Clear any latched events */
894         writel(RP1_GPIO_CTRL_IRQRESET, pin->gpio + RP1_SET_OFFSET + RP1_GPIO_CTRL);
895 }
896
897 static struct irq_chip rp1_gpio_irq_chip = {
898         .name = MODULE_NAME,
899         .irq_enable = rp1_gpio_irq_enable,
900         .irq_disable = rp1_gpio_irq_disable,
901         .irq_set_type = rp1_gpio_irq_set_type,
902         .irq_ack = rp1_gpio_irq_ack,
903         .irq_mask = rp1_gpio_irq_disable,
904         .irq_unmask = rp1_gpio_irq_enable,
905         .flags = IRQCHIP_IMMUTABLE,
906 };
907
908 static int rp1_pctl_get_groups_count(struct pinctrl_dev *pctldev)
909 {
910         return ARRAY_SIZE(rp1_gpio_groups);
911 }
912
913 static const char *rp1_pctl_get_group_name(struct pinctrl_dev *pctldev,
914                                            unsigned selector)
915 {
916         return rp1_gpio_groups[selector];
917 }
918
919 static enum funcs rp1_get_fsel_func(unsigned pin, unsigned fsel)
920 {
921         if (pin < RP1_NUM_GPIOS) {
922                 if (fsel < RP1_FSEL_COUNT)
923                         return rp1_gpio_pin_funcs[pin].funcs[fsel];
924                 else if (fsel == RP1_FSEL_NONE)
925                         return func_none;
926         }
927         return func_invalid;
928 }
929
930 static int rp1_pctl_get_group_pins(struct pinctrl_dev *pctldev,
931                                    unsigned selector,
932                                    const unsigned **pins,
933                                    unsigned *num_pins)
934 {
935         *pins = &rp1_gpio_pins[selector].number;
936         *num_pins = 1;
937
938         return 0;
939 }
940
941 static void rp1_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
942                                   struct seq_file *s,
943                                   unsigned offset)
944 {
945         struct rp1_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
946         struct gpio_chip *chip = &pc->gpio_chip;
947         struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
948         u32 fsel = rp1_get_fsel(pin);
949         enum funcs func = rp1_get_fsel_func(offset, fsel);
950         int value = rp1_get_value(pin);
951         int irq = irq_find_mapping(chip->irq.domain, offset);
952
953         seq_printf(s, "function %s (%s) in %s; irq %d (%s)",
954                    rp1_func_names[fsel], rp1_func_names[func],
955                    value ? "hi" : "lo",
956                    irq, irq_type_names[pin->irq_type]);
957 }
958
959 static void rp1_pctl_dt_free_map(struct pinctrl_dev *pctldev,
960                                  struct pinctrl_map *maps, unsigned num_maps)
961 {
962         int i;
963
964         for (i = 0; i < num_maps; i++)
965                 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
966                         kfree(maps[i].data.configs.configs);
967
968         kfree(maps);
969 }
970
971 static int rp1_pctl_legacy_map_func(struct rp1_pinctrl *pc,
972                                     struct device_node *np, u32 pin, u32 fnum,
973                                     struct pinctrl_map *maps,
974                                     unsigned int *num_maps)
975 {
976         struct pinctrl_map *map = &maps[*num_maps];
977         enum funcs func;
978
979         if (fnum >= ARRAY_SIZE(legacy_fsel_map[0])) {
980                 dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
981                 return -EINVAL;
982         }
983
984         func = legacy_fsel_map[pin][fnum];
985         if (func == func_invalid) {
986                 dev_err(pc->dev, "%pOF: brcm,function %d not supported on pin %d\n",
987                         np, fnum, pin);
988         }
989
990         map->type = PIN_MAP_TYPE_MUX_GROUP;
991         map->data.mux.group = rp1_gpio_groups[pin];
992         map->data.mux.function = rp1_func_names[func];
993         (*num_maps)++;
994
995         return 0;
996 }
997
998 static int rp1_pctl_legacy_map_pull(struct rp1_pinctrl *pc,
999                                     struct device_node *np, u32 pin, u32 pull,
1000                                     struct pinctrl_map *maps,
1001                                     unsigned int *num_maps)
1002 {
1003         struct pinctrl_map *map = &maps[*num_maps];
1004         enum pin_config_param param;
1005         unsigned long *configs;
1006
1007         switch (pull) {
1008         case RP1_PUD_OFF:
1009                 param = PIN_CONFIG_BIAS_DISABLE;
1010                 break;
1011         case RP1_PUD_DOWN:
1012                 param = PIN_CONFIG_BIAS_PULL_DOWN;
1013                 break;
1014         case RP1_PUD_UP:
1015                 param = PIN_CONFIG_BIAS_PULL_UP;
1016                 break;
1017         default:
1018                 dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
1019                 return -EINVAL;
1020         }
1021
1022         configs = kzalloc(sizeof(*configs), GFP_KERNEL);
1023         if (!configs)
1024                 return -ENOMEM;
1025
1026         configs[0] = pinconf_to_config_packed(param, 0);
1027         map->type = PIN_MAP_TYPE_CONFIGS_PIN;
1028         map->data.configs.group_or_pin = rp1_gpio_pins[pin].name;
1029         map->data.configs.configs = configs;
1030         map->data.configs.num_configs = 1;
1031         (*num_maps)++;
1032
1033         return 0;
1034 }
1035
1036 static int rp1_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
1037                                    struct device_node *np,
1038                                    struct pinctrl_map **map,
1039                                    unsigned int *num_maps)
1040 {
1041         struct rp1_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
1042         struct property *pins, *funcs, *pulls;
1043         int num_pins, num_funcs, num_pulls, maps_per_pin;
1044         struct pinctrl_map *maps;
1045         unsigned long *configs = NULL;
1046         const char *function = NULL;
1047         unsigned int reserved_maps;
1048         int num_configs = 0;
1049         int i, err;
1050         u32 pin, func, pull;
1051
1052         /* Check for legacy pin declaration */
1053         pins = of_find_property(np, "brcm,pins", NULL);
1054
1055         if (!pins) /* Assume generic bindings in this node */
1056                 return pinconf_generic_dt_node_to_map_all(pctldev, np, map, num_maps);
1057
1058         funcs = of_find_property(np, "brcm,function", NULL);
1059         if (!funcs)
1060                 of_property_read_string(np, "function", &function);
1061
1062         pulls = of_find_property(np, "brcm,pull", NULL);
1063         if (!pulls)
1064                 pinconf_generic_parse_dt_config(np, pctldev, &configs, &num_configs);
1065
1066         if (!function && !funcs && !num_configs && !pulls) {
1067                 dev_err(pc->dev,
1068                         "%pOF: no function, brcm,function, brcm,pull, etc.\n",
1069                         np);
1070                 return -EINVAL;
1071         }
1072
1073         num_pins = pins->length / 4;
1074         num_funcs = funcs ? (funcs->length / 4) : 0;
1075         num_pulls = pulls ? (pulls->length / 4) : 0;
1076
1077         if (num_funcs > 1 && num_funcs != num_pins) {
1078                 dev_err(pc->dev,
1079                         "%pOF: brcm,function must have 1 or %d entries\n",
1080                         np, num_pins);
1081                 return -EINVAL;
1082         }
1083
1084         if (num_pulls > 1 && num_pulls != num_pins) {
1085                 dev_err(pc->dev,
1086                         "%pOF: brcm,pull must have 1 or %d entries\n",
1087                         np, num_pins);
1088                 return -EINVAL;
1089         }
1090
1091         maps_per_pin = 0;
1092         if (function || num_funcs)
1093                 maps_per_pin++;
1094         if (num_configs || num_pulls)
1095                 maps_per_pin++;
1096         reserved_maps = num_pins * maps_per_pin;
1097         maps = kcalloc(reserved_maps, sizeof(*maps), GFP_KERNEL);
1098         if (!maps)
1099                 return -ENOMEM;
1100
1101         *num_maps = 0;
1102
1103         for (i = 0; i < num_pins; i++) {
1104                 err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
1105                 if (err)
1106                         goto out;
1107                 if (pin >= ARRAY_SIZE(legacy_fsel_map)) {
1108                         dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
1109                                 np, pin);
1110                         err = -EINVAL;
1111                         goto out;
1112                 }
1113
1114                 if (num_funcs) {
1115                         err = of_property_read_u32_index(np, "brcm,function",
1116                                                          (num_funcs > 1) ? i : 0,
1117                                                          &func);
1118                         if (err)
1119                                 goto out;
1120                         err = rp1_pctl_legacy_map_func(pc, np, pin, func,
1121                                                        maps, num_maps);
1122                 } else if (function) {
1123                         err = pinctrl_utils_add_map_mux(pctldev, &maps,
1124                                                         &reserved_maps, num_maps,
1125                                                         rp1_gpio_groups[pin],
1126                                                         function);
1127                 }
1128
1129                 if (err)
1130                         goto out;
1131
1132                 if (num_pulls) {
1133                         err = of_property_read_u32_index(np, "brcm,pull",
1134                                                          (num_pulls > 1) ? i : 0,
1135                                                          &pull);
1136                         if (err)
1137                                 goto out;
1138                         err = rp1_pctl_legacy_map_pull(pc, np, pin, pull,
1139                                                        maps, num_maps);
1140                 } else if (num_configs) {
1141                         err = pinctrl_utils_add_map_configs(pctldev, &maps,
1142                                                             &reserved_maps, num_maps,
1143                                                             rp1_gpio_groups[pin],
1144                                                             configs, num_configs,
1145                                                             PIN_MAP_TYPE_CONFIGS_PIN);
1146                 }
1147
1148                 if (err)
1149                         goto out;
1150         }
1151
1152         *map = maps;
1153
1154         return 0;
1155
1156 out:
1157         rp1_pctl_dt_free_map(pctldev, maps, reserved_maps);
1158         return err;
1159 }
1160
1161 static const struct pinctrl_ops rp1_pctl_ops = {
1162         .get_groups_count = rp1_pctl_get_groups_count,
1163         .get_group_name = rp1_pctl_get_group_name,
1164         .get_group_pins = rp1_pctl_get_group_pins,
1165         .pin_dbg_show = rp1_pctl_pin_dbg_show,
1166         .dt_node_to_map = rp1_pctl_dt_node_to_map,
1167         .dt_free_map = rp1_pctl_dt_free_map,
1168 };
1169
1170 static int rp1_pmx_free(struct pinctrl_dev *pctldev, unsigned offset)
1171 {
1172         struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
1173         u32 fsel = rp1_get_fsel(pin);
1174
1175         /* Return non-GPIOs to GPIO_IN */
1176         if (fsel != RP1_FSEL_GPIO) {
1177                 rp1_set_dir(pin, RP1_DIR_INPUT);
1178                 rp1_set_fsel(pin, RP1_FSEL_GPIO);
1179         }
1180
1181         return 0;
1182 }
1183
1184 static int rp1_pmx_get_functions_count(struct pinctrl_dev *pctldev)
1185 {
1186         return func_count;
1187 }
1188
1189 static const char *rp1_pmx_get_function_name(struct pinctrl_dev *pctldev,
1190                                              unsigned selector)
1191 {
1192         return (selector < func_count) ? rp1_func_names[selector] : NULL;
1193 }
1194
1195 static int rp1_pmx_get_function_groups(struct pinctrl_dev *pctldev,
1196                                        unsigned selector,
1197                                        const char * const **groups,
1198                                        unsigned * const num_groups)
1199 {
1200         /* every pin can do every function */
1201         *groups = rp1_gpio_groups;
1202         *num_groups = ARRAY_SIZE(rp1_gpio_groups);
1203
1204         return 0;
1205 }
1206
1207 static int rp1_pmx_set(struct pinctrl_dev *pctldev, unsigned func_selector,
1208                        unsigned group_selector)
1209 {
1210         struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, group_selector);
1211         const u8 *pin_funcs;
1212         int fsel;
1213
1214         /* func_selector is an enum funcs, so needs translation */
1215
1216         if (func_selector >= RP1_FSEL_COUNT) {
1217                 /* Convert to an fsel number */
1218                 pin_funcs = rp1_gpio_pin_funcs[pin->num].funcs;
1219                 for (fsel = 0; fsel < RP1_FSEL_COUNT; fsel++) {
1220                         if (pin_funcs[fsel] == func_selector)
1221                                 break;
1222                 }
1223         } else {
1224                 fsel = (int)func_selector;
1225         }
1226
1227         if (fsel >= RP1_FSEL_COUNT && fsel != RP1_FSEL_NONE)
1228                 return -EINVAL;
1229
1230         rp1_set_fsel(pin, fsel);
1231
1232         return 0;
1233 }
1234
1235 static void rp1_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
1236                                       struct pinctrl_gpio_range *range,
1237                                       unsigned offset)
1238 {
1239         (void)rp1_pmx_free(pctldev, offset);
1240 }
1241
1242 static int rp1_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
1243                                       struct pinctrl_gpio_range *range,
1244                                       unsigned offset,
1245                                       bool input)
1246 {
1247         struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
1248
1249         rp1_set_dir(pin, input);
1250         rp1_set_fsel(pin, RP1_FSEL_GPIO);
1251
1252         return 0;
1253 }
1254
1255 static const struct pinmux_ops rp1_pmx_ops = {
1256         .free = rp1_pmx_free,
1257         .get_functions_count = rp1_pmx_get_functions_count,
1258         .get_function_name = rp1_pmx_get_function_name,
1259         .get_function_groups = rp1_pmx_get_function_groups,
1260         .set_mux = rp1_pmx_set,
1261         .gpio_disable_free = rp1_pmx_gpio_disable_free,
1262         .gpio_set_direction = rp1_pmx_gpio_set_direction,
1263 };
1264
1265 static void rp1_pull_config_set(struct rp1_pin_info *pin, unsigned int arg)
1266 {
1267         u32 padctrl = readl(pin->pad);
1268
1269         FLD_SET(padctrl, RP1_PAD_PULL, arg & 0x3);
1270
1271         writel(padctrl, pin->pad);
1272 }
1273
1274 /* Generic pinconf methods */
1275
1276 static int rp1_pinconf_set(struct pinctrl_dev *pctldev, unsigned int offset,
1277                            unsigned long *configs, unsigned int num_configs)
1278 {
1279         struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
1280         u32 param, arg;
1281         int i;
1282
1283         if (!pin)
1284                 return -EINVAL;
1285
1286         for (i = 0; i < num_configs; i++) {
1287                 param = pinconf_to_config_param(configs[i]);
1288                 arg = pinconf_to_config_argument(configs[i]);
1289
1290                 switch (param) {
1291                 case PIN_CONFIG_BIAS_DISABLE:
1292                         rp1_pull_config_set(pin, RP1_PUD_OFF);
1293                         break;
1294
1295                 case PIN_CONFIG_BIAS_PULL_DOWN:
1296                         rp1_pull_config_set(pin, RP1_PUD_DOWN);
1297                         break;
1298
1299                 case PIN_CONFIG_BIAS_PULL_UP:
1300                         rp1_pull_config_set(pin, RP1_PUD_UP);
1301                         break;
1302
1303                 case PIN_CONFIG_INPUT_ENABLE:
1304                         rp1_input_enable(pin, arg);
1305                         break;
1306
1307                 case PIN_CONFIG_OUTPUT_ENABLE:
1308                         rp1_output_enable(pin, arg);
1309                         break;
1310
1311                 case PIN_CONFIG_OUTPUT:
1312                         rp1_set_value(pin, arg);
1313                         rp1_set_dir(pin, RP1_DIR_OUTPUT);
1314                         rp1_set_fsel(pin, RP1_FSEL_GPIO);
1315                         break;
1316
1317                 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1318                         rp1_pad_update(pin, RP1_PAD_SCHMITT_MASK,
1319                                        arg ? RP1_PAD_SCHMITT_MASK : 0);
1320                         break;
1321
1322                 case PIN_CONFIG_SLEW_RATE:
1323                         rp1_pad_update(pin, RP1_PAD_SLEWFAST_MASK,
1324                                        arg ? RP1_PAD_SLEWFAST_MASK : 0);
1325                         break;
1326
1327                 case PIN_CONFIG_DRIVE_STRENGTH:
1328                         switch (arg) {
1329                         case 2:
1330                                 arg = RP1_PAD_DRIVE_2MA;
1331                                 break;
1332                         case 4:
1333                                 arg = RP1_PAD_DRIVE_4MA;
1334                                 break;
1335                         case 8:
1336                                 arg = RP1_PAD_DRIVE_8MA;
1337                                 break;
1338                         case 12:
1339                                 arg = RP1_PAD_DRIVE_12MA;
1340                                 break;
1341                         default:
1342                                 return -ENOTSUPP;
1343                         }
1344                         rp1_pad_update(pin, RP1_PAD_DRIVE_MASK, arg);
1345                         break;
1346
1347                 default:
1348                         return -ENOTSUPP;
1349
1350                 } /* switch param type */
1351         } /* for each config */
1352
1353         return 0;
1354 }
1355
1356 static int rp1_pinconf_get(struct pinctrl_dev *pctldev, unsigned offset,
1357                            unsigned long *config)
1358 {
1359         struct rp1_pin_info *pin = rp1_get_pin_pctl(pctldev, offset);
1360         enum pin_config_param param = pinconf_to_config_param(*config);
1361         u32 padctrl;
1362         u32 arg;
1363
1364         if (!pin)
1365                 return -EINVAL;
1366
1367         padctrl = readl(pin->pad);
1368
1369         switch (param) {
1370         case PIN_CONFIG_INPUT_ENABLE:
1371                 arg = !!(padctrl & RP1_PAD_IN_ENABLE_MASK);
1372                 break;
1373         case PIN_CONFIG_OUTPUT_ENABLE:
1374                 arg = !(padctrl & RP1_PAD_OUT_DISABLE_MASK);
1375                 break;
1376         case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1377                 arg = !!(padctrl & RP1_PAD_SCHMITT_MASK);
1378                 break;
1379         case PIN_CONFIG_SLEW_RATE:
1380                 arg = !!(padctrl & RP1_PAD_SLEWFAST_MASK);
1381                 break;
1382         case PIN_CONFIG_DRIVE_STRENGTH:
1383                 switch (padctrl & RP1_PAD_DRIVE_MASK) {
1384                 case RP1_PAD_DRIVE_2MA:
1385                         arg = 2;
1386                         break;
1387                 case RP1_PAD_DRIVE_4MA:
1388                         arg = 4;
1389                         break;
1390                 case RP1_PAD_DRIVE_8MA:
1391                         arg = 8;
1392                         break;
1393                 case RP1_PAD_DRIVE_12MA:
1394                         arg = 12;
1395                         break;
1396                 }
1397                 break;
1398         case PIN_CONFIG_BIAS_DISABLE:
1399                 arg = ((padctrl & RP1_PAD_PULL_MASK) == (RP1_PUD_OFF << RP1_PAD_PULL_LSB));
1400                 break;
1401         case PIN_CONFIG_BIAS_PULL_DOWN:
1402                 arg = ((padctrl & RP1_PAD_PULL_MASK) == (RP1_PUD_DOWN << RP1_PAD_PULL_LSB));
1403                 break;
1404
1405         case PIN_CONFIG_BIAS_PULL_UP:
1406                 arg = ((padctrl & RP1_PAD_PULL_MASK) == (RP1_PUD_UP << RP1_PAD_PULL_LSB));
1407                 break;
1408         default:
1409                 return -ENOTSUPP;
1410         }
1411
1412         *config = pinconf_to_config_packed(param, arg);
1413
1414         return 0;
1415 }
1416
1417 static const struct pinconf_ops rp1_pinconf_ops = {
1418         .is_generic = true,
1419         .pin_config_get = rp1_pinconf_get,
1420         .pin_config_set = rp1_pinconf_set,
1421 };
1422
1423 static struct pinctrl_desc rp1_pinctrl_desc = {
1424         .name = MODULE_NAME,
1425         .pins = rp1_gpio_pins,
1426         .npins = ARRAY_SIZE(rp1_gpio_pins),
1427         .pctlops = &rp1_pctl_ops,
1428         .pmxops = &rp1_pmx_ops,
1429         .confops = &rp1_pinconf_ops,
1430         .owner = THIS_MODULE,
1431 };
1432
1433 static struct pinctrl_gpio_range rp1_pinctrl_gpio_range = {
1434         .name = MODULE_NAME,
1435         .npins = RP1_NUM_GPIOS,
1436 };
1437
1438 static const struct of_device_id rp1_pinctrl_match[] = {
1439         {
1440                 .compatible = "raspberrypi,rp1-gpio",
1441                 .data = &rp1_pinconf_ops,
1442         },
1443         {}
1444 };
1445
1446 static inline void __iomem *devm_auto_iomap(struct platform_device *pdev,
1447                                             unsigned int index)
1448 {
1449         struct device *dev = &pdev->dev;
1450         struct device_node *np = dev->of_node;
1451
1452         if (np)
1453                 return devm_of_iomap(dev, np, (int)index, NULL);
1454         else
1455                 return devm_platform_ioremap_resource(pdev, index);
1456 }
1457
1458 static int rp1_pinctrl_probe(struct platform_device *pdev)
1459 {
1460         struct device *dev = &pdev->dev;
1461         struct device_node *np = dev->of_node;
1462         struct rp1_pinctrl *pc;
1463         struct gpio_irq_chip *girq;
1464         int err, i;
1465
1466         BUILD_BUG_ON(ARRAY_SIZE(rp1_gpio_pins) != RP1_NUM_GPIOS);
1467         BUILD_BUG_ON(ARRAY_SIZE(rp1_gpio_groups) != RP1_NUM_GPIOS);
1468
1469         pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
1470         if (!pc)
1471                 return -ENOMEM;
1472
1473         platform_set_drvdata(pdev, pc);
1474         pc->dev = dev;
1475
1476         pc->gpio_base = devm_auto_iomap(pdev, 0);
1477         if (IS_ERR(pc->gpio_base)) {
1478                 dev_err(dev, "could not get GPIO IO memory\n");
1479                 return PTR_ERR(pc->gpio_base);
1480         }
1481
1482         pc->rio_base = devm_auto_iomap(pdev, 1);
1483         if (IS_ERR(pc->rio_base)) {
1484                 dev_err(dev, "could not get RIO IO memory\n");
1485                 return PTR_ERR(pc->rio_base);
1486         }
1487
1488         pc->pads_base = devm_auto_iomap(pdev, 2);
1489         if (IS_ERR(pc->pads_base)) {
1490                 dev_err(dev, "could not get PADS IO memory\n");
1491                 return PTR_ERR(pc->pads_base);
1492         }
1493
1494         pc->gpio_chip = rp1_gpio_chip;
1495         pc->gpio_chip.parent = dev;
1496
1497         for (i = 0; i < RP1_NUM_BANKS; i++) {
1498                 const struct rp1_iobank_desc *bank = &rp1_iobanks[i];
1499                 int j;
1500
1501                 for (j = 0; j < bank->num_gpios; j++) {
1502                         struct rp1_pin_info *pin =
1503                                 &pc->pins[bank->min_gpio + j];
1504
1505                         pin->num = bank->min_gpio + j;
1506                         pin->bank = i;
1507                         pin->offset = j;
1508
1509                         pin->gpio = pc->gpio_base + bank->gpio_offset +
1510                                     j * sizeof(u32) * 2;
1511                         pin->inte = pc->gpio_base + bank->inte_offset;
1512                         pin->ints = pc->gpio_base + bank->ints_offset;
1513                         pin->rio  = pc->rio_base + bank->rio_offset;
1514                         pin->pad  = pc->pads_base + bank->pads_offset +
1515                                     j * sizeof(u32);
1516                 }
1517
1518                 raw_spin_lock_init(&pc->irq_lock[i]);
1519         }
1520
1521         pc->pctl_dev = devm_pinctrl_register(dev, &rp1_pinctrl_desc, pc);
1522         if (IS_ERR(pc->pctl_dev))
1523                 return PTR_ERR(pc->pctl_dev);
1524
1525         girq = &pc->gpio_chip.irq;
1526         girq->chip = &rp1_gpio_irq_chip;
1527         girq->parent_handler = rp1_gpio_irq_handler;
1528         girq->num_parents = RP1_NUM_BANKS;
1529         girq->parents = pc->irq;
1530
1531         /*
1532          * Use the same handler for all groups: this is necessary
1533          * since we use one gpiochip to cover all lines - the
1534          * irq handler then needs to figure out which group and
1535          * bank that was firing the IRQ and look up the per-group
1536          * and bank data.
1537          */
1538         for (i = 0; i < RP1_NUM_BANKS; i++) {
1539                 pc->irq[i] = irq_of_parse_and_map(np, i);
1540                 if (!pc->irq[i]) {
1541                         girq->num_parents = i;
1542                         break;
1543                 }
1544         }
1545
1546         girq->default_type = IRQ_TYPE_NONE;
1547         girq->handler = handle_level_irq;
1548
1549         err = devm_gpiochip_add_data(dev, &pc->gpio_chip, pc);
1550         if (err) {
1551                 dev_err(dev, "could not add GPIO chip\n");
1552                 return err;
1553         }
1554
1555         pc->gpio_range = rp1_pinctrl_gpio_range;
1556         pc->gpio_range.base = pc->gpio_chip.base;
1557         pc->gpio_range.gc = &pc->gpio_chip;
1558         pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
1559
1560         return 0;
1561 }
1562
1563 static struct platform_driver rp1_pinctrl_driver = {
1564         .probe = rp1_pinctrl_probe,
1565         .driver = {
1566                 .name = MODULE_NAME,
1567                 .of_match_table = rp1_pinctrl_match,
1568                 .suppress_bind_attrs = true,
1569         },
1570 };
1571 builtin_platform_driver(rp1_pinctrl_driver);