1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 IBM Corp. 4 */ 5 6 #include <linux/mfd/syscon.h> 7 #include <linux/platform_device.h> 8 #include <linux/slab.h> 9 #include <linux/string.h> 10 #include "../core.h" 11 #include "pinctrl-aspeed.h" 12 13 int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev) 14 { 15 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 16 17 return pdata->pinmux.ngroups; 18 } 19 20 const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev, 21 unsigned int group) 22 { 23 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 24 25 return pdata->pinmux.groups[group].name; 26 } 27 28 int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, 29 unsigned int group, const unsigned int **pins, 30 unsigned int *npins) 31 { 32 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 33 34 *pins = &pdata->pinmux.groups[group].pins[0]; 35 *npins = pdata->pinmux.groups[group].npins; 36 37 return 0; 38 } 39 40 void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev, 41 struct seq_file *s, unsigned int offset) 42 { 43 seq_printf(s, " %s", dev_name(pctldev->dev)); 44 } 45 46 int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev) 47 { 48 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 49 50 return pdata->pinmux.nfunctions; 51 } 52 53 const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev, 54 unsigned int function) 55 { 56 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 57 58 return pdata->pinmux.functions[function].name; 59 } 60 61 int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev, 62 unsigned int function, 63 const char * const **groups, 64 unsigned int * const num_groups) 65 { 66 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 67 68 *groups = pdata->pinmux.functions[function].groups; 69 *num_groups = pdata->pinmux.functions[function].ngroups; 70 71 return 0; 72 } 73 74 static int aspeed_sig_expr_enable(const struct aspeed_pinmux_data *ctx, 75 const struct aspeed_sig_expr *expr) 76 { 77 int ret; 78 79 ret = aspeed_sig_expr_eval(ctx, expr, true); 80 if (ret < 0) 81 return ret; 82 83 if (!ret) 84 return aspeed_sig_expr_set(ctx, expr, true); 85 86 return 0; 87 } 88 89 static int aspeed_sig_expr_disable(const struct aspeed_pinmux_data *ctx, 90 const struct aspeed_sig_expr *expr) 91 { 92 int ret; 93 94 ret = aspeed_sig_expr_eval(ctx, expr, true); 95 if (ret < 0) 96 return ret; 97 98 if (ret) 99 return aspeed_sig_expr_set(ctx, expr, false); 100 101 return 0; 102 } 103 104 /** 105 * Disable a signal on a pin by disabling all provided signal expressions. 106 * 107 * @ctx: The pinmux context 108 * @exprs: The list of signal expressions (from a priority level on a pin) 109 * 110 * Return: 0 if all expressions are disabled, otherwise a negative error code 111 */ 112 static int aspeed_disable_sig(const struct aspeed_pinmux_data *ctx, 113 const struct aspeed_sig_expr **exprs) 114 { 115 int ret = 0; 116 117 if (!exprs) 118 return true; 119 120 while (*exprs && !ret) { 121 ret = aspeed_sig_expr_disable(ctx, *exprs); 122 exprs++; 123 } 124 125 return ret; 126 } 127 128 /** 129 * Search for the signal expression needed to enable the pin's signal for the 130 * requested function. 131 * 132 * @exprs: List of signal expressions (haystack) 133 * @name: The name of the requested function (needle) 134 * 135 * Return: A pointer to the signal expression whose function tag matches the 136 * provided name, otherwise NULL. 137 * 138 */ 139 static const struct aspeed_sig_expr *aspeed_find_expr_by_name( 140 const struct aspeed_sig_expr **exprs, const char *name) 141 { 142 while (*exprs) { 143 if (strcmp((*exprs)->function, name) == 0) 144 return *exprs; 145 exprs++; 146 } 147 148 return NULL; 149 } 150 151 static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc, 152 const char *(*get)( 153 const struct aspeed_sig_expr *)) 154 { 155 char *found = NULL; 156 size_t len = 0; 157 const struct aspeed_sig_expr ***prios, **funcs, *expr; 158 159 prios = pdesc->prios; 160 161 while ((funcs = *prios)) { 162 while ((expr = *funcs)) { 163 const char *str = get(expr); 164 size_t delta = strlen(str) + 2; 165 char *expanded; 166 167 expanded = krealloc(found, len + delta + 1, GFP_KERNEL); 168 if (!expanded) { 169 kfree(found); 170 return expanded; 171 } 172 173 found = expanded; 174 found[len] = '\0'; 175 len += delta; 176 177 strcat(found, str); 178 strcat(found, ", "); 179 180 funcs++; 181 } 182 prios++; 183 } 184 185 if (len < 2) { 186 kfree(found); 187 return NULL; 188 } 189 190 found[len - 2] = '\0'; 191 192 return found; 193 } 194 195 static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr) 196 { 197 return expr->function; 198 } 199 200 static char *get_defined_functions(const struct aspeed_pin_desc *pdesc) 201 { 202 return get_defined_attribute(pdesc, aspeed_sig_expr_function); 203 } 204 205 static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr) 206 { 207 return expr->signal; 208 } 209 210 static char *get_defined_signals(const struct aspeed_pin_desc *pdesc) 211 { 212 return get_defined_attribute(pdesc, aspeed_sig_expr_signal); 213 } 214 215 int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function, 216 unsigned int group) 217 { 218 int i; 219 int ret; 220 const struct aspeed_pinctrl_data *pdata = 221 pinctrl_dev_get_drvdata(pctldev); 222 const struct aspeed_pin_group *pgroup = &pdata->pinmux.groups[group]; 223 const struct aspeed_pin_function *pfunc = 224 &pdata->pinmux.functions[function]; 225 226 for (i = 0; i < pgroup->npins; i++) { 227 int pin = pgroup->pins[i]; 228 const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data; 229 const struct aspeed_sig_expr *expr = NULL; 230 const struct aspeed_sig_expr **funcs; 231 const struct aspeed_sig_expr ***prios; 232 233 pr_debug("Muxing pin %d for %s\n", pin, pfunc->name); 234 235 if (!pdesc) 236 return -EINVAL; 237 238 prios = pdesc->prios; 239 240 if (!prios) 241 continue; 242 243 /* Disable functions at a higher priority than that requested */ 244 while ((funcs = *prios)) { 245 expr = aspeed_find_expr_by_name(funcs, pfunc->name); 246 247 if (expr) 248 break; 249 250 ret = aspeed_disable_sig(&pdata->pinmux, funcs); 251 if (ret) 252 return ret; 253 254 prios++; 255 } 256 257 if (!expr) { 258 char *functions = get_defined_functions(pdesc); 259 char *signals = get_defined_signals(pdesc); 260 261 pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n", 262 pfunc->name, pdesc->name, pin, signals, 263 functions); 264 kfree(signals); 265 kfree(functions); 266 267 return -ENXIO; 268 } 269 270 ret = aspeed_sig_expr_enable(&pdata->pinmux, expr); 271 if (ret) 272 return ret; 273 } 274 275 return 0; 276 } 277 278 static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr) 279 { 280 /* 281 * The signal type is GPIO if the signal name has "GPIO" as a prefix. 282 * strncmp (rather than strcmp) is used to implement the prefix 283 * requirement. 284 * 285 * expr->signal might look like "GPIOT3" in the GPIO case. 286 */ 287 return strncmp(expr->signal, "GPIO", 4) == 0; 288 } 289 290 static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs) 291 { 292 if (!exprs) 293 return false; 294 295 while (*exprs) { 296 if (aspeed_expr_is_gpio(*exprs)) 297 return true; 298 exprs++; 299 } 300 301 return false; 302 } 303 304 int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev, 305 struct pinctrl_gpio_range *range, 306 unsigned int offset) 307 { 308 int ret; 309 const struct aspeed_pinctrl_data *pdata = 310 pinctrl_dev_get_drvdata(pctldev); 311 const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data; 312 const struct aspeed_sig_expr ***prios, **funcs, *expr; 313 314 if (!pdesc) 315 return -EINVAL; 316 317 prios = pdesc->prios; 318 319 if (!prios) 320 return -ENXIO; 321 322 /* Disable any functions of higher priority than GPIO */ 323 while ((funcs = *prios)) { 324 if (aspeed_gpio_in_exprs(funcs)) 325 break; 326 327 ret = aspeed_disable_sig(&pdata->pinmux, funcs); 328 if (ret) 329 return ret; 330 331 prios++; 332 } 333 334 if (!funcs) { 335 char *signals = get_defined_signals(pdesc); 336 337 pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n", 338 pdesc->name, offset, signals); 339 kfree(signals); 340 341 return -ENXIO; 342 } 343 344 expr = *funcs; 345 346 /* 347 * Disabling all higher-priority expressions is enough to enable the 348 * lowest-priority signal type. As such it has no associated 349 * expression. 350 */ 351 if (!expr) 352 return 0; 353 354 /* 355 * If GPIO is not the lowest priority signal type, assume there is only 356 * one expression defined to enable the GPIO function 357 */ 358 return aspeed_sig_expr_enable(&pdata->pinmux, expr); 359 } 360 361 int aspeed_pinctrl_probe(struct platform_device *pdev, 362 struct pinctrl_desc *pdesc, 363 struct aspeed_pinctrl_data *pdata) 364 { 365 struct device *parent; 366 struct pinctrl_dev *pctl; 367 368 parent = pdev->dev.parent; 369 if (!parent) { 370 dev_err(&pdev->dev, "No parent for syscon pincontroller\n"); 371 return -ENODEV; 372 } 373 374 pdata->scu = syscon_node_to_regmap(parent->of_node); 375 if (IS_ERR(pdata->scu)) { 376 dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n"); 377 return PTR_ERR(pdata->scu); 378 } 379 380 pdata->pinmux.maps[ASPEED_IP_SCU] = pdata->scu; 381 382 pctl = pinctrl_register(pdesc, &pdev->dev, pdata); 383 384 if (IS_ERR(pctl)) { 385 dev_err(&pdev->dev, "Failed to register pinctrl\n"); 386 return PTR_ERR(pctl); 387 } 388 389 platform_set_drvdata(pdev, pdata); 390 391 return 0; 392 } 393 394 static inline bool pin_in_config_range(unsigned int offset, 395 const struct aspeed_pin_config *config) 396 { 397 return offset >= config->pins[0] && offset <= config->pins[1]; 398 } 399 400 static inline const struct aspeed_pin_config *find_pinconf_config( 401 const struct aspeed_pinctrl_data *pdata, 402 unsigned int offset, 403 enum pin_config_param param) 404 { 405 unsigned int i; 406 407 for (i = 0; i < pdata->nconfigs; i++) { 408 if (param == pdata->configs[i].param && 409 pin_in_config_range(offset, &pdata->configs[i])) 410 return &pdata->configs[i]; 411 } 412 413 return NULL; 414 } 415 416 /* 417 * Aspeed pin configuration description. 418 * 419 * @param: pinconf configuration parameter 420 * @arg: The supported argument for @param, or -1 if any value is supported 421 * @val: The register value to write to configure @arg for @param 422 * 423 * The map is to be used in conjunction with the configuration array supplied 424 * by the driver implementation. 425 */ 426 struct aspeed_pin_config_map { 427 enum pin_config_param param; 428 s32 arg; 429 u32 val; 430 }; 431 432 enum aspeed_pin_config_map_type { MAP_TYPE_ARG, MAP_TYPE_VAL }; 433 434 /* Aspeed consistently both: 435 * 436 * 1. Defines "disable bits" for internal pull-downs 437 * 2. Uses 8mA or 16mA drive strengths 438 */ 439 static const struct aspeed_pin_config_map pin_config_map[] = { 440 { PIN_CONFIG_BIAS_PULL_DOWN, 0, 1 }, 441 { PIN_CONFIG_BIAS_PULL_DOWN, -1, 0 }, 442 { PIN_CONFIG_BIAS_DISABLE, -1, 1 }, 443 { PIN_CONFIG_DRIVE_STRENGTH, 8, 0 }, 444 { PIN_CONFIG_DRIVE_STRENGTH, 16, 1 }, 445 }; 446 447 static const struct aspeed_pin_config_map *find_pinconf_map( 448 enum pin_config_param param, 449 enum aspeed_pin_config_map_type type, 450 s64 value) 451 { 452 int i; 453 454 for (i = 0; i < ARRAY_SIZE(pin_config_map); i++) { 455 const struct aspeed_pin_config_map *elem; 456 bool match; 457 458 elem = &pin_config_map[i]; 459 460 switch (type) { 461 case MAP_TYPE_ARG: 462 match = (elem->arg == -1 || elem->arg == value); 463 break; 464 case MAP_TYPE_VAL: 465 match = (elem->val == value); 466 break; 467 } 468 469 if (param == elem->param && match) 470 return elem; 471 } 472 473 return NULL; 474 } 475 476 int aspeed_pin_config_get(struct pinctrl_dev *pctldev, unsigned int offset, 477 unsigned long *config) 478 { 479 const enum pin_config_param param = pinconf_to_config_param(*config); 480 const struct aspeed_pin_config_map *pmap; 481 const struct aspeed_pinctrl_data *pdata; 482 const struct aspeed_pin_config *pconf; 483 unsigned int val; 484 int rc = 0; 485 u32 arg; 486 487 pdata = pinctrl_dev_get_drvdata(pctldev); 488 pconf = find_pinconf_config(pdata, offset, param); 489 if (!pconf) 490 return -ENOTSUPP; 491 492 rc = regmap_read(pdata->scu, pconf->reg, &val); 493 if (rc < 0) 494 return rc; 495 496 pmap = find_pinconf_map(param, MAP_TYPE_VAL, 497 (val & BIT(pconf->bit)) >> pconf->bit); 498 499 if (!pmap) 500 return -EINVAL; 501 502 if (param == PIN_CONFIG_DRIVE_STRENGTH) 503 arg = (u32) pmap->arg; 504 else if (param == PIN_CONFIG_BIAS_PULL_DOWN) 505 arg = !!pmap->arg; 506 else 507 arg = 1; 508 509 if (!arg) 510 return -EINVAL; 511 512 *config = pinconf_to_config_packed(param, arg); 513 514 return 0; 515 } 516 517 int aspeed_pin_config_set(struct pinctrl_dev *pctldev, unsigned int offset, 518 unsigned long *configs, unsigned int num_configs) 519 { 520 const struct aspeed_pinctrl_data *pdata; 521 unsigned int i; 522 int rc = 0; 523 524 pdata = pinctrl_dev_get_drvdata(pctldev); 525 526 for (i = 0; i < num_configs; i++) { 527 const struct aspeed_pin_config_map *pmap; 528 const struct aspeed_pin_config *pconf; 529 enum pin_config_param param; 530 unsigned int val; 531 u32 arg; 532 533 param = pinconf_to_config_param(configs[i]); 534 arg = pinconf_to_config_argument(configs[i]); 535 536 pconf = find_pinconf_config(pdata, offset, param); 537 if (!pconf) 538 return -ENOTSUPP; 539 540 pmap = find_pinconf_map(param, MAP_TYPE_ARG, arg); 541 542 if (WARN_ON(!pmap)) 543 return -EINVAL; 544 545 val = pmap->val << pconf->bit; 546 547 rc = regmap_update_bits(pdata->scu, pconf->reg, 548 BIT(pconf->bit), val); 549 550 if (rc < 0) 551 return rc; 552 553 pr_debug("%s: Set SCU%02X[%d]=%d for param %d(=%d) on pin %d\n", 554 __func__, pconf->reg, pconf->bit, pmap->val, 555 param, arg, offset); 556 } 557 558 return 0; 559 } 560 561 int aspeed_pin_config_group_get(struct pinctrl_dev *pctldev, 562 unsigned int selector, 563 unsigned long *config) 564 { 565 const unsigned int *pins; 566 unsigned int npins; 567 int rc; 568 569 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins); 570 if (rc < 0) 571 return rc; 572 573 if (!npins) 574 return -ENODEV; 575 576 rc = aspeed_pin_config_get(pctldev, pins[0], config); 577 578 return rc; 579 } 580 581 int aspeed_pin_config_group_set(struct pinctrl_dev *pctldev, 582 unsigned int selector, 583 unsigned long *configs, 584 unsigned int num_configs) 585 { 586 const unsigned int *pins; 587 unsigned int npins; 588 int rc; 589 int i; 590 591 pr_debug("%s: Fetching pins for group selector %d\n", 592 __func__, selector); 593 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins); 594 if (rc < 0) 595 return rc; 596 597 for (i = 0; i < npins; i++) { 598 rc = aspeed_pin_config_set(pctldev, pins[i], configs, 599 num_configs); 600 if (rc < 0) 601 return rc; 602 } 603 604 return 0; 605 } 606