1 /*
2  * Allwinner A1X SoCs pinctrl driver.
3  *
4  * Copyright (C) 2012 Maxime Ripard
5  *
6  * Maxime Ripard <maxime.ripard@free-electrons.com>
7  *
8  * This file is licensed under the terms of the GNU General Public
9  * License version 2.  This program is licensed "as is" without any
10  * warranty of any kind, whether express or implied.
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/export.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/interrupt.h>
17 #include <linux/io.h>
18 #include <linux/irqchip/chained_irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/of.h>
21 #include <linux/of_clk.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pinctrl/machine.h>
28 #include <linux/pinctrl/pinconf-generic.h>
29 #include <linux/pinctrl/pinconf.h>
30 #include <linux/pinctrl/pinctrl.h>
31 #include <linux/pinctrl/pinmux.h>
32 
33 #include <dt-bindings/pinctrl/sun4i-a10.h>
34 
35 #include "../core.h"
36 #include "pinctrl-sunxi.h"
37 
38 /*
39  * These lock classes tell lockdep that GPIO IRQs are in a different
40  * category than their parents, so it won't report false recursion.
41  */
42 static struct lock_class_key sunxi_pinctrl_irq_lock_class;
43 static struct lock_class_key sunxi_pinctrl_irq_request_class;
44 
45 static struct irq_chip sunxi_pinctrl_edge_irq_chip;
46 static struct irq_chip sunxi_pinctrl_level_irq_chip;
47 
48 /*
49  * The sunXi PIO registers are organized as a series of banks, with registers
50  * for each bank in the following order:
51  *  - Mux config
52  *  - Data value
53  *  - Drive level
54  *  - Pull direction
55  *
56  * Multiple consecutive registers are used for fields wider than one bit.
57  *
58  * The following functions calculate the register and the bit offset to access.
59  * They take a pin number which is relative to the start of the current device.
60  */
61 
62 /*
63  * When using the extended register layout, Bank K does not fit into the
64  * space used for the other banks. Instead it lives at offset 0x500.
65  */
66 static u32 sunxi_bank_offset(const struct sunxi_pinctrl *pctl, u32 pin)
67 {
68 	u32 offset = 0;
69 
70 	if (pin >= PK_BASE) {
71 		pin -= PK_BASE;
72 		offset = PIO_BANK_K_OFFSET;
73 	}
74 
75 	return offset + (pin / PINS_PER_BANK) * pctl->bank_mem_size;
76 }
77 
78 static void sunxi_mux_reg(const struct sunxi_pinctrl *pctl,
79 			  u32 pin, u32 *reg, u32 *shift, u32 *mask)
80 {
81 	u32 offset = pin % PINS_PER_BANK * MUX_FIELD_WIDTH;
82 
83 	*reg   = sunxi_bank_offset(pctl, pin) + MUX_REGS_OFFSET +
84 		 offset / BITS_PER_TYPE(u32) * sizeof(u32);
85 	*shift = offset % BITS_PER_TYPE(u32);
86 	*mask  = (BIT(MUX_FIELD_WIDTH) - 1) << *shift;
87 }
88 
89 static void sunxi_data_reg(const struct sunxi_pinctrl *pctl,
90 			   u32 pin, u32 *reg, u32 *shift, u32 *mask)
91 {
92 	u32 offset = pin % PINS_PER_BANK * DATA_FIELD_WIDTH;
93 
94 	*reg   = sunxi_bank_offset(pctl, pin) + DATA_REGS_OFFSET +
95 		 offset / BITS_PER_TYPE(u32) * sizeof(u32);
96 	*shift = offset % BITS_PER_TYPE(u32);
97 	*mask  = (BIT(DATA_FIELD_WIDTH) - 1) << *shift;
98 }
99 
100 static void sunxi_dlevel_reg(const struct sunxi_pinctrl *pctl,
101 			     u32 pin, u32 *reg, u32 *shift, u32 *mask)
102 {
103 	u32 offset = pin % PINS_PER_BANK * pctl->dlevel_field_width;
104 
105 	*reg   = sunxi_bank_offset(pctl, pin) + DLEVEL_REGS_OFFSET +
106 		 offset / BITS_PER_TYPE(u32) * sizeof(u32);
107 	*shift = offset % BITS_PER_TYPE(u32);
108 	*mask  = (BIT(pctl->dlevel_field_width) - 1) << *shift;
109 }
110 
111 static void sunxi_pull_reg(const struct sunxi_pinctrl *pctl,
112 			   u32 pin, u32 *reg, u32 *shift, u32 *mask)
113 {
114 	u32 offset = pin % PINS_PER_BANK * PULL_FIELD_WIDTH;
115 
116 	*reg   = sunxi_bank_offset(pctl, pin) + pctl->pull_regs_offset +
117 		 offset / BITS_PER_TYPE(u32) * sizeof(u32);
118 	*shift = offset % BITS_PER_TYPE(u32);
119 	*mask  = (BIT(PULL_FIELD_WIDTH) - 1) << *shift;
120 }
121 
122 static struct sunxi_pinctrl_group *
123 sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
124 {
125 	int i;
126 
127 	for (i = 0; i < pctl->ngroups; i++) {
128 		struct sunxi_pinctrl_group *grp = pctl->groups + i;
129 
130 		if (!strcmp(grp->name, group))
131 			return grp;
132 	}
133 
134 	return NULL;
135 }
136 
137 static struct sunxi_pinctrl_function *
138 sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
139 				    const char *name)
140 {
141 	struct sunxi_pinctrl_function *func = pctl->functions;
142 	int i;
143 
144 	for (i = 0; i < pctl->nfunctions; i++) {
145 		if (!func[i].name)
146 			break;
147 
148 		if (!strcmp(func[i].name, name))
149 			return func + i;
150 	}
151 
152 	return NULL;
153 }
154 
155 static struct sunxi_desc_function *
156 sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
157 					 const char *pin_name,
158 					 const char *func_name)
159 {
160 	int i;
161 
162 	for (i = 0; i < pctl->desc->npins; i++) {
163 		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
164 
165 		if (!strcmp(pin->pin.name, pin_name)) {
166 			struct sunxi_desc_function *func = pin->functions;
167 
168 			while (func->name) {
169 				if (!strcmp(func->name, func_name) &&
170 					(!func->variant ||
171 					func->variant & pctl->variant))
172 					return func;
173 
174 				func++;
175 			}
176 		}
177 	}
178 
179 	return NULL;
180 }
181 
182 static struct sunxi_desc_function *
183 sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
184 					const u16 pin_num,
185 					const char *func_name)
186 {
187 	int i;
188 
189 	for (i = 0; i < pctl->desc->npins; i++) {
190 		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
191 
192 		if (pin->pin.number == pin_num) {
193 			struct sunxi_desc_function *func = pin->functions;
194 
195 			while (func->name) {
196 				if (!strcmp(func->name, func_name))
197 					return func;
198 
199 				func++;
200 			}
201 		}
202 	}
203 
204 	return NULL;
205 }
206 
207 static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
208 {
209 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
210 
211 	return pctl->ngroups;
212 }
213 
214 static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
215 					      unsigned group)
216 {
217 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
218 
219 	return pctl->groups[group].name;
220 }
221 
222 static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
223 				      unsigned group,
224 				      const unsigned **pins,
225 				      unsigned *num_pins)
226 {
227 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
228 
229 	*pins = (unsigned *)&pctl->groups[group].pin;
230 	*num_pins = 1;
231 
232 	return 0;
233 }
234 
235 static bool sunxi_pctrl_has_bias_prop(struct device_node *node)
236 {
237 	return of_property_present(node, "bias-pull-up") ||
238 		of_property_present(node, "bias-pull-down") ||
239 		of_property_present(node, "bias-disable") ||
240 		of_property_present(node, "allwinner,pull");
241 }
242 
243 static bool sunxi_pctrl_has_drive_prop(struct device_node *node)
244 {
245 	return of_property_present(node, "drive-strength") ||
246 		of_property_present(node, "allwinner,drive");
247 }
248 
249 static int sunxi_pctrl_parse_bias_prop(struct device_node *node)
250 {
251 	u32 val;
252 
253 	/* Try the new style binding */
254 	if (of_property_present(node, "bias-pull-up"))
255 		return PIN_CONFIG_BIAS_PULL_UP;
256 
257 	if (of_property_present(node, "bias-pull-down"))
258 		return PIN_CONFIG_BIAS_PULL_DOWN;
259 
260 	if (of_property_present(node, "bias-disable"))
261 		return PIN_CONFIG_BIAS_DISABLE;
262 
263 	/* And fall back to the old binding */
264 	if (of_property_read_u32(node, "allwinner,pull", &val))
265 		return -EINVAL;
266 
267 	switch (val) {
268 	case SUN4I_PINCTRL_NO_PULL:
269 		return PIN_CONFIG_BIAS_DISABLE;
270 	case SUN4I_PINCTRL_PULL_UP:
271 		return PIN_CONFIG_BIAS_PULL_UP;
272 	case SUN4I_PINCTRL_PULL_DOWN:
273 		return PIN_CONFIG_BIAS_PULL_DOWN;
274 	}
275 
276 	return -EINVAL;
277 }
278 
279 static int sunxi_pctrl_parse_drive_prop(struct device_node *node)
280 {
281 	u32 val;
282 
283 	/* Try the new style binding */
284 	if (!of_property_read_u32(node, "drive-strength", &val)) {
285 		/* We can't go below 10mA ... */
286 		if (val < 10)
287 			return -EINVAL;
288 
289 		/* ... and only up to 40 mA ... */
290 		if (val > 40)
291 			val = 40;
292 
293 		/* by steps of 10 mA */
294 		return rounddown(val, 10);
295 	}
296 
297 	/* And then fall back to the old binding */
298 	if (of_property_read_u32(node, "allwinner,drive", &val))
299 		return -EINVAL;
300 
301 	return (val + 1) * 10;
302 }
303 
304 static const char *sunxi_pctrl_parse_function_prop(struct device_node *node)
305 {
306 	const char *function;
307 	int ret;
308 
309 	/* Try the generic binding */
310 	ret = of_property_read_string(node, "function", &function);
311 	if (!ret)
312 		return function;
313 
314 	/* And fall back to our legacy one */
315 	ret = of_property_read_string(node, "allwinner,function", &function);
316 	if (!ret)
317 		return function;
318 
319 	return NULL;
320 }
321 
322 static const char *sunxi_pctrl_find_pins_prop(struct device_node *node,
323 					      int *npins)
324 {
325 	int count;
326 
327 	/* Try the generic binding */
328 	count = of_property_count_strings(node, "pins");
329 	if (count > 0) {
330 		*npins = count;
331 		return "pins";
332 	}
333 
334 	/* And fall back to our legacy one */
335 	count = of_property_count_strings(node, "allwinner,pins");
336 	if (count > 0) {
337 		*npins = count;
338 		return "allwinner,pins";
339 	}
340 
341 	return NULL;
342 }
343 
344 static unsigned long *sunxi_pctrl_build_pin_config(struct device_node *node,
345 						   unsigned int *len)
346 {
347 	unsigned long *pinconfig;
348 	unsigned int configlen = 0, idx = 0;
349 	int ret;
350 
351 	if (sunxi_pctrl_has_drive_prop(node))
352 		configlen++;
353 	if (sunxi_pctrl_has_bias_prop(node))
354 		configlen++;
355 
356 	/*
357 	 * If we don't have any configuration, bail out
358 	 */
359 	if (!configlen)
360 		return NULL;
361 
362 	pinconfig = kcalloc(configlen, sizeof(*pinconfig), GFP_KERNEL);
363 	if (!pinconfig)
364 		return ERR_PTR(-ENOMEM);
365 
366 	if (sunxi_pctrl_has_drive_prop(node)) {
367 		int drive = sunxi_pctrl_parse_drive_prop(node);
368 		if (drive < 0) {
369 			ret = drive;
370 			goto err_free;
371 		}
372 
373 		pinconfig[idx++] = pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
374 							  drive);
375 	}
376 
377 	if (sunxi_pctrl_has_bias_prop(node)) {
378 		int pull = sunxi_pctrl_parse_bias_prop(node);
379 		int arg = 0;
380 		if (pull < 0) {
381 			ret = pull;
382 			goto err_free;
383 		}
384 
385 		if (pull != PIN_CONFIG_BIAS_DISABLE)
386 			arg = 1; /* hardware uses weak pull resistors */
387 
388 		pinconfig[idx++] = pinconf_to_config_packed(pull, arg);
389 	}
390 
391 
392 	*len = configlen;
393 	return pinconfig;
394 
395 err_free:
396 	kfree(pinconfig);
397 	return ERR_PTR(ret);
398 }
399 
400 static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
401 				      struct device_node *node,
402 				      struct pinctrl_map **map,
403 				      unsigned *num_maps)
404 {
405 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
406 	unsigned long *pinconfig;
407 	struct property *prop;
408 	const char *function, *pin_prop;
409 	const char *group;
410 	int ret, npins, nmaps, configlen = 0, i = 0;
411 
412 	*map = NULL;
413 	*num_maps = 0;
414 
415 	function = sunxi_pctrl_parse_function_prop(node);
416 	if (!function) {
417 		dev_err(pctl->dev, "missing function property in node %pOFn\n",
418 			node);
419 		return -EINVAL;
420 	}
421 
422 	pin_prop = sunxi_pctrl_find_pins_prop(node, &npins);
423 	if (!pin_prop) {
424 		dev_err(pctl->dev, "missing pins property in node %pOFn\n",
425 			node);
426 		return -EINVAL;
427 	}
428 
429 	/*
430 	 * We have two maps for each pin: one for the function, one
431 	 * for the configuration (bias, strength, etc).
432 	 *
433 	 * We might be slightly overshooting, since we might not have
434 	 * any configuration.
435 	 */
436 	nmaps = npins * 2;
437 	*map = kmalloc_array(nmaps, sizeof(struct pinctrl_map), GFP_KERNEL);
438 	if (!*map)
439 		return -ENOMEM;
440 
441 	pinconfig = sunxi_pctrl_build_pin_config(node, &configlen);
442 	if (IS_ERR(pinconfig)) {
443 		ret = PTR_ERR(pinconfig);
444 		goto err_free_map;
445 	}
446 
447 	of_property_for_each_string(node, pin_prop, prop, group) {
448 		struct sunxi_pinctrl_group *grp =
449 			sunxi_pinctrl_find_group_by_name(pctl, group);
450 
451 		if (!grp) {
452 			dev_err(pctl->dev, "unknown pin %s", group);
453 			continue;
454 		}
455 
456 		if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
457 							      grp->name,
458 							      function)) {
459 			dev_err(pctl->dev, "unsupported function %s on pin %s",
460 				function, group);
461 			continue;
462 		}
463 
464 		(*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
465 		(*map)[i].data.mux.group = group;
466 		(*map)[i].data.mux.function = function;
467 
468 		i++;
469 
470 		if (pinconfig) {
471 			(*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
472 			(*map)[i].data.configs.group_or_pin = group;
473 			(*map)[i].data.configs.configs = pinconfig;
474 			(*map)[i].data.configs.num_configs = configlen;
475 			i++;
476 		}
477 	}
478 
479 	*num_maps = i;
480 
481 	/*
482 	 * We know have the number of maps we need, we can resize our
483 	 * map array
484 	 */
485 	*map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL);
486 	if (!*map)
487 		return -ENOMEM;
488 
489 	return 0;
490 
491 err_free_map:
492 	kfree(*map);
493 	*map = NULL;
494 	return ret;
495 }
496 
497 static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
498 				    struct pinctrl_map *map,
499 				    unsigned num_maps)
500 {
501 	int i;
502 
503 	/* pin config is never in the first map */
504 	for (i = 1; i < num_maps; i++) {
505 		if (map[i].type != PIN_MAP_TYPE_CONFIGS_GROUP)
506 			continue;
507 
508 		/*
509 		 * All the maps share the same pin config,
510 		 * free only the first one we find.
511 		 */
512 		kfree(map[i].data.configs.configs);
513 		break;
514 	}
515 
516 	kfree(map);
517 }
518 
519 static const struct pinctrl_ops sunxi_pctrl_ops = {
520 	.dt_node_to_map		= sunxi_pctrl_dt_node_to_map,
521 	.dt_free_map		= sunxi_pctrl_dt_free_map,
522 	.get_groups_count	= sunxi_pctrl_get_groups_count,
523 	.get_group_name		= sunxi_pctrl_get_group_name,
524 	.get_group_pins		= sunxi_pctrl_get_group_pins,
525 };
526 
527 static int sunxi_pconf_reg(const struct sunxi_pinctrl *pctl,
528 			   u32 pin, enum pin_config_param param,
529 			   u32 *reg, u32 *shift, u32 *mask)
530 {
531 	switch (param) {
532 	case PIN_CONFIG_DRIVE_STRENGTH:
533 		sunxi_dlevel_reg(pctl, pin, reg, shift, mask);
534 		break;
535 
536 	case PIN_CONFIG_BIAS_PULL_UP:
537 	case PIN_CONFIG_BIAS_PULL_DOWN:
538 	case PIN_CONFIG_BIAS_DISABLE:
539 		sunxi_pull_reg(pctl, pin, reg, shift, mask);
540 		break;
541 
542 	default:
543 		return -ENOTSUPP;
544 	}
545 
546 	return 0;
547 }
548 
549 static int sunxi_pconf_get(struct pinctrl_dev *pctldev, unsigned pin,
550 			   unsigned long *config)
551 {
552 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
553 	enum pin_config_param param = pinconf_to_config_param(*config);
554 	u32 reg, shift, mask, val;
555 	u16 arg;
556 	int ret;
557 
558 	pin -= pctl->desc->pin_base;
559 
560 	ret = sunxi_pconf_reg(pctl, pin, param, &reg, &shift, &mask);
561 	if (ret < 0)
562 		return ret;
563 
564 	val = (readl(pctl->membase + reg) & mask) >> shift;
565 
566 	switch (pinconf_to_config_param(*config)) {
567 	case PIN_CONFIG_DRIVE_STRENGTH:
568 		arg = (val + 1) * 10;
569 		break;
570 
571 	case PIN_CONFIG_BIAS_PULL_UP:
572 		if (val != SUN4I_PINCTRL_PULL_UP)
573 			return -EINVAL;
574 		arg = 1; /* hardware is weak pull-up */
575 		break;
576 
577 	case PIN_CONFIG_BIAS_PULL_DOWN:
578 		if (val != SUN4I_PINCTRL_PULL_DOWN)
579 			return -EINVAL;
580 		arg = 1; /* hardware is weak pull-down */
581 		break;
582 
583 	case PIN_CONFIG_BIAS_DISABLE:
584 		if (val != SUN4I_PINCTRL_NO_PULL)
585 			return -EINVAL;
586 		arg = 0;
587 		break;
588 
589 	default:
590 		/* sunxi_pconf_reg should catch anything unsupported */
591 		WARN_ON(1);
592 		return -ENOTSUPP;
593 	}
594 
595 	*config = pinconf_to_config_packed(param, arg);
596 
597 	return 0;
598 }
599 
600 static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
601 				 unsigned group,
602 				 unsigned long *config)
603 {
604 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
605 	struct sunxi_pinctrl_group *g = &pctl->groups[group];
606 
607 	/* We only support 1 pin per group. Chain it to the pin callback */
608 	return sunxi_pconf_get(pctldev, g->pin, config);
609 }
610 
611 static int sunxi_pconf_set(struct pinctrl_dev *pctldev, unsigned pin,
612 			   unsigned long *configs, unsigned num_configs)
613 {
614 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
615 	int i;
616 
617 	pin -= pctl->desc->pin_base;
618 
619 	for (i = 0; i < num_configs; i++) {
620 		u32 arg, reg, shift, mask, val;
621 		enum pin_config_param param;
622 		unsigned long flags;
623 		int ret;
624 
625 		param = pinconf_to_config_param(configs[i]);
626 		arg = pinconf_to_config_argument(configs[i]);
627 
628 		ret = sunxi_pconf_reg(pctl, pin, param, &reg, &shift, &mask);
629 		if (ret < 0)
630 			return ret;
631 
632 		switch (param) {
633 		case PIN_CONFIG_DRIVE_STRENGTH:
634 			if (arg < 10 || arg > 40)
635 				return -EINVAL;
636 			/*
637 			 * We convert from mA to what the register expects:
638 			 *   0: 10mA
639 			 *   1: 20mA
640 			 *   2: 30mA
641 			 *   3: 40mA
642 			 */
643 			val = arg / 10 - 1;
644 			break;
645 		case PIN_CONFIG_BIAS_DISABLE:
646 			val = 0;
647 			break;
648 		case PIN_CONFIG_BIAS_PULL_UP:
649 			if (arg == 0)
650 				return -EINVAL;
651 			val = 1;
652 			break;
653 		case PIN_CONFIG_BIAS_PULL_DOWN:
654 			if (arg == 0)
655 				return -EINVAL;
656 			val = 2;
657 			break;
658 		default:
659 			/* sunxi_pconf_reg should catch anything unsupported */
660 			WARN_ON(1);
661 			return -ENOTSUPP;
662 		}
663 
664 		raw_spin_lock_irqsave(&pctl->lock, flags);
665 		writel((readl(pctl->membase + reg) & ~mask) | val << shift,
666 		       pctl->membase + reg);
667 		raw_spin_unlock_irqrestore(&pctl->lock, flags);
668 	} /* for each config */
669 
670 	return 0;
671 }
672 
673 static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
674 				 unsigned long *configs, unsigned num_configs)
675 {
676 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
677 	struct sunxi_pinctrl_group *g = &pctl->groups[group];
678 
679 	/* We only support 1 pin per group. Chain it to the pin callback */
680 	return sunxi_pconf_set(pctldev, g->pin, configs, num_configs);
681 }
682 
683 static const struct pinconf_ops sunxi_pconf_ops = {
684 	.is_generic		= true,
685 	.pin_config_get		= sunxi_pconf_get,
686 	.pin_config_set		= sunxi_pconf_set,
687 	.pin_config_group_get	= sunxi_pconf_group_get,
688 	.pin_config_group_set	= sunxi_pconf_group_set,
689 };
690 
691 static int sunxi_pinctrl_set_io_bias_cfg(struct sunxi_pinctrl *pctl,
692 					 unsigned pin,
693 					 struct regulator *supply)
694 {
695 	unsigned short bank;
696 	unsigned long flags;
697 	u32 val, reg;
698 	int uV;
699 
700 	if (!pctl->desc->io_bias_cfg_variant)
701 		return 0;
702 
703 	uV = regulator_get_voltage(supply);
704 	if (uV < 0)
705 		return uV;
706 
707 	/* Might be dummy regulator with no voltage set */
708 	if (uV == 0)
709 		return 0;
710 
711 	pin -= pctl->desc->pin_base;
712 	bank = pin / PINS_PER_BANK;
713 
714 	switch (pctl->desc->io_bias_cfg_variant) {
715 	case BIAS_VOLTAGE_GRP_CONFIG:
716 		/*
717 		 * Configured value must be equal or greater to actual
718 		 * voltage.
719 		 */
720 		if (uV <= 1800000)
721 			val = 0x0; /* 1.8V */
722 		else if (uV <= 2500000)
723 			val = 0x6; /* 2.5V */
724 		else if (uV <= 2800000)
725 			val = 0x9; /* 2.8V */
726 		else if (uV <= 3000000)
727 			val = 0xA; /* 3.0V */
728 		else
729 			val = 0xD; /* 3.3V */
730 
731 		reg = readl(pctl->membase + sunxi_grp_config_reg(pin));
732 		reg &= ~IO_BIAS_MASK;
733 		writel(reg | val, pctl->membase + sunxi_grp_config_reg(pin));
734 		return 0;
735 	case BIAS_VOLTAGE_PIO_POW_MODE_CTL:
736 		val = uV > 1800000 && uV <= 2500000 ? BIT(bank) : 0;
737 
738 		raw_spin_lock_irqsave(&pctl->lock, flags);
739 		reg = readl(pctl->membase + pctl->pow_mod_sel_offset +
740 			    PIO_POW_MOD_CTL_OFS);
741 		reg &= ~BIT(bank);
742 		writel(reg | val, pctl->membase + pctl->pow_mod_sel_offset +
743 		       PIO_POW_MOD_CTL_OFS);
744 		raw_spin_unlock_irqrestore(&pctl->lock, flags);
745 
746 		fallthrough;
747 	case BIAS_VOLTAGE_PIO_POW_MODE_SEL:
748 		val = uV <= 1800000 ? 1 : 0;
749 
750 		raw_spin_lock_irqsave(&pctl->lock, flags);
751 		reg = readl(pctl->membase + pctl->pow_mod_sel_offset);
752 		reg &= ~(1 << bank);
753 		writel(reg | val << bank,
754 		       pctl->membase + pctl->pow_mod_sel_offset);
755 		raw_spin_unlock_irqrestore(&pctl->lock, flags);
756 		return 0;
757 	default:
758 		return -EINVAL;
759 	}
760 }
761 
762 static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
763 {
764 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
765 
766 	return pctl->nfunctions;
767 }
768 
769 static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
770 					   unsigned function)
771 {
772 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
773 
774 	return pctl->functions[function].name;
775 }
776 
777 static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
778 				     unsigned function,
779 				     const char * const **groups,
780 				     unsigned * const num_groups)
781 {
782 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
783 
784 	*groups = pctl->functions[function].groups;
785 	*num_groups = pctl->functions[function].ngroups;
786 
787 	return 0;
788 }
789 
790 static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
791 				 unsigned pin,
792 				 u8 config)
793 {
794 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
795 	u32 reg, shift, mask;
796 	unsigned long flags;
797 
798 	pin -= pctl->desc->pin_base;
799 	sunxi_mux_reg(pctl, pin, &reg, &shift, &mask);
800 
801 	raw_spin_lock_irqsave(&pctl->lock, flags);
802 
803 	writel((readl(pctl->membase + reg) & ~mask) | config << shift,
804 	       pctl->membase + reg);
805 
806 	raw_spin_unlock_irqrestore(&pctl->lock, flags);
807 }
808 
809 static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
810 			     unsigned function,
811 			     unsigned group)
812 {
813 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
814 	struct sunxi_pinctrl_group *g = pctl->groups + group;
815 	struct sunxi_pinctrl_function *func = pctl->functions + function;
816 	struct sunxi_desc_function *desc =
817 		sunxi_pinctrl_desc_find_function_by_name(pctl,
818 							 g->name,
819 							 func->name);
820 
821 	if (!desc)
822 		return -EINVAL;
823 
824 	sunxi_pmx_set(pctldev, g->pin, desc->muxval);
825 
826 	return 0;
827 }
828 
829 static int
830 sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
831 			struct pinctrl_gpio_range *range,
832 			unsigned offset,
833 			bool input)
834 {
835 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
836 	struct sunxi_desc_function *desc;
837 	const char *func;
838 
839 	if (input)
840 		func = "gpio_in";
841 	else
842 		func = "gpio_out";
843 
844 	desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
845 	if (!desc)
846 		return -EINVAL;
847 
848 	sunxi_pmx_set(pctldev, offset, desc->muxval);
849 
850 	return 0;
851 }
852 
853 static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
854 {
855 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
856 	unsigned short bank = offset / PINS_PER_BANK;
857 	unsigned short bank_offset = bank - pctl->desc->pin_base /
858 					    PINS_PER_BANK;
859 	struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
860 	struct regulator *reg = s_reg->regulator;
861 	char supply[16];
862 	int ret;
863 
864 	if (WARN_ON_ONCE(bank_offset >= ARRAY_SIZE(pctl->regulators)))
865 		return -EINVAL;
866 
867 	if (reg) {
868 		refcount_inc(&s_reg->refcount);
869 		return 0;
870 	}
871 
872 	snprintf(supply, sizeof(supply), "vcc-p%c", 'a' + bank);
873 	reg = regulator_get(pctl->dev, supply);
874 	if (IS_ERR(reg))
875 		return dev_err_probe(pctl->dev, PTR_ERR(reg),
876 				     "Couldn't get bank P%c regulator\n",
877 				     'A' + bank);
878 
879 	ret = regulator_enable(reg);
880 	if (ret) {
881 		dev_err(pctl->dev,
882 			"Couldn't enable bank P%c regulator\n", 'A' + bank);
883 		goto out;
884 	}
885 
886 	sunxi_pinctrl_set_io_bias_cfg(pctl, offset, reg);
887 
888 	s_reg->regulator = reg;
889 	refcount_set(&s_reg->refcount, 1);
890 
891 	return 0;
892 
893 out:
894 	regulator_put(s_reg->regulator);
895 
896 	return ret;
897 }
898 
899 static int sunxi_pmx_free(struct pinctrl_dev *pctldev, unsigned offset)
900 {
901 	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
902 	unsigned short bank = offset / PINS_PER_BANK;
903 	unsigned short bank_offset = bank - pctl->desc->pin_base /
904 					    PINS_PER_BANK;
905 	struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
906 
907 	if (!refcount_dec_and_test(&s_reg->refcount))
908 		return 0;
909 
910 	regulator_disable(s_reg->regulator);
911 	regulator_put(s_reg->regulator);
912 	s_reg->regulator = NULL;
913 
914 	return 0;
915 }
916 
917 static const struct pinmux_ops sunxi_pmx_ops = {
918 	.get_functions_count	= sunxi_pmx_get_funcs_cnt,
919 	.get_function_name	= sunxi_pmx_get_func_name,
920 	.get_function_groups	= sunxi_pmx_get_func_groups,
921 	.set_mux		= sunxi_pmx_set_mux,
922 	.gpio_set_direction	= sunxi_pmx_gpio_set_direction,
923 	.request		= sunxi_pmx_request,
924 	.free			= sunxi_pmx_free,
925 	.strict			= true,
926 };
927 
928 static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
929 					unsigned offset)
930 {
931 	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
932 
933 	return sunxi_pmx_gpio_set_direction(pctl->pctl_dev, NULL,
934 					    chip->base + offset, true);
935 }
936 
937 static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
938 {
939 	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
940 	bool set_mux = pctl->desc->irq_read_needs_mux &&
941 		gpiochip_line_is_irq(chip, offset);
942 	u32 pin = offset + chip->base;
943 	u32 reg, shift, mask, val;
944 
945 	sunxi_data_reg(pctl, offset, &reg, &shift, &mask);
946 
947 	if (set_mux)
948 		sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT);
949 
950 	val = (readl(pctl->membase + reg) & mask) >> shift;
951 
952 	if (set_mux)
953 		sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ);
954 
955 	return val;
956 }
957 
958 static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
959 				unsigned offset, int value)
960 {
961 	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
962 	u32 reg, shift, mask, val;
963 	unsigned long flags;
964 
965 	sunxi_data_reg(pctl, offset, &reg, &shift, &mask);
966 
967 	raw_spin_lock_irqsave(&pctl->lock, flags);
968 
969 	val = readl(pctl->membase + reg);
970 
971 	if (value)
972 		val |= mask;
973 	else
974 		val &= ~mask;
975 
976 	writel(val, pctl->membase + reg);
977 
978 	raw_spin_unlock_irqrestore(&pctl->lock, flags);
979 }
980 
981 static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
982 					unsigned offset, int value)
983 {
984 	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
985 
986 	sunxi_pinctrl_gpio_set(chip, offset, value);
987 	return sunxi_pmx_gpio_set_direction(pctl->pctl_dev, NULL,
988 					    chip->base + offset, false);
989 }
990 
991 static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
992 				const struct of_phandle_args *gpiospec,
993 				u32 *flags)
994 {
995 	int pin, base;
996 
997 	base = PINS_PER_BANK * gpiospec->args[0];
998 	pin = base + gpiospec->args[1];
999 
1000 	if (pin > gc->ngpio)
1001 		return -EINVAL;
1002 
1003 	if (flags)
1004 		*flags = gpiospec->args[2];
1005 
1006 	return pin;
1007 }
1008 
1009 static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1010 {
1011 	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
1012 	struct sunxi_desc_function *desc;
1013 	unsigned pinnum = pctl->desc->pin_base + offset;
1014 	unsigned irqnum;
1015 
1016 	if (offset >= chip->ngpio)
1017 		return -ENXIO;
1018 
1019 	desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
1020 	if (!desc)
1021 		return -EINVAL;
1022 
1023 	irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
1024 
1025 	dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n",
1026 		chip->label, offset + chip->base, irqnum);
1027 
1028 	return irq_find_mapping(pctl->domain, irqnum);
1029 }
1030 
1031 static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
1032 {
1033 	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1034 	struct sunxi_desc_function *func;
1035 	int ret;
1036 
1037 	func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
1038 					pctl->irq_array[d->hwirq], "irq");
1039 	if (!func)
1040 		return -EINVAL;
1041 
1042 	ret = gpiochip_lock_as_irq(pctl->chip,
1043 			pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
1044 	if (ret) {
1045 		dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
1046 			irqd_to_hwirq(d));
1047 		return ret;
1048 	}
1049 
1050 	/* Change muxing to INT mode */
1051 	sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
1052 
1053 	return 0;
1054 }
1055 
1056 static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
1057 {
1058 	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1059 
1060 	gpiochip_unlock_as_irq(pctl->chip,
1061 			      pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
1062 }
1063 
1064 static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
1065 {
1066 	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1067 	u32 reg = sunxi_irq_cfg_reg(pctl->desc, d->hwirq);
1068 	u8 index = sunxi_irq_cfg_offset(d->hwirq);
1069 	unsigned long flags;
1070 	u32 regval;
1071 	u8 mode;
1072 
1073 	switch (type) {
1074 	case IRQ_TYPE_EDGE_RISING:
1075 		mode = IRQ_EDGE_RISING;
1076 		break;
1077 	case IRQ_TYPE_EDGE_FALLING:
1078 		mode = IRQ_EDGE_FALLING;
1079 		break;
1080 	case IRQ_TYPE_EDGE_BOTH:
1081 		mode = IRQ_EDGE_BOTH;
1082 		break;
1083 	case IRQ_TYPE_LEVEL_HIGH:
1084 		mode = IRQ_LEVEL_HIGH;
1085 		break;
1086 	case IRQ_TYPE_LEVEL_LOW:
1087 		mode = IRQ_LEVEL_LOW;
1088 		break;
1089 	default:
1090 		return -EINVAL;
1091 	}
1092 
1093 	raw_spin_lock_irqsave(&pctl->lock, flags);
1094 
1095 	if (type & IRQ_TYPE_LEVEL_MASK)
1096 		irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
1097 						 handle_fasteoi_irq, NULL);
1098 	else
1099 		irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
1100 						 handle_edge_irq, NULL);
1101 
1102 	regval = readl(pctl->membase + reg);
1103 	regval &= ~(IRQ_CFG_IRQ_MASK << index);
1104 	writel(regval | (mode << index), pctl->membase + reg);
1105 
1106 	raw_spin_unlock_irqrestore(&pctl->lock, flags);
1107 
1108 	return 0;
1109 }
1110 
1111 static void sunxi_pinctrl_irq_ack(struct irq_data *d)
1112 {
1113 	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1114 	u32 status_reg = sunxi_irq_status_reg(pctl->desc, d->hwirq);
1115 	u8 status_idx = sunxi_irq_status_offset(d->hwirq);
1116 
1117 	/* Clear the IRQ */
1118 	writel(1 << status_idx, pctl->membase + status_reg);
1119 }
1120 
1121 static void sunxi_pinctrl_irq_mask(struct irq_data *d)
1122 {
1123 	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1124 	u32 reg = sunxi_irq_ctrl_reg(pctl->desc, d->hwirq);
1125 	u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
1126 	unsigned long flags;
1127 	u32 val;
1128 
1129 	raw_spin_lock_irqsave(&pctl->lock, flags);
1130 
1131 	/* Mask the IRQ */
1132 	val = readl(pctl->membase + reg);
1133 	writel(val & ~(1 << idx), pctl->membase + reg);
1134 
1135 	raw_spin_unlock_irqrestore(&pctl->lock, flags);
1136 }
1137 
1138 static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
1139 {
1140 	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1141 	u32 reg = sunxi_irq_ctrl_reg(pctl->desc, d->hwirq);
1142 	u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
1143 	unsigned long flags;
1144 	u32 val;
1145 
1146 	raw_spin_lock_irqsave(&pctl->lock, flags);
1147 
1148 	/* Unmask the IRQ */
1149 	val = readl(pctl->membase + reg);
1150 	writel(val | (1 << idx), pctl->membase + reg);
1151 
1152 	raw_spin_unlock_irqrestore(&pctl->lock, flags);
1153 }
1154 
1155 static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
1156 {
1157 	sunxi_pinctrl_irq_ack(d);
1158 	sunxi_pinctrl_irq_unmask(d);
1159 }
1160 
1161 static int sunxi_pinctrl_irq_set_wake(struct irq_data *d, unsigned int on)
1162 {
1163 	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1164 	u8 bank = d->hwirq / IRQ_PER_BANK;
1165 
1166 	return irq_set_irq_wake(pctl->irq[bank], on);
1167 }
1168 
1169 static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
1170 	.name		= "sunxi_pio_edge",
1171 	.irq_ack	= sunxi_pinctrl_irq_ack,
1172 	.irq_mask	= sunxi_pinctrl_irq_mask,
1173 	.irq_unmask	= sunxi_pinctrl_irq_unmask,
1174 	.irq_request_resources = sunxi_pinctrl_irq_request_resources,
1175 	.irq_release_resources = sunxi_pinctrl_irq_release_resources,
1176 	.irq_set_type	= sunxi_pinctrl_irq_set_type,
1177 	.irq_set_wake	= sunxi_pinctrl_irq_set_wake,
1178 	.flags		= IRQCHIP_MASK_ON_SUSPEND,
1179 };
1180 
1181 static struct irq_chip sunxi_pinctrl_level_irq_chip = {
1182 	.name		= "sunxi_pio_level",
1183 	.irq_eoi	= sunxi_pinctrl_irq_ack,
1184 	.irq_mask	= sunxi_pinctrl_irq_mask,
1185 	.irq_unmask	= sunxi_pinctrl_irq_unmask,
1186 	/* Define irq_enable / disable to avoid spurious irqs for drivers
1187 	 * using these to suppress irqs while they clear the irq source */
1188 	.irq_enable	= sunxi_pinctrl_irq_ack_unmask,
1189 	.irq_disable	= sunxi_pinctrl_irq_mask,
1190 	.irq_request_resources = sunxi_pinctrl_irq_request_resources,
1191 	.irq_release_resources = sunxi_pinctrl_irq_release_resources,
1192 	.irq_set_type	= sunxi_pinctrl_irq_set_type,
1193 	.irq_set_wake	= sunxi_pinctrl_irq_set_wake,
1194 	.flags		= IRQCHIP_EOI_THREADED |
1195 			  IRQCHIP_MASK_ON_SUSPEND |
1196 			  IRQCHIP_EOI_IF_HANDLED,
1197 };
1198 
1199 static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
1200 				      struct device_node *node,
1201 				      const u32 *intspec,
1202 				      unsigned int intsize,
1203 				      unsigned long *out_hwirq,
1204 				      unsigned int *out_type)
1205 {
1206 	struct sunxi_pinctrl *pctl = d->host_data;
1207 	struct sunxi_desc_function *desc;
1208 	int pin, base;
1209 
1210 	if (intsize < 3)
1211 		return -EINVAL;
1212 
1213 	base = PINS_PER_BANK * intspec[0];
1214 	pin = pctl->desc->pin_base + base + intspec[1];
1215 
1216 	desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
1217 	if (!desc)
1218 		return -EINVAL;
1219 
1220 	*out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
1221 	*out_type = intspec[2];
1222 
1223 	return 0;
1224 }
1225 
1226 static const struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
1227 	.xlate		= sunxi_pinctrl_irq_of_xlate,
1228 };
1229 
1230 static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
1231 {
1232 	unsigned int irq = irq_desc_get_irq(desc);
1233 	struct irq_chip *chip = irq_desc_get_chip(desc);
1234 	struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
1235 	unsigned long bank, reg, val;
1236 
1237 	for (bank = 0; bank < pctl->desc->irq_banks; bank++)
1238 		if (irq == pctl->irq[bank])
1239 			break;
1240 
1241 	WARN_ON(bank == pctl->desc->irq_banks);
1242 
1243 	chained_irq_enter(chip, desc);
1244 
1245 	reg = sunxi_irq_status_reg_from_bank(pctl->desc, bank);
1246 	val = readl(pctl->membase + reg);
1247 
1248 	if (val) {
1249 		int irqoffset;
1250 
1251 		for_each_set_bit(irqoffset, &val, IRQ_PER_BANK)
1252 			generic_handle_domain_irq(pctl->domain,
1253 						  bank * IRQ_PER_BANK + irqoffset);
1254 	}
1255 
1256 	chained_irq_exit(chip, desc);
1257 }
1258 
1259 static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
1260 					const char *name)
1261 {
1262 	struct sunxi_pinctrl_function *func = pctl->functions;
1263 
1264 	while (func->name) {
1265 		/* function already there */
1266 		if (strcmp(func->name, name) == 0) {
1267 			func->ngroups++;
1268 			return -EEXIST;
1269 		}
1270 		func++;
1271 	}
1272 
1273 	func->name = name;
1274 	func->ngroups = 1;
1275 
1276 	pctl->nfunctions++;
1277 
1278 	return 0;
1279 }
1280 
1281 static int sunxi_pinctrl_build_state(struct platform_device *pdev)
1282 {
1283 	struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
1284 	void *ptr;
1285 	int i;
1286 
1287 	/*
1288 	 * Allocate groups
1289 	 *
1290 	 * We assume that the number of groups is the number of pins
1291 	 * given in the data array.
1292 
1293 	 * This will not always be true, since some pins might not be
1294 	 * available in the current variant, but fortunately for us,
1295 	 * this means that the number of pins is the maximum group
1296 	 * number we will ever see.
1297 	 */
1298 	pctl->groups = devm_kcalloc(&pdev->dev,
1299 				    pctl->desc->npins, sizeof(*pctl->groups),
1300 				    GFP_KERNEL);
1301 	if (!pctl->groups)
1302 		return -ENOMEM;
1303 
1304 	for (i = 0; i < pctl->desc->npins; i++) {
1305 		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1306 		struct sunxi_pinctrl_group *group = pctl->groups + pctl->ngroups;
1307 
1308 		if (pin->variant && !(pctl->variant & pin->variant))
1309 			continue;
1310 
1311 		group->name = pin->pin.name;
1312 		group->pin = pin->pin.number;
1313 
1314 		/* And now we count the actual number of pins / groups */
1315 		pctl->ngroups++;
1316 	}
1317 
1318 	/*
1319 	 * Find an upper bound for the maximum number of functions: in
1320 	 * the worst case we have gpio_in, gpio_out, irq and up to seven
1321 	 * special functions per pin, plus one entry for the sentinel.
1322 	 * We'll reallocate that later anyway.
1323 	 */
1324 	pctl->functions = kcalloc(7 * pctl->ngroups + 4,
1325 				  sizeof(*pctl->functions),
1326 				  GFP_KERNEL);
1327 	if (!pctl->functions)
1328 		return -ENOMEM;
1329 
1330 	/* Count functions and their associated groups */
1331 	for (i = 0; i < pctl->desc->npins; i++) {
1332 		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1333 		struct sunxi_desc_function *func;
1334 
1335 		if (pin->variant && !(pctl->variant & pin->variant))
1336 			continue;
1337 
1338 		for (func = pin->functions; func->name; func++) {
1339 			if (func->variant && !(pctl->variant & func->variant))
1340 				continue;
1341 
1342 			/* Create interrupt mapping while we're at it */
1343 			if (!strcmp(func->name, "irq")) {
1344 				int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
1345 				pctl->irq_array[irqnum] = pin->pin.number;
1346 			}
1347 
1348 			sunxi_pinctrl_add_function(pctl, func->name);
1349 		}
1350 	}
1351 
1352 	/* And now allocated and fill the array for real */
1353 	ptr = krealloc(pctl->functions,
1354 		       pctl->nfunctions * sizeof(*pctl->functions),
1355 		       GFP_KERNEL);
1356 	if (!ptr) {
1357 		kfree(pctl->functions);
1358 		pctl->functions = NULL;
1359 		return -ENOMEM;
1360 	}
1361 	pctl->functions = ptr;
1362 
1363 	for (i = 0; i < pctl->desc->npins; i++) {
1364 		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1365 		struct sunxi_desc_function *func;
1366 
1367 		if (pin->variant && !(pctl->variant & pin->variant))
1368 			continue;
1369 
1370 		for (func = pin->functions; func->name; func++) {
1371 			struct sunxi_pinctrl_function *func_item;
1372 			const char **func_grp;
1373 
1374 			if (func->variant && !(pctl->variant & func->variant))
1375 				continue;
1376 
1377 			func_item = sunxi_pinctrl_find_function_by_name(pctl,
1378 									func->name);
1379 			if (!func_item) {
1380 				kfree(pctl->functions);
1381 				return -EINVAL;
1382 			}
1383 
1384 			if (!func_item->groups) {
1385 				func_item->groups =
1386 					devm_kcalloc(&pdev->dev,
1387 						     func_item->ngroups,
1388 						     sizeof(*func_item->groups),
1389 						     GFP_KERNEL);
1390 				if (!func_item->groups) {
1391 					kfree(pctl->functions);
1392 					return -ENOMEM;
1393 				}
1394 			}
1395 
1396 			func_grp = func_item->groups;
1397 			while (*func_grp)
1398 				func_grp++;
1399 
1400 			*func_grp = pin->pin.name;
1401 		}
1402 	}
1403 
1404 	return 0;
1405 }
1406 
1407 static int sunxi_pinctrl_get_debounce_div(struct clk *clk, int freq, int *diff)
1408 {
1409 	unsigned long clock = clk_get_rate(clk);
1410 	unsigned int best_diff, best_div;
1411 	int i;
1412 
1413 	best_diff = abs(freq - clock);
1414 	best_div = 0;
1415 
1416 	for (i = 1; i < 8; i++) {
1417 		int cur_diff = abs(freq - (clock >> i));
1418 
1419 		if (cur_diff < best_diff) {
1420 			best_diff = cur_diff;
1421 			best_div = i;
1422 		}
1423 	}
1424 
1425 	*diff = best_diff;
1426 	return best_div;
1427 }
1428 
1429 static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
1430 					struct device_node *node)
1431 {
1432 	unsigned int hosc_diff, losc_diff;
1433 	unsigned int hosc_div, losc_div;
1434 	struct clk *hosc, *losc;
1435 	u8 div, src;
1436 	int i, ret;
1437 
1438 	/* Deal with old DTs that didn't have the oscillators */
1439 	if (of_clk_get_parent_count(node) != 3)
1440 		return 0;
1441 
1442 	/* If we don't have any setup, bail out */
1443 	if (!of_property_present(node, "input-debounce"))
1444 		return 0;
1445 
1446 	losc = devm_clk_get(pctl->dev, "losc");
1447 	if (IS_ERR(losc))
1448 		return PTR_ERR(losc);
1449 
1450 	hosc = devm_clk_get(pctl->dev, "hosc");
1451 	if (IS_ERR(hosc))
1452 		return PTR_ERR(hosc);
1453 
1454 	for (i = 0; i < pctl->desc->irq_banks; i++) {
1455 		unsigned long debounce_freq;
1456 		u32 debounce;
1457 
1458 		ret = of_property_read_u32_index(node, "input-debounce",
1459 						 i, &debounce);
1460 		if (ret)
1461 			return ret;
1462 
1463 		if (!debounce)
1464 			continue;
1465 
1466 		debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce);
1467 		losc_div = sunxi_pinctrl_get_debounce_div(losc,
1468 							  debounce_freq,
1469 							  &losc_diff);
1470 
1471 		hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
1472 							  debounce_freq,
1473 							  &hosc_diff);
1474 
1475 		if (hosc_diff < losc_diff) {
1476 			div = hosc_div;
1477 			src = 1;
1478 		} else {
1479 			div = losc_div;
1480 			src = 0;
1481 		}
1482 
1483 		writel(src | div << 4,
1484 		       pctl->membase +
1485 		       sunxi_irq_debounce_reg_from_bank(pctl->desc, i));
1486 	}
1487 
1488 	return 0;
1489 }
1490 
1491 int sunxi_pinctrl_init_with_flags(struct platform_device *pdev,
1492 				  const struct sunxi_pinctrl_desc *desc,
1493 				  unsigned long flags)
1494 {
1495 	struct device_node *node = pdev->dev.of_node;
1496 	struct pinctrl_desc *pctrl_desc;
1497 	struct pinctrl_pin_desc *pins;
1498 	struct sunxi_pinctrl *pctl;
1499 	struct pinmux_ops *pmxops;
1500 	int i, ret, last_pin, pin_idx;
1501 	struct clk *clk;
1502 
1503 	pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
1504 	if (!pctl)
1505 		return -ENOMEM;
1506 	platform_set_drvdata(pdev, pctl);
1507 
1508 	raw_spin_lock_init(&pctl->lock);
1509 
1510 	pctl->membase = devm_platform_ioremap_resource(pdev, 0);
1511 	if (IS_ERR(pctl->membase))
1512 		return PTR_ERR(pctl->membase);
1513 
1514 	pctl->dev = &pdev->dev;
1515 	pctl->desc = desc;
1516 	pctl->variant = flags & SUNXI_PINCTRL_VARIANT_MASK;
1517 	if (flags & SUNXI_PINCTRL_NEW_REG_LAYOUT) {
1518 		pctl->bank_mem_size = D1_BANK_MEM_SIZE;
1519 		pctl->pull_regs_offset = D1_PULL_REGS_OFFSET;
1520 		pctl->dlevel_field_width = D1_DLEVEL_FIELD_WIDTH;
1521 	} else {
1522 		pctl->bank_mem_size = BANK_MEM_SIZE;
1523 		pctl->pull_regs_offset = PULL_REGS_OFFSET;
1524 		pctl->dlevel_field_width = DLEVEL_FIELD_WIDTH;
1525 	}
1526 	if (flags & SUNXI_PINCTRL_ELEVEN_BANKS)
1527 		pctl->pow_mod_sel_offset = PIO_11B_POW_MOD_SEL_REG;
1528 	else
1529 		pctl->pow_mod_sel_offset = PIO_POW_MOD_SEL_REG;
1530 
1531 	pctl->irq_array = devm_kcalloc(&pdev->dev,
1532 				       IRQ_PER_BANK * pctl->desc->irq_banks,
1533 				       sizeof(*pctl->irq_array),
1534 				       GFP_KERNEL);
1535 	if (!pctl->irq_array)
1536 		return -ENOMEM;
1537 
1538 	ret = sunxi_pinctrl_build_state(pdev);
1539 	if (ret) {
1540 		dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
1541 		return ret;
1542 	}
1543 
1544 	pins = devm_kcalloc(&pdev->dev,
1545 			    pctl->desc->npins, sizeof(*pins),
1546 			    GFP_KERNEL);
1547 	if (!pins)
1548 		return -ENOMEM;
1549 
1550 	for (i = 0, pin_idx = 0; i < pctl->desc->npins; i++) {
1551 		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1552 
1553 		if (pin->variant && !(pctl->variant & pin->variant))
1554 			continue;
1555 
1556 		pins[pin_idx++] = pin->pin;
1557 	}
1558 
1559 	pctrl_desc = devm_kzalloc(&pdev->dev,
1560 				  sizeof(*pctrl_desc),
1561 				  GFP_KERNEL);
1562 	if (!pctrl_desc)
1563 		return -ENOMEM;
1564 
1565 	pctrl_desc->name = dev_name(&pdev->dev);
1566 	pctrl_desc->owner = THIS_MODULE;
1567 	pctrl_desc->pins = pins;
1568 	pctrl_desc->npins = pctl->ngroups;
1569 	pctrl_desc->confops = &sunxi_pconf_ops;
1570 	pctrl_desc->pctlops = &sunxi_pctrl_ops;
1571 
1572 	pmxops = devm_kmemdup(&pdev->dev, &sunxi_pmx_ops, sizeof(sunxi_pmx_ops),
1573 			      GFP_KERNEL);
1574 	if (!pmxops)
1575 		return -ENOMEM;
1576 
1577 	if (desc->disable_strict_mode)
1578 		pmxops->strict = false;
1579 
1580 	pctrl_desc->pmxops = pmxops;
1581 
1582 	pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
1583 	if (IS_ERR(pctl->pctl_dev)) {
1584 		dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
1585 		return PTR_ERR(pctl->pctl_dev);
1586 	}
1587 
1588 	pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
1589 	if (!pctl->chip)
1590 		return -ENOMEM;
1591 
1592 	last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
1593 	pctl->chip->owner = THIS_MODULE;
1594 	pctl->chip->request = gpiochip_generic_request;
1595 	pctl->chip->free = gpiochip_generic_free;
1596 	pctl->chip->set_config = gpiochip_generic_config;
1597 	pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input;
1598 	pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output;
1599 	pctl->chip->get = sunxi_pinctrl_gpio_get;
1600 	pctl->chip->set = sunxi_pinctrl_gpio_set;
1601 	pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate;
1602 	pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq;
1603 	pctl->chip->of_gpio_n_cells = 3;
1604 	pctl->chip->can_sleep = false;
1605 	pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
1606 			    pctl->desc->pin_base;
1607 	pctl->chip->label = dev_name(&pdev->dev);
1608 	pctl->chip->parent = &pdev->dev;
1609 	pctl->chip->base = pctl->desc->pin_base;
1610 
1611 	ret = gpiochip_add_data(pctl->chip, pctl);
1612 	if (ret)
1613 		return ret;
1614 
1615 	for (i = 0; i < pctl->desc->npins; i++) {
1616 		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1617 
1618 		ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
1619 					     pin->pin.number - pctl->desc->pin_base,
1620 					     pin->pin.number, 1);
1621 		if (ret)
1622 			goto gpiochip_error;
1623 	}
1624 
1625 	ret = of_clk_get_parent_count(node);
1626 	clk = devm_clk_get_enabled(&pdev->dev, ret == 1 ? NULL : "apb");
1627 	if (IS_ERR(clk)) {
1628 		ret = PTR_ERR(clk);
1629 		goto gpiochip_error;
1630 	}
1631 
1632 	pctl->irq = devm_kcalloc(&pdev->dev,
1633 				 pctl->desc->irq_banks,
1634 				 sizeof(*pctl->irq),
1635 				 GFP_KERNEL);
1636 	if (!pctl->irq) {
1637 		ret = -ENOMEM;
1638 		goto gpiochip_error;
1639 	}
1640 
1641 	for (i = 0; i < pctl->desc->irq_banks; i++) {
1642 		pctl->irq[i] = platform_get_irq(pdev, i);
1643 		if (pctl->irq[i] < 0) {
1644 			ret = pctl->irq[i];
1645 			goto gpiochip_error;
1646 		}
1647 	}
1648 
1649 	pctl->domain = irq_domain_create_linear(of_fwnode_handle(node),
1650 						pctl->desc->irq_banks * IRQ_PER_BANK,
1651 						&sunxi_pinctrl_irq_domain_ops, pctl);
1652 	if (!pctl->domain) {
1653 		dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
1654 		ret = -ENOMEM;
1655 		goto gpiochip_error;
1656 	}
1657 
1658 	for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
1659 		int irqno = irq_create_mapping(pctl->domain, i);
1660 
1661 		irq_set_lockdep_class(irqno, &sunxi_pinctrl_irq_lock_class,
1662 				      &sunxi_pinctrl_irq_request_class);
1663 		irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
1664 					 handle_edge_irq);
1665 		irq_set_chip_data(irqno, pctl);
1666 	}
1667 
1668 	for (i = 0; i < pctl->desc->irq_banks; i++) {
1669 		/* Mask and clear all IRQs before registering a handler */
1670 		writel(0, pctl->membase +
1671 			  sunxi_irq_ctrl_reg_from_bank(pctl->desc, i));
1672 		writel(0xffffffff,
1673 		       pctl->membase +
1674 		       sunxi_irq_status_reg_from_bank(pctl->desc, i));
1675 
1676 		irq_set_chained_handler_and_data(pctl->irq[i],
1677 						 sunxi_pinctrl_irq_handler,
1678 						 pctl);
1679 	}
1680 
1681 	sunxi_pinctrl_setup_debounce(pctl, node);
1682 
1683 	dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
1684 
1685 	return 0;
1686 
1687 gpiochip_error:
1688 	gpiochip_remove(pctl->chip);
1689 	return ret;
1690 }
1691