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 | // SPDX-License-Identifier: GPL-2.0-or-later /* * Realtek DHC pin controller driver * * Copyright (c) 2023 Realtek Semiconductor Corp. */ #include <linux/bitops.h> #include <linux/io.h> #include <linux/module.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/pinctrl/machine.h> #include <linux/pinctrl/pinconf.h> #include <linux/pinctrl/pinconf-generic.h> #include <linux/pinctrl/pinctrl.h> #include <linux/pinctrl/pinmux.h> #include <linux/platform_device.h> #include <linux/seq_file.h> #include <linux/regmap.h> #include <linux/slab.h> #include "../core.h" #include "../pinctrl-utils.h" #include "pinctrl-rtd.h" struct rtd_pinctrl { struct device *dev; struct pinctrl_dev *pcdev; void __iomem *base; struct pinctrl_desc desc; const struct rtd_pinctrl_desc *info; struct regmap *regmap_pinctrl; }; /* custom pinconf parameters */ #define RTD_DRIVE_STRENGH_P (PIN_CONFIG_END + 1) #define RTD_DRIVE_STRENGH_N (PIN_CONFIG_END + 2) #define RTD_DUTY_CYCLE (PIN_CONFIG_END + 3) static const struct pinconf_generic_params rtd_custom_bindings[] = { {"realtek,drive-strength-p", RTD_DRIVE_STRENGH_P, 0}, {"realtek,drive-strength-n", RTD_DRIVE_STRENGH_N, 0}, {"realtek,duty-cycle", RTD_DUTY_CYCLE, 0}, }; static int rtd_pinctrl_get_groups_count(struct pinctrl_dev *pcdev) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); return data->info->num_groups; } static const char *rtd_pinctrl_get_group_name(struct pinctrl_dev *pcdev, unsigned int selector) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); return data->info->groups[selector].name; } static int rtd_pinctrl_get_group_pins(struct pinctrl_dev *pcdev, unsigned int selector, const unsigned int **pins, unsigned int *num_pins) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); *pins = data->info->groups[selector].pins; *num_pins = data->info->groups[selector].num_pins; return 0; } static void rtd_pinctrl_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s, unsigned int offset) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); const struct rtd_pin_desc *mux = &data->info->muxes[offset]; const struct rtd_pin_mux_desc *func; u32 val; u32 mask; u32 pin_val; int is_map; if (!mux->name) { seq_puts(s, "[not defined]"); return; } val = readl_relaxed(data->base + mux->mux_offset); mask = mux->mux_mask; pin_val = val & mask; is_map = 0; func = &mux->functions[0]; seq_puts(s, "function: "); while (func->name) { if (func->mux_value == pin_val) { is_map = 1; seq_printf(s, "[%s] ", func->name); } else { seq_printf(s, "%s ", func->name); } func++; } if (!is_map) seq_puts(s, "[not defined]"); } static const struct pinctrl_ops rtd_pinctrl_ops = { .dt_node_to_map = pinconf_generic_dt_node_to_map_all, .dt_free_map = pinctrl_utils_free_map, .get_groups_count = rtd_pinctrl_get_groups_count, .get_group_name = rtd_pinctrl_get_group_name, .get_group_pins = rtd_pinctrl_get_group_pins, .pin_dbg_show = rtd_pinctrl_dbg_show, }; static int rtd_pinctrl_get_functions_count(struct pinctrl_dev *pcdev) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); return data->info->num_functions; } static const char *rtd_pinctrl_get_function_name(struct pinctrl_dev *pcdev, unsigned int selector) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); return data->info->functions[selector].name; } static int rtd_pinctrl_get_function_groups(struct pinctrl_dev *pcdev, unsigned int selector, const char * const **groups, unsigned int * const num_groups) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); *groups = data->info->functions[selector].groups; *num_groups = data->info->functions[selector].num_groups; return 0; } static const struct rtd_pin_desc *rtd_pinctrl_find_mux(struct rtd_pinctrl *data, unsigned int pin) { if (data->info->muxes[pin].name) return &data->info->muxes[pin]; return NULL; } static int rtd_pinctrl_set_one_mux(struct pinctrl_dev *pcdev, unsigned int pin, const char *func_name) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); const struct rtd_pin_desc *mux; int ret = 0; int i; mux = rtd_pinctrl_find_mux(data, pin); if (!mux) return 0; if (!mux->functions) { if (!mux->name) dev_err(pcdev->dev, "NULL pin has no functions\n"); else dev_err(pcdev->dev, "No functions available for pin %s\n", mux->name); return -ENOTSUPP; } for (i = 0; mux->functions[i].name; i++) { if (strcmp(mux->functions[i].name, func_name) != 0) continue; ret = regmap_update_bits(data->regmap_pinctrl, mux->mux_offset, mux->mux_mask, mux->functions[i].mux_value); return ret; } if (!mux->name) { dev_err(pcdev->dev, "NULL pin provided for function %s\n", func_name); return -EINVAL; } dev_err(pcdev->dev, "No function %s available for pin %s\n", func_name, mux->name); return -EINVAL; } static int rtd_pinctrl_set_mux(struct pinctrl_dev *pcdev, unsigned int function, unsigned int group) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); const unsigned int *pins; unsigned int num_pins; const char *func_name; const char *group_name; int i, ret; func_name = data->info->functions[function].name; group_name = data->info->groups[group].name; ret = rtd_pinctrl_get_group_pins(pcdev, group, &pins, &num_pins); if (ret) { dev_err(pcdev->dev, "Getting pins for group %s failed\n", group_name); return ret; } for (i = 0; i < num_pins; i++) { ret = rtd_pinctrl_set_one_mux(pcdev, pins[i], func_name); if (ret) return ret; } return 0; } static int rtd_pinctrl_gpio_request_enable(struct pinctrl_dev *pcdev, struct pinctrl_gpio_range *range, unsigned int offset) { return rtd_pinctrl_set_one_mux(pcdev, offset, "gpio"); } static const struct pinmux_ops rtd_pinmux_ops = { .get_functions_count = rtd_pinctrl_get_functions_count, .get_function_name = rtd_pinctrl_get_function_name, .get_function_groups = rtd_pinctrl_get_function_groups, .set_mux = rtd_pinctrl_set_mux, .gpio_request_enable = rtd_pinctrl_gpio_request_enable, }; static const struct pinctrl_pin_desc *rtd_pinctrl_get_pin_by_number(struct rtd_pinctrl *data, int number) { int i; for (i = 0; i < data->info->num_pins; i++) { if (data->info->pins[i].number == number) return &data->info->pins[i]; } return NULL; } static const struct rtd_pin_config_desc *rtd_pinctrl_find_config(struct rtd_pinctrl *data, unsigned int pin) { if (data->info->configs[pin].name) return &data->info->configs[pin]; return NULL; } static const struct rtd_pin_sconfig_desc *rtd_pinctrl_find_sconfig(struct rtd_pinctrl *data, unsigned int pin) { int i; const struct pinctrl_pin_desc *pin_desc; const char *pin_name; pin_desc = rtd_pinctrl_get_pin_by_number(data, pin); if (!pin_desc) return NULL; pin_name = pin_desc->name; for (i = 0; i < data->info->num_sconfigs; i++) { if (strcmp(data->info->sconfigs[i].name, pin_name) == 0) return &data->info->sconfigs[i]; } return NULL; } static int rtd_pconf_parse_conf(struct rtd_pinctrl *data, unsigned int pinnr, enum pin_config_param param, enum pin_config_param arg) { const struct rtd_pin_config_desc *config_desc; const struct rtd_pin_sconfig_desc *sconfig_desc; u8 set_val = 0; u16 strength; u32 val; u32 mask; u32 pulsel_off, pulen_off, smt_off, curr_off, pow_off, reg_off, p_off, n_off; const char *name = data->info->pins[pinnr].name; int ret = 0; config_desc = rtd_pinctrl_find_config(data, pinnr); if (!config_desc) { dev_err(data->dev, "Not support pin config for pin: %s\n", name); return -ENOTSUPP; } switch ((u32)param) { case PIN_CONFIG_INPUT_SCHMITT: case PIN_CONFIG_INPUT_SCHMITT_ENABLE: if (config_desc->smt_offset == NA) { dev_err(data->dev, "Not support input schmitt for pin: %s\n", name); return -ENOTSUPP; } smt_off = config_desc->base_bit + config_desc->smt_offset; reg_off = config_desc->reg_offset; set_val = arg; mask = BIT(smt_off); val = set_val ? BIT(smt_off) : 0; break; case PIN_CONFIG_DRIVE_PUSH_PULL: if (config_desc->pud_en_offset == NA) { dev_err(data->dev, "Not support push pull for pin: %s\n", name); return -ENOTSUPP; } pulen_off = config_desc->base_bit + config_desc->pud_en_offset; reg_off = config_desc->reg_offset; mask = BIT(pulen_off); val = 0; break; case PIN_CONFIG_BIAS_DISABLE: if (config_desc->pud_en_offset == NA) { dev_err(data->dev, "Not support bias disable for pin: %s\n", name); return -ENOTSUPP; } pulen_off = config_desc->base_bit + config_desc->pud_en_offset; reg_off = config_desc->reg_offset; mask = BIT(pulen_off); val = 0; break; case PIN_CONFIG_BIAS_PULL_UP: if (config_desc->pud_en_offset == NA) { dev_err(data->dev, "Not support bias pull up for pin:%s\n", name); return -ENOTSUPP; } pulen_off = config_desc->base_bit + config_desc->pud_en_offset; pulsel_off = config_desc->base_bit + config_desc->pud_sel_offset; reg_off = config_desc->reg_offset; mask = BIT(pulen_off) | BIT(pulsel_off); val = mask; break; case PIN_CONFIG_BIAS_PULL_DOWN: if (config_desc->pud_en_offset == NA) { dev_err(data->dev, "Not support bias pull down for pin: %s\n", name); return -ENOTSUPP; } pulen_off = config_desc->base_bit + config_desc->pud_en_offset; pulsel_off = config_desc->base_bit + config_desc->pud_sel_offset; reg_off = config_desc->reg_offset; mask = BIT(pulen_off) | BIT(pulsel_off); val = BIT(pulen_off); break; case PIN_CONFIG_DRIVE_STRENGTH: curr_off = config_desc->base_bit + config_desc->curr_offset; reg_off = config_desc->reg_offset; strength = arg; val = 0; switch (config_desc->curr_type) { case PADDRI_4_8: if (strength == 4) val = 0; else if (strength == 8) val = BIT(curr_off); else return -EINVAL; break; case PADDRI_2_4: if (strength == 2) val = 0; else if (strength == 4) val = BIT(curr_off); else return -EINVAL; break; case NA: dev_err(data->dev, "Not support drive strength for pin: %s\n", name); return -ENOTSUPP; default: return -EINVAL; } mask = BIT(curr_off); break; case PIN_CONFIG_POWER_SOURCE: if (config_desc->power_offset == NA) { dev_err(data->dev, "Not support power source for pin: %s\n", name); return -ENOTSUPP; } reg_off = config_desc->reg_offset; pow_off = config_desc->base_bit + config_desc->power_offset; if (pow_off >= 32) { reg_off += 0x4; pow_off -= 32; } set_val = arg; mask = BIT(pow_off); val = set_val ? mask : 0; break; case RTD_DRIVE_STRENGH_P: sconfig_desc = rtd_pinctrl_find_sconfig(data, pinnr); if (!sconfig_desc) { dev_err(data->dev, "Not support P driving for pin: %s\n", name); return -ENOTSUPP; } set_val = arg; reg_off = sconfig_desc->reg_offset; p_off = sconfig_desc->pdrive_offset; if (p_off >= 32) { reg_off += 0x4; p_off -= 32; } mask = GENMASK(p_off + sconfig_desc->pdrive_maskbits - 1, p_off); val = set_val << p_off; break; case RTD_DRIVE_STRENGH_N: sconfig_desc = rtd_pinctrl_find_sconfig(data, pinnr); if (!sconfig_desc) { dev_err(data->dev, "Not support N driving for pin: %s\n", name); return -ENOTSUPP; } set_val = arg; reg_off = sconfig_desc->reg_offset; n_off = sconfig_desc->ndrive_offset; if (n_off >= 32) { reg_off += 0x4; n_off -= 32; } mask = GENMASK(n_off + sconfig_desc->ndrive_maskbits - 1, n_off); val = set_val << n_off; break; case RTD_DUTY_CYCLE: sconfig_desc = rtd_pinctrl_find_sconfig(data, pinnr); if (!sconfig_desc || sconfig_desc->dcycle_offset == NA) { dev_err(data->dev, "Not support duty cycle for pin: %s\n", name); return -ENOTSUPP; } set_val = arg; reg_off = config_desc->reg_offset; mask = GENMASK(sconfig_desc->dcycle_offset + sconfig_desc->dcycle_maskbits - 1, sconfig_desc->dcycle_offset); val = set_val << sconfig_desc->dcycle_offset; break; default: dev_err(data->dev, "unsupported pinconf: %d\n", (u32)param); return -EINVAL; } ret = regmap_update_bits(data->regmap_pinctrl, reg_off, mask, val); if (ret) dev_err(data->dev, "could not update pinconf(%d) for pin(%s)\n", (u32)param, name); return ret; } static int rtd_pin_config_get(struct pinctrl_dev *pcdev, unsigned int pinnr, unsigned long *config) { unsigned int param = pinconf_to_config_param(*config); unsigned int arg = 0; switch (param) { default: return -ENOTSUPP; } *config = pinconf_to_config_packed(param, arg); return 0; } static int rtd_pin_config_set(struct pinctrl_dev *pcdev, unsigned int pinnr, unsigned long *configs, unsigned int num_configs) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); int i; int ret = 0; for (i = 0; i < num_configs; i++) { ret = rtd_pconf_parse_conf(data, pinnr, pinconf_to_config_param(configs[i]), pinconf_to_config_argument(configs[i])); if (ret < 0) return ret; } return 0; } static int rtd_pin_config_group_set(struct pinctrl_dev *pcdev, unsigned int group, unsigned long *configs, unsigned int num_configs) { struct rtd_pinctrl *data = pinctrl_dev_get_drvdata(pcdev); const unsigned int *pins; unsigned int num_pins; const char *group_name; int i, ret; group_name = data->info->groups[group].name; ret = rtd_pinctrl_get_group_pins(pcdev, group, &pins, &num_pins); if (ret) { dev_err(pcdev->dev, "Getting pins for group %s failed\n", group_name); return ret; } for (i = 0; i < num_pins; i++) { ret = rtd_pin_config_set(pcdev, pins[i], configs, num_configs); if (ret) return ret; } return 0; } static const struct pinconf_ops rtd_pinconf_ops = { .is_generic = true, .pin_config_get = rtd_pin_config_get, .pin_config_set = rtd_pin_config_set, .pin_config_group_set = rtd_pin_config_group_set, }; static const struct regmap_config rtd_pinctrl_regmap_config = { .reg_bits = 32, .val_bits = 32, .reg_stride = 4, .use_relaxed_mmio = true, }; int rtd_pinctrl_probe(struct platform_device *pdev, const struct rtd_pinctrl_desc *desc) { struct rtd_pinctrl *data; int ret; data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM; data->base = of_iomap(pdev->dev.of_node, 0); if (!data->base) return -ENOMEM; data->dev = &pdev->dev; data->info = desc; data->desc.name = dev_name(&pdev->dev); data->desc.pins = data->info->pins; data->desc.npins = data->info->num_pins; data->desc.pctlops = &rtd_pinctrl_ops; data->desc.pmxops = &rtd_pinmux_ops; data->desc.confops = &rtd_pinconf_ops; data->desc.custom_params = rtd_custom_bindings; data->desc.num_custom_params = ARRAY_SIZE(rtd_custom_bindings); data->desc.owner = THIS_MODULE; data->regmap_pinctrl = devm_regmap_init_mmio(data->dev, data->base, &rtd_pinctrl_regmap_config); if (IS_ERR(data->regmap_pinctrl)) { dev_err(data->dev, "failed to init regmap: %ld\n", PTR_ERR(data->regmap_pinctrl)); ret = PTR_ERR(data->regmap_pinctrl); goto unmap; } data->pcdev = pinctrl_register(&data->desc, &pdev->dev, data); if (IS_ERR(data->pcdev)) { ret = PTR_ERR(data->pcdev); goto unmap; } platform_set_drvdata(pdev, data); dev_dbg(&pdev->dev, "probed\n"); return 0; unmap: iounmap(data->base); return ret; } EXPORT_SYMBOL(rtd_pinctrl_probe); MODULE_DESCRIPTION("Realtek DHC SoC pinctrl driver"); MODULE_LICENSE("GPL v2"); |