summaryrefslogtreecommitdiffstats
path: root/drivers/clk/clk-fractional-divider_test.c
blob: 929eec92754806b95950a88c65c165ad840a6b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-License-Identifier: GPL-2.0
/*
 * Kunit test for clock fractional divider
 */
#include <linux/clk-provider.h>
#include <kunit/test.h>

#include "clk-fractional-divider.h"

/*
 * Test the maximum denominator case for fd clock without flags.
 *
 * Expect the highest possible denominator to be used in order to get as close as possible to the
 * requested rate.
 */
static void clk_fd_test_approximation_max_denominator(struct kunit *test)
{
	struct clk_fractional_divider *fd;
	unsigned long rate, parent_rate, parent_rate_before, m, n, max_n;

	fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fd);

	fd->mwidth = 3;
	fd->nwidth = 3;
	max_n = 7;

	rate = 240000000;
	parent_rate = (max_n + 1) * rate; /* so that it exceeds the maximum divisor */
	parent_rate_before = parent_rate;

	clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
	KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);

	KUNIT_EXPECT_EQ(test, m, 1);
	KUNIT_EXPECT_EQ(test, n, max_n);
}

/*
 * Test the maximum numerator case for fd clock without flags.
 *
 * Expect the highest possible numerator to be used in order to get as close as possible to the
 * requested rate.
 */
static void clk_fd_test_approximation_max_numerator(struct kunit *test)
{
	struct clk_fractional_divider *fd;
	unsigned long rate, parent_rate, parent_rate_before, m, n, max_m;

	fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fd);

	fd->mwidth = 3;
	max_m = 7;
	fd->nwidth = 3;

	rate = 240000000;
	parent_rate = rate / (max_m + 1); /* so that it exceeds the maximum numerator */
	parent_rate_before = parent_rate;

	clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
	KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);

	KUNIT_EXPECT_EQ(test, m, max_m);
	KUNIT_EXPECT_EQ(test, n, 1);
}

/*
 * Test the maximum denominator case for zero based fd clock.
 *
 * Expect the highest possible denominator to be used in order to get as close as possible to the
 * requested rate.
 */
static void clk_fd_test_approximation_max_denominator_zero_based(struct kunit *test)
{
	struct clk_fractional_divider *fd;
	unsigned long rate, parent_rate, parent_rate_before, m, n, max_n;

	fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fd);

	fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
	fd->mwidth = 3;
	fd->nwidth = 3;
	max_n = 8;

	rate = 240000000;
	parent_rate = (max_n + 1) * rate; /* so that it exceeds the maximum divisor */
	parent_rate_before = parent_rate;

	clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
	KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);

	KUNIT_EXPECT_EQ(test, m, 1);
	KUNIT_EXPECT_EQ(test, n, max_n);
}

/*
 * Test the maximum numerator case for zero based fd clock.
 *
 * Expect the highest possible numerator to be used in order to get as close as possible to the
 * requested rate.
 */
static void clk_fd_test_approximation_max_numerator_zero_based(struct kunit *test)
{
	struct clk_fractional_divider *fd;
	unsigned long rate, parent_rate, parent_rate_before, m, n, max_m;

	fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fd);

	fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
	fd->mwidth = 3;
	max_m = 8;
	fd->nwidth = 3;

	rate = 240000000;
	parent_rate = rate / (max_m + 1); /* so that it exceeds the maximum numerator */
	parent_rate_before = parent_rate;

	clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);
	KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);

	KUNIT_EXPECT_EQ(test, m, max_m);
	KUNIT_EXPECT_EQ(test, n, 1);
}

static struct kunit_case clk_fd_approximation_test_cases[] = {
	KUNIT_CASE(clk_fd_test_approximation_max_denominator),
	KUNIT_CASE(clk_fd_test_approximation_max_numerator),
	KUNIT_CASE(clk_fd_test_approximation_max_denominator_zero_based),
	KUNIT_CASE(clk_fd_test_approximation_max_numerator_zero_based),
	{}
};

/*
 * Test suite for clk_fractional_divider_general_approximation().
 */
static struct kunit_suite clk_fd_approximation_suite = {
	.name = "clk-fd-approximation",
	.test_cases = clk_fd_approximation_test_cases,
};

kunit_test_suites(
	&clk_fd_approximation_suite
);
MODULE_LICENSE("GPL");