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 = clk_hw_get_clk(hw, NULL);
199 rate = clk_get_rate(clk);
200 KUNIT_ASSERT_GT(test, rate, 0);
201 KUNIT_EXPECT_EQ(test, rate, ctx->rate);
207 * Test that, after a call to clk_set_rate(), the rate returned by
208 * clk_get_rate() matches.
210 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
211 * modify the requested rate, which is our case in clk_dummy_rate_ops.
213 static void clk_test_set_get_rate(struct kunit *test)
215 struct clk_dummy_context *ctx = test->priv;
216 struct clk_hw *hw = &ctx->hw;
217 struct clk *clk = clk_hw_get_clk(hw, NULL);
220 KUNIT_ASSERT_EQ(test,
221 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
224 rate = clk_get_rate(clk);
225 KUNIT_ASSERT_GT(test, rate, 0);
226 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
232 * Test that, after several calls to clk_set_rate(), the rate returned
233 * by clk_get_rate() matches the last one.
235 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
236 * modify the requested rate, which is our case in clk_dummy_rate_ops.
238 static void clk_test_set_set_get_rate(struct kunit *test)
240 struct clk_dummy_context *ctx = test->priv;
241 struct clk_hw *hw = &ctx->hw;
242 struct clk *clk = clk_hw_get_clk(hw, NULL);
245 KUNIT_ASSERT_EQ(test,
246 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
249 KUNIT_ASSERT_EQ(test,
250 clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
253 rate = clk_get_rate(clk);
254 KUNIT_ASSERT_GT(test, rate, 0);
255 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
261 * Test that clk_round_rate and clk_set_rate are consitent and will
262 * return the same frequency.
264 static void clk_test_round_set_get_rate(struct kunit *test)
266 struct clk_dummy_context *ctx = test->priv;
267 struct clk_hw *hw = &ctx->hw;
268 struct clk *clk = clk_hw_get_clk(hw, NULL);
269 unsigned long rounded_rate, set_rate;
271 rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
272 KUNIT_ASSERT_GT(test, rounded_rate, 0);
273 KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
275 KUNIT_ASSERT_EQ(test,
276 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
279 set_rate = clk_get_rate(clk);
280 KUNIT_ASSERT_GT(test, set_rate, 0);
281 KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
286 static struct kunit_case clk_test_cases[] = {
287 KUNIT_CASE(clk_test_get_rate),
288 KUNIT_CASE(clk_test_set_get_rate),
289 KUNIT_CASE(clk_test_set_set_get_rate),
290 KUNIT_CASE(clk_test_round_set_get_rate),
295 * Test suite for a basic rate clock, without any parent.
297 * These tests exercise the rate API with simple scenarios
299 static struct kunit_suite clk_test_suite = {
301 .init = clk_test_init,
302 .exit = clk_test_exit,
303 .test_cases = clk_test_cases,
306 static int clk_uncached_test_init(struct kunit *test)
308 struct clk_dummy_context *ctx;
311 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
316 ctx->rate = DUMMY_CLOCK_INIT_RATE;
317 ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
319 CLK_GET_RATE_NOCACHE);
321 ret = clk_hw_register(NULL, &ctx->hw);
329 * Test that for an uncached clock, the clock framework doesn't cache
330 * the rate and clk_get_rate() will return the underlying clock rate
331 * even if it changed.
333 static void clk_test_uncached_get_rate(struct kunit *test)
335 struct clk_dummy_context *ctx = test->priv;
336 struct clk_hw *hw = &ctx->hw;
337 struct clk *clk = clk_hw_get_clk(hw, NULL);
340 rate = clk_get_rate(clk);
341 KUNIT_ASSERT_GT(test, rate, 0);
342 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
344 /* We change the rate behind the clock framework's back */
345 ctx->rate = DUMMY_CLOCK_RATE_1;
346 rate = clk_get_rate(clk);
347 KUNIT_ASSERT_GT(test, rate, 0);
348 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
354 * Test that for an uncached clock, clk_set_rate_range() will work
355 * properly if the rate hasn't changed.
357 static void clk_test_uncached_set_range(struct kunit *test)
359 struct clk_dummy_context *ctx = test->priv;
360 struct clk_hw *hw = &ctx->hw;
361 struct clk *clk = clk_hw_get_clk(hw, NULL);
364 KUNIT_ASSERT_EQ(test,
365 clk_set_rate_range(clk,
370 rate = clk_get_rate(clk);
371 KUNIT_ASSERT_GT(test, rate, 0);
372 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
373 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
379 * Test that for an uncached clock, clk_set_rate_range() will work
380 * properly if the rate has changed in hardware.
382 * In this case, it means that if the rate wasn't initially in the range
383 * we're trying to set, but got changed at some point into the range
384 * without the kernel knowing about it, its rate shouldn't be affected.
386 static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
388 struct clk_dummy_context *ctx = test->priv;
389 struct clk_hw *hw = &ctx->hw;
390 struct clk *clk = clk_hw_get_clk(hw, NULL);
393 /* We change the rate behind the clock framework's back */
394 ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
395 KUNIT_ASSERT_EQ(test,
396 clk_set_rate_range(clk,
401 rate = clk_get_rate(clk);
402 KUNIT_ASSERT_GT(test, rate, 0);
403 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
408 static struct kunit_case clk_uncached_test_cases[] = {
409 KUNIT_CASE(clk_test_uncached_get_rate),
410 KUNIT_CASE(clk_test_uncached_set_range),
411 KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
416 * Test suite for a basic, uncached, rate clock, without any parent.
418 * These tests exercise the rate API with simple scenarios
420 static struct kunit_suite clk_uncached_test_suite = {
421 .name = "clk-uncached-test",
422 .init = clk_uncached_test_init,
423 .exit = clk_test_exit,
424 .test_cases = clk_uncached_test_cases,
428 clk_multiple_parents_mux_test_init(struct kunit *test)
430 struct clk_multiple_parent_ctx *ctx;
431 const char *parents[2] = { "parent-0", "parent-1"};
434 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
439 ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
442 ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
443 ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
447 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
450 ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
451 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
455 ctx->current_parent = 0;
456 ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
457 &clk_multiple_parents_mux_ops,
458 CLK_SET_RATE_PARENT);
459 ret = clk_hw_register(NULL, &ctx->hw);
467 clk_multiple_parents_mux_test_exit(struct kunit *test)
469 struct clk_multiple_parent_ctx *ctx = test->priv;
471 clk_hw_unregister(&ctx->hw);
472 clk_hw_unregister(&ctx->parents_ctx[0].hw);
473 clk_hw_unregister(&ctx->parents_ctx[1].hw);
477 * Test that for a clock with multiple parents, clk_get_parent()
478 * actually returns the current one.
481 clk_test_multiple_parents_mux_get_parent(struct kunit *test)
483 struct clk_multiple_parent_ctx *ctx = test->priv;
484 struct clk_hw *hw = &ctx->hw;
485 struct clk *clk = clk_hw_get_clk(hw, NULL);
486 struct clk *parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
488 KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
495 * Test that for a clock with a multiple parents, clk_has_parent()
496 * actually reports all of them as parents.
499 clk_test_multiple_parents_mux_has_parent(struct kunit *test)
501 struct clk_multiple_parent_ctx *ctx = test->priv;
502 struct clk_hw *hw = &ctx->hw;
503 struct clk *clk = clk_hw_get_clk(hw, NULL);
506 parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
507 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
510 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
511 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
518 * Test that for a clock with a multiple parents, if we set a range on
519 * that clock and the parent is changed, its rate after the reparenting
520 * is still within the range we asked for.
522 * FIXME: clk_set_parent() only does the reparenting but doesn't
523 * reevaluate whether the new clock rate is within its boundaries or
527 clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
529 struct clk_multiple_parent_ctx *ctx = test->priv;
530 struct clk_hw *hw = &ctx->hw;
531 struct clk *clk = clk_hw_get_clk(hw, NULL);
532 struct clk *parent1, *parent2;
536 kunit_skip(test, "This needs to be fixed in the core.");
538 parent1 = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
539 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent1);
540 KUNIT_ASSERT_TRUE(test, clk_is_match(clk_get_parent(clk), parent1));
542 parent2 = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
543 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent2);
545 ret = clk_set_rate(parent1, DUMMY_CLOCK_RATE_1);
546 KUNIT_ASSERT_EQ(test, ret, 0);
548 ret = clk_set_rate(parent2, DUMMY_CLOCK_RATE_2);
549 KUNIT_ASSERT_EQ(test, ret, 0);
551 ret = clk_set_rate_range(clk,
552 DUMMY_CLOCK_RATE_1 - 1000,
553 DUMMY_CLOCK_RATE_1 + 1000);
554 KUNIT_ASSERT_EQ(test, ret, 0);
556 ret = clk_set_parent(clk, parent2);
557 KUNIT_ASSERT_EQ(test, ret, 0);
559 rate = clk_get_rate(clk);
560 KUNIT_ASSERT_GT(test, rate, 0);
561 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
562 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
569 static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
570 KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
571 KUNIT_CASE(clk_test_multiple_parents_mux_has_parent),
572 KUNIT_CASE(clk_test_multiple_parents_mux_set_range_set_parent_get_rate),
577 * Test suite for a basic mux clock with two parents, with
578 * CLK_SET_RATE_PARENT on the child.
580 * These tests exercise the consumer API and check that the state of the
581 * child and parents are sane and consistent.
583 static struct kunit_suite
584 clk_multiple_parents_mux_test_suite = {
585 .name = "clk-multiple-parents-mux-test",
586 .init = clk_multiple_parents_mux_test_init,
587 .exit = clk_multiple_parents_mux_test_exit,
588 .test_cases = clk_multiple_parents_mux_test_cases,
592 clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test)
594 struct clk_multiple_parent_ctx *ctx;
595 const char *parents[2] = { "missing-parent", "proper-parent"};
598 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
603 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("proper-parent",
606 ctx->parents_ctx[1].rate = DUMMY_CLOCK_INIT_RATE;
607 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
611 ctx->hw.init = CLK_HW_INIT_PARENTS("test-orphan-mux", parents,
612 &clk_multiple_parents_mux_ops,
613 CLK_SET_RATE_PARENT);
614 ret = clk_hw_register(NULL, &ctx->hw);
622 clk_orphan_transparent_multiple_parent_mux_test_exit(struct kunit *test)
624 struct clk_multiple_parent_ctx *ctx = test->priv;
626 clk_hw_unregister(&ctx->hw);
627 clk_hw_unregister(&ctx->parents_ctx[1].hw);
631 * Test that, for a mux whose current parent hasn't been registered yet and is
632 * thus orphan, clk_get_parent() will return NULL.
635 clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit *test)
637 struct clk_multiple_parent_ctx *ctx = test->priv;
638 struct clk_hw *hw = &ctx->hw;
639 struct clk *clk = clk_hw_get_clk(hw, NULL);
641 KUNIT_EXPECT_PTR_EQ(test, clk_get_parent(clk), NULL);
647 * Test that, for a mux whose current parent hasn't been registered yet,
648 * calling clk_set_parent() to a valid parent will properly update the
649 * mux parent and its orphan status.
652 clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit *test)
654 struct clk_multiple_parent_ctx *ctx = test->priv;
655 struct clk_hw *hw = &ctx->hw;
656 struct clk *clk = clk_hw_get_clk(hw, NULL);
657 struct clk *parent, *new_parent;
660 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
661 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
663 ret = clk_set_parent(clk, parent);
664 KUNIT_ASSERT_EQ(test, ret, 0);
666 new_parent = clk_get_parent(clk);
667 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
668 KUNIT_EXPECT_TRUE(test, clk_is_match(parent, new_parent));
675 * Test that, for a mux that started orphan but got switched to a valid
676 * parent, calling clk_drop_range() on the mux won't affect the parent
680 clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit *test)
682 struct clk_multiple_parent_ctx *ctx = test->priv;
683 struct clk_hw *hw = &ctx->hw;
684 struct clk *clk = clk_hw_get_clk(hw, NULL);
686 unsigned long parent_rate, new_parent_rate;
689 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
690 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
692 parent_rate = clk_get_rate(parent);
693 KUNIT_ASSERT_GT(test, parent_rate, 0);
695 ret = clk_set_parent(clk, parent);
696 KUNIT_ASSERT_EQ(test, ret, 0);
698 ret = clk_drop_range(clk);
699 KUNIT_ASSERT_EQ(test, ret, 0);
701 new_parent_rate = clk_get_rate(clk);
702 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
703 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
710 * Test that, for a mux that started orphan but got switched to a valid
711 * parent, the rate of the mux and its new parent are consistent.
714 clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit *test)
716 struct clk_multiple_parent_ctx *ctx = test->priv;
717 struct clk_hw *hw = &ctx->hw;
718 struct clk *clk = clk_hw_get_clk(hw, NULL);
720 unsigned long parent_rate, rate;
723 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
724 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
726 parent_rate = clk_get_rate(parent);
727 KUNIT_ASSERT_GT(test, parent_rate, 0);
729 ret = clk_set_parent(clk, parent);
730 KUNIT_ASSERT_EQ(test, ret, 0);
732 rate = clk_get_rate(clk);
733 KUNIT_ASSERT_GT(test, rate, 0);
734 KUNIT_EXPECT_EQ(test, parent_rate, rate);
741 * Test that, for a mux that started orphan but got switched to a valid
742 * parent, calling clk_put() on the mux won't affect the parent rate.
745 clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit *test)
747 struct clk_multiple_parent_ctx *ctx = test->priv;
748 struct clk *clk, *parent;
749 unsigned long parent_rate, new_parent_rate;
752 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
753 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
755 clk = clk_hw_get_clk(&ctx->hw, NULL);
756 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
758 parent_rate = clk_get_rate(parent);
759 KUNIT_ASSERT_GT(test, parent_rate, 0);
761 ret = clk_set_parent(clk, parent);
762 KUNIT_ASSERT_EQ(test, ret, 0);
766 new_parent_rate = clk_get_rate(parent);
767 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
768 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
774 * Test that, for a mux that started orphan but got switched to a valid
775 * parent, calling clk_set_rate_range() will affect the parent state if
776 * its rate is out of range.
779 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit *test)
781 struct clk_multiple_parent_ctx *ctx = test->priv;
782 struct clk_hw *hw = &ctx->hw;
783 struct clk *clk = clk_hw_get_clk(hw, NULL);
788 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
789 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
791 ret = clk_set_parent(clk, parent);
792 KUNIT_ASSERT_EQ(test, ret, 0);
794 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
795 KUNIT_ASSERT_EQ(test, ret, 0);
797 rate = clk_get_rate(clk);
798 KUNIT_ASSERT_GT(test, rate, 0);
799 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
800 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
807 * Test that, for a mux that started orphan but got switched to a valid
808 * parent, calling clk_set_rate_range() won't affect the parent state if
809 * its rate is within range.
812 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit *test)
814 struct clk_multiple_parent_ctx *ctx = test->priv;
815 struct clk_hw *hw = &ctx->hw;
816 struct clk *clk = clk_hw_get_clk(hw, NULL);
818 unsigned long parent_rate, new_parent_rate;
821 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
822 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
824 parent_rate = clk_get_rate(parent);
825 KUNIT_ASSERT_GT(test, parent_rate, 0);
827 ret = clk_set_parent(clk, parent);
828 KUNIT_ASSERT_EQ(test, ret, 0);
830 ret = clk_set_rate_range(clk,
831 DUMMY_CLOCK_INIT_RATE - 1000,
832 DUMMY_CLOCK_INIT_RATE + 1000);
833 KUNIT_ASSERT_EQ(test, ret, 0);
835 new_parent_rate = clk_get_rate(parent);
836 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
837 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
844 * Test that, for a mux whose current parent hasn't been registered yet,
845 * calling clk_set_rate_range() will succeed, and will be taken into
846 * account when rounding a rate.
849 clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit *test)
851 struct clk_multiple_parent_ctx *ctx = test->priv;
852 struct clk_hw *hw = &ctx->hw;
853 struct clk *clk = clk_hw_get_clk(hw, NULL);
857 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
858 KUNIT_ASSERT_EQ(test, ret, 0);
860 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
861 KUNIT_ASSERT_GT(test, rate, 0);
862 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
863 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
869 * Test that, for a mux that started orphan, was assigned and rate and
870 * then got switched to a valid parent, its rate is eventually within
873 * FIXME: Even though we update the rate as part of clk_set_parent(), we
874 * don't evaluate whether that new rate is within range and needs to be
878 clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit *test)
880 struct clk_multiple_parent_ctx *ctx = test->priv;
881 struct clk_hw *hw = &ctx->hw;
882 struct clk *clk = clk_hw_get_clk(hw, NULL);
887 kunit_skip(test, "This needs to be fixed in the core.");
889 clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
891 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
892 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
894 ret = clk_set_parent(clk, parent);
895 KUNIT_ASSERT_EQ(test, ret, 0);
897 rate = clk_get_rate(clk);
898 KUNIT_ASSERT_GT(test, rate, 0);
899 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
900 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
906 static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = {
907 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_get_parent),
908 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent),
909 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range),
910 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate),
911 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_put),
912 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified),
913 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched),
914 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate),
915 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate),
920 * Test suite for a basic mux clock with two parents. The default parent
921 * isn't registered, only the second parent is. By default, the clock
922 * will thus be orphan.
924 * These tests exercise the behaviour of the consumer API when dealing
925 * with an orphan clock, and how we deal with the transition to a valid
928 static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = {
929 .name = "clk-orphan-transparent-multiple-parent-mux-test",
930 .init = clk_orphan_transparent_multiple_parent_mux_test_init,
931 .exit = clk_orphan_transparent_multiple_parent_mux_test_exit,
932 .test_cases = clk_orphan_transparent_multiple_parent_mux_test_cases,
935 struct clk_single_parent_ctx {
936 struct clk_dummy_context parent_ctx;
940 static int clk_single_parent_mux_test_init(struct kunit *test)
942 struct clk_single_parent_ctx *ctx;
945 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
950 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
951 ctx->parent_ctx.hw.init =
952 CLK_HW_INIT_NO_PARENT("parent-clk",
956 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
960 ctx->hw.init = CLK_HW_INIT("test-clk", "parent-clk",
961 &clk_dummy_single_parent_ops,
962 CLK_SET_RATE_PARENT);
964 ret = clk_hw_register(NULL, &ctx->hw);
972 clk_single_parent_mux_test_exit(struct kunit *test)
974 struct clk_single_parent_ctx *ctx = test->priv;
976 clk_hw_unregister(&ctx->hw);
977 clk_hw_unregister(&ctx->parent_ctx.hw);
981 * Test that for a clock with a single parent, clk_get_parent() actually
982 * returns the parent.
985 clk_test_single_parent_mux_get_parent(struct kunit *test)
987 struct clk_single_parent_ctx *ctx = test->priv;
988 struct clk_hw *hw = &ctx->hw;
989 struct clk *clk = clk_hw_get_clk(hw, NULL);
990 struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
992 KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
999 * Test that for a clock with a single parent, clk_has_parent() actually
1000 * reports it as a parent.
1003 clk_test_single_parent_mux_has_parent(struct kunit *test)
1005 struct clk_single_parent_ctx *ctx = test->priv;
1006 struct clk_hw *hw = &ctx->hw;
1007 struct clk *clk = clk_hw_get_clk(hw, NULL);
1008 struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
1010 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
1017 * Test that for a clock that can't modify its rate and with a single
1018 * parent, if we set disjoints range on the parent and then the child,
1019 * the second will return an error.
1021 * FIXME: clk_set_rate_range() only considers the current clock when
1022 * evaluating whether ranges are disjoints and not the upstream clocks
1026 clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
1028 struct clk_single_parent_ctx *ctx = test->priv;
1029 struct clk_hw *hw = &ctx->hw;
1030 struct clk *clk = clk_hw_get_clk(hw, NULL);
1034 kunit_skip(test, "This needs to be fixed in the core.");
1036 parent = clk_get_parent(clk);
1037 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1039 ret = clk_set_rate_range(parent, 1000, 2000);
1040 KUNIT_ASSERT_EQ(test, ret, 0);
1042 ret = clk_set_rate_range(clk, 3000, 4000);
1043 KUNIT_EXPECT_LT(test, ret, 0);
1049 * Test that for a clock that can't modify its rate and with a single
1050 * parent, if we set disjoints range on the child and then the parent,
1051 * the second will return an error.
1053 * FIXME: clk_set_rate_range() only considers the current clock when
1054 * evaluating whether ranges are disjoints and not the downstream clocks
1058 clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
1060 struct clk_single_parent_ctx *ctx = test->priv;
1061 struct clk_hw *hw = &ctx->hw;
1062 struct clk *clk = clk_hw_get_clk(hw, NULL);
1066 kunit_skip(test, "This needs to be fixed in the core.");
1068 parent = clk_get_parent(clk);
1069 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1071 ret = clk_set_rate_range(clk, 1000, 2000);
1072 KUNIT_ASSERT_EQ(test, ret, 0);
1074 ret = clk_set_rate_range(parent, 3000, 4000);
1075 KUNIT_EXPECT_LT(test, ret, 0);
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 then call
1083 * clk_round_rate(), the boundaries of the parent are taken into
1087 clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test)
1089 struct clk_single_parent_ctx *ctx = test->priv;
1090 struct clk_hw *hw = &ctx->hw;
1091 struct clk *clk = clk_hw_get_clk(hw, NULL);
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 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1103 KUNIT_ASSERT_GT(test, rate, 0);
1104 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1105 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1111 * Test that for a clock that can't modify its rate and with a single
1112 * parent, if we set a range on the parent and a more restrictive one on
1113 * the child, and then call clk_round_rate(), the boundaries of the
1114 * two clocks are taken into account.
1117 clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test)
1119 struct clk_single_parent_ctx *ctx = test->priv;
1120 struct clk_hw *hw = &ctx->hw;
1121 struct clk *clk = clk_hw_get_clk(hw, NULL);
1126 parent = clk_get_parent(clk);
1127 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1129 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1130 KUNIT_ASSERT_EQ(test, ret, 0);
1132 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1133 KUNIT_ASSERT_EQ(test, ret, 0);
1135 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1136 KUNIT_ASSERT_GT(test, rate, 0);
1137 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1138 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1140 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1141 KUNIT_ASSERT_GT(test, rate, 0);
1142 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1143 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1149 * Test that for a clock that can't modify its rate and with a single
1150 * parent, if we set a range on the child and a more restrictive one on
1151 * the parent, and then call clk_round_rate(), the boundaries of the
1152 * two clocks are taken into account.
1155 clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test)
1157 struct clk_single_parent_ctx *ctx = test->priv;
1158 struct clk_hw *hw = &ctx->hw;
1159 struct clk *clk = clk_hw_get_clk(hw, NULL);
1164 parent = clk_get_parent(clk);
1165 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1167 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1168 KUNIT_ASSERT_EQ(test, ret, 0);
1170 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1171 KUNIT_ASSERT_EQ(test, ret, 0);
1173 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1174 KUNIT_ASSERT_GT(test, rate, 0);
1175 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1176 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1178 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1179 KUNIT_ASSERT_GT(test, rate, 0);
1180 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1181 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1186 static struct kunit_case clk_single_parent_mux_test_cases[] = {
1187 KUNIT_CASE(clk_test_single_parent_mux_get_parent),
1188 KUNIT_CASE(clk_test_single_parent_mux_has_parent),
1189 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
1190 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
1191 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),
1192 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only),
1193 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller),
1198 * Test suite for a basic mux clock with one parent, with
1199 * CLK_SET_RATE_PARENT on the child.
1201 * These tests exercise the consumer API and check that the state of the
1202 * child and parent are sane and consistent.
1204 static struct kunit_suite
1205 clk_single_parent_mux_test_suite = {
1206 .name = "clk-single-parent-mux-test",
1207 .init = clk_single_parent_mux_test_init,
1208 .exit = clk_single_parent_mux_test_exit,
1209 .test_cases = clk_single_parent_mux_test_cases,
1212 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
1214 struct clk_single_parent_ctx *ctx;
1215 struct clk_init_data init = { };
1216 const char * const parents[] = { "orphan_parent" };
1219 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1224 init.name = "test_orphan_dummy_parent";
1225 init.ops = &clk_dummy_single_parent_ops;
1226 init.parent_names = parents;
1227 init.num_parents = ARRAY_SIZE(parents);
1228 init.flags = CLK_SET_RATE_PARENT;
1229 ctx->hw.init = &init;
1231 ret = clk_hw_register(NULL, &ctx->hw);
1235 memset(&init, 0, sizeof(init));
1236 init.name = "orphan_parent";
1237 init.ops = &clk_dummy_rate_ops;
1238 ctx->parent_ctx.hw.init = &init;
1239 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1241 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1249 * Test that a mux-only clock, with an initial rate within a range,
1250 * will still have the same rate after the range has been enforced.
1253 * https://lore.kernel.org/linux-clk/7720158d-10a7-a17b-73a4-a8615c9c6d5c@collabora.com/
1255 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
1257 struct clk_single_parent_ctx *ctx = test->priv;
1258 struct clk_hw *hw = &ctx->hw;
1259 struct clk *clk = clk_hw_get_clk(hw, NULL);
1260 unsigned long rate, new_rate;
1262 rate = clk_get_rate(clk);
1263 KUNIT_ASSERT_GT(test, rate, 0);
1265 KUNIT_ASSERT_EQ(test,
1266 clk_set_rate_range(clk,
1267 ctx->parent_ctx.rate - 1000,
1268 ctx->parent_ctx.rate + 1000),
1271 new_rate = clk_get_rate(clk);
1272 KUNIT_ASSERT_GT(test, new_rate, 0);
1273 KUNIT_EXPECT_EQ(test, rate, new_rate);
1278 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
1279 KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
1284 * Test suite for a basic mux clock with one parent. The parent is
1285 * registered after its child. The clock will thus be an orphan when
1286 * registered, but will no longer be when the tests run.
1288 * These tests make sure a clock that used to be orphan has a sane,
1289 * consistent, behaviour.
1291 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
1292 .name = "clk-orphan-transparent-single-parent-test",
1293 .init = clk_orphan_transparent_single_parent_mux_test_init,
1294 .exit = clk_single_parent_mux_test_exit,
1295 .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
1298 struct clk_single_parent_two_lvl_ctx {
1299 struct clk_dummy_context parent_parent_ctx;
1300 struct clk_dummy_context parent_ctx;
1305 clk_orphan_two_level_root_last_test_init(struct kunit *test)
1307 struct clk_single_parent_two_lvl_ctx *ctx;
1310 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1315 ctx->parent_ctx.hw.init =
1316 CLK_HW_INIT("intermediate-parent",
1318 &clk_dummy_single_parent_ops,
1319 CLK_SET_RATE_PARENT);
1320 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1325 CLK_HW_INIT("test-clk", "intermediate-parent",
1326 &clk_dummy_single_parent_ops,
1327 CLK_SET_RATE_PARENT);
1328 ret = clk_hw_register(NULL, &ctx->hw);
1332 ctx->parent_parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1333 ctx->parent_parent_ctx.hw.init =
1334 CLK_HW_INIT_NO_PARENT("root-parent",
1335 &clk_dummy_rate_ops,
1337 ret = clk_hw_register(NULL, &ctx->parent_parent_ctx.hw);
1345 clk_orphan_two_level_root_last_test_exit(struct kunit *test)
1347 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1349 clk_hw_unregister(&ctx->hw);
1350 clk_hw_unregister(&ctx->parent_ctx.hw);
1351 clk_hw_unregister(&ctx->parent_parent_ctx.hw);
1355 * Test that, for a clock whose parent used to be orphan, clk_get_rate()
1356 * will return the proper rate.
1359 clk_orphan_two_level_root_last_test_get_rate(struct kunit *test)
1361 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1362 struct clk_hw *hw = &ctx->hw;
1363 struct clk *clk = clk_hw_get_clk(hw, NULL);
1366 rate = clk_get_rate(clk);
1367 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1373 * Test that, for a clock whose parent used to be orphan,
1374 * clk_set_rate_range() won't affect its rate if it is already within
1377 * See (for Exynos 4210):
1378 * https://lore.kernel.org/linux-clk/366a0232-bb4a-c357-6aa8-636e398e05eb@samsung.com/
1381 clk_orphan_two_level_root_last_test_set_range(struct kunit *test)
1383 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1384 struct clk_hw *hw = &ctx->hw;
1385 struct clk *clk = clk_hw_get_clk(hw, NULL);
1389 ret = clk_set_rate_range(clk,
1390 DUMMY_CLOCK_INIT_RATE - 1000,
1391 DUMMY_CLOCK_INIT_RATE + 1000);
1392 KUNIT_ASSERT_EQ(test, ret, 0);
1394 rate = clk_get_rate(clk);
1395 KUNIT_ASSERT_GT(test, rate, 0);
1396 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1401 static struct kunit_case
1402 clk_orphan_two_level_root_last_test_cases[] = {
1403 KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate),
1404 KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range),
1409 * Test suite for a basic, transparent, clock with a parent that is also
1410 * such a clock. The parent's parent is registered last, while the
1411 * parent and its child are registered in that order. The intermediate
1412 * and leaf clocks will thus be orphan when registered, but the leaf
1413 * clock itself will always have its parent and will never be
1414 * reparented. Indeed, it's only orphan because its parent is.
1416 * These tests exercise the behaviour of the consumer API when dealing
1417 * with an orphan clock, and how we deal with the transition to a valid
1420 static struct kunit_suite
1421 clk_orphan_two_level_root_last_test_suite = {
1422 .name = "clk-orphan-two-level-root-last-test",
1423 .init = clk_orphan_two_level_root_last_test_init,
1424 .exit = clk_orphan_two_level_root_last_test_exit,
1425 .test_cases = clk_orphan_two_level_root_last_test_cases,
1429 * Test that clk_set_rate_range won't return an error for a valid range
1430 * and that it will make sure the rate of the clock is within the
1433 static void clk_range_test_set_range(struct kunit *test)
1435 struct clk_dummy_context *ctx = test->priv;
1436 struct clk_hw *hw = &ctx->hw;
1437 struct clk *clk = clk_hw_get_clk(hw, NULL);
1440 KUNIT_ASSERT_EQ(test,
1441 clk_set_rate_range(clk,
1443 DUMMY_CLOCK_RATE_2),
1446 rate = clk_get_rate(clk);
1447 KUNIT_ASSERT_GT(test, rate, 0);
1448 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1449 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1455 * Test that calling clk_set_rate_range with a minimum rate higher than
1456 * the maximum rate returns an error.
1458 static void clk_range_test_set_range_invalid(struct kunit *test)
1460 struct clk_dummy_context *ctx = test->priv;
1461 struct clk_hw *hw = &ctx->hw;
1462 struct clk *clk = clk_hw_get_clk(hw, NULL);
1464 KUNIT_EXPECT_LT(test,
1465 clk_set_rate_range(clk,
1466 DUMMY_CLOCK_RATE_1 + 1000,
1467 DUMMY_CLOCK_RATE_1),
1474 * Test that users can't set multiple, disjoints, range that would be
1475 * impossible to meet.
1477 static void clk_range_test_multiple_disjoints_range(struct kunit *test)
1479 struct clk_dummy_context *ctx = test->priv;
1480 struct clk_hw *hw = &ctx->hw;
1481 struct clk *user1, *user2;
1483 user1 = clk_hw_get_clk(hw, NULL);
1484 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1486 user2 = clk_hw_get_clk(hw, NULL);
1487 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1489 KUNIT_ASSERT_EQ(test,
1490 clk_set_rate_range(user1, 1000, 2000),
1493 KUNIT_EXPECT_LT(test,
1494 clk_set_rate_range(user2, 3000, 4000),
1502 * Test that if our clock has some boundaries and we try to round a rate
1503 * lower than the minimum, the returned rate will be within range.
1505 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
1507 struct clk_dummy_context *ctx = test->priv;
1508 struct clk_hw *hw = &ctx->hw;
1509 struct clk *clk = clk_hw_get_clk(hw, NULL);
1512 KUNIT_ASSERT_EQ(test,
1513 clk_set_rate_range(clk,
1515 DUMMY_CLOCK_RATE_2),
1518 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1519 KUNIT_ASSERT_GT(test, rate, 0);
1520 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1521 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1527 * Test that if our clock has some boundaries and we try to set a rate
1528 * higher than the maximum, the new rate will be within range.
1530 static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
1532 struct clk_dummy_context *ctx = test->priv;
1533 struct clk_hw *hw = &ctx->hw;
1534 struct clk *clk = clk_hw_get_clk(hw, NULL);
1537 KUNIT_ASSERT_EQ(test,
1538 clk_set_rate_range(clk,
1540 DUMMY_CLOCK_RATE_2),
1543 KUNIT_ASSERT_EQ(test,
1544 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1547 rate = clk_get_rate(clk);
1548 KUNIT_ASSERT_GT(test, rate, 0);
1549 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1550 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1556 * Test that if our clock has some boundaries and we try to round and
1557 * set a rate lower than the minimum, the rate returned by
1558 * clk_round_rate() will be consistent with the new rate set by
1561 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
1563 struct clk_dummy_context *ctx = test->priv;
1564 struct clk_hw *hw = &ctx->hw;
1565 struct clk *clk = clk_hw_get_clk(hw, NULL);
1568 KUNIT_ASSERT_EQ(test,
1569 clk_set_rate_range(clk,
1571 DUMMY_CLOCK_RATE_2),
1574 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1575 KUNIT_ASSERT_GT(test, rounded, 0);
1577 KUNIT_ASSERT_EQ(test,
1578 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1581 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1587 * Test that if our clock has some boundaries and we try to round a rate
1588 * higher than the maximum, the returned rate will be within range.
1590 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
1592 struct clk_dummy_context *ctx = test->priv;
1593 struct clk_hw *hw = &ctx->hw;
1594 struct clk *clk = clk_hw_get_clk(hw, NULL);
1597 KUNIT_ASSERT_EQ(test,
1598 clk_set_rate_range(clk,
1600 DUMMY_CLOCK_RATE_2),
1603 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1604 KUNIT_ASSERT_GT(test, rate, 0);
1605 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1606 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1612 * Test that if our clock has some boundaries and we try to set a rate
1613 * higher than the maximum, the new rate will be within range.
1615 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
1617 struct clk_dummy_context *ctx = test->priv;
1618 struct clk_hw *hw = &ctx->hw;
1619 struct clk *clk = clk_hw_get_clk(hw, NULL);
1622 KUNIT_ASSERT_EQ(test,
1623 clk_set_rate_range(clk,
1625 DUMMY_CLOCK_RATE_2),
1628 KUNIT_ASSERT_EQ(test,
1629 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1632 rate = clk_get_rate(clk);
1633 KUNIT_ASSERT_GT(test, rate, 0);
1634 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1635 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1641 * Test that if our clock has some boundaries and we try to round and
1642 * set a rate higher than the maximum, the rate returned by
1643 * clk_round_rate() will be consistent with the new rate set by
1646 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
1648 struct clk_dummy_context *ctx = test->priv;
1649 struct clk_hw *hw = &ctx->hw;
1650 struct clk *clk = clk_hw_get_clk(hw, NULL);
1653 KUNIT_ASSERT_EQ(test,
1654 clk_set_rate_range(clk,
1656 DUMMY_CLOCK_RATE_2),
1659 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1660 KUNIT_ASSERT_GT(test, rounded, 0);
1662 KUNIT_ASSERT_EQ(test,
1663 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1666 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1672 * Test that if our clock has a rate lower than the minimum set by a
1673 * call to clk_set_rate_range(), the rate will be raised to match the
1676 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1677 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1679 static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
1681 struct clk_dummy_context *ctx = test->priv;
1682 struct clk_hw *hw = &ctx->hw;
1683 struct clk *clk = clk_hw_get_clk(hw, NULL);
1686 KUNIT_ASSERT_EQ(test,
1687 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1690 KUNIT_ASSERT_EQ(test,
1691 clk_set_rate_range(clk,
1693 DUMMY_CLOCK_RATE_2),
1696 rate = clk_get_rate(clk);
1697 KUNIT_ASSERT_GT(test, rate, 0);
1698 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1704 * Test that if our clock has a rate higher than the maximum set by a
1705 * call to clk_set_rate_range(), the rate will be lowered to match the
1708 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1709 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1711 static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
1713 struct clk_dummy_context *ctx = test->priv;
1714 struct clk_hw *hw = &ctx->hw;
1715 struct clk *clk = clk_hw_get_clk(hw, NULL);
1718 KUNIT_ASSERT_EQ(test,
1719 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1722 KUNIT_ASSERT_EQ(test,
1723 clk_set_rate_range(clk,
1725 DUMMY_CLOCK_RATE_2),
1728 rate = clk_get_rate(clk);
1729 KUNIT_ASSERT_GT(test, rate, 0);
1730 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1735 static struct kunit_case clk_range_test_cases[] = {
1736 KUNIT_CASE(clk_range_test_set_range),
1737 KUNIT_CASE(clk_range_test_set_range_invalid),
1738 KUNIT_CASE(clk_range_test_multiple_disjoints_range),
1739 KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
1740 KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
1741 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
1742 KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
1743 KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
1744 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
1745 KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
1746 KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
1751 * Test suite for a basic rate clock, without any parent.
1753 * These tests exercise the rate range API: clk_set_rate_range(),
1754 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range().
1756 static struct kunit_suite clk_range_test_suite = {
1757 .name = "clk-range-test",
1758 .init = clk_test_init,
1759 .exit = clk_test_exit,
1760 .test_cases = clk_range_test_cases,
1764 * Test that if we have several subsequent calls to
1765 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1766 * needed each and every time.
1768 * With clk_dummy_maximize_rate_ops, this means that the rate will
1769 * trail along the maximum as it evolves.
1771 static void clk_range_test_set_range_rate_maximized(struct kunit *test)
1773 struct clk_dummy_context *ctx = test->priv;
1774 struct clk_hw *hw = &ctx->hw;
1775 struct clk *clk = clk_hw_get_clk(hw, NULL);
1778 KUNIT_ASSERT_EQ(test,
1779 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1782 KUNIT_ASSERT_EQ(test,
1783 clk_set_rate_range(clk,
1785 DUMMY_CLOCK_RATE_2),
1788 rate = clk_get_rate(clk);
1789 KUNIT_ASSERT_GT(test, rate, 0);
1790 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1792 KUNIT_ASSERT_EQ(test,
1793 clk_set_rate_range(clk,
1795 DUMMY_CLOCK_RATE_2 - 1000),
1798 rate = clk_get_rate(clk);
1799 KUNIT_ASSERT_GT(test, rate, 0);
1800 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1802 KUNIT_ASSERT_EQ(test,
1803 clk_set_rate_range(clk,
1805 DUMMY_CLOCK_RATE_2),
1808 rate = clk_get_rate(clk);
1809 KUNIT_ASSERT_GT(test, rate, 0);
1810 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1816 * Test that if we have several subsequent calls to
1817 * clk_set_rate_range(), across multiple users, the core will reevaluate
1818 * whether a new rate is needed each and every time.
1820 * With clk_dummy_maximize_rate_ops, this means that the rate will
1821 * trail along the maximum as it evolves.
1823 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
1825 struct clk_dummy_context *ctx = test->priv;
1826 struct clk_hw *hw = &ctx->hw;
1827 struct clk *clk = clk_hw_get_clk(hw, NULL);
1828 struct clk *user1, *user2;
1831 user1 = clk_hw_get_clk(hw, NULL);
1832 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1834 user2 = clk_hw_get_clk(hw, NULL);
1835 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1837 KUNIT_ASSERT_EQ(test,
1838 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1841 KUNIT_ASSERT_EQ(test,
1842 clk_set_rate_range(user1,
1844 DUMMY_CLOCK_RATE_2),
1847 rate = clk_get_rate(clk);
1848 KUNIT_ASSERT_GT(test, rate, 0);
1849 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1851 KUNIT_ASSERT_EQ(test,
1852 clk_set_rate_range(user2,
1854 DUMMY_CLOCK_RATE_1),
1857 rate = clk_get_rate(clk);
1858 KUNIT_ASSERT_GT(test, rate, 0);
1859 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1861 KUNIT_ASSERT_EQ(test,
1862 clk_drop_range(user2),
1865 rate = clk_get_rate(clk);
1866 KUNIT_ASSERT_GT(test, rate, 0);
1867 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1875 * Test that if we have several subsequent calls to
1876 * clk_set_rate_range(), across multiple users, the core will reevaluate
1877 * whether a new rate is needed, including when a user drop its clock.
1879 * With clk_dummy_maximize_rate_ops, this means that the rate will
1880 * trail along the maximum as it evolves.
1882 static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
1884 struct clk_dummy_context *ctx = test->priv;
1885 struct clk_hw *hw = &ctx->hw;
1886 struct clk *clk = clk_hw_get_clk(hw, NULL);
1887 struct clk *user1, *user2;
1890 user1 = clk_hw_get_clk(hw, NULL);
1891 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1893 user2 = clk_hw_get_clk(hw, NULL);
1894 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1896 KUNIT_ASSERT_EQ(test,
1897 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1900 KUNIT_ASSERT_EQ(test,
1901 clk_set_rate_range(user1,
1903 DUMMY_CLOCK_RATE_2),
1906 rate = clk_get_rate(clk);
1907 KUNIT_ASSERT_GT(test, rate, 0);
1908 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1910 KUNIT_ASSERT_EQ(test,
1911 clk_set_rate_range(user2,
1913 DUMMY_CLOCK_RATE_1),
1916 rate = clk_get_rate(clk);
1917 KUNIT_ASSERT_GT(test, rate, 0);
1918 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1922 rate = clk_get_rate(clk);
1923 KUNIT_ASSERT_GT(test, rate, 0);
1924 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1930 static struct kunit_case clk_range_maximize_test_cases[] = {
1931 KUNIT_CASE(clk_range_test_set_range_rate_maximized),
1932 KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
1933 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
1938 * Test suite for a basic rate clock, without any parent.
1940 * These tests exercise the rate range API: clk_set_rate_range(),
1941 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
1942 * driver that will always try to run at the highest possible rate.
1944 static struct kunit_suite clk_range_maximize_test_suite = {
1945 .name = "clk-range-maximize-test",
1946 .init = clk_maximize_test_init,
1947 .exit = clk_test_exit,
1948 .test_cases = clk_range_maximize_test_cases,
1952 * Test that if we have several subsequent calls to
1953 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1954 * needed each and every time.
1956 * With clk_dummy_minimize_rate_ops, this means that the rate will
1957 * trail along the minimum as it evolves.
1959 static void clk_range_test_set_range_rate_minimized(struct kunit *test)
1961 struct clk_dummy_context *ctx = test->priv;
1962 struct clk_hw *hw = &ctx->hw;
1963 struct clk *clk = clk_hw_get_clk(hw, NULL);
1966 KUNIT_ASSERT_EQ(test,
1967 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1970 KUNIT_ASSERT_EQ(test,
1971 clk_set_rate_range(clk,
1973 DUMMY_CLOCK_RATE_2),
1976 rate = clk_get_rate(clk);
1977 KUNIT_ASSERT_GT(test, rate, 0);
1978 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1980 KUNIT_ASSERT_EQ(test,
1981 clk_set_rate_range(clk,
1982 DUMMY_CLOCK_RATE_1 + 1000,
1983 DUMMY_CLOCK_RATE_2),
1986 rate = clk_get_rate(clk);
1987 KUNIT_ASSERT_GT(test, rate, 0);
1988 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1990 KUNIT_ASSERT_EQ(test,
1991 clk_set_rate_range(clk,
1993 DUMMY_CLOCK_RATE_2),
1996 rate = clk_get_rate(clk);
1997 KUNIT_ASSERT_GT(test, rate, 0);
1998 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2004 * Test that if we have several subsequent calls to
2005 * clk_set_rate_range(), across multiple users, the core will reevaluate
2006 * whether a new rate is needed each and every time.
2008 * With clk_dummy_minimize_rate_ops, this means that the rate will
2009 * trail along the minimum as it evolves.
2011 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
2013 struct clk_dummy_context *ctx = test->priv;
2014 struct clk_hw *hw = &ctx->hw;
2015 struct clk *clk = clk_hw_get_clk(hw, NULL);
2016 struct clk *user1, *user2;
2019 user1 = clk_hw_get_clk(hw, NULL);
2020 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2022 user2 = clk_hw_get_clk(hw, NULL);
2023 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2025 KUNIT_ASSERT_EQ(test,
2026 clk_set_rate_range(user1,
2031 rate = clk_get_rate(clk);
2032 KUNIT_ASSERT_GT(test, rate, 0);
2033 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2035 KUNIT_ASSERT_EQ(test,
2036 clk_set_rate_range(user2,
2041 rate = clk_get_rate(clk);
2042 KUNIT_ASSERT_GT(test, rate, 0);
2043 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2045 KUNIT_ASSERT_EQ(test,
2046 clk_drop_range(user2),
2049 rate = clk_get_rate(clk);
2050 KUNIT_ASSERT_GT(test, rate, 0);
2051 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2059 * Test that if we have several subsequent calls to
2060 * clk_set_rate_range(), across multiple users, the core will reevaluate
2061 * whether a new rate is needed, including when a user drop its clock.
2063 * With clk_dummy_minimize_rate_ops, this means that the rate will
2064 * trail along the minimum as it evolves.
2066 static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
2068 struct clk_dummy_context *ctx = test->priv;
2069 struct clk_hw *hw = &ctx->hw;
2070 struct clk *clk = clk_hw_get_clk(hw, NULL);
2071 struct clk *user1, *user2;
2074 user1 = clk_hw_get_clk(hw, NULL);
2075 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2077 user2 = clk_hw_get_clk(hw, NULL);
2078 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2080 KUNIT_ASSERT_EQ(test,
2081 clk_set_rate_range(user1,
2086 rate = clk_get_rate(clk);
2087 KUNIT_ASSERT_GT(test, rate, 0);
2088 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2090 KUNIT_ASSERT_EQ(test,
2091 clk_set_rate_range(user2,
2096 rate = clk_get_rate(clk);
2097 KUNIT_ASSERT_GT(test, rate, 0);
2098 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2102 rate = clk_get_rate(clk);
2103 KUNIT_ASSERT_GT(test, rate, 0);
2104 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2110 static struct kunit_case clk_range_minimize_test_cases[] = {
2111 KUNIT_CASE(clk_range_test_set_range_rate_minimized),
2112 KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
2113 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
2118 * Test suite for a basic rate clock, without any parent.
2120 * These tests exercise the rate range API: clk_set_rate_range(),
2121 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
2122 * driver that will always try to run at the lowest possible rate.
2124 static struct kunit_suite clk_range_minimize_test_suite = {
2125 .name = "clk-range-minimize-test",
2126 .init = clk_minimize_test_init,
2127 .exit = clk_test_exit,
2128 .test_cases = clk_range_minimize_test_cases,
2131 struct clk_leaf_mux_ctx {
2132 struct clk_multiple_parent_ctx mux_ctx;
2137 clk_leaf_mux_set_rate_parent_test_init(struct kunit *test)
2139 struct clk_leaf_mux_ctx *ctx;
2140 const char *top_parents[2] = { "parent-0", "parent-1" };
2143 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2148 ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2149 &clk_dummy_rate_ops,
2151 ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2152 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2156 ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2157 &clk_dummy_rate_ops,
2159 ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2160 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2164 ctx->mux_ctx.current_parent = 0;
2165 ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2166 &clk_multiple_parents_mux_ops,
2168 ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2172 ctx->hw.init = CLK_HW_INIT_HW("test-clock", &ctx->mux_ctx.hw,
2173 &clk_dummy_single_parent_ops,
2174 CLK_SET_RATE_PARENT);
2175 ret = clk_hw_register(NULL, &ctx->hw);
2182 static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test)
2184 struct clk_leaf_mux_ctx *ctx = test->priv;
2186 clk_hw_unregister(&ctx->hw);
2187 clk_hw_unregister(&ctx->mux_ctx.hw);
2188 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2189 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2193 * Test that, for a clock that will forward any rate request to its
2194 * parent, the rate request structure returned by __clk_determine_rate
2195 * is sane and will be what we expect.
2197 static void clk_leaf_mux_set_rate_parent_determine_rate(struct kunit *test)
2199 struct clk_leaf_mux_ctx *ctx = test->priv;
2200 struct clk_hw *hw = &ctx->hw;
2201 struct clk *clk = clk_hw_get_clk(hw, NULL);
2202 struct clk_rate_request req;
2206 rate = clk_get_rate(clk);
2207 KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2209 clk_hw_init_rate_request(hw, &req, DUMMY_CLOCK_RATE_2);
2211 ret = __clk_determine_rate(hw, &req);
2212 KUNIT_ASSERT_EQ(test, ret, 0);
2214 KUNIT_EXPECT_EQ(test, req.rate, DUMMY_CLOCK_RATE_2);
2215 KUNIT_EXPECT_EQ(test, req.best_parent_rate, DUMMY_CLOCK_RATE_2);
2216 KUNIT_EXPECT_PTR_EQ(test, req.best_parent_hw, &ctx->mux_ctx.hw);
2221 static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = {
2222 KUNIT_CASE(clk_leaf_mux_set_rate_parent_determine_rate),
2227 * Test suite for a clock whose parent is a mux with multiple parents.
2228 * The leaf clock has CLK_SET_RATE_PARENT, and will forward rate
2229 * requests to the mux, which will then select which parent is the best
2230 * fit for a given rate.
2232 * These tests exercise the behaviour of muxes, and the proper selection
2235 static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = {
2236 .name = "clk-leaf-mux-set-rate-parent",
2237 .init = clk_leaf_mux_set_rate_parent_test_init,
2238 .exit = clk_leaf_mux_set_rate_parent_test_exit,
2239 .test_cases = clk_leaf_mux_set_rate_parent_test_cases,
2242 struct clk_mux_notifier_rate_change {
2244 unsigned long old_rate;
2245 unsigned long new_rate;
2246 wait_queue_head_t wq;
2249 struct clk_mux_notifier_ctx {
2250 struct clk_multiple_parent_ctx mux_ctx;
2252 struct notifier_block clk_nb;
2253 struct clk_mux_notifier_rate_change pre_rate_change;
2254 struct clk_mux_notifier_rate_change post_rate_change;
2257 #define NOTIFIER_TIMEOUT_MS 100
2259 static int clk_mux_notifier_callback(struct notifier_block *nb,
2260 unsigned long action, void *data)
2262 struct clk_notifier_data *clk_data = data;
2263 struct clk_mux_notifier_ctx *ctx = container_of(nb,
2264 struct clk_mux_notifier_ctx,
2267 if (action & PRE_RATE_CHANGE) {
2268 ctx->pre_rate_change.old_rate = clk_data->old_rate;
2269 ctx->pre_rate_change.new_rate = clk_data->new_rate;
2270 ctx->pre_rate_change.done = true;
2271 wake_up_interruptible(&ctx->pre_rate_change.wq);
2274 if (action & POST_RATE_CHANGE) {
2275 ctx->post_rate_change.old_rate = clk_data->old_rate;
2276 ctx->post_rate_change.new_rate = clk_data->new_rate;
2277 ctx->post_rate_change.done = true;
2278 wake_up_interruptible(&ctx->post_rate_change.wq);
2284 static int clk_mux_notifier_test_init(struct kunit *test)
2286 struct clk_mux_notifier_ctx *ctx;
2287 const char *top_parents[2] = { "parent-0", "parent-1" };
2290 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2294 ctx->clk_nb.notifier_call = clk_mux_notifier_callback;
2295 init_waitqueue_head(&ctx->pre_rate_change.wq);
2296 init_waitqueue_head(&ctx->post_rate_change.wq);
2298 ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2299 &clk_dummy_rate_ops,
2301 ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2302 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2306 ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2307 &clk_dummy_rate_ops,
2309 ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2310 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2314 ctx->mux_ctx.current_parent = 0;
2315 ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2316 &clk_multiple_parents_mux_ops,
2318 ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2322 ctx->clk = clk_hw_get_clk(&ctx->mux_ctx.hw, NULL);
2323 ret = clk_notifier_register(ctx->clk, &ctx->clk_nb);
2330 static void clk_mux_notifier_test_exit(struct kunit *test)
2332 struct clk_mux_notifier_ctx *ctx = test->priv;
2333 struct clk *clk = ctx->clk;
2335 clk_notifier_unregister(clk, &ctx->clk_nb);
2338 clk_hw_unregister(&ctx->mux_ctx.hw);
2339 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2340 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2344 * Test that if the we have a notifier registered on a mux, the core
2345 * will notify us when we switch to another parent, and with the proper
2346 * old and new rates.
2348 static void clk_mux_notifier_set_parent_test(struct kunit *test)
2350 struct clk_mux_notifier_ctx *ctx = test->priv;
2351 struct clk_hw *hw = &ctx->mux_ctx.hw;
2352 struct clk *clk = clk_hw_get_clk(hw, NULL);
2353 struct clk *new_parent = clk_hw_get_clk(&ctx->mux_ctx.parents_ctx[1].hw, NULL);
2356 ret = clk_set_parent(clk, new_parent);
2357 KUNIT_ASSERT_EQ(test, ret, 0);
2359 ret = wait_event_interruptible_timeout(ctx->pre_rate_change.wq,
2360 ctx->pre_rate_change.done,
2361 msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
2362 KUNIT_ASSERT_GT(test, ret, 0);
2364 KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
2365 KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
2367 ret = wait_event_interruptible_timeout(ctx->post_rate_change.wq,
2368 ctx->post_rate_change.done,
2369 msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
2370 KUNIT_ASSERT_GT(test, ret, 0);
2372 KUNIT_EXPECT_EQ(test, ctx->post_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
2373 KUNIT_EXPECT_EQ(test, ctx->post_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
2375 clk_put(new_parent);
2379 static struct kunit_case clk_mux_notifier_test_cases[] = {
2380 KUNIT_CASE(clk_mux_notifier_set_parent_test),
2385 * Test suite for a mux with multiple parents, and a notifier registered
2388 * These tests exercise the behaviour of notifiers.
2390 static struct kunit_suite clk_mux_notifier_test_suite = {
2391 .name = "clk-mux-notifier",
2392 .init = clk_mux_notifier_test_init,
2393 .exit = clk_mux_notifier_test_exit,
2394 .test_cases = clk_mux_notifier_test_cases,
2398 &clk_leaf_mux_set_rate_parent_test_suite,
2400 &clk_multiple_parents_mux_test_suite,
2401 &clk_mux_notifier_test_suite,
2402 &clk_orphan_transparent_multiple_parent_mux_test_suite,
2403 &clk_orphan_transparent_single_parent_test_suite,
2404 &clk_orphan_two_level_root_last_test_suite,
2405 &clk_range_test_suite,
2406 &clk_range_maximize_test_suite,
2407 &clk_range_minimize_test_suite,
2408 &clk_single_parent_mux_test_suite,
2409 &clk_uncached_test_suite
2411 MODULE_LICENSE("GPL v2");