716-v6.9-03-net-phy-add-devm-of_phy_package_join-helper.patch 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. From 471e8fd3afcef5a9f9089f0bd21965ad9ba35c91 Mon Sep 17 00:00:00 2001
  2. From: Christian Marangi <[email protected]>
  3. Date: Tue, 6 Feb 2024 18:31:06 +0100
  4. Subject: [PATCH 03/10] net: phy: add devm/of_phy_package_join helper
  5. Add devm/of_phy_package_join helper to join PHYs in a PHY package. These
  6. are variant of the manual phy_package_join with the difference that
  7. these will use DT nodes to derive the base_addr instead of manually
  8. passing an hardcoded value.
  9. An additional value is added in phy_package_shared, "np" to reference
  10. the PHY package node pointer in specific PHY driver probe_once and
  11. config_init_once functions to make use of additional specific properties
  12. defined in the PHY package node in DT.
  13. The np value is filled only with of_phy_package_join if a valid PHY
  14. package node is found. A valid PHY package node must have the node name
  15. set to "ethernet-phy-package".
  16. Signed-off-by: Christian Marangi <[email protected]>
  17. Reviewed-by: Andrew Lunn <[email protected]>
  18. Signed-off-by: David S. Miller <[email protected]>
  19. ---
  20. drivers/net/phy/phy_device.c | 96 ++++++++++++++++++++++++++++++++++++
  21. include/linux/phy.h | 6 +++
  22. 2 files changed, 102 insertions(+)
  23. --- a/drivers/net/phy/phy_device.c
  24. +++ b/drivers/net/phy/phy_device.c
  25. @@ -1648,6 +1648,7 @@ int phy_package_join(struct phy_device *
  26. shared->priv_size = priv_size;
  27. }
  28. shared->base_addr = base_addr;
  29. + shared->np = NULL;
  30. refcount_set(&shared->refcnt, 1);
  31. bus->shared[base_addr] = shared;
  32. } else {
  33. @@ -1671,6 +1672,63 @@ err_unlock:
  34. EXPORT_SYMBOL_GPL(phy_package_join);
  35. /**
  36. + * of_phy_package_join - join a common PHY group in PHY package
  37. + * @phydev: target phy_device struct
  38. + * @priv_size: if non-zero allocate this amount of bytes for private data
  39. + *
  40. + * This is a variant of phy_package_join for PHY package defined in DT.
  41. + *
  42. + * The parent node of the @phydev is checked as a valid PHY package node
  43. + * structure (by matching the node name "ethernet-phy-package") and the
  44. + * base_addr for the PHY package is passed to phy_package_join.
  45. + *
  46. + * With this configuration the shared struct will also have the np value
  47. + * filled to use additional DT defined properties in PHY specific
  48. + * probe_once and config_init_once PHY package OPs.
  49. + *
  50. + * Returns < 0 on error, 0 on success. Esp. calling phy_package_join()
  51. + * with the same cookie but a different priv_size is an error. Or a parent
  52. + * node is not detected or is not valid or doesn't match the expected node
  53. + * name for PHY package.
  54. + */
  55. +int of_phy_package_join(struct phy_device *phydev, size_t priv_size)
  56. +{
  57. + struct device_node *node = phydev->mdio.dev.of_node;
  58. + struct device_node *package_node;
  59. + u32 base_addr;
  60. + int ret;
  61. +
  62. + if (!node)
  63. + return -EINVAL;
  64. +
  65. + package_node = of_get_parent(node);
  66. + if (!package_node)
  67. + return -EINVAL;
  68. +
  69. + if (!of_node_name_eq(package_node, "ethernet-phy-package")) {
  70. + ret = -EINVAL;
  71. + goto exit;
  72. + }
  73. +
  74. + if (of_property_read_u32(package_node, "reg", &base_addr)) {
  75. + ret = -EINVAL;
  76. + goto exit;
  77. + }
  78. +
  79. + ret = phy_package_join(phydev, base_addr, priv_size);
  80. + if (ret)
  81. + goto exit;
  82. +
  83. + phydev->shared->np = package_node;
  84. +
  85. + return 0;
  86. +exit:
  87. + of_node_put(package_node);
  88. + return ret;
  89. +}
  90. +EXPORT_SYMBOL_GPL(of_phy_package_join);
  91. +
  92. +/**
  93. * phy_package_leave - leave a common PHY group
  94. * @phydev: target phy_device struct
  95. *
  96. @@ -1686,6 +1744,10 @@ void phy_package_leave(struct phy_device
  97. if (!shared)
  98. return;
  99. + /* Decrease the node refcount on leave if present */
  100. + if (shared->np)
  101. + of_node_put(shared->np);
  102. +
  103. if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
  104. bus->shared[shared->base_addr] = NULL;
  105. mutex_unlock(&bus->shared_lock);
  106. @@ -1739,6 +1801,40 @@ int devm_phy_package_join(struct device
  107. EXPORT_SYMBOL_GPL(devm_phy_package_join);
  108. /**
  109. + * devm_of_phy_package_join - resource managed of_phy_package_join()
  110. + * @dev: device that is registering this PHY package
  111. + * @phydev: target phy_device struct
  112. + * @priv_size: if non-zero allocate this amount of bytes for private data
  113. + *
  114. + * Managed of_phy_package_join(). Shared storage fetched by this function,
  115. + * phy_package_leave() is automatically called on driver detach. See
  116. + * of_phy_package_join() for more information.
  117. + */
  118. +int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
  119. + size_t priv_size)
  120. +{
  121. + struct phy_device **ptr;
  122. + int ret;
  123. +
  124. + ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
  125. + GFP_KERNEL);
  126. + if (!ptr)
  127. + return -ENOMEM;
  128. +
  129. + ret = of_phy_package_join(phydev, priv_size);
  130. +
  131. + if (!ret) {
  132. + *ptr = phydev;
  133. + devres_add(dev, ptr);
  134. + } else {
  135. + devres_free(ptr);
  136. + }
  137. +
  138. + return ret;
  139. +}
  140. +EXPORT_SYMBOL_GPL(devm_of_phy_package_join);
  141. +
  142. +/**
  143. * phy_detach - detach a PHY device from its network device
  144. * @phydev: target phy_device struct
  145. *
  146. --- a/include/linux/phy.h
  147. +++ b/include/linux/phy.h
  148. @@ -321,6 +321,7 @@ struct mdio_bus_stats {
  149. * struct phy_package_shared - Shared information in PHY packages
  150. * @base_addr: Base PHY address of PHY package used to combine PHYs
  151. * in one package and for offset calculation of phy_package_read/write
  152. + * @np: Pointer to the Device Node if PHY package defined in DT
  153. * @refcnt: Number of PHYs connected to this shared data
  154. * @flags: Initialization of PHY package
  155. * @priv_size: Size of the shared private data @priv
  156. @@ -332,6 +333,8 @@ struct mdio_bus_stats {
  157. */
  158. struct phy_package_shared {
  159. u8 base_addr;
  160. + /* With PHY package defined in DT this points to the PHY package node */
  161. + struct device_node *np;
  162. refcount_t refcnt;
  163. unsigned long flags;
  164. size_t priv_size;
  165. @@ -1793,9 +1796,12 @@ int phy_ethtool_set_link_ksettings(struc
  166. const struct ethtool_link_ksettings *cmd);
  167. int phy_ethtool_nway_reset(struct net_device *ndev);
  168. int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size);
  169. +int of_phy_package_join(struct phy_device *phydev, size_t priv_size);
  170. void phy_package_leave(struct phy_device *phydev);
  171. int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
  172. int base_addr, size_t priv_size);
  173. +int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
  174. + size_t priv_size);
  175. #if IS_ENABLED(CONFIG_PHYLIB)
  176. int __init mdio_bus_init(void);