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