clk-fixed-rate_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test for clk fixed rate basic type
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/completion.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include <kunit/clk.h>
  11. #include <kunit/of.h>
  12. #include <kunit/platform_device.h>
  13. #include <kunit/resource.h>
  14. #include <kunit/test.h>
  15. #include "clk-fixed-rate_test.h"
  16. /**
  17. * struct clk_hw_fixed_rate_kunit_params - Parameters to pass to __clk_hw_register_fixed_rate()
  18. * @dev: device registering clk
  19. * @np: device_node of device registering clk
  20. * @name: name of clk
  21. * @parent_name: parent name of clk
  22. * @parent_hw: clk_hw pointer to parent of clk
  23. * @parent_data: parent_data describing parent of clk
  24. * @flags: clk framework flags
  25. * @fixed_rate: frequency of clk
  26. * @fixed_accuracy: accuracy of clk
  27. * @clk_fixed_flags: fixed rate specific clk flags
  28. */
  29. struct clk_hw_fixed_rate_kunit_params {
  30. struct device *dev;
  31. struct device_node *np;
  32. const char *name;
  33. const char *parent_name;
  34. const struct clk_hw *parent_hw;
  35. const struct clk_parent_data *parent_data;
  36. unsigned long flags;
  37. unsigned long fixed_rate;
  38. unsigned long fixed_accuracy;
  39. unsigned long clk_fixed_flags;
  40. };
  41. static int
  42. clk_hw_register_fixed_rate_kunit_init(struct kunit_resource *res, void *context)
  43. {
  44. struct clk_hw_fixed_rate_kunit_params *params = context;
  45. struct clk_hw *hw;
  46. hw = __clk_hw_register_fixed_rate(params->dev, params->np,
  47. params->name,
  48. params->parent_name,
  49. params->parent_hw,
  50. params->parent_data,
  51. params->flags,
  52. params->fixed_rate,
  53. params->fixed_accuracy,
  54. params->clk_fixed_flags,
  55. false);
  56. if (IS_ERR(hw))
  57. return PTR_ERR(hw);
  58. res->data = hw;
  59. return 0;
  60. }
  61. static void clk_hw_register_fixed_rate_kunit_exit(struct kunit_resource *res)
  62. {
  63. struct clk_hw *hw = res->data;
  64. clk_hw_unregister_fixed_rate(hw);
  65. }
  66. /**
  67. * clk_hw_register_fixed_rate_kunit() - Test managed __clk_hw_register_fixed_rate()
  68. * @test: The test context
  69. * @params: Arguments to __clk_hw_register_fixed_rate()
  70. *
  71. * Return: Registered fixed rate clk_hw or ERR_PTR on failure
  72. */
  73. static struct clk_hw *
  74. clk_hw_register_fixed_rate_kunit(struct kunit *test,
  75. struct clk_hw_fixed_rate_kunit_params *params)
  76. {
  77. struct clk_hw *hw;
  78. hw = kunit_alloc_resource(test,
  79. clk_hw_register_fixed_rate_kunit_init,
  80. clk_hw_register_fixed_rate_kunit_exit,
  81. GFP_KERNEL, params);
  82. if (!hw)
  83. return ERR_PTR(-EINVAL);
  84. return hw;
  85. }
  86. /**
  87. * clk_hw_unregister_fixed_rate_kunit() - Test managed clk_hw_unregister_fixed_rate()
  88. * @test: The test context
  89. * @hw: fixed rate clk to unregister upon test completion
  90. *
  91. * Automatically unregister @hw when @test is complete via
  92. * clk_hw_unregister_fixed_rate().
  93. *
  94. * Return: 0 on success or negative errno on failure
  95. */
  96. static int clk_hw_unregister_fixed_rate_kunit(struct kunit *test, struct clk_hw *hw)
  97. {
  98. if (!kunit_alloc_resource(test, NULL,
  99. clk_hw_register_fixed_rate_kunit_exit,
  100. GFP_KERNEL, hw))
  101. return -ENOMEM;
  102. return 0;
  103. }
  104. /*
  105. * Test that clk_get_rate() on a fixed rate clk registered with
  106. * clk_hw_register_fixed_rate() gets the proper frequency.
  107. */
  108. static void clk_fixed_rate_rate_test(struct kunit *test)
  109. {
  110. struct clk_hw *hw;
  111. struct clk *clk;
  112. const unsigned long fixed_rate = 230000;
  113. hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", NULL, 0, fixed_rate);
  114. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  115. KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
  116. clk = clk_hw_get_clk_prepared_enabled_kunit(test, hw, __func__);
  117. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
  118. KUNIT_EXPECT_EQ(test, fixed_rate, clk_get_rate(clk));
  119. }
  120. /*
  121. * Test that clk_get_accuracy() on a fixed rate clk registered via
  122. * clk_hw_register_fixed_rate_with_accuracy() gets the proper accuracy.
  123. */
  124. static void clk_fixed_rate_accuracy_test(struct kunit *test)
  125. {
  126. struct clk_hw *hw;
  127. struct clk *clk;
  128. const unsigned long fixed_accuracy = 5000;
  129. hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate",
  130. NULL, 0, 0,
  131. fixed_accuracy);
  132. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  133. KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
  134. clk = clk_hw_get_clk_kunit(test, hw, __func__);
  135. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
  136. KUNIT_EXPECT_EQ(test, fixed_accuracy, clk_get_accuracy(clk));
  137. }
  138. /* Test suite for a fixed rate clk without any parent */
  139. static struct kunit_case clk_fixed_rate_test_cases[] = {
  140. KUNIT_CASE(clk_fixed_rate_rate_test),
  141. KUNIT_CASE(clk_fixed_rate_accuracy_test),
  142. {}
  143. };
  144. static struct kunit_suite clk_fixed_rate_suite = {
  145. .name = "clk_fixed_rate",
  146. .test_cases = clk_fixed_rate_test_cases,
  147. };
  148. /*
  149. * Test that clk_get_parent() on a fixed rate clk gets the proper parent.
  150. */
  151. static void clk_fixed_rate_parent_test(struct kunit *test)
  152. {
  153. struct clk_hw *hw, *parent_hw;
  154. struct clk *expected_parent, *actual_parent;
  155. struct clk *clk;
  156. const char *parent_name = "test-fixed-rate-parent";
  157. struct clk_hw_fixed_rate_kunit_params parent_params = {
  158. .name = parent_name,
  159. };
  160. parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
  161. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
  162. KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
  163. expected_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
  164. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
  165. hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0, 0);
  166. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  167. KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
  168. clk = clk_hw_get_clk_kunit(test, hw, __func__);
  169. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
  170. actual_parent = clk_get_parent(clk);
  171. KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
  172. }
  173. /*
  174. * Test that clk_get_rate() on a fixed rate clk ignores the parent rate.
  175. */
  176. static void clk_fixed_rate_parent_rate_test(struct kunit *test)
  177. {
  178. struct clk_hw *hw, *parent_hw;
  179. struct clk *clk;
  180. const unsigned long expected_rate = 1405;
  181. const unsigned long parent_rate = 90402;
  182. const char *parent_name = "test-fixed-rate-parent";
  183. struct clk_hw_fixed_rate_kunit_params parent_params = {
  184. .name = parent_name,
  185. .fixed_rate = parent_rate,
  186. };
  187. parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
  188. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
  189. KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
  190. hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0,
  191. expected_rate);
  192. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  193. KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
  194. clk = clk_hw_get_clk_prepared_enabled_kunit(test, hw, __func__);
  195. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
  196. KUNIT_EXPECT_EQ(test, expected_rate, clk_get_rate(clk));
  197. }
  198. /*
  199. * Test that clk_get_accuracy() on a fixed rate clk ignores the parent accuracy.
  200. */
  201. static void clk_fixed_rate_parent_accuracy_test(struct kunit *test)
  202. {
  203. struct clk_hw *hw, *parent_hw;
  204. struct clk *clk;
  205. const unsigned long expected_accuracy = 900;
  206. const unsigned long parent_accuracy = 24000;
  207. const char *parent_name = "test-fixed-rate-parent";
  208. struct clk_hw_fixed_rate_kunit_params parent_params = {
  209. .name = parent_name,
  210. .fixed_accuracy = parent_accuracy,
  211. };
  212. parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
  213. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
  214. KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
  215. hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate",
  216. parent_name, 0, 0,
  217. expected_accuracy);
  218. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  219. KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
  220. clk = clk_hw_get_clk_kunit(test, hw, __func__);
  221. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
  222. KUNIT_EXPECT_EQ(test, expected_accuracy, clk_get_accuracy(clk));
  223. }
  224. /* Test suite for a fixed rate clk with a parent */
  225. static struct kunit_case clk_fixed_rate_parent_test_cases[] = {
  226. KUNIT_CASE(clk_fixed_rate_parent_test),
  227. KUNIT_CASE(clk_fixed_rate_parent_rate_test),
  228. KUNIT_CASE(clk_fixed_rate_parent_accuracy_test),
  229. {}
  230. };
  231. static struct kunit_suite clk_fixed_rate_parent_suite = {
  232. .name = "clk_fixed_rate_parent",
  233. .test_cases = clk_fixed_rate_parent_test_cases,
  234. };
  235. struct clk_fixed_rate_of_test_context {
  236. struct device *dev;
  237. struct platform_driver pdrv;
  238. struct completion probed;
  239. };
  240. static inline struct clk_fixed_rate_of_test_context *
  241. pdev_to_clk_fixed_rate_of_test_context(struct platform_device *pdev)
  242. {
  243. return container_of(to_platform_driver(pdev->dev.driver),
  244. struct clk_fixed_rate_of_test_context,
  245. pdrv);
  246. }
  247. /*
  248. * Test that of_fixed_clk_setup() registers a fixed rate clk with the proper
  249. * rate.
  250. */
  251. static void clk_fixed_rate_of_probe_test(struct kunit *test)
  252. {
  253. struct clk_fixed_rate_of_test_context *ctx = test->priv;
  254. struct device *dev = ctx->dev;
  255. struct clk *clk;
  256. clk = clk_get_kunit(test, dev, NULL);
  257. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
  258. KUNIT_ASSERT_EQ(test, 0, clk_prepare_enable_kunit(test, clk));
  259. KUNIT_EXPECT_EQ(test, TEST_FIXED_FREQUENCY, clk_get_rate(clk));
  260. }
  261. /*
  262. * Test that of_fixed_clk_setup() registers a fixed rate clk with the proper
  263. * accuracy.
  264. */
  265. static void clk_fixed_rate_of_accuracy_test(struct kunit *test)
  266. {
  267. struct clk_fixed_rate_of_test_context *ctx = test->priv;
  268. struct device *dev = ctx->dev;
  269. struct clk *clk;
  270. clk = clk_get_kunit(test, dev, NULL);
  271. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
  272. KUNIT_EXPECT_EQ(test, TEST_FIXED_ACCURACY, clk_get_accuracy(clk));
  273. }
  274. static struct kunit_case clk_fixed_rate_of_cases[] = {
  275. KUNIT_CASE(clk_fixed_rate_of_probe_test),
  276. KUNIT_CASE(clk_fixed_rate_of_accuracy_test),
  277. {}
  278. };
  279. static int clk_fixed_rate_of_test_probe(struct platform_device *pdev)
  280. {
  281. struct clk_fixed_rate_of_test_context *ctx;
  282. ctx = pdev_to_clk_fixed_rate_of_test_context(pdev);
  283. ctx->dev = &pdev->dev;
  284. complete(&ctx->probed);
  285. return 0;
  286. }
  287. static int clk_fixed_rate_of_init(struct kunit *test)
  288. {
  289. struct clk_fixed_rate_of_test_context *ctx;
  290. static const struct of_device_id match_table[] = {
  291. { .compatible = "test,single-clk-consumer" },
  292. { }
  293. };
  294. KUNIT_ASSERT_EQ(test, 0, of_overlay_apply_kunit(test, kunit_clk_fixed_rate_test));
  295. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  296. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
  297. test->priv = ctx;
  298. ctx->pdrv.probe = clk_fixed_rate_of_test_probe;
  299. ctx->pdrv.driver.of_match_table = match_table;
  300. ctx->pdrv.driver.name = __func__;
  301. ctx->pdrv.driver.owner = THIS_MODULE;
  302. init_completion(&ctx->probed);
  303. KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
  304. KUNIT_ASSERT_NE(test, 0, wait_for_completion_timeout(&ctx->probed, HZ));
  305. return 0;
  306. }
  307. static struct kunit_suite clk_fixed_rate_of_suite = {
  308. .name = "clk_fixed_rate_of",
  309. .init = clk_fixed_rate_of_init,
  310. .test_cases = clk_fixed_rate_of_cases,
  311. };
  312. kunit_test_suites(
  313. &clk_fixed_rate_suite,
  314. &clk_fixed_rate_of_suite,
  315. &clk_fixed_rate_parent_suite,
  316. );
  317. MODULE_LICENSE("GPL");
  318. MODULE_DESCRIPTION("KUnit test for clk fixed rate basic type");