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