phy-uclass.c 2.7 KB

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