clk-fractional-divider_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Kunit tests for clk fractional divider
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <kunit/test.h>
  7. #include "clk-fractional-divider.h"
  8. /*
  9. * Test the maximum denominator case for fd clock without flags.
  10. *
  11. * Expect the highest possible denominator to be used in order to get as close as possible to the
  12. * requested rate.
  13. */
  14. static void clk_fd_test_approximation_max_denominator(struct kunit *test)
  15. {
  16. struct clk_fractional_divider *fd;
  17. unsigned long rate, parent_rate, parent_rate_before, m, n, max_n;
  18. fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
  19. KUNIT_ASSERT_NOT_NULL(test, fd);
  20. fd->mwidth = 3;
  21. fd->nwidth = 3;
  22. max_n = 7;
  23. rate = 240000000;
  24. parent_rate = (max_n + 1) * rate; /* so that it exceeds the maximum divisor */
  25. parent_rate_before = parent_rate;
  26. clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
  27. KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);
  28. KUNIT_EXPECT_EQ(test, m, 1);
  29. KUNIT_EXPECT_EQ(test, n, max_n);
  30. }
  31. /*
  32. * Test the maximum numerator case for fd clock without flags.
  33. *
  34. * Expect the highest possible numerator to be used in order to get as close as possible to the
  35. * requested rate.
  36. */
  37. static void clk_fd_test_approximation_max_numerator(struct kunit *test)
  38. {
  39. struct clk_fractional_divider *fd;
  40. unsigned long rate, parent_rate, parent_rate_before, m, n, max_m;
  41. fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
  42. KUNIT_ASSERT_NOT_NULL(test, fd);
  43. fd->mwidth = 3;
  44. max_m = 7;
  45. fd->nwidth = 3;
  46. rate = 240000000;
  47. parent_rate = rate / (max_m + 1); /* so that it exceeds the maximum numerator */
  48. parent_rate_before = parent_rate;
  49. clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
  50. KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);
  51. KUNIT_EXPECT_EQ(test, m, max_m);
  52. KUNIT_EXPECT_EQ(test, n, 1);
  53. }
  54. /*
  55. * Test the maximum denominator case for zero based fd clock.
  56. *
  57. * Expect the highest possible denominator to be used in order to get as close as possible to the
  58. * requested rate.
  59. */
  60. static void clk_fd_test_approximation_max_denominator_zero_based(struct kunit *test)
  61. {
  62. struct clk_fractional_divider *fd;
  63. unsigned long rate, parent_rate, parent_rate_before, m, n, max_n;
  64. fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
  65. KUNIT_ASSERT_NOT_NULL(test, fd);
  66. fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
  67. fd->mwidth = 3;
  68. fd->nwidth = 3;
  69. max_n = 8;
  70. rate = 240000000;
  71. parent_rate = (max_n + 1) * rate; /* so that it exceeds the maximum divisor */
  72. parent_rate_before = parent_rate;
  73. clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
  74. KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);
  75. KUNIT_EXPECT_EQ(test, m, 1);
  76. KUNIT_EXPECT_EQ(test, n, max_n);
  77. }
  78. /*
  79. * Test the maximum numerator case for zero based fd clock.
  80. *
  81. * Expect the highest possible numerator to be used in order to get as close as possible to the
  82. * requested rate.
  83. */
  84. static void clk_fd_test_approximation_max_numerator_zero_based(struct kunit *test)
  85. {
  86. struct clk_fractional_divider *fd;
  87. unsigned long rate, parent_rate, parent_rate_before, m, n, max_m;
  88. fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
  89. KUNIT_ASSERT_NOT_NULL(test, fd);
  90. fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
  91. fd->mwidth = 3;
  92. max_m = 8;
  93. fd->nwidth = 3;
  94. rate = 240000000;
  95. parent_rate = rate / (max_m + 1); /* so that it exceeds the maximum numerator */
  96. parent_rate_before = parent_rate;
  97. clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
  98. KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);
  99. KUNIT_EXPECT_EQ(test, m, max_m);
  100. KUNIT_EXPECT_EQ(test, n, 1);
  101. }
  102. static struct kunit_case clk_fd_approximation_test_cases[] = {
  103. KUNIT_CASE(clk_fd_test_approximation_max_denominator),
  104. KUNIT_CASE(clk_fd_test_approximation_max_numerator),
  105. KUNIT_CASE(clk_fd_test_approximation_max_denominator_zero_based),
  106. KUNIT_CASE(clk_fd_test_approximation_max_numerator_zero_based),
  107. {}
  108. };
  109. /*
  110. * Test suite for clk_fractional_divider_general_approximation().
  111. */
  112. static struct kunit_suite clk_fd_approximation_suite = {
  113. .name = "clk-fd-approximation",
  114. .test_cases = clk_fd_approximation_test_cases,
  115. };
  116. kunit_test_suites(
  117. &clk_fd_approximation_suite
  118. );
  119. MODULE_DESCRIPTION("Kunit tests for clk fractional divider");
  120. MODULE_LICENSE("GPL");