xref: /openbmc/linux/drivers/pinctrl/renesas/pinctrl.c (revision a3ee0a246df1755af04d79cd6cd53939aeb6a0db)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SuperH Pin Function Controller pinmux support.
4  *
5  * Copyright (C) 2012  Paul Mundt
6  */
7 
8 #define DRV_NAME "sh-pfc"
9 
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/pinctrl/machine.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 
24 #include "core.h"
25 #include "../core.h"
26 #include "../pinconf.h"
27 
28 struct sh_pfc_pin_config {
29 	u16 gpio_enabled:1;
30 	u16 mux_mark:15;
31 };
32 
33 struct sh_pfc_pinctrl {
34 	struct pinctrl_dev *pctl;
35 	struct pinctrl_desc pctl_desc;
36 
37 	struct sh_pfc *pfc;
38 
39 	struct pinctrl_pin_desc *pins;
40 	struct sh_pfc_pin_config *configs;
41 
42 	const char *func_prop_name;
43 	const char *groups_prop_name;
44 	const char *pins_prop_name;
45 };
46 
47 static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
48 {
49 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
50 
51 	return pmx->pfc->info->nr_groups;
52 }
53 
54 static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
55 					 unsigned selector)
56 {
57 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
58 
59 	return pmx->pfc->info->groups[selector].name;
60 }
61 
62 static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
63 				 const unsigned **pins, unsigned *num_pins)
64 {
65 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
66 
67 	*pins = pmx->pfc->info->groups[selector].pins;
68 	*num_pins = pmx->pfc->info->groups[selector].nr_pins;
69 
70 	return 0;
71 }
72 
73 static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
74 				unsigned offset)
75 {
76 	seq_puts(s, DRV_NAME);
77 }
78 
79 #ifdef CONFIG_OF
80 static int sh_pfc_map_add_config(struct pinctrl_map *map,
81 				 const char *group_or_pin,
82 				 enum pinctrl_map_type type,
83 				 unsigned long *configs,
84 				 unsigned int num_configs)
85 {
86 	unsigned long *cfgs;
87 
88 	cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
89 		       GFP_KERNEL);
90 	if (cfgs == NULL)
91 		return -ENOMEM;
92 
93 	map->type = type;
94 	map->data.configs.group_or_pin = group_or_pin;
95 	map->data.configs.configs = cfgs;
96 	map->data.configs.num_configs = num_configs;
97 
98 	return 0;
99 }
100 
101 static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev *pctldev,
102 				    struct device_node *np,
103 				    struct pinctrl_map **map,
104 				    unsigned int *num_maps, unsigned int *index)
105 {
106 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
107 	struct device *dev = pmx->pfc->dev;
108 	struct pinctrl_map *maps = *map;
109 	unsigned int nmaps = *num_maps;
110 	unsigned int idx = *index;
111 	unsigned int num_configs;
112 	const char *function = NULL;
113 	unsigned long *configs;
114 	struct property *prop;
115 	unsigned int num_groups;
116 	unsigned int num_pins;
117 	const char *group;
118 	const char *pin;
119 	int ret;
120 
121 	/* Support both the old Renesas-specific properties and the new standard
122 	 * properties. Mixing old and new properties isn't allowed, neither
123 	 * inside a subnode nor across subnodes.
124 	 */
125 	if (!pmx->func_prop_name) {
126 		if (of_find_property(np, "groups", NULL) ||
127 		    of_find_property(np, "pins", NULL)) {
128 			pmx->func_prop_name = "function";
129 			pmx->groups_prop_name = "groups";
130 			pmx->pins_prop_name = "pins";
131 		} else {
132 			pmx->func_prop_name = "renesas,function";
133 			pmx->groups_prop_name = "renesas,groups";
134 			pmx->pins_prop_name = "renesas,pins";
135 		}
136 	}
137 
138 	/* Parse the function and configuration properties. At least a function
139 	 * or one configuration must be specified.
140 	 */
141 	ret = of_property_read_string(np, pmx->func_prop_name, &function);
142 	if (ret < 0 && ret != -EINVAL) {
143 		dev_err(dev, "Invalid function in DT\n");
144 		return ret;
145 	}
146 
147 	ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
148 	if (ret < 0)
149 		return ret;
150 
151 	if (!function && num_configs == 0) {
152 		dev_err(dev,
153 			"DT node must contain at least a function or config\n");
154 		ret = -ENODEV;
155 		goto done;
156 	}
157 
158 	/* Count the number of pins and groups and reallocate mappings. */
159 	ret = of_property_count_strings(np, pmx->pins_prop_name);
160 	if (ret == -EINVAL) {
161 		num_pins = 0;
162 	} else if (ret < 0) {
163 		dev_err(dev, "Invalid pins list in DT\n");
164 		goto done;
165 	} else {
166 		num_pins = ret;
167 	}
168 
169 	ret = of_property_count_strings(np, pmx->groups_prop_name);
170 	if (ret == -EINVAL) {
171 		num_groups = 0;
172 	} else if (ret < 0) {
173 		dev_err(dev, "Invalid pin groups list in DT\n");
174 		goto done;
175 	} else {
176 		num_groups = ret;
177 	}
178 
179 	if (!num_pins && !num_groups) {
180 		dev_err(dev, "No pin or group provided in DT node\n");
181 		ret = -ENODEV;
182 		goto done;
183 	}
184 
185 	if (function)
186 		nmaps += num_groups;
187 	if (configs)
188 		nmaps += num_pins + num_groups;
189 
190 	maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
191 	if (maps == NULL) {
192 		ret = -ENOMEM;
193 		goto done;
194 	}
195 
196 	*map = maps;
197 	*num_maps = nmaps;
198 
199 	/* Iterate over pins and groups and create the mappings. */
200 	of_property_for_each_string(np, pmx->groups_prop_name, prop, group) {
201 		if (function) {
202 			maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
203 			maps[idx].data.mux.group = group;
204 			maps[idx].data.mux.function = function;
205 			idx++;
206 		}
207 
208 		if (configs) {
209 			ret = sh_pfc_map_add_config(&maps[idx], group,
210 						    PIN_MAP_TYPE_CONFIGS_GROUP,
211 						    configs, num_configs);
212 			if (ret < 0)
213 				goto done;
214 
215 			idx++;
216 		}
217 	}
218 
219 	if (!configs) {
220 		ret = 0;
221 		goto done;
222 	}
223 
224 	of_property_for_each_string(np, pmx->pins_prop_name, prop, pin) {
225 		ret = sh_pfc_map_add_config(&maps[idx], pin,
226 					    PIN_MAP_TYPE_CONFIGS_PIN,
227 					    configs, num_configs);
228 		if (ret < 0)
229 			goto done;
230 
231 		idx++;
232 	}
233 
234 done:
235 	*index = idx;
236 	kfree(configs);
237 	return ret;
238 }
239 
240 static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
241 			       struct pinctrl_map *map, unsigned num_maps)
242 {
243 	unsigned int i;
244 
245 	if (map == NULL)
246 		return;
247 
248 	for (i = 0; i < num_maps; ++i) {
249 		if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
250 		    map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
251 			kfree(map[i].data.configs.configs);
252 	}
253 
254 	kfree(map);
255 }
256 
257 static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
258 				 struct device_node *np,
259 				 struct pinctrl_map **map, unsigned *num_maps)
260 {
261 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
262 	struct device *dev = pmx->pfc->dev;
263 	struct device_node *child;
264 	unsigned int index;
265 	int ret;
266 
267 	*map = NULL;
268 	*num_maps = 0;
269 	index = 0;
270 
271 	for_each_child_of_node(np, child) {
272 		ret = sh_pfc_dt_subnode_to_map(pctldev, child, map, num_maps,
273 					       &index);
274 		if (ret < 0) {
275 			of_node_put(child);
276 			goto done;
277 		}
278 	}
279 
280 	/* If no mapping has been found in child nodes try the config node. */
281 	if (*num_maps == 0) {
282 		ret = sh_pfc_dt_subnode_to_map(pctldev, np, map, num_maps,
283 					       &index);
284 		if (ret < 0)
285 			goto done;
286 	}
287 
288 	if (*num_maps)
289 		return 0;
290 
291 	dev_err(dev, "no mapping found in node %pOF\n", np);
292 	ret = -EINVAL;
293 
294 done:
295 	if (ret < 0)
296 		sh_pfc_dt_free_map(pctldev, *map, *num_maps);
297 
298 	return ret;
299 }
300 #endif /* CONFIG_OF */
301 
302 static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
303 	.get_groups_count	= sh_pfc_get_groups_count,
304 	.get_group_name		= sh_pfc_get_group_name,
305 	.get_group_pins		= sh_pfc_get_group_pins,
306 	.pin_dbg_show		= sh_pfc_pin_dbg_show,
307 #ifdef CONFIG_OF
308 	.dt_node_to_map		= sh_pfc_dt_node_to_map,
309 	.dt_free_map		= sh_pfc_dt_free_map,
310 #endif
311 };
312 
313 static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
314 {
315 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
316 
317 	return pmx->pfc->info->nr_functions;
318 }
319 
320 static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
321 					    unsigned selector)
322 {
323 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
324 
325 	return pmx->pfc->info->functions[selector].name;
326 }
327 
328 static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
329 				      unsigned selector,
330 				      const char * const **groups,
331 				      unsigned * const num_groups)
332 {
333 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
334 
335 	*groups = pmx->pfc->info->functions[selector].groups;
336 	*num_groups = pmx->pfc->info->functions[selector].nr_groups;
337 
338 	return 0;
339 }
340 
341 static int sh_pfc_func_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
342 			       unsigned group)
343 {
344 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
345 	struct sh_pfc *pfc = pmx->pfc;
346 	const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
347 	unsigned long flags;
348 	unsigned int i;
349 	int ret = 0;
350 
351 	dev_dbg(pctldev->dev, "Configuring pin group %s\n", grp->name);
352 
353 	spin_lock_irqsave(&pfc->lock, flags);
354 
355 	for (i = 0; i < grp->nr_pins; ++i) {
356 		int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
357 		struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
358 
359 		/*
360 		 * This driver cannot manage both gpio and mux when the gpio
361 		 * pin is already enabled. So, this function fails.
362 		 */
363 		if (cfg->gpio_enabled) {
364 			ret = -EBUSY;
365 			goto done;
366 		}
367 
368 		ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
369 		if (ret < 0)
370 			goto done;
371 	}
372 
373 	/* All group pins are configured, mark the pins as muxed */
374 	for (i = 0; i < grp->nr_pins; ++i) {
375 		int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
376 		struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
377 
378 		cfg->mux_mark = grp->mux[i];
379 	}
380 
381 done:
382 	spin_unlock_irqrestore(&pfc->lock, flags);
383 	return ret;
384 }
385 
386 static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
387 				      struct pinctrl_gpio_range *range,
388 				      unsigned offset)
389 {
390 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
391 	struct sh_pfc *pfc = pmx->pfc;
392 	int idx = sh_pfc_get_pin_index(pfc, offset);
393 	struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
394 	unsigned long flags;
395 	int ret;
396 
397 	spin_lock_irqsave(&pfc->lock, flags);
398 
399 	if (!pfc->gpio) {
400 		/* If GPIOs are handled externally the pin mux type needs to be
401 		 * set to GPIO here.
402 		 */
403 		const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
404 
405 		ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
406 		if (ret < 0)
407 			goto done;
408 	}
409 
410 	cfg->gpio_enabled = true;
411 
412 	ret = 0;
413 
414 done:
415 	spin_unlock_irqrestore(&pfc->lock, flags);
416 
417 	return ret;
418 }
419 
420 static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
421 				     struct pinctrl_gpio_range *range,
422 				     unsigned offset)
423 {
424 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
425 	struct sh_pfc *pfc = pmx->pfc;
426 	int idx = sh_pfc_get_pin_index(pfc, offset);
427 	struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
428 	unsigned long flags;
429 
430 	spin_lock_irqsave(&pfc->lock, flags);
431 	cfg->gpio_enabled = false;
432 	/* If mux is already set, this configures it here */
433 	if (cfg->mux_mark)
434 		sh_pfc_config_mux(pfc, cfg->mux_mark, PINMUX_TYPE_FUNCTION);
435 	spin_unlock_irqrestore(&pfc->lock, flags);
436 }
437 
438 static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
439 				     struct pinctrl_gpio_range *range,
440 				     unsigned offset, bool input)
441 {
442 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
443 	struct sh_pfc *pfc = pmx->pfc;
444 	int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
445 	int idx = sh_pfc_get_pin_index(pfc, offset);
446 	const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
447 	unsigned long flags;
448 	unsigned int dir;
449 	int ret;
450 
451 	/* Check if the requested direction is supported by the pin. Not all
452 	 * SoCs provide pin config data, so perform the check conditionally.
453 	 */
454 	if (pin->configs) {
455 		dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
456 		if (!(pin->configs & dir))
457 			return -EINVAL;
458 	}
459 
460 	spin_lock_irqsave(&pfc->lock, flags);
461 	ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
462 	spin_unlock_irqrestore(&pfc->lock, flags);
463 	return ret;
464 }
465 
466 static const struct pinmux_ops sh_pfc_pinmux_ops = {
467 	.get_functions_count	= sh_pfc_get_functions_count,
468 	.get_function_name	= sh_pfc_get_function_name,
469 	.get_function_groups	= sh_pfc_get_function_groups,
470 	.set_mux		= sh_pfc_func_set_mux,
471 	.gpio_request_enable	= sh_pfc_gpio_request_enable,
472 	.gpio_disable_free	= sh_pfc_gpio_disable_free,
473 	.gpio_set_direction	= sh_pfc_gpio_set_direction,
474 };
475 
476 static u32 sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc,
477 		unsigned int pin, unsigned int *offset, unsigned int *size)
478 {
479 	const struct pinmux_drive_reg_field *field;
480 	const struct pinmux_drive_reg *reg;
481 	unsigned int i;
482 
483 	for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
484 		for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
485 			field = &reg->fields[i];
486 
487 			if (field->size && field->pin == pin) {
488 				*offset = field->offset;
489 				*size = field->size;
490 
491 				return reg->reg;
492 			}
493 		}
494 	}
495 
496 	return 0;
497 }
498 
499 static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc *pfc,
500 					     unsigned int pin)
501 {
502 	unsigned long flags;
503 	unsigned int offset;
504 	unsigned int size;
505 	u32 reg;
506 	u32 val;
507 
508 	reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
509 	if (!reg)
510 		return -EINVAL;
511 
512 	spin_lock_irqsave(&pfc->lock, flags);
513 	val = sh_pfc_read(pfc, reg);
514 	spin_unlock_irqrestore(&pfc->lock, flags);
515 
516 	val = (val >> offset) & GENMASK(size - 1, 0);
517 
518 	/* Convert the value to mA based on a full drive strength value of 24mA.
519 	 * We can make the full value configurable later if needed.
520 	 */
521 	return (val + 1) * (size == 2 ? 6 : 3);
522 }
523 
524 static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
525 					     unsigned int pin, u16 strength)
526 {
527 	unsigned long flags;
528 	unsigned int offset;
529 	unsigned int size;
530 	unsigned int step;
531 	u32 reg;
532 	u32 val;
533 
534 	reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
535 	if (!reg)
536 		return -EINVAL;
537 
538 	step = size == 2 ? 6 : 3;
539 
540 	if (strength < step || strength > 24)
541 		return -EINVAL;
542 
543 	/* Convert the value from mA based on a full drive strength value of
544 	 * 24mA. We can make the full value configurable later if needed.
545 	 */
546 	strength = strength / step - 1;
547 
548 	spin_lock_irqsave(&pfc->lock, flags);
549 
550 	val = sh_pfc_read(pfc, reg);
551 	val &= ~GENMASK(offset + size - 1, offset);
552 	val |= strength << offset;
553 
554 	sh_pfc_write(pfc, reg, val);
555 
556 	spin_unlock_irqrestore(&pfc->lock, flags);
557 
558 	return 0;
559 }
560 
561 /* Check whether the requested parameter is supported for a pin. */
562 static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
563 				    enum pin_config_param param)
564 {
565 	int idx = sh_pfc_get_pin_index(pfc, _pin);
566 	const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
567 
568 	switch (param) {
569 	case PIN_CONFIG_BIAS_DISABLE:
570 		return pin->configs & SH_PFC_PIN_CFG_PULL_UP_DOWN;
571 
572 	case PIN_CONFIG_BIAS_PULL_UP:
573 		return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
574 
575 	case PIN_CONFIG_BIAS_PULL_DOWN:
576 		return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
577 
578 	case PIN_CONFIG_DRIVE_STRENGTH:
579 		return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
580 
581 	case PIN_CONFIG_POWER_SOURCE:
582 		return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
583 
584 	default:
585 		return false;
586 	}
587 }
588 
589 static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
590 			      unsigned long *config)
591 {
592 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
593 	struct sh_pfc *pfc = pmx->pfc;
594 	enum pin_config_param param = pinconf_to_config_param(*config);
595 	unsigned long flags;
596 	unsigned int arg;
597 
598 	if (!sh_pfc_pinconf_validate(pfc, _pin, param))
599 		return -ENOTSUPP;
600 
601 	switch (param) {
602 	case PIN_CONFIG_BIAS_DISABLE:
603 	case PIN_CONFIG_BIAS_PULL_UP:
604 	case PIN_CONFIG_BIAS_PULL_DOWN: {
605 		unsigned int bias;
606 
607 		if (!pfc->info->ops || !pfc->info->ops->get_bias)
608 			return -ENOTSUPP;
609 
610 		spin_lock_irqsave(&pfc->lock, flags);
611 		bias = pfc->info->ops->get_bias(pfc, _pin);
612 		spin_unlock_irqrestore(&pfc->lock, flags);
613 
614 		if (bias != param)
615 			return -EINVAL;
616 
617 		arg = 0;
618 		break;
619 	}
620 
621 	case PIN_CONFIG_DRIVE_STRENGTH: {
622 		int ret;
623 
624 		ret = sh_pfc_pinconf_get_drive_strength(pfc, _pin);
625 		if (ret < 0)
626 			return ret;
627 
628 		arg = ret;
629 		break;
630 	}
631 
632 	case PIN_CONFIG_POWER_SOURCE: {
633 		u32 pocctrl, val;
634 		int bit;
635 
636 		if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
637 			return -ENOTSUPP;
638 
639 		bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
640 		if (WARN(bit < 0, "invalid pin %#x", _pin))
641 			return bit;
642 
643 		spin_lock_irqsave(&pfc->lock, flags);
644 		val = sh_pfc_read(pfc, pocctrl);
645 		spin_unlock_irqrestore(&pfc->lock, flags);
646 
647 		arg = (val & BIT(bit)) ? 3300 : 1800;
648 		break;
649 	}
650 
651 	default:
652 		return -ENOTSUPP;
653 	}
654 
655 	*config = pinconf_to_config_packed(param, arg);
656 	return 0;
657 }
658 
659 static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
660 			      unsigned long *configs, unsigned num_configs)
661 {
662 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
663 	struct sh_pfc *pfc = pmx->pfc;
664 	enum pin_config_param param;
665 	unsigned long flags;
666 	unsigned int i;
667 
668 	for (i = 0; i < num_configs; i++) {
669 		param = pinconf_to_config_param(configs[i]);
670 
671 		if (!sh_pfc_pinconf_validate(pfc, _pin, param))
672 			return -ENOTSUPP;
673 
674 		switch (param) {
675 		case PIN_CONFIG_BIAS_PULL_UP:
676 		case PIN_CONFIG_BIAS_PULL_DOWN:
677 		case PIN_CONFIG_BIAS_DISABLE:
678 			if (!pfc->info->ops || !pfc->info->ops->set_bias)
679 				return -ENOTSUPP;
680 
681 			spin_lock_irqsave(&pfc->lock, flags);
682 			pfc->info->ops->set_bias(pfc, _pin, param);
683 			spin_unlock_irqrestore(&pfc->lock, flags);
684 
685 			break;
686 
687 		case PIN_CONFIG_DRIVE_STRENGTH: {
688 			unsigned int arg =
689 				pinconf_to_config_argument(configs[i]);
690 			int ret;
691 
692 			ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
693 			if (ret < 0)
694 				return ret;
695 
696 			break;
697 		}
698 
699 		case PIN_CONFIG_POWER_SOURCE: {
700 			unsigned int mV = pinconf_to_config_argument(configs[i]);
701 			u32 pocctrl, val;
702 			int bit;
703 
704 			if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
705 				return -ENOTSUPP;
706 
707 			bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
708 			if (WARN(bit < 0, "invalid pin %#x", _pin))
709 				return bit;
710 
711 			if (mV != 1800 && mV != 3300)
712 				return -EINVAL;
713 
714 			spin_lock_irqsave(&pfc->lock, flags);
715 			val = sh_pfc_read(pfc, pocctrl);
716 			if (mV == 3300)
717 				val |= BIT(bit);
718 			else
719 				val &= ~BIT(bit);
720 			sh_pfc_write(pfc, pocctrl, val);
721 			spin_unlock_irqrestore(&pfc->lock, flags);
722 
723 			break;
724 		}
725 
726 		default:
727 			return -ENOTSUPP;
728 		}
729 	} /* for each config */
730 
731 	return 0;
732 }
733 
734 static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
735 				    unsigned long *configs,
736 				    unsigned num_configs)
737 {
738 	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
739 	const unsigned int *pins;
740 	unsigned int num_pins;
741 	unsigned int i, ret;
742 
743 	pins = pmx->pfc->info->groups[group].pins;
744 	num_pins = pmx->pfc->info->groups[group].nr_pins;
745 
746 	for (i = 0; i < num_pins; ++i) {
747 		ret = sh_pfc_pinconf_set(pctldev, pins[i], configs, num_configs);
748 		if (ret)
749 			return ret;
750 	}
751 
752 	return 0;
753 }
754 
755 static const struct pinconf_ops sh_pfc_pinconf_ops = {
756 	.is_generic			= true,
757 	.pin_config_get			= sh_pfc_pinconf_get,
758 	.pin_config_set			= sh_pfc_pinconf_set,
759 	.pin_config_group_set		= sh_pfc_pinconf_group_set,
760 	.pin_config_config_dbg_show	= pinconf_generic_dump_config,
761 };
762 
763 /* PFC ranges -> pinctrl pin descs */
764 static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
765 {
766 	unsigned int i;
767 
768 	/* Allocate and initialize the pins and configs arrays. */
769 	pmx->pins = devm_kcalloc(pfc->dev,
770 				 pfc->info->nr_pins, sizeof(*pmx->pins),
771 				 GFP_KERNEL);
772 	if (unlikely(!pmx->pins))
773 		return -ENOMEM;
774 
775 	pmx->configs = devm_kcalloc(pfc->dev,
776 				    pfc->info->nr_pins, sizeof(*pmx->configs),
777 				    GFP_KERNEL);
778 	if (unlikely(!pmx->configs))
779 		return -ENOMEM;
780 
781 	for (i = 0; i < pfc->info->nr_pins; ++i) {
782 		const struct sh_pfc_pin *info = &pfc->info->pins[i];
783 		struct pinctrl_pin_desc *pin = &pmx->pins[i];
784 
785 		/* If the pin number is equal to -1 all pins are considered */
786 		pin->number = info->pin != (u16)-1 ? info->pin : i;
787 		pin->name = info->name;
788 	}
789 
790 	return 0;
791 }
792 
793 int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
794 {
795 	struct sh_pfc_pinctrl *pmx;
796 	int ret;
797 
798 	pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
799 	if (unlikely(!pmx))
800 		return -ENOMEM;
801 
802 	pmx->pfc = pfc;
803 
804 	ret = sh_pfc_map_pins(pfc, pmx);
805 	if (ret < 0)
806 		return ret;
807 
808 	pmx->pctl_desc.name = DRV_NAME;
809 	pmx->pctl_desc.owner = THIS_MODULE;
810 	pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
811 	pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
812 	pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
813 	pmx->pctl_desc.pins = pmx->pins;
814 	pmx->pctl_desc.npins = pfc->info->nr_pins;
815 
816 	ret = devm_pinctrl_register_and_init(pfc->dev, &pmx->pctl_desc, pmx,
817 					     &pmx->pctl);
818 	if (ret) {
819 		dev_err(pfc->dev, "could not register: %i\n", ret);
820 
821 		return ret;
822 	}
823 
824 	return pinctrl_enable(pmx->pctl);
825 }
826 
827 unsigned int rcar_pinmux_get_bias(struct sh_pfc *pfc, unsigned int pin)
828 {
829 	const struct pinmux_bias_reg *reg;
830 	unsigned int bit;
831 
832 	reg = sh_pfc_pin_to_bias_reg(pfc, pin, &bit);
833 	if (!reg)
834 		return PIN_CONFIG_BIAS_DISABLE;
835 
836 	if (!(sh_pfc_read(pfc, reg->puen) & BIT(bit)))
837 		return PIN_CONFIG_BIAS_DISABLE;
838 	else if (!reg->pud || (sh_pfc_read(pfc, reg->pud) & BIT(bit)))
839 		return PIN_CONFIG_BIAS_PULL_UP;
840 	else
841 		return PIN_CONFIG_BIAS_PULL_DOWN;
842 }
843 
844 void rcar_pinmux_set_bias(struct sh_pfc *pfc, unsigned int pin,
845 			  unsigned int bias)
846 {
847 	const struct pinmux_bias_reg *reg;
848 	u32 enable, updown;
849 	unsigned int bit;
850 
851 	reg = sh_pfc_pin_to_bias_reg(pfc, pin, &bit);
852 	if (!reg)
853 		return;
854 
855 	enable = sh_pfc_read(pfc, reg->puen) & ~BIT(bit);
856 	if (bias != PIN_CONFIG_BIAS_DISABLE)
857 		enable |= BIT(bit);
858 
859 	if (reg->pud) {
860 		updown = sh_pfc_read(pfc, reg->pud) & ~BIT(bit);
861 		if (bias == PIN_CONFIG_BIAS_PULL_UP)
862 			updown |= BIT(bit);
863 
864 		sh_pfc_write(pfc, reg->pud, updown);
865 	}
866 
867 	sh_pfc_write(pfc, reg->puen, enable);
868 }
869