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, clk_has_parent() actually
979 * reports it as a parent.
982 clk_test_single_parent_mux_has_parent(struct kunit *test)
984 struct clk_single_parent_ctx *ctx = test->priv;
985 struct clk_hw *hw = &ctx->hw;
986 struct clk *clk = hw->clk;
987 struct clk *parent = ctx->parent_ctx.hw.clk;
989 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
993 * Test that for a clock that can't modify its rate and with a single
994 * parent, if we set disjoints range on the parent and then the child,
995 * the second will return an error.
997 * FIXME: clk_set_rate_range() only considers the current clock when
998 * evaluating whether ranges are disjoints and not the upstream clocks
1002 clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
1004 struct clk_single_parent_ctx *ctx = test->priv;
1005 struct clk_hw *hw = &ctx->hw;
1006 struct clk *clk = hw->clk;
1010 kunit_skip(test, "This needs to be fixed in the core.");
1012 parent = clk_get_parent(clk);
1013 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1015 ret = clk_set_rate_range(parent, 1000, 2000);
1016 KUNIT_ASSERT_EQ(test, ret, 0);
1018 ret = clk_set_rate_range(clk, 3000, 4000);
1019 KUNIT_EXPECT_LT(test, ret, 0);
1023 * Test that for a clock that can't modify its rate and with a single
1024 * parent, if we set disjoints range on the child and then the parent,
1025 * the second will return an error.
1027 * FIXME: clk_set_rate_range() only considers the current clock when
1028 * evaluating whether ranges are disjoints and not the downstream clocks
1032 clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
1034 struct clk_single_parent_ctx *ctx = test->priv;
1035 struct clk_hw *hw = &ctx->hw;
1036 struct clk *clk = hw->clk;
1040 kunit_skip(test, "This needs to be fixed in the core.");
1042 parent = clk_get_parent(clk);
1043 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1045 ret = clk_set_rate_range(clk, 1000, 2000);
1046 KUNIT_ASSERT_EQ(test, ret, 0);
1048 ret = clk_set_rate_range(parent, 3000, 4000);
1049 KUNIT_EXPECT_LT(test, ret, 0);
1053 * Test that for a clock that can't modify its rate and with a single
1054 * parent, if we set a range on the parent and then call
1055 * clk_round_rate(), the boundaries of the parent are taken into
1059 clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test)
1061 struct clk_single_parent_ctx *ctx = test->priv;
1062 struct clk_hw *hw = &ctx->hw;
1063 struct clk *clk = hw->clk;
1068 parent = clk_get_parent(clk);
1069 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1071 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1072 KUNIT_ASSERT_EQ(test, ret, 0);
1074 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1075 KUNIT_ASSERT_GT(test, rate, 0);
1076 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1077 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1081 * Test that for a clock that can't modify its rate and with a single
1082 * parent, if we set a range on the parent and a more restrictive one on
1083 * the child, and then call clk_round_rate(), the boundaries of the
1084 * two clocks are taken into account.
1087 clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test)
1089 struct clk_single_parent_ctx *ctx = test->priv;
1090 struct clk_hw *hw = &ctx->hw;
1091 struct clk *clk = hw->clk;
1096 parent = clk_get_parent(clk);
1097 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1099 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1100 KUNIT_ASSERT_EQ(test, ret, 0);
1102 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1103 KUNIT_ASSERT_EQ(test, ret, 0);
1105 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1106 KUNIT_ASSERT_GT(test, rate, 0);
1107 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1108 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1110 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1111 KUNIT_ASSERT_GT(test, rate, 0);
1112 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1113 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1117 * Test that for a clock that can't modify its rate and with a single
1118 * parent, if we set a range on the child and a more restrictive one on
1119 * the parent, and then call clk_round_rate(), the boundaries of the
1120 * two clocks are taken into account.
1123 clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test)
1125 struct clk_single_parent_ctx *ctx = test->priv;
1126 struct clk_hw *hw = &ctx->hw;
1127 struct clk *clk = hw->clk;
1132 parent = clk_get_parent(clk);
1133 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1135 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1136 KUNIT_ASSERT_EQ(test, ret, 0);
1138 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1139 KUNIT_ASSERT_EQ(test, ret, 0);
1141 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1142 KUNIT_ASSERT_GT(test, rate, 0);
1143 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1144 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1146 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1147 KUNIT_ASSERT_GT(test, rate, 0);
1148 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1149 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1152 static struct kunit_case clk_single_parent_mux_test_cases[] = {
1153 KUNIT_CASE(clk_test_single_parent_mux_get_parent),
1154 KUNIT_CASE(clk_test_single_parent_mux_has_parent),
1155 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
1156 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
1157 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),
1158 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only),
1159 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller),
1164 * Test suite for a basic mux clock with one parent, with
1165 * CLK_SET_RATE_PARENT on the child.
1167 * These tests are supposed to exercise the consumer API and check that
1168 * the state of the child and parent are sane and consistent.
1170 static struct kunit_suite
1171 clk_single_parent_mux_test_suite = {
1172 .name = "clk-single-parent-mux-test",
1173 .init = clk_single_parent_mux_test_init,
1174 .exit = clk_single_parent_mux_test_exit,
1175 .test_cases = clk_single_parent_mux_test_cases,
1178 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
1180 struct clk_single_parent_ctx *ctx;
1181 struct clk_init_data init = { };
1182 const char *parents[] = { "orphan_parent" };
1185 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1190 init.name = "test_orphan_dummy_parent";
1191 init.ops = &clk_dummy_single_parent_ops;
1192 init.parent_names = parents;
1193 init.num_parents = ARRAY_SIZE(parents);
1194 init.flags = CLK_SET_RATE_PARENT;
1195 ctx->hw.init = &init;
1197 ret = clk_hw_register(NULL, &ctx->hw);
1201 memset(&init, 0, sizeof(init));
1202 init.name = "orphan_parent";
1203 init.ops = &clk_dummy_rate_ops;
1204 ctx->parent_ctx.hw.init = &init;
1205 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1207 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1215 * Test that a mux-only clock, with an initial rate within a range,
1216 * will still have the same rate after the range has been enforced.
1219 * https://lore.kernel.org/linux-clk/7720158d-10a7-a17b-73a4-a8615c9c6d5c@collabora.com/
1221 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
1223 struct clk_single_parent_ctx *ctx = test->priv;
1224 struct clk_hw *hw = &ctx->hw;
1225 struct clk *clk = hw->clk;
1226 unsigned long rate, new_rate;
1228 rate = clk_get_rate(clk);
1229 KUNIT_ASSERT_GT(test, rate, 0);
1231 KUNIT_ASSERT_EQ(test,
1232 clk_set_rate_range(clk,
1233 ctx->parent_ctx.rate - 1000,
1234 ctx->parent_ctx.rate + 1000),
1237 new_rate = clk_get_rate(clk);
1238 KUNIT_ASSERT_GT(test, new_rate, 0);
1239 KUNIT_EXPECT_EQ(test, rate, new_rate);
1242 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
1243 KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
1248 * Test suite for a basic mux clock with one parent. The parent is
1249 * registered after its child. The clock will thus be orphan when
1250 * registered, but will no longer be when the tests run.
1252 * These tests are supposed to make sure a clock that used to be orphan
1253 * has a sane, consistent, behaviour.
1255 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
1256 .name = "clk-orphan-transparent-single-parent-test",
1257 .init = clk_orphan_transparent_single_parent_mux_test_init,
1258 .exit = clk_single_parent_mux_test_exit,
1259 .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
1262 struct clk_single_parent_two_lvl_ctx {
1263 struct clk_dummy_context parent_parent_ctx;
1264 struct clk_dummy_context parent_ctx;
1269 clk_orphan_two_level_root_last_test_init(struct kunit *test)
1271 struct clk_single_parent_two_lvl_ctx *ctx;
1274 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1279 ctx->parent_ctx.hw.init =
1280 CLK_HW_INIT("intermediate-parent",
1282 &clk_dummy_single_parent_ops,
1283 CLK_SET_RATE_PARENT);
1284 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1289 CLK_HW_INIT("test-clk", "intermediate-parent",
1290 &clk_dummy_single_parent_ops,
1291 CLK_SET_RATE_PARENT);
1292 ret = clk_hw_register(NULL, &ctx->hw);
1296 ctx->parent_parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1297 ctx->parent_parent_ctx.hw.init =
1298 CLK_HW_INIT_NO_PARENT("root-parent",
1299 &clk_dummy_rate_ops,
1301 ret = clk_hw_register(NULL, &ctx->parent_parent_ctx.hw);
1309 clk_orphan_two_level_root_last_test_exit(struct kunit *test)
1311 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1313 clk_hw_unregister(&ctx->hw);
1314 clk_hw_unregister(&ctx->parent_ctx.hw);
1315 clk_hw_unregister(&ctx->parent_parent_ctx.hw);
1319 * Test that, for a clock whose parent used to be orphan, clk_get_rate()
1320 * will return the proper rate.
1323 clk_orphan_two_level_root_last_test_get_rate(struct kunit *test)
1325 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1326 struct clk_hw *hw = &ctx->hw;
1327 struct clk *clk = hw->clk;
1330 rate = clk_get_rate(clk);
1331 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1335 * Test that, for a clock whose parent used to be orphan,
1336 * clk_set_rate_range() won't affect its rate if it is already within
1339 * See (for Exynos 4210):
1340 * https://lore.kernel.org/linux-clk/366a0232-bb4a-c357-6aa8-636e398e05eb@samsung.com/
1343 clk_orphan_two_level_root_last_test_set_range(struct kunit *test)
1345 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1346 struct clk_hw *hw = &ctx->hw;
1347 struct clk *clk = hw->clk;
1351 ret = clk_set_rate_range(clk,
1352 DUMMY_CLOCK_INIT_RATE - 1000,
1353 DUMMY_CLOCK_INIT_RATE + 1000);
1354 KUNIT_ASSERT_EQ(test, ret, 0);
1356 rate = clk_get_rate(clk);
1357 KUNIT_ASSERT_GT(test, rate, 0);
1358 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1361 static struct kunit_case
1362 clk_orphan_two_level_root_last_test_cases[] = {
1363 KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate),
1364 KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range),
1369 * Test suite for a basic, transparent, clock with a parent that is also
1370 * such a clock. The parent's parent is registered last, while the
1371 * parent and its child are registered in that order. The intermediate
1372 * and leaf clocks will thus be orphan when registered, but the leaf
1373 * clock itself will always have its parent and will never be
1374 * reparented. Indeed, it's only orphan because its parent is.
1376 * These tests are supposed to exercise the behaviour of the consumer
1377 * API when dealing with an orphan clock, and how we deal with the
1378 * transition to a valid parent.
1380 static struct kunit_suite
1381 clk_orphan_two_level_root_last_test_suite = {
1382 .name = "clk-orphan-two-level-root-last-test",
1383 .init = clk_orphan_two_level_root_last_test_init,
1384 .exit = clk_orphan_two_level_root_last_test_exit,
1385 .test_cases = clk_orphan_two_level_root_last_test_cases,
1389 * Test that clk_set_rate_range won't return an error for a valid range
1390 * and that it will make sure the rate of the clock is within the
1393 static void clk_range_test_set_range(struct kunit *test)
1395 struct clk_dummy_context *ctx = test->priv;
1396 struct clk_hw *hw = &ctx->hw;
1397 struct clk *clk = hw->clk;
1400 KUNIT_ASSERT_EQ(test,
1401 clk_set_rate_range(clk,
1403 DUMMY_CLOCK_RATE_2),
1406 rate = clk_get_rate(clk);
1407 KUNIT_ASSERT_GT(test, rate, 0);
1408 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1409 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1413 * Test that calling clk_set_rate_range with a minimum rate higher than
1414 * the maximum rate returns an error.
1416 static void clk_range_test_set_range_invalid(struct kunit *test)
1418 struct clk_dummy_context *ctx = test->priv;
1419 struct clk_hw *hw = &ctx->hw;
1420 struct clk *clk = hw->clk;
1422 KUNIT_EXPECT_LT(test,
1423 clk_set_rate_range(clk,
1424 DUMMY_CLOCK_RATE_1 + 1000,
1425 DUMMY_CLOCK_RATE_1),
1430 * Test that users can't set multiple, disjoints, range that would be
1431 * impossible to meet.
1433 static void clk_range_test_multiple_disjoints_range(struct kunit *test)
1435 struct clk_dummy_context *ctx = test->priv;
1436 struct clk_hw *hw = &ctx->hw;
1437 struct clk *user1, *user2;
1439 user1 = clk_hw_get_clk(hw, NULL);
1440 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1442 user2 = clk_hw_get_clk(hw, NULL);
1443 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1445 KUNIT_ASSERT_EQ(test,
1446 clk_set_rate_range(user1, 1000, 2000),
1449 KUNIT_EXPECT_LT(test,
1450 clk_set_rate_range(user2, 3000, 4000),
1458 * Test that if our clock has some boundaries and we try to round a rate
1459 * lower than the minimum, the returned rate will be within range.
1461 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
1463 struct clk_dummy_context *ctx = test->priv;
1464 struct clk_hw *hw = &ctx->hw;
1465 struct clk *clk = hw->clk;
1468 KUNIT_ASSERT_EQ(test,
1469 clk_set_rate_range(clk,
1471 DUMMY_CLOCK_RATE_2),
1474 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1475 KUNIT_ASSERT_GT(test, rate, 0);
1476 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1477 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1481 * Test that if our clock has some boundaries and we try to set a rate
1482 * higher than the maximum, the new rate will be within range.
1484 static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
1486 struct clk_dummy_context *ctx = test->priv;
1487 struct clk_hw *hw = &ctx->hw;
1488 struct clk *clk = hw->clk;
1491 KUNIT_ASSERT_EQ(test,
1492 clk_set_rate_range(clk,
1494 DUMMY_CLOCK_RATE_2),
1497 KUNIT_ASSERT_EQ(test,
1498 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1501 rate = clk_get_rate(clk);
1502 KUNIT_ASSERT_GT(test, rate, 0);
1503 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1504 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1508 * Test that if our clock has some boundaries and we try to round and
1509 * set a rate lower than the minimum, the rate returned by
1510 * clk_round_rate() will be consistent with the new rate set by
1513 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
1515 struct clk_dummy_context *ctx = test->priv;
1516 struct clk_hw *hw = &ctx->hw;
1517 struct clk *clk = hw->clk;
1520 KUNIT_ASSERT_EQ(test,
1521 clk_set_rate_range(clk,
1523 DUMMY_CLOCK_RATE_2),
1526 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1527 KUNIT_ASSERT_GT(test, rounded, 0);
1529 KUNIT_ASSERT_EQ(test,
1530 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1533 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1537 * Test that if our clock has some boundaries and we try to round a rate
1538 * higher than the maximum, the returned rate will be within range.
1540 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
1542 struct clk_dummy_context *ctx = test->priv;
1543 struct clk_hw *hw = &ctx->hw;
1544 struct clk *clk = hw->clk;
1547 KUNIT_ASSERT_EQ(test,
1548 clk_set_rate_range(clk,
1550 DUMMY_CLOCK_RATE_2),
1553 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1554 KUNIT_ASSERT_GT(test, rate, 0);
1555 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1556 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1560 * Test that if our clock has some boundaries and we try to set a rate
1561 * higher than the maximum, the new rate will be within range.
1563 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
1565 struct clk_dummy_context *ctx = test->priv;
1566 struct clk_hw *hw = &ctx->hw;
1567 struct clk *clk = hw->clk;
1570 KUNIT_ASSERT_EQ(test,
1571 clk_set_rate_range(clk,
1573 DUMMY_CLOCK_RATE_2),
1576 KUNIT_ASSERT_EQ(test,
1577 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1580 rate = clk_get_rate(clk);
1581 KUNIT_ASSERT_GT(test, rate, 0);
1582 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1583 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1587 * Test that if our clock has some boundaries and we try to round and
1588 * set a rate higher than the maximum, the rate returned by
1589 * clk_round_rate() will be consistent with the new rate set by
1592 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
1594 struct clk_dummy_context *ctx = test->priv;
1595 struct clk_hw *hw = &ctx->hw;
1596 struct clk *clk = hw->clk;
1599 KUNIT_ASSERT_EQ(test,
1600 clk_set_rate_range(clk,
1602 DUMMY_CLOCK_RATE_2),
1605 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1606 KUNIT_ASSERT_GT(test, rounded, 0);
1608 KUNIT_ASSERT_EQ(test,
1609 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1612 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1616 * Test that if our clock has a rate lower than the minimum set by a
1617 * call to clk_set_rate_range(), the rate will be raised to match the
1620 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1621 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1623 static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
1625 struct clk_dummy_context *ctx = test->priv;
1626 struct clk_hw *hw = &ctx->hw;
1627 struct clk *clk = hw->clk;
1630 KUNIT_ASSERT_EQ(test,
1631 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1634 KUNIT_ASSERT_EQ(test,
1635 clk_set_rate_range(clk,
1637 DUMMY_CLOCK_RATE_2),
1640 rate = clk_get_rate(clk);
1641 KUNIT_ASSERT_GT(test, rate, 0);
1642 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1646 * Test that if our clock has a rate higher than the maximum set by a
1647 * call to clk_set_rate_range(), the rate will be lowered to match the
1650 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1651 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1653 static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
1655 struct clk_dummy_context *ctx = test->priv;
1656 struct clk_hw *hw = &ctx->hw;
1657 struct clk *clk = hw->clk;
1660 KUNIT_ASSERT_EQ(test,
1661 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1664 KUNIT_ASSERT_EQ(test,
1665 clk_set_rate_range(clk,
1667 DUMMY_CLOCK_RATE_2),
1670 rate = clk_get_rate(clk);
1671 KUNIT_ASSERT_GT(test, rate, 0);
1672 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1675 static struct kunit_case clk_range_test_cases[] = {
1676 KUNIT_CASE(clk_range_test_set_range),
1677 KUNIT_CASE(clk_range_test_set_range_invalid),
1678 KUNIT_CASE(clk_range_test_multiple_disjoints_range),
1679 KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
1680 KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
1681 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
1682 KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
1683 KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
1684 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
1685 KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
1686 KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
1691 * Test suite for a basic rate clock, without any parent.
1693 * These tests are supposed to exercise the rate range API
1694 * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
1697 static struct kunit_suite clk_range_test_suite = {
1698 .name = "clk-range-test",
1699 .init = clk_test_init,
1700 .exit = clk_test_exit,
1701 .test_cases = clk_range_test_cases,
1705 * Test that if we have several subsequent calls to
1706 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1707 * needed each and every time.
1709 * With clk_dummy_maximize_rate_ops, this means that the the rate will
1710 * trail along the maximum as it evolves.
1712 static void clk_range_test_set_range_rate_maximized(struct kunit *test)
1714 struct clk_dummy_context *ctx = test->priv;
1715 struct clk_hw *hw = &ctx->hw;
1716 struct clk *clk = hw->clk;
1719 KUNIT_ASSERT_EQ(test,
1720 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1723 KUNIT_ASSERT_EQ(test,
1724 clk_set_rate_range(clk,
1726 DUMMY_CLOCK_RATE_2),
1729 rate = clk_get_rate(clk);
1730 KUNIT_ASSERT_GT(test, rate, 0);
1731 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1733 KUNIT_ASSERT_EQ(test,
1734 clk_set_rate_range(clk,
1736 DUMMY_CLOCK_RATE_2 - 1000),
1739 rate = clk_get_rate(clk);
1740 KUNIT_ASSERT_GT(test, rate, 0);
1741 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1743 KUNIT_ASSERT_EQ(test,
1744 clk_set_rate_range(clk,
1746 DUMMY_CLOCK_RATE_2),
1749 rate = clk_get_rate(clk);
1750 KUNIT_ASSERT_GT(test, rate, 0);
1751 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1755 * Test that if we have several subsequent calls to
1756 * clk_set_rate_range(), across multiple users, the core will reevaluate
1757 * whether a new rate is needed each and every time.
1759 * With clk_dummy_maximize_rate_ops, this means that the the rate will
1760 * trail along the maximum as it evolves.
1762 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
1764 struct clk_dummy_context *ctx = test->priv;
1765 struct clk_hw *hw = &ctx->hw;
1766 struct clk *clk = hw->clk;
1767 struct clk *user1, *user2;
1770 user1 = clk_hw_get_clk(hw, NULL);
1771 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1773 user2 = clk_hw_get_clk(hw, NULL);
1774 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1776 KUNIT_ASSERT_EQ(test,
1777 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1780 KUNIT_ASSERT_EQ(test,
1781 clk_set_rate_range(user1,
1783 DUMMY_CLOCK_RATE_2),
1786 rate = clk_get_rate(clk);
1787 KUNIT_ASSERT_GT(test, rate, 0);
1788 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1790 KUNIT_ASSERT_EQ(test,
1791 clk_set_rate_range(user2,
1793 DUMMY_CLOCK_RATE_1),
1796 rate = clk_get_rate(clk);
1797 KUNIT_ASSERT_GT(test, rate, 0);
1798 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1800 KUNIT_ASSERT_EQ(test,
1801 clk_drop_range(user2),
1804 rate = clk_get_rate(clk);
1805 KUNIT_ASSERT_GT(test, rate, 0);
1806 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1813 * Test that if we have several subsequent calls to
1814 * clk_set_rate_range(), across multiple users, the core will reevaluate
1815 * whether a new rate is needed, including when a user drop its clock.
1817 * With clk_dummy_maximize_rate_ops, this means that the rate will
1818 * trail along the maximum as it evolves.
1820 static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
1822 struct clk_dummy_context *ctx = test->priv;
1823 struct clk_hw *hw = &ctx->hw;
1824 struct clk *clk = hw->clk;
1825 struct clk *user1, *user2;
1828 user1 = clk_hw_get_clk(hw, NULL);
1829 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1831 user2 = clk_hw_get_clk(hw, NULL);
1832 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1834 KUNIT_ASSERT_EQ(test,
1835 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1838 KUNIT_ASSERT_EQ(test,
1839 clk_set_rate_range(user1,
1841 DUMMY_CLOCK_RATE_2),
1844 rate = clk_get_rate(clk);
1845 KUNIT_ASSERT_GT(test, rate, 0);
1846 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1848 KUNIT_ASSERT_EQ(test,
1849 clk_set_rate_range(user2,
1851 DUMMY_CLOCK_RATE_1),
1854 rate = clk_get_rate(clk);
1855 KUNIT_ASSERT_GT(test, rate, 0);
1856 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1860 rate = clk_get_rate(clk);
1861 KUNIT_ASSERT_GT(test, rate, 0);
1862 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1867 static struct kunit_case clk_range_maximize_test_cases[] = {
1868 KUNIT_CASE(clk_range_test_set_range_rate_maximized),
1869 KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
1870 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
1875 * Test suite for a basic rate clock, without any parent.
1877 * These tests are supposed to exercise the rate range API
1878 * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
1879 * clk_drop_range), with a driver that will always try to run at the
1880 * highest possible rate.
1882 static struct kunit_suite clk_range_maximize_test_suite = {
1883 .name = "clk-range-maximize-test",
1884 .init = clk_maximize_test_init,
1885 .exit = clk_test_exit,
1886 .test_cases = clk_range_maximize_test_cases,
1890 * Test that if we have several subsequent calls to
1891 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1892 * needed each and every time.
1894 * With clk_dummy_minimize_rate_ops, this means that the the rate will
1895 * trail along the minimum as it evolves.
1897 static void clk_range_test_set_range_rate_minimized(struct kunit *test)
1899 struct clk_dummy_context *ctx = test->priv;
1900 struct clk_hw *hw = &ctx->hw;
1901 struct clk *clk = hw->clk;
1904 KUNIT_ASSERT_EQ(test,
1905 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1908 KUNIT_ASSERT_EQ(test,
1909 clk_set_rate_range(clk,
1911 DUMMY_CLOCK_RATE_2),
1914 rate = clk_get_rate(clk);
1915 KUNIT_ASSERT_GT(test, rate, 0);
1916 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1918 KUNIT_ASSERT_EQ(test,
1919 clk_set_rate_range(clk,
1920 DUMMY_CLOCK_RATE_1 + 1000,
1921 DUMMY_CLOCK_RATE_2),
1924 rate = clk_get_rate(clk);
1925 KUNIT_ASSERT_GT(test, rate, 0);
1926 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1928 KUNIT_ASSERT_EQ(test,
1929 clk_set_rate_range(clk,
1931 DUMMY_CLOCK_RATE_2),
1934 rate = clk_get_rate(clk);
1935 KUNIT_ASSERT_GT(test, rate, 0);
1936 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1940 * Test that if we have several subsequent calls to
1941 * clk_set_rate_range(), across multiple users, the core will reevaluate
1942 * whether a new rate is needed each and every time.
1944 * With clk_dummy_minimize_rate_ops, this means that the the rate will
1945 * trail along the minimum as it evolves.
1947 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
1949 struct clk_dummy_context *ctx = test->priv;
1950 struct clk_hw *hw = &ctx->hw;
1951 struct clk *clk = hw->clk;
1952 struct clk *user1, *user2;
1955 user1 = clk_hw_get_clk(hw, NULL);
1956 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1958 user2 = clk_hw_get_clk(hw, NULL);
1959 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1961 KUNIT_ASSERT_EQ(test,
1962 clk_set_rate_range(user1,
1967 rate = clk_get_rate(clk);
1968 KUNIT_ASSERT_GT(test, rate, 0);
1969 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1971 KUNIT_ASSERT_EQ(test,
1972 clk_set_rate_range(user2,
1977 rate = clk_get_rate(clk);
1978 KUNIT_ASSERT_GT(test, rate, 0);
1979 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1981 KUNIT_ASSERT_EQ(test,
1982 clk_drop_range(user2),
1985 rate = clk_get_rate(clk);
1986 KUNIT_ASSERT_GT(test, rate, 0);
1987 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1994 * Test that if we have several subsequent calls to
1995 * clk_set_rate_range(), across multiple users, the core will reevaluate
1996 * whether a new rate is needed, including when a user drop its clock.
1998 * With clk_dummy_minimize_rate_ops, this means that the rate will
1999 * trail along the minimum as it evolves.
2001 static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
2003 struct clk_dummy_context *ctx = test->priv;
2004 struct clk_hw *hw = &ctx->hw;
2005 struct clk *clk = hw->clk;
2006 struct clk *user1, *user2;
2009 user1 = clk_hw_get_clk(hw, NULL);
2010 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2012 user2 = clk_hw_get_clk(hw, NULL);
2013 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2015 KUNIT_ASSERT_EQ(test,
2016 clk_set_rate_range(user1,
2021 rate = clk_get_rate(clk);
2022 KUNIT_ASSERT_GT(test, rate, 0);
2023 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2025 KUNIT_ASSERT_EQ(test,
2026 clk_set_rate_range(user2,
2031 rate = clk_get_rate(clk);
2032 KUNIT_ASSERT_GT(test, rate, 0);
2033 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2037 rate = clk_get_rate(clk);
2038 KUNIT_ASSERT_GT(test, rate, 0);
2039 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2044 static struct kunit_case clk_range_minimize_test_cases[] = {
2045 KUNIT_CASE(clk_range_test_set_range_rate_minimized),
2046 KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
2047 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
2052 * Test suite for a basic rate clock, without any parent.
2054 * These tests are supposed to exercise the rate range API
2055 * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
2056 * clk_drop_range), with a driver that will always try to run at the
2057 * lowest possible rate.
2059 static struct kunit_suite clk_range_minimize_test_suite = {
2060 .name = "clk-range-minimize-test",
2061 .init = clk_minimize_test_init,
2062 .exit = clk_test_exit,
2063 .test_cases = clk_range_minimize_test_cases,
2066 struct clk_leaf_mux_ctx {
2067 struct clk_multiple_parent_ctx mux_ctx;
2072 clk_leaf_mux_set_rate_parent_test_init(struct kunit *test)
2074 struct clk_leaf_mux_ctx *ctx;
2075 const char *top_parents[2] = { "parent-0", "parent-1" };
2078 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2083 ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2084 &clk_dummy_rate_ops,
2086 ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2087 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2091 ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2092 &clk_dummy_rate_ops,
2094 ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2095 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2099 ctx->mux_ctx.current_parent = 0;
2100 ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2101 &clk_multiple_parents_mux_ops,
2103 ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2107 ctx->hw.init = CLK_HW_INIT_HW("test-clock", &ctx->mux_ctx.hw,
2108 &clk_dummy_single_parent_ops,
2109 CLK_SET_RATE_PARENT);
2110 ret = clk_hw_register(NULL, &ctx->hw);
2117 static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test)
2119 struct clk_leaf_mux_ctx *ctx = test->priv;
2121 clk_hw_unregister(&ctx->hw);
2122 clk_hw_unregister(&ctx->mux_ctx.hw);
2123 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2124 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2128 * Test that, for a clock that will forward any rate request to its
2129 * parent, the rate request structure returned by __clk_determine_rate
2130 * is sane and will be what we expect.
2132 static void clk_leaf_mux_set_rate_parent_determine_rate(struct kunit *test)
2134 struct clk_leaf_mux_ctx *ctx = test->priv;
2135 struct clk_hw *hw = &ctx->hw;
2136 struct clk *clk = hw->clk;
2137 struct clk_rate_request req;
2141 rate = clk_get_rate(clk);
2142 KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2144 clk_hw_init_rate_request(hw, &req, DUMMY_CLOCK_RATE_2);
2146 ret = __clk_determine_rate(hw, &req);
2147 KUNIT_ASSERT_EQ(test, ret, 0);
2149 KUNIT_EXPECT_EQ(test, req.rate, DUMMY_CLOCK_RATE_2);
2150 KUNIT_EXPECT_EQ(test, req.best_parent_rate, DUMMY_CLOCK_RATE_2);
2151 KUNIT_EXPECT_PTR_EQ(test, req.best_parent_hw, &ctx->mux_ctx.hw);
2154 static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = {
2155 KUNIT_CASE(clk_leaf_mux_set_rate_parent_determine_rate),
2160 * Test suite for a clock whose parent is a mux with multiple parents.
2161 * The leaf clock has CLK_SET_RATE_PARENT, and will forward rate
2162 * requests to the mux, which will then select which parent is the best
2163 * fit for a given rate.
2165 * These tests are supposed to exercise the behaviour of muxes, and the
2166 * proper selection of parents.
2168 static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = {
2169 .name = "clk-leaf-mux-set-rate-parent",
2170 .init = clk_leaf_mux_set_rate_parent_test_init,
2171 .exit = clk_leaf_mux_set_rate_parent_test_exit,
2172 .test_cases = clk_leaf_mux_set_rate_parent_test_cases,
2176 &clk_leaf_mux_set_rate_parent_test_suite,
2178 &clk_multiple_parents_mux_test_suite,
2179 &clk_orphan_transparent_multiple_parent_mux_test_suite,
2180 &clk_orphan_transparent_single_parent_test_suite,
2181 &clk_orphan_two_level_root_last_test_suite,
2182 &clk_range_test_suite,
2183 &clk_range_maximize_test_suite,
2184 &clk_range_minimize_test_suite,
2185 &clk_single_parent_mux_test_suite,
2186 &clk_uncached_test_suite
2188 MODULE_LICENSE("GPL v2");