1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Framework for Ethernet Power Sourcing Equipment 4 // 5 // Copyright (c) 2022 Pengutronix, Oleksij Rempel <kernel@pengutronix.de> 6 // 7 8 #include <linux/device.h> 9 #include <linux/of.h> 10 #include <linux/pse-pd/pse.h> 11 #include <linux/regulator/driver.h> 12 #include <linux/regulator/machine.h> 13 14 static DEFINE_MUTEX(pse_list_mutex); 15 static LIST_HEAD(pse_controller_list); 16 17 /** 18 * struct pse_control - a PSE control 19 * @pcdev: a pointer to the PSE controller device 20 * this PSE control belongs to 21 * @ps: PSE PI supply of the PSE control 22 * @list: list entry for the pcdev's PSE controller list 23 * @id: ID of the PSE line in the PSE controller device 24 * @refcnt: Number of gets of this pse_control 25 */ 26 struct pse_control { 27 struct pse_controller_dev *pcdev; 28 struct regulator *ps; 29 struct list_head list; 30 unsigned int id; 31 struct kref refcnt; 32 }; 33 34 static int of_load_single_pse_pi_pairset(struct device_node *node, 35 struct pse_pi *pi, 36 int pairset_num) 37 { 38 struct device_node *pairset_np; 39 const char *name; 40 int ret; 41 42 ret = of_property_read_string_index(node, "pairset-names", 43 pairset_num, &name); 44 if (ret) 45 return ret; 46 47 if (!strcmp(name, "alternative-a")) { 48 pi->pairset[pairset_num].pinout = ALTERNATIVE_A; 49 } else if (!strcmp(name, "alternative-b")) { 50 pi->pairset[pairset_num].pinout = ALTERNATIVE_B; 51 } else { 52 pr_err("pse: wrong pairset-names value %s (%pOF)\n", 53 name, node); 54 return -EINVAL; 55 } 56 57 pairset_np = of_parse_phandle(node, "pairsets", pairset_num); 58 if (!pairset_np) 59 return -ENODEV; 60 61 pi->pairset[pairset_num].np = pairset_np; 62 63 return 0; 64 } 65 66 /** 67 * of_load_pse_pi_pairsets - load PSE PI pairsets pinout and polarity 68 * @node: a pointer of the device node 69 * @pi: a pointer of the PSE PI to fill 70 * @npairsets: the number of pairsets (1 or 2) used by the PI 71 * 72 * Return: 0 on success and failure value on error 73 */ 74 static int of_load_pse_pi_pairsets(struct device_node *node, 75 struct pse_pi *pi, 76 int npairsets) 77 { 78 int i, ret; 79 80 ret = of_property_count_strings(node, "pairset-names"); 81 if (ret != npairsets) { 82 pr_err("pse: amount of pairsets and pairset-names is not equal %d != %d (%pOF)\n", 83 npairsets, ret, node); 84 return -EINVAL; 85 } 86 87 for (i = 0; i < npairsets; i++) { 88 ret = of_load_single_pse_pi_pairset(node, pi, i); 89 if (ret) 90 goto out; 91 } 92 93 if (npairsets == 2 && 94 pi->pairset[0].pinout == pi->pairset[1].pinout) { 95 pr_err("pse: two PI pairsets can not have identical pinout (%pOF)", 96 node); 97 ret = -EINVAL; 98 } 99 100 out: 101 /* If an error appears, release all the pairset device node kref */ 102 if (ret) { 103 of_node_put(pi->pairset[0].np); 104 pi->pairset[0].np = NULL; 105 of_node_put(pi->pairset[1].np); 106 pi->pairset[1].np = NULL; 107 } 108 109 return ret; 110 } 111 112 static void pse_release_pis(struct pse_controller_dev *pcdev) 113 { 114 int i; 115 116 for (i = 0; i <= pcdev->nr_lines; i++) { 117 of_node_put(pcdev->pi[i].pairset[0].np); 118 of_node_put(pcdev->pi[i].pairset[1].np); 119 of_node_put(pcdev->pi[i].np); 120 } 121 kfree(pcdev->pi); 122 } 123 124 /** 125 * of_load_pse_pis - load all the PSE PIs 126 * @pcdev: a pointer to the PSE controller device 127 * 128 * Return: 0 on success and failure value on error 129 */ 130 static int of_load_pse_pis(struct pse_controller_dev *pcdev) 131 { 132 struct device_node *np = pcdev->dev->of_node; 133 struct device_node *node, *pis; 134 int ret; 135 136 if (!np) 137 return -ENODEV; 138 139 pcdev->pi = kcalloc(pcdev->nr_lines, sizeof(*pcdev->pi), GFP_KERNEL); 140 if (!pcdev->pi) 141 return -ENOMEM; 142 143 pis = of_get_child_by_name(np, "pse-pis"); 144 if (!pis) { 145 /* no description of PSE PIs */ 146 pcdev->no_of_pse_pi = true; 147 return 0; 148 } 149 150 for_each_child_of_node(pis, node) { 151 struct pse_pi pi = {0}; 152 u32 id; 153 154 if (!of_node_name_eq(node, "pse-pi")) 155 continue; 156 157 ret = of_property_read_u32(node, "reg", &id); 158 if (ret) { 159 dev_err(pcdev->dev, 160 "can't get reg property for node '%pOF'", 161 node); 162 goto out; 163 } 164 165 if (id >= pcdev->nr_lines) { 166 dev_err(pcdev->dev, 167 "reg value (%u) is out of range (%u) (%pOF)\n", 168 id, pcdev->nr_lines, node); 169 ret = -EINVAL; 170 goto out; 171 } 172 173 if (pcdev->pi[id].np) { 174 dev_err(pcdev->dev, 175 "other node with same reg value was already registered. %pOF : %pOF\n", 176 pcdev->pi[id].np, node); 177 ret = -EINVAL; 178 goto out; 179 } 180 181 ret = of_count_phandle_with_args(node, "pairsets", NULL); 182 /* npairsets is limited to value one or two */ 183 if (ret == 1 || ret == 2) { 184 ret = of_load_pse_pi_pairsets(node, &pi, ret); 185 if (ret) 186 goto out; 187 } else if (ret != ENOENT) { 188 dev_err(pcdev->dev, 189 "error: wrong number of pairsets. Should be 1 or 2, got %d (%pOF)\n", 190 ret, node); 191 ret = -EINVAL; 192 goto out; 193 } 194 195 of_node_get(node); 196 pi.np = node; 197 memcpy(&pcdev->pi[id], &pi, sizeof(pi)); 198 } 199 200 of_node_put(pis); 201 return 0; 202 203 out: 204 pse_release_pis(pcdev); 205 of_node_put(node); 206 of_node_put(pis); 207 return ret; 208 } 209 210 static int pse_pi_is_enabled(struct regulator_dev *rdev) 211 { 212 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 213 const struct pse_controller_ops *ops; 214 int id, ret; 215 216 ops = pcdev->ops; 217 if (!ops->pi_is_enabled) 218 return -EOPNOTSUPP; 219 220 id = rdev_get_id(rdev); 221 mutex_lock(&pcdev->lock); 222 ret = ops->pi_is_enabled(pcdev, id); 223 mutex_unlock(&pcdev->lock); 224 225 return ret; 226 } 227 228 static int pse_pi_enable(struct regulator_dev *rdev) 229 { 230 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 231 const struct pse_controller_ops *ops; 232 int id, ret; 233 234 ops = pcdev->ops; 235 if (!ops->pi_enable) 236 return -EOPNOTSUPP; 237 238 id = rdev_get_id(rdev); 239 mutex_lock(&pcdev->lock); 240 ret = ops->pi_enable(pcdev, id); 241 if (!ret) 242 pcdev->pi[id].admin_state_enabled = 1; 243 mutex_unlock(&pcdev->lock); 244 245 return ret; 246 } 247 248 static int pse_pi_disable(struct regulator_dev *rdev) 249 { 250 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 251 const struct pse_controller_ops *ops; 252 int id, ret; 253 254 ops = pcdev->ops; 255 if (!ops->pi_disable) 256 return -EOPNOTSUPP; 257 258 id = rdev_get_id(rdev); 259 mutex_lock(&pcdev->lock); 260 ret = ops->pi_disable(pcdev, id); 261 if (!ret) 262 pcdev->pi[id].admin_state_enabled = 0; 263 mutex_unlock(&pcdev->lock); 264 265 return ret; 266 } 267 268 static const struct regulator_ops pse_pi_ops = { 269 .is_enabled = pse_pi_is_enabled, 270 .enable = pse_pi_enable, 271 .disable = pse_pi_disable, 272 }; 273 274 static int 275 devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev, 276 char *name, int id) 277 { 278 struct regulator_init_data *rinit_data; 279 struct regulator_config rconfig = {0}; 280 struct regulator_desc *rdesc; 281 struct regulator_dev *rdev; 282 283 rinit_data = devm_kzalloc(pcdev->dev, sizeof(*rinit_data), 284 GFP_KERNEL); 285 if (!rinit_data) 286 return -ENOMEM; 287 288 rdesc = devm_kzalloc(pcdev->dev, sizeof(*rdesc), GFP_KERNEL); 289 if (!rdesc) 290 return -ENOMEM; 291 292 /* Regulator descriptor id have to be the same as its associated 293 * PSE PI id for the well functioning of the PSE controls. 294 */ 295 rdesc->id = id; 296 rdesc->name = name; 297 rdesc->type = REGULATOR_CURRENT; 298 rdesc->ops = &pse_pi_ops; 299 rdesc->owner = pcdev->owner; 300 301 rinit_data->constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS; 302 rinit_data->supply_regulator = "vpwr"; 303 304 rconfig.dev = pcdev->dev; 305 rconfig.driver_data = pcdev; 306 rconfig.init_data = rinit_data; 307 308 rdev = devm_regulator_register(pcdev->dev, rdesc, &rconfig); 309 if (IS_ERR(rdev)) { 310 dev_err_probe(pcdev->dev, PTR_ERR(rdev), 311 "Failed to register regulator\n"); 312 return PTR_ERR(rdev); 313 } 314 315 pcdev->pi[id].rdev = rdev; 316 317 return 0; 318 } 319 320 /** 321 * pse_controller_register - register a PSE controller device 322 * @pcdev: a pointer to the initialized PSE controller device 323 */ 324 int pse_controller_register(struct pse_controller_dev *pcdev) 325 { 326 size_t reg_name_len; 327 int ret, i; 328 329 mutex_init(&pcdev->lock); 330 INIT_LIST_HEAD(&pcdev->pse_control_head); 331 332 if (!pcdev->nr_lines) 333 pcdev->nr_lines = 1; 334 335 ret = of_load_pse_pis(pcdev); 336 if (ret) 337 return ret; 338 339 if (pcdev->ops->setup_pi_matrix) { 340 ret = pcdev->ops->setup_pi_matrix(pcdev); 341 if (ret) 342 return ret; 343 } 344 345 /* Each regulator name len is pcdev dev name + 7 char + 346 * int max digit number (10) + 1 347 */ 348 reg_name_len = strlen(dev_name(pcdev->dev)) + 18; 349 350 /* Register PI regulators */ 351 for (i = 0; i < pcdev->nr_lines; i++) { 352 char *reg_name; 353 354 /* Do not register regulator for PIs not described */ 355 if (!pcdev->no_of_pse_pi && !pcdev->pi[i].np) 356 continue; 357 358 reg_name = devm_kzalloc(pcdev->dev, reg_name_len, GFP_KERNEL); 359 if (!reg_name) 360 return -ENOMEM; 361 362 snprintf(reg_name, reg_name_len, "pse-%s_pi%d", 363 dev_name(pcdev->dev), i); 364 365 ret = devm_pse_pi_regulator_register(pcdev, reg_name, i); 366 if (ret) 367 return ret; 368 } 369 370 mutex_lock(&pse_list_mutex); 371 list_add(&pcdev->list, &pse_controller_list); 372 mutex_unlock(&pse_list_mutex); 373 374 return 0; 375 } 376 EXPORT_SYMBOL_GPL(pse_controller_register); 377 378 /** 379 * pse_controller_unregister - unregister a PSE controller device 380 * @pcdev: a pointer to the PSE controller device 381 */ 382 void pse_controller_unregister(struct pse_controller_dev *pcdev) 383 { 384 pse_release_pis(pcdev); 385 mutex_lock(&pse_list_mutex); 386 list_del(&pcdev->list); 387 mutex_unlock(&pse_list_mutex); 388 } 389 EXPORT_SYMBOL_GPL(pse_controller_unregister); 390 391 static void devm_pse_controller_release(struct device *dev, void *res) 392 { 393 pse_controller_unregister(*(struct pse_controller_dev **)res); 394 } 395 396 /** 397 * devm_pse_controller_register - resource managed pse_controller_register() 398 * @dev: device that is registering this PSE controller 399 * @pcdev: a pointer to the initialized PSE controller device 400 * 401 * Managed pse_controller_register(). For PSE controllers registered by 402 * this function, pse_controller_unregister() is automatically called on 403 * driver detach. See pse_controller_register() for more information. 404 */ 405 int devm_pse_controller_register(struct device *dev, 406 struct pse_controller_dev *pcdev) 407 { 408 struct pse_controller_dev **pcdevp; 409 int ret; 410 411 pcdevp = devres_alloc(devm_pse_controller_release, sizeof(*pcdevp), 412 GFP_KERNEL); 413 if (!pcdevp) 414 return -ENOMEM; 415 416 ret = pse_controller_register(pcdev); 417 if (ret) { 418 devres_free(pcdevp); 419 return ret; 420 } 421 422 *pcdevp = pcdev; 423 devres_add(dev, pcdevp); 424 425 return 0; 426 } 427 EXPORT_SYMBOL_GPL(devm_pse_controller_register); 428 429 /* PSE control section */ 430 431 static void __pse_control_release(struct kref *kref) 432 { 433 struct pse_control *psec = container_of(kref, struct pse_control, 434 refcnt); 435 436 lockdep_assert_held(&pse_list_mutex); 437 438 if (psec->pcdev->pi[psec->id].admin_state_enabled) 439 regulator_disable(psec->ps); 440 devm_regulator_put(psec->ps); 441 442 module_put(psec->pcdev->owner); 443 444 list_del(&psec->list); 445 kfree(psec); 446 } 447 448 static void __pse_control_put_internal(struct pse_control *psec) 449 { 450 lockdep_assert_held(&pse_list_mutex); 451 452 kref_put(&psec->refcnt, __pse_control_release); 453 } 454 455 /** 456 * pse_control_put - free the PSE control 457 * @psec: PSE control pointer 458 */ 459 void pse_control_put(struct pse_control *psec) 460 { 461 if (IS_ERR_OR_NULL(psec)) 462 return; 463 464 mutex_lock(&pse_list_mutex); 465 __pse_control_put_internal(psec); 466 mutex_unlock(&pse_list_mutex); 467 } 468 EXPORT_SYMBOL_GPL(pse_control_put); 469 470 static struct pse_control * 471 pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index) 472 { 473 struct pse_control *psec; 474 int ret; 475 476 lockdep_assert_held(&pse_list_mutex); 477 478 list_for_each_entry(psec, &pcdev->pse_control_head, list) { 479 if (psec->id == index) { 480 kref_get(&psec->refcnt); 481 return psec; 482 } 483 } 484 485 psec = kzalloc(sizeof(*psec), GFP_KERNEL); 486 if (!psec) 487 return ERR_PTR(-ENOMEM); 488 489 if (!try_module_get(pcdev->owner)) { 490 ret = -ENODEV; 491 goto free_psec; 492 } 493 494 psec->ps = devm_regulator_get_exclusive(pcdev->dev, 495 rdev_get_name(pcdev->pi[index].rdev)); 496 if (IS_ERR(psec->ps)) { 497 ret = PTR_ERR(psec->ps); 498 goto put_module; 499 } 500 501 ret = regulator_is_enabled(psec->ps); 502 if (ret < 0) 503 goto regulator_put; 504 505 pcdev->pi[index].admin_state_enabled = ret; 506 507 psec->pcdev = pcdev; 508 list_add(&psec->list, &pcdev->pse_control_head); 509 psec->id = index; 510 kref_init(&psec->refcnt); 511 512 return psec; 513 514 regulator_put: 515 devm_regulator_put(psec->ps); 516 put_module: 517 module_put(pcdev->owner); 518 free_psec: 519 kfree(psec); 520 521 return ERR_PTR(ret); 522 } 523 524 /** 525 * of_pse_match_pi - Find the PSE PI id matching the device node phandle 526 * @pcdev: a pointer to the PSE controller device 527 * @np: a pointer to the device node 528 * 529 * Return: id of the PSE PI, -EINVAL if not found 530 */ 531 static int of_pse_match_pi(struct pse_controller_dev *pcdev, 532 struct device_node *np) 533 { 534 int i; 535 536 for (i = 0; i <= pcdev->nr_lines; i++) { 537 if (pcdev->pi[i].np == np) 538 return i; 539 } 540 541 return -EINVAL; 542 } 543 544 /** 545 * psec_id_xlate - translate pse_spec to the PSE line number according 546 * to the number of pse-cells in case of no pse_pi node 547 * @pcdev: a pointer to the PSE controller device 548 * @pse_spec: PSE line specifier as found in the device tree 549 * 550 * Return: 0 if #pse-cells = <0>. Return PSE line number otherwise. 551 */ 552 static int psec_id_xlate(struct pse_controller_dev *pcdev, 553 const struct of_phandle_args *pse_spec) 554 { 555 if (!pcdev->of_pse_n_cells) 556 return 0; 557 558 if (pcdev->of_pse_n_cells > 1 || 559 pse_spec->args[0] >= pcdev->nr_lines) 560 return -EINVAL; 561 562 return pse_spec->args[0]; 563 } 564 565 struct pse_control *of_pse_control_get(struct device_node *node) 566 { 567 struct pse_controller_dev *r, *pcdev; 568 struct of_phandle_args args; 569 struct pse_control *psec; 570 int psec_id; 571 int ret; 572 573 if (!node) 574 return ERR_PTR(-EINVAL); 575 576 ret = of_parse_phandle_with_args(node, "pses", "#pse-cells", 0, &args); 577 if (ret) 578 return ERR_PTR(ret); 579 580 mutex_lock(&pse_list_mutex); 581 pcdev = NULL; 582 list_for_each_entry(r, &pse_controller_list, list) { 583 if (!r->no_of_pse_pi) { 584 ret = of_pse_match_pi(r, args.np); 585 if (ret >= 0) { 586 pcdev = r; 587 psec_id = ret; 588 break; 589 } 590 } else if (args.np == r->dev->of_node) { 591 pcdev = r; 592 break; 593 } 594 } 595 596 if (!pcdev) { 597 psec = ERR_PTR(-EPROBE_DEFER); 598 goto out; 599 } 600 601 if (WARN_ON(args.args_count != pcdev->of_pse_n_cells)) { 602 psec = ERR_PTR(-EINVAL); 603 goto out; 604 } 605 606 if (pcdev->no_of_pse_pi) { 607 psec_id = psec_id_xlate(pcdev, &args); 608 if (psec_id < 0) { 609 psec = ERR_PTR(psec_id); 610 goto out; 611 } 612 } 613 614 /* pse_list_mutex also protects the pcdev's pse_control list */ 615 psec = pse_control_get_internal(pcdev, psec_id); 616 617 out: 618 mutex_unlock(&pse_list_mutex); 619 of_node_put(args.np); 620 621 return psec; 622 } 623 EXPORT_SYMBOL_GPL(of_pse_control_get); 624 625 /** 626 * pse_ethtool_get_status - get status of PSE control 627 * @psec: PSE control pointer 628 * @extack: extack for reporting useful error messages 629 * @status: struct to store PSE status 630 */ 631 int pse_ethtool_get_status(struct pse_control *psec, 632 struct netlink_ext_ack *extack, 633 struct pse_control_status *status) 634 { 635 const struct pse_controller_ops *ops; 636 int err; 637 638 ops = psec->pcdev->ops; 639 640 if (!ops->ethtool_get_status) { 641 NL_SET_ERR_MSG(extack, 642 "PSE driver does not support status report"); 643 return -EOPNOTSUPP; 644 } 645 646 mutex_lock(&psec->pcdev->lock); 647 err = ops->ethtool_get_status(psec->pcdev, psec->id, extack, status); 648 mutex_unlock(&psec->pcdev->lock); 649 650 return err; 651 } 652 EXPORT_SYMBOL_GPL(pse_ethtool_get_status); 653 654 static int pse_ethtool_c33_set_config(struct pse_control *psec, 655 const struct pse_control_config *config) 656 { 657 int err = 0; 658 659 /* Look at admin_state_enabled status to not call regulator_enable 660 * or regulator_disable twice creating a regulator counter mismatch 661 */ 662 switch (config->c33_admin_control) { 663 case ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED: 664 if (!psec->pcdev->pi[psec->id].admin_state_enabled) 665 err = regulator_enable(psec->ps); 666 break; 667 case ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED: 668 if (psec->pcdev->pi[psec->id].admin_state_enabled) 669 err = regulator_disable(psec->ps); 670 break; 671 default: 672 err = -EOPNOTSUPP; 673 } 674 675 return err; 676 } 677 678 static int pse_ethtool_podl_set_config(struct pse_control *psec, 679 const struct pse_control_config *config) 680 { 681 int err = 0; 682 683 /* Look at admin_state_enabled status to not call regulator_enable 684 * or regulator_disable twice creating a regulator counter mismatch 685 */ 686 switch (config->podl_admin_control) { 687 case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED: 688 if (!psec->pcdev->pi[psec->id].admin_state_enabled) 689 err = regulator_enable(psec->ps); 690 break; 691 case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED: 692 if (psec->pcdev->pi[psec->id].admin_state_enabled) 693 err = regulator_disable(psec->ps); 694 break; 695 default: 696 err = -EOPNOTSUPP; 697 } 698 699 return err; 700 } 701 702 /** 703 * pse_ethtool_set_config - set PSE control configuration 704 * @psec: PSE control pointer 705 * @extack: extack for reporting useful error messages 706 * @config: Configuration of the test to run 707 */ 708 int pse_ethtool_set_config(struct pse_control *psec, 709 struct netlink_ext_ack *extack, 710 const struct pse_control_config *config) 711 { 712 int err = 0; 713 714 if (pse_has_c33(psec)) { 715 err = pse_ethtool_c33_set_config(psec, config); 716 if (err) 717 return err; 718 } 719 720 if (pse_has_podl(psec)) 721 err = pse_ethtool_podl_set_config(psec, config); 722 723 return err; 724 } 725 EXPORT_SYMBOL_GPL(pse_ethtool_set_config); 726 727 bool pse_has_podl(struct pse_control *psec) 728 { 729 return psec->pcdev->types & ETHTOOL_PSE_PODL; 730 } 731 EXPORT_SYMBOL_GPL(pse_has_podl); 732 733 bool pse_has_c33(struct pse_control *psec) 734 { 735 return psec->pcdev->types & ETHTOOL_PSE_C33; 736 } 737 EXPORT_SYMBOL_GPL(pse_has_c33); 738