phy-common-props.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * phy-common-props.c -- Common PHY properties
  4. *
  5. * Copyright 2025-2026 NXP
  6. */
  7. #include <linux/export.h>
  8. #include <linux/fwnode.h>
  9. #include <linux/phy/phy-common-props.h>
  10. #include <linux/printk.h>
  11. #include <linux/property.h>
  12. #include <linux/slab.h>
  13. /**
  14. * fwnode_get_u32_prop_for_name - Find u32 property by name, or default value
  15. * @fwnode: Pointer to firmware node, or NULL to use @default_val
  16. * @name: Property name used as lookup key in @names_title (must not be NULL)
  17. * @props_title: Name of u32 array property holding values
  18. * @names_title: Name of string array property holding lookup keys
  19. * @default_val: Default value if @fwnode is NULL or @props_title is empty
  20. * @val: Pointer to store the returned value
  21. *
  22. * This function retrieves a u32 value from @props_title based on a name lookup
  23. * in @names_title. The value stored in @val is determined as follows:
  24. *
  25. * - If @fwnode is NULL or @props_title is empty: @default_val is used
  26. * - If @props_title has exactly one element and @names_title is empty:
  27. * that element is used
  28. * - Otherwise: @val is set to the element at the same index where @name is
  29. * found in @names_title.
  30. * - If @name is not found, the function looks for a "default" entry in
  31. * @names_title and uses the corresponding value from @props_title
  32. *
  33. * When both @props_title and @names_title are present, they must have the
  34. * same number of elements (except when @props_title has exactly one element).
  35. *
  36. * Return: zero on success, negative error on failure.
  37. */
  38. static int fwnode_get_u32_prop_for_name(struct fwnode_handle *fwnode,
  39. const char *name,
  40. const char *props_title,
  41. const char *names_title,
  42. unsigned int default_val,
  43. unsigned int *val)
  44. {
  45. int err, n_props, n_names, idx;
  46. u32 *props;
  47. if (!name) {
  48. pr_err("Lookup key inside \"%s\" is mandatory\n", names_title);
  49. return -EINVAL;
  50. }
  51. n_props = fwnode_property_count_u32(fwnode, props_title);
  52. if (n_props <= 0) {
  53. /* fwnode is NULL, or is missing requested property */
  54. *val = default_val;
  55. return 0;
  56. }
  57. n_names = fwnode_property_string_array_count(fwnode, names_title);
  58. if (n_names >= 0 && n_props != n_names) {
  59. pr_err("%pfw mismatch between \"%s\" and \"%s\" property count (%d vs %d)\n",
  60. fwnode, props_title, names_title, n_props, n_names);
  61. return -EINVAL;
  62. }
  63. idx = fwnode_property_match_string(fwnode, names_title, name);
  64. if (idx < 0)
  65. idx = fwnode_property_match_string(fwnode, names_title, "default");
  66. /*
  67. * If the mode name is missing, it can only mean the specified property
  68. * is the default one for all modes, so reject any other property count
  69. * than 1.
  70. */
  71. if (idx < 0 && n_props != 1) {
  72. pr_err("%pfw \"%s \" property has %d elements, but cannot find \"%s\" in \"%s\" and there is no default value\n",
  73. fwnode, props_title, n_props, name, names_title);
  74. return -EINVAL;
  75. }
  76. if (n_props == 1) {
  77. err = fwnode_property_read_u32(fwnode, props_title, val);
  78. if (err)
  79. return err;
  80. return 0;
  81. }
  82. /* We implicitly know idx >= 0 here */
  83. props = kcalloc(n_props, sizeof(*props), GFP_KERNEL);
  84. if (!props)
  85. return -ENOMEM;
  86. err = fwnode_property_read_u32_array(fwnode, props_title, props, n_props);
  87. if (err >= 0)
  88. *val = props[idx];
  89. kfree(props);
  90. return err;
  91. }
  92. static int phy_get_polarity_for_mode(struct fwnode_handle *fwnode,
  93. const char *mode_name,
  94. unsigned int supported,
  95. unsigned int default_val,
  96. const char *polarity_prop,
  97. const char *names_prop,
  98. unsigned int *val)
  99. {
  100. int err;
  101. err = fwnode_get_u32_prop_for_name(fwnode, mode_name, polarity_prop,
  102. names_prop, default_val, val);
  103. if (err)
  104. return err;
  105. if (!(supported & BIT(*val))) {
  106. pr_err("%d is not a supported value for %pfw '%s' element '%s'\n",
  107. *val, fwnode, polarity_prop, mode_name);
  108. err = -EOPNOTSUPP;
  109. }
  110. return err;
  111. }
  112. /**
  113. * phy_get_rx_polarity - Get RX polarity for PHY differential lane
  114. * @fwnode: Pointer to the PHY's firmware node.
  115. * @mode_name: The name of the PHY mode to look up.
  116. * @supported: Bit mask of PHY_POL_NORMAL, PHY_POL_INVERT and PHY_POL_AUTO
  117. * @default_val: Default polarity value if property is missing
  118. * @val: Pointer to returned polarity.
  119. *
  120. * Return: zero on success, negative error on failure.
  121. */
  122. int __must_check phy_get_rx_polarity(struct fwnode_handle *fwnode,
  123. const char *mode_name,
  124. unsigned int supported,
  125. unsigned int default_val,
  126. unsigned int *val)
  127. {
  128. return phy_get_polarity_for_mode(fwnode, mode_name, supported,
  129. default_val, "rx-polarity",
  130. "rx-polarity-names", val);
  131. }
  132. EXPORT_SYMBOL_GPL(phy_get_rx_polarity);
  133. /**
  134. * phy_get_tx_polarity - Get TX polarity for PHY differential lane
  135. * @fwnode: Pointer to the PHY's firmware node.
  136. * @mode_name: The name of the PHY mode to look up.
  137. * @supported: Bit mask of PHY_POL_NORMAL and PHY_POL_INVERT
  138. * @default_val: Default polarity value if property is missing
  139. * @val: Pointer to returned polarity.
  140. *
  141. * Return: zero on success, negative error on failure.
  142. */
  143. int __must_check phy_get_tx_polarity(struct fwnode_handle *fwnode,
  144. const char *mode_name, unsigned int supported,
  145. unsigned int default_val, unsigned int *val)
  146. {
  147. return phy_get_polarity_for_mode(fwnode, mode_name, supported,
  148. default_val, "tx-polarity",
  149. "tx-polarity-names", val);
  150. }
  151. EXPORT_SYMBOL_GPL(phy_get_tx_polarity);
  152. /**
  153. * phy_get_manual_rx_polarity - Get manual RX polarity for PHY differential lane
  154. * @fwnode: Pointer to the PHY's firmware node.
  155. * @mode_name: The name of the PHY mode to look up.
  156. * @val: Pointer to returned polarity.
  157. *
  158. * Helper for PHYs which do not support protocols with automatic RX polarity
  159. * detection and correction.
  160. *
  161. * Return: zero on success, negative error on failure.
  162. */
  163. int __must_check phy_get_manual_rx_polarity(struct fwnode_handle *fwnode,
  164. const char *mode_name,
  165. unsigned int *val)
  166. {
  167. return phy_get_rx_polarity(fwnode, mode_name,
  168. BIT(PHY_POL_NORMAL) | BIT(PHY_POL_INVERT),
  169. PHY_POL_NORMAL, val);
  170. }
  171. EXPORT_SYMBOL_GPL(phy_get_manual_rx_polarity);
  172. /**
  173. * phy_get_manual_tx_polarity - Get manual TX polarity for PHY differential lane
  174. * @fwnode: Pointer to the PHY's firmware node.
  175. * @mode_name: The name of the PHY mode to look up.
  176. * @val: Pointer to returned polarity.
  177. *
  178. * Helper for PHYs without any custom default value for the TX polarity.
  179. *
  180. * Return: zero on success, negative error on failure.
  181. */
  182. int __must_check phy_get_manual_tx_polarity(struct fwnode_handle *fwnode,
  183. const char *mode_name,
  184. unsigned int *val)
  185. {
  186. return phy_get_tx_polarity(fwnode, mode_name,
  187. BIT(PHY_POL_NORMAL) | BIT(PHY_POL_INVERT),
  188. PHY_POL_NORMAL, val);
  189. }
  190. EXPORT_SYMBOL_GPL(phy_get_manual_tx_polarity);