1 // SPDX-License-Identifier: GPL-2.0
3 * Kunit test for clk rate management
6 #include <linux/clk-provider.h>
8 /* Needed for clk_hw_get_clk() */
11 #include <kunit/test.h>
13 #define DUMMY_CLOCK_INIT_RATE (42 * 1000 * 1000)
14 #define DUMMY_CLOCK_RATE_1 (142 * 1000 * 1000)
15 #define DUMMY_CLOCK_RATE_2 (242 * 1000 * 1000)
17 struct clk_dummy_context {
22 static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw,
23 unsigned long parent_rate)
25 struct clk_dummy_context *ctx =
26 container_of(hw, struct clk_dummy_context, hw);
31 static int clk_dummy_determine_rate(struct clk_hw *hw,
32 struct clk_rate_request *req)
34 /* Just return the same rate without modifying it */
38 static int clk_dummy_maximize_rate(struct clk_hw *hw,
39 struct clk_rate_request *req)
42 * If there's a maximum set, always run the clock at the maximum
45 if (req->max_rate < ULONG_MAX)
46 req->rate = req->max_rate;
51 static int clk_dummy_minimize_rate(struct clk_hw *hw,
52 struct clk_rate_request *req)
55 * If there's a minimum set, always run the clock at the minimum
58 if (req->min_rate > 0)
59 req->rate = req->min_rate;
64 static int clk_dummy_set_rate(struct clk_hw *hw,
66 unsigned long parent_rate)
68 struct clk_dummy_context *ctx =
69 container_of(hw, struct clk_dummy_context, hw);
75 static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
77 if (index >= clk_hw_get_num_parents(hw))
83 static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
88 static const struct clk_ops clk_dummy_rate_ops = {
89 .recalc_rate = clk_dummy_recalc_rate,
90 .determine_rate = clk_dummy_determine_rate,
91 .set_rate = clk_dummy_set_rate,
94 static const struct clk_ops clk_dummy_maximize_rate_ops = {
95 .recalc_rate = clk_dummy_recalc_rate,
96 .determine_rate = clk_dummy_maximize_rate,
97 .set_rate = clk_dummy_set_rate,
100 static const struct clk_ops clk_dummy_minimize_rate_ops = {
101 .recalc_rate = clk_dummy_recalc_rate,
102 .determine_rate = clk_dummy_minimize_rate,
103 .set_rate = clk_dummy_set_rate,
106 static const struct clk_ops clk_dummy_single_parent_ops = {
107 .set_parent = clk_dummy_single_set_parent,
108 .get_parent = clk_dummy_single_get_parent,
111 struct clk_multiple_parent_ctx {
112 struct clk_dummy_context parents_ctx[2];
117 static int clk_multiple_parents_mux_set_parent(struct clk_hw *hw, u8 index)
119 struct clk_multiple_parent_ctx *ctx =
120 container_of(hw, struct clk_multiple_parent_ctx, hw);
122 if (index >= clk_hw_get_num_parents(hw))
125 ctx->current_parent = index;
130 static u8 clk_multiple_parents_mux_get_parent(struct clk_hw *hw)
132 struct clk_multiple_parent_ctx *ctx =
133 container_of(hw, struct clk_multiple_parent_ctx, hw);
135 return ctx->current_parent;
138 static const struct clk_ops clk_multiple_parents_mux_ops = {
139 .get_parent = clk_multiple_parents_mux_get_parent,
140 .set_parent = clk_multiple_parents_mux_set_parent,
141 .determine_rate = __clk_mux_determine_rate_closest,
144 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
146 struct clk_dummy_context *ctx;
147 struct clk_init_data init = { };
150 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
153 ctx->rate = DUMMY_CLOCK_INIT_RATE;
156 init.name = "test_dummy_rate";
158 ctx->hw.init = &init;
160 ret = clk_hw_register(NULL, &ctx->hw);
167 static int clk_test_init(struct kunit *test)
169 return clk_test_init_with_ops(test, &clk_dummy_rate_ops);
172 static int clk_maximize_test_init(struct kunit *test)
174 return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops);
177 static int clk_minimize_test_init(struct kunit *test)
179 return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops);
182 static void clk_test_exit(struct kunit *test)
184 struct clk_dummy_context *ctx = test->priv;
186 clk_hw_unregister(&ctx->hw);
190 * Test that the actual rate matches what is returned by clk_get_rate()
192 static void clk_test_get_rate(struct kunit *test)
194 struct clk_dummy_context *ctx = test->priv;
195 struct clk_hw *hw = &ctx->hw;
196 struct clk *clk = hw->clk;
199 rate = clk_get_rate(clk);
200 KUNIT_ASSERT_GT(test, rate, 0);
201 KUNIT_EXPECT_EQ(test, rate, ctx->rate);
205 * Test that, after a call to clk_set_rate(), the rate returned by
206 * clk_get_rate() matches.
208 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
209 * modify the requested rate, which is our case in clk_dummy_rate_ops.
211 static void clk_test_set_get_rate(struct kunit *test)
213 struct clk_dummy_context *ctx = test->priv;
214 struct clk_hw *hw = &ctx->hw;
215 struct clk *clk = hw->clk;
218 KUNIT_ASSERT_EQ(test,
219 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
222 rate = clk_get_rate(clk);
223 KUNIT_ASSERT_GT(test, rate, 0);
224 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
228 * Test that, after several calls to clk_set_rate(), the rate returned
229 * by clk_get_rate() matches the last one.
231 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
232 * modify the requested rate, which is our case in clk_dummy_rate_ops.
234 static void clk_test_set_set_get_rate(struct kunit *test)
236 struct clk_dummy_context *ctx = test->priv;
237 struct clk_hw *hw = &ctx->hw;
238 struct clk *clk = hw->clk;
241 KUNIT_ASSERT_EQ(test,
242 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
245 KUNIT_ASSERT_EQ(test,
246 clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
249 rate = clk_get_rate(clk);
250 KUNIT_ASSERT_GT(test, rate, 0);
251 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
255 * Test that clk_round_rate and clk_set_rate are consitent and will
256 * return the same frequency.
258 static void clk_test_round_set_get_rate(struct kunit *test)
260 struct clk_dummy_context *ctx = test->priv;
261 struct clk_hw *hw = &ctx->hw;
262 struct clk *clk = hw->clk;
263 unsigned long rounded_rate, set_rate;
265 rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
266 KUNIT_ASSERT_GT(test, rounded_rate, 0);
267 KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
269 KUNIT_ASSERT_EQ(test,
270 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
273 set_rate = clk_get_rate(clk);
274 KUNIT_ASSERT_GT(test, set_rate, 0);
275 KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
278 static struct kunit_case clk_test_cases[] = {
279 KUNIT_CASE(clk_test_get_rate),
280 KUNIT_CASE(clk_test_set_get_rate),
281 KUNIT_CASE(clk_test_set_set_get_rate),
282 KUNIT_CASE(clk_test_round_set_get_rate),
287 * Test suite for a basic rate clock, without any parent.
289 * These tests are supposed to exercise the rate API with simple scenarios
291 static struct kunit_suite clk_test_suite = {
293 .init = clk_test_init,
294 .exit = clk_test_exit,
295 .test_cases = clk_test_cases,
298 static int clk_uncached_test_init(struct kunit *test)
300 struct clk_dummy_context *ctx;
303 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
308 ctx->rate = DUMMY_CLOCK_INIT_RATE;
309 ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
311 CLK_GET_RATE_NOCACHE);
313 ret = clk_hw_register(NULL, &ctx->hw);
321 * Test that for an uncached clock, the clock framework doesn't cache
322 * the rate and clk_get_rate() will return the underlying clock rate
323 * even if it changed.
325 static void clk_test_uncached_get_rate(struct kunit *test)
327 struct clk_dummy_context *ctx = test->priv;
328 struct clk_hw *hw = &ctx->hw;
329 struct clk *clk = hw->clk;
332 rate = clk_get_rate(clk);
333 KUNIT_ASSERT_GT(test, rate, 0);
334 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
336 ctx->rate = DUMMY_CLOCK_RATE_1;
337 rate = clk_get_rate(clk);
338 KUNIT_ASSERT_GT(test, rate, 0);
339 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
343 * Test that for an uncached clock, clk_set_rate_range() will work
344 * properly if the rate hasn't changed.
346 static void clk_test_uncached_set_range(struct kunit *test)
348 struct clk_dummy_context *ctx = test->priv;
349 struct clk_hw *hw = &ctx->hw;
350 struct clk *clk = hw->clk;
353 KUNIT_ASSERT_EQ(test,
354 clk_set_rate_range(clk,
359 rate = clk_get_rate(clk);
360 KUNIT_ASSERT_GT(test, rate, 0);
361 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
362 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
366 * Test that for an uncached clock, clk_set_rate_range() will work
367 * properly if the rate has changed in hardware.
369 * In this case, it means that if the rate wasn't initially in the range
370 * we're trying to set, but got changed at some point into the range
371 * without the kernel knowing about it, its rate shouldn't be affected.
373 static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
375 struct clk_dummy_context *ctx = test->priv;
376 struct clk_hw *hw = &ctx->hw;
377 struct clk *clk = hw->clk;
380 ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
381 KUNIT_ASSERT_EQ(test,
382 clk_set_rate_range(clk,
387 rate = clk_get_rate(clk);
388 KUNIT_ASSERT_GT(test, rate, 0);
389 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
392 static struct kunit_case clk_uncached_test_cases[] = {
393 KUNIT_CASE(clk_test_uncached_get_rate),
394 KUNIT_CASE(clk_test_uncached_set_range),
395 KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
400 * Test suite for a basic, uncached, rate clock, without any parent.
402 * These tests are supposed to exercise the rate API with simple scenarios
404 static struct kunit_suite clk_uncached_test_suite = {
405 .name = "clk-uncached-test",
406 .init = clk_uncached_test_init,
407 .exit = clk_test_exit,
408 .test_cases = clk_uncached_test_cases,
412 clk_multiple_parents_mux_test_init(struct kunit *test)
414 struct clk_multiple_parent_ctx *ctx;
415 const char *parents[2] = { "parent-0", "parent-1"};
418 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
423 ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
426 ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
427 ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
431 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
434 ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
435 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
439 ctx->current_parent = 0;
440 ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
441 &clk_multiple_parents_mux_ops,
442 CLK_SET_RATE_PARENT);
443 ret = clk_hw_register(NULL, &ctx->hw);
451 clk_multiple_parents_mux_test_exit(struct kunit *test)
453 struct clk_multiple_parent_ctx *ctx = test->priv;
455 clk_hw_unregister(&ctx->hw);
456 clk_hw_unregister(&ctx->parents_ctx[0].hw);
457 clk_hw_unregister(&ctx->parents_ctx[1].hw);
461 * Test that for a clock with multiple parents, clk_get_parent()
462 * actually returns the current one.
465 clk_test_multiple_parents_mux_get_parent(struct kunit *test)
467 struct clk_multiple_parent_ctx *ctx = test->priv;
468 struct clk_hw *hw = &ctx->hw;
469 struct clk *clk = hw->clk;
472 parent = clk_get_parent(clk);
473 KUNIT_EXPECT_TRUE(test, clk_is_match(parent, ctx->parents_ctx[0].hw.clk));
477 * Test that for a clock with a multiple parents, clk_has_parent()
478 * actually reports all of them as parents.
481 clk_test_multiple_parents_mux_has_parent(struct kunit *test)
483 struct clk_multiple_parent_ctx *ctx = test->priv;
484 struct clk_hw *hw = &ctx->hw;
485 struct clk *clk = hw->clk;
487 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, ctx->parents_ctx[0].hw.clk));
488 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, ctx->parents_ctx[1].hw.clk));
492 * Test that for a clock with a multiple parents, if we set a range on
493 * that clock and the parent is changed, its rate after the reparenting
494 * is still within the range we asked for.
496 * FIXME: clk_set_parent() only does the reparenting but doesn't
497 * reevaluate whether the new clock rate is within its boundaries or
501 clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
503 struct clk_multiple_parent_ctx *ctx = test->priv;
504 struct clk_hw *hw = &ctx->hw;
505 struct clk *clk = hw->clk;
506 struct clk *parent1, *parent2;
510 kunit_skip(test, "This needs to be fixed in the core.");
512 parent1 = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
513 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent1);
514 KUNIT_ASSERT_TRUE(test, clk_is_match(clk_get_parent(clk), parent1));
516 parent2 = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
517 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent2);
519 ret = clk_set_rate(parent1, DUMMY_CLOCK_RATE_1);
520 KUNIT_ASSERT_EQ(test, ret, 0);
522 ret = clk_set_rate(parent2, DUMMY_CLOCK_RATE_2);
523 KUNIT_ASSERT_EQ(test, ret, 0);
525 ret = clk_set_rate_range(clk,
526 DUMMY_CLOCK_RATE_1 - 1000,
527 DUMMY_CLOCK_RATE_1 + 1000);
528 KUNIT_ASSERT_EQ(test, ret, 0);
530 ret = clk_set_parent(clk, parent2);
531 KUNIT_ASSERT_EQ(test, ret, 0);
533 rate = clk_get_rate(clk);
534 KUNIT_ASSERT_GT(test, rate, 0);
535 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
536 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
542 static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
543 KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
544 KUNIT_CASE(clk_test_multiple_parents_mux_has_parent),
545 KUNIT_CASE(clk_test_multiple_parents_mux_set_range_set_parent_get_rate),
550 * Test suite for a basic mux clock with two parents, with
551 * CLK_SET_RATE_PARENT on the child.
553 * These tests are supposed to exercise the consumer API and check that
554 * the state of the child and parents are sane and consistent.
556 static struct kunit_suite
557 clk_multiple_parents_mux_test_suite = {
558 .name = "clk-multiple-parents-mux-test",
559 .init = clk_multiple_parents_mux_test_init,
560 .exit = clk_multiple_parents_mux_test_exit,
561 .test_cases = clk_multiple_parents_mux_test_cases,
565 clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test)
567 struct clk_multiple_parent_ctx *ctx;
568 const char *parents[2] = { "missing-parent", "proper-parent"};
571 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
576 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("proper-parent",
579 ctx->parents_ctx[1].rate = DUMMY_CLOCK_INIT_RATE;
580 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
584 ctx->hw.init = CLK_HW_INIT_PARENTS("test-orphan-mux", parents,
585 &clk_multiple_parents_mux_ops,
586 CLK_SET_RATE_PARENT);
587 ret = clk_hw_register(NULL, &ctx->hw);
595 clk_orphan_transparent_multiple_parent_mux_test_exit(struct kunit *test)
597 struct clk_multiple_parent_ctx *ctx = test->priv;
599 clk_hw_unregister(&ctx->hw);
600 clk_hw_unregister(&ctx->parents_ctx[1].hw);
604 * Test that, for a mux whose current parent hasn't been registered yet,
605 * clk_get_parent() will return NULL.
608 clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit *test)
610 struct clk_multiple_parent_ctx *ctx = test->priv;
611 struct clk_hw *hw = &ctx->hw;
612 struct clk *clk = hw->clk;
615 parent = clk_get_parent(clk);
616 KUNIT_EXPECT_PTR_EQ(test, parent, NULL);
620 * Test that, for a mux whose current parent hasn't been registered yet,
621 * calling clk_set_parent() to a valid parent will properly update the
622 * mux parent and its orphan status.
625 clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit *test)
627 struct clk_multiple_parent_ctx *ctx = test->priv;
628 struct clk_hw *hw = &ctx->hw;
629 struct clk *clk = hw->clk;
630 struct clk *parent, *new_parent;
633 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
634 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
636 ret = clk_set_parent(clk, parent);
637 KUNIT_ASSERT_EQ(test, ret, 0);
639 new_parent = clk_get_parent(clk);
640 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
641 KUNIT_EXPECT_TRUE(test, clk_is_match(parent, new_parent));
647 * Test that, for a mux that started orphan but got switched to a valid
648 * parent, calling clk_drop_range() on the mux won't affect the parent
652 clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit *test)
654 struct clk_multiple_parent_ctx *ctx = test->priv;
655 struct clk_hw *hw = &ctx->hw;
656 struct clk *clk = hw->clk, *parent;
657 unsigned long parent_rate, new_parent_rate;
660 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
661 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
663 parent_rate = clk_get_rate(parent);
664 KUNIT_ASSERT_GT(test, parent_rate, 0);
666 ret = clk_set_parent(clk, parent);
667 KUNIT_ASSERT_EQ(test, ret, 0);
669 ret = clk_drop_range(clk);
670 KUNIT_ASSERT_EQ(test, ret, 0);
672 new_parent_rate = clk_get_rate(clk);
673 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
674 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
680 * Test that, for a mux that started orphan but got switched to a valid
681 * parent, the rate of the mux and its new parent are consistent.
684 clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit *test)
686 struct clk_multiple_parent_ctx *ctx = test->priv;
687 struct clk_hw *hw = &ctx->hw;
688 struct clk *clk = hw->clk;
690 unsigned long parent_rate, rate;
693 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
694 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
696 parent_rate = clk_get_rate(parent);
697 KUNIT_ASSERT_GT(test, parent_rate, 0);
699 ret = clk_set_parent(clk, parent);
700 KUNIT_ASSERT_EQ(test, ret, 0);
702 rate = clk_get_rate(clk);
703 KUNIT_ASSERT_GT(test, rate, 0);
704 KUNIT_EXPECT_EQ(test, parent_rate, rate);
708 * Test that, for a mux that started orphan but got switched to a valid
709 * parent, calling clk_put() on the mux won't affect the parent rate.
712 clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit *test)
714 struct clk_multiple_parent_ctx *ctx = test->priv;
715 struct clk *clk, *parent;
716 unsigned long parent_rate, new_parent_rate;
719 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
720 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
722 clk = clk_hw_get_clk(&ctx->hw, NULL);
723 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
725 parent_rate = clk_get_rate(parent);
726 KUNIT_ASSERT_GT(test, parent_rate, 0);
728 ret = clk_set_parent(clk, parent);
729 KUNIT_ASSERT_EQ(test, ret, 0);
733 new_parent_rate = clk_get_rate(parent);
734 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
735 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
741 * Test that, for a mux that started orphan but got switched to a valid
742 * parent, calling clk_set_rate_range() will affect the parent state if
743 * its rate is out of range.
746 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit *test)
748 struct clk_multiple_parent_ctx *ctx = test->priv;
749 struct clk_hw *hw = &ctx->hw;
750 struct clk *clk = hw->clk, *parent;
754 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
755 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
757 ret = clk_set_parent(clk, parent);
758 KUNIT_ASSERT_EQ(test, ret, 0);
760 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
761 KUNIT_ASSERT_EQ(test, ret, 0);
763 rate = clk_get_rate(clk);
764 KUNIT_ASSERT_GT(test, rate, 0);
765 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
766 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
772 * Test that, for a mux that started orphan but got switched to a valid
773 * parent, calling clk_set_rate_range() won't affect the parent state if
774 * its rate is within range.
777 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit *test)
779 struct clk_multiple_parent_ctx *ctx = test->priv;
780 struct clk_hw *hw = &ctx->hw;
781 struct clk *clk = hw->clk, *parent;
782 unsigned long parent_rate, new_parent_rate;
785 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
786 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
788 parent_rate = clk_get_rate(parent);
789 KUNIT_ASSERT_GT(test, parent_rate, 0);
791 ret = clk_set_parent(clk, parent);
792 KUNIT_ASSERT_EQ(test, ret, 0);
794 ret = clk_set_rate_range(clk,
795 DUMMY_CLOCK_INIT_RATE - 1000,
796 DUMMY_CLOCK_INIT_RATE + 1000);
797 KUNIT_ASSERT_EQ(test, ret, 0);
799 new_parent_rate = clk_get_rate(parent);
800 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
801 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
807 * Test that, for a mux whose current parent hasn't been registered yet,
808 * calling clk_set_rate_range() will succeed but won't affect its rate.
811 clk_test_orphan_transparent_multiple_parent_mux_set_range_get_rate(struct kunit *test)
813 struct clk_multiple_parent_ctx *ctx = test->priv;
814 struct clk_hw *hw = &ctx->hw;
815 struct clk *clk = hw->clk;
819 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
820 KUNIT_ASSERT_EQ(test, ret, 0);
822 rate = clk_get_rate(clk);
823 KUNIT_EXPECT_EQ(test, rate, 0);
827 * Test that, for a mux whose current parent hasn't been registered yet,
828 * calling clk_set_rate_range() will succeed, and will be taken into
829 * account when rounding a rate.
832 clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit *test)
834 struct clk_multiple_parent_ctx *ctx = test->priv;
835 struct clk_hw *hw = &ctx->hw;
836 struct clk *clk = hw->clk;
840 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
841 KUNIT_ASSERT_EQ(test, ret, 0);
843 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
844 KUNIT_ASSERT_GT(test, rate, 0);
845 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
846 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
850 * Test that, for a mux that started orphan, was assigned and rate and
851 * then got switched to a valid parent, its rate is eventually within
854 * FIXME: Even though we update the rate as part of clk_set_parent(), we
855 * don't evaluate whether that new rate is within range and needs to be
859 clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit *test)
861 struct clk_multiple_parent_ctx *ctx = test->priv;
862 struct clk_hw *hw = &ctx->hw;
863 struct clk *clk = hw->clk, *parent;
867 kunit_skip(test, "This needs to be fixed in the core.");
869 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
870 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
872 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
873 KUNIT_ASSERT_EQ(test, ret, 0);
875 ret = clk_set_parent(clk, parent);
876 KUNIT_ASSERT_EQ(test, ret, 0);
878 rate = clk_get_rate(clk);
879 KUNIT_ASSERT_GT(test, rate, 0);
880 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
881 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
886 static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = {
887 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_get_parent),
888 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent),
889 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range),
890 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate),
891 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_put),
892 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified),
893 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched),
894 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_get_rate),
895 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate),
896 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate),
901 * Test suite for a basic mux clock with two parents. The default parent
902 * isn't registered, only the second parent is. By default, the clock
903 * will thus be orphan.
905 * These tests are supposed to exercise the behaviour of the consumer
906 * API when dealing with an orphan clock, and how we deal with the
907 * transition to a valid parent.
909 static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = {
910 .name = "clk-orphan-transparent-multiple-parent-mux-test",
911 .init = clk_orphan_transparent_multiple_parent_mux_test_init,
912 .exit = clk_orphan_transparent_multiple_parent_mux_test_exit,
913 .test_cases = clk_orphan_transparent_multiple_parent_mux_test_cases,
916 struct clk_single_parent_ctx {
917 struct clk_dummy_context parent_ctx;
921 static int clk_single_parent_mux_test_init(struct kunit *test)
923 struct clk_single_parent_ctx *ctx;
926 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
931 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
932 ctx->parent_ctx.hw.init =
933 CLK_HW_INIT_NO_PARENT("parent-clk",
937 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
941 ctx->hw.init = CLK_HW_INIT("test-clk", "parent-clk",
942 &clk_dummy_single_parent_ops,
943 CLK_SET_RATE_PARENT);
945 ret = clk_hw_register(NULL, &ctx->hw);
953 clk_single_parent_mux_test_exit(struct kunit *test)
955 struct clk_single_parent_ctx *ctx = test->priv;
957 clk_hw_unregister(&ctx->hw);
958 clk_hw_unregister(&ctx->parent_ctx.hw);
962 * Test that for a clock with a single parent, clk_get_parent() actually
963 * returns the parent.
966 clk_test_single_parent_mux_get_parent(struct kunit *test)
968 struct clk_single_parent_ctx *ctx = test->priv;
969 struct clk_hw *hw = &ctx->hw;
970 struct clk *clk = hw->clk;
973 parent = clk_get_parent(clk);
974 KUNIT_EXPECT_TRUE(test, clk_is_match(parent, ctx->parent_ctx.hw.clk));
978 * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
979 * if we set a range on both the child clock and its parent, with a
980 * smaller range on the child, the rate range returned by
981 * clk_get_rate_range() is aggregate of both ranges.
984 clk_test_single_parent_mux_get_range_both_child_smaller(struct kunit *test)
986 struct clk_single_parent_ctx *ctx = test->priv;
987 struct clk_hw *hw = &ctx->hw;
988 struct clk *clk = hw->clk;
990 unsigned long min, max;
993 parent = clk_get_parent(clk);
994 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
996 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
997 KUNIT_ASSERT_EQ(test, ret, 0);
999 ret = clk_set_rate_range(clk,
1000 DUMMY_CLOCK_RATE_1 + 1000,
1001 DUMMY_CLOCK_RATE_2 - 1000);
1002 KUNIT_ASSERT_EQ(test, ret, 0);
1004 clk_get_rate_range(clk, &min, &max);
1005 KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
1006 KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2 - 1000);
1010 * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
1011 * if we set a range on both the child clock and its parent, with a
1012 * smaller range on the parent, the rate range returned by
1013 * clk_get_rate_range() is aggregate of both ranges.
1015 * FIXME: clk_get_rate_range() (and clk_core_get_boundaries() in
1016 * particular) doesn't take the parent range into account when the clock
1017 * has CLK_SET_RATE_PARENT.
1020 clk_test_single_parent_mux_get_range_both_parent_smaller(struct kunit *test)
1022 struct clk_single_parent_ctx *ctx = test->priv;
1023 struct clk_hw *hw = &ctx->hw;
1024 struct clk *clk = hw->clk;
1026 unsigned long min, max;
1029 kunit_skip(test, "This needs to be fixed in the core.");
1031 parent = clk_get_parent(clk);
1032 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1034 ret = clk_set_rate_range(parent,
1035 DUMMY_CLOCK_RATE_1 + 1000,
1036 DUMMY_CLOCK_RATE_2 - 1000);
1037 KUNIT_ASSERT_EQ(test, ret, 0);
1039 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1040 KUNIT_ASSERT_EQ(test, ret, 0);
1042 clk_get_rate_range(clk, &min, &max);
1043 KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
1044 KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_2 - 1000);
1048 * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
1049 * if we set a range on the parent clock only, the rate range returned
1050 * by clk_get_rate_range() on the children clock matches the parent
1053 * FIXME: clk_get_rate_range() (and clk_core_get_boundaries() in
1054 * particular) doesn't take the parent range into account when the clock
1055 * has CLK_SET_RATE_PARENT.
1058 clk_test_single_parent_mux_get_range_parent_only(struct kunit *test)
1060 struct clk_single_parent_ctx *ctx = test->priv;
1061 struct clk_hw *hw = &ctx->hw;
1062 struct clk *clk = hw->clk;
1064 unsigned long min, max;
1067 kunit_skip(test, "This needs to be fixed in the core.");
1069 parent = clk_get_parent(clk);
1070 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1072 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1073 KUNIT_ASSERT_EQ(test, ret, 0);
1075 clk_get_rate_range(clk, &min, &max);
1076 KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
1077 KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_2);
1081 * Test that for a clock with a single parent, clk_has_parent() actually
1082 * reports it as a parent.
1085 clk_test_single_parent_mux_has_parent(struct kunit *test)
1087 struct clk_single_parent_ctx *ctx = test->priv;
1088 struct clk_hw *hw = &ctx->hw;
1089 struct clk *clk = hw->clk;
1090 struct clk *parent = ctx->parent_ctx.hw.clk;
1092 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
1096 * Test that for a clock that can't modify its rate and with a single
1097 * parent, if we set disjoints range on the parent and then the child,
1098 * the second will return an error.
1100 * FIXME: clk_set_rate_range() only considers the current clock when
1101 * evaluating whether ranges are disjoints and not the upstream clocks
1105 clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
1107 struct clk_single_parent_ctx *ctx = test->priv;
1108 struct clk_hw *hw = &ctx->hw;
1109 struct clk *clk = hw->clk;
1113 kunit_skip(test, "This needs to be fixed in the core.");
1115 parent = clk_get_parent(clk);
1116 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1118 ret = clk_set_rate_range(parent, 1000, 2000);
1119 KUNIT_ASSERT_EQ(test, ret, 0);
1121 ret = clk_set_rate_range(clk, 3000, 4000);
1122 KUNIT_EXPECT_LT(test, ret, 0);
1126 * Test that for a clock that can't modify its rate and with a single
1127 * parent, if we set disjoints range on the child and then the parent,
1128 * the second will return an error.
1130 * FIXME: clk_set_rate_range() only considers the current clock when
1131 * evaluating whether ranges are disjoints and not the downstream clocks
1135 clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
1137 struct clk_single_parent_ctx *ctx = test->priv;
1138 struct clk_hw *hw = &ctx->hw;
1139 struct clk *clk = hw->clk;
1143 kunit_skip(test, "This needs to be fixed in the core.");
1145 parent = clk_get_parent(clk);
1146 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1148 ret = clk_set_rate_range(clk, 1000, 2000);
1149 KUNIT_ASSERT_EQ(test, ret, 0);
1151 ret = clk_set_rate_range(parent, 3000, 4000);
1152 KUNIT_EXPECT_LT(test, ret, 0);
1156 * Test that for a clock that can't modify its rate and with a single
1157 * parent, if we set a range on the parent and then call
1158 * clk_round_rate(), the boundaries of the parent are taken into
1162 clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test)
1164 struct clk_single_parent_ctx *ctx = test->priv;
1165 struct clk_hw *hw = &ctx->hw;
1166 struct clk *clk = hw->clk;
1171 parent = clk_get_parent(clk);
1172 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1174 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1175 KUNIT_ASSERT_EQ(test, ret, 0);
1177 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1178 KUNIT_ASSERT_GT(test, rate, 0);
1179 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1180 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1184 * Test that for a clock that can't modify its rate and with a single
1185 * parent, if we set a range on the parent and a more restrictive one on
1186 * the child, and then call clk_round_rate(), the boundaries of the
1187 * two clocks are taken into account.
1190 clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test)
1192 struct clk_single_parent_ctx *ctx = test->priv;
1193 struct clk_hw *hw = &ctx->hw;
1194 struct clk *clk = hw->clk;
1199 parent = clk_get_parent(clk);
1200 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1202 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1203 KUNIT_ASSERT_EQ(test, ret, 0);
1205 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1206 KUNIT_ASSERT_EQ(test, ret, 0);
1208 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1209 KUNIT_ASSERT_GT(test, rate, 0);
1210 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1211 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1213 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1214 KUNIT_ASSERT_GT(test, rate, 0);
1215 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1216 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1220 * Test that for a clock that can't modify its rate and with a single
1221 * parent, if we set a range on the child and a more restrictive one on
1222 * the parent, and then call clk_round_rate(), the boundaries of the
1223 * two clocks are taken into account.
1226 clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test)
1228 struct clk_single_parent_ctx *ctx = test->priv;
1229 struct clk_hw *hw = &ctx->hw;
1230 struct clk *clk = hw->clk;
1235 parent = clk_get_parent(clk);
1236 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1238 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1239 KUNIT_ASSERT_EQ(test, ret, 0);
1241 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1242 KUNIT_ASSERT_EQ(test, ret, 0);
1244 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1245 KUNIT_ASSERT_GT(test, rate, 0);
1246 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1247 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1249 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1250 KUNIT_ASSERT_GT(test, rate, 0);
1251 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1252 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1255 static struct kunit_case clk_single_parent_mux_test_cases[] = {
1256 KUNIT_CASE(clk_test_single_parent_mux_get_parent),
1257 KUNIT_CASE(clk_test_single_parent_mux_get_range_both_child_smaller),
1258 KUNIT_CASE(clk_test_single_parent_mux_get_range_both_parent_smaller),
1259 KUNIT_CASE(clk_test_single_parent_mux_get_range_parent_only),
1260 KUNIT_CASE(clk_test_single_parent_mux_has_parent),
1261 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
1262 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
1263 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),
1264 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only),
1265 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller),
1270 * Test suite for a basic mux clock with one parent, with
1271 * CLK_SET_RATE_PARENT on the child.
1273 * These tests are supposed to exercise the consumer API and check that
1274 * the state of the child and parent are sane and consistent.
1276 static struct kunit_suite
1277 clk_single_parent_mux_test_suite = {
1278 .name = "clk-single-parent-mux-test",
1279 .init = clk_single_parent_mux_test_init,
1280 .exit = clk_single_parent_mux_test_exit,
1281 .test_cases = clk_single_parent_mux_test_cases,
1284 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
1286 struct clk_single_parent_ctx *ctx;
1287 struct clk_init_data init = { };
1288 const char *parents[] = { "orphan_parent" };
1291 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1296 init.name = "test_orphan_dummy_parent";
1297 init.ops = &clk_dummy_single_parent_ops;
1298 init.parent_names = parents;
1299 init.num_parents = ARRAY_SIZE(parents);
1300 init.flags = CLK_SET_RATE_PARENT;
1301 ctx->hw.init = &init;
1303 ret = clk_hw_register(NULL, &ctx->hw);
1307 memset(&init, 0, sizeof(init));
1308 init.name = "orphan_parent";
1309 init.ops = &clk_dummy_rate_ops;
1310 ctx->parent_ctx.hw.init = &init;
1311 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1313 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1321 * Test that a mux-only clock, with an initial rate within a range,
1322 * will still have the same rate after the range has been enforced.
1325 * https://lore.kernel.org/linux-clk/7720158d-10a7-a17b-73a4-a8615c9c6d5c@collabora.com/
1327 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
1329 struct clk_single_parent_ctx *ctx = test->priv;
1330 struct clk_hw *hw = &ctx->hw;
1331 struct clk *clk = hw->clk;
1332 unsigned long rate, new_rate;
1334 rate = clk_get_rate(clk);
1335 KUNIT_ASSERT_GT(test, rate, 0);
1337 KUNIT_ASSERT_EQ(test,
1338 clk_set_rate_range(clk,
1339 ctx->parent_ctx.rate - 1000,
1340 ctx->parent_ctx.rate + 1000),
1343 new_rate = clk_get_rate(clk);
1344 KUNIT_ASSERT_GT(test, new_rate, 0);
1345 KUNIT_EXPECT_EQ(test, rate, new_rate);
1348 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
1349 KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
1354 * Test suite for a basic mux clock with one parent. The parent is
1355 * registered after its child. The clock will thus be orphan when
1356 * registered, but will no longer be when the tests run.
1358 * These tests are supposed to make sure a clock that used to be orphan
1359 * has a sane, consistent, behaviour.
1361 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
1362 .name = "clk-orphan-transparent-single-parent-test",
1363 .init = clk_orphan_transparent_single_parent_mux_test_init,
1364 .exit = clk_single_parent_mux_test_exit,
1365 .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
1368 struct clk_single_parent_two_lvl_ctx {
1369 struct clk_dummy_context parent_parent_ctx;
1370 struct clk_dummy_context parent_ctx;
1375 clk_orphan_two_level_root_last_test_init(struct kunit *test)
1377 struct clk_single_parent_two_lvl_ctx *ctx;
1380 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1385 ctx->parent_ctx.hw.init =
1386 CLK_HW_INIT("intermediate-parent",
1388 &clk_dummy_single_parent_ops,
1389 CLK_SET_RATE_PARENT);
1390 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1395 CLK_HW_INIT("test-clk", "intermediate-parent",
1396 &clk_dummy_single_parent_ops,
1397 CLK_SET_RATE_PARENT);
1398 ret = clk_hw_register(NULL, &ctx->hw);
1402 ctx->parent_parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1403 ctx->parent_parent_ctx.hw.init =
1404 CLK_HW_INIT_NO_PARENT("root-parent",
1405 &clk_dummy_rate_ops,
1407 ret = clk_hw_register(NULL, &ctx->parent_parent_ctx.hw);
1415 clk_orphan_two_level_root_last_test_exit(struct kunit *test)
1417 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1419 clk_hw_unregister(&ctx->hw);
1420 clk_hw_unregister(&ctx->parent_ctx.hw);
1421 clk_hw_unregister(&ctx->parent_parent_ctx.hw);
1425 * Test that, for a clock whose parent used to be orphan, clk_get_rate()
1426 * will return the proper rate.
1429 clk_orphan_two_level_root_last_test_get_rate(struct kunit *test)
1431 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1432 struct clk_hw *hw = &ctx->hw;
1433 struct clk *clk = hw->clk;
1436 rate = clk_get_rate(clk);
1437 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1441 * Test that, for a clock whose parent used to be orphan,
1442 * clk_set_rate_range() won't affect its rate if it is already within
1445 * See (for Exynos 4210):
1446 * https://lore.kernel.org/linux-clk/366a0232-bb4a-c357-6aa8-636e398e05eb@samsung.com/
1449 clk_orphan_two_level_root_last_test_set_range(struct kunit *test)
1451 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1452 struct clk_hw *hw = &ctx->hw;
1453 struct clk *clk = hw->clk;
1457 ret = clk_set_rate_range(clk,
1458 DUMMY_CLOCK_INIT_RATE - 1000,
1459 DUMMY_CLOCK_INIT_RATE + 1000);
1460 KUNIT_ASSERT_EQ(test, ret, 0);
1462 rate = clk_get_rate(clk);
1463 KUNIT_ASSERT_GT(test, rate, 0);
1464 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1467 static struct kunit_case
1468 clk_orphan_two_level_root_last_test_cases[] = {
1469 KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate),
1470 KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range),
1475 * Test suite for a basic, transparent, clock with a parent that is also
1476 * such a clock. The parent's parent is registered last, while the
1477 * parent and its child are registered in that order. The intermediate
1478 * and leaf clocks will thus be orphan when registered, but the leaf
1479 * clock itself will always have its parent and will never be
1480 * reparented. Indeed, it's only orphan because its parent is.
1482 * These tests are supposed to exercise the behaviour of the consumer
1483 * API when dealing with an orphan clock, and how we deal with the
1484 * transition to a valid parent.
1486 static struct kunit_suite
1487 clk_orphan_two_level_root_last_test_suite = {
1488 .name = "clk-orphan-two-level-root-last-test",
1489 .init = clk_orphan_two_level_root_last_test_init,
1490 .exit = clk_orphan_two_level_root_last_test_exit,
1491 .test_cases = clk_orphan_two_level_root_last_test_cases,
1495 * Test that clk_set_rate_range() and clk_get_rate_range() are
1496 * consistent on a simple clock without any parent.
1498 static void clk_range_test_get_range(struct kunit *test)
1500 struct clk_dummy_context *ctx = test->priv;
1501 struct clk_hw *hw = &ctx->hw;
1502 struct clk *clk = hw->clk;
1503 unsigned long min, max;
1506 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1507 KUNIT_ASSERT_EQ(test, ret, 0);
1509 clk_get_rate_range(clk, &min, &max);
1510 KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
1511 KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2);
1515 * Test that, on a simple clock without any parent, if a rate range is
1516 * set on a clk, it's properly reported by clk_get_rate_range() on all
1517 * the clk structure of that clock.
1519 static void clk_range_test_get_range_multiple_clk(struct kunit *test)
1521 struct clk_dummy_context *ctx = test->priv;
1522 struct clk_hw *hw = &ctx->hw;
1523 struct clk *user1, *user2;
1524 unsigned long min, max;
1527 user1 = clk_hw_get_clk(hw, NULL);
1528 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1530 user2 = clk_hw_get_clk(hw, NULL);
1531 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1533 ret = clk_set_rate_range(user1,
1535 DUMMY_CLOCK_RATE_2);
1536 KUNIT_ASSERT_EQ(test, ret, 0);
1538 clk_get_rate_range(user2, &min, &max);
1539 KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
1540 KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2);
1544 * Test that, on a simple clock without any parent, if a rate range is
1545 * set on struct clk_hw, it's properly reported by clk_get_rate_range().
1547 static void clk_range_test_get_range_with_hw(struct kunit *test)
1549 struct clk_dummy_context *ctx = test->priv;
1550 struct clk_hw *hw = &ctx->hw;
1551 struct clk *clk = hw->clk;
1552 unsigned long min, max;
1555 clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1557 ret = clk_set_rate_range(clk,
1558 DUMMY_CLOCK_RATE_1 + 1000,
1559 DUMMY_CLOCK_RATE_2 - 1000);
1560 KUNIT_ASSERT_EQ(test, ret, 0);
1562 clk_get_rate_range(clk, &min, &max);
1563 KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
1564 KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2 - 1000);
1568 * Test that clk_set_rate_range won't return an error for a valid range
1569 * and that it will make sure the rate of the clock is within the
1572 static void clk_range_test_set_range(struct kunit *test)
1574 struct clk_dummy_context *ctx = test->priv;
1575 struct clk_hw *hw = &ctx->hw;
1576 struct clk *clk = hw->clk;
1579 KUNIT_ASSERT_EQ(test,
1580 clk_set_rate_range(clk,
1582 DUMMY_CLOCK_RATE_2),
1585 rate = clk_get_rate(clk);
1586 KUNIT_ASSERT_GT(test, rate, 0);
1587 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1588 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1592 * Test that calling clk_set_rate_range with a minimum rate higher than
1593 * the maximum rate returns an error.
1595 static void clk_range_test_set_range_invalid(struct kunit *test)
1597 struct clk_dummy_context *ctx = test->priv;
1598 struct clk_hw *hw = &ctx->hw;
1599 struct clk *clk = hw->clk;
1601 KUNIT_EXPECT_LT(test,
1602 clk_set_rate_range(clk,
1603 DUMMY_CLOCK_RATE_1 + 1000,
1604 DUMMY_CLOCK_RATE_1),
1609 * Test that users can't set multiple, disjoints, range that would be
1610 * impossible to meet.
1612 static void clk_range_test_multiple_disjoints_range(struct kunit *test)
1614 struct clk_dummy_context *ctx = test->priv;
1615 struct clk_hw *hw = &ctx->hw;
1616 struct clk *user1, *user2;
1618 user1 = clk_hw_get_clk(hw, NULL);
1619 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1621 user2 = clk_hw_get_clk(hw, NULL);
1622 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1624 KUNIT_ASSERT_EQ(test,
1625 clk_set_rate_range(user1, 1000, 2000),
1628 KUNIT_EXPECT_LT(test,
1629 clk_set_rate_range(user2, 3000, 4000),
1637 * Test that if our clock has some boundaries and we try to round a rate
1638 * lower than the minimum, the returned rate will be within range.
1640 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
1642 struct clk_dummy_context *ctx = test->priv;
1643 struct clk_hw *hw = &ctx->hw;
1644 struct clk *clk = hw->clk;
1647 KUNIT_ASSERT_EQ(test,
1648 clk_set_rate_range(clk,
1650 DUMMY_CLOCK_RATE_2),
1653 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1654 KUNIT_ASSERT_GT(test, rate, 0);
1655 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1656 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1660 * Test that if our clock has some boundaries and we try to set a rate
1661 * higher than the maximum, the new rate will be within range.
1663 static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
1665 struct clk_dummy_context *ctx = test->priv;
1666 struct clk_hw *hw = &ctx->hw;
1667 struct clk *clk = hw->clk;
1670 KUNIT_ASSERT_EQ(test,
1671 clk_set_rate_range(clk,
1673 DUMMY_CLOCK_RATE_2),
1676 KUNIT_ASSERT_EQ(test,
1677 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1680 rate = clk_get_rate(clk);
1681 KUNIT_ASSERT_GT(test, rate, 0);
1682 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1683 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1687 * Test that if our clock has some boundaries and we try to round and
1688 * set a rate lower than the minimum, the rate returned by
1689 * clk_round_rate() will be consistent with the new rate set by
1692 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
1694 struct clk_dummy_context *ctx = test->priv;
1695 struct clk_hw *hw = &ctx->hw;
1696 struct clk *clk = hw->clk;
1699 KUNIT_ASSERT_EQ(test,
1700 clk_set_rate_range(clk,
1702 DUMMY_CLOCK_RATE_2),
1705 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1706 KUNIT_ASSERT_GT(test, rounded, 0);
1708 KUNIT_ASSERT_EQ(test,
1709 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1712 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1716 * Test that if our clock has some boundaries and we try to round a rate
1717 * higher than the maximum, the returned rate will be within range.
1719 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
1721 struct clk_dummy_context *ctx = test->priv;
1722 struct clk_hw *hw = &ctx->hw;
1723 struct clk *clk = hw->clk;
1726 KUNIT_ASSERT_EQ(test,
1727 clk_set_rate_range(clk,
1729 DUMMY_CLOCK_RATE_2),
1732 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1733 KUNIT_ASSERT_GT(test, rate, 0);
1734 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1735 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1739 * Test that if our clock has some boundaries and we try to set a rate
1740 * higher than the maximum, the new rate will be within range.
1742 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
1744 struct clk_dummy_context *ctx = test->priv;
1745 struct clk_hw *hw = &ctx->hw;
1746 struct clk *clk = hw->clk;
1749 KUNIT_ASSERT_EQ(test,
1750 clk_set_rate_range(clk,
1752 DUMMY_CLOCK_RATE_2),
1755 KUNIT_ASSERT_EQ(test,
1756 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1759 rate = clk_get_rate(clk);
1760 KUNIT_ASSERT_GT(test, rate, 0);
1761 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1762 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1766 * Test that if our clock has some boundaries and we try to round and
1767 * set a rate higher than the maximum, the rate returned by
1768 * clk_round_rate() will be consistent with the new rate set by
1771 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
1773 struct clk_dummy_context *ctx = test->priv;
1774 struct clk_hw *hw = &ctx->hw;
1775 struct clk *clk = hw->clk;
1778 KUNIT_ASSERT_EQ(test,
1779 clk_set_rate_range(clk,
1781 DUMMY_CLOCK_RATE_2),
1784 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1785 KUNIT_ASSERT_GT(test, rounded, 0);
1787 KUNIT_ASSERT_EQ(test,
1788 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1791 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1795 * Test that if our clock has a rate lower than the minimum set by a
1796 * call to clk_set_rate_range(), the rate will be raised to match the
1799 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1800 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1802 static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
1804 struct clk_dummy_context *ctx = test->priv;
1805 struct clk_hw *hw = &ctx->hw;
1806 struct clk *clk = hw->clk;
1809 KUNIT_ASSERT_EQ(test,
1810 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1813 KUNIT_ASSERT_EQ(test,
1814 clk_set_rate_range(clk,
1816 DUMMY_CLOCK_RATE_2),
1819 rate = clk_get_rate(clk);
1820 KUNIT_ASSERT_GT(test, rate, 0);
1821 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1825 * Test that if our clock has a rate higher than the maximum set by a
1826 * call to clk_set_rate_range(), the rate will be lowered to match the
1829 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1830 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1832 static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
1834 struct clk_dummy_context *ctx = test->priv;
1835 struct clk_hw *hw = &ctx->hw;
1836 struct clk *clk = hw->clk;
1839 KUNIT_ASSERT_EQ(test,
1840 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1843 KUNIT_ASSERT_EQ(test,
1844 clk_set_rate_range(clk,
1846 DUMMY_CLOCK_RATE_2),
1849 rate = clk_get_rate(clk);
1850 KUNIT_ASSERT_GT(test, rate, 0);
1851 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1854 static struct kunit_case clk_range_test_cases[] = {
1855 KUNIT_CASE(clk_range_test_get_range),
1856 KUNIT_CASE(clk_range_test_get_range_with_hw),
1857 KUNIT_CASE(clk_range_test_get_range_multiple_clk),
1858 KUNIT_CASE(clk_range_test_set_range),
1859 KUNIT_CASE(clk_range_test_set_range_invalid),
1860 KUNIT_CASE(clk_range_test_multiple_disjoints_range),
1861 KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
1862 KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
1863 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
1864 KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
1865 KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
1866 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
1867 KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
1868 KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
1873 * Test suite for a basic rate clock, without any parent.
1875 * These tests are supposed to exercise the rate range API
1876 * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
1879 static struct kunit_suite clk_range_test_suite = {
1880 .name = "clk-range-test",
1881 .init = clk_test_init,
1882 .exit = clk_test_exit,
1883 .test_cases = clk_range_test_cases,
1887 * Test that if we have several subsequent calls to
1888 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1889 * needed each and every time.
1891 * With clk_dummy_maximize_rate_ops, this means that the the rate will
1892 * trail along the maximum as it evolves.
1894 static void clk_range_test_set_range_rate_maximized(struct kunit *test)
1896 struct clk_dummy_context *ctx = test->priv;
1897 struct clk_hw *hw = &ctx->hw;
1898 struct clk *clk = hw->clk;
1901 KUNIT_ASSERT_EQ(test,
1902 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1905 KUNIT_ASSERT_EQ(test,
1906 clk_set_rate_range(clk,
1908 DUMMY_CLOCK_RATE_2),
1911 rate = clk_get_rate(clk);
1912 KUNIT_ASSERT_GT(test, rate, 0);
1913 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1915 KUNIT_ASSERT_EQ(test,
1916 clk_set_rate_range(clk,
1918 DUMMY_CLOCK_RATE_2 - 1000),
1921 rate = clk_get_rate(clk);
1922 KUNIT_ASSERT_GT(test, rate, 0);
1923 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1925 KUNIT_ASSERT_EQ(test,
1926 clk_set_rate_range(clk,
1928 DUMMY_CLOCK_RATE_2),
1931 rate = clk_get_rate(clk);
1932 KUNIT_ASSERT_GT(test, rate, 0);
1933 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1937 * Test that if we have several subsequent calls to
1938 * clk_set_rate_range(), across multiple users, the core will reevaluate
1939 * whether a new rate is needed each and every time.
1941 * With clk_dummy_maximize_rate_ops, this means that the the rate will
1942 * trail along the maximum as it evolves.
1944 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
1946 struct clk_dummy_context *ctx = test->priv;
1947 struct clk_hw *hw = &ctx->hw;
1948 struct clk *clk = hw->clk;
1949 struct clk *user1, *user2;
1952 user1 = clk_hw_get_clk(hw, NULL);
1953 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1955 user2 = clk_hw_get_clk(hw, NULL);
1956 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1958 KUNIT_ASSERT_EQ(test,
1959 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1962 KUNIT_ASSERT_EQ(test,
1963 clk_set_rate_range(user1,
1965 DUMMY_CLOCK_RATE_2),
1968 rate = clk_get_rate(clk);
1969 KUNIT_ASSERT_GT(test, rate, 0);
1970 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1972 KUNIT_ASSERT_EQ(test,
1973 clk_set_rate_range(user2,
1975 DUMMY_CLOCK_RATE_1),
1978 rate = clk_get_rate(clk);
1979 KUNIT_ASSERT_GT(test, rate, 0);
1980 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1982 KUNIT_ASSERT_EQ(test,
1983 clk_drop_range(user2),
1986 rate = clk_get_rate(clk);
1987 KUNIT_ASSERT_GT(test, rate, 0);
1988 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1995 * Test that if we have several subsequent calls to
1996 * clk_set_rate_range(), across multiple users, the core will reevaluate
1997 * whether a new rate is needed, including when a user drop its clock.
1999 * With clk_dummy_maximize_rate_ops, this means that the rate will
2000 * trail along the maximum as it evolves.
2002 static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
2004 struct clk_dummy_context *ctx = test->priv;
2005 struct clk_hw *hw = &ctx->hw;
2006 struct clk *clk = hw->clk;
2007 struct clk *user1, *user2;
2010 user1 = clk_hw_get_clk(hw, NULL);
2011 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2013 user2 = clk_hw_get_clk(hw, NULL);
2014 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2016 KUNIT_ASSERT_EQ(test,
2017 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
2020 KUNIT_ASSERT_EQ(test,
2021 clk_set_rate_range(user1,
2023 DUMMY_CLOCK_RATE_2),
2026 rate = clk_get_rate(clk);
2027 KUNIT_ASSERT_GT(test, rate, 0);
2028 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2030 KUNIT_ASSERT_EQ(test,
2031 clk_set_rate_range(user2,
2033 DUMMY_CLOCK_RATE_1),
2036 rate = clk_get_rate(clk);
2037 KUNIT_ASSERT_GT(test, rate, 0);
2038 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2042 rate = clk_get_rate(clk);
2043 KUNIT_ASSERT_GT(test, rate, 0);
2044 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2049 static struct kunit_case clk_range_maximize_test_cases[] = {
2050 KUNIT_CASE(clk_range_test_set_range_rate_maximized),
2051 KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
2052 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
2057 * Test suite for a basic rate clock, without any parent.
2059 * These tests are supposed to exercise the rate range API
2060 * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
2061 * clk_drop_range), with a driver that will always try to run at the
2062 * highest possible rate.
2064 static struct kunit_suite clk_range_maximize_test_suite = {
2065 .name = "clk-range-maximize-test",
2066 .init = clk_maximize_test_init,
2067 .exit = clk_test_exit,
2068 .test_cases = clk_range_maximize_test_cases,
2072 * Test that if we have several subsequent calls to
2073 * clk_set_rate_range(), the core will reevaluate whether a new rate is
2074 * needed each and every time.
2076 * With clk_dummy_minimize_rate_ops, this means that the the rate will
2077 * trail along the minimum as it evolves.
2079 static void clk_range_test_set_range_rate_minimized(struct kunit *test)
2081 struct clk_dummy_context *ctx = test->priv;
2082 struct clk_hw *hw = &ctx->hw;
2083 struct clk *clk = hw->clk;
2086 KUNIT_ASSERT_EQ(test,
2087 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
2090 KUNIT_ASSERT_EQ(test,
2091 clk_set_rate_range(clk,
2093 DUMMY_CLOCK_RATE_2),
2096 rate = clk_get_rate(clk);
2097 KUNIT_ASSERT_GT(test, rate, 0);
2098 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2100 KUNIT_ASSERT_EQ(test,
2101 clk_set_rate_range(clk,
2102 DUMMY_CLOCK_RATE_1 + 1000,
2103 DUMMY_CLOCK_RATE_2),
2106 rate = clk_get_rate(clk);
2107 KUNIT_ASSERT_GT(test, rate, 0);
2108 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
2110 KUNIT_ASSERT_EQ(test,
2111 clk_set_rate_range(clk,
2113 DUMMY_CLOCK_RATE_2),
2116 rate = clk_get_rate(clk);
2117 KUNIT_ASSERT_GT(test, rate, 0);
2118 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2122 * Test that if we have several subsequent calls to
2123 * clk_set_rate_range(), across multiple users, the core will reevaluate
2124 * whether a new rate is needed each and every time.
2126 * With clk_dummy_minimize_rate_ops, this means that the the rate will
2127 * trail along the minimum as it evolves.
2129 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
2131 struct clk_dummy_context *ctx = test->priv;
2132 struct clk_hw *hw = &ctx->hw;
2133 struct clk *clk = hw->clk;
2134 struct clk *user1, *user2;
2137 user1 = clk_hw_get_clk(hw, NULL);
2138 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2140 user2 = clk_hw_get_clk(hw, NULL);
2141 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2143 KUNIT_ASSERT_EQ(test,
2144 clk_set_rate_range(user1,
2149 rate = clk_get_rate(clk);
2150 KUNIT_ASSERT_GT(test, rate, 0);
2151 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2153 KUNIT_ASSERT_EQ(test,
2154 clk_set_rate_range(user2,
2159 rate = clk_get_rate(clk);
2160 KUNIT_ASSERT_GT(test, rate, 0);
2161 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2163 KUNIT_ASSERT_EQ(test,
2164 clk_drop_range(user2),
2167 rate = clk_get_rate(clk);
2168 KUNIT_ASSERT_GT(test, rate, 0);
2169 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2176 * Test that if we have several subsequent calls to
2177 * clk_set_rate_range(), across multiple users, the core will reevaluate
2178 * whether a new rate is needed, including when a user drop its clock.
2180 * With clk_dummy_minimize_rate_ops, this means that the rate will
2181 * trail along the minimum as it evolves.
2183 static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
2185 struct clk_dummy_context *ctx = test->priv;
2186 struct clk_hw *hw = &ctx->hw;
2187 struct clk *clk = hw->clk;
2188 struct clk *user1, *user2;
2191 user1 = clk_hw_get_clk(hw, NULL);
2192 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2194 user2 = clk_hw_get_clk(hw, NULL);
2195 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2197 KUNIT_ASSERT_EQ(test,
2198 clk_set_rate_range(user1,
2203 rate = clk_get_rate(clk);
2204 KUNIT_ASSERT_GT(test, rate, 0);
2205 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2207 KUNIT_ASSERT_EQ(test,
2208 clk_set_rate_range(user2,
2213 rate = clk_get_rate(clk);
2214 KUNIT_ASSERT_GT(test, rate, 0);
2215 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2219 rate = clk_get_rate(clk);
2220 KUNIT_ASSERT_GT(test, rate, 0);
2221 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2226 static struct kunit_case clk_range_minimize_test_cases[] = {
2227 KUNIT_CASE(clk_range_test_set_range_rate_minimized),
2228 KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
2229 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
2234 * Test suite for a basic rate clock, without any parent.
2236 * These tests are supposed to exercise the rate range API
2237 * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
2238 * clk_drop_range), with a driver that will always try to run at the
2239 * lowest possible rate.
2241 static struct kunit_suite clk_range_minimize_test_suite = {
2242 .name = "clk-range-minimize-test",
2243 .init = clk_minimize_test_init,
2244 .exit = clk_test_exit,
2245 .test_cases = clk_range_minimize_test_cases,
2248 struct clk_leaf_mux_ctx {
2249 struct clk_multiple_parent_ctx mux_ctx;
2254 clk_leaf_mux_set_rate_parent_test_init(struct kunit *test)
2256 struct clk_leaf_mux_ctx *ctx;
2257 const char *top_parents[2] = { "parent-0", "parent-1" };
2260 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2265 ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2266 &clk_dummy_rate_ops,
2268 ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2269 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2273 ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2274 &clk_dummy_rate_ops,
2276 ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2277 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2281 ctx->mux_ctx.current_parent = 0;
2282 ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2283 &clk_multiple_parents_mux_ops,
2285 ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2289 ctx->hw.init = CLK_HW_INIT_HW("test-clock", &ctx->mux_ctx.hw,
2290 &clk_dummy_single_parent_ops,
2291 CLK_SET_RATE_PARENT);
2292 ret = clk_hw_register(NULL, &ctx->hw);
2299 static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test)
2301 struct clk_leaf_mux_ctx *ctx = test->priv;
2303 clk_hw_unregister(&ctx->hw);
2304 clk_hw_unregister(&ctx->mux_ctx.hw);
2305 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2306 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2310 * Test that, for a clock that will forward any rate request to its
2311 * parent, the rate request structure returned by __clk_determine_rate
2312 * is sane and will be what we expect.
2314 static void clk_leaf_mux_set_rate_parent_determine_rate(struct kunit *test)
2316 struct clk_leaf_mux_ctx *ctx = test->priv;
2317 struct clk_hw *hw = &ctx->hw;
2318 struct clk *clk = hw->clk;
2319 struct clk_rate_request req;
2323 rate = clk_get_rate(clk);
2324 KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2326 clk_hw_init_rate_request(hw, &req, DUMMY_CLOCK_RATE_2);
2328 ret = __clk_determine_rate(hw, &req);
2329 KUNIT_ASSERT_EQ(test, ret, 0);
2331 KUNIT_EXPECT_EQ(test, req.rate, DUMMY_CLOCK_RATE_2);
2332 KUNIT_EXPECT_EQ(test, req.best_parent_rate, DUMMY_CLOCK_RATE_2);
2333 KUNIT_EXPECT_PTR_EQ(test, req.best_parent_hw, &ctx->mux_ctx.hw);
2336 static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = {
2337 KUNIT_CASE(clk_leaf_mux_set_rate_parent_determine_rate),
2342 * Test suite for a clock whose parent is a mux with multiple parents.
2343 * The leaf clock has CLK_SET_RATE_PARENT, and will forward rate
2344 * requests to the mux, which will then select which parent is the best
2345 * fit for a given rate.
2347 * These tests are supposed to exercise the behaviour of muxes, and the
2348 * proper selection of parents.
2350 static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = {
2351 .name = "clk-leaf-mux-set-rate-parent",
2352 .init = clk_leaf_mux_set_rate_parent_test_init,
2353 .exit = clk_leaf_mux_set_rate_parent_test_exit,
2354 .test_cases = clk_leaf_mux_set_rate_parent_test_cases,
2358 &clk_leaf_mux_set_rate_parent_test_suite,
2360 &clk_multiple_parents_mux_test_suite,
2361 &clk_orphan_transparent_multiple_parent_mux_test_suite,
2362 &clk_orphan_transparent_single_parent_test_suite,
2363 &clk_orphan_two_level_root_last_test_suite,
2364 &clk_range_test_suite,
2365 &clk_range_maximize_test_suite,
2366 &clk_range_minimize_test_suite,
2367 &clk_single_parent_mux_test_suite,
2368 &clk_uncached_test_suite
2370 MODULE_LICENSE("GPL v2");