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