1 From 96da86bde649d53c634b35be715c1e499e39ccf7 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Fri, 15 Apr 2022 14:45:17 +0200
4 Subject: [PATCH] clk: tests: Add some tests for clk_get_rate_range()
6 Let's introduce a bunch of unit tests to make sure the values returned
7 by clk_get_rate_range() are sane.
9 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
11 drivers/clk/clk_test.c | 182 +++++++++++++++++++++++++++++++++++++++++
12 1 file changed, 182 insertions(+)
14 --- a/drivers/clk/clk_test.c
15 +++ b/drivers/clk/clk_test.c
16 @@ -923,6 +923,109 @@ clk_test_single_parent_mux_get_parent(st
20 + * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
21 + * if we set a range on both the child clock and its parent, with a
22 + * smaller range on the child, the rate range returned by
23 + * clk_get_rate_range() is aggregate of both ranges.
26 +clk_test_single_parent_mux_get_range_both_child_smaller(struct kunit *test)
28 + struct clk_single_parent_ctx *ctx = test->priv;
29 + struct clk_hw *hw = &ctx->hw;
30 + struct clk *clk = hw->clk;
32 + unsigned long min, max;
35 + parent = clk_get_parent(clk);
36 + KUNIT_ASSERT_PTR_NE(test, parent, NULL);
38 + ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
39 + KUNIT_ASSERT_EQ(test, ret, 0);
41 + ret = clk_set_rate_range(clk,
42 + DUMMY_CLOCK_RATE_1 + 1000,
43 + DUMMY_CLOCK_RATE_2 - 1000);
44 + KUNIT_ASSERT_EQ(test, ret, 0);
46 + clk_get_rate_range(clk, &min, &max);
47 + KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
48 + KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2 - 1000);
52 + * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
53 + * if we set a range on both the child clock and its parent, with a
54 + * smaller range on the parent, the rate range returned by
55 + * clk_get_rate_range() is aggregate of both ranges.
57 + * FIXME: clk_get_rate_range() (and clk_core_get_boundaries() in
58 + * particular) doesn't take the parent range into account when the clock
59 + * has CLK_SET_RATE_PARENT.
62 +clk_test_single_parent_mux_get_range_both_parent_smaller(struct kunit *test)
64 + struct clk_single_parent_ctx *ctx = test->priv;
65 + struct clk_hw *hw = &ctx->hw;
66 + struct clk *clk = hw->clk;
68 + unsigned long min, max;
71 + kunit_skip(test, "This needs to be fixed in the core.");
73 + parent = clk_get_parent(clk);
74 + KUNIT_ASSERT_PTR_NE(test, parent, NULL);
76 + ret = clk_set_rate_range(parent,
77 + DUMMY_CLOCK_RATE_1 + 1000,
78 + DUMMY_CLOCK_RATE_2 - 1000);
79 + KUNIT_ASSERT_EQ(test, ret, 0);
81 + ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
82 + KUNIT_ASSERT_EQ(test, ret, 0);
84 + clk_get_rate_range(clk, &min, &max);
85 + KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
86 + KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_2 - 1000);
90 + * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
91 + * if we set a range on the parent clock only, the rate range returned
92 + * by clk_get_rate_range() on the children clock matches the parent
95 + * FIXME: clk_get_rate_range() (and clk_core_get_boundaries() in
96 + * particular) doesn't take the parent range into account when the clock
97 + * has CLK_SET_RATE_PARENT.
100 +clk_test_single_parent_mux_get_range_parent_only(struct kunit *test)
102 + struct clk_single_parent_ctx *ctx = test->priv;
103 + struct clk_hw *hw = &ctx->hw;
104 + struct clk *clk = hw->clk;
105 + struct clk *parent;
106 + unsigned long min, max;
109 + kunit_skip(test, "This needs to be fixed in the core.");
111 + parent = clk_get_parent(clk);
112 + KUNIT_ASSERT_PTR_NE(test, parent, NULL);
114 + ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
115 + KUNIT_ASSERT_EQ(test, ret, 0);
117 + clk_get_rate_range(clk, &min, &max);
118 + KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
119 + KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_2);
123 * Test that for a clock with a single parent, clk_has_parent() actually
124 * reports it as a parent.
126 @@ -1099,6 +1202,9 @@ clk_test_single_parent_mux_set_range_rou
128 static struct kunit_case clk_single_parent_mux_test_cases[] = {
129 KUNIT_CASE(clk_test_single_parent_mux_get_parent),
130 + KUNIT_CASE(clk_test_single_parent_mux_get_range_both_child_smaller),
131 + KUNIT_CASE(clk_test_single_parent_mux_get_range_both_parent_smaller),
132 + KUNIT_CASE(clk_test_single_parent_mux_get_range_parent_only),
133 KUNIT_CASE(clk_test_single_parent_mux_has_parent),
134 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
135 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
136 @@ -1334,6 +1440,79 @@ clk_orphan_two_level_root_last_test_suit
140 + * Test that clk_set_rate_range() and clk_get_rate_range() are
141 + * consistent on a simple clock without any parent.
143 +static void clk_range_test_get_range(struct kunit *test)
145 + struct clk_dummy_context *ctx = test->priv;
146 + struct clk_hw *hw = &ctx->hw;
147 + struct clk *clk = hw->clk;
148 + unsigned long min, max;
151 + ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
152 + KUNIT_ASSERT_EQ(test, ret, 0);
154 + clk_get_rate_range(clk, &min, &max);
155 + KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
156 + KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2);
160 + * Test that, on a simple clock without any parent, if a rate range is
161 + * set on a clk, it's properly reported by clk_get_rate_range() on all
162 + * the clk structure of that clock.
164 +static void clk_range_test_get_range_multiple_clk(struct kunit *test)
166 + struct clk_dummy_context *ctx = test->priv;
167 + struct clk_hw *hw = &ctx->hw;
168 + struct clk *user1, *user2;
169 + unsigned long min, max;
172 + user1 = clk_hw_get_clk(hw, NULL);
173 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
175 + user2 = clk_hw_get_clk(hw, NULL);
176 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
178 + ret = clk_set_rate_range(user1,
179 + DUMMY_CLOCK_RATE_1,
180 + DUMMY_CLOCK_RATE_2);
181 + KUNIT_ASSERT_EQ(test, ret, 0);
183 + clk_get_rate_range(user2, &min, &max);
184 + KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
185 + KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2);
189 + * Test that, on a simple clock without any parent, if a rate range is
190 + * set on struct clk_hw, it's properly reported by clk_get_rate_range().
192 +static void clk_range_test_get_range_with_hw(struct kunit *test)
194 + struct clk_dummy_context *ctx = test->priv;
195 + struct clk_hw *hw = &ctx->hw;
196 + struct clk *clk = hw->clk;
197 + unsigned long min, max;
200 + clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
202 + ret = clk_set_rate_range(clk,
203 + DUMMY_CLOCK_RATE_1 + 1000,
204 + DUMMY_CLOCK_RATE_2 - 1000);
205 + KUNIT_ASSERT_EQ(test, ret, 0);
207 + clk_get_rate_range(clk, &min, &max);
208 + KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
209 + KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2 - 1000);
213 * Test that clk_set_rate_range won't return an error for a valid range
214 * and that it will make sure the rate of the clock is within the
216 @@ -1621,6 +1800,9 @@ static void clk_range_test_set_range_get
219 static struct kunit_case clk_range_test_cases[] = {
220 + KUNIT_CASE(clk_range_test_get_range),
221 + KUNIT_CASE(clk_range_test_get_range_with_hw),
222 + KUNIT_CASE(clk_range_test_get_range_multiple_clk),
223 KUNIT_CASE(clk_range_test_set_range),
224 KUNIT_CASE(clk_range_test_set_range_invalid),
225 KUNIT_CASE(clk_range_test_multiple_disjoints_range),