Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 | // SPDX-License-Identifier: GPL-2.0-or-later /* * Core driver for the S32 CC (Common Chassis) pin controller * * Copyright 2017-2022 NXP * Copyright (C) 2022 SUSE LLC * Copyright 2015-2016 Freescale Semiconductor, Inc. */ #include <linux/bitops.h> #include <linux/err.h> #include <linux/gpio/driver.h> #include <linux/init.h> #include <linux/io.h> #include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> #include <linux/pinctrl/machine.h> #include <linux/pinctrl/pinconf.h> #include <linux/pinctrl/pinctrl.h> #include <linux/pinctrl/pinmux.h> #include <linux/regmap.h> #include <linux/seq_file.h> #include <linux/slab.h> #include "../core.h" #include "../pinconf.h" #include "../pinctrl-utils.h" #include "pinctrl-s32.h" #define S32_PIN_ID_SHIFT 4 #define S32_PIN_ID_MASK GENMASK(31, S32_PIN_ID_SHIFT) #define S32_MSCR_SSS_MASK GENMASK(2, 0) #define S32_MSCR_PUS BIT(12) #define S32_MSCR_PUE BIT(13) #define S32_MSCR_SRE(X) (((X) & GENMASK(3, 0)) << 14) #define S32_MSCR_IBE BIT(19) #define S32_MSCR_ODE BIT(20) #define S32_MSCR_OBE BIT(21) static struct regmap_config s32_regmap_config = { .reg_bits = 32, .val_bits = 32, .reg_stride = 4, }; static u32 get_pin_no(u32 pinmux) { return (pinmux & S32_PIN_ID_MASK) >> S32_PIN_ID_SHIFT; } static u32 get_pin_func(u32 pinmux) { return pinmux & GENMASK(3, 0); } struct s32_pinctrl_mem_region { struct regmap *map; const struct s32_pin_range *pin_range; char name[8]; }; /* * Holds pin configuration for GPIO's. * @pin_id: Pin ID for this GPIO * @config: Pin settings * @list: Linked list entry for each gpio pin */ struct gpio_pin_config { unsigned int pin_id; unsigned int config; struct list_head list; }; /* * Pad config save/restore for power suspend/resume. */ struct s32_pinctrl_context { unsigned int *pads; }; /* * @dev: a pointer back to containing device * @pctl: a pointer to the pinctrl device structure * @regions: reserved memory regions with start/end pin * @info: structure containing information about the pin * @gpio_configs: Saved configurations for GPIO pins * @gpiop_configs_lock: lock for the `gpio_configs` list * @s32_pinctrl_context: Configuration saved over system sleep */ struct s32_pinctrl { struct device *dev; struct pinctrl_dev *pctl; struct s32_pinctrl_mem_region *regions; struct s32_pinctrl_soc_info *info; struct list_head gpio_configs; spinlock_t gpio_configs_lock; #ifdef CONFIG_PM_SLEEP struct s32_pinctrl_context saved_context; #endif }; static struct s32_pinctrl_mem_region * s32_get_region(struct pinctrl_dev *pctldev, unsigned int pin) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pin_range *pin_range; unsigned int mem_regions = ipctl->info->soc_data->mem_regions; unsigned int i; for (i = 0; i < mem_regions; i++) { pin_range = ipctl->regions[i].pin_range; if (pin >= pin_range->start && pin <= pin_range->end) return &ipctl->regions[i]; } return NULL; } static inline int s32_check_pin(struct pinctrl_dev *pctldev, unsigned int pin) { return s32_get_region(pctldev, pin) ? 0 : -EINVAL; } static inline int s32_regmap_read(struct pinctrl_dev *pctldev, unsigned int pin, unsigned int *val) { struct s32_pinctrl_mem_region *region; unsigned int offset; region = s32_get_region(pctldev, pin); if (!region) return -EINVAL; offset = (pin - region->pin_range->start) * regmap_get_reg_stride(region->map); return regmap_read(region->map, offset, val); } static inline int s32_regmap_write(struct pinctrl_dev *pctldev, unsigned int pin, unsigned int val) { struct s32_pinctrl_mem_region *region; unsigned int offset; region = s32_get_region(pctldev, pin); if (!region) return -EINVAL; offset = (pin - region->pin_range->start) * regmap_get_reg_stride(region->map); return regmap_write(region->map, offset, val); } static inline int s32_regmap_update(struct pinctrl_dev *pctldev, unsigned int pin, unsigned int mask, unsigned int val) { struct s32_pinctrl_mem_region *region; unsigned int offset; region = s32_get_region(pctldev, pin); if (!region) return -EINVAL; offset = (pin - region->pin_range->start) * regmap_get_reg_stride(region->map); return regmap_update_bits(region->map, offset, mask, val); } static int s32_get_groups_count(struct pinctrl_dev *pctldev) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; return info->ngroups; } static const char *s32_get_group_name(struct pinctrl_dev *pctldev, unsigned int selector) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; return info->groups[selector].data.name; } static int s32_get_group_pins(struct pinctrl_dev *pctldev, unsigned int selector, const unsigned int **pins, unsigned int *npins) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; *pins = info->groups[selector].data.pins; *npins = info->groups[selector].data.npins; return 0; } static void s32_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, unsigned int offset) { seq_printf(s, "%s", dev_name(pctldev->dev)); } static int s32_dt_group_node_to_map(struct pinctrl_dev *pctldev, struct device_node *np, struct pinctrl_map **map, unsigned int *reserved_maps, unsigned int *num_maps, const char *func_name) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); struct device *dev = ipctl->dev; unsigned long *cfgs = NULL; unsigned int n_cfgs, reserve = 1; int n_pins, ret; n_pins = of_property_count_elems_of_size(np, "pinmux", sizeof(u32)); if (n_pins < 0) { dev_warn(dev, "Can't find 'pinmux' property in node %pOFn\n", np); } else if (!n_pins) { return -EINVAL; } ret = pinconf_generic_parse_dt_config(np, pctldev, &cfgs, &n_cfgs); if (ret) { dev_err(dev, "%pOF: could not parse node property\n", np); return ret; } if (n_cfgs) reserve++; ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps, reserve); if (ret < 0) goto free_cfgs; ret = pinctrl_utils_add_map_mux(pctldev, map, reserved_maps, num_maps, np->name, func_name); if (ret < 0) goto free_cfgs; if (n_cfgs) { ret = pinctrl_utils_add_map_configs(pctldev, map, reserved_maps, num_maps, np->name, cfgs, n_cfgs, PIN_MAP_TYPE_CONFIGS_GROUP); if (ret < 0) goto free_cfgs; } free_cfgs: kfree(cfgs); return ret; } static int s32_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node *np_config, struct pinctrl_map **map, unsigned int *num_maps) { unsigned int reserved_maps; struct device_node *np; int ret = 0; reserved_maps = 0; *map = NULL; *num_maps = 0; for_each_available_child_of_node(np_config, np) { ret = s32_dt_group_node_to_map(pctldev, np, map, &reserved_maps, num_maps, np_config->name); if (ret < 0) { of_node_put(np); break; } } if (ret) pinctrl_utils_free_map(pctldev, *map, *num_maps); return ret; } static const struct pinctrl_ops s32_pctrl_ops = { .get_groups_count = s32_get_groups_count, .get_group_name = s32_get_group_name, .get_group_pins = s32_get_group_pins, .pin_dbg_show = s32_pin_dbg_show, .dt_node_to_map = s32_dt_node_to_map, .dt_free_map = pinctrl_utils_free_map, }; static int s32_pmx_set(struct pinctrl_dev *pctldev, unsigned int selector, unsigned int group) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; int i, ret; struct s32_pin_group *grp; /* * Configure the mux mode for each pin in the group for a specific * function. */ grp = &info->groups[group]; dev_dbg(ipctl->dev, "set mux for function %s group %s\n", info->functions[selector].name, grp->data.name); /* Check beforehand so we don't have a partial config. */ for (i = 0; i < grp->data.npins; i++) { if (s32_check_pin(pctldev, grp->data.pins[i]) != 0) { dev_err(info->dev, "invalid pin: %u in group: %u\n", grp->data.pins[i], group); return -EINVAL; } } for (i = 0, ret = 0; i < grp->data.npins && !ret; i++) { ret = s32_regmap_update(pctldev, grp->data.pins[i], S32_MSCR_SSS_MASK, grp->pin_sss[i]); if (ret) { dev_err(info->dev, "Failed to set pin %u\n", grp->data.pins[i]); return ret; } } return 0; } static int s32_pmx_get_funcs_count(struct pinctrl_dev *pctldev) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; return info->nfunctions; } static const char *s32_pmx_get_func_name(struct pinctrl_dev *pctldev, unsigned int selector) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; return info->functions[selector].name; } static int s32_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned int selector, const char * const **groups, unsigned int * const num_groups) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; *groups = info->functions[selector].groups; *num_groups = info->functions[selector].ngroups; return 0; } static int s32_pmx_gpio_request_enable(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsigned int offset) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); struct gpio_pin_config *gpio_pin; unsigned int config; unsigned long flags; int ret; ret = s32_regmap_read(pctldev, offset, &config); if (ret) return ret; /* Save current configuration */ gpio_pin = kmalloc(sizeof(*gpio_pin), GFP_KERNEL); if (!gpio_pin) return -ENOMEM; gpio_pin->pin_id = offset; gpio_pin->config = config; spin_lock_irqsave(&ipctl->gpio_configs_lock, flags); list_add(&gpio_pin->list, &ipctl->gpio_configs); spin_unlock_irqrestore(&ipctl->gpio_configs_lock, flags); /* GPIO pin means SSS = 0 */ config &= ~S32_MSCR_SSS_MASK; return s32_regmap_write(pctldev, offset, config); } static void s32_pmx_gpio_disable_free(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsigned int offset) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); struct gpio_pin_config *gpio_pin, *tmp; unsigned long flags; int ret; spin_lock_irqsave(&ipctl->gpio_configs_lock, flags); list_for_each_entry_safe(gpio_pin, tmp, &ipctl->gpio_configs, list) { if (gpio_pin->pin_id == offset) { ret = s32_regmap_write(pctldev, gpio_pin->pin_id, gpio_pin->config); if (ret != 0) goto unlock; list_del(&gpio_pin->list); kfree(gpio_pin); break; } } unlock: spin_unlock_irqrestore(&ipctl->gpio_configs_lock, flags); } static int s32_pmx_gpio_set_direction(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsigned int offset, bool input) { unsigned int config; unsigned int mask = S32_MSCR_IBE | S32_MSCR_OBE; if (input) { /* Disable output buffer and enable input buffer */ config = S32_MSCR_IBE; } else { /* Disable input buffer and enable output buffer */ config = S32_MSCR_OBE; } return s32_regmap_update(pctldev, offset, mask, config); } static const struct pinmux_ops s32_pmx_ops = { .get_functions_count = s32_pmx_get_funcs_count, .get_function_name = s32_pmx_get_func_name, .get_function_groups = s32_pmx_get_groups, .set_mux = s32_pmx_set, .gpio_request_enable = s32_pmx_gpio_request_enable, .gpio_disable_free = s32_pmx_gpio_disable_free, .gpio_set_direction = s32_pmx_gpio_set_direction, }; /* Set the reserved elements as -1 */ static const int support_slew[] = {208, -1, -1, -1, 166, 150, 133, 83}; static int s32_get_slew_regval(int arg) { unsigned int i; /* Translate a real slew rate (MHz) to a register value */ for (i = 0; i < ARRAY_SIZE(support_slew); i++) { if (arg == support_slew[i]) return i; } return -EINVAL; } static inline void s32_pin_set_pull(enum pin_config_param param, unsigned int *mask, unsigned int *config) { switch (param) { case PIN_CONFIG_BIAS_DISABLE: case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: *config &= ~(S32_MSCR_PUS | S32_MSCR_PUE); break; case PIN_CONFIG_BIAS_PULL_UP: *config |= S32_MSCR_PUS | S32_MSCR_PUE; break; case PIN_CONFIG_BIAS_PULL_DOWN: *config &= ~S32_MSCR_PUS; *config |= S32_MSCR_PUE; break; default: return; } *mask |= S32_MSCR_PUS | S32_MSCR_PUE; } static int s32_parse_pincfg(unsigned long pincfg, unsigned int *mask, unsigned int *config) { enum pin_config_param param; u32 arg; int ret; param = pinconf_to_config_param(pincfg); arg = pinconf_to_config_argument(pincfg); switch (param) { /* All pins are persistent over suspend */ case PIN_CONFIG_PERSIST_STATE: return 0; case PIN_CONFIG_DRIVE_OPEN_DRAIN: *config |= S32_MSCR_ODE; *mask |= S32_MSCR_ODE; break; case PIN_CONFIG_OUTPUT_ENABLE: if (arg) *config |= S32_MSCR_OBE; else *config &= ~S32_MSCR_OBE; *mask |= S32_MSCR_OBE; break; case PIN_CONFIG_INPUT_ENABLE: if (arg) *config |= S32_MSCR_IBE; else *config &= ~S32_MSCR_IBE; *mask |= S32_MSCR_IBE; break; case PIN_CONFIG_SLEW_RATE: ret = s32_get_slew_regval(arg); if (ret < 0) return ret; *config |= S32_MSCR_SRE((u32)ret); *mask |= S32_MSCR_SRE(~0); break; case PIN_CONFIG_BIAS_DISABLE: case PIN_CONFIG_BIAS_PULL_UP: case PIN_CONFIG_BIAS_PULL_DOWN: s32_pin_set_pull(param, mask, config); break; case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: *config &= ~(S32_MSCR_ODE | S32_MSCR_OBE | S32_MSCR_IBE); *mask |= S32_MSCR_ODE | S32_MSCR_OBE | S32_MSCR_IBE; s32_pin_set_pull(param, mask, config); break; default: return -EOPNOTSUPP; } return 0; } static int s32_pinconf_mscr_update(struct pinctrl_dev *pctldev, unsigned int pin_id, unsigned long *configs, unsigned int num_configs) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); unsigned int config = 0, mask = 0; int i, ret; ret = s32_check_pin(pctldev, pin_id); if (ret) return ret; dev_dbg(ipctl->dev, "pinconf set pin %s with %u configs\n", pin_get_name(pctldev, pin_id), num_configs); for (i = 0; i < num_configs; i++) { ret = s32_parse_pincfg(configs[i], &mask, &config); if (ret) return ret; } if (!config && !mask) return 0; dev_dbg(ipctl->dev, "update: pin %u cfg 0x%x\n", pin_id, config); return s32_regmap_update(pctldev, pin_id, mask, config); } static int s32_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin_id, unsigned long *config) { return s32_regmap_read(pctldev, pin_id, (unsigned int *)config); } static int s32_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin_id, unsigned long *configs, unsigned int num_configs) { return s32_pinconf_mscr_update(pctldev, pin_id, configs, num_configs); } static int s32_pconf_group_set(struct pinctrl_dev *pctldev, unsigned int selector, unsigned long *configs, unsigned int num_configs) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; struct s32_pin_group *grp; int i, ret; grp = &info->groups[selector]; for (i = 0; i < grp->data.npins; i++) { ret = s32_pinconf_mscr_update(pctldev, grp->data.pins[i], configs, num_configs); if (ret) return ret; } return 0; } static void s32_pinconf_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, unsigned int pin_id) { unsigned int config; int ret; ret = s32_regmap_read(pctldev, pin_id, &config); if (ret) return; seq_printf(s, "0x%x", config); } static void s32_pinconf_group_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, unsigned int selector) { struct s32_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); const struct s32_pinctrl_soc_info *info = ipctl->info; struct s32_pin_group *grp; unsigned int config; const char *name; int i, ret; seq_puts(s, "\n"); grp = &info->groups[selector]; for (i = 0; i < grp->data.npins; i++) { name = pin_get_name(pctldev, grp->data.pins[i]); ret = s32_regmap_read(pctldev, grp->data.pins[i], &config); if (ret) return; seq_printf(s, "%s: 0x%x\n", name, config); } } static const struct pinconf_ops s32_pinconf_ops = { .pin_config_get = s32_pinconf_get, .pin_config_set = s32_pinconf_set, .pin_config_group_set = s32_pconf_group_set, .pin_config_dbg_show = s32_pinconf_dbg_show, .pin_config_group_dbg_show = s32_pinconf_group_dbg_show, }; #ifdef CONFIG_PM_SLEEP static bool s32_pinctrl_should_save(struct s32_pinctrl *ipctl, unsigned int pin) { const struct pin_desc *pd = pin_desc_get(ipctl->pctl, pin); if (!pd) return false; /* * Only restore the pin if it is actually in use by the kernel (or * by userspace). */ if (pd->mux_owner || pd->gpio_owner) return true; return false; } int s32_pinctrl_suspend(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); struct s32_pinctrl *ipctl = platform_get_drvdata(pdev); const struct pinctrl_pin_desc *pin; const struct s32_pinctrl_soc_info *info = ipctl->info; struct s32_pinctrl_context *saved_context = &ipctl->saved_context; int i; int ret; unsigned int config; for (i = 0; i < info->soc_data->npins; i++) { pin = &info->soc_data->pins[i]; if (!s32_pinctrl_should_save(ipctl, pin->number)) continue; ret = s32_regmap_read(ipctl->pctl, pin->number, &config); if (ret) return -EINVAL; saved_context->pads[i] = config; } return 0; } int s32_pinctrl_resume(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); struct s32_pinctrl *ipctl = platform_get_drvdata(pdev); const struct s32_pinctrl_soc_info *info = ipctl->info; const struct pinctrl_pin_desc *pin; struct s32_pinctrl_context *saved_context = &ipctl->saved_context; int ret, i; for (i = 0; i < info->soc_data->npins; i++) { pin = &info->soc_data->pins[i]; if (!s32_pinctrl_should_save(ipctl, pin->number)) continue; ret = s32_regmap_write(ipctl->pctl, pin->number, saved_context->pads[i]); if (ret) return ret; } return 0; } #endif static int s32_pinctrl_parse_groups(struct device_node *np, struct s32_pin_group *grp, struct s32_pinctrl_soc_info *info) { const __be32 *p; struct device *dev; struct property *prop; unsigned int *pins, *sss; int i, npins; u32 pinmux; dev = info->dev; dev_dbg(dev, "group: %pOFn\n", np); /* Initialise group */ grp->data.name = np->name; npins = of_property_count_elems_of_size(np, "pinmux", sizeof(u32)); if (npins < 0) { dev_err(dev, "Failed to read 'pinmux' property in node %s.\n", grp->data.name); return -EINVAL; } if (!npins) { dev_err(dev, "The group %s has no pins.\n", grp->data.name); return -EINVAL; } grp->data.npins = npins; pins = devm_kcalloc(info->dev, npins, sizeof(*pins), GFP_KERNEL); sss = devm_kcalloc(info->dev, npins, sizeof(*sss), GFP_KERNEL); if (!pins || !sss) return -ENOMEM; i = 0; of_property_for_each_u32(np, "pinmux", prop, p, pinmux) { pins[i] = get_pin_no(pinmux); sss[i] = get_pin_func(pinmux); dev_dbg(info->dev, "pin: 0x%x, sss: 0x%x", pins[i], sss[i]); i++; } grp->data.pins = pins; grp->pin_sss = sss; return 0; } static int s32_pinctrl_parse_functions(struct device_node *np, struct s32_pinctrl_soc_info *info, u32 index) { struct device_node *child; struct pinfunction *func; struct s32_pin_group *grp; const char **groups; u32 i = 0; int ret = 0; dev_dbg(info->dev, "parse function(%u): %pOFn\n", index, np); func = &info->functions[index]; /* Initialise function */ func->name = np->name; func->ngroups = of_get_child_count(np); if (func->ngroups == 0) { dev_err(info->dev, "no groups defined in %pOF\n", np); return -EINVAL; } groups = devm_kcalloc(info->dev, func->ngroups, sizeof(*func->groups), GFP_KERNEL); if (!groups) return -ENOMEM; for_each_child_of_node(np, child) { groups[i] = child->name; grp = &info->groups[info->grp_index++]; ret = s32_pinctrl_parse_groups(child, grp, info); if (ret) { of_node_put(child); return ret; } i++; } func->groups = groups; return 0; } static int s32_pinctrl_probe_dt(struct platform_device *pdev, struct s32_pinctrl *ipctl) { struct s32_pinctrl_soc_info *info = ipctl->info; struct device_node *np = pdev->dev.of_node; struct device_node *child; struct resource *res; struct regmap *map; void __iomem *base; unsigned int mem_regions = info->soc_data->mem_regions; int ret; u32 nfuncs = 0; u32 i = 0; if (!np) return -ENODEV; if (mem_regions == 0 || mem_regions >= 10000) { dev_err(&pdev->dev, "mem_regions is invalid: %u\n", mem_regions); return -EINVAL; } ipctl->regions = devm_kcalloc(&pdev->dev, mem_regions, sizeof(*ipctl->regions), GFP_KERNEL); if (!ipctl->regions) return -ENOMEM; for (i = 0; i < mem_regions; i++) { base = devm_platform_get_and_ioremap_resource(pdev, i, &res); if (IS_ERR(base)) return PTR_ERR(base); snprintf(ipctl->regions[i].name, sizeof(ipctl->regions[i].name), "map%u", i); s32_regmap_config.name = ipctl->regions[i].name; s32_regmap_config.max_register = resource_size(res) - s32_regmap_config.reg_stride; map = devm_regmap_init_mmio(&pdev->dev, base, &s32_regmap_config); if (IS_ERR(map)) { dev_err(&pdev->dev, "Failed to init regmap[%u]\n", i); return PTR_ERR(map); } ipctl->regions[i].map = map; ipctl->regions[i].pin_range = &info->soc_data->mem_pin_ranges[i]; } nfuncs = of_get_child_count(np); if (nfuncs <= 0) { dev_err(&pdev->dev, "no functions defined\n"); return -EINVAL; } info->nfunctions = nfuncs; info->functions = devm_kcalloc(&pdev->dev, nfuncs, sizeof(*info->functions), GFP_KERNEL); if (!info->functions) return -ENOMEM; info->ngroups = 0; for_each_child_of_node(np, child) info->ngroups += of_get_child_count(child); info->groups = devm_kcalloc(&pdev->dev, info->ngroups, sizeof(*info->groups), GFP_KERNEL); if (!info->groups) return -ENOMEM; i = 0; for_each_child_of_node(np, child) { ret = s32_pinctrl_parse_functions(child, info, i++); if (ret) { of_node_put(child); return ret; } } return 0; } int s32_pinctrl_probe(struct platform_device *pdev, const struct s32_pinctrl_soc_data *soc_data) { struct s32_pinctrl *ipctl; int ret; struct pinctrl_desc *s32_pinctrl_desc; struct s32_pinctrl_soc_info *info; #ifdef CONFIG_PM_SLEEP struct s32_pinctrl_context *saved_context; #endif if (!soc_data || !soc_data->pins || !soc_data->npins) { dev_err(&pdev->dev, "wrong pinctrl info\n"); return -EINVAL; } info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); if (!info) return -ENOMEM; info->soc_data = soc_data; info->dev = &pdev->dev; /* Create state holders etc for this driver */ ipctl = devm_kzalloc(&pdev->dev, sizeof(*ipctl), GFP_KERNEL); if (!ipctl) return -ENOMEM; ipctl->info = info; ipctl->dev = info->dev; platform_set_drvdata(pdev, ipctl); INIT_LIST_HEAD(&ipctl->gpio_configs); spin_lock_init(&ipctl->gpio_configs_lock); s32_pinctrl_desc = devm_kmalloc(&pdev->dev, sizeof(*s32_pinctrl_desc), GFP_KERNEL); if (!s32_pinctrl_desc) return -ENOMEM; s32_pinctrl_desc->name = dev_name(&pdev->dev); s32_pinctrl_desc->pins = info->soc_data->pins; s32_pinctrl_desc->npins = info->soc_data->npins; s32_pinctrl_desc->pctlops = &s32_pctrl_ops; s32_pinctrl_desc->pmxops = &s32_pmx_ops; s32_pinctrl_desc->confops = &s32_pinconf_ops; s32_pinctrl_desc->owner = THIS_MODULE; ret = s32_pinctrl_probe_dt(pdev, ipctl); if (ret) { dev_err(&pdev->dev, "fail to probe dt properties\n"); return ret; } ipctl->pctl = devm_pinctrl_register(&pdev->dev, s32_pinctrl_desc, ipctl); if (IS_ERR(ipctl->pctl)) return dev_err_probe(&pdev->dev, PTR_ERR(ipctl->pctl), "could not register s32 pinctrl driver\n"); #ifdef CONFIG_PM_SLEEP saved_context = &ipctl->saved_context; saved_context->pads = devm_kcalloc(&pdev->dev, info->soc_data->npins, sizeof(*saved_context->pads), GFP_KERNEL); if (!saved_context->pads) return -ENOMEM; #endif dev_info(&pdev->dev, "initialized s32 pinctrl driver\n"); return 0; } |