xref: /linux/drivers/pinctrl/renesas/pinctrl-rzv2m.c (revision c2a96b7f187fb6a455836d4a6e113947ff11de97)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Renesas RZ/V2M Pin Control and GPIO driver core
4  *
5  * Based on:
6  *   Renesas RZ/G2L Pin Control and GPIO driver core
7  *
8  * Copyright (C) 2022 Renesas Electronics Corporation.
9  */
10 
11 #include <linux/bitfield.h>
12 #include <linux/bitops.h>
13 #include <linux/clk.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/spinlock.h>
21 
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pinctrl/pinconf-generic.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/pinctrl/pinctrl.h>
26 #include <linux/pinctrl/pinmux.h>
27 
28 #include <dt-bindings/pinctrl/rzv2m-pinctrl.h>
29 
30 #include "../core.h"
31 #include "../pinconf.h"
32 #include "../pinmux.h"
33 
34 #define DRV_NAME	"pinctrl-rzv2m"
35 
36 /*
37  * Use 16 lower bits [15:0] for pin identifier
38  * Use 16 higher bits [31:16] for pin mux function
39  */
40 #define MUX_PIN_ID_MASK		GENMASK(15, 0)
41 #define MUX_FUNC_MASK		GENMASK(31, 16)
42 #define MUX_FUNC(pinconf)	FIELD_GET(MUX_FUNC_MASK, (pinconf))
43 
44 /* PIN capabilities */
45 #define PIN_CFG_GRP_1_8V_2		1
46 #define PIN_CFG_GRP_1_8V_3		2
47 #define PIN_CFG_GRP_SWIO_1		3
48 #define PIN_CFG_GRP_SWIO_2		4
49 #define PIN_CFG_GRP_3_3V		5
50 #define PIN_CFG_GRP_MASK		GENMASK(2, 0)
51 #define PIN_CFG_BIAS			BIT(3)
52 #define PIN_CFG_DRV			BIT(4)
53 #define PIN_CFG_SLEW			BIT(5)
54 
55 #define RZV2M_MPXED_PIN_FUNCS		(PIN_CFG_BIAS | \
56 					 PIN_CFG_DRV | \
57 					 PIN_CFG_SLEW)
58 
59 /*
60  * n indicates number of pins in the port, a is the register index
61  * and f is pin configuration capabilities supported.
62  */
63 #define RZV2M_GPIO_PORT_PACK(n, a, f)	(((n) << 24) | ((a) << 16) | (f))
64 #define RZV2M_GPIO_PORT_GET_PINCNT(x)	FIELD_GET(GENMASK(31, 24), (x))
65 #define RZV2M_GPIO_PORT_GET_INDEX(x)	FIELD_GET(GENMASK(23, 16), (x))
66 #define RZV2M_GPIO_PORT_GET_CFGS(x)	FIELD_GET(GENMASK(15, 0), (x))
67 
68 #define RZV2M_DEDICATED_PORT_IDX	22
69 
70 /*
71  * BIT(31) indicates dedicated pin, b is the register bits (b * 16)
72  * and f is the pin configuration capabilities supported.
73  */
74 #define RZV2M_SINGLE_PIN		BIT(31)
75 #define RZV2M_SINGLE_PIN_PACK(b, f)	(RZV2M_SINGLE_PIN | \
76 					 ((RZV2M_DEDICATED_PORT_IDX) << 24) | \
77 					 ((b) << 16) | (f))
78 #define RZV2M_SINGLE_PIN_GET_PORT(x)	FIELD_GET(GENMASK(30, 24), (x))
79 #define RZV2M_SINGLE_PIN_GET_BIT(x)	FIELD_GET(GENMASK(23, 16), (x))
80 #define RZV2M_SINGLE_PIN_GET_CFGS(x)	FIELD_GET(GENMASK(15, 0), (x))
81 
82 #define RZV2M_PIN_ID_TO_PORT(id)	((id) / RZV2M_PINS_PER_PORT)
83 #define RZV2M_PIN_ID_TO_PIN(id)		((id) % RZV2M_PINS_PER_PORT)
84 
85 #define DO(n)		(0x00 + (n) * 0x40)
86 #define OE(n)		(0x04 + (n) * 0x40)
87 #define IE(n)		(0x08 + (n) * 0x40)
88 #define PFSEL(n)	(0x10 + (n) * 0x40)
89 #define DI(n)		(0x20 + (n) * 0x40)
90 #define PUPD(n)		(0x24 + (n) * 0x40)
91 #define DRV(n)		((n) < RZV2M_DEDICATED_PORT_IDX ? (0x28 + (n) * 0x40) \
92 							: 0x590)
93 #define SR(n)		((n) < RZV2M_DEDICATED_PORT_IDX ? (0x2c + (n) * 0x40) \
94 							: 0x594)
95 #define DI_MSK(n)	(0x30 + (n) * 0x40)
96 #define EN_MSK(n)	(0x34 + (n) * 0x40)
97 
98 #define PFC_MASK	0x07
99 #define PUPD_MASK	0x03
100 #define DRV_MASK	0x03
101 
102 struct rzv2m_dedicated_configs {
103 	const char *name;
104 	u32 config;
105 };
106 
107 struct rzv2m_pinctrl_data {
108 	const char * const *port_pins;
109 	const u32 *port_pin_configs;
110 	const struct rzv2m_dedicated_configs *dedicated_pins;
111 	unsigned int n_port_pins;
112 	unsigned int n_dedicated_pins;
113 };
114 
115 struct rzv2m_pinctrl {
116 	struct pinctrl_dev		*pctl;
117 	struct pinctrl_desc		desc;
118 	struct pinctrl_pin_desc		*pins;
119 
120 	const struct rzv2m_pinctrl_data	*data;
121 	void __iomem			*base;
122 	struct device			*dev;
123 
124 	struct gpio_chip		gpio_chip;
125 	struct pinctrl_gpio_range	gpio_range;
126 
127 	spinlock_t			lock; /* lock read/write registers */
128 	struct mutex			mutex; /* serialize adding groups and functions */
129 };
130 
131 static const unsigned int drv_1_8V_group2_uA[] = { 1800, 3800, 7800, 11000 };
132 static const unsigned int drv_1_8V_group3_uA[] = { 1600, 3200, 6400, 9600 };
133 static const unsigned int drv_SWIO_group2_3_3V_uA[] = { 9000, 11000, 13000, 18000 };
134 static const unsigned int drv_3_3V_group_uA[] = { 2000, 4000, 8000, 12000 };
135 
136 /* Helper for registers that have a write enable bit in the upper word */
137 static void rzv2m_writel_we(void __iomem *addr, u8 shift, u8 value)
138 {
139 	writel((BIT(16) | value) << shift, addr);
140 }
141 
142 static void rzv2m_pinctrl_set_pfc_mode(struct rzv2m_pinctrl *pctrl,
143 				       u8 port, u8 pin, u8 func)
144 {
145 	void __iomem *addr;
146 
147 	/* Mask input/output */
148 	rzv2m_writel_we(pctrl->base + DI_MSK(port), pin, 1);
149 	rzv2m_writel_we(pctrl->base + EN_MSK(port), pin, 1);
150 
151 	/* Select the function and set the write enable bits */
152 	addr = pctrl->base + PFSEL(port) + (pin / 4) * 4;
153 	writel(((PFC_MASK << 16) | func) << ((pin % 4) * 4), addr);
154 
155 	/* Unmask input/output */
156 	rzv2m_writel_we(pctrl->base + EN_MSK(port), pin, 0);
157 	rzv2m_writel_we(pctrl->base + DI_MSK(port), pin, 0);
158 };
159 
160 static int rzv2m_pinctrl_set_mux(struct pinctrl_dev *pctldev,
161 				 unsigned int func_selector,
162 				 unsigned int group_selector)
163 {
164 	struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
165 	struct function_desc *func;
166 	unsigned int i, *psel_val;
167 	struct group_desc *group;
168 	const unsigned int *pins;
169 
170 	func = pinmux_generic_get_function(pctldev, func_selector);
171 	if (!func)
172 		return -EINVAL;
173 	group = pinctrl_generic_get_group(pctldev, group_selector);
174 	if (!group)
175 		return -EINVAL;
176 
177 	psel_val = func->data;
178 	pins = group->grp.pins;
179 
180 	for (i = 0; i < group->grp.npins; i++) {
181 		dev_dbg(pctrl->dev, "port:%u pin: %u PSEL:%u\n",
182 			RZV2M_PIN_ID_TO_PORT(pins[i]), RZV2M_PIN_ID_TO_PIN(pins[i]),
183 			psel_val[i]);
184 		rzv2m_pinctrl_set_pfc_mode(pctrl, RZV2M_PIN_ID_TO_PORT(pins[i]),
185 					   RZV2M_PIN_ID_TO_PIN(pins[i]), psel_val[i]);
186 	}
187 
188 	return 0;
189 };
190 
191 static int rzv2m_map_add_config(struct pinctrl_map *map,
192 				const char *group_or_pin,
193 				enum pinctrl_map_type type,
194 				unsigned long *configs,
195 				unsigned int num_configs)
196 {
197 	unsigned long *cfgs;
198 
199 	cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
200 		       GFP_KERNEL);
201 	if (!cfgs)
202 		return -ENOMEM;
203 
204 	map->type = type;
205 	map->data.configs.group_or_pin = group_or_pin;
206 	map->data.configs.configs = cfgs;
207 	map->data.configs.num_configs = num_configs;
208 
209 	return 0;
210 }
211 
212 static int rzv2m_dt_subnode_to_map(struct pinctrl_dev *pctldev,
213 				   struct device_node *np,
214 				   struct device_node *parent,
215 				   struct pinctrl_map **map,
216 				   unsigned int *num_maps,
217 				   unsigned int *index)
218 {
219 	struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
220 	struct pinctrl_map *maps = *map;
221 	unsigned int nmaps = *num_maps;
222 	unsigned long *configs = NULL;
223 	unsigned int *pins, *psel_val;
224 	unsigned int num_pinmux = 0;
225 	unsigned int idx = *index;
226 	unsigned int num_pins, i;
227 	unsigned int num_configs;
228 	struct property *pinmux;
229 	struct property *prop;
230 	int ret, gsel, fsel;
231 	const char **pin_fn;
232 	const char *name;
233 	const char *pin;
234 
235 	pinmux = of_find_property(np, "pinmux", NULL);
236 	if (pinmux)
237 		num_pinmux = pinmux->length / sizeof(u32);
238 
239 	ret = of_property_count_strings(np, "pins");
240 	if (ret == -EINVAL) {
241 		num_pins = 0;
242 	} else if (ret < 0) {
243 		dev_err(pctrl->dev, "Invalid pins list in DT\n");
244 		return ret;
245 	} else {
246 		num_pins = ret;
247 	}
248 
249 	if (!num_pinmux && !num_pins)
250 		return 0;
251 
252 	if (num_pinmux && num_pins) {
253 		dev_err(pctrl->dev,
254 			"DT node must contain either a pinmux or pins and not both\n");
255 		return -EINVAL;
256 	}
257 
258 	ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
259 	if (ret < 0)
260 		return ret;
261 
262 	if (num_pins && !num_configs) {
263 		dev_err(pctrl->dev, "DT node must contain a config\n");
264 		ret = -ENODEV;
265 		goto done;
266 	}
267 
268 	if (num_pinmux)
269 		nmaps += 1;
270 
271 	if (num_pins)
272 		nmaps += num_pins;
273 
274 	maps = krealloc_array(maps, nmaps, sizeof(*maps), GFP_KERNEL);
275 	if (!maps) {
276 		ret = -ENOMEM;
277 		goto done;
278 	}
279 
280 	*map = maps;
281 	*num_maps = nmaps;
282 	if (num_pins) {
283 		of_property_for_each_string(np, "pins", prop, pin) {
284 			ret = rzv2m_map_add_config(&maps[idx], pin,
285 						   PIN_MAP_TYPE_CONFIGS_PIN,
286 						   configs, num_configs);
287 			if (ret < 0)
288 				goto done;
289 
290 			idx++;
291 		}
292 		ret = 0;
293 		goto done;
294 	}
295 
296 	pins = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*pins), GFP_KERNEL);
297 	psel_val = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*psel_val),
298 				GFP_KERNEL);
299 	pin_fn = devm_kzalloc(pctrl->dev, sizeof(*pin_fn), GFP_KERNEL);
300 	if (!pins || !psel_val || !pin_fn) {
301 		ret = -ENOMEM;
302 		goto done;
303 	}
304 
305 	/* Collect pin locations and mux settings from DT properties */
306 	for (i = 0; i < num_pinmux; ++i) {
307 		u32 value;
308 
309 		ret = of_property_read_u32_index(np, "pinmux", i, &value);
310 		if (ret)
311 			goto done;
312 		pins[i] = value & MUX_PIN_ID_MASK;
313 		psel_val[i] = MUX_FUNC(value);
314 	}
315 
316 	if (parent) {
317 		name = devm_kasprintf(pctrl->dev, GFP_KERNEL, "%pOFn.%pOFn",
318 				      parent, np);
319 		if (!name) {
320 			ret = -ENOMEM;
321 			goto done;
322 		}
323 	} else {
324 		name = np->name;
325 	}
326 
327 	mutex_lock(&pctrl->mutex);
328 
329 	/* Register a single pin group listing all the pins we read from DT */
330 	gsel = pinctrl_generic_add_group(pctldev, name, pins, num_pinmux, NULL);
331 	if (gsel < 0) {
332 		ret = gsel;
333 		goto unlock;
334 	}
335 
336 	/*
337 	 * Register a single group function where the 'data' is an array PSEL
338 	 * register values read from DT.
339 	 */
340 	pin_fn[0] = name;
341 	fsel = pinmux_generic_add_function(pctldev, name, pin_fn, 1, psel_val);
342 	if (fsel < 0) {
343 		ret = fsel;
344 		goto remove_group;
345 	}
346 
347 	mutex_unlock(&pctrl->mutex);
348 
349 	maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
350 	maps[idx].data.mux.group = name;
351 	maps[idx].data.mux.function = name;
352 	idx++;
353 
354 	dev_dbg(pctrl->dev, "Parsed %pOF with %d pins\n", np, num_pinmux);
355 	ret = 0;
356 	goto done;
357 
358 remove_group:
359 	pinctrl_generic_remove_group(pctldev, gsel);
360 unlock:
361 	mutex_unlock(&pctrl->mutex);
362 done:
363 	*index = idx;
364 	kfree(configs);
365 	return ret;
366 }
367 
368 static void rzv2m_dt_free_map(struct pinctrl_dev *pctldev,
369 			      struct pinctrl_map *map,
370 			      unsigned int num_maps)
371 {
372 	unsigned int i;
373 
374 	if (!map)
375 		return;
376 
377 	for (i = 0; i < num_maps; ++i) {
378 		if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
379 		    map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
380 			kfree(map[i].data.configs.configs);
381 	}
382 	kfree(map);
383 }
384 
385 static int rzv2m_dt_node_to_map(struct pinctrl_dev *pctldev,
386 				struct device_node *np,
387 				struct pinctrl_map **map,
388 				unsigned int *num_maps)
389 {
390 	struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
391 	unsigned int index;
392 	int ret;
393 
394 	*map = NULL;
395 	*num_maps = 0;
396 	index = 0;
397 
398 	for_each_child_of_node_scoped(np, child) {
399 		ret = rzv2m_dt_subnode_to_map(pctldev, child, np, map,
400 					      num_maps, &index);
401 		if (ret < 0)
402 			goto done;
403 	}
404 
405 	if (*num_maps == 0) {
406 		ret = rzv2m_dt_subnode_to_map(pctldev, np, NULL, map,
407 					      num_maps, &index);
408 		if (ret < 0)
409 			goto done;
410 	}
411 
412 	if (*num_maps)
413 		return 0;
414 
415 	dev_err(pctrl->dev, "no mapping found in node %pOF\n", np);
416 	ret = -EINVAL;
417 
418 done:
419 	rzv2m_dt_free_map(pctldev, *map, *num_maps);
420 
421 	return ret;
422 }
423 
424 static int rzv2m_validate_gpio_pin(struct rzv2m_pinctrl *pctrl,
425 				   u32 cfg, u32 port, u8 bit)
426 {
427 	u8 pincount = RZV2M_GPIO_PORT_GET_PINCNT(cfg);
428 	u32 port_index = RZV2M_GPIO_PORT_GET_INDEX(cfg);
429 	u32 data;
430 
431 	if (bit >= pincount || port >= pctrl->data->n_port_pins)
432 		return -EINVAL;
433 
434 	data = pctrl->data->port_pin_configs[port];
435 	if (port_index != RZV2M_GPIO_PORT_GET_INDEX(data))
436 		return -EINVAL;
437 
438 	return 0;
439 }
440 
441 static void rzv2m_rmw_pin_config(struct rzv2m_pinctrl *pctrl, u32 offset,
442 				 u8 shift, u32 mask, u32 val)
443 {
444 	void __iomem *addr = pctrl->base + offset;
445 	unsigned long flags;
446 	u32 reg;
447 
448 	spin_lock_irqsave(&pctrl->lock, flags);
449 	reg = readl(addr) & ~(mask << shift);
450 	writel(reg | (val << shift), addr);
451 	spin_unlock_irqrestore(&pctrl->lock, flags);
452 }
453 
454 static int rzv2m_pinctrl_pinconf_get(struct pinctrl_dev *pctldev,
455 				     unsigned int _pin,
456 				     unsigned long *config)
457 {
458 	struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
459 	enum pin_config_param param = pinconf_to_config_param(*config);
460 	const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
461 	unsigned int *pin_data = pin->drv_data;
462 	unsigned int arg = 0;
463 	u32 port;
464 	u32 cfg;
465 	u8 bit;
466 	u32 val;
467 
468 	if (!pin_data)
469 		return -EINVAL;
470 
471 	if (*pin_data & RZV2M_SINGLE_PIN) {
472 		port = RZV2M_SINGLE_PIN_GET_PORT(*pin_data);
473 		cfg = RZV2M_SINGLE_PIN_GET_CFGS(*pin_data);
474 		bit = RZV2M_SINGLE_PIN_GET_BIT(*pin_data);
475 	} else {
476 		cfg = RZV2M_GPIO_PORT_GET_CFGS(*pin_data);
477 		port = RZV2M_PIN_ID_TO_PORT(_pin);
478 		bit = RZV2M_PIN_ID_TO_PIN(_pin);
479 
480 		if (rzv2m_validate_gpio_pin(pctrl, *pin_data, RZV2M_PIN_ID_TO_PORT(_pin), bit))
481 			return -EINVAL;
482 	}
483 
484 	switch (param) {
485 	case PIN_CONFIG_BIAS_DISABLE:
486 	case PIN_CONFIG_BIAS_PULL_UP:
487 	case PIN_CONFIG_BIAS_PULL_DOWN: {
488 		enum pin_config_param bias;
489 
490 		if (!(cfg & PIN_CFG_BIAS))
491 			return -EINVAL;
492 
493 		/* PUPD uses 2-bits per pin */
494 		bit *= 2;
495 
496 		switch ((readl(pctrl->base + PUPD(port)) >> bit) & PUPD_MASK) {
497 		case 0:
498 			bias = PIN_CONFIG_BIAS_PULL_DOWN;
499 			break;
500 		case 2:
501 			bias = PIN_CONFIG_BIAS_PULL_UP;
502 			break;
503 		default:
504 			bias = PIN_CONFIG_BIAS_DISABLE;
505 		}
506 
507 		if (bias != param)
508 			return -EINVAL;
509 		break;
510 	}
511 
512 	case PIN_CONFIG_DRIVE_STRENGTH_UA:
513 		if (!(cfg & PIN_CFG_DRV))
514 			return -EINVAL;
515 
516 		/* DRV uses 2-bits per pin */
517 		bit *= 2;
518 
519 		val = (readl(pctrl->base + DRV(port)) >> bit) & DRV_MASK;
520 
521 		switch (cfg & PIN_CFG_GRP_MASK) {
522 		case PIN_CFG_GRP_1_8V_2:
523 			arg = drv_1_8V_group2_uA[val];
524 			break;
525 		case PIN_CFG_GRP_1_8V_3:
526 			arg = drv_1_8V_group3_uA[val];
527 			break;
528 		case PIN_CFG_GRP_SWIO_2:
529 			arg = drv_SWIO_group2_3_3V_uA[val];
530 			break;
531 		case PIN_CFG_GRP_SWIO_1:
532 		case PIN_CFG_GRP_3_3V:
533 			arg = drv_3_3V_group_uA[val];
534 			break;
535 		default:
536 			return -EINVAL;
537 		}
538 
539 		break;
540 
541 	case PIN_CONFIG_SLEW_RATE:
542 		if (!(cfg & PIN_CFG_SLEW))
543 			return -EINVAL;
544 
545 		arg = readl(pctrl->base + SR(port)) & BIT(bit);
546 		break;
547 
548 	default:
549 		return -ENOTSUPP;
550 	}
551 
552 	*config = pinconf_to_config_packed(param, arg);
553 
554 	return 0;
555 };
556 
557 static int rzv2m_pinctrl_pinconf_set(struct pinctrl_dev *pctldev,
558 				     unsigned int _pin,
559 				     unsigned long *_configs,
560 				     unsigned int num_configs)
561 {
562 	struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
563 	const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
564 	unsigned int *pin_data = pin->drv_data;
565 	enum pin_config_param param;
566 	u32 port;
567 	unsigned int i;
568 	u32 cfg;
569 	u8 bit;
570 	u32 val;
571 
572 	if (!pin_data)
573 		return -EINVAL;
574 
575 	if (*pin_data & RZV2M_SINGLE_PIN) {
576 		port = RZV2M_SINGLE_PIN_GET_PORT(*pin_data);
577 		cfg = RZV2M_SINGLE_PIN_GET_CFGS(*pin_data);
578 		bit = RZV2M_SINGLE_PIN_GET_BIT(*pin_data);
579 	} else {
580 		cfg = RZV2M_GPIO_PORT_GET_CFGS(*pin_data);
581 		port = RZV2M_PIN_ID_TO_PORT(_pin);
582 		bit = RZV2M_PIN_ID_TO_PIN(_pin);
583 
584 		if (rzv2m_validate_gpio_pin(pctrl, *pin_data, RZV2M_PIN_ID_TO_PORT(_pin), bit))
585 			return -EINVAL;
586 	}
587 
588 	for (i = 0; i < num_configs; i++) {
589 		param = pinconf_to_config_param(_configs[i]);
590 		switch (param) {
591 		case PIN_CONFIG_BIAS_DISABLE:
592 		case PIN_CONFIG_BIAS_PULL_UP:
593 		case PIN_CONFIG_BIAS_PULL_DOWN:
594 			if (!(cfg & PIN_CFG_BIAS))
595 				return -EINVAL;
596 
597 			/* PUPD uses 2-bits per pin */
598 			bit *= 2;
599 
600 			switch (param) {
601 			case PIN_CONFIG_BIAS_PULL_DOWN:
602 				val = 0;
603 				break;
604 			case PIN_CONFIG_BIAS_PULL_UP:
605 				val = 2;
606 				break;
607 			default:
608 				val = 1;
609 			}
610 
611 			rzv2m_rmw_pin_config(pctrl, PUPD(port), bit, PUPD_MASK, val);
612 			break;
613 
614 		case PIN_CONFIG_DRIVE_STRENGTH_UA: {
615 			unsigned int arg = pinconf_to_config_argument(_configs[i]);
616 			const unsigned int *drv_strengths;
617 			unsigned int index;
618 
619 			if (!(cfg & PIN_CFG_DRV))
620 				return -EINVAL;
621 
622 			switch (cfg & PIN_CFG_GRP_MASK) {
623 			case PIN_CFG_GRP_1_8V_2:
624 				drv_strengths = drv_1_8V_group2_uA;
625 				break;
626 			case PIN_CFG_GRP_1_8V_3:
627 				drv_strengths = drv_1_8V_group3_uA;
628 				break;
629 			case PIN_CFG_GRP_SWIO_2:
630 				drv_strengths = drv_SWIO_group2_3_3V_uA;
631 				break;
632 			case PIN_CFG_GRP_SWIO_1:
633 			case PIN_CFG_GRP_3_3V:
634 				drv_strengths = drv_3_3V_group_uA;
635 				break;
636 			default:
637 				return -EINVAL;
638 			}
639 
640 			for (index = 0; index < 4; index++) {
641 				if (arg == drv_strengths[index])
642 					break;
643 			}
644 			if (index >= 4)
645 				return -EINVAL;
646 
647 			/* DRV uses 2-bits per pin */
648 			bit *= 2;
649 
650 			rzv2m_rmw_pin_config(pctrl, DRV(port), bit, DRV_MASK, index);
651 			break;
652 		}
653 
654 		case PIN_CONFIG_SLEW_RATE: {
655 			unsigned int arg = pinconf_to_config_argument(_configs[i]);
656 
657 			if (!(cfg & PIN_CFG_SLEW))
658 				return -EINVAL;
659 
660 			rzv2m_writel_we(pctrl->base + SR(port), bit, !arg);
661 			break;
662 		}
663 
664 		default:
665 			return -EOPNOTSUPP;
666 		}
667 	}
668 
669 	return 0;
670 }
671 
672 static int rzv2m_pinctrl_pinconf_group_set(struct pinctrl_dev *pctldev,
673 					   unsigned int group,
674 					   unsigned long *configs,
675 					   unsigned int num_configs)
676 {
677 	const unsigned int *pins;
678 	unsigned int i, npins;
679 	int ret;
680 
681 	ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
682 	if (ret)
683 		return ret;
684 
685 	for (i = 0; i < npins; i++) {
686 		ret = rzv2m_pinctrl_pinconf_set(pctldev, pins[i], configs,
687 						num_configs);
688 		if (ret)
689 			return ret;
690 	}
691 
692 	return 0;
693 };
694 
695 static int rzv2m_pinctrl_pinconf_group_get(struct pinctrl_dev *pctldev,
696 					   unsigned int group,
697 					   unsigned long *config)
698 {
699 	const unsigned int *pins;
700 	unsigned int i, npins, prev_config = 0;
701 	int ret;
702 
703 	ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
704 	if (ret)
705 		return ret;
706 
707 	for (i = 0; i < npins; i++) {
708 		ret = rzv2m_pinctrl_pinconf_get(pctldev, pins[i], config);
709 		if (ret)
710 			return ret;
711 
712 		/* Check config matches previous pins */
713 		if (i && prev_config != *config)
714 			return -EOPNOTSUPP;
715 
716 		prev_config = *config;
717 	}
718 
719 	return 0;
720 };
721 
722 static const struct pinctrl_ops rzv2m_pinctrl_pctlops = {
723 	.get_groups_count = pinctrl_generic_get_group_count,
724 	.get_group_name = pinctrl_generic_get_group_name,
725 	.get_group_pins = pinctrl_generic_get_group_pins,
726 	.dt_node_to_map = rzv2m_dt_node_to_map,
727 	.dt_free_map = rzv2m_dt_free_map,
728 };
729 
730 static const struct pinmux_ops rzv2m_pinctrl_pmxops = {
731 	.get_functions_count = pinmux_generic_get_function_count,
732 	.get_function_name = pinmux_generic_get_function_name,
733 	.get_function_groups = pinmux_generic_get_function_groups,
734 	.set_mux = rzv2m_pinctrl_set_mux,
735 	.strict = true,
736 };
737 
738 static const struct pinconf_ops rzv2m_pinctrl_confops = {
739 	.is_generic = true,
740 	.pin_config_get = rzv2m_pinctrl_pinconf_get,
741 	.pin_config_set = rzv2m_pinctrl_pinconf_set,
742 	.pin_config_group_set = rzv2m_pinctrl_pinconf_group_set,
743 	.pin_config_group_get = rzv2m_pinctrl_pinconf_group_get,
744 	.pin_config_config_dbg_show = pinconf_generic_dump_config,
745 };
746 
747 static int rzv2m_gpio_request(struct gpio_chip *chip, unsigned int offset)
748 {
749 	struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
750 	u32 port = RZV2M_PIN_ID_TO_PORT(offset);
751 	u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
752 	int ret;
753 
754 	ret = pinctrl_gpio_request(chip, offset);
755 	if (ret)
756 		return ret;
757 
758 	rzv2m_pinctrl_set_pfc_mode(pctrl, port, bit, 0);
759 
760 	return 0;
761 }
762 
763 static void rzv2m_gpio_set_direction(struct rzv2m_pinctrl *pctrl, u32 port,
764 				     u8 bit, bool output)
765 {
766 	rzv2m_writel_we(pctrl->base + OE(port), bit, output);
767 	rzv2m_writel_we(pctrl->base + IE(port), bit, !output);
768 }
769 
770 static int rzv2m_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
771 {
772 	struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
773 	u32 port = RZV2M_PIN_ID_TO_PORT(offset);
774 	u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
775 
776 	if (!(readl(pctrl->base + IE(port)) & BIT(bit)))
777 		return GPIO_LINE_DIRECTION_OUT;
778 
779 	return GPIO_LINE_DIRECTION_IN;
780 }
781 
782 static int rzv2m_gpio_direction_input(struct gpio_chip *chip,
783 				      unsigned int offset)
784 {
785 	struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
786 	u32 port = RZV2M_PIN_ID_TO_PORT(offset);
787 	u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
788 
789 	rzv2m_gpio_set_direction(pctrl, port, bit, false);
790 
791 	return 0;
792 }
793 
794 static void rzv2m_gpio_set(struct gpio_chip *chip, unsigned int offset,
795 			   int value)
796 {
797 	struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
798 	u32 port = RZV2M_PIN_ID_TO_PORT(offset);
799 	u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
800 
801 	rzv2m_writel_we(pctrl->base + DO(port), bit, !!value);
802 }
803 
804 static int rzv2m_gpio_direction_output(struct gpio_chip *chip,
805 				       unsigned int offset, int value)
806 {
807 	struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
808 	u32 port = RZV2M_PIN_ID_TO_PORT(offset);
809 	u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
810 
811 	rzv2m_gpio_set(chip, offset, value);
812 	rzv2m_gpio_set_direction(pctrl, port, bit, true);
813 
814 	return 0;
815 }
816 
817 static int rzv2m_gpio_get(struct gpio_chip *chip, unsigned int offset)
818 {
819 	struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
820 	u32 port = RZV2M_PIN_ID_TO_PORT(offset);
821 	u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
822 	int direction = rzv2m_gpio_get_direction(chip, offset);
823 
824 	if (direction == GPIO_LINE_DIRECTION_IN)
825 		return !!(readl(pctrl->base + DI(port)) & BIT(bit));
826 	else
827 		return !!(readl(pctrl->base + DO(port)) & BIT(bit));
828 }
829 
830 static void rzv2m_gpio_free(struct gpio_chip *chip, unsigned int offset)
831 {
832 	pinctrl_gpio_free(chip, offset);
833 
834 	/*
835 	 * Set the GPIO as an input to ensure that the next GPIO request won't
836 	 * drive the GPIO pin as an output.
837 	 */
838 	rzv2m_gpio_direction_input(chip, offset);
839 }
840 
841 static const char * const rzv2m_gpio_names[] = {
842 	"P0_0", "P0_1", "P0_2", "P0_3", "P0_4", "P0_5", "P0_6", "P0_7",
843 	"P0_8", "P0_9", "P0_10", "P0_11", "P0_12", "P0_13", "P0_14", "P0_15",
844 	"P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7",
845 	"P1_8", "P1_9", "P1_10", "P1_11", "P1_12", "P1_13", "P1_14", "P1_15",
846 	"P2_0", "P2_1", "P2_2", "P2_3", "P2_4", "P2_5", "P2_6", "P2_7",
847 	"P2_8", "P2_9", "P2_10", "P2_11", "P2_12", "P2_13", "P2_14", "P2_15",
848 	"P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7",
849 	"P3_8", "P3_9", "P3_10", "P3_11", "P3_12", "P3_13", "P3_14", "P3_15",
850 	"P4_0", "P4_1", "P4_2", "P4_3", "P4_4", "P4_5", "P4_6", "P4_7",
851 	"P4_8", "P4_9", "P4_10", "P4_11", "P4_12", "P4_13", "P4_14", "P4_15",
852 	"P5_0", "P5_1", "P5_2", "P5_3", "P5_4", "P5_5", "P5_6", "P5_7",
853 	"P5_8", "P5_9", "P5_10", "P5_11", "P5_12", "P5_13", "P5_14", "P5_15",
854 	"P6_0", "P6_1", "P6_2", "P6_3", "P6_4", "P6_5", "P6_6", "P6_7",
855 	"P6_8", "P6_9", "P6_10", "P6_11", "P6_12", "P6_13", "P6_14", "P6_15",
856 	"P7_0", "P7_1", "P7_2", "P7_3", "P7_4", "P7_5", "P7_6", "P7_7",
857 	"P7_8", "P7_9", "P7_10", "P7_11", "P7_12", "P7_13", "P7_14", "P7_15",
858 	"P8_0", "P8_1", "P8_2", "P8_3", "P8_4", "P8_5", "P8_6", "P8_7",
859 	"P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
860 	"P9_0", "P9_1", "P9_2", "P9_3", "P9_4", "P9_5", "P9_6", "P9_7",
861 	"P9_8", "P9_9", "P9_10", "P9_11", "P9_12", "P9_13", "P9_14", "P9_15",
862 	"P10_0", "P10_1", "P10_2", "P10_3", "P10_4", "P10_5", "P10_6", "P10_7",
863 	"P10_8", "P10_9", "P10_10", "P10_11", "P10_12", "P10_13", "P10_14", "P10_15",
864 	"P11_0", "P11_1", "P11_2", "P11_3", "P11_4", "P11_5", "P11_6", "P11_7",
865 	"P11_8", "P11_9", "P11_10", "P11_11", "P11_12", "P11_13", "P11_14", "P11_15",
866 	"P12_0", "P12_1", "P12_2", "P12_3", "P12_4", "P12_5", "P12_6", "P12_7",
867 	"P12_8", "P12_9", "P12_10", "P12_11", "P12_12", "P12_13", "P12_14", "P12_15",
868 	"P13_0", "P13_1", "P13_2", "P13_3", "P13_4", "P13_5", "P13_6", "P13_7",
869 	"P13_8", "P13_9", "P13_10", "P13_11", "P13_12", "P13_13", "P13_14", "P13_15",
870 	"P14_0", "P14_1", "P14_2", "P14_3", "P14_4", "P14_5", "P14_6", "P14_7",
871 	"P14_8", "P14_9", "P14_10", "P14_11", "P14_12", "P14_13", "P14_14", "P14_15",
872 	"P15_0", "P15_1", "P15_2", "P15_3", "P15_4", "P15_5", "P15_6", "P15_7",
873 	"P15_8", "P15_9", "P15_10", "P15_11", "P15_12", "P15_13", "P15_14", "P15_15",
874 	"P16_0", "P16_1", "P16_2", "P16_3", "P16_4", "P16_5", "P16_6", "P16_7",
875 	"P16_8", "P16_9", "P16_10", "P16_11", "P16_12", "P16_13", "P16_14", "P16_15",
876 	"P17_0", "P17_1", "P17_2", "P17_3", "P17_4", "P17_5", "P17_6", "P17_7",
877 	"P17_8", "P17_9", "P17_10", "P17_11", "P17_12", "P17_13", "P17_14", "P17_15",
878 	"P18_0", "P18_1", "P18_2", "P18_3", "P18_4", "P18_5", "P18_6", "P18_7",
879 	"P18_8", "P18_9", "P18_10", "P18_11", "P18_12", "P18_13", "P18_14", "P18_15",
880 	"P19_0", "P19_1", "P19_2", "P19_3", "P19_4", "P19_5", "P19_6", "P19_7",
881 	"P19_8", "P19_9", "P19_10", "P19_11", "P19_12", "P19_13", "P19_14", "P19_15",
882 	"P20_0", "P20_1", "P20_2", "P20_3", "P20_4", "P20_5", "P20_6", "P20_7",
883 	"P20_8", "P20_9", "P20_10", "P20_11", "P20_12", "P20_13", "P20_14", "P20_15",
884 	"P21_0", "P21_1", "P21_2", "P21_3", "P21_4", "P21_5", "P21_6", "P21_7",
885 	"P21_8", "P21_9", "P21_10", "P21_11", "P21_12", "P21_13", "P21_14", "P21_15",
886 };
887 
888 static const u32 rzv2m_gpio_configs[] = {
889 	RZV2M_GPIO_PORT_PACK(14, 0, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
890 	RZV2M_GPIO_PORT_PACK(16, 1, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
891 	RZV2M_GPIO_PORT_PACK(8,  2, PIN_CFG_GRP_1_8V_3 | RZV2M_MPXED_PIN_FUNCS),
892 	RZV2M_GPIO_PORT_PACK(16, 3, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
893 	RZV2M_GPIO_PORT_PACK(8,  4, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
894 	RZV2M_GPIO_PORT_PACK(4,  5, PIN_CFG_GRP_1_8V_3 | RZV2M_MPXED_PIN_FUNCS),
895 	RZV2M_GPIO_PORT_PACK(12, 6, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
896 	RZV2M_GPIO_PORT_PACK(6,  7, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
897 	RZV2M_GPIO_PORT_PACK(8,  8, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
898 	RZV2M_GPIO_PORT_PACK(8,  9, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
899 	RZV2M_GPIO_PORT_PACK(9,  10, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
900 	RZV2M_GPIO_PORT_PACK(9,  11, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
901 	RZV2M_GPIO_PORT_PACK(4,  12, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
902 	RZV2M_GPIO_PORT_PACK(12, 13, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
903 	RZV2M_GPIO_PORT_PACK(8,  14, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
904 	RZV2M_GPIO_PORT_PACK(16, 15, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
905 	RZV2M_GPIO_PORT_PACK(14, 16, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
906 	RZV2M_GPIO_PORT_PACK(1,  17, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
907 	RZV2M_GPIO_PORT_PACK(0,  18, 0),
908 	RZV2M_GPIO_PORT_PACK(0,  19, 0),
909 	RZV2M_GPIO_PORT_PACK(3,  20, PIN_CFG_GRP_1_8V_2 | PIN_CFG_DRV),
910 	RZV2M_GPIO_PORT_PACK(1,  21, PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW),
911 };
912 
913 static const struct rzv2m_dedicated_configs rzv2m_dedicated_pins[] = {
914 	{ "NAWPN", RZV2M_SINGLE_PIN_PACK(0,
915 		(PIN_CFG_GRP_SWIO_2 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
916 	{ "IM0CLK", RZV2M_SINGLE_PIN_PACK(1,
917 		(PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
918 	{ "IM1CLK", RZV2M_SINGLE_PIN_PACK(2,
919 		(PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
920 	{ "DETDO", RZV2M_SINGLE_PIN_PACK(5,
921 		(PIN_CFG_GRP_1_8V_3 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
922 	{ "DETMS", RZV2M_SINGLE_PIN_PACK(6,
923 		(PIN_CFG_GRP_1_8V_3 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
924 	{ "PCRSTOUTB", RZV2M_SINGLE_PIN_PACK(12,
925 		(PIN_CFG_GRP_3_3V | PIN_CFG_DRV | PIN_CFG_SLEW)) },
926 	{ "USPWEN", RZV2M_SINGLE_PIN_PACK(14,
927 		(PIN_CFG_GRP_3_3V | PIN_CFG_DRV | PIN_CFG_SLEW)) },
928 };
929 
930 static int rzv2m_gpio_register(struct rzv2m_pinctrl *pctrl)
931 {
932 	struct device_node *np = pctrl->dev->of_node;
933 	struct gpio_chip *chip = &pctrl->gpio_chip;
934 	const char *name = dev_name(pctrl->dev);
935 	struct of_phandle_args of_args;
936 	int ret;
937 
938 	ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &of_args);
939 	if (ret) {
940 		dev_err(pctrl->dev, "Unable to parse gpio-ranges\n");
941 		return ret;
942 	}
943 
944 	if (of_args.args[0] != 0 || of_args.args[1] != 0 ||
945 	    of_args.args[2] != pctrl->data->n_port_pins) {
946 		dev_err(pctrl->dev, "gpio-ranges does not match selected SOC\n");
947 		return -EINVAL;
948 	}
949 
950 	chip->names = pctrl->data->port_pins;
951 	chip->request = rzv2m_gpio_request;
952 	chip->free = rzv2m_gpio_free;
953 	chip->get_direction = rzv2m_gpio_get_direction;
954 	chip->direction_input = rzv2m_gpio_direction_input;
955 	chip->direction_output = rzv2m_gpio_direction_output;
956 	chip->get = rzv2m_gpio_get;
957 	chip->set = rzv2m_gpio_set;
958 	chip->label = name;
959 	chip->parent = pctrl->dev;
960 	chip->owner = THIS_MODULE;
961 	chip->base = -1;
962 	chip->ngpio = of_args.args[2];
963 
964 	pctrl->gpio_range.id = 0;
965 	pctrl->gpio_range.pin_base = 0;
966 	pctrl->gpio_range.base = 0;
967 	pctrl->gpio_range.npins = chip->ngpio;
968 	pctrl->gpio_range.name = chip->label;
969 	pctrl->gpio_range.gc = chip;
970 	ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl);
971 	if (ret) {
972 		dev_err(pctrl->dev, "failed to add GPIO controller\n");
973 		return ret;
974 	}
975 
976 	dev_dbg(pctrl->dev, "Registered gpio controller\n");
977 
978 	return 0;
979 }
980 
981 static int rzv2m_pinctrl_register(struct rzv2m_pinctrl *pctrl)
982 {
983 	struct pinctrl_pin_desc *pins;
984 	unsigned int i, j;
985 	u32 *pin_data;
986 	int ret;
987 
988 	pctrl->desc.name = DRV_NAME;
989 	pctrl->desc.npins = pctrl->data->n_port_pins + pctrl->data->n_dedicated_pins;
990 	pctrl->desc.pctlops = &rzv2m_pinctrl_pctlops;
991 	pctrl->desc.pmxops = &rzv2m_pinctrl_pmxops;
992 	pctrl->desc.confops = &rzv2m_pinctrl_confops;
993 	pctrl->desc.owner = THIS_MODULE;
994 
995 	pins = devm_kcalloc(pctrl->dev, pctrl->desc.npins, sizeof(*pins), GFP_KERNEL);
996 	if (!pins)
997 		return -ENOMEM;
998 
999 	pin_data = devm_kcalloc(pctrl->dev, pctrl->desc.npins,
1000 				sizeof(*pin_data), GFP_KERNEL);
1001 	if (!pin_data)
1002 		return -ENOMEM;
1003 
1004 	pctrl->pins = pins;
1005 	pctrl->desc.pins = pins;
1006 
1007 	for (i = 0, j = 0; i < pctrl->data->n_port_pins; i++) {
1008 		pins[i].number = i;
1009 		pins[i].name = pctrl->data->port_pins[i];
1010 		if (i && !(i % RZV2M_PINS_PER_PORT))
1011 			j++;
1012 		pin_data[i] = pctrl->data->port_pin_configs[j];
1013 		pins[i].drv_data = &pin_data[i];
1014 	}
1015 
1016 	for (i = 0; i < pctrl->data->n_dedicated_pins; i++) {
1017 		unsigned int index = pctrl->data->n_port_pins + i;
1018 
1019 		pins[index].number = index;
1020 		pins[index].name = pctrl->data->dedicated_pins[i].name;
1021 		pin_data[index] = pctrl->data->dedicated_pins[i].config;
1022 		pins[index].drv_data = &pin_data[index];
1023 	}
1024 
1025 	ret = devm_pinctrl_register_and_init(pctrl->dev, &pctrl->desc, pctrl,
1026 					     &pctrl->pctl);
1027 	if (ret) {
1028 		dev_err(pctrl->dev, "pinctrl registration failed\n");
1029 		return ret;
1030 	}
1031 
1032 	ret = pinctrl_enable(pctrl->pctl);
1033 	if (ret) {
1034 		dev_err(pctrl->dev, "pinctrl enable failed\n");
1035 		return ret;
1036 	}
1037 
1038 	ret = rzv2m_gpio_register(pctrl);
1039 	if (ret) {
1040 		dev_err(pctrl->dev, "failed to add GPIO chip: %i\n", ret);
1041 		return ret;
1042 	}
1043 
1044 	return 0;
1045 }
1046 
1047 static int rzv2m_pinctrl_probe(struct platform_device *pdev)
1048 {
1049 	struct rzv2m_pinctrl *pctrl;
1050 	struct clk *clk;
1051 	int ret;
1052 
1053 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1054 	if (!pctrl)
1055 		return -ENOMEM;
1056 
1057 	pctrl->dev = &pdev->dev;
1058 
1059 	pctrl->data = of_device_get_match_data(&pdev->dev);
1060 	if (!pctrl->data)
1061 		return -EINVAL;
1062 
1063 	pctrl->base = devm_platform_ioremap_resource(pdev, 0);
1064 	if (IS_ERR(pctrl->base))
1065 		return PTR_ERR(pctrl->base);
1066 
1067 	clk = devm_clk_get_enabled(pctrl->dev, NULL);
1068 	if (IS_ERR(clk))
1069 		return dev_err_probe(pctrl->dev, PTR_ERR(clk),
1070 				     "failed to enable GPIO clk\n");
1071 
1072 	spin_lock_init(&pctrl->lock);
1073 	mutex_init(&pctrl->mutex);
1074 
1075 	platform_set_drvdata(pdev, pctrl);
1076 
1077 	ret = rzv2m_pinctrl_register(pctrl);
1078 	if (ret)
1079 		return ret;
1080 
1081 	dev_info(pctrl->dev, "%s support registered\n", DRV_NAME);
1082 	return 0;
1083 }
1084 
1085 static struct rzv2m_pinctrl_data r9a09g011_data = {
1086 	.port_pins = rzv2m_gpio_names,
1087 	.port_pin_configs = rzv2m_gpio_configs,
1088 	.dedicated_pins = rzv2m_dedicated_pins,
1089 	.n_port_pins = ARRAY_SIZE(rzv2m_gpio_configs) * RZV2M_PINS_PER_PORT,
1090 	.n_dedicated_pins = ARRAY_SIZE(rzv2m_dedicated_pins),
1091 };
1092 
1093 static const struct of_device_id rzv2m_pinctrl_of_table[] = {
1094 	{
1095 		.compatible = "renesas,r9a09g011-pinctrl",
1096 		.data = &r9a09g011_data,
1097 	},
1098 	{ /* sentinel */ }
1099 };
1100 
1101 static struct platform_driver rzv2m_pinctrl_driver = {
1102 	.driver = {
1103 		.name = DRV_NAME,
1104 		.of_match_table = of_match_ptr(rzv2m_pinctrl_of_table),
1105 	},
1106 	.probe = rzv2m_pinctrl_probe,
1107 };
1108 
1109 static int __init rzv2m_pinctrl_init(void)
1110 {
1111 	return platform_driver_register(&rzv2m_pinctrl_driver);
1112 }
1113 core_initcall(rzv2m_pinctrl_init);
1114 
1115 MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
1116 MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/V2M");
1117