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