xref: /linux/drivers/pinctrl/renesas/pinctrl-rza1.c (revision 077365a941166f3a7f5894017f9d26d17cdec00e)
1*077365a9SGeert Uytterhoeven // SPDX-License-Identifier: GPL-2.0
2*077365a9SGeert Uytterhoeven /*
3*077365a9SGeert Uytterhoeven  * Combined GPIO and pin controller support for Renesas RZ/A1 (r7s72100) SoC
4*077365a9SGeert Uytterhoeven  *
5*077365a9SGeert Uytterhoeven  * Copyright (C) 2017 Jacopo Mondi
6*077365a9SGeert Uytterhoeven  */
7*077365a9SGeert Uytterhoeven 
8*077365a9SGeert Uytterhoeven /*
9*077365a9SGeert Uytterhoeven  * This pin controller/gpio combined driver supports Renesas devices of RZ/A1
10*077365a9SGeert Uytterhoeven  * family.
11*077365a9SGeert Uytterhoeven  * This includes SoCs which are sub- or super- sets of this particular line,
12*077365a9SGeert Uytterhoeven  * as RZ/A1H (r7s721000), RZ/A1M (r7s721010) and RZ/A1L (r7s721020).
13*077365a9SGeert Uytterhoeven  */
14*077365a9SGeert Uytterhoeven 
15*077365a9SGeert Uytterhoeven #include <linux/bitops.h>
16*077365a9SGeert Uytterhoeven #include <linux/err.h>
17*077365a9SGeert Uytterhoeven #include <linux/gpio/driver.h>
18*077365a9SGeert Uytterhoeven #include <linux/init.h>
19*077365a9SGeert Uytterhoeven #include <linux/ioport.h>
20*077365a9SGeert Uytterhoeven #include <linux/module.h>
21*077365a9SGeert Uytterhoeven #include <linux/of.h>
22*077365a9SGeert Uytterhoeven #include <linux/of_address.h>
23*077365a9SGeert Uytterhoeven #include <linux/of_device.h>
24*077365a9SGeert Uytterhoeven #include <linux/pinctrl/pinconf-generic.h>
25*077365a9SGeert Uytterhoeven #include <linux/pinctrl/pinctrl.h>
26*077365a9SGeert Uytterhoeven #include <linux/pinctrl/pinmux.h>
27*077365a9SGeert Uytterhoeven #include <linux/slab.h>
28*077365a9SGeert Uytterhoeven 
29*077365a9SGeert Uytterhoeven #include "../core.h"
30*077365a9SGeert Uytterhoeven #include "../devicetree.h"
31*077365a9SGeert Uytterhoeven #include "../pinconf.h"
32*077365a9SGeert Uytterhoeven #include "../pinmux.h"
33*077365a9SGeert Uytterhoeven 
34*077365a9SGeert Uytterhoeven #define DRIVER_NAME			"pinctrl-rza1"
35*077365a9SGeert Uytterhoeven 
36*077365a9SGeert Uytterhoeven #define RZA1_P_REG			0x0000
37*077365a9SGeert Uytterhoeven #define RZA1_PPR_REG			0x0200
38*077365a9SGeert Uytterhoeven #define RZA1_PM_REG			0x0300
39*077365a9SGeert Uytterhoeven #define RZA1_PMC_REG			0x0400
40*077365a9SGeert Uytterhoeven #define RZA1_PFC_REG			0x0500
41*077365a9SGeert Uytterhoeven #define RZA1_PFCE_REG			0x0600
42*077365a9SGeert Uytterhoeven #define RZA1_PFCEA_REG			0x0a00
43*077365a9SGeert Uytterhoeven #define RZA1_PIBC_REG			0x4000
44*077365a9SGeert Uytterhoeven #define RZA1_PBDC_REG			0x4100
45*077365a9SGeert Uytterhoeven #define RZA1_PIPC_REG			0x4200
46*077365a9SGeert Uytterhoeven 
47*077365a9SGeert Uytterhoeven #define RZA1_ADDR(mem, reg, port)	((mem) + (reg) + ((port) * 4))
48*077365a9SGeert Uytterhoeven 
49*077365a9SGeert Uytterhoeven #define RZA1_NPORTS			12
50*077365a9SGeert Uytterhoeven #define RZA1_PINS_PER_PORT		16
51*077365a9SGeert Uytterhoeven #define RZA1_NPINS			(RZA1_PINS_PER_PORT * RZA1_NPORTS)
52*077365a9SGeert Uytterhoeven #define RZA1_PIN_ID_TO_PORT(id)		((id) / RZA1_PINS_PER_PORT)
53*077365a9SGeert Uytterhoeven #define RZA1_PIN_ID_TO_PIN(id)		((id) % RZA1_PINS_PER_PORT)
54*077365a9SGeert Uytterhoeven 
55*077365a9SGeert Uytterhoeven /*
56*077365a9SGeert Uytterhoeven  * Use 16 lower bits [15:0] for pin identifier
57*077365a9SGeert Uytterhoeven  * Use 16 higher bits [31:16] for pin mux function
58*077365a9SGeert Uytterhoeven  */
59*077365a9SGeert Uytterhoeven #define MUX_PIN_ID_MASK			GENMASK(15, 0)
60*077365a9SGeert Uytterhoeven #define MUX_FUNC_MASK			GENMASK(31, 16)
61*077365a9SGeert Uytterhoeven 
62*077365a9SGeert Uytterhoeven #define MUX_FUNC_OFFS			16
63*077365a9SGeert Uytterhoeven #define MUX_FUNC(pinconf)		\
64*077365a9SGeert Uytterhoeven 	((pinconf & MUX_FUNC_MASK) >> MUX_FUNC_OFFS)
65*077365a9SGeert Uytterhoeven #define MUX_FUNC_PFC_MASK		BIT(0)
66*077365a9SGeert Uytterhoeven #define MUX_FUNC_PFCE_MASK		BIT(1)
67*077365a9SGeert Uytterhoeven #define MUX_FUNC_PFCEA_MASK		BIT(2)
68*077365a9SGeert Uytterhoeven 
69*077365a9SGeert Uytterhoeven /* Pin mux flags */
70*077365a9SGeert Uytterhoeven #define MUX_FLAGS_BIDIR			BIT(0)
71*077365a9SGeert Uytterhoeven #define MUX_FLAGS_SWIO_INPUT		BIT(1)
72*077365a9SGeert Uytterhoeven #define MUX_FLAGS_SWIO_OUTPUT		BIT(2)
73*077365a9SGeert Uytterhoeven 
74*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
75*077365a9SGeert Uytterhoeven  * RZ/A1 pinmux flags
76*077365a9SGeert Uytterhoeven  */
77*077365a9SGeert Uytterhoeven 
78*077365a9SGeert Uytterhoeven /*
79*077365a9SGeert Uytterhoeven  * rza1_bidir_pin - describe a single pin that needs bidir flag applied.
80*077365a9SGeert Uytterhoeven  */
81*077365a9SGeert Uytterhoeven struct rza1_bidir_pin {
82*077365a9SGeert Uytterhoeven 	u8 pin: 4;
83*077365a9SGeert Uytterhoeven 	u8 func: 4;
84*077365a9SGeert Uytterhoeven };
85*077365a9SGeert Uytterhoeven 
86*077365a9SGeert Uytterhoeven /*
87*077365a9SGeert Uytterhoeven  * rza1_bidir_entry - describe a list of pins that needs bidir flag applied.
88*077365a9SGeert Uytterhoeven  *		      Each struct rza1_bidir_entry describes a port.
89*077365a9SGeert Uytterhoeven  */
90*077365a9SGeert Uytterhoeven struct rza1_bidir_entry {
91*077365a9SGeert Uytterhoeven 	const unsigned int npins;
92*077365a9SGeert Uytterhoeven 	const struct rza1_bidir_pin *pins;
93*077365a9SGeert Uytterhoeven };
94*077365a9SGeert Uytterhoeven 
95*077365a9SGeert Uytterhoeven /*
96*077365a9SGeert Uytterhoeven  * rza1_swio_pin - describe a single pin that needs swio flag applied.
97*077365a9SGeert Uytterhoeven  */
98*077365a9SGeert Uytterhoeven struct rza1_swio_pin {
99*077365a9SGeert Uytterhoeven 	u16 pin: 4;
100*077365a9SGeert Uytterhoeven 	u16 port: 4;
101*077365a9SGeert Uytterhoeven 	u16 func: 4;
102*077365a9SGeert Uytterhoeven 	u16 input: 1;
103*077365a9SGeert Uytterhoeven };
104*077365a9SGeert Uytterhoeven 
105*077365a9SGeert Uytterhoeven /*
106*077365a9SGeert Uytterhoeven  * rza1_swio_entry - describe a list of pins that needs swio flag applied
107*077365a9SGeert Uytterhoeven  */
108*077365a9SGeert Uytterhoeven struct rza1_swio_entry {
109*077365a9SGeert Uytterhoeven 	const unsigned int npins;
110*077365a9SGeert Uytterhoeven 	const struct rza1_swio_pin *pins;
111*077365a9SGeert Uytterhoeven };
112*077365a9SGeert Uytterhoeven 
113*077365a9SGeert Uytterhoeven /*
114*077365a9SGeert Uytterhoeven  * rza1_pinmux_conf - group together bidir and swio pinmux flag tables
115*077365a9SGeert Uytterhoeven  */
116*077365a9SGeert Uytterhoeven struct rza1_pinmux_conf {
117*077365a9SGeert Uytterhoeven 	const struct rza1_bidir_entry *bidir_entries;
118*077365a9SGeert Uytterhoeven 	const struct rza1_swio_entry *swio_entries;
119*077365a9SGeert Uytterhoeven };
120*077365a9SGeert Uytterhoeven 
121*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
122*077365a9SGeert Uytterhoeven  * RZ/A1H (r7s72100) pinmux flags
123*077365a9SGeert Uytterhoeven  */
124*077365a9SGeert Uytterhoeven 
125*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p1[] = {
126*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 1 },
127*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 1 },
128*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 1 },
129*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 1 },
130*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 1 },
131*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 1 },
132*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 1 },
133*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 1 },
134*077365a9SGeert Uytterhoeven };
135*077365a9SGeert Uytterhoeven 
136*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p2[] = {
137*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 1 },
138*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 1 },
139*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 1 },
140*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 1 },
141*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 1 },
142*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 4 },
143*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 4 },
144*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 4 },
145*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 4 },
146*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 1 },
147*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 1 },
148*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 1 },
149*077365a9SGeert Uytterhoeven 	{ .pin = 8, .func = 1 },
150*077365a9SGeert Uytterhoeven 	{ .pin = 9, .func = 1 },
151*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 1 },
152*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 1 },
153*077365a9SGeert Uytterhoeven 	{ .pin = 12, .func = 1 },
154*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 1 },
155*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 1 },
156*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 1 },
157*077365a9SGeert Uytterhoeven 	{ .pin = 12, .func = 4 },
158*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 4 },
159*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 4 },
160*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 4 },
161*077365a9SGeert Uytterhoeven };
162*077365a9SGeert Uytterhoeven 
163*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p3[] = {
164*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 2 },
165*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 7 },
166*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 7 },
167*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 7 },
168*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 7 },
169*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 7 },
170*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 8 },
171*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 8 },
172*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 8 },
173*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 8 },
174*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 8 },
175*077365a9SGeert Uytterhoeven };
176*077365a9SGeert Uytterhoeven 
177*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p4[] = {
178*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 8 },
179*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 8 },
180*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 8 },
181*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 8 },
182*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 3 },
183*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 3 },
184*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 3 },
185*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 3 },
186*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 3 },
187*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 4 },
188*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 4 },
189*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 4 },
190*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 4 },
191*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 4 },
192*077365a9SGeert Uytterhoeven 	{ .pin = 12, .func = 5 },
193*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 5 },
194*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 5 },
195*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 5 },
196*077365a9SGeert Uytterhoeven };
197*077365a9SGeert Uytterhoeven 
198*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p6[] = {
199*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 1 },
200*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 1 },
201*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 1 },
202*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 1 },
203*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 1 },
204*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 1 },
205*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 1 },
206*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 1 },
207*077365a9SGeert Uytterhoeven 	{ .pin = 8, .func = 1 },
208*077365a9SGeert Uytterhoeven 	{ .pin = 9, .func = 1 },
209*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 1 },
210*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 1 },
211*077365a9SGeert Uytterhoeven 	{ .pin = 12, .func = 1 },
212*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 1 },
213*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 1 },
214*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 1 },
215*077365a9SGeert Uytterhoeven };
216*077365a9SGeert Uytterhoeven 
217*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p7[] = {
218*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 3 },
219*077365a9SGeert Uytterhoeven };
220*077365a9SGeert Uytterhoeven 
221*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p8[] = {
222*077365a9SGeert Uytterhoeven 	{ .pin = 8, .func = 3 },
223*077365a9SGeert Uytterhoeven 	{ .pin = 9, .func = 3 },
224*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 3 },
225*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 3 },
226*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 2 },
227*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 2 },
228*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 3 },
229*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 3 },
230*077365a9SGeert Uytterhoeven };
231*077365a9SGeert Uytterhoeven 
232*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p9[] = {
233*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 2 },
234*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 2 },
235*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 2 },
236*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 2 },
237*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 2 },
238*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 2 },
239*077365a9SGeert Uytterhoeven };
240*077365a9SGeert Uytterhoeven 
241*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1h_bidir_pins_p11[] = {
242*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 2 },
243*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 2 },
244*077365a9SGeert Uytterhoeven 	{ .pin = 9, .func = 2 },
245*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 4 },
246*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 4 },
247*077365a9SGeert Uytterhoeven 	{ .pin = 9, .func = 4 },
248*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 2 },
249*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 2 },
250*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 4 },
251*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 4 },
252*077365a9SGeert Uytterhoeven 	{ .pin = 12, .func = 4 },
253*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 4 },
254*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 4 },
255*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 4 },
256*077365a9SGeert Uytterhoeven };
257*077365a9SGeert Uytterhoeven 
258*077365a9SGeert Uytterhoeven static const struct rza1_swio_pin rza1h_swio_pins[] = {
259*077365a9SGeert Uytterhoeven 	{ .port = 2, .pin = 7, .func = 4, .input = 0 },
260*077365a9SGeert Uytterhoeven 	{ .port = 2, .pin = 11, .func = 4, .input = 0 },
261*077365a9SGeert Uytterhoeven 	{ .port = 3, .pin = 7, .func = 3, .input = 0 },
262*077365a9SGeert Uytterhoeven 	{ .port = 3, .pin = 7, .func = 8, .input = 0 },
263*077365a9SGeert Uytterhoeven 	{ .port = 4, .pin = 7, .func = 5, .input = 0 },
264*077365a9SGeert Uytterhoeven 	{ .port = 4, .pin = 7, .func = 11, .input = 0 },
265*077365a9SGeert Uytterhoeven 	{ .port = 4, .pin = 15, .func = 6, .input = 0 },
266*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 0, .func = 1, .input = 1 },
267*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 1, .func = 1, .input = 1 },
268*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 2, .func = 1, .input = 1 },
269*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 3, .func = 1, .input = 1 },
270*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 4, .func = 1, .input = 1 },
271*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 5, .func = 1, .input = 1 },
272*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 6, .func = 1, .input = 1 },
273*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 7, .func = 1, .input = 1 },
274*077365a9SGeert Uytterhoeven 	{ .port = 7, .pin = 4, .func = 6, .input = 0 },
275*077365a9SGeert Uytterhoeven 	{ .port = 7, .pin = 11, .func = 2, .input = 0 },
276*077365a9SGeert Uytterhoeven 	{ .port = 8, .pin = 10, .func = 8, .input = 0 },
277*077365a9SGeert Uytterhoeven 	{ .port = 10, .pin = 15, .func = 2, .input = 0 },
278*077365a9SGeert Uytterhoeven };
279*077365a9SGeert Uytterhoeven 
280*077365a9SGeert Uytterhoeven static const struct rza1_bidir_entry rza1h_bidir_entries[RZA1_NPORTS] = {
281*077365a9SGeert Uytterhoeven 	[1] = { ARRAY_SIZE(rza1h_bidir_pins_p1), rza1h_bidir_pins_p1 },
282*077365a9SGeert Uytterhoeven 	[2] = { ARRAY_SIZE(rza1h_bidir_pins_p2), rza1h_bidir_pins_p2 },
283*077365a9SGeert Uytterhoeven 	[3] = { ARRAY_SIZE(rza1h_bidir_pins_p3), rza1h_bidir_pins_p3 },
284*077365a9SGeert Uytterhoeven 	[4] = { ARRAY_SIZE(rza1h_bidir_pins_p4), rza1h_bidir_pins_p4 },
285*077365a9SGeert Uytterhoeven 	[6] = { ARRAY_SIZE(rza1h_bidir_pins_p6), rza1h_bidir_pins_p6 },
286*077365a9SGeert Uytterhoeven 	[7] = { ARRAY_SIZE(rza1h_bidir_pins_p7), rza1h_bidir_pins_p7 },
287*077365a9SGeert Uytterhoeven 	[8] = { ARRAY_SIZE(rza1h_bidir_pins_p8), rza1h_bidir_pins_p8 },
288*077365a9SGeert Uytterhoeven 	[9] = { ARRAY_SIZE(rza1h_bidir_pins_p9), rza1h_bidir_pins_p9 },
289*077365a9SGeert Uytterhoeven 	[11] = { ARRAY_SIZE(rza1h_bidir_pins_p11), rza1h_bidir_pins_p11 },
290*077365a9SGeert Uytterhoeven };
291*077365a9SGeert Uytterhoeven 
292*077365a9SGeert Uytterhoeven static const struct rza1_swio_entry rza1h_swio_entries[] = {
293*077365a9SGeert Uytterhoeven 	[0] = { ARRAY_SIZE(rza1h_swio_pins), rza1h_swio_pins },
294*077365a9SGeert Uytterhoeven };
295*077365a9SGeert Uytterhoeven 
296*077365a9SGeert Uytterhoeven /* RZ/A1H (r7s72100x) pinmux flags table */
297*077365a9SGeert Uytterhoeven static const struct rza1_pinmux_conf rza1h_pmx_conf = {
298*077365a9SGeert Uytterhoeven 	.bidir_entries	= rza1h_bidir_entries,
299*077365a9SGeert Uytterhoeven 	.swio_entries	= rza1h_swio_entries,
300*077365a9SGeert Uytterhoeven };
301*077365a9SGeert Uytterhoeven 
302*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
303*077365a9SGeert Uytterhoeven  * RZ/A1L (r7s72102) pinmux flags
304*077365a9SGeert Uytterhoeven  */
305*077365a9SGeert Uytterhoeven 
306*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1l_bidir_pins_p1[] = {
307*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 1 },
308*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 1 },
309*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 1 },
310*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 1 },
311*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 1 },
312*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 1 },
313*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 1 },
314*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 1 },
315*077365a9SGeert Uytterhoeven };
316*077365a9SGeert Uytterhoeven 
317*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1l_bidir_pins_p3[] = {
318*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 2 },
319*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 2 },
320*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 2 },
321*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 2 },
322*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 2 },
323*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 2 },
324*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 2 },
325*077365a9SGeert Uytterhoeven 	{ .pin = 12, .func = 2 },
326*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 2 },
327*077365a9SGeert Uytterhoeven };
328*077365a9SGeert Uytterhoeven 
329*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1l_bidir_pins_p4[] = {
330*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 4 },
331*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 2 },
332*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 2 },
333*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 2 },
334*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 2 },
335*077365a9SGeert Uytterhoeven };
336*077365a9SGeert Uytterhoeven 
337*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1l_bidir_pins_p5[] = {
338*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 1 },
339*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 1 },
340*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 1 },
341*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 1 },
342*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 1 },
343*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 1 },
344*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 1 },
345*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 1 },
346*077365a9SGeert Uytterhoeven 	{ .pin = 8, .func = 1 },
347*077365a9SGeert Uytterhoeven 	{ .pin = 9, .func = 1 },
348*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 1 },
349*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 1 },
350*077365a9SGeert Uytterhoeven 	{ .pin = 12, .func = 1 },
351*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 1 },
352*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 1 },
353*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 1 },
354*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 2 },
355*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 2 },
356*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 2 },
357*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 2 },
358*077365a9SGeert Uytterhoeven };
359*077365a9SGeert Uytterhoeven 
360*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1l_bidir_pins_p6[] = {
361*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 1 },
362*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 1 },
363*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 1 },
364*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 1 },
365*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 1 },
366*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 1 },
367*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 1 },
368*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 1 },
369*077365a9SGeert Uytterhoeven 	{ .pin = 8, .func = 1 },
370*077365a9SGeert Uytterhoeven 	{ .pin = 9, .func = 1 },
371*077365a9SGeert Uytterhoeven 	{ .pin = 10, .func = 1 },
372*077365a9SGeert Uytterhoeven 	{ .pin = 11, .func = 1 },
373*077365a9SGeert Uytterhoeven 	{ .pin = 12, .func = 1 },
374*077365a9SGeert Uytterhoeven 	{ .pin = 13, .func = 1 },
375*077365a9SGeert Uytterhoeven 	{ .pin = 14, .func = 1 },
376*077365a9SGeert Uytterhoeven 	{ .pin = 15, .func = 1 },
377*077365a9SGeert Uytterhoeven };
378*077365a9SGeert Uytterhoeven 
379*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1l_bidir_pins_p7[] = {
380*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 2 },
381*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 2 },
382*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 2 },
383*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 2 },
384*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 2 },
385*077365a9SGeert Uytterhoeven 	{ .pin = 2, .func = 3 },
386*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 3 },
387*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 3 },
388*077365a9SGeert Uytterhoeven 	{ .pin = 6, .func = 3 },
389*077365a9SGeert Uytterhoeven 	{ .pin = 7, .func = 3 },
390*077365a9SGeert Uytterhoeven };
391*077365a9SGeert Uytterhoeven 
392*077365a9SGeert Uytterhoeven static const struct rza1_bidir_pin rza1l_bidir_pins_p9[] = {
393*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 2 },
394*077365a9SGeert Uytterhoeven 	{ .pin = 0, .func = 3 },
395*077365a9SGeert Uytterhoeven 	{ .pin = 1, .func = 3 },
396*077365a9SGeert Uytterhoeven 	{ .pin = 3, .func = 3 },
397*077365a9SGeert Uytterhoeven 	{ .pin = 4, .func = 3 },
398*077365a9SGeert Uytterhoeven 	{ .pin = 5, .func = 3 },
399*077365a9SGeert Uytterhoeven };
400*077365a9SGeert Uytterhoeven 
401*077365a9SGeert Uytterhoeven static const struct rza1_swio_pin rza1l_swio_pins[] = {
402*077365a9SGeert Uytterhoeven 	{ .port = 2, .pin = 8, .func = 2, .input = 0 },
403*077365a9SGeert Uytterhoeven 	{ .port = 5, .pin = 6, .func = 3, .input = 0 },
404*077365a9SGeert Uytterhoeven 	{ .port = 6, .pin = 6, .func = 3, .input = 0 },
405*077365a9SGeert Uytterhoeven 	{ .port = 6, .pin = 10, .func = 3, .input = 0 },
406*077365a9SGeert Uytterhoeven 	{ .port = 7, .pin = 10, .func = 2, .input = 0 },
407*077365a9SGeert Uytterhoeven 	{ .port = 8, .pin = 2, .func = 3, .input = 0 },
408*077365a9SGeert Uytterhoeven };
409*077365a9SGeert Uytterhoeven 
410*077365a9SGeert Uytterhoeven static const struct rza1_bidir_entry rza1l_bidir_entries[RZA1_NPORTS] = {
411*077365a9SGeert Uytterhoeven 	[1] = { ARRAY_SIZE(rza1l_bidir_pins_p1), rza1l_bidir_pins_p1 },
412*077365a9SGeert Uytterhoeven 	[3] = { ARRAY_SIZE(rza1l_bidir_pins_p3), rza1l_bidir_pins_p3 },
413*077365a9SGeert Uytterhoeven 	[4] = { ARRAY_SIZE(rza1l_bidir_pins_p4), rza1l_bidir_pins_p4 },
414*077365a9SGeert Uytterhoeven 	[5] = { ARRAY_SIZE(rza1l_bidir_pins_p4), rza1l_bidir_pins_p5 },
415*077365a9SGeert Uytterhoeven 	[6] = { ARRAY_SIZE(rza1l_bidir_pins_p6), rza1l_bidir_pins_p6 },
416*077365a9SGeert Uytterhoeven 	[7] = { ARRAY_SIZE(rza1l_bidir_pins_p7), rza1l_bidir_pins_p7 },
417*077365a9SGeert Uytterhoeven 	[9] = { ARRAY_SIZE(rza1l_bidir_pins_p9), rza1l_bidir_pins_p9 },
418*077365a9SGeert Uytterhoeven };
419*077365a9SGeert Uytterhoeven 
420*077365a9SGeert Uytterhoeven static const struct rza1_swio_entry rza1l_swio_entries[] = {
421*077365a9SGeert Uytterhoeven 	[0] = { ARRAY_SIZE(rza1l_swio_pins), rza1l_swio_pins },
422*077365a9SGeert Uytterhoeven };
423*077365a9SGeert Uytterhoeven 
424*077365a9SGeert Uytterhoeven /* RZ/A1L (r7s72102x) pinmux flags table */
425*077365a9SGeert Uytterhoeven static const struct rza1_pinmux_conf rza1l_pmx_conf = {
426*077365a9SGeert Uytterhoeven 	.bidir_entries	= rza1l_bidir_entries,
427*077365a9SGeert Uytterhoeven 	.swio_entries	= rza1l_swio_entries,
428*077365a9SGeert Uytterhoeven };
429*077365a9SGeert Uytterhoeven 
430*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
431*077365a9SGeert Uytterhoeven  * RZ/A1 types
432*077365a9SGeert Uytterhoeven  */
433*077365a9SGeert Uytterhoeven /**
434*077365a9SGeert Uytterhoeven  * struct rza1_mux_conf - describes a pin multiplexing operation
435*077365a9SGeert Uytterhoeven  *
436*077365a9SGeert Uytterhoeven  * @id: the pin identifier from 0 to RZA1_NPINS
437*077365a9SGeert Uytterhoeven  * @port: the port where pin sits on
438*077365a9SGeert Uytterhoeven  * @pin: pin id
439*077365a9SGeert Uytterhoeven  * @mux_func: alternate function id number
440*077365a9SGeert Uytterhoeven  * @mux_flags: alternate function flags
441*077365a9SGeert Uytterhoeven  * @value: output value to set the pin to
442*077365a9SGeert Uytterhoeven  */
443*077365a9SGeert Uytterhoeven struct rza1_mux_conf {
444*077365a9SGeert Uytterhoeven 	u16 id;
445*077365a9SGeert Uytterhoeven 	u8 port;
446*077365a9SGeert Uytterhoeven 	u8 pin;
447*077365a9SGeert Uytterhoeven 	u8 mux_func;
448*077365a9SGeert Uytterhoeven 	u8 mux_flags;
449*077365a9SGeert Uytterhoeven 	u8 value;
450*077365a9SGeert Uytterhoeven };
451*077365a9SGeert Uytterhoeven 
452*077365a9SGeert Uytterhoeven /**
453*077365a9SGeert Uytterhoeven  * struct rza1_port - describes a pin port
454*077365a9SGeert Uytterhoeven  *
455*077365a9SGeert Uytterhoeven  * This is mostly useful to lock register writes per-bank and not globally.
456*077365a9SGeert Uytterhoeven  *
457*077365a9SGeert Uytterhoeven  * @lock: protect access to HW registers
458*077365a9SGeert Uytterhoeven  * @id: port number
459*077365a9SGeert Uytterhoeven  * @base: logical address base
460*077365a9SGeert Uytterhoeven  * @pins: pins sitting on this port
461*077365a9SGeert Uytterhoeven  */
462*077365a9SGeert Uytterhoeven struct rza1_port {
463*077365a9SGeert Uytterhoeven 	spinlock_t lock;
464*077365a9SGeert Uytterhoeven 	unsigned int id;
465*077365a9SGeert Uytterhoeven 	void __iomem *base;
466*077365a9SGeert Uytterhoeven 	struct pinctrl_pin_desc *pins;
467*077365a9SGeert Uytterhoeven };
468*077365a9SGeert Uytterhoeven 
469*077365a9SGeert Uytterhoeven /**
470*077365a9SGeert Uytterhoeven  * struct rza1_pinctrl - RZ pincontroller device
471*077365a9SGeert Uytterhoeven  *
472*077365a9SGeert Uytterhoeven  * @dev: parent device structure
473*077365a9SGeert Uytterhoeven  * @mutex: protect [pinctrl|pinmux]_generic functions
474*077365a9SGeert Uytterhoeven  * @base: logical address base
475*077365a9SGeert Uytterhoeven  * @nport: number of pin controller ports
476*077365a9SGeert Uytterhoeven  * @ports: pin controller banks
477*077365a9SGeert Uytterhoeven  * @pins: pin array for pinctrl core
478*077365a9SGeert Uytterhoeven  * @desc: pincontroller desc for pinctrl core
479*077365a9SGeert Uytterhoeven  * @pctl: pinctrl device
480*077365a9SGeert Uytterhoeven  * @data: device specific data
481*077365a9SGeert Uytterhoeven  */
482*077365a9SGeert Uytterhoeven struct rza1_pinctrl {
483*077365a9SGeert Uytterhoeven 	struct device *dev;
484*077365a9SGeert Uytterhoeven 
485*077365a9SGeert Uytterhoeven 	struct mutex mutex;
486*077365a9SGeert Uytterhoeven 
487*077365a9SGeert Uytterhoeven 	void __iomem *base;
488*077365a9SGeert Uytterhoeven 
489*077365a9SGeert Uytterhoeven 	unsigned int nport;
490*077365a9SGeert Uytterhoeven 	struct rza1_port *ports;
491*077365a9SGeert Uytterhoeven 
492*077365a9SGeert Uytterhoeven 	struct pinctrl_pin_desc *pins;
493*077365a9SGeert Uytterhoeven 	struct pinctrl_desc desc;
494*077365a9SGeert Uytterhoeven 	struct pinctrl_dev *pctl;
495*077365a9SGeert Uytterhoeven 
496*077365a9SGeert Uytterhoeven 	const void *data;
497*077365a9SGeert Uytterhoeven };
498*077365a9SGeert Uytterhoeven 
499*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
500*077365a9SGeert Uytterhoeven  * RZ/A1 pinmux flags
501*077365a9SGeert Uytterhoeven  */
502*077365a9SGeert Uytterhoeven static inline bool rza1_pinmux_get_bidir(unsigned int port,
503*077365a9SGeert Uytterhoeven 					 unsigned int pin,
504*077365a9SGeert Uytterhoeven 					 unsigned int func,
505*077365a9SGeert Uytterhoeven 					 const struct rza1_bidir_entry *table)
506*077365a9SGeert Uytterhoeven {
507*077365a9SGeert Uytterhoeven 	const struct rza1_bidir_entry *entry = &table[port];
508*077365a9SGeert Uytterhoeven 	const struct rza1_bidir_pin *bidir_pin;
509*077365a9SGeert Uytterhoeven 	unsigned int i;
510*077365a9SGeert Uytterhoeven 
511*077365a9SGeert Uytterhoeven 	for (i = 0; i < entry->npins; ++i) {
512*077365a9SGeert Uytterhoeven 		bidir_pin = &entry->pins[i];
513*077365a9SGeert Uytterhoeven 		if (bidir_pin->pin == pin && bidir_pin->func == func)
514*077365a9SGeert Uytterhoeven 			return true;
515*077365a9SGeert Uytterhoeven 	}
516*077365a9SGeert Uytterhoeven 
517*077365a9SGeert Uytterhoeven 	return false;
518*077365a9SGeert Uytterhoeven }
519*077365a9SGeert Uytterhoeven 
520*077365a9SGeert Uytterhoeven static inline int rza1_pinmux_get_swio(unsigned int port,
521*077365a9SGeert Uytterhoeven 				       unsigned int pin,
522*077365a9SGeert Uytterhoeven 				       unsigned int func,
523*077365a9SGeert Uytterhoeven 				       const struct rza1_swio_entry *table)
524*077365a9SGeert Uytterhoeven {
525*077365a9SGeert Uytterhoeven 	const struct rza1_swio_pin *swio_pin;
526*077365a9SGeert Uytterhoeven 	unsigned int i;
527*077365a9SGeert Uytterhoeven 
528*077365a9SGeert Uytterhoeven 
529*077365a9SGeert Uytterhoeven 	for (i = 0; i < table->npins; ++i) {
530*077365a9SGeert Uytterhoeven 		swio_pin = &table->pins[i];
531*077365a9SGeert Uytterhoeven 		if (swio_pin->port == port && swio_pin->pin == pin &&
532*077365a9SGeert Uytterhoeven 		    swio_pin->func == func)
533*077365a9SGeert Uytterhoeven 			return swio_pin->input;
534*077365a9SGeert Uytterhoeven 	}
535*077365a9SGeert Uytterhoeven 
536*077365a9SGeert Uytterhoeven 	return -ENOENT;
537*077365a9SGeert Uytterhoeven }
538*077365a9SGeert Uytterhoeven 
539*077365a9SGeert Uytterhoeven /*
540*077365a9SGeert Uytterhoeven  * rza1_pinmux_get_flags() - return pinmux flags associated to a pin
541*077365a9SGeert Uytterhoeven  */
542*077365a9SGeert Uytterhoeven static unsigned int rza1_pinmux_get_flags(unsigned int port, unsigned int pin,
543*077365a9SGeert Uytterhoeven 					  unsigned int func,
544*077365a9SGeert Uytterhoeven 					  struct rza1_pinctrl *rza1_pctl)
545*077365a9SGeert Uytterhoeven 
546*077365a9SGeert Uytterhoeven {
547*077365a9SGeert Uytterhoeven 	const struct rza1_pinmux_conf *pmx_conf = rza1_pctl->data;
548*077365a9SGeert Uytterhoeven 	const struct rza1_bidir_entry *bidir_entries = pmx_conf->bidir_entries;
549*077365a9SGeert Uytterhoeven 	const struct rza1_swio_entry *swio_entries = pmx_conf->swio_entries;
550*077365a9SGeert Uytterhoeven 	unsigned int pmx_flags = 0;
551*077365a9SGeert Uytterhoeven 	int ret;
552*077365a9SGeert Uytterhoeven 
553*077365a9SGeert Uytterhoeven 	if (rza1_pinmux_get_bidir(port, pin, func, bidir_entries))
554*077365a9SGeert Uytterhoeven 		pmx_flags |= MUX_FLAGS_BIDIR;
555*077365a9SGeert Uytterhoeven 
556*077365a9SGeert Uytterhoeven 	ret = rza1_pinmux_get_swio(port, pin, func, swio_entries);
557*077365a9SGeert Uytterhoeven 	if (ret == 0)
558*077365a9SGeert Uytterhoeven 		pmx_flags |= MUX_FLAGS_SWIO_OUTPUT;
559*077365a9SGeert Uytterhoeven 	else if (ret > 0)
560*077365a9SGeert Uytterhoeven 		pmx_flags |= MUX_FLAGS_SWIO_INPUT;
561*077365a9SGeert Uytterhoeven 
562*077365a9SGeert Uytterhoeven 	return pmx_flags;
563*077365a9SGeert Uytterhoeven }
564*077365a9SGeert Uytterhoeven 
565*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
566*077365a9SGeert Uytterhoeven  * RZ/A1 SoC operations
567*077365a9SGeert Uytterhoeven  */
568*077365a9SGeert Uytterhoeven 
569*077365a9SGeert Uytterhoeven /*
570*077365a9SGeert Uytterhoeven  * rza1_set_bit() - un-locked set/clear a single bit in pin configuration
571*077365a9SGeert Uytterhoeven  *		    registers
572*077365a9SGeert Uytterhoeven  */
573*077365a9SGeert Uytterhoeven static inline void rza1_set_bit(struct rza1_port *port, unsigned int reg,
574*077365a9SGeert Uytterhoeven 				unsigned int bit, bool set)
575*077365a9SGeert Uytterhoeven {
576*077365a9SGeert Uytterhoeven 	void __iomem *mem = RZA1_ADDR(port->base, reg, port->id);
577*077365a9SGeert Uytterhoeven 	u16 val = ioread16(mem);
578*077365a9SGeert Uytterhoeven 
579*077365a9SGeert Uytterhoeven 	if (set)
580*077365a9SGeert Uytterhoeven 		val |= BIT(bit);
581*077365a9SGeert Uytterhoeven 	else
582*077365a9SGeert Uytterhoeven 		val &= ~BIT(bit);
583*077365a9SGeert Uytterhoeven 
584*077365a9SGeert Uytterhoeven 	iowrite16(val, mem);
585*077365a9SGeert Uytterhoeven }
586*077365a9SGeert Uytterhoeven 
587*077365a9SGeert Uytterhoeven static inline unsigned int rza1_get_bit(struct rza1_port *port,
588*077365a9SGeert Uytterhoeven 					unsigned int reg, unsigned int bit)
589*077365a9SGeert Uytterhoeven {
590*077365a9SGeert Uytterhoeven 	void __iomem *mem = RZA1_ADDR(port->base, reg, port->id);
591*077365a9SGeert Uytterhoeven 
592*077365a9SGeert Uytterhoeven 	return ioread16(mem) & BIT(bit);
593*077365a9SGeert Uytterhoeven }
594*077365a9SGeert Uytterhoeven 
595*077365a9SGeert Uytterhoeven /**
596*077365a9SGeert Uytterhoeven  * rza1_pin_reset() - reset a pin to default initial state
597*077365a9SGeert Uytterhoeven  *
598*077365a9SGeert Uytterhoeven  * Reset pin state disabling input buffer and bi-directional control,
599*077365a9SGeert Uytterhoeven  * and configure it as input port.
600*077365a9SGeert Uytterhoeven  * Note that pin is now configured with direction as input but with input
601*077365a9SGeert Uytterhoeven  * buffer disabled. This implies the pin value cannot be read in this state.
602*077365a9SGeert Uytterhoeven  *
603*077365a9SGeert Uytterhoeven  * @port: port where pin sits on
604*077365a9SGeert Uytterhoeven  * @pin: pin offset
605*077365a9SGeert Uytterhoeven  */
606*077365a9SGeert Uytterhoeven static void rza1_pin_reset(struct rza1_port *port, unsigned int pin)
607*077365a9SGeert Uytterhoeven {
608*077365a9SGeert Uytterhoeven 	unsigned long irqflags;
609*077365a9SGeert Uytterhoeven 
610*077365a9SGeert Uytterhoeven 	spin_lock_irqsave(&port->lock, irqflags);
611*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PIBC_REG, pin, 0);
612*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PBDC_REG, pin, 0);
613*077365a9SGeert Uytterhoeven 
614*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PM_REG, pin, 1);
615*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PMC_REG, pin, 0);
616*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PIPC_REG, pin, 0);
617*077365a9SGeert Uytterhoeven 	spin_unlock_irqrestore(&port->lock, irqflags);
618*077365a9SGeert Uytterhoeven }
619*077365a9SGeert Uytterhoeven 
620*077365a9SGeert Uytterhoeven /**
621*077365a9SGeert Uytterhoeven  * rza1_pin_set_direction() - set I/O direction on a pin in port mode
622*077365a9SGeert Uytterhoeven  *
623*077365a9SGeert Uytterhoeven  * When running in output port mode keep PBDC enabled to allow reading the
624*077365a9SGeert Uytterhoeven  * pin value from PPR.
625*077365a9SGeert Uytterhoeven  *
626*077365a9SGeert Uytterhoeven  * @port: port where pin sits on
627*077365a9SGeert Uytterhoeven  * @pin: pin offset
628*077365a9SGeert Uytterhoeven  * @input: input enable/disable flag
629*077365a9SGeert Uytterhoeven  */
630*077365a9SGeert Uytterhoeven static inline void rza1_pin_set_direction(struct rza1_port *port,
631*077365a9SGeert Uytterhoeven 					  unsigned int pin, bool input)
632*077365a9SGeert Uytterhoeven {
633*077365a9SGeert Uytterhoeven 	unsigned long irqflags;
634*077365a9SGeert Uytterhoeven 
635*077365a9SGeert Uytterhoeven 	spin_lock_irqsave(&port->lock, irqflags);
636*077365a9SGeert Uytterhoeven 
637*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PIBC_REG, pin, 1);
638*077365a9SGeert Uytterhoeven 	if (input) {
639*077365a9SGeert Uytterhoeven 		rza1_set_bit(port, RZA1_PM_REG, pin, 1);
640*077365a9SGeert Uytterhoeven 		rza1_set_bit(port, RZA1_PBDC_REG, pin, 0);
641*077365a9SGeert Uytterhoeven 	} else {
642*077365a9SGeert Uytterhoeven 		rza1_set_bit(port, RZA1_PM_REG, pin, 0);
643*077365a9SGeert Uytterhoeven 		rza1_set_bit(port, RZA1_PBDC_REG, pin, 1);
644*077365a9SGeert Uytterhoeven 	}
645*077365a9SGeert Uytterhoeven 
646*077365a9SGeert Uytterhoeven 	spin_unlock_irqrestore(&port->lock, irqflags);
647*077365a9SGeert Uytterhoeven }
648*077365a9SGeert Uytterhoeven 
649*077365a9SGeert Uytterhoeven static inline void rza1_pin_set(struct rza1_port *port, unsigned int pin,
650*077365a9SGeert Uytterhoeven 				unsigned int value)
651*077365a9SGeert Uytterhoeven {
652*077365a9SGeert Uytterhoeven 	unsigned long irqflags;
653*077365a9SGeert Uytterhoeven 
654*077365a9SGeert Uytterhoeven 	spin_lock_irqsave(&port->lock, irqflags);
655*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_P_REG, pin, !!value);
656*077365a9SGeert Uytterhoeven 	spin_unlock_irqrestore(&port->lock, irqflags);
657*077365a9SGeert Uytterhoeven }
658*077365a9SGeert Uytterhoeven 
659*077365a9SGeert Uytterhoeven static inline int rza1_pin_get(struct rza1_port *port, unsigned int pin)
660*077365a9SGeert Uytterhoeven {
661*077365a9SGeert Uytterhoeven 	return rza1_get_bit(port, RZA1_PPR_REG, pin);
662*077365a9SGeert Uytterhoeven }
663*077365a9SGeert Uytterhoeven 
664*077365a9SGeert Uytterhoeven /**
665*077365a9SGeert Uytterhoeven  * rza1_pin_mux_single() - configure pin multiplexing on a single pin
666*077365a9SGeert Uytterhoeven  *
667*077365a9SGeert Uytterhoeven  * @rza1_pctl: RZ/A1 pin controller device
668*077365a9SGeert Uytterhoeven  * @mux_conf: pin multiplexing descriptor
669*077365a9SGeert Uytterhoeven  */
670*077365a9SGeert Uytterhoeven static int rza1_pin_mux_single(struct rza1_pinctrl *rza1_pctl,
671*077365a9SGeert Uytterhoeven 			       struct rza1_mux_conf *mux_conf)
672*077365a9SGeert Uytterhoeven {
673*077365a9SGeert Uytterhoeven 	struct rza1_port *port = &rza1_pctl->ports[mux_conf->port];
674*077365a9SGeert Uytterhoeven 	unsigned int pin = mux_conf->pin;
675*077365a9SGeert Uytterhoeven 	u8 mux_func = mux_conf->mux_func;
676*077365a9SGeert Uytterhoeven 	u8 mux_flags = mux_conf->mux_flags;
677*077365a9SGeert Uytterhoeven 	u8 mux_flags_from_table;
678*077365a9SGeert Uytterhoeven 
679*077365a9SGeert Uytterhoeven 	rza1_pin_reset(port, pin);
680*077365a9SGeert Uytterhoeven 
681*077365a9SGeert Uytterhoeven 	/* SWIO pinmux flags coming from DT are high precedence */
682*077365a9SGeert Uytterhoeven 	mux_flags_from_table = rza1_pinmux_get_flags(port->id, pin, mux_func,
683*077365a9SGeert Uytterhoeven 						     rza1_pctl);
684*077365a9SGeert Uytterhoeven 	if (mux_flags)
685*077365a9SGeert Uytterhoeven 		mux_flags |= (mux_flags_from_table & MUX_FLAGS_BIDIR);
686*077365a9SGeert Uytterhoeven 	else
687*077365a9SGeert Uytterhoeven 		mux_flags = mux_flags_from_table;
688*077365a9SGeert Uytterhoeven 
689*077365a9SGeert Uytterhoeven 	if (mux_flags & MUX_FLAGS_BIDIR)
690*077365a9SGeert Uytterhoeven 		rza1_set_bit(port, RZA1_PBDC_REG, pin, 1);
691*077365a9SGeert Uytterhoeven 
692*077365a9SGeert Uytterhoeven 	/*
693*077365a9SGeert Uytterhoeven 	 * Enable alternate function mode and select it.
694*077365a9SGeert Uytterhoeven 	 *
695*077365a9SGeert Uytterhoeven 	 * Be careful here: the pin mux sub-nodes in device tree
696*077365a9SGeert Uytterhoeven 	 * enumerate alternate functions from 1 to 8;
697*077365a9SGeert Uytterhoeven 	 * subtract 1 before using macros to match registers configuration
698*077365a9SGeert Uytterhoeven 	 * which expects numbers from 0 to 7 instead.
699*077365a9SGeert Uytterhoeven 	 *
700*077365a9SGeert Uytterhoeven 	 * ----------------------------------------------------
701*077365a9SGeert Uytterhoeven 	 * Alternate mode selection table:
702*077365a9SGeert Uytterhoeven 	 *
703*077365a9SGeert Uytterhoeven 	 * PMC	PFC	PFCE	PFCAE	(mux_func - 1)
704*077365a9SGeert Uytterhoeven 	 * 1	0	0	0	0
705*077365a9SGeert Uytterhoeven 	 * 1	1	0	0	1
706*077365a9SGeert Uytterhoeven 	 * 1	0	1	0	2
707*077365a9SGeert Uytterhoeven 	 * 1	1	1	0	3
708*077365a9SGeert Uytterhoeven 	 * 1	0	0	1	4
709*077365a9SGeert Uytterhoeven 	 * 1	1	0	1	5
710*077365a9SGeert Uytterhoeven 	 * 1	0	1	1	6
711*077365a9SGeert Uytterhoeven 	 * 1	1	1	1	7
712*077365a9SGeert Uytterhoeven 	 * ----------------------------------------------------
713*077365a9SGeert Uytterhoeven 	 */
714*077365a9SGeert Uytterhoeven 	mux_func -= 1;
715*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PFC_REG, pin, mux_func & MUX_FUNC_PFC_MASK);
716*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PFCE_REG, pin, mux_func & MUX_FUNC_PFCE_MASK);
717*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PFCEA_REG, pin, mux_func & MUX_FUNC_PFCEA_MASK);
718*077365a9SGeert Uytterhoeven 
719*077365a9SGeert Uytterhoeven 	/*
720*077365a9SGeert Uytterhoeven 	 * All alternate functions except a few need PIPCn = 1.
721*077365a9SGeert Uytterhoeven 	 * If PIPCn has to stay disabled (SW IO mode), configure PMn according
722*077365a9SGeert Uytterhoeven 	 * to I/O direction specified by pin configuration -after- PMC has been
723*077365a9SGeert Uytterhoeven 	 * set to one.
724*077365a9SGeert Uytterhoeven 	 */
725*077365a9SGeert Uytterhoeven 	if (mux_flags & (MUX_FLAGS_SWIO_INPUT | MUX_FLAGS_SWIO_OUTPUT))
726*077365a9SGeert Uytterhoeven 		rza1_set_bit(port, RZA1_PM_REG, pin,
727*077365a9SGeert Uytterhoeven 			     mux_flags & MUX_FLAGS_SWIO_INPUT);
728*077365a9SGeert Uytterhoeven 	else
729*077365a9SGeert Uytterhoeven 		rza1_set_bit(port, RZA1_PIPC_REG, pin, 1);
730*077365a9SGeert Uytterhoeven 
731*077365a9SGeert Uytterhoeven 	rza1_set_bit(port, RZA1_PMC_REG, pin, 1);
732*077365a9SGeert Uytterhoeven 
733*077365a9SGeert Uytterhoeven 	return 0;
734*077365a9SGeert Uytterhoeven }
735*077365a9SGeert Uytterhoeven 
736*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
737*077365a9SGeert Uytterhoeven  * gpio operations
738*077365a9SGeert Uytterhoeven  */
739*077365a9SGeert Uytterhoeven 
740*077365a9SGeert Uytterhoeven /**
741*077365a9SGeert Uytterhoeven  * rza1_gpio_request() - configure pin in port mode
742*077365a9SGeert Uytterhoeven  *
743*077365a9SGeert Uytterhoeven  * Configure a pin as gpio (port mode).
744*077365a9SGeert Uytterhoeven  * After reset, the pin is in input mode with input buffer disabled.
745*077365a9SGeert Uytterhoeven  * To use the pin as input or output, set_direction shall be called first
746*077365a9SGeert Uytterhoeven  *
747*077365a9SGeert Uytterhoeven  * @chip: gpio chip where the gpio sits on
748*077365a9SGeert Uytterhoeven  * @gpio: gpio offset
749*077365a9SGeert Uytterhoeven  */
750*077365a9SGeert Uytterhoeven static int rza1_gpio_request(struct gpio_chip *chip, unsigned int gpio)
751*077365a9SGeert Uytterhoeven {
752*077365a9SGeert Uytterhoeven 	struct rza1_port *port = gpiochip_get_data(chip);
753*077365a9SGeert Uytterhoeven 
754*077365a9SGeert Uytterhoeven 	rza1_pin_reset(port, gpio);
755*077365a9SGeert Uytterhoeven 
756*077365a9SGeert Uytterhoeven 	return 0;
757*077365a9SGeert Uytterhoeven }
758*077365a9SGeert Uytterhoeven 
759*077365a9SGeert Uytterhoeven /**
760*077365a9SGeert Uytterhoeven  * rza1_gpio_disable_free() - reset a pin
761*077365a9SGeert Uytterhoeven  *
762*077365a9SGeert Uytterhoeven  * Surprisingly, disable_free a gpio, is equivalent to request it.
763*077365a9SGeert Uytterhoeven  * Reset pin to port mode, with input buffer disabled. This overwrites all
764*077365a9SGeert Uytterhoeven  * port direction settings applied with set_direction
765*077365a9SGeert Uytterhoeven  *
766*077365a9SGeert Uytterhoeven  * @chip: gpio chip where the gpio sits on
767*077365a9SGeert Uytterhoeven  * @gpio: gpio offset
768*077365a9SGeert Uytterhoeven  */
769*077365a9SGeert Uytterhoeven static void rza1_gpio_free(struct gpio_chip *chip, unsigned int gpio)
770*077365a9SGeert Uytterhoeven {
771*077365a9SGeert Uytterhoeven 	struct rza1_port *port = gpiochip_get_data(chip);
772*077365a9SGeert Uytterhoeven 
773*077365a9SGeert Uytterhoeven 	rza1_pin_reset(port, gpio);
774*077365a9SGeert Uytterhoeven }
775*077365a9SGeert Uytterhoeven 
776*077365a9SGeert Uytterhoeven static int rza1_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
777*077365a9SGeert Uytterhoeven {
778*077365a9SGeert Uytterhoeven 	struct rza1_port *port = gpiochip_get_data(chip);
779*077365a9SGeert Uytterhoeven 
780*077365a9SGeert Uytterhoeven 	if (rza1_get_bit(port, RZA1_PM_REG, gpio))
781*077365a9SGeert Uytterhoeven 		return GPIO_LINE_DIRECTION_IN;
782*077365a9SGeert Uytterhoeven 
783*077365a9SGeert Uytterhoeven 	return GPIO_LINE_DIRECTION_OUT;
784*077365a9SGeert Uytterhoeven }
785*077365a9SGeert Uytterhoeven 
786*077365a9SGeert Uytterhoeven static int rza1_gpio_direction_input(struct gpio_chip *chip,
787*077365a9SGeert Uytterhoeven 				     unsigned int gpio)
788*077365a9SGeert Uytterhoeven {
789*077365a9SGeert Uytterhoeven 	struct rza1_port *port = gpiochip_get_data(chip);
790*077365a9SGeert Uytterhoeven 
791*077365a9SGeert Uytterhoeven 	rza1_pin_set_direction(port, gpio, true);
792*077365a9SGeert Uytterhoeven 
793*077365a9SGeert Uytterhoeven 	return 0;
794*077365a9SGeert Uytterhoeven }
795*077365a9SGeert Uytterhoeven 
796*077365a9SGeert Uytterhoeven static int rza1_gpio_direction_output(struct gpio_chip *chip,
797*077365a9SGeert Uytterhoeven 				      unsigned int gpio,
798*077365a9SGeert Uytterhoeven 				      int value)
799*077365a9SGeert Uytterhoeven {
800*077365a9SGeert Uytterhoeven 	struct rza1_port *port = gpiochip_get_data(chip);
801*077365a9SGeert Uytterhoeven 
802*077365a9SGeert Uytterhoeven 	/* Set value before driving pin direction */
803*077365a9SGeert Uytterhoeven 	rza1_pin_set(port, gpio, value);
804*077365a9SGeert Uytterhoeven 	rza1_pin_set_direction(port, gpio, false);
805*077365a9SGeert Uytterhoeven 
806*077365a9SGeert Uytterhoeven 	return 0;
807*077365a9SGeert Uytterhoeven }
808*077365a9SGeert Uytterhoeven 
809*077365a9SGeert Uytterhoeven /**
810*077365a9SGeert Uytterhoeven  * rza1_gpio_get() - read a gpio pin value
811*077365a9SGeert Uytterhoeven  *
812*077365a9SGeert Uytterhoeven  * Read gpio pin value through PPR register.
813*077365a9SGeert Uytterhoeven  * Requires bi-directional mode to work when reading the value of a pin
814*077365a9SGeert Uytterhoeven  * in output mode
815*077365a9SGeert Uytterhoeven  *
816*077365a9SGeert Uytterhoeven  * @chip: gpio chip where the gpio sits on
817*077365a9SGeert Uytterhoeven  * @gpio: gpio offset
818*077365a9SGeert Uytterhoeven  */
819*077365a9SGeert Uytterhoeven static int rza1_gpio_get(struct gpio_chip *chip, unsigned int gpio)
820*077365a9SGeert Uytterhoeven {
821*077365a9SGeert Uytterhoeven 	struct rza1_port *port = gpiochip_get_data(chip);
822*077365a9SGeert Uytterhoeven 
823*077365a9SGeert Uytterhoeven 	return rza1_pin_get(port, gpio);
824*077365a9SGeert Uytterhoeven }
825*077365a9SGeert Uytterhoeven 
826*077365a9SGeert Uytterhoeven static void rza1_gpio_set(struct gpio_chip *chip, unsigned int gpio,
827*077365a9SGeert Uytterhoeven 			  int value)
828*077365a9SGeert Uytterhoeven {
829*077365a9SGeert Uytterhoeven 	struct rza1_port *port = gpiochip_get_data(chip);
830*077365a9SGeert Uytterhoeven 
831*077365a9SGeert Uytterhoeven 	rza1_pin_set(port, gpio, value);
832*077365a9SGeert Uytterhoeven }
833*077365a9SGeert Uytterhoeven 
834*077365a9SGeert Uytterhoeven static const struct gpio_chip rza1_gpiochip_template = {
835*077365a9SGeert Uytterhoeven 	.request		= rza1_gpio_request,
836*077365a9SGeert Uytterhoeven 	.free			= rza1_gpio_free,
837*077365a9SGeert Uytterhoeven 	.get_direction		= rza1_gpio_get_direction,
838*077365a9SGeert Uytterhoeven 	.direction_input	= rza1_gpio_direction_input,
839*077365a9SGeert Uytterhoeven 	.direction_output	= rza1_gpio_direction_output,
840*077365a9SGeert Uytterhoeven 	.get			= rza1_gpio_get,
841*077365a9SGeert Uytterhoeven 	.set			= rza1_gpio_set,
842*077365a9SGeert Uytterhoeven };
843*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
844*077365a9SGeert Uytterhoeven  * pinctrl operations
845*077365a9SGeert Uytterhoeven  */
846*077365a9SGeert Uytterhoeven 
847*077365a9SGeert Uytterhoeven /**
848*077365a9SGeert Uytterhoeven  * rza1_dt_node_pin_count() - Count number of pins in a dt node or in all its
849*077365a9SGeert Uytterhoeven  *			      children sub-nodes
850*077365a9SGeert Uytterhoeven  *
851*077365a9SGeert Uytterhoeven  * @np: device tree node to parse
852*077365a9SGeert Uytterhoeven  */
853*077365a9SGeert Uytterhoeven static int rza1_dt_node_pin_count(struct device_node *np)
854*077365a9SGeert Uytterhoeven {
855*077365a9SGeert Uytterhoeven 	struct device_node *child;
856*077365a9SGeert Uytterhoeven 	struct property *of_pins;
857*077365a9SGeert Uytterhoeven 	unsigned int npins;
858*077365a9SGeert Uytterhoeven 
859*077365a9SGeert Uytterhoeven 	of_pins = of_find_property(np, "pinmux", NULL);
860*077365a9SGeert Uytterhoeven 	if (of_pins)
861*077365a9SGeert Uytterhoeven 		return of_pins->length / sizeof(u32);
862*077365a9SGeert Uytterhoeven 
863*077365a9SGeert Uytterhoeven 	npins = 0;
864*077365a9SGeert Uytterhoeven 	for_each_child_of_node(np, child) {
865*077365a9SGeert Uytterhoeven 		of_pins = of_find_property(child, "pinmux", NULL);
866*077365a9SGeert Uytterhoeven 		if (!of_pins) {
867*077365a9SGeert Uytterhoeven 			of_node_put(child);
868*077365a9SGeert Uytterhoeven 			return -EINVAL;
869*077365a9SGeert Uytterhoeven 		}
870*077365a9SGeert Uytterhoeven 
871*077365a9SGeert Uytterhoeven 		npins += of_pins->length / sizeof(u32);
872*077365a9SGeert Uytterhoeven 	}
873*077365a9SGeert Uytterhoeven 
874*077365a9SGeert Uytterhoeven 	return npins;
875*077365a9SGeert Uytterhoeven }
876*077365a9SGeert Uytterhoeven 
877*077365a9SGeert Uytterhoeven /**
878*077365a9SGeert Uytterhoeven  * rza1_parse_pmx_function() - parse a pin mux sub-node
879*077365a9SGeert Uytterhoeven  *
880*077365a9SGeert Uytterhoeven  * @rza1_pctl: RZ/A1 pin controller device
881*077365a9SGeert Uytterhoeven  * @np: of pmx sub-node
882*077365a9SGeert Uytterhoeven  * @mux_confs: array of pin mux configurations to fill with parsed info
883*077365a9SGeert Uytterhoeven  * @grpins: array of pin ids to mux
884*077365a9SGeert Uytterhoeven  */
885*077365a9SGeert Uytterhoeven static int rza1_parse_pinmux_node(struct rza1_pinctrl *rza1_pctl,
886*077365a9SGeert Uytterhoeven 				  struct device_node *np,
887*077365a9SGeert Uytterhoeven 				  struct rza1_mux_conf *mux_confs,
888*077365a9SGeert Uytterhoeven 				  unsigned int *grpins)
889*077365a9SGeert Uytterhoeven {
890*077365a9SGeert Uytterhoeven 	struct pinctrl_dev *pctldev = rza1_pctl->pctl;
891*077365a9SGeert Uytterhoeven 	char const *prop_name = "pinmux";
892*077365a9SGeert Uytterhoeven 	unsigned long *pin_configs;
893*077365a9SGeert Uytterhoeven 	unsigned int npin_configs;
894*077365a9SGeert Uytterhoeven 	struct property *of_pins;
895*077365a9SGeert Uytterhoeven 	unsigned int npins;
896*077365a9SGeert Uytterhoeven 	u8 pinmux_flags;
897*077365a9SGeert Uytterhoeven 	unsigned int i;
898*077365a9SGeert Uytterhoeven 	int ret;
899*077365a9SGeert Uytterhoeven 
900*077365a9SGeert Uytterhoeven 	of_pins = of_find_property(np, prop_name, NULL);
901*077365a9SGeert Uytterhoeven 	if (!of_pins) {
902*077365a9SGeert Uytterhoeven 		dev_dbg(rza1_pctl->dev, "Missing %s property\n", prop_name);
903*077365a9SGeert Uytterhoeven 		return -ENOENT;
904*077365a9SGeert Uytterhoeven 	}
905*077365a9SGeert Uytterhoeven 	npins = of_pins->length / sizeof(u32);
906*077365a9SGeert Uytterhoeven 
907*077365a9SGeert Uytterhoeven 	/*
908*077365a9SGeert Uytterhoeven 	 * Collect pin configuration properties: they apply to all pins in
909*077365a9SGeert Uytterhoeven 	 * this sub-node
910*077365a9SGeert Uytterhoeven 	 */
911*077365a9SGeert Uytterhoeven 	ret = pinconf_generic_parse_dt_config(np, pctldev, &pin_configs,
912*077365a9SGeert Uytterhoeven 					      &npin_configs);
913*077365a9SGeert Uytterhoeven 	if (ret) {
914*077365a9SGeert Uytterhoeven 		dev_err(rza1_pctl->dev,
915*077365a9SGeert Uytterhoeven 			"Unable to parse pin configuration options for %pOFn\n",
916*077365a9SGeert Uytterhoeven 			np);
917*077365a9SGeert Uytterhoeven 		return ret;
918*077365a9SGeert Uytterhoeven 	}
919*077365a9SGeert Uytterhoeven 
920*077365a9SGeert Uytterhoeven 	/*
921*077365a9SGeert Uytterhoeven 	 * Create a mask with pinmux flags from pin configuration;
922*077365a9SGeert Uytterhoeven 	 * very few pins (TIOC[0-4][A|B|C|D] require SWIO direction
923*077365a9SGeert Uytterhoeven 	 * specified in device tree.
924*077365a9SGeert Uytterhoeven 	 */
925*077365a9SGeert Uytterhoeven 	pinmux_flags = 0;
926*077365a9SGeert Uytterhoeven 	for (i = 0; i < npin_configs && pinmux_flags == 0; i++)
927*077365a9SGeert Uytterhoeven 		switch (pinconf_to_config_param(pin_configs[i])) {
928*077365a9SGeert Uytterhoeven 		case PIN_CONFIG_INPUT_ENABLE:
929*077365a9SGeert Uytterhoeven 			pinmux_flags |= MUX_FLAGS_SWIO_INPUT;
930*077365a9SGeert Uytterhoeven 			break;
931*077365a9SGeert Uytterhoeven 		case PIN_CONFIG_OUTPUT:	/* for DT backwards compatibility */
932*077365a9SGeert Uytterhoeven 		case PIN_CONFIG_OUTPUT_ENABLE:
933*077365a9SGeert Uytterhoeven 			pinmux_flags |= MUX_FLAGS_SWIO_OUTPUT;
934*077365a9SGeert Uytterhoeven 		default:
935*077365a9SGeert Uytterhoeven 			break;
936*077365a9SGeert Uytterhoeven 
937*077365a9SGeert Uytterhoeven 		}
938*077365a9SGeert Uytterhoeven 
939*077365a9SGeert Uytterhoeven 	kfree(pin_configs);
940*077365a9SGeert Uytterhoeven 
941*077365a9SGeert Uytterhoeven 	/* Collect pin positions and their mux settings. */
942*077365a9SGeert Uytterhoeven 	for (i = 0; i < npins; ++i) {
943*077365a9SGeert Uytterhoeven 		u32 of_pinconf;
944*077365a9SGeert Uytterhoeven 		struct rza1_mux_conf *mux_conf = &mux_confs[i];
945*077365a9SGeert Uytterhoeven 
946*077365a9SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, prop_name, i, &of_pinconf);
947*077365a9SGeert Uytterhoeven 		if (ret)
948*077365a9SGeert Uytterhoeven 			return ret;
949*077365a9SGeert Uytterhoeven 
950*077365a9SGeert Uytterhoeven 		mux_conf->id		= of_pinconf & MUX_PIN_ID_MASK;
951*077365a9SGeert Uytterhoeven 		mux_conf->port		= RZA1_PIN_ID_TO_PORT(mux_conf->id);
952*077365a9SGeert Uytterhoeven 		mux_conf->pin		= RZA1_PIN_ID_TO_PIN(mux_conf->id);
953*077365a9SGeert Uytterhoeven 		mux_conf->mux_func	= MUX_FUNC(of_pinconf);
954*077365a9SGeert Uytterhoeven 		mux_conf->mux_flags	= pinmux_flags;
955*077365a9SGeert Uytterhoeven 
956*077365a9SGeert Uytterhoeven 		if (mux_conf->port >= RZA1_NPORTS ||
957*077365a9SGeert Uytterhoeven 		    mux_conf->pin >= RZA1_PINS_PER_PORT) {
958*077365a9SGeert Uytterhoeven 			dev_err(rza1_pctl->dev,
959*077365a9SGeert Uytterhoeven 				"Wrong port %u pin %u for %s property\n",
960*077365a9SGeert Uytterhoeven 				mux_conf->port, mux_conf->pin, prop_name);
961*077365a9SGeert Uytterhoeven 			return -EINVAL;
962*077365a9SGeert Uytterhoeven 		}
963*077365a9SGeert Uytterhoeven 
964*077365a9SGeert Uytterhoeven 		grpins[i] = mux_conf->id;
965*077365a9SGeert Uytterhoeven 	}
966*077365a9SGeert Uytterhoeven 
967*077365a9SGeert Uytterhoeven 	return npins;
968*077365a9SGeert Uytterhoeven }
969*077365a9SGeert Uytterhoeven 
970*077365a9SGeert Uytterhoeven /**
971*077365a9SGeert Uytterhoeven  * rza1_dt_node_to_map() - map a pin mux node to a function/group
972*077365a9SGeert Uytterhoeven  *
973*077365a9SGeert Uytterhoeven  * Parse and register a pin mux function.
974*077365a9SGeert Uytterhoeven  *
975*077365a9SGeert Uytterhoeven  * @pctldev: pin controller device
976*077365a9SGeert Uytterhoeven  * @np: device tree node to parse
977*077365a9SGeert Uytterhoeven  * @map: pointer to pin map (output)
978*077365a9SGeert Uytterhoeven  * @num_maps: number of collected maps (output)
979*077365a9SGeert Uytterhoeven  */
980*077365a9SGeert Uytterhoeven static int rza1_dt_node_to_map(struct pinctrl_dev *pctldev,
981*077365a9SGeert Uytterhoeven 			       struct device_node *np,
982*077365a9SGeert Uytterhoeven 			       struct pinctrl_map **map,
983*077365a9SGeert Uytterhoeven 			       unsigned int *num_maps)
984*077365a9SGeert Uytterhoeven {
985*077365a9SGeert Uytterhoeven 	struct rza1_pinctrl *rza1_pctl = pinctrl_dev_get_drvdata(pctldev);
986*077365a9SGeert Uytterhoeven 	struct rza1_mux_conf *mux_confs, *mux_conf;
987*077365a9SGeert Uytterhoeven 	unsigned int *grpins, *grpin;
988*077365a9SGeert Uytterhoeven 	struct device_node *child;
989*077365a9SGeert Uytterhoeven 	const char *grpname;
990*077365a9SGeert Uytterhoeven 	const char **fngrps;
991*077365a9SGeert Uytterhoeven 	int ret, npins;
992*077365a9SGeert Uytterhoeven 	int gsel, fsel;
993*077365a9SGeert Uytterhoeven 
994*077365a9SGeert Uytterhoeven 	npins = rza1_dt_node_pin_count(np);
995*077365a9SGeert Uytterhoeven 	if (npins < 0) {
996*077365a9SGeert Uytterhoeven 		dev_err(rza1_pctl->dev, "invalid pinmux node structure\n");
997*077365a9SGeert Uytterhoeven 		return -EINVAL;
998*077365a9SGeert Uytterhoeven 	}
999*077365a9SGeert Uytterhoeven 
1000*077365a9SGeert Uytterhoeven 	/*
1001*077365a9SGeert Uytterhoeven 	 * Functions are made of 1 group only;
1002*077365a9SGeert Uytterhoeven 	 * in fact, functions and groups are identical for this pin controller
1003*077365a9SGeert Uytterhoeven 	 * except that functions carry an array of per-pin mux configuration
1004*077365a9SGeert Uytterhoeven 	 * settings.
1005*077365a9SGeert Uytterhoeven 	 */
1006*077365a9SGeert Uytterhoeven 	mux_confs = devm_kcalloc(rza1_pctl->dev, npins, sizeof(*mux_confs),
1007*077365a9SGeert Uytterhoeven 				 GFP_KERNEL);
1008*077365a9SGeert Uytterhoeven 	grpins = devm_kcalloc(rza1_pctl->dev, npins, sizeof(*grpins),
1009*077365a9SGeert Uytterhoeven 			      GFP_KERNEL);
1010*077365a9SGeert Uytterhoeven 	fngrps = devm_kzalloc(rza1_pctl->dev, sizeof(*fngrps), GFP_KERNEL);
1011*077365a9SGeert Uytterhoeven 
1012*077365a9SGeert Uytterhoeven 	if (!mux_confs || !grpins || !fngrps)
1013*077365a9SGeert Uytterhoeven 		return -ENOMEM;
1014*077365a9SGeert Uytterhoeven 
1015*077365a9SGeert Uytterhoeven 	/*
1016*077365a9SGeert Uytterhoeven 	 * Parse the pinmux node.
1017*077365a9SGeert Uytterhoeven 	 * If the node does not contain "pinmux" property (-ENOENT)
1018*077365a9SGeert Uytterhoeven 	 * that property shall be specified in all its children sub-nodes.
1019*077365a9SGeert Uytterhoeven 	 */
1020*077365a9SGeert Uytterhoeven 	mux_conf = &mux_confs[0];
1021*077365a9SGeert Uytterhoeven 	grpin = &grpins[0];
1022*077365a9SGeert Uytterhoeven 
1023*077365a9SGeert Uytterhoeven 	ret = rza1_parse_pinmux_node(rza1_pctl, np, mux_conf, grpin);
1024*077365a9SGeert Uytterhoeven 	if (ret == -ENOENT)
1025*077365a9SGeert Uytterhoeven 		for_each_child_of_node(np, child) {
1026*077365a9SGeert Uytterhoeven 			ret = rza1_parse_pinmux_node(rza1_pctl, child, mux_conf,
1027*077365a9SGeert Uytterhoeven 						     grpin);
1028*077365a9SGeert Uytterhoeven 			if (ret < 0) {
1029*077365a9SGeert Uytterhoeven 				of_node_put(child);
1030*077365a9SGeert Uytterhoeven 				return ret;
1031*077365a9SGeert Uytterhoeven 			}
1032*077365a9SGeert Uytterhoeven 
1033*077365a9SGeert Uytterhoeven 			grpin += ret;
1034*077365a9SGeert Uytterhoeven 			mux_conf += ret;
1035*077365a9SGeert Uytterhoeven 		}
1036*077365a9SGeert Uytterhoeven 	else if (ret < 0)
1037*077365a9SGeert Uytterhoeven 		return ret;
1038*077365a9SGeert Uytterhoeven 
1039*077365a9SGeert Uytterhoeven 	/* Register pin group and function name to pinctrl_generic */
1040*077365a9SGeert Uytterhoeven 	grpname	= np->name;
1041*077365a9SGeert Uytterhoeven 	fngrps[0] = grpname;
1042*077365a9SGeert Uytterhoeven 
1043*077365a9SGeert Uytterhoeven 	mutex_lock(&rza1_pctl->mutex);
1044*077365a9SGeert Uytterhoeven 	gsel = pinctrl_generic_add_group(pctldev, grpname, grpins, npins,
1045*077365a9SGeert Uytterhoeven 					 NULL);
1046*077365a9SGeert Uytterhoeven 	if (gsel < 0) {
1047*077365a9SGeert Uytterhoeven 		mutex_unlock(&rza1_pctl->mutex);
1048*077365a9SGeert Uytterhoeven 		return gsel;
1049*077365a9SGeert Uytterhoeven 	}
1050*077365a9SGeert Uytterhoeven 
1051*077365a9SGeert Uytterhoeven 	fsel = pinmux_generic_add_function(pctldev, grpname, fngrps, 1,
1052*077365a9SGeert Uytterhoeven 					   mux_confs);
1053*077365a9SGeert Uytterhoeven 	if (fsel < 0) {
1054*077365a9SGeert Uytterhoeven 		ret = fsel;
1055*077365a9SGeert Uytterhoeven 		goto remove_group;
1056*077365a9SGeert Uytterhoeven 	}
1057*077365a9SGeert Uytterhoeven 
1058*077365a9SGeert Uytterhoeven 	dev_info(rza1_pctl->dev, "Parsed function and group %s with %d pins\n",
1059*077365a9SGeert Uytterhoeven 				 grpname, npins);
1060*077365a9SGeert Uytterhoeven 
1061*077365a9SGeert Uytterhoeven 	/* Create map where to retrieve function and mux settings from */
1062*077365a9SGeert Uytterhoeven 	*num_maps = 0;
1063*077365a9SGeert Uytterhoeven 	*map = kzalloc(sizeof(**map), GFP_KERNEL);
1064*077365a9SGeert Uytterhoeven 	if (!*map) {
1065*077365a9SGeert Uytterhoeven 		ret = -ENOMEM;
1066*077365a9SGeert Uytterhoeven 		goto remove_function;
1067*077365a9SGeert Uytterhoeven 	}
1068*077365a9SGeert Uytterhoeven 
1069*077365a9SGeert Uytterhoeven 	(*map)->type = PIN_MAP_TYPE_MUX_GROUP;
1070*077365a9SGeert Uytterhoeven 	(*map)->data.mux.group = np->name;
1071*077365a9SGeert Uytterhoeven 	(*map)->data.mux.function = np->name;
1072*077365a9SGeert Uytterhoeven 	*num_maps = 1;
1073*077365a9SGeert Uytterhoeven 	mutex_unlock(&rza1_pctl->mutex);
1074*077365a9SGeert Uytterhoeven 
1075*077365a9SGeert Uytterhoeven 	return 0;
1076*077365a9SGeert Uytterhoeven 
1077*077365a9SGeert Uytterhoeven remove_function:
1078*077365a9SGeert Uytterhoeven 	pinmux_generic_remove_function(pctldev, fsel);
1079*077365a9SGeert Uytterhoeven 
1080*077365a9SGeert Uytterhoeven remove_group:
1081*077365a9SGeert Uytterhoeven 	pinctrl_generic_remove_group(pctldev, gsel);
1082*077365a9SGeert Uytterhoeven 	mutex_unlock(&rza1_pctl->mutex);
1083*077365a9SGeert Uytterhoeven 
1084*077365a9SGeert Uytterhoeven 	dev_info(rza1_pctl->dev, "Unable to parse function and group %s\n",
1085*077365a9SGeert Uytterhoeven 				 grpname);
1086*077365a9SGeert Uytterhoeven 
1087*077365a9SGeert Uytterhoeven 	return ret;
1088*077365a9SGeert Uytterhoeven }
1089*077365a9SGeert Uytterhoeven 
1090*077365a9SGeert Uytterhoeven static void rza1_dt_free_map(struct pinctrl_dev *pctldev,
1091*077365a9SGeert Uytterhoeven 			     struct pinctrl_map *map, unsigned int num_maps)
1092*077365a9SGeert Uytterhoeven {
1093*077365a9SGeert Uytterhoeven 	kfree(map);
1094*077365a9SGeert Uytterhoeven }
1095*077365a9SGeert Uytterhoeven 
1096*077365a9SGeert Uytterhoeven static const struct pinctrl_ops rza1_pinctrl_ops = {
1097*077365a9SGeert Uytterhoeven 	.get_groups_count	= pinctrl_generic_get_group_count,
1098*077365a9SGeert Uytterhoeven 	.get_group_name		= pinctrl_generic_get_group_name,
1099*077365a9SGeert Uytterhoeven 	.get_group_pins		= pinctrl_generic_get_group_pins,
1100*077365a9SGeert Uytterhoeven 	.dt_node_to_map		= rza1_dt_node_to_map,
1101*077365a9SGeert Uytterhoeven 	.dt_free_map		= rza1_dt_free_map,
1102*077365a9SGeert Uytterhoeven };
1103*077365a9SGeert Uytterhoeven 
1104*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
1105*077365a9SGeert Uytterhoeven  * pinmux operations
1106*077365a9SGeert Uytterhoeven  */
1107*077365a9SGeert Uytterhoeven 
1108*077365a9SGeert Uytterhoeven /**
1109*077365a9SGeert Uytterhoeven  * rza1_set_mux() - retrieve pins from a group and apply their mux settings
1110*077365a9SGeert Uytterhoeven  *
1111*077365a9SGeert Uytterhoeven  * @pctldev: pin controller device
1112*077365a9SGeert Uytterhoeven  * @selector: function selector
1113*077365a9SGeert Uytterhoeven  * @group: group selector
1114*077365a9SGeert Uytterhoeven  */
1115*077365a9SGeert Uytterhoeven static int rza1_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
1116*077365a9SGeert Uytterhoeven 			   unsigned int group)
1117*077365a9SGeert Uytterhoeven {
1118*077365a9SGeert Uytterhoeven 	struct rza1_pinctrl *rza1_pctl = pinctrl_dev_get_drvdata(pctldev);
1119*077365a9SGeert Uytterhoeven 	struct rza1_mux_conf *mux_confs;
1120*077365a9SGeert Uytterhoeven 	struct function_desc *func;
1121*077365a9SGeert Uytterhoeven 	struct group_desc *grp;
1122*077365a9SGeert Uytterhoeven 	int i;
1123*077365a9SGeert Uytterhoeven 
1124*077365a9SGeert Uytterhoeven 	grp = pinctrl_generic_get_group(pctldev, group);
1125*077365a9SGeert Uytterhoeven 	if (!grp)
1126*077365a9SGeert Uytterhoeven 		return -EINVAL;
1127*077365a9SGeert Uytterhoeven 
1128*077365a9SGeert Uytterhoeven 	func = pinmux_generic_get_function(pctldev, selector);
1129*077365a9SGeert Uytterhoeven 	if (!func)
1130*077365a9SGeert Uytterhoeven 		return -EINVAL;
1131*077365a9SGeert Uytterhoeven 
1132*077365a9SGeert Uytterhoeven 	mux_confs = (struct rza1_mux_conf *)func->data;
1133*077365a9SGeert Uytterhoeven 	for (i = 0; i < grp->num_pins; ++i) {
1134*077365a9SGeert Uytterhoeven 		int ret;
1135*077365a9SGeert Uytterhoeven 
1136*077365a9SGeert Uytterhoeven 		ret = rza1_pin_mux_single(rza1_pctl, &mux_confs[i]);
1137*077365a9SGeert Uytterhoeven 		if (ret)
1138*077365a9SGeert Uytterhoeven 			return ret;
1139*077365a9SGeert Uytterhoeven 	}
1140*077365a9SGeert Uytterhoeven 
1141*077365a9SGeert Uytterhoeven 	return 0;
1142*077365a9SGeert Uytterhoeven }
1143*077365a9SGeert Uytterhoeven 
1144*077365a9SGeert Uytterhoeven static const struct pinmux_ops rza1_pinmux_ops = {
1145*077365a9SGeert Uytterhoeven 	.get_functions_count	= pinmux_generic_get_function_count,
1146*077365a9SGeert Uytterhoeven 	.get_function_name	= pinmux_generic_get_function_name,
1147*077365a9SGeert Uytterhoeven 	.get_function_groups	= pinmux_generic_get_function_groups,
1148*077365a9SGeert Uytterhoeven 	.set_mux		= rza1_set_mux,
1149*077365a9SGeert Uytterhoeven 	.strict			= true,
1150*077365a9SGeert Uytterhoeven };
1151*077365a9SGeert Uytterhoeven 
1152*077365a9SGeert Uytterhoeven /* ----------------------------------------------------------------------------
1153*077365a9SGeert Uytterhoeven  * RZ/A1 pin controller driver operations
1154*077365a9SGeert Uytterhoeven  */
1155*077365a9SGeert Uytterhoeven 
1156*077365a9SGeert Uytterhoeven static unsigned int rza1_count_gpio_chips(struct device_node *np)
1157*077365a9SGeert Uytterhoeven {
1158*077365a9SGeert Uytterhoeven 	struct device_node *child;
1159*077365a9SGeert Uytterhoeven 	unsigned int count = 0;
1160*077365a9SGeert Uytterhoeven 
1161*077365a9SGeert Uytterhoeven 	for_each_child_of_node(np, child) {
1162*077365a9SGeert Uytterhoeven 		if (!of_property_read_bool(child, "gpio-controller"))
1163*077365a9SGeert Uytterhoeven 			continue;
1164*077365a9SGeert Uytterhoeven 
1165*077365a9SGeert Uytterhoeven 		count++;
1166*077365a9SGeert Uytterhoeven 	}
1167*077365a9SGeert Uytterhoeven 
1168*077365a9SGeert Uytterhoeven 	return count;
1169*077365a9SGeert Uytterhoeven }
1170*077365a9SGeert Uytterhoeven 
1171*077365a9SGeert Uytterhoeven /**
1172*077365a9SGeert Uytterhoeven  * rza1_parse_gpiochip() - parse and register a gpio chip and pin range
1173*077365a9SGeert Uytterhoeven  *
1174*077365a9SGeert Uytterhoeven  * The gpio controller subnode shall provide a "gpio-ranges" list property as
1175*077365a9SGeert Uytterhoeven  * defined by gpio device tree binding documentation.
1176*077365a9SGeert Uytterhoeven  *
1177*077365a9SGeert Uytterhoeven  * @rza1_pctl: RZ/A1 pin controller device
1178*077365a9SGeert Uytterhoeven  * @np: of gpio-controller node
1179*077365a9SGeert Uytterhoeven  * @chip: gpio chip to register to gpiolib
1180*077365a9SGeert Uytterhoeven  * @range: pin range to register to pinctrl core
1181*077365a9SGeert Uytterhoeven  */
1182*077365a9SGeert Uytterhoeven static int rza1_parse_gpiochip(struct rza1_pinctrl *rza1_pctl,
1183*077365a9SGeert Uytterhoeven 			       struct device_node *np,
1184*077365a9SGeert Uytterhoeven 			       struct gpio_chip *chip,
1185*077365a9SGeert Uytterhoeven 			       struct pinctrl_gpio_range *range)
1186*077365a9SGeert Uytterhoeven {
1187*077365a9SGeert Uytterhoeven 	const char *list_name = "gpio-ranges";
1188*077365a9SGeert Uytterhoeven 	struct of_phandle_args of_args;
1189*077365a9SGeert Uytterhoeven 	unsigned int gpioport;
1190*077365a9SGeert Uytterhoeven 	u32 pinctrl_base;
1191*077365a9SGeert Uytterhoeven 	int ret;
1192*077365a9SGeert Uytterhoeven 
1193*077365a9SGeert Uytterhoeven 	ret = of_parse_phandle_with_fixed_args(np, list_name, 3, 0, &of_args);
1194*077365a9SGeert Uytterhoeven 	if (ret) {
1195*077365a9SGeert Uytterhoeven 		dev_err(rza1_pctl->dev, "Unable to parse %s list property\n",
1196*077365a9SGeert Uytterhoeven 			list_name);
1197*077365a9SGeert Uytterhoeven 		return ret;
1198*077365a9SGeert Uytterhoeven 	}
1199*077365a9SGeert Uytterhoeven 
1200*077365a9SGeert Uytterhoeven 	/*
1201*077365a9SGeert Uytterhoeven 	 * Find out on which port this gpio-chip maps to by inspecting the
1202*077365a9SGeert Uytterhoeven 	 * second argument of the "gpio-ranges" property.
1203*077365a9SGeert Uytterhoeven 	 */
1204*077365a9SGeert Uytterhoeven 	pinctrl_base = of_args.args[1];
1205*077365a9SGeert Uytterhoeven 	gpioport = RZA1_PIN_ID_TO_PORT(pinctrl_base);
1206*077365a9SGeert Uytterhoeven 	if (gpioport >= RZA1_NPORTS) {
1207*077365a9SGeert Uytterhoeven 		dev_err(rza1_pctl->dev,
1208*077365a9SGeert Uytterhoeven 			"Invalid values in property %s\n", list_name);
1209*077365a9SGeert Uytterhoeven 		return -EINVAL;
1210*077365a9SGeert Uytterhoeven 	}
1211*077365a9SGeert Uytterhoeven 
1212*077365a9SGeert Uytterhoeven 	*chip		= rza1_gpiochip_template;
1213*077365a9SGeert Uytterhoeven 	chip->base	= -1;
1214*077365a9SGeert Uytterhoeven 	chip->label	= devm_kasprintf(rza1_pctl->dev, GFP_KERNEL, "%pOFn",
1215*077365a9SGeert Uytterhoeven 					 np);
1216*077365a9SGeert Uytterhoeven 	if (!chip->label)
1217*077365a9SGeert Uytterhoeven 		return -ENOMEM;
1218*077365a9SGeert Uytterhoeven 
1219*077365a9SGeert Uytterhoeven 	chip->ngpio	= of_args.args[2];
1220*077365a9SGeert Uytterhoeven 	chip->of_node	= np;
1221*077365a9SGeert Uytterhoeven 	chip->parent	= rza1_pctl->dev;
1222*077365a9SGeert Uytterhoeven 
1223*077365a9SGeert Uytterhoeven 	range->id	= gpioport;
1224*077365a9SGeert Uytterhoeven 	range->name	= chip->label;
1225*077365a9SGeert Uytterhoeven 	range->pin_base	= range->base = pinctrl_base;
1226*077365a9SGeert Uytterhoeven 	range->npins	= of_args.args[2];
1227*077365a9SGeert Uytterhoeven 	range->gc	= chip;
1228*077365a9SGeert Uytterhoeven 
1229*077365a9SGeert Uytterhoeven 	ret = devm_gpiochip_add_data(rza1_pctl->dev, chip,
1230*077365a9SGeert Uytterhoeven 				     &rza1_pctl->ports[gpioport]);
1231*077365a9SGeert Uytterhoeven 	if (ret)
1232*077365a9SGeert Uytterhoeven 		return ret;
1233*077365a9SGeert Uytterhoeven 
1234*077365a9SGeert Uytterhoeven 	pinctrl_add_gpio_range(rza1_pctl->pctl, range);
1235*077365a9SGeert Uytterhoeven 
1236*077365a9SGeert Uytterhoeven 	dev_dbg(rza1_pctl->dev, "Parsed gpiochip %s with %d pins\n",
1237*077365a9SGeert Uytterhoeven 		chip->label, chip->ngpio);
1238*077365a9SGeert Uytterhoeven 
1239*077365a9SGeert Uytterhoeven 	return 0;
1240*077365a9SGeert Uytterhoeven }
1241*077365a9SGeert Uytterhoeven 
1242*077365a9SGeert Uytterhoeven /**
1243*077365a9SGeert Uytterhoeven  * rza1_gpio_register() - parse DT to collect gpio-chips and gpio-ranges
1244*077365a9SGeert Uytterhoeven  *
1245*077365a9SGeert Uytterhoeven  * @rza1_pctl: RZ/A1 pin controller device
1246*077365a9SGeert Uytterhoeven  */
1247*077365a9SGeert Uytterhoeven static int rza1_gpio_register(struct rza1_pinctrl *rza1_pctl)
1248*077365a9SGeert Uytterhoeven {
1249*077365a9SGeert Uytterhoeven 	struct device_node *np = rza1_pctl->dev->of_node;
1250*077365a9SGeert Uytterhoeven 	struct pinctrl_gpio_range *gpio_ranges;
1251*077365a9SGeert Uytterhoeven 	struct gpio_chip *gpio_chips;
1252*077365a9SGeert Uytterhoeven 	struct device_node *child;
1253*077365a9SGeert Uytterhoeven 	unsigned int ngpiochips;
1254*077365a9SGeert Uytterhoeven 	unsigned int i;
1255*077365a9SGeert Uytterhoeven 	int ret;
1256*077365a9SGeert Uytterhoeven 
1257*077365a9SGeert Uytterhoeven 	ngpiochips = rza1_count_gpio_chips(np);
1258*077365a9SGeert Uytterhoeven 	if (ngpiochips == 0) {
1259*077365a9SGeert Uytterhoeven 		dev_dbg(rza1_pctl->dev, "No gpiochip registered\n");
1260*077365a9SGeert Uytterhoeven 		return 0;
1261*077365a9SGeert Uytterhoeven 	}
1262*077365a9SGeert Uytterhoeven 
1263*077365a9SGeert Uytterhoeven 	gpio_chips = devm_kcalloc(rza1_pctl->dev, ngpiochips,
1264*077365a9SGeert Uytterhoeven 				  sizeof(*gpio_chips), GFP_KERNEL);
1265*077365a9SGeert Uytterhoeven 	gpio_ranges = devm_kcalloc(rza1_pctl->dev, ngpiochips,
1266*077365a9SGeert Uytterhoeven 				   sizeof(*gpio_ranges), GFP_KERNEL);
1267*077365a9SGeert Uytterhoeven 	if (!gpio_chips || !gpio_ranges)
1268*077365a9SGeert Uytterhoeven 		return -ENOMEM;
1269*077365a9SGeert Uytterhoeven 
1270*077365a9SGeert Uytterhoeven 	i = 0;
1271*077365a9SGeert Uytterhoeven 	for_each_child_of_node(np, child) {
1272*077365a9SGeert Uytterhoeven 		if (!of_property_read_bool(child, "gpio-controller"))
1273*077365a9SGeert Uytterhoeven 			continue;
1274*077365a9SGeert Uytterhoeven 
1275*077365a9SGeert Uytterhoeven 		ret = rza1_parse_gpiochip(rza1_pctl, child, &gpio_chips[i],
1276*077365a9SGeert Uytterhoeven 					  &gpio_ranges[i]);
1277*077365a9SGeert Uytterhoeven 		if (ret) {
1278*077365a9SGeert Uytterhoeven 			of_node_put(child);
1279*077365a9SGeert Uytterhoeven 			return ret;
1280*077365a9SGeert Uytterhoeven 		}
1281*077365a9SGeert Uytterhoeven 
1282*077365a9SGeert Uytterhoeven 		++i;
1283*077365a9SGeert Uytterhoeven 	}
1284*077365a9SGeert Uytterhoeven 
1285*077365a9SGeert Uytterhoeven 	dev_info(rza1_pctl->dev, "Registered %u gpio controllers\n", i);
1286*077365a9SGeert Uytterhoeven 
1287*077365a9SGeert Uytterhoeven 	return 0;
1288*077365a9SGeert Uytterhoeven }
1289*077365a9SGeert Uytterhoeven 
1290*077365a9SGeert Uytterhoeven /**
1291*077365a9SGeert Uytterhoeven  * rza1_pinctrl_register() - Enumerate pins, ports and gpiochips; register
1292*077365a9SGeert Uytterhoeven  *			     them to pinctrl and gpio cores.
1293*077365a9SGeert Uytterhoeven  *
1294*077365a9SGeert Uytterhoeven  * @rza1_pctl: RZ/A1 pin controller device
1295*077365a9SGeert Uytterhoeven  */
1296*077365a9SGeert Uytterhoeven static int rza1_pinctrl_register(struct rza1_pinctrl *rza1_pctl)
1297*077365a9SGeert Uytterhoeven {
1298*077365a9SGeert Uytterhoeven 	struct pinctrl_pin_desc *pins;
1299*077365a9SGeert Uytterhoeven 	struct rza1_port *ports;
1300*077365a9SGeert Uytterhoeven 	unsigned int i;
1301*077365a9SGeert Uytterhoeven 	int ret;
1302*077365a9SGeert Uytterhoeven 
1303*077365a9SGeert Uytterhoeven 	pins = devm_kcalloc(rza1_pctl->dev, RZA1_NPINS, sizeof(*pins),
1304*077365a9SGeert Uytterhoeven 			    GFP_KERNEL);
1305*077365a9SGeert Uytterhoeven 	ports = devm_kcalloc(rza1_pctl->dev, RZA1_NPORTS, sizeof(*ports),
1306*077365a9SGeert Uytterhoeven 			     GFP_KERNEL);
1307*077365a9SGeert Uytterhoeven 	if (!pins || !ports)
1308*077365a9SGeert Uytterhoeven 		return -ENOMEM;
1309*077365a9SGeert Uytterhoeven 
1310*077365a9SGeert Uytterhoeven 	rza1_pctl->pins		= pins;
1311*077365a9SGeert Uytterhoeven 	rza1_pctl->desc.pins	= pins;
1312*077365a9SGeert Uytterhoeven 	rza1_pctl->desc.npins	= RZA1_NPINS;
1313*077365a9SGeert Uytterhoeven 	rza1_pctl->ports	= ports;
1314*077365a9SGeert Uytterhoeven 
1315*077365a9SGeert Uytterhoeven 	for (i = 0; i < RZA1_NPINS; ++i) {
1316*077365a9SGeert Uytterhoeven 		unsigned int pin = RZA1_PIN_ID_TO_PIN(i);
1317*077365a9SGeert Uytterhoeven 		unsigned int port = RZA1_PIN_ID_TO_PORT(i);
1318*077365a9SGeert Uytterhoeven 
1319*077365a9SGeert Uytterhoeven 		pins[i].number = i;
1320*077365a9SGeert Uytterhoeven 		pins[i].name = devm_kasprintf(rza1_pctl->dev, GFP_KERNEL,
1321*077365a9SGeert Uytterhoeven 					      "P%u-%u", port, pin);
1322*077365a9SGeert Uytterhoeven 		if (!pins[i].name)
1323*077365a9SGeert Uytterhoeven 			return -ENOMEM;
1324*077365a9SGeert Uytterhoeven 
1325*077365a9SGeert Uytterhoeven 		if (i % RZA1_PINS_PER_PORT == 0) {
1326*077365a9SGeert Uytterhoeven 			/*
1327*077365a9SGeert Uytterhoeven 			 * Setup ports;
1328*077365a9SGeert Uytterhoeven 			 * they provide per-port lock and logical base address.
1329*077365a9SGeert Uytterhoeven 			 */
1330*077365a9SGeert Uytterhoeven 			unsigned int port_id = RZA1_PIN_ID_TO_PORT(i);
1331*077365a9SGeert Uytterhoeven 
1332*077365a9SGeert Uytterhoeven 			ports[port_id].id	= port_id;
1333*077365a9SGeert Uytterhoeven 			ports[port_id].base	= rza1_pctl->base;
1334*077365a9SGeert Uytterhoeven 			ports[port_id].pins	= &pins[i];
1335*077365a9SGeert Uytterhoeven 			spin_lock_init(&ports[port_id].lock);
1336*077365a9SGeert Uytterhoeven 		}
1337*077365a9SGeert Uytterhoeven 	}
1338*077365a9SGeert Uytterhoeven 
1339*077365a9SGeert Uytterhoeven 	ret = devm_pinctrl_register_and_init(rza1_pctl->dev, &rza1_pctl->desc,
1340*077365a9SGeert Uytterhoeven 					     rza1_pctl, &rza1_pctl->pctl);
1341*077365a9SGeert Uytterhoeven 	if (ret) {
1342*077365a9SGeert Uytterhoeven 		dev_err(rza1_pctl->dev,
1343*077365a9SGeert Uytterhoeven 			"RZ/A1 pin controller registration failed\n");
1344*077365a9SGeert Uytterhoeven 		return ret;
1345*077365a9SGeert Uytterhoeven 	}
1346*077365a9SGeert Uytterhoeven 
1347*077365a9SGeert Uytterhoeven 	ret = pinctrl_enable(rza1_pctl->pctl);
1348*077365a9SGeert Uytterhoeven 	if (ret) {
1349*077365a9SGeert Uytterhoeven 		dev_err(rza1_pctl->dev,
1350*077365a9SGeert Uytterhoeven 			"RZ/A1 pin controller failed to start\n");
1351*077365a9SGeert Uytterhoeven 		return ret;
1352*077365a9SGeert Uytterhoeven 	}
1353*077365a9SGeert Uytterhoeven 
1354*077365a9SGeert Uytterhoeven 	ret = rza1_gpio_register(rza1_pctl);
1355*077365a9SGeert Uytterhoeven 	if (ret) {
1356*077365a9SGeert Uytterhoeven 		dev_err(rza1_pctl->dev, "RZ/A1 GPIO registration failed\n");
1357*077365a9SGeert Uytterhoeven 		return ret;
1358*077365a9SGeert Uytterhoeven 	}
1359*077365a9SGeert Uytterhoeven 
1360*077365a9SGeert Uytterhoeven 	return 0;
1361*077365a9SGeert Uytterhoeven }
1362*077365a9SGeert Uytterhoeven 
1363*077365a9SGeert Uytterhoeven static int rza1_pinctrl_probe(struct platform_device *pdev)
1364*077365a9SGeert Uytterhoeven {
1365*077365a9SGeert Uytterhoeven 	struct rza1_pinctrl *rza1_pctl;
1366*077365a9SGeert Uytterhoeven 	int ret;
1367*077365a9SGeert Uytterhoeven 
1368*077365a9SGeert Uytterhoeven 	rza1_pctl = devm_kzalloc(&pdev->dev, sizeof(*rza1_pctl), GFP_KERNEL);
1369*077365a9SGeert Uytterhoeven 	if (!rza1_pctl)
1370*077365a9SGeert Uytterhoeven 		return -ENOMEM;
1371*077365a9SGeert Uytterhoeven 
1372*077365a9SGeert Uytterhoeven 	rza1_pctl->dev = &pdev->dev;
1373*077365a9SGeert Uytterhoeven 
1374*077365a9SGeert Uytterhoeven 	rza1_pctl->base = devm_platform_ioremap_resource(pdev, 0);
1375*077365a9SGeert Uytterhoeven 	if (IS_ERR(rza1_pctl->base))
1376*077365a9SGeert Uytterhoeven 		return PTR_ERR(rza1_pctl->base);
1377*077365a9SGeert Uytterhoeven 
1378*077365a9SGeert Uytterhoeven 	mutex_init(&rza1_pctl->mutex);
1379*077365a9SGeert Uytterhoeven 
1380*077365a9SGeert Uytterhoeven 	platform_set_drvdata(pdev, rza1_pctl);
1381*077365a9SGeert Uytterhoeven 
1382*077365a9SGeert Uytterhoeven 	rza1_pctl->desc.name	= DRIVER_NAME;
1383*077365a9SGeert Uytterhoeven 	rza1_pctl->desc.pctlops	= &rza1_pinctrl_ops;
1384*077365a9SGeert Uytterhoeven 	rza1_pctl->desc.pmxops	= &rza1_pinmux_ops;
1385*077365a9SGeert Uytterhoeven 	rza1_pctl->desc.owner	= THIS_MODULE;
1386*077365a9SGeert Uytterhoeven 	rza1_pctl->data		= of_device_get_match_data(&pdev->dev);
1387*077365a9SGeert Uytterhoeven 
1388*077365a9SGeert Uytterhoeven 	ret = rza1_pinctrl_register(rza1_pctl);
1389*077365a9SGeert Uytterhoeven 	if (ret)
1390*077365a9SGeert Uytterhoeven 		return ret;
1391*077365a9SGeert Uytterhoeven 
1392*077365a9SGeert Uytterhoeven 	dev_info(&pdev->dev,
1393*077365a9SGeert Uytterhoeven 		 "RZ/A1 pin controller and gpio successfully registered\n");
1394*077365a9SGeert Uytterhoeven 
1395*077365a9SGeert Uytterhoeven 	return 0;
1396*077365a9SGeert Uytterhoeven }
1397*077365a9SGeert Uytterhoeven 
1398*077365a9SGeert Uytterhoeven static const struct of_device_id rza1_pinctrl_of_match[] = {
1399*077365a9SGeert Uytterhoeven 	{
1400*077365a9SGeert Uytterhoeven 		/* RZ/A1H, RZ/A1M */
1401*077365a9SGeert Uytterhoeven 		.compatible	= "renesas,r7s72100-ports",
1402*077365a9SGeert Uytterhoeven 		.data		= &rza1h_pmx_conf,
1403*077365a9SGeert Uytterhoeven 	},
1404*077365a9SGeert Uytterhoeven 	{
1405*077365a9SGeert Uytterhoeven 		/* RZ/A1L */
1406*077365a9SGeert Uytterhoeven 		.compatible	= "renesas,r7s72102-ports",
1407*077365a9SGeert Uytterhoeven 		.data		= &rza1l_pmx_conf,
1408*077365a9SGeert Uytterhoeven 	},
1409*077365a9SGeert Uytterhoeven 	{ }
1410*077365a9SGeert Uytterhoeven };
1411*077365a9SGeert Uytterhoeven 
1412*077365a9SGeert Uytterhoeven static struct platform_driver rza1_pinctrl_driver = {
1413*077365a9SGeert Uytterhoeven 	.driver = {
1414*077365a9SGeert Uytterhoeven 		.name = DRIVER_NAME,
1415*077365a9SGeert Uytterhoeven 		.of_match_table = rza1_pinctrl_of_match,
1416*077365a9SGeert Uytterhoeven 	},
1417*077365a9SGeert Uytterhoeven 	.probe = rza1_pinctrl_probe,
1418*077365a9SGeert Uytterhoeven };
1419*077365a9SGeert Uytterhoeven 
1420*077365a9SGeert Uytterhoeven static int __init rza1_pinctrl_init(void)
1421*077365a9SGeert Uytterhoeven {
1422*077365a9SGeert Uytterhoeven 	return platform_driver_register(&rza1_pinctrl_driver);
1423*077365a9SGeert Uytterhoeven }
1424*077365a9SGeert Uytterhoeven core_initcall(rza1_pinctrl_init);
1425*077365a9SGeert Uytterhoeven 
1426*077365a9SGeert Uytterhoeven MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org");
1427*077365a9SGeert Uytterhoeven MODULE_DESCRIPTION("Pin and gpio controller driver for Reneas RZ/A1 SoC");
1428*077365a9SGeert Uytterhoeven MODULE_LICENSE("GPL v2");
1429