of_mdio.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * OF helpers for the MDIO (Ethernet PHY) API
  4. *
  5. * Copyright (c) 2009 Secret Lab Technologies, Ltd.
  6. */
  7. #ifndef __LINUX_OF_MDIO_H
  8. #define __LINUX_OF_MDIO_H
  9. #include <linux/device.h>
  10. #include <linux/phy.h>
  11. #include <linux/of.h>
  12. #if IS_ENABLED(CONFIG_OF_MDIO)
  13. bool of_mdiobus_child_is_phy(struct device_node *child);
  14. int __of_mdiobus_register(struct mii_bus *mdio, struct device_node *np,
  15. struct module *owner);
  16. static inline int of_mdiobus_register(struct mii_bus *mdio,
  17. struct device_node *np)
  18. {
  19. return __of_mdiobus_register(mdio, np, THIS_MODULE);
  20. }
  21. int __devm_of_mdiobus_register(struct device *dev, struct mii_bus *mdio,
  22. struct device_node *np, struct module *owner);
  23. static inline int devm_of_mdiobus_register(struct device *dev,
  24. struct mii_bus *mdio,
  25. struct device_node *np)
  26. {
  27. return __devm_of_mdiobus_register(dev, mdio, np, THIS_MODULE);
  28. }
  29. struct mdio_device *of_mdio_find_device(struct device_node *np);
  30. struct phy_device *of_phy_find_device(struct device_node *phy_np);
  31. struct phy_device *
  32. of_phy_connect(struct net_device *dev, struct device_node *phy_np,
  33. void (*hndlr)(struct net_device *), u32 flags,
  34. phy_interface_t iface);
  35. struct phy_device *
  36. of_phy_get_and_connect(struct net_device *dev, struct device_node *np,
  37. void (*hndlr)(struct net_device *));
  38. struct mii_bus *of_mdio_find_bus(struct device_node *mdio_np);
  39. int of_phy_register_fixed_link(struct device_node *np);
  40. void of_phy_deregister_fixed_link(struct device_node *np);
  41. bool of_phy_is_fixed_link(struct device_node *np);
  42. int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
  43. struct device_node *child, u32 addr);
  44. static inline int of_mdio_parse_addr(struct device *dev,
  45. const struct device_node *np)
  46. {
  47. u32 addr;
  48. int ret;
  49. ret = of_property_read_u32(np, "reg", &addr);
  50. if (ret < 0) {
  51. dev_err(dev, "%s has invalid PHY address\n", np->full_name);
  52. return ret;
  53. }
  54. /* A PHY must have a reg property in the range [0-31] */
  55. if (addr >= PHY_MAX_ADDR) {
  56. dev_err(dev, "%s PHY address %i is too large\n",
  57. np->full_name, addr);
  58. return -EINVAL;
  59. }
  60. return addr;
  61. }
  62. #else /* CONFIG_OF_MDIO */
  63. static inline bool of_mdiobus_child_is_phy(struct device_node *child)
  64. {
  65. return false;
  66. }
  67. static inline int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  68. {
  69. /*
  70. * Fall back to the non-DT function to register a bus.
  71. * This way, we don't have to keep compat bits around in drivers.
  72. */
  73. return mdiobus_register(mdio);
  74. }
  75. static inline int devm_of_mdiobus_register(struct device *dev,
  76. struct mii_bus *mdio,
  77. struct device_node *np)
  78. {
  79. return devm_mdiobus_register(dev, mdio);
  80. }
  81. static inline struct mdio_device *of_mdio_find_device(struct device_node *np)
  82. {
  83. return NULL;
  84. }
  85. static inline struct phy_device *of_phy_find_device(struct device_node *phy_np)
  86. {
  87. return NULL;
  88. }
  89. static inline struct phy_device *of_phy_connect(struct net_device *dev,
  90. struct device_node *phy_np,
  91. void (*hndlr)(struct net_device *),
  92. u32 flags, phy_interface_t iface)
  93. {
  94. return NULL;
  95. }
  96. static inline struct phy_device *
  97. of_phy_get_and_connect(struct net_device *dev, struct device_node *np,
  98. void (*hndlr)(struct net_device *))
  99. {
  100. return NULL;
  101. }
  102. static inline struct mii_bus *of_mdio_find_bus(struct device_node *mdio_np)
  103. {
  104. return NULL;
  105. }
  106. static inline int of_mdio_parse_addr(struct device *dev,
  107. const struct device_node *np)
  108. {
  109. return -ENOSYS;
  110. }
  111. static inline int of_phy_register_fixed_link(struct device_node *np)
  112. {
  113. return -ENOSYS;
  114. }
  115. static inline void of_phy_deregister_fixed_link(struct device_node *np)
  116. {
  117. }
  118. static inline bool of_phy_is_fixed_link(struct device_node *np)
  119. {
  120. return false;
  121. }
  122. static inline int of_mdiobus_phy_device_register(struct mii_bus *mdio,
  123. struct phy_device *phy,
  124. struct device_node *child, u32 addr)
  125. {
  126. return -ENOSYS;
  127. }
  128. #endif
  129. #endif /* __LINUX_OF_MDIO_H */