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