1 // SPDX-License-Identifier: GPL-2.0
3 * Kunit tests for clk fractional divider
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
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
);
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
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
);
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
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
;
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
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
;
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
,
145 &clk_fd_approximation_suite
147 MODULE_DESCRIPTION("Kunit tests for clk fractional divider");
148 MODULE_LICENSE("GPL");