phy-common-props-test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phy-common-props-test.c -- Unit tests for PHY common properties API
  4. *
  5. * Copyright 2025-2026 NXP
  6. */
  7. #include <kunit/test.h>
  8. #include <linux/property.h>
  9. #include <linux/phy/phy-common-props.h>
  10. #include <dt-bindings/phy/phy.h>
  11. /* Test: rx-polarity property is missing */
  12. static void phy_test_rx_polarity_is_missing(struct kunit *test)
  13. {
  14. static const struct property_entry entries[] = {
  15. {}
  16. };
  17. struct fwnode_handle *node;
  18. unsigned int val;
  19. int ret;
  20. node = fwnode_create_software_node(entries, NULL);
  21. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  22. ret = phy_get_manual_rx_polarity(node, "sgmii", &val);
  23. KUNIT_EXPECT_EQ(test, ret, 0);
  24. KUNIT_EXPECT_EQ(test, val, PHY_POL_NORMAL);
  25. fwnode_remove_software_node(node);
  26. }
  27. /* Test: rx-polarity has more values than rx-polarity-names */
  28. static void phy_test_rx_polarity_more_values_than_names(struct kunit *test)
  29. {
  30. static const u32 rx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT, PHY_POL_NORMAL };
  31. static const char * const rx_pol_names[] = { "sgmii", "2500base-x" };
  32. static const struct property_entry entries[] = {
  33. PROPERTY_ENTRY_U32_ARRAY("rx-polarity", rx_pol),
  34. PROPERTY_ENTRY_STRING_ARRAY("rx-polarity-names", rx_pol_names),
  35. {}
  36. };
  37. struct fwnode_handle *node;
  38. unsigned int val;
  39. int ret;
  40. node = fwnode_create_software_node(entries, NULL);
  41. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  42. ret = phy_get_manual_rx_polarity(node, "sgmii", &val);
  43. KUNIT_EXPECT_EQ(test, ret, -EINVAL);
  44. fwnode_remove_software_node(node);
  45. }
  46. /* Test: rx-polarity has 1 value and rx-polarity-names does not exist */
  47. static void phy_test_rx_polarity_single_value_no_names(struct kunit *test)
  48. {
  49. static const u32 rx_pol[] = { PHY_POL_INVERT };
  50. static const struct property_entry entries[] = {
  51. PROPERTY_ENTRY_U32_ARRAY("rx-polarity", rx_pol),
  52. {}
  53. };
  54. struct fwnode_handle *node;
  55. unsigned int val;
  56. int ret;
  57. node = fwnode_create_software_node(entries, NULL);
  58. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  59. ret = phy_get_manual_rx_polarity(node, "sgmii", &val);
  60. KUNIT_EXPECT_EQ(test, ret, 0);
  61. KUNIT_EXPECT_EQ(test, val, PHY_POL_INVERT);
  62. fwnode_remove_software_node(node);
  63. }
  64. /* Test: rx-polarity-names has more values than rx-polarity */
  65. static void phy_test_rx_polarity_more_names_than_values(struct kunit *test)
  66. {
  67. static const u32 rx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT };
  68. static const char * const rx_pol_names[] = { "sgmii", "2500base-x", "1000base-x" };
  69. static const struct property_entry entries[] = {
  70. PROPERTY_ENTRY_U32_ARRAY("rx-polarity", rx_pol),
  71. PROPERTY_ENTRY_STRING_ARRAY("rx-polarity-names", rx_pol_names),
  72. {}
  73. };
  74. struct fwnode_handle *node;
  75. unsigned int val;
  76. int ret;
  77. node = fwnode_create_software_node(entries, NULL);
  78. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  79. ret = phy_get_manual_rx_polarity(node, "sgmii", &val);
  80. KUNIT_EXPECT_EQ(test, ret, -EINVAL);
  81. fwnode_remove_software_node(node);
  82. }
  83. /* Test: rx-polarity and rx-polarity-names have same length, find the name */
  84. static void phy_test_rx_polarity_find_by_name(struct kunit *test)
  85. {
  86. static const u32 rx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT, PHY_POL_AUTO };
  87. static const char * const rx_pol_names[] = { "sgmii", "2500base-x", "usb-ss" };
  88. static const struct property_entry entries[] = {
  89. PROPERTY_ENTRY_U32_ARRAY("rx-polarity", rx_pol),
  90. PROPERTY_ENTRY_STRING_ARRAY("rx-polarity-names", rx_pol_names),
  91. {}
  92. };
  93. struct fwnode_handle *node;
  94. unsigned int val;
  95. int ret;
  96. node = fwnode_create_software_node(entries, NULL);
  97. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  98. ret = phy_get_manual_rx_polarity(node, "sgmii", &val);
  99. KUNIT_EXPECT_EQ(test, ret, 0);
  100. KUNIT_EXPECT_EQ(test, val, PHY_POL_NORMAL);
  101. ret = phy_get_manual_rx_polarity(node, "2500base-x", &val);
  102. KUNIT_EXPECT_EQ(test, ret, 0);
  103. KUNIT_EXPECT_EQ(test, val, PHY_POL_INVERT);
  104. ret = phy_get_rx_polarity(node, "usb-ss", BIT(PHY_POL_AUTO),
  105. PHY_POL_AUTO, &val);
  106. KUNIT_EXPECT_EQ(test, ret, 0);
  107. KUNIT_EXPECT_EQ(test, val, PHY_POL_AUTO);
  108. fwnode_remove_software_node(node);
  109. }
  110. /* Test: same length, name not found, no "default" - error */
  111. static void phy_test_rx_polarity_name_not_found_no_default(struct kunit *test)
  112. {
  113. static const u32 rx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT };
  114. static const char * const rx_pol_names[] = { "2500base-x", "1000base-x" };
  115. static const struct property_entry entries[] = {
  116. PROPERTY_ENTRY_U32_ARRAY("rx-polarity", rx_pol),
  117. PROPERTY_ENTRY_STRING_ARRAY("rx-polarity-names", rx_pol_names),
  118. {}
  119. };
  120. struct fwnode_handle *node;
  121. unsigned int val;
  122. int ret;
  123. node = fwnode_create_software_node(entries, NULL);
  124. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  125. ret = phy_get_manual_rx_polarity(node, "sgmii", &val);
  126. KUNIT_EXPECT_EQ(test, ret, -EINVAL);
  127. fwnode_remove_software_node(node);
  128. }
  129. /* Test: same length, name not found, but "default" exists */
  130. static void phy_test_rx_polarity_name_not_found_with_default(struct kunit *test)
  131. {
  132. static const u32 rx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT };
  133. static const char * const rx_pol_names[] = { "2500base-x", "default" };
  134. static const struct property_entry entries[] = {
  135. PROPERTY_ENTRY_U32_ARRAY("rx-polarity", rx_pol),
  136. PROPERTY_ENTRY_STRING_ARRAY("rx-polarity-names", rx_pol_names),
  137. {}
  138. };
  139. struct fwnode_handle *node;
  140. unsigned int val;
  141. int ret;
  142. node = fwnode_create_software_node(entries, NULL);
  143. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  144. ret = phy_get_manual_rx_polarity(node, "sgmii", &val);
  145. KUNIT_EXPECT_EQ(test, ret, 0);
  146. KUNIT_EXPECT_EQ(test, val, PHY_POL_INVERT);
  147. fwnode_remove_software_node(node);
  148. }
  149. /* Test: polarity found but value is unsupported */
  150. static void phy_test_rx_polarity_unsupported_value(struct kunit *test)
  151. {
  152. static const u32 rx_pol[] = { PHY_POL_AUTO };
  153. static const char * const rx_pol_names[] = { "sgmii" };
  154. static const struct property_entry entries[] = {
  155. PROPERTY_ENTRY_U32_ARRAY("rx-polarity", rx_pol),
  156. PROPERTY_ENTRY_STRING_ARRAY("rx-polarity-names", rx_pol_names),
  157. {}
  158. };
  159. struct fwnode_handle *node;
  160. unsigned int val;
  161. int ret;
  162. node = fwnode_create_software_node(entries, NULL);
  163. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  164. ret = phy_get_manual_rx_polarity(node, "sgmii", &val);
  165. KUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP);
  166. fwnode_remove_software_node(node);
  167. }
  168. /* Test: tx-polarity property is missing */
  169. static void phy_test_tx_polarity_is_missing(struct kunit *test)
  170. {
  171. static const struct property_entry entries[] = {
  172. {}
  173. };
  174. struct fwnode_handle *node;
  175. unsigned int val;
  176. int ret;
  177. node = fwnode_create_software_node(entries, NULL);
  178. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  179. ret = phy_get_manual_tx_polarity(node, "sgmii", &val);
  180. KUNIT_EXPECT_EQ(test, ret, 0);
  181. KUNIT_EXPECT_EQ(test, val, PHY_POL_NORMAL);
  182. fwnode_remove_software_node(node);
  183. }
  184. /* Test: tx-polarity has more values than tx-polarity-names */
  185. static void phy_test_tx_polarity_more_values_than_names(struct kunit *test)
  186. {
  187. static const u32 tx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT, PHY_POL_NORMAL };
  188. static const char * const tx_pol_names[] = { "sgmii", "2500base-x" };
  189. static const struct property_entry entries[] = {
  190. PROPERTY_ENTRY_U32_ARRAY("tx-polarity", tx_pol),
  191. PROPERTY_ENTRY_STRING_ARRAY("tx-polarity-names", tx_pol_names),
  192. {}
  193. };
  194. struct fwnode_handle *node;
  195. unsigned int val;
  196. int ret;
  197. node = fwnode_create_software_node(entries, NULL);
  198. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  199. ret = phy_get_manual_tx_polarity(node, "sgmii", &val);
  200. KUNIT_EXPECT_EQ(test, ret, -EINVAL);
  201. fwnode_remove_software_node(node);
  202. }
  203. /* Test: tx-polarity has 1 value and tx-polarity-names does not exist */
  204. static void phy_test_tx_polarity_single_value_no_names(struct kunit *test)
  205. {
  206. static const u32 tx_pol[] = { PHY_POL_INVERT };
  207. static const struct property_entry entries[] = {
  208. PROPERTY_ENTRY_U32_ARRAY("tx-polarity", tx_pol),
  209. {}
  210. };
  211. struct fwnode_handle *node;
  212. unsigned int val;
  213. int ret;
  214. node = fwnode_create_software_node(entries, NULL);
  215. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  216. ret = phy_get_manual_tx_polarity(node, "sgmii", &val);
  217. KUNIT_EXPECT_EQ(test, ret, 0);
  218. KUNIT_EXPECT_EQ(test, val, PHY_POL_INVERT);
  219. fwnode_remove_software_node(node);
  220. }
  221. /* Test: tx-polarity-names has more values than tx-polarity */
  222. static void phy_test_tx_polarity_more_names_than_values(struct kunit *test)
  223. {
  224. static const u32 tx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT };
  225. static const char * const tx_pol_names[] = { "sgmii", "2500base-x", "1000base-x" };
  226. static const struct property_entry entries[] = {
  227. PROPERTY_ENTRY_U32_ARRAY("tx-polarity", tx_pol),
  228. PROPERTY_ENTRY_STRING_ARRAY("tx-polarity-names", tx_pol_names),
  229. {}
  230. };
  231. struct fwnode_handle *node;
  232. unsigned int val;
  233. int ret;
  234. node = fwnode_create_software_node(entries, NULL);
  235. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  236. ret = phy_get_manual_tx_polarity(node, "sgmii", &val);
  237. KUNIT_EXPECT_EQ(test, ret, -EINVAL);
  238. fwnode_remove_software_node(node);
  239. }
  240. /* Test: tx-polarity and tx-polarity-names have same length, find the name */
  241. static void phy_test_tx_polarity_find_by_name(struct kunit *test)
  242. {
  243. static const u32 tx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT, PHY_POL_NORMAL };
  244. static const char * const tx_pol_names[] = { "sgmii", "2500base-x", "1000base-x" };
  245. static const struct property_entry entries[] = {
  246. PROPERTY_ENTRY_U32_ARRAY("tx-polarity", tx_pol),
  247. PROPERTY_ENTRY_STRING_ARRAY("tx-polarity-names", tx_pol_names),
  248. {}
  249. };
  250. struct fwnode_handle *node;
  251. unsigned int val;
  252. int ret;
  253. node = fwnode_create_software_node(entries, NULL);
  254. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  255. ret = phy_get_manual_tx_polarity(node, "sgmii", &val);
  256. KUNIT_EXPECT_EQ(test, ret, 0);
  257. KUNIT_EXPECT_EQ(test, val, PHY_POL_NORMAL);
  258. ret = phy_get_manual_tx_polarity(node, "2500base-x", &val);
  259. KUNIT_EXPECT_EQ(test, ret, 0);
  260. KUNIT_EXPECT_EQ(test, val, PHY_POL_INVERT);
  261. ret = phy_get_manual_tx_polarity(node, "1000base-x", &val);
  262. KUNIT_EXPECT_EQ(test, ret, 0);
  263. KUNIT_EXPECT_EQ(test, val, PHY_POL_NORMAL);
  264. fwnode_remove_software_node(node);
  265. }
  266. /* Test: same length, name not found, no "default" - error */
  267. static void phy_test_tx_polarity_name_not_found_no_default(struct kunit *test)
  268. {
  269. static const u32 tx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT };
  270. static const char * const tx_pol_names[] = { "2500base-x", "1000base-x" };
  271. static const struct property_entry entries[] = {
  272. PROPERTY_ENTRY_U32_ARRAY("tx-polarity", tx_pol),
  273. PROPERTY_ENTRY_STRING_ARRAY("tx-polarity-names", tx_pol_names),
  274. {}
  275. };
  276. struct fwnode_handle *node;
  277. unsigned int val;
  278. int ret;
  279. node = fwnode_create_software_node(entries, NULL);
  280. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  281. ret = phy_get_manual_tx_polarity(node, "sgmii", &val);
  282. KUNIT_EXPECT_EQ(test, ret, -EINVAL);
  283. fwnode_remove_software_node(node);
  284. }
  285. /* Test: same length, name not found, but "default" exists */
  286. static void phy_test_tx_polarity_name_not_found_with_default(struct kunit *test)
  287. {
  288. static const u32 tx_pol[] = { PHY_POL_NORMAL, PHY_POL_INVERT };
  289. static const char * const tx_pol_names[] = { "2500base-x", "default" };
  290. static const struct property_entry entries[] = {
  291. PROPERTY_ENTRY_U32_ARRAY("tx-polarity", tx_pol),
  292. PROPERTY_ENTRY_STRING_ARRAY("tx-polarity-names", tx_pol_names),
  293. {}
  294. };
  295. struct fwnode_handle *node;
  296. unsigned int val;
  297. int ret;
  298. node = fwnode_create_software_node(entries, NULL);
  299. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  300. ret = phy_get_manual_tx_polarity(node, "sgmii", &val);
  301. KUNIT_EXPECT_EQ(test, ret, 0);
  302. KUNIT_EXPECT_EQ(test, val, PHY_POL_INVERT);
  303. fwnode_remove_software_node(node);
  304. }
  305. /* Test: polarity found but value is unsupported (AUTO for TX) */
  306. static void phy_test_tx_polarity_unsupported_value(struct kunit *test)
  307. {
  308. static const u32 tx_pol[] = { PHY_POL_AUTO };
  309. static const char * const tx_pol_names[] = { "sgmii" };
  310. static const struct property_entry entries[] = {
  311. PROPERTY_ENTRY_U32_ARRAY("tx-polarity", tx_pol),
  312. PROPERTY_ENTRY_STRING_ARRAY("tx-polarity-names", tx_pol_names),
  313. {}
  314. };
  315. struct fwnode_handle *node;
  316. unsigned int val;
  317. int ret;
  318. node = fwnode_create_software_node(entries, NULL);
  319. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  320. ret = phy_get_manual_tx_polarity(node, "sgmii", &val);
  321. KUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP);
  322. fwnode_remove_software_node(node);
  323. }
  324. static struct kunit_case phy_common_props_test_cases[] = {
  325. KUNIT_CASE(phy_test_rx_polarity_is_missing),
  326. KUNIT_CASE(phy_test_rx_polarity_more_values_than_names),
  327. KUNIT_CASE(phy_test_rx_polarity_single_value_no_names),
  328. KUNIT_CASE(phy_test_rx_polarity_more_names_than_values),
  329. KUNIT_CASE(phy_test_rx_polarity_find_by_name),
  330. KUNIT_CASE(phy_test_rx_polarity_name_not_found_no_default),
  331. KUNIT_CASE(phy_test_rx_polarity_name_not_found_with_default),
  332. KUNIT_CASE(phy_test_rx_polarity_unsupported_value),
  333. KUNIT_CASE(phy_test_tx_polarity_is_missing),
  334. KUNIT_CASE(phy_test_tx_polarity_more_values_than_names),
  335. KUNIT_CASE(phy_test_tx_polarity_single_value_no_names),
  336. KUNIT_CASE(phy_test_tx_polarity_more_names_than_values),
  337. KUNIT_CASE(phy_test_tx_polarity_find_by_name),
  338. KUNIT_CASE(phy_test_tx_polarity_name_not_found_no_default),
  339. KUNIT_CASE(phy_test_tx_polarity_name_not_found_with_default),
  340. KUNIT_CASE(phy_test_tx_polarity_unsupported_value),
  341. {}
  342. };
  343. static struct kunit_suite phy_common_props_test_suite = {
  344. .name = "phy-common-props",
  345. .test_cases = phy_common_props_test_cases,
  346. };
  347. kunit_test_suite(phy_common_props_test_suite);
  348. MODULE_DESCRIPTION("Test module for PHY common properties API");
  349. MODULE_AUTHOR("Vladimir Oltean <vladimir.oltean@nxp.com>");
  350. MODULE_LICENSE("GPL");