phy-uclass.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
  4. * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <generic-phy.h>
  9. static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
  10. {
  11. return (struct phy_ops *)dev->driver->ops;
  12. }
  13. static int generic_phy_xlate_offs_flags(struct phy *phy,
  14. struct ofnode_phandle_args *args)
  15. {
  16. debug("%s(phy=%p)\n", __func__, phy);
  17. if (args->args_count > 1) {
  18. debug("Invaild args_count: %d\n", args->args_count);
  19. return -EINVAL;
  20. }
  21. if (args->args_count)
  22. phy->id = args->args[0];
  23. else
  24. phy->id = 0;
  25. return 0;
  26. }
  27. int generic_phy_get_by_index(struct udevice *dev, int index,
  28. struct phy *phy)
  29. {
  30. struct ofnode_phandle_args args;
  31. struct phy_ops *ops;
  32. int ret;
  33. struct udevice *phydev;
  34. debug("%s(dev=%p, index=%d, phy=%p)\n", __func__, dev, index, phy);
  35. assert(phy);
  36. phy->dev = NULL;
  37. ret = dev_read_phandle_with_args(dev, "phys", "#phy-cells", 0, index,
  38. &args);
  39. if (ret) {
  40. debug("%s: dev_read_phandle_with_args failed: err=%d\n",
  41. __func__, ret);
  42. return ret;
  43. }
  44. ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
  45. if (ret) {
  46. debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
  47. __func__, ret);
  48. return ret;
  49. }
  50. phy->dev = phydev;
  51. ops = phy_dev_ops(phydev);
  52. if (ops->of_xlate)
  53. ret = ops->of_xlate(phy, &args);
  54. else
  55. ret = generic_phy_xlate_offs_flags(phy, &args);
  56. if (ret) {
  57. debug("of_xlate() failed: %d\n", ret);
  58. goto err;
  59. }
  60. return 0;
  61. err:
  62. return ret;
  63. }
  64. int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
  65. struct phy *phy)
  66. {
  67. int index;
  68. debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
  69. index = dev_read_stringlist_search(dev, "phy-names", phy_name);
  70. if (index < 0) {
  71. debug("dev_read_stringlist_search() failed: %d\n", index);
  72. return index;
  73. }
  74. return generic_phy_get_by_index(dev, index, phy);
  75. }
  76. int generic_phy_init(struct phy *phy)
  77. {
  78. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  79. return ops->init ? ops->init(phy) : 0;
  80. }
  81. int generic_phy_reset(struct phy *phy)
  82. {
  83. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  84. return ops->reset ? ops->reset(phy) : 0;
  85. }
  86. int generic_phy_exit(struct phy *phy)
  87. {
  88. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  89. return ops->exit ? ops->exit(phy) : 0;
  90. }
  91. int generic_phy_power_on(struct phy *phy)
  92. {
  93. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  94. return ops->power_on ? ops->power_on(phy) : 0;
  95. }
  96. int generic_phy_power_off(struct phy *phy)
  97. {
  98. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  99. return ops->power_off ? ops->power_off(phy) : 0;
  100. }
  101. UCLASS_DRIVER(phy) = {
  102. .id = UCLASS_PHY,
  103. .name = "phy",
  104. };