phy-uclass.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. struct udevice *phydev;
  33. int i, ret;
  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. /* Check if args.node's parent is a PHY provider */
  49. ret = uclass_get_device_by_ofnode(UCLASS_PHY,
  50. ofnode_get_parent(args.node),
  51. &phydev);
  52. if (ret)
  53. return ret;
  54. /* insert phy idx at first position into args array */
  55. for (i = args.args_count; i >= 1 ; i--)
  56. args.args[i] = args.args[i - 1];
  57. args.args_count++;
  58. args.args[0] = ofnode_read_u32_default(args.node, "reg", -1);
  59. }
  60. phy->dev = phydev;
  61. ops = phy_dev_ops(phydev);
  62. if (ops->of_xlate)
  63. ret = ops->of_xlate(phy, &args);
  64. else
  65. ret = generic_phy_xlate_offs_flags(phy, &args);
  66. if (ret) {
  67. debug("of_xlate() failed: %d\n", ret);
  68. goto err;
  69. }
  70. return 0;
  71. err:
  72. return ret;
  73. }
  74. int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
  75. struct phy *phy)
  76. {
  77. int index;
  78. debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
  79. index = dev_read_stringlist_search(dev, "phy-names", phy_name);
  80. if (index < 0) {
  81. debug("dev_read_stringlist_search() failed: %d\n", index);
  82. return index;
  83. }
  84. return generic_phy_get_by_index(dev, index, phy);
  85. }
  86. int generic_phy_init(struct phy *phy)
  87. {
  88. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  89. return ops->init ? ops->init(phy) : 0;
  90. }
  91. int generic_phy_reset(struct phy *phy)
  92. {
  93. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  94. return ops->reset ? ops->reset(phy) : 0;
  95. }
  96. int generic_phy_exit(struct phy *phy)
  97. {
  98. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  99. return ops->exit ? ops->exit(phy) : 0;
  100. }
  101. int generic_phy_power_on(struct phy *phy)
  102. {
  103. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  104. return ops->power_on ? ops->power_on(phy) : 0;
  105. }
  106. int generic_phy_power_off(struct phy *phy)
  107. {
  108. struct phy_ops const *ops = phy_dev_ops(phy->dev);
  109. return ops->power_off ? ops->power_off(phy) : 0;
  110. }
  111. UCLASS_DRIVER(phy) = {
  112. .id = UCLASS_PHY,
  113. .name = "phy",
  114. };