1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Renesas RZ/G2L Pin Control and GPIO driver core
4  *
5  * Copyright (C) 2021 Renesas Electronics Corporation.
6  */
7 
8 #include <linux/bitfield.h>
9 #include <linux/bitops.h>
10 #include <linux/clk.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of.h>
17 #include <linux/of_irq.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/seq_file.h>
21 #include <linux/spinlock.h>
22 
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pinctrl/pinconf-generic.h>
25 #include <linux/pinctrl/pinconf.h>
26 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/pinctrl/pinmux.h>
28 
29 #include <dt-bindings/pinctrl/rzg2l-pinctrl.h>
30 
31 #include "../core.h"
32 #include "../pinconf.h"
33 #include "../pinmux.h"
34 
35 #define DRV_NAME	"pinctrl-rzg2l"
36 
37 /*
38  * Use 16 lower bits [15:0] for pin identifier
39  * Use 16 higher bits [31:16] for pin mux function
40  */
41 #define MUX_PIN_ID_MASK		GENMASK(15, 0)
42 #define MUX_FUNC_MASK		GENMASK(31, 16)
43 
44 /* PIN capabilities */
45 #define PIN_CFG_IOLH_A			BIT(0)
46 #define PIN_CFG_IOLH_B			BIT(1)
47 #define PIN_CFG_SR			BIT(2)
48 #define PIN_CFG_IEN			BIT(3)
49 #define PIN_CFG_PUPD			BIT(4)
50 #define PIN_CFG_IO_VMC_SD0		BIT(5)
51 #define PIN_CFG_IO_VMC_SD1		BIT(6)
52 #define PIN_CFG_IO_VMC_QSPI		BIT(7)
53 #define PIN_CFG_IO_VMC_ETH0		BIT(8)
54 #define PIN_CFG_IO_VMC_ETH1		BIT(9)
55 #define PIN_CFG_NF			BIT(10)	/* Digital noise filter */
56 #define PIN_CFG_IOLH_C			BIT(11)
57 #define PIN_CFG_SOFT_PS			BIT(12)
58 #define PIN_CFG_OEN			BIT(13)
59 #define PIN_CFG_NOGPIO_INT		BIT(14)
60 #define PIN_CFG_NOD			BIT(15)	/* N-ch Open Drain */
61 #define PIN_CFG_SMT			BIT(16)	/* Schmitt-trigger input control */
62 #define PIN_CFG_ELC			BIT(17)
63 #define PIN_CFG_IOLH_RZV2H		BIT(18)
64 
65 #define RZG2L_SINGLE_PIN		BIT_ULL(63)	/* Dedicated pin */
66 #define RZG2L_VARIABLE_CFG		BIT_ULL(62)	/* Variable cfg for port pins */
67 
68 #define RZG2L_MPXED_COMMON_PIN_FUNCS(group) \
69 					(PIN_CFG_IOLH_##group | \
70 					 PIN_CFG_PUPD | \
71 					 PIN_CFG_NF)
72 
73 #define RZG2L_MPXED_PIN_FUNCS		(RZG2L_MPXED_COMMON_PIN_FUNCS(A) | \
74 					 PIN_CFG_SR)
75 
76 #define RZG3S_MPXED_PIN_FUNCS(group)	(RZG2L_MPXED_COMMON_PIN_FUNCS(group) | \
77 					 PIN_CFG_SOFT_PS)
78 
79 #define RZV2H_MPXED_PIN_FUNCS		(RZG2L_MPXED_COMMON_PIN_FUNCS(RZV2H) | \
80 					 PIN_CFG_NOD | \
81 					 PIN_CFG_SR | \
82 					 PIN_CFG_SMT)
83 
84 #define RZG2L_MPXED_ETH_PIN_FUNCS(x)	((x) | PIN_CFG_NF)
85 
86 #define PIN_CFG_PIN_MAP_MASK		GENMASK_ULL(61, 54)
87 #define PIN_CFG_PIN_REG_MASK		GENMASK_ULL(53, 46)
88 #define PIN_CFG_MASK			GENMASK_ULL(31, 0)
89 
90 /*
91  * m indicates the bitmap of supported pins, a is the register index
92  * and f is pin configuration capabilities supported.
93  */
94 #define RZG2L_GPIO_PORT_SPARSE_PACK(m, a, f)	(FIELD_PREP_CONST(PIN_CFG_PIN_MAP_MASK, (m)) | \
95 						 FIELD_PREP_CONST(PIN_CFG_PIN_REG_MASK, (a)) | \
96 						 FIELD_PREP_CONST(PIN_CFG_MASK, (f)))
97 #define RZG2L_GPIO_PORT_SPARSE_PACK_VARIABLE(m, a)	\
98 						(RZG2L_VARIABLE_CFG | \
99 						 RZG2L_GPIO_PORT_SPARSE_PACK(m, a, 0))
100 
101 /*
102  * n indicates number of pins in the port, a is the register index
103  * and f is pin configuration capabilities supported.
104  */
105 #define RZG2L_GPIO_PORT_PACK(n, a, f)	RZG2L_GPIO_PORT_SPARSE_PACK((1ULL << (n)) - 1, (a), (f))
106 #define RZG2L_GPIO_PORT_PACK_VARIABLE(n, a)	(RZG2L_VARIABLE_CFG | \
107 						 RZG2L_GPIO_PORT_PACK(n, a, 0))
108 
109 #define RZG2L_SINGLE_PIN_INDEX_MASK	GENMASK_ULL(62, 56)
110 #define RZG2L_SINGLE_PIN_BITS_MASK	GENMASK_ULL(55, 53)
111 /*
112  * p is the register index while referencing to SR/IEN/IOLH/FILxx
113  * registers, b is the register bits (b * 8) and f is the pin
114  * configuration capabilities supported.
115  */
116 #define RZG2L_SINGLE_PIN_PACK(p, b, f)	(RZG2L_SINGLE_PIN | \
117 					 FIELD_PREP_CONST(RZG2L_SINGLE_PIN_INDEX_MASK, (p)) | \
118 					 FIELD_PREP_CONST(RZG2L_SINGLE_PIN_BITS_MASK, (b)) | \
119 					 FIELD_PREP_CONST(PIN_CFG_MASK, (f)))
120 
121 #define RZG2L_PIN_CFG_TO_PORT_OFFSET(cfg)	((cfg) & RZG2L_SINGLE_PIN ? \
122 						 FIELD_GET(RZG2L_SINGLE_PIN_INDEX_MASK, (cfg)) : \
123 						 FIELD_GET(PIN_CFG_PIN_REG_MASK, (cfg)))
124 
125 #define VARIABLE_PIN_CFG_PIN_MASK		GENMASK_ULL(54, 52)
126 #define VARIABLE_PIN_CFG_PORT_MASK		GENMASK_ULL(51, 47)
127 #define RZG2L_VARIABLE_PIN_CFG_PACK(port, pin, cfg) \
128 	(FIELD_PREP_CONST(VARIABLE_PIN_CFG_PIN_MASK, (pin)) | \
129 	 FIELD_PREP_CONST(VARIABLE_PIN_CFG_PORT_MASK, (port)) | \
130 	 FIELD_PREP_CONST(PIN_CFG_MASK, (cfg)))
131 
132 #define P(off)			(0x0000 + (off))
133 #define PM(off)			(0x0100 + (off) * 2)
134 #define PMC(off)		(0x0200 + (off))
135 #define PFC(off)		(0x0400 + (off) * 4)
136 #define PIN(off)		(0x0800 + (off))
137 #define IOLH(off)		(0x1000 + (off) * 8)
138 #define SR(off)			(0x1400 + (off) * 8)
139 #define IEN(off)		(0x1800 + (off) * 8)
140 #define PUPD(off)		(0x1C00 + (off) * 8)
141 #define ISEL(off)		(0x2C00 + (off) * 8)
142 #define SD_CH(off, ch)		((off) + (ch) * 4)
143 #define ETH_POC(off, ch)	((off) + (ch) * 4)
144 #define QSPI			(0x3008)
145 #define ETH_MODE		(0x3018)
146 #define PFC_OEN			(0x3C40) /* known on RZ/V2H(P) only */
147 
148 #define PVDD_2500		2	/* I/O domain voltage 2.5V */
149 #define PVDD_1800		1	/* I/O domain voltage <= 1.8V */
150 #define PVDD_3300		0	/* I/O domain voltage >= 3.3V */
151 
152 #define PWPR_B0WI		BIT(7)	/* Bit Write Disable */
153 #define PWPR_PFCWE		BIT(6)	/* PFC Register Write Enable */
154 #define PWPR_REGWE_A		BIT(6)	/* PFC and PMC Register Write Enable on RZ/V2H(P) */
155 #define PWPR_REGWE_B		BIT(5)	/* OEN Register Write Enable, known only in RZ/V2H(P) */
156 
157 #define PM_MASK			0x03
158 #define PFC_MASK		0x0f
159 #define IEN_MASK		0x01
160 #define IOLH_MASK		0x03
161 #define SR_MASK			0x01
162 #define PUPD_MASK		0x03
163 
164 #define PM_INPUT		0x1
165 #define PM_OUTPUT		0x2
166 
167 #define RZG2L_PIN_ID_TO_PORT(id)	((id) / RZG2L_PINS_PER_PORT)
168 #define RZG2L_PIN_ID_TO_PIN(id)		((id) % RZG2L_PINS_PER_PORT)
169 
170 #define RZG2L_TINT_MAX_INTERRUPT	32
171 #define RZG2L_TINT_IRQ_START_INDEX	9
172 #define RZG2L_PACK_HWIRQ(t, i)		(((t) << 16) | (i))
173 
174 /* Custom pinconf parameters */
175 #define RENESAS_RZV2H_PIN_CONFIG_OUTPUT_IMPEDANCE	(PIN_CONFIG_END + 1)
176 
177 static const struct pinconf_generic_params renesas_rzv2h_custom_bindings[] = {
178 	{ "renesas,output-impedance", RENESAS_RZV2H_PIN_CONFIG_OUTPUT_IMPEDANCE, 1 },
179 };
180 
181 #ifdef CONFIG_DEBUG_FS
182 static const struct pin_config_item renesas_rzv2h_conf_items[] = {
183 	PCONFDUMP(RENESAS_RZV2H_PIN_CONFIG_OUTPUT_IMPEDANCE, "output-impedance", "x", true),
184 };
185 #endif
186 
187 /* Read/write 8 bits register */
188 #define RZG2L_PCTRL_REG_ACCESS8(_read, _addr, _val)	\
189 	do {						\
190 		if (_read)				\
191 			_val = readb(_addr);		\
192 		else					\
193 			writeb(_val, _addr);		\
194 	} while (0)
195 
196 /* Read/write 16 bits register */
197 #define RZG2L_PCTRL_REG_ACCESS16(_read, _addr, _val)	\
198 	do {						\
199 		if (_read)				\
200 			_val = readw(_addr);		\
201 		else					\
202 			writew(_val, _addr);		\
203 	} while (0)
204 
205 /* Read/write 32 bits register */
206 #define RZG2L_PCTRL_REG_ACCESS32(_read, _addr, _val)	\
207 	do {						\
208 		if (_read)				\
209 			_val = readl(_addr);		\
210 		else					\
211 			writel(_val, _addr);		\
212 	} while (0)
213 
214 /**
215  * struct rzg2l_register_offsets - specific register offsets
216  * @pwpr: PWPR register offset
217  * @sd_ch: SD_CH register offset
218  * @eth_poc: ETH_POC register offset
219  */
220 struct rzg2l_register_offsets {
221 	u16 pwpr;
222 	u16 sd_ch;
223 	u16 eth_poc;
224 };
225 
226 /**
227  * enum rzg2l_iolh_index - starting indices in IOLH specific arrays
228  * @RZG2L_IOLH_IDX_1V8: starting index for 1V8 power source
229  * @RZG2L_IOLH_IDX_2V5: starting index for 2V5 power source
230  * @RZG2L_IOLH_IDX_3V3: starting index for 3V3 power source
231  * @RZG2L_IOLH_IDX_MAX: maximum index
232  */
233 enum rzg2l_iolh_index {
234 	RZG2L_IOLH_IDX_1V8 = 0,
235 	RZG2L_IOLH_IDX_2V5 = 4,
236 	RZG2L_IOLH_IDX_3V3 = 8,
237 	RZG2L_IOLH_IDX_MAX = 12,
238 };
239 
240 /* Maximum number of driver strength entries per power source. */
241 #define RZG2L_IOLH_MAX_DS_ENTRIES	(4)
242 
243 /**
244  * struct rzg2l_hwcfg - hardware configuration data structure
245  * @regs: hardware specific register offsets
246  * @iolh_groupa_ua: IOLH group A uA specific values
247  * @iolh_groupb_ua: IOLH group B uA specific values
248  * @iolh_groupc_ua: IOLH group C uA specific values
249  * @iolh_groupb_oi: IOLH group B output impedance specific values
250  * @drive_strength_ua: drive strength in uA is supported (otherwise mA is supported)
251  * @func_base: base number for port function (see register PFC)
252  * @oen_max_pin: the maximum pin number supporting output enable
253  * @oen_max_port: the maximum port number supporting output enable
254  */
255 struct rzg2l_hwcfg {
256 	const struct rzg2l_register_offsets regs;
257 	u16 iolh_groupa_ua[RZG2L_IOLH_IDX_MAX];
258 	u16 iolh_groupb_ua[RZG2L_IOLH_IDX_MAX];
259 	u16 iolh_groupc_ua[RZG2L_IOLH_IDX_MAX];
260 	u16 iolh_groupb_oi[4];
261 	bool drive_strength_ua;
262 	u8 func_base;
263 	u8 oen_max_pin;
264 	u8 oen_max_port;
265 };
266 
267 struct rzg2l_dedicated_configs {
268 	const char *name;
269 	u64 config;
270 };
271 
272 struct rzg2l_pinctrl;
273 
274 struct rzg2l_pinctrl_data {
275 	const char * const *port_pins;
276 	const u64 *port_pin_configs;
277 	unsigned int n_ports;
278 	const struct rzg2l_dedicated_configs *dedicated_pins;
279 	unsigned int n_port_pins;
280 	unsigned int n_dedicated_pins;
281 	const struct rzg2l_hwcfg *hwcfg;
282 	const u64 *variable_pin_cfg;
283 	unsigned int n_variable_pin_cfg;
284 	unsigned int num_custom_params;
285 	const struct pinconf_generic_params *custom_params;
286 #ifdef CONFIG_DEBUG_FS
287 	const struct pin_config_item *custom_conf_items;
288 #endif
289 	void (*pwpr_pfc_lock_unlock)(struct rzg2l_pinctrl *pctrl, bool lock);
290 	void (*pmc_writeb)(struct rzg2l_pinctrl *pctrl, u8 val, u16 offset);
291 	u32 (*oen_read)(struct rzg2l_pinctrl *pctrl, unsigned int _pin);
292 	int (*oen_write)(struct rzg2l_pinctrl *pctrl, unsigned int _pin, u8 oen);
293 	int (*hw_to_bias_param)(unsigned int val);
294 	int (*bias_param_to_hw)(enum pin_config_param param);
295 };
296 
297 /**
298  * struct rzg2l_pinctrl_pin_settings - pin data
299  * @power_source: power source
300  * @drive_strength_ua: drive strength (in micro amps)
301  */
302 struct rzg2l_pinctrl_pin_settings {
303 	u16 power_source;
304 	u16 drive_strength_ua;
305 };
306 
307 /**
308  * struct rzg2l_pinctrl_reg_cache - register cache structure (to be used in suspend/resume)
309  * @p: P registers cache
310  * @pm: PM registers cache
311  * @pmc: PMC registers cache
312  * @pfc: PFC registers cache
313  * @iolh: IOLH registers cache
314  * @pupd: PUPD registers cache
315  * @ien: IEN registers cache
316  * @sd_ch: SD_CH registers cache
317  * @eth_poc: ET_POC registers cache
318  * @eth_mode: ETH_MODE register cache
319  * @qspi: QSPI registers cache
320  */
321 struct rzg2l_pinctrl_reg_cache {
322 	u8	*p;
323 	u16	*pm;
324 	u8	*pmc;
325 	u32	*pfc;
326 	u32	*iolh[2];
327 	u32	*ien[2];
328 	u32	*pupd[2];
329 	u8	sd_ch[2];
330 	u8	eth_poc[2];
331 	u8	eth_mode;
332 	u8	qspi;
333 };
334 
335 struct rzg2l_pinctrl {
336 	struct pinctrl_dev		*pctl;
337 	struct pinctrl_desc		desc;
338 	struct pinctrl_pin_desc		*pins;
339 
340 	const struct rzg2l_pinctrl_data	*data;
341 	void __iomem			*base;
342 	struct device			*dev;
343 
344 	struct clk			*clk;
345 
346 	struct gpio_chip		gpio_chip;
347 	struct pinctrl_gpio_range	gpio_range;
348 	DECLARE_BITMAP(tint_slot, RZG2L_TINT_MAX_INTERRUPT);
349 	spinlock_t			bitmap_lock; /* protect tint_slot bitmap */
350 	unsigned int			hwirq[RZG2L_TINT_MAX_INTERRUPT];
351 
352 	spinlock_t			lock; /* lock read/write registers */
353 	struct mutex			mutex; /* serialize adding groups and functions */
354 
355 	struct rzg2l_pinctrl_pin_settings *settings;
356 	struct rzg2l_pinctrl_reg_cache	*cache;
357 	struct rzg2l_pinctrl_reg_cache	*dedicated_cache;
358 	atomic_t			wakeup_path;
359 };
360 
361 static const u16 available_ps[] = { 1800, 2500, 3300 };
362 
rzg2l_pinctrl_get_variable_pin_cfg(struct rzg2l_pinctrl * pctrl,u64 pincfg,unsigned int port,u8 pin)363 static u64 rzg2l_pinctrl_get_variable_pin_cfg(struct rzg2l_pinctrl *pctrl,
364 					      u64 pincfg,
365 					      unsigned int port,
366 					      u8 pin)
367 {
368 	unsigned int i;
369 
370 	for (i = 0; i < pctrl->data->n_variable_pin_cfg; i++) {
371 		u64 cfg = pctrl->data->variable_pin_cfg[i];
372 
373 		if (FIELD_GET(VARIABLE_PIN_CFG_PORT_MASK, cfg) == port &&
374 		    FIELD_GET(VARIABLE_PIN_CFG_PIN_MASK, cfg) == pin)
375 			return (pincfg & ~RZG2L_VARIABLE_CFG) | FIELD_GET(PIN_CFG_MASK, cfg);
376 	}
377 
378 	return 0;
379 }
380 
381 static const u64 r9a09g057_variable_pin_cfg[] = {
382 	RZG2L_VARIABLE_PIN_CFG_PACK(11, 0, RZV2H_MPXED_PIN_FUNCS),
383 	RZG2L_VARIABLE_PIN_CFG_PACK(11, 1, RZV2H_MPXED_PIN_FUNCS | PIN_CFG_IEN),
384 	RZG2L_VARIABLE_PIN_CFG_PACK(11, 2, RZV2H_MPXED_PIN_FUNCS | PIN_CFG_IEN),
385 	RZG2L_VARIABLE_PIN_CFG_PACK(11, 3, RZV2H_MPXED_PIN_FUNCS | PIN_CFG_IEN),
386 	RZG2L_VARIABLE_PIN_CFG_PACK(11, 4, RZV2H_MPXED_PIN_FUNCS | PIN_CFG_IEN),
387 	RZG2L_VARIABLE_PIN_CFG_PACK(11, 5, RZV2H_MPXED_PIN_FUNCS | PIN_CFG_IEN),
388 };
389 
390 #ifdef CONFIG_RISCV
391 static const u64 r9a07g043f_variable_pin_cfg[] = {
392 	RZG2L_VARIABLE_PIN_CFG_PACK(20, 0, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
393 					   PIN_CFG_NF |
394 					   PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),
395 	RZG2L_VARIABLE_PIN_CFG_PACK(20, 1, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
396 					   PIN_CFG_NF |
397 					   PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),
398 	RZG2L_VARIABLE_PIN_CFG_PACK(20, 2, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
399 					   PIN_CFG_NF |
400 					   PIN_CFG_IEN  | PIN_CFG_NOGPIO_INT),
401 	RZG2L_VARIABLE_PIN_CFG_PACK(20, 3, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
402 					   PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),
403 	RZG2L_VARIABLE_PIN_CFG_PACK(20, 4, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
404 					   PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),
405 	RZG2L_VARIABLE_PIN_CFG_PACK(20, 5, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
406 					   PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),
407 	RZG2L_VARIABLE_PIN_CFG_PACK(20, 6, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
408 					   PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),
409 	RZG2L_VARIABLE_PIN_CFG_PACK(20, 7, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
410 					   PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),
411 	RZG2L_VARIABLE_PIN_CFG_PACK(23, 1, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
412 					   PIN_CFG_NOGPIO_INT),
413 	RZG2L_VARIABLE_PIN_CFG_PACK(23, 2, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
414 					   PIN_CFG_NOGPIO_INT),
415 	RZG2L_VARIABLE_PIN_CFG_PACK(23, 3, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
416 					   PIN_CFG_NOGPIO_INT),
417 	RZG2L_VARIABLE_PIN_CFG_PACK(23, 4, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
418 					   PIN_CFG_NOGPIO_INT),
419 	RZG2L_VARIABLE_PIN_CFG_PACK(23, 5, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_NOGPIO_INT),
420 	RZG2L_VARIABLE_PIN_CFG_PACK(24, 0, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_NOGPIO_INT),
421 	RZG2L_VARIABLE_PIN_CFG_PACK(24, 1, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
422 					   PIN_CFG_NOGPIO_INT),
423 	RZG2L_VARIABLE_PIN_CFG_PACK(24, 2, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
424 					   PIN_CFG_NOGPIO_INT),
425 	RZG2L_VARIABLE_PIN_CFG_PACK(24, 3, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
426 					   PIN_CFG_NOGPIO_INT),
427 	RZG2L_VARIABLE_PIN_CFG_PACK(24, 4, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
428 					   PIN_CFG_NOGPIO_INT),
429 	RZG2L_VARIABLE_PIN_CFG_PACK(24, 5, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
430 					   PIN_CFG_NF |
431 					   PIN_CFG_NOGPIO_INT),
432 };
433 #endif
434 
rzg2l_pmc_writeb(struct rzg2l_pinctrl * pctrl,u8 val,u16 offset)435 static void rzg2l_pmc_writeb(struct rzg2l_pinctrl *pctrl, u8 val, u16 offset)
436 {
437 	writeb(val, pctrl->base + offset);
438 }
439 
rzv2h_pmc_writeb(struct rzg2l_pinctrl * pctrl,u8 val,u16 offset)440 static void rzv2h_pmc_writeb(struct rzg2l_pinctrl *pctrl, u8 val, u16 offset)
441 {
442 	const struct rzg2l_register_offsets *regs = &pctrl->data->hwcfg->regs;
443 	u8 pwpr;
444 
445 	pwpr = readb(pctrl->base + regs->pwpr);
446 	writeb(pwpr | PWPR_REGWE_A, pctrl->base + regs->pwpr);
447 	writeb(val, pctrl->base + offset);
448 	writeb(pwpr & ~PWPR_REGWE_A, pctrl->base + regs->pwpr);
449 }
450 
rzg2l_pinctrl_set_pfc_mode(struct rzg2l_pinctrl * pctrl,u8 pin,u8 off,u8 func)451 static void rzg2l_pinctrl_set_pfc_mode(struct rzg2l_pinctrl *pctrl,
452 				       u8 pin, u8 off, u8 func)
453 {
454 	unsigned long flags;
455 	u32 reg;
456 
457 	spin_lock_irqsave(&pctrl->lock, flags);
458 
459 	/* Set pin to 'Non-use (Hi-Z input protection)'  */
460 	reg = readw(pctrl->base + PM(off));
461 	reg &= ~(PM_MASK << (pin * 2));
462 	writew(reg, pctrl->base + PM(off));
463 
464 	pctrl->data->pwpr_pfc_lock_unlock(pctrl, false);
465 
466 	/* Temporarily switch to GPIO mode with PMC register */
467 	reg = readb(pctrl->base + PMC(off));
468 	writeb(reg & ~BIT(pin), pctrl->base + PMC(off));
469 
470 	/* Select Pin function mode with PFC register */
471 	reg = readl(pctrl->base + PFC(off));
472 	reg &= ~(PFC_MASK << (pin * 4));
473 	writel(reg | (func << (pin * 4)), pctrl->base + PFC(off));
474 
475 	/* Switch to Peripheral pin function with PMC register */
476 	reg = readb(pctrl->base + PMC(off));
477 	writeb(reg | BIT(pin), pctrl->base + PMC(off));
478 
479 	pctrl->data->pwpr_pfc_lock_unlock(pctrl, true);
480 
481 	spin_unlock_irqrestore(&pctrl->lock, flags);
482 };
483 
rzg2l_pinctrl_set_mux(struct pinctrl_dev * pctldev,unsigned int func_selector,unsigned int group_selector)484 static int rzg2l_pinctrl_set_mux(struct pinctrl_dev *pctldev,
485 				 unsigned int func_selector,
486 				 unsigned int group_selector)
487 {
488 	struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
489 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
490 	struct function_desc *func;
491 	unsigned int i, *psel_val;
492 	struct group_desc *group;
493 	const unsigned int *pins;
494 
495 	func = pinmux_generic_get_function(pctldev, func_selector);
496 	if (!func)
497 		return -EINVAL;
498 	group = pinctrl_generic_get_group(pctldev, group_selector);
499 	if (!group)
500 		return -EINVAL;
501 
502 	psel_val = func->data;
503 	pins = group->grp.pins;
504 
505 	for (i = 0; i < group->grp.npins; i++) {
506 		u64 *pin_data = pctrl->desc.pins[pins[i]].drv_data;
507 		u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
508 		u32 pin = RZG2L_PIN_ID_TO_PIN(pins[i]);
509 
510 		dev_dbg(pctrl->dev, "port:%u pin: %u off:%x PSEL:%u\n",
511 			RZG2L_PIN_ID_TO_PORT(pins[i]), pin, off, psel_val[i] - hwcfg->func_base);
512 
513 		rzg2l_pinctrl_set_pfc_mode(pctrl, pin, off, psel_val[i] - hwcfg->func_base);
514 	}
515 
516 	return 0;
517 };
518 
rzg2l_map_add_config(struct pinctrl_map * map,const char * group_or_pin,enum pinctrl_map_type type,unsigned long * configs,unsigned int num_configs)519 static int rzg2l_map_add_config(struct pinctrl_map *map,
520 				const char *group_or_pin,
521 				enum pinctrl_map_type type,
522 				unsigned long *configs,
523 				unsigned int num_configs)
524 {
525 	unsigned long *cfgs;
526 
527 	cfgs = kmemdup_array(configs, num_configs, sizeof(*cfgs), GFP_KERNEL);
528 	if (!cfgs)
529 		return -ENOMEM;
530 
531 	map->type = type;
532 	map->data.configs.group_or_pin = group_or_pin;
533 	map->data.configs.configs = cfgs;
534 	map->data.configs.num_configs = num_configs;
535 
536 	return 0;
537 }
538 
rzg2l_dt_subnode_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct device_node * parent,struct pinctrl_map ** map,unsigned int * num_maps,unsigned int * index)539 static int rzg2l_dt_subnode_to_map(struct pinctrl_dev *pctldev,
540 				   struct device_node *np,
541 				   struct device_node *parent,
542 				   struct pinctrl_map **map,
543 				   unsigned int *num_maps,
544 				   unsigned int *index)
545 {
546 	struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
547 	struct pinctrl_map *maps = *map;
548 	unsigned int nmaps = *num_maps;
549 	unsigned long *configs = NULL;
550 	unsigned int *pins, *psel_val;
551 	unsigned int num_pinmux = 0;
552 	unsigned int idx = *index;
553 	unsigned int num_pins, i;
554 	unsigned int num_configs;
555 	struct property *pinmux;
556 	struct property *prop;
557 	int ret, gsel, fsel;
558 	const char **pin_fn;
559 	const char *name;
560 	const char *pin;
561 
562 	pinmux = of_find_property(np, "pinmux", NULL);
563 	if (pinmux)
564 		num_pinmux = pinmux->length / sizeof(u32);
565 
566 	ret = of_property_count_strings(np, "pins");
567 	if (ret == -EINVAL) {
568 		num_pins = 0;
569 	} else if (ret < 0) {
570 		dev_err(pctrl->dev, "Invalid pins list in DT\n");
571 		return ret;
572 	} else {
573 		num_pins = ret;
574 	}
575 
576 	if (!num_pinmux && !num_pins)
577 		return 0;
578 
579 	if (num_pinmux && num_pins) {
580 		dev_err(pctrl->dev,
581 			"DT node must contain either a pinmux or pins and not both\n");
582 		return -EINVAL;
583 	}
584 
585 	ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &num_configs);
586 	if (ret < 0)
587 		return ret;
588 
589 	if (num_pins && !num_configs) {
590 		dev_err(pctrl->dev, "DT node must contain a config\n");
591 		ret = -ENODEV;
592 		goto done;
593 	}
594 
595 	if (num_pinmux) {
596 		nmaps += 1;
597 		if (num_configs)
598 			nmaps += 1;
599 	}
600 
601 	if (num_pins)
602 		nmaps += num_pins;
603 
604 	maps = krealloc_array(maps, nmaps, sizeof(*maps), GFP_KERNEL);
605 	if (!maps) {
606 		ret = -ENOMEM;
607 		goto done;
608 	}
609 
610 	*map = maps;
611 	*num_maps = nmaps;
612 	if (num_pins) {
613 		of_property_for_each_string(np, "pins", prop, pin) {
614 			ret = rzg2l_map_add_config(&maps[idx], pin,
615 						   PIN_MAP_TYPE_CONFIGS_PIN,
616 						   configs, num_configs);
617 			if (ret < 0)
618 				goto done;
619 
620 			idx++;
621 		}
622 		ret = 0;
623 		goto done;
624 	}
625 
626 	pins = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*pins), GFP_KERNEL);
627 	psel_val = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*psel_val),
628 				GFP_KERNEL);
629 	pin_fn = devm_kzalloc(pctrl->dev, sizeof(*pin_fn), GFP_KERNEL);
630 	if (!pins || !psel_val || !pin_fn) {
631 		ret = -ENOMEM;
632 		goto done;
633 	}
634 
635 	/* Collect pin locations and mux settings from DT properties */
636 	for (i = 0; i < num_pinmux; ++i) {
637 		u32 value;
638 
639 		ret = of_property_read_u32_index(np, "pinmux", i, &value);
640 		if (ret)
641 			goto done;
642 		pins[i] = FIELD_GET(MUX_PIN_ID_MASK, value);
643 		psel_val[i] = FIELD_GET(MUX_FUNC_MASK, value);
644 	}
645 
646 	if (parent) {
647 		name = devm_kasprintf(pctrl->dev, GFP_KERNEL, "%pOFn.%pOFn",
648 				      parent, np);
649 		if (!name) {
650 			ret = -ENOMEM;
651 			goto done;
652 		}
653 	} else {
654 		name = np->name;
655 	}
656 
657 	if (num_configs) {
658 		ret = rzg2l_map_add_config(&maps[idx], name,
659 					   PIN_MAP_TYPE_CONFIGS_GROUP,
660 					   configs, num_configs);
661 		if (ret < 0)
662 			goto done;
663 
664 		idx++;
665 	}
666 
667 	mutex_lock(&pctrl->mutex);
668 
669 	/* Register a single pin group listing all the pins we read from DT */
670 	gsel = pinctrl_generic_add_group(pctldev, name, pins, num_pinmux, NULL);
671 	if (gsel < 0) {
672 		ret = gsel;
673 		goto unlock;
674 	}
675 
676 	/*
677 	 * Register a single group function where the 'data' is an array PSEL
678 	 * register values read from DT.
679 	 */
680 	pin_fn[0] = name;
681 	fsel = pinmux_generic_add_function(pctldev, name, pin_fn, 1, psel_val);
682 	if (fsel < 0) {
683 		ret = fsel;
684 		goto remove_group;
685 	}
686 
687 	mutex_unlock(&pctrl->mutex);
688 
689 	maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
690 	maps[idx].data.mux.group = name;
691 	maps[idx].data.mux.function = name;
692 	idx++;
693 
694 	dev_dbg(pctrl->dev, "Parsed %pOF with %d pins\n", np, num_pinmux);
695 	ret = 0;
696 	goto done;
697 
698 remove_group:
699 	pinctrl_generic_remove_group(pctldev, gsel);
700 unlock:
701 	mutex_unlock(&pctrl->mutex);
702 done:
703 	*index = idx;
704 	kfree(configs);
705 	return ret;
706 }
707 
rzg2l_dt_free_map(struct pinctrl_dev * pctldev,struct pinctrl_map * map,unsigned int num_maps)708 static void rzg2l_dt_free_map(struct pinctrl_dev *pctldev,
709 			      struct pinctrl_map *map,
710 			      unsigned int num_maps)
711 {
712 	unsigned int i;
713 
714 	if (!map)
715 		return;
716 
717 	for (i = 0; i < num_maps; ++i) {
718 		if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
719 		    map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
720 			kfree(map[i].data.configs.configs);
721 	}
722 	kfree(map);
723 }
724 
rzg2l_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** map,unsigned int * num_maps)725 static int rzg2l_dt_node_to_map(struct pinctrl_dev *pctldev,
726 				struct device_node *np,
727 				struct pinctrl_map **map,
728 				unsigned int *num_maps)
729 {
730 	struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
731 	unsigned int index;
732 	int ret;
733 
734 	*map = NULL;
735 	*num_maps = 0;
736 	index = 0;
737 
738 	for_each_child_of_node_scoped(np, child) {
739 		ret = rzg2l_dt_subnode_to_map(pctldev, child, np, map,
740 					      num_maps, &index);
741 		if (ret < 0)
742 			goto done;
743 	}
744 
745 	if (*num_maps == 0) {
746 		ret = rzg2l_dt_subnode_to_map(pctldev, np, NULL, map,
747 					      num_maps, &index);
748 		if (ret < 0)
749 			goto done;
750 	}
751 
752 	if (*num_maps)
753 		return 0;
754 
755 	dev_err(pctrl->dev, "no mapping found in node %pOF\n", np);
756 	ret = -EINVAL;
757 
758 done:
759 	rzg2l_dt_free_map(pctldev, *map, *num_maps);
760 
761 	return ret;
762 }
763 
rzg2l_validate_gpio_pin(struct rzg2l_pinctrl * pctrl,u64 cfg,u32 port,u8 bit)764 static int rzg2l_validate_gpio_pin(struct rzg2l_pinctrl *pctrl,
765 				   u64 cfg, u32 port, u8 bit)
766 {
767 	u8 pinmap = FIELD_GET(PIN_CFG_PIN_MAP_MASK, cfg);
768 	u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(cfg);
769 	u64 data;
770 
771 	if (!(pinmap & BIT(bit)) || port >= pctrl->data->n_port_pins)
772 		return -EINVAL;
773 
774 	data = pctrl->data->port_pin_configs[port];
775 	if (off != RZG2L_PIN_CFG_TO_PORT_OFFSET(data))
776 		return -EINVAL;
777 
778 	return 0;
779 }
780 
rzg2l_read_pin_config(struct rzg2l_pinctrl * pctrl,u32 offset,u8 bit,u32 mask)781 static u32 rzg2l_read_pin_config(struct rzg2l_pinctrl *pctrl, u32 offset,
782 				 u8 bit, u32 mask)
783 {
784 	void __iomem *addr = pctrl->base + offset;
785 
786 	/* handle _L/_H for 32-bit register read/write */
787 	if (bit >= 4) {
788 		bit -= 4;
789 		addr += 4;
790 	}
791 
792 	return (readl(addr) >> (bit * 8)) & mask;
793 }
794 
rzg2l_rmw_pin_config(struct rzg2l_pinctrl * pctrl,u32 offset,u8 bit,u32 mask,u32 val)795 static void rzg2l_rmw_pin_config(struct rzg2l_pinctrl *pctrl, u32 offset,
796 				 u8 bit, u32 mask, u32 val)
797 {
798 	void __iomem *addr = pctrl->base + offset;
799 	unsigned long flags;
800 	u32 reg;
801 
802 	/* handle _L/_H for 32-bit register read/write */
803 	if (bit >= 4) {
804 		bit -= 4;
805 		addr += 4;
806 	}
807 
808 	spin_lock_irqsave(&pctrl->lock, flags);
809 	reg = readl(addr) & ~(mask << (bit * 8));
810 	writel(reg | (val << (bit * 8)), addr);
811 	spin_unlock_irqrestore(&pctrl->lock, flags);
812 }
813 
rzg2l_caps_to_pwr_reg(const struct rzg2l_register_offsets * regs,u32 caps)814 static int rzg2l_caps_to_pwr_reg(const struct rzg2l_register_offsets *regs, u32 caps)
815 {
816 	if (caps & PIN_CFG_IO_VMC_SD0)
817 		return SD_CH(regs->sd_ch, 0);
818 	if (caps & PIN_CFG_IO_VMC_SD1)
819 		return SD_CH(regs->sd_ch, 1);
820 	if (caps & PIN_CFG_IO_VMC_ETH0)
821 		return ETH_POC(regs->eth_poc, 0);
822 	if (caps & PIN_CFG_IO_VMC_ETH1)
823 		return ETH_POC(regs->eth_poc, 1);
824 	if (caps & PIN_CFG_IO_VMC_QSPI)
825 		return QSPI;
826 
827 	return -EINVAL;
828 }
829 
rzg2l_get_power_source(struct rzg2l_pinctrl * pctrl,u32 pin,u32 caps)830 static int rzg2l_get_power_source(struct rzg2l_pinctrl *pctrl, u32 pin, u32 caps)
831 {
832 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
833 	const struct rzg2l_register_offsets *regs = &hwcfg->regs;
834 	int pwr_reg;
835 	u8 val;
836 
837 	if (caps & PIN_CFG_SOFT_PS)
838 		return pctrl->settings[pin].power_source;
839 
840 	pwr_reg = rzg2l_caps_to_pwr_reg(regs, caps);
841 	if (pwr_reg < 0)
842 		return pwr_reg;
843 
844 	val = readb(pctrl->base + pwr_reg);
845 	switch (val) {
846 	case PVDD_1800:
847 		return 1800;
848 	case PVDD_2500:
849 		return 2500;
850 	case PVDD_3300:
851 		return 3300;
852 	default:
853 		/* Should not happen. */
854 		return -EINVAL;
855 	}
856 }
857 
rzg2l_set_power_source(struct rzg2l_pinctrl * pctrl,u32 pin,u32 caps,u32 ps)858 static int rzg2l_set_power_source(struct rzg2l_pinctrl *pctrl, u32 pin, u32 caps, u32 ps)
859 {
860 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
861 	const struct rzg2l_register_offsets *regs = &hwcfg->regs;
862 	int pwr_reg;
863 	u8 val;
864 
865 	if (caps & PIN_CFG_SOFT_PS) {
866 		pctrl->settings[pin].power_source = ps;
867 		return 0;
868 	}
869 
870 	switch (ps) {
871 	case 1800:
872 		val = PVDD_1800;
873 		break;
874 	case 2500:
875 		if (!(caps & (PIN_CFG_IO_VMC_ETH0 | PIN_CFG_IO_VMC_ETH1)))
876 			return -EINVAL;
877 		val = PVDD_2500;
878 		break;
879 	case 3300:
880 		val = PVDD_3300;
881 		break;
882 	default:
883 		return -EINVAL;
884 	}
885 
886 	pwr_reg = rzg2l_caps_to_pwr_reg(regs, caps);
887 	if (pwr_reg < 0)
888 		return pwr_reg;
889 
890 	writeb(val, pctrl->base + pwr_reg);
891 	pctrl->settings[pin].power_source = ps;
892 
893 	return 0;
894 }
895 
rzg2l_ps_is_supported(u16 ps)896 static bool rzg2l_ps_is_supported(u16 ps)
897 {
898 	unsigned int i;
899 
900 	for (i = 0; i < ARRAY_SIZE(available_ps); i++) {
901 		if (available_ps[i] == ps)
902 			return true;
903 	}
904 
905 	return false;
906 }
907 
rzg2l_ps_to_iolh_idx(u16 ps)908 static enum rzg2l_iolh_index rzg2l_ps_to_iolh_idx(u16 ps)
909 {
910 	unsigned int i;
911 
912 	for (i = 0; i < ARRAY_SIZE(available_ps); i++) {
913 		if (available_ps[i] == ps)
914 			break;
915 	}
916 
917 	/*
918 	 * We multiply with RZG2L_IOLH_MAX_DS_ENTRIES as we have
919 	 * RZG2L_IOLH_MAX_DS_ENTRIES DS values per power source
920 	 */
921 	return i * RZG2L_IOLH_MAX_DS_ENTRIES;
922 }
923 
rzg2l_iolh_val_to_ua(const struct rzg2l_hwcfg * hwcfg,u32 caps,u8 val)924 static u16 rzg2l_iolh_val_to_ua(const struct rzg2l_hwcfg *hwcfg, u32 caps, u8 val)
925 {
926 	if (caps & PIN_CFG_IOLH_A)
927 		return hwcfg->iolh_groupa_ua[val];
928 
929 	if (caps & PIN_CFG_IOLH_B)
930 		return hwcfg->iolh_groupb_ua[val];
931 
932 	if (caps & PIN_CFG_IOLH_C)
933 		return hwcfg->iolh_groupc_ua[val];
934 
935 	/* Should not happen. */
936 	return 0;
937 }
938 
rzg2l_iolh_ua_to_val(const struct rzg2l_hwcfg * hwcfg,u32 caps,enum rzg2l_iolh_index ps_index,u16 ua)939 static int rzg2l_iolh_ua_to_val(const struct rzg2l_hwcfg *hwcfg, u32 caps,
940 				enum rzg2l_iolh_index ps_index, u16 ua)
941 {
942 	const u16 *array = NULL;
943 	unsigned int i;
944 
945 	if (caps & PIN_CFG_IOLH_A)
946 		array = &hwcfg->iolh_groupa_ua[ps_index];
947 
948 	if (caps & PIN_CFG_IOLH_B)
949 		array = &hwcfg->iolh_groupb_ua[ps_index];
950 
951 	if (caps & PIN_CFG_IOLH_C)
952 		array = &hwcfg->iolh_groupc_ua[ps_index];
953 
954 	if (!array)
955 		return -EINVAL;
956 
957 	for (i = 0; i < RZG2L_IOLH_MAX_DS_ENTRIES; i++) {
958 		if (array[i] == ua)
959 			return i;
960 	}
961 
962 	return -EINVAL;
963 }
964 
rzg2l_ds_is_supported(struct rzg2l_pinctrl * pctrl,u32 caps,enum rzg2l_iolh_index iolh_idx,u16 ds)965 static bool rzg2l_ds_is_supported(struct rzg2l_pinctrl *pctrl, u32 caps,
966 				  enum rzg2l_iolh_index iolh_idx,
967 				  u16 ds)
968 {
969 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
970 	const u16 *array = NULL;
971 	unsigned int i;
972 
973 	if (caps & PIN_CFG_IOLH_A)
974 		array = hwcfg->iolh_groupa_ua;
975 
976 	if (caps & PIN_CFG_IOLH_B)
977 		array = hwcfg->iolh_groupb_ua;
978 
979 	if (caps & PIN_CFG_IOLH_C)
980 		array = hwcfg->iolh_groupc_ua;
981 
982 	/* Should not happen. */
983 	if (!array)
984 		return false;
985 
986 	if (!array[iolh_idx])
987 		return false;
988 
989 	for (i = 0; i < RZG2L_IOLH_MAX_DS_ENTRIES; i++) {
990 		if (array[iolh_idx + i] == ds)
991 			return true;
992 	}
993 
994 	return false;
995 }
996 
rzg2l_pin_to_oen_bit(struct rzg2l_pinctrl * pctrl,unsigned int _pin)997 static int rzg2l_pin_to_oen_bit(struct rzg2l_pinctrl *pctrl, unsigned int _pin)
998 {
999 	u64 *pin_data = pctrl->desc.pins[_pin].drv_data;
1000 	u64 caps = FIELD_GET(PIN_CFG_MASK, *pin_data);
1001 	u8 pin = RZG2L_PIN_ID_TO_PIN(_pin);
1002 
1003 	if (pin > pctrl->data->hwcfg->oen_max_pin)
1004 		return -EINVAL;
1005 
1006 	/*
1007 	 * We can determine which Ethernet interface we're dealing with from
1008 	 * the caps.
1009 	 */
1010 	if (caps & PIN_CFG_IO_VMC_ETH0)
1011 		return 0;
1012 	if (caps & PIN_CFG_IO_VMC_ETH1)
1013 		return 1;
1014 
1015 	return -EINVAL;
1016 }
1017 
rzg2l_read_oen(struct rzg2l_pinctrl * pctrl,unsigned int _pin)1018 static u32 rzg2l_read_oen(struct rzg2l_pinctrl *pctrl, unsigned int _pin)
1019 {
1020 	int bit;
1021 
1022 	bit = rzg2l_pin_to_oen_bit(pctrl, _pin);
1023 	if (bit < 0)
1024 		return 0;
1025 
1026 	return !(readb(pctrl->base + ETH_MODE) & BIT(bit));
1027 }
1028 
rzg2l_write_oen(struct rzg2l_pinctrl * pctrl,unsigned int _pin,u8 oen)1029 static int rzg2l_write_oen(struct rzg2l_pinctrl *pctrl, unsigned int _pin, u8 oen)
1030 {
1031 	unsigned long flags;
1032 	int bit;
1033 	u8 val;
1034 
1035 	bit = rzg2l_pin_to_oen_bit(pctrl, _pin);
1036 	if (bit < 0)
1037 		return bit;
1038 
1039 	spin_lock_irqsave(&pctrl->lock, flags);
1040 	val = readb(pctrl->base + ETH_MODE);
1041 	if (oen)
1042 		val &= ~BIT(bit);
1043 	else
1044 		val |= BIT(bit);
1045 	writeb(val, pctrl->base + ETH_MODE);
1046 	spin_unlock_irqrestore(&pctrl->lock, flags);
1047 
1048 	return 0;
1049 }
1050 
rzg3s_pin_to_oen_bit(struct rzg2l_pinctrl * pctrl,unsigned int _pin)1051 static int rzg3s_pin_to_oen_bit(struct rzg2l_pinctrl *pctrl, unsigned int _pin)
1052 {
1053 	u64 *pin_data = pctrl->desc.pins[_pin].drv_data;
1054 	u8 port, pin, bit;
1055 
1056 	if (*pin_data & RZG2L_SINGLE_PIN)
1057 		return -EINVAL;
1058 
1059 	port = RZG2L_PIN_ID_TO_PORT(_pin);
1060 	pin = RZG2L_PIN_ID_TO_PIN(_pin);
1061 	if (pin > pctrl->data->hwcfg->oen_max_pin)
1062 		return -EINVAL;
1063 
1064 	bit = pin * 2;
1065 	if (port == pctrl->data->hwcfg->oen_max_port)
1066 		bit += 1;
1067 
1068 	return bit;
1069 }
1070 
rzg3s_oen_read(struct rzg2l_pinctrl * pctrl,unsigned int _pin)1071 static u32 rzg3s_oen_read(struct rzg2l_pinctrl *pctrl, unsigned int _pin)
1072 {
1073 	int bit;
1074 
1075 	bit = rzg3s_pin_to_oen_bit(pctrl, _pin);
1076 	if (bit < 0)
1077 		return bit;
1078 
1079 	return !(readb(pctrl->base + ETH_MODE) & BIT(bit));
1080 }
1081 
rzg3s_oen_write(struct rzg2l_pinctrl * pctrl,unsigned int _pin,u8 oen)1082 static int rzg3s_oen_write(struct rzg2l_pinctrl *pctrl, unsigned int _pin, u8 oen)
1083 {
1084 	unsigned long flags;
1085 	int bit;
1086 	u8 val;
1087 
1088 	bit = rzg3s_pin_to_oen_bit(pctrl, _pin);
1089 	if (bit < 0)
1090 		return bit;
1091 
1092 	spin_lock_irqsave(&pctrl->lock, flags);
1093 	val = readb(pctrl->base + ETH_MODE);
1094 	if (oen)
1095 		val &= ~BIT(bit);
1096 	else
1097 		val |= BIT(bit);
1098 	writeb(val, pctrl->base + ETH_MODE);
1099 	spin_unlock_irqrestore(&pctrl->lock, flags);
1100 
1101 	return 0;
1102 }
1103 
rzg2l_hw_to_bias_param(unsigned int bias)1104 static int rzg2l_hw_to_bias_param(unsigned int bias)
1105 {
1106 	switch (bias) {
1107 	case 0:
1108 		return PIN_CONFIG_BIAS_DISABLE;
1109 	case 1:
1110 		return PIN_CONFIG_BIAS_PULL_UP;
1111 	case 2:
1112 		return PIN_CONFIG_BIAS_PULL_DOWN;
1113 	default:
1114 		break;
1115 	}
1116 
1117 	return -EINVAL;
1118 }
1119 
rzg2l_bias_param_to_hw(enum pin_config_param param)1120 static int rzg2l_bias_param_to_hw(enum pin_config_param param)
1121 {
1122 	switch (param) {
1123 	case PIN_CONFIG_BIAS_DISABLE:
1124 		return 0;
1125 	case PIN_CONFIG_BIAS_PULL_UP:
1126 		return 1;
1127 	case PIN_CONFIG_BIAS_PULL_DOWN:
1128 		return 2;
1129 	default:
1130 		break;
1131 	}
1132 
1133 	return -EINVAL;
1134 }
1135 
rzv2h_hw_to_bias_param(unsigned int bias)1136 static int rzv2h_hw_to_bias_param(unsigned int bias)
1137 {
1138 	switch (bias) {
1139 	case 0:
1140 	case 1:
1141 		return PIN_CONFIG_BIAS_DISABLE;
1142 	case 2:
1143 		return PIN_CONFIG_BIAS_PULL_DOWN;
1144 	case 3:
1145 		return PIN_CONFIG_BIAS_PULL_UP;
1146 	default:
1147 		break;
1148 	}
1149 
1150 	return -EINVAL;
1151 }
1152 
rzv2h_bias_param_to_hw(enum pin_config_param param)1153 static int rzv2h_bias_param_to_hw(enum pin_config_param param)
1154 {
1155 	switch (param) {
1156 	case PIN_CONFIG_BIAS_DISABLE:
1157 		return 0;
1158 	case PIN_CONFIG_BIAS_PULL_DOWN:
1159 		return 2;
1160 	case PIN_CONFIG_BIAS_PULL_UP:
1161 		return 3;
1162 	default:
1163 		break;
1164 	}
1165 
1166 	return -EINVAL;
1167 }
1168 
rzv2h_pin_to_oen_bit(struct rzg2l_pinctrl * pctrl,unsigned int _pin)1169 static u8 rzv2h_pin_to_oen_bit(struct rzg2l_pinctrl *pctrl, unsigned int _pin)
1170 {
1171 	static const char * const pin_names[] = { "ET0_TXC_TXCLK", "ET1_TXC_TXCLK",
1172 						  "XSPI0_RESET0N", "XSPI0_CS0N",
1173 						  "XSPI0_CKN", "XSPI0_CKP" };
1174 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[_pin];
1175 	unsigned int i;
1176 
1177 	for (i = 0; i < ARRAY_SIZE(pin_names); i++) {
1178 		if (!strcmp(pin_desc->name, pin_names[i]))
1179 			return i;
1180 	}
1181 
1182 	/* Should not happen. */
1183 	return 0;
1184 }
1185 
rzv2h_oen_read(struct rzg2l_pinctrl * pctrl,unsigned int _pin)1186 static u32 rzv2h_oen_read(struct rzg2l_pinctrl *pctrl, unsigned int _pin)
1187 {
1188 	u8 bit;
1189 
1190 	bit = rzv2h_pin_to_oen_bit(pctrl, _pin);
1191 
1192 	return !(readb(pctrl->base + PFC_OEN) & BIT(bit));
1193 }
1194 
rzv2h_oen_write(struct rzg2l_pinctrl * pctrl,unsigned int _pin,u8 oen)1195 static int rzv2h_oen_write(struct rzg2l_pinctrl *pctrl, unsigned int _pin, u8 oen)
1196 {
1197 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
1198 	const struct rzg2l_register_offsets *regs = &hwcfg->regs;
1199 	unsigned long flags;
1200 	u8 val, bit;
1201 	u8 pwpr;
1202 
1203 	bit = rzv2h_pin_to_oen_bit(pctrl, _pin);
1204 	spin_lock_irqsave(&pctrl->lock, flags);
1205 	val = readb(pctrl->base + PFC_OEN);
1206 	if (oen)
1207 		val &= ~BIT(bit);
1208 	else
1209 		val |= BIT(bit);
1210 
1211 	pwpr = readb(pctrl->base + regs->pwpr);
1212 	writeb(pwpr | PWPR_REGWE_B, pctrl->base + regs->pwpr);
1213 	writeb(val, pctrl->base + PFC_OEN);
1214 	writeb(pwpr & ~PWPR_REGWE_B, pctrl->base + regs->pwpr);
1215 	spin_unlock_irqrestore(&pctrl->lock, flags);
1216 
1217 	return 0;
1218 }
1219 
rzg2l_pinctrl_pinconf_get(struct pinctrl_dev * pctldev,unsigned int _pin,unsigned long * config)1220 static int rzg2l_pinctrl_pinconf_get(struct pinctrl_dev *pctldev,
1221 				     unsigned int _pin,
1222 				     unsigned long *config)
1223 {
1224 	struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
1225 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
1226 	const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
1227 	u32 param = pinconf_to_config_param(*config);
1228 	u64 *pin_data = pin->drv_data;
1229 	unsigned int arg = 0;
1230 	u32 off;
1231 	u32 cfg;
1232 	int ret;
1233 	u8 bit;
1234 
1235 	if (!pin_data)
1236 		return -EINVAL;
1237 
1238 	off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
1239 	cfg = FIELD_GET(PIN_CFG_MASK, *pin_data);
1240 	if (*pin_data & RZG2L_SINGLE_PIN) {
1241 		bit = FIELD_GET(RZG2L_SINGLE_PIN_BITS_MASK, *pin_data);
1242 	} else {
1243 		bit = RZG2L_PIN_ID_TO_PIN(_pin);
1244 
1245 		if (rzg2l_validate_gpio_pin(pctrl, *pin_data, RZG2L_PIN_ID_TO_PORT(_pin), bit))
1246 			return -EINVAL;
1247 	}
1248 
1249 	switch (param) {
1250 	case PIN_CONFIG_INPUT_ENABLE:
1251 		if (!(cfg & PIN_CFG_IEN))
1252 			return -EINVAL;
1253 		arg = rzg2l_read_pin_config(pctrl, IEN(off), bit, IEN_MASK);
1254 		if (!arg)
1255 			return -EINVAL;
1256 		break;
1257 
1258 	case PIN_CONFIG_OUTPUT_ENABLE:
1259 		if (!(cfg & PIN_CFG_OEN))
1260 			return -EINVAL;
1261 		if (!pctrl->data->oen_read)
1262 			return -EOPNOTSUPP;
1263 		arg = pctrl->data->oen_read(pctrl, _pin);
1264 		if (!arg)
1265 			return -EINVAL;
1266 		break;
1267 
1268 	case PIN_CONFIG_POWER_SOURCE:
1269 		ret = rzg2l_get_power_source(pctrl, _pin, cfg);
1270 		if (ret < 0)
1271 			return ret;
1272 		arg = ret;
1273 		break;
1274 
1275 	case PIN_CONFIG_SLEW_RATE:
1276 		if (!(cfg & PIN_CFG_SR))
1277 			return -EINVAL;
1278 
1279 		arg = rzg2l_read_pin_config(pctrl, SR(off), bit, SR_MASK);
1280 		break;
1281 
1282 	case PIN_CONFIG_BIAS_DISABLE:
1283 	case PIN_CONFIG_BIAS_PULL_UP:
1284 	case PIN_CONFIG_BIAS_PULL_DOWN:
1285 		if (!(cfg & PIN_CFG_PUPD))
1286 			return -EINVAL;
1287 
1288 		arg = rzg2l_read_pin_config(pctrl, PUPD(off), bit, PUPD_MASK);
1289 		ret = pctrl->data->hw_to_bias_param(arg);
1290 		if (ret < 0)
1291 			return ret;
1292 
1293 		if (ret != param)
1294 			return -EINVAL;
1295 		/* for PIN_CONFIG_BIAS_PULL_UP/DOWN when enabled we just return 1 */
1296 		arg = 1;
1297 		break;
1298 
1299 	case PIN_CONFIG_DRIVE_STRENGTH: {
1300 		unsigned int index;
1301 
1302 		if (!(cfg & PIN_CFG_IOLH_A) || hwcfg->drive_strength_ua)
1303 			return -EINVAL;
1304 
1305 		index = rzg2l_read_pin_config(pctrl, IOLH(off), bit, IOLH_MASK);
1306 		/*
1307 		 * Drive strenght mA is supported only by group A and only
1308 		 * for 3V3 port source.
1309 		 */
1310 		arg = hwcfg->iolh_groupa_ua[index + RZG2L_IOLH_IDX_3V3] / 1000;
1311 		break;
1312 	}
1313 
1314 	case PIN_CONFIG_DRIVE_STRENGTH_UA: {
1315 		enum rzg2l_iolh_index iolh_idx;
1316 		u8 val;
1317 
1318 		if (!(cfg & (PIN_CFG_IOLH_A | PIN_CFG_IOLH_B | PIN_CFG_IOLH_C)) ||
1319 		    !hwcfg->drive_strength_ua)
1320 			return -EINVAL;
1321 
1322 		ret = rzg2l_get_power_source(pctrl, _pin, cfg);
1323 		if (ret < 0)
1324 			return ret;
1325 		iolh_idx = rzg2l_ps_to_iolh_idx(ret);
1326 		val = rzg2l_read_pin_config(pctrl, IOLH(off), bit, IOLH_MASK);
1327 		arg = rzg2l_iolh_val_to_ua(hwcfg, cfg, iolh_idx + val);
1328 		break;
1329 	}
1330 
1331 	case PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS: {
1332 		unsigned int index;
1333 
1334 		if (!(cfg & PIN_CFG_IOLH_B) || !hwcfg->iolh_groupb_oi[0])
1335 			return -EINVAL;
1336 
1337 		index = rzg2l_read_pin_config(pctrl, IOLH(off), bit, IOLH_MASK);
1338 		arg = hwcfg->iolh_groupb_oi[index];
1339 		break;
1340 	}
1341 
1342 	case RENESAS_RZV2H_PIN_CONFIG_OUTPUT_IMPEDANCE:
1343 		if (!(cfg & PIN_CFG_IOLH_RZV2H))
1344 			return -EINVAL;
1345 
1346 		arg = rzg2l_read_pin_config(pctrl, IOLH(off), bit, IOLH_MASK);
1347 		break;
1348 
1349 	default:
1350 		return -ENOTSUPP;
1351 	}
1352 
1353 	*config = pinconf_to_config_packed(param, arg);
1354 
1355 	return 0;
1356 };
1357 
rzg2l_pinctrl_pinconf_set(struct pinctrl_dev * pctldev,unsigned int _pin,unsigned long * _configs,unsigned int num_configs)1358 static int rzg2l_pinctrl_pinconf_set(struct pinctrl_dev *pctldev,
1359 				     unsigned int _pin,
1360 				     unsigned long *_configs,
1361 				     unsigned int num_configs)
1362 {
1363 	struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
1364 	const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
1365 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
1366 	struct rzg2l_pinctrl_pin_settings settings = pctrl->settings[_pin];
1367 	u64 *pin_data = pin->drv_data;
1368 	unsigned int i, arg, index;
1369 	u32 off, param;
1370 	u32 cfg;
1371 	int ret;
1372 	u8 bit;
1373 
1374 	if (!pin_data)
1375 		return -EINVAL;
1376 
1377 	off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
1378 	cfg = FIELD_GET(PIN_CFG_MASK, *pin_data);
1379 	if (*pin_data & RZG2L_SINGLE_PIN) {
1380 		bit = FIELD_GET(RZG2L_SINGLE_PIN_BITS_MASK, *pin_data);
1381 	} else {
1382 		bit = RZG2L_PIN_ID_TO_PIN(_pin);
1383 
1384 		if (rzg2l_validate_gpio_pin(pctrl, *pin_data, RZG2L_PIN_ID_TO_PORT(_pin), bit))
1385 			return -EINVAL;
1386 	}
1387 
1388 	for (i = 0; i < num_configs; i++) {
1389 		param = pinconf_to_config_param(_configs[i]);
1390 		arg = pinconf_to_config_argument(_configs[i]);
1391 		switch (param) {
1392 		case PIN_CONFIG_INPUT_ENABLE:
1393 
1394 			if (!(cfg & PIN_CFG_IEN))
1395 				return -EINVAL;
1396 
1397 			rzg2l_rmw_pin_config(pctrl, IEN(off), bit, IEN_MASK, !!arg);
1398 			break;
1399 
1400 		case PIN_CONFIG_OUTPUT_ENABLE:
1401 			if (!(cfg & PIN_CFG_OEN))
1402 				return -EINVAL;
1403 			if (!pctrl->data->oen_write)
1404 				return -EOPNOTSUPP;
1405 			ret = pctrl->data->oen_write(pctrl, _pin, !!arg);
1406 			if (ret)
1407 				return ret;
1408 			break;
1409 
1410 		case PIN_CONFIG_POWER_SOURCE:
1411 			settings.power_source = arg;
1412 			break;
1413 
1414 		case PIN_CONFIG_SLEW_RATE:
1415 			if (!(cfg & PIN_CFG_SR) || arg > 1)
1416 				return -EINVAL;
1417 
1418 			rzg2l_rmw_pin_config(pctrl, SR(off), bit, SR_MASK, arg);
1419 			break;
1420 
1421 		case PIN_CONFIG_BIAS_DISABLE:
1422 		case PIN_CONFIG_BIAS_PULL_UP:
1423 		case PIN_CONFIG_BIAS_PULL_DOWN:
1424 			if (!(cfg & PIN_CFG_PUPD))
1425 				return -EINVAL;
1426 
1427 			ret = pctrl->data->bias_param_to_hw(param);
1428 			if (ret < 0)
1429 				return ret;
1430 
1431 			rzg2l_rmw_pin_config(pctrl, PUPD(off), bit, PUPD_MASK, ret);
1432 			break;
1433 
1434 		case PIN_CONFIG_DRIVE_STRENGTH:
1435 			if (!(cfg & PIN_CFG_IOLH_A) || hwcfg->drive_strength_ua)
1436 				return -EINVAL;
1437 
1438 			for (index = RZG2L_IOLH_IDX_3V3;
1439 			     index < RZG2L_IOLH_IDX_3V3 + RZG2L_IOLH_MAX_DS_ENTRIES; index++) {
1440 				if (arg == (hwcfg->iolh_groupa_ua[index] / 1000))
1441 					break;
1442 			}
1443 			if (index == (RZG2L_IOLH_IDX_3V3 + RZG2L_IOLH_MAX_DS_ENTRIES))
1444 				return -EINVAL;
1445 
1446 			rzg2l_rmw_pin_config(pctrl, IOLH(off), bit, IOLH_MASK, index);
1447 			break;
1448 
1449 		case PIN_CONFIG_DRIVE_STRENGTH_UA:
1450 			if (!(cfg & (PIN_CFG_IOLH_A | PIN_CFG_IOLH_B | PIN_CFG_IOLH_C)) ||
1451 			    !hwcfg->drive_strength_ua)
1452 				return -EINVAL;
1453 
1454 			settings.drive_strength_ua = arg;
1455 			break;
1456 
1457 		case PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS:
1458 			if (!(cfg & PIN_CFG_IOLH_B) || !hwcfg->iolh_groupb_oi[0])
1459 				return -EINVAL;
1460 
1461 			for (index = 0; index < ARRAY_SIZE(hwcfg->iolh_groupb_oi); index++) {
1462 				if (arg == hwcfg->iolh_groupb_oi[index])
1463 					break;
1464 			}
1465 			if (index == ARRAY_SIZE(hwcfg->iolh_groupb_oi))
1466 				return -EINVAL;
1467 
1468 			rzg2l_rmw_pin_config(pctrl, IOLH(off), bit, IOLH_MASK, index);
1469 			break;
1470 
1471 		case RENESAS_RZV2H_PIN_CONFIG_OUTPUT_IMPEDANCE:
1472 			if (!(cfg & PIN_CFG_IOLH_RZV2H))
1473 				return -EINVAL;
1474 
1475 			if (arg > 3)
1476 				return -EINVAL;
1477 			rzg2l_rmw_pin_config(pctrl, IOLH(off), bit, IOLH_MASK, arg);
1478 			break;
1479 
1480 		default:
1481 			return -EOPNOTSUPP;
1482 		}
1483 	}
1484 
1485 	/* Apply power source. */
1486 	if (settings.power_source != pctrl->settings[_pin].power_source) {
1487 		ret = rzg2l_ps_is_supported(settings.power_source);
1488 		if (!ret)
1489 			return -EINVAL;
1490 
1491 		/* Apply power source. */
1492 		ret = rzg2l_set_power_source(pctrl, _pin, cfg, settings.power_source);
1493 		if (ret)
1494 			return ret;
1495 	}
1496 
1497 	/* Apply drive strength. */
1498 	if (settings.drive_strength_ua != pctrl->settings[_pin].drive_strength_ua) {
1499 		enum rzg2l_iolh_index iolh_idx;
1500 		int val;
1501 
1502 		iolh_idx = rzg2l_ps_to_iolh_idx(settings.power_source);
1503 		ret = rzg2l_ds_is_supported(pctrl, cfg, iolh_idx,
1504 					    settings.drive_strength_ua);
1505 		if (!ret)
1506 			return -EINVAL;
1507 
1508 		/* Get register value for this PS/DS tuple. */
1509 		val = rzg2l_iolh_ua_to_val(hwcfg, cfg, iolh_idx, settings.drive_strength_ua);
1510 		if (val < 0)
1511 			return val;
1512 
1513 		/* Apply drive strength. */
1514 		rzg2l_rmw_pin_config(pctrl, IOLH(off), bit, IOLH_MASK, val);
1515 		pctrl->settings[_pin].drive_strength_ua = settings.drive_strength_ua;
1516 	}
1517 
1518 	return 0;
1519 }
1520 
rzg2l_pinctrl_pinconf_group_set(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * configs,unsigned int num_configs)1521 static int rzg2l_pinctrl_pinconf_group_set(struct pinctrl_dev *pctldev,
1522 					   unsigned int group,
1523 					   unsigned long *configs,
1524 					   unsigned int num_configs)
1525 {
1526 	const unsigned int *pins;
1527 	unsigned int i, npins;
1528 	int ret;
1529 
1530 	ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
1531 	if (ret)
1532 		return ret;
1533 
1534 	for (i = 0; i < npins; i++) {
1535 		ret = rzg2l_pinctrl_pinconf_set(pctldev, pins[i], configs,
1536 						num_configs);
1537 		if (ret)
1538 			return ret;
1539 	}
1540 
1541 	return 0;
1542 };
1543 
rzg2l_pinctrl_pinconf_group_get(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * config)1544 static int rzg2l_pinctrl_pinconf_group_get(struct pinctrl_dev *pctldev,
1545 					   unsigned int group,
1546 					   unsigned long *config)
1547 {
1548 	const unsigned int *pins;
1549 	unsigned int i, npins, prev_config = 0;
1550 	int ret;
1551 
1552 	ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
1553 	if (ret)
1554 		return ret;
1555 
1556 	for (i = 0; i < npins; i++) {
1557 		ret = rzg2l_pinctrl_pinconf_get(pctldev, pins[i], config);
1558 		if (ret)
1559 			return ret;
1560 
1561 		/* Check config matching between to pin  */
1562 		if (i && prev_config != *config)
1563 			return -EOPNOTSUPP;
1564 
1565 		prev_config = *config;
1566 	}
1567 
1568 	return 0;
1569 };
1570 
1571 static const struct pinctrl_ops rzg2l_pinctrl_pctlops = {
1572 	.get_groups_count = pinctrl_generic_get_group_count,
1573 	.get_group_name = pinctrl_generic_get_group_name,
1574 	.get_group_pins = pinctrl_generic_get_group_pins,
1575 	.dt_node_to_map = rzg2l_dt_node_to_map,
1576 	.dt_free_map = rzg2l_dt_free_map,
1577 };
1578 
1579 static const struct pinmux_ops rzg2l_pinctrl_pmxops = {
1580 	.get_functions_count = pinmux_generic_get_function_count,
1581 	.get_function_name = pinmux_generic_get_function_name,
1582 	.get_function_groups = pinmux_generic_get_function_groups,
1583 	.set_mux = rzg2l_pinctrl_set_mux,
1584 	.strict = true,
1585 };
1586 
1587 static const struct pinconf_ops rzg2l_pinctrl_confops = {
1588 	.is_generic = true,
1589 	.pin_config_get = rzg2l_pinctrl_pinconf_get,
1590 	.pin_config_set = rzg2l_pinctrl_pinconf_set,
1591 	.pin_config_group_set = rzg2l_pinctrl_pinconf_group_set,
1592 	.pin_config_group_get = rzg2l_pinctrl_pinconf_group_get,
1593 	.pin_config_config_dbg_show = pinconf_generic_dump_config,
1594 };
1595 
rzg2l_gpio_request(struct gpio_chip * chip,unsigned int offset)1596 static int rzg2l_gpio_request(struct gpio_chip *chip, unsigned int offset)
1597 {
1598 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
1599 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[offset];
1600 	u64 *pin_data = pin_desc->drv_data;
1601 	u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
1602 	u32 port = RZG2L_PIN_ID_TO_PORT(offset);
1603 	u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
1604 	unsigned long flags;
1605 	u8 reg8;
1606 	int ret;
1607 
1608 	ret = rzg2l_validate_gpio_pin(pctrl, *pin_data, port, bit);
1609 	if (ret)
1610 		return ret;
1611 
1612 	ret = pinctrl_gpio_request(chip, offset);
1613 	if (ret)
1614 		return ret;
1615 
1616 	spin_lock_irqsave(&pctrl->lock, flags);
1617 
1618 	/* Select GPIO mode in PMC Register */
1619 	reg8 = readb(pctrl->base + PMC(off));
1620 	reg8 &= ~BIT(bit);
1621 	pctrl->data->pmc_writeb(pctrl, reg8, PMC(off));
1622 
1623 	spin_unlock_irqrestore(&pctrl->lock, flags);
1624 
1625 	return 0;
1626 }
1627 
rzg2l_gpio_set_direction(struct rzg2l_pinctrl * pctrl,u32 offset,bool output)1628 static void rzg2l_gpio_set_direction(struct rzg2l_pinctrl *pctrl, u32 offset,
1629 				     bool output)
1630 {
1631 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[offset];
1632 	u64 *pin_data = pin_desc->drv_data;
1633 	u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
1634 	u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
1635 	unsigned long flags;
1636 	u16 reg16;
1637 
1638 	spin_lock_irqsave(&pctrl->lock, flags);
1639 
1640 	reg16 = readw(pctrl->base + PM(off));
1641 	reg16 &= ~(PM_MASK << (bit * 2));
1642 
1643 	reg16 |= (output ? PM_OUTPUT : PM_INPUT) << (bit * 2);
1644 	writew(reg16, pctrl->base + PM(off));
1645 
1646 	spin_unlock_irqrestore(&pctrl->lock, flags);
1647 }
1648 
rzg2l_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)1649 static int rzg2l_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
1650 {
1651 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
1652 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[offset];
1653 	u64 *pin_data = pin_desc->drv_data;
1654 	u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
1655 	u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
1656 
1657 	if (!(readb(pctrl->base + PMC(off)) & BIT(bit))) {
1658 		u16 reg16;
1659 
1660 		reg16 = readw(pctrl->base + PM(off));
1661 		reg16 = (reg16 >> (bit * 2)) & PM_MASK;
1662 		if (reg16 == PM_OUTPUT)
1663 			return GPIO_LINE_DIRECTION_OUT;
1664 	}
1665 
1666 	return GPIO_LINE_DIRECTION_IN;
1667 }
1668 
rzg2l_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)1669 static int rzg2l_gpio_direction_input(struct gpio_chip *chip,
1670 				      unsigned int offset)
1671 {
1672 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
1673 
1674 	rzg2l_gpio_set_direction(pctrl, offset, false);
1675 
1676 	return 0;
1677 }
1678 
rzg2l_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)1679 static void rzg2l_gpio_set(struct gpio_chip *chip, unsigned int offset,
1680 			   int value)
1681 {
1682 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
1683 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[offset];
1684 	u64 *pin_data = pin_desc->drv_data;
1685 	u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
1686 	u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
1687 	unsigned long flags;
1688 	u8 reg8;
1689 
1690 	spin_lock_irqsave(&pctrl->lock, flags);
1691 
1692 	reg8 = readb(pctrl->base + P(off));
1693 
1694 	if (value)
1695 		writeb(reg8 | BIT(bit), pctrl->base + P(off));
1696 	else
1697 		writeb(reg8 & ~BIT(bit), pctrl->base + P(off));
1698 
1699 	spin_unlock_irqrestore(&pctrl->lock, flags);
1700 }
1701 
rzg2l_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int value)1702 static int rzg2l_gpio_direction_output(struct gpio_chip *chip,
1703 				       unsigned int offset, int value)
1704 {
1705 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
1706 
1707 	rzg2l_gpio_set(chip, offset, value);
1708 	rzg2l_gpio_set_direction(pctrl, offset, true);
1709 
1710 	return 0;
1711 }
1712 
rzg2l_gpio_get(struct gpio_chip * chip,unsigned int offset)1713 static int rzg2l_gpio_get(struct gpio_chip *chip, unsigned int offset)
1714 {
1715 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
1716 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[offset];
1717 	u64 *pin_data = pin_desc->drv_data;
1718 	u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
1719 	u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
1720 	u16 reg16;
1721 
1722 	reg16 = readw(pctrl->base + PM(off));
1723 	reg16 = (reg16 >> (bit * 2)) & PM_MASK;
1724 
1725 	if (reg16 == PM_INPUT)
1726 		return !!(readb(pctrl->base + PIN(off)) & BIT(bit));
1727 	else if (reg16 == PM_OUTPUT)
1728 		return !!(readb(pctrl->base + P(off)) & BIT(bit));
1729 	else
1730 		return -EINVAL;
1731 }
1732 
rzg2l_gpio_free(struct gpio_chip * chip,unsigned int offset)1733 static void rzg2l_gpio_free(struct gpio_chip *chip, unsigned int offset)
1734 {
1735 	unsigned int virq;
1736 
1737 	pinctrl_gpio_free(chip, offset);
1738 
1739 	virq = irq_find_mapping(chip->irq.domain, offset);
1740 	if (virq)
1741 		irq_dispose_mapping(virq);
1742 
1743 	/*
1744 	 * Set the GPIO as an input to ensure that the next GPIO request won't
1745 	 * drive the GPIO pin as an output.
1746 	 */
1747 	rzg2l_gpio_direction_input(chip, offset);
1748 }
1749 
1750 static const char * const rzg2l_gpio_names[] = {
1751 	"P0_0", "P0_1", "P0_2", "P0_3", "P0_4", "P0_5", "P0_6", "P0_7",
1752 	"P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7",
1753 	"P2_0", "P2_1", "P2_2", "P2_3", "P2_4", "P2_5", "P2_6", "P2_7",
1754 	"P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7",
1755 	"P4_0", "P4_1", "P4_2", "P4_3", "P4_4", "P4_5", "P4_6", "P4_7",
1756 	"P5_0", "P5_1", "P5_2", "P5_3", "P5_4", "P5_5", "P5_6", "P5_7",
1757 	"P6_0", "P6_1", "P6_2", "P6_3", "P6_4", "P6_5", "P6_6", "P6_7",
1758 	"P7_0", "P7_1", "P7_2", "P7_3", "P7_4", "P7_5", "P7_6", "P7_7",
1759 	"P8_0", "P8_1", "P8_2", "P8_3", "P8_4", "P8_5", "P8_6", "P8_7",
1760 	"P9_0", "P9_1", "P9_2", "P9_3", "P9_4", "P9_5", "P9_6", "P9_7",
1761 	"P10_0", "P10_1", "P10_2", "P10_3", "P10_4", "P10_5", "P10_6", "P10_7",
1762 	"P11_0", "P11_1", "P11_2", "P11_3", "P11_4", "P11_5", "P11_6", "P11_7",
1763 	"P12_0", "P12_1", "P12_2", "P12_3", "P12_4", "P12_5", "P12_6", "P12_7",
1764 	"P13_0", "P13_1", "P13_2", "P13_3", "P13_4", "P13_5", "P13_6", "P13_7",
1765 	"P14_0", "P14_1", "P14_2", "P14_3", "P14_4", "P14_5", "P14_6", "P14_7",
1766 	"P15_0", "P15_1", "P15_2", "P15_3", "P15_4", "P15_5", "P15_6", "P15_7",
1767 	"P16_0", "P16_1", "P16_2", "P16_3", "P16_4", "P16_5", "P16_6", "P16_7",
1768 	"P17_0", "P17_1", "P17_2", "P17_3", "P17_4", "P17_5", "P17_6", "P17_7",
1769 	"P18_0", "P18_1", "P18_2", "P18_3", "P18_4", "P18_5", "P18_6", "P18_7",
1770 	"P19_0", "P19_1", "P19_2", "P19_3", "P19_4", "P19_5", "P19_6", "P19_7",
1771 	"P20_0", "P20_1", "P20_2", "P20_3", "P20_4", "P20_5", "P20_6", "P20_7",
1772 	"P21_0", "P21_1", "P21_2", "P21_3", "P21_4", "P21_5", "P21_6", "P21_7",
1773 	"P22_0", "P22_1", "P22_2", "P22_3", "P22_4", "P22_5", "P22_6", "P22_7",
1774 	"P23_0", "P23_1", "P23_2", "P23_3", "P23_4", "P23_5", "P23_6", "P23_7",
1775 	"P24_0", "P24_1", "P24_2", "P24_3", "P24_4", "P24_5", "P24_6", "P24_7",
1776 	"P25_0", "P25_1", "P25_2", "P25_3", "P25_4", "P25_5", "P25_6", "P25_7",
1777 	"P26_0", "P26_1", "P26_2", "P26_3", "P26_4", "P26_5", "P26_6", "P26_7",
1778 	"P27_0", "P27_1", "P27_2", "P27_3", "P27_4", "P27_5", "P27_6", "P27_7",
1779 	"P28_0", "P28_1", "P28_2", "P28_3", "P28_4", "P28_5", "P28_6", "P28_7",
1780 	"P29_0", "P29_1", "P29_2", "P29_3", "P29_4", "P29_5", "P29_6", "P29_7",
1781 	"P30_0", "P30_1", "P30_2", "P30_3", "P30_4", "P30_5", "P30_6", "P30_7",
1782 	"P31_0", "P31_1", "P31_2", "P31_3", "P31_4", "P31_5", "P31_6", "P31_7",
1783 	"P32_0", "P32_1", "P32_2", "P32_3", "P32_4", "P32_5", "P32_6", "P32_7",
1784 	"P33_0", "P33_1", "P33_2", "P33_3", "P33_4", "P33_5", "P33_6", "P33_7",
1785 	"P34_0", "P34_1", "P34_2", "P34_3", "P34_4", "P34_5", "P34_6", "P34_7",
1786 	"P35_0", "P35_1", "P35_2", "P35_3", "P35_4", "P35_5", "P35_6", "P35_7",
1787 	"P36_0", "P36_1", "P36_2", "P36_3", "P36_4", "P36_5", "P36_6", "P36_7",
1788 	"P37_0", "P37_1", "P37_2", "P37_3", "P37_4", "P37_5", "P37_6", "P37_7",
1789 	"P38_0", "P38_1", "P38_2", "P38_3", "P38_4", "P38_5", "P38_6", "P38_7",
1790 	"P39_0", "P39_1", "P39_2", "P39_3", "P39_4", "P39_5", "P39_6", "P39_7",
1791 	"P40_0", "P40_1", "P40_2", "P40_3", "P40_4", "P40_5", "P40_6", "P40_7",
1792 	"P41_0", "P41_1", "P41_2", "P41_3", "P41_4", "P41_5", "P41_6", "P41_7",
1793 	"P42_0", "P42_1", "P42_2", "P42_3", "P42_4", "P42_5", "P42_6", "P42_7",
1794 	"P43_0", "P43_1", "P43_2", "P43_3", "P43_4", "P43_5", "P43_6", "P43_7",
1795 	"P44_0", "P44_1", "P44_2", "P44_3", "P44_4", "P44_5", "P44_6", "P44_7",
1796 	"P45_0", "P45_1", "P45_2", "P45_3", "P45_4", "P45_5", "P45_6", "P45_7",
1797 	"P46_0", "P46_1", "P46_2", "P46_3", "P46_4", "P46_5", "P46_6", "P46_7",
1798 	"P47_0", "P47_1", "P47_2", "P47_3", "P47_4", "P47_5", "P47_6", "P47_7",
1799 	"P48_0", "P48_1", "P48_2", "P48_3", "P48_4", "P48_5", "P48_6", "P48_7",
1800 };
1801 
1802 static const u64 r9a07g044_gpio_configs[] = {
1803 	RZG2L_GPIO_PORT_PACK(2, 0x10, RZG2L_MPXED_PIN_FUNCS),
1804 	RZG2L_GPIO_PORT_PACK(2, 0x11, RZG2L_MPXED_PIN_FUNCS),
1805 	RZG2L_GPIO_PORT_PACK(2, 0x12, RZG2L_MPXED_PIN_FUNCS),
1806 	RZG2L_GPIO_PORT_PACK(2, 0x13, RZG2L_MPXED_PIN_FUNCS),
1807 	RZG2L_GPIO_PORT_PACK(2, 0x14, RZG2L_MPXED_PIN_FUNCS),
1808 	RZG2L_GPIO_PORT_PACK(3, 0x15, RZG2L_MPXED_PIN_FUNCS),
1809 	RZG2L_GPIO_PORT_PACK(2, 0x16, RZG2L_MPXED_PIN_FUNCS),
1810 	RZG2L_GPIO_PORT_PACK(3, 0x17, RZG2L_MPXED_PIN_FUNCS),
1811 	RZG2L_GPIO_PORT_PACK(3, 0x18, RZG2L_MPXED_PIN_FUNCS),
1812 	RZG2L_GPIO_PORT_PACK(2, 0x19, RZG2L_MPXED_PIN_FUNCS),
1813 	RZG2L_GPIO_PORT_PACK(2, 0x1a, RZG2L_MPXED_PIN_FUNCS),
1814 	RZG2L_GPIO_PORT_PACK(2, 0x1b, RZG2L_MPXED_PIN_FUNCS),
1815 	RZG2L_GPIO_PORT_PACK(2, 0x1c, RZG2L_MPXED_PIN_FUNCS),
1816 	RZG2L_GPIO_PORT_PACK(3, 0x1d, RZG2L_MPXED_PIN_FUNCS),
1817 	RZG2L_GPIO_PORT_PACK(2, 0x1e, RZG2L_MPXED_PIN_FUNCS),
1818 	RZG2L_GPIO_PORT_PACK(2, 0x1f, RZG2L_MPXED_PIN_FUNCS),
1819 	RZG2L_GPIO_PORT_PACK(2, 0x20, RZG2L_MPXED_PIN_FUNCS),
1820 	RZG2L_GPIO_PORT_PACK(3, 0x21, RZG2L_MPXED_PIN_FUNCS),
1821 	RZG2L_GPIO_PORT_PACK(2, 0x22, RZG2L_MPXED_PIN_FUNCS),
1822 	RZG2L_GPIO_PORT_PACK(2, 0x23, RZG2L_MPXED_PIN_FUNCS),
1823 	RZG2L_GPIO_PORT_PACK(3, 0x24, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0) | PIN_CFG_OEN),
1824 	RZG2L_GPIO_PORT_PACK(2, 0x25, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1825 	RZG2L_GPIO_PORT_PACK(2, 0x26, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1826 	RZG2L_GPIO_PORT_PACK(2, 0x27, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1827 	RZG2L_GPIO_PORT_PACK(2, 0x28, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1828 	RZG2L_GPIO_PORT_PACK(2, 0x29, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1829 	RZG2L_GPIO_PORT_PACK(2, 0x2a, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1830 	RZG2L_GPIO_PORT_PACK(2, 0x2b, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1831 	RZG2L_GPIO_PORT_PACK(2, 0x2c, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1832 	RZG2L_GPIO_PORT_PACK(2, 0x2d, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1) | PIN_CFG_OEN),
1833 	RZG2L_GPIO_PORT_PACK(2, 0x2e, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1834 	RZG2L_GPIO_PORT_PACK(2, 0x2f, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1835 	RZG2L_GPIO_PORT_PACK(2, 0x30, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1836 	RZG2L_GPIO_PORT_PACK(2, 0x31, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1837 	RZG2L_GPIO_PORT_PACK(2, 0x32, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1838 	RZG2L_GPIO_PORT_PACK(2, 0x33, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1839 	RZG2L_GPIO_PORT_PACK(2, 0x34, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1840 	RZG2L_GPIO_PORT_PACK(3, 0x35, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1841 	RZG2L_GPIO_PORT_PACK(2, 0x36, RZG2L_MPXED_PIN_FUNCS),
1842 	RZG2L_GPIO_PORT_PACK(3, 0x37, RZG2L_MPXED_PIN_FUNCS),
1843 	RZG2L_GPIO_PORT_PACK(3, 0x38, RZG2L_MPXED_PIN_FUNCS),
1844 	RZG2L_GPIO_PORT_PACK(2, 0x39, RZG2L_MPXED_PIN_FUNCS),
1845 	RZG2L_GPIO_PORT_PACK(5, 0x3a, RZG2L_MPXED_PIN_FUNCS),
1846 	RZG2L_GPIO_PORT_PACK(4, 0x3b, RZG2L_MPXED_PIN_FUNCS),
1847 	RZG2L_GPIO_PORT_PACK(4, 0x3c, RZG2L_MPXED_PIN_FUNCS),
1848 	RZG2L_GPIO_PORT_PACK(4, 0x3d, RZG2L_MPXED_PIN_FUNCS),
1849 	RZG2L_GPIO_PORT_PACK(4, 0x3e, RZG2L_MPXED_PIN_FUNCS),
1850 	RZG2L_GPIO_PORT_PACK(4, 0x3f, RZG2L_MPXED_PIN_FUNCS),
1851 	RZG2L_GPIO_PORT_PACK(5, 0x40, RZG2L_MPXED_PIN_FUNCS),
1852 };
1853 
1854 static const u64 r9a07g043_gpio_configs[] = {
1855 	RZG2L_GPIO_PORT_PACK(4, 0x10, RZG2L_MPXED_PIN_FUNCS),
1856 	RZG2L_GPIO_PORT_PACK(5, 0x11, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0) | PIN_CFG_OEN),
1857 	RZG2L_GPIO_PORT_PACK(4, 0x12, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1858 	RZG2L_GPIO_PORT_PACK(4, 0x13, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1859 	RZG2L_GPIO_PORT_PACK(6, 0x14, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
1860 	RZG2L_GPIO_PORT_PACK(5, 0x15, RZG2L_MPXED_PIN_FUNCS),
1861 	RZG2L_GPIO_PORT_PACK(5, 0x16, RZG2L_MPXED_PIN_FUNCS),
1862 	RZG2L_GPIO_PORT_PACK(5, 0x17, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1) | PIN_CFG_OEN),
1863 	RZG2L_GPIO_PORT_PACK(5, 0x18, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1864 	RZG2L_GPIO_PORT_PACK(4, 0x19, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1865 	RZG2L_GPIO_PORT_PACK(5, 0x1a, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
1866 	RZG2L_GPIO_PORT_PACK(4, 0x1b, RZG2L_MPXED_PIN_FUNCS),
1867 	RZG2L_GPIO_PORT_PACK(2, 0x1c, RZG2L_MPXED_PIN_FUNCS),
1868 	RZG2L_GPIO_PORT_PACK(5, 0x1d, RZG2L_MPXED_PIN_FUNCS),
1869 	RZG2L_GPIO_PORT_PACK(3, 0x1e, RZG2L_MPXED_PIN_FUNCS),
1870 	RZG2L_GPIO_PORT_PACK(4, 0x1f, RZG2L_MPXED_PIN_FUNCS),
1871 	RZG2L_GPIO_PORT_PACK(2, 0x20, RZG2L_MPXED_PIN_FUNCS),
1872 	RZG2L_GPIO_PORT_PACK(4, 0x21, RZG2L_MPXED_PIN_FUNCS),
1873 	RZG2L_GPIO_PORT_PACK(6, 0x22, RZG2L_MPXED_PIN_FUNCS),
1874 #ifdef CONFIG_RISCV
1875 	/* Below additional port pins (P19 - P28) are exclusively available on RZ/Five SoC only */
1876 	RZG2L_GPIO_PORT_SPARSE_PACK(0x2, 0x06, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
1877 				    PIN_CFG_NF | PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),	/* P19 */
1878 	RZG2L_GPIO_PORT_PACK_VARIABLE(8, 0x07),						/* P20 */
1879 	RZG2L_GPIO_PORT_SPARSE_PACK(0x2, 0x08, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
1880 				    PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),			/* P21 */
1881 	RZG2L_GPIO_PORT_PACK(4, 0x09, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_PUPD |
1882 			     PIN_CFG_IEN | PIN_CFG_NOGPIO_INT),				/* P22 */
1883 	RZG2L_GPIO_PORT_SPARSE_PACK_VARIABLE(0x3e, 0x0a),				/* P23 */
1884 	RZG2L_GPIO_PORT_PACK_VARIABLE(6, 0x0b),						/* P24 */
1885 	RZG2L_GPIO_PORT_SPARSE_PACK(0x2, 0x0c, PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_NF |
1886 				    PIN_CFG_NOGPIO_INT),				/* P25 */
1887 	0x0,										/* P26 */
1888 	0x0,										/* P27 */
1889 	RZG2L_GPIO_PORT_PACK(6, 0x0f, RZG2L_MPXED_PIN_FUNCS | PIN_CFG_NOGPIO_INT),	/* P28 */
1890 #endif
1891 };
1892 
1893 static const u64 r9a08g045_gpio_configs[] = {
1894 	RZG2L_GPIO_PORT_PACK(4, 0x20, RZG3S_MPXED_PIN_FUNCS(A)),			/* P0  */
1895 	RZG2L_GPIO_PORT_PACK(5, 0x30, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IOLH_C |
1896 								PIN_CFG_IO_VMC_ETH0)) |
1897 				      PIN_CFG_OEN | PIN_CFG_IEN,			/* P1 */
1898 	RZG2L_GPIO_PORT_PACK(4, 0x31, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IOLH_C |
1899 								PIN_CFG_IO_VMC_ETH0)),	/* P2 */
1900 	RZG2L_GPIO_PORT_PACK(4, 0x32, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IOLH_C |
1901 								PIN_CFG_IO_VMC_ETH0)),	/* P3 */
1902 	RZG2L_GPIO_PORT_PACK(6, 0x33, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IOLH_C |
1903 								PIN_CFG_IO_VMC_ETH0)),	/* P4 */
1904 	RZG2L_GPIO_PORT_PACK(5, 0x21, RZG3S_MPXED_PIN_FUNCS(A)),			/* P5  */
1905 	RZG2L_GPIO_PORT_PACK(5, 0x22, RZG3S_MPXED_PIN_FUNCS(A)),			/* P6  */
1906 	RZG2L_GPIO_PORT_PACK(5, 0x34, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IOLH_C |
1907 								PIN_CFG_IO_VMC_ETH1)) |
1908 				      PIN_CFG_OEN | PIN_CFG_IEN,			/* P7 */
1909 	RZG2L_GPIO_PORT_PACK(5, 0x35, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IOLH_C |
1910 								PIN_CFG_IO_VMC_ETH1)),	/* P8 */
1911 	RZG2L_GPIO_PORT_PACK(4, 0x36, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IOLH_C |
1912 								PIN_CFG_IO_VMC_ETH1)),	/* P9 */
1913 	RZG2L_GPIO_PORT_PACK(5, 0x37, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IOLH_C |
1914 								PIN_CFG_IO_VMC_ETH1)),	/* P10 */
1915 	RZG2L_GPIO_PORT_PACK(4, 0x23, RZG3S_MPXED_PIN_FUNCS(B) | PIN_CFG_IEN),		/* P11  */
1916 	RZG2L_GPIO_PORT_PACK(2, 0x24, RZG3S_MPXED_PIN_FUNCS(B) | PIN_CFG_IEN),		/* P12  */
1917 	RZG2L_GPIO_PORT_PACK(5, 0x25, RZG3S_MPXED_PIN_FUNCS(A)),			/* P13  */
1918 	RZG2L_GPIO_PORT_PACK(3, 0x26, RZG3S_MPXED_PIN_FUNCS(A)),			/* P14  */
1919 	RZG2L_GPIO_PORT_PACK(4, 0x27, RZG3S_MPXED_PIN_FUNCS(A)),			/* P15  */
1920 	RZG2L_GPIO_PORT_PACK(2, 0x28, RZG3S_MPXED_PIN_FUNCS(A)),			/* P16  */
1921 	RZG2L_GPIO_PORT_PACK(4, 0x29, RZG3S_MPXED_PIN_FUNCS(A)),			/* P17  */
1922 	RZG2L_GPIO_PORT_PACK(6, 0x2a, RZG3S_MPXED_PIN_FUNCS(A)),			/* P18 */
1923 };
1924 
1925 static const char * const rzv2h_gpio_names[] = {
1926 	"P00", "P01", "P02", "P03", "P04", "P05", "P06", "P07",
1927 	"P10", "P11", "P12", "P13", "P14", "P15", "P16", "P17",
1928 	"P20", "P21", "P22", "P23", "P24", "P25", "P26", "P27",
1929 	"P30", "P31", "P32", "P33", "P34", "P35", "P36", "P37",
1930 	"P40", "P41", "P42", "P43", "P44", "P45", "P46", "P47",
1931 	"P50", "P51", "P52", "P53", "P54", "P55", "P56", "P57",
1932 	"P60", "P61", "P62", "P63", "P64", "P65", "P66", "P67",
1933 	"P70", "P71", "P72", "P73", "P74", "P75", "P76", "P77",
1934 	"P80", "P81", "P82", "P83", "P84", "P85", "P86", "P87",
1935 	"P90", "P91", "P92", "P93", "P94", "P95", "P96", "P97",
1936 	"PA0", "PA1", "PA2", "PA3", "PA4", "PA5", "PA6", "PA7",
1937 	"PB0", "PB1", "PB2", "PB3", "PB4", "PB5", "PB6", "PB7",
1938 };
1939 
1940 static const u64 r9a09g057_gpio_configs[] = {
1941 	RZG2L_GPIO_PORT_PACK(8, 0x20, RZV2H_MPXED_PIN_FUNCS),	/* P0 */
1942 	RZG2L_GPIO_PORT_PACK(6, 0x21, RZV2H_MPXED_PIN_FUNCS),	/* P1 */
1943 	RZG2L_GPIO_PORT_PACK(2, 0x22, RZG2L_MPXED_COMMON_PIN_FUNCS(RZV2H) |
1944 				      PIN_CFG_NOD),		/* P2 */
1945 	RZG2L_GPIO_PORT_PACK(8, 0x23, RZV2H_MPXED_PIN_FUNCS),	/* P3 */
1946 	RZG2L_GPIO_PORT_PACK(8, 0x24, RZV2H_MPXED_PIN_FUNCS),	/* P4 */
1947 	RZG2L_GPIO_PORT_PACK(8, 0x25, RZV2H_MPXED_PIN_FUNCS),	/* P5 */
1948 	RZG2L_GPIO_PORT_PACK(8, 0x26, RZV2H_MPXED_PIN_FUNCS |
1949 				      PIN_CFG_ELC),		/* P6 */
1950 	RZG2L_GPIO_PORT_PACK(8, 0x27, RZV2H_MPXED_PIN_FUNCS),	/* P7 */
1951 	RZG2L_GPIO_PORT_PACK(8, 0x28, RZV2H_MPXED_PIN_FUNCS |
1952 				      PIN_CFG_ELC),		/* P8 */
1953 	RZG2L_GPIO_PORT_PACK(8, 0x29, RZV2H_MPXED_PIN_FUNCS),	/* P9 */
1954 	RZG2L_GPIO_PORT_PACK(8, 0x2a, RZV2H_MPXED_PIN_FUNCS),	/* PA */
1955 	RZG2L_GPIO_PORT_PACK_VARIABLE(6, 0x2b),			/* PB */
1956 };
1957 
1958 static const struct {
1959 	struct rzg2l_dedicated_configs common[35];
1960 	struct rzg2l_dedicated_configs rzg2l_pins[7];
1961 } rzg2l_dedicated_pins = {
1962 	.common = {
1963 		{ "NMI", RZG2L_SINGLE_PIN_PACK(0x1, 0, PIN_CFG_NF) },
1964 		{ "TMS/SWDIO", RZG2L_SINGLE_PIN_PACK(0x2, 0,
1965 		 (PIN_CFG_IOLH_A | PIN_CFG_SR | PIN_CFG_IEN)) },
1966 		{ "TDO", RZG2L_SINGLE_PIN_PACK(0x3, 0,
1967 		 (PIN_CFG_IOLH_A | PIN_CFG_SR | PIN_CFG_IEN)) },
1968 		{ "AUDIO_CLK1", RZG2L_SINGLE_PIN_PACK(0x4, 0, PIN_CFG_IEN) },
1969 		{ "AUDIO_CLK2", RZG2L_SINGLE_PIN_PACK(0x4, 1, PIN_CFG_IEN) },
1970 		{ "SD0_CLK", RZG2L_SINGLE_PIN_PACK(0x6, 0,
1971 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD0)) },
1972 		{ "SD0_CMD", RZG2L_SINGLE_PIN_PACK(0x6, 1,
1973 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1974 		{ "SD0_RST#", RZG2L_SINGLE_PIN_PACK(0x6, 2,
1975 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD0)) },
1976 		{ "SD0_DATA0", RZG2L_SINGLE_PIN_PACK(0x7, 0,
1977 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1978 		{ "SD0_DATA1", RZG2L_SINGLE_PIN_PACK(0x7, 1,
1979 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1980 		{ "SD0_DATA2", RZG2L_SINGLE_PIN_PACK(0x7, 2,
1981 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1982 		{ "SD0_DATA3", RZG2L_SINGLE_PIN_PACK(0x7, 3,
1983 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1984 		{ "SD0_DATA4", RZG2L_SINGLE_PIN_PACK(0x7, 4,
1985 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1986 		{ "SD0_DATA5", RZG2L_SINGLE_PIN_PACK(0x7, 5,
1987 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1988 		{ "SD0_DATA6", RZG2L_SINGLE_PIN_PACK(0x7, 6,
1989 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1990 		{ "SD0_DATA7", RZG2L_SINGLE_PIN_PACK(0x7, 7,
1991 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
1992 		{ "SD1_CLK", RZG2L_SINGLE_PIN_PACK(0x8, 0,
1993 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD1)) },
1994 		{ "SD1_CMD", RZG2L_SINGLE_PIN_PACK(0x8, 1,
1995 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
1996 		{ "SD1_DATA0", RZG2L_SINGLE_PIN_PACK(0x9, 0,
1997 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
1998 		{ "SD1_DATA1", RZG2L_SINGLE_PIN_PACK(0x9, 1,
1999 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
2000 		{ "SD1_DATA2", RZG2L_SINGLE_PIN_PACK(0x9, 2,
2001 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
2002 		{ "SD1_DATA3", RZG2L_SINGLE_PIN_PACK(0x9, 3,
2003 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
2004 		{ "QSPI0_SPCLK", RZG2L_SINGLE_PIN_PACK(0xa, 0,
2005 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2006 		{ "QSPI0_IO0", RZG2L_SINGLE_PIN_PACK(0xa, 1,
2007 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2008 		{ "QSPI0_IO1", RZG2L_SINGLE_PIN_PACK(0xa, 2,
2009 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2010 		{ "QSPI0_IO2", RZG2L_SINGLE_PIN_PACK(0xa, 3,
2011 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2012 		{ "QSPI0_IO3", RZG2L_SINGLE_PIN_PACK(0xa, 4,
2013 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2014 		{ "QSPI0_SSL", RZG2L_SINGLE_PIN_PACK(0xa, 5,
2015 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2016 		{ "QSPI_RESET#", RZG2L_SINGLE_PIN_PACK(0xc, 0,
2017 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2018 		{ "QSPI_WP#", RZG2L_SINGLE_PIN_PACK(0xc, 1,
2019 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2020 		{ "WDTOVF_PERROUT#", RZG2L_SINGLE_PIN_PACK(0xd, 0, (PIN_CFG_IOLH_A | PIN_CFG_SR)) },
2021 		{ "RIIC0_SDA", RZG2L_SINGLE_PIN_PACK(0xe, 0, PIN_CFG_IEN) },
2022 		{ "RIIC0_SCL", RZG2L_SINGLE_PIN_PACK(0xe, 1, PIN_CFG_IEN) },
2023 		{ "RIIC1_SDA", RZG2L_SINGLE_PIN_PACK(0xe, 2, PIN_CFG_IEN) },
2024 		{ "RIIC1_SCL", RZG2L_SINGLE_PIN_PACK(0xe, 3, PIN_CFG_IEN) },
2025 	},
2026 	.rzg2l_pins = {
2027 		{ "QSPI_INT#", RZG2L_SINGLE_PIN_PACK(0xc, 2, (PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2028 		{ "QSPI1_SPCLK", RZG2L_SINGLE_PIN_PACK(0xb, 0,
2029 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2030 		{ "QSPI1_IO0", RZG2L_SINGLE_PIN_PACK(0xb, 1,
2031 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2032 		{ "QSPI1_IO1", RZG2L_SINGLE_PIN_PACK(0xb, 2,
2033 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2034 		{ "QSPI1_IO2", RZG2L_SINGLE_PIN_PACK(0xb, 3,
2035 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2036 		{ "QSPI1_IO3", RZG2L_SINGLE_PIN_PACK(0xb, 4,
2037 		 (PIN_CFG_IOLH_B | PIN_CFG_SR  | PIN_CFG_IO_VMC_QSPI)) },
2038 		{ "QSPI1_SSL", RZG2L_SINGLE_PIN_PACK(0xb, 5,
2039 		 (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
2040 	}
2041 };
2042 
2043 static const struct rzg2l_dedicated_configs rzg3s_dedicated_pins[] = {
2044 	{ "NMI", RZG2L_SINGLE_PIN_PACK(0x0, 0, PIN_CFG_NF) },
2045 	{ "TMS/SWDIO", RZG2L_SINGLE_PIN_PACK(0x1, 0, (PIN_CFG_IOLH_A | PIN_CFG_IEN |
2046 						      PIN_CFG_SOFT_PS)) },
2047 	{ "TDO", RZG2L_SINGLE_PIN_PACK(0x1, 1, (PIN_CFG_IOLH_A | PIN_CFG_SOFT_PS)) },
2048 	{ "WDTOVF_PERROUT#", RZG2L_SINGLE_PIN_PACK(0x6, 0, PIN_CFG_IOLH_A | PIN_CFG_SOFT_PS) },
2049 	{ "SD0_CLK", RZG2L_SINGLE_PIN_PACK(0x10, 0, (PIN_CFG_IOLH_B | PIN_CFG_IO_VMC_SD0)) },
2050 	{ "SD0_CMD", RZG2L_SINGLE_PIN_PACK(0x10, 1, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2051 						     PIN_CFG_IO_VMC_SD0)) },
2052 	{ "SD0_RST#", RZG2L_SINGLE_PIN_PACK(0x10, 2, (PIN_CFG_IOLH_B | PIN_CFG_IO_VMC_SD0)) },
2053 	{ "SD0_DATA0", RZG2L_SINGLE_PIN_PACK(0x11, 0, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2054 						       PIN_CFG_IO_VMC_SD0)) },
2055 	{ "SD0_DATA1", RZG2L_SINGLE_PIN_PACK(0x11, 1, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2056 						       PIN_CFG_IO_VMC_SD0)) },
2057 	{ "SD0_DATA2", RZG2L_SINGLE_PIN_PACK(0x11, 2, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2058 						       PIN_CFG_IO_VMC_SD0)) },
2059 	{ "SD0_DATA3", RZG2L_SINGLE_PIN_PACK(0x11, 3, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2060 						       PIN_CFG_IO_VMC_SD0)) },
2061 	{ "SD0_DATA4", RZG2L_SINGLE_PIN_PACK(0x11, 4, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2062 						       PIN_CFG_IO_VMC_SD0)) },
2063 	{ "SD0_DATA5", RZG2L_SINGLE_PIN_PACK(0x11, 5, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2064 						       PIN_CFG_IO_VMC_SD0)) },
2065 	{ "SD0_DATA6", RZG2L_SINGLE_PIN_PACK(0x11, 6, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2066 						       PIN_CFG_IO_VMC_SD0)) },
2067 	{ "SD0_DATA7", RZG2L_SINGLE_PIN_PACK(0x11, 7, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2068 						       PIN_CFG_IO_VMC_SD0)) },
2069 	{ "SD1_CLK", RZG2L_SINGLE_PIN_PACK(0x12, 0, (PIN_CFG_IOLH_B | PIN_CFG_IO_VMC_SD1)) },
2070 	{ "SD1_CMD", RZG2L_SINGLE_PIN_PACK(0x12, 1, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2071 						     PIN_CFG_IO_VMC_SD1)) },
2072 	{ "SD1_DATA0", RZG2L_SINGLE_PIN_PACK(0x13, 0, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2073 						       PIN_CFG_IO_VMC_SD1)) },
2074 	{ "SD1_DATA1", RZG2L_SINGLE_PIN_PACK(0x13, 1, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2075 						       PIN_CFG_IO_VMC_SD1)) },
2076 	{ "SD1_DATA2", RZG2L_SINGLE_PIN_PACK(0x13, 2, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2077 						       PIN_CFG_IO_VMC_SD1)) },
2078 	{ "SD1_DATA3", RZG2L_SINGLE_PIN_PACK(0x13, 3, (PIN_CFG_IOLH_B | PIN_CFG_IEN |
2079 						       PIN_CFG_IO_VMC_SD1)) },
2080 };
2081 
2082 static struct rzg2l_dedicated_configs rzv2h_dedicated_pins[] = {
2083 	{ "NMI", RZG2L_SINGLE_PIN_PACK(0x1, 0, PIN_CFG_NF) },
2084 	{ "TMS_SWDIO", RZG2L_SINGLE_PIN_PACK(0x3, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2085 						      PIN_CFG_IEN)) },
2086 	{ "TDO", RZG2L_SINGLE_PIN_PACK(0x3, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR)) },
2087 	{ "WDTUDFCA", RZG2L_SINGLE_PIN_PACK(0x5, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2088 						     PIN_CFG_PUPD | PIN_CFG_NOD)) },
2089 	{ "WDTUDFCM", RZG2L_SINGLE_PIN_PACK(0x5, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2090 						     PIN_CFG_PUPD | PIN_CFG_NOD)) },
2091 	{ "SCIF_RXD", RZG2L_SINGLE_PIN_PACK(0x6, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2092 						     PIN_CFG_PUPD)) },
2093 	{ "SCIF_TXD", RZG2L_SINGLE_PIN_PACK(0x6, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2094 						     PIN_CFG_PUPD)) },
2095 	{ "XSPI0_CKP", RZG2L_SINGLE_PIN_PACK(0x7, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2096 						      PIN_CFG_PUPD | PIN_CFG_OEN)) },
2097 	{ "XSPI0_CKN", RZG2L_SINGLE_PIN_PACK(0x7, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2098 						      PIN_CFG_PUPD | PIN_CFG_OEN)) },
2099 	{ "XSPI0_CS0N", RZG2L_SINGLE_PIN_PACK(0x7, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2100 						       PIN_CFG_PUPD | PIN_CFG_OEN)) },
2101 	{ "XSPI0_DS", RZG2L_SINGLE_PIN_PACK(0x7, 3, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2102 						     PIN_CFG_PUPD)) },
2103 	{ "XSPI0_RESET0N", RZG2L_SINGLE_PIN_PACK(0x7, 4, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2104 							  PIN_CFG_PUPD | PIN_CFG_OEN)) },
2105 	{ "XSPI0_RSTO0N", RZG2L_SINGLE_PIN_PACK(0x7, 5, (PIN_CFG_PUPD)) },
2106 	{ "XSPI0_INT0N", RZG2L_SINGLE_PIN_PACK(0x7, 6, (PIN_CFG_PUPD)) },
2107 	{ "XSPI0_ECS0N", RZG2L_SINGLE_PIN_PACK(0x7, 7, (PIN_CFG_PUPD)) },
2108 	{ "XSPI0_IO0", RZG2L_SINGLE_PIN_PACK(0x8, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2109 						      PIN_CFG_PUPD)) },
2110 	{ "XSPI0_IO1", RZG2L_SINGLE_PIN_PACK(0x8, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2111 						      PIN_CFG_PUPD)) },
2112 	{ "XSPI0_IO2", RZG2L_SINGLE_PIN_PACK(0x8, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2113 						      PIN_CFG_PUPD)) },
2114 	{ "XSPI0_IO3", RZG2L_SINGLE_PIN_PACK(0x8, 3, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2115 						      PIN_CFG_PUPD)) },
2116 	{ "XSPI0_IO4", RZG2L_SINGLE_PIN_PACK(0x8, 4, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2117 						      PIN_CFG_PUPD)) },
2118 	{ "XSPI0_IO5", RZG2L_SINGLE_PIN_PACK(0x8, 5, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2119 						      PIN_CFG_PUPD)) },
2120 	{ "XSPI0_IO6", RZG2L_SINGLE_PIN_PACK(0x8, 6, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2121 						      PIN_CFG_PUPD)) },
2122 	{ "XSPI0_IO7", RZG2L_SINGLE_PIN_PACK(0x8, 7, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2123 						      PIN_CFG_PUPD)) },
2124 	{ "SD0CLK", RZG2L_SINGLE_PIN_PACK(0x9, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR)) },
2125 	{ "SD0CMD", RZG2L_SINGLE_PIN_PACK(0x9, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2126 						   PIN_CFG_IEN | PIN_CFG_PUPD)) },
2127 	{ "SD0RSTN", RZG2L_SINGLE_PIN_PACK(0x9, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR)) },
2128 	{ "SD0DAT0", RZG2L_SINGLE_PIN_PACK(0xa, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2129 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2130 	{ "SD0DAT1", RZG2L_SINGLE_PIN_PACK(0xa, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2131 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2132 	{ "SD0DAT2", RZG2L_SINGLE_PIN_PACK(0xa, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2133 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2134 	{ "SD0DAT3", RZG2L_SINGLE_PIN_PACK(0xa, 3, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2135 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2136 	{ "SD0DAT4", RZG2L_SINGLE_PIN_PACK(0xa, 4, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2137 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2138 	{ "SD0DAT5", RZG2L_SINGLE_PIN_PACK(0xa, 5, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2139 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2140 	{ "SD0DAT6", RZG2L_SINGLE_PIN_PACK(0xa, 6, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2141 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2142 	{ "SD0DAT7", RZG2L_SINGLE_PIN_PACK(0xa, 7, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2143 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2144 	{ "SD1CLK", RZG2L_SINGLE_PIN_PACK(0xb, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR)) },
2145 	{ "SD1CMD", RZG2L_SINGLE_PIN_PACK(0xb, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2146 						   PIN_CFG_IEN | PIN_CFG_PUPD)) },
2147 	{ "SD1DAT0", RZG2L_SINGLE_PIN_PACK(0xc, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2148 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2149 	{ "SD1DAT1", RZG2L_SINGLE_PIN_PACK(0xc, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2150 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2151 	{ "SD1DAT2", RZG2L_SINGLE_PIN_PACK(0xc, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2152 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2153 	{ "SD1DAT3", RZG2L_SINGLE_PIN_PACK(0xc, 3, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2154 						    PIN_CFG_IEN | PIN_CFG_PUPD)) },
2155 	{ "PCIE0_RSTOUTB", RZG2L_SINGLE_PIN_PACK(0xe, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR)) },
2156 	{ "PCIE1_RSTOUTB", RZG2L_SINGLE_PIN_PACK(0xe, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR)) },
2157 	{ "ET0_MDIO", RZG2L_SINGLE_PIN_PACK(0xf, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2158 						     PIN_CFG_IEN | PIN_CFG_PUPD)) },
2159 	{ "ET0_MDC", RZG2L_SINGLE_PIN_PACK(0xf, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2160 						    PIN_CFG_PUPD)) },
2161 	{ "ET0_RXCTL_RXDV", RZG2L_SINGLE_PIN_PACK(0x10, 0, (PIN_CFG_PUPD)) },
2162 	{ "ET0_TXCTL_TXEN", RZG2L_SINGLE_PIN_PACK(0x10, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2163 							    PIN_CFG_PUPD)) },
2164 	{ "ET0_TXER", RZG2L_SINGLE_PIN_PACK(0x10, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2165 						      PIN_CFG_PUPD)) },
2166 	{ "ET0_RXER", RZG2L_SINGLE_PIN_PACK(0x10, 3, (PIN_CFG_PUPD)) },
2167 	{ "ET0_RXC_RXCLK", RZG2L_SINGLE_PIN_PACK(0x10, 4, (PIN_CFG_PUPD)) },
2168 	{ "ET0_TXC_TXCLK", RZG2L_SINGLE_PIN_PACK(0x10, 5, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2169 							   PIN_CFG_PUPD | PIN_CFG_OEN)) },
2170 	{ "ET0_CRS", RZG2L_SINGLE_PIN_PACK(0x10, 6, (PIN_CFG_PUPD)) },
2171 	{ "ET0_COL", RZG2L_SINGLE_PIN_PACK(0x10, 7, (PIN_CFG_PUPD)) },
2172 	{ "ET0_TXD0", RZG2L_SINGLE_PIN_PACK(0x11, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2173 						      PIN_CFG_PUPD)) },
2174 	{ "ET0_TXD1", RZG2L_SINGLE_PIN_PACK(0x11, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2175 						      PIN_CFG_PUPD)) },
2176 	{ "ET0_TXD2", RZG2L_SINGLE_PIN_PACK(0x11, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2177 						      PIN_CFG_PUPD)) },
2178 	{ "ET0_TXD3", RZG2L_SINGLE_PIN_PACK(0x11, 3, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2179 						      PIN_CFG_PUPD)) },
2180 	{ "ET0_RXD0", RZG2L_SINGLE_PIN_PACK(0x11, 4, (PIN_CFG_PUPD)) },
2181 	{ "ET0_RXD1", RZG2L_SINGLE_PIN_PACK(0x11, 5, (PIN_CFG_PUPD)) },
2182 	{ "ET0_RXD2", RZG2L_SINGLE_PIN_PACK(0x11, 6, (PIN_CFG_PUPD)) },
2183 	{ "ET0_RXD3", RZG2L_SINGLE_PIN_PACK(0x11, 7, (PIN_CFG_PUPD)) },
2184 	{ "ET1_MDIO", RZG2L_SINGLE_PIN_PACK(0x12, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2185 						      PIN_CFG_IEN | PIN_CFG_PUPD)) },
2186 	{ "ET1_MDC", RZG2L_SINGLE_PIN_PACK(0x12, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2187 						     PIN_CFG_PUPD)) },
2188 	{ "ET1_RXCTL_RXDV", RZG2L_SINGLE_PIN_PACK(0x13, 0, (PIN_CFG_PUPD)) },
2189 	{ "ET1_TXCTL_TXEN", RZG2L_SINGLE_PIN_PACK(0x13, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2190 							    PIN_CFG_PUPD)) },
2191 	{ "ET1_TXER", RZG2L_SINGLE_PIN_PACK(0x13, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2192 						       PIN_CFG_PUPD)) },
2193 	{ "ET1_RXER", RZG2L_SINGLE_PIN_PACK(0x13, 3, (PIN_CFG_PUPD)) },
2194 	{ "ET1_RXC_RXCLK", RZG2L_SINGLE_PIN_PACK(0x13, 4, (PIN_CFG_PUPD)) },
2195 	{ "ET1_TXC_TXCLK", RZG2L_SINGLE_PIN_PACK(0x13, 5, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2196 							   PIN_CFG_PUPD | PIN_CFG_OEN)) },
2197 	{ "ET1_CRS", RZG2L_SINGLE_PIN_PACK(0x13, 6, (PIN_CFG_PUPD)) },
2198 	{ "ET1_COL", RZG2L_SINGLE_PIN_PACK(0x13, 7, (PIN_CFG_PUPD)) },
2199 	{ "ET1_TXD0", RZG2L_SINGLE_PIN_PACK(0x14, 0, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2200 						      PIN_CFG_PUPD)) },
2201 	{ "ET1_TXD1", RZG2L_SINGLE_PIN_PACK(0x14, 1, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2202 						      PIN_CFG_PUPD)) },
2203 	{ "ET1_TXD2", RZG2L_SINGLE_PIN_PACK(0x14, 2, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2204 						      PIN_CFG_PUPD)) },
2205 	{ "ET1_TXD3", RZG2L_SINGLE_PIN_PACK(0x14, 3, (PIN_CFG_IOLH_RZV2H | PIN_CFG_SR |
2206 						      PIN_CFG_PUPD)) },
2207 	{ "ET1_RXD0", RZG2L_SINGLE_PIN_PACK(0x14, 4, (PIN_CFG_PUPD)) },
2208 	{ "ET1_RXD1", RZG2L_SINGLE_PIN_PACK(0x14, 5, (PIN_CFG_PUPD)) },
2209 	{ "ET1_RXD2", RZG2L_SINGLE_PIN_PACK(0x14, 6, (PIN_CFG_PUPD)) },
2210 	{ "ET1_RXD3", RZG2L_SINGLE_PIN_PACK(0x14, 7, (PIN_CFG_PUPD)) },
2211 };
2212 
rzg2l_gpio_get_gpioint(unsigned int virq,struct rzg2l_pinctrl * pctrl)2213 static int rzg2l_gpio_get_gpioint(unsigned int virq, struct rzg2l_pinctrl *pctrl)
2214 {
2215 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[virq];
2216 	const struct rzg2l_pinctrl_data *data = pctrl->data;
2217 	u64 *pin_data = pin_desc->drv_data;
2218 	unsigned int gpioint;
2219 	unsigned int i;
2220 	u32 port, bit;
2221 
2222 	if (*pin_data & PIN_CFG_NOGPIO_INT)
2223 		return -EINVAL;
2224 
2225 	port = virq / 8;
2226 	bit = virq % 8;
2227 
2228 	if (port >= data->n_ports ||
2229 	    bit >= hweight8(FIELD_GET(PIN_CFG_PIN_MAP_MASK, data->port_pin_configs[port])))
2230 		return -EINVAL;
2231 
2232 	gpioint = bit;
2233 	for (i = 0; i < port; i++)
2234 		gpioint += hweight8(FIELD_GET(PIN_CFG_PIN_MAP_MASK, data->port_pin_configs[i]));
2235 
2236 	return gpioint;
2237 }
2238 
rzg2l_gpio_irq_endisable(struct rzg2l_pinctrl * pctrl,unsigned int hwirq,bool enable)2239 static void rzg2l_gpio_irq_endisable(struct rzg2l_pinctrl *pctrl,
2240 				     unsigned int hwirq, bool enable)
2241 {
2242 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[hwirq];
2243 	u64 *pin_data = pin_desc->drv_data;
2244 	u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
2245 	u8 bit = RZG2L_PIN_ID_TO_PIN(hwirq);
2246 	unsigned long flags;
2247 	void __iomem *addr;
2248 
2249 	addr = pctrl->base + ISEL(off);
2250 	if (bit >= 4) {
2251 		bit -= 4;
2252 		addr += 4;
2253 	}
2254 
2255 	spin_lock_irqsave(&pctrl->lock, flags);
2256 	if (enable)
2257 		writel(readl(addr) | BIT(bit * 8), addr);
2258 	else
2259 		writel(readl(addr) & ~BIT(bit * 8), addr);
2260 	spin_unlock_irqrestore(&pctrl->lock, flags);
2261 }
2262 
rzg2l_gpio_irq_disable(struct irq_data * d)2263 static void rzg2l_gpio_irq_disable(struct irq_data *d)
2264 {
2265 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
2266 	unsigned int hwirq = irqd_to_hwirq(d);
2267 
2268 	irq_chip_disable_parent(d);
2269 	gpiochip_disable_irq(gc, hwirq);
2270 }
2271 
rzg2l_gpio_irq_enable(struct irq_data * d)2272 static void rzg2l_gpio_irq_enable(struct irq_data *d)
2273 {
2274 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
2275 	unsigned int hwirq = irqd_to_hwirq(d);
2276 
2277 	gpiochip_enable_irq(gc, hwirq);
2278 	irq_chip_enable_parent(d);
2279 }
2280 
rzg2l_gpio_irq_set_type(struct irq_data * d,unsigned int type)2281 static int rzg2l_gpio_irq_set_type(struct irq_data *d, unsigned int type)
2282 {
2283 	return irq_chip_set_type_parent(d, type);
2284 }
2285 
rzg2l_gpio_irqc_eoi(struct irq_data * d)2286 static void rzg2l_gpio_irqc_eoi(struct irq_data *d)
2287 {
2288 	irq_chip_eoi_parent(d);
2289 }
2290 
rzg2l_gpio_irq_print_chip(struct irq_data * data,struct seq_file * p)2291 static void rzg2l_gpio_irq_print_chip(struct irq_data *data, struct seq_file *p)
2292 {
2293 	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
2294 
2295 	seq_printf(p, dev_name(gc->parent));
2296 }
2297 
rzg2l_gpio_irq_set_wake(struct irq_data * data,unsigned int on)2298 static int rzg2l_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
2299 {
2300 	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
2301 	struct rzg2l_pinctrl *pctrl = container_of(gc, struct rzg2l_pinctrl, gpio_chip);
2302 	int ret;
2303 
2304 	/* It should not happen. */
2305 	if (!data->parent_data)
2306 		return -EOPNOTSUPP;
2307 
2308 	ret = irq_chip_set_wake_parent(data, on);
2309 	if (ret)
2310 		return ret;
2311 
2312 	if (on)
2313 		atomic_inc(&pctrl->wakeup_path);
2314 	else
2315 		atomic_dec(&pctrl->wakeup_path);
2316 
2317 	return 0;
2318 }
2319 
2320 static const struct irq_chip rzg2l_gpio_irqchip = {
2321 	.name = "rzg2l-gpio",
2322 	.irq_disable = rzg2l_gpio_irq_disable,
2323 	.irq_enable = rzg2l_gpio_irq_enable,
2324 	.irq_mask = irq_chip_mask_parent,
2325 	.irq_unmask = irq_chip_unmask_parent,
2326 	.irq_set_type = rzg2l_gpio_irq_set_type,
2327 	.irq_eoi = rzg2l_gpio_irqc_eoi,
2328 	.irq_print_chip = rzg2l_gpio_irq_print_chip,
2329 	.irq_set_affinity = irq_chip_set_affinity_parent,
2330 	.irq_set_wake = rzg2l_gpio_irq_set_wake,
2331 	.flags = IRQCHIP_IMMUTABLE,
2332 	GPIOCHIP_IRQ_RESOURCE_HELPERS,
2333 };
2334 
rzg2l_gpio_interrupt_input_mode(struct gpio_chip * chip,unsigned int offset)2335 static int rzg2l_gpio_interrupt_input_mode(struct gpio_chip *chip, unsigned int offset)
2336 {
2337 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
2338 	const struct pinctrl_pin_desc *pin_desc = &pctrl->desc.pins[offset];
2339 	u64 *pin_data = pin_desc->drv_data;
2340 	u32 off = RZG2L_PIN_CFG_TO_PORT_OFFSET(*pin_data);
2341 	u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
2342 	u8 reg8;
2343 	int ret;
2344 
2345 	reg8 = readb(pctrl->base + PMC(off));
2346 	if (reg8 & BIT(bit)) {
2347 		ret = rzg2l_gpio_request(chip, offset);
2348 		if (ret)
2349 			return ret;
2350 	}
2351 
2352 	return rzg2l_gpio_direction_input(chip, offset);
2353 }
2354 
rzg2l_gpio_child_to_parent_hwirq(struct gpio_chip * gc,unsigned int child,unsigned int child_type,unsigned int * parent,unsigned int * parent_type)2355 static int rzg2l_gpio_child_to_parent_hwirq(struct gpio_chip *gc,
2356 					    unsigned int child,
2357 					    unsigned int child_type,
2358 					    unsigned int *parent,
2359 					    unsigned int *parent_type)
2360 {
2361 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(gc);
2362 	unsigned long flags;
2363 	int gpioint, irq;
2364 	int ret;
2365 
2366 	gpioint = rzg2l_gpio_get_gpioint(child, pctrl);
2367 	if (gpioint < 0)
2368 		return gpioint;
2369 
2370 	ret = rzg2l_gpio_interrupt_input_mode(gc, child);
2371 	if (ret)
2372 		return ret;
2373 
2374 	spin_lock_irqsave(&pctrl->bitmap_lock, flags);
2375 	irq = bitmap_find_free_region(pctrl->tint_slot, RZG2L_TINT_MAX_INTERRUPT, get_order(1));
2376 	spin_unlock_irqrestore(&pctrl->bitmap_lock, flags);
2377 	if (irq < 0) {
2378 		ret = -ENOSPC;
2379 		goto err;
2380 	}
2381 
2382 	rzg2l_gpio_irq_endisable(pctrl, child, true);
2383 	pctrl->hwirq[irq] = child;
2384 	irq += RZG2L_TINT_IRQ_START_INDEX;
2385 
2386 	/* All these interrupts are level high in the CPU */
2387 	*parent_type = IRQ_TYPE_LEVEL_HIGH;
2388 	*parent = RZG2L_PACK_HWIRQ(gpioint, irq);
2389 	return 0;
2390 
2391 err:
2392 	rzg2l_gpio_free(gc, child);
2393 	return ret;
2394 }
2395 
rzg2l_gpio_populate_parent_fwspec(struct gpio_chip * chip,union gpio_irq_fwspec * gfwspec,unsigned int parent_hwirq,unsigned int parent_type)2396 static int rzg2l_gpio_populate_parent_fwspec(struct gpio_chip *chip,
2397 					     union gpio_irq_fwspec *gfwspec,
2398 					     unsigned int parent_hwirq,
2399 					     unsigned int parent_type)
2400 {
2401 	struct irq_fwspec *fwspec = &gfwspec->fwspec;
2402 
2403 	fwspec->fwnode = chip->irq.parent_domain->fwnode;
2404 	fwspec->param_count = 2;
2405 	fwspec->param[0] = parent_hwirq;
2406 	fwspec->param[1] = parent_type;
2407 
2408 	return 0;
2409 }
2410 
rzg2l_gpio_irq_restore(struct rzg2l_pinctrl * pctrl)2411 static void rzg2l_gpio_irq_restore(struct rzg2l_pinctrl *pctrl)
2412 {
2413 	struct irq_domain *domain = pctrl->gpio_chip.irq.domain;
2414 
2415 	for (unsigned int i = 0; i < RZG2L_TINT_MAX_INTERRUPT; i++) {
2416 		struct irq_data *data;
2417 		unsigned long flags;
2418 		unsigned int virq;
2419 		int ret;
2420 
2421 		if (!pctrl->hwirq[i])
2422 			continue;
2423 
2424 		virq = irq_find_mapping(domain, pctrl->hwirq[i]);
2425 		if (!virq) {
2426 			dev_crit(pctrl->dev, "Failed to find IRQ mapping for hwirq %u\n",
2427 				 pctrl->hwirq[i]);
2428 			continue;
2429 		}
2430 
2431 		data = irq_domain_get_irq_data(domain, virq);
2432 		if (!data) {
2433 			dev_crit(pctrl->dev, "Failed to get IRQ data for virq=%u\n", virq);
2434 			continue;
2435 		}
2436 
2437 		/*
2438 		 * This has to be atomically executed to protect against a concurrent
2439 		 * interrupt.
2440 		 */
2441 		spin_lock_irqsave(&pctrl->lock, flags);
2442 		ret = rzg2l_gpio_irq_set_type(data, irqd_get_trigger_type(data));
2443 		if (!ret && !irqd_irq_disabled(data))
2444 			rzg2l_gpio_irq_enable(data);
2445 		spin_unlock_irqrestore(&pctrl->lock, flags);
2446 
2447 		if (ret)
2448 			dev_crit(pctrl->dev, "Failed to set IRQ type for virq=%u\n", virq);
2449 	}
2450 }
2451 
rzg2l_gpio_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)2452 static void rzg2l_gpio_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2453 				       unsigned int nr_irqs)
2454 {
2455 	struct irq_data *d;
2456 
2457 	d = irq_domain_get_irq_data(domain, virq);
2458 	if (d) {
2459 		struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
2460 		struct rzg2l_pinctrl *pctrl = container_of(gc, struct rzg2l_pinctrl, gpio_chip);
2461 		irq_hw_number_t hwirq = irqd_to_hwirq(d);
2462 		unsigned long flags;
2463 		unsigned int i;
2464 
2465 		for (i = 0; i < RZG2L_TINT_MAX_INTERRUPT; i++) {
2466 			if (pctrl->hwirq[i] == hwirq) {
2467 				rzg2l_gpio_irq_endisable(pctrl, hwirq, false);
2468 				rzg2l_gpio_free(gc, hwirq);
2469 				spin_lock_irqsave(&pctrl->bitmap_lock, flags);
2470 				bitmap_release_region(pctrl->tint_slot, i, get_order(1));
2471 				spin_unlock_irqrestore(&pctrl->bitmap_lock, flags);
2472 				pctrl->hwirq[i] = 0;
2473 				break;
2474 			}
2475 		}
2476 	}
2477 	irq_domain_free_irqs_common(domain, virq, nr_irqs);
2478 }
2479 
rzg2l_init_irq_valid_mask(struct gpio_chip * gc,unsigned long * valid_mask,unsigned int ngpios)2480 static void rzg2l_init_irq_valid_mask(struct gpio_chip *gc,
2481 				      unsigned long *valid_mask,
2482 				      unsigned int ngpios)
2483 {
2484 	struct rzg2l_pinctrl *pctrl = gpiochip_get_data(gc);
2485 	struct gpio_chip *chip = &pctrl->gpio_chip;
2486 	unsigned int offset;
2487 
2488 	/* Forbid unused lines to be mapped as IRQs */
2489 	for (offset = 0; offset < chip->ngpio; offset++) {
2490 		u32 port, bit;
2491 
2492 		port = offset / 8;
2493 		bit = offset % 8;
2494 
2495 		if (port >= pctrl->data->n_ports ||
2496 		    bit >= hweight8(FIELD_GET(PIN_CFG_PIN_MAP_MASK,
2497 					      pctrl->data->port_pin_configs[port])))
2498 			clear_bit(offset, valid_mask);
2499 	}
2500 }
2501 
rzg2l_pinctrl_reg_cache_alloc(struct rzg2l_pinctrl * pctrl)2502 static int rzg2l_pinctrl_reg_cache_alloc(struct rzg2l_pinctrl *pctrl)
2503 {
2504 	u32 nports = pctrl->data->n_port_pins / RZG2L_PINS_PER_PORT;
2505 	struct rzg2l_pinctrl_reg_cache *cache, *dedicated_cache;
2506 
2507 	cache = devm_kzalloc(pctrl->dev, sizeof(*cache), GFP_KERNEL);
2508 	if (!cache)
2509 		return -ENOMEM;
2510 
2511 	dedicated_cache = devm_kzalloc(pctrl->dev, sizeof(*dedicated_cache), GFP_KERNEL);
2512 	if (!dedicated_cache)
2513 		return -ENOMEM;
2514 
2515 	cache->p = devm_kcalloc(pctrl->dev, nports, sizeof(*cache->p), GFP_KERNEL);
2516 	if (!cache->p)
2517 		return -ENOMEM;
2518 
2519 	cache->pm = devm_kcalloc(pctrl->dev, nports, sizeof(*cache->pm), GFP_KERNEL);
2520 	if (!cache->pm)
2521 		return -ENOMEM;
2522 
2523 	cache->pmc = devm_kcalloc(pctrl->dev, nports, sizeof(*cache->pmc), GFP_KERNEL);
2524 	if (!cache->pmc)
2525 		return -ENOMEM;
2526 
2527 	cache->pfc = devm_kcalloc(pctrl->dev, nports, sizeof(*cache->pfc), GFP_KERNEL);
2528 	if (!cache->pfc)
2529 		return -ENOMEM;
2530 
2531 	for (u8 i = 0; i < 2; i++) {
2532 		u32 n_dedicated_pins = pctrl->data->n_dedicated_pins;
2533 
2534 		cache->iolh[i] = devm_kcalloc(pctrl->dev, nports, sizeof(*cache->iolh[i]),
2535 					      GFP_KERNEL);
2536 		if (!cache->iolh[i])
2537 			return -ENOMEM;
2538 
2539 		cache->ien[i] = devm_kcalloc(pctrl->dev, nports, sizeof(*cache->ien[i]),
2540 					     GFP_KERNEL);
2541 		if (!cache->ien[i])
2542 			return -ENOMEM;
2543 
2544 		cache->pupd[i] = devm_kcalloc(pctrl->dev, nports, sizeof(*cache->pupd[i]),
2545 					      GFP_KERNEL);
2546 		if (!cache->pupd[i])
2547 			return -ENOMEM;
2548 
2549 		/* Allocate dedicated cache. */
2550 		dedicated_cache->iolh[i] = devm_kcalloc(pctrl->dev, n_dedicated_pins,
2551 							sizeof(*dedicated_cache->iolh[i]),
2552 							GFP_KERNEL);
2553 		if (!dedicated_cache->iolh[i])
2554 			return -ENOMEM;
2555 
2556 		dedicated_cache->ien[i] = devm_kcalloc(pctrl->dev, n_dedicated_pins,
2557 						       sizeof(*dedicated_cache->ien[i]),
2558 						       GFP_KERNEL);
2559 		if (!dedicated_cache->ien[i])
2560 			return -ENOMEM;
2561 	}
2562 
2563 	pctrl->cache = cache;
2564 	pctrl->dedicated_cache = dedicated_cache;
2565 
2566 	return 0;
2567 }
2568 
rzg2l_gpio_register(struct rzg2l_pinctrl * pctrl)2569 static int rzg2l_gpio_register(struct rzg2l_pinctrl *pctrl)
2570 {
2571 	struct device_node *np = pctrl->dev->of_node;
2572 	struct gpio_chip *chip = &pctrl->gpio_chip;
2573 	const char *name = dev_name(pctrl->dev);
2574 	struct irq_domain *parent_domain;
2575 	struct of_phandle_args of_args;
2576 	struct device_node *parent_np;
2577 	struct gpio_irq_chip *girq;
2578 	int ret;
2579 
2580 	parent_np = of_irq_find_parent(np);
2581 	if (!parent_np)
2582 		return -ENXIO;
2583 
2584 	parent_domain = irq_find_host(parent_np);
2585 	of_node_put(parent_np);
2586 	if (!parent_domain)
2587 		return -EPROBE_DEFER;
2588 
2589 	ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &of_args);
2590 	if (ret)
2591 		return dev_err_probe(pctrl->dev, ret, "Unable to parse gpio-ranges\n");
2592 
2593 	of_node_put(of_args.np);
2594 
2595 	if (of_args.args[0] != 0 || of_args.args[1] != 0 ||
2596 	    of_args.args[2] != pctrl->data->n_port_pins)
2597 		return dev_err_probe(pctrl->dev, -EINVAL,
2598 				     "gpio-ranges does not match selected SOC\n");
2599 
2600 	chip->names = pctrl->data->port_pins;
2601 	chip->request = rzg2l_gpio_request;
2602 	chip->free = rzg2l_gpio_free;
2603 	chip->get_direction = rzg2l_gpio_get_direction;
2604 	chip->direction_input = rzg2l_gpio_direction_input;
2605 	chip->direction_output = rzg2l_gpio_direction_output;
2606 	chip->get = rzg2l_gpio_get;
2607 	chip->set = rzg2l_gpio_set;
2608 	chip->label = name;
2609 	chip->parent = pctrl->dev;
2610 	chip->owner = THIS_MODULE;
2611 	chip->base = -1;
2612 	chip->ngpio = of_args.args[2];
2613 
2614 	girq = &chip->irq;
2615 	gpio_irq_chip_set_chip(girq, &rzg2l_gpio_irqchip);
2616 	girq->fwnode = dev_fwnode(pctrl->dev);
2617 	girq->parent_domain = parent_domain;
2618 	girq->child_to_parent_hwirq = rzg2l_gpio_child_to_parent_hwirq;
2619 	girq->populate_parent_alloc_arg = rzg2l_gpio_populate_parent_fwspec;
2620 	girq->child_irq_domain_ops.free = rzg2l_gpio_irq_domain_free;
2621 	girq->init_valid_mask = rzg2l_init_irq_valid_mask;
2622 
2623 	pctrl->gpio_range.id = 0;
2624 	pctrl->gpio_range.pin_base = 0;
2625 	pctrl->gpio_range.base = 0;
2626 	pctrl->gpio_range.npins = chip->ngpio;
2627 	pctrl->gpio_range.name = chip->label;
2628 	pctrl->gpio_range.gc = chip;
2629 	ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl);
2630 	if (ret)
2631 		return dev_err_probe(pctrl->dev, ret, "failed to add GPIO controller\n");
2632 
2633 	dev_dbg(pctrl->dev, "Registered gpio controller\n");
2634 
2635 	return 0;
2636 }
2637 
rzg2l_pinctrl_register(struct rzg2l_pinctrl * pctrl)2638 static int rzg2l_pinctrl_register(struct rzg2l_pinctrl *pctrl)
2639 {
2640 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
2641 	struct pinctrl_pin_desc *pins;
2642 	unsigned int i, j;
2643 	u64 *pin_data;
2644 	int ret;
2645 
2646 	pctrl->desc.name = DRV_NAME;
2647 	pctrl->desc.npins = pctrl->data->n_port_pins + pctrl->data->n_dedicated_pins;
2648 	pctrl->desc.pctlops = &rzg2l_pinctrl_pctlops;
2649 	pctrl->desc.pmxops = &rzg2l_pinctrl_pmxops;
2650 	pctrl->desc.confops = &rzg2l_pinctrl_confops;
2651 	pctrl->desc.owner = THIS_MODULE;
2652 	if (pctrl->data->num_custom_params) {
2653 		pctrl->desc.num_custom_params = pctrl->data->num_custom_params;
2654 		pctrl->desc.custom_params = pctrl->data->custom_params;
2655 #ifdef CONFIG_DEBUG_FS
2656 		pctrl->desc.custom_conf_items = pctrl->data->custom_conf_items;
2657 #endif
2658 	}
2659 
2660 	pins = devm_kcalloc(pctrl->dev, pctrl->desc.npins, sizeof(*pins), GFP_KERNEL);
2661 	if (!pins)
2662 		return -ENOMEM;
2663 
2664 	pin_data = devm_kcalloc(pctrl->dev, pctrl->desc.npins,
2665 				sizeof(*pin_data), GFP_KERNEL);
2666 	if (!pin_data)
2667 		return -ENOMEM;
2668 
2669 	pctrl->pins = pins;
2670 	pctrl->desc.pins = pins;
2671 
2672 	for (i = 0, j = 0; i < pctrl->data->n_port_pins; i++) {
2673 		pins[i].number = i;
2674 		pins[i].name = pctrl->data->port_pins[i];
2675 		if (i && !(i % RZG2L_PINS_PER_PORT))
2676 			j++;
2677 		pin_data[i] = pctrl->data->port_pin_configs[j];
2678 		if (pin_data[i] & RZG2L_VARIABLE_CFG)
2679 			pin_data[i] = rzg2l_pinctrl_get_variable_pin_cfg(pctrl,
2680 									 pin_data[i],
2681 									 j,
2682 									 i % RZG2L_PINS_PER_PORT);
2683 		pins[i].drv_data = &pin_data[i];
2684 	}
2685 
2686 	for (i = 0; i < pctrl->data->n_dedicated_pins; i++) {
2687 		unsigned int index = pctrl->data->n_port_pins + i;
2688 
2689 		pins[index].number = index;
2690 		pins[index].name = pctrl->data->dedicated_pins[i].name;
2691 		pin_data[index] = pctrl->data->dedicated_pins[i].config;
2692 		pins[index].drv_data = &pin_data[index];
2693 	}
2694 
2695 	pctrl->settings = devm_kcalloc(pctrl->dev, pctrl->desc.npins, sizeof(*pctrl->settings),
2696 				       GFP_KERNEL);
2697 	if (!pctrl->settings)
2698 		return -ENOMEM;
2699 
2700 	for (i = 0; hwcfg->drive_strength_ua && i < pctrl->desc.npins; i++) {
2701 		if (pin_data[i] & PIN_CFG_SOFT_PS) {
2702 			pctrl->settings[i].power_source = 3300;
2703 		} else {
2704 			ret = rzg2l_get_power_source(pctrl, i, pin_data[i]);
2705 			if (ret < 0)
2706 				continue;
2707 			pctrl->settings[i].power_source = ret;
2708 		}
2709 	}
2710 
2711 	ret = rzg2l_pinctrl_reg_cache_alloc(pctrl);
2712 	if (ret)
2713 		return ret;
2714 
2715 	ret = devm_pinctrl_register_and_init(pctrl->dev, &pctrl->desc, pctrl,
2716 					     &pctrl->pctl);
2717 	if (ret)
2718 		return dev_err_probe(pctrl->dev, ret, "pinctrl registration failed\n");
2719 
2720 	ret = pinctrl_enable(pctrl->pctl);
2721 	if (ret)
2722 		return dev_err_probe(pctrl->dev, ret, "pinctrl enable failed\n");
2723 
2724 	ret = rzg2l_gpio_register(pctrl);
2725 	if (ret)
2726 		return dev_err_probe(pctrl->dev, ret, "failed to add GPIO chip\n");
2727 
2728 	return 0;
2729 }
2730 
rzg2l_pinctrl_probe(struct platform_device * pdev)2731 static int rzg2l_pinctrl_probe(struct platform_device *pdev)
2732 {
2733 	struct rzg2l_pinctrl *pctrl;
2734 	int ret;
2735 
2736 	BUILD_BUG_ON(ARRAY_SIZE(r9a07g044_gpio_configs) * RZG2L_PINS_PER_PORT >
2737 		     ARRAY_SIZE(rzg2l_gpio_names));
2738 
2739 	BUILD_BUG_ON(ARRAY_SIZE(r9a07g043_gpio_configs) * RZG2L_PINS_PER_PORT >
2740 		     ARRAY_SIZE(rzg2l_gpio_names));
2741 
2742 	BUILD_BUG_ON(ARRAY_SIZE(r9a08g045_gpio_configs) * RZG2L_PINS_PER_PORT >
2743 		     ARRAY_SIZE(rzg2l_gpio_names));
2744 
2745 	BUILD_BUG_ON(ARRAY_SIZE(r9a09g057_gpio_configs) * RZG2L_PINS_PER_PORT >
2746 		     ARRAY_SIZE(rzv2h_gpio_names));
2747 
2748 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
2749 	if (!pctrl)
2750 		return -ENOMEM;
2751 
2752 	pctrl->dev = &pdev->dev;
2753 
2754 	pctrl->data = of_device_get_match_data(&pdev->dev);
2755 	if (!pctrl->data)
2756 		return -EINVAL;
2757 
2758 	pctrl->base = devm_platform_ioremap_resource(pdev, 0);
2759 	if (IS_ERR(pctrl->base))
2760 		return PTR_ERR(pctrl->base);
2761 
2762 	pctrl->clk = devm_clk_get_enabled(pctrl->dev, NULL);
2763 	if (IS_ERR(pctrl->clk)) {
2764 		return dev_err_probe(pctrl->dev, PTR_ERR(pctrl->clk),
2765 				     "failed to enable GPIO clk\n");
2766 	}
2767 
2768 	spin_lock_init(&pctrl->lock);
2769 	spin_lock_init(&pctrl->bitmap_lock);
2770 	mutex_init(&pctrl->mutex);
2771 	atomic_set(&pctrl->wakeup_path, 0);
2772 
2773 	platform_set_drvdata(pdev, pctrl);
2774 
2775 	ret = rzg2l_pinctrl_register(pctrl);
2776 	if (ret)
2777 		return ret;
2778 
2779 	dev_info(pctrl->dev, "%s support registered\n", DRV_NAME);
2780 	return 0;
2781 }
2782 
rzg2l_pinctrl_pm_setup_regs(struct rzg2l_pinctrl * pctrl,bool suspend)2783 static void rzg2l_pinctrl_pm_setup_regs(struct rzg2l_pinctrl *pctrl, bool suspend)
2784 {
2785 	u32 nports = pctrl->data->n_port_pins / RZG2L_PINS_PER_PORT;
2786 	struct rzg2l_pinctrl_reg_cache *cache = pctrl->cache;
2787 
2788 	for (u32 port = 0; port < nports; port++) {
2789 		bool has_iolh, has_ien, has_pupd;
2790 		u32 off, caps;
2791 		u8 pincnt;
2792 		u64 cfg;
2793 
2794 		cfg = pctrl->data->port_pin_configs[port];
2795 		off = RZG2L_PIN_CFG_TO_PORT_OFFSET(cfg);
2796 		pincnt = hweight8(FIELD_GET(PIN_CFG_PIN_MAP_MASK, cfg));
2797 
2798 		caps = FIELD_GET(PIN_CFG_MASK, cfg);
2799 		has_iolh = !!(caps & (PIN_CFG_IOLH_A | PIN_CFG_IOLH_B | PIN_CFG_IOLH_C));
2800 		has_ien = !!(caps & PIN_CFG_IEN);
2801 		has_pupd = !!(caps & PIN_CFG_PUPD);
2802 
2803 		if (suspend)
2804 			RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + PFC(off), cache->pfc[port]);
2805 
2806 		/*
2807 		 * Now cache the registers or set them in the order suggested by
2808 		 * HW manual (section "Operation for GPIO Function").
2809 		 */
2810 		RZG2L_PCTRL_REG_ACCESS8(suspend, pctrl->base + PMC(off), cache->pmc[port]);
2811 		if (has_iolh) {
2812 			RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + IOLH(off),
2813 						 cache->iolh[0][port]);
2814 			if (pincnt >= 4) {
2815 				RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + IOLH(off) + 4,
2816 							 cache->iolh[1][port]);
2817 			}
2818 		}
2819 
2820 		if (has_pupd) {
2821 			RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + PUPD(off),
2822 						 cache->pupd[0][port]);
2823 			if (pincnt >= 4) {
2824 				RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + PUPD(off),
2825 							 cache->pupd[1][port]);
2826 			}
2827 		}
2828 
2829 		RZG2L_PCTRL_REG_ACCESS16(suspend, pctrl->base + PM(off), cache->pm[port]);
2830 		RZG2L_PCTRL_REG_ACCESS8(suspend, pctrl->base + P(off), cache->p[port]);
2831 
2832 		if (has_ien) {
2833 			RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + IEN(off),
2834 						 cache->ien[0][port]);
2835 			if (pincnt >= 4) {
2836 				RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + IEN(off) + 4,
2837 							 cache->ien[1][port]);
2838 			}
2839 		}
2840 	}
2841 }
2842 
rzg2l_pinctrl_pm_setup_dedicated_regs(struct rzg2l_pinctrl * pctrl,bool suspend)2843 static void rzg2l_pinctrl_pm_setup_dedicated_regs(struct rzg2l_pinctrl *pctrl, bool suspend)
2844 {
2845 	struct rzg2l_pinctrl_reg_cache *cache = pctrl->dedicated_cache;
2846 	u32 caps;
2847 	u32 i;
2848 
2849 	/*
2850 	 * Make sure entries in pctrl->data->n_dedicated_pins[] having the same
2851 	 * port offset are close together.
2852 	 */
2853 	for (i = 0, caps = 0; i < pctrl->data->n_dedicated_pins; i++) {
2854 		bool has_iolh, has_ien;
2855 		u32 off, next_off = 0;
2856 		u64 cfg, next_cfg;
2857 		u8 pincnt;
2858 
2859 		cfg = pctrl->data->dedicated_pins[i].config;
2860 		off = RZG2L_PIN_CFG_TO_PORT_OFFSET(cfg);
2861 		if (i + 1 < pctrl->data->n_dedicated_pins) {
2862 			next_cfg = pctrl->data->dedicated_pins[i + 1].config;
2863 			next_off = RZG2L_PIN_CFG_TO_PORT_OFFSET(next_cfg);
2864 		}
2865 
2866 		if (off == next_off) {
2867 			/* Gather caps of all port pins. */
2868 			caps |= FIELD_GET(PIN_CFG_MASK, cfg);
2869 			continue;
2870 		}
2871 
2872 		/* And apply them in a single shot. */
2873 		has_iolh = !!(caps & (PIN_CFG_IOLH_A | PIN_CFG_IOLH_B | PIN_CFG_IOLH_C));
2874 		has_ien = !!(caps & PIN_CFG_IEN);
2875 		pincnt = hweight8(FIELD_GET(RZG2L_SINGLE_PIN_BITS_MASK, cfg));
2876 
2877 		if (has_iolh) {
2878 			RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + IOLH(off),
2879 						 cache->iolh[0][i]);
2880 		}
2881 		if (has_ien) {
2882 			RZG2L_PCTRL_REG_ACCESS32(suspend, pctrl->base + IEN(off),
2883 						 cache->ien[0][i]);
2884 		}
2885 
2886 		if (pincnt >= 4) {
2887 			if (has_iolh) {
2888 				RZG2L_PCTRL_REG_ACCESS32(suspend,
2889 							 pctrl->base + IOLH(off) + 4,
2890 							 cache->iolh[1][i]);
2891 			}
2892 			if (has_ien) {
2893 				RZG2L_PCTRL_REG_ACCESS32(suspend,
2894 							 pctrl->base + IEN(off) + 4,
2895 							 cache->ien[1][i]);
2896 			}
2897 		}
2898 		caps = 0;
2899 	}
2900 }
2901 
rzg2l_pinctrl_pm_setup_pfc(struct rzg2l_pinctrl * pctrl)2902 static void rzg2l_pinctrl_pm_setup_pfc(struct rzg2l_pinctrl *pctrl)
2903 {
2904 	u32 nports = pctrl->data->n_port_pins / RZG2L_PINS_PER_PORT;
2905 	unsigned long flags;
2906 
2907 	spin_lock_irqsave(&pctrl->lock, flags);
2908 	pctrl->data->pwpr_pfc_lock_unlock(pctrl, false);
2909 
2910 	/* Restore port registers. */
2911 	for (u32 port = 0; port < nports; port++) {
2912 		unsigned long pinmap;
2913 		u8 pmc = 0, max_pin;
2914 		u32 off, pfc = 0;
2915 		u64 cfg;
2916 		u16 pm;
2917 		u8 pin;
2918 
2919 		cfg = pctrl->data->port_pin_configs[port];
2920 		off = RZG2L_PIN_CFG_TO_PORT_OFFSET(cfg);
2921 		pinmap = FIELD_GET(PIN_CFG_PIN_MAP_MASK, cfg);
2922 		max_pin = fls(pinmap);
2923 
2924 		pm = readw(pctrl->base + PM(off));
2925 		for_each_set_bit(pin, &pinmap, max_pin) {
2926 			struct rzg2l_pinctrl_reg_cache *cache = pctrl->cache;
2927 
2928 			/* Nothing to do if PFC was not configured before. */
2929 			if (!(cache->pmc[port] & BIT(pin)))
2930 				continue;
2931 
2932 			/* Set pin to 'Non-use (Hi-Z input protection)' */
2933 			pm &= ~(PM_MASK << (pin * 2));
2934 			writew(pm, pctrl->base + PM(off));
2935 
2936 			/* Temporarily switch to GPIO mode with PMC register */
2937 			pmc &= ~BIT(pin);
2938 			writeb(pmc, pctrl->base + PMC(off));
2939 
2940 			/* Select Pin function mode. */
2941 			pfc &= ~(PFC_MASK << (pin * 4));
2942 			pfc |= (cache->pfc[port] & (PFC_MASK << (pin * 4)));
2943 			writel(pfc, pctrl->base + PFC(off));
2944 
2945 			/* Switch to Peripheral pin function. */
2946 			pmc |= BIT(pin);
2947 			writeb(pmc, pctrl->base + PMC(off));
2948 		}
2949 	}
2950 
2951 	pctrl->data->pwpr_pfc_lock_unlock(pctrl, true);
2952 	spin_unlock_irqrestore(&pctrl->lock, flags);
2953 }
2954 
rzg2l_pinctrl_suspend_noirq(struct device * dev)2955 static int rzg2l_pinctrl_suspend_noirq(struct device *dev)
2956 {
2957 	struct rzg2l_pinctrl *pctrl = dev_get_drvdata(dev);
2958 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
2959 	const struct rzg2l_register_offsets *regs = &hwcfg->regs;
2960 	struct rzg2l_pinctrl_reg_cache *cache = pctrl->cache;
2961 
2962 	rzg2l_pinctrl_pm_setup_regs(pctrl, true);
2963 	rzg2l_pinctrl_pm_setup_dedicated_regs(pctrl, true);
2964 
2965 	for (u8 i = 0; i < 2; i++) {
2966 		if (regs->sd_ch)
2967 			cache->sd_ch[i] = readb(pctrl->base + SD_CH(regs->sd_ch, i));
2968 		if (regs->eth_poc)
2969 			cache->eth_poc[i] = readb(pctrl->base + ETH_POC(regs->eth_poc, i));
2970 	}
2971 
2972 	cache->qspi = readb(pctrl->base + QSPI);
2973 	cache->eth_mode = readb(pctrl->base + ETH_MODE);
2974 
2975 	if (!atomic_read(&pctrl->wakeup_path))
2976 		clk_disable_unprepare(pctrl->clk);
2977 	else
2978 		device_set_wakeup_path(dev);
2979 
2980 	return 0;
2981 }
2982 
rzg2l_pinctrl_resume_noirq(struct device * dev)2983 static int rzg2l_pinctrl_resume_noirq(struct device *dev)
2984 {
2985 	struct rzg2l_pinctrl *pctrl = dev_get_drvdata(dev);
2986 	const struct rzg2l_hwcfg *hwcfg = pctrl->data->hwcfg;
2987 	const struct rzg2l_register_offsets *regs = &hwcfg->regs;
2988 	struct rzg2l_pinctrl_reg_cache *cache = pctrl->cache;
2989 	int ret;
2990 
2991 	if (!atomic_read(&pctrl->wakeup_path)) {
2992 		ret = clk_prepare_enable(pctrl->clk);
2993 		if (ret)
2994 			return ret;
2995 	}
2996 
2997 	writeb(cache->qspi, pctrl->base + QSPI);
2998 	writeb(cache->eth_mode, pctrl->base + ETH_MODE);
2999 	for (u8 i = 0; i < 2; i++) {
3000 		if (regs->sd_ch)
3001 			writeb(cache->sd_ch[i], pctrl->base + SD_CH(regs->sd_ch, i));
3002 		if (regs->eth_poc)
3003 			writeb(cache->eth_poc[i], pctrl->base + ETH_POC(regs->eth_poc, i));
3004 	}
3005 
3006 	rzg2l_pinctrl_pm_setup_pfc(pctrl);
3007 	rzg2l_pinctrl_pm_setup_regs(pctrl, false);
3008 	rzg2l_pinctrl_pm_setup_dedicated_regs(pctrl, false);
3009 	rzg2l_gpio_irq_restore(pctrl);
3010 
3011 	return 0;
3012 }
3013 
rzg2l_pwpr_pfc_lock_unlock(struct rzg2l_pinctrl * pctrl,bool lock)3014 static void rzg2l_pwpr_pfc_lock_unlock(struct rzg2l_pinctrl *pctrl, bool lock)
3015 {
3016 	const struct rzg2l_register_offsets *regs = &pctrl->data->hwcfg->regs;
3017 
3018 	if (lock) {
3019 		/* Set the PWPR register to be write-protected */
3020 		writel(0x0, pctrl->base + regs->pwpr);		/* B0WI=0, PFCWE=0 */
3021 		writel(PWPR_B0WI, pctrl->base + regs->pwpr);	/* B0WI=1, PFCWE=0 */
3022 	} else {
3023 		/* Set the PWPR register to allow PFC register to write */
3024 		writel(0x0, pctrl->base + regs->pwpr);		/* B0WI=0, PFCWE=0 */
3025 		writel(PWPR_PFCWE, pctrl->base + regs->pwpr);	/* B0WI=0, PFCWE=1 */
3026 	}
3027 }
3028 
rzv2h_pwpr_pfc_lock_unlock(struct rzg2l_pinctrl * pctrl,bool lock)3029 static void rzv2h_pwpr_pfc_lock_unlock(struct rzg2l_pinctrl *pctrl, bool lock)
3030 {
3031 	const struct rzg2l_register_offsets *regs = &pctrl->data->hwcfg->regs;
3032 	u8 pwpr;
3033 
3034 	if (lock) {
3035 		/* Set the PWPR register to be write-protected */
3036 		pwpr = readb(pctrl->base + regs->pwpr);
3037 		writeb(pwpr & ~PWPR_REGWE_A, pctrl->base + regs->pwpr);
3038 	} else {
3039 		/* Set the PWPR register to allow PFC and PMC register to write */
3040 		pwpr = readb(pctrl->base + regs->pwpr);
3041 		writeb(PWPR_REGWE_A | pwpr, pctrl->base + regs->pwpr);
3042 	}
3043 }
3044 
3045 static const struct rzg2l_hwcfg rzg2l_hwcfg = {
3046 	.regs = {
3047 		.pwpr = 0x3014,
3048 		.sd_ch = 0x3000,
3049 		.eth_poc = 0x300c,
3050 	},
3051 	.iolh_groupa_ua = {
3052 		/* 3v3 power source */
3053 		[RZG2L_IOLH_IDX_3V3] = 2000, 4000, 8000, 12000,
3054 	},
3055 	.iolh_groupb_oi = { 100, 66, 50, 33, },
3056 	.oen_max_pin = 0,
3057 };
3058 
3059 static const struct rzg2l_hwcfg rzg3s_hwcfg = {
3060 	.regs = {
3061 		.pwpr = 0x3000,
3062 		.sd_ch = 0x3004,
3063 		.eth_poc = 0x3010,
3064 	},
3065 	.iolh_groupa_ua = {
3066 		/* 1v8 power source */
3067 		[RZG2L_IOLH_IDX_1V8] = 2200, 4400, 9000, 10000,
3068 		/* 3v3 power source */
3069 		[RZG2L_IOLH_IDX_3V3] = 1900, 4000, 8000, 9000,
3070 	},
3071 	.iolh_groupb_ua = {
3072 		/* 1v8 power source */
3073 		[RZG2L_IOLH_IDX_1V8] = 7000, 8000, 9000, 10000,
3074 		/* 3v3 power source */
3075 		[RZG2L_IOLH_IDX_3V3] = 4000, 6000, 8000, 9000,
3076 	},
3077 	.iolh_groupc_ua = {
3078 		/* 1v8 power source */
3079 		[RZG2L_IOLH_IDX_1V8] = 5200, 6000, 6550, 6800,
3080 		/* 2v5 source */
3081 		[RZG2L_IOLH_IDX_2V5] = 4700, 5300, 5800, 6100,
3082 		/* 3v3 power source */
3083 		[RZG2L_IOLH_IDX_3V3] = 4500, 5200, 5700, 6050,
3084 	},
3085 	.drive_strength_ua = true,
3086 	.func_base = 1,
3087 	.oen_max_pin = 1, /* Pin 1 of P0 and P7 is the maximum OEN pin. */
3088 	.oen_max_port = 7, /* P7_1 is the maximum OEN port. */
3089 };
3090 
3091 static const struct rzg2l_hwcfg rzv2h_hwcfg = {
3092 	.regs = {
3093 		.pwpr = 0x3c04,
3094 	},
3095 };
3096 
3097 static struct rzg2l_pinctrl_data r9a07g043_data = {
3098 	.port_pins = rzg2l_gpio_names,
3099 	.port_pin_configs = r9a07g043_gpio_configs,
3100 	.n_ports = ARRAY_SIZE(r9a07g043_gpio_configs),
3101 	.dedicated_pins = rzg2l_dedicated_pins.common,
3102 	.n_port_pins = ARRAY_SIZE(r9a07g043_gpio_configs) * RZG2L_PINS_PER_PORT,
3103 	.n_dedicated_pins = ARRAY_SIZE(rzg2l_dedicated_pins.common),
3104 	.hwcfg = &rzg2l_hwcfg,
3105 #ifdef CONFIG_RISCV
3106 	.variable_pin_cfg = r9a07g043f_variable_pin_cfg,
3107 	.n_variable_pin_cfg = ARRAY_SIZE(r9a07g043f_variable_pin_cfg),
3108 #endif
3109 	.pwpr_pfc_lock_unlock = &rzg2l_pwpr_pfc_lock_unlock,
3110 	.pmc_writeb = &rzg2l_pmc_writeb,
3111 	.oen_read = &rzg2l_read_oen,
3112 	.oen_write = &rzg2l_write_oen,
3113 	.hw_to_bias_param = &rzg2l_hw_to_bias_param,
3114 	.bias_param_to_hw = &rzg2l_bias_param_to_hw,
3115 };
3116 
3117 static struct rzg2l_pinctrl_data r9a07g044_data = {
3118 	.port_pins = rzg2l_gpio_names,
3119 	.port_pin_configs = r9a07g044_gpio_configs,
3120 	.n_ports = ARRAY_SIZE(r9a07g044_gpio_configs),
3121 	.dedicated_pins = rzg2l_dedicated_pins.common,
3122 	.n_port_pins = ARRAY_SIZE(r9a07g044_gpio_configs) * RZG2L_PINS_PER_PORT,
3123 	.n_dedicated_pins = ARRAY_SIZE(rzg2l_dedicated_pins.common) +
3124 		ARRAY_SIZE(rzg2l_dedicated_pins.rzg2l_pins),
3125 	.hwcfg = &rzg2l_hwcfg,
3126 	.pwpr_pfc_lock_unlock = &rzg2l_pwpr_pfc_lock_unlock,
3127 	.pmc_writeb = &rzg2l_pmc_writeb,
3128 	.oen_read = &rzg2l_read_oen,
3129 	.oen_write = &rzg2l_write_oen,
3130 	.hw_to_bias_param = &rzg2l_hw_to_bias_param,
3131 	.bias_param_to_hw = &rzg2l_bias_param_to_hw,
3132 };
3133 
3134 static struct rzg2l_pinctrl_data r9a08g045_data = {
3135 	.port_pins = rzg2l_gpio_names,
3136 	.port_pin_configs = r9a08g045_gpio_configs,
3137 	.n_ports = ARRAY_SIZE(r9a08g045_gpio_configs),
3138 	.dedicated_pins = rzg3s_dedicated_pins,
3139 	.n_port_pins = ARRAY_SIZE(r9a08g045_gpio_configs) * RZG2L_PINS_PER_PORT,
3140 	.n_dedicated_pins = ARRAY_SIZE(rzg3s_dedicated_pins),
3141 	.hwcfg = &rzg3s_hwcfg,
3142 	.pwpr_pfc_lock_unlock = &rzg2l_pwpr_pfc_lock_unlock,
3143 	.pmc_writeb = &rzg2l_pmc_writeb,
3144 	.oen_read = &rzg3s_oen_read,
3145 	.oen_write = &rzg3s_oen_write,
3146 	.hw_to_bias_param = &rzg2l_hw_to_bias_param,
3147 	.bias_param_to_hw = &rzg2l_bias_param_to_hw,
3148 };
3149 
3150 static struct rzg2l_pinctrl_data r9a09g057_data = {
3151 	.port_pins = rzv2h_gpio_names,
3152 	.port_pin_configs = r9a09g057_gpio_configs,
3153 	.n_ports = ARRAY_SIZE(r9a09g057_gpio_configs),
3154 	.dedicated_pins = rzv2h_dedicated_pins,
3155 	.n_port_pins = ARRAY_SIZE(r9a09g057_gpio_configs) * RZG2L_PINS_PER_PORT,
3156 	.n_dedicated_pins = ARRAY_SIZE(rzv2h_dedicated_pins),
3157 	.hwcfg = &rzv2h_hwcfg,
3158 	.variable_pin_cfg = r9a09g057_variable_pin_cfg,
3159 	.n_variable_pin_cfg = ARRAY_SIZE(r9a09g057_variable_pin_cfg),
3160 	.num_custom_params = ARRAY_SIZE(renesas_rzv2h_custom_bindings),
3161 	.custom_params = renesas_rzv2h_custom_bindings,
3162 #ifdef CONFIG_DEBUG_FS
3163 	.custom_conf_items = renesas_rzv2h_conf_items,
3164 #endif
3165 	.pwpr_pfc_lock_unlock = &rzv2h_pwpr_pfc_lock_unlock,
3166 	.pmc_writeb = &rzv2h_pmc_writeb,
3167 	.oen_read = &rzv2h_oen_read,
3168 	.oen_write = &rzv2h_oen_write,
3169 	.hw_to_bias_param = &rzv2h_hw_to_bias_param,
3170 	.bias_param_to_hw = &rzv2h_bias_param_to_hw,
3171 };
3172 
3173 static const struct of_device_id rzg2l_pinctrl_of_table[] = {
3174 	{
3175 		.compatible = "renesas,r9a07g043-pinctrl",
3176 		.data = &r9a07g043_data,
3177 	},
3178 	{
3179 		.compatible = "renesas,r9a07g044-pinctrl",
3180 		.data = &r9a07g044_data,
3181 	},
3182 	{
3183 		.compatible = "renesas,r9a08g045-pinctrl",
3184 		.data = &r9a08g045_data,
3185 	},
3186 	{
3187 		.compatible = "renesas,r9a09g057-pinctrl",
3188 		.data = &r9a09g057_data,
3189 	},
3190 	{ /* sentinel */ }
3191 };
3192 
3193 static const struct dev_pm_ops rzg2l_pinctrl_pm_ops = {
3194 	NOIRQ_SYSTEM_SLEEP_PM_OPS(rzg2l_pinctrl_suspend_noirq, rzg2l_pinctrl_resume_noirq)
3195 };
3196 
3197 static struct platform_driver rzg2l_pinctrl_driver = {
3198 	.driver = {
3199 		.name = DRV_NAME,
3200 		.of_match_table = of_match_ptr(rzg2l_pinctrl_of_table),
3201 		.pm = pm_sleep_ptr(&rzg2l_pinctrl_pm_ops),
3202 		.suppress_bind_attrs = true,
3203 	},
3204 	.probe = rzg2l_pinctrl_probe,
3205 };
3206 
rzg2l_pinctrl_init(void)3207 static int __init rzg2l_pinctrl_init(void)
3208 {
3209 	return platform_driver_register(&rzg2l_pinctrl_driver);
3210 }
3211 core_initcall(rzg2l_pinctrl_init);
3212 
3213 MODULE_AUTHOR("Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>");
3214 MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/G2L family");
3215