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 static const struct clk_ops empty_clk_ops = { };
15 #define DUMMY_CLOCK_INIT_RATE (42 * 1000 * 1000)
16 #define DUMMY_CLOCK_RATE_1 (142 * 1000 * 1000)
17 #define DUMMY_CLOCK_RATE_2 (242 * 1000 * 1000)
19 struct clk_dummy_context {
24 static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw,
25 unsigned long parent_rate)
27 struct clk_dummy_context *ctx =
28 container_of(hw, struct clk_dummy_context, hw);
33 static int clk_dummy_determine_rate(struct clk_hw *hw,
34 struct clk_rate_request *req)
36 /* Just return the same rate without modifying it */
40 static int clk_dummy_maximize_rate(struct clk_hw *hw,
41 struct clk_rate_request *req)
44 * If there's a maximum set, always run the clock at the maximum
47 if (req->max_rate < ULONG_MAX)
48 req->rate = req->max_rate;
53 static int clk_dummy_minimize_rate(struct clk_hw *hw,
54 struct clk_rate_request *req)
57 * If there's a minimum set, always run the clock at the minimum
60 if (req->min_rate > 0)
61 req->rate = req->min_rate;
66 static int clk_dummy_set_rate(struct clk_hw *hw,
68 unsigned long parent_rate)
70 struct clk_dummy_context *ctx =
71 container_of(hw, struct clk_dummy_context, hw);
77 static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
79 if (index >= clk_hw_get_num_parents(hw))
85 static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
90 static const struct clk_ops clk_dummy_rate_ops = {
91 .recalc_rate = clk_dummy_recalc_rate,
92 .determine_rate = clk_dummy_determine_rate,
93 .set_rate = clk_dummy_set_rate,
96 static const struct clk_ops clk_dummy_maximize_rate_ops = {
97 .recalc_rate = clk_dummy_recalc_rate,
98 .determine_rate = clk_dummy_maximize_rate,
99 .set_rate = clk_dummy_set_rate,
102 static const struct clk_ops clk_dummy_minimize_rate_ops = {
103 .recalc_rate = clk_dummy_recalc_rate,
104 .determine_rate = clk_dummy_minimize_rate,
105 .set_rate = clk_dummy_set_rate,
108 static const struct clk_ops clk_dummy_single_parent_ops = {
110 * FIXME: Even though we should probably be able to use
111 * __clk_mux_determine_rate() here, if we use it and call
112 * clk_round_rate() or clk_set_rate() with a rate lower than
113 * what all the parents can provide, it will return -EINVAL.
115 * This is due to the fact that it has the undocumented
116 * behaviour to always pick up the closest rate higher than the
117 * requested rate. If we get something lower, it thus considers
118 * that it's not acceptable and will return an error.
120 * It's somewhat inconsistent and creates a weird threshold
121 * between rates above the parent rate which would be rounded to
122 * what the parent can provide, but rates below will simply
125 .determine_rate = __clk_mux_determine_rate_closest,
126 .set_parent = clk_dummy_single_set_parent,
127 .get_parent = clk_dummy_single_get_parent,
130 struct clk_multiple_parent_ctx {
131 struct clk_dummy_context parents_ctx[2];
136 static int clk_multiple_parents_mux_set_parent(struct clk_hw *hw, u8 index)
138 struct clk_multiple_parent_ctx *ctx =
139 container_of(hw, struct clk_multiple_parent_ctx, hw);
141 if (index >= clk_hw_get_num_parents(hw))
144 ctx->current_parent = index;
149 static u8 clk_multiple_parents_mux_get_parent(struct clk_hw *hw)
151 struct clk_multiple_parent_ctx *ctx =
152 container_of(hw, struct clk_multiple_parent_ctx, hw);
154 return ctx->current_parent;
157 static const struct clk_ops clk_multiple_parents_mux_ops = {
158 .get_parent = clk_multiple_parents_mux_get_parent,
159 .set_parent = clk_multiple_parents_mux_set_parent,
160 .determine_rate = __clk_mux_determine_rate_closest,
163 static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = {
164 .determine_rate = clk_hw_determine_rate_no_reparent,
165 .get_parent = clk_multiple_parents_mux_get_parent,
166 .set_parent = clk_multiple_parents_mux_set_parent,
169 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
171 struct clk_dummy_context *ctx;
172 struct clk_init_data init = { };
175 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
178 ctx->rate = DUMMY_CLOCK_INIT_RATE;
181 init.name = "test_dummy_rate";
183 ctx->hw.init = &init;
185 ret = clk_hw_register(NULL, &ctx->hw);
192 static int clk_test_init(struct kunit *test)
194 return clk_test_init_with_ops(test, &clk_dummy_rate_ops);
197 static int clk_maximize_test_init(struct kunit *test)
199 return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops);
202 static int clk_minimize_test_init(struct kunit *test)
204 return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops);
207 static void clk_test_exit(struct kunit *test)
209 struct clk_dummy_context *ctx = test->priv;
211 clk_hw_unregister(&ctx->hw);
215 * Test that the actual rate matches what is returned by clk_get_rate()
217 static void clk_test_get_rate(struct kunit *test)
219 struct clk_dummy_context *ctx = test->priv;
220 struct clk_hw *hw = &ctx->hw;
221 struct clk *clk = clk_hw_get_clk(hw, NULL);
224 rate = clk_get_rate(clk);
225 KUNIT_ASSERT_GT(test, rate, 0);
226 KUNIT_EXPECT_EQ(test, rate, ctx->rate);
232 * Test that, after a call to clk_set_rate(), the rate returned by
233 * clk_get_rate() matches.
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_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 rate = clk_get_rate(clk);
250 KUNIT_ASSERT_GT(test, rate, 0);
251 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
257 * Test that, after several calls to clk_set_rate(), the rate returned
258 * by clk_get_rate() matches the last one.
260 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
261 * modify the requested rate, which is our case in clk_dummy_rate_ops.
263 static void clk_test_set_set_get_rate(struct kunit *test)
265 struct clk_dummy_context *ctx = test->priv;
266 struct clk_hw *hw = &ctx->hw;
267 struct clk *clk = clk_hw_get_clk(hw, NULL);
270 KUNIT_ASSERT_EQ(test,
271 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
274 KUNIT_ASSERT_EQ(test,
275 clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
278 rate = clk_get_rate(clk);
279 KUNIT_ASSERT_GT(test, rate, 0);
280 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
286 * Test that clk_round_rate and clk_set_rate are consitent and will
287 * return the same frequency.
289 static void clk_test_round_set_get_rate(struct kunit *test)
291 struct clk_dummy_context *ctx = test->priv;
292 struct clk_hw *hw = &ctx->hw;
293 struct clk *clk = clk_hw_get_clk(hw, NULL);
294 unsigned long set_rate;
297 rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
298 KUNIT_ASSERT_GT(test, rounded_rate, 0);
299 KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
301 KUNIT_ASSERT_EQ(test,
302 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
305 set_rate = clk_get_rate(clk);
306 KUNIT_ASSERT_GT(test, set_rate, 0);
307 KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
312 static struct kunit_case clk_test_cases[] = {
313 KUNIT_CASE(clk_test_get_rate),
314 KUNIT_CASE(clk_test_set_get_rate),
315 KUNIT_CASE(clk_test_set_set_get_rate),
316 KUNIT_CASE(clk_test_round_set_get_rate),
321 * Test suite for a basic rate clock, without any parent.
323 * These tests exercise the rate API with simple scenarios
325 static struct kunit_suite clk_test_suite = {
327 .init = clk_test_init,
328 .exit = clk_test_exit,
329 .test_cases = clk_test_cases,
332 static int clk_uncached_test_init(struct kunit *test)
334 struct clk_dummy_context *ctx;
337 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
342 ctx->rate = DUMMY_CLOCK_INIT_RATE;
343 ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
345 CLK_GET_RATE_NOCACHE);
347 ret = clk_hw_register(NULL, &ctx->hw);
355 * Test that for an uncached clock, the clock framework doesn't cache
356 * the rate and clk_get_rate() will return the underlying clock rate
357 * even if it changed.
359 static void clk_test_uncached_get_rate(struct kunit *test)
361 struct clk_dummy_context *ctx = test->priv;
362 struct clk_hw *hw = &ctx->hw;
363 struct clk *clk = clk_hw_get_clk(hw, NULL);
366 rate = clk_get_rate(clk);
367 KUNIT_ASSERT_GT(test, rate, 0);
368 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
370 /* We change the rate behind the clock framework's back */
371 ctx->rate = DUMMY_CLOCK_RATE_1;
372 rate = clk_get_rate(clk);
373 KUNIT_ASSERT_GT(test, rate, 0);
374 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
380 * Test that for an uncached clock, clk_set_rate_range() will work
381 * properly if the rate hasn't changed.
383 static void clk_test_uncached_set_range(struct kunit *test)
385 struct clk_dummy_context *ctx = test->priv;
386 struct clk_hw *hw = &ctx->hw;
387 struct clk *clk = clk_hw_get_clk(hw, NULL);
390 KUNIT_ASSERT_EQ(test,
391 clk_set_rate_range(clk,
396 rate = clk_get_rate(clk);
397 KUNIT_ASSERT_GT(test, rate, 0);
398 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
399 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
405 * Test that for an uncached clock, clk_set_rate_range() will work
406 * properly if the rate has changed in hardware.
408 * In this case, it means that if the rate wasn't initially in the range
409 * we're trying to set, but got changed at some point into the range
410 * without the kernel knowing about it, its rate shouldn't be affected.
412 static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
414 struct clk_dummy_context *ctx = test->priv;
415 struct clk_hw *hw = &ctx->hw;
416 struct clk *clk = clk_hw_get_clk(hw, NULL);
419 /* We change the rate behind the clock framework's back */
420 ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
421 KUNIT_ASSERT_EQ(test,
422 clk_set_rate_range(clk,
427 rate = clk_get_rate(clk);
428 KUNIT_ASSERT_GT(test, rate, 0);
429 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
434 static struct kunit_case clk_uncached_test_cases[] = {
435 KUNIT_CASE(clk_test_uncached_get_rate),
436 KUNIT_CASE(clk_test_uncached_set_range),
437 KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
442 * Test suite for a basic, uncached, rate clock, without any parent.
444 * These tests exercise the rate API with simple scenarios
446 static struct kunit_suite clk_uncached_test_suite = {
447 .name = "clk-uncached-test",
448 .init = clk_uncached_test_init,
449 .exit = clk_test_exit,
450 .test_cases = clk_uncached_test_cases,
454 clk_multiple_parents_mux_test_init(struct kunit *test)
456 struct clk_multiple_parent_ctx *ctx;
457 const char *parents[2] = { "parent-0", "parent-1"};
460 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
465 ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
468 ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
469 ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
473 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
476 ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
477 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
481 ctx->current_parent = 0;
482 ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
483 &clk_multiple_parents_mux_ops,
484 CLK_SET_RATE_PARENT);
485 ret = clk_hw_register(NULL, &ctx->hw);
493 clk_multiple_parents_mux_test_exit(struct kunit *test)
495 struct clk_multiple_parent_ctx *ctx = test->priv;
497 clk_hw_unregister(&ctx->hw);
498 clk_hw_unregister(&ctx->parents_ctx[0].hw);
499 clk_hw_unregister(&ctx->parents_ctx[1].hw);
503 * Test that for a clock with multiple parents, clk_get_parent()
504 * actually returns the current one.
507 clk_test_multiple_parents_mux_get_parent(struct kunit *test)
509 struct clk_multiple_parent_ctx *ctx = test->priv;
510 struct clk_hw *hw = &ctx->hw;
511 struct clk *clk = clk_hw_get_clk(hw, NULL);
512 struct clk *parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
514 KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
521 * Test that for a clock with a multiple parents, clk_has_parent()
522 * actually reports all of them as parents.
525 clk_test_multiple_parents_mux_has_parent(struct kunit *test)
527 struct clk_multiple_parent_ctx *ctx = test->priv;
528 struct clk_hw *hw = &ctx->hw;
529 struct clk *clk = clk_hw_get_clk(hw, NULL);
532 parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
533 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
536 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
537 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
544 * Test that for a clock with a multiple parents, if we set a range on
545 * that clock and the parent is changed, its rate after the reparenting
546 * is still within the range we asked for.
548 * FIXME: clk_set_parent() only does the reparenting but doesn't
549 * reevaluate whether the new clock rate is within its boundaries or
553 clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
555 struct clk_multiple_parent_ctx *ctx = test->priv;
556 struct clk_hw *hw = &ctx->hw;
557 struct clk *clk = clk_hw_get_clk(hw, NULL);
558 struct clk *parent1, *parent2;
562 kunit_skip(test, "This needs to be fixed in the core.");
564 parent1 = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
565 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent1);
566 KUNIT_ASSERT_TRUE(test, clk_is_match(clk_get_parent(clk), parent1));
568 parent2 = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
569 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent2);
571 ret = clk_set_rate(parent1, DUMMY_CLOCK_RATE_1);
572 KUNIT_ASSERT_EQ(test, ret, 0);
574 ret = clk_set_rate(parent2, DUMMY_CLOCK_RATE_2);
575 KUNIT_ASSERT_EQ(test, ret, 0);
577 ret = clk_set_rate_range(clk,
578 DUMMY_CLOCK_RATE_1 - 1000,
579 DUMMY_CLOCK_RATE_1 + 1000);
580 KUNIT_ASSERT_EQ(test, ret, 0);
582 ret = clk_set_parent(clk, parent2);
583 KUNIT_ASSERT_EQ(test, ret, 0);
585 rate = clk_get_rate(clk);
586 KUNIT_ASSERT_GT(test, rate, 0);
587 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
588 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
595 static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
596 KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
597 KUNIT_CASE(clk_test_multiple_parents_mux_has_parent),
598 KUNIT_CASE(clk_test_multiple_parents_mux_set_range_set_parent_get_rate),
603 * Test suite for a basic mux clock with two parents, with
604 * CLK_SET_RATE_PARENT on the child.
606 * These tests exercise the consumer API and check that the state of the
607 * child and parents are sane and consistent.
609 static struct kunit_suite
610 clk_multiple_parents_mux_test_suite = {
611 .name = "clk-multiple-parents-mux-test",
612 .init = clk_multiple_parents_mux_test_init,
613 .exit = clk_multiple_parents_mux_test_exit,
614 .test_cases = clk_multiple_parents_mux_test_cases,
618 clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test)
620 struct clk_multiple_parent_ctx *ctx;
621 const char *parents[2] = { "missing-parent", "proper-parent"};
624 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
629 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("proper-parent",
632 ctx->parents_ctx[1].rate = DUMMY_CLOCK_INIT_RATE;
633 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
637 ctx->hw.init = CLK_HW_INIT_PARENTS("test-orphan-mux", parents,
638 &clk_multiple_parents_mux_ops,
639 CLK_SET_RATE_PARENT);
640 ret = clk_hw_register(NULL, &ctx->hw);
648 clk_orphan_transparent_multiple_parent_mux_test_exit(struct kunit *test)
650 struct clk_multiple_parent_ctx *ctx = test->priv;
652 clk_hw_unregister(&ctx->hw);
653 clk_hw_unregister(&ctx->parents_ctx[1].hw);
657 * Test that, for a mux whose current parent hasn't been registered yet and is
658 * thus orphan, clk_get_parent() will return NULL.
661 clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit *test)
663 struct clk_multiple_parent_ctx *ctx = test->priv;
664 struct clk_hw *hw = &ctx->hw;
665 struct clk *clk = clk_hw_get_clk(hw, NULL);
667 KUNIT_EXPECT_PTR_EQ(test, clk_get_parent(clk), NULL);
673 * Test that, for a mux whose current parent hasn't been registered yet,
674 * calling clk_set_parent() to a valid parent will properly update the
675 * mux parent and its orphan status.
678 clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit *test)
680 struct clk_multiple_parent_ctx *ctx = test->priv;
681 struct clk_hw *hw = &ctx->hw;
682 struct clk *clk = clk_hw_get_clk(hw, NULL);
683 struct clk *parent, *new_parent;
686 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
687 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
689 ret = clk_set_parent(clk, parent);
690 KUNIT_ASSERT_EQ(test, ret, 0);
692 new_parent = clk_get_parent(clk);
693 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
694 KUNIT_EXPECT_TRUE(test, clk_is_match(parent, new_parent));
701 * Test that, for a mux that started orphan but got switched to a valid
702 * parent, calling clk_drop_range() on the mux won't affect the parent
706 clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit *test)
708 struct clk_multiple_parent_ctx *ctx = test->priv;
709 struct clk_hw *hw = &ctx->hw;
710 struct clk *clk = clk_hw_get_clk(hw, NULL);
712 unsigned long parent_rate, new_parent_rate;
715 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
716 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
718 parent_rate = clk_get_rate(parent);
719 KUNIT_ASSERT_GT(test, parent_rate, 0);
721 ret = clk_set_parent(clk, parent);
722 KUNIT_ASSERT_EQ(test, ret, 0);
724 ret = clk_drop_range(clk);
725 KUNIT_ASSERT_EQ(test, ret, 0);
727 new_parent_rate = clk_get_rate(clk);
728 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
729 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
736 * Test that, for a mux that started orphan but got switched to a valid
737 * parent, the rate of the mux and its new parent are consistent.
740 clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit *test)
742 struct clk_multiple_parent_ctx *ctx = test->priv;
743 struct clk_hw *hw = &ctx->hw;
744 struct clk *clk = clk_hw_get_clk(hw, NULL);
746 unsigned long parent_rate, rate;
749 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
750 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
752 parent_rate = clk_get_rate(parent);
753 KUNIT_ASSERT_GT(test, parent_rate, 0);
755 ret = clk_set_parent(clk, parent);
756 KUNIT_ASSERT_EQ(test, ret, 0);
758 rate = clk_get_rate(clk);
759 KUNIT_ASSERT_GT(test, rate, 0);
760 KUNIT_EXPECT_EQ(test, parent_rate, rate);
767 * Test that, for a mux that started orphan but got switched to a valid
768 * parent, calling clk_put() on the mux won't affect the parent rate.
771 clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit *test)
773 struct clk_multiple_parent_ctx *ctx = test->priv;
774 struct clk *clk, *parent;
775 unsigned long parent_rate, new_parent_rate;
778 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
779 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
781 clk = clk_hw_get_clk(&ctx->hw, NULL);
782 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
784 parent_rate = clk_get_rate(parent);
785 KUNIT_ASSERT_GT(test, parent_rate, 0);
787 ret = clk_set_parent(clk, parent);
788 KUNIT_ASSERT_EQ(test, ret, 0);
792 new_parent_rate = clk_get_rate(parent);
793 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
794 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
800 * Test that, for a mux that started orphan but got switched to a valid
801 * parent, calling clk_set_rate_range() will affect the parent state if
802 * its rate is out of range.
805 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit *test)
807 struct clk_multiple_parent_ctx *ctx = test->priv;
808 struct clk_hw *hw = &ctx->hw;
809 struct clk *clk = clk_hw_get_clk(hw, NULL);
814 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
815 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
817 ret = clk_set_parent(clk, parent);
818 KUNIT_ASSERT_EQ(test, ret, 0);
820 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
821 KUNIT_ASSERT_EQ(test, ret, 0);
823 rate = clk_get_rate(clk);
824 KUNIT_ASSERT_GT(test, rate, 0);
825 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
826 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
833 * Test that, for a mux that started orphan but got switched to a valid
834 * parent, calling clk_set_rate_range() won't affect the parent state if
835 * its rate is within range.
838 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit *test)
840 struct clk_multiple_parent_ctx *ctx = test->priv;
841 struct clk_hw *hw = &ctx->hw;
842 struct clk *clk = clk_hw_get_clk(hw, NULL);
844 unsigned long parent_rate, new_parent_rate;
847 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
848 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
850 parent_rate = clk_get_rate(parent);
851 KUNIT_ASSERT_GT(test, parent_rate, 0);
853 ret = clk_set_parent(clk, parent);
854 KUNIT_ASSERT_EQ(test, ret, 0);
856 ret = clk_set_rate_range(clk,
857 DUMMY_CLOCK_INIT_RATE - 1000,
858 DUMMY_CLOCK_INIT_RATE + 1000);
859 KUNIT_ASSERT_EQ(test, ret, 0);
861 new_parent_rate = clk_get_rate(parent);
862 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
863 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
870 * Test that, for a mux whose current parent hasn't been registered yet,
871 * calling clk_set_rate_range() will succeed, and will be taken into
872 * account when rounding a rate.
875 clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit *test)
877 struct clk_multiple_parent_ctx *ctx = test->priv;
878 struct clk_hw *hw = &ctx->hw;
879 struct clk *clk = clk_hw_get_clk(hw, NULL);
883 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
884 KUNIT_ASSERT_EQ(test, ret, 0);
886 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
887 KUNIT_ASSERT_GT(test, rate, 0);
888 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
889 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
895 * Test that, for a mux that started orphan, was assigned and rate and
896 * then got switched to a valid parent, its rate is eventually within
899 * FIXME: Even though we update the rate as part of clk_set_parent(), we
900 * don't evaluate whether that new rate is within range and needs to be
904 clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit *test)
906 struct clk_multiple_parent_ctx *ctx = test->priv;
907 struct clk_hw *hw = &ctx->hw;
908 struct clk *clk = clk_hw_get_clk(hw, NULL);
913 kunit_skip(test, "This needs to be fixed in the core.");
915 clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
917 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
918 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
920 ret = clk_set_parent(clk, parent);
921 KUNIT_ASSERT_EQ(test, ret, 0);
923 rate = clk_get_rate(clk);
924 KUNIT_ASSERT_GT(test, rate, 0);
925 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
926 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
932 static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = {
933 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_get_parent),
934 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent),
935 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range),
936 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate),
937 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_put),
938 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified),
939 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched),
940 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate),
941 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate),
946 * Test suite for a basic mux clock with two parents. The default parent
947 * isn't registered, only the second parent is. By default, the clock
948 * will thus be orphan.
950 * These tests exercise the behaviour of the consumer API when dealing
951 * with an orphan clock, and how we deal with the transition to a valid
954 static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = {
955 .name = "clk-orphan-transparent-multiple-parent-mux-test",
956 .init = clk_orphan_transparent_multiple_parent_mux_test_init,
957 .exit = clk_orphan_transparent_multiple_parent_mux_test_exit,
958 .test_cases = clk_orphan_transparent_multiple_parent_mux_test_cases,
961 struct clk_single_parent_ctx {
962 struct clk_dummy_context parent_ctx;
966 static int clk_single_parent_mux_test_init(struct kunit *test)
968 struct clk_single_parent_ctx *ctx;
971 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
976 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
977 ctx->parent_ctx.hw.init =
978 CLK_HW_INIT_NO_PARENT("parent-clk",
982 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
986 ctx->hw.init = CLK_HW_INIT("test-clk", "parent-clk",
987 &clk_dummy_single_parent_ops,
988 CLK_SET_RATE_PARENT);
990 ret = clk_hw_register(NULL, &ctx->hw);
998 clk_single_parent_mux_test_exit(struct kunit *test)
1000 struct clk_single_parent_ctx *ctx = test->priv;
1002 clk_hw_unregister(&ctx->hw);
1003 clk_hw_unregister(&ctx->parent_ctx.hw);
1007 * Test that for a clock with a single parent, clk_get_parent() actually
1008 * returns the parent.
1011 clk_test_single_parent_mux_get_parent(struct kunit *test)
1013 struct clk_single_parent_ctx *ctx = test->priv;
1014 struct clk_hw *hw = &ctx->hw;
1015 struct clk *clk = clk_hw_get_clk(hw, NULL);
1016 struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
1018 KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
1025 * Test that for a clock with a single parent, clk_has_parent() actually
1026 * reports it as a parent.
1029 clk_test_single_parent_mux_has_parent(struct kunit *test)
1031 struct clk_single_parent_ctx *ctx = test->priv;
1032 struct clk_hw *hw = &ctx->hw;
1033 struct clk *clk = clk_hw_get_clk(hw, NULL);
1034 struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
1036 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
1043 * Test that for a clock that can't modify its rate and with a single
1044 * parent, if we set disjoints range on the parent and then the child,
1045 * the second will return an error.
1047 * FIXME: clk_set_rate_range() only considers the current clock when
1048 * evaluating whether ranges are disjoints and not the upstream clocks
1052 clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
1054 struct clk_single_parent_ctx *ctx = test->priv;
1055 struct clk_hw *hw = &ctx->hw;
1056 struct clk *clk = clk_hw_get_clk(hw, NULL);
1060 kunit_skip(test, "This needs to be fixed in the core.");
1062 parent = clk_get_parent(clk);
1063 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1065 ret = clk_set_rate_range(parent, 1000, 2000);
1066 KUNIT_ASSERT_EQ(test, ret, 0);
1068 ret = clk_set_rate_range(clk, 3000, 4000);
1069 KUNIT_EXPECT_LT(test, ret, 0);
1075 * Test that for a clock that can't modify its rate and with a single
1076 * parent, if we set disjoints range on the child and then the parent,
1077 * the second will return an error.
1079 * FIXME: clk_set_rate_range() only considers the current clock when
1080 * evaluating whether ranges are disjoints and not the downstream clocks
1084 clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
1086 struct clk_single_parent_ctx *ctx = test->priv;
1087 struct clk_hw *hw = &ctx->hw;
1088 struct clk *clk = clk_hw_get_clk(hw, NULL);
1092 kunit_skip(test, "This needs to be fixed in the core.");
1094 parent = clk_get_parent(clk);
1095 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1097 ret = clk_set_rate_range(clk, 1000, 2000);
1098 KUNIT_ASSERT_EQ(test, ret, 0);
1100 ret = clk_set_rate_range(parent, 3000, 4000);
1101 KUNIT_EXPECT_LT(test, ret, 0);
1107 * Test that for a clock that can't modify its rate and with a single
1108 * parent, if we set a range on the parent and then call
1109 * clk_round_rate(), the boundaries of the parent are taken into
1113 clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test)
1115 struct clk_single_parent_ctx *ctx = test->priv;
1116 struct clk_hw *hw = &ctx->hw;
1117 struct clk *clk = clk_hw_get_clk(hw, NULL);
1122 parent = clk_get_parent(clk);
1123 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1125 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1126 KUNIT_ASSERT_EQ(test, ret, 0);
1128 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1129 KUNIT_ASSERT_GT(test, rate, 0);
1130 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1131 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1137 * Test that for a clock that can't modify its rate and with a single
1138 * parent, if we set a range on the parent and a more restrictive one on
1139 * the child, and then call clk_round_rate(), the boundaries of the
1140 * two clocks are taken into account.
1143 clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test)
1145 struct clk_single_parent_ctx *ctx = test->priv;
1146 struct clk_hw *hw = &ctx->hw;
1147 struct clk *clk = clk_hw_get_clk(hw, NULL);
1152 parent = clk_get_parent(clk);
1153 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1155 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1156 KUNIT_ASSERT_EQ(test, ret, 0);
1158 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1159 KUNIT_ASSERT_EQ(test, ret, 0);
1161 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1162 KUNIT_ASSERT_GT(test, rate, 0);
1163 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1164 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1166 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1167 KUNIT_ASSERT_GT(test, rate, 0);
1168 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1169 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1175 * Test that for a clock that can't modify its rate and with a single
1176 * parent, if we set a range on the child and a more restrictive one on
1177 * the parent, and then call clk_round_rate(), the boundaries of the
1178 * two clocks are taken into account.
1181 clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test)
1183 struct clk_single_parent_ctx *ctx = test->priv;
1184 struct clk_hw *hw = &ctx->hw;
1185 struct clk *clk = clk_hw_get_clk(hw, NULL);
1190 parent = clk_get_parent(clk);
1191 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1193 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1194 KUNIT_ASSERT_EQ(test, ret, 0);
1196 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1197 KUNIT_ASSERT_EQ(test, ret, 0);
1199 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1200 KUNIT_ASSERT_GT(test, rate, 0);
1201 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1202 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1204 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1205 KUNIT_ASSERT_GT(test, rate, 0);
1206 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1207 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1212 static struct kunit_case clk_single_parent_mux_test_cases[] = {
1213 KUNIT_CASE(clk_test_single_parent_mux_get_parent),
1214 KUNIT_CASE(clk_test_single_parent_mux_has_parent),
1215 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
1216 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
1217 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),
1218 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only),
1219 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller),
1224 * Test suite for a basic mux clock with one parent, with
1225 * CLK_SET_RATE_PARENT on the child.
1227 * These tests exercise the consumer API and check that the state of the
1228 * child and parent are sane and consistent.
1230 static struct kunit_suite
1231 clk_single_parent_mux_test_suite = {
1232 .name = "clk-single-parent-mux-test",
1233 .init = clk_single_parent_mux_test_init,
1234 .exit = clk_single_parent_mux_test_exit,
1235 .test_cases = clk_single_parent_mux_test_cases,
1238 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
1240 struct clk_single_parent_ctx *ctx;
1241 struct clk_init_data init = { };
1242 const char * const parents[] = { "orphan_parent" };
1245 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1250 init.name = "test_orphan_dummy_parent";
1251 init.ops = &clk_dummy_single_parent_ops;
1252 init.parent_names = parents;
1253 init.num_parents = ARRAY_SIZE(parents);
1254 init.flags = CLK_SET_RATE_PARENT;
1255 ctx->hw.init = &init;
1257 ret = clk_hw_register(NULL, &ctx->hw);
1261 memset(&init, 0, sizeof(init));
1262 init.name = "orphan_parent";
1263 init.ops = &clk_dummy_rate_ops;
1264 ctx->parent_ctx.hw.init = &init;
1265 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1267 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1275 * Test that a mux-only clock, with an initial rate within a range,
1276 * will still have the same rate after the range has been enforced.
1281 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
1283 struct clk_single_parent_ctx *ctx = test->priv;
1284 struct clk_hw *hw = &ctx->hw;
1285 struct clk *clk = clk_hw_get_clk(hw, NULL);
1286 unsigned long rate, new_rate;
1288 rate = clk_get_rate(clk);
1289 KUNIT_ASSERT_GT(test, rate, 0);
1291 KUNIT_ASSERT_EQ(test,
1292 clk_set_rate_range(clk,
1293 ctx->parent_ctx.rate - 1000,
1294 ctx->parent_ctx.rate + 1000),
1297 new_rate = clk_get_rate(clk);
1298 KUNIT_ASSERT_GT(test, new_rate, 0);
1299 KUNIT_EXPECT_EQ(test, rate, new_rate);
1304 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
1305 KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
1310 * Test suite for a basic mux clock with one parent. The parent is
1311 * registered after its child. The clock will thus be an orphan when
1312 * registered, but will no longer be when the tests run.
1314 * These tests make sure a clock that used to be orphan has a sane,
1315 * consistent, behaviour.
1317 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
1318 .name = "clk-orphan-transparent-single-parent-test",
1319 .init = clk_orphan_transparent_single_parent_mux_test_init,
1320 .exit = clk_single_parent_mux_test_exit,
1321 .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
1324 struct clk_single_parent_two_lvl_ctx {
1325 struct clk_dummy_context parent_parent_ctx;
1326 struct clk_dummy_context parent_ctx;
1331 clk_orphan_two_level_root_last_test_init(struct kunit *test)
1333 struct clk_single_parent_two_lvl_ctx *ctx;
1336 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1341 ctx->parent_ctx.hw.init =
1342 CLK_HW_INIT("intermediate-parent",
1344 &clk_dummy_single_parent_ops,
1345 CLK_SET_RATE_PARENT);
1346 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1351 CLK_HW_INIT("test-clk", "intermediate-parent",
1352 &clk_dummy_single_parent_ops,
1353 CLK_SET_RATE_PARENT);
1354 ret = clk_hw_register(NULL, &ctx->hw);
1358 ctx->parent_parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1359 ctx->parent_parent_ctx.hw.init =
1360 CLK_HW_INIT_NO_PARENT("root-parent",
1361 &clk_dummy_rate_ops,
1363 ret = clk_hw_register(NULL, &ctx->parent_parent_ctx.hw);
1371 clk_orphan_two_level_root_last_test_exit(struct kunit *test)
1373 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1375 clk_hw_unregister(&ctx->hw);
1376 clk_hw_unregister(&ctx->parent_ctx.hw);
1377 clk_hw_unregister(&ctx->parent_parent_ctx.hw);
1381 * Test that, for a clock whose parent used to be orphan, clk_get_rate()
1382 * will return the proper rate.
1385 clk_orphan_two_level_root_last_test_get_rate(struct kunit *test)
1387 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1388 struct clk_hw *hw = &ctx->hw;
1389 struct clk *clk = clk_hw_get_clk(hw, NULL);
1392 rate = clk_get_rate(clk);
1393 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1399 * Test that, for a clock whose parent used to be orphan,
1400 * clk_set_rate_range() won't affect its rate if it is already within
1403 * See (for Exynos 4210):
1407 clk_orphan_two_level_root_last_test_set_range(struct kunit *test)
1409 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1410 struct clk_hw *hw = &ctx->hw;
1411 struct clk *clk = clk_hw_get_clk(hw, NULL);
1415 ret = clk_set_rate_range(clk,
1416 DUMMY_CLOCK_INIT_RATE - 1000,
1417 DUMMY_CLOCK_INIT_RATE + 1000);
1418 KUNIT_ASSERT_EQ(test, ret, 0);
1420 rate = clk_get_rate(clk);
1421 KUNIT_ASSERT_GT(test, rate, 0);
1422 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1427 static struct kunit_case
1428 clk_orphan_two_level_root_last_test_cases[] = {
1429 KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate),
1430 KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range),
1435 * Test suite for a basic, transparent, clock with a parent that is also
1436 * such a clock. The parent's parent is registered last, while the
1437 * parent and its child are registered in that order. The intermediate
1438 * and leaf clocks will thus be orphan when registered, but the leaf
1439 * clock itself will always have its parent and will never be
1440 * reparented. Indeed, it's only orphan because its parent is.
1442 * These tests exercise the behaviour of the consumer API when dealing
1443 * with an orphan clock, and how we deal with the transition to a valid
1446 static struct kunit_suite
1447 clk_orphan_two_level_root_last_test_suite = {
1448 .name = "clk-orphan-two-level-root-last-test",
1449 .init = clk_orphan_two_level_root_last_test_init,
1450 .exit = clk_orphan_two_level_root_last_test_exit,
1451 .test_cases = clk_orphan_two_level_root_last_test_cases,
1455 * Test that clk_set_rate_range won't return an error for a valid range
1456 * and that it will make sure the rate of the clock is within the
1459 static void clk_range_test_set_range(struct kunit *test)
1461 struct clk_dummy_context *ctx = test->priv;
1462 struct clk_hw *hw = &ctx->hw;
1463 struct clk *clk = clk_hw_get_clk(hw, NULL);
1466 KUNIT_ASSERT_EQ(test,
1467 clk_set_rate_range(clk,
1469 DUMMY_CLOCK_RATE_2),
1472 rate = clk_get_rate(clk);
1473 KUNIT_ASSERT_GT(test, rate, 0);
1474 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1475 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1481 * Test that calling clk_set_rate_range with a minimum rate higher than
1482 * the maximum rate returns an error.
1484 static void clk_range_test_set_range_invalid(struct kunit *test)
1486 struct clk_dummy_context *ctx = test->priv;
1487 struct clk_hw *hw = &ctx->hw;
1488 struct clk *clk = clk_hw_get_clk(hw, NULL);
1490 KUNIT_EXPECT_LT(test,
1491 clk_set_rate_range(clk,
1492 DUMMY_CLOCK_RATE_1 + 1000,
1493 DUMMY_CLOCK_RATE_1),
1500 * Test that users can't set multiple, disjoints, range that would be
1501 * impossible to meet.
1503 static void clk_range_test_multiple_disjoints_range(struct kunit *test)
1505 struct clk_dummy_context *ctx = test->priv;
1506 struct clk_hw *hw = &ctx->hw;
1507 struct clk *user1, *user2;
1509 user1 = clk_hw_get_clk(hw, NULL);
1510 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1512 user2 = clk_hw_get_clk(hw, NULL);
1513 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1515 KUNIT_ASSERT_EQ(test,
1516 clk_set_rate_range(user1, 1000, 2000),
1519 KUNIT_EXPECT_LT(test,
1520 clk_set_rate_range(user2, 3000, 4000),
1528 * Test that if our clock has some boundaries and we try to round a rate
1529 * lower than the minimum, the returned rate will be within range.
1531 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
1533 struct clk_dummy_context *ctx = test->priv;
1534 struct clk_hw *hw = &ctx->hw;
1535 struct clk *clk = clk_hw_get_clk(hw, NULL);
1538 KUNIT_ASSERT_EQ(test,
1539 clk_set_rate_range(clk,
1541 DUMMY_CLOCK_RATE_2),
1544 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1545 KUNIT_ASSERT_GT(test, rate, 0);
1546 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1547 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1553 * Test that if our clock has some boundaries and we try to set a rate
1554 * higher than the maximum, the new rate will be within range.
1556 static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
1558 struct clk_dummy_context *ctx = test->priv;
1559 struct clk_hw *hw = &ctx->hw;
1560 struct clk *clk = clk_hw_get_clk(hw, NULL);
1563 KUNIT_ASSERT_EQ(test,
1564 clk_set_rate_range(clk,
1566 DUMMY_CLOCK_RATE_2),
1569 KUNIT_ASSERT_EQ(test,
1570 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1573 rate = clk_get_rate(clk);
1574 KUNIT_ASSERT_GT(test, rate, 0);
1575 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1576 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1582 * Test that if our clock has some boundaries and we try to round and
1583 * set a rate lower than the minimum, the rate returned by
1584 * clk_round_rate() will be consistent with the new rate set by
1587 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
1589 struct clk_dummy_context *ctx = test->priv;
1590 struct clk_hw *hw = &ctx->hw;
1591 struct clk *clk = clk_hw_get_clk(hw, NULL);
1594 KUNIT_ASSERT_EQ(test,
1595 clk_set_rate_range(clk,
1597 DUMMY_CLOCK_RATE_2),
1600 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1601 KUNIT_ASSERT_GT(test, rounded, 0);
1603 KUNIT_ASSERT_EQ(test,
1604 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1607 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1613 * Test that if our clock has some boundaries and we try to round a rate
1614 * higher than the maximum, the returned rate will be within range.
1616 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
1618 struct clk_dummy_context *ctx = test->priv;
1619 struct clk_hw *hw = &ctx->hw;
1620 struct clk *clk = clk_hw_get_clk(hw, NULL);
1623 KUNIT_ASSERT_EQ(test,
1624 clk_set_rate_range(clk,
1626 DUMMY_CLOCK_RATE_2),
1629 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1630 KUNIT_ASSERT_GT(test, rate, 0);
1631 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1632 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1638 * Test that if our clock has some boundaries and we try to set a rate
1639 * higher than the maximum, the new rate will be within range.
1641 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
1643 struct clk_dummy_context *ctx = test->priv;
1644 struct clk_hw *hw = &ctx->hw;
1645 struct clk *clk = clk_hw_get_clk(hw, NULL);
1648 KUNIT_ASSERT_EQ(test,
1649 clk_set_rate_range(clk,
1651 DUMMY_CLOCK_RATE_2),
1654 KUNIT_ASSERT_EQ(test,
1655 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1658 rate = clk_get_rate(clk);
1659 KUNIT_ASSERT_GT(test, rate, 0);
1660 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1661 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1667 * Test that if our clock has some boundaries and we try to round and
1668 * set a rate higher than the maximum, the rate returned by
1669 * clk_round_rate() will be consistent with the new rate set by
1672 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
1674 struct clk_dummy_context *ctx = test->priv;
1675 struct clk_hw *hw = &ctx->hw;
1676 struct clk *clk = clk_hw_get_clk(hw, NULL);
1679 KUNIT_ASSERT_EQ(test,
1680 clk_set_rate_range(clk,
1682 DUMMY_CLOCK_RATE_2),
1685 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1686 KUNIT_ASSERT_GT(test, rounded, 0);
1688 KUNIT_ASSERT_EQ(test,
1689 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1692 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1698 * Test that if our clock has a rate lower than the minimum set by a
1699 * call to clk_set_rate_range(), the rate will be raised to match the
1702 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1703 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1705 static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
1707 struct clk_dummy_context *ctx = test->priv;
1708 struct clk_hw *hw = &ctx->hw;
1709 struct clk *clk = clk_hw_get_clk(hw, NULL);
1712 KUNIT_ASSERT_EQ(test,
1713 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1716 KUNIT_ASSERT_EQ(test,
1717 clk_set_rate_range(clk,
1719 DUMMY_CLOCK_RATE_2),
1722 rate = clk_get_rate(clk);
1723 KUNIT_ASSERT_GT(test, rate, 0);
1724 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1730 * Test that if our clock has a rate higher than the maximum set by a
1731 * call to clk_set_rate_range(), the rate will be lowered to match the
1734 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1735 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1737 static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
1739 struct clk_dummy_context *ctx = test->priv;
1740 struct clk_hw *hw = &ctx->hw;
1741 struct clk *clk = clk_hw_get_clk(hw, NULL);
1744 KUNIT_ASSERT_EQ(test,
1745 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1748 KUNIT_ASSERT_EQ(test,
1749 clk_set_rate_range(clk,
1751 DUMMY_CLOCK_RATE_2),
1754 rate = clk_get_rate(clk);
1755 KUNIT_ASSERT_GT(test, rate, 0);
1756 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1761 static struct kunit_case clk_range_test_cases[] = {
1762 KUNIT_CASE(clk_range_test_set_range),
1763 KUNIT_CASE(clk_range_test_set_range_invalid),
1764 KUNIT_CASE(clk_range_test_multiple_disjoints_range),
1765 KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
1766 KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
1767 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
1768 KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
1769 KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
1770 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
1771 KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
1772 KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
1777 * Test suite for a basic rate clock, without any parent.
1779 * These tests exercise the rate range API: clk_set_rate_range(),
1780 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range().
1782 static struct kunit_suite clk_range_test_suite = {
1783 .name = "clk-range-test",
1784 .init = clk_test_init,
1785 .exit = clk_test_exit,
1786 .test_cases = clk_range_test_cases,
1790 * Test that if we have several subsequent calls to
1791 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1792 * needed each and every time.
1794 * With clk_dummy_maximize_rate_ops, this means that the rate will
1795 * trail along the maximum as it evolves.
1797 static void clk_range_test_set_range_rate_maximized(struct kunit *test)
1799 struct clk_dummy_context *ctx = test->priv;
1800 struct clk_hw *hw = &ctx->hw;
1801 struct clk *clk = clk_hw_get_clk(hw, NULL);
1804 KUNIT_ASSERT_EQ(test,
1805 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1808 KUNIT_ASSERT_EQ(test,
1809 clk_set_rate_range(clk,
1811 DUMMY_CLOCK_RATE_2),
1814 rate = clk_get_rate(clk);
1815 KUNIT_ASSERT_GT(test, rate, 0);
1816 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1818 KUNIT_ASSERT_EQ(test,
1819 clk_set_rate_range(clk,
1821 DUMMY_CLOCK_RATE_2 - 1000),
1824 rate = clk_get_rate(clk);
1825 KUNIT_ASSERT_GT(test, rate, 0);
1826 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1828 KUNIT_ASSERT_EQ(test,
1829 clk_set_rate_range(clk,
1831 DUMMY_CLOCK_RATE_2),
1834 rate = clk_get_rate(clk);
1835 KUNIT_ASSERT_GT(test, rate, 0);
1836 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1842 * Test that if we have several subsequent calls to
1843 * clk_set_rate_range(), across multiple users, the core will reevaluate
1844 * whether a new rate is needed each and every time.
1846 * With clk_dummy_maximize_rate_ops, this means that the rate will
1847 * trail along the maximum as it evolves.
1849 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
1851 struct clk_dummy_context *ctx = test->priv;
1852 struct clk_hw *hw = &ctx->hw;
1853 struct clk *clk = clk_hw_get_clk(hw, NULL);
1854 struct clk *user1, *user2;
1857 user1 = clk_hw_get_clk(hw, NULL);
1858 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1860 user2 = clk_hw_get_clk(hw, NULL);
1861 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1863 KUNIT_ASSERT_EQ(test,
1864 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1867 KUNIT_ASSERT_EQ(test,
1868 clk_set_rate_range(user1,
1870 DUMMY_CLOCK_RATE_2),
1873 rate = clk_get_rate(clk);
1874 KUNIT_ASSERT_GT(test, rate, 0);
1875 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1877 KUNIT_ASSERT_EQ(test,
1878 clk_set_rate_range(user2,
1880 DUMMY_CLOCK_RATE_1),
1883 rate = clk_get_rate(clk);
1884 KUNIT_ASSERT_GT(test, rate, 0);
1885 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1887 KUNIT_ASSERT_EQ(test,
1888 clk_drop_range(user2),
1891 rate = clk_get_rate(clk);
1892 KUNIT_ASSERT_GT(test, rate, 0);
1893 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1901 * Test that if we have several subsequent calls to
1902 * clk_set_rate_range(), across multiple users, the core will reevaluate
1903 * whether a new rate is needed, including when a user drop its clock.
1905 * With clk_dummy_maximize_rate_ops, this means that the rate will
1906 * trail along the maximum as it evolves.
1908 static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
1910 struct clk_dummy_context *ctx = test->priv;
1911 struct clk_hw *hw = &ctx->hw;
1912 struct clk *clk = clk_hw_get_clk(hw, NULL);
1913 struct clk *user1, *user2;
1916 user1 = clk_hw_get_clk(hw, NULL);
1917 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1919 user2 = clk_hw_get_clk(hw, NULL);
1920 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1922 KUNIT_ASSERT_EQ(test,
1923 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1926 KUNIT_ASSERT_EQ(test,
1927 clk_set_rate_range(user1,
1929 DUMMY_CLOCK_RATE_2),
1932 rate = clk_get_rate(clk);
1933 KUNIT_ASSERT_GT(test, rate, 0);
1934 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1936 KUNIT_ASSERT_EQ(test,
1937 clk_set_rate_range(user2,
1939 DUMMY_CLOCK_RATE_1),
1942 rate = clk_get_rate(clk);
1943 KUNIT_ASSERT_GT(test, rate, 0);
1944 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1948 rate = clk_get_rate(clk);
1949 KUNIT_ASSERT_GT(test, rate, 0);
1950 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1956 static struct kunit_case clk_range_maximize_test_cases[] = {
1957 KUNIT_CASE(clk_range_test_set_range_rate_maximized),
1958 KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
1959 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
1964 * Test suite for a basic rate clock, without any parent.
1966 * These tests exercise the rate range API: clk_set_rate_range(),
1967 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
1968 * driver that will always try to run at the highest possible rate.
1970 static struct kunit_suite clk_range_maximize_test_suite = {
1971 .name = "clk-range-maximize-test",
1972 .init = clk_maximize_test_init,
1973 .exit = clk_test_exit,
1974 .test_cases = clk_range_maximize_test_cases,
1978 * Test that if we have several subsequent calls to
1979 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1980 * needed each and every time.
1982 * With clk_dummy_minimize_rate_ops, this means that the rate will
1983 * trail along the minimum as it evolves.
1985 static void clk_range_test_set_range_rate_minimized(struct kunit *test)
1987 struct clk_dummy_context *ctx = test->priv;
1988 struct clk_hw *hw = &ctx->hw;
1989 struct clk *clk = clk_hw_get_clk(hw, NULL);
1992 KUNIT_ASSERT_EQ(test,
1993 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1996 KUNIT_ASSERT_EQ(test,
1997 clk_set_rate_range(clk,
1999 DUMMY_CLOCK_RATE_2),
2002 rate = clk_get_rate(clk);
2003 KUNIT_ASSERT_GT(test, rate, 0);
2004 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2006 KUNIT_ASSERT_EQ(test,
2007 clk_set_rate_range(clk,
2008 DUMMY_CLOCK_RATE_1 + 1000,
2009 DUMMY_CLOCK_RATE_2),
2012 rate = clk_get_rate(clk);
2013 KUNIT_ASSERT_GT(test, rate, 0);
2014 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
2016 KUNIT_ASSERT_EQ(test,
2017 clk_set_rate_range(clk,
2019 DUMMY_CLOCK_RATE_2),
2022 rate = clk_get_rate(clk);
2023 KUNIT_ASSERT_GT(test, rate, 0);
2024 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2030 * Test that if we have several subsequent calls to
2031 * clk_set_rate_range(), across multiple users, the core will reevaluate
2032 * whether a new rate is needed each and every time.
2034 * With clk_dummy_minimize_rate_ops, this means that the rate will
2035 * trail along the minimum as it evolves.
2037 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
2039 struct clk_dummy_context *ctx = test->priv;
2040 struct clk_hw *hw = &ctx->hw;
2041 struct clk *clk = clk_hw_get_clk(hw, NULL);
2042 struct clk *user1, *user2;
2045 user1 = clk_hw_get_clk(hw, NULL);
2046 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2048 user2 = clk_hw_get_clk(hw, NULL);
2049 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2051 KUNIT_ASSERT_EQ(test,
2052 clk_set_rate_range(user1,
2057 rate = clk_get_rate(clk);
2058 KUNIT_ASSERT_GT(test, rate, 0);
2059 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2061 KUNIT_ASSERT_EQ(test,
2062 clk_set_rate_range(user2,
2067 rate = clk_get_rate(clk);
2068 KUNIT_ASSERT_GT(test, rate, 0);
2069 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2071 KUNIT_ASSERT_EQ(test,
2072 clk_drop_range(user2),
2075 rate = clk_get_rate(clk);
2076 KUNIT_ASSERT_GT(test, rate, 0);
2077 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2085 * Test that if we have several subsequent calls to
2086 * clk_set_rate_range(), across multiple users, the core will reevaluate
2087 * whether a new rate is needed, including when a user drop its clock.
2089 * With clk_dummy_minimize_rate_ops, this means that the rate will
2090 * trail along the minimum as it evolves.
2092 static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
2094 struct clk_dummy_context *ctx = test->priv;
2095 struct clk_hw *hw = &ctx->hw;
2096 struct clk *clk = clk_hw_get_clk(hw, NULL);
2097 struct clk *user1, *user2;
2100 user1 = clk_hw_get_clk(hw, NULL);
2101 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2103 user2 = clk_hw_get_clk(hw, NULL);
2104 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2106 KUNIT_ASSERT_EQ(test,
2107 clk_set_rate_range(user1,
2112 rate = clk_get_rate(clk);
2113 KUNIT_ASSERT_GT(test, rate, 0);
2114 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2116 KUNIT_ASSERT_EQ(test,
2117 clk_set_rate_range(user2,
2122 rate = clk_get_rate(clk);
2123 KUNIT_ASSERT_GT(test, rate, 0);
2124 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2128 rate = clk_get_rate(clk);
2129 KUNIT_ASSERT_GT(test, rate, 0);
2130 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2136 static struct kunit_case clk_range_minimize_test_cases[] = {
2137 KUNIT_CASE(clk_range_test_set_range_rate_minimized),
2138 KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
2139 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
2144 * Test suite for a basic rate clock, without any parent.
2146 * These tests exercise the rate range API: clk_set_rate_range(),
2147 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
2148 * driver that will always try to run at the lowest possible rate.
2150 static struct kunit_suite clk_range_minimize_test_suite = {
2151 .name = "clk-range-minimize-test",
2152 .init = clk_minimize_test_init,
2153 .exit = clk_test_exit,
2154 .test_cases = clk_range_minimize_test_cases,
2157 struct clk_leaf_mux_ctx {
2158 struct clk_multiple_parent_ctx mux_ctx;
2160 struct clk_hw parent;
2161 struct clk_rate_request *req;
2162 int (*determine_rate_func)(struct clk_hw *hw, struct clk_rate_request *req);
2165 static int clk_leaf_mux_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
2167 struct clk_leaf_mux_ctx *ctx = container_of(hw, struct clk_leaf_mux_ctx, hw);
2169 struct clk_rate_request *parent_req = ctx->req;
2171 clk_hw_forward_rate_request(hw, req, req->best_parent_hw, parent_req, req->rate);
2172 ret = ctx->determine_rate_func(req->best_parent_hw, parent_req);
2176 req->rate = parent_req->rate;
2181 static const struct clk_ops clk_leaf_mux_set_rate_parent_ops = {
2182 .determine_rate = clk_leaf_mux_determine_rate,
2183 .set_parent = clk_dummy_single_set_parent,
2184 .get_parent = clk_dummy_single_get_parent,
2188 clk_leaf_mux_set_rate_parent_test_init(struct kunit *test)
2190 struct clk_leaf_mux_ctx *ctx;
2191 const char *top_parents[2] = { "parent-0", "parent-1" };
2194 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2199 ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2200 &clk_dummy_rate_ops,
2202 ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2203 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2207 ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2208 &clk_dummy_rate_ops,
2210 ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2211 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2215 ctx->mux_ctx.current_parent = 0;
2216 ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2217 &clk_multiple_parents_mux_ops,
2219 ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2223 ctx->parent.init = CLK_HW_INIT_HW("test-parent", &ctx->mux_ctx.hw,
2224 &empty_clk_ops, CLK_SET_RATE_PARENT);
2225 ret = clk_hw_register(NULL, &ctx->parent);
2229 ctx->hw.init = CLK_HW_INIT_HW("test-clock", &ctx->parent,
2230 &clk_leaf_mux_set_rate_parent_ops,
2231 CLK_SET_RATE_PARENT);
2232 ret = clk_hw_register(NULL, &ctx->hw);
2239 static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test)
2241 struct clk_leaf_mux_ctx *ctx = test->priv;
2243 clk_hw_unregister(&ctx->hw);
2244 clk_hw_unregister(&ctx->parent);
2245 clk_hw_unregister(&ctx->mux_ctx.hw);
2246 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2247 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2250 struct clk_leaf_mux_set_rate_parent_determine_rate_test_case {
2252 int (*determine_rate_func)(struct clk_hw *hw, struct clk_rate_request *req);
2256 clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc(
2257 const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *t, char *desc)
2259 strcpy(desc, t->desc);
2262 static const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case
2263 clk_leaf_mux_set_rate_parent_determine_rate_test_cases[] = {
2266 * Test that __clk_determine_rate() on the parent that can't
2267 * change rate doesn't return a clk_rate_request structure with
2268 * the best_parent_hw pointer pointing to the parent.
2270 .desc = "clk_leaf_mux_set_rate_parent__clk_determine_rate_proper_parent",
2271 .determine_rate_func = __clk_determine_rate,
2275 * Test that __clk_mux_determine_rate() on the parent that
2276 * can't change rate doesn't return a clk_rate_request
2277 * structure with the best_parent_hw pointer pointing to
2280 .desc = "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_proper_parent",
2281 .determine_rate_func = __clk_mux_determine_rate,
2285 * Test that __clk_mux_determine_rate_closest() on the parent
2286 * that can't change rate doesn't return a clk_rate_request
2287 * structure with the best_parent_hw pointer pointing to
2290 .desc = "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_closest_proper_parent",
2291 .determine_rate_func = __clk_mux_determine_rate_closest,
2295 * Test that clk_hw_determine_rate_no_reparent() on the parent
2296 * that can't change rate doesn't return a clk_rate_request
2297 * structure with the best_parent_hw pointer pointing to
2300 .desc = "clk_leaf_mux_set_rate_parent_clk_hw_determine_rate_no_reparent_proper_parent",
2301 .determine_rate_func = clk_hw_determine_rate_no_reparent,
2305 KUNIT_ARRAY_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,
2306 clk_leaf_mux_set_rate_parent_determine_rate_test_cases,
2307 clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc)
2310 * Test that when a clk that can't change rate itself calls a function like
2311 * __clk_determine_rate() on its parent it doesn't get back a clk_rate_request
2312 * structure that has the best_parent_hw pointer point to the clk_hw passed
2313 * into the determine rate function. See commit 262ca38f4b6e ("clk: Stop
2314 * forwarding clk_rate_requests to the parent") for more background.
2316 static void clk_leaf_mux_set_rate_parent_determine_rate_test(struct kunit *test)
2318 struct clk_leaf_mux_ctx *ctx = test->priv;
2319 struct clk_hw *hw = &ctx->hw;
2320 struct clk *clk = clk_hw_get_clk(hw, NULL);
2321 struct clk_rate_request req;
2323 const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *test_param;
2325 test_param = test->param_value;
2326 ctx->determine_rate_func = test_param->determine_rate_func;
2329 rate = clk_get_rate(clk);
2330 KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2331 KUNIT_ASSERT_EQ(test, DUMMY_CLOCK_RATE_2, clk_round_rate(clk, DUMMY_CLOCK_RATE_2));
2333 KUNIT_EXPECT_EQ(test, req.rate, DUMMY_CLOCK_RATE_2);
2334 KUNIT_EXPECT_EQ(test, req.best_parent_rate, DUMMY_CLOCK_RATE_2);
2335 KUNIT_EXPECT_PTR_EQ(test, req.best_parent_hw, &ctx->mux_ctx.hw);
2340 static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = {
2341 KUNIT_CASE_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,
2342 clk_leaf_mux_set_rate_parent_determine_rate_test_gen_params),
2347 * Test suite for a clock whose parent is a pass-through clk whose parent is a
2348 * mux with multiple parents. The leaf and pass-through clocks have the
2349 * CLK_SET_RATE_PARENT flag, and will forward rate requests to the mux, which
2350 * will then select which parent is the best fit for a given rate.
2352 * These tests exercise the behaviour of muxes, and the proper selection
2355 static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = {
2356 .name = "clk-leaf-mux-set-rate-parent",
2357 .init = clk_leaf_mux_set_rate_parent_test_init,
2358 .exit = clk_leaf_mux_set_rate_parent_test_exit,
2359 .test_cases = clk_leaf_mux_set_rate_parent_test_cases,
2362 struct clk_mux_notifier_rate_change {
2364 unsigned long old_rate;
2365 unsigned long new_rate;
2366 wait_queue_head_t wq;
2369 struct clk_mux_notifier_ctx {
2370 struct clk_multiple_parent_ctx mux_ctx;
2372 struct notifier_block clk_nb;
2373 struct clk_mux_notifier_rate_change pre_rate_change;
2374 struct clk_mux_notifier_rate_change post_rate_change;
2377 #define NOTIFIER_TIMEOUT_MS 100
2379 static int clk_mux_notifier_callback(struct notifier_block *nb,
2380 unsigned long action, void *data)
2382 struct clk_notifier_data *clk_data = data;
2383 struct clk_mux_notifier_ctx *ctx = container_of(nb,
2384 struct clk_mux_notifier_ctx,
2387 if (action & PRE_RATE_CHANGE) {
2388 ctx->pre_rate_change.old_rate = clk_data->old_rate;
2389 ctx->pre_rate_change.new_rate = clk_data->new_rate;
2390 ctx->pre_rate_change.done = true;
2391 wake_up_interruptible(&ctx->pre_rate_change.wq);
2394 if (action & POST_RATE_CHANGE) {
2395 ctx->post_rate_change.old_rate = clk_data->old_rate;
2396 ctx->post_rate_change.new_rate = clk_data->new_rate;
2397 ctx->post_rate_change.done = true;
2398 wake_up_interruptible(&ctx->post_rate_change.wq);
2404 static int clk_mux_notifier_test_init(struct kunit *test)
2406 struct clk_mux_notifier_ctx *ctx;
2407 const char *top_parents[2] = { "parent-0", "parent-1" };
2410 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2414 ctx->clk_nb.notifier_call = clk_mux_notifier_callback;
2415 init_waitqueue_head(&ctx->pre_rate_change.wq);
2416 init_waitqueue_head(&ctx->post_rate_change.wq);
2418 ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2419 &clk_dummy_rate_ops,
2421 ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2422 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2426 ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2427 &clk_dummy_rate_ops,
2429 ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2430 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2434 ctx->mux_ctx.current_parent = 0;
2435 ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2436 &clk_multiple_parents_mux_ops,
2438 ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2442 ctx->clk = clk_hw_get_clk(&ctx->mux_ctx.hw, NULL);
2443 ret = clk_notifier_register(ctx->clk, &ctx->clk_nb);
2450 static void clk_mux_notifier_test_exit(struct kunit *test)
2452 struct clk_mux_notifier_ctx *ctx = test->priv;
2453 struct clk *clk = ctx->clk;
2455 clk_notifier_unregister(clk, &ctx->clk_nb);
2458 clk_hw_unregister(&ctx->mux_ctx.hw);
2459 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2460 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2464 * Test that if the we have a notifier registered on a mux, the core
2465 * will notify us when we switch to another parent, and with the proper
2466 * old and new rates.
2468 static void clk_mux_notifier_set_parent_test(struct kunit *test)
2470 struct clk_mux_notifier_ctx *ctx = test->priv;
2471 struct clk_hw *hw = &ctx->mux_ctx.hw;
2472 struct clk *clk = clk_hw_get_clk(hw, NULL);
2473 struct clk *new_parent = clk_hw_get_clk(&ctx->mux_ctx.parents_ctx[1].hw, NULL);
2476 ret = clk_set_parent(clk, new_parent);
2477 KUNIT_ASSERT_EQ(test, ret, 0);
2479 ret = wait_event_interruptible_timeout(ctx->pre_rate_change.wq,
2480 ctx->pre_rate_change.done,
2481 msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
2482 KUNIT_ASSERT_GT(test, ret, 0);
2484 KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
2485 KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
2487 ret = wait_event_interruptible_timeout(ctx->post_rate_change.wq,
2488 ctx->post_rate_change.done,
2489 msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
2490 KUNIT_ASSERT_GT(test, ret, 0);
2492 KUNIT_EXPECT_EQ(test, ctx->post_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
2493 KUNIT_EXPECT_EQ(test, ctx->post_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
2495 clk_put(new_parent);
2499 static struct kunit_case clk_mux_notifier_test_cases[] = {
2500 KUNIT_CASE(clk_mux_notifier_set_parent_test),
2505 * Test suite for a mux with multiple parents, and a notifier registered
2508 * These tests exercise the behaviour of notifiers.
2510 static struct kunit_suite clk_mux_notifier_test_suite = {
2511 .name = "clk-mux-notifier",
2512 .init = clk_mux_notifier_test_init,
2513 .exit = clk_mux_notifier_test_exit,
2514 .test_cases = clk_mux_notifier_test_cases,
2518 clk_mux_no_reparent_test_init(struct kunit *test)
2520 struct clk_multiple_parent_ctx *ctx;
2521 const char *parents[2] = { "parent-0", "parent-1"};
2524 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2529 ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2530 &clk_dummy_rate_ops,
2532 ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2533 ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
2537 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2538 &clk_dummy_rate_ops,
2540 ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2541 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
2545 ctx->current_parent = 0;
2546 ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
2547 &clk_multiple_parents_no_reparent_mux_ops,
2549 ret = clk_hw_register(NULL, &ctx->hw);
2557 clk_mux_no_reparent_test_exit(struct kunit *test)
2559 struct clk_multiple_parent_ctx *ctx = test->priv;
2561 clk_hw_unregister(&ctx->hw);
2562 clk_hw_unregister(&ctx->parents_ctx[0].hw);
2563 clk_hw_unregister(&ctx->parents_ctx[1].hw);
2567 * Test that if the we have a mux that cannot change parent and we call
2568 * clk_round_rate() on it with a rate that should cause it to change
2571 static void clk_mux_no_reparent_round_rate(struct kunit *test)
2573 struct clk_multiple_parent_ctx *ctx = test->priv;
2574 struct clk_hw *hw = &ctx->hw;
2575 struct clk *clk = clk_hw_get_clk(hw, NULL);
2576 struct clk *other_parent, *parent;
2577 unsigned long other_parent_rate;
2578 unsigned long parent_rate;
2581 parent = clk_get_parent(clk);
2582 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
2584 parent_rate = clk_get_rate(parent);
2585 KUNIT_ASSERT_GT(test, parent_rate, 0);
2587 other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
2588 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
2589 KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
2591 other_parent_rate = clk_get_rate(other_parent);
2592 KUNIT_ASSERT_GT(test, other_parent_rate, 0);
2593 clk_put(other_parent);
2595 rounded_rate = clk_round_rate(clk, other_parent_rate);
2596 KUNIT_ASSERT_GT(test, rounded_rate, 0);
2597 KUNIT_EXPECT_EQ(test, rounded_rate, parent_rate);
2603 * Test that if the we have a mux that cannot change parent and we call
2604 * clk_set_rate() on it with a rate that should cause it to change
2607 static void clk_mux_no_reparent_set_rate(struct kunit *test)
2609 struct clk_multiple_parent_ctx *ctx = test->priv;
2610 struct clk_hw *hw = &ctx->hw;
2611 struct clk *clk = clk_hw_get_clk(hw, NULL);
2612 struct clk *other_parent, *parent;
2613 unsigned long other_parent_rate;
2614 unsigned long parent_rate;
2618 parent = clk_get_parent(clk);
2619 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
2621 parent_rate = clk_get_rate(parent);
2622 KUNIT_ASSERT_GT(test, parent_rate, 0);
2624 other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
2625 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
2626 KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
2628 other_parent_rate = clk_get_rate(other_parent);
2629 KUNIT_ASSERT_GT(test, other_parent_rate, 0);
2630 clk_put(other_parent);
2632 ret = clk_set_rate(clk, other_parent_rate);
2633 KUNIT_ASSERT_EQ(test, ret, 0);
2635 rate = clk_get_rate(clk);
2636 KUNIT_ASSERT_GT(test, rate, 0);
2637 KUNIT_EXPECT_EQ(test, rate, parent_rate);
2642 static struct kunit_case clk_mux_no_reparent_test_cases[] = {
2643 KUNIT_CASE(clk_mux_no_reparent_round_rate),
2644 KUNIT_CASE(clk_mux_no_reparent_set_rate),
2649 * Test suite for a clock mux that isn't allowed to change parent, using
2650 * the clk_hw_determine_rate_no_reparent() helper.
2652 * These tests exercise that helper, and the proper selection of
2653 * rates and parents.
2655 static struct kunit_suite clk_mux_no_reparent_test_suite = {
2656 .name = "clk-mux-no-reparent",
2657 .init = clk_mux_no_reparent_test_init,
2658 .exit = clk_mux_no_reparent_test_exit,
2659 .test_cases = clk_mux_no_reparent_test_cases,
2663 &clk_leaf_mux_set_rate_parent_test_suite,
2665 &clk_multiple_parents_mux_test_suite,
2666 &clk_mux_no_reparent_test_suite,
2667 &clk_mux_notifier_test_suite,
2668 &clk_orphan_transparent_multiple_parent_mux_test_suite,
2669 &clk_orphan_transparent_single_parent_test_suite,
2670 &clk_orphan_two_level_root_last_test_suite,
2671 &clk_range_test_suite,
2672 &clk_range_maximize_test_suite,
2673 &clk_range_minimize_test_suite,
2674 &clk_single_parent_mux_test_suite,
2675 &clk_uncached_test_suite
2677 MODULE_LICENSE("GPL v2");