bcm27xx: add support for linux v5.15
[openwrt/staging/ldir.git] / target / linux / bcm27xx / patches-5.15 / 950-0831-clk-Introduce-Kunit-Tests-for-the-framework.patch
1 From 93028ab89e186100ef28ba6d6adcc30f14fa3dc3 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Wed, 19 Jan 2022 16:22:19 +0100
4 Subject: [PATCH] clk: Introduce Kunit Tests for the framework
5
6 Let's test various parts of the rate-related clock API with the kunit
7 testing framework.
8
9 Cc: kunit-dev@googlegroups.com
10 Tested-by: Daniel Latypov <dlatypov@google.com>
11 Suggested-by: Stephen Boyd <sboyd@kernel.org>
12 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
13 ---
14 drivers/clk/.kunitconfig | 3 +
15 drivers/clk/Kconfig | 8 +
16 drivers/clk/Makefile | 1 +
17 drivers/clk/clk_test.c | 787 +++++++++++++++++++++++++++++++++++++++
18 4 files changed, 799 insertions(+)
19 create mode 100644 drivers/clk/.kunitconfig
20 create mode 100644 drivers/clk/clk_test.c
21
22 --- /dev/null
23 +++ b/drivers/clk/.kunitconfig
24 @@ -0,0 +1,3 @@
25 +CONFIG_KUNIT=y
26 +CONFIG_COMMON_CLK=y
27 +CONFIG_CLK_KUNIT_TEST=y
28 --- a/drivers/clk/Kconfig
29 +++ b/drivers/clk/Kconfig
30 @@ -427,4 +427,12 @@ source "drivers/clk/x86/Kconfig"
31 source "drivers/clk/xilinx/Kconfig"
32 source "drivers/clk/zynqmp/Kconfig"
33
34 +# Kunit test cases
35 +config CLK_KUNIT_TEST
36 + tristate "Basic Clock Framework Kunit Tests" if !KUNIT_ALL_TESTS
37 + depends on KUNIT
38 + default KUNIT_ALL_TESTS
39 + help
40 + Kunit tests for the common clock framework.
41 +
42 endif
43 --- a/drivers/clk/Makefile
44 +++ b/drivers/clk/Makefile
45 @@ -2,6 +2,7 @@
46 # common clock types
47 obj-$(CONFIG_HAVE_CLK) += clk-devres.o clk-bulk.o clkdev.o
48 obj-$(CONFIG_COMMON_CLK) += clk.o
49 +obj-$(CONFIG_CLK_KUNIT_TEST) += clk_test.o
50 obj-$(CONFIG_COMMON_CLK) += clk-divider.o
51 obj-$(CONFIG_COMMON_CLK) += clk-fixed-factor.o
52 obj-$(CONFIG_COMMON_CLK) += clk-fixed-rate.o
53 --- /dev/null
54 +++ b/drivers/clk/clk_test.c
55 @@ -0,0 +1,787 @@
56 +// SPDX-License-Identifier: GPL-2.0
57 +/*
58 + * Kunit test for clk rate management
59 + */
60 +#include <linux/clk.h>
61 +#include <linux/clk-provider.h>
62 +
63 +/* Needed for clk_hw_get_clk() */
64 +#include "clk.h"
65 +
66 +#include <kunit/test.h>
67 +
68 +#define DUMMY_CLOCK_INIT_RATE (42 * 1000 * 1000)
69 +#define DUMMY_CLOCK_RATE_1 (142 * 1000 * 1000)
70 +#define DUMMY_CLOCK_RATE_2 (242 * 1000 * 1000)
71 +
72 +struct clk_dummy_context {
73 + struct clk_hw hw;
74 + unsigned long rate;
75 +};
76 +
77 +static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw,
78 + unsigned long parent_rate)
79 +{
80 + struct clk_dummy_context *ctx =
81 + container_of(hw, struct clk_dummy_context, hw);
82 +
83 + return ctx->rate;
84 +}
85 +
86 +static int clk_dummy_determine_rate(struct clk_hw *hw,
87 + struct clk_rate_request *req)
88 +{
89 + /* Just return the same rate without modifying it */
90 + return 0;
91 +}
92 +
93 +static int clk_dummy_maximize_rate(struct clk_hw *hw,
94 + struct clk_rate_request *req)
95 +{
96 + /*
97 + * If there's a maximum set, always run the clock at the maximum
98 + * allowed.
99 + */
100 + if (req->max_rate < ULONG_MAX)
101 + req->rate = req->max_rate;
102 +
103 + return 0;
104 +}
105 +
106 +static int clk_dummy_minimize_rate(struct clk_hw *hw,
107 + struct clk_rate_request *req)
108 +{
109 + /*
110 + * If there's a minimum set, always run the clock at the minimum
111 + * allowed.
112 + */
113 + if (req->min_rate > 0)
114 + req->rate = req->min_rate;
115 +
116 + return 0;
117 +}
118 +
119 +static int clk_dummy_set_rate(struct clk_hw *hw,
120 + unsigned long rate,
121 + unsigned long parent_rate)
122 +{
123 + struct clk_dummy_context *ctx =
124 + container_of(hw, struct clk_dummy_context, hw);
125 +
126 + ctx->rate = rate;
127 + return 0;
128 +}
129 +
130 +static const struct clk_ops clk_dummy_rate_ops = {
131 + .recalc_rate = clk_dummy_recalc_rate,
132 + .determine_rate = clk_dummy_determine_rate,
133 + .set_rate = clk_dummy_set_rate,
134 +};
135 +
136 +static const struct clk_ops clk_dummy_maximize_rate_ops = {
137 + .recalc_rate = clk_dummy_recalc_rate,
138 + .determine_rate = clk_dummy_maximize_rate,
139 + .set_rate = clk_dummy_set_rate,
140 +};
141 +
142 +static const struct clk_ops clk_dummy_minimize_rate_ops = {
143 + .recalc_rate = clk_dummy_recalc_rate,
144 + .determine_rate = clk_dummy_minimize_rate,
145 + .set_rate = clk_dummy_set_rate,
146 +};
147 +
148 +static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
149 +{
150 + struct clk_dummy_context *ctx;
151 + struct clk_init_data init = { };
152 + int ret;
153 +
154 + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
155 + if (!ctx)
156 + return -ENOMEM;
157 + ctx->rate = DUMMY_CLOCK_INIT_RATE;
158 + test->priv = ctx;
159 +
160 + init.name = "test_dummy_rate";
161 + init.ops = ops;
162 + ctx->hw.init = &init;
163 +
164 + ret = clk_hw_register(NULL, &ctx->hw);
165 + if (ret)
166 + return ret;
167 +
168 + return 0;
169 +}
170 +
171 +static int clk_test_init(struct kunit *test)
172 +{
173 + return clk_test_init_with_ops(test, &clk_dummy_rate_ops);
174 +}
175 +
176 +static int clk_maximize_test_init(struct kunit *test)
177 +{
178 + return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops);
179 +}
180 +
181 +static int clk_minimize_test_init(struct kunit *test)
182 +{
183 + return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops);
184 +}
185 +
186 +static void clk_test_exit(struct kunit *test)
187 +{
188 + struct clk_dummy_context *ctx = test->priv;
189 +
190 + clk_hw_unregister(&ctx->hw);
191 +}
192 +
193 +/*
194 + * Test that the actual rate matches what is returned by clk_get_rate()
195 + */
196 +static void clk_test_get_rate(struct kunit *test)
197 +{
198 + struct clk_dummy_context *ctx = test->priv;
199 + struct clk_hw *hw = &ctx->hw;
200 + struct clk *clk = hw->clk;
201 + unsigned long rate;
202 +
203 + rate = clk_get_rate(clk);
204 + KUNIT_ASSERT_GT(test, rate, 0);
205 + KUNIT_EXPECT_EQ(test, rate, ctx->rate);
206 +}
207 +
208 +/*
209 + * Test that, after a call to clk_set_rate(), the rate returned by
210 + * clk_get_rate() matches.
211 + *
212 + * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
213 + * modify the requested rate, which is our case in clk_dummy_rate_ops.
214 + */
215 +static void clk_test_set_get_rate(struct kunit *test)
216 +{
217 + struct clk_dummy_context *ctx = test->priv;
218 + struct clk_hw *hw = &ctx->hw;
219 + struct clk *clk = hw->clk;
220 + unsigned long rate;
221 +
222 + KUNIT_ASSERT_EQ(test,
223 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
224 + 0);
225 +
226 + rate = clk_get_rate(clk);
227 + KUNIT_ASSERT_GT(test, rate, 0);
228 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
229 +}
230 +
231 +/*
232 + * Test that, after several calls to clk_set_rate(), the rate returned
233 + * by clk_get_rate() matches the last one.
234 + *
235 + * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
236 + * modify the requested rate, which is our case in clk_dummy_rate_ops.
237 + */
238 +static void clk_test_set_set_get_rate(struct kunit *test)
239 +{
240 + struct clk_dummy_context *ctx = test->priv;
241 + struct clk_hw *hw = &ctx->hw;
242 + struct clk *clk = hw->clk;
243 + unsigned long rate;
244 +
245 + KUNIT_ASSERT_EQ(test,
246 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
247 + 0);
248 +
249 + KUNIT_ASSERT_EQ(test,
250 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
251 + 0);
252 +
253 + rate = clk_get_rate(clk);
254 + KUNIT_ASSERT_GT(test, rate, 0);
255 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
256 +}
257 +
258 +/*
259 + * Test that clk_round_rate and clk_set_rate are consitent and will
260 + * return the same frequency.
261 + */
262 +static void clk_test_round_set_get_rate(struct kunit *test)
263 +{
264 + struct clk_dummy_context *ctx = test->priv;
265 + struct clk_hw *hw = &ctx->hw;
266 + struct clk *clk = hw->clk;
267 + unsigned long rounded_rate, set_rate;
268 +
269 + rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
270 + KUNIT_ASSERT_GT(test, rounded_rate, 0);
271 + KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
272 +
273 + KUNIT_ASSERT_EQ(test,
274 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
275 + 0);
276 +
277 + set_rate = clk_get_rate(clk);
278 + KUNIT_ASSERT_GT(test, set_rate, 0);
279 + KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
280 +}
281 +
282 +static struct kunit_case clk_test_cases[] = {
283 + KUNIT_CASE(clk_test_get_rate),
284 + KUNIT_CASE(clk_test_set_get_rate),
285 + KUNIT_CASE(clk_test_set_set_get_rate),
286 + KUNIT_CASE(clk_test_round_set_get_rate),
287 + {}
288 +};
289 +
290 +static struct kunit_suite clk_test_suite = {
291 + .name = "clk-test",
292 + .init = clk_test_init,
293 + .exit = clk_test_exit,
294 + .test_cases = clk_test_cases,
295 +};
296 +
297 +/*
298 + * Test that clk_set_rate_range won't return an error for a valid range
299 + * and that it will make sure the rate of the clock is within the
300 + * boundaries.
301 + */
302 +static void clk_range_test_set_range(struct kunit *test)
303 +{
304 + struct clk_dummy_context *ctx = test->priv;
305 + struct clk_hw *hw = &ctx->hw;
306 + struct clk *clk = hw->clk;
307 + unsigned long rate;
308 +
309 + KUNIT_ASSERT_EQ(test,
310 + clk_set_rate_range(clk,
311 + DUMMY_CLOCK_RATE_1,
312 + DUMMY_CLOCK_RATE_2),
313 + 0);
314 +
315 + rate = clk_get_rate(clk);
316 + KUNIT_ASSERT_GT(test, rate, 0);
317 + KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
318 + KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
319 +}
320 +
321 +/*
322 + * Test that calling clk_set_rate_range with a minimum rate higher than
323 + * the maximum rate returns an error.
324 + */
325 +static void clk_range_test_set_range_invalid(struct kunit *test)
326 +{
327 + struct clk_dummy_context *ctx = test->priv;
328 + struct clk_hw *hw = &ctx->hw;
329 + struct clk *clk = hw->clk;
330 +
331 + KUNIT_EXPECT_LT(test,
332 + clk_set_rate_range(clk,
333 + DUMMY_CLOCK_RATE_1 + 1000,
334 + DUMMY_CLOCK_RATE_1),
335 + 0);
336 +}
337 +
338 +/*
339 + * Test that users can't set multiple, disjoints, range that would be
340 + * impossible to meet.
341 + */
342 +static void clk_range_test_multiple_disjoints_range(struct kunit *test)
343 +{
344 + struct clk_dummy_context *ctx = test->priv;
345 + struct clk_hw *hw = &ctx->hw;
346 + struct clk *user1, *user2;
347 +
348 + user1 = clk_hw_get_clk(hw, NULL);
349 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
350 +
351 + user2 = clk_hw_get_clk(hw, NULL);
352 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
353 +
354 + KUNIT_ASSERT_EQ(test,
355 + clk_set_rate_range(user1, 1000, 2000),
356 + 0);
357 +
358 + KUNIT_EXPECT_LT(test,
359 + clk_set_rate_range(user2, 3000, 4000),
360 + 0);
361 +
362 + clk_put(user2);
363 + clk_put(user1);
364 +}
365 +
366 +/*
367 + * Test that if our clock has some boundaries and we try to round a rate
368 + * lower than the minimum, the returned rate won't be affected by the
369 + * boundaries.
370 + */
371 +static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
372 +{
373 + struct clk_dummy_context *ctx = test->priv;
374 + struct clk_hw *hw = &ctx->hw;
375 + struct clk *clk = hw->clk;
376 + long rate;
377 +
378 + KUNIT_ASSERT_EQ(test,
379 + clk_set_rate_range(clk,
380 + DUMMY_CLOCK_RATE_1,
381 + DUMMY_CLOCK_RATE_2),
382 + 0);
383 +
384 + rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
385 + KUNIT_ASSERT_GT(test, rate, 0);
386 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
387 +}
388 +
389 +/*
390 + * Test that if our clock has some boundaries and we try to set a rate
391 + * lower than the minimum, we'll get an error.
392 + */
393 +static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
394 +{
395 + struct clk_dummy_context *ctx = test->priv;
396 + struct clk_hw *hw = &ctx->hw;
397 + struct clk *clk = hw->clk;
398 +
399 + KUNIT_ASSERT_EQ(test,
400 + clk_set_rate_range(clk,
401 + DUMMY_CLOCK_RATE_1,
402 + DUMMY_CLOCK_RATE_2),
403 + 0);
404 +
405 + KUNIT_ASSERT_LT(test,
406 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
407 + 0);
408 +}
409 +
410 +/*
411 + * Test that if our clock has some boundaries and we try to round and
412 + * set a rate lower than the minimum, the values won't be consistent
413 + * between clk_round_rate() and clk_set_rate().
414 + */
415 +static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
416 +{
417 + struct clk_dummy_context *ctx = test->priv;
418 + struct clk_hw *hw = &ctx->hw;
419 + struct clk *clk = hw->clk;
420 + long rounded;
421 +
422 + KUNIT_ASSERT_EQ(test,
423 + clk_set_rate_range(clk,
424 + DUMMY_CLOCK_RATE_1,
425 + DUMMY_CLOCK_RATE_2),
426 + 0);
427 +
428 + rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
429 + KUNIT_ASSERT_GT(test, rounded, 0);
430 +
431 + KUNIT_EXPECT_LT(test,
432 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
433 + 0);
434 +
435 + KUNIT_EXPECT_NE(test, rounded, clk_get_rate(clk));
436 +}
437 +
438 +/*
439 + * Test that if our clock has some boundaries and we try to round a rate
440 + * higher than the maximum, the returned rate won't be affected by the
441 + * boundaries.
442 + */
443 +static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
444 +{
445 + struct clk_dummy_context *ctx = test->priv;
446 + struct clk_hw *hw = &ctx->hw;
447 + struct clk *clk = hw->clk;
448 + long rate;
449 +
450 + KUNIT_ASSERT_EQ(test,
451 + clk_set_rate_range(clk,
452 + DUMMY_CLOCK_RATE_1,
453 + DUMMY_CLOCK_RATE_2),
454 + 0);
455 +
456 + rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
457 + KUNIT_ASSERT_GT(test, rate, 0);
458 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 + 1000);
459 +}
460 +
461 +/*
462 + * Test that if our clock has some boundaries and we try to set a rate
463 + * lower than the maximum, we'll get an error.
464 + */
465 +static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
466 +{
467 + struct clk_dummy_context *ctx = test->priv;
468 + struct clk_hw *hw = &ctx->hw;
469 + struct clk *clk = hw->clk;
470 +
471 + KUNIT_ASSERT_EQ(test,
472 + clk_set_rate_range(clk,
473 + DUMMY_CLOCK_RATE_1,
474 + DUMMY_CLOCK_RATE_2),
475 + 0);
476 +
477 + KUNIT_ASSERT_LT(test,
478 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
479 + 0);
480 +}
481 +
482 +/*
483 + * Test that if our clock has some boundaries and we try to round and
484 + * set a rate higher than the maximum, the values won't be consistent
485 + * between clk_round_rate() and clk_set_rate().
486 + */
487 +static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
488 +{
489 + struct clk_dummy_context *ctx = test->priv;
490 + struct clk_hw *hw = &ctx->hw;
491 + struct clk *clk = hw->clk;
492 + long rounded;
493 +
494 + KUNIT_ASSERT_EQ(test,
495 + clk_set_rate_range(clk,
496 + DUMMY_CLOCK_RATE_1,
497 + DUMMY_CLOCK_RATE_2),
498 + 0);
499 +
500 + rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
501 + KUNIT_ASSERT_GT(test, rounded, 0);
502 +
503 + KUNIT_EXPECT_LT(test,
504 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
505 + 0);
506 +
507 + KUNIT_EXPECT_NE(test, rounded, clk_get_rate(clk));
508 +}
509 +
510 +/*
511 + * Test that if our clock has a rate lower than the minimum set by a
512 + * call to clk_set_rate_range(), the rate will be raised to match the
513 + * new minimum.
514 + *
515 + * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
516 + * modify the requested rate, which is our case in clk_dummy_rate_ops.
517 + */
518 +static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
519 +{
520 + struct clk_dummy_context *ctx = test->priv;
521 + struct clk_hw *hw = &ctx->hw;
522 + struct clk *clk = hw->clk;
523 + unsigned long rate;
524 +
525 + KUNIT_ASSERT_EQ(test,
526 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
527 + 0);
528 +
529 + KUNIT_ASSERT_EQ(test,
530 + clk_set_rate_range(clk,
531 + DUMMY_CLOCK_RATE_1,
532 + DUMMY_CLOCK_RATE_2),
533 + 0);
534 +
535 + rate = clk_get_rate(clk);
536 + KUNIT_ASSERT_GT(test, rate, 0);
537 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
538 +}
539 +
540 +/*
541 + * Test that if our clock has a rate higher than the maximum set by a
542 + * call to clk_set_rate_range(), the rate will be lowered to match the
543 + * new maximum.
544 + *
545 + * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
546 + * modify the requested rate, which is our case in clk_dummy_rate_ops.
547 + */
548 +static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
549 +{
550 + struct clk_dummy_context *ctx = test->priv;
551 + struct clk_hw *hw = &ctx->hw;
552 + struct clk *clk = hw->clk;
553 + unsigned long rate;
554 +
555 + KUNIT_ASSERT_EQ(test,
556 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
557 + 0);
558 +
559 + KUNIT_ASSERT_EQ(test,
560 + clk_set_rate_range(clk,
561 + DUMMY_CLOCK_RATE_1,
562 + DUMMY_CLOCK_RATE_2),
563 + 0);
564 +
565 + rate = clk_get_rate(clk);
566 + KUNIT_ASSERT_GT(test, rate, 0);
567 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
568 +}
569 +
570 +static struct kunit_case clk_range_test_cases[] = {
571 + KUNIT_CASE(clk_range_test_set_range),
572 + KUNIT_CASE(clk_range_test_set_range_invalid),
573 + KUNIT_CASE(clk_range_test_multiple_disjoints_range),
574 + KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
575 + KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
576 + KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
577 + KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
578 + KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
579 + KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
580 + KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
581 + KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
582 + {}
583 +};
584 +
585 +static struct kunit_suite clk_range_test_suite = {
586 + .name = "clk-range-test",
587 + .init = clk_test_init,
588 + .exit = clk_test_exit,
589 + .test_cases = clk_range_test_cases,
590 +};
591 +
592 +/*
593 + * Test that if:
594 + * - we have several subsequent calls to clk_set_rate_range();
595 + * - and we have a round_rate ops that always return the maximum
596 + * frequency allowed;
597 + *
598 + * The clock will run at the minimum of all maximum boundaries
599 + * requested, even if those boundaries aren't there anymore.
600 + */
601 +static void clk_range_test_set_range_rate_maximized(struct kunit *test)
602 +{
603 + struct clk_dummy_context *ctx = test->priv;
604 + struct clk_hw *hw = &ctx->hw;
605 + struct clk *clk = hw->clk;
606 + unsigned long rate;
607 +
608 + KUNIT_ASSERT_EQ(test,
609 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
610 + 0);
611 +
612 + KUNIT_ASSERT_EQ(test,
613 + clk_set_rate_range(clk,
614 + DUMMY_CLOCK_RATE_1,
615 + DUMMY_CLOCK_RATE_2),
616 + 0);
617 +
618 + rate = clk_get_rate(clk);
619 + KUNIT_ASSERT_GT(test, rate, 0);
620 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
621 +
622 + KUNIT_ASSERT_EQ(test,
623 + clk_set_rate_range(clk,
624 + DUMMY_CLOCK_RATE_1,
625 + DUMMY_CLOCK_RATE_2 - 1000),
626 + 0);
627 +
628 + rate = clk_get_rate(clk);
629 + KUNIT_ASSERT_GT(test, rate, 0);
630 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
631 +
632 + KUNIT_ASSERT_EQ(test,
633 + clk_set_rate_range(clk,
634 + DUMMY_CLOCK_RATE_1,
635 + DUMMY_CLOCK_RATE_2),
636 + 0);
637 +
638 + rate = clk_get_rate(clk);
639 + KUNIT_ASSERT_GT(test, rate, 0);
640 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
641 +}
642 +
643 +/*
644 + * Test that if:
645 + * - we have several subsequent calls to clk_set_rate_range(), across
646 + * multiple users;
647 + * - and we have a round_rate ops that always return the maximum
648 + * frequency allowed;
649 + *
650 + * The clock will run at the minimum of all maximum boundaries
651 + * requested, even if those boundaries aren't there anymore.
652 + */
653 +static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
654 +{
655 + struct clk_dummy_context *ctx = test->priv;
656 + struct clk_hw *hw = &ctx->hw;
657 + struct clk *clk = hw->clk;
658 + struct clk *user1, *user2;
659 + unsigned long rate;
660 +
661 + user1 = clk_hw_get_clk(hw, NULL);
662 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
663 +
664 + user2 = clk_hw_get_clk(hw, NULL);
665 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
666 +
667 + KUNIT_ASSERT_EQ(test,
668 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
669 + 0);
670 +
671 + KUNIT_ASSERT_EQ(test,
672 + clk_set_rate_range(user1,
673 + 0,
674 + DUMMY_CLOCK_RATE_2),
675 + 0);
676 +
677 + rate = clk_get_rate(clk);
678 + KUNIT_ASSERT_GT(test, rate, 0);
679 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
680 +
681 + KUNIT_ASSERT_EQ(test,
682 + clk_set_rate_range(user2,
683 + 0,
684 + DUMMY_CLOCK_RATE_1),
685 + 0);
686 +
687 + rate = clk_get_rate(clk);
688 + KUNIT_ASSERT_GT(test, rate, 0);
689 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
690 +
691 + KUNIT_ASSERT_EQ(test,
692 + clk_set_rate_range(user2, 0, ULONG_MAX),
693 + 0);
694 +
695 + rate = clk_get_rate(clk);
696 + KUNIT_ASSERT_GT(test, rate, 0);
697 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
698 +
699 + clk_put(user2);
700 + clk_put(user1);
701 +}
702 +
703 +static struct kunit_case clk_range_maximize_test_cases[] = {
704 + KUNIT_CASE(clk_range_test_set_range_rate_maximized),
705 + KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
706 + {}
707 +};
708 +
709 +static struct kunit_suite clk_range_maximize_test_suite = {
710 + .name = "clk-range-maximize-test",
711 + .init = clk_maximize_test_init,
712 + .exit = clk_test_exit,
713 + .test_cases = clk_range_maximize_test_cases,
714 +};
715 +
716 +/*
717 + * Test that if:
718 + * - we have several subsequent calls to clk_set_rate_range()
719 + * - and we have a round_rate ops that always return the minimum
720 + * frequency allowed;
721 + *
722 + * The clock will run at the maximum of all minimum boundaries
723 + * requested, even if those boundaries aren't there anymore.
724 +*/
725 +static void clk_range_test_set_range_rate_minimized(struct kunit *test)
726 +{
727 + struct clk_dummy_context *ctx = test->priv;
728 + struct clk_hw *hw = &ctx->hw;
729 + struct clk *clk = hw->clk;
730 + unsigned long rate;
731 +
732 + KUNIT_ASSERT_EQ(test,
733 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
734 + 0);
735 +
736 + KUNIT_ASSERT_EQ(test,
737 + clk_set_rate_range(clk,
738 + DUMMY_CLOCK_RATE_1,
739 + DUMMY_CLOCK_RATE_2),
740 + 0);
741 +
742 + rate = clk_get_rate(clk);
743 + KUNIT_ASSERT_GT(test, rate, 0);
744 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
745 +
746 + KUNIT_ASSERT_EQ(test,
747 + clk_set_rate_range(clk,
748 + DUMMY_CLOCK_RATE_1 + 1000,
749 + DUMMY_CLOCK_RATE_2),
750 + 0);
751 +
752 + rate = clk_get_rate(clk);
753 + KUNIT_ASSERT_GT(test, rate, 0);
754 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
755 +
756 + KUNIT_ASSERT_EQ(test,
757 + clk_set_rate_range(clk,
758 + DUMMY_CLOCK_RATE_1,
759 + DUMMY_CLOCK_RATE_2),
760 + 0);
761 +
762 + rate = clk_get_rate(clk);
763 + KUNIT_ASSERT_GT(test, rate, 0);
764 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
765 +}
766 +
767 +/*
768 + * Test that if:
769 + * - we have several subsequent calls to clk_set_rate_range(), across
770 + * multiple users;
771 + * - and we have a round_rate ops that always return the minimum
772 + * frequency allowed;
773 + *
774 + * The clock will run at the maximum of all minimum boundaries
775 + * requested, even if those boundaries aren't there anymore.
776 +*/
777 +static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
778 +{
779 + struct clk_dummy_context *ctx = test->priv;
780 + struct clk_hw *hw = &ctx->hw;
781 + struct clk *clk = hw->clk;
782 + struct clk *user1, *user2;
783 + unsigned long rate;
784 +
785 + user1 = clk_hw_get_clk(hw, NULL);
786 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
787 +
788 + user2 = clk_hw_get_clk(hw, NULL);
789 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
790 +
791 + KUNIT_ASSERT_EQ(test,
792 + clk_set_rate_range(user1,
793 + DUMMY_CLOCK_RATE_1,
794 + ULONG_MAX),
795 + 0);
796 +
797 + rate = clk_get_rate(clk);
798 + KUNIT_ASSERT_GT(test, rate, 0);
799 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
800 +
801 + KUNIT_ASSERT_EQ(test,
802 + clk_set_rate_range(user2,
803 + DUMMY_CLOCK_RATE_2,
804 + ULONG_MAX),
805 + 0);
806 +
807 + rate = clk_get_rate(clk);
808 + KUNIT_ASSERT_GT(test, rate, 0);
809 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
810 +
811 + KUNIT_ASSERT_EQ(test,
812 + clk_set_rate_range(user2, 0, ULONG_MAX),
813 + 0);
814 +
815 + rate = clk_get_rate(clk);
816 + KUNIT_ASSERT_GT(test, rate, 0);
817 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
818 +
819 + clk_put(user2);
820 + clk_put(user1);
821 +}
822 +
823 +static struct kunit_case clk_range_minimize_test_cases[] = {
824 + KUNIT_CASE(clk_range_test_set_range_rate_minimized),
825 + KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
826 + {}
827 +};
828 +
829 +static struct kunit_suite clk_range_minimize_test_suite = {
830 + .name = "clk-range-minimize-test",
831 + .init = clk_minimize_test_init,
832 + .exit = clk_test_exit,
833 + .test_cases = clk_range_minimize_test_cases,
834 +};
835 +
836 +kunit_test_suites(
837 + &clk_test_suite,
838 + &clk_range_test_suite,
839 + &clk_range_maximize_test_suite,
840 + &clk_range_minimize_test_suite
841 +);
842 +MODULE_LICENSE("GPL v2");