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