1 // SPDX-License-Identifier: GPL-2.0
3 * Kunit tests for clk framework
6 #include <linux/clk-provider.h>
8 #include <linux/platform_device.h>
10 /* Needed for clk_hw_get_clk() */
13 #include <kunit/clk.h>
15 #include <kunit/platform_device.h>
16 #include <kunit/test.h>
18 #include "clk_parent_data_test.h"
20 static const struct clk_ops empty_clk_ops = { };
22 #define DUMMY_CLOCK_INIT_RATE (42 * 1000 * 1000)
23 #define DUMMY_CLOCK_RATE_1 (142 * 1000 * 1000)
24 #define DUMMY_CLOCK_RATE_2 (242 * 1000 * 1000)
26 struct clk_dummy_context {
31 static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw,
32 unsigned long parent_rate)
34 struct clk_dummy_context *ctx =
35 container_of(hw, struct clk_dummy_context, hw);
40 static int clk_dummy_determine_rate(struct clk_hw *hw,
41 struct clk_rate_request *req)
43 /* Just return the same rate without modifying it */
47 static int clk_dummy_maximize_rate(struct clk_hw *hw,
48 struct clk_rate_request *req)
51 * If there's a maximum set, always run the clock at the maximum
54 if (req->max_rate < ULONG_MAX)
55 req->rate = req->max_rate;
60 static int clk_dummy_minimize_rate(struct clk_hw *hw,
61 struct clk_rate_request *req)
64 * If there's a minimum set, always run the clock at the minimum
67 if (req->min_rate > 0)
68 req->rate = req->min_rate;
73 static int clk_dummy_set_rate(struct clk_hw *hw,
75 unsigned long parent_rate)
77 struct clk_dummy_context *ctx =
78 container_of(hw, struct clk_dummy_context, hw);
84 static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
86 if (index >= clk_hw_get_num_parents(hw))
92 static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
97 static const struct clk_ops clk_dummy_rate_ops = {
98 .recalc_rate = clk_dummy_recalc_rate,
99 .determine_rate = clk_dummy_determine_rate,
100 .set_rate = clk_dummy_set_rate,
103 static const struct clk_ops clk_dummy_maximize_rate_ops = {
104 .recalc_rate = clk_dummy_recalc_rate,
105 .determine_rate = clk_dummy_maximize_rate,
106 .set_rate = clk_dummy_set_rate,
109 static const struct clk_ops clk_dummy_minimize_rate_ops = {
110 .recalc_rate = clk_dummy_recalc_rate,
111 .determine_rate = clk_dummy_minimize_rate,
112 .set_rate = clk_dummy_set_rate,
115 static const struct clk_ops clk_dummy_single_parent_ops = {
117 * FIXME: Even though we should probably be able to use
118 * __clk_mux_determine_rate() here, if we use it and call
119 * clk_round_rate() or clk_set_rate() with a rate lower than
120 * what all the parents can provide, it will return -EINVAL.
122 * This is due to the fact that it has the undocumented
123 * behaviour to always pick up the closest rate higher than the
124 * requested rate. If we get something lower, it thus considers
125 * that it's not acceptable and will return an error.
127 * It's somewhat inconsistent and creates a weird threshold
128 * between rates above the parent rate which would be rounded to
129 * what the parent can provide, but rates below will simply
132 .determine_rate = __clk_mux_determine_rate_closest,
133 .set_parent = clk_dummy_single_set_parent,
134 .get_parent = clk_dummy_single_get_parent,
137 struct clk_multiple_parent_ctx {
138 struct clk_dummy_context parents_ctx[2];
143 static int clk_multiple_parents_mux_set_parent(struct clk_hw *hw, u8 index)
145 struct clk_multiple_parent_ctx *ctx =
146 container_of(hw, struct clk_multiple_parent_ctx, hw);
148 if (index >= clk_hw_get_num_parents(hw))
151 ctx->current_parent = index;
156 static u8 clk_multiple_parents_mux_get_parent(struct clk_hw *hw)
158 struct clk_multiple_parent_ctx *ctx =
159 container_of(hw, struct clk_multiple_parent_ctx, hw);
161 return ctx->current_parent;
164 static const struct clk_ops clk_multiple_parents_mux_ops = {
165 .get_parent = clk_multiple_parents_mux_get_parent,
166 .set_parent = clk_multiple_parents_mux_set_parent,
167 .determine_rate = __clk_mux_determine_rate_closest,
170 static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = {
171 .determine_rate = clk_hw_determine_rate_no_reparent,
172 .get_parent = clk_multiple_parents_mux_get_parent,
173 .set_parent = clk_multiple_parents_mux_set_parent,
176 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
178 struct clk_dummy_context *ctx;
179 struct clk_init_data init = { };
182 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
185 ctx->rate = DUMMY_CLOCK_INIT_RATE;
188 init.name = "test_dummy_rate";
190 ctx->hw.init = &init;
192 ret = clk_hw_register(NULL, &ctx->hw);
199 static int clk_test_init(struct kunit *test)
201 return clk_test_init_with_ops(test, &clk_dummy_rate_ops);
204 static int clk_maximize_test_init(struct kunit *test)
206 return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops);
209 static int clk_minimize_test_init(struct kunit *test)
211 return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops);
214 static void clk_test_exit(struct kunit *test)
216 struct clk_dummy_context *ctx = test->priv;
218 clk_hw_unregister(&ctx->hw);
222 * Test that the actual rate matches what is returned by clk_get_rate()
224 static void clk_test_get_rate(struct kunit *test)
226 struct clk_dummy_context *ctx = test->priv;
227 struct clk_hw *hw = &ctx->hw;
228 struct clk *clk = clk_hw_get_clk(hw, NULL);
231 rate = clk_get_rate(clk);
232 KUNIT_ASSERT_GT(test, rate, 0);
233 KUNIT_EXPECT_EQ(test, rate, ctx->rate);
239 * Test that, after a call to clk_set_rate(), the rate returned by
240 * clk_get_rate() matches.
242 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
243 * modify the requested rate, which is our case in clk_dummy_rate_ops.
245 static void clk_test_set_get_rate(struct kunit *test)
247 struct clk_dummy_context *ctx = test->priv;
248 struct clk_hw *hw = &ctx->hw;
249 struct clk *clk = clk_hw_get_clk(hw, NULL);
252 KUNIT_ASSERT_EQ(test,
253 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
256 rate = clk_get_rate(clk);
257 KUNIT_ASSERT_GT(test, rate, 0);
258 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
264 * Test that, after several calls to clk_set_rate(), the rate returned
265 * by clk_get_rate() matches the last one.
267 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
268 * modify the requested rate, which is our case in clk_dummy_rate_ops.
270 static void clk_test_set_set_get_rate(struct kunit *test)
272 struct clk_dummy_context *ctx = test->priv;
273 struct clk_hw *hw = &ctx->hw;
274 struct clk *clk = clk_hw_get_clk(hw, NULL);
277 KUNIT_ASSERT_EQ(test,
278 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
281 KUNIT_ASSERT_EQ(test,
282 clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
285 rate = clk_get_rate(clk);
286 KUNIT_ASSERT_GT(test, rate, 0);
287 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
293 * Test that clk_round_rate and clk_set_rate are consitent and will
294 * return the same frequency.
296 static void clk_test_round_set_get_rate(struct kunit *test)
298 struct clk_dummy_context *ctx = test->priv;
299 struct clk_hw *hw = &ctx->hw;
300 struct clk *clk = clk_hw_get_clk(hw, NULL);
301 unsigned long set_rate;
304 rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
305 KUNIT_ASSERT_GT(test, rounded_rate, 0);
306 KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
308 KUNIT_ASSERT_EQ(test,
309 clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
312 set_rate = clk_get_rate(clk);
313 KUNIT_ASSERT_GT(test, set_rate, 0);
314 KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
319 static struct kunit_case clk_test_cases[] = {
320 KUNIT_CASE(clk_test_get_rate),
321 KUNIT_CASE(clk_test_set_get_rate),
322 KUNIT_CASE(clk_test_set_set_get_rate),
323 KUNIT_CASE(clk_test_round_set_get_rate),
328 * Test suite for a basic rate clock, without any parent.
330 * These tests exercise the rate API with simple scenarios
332 static struct kunit_suite clk_test_suite = {
334 .init = clk_test_init,
335 .exit = clk_test_exit,
336 .test_cases = clk_test_cases,
339 static int clk_uncached_test_init(struct kunit *test)
341 struct clk_dummy_context *ctx;
344 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
349 ctx->rate = DUMMY_CLOCK_INIT_RATE;
350 ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
352 CLK_GET_RATE_NOCACHE);
354 ret = clk_hw_register(NULL, &ctx->hw);
362 * Test that for an uncached clock, the clock framework doesn't cache
363 * the rate and clk_get_rate() will return the underlying clock rate
364 * even if it changed.
366 static void clk_test_uncached_get_rate(struct kunit *test)
368 struct clk_dummy_context *ctx = test->priv;
369 struct clk_hw *hw = &ctx->hw;
370 struct clk *clk = clk_hw_get_clk(hw, NULL);
373 rate = clk_get_rate(clk);
374 KUNIT_ASSERT_GT(test, rate, 0);
375 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
377 /* We change the rate behind the clock framework's back */
378 ctx->rate = DUMMY_CLOCK_RATE_1;
379 rate = clk_get_rate(clk);
380 KUNIT_ASSERT_GT(test, rate, 0);
381 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
387 * Test that for an uncached clock, clk_set_rate_range() will work
388 * properly if the rate hasn't changed.
390 static void clk_test_uncached_set_range(struct kunit *test)
392 struct clk_dummy_context *ctx = test->priv;
393 struct clk_hw *hw = &ctx->hw;
394 struct clk *clk = clk_hw_get_clk(hw, NULL);
397 KUNIT_ASSERT_EQ(test,
398 clk_set_rate_range(clk,
403 rate = clk_get_rate(clk);
404 KUNIT_ASSERT_GT(test, rate, 0);
405 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
406 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
412 * Test that for an uncached clock, clk_set_rate_range() will work
413 * properly if the rate has changed in hardware.
415 * In this case, it means that if the rate wasn't initially in the range
416 * we're trying to set, but got changed at some point into the range
417 * without the kernel knowing about it, its rate shouldn't be affected.
419 static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
421 struct clk_dummy_context *ctx = test->priv;
422 struct clk_hw *hw = &ctx->hw;
423 struct clk *clk = clk_hw_get_clk(hw, NULL);
426 /* We change the rate behind the clock framework's back */
427 ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
428 KUNIT_ASSERT_EQ(test,
429 clk_set_rate_range(clk,
434 rate = clk_get_rate(clk);
435 KUNIT_ASSERT_GT(test, rate, 0);
436 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
441 static struct kunit_case clk_uncached_test_cases[] = {
442 KUNIT_CASE(clk_test_uncached_get_rate),
443 KUNIT_CASE(clk_test_uncached_set_range),
444 KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
449 * Test suite for a basic, uncached, rate clock, without any parent.
451 * These tests exercise the rate API with simple scenarios
453 static struct kunit_suite clk_uncached_test_suite = {
454 .name = "clk-uncached-test",
455 .init = clk_uncached_test_init,
456 .exit = clk_test_exit,
457 .test_cases = clk_uncached_test_cases,
461 clk_multiple_parents_mux_test_init(struct kunit *test)
463 struct clk_multiple_parent_ctx *ctx;
464 const char *parents[2] = { "parent-0", "parent-1"};
467 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
472 ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
475 ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
476 ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
480 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
483 ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
484 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
488 ctx->current_parent = 0;
489 ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
490 &clk_multiple_parents_mux_ops,
491 CLK_SET_RATE_PARENT);
492 ret = clk_hw_register(NULL, &ctx->hw);
500 clk_multiple_parents_mux_test_exit(struct kunit *test)
502 struct clk_multiple_parent_ctx *ctx = test->priv;
504 clk_hw_unregister(&ctx->hw);
505 clk_hw_unregister(&ctx->parents_ctx[0].hw);
506 clk_hw_unregister(&ctx->parents_ctx[1].hw);
510 * Test that for a clock with multiple parents, clk_get_parent()
511 * actually returns the current one.
514 clk_test_multiple_parents_mux_get_parent(struct kunit *test)
516 struct clk_multiple_parent_ctx *ctx = test->priv;
517 struct clk_hw *hw = &ctx->hw;
518 struct clk *clk = clk_hw_get_clk(hw, NULL);
519 struct clk *parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
521 KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
528 * Test that for a clock with a multiple parents, clk_has_parent()
529 * actually reports all of them as parents.
532 clk_test_multiple_parents_mux_has_parent(struct kunit *test)
534 struct clk_multiple_parent_ctx *ctx = test->priv;
535 struct clk_hw *hw = &ctx->hw;
536 struct clk *clk = clk_hw_get_clk(hw, NULL);
539 parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
540 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
543 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
544 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
551 * Test that for a clock with a multiple parents, if we set a range on
552 * that clock and the parent is changed, its rate after the reparenting
553 * is still within the range we asked for.
555 * FIXME: clk_set_parent() only does the reparenting but doesn't
556 * reevaluate whether the new clock rate is within its boundaries or
560 clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
562 struct clk_multiple_parent_ctx *ctx = test->priv;
563 struct clk_hw *hw = &ctx->hw;
564 struct clk *clk = clk_hw_get_clk(hw, NULL);
565 struct clk *parent1, *parent2;
569 kunit_skip(test, "This needs to be fixed in the core.");
571 parent1 = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
572 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent1);
573 KUNIT_ASSERT_TRUE(test, clk_is_match(clk_get_parent(clk), parent1));
575 parent2 = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
576 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent2);
578 ret = clk_set_rate(parent1, DUMMY_CLOCK_RATE_1);
579 KUNIT_ASSERT_EQ(test, ret, 0);
581 ret = clk_set_rate(parent2, DUMMY_CLOCK_RATE_2);
582 KUNIT_ASSERT_EQ(test, ret, 0);
584 ret = clk_set_rate_range(clk,
585 DUMMY_CLOCK_RATE_1 - 1000,
586 DUMMY_CLOCK_RATE_1 + 1000);
587 KUNIT_ASSERT_EQ(test, ret, 0);
589 ret = clk_set_parent(clk, parent2);
590 KUNIT_ASSERT_EQ(test, ret, 0);
592 rate = clk_get_rate(clk);
593 KUNIT_ASSERT_GT(test, rate, 0);
594 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
595 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
602 static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
603 KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
604 KUNIT_CASE(clk_test_multiple_parents_mux_has_parent),
605 KUNIT_CASE(clk_test_multiple_parents_mux_set_range_set_parent_get_rate),
610 * Test suite for a basic mux clock with two parents, with
611 * CLK_SET_RATE_PARENT on the child.
613 * These tests exercise the consumer API and check that the state of the
614 * child and parents are sane and consistent.
616 static struct kunit_suite
617 clk_multiple_parents_mux_test_suite = {
618 .name = "clk-multiple-parents-mux-test",
619 .init = clk_multiple_parents_mux_test_init,
620 .exit = clk_multiple_parents_mux_test_exit,
621 .test_cases = clk_multiple_parents_mux_test_cases,
625 clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test)
627 struct clk_multiple_parent_ctx *ctx;
628 const char *parents[2] = { "missing-parent", "proper-parent"};
631 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
636 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("proper-parent",
639 ctx->parents_ctx[1].rate = DUMMY_CLOCK_INIT_RATE;
640 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
644 ctx->hw.init = CLK_HW_INIT_PARENTS("test-orphan-mux", parents,
645 &clk_multiple_parents_mux_ops,
646 CLK_SET_RATE_PARENT);
647 ret = clk_hw_register(NULL, &ctx->hw);
655 clk_orphan_transparent_multiple_parent_mux_test_exit(struct kunit *test)
657 struct clk_multiple_parent_ctx *ctx = test->priv;
659 clk_hw_unregister(&ctx->hw);
660 clk_hw_unregister(&ctx->parents_ctx[1].hw);
664 * Test that, for a mux whose current parent hasn't been registered yet and is
665 * thus orphan, clk_get_parent() will return NULL.
668 clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit *test)
670 struct clk_multiple_parent_ctx *ctx = test->priv;
671 struct clk_hw *hw = &ctx->hw;
672 struct clk *clk = clk_hw_get_clk(hw, NULL);
674 KUNIT_EXPECT_PTR_EQ(test, clk_get_parent(clk), NULL);
680 * Test that, for a mux whose current parent hasn't been registered yet,
681 * calling clk_set_parent() to a valid parent will properly update the
682 * mux parent and its orphan status.
685 clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit *test)
687 struct clk_multiple_parent_ctx *ctx = test->priv;
688 struct clk_hw *hw = &ctx->hw;
689 struct clk *clk = clk_hw_get_clk(hw, NULL);
690 struct clk *parent, *new_parent;
693 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
694 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
696 ret = clk_set_parent(clk, parent);
697 KUNIT_ASSERT_EQ(test, ret, 0);
699 new_parent = clk_get_parent(clk);
700 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
701 KUNIT_EXPECT_TRUE(test, clk_is_match(parent, new_parent));
708 * Test that, for a mux that started orphan but got switched to a valid
709 * parent, calling clk_drop_range() on the mux won't affect the parent
713 clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit *test)
715 struct clk_multiple_parent_ctx *ctx = test->priv;
716 struct clk_hw *hw = &ctx->hw;
717 struct clk *clk = clk_hw_get_clk(hw, NULL);
719 unsigned long parent_rate, new_parent_rate;
722 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
723 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
725 parent_rate = clk_get_rate(parent);
726 KUNIT_ASSERT_GT(test, parent_rate, 0);
728 ret = clk_set_parent(clk, parent);
729 KUNIT_ASSERT_EQ(test, ret, 0);
731 ret = clk_drop_range(clk);
732 KUNIT_ASSERT_EQ(test, ret, 0);
734 new_parent_rate = clk_get_rate(clk);
735 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
736 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
743 * Test that, for a mux that started orphan but got switched to a valid
744 * parent, the rate of the mux and its new parent are consistent.
747 clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit *test)
749 struct clk_multiple_parent_ctx *ctx = test->priv;
750 struct clk_hw *hw = &ctx->hw;
751 struct clk *clk = clk_hw_get_clk(hw, NULL);
753 unsigned long parent_rate, rate;
756 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
757 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
759 parent_rate = clk_get_rate(parent);
760 KUNIT_ASSERT_GT(test, parent_rate, 0);
762 ret = clk_set_parent(clk, parent);
763 KUNIT_ASSERT_EQ(test, ret, 0);
765 rate = clk_get_rate(clk);
766 KUNIT_ASSERT_GT(test, rate, 0);
767 KUNIT_EXPECT_EQ(test, parent_rate, rate);
774 * Test that, for a mux that started orphan but got switched to a valid
775 * parent, calling clk_put() on the mux won't affect the parent rate.
778 clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit *test)
780 struct clk_multiple_parent_ctx *ctx = test->priv;
781 struct clk *clk, *parent;
782 unsigned long parent_rate, new_parent_rate;
785 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
786 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
788 clk = clk_hw_get_clk(&ctx->hw, NULL);
789 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
791 parent_rate = clk_get_rate(parent);
792 KUNIT_ASSERT_GT(test, parent_rate, 0);
794 ret = clk_set_parent(clk, parent);
795 KUNIT_ASSERT_EQ(test, ret, 0);
799 new_parent_rate = clk_get_rate(parent);
800 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
801 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
807 * Test that, for a mux that started orphan but got switched to a valid
808 * parent, calling clk_set_rate_range() will affect the parent state if
809 * its rate is out of range.
812 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(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);
821 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
822 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
824 ret = clk_set_parent(clk, parent);
825 KUNIT_ASSERT_EQ(test, ret, 0);
827 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
828 KUNIT_ASSERT_EQ(test, ret, 0);
830 rate = clk_get_rate(clk);
831 KUNIT_ASSERT_GT(test, rate, 0);
832 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
833 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
840 * Test that, for a mux that started orphan but got switched to a valid
841 * parent, calling clk_set_rate_range() won't affect the parent state if
842 * its rate is within range.
845 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit *test)
847 struct clk_multiple_parent_ctx *ctx = test->priv;
848 struct clk_hw *hw = &ctx->hw;
849 struct clk *clk = clk_hw_get_clk(hw, NULL);
851 unsigned long parent_rate, new_parent_rate;
854 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
855 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
857 parent_rate = clk_get_rate(parent);
858 KUNIT_ASSERT_GT(test, parent_rate, 0);
860 ret = clk_set_parent(clk, parent);
861 KUNIT_ASSERT_EQ(test, ret, 0);
863 ret = clk_set_rate_range(clk,
864 DUMMY_CLOCK_INIT_RATE - 1000,
865 DUMMY_CLOCK_INIT_RATE + 1000);
866 KUNIT_ASSERT_EQ(test, ret, 0);
868 new_parent_rate = clk_get_rate(parent);
869 KUNIT_ASSERT_GT(test, new_parent_rate, 0);
870 KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
877 * Test that, for a mux whose current parent hasn't been registered yet,
878 * calling clk_set_rate_range() will succeed, and will be taken into
879 * account when rounding a rate.
882 clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit *test)
884 struct clk_multiple_parent_ctx *ctx = test->priv;
885 struct clk_hw *hw = &ctx->hw;
886 struct clk *clk = clk_hw_get_clk(hw, NULL);
890 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
891 KUNIT_ASSERT_EQ(test, ret, 0);
893 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
894 KUNIT_ASSERT_GT(test, rate, 0);
895 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
896 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
902 * Test that, for a mux that started orphan, was assigned and rate and
903 * then got switched to a valid parent, its rate is eventually within
906 * FIXME: Even though we update the rate as part of clk_set_parent(), we
907 * don't evaluate whether that new rate is within range and needs to be
911 clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit *test)
913 struct clk_multiple_parent_ctx *ctx = test->priv;
914 struct clk_hw *hw = &ctx->hw;
915 struct clk *clk = clk_hw_get_clk(hw, NULL);
920 kunit_skip(test, "This needs to be fixed in the core.");
922 clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
924 parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
925 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
927 ret = clk_set_parent(clk, parent);
928 KUNIT_ASSERT_EQ(test, ret, 0);
930 rate = clk_get_rate(clk);
931 KUNIT_ASSERT_GT(test, rate, 0);
932 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
933 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
939 static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = {
940 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_get_parent),
941 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent),
942 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range),
943 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate),
944 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_put),
945 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified),
946 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched),
947 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate),
948 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate),
953 * Test suite for a basic mux clock with two parents. The default parent
954 * isn't registered, only the second parent is. By default, the clock
955 * will thus be orphan.
957 * These tests exercise the behaviour of the consumer API when dealing
958 * with an orphan clock, and how we deal with the transition to a valid
961 static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = {
962 .name = "clk-orphan-transparent-multiple-parent-mux-test",
963 .init = clk_orphan_transparent_multiple_parent_mux_test_init,
964 .exit = clk_orphan_transparent_multiple_parent_mux_test_exit,
965 .test_cases = clk_orphan_transparent_multiple_parent_mux_test_cases,
968 struct clk_single_parent_ctx {
969 struct clk_dummy_context parent_ctx;
973 static int clk_single_parent_mux_test_init(struct kunit *test)
975 struct clk_single_parent_ctx *ctx;
978 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
983 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
984 ctx->parent_ctx.hw.init =
985 CLK_HW_INIT_NO_PARENT("parent-clk",
989 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
993 ctx->hw.init = CLK_HW_INIT("test-clk", "parent-clk",
994 &clk_dummy_single_parent_ops,
995 CLK_SET_RATE_PARENT);
997 ret = clk_hw_register(NULL, &ctx->hw);
1005 clk_single_parent_mux_test_exit(struct kunit *test)
1007 struct clk_single_parent_ctx *ctx = test->priv;
1009 clk_hw_unregister(&ctx->hw);
1010 clk_hw_unregister(&ctx->parent_ctx.hw);
1014 * Test that for a clock with a single parent, clk_get_parent() actually
1015 * returns the parent.
1018 clk_test_single_parent_mux_get_parent(struct kunit *test)
1020 struct clk_single_parent_ctx *ctx = test->priv;
1021 struct clk_hw *hw = &ctx->hw;
1022 struct clk *clk = clk_hw_get_clk(hw, NULL);
1023 struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
1025 KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
1032 * Test that for a clock with a single parent, clk_has_parent() actually
1033 * reports it as a parent.
1036 clk_test_single_parent_mux_has_parent(struct kunit *test)
1038 struct clk_single_parent_ctx *ctx = test->priv;
1039 struct clk_hw *hw = &ctx->hw;
1040 struct clk *clk = clk_hw_get_clk(hw, NULL);
1041 struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
1043 KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
1050 * Test that for a clock that can't modify its rate and with a single
1051 * parent, if we set disjoints range on the parent and then the child,
1052 * the second will return an error.
1054 * FIXME: clk_set_rate_range() only considers the current clock when
1055 * evaluating whether ranges are disjoints and not the upstream clocks
1059 clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
1061 struct clk_single_parent_ctx *ctx = test->priv;
1062 struct clk_hw *hw = &ctx->hw;
1063 struct clk *clk = clk_hw_get_clk(hw, NULL);
1067 kunit_skip(test, "This needs to be fixed in the core.");
1069 parent = clk_get_parent(clk);
1070 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1072 ret = clk_set_rate_range(parent, 1000, 2000);
1073 KUNIT_ASSERT_EQ(test, ret, 0);
1075 ret = clk_set_rate_range(clk, 3000, 4000);
1076 KUNIT_EXPECT_LT(test, ret, 0);
1082 * Test that for a clock that can't modify its rate and with a single
1083 * parent, if we set disjoints range on the child and then the parent,
1084 * the second will return an error.
1086 * FIXME: clk_set_rate_range() only considers the current clock when
1087 * evaluating whether ranges are disjoints and not the downstream clocks
1091 clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
1093 struct clk_single_parent_ctx *ctx = test->priv;
1094 struct clk_hw *hw = &ctx->hw;
1095 struct clk *clk = clk_hw_get_clk(hw, NULL);
1099 kunit_skip(test, "This needs to be fixed in the core.");
1101 parent = clk_get_parent(clk);
1102 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1104 ret = clk_set_rate_range(clk, 1000, 2000);
1105 KUNIT_ASSERT_EQ(test, ret, 0);
1107 ret = clk_set_rate_range(parent, 3000, 4000);
1108 KUNIT_EXPECT_LT(test, ret, 0);
1114 * Test that for a clock that can't modify its rate and with a single
1115 * parent, if we set a range on the parent and then call
1116 * clk_round_rate(), the boundaries of the parent are taken into
1120 clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test)
1122 struct clk_single_parent_ctx *ctx = test->priv;
1123 struct clk_hw *hw = &ctx->hw;
1124 struct clk *clk = clk_hw_get_clk(hw, NULL);
1129 parent = clk_get_parent(clk);
1130 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1132 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
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);
1138 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1144 * Test that for a clock that can't modify its rate and with a single
1145 * parent, if we set a range on the parent and a more restrictive one on
1146 * the child, and then call clk_round_rate(), the boundaries of the
1147 * two clocks are taken into account.
1150 clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test)
1152 struct clk_single_parent_ctx *ctx = test->priv;
1153 struct clk_hw *hw = &ctx->hw;
1154 struct clk *clk = clk_hw_get_clk(hw, NULL);
1159 parent = clk_get_parent(clk);
1160 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1162 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1163 KUNIT_ASSERT_EQ(test, ret, 0);
1165 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1166 KUNIT_ASSERT_EQ(test, ret, 0);
1168 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1169 KUNIT_ASSERT_GT(test, rate, 0);
1170 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1171 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1173 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 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);
1182 * Test that for a clock that can't modify its rate and with a single
1183 * parent, if we set a range on the child and a more restrictive one on
1184 * the parent, and then call clk_round_rate(), the boundaries of the
1185 * two clocks are taken into account.
1188 clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test)
1190 struct clk_single_parent_ctx *ctx = test->priv;
1191 struct clk_hw *hw = &ctx->hw;
1192 struct clk *clk = clk_hw_get_clk(hw, NULL);
1197 parent = clk_get_parent(clk);
1198 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1200 ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1201 KUNIT_ASSERT_EQ(test, ret, 0);
1203 ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1204 KUNIT_ASSERT_EQ(test, ret, 0);
1206 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1207 KUNIT_ASSERT_GT(test, rate, 0);
1208 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1209 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1211 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1212 KUNIT_ASSERT_GT(test, rate, 0);
1213 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1214 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1219 static struct kunit_case clk_single_parent_mux_test_cases[] = {
1220 KUNIT_CASE(clk_test_single_parent_mux_get_parent),
1221 KUNIT_CASE(clk_test_single_parent_mux_has_parent),
1222 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
1223 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
1224 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),
1225 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only),
1226 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller),
1231 * Test suite for a basic mux clock with one parent, with
1232 * CLK_SET_RATE_PARENT on the child.
1234 * These tests exercise the consumer API and check that the state of the
1235 * child and parent are sane and consistent.
1237 static struct kunit_suite
1238 clk_single_parent_mux_test_suite = {
1239 .name = "clk-single-parent-mux-test",
1240 .init = clk_single_parent_mux_test_init,
1241 .exit = clk_single_parent_mux_test_exit,
1242 .test_cases = clk_single_parent_mux_test_cases,
1245 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
1247 struct clk_single_parent_ctx *ctx;
1248 struct clk_init_data init = { };
1249 const char * const parents[] = { "orphan_parent" };
1252 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1257 init.name = "test_orphan_dummy_parent";
1258 init.ops = &clk_dummy_single_parent_ops;
1259 init.parent_names = parents;
1260 init.num_parents = ARRAY_SIZE(parents);
1261 init.flags = CLK_SET_RATE_PARENT;
1262 ctx->hw.init = &init;
1264 ret = clk_hw_register(NULL, &ctx->hw);
1268 memset(&init, 0, sizeof(init));
1269 init.name = "orphan_parent";
1270 init.ops = &clk_dummy_rate_ops;
1271 ctx->parent_ctx.hw.init = &init;
1272 ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1274 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1282 * Test that a mux-only clock, with an initial rate within a range,
1283 * will still have the same rate after the range has been enforced.
1288 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
1290 struct clk_single_parent_ctx *ctx = test->priv;
1291 struct clk_hw *hw = &ctx->hw;
1292 struct clk *clk = clk_hw_get_clk(hw, NULL);
1293 unsigned long rate, new_rate;
1295 rate = clk_get_rate(clk);
1296 KUNIT_ASSERT_GT(test, rate, 0);
1298 KUNIT_ASSERT_EQ(test,
1299 clk_set_rate_range(clk,
1300 ctx->parent_ctx.rate - 1000,
1301 ctx->parent_ctx.rate + 1000),
1304 new_rate = clk_get_rate(clk);
1305 KUNIT_ASSERT_GT(test, new_rate, 0);
1306 KUNIT_EXPECT_EQ(test, rate, new_rate);
1311 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
1312 KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
1317 * Test suite for a basic mux clock with one parent. The parent is
1318 * registered after its child. The clock will thus be an orphan when
1319 * registered, but will no longer be when the tests run.
1321 * These tests make sure a clock that used to be orphan has a sane,
1322 * consistent, behaviour.
1324 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
1325 .name = "clk-orphan-transparent-single-parent-test",
1326 .init = clk_orphan_transparent_single_parent_mux_test_init,
1327 .exit = clk_single_parent_mux_test_exit,
1328 .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
1331 struct clk_single_parent_two_lvl_ctx {
1332 struct clk_dummy_context parent_parent_ctx;
1333 struct clk_dummy_context parent_ctx;
1338 clk_orphan_two_level_root_last_test_init(struct kunit *test)
1340 struct clk_single_parent_two_lvl_ctx *ctx;
1343 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1348 ctx->parent_ctx.hw.init =
1349 CLK_HW_INIT("intermediate-parent",
1351 &clk_dummy_single_parent_ops,
1352 CLK_SET_RATE_PARENT);
1353 ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1358 CLK_HW_INIT("test-clk", "intermediate-parent",
1359 &clk_dummy_single_parent_ops,
1360 CLK_SET_RATE_PARENT);
1361 ret = clk_hw_register(NULL, &ctx->hw);
1365 ctx->parent_parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1366 ctx->parent_parent_ctx.hw.init =
1367 CLK_HW_INIT_NO_PARENT("root-parent",
1368 &clk_dummy_rate_ops,
1370 ret = clk_hw_register(NULL, &ctx->parent_parent_ctx.hw);
1378 clk_orphan_two_level_root_last_test_exit(struct kunit *test)
1380 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1382 clk_hw_unregister(&ctx->hw);
1383 clk_hw_unregister(&ctx->parent_ctx.hw);
1384 clk_hw_unregister(&ctx->parent_parent_ctx.hw);
1388 * Test that, for a clock whose parent used to be orphan, clk_get_rate()
1389 * will return the proper rate.
1392 clk_orphan_two_level_root_last_test_get_rate(struct kunit *test)
1394 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1395 struct clk_hw *hw = &ctx->hw;
1396 struct clk *clk = clk_hw_get_clk(hw, NULL);
1399 rate = clk_get_rate(clk);
1400 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1406 * Test that, for a clock whose parent used to be orphan,
1407 * clk_set_rate_range() won't affect its rate if it is already within
1410 * See (for Exynos 4210):
1414 clk_orphan_two_level_root_last_test_set_range(struct kunit *test)
1416 struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1417 struct clk_hw *hw = &ctx->hw;
1418 struct clk *clk = clk_hw_get_clk(hw, NULL);
1422 ret = clk_set_rate_range(clk,
1423 DUMMY_CLOCK_INIT_RATE - 1000,
1424 DUMMY_CLOCK_INIT_RATE + 1000);
1425 KUNIT_ASSERT_EQ(test, ret, 0);
1427 rate = clk_get_rate(clk);
1428 KUNIT_ASSERT_GT(test, rate, 0);
1429 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1434 static struct kunit_case
1435 clk_orphan_two_level_root_last_test_cases[] = {
1436 KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate),
1437 KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range),
1442 * Test suite for a basic, transparent, clock with a parent that is also
1443 * such a clock. The parent's parent is registered last, while the
1444 * parent and its child are registered in that order. The intermediate
1445 * and leaf clocks will thus be orphan when registered, but the leaf
1446 * clock itself will always have its parent and will never be
1447 * reparented. Indeed, it's only orphan because its parent is.
1449 * These tests exercise the behaviour of the consumer API when dealing
1450 * with an orphan clock, and how we deal with the transition to a valid
1453 static struct kunit_suite
1454 clk_orphan_two_level_root_last_test_suite = {
1455 .name = "clk-orphan-two-level-root-last-test",
1456 .init = clk_orphan_two_level_root_last_test_init,
1457 .exit = clk_orphan_two_level_root_last_test_exit,
1458 .test_cases = clk_orphan_two_level_root_last_test_cases,
1462 * Test that clk_set_rate_range won't return an error for a valid range
1463 * and that it will make sure the rate of the clock is within the
1466 static void clk_range_test_set_range(struct kunit *test)
1468 struct clk_dummy_context *ctx = test->priv;
1469 struct clk_hw *hw = &ctx->hw;
1470 struct clk *clk = clk_hw_get_clk(hw, NULL);
1473 KUNIT_ASSERT_EQ(test,
1474 clk_set_rate_range(clk,
1476 DUMMY_CLOCK_RATE_2),
1479 rate = clk_get_rate(clk);
1480 KUNIT_ASSERT_GT(test, rate, 0);
1481 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1482 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1488 * Test that calling clk_set_rate_range with a minimum rate higher than
1489 * the maximum rate returns an error.
1491 static void clk_range_test_set_range_invalid(struct kunit *test)
1493 struct clk_dummy_context *ctx = test->priv;
1494 struct clk_hw *hw = &ctx->hw;
1495 struct clk *clk = clk_hw_get_clk(hw, NULL);
1497 KUNIT_EXPECT_LT(test,
1498 clk_set_rate_range(clk,
1499 DUMMY_CLOCK_RATE_1 + 1000,
1500 DUMMY_CLOCK_RATE_1),
1507 * Test that users can't set multiple, disjoints, range that would be
1508 * impossible to meet.
1510 static void clk_range_test_multiple_disjoints_range(struct kunit *test)
1512 struct clk_dummy_context *ctx = test->priv;
1513 struct clk_hw *hw = &ctx->hw;
1514 struct clk *user1, *user2;
1516 user1 = clk_hw_get_clk(hw, NULL);
1517 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1519 user2 = clk_hw_get_clk(hw, NULL);
1520 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1522 KUNIT_ASSERT_EQ(test,
1523 clk_set_rate_range(user1, 1000, 2000),
1526 KUNIT_EXPECT_LT(test,
1527 clk_set_rate_range(user2, 3000, 4000),
1535 * Test that if our clock has some boundaries and we try to round a rate
1536 * lower than the minimum, the returned rate will be within range.
1538 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
1540 struct clk_dummy_context *ctx = test->priv;
1541 struct clk_hw *hw = &ctx->hw;
1542 struct clk *clk = clk_hw_get_clk(hw, NULL);
1545 KUNIT_ASSERT_EQ(test,
1546 clk_set_rate_range(clk,
1548 DUMMY_CLOCK_RATE_2),
1551 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1552 KUNIT_ASSERT_GT(test, rate, 0);
1553 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1554 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1560 * Test that if our clock has some boundaries and we try to set a rate
1561 * higher than the maximum, the new rate will be within range.
1563 static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
1565 struct clk_dummy_context *ctx = test->priv;
1566 struct clk_hw *hw = &ctx->hw;
1567 struct clk *clk = clk_hw_get_clk(hw, NULL);
1570 KUNIT_ASSERT_EQ(test,
1571 clk_set_rate_range(clk,
1573 DUMMY_CLOCK_RATE_2),
1576 KUNIT_ASSERT_EQ(test,
1577 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1580 rate = clk_get_rate(clk);
1581 KUNIT_ASSERT_GT(test, rate, 0);
1582 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1583 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1589 * Test that if our clock has some boundaries and we try to round and
1590 * set a rate lower than the minimum, the rate returned by
1591 * clk_round_rate() will be consistent with the new rate set by
1594 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
1596 struct clk_dummy_context *ctx = test->priv;
1597 struct clk_hw *hw = &ctx->hw;
1598 struct clk *clk = clk_hw_get_clk(hw, NULL);
1601 KUNIT_ASSERT_EQ(test,
1602 clk_set_rate_range(clk,
1604 DUMMY_CLOCK_RATE_2),
1607 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1608 KUNIT_ASSERT_GT(test, rounded, 0);
1610 KUNIT_ASSERT_EQ(test,
1611 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1614 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1620 * Test that if our clock has some boundaries and we try to round a rate
1621 * higher than the maximum, the returned rate will be within range.
1623 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
1625 struct clk_dummy_context *ctx = test->priv;
1626 struct clk_hw *hw = &ctx->hw;
1627 struct clk *clk = clk_hw_get_clk(hw, NULL);
1630 KUNIT_ASSERT_EQ(test,
1631 clk_set_rate_range(clk,
1633 DUMMY_CLOCK_RATE_2),
1636 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1637 KUNIT_ASSERT_GT(test, rate, 0);
1638 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1639 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1645 * Test that if our clock has some boundaries and we try to set a rate
1646 * higher than the maximum, the new rate will be within range.
1648 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
1650 struct clk_dummy_context *ctx = test->priv;
1651 struct clk_hw *hw = &ctx->hw;
1652 struct clk *clk = clk_hw_get_clk(hw, NULL);
1655 KUNIT_ASSERT_EQ(test,
1656 clk_set_rate_range(clk,
1658 DUMMY_CLOCK_RATE_2),
1661 KUNIT_ASSERT_EQ(test,
1662 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1665 rate = clk_get_rate(clk);
1666 KUNIT_ASSERT_GT(test, rate, 0);
1667 KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1668 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1674 * Test that if our clock has some boundaries and we try to round and
1675 * set a rate higher than the maximum, the rate returned by
1676 * clk_round_rate() will be consistent with the new rate set by
1679 static void clk_range_test_set_range_set_round_rate_consistent_higher(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_range(clk,
1689 DUMMY_CLOCK_RATE_2),
1692 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1693 KUNIT_ASSERT_GT(test, rounded, 0);
1695 KUNIT_ASSERT_EQ(test,
1696 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1699 KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1705 * Test that if our clock has a rate lower than the minimum set by a
1706 * call to clk_set_rate_range(), the rate will be raised to match the
1709 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1710 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1712 static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
1714 struct clk_dummy_context *ctx = test->priv;
1715 struct clk_hw *hw = &ctx->hw;
1716 struct clk *clk = clk_hw_get_clk(hw, NULL);
1719 KUNIT_ASSERT_EQ(test,
1720 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1723 KUNIT_ASSERT_EQ(test,
1724 clk_set_rate_range(clk,
1726 DUMMY_CLOCK_RATE_2),
1729 rate = clk_get_rate(clk);
1730 KUNIT_ASSERT_GT(test, rate, 0);
1731 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1737 * Test that if our clock has a rate higher than the maximum set by a
1738 * call to clk_set_rate_range(), the rate will be lowered to match the
1741 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1742 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1744 static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
1746 struct clk_dummy_context *ctx = test->priv;
1747 struct clk_hw *hw = &ctx->hw;
1748 struct clk *clk = clk_hw_get_clk(hw, NULL);
1751 KUNIT_ASSERT_EQ(test,
1752 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1755 KUNIT_ASSERT_EQ(test,
1756 clk_set_rate_range(clk,
1758 DUMMY_CLOCK_RATE_2),
1761 rate = clk_get_rate(clk);
1762 KUNIT_ASSERT_GT(test, rate, 0);
1763 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1768 static struct kunit_case clk_range_test_cases[] = {
1769 KUNIT_CASE(clk_range_test_set_range),
1770 KUNIT_CASE(clk_range_test_set_range_invalid),
1771 KUNIT_CASE(clk_range_test_multiple_disjoints_range),
1772 KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
1773 KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
1774 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
1775 KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
1776 KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
1777 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
1778 KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
1779 KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
1784 * Test suite for a basic rate clock, without any parent.
1786 * These tests exercise the rate range API: clk_set_rate_range(),
1787 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range().
1789 static struct kunit_suite clk_range_test_suite = {
1790 .name = "clk-range-test",
1791 .init = clk_test_init,
1792 .exit = clk_test_exit,
1793 .test_cases = clk_range_test_cases,
1797 * Test that if we have several subsequent calls to
1798 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1799 * needed each and every time.
1801 * With clk_dummy_maximize_rate_ops, this means that the rate will
1802 * trail along the maximum as it evolves.
1804 static void clk_range_test_set_range_rate_maximized(struct kunit *test)
1806 struct clk_dummy_context *ctx = test->priv;
1807 struct clk_hw *hw = &ctx->hw;
1808 struct clk *clk = clk_hw_get_clk(hw, NULL);
1811 KUNIT_ASSERT_EQ(test,
1812 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1815 KUNIT_ASSERT_EQ(test,
1816 clk_set_rate_range(clk,
1818 DUMMY_CLOCK_RATE_2),
1821 rate = clk_get_rate(clk);
1822 KUNIT_ASSERT_GT(test, rate, 0);
1823 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1825 KUNIT_ASSERT_EQ(test,
1826 clk_set_rate_range(clk,
1828 DUMMY_CLOCK_RATE_2 - 1000),
1831 rate = clk_get_rate(clk);
1832 KUNIT_ASSERT_GT(test, rate, 0);
1833 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1835 KUNIT_ASSERT_EQ(test,
1836 clk_set_rate_range(clk,
1838 DUMMY_CLOCK_RATE_2),
1841 rate = clk_get_rate(clk);
1842 KUNIT_ASSERT_GT(test, rate, 0);
1843 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1849 * Test that if we have several subsequent calls to
1850 * clk_set_rate_range(), across multiple users, the core will reevaluate
1851 * whether a new rate is needed each and every time.
1853 * With clk_dummy_maximize_rate_ops, this means that the rate will
1854 * trail along the maximum as it evolves.
1856 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
1858 struct clk_dummy_context *ctx = test->priv;
1859 struct clk_hw *hw = &ctx->hw;
1860 struct clk *clk = clk_hw_get_clk(hw, NULL);
1861 struct clk *user1, *user2;
1864 user1 = clk_hw_get_clk(hw, NULL);
1865 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1867 user2 = clk_hw_get_clk(hw, NULL);
1868 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1870 KUNIT_ASSERT_EQ(test,
1871 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1874 KUNIT_ASSERT_EQ(test,
1875 clk_set_rate_range(user1,
1877 DUMMY_CLOCK_RATE_2),
1880 rate = clk_get_rate(clk);
1881 KUNIT_ASSERT_GT(test, rate, 0);
1882 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1884 KUNIT_ASSERT_EQ(test,
1885 clk_set_rate_range(user2,
1887 DUMMY_CLOCK_RATE_1),
1890 rate = clk_get_rate(clk);
1891 KUNIT_ASSERT_GT(test, rate, 0);
1892 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1894 KUNIT_ASSERT_EQ(test,
1895 clk_drop_range(user2),
1898 rate = clk_get_rate(clk);
1899 KUNIT_ASSERT_GT(test, rate, 0);
1900 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1908 * Test that if we have several subsequent calls to
1909 * clk_set_rate_range(), across multiple users, the core will reevaluate
1910 * whether a new rate is needed, including when a user drop its clock.
1912 * With clk_dummy_maximize_rate_ops, this means that the rate will
1913 * trail along the maximum as it evolves.
1915 static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
1917 struct clk_dummy_context *ctx = test->priv;
1918 struct clk_hw *hw = &ctx->hw;
1919 struct clk *clk = clk_hw_get_clk(hw, NULL);
1920 struct clk *user1, *user2;
1923 user1 = clk_hw_get_clk(hw, NULL);
1924 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1926 user2 = clk_hw_get_clk(hw, NULL);
1927 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1929 KUNIT_ASSERT_EQ(test,
1930 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1933 KUNIT_ASSERT_EQ(test,
1934 clk_set_rate_range(user1,
1936 DUMMY_CLOCK_RATE_2),
1939 rate = clk_get_rate(clk);
1940 KUNIT_ASSERT_GT(test, rate, 0);
1941 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1943 KUNIT_ASSERT_EQ(test,
1944 clk_set_rate_range(user2,
1946 DUMMY_CLOCK_RATE_1),
1949 rate = clk_get_rate(clk);
1950 KUNIT_ASSERT_GT(test, rate, 0);
1951 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1955 rate = clk_get_rate(clk);
1956 KUNIT_ASSERT_GT(test, rate, 0);
1957 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1963 static struct kunit_case clk_range_maximize_test_cases[] = {
1964 KUNIT_CASE(clk_range_test_set_range_rate_maximized),
1965 KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
1966 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
1971 * Test suite for a basic rate clock, without any parent.
1973 * These tests exercise the rate range API: clk_set_rate_range(),
1974 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
1975 * driver that will always try to run at the highest possible rate.
1977 static struct kunit_suite clk_range_maximize_test_suite = {
1978 .name = "clk-range-maximize-test",
1979 .init = clk_maximize_test_init,
1980 .exit = clk_test_exit,
1981 .test_cases = clk_range_maximize_test_cases,
1985 * Test that if we have several subsequent calls to
1986 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1987 * needed each and every time.
1989 * With clk_dummy_minimize_rate_ops, this means that the rate will
1990 * trail along the minimum as it evolves.
1992 static void clk_range_test_set_range_rate_minimized(struct kunit *test)
1994 struct clk_dummy_context *ctx = test->priv;
1995 struct clk_hw *hw = &ctx->hw;
1996 struct clk *clk = clk_hw_get_clk(hw, NULL);
1999 KUNIT_ASSERT_EQ(test,
2000 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
2003 KUNIT_ASSERT_EQ(test,
2004 clk_set_rate_range(clk,
2006 DUMMY_CLOCK_RATE_2),
2009 rate = clk_get_rate(clk);
2010 KUNIT_ASSERT_GT(test, rate, 0);
2011 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2013 KUNIT_ASSERT_EQ(test,
2014 clk_set_rate_range(clk,
2015 DUMMY_CLOCK_RATE_1 + 1000,
2016 DUMMY_CLOCK_RATE_2),
2019 rate = clk_get_rate(clk);
2020 KUNIT_ASSERT_GT(test, rate, 0);
2021 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
2023 KUNIT_ASSERT_EQ(test,
2024 clk_set_rate_range(clk,
2026 DUMMY_CLOCK_RATE_2),
2029 rate = clk_get_rate(clk);
2030 KUNIT_ASSERT_GT(test, rate, 0);
2031 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2037 * Test that if we have several subsequent calls to
2038 * clk_set_rate_range(), across multiple users, the core will reevaluate
2039 * whether a new rate is needed each and every time.
2041 * With clk_dummy_minimize_rate_ops, this means that the rate will
2042 * trail along the minimum as it evolves.
2044 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
2046 struct clk_dummy_context *ctx = test->priv;
2047 struct clk_hw *hw = &ctx->hw;
2048 struct clk *clk = clk_hw_get_clk(hw, NULL);
2049 struct clk *user1, *user2;
2052 user1 = clk_hw_get_clk(hw, NULL);
2053 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2055 user2 = clk_hw_get_clk(hw, NULL);
2056 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2058 KUNIT_ASSERT_EQ(test,
2059 clk_set_rate_range(user1,
2064 rate = clk_get_rate(clk);
2065 KUNIT_ASSERT_GT(test, rate, 0);
2066 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2068 KUNIT_ASSERT_EQ(test,
2069 clk_set_rate_range(user2,
2074 rate = clk_get_rate(clk);
2075 KUNIT_ASSERT_GT(test, rate, 0);
2076 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2078 KUNIT_ASSERT_EQ(test,
2079 clk_drop_range(user2),
2082 rate = clk_get_rate(clk);
2083 KUNIT_ASSERT_GT(test, rate, 0);
2084 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2092 * Test that if we have several subsequent calls to
2093 * clk_set_rate_range(), across multiple users, the core will reevaluate
2094 * whether a new rate is needed, including when a user drop its clock.
2096 * With clk_dummy_minimize_rate_ops, this means that the rate will
2097 * trail along the minimum as it evolves.
2099 static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
2101 struct clk_dummy_context *ctx = test->priv;
2102 struct clk_hw *hw = &ctx->hw;
2103 struct clk *clk = clk_hw_get_clk(hw, NULL);
2104 struct clk *user1, *user2;
2107 user1 = clk_hw_get_clk(hw, NULL);
2108 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2110 user2 = clk_hw_get_clk(hw, NULL);
2111 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2113 KUNIT_ASSERT_EQ(test,
2114 clk_set_rate_range(user1,
2119 rate = clk_get_rate(clk);
2120 KUNIT_ASSERT_GT(test, rate, 0);
2121 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2123 KUNIT_ASSERT_EQ(test,
2124 clk_set_rate_range(user2,
2129 rate = clk_get_rate(clk);
2130 KUNIT_ASSERT_GT(test, rate, 0);
2131 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2135 rate = clk_get_rate(clk);
2136 KUNIT_ASSERT_GT(test, rate, 0);
2137 KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2143 static struct kunit_case clk_range_minimize_test_cases[] = {
2144 KUNIT_CASE(clk_range_test_set_range_rate_minimized),
2145 KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
2146 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
2151 * Test suite for a basic rate clock, without any parent.
2153 * These tests exercise the rate range API: clk_set_rate_range(),
2154 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
2155 * driver that will always try to run at the lowest possible rate.
2157 static struct kunit_suite clk_range_minimize_test_suite = {
2158 .name = "clk-range-minimize-test",
2159 .init = clk_minimize_test_init,
2160 .exit = clk_test_exit,
2161 .test_cases = clk_range_minimize_test_cases,
2164 struct clk_leaf_mux_ctx {
2165 struct clk_multiple_parent_ctx mux_ctx;
2167 struct clk_hw parent;
2168 struct clk_rate_request *req;
2169 int (*determine_rate_func)(struct clk_hw *hw, struct clk_rate_request *req);
2172 static int clk_leaf_mux_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
2174 struct clk_leaf_mux_ctx *ctx = container_of(hw, struct clk_leaf_mux_ctx, hw);
2176 struct clk_rate_request *parent_req = ctx->req;
2178 clk_hw_forward_rate_request(hw, req, req->best_parent_hw, parent_req, req->rate);
2179 ret = ctx->determine_rate_func(req->best_parent_hw, parent_req);
2183 req->rate = parent_req->rate;
2188 static const struct clk_ops clk_leaf_mux_set_rate_parent_ops = {
2189 .determine_rate = clk_leaf_mux_determine_rate,
2190 .set_parent = clk_dummy_single_set_parent,
2191 .get_parent = clk_dummy_single_get_parent,
2195 clk_leaf_mux_set_rate_parent_test_init(struct kunit *test)
2197 struct clk_leaf_mux_ctx *ctx;
2198 const char *top_parents[2] = { "parent-0", "parent-1" };
2201 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2206 ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2207 &clk_dummy_rate_ops,
2209 ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2210 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2214 ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2215 &clk_dummy_rate_ops,
2217 ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2218 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2222 ctx->mux_ctx.current_parent = 0;
2223 ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2224 &clk_multiple_parents_mux_ops,
2226 ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2230 ctx->parent.init = CLK_HW_INIT_HW("test-parent", &ctx->mux_ctx.hw,
2231 &empty_clk_ops, CLK_SET_RATE_PARENT);
2232 ret = clk_hw_register(NULL, &ctx->parent);
2236 ctx->hw.init = CLK_HW_INIT_HW("test-clock", &ctx->parent,
2237 &clk_leaf_mux_set_rate_parent_ops,
2238 CLK_SET_RATE_PARENT);
2239 ret = clk_hw_register(NULL, &ctx->hw);
2246 static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test)
2248 struct clk_leaf_mux_ctx *ctx = test->priv;
2250 clk_hw_unregister(&ctx->hw);
2251 clk_hw_unregister(&ctx->parent);
2252 clk_hw_unregister(&ctx->mux_ctx.hw);
2253 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2254 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2257 struct clk_leaf_mux_set_rate_parent_determine_rate_test_case {
2259 int (*determine_rate_func)(struct clk_hw *hw, struct clk_rate_request *req);
2263 clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc(
2264 const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *t, char *desc)
2266 strcpy(desc, t->desc);
2269 static const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case
2270 clk_leaf_mux_set_rate_parent_determine_rate_test_cases[] = {
2273 * Test that __clk_determine_rate() on the parent that can't
2274 * change rate doesn't return a clk_rate_request structure with
2275 * the best_parent_hw pointer pointing to the parent.
2277 .desc = "clk_leaf_mux_set_rate_parent__clk_determine_rate_proper_parent",
2278 .determine_rate_func = __clk_determine_rate,
2282 * Test that __clk_mux_determine_rate() on the parent that
2283 * can't change rate doesn't return a clk_rate_request
2284 * structure with the best_parent_hw pointer pointing to
2287 .desc = "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_proper_parent",
2288 .determine_rate_func = __clk_mux_determine_rate,
2292 * Test that __clk_mux_determine_rate_closest() on the parent
2293 * that can't change rate doesn't return a clk_rate_request
2294 * structure with the best_parent_hw pointer pointing to
2297 .desc = "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_closest_proper_parent",
2298 .determine_rate_func = __clk_mux_determine_rate_closest,
2302 * Test that clk_hw_determine_rate_no_reparent() on the parent
2303 * that can't change rate doesn't return a clk_rate_request
2304 * structure with the best_parent_hw pointer pointing to
2307 .desc = "clk_leaf_mux_set_rate_parent_clk_hw_determine_rate_no_reparent_proper_parent",
2308 .determine_rate_func = clk_hw_determine_rate_no_reparent,
2312 KUNIT_ARRAY_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,
2313 clk_leaf_mux_set_rate_parent_determine_rate_test_cases,
2314 clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc)
2317 * Test that when a clk that can't change rate itself calls a function like
2318 * __clk_determine_rate() on its parent it doesn't get back a clk_rate_request
2319 * structure that has the best_parent_hw pointer point to the clk_hw passed
2320 * into the determine rate function. See commit 262ca38f4b6e ("clk: Stop
2321 * forwarding clk_rate_requests to the parent") for more background.
2323 static void clk_leaf_mux_set_rate_parent_determine_rate_test(struct kunit *test)
2325 struct clk_leaf_mux_ctx *ctx = test->priv;
2326 struct clk_hw *hw = &ctx->hw;
2327 struct clk *clk = clk_hw_get_clk(hw, NULL);
2328 struct clk_rate_request req;
2330 const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *test_param;
2332 test_param = test->param_value;
2333 ctx->determine_rate_func = test_param->determine_rate_func;
2336 rate = clk_get_rate(clk);
2337 KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2338 KUNIT_ASSERT_EQ(test, DUMMY_CLOCK_RATE_2, clk_round_rate(clk, DUMMY_CLOCK_RATE_2));
2340 KUNIT_EXPECT_EQ(test, req.rate, DUMMY_CLOCK_RATE_2);
2341 KUNIT_EXPECT_EQ(test, req.best_parent_rate, DUMMY_CLOCK_RATE_2);
2342 KUNIT_EXPECT_PTR_EQ(test, req.best_parent_hw, &ctx->mux_ctx.hw);
2347 static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = {
2348 KUNIT_CASE_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,
2349 clk_leaf_mux_set_rate_parent_determine_rate_test_gen_params),
2354 * Test suite for a clock whose parent is a pass-through clk whose parent is a
2355 * mux with multiple parents. The leaf and pass-through clocks have the
2356 * CLK_SET_RATE_PARENT flag, and will forward rate requests to the mux, which
2357 * will then select which parent is the best fit for a given rate.
2359 * These tests exercise the behaviour of muxes, and the proper selection
2362 static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = {
2363 .name = "clk-leaf-mux-set-rate-parent",
2364 .init = clk_leaf_mux_set_rate_parent_test_init,
2365 .exit = clk_leaf_mux_set_rate_parent_test_exit,
2366 .test_cases = clk_leaf_mux_set_rate_parent_test_cases,
2369 struct clk_mux_notifier_rate_change {
2371 unsigned long old_rate;
2372 unsigned long new_rate;
2373 wait_queue_head_t wq;
2376 struct clk_mux_notifier_ctx {
2377 struct clk_multiple_parent_ctx mux_ctx;
2379 struct notifier_block clk_nb;
2380 struct clk_mux_notifier_rate_change pre_rate_change;
2381 struct clk_mux_notifier_rate_change post_rate_change;
2384 #define NOTIFIER_TIMEOUT_MS 100
2386 static int clk_mux_notifier_callback(struct notifier_block *nb,
2387 unsigned long action, void *data)
2389 struct clk_notifier_data *clk_data = data;
2390 struct clk_mux_notifier_ctx *ctx = container_of(nb,
2391 struct clk_mux_notifier_ctx,
2394 if (action & PRE_RATE_CHANGE) {
2395 ctx->pre_rate_change.old_rate = clk_data->old_rate;
2396 ctx->pre_rate_change.new_rate = clk_data->new_rate;
2397 ctx->pre_rate_change.done = true;
2398 wake_up_interruptible(&ctx->pre_rate_change.wq);
2401 if (action & POST_RATE_CHANGE) {
2402 ctx->post_rate_change.old_rate = clk_data->old_rate;
2403 ctx->post_rate_change.new_rate = clk_data->new_rate;
2404 ctx->post_rate_change.done = true;
2405 wake_up_interruptible(&ctx->post_rate_change.wq);
2411 static int clk_mux_notifier_test_init(struct kunit *test)
2413 struct clk_mux_notifier_ctx *ctx;
2414 const char *top_parents[2] = { "parent-0", "parent-1" };
2417 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2421 ctx->clk_nb.notifier_call = clk_mux_notifier_callback;
2422 init_waitqueue_head(&ctx->pre_rate_change.wq);
2423 init_waitqueue_head(&ctx->post_rate_change.wq);
2425 ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2426 &clk_dummy_rate_ops,
2428 ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2429 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2433 ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2434 &clk_dummy_rate_ops,
2436 ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2437 ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2441 ctx->mux_ctx.current_parent = 0;
2442 ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2443 &clk_multiple_parents_mux_ops,
2445 ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2449 ctx->clk = clk_hw_get_clk(&ctx->mux_ctx.hw, NULL);
2450 ret = clk_notifier_register(ctx->clk, &ctx->clk_nb);
2457 static void clk_mux_notifier_test_exit(struct kunit *test)
2459 struct clk_mux_notifier_ctx *ctx = test->priv;
2460 struct clk *clk = ctx->clk;
2462 clk_notifier_unregister(clk, &ctx->clk_nb);
2465 clk_hw_unregister(&ctx->mux_ctx.hw);
2466 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2467 clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2471 * Test that if the we have a notifier registered on a mux, the core
2472 * will notify us when we switch to another parent, and with the proper
2473 * old and new rates.
2475 static void clk_mux_notifier_set_parent_test(struct kunit *test)
2477 struct clk_mux_notifier_ctx *ctx = test->priv;
2478 struct clk_hw *hw = &ctx->mux_ctx.hw;
2479 struct clk *clk = clk_hw_get_clk(hw, NULL);
2480 struct clk *new_parent = clk_hw_get_clk(&ctx->mux_ctx.parents_ctx[1].hw, NULL);
2483 ret = clk_set_parent(clk, new_parent);
2484 KUNIT_ASSERT_EQ(test, ret, 0);
2486 ret = wait_event_interruptible_timeout(ctx->pre_rate_change.wq,
2487 ctx->pre_rate_change.done,
2488 msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
2489 KUNIT_ASSERT_GT(test, ret, 0);
2491 KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
2492 KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
2494 ret = wait_event_interruptible_timeout(ctx->post_rate_change.wq,
2495 ctx->post_rate_change.done,
2496 msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
2497 KUNIT_ASSERT_GT(test, ret, 0);
2499 KUNIT_EXPECT_EQ(test, ctx->post_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
2500 KUNIT_EXPECT_EQ(test, ctx->post_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
2502 clk_put(new_parent);
2506 static struct kunit_case clk_mux_notifier_test_cases[] = {
2507 KUNIT_CASE(clk_mux_notifier_set_parent_test),
2512 * Test suite for a mux with multiple parents, and a notifier registered
2515 * These tests exercise the behaviour of notifiers.
2517 static struct kunit_suite clk_mux_notifier_test_suite = {
2518 .name = "clk-mux-notifier",
2519 .init = clk_mux_notifier_test_init,
2520 .exit = clk_mux_notifier_test_exit,
2521 .test_cases = clk_mux_notifier_test_cases,
2525 clk_mux_no_reparent_test_init(struct kunit *test)
2527 struct clk_multiple_parent_ctx *ctx;
2528 const char *parents[2] = { "parent-0", "parent-1"};
2531 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2536 ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2537 &clk_dummy_rate_ops,
2539 ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2540 ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
2544 ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2545 &clk_dummy_rate_ops,
2547 ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2548 ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
2552 ctx->current_parent = 0;
2553 ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
2554 &clk_multiple_parents_no_reparent_mux_ops,
2556 ret = clk_hw_register(NULL, &ctx->hw);
2564 clk_mux_no_reparent_test_exit(struct kunit *test)
2566 struct clk_multiple_parent_ctx *ctx = test->priv;
2568 clk_hw_unregister(&ctx->hw);
2569 clk_hw_unregister(&ctx->parents_ctx[0].hw);
2570 clk_hw_unregister(&ctx->parents_ctx[1].hw);
2574 * Test that if the we have a mux that cannot change parent and we call
2575 * clk_round_rate() on it with a rate that should cause it to change
2578 static void clk_mux_no_reparent_round_rate(struct kunit *test)
2580 struct clk_multiple_parent_ctx *ctx = test->priv;
2581 struct clk_hw *hw = &ctx->hw;
2582 struct clk *clk = clk_hw_get_clk(hw, NULL);
2583 struct clk *other_parent, *parent;
2584 unsigned long other_parent_rate;
2585 unsigned long parent_rate;
2588 parent = clk_get_parent(clk);
2589 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
2591 parent_rate = clk_get_rate(parent);
2592 KUNIT_ASSERT_GT(test, parent_rate, 0);
2594 other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
2595 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
2596 KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
2598 other_parent_rate = clk_get_rate(other_parent);
2599 KUNIT_ASSERT_GT(test, other_parent_rate, 0);
2600 clk_put(other_parent);
2602 rounded_rate = clk_round_rate(clk, other_parent_rate);
2603 KUNIT_ASSERT_GT(test, rounded_rate, 0);
2604 KUNIT_EXPECT_EQ(test, rounded_rate, parent_rate);
2610 * Test that if the we have a mux that cannot change parent and we call
2611 * clk_set_rate() on it with a rate that should cause it to change
2614 static void clk_mux_no_reparent_set_rate(struct kunit *test)
2616 struct clk_multiple_parent_ctx *ctx = test->priv;
2617 struct clk_hw *hw = &ctx->hw;
2618 struct clk *clk = clk_hw_get_clk(hw, NULL);
2619 struct clk *other_parent, *parent;
2620 unsigned long other_parent_rate;
2621 unsigned long parent_rate;
2625 parent = clk_get_parent(clk);
2626 KUNIT_ASSERT_PTR_NE(test, parent, NULL);
2628 parent_rate = clk_get_rate(parent);
2629 KUNIT_ASSERT_GT(test, parent_rate, 0);
2631 other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
2632 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
2633 KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
2635 other_parent_rate = clk_get_rate(other_parent);
2636 KUNIT_ASSERT_GT(test, other_parent_rate, 0);
2637 clk_put(other_parent);
2639 ret = clk_set_rate(clk, other_parent_rate);
2640 KUNIT_ASSERT_EQ(test, ret, 0);
2642 rate = clk_get_rate(clk);
2643 KUNIT_ASSERT_GT(test, rate, 0);
2644 KUNIT_EXPECT_EQ(test, rate, parent_rate);
2649 static struct kunit_case clk_mux_no_reparent_test_cases[] = {
2650 KUNIT_CASE(clk_mux_no_reparent_round_rate),
2651 KUNIT_CASE(clk_mux_no_reparent_set_rate),
2656 * Test suite for a clock mux that isn't allowed to change parent, using
2657 * the clk_hw_determine_rate_no_reparent() helper.
2659 * These tests exercise that helper, and the proper selection of
2660 * rates and parents.
2662 static struct kunit_suite clk_mux_no_reparent_test_suite = {
2663 .name = "clk-mux-no-reparent",
2664 .init = clk_mux_no_reparent_test_init,
2665 .exit = clk_mux_no_reparent_test_exit,
2666 .test_cases = clk_mux_no_reparent_test_cases,
2669 struct clk_register_clk_parent_data_test_case {
2671 struct clk_parent_data pdata;
2675 clk_register_clk_parent_data_test_case_to_desc(
2676 const struct clk_register_clk_parent_data_test_case *t, char *desc)
2678 strcpy(desc, t->desc);
2681 static const struct clk_register_clk_parent_data_test_case
2682 clk_register_clk_parent_data_of_cases[] = {
2685 * Test that a clk registered with a struct device_node can
2686 * find a parent based on struct clk_parent_data::index.
2688 .desc = "clk_parent_data_of_index_test",
2693 * Test that a clk registered with a struct device_node can
2694 * find a parent based on struct clk_parent_data::fwname.
2696 .desc = "clk_parent_data_of_fwname_test",
2697 .pdata.fw_name = CLK_PARENT_DATA_PARENT1,
2701 * Test that a clk registered with a struct device_node can
2702 * find a parent based on struct clk_parent_data::name.
2704 .desc = "clk_parent_data_of_name_test",
2705 /* The index must be negative to indicate firmware not used */
2707 .pdata.name = CLK_PARENT_DATA_1MHZ_NAME,
2711 * Test that a clk registered with a struct device_node can
2712 * find a parent based on struct
2713 * clk_parent_data::{fw_name,name}.
2715 .desc = "clk_parent_data_of_fwname_name_test",
2716 .pdata.fw_name = CLK_PARENT_DATA_PARENT1,
2717 .pdata.name = "not_matching",
2721 * Test that a clk registered with a struct device_node can
2722 * find a parent based on struct clk_parent_data::{index,name}.
2723 * Index takes priority.
2725 .desc = "clk_parent_data_of_index_name_priority_test",
2727 .pdata.name = "not_matching",
2731 * Test that a clk registered with a struct device_node can
2732 * find a parent based on struct
2733 * clk_parent_data::{index,fwname,name}. The fw_name takes
2734 * priority over index and name.
2736 .desc = "clk_parent_data_of_index_fwname_name_priority_test",
2738 .pdata.fw_name = CLK_PARENT_DATA_PARENT1,
2739 .pdata.name = "not_matching",
2743 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_of_test, clk_register_clk_parent_data_of_cases,
2744 clk_register_clk_parent_data_test_case_to_desc)
2747 * struct clk_register_clk_parent_data_of_ctx - Context for clk_parent_data OF tests
2748 * @np: device node of clk under test
2749 * @hw: clk_hw for clk under test
2751 struct clk_register_clk_parent_data_of_ctx {
2752 struct device_node *np;
2756 static int clk_register_clk_parent_data_of_test_init(struct kunit *test)
2758 struct clk_register_clk_parent_data_of_ctx *ctx;
2760 KUNIT_ASSERT_EQ(test, 0,
2761 of_overlay_apply_kunit(test, kunit_clk_parent_data_test));
2763 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2768 ctx->np = of_find_compatible_node(NULL, NULL, "test,clk-parent-data");
2772 of_node_put_kunit(test, ctx->np);
2778 * Test that a clk registered with a struct device_node can find a parent based on
2779 * struct clk_parent_data when the hw member isn't set.
2781 static void clk_register_clk_parent_data_of_test(struct kunit *test)
2783 struct clk_register_clk_parent_data_of_ctx *ctx = test->priv;
2784 struct clk_hw *parent_hw;
2785 const struct clk_register_clk_parent_data_test_case *test_param;
2786 struct clk_init_data init = { };
2787 struct clk *expected_parent, *actual_parent;
2789 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->np);
2791 expected_parent = of_clk_get_kunit(test, ctx->np, 0);
2792 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
2794 test_param = test->param_value;
2795 init.parent_data = &test_param->pdata;
2796 init.num_parents = 1;
2797 init.name = "parent_data_of_test_clk";
2798 init.ops = &clk_dummy_single_parent_ops;
2799 ctx->hw.init = &init;
2800 KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, ctx->np, &ctx->hw));
2802 parent_hw = clk_hw_get_parent(&ctx->hw);
2803 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
2805 actual_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
2806 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, actual_parent);
2808 KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
2811 static struct kunit_case clk_register_clk_parent_data_of_test_cases[] = {
2812 KUNIT_CASE_PARAM(clk_register_clk_parent_data_of_test,
2813 clk_register_clk_parent_data_of_test_gen_params),
2818 * Test suite for registering clks with struct clk_parent_data and a struct
2821 static struct kunit_suite clk_register_clk_parent_data_of_suite = {
2822 .name = "clk_register_clk_parent_data_of",
2823 .init = clk_register_clk_parent_data_of_test_init,
2824 .test_cases = clk_register_clk_parent_data_of_test_cases,
2828 * struct clk_register_clk_parent_data_device_ctx - Context for clk_parent_data device tests
2829 * @dev: device of clk under test
2830 * @hw: clk_hw for clk under test
2831 * @pdrv: driver to attach to find @dev
2833 struct clk_register_clk_parent_data_device_ctx {
2836 struct platform_driver pdrv;
2839 static inline struct clk_register_clk_parent_data_device_ctx *
2840 clk_register_clk_parent_data_driver_to_test_context(struct platform_device *pdev)
2842 return container_of(to_platform_driver(pdev->dev.driver),
2843 struct clk_register_clk_parent_data_device_ctx, pdrv);
2846 static int clk_register_clk_parent_data_device_probe(struct platform_device *pdev)
2848 struct clk_register_clk_parent_data_device_ctx *ctx;
2850 ctx = clk_register_clk_parent_data_driver_to_test_context(pdev);
2851 ctx->dev = &pdev->dev;
2856 static void clk_register_clk_parent_data_device_driver(struct kunit *test)
2858 struct clk_register_clk_parent_data_device_ctx *ctx = test->priv;
2859 static const struct of_device_id match_table[] = {
2860 { .compatible = "test,clk-parent-data" },
2864 ctx->pdrv.probe = clk_register_clk_parent_data_device_probe;
2865 ctx->pdrv.driver.of_match_table = match_table;
2866 ctx->pdrv.driver.name = __func__;
2867 ctx->pdrv.driver.owner = THIS_MODULE;
2869 KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
2870 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->dev);
2873 static const struct clk_register_clk_parent_data_test_case
2874 clk_register_clk_parent_data_device_cases[] = {
2877 * Test that a clk registered with a struct device can find a
2878 * parent based on struct clk_parent_data::index.
2880 .desc = "clk_parent_data_device_index_test",
2885 * Test that a clk registered with a struct device can find a
2886 * parent based on struct clk_parent_data::fwname.
2888 .desc = "clk_parent_data_device_fwname_test",
2889 .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
2893 * Test that a clk registered with a struct device can find a
2894 * parent based on struct clk_parent_data::name.
2896 .desc = "clk_parent_data_device_name_test",
2897 /* The index must be negative to indicate firmware not used */
2899 .pdata.name = CLK_PARENT_DATA_50MHZ_NAME,
2903 * Test that a clk registered with a struct device can find a
2904 * parent based on struct clk_parent_data::{fw_name,name}.
2906 .desc = "clk_parent_data_device_fwname_name_test",
2907 .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
2908 .pdata.name = "not_matching",
2912 * Test that a clk registered with a struct device can find a
2913 * parent based on struct clk_parent_data::{index,name}. Index
2916 .desc = "clk_parent_data_device_index_name_priority_test",
2918 .pdata.name = "not_matching",
2922 * Test that a clk registered with a struct device can find a
2923 * parent based on struct clk_parent_data::{index,fwname,name}.
2924 * The fw_name takes priority over index and name.
2926 .desc = "clk_parent_data_device_index_fwname_name_priority_test",
2928 .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
2929 .pdata.name = "not_matching",
2933 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_test,
2934 clk_register_clk_parent_data_device_cases,
2935 clk_register_clk_parent_data_test_case_to_desc)
2938 * Test that a clk registered with a struct device can find a parent based on
2939 * struct clk_parent_data when the hw member isn't set.
2941 static void clk_register_clk_parent_data_device_test(struct kunit *test)
2943 struct clk_register_clk_parent_data_device_ctx *ctx;
2944 const struct clk_register_clk_parent_data_test_case *test_param;
2945 struct clk_hw *parent_hw;
2946 struct clk_init_data init = { };
2947 struct clk *expected_parent, *actual_parent;
2949 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2950 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
2953 clk_register_clk_parent_data_device_driver(test);
2955 expected_parent = clk_get_kunit(test, ctx->dev, "50");
2956 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
2958 test_param = test->param_value;
2959 init.parent_data = &test_param->pdata;
2960 init.num_parents = 1;
2961 init.name = "parent_data_device_test_clk";
2962 init.ops = &clk_dummy_single_parent_ops;
2963 ctx->hw.init = &init;
2964 KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, ctx->dev, &ctx->hw));
2966 parent_hw = clk_hw_get_parent(&ctx->hw);
2967 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
2969 actual_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
2970 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, actual_parent);
2972 KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
2975 static const struct clk_register_clk_parent_data_test_case
2976 clk_register_clk_parent_data_device_hw_cases[] = {
2979 * Test that a clk registered with a struct device can find a
2980 * parent based on struct clk_parent_data::hw.
2982 .desc = "clk_parent_data_device_hw_index_test",
2983 /* The index must be negative to indicate firmware not used */
2988 * Test that a clk registered with a struct device can find a
2989 * parent based on struct clk_parent_data::hw when
2990 * struct clk_parent_data::fw_name is set.
2992 .desc = "clk_parent_data_device_hw_fwname_test",
2993 .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
2997 * Test that a clk registered with a struct device can find a
2998 * parent based on struct clk_parent_data::hw when struct
2999 * clk_parent_data::name is set.
3001 .desc = "clk_parent_data_device_hw_name_test",
3002 /* The index must be negative to indicate firmware not used */
3004 .pdata.name = CLK_PARENT_DATA_50MHZ_NAME,
3008 * Test that a clk registered with a struct device can find a
3009 * parent based on struct clk_parent_data::hw when struct
3010 * clk_parent_data::{fw_name,name} are set.
3012 .desc = "clk_parent_data_device_hw_fwname_name_test",
3013 .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
3014 .pdata.name = "not_matching",
3018 * Test that a clk registered with a struct device can find a
3019 * parent based on struct clk_parent_data::hw when struct
3020 * clk_parent_data::index is set. The hw pointer takes
3023 .desc = "clk_parent_data_device_hw_index_priority_test",
3028 * Test that a clk registered with a struct device can find a
3029 * parent based on struct clk_parent_data::hw when
3030 * struct clk_parent_data::{index,fwname,name} are set.
3031 * The hw pointer takes priority over everything else.
3033 .desc = "clk_parent_data_device_hw_index_fwname_name_priority_test",
3035 .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
3036 .pdata.name = "not_matching",
3040 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_hw_test,
3041 clk_register_clk_parent_data_device_hw_cases,
3042 clk_register_clk_parent_data_test_case_to_desc)
3045 * Test that a clk registered with a struct device can find a
3046 * parent based on struct clk_parent_data::hw.
3048 static void clk_register_clk_parent_data_device_hw_test(struct kunit *test)
3050 struct clk_register_clk_parent_data_device_ctx *ctx;
3051 const struct clk_register_clk_parent_data_test_case *test_param;
3052 struct clk_dummy_context *parent;
3053 struct clk_hw *parent_hw;
3054 struct clk_parent_data pdata = { };
3055 struct clk_init_data init = { };
3057 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
3058 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
3061 clk_register_clk_parent_data_device_driver(test);
3063 parent = kunit_kzalloc(test, sizeof(*parent), GFP_KERNEL);
3064 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
3066 parent_hw = &parent->hw;
3067 parent_hw->init = CLK_HW_INIT_NO_PARENT("parent-clk",
3068 &clk_dummy_rate_ops, 0);
3070 KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, ctx->dev, parent_hw));
3072 test_param = test->param_value;
3073 memcpy(&pdata, &test_param->pdata, sizeof(pdata));
3074 pdata.hw = parent_hw;
3075 init.parent_data = &pdata;
3076 init.num_parents = 1;
3077 init.ops = &clk_dummy_single_parent_ops;
3078 init.name = "parent_data_device_hw_test_clk";
3079 ctx->hw.init = &init;
3080 KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, ctx->dev, &ctx->hw));
3082 KUNIT_EXPECT_PTR_EQ(test, parent_hw, clk_hw_get_parent(&ctx->hw));
3085 static struct kunit_case clk_register_clk_parent_data_device_test_cases[] = {
3086 KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_test,
3087 clk_register_clk_parent_data_device_test_gen_params),
3088 KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_hw_test,
3089 clk_register_clk_parent_data_device_hw_test_gen_params),
3093 static int clk_register_clk_parent_data_device_init(struct kunit *test)
3095 KUNIT_ASSERT_EQ(test, 0,
3096 of_overlay_apply_kunit(test, kunit_clk_parent_data_test));
3102 * Test suite for registering clks with struct clk_parent_data and a struct
3105 static struct kunit_suite clk_register_clk_parent_data_device_suite = {
3106 .name = "clk_register_clk_parent_data_device",
3107 .init = clk_register_clk_parent_data_device_init,
3108 .test_cases = clk_register_clk_parent_data_device_test_cases,
3112 &clk_leaf_mux_set_rate_parent_test_suite,
3114 &clk_multiple_parents_mux_test_suite,
3115 &clk_mux_no_reparent_test_suite,
3116 &clk_mux_notifier_test_suite,
3117 &clk_orphan_transparent_multiple_parent_mux_test_suite,
3118 &clk_orphan_transparent_single_parent_test_suite,
3119 &clk_orphan_two_level_root_last_test_suite,
3120 &clk_range_test_suite,
3121 &clk_range_maximize_test_suite,
3122 &clk_range_minimize_test_suite,
3123 &clk_register_clk_parent_data_of_suite,
3124 &clk_register_clk_parent_data_device_suite,
3125 &clk_single_parent_mux_test_suite,
3126 &clk_uncached_test_suite,
3128 MODULE_DESCRIPTION("Kunit tests for clk framework");
3129 MODULE_LICENSE("GPL v2");