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_kunit(test
, 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_kunit(test
, 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_kunit(test
, NULL
, &ctx
->hw
);
500 * Test that for a clock with multiple parents, clk_get_parent()
501 * actually returns the current one.
504 clk_test_multiple_parents_mux_get_parent(struct kunit
*test
)
506 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
507 struct clk_hw
*hw
= &ctx
->hw
;
508 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
509 struct clk
*parent
= clk_hw_get_clk(&ctx
->parents_ctx
[0].hw
, NULL
);
511 KUNIT_EXPECT_TRUE(test
, clk_is_match(clk_get_parent(clk
), parent
));
518 * Test that for a clock with a multiple parents, clk_has_parent()
519 * actually reports all of them as parents.
522 clk_test_multiple_parents_mux_has_parent(struct kunit
*test
)
524 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
525 struct clk_hw
*hw
= &ctx
->hw
;
526 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
529 parent
= clk_hw_get_clk(&ctx
->parents_ctx
[0].hw
, NULL
);
530 KUNIT_EXPECT_TRUE(test
, clk_has_parent(clk
, parent
));
533 parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
534 KUNIT_EXPECT_TRUE(test
, clk_has_parent(clk
, parent
));
541 * Test that for a clock with a multiple parents, if we set a range on
542 * that clock and the parent is changed, its rate after the reparenting
543 * is still within the range we asked for.
545 * FIXME: clk_set_parent() only does the reparenting but doesn't
546 * reevaluate whether the new clock rate is within its boundaries or
550 clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit
*test
)
552 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
553 struct clk_hw
*hw
= &ctx
->hw
;
554 struct clk
*clk
= clk_hw_get_clk_kunit(test
, hw
, NULL
);
555 struct clk
*parent1
, *parent2
;
559 kunit_skip(test
, "This needs to be fixed in the core.");
561 parent1
= clk_hw_get_clk_kunit(test
, &ctx
->parents_ctx
[0].hw
, NULL
);
562 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent1
);
563 KUNIT_ASSERT_TRUE(test
, clk_is_match(clk_get_parent(clk
), parent1
));
565 parent2
= clk_hw_get_clk_kunit(test
, &ctx
->parents_ctx
[1].hw
, NULL
);
566 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent2
);
568 ret
= clk_set_rate(parent1
, DUMMY_CLOCK_RATE_1
);
569 KUNIT_ASSERT_EQ(test
, ret
, 0);
571 ret
= clk_set_rate(parent2
, DUMMY_CLOCK_RATE_2
);
572 KUNIT_ASSERT_EQ(test
, ret
, 0);
574 ret
= clk_set_rate_range(clk
,
575 DUMMY_CLOCK_RATE_1
- 1000,
576 DUMMY_CLOCK_RATE_1
+ 1000);
577 KUNIT_ASSERT_EQ(test
, ret
, 0);
579 ret
= clk_set_parent(clk
, parent2
);
580 KUNIT_ASSERT_EQ(test
, ret
, 0);
582 rate
= clk_get_rate(clk
);
583 KUNIT_ASSERT_GT(test
, rate
, 0);
584 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
- 1000);
585 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_1
+ 1000);
588 static struct kunit_case clk_multiple_parents_mux_test_cases
[] = {
589 KUNIT_CASE(clk_test_multiple_parents_mux_get_parent
),
590 KUNIT_CASE(clk_test_multiple_parents_mux_has_parent
),
591 KUNIT_CASE(clk_test_multiple_parents_mux_set_range_set_parent_get_rate
),
596 * Test suite for a basic mux clock with two parents, with
597 * CLK_SET_RATE_PARENT on the child.
599 * These tests exercise the consumer API and check that the state of the
600 * child and parents are sane and consistent.
602 static struct kunit_suite
603 clk_multiple_parents_mux_test_suite
= {
604 .name
= "clk-multiple-parents-mux-test",
605 .init
= clk_multiple_parents_mux_test_init
,
606 .test_cases
= clk_multiple_parents_mux_test_cases
,
610 clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit
*test
)
612 struct clk_multiple_parent_ctx
*ctx
;
613 const char *parents
[2] = { "missing-parent", "proper-parent"};
616 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
621 ctx
->parents_ctx
[1].hw
.init
= CLK_HW_INIT_NO_PARENT("proper-parent",
624 ctx
->parents_ctx
[1].rate
= DUMMY_CLOCK_INIT_RATE
;
625 ret
= clk_hw_register_kunit(test
, NULL
, &ctx
->parents_ctx
[1].hw
);
629 ctx
->hw
.init
= CLK_HW_INIT_PARENTS("test-orphan-mux", parents
,
630 &clk_multiple_parents_mux_ops
,
631 CLK_SET_RATE_PARENT
);
632 ret
= clk_hw_register_kunit(test
, NULL
, &ctx
->hw
);
640 * Test that, for a mux whose current parent hasn't been registered yet and is
641 * thus orphan, clk_get_parent() will return NULL.
644 clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit
*test
)
646 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
647 struct clk_hw
*hw
= &ctx
->hw
;
648 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
650 KUNIT_EXPECT_PTR_EQ(test
, clk_get_parent(clk
), NULL
);
656 * Test that, for a mux whose current parent hasn't been registered yet,
657 * calling clk_set_parent() to a valid parent will properly update the
658 * mux parent and its orphan status.
661 clk_test_orphan_transparent_multiple_parent_mux_set_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
);
666 struct clk
*parent
, *new_parent
;
669 parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
670 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
672 ret
= clk_set_parent(clk
, parent
);
673 KUNIT_ASSERT_EQ(test
, ret
, 0);
675 new_parent
= clk_get_parent(clk
);
676 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
677 KUNIT_EXPECT_TRUE(test
, clk_is_match(parent
, new_parent
));
684 * Test that, for a mux that started orphan but got switched to a valid
685 * parent, calling clk_drop_range() on the mux won't affect the parent
689 clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit
*test
)
691 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
692 struct clk_hw
*hw
= &ctx
->hw
;
693 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
695 unsigned long parent_rate
, new_parent_rate
;
698 parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
699 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
701 parent_rate
= clk_get_rate(parent
);
702 KUNIT_ASSERT_GT(test
, parent_rate
, 0);
704 ret
= clk_set_parent(clk
, parent
);
705 KUNIT_ASSERT_EQ(test
, ret
, 0);
707 ret
= clk_drop_range(clk
);
708 KUNIT_ASSERT_EQ(test
, ret
, 0);
710 new_parent_rate
= clk_get_rate(clk
);
711 KUNIT_ASSERT_GT(test
, new_parent_rate
, 0);
712 KUNIT_EXPECT_EQ(test
, parent_rate
, new_parent_rate
);
719 * Test that, for a mux that started orphan but got switched to a valid
720 * parent, the rate of the mux and its new parent are consistent.
723 clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit
*test
)
725 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
726 struct clk_hw
*hw
= &ctx
->hw
;
727 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
729 unsigned long parent_rate
, rate
;
732 parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
733 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
735 parent_rate
= clk_get_rate(parent
);
736 KUNIT_ASSERT_GT(test
, parent_rate
, 0);
738 ret
= clk_set_parent(clk
, parent
);
739 KUNIT_ASSERT_EQ(test
, ret
, 0);
741 rate
= clk_get_rate(clk
);
742 KUNIT_ASSERT_GT(test
, rate
, 0);
743 KUNIT_EXPECT_EQ(test
, parent_rate
, rate
);
750 * Test that, for a mux that started orphan but got switched to a valid
751 * parent, calling clk_put() on the mux won't affect the parent rate.
754 clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit
*test
)
756 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
757 struct clk
*clk
, *parent
;
758 unsigned long parent_rate
, new_parent_rate
;
761 parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
762 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
764 clk
= clk_hw_get_clk(&ctx
->hw
, NULL
);
765 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, clk
);
767 parent_rate
= clk_get_rate(parent
);
768 KUNIT_ASSERT_GT(test
, parent_rate
, 0);
770 ret
= clk_set_parent(clk
, parent
);
771 KUNIT_ASSERT_EQ(test
, ret
, 0);
775 new_parent_rate
= clk_get_rate(parent
);
776 KUNIT_ASSERT_GT(test
, new_parent_rate
, 0);
777 KUNIT_EXPECT_EQ(test
, parent_rate
, new_parent_rate
);
783 * Test that, for a mux that started orphan but got switched to a valid
784 * parent, calling clk_set_rate_range() will affect the parent state if
785 * its rate is out of range.
788 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit
*test
)
790 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
791 struct clk_hw
*hw
= &ctx
->hw
;
792 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
797 parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
798 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
800 ret
= clk_set_parent(clk
, parent
);
801 KUNIT_ASSERT_EQ(test
, ret
, 0);
803 ret
= clk_set_rate_range(clk
, DUMMY_CLOCK_RATE_1
, DUMMY_CLOCK_RATE_2
);
804 KUNIT_ASSERT_EQ(test
, ret
, 0);
806 rate
= clk_get_rate(clk
);
807 KUNIT_ASSERT_GT(test
, rate
, 0);
808 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
809 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
816 * Test that, for a mux that started orphan but got switched to a valid
817 * parent, calling clk_set_rate_range() won't affect the parent state if
818 * its rate is within range.
821 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit
*test
)
823 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
824 struct clk_hw
*hw
= &ctx
->hw
;
825 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
827 unsigned long parent_rate
, new_parent_rate
;
830 parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
831 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
833 parent_rate
= clk_get_rate(parent
);
834 KUNIT_ASSERT_GT(test
, parent_rate
, 0);
836 ret
= clk_set_parent(clk
, parent
);
837 KUNIT_ASSERT_EQ(test
, ret
, 0);
839 ret
= clk_set_rate_range(clk
,
840 DUMMY_CLOCK_INIT_RATE
- 1000,
841 DUMMY_CLOCK_INIT_RATE
+ 1000);
842 KUNIT_ASSERT_EQ(test
, ret
, 0);
844 new_parent_rate
= clk_get_rate(parent
);
845 KUNIT_ASSERT_GT(test
, new_parent_rate
, 0);
846 KUNIT_EXPECT_EQ(test
, parent_rate
, new_parent_rate
);
853 * Test that, for a mux whose current parent hasn't been registered yet,
854 * calling clk_set_rate_range() will succeed, and will be taken into
855 * account when rounding a rate.
858 clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit
*test
)
860 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
861 struct clk_hw
*hw
= &ctx
->hw
;
862 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
866 ret
= clk_set_rate_range(clk
, DUMMY_CLOCK_RATE_1
, DUMMY_CLOCK_RATE_2
);
867 KUNIT_ASSERT_EQ(test
, ret
, 0);
869 rate
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000);
870 KUNIT_ASSERT_GT(test
, rate
, 0);
871 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
872 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
878 * Test that, for a mux that started orphan, was assigned and rate and
879 * then got switched to a valid parent, its rate is eventually within
882 * FIXME: Even though we update the rate as part of clk_set_parent(), we
883 * don't evaluate whether that new rate is within range and needs to be
887 clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit
*test
)
889 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
890 struct clk_hw
*hw
= &ctx
->hw
;
891 struct clk
*clk
= clk_hw_get_clk_kunit(test
, hw
, NULL
);
896 kunit_skip(test
, "This needs to be fixed in the core.");
898 clk_hw_set_rate_range(hw
, DUMMY_CLOCK_RATE_1
, DUMMY_CLOCK_RATE_2
);
900 parent
= clk_hw_get_clk_kunit(test
, &ctx
->parents_ctx
[1].hw
, NULL
);
901 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
903 ret
= clk_set_parent(clk
, parent
);
904 KUNIT_ASSERT_EQ(test
, ret
, 0);
906 rate
= clk_get_rate(clk
);
907 KUNIT_ASSERT_GT(test
, rate
, 0);
908 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
909 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
912 static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases
[] = {
913 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_get_parent
),
914 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent
),
915 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range
),
916 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate
),
917 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_put
),
918 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified
),
919 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched
),
920 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate
),
921 KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate
),
926 * Test suite for a basic mux clock with two parents. The default parent
927 * isn't registered, only the second parent is. By default, the clock
928 * will thus be orphan.
930 * These tests exercise the behaviour of the consumer API when dealing
931 * with an orphan clock, and how we deal with the transition to a valid
934 static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite
= {
935 .name
= "clk-orphan-transparent-multiple-parent-mux-test",
936 .init
= clk_orphan_transparent_multiple_parent_mux_test_init
,
937 .test_cases
= clk_orphan_transparent_multiple_parent_mux_test_cases
,
940 struct clk_single_parent_ctx
{
941 struct clk_dummy_context parent_ctx
;
945 static int clk_single_parent_mux_test_init(struct kunit
*test
)
947 struct clk_single_parent_ctx
*ctx
;
950 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
955 ctx
->parent_ctx
.rate
= DUMMY_CLOCK_INIT_RATE
;
956 ctx
->parent_ctx
.hw
.init
=
957 CLK_HW_INIT_NO_PARENT("parent-clk",
961 ret
= clk_hw_register_kunit(test
, NULL
, &ctx
->parent_ctx
.hw
);
965 ctx
->hw
.init
= CLK_HW_INIT("test-clk", "parent-clk",
966 &clk_dummy_single_parent_ops
,
967 CLK_SET_RATE_PARENT
);
969 ret
= clk_hw_register_kunit(test
, NULL
, &ctx
->hw
);
977 clk_single_parent_mux_test_exit(struct kunit
*test
)
979 struct clk_single_parent_ctx
*ctx
= test
->priv
;
981 clk_hw_unregister(&ctx
->hw
);
982 clk_hw_unregister(&ctx
->parent_ctx
.hw
);
986 * Test that for a clock with a single parent, clk_get_parent() actually
987 * returns the parent.
990 clk_test_single_parent_mux_get_parent(struct kunit
*test
)
992 struct clk_single_parent_ctx
*ctx
= test
->priv
;
993 struct clk_hw
*hw
= &ctx
->hw
;
994 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
995 struct clk
*parent
= clk_hw_get_clk(&ctx
->parent_ctx
.hw
, NULL
);
997 KUNIT_EXPECT_TRUE(test
, clk_is_match(clk_get_parent(clk
), parent
));
1004 * Test that for a clock with a single parent, clk_has_parent() actually
1005 * reports it as a parent.
1008 clk_test_single_parent_mux_has_parent(struct kunit
*test
)
1010 struct clk_single_parent_ctx
*ctx
= test
->priv
;
1011 struct clk_hw
*hw
= &ctx
->hw
;
1012 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1013 struct clk
*parent
= clk_hw_get_clk(&ctx
->parent_ctx
.hw
, NULL
);
1015 KUNIT_EXPECT_TRUE(test
, clk_has_parent(clk
, parent
));
1022 * Test that for a clock that can't modify its rate and with a single
1023 * parent, if we set disjoints range on the parent and then the child,
1024 * the second will return an error.
1026 * FIXME: clk_set_rate_range() only considers the current clock when
1027 * evaluating whether ranges are disjoints and not the upstream clocks
1031 clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit
*test
)
1033 struct clk_single_parent_ctx
*ctx
= test
->priv
;
1034 struct clk_hw
*hw
= &ctx
->hw
;
1035 struct clk
*clk
= clk_hw_get_clk_kunit(test
, hw
, NULL
);
1039 kunit_skip(test
, "This needs to be fixed in the core.");
1041 parent
= clk_get_parent(clk
);
1042 KUNIT_ASSERT_PTR_NE(test
, parent
, NULL
);
1044 ret
= clk_set_rate_range(parent
, 1000, 2000);
1045 KUNIT_ASSERT_EQ(test
, ret
, 0);
1047 ret
= clk_set_rate_range(clk
, 3000, 4000);
1048 KUNIT_EXPECT_LT(test
, ret
, 0);
1052 * Test that for a clock that can't modify its rate and with a single
1053 * parent, if we set disjoints range on the child and then the parent,
1054 * the second will return an error.
1056 * FIXME: clk_set_rate_range() only considers the current clock when
1057 * evaluating whether ranges are disjoints and not the downstream clocks
1061 clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit
*test
)
1063 struct clk_single_parent_ctx
*ctx
= test
->priv
;
1064 struct clk_hw
*hw
= &ctx
->hw
;
1065 struct clk
*clk
= clk_hw_get_clk_kunit(test
, hw
, NULL
);
1069 kunit_skip(test
, "This needs to be fixed in the core.");
1071 parent
= clk_get_parent(clk
);
1072 KUNIT_ASSERT_PTR_NE(test
, parent
, NULL
);
1074 ret
= clk_set_rate_range(clk
, 1000, 2000);
1075 KUNIT_ASSERT_EQ(test
, ret
, 0);
1077 ret
= clk_set_rate_range(parent
, 3000, 4000);
1078 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 a range on the parent and then call
1084 * clk_round_rate(), the boundaries of the parent are taken into
1088 clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit
*test
)
1090 struct clk_single_parent_ctx
*ctx
= test
->priv
;
1091 struct clk_hw
*hw
= &ctx
->hw
;
1092 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1097 parent
= clk_get_parent(clk
);
1098 KUNIT_ASSERT_PTR_NE(test
, parent
, NULL
);
1100 ret
= clk_set_rate_range(parent
, DUMMY_CLOCK_RATE_1
, DUMMY_CLOCK_RATE_2
);
1101 KUNIT_ASSERT_EQ(test
, ret
, 0);
1103 rate
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000);
1104 KUNIT_ASSERT_GT(test
, rate
, 0);
1105 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
1106 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
1112 * Test that for a clock that can't modify its rate and with a single
1113 * parent, if we set a range on the parent and a more restrictive one on
1114 * the child, and then call clk_round_rate(), the boundaries of the
1115 * two clocks are taken into account.
1118 clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit
*test
)
1120 struct clk_single_parent_ctx
*ctx
= test
->priv
;
1121 struct clk_hw
*hw
= &ctx
->hw
;
1122 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1127 parent
= clk_get_parent(clk
);
1128 KUNIT_ASSERT_PTR_NE(test
, parent
, NULL
);
1130 ret
= clk_set_rate_range(parent
, DUMMY_CLOCK_RATE_1
, DUMMY_CLOCK_RATE_2
);
1131 KUNIT_ASSERT_EQ(test
, ret
, 0);
1133 ret
= clk_set_rate_range(clk
, DUMMY_CLOCK_RATE_1
+ 1000, DUMMY_CLOCK_RATE_2
- 1000);
1134 KUNIT_ASSERT_EQ(test
, ret
, 0);
1136 rate
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000);
1137 KUNIT_ASSERT_GT(test
, rate
, 0);
1138 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
+ 1000);
1139 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
- 1000);
1141 rate
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000);
1142 KUNIT_ASSERT_GT(test
, rate
, 0);
1143 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
+ 1000);
1144 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
- 1000);
1150 * Test that for a clock that can't modify its rate and with a single
1151 * parent, if we set a range on the child and a more restrictive one on
1152 * the parent, and then call clk_round_rate(), the boundaries of the
1153 * two clocks are taken into account.
1156 clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit
*test
)
1158 struct clk_single_parent_ctx
*ctx
= test
->priv
;
1159 struct clk_hw
*hw
= &ctx
->hw
;
1160 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1165 parent
= clk_get_parent(clk
);
1166 KUNIT_ASSERT_PTR_NE(test
, parent
, NULL
);
1168 ret
= clk_set_rate_range(parent
, DUMMY_CLOCK_RATE_1
+ 1000, DUMMY_CLOCK_RATE_2
- 1000);
1169 KUNIT_ASSERT_EQ(test
, ret
, 0);
1171 ret
= clk_set_rate_range(clk
, DUMMY_CLOCK_RATE_1
, DUMMY_CLOCK_RATE_2
);
1172 KUNIT_ASSERT_EQ(test
, ret
, 0);
1174 rate
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000);
1175 KUNIT_ASSERT_GT(test
, rate
, 0);
1176 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
+ 1000);
1177 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
- 1000);
1179 rate
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000);
1180 KUNIT_ASSERT_GT(test
, rate
, 0);
1181 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
+ 1000);
1182 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
- 1000);
1187 static struct kunit_case clk_single_parent_mux_test_cases
[] = {
1188 KUNIT_CASE(clk_test_single_parent_mux_get_parent
),
1189 KUNIT_CASE(clk_test_single_parent_mux_has_parent
),
1190 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last
),
1191 KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last
),
1192 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller
),
1193 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only
),
1194 KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller
),
1199 * Test suite for a basic mux clock with one parent, with
1200 * CLK_SET_RATE_PARENT on the child.
1202 * These tests exercise the consumer API and check that the state of the
1203 * child and parent are sane and consistent.
1205 static struct kunit_suite
1206 clk_single_parent_mux_test_suite
= {
1207 .name
= "clk-single-parent-mux-test",
1208 .init
= clk_single_parent_mux_test_init
,
1209 .test_cases
= clk_single_parent_mux_test_cases
,
1212 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit
*test
)
1214 struct clk_single_parent_ctx
*ctx
;
1215 struct clk_init_data init
= { };
1216 const char * const parents
[] = { "orphan_parent" };
1219 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
1224 init
.name
= "test_orphan_dummy_parent";
1225 init
.ops
= &clk_dummy_single_parent_ops
;
1226 init
.parent_names
= parents
;
1227 init
.num_parents
= ARRAY_SIZE(parents
);
1228 init
.flags
= CLK_SET_RATE_PARENT
;
1229 ctx
->hw
.init
= &init
;
1231 ret
= clk_hw_register(NULL
, &ctx
->hw
);
1235 memset(&init
, 0, sizeof(init
));
1236 init
.name
= "orphan_parent";
1237 init
.ops
= &clk_dummy_rate_ops
;
1238 ctx
->parent_ctx
.hw
.init
= &init
;
1239 ctx
->parent_ctx
.rate
= DUMMY_CLOCK_INIT_RATE
;
1241 ret
= clk_hw_register(NULL
, &ctx
->parent_ctx
.hw
);
1249 * Test that a mux-only clock, with an initial rate within a range,
1250 * will still have the same rate after the range has been enforced.
1253 * https://lore.kernel.org/linux-clk/7720158d-10a7-a17b-73a4-a8615c9c6d5c@collabora.com/
1255 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit
*test
)
1257 struct clk_single_parent_ctx
*ctx
= test
->priv
;
1258 struct clk_hw
*hw
= &ctx
->hw
;
1259 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1260 unsigned long rate
, new_rate
;
1262 rate
= clk_get_rate(clk
);
1263 KUNIT_ASSERT_GT(test
, rate
, 0);
1265 KUNIT_ASSERT_EQ(test
,
1266 clk_set_rate_range(clk
,
1267 ctx
->parent_ctx
.rate
- 1000,
1268 ctx
->parent_ctx
.rate
+ 1000),
1271 new_rate
= clk_get_rate(clk
);
1272 KUNIT_ASSERT_GT(test
, new_rate
, 0);
1273 KUNIT_EXPECT_EQ(test
, rate
, new_rate
);
1278 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases
[] = {
1279 KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range
),
1284 * Test suite for a basic mux clock with one parent. The parent is
1285 * registered after its child. The clock will thus be an orphan when
1286 * registered, but will no longer be when the tests run.
1288 * These tests make sure a clock that used to be orphan has a sane,
1289 * consistent, behaviour.
1291 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite
= {
1292 .name
= "clk-orphan-transparent-single-parent-test",
1293 .init
= clk_orphan_transparent_single_parent_mux_test_init
,
1294 .exit
= clk_single_parent_mux_test_exit
,
1295 .test_cases
= clk_orphan_transparent_single_parent_mux_test_cases
,
1298 struct clk_single_parent_two_lvl_ctx
{
1299 struct clk_dummy_context parent_parent_ctx
;
1300 struct clk_dummy_context parent_ctx
;
1305 clk_orphan_two_level_root_last_test_init(struct kunit
*test
)
1307 struct clk_single_parent_two_lvl_ctx
*ctx
;
1310 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
1315 ctx
->parent_ctx
.hw
.init
=
1316 CLK_HW_INIT("intermediate-parent",
1318 &clk_dummy_single_parent_ops
,
1319 CLK_SET_RATE_PARENT
);
1320 ret
= clk_hw_register(NULL
, &ctx
->parent_ctx
.hw
);
1325 CLK_HW_INIT("test-clk", "intermediate-parent",
1326 &clk_dummy_single_parent_ops
,
1327 CLK_SET_RATE_PARENT
);
1328 ret
= clk_hw_register(NULL
, &ctx
->hw
);
1332 ctx
->parent_parent_ctx
.rate
= DUMMY_CLOCK_INIT_RATE
;
1333 ctx
->parent_parent_ctx
.hw
.init
=
1334 CLK_HW_INIT_NO_PARENT("root-parent",
1335 &clk_dummy_rate_ops
,
1337 ret
= clk_hw_register(NULL
, &ctx
->parent_parent_ctx
.hw
);
1345 clk_orphan_two_level_root_last_test_exit(struct kunit
*test
)
1347 struct clk_single_parent_two_lvl_ctx
*ctx
= test
->priv
;
1349 clk_hw_unregister(&ctx
->hw
);
1350 clk_hw_unregister(&ctx
->parent_ctx
.hw
);
1351 clk_hw_unregister(&ctx
->parent_parent_ctx
.hw
);
1355 * Test that, for a clock whose parent used to be orphan, clk_get_rate()
1356 * will return the proper rate.
1359 clk_orphan_two_level_root_last_test_get_rate(struct kunit
*test
)
1361 struct clk_single_parent_two_lvl_ctx
*ctx
= test
->priv
;
1362 struct clk_hw
*hw
= &ctx
->hw
;
1363 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1366 rate
= clk_get_rate(clk
);
1367 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_INIT_RATE
);
1373 * Test that, for a clock whose parent used to be orphan,
1374 * clk_set_rate_range() won't affect its rate if it is already within
1377 * See (for Exynos 4210):
1378 * https://lore.kernel.org/linux-clk/366a0232-bb4a-c357-6aa8-636e398e05eb@samsung.com/
1381 clk_orphan_two_level_root_last_test_set_range(struct kunit
*test
)
1383 struct clk_single_parent_two_lvl_ctx
*ctx
= test
->priv
;
1384 struct clk_hw
*hw
= &ctx
->hw
;
1385 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1389 ret
= clk_set_rate_range(clk
,
1390 DUMMY_CLOCK_INIT_RATE
- 1000,
1391 DUMMY_CLOCK_INIT_RATE
+ 1000);
1392 KUNIT_ASSERT_EQ(test
, ret
, 0);
1394 rate
= clk_get_rate(clk
);
1395 KUNIT_ASSERT_GT(test
, rate
, 0);
1396 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_INIT_RATE
);
1401 static struct kunit_case
1402 clk_orphan_two_level_root_last_test_cases
[] = {
1403 KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate
),
1404 KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range
),
1409 * Test suite for a basic, transparent, clock with a parent that is also
1410 * such a clock. The parent's parent is registered last, while the
1411 * parent and its child are registered in that order. The intermediate
1412 * and leaf clocks will thus be orphan when registered, but the leaf
1413 * clock itself will always have its parent and will never be
1414 * reparented. Indeed, it's only orphan because its parent is.
1416 * These tests exercise the behaviour of the consumer API when dealing
1417 * with an orphan clock, and how we deal with the transition to a valid
1420 static struct kunit_suite
1421 clk_orphan_two_level_root_last_test_suite
= {
1422 .name
= "clk-orphan-two-level-root-last-test",
1423 .init
= clk_orphan_two_level_root_last_test_init
,
1424 .exit
= clk_orphan_two_level_root_last_test_exit
,
1425 .test_cases
= clk_orphan_two_level_root_last_test_cases
,
1429 * Test that clk_set_rate_range won't return an error for a valid range
1430 * and that it will make sure the rate of the clock is within the
1433 static void clk_range_test_set_range(struct kunit
*test
)
1435 struct clk_dummy_context
*ctx
= test
->priv
;
1436 struct clk_hw
*hw
= &ctx
->hw
;
1437 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1440 KUNIT_ASSERT_EQ(test
,
1441 clk_set_rate_range(clk
,
1443 DUMMY_CLOCK_RATE_2
),
1446 rate
= clk_get_rate(clk
);
1447 KUNIT_ASSERT_GT(test
, rate
, 0);
1448 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
1449 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
1455 * Test that calling clk_set_rate_range with a minimum rate higher than
1456 * the maximum rate returns an error.
1458 static void clk_range_test_set_range_invalid(struct kunit
*test
)
1460 struct clk_dummy_context
*ctx
= test
->priv
;
1461 struct clk_hw
*hw
= &ctx
->hw
;
1462 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1464 KUNIT_EXPECT_LT(test
,
1465 clk_set_rate_range(clk
,
1466 DUMMY_CLOCK_RATE_1
+ 1000,
1467 DUMMY_CLOCK_RATE_1
),
1474 * Test that users can't set multiple, disjoints, range that would be
1475 * impossible to meet.
1477 static void clk_range_test_multiple_disjoints_range(struct kunit
*test
)
1479 struct clk_dummy_context
*ctx
= test
->priv
;
1480 struct clk_hw
*hw
= &ctx
->hw
;
1481 struct clk
*user1
, *user2
;
1483 user1
= clk_hw_get_clk(hw
, NULL
);
1484 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user1
);
1486 user2
= clk_hw_get_clk(hw
, NULL
);
1487 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user2
);
1489 KUNIT_ASSERT_EQ(test
,
1490 clk_set_rate_range(user1
, 1000, 2000),
1493 KUNIT_EXPECT_LT(test
,
1494 clk_set_rate_range(user2
, 3000, 4000),
1502 * Test that if our clock has some boundaries and we try to round a rate
1503 * lower than the minimum, the returned rate will be within range.
1505 static void clk_range_test_set_range_round_rate_lower(struct kunit
*test
)
1507 struct clk_dummy_context
*ctx
= test
->priv
;
1508 struct clk_hw
*hw
= &ctx
->hw
;
1509 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1512 KUNIT_ASSERT_EQ(test
,
1513 clk_set_rate_range(clk
,
1515 DUMMY_CLOCK_RATE_2
),
1518 rate
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000);
1519 KUNIT_ASSERT_GT(test
, rate
, 0);
1520 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
1521 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
1527 * Test that if our clock has some boundaries and we try to set a rate
1528 * higher than the maximum, the new rate will be within range.
1530 static void clk_range_test_set_range_set_rate_lower(struct kunit
*test
)
1532 struct clk_dummy_context
*ctx
= test
->priv
;
1533 struct clk_hw
*hw
= &ctx
->hw
;
1534 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1537 KUNIT_ASSERT_EQ(test
,
1538 clk_set_rate_range(clk
,
1540 DUMMY_CLOCK_RATE_2
),
1543 KUNIT_ASSERT_EQ(test
,
1544 clk_set_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000),
1547 rate
= clk_get_rate(clk
);
1548 KUNIT_ASSERT_GT(test
, rate
, 0);
1549 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
1550 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
1556 * Test that if our clock has some boundaries and we try to round and
1557 * set a rate lower than the minimum, the rate returned by
1558 * clk_round_rate() will be consistent with the new rate set by
1561 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit
*test
)
1563 struct clk_dummy_context
*ctx
= test
->priv
;
1564 struct clk_hw
*hw
= &ctx
->hw
;
1565 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1568 KUNIT_ASSERT_EQ(test
,
1569 clk_set_rate_range(clk
,
1571 DUMMY_CLOCK_RATE_2
),
1574 rounded
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000);
1575 KUNIT_ASSERT_GT(test
, rounded
, 0);
1577 KUNIT_ASSERT_EQ(test
,
1578 clk_set_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000),
1581 KUNIT_EXPECT_EQ(test
, rounded
, clk_get_rate(clk
));
1587 * Test that if our clock has some boundaries and we try to round a rate
1588 * higher than the maximum, the returned rate will be within range.
1590 static void clk_range_test_set_range_round_rate_higher(struct kunit
*test
)
1592 struct clk_dummy_context
*ctx
= test
->priv
;
1593 struct clk_hw
*hw
= &ctx
->hw
;
1594 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1597 KUNIT_ASSERT_EQ(test
,
1598 clk_set_rate_range(clk
,
1600 DUMMY_CLOCK_RATE_2
),
1603 rate
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000);
1604 KUNIT_ASSERT_GT(test
, rate
, 0);
1605 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
1606 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
1612 * Test that if our clock has some boundaries and we try to set a rate
1613 * higher than the maximum, the new rate will be within range.
1615 static void clk_range_test_set_range_set_rate_higher(struct kunit
*test
)
1617 struct clk_dummy_context
*ctx
= test
->priv
;
1618 struct clk_hw
*hw
= &ctx
->hw
;
1619 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1622 KUNIT_ASSERT_EQ(test
,
1623 clk_set_rate_range(clk
,
1625 DUMMY_CLOCK_RATE_2
),
1628 KUNIT_ASSERT_EQ(test
,
1629 clk_set_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000),
1632 rate
= clk_get_rate(clk
);
1633 KUNIT_ASSERT_GT(test
, rate
, 0);
1634 KUNIT_EXPECT_GE(test
, rate
, DUMMY_CLOCK_RATE_1
);
1635 KUNIT_EXPECT_LE(test
, rate
, DUMMY_CLOCK_RATE_2
);
1641 * Test that if our clock has some boundaries and we try to round and
1642 * set a rate higher than the maximum, the rate returned by
1643 * clk_round_rate() will be consistent with the new rate set by
1646 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit
*test
)
1648 struct clk_dummy_context
*ctx
= test
->priv
;
1649 struct clk_hw
*hw
= &ctx
->hw
;
1650 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1653 KUNIT_ASSERT_EQ(test
,
1654 clk_set_rate_range(clk
,
1656 DUMMY_CLOCK_RATE_2
),
1659 rounded
= clk_round_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000);
1660 KUNIT_ASSERT_GT(test
, rounded
, 0);
1662 KUNIT_ASSERT_EQ(test
,
1663 clk_set_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000),
1666 KUNIT_EXPECT_EQ(test
, rounded
, clk_get_rate(clk
));
1672 * Test that if our clock has a rate lower than the minimum set by a
1673 * call to clk_set_rate_range(), the rate will be raised to match the
1676 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1677 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1679 static void clk_range_test_set_range_get_rate_raised(struct kunit
*test
)
1681 struct clk_dummy_context
*ctx
= test
->priv
;
1682 struct clk_hw
*hw
= &ctx
->hw
;
1683 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1686 KUNIT_ASSERT_EQ(test
,
1687 clk_set_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000),
1690 KUNIT_ASSERT_EQ(test
,
1691 clk_set_rate_range(clk
,
1693 DUMMY_CLOCK_RATE_2
),
1696 rate
= clk_get_rate(clk
);
1697 KUNIT_ASSERT_GT(test
, rate
, 0);
1698 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
1704 * Test that if our clock has a rate higher than the maximum set by a
1705 * call to clk_set_rate_range(), the rate will be lowered to match the
1708 * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1709 * modify the requested rate, which is our case in clk_dummy_rate_ops.
1711 static void clk_range_test_set_range_get_rate_lowered(struct kunit
*test
)
1713 struct clk_dummy_context
*ctx
= test
->priv
;
1714 struct clk_hw
*hw
= &ctx
->hw
;
1715 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1718 KUNIT_ASSERT_EQ(test
,
1719 clk_set_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000),
1722 KUNIT_ASSERT_EQ(test
,
1723 clk_set_rate_range(clk
,
1725 DUMMY_CLOCK_RATE_2
),
1728 rate
= clk_get_rate(clk
);
1729 KUNIT_ASSERT_GT(test
, rate
, 0);
1730 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
1735 static struct kunit_case clk_range_test_cases
[] = {
1736 KUNIT_CASE(clk_range_test_set_range
),
1737 KUNIT_CASE(clk_range_test_set_range_invalid
),
1738 KUNIT_CASE(clk_range_test_multiple_disjoints_range
),
1739 KUNIT_CASE(clk_range_test_set_range_round_rate_lower
),
1740 KUNIT_CASE(clk_range_test_set_range_set_rate_lower
),
1741 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower
),
1742 KUNIT_CASE(clk_range_test_set_range_round_rate_higher
),
1743 KUNIT_CASE(clk_range_test_set_range_set_rate_higher
),
1744 KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher
),
1745 KUNIT_CASE(clk_range_test_set_range_get_rate_raised
),
1746 KUNIT_CASE(clk_range_test_set_range_get_rate_lowered
),
1751 * Test suite for a basic rate clock, without any parent.
1753 * These tests exercise the rate range API: clk_set_rate_range(),
1754 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range().
1756 static struct kunit_suite clk_range_test_suite
= {
1757 .name
= "clk-range-test",
1758 .init
= clk_test_init
,
1759 .exit
= clk_test_exit
,
1760 .test_cases
= clk_range_test_cases
,
1764 * Test that if we have several subsequent calls to
1765 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1766 * needed each and every time.
1768 * With clk_dummy_maximize_rate_ops, this means that the rate will
1769 * trail along the maximum as it evolves.
1771 static void clk_range_test_set_range_rate_maximized(struct kunit
*test
)
1773 struct clk_dummy_context
*ctx
= test
->priv
;
1774 struct clk_hw
*hw
= &ctx
->hw
;
1775 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1778 KUNIT_ASSERT_EQ(test
,
1779 clk_set_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000),
1782 KUNIT_ASSERT_EQ(test
,
1783 clk_set_rate_range(clk
,
1785 DUMMY_CLOCK_RATE_2
),
1788 rate
= clk_get_rate(clk
);
1789 KUNIT_ASSERT_GT(test
, rate
, 0);
1790 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
1792 KUNIT_ASSERT_EQ(test
,
1793 clk_set_rate_range(clk
,
1795 DUMMY_CLOCK_RATE_2
- 1000),
1798 rate
= clk_get_rate(clk
);
1799 KUNIT_ASSERT_GT(test
, rate
, 0);
1800 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
- 1000);
1802 KUNIT_ASSERT_EQ(test
,
1803 clk_set_rate_range(clk
,
1805 DUMMY_CLOCK_RATE_2
),
1808 rate
= clk_get_rate(clk
);
1809 KUNIT_ASSERT_GT(test
, rate
, 0);
1810 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
1816 * Test that if we have several subsequent calls to
1817 * clk_set_rate_range(), across multiple users, the core will reevaluate
1818 * whether a new rate is needed each and every time.
1820 * With clk_dummy_maximize_rate_ops, this means that the rate will
1821 * trail along the maximum as it evolves.
1823 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit
*test
)
1825 struct clk_dummy_context
*ctx
= test
->priv
;
1826 struct clk_hw
*hw
= &ctx
->hw
;
1827 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1828 struct clk
*user1
, *user2
;
1831 user1
= clk_hw_get_clk(hw
, NULL
);
1832 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user1
);
1834 user2
= clk_hw_get_clk(hw
, NULL
);
1835 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user2
);
1837 KUNIT_ASSERT_EQ(test
,
1838 clk_set_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000),
1841 KUNIT_ASSERT_EQ(test
,
1842 clk_set_rate_range(user1
,
1844 DUMMY_CLOCK_RATE_2
),
1847 rate
= clk_get_rate(clk
);
1848 KUNIT_ASSERT_GT(test
, rate
, 0);
1849 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
1851 KUNIT_ASSERT_EQ(test
,
1852 clk_set_rate_range(user2
,
1854 DUMMY_CLOCK_RATE_1
),
1857 rate
= clk_get_rate(clk
);
1858 KUNIT_ASSERT_GT(test
, rate
, 0);
1859 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
1861 KUNIT_ASSERT_EQ(test
,
1862 clk_drop_range(user2
),
1865 rate
= clk_get_rate(clk
);
1866 KUNIT_ASSERT_GT(test
, rate
, 0);
1867 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
1875 * Test that if we have several subsequent calls to
1876 * clk_set_rate_range(), across multiple users, the core will reevaluate
1877 * whether a new rate is needed, including when a user drop its clock.
1879 * With clk_dummy_maximize_rate_ops, this means that the rate will
1880 * trail along the maximum as it evolves.
1882 static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit
*test
)
1884 struct clk_dummy_context
*ctx
= test
->priv
;
1885 struct clk_hw
*hw
= &ctx
->hw
;
1886 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1887 struct clk
*user1
, *user2
;
1890 user1
= clk_hw_get_clk(hw
, NULL
);
1891 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user1
);
1893 user2
= clk_hw_get_clk(hw
, NULL
);
1894 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user2
);
1896 KUNIT_ASSERT_EQ(test
,
1897 clk_set_rate(clk
, DUMMY_CLOCK_RATE_2
+ 1000),
1900 KUNIT_ASSERT_EQ(test
,
1901 clk_set_rate_range(user1
,
1903 DUMMY_CLOCK_RATE_2
),
1906 rate
= clk_get_rate(clk
);
1907 KUNIT_ASSERT_GT(test
, rate
, 0);
1908 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
1910 KUNIT_ASSERT_EQ(test
,
1911 clk_set_rate_range(user2
,
1913 DUMMY_CLOCK_RATE_1
),
1916 rate
= clk_get_rate(clk
);
1917 KUNIT_ASSERT_GT(test
, rate
, 0);
1918 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
1922 rate
= clk_get_rate(clk
);
1923 KUNIT_ASSERT_GT(test
, rate
, 0);
1924 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
1930 static struct kunit_case clk_range_maximize_test_cases
[] = {
1931 KUNIT_CASE(clk_range_test_set_range_rate_maximized
),
1932 KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized
),
1933 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized
),
1938 * Test suite for a basic rate clock, without any parent.
1940 * These tests exercise the rate range API: clk_set_rate_range(),
1941 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
1942 * driver that will always try to run at the highest possible rate.
1944 static struct kunit_suite clk_range_maximize_test_suite
= {
1945 .name
= "clk-range-maximize-test",
1946 .init
= clk_maximize_test_init
,
1947 .exit
= clk_test_exit
,
1948 .test_cases
= clk_range_maximize_test_cases
,
1952 * Test that if we have several subsequent calls to
1953 * clk_set_rate_range(), the core will reevaluate whether a new rate is
1954 * needed each and every time.
1956 * With clk_dummy_minimize_rate_ops, this means that the rate will
1957 * trail along the minimum as it evolves.
1959 static void clk_range_test_set_range_rate_minimized(struct kunit
*test
)
1961 struct clk_dummy_context
*ctx
= test
->priv
;
1962 struct clk_hw
*hw
= &ctx
->hw
;
1963 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
1966 KUNIT_ASSERT_EQ(test
,
1967 clk_set_rate(clk
, DUMMY_CLOCK_RATE_1
- 1000),
1970 KUNIT_ASSERT_EQ(test
,
1971 clk_set_rate_range(clk
,
1973 DUMMY_CLOCK_RATE_2
),
1976 rate
= clk_get_rate(clk
);
1977 KUNIT_ASSERT_GT(test
, rate
, 0);
1978 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
1980 KUNIT_ASSERT_EQ(test
,
1981 clk_set_rate_range(clk
,
1982 DUMMY_CLOCK_RATE_1
+ 1000,
1983 DUMMY_CLOCK_RATE_2
),
1986 rate
= clk_get_rate(clk
);
1987 KUNIT_ASSERT_GT(test
, rate
, 0);
1988 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
+ 1000);
1990 KUNIT_ASSERT_EQ(test
,
1991 clk_set_rate_range(clk
,
1993 DUMMY_CLOCK_RATE_2
),
1996 rate
= clk_get_rate(clk
);
1997 KUNIT_ASSERT_GT(test
, rate
, 0);
1998 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
2004 * Test that if we have several subsequent calls to
2005 * clk_set_rate_range(), across multiple users, the core will reevaluate
2006 * whether a new rate is needed each and every time.
2008 * With clk_dummy_minimize_rate_ops, this means that the rate will
2009 * trail along the minimum as it evolves.
2011 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit
*test
)
2013 struct clk_dummy_context
*ctx
= test
->priv
;
2014 struct clk_hw
*hw
= &ctx
->hw
;
2015 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
2016 struct clk
*user1
, *user2
;
2019 user1
= clk_hw_get_clk(hw
, NULL
);
2020 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user1
);
2022 user2
= clk_hw_get_clk(hw
, NULL
);
2023 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user2
);
2025 KUNIT_ASSERT_EQ(test
,
2026 clk_set_rate_range(user1
,
2031 rate
= clk_get_rate(clk
);
2032 KUNIT_ASSERT_GT(test
, rate
, 0);
2033 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
2035 KUNIT_ASSERT_EQ(test
,
2036 clk_set_rate_range(user2
,
2041 rate
= clk_get_rate(clk
);
2042 KUNIT_ASSERT_GT(test
, rate
, 0);
2043 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
2045 KUNIT_ASSERT_EQ(test
,
2046 clk_drop_range(user2
),
2049 rate
= clk_get_rate(clk
);
2050 KUNIT_ASSERT_GT(test
, rate
, 0);
2051 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
2059 * Test that if we have several subsequent calls to
2060 * clk_set_rate_range(), across multiple users, the core will reevaluate
2061 * whether a new rate is needed, including when a user drop its clock.
2063 * With clk_dummy_minimize_rate_ops, this means that the rate will
2064 * trail along the minimum as it evolves.
2066 static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit
*test
)
2068 struct clk_dummy_context
*ctx
= test
->priv
;
2069 struct clk_hw
*hw
= &ctx
->hw
;
2070 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
2071 struct clk
*user1
, *user2
;
2074 user1
= clk_hw_get_clk(hw
, NULL
);
2075 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user1
);
2077 user2
= clk_hw_get_clk(hw
, NULL
);
2078 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, user2
);
2080 KUNIT_ASSERT_EQ(test
,
2081 clk_set_rate_range(user1
,
2086 rate
= clk_get_rate(clk
);
2087 KUNIT_ASSERT_GT(test
, rate
, 0);
2088 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
2090 KUNIT_ASSERT_EQ(test
,
2091 clk_set_rate_range(user2
,
2096 rate
= clk_get_rate(clk
);
2097 KUNIT_ASSERT_GT(test
, rate
, 0);
2098 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_2
);
2102 rate
= clk_get_rate(clk
);
2103 KUNIT_ASSERT_GT(test
, rate
, 0);
2104 KUNIT_EXPECT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
2110 static struct kunit_case clk_range_minimize_test_cases
[] = {
2111 KUNIT_CASE(clk_range_test_set_range_rate_minimized
),
2112 KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized
),
2113 KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized
),
2118 * Test suite for a basic rate clock, without any parent.
2120 * These tests exercise the rate range API: clk_set_rate_range(),
2121 * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
2122 * driver that will always try to run at the lowest possible rate.
2124 static struct kunit_suite clk_range_minimize_test_suite
= {
2125 .name
= "clk-range-minimize-test",
2126 .init
= clk_minimize_test_init
,
2127 .exit
= clk_test_exit
,
2128 .test_cases
= clk_range_minimize_test_cases
,
2131 struct clk_leaf_mux_ctx
{
2132 struct clk_multiple_parent_ctx mux_ctx
;
2134 struct clk_hw parent
;
2135 struct clk_rate_request
*req
;
2136 int (*determine_rate_func
)(struct clk_hw
*hw
, struct clk_rate_request
*req
);
2139 static int clk_leaf_mux_determine_rate(struct clk_hw
*hw
, struct clk_rate_request
*req
)
2141 struct clk_leaf_mux_ctx
*ctx
= container_of(hw
, struct clk_leaf_mux_ctx
, hw
);
2143 struct clk_rate_request
*parent_req
= ctx
->req
;
2145 clk_hw_forward_rate_request(hw
, req
, req
->best_parent_hw
, parent_req
, req
->rate
);
2146 ret
= ctx
->determine_rate_func(req
->best_parent_hw
, parent_req
);
2150 req
->rate
= parent_req
->rate
;
2155 static const struct clk_ops clk_leaf_mux_set_rate_parent_ops
= {
2156 .determine_rate
= clk_leaf_mux_determine_rate
,
2157 .set_parent
= clk_dummy_single_set_parent
,
2158 .get_parent
= clk_dummy_single_get_parent
,
2162 clk_leaf_mux_set_rate_parent_test_init(struct kunit
*test
)
2164 struct clk_leaf_mux_ctx
*ctx
;
2165 const char *top_parents
[2] = { "parent-0", "parent-1" };
2168 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
2173 ctx
->mux_ctx
.parents_ctx
[0].hw
.init
= CLK_HW_INIT_NO_PARENT("parent-0",
2174 &clk_dummy_rate_ops
,
2176 ctx
->mux_ctx
.parents_ctx
[0].rate
= DUMMY_CLOCK_RATE_1
;
2177 ret
= clk_hw_register(NULL
, &ctx
->mux_ctx
.parents_ctx
[0].hw
);
2181 ctx
->mux_ctx
.parents_ctx
[1].hw
.init
= CLK_HW_INIT_NO_PARENT("parent-1",
2182 &clk_dummy_rate_ops
,
2184 ctx
->mux_ctx
.parents_ctx
[1].rate
= DUMMY_CLOCK_RATE_2
;
2185 ret
= clk_hw_register(NULL
, &ctx
->mux_ctx
.parents_ctx
[1].hw
);
2189 ctx
->mux_ctx
.current_parent
= 0;
2190 ctx
->mux_ctx
.hw
.init
= CLK_HW_INIT_PARENTS("test-mux", top_parents
,
2191 &clk_multiple_parents_mux_ops
,
2193 ret
= clk_hw_register(NULL
, &ctx
->mux_ctx
.hw
);
2197 ctx
->parent
.init
= CLK_HW_INIT_HW("test-parent", &ctx
->mux_ctx
.hw
,
2198 &empty_clk_ops
, CLK_SET_RATE_PARENT
);
2199 ret
= clk_hw_register(NULL
, &ctx
->parent
);
2203 ctx
->hw
.init
= CLK_HW_INIT_HW("test-clock", &ctx
->parent
,
2204 &clk_leaf_mux_set_rate_parent_ops
,
2205 CLK_SET_RATE_PARENT
);
2206 ret
= clk_hw_register(NULL
, &ctx
->hw
);
2213 static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit
*test
)
2215 struct clk_leaf_mux_ctx
*ctx
= test
->priv
;
2217 clk_hw_unregister(&ctx
->hw
);
2218 clk_hw_unregister(&ctx
->parent
);
2219 clk_hw_unregister(&ctx
->mux_ctx
.hw
);
2220 clk_hw_unregister(&ctx
->mux_ctx
.parents_ctx
[0].hw
);
2221 clk_hw_unregister(&ctx
->mux_ctx
.parents_ctx
[1].hw
);
2224 struct clk_leaf_mux_set_rate_parent_determine_rate_test_case
{
2226 int (*determine_rate_func
)(struct clk_hw
*hw
, struct clk_rate_request
*req
);
2230 clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc(
2231 const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case
*t
, char *desc
)
2233 strcpy(desc
, t
->desc
);
2236 static const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case
2237 clk_leaf_mux_set_rate_parent_determine_rate_test_cases
[] = {
2240 * Test that __clk_determine_rate() on the parent that can't
2241 * change rate doesn't return a clk_rate_request structure with
2242 * the best_parent_hw pointer pointing to the parent.
2244 .desc
= "clk_leaf_mux_set_rate_parent__clk_determine_rate_proper_parent",
2245 .determine_rate_func
= __clk_determine_rate
,
2249 * Test that __clk_mux_determine_rate() on the parent that
2250 * can't change rate doesn't return a clk_rate_request
2251 * structure with the best_parent_hw pointer pointing to
2254 .desc
= "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_proper_parent",
2255 .determine_rate_func
= __clk_mux_determine_rate
,
2259 * Test that __clk_mux_determine_rate_closest() on the parent
2260 * that can't change rate doesn't return a clk_rate_request
2261 * structure with the best_parent_hw pointer pointing to
2264 .desc
= "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_closest_proper_parent",
2265 .determine_rate_func
= __clk_mux_determine_rate_closest
,
2269 * Test that clk_hw_determine_rate_no_reparent() on the parent
2270 * that can't change rate doesn't return a clk_rate_request
2271 * structure with the best_parent_hw pointer pointing to
2274 .desc
= "clk_leaf_mux_set_rate_parent_clk_hw_determine_rate_no_reparent_proper_parent",
2275 .determine_rate_func
= clk_hw_determine_rate_no_reparent
,
2279 KUNIT_ARRAY_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test
,
2280 clk_leaf_mux_set_rate_parent_determine_rate_test_cases
,
2281 clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc
)
2284 * Test that when a clk that can't change rate itself calls a function like
2285 * __clk_determine_rate() on its parent it doesn't get back a clk_rate_request
2286 * structure that has the best_parent_hw pointer point to the clk_hw passed
2287 * into the determine rate function. See commit 262ca38f4b6e ("clk: Stop
2288 * forwarding clk_rate_requests to the parent") for more background.
2290 static void clk_leaf_mux_set_rate_parent_determine_rate_test(struct kunit
*test
)
2292 struct clk_leaf_mux_ctx
*ctx
= test
->priv
;
2293 struct clk_hw
*hw
= &ctx
->hw
;
2294 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
2295 struct clk_rate_request req
;
2297 const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case
*test_param
;
2299 test_param
= test
->param_value
;
2300 ctx
->determine_rate_func
= test_param
->determine_rate_func
;
2303 rate
= clk_get_rate(clk
);
2304 KUNIT_ASSERT_EQ(test
, rate
, DUMMY_CLOCK_RATE_1
);
2305 KUNIT_ASSERT_EQ(test
, DUMMY_CLOCK_RATE_2
, clk_round_rate(clk
, DUMMY_CLOCK_RATE_2
));
2307 KUNIT_EXPECT_EQ(test
, req
.rate
, DUMMY_CLOCK_RATE_2
);
2308 KUNIT_EXPECT_EQ(test
, req
.best_parent_rate
, DUMMY_CLOCK_RATE_2
);
2309 KUNIT_EXPECT_PTR_EQ(test
, req
.best_parent_hw
, &ctx
->mux_ctx
.hw
);
2314 static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases
[] = {
2315 KUNIT_CASE_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test
,
2316 clk_leaf_mux_set_rate_parent_determine_rate_test_gen_params
),
2321 * Test suite for a clock whose parent is a pass-through clk whose parent is a
2322 * mux with multiple parents. The leaf and pass-through clocks have the
2323 * CLK_SET_RATE_PARENT flag, and will forward rate requests to the mux, which
2324 * will then select which parent is the best fit for a given rate.
2326 * These tests exercise the behaviour of muxes, and the proper selection
2329 static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite
= {
2330 .name
= "clk-leaf-mux-set-rate-parent",
2331 .init
= clk_leaf_mux_set_rate_parent_test_init
,
2332 .exit
= clk_leaf_mux_set_rate_parent_test_exit
,
2333 .test_cases
= clk_leaf_mux_set_rate_parent_test_cases
,
2336 struct clk_mux_notifier_rate_change
{
2338 unsigned long old_rate
;
2339 unsigned long new_rate
;
2340 wait_queue_head_t wq
;
2343 struct clk_mux_notifier_ctx
{
2344 struct clk_multiple_parent_ctx mux_ctx
;
2346 struct notifier_block clk_nb
;
2347 struct clk_mux_notifier_rate_change pre_rate_change
;
2348 struct clk_mux_notifier_rate_change post_rate_change
;
2351 #define NOTIFIER_TIMEOUT_MS 100
2353 static int clk_mux_notifier_callback(struct notifier_block
*nb
,
2354 unsigned long action
, void *data
)
2356 struct clk_notifier_data
*clk_data
= data
;
2357 struct clk_mux_notifier_ctx
*ctx
= container_of(nb
,
2358 struct clk_mux_notifier_ctx
,
2361 if (action
& PRE_RATE_CHANGE
) {
2362 ctx
->pre_rate_change
.old_rate
= clk_data
->old_rate
;
2363 ctx
->pre_rate_change
.new_rate
= clk_data
->new_rate
;
2364 ctx
->pre_rate_change
.done
= true;
2365 wake_up_interruptible(&ctx
->pre_rate_change
.wq
);
2368 if (action
& POST_RATE_CHANGE
) {
2369 ctx
->post_rate_change
.old_rate
= clk_data
->old_rate
;
2370 ctx
->post_rate_change
.new_rate
= clk_data
->new_rate
;
2371 ctx
->post_rate_change
.done
= true;
2372 wake_up_interruptible(&ctx
->post_rate_change
.wq
);
2378 static int clk_mux_notifier_test_init(struct kunit
*test
)
2380 struct clk_mux_notifier_ctx
*ctx
;
2381 const char *top_parents
[2] = { "parent-0", "parent-1" };
2384 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
2388 ctx
->clk_nb
.notifier_call
= clk_mux_notifier_callback
;
2389 init_waitqueue_head(&ctx
->pre_rate_change
.wq
);
2390 init_waitqueue_head(&ctx
->post_rate_change
.wq
);
2392 ctx
->mux_ctx
.parents_ctx
[0].hw
.init
= CLK_HW_INIT_NO_PARENT("parent-0",
2393 &clk_dummy_rate_ops
,
2395 ctx
->mux_ctx
.parents_ctx
[0].rate
= DUMMY_CLOCK_RATE_1
;
2396 ret
= clk_hw_register(NULL
, &ctx
->mux_ctx
.parents_ctx
[0].hw
);
2400 ctx
->mux_ctx
.parents_ctx
[1].hw
.init
= CLK_HW_INIT_NO_PARENT("parent-1",
2401 &clk_dummy_rate_ops
,
2403 ctx
->mux_ctx
.parents_ctx
[1].rate
= DUMMY_CLOCK_RATE_2
;
2404 ret
= clk_hw_register(NULL
, &ctx
->mux_ctx
.parents_ctx
[1].hw
);
2408 ctx
->mux_ctx
.current_parent
= 0;
2409 ctx
->mux_ctx
.hw
.init
= CLK_HW_INIT_PARENTS("test-mux", top_parents
,
2410 &clk_multiple_parents_mux_ops
,
2412 ret
= clk_hw_register(NULL
, &ctx
->mux_ctx
.hw
);
2416 ctx
->clk
= clk_hw_get_clk(&ctx
->mux_ctx
.hw
, NULL
);
2417 ret
= clk_notifier_register(ctx
->clk
, &ctx
->clk_nb
);
2424 static void clk_mux_notifier_test_exit(struct kunit
*test
)
2426 struct clk_mux_notifier_ctx
*ctx
= test
->priv
;
2427 struct clk
*clk
= ctx
->clk
;
2429 clk_notifier_unregister(clk
, &ctx
->clk_nb
);
2432 clk_hw_unregister(&ctx
->mux_ctx
.hw
);
2433 clk_hw_unregister(&ctx
->mux_ctx
.parents_ctx
[0].hw
);
2434 clk_hw_unregister(&ctx
->mux_ctx
.parents_ctx
[1].hw
);
2438 * Test that if the we have a notifier registered on a mux, the core
2439 * will notify us when we switch to another parent, and with the proper
2440 * old and new rates.
2442 static void clk_mux_notifier_set_parent_test(struct kunit
*test
)
2444 struct clk_mux_notifier_ctx
*ctx
= test
->priv
;
2445 struct clk_hw
*hw
= &ctx
->mux_ctx
.hw
;
2446 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
2447 struct clk
*new_parent
= clk_hw_get_clk(&ctx
->mux_ctx
.parents_ctx
[1].hw
, NULL
);
2450 ret
= clk_set_parent(clk
, new_parent
);
2451 KUNIT_ASSERT_EQ(test
, ret
, 0);
2453 ret
= wait_event_interruptible_timeout(ctx
->pre_rate_change
.wq
,
2454 ctx
->pre_rate_change
.done
,
2455 msecs_to_jiffies(NOTIFIER_TIMEOUT_MS
));
2456 KUNIT_ASSERT_GT(test
, ret
, 0);
2458 KUNIT_EXPECT_EQ(test
, ctx
->pre_rate_change
.old_rate
, DUMMY_CLOCK_RATE_1
);
2459 KUNIT_EXPECT_EQ(test
, ctx
->pre_rate_change
.new_rate
, DUMMY_CLOCK_RATE_2
);
2461 ret
= wait_event_interruptible_timeout(ctx
->post_rate_change
.wq
,
2462 ctx
->post_rate_change
.done
,
2463 msecs_to_jiffies(NOTIFIER_TIMEOUT_MS
));
2464 KUNIT_ASSERT_GT(test
, ret
, 0);
2466 KUNIT_EXPECT_EQ(test
, ctx
->post_rate_change
.old_rate
, DUMMY_CLOCK_RATE_1
);
2467 KUNIT_EXPECT_EQ(test
, ctx
->post_rate_change
.new_rate
, DUMMY_CLOCK_RATE_2
);
2469 clk_put(new_parent
);
2473 static struct kunit_case clk_mux_notifier_test_cases
[] = {
2474 KUNIT_CASE(clk_mux_notifier_set_parent_test
),
2479 * Test suite for a mux with multiple parents, and a notifier registered
2482 * These tests exercise the behaviour of notifiers.
2484 static struct kunit_suite clk_mux_notifier_test_suite
= {
2485 .name
= "clk-mux-notifier",
2486 .init
= clk_mux_notifier_test_init
,
2487 .exit
= clk_mux_notifier_test_exit
,
2488 .test_cases
= clk_mux_notifier_test_cases
,
2492 clk_mux_no_reparent_test_init(struct kunit
*test
)
2494 struct clk_multiple_parent_ctx
*ctx
;
2495 const char *parents
[2] = { "parent-0", "parent-1"};
2498 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
2503 ctx
->parents_ctx
[0].hw
.init
= CLK_HW_INIT_NO_PARENT("parent-0",
2504 &clk_dummy_rate_ops
,
2506 ctx
->parents_ctx
[0].rate
= DUMMY_CLOCK_RATE_1
;
2507 ret
= clk_hw_register(NULL
, &ctx
->parents_ctx
[0].hw
);
2511 ctx
->parents_ctx
[1].hw
.init
= CLK_HW_INIT_NO_PARENT("parent-1",
2512 &clk_dummy_rate_ops
,
2514 ctx
->parents_ctx
[1].rate
= DUMMY_CLOCK_RATE_2
;
2515 ret
= clk_hw_register(NULL
, &ctx
->parents_ctx
[1].hw
);
2519 ctx
->current_parent
= 0;
2520 ctx
->hw
.init
= CLK_HW_INIT_PARENTS("test-mux", parents
,
2521 &clk_multiple_parents_no_reparent_mux_ops
,
2523 ret
= clk_hw_register(NULL
, &ctx
->hw
);
2531 clk_mux_no_reparent_test_exit(struct kunit
*test
)
2533 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
2535 clk_hw_unregister(&ctx
->hw
);
2536 clk_hw_unregister(&ctx
->parents_ctx
[0].hw
);
2537 clk_hw_unregister(&ctx
->parents_ctx
[1].hw
);
2541 * Test that if the we have a mux that cannot change parent and we call
2542 * clk_round_rate() on it with a rate that should cause it to change
2545 static void clk_mux_no_reparent_round_rate(struct kunit
*test
)
2547 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
2548 struct clk_hw
*hw
= &ctx
->hw
;
2549 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
2550 struct clk
*other_parent
, *parent
;
2551 unsigned long other_parent_rate
;
2552 unsigned long parent_rate
;
2555 parent
= clk_get_parent(clk
);
2556 KUNIT_ASSERT_PTR_NE(test
, parent
, NULL
);
2558 parent_rate
= clk_get_rate(parent
);
2559 KUNIT_ASSERT_GT(test
, parent_rate
, 0);
2561 other_parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
2562 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, other_parent
);
2563 KUNIT_ASSERT_FALSE(test
, clk_is_match(parent
, other_parent
));
2565 other_parent_rate
= clk_get_rate(other_parent
);
2566 KUNIT_ASSERT_GT(test
, other_parent_rate
, 0);
2567 clk_put(other_parent
);
2569 rounded_rate
= clk_round_rate(clk
, other_parent_rate
);
2570 KUNIT_ASSERT_GT(test
, rounded_rate
, 0);
2571 KUNIT_EXPECT_EQ(test
, rounded_rate
, parent_rate
);
2577 * Test that if the we have a mux that cannot change parent and we call
2578 * clk_set_rate() on it with a rate that should cause it to change
2581 static void clk_mux_no_reparent_set_rate(struct kunit
*test
)
2583 struct clk_multiple_parent_ctx
*ctx
= test
->priv
;
2584 struct clk_hw
*hw
= &ctx
->hw
;
2585 struct clk
*clk
= clk_hw_get_clk(hw
, NULL
);
2586 struct clk
*other_parent
, *parent
;
2587 unsigned long other_parent_rate
;
2588 unsigned long parent_rate
;
2592 parent
= clk_get_parent(clk
);
2593 KUNIT_ASSERT_PTR_NE(test
, parent
, NULL
);
2595 parent_rate
= clk_get_rate(parent
);
2596 KUNIT_ASSERT_GT(test
, parent_rate
, 0);
2598 other_parent
= clk_hw_get_clk(&ctx
->parents_ctx
[1].hw
, NULL
);
2599 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, other_parent
);
2600 KUNIT_ASSERT_FALSE(test
, clk_is_match(parent
, other_parent
));
2602 other_parent_rate
= clk_get_rate(other_parent
);
2603 KUNIT_ASSERT_GT(test
, other_parent_rate
, 0);
2604 clk_put(other_parent
);
2606 ret
= clk_set_rate(clk
, other_parent_rate
);
2607 KUNIT_ASSERT_EQ(test
, ret
, 0);
2609 rate
= clk_get_rate(clk
);
2610 KUNIT_ASSERT_GT(test
, rate
, 0);
2611 KUNIT_EXPECT_EQ(test
, rate
, parent_rate
);
2616 static struct kunit_case clk_mux_no_reparent_test_cases
[] = {
2617 KUNIT_CASE(clk_mux_no_reparent_round_rate
),
2618 KUNIT_CASE(clk_mux_no_reparent_set_rate
),
2623 * Test suite for a clock mux that isn't allowed to change parent, using
2624 * the clk_hw_determine_rate_no_reparent() helper.
2626 * These tests exercise that helper, and the proper selection of
2627 * rates and parents.
2629 static struct kunit_suite clk_mux_no_reparent_test_suite
= {
2630 .name
= "clk-mux-no-reparent",
2631 .init
= clk_mux_no_reparent_test_init
,
2632 .exit
= clk_mux_no_reparent_test_exit
,
2633 .test_cases
= clk_mux_no_reparent_test_cases
,
2636 struct clk_register_clk_parent_data_test_case
{
2638 struct clk_parent_data pdata
;
2642 clk_register_clk_parent_data_test_case_to_desc(
2643 const struct clk_register_clk_parent_data_test_case
*t
, char *desc
)
2645 strcpy(desc
, t
->desc
);
2648 static const struct clk_register_clk_parent_data_test_case
2649 clk_register_clk_parent_data_of_cases
[] = {
2652 * Test that a clk registered with a struct device_node can
2653 * find a parent based on struct clk_parent_data::index.
2655 .desc
= "clk_parent_data_of_index_test",
2660 * Test that a clk registered with a struct device_node can
2661 * find a parent based on struct clk_parent_data::fwname.
2663 .desc
= "clk_parent_data_of_fwname_test",
2664 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT1
,
2668 * Test that a clk registered with a struct device_node can
2669 * find a parent based on struct clk_parent_data::name.
2671 .desc
= "clk_parent_data_of_name_test",
2672 /* The index must be negative to indicate firmware not used */
2674 .pdata
.name
= CLK_PARENT_DATA_1MHZ_NAME
,
2678 * Test that a clk registered with a struct device_node can
2679 * find a parent based on struct
2680 * clk_parent_data::{fw_name,name}.
2682 .desc
= "clk_parent_data_of_fwname_name_test",
2683 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT1
,
2684 .pdata
.name
= "not_matching",
2688 * Test that a clk registered with a struct device_node can
2689 * find a parent based on struct clk_parent_data::{index,name}.
2690 * Index takes priority.
2692 .desc
= "clk_parent_data_of_index_name_priority_test",
2694 .pdata
.name
= "not_matching",
2698 * Test that a clk registered with a struct device_node can
2699 * find a parent based on struct
2700 * clk_parent_data::{index,fwname,name}. The fw_name takes
2701 * priority over index and name.
2703 .desc
= "clk_parent_data_of_index_fwname_name_priority_test",
2705 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT1
,
2706 .pdata
.name
= "not_matching",
2710 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_of_test
, clk_register_clk_parent_data_of_cases
,
2711 clk_register_clk_parent_data_test_case_to_desc
)
2714 * struct clk_register_clk_parent_data_of_ctx - Context for clk_parent_data OF tests
2715 * @np: device node of clk under test
2716 * @hw: clk_hw for clk under test
2718 struct clk_register_clk_parent_data_of_ctx
{
2719 struct device_node
*np
;
2723 static int clk_register_clk_parent_data_of_test_init(struct kunit
*test
)
2725 struct clk_register_clk_parent_data_of_ctx
*ctx
;
2727 KUNIT_ASSERT_EQ(test
, 0,
2728 of_overlay_apply_kunit(test
, kunit_clk_parent_data_test
));
2730 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
2735 ctx
->np
= of_find_compatible_node(NULL
, NULL
, "test,clk-parent-data");
2739 of_node_put_kunit(test
, ctx
->np
);
2745 * Test that a clk registered with a struct device_node can find a parent based on
2746 * struct clk_parent_data when the hw member isn't set.
2748 static void clk_register_clk_parent_data_of_test(struct kunit
*test
)
2750 struct clk_register_clk_parent_data_of_ctx
*ctx
= test
->priv
;
2751 struct clk_hw
*parent_hw
;
2752 const struct clk_register_clk_parent_data_test_case
*test_param
;
2753 struct clk_init_data init
= { };
2754 struct clk
*expected_parent
, *actual_parent
;
2756 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
->np
);
2758 expected_parent
= of_clk_get_kunit(test
, ctx
->np
, 0);
2759 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, expected_parent
);
2761 test_param
= test
->param_value
;
2762 init
.parent_data
= &test_param
->pdata
;
2763 init
.num_parents
= 1;
2764 init
.name
= "parent_data_of_test_clk";
2765 init
.ops
= &clk_dummy_single_parent_ops
;
2766 ctx
->hw
.init
= &init
;
2767 KUNIT_ASSERT_EQ(test
, 0, of_clk_hw_register_kunit(test
, ctx
->np
, &ctx
->hw
));
2769 parent_hw
= clk_hw_get_parent(&ctx
->hw
);
2770 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent_hw
);
2772 actual_parent
= clk_hw_get_clk_kunit(test
, parent_hw
, __func__
);
2773 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, actual_parent
);
2775 KUNIT_EXPECT_TRUE(test
, clk_is_match(expected_parent
, actual_parent
));
2778 static struct kunit_case clk_register_clk_parent_data_of_test_cases
[] = {
2779 KUNIT_CASE_PARAM(clk_register_clk_parent_data_of_test
,
2780 clk_register_clk_parent_data_of_test_gen_params
),
2785 * Test suite for registering clks with struct clk_parent_data and a struct
2788 static struct kunit_suite clk_register_clk_parent_data_of_suite
= {
2789 .name
= "clk_register_clk_parent_data_of",
2790 .init
= clk_register_clk_parent_data_of_test_init
,
2791 .test_cases
= clk_register_clk_parent_data_of_test_cases
,
2795 * struct clk_register_clk_parent_data_device_ctx - Context for clk_parent_data device tests
2796 * @dev: device of clk under test
2797 * @hw: clk_hw for clk under test
2798 * @pdrv: driver to attach to find @dev
2800 struct clk_register_clk_parent_data_device_ctx
{
2803 struct platform_driver pdrv
;
2806 static inline struct clk_register_clk_parent_data_device_ctx
*
2807 clk_register_clk_parent_data_driver_to_test_context(struct platform_device
*pdev
)
2809 return container_of(to_platform_driver(pdev
->dev
.driver
),
2810 struct clk_register_clk_parent_data_device_ctx
, pdrv
);
2813 static int clk_register_clk_parent_data_device_probe(struct platform_device
*pdev
)
2815 struct clk_register_clk_parent_data_device_ctx
*ctx
;
2817 ctx
= clk_register_clk_parent_data_driver_to_test_context(pdev
);
2818 ctx
->dev
= &pdev
->dev
;
2823 static void clk_register_clk_parent_data_device_driver(struct kunit
*test
)
2825 struct clk_register_clk_parent_data_device_ctx
*ctx
= test
->priv
;
2826 static const struct of_device_id match_table
[] = {
2827 { .compatible
= "test,clk-parent-data" },
2831 ctx
->pdrv
.probe
= clk_register_clk_parent_data_device_probe
;
2832 ctx
->pdrv
.driver
.of_match_table
= match_table
;
2833 ctx
->pdrv
.driver
.name
= __func__
;
2834 ctx
->pdrv
.driver
.owner
= THIS_MODULE
;
2836 KUNIT_ASSERT_EQ(test
, 0, kunit_platform_driver_register(test
, &ctx
->pdrv
));
2837 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
->dev
);
2840 static const struct clk_register_clk_parent_data_test_case
2841 clk_register_clk_parent_data_device_cases
[] = {
2844 * Test that a clk registered with a struct device can find a
2845 * parent based on struct clk_parent_data::index.
2847 .desc
= "clk_parent_data_device_index_test",
2852 * Test that a clk registered with a struct device can find a
2853 * parent based on struct clk_parent_data::fwname.
2855 .desc
= "clk_parent_data_device_fwname_test",
2856 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT2
,
2860 * Test that a clk registered with a struct device can find a
2861 * parent based on struct clk_parent_data::name.
2863 .desc
= "clk_parent_data_device_name_test",
2864 /* The index must be negative to indicate firmware not used */
2866 .pdata
.name
= CLK_PARENT_DATA_50MHZ_NAME
,
2870 * Test that a clk registered with a struct device can find a
2871 * parent based on struct clk_parent_data::{fw_name,name}.
2873 .desc
= "clk_parent_data_device_fwname_name_test",
2874 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT2
,
2875 .pdata
.name
= "not_matching",
2879 * Test that a clk registered with a struct device can find a
2880 * parent based on struct clk_parent_data::{index,name}. Index
2883 .desc
= "clk_parent_data_device_index_name_priority_test",
2885 .pdata
.name
= "not_matching",
2889 * Test that a clk registered with a struct device can find a
2890 * parent based on struct clk_parent_data::{index,fwname,name}.
2891 * The fw_name takes priority over index and name.
2893 .desc
= "clk_parent_data_device_index_fwname_name_priority_test",
2895 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT2
,
2896 .pdata
.name
= "not_matching",
2900 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_test
,
2901 clk_register_clk_parent_data_device_cases
,
2902 clk_register_clk_parent_data_test_case_to_desc
)
2905 * Test that a clk registered with a struct device can find a parent based on
2906 * struct clk_parent_data when the hw member isn't set.
2908 static void clk_register_clk_parent_data_device_test(struct kunit
*test
)
2910 struct clk_register_clk_parent_data_device_ctx
*ctx
;
2911 const struct clk_register_clk_parent_data_test_case
*test_param
;
2912 struct clk_hw
*parent_hw
;
2913 struct clk_init_data init
= { };
2914 struct clk
*expected_parent
, *actual_parent
;
2916 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
2917 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
);
2920 clk_register_clk_parent_data_device_driver(test
);
2922 expected_parent
= clk_get_kunit(test
, ctx
->dev
, "50");
2923 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, expected_parent
);
2925 test_param
= test
->param_value
;
2926 init
.parent_data
= &test_param
->pdata
;
2927 init
.num_parents
= 1;
2928 init
.name
= "parent_data_device_test_clk";
2929 init
.ops
= &clk_dummy_single_parent_ops
;
2930 ctx
->hw
.init
= &init
;
2931 KUNIT_ASSERT_EQ(test
, 0, clk_hw_register_kunit(test
, ctx
->dev
, &ctx
->hw
));
2933 parent_hw
= clk_hw_get_parent(&ctx
->hw
);
2934 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent_hw
);
2936 actual_parent
= clk_hw_get_clk_kunit(test
, parent_hw
, __func__
);
2937 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, actual_parent
);
2939 KUNIT_EXPECT_TRUE(test
, clk_is_match(expected_parent
, actual_parent
));
2942 static const struct clk_register_clk_parent_data_test_case
2943 clk_register_clk_parent_data_device_hw_cases
[] = {
2946 * Test that a clk registered with a struct device can find a
2947 * parent based on struct clk_parent_data::hw.
2949 .desc
= "clk_parent_data_device_hw_index_test",
2950 /* The index must be negative to indicate firmware not used */
2955 * Test that a clk registered with a struct device can find a
2956 * parent based on struct clk_parent_data::hw when
2957 * struct clk_parent_data::fw_name is set.
2959 .desc
= "clk_parent_data_device_hw_fwname_test",
2960 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT2
,
2964 * Test that a clk registered with a struct device can find a
2965 * parent based on struct clk_parent_data::hw when struct
2966 * clk_parent_data::name is set.
2968 .desc
= "clk_parent_data_device_hw_name_test",
2969 /* The index must be negative to indicate firmware not used */
2971 .pdata
.name
= CLK_PARENT_DATA_50MHZ_NAME
,
2975 * Test that a clk registered with a struct device can find a
2976 * parent based on struct clk_parent_data::hw when struct
2977 * clk_parent_data::{fw_name,name} are set.
2979 .desc
= "clk_parent_data_device_hw_fwname_name_test",
2980 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT2
,
2981 .pdata
.name
= "not_matching",
2985 * Test that a clk registered with a struct device can find a
2986 * parent based on struct clk_parent_data::hw when struct
2987 * clk_parent_data::index is set. The hw pointer takes
2990 .desc
= "clk_parent_data_device_hw_index_priority_test",
2995 * Test that a clk registered with a struct device can find a
2996 * parent based on struct clk_parent_data::hw when
2997 * struct clk_parent_data::{index,fwname,name} are set.
2998 * The hw pointer takes priority over everything else.
3000 .desc
= "clk_parent_data_device_hw_index_fwname_name_priority_test",
3002 .pdata
.fw_name
= CLK_PARENT_DATA_PARENT2
,
3003 .pdata
.name
= "not_matching",
3007 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_hw_test
,
3008 clk_register_clk_parent_data_device_hw_cases
,
3009 clk_register_clk_parent_data_test_case_to_desc
)
3012 * Test that a clk registered with a struct device can find a
3013 * parent based on struct clk_parent_data::hw.
3015 static void clk_register_clk_parent_data_device_hw_test(struct kunit
*test
)
3017 struct clk_register_clk_parent_data_device_ctx
*ctx
;
3018 const struct clk_register_clk_parent_data_test_case
*test_param
;
3019 struct clk_dummy_context
*parent
;
3020 struct clk_hw
*parent_hw
;
3021 struct clk_parent_data pdata
= { };
3022 struct clk_init_data init
= { };
3024 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
3025 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
);
3028 clk_register_clk_parent_data_device_driver(test
);
3030 parent
= kunit_kzalloc(test
, sizeof(*parent
), GFP_KERNEL
);
3031 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, parent
);
3033 parent_hw
= &parent
->hw
;
3034 parent_hw
->init
= CLK_HW_INIT_NO_PARENT("parent-clk",
3035 &clk_dummy_rate_ops
, 0);
3037 KUNIT_ASSERT_EQ(test
, 0, clk_hw_register_kunit(test
, ctx
->dev
, parent_hw
));
3039 test_param
= test
->param_value
;
3040 memcpy(&pdata
, &test_param
->pdata
, sizeof(pdata
));
3041 pdata
.hw
= parent_hw
;
3042 init
.parent_data
= &pdata
;
3043 init
.num_parents
= 1;
3044 init
.ops
= &clk_dummy_single_parent_ops
;
3045 init
.name
= "parent_data_device_hw_test_clk";
3046 ctx
->hw
.init
= &init
;
3047 KUNIT_ASSERT_EQ(test
, 0, clk_hw_register_kunit(test
, ctx
->dev
, &ctx
->hw
));
3049 KUNIT_EXPECT_PTR_EQ(test
, parent_hw
, clk_hw_get_parent(&ctx
->hw
));
3052 static struct kunit_case clk_register_clk_parent_data_device_test_cases
[] = {
3053 KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_test
,
3054 clk_register_clk_parent_data_device_test_gen_params
),
3055 KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_hw_test
,
3056 clk_register_clk_parent_data_device_hw_test_gen_params
),
3060 static int clk_register_clk_parent_data_device_init(struct kunit
*test
)
3062 KUNIT_ASSERT_EQ(test
, 0,
3063 of_overlay_apply_kunit(test
, kunit_clk_parent_data_test
));
3069 * Test suite for registering clks with struct clk_parent_data and a struct
3072 static struct kunit_suite clk_register_clk_parent_data_device_suite
= {
3073 .name
= "clk_register_clk_parent_data_device",
3074 .init
= clk_register_clk_parent_data_device_init
,
3075 .test_cases
= clk_register_clk_parent_data_device_test_cases
,
3079 &clk_leaf_mux_set_rate_parent_test_suite
,
3081 &clk_multiple_parents_mux_test_suite
,
3082 &clk_mux_no_reparent_test_suite
,
3083 &clk_mux_notifier_test_suite
,
3084 &clk_orphan_transparent_multiple_parent_mux_test_suite
,
3085 &clk_orphan_transparent_single_parent_test_suite
,
3086 &clk_orphan_two_level_root_last_test_suite
,
3087 &clk_range_test_suite
,
3088 &clk_range_maximize_test_suite
,
3089 &clk_range_minimize_test_suite
,
3090 &clk_register_clk_parent_data_of_suite
,
3091 &clk_register_clk_parent_data_device_suite
,
3092 &clk_single_parent_mux_test_suite
,
3093 &clk_uncached_test_suite
,
3095 MODULE_DESCRIPTION("Kunit tests for clk framework");
3096 MODULE_LICENSE("GPL v2");