1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
4  * Copyright (c) 2020 Western Digital Corporation or its affiliates.
5  */
6 #include <linux/io.h>
7 #include <linux/of_device.h>
8 #include <linux/clk.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/platform_device.h>
11 #include <linux/bitfield.h>
12 #include <linux/regmap.h>
13 #include <linux/slab.h>
14 #include <linux/pinctrl/pinctrl.h>
15 #include <linux/pinctrl/pinmux.h>
16 #include <linux/pinctrl/pinconf.h>
17 #include <linux/pinctrl/pinconf-generic.h>
18 #include <linux/io.h>
19 
20 #include <dt-bindings/pinctrl/k210-fpioa.h>
21 
22 #include "core.h"
23 #include "pinconf.h"
24 #include "pinctrl-utils.h"
25 
26 /*
27  * The K210 only implements 8 drive levels, even though
28  * there is register space for 16
29  */
30 #define K210_PC_DRIVE_MASK	GENMASK(11, 8)
31 #define K210_PC_DRIVE_SHIFT	8
32 #define K210_PC_DRIVE_0		(0 << K210_PC_DRIVE_SHIFT)
33 #define K210_PC_DRIVE_1		(1 << K210_PC_DRIVE_SHIFT)
34 #define K210_PC_DRIVE_2		(2 << K210_PC_DRIVE_SHIFT)
35 #define K210_PC_DRIVE_3		(3 << K210_PC_DRIVE_SHIFT)
36 #define K210_PC_DRIVE_4		(4 << K210_PC_DRIVE_SHIFT)
37 #define K210_PC_DRIVE_5		(5 << K210_PC_DRIVE_SHIFT)
38 #define K210_PC_DRIVE_6		(6 << K210_PC_DRIVE_SHIFT)
39 #define K210_PC_DRIVE_7		(7 << K210_PC_DRIVE_SHIFT)
40 #define K210_PC_DRIVE_MAX	7
41 #define K210_PC_MODE_MASK	GENMASK(23, 12)
42 
43 /*
44  * output enabled == PC_OE & (PC_OE_INV ^ FUNCTION_OE)
45  * where FUNCTION_OE is a physical signal from the function.
46  */
47 #define K210_PC_OE		BIT(12) /* Output Enable */
48 #define K210_PC_OE_INV		BIT(13) /* INVert Output Enable */
49 #define K210_PC_DO_OE		BIT(14) /* set Data Out to Output Enable sig */
50 #define K210_PC_DO_INV		BIT(15) /* INVert final Data Output */
51 #define K210_PC_PU		BIT(16) /* Pull Up */
52 #define K210_PC_PD		BIT(17) /* Pull Down */
53 /* Strong pull up not implemented on K210 */
54 #define K210_PC_SL		BIT(19) /* reduce SLew rate */
55 /* Same semantics as OE above */
56 #define K210_PC_IE		BIT(20) /* Input Enable */
57 #define K210_PC_IE_INV		BIT(21) /* INVert Input Enable */
58 #define K210_PC_DI_INV		BIT(22) /* INVert Data Input */
59 #define K210_PC_ST		BIT(23) /* Schmitt Trigger */
60 #define K210_PC_DI		BIT(31) /* raw Data Input */
61 
62 #define K210_PC_BIAS_MASK	(K210_PC_PU & K210_PC_PD)
63 
64 #define K210_PC_MODE_IN		(K210_PC_IE | K210_PC_ST)
65 #define K210_PC_MODE_OUT	(K210_PC_DRIVE_7 | K210_PC_OE)
66 #define K210_PC_MODE_I2C	(K210_PC_MODE_IN | K210_PC_SL | \
67 				 K210_PC_OE | K210_PC_PU)
68 #define K210_PC_MODE_SCCB	(K210_PC_MODE_I2C | \
69 				 K210_PC_OE_INV | K210_PC_IE_INV)
70 #define K210_PC_MODE_SPI	(K210_PC_MODE_IN | K210_PC_IE_INV | \
71 				 K210_PC_MODE_OUT | K210_PC_OE_INV)
72 #define K210_PC_MODE_GPIO	(K210_PC_MODE_IN | K210_PC_MODE_OUT)
73 
74 #define K210_PG_FUNC		GENMASK(7, 0)
75 #define K210_PG_DO		BIT(8)
76 #define K210_PG_PIN		GENMASK(22, 16)
77 
78 /*
79  * struct k210_fpioa: Kendryte K210 FPIOA memory mapped registers
80  * @pins: 48 32-bits IO pin registers
81  * @tie_en: 256 (one per function) input tie enable bits
82  * @tie_val: 256 (one per function) input tie value bits
83  */
84 struct k210_fpioa {
85 	u32 pins[48];
86 	u32 tie_en[8];
87 	u32 tie_val[8];
88 };
89 
90 struct k210_fpioa_data {
91 
92 	struct device *dev;
93 	struct pinctrl_dev *pctl;
94 
95 	struct k210_fpioa __iomem *fpioa;
96 	struct regmap *sysctl_map;
97 	u32 power_offset;
98 	struct clk *clk;
99 	struct clk *pclk;
100 };
101 
102 #define K210_PIN_NAME(i)	("IO_" #i)
103 #define K210_PIN(i)		[(i)] = PINCTRL_PIN((i), K210_PIN_NAME(i))
104 
105 static const struct pinctrl_pin_desc k210_pins[] = {
106 	K210_PIN(0),  K210_PIN(1),  K210_PIN(2),
107 	K210_PIN(3),  K210_PIN(4),  K210_PIN(5),
108 	K210_PIN(6),  K210_PIN(7),  K210_PIN(8),
109 	K210_PIN(9),  K210_PIN(10), K210_PIN(11),
110 	K210_PIN(12), K210_PIN(13), K210_PIN(14),
111 	K210_PIN(15), K210_PIN(16), K210_PIN(17),
112 	K210_PIN(18), K210_PIN(19), K210_PIN(20),
113 	K210_PIN(21), K210_PIN(22), K210_PIN(23),
114 	K210_PIN(24), K210_PIN(25), K210_PIN(26),
115 	K210_PIN(27), K210_PIN(28), K210_PIN(29),
116 	K210_PIN(30), K210_PIN(31), K210_PIN(32),
117 	K210_PIN(33), K210_PIN(34), K210_PIN(35),
118 	K210_PIN(36), K210_PIN(37), K210_PIN(38),
119 	K210_PIN(39), K210_PIN(40), K210_PIN(41),
120 	K210_PIN(42), K210_PIN(43), K210_PIN(44),
121 	K210_PIN(45), K210_PIN(46), K210_PIN(47)
122 };
123 
124 #define K210_NPINS ARRAY_SIZE(k210_pins)
125 
126 /*
127  * Pin groups: each of the 48 programmable pins is a group.
128  * To this are added 8 power domain groups, which for the purposes of
129  * the pin subsystem, contain no pins. The power domain groups only exist
130  * to set the power level. The id should never be used (since there are
131  * no pins 48-55).
132  */
133 static const char *const k210_group_names[] = {
134 	/* The first 48 groups are for pins, one each */
135 	K210_PIN_NAME(0),  K210_PIN_NAME(1),  K210_PIN_NAME(2),
136 	K210_PIN_NAME(3),  K210_PIN_NAME(4),  K210_PIN_NAME(5),
137 	K210_PIN_NAME(6),  K210_PIN_NAME(7),  K210_PIN_NAME(8),
138 	K210_PIN_NAME(9),  K210_PIN_NAME(10), K210_PIN_NAME(11),
139 	K210_PIN_NAME(12), K210_PIN_NAME(13), K210_PIN_NAME(14),
140 	K210_PIN_NAME(15), K210_PIN_NAME(16), K210_PIN_NAME(17),
141 	K210_PIN_NAME(18), K210_PIN_NAME(19), K210_PIN_NAME(20),
142 	K210_PIN_NAME(21), K210_PIN_NAME(22), K210_PIN_NAME(23),
143 	K210_PIN_NAME(24), K210_PIN_NAME(25), K210_PIN_NAME(26),
144 	K210_PIN_NAME(27), K210_PIN_NAME(28), K210_PIN_NAME(29),
145 	K210_PIN_NAME(30), K210_PIN_NAME(31), K210_PIN_NAME(32),
146 	K210_PIN_NAME(33), K210_PIN_NAME(34), K210_PIN_NAME(35),
147 	K210_PIN_NAME(36), K210_PIN_NAME(37), K210_PIN_NAME(38),
148 	K210_PIN_NAME(39), K210_PIN_NAME(40), K210_PIN_NAME(41),
149 	K210_PIN_NAME(42), K210_PIN_NAME(43), K210_PIN_NAME(44),
150 	K210_PIN_NAME(45), K210_PIN_NAME(46), K210_PIN_NAME(47),
151 	[48] = "A0", [49] = "A1", [50] = "A2",
152 	[51] = "B3", [52] = "B4", [53] = "B5",
153 	[54] = "C6", [55] = "C7"
154 };
155 
156 #define K210_NGROUPS	ARRAY_SIZE(k210_group_names)
157 
158 enum k210_pinctrl_mode_id {
159 	K210_PC_DEFAULT_DISABLED,
160 	K210_PC_DEFAULT_IN,
161 	K210_PC_DEFAULT_IN_TIE,
162 	K210_PC_DEFAULT_OUT,
163 	K210_PC_DEFAULT_I2C,
164 	K210_PC_DEFAULT_SCCB,
165 	K210_PC_DEFAULT_SPI,
166 	K210_PC_DEFAULT_GPIO,
167 	K210_PC_DEFAULT_INT13,
168 };
169 
170 #define K210_PC_DEFAULT(mode) \
171 	[K210_PC_DEFAULT_##mode] = K210_PC_MODE_##mode
172 
173 static const u32 k210_pinconf_mode_id_to_mode[] = {
174 	[K210_PC_DEFAULT_DISABLED] = 0,
175 	K210_PC_DEFAULT(IN),
176 	[K210_PC_DEFAULT_IN_TIE] = K210_PC_MODE_IN,
177 	K210_PC_DEFAULT(OUT),
178 	K210_PC_DEFAULT(I2C),
179 	K210_PC_DEFAULT(SCCB),
180 	K210_PC_DEFAULT(SPI),
181 	K210_PC_DEFAULT(GPIO),
182 	[K210_PC_DEFAULT_INT13] = K210_PC_MODE_IN | K210_PC_PU,
183 };
184 
185 #undef DEFAULT
186 
187 /*
188  * Pin functions configuration information.
189  */
190 struct k210_pcf_info {
191 	char name[15];
192 	u8 mode_id;
193 };
194 
195 #define K210_FUNC(id, mode)				\
196 	[K210_PCF_##id] = {				\
197 		.name = #id,				\
198 		.mode_id = K210_PC_DEFAULT_##mode	\
199 	}
200 
201 static const struct k210_pcf_info k210_pcf_infos[] = {
202 	K210_FUNC(JTAG_TCLK,		IN),
203 	K210_FUNC(JTAG_TDI,		IN),
204 	K210_FUNC(JTAG_TMS,		IN),
205 	K210_FUNC(JTAG_TDO,		OUT),
206 	K210_FUNC(SPI0_D0,		SPI),
207 	K210_FUNC(SPI0_D1,		SPI),
208 	K210_FUNC(SPI0_D2,		SPI),
209 	K210_FUNC(SPI0_D3,		SPI),
210 	K210_FUNC(SPI0_D4,		SPI),
211 	K210_FUNC(SPI0_D5,		SPI),
212 	K210_FUNC(SPI0_D6,		SPI),
213 	K210_FUNC(SPI0_D7,		SPI),
214 	K210_FUNC(SPI0_SS0,		OUT),
215 	K210_FUNC(SPI0_SS1,		OUT),
216 	K210_FUNC(SPI0_SS2,		OUT),
217 	K210_FUNC(SPI0_SS3,		OUT),
218 	K210_FUNC(SPI0_ARB,		IN_TIE),
219 	K210_FUNC(SPI0_SCLK,		OUT),
220 	K210_FUNC(UARTHS_RX,		IN),
221 	K210_FUNC(UARTHS_TX,		OUT),
222 	K210_FUNC(RESV6,		IN),
223 	K210_FUNC(RESV7,		IN),
224 	K210_FUNC(CLK_SPI1,		OUT),
225 	K210_FUNC(CLK_I2C1,		OUT),
226 	K210_FUNC(GPIOHS0,		GPIO),
227 	K210_FUNC(GPIOHS1,		GPIO),
228 	K210_FUNC(GPIOHS2,		GPIO),
229 	K210_FUNC(GPIOHS3,		GPIO),
230 	K210_FUNC(GPIOHS4,		GPIO),
231 	K210_FUNC(GPIOHS5,		GPIO),
232 	K210_FUNC(GPIOHS6,		GPIO),
233 	K210_FUNC(GPIOHS7,		GPIO),
234 	K210_FUNC(GPIOHS8,		GPIO),
235 	K210_FUNC(GPIOHS9,		GPIO),
236 	K210_FUNC(GPIOHS10,		GPIO),
237 	K210_FUNC(GPIOHS11,		GPIO),
238 	K210_FUNC(GPIOHS12,		GPIO),
239 	K210_FUNC(GPIOHS13,		GPIO),
240 	K210_FUNC(GPIOHS14,		GPIO),
241 	K210_FUNC(GPIOHS15,		GPIO),
242 	K210_FUNC(GPIOHS16,		GPIO),
243 	K210_FUNC(GPIOHS17,		GPIO),
244 	K210_FUNC(GPIOHS18,		GPIO),
245 	K210_FUNC(GPIOHS19,		GPIO),
246 	K210_FUNC(GPIOHS20,		GPIO),
247 	K210_FUNC(GPIOHS21,		GPIO),
248 	K210_FUNC(GPIOHS22,		GPIO),
249 	K210_FUNC(GPIOHS23,		GPIO),
250 	K210_FUNC(GPIOHS24,		GPIO),
251 	K210_FUNC(GPIOHS25,		GPIO),
252 	K210_FUNC(GPIOHS26,		GPIO),
253 	K210_FUNC(GPIOHS27,		GPIO),
254 	K210_FUNC(GPIOHS28,		GPIO),
255 	K210_FUNC(GPIOHS29,		GPIO),
256 	K210_FUNC(GPIOHS30,		GPIO),
257 	K210_FUNC(GPIOHS31,		GPIO),
258 	K210_FUNC(GPIO0,		GPIO),
259 	K210_FUNC(GPIO1,		GPIO),
260 	K210_FUNC(GPIO2,		GPIO),
261 	K210_FUNC(GPIO3,		GPIO),
262 	K210_FUNC(GPIO4,		GPIO),
263 	K210_FUNC(GPIO5,		GPIO),
264 	K210_FUNC(GPIO6,		GPIO),
265 	K210_FUNC(GPIO7,		GPIO),
266 	K210_FUNC(UART1_RX,		IN),
267 	K210_FUNC(UART1_TX,		OUT),
268 	K210_FUNC(UART2_RX,		IN),
269 	K210_FUNC(UART2_TX,		OUT),
270 	K210_FUNC(UART3_RX,		IN),
271 	K210_FUNC(UART3_TX,		OUT),
272 	K210_FUNC(SPI1_D0,		SPI),
273 	K210_FUNC(SPI1_D1,		SPI),
274 	K210_FUNC(SPI1_D2,		SPI),
275 	K210_FUNC(SPI1_D3,		SPI),
276 	K210_FUNC(SPI1_D4,		SPI),
277 	K210_FUNC(SPI1_D5,		SPI),
278 	K210_FUNC(SPI1_D6,		SPI),
279 	K210_FUNC(SPI1_D7,		SPI),
280 	K210_FUNC(SPI1_SS0,		OUT),
281 	K210_FUNC(SPI1_SS1,		OUT),
282 	K210_FUNC(SPI1_SS2,		OUT),
283 	K210_FUNC(SPI1_SS3,		OUT),
284 	K210_FUNC(SPI1_ARB,		IN_TIE),
285 	K210_FUNC(SPI1_SCLK,		OUT),
286 	K210_FUNC(SPI2_D0,		SPI),
287 	K210_FUNC(SPI2_SS,		IN),
288 	K210_FUNC(SPI2_SCLK,		IN),
289 	K210_FUNC(I2S0_MCLK,		OUT),
290 	K210_FUNC(I2S0_SCLK,		OUT),
291 	K210_FUNC(I2S0_WS,		OUT),
292 	K210_FUNC(I2S0_IN_D0,		IN),
293 	K210_FUNC(I2S0_IN_D1,		IN),
294 	K210_FUNC(I2S0_IN_D2,		IN),
295 	K210_FUNC(I2S0_IN_D3,		IN),
296 	K210_FUNC(I2S0_OUT_D0,		OUT),
297 	K210_FUNC(I2S0_OUT_D1,		OUT),
298 	K210_FUNC(I2S0_OUT_D2,		OUT),
299 	K210_FUNC(I2S0_OUT_D3,		OUT),
300 	K210_FUNC(I2S1_MCLK,		OUT),
301 	K210_FUNC(I2S1_SCLK,		OUT),
302 	K210_FUNC(I2S1_WS,		OUT),
303 	K210_FUNC(I2S1_IN_D0,		IN),
304 	K210_FUNC(I2S1_IN_D1,		IN),
305 	K210_FUNC(I2S1_IN_D2,		IN),
306 	K210_FUNC(I2S1_IN_D3,		IN),
307 	K210_FUNC(I2S1_OUT_D0,		OUT),
308 	K210_FUNC(I2S1_OUT_D1,		OUT),
309 	K210_FUNC(I2S1_OUT_D2,		OUT),
310 	K210_FUNC(I2S1_OUT_D3,		OUT),
311 	K210_FUNC(I2S2_MCLK,		OUT),
312 	K210_FUNC(I2S2_SCLK,		OUT),
313 	K210_FUNC(I2S2_WS,		OUT),
314 	K210_FUNC(I2S2_IN_D0,		IN),
315 	K210_FUNC(I2S2_IN_D1,		IN),
316 	K210_FUNC(I2S2_IN_D2,		IN),
317 	K210_FUNC(I2S2_IN_D3,		IN),
318 	K210_FUNC(I2S2_OUT_D0,		OUT),
319 	K210_FUNC(I2S2_OUT_D1,		OUT),
320 	K210_FUNC(I2S2_OUT_D2,		OUT),
321 	K210_FUNC(I2S2_OUT_D3,		OUT),
322 	K210_FUNC(RESV0,		DISABLED),
323 	K210_FUNC(RESV1,		DISABLED),
324 	K210_FUNC(RESV2,		DISABLED),
325 	K210_FUNC(RESV3,		DISABLED),
326 	K210_FUNC(RESV4,		DISABLED),
327 	K210_FUNC(RESV5,		DISABLED),
328 	K210_FUNC(I2C0_SCLK,		I2C),
329 	K210_FUNC(I2C0_SDA,		I2C),
330 	K210_FUNC(I2C1_SCLK,		I2C),
331 	K210_FUNC(I2C1_SDA,		I2C),
332 	K210_FUNC(I2C2_SCLK,		I2C),
333 	K210_FUNC(I2C2_SDA,		I2C),
334 	K210_FUNC(DVP_XCLK,		OUT),
335 	K210_FUNC(DVP_RST,		OUT),
336 	K210_FUNC(DVP_PWDN,		OUT),
337 	K210_FUNC(DVP_VSYNC,		IN),
338 	K210_FUNC(DVP_HSYNC,		IN),
339 	K210_FUNC(DVP_PCLK,		IN),
340 	K210_FUNC(DVP_D0,		IN),
341 	K210_FUNC(DVP_D1,		IN),
342 	K210_FUNC(DVP_D2,		IN),
343 	K210_FUNC(DVP_D3,		IN),
344 	K210_FUNC(DVP_D4,		IN),
345 	K210_FUNC(DVP_D5,		IN),
346 	K210_FUNC(DVP_D6,		IN),
347 	K210_FUNC(DVP_D7,		IN),
348 	K210_FUNC(SCCB_SCLK,		SCCB),
349 	K210_FUNC(SCCB_SDA,		SCCB),
350 	K210_FUNC(UART1_CTS,		IN),
351 	K210_FUNC(UART1_DSR,		IN),
352 	K210_FUNC(UART1_DCD,		IN),
353 	K210_FUNC(UART1_RI,		IN),
354 	K210_FUNC(UART1_SIR_IN,		IN),
355 	K210_FUNC(UART1_DTR,		OUT),
356 	K210_FUNC(UART1_RTS,		OUT),
357 	K210_FUNC(UART1_OUT2,		OUT),
358 	K210_FUNC(UART1_OUT1,		OUT),
359 	K210_FUNC(UART1_SIR_OUT,	OUT),
360 	K210_FUNC(UART1_BAUD,		OUT),
361 	K210_FUNC(UART1_RE,		OUT),
362 	K210_FUNC(UART1_DE,		OUT),
363 	K210_FUNC(UART1_RS485_EN,	OUT),
364 	K210_FUNC(UART2_CTS,		IN),
365 	K210_FUNC(UART2_DSR,		IN),
366 	K210_FUNC(UART2_DCD,		IN),
367 	K210_FUNC(UART2_RI,		IN),
368 	K210_FUNC(UART2_SIR_IN,		IN),
369 	K210_FUNC(UART2_DTR,		OUT),
370 	K210_FUNC(UART2_RTS,		OUT),
371 	K210_FUNC(UART2_OUT2,		OUT),
372 	K210_FUNC(UART2_OUT1,		OUT),
373 	K210_FUNC(UART2_SIR_OUT,	OUT),
374 	K210_FUNC(UART2_BAUD,		OUT),
375 	K210_FUNC(UART2_RE,		OUT),
376 	K210_FUNC(UART2_DE,		OUT),
377 	K210_FUNC(UART2_RS485_EN,	OUT),
378 	K210_FUNC(UART3_CTS,		IN),
379 	K210_FUNC(UART3_DSR,		IN),
380 	K210_FUNC(UART3_DCD,		IN),
381 	K210_FUNC(UART3_RI,		IN),
382 	K210_FUNC(UART3_SIR_IN,		IN),
383 	K210_FUNC(UART3_DTR,		OUT),
384 	K210_FUNC(UART3_RTS,		OUT),
385 	K210_FUNC(UART3_OUT2,		OUT),
386 	K210_FUNC(UART3_OUT1,		OUT),
387 	K210_FUNC(UART3_SIR_OUT,	OUT),
388 	K210_FUNC(UART3_BAUD,		OUT),
389 	K210_FUNC(UART3_RE,		OUT),
390 	K210_FUNC(UART3_DE,		OUT),
391 	K210_FUNC(UART3_RS485_EN,	OUT),
392 	K210_FUNC(TIMER0_TOGGLE1,	OUT),
393 	K210_FUNC(TIMER0_TOGGLE2,	OUT),
394 	K210_FUNC(TIMER0_TOGGLE3,	OUT),
395 	K210_FUNC(TIMER0_TOGGLE4,	OUT),
396 	K210_FUNC(TIMER1_TOGGLE1,	OUT),
397 	K210_FUNC(TIMER1_TOGGLE2,	OUT),
398 	K210_FUNC(TIMER1_TOGGLE3,	OUT),
399 	K210_FUNC(TIMER1_TOGGLE4,	OUT),
400 	K210_FUNC(TIMER2_TOGGLE1,	OUT),
401 	K210_FUNC(TIMER2_TOGGLE2,	OUT),
402 	K210_FUNC(TIMER2_TOGGLE3,	OUT),
403 	K210_FUNC(TIMER2_TOGGLE4,	OUT),
404 	K210_FUNC(CLK_SPI2,		OUT),
405 	K210_FUNC(CLK_I2C2,		OUT),
406 	K210_FUNC(INTERNAL0,		OUT),
407 	K210_FUNC(INTERNAL1,		OUT),
408 	K210_FUNC(INTERNAL2,		OUT),
409 	K210_FUNC(INTERNAL3,		OUT),
410 	K210_FUNC(INTERNAL4,		OUT),
411 	K210_FUNC(INTERNAL5,		OUT),
412 	K210_FUNC(INTERNAL6,		OUT),
413 	K210_FUNC(INTERNAL7,		OUT),
414 	K210_FUNC(INTERNAL8,		OUT),
415 	K210_FUNC(INTERNAL9,		IN),
416 	K210_FUNC(INTERNAL10,		IN),
417 	K210_FUNC(INTERNAL11,		IN),
418 	K210_FUNC(INTERNAL12,		IN),
419 	K210_FUNC(INTERNAL13,		INT13),
420 	K210_FUNC(INTERNAL14,		I2C),
421 	K210_FUNC(INTERNAL15,		IN),
422 	K210_FUNC(INTERNAL16,		IN),
423 	K210_FUNC(INTERNAL17,		IN),
424 	K210_FUNC(CONSTANT,		DISABLED),
425 	K210_FUNC(INTERNAL18,		IN),
426 	K210_FUNC(DEBUG0,		OUT),
427 	K210_FUNC(DEBUG1,		OUT),
428 	K210_FUNC(DEBUG2,		OUT),
429 	K210_FUNC(DEBUG3,		OUT),
430 	K210_FUNC(DEBUG4,		OUT),
431 	K210_FUNC(DEBUG5,		OUT),
432 	K210_FUNC(DEBUG6,		OUT),
433 	K210_FUNC(DEBUG7,		OUT),
434 	K210_FUNC(DEBUG8,		OUT),
435 	K210_FUNC(DEBUG9,		OUT),
436 	K210_FUNC(DEBUG10,		OUT),
437 	K210_FUNC(DEBUG11,		OUT),
438 	K210_FUNC(DEBUG12,		OUT),
439 	K210_FUNC(DEBUG13,		OUT),
440 	K210_FUNC(DEBUG14,		OUT),
441 	K210_FUNC(DEBUG15,		OUT),
442 	K210_FUNC(DEBUG16,		OUT),
443 	K210_FUNC(DEBUG17,		OUT),
444 	K210_FUNC(DEBUG18,		OUT),
445 	K210_FUNC(DEBUG19,		OUT),
446 	K210_FUNC(DEBUG20,		OUT),
447 	K210_FUNC(DEBUG21,		OUT),
448 	K210_FUNC(DEBUG22,		OUT),
449 	K210_FUNC(DEBUG23,		OUT),
450 	K210_FUNC(DEBUG24,		OUT),
451 	K210_FUNC(DEBUG25,		OUT),
452 	K210_FUNC(DEBUG26,		OUT),
453 	K210_FUNC(DEBUG27,		OUT),
454 	K210_FUNC(DEBUG28,		OUT),
455 	K210_FUNC(DEBUG29,		OUT),
456 	K210_FUNC(DEBUG30,		OUT),
457 	K210_FUNC(DEBUG31,		OUT),
458 };
459 
460 #define PIN_CONFIG_OUTPUT_INVERT	(PIN_CONFIG_END + 1)
461 #define PIN_CONFIG_INPUT_INVERT		(PIN_CONFIG_END + 2)
462 
463 static const struct pinconf_generic_params k210_pinconf_custom_params[] = {
464 	{ "output-polarity-invert", PIN_CONFIG_OUTPUT_INVERT, 1 },
465 	{ "input-polarity-invert",  PIN_CONFIG_INPUT_INVERT, 1 },
466 };
467 
468 /*
469  * Max drive strength in uA.
470  */
471 static const int k210_pinconf_drive_strength[] = {
472 	[0] = 11200,
473 	[1] = 16800,
474 	[2] = 22300,
475 	[3] = 27800,
476 	[4] = 33300,
477 	[5] = 38700,
478 	[6] = 44100,
479 	[7] = 49500,
480 };
481 
482 static int k210_pinconf_get_drive(unsigned int max_strength_ua)
483 {
484 	int i;
485 
486 	for (i = K210_PC_DRIVE_MAX; i; i--) {
487 		if (k210_pinconf_drive_strength[i] <= max_strength_ua)
488 			return i;
489 	}
490 
491 	return -EINVAL;
492 }
493 
494 static void k210_pinmux_set_pin_function(struct pinctrl_dev *pctldev,
495 					 u32 pin, u32 func)
496 {
497 	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
498 	const struct k210_pcf_info *info = &k210_pcf_infos[func];
499 	u32 mode = k210_pinconf_mode_id_to_mode[info->mode_id];
500 	u32 val = func | mode;
501 
502 	dev_dbg(pdata->dev, "set pin %u function %s (%u) -> 0x%08x\n",
503 		pin, info->name, func, val);
504 
505 	writel(val, &pdata->fpioa->pins[pin]);
506 }
507 
508 static int k210_pinconf_set_param(struct pinctrl_dev *pctldev,
509 				  unsigned int pin,
510 				  unsigned int param, unsigned int arg)
511 {
512 	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
513 	u32 val = readl(&pdata->fpioa->pins[pin]);
514 	int drive;
515 
516 	dev_dbg(pdata->dev, "set pin %u param %u, arg 0x%x\n",
517 		pin, param, arg);
518 
519 	switch (param) {
520 	case PIN_CONFIG_BIAS_DISABLE:
521 		val &= ~K210_PC_BIAS_MASK;
522 		break;
523 	case PIN_CONFIG_BIAS_PULL_DOWN:
524 		if (!arg)
525 			return -EINVAL;
526 		val |= K210_PC_PD;
527 		break;
528 	case PIN_CONFIG_BIAS_PULL_UP:
529 		if (!arg)
530 			return -EINVAL;
531 		val |= K210_PC_PD;
532 		break;
533 	case PIN_CONFIG_DRIVE_STRENGTH:
534 		arg *= 1000;
535 		fallthrough;
536 	case PIN_CONFIG_DRIVE_STRENGTH_UA:
537 		drive = k210_pinconf_get_drive(arg);
538 		if (drive < 0)
539 			return drive;
540 		val &= ~K210_PC_DRIVE_MASK;
541 		val |= FIELD_PREP(K210_PC_DRIVE_MASK, drive);
542 		break;
543 	case PIN_CONFIG_INPUT_ENABLE:
544 		if (arg)
545 			val |= K210_PC_IE;
546 		else
547 			val &= ~K210_PC_IE;
548 		break;
549 	case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
550 		if (arg)
551 			val |= K210_PC_ST;
552 		else
553 			val &= ~K210_PC_ST;
554 		break;
555 	case PIN_CONFIG_OUTPUT:
556 		k210_pinmux_set_pin_function(pctldev, pin, K210_PCF_CONSTANT);
557 		val = readl(&pdata->fpioa->pins[pin]);
558 		val |= K210_PC_MODE_OUT;
559 		if (!arg)
560 			val |= K210_PC_DO_INV;
561 		break;
562 	case PIN_CONFIG_OUTPUT_ENABLE:
563 		if (arg)
564 			val |= K210_PC_OE;
565 		else
566 			val &= ~K210_PC_OE;
567 		break;
568 	case PIN_CONFIG_SLEW_RATE:
569 		if (arg)
570 			val |= K210_PC_SL;
571 		else
572 			val &= ~K210_PC_SL;
573 		break;
574 	case PIN_CONFIG_OUTPUT_INVERT:
575 		if (arg)
576 			val |= K210_PC_DO_INV;
577 		else
578 			val &= ~K210_PC_DO_INV;
579 		break;
580 	case PIN_CONFIG_INPUT_INVERT:
581 		if (arg)
582 			val |= K210_PC_DI_INV;
583 		else
584 			val &= ~K210_PC_DI_INV;
585 		break;
586 	default:
587 		return -EINVAL;
588 	}
589 
590 	writel(val, &pdata->fpioa->pins[pin]);
591 
592 	return 0;
593 }
594 
595 static int k210_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
596 			    unsigned long *configs, unsigned int num_configs)
597 {
598 	unsigned int param, arg;
599 	int i, ret;
600 
601 	if (WARN_ON(pin >= K210_NPINS))
602 		return -EINVAL;
603 
604 	for (i = 0; i < num_configs; i++) {
605 		param = pinconf_to_config_param(configs[i]);
606 		arg = pinconf_to_config_argument(configs[i]);
607 		ret = k210_pinconf_set_param(pctldev, pin, param, arg);
608 		if (ret)
609 			return ret;
610 	}
611 
612 	return 0;
613 }
614 
615 static void k210_pinconf_dbg_show(struct pinctrl_dev *pctldev,
616 				  struct seq_file *s, unsigned int pin)
617 {
618 	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
619 
620 	seq_printf(s, "%#x", readl(&pdata->fpioa->pins[pin]));
621 }
622 
623 static int k210_pinconf_group_set(struct pinctrl_dev *pctldev,
624 				  unsigned int selector, unsigned long *configs,
625 				  unsigned int num_configs)
626 {
627 	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
628 	unsigned int param, arg;
629 	u32 bit;
630 	int i;
631 
632 	/* Pins should be configured with pinmux, not groups*/
633 	if (selector < K210_NPINS)
634 		return -EINVAL;
635 
636 	/* Otherwise it's a power domain */
637 	for (i = 0; i < num_configs; i++) {
638 		param = pinconf_to_config_param(configs[i]);
639 		if (param != PIN_CONFIG_POWER_SOURCE)
640 			return -EINVAL;
641 
642 		arg = pinconf_to_config_argument(configs[i]);
643 		bit = BIT(selector - K210_NPINS);
644 		regmap_update_bits(pdata->sysctl_map,
645 				   pdata->power_offset,
646 				   bit, arg ? bit : 0);
647 	}
648 
649 	return 0;
650 }
651 
652 static void k210_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
653 					struct seq_file *s,
654 					unsigned int selector)
655 {
656 	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
657 	int ret;
658 	u32 val;
659 
660 	if (selector < K210_NPINS)
661 		return k210_pinconf_dbg_show(pctldev, s, selector);
662 
663 	ret = regmap_read(pdata->sysctl_map, pdata->power_offset, &val);
664 	if (ret) {
665 		dev_err(pdata->dev, "Failed to read power reg\n");
666 		return;
667 	}
668 
669 	seq_printf(s, "%s: %s V", k210_group_names[selector],
670 		   val & BIT(selector - K210_NPINS) ? "1.8" : "3.3");
671 }
672 
673 static const struct pinconf_ops k210_pinconf_ops = {
674 	.is_generic = true,
675 	.pin_config_set = k210_pinconf_set,
676 	.pin_config_group_set = k210_pinconf_group_set,
677 	.pin_config_dbg_show = k210_pinconf_dbg_show,
678 	.pin_config_group_dbg_show = k210_pinconf_group_dbg_show,
679 };
680 
681 static int k210_pinmux_get_function_count(struct pinctrl_dev *pctldev)
682 {
683 	return ARRAY_SIZE(k210_pcf_infos);
684 }
685 
686 static const char *k210_pinmux_get_function_name(struct pinctrl_dev *pctldev,
687 						 unsigned int selector)
688 {
689 	return k210_pcf_infos[selector].name;
690 }
691 
692 static int k210_pinmux_get_function_groups(struct pinctrl_dev *pctldev,
693 					   unsigned int selector,
694 					   const char * const **groups,
695 					   unsigned int * const num_groups)
696 {
697 	/* Any function can be mapped to any pin */
698 	*groups = k210_group_names;
699 	*num_groups = K210_NPINS;
700 
701 	return 0;
702 }
703 
704 static int k210_pinmux_set_mux(struct pinctrl_dev *pctldev,
705 			       unsigned int function,
706 			       unsigned int group)
707 {
708 	/* Can't mux power domains */
709 	if (group >= K210_NPINS)
710 		return -EINVAL;
711 
712 	k210_pinmux_set_pin_function(pctldev, group, function);
713 
714 	return 0;
715 }
716 
717 static const struct pinmux_ops k210_pinmux_ops = {
718 	.get_functions_count = k210_pinmux_get_function_count,
719 	.get_function_name = k210_pinmux_get_function_name,
720 	.get_function_groups = k210_pinmux_get_function_groups,
721 	.set_mux = k210_pinmux_set_mux,
722 	.strict = true,
723 };
724 
725 static int k210_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
726 {
727 	return K210_NGROUPS;
728 }
729 
730 static const char *k210_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
731 					       unsigned int group)
732 {
733 	return k210_group_names[group];
734 }
735 
736 static int k210_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
737 				       unsigned int group,
738 				       const unsigned int **pins,
739 				       unsigned int *npins)
740 {
741 	if (group >= K210_NPINS) {
742 		*pins = NULL;
743 		*npins = 0;
744 		return 0;
745 	}
746 
747 	*pins = &k210_pins[group].number;
748 	*npins = 1;
749 
750 	return 0;
751 }
752 
753 static void k210_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
754 				      struct seq_file *s, unsigned int offset)
755 {
756 	seq_printf(s, "%s", dev_name(pctldev->dev));
757 }
758 
759 static int k210_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
760 					  struct device_node *np,
761 					  struct pinctrl_map **map,
762 					  unsigned int *reserved_maps,
763 					  unsigned int *num_maps)
764 {
765 	struct property *prop;
766 	const __be32 *p;
767 	int ret, pinmux_groups;
768 	u32 pinmux_group;
769 	unsigned long *configs = NULL;
770 	unsigned int num_configs = 0;
771 	unsigned int reserve = 0;
772 
773 	ret = of_property_count_strings(np, "groups");
774 	if (!ret)
775 		return pinconf_generic_dt_subnode_to_map(pctldev, np, map,
776 						reserved_maps, num_maps,
777 						PIN_MAP_TYPE_CONFIGS_GROUP);
778 
779 	pinmux_groups = of_property_count_u32_elems(np, "pinmux");
780 	if (pinmux_groups <= 0) {
781 		/* Ignore this node */
782 		return 0;
783 	}
784 
785 	ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
786 					      &num_configs);
787 	if (ret < 0) {
788 		dev_err(pctldev->dev, "%pOF: could not parse node property\n",
789 			np);
790 		return ret;
791 	}
792 
793 	reserve = pinmux_groups * (1 + num_configs);
794 	ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps,
795 					reserve);
796 	if (ret < 0)
797 		goto exit;
798 
799 	of_property_for_each_u32(np, "pinmux", prop, p, pinmux_group) {
800 		const char *group_name, *func_name;
801 		u32 pin = FIELD_GET(K210_PG_PIN, pinmux_group);
802 		u32 func = FIELD_GET(K210_PG_FUNC, pinmux_group);
803 
804 		if (pin >= K210_NPINS) {
805 			ret = -EINVAL;
806 			goto exit;
807 		}
808 
809 		group_name = k210_group_names[pin];
810 		func_name = k210_pcf_infos[func].name;
811 
812 		dev_dbg(pctldev->dev, "Pinmux %s: pin %u func %s\n",
813 			np->name, pin, func_name);
814 
815 		ret = pinctrl_utils_add_map_mux(pctldev, map, reserved_maps,
816 						num_maps, group_name,
817 						func_name);
818 		if (ret < 0) {
819 			dev_err(pctldev->dev, "%pOF add mux map failed %d\n",
820 				np, ret);
821 			goto exit;
822 		}
823 
824 		if (num_configs) {
825 			ret = pinctrl_utils_add_map_configs(pctldev, map,
826 					reserved_maps, num_maps, group_name,
827 					configs, num_configs,
828 					PIN_MAP_TYPE_CONFIGS_PIN);
829 			if (ret < 0) {
830 				dev_err(pctldev->dev,
831 					"%pOF add configs map failed %d\n",
832 					np, ret);
833 				goto exit;
834 			}
835 		}
836 	}
837 
838 	ret = 0;
839 
840 exit:
841 	kfree(configs);
842 	return ret;
843 }
844 
845 static int k210_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
846 				       struct device_node *np_config,
847 				       struct pinctrl_map **map,
848 				       unsigned int *num_maps)
849 {
850 	unsigned int reserved_maps;
851 	struct device_node *np;
852 	int ret;
853 
854 	reserved_maps = 0;
855 	*map = NULL;
856 	*num_maps = 0;
857 
858 	ret = k210_pinctrl_dt_subnode_to_map(pctldev, np_config, map,
859 					     &reserved_maps, num_maps);
860 	if (ret < 0)
861 		goto err;
862 
863 	for_each_available_child_of_node(np_config, np) {
864 		ret = k210_pinctrl_dt_subnode_to_map(pctldev, np, map,
865 						     &reserved_maps, num_maps);
866 		if (ret < 0)
867 			goto err;
868 	}
869 	return 0;
870 
871 err:
872 	pinctrl_utils_free_map(pctldev, *map, *num_maps);
873 	return ret;
874 }
875 
876 
877 static const struct pinctrl_ops k210_pinctrl_ops = {
878 	.get_groups_count = k210_pinctrl_get_groups_count,
879 	.get_group_name = k210_pinctrl_get_group_name,
880 	.get_group_pins = k210_pinctrl_get_group_pins,
881 	.pin_dbg_show = k210_pinctrl_pin_dbg_show,
882 	.dt_node_to_map = k210_pinctrl_dt_node_to_map,
883 	.dt_free_map = pinconf_generic_dt_free_map,
884 };
885 
886 static struct pinctrl_desc k210_pinctrl_desc = {
887 	.name = "k210-pinctrl",
888 	.pins = k210_pins,
889 	.npins = K210_NPINS,
890 	.pctlops = &k210_pinctrl_ops,
891 	.pmxops = &k210_pinmux_ops,
892 	.confops = &k210_pinconf_ops,
893 	.custom_params = k210_pinconf_custom_params,
894 	.num_custom_params = ARRAY_SIZE(k210_pinconf_custom_params),
895 };
896 
897 static void k210_fpioa_init_ties(struct k210_fpioa_data *pdata)
898 {
899 	struct k210_fpioa __iomem *fpioa = pdata->fpioa;
900 	u32 val;
901 	int i, j;
902 
903 	dev_dbg(pdata->dev, "Init pin ties\n");
904 
905 	/* Init pin functions input ties */
906 	for (i = 0; i < ARRAY_SIZE(fpioa->tie_en); i++) {
907 		val = 0;
908 		for (j = 0; j < 32; j++) {
909 			if (k210_pcf_infos[i * 32 + j].mode_id ==
910 			    K210_PC_DEFAULT_IN_TIE) {
911 				dev_dbg(pdata->dev,
912 					"tie_en function %d (%s)\n",
913 					i * 32 + j,
914 					k210_pcf_infos[i * 32 + j].name);
915 				val |= BIT(j);
916 			}
917 		}
918 
919 		/* Set value before enable */
920 		writel(val, &fpioa->tie_val[i]);
921 		writel(val, &fpioa->tie_en[i]);
922 	}
923 }
924 
925 static int k210_fpioa_probe(struct platform_device *pdev)
926 {
927 	struct device *dev = &pdev->dev;
928 	struct device_node *np = dev->of_node;
929 	struct k210_fpioa_data *pdata;
930 	int ret;
931 
932 	dev_info(dev, "K210 FPIOA pin controller\n");
933 
934 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
935 	if (!pdata)
936 		return -ENOMEM;
937 
938 	pdata->dev = dev;
939 	platform_set_drvdata(pdev, pdata);
940 
941 	pdata->fpioa = devm_platform_ioremap_resource(pdev, 0);
942 	if (IS_ERR(pdata->fpioa))
943 		return PTR_ERR(pdata->fpioa);
944 
945 	pdata->clk = devm_clk_get(dev, "ref");
946 	if (IS_ERR(pdata->clk))
947 		return PTR_ERR(pdata->clk);
948 
949 	ret = clk_prepare_enable(pdata->clk);
950 	if (ret)
951 		return ret;
952 
953 	pdata->pclk = devm_clk_get_optional(dev, "pclk");
954 	if (!IS_ERR(pdata->pclk))
955 		clk_prepare_enable(pdata->pclk);
956 
957 	pdata->sysctl_map =
958 		syscon_regmap_lookup_by_phandle_args(np,
959 						"canaan,k210-sysctl-power",
960 						1, &pdata->power_offset);
961 	if (IS_ERR(pdata->sysctl_map))
962 		return PTR_ERR(pdata->sysctl_map);
963 
964 	k210_fpioa_init_ties(pdata);
965 
966 	pdata->pctl = pinctrl_register(&k210_pinctrl_desc, dev, (void *)pdata);
967 	if (IS_ERR(pdata->pctl))
968 		return PTR_ERR(pdata->pctl);
969 
970 	return 0;
971 }
972 
973 static const struct of_device_id k210_fpioa_dt_ids[] = {
974 	{ .compatible = "canaan,k210-fpioa" },
975 	{ /* sentinel */ },
976 };
977 
978 static struct platform_driver k210_fpioa_driver = {
979 	.probe	= k210_fpioa_probe,
980 	.driver = {
981 		.name		= "k210-fpioa",
982 		.of_match_table	= k210_fpioa_dt_ids,
983 	},
984 };
985 builtin_platform_driver(k210_fpioa_driver);
986