1 /* 2 * Copyright (C) 2016 IBM Corp. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 */ 9 10 #include <linux/mfd/syscon.h> 11 #include <linux/platform_device.h> 12 #include <linux/slab.h> 13 #include <linux/string.h> 14 #include "../core.h" 15 #include "pinctrl-aspeed.h" 16 17 static const char *const aspeed_pinmux_ips[] = { 18 [ASPEED_IP_SCU] = "SCU", 19 [ASPEED_IP_GFX] = "GFX", 20 [ASPEED_IP_LPC] = "LPC", 21 }; 22 23 int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev) 24 { 25 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 26 27 return pdata->ngroups; 28 } 29 30 const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev, 31 unsigned int group) 32 { 33 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 34 35 return pdata->groups[group].name; 36 } 37 38 int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, 39 unsigned int group, const unsigned int **pins, 40 unsigned int *npins) 41 { 42 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 43 44 *pins = &pdata->groups[group].pins[0]; 45 *npins = pdata->groups[group].npins; 46 47 return 0; 48 } 49 50 void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev, 51 struct seq_file *s, unsigned int offset) 52 { 53 seq_printf(s, " %s", dev_name(pctldev->dev)); 54 } 55 56 int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev) 57 { 58 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 59 60 return pdata->nfunctions; 61 } 62 63 const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev, 64 unsigned int function) 65 { 66 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 67 68 return pdata->functions[function].name; 69 } 70 71 int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev, 72 unsigned int function, 73 const char * const **groups, 74 unsigned int * const num_groups) 75 { 76 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 77 78 *groups = pdata->functions[function].groups; 79 *num_groups = pdata->functions[function].ngroups; 80 81 return 0; 82 } 83 84 static inline void aspeed_sig_desc_print_val( 85 const struct aspeed_sig_desc *desc, bool enable, u32 rv) 86 { 87 pr_debug("Want %s%X[0x%08X]=0x%X, got 0x%X from 0x%08X\n", 88 aspeed_pinmux_ips[desc->ip], desc->reg, 89 desc->mask, enable ? desc->enable : desc->disable, 90 (rv & desc->mask) >> __ffs(desc->mask), rv); 91 } 92 93 /** 94 * Query the enabled or disabled state of a signal descriptor 95 * 96 * @desc: The signal descriptor of interest 97 * @enabled: True to query the enabled state, false to query disabled state 98 * @regmap: The IP block's regmap instance 99 * 100 * Return: 1 if the descriptor's bitfield is configured to the state 101 * selected by @enabled, 0 if not, and less than zero if an unrecoverable 102 * failure occurred 103 * 104 * Evaluation of descriptor state is non-trivial in that it is not a binary 105 * outcome: The bitfields can be greater than one bit in size and thus can take 106 * a value that is neither the enabled nor disabled state recorded in the 107 * descriptor (typically this means a different function to the one of interest 108 * is enabled). Thus we must explicitly test for either condition as required. 109 */ 110 static int aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc, 111 bool enabled, struct regmap *map) 112 { 113 int ret; 114 unsigned int raw; 115 u32 want; 116 117 if (!map) 118 return -ENODEV; 119 120 ret = regmap_read(map, desc->reg, &raw); 121 if (ret) 122 return ret; 123 124 aspeed_sig_desc_print_val(desc, enabled, raw); 125 want = enabled ? desc->enable : desc->disable; 126 127 return ((raw & desc->mask) >> __ffs(desc->mask)) == want; 128 } 129 130 /** 131 * Query the enabled or disabled state for a mux function's signal on a pin 132 * 133 * @expr: An expression controlling the signal for a mux function on a pin 134 * @enabled: True to query the enabled state, false to query disabled state 135 * @maps: The list of regmap instances 136 * 137 * Return: 1 if the expression composed by @enabled evaluates true, 0 if not, 138 * and less than zero if an unrecoverable failure occurred. 139 * 140 * A mux function is enabled or disabled if the function's signal expression 141 * for each pin in the function's pin group evaluates true for the desired 142 * state. An signal expression evaluates true if all of its associated signal 143 * descriptors evaluate true for the desired state. 144 * 145 * If an expression's state is described by more than one bit, either through 146 * multi-bit bitfields in a single signal descriptor or through multiple signal 147 * descriptors of a single bit then it is possible for the expression to be in 148 * neither the enabled nor disabled state. Thus we must explicitly test for 149 * either condition as required. 150 */ 151 static int aspeed_sig_expr_eval(const struct aspeed_sig_expr *expr, 152 bool enabled, struct regmap * const *maps) 153 { 154 int i; 155 int ret; 156 157 for (i = 0; i < expr->ndescs; i++) { 158 const struct aspeed_sig_desc *desc = &expr->descs[i]; 159 160 ret = aspeed_sig_desc_eval(desc, enabled, maps[desc->ip]); 161 if (ret <= 0) 162 return ret; 163 } 164 165 return 1; 166 } 167 168 /** 169 * Configure a pin's signal by applying an expression's descriptor state for 170 * all descriptors in the expression. 171 * 172 * @expr: The expression associated with the function whose signal is to be 173 * configured 174 * @enable: true to enable an function's signal through a pin's signal 175 * expression, false to disable the function's signal 176 * @maps: The list of regmap instances for pinmux register access. 177 * 178 * Return: 0 if the expression is configured as requested and a negative error 179 * code otherwise 180 */ 181 static int aspeed_sig_expr_set(const struct aspeed_sig_expr *expr, 182 bool enable, struct regmap * const *maps) 183 { 184 int ret; 185 int i; 186 187 for (i = 0; i < expr->ndescs; i++) { 188 const struct aspeed_sig_desc *desc = &expr->descs[i]; 189 u32 pattern = enable ? desc->enable : desc->disable; 190 u32 val = (pattern << __ffs(desc->mask)); 191 192 if (!maps[desc->ip]) 193 return -ENODEV; 194 195 /* 196 * Strap registers are configured in hardware or by early-boot 197 * firmware. Treat them as read-only despite that we can write 198 * them. This may mean that certain functions cannot be 199 * deconfigured and is the reason we re-evaluate after writing 200 * all descriptor bits. 201 * 202 * Port D and port E GPIO loopback modes are the only exception 203 * as those are commonly used with front-panel buttons to allow 204 * normal operation of the host when the BMC is powered off or 205 * fails to boot. Once the BMC has booted, the loopback mode 206 * must be disabled for the BMC to control host power-on and 207 * reset. 208 */ 209 if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP1 && 210 !(desc->mask & (BIT(21) | BIT(22)))) 211 continue; 212 213 if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP2) 214 continue; 215 216 ret = regmap_update_bits(maps[desc->ip], desc->reg, 217 desc->mask, val); 218 219 if (ret) 220 return ret; 221 } 222 223 ret = aspeed_sig_expr_eval(expr, enable, maps); 224 if (ret < 0) 225 return ret; 226 227 if (!ret) 228 return -EPERM; 229 230 return 0; 231 } 232 233 static int aspeed_sig_expr_enable(const struct aspeed_sig_expr *expr, 234 struct regmap * const *maps) 235 { 236 int ret; 237 238 ret = aspeed_sig_expr_eval(expr, true, maps); 239 if (ret < 0) 240 return ret; 241 242 if (!ret) 243 return aspeed_sig_expr_set(expr, true, maps); 244 245 return 0; 246 } 247 248 static int aspeed_sig_expr_disable(const struct aspeed_sig_expr *expr, 249 struct regmap * const *maps) 250 { 251 int ret; 252 253 ret = aspeed_sig_expr_eval(expr, true, maps); 254 if (ret < 0) 255 return ret; 256 257 if (ret) 258 return aspeed_sig_expr_set(expr, false, maps); 259 260 return 0; 261 } 262 263 /** 264 * Disable a signal on a pin by disabling all provided signal expressions. 265 * 266 * @exprs: The list of signal expressions (from a priority level on a pin) 267 * @maps: The list of regmap instances for pinmux register access. 268 * 269 * Return: 0 if all expressions are disabled, otherwise a negative error code 270 */ 271 static int aspeed_disable_sig(const struct aspeed_sig_expr **exprs, 272 struct regmap * const *maps) 273 { 274 int ret = 0; 275 276 if (!exprs) 277 return true; 278 279 while (*exprs && !ret) { 280 ret = aspeed_sig_expr_disable(*exprs, maps); 281 exprs++; 282 } 283 284 return ret; 285 } 286 287 /** 288 * Search for the signal expression needed to enable the pin's signal for the 289 * requested function. 290 * 291 * @exprs: List of signal expressions (haystack) 292 * @name: The name of the requested function (needle) 293 * 294 * Return: A pointer to the signal expression whose function tag matches the 295 * provided name, otherwise NULL. 296 * 297 */ 298 static const struct aspeed_sig_expr *aspeed_find_expr_by_name( 299 const struct aspeed_sig_expr **exprs, const char *name) 300 { 301 while (*exprs) { 302 if (strcmp((*exprs)->function, name) == 0) 303 return *exprs; 304 exprs++; 305 } 306 307 return NULL; 308 } 309 310 static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc, 311 const char *(*get)( 312 const struct aspeed_sig_expr *)) 313 { 314 char *found = NULL; 315 size_t len = 0; 316 const struct aspeed_sig_expr ***prios, **funcs, *expr; 317 318 prios = pdesc->prios; 319 320 while ((funcs = *prios)) { 321 while ((expr = *funcs)) { 322 const char *str = get(expr); 323 size_t delta = strlen(str) + 2; 324 char *expanded; 325 326 expanded = krealloc(found, len + delta + 1, GFP_KERNEL); 327 if (!expanded) { 328 kfree(found); 329 return expanded; 330 } 331 332 found = expanded; 333 found[len] = '\0'; 334 len += delta; 335 336 strcat(found, str); 337 strcat(found, ", "); 338 339 funcs++; 340 } 341 prios++; 342 } 343 344 if (len < 2) { 345 kfree(found); 346 return NULL; 347 } 348 349 found[len - 2] = '\0'; 350 351 return found; 352 } 353 354 static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr) 355 { 356 return expr->function; 357 } 358 359 static char *get_defined_functions(const struct aspeed_pin_desc *pdesc) 360 { 361 return get_defined_attribute(pdesc, aspeed_sig_expr_function); 362 } 363 364 static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr) 365 { 366 return expr->signal; 367 } 368 369 static char *get_defined_signals(const struct aspeed_pin_desc *pdesc) 370 { 371 return get_defined_attribute(pdesc, aspeed_sig_expr_signal); 372 } 373 374 int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function, 375 unsigned int group) 376 { 377 int i; 378 int ret; 379 const struct aspeed_pinctrl_data *pdata = 380 pinctrl_dev_get_drvdata(pctldev); 381 const struct aspeed_pin_group *pgroup = &pdata->groups[group]; 382 const struct aspeed_pin_function *pfunc = 383 &pdata->functions[function]; 384 385 for (i = 0; i < pgroup->npins; i++) { 386 int pin = pgroup->pins[i]; 387 const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data; 388 const struct aspeed_sig_expr *expr = NULL; 389 const struct aspeed_sig_expr **funcs; 390 const struct aspeed_sig_expr ***prios; 391 392 pr_debug("Muxing pin %d for %s\n", pin, pfunc->name); 393 394 if (!pdesc) 395 return -EINVAL; 396 397 prios = pdesc->prios; 398 399 if (!prios) 400 continue; 401 402 /* Disable functions at a higher priority than that requested */ 403 while ((funcs = *prios)) { 404 expr = aspeed_find_expr_by_name(funcs, pfunc->name); 405 406 if (expr) 407 break; 408 409 ret = aspeed_disable_sig(funcs, pdata->maps); 410 if (ret) 411 return ret; 412 413 prios++; 414 } 415 416 if (!expr) { 417 char *functions = get_defined_functions(pdesc); 418 char *signals = get_defined_signals(pdesc); 419 420 pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n", 421 pfunc->name, pdesc->name, pin, signals, 422 functions); 423 kfree(signals); 424 kfree(functions); 425 426 return -ENXIO; 427 } 428 429 ret = aspeed_sig_expr_enable(expr, pdata->maps); 430 if (ret) 431 return ret; 432 } 433 434 return 0; 435 } 436 437 static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr) 438 { 439 /* 440 * The signal type is GPIO if the signal name has "GPIO" as a prefix. 441 * strncmp (rather than strcmp) is used to implement the prefix 442 * requirement. 443 * 444 * expr->signal might look like "GPIOT3" in the GPIO case. 445 */ 446 return strncmp(expr->signal, "GPIO", 4) == 0; 447 } 448 449 static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs) 450 { 451 if (!exprs) 452 return false; 453 454 while (*exprs) { 455 if (aspeed_expr_is_gpio(*exprs)) 456 return true; 457 exprs++; 458 } 459 460 return false; 461 } 462 463 int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev, 464 struct pinctrl_gpio_range *range, 465 unsigned int offset) 466 { 467 int ret; 468 const struct aspeed_pinctrl_data *pdata = 469 pinctrl_dev_get_drvdata(pctldev); 470 const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data; 471 const struct aspeed_sig_expr ***prios, **funcs, *expr; 472 473 if (!pdesc) 474 return -EINVAL; 475 476 prios = pdesc->prios; 477 478 if (!prios) 479 return -ENXIO; 480 481 /* Disable any functions of higher priority than GPIO */ 482 while ((funcs = *prios)) { 483 if (aspeed_gpio_in_exprs(funcs)) 484 break; 485 486 ret = aspeed_disable_sig(funcs, pdata->maps); 487 if (ret) 488 return ret; 489 490 prios++; 491 } 492 493 if (!funcs) { 494 char *signals = get_defined_signals(pdesc); 495 496 pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n", 497 pdesc->name, offset, signals); 498 kfree(signals); 499 500 return -ENXIO; 501 } 502 503 expr = *funcs; 504 505 /* 506 * Disabling all higher-priority expressions is enough to enable the 507 * lowest-priority signal type. As such it has no associated 508 * expression. 509 */ 510 if (!expr) 511 return 0; 512 513 /* 514 * If GPIO is not the lowest priority signal type, assume there is only 515 * one expression defined to enable the GPIO function 516 */ 517 return aspeed_sig_expr_enable(expr, pdata->maps); 518 } 519 520 int aspeed_pinctrl_probe(struct platform_device *pdev, 521 struct pinctrl_desc *pdesc, 522 struct aspeed_pinctrl_data *pdata) 523 { 524 struct device *parent; 525 struct pinctrl_dev *pctl; 526 527 parent = pdev->dev.parent; 528 if (!parent) { 529 dev_err(&pdev->dev, "No parent for syscon pincontroller\n"); 530 return -ENODEV; 531 } 532 533 pdata->maps[ASPEED_IP_SCU] = syscon_node_to_regmap(parent->of_node); 534 if (IS_ERR(pdata->maps[ASPEED_IP_SCU])) { 535 dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n"); 536 return PTR_ERR(pdata->maps[ASPEED_IP_SCU]); 537 } 538 539 pctl = pinctrl_register(pdesc, &pdev->dev, pdata); 540 541 if (IS_ERR(pctl)) { 542 dev_err(&pdev->dev, "Failed to register pinctrl\n"); 543 return PTR_ERR(pctl); 544 } 545 546 platform_set_drvdata(pdev, pdata); 547 548 return 0; 549 } 550 551 static inline bool pin_in_config_range(unsigned int offset, 552 const struct aspeed_pin_config *config) 553 { 554 return offset >= config->pins[0] && offset <= config->pins[1]; 555 } 556 557 static inline const struct aspeed_pin_config *find_pinconf_config( 558 const struct aspeed_pinctrl_data *pdata, 559 unsigned int offset, 560 enum pin_config_param param) 561 { 562 unsigned int i; 563 564 for (i = 0; i < pdata->nconfigs; i++) { 565 if (param == pdata->configs[i].param && 566 pin_in_config_range(offset, &pdata->configs[i])) 567 return &pdata->configs[i]; 568 } 569 570 return NULL; 571 } 572 573 /** 574 * @param: pinconf configuration parameter 575 * @arg: The supported argument for @param, or -1 if any value is supported 576 * @value: The register value to write to configure @arg for @param 577 * 578 * The map is to be used in conjunction with the configuration array supplied 579 * by the driver implementation. 580 */ 581 struct aspeed_pin_config_map { 582 enum pin_config_param param; 583 s32 arg; 584 u32 val; 585 }; 586 587 enum aspeed_pin_config_map_type { MAP_TYPE_ARG, MAP_TYPE_VAL }; 588 589 /* Aspeed consistently both: 590 * 591 * 1. Defines "disable bits" for internal pull-downs 592 * 2. Uses 8mA or 16mA drive strengths 593 */ 594 static const struct aspeed_pin_config_map pin_config_map[] = { 595 { PIN_CONFIG_BIAS_PULL_DOWN, 0, 1 }, 596 { PIN_CONFIG_BIAS_PULL_DOWN, -1, 0 }, 597 { PIN_CONFIG_BIAS_DISABLE, -1, 1 }, 598 { PIN_CONFIG_DRIVE_STRENGTH, 8, 0 }, 599 { PIN_CONFIG_DRIVE_STRENGTH, 16, 1 }, 600 }; 601 602 static const struct aspeed_pin_config_map *find_pinconf_map( 603 enum pin_config_param param, 604 enum aspeed_pin_config_map_type type, 605 s64 value) 606 { 607 int i; 608 609 for (i = 0; i < ARRAY_SIZE(pin_config_map); i++) { 610 const struct aspeed_pin_config_map *elem; 611 bool match; 612 613 elem = &pin_config_map[i]; 614 615 switch (type) { 616 case MAP_TYPE_ARG: 617 match = (elem->arg == -1 || elem->arg == value); 618 break; 619 case MAP_TYPE_VAL: 620 match = (elem->val == value); 621 break; 622 } 623 624 if (param == elem->param && match) 625 return elem; 626 } 627 628 return NULL; 629 } 630 631 int aspeed_pin_config_get(struct pinctrl_dev *pctldev, unsigned int offset, 632 unsigned long *config) 633 { 634 const enum pin_config_param param = pinconf_to_config_param(*config); 635 const struct aspeed_pin_config_map *pmap; 636 const struct aspeed_pinctrl_data *pdata; 637 const struct aspeed_pin_config *pconf; 638 unsigned int val; 639 int rc = 0; 640 u32 arg; 641 642 pdata = pinctrl_dev_get_drvdata(pctldev); 643 pconf = find_pinconf_config(pdata, offset, param); 644 if (!pconf) 645 return -ENOTSUPP; 646 647 rc = regmap_read(pdata->maps[ASPEED_IP_SCU], pconf->reg, &val); 648 if (rc < 0) 649 return rc; 650 651 pmap = find_pinconf_map(param, MAP_TYPE_VAL, 652 (val & BIT(pconf->bit)) >> pconf->bit); 653 654 if (!pmap) 655 return -EINVAL; 656 657 if (param == PIN_CONFIG_DRIVE_STRENGTH) 658 arg = (u32) pmap->arg; 659 else if (param == PIN_CONFIG_BIAS_PULL_DOWN) 660 arg = !!pmap->arg; 661 else 662 arg = 1; 663 664 if (!arg) 665 return -EINVAL; 666 667 *config = pinconf_to_config_packed(param, arg); 668 669 return 0; 670 } 671 672 int aspeed_pin_config_set(struct pinctrl_dev *pctldev, unsigned int offset, 673 unsigned long *configs, unsigned int num_configs) 674 { 675 const struct aspeed_pinctrl_data *pdata; 676 unsigned int i; 677 int rc = 0; 678 679 pdata = pinctrl_dev_get_drvdata(pctldev); 680 681 for (i = 0; i < num_configs; i++) { 682 const struct aspeed_pin_config_map *pmap; 683 const struct aspeed_pin_config *pconf; 684 enum pin_config_param param; 685 unsigned int val; 686 u32 arg; 687 688 param = pinconf_to_config_param(configs[i]); 689 arg = pinconf_to_config_argument(configs[i]); 690 691 pconf = find_pinconf_config(pdata, offset, param); 692 if (!pconf) 693 return -ENOTSUPP; 694 695 pmap = find_pinconf_map(param, MAP_TYPE_ARG, arg); 696 697 if (unlikely(WARN_ON(!pmap))) 698 return -EINVAL; 699 700 val = pmap->val << pconf->bit; 701 702 rc = regmap_update_bits(pdata->maps[ASPEED_IP_SCU], pconf->reg, 703 BIT(pconf->bit), val); 704 705 if (rc < 0) 706 return rc; 707 708 pr_debug("%s: Set SCU%02X[%d]=%d for param %d(=%d) on pin %d\n", 709 __func__, pconf->reg, pconf->bit, pmap->val, 710 param, arg, offset); 711 } 712 713 return 0; 714 } 715 716 int aspeed_pin_config_group_get(struct pinctrl_dev *pctldev, 717 unsigned int selector, 718 unsigned long *config) 719 { 720 const unsigned int *pins; 721 unsigned int npins; 722 int rc; 723 724 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins); 725 if (rc < 0) 726 return rc; 727 728 if (!npins) 729 return -ENODEV; 730 731 rc = aspeed_pin_config_get(pctldev, pins[0], config); 732 733 return rc; 734 } 735 736 int aspeed_pin_config_group_set(struct pinctrl_dev *pctldev, 737 unsigned int selector, 738 unsigned long *configs, 739 unsigned int num_configs) 740 { 741 const unsigned int *pins; 742 unsigned int npins; 743 int rc; 744 int i; 745 746 pr_debug("%s: Fetching pins for group selector %d\n", 747 __func__, selector); 748 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins); 749 if (rc < 0) 750 return rc; 751 752 for (i = 0; i < npins; i++) { 753 rc = aspeed_pin_config_set(pctldev, pins[i], configs, 754 num_configs); 755 if (rc < 0) 756 return rc; 757 } 758 759 return 0; 760 } 761