drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / clk / clk-fractional-divider_test.c
blob25fa35d89c1a30b54f1153e65289ebb75d86708e
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>
8 #include "clk-fractional-divider.h"
11 * Test the maximum denominator case for fd clock without flags.
13 * Expect the highest possible denominator to be used in order to get as close as possible to the
14 * requested rate.
16 static void clk_fd_test_approximation_max_denominator(struct kunit *test)
18 struct clk_fractional_divider *fd;
19 unsigned long rate, parent_rate, parent_rate_before, m, n, max_n;
21 fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
22 KUNIT_ASSERT_NOT_NULL(test, fd);
24 fd->mwidth = 3;
25 fd->nwidth = 3;
26 max_n = 7;
28 rate = 240000000;
29 parent_rate = (max_n + 1) * rate; /* so that it exceeds the maximum divisor */
30 parent_rate_before = parent_rate;
32 clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
33 KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);
35 KUNIT_EXPECT_EQ(test, m, 1);
36 KUNIT_EXPECT_EQ(test, n, max_n);
40 * Test the maximum numerator case for fd clock without flags.
42 * Expect the highest possible numerator to be used in order to get as close as possible to the
43 * requested rate.
45 static void clk_fd_test_approximation_max_numerator(struct kunit *test)
47 struct clk_fractional_divider *fd;
48 unsigned long rate, parent_rate, parent_rate_before, m, n, max_m;
50 fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
51 KUNIT_ASSERT_NOT_NULL(test, fd);
53 fd->mwidth = 3;
54 max_m = 7;
55 fd->nwidth = 3;
57 rate = 240000000;
58 parent_rate = rate / (max_m + 1); /* so that it exceeds the maximum numerator */
59 parent_rate_before = parent_rate;
61 clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
62 KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);
64 KUNIT_EXPECT_EQ(test, m, max_m);
65 KUNIT_EXPECT_EQ(test, n, 1);
69 * Test the maximum denominator case for zero based fd clock.
71 * Expect the highest possible denominator to be used in order to get as close as possible to the
72 * requested rate.
74 static void clk_fd_test_approximation_max_denominator_zero_based(struct kunit *test)
76 struct clk_fractional_divider *fd;
77 unsigned long rate, parent_rate, parent_rate_before, m, n, max_n;
79 fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
80 KUNIT_ASSERT_NOT_NULL(test, fd);
82 fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
83 fd->mwidth = 3;
84 fd->nwidth = 3;
85 max_n = 8;
87 rate = 240000000;
88 parent_rate = (max_n + 1) * rate; /* so that it exceeds the maximum divisor */
89 parent_rate_before = parent_rate;
91 clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
92 KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);
94 KUNIT_EXPECT_EQ(test, m, 1);
95 KUNIT_EXPECT_EQ(test, n, max_n);
99 * Test the maximum numerator case for zero based fd clock.
101 * Expect the highest possible numerator to be used in order to get as close as possible to the
102 * requested rate.
104 static void clk_fd_test_approximation_max_numerator_zero_based(struct kunit *test)
106 struct clk_fractional_divider *fd;
107 unsigned long rate, parent_rate, parent_rate_before, m, n, max_m;
109 fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
110 KUNIT_ASSERT_NOT_NULL(test, fd);
112 fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
113 fd->mwidth = 3;
114 max_m = 8;
115 fd->nwidth = 3;
117 rate = 240000000;
118 parent_rate = rate / (max_m + 1); /* so that it exceeds the maximum numerator */
119 parent_rate_before = parent_rate;
121 clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
122 KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);
124 KUNIT_EXPECT_EQ(test, m, max_m);
125 KUNIT_EXPECT_EQ(test, n, 1);
128 static struct kunit_case clk_fd_approximation_test_cases[] = {
129 KUNIT_CASE(clk_fd_test_approximation_max_denominator),
130 KUNIT_CASE(clk_fd_test_approximation_max_numerator),
131 KUNIT_CASE(clk_fd_test_approximation_max_denominator_zero_based),
132 KUNIT_CASE(clk_fd_test_approximation_max_numerator_zero_based),
137 * Test suite for clk_fractional_divider_general_approximation().
139 static struct kunit_suite clk_fd_approximation_suite = {
140 .name = "clk-fd-approximation",
141 .test_cases = clk_fd_approximation_test_cases,
144 kunit_test_suites(
145 &clk_fd_approximation_suite
147 MODULE_DESCRIPTION("Kunit tests for clk fractional divider");
148 MODULE_LICENSE("GPL");