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/ethtool.h> 10 #include <linux/ethtool_netlink.h> 11 #include <linux/of.h> 12 #include <linux/phy.h> 13 #include <linux/pse-pd/pse.h> 14 #include <linux/regulator/driver.h> 15 #include <linux/regulator/machine.h> 16 #include <linux/rtnetlink.h> 17 #include <net/net_trackers.h> 18 19 #define PSE_PW_D_LIMIT INT_MAX 20 21 static DEFINE_MUTEX(pse_list_mutex); 22 static LIST_HEAD(pse_controller_list); 23 static DEFINE_XARRAY_ALLOC(pse_pw_d_map); 24 static DEFINE_MUTEX(pse_pw_d_mutex); 25 26 /** 27 * struct pse_control - a PSE control 28 * @pcdev: a pointer to the PSE controller device 29 * this PSE control belongs to 30 * @ps: PSE PI supply of the PSE control 31 * @list: list entry for the pcdev's PSE controller list 32 * @id: ID of the PSE line in the PSE controller device 33 * @refcnt: Number of gets of this pse_control 34 * @attached_phydev: PHY device pointer attached by the PSE control 35 */ 36 struct pse_control { 37 struct pse_controller_dev *pcdev; 38 struct regulator *ps; 39 struct list_head list; 40 unsigned int id; 41 struct kref refcnt; 42 struct phy_device *attached_phydev; 43 }; 44 45 /** 46 * struct pse_power_domain - a PSE power domain 47 * @id: ID of the power domain 48 * @supply: Power supply the Power Domain 49 * @refcnt: Number of gets of this pse_power_domain 50 * @budget_eval_strategy: Current power budget evaluation strategy of the 51 * power domain 52 */ 53 struct pse_power_domain { 54 int id; 55 struct regulator *supply; 56 struct kref refcnt; 57 u32 budget_eval_strategy; 58 }; 59 60 static int of_load_single_pse_pi_pairset(struct device_node *node, 61 struct pse_pi *pi, 62 int pairset_num) 63 { 64 struct device_node *pairset_np; 65 const char *name; 66 int ret; 67 68 ret = of_property_read_string_index(node, "pairset-names", 69 pairset_num, &name); 70 if (ret) 71 return ret; 72 73 if (!strcmp(name, "alternative-a")) { 74 pi->pairset[pairset_num].pinout = ALTERNATIVE_A; 75 } else if (!strcmp(name, "alternative-b")) { 76 pi->pairset[pairset_num].pinout = ALTERNATIVE_B; 77 } else { 78 pr_err("pse: wrong pairset-names value %s (%pOF)\n", 79 name, node); 80 return -EINVAL; 81 } 82 83 pairset_np = of_parse_phandle(node, "pairsets", pairset_num); 84 if (!pairset_np) 85 return -ENODEV; 86 87 pi->pairset[pairset_num].np = pairset_np; 88 89 return 0; 90 } 91 92 /** 93 * of_load_pse_pi_pairsets - load PSE PI pairsets pinout and polarity 94 * @node: a pointer of the device node 95 * @pi: a pointer of the PSE PI to fill 96 * @npairsets: the number of pairsets (1 or 2) used by the PI 97 * 98 * Return: 0 on success and failure value on error 99 */ 100 static int of_load_pse_pi_pairsets(struct device_node *node, 101 struct pse_pi *pi, 102 int npairsets) 103 { 104 int i, ret; 105 106 ret = of_property_count_strings(node, "pairset-names"); 107 if (ret != npairsets) { 108 pr_err("pse: amount of pairsets and pairset-names is not equal %d != %d (%pOF)\n", 109 npairsets, ret, node); 110 return -EINVAL; 111 } 112 113 for (i = 0; i < npairsets; i++) { 114 ret = of_load_single_pse_pi_pairset(node, pi, i); 115 if (ret) 116 goto out; 117 } 118 119 if (npairsets == 2 && 120 pi->pairset[0].pinout == pi->pairset[1].pinout) { 121 pr_err("pse: two PI pairsets can not have identical pinout (%pOF)", 122 node); 123 ret = -EINVAL; 124 } 125 126 out: 127 /* If an error appears, release all the pairset device node kref */ 128 if (ret) { 129 of_node_put(pi->pairset[0].np); 130 pi->pairset[0].np = NULL; 131 of_node_put(pi->pairset[1].np); 132 pi->pairset[1].np = NULL; 133 } 134 135 return ret; 136 } 137 138 static void pse_release_pis(struct pse_controller_dev *pcdev) 139 { 140 int i; 141 142 for (i = 0; i < pcdev->nr_lines; i++) { 143 of_node_put(pcdev->pi[i].pairset[0].np); 144 of_node_put(pcdev->pi[i].pairset[1].np); 145 of_node_put(pcdev->pi[i].np); 146 } 147 kfree(pcdev->pi); 148 } 149 150 /** 151 * of_load_pse_pis - load all the PSE PIs 152 * @pcdev: a pointer to the PSE controller device 153 * 154 * Return: 0 on success and failure value on error 155 */ 156 static int of_load_pse_pis(struct pse_controller_dev *pcdev) 157 { 158 struct device_node *np = pcdev->dev->of_node; 159 struct device_node *node, *pis; 160 int ret; 161 162 if (!np) 163 return -ENODEV; 164 165 pcdev->pi = kzalloc_objs(*pcdev->pi, pcdev->nr_lines); 166 if (!pcdev->pi) 167 return -ENOMEM; 168 169 pis = of_get_child_by_name(np, "pse-pis"); 170 if (!pis) { 171 /* no description of PSE PIs */ 172 pcdev->no_of_pse_pi = true; 173 return 0; 174 } 175 176 for_each_child_of_node(pis, node) { 177 struct pse_pi pi = {0}; 178 u32 id; 179 180 if (!of_node_name_eq(node, "pse-pi")) 181 continue; 182 183 ret = of_property_read_u32(node, "reg", &id); 184 if (ret) { 185 dev_err(pcdev->dev, 186 "can't get reg property for node '%pOF'", 187 node); 188 goto out; 189 } 190 191 if (id >= pcdev->nr_lines) { 192 dev_err(pcdev->dev, 193 "reg value (%u) is out of range (%u) (%pOF)\n", 194 id, pcdev->nr_lines, node); 195 ret = -EINVAL; 196 goto out; 197 } 198 199 if (pcdev->pi[id].np) { 200 dev_err(pcdev->dev, 201 "other node with same reg value was already registered. %pOF : %pOF\n", 202 pcdev->pi[id].np, node); 203 ret = -EINVAL; 204 goto out; 205 } 206 207 ret = of_count_phandle_with_args(node, "pairsets", NULL); 208 /* npairsets is limited to value one or two */ 209 if (ret == 1 || ret == 2) { 210 ret = of_load_pse_pi_pairsets(node, &pi, ret); 211 if (ret) 212 goto out; 213 } else if (ret != -ENOENT) { 214 dev_err(pcdev->dev, 215 "error: wrong number of pairsets. Should be 1 or 2, got %d (%pOF)\n", 216 ret, node); 217 ret = -EINVAL; 218 goto out; 219 } 220 221 of_node_get(node); 222 pi.np = node; 223 memcpy(&pcdev->pi[id], &pi, sizeof(pi)); 224 } 225 226 of_node_put(pis); 227 return 0; 228 229 out: 230 pse_release_pis(pcdev); 231 of_node_put(node); 232 of_node_put(pis); 233 return ret; 234 } 235 236 /** 237 * pse_control_find_by_id - Find pse_control from an id 238 * @pcdev: a pointer to the PSE 239 * @id: index of the PSE control 240 * 241 * Return: pse_control pointer or NULL. The device returned has had a 242 * reference added and the pointer is safe until the user calls 243 * pse_control_put() to indicate they have finished with it. 244 */ 245 static struct pse_control * 246 pse_control_find_by_id(struct pse_controller_dev *pcdev, int id) 247 { 248 struct pse_control *psec; 249 250 mutex_lock(&pse_list_mutex); 251 list_for_each_entry(psec, &pcdev->pse_control_head, list) { 252 if (psec->id == id) { 253 kref_get(&psec->refcnt); 254 mutex_unlock(&pse_list_mutex); 255 return psec; 256 } 257 } 258 mutex_unlock(&pse_list_mutex); 259 return NULL; 260 } 261 262 /** 263 * pse_control_get_netdev - Return netdev associated to a PSE control 264 * @psec: PSE control pointer 265 * 266 * Return: netdev pointer or NULL 267 */ 268 static struct net_device *pse_control_get_netdev(struct pse_control *psec) 269 { 270 ASSERT_RTNL(); 271 272 if (!psec || !psec->attached_phydev) 273 return NULL; 274 275 return psec->attached_phydev->attached_dev; 276 } 277 278 /** 279 * pse_pi_is_hw_enabled - Is PI enabled at the hardware level 280 * @pcdev: a pointer to the PSE controller device 281 * @id: Index of the PI 282 * 283 * Return: 1 if the PI is enabled at the hardware level, 0 if not, and 284 * a failure value on error 285 */ 286 static int pse_pi_is_hw_enabled(struct pse_controller_dev *pcdev, int id) 287 { 288 struct pse_admin_state admin_state = {0}; 289 int ret; 290 291 ret = pcdev->ops->pi_get_admin_state(pcdev, id, &admin_state); 292 if (ret < 0) 293 return ret; 294 295 /* PI is well enabled at the hardware level */ 296 if (admin_state.podl_admin_state == ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED || 297 admin_state.c33_admin_state == ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED) 298 return 1; 299 300 return 0; 301 } 302 303 /** 304 * pse_pi_is_admin_enable_pending - Check if PI is in admin enable pending state 305 * which mean the power is not yet being 306 * delivered 307 * @pcdev: a pointer to the PSE controller device 308 * @id: Index of the PI 309 * 310 * Detects if a PI is enabled in software with a PD detected, but the hardware 311 * admin state hasn't been applied yet. 312 * 313 * This function is used in the power delivery and retry mechanisms to determine 314 * which PIs need to have power delivery attempted again. 315 * 316 * Return: true if the PI has admin enable flag set in software but not yet 317 * reflected in the hardware admin state, false otherwise. 318 */ 319 static bool 320 pse_pi_is_admin_enable_pending(struct pse_controller_dev *pcdev, int id) 321 { 322 int ret; 323 324 /* PI not enabled or nothing is plugged */ 325 if (!pcdev->pi[id].admin_state_enabled || 326 !pcdev->pi[id].isr_pd_detected) 327 return false; 328 329 ret = pse_pi_is_hw_enabled(pcdev, id); 330 /* PSE PI is already enabled at hardware level */ 331 if (ret == 1) 332 return false; 333 334 return true; 335 } 336 337 static int _pse_pi_delivery_power_sw_pw_ctrl(struct pse_controller_dev *pcdev, 338 int id, 339 struct netlink_ext_ack *extack); 340 341 /** 342 * pse_pw_d_retry_power_delivery - Retry power delivery for pending ports in a 343 * PSE power domain 344 * @pcdev: a pointer to the PSE controller device 345 * @pw_d: a pointer to the PSE power domain 346 * 347 * Scans all ports in the specified power domain and attempts to enable power 348 * delivery to any ports that have admin enable state set but don't yet have 349 * hardware power enabled. Used when there are changes in connection status, 350 * admin state, or priority that might allow previously unpowered ports to 351 * receive power, especially in over-budget conditions. 352 */ 353 static void pse_pw_d_retry_power_delivery(struct pse_controller_dev *pcdev, 354 struct pse_power_domain *pw_d) 355 { 356 int i, ret = 0; 357 358 for (i = 0; i < pcdev->nr_lines; i++) { 359 int prio_max = pcdev->nr_lines; 360 struct netlink_ext_ack extack; 361 362 if (pcdev->pi[i].pw_d != pw_d) 363 continue; 364 365 if (!pse_pi_is_admin_enable_pending(pcdev, i)) 366 continue; 367 368 /* Do not try to enable PI with a lower prio (higher value) 369 * than one which already can't be enabled. 370 */ 371 if (pcdev->pi[i].prio > prio_max) 372 continue; 373 374 ret = _pse_pi_delivery_power_sw_pw_ctrl(pcdev, i, &extack); 375 if (ret == -ERANGE) 376 prio_max = pcdev->pi[i].prio; 377 } 378 } 379 380 /** 381 * pse_pw_d_is_sw_pw_control - Determine if power control is software managed 382 * @pcdev: a pointer to the PSE controller device 383 * @pw_d: a pointer to the PSE power domain 384 * 385 * This function determines whether the power control for a specific power 386 * domain is managed by software in the interrupt handler rather than directly 387 * by hardware. 388 * 389 * Software power control is active in the following cases: 390 * - When the budget evaluation strategy is set to static 391 * - When the budget evaluation strategy is disabled but the PSE controller 392 * has an interrupt handler that can report if a Powered Device is connected 393 * 394 * Return: true if the power control of the power domain is managed by software, 395 * false otherwise 396 */ 397 static bool pse_pw_d_is_sw_pw_control(struct pse_controller_dev *pcdev, 398 struct pse_power_domain *pw_d) 399 { 400 if (!pw_d) 401 return false; 402 403 if (pw_d->budget_eval_strategy == PSE_BUDGET_EVAL_STRAT_STATIC) 404 return true; 405 if (pw_d->budget_eval_strategy == PSE_BUDGET_EVAL_STRAT_DISABLED && 406 pcdev->ops->pi_enable && pcdev->irq) 407 return true; 408 409 return false; 410 } 411 412 static int pse_pi_is_enabled(struct regulator_dev *rdev) 413 { 414 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 415 const struct pse_controller_ops *ops; 416 int id, ret; 417 418 ops = pcdev->ops; 419 if (!ops->pi_get_admin_state) 420 return -EOPNOTSUPP; 421 422 id = rdev_get_id(rdev); 423 mutex_lock(&pcdev->lock); 424 if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[id].pw_d)) { 425 ret = pcdev->pi[id].admin_state_enabled; 426 goto out; 427 } 428 429 ret = pse_pi_is_hw_enabled(pcdev, id); 430 431 out: 432 mutex_unlock(&pcdev->lock); 433 434 return ret; 435 } 436 437 /** 438 * pse_pi_deallocate_pw_budget - Deallocate power budget of the PI 439 * @pi: a pointer to the PSE PI 440 */ 441 static void pse_pi_deallocate_pw_budget(struct pse_pi *pi) 442 { 443 if (!pi->pw_d || !pi->pw_allocated_mW) 444 return; 445 446 regulator_free_power_budget(pi->pw_d->supply, pi->pw_allocated_mW); 447 pi->pw_allocated_mW = 0; 448 } 449 450 /** 451 * _pse_pi_disable - Call disable operation. Assumes the PSE lock has been 452 * acquired. 453 * @pcdev: a pointer to the PSE 454 * @id: index of the PSE control 455 * 456 * Return: 0 on success and failure value on error 457 */ 458 static int _pse_pi_disable(struct pse_controller_dev *pcdev, int id) 459 { 460 const struct pse_controller_ops *ops = pcdev->ops; 461 int ret; 462 463 if (!ops->pi_disable) 464 return -EOPNOTSUPP; 465 466 ret = ops->pi_disable(pcdev, id); 467 if (ret) 468 return ret; 469 470 pse_pi_deallocate_pw_budget(&pcdev->pi[id]); 471 472 if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[id].pw_d)) 473 pse_pw_d_retry_power_delivery(pcdev, pcdev->pi[id].pw_d); 474 475 return 0; 476 } 477 478 /** 479 * pse_disable_pi_pol - Disable a PI on a power budget policy 480 * @pcdev: a pointer to the PSE 481 * @id: index of the PSE PI 482 * 483 * Return: 0 on success and failure value on error 484 */ 485 static int pse_disable_pi_pol(struct pse_controller_dev *pcdev, int id) 486 { 487 unsigned long notifs = ETHTOOL_PSE_EVENT_OVER_BUDGET; 488 struct pse_ntf ntf = {}; 489 int ret; 490 491 dev_dbg(pcdev->dev, "Disabling PI %d to free power budget\n", id); 492 493 ret = _pse_pi_disable(pcdev, id); 494 if (ret) 495 notifs |= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR; 496 497 ntf.notifs = notifs; 498 ntf.id = id; 499 kfifo_in_spinlocked(&pcdev->ntf_fifo, &ntf, 1, &pcdev->ntf_fifo_lock); 500 schedule_work(&pcdev->ntf_work); 501 502 return ret; 503 } 504 505 /** 506 * pse_disable_pi_prio - Disable all PIs of a given priority inside a PSE 507 * power domain 508 * @pcdev: a pointer to the PSE 509 * @pw_d: a pointer to the PSE power domain 510 * @prio: priority 511 * 512 * Return: 0 on success and failure value on error 513 */ 514 static int pse_disable_pi_prio(struct pse_controller_dev *pcdev, 515 struct pse_power_domain *pw_d, 516 int prio) 517 { 518 int i; 519 520 for (i = 0; i < pcdev->nr_lines; i++) { 521 int ret; 522 523 if (pcdev->pi[i].prio != prio || 524 pcdev->pi[i].pw_d != pw_d || 525 pse_pi_is_hw_enabled(pcdev, i) <= 0) 526 continue; 527 528 ret = pse_disable_pi_pol(pcdev, i); 529 if (ret) 530 return ret; 531 } 532 533 return 0; 534 } 535 536 /** 537 * pse_pi_allocate_pw_budget_static_prio - Allocate power budget for the PI 538 * when the budget eval strategy is 539 * static 540 * @pcdev: a pointer to the PSE 541 * @id: index of the PSE control 542 * @pw_req: power requested in mW 543 * @extack: extack for error reporting 544 * 545 * Allocates power using static budget evaluation strategy, where allocation 546 * is based on PD classification. When insufficient budget is available, 547 * lower-priority ports (higher priority numbers) are turned off first. 548 * 549 * Return: 0 on success and failure value on error 550 */ 551 static int 552 pse_pi_allocate_pw_budget_static_prio(struct pse_controller_dev *pcdev, int id, 553 int pw_req, struct netlink_ext_ack *extack) 554 { 555 struct pse_pi *pi = &pcdev->pi[id]; 556 int ret, _prio; 557 558 _prio = pcdev->nr_lines; 559 while (regulator_request_power_budget(pi->pw_d->supply, pw_req) == -ERANGE) { 560 if (_prio <= pi->prio) { 561 NL_SET_ERR_MSG_FMT(extack, 562 "PI %d: not enough power budget available", 563 id); 564 return -ERANGE; 565 } 566 567 ret = pse_disable_pi_prio(pcdev, pi->pw_d, _prio); 568 if (ret < 0) 569 return ret; 570 571 _prio--; 572 } 573 574 pi->pw_allocated_mW = pw_req; 575 return 0; 576 } 577 578 /** 579 * pse_pi_allocate_pw_budget - Allocate power budget for the PI 580 * @pcdev: a pointer to the PSE 581 * @id: index of the PSE control 582 * @pw_req: power requested in mW 583 * @extack: extack for error reporting 584 * 585 * Return: 0 on success and failure value on error 586 */ 587 static int pse_pi_allocate_pw_budget(struct pse_controller_dev *pcdev, int id, 588 int pw_req, struct netlink_ext_ack *extack) 589 { 590 struct pse_pi *pi = &pcdev->pi[id]; 591 592 if (!pi->pw_d) 593 return 0; 594 595 /* PSE_BUDGET_EVAL_STRAT_STATIC */ 596 if (pi->pw_d->budget_eval_strategy == PSE_BUDGET_EVAL_STRAT_STATIC) 597 return pse_pi_allocate_pw_budget_static_prio(pcdev, id, pw_req, 598 extack); 599 600 return 0; 601 } 602 603 /** 604 * _pse_pi_delivery_power_sw_pw_ctrl - Enable PSE PI in case of software power 605 * control. Assumes the PSE lock has been 606 * acquired. 607 * @pcdev: a pointer to the PSE 608 * @id: index of the PSE control 609 * @extack: extack for error reporting 610 * 611 * Return: 0 on success and failure value on error 612 */ 613 static int _pse_pi_delivery_power_sw_pw_ctrl(struct pse_controller_dev *pcdev, 614 int id, 615 struct netlink_ext_ack *extack) 616 { 617 const struct pse_controller_ops *ops = pcdev->ops; 618 struct pse_pi *pi = &pcdev->pi[id]; 619 int ret, pw_req; 620 621 if (!ops->pi_get_pw_req) { 622 /* No power allocation management */ 623 ret = ops->pi_enable(pcdev, id); 624 if (ret) 625 NL_SET_ERR_MSG_FMT(extack, 626 "PI %d: enable error %d", 627 id, ret); 628 return ret; 629 } 630 631 ret = ops->pi_get_pw_req(pcdev, id); 632 if (ret < 0) 633 return ret; 634 635 pw_req = ret; 636 637 /* Compare requested power with port power limit and use the lowest 638 * one. 639 */ 640 if (ops->pi_get_pw_limit) { 641 ret = ops->pi_get_pw_limit(pcdev, id); 642 if (ret < 0) 643 return ret; 644 645 if (ret < pw_req) 646 pw_req = ret; 647 } 648 649 ret = pse_pi_allocate_pw_budget(pcdev, id, pw_req, extack); 650 if (ret) 651 return ret; 652 653 ret = ops->pi_enable(pcdev, id); 654 if (ret) { 655 pse_pi_deallocate_pw_budget(pi); 656 NL_SET_ERR_MSG_FMT(extack, 657 "PI %d: enable error %d", 658 id, ret); 659 return ret; 660 } 661 662 return 0; 663 } 664 665 static int pse_pi_enable(struct regulator_dev *rdev) 666 { 667 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 668 const struct pse_controller_ops *ops; 669 int id, ret = 0; 670 671 ops = pcdev->ops; 672 if (!ops->pi_enable) 673 return -EOPNOTSUPP; 674 675 id = rdev_get_id(rdev); 676 mutex_lock(&pcdev->lock); 677 if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[id].pw_d)) { 678 /* Manage enabled status by software. 679 * Real enable process will happen if a port is connected. 680 */ 681 if (pcdev->pi[id].isr_pd_detected) { 682 struct netlink_ext_ack extack; 683 684 ret = _pse_pi_delivery_power_sw_pw_ctrl(pcdev, id, &extack); 685 } 686 if (!ret || ret == -ERANGE) { 687 pcdev->pi[id].admin_state_enabled = 1; 688 ret = 0; 689 } 690 mutex_unlock(&pcdev->lock); 691 return ret; 692 } 693 694 ret = ops->pi_enable(pcdev, id); 695 if (!ret) 696 pcdev->pi[id].admin_state_enabled = 1; 697 mutex_unlock(&pcdev->lock); 698 699 return ret; 700 } 701 702 static int pse_pi_disable(struct regulator_dev *rdev) 703 { 704 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 705 struct pse_pi *pi; 706 int id, ret; 707 708 id = rdev_get_id(rdev); 709 pi = &pcdev->pi[id]; 710 mutex_lock(&pcdev->lock); 711 ret = _pse_pi_disable(pcdev, id); 712 if (!ret) 713 pi->admin_state_enabled = 0; 714 715 mutex_unlock(&pcdev->lock); 716 return 0; 717 } 718 719 static int _pse_pi_get_voltage(struct regulator_dev *rdev) 720 { 721 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 722 const struct pse_controller_ops *ops; 723 int id; 724 725 ops = pcdev->ops; 726 if (!ops->pi_get_voltage) 727 return -EOPNOTSUPP; 728 729 id = rdev_get_id(rdev); 730 return ops->pi_get_voltage(pcdev, id); 731 } 732 733 static int pse_pi_get_voltage(struct regulator_dev *rdev) 734 { 735 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 736 int ret; 737 738 mutex_lock(&pcdev->lock); 739 ret = _pse_pi_get_voltage(rdev); 740 mutex_unlock(&pcdev->lock); 741 742 return ret; 743 } 744 745 static int pse_pi_get_current_limit(struct regulator_dev *rdev) 746 { 747 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 748 const struct pse_controller_ops *ops; 749 int id, uV, mW, ret; 750 s64 tmp_64; 751 752 ops = pcdev->ops; 753 id = rdev_get_id(rdev); 754 if (!ops->pi_get_pw_limit || !ops->pi_get_voltage) 755 return -EOPNOTSUPP; 756 757 mutex_lock(&pcdev->lock); 758 ret = ops->pi_get_pw_limit(pcdev, id); 759 if (ret < 0) 760 goto out; 761 mW = ret; 762 763 ret = _pse_pi_get_voltage(rdev); 764 if (!ret) { 765 dev_err(pcdev->dev, "Voltage null\n"); 766 ret = -ERANGE; 767 goto out; 768 } 769 if (ret < 0) 770 goto out; 771 uV = ret; 772 773 tmp_64 = mW; 774 tmp_64 *= 1000000000ull; 775 /* uA = mW * 1000000000 / uV */ 776 ret = DIV_ROUND_CLOSEST_ULL(tmp_64, uV); 777 778 out: 779 mutex_unlock(&pcdev->lock); 780 return ret; 781 } 782 783 static int pse_pi_set_current_limit(struct regulator_dev *rdev, int min_uA, 784 int max_uA) 785 { 786 struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev); 787 const struct pse_controller_ops *ops; 788 int id, mW, ret; 789 s64 tmp_64; 790 791 ops = pcdev->ops; 792 if (!ops->pi_set_pw_limit || !ops->pi_get_voltage) 793 return -EOPNOTSUPP; 794 795 if (max_uA > MAX_PI_CURRENT) 796 return -ERANGE; 797 798 id = rdev_get_id(rdev); 799 mutex_lock(&pcdev->lock); 800 ret = _pse_pi_get_voltage(rdev); 801 if (!ret) { 802 dev_err(pcdev->dev, "Voltage null\n"); 803 ret = -ERANGE; 804 goto out; 805 } 806 if (ret < 0) 807 goto out; 808 809 tmp_64 = ret; 810 tmp_64 *= max_uA; 811 /* mW = uA * uV / 1000000000 */ 812 mW = DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000); 813 ret = ops->pi_set_pw_limit(pcdev, id, mW); 814 out: 815 mutex_unlock(&pcdev->lock); 816 817 return ret; 818 } 819 820 static const struct regulator_ops pse_pi_ops = { 821 .is_enabled = pse_pi_is_enabled, 822 .enable = pse_pi_enable, 823 .disable = pse_pi_disable, 824 .get_voltage = pse_pi_get_voltage, 825 .get_current_limit = pse_pi_get_current_limit, 826 .set_current_limit = pse_pi_set_current_limit, 827 }; 828 829 static int 830 devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev, 831 char *name, int id) 832 { 833 struct regulator_init_data *rinit_data; 834 struct regulator_config rconfig = {0}; 835 struct regulator_desc *rdesc; 836 struct regulator_dev *rdev; 837 838 rinit_data = devm_kzalloc_obj(pcdev->dev, *rinit_data); 839 if (!rinit_data) 840 return -ENOMEM; 841 842 rdesc = devm_kzalloc_obj(pcdev->dev, *rdesc); 843 if (!rdesc) 844 return -ENOMEM; 845 846 /* Regulator descriptor id have to be the same as its associated 847 * PSE PI id for the well functioning of the PSE controls. 848 */ 849 rdesc->id = id; 850 rdesc->name = name; 851 rdesc->type = REGULATOR_VOLTAGE; 852 rdesc->ops = &pse_pi_ops; 853 rdesc->owner = pcdev->owner; 854 855 rinit_data->constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS; 856 857 if (pcdev->ops->pi_set_pw_limit) 858 rinit_data->constraints.valid_ops_mask |= 859 REGULATOR_CHANGE_CURRENT; 860 861 rinit_data->supply_regulator = "vpwr"; 862 863 rconfig.dev = pcdev->dev; 864 rconfig.driver_data = pcdev; 865 rconfig.init_data = rinit_data; 866 rconfig.of_node = pcdev->pi[id].np; 867 868 rdev = devm_regulator_register(pcdev->dev, rdesc, &rconfig); 869 if (IS_ERR(rdev)) { 870 dev_err_probe(pcdev->dev, PTR_ERR(rdev), 871 "Failed to register regulator\n"); 872 return PTR_ERR(rdev); 873 } 874 875 pcdev->pi[id].rdev = rdev; 876 877 return 0; 878 } 879 880 static void __pse_pw_d_release(struct kref *kref) 881 { 882 struct pse_power_domain *pw_d = container_of(kref, 883 struct pse_power_domain, 884 refcnt); 885 886 regulator_put(pw_d->supply); 887 xa_erase(&pse_pw_d_map, pw_d->id); 888 mutex_unlock(&pse_pw_d_mutex); 889 } 890 891 /** 892 * pse_flush_pw_ds - flush all PSE power domains of a PSE 893 * @pcdev: a pointer to the initialized PSE controller device 894 */ 895 static void pse_flush_pw_ds(struct pse_controller_dev *pcdev) 896 { 897 struct pse_power_domain *pw_d; 898 int i; 899 900 for (i = 0; i < pcdev->nr_lines; i++) { 901 if (!pcdev->pi[i].pw_d) 902 continue; 903 904 pw_d = xa_load(&pse_pw_d_map, pcdev->pi[i].pw_d->id); 905 if (!pw_d) 906 continue; 907 908 kref_put_mutex(&pw_d->refcnt, __pse_pw_d_release, 909 &pse_pw_d_mutex); 910 } 911 } 912 913 /** 914 * devm_pse_alloc_pw_d - allocate a new PSE power domain for a device 915 * @dev: device that is registering this PSE power domain 916 * 917 * Return: Pointer to the newly allocated PSE power domain or error pointers 918 */ 919 static struct pse_power_domain *devm_pse_alloc_pw_d(struct device *dev) 920 { 921 struct pse_power_domain *pw_d; 922 int index, ret; 923 924 pw_d = devm_kzalloc_obj(dev, *pw_d); 925 if (!pw_d) 926 return ERR_PTR(-ENOMEM); 927 928 ret = xa_alloc(&pse_pw_d_map, &index, pw_d, XA_LIMIT(1, PSE_PW_D_LIMIT), 929 GFP_KERNEL); 930 if (ret) 931 return ERR_PTR(ret); 932 933 kref_init(&pw_d->refcnt); 934 pw_d->id = index; 935 return pw_d; 936 } 937 938 /** 939 * pse_register_pw_ds - register the PSE power domains for a PSE 940 * @pcdev: a pointer to the PSE controller device 941 * 942 * Return: 0 on success and failure value on error 943 */ 944 static int pse_register_pw_ds(struct pse_controller_dev *pcdev) 945 { 946 int i, ret = 0; 947 948 mutex_lock(&pse_pw_d_mutex); 949 for (i = 0; i < pcdev->nr_lines; i++) { 950 struct regulator_dev *rdev = pcdev->pi[i].rdev; 951 struct pse_power_domain *pw_d; 952 struct regulator *supply; 953 bool present = false; 954 unsigned long index; 955 956 /* No regulator or regulator parent supply registered. 957 * We need a regulator parent to register a PSE power domain 958 */ 959 if (!rdev || !rdev->supply) 960 continue; 961 962 xa_for_each(&pse_pw_d_map, index, pw_d) { 963 /* Power supply already registered as a PSE power 964 * domain. 965 */ 966 if (regulator_is_equal(pw_d->supply, rdev->supply)) { 967 present = true; 968 pcdev->pi[i].pw_d = pw_d; 969 break; 970 } 971 } 972 if (present) { 973 kref_get(&pw_d->refcnt); 974 continue; 975 } 976 977 pw_d = devm_pse_alloc_pw_d(pcdev->dev); 978 if (IS_ERR(pw_d)) { 979 ret = PTR_ERR(pw_d); 980 goto out; 981 } 982 983 supply = regulator_get(&rdev->dev, rdev->supply_name); 984 if (IS_ERR(supply)) { 985 xa_erase(&pse_pw_d_map, pw_d->id); 986 ret = PTR_ERR(supply); 987 goto out; 988 } 989 990 pw_d->supply = supply; 991 if (pcdev->supp_budget_eval_strategies) 992 pw_d->budget_eval_strategy = pcdev->supp_budget_eval_strategies; 993 else 994 pw_d->budget_eval_strategy = PSE_BUDGET_EVAL_STRAT_DISABLED; 995 kref_init(&pw_d->refcnt); 996 pcdev->pi[i].pw_d = pw_d; 997 } 998 999 out: 1000 mutex_unlock(&pse_pw_d_mutex); 1001 return ret; 1002 } 1003 1004 /** 1005 * pse_send_ntf_worker - Worker to send PSE notifications 1006 * @work: work object 1007 * 1008 * Manage and send PSE netlink notifications using a workqueue to avoid 1009 * deadlock between pcdev_lock and pse_list_mutex. 1010 */ 1011 static void pse_send_ntf_worker(struct work_struct *work) 1012 { 1013 struct pse_controller_dev *pcdev; 1014 struct pse_ntf ntf; 1015 1016 pcdev = container_of(work, struct pse_controller_dev, ntf_work); 1017 1018 while (kfifo_out(&pcdev->ntf_fifo, &ntf, 1)) { 1019 struct net_device *netdev; 1020 struct pse_control *psec; 1021 1022 psec = pse_control_find_by_id(pcdev, ntf.id); 1023 rtnl_lock(); 1024 netdev = pse_control_get_netdev(psec); 1025 if (netdev) 1026 ethnl_pse_send_ntf(netdev, ntf.notifs); 1027 rtnl_unlock(); 1028 pse_control_put(psec); 1029 } 1030 } 1031 1032 /** 1033 * pse_controller_register - register a PSE controller device 1034 * @pcdev: a pointer to the initialized PSE controller device 1035 * 1036 * Return: 0 on success and failure value on error 1037 */ 1038 int pse_controller_register(struct pse_controller_dev *pcdev) 1039 { 1040 size_t reg_name_len; 1041 int ret, i; 1042 1043 mutex_init(&pcdev->lock); 1044 INIT_LIST_HEAD(&pcdev->pse_control_head); 1045 spin_lock_init(&pcdev->ntf_fifo_lock); 1046 ret = kfifo_alloc(&pcdev->ntf_fifo, pcdev->nr_lines, GFP_KERNEL); 1047 if (ret) { 1048 dev_err(pcdev->dev, "failed to allocate kfifo notifications\n"); 1049 return ret; 1050 } 1051 INIT_WORK(&pcdev->ntf_work, pse_send_ntf_worker); 1052 1053 if (!pcdev->nr_lines) 1054 pcdev->nr_lines = 1; 1055 1056 if (!pcdev->ops->pi_get_admin_state || 1057 !pcdev->ops->pi_get_pw_status) { 1058 dev_err(pcdev->dev, 1059 "Mandatory status report callbacks are missing"); 1060 return -EINVAL; 1061 } 1062 1063 ret = of_load_pse_pis(pcdev); 1064 if (ret) 1065 return ret; 1066 1067 if (pcdev->ops->setup_pi_matrix) { 1068 ret = pcdev->ops->setup_pi_matrix(pcdev); 1069 if (ret) 1070 return ret; 1071 } 1072 1073 /* Each regulator name len is pcdev dev name + 7 char + 1074 * int max digit number (10) + 1 1075 */ 1076 reg_name_len = strlen(dev_name(pcdev->dev)) + 18; 1077 1078 /* Register PI regulators */ 1079 for (i = 0; i < pcdev->nr_lines; i++) { 1080 char *reg_name; 1081 1082 /* Do not register regulator for PIs not described */ 1083 if (!pcdev->no_of_pse_pi && !pcdev->pi[i].np) 1084 continue; 1085 1086 reg_name = devm_kzalloc(pcdev->dev, reg_name_len, GFP_KERNEL); 1087 if (!reg_name) 1088 return -ENOMEM; 1089 1090 snprintf(reg_name, reg_name_len, "pse-%s_pi%d", 1091 dev_name(pcdev->dev), i); 1092 1093 ret = devm_pse_pi_regulator_register(pcdev, reg_name, i); 1094 if (ret) 1095 return ret; 1096 } 1097 1098 ret = pse_register_pw_ds(pcdev); 1099 if (ret) 1100 return ret; 1101 1102 mutex_lock(&pse_list_mutex); 1103 list_add(&pcdev->list, &pse_controller_list); 1104 mutex_unlock(&pse_list_mutex); 1105 1106 return 0; 1107 } 1108 EXPORT_SYMBOL_GPL(pse_controller_register); 1109 1110 /** 1111 * pse_controller_unregister - unregister a PSE controller device 1112 * @pcdev: a pointer to the PSE controller device 1113 */ 1114 void pse_controller_unregister(struct pse_controller_dev *pcdev) 1115 { 1116 pse_flush_pw_ds(pcdev); 1117 pse_release_pis(pcdev); 1118 if (pcdev->irq) 1119 disable_irq(pcdev->irq); 1120 cancel_work_sync(&pcdev->ntf_work); 1121 kfifo_free(&pcdev->ntf_fifo); 1122 mutex_lock(&pse_list_mutex); 1123 list_del(&pcdev->list); 1124 mutex_unlock(&pse_list_mutex); 1125 } 1126 EXPORT_SYMBOL_GPL(pse_controller_unregister); 1127 1128 static void devm_pse_controller_release(struct device *dev, void *res) 1129 { 1130 pse_controller_unregister(*(struct pse_controller_dev **)res); 1131 } 1132 1133 /** 1134 * devm_pse_controller_register - resource managed pse_controller_register() 1135 * @dev: device that is registering this PSE controller 1136 * @pcdev: a pointer to the initialized PSE controller device 1137 * 1138 * Managed pse_controller_register(). For PSE controllers registered by 1139 * this function, pse_controller_unregister() is automatically called on 1140 * driver detach. See pse_controller_register() for more information. 1141 * 1142 * Return: 0 on success and failure value on error 1143 */ 1144 int devm_pse_controller_register(struct device *dev, 1145 struct pse_controller_dev *pcdev) 1146 { 1147 struct pse_controller_dev **pcdevp; 1148 int ret; 1149 1150 pcdevp = devres_alloc(devm_pse_controller_release, sizeof(*pcdevp), 1151 GFP_KERNEL); 1152 if (!pcdevp) 1153 return -ENOMEM; 1154 1155 ret = pse_controller_register(pcdev); 1156 if (ret) { 1157 devres_free(pcdevp); 1158 return ret; 1159 } 1160 1161 *pcdevp = pcdev; 1162 devres_add(dev, pcdevp); 1163 1164 return 0; 1165 } 1166 EXPORT_SYMBOL_GPL(devm_pse_controller_register); 1167 1168 struct pse_irq { 1169 struct pse_controller_dev *pcdev; 1170 struct pse_irq_desc desc; 1171 unsigned long *notifs; 1172 unsigned long *notifs_mask; 1173 }; 1174 1175 /** 1176 * pse_to_regulator_notifs - Convert PSE notifications to Regulator 1177 * notifications 1178 * @notifs: PSE notifications 1179 * 1180 * Return: Regulator notifications 1181 */ 1182 static unsigned long pse_to_regulator_notifs(unsigned long notifs) 1183 { 1184 unsigned long rnotifs = 0; 1185 1186 if (notifs & ETHTOOL_PSE_EVENT_OVER_CURRENT) 1187 rnotifs |= REGULATOR_EVENT_OVER_CURRENT; 1188 if (notifs & ETHTOOL_PSE_EVENT_OVER_TEMP) 1189 rnotifs |= REGULATOR_EVENT_OVER_TEMP; 1190 1191 return rnotifs; 1192 } 1193 1194 /** 1195 * pse_set_config_isr - Set PSE control config according to the PSE 1196 * notifications 1197 * @pcdev: a pointer to the PSE 1198 * @id: index of the PSE control 1199 * @notifs: PSE event notifications 1200 * 1201 * Return: 0 on success and failure value on error 1202 */ 1203 static int pse_set_config_isr(struct pse_controller_dev *pcdev, int id, 1204 unsigned long notifs) 1205 { 1206 int ret = 0; 1207 1208 if (notifs & PSE_BUDGET_EVAL_STRAT_DYNAMIC) 1209 return 0; 1210 1211 if ((notifs & ETHTOOL_C33_PSE_EVENT_DISCONNECTION) && 1212 ((notifs & ETHTOOL_C33_PSE_EVENT_DETECTION) || 1213 (notifs & ETHTOOL_C33_PSE_EVENT_CLASSIFICATION))) { 1214 dev_dbg(pcdev->dev, 1215 "PI %d: error, connection and disconnection reported simultaneously", 1216 id); 1217 return -EINVAL; 1218 } 1219 1220 if (notifs & ETHTOOL_C33_PSE_EVENT_CLASSIFICATION) { 1221 struct netlink_ext_ack extack; 1222 1223 pcdev->pi[id].isr_pd_detected = true; 1224 if (pcdev->pi[id].admin_state_enabled) { 1225 ret = _pse_pi_delivery_power_sw_pw_ctrl(pcdev, id, 1226 &extack); 1227 if (ret == -ERANGE) 1228 ret = 0; 1229 } 1230 } else if (notifs & ETHTOOL_C33_PSE_EVENT_DISCONNECTION) { 1231 if (pcdev->pi[id].admin_state_enabled && 1232 pcdev->pi[id].isr_pd_detected) 1233 ret = _pse_pi_disable(pcdev, id); 1234 pcdev->pi[id].isr_pd_detected = false; 1235 } 1236 1237 return ret; 1238 } 1239 1240 /** 1241 * pse_isr - IRQ handler for PSE 1242 * @irq: irq number 1243 * @data: pointer to user interrupt structure 1244 * 1245 * Return: irqreturn_t - status of IRQ 1246 */ 1247 static irqreturn_t pse_isr(int irq, void *data) 1248 { 1249 struct pse_controller_dev *pcdev; 1250 struct pse_irq_desc *desc; 1251 struct pse_irq *h = data; 1252 int ret, i; 1253 1254 desc = &h->desc; 1255 pcdev = h->pcdev; 1256 1257 /* Clear notifs mask */ 1258 memset(h->notifs, 0, pcdev->nr_lines * sizeof(*h->notifs)); 1259 bitmap_zero(h->notifs_mask, pcdev->nr_lines); 1260 mutex_lock(&pcdev->lock); 1261 ret = desc->map_event(irq, pcdev, h->notifs, h->notifs_mask); 1262 if (ret || bitmap_empty(h->notifs_mask, pcdev->nr_lines)) { 1263 mutex_unlock(&pcdev->lock); 1264 return IRQ_NONE; 1265 } 1266 1267 for_each_set_bit(i, h->notifs_mask, pcdev->nr_lines) { 1268 unsigned long notifs, rnotifs; 1269 struct pse_ntf ntf = {}; 1270 1271 /* Do nothing PI not described */ 1272 if (!pcdev->pi[i].rdev) 1273 continue; 1274 1275 notifs = h->notifs[i]; 1276 if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[i].pw_d)) { 1277 ret = pse_set_config_isr(pcdev, i, notifs); 1278 if (ret) 1279 notifs |= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR; 1280 } 1281 1282 dev_dbg(h->pcdev->dev, 1283 "Sending PSE notification EVT 0x%lx\n", notifs); 1284 1285 ntf.notifs = notifs; 1286 ntf.id = i; 1287 kfifo_in_spinlocked(&pcdev->ntf_fifo, &ntf, 1, 1288 &pcdev->ntf_fifo_lock); 1289 schedule_work(&pcdev->ntf_work); 1290 1291 rnotifs = pse_to_regulator_notifs(notifs); 1292 regulator_notifier_call_chain(pcdev->pi[i].rdev, rnotifs, 1293 NULL); 1294 } 1295 1296 mutex_unlock(&pcdev->lock); 1297 1298 return IRQ_HANDLED; 1299 } 1300 1301 /** 1302 * devm_pse_irq_helper - Register IRQ based PSE event notifier 1303 * @pcdev: a pointer to the PSE 1304 * @irq: the irq value to be passed to request_irq 1305 * @irq_flags: the flags to be passed to request_irq 1306 * @d: PSE interrupt description 1307 * 1308 * Return: 0 on success and errno on failure 1309 */ 1310 int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq, 1311 int irq_flags, const struct pse_irq_desc *d) 1312 { 1313 struct device *dev = pcdev->dev; 1314 size_t irq_name_len; 1315 struct pse_irq *h; 1316 char *irq_name; 1317 int ret; 1318 1319 if (!d || !d->map_event || !d->name) 1320 return -EINVAL; 1321 1322 h = devm_kzalloc_obj(dev, *h); 1323 if (!h) 1324 return -ENOMEM; 1325 1326 h->pcdev = pcdev; 1327 h->desc = *d; 1328 1329 /* IRQ name len is pcdev dev name + 5 char + irq desc name + 1 */ 1330 irq_name_len = strlen(dev_name(pcdev->dev)) + 5 + strlen(d->name) + 1; 1331 irq_name = devm_kzalloc(dev, irq_name_len, GFP_KERNEL); 1332 if (!irq_name) 1333 return -ENOMEM; 1334 1335 snprintf(irq_name, irq_name_len, "pse-%s:%s", dev_name(pcdev->dev), 1336 d->name); 1337 1338 h->notifs = devm_kzalloc_objs(dev, *h->notifs, pcdev->nr_lines); 1339 if (!h->notifs) 1340 return -ENOMEM; 1341 1342 h->notifs_mask = devm_bitmap_zalloc(dev, pcdev->nr_lines, GFP_KERNEL); 1343 if (!h->notifs_mask) 1344 return -ENOMEM; 1345 1346 ret = devm_request_threaded_irq(dev, irq, NULL, pse_isr, 1347 IRQF_ONESHOT | irq_flags, 1348 irq_name, h); 1349 if (ret) 1350 dev_err(pcdev->dev, "Failed to request IRQ %d\n", irq); 1351 1352 pcdev->irq = irq; 1353 return ret; 1354 } 1355 EXPORT_SYMBOL_GPL(devm_pse_irq_helper); 1356 1357 /* PSE control section */ 1358 1359 static void __pse_control_release(struct kref *kref) 1360 { 1361 struct pse_control *psec = container_of(kref, struct pse_control, 1362 refcnt); 1363 1364 lockdep_assert_held(&pse_list_mutex); 1365 1366 if (psec->pcdev->pi[psec->id].admin_state_enabled) 1367 regulator_disable(psec->ps); 1368 regulator_put(psec->ps); 1369 1370 module_put(psec->pcdev->owner); 1371 1372 list_del(&psec->list); 1373 kfree(psec); 1374 } 1375 1376 static void __pse_control_put_internal(struct pse_control *psec) 1377 { 1378 lockdep_assert_held(&pse_list_mutex); 1379 1380 kref_put(&psec->refcnt, __pse_control_release); 1381 } 1382 1383 /** 1384 * pse_control_put - free the PSE control 1385 * @psec: PSE control pointer 1386 */ 1387 void pse_control_put(struct pse_control *psec) 1388 { 1389 if (IS_ERR_OR_NULL(psec)) 1390 return; 1391 1392 mutex_lock(&pse_list_mutex); 1393 __pse_control_put_internal(psec); 1394 mutex_unlock(&pse_list_mutex); 1395 } 1396 EXPORT_SYMBOL_GPL(pse_control_put); 1397 1398 static struct pse_control * 1399 pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index, 1400 struct phy_device *phydev) 1401 { 1402 struct pse_control *psec; 1403 int ret; 1404 1405 lockdep_assert_held(&pse_list_mutex); 1406 1407 list_for_each_entry(psec, &pcdev->pse_control_head, list) { 1408 if (psec->id == index) { 1409 kref_get(&psec->refcnt); 1410 return psec; 1411 } 1412 } 1413 1414 psec = kzalloc_obj(*psec); 1415 if (!psec) 1416 return ERR_PTR(-ENOMEM); 1417 1418 if (!try_module_get(pcdev->owner)) { 1419 ret = -ENODEV; 1420 goto free_psec; 1421 } 1422 1423 if (!pcdev->ops->pi_get_admin_state) { 1424 ret = -EOPNOTSUPP; 1425 goto free_psec; 1426 } 1427 1428 /* Initialize admin_state_enabled before the regulator_get. This 1429 * aims to have the right value reported in the first is_enabled 1430 * call in case of control managed by software. 1431 */ 1432 ret = pse_pi_is_hw_enabled(pcdev, index); 1433 if (ret < 0) 1434 goto free_psec; 1435 1436 pcdev->pi[index].admin_state_enabled = ret; 1437 psec->ps = regulator_get_exclusive(pcdev->dev, 1438 rdev_get_name(pcdev->pi[index].rdev)); 1439 if (IS_ERR(psec->ps)) { 1440 ret = PTR_ERR(psec->ps); 1441 goto put_module; 1442 } 1443 1444 psec->pcdev = pcdev; 1445 list_add(&psec->list, &pcdev->pse_control_head); 1446 psec->id = index; 1447 psec->attached_phydev = phydev; 1448 kref_init(&psec->refcnt); 1449 1450 return psec; 1451 1452 put_module: 1453 module_put(pcdev->owner); 1454 free_psec: 1455 kfree(psec); 1456 1457 return ERR_PTR(ret); 1458 } 1459 1460 /** 1461 * of_pse_match_pi - Find the PSE PI id matching the device node phandle 1462 * @pcdev: a pointer to the PSE controller device 1463 * @np: a pointer to the device node 1464 * 1465 * Return: id of the PSE PI, -EINVAL if not found 1466 */ 1467 static int of_pse_match_pi(struct pse_controller_dev *pcdev, 1468 struct device_node *np) 1469 { 1470 int i; 1471 1472 for (i = 0; i < pcdev->nr_lines; i++) { 1473 if (pcdev->pi[i].np == np) 1474 return i; 1475 } 1476 1477 return -EINVAL; 1478 } 1479 1480 /** 1481 * psec_id_xlate - translate pse_spec to the PSE line number according 1482 * to the number of pse-cells in case of no pse_pi node 1483 * @pcdev: a pointer to the PSE controller device 1484 * @pse_spec: PSE line specifier as found in the device tree 1485 * 1486 * Return: 0 if #pse-cells = <0>. Return PSE line number otherwise. 1487 */ 1488 static int psec_id_xlate(struct pse_controller_dev *pcdev, 1489 const struct of_phandle_args *pse_spec) 1490 { 1491 if (!pcdev->of_pse_n_cells) 1492 return 0; 1493 1494 if (pcdev->of_pse_n_cells > 1 || 1495 pse_spec->args[0] >= pcdev->nr_lines) 1496 return -EINVAL; 1497 1498 return pse_spec->args[0]; 1499 } 1500 1501 struct pse_control *of_pse_control_get(struct device_node *node, 1502 struct phy_device *phydev) 1503 { 1504 struct pse_controller_dev *r, *pcdev; 1505 struct of_phandle_args args; 1506 struct pse_control *psec; 1507 int psec_id; 1508 int ret; 1509 1510 if (!node) 1511 return ERR_PTR(-EINVAL); 1512 1513 ret = of_parse_phandle_with_args(node, "pses", "#pse-cells", 0, &args); 1514 if (ret) 1515 return ERR_PTR(ret); 1516 1517 mutex_lock(&pse_list_mutex); 1518 pcdev = NULL; 1519 list_for_each_entry(r, &pse_controller_list, list) { 1520 if (!r->no_of_pse_pi) { 1521 ret = of_pse_match_pi(r, args.np); 1522 if (ret >= 0) { 1523 pcdev = r; 1524 psec_id = ret; 1525 break; 1526 } 1527 } else if (args.np == r->dev->of_node) { 1528 pcdev = r; 1529 break; 1530 } 1531 } 1532 1533 if (!pcdev) { 1534 psec = ERR_PTR(-EPROBE_DEFER); 1535 goto out; 1536 } 1537 1538 if (WARN_ON(args.args_count != pcdev->of_pse_n_cells)) { 1539 psec = ERR_PTR(-EINVAL); 1540 goto out; 1541 } 1542 1543 if (pcdev->no_of_pse_pi) { 1544 psec_id = psec_id_xlate(pcdev, &args); 1545 if (psec_id < 0) { 1546 psec = ERR_PTR(psec_id); 1547 goto out; 1548 } 1549 } 1550 1551 /* pse_list_mutex also protects the pcdev's pse_control list */ 1552 psec = pse_control_get_internal(pcdev, psec_id, phydev); 1553 1554 out: 1555 mutex_unlock(&pse_list_mutex); 1556 of_node_put(args.np); 1557 1558 return psec; 1559 } 1560 EXPORT_SYMBOL_GPL(of_pse_control_get); 1561 1562 /** 1563 * pse_get_sw_admin_state - Convert the software admin state to c33 or podl 1564 * admin state value used in the standard 1565 * @psec: PSE control pointer 1566 * @admin_state: a pointer to the admin_state structure 1567 */ 1568 static void pse_get_sw_admin_state(struct pse_control *psec, 1569 struct pse_admin_state *admin_state) 1570 { 1571 struct pse_pi *pi = &psec->pcdev->pi[psec->id]; 1572 1573 if (pse_has_podl(psec)) { 1574 if (pi->admin_state_enabled) 1575 admin_state->podl_admin_state = 1576 ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED; 1577 else 1578 admin_state->podl_admin_state = 1579 ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED; 1580 } 1581 if (pse_has_c33(psec)) { 1582 if (pi->admin_state_enabled) 1583 admin_state->c33_admin_state = 1584 ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED; 1585 else 1586 admin_state->c33_admin_state = 1587 ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED; 1588 } 1589 } 1590 1591 /** 1592 * pse_ethtool_get_status - get status of PSE control 1593 * @psec: PSE control pointer 1594 * @extack: extack for reporting useful error messages 1595 * @status: struct to store PSE status 1596 * 1597 * Return: 0 on success and failure value on error 1598 */ 1599 int pse_ethtool_get_status(struct pse_control *psec, 1600 struct netlink_ext_ack *extack, 1601 struct ethtool_pse_control_status *status) 1602 { 1603 struct pse_admin_state admin_state = {0}; 1604 struct pse_pw_status pw_status = {0}; 1605 const struct pse_controller_ops *ops; 1606 struct pse_controller_dev *pcdev; 1607 struct pse_pi *pi; 1608 int ret; 1609 1610 pcdev = psec->pcdev; 1611 ops = pcdev->ops; 1612 1613 pi = &pcdev->pi[psec->id]; 1614 mutex_lock(&pcdev->lock); 1615 if (pi->pw_d) { 1616 status->pw_d_id = pi->pw_d->id; 1617 if (pse_pw_d_is_sw_pw_control(pcdev, pi->pw_d)) { 1618 pse_get_sw_admin_state(psec, &admin_state); 1619 } else { 1620 ret = ops->pi_get_admin_state(pcdev, psec->id, 1621 &admin_state); 1622 if (ret) 1623 goto out; 1624 } 1625 status->podl_admin_state = admin_state.podl_admin_state; 1626 status->c33_admin_state = admin_state.c33_admin_state; 1627 1628 switch (pi->pw_d->budget_eval_strategy) { 1629 case PSE_BUDGET_EVAL_STRAT_STATIC: 1630 status->prio_max = pcdev->nr_lines - 1; 1631 status->prio = pi->prio; 1632 break; 1633 case PSE_BUDGET_EVAL_STRAT_DYNAMIC: 1634 status->prio_max = pcdev->pis_prio_max; 1635 if (ops->pi_get_prio) { 1636 ret = ops->pi_get_prio(pcdev, psec->id); 1637 if (ret < 0) 1638 goto out; 1639 1640 status->prio = ret; 1641 } 1642 break; 1643 default: 1644 break; 1645 } 1646 } 1647 1648 ret = ops->pi_get_pw_status(pcdev, psec->id, &pw_status); 1649 if (ret) 1650 goto out; 1651 status->podl_pw_status = pw_status.podl_pw_status; 1652 status->c33_pw_status = pw_status.c33_pw_status; 1653 1654 if (ops->pi_get_ext_state) { 1655 struct pse_ext_state_info ext_state_info = {0}; 1656 1657 ret = ops->pi_get_ext_state(pcdev, psec->id, 1658 &ext_state_info); 1659 if (ret) 1660 goto out; 1661 1662 memcpy(&status->c33_ext_state_info, 1663 &ext_state_info.c33_ext_state_info, 1664 sizeof(status->c33_ext_state_info)); 1665 } 1666 1667 if (ops->pi_get_pw_class) { 1668 ret = ops->pi_get_pw_class(pcdev, psec->id); 1669 if (ret < 0) 1670 goto out; 1671 1672 status->c33_pw_class = ret; 1673 } 1674 1675 if (ops->pi_get_actual_pw) { 1676 ret = ops->pi_get_actual_pw(pcdev, psec->id); 1677 if (ret < 0) 1678 goto out; 1679 1680 status->c33_actual_pw = ret; 1681 } 1682 1683 if (ops->pi_get_pw_limit) { 1684 ret = ops->pi_get_pw_limit(pcdev, psec->id); 1685 if (ret < 0) 1686 goto out; 1687 1688 status->c33_avail_pw_limit = ret; 1689 } 1690 1691 if (ops->pi_get_pw_limit_ranges) { 1692 struct pse_pw_limit_ranges pw_limit_ranges = {0}; 1693 1694 ret = ops->pi_get_pw_limit_ranges(pcdev, psec->id, 1695 &pw_limit_ranges); 1696 if (ret < 0) 1697 goto out; 1698 1699 status->c33_pw_limit_ranges = 1700 pw_limit_ranges.c33_pw_limit_ranges; 1701 status->c33_pw_limit_nb_ranges = ret; 1702 } 1703 out: 1704 mutex_unlock(&psec->pcdev->lock); 1705 return ret; 1706 } 1707 EXPORT_SYMBOL_GPL(pse_ethtool_get_status); 1708 1709 static int pse_ethtool_c33_set_config(struct pse_control *psec, 1710 const struct pse_control_config *config) 1711 { 1712 int err = 0; 1713 1714 /* Look at admin_state_enabled status to not call regulator_enable 1715 * or regulator_disable twice creating a regulator counter mismatch 1716 */ 1717 switch (config->c33_admin_control) { 1718 case ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED: 1719 /* We could have mismatch between admin_state_enabled and 1720 * state reported by regulator_is_enabled. This can occur when 1721 * the PI is forcibly turn off by the controller. Call 1722 * regulator_disable on that case to fix the counters state. 1723 */ 1724 if (psec->pcdev->pi[psec->id].admin_state_enabled && 1725 !regulator_is_enabled(psec->ps)) { 1726 err = regulator_disable(psec->ps); 1727 if (err) 1728 break; 1729 } 1730 if (!psec->pcdev->pi[psec->id].admin_state_enabled) 1731 err = regulator_enable(psec->ps); 1732 break; 1733 case ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED: 1734 if (psec->pcdev->pi[psec->id].admin_state_enabled) 1735 err = regulator_disable(psec->ps); 1736 break; 1737 default: 1738 err = -EOPNOTSUPP; 1739 } 1740 1741 return err; 1742 } 1743 1744 static int pse_ethtool_podl_set_config(struct pse_control *psec, 1745 const struct pse_control_config *config) 1746 { 1747 int err = 0; 1748 1749 /* Look at admin_state_enabled status to not call regulator_enable 1750 * or regulator_disable twice creating a regulator counter mismatch 1751 */ 1752 switch (config->podl_admin_control) { 1753 case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED: 1754 if (!psec->pcdev->pi[psec->id].admin_state_enabled) 1755 err = regulator_enable(psec->ps); 1756 break; 1757 case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED: 1758 if (psec->pcdev->pi[psec->id].admin_state_enabled) 1759 err = regulator_disable(psec->ps); 1760 break; 1761 default: 1762 err = -EOPNOTSUPP; 1763 } 1764 1765 return err; 1766 } 1767 1768 /** 1769 * pse_ethtool_set_config - set PSE control configuration 1770 * @psec: PSE control pointer 1771 * @extack: extack for reporting useful error messages 1772 * @config: Configuration of the test to run 1773 * 1774 * Return: 0 on success and failure value on error 1775 */ 1776 int pse_ethtool_set_config(struct pse_control *psec, 1777 struct netlink_ext_ack *extack, 1778 const struct pse_control_config *config) 1779 { 1780 int err = 0; 1781 1782 if (pse_has_c33(psec) && config->c33_admin_control) { 1783 err = pse_ethtool_c33_set_config(psec, config); 1784 if (err) 1785 return err; 1786 } 1787 1788 if (pse_has_podl(psec) && config->podl_admin_control) 1789 err = pse_ethtool_podl_set_config(psec, config); 1790 1791 return err; 1792 } 1793 EXPORT_SYMBOL_GPL(pse_ethtool_set_config); 1794 1795 /** 1796 * pse_pi_update_pw_budget - Update PSE power budget allocated with new 1797 * power in mW 1798 * @pcdev: a pointer to the PSE controller device 1799 * @id: index of the PSE PI 1800 * @pw_req: power requested 1801 * @extack: extack for reporting useful error messages 1802 * 1803 * Return: Previous power allocated on success and failure value on error 1804 */ 1805 static int pse_pi_update_pw_budget(struct pse_controller_dev *pcdev, int id, 1806 const unsigned int pw_req, 1807 struct netlink_ext_ack *extack) 1808 { 1809 struct pse_pi *pi = &pcdev->pi[id]; 1810 int previous_pw_allocated; 1811 int pw_diff, ret = 0; 1812 1813 /* We don't want pw_allocated_mW value change in the middle of an 1814 * power budget update 1815 */ 1816 mutex_lock(&pcdev->lock); 1817 previous_pw_allocated = pi->pw_allocated_mW; 1818 pw_diff = pw_req - previous_pw_allocated; 1819 if (!pw_diff) { 1820 goto out; 1821 } else if (pw_diff > 0) { 1822 ret = regulator_request_power_budget(pi->pw_d->supply, pw_diff); 1823 if (ret) { 1824 NL_SET_ERR_MSG_FMT(extack, 1825 "PI %d: not enough power budget available", 1826 id); 1827 goto out; 1828 } 1829 1830 } else { 1831 regulator_free_power_budget(pi->pw_d->supply, -pw_diff); 1832 } 1833 pi->pw_allocated_mW = pw_req; 1834 ret = previous_pw_allocated; 1835 1836 out: 1837 mutex_unlock(&pcdev->lock); 1838 return ret; 1839 } 1840 1841 /** 1842 * pse_ethtool_set_pw_limit - set PSE control power limit 1843 * @psec: PSE control pointer 1844 * @extack: extack for reporting useful error messages 1845 * @pw_limit: power limit value in mW 1846 * 1847 * Return: 0 on success and failure value on error 1848 */ 1849 int pse_ethtool_set_pw_limit(struct pse_control *psec, 1850 struct netlink_ext_ack *extack, 1851 const unsigned int pw_limit) 1852 { 1853 int uV, uA, ret, previous_pw_allocated = 0; 1854 s64 tmp_64; 1855 1856 if (pw_limit > MAX_PI_PW) 1857 return -ERANGE; 1858 1859 ret = regulator_get_voltage(psec->ps); 1860 if (!ret) { 1861 NL_SET_ERR_MSG(extack, 1862 "Can't calculate the current, PSE voltage read is 0"); 1863 return -ERANGE; 1864 } 1865 if (ret < 0) { 1866 NL_SET_ERR_MSG(extack, 1867 "Error reading PSE voltage"); 1868 return ret; 1869 } 1870 uV = ret; 1871 1872 tmp_64 = pw_limit; 1873 tmp_64 *= 1000000000ull; 1874 /* uA = mW * 1000000000 / uV */ 1875 uA = DIV_ROUND_CLOSEST_ULL(tmp_64, uV); 1876 1877 /* Update power budget only in software power control case and 1878 * if a Power Device is powered. 1879 */ 1880 if (pse_pw_d_is_sw_pw_control(psec->pcdev, 1881 psec->pcdev->pi[psec->id].pw_d) && 1882 psec->pcdev->pi[psec->id].admin_state_enabled && 1883 psec->pcdev->pi[psec->id].isr_pd_detected) { 1884 ret = pse_pi_update_pw_budget(psec->pcdev, psec->id, 1885 pw_limit, extack); 1886 if (ret < 0) 1887 return ret; 1888 previous_pw_allocated = ret; 1889 } 1890 1891 ret = regulator_set_current_limit(psec->ps, 0, uA); 1892 if (ret < 0 && previous_pw_allocated) { 1893 pse_pi_update_pw_budget(psec->pcdev, psec->id, 1894 previous_pw_allocated, extack); 1895 } 1896 1897 return ret; 1898 } 1899 EXPORT_SYMBOL_GPL(pse_ethtool_set_pw_limit); 1900 1901 /** 1902 * pse_ethtool_set_prio - Set PSE PI priority according to the budget 1903 * evaluation strategy 1904 * @psec: PSE control pointer 1905 * @extack: extack for reporting useful error messages 1906 * @prio: priovity value 1907 * 1908 * Return: 0 on success and failure value on error 1909 */ 1910 int pse_ethtool_set_prio(struct pse_control *psec, 1911 struct netlink_ext_ack *extack, 1912 unsigned int prio) 1913 { 1914 struct pse_controller_dev *pcdev = psec->pcdev; 1915 const struct pse_controller_ops *ops; 1916 int ret = 0; 1917 1918 if (!pcdev->pi[psec->id].pw_d) { 1919 NL_SET_ERR_MSG(extack, "no power domain attached"); 1920 return -EOPNOTSUPP; 1921 } 1922 1923 /* We don't want priority change in the middle of an 1924 * enable/disable call or a priority mode change 1925 */ 1926 mutex_lock(&pcdev->lock); 1927 switch (pcdev->pi[psec->id].pw_d->budget_eval_strategy) { 1928 case PSE_BUDGET_EVAL_STRAT_STATIC: 1929 if (prio >= pcdev->nr_lines) { 1930 NL_SET_ERR_MSG_FMT(extack, 1931 "priority %d exceed priority max %d", 1932 prio, pcdev->nr_lines); 1933 ret = -ERANGE; 1934 goto out; 1935 } 1936 1937 pcdev->pi[psec->id].prio = prio; 1938 pse_pw_d_retry_power_delivery(pcdev, pcdev->pi[psec->id].pw_d); 1939 break; 1940 1941 case PSE_BUDGET_EVAL_STRAT_DYNAMIC: 1942 ops = psec->pcdev->ops; 1943 if (!ops->pi_set_prio) { 1944 NL_SET_ERR_MSG(extack, 1945 "pse driver does not support setting port priority"); 1946 ret = -EOPNOTSUPP; 1947 goto out; 1948 } 1949 1950 if (prio > pcdev->pis_prio_max) { 1951 NL_SET_ERR_MSG_FMT(extack, 1952 "priority %d exceed priority max %d", 1953 prio, pcdev->pis_prio_max); 1954 ret = -ERANGE; 1955 goto out; 1956 } 1957 1958 ret = ops->pi_set_prio(pcdev, psec->id, prio); 1959 break; 1960 1961 default: 1962 ret = -EOPNOTSUPP; 1963 } 1964 1965 out: 1966 mutex_unlock(&pcdev->lock); 1967 return ret; 1968 } 1969 EXPORT_SYMBOL_GPL(pse_ethtool_set_prio); 1970 1971 bool pse_has_podl(struct pse_control *psec) 1972 { 1973 return psec->pcdev->types & ETHTOOL_PSE_PODL; 1974 } 1975 EXPORT_SYMBOL_GPL(pse_has_podl); 1976 1977 bool pse_has_c33(struct pse_control *psec) 1978 { 1979 return psec->pcdev->types & ETHTOOL_PSE_C33; 1980 } 1981 EXPORT_SYMBOL_GPL(pse_has_c33); 1982