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