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