1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * phy-core.c -- Generic Phy framework. 4 * 5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com 6 * 7 * Author: Kishon Vijay Abraham I <kishon@ti.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/export.h> 12 #include <linux/module.h> 13 #include <linux/err.h> 14 #include <linux/debugfs.h> 15 #include <linux/device.h> 16 #include <linux/slab.h> 17 #include <linux/of.h> 18 #include <linux/phy/phy.h> 19 #include <linux/idr.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/regulator/consumer.h> 22 23 static void phy_release(struct device *dev); 24 static const struct class phy_class = { 25 .name = "phy", 26 .dev_release = phy_release, 27 }; 28 29 static struct dentry *phy_debugfs_root; 30 static DEFINE_MUTEX(phy_provider_mutex); 31 static LIST_HEAD(phy_provider_list); 32 static LIST_HEAD(phys); 33 static DEFINE_IDA(phy_ida); 34 35 static void devm_phy_release(struct device *dev, void *res) 36 { 37 struct phy *phy = *(struct phy **)res; 38 39 phy_put(dev, phy); 40 } 41 42 static void devm_phy_provider_release(struct device *dev, void *res) 43 { 44 struct phy_provider *phy_provider = *(struct phy_provider **)res; 45 46 of_phy_provider_unregister(phy_provider); 47 } 48 49 static void devm_phy_consume(struct device *dev, void *res) 50 { 51 struct phy *phy = *(struct phy **)res; 52 53 phy_destroy(phy); 54 } 55 56 static int devm_phy_match(struct device *dev, void *res, void *match_data) 57 { 58 struct phy **phy = res; 59 60 return *phy == match_data; 61 } 62 63 /** 64 * phy_create_lookup() - allocate and register PHY/device association 65 * @phy: the phy of the association 66 * @con_id: connection ID string on device 67 * @dev_id: the device of the association 68 * 69 * Creates and registers phy_lookup entry. 70 */ 71 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id) 72 { 73 struct phy_lookup *pl; 74 75 if (!phy || !dev_id || !con_id) 76 return -EINVAL; 77 78 pl = kzalloc_obj(*pl); 79 if (!pl) 80 return -ENOMEM; 81 82 pl->dev_id = dev_id; 83 pl->con_id = con_id; 84 pl->phy = phy; 85 86 mutex_lock(&phy_provider_mutex); 87 list_add_tail(&pl->node, &phys); 88 mutex_unlock(&phy_provider_mutex); 89 90 return 0; 91 } 92 EXPORT_SYMBOL_GPL(phy_create_lookup); 93 94 /** 95 * phy_remove_lookup() - find and remove PHY/device association 96 * @phy: the phy of the association 97 * @con_id: connection ID string on device 98 * @dev_id: the device of the association 99 * 100 * Finds and unregisters phy_lookup entry that was created with 101 * phy_create_lookup(). 102 */ 103 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id) 104 { 105 struct phy_lookup *pl; 106 107 if (!phy || !dev_id || !con_id) 108 return; 109 110 mutex_lock(&phy_provider_mutex); 111 list_for_each_entry(pl, &phys, node) 112 if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) && 113 !strcmp(pl->con_id, con_id)) { 114 list_del(&pl->node); 115 kfree(pl); 116 break; 117 } 118 mutex_unlock(&phy_provider_mutex); 119 } 120 EXPORT_SYMBOL_GPL(phy_remove_lookup); 121 122 static struct phy *phy_find(struct device *dev, const char *con_id) 123 { 124 const char *dev_id = dev_name(dev); 125 struct phy_lookup *p, *pl = NULL; 126 127 lockdep_assert_held(&phy_provider_mutex); 128 129 list_for_each_entry(p, &phys, node) 130 if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) { 131 pl = p; 132 break; 133 } 134 135 return pl ? pl->phy : ERR_PTR(-ENODEV); 136 } 137 138 static struct phy_provider *of_phy_provider_lookup(struct device_node *node) 139 { 140 struct phy_provider *phy_provider; 141 142 list_for_each_entry(phy_provider, &phy_provider_list, list) { 143 if (phy_provider->dev->of_node == node) 144 return phy_provider; 145 146 for_each_child_of_node_scoped(phy_provider->children, child) 147 if (child == node) 148 return phy_provider; 149 } 150 151 return ERR_PTR(-EPROBE_DEFER); 152 } 153 154 int phy_pm_runtime_get(struct phy *phy) 155 { 156 int ret; 157 158 if (!phy) 159 return 0; 160 161 if (!pm_runtime_enabled(&phy->dev)) 162 return -ENOTSUPP; 163 164 ret = pm_runtime_get(&phy->dev); 165 if (ret < 0 && ret != -EINPROGRESS) 166 pm_runtime_put_noidle(&phy->dev); 167 168 return ret; 169 } 170 EXPORT_SYMBOL_GPL(phy_pm_runtime_get); 171 172 int phy_pm_runtime_get_sync(struct phy *phy) 173 { 174 int ret; 175 176 if (!phy) 177 return 0; 178 179 if (!pm_runtime_enabled(&phy->dev)) 180 return -ENOTSUPP; 181 182 ret = pm_runtime_get_sync(&phy->dev); 183 if (ret < 0) 184 pm_runtime_put_sync(&phy->dev); 185 186 return ret; 187 } 188 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync); 189 190 void phy_pm_runtime_put(struct phy *phy) 191 { 192 if (!phy) 193 return; 194 195 if (!pm_runtime_enabled(&phy->dev)) 196 return; 197 198 pm_runtime_put(&phy->dev); 199 } 200 EXPORT_SYMBOL_GPL(phy_pm_runtime_put); 201 202 int phy_pm_runtime_put_sync(struct phy *phy) 203 { 204 if (!phy) 205 return 0; 206 207 if (!pm_runtime_enabled(&phy->dev)) 208 return -ENOTSUPP; 209 210 return pm_runtime_put_sync(&phy->dev); 211 } 212 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync); 213 214 /** 215 * phy_init - phy internal initialization before phy operation 216 * @phy: the phy returned by phy_get() 217 * 218 * Used to allow phy's driver to perform phy internal initialization, 219 * such as PLL block powering, clock initialization or anything that's 220 * is required by the phy to perform the start of operation. 221 * Must be called before phy_power_on(). 222 * 223 * Return: %0 if successful, a negative error code otherwise 224 */ 225 int phy_init(struct phy *phy) 226 { 227 int ret; 228 229 if (!phy) 230 return 0; 231 232 ret = phy_pm_runtime_get_sync(phy); 233 if (ret < 0 && ret != -ENOTSUPP) 234 return ret; 235 ret = 0; /* Override possible ret == -ENOTSUPP */ 236 237 mutex_lock(&phy->mutex); 238 if (phy->power_count > phy->init_count) 239 dev_warn(&phy->dev, "phy_power_on was called before phy_init\n"); 240 241 if (phy->init_count == 0 && phy->ops->init) { 242 ret = phy->ops->init(phy); 243 if (ret < 0) { 244 dev_err(&phy->dev, "phy init failed --> %d\n", ret); 245 goto out; 246 } 247 } 248 ++phy->init_count; 249 250 out: 251 mutex_unlock(&phy->mutex); 252 phy_pm_runtime_put(phy); 253 return ret; 254 } 255 EXPORT_SYMBOL_GPL(phy_init); 256 257 /** 258 * phy_exit - Phy internal un-initialization 259 * @phy: the phy returned by phy_get() 260 * 261 * Must be called after phy_power_off(). 262 * 263 * Return: %0 if successful, a negative error code otherwise 264 */ 265 int phy_exit(struct phy *phy) 266 { 267 int ret; 268 269 if (!phy) 270 return 0; 271 272 ret = phy_pm_runtime_get_sync(phy); 273 if (ret < 0 && ret != -ENOTSUPP) 274 return ret; 275 ret = 0; /* Override possible ret == -ENOTSUPP */ 276 277 mutex_lock(&phy->mutex); 278 if (phy->init_count == 1 && phy->ops->exit) { 279 ret = phy->ops->exit(phy); 280 if (ret < 0) { 281 dev_err(&phy->dev, "phy exit failed --> %d\n", ret); 282 goto out; 283 } 284 } 285 --phy->init_count; 286 287 out: 288 mutex_unlock(&phy->mutex); 289 phy_pm_runtime_put(phy); 290 return ret; 291 } 292 EXPORT_SYMBOL_GPL(phy_exit); 293 294 /** 295 * phy_power_on - Enable the phy and enter proper operation 296 * @phy: the phy returned by phy_get() 297 * 298 * Must be called after phy_init(). 299 * 300 * Return: %0 if successful, a negative error code otherwise 301 */ 302 int phy_power_on(struct phy *phy) 303 { 304 int ret = 0; 305 306 if (!phy) 307 goto out; 308 309 if (phy->pwr) { 310 ret = regulator_enable(phy->pwr); 311 if (ret) 312 goto out; 313 } 314 315 ret = phy_pm_runtime_get_sync(phy); 316 if (ret < 0 && ret != -ENOTSUPP) 317 goto err_pm_sync; 318 319 ret = 0; /* Override possible ret == -ENOTSUPP */ 320 321 mutex_lock(&phy->mutex); 322 if (phy->power_count == 0 && phy->ops->power_on) { 323 ret = phy->ops->power_on(phy); 324 if (ret < 0) { 325 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret); 326 goto err_pwr_on; 327 } 328 } 329 ++phy->power_count; 330 mutex_unlock(&phy->mutex); 331 return 0; 332 333 err_pwr_on: 334 mutex_unlock(&phy->mutex); 335 phy_pm_runtime_put_sync(phy); 336 err_pm_sync: 337 if (phy->pwr) 338 regulator_disable(phy->pwr); 339 out: 340 return ret; 341 } 342 EXPORT_SYMBOL_GPL(phy_power_on); 343 344 /** 345 * phy_power_off - Disable the phy. 346 * @phy: the phy returned by phy_get() 347 * 348 * Must be called before phy_exit(). 349 * 350 * Return: %0 if successful, a negative error code otherwise 351 */ 352 int phy_power_off(struct phy *phy) 353 { 354 int ret; 355 356 if (!phy) 357 return 0; 358 359 mutex_lock(&phy->mutex); 360 if (phy->power_count == 1 && phy->ops->power_off) { 361 ret = phy->ops->power_off(phy); 362 if (ret < 0) { 363 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret); 364 mutex_unlock(&phy->mutex); 365 return ret; 366 } 367 } 368 --phy->power_count; 369 mutex_unlock(&phy->mutex); 370 phy_pm_runtime_put(phy); 371 372 if (phy->pwr) 373 regulator_disable(phy->pwr); 374 375 return 0; 376 } 377 EXPORT_SYMBOL_GPL(phy_power_off); 378 379 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode) 380 { 381 int ret = 0; 382 383 if (!phy) 384 return 0; 385 386 mutex_lock(&phy->mutex); 387 if (phy->ops->set_mode) 388 ret = phy->ops->set_mode(phy, mode, submode); 389 if (!ret) 390 phy->attrs.mode = mode; 391 mutex_unlock(&phy->mutex); 392 393 return ret; 394 } 395 EXPORT_SYMBOL_GPL(phy_set_mode_ext); 396 397 int phy_set_media(struct phy *phy, enum phy_media media) 398 { 399 int ret; 400 401 if (!phy || !phy->ops->set_media) 402 return 0; 403 404 mutex_lock(&phy->mutex); 405 ret = phy->ops->set_media(phy, media); 406 mutex_unlock(&phy->mutex); 407 408 return ret; 409 } 410 EXPORT_SYMBOL_GPL(phy_set_media); 411 412 int phy_set_speed(struct phy *phy, int speed) 413 { 414 int ret; 415 416 if (!phy || !phy->ops->set_speed) 417 return 0; 418 419 mutex_lock(&phy->mutex); 420 ret = phy->ops->set_speed(phy, speed); 421 mutex_unlock(&phy->mutex); 422 423 return ret; 424 } 425 EXPORT_SYMBOL_GPL(phy_set_speed); 426 427 int phy_reset(struct phy *phy) 428 { 429 int ret; 430 431 if (!phy || !phy->ops->reset) 432 return 0; 433 434 ret = phy_pm_runtime_get_sync(phy); 435 if (ret < 0 && ret != -ENOTSUPP) 436 return ret; 437 438 mutex_lock(&phy->mutex); 439 ret = phy->ops->reset(phy); 440 mutex_unlock(&phy->mutex); 441 442 phy_pm_runtime_put(phy); 443 444 return ret; 445 } 446 EXPORT_SYMBOL_GPL(phy_reset); 447 448 /** 449 * phy_calibrate() - Tunes the phy hw parameters for current configuration 450 * @phy: the phy returned by phy_get() 451 * 452 * Used to calibrate phy hardware, typically by adjusting some parameters in 453 * runtime, which are otherwise lost after host controller reset and cannot 454 * be applied in phy_init() or phy_power_on(). 455 * 456 * Return: %0 if successful, a negative error code otherwise 457 */ 458 int phy_calibrate(struct phy *phy) 459 { 460 int ret; 461 462 if (!phy || !phy->ops->calibrate) 463 return 0; 464 465 mutex_lock(&phy->mutex); 466 ret = phy->ops->calibrate(phy); 467 mutex_unlock(&phy->mutex); 468 469 return ret; 470 } 471 EXPORT_SYMBOL_GPL(phy_calibrate); 472 473 /** 474 * phy_notify_connect() - phy connect notification 475 * @phy: the phy returned by phy_get() 476 * @port: the port index for connect 477 * 478 * If the phy needs to get connection status, the callback can be used. 479 * Returns: %0 if successful, a negative error code otherwise 480 */ 481 int phy_notify_connect(struct phy *phy, int port) 482 { 483 int ret; 484 485 if (!phy || !phy->ops->connect) 486 return 0; 487 488 mutex_lock(&phy->mutex); 489 ret = phy->ops->connect(phy, port); 490 mutex_unlock(&phy->mutex); 491 492 return ret; 493 } 494 EXPORT_SYMBOL_GPL(phy_notify_connect); 495 496 /** 497 * phy_notify_disconnect() - phy disconnect notification 498 * @phy: the phy returned by phy_get() 499 * @port: the port index for disconnect 500 * 501 * If the phy needs to get connection status, the callback can be used. 502 * 503 * Returns: %0 if successful, a negative error code otherwise 504 */ 505 int phy_notify_disconnect(struct phy *phy, int port) 506 { 507 int ret; 508 509 if (!phy || !phy->ops->disconnect) 510 return 0; 511 512 mutex_lock(&phy->mutex); 513 ret = phy->ops->disconnect(phy, port); 514 mutex_unlock(&phy->mutex); 515 516 return ret; 517 } 518 EXPORT_SYMBOL_GPL(phy_notify_disconnect); 519 520 /** 521 * phy_notify_state() - phy state notification 522 * @phy: the PHY returned by phy_get() 523 * @state: the PHY state 524 * 525 * Notify the PHY of a state transition. Used to notify and 526 * configure the PHY accordingly. 527 * 528 * Returns: %0 if successful, a negative error code otherwise 529 */ 530 int phy_notify_state(struct phy *phy, union phy_notify state) 531 { 532 int ret; 533 534 if (!phy || !phy->ops->notify_phystate) 535 return 0; 536 537 mutex_lock(&phy->mutex); 538 ret = phy->ops->notify_phystate(phy, state); 539 mutex_unlock(&phy->mutex); 540 541 return ret; 542 } 543 EXPORT_SYMBOL_GPL(phy_notify_state); 544 545 /** 546 * phy_configure() - Changes the phy parameters 547 * @phy: the phy returned by phy_get() 548 * @opts: New configuration to apply 549 * 550 * Used to change the PHY parameters. phy_init() must have been called 551 * on the phy. The configuration will be applied on the current phy 552 * mode, that can be changed using phy_set_mode(). 553 * 554 * Return: %0 if successful, a negative error code otherwise 555 */ 556 int phy_configure(struct phy *phy, union phy_configure_opts *opts) 557 { 558 int ret; 559 560 if (!phy) 561 return -EINVAL; 562 563 if (!phy->ops->configure) 564 return -EOPNOTSUPP; 565 566 mutex_lock(&phy->mutex); 567 ret = phy->ops->configure(phy, opts); 568 mutex_unlock(&phy->mutex); 569 570 return ret; 571 } 572 EXPORT_SYMBOL_GPL(phy_configure); 573 574 /** 575 * phy_validate() - Checks the phy parameters 576 * @phy: the phy returned by phy_get() 577 * @mode: phy_mode the configuration is applicable to. 578 * @submode: PHY submode the configuration is applicable to. 579 * @opts: Configuration to check 580 * 581 * Used to check that the current set of parameters can be handled by 582 * the phy. Implementations are free to tune the parameters passed as 583 * arguments if needed by some implementation detail or 584 * constraints. It will not change any actual configuration of the 585 * PHY, so calling it as many times as deemed fit will have no side 586 * effect. 587 * 588 * Return: %0 if successful, a negative error code otherwise 589 */ 590 int phy_validate(struct phy *phy, enum phy_mode mode, int submode, 591 union phy_configure_opts *opts) 592 { 593 int ret; 594 595 if (!phy) 596 return -EINVAL; 597 598 if (!phy->ops->validate) 599 return -EOPNOTSUPP; 600 601 mutex_lock(&phy->mutex); 602 ret = phy->ops->validate(phy, mode, submode, opts); 603 mutex_unlock(&phy->mutex); 604 605 return ret; 606 } 607 EXPORT_SYMBOL_GPL(phy_validate); 608 609 /** 610 * _of_phy_get_with_args() - lookup and obtain a reference to a phy by of_phandle_args 611 * @args: of_phandle_args to the phy 612 * 613 * Returns: the phy from the provider's of_xlate, -ENODEV if disabled, 614 * -EPROBE_DEFER if the provider is not yet registered. 615 */ 616 static struct phy *_of_phy_get_with_args(struct of_phandle_args *args) 617 { 618 struct phy *phy; 619 struct phy_provider *phy_provider; 620 621 lockdep_assert_held(&phy_provider_mutex); 622 623 phy_provider = of_phy_provider_lookup(args->np); 624 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) 625 return ERR_PTR(-EPROBE_DEFER); 626 627 if (!of_device_is_available(args->np)) { 628 dev_warn(phy_provider->dev, "Requested PHY is disabled\n"); 629 phy = ERR_PTR(-ENODEV); 630 goto out_put_module; 631 } 632 633 phy = phy_provider->of_xlate(phy_provider->dev, args); 634 635 out_put_module: 636 module_put(phy_provider->owner); 637 638 return phy; 639 } 640 641 /** 642 * _of_phy_get() - lookup and obtain a reference to a phy by phandle 643 * @np: device_node for which to get the phy 644 * @index: the index of the phy 645 * 646 * Returns: the phy associated with the given phandle value, 647 * after getting a refcount to it or -ENODEV if there is no such phy or 648 * -EPROBE_DEFER if there is a phandle to the phy, but the device is 649 * not yet loaded. This function uses of_xlate call back function provided 650 * while registering the phy_provider to find the phy instance. 651 */ 652 static struct phy *_of_phy_get(struct device_node *np, int index) 653 { 654 int ret; 655 struct phy *phy; 656 struct of_phandle_args args; 657 658 lockdep_assert_held(&phy_provider_mutex); 659 660 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells", 661 index, &args); 662 if (ret) 663 return ERR_PTR(-ENODEV); 664 665 /* This phy type handled by the usb-phy subsystem for now */ 666 if (of_device_is_compatible(args.np, "usb-nop-xceiv")) { 667 phy = ERR_PTR(-ENODEV); 668 goto out_put_node; 669 } 670 671 phy = _of_phy_get_with_args(&args); 672 673 out_put_node: 674 of_node_put(args.np); 675 676 return phy; 677 } 678 679 /** 680 * of_phy_get() - lookup and obtain a reference to a phy using a device_node. 681 * @np: device_node for which to get the phy 682 * @con_id: name of the phy from device's point of view 683 * 684 * Returns: the phy driver, after getting a refcount to it; or 685 * -ENODEV if there is no such phy. The caller is responsible for 686 * calling of_phy_put() to release that count. 687 */ 688 struct phy *of_phy_get(struct device_node *np, const char *con_id) 689 { 690 struct phy *phy = NULL; 691 int index = 0; 692 693 if (con_id) 694 index = of_property_match_string(np, "phy-names", con_id); 695 696 mutex_lock(&phy_provider_mutex); 697 698 phy = _of_phy_get(np, index); 699 if (IS_ERR(phy)) 700 goto out_unlock; 701 702 if (!try_module_get(phy->ops->owner)) { 703 phy = ERR_PTR(-EPROBE_DEFER); 704 goto out_unlock; 705 } 706 707 get_device(&phy->dev); 708 709 out_unlock: 710 mutex_unlock(&phy_provider_mutex); 711 return phy; 712 } 713 EXPORT_SYMBOL_GPL(of_phy_get); 714 715 /** 716 * of_phy_put() - release the PHY 717 * @phy: the phy returned by of_phy_get() 718 * 719 * Releases a refcount the caller received from of_phy_get(). 720 */ 721 void of_phy_put(struct phy *phy) 722 { 723 if (!phy || IS_ERR(phy)) 724 return; 725 726 mutex_lock(&phy->mutex); 727 if (phy->ops->release) 728 phy->ops->release(phy); 729 mutex_unlock(&phy->mutex); 730 731 module_put(phy->ops->owner); 732 put_device(&phy->dev); 733 } 734 EXPORT_SYMBOL_GPL(of_phy_put); 735 736 /** 737 * phy_put() - release the PHY 738 * @dev: device that wants to release this phy 739 * @phy: the phy returned by phy_get() 740 * 741 * Releases a refcount the caller received from phy_get(). 742 */ 743 void phy_put(struct device *dev, struct phy *phy) 744 { 745 device_link_remove(dev, &phy->dev); 746 of_phy_put(phy); 747 } 748 EXPORT_SYMBOL_GPL(phy_put); 749 750 /** 751 * devm_phy_put() - release the PHY 752 * @dev: device that wants to release this phy 753 * @phy: the phy returned by devm_phy_get() 754 * 755 * destroys the devres associated with this phy and invokes phy_put 756 * to release the phy. 757 */ 758 void devm_phy_put(struct device *dev, struct phy *phy) 759 { 760 int r; 761 762 if (!phy) 763 return; 764 765 r = devres_release(dev, devm_phy_release, devm_phy_match, phy); 766 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); 767 } 768 EXPORT_SYMBOL_GPL(devm_phy_put); 769 770 /** 771 * of_phy_simple_xlate() - returns the phy instance from phy provider 772 * @dev: the PHY provider device (not used here) 773 * @args: of_phandle_args 774 * 775 * Intended to be used by phy provider for the common case where #phy-cells is 776 * 0. For other cases where #phy-cells is greater than '0', the phy provider 777 * should provide a custom of_xlate function that reads the *args* and returns 778 * the appropriate phy. 779 */ 780 struct phy *of_phy_simple_xlate(struct device *dev, 781 const struct of_phandle_args *args) 782 { 783 struct device *target_dev; 784 785 target_dev = class_find_device_by_of_node(&phy_class, args->np); 786 if (!target_dev) 787 return ERR_PTR(-ENODEV); 788 789 put_device(target_dev); 790 return to_phy(target_dev); 791 } 792 EXPORT_SYMBOL_GPL(of_phy_simple_xlate); 793 794 /** 795 * phy_get() - lookup and obtain a reference to a phy. 796 * @dev: device that requests this phy 797 * @string: the phy name as given in the dt data or the name of the controller 798 * port for non-dt case 799 * 800 * Returns: the phy driver, after getting a refcount to it; or 801 * -ENODEV if there is no such phy. The caller is responsible for 802 * calling phy_put() to release that count. 803 */ 804 struct phy *phy_get(struct device *dev, const char *string) 805 { 806 int index = 0; 807 struct phy *phy; 808 struct device_link *link; 809 810 mutex_lock(&phy_provider_mutex); 811 if (dev->of_node) { 812 if (string) 813 index = of_property_match_string(dev->of_node, "phy-names", 814 string); 815 else 816 index = 0; 817 phy = _of_phy_get(dev->of_node, index); 818 } else { 819 if (string == NULL) { 820 dev_WARN(dev, "missing string\n"); 821 phy = ERR_PTR(-EINVAL); 822 goto out_unlock; 823 } 824 phy = phy_find(dev, string); 825 } 826 if (IS_ERR(phy)) 827 goto out_unlock; 828 829 if (!try_module_get(phy->ops->owner)) { 830 phy = ERR_PTR(-EPROBE_DEFER); 831 goto out_unlock; 832 } 833 834 get_device(&phy->dev); 835 836 link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); 837 if (!link) 838 dev_dbg(dev, "failed to create device link to %s\n", 839 dev_name(phy->dev.parent)); 840 841 out_unlock: 842 mutex_unlock(&phy_provider_mutex); 843 return phy; 844 } 845 EXPORT_SYMBOL_GPL(phy_get); 846 847 /** 848 * devm_phy_get() - lookup and obtain a reference to a phy. 849 * @dev: device that requests this phy 850 * @string: the phy name as given in the dt data or phy device name 851 * for non-dt case 852 * 853 * Gets the phy using phy_get(), and associates a device with it using 854 * devres. On driver detach, release function is invoked on the devres data, 855 * then, devres data is freed. 856 */ 857 struct phy *devm_phy_get(struct device *dev, const char *string) 858 { 859 struct phy **ptr, *phy; 860 861 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 862 if (!ptr) 863 return ERR_PTR(-ENOMEM); 864 865 phy = phy_get(dev, string); 866 if (!IS_ERR(phy)) { 867 *ptr = phy; 868 devres_add(dev, ptr); 869 } else { 870 devres_free(ptr); 871 } 872 873 return phy; 874 } 875 EXPORT_SYMBOL_GPL(devm_phy_get); 876 877 /** 878 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy. 879 * @dev: device that requests this phy 880 * @string: the phy name as given in the dt data or phy device name 881 * for non-dt case 882 * 883 * Gets the phy using phy_get(), and associates a device with it using 884 * devres. On driver detach, release function is invoked on the devres 885 * data, then, devres data is freed. This differs to devm_phy_get() in 886 * that if the phy does not exist, it is not considered an error and 887 * -ENODEV will not be returned. Instead the NULL phy is returned, 888 * which can be passed to all other phy consumer calls. 889 */ 890 struct phy *devm_phy_optional_get(struct device *dev, const char *string) 891 { 892 struct phy *phy = devm_phy_get(dev, string); 893 894 if (PTR_ERR(phy) == -ENODEV) 895 phy = NULL; 896 897 return phy; 898 } 899 EXPORT_SYMBOL_GPL(devm_phy_optional_get); 900 901 /** 902 * devm_of_phy_get() - lookup and obtain a reference to a phy. 903 * @dev: device that requests this phy 904 * @np: node containing the phy 905 * @con_id: name of the phy from device's point of view 906 * 907 * Gets the phy using of_phy_get(), and associates a device with it using 908 * devres. On driver detach, release function is invoked on the devres data, 909 * then, devres data is freed. 910 */ 911 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, 912 const char *con_id) 913 { 914 struct phy **ptr, *phy; 915 struct device_link *link; 916 917 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 918 if (!ptr) 919 return ERR_PTR(-ENOMEM); 920 921 phy = of_phy_get(np, con_id); 922 if (!IS_ERR(phy)) { 923 *ptr = phy; 924 devres_add(dev, ptr); 925 } else { 926 devres_free(ptr); 927 return phy; 928 } 929 930 link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); 931 if (!link) 932 dev_dbg(dev, "failed to create device link to %s\n", 933 dev_name(phy->dev.parent)); 934 935 return phy; 936 } 937 EXPORT_SYMBOL_GPL(devm_of_phy_get); 938 939 /** 940 * devm_of_phy_optional_get() - lookup and obtain a reference to an optional 941 * phy. 942 * @dev: device that requests this phy 943 * @np: node containing the phy 944 * @con_id: name of the phy from device's point of view 945 * 946 * Gets the phy using of_phy_get(), and associates a device with it using 947 * devres. On driver detach, release function is invoked on the devres data, 948 * then, devres data is freed. This differs to devm_of_phy_get() in 949 * that if the phy does not exist, it is not considered an error and 950 * -ENODEV will not be returned. Instead the NULL phy is returned, 951 * which can be passed to all other phy consumer calls. 952 */ 953 struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np, 954 const char *con_id) 955 { 956 struct phy *phy = devm_of_phy_get(dev, np, con_id); 957 958 if (PTR_ERR(phy) == -ENODEV) 959 phy = NULL; 960 961 if (IS_ERR(phy)) 962 dev_err_probe(dev, PTR_ERR(phy), "failed to get PHY %pOF:%s", 963 np, con_id); 964 965 return phy; 966 } 967 EXPORT_SYMBOL_GPL(devm_of_phy_optional_get); 968 969 /** 970 * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index. 971 * @dev: device that requests this phy 972 * @np: node containing the phy 973 * @index: index of the phy 974 * 975 * Gets the phy using _of_phy_get(), then gets a refcount to it, 976 * and associates a device with it using devres. On driver detach, 977 * release function is invoked on the devres data, 978 * then, devres data is freed. 979 * 980 */ 981 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np, 982 int index) 983 { 984 struct phy **ptr, *phy; 985 struct device_link *link; 986 987 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 988 if (!ptr) 989 return ERR_PTR(-ENOMEM); 990 991 mutex_lock(&phy_provider_mutex); 992 phy = _of_phy_get(np, index); 993 if (IS_ERR(phy)) { 994 devres_free(ptr); 995 goto out_unlock; 996 } 997 998 if (!try_module_get(phy->ops->owner)) { 999 devres_free(ptr); 1000 phy = ERR_PTR(-EPROBE_DEFER); 1001 goto out_unlock; 1002 } 1003 1004 get_device(&phy->dev); 1005 1006 *ptr = phy; 1007 devres_add(dev, ptr); 1008 1009 link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); 1010 if (!link) 1011 dev_dbg(dev, "failed to create device link to %s\n", 1012 dev_name(phy->dev.parent)); 1013 1014 out_unlock: 1015 mutex_unlock(&phy_provider_mutex); 1016 return phy; 1017 } 1018 EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index); 1019 1020 /** 1021 * phy_get_by_of_node() - lookup and obtain a reference to a phy by device_node 1022 * @np: node containing the phy 1023 * 1024 * Returns: the phy associated with the device node or ERR_PTR. 1025 */ 1026 struct phy *phy_get_by_of_node(struct device_node *np) 1027 { 1028 struct of_phandle_args args = { .np = np, .args_count = 0 }; 1029 struct phy *phy; 1030 1031 mutex_lock(&phy_provider_mutex); 1032 1033 phy = _of_phy_get_with_args(&args); 1034 if (IS_ERR(phy)) 1035 goto out_unlock; 1036 1037 if (!try_module_get(phy->ops->owner)) { 1038 phy = ERR_PTR(-EPROBE_DEFER); 1039 goto out_unlock; 1040 } 1041 1042 get_device(&phy->dev); 1043 1044 out_unlock: 1045 mutex_unlock(&phy_provider_mutex); 1046 1047 return phy; 1048 } 1049 EXPORT_SYMBOL_GPL(phy_get_by_of_node); 1050 1051 /** 1052 * devm_phy_get_by_of_node() - devm managed lookup and obtain phy reference by device node 1053 * @dev: device requesting the PHY 1054 * @np: device_node of the PHY provider 1055 * 1056 * Returns: phy associated with the device_node or ERR_PTR. devres manages 1057 * releasing resources. 1058 */ 1059 struct phy *devm_phy_get_by_of_node(struct device *dev, struct device_node *np) 1060 { 1061 struct phy **ptr, *phy; 1062 struct device_link *link; 1063 1064 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 1065 if (!ptr) 1066 return ERR_PTR(-ENOMEM); 1067 1068 phy = phy_get_by_of_node(np); 1069 if (IS_ERR(phy)) { 1070 devres_free(ptr); 1071 return phy; 1072 } 1073 1074 *ptr = phy; 1075 devres_add(dev, ptr); 1076 link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); 1077 if (!link) 1078 dev_dbg(dev, "failed to create device link to %s\n", 1079 dev_name(phy->dev.parent)); 1080 1081 return phy; 1082 } 1083 EXPORT_SYMBOL_GPL(devm_phy_get_by_of_node); 1084 1085 /** 1086 * phy_create() - create a new phy 1087 * @dev: device that is creating the new phy 1088 * @node: device node of the phy 1089 * @ops: function pointers for performing phy operations 1090 * 1091 * Called to create a phy using phy framework. 1092 */ 1093 struct phy *phy_create(struct device *dev, struct device_node *node, 1094 const struct phy_ops *ops) 1095 { 1096 int ret; 1097 int id; 1098 struct phy *phy; 1099 1100 if (WARN_ON(!dev)) 1101 return ERR_PTR(-EINVAL); 1102 1103 phy = kzalloc_obj(*phy); 1104 if (!phy) 1105 return ERR_PTR(-ENOMEM); 1106 1107 id = ida_alloc(&phy_ida, GFP_KERNEL); 1108 if (id < 0) { 1109 dev_err(dev, "unable to get id\n"); 1110 ret = id; 1111 goto free_phy; 1112 } 1113 1114 device_initialize(&phy->dev); 1115 lockdep_register_key(&phy->lockdep_key); 1116 mutex_init_with_key(&phy->mutex, &phy->lockdep_key); 1117 1118 phy->dev.class = &phy_class; 1119 phy->dev.parent = dev; 1120 phy->dev.of_node = node ?: dev->of_node; 1121 phy->id = id; 1122 phy->ops = ops; 1123 1124 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id); 1125 if (ret) 1126 goto put_dev; 1127 1128 /* phy-supply */ 1129 phy->pwr = regulator_get_optional(&phy->dev, "phy"); 1130 if (IS_ERR(phy->pwr)) { 1131 ret = PTR_ERR(phy->pwr); 1132 if (ret == -EPROBE_DEFER) 1133 goto put_dev; 1134 1135 phy->pwr = NULL; 1136 } 1137 1138 ret = device_add(&phy->dev); 1139 if (ret) 1140 goto put_dev; 1141 1142 if (pm_runtime_enabled(dev)) { 1143 pm_runtime_enable(&phy->dev); 1144 pm_runtime_no_callbacks(&phy->dev); 1145 } 1146 1147 phy->debugfs = debugfs_create_dir(dev_name(&phy->dev), phy_debugfs_root); 1148 1149 return phy; 1150 1151 put_dev: 1152 put_device(&phy->dev); /* calls phy_release() which frees resources */ 1153 return ERR_PTR(ret); 1154 1155 free_phy: 1156 kfree(phy); 1157 return ERR_PTR(ret); 1158 } 1159 EXPORT_SYMBOL_GPL(phy_create); 1160 1161 /** 1162 * devm_phy_create() - create a new phy 1163 * @dev: device that is creating the new phy 1164 * @node: device node of the phy 1165 * @ops: function pointers for performing phy operations 1166 * 1167 * Creates a new PHY device adding it to the PHY class. 1168 * While at that, it also associates the device with the phy using devres. 1169 * On driver detach, release function is invoked on the devres data, 1170 * then, devres data is freed. 1171 */ 1172 struct phy *devm_phy_create(struct device *dev, struct device_node *node, 1173 const struct phy_ops *ops) 1174 { 1175 struct phy **ptr, *phy; 1176 1177 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL); 1178 if (!ptr) 1179 return ERR_PTR(-ENOMEM); 1180 1181 phy = phy_create(dev, node, ops); 1182 if (!IS_ERR(phy)) { 1183 *ptr = phy; 1184 devres_add(dev, ptr); 1185 } else { 1186 devres_free(ptr); 1187 } 1188 1189 return phy; 1190 } 1191 EXPORT_SYMBOL_GPL(devm_phy_create); 1192 1193 /** 1194 * phy_destroy() - destroy the phy 1195 * @phy: the phy to be destroyed 1196 * 1197 * Called to destroy the phy. 1198 */ 1199 void phy_destroy(struct phy *phy) 1200 { 1201 pm_runtime_disable(&phy->dev); 1202 device_unregister(&phy->dev); 1203 } 1204 EXPORT_SYMBOL_GPL(phy_destroy); 1205 1206 /** 1207 * devm_phy_destroy() - destroy the PHY 1208 * @dev: device that wants to release this phy 1209 * @phy: the phy returned by devm_phy_get() 1210 * 1211 * destroys the devres associated with this phy and invokes phy_destroy 1212 * to destroy the phy. 1213 */ 1214 void devm_phy_destroy(struct device *dev, struct phy *phy) 1215 { 1216 int r; 1217 1218 r = devres_release(dev, devm_phy_consume, devm_phy_match, phy); 1219 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); 1220 } 1221 EXPORT_SYMBOL_GPL(devm_phy_destroy); 1222 1223 /** 1224 * __of_phy_provider_register() - create/register phy provider with the framework 1225 * @dev: struct device of the phy provider 1226 * @children: device node containing children (if different from dev->of_node) 1227 * @owner: the module owner containing of_xlate 1228 * @of_xlate: function pointer to obtain phy instance from phy provider 1229 * 1230 * Creates struct phy_provider from dev and of_xlate function pointer. 1231 * This is used in the case of dt boot for finding the phy instance from 1232 * phy provider. 1233 * 1234 * If the PHY provider doesn't nest children directly but uses a separate 1235 * child node to contain the individual children, the @children parameter 1236 * can be used to override the default. If NULL, the default (dev->of_node) 1237 * will be used. If non-NULL, the device node must be a child (or further 1238 * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL 1239 * error code is returned. 1240 */ 1241 struct phy_provider *__of_phy_provider_register(struct device *dev, 1242 struct device_node *children, struct module *owner, 1243 struct phy * (*of_xlate)(struct device *dev, 1244 const struct of_phandle_args *args)) 1245 { 1246 struct phy_provider *phy_provider; 1247 1248 /* 1249 * If specified, the device node containing the children must itself 1250 * be the provider's device node or a child (or further descendant) 1251 * thereof. 1252 */ 1253 if (children) { 1254 struct device_node *parent = of_node_get(children), *next; 1255 1256 while (parent) { 1257 if (parent == dev->of_node) 1258 break; 1259 1260 next = of_get_parent(parent); 1261 of_node_put(parent); 1262 parent = next; 1263 } 1264 1265 if (!parent) 1266 return ERR_PTR(-EINVAL); 1267 1268 of_node_put(parent); 1269 } else { 1270 children = dev->of_node; 1271 } 1272 1273 phy_provider = kzalloc_obj(*phy_provider); 1274 if (!phy_provider) 1275 return ERR_PTR(-ENOMEM); 1276 1277 phy_provider->dev = dev; 1278 phy_provider->children = of_node_get(children); 1279 phy_provider->owner = owner; 1280 phy_provider->of_xlate = of_xlate; 1281 1282 mutex_lock(&phy_provider_mutex); 1283 list_add_tail(&phy_provider->list, &phy_provider_list); 1284 mutex_unlock(&phy_provider_mutex); 1285 1286 return phy_provider; 1287 } 1288 EXPORT_SYMBOL_GPL(__of_phy_provider_register); 1289 1290 /** 1291 * __devm_of_phy_provider_register() - create/register phy provider with the 1292 * framework 1293 * @dev: struct device of the phy provider 1294 * @children: device node containing children (if different from dev->of_node) 1295 * @owner: the module owner containing of_xlate 1296 * @of_xlate: function pointer to obtain phy instance from phy provider 1297 * 1298 * Creates struct phy_provider from dev and of_xlate function pointer. 1299 * This is used in the case of dt boot for finding the phy instance from 1300 * phy provider. While at that, it also associates the device with the 1301 * phy provider using devres. On driver detach, release function is invoked 1302 * on the devres data, then, devres data is freed. 1303 */ 1304 struct phy_provider *__devm_of_phy_provider_register(struct device *dev, 1305 struct device_node *children, struct module *owner, 1306 struct phy * (*of_xlate)(struct device *dev, 1307 const struct of_phandle_args *args)) 1308 { 1309 struct phy_provider **ptr, *phy_provider; 1310 1311 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL); 1312 if (!ptr) 1313 return ERR_PTR(-ENOMEM); 1314 1315 phy_provider = __of_phy_provider_register(dev, children, owner, 1316 of_xlate); 1317 if (!IS_ERR(phy_provider)) { 1318 *ptr = phy_provider; 1319 devres_add(dev, ptr); 1320 } else { 1321 devres_free(ptr); 1322 } 1323 1324 return phy_provider; 1325 } 1326 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register); 1327 1328 /** 1329 * of_phy_provider_unregister() - unregister phy provider from the framework 1330 * @phy_provider: phy provider returned by of_phy_provider_register() 1331 * 1332 * Removes the phy_provider created using of_phy_provider_register(). 1333 */ 1334 void of_phy_provider_unregister(struct phy_provider *phy_provider) 1335 { 1336 if (IS_ERR(phy_provider)) 1337 return; 1338 1339 mutex_lock(&phy_provider_mutex); 1340 list_del(&phy_provider->list); 1341 of_node_put(phy_provider->children); 1342 kfree(phy_provider); 1343 mutex_unlock(&phy_provider_mutex); 1344 } 1345 EXPORT_SYMBOL_GPL(of_phy_provider_unregister); 1346 1347 /** 1348 * devm_of_phy_provider_unregister() - remove phy provider from the framework 1349 * @dev: struct device of the phy provider 1350 * @phy_provider: phy provider returned by of_phy_provider_register() 1351 * 1352 * destroys the devres associated with this phy provider and invokes 1353 * of_phy_provider_unregister to unregister the phy provider. 1354 */ 1355 void devm_of_phy_provider_unregister(struct device *dev, 1356 struct phy_provider *phy_provider) 1357 { 1358 int r; 1359 1360 r = devres_release(dev, devm_phy_provider_release, devm_phy_match, 1361 phy_provider); 1362 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n"); 1363 } 1364 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister); 1365 1366 /** 1367 * phy_release() - release the phy 1368 * @dev: the dev member within phy 1369 * 1370 * When the last reference to the device is removed, it is called 1371 * from the embedded kobject as release method. 1372 */ 1373 static void phy_release(struct device *dev) 1374 { 1375 struct phy *phy; 1376 1377 phy = to_phy(dev); 1378 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev)); 1379 debugfs_remove_recursive(phy->debugfs); 1380 regulator_put(phy->pwr); 1381 mutex_destroy(&phy->mutex); 1382 lockdep_unregister_key(&phy->lockdep_key); 1383 ida_free(&phy_ida, phy->id); 1384 kfree(phy); 1385 } 1386 1387 static int __init phy_core_init(void) 1388 { 1389 int err; 1390 1391 err = class_register(&phy_class); 1392 if (err) { 1393 pr_err("failed to register phy class"); 1394 return err; 1395 } 1396 1397 phy_debugfs_root = debugfs_create_dir("phy", NULL); 1398 1399 return 0; 1400 } 1401 device_initcall(phy_core_init); 1402 1403 static void __exit phy_core_exit(void) 1404 { 1405 debugfs_remove_recursive(phy_debugfs_root); 1406 class_unregister(&phy_class); 1407 } 1408 module_exit(phy_core_exit); 1409