1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ZynqMP pin controller 4 * 5 * Copyright (C) 2020, 2021 Xilinx, Inc. 6 * 7 * Sai Krishna Potthuri <lakshmi.sai.krishna.potthuri@xilinx.com> 8 * Rajan Vaja <rajan.vaja@xilinx.com> 9 */ 10 11 #include <dt-bindings/pinctrl/pinctrl-zynqmp.h> 12 13 #include <linux/init.h> 14 #include <linux/module.h> 15 #include <linux/of_address.h> 16 #include <linux/platform_device.h> 17 #include <linux/firmware/xlnx-zynqmp.h> 18 19 #include <linux/pinctrl/pinmux.h> 20 #include <linux/pinctrl/pinconf-generic.h> 21 22 #include "core.h" 23 #include "pinctrl-utils.h" 24 25 #define ZYNQMP_PIN_PREFIX "MIO" 26 #define PINCTRL_GET_FUNC_NAME_RESP_LEN 16 27 #define MAX_FUNC_NAME_LEN 16 28 #define MAX_GROUP_PIN 50 29 #define MAX_PIN_GROUPS 50 30 #define END_OF_FUNCTIONS "END_OF_FUNCTIONS" 31 #define NUM_GROUPS_PER_RESP 6 32 33 #define PINCTRL_GET_FUNC_GROUPS_RESP_LEN 12 34 #define PINCTRL_GET_PIN_GROUPS_RESP_LEN 12 35 #define NA_GROUP 0xFFFF 36 #define RESERVED_GROUP 0xFFFE 37 38 #define DRIVE_STRENGTH_2MA 2 39 #define DRIVE_STRENGTH_4MA 4 40 #define DRIVE_STRENGTH_8MA 8 41 #define DRIVE_STRENGTH_12MA 12 42 43 /** 44 * struct zynqmp_pmux_function - a pinmux function 45 * @name: Name of the pin mux function 46 * @groups: List of pin groups for this function 47 * @ngroups: Number of entries in @groups 48 * @node: Firmware node matching with the function 49 * 50 * This structure holds information about pin control function 51 * and function group names supporting that function. 52 */ 53 struct zynqmp_pmux_function { 54 char name[MAX_FUNC_NAME_LEN]; 55 const char * const *groups; 56 unsigned int ngroups; 57 }; 58 59 /** 60 * struct zynqmp_pinctrl - driver data 61 * @pctrl: Pin control device 62 * @groups: Pin groups 63 * @ngroups: Number of @groups 64 * @funcs: Pin mux functions 65 * @nfuncs: Number of @funcs 66 * 67 * This struct is stored as driver data and used to retrieve 68 * information regarding pin control functions, groups and 69 * group pins. 70 */ 71 struct zynqmp_pinctrl { 72 struct pinctrl_dev *pctrl; 73 const struct zynqmp_pctrl_group *groups; 74 unsigned int ngroups; 75 const struct zynqmp_pmux_function *funcs; 76 unsigned int nfuncs; 77 }; 78 79 /** 80 * struct zynqmp_pctrl_group - Pin control group info 81 * @name: Group name 82 * @pins: Group pin numbers 83 * @npins: Number of pins in the group 84 */ 85 struct zynqmp_pctrl_group { 86 const char *name; 87 unsigned int pins[MAX_GROUP_PIN]; 88 unsigned int npins; 89 }; 90 91 static struct pinctrl_desc zynqmp_desc; 92 93 static int zynqmp_pctrl_get_groups_count(struct pinctrl_dev *pctldev) 94 { 95 struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 96 97 return pctrl->ngroups; 98 } 99 100 static const char *zynqmp_pctrl_get_group_name(struct pinctrl_dev *pctldev, 101 unsigned int selector) 102 { 103 struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 104 105 return pctrl->groups[selector].name; 106 } 107 108 static int zynqmp_pctrl_get_group_pins(struct pinctrl_dev *pctldev, 109 unsigned int selector, 110 const unsigned int **pins, 111 unsigned int *npins) 112 { 113 struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 114 115 *pins = pctrl->groups[selector].pins; 116 *npins = pctrl->groups[selector].npins; 117 118 return 0; 119 } 120 121 static const struct pinctrl_ops zynqmp_pctrl_ops = { 122 .get_groups_count = zynqmp_pctrl_get_groups_count, 123 .get_group_name = zynqmp_pctrl_get_group_name, 124 .get_group_pins = zynqmp_pctrl_get_group_pins, 125 .dt_node_to_map = pinconf_generic_dt_node_to_map_all, 126 .dt_free_map = pinctrl_utils_free_map, 127 }; 128 129 static int zynqmp_pinmux_request_pin(struct pinctrl_dev *pctldev, 130 unsigned int pin) 131 { 132 int ret; 133 134 ret = zynqmp_pm_pinctrl_request(pin); 135 if (ret) { 136 dev_err(pctldev->dev, "request failed for pin %u\n", pin); 137 return ret; 138 } 139 140 return 0; 141 } 142 143 static int zynqmp_pmux_get_functions_count(struct pinctrl_dev *pctldev) 144 { 145 struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 146 147 return pctrl->nfuncs; 148 } 149 150 static const char *zynqmp_pmux_get_function_name(struct pinctrl_dev *pctldev, 151 unsigned int selector) 152 { 153 struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 154 155 return pctrl->funcs[selector].name; 156 } 157 158 /** 159 * zynqmp_pmux_get_function_groups() - Get groups for the function 160 * @pctldev: Pincontrol device pointer. 161 * @selector: Function ID 162 * @groups: Group names. 163 * @num_groups: Number of function groups. 164 * 165 * Get function's group count and group names. 166 * 167 * Return: 0 168 */ 169 static int zynqmp_pmux_get_function_groups(struct pinctrl_dev *pctldev, 170 unsigned int selector, 171 const char * const **groups, 172 unsigned * const num_groups) 173 { 174 struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 175 176 *groups = pctrl->funcs[selector].groups; 177 *num_groups = pctrl->funcs[selector].ngroups; 178 179 return 0; 180 } 181 182 /** 183 * zynqmp_pinmux_set_mux() - Set requested function for the group 184 * @pctldev: Pincontrol device pointer. 185 * @function: Function ID. 186 * @group: Group ID. 187 * 188 * Loop through all pins of the group and call firmware API 189 * to set requested function for all pins in the group. 190 * 191 * Return: 0 on success else error code. 192 */ 193 static int zynqmp_pinmux_set_mux(struct pinctrl_dev *pctldev, 194 unsigned int function, 195 unsigned int group) 196 { 197 struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 198 const struct zynqmp_pctrl_group *pgrp = &pctrl->groups[group]; 199 int ret, i; 200 201 for (i = 0; i < pgrp->npins; i++) { 202 unsigned int pin = pgrp->pins[i]; 203 204 ret = zynqmp_pm_pinctrl_set_function(pin, function); 205 if (ret) { 206 dev_err(pctldev->dev, "set mux failed for pin %u\n", 207 pin); 208 return ret; 209 } 210 } 211 212 return 0; 213 } 214 215 static int zynqmp_pinmux_release_pin(struct pinctrl_dev *pctldev, 216 unsigned int pin) 217 { 218 int ret; 219 220 ret = zynqmp_pm_pinctrl_release(pin); 221 if (ret) { 222 dev_err(pctldev->dev, "free pin failed for pin %u\n", 223 pin); 224 return ret; 225 } 226 227 return 0; 228 } 229 230 static const struct pinmux_ops zynqmp_pinmux_ops = { 231 .request = zynqmp_pinmux_request_pin, 232 .get_functions_count = zynqmp_pmux_get_functions_count, 233 .get_function_name = zynqmp_pmux_get_function_name, 234 .get_function_groups = zynqmp_pmux_get_function_groups, 235 .set_mux = zynqmp_pinmux_set_mux, 236 .free = zynqmp_pinmux_release_pin, 237 }; 238 239 /** 240 * zynqmp_pinconf_cfg_get() - get config value for the pin 241 * @pctldev: Pin control device pointer. 242 * @pin: Pin number. 243 * @config: Value of config param. 244 * 245 * Get value of the requested configuration parameter for the 246 * given pin. 247 * 248 * Return: 0 on success else error code. 249 */ 250 static int zynqmp_pinconf_cfg_get(struct pinctrl_dev *pctldev, 251 unsigned int pin, 252 unsigned long *config) 253 { 254 unsigned int arg, param = pinconf_to_config_param(*config); 255 int ret; 256 257 switch (param) { 258 case PIN_CONFIG_SLEW_RATE: 259 param = PM_PINCTRL_CONFIG_SLEW_RATE; 260 ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg); 261 break; 262 case PIN_CONFIG_BIAS_PULL_UP: 263 param = PM_PINCTRL_CONFIG_PULL_CTRL; 264 ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg); 265 if (arg != PM_PINCTRL_BIAS_PULL_UP) 266 return -EINVAL; 267 268 arg = 1; 269 break; 270 case PIN_CONFIG_BIAS_PULL_DOWN: 271 param = PM_PINCTRL_CONFIG_PULL_CTRL; 272 ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg); 273 if (arg != PM_PINCTRL_BIAS_PULL_DOWN) 274 return -EINVAL; 275 276 arg = 1; 277 break; 278 case PIN_CONFIG_BIAS_DISABLE: 279 param = PM_PINCTRL_CONFIG_BIAS_STATUS; 280 ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg); 281 if (arg != PM_PINCTRL_BIAS_DISABLE) 282 return -EINVAL; 283 284 arg = 1; 285 break; 286 case PIN_CONFIG_POWER_SOURCE: 287 param = PM_PINCTRL_CONFIG_VOLTAGE_STATUS; 288 ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg); 289 break; 290 case PIN_CONFIG_INPUT_SCHMITT_ENABLE: 291 param = PM_PINCTRL_CONFIG_SCHMITT_CMOS; 292 ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg); 293 break; 294 case PIN_CONFIG_DRIVE_STRENGTH: 295 param = PM_PINCTRL_CONFIG_DRIVE_STRENGTH; 296 ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg); 297 switch (arg) { 298 case PM_PINCTRL_DRIVE_STRENGTH_2MA: 299 arg = DRIVE_STRENGTH_2MA; 300 break; 301 case PM_PINCTRL_DRIVE_STRENGTH_4MA: 302 arg = DRIVE_STRENGTH_4MA; 303 break; 304 case PM_PINCTRL_DRIVE_STRENGTH_8MA: 305 arg = DRIVE_STRENGTH_8MA; 306 break; 307 case PM_PINCTRL_DRIVE_STRENGTH_12MA: 308 arg = DRIVE_STRENGTH_12MA; 309 break; 310 default: 311 /* Invalid drive strength */ 312 dev_warn(pctldev->dev, 313 "Invalid drive strength for pin %d\n", 314 pin); 315 return -EINVAL; 316 } 317 break; 318 default: 319 ret = -ENOTSUPP; 320 break; 321 } 322 323 if (ret) 324 return ret; 325 326 param = pinconf_to_config_param(*config); 327 *config = pinconf_to_config_packed(param, arg); 328 329 return 0; 330 } 331 332 /** 333 * zynqmp_pinconf_cfg_set() - Set requested config for the pin 334 * @pctldev: Pincontrol device pointer. 335 * @pin: Pin number. 336 * @configs: Configuration to set. 337 * @num_configs: Number of configurations. 338 * 339 * Loop through all configurations and call firmware API 340 * to set requested configurations for the pin. 341 * 342 * Return: 0 on success else error code. 343 */ 344 static int zynqmp_pinconf_cfg_set(struct pinctrl_dev *pctldev, 345 unsigned int pin, unsigned long *configs, 346 unsigned int num_configs) 347 { 348 int i, ret; 349 350 for (i = 0; i < num_configs; i++) { 351 unsigned int param = pinconf_to_config_param(configs[i]); 352 unsigned int arg = pinconf_to_config_argument(configs[i]); 353 unsigned int value; 354 355 switch (param) { 356 case PIN_CONFIG_SLEW_RATE: 357 param = PM_PINCTRL_CONFIG_SLEW_RATE; 358 ret = zynqmp_pm_pinctrl_set_config(pin, param, arg); 359 break; 360 case PIN_CONFIG_BIAS_PULL_UP: 361 param = PM_PINCTRL_CONFIG_PULL_CTRL; 362 arg = PM_PINCTRL_BIAS_PULL_UP; 363 ret = zynqmp_pm_pinctrl_set_config(pin, param, arg); 364 break; 365 case PIN_CONFIG_BIAS_PULL_DOWN: 366 param = PM_PINCTRL_CONFIG_PULL_CTRL; 367 arg = PM_PINCTRL_BIAS_PULL_DOWN; 368 ret = zynqmp_pm_pinctrl_set_config(pin, param, arg); 369 break; 370 case PIN_CONFIG_BIAS_DISABLE: 371 param = PM_PINCTRL_CONFIG_BIAS_STATUS; 372 arg = PM_PINCTRL_BIAS_DISABLE; 373 ret = zynqmp_pm_pinctrl_set_config(pin, param, arg); 374 break; 375 case PIN_CONFIG_INPUT_SCHMITT_ENABLE: 376 param = PM_PINCTRL_CONFIG_SCHMITT_CMOS; 377 ret = zynqmp_pm_pinctrl_set_config(pin, param, arg); 378 break; 379 case PIN_CONFIG_DRIVE_STRENGTH: 380 switch (arg) { 381 case DRIVE_STRENGTH_2MA: 382 value = PM_PINCTRL_DRIVE_STRENGTH_2MA; 383 break; 384 case DRIVE_STRENGTH_4MA: 385 value = PM_PINCTRL_DRIVE_STRENGTH_4MA; 386 break; 387 case DRIVE_STRENGTH_8MA: 388 value = PM_PINCTRL_DRIVE_STRENGTH_8MA; 389 break; 390 case DRIVE_STRENGTH_12MA: 391 value = PM_PINCTRL_DRIVE_STRENGTH_12MA; 392 break; 393 default: 394 /* Invalid drive strength */ 395 dev_warn(pctldev->dev, 396 "Invalid drive strength for pin %d\n", 397 pin); 398 return -EINVAL; 399 } 400 401 param = PM_PINCTRL_CONFIG_DRIVE_STRENGTH; 402 ret = zynqmp_pm_pinctrl_set_config(pin, param, value); 403 break; 404 case PIN_CONFIG_POWER_SOURCE: 405 param = PM_PINCTRL_CONFIG_VOLTAGE_STATUS; 406 ret = zynqmp_pm_pinctrl_get_config(pin, param, &value); 407 408 if (arg != value) 409 dev_warn(pctldev->dev, 410 "Invalid IO Standard requested for pin %d\n", 411 pin); 412 413 break; 414 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: 415 case PIN_CONFIG_MODE_LOW_POWER: 416 /* 417 * These cases are mentioned in dts but configurable 418 * registers are unknown. So falling through to ignore 419 * boot time warnings as of now. 420 */ 421 ret = 0; 422 break; 423 default: 424 dev_warn(pctldev->dev, 425 "unsupported configuration parameter '%u'\n", 426 param); 427 ret = -ENOTSUPP; 428 break; 429 } 430 431 param = pinconf_to_config_param(configs[i]); 432 arg = pinconf_to_config_argument(configs[i]); 433 if (ret) 434 dev_warn(pctldev->dev, 435 "failed to set: pin %u param %u value %u\n", 436 pin, param, arg); 437 } 438 439 return 0; 440 } 441 442 /** 443 * zynqmp_pinconf_group_set() - Set requested config for the group 444 * @pctldev: Pincontrol device pointer. 445 * @selector: Group ID. 446 * @configs: Configuration to set. 447 * @num_configs: Number of configurations. 448 * 449 * Call function to set configs for each pin in the group. 450 * 451 * Return: 0 on success else error code. 452 */ 453 static int zynqmp_pinconf_group_set(struct pinctrl_dev *pctldev, 454 unsigned int selector, 455 unsigned long *configs, 456 unsigned int num_configs) 457 { 458 int i, ret; 459 struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 460 const struct zynqmp_pctrl_group *pgrp = &pctrl->groups[selector]; 461 462 for (i = 0; i < pgrp->npins; i++) { 463 ret = zynqmp_pinconf_cfg_set(pctldev, pgrp->pins[i], configs, 464 num_configs); 465 if (ret) 466 return ret; 467 } 468 469 return 0; 470 } 471 472 static const struct pinconf_ops zynqmp_pinconf_ops = { 473 .is_generic = true, 474 .pin_config_get = zynqmp_pinconf_cfg_get, 475 .pin_config_set = zynqmp_pinconf_cfg_set, 476 .pin_config_group_set = zynqmp_pinconf_group_set, 477 }; 478 479 static struct pinctrl_desc zynqmp_desc = { 480 .name = "zynqmp_pinctrl", 481 .owner = THIS_MODULE, 482 .pctlops = &zynqmp_pctrl_ops, 483 .pmxops = &zynqmp_pinmux_ops, 484 .confops = &zynqmp_pinconf_ops, 485 }; 486 487 static int zynqmp_pinctrl_get_function_groups(u32 fid, u32 index, u16 *groups) 488 { 489 struct zynqmp_pm_query_data qdata = {0}; 490 u32 payload[PAYLOAD_ARG_CNT]; 491 int ret; 492 493 qdata.qid = PM_QID_PINCTRL_GET_FUNCTION_GROUPS; 494 qdata.arg1 = fid; 495 qdata.arg2 = index; 496 497 ret = zynqmp_pm_query_data(qdata, payload); 498 if (ret) 499 return ret; 500 501 memcpy(groups, &payload[1], PINCTRL_GET_FUNC_GROUPS_RESP_LEN); 502 503 return 0; 504 } 505 506 static int zynqmp_pinctrl_get_func_num_groups(u32 fid, unsigned int *ngroups) 507 { 508 struct zynqmp_pm_query_data qdata = {0}; 509 u32 payload[PAYLOAD_ARG_CNT]; 510 int ret; 511 512 qdata.qid = PM_QID_PINCTRL_GET_NUM_FUNCTION_GROUPS; 513 qdata.arg1 = fid; 514 515 ret = zynqmp_pm_query_data(qdata, payload); 516 if (ret) 517 return ret; 518 519 *ngroups = payload[1]; 520 521 return 0; 522 } 523 524 /** 525 * zynqmp_pinctrl_prepare_func_groups() - prepare function and groups data 526 * @dev: Device pointer. 527 * @fid: Function ID. 528 * @func: Function data. 529 * @groups: Groups data. 530 * 531 * Query firmware to get group IDs for each function. Firmware returns 532 * group IDs. Based on the group index for the function, group names in 533 * the function are stored. For example, the first group in "eth0" function 534 * is named as "eth0_0" and the second group as "eth0_1" and so on. 535 * 536 * Based on the group ID received from the firmware, function stores name of 537 * the group for that group ID. For example, if "eth0" first group ID 538 * is x, groups[x] name will be stored as "eth0_0". 539 * 540 * Once done for each function, each function would have its group names 541 * and each group would also have their names. 542 * 543 * Return: 0 on success else error code. 544 */ 545 static int zynqmp_pinctrl_prepare_func_groups(struct device *dev, u32 fid, 546 struct zynqmp_pmux_function *func, 547 struct zynqmp_pctrl_group *groups) 548 { 549 u16 resp[NUM_GROUPS_PER_RESP] = {0}; 550 const char **fgroups; 551 int ret, index, i; 552 553 fgroups = devm_kzalloc(dev, sizeof(*fgroups) * func->ngroups, GFP_KERNEL); 554 if (!fgroups) 555 return -ENOMEM; 556 557 for (index = 0; index < func->ngroups; index += NUM_GROUPS_PER_RESP) { 558 ret = zynqmp_pinctrl_get_function_groups(fid, index, resp); 559 if (ret) 560 return ret; 561 562 for (i = 0; i < NUM_GROUPS_PER_RESP; i++) { 563 if (resp[i] == NA_GROUP) 564 goto done; 565 566 if (resp[i] == RESERVED_GROUP) 567 continue; 568 569 fgroups[index + i] = devm_kasprintf(dev, GFP_KERNEL, 570 "%s_%d_grp", 571 func->name, 572 index + i); 573 if (!fgroups[index + i]) 574 return -ENOMEM; 575 576 groups[resp[i]].name = devm_kasprintf(dev, GFP_KERNEL, 577 "%s_%d_grp", 578 func->name, 579 index + i); 580 if (!groups[resp[i]].name) 581 return -ENOMEM; 582 } 583 } 584 done: 585 func->groups = fgroups; 586 587 return 0; 588 } 589 590 static void zynqmp_pinctrl_get_function_name(u32 fid, char *name) 591 { 592 struct zynqmp_pm_query_data qdata = {0}; 593 u32 payload[PAYLOAD_ARG_CNT]; 594 595 qdata.qid = PM_QID_PINCTRL_GET_FUNCTION_NAME; 596 qdata.arg1 = fid; 597 598 /* 599 * Name of the function is maximum 16 bytes and cannot 600 * accommodate the return value in SMC buffers, hence ignoring 601 * the return value for this specific qid. 602 */ 603 zynqmp_pm_query_data(qdata, payload); 604 memcpy(name, payload, PINCTRL_GET_FUNC_NAME_RESP_LEN); 605 } 606 607 static int zynqmp_pinctrl_get_num_functions(unsigned int *nfuncs) 608 { 609 struct zynqmp_pm_query_data qdata = {0}; 610 u32 payload[PAYLOAD_ARG_CNT]; 611 int ret; 612 613 qdata.qid = PM_QID_PINCTRL_GET_NUM_FUNCTIONS; 614 615 ret = zynqmp_pm_query_data(qdata, payload); 616 if (ret) 617 return ret; 618 619 *nfuncs = payload[1]; 620 621 return 0; 622 } 623 624 static int zynqmp_pinctrl_get_pin_groups(u32 pin, u32 index, u16 *groups) 625 { 626 struct zynqmp_pm_query_data qdata = {0}; 627 u32 payload[PAYLOAD_ARG_CNT]; 628 int ret; 629 630 qdata.qid = PM_QID_PINCTRL_GET_PIN_GROUPS; 631 qdata.arg1 = pin; 632 qdata.arg2 = index; 633 634 ret = zynqmp_pm_query_data(qdata, payload); 635 if (ret) 636 return ret; 637 638 memcpy(groups, &payload[1], PINCTRL_GET_PIN_GROUPS_RESP_LEN); 639 640 return 0; 641 } 642 643 static void zynqmp_pinctrl_group_add_pin(struct zynqmp_pctrl_group *group, 644 unsigned int pin) 645 { 646 group->pins[group->npins++] = pin; 647 } 648 649 /** 650 * zynqmp_pinctrl_create_pin_groups() - assign pins to respective groups 651 * @dev: Device pointer. 652 * @groups: Groups data. 653 * @pin: Pin number. 654 * 655 * Query firmware to get groups available for the given pin. 656 * Based on the firmware response(group IDs for the pin), add 657 * pin number to the respective group's pin array. 658 * 659 * Once all pins are queries, each group would have its number 660 * of pins and pin numbers data. 661 * 662 * Return: 0 on success else error code. 663 */ 664 static int zynqmp_pinctrl_create_pin_groups(struct device *dev, 665 struct zynqmp_pctrl_group *groups, 666 unsigned int pin) 667 { 668 u16 resp[NUM_GROUPS_PER_RESP] = {0}; 669 int ret, i, index = 0; 670 671 do { 672 ret = zynqmp_pinctrl_get_pin_groups(pin, index, resp); 673 if (ret) 674 return ret; 675 676 for (i = 0; i < NUM_GROUPS_PER_RESP; i++) { 677 if (resp[i] == NA_GROUP) 678 return ret; 679 680 if (resp[i] == RESERVED_GROUP) 681 continue; 682 683 zynqmp_pinctrl_group_add_pin(&groups[resp[i]], pin); 684 } 685 index += NUM_GROUPS_PER_RESP; 686 } while (index <= MAX_PIN_GROUPS); 687 688 return 0; 689 } 690 691 /** 692 * zynqmp_pinctrl_prepare_group_pins() - prepare each group's pin data 693 * @dev: Device pointer. 694 * @groups: Groups data. 695 * @ngroups: Number of groups. 696 * 697 * Prepare pin number and number of pins data for each pins. 698 * 699 * Return: 0 on success else error code. 700 */ 701 static int zynqmp_pinctrl_prepare_group_pins(struct device *dev, 702 struct zynqmp_pctrl_group *groups, 703 unsigned int ngroups) 704 { 705 unsigned int pin; 706 int ret; 707 708 for (pin = 0; pin < zynqmp_desc.npins; pin++) { 709 ret = zynqmp_pinctrl_create_pin_groups(dev, groups, pin); 710 if (ret) 711 return ret; 712 } 713 714 return 0; 715 } 716 717 /** 718 * zynqmp_pinctrl_prepare_function_info() - prepare function info 719 * @dev: Device pointer. 720 * @pctrl: Pin control driver data. 721 * 722 * Query firmware for functions, groups and pin information and 723 * prepare pin control driver data. 724 * 725 * Query number of functions and number of function groups (number 726 * of groups in the given function) to allocate required memory buffers 727 * for functions and groups. Once buffers are allocated to store 728 * functions and groups data, query and store required information 729 * (number of groups and group names for each function, number of 730 * pins and pin numbers for each group). 731 * 732 * Return: 0 on success else error code. 733 */ 734 static int zynqmp_pinctrl_prepare_function_info(struct device *dev, 735 struct zynqmp_pinctrl *pctrl) 736 { 737 struct zynqmp_pmux_function *funcs; 738 struct zynqmp_pctrl_group *groups; 739 int ret, i; 740 741 ret = zynqmp_pinctrl_get_num_functions(&pctrl->nfuncs); 742 if (ret) 743 return ret; 744 745 funcs = devm_kzalloc(dev, sizeof(*funcs) * pctrl->nfuncs, GFP_KERNEL); 746 if (!funcs) 747 return -ENOMEM; 748 749 for (i = 0; i < pctrl->nfuncs; i++) { 750 zynqmp_pinctrl_get_function_name(i, funcs[i].name); 751 752 ret = zynqmp_pinctrl_get_func_num_groups(i, &funcs[i].ngroups); 753 if (ret) 754 return ret; 755 756 pctrl->ngroups += funcs[i].ngroups; 757 } 758 759 groups = devm_kzalloc(dev, sizeof(*groups) * pctrl->ngroups, GFP_KERNEL); 760 if (!groups) 761 return -ENOMEM; 762 763 for (i = 0; i < pctrl->nfuncs; i++) { 764 ret = zynqmp_pinctrl_prepare_func_groups(dev, i, &funcs[i], 765 groups); 766 if (ret) 767 return ret; 768 } 769 770 ret = zynqmp_pinctrl_prepare_group_pins(dev, groups, pctrl->ngroups); 771 if (ret) 772 return ret; 773 774 pctrl->funcs = funcs; 775 pctrl->groups = groups; 776 777 return 0; 778 } 779 780 static int zynqmp_pinctrl_get_num_pins(unsigned int *npins) 781 { 782 struct zynqmp_pm_query_data qdata = {0}; 783 u32 payload[PAYLOAD_ARG_CNT]; 784 int ret; 785 786 qdata.qid = PM_QID_PINCTRL_GET_NUM_PINS; 787 788 ret = zynqmp_pm_query_data(qdata, payload); 789 if (ret) 790 return ret; 791 792 *npins = payload[1]; 793 794 return 0; 795 } 796 797 /** 798 * zynqmp_pinctrl_prepare_pin_desc() - prepare pin description info 799 * @dev: Device pointer. 800 * @zynqmp_pins: Pin information. 801 * @npins: Number of pins. 802 * 803 * Query number of pins information from firmware and prepare pin 804 * description containing pin number and pin name. 805 * 806 * Return: 0 on success else error code. 807 */ 808 static int zynqmp_pinctrl_prepare_pin_desc(struct device *dev, 809 const struct pinctrl_pin_desc 810 **zynqmp_pins, 811 unsigned int *npins) 812 { 813 struct pinctrl_pin_desc *pins, *pin; 814 int ret; 815 int i; 816 817 ret = zynqmp_pinctrl_get_num_pins(npins); 818 if (ret) 819 return ret; 820 821 pins = devm_kzalloc(dev, sizeof(*pins) * *npins, GFP_KERNEL); 822 if (!pins) 823 return -ENOMEM; 824 825 for (i = 0; i < *npins; i++) { 826 pin = &pins[i]; 827 pin->number = i; 828 pin->name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", 829 ZYNQMP_PIN_PREFIX, i); 830 if (!pin->name) 831 return -ENOMEM; 832 } 833 834 *zynqmp_pins = pins; 835 836 return 0; 837 } 838 839 static int zynqmp_pinctrl_probe(struct platform_device *pdev) 840 { 841 struct zynqmp_pinctrl *pctrl; 842 int ret; 843 844 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL); 845 if (!pctrl) 846 return -ENOMEM; 847 848 ret = zynqmp_pinctrl_prepare_pin_desc(&pdev->dev, 849 &zynqmp_desc.pins, 850 &zynqmp_desc.npins); 851 if (ret) { 852 dev_err(&pdev->dev, "pin desc prepare fail with %d\n", ret); 853 return ret; 854 } 855 856 ret = zynqmp_pinctrl_prepare_function_info(&pdev->dev, pctrl); 857 if (ret) { 858 dev_err(&pdev->dev, "function info prepare fail with %d\n", ret); 859 return ret; 860 } 861 862 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &zynqmp_desc, pctrl); 863 if (IS_ERR(pctrl->pctrl)) 864 return PTR_ERR(pctrl->pctrl); 865 866 platform_set_drvdata(pdev, pctrl); 867 868 return ret; 869 } 870 871 static const struct of_device_id zynqmp_pinctrl_of_match[] = { 872 { .compatible = "xlnx,zynqmp-pinctrl" }, 873 { } 874 }; 875 MODULE_DEVICE_TABLE(of, zynqmp_pinctrl_of_match); 876 877 static struct platform_driver zynqmp_pinctrl_driver = { 878 .driver = { 879 .name = "zynqmp-pinctrl", 880 .of_match_table = zynqmp_pinctrl_of_match, 881 }, 882 .probe = zynqmp_pinctrl_probe, 883 }; 884 module_platform_driver(zynqmp_pinctrl_driver); 885 886 MODULE_AUTHOR("Sai Krishna Potthuri <lakshmi.sai.krishna.potthuri@xilinx.com>"); 887 MODULE_DESCRIPTION("ZynqMP Pin Controller Driver"); 888 MODULE_LICENSE("GPL v2"); 889