1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Pin Control driver for SuperH Pin Function Controller.
4  *
5  * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
6  *
7  * Copyright (C) 2008 Magnus Damm
8  * Copyright (C) 2009 - 2012 Paul Mundt
9  * Copyright (C) 2017 Marek Vasut
10  */
11 
12 #define DRV_NAME "sh-pfc"
13 
14 #include <common.h>
15 #include <dm.h>
16 #include <errno.h>
17 #include <dm/device_compat.h>
18 #include <dm/devres.h>
19 #include <dm/pinctrl.h>
20 #include <linux/bitops.h>
21 #include <linux/bug.h>
22 #include <linux/io.h>
23 #include <linux/sizes.h>
24 
25 #include "sh_pfc.h"
26 
27 enum sh_pfc_model {
28 	SH_PFC_R8A7790 = 0,
29 	SH_PFC_R8A7791,
30 	SH_PFC_R8A7792,
31 	SH_PFC_R8A7793,
32 	SH_PFC_R8A7794,
33 	SH_PFC_R8A7795,
34 	SH_PFC_R8A7796,
35 	SH_PFC_R8A774A1,
36 	SH_PFC_R8A774B1,
37 	SH_PFC_R8A774E1,
38 	SH_PFC_R8A77965,
39 	SH_PFC_R8A77970,
40 	SH_PFC_R8A77980,
41 	SH_PFC_R8A77990,
42 	SH_PFC_R8A77995,
43 };
44 
45 struct sh_pfc_pin_config {
46 	u32 type;
47 	const char *name;
48 };
49 
50 struct sh_pfc_pinctrl {
51 	struct sh_pfc *pfc;
52 
53 	struct sh_pfc_pin_config *configs;
54 };
55 
56 struct sh_pfc_pin_range {
57 	u16 start;
58 	u16 end;
59 };
60 
61 struct sh_pfc_pinctrl_priv {
62 	struct sh_pfc			pfc;
63 	struct sh_pfc_pinctrl		pmx;
64 };
65 
sh_pfc_get_pin_index(struct sh_pfc * pfc,unsigned int pin)66 int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
67 {
68 	unsigned int offset;
69 	unsigned int i;
70 
71 	for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
72 		const struct sh_pfc_pin_range *range = &pfc->ranges[i];
73 
74 		if (pin <= range->end)
75 			return pin >= range->start
76 			     ? offset + pin - range->start : -1;
77 
78 		offset += range->end - range->start + 1;
79 	}
80 
81 	return -EINVAL;
82 }
83 
sh_pfc_enum_in_range(u16 enum_id,const struct pinmux_range * r)84 static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
85 {
86 	if (enum_id < r->begin)
87 		return 0;
88 
89 	if (enum_id > r->end)
90 		return 0;
91 
92 	return 1;
93 }
94 
sh_pfc_read_raw_reg(void __iomem * mapped_reg,unsigned int reg_width)95 u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
96 {
97 	switch (reg_width) {
98 	case 8:
99 		return readb(mapped_reg);
100 	case 16:
101 		return readw(mapped_reg);
102 	case 32:
103 		return readl(mapped_reg);
104 	}
105 
106 	BUG();
107 	return 0;
108 }
109 
sh_pfc_write_raw_reg(void __iomem * mapped_reg,unsigned int reg_width,u32 data)110 void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
111 			  u32 data)
112 {
113 	switch (reg_width) {
114 	case 8:
115 		writeb(data, mapped_reg);
116 		return;
117 	case 16:
118 		writew(data, mapped_reg);
119 		return;
120 	case 32:
121 		writel(data, mapped_reg);
122 		return;
123 	}
124 
125 	BUG();
126 }
127 
sh_pfc_read(struct sh_pfc * pfc,u32 reg)128 u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
129 {
130 	return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32);
131 }
132 
sh_pfc_write(struct sh_pfc * pfc,u32 reg,u32 data)133 void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
134 {
135 	void __iomem *unlock_reg =
136 		(void __iomem *)(uintptr_t)pfc->info->unlock_reg;
137 
138 	if (pfc->info->unlock_reg)
139 		sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
140 
141 	sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)reg, 32, data);
142 }
143 
sh_pfc_config_reg_helper(struct sh_pfc * pfc,const struct pinmux_cfg_reg * crp,unsigned int in_pos,void __iomem ** mapped_regp,u32 * maskp,unsigned int * posp)144 static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
145 				     const struct pinmux_cfg_reg *crp,
146 				     unsigned int in_pos,
147 				     void __iomem **mapped_regp, u32 *maskp,
148 				     unsigned int *posp)
149 {
150 	unsigned int k;
151 
152 	*mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
153 
154 	if (crp->field_width) {
155 		*maskp = (1 << crp->field_width) - 1;
156 		*posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
157 	} else {
158 		*maskp = (1 << crp->var_field_width[in_pos]) - 1;
159 		*posp = crp->reg_width;
160 		for (k = 0; k <= in_pos; k++)
161 			*posp -= crp->var_field_width[k];
162 	}
163 }
164 
sh_pfc_write_config_reg(struct sh_pfc * pfc,const struct pinmux_cfg_reg * crp,unsigned int field,u32 value)165 static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
166 				    const struct pinmux_cfg_reg *crp,
167 				    unsigned int field, u32 value)
168 {
169 	void __iomem *mapped_reg;
170 	void __iomem *unlock_reg =
171 		(void __iomem *)(uintptr_t)pfc->info->unlock_reg;
172 	unsigned int pos;
173 	u32 mask, data;
174 
175 	sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
176 
177 	dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
178 		"r_width = %u, f_width = %u\n",
179 		crp->reg, value, field, crp->reg_width, crp->field_width);
180 
181 	mask = ~(mask << pos);
182 	value = value << pos;
183 
184 	data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
185 	data &= mask;
186 	data |= value;
187 
188 	if (pfc->info->unlock_reg)
189 		sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
190 
191 	sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
192 }
193 
sh_pfc_get_config_reg(struct sh_pfc * pfc,u16 enum_id,const struct pinmux_cfg_reg ** crp,unsigned int * fieldp,u32 * valuep)194 static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
195 				 const struct pinmux_cfg_reg **crp,
196 				 unsigned int *fieldp, u32 *valuep)
197 {
198 	unsigned int k = 0;
199 
200 	while (1) {
201 		const struct pinmux_cfg_reg *config_reg =
202 			pfc->info->cfg_regs + k;
203 		unsigned int r_width = config_reg->reg_width;
204 		unsigned int f_width = config_reg->field_width;
205 		unsigned int curr_width;
206 		unsigned int bit_pos;
207 		unsigned int pos = 0;
208 		unsigned int m = 0;
209 
210 		if (!r_width)
211 			break;
212 
213 		for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
214 			u32 ncomb;
215 			u32 n;
216 
217 			if (f_width)
218 				curr_width = f_width;
219 			else
220 				curr_width = config_reg->var_field_width[m];
221 
222 			ncomb = 1 << curr_width;
223 			for (n = 0; n < ncomb; n++) {
224 				if (config_reg->enum_ids[pos + n] == enum_id) {
225 					*crp = config_reg;
226 					*fieldp = m;
227 					*valuep = n;
228 					return 0;
229 				}
230 			}
231 			pos += ncomb;
232 			m++;
233 		}
234 		k++;
235 	}
236 
237 	return -EINVAL;
238 }
239 
sh_pfc_mark_to_enum(struct sh_pfc * pfc,u16 mark,int pos,u16 * enum_idp)240 static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
241 			      u16 *enum_idp)
242 {
243 	const u16 *data = pfc->info->pinmux_data;
244 	unsigned int k;
245 
246 	if (pos) {
247 		*enum_idp = data[pos + 1];
248 		return pos + 1;
249 	}
250 
251 	for (k = 0; k < pfc->info->pinmux_data_size; k++) {
252 		if (data[k] == mark) {
253 			*enum_idp = data[k + 1];
254 			return k + 1;
255 		}
256 	}
257 
258 	dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
259 		mark);
260 	return -EINVAL;
261 }
262 
sh_pfc_config_mux(struct sh_pfc * pfc,unsigned mark,int pinmux_type)263 int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
264 {
265 	const struct pinmux_range *range;
266 	int pos = 0;
267 
268 	switch (pinmux_type) {
269 	case PINMUX_TYPE_GPIO:
270 	case PINMUX_TYPE_FUNCTION:
271 		range = NULL;
272 		break;
273 
274 	case PINMUX_TYPE_OUTPUT:
275 		range = &pfc->info->output;
276 		break;
277 
278 	case PINMUX_TYPE_INPUT:
279 		range = &pfc->info->input;
280 		break;
281 
282 	default:
283 		return -EINVAL;
284 	}
285 
286 	/* Iterate over all the configuration fields we need to update. */
287 	while (1) {
288 		const struct pinmux_cfg_reg *cr;
289 		unsigned int field;
290 		u16 enum_id;
291 		u32 value;
292 		int in_range;
293 		int ret;
294 
295 		pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
296 		if (pos < 0)
297 			return pos;
298 
299 		if (!enum_id)
300 			break;
301 
302 		/* Check if the configuration field selects a function. If it
303 		 * doesn't, skip the field if it's not applicable to the
304 		 * requested pinmux type.
305 		 */
306 		in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
307 		if (!in_range) {
308 			if (pinmux_type == PINMUX_TYPE_FUNCTION) {
309 				/* Functions are allowed to modify all
310 				 * fields.
311 				 */
312 				in_range = 1;
313 			} else if (pinmux_type != PINMUX_TYPE_GPIO) {
314 				/* Input/output types can only modify fields
315 				 * that correspond to their respective ranges.
316 				 */
317 				in_range = sh_pfc_enum_in_range(enum_id, range);
318 
319 				/*
320 				 * special case pass through for fixed
321 				 * input-only or output-only pins without
322 				 * function enum register association.
323 				 */
324 				if (in_range && enum_id == range->force)
325 					continue;
326 			}
327 			/* GPIOs are only allowed to modify function fields. */
328 		}
329 
330 		if (!in_range)
331 			continue;
332 
333 		ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
334 		if (ret < 0)
335 			return ret;
336 
337 		sh_pfc_write_config_reg(pfc, cr, field, value);
338 	}
339 
340 	return 0;
341 }
342 
343 const struct pinmux_bias_reg *
sh_pfc_pin_to_bias_reg(const struct sh_pfc * pfc,unsigned int pin,unsigned int * bit)344 sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin,
345 		       unsigned int *bit)
346 {
347 	unsigned int i, j;
348 
349 	for (i = 0; pfc->info->bias_regs[i].puen; i++) {
350 		for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) {
351 			if (pfc->info->bias_regs[i].pins[j] == pin) {
352 				*bit = j;
353 				return &pfc->info->bias_regs[i];
354 			}
355 		}
356 	}
357 
358 	WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
359 
360 	return NULL;
361 }
362 
sh_pfc_init_ranges(struct sh_pfc * pfc)363 static int sh_pfc_init_ranges(struct sh_pfc *pfc)
364 {
365 	struct sh_pfc_pin_range *range;
366 	unsigned int nr_ranges;
367 	unsigned int i;
368 
369 	if (pfc->info->pins[0].pin == (u16)-1) {
370 		/* Pin number -1 denotes that the SoC doesn't report pin numbers
371 		 * in its pin arrays yet. Consider the pin numbers range as
372 		 * continuous and allocate a single range.
373 		 */
374 		pfc->nr_ranges = 1;
375 		pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
376 		if (pfc->ranges == NULL)
377 			return -ENOMEM;
378 
379 		pfc->ranges->start = 0;
380 		pfc->ranges->end = pfc->info->nr_pins - 1;
381 		pfc->nr_gpio_pins = pfc->info->nr_pins;
382 
383 		return 0;
384 	}
385 
386 	/* Count, allocate and fill the ranges. The PFC SoC data pins array must
387 	 * be sorted by pin numbers, and pins without a GPIO port must come
388 	 * last.
389 	 */
390 	for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
391 		if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
392 			nr_ranges++;
393 	}
394 
395 	pfc->nr_ranges = nr_ranges;
396 	pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
397 	if (pfc->ranges == NULL)
398 		return -ENOMEM;
399 
400 	range = pfc->ranges;
401 	range->start = pfc->info->pins[0].pin;
402 
403 	for (i = 1; i < pfc->info->nr_pins; ++i) {
404 		if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
405 			continue;
406 
407 		range->end = pfc->info->pins[i-1].pin;
408 		if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
409 			pfc->nr_gpio_pins = range->end + 1;
410 
411 		range++;
412 		range->start = pfc->info->pins[i].pin;
413 	}
414 
415 	range->end = pfc->info->pins[i-1].pin;
416 	if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
417 		pfc->nr_gpio_pins = range->end + 1;
418 
419 	return 0;
420 }
421 
sh_pfc_pinctrl_get_pins_count(struct udevice * dev)422 static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
423 {
424 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
425 
426 	return priv->pfc.info->nr_pins;
427 }
428 
sh_pfc_pinctrl_get_pin_name(struct udevice * dev,unsigned selector)429 static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
430 						  unsigned selector)
431 {
432 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
433 
434 	return priv->pfc.info->pins[selector].name;
435 }
436 
sh_pfc_pinctrl_get_groups_count(struct udevice * dev)437 static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
438 {
439 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
440 
441 	return priv->pfc.info->nr_groups;
442 }
443 
sh_pfc_pinctrl_get_group_name(struct udevice * dev,unsigned selector)444 static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
445 						  unsigned selector)
446 {
447 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
448 
449 	return priv->pfc.info->groups[selector].name;
450 }
451 
sh_pfc_pinctrl_get_pin_muxing(struct udevice * dev,unsigned int selector,char * buf,int size)452 static int sh_pfc_pinctrl_get_pin_muxing(struct udevice *dev,
453 					 unsigned int selector,
454 					 char *buf, int size)
455 {
456 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
457 	struct sh_pfc_pinctrl *pmx = &priv->pmx;
458 	struct sh_pfc *pfc = &priv->pfc;
459 	struct sh_pfc_pin_config *cfg;
460 	const struct sh_pfc_pin *pin;
461 	int idx;
462 
463 	pin = &priv->pfc.info->pins[selector];
464 	if (!pin) {
465 		snprintf(buf, size, "Unknown");
466 		return -EINVAL;
467 	}
468 
469 	idx = sh_pfc_get_pin_index(pfc, pin->pin);
470 	cfg = &pmx->configs[idx];
471 	snprintf(buf, size, "%s", cfg->name);
472 
473 	return 0;
474 }
475 
sh_pfc_pinctrl_get_functions_count(struct udevice * dev)476 static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
477 {
478 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
479 
480 	return priv->pfc.info->nr_functions;
481 }
482 
sh_pfc_pinctrl_get_function_name(struct udevice * dev,unsigned selector)483 static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
484 						  unsigned selector)
485 {
486 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
487 
488 	return priv->pfc.info->functions[selector].name;
489 }
490 
sh_pfc_gpio_request_enable(struct udevice * dev,unsigned pin_selector)491 static int sh_pfc_gpio_request_enable(struct udevice *dev,
492 				      unsigned pin_selector)
493 {
494 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
495 	struct sh_pfc_pinctrl *pmx = &priv->pmx;
496 	struct sh_pfc *pfc = &priv->pfc;
497 	struct sh_pfc_pin_config *cfg;
498 	const struct sh_pfc_pin *pin = NULL;
499 	int i, ret, idx;
500 
501 	for (i = 0; i < pfc->info->nr_pins; i++) {
502 		if (priv->pfc.info->pins[i].pin != pin_selector)
503 			continue;
504 
505 		pin = &priv->pfc.info->pins[i];
506 		break;
507 	}
508 
509 	if (!pin)
510 		return -EINVAL;
511 
512 	idx = sh_pfc_get_pin_index(pfc, pin->pin);
513 	cfg = &pmx->configs[idx];
514 
515 	if (cfg->type != PINMUX_TYPE_NONE) {
516 		if (!strcmp(cfg->name, pin->name))
517 			return 0;
518 
519 		dev_err(pfc->dev, "Pin already used as %s\n",
520 			cfg->name);
521 		return -EBUSY;
522 	}
523 
524 	ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
525 	if (ret)
526 		return ret;
527 
528 	cfg->type = PINMUX_TYPE_GPIO;
529 	cfg->name = "gpio";
530 
531 	return 0;
532 }
533 
sh_pfc_gpio_disable_free(struct udevice * dev,unsigned pin_selector)534 static int sh_pfc_gpio_disable_free(struct udevice *dev,
535 				    unsigned pin_selector)
536 {
537 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
538 	struct sh_pfc_pinctrl *pmx = &priv->pmx;
539 	struct sh_pfc *pfc = &priv->pfc;
540 	struct sh_pfc_pin_config *cfg;
541 	const struct sh_pfc_pin *pin = NULL;
542 	int i, idx;
543 
544 	for (i = 0; i < pfc->info->nr_pins; i++) {
545 		if (priv->pfc.info->pins[i].pin != pin_selector)
546 			continue;
547 
548 		pin = &priv->pfc.info->pins[i];
549 		break;
550 	}
551 
552 	if (!pin)
553 		return -EINVAL;
554 
555 	idx = sh_pfc_get_pin_index(pfc, pin->pin);
556 	cfg = &pmx->configs[idx];
557 
558 	cfg->type = PINMUX_TYPE_NONE;
559 	cfg->name = "none";
560 
561 	return 0;
562 }
563 
sh_pfc_pinctrl_pin_set(struct udevice * dev,unsigned pin_selector,unsigned func_selector)564 static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
565 				  unsigned func_selector)
566 {
567 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
568 	struct sh_pfc_pinctrl *pmx = &priv->pmx;
569 	struct sh_pfc *pfc = &priv->pfc;
570 	const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
571 	int idx = sh_pfc_get_pin_index(pfc, pin->pin);
572 	struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
573 	int ret;
574 
575 	if (cfg->type != PINMUX_TYPE_NONE) {
576 		if (!strcmp(cfg->name, pin->name))
577 			return 0;
578 
579 		dev_err(pfc->dev, "Pin already used as %s\n",
580 			cfg->name);
581 		return -EBUSY;
582 	}
583 
584 	ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
585 	if (ret)
586 		return ret;
587 
588 	cfg->type = PINMUX_TYPE_FUNCTION;
589 	cfg->name = "function";
590 
591 	return 0;
592 }
593 
sh_pfc_pinctrl_group_set(struct udevice * dev,unsigned group_selector,unsigned func_selector)594 static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
595 				     unsigned func_selector)
596 {
597 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
598 	struct sh_pfc_pinctrl *pmx = &priv->pmx;
599 	struct sh_pfc *pfc = &priv->pfc;
600 	const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
601 	bool grp_pins_configured = true;
602 	struct sh_pfc_pin_config *cfg;
603 	unsigned int i;
604 	int ret = 0;
605 	int idx;
606 
607 	for (i = 0; i < grp->nr_pins; ++i) {
608 		idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
609 		cfg = &pmx->configs[idx];
610 
611 		if (cfg->type != PINMUX_TYPE_NONE) {
612 			if (!strcmp(cfg->name, grp->name))
613 				continue;
614 
615 			dev_err(pfc->dev, "Pin already used as %s\n",
616 				cfg->name);
617 			ret = -EBUSY;
618 			goto done;
619 		} else {
620 			grp_pins_configured = false;
621 		}
622 	}
623 
624 	if (grp_pins_configured)
625 		return 0;
626 
627 	for (i = 0; i < grp->nr_pins; ++i) {
628 		ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
629 		if (ret < 0)
630 			break;
631 
632 		idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
633 		cfg = &pmx->configs[idx];
634 		cfg->type = PINMUX_TYPE_FUNCTION;
635 		cfg->name = priv->pfc.info->groups[group_selector].name;
636 	}
637 
638 done:
639 	return ret;
640 }
641 #if CONFIG_IS_ENABLED(PINCONF)
642 static const struct pinconf_param sh_pfc_pinconf_params[] = {
643 	{ "bias-disable",	PIN_CONFIG_BIAS_DISABLE,	0 },
644 	{ "bias-pull-up",	PIN_CONFIG_BIAS_PULL_UP,	1 },
645 	{ "bias-pull-down",	PIN_CONFIG_BIAS_PULL_DOWN,	1 },
646 	{ "drive-strength",	PIN_CONFIG_DRIVE_STRENGTH,	0 },
647 	{ "power-source",	PIN_CONFIG_POWER_SOURCE,	3300 },
648 };
649 
650 static void __iomem *
sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc * pfc,unsigned int pin,unsigned int * offset,unsigned int * size)651 sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
652 				       unsigned int *offset, unsigned int *size)
653 {
654 	const struct pinmux_drive_reg_field *field;
655 	const struct pinmux_drive_reg *reg;
656 	unsigned int i;
657 
658 	for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
659 		for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
660 			field = &reg->fields[i];
661 
662 			if (field->size && field->pin == pin) {
663 				*offset = field->offset;
664 				*size = field->size;
665 
666 				return (void __iomem *)(uintptr_t)reg->reg;
667 			}
668 		}
669 	}
670 
671 	return NULL;
672 }
673 
sh_pfc_pinconf_set_drive_strength(struct sh_pfc * pfc,unsigned int pin,u16 strength)674 static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
675 					     unsigned int pin, u16 strength)
676 {
677 	unsigned int offset;
678 	unsigned int size;
679 	unsigned int step;
680 	void __iomem *reg;
681 	void __iomem *unlock_reg =
682 		(void __iomem *)(uintptr_t)pfc->info->unlock_reg;
683 	u32 val;
684 
685 	reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
686 	if (!reg)
687 		return -EINVAL;
688 
689 	step = size == 2 ? 6 : 3;
690 
691 	if (strength < step || strength > 24)
692 		return -EINVAL;
693 
694 	/* Convert the value from mA based on a full drive strength value of
695 	 * 24mA. We can make the full value configurable later if needed.
696 	 */
697 	strength = strength / step - 1;
698 
699 	val = sh_pfc_read_raw_reg(reg, 32);
700 	val &= ~GENMASK(offset + 4 - 1, offset);
701 	val |= strength << offset;
702 
703 	if (unlock_reg)
704 		sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
705 
706 	sh_pfc_write_raw_reg(reg, 32, val);
707 
708 	return 0;
709 }
710 
711 /* Check whether the requested parameter is supported for a pin. */
sh_pfc_pinconf_validate(struct sh_pfc * pfc,unsigned int _pin,unsigned int param)712 static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
713 				    unsigned int param)
714 {
715 	int idx = sh_pfc_get_pin_index(pfc, _pin);
716 	const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
717 
718 	switch (param) {
719 	case PIN_CONFIG_BIAS_DISABLE:
720 		return pin->configs &
721 			(SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
722 
723 	case PIN_CONFIG_BIAS_PULL_UP:
724 		return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
725 
726 	case PIN_CONFIG_BIAS_PULL_DOWN:
727 		return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
728 
729 	case PIN_CONFIG_DRIVE_STRENGTH:
730 		return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
731 
732 	case PIN_CONFIG_POWER_SOURCE:
733 		return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
734 
735 	default:
736 		return false;
737 	}
738 }
739 
sh_pfc_pinconf_set(struct sh_pfc_pinctrl * pmx,unsigned _pin,unsigned int param,unsigned int arg)740 static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
741 			      unsigned int param, unsigned int arg)
742 {
743 	struct sh_pfc *pfc = pmx->pfc;
744 	void __iomem *pocctrl;
745 	void __iomem *unlock_reg =
746 		(void __iomem *)(uintptr_t)pfc->info->unlock_reg;
747 	u32 addr, val;
748 	int bit, ret;
749 
750 	if (!sh_pfc_pinconf_validate(pfc, _pin, param))
751 		return -ENOTSUPP;
752 
753 	switch (param) {
754 	case PIN_CONFIG_BIAS_PULL_UP:
755 	case PIN_CONFIG_BIAS_PULL_DOWN:
756 	case PIN_CONFIG_BIAS_DISABLE:
757 		if (!pfc->info->ops || !pfc->info->ops->set_bias)
758 			return -ENOTSUPP;
759 
760 		pfc->info->ops->set_bias(pfc, _pin, param);
761 
762 		break;
763 
764 	case PIN_CONFIG_DRIVE_STRENGTH:
765 		ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
766 		if (ret < 0)
767 			return ret;
768 
769 		break;
770 
771 	case PIN_CONFIG_POWER_SOURCE:
772 		if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
773 			return -ENOTSUPP;
774 
775 		bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
776 		if (bit < 0) {
777 			printf("invalid pin %#x", _pin);
778 			return bit;
779 		}
780 
781 		if (arg != 1800 && arg != 3300)
782 			return -EINVAL;
783 
784 		pocctrl = (void __iomem *)(uintptr_t)addr;
785 
786 		val = sh_pfc_read_raw_reg(pocctrl, 32);
787 		if (arg == 3300)
788 			val |= BIT(bit);
789 		else
790 			val &= ~BIT(bit);
791 
792 		if (unlock_reg)
793 			sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
794 
795 		sh_pfc_write_raw_reg(pocctrl, 32, val);
796 
797 		break;
798 
799 	default:
800 		return -ENOTSUPP;
801 	}
802 
803 	return 0;
804 }
805 
sh_pfc_pinconf_pin_set(struct udevice * dev,unsigned int pin_selector,unsigned int param,unsigned int arg)806 static int sh_pfc_pinconf_pin_set(struct udevice *dev,
807 				  unsigned int pin_selector,
808 				  unsigned int param, unsigned int arg)
809 {
810 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
811 	struct sh_pfc_pinctrl *pmx = &priv->pmx;
812 	struct sh_pfc *pfc = &priv->pfc;
813 	const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
814 
815 	sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
816 
817 	return 0;
818 }
819 
sh_pfc_pinconf_group_set(struct udevice * dev,unsigned int group_selector,unsigned int param,unsigned int arg)820 static int sh_pfc_pinconf_group_set(struct udevice *dev,
821 				      unsigned int group_selector,
822 				      unsigned int param, unsigned int arg)
823 {
824 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
825 	struct sh_pfc_pinctrl *pmx = &priv->pmx;
826 	struct sh_pfc *pfc = &priv->pfc;
827 	const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
828 	unsigned int i;
829 
830 	for (i = 0; i < grp->nr_pins; i++)
831 		sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
832 
833 	return 0;
834 }
835 #endif
836 
837 static struct pinctrl_ops sh_pfc_pinctrl_ops = {
838 	.get_pins_count		= sh_pfc_pinctrl_get_pins_count,
839 	.get_pin_name		= sh_pfc_pinctrl_get_pin_name,
840 	.get_groups_count	= sh_pfc_pinctrl_get_groups_count,
841 	.get_group_name		= sh_pfc_pinctrl_get_group_name,
842 	.get_pin_muxing		= sh_pfc_pinctrl_get_pin_muxing,
843 	.get_functions_count	= sh_pfc_pinctrl_get_functions_count,
844 	.get_function_name	= sh_pfc_pinctrl_get_function_name,
845 
846 #if CONFIG_IS_ENABLED(PINCONF)
847 	.pinconf_num_params	= ARRAY_SIZE(sh_pfc_pinconf_params),
848 	.pinconf_params		= sh_pfc_pinconf_params,
849 	.pinconf_set		= sh_pfc_pinconf_pin_set,
850 	.pinconf_group_set	= sh_pfc_pinconf_group_set,
851 #endif
852 	.pinmux_set		= sh_pfc_pinctrl_pin_set,
853 	.pinmux_group_set	= sh_pfc_pinctrl_group_set,
854 	.set_state		= pinctrl_generic_set_state,
855 
856 	.gpio_request_enable	= sh_pfc_gpio_request_enable,
857 	.gpio_disable_free	= sh_pfc_gpio_disable_free,
858 };
859 
sh_pfc_map_pins(struct sh_pfc * pfc,struct sh_pfc_pinctrl * pmx)860 static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
861 {
862 	unsigned int i;
863 
864 	/* Allocate and initialize the pins and configs arrays. */
865 	pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
866 				    GFP_KERNEL);
867 	if (unlikely(!pmx->configs))
868 		return -ENOMEM;
869 
870 	for (i = 0; i < pfc->info->nr_pins; ++i) {
871 		struct sh_pfc_pin_config *cfg = &pmx->configs[i];
872 		cfg->type = PINMUX_TYPE_NONE;
873 		cfg->name = "none";
874 	}
875 
876 	return 0;
877 }
878 
879 
sh_pfc_pinctrl_probe(struct udevice * dev)880 static int sh_pfc_pinctrl_probe(struct udevice *dev)
881 {
882 	struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
883 	enum sh_pfc_model model = dev_get_driver_data(dev);
884 	fdt_addr_t base;
885 
886 	base = dev_read_addr(dev);
887 	if (base == FDT_ADDR_T_NONE)
888 		return -EINVAL;
889 
890 	priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
891 	if (!priv->pfc.regs)
892 		return -ENOMEM;
893 
894 #ifdef CONFIG_PINCTRL_PFC_R8A7790
895 	if (model == SH_PFC_R8A7790)
896 		priv->pfc.info = &r8a7790_pinmux_info;
897 #endif
898 #ifdef CONFIG_PINCTRL_PFC_R8A7791
899 	if (model == SH_PFC_R8A7791)
900 		priv->pfc.info = &r8a7791_pinmux_info;
901 #endif
902 #ifdef CONFIG_PINCTRL_PFC_R8A7792
903 	if (model == SH_PFC_R8A7792)
904 		priv->pfc.info = &r8a7792_pinmux_info;
905 #endif
906 #ifdef CONFIG_PINCTRL_PFC_R8A7793
907 	if (model == SH_PFC_R8A7793)
908 		priv->pfc.info = &r8a7793_pinmux_info;
909 #endif
910 #ifdef CONFIG_PINCTRL_PFC_R8A7794
911 	if (model == SH_PFC_R8A7794)
912 		priv->pfc.info = &r8a7794_pinmux_info;
913 #endif
914 #ifdef CONFIG_PINCTRL_PFC_R8A7795
915 	if (model == SH_PFC_R8A7795)
916 		priv->pfc.info = &r8a7795_pinmux_info;
917 #endif
918 #ifdef CONFIG_PINCTRL_PFC_R8A7796
919 	if (model == SH_PFC_R8A7796)
920 		priv->pfc.info = &r8a7796_pinmux_info;
921 #endif
922 #ifdef CONFIG_PINCTRL_PFC_R8A774A1
923 	if (model == SH_PFC_R8A774A1)
924 		priv->pfc.info = &r8a774a1_pinmux_info;
925 #endif
926 #ifdef CONFIG_PINCTRL_PFC_R8A774B1
927 	if (model == SH_PFC_R8A774B1)
928 		priv->pfc.info = &r8a774b1_pinmux_info;
929 #endif
930 #ifdef CONFIG_PINCTRL_PFC_R8A774E1
931 	if (model == SH_PFC_R8A774E1)
932 		priv->pfc.info = &r8a774e1_pinmux_info;
933 #endif
934 #ifdef CONFIG_PINCTRL_PFC_R8A77965
935 	if (model == SH_PFC_R8A77965)
936 		priv->pfc.info = &r8a77965_pinmux_info;
937 #endif
938 #ifdef CONFIG_PINCTRL_PFC_R8A77970
939 	if (model == SH_PFC_R8A77970)
940 		priv->pfc.info = &r8a77970_pinmux_info;
941 #endif
942 #ifdef CONFIG_PINCTRL_PFC_R8A77980
943 	if (model == SH_PFC_R8A77980)
944 		priv->pfc.info = &r8a77980_pinmux_info;
945 #endif
946 #ifdef CONFIG_PINCTRL_PFC_R8A77990
947 	if (model == SH_PFC_R8A77990)
948 		priv->pfc.info = &r8a77990_pinmux_info;
949 #endif
950 #ifdef CONFIG_PINCTRL_PFC_R8A77995
951 	if (model == SH_PFC_R8A77995)
952 		priv->pfc.info = &r8a77995_pinmux_info;
953 #endif
954 
955 	priv->pmx.pfc = &priv->pfc;
956 	sh_pfc_init_ranges(&priv->pfc);
957 	sh_pfc_map_pins(&priv->pfc, &priv->pmx);
958 
959 	return 0;
960 }
961 
962 static const struct udevice_id sh_pfc_pinctrl_ids[] = {
963 #ifdef CONFIG_PINCTRL_PFC_R8A7790
964 	{
965 		.compatible = "renesas,pfc-r8a7790",
966 		.data = SH_PFC_R8A7790,
967 	},
968 #endif
969 #ifdef CONFIG_PINCTRL_PFC_R8A7791
970 	{
971 		.compatible = "renesas,pfc-r8a7791",
972 		.data = SH_PFC_R8A7791,
973 	},
974 #endif
975 #ifdef CONFIG_PINCTRL_PFC_R8A7792
976 	{
977 		.compatible = "renesas,pfc-r8a7792",
978 		.data = SH_PFC_R8A7792,
979 	},
980 #endif
981 #ifdef CONFIG_PINCTRL_PFC_R8A7793
982 	{
983 		.compatible = "renesas,pfc-r8a7793",
984 		.data = SH_PFC_R8A7793,
985 	},
986 #endif
987 #ifdef CONFIG_PINCTRL_PFC_R8A7794
988 	{
989 		.compatible = "renesas,pfc-r8a7794",
990 		.data = SH_PFC_R8A7794,
991 	},
992 #endif
993 #ifdef CONFIG_PINCTRL_PFC_R8A7795
994 	{
995 		.compatible = "renesas,pfc-r8a7795",
996 		.data = SH_PFC_R8A7795,
997 	},
998 #endif
999 #ifdef CONFIG_PINCTRL_PFC_R8A7796
1000 	{
1001 		.compatible = "renesas,pfc-r8a7796",
1002 		.data = SH_PFC_R8A7796,
1003 	},
1004 #endif
1005 #ifdef CONFIG_PINCTRL_PFC_R8A774A1
1006 	{
1007 		.compatible = "renesas,pfc-r8a774a1",
1008 		.data = SH_PFC_R8A774A1,
1009 	},
1010 #endif
1011 #ifdef CONFIG_PINCTRL_PFC_R8A774B1
1012 	{
1013 		.compatible = "renesas,pfc-r8a774b1",
1014 		.data = SH_PFC_R8A774B1,
1015 	},
1016 #endif
1017 #ifdef CONFIG_PINCTRL_PFC_R8A774E1
1018 	{
1019 		.compatible = "renesas,pfc-r8a774e1",
1020 		.data = SH_PFC_R8A774E1,
1021 	},
1022 #endif
1023 #ifdef CONFIG_PINCTRL_PFC_R8A77965
1024 	{
1025 		.compatible = "renesas,pfc-r8a77965",
1026 		.data = SH_PFC_R8A77965,
1027 	},
1028 #endif
1029 #ifdef CONFIG_PINCTRL_PFC_R8A77970
1030 	{
1031 		.compatible = "renesas,pfc-r8a77970",
1032 		.data = SH_PFC_R8A77970,
1033 	},
1034 #endif
1035 #ifdef CONFIG_PINCTRL_PFC_R8A77980
1036 	{
1037 		.compatible = "renesas,pfc-r8a77980",
1038 		.data = SH_PFC_R8A77980,
1039 	},
1040 #endif
1041 #ifdef CONFIG_PINCTRL_PFC_R8A77990
1042 	{
1043 		.compatible = "renesas,pfc-r8a77990",
1044 		.data = SH_PFC_R8A77990,
1045 	},
1046 #endif
1047 #ifdef CONFIG_PINCTRL_PFC_R8A77995
1048 	{
1049 		.compatible = "renesas,pfc-r8a77995",
1050 		.data = SH_PFC_R8A77995,
1051 	},
1052 #endif
1053 	{ },
1054 };
1055 
1056 U_BOOT_DRIVER(pinctrl_sh_pfc) = {
1057 	.name		= "sh_pfc_pinctrl",
1058 	.id		= UCLASS_PINCTRL,
1059 	.of_match	= sh_pfc_pinctrl_ids,
1060 	.priv_auto	= sizeof(struct sh_pfc_pinctrl_priv),
1061 	.ops		= &sh_pfc_pinctrl_ops,
1062 	.probe		= sh_pfc_pinctrl_probe,
1063 };
1064