Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / clk / clk_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kunit test for clk rate management
4  */
5 #include <linux/clk.h>
6 #include <linux/clk-provider.h>
7
8 /* Needed for clk_hw_get_clk() */
9 #include "clk.h"
10
11 #include <kunit/test.h>
12
13 #define DUMMY_CLOCK_INIT_RATE   (42 * 1000 * 1000)
14 #define DUMMY_CLOCK_RATE_1      (142 * 1000 * 1000)
15 #define DUMMY_CLOCK_RATE_2      (242 * 1000 * 1000)
16
17 struct clk_dummy_context {
18         struct clk_hw hw;
19         unsigned long rate;
20 };
21
22 static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw,
23                                            unsigned long parent_rate)
24 {
25         struct clk_dummy_context *ctx =
26                 container_of(hw, struct clk_dummy_context, hw);
27
28         return ctx->rate;
29 }
30
31 static int clk_dummy_determine_rate(struct clk_hw *hw,
32                                     struct clk_rate_request *req)
33 {
34         /* Just return the same rate without modifying it */
35         return 0;
36 }
37
38 static int clk_dummy_maximize_rate(struct clk_hw *hw,
39                                    struct clk_rate_request *req)
40 {
41         /*
42          * If there's a maximum set, always run the clock at the maximum
43          * allowed.
44          */
45         if (req->max_rate < ULONG_MAX)
46                 req->rate = req->max_rate;
47
48         return 0;
49 }
50
51 static int clk_dummy_minimize_rate(struct clk_hw *hw,
52                                    struct clk_rate_request *req)
53 {
54         /*
55          * If there's a minimum set, always run the clock at the minimum
56          * allowed.
57          */
58         if (req->min_rate > 0)
59                 req->rate = req->min_rate;
60
61         return 0;
62 }
63
64 static int clk_dummy_set_rate(struct clk_hw *hw,
65                               unsigned long rate,
66                               unsigned long parent_rate)
67 {
68         struct clk_dummy_context *ctx =
69                 container_of(hw, struct clk_dummy_context, hw);
70
71         ctx->rate = rate;
72         return 0;
73 }
74
75 static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
76 {
77         if (index >= clk_hw_get_num_parents(hw))
78                 return -EINVAL;
79
80         return 0;
81 }
82
83 static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
84 {
85         return 0;
86 }
87
88 static const struct clk_ops clk_dummy_rate_ops = {
89         .recalc_rate = clk_dummy_recalc_rate,
90         .determine_rate = clk_dummy_determine_rate,
91         .set_rate = clk_dummy_set_rate,
92 };
93
94 static const struct clk_ops clk_dummy_maximize_rate_ops = {
95         .recalc_rate = clk_dummy_recalc_rate,
96         .determine_rate = clk_dummy_maximize_rate,
97         .set_rate = clk_dummy_set_rate,
98 };
99
100 static const struct clk_ops clk_dummy_minimize_rate_ops = {
101         .recalc_rate = clk_dummy_recalc_rate,
102         .determine_rate = clk_dummy_minimize_rate,
103         .set_rate = clk_dummy_set_rate,
104 };
105
106 static const struct clk_ops clk_dummy_single_parent_ops = {
107         .set_parent = clk_dummy_single_set_parent,
108         .get_parent = clk_dummy_single_get_parent,
109 };
110
111 struct clk_multiple_parent_ctx {
112         struct clk_dummy_context parents_ctx[2];
113         struct clk_hw hw;
114         u8 current_parent;
115 };
116
117 static int clk_multiple_parents_mux_set_parent(struct clk_hw *hw, u8 index)
118 {
119         struct clk_multiple_parent_ctx *ctx =
120                 container_of(hw, struct clk_multiple_parent_ctx, hw);
121
122         if (index >= clk_hw_get_num_parents(hw))
123                 return -EINVAL;
124
125         ctx->current_parent = index;
126
127         return 0;
128 }
129
130 static u8 clk_multiple_parents_mux_get_parent(struct clk_hw *hw)
131 {
132         struct clk_multiple_parent_ctx *ctx =
133                 container_of(hw, struct clk_multiple_parent_ctx, hw);
134
135         return ctx->current_parent;
136 }
137
138 static const struct clk_ops clk_multiple_parents_mux_ops = {
139         .get_parent = clk_multiple_parents_mux_get_parent,
140         .set_parent = clk_multiple_parents_mux_set_parent,
141         .determine_rate = __clk_mux_determine_rate_closest,
142 };
143
144 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
145 {
146         struct clk_dummy_context *ctx;
147         struct clk_init_data init = { };
148         int ret;
149
150         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
151         if (!ctx)
152                 return -ENOMEM;
153         ctx->rate = DUMMY_CLOCK_INIT_RATE;
154         test->priv = ctx;
155
156         init.name = "test_dummy_rate";
157         init.ops = ops;
158         ctx->hw.init = &init;
159
160         ret = clk_hw_register(NULL, &ctx->hw);
161         if (ret)
162                 return ret;
163
164         return 0;
165 }
166
167 static int clk_test_init(struct kunit *test)
168 {
169         return clk_test_init_with_ops(test, &clk_dummy_rate_ops);
170 }
171
172 static int clk_maximize_test_init(struct kunit *test)
173 {
174         return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops);
175 }
176
177 static int clk_minimize_test_init(struct kunit *test)
178 {
179         return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops);
180 }
181
182 static void clk_test_exit(struct kunit *test)
183 {
184         struct clk_dummy_context *ctx = test->priv;
185
186         clk_hw_unregister(&ctx->hw);
187 }
188
189 /*
190  * Test that the actual rate matches what is returned by clk_get_rate()
191  */
192 static void clk_test_get_rate(struct kunit *test)
193 {
194         struct clk_dummy_context *ctx = test->priv;
195         struct clk_hw *hw = &ctx->hw;
196         struct clk *clk = hw->clk;
197         unsigned long rate;
198
199         rate = clk_get_rate(clk);
200         KUNIT_ASSERT_GT(test, rate, 0);
201         KUNIT_EXPECT_EQ(test, rate, ctx->rate);
202 }
203
204 /*
205  * Test that, after a call to clk_set_rate(), the rate returned by
206  * clk_get_rate() matches.
207  *
208  * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
209  * modify the requested rate, which is our case in clk_dummy_rate_ops.
210  */
211 static void clk_test_set_get_rate(struct kunit *test)
212 {
213         struct clk_dummy_context *ctx = test->priv;
214         struct clk_hw *hw = &ctx->hw;
215         struct clk *clk = hw->clk;
216         unsigned long rate;
217
218         KUNIT_ASSERT_EQ(test,
219                         clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
220                         0);
221
222         rate = clk_get_rate(clk);
223         KUNIT_ASSERT_GT(test, rate, 0);
224         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
225 }
226
227 /*
228  * Test that, after several calls to clk_set_rate(), the rate returned
229  * by clk_get_rate() matches the last one.
230  *
231  * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
232  * modify the requested rate, which is our case in clk_dummy_rate_ops.
233  */
234 static void clk_test_set_set_get_rate(struct kunit *test)
235 {
236         struct clk_dummy_context *ctx = test->priv;
237         struct clk_hw *hw = &ctx->hw;
238         struct clk *clk = hw->clk;
239         unsigned long rate;
240
241         KUNIT_ASSERT_EQ(test,
242                         clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
243                         0);
244
245         KUNIT_ASSERT_EQ(test,
246                         clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
247                         0);
248
249         rate = clk_get_rate(clk);
250         KUNIT_ASSERT_GT(test, rate, 0);
251         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
252 }
253
254 /*
255  * Test that clk_round_rate and clk_set_rate are consitent and will
256  * return the same frequency.
257  */
258 static void clk_test_round_set_get_rate(struct kunit *test)
259 {
260         struct clk_dummy_context *ctx = test->priv;
261         struct clk_hw *hw = &ctx->hw;
262         struct clk *clk = hw->clk;
263         unsigned long rounded_rate, set_rate;
264
265         rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
266         KUNIT_ASSERT_GT(test, rounded_rate, 0);
267         KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
268
269         KUNIT_ASSERT_EQ(test,
270                         clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
271                         0);
272
273         set_rate = clk_get_rate(clk);
274         KUNIT_ASSERT_GT(test, set_rate, 0);
275         KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
276 }
277
278 static struct kunit_case clk_test_cases[] = {
279         KUNIT_CASE(clk_test_get_rate),
280         KUNIT_CASE(clk_test_set_get_rate),
281         KUNIT_CASE(clk_test_set_set_get_rate),
282         KUNIT_CASE(clk_test_round_set_get_rate),
283         {}
284 };
285
286 /*
287  * Test suite for a basic rate clock, without any parent.
288  *
289  * These tests are supposed to exercise the rate API with simple scenarios
290  */
291 static struct kunit_suite clk_test_suite = {
292         .name = "clk-test",
293         .init = clk_test_init,
294         .exit = clk_test_exit,
295         .test_cases = clk_test_cases,
296 };
297
298 static int clk_uncached_test_init(struct kunit *test)
299 {
300         struct clk_dummy_context *ctx;
301         int ret;
302
303         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
304         if (!ctx)
305                 return -ENOMEM;
306         test->priv = ctx;
307
308         ctx->rate = DUMMY_CLOCK_INIT_RATE;
309         ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
310                                              &clk_dummy_rate_ops,
311                                              CLK_GET_RATE_NOCACHE);
312
313         ret = clk_hw_register(NULL, &ctx->hw);
314         if (ret)
315                 return ret;
316
317         return 0;
318 }
319
320 /*
321  * Test that for an uncached clock, the clock framework doesn't cache
322  * the rate and clk_get_rate() will return the underlying clock rate
323  * even if it changed.
324  */
325 static void clk_test_uncached_get_rate(struct kunit *test)
326 {
327         struct clk_dummy_context *ctx = test->priv;
328         struct clk_hw *hw = &ctx->hw;
329         struct clk *clk = hw->clk;
330         unsigned long rate;
331
332         rate = clk_get_rate(clk);
333         KUNIT_ASSERT_GT(test, rate, 0);
334         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
335
336         ctx->rate = DUMMY_CLOCK_RATE_1;
337         rate = clk_get_rate(clk);
338         KUNIT_ASSERT_GT(test, rate, 0);
339         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
340 }
341
342 /*
343  * Test that for an uncached clock, clk_set_rate_range() will work
344  * properly if the rate hasn't changed.
345  */
346 static void clk_test_uncached_set_range(struct kunit *test)
347 {
348         struct clk_dummy_context *ctx = test->priv;
349         struct clk_hw *hw = &ctx->hw;
350         struct clk *clk = hw->clk;
351         unsigned long rate;
352
353         KUNIT_ASSERT_EQ(test,
354                         clk_set_rate_range(clk,
355                                            DUMMY_CLOCK_RATE_1,
356                                            DUMMY_CLOCK_RATE_2),
357                         0);
358
359         rate = clk_get_rate(clk);
360         KUNIT_ASSERT_GT(test, rate, 0);
361         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
362         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
363 }
364
365 /*
366  * Test that for an uncached clock, clk_set_rate_range() will work
367  * properly if the rate has changed in hardware.
368  *
369  * In this case, it means that if the rate wasn't initially in the range
370  * we're trying to set, but got changed at some point into the range
371  * without the kernel knowing about it, its rate shouldn't be affected.
372  */
373 static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
374 {
375         struct clk_dummy_context *ctx = test->priv;
376         struct clk_hw *hw = &ctx->hw;
377         struct clk *clk = hw->clk;
378         unsigned long rate;
379
380         ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
381         KUNIT_ASSERT_EQ(test,
382                         clk_set_rate_range(clk,
383                                            DUMMY_CLOCK_RATE_1,
384                                            DUMMY_CLOCK_RATE_2),
385                         0);
386
387         rate = clk_get_rate(clk);
388         KUNIT_ASSERT_GT(test, rate, 0);
389         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
390 }
391
392 static struct kunit_case clk_uncached_test_cases[] = {
393         KUNIT_CASE(clk_test_uncached_get_rate),
394         KUNIT_CASE(clk_test_uncached_set_range),
395         KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
396         {}
397 };
398
399 /*
400  * Test suite for a basic, uncached, rate clock, without any parent.
401  *
402  * These tests are supposed to exercise the rate API with simple scenarios
403  */
404 static struct kunit_suite clk_uncached_test_suite = {
405         .name = "clk-uncached-test",
406         .init = clk_uncached_test_init,
407         .exit = clk_test_exit,
408         .test_cases = clk_uncached_test_cases,
409 };
410
411 static int
412 clk_multiple_parents_mux_test_init(struct kunit *test)
413 {
414         struct clk_multiple_parent_ctx *ctx;
415         const char *parents[2] = { "parent-0", "parent-1"};
416         int ret;
417
418         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
419         if (!ctx)
420                 return -ENOMEM;
421         test->priv = ctx;
422
423         ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
424                                                             &clk_dummy_rate_ops,
425                                                             0);
426         ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
427         ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
428         if (ret)
429                 return ret;
430
431         ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
432                                                             &clk_dummy_rate_ops,
433                                                             0);
434         ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
435         ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
436         if (ret)
437                 return ret;
438
439         ctx->current_parent = 0;
440         ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
441                                            &clk_multiple_parents_mux_ops,
442                                            CLK_SET_RATE_PARENT);
443         ret = clk_hw_register(NULL, &ctx->hw);
444         if (ret)
445                 return ret;
446
447         return 0;
448 }
449
450 static void
451 clk_multiple_parents_mux_test_exit(struct kunit *test)
452 {
453         struct clk_multiple_parent_ctx *ctx = test->priv;
454
455         clk_hw_unregister(&ctx->hw);
456         clk_hw_unregister(&ctx->parents_ctx[0].hw);
457         clk_hw_unregister(&ctx->parents_ctx[1].hw);
458 }
459
460 /*
461  * Test that for a clock with multiple parents, clk_get_parent()
462  * actually returns the current one.
463  */
464 static void
465 clk_test_multiple_parents_mux_get_parent(struct kunit *test)
466 {
467         struct clk_multiple_parent_ctx *ctx = test->priv;
468         struct clk_hw *hw = &ctx->hw;
469         struct clk *clk = hw->clk;
470         struct clk *parent;
471
472         parent = clk_get_parent(clk);
473         KUNIT_EXPECT_TRUE(test, clk_is_match(parent, ctx->parents_ctx[0].hw.clk));
474 }
475
476 /*
477  * Test that for a clock with a multiple parents, clk_has_parent()
478  * actually reports all of them as parents.
479  */
480 static void
481 clk_test_multiple_parents_mux_has_parent(struct kunit *test)
482 {
483         struct clk_multiple_parent_ctx *ctx = test->priv;
484         struct clk_hw *hw = &ctx->hw;
485         struct clk *clk = hw->clk;
486
487         KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, ctx->parents_ctx[0].hw.clk));
488         KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, ctx->parents_ctx[1].hw.clk));
489 }
490
491 /*
492  * Test that for a clock with a multiple parents, if we set a range on
493  * that clock and the parent is changed, its rate after the reparenting
494  * is still within the range we asked for.
495  *
496  * FIXME: clk_set_parent() only does the reparenting but doesn't
497  * reevaluate whether the new clock rate is within its boundaries or
498  * not.
499  */
500 static void
501 clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
502 {
503         struct clk_multiple_parent_ctx *ctx = test->priv;
504         struct clk_hw *hw = &ctx->hw;
505         struct clk *clk = hw->clk;
506         struct clk *parent1, *parent2;
507         unsigned long rate;
508         int ret;
509
510         kunit_skip(test, "This needs to be fixed in the core.");
511
512         parent1 = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
513         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent1);
514         KUNIT_ASSERT_TRUE(test, clk_is_match(clk_get_parent(clk), parent1));
515
516         parent2 = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
517         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent2);
518
519         ret = clk_set_rate(parent1, DUMMY_CLOCK_RATE_1);
520         KUNIT_ASSERT_EQ(test, ret, 0);
521
522         ret = clk_set_rate(parent2, DUMMY_CLOCK_RATE_2);
523         KUNIT_ASSERT_EQ(test, ret, 0);
524
525         ret = clk_set_rate_range(clk,
526                                  DUMMY_CLOCK_RATE_1 - 1000,
527                                  DUMMY_CLOCK_RATE_1 + 1000);
528         KUNIT_ASSERT_EQ(test, ret, 0);
529
530         ret = clk_set_parent(clk, parent2);
531         KUNIT_ASSERT_EQ(test, ret, 0);
532
533         rate = clk_get_rate(clk);
534         KUNIT_ASSERT_GT(test, rate, 0);
535         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
536         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
537
538         clk_put(parent2);
539         clk_put(parent1);
540 }
541
542 static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
543         KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
544         KUNIT_CASE(clk_test_multiple_parents_mux_has_parent),
545         KUNIT_CASE(clk_test_multiple_parents_mux_set_range_set_parent_get_rate),
546         {}
547 };
548
549 /*
550  * Test suite for a basic mux clock with two parents, with
551  * CLK_SET_RATE_PARENT on the child.
552  *
553  * These tests are supposed to exercise the consumer API and check that
554  * the state of the child and parents are sane and consistent.
555  */
556 static struct kunit_suite
557 clk_multiple_parents_mux_test_suite = {
558         .name = "clk-multiple-parents-mux-test",
559         .init = clk_multiple_parents_mux_test_init,
560         .exit = clk_multiple_parents_mux_test_exit,
561         .test_cases = clk_multiple_parents_mux_test_cases,
562 };
563
564 static int
565 clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test)
566 {
567         struct clk_multiple_parent_ctx *ctx;
568         const char *parents[2] = { "missing-parent", "proper-parent"};
569         int ret;
570
571         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
572         if (!ctx)
573                 return -ENOMEM;
574         test->priv = ctx;
575
576         ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("proper-parent",
577                                                             &clk_dummy_rate_ops,
578                                                             0);
579         ctx->parents_ctx[1].rate = DUMMY_CLOCK_INIT_RATE;
580         ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
581         if (ret)
582                 return ret;
583
584         ctx->hw.init = CLK_HW_INIT_PARENTS("test-orphan-mux", parents,
585                                            &clk_multiple_parents_mux_ops,
586                                            CLK_SET_RATE_PARENT);
587         ret = clk_hw_register(NULL, &ctx->hw);
588         if (ret)
589                 return ret;
590
591         return 0;
592 }
593
594 static void
595 clk_orphan_transparent_multiple_parent_mux_test_exit(struct kunit *test)
596 {
597         struct clk_multiple_parent_ctx *ctx = test->priv;
598
599         clk_hw_unregister(&ctx->hw);
600         clk_hw_unregister(&ctx->parents_ctx[1].hw);
601 }
602
603 /*
604  * Test that, for a mux whose current parent hasn't been registered yet,
605  * clk_get_parent() will return NULL.
606  */
607 static void
608 clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit *test)
609 {
610         struct clk_multiple_parent_ctx *ctx = test->priv;
611         struct clk_hw *hw = &ctx->hw;
612         struct clk *clk = hw->clk;
613         struct clk *parent;
614
615         parent = clk_get_parent(clk);
616         KUNIT_EXPECT_PTR_EQ(test, parent, NULL);
617 }
618
619 /*
620  * Test that, for a mux whose current parent hasn't been registered yet,
621  * calling clk_set_parent() to a valid parent will properly update the
622  * mux parent and its orphan status.
623  */
624 static void
625 clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit *test)
626 {
627         struct clk_multiple_parent_ctx *ctx = test->priv;
628         struct clk_hw *hw = &ctx->hw;
629         struct clk *clk = hw->clk;
630         struct clk *parent, *new_parent;
631         int ret;
632
633         parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
634         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
635
636         ret = clk_set_parent(clk, parent);
637         KUNIT_ASSERT_EQ(test, ret, 0);
638
639         new_parent = clk_get_parent(clk);
640         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
641         KUNIT_EXPECT_TRUE(test, clk_is_match(parent, new_parent));
642
643         clk_put(parent);
644 }
645
646 /*
647  * Test that, for a mux that started orphan but got switched to a valid
648  * parent, calling clk_drop_range() on the mux won't affect the parent
649  * rate.
650  */
651 static void
652 clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit *test)
653 {
654         struct clk_multiple_parent_ctx *ctx = test->priv;
655         struct clk_hw *hw = &ctx->hw;
656         struct clk *clk = hw->clk, *parent;
657         unsigned long parent_rate, new_parent_rate;
658         int ret;
659
660         parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
661         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
662
663         parent_rate = clk_get_rate(parent);
664         KUNIT_ASSERT_GT(test, parent_rate, 0);
665
666         ret = clk_set_parent(clk, parent);
667         KUNIT_ASSERT_EQ(test, ret, 0);
668
669         ret = clk_drop_range(clk);
670         KUNIT_ASSERT_EQ(test, ret, 0);
671
672         new_parent_rate = clk_get_rate(clk);
673         KUNIT_ASSERT_GT(test, new_parent_rate, 0);
674         KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
675
676         clk_put(parent);
677 }
678
679 /*
680  * Test that, for a mux that started orphan but got switched to a valid
681  * parent, the rate of the mux and its new parent are consistent.
682  */
683 static void
684 clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit *test)
685 {
686         struct clk_multiple_parent_ctx *ctx = test->priv;
687         struct clk_hw *hw = &ctx->hw;
688         struct clk *clk = hw->clk;
689         struct clk *parent;
690         unsigned long parent_rate, rate;
691         int ret;
692
693         parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
694         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
695
696         parent_rate = clk_get_rate(parent);
697         KUNIT_ASSERT_GT(test, parent_rate, 0);
698
699         ret = clk_set_parent(clk, parent);
700         KUNIT_ASSERT_EQ(test, ret, 0);
701
702         rate = clk_get_rate(clk);
703         KUNIT_ASSERT_GT(test, rate, 0);
704         KUNIT_EXPECT_EQ(test, parent_rate, rate);
705 }
706
707 /*
708  * Test that, for a mux that started orphan but got switched to a valid
709  * parent, calling clk_put() on the mux won't affect the parent rate.
710  */
711 static void
712 clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit *test)
713 {
714         struct clk_multiple_parent_ctx *ctx = test->priv;
715         struct clk *clk, *parent;
716         unsigned long parent_rate, new_parent_rate;
717         int ret;
718
719         parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
720         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
721
722         clk = clk_hw_get_clk(&ctx->hw, NULL);
723         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
724
725         parent_rate = clk_get_rate(parent);
726         KUNIT_ASSERT_GT(test, parent_rate, 0);
727
728         ret = clk_set_parent(clk, parent);
729         KUNIT_ASSERT_EQ(test, ret, 0);
730
731         clk_put(clk);
732
733         new_parent_rate = clk_get_rate(parent);
734         KUNIT_ASSERT_GT(test, new_parent_rate, 0);
735         KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
736
737         clk_put(parent);
738 }
739
740 /*
741  * Test that, for a mux that started orphan but got switched to a valid
742  * parent, calling clk_set_rate_range() will affect the parent state if
743  * its rate is out of range.
744  */
745 static void
746 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit *test)
747 {
748         struct clk_multiple_parent_ctx *ctx = test->priv;
749         struct clk_hw *hw = &ctx->hw;
750         struct clk *clk = hw->clk, *parent;
751         unsigned long rate;
752         int ret;
753
754         parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
755         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
756
757         ret = clk_set_parent(clk, parent);
758         KUNIT_ASSERT_EQ(test, ret, 0);
759
760         ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
761         KUNIT_ASSERT_EQ(test, ret, 0);
762
763         rate = clk_get_rate(clk);
764         KUNIT_ASSERT_GT(test, rate, 0);
765         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
766         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
767
768         clk_put(parent);
769 }
770
771 /*
772  * Test that, for a mux that started orphan but got switched to a valid
773  * parent, calling clk_set_rate_range() won't affect the parent state if
774  * its rate is within range.
775  */
776 static void
777 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit *test)
778 {
779         struct clk_multiple_parent_ctx *ctx = test->priv;
780         struct clk_hw *hw = &ctx->hw;
781         struct clk *clk = hw->clk, *parent;
782         unsigned long parent_rate, new_parent_rate;
783         int ret;
784
785         parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
786         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
787
788         parent_rate = clk_get_rate(parent);
789         KUNIT_ASSERT_GT(test, parent_rate, 0);
790
791         ret = clk_set_parent(clk, parent);
792         KUNIT_ASSERT_EQ(test, ret, 0);
793
794         ret = clk_set_rate_range(clk,
795                                  DUMMY_CLOCK_INIT_RATE - 1000,
796                                  DUMMY_CLOCK_INIT_RATE + 1000);
797         KUNIT_ASSERT_EQ(test, ret, 0);
798
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);
802
803         clk_put(parent);
804 }
805
806 /*
807  * Test that, for a mux whose current parent hasn't been registered yet,
808  * calling clk_set_rate_range() will succeed but won't affect its rate.
809  */
810 static void
811 clk_test_orphan_transparent_multiple_parent_mux_set_range_get_rate(struct kunit *test)
812 {
813         struct clk_multiple_parent_ctx *ctx = test->priv;
814         struct clk_hw *hw = &ctx->hw;
815         struct clk *clk = hw->clk;
816         unsigned long rate;
817         int ret;
818
819         ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
820         KUNIT_ASSERT_EQ(test, ret, 0);
821
822         rate = clk_get_rate(clk);
823         KUNIT_EXPECT_EQ(test, rate, 0);
824 }
825
826 /*
827  * Test that, for a mux whose current parent hasn't been registered yet,
828  * calling clk_set_rate_range() will succeed, and will be taken into
829  * account when rounding a rate.
830  */
831 static void
832 clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit *test)
833 {
834         struct clk_multiple_parent_ctx *ctx = test->priv;
835         struct clk_hw *hw = &ctx->hw;
836         struct clk *clk = hw->clk;
837         unsigned long rate;
838         int ret;
839
840         ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
841         KUNIT_ASSERT_EQ(test, ret, 0);
842
843         rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
844         KUNIT_ASSERT_GT(test, rate, 0);
845         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
846         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
847 }
848
849 /*
850  * Test that, for a mux that started orphan, was assigned and rate and
851  * then got switched to a valid parent, its rate is eventually within
852  * range.
853  *
854  * FIXME: Even though we update the rate as part of clk_set_parent(), we
855  * don't evaluate whether that new rate is within range and needs to be
856  * adjusted.
857  */
858 static void
859 clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit *test)
860 {
861         struct clk_multiple_parent_ctx *ctx = test->priv;
862         struct clk_hw *hw = &ctx->hw;
863         struct clk *clk = hw->clk, *parent;
864         unsigned long rate;
865         int ret;
866
867         kunit_skip(test, "This needs to be fixed in the core.");
868
869         parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
870         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
871
872         ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
873         KUNIT_ASSERT_EQ(test, ret, 0);
874
875         ret = clk_set_parent(clk, parent);
876         KUNIT_ASSERT_EQ(test, ret, 0);
877
878         rate = clk_get_rate(clk);
879         KUNIT_ASSERT_GT(test, rate, 0);
880         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
881         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
882
883         clk_put(parent);
884 }
885
886 static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = {
887         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_get_parent),
888         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent),
889         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range),
890         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate),
891         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_put),
892         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified),
893         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched),
894         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_get_rate),
895         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate),
896         KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate),
897         {}
898 };
899
900 /*
901  * Test suite for a basic mux clock with two parents. The default parent
902  * isn't registered, only the second parent is. By default, the clock
903  * will thus be orphan.
904  *
905  * These tests are supposed to exercise the behaviour of the consumer
906  * API when dealing with an orphan clock, and how we deal with the
907  * transition to a valid parent.
908  */
909 static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = {
910         .name = "clk-orphan-transparent-multiple-parent-mux-test",
911         .init = clk_orphan_transparent_multiple_parent_mux_test_init,
912         .exit = clk_orphan_transparent_multiple_parent_mux_test_exit,
913         .test_cases = clk_orphan_transparent_multiple_parent_mux_test_cases,
914 };
915
916 struct clk_single_parent_ctx {
917         struct clk_dummy_context parent_ctx;
918         struct clk_hw hw;
919 };
920
921 static int clk_single_parent_mux_test_init(struct kunit *test)
922 {
923         struct clk_single_parent_ctx *ctx;
924         int ret;
925
926         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
927         if (!ctx)
928                 return -ENOMEM;
929         test->priv = ctx;
930
931         ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
932         ctx->parent_ctx.hw.init =
933                 CLK_HW_INIT_NO_PARENT("parent-clk",
934                                       &clk_dummy_rate_ops,
935                                       0);
936
937         ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
938         if (ret)
939                 return ret;
940
941         ctx->hw.init = CLK_HW_INIT("test-clk", "parent-clk",
942                                    &clk_dummy_single_parent_ops,
943                                    CLK_SET_RATE_PARENT);
944
945         ret = clk_hw_register(NULL, &ctx->hw);
946         if (ret)
947                 return ret;
948
949         return 0;
950 }
951
952 static void
953 clk_single_parent_mux_test_exit(struct kunit *test)
954 {
955         struct clk_single_parent_ctx *ctx = test->priv;
956
957         clk_hw_unregister(&ctx->hw);
958         clk_hw_unregister(&ctx->parent_ctx.hw);
959 }
960
961 /*
962  * Test that for a clock with a single parent, clk_get_parent() actually
963  * returns the parent.
964  */
965 static void
966 clk_test_single_parent_mux_get_parent(struct kunit *test)
967 {
968         struct clk_single_parent_ctx *ctx = test->priv;
969         struct clk_hw *hw = &ctx->hw;
970         struct clk *clk = hw->clk;
971         struct clk *parent;
972
973         parent = clk_get_parent(clk);
974         KUNIT_EXPECT_TRUE(test, clk_is_match(parent, ctx->parent_ctx.hw.clk));
975 }
976
977 /*
978  * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
979  * if we set a range on both the child clock and its parent, with a
980  * smaller range on the child, the rate range returned by
981  * clk_get_rate_range() is aggregate of both ranges.
982  */
983 static void
984 clk_test_single_parent_mux_get_range_both_child_smaller(struct kunit *test)
985 {
986         struct clk_single_parent_ctx *ctx = test->priv;
987         struct clk_hw *hw = &ctx->hw;
988         struct clk *clk = hw->clk;
989         struct clk *parent;
990         unsigned long min, max;
991         int ret;
992
993         parent = clk_get_parent(clk);
994         KUNIT_ASSERT_PTR_NE(test, parent, NULL);
995
996         ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
997         KUNIT_ASSERT_EQ(test, ret, 0);
998
999         ret = clk_set_rate_range(clk,
1000                                  DUMMY_CLOCK_RATE_1 + 1000,
1001                                  DUMMY_CLOCK_RATE_2 - 1000);
1002         KUNIT_ASSERT_EQ(test, ret, 0);
1003
1004         clk_get_rate_range(clk, &min, &max);
1005         KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
1006         KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2 - 1000);
1007 }
1008
1009 /*
1010  * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
1011  * if we set a range on both the child clock and its parent, with a
1012  * smaller range on the parent, the rate range returned by
1013  * clk_get_rate_range() is aggregate of both ranges.
1014  *
1015  * FIXME: clk_get_rate_range() (and clk_core_get_boundaries() in
1016  * particular) doesn't take the parent range into account when the clock
1017  * has CLK_SET_RATE_PARENT.
1018  */
1019 static void
1020 clk_test_single_parent_mux_get_range_both_parent_smaller(struct kunit *test)
1021 {
1022         struct clk_single_parent_ctx *ctx = test->priv;
1023         struct clk_hw *hw = &ctx->hw;
1024         struct clk *clk = hw->clk;
1025         struct clk *parent;
1026         unsigned long min, max;
1027         int ret;
1028
1029         kunit_skip(test, "This needs to be fixed in the core.");
1030
1031         parent = clk_get_parent(clk);
1032         KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1033
1034         ret = clk_set_rate_range(parent,
1035                                  DUMMY_CLOCK_RATE_1 + 1000,
1036                                  DUMMY_CLOCK_RATE_2 - 1000);
1037         KUNIT_ASSERT_EQ(test, ret, 0);
1038
1039         ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1040         KUNIT_ASSERT_EQ(test, ret, 0);
1041
1042         clk_get_rate_range(clk, &min, &max);
1043         KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
1044         KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_2 - 1000);
1045 }
1046
1047 /*
1048  * Test that for a clock with a single parent and CLK_SET_RATE_PARENT,
1049  * if we set a range on the parent clock only, the rate range returned
1050  * by clk_get_rate_range() on the children clock matches the parent
1051  * range.
1052  *
1053  * FIXME: clk_get_rate_range() (and clk_core_get_boundaries() in
1054  * particular) doesn't take the parent range into account when the clock
1055  * has CLK_SET_RATE_PARENT.
1056  */
1057 static void
1058 clk_test_single_parent_mux_get_range_parent_only(struct kunit *test)
1059 {
1060         struct clk_single_parent_ctx *ctx = test->priv;
1061         struct clk_hw *hw = &ctx->hw;
1062         struct clk *clk = hw->clk;
1063         struct clk *parent;
1064         unsigned long min, max;
1065         int ret;
1066
1067         kunit_skip(test, "This needs to be fixed in the core.");
1068
1069         parent = clk_get_parent(clk);
1070         KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1071
1072         ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1073         KUNIT_ASSERT_EQ(test, ret, 0);
1074
1075         clk_get_rate_range(clk, &min, &max);
1076         KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
1077         KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_2);
1078 }
1079
1080 /*
1081  * Test that for a clock with a single parent, clk_has_parent() actually
1082  * reports it as a parent.
1083  */
1084 static void
1085 clk_test_single_parent_mux_has_parent(struct kunit *test)
1086 {
1087         struct clk_single_parent_ctx *ctx = test->priv;
1088         struct clk_hw *hw = &ctx->hw;
1089         struct clk *clk = hw->clk;
1090         struct clk *parent = ctx->parent_ctx.hw.clk;
1091
1092         KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
1093 }
1094
1095 /*
1096  * Test that for a clock that can't modify its rate and with a single
1097  * parent, if we set disjoints range on the parent and then the child,
1098  * the second will return an error.
1099  *
1100  * FIXME: clk_set_rate_range() only considers the current clock when
1101  * evaluating whether ranges are disjoints and not the upstream clocks
1102  * ranges.
1103  */
1104 static void
1105 clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
1106 {
1107         struct clk_single_parent_ctx *ctx = test->priv;
1108         struct clk_hw *hw = &ctx->hw;
1109         struct clk *clk = hw->clk;
1110         struct clk *parent;
1111         int ret;
1112
1113         kunit_skip(test, "This needs to be fixed in the core.");
1114
1115         parent = clk_get_parent(clk);
1116         KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1117
1118         ret = clk_set_rate_range(parent, 1000, 2000);
1119         KUNIT_ASSERT_EQ(test, ret, 0);
1120
1121         ret = clk_set_rate_range(clk, 3000, 4000);
1122         KUNIT_EXPECT_LT(test, ret, 0);
1123 }
1124
1125 /*
1126  * Test that for a clock that can't modify its rate and with a single
1127  * parent, if we set disjoints range on the child and then the parent,
1128  * the second will return an error.
1129  *
1130  * FIXME: clk_set_rate_range() only considers the current clock when
1131  * evaluating whether ranges are disjoints and not the downstream clocks
1132  * ranges.
1133  */
1134 static void
1135 clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
1136 {
1137         struct clk_single_parent_ctx *ctx = test->priv;
1138         struct clk_hw *hw = &ctx->hw;
1139         struct clk *clk = hw->clk;
1140         struct clk *parent;
1141         int ret;
1142
1143         kunit_skip(test, "This needs to be fixed in the core.");
1144
1145         parent = clk_get_parent(clk);
1146         KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1147
1148         ret = clk_set_rate_range(clk, 1000, 2000);
1149         KUNIT_ASSERT_EQ(test, ret, 0);
1150
1151         ret = clk_set_rate_range(parent, 3000, 4000);
1152         KUNIT_EXPECT_LT(test, ret, 0);
1153 }
1154
1155 /*
1156  * Test that for a clock that can't modify its rate and with a single
1157  * parent, if we set a range on the parent and then call
1158  * clk_round_rate(), the boundaries of the parent are taken into
1159  * account.
1160  */
1161 static void
1162 clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test)
1163 {
1164         struct clk_single_parent_ctx *ctx = test->priv;
1165         struct clk_hw *hw = &ctx->hw;
1166         struct clk *clk = hw->clk;
1167         struct clk *parent;
1168         unsigned long rate;
1169         int ret;
1170
1171         parent = clk_get_parent(clk);
1172         KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1173
1174         ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1175         KUNIT_ASSERT_EQ(test, ret, 0);
1176
1177         rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1178         KUNIT_ASSERT_GT(test, rate, 0);
1179         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1180         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1181 }
1182
1183 /*
1184  * Test that for a clock that can't modify its rate and with a single
1185  * parent, if we set a range on the parent and a more restrictive one on
1186  * the child, and then call clk_round_rate(), the boundaries of the
1187  * two clocks are taken into account.
1188  */
1189 static void
1190 clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test)
1191 {
1192         struct clk_single_parent_ctx *ctx = test->priv;
1193         struct clk_hw *hw = &ctx->hw;
1194         struct clk *clk = hw->clk;
1195         struct clk *parent;
1196         unsigned long rate;
1197         int ret;
1198
1199         parent = clk_get_parent(clk);
1200         KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1201
1202         ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1203         KUNIT_ASSERT_EQ(test, ret, 0);
1204
1205         ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1206         KUNIT_ASSERT_EQ(test, ret, 0);
1207
1208         rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1209         KUNIT_ASSERT_GT(test, rate, 0);
1210         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1211         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1212
1213         rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1214         KUNIT_ASSERT_GT(test, rate, 0);
1215         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1216         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1217 }
1218
1219 /*
1220  * Test that for a clock that can't modify its rate and with a single
1221  * parent, if we set a range on the child and a more restrictive one on
1222  * the parent, and then call clk_round_rate(), the boundaries of the
1223  * two clocks are taken into account.
1224  */
1225 static void
1226 clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test)
1227 {
1228         struct clk_single_parent_ctx *ctx = test->priv;
1229         struct clk_hw *hw = &ctx->hw;
1230         struct clk *clk = hw->clk;
1231         struct clk *parent;
1232         unsigned long rate;
1233         int ret;
1234
1235         parent = clk_get_parent(clk);
1236         KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1237
1238         ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1239         KUNIT_ASSERT_EQ(test, ret, 0);
1240
1241         ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1242         KUNIT_ASSERT_EQ(test, ret, 0);
1243
1244         rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1245         KUNIT_ASSERT_GT(test, rate, 0);
1246         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1247         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1248
1249         rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1250         KUNIT_ASSERT_GT(test, rate, 0);
1251         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1252         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1253 }
1254
1255 static struct kunit_case clk_single_parent_mux_test_cases[] = {
1256         KUNIT_CASE(clk_test_single_parent_mux_get_parent),
1257         KUNIT_CASE(clk_test_single_parent_mux_get_range_both_child_smaller),
1258         KUNIT_CASE(clk_test_single_parent_mux_get_range_both_parent_smaller),
1259         KUNIT_CASE(clk_test_single_parent_mux_get_range_parent_only),
1260         KUNIT_CASE(clk_test_single_parent_mux_has_parent),
1261         KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
1262         KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
1263         KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),
1264         KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only),
1265         KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller),
1266         {}
1267 };
1268
1269 /*
1270  * Test suite for a basic mux clock with one parent, with
1271  * CLK_SET_RATE_PARENT on the child.
1272  *
1273  * These tests are supposed to exercise the consumer API and check that
1274  * the state of the child and parent are sane and consistent.
1275  */
1276 static struct kunit_suite
1277 clk_single_parent_mux_test_suite = {
1278         .name = "clk-single-parent-mux-test",
1279         .init = clk_single_parent_mux_test_init,
1280         .exit = clk_single_parent_mux_test_exit,
1281         .test_cases = clk_single_parent_mux_test_cases,
1282 };
1283
1284 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
1285 {
1286         struct clk_single_parent_ctx *ctx;
1287         struct clk_init_data init = { };
1288         const char *parents[] = { "orphan_parent" };
1289         int ret;
1290
1291         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1292         if (!ctx)
1293                 return -ENOMEM;
1294         test->priv = ctx;
1295
1296         init.name = "test_orphan_dummy_parent";
1297         init.ops = &clk_dummy_single_parent_ops;
1298         init.parent_names = parents;
1299         init.num_parents = ARRAY_SIZE(parents);
1300         init.flags = CLK_SET_RATE_PARENT;
1301         ctx->hw.init = &init;
1302
1303         ret = clk_hw_register(NULL, &ctx->hw);
1304         if (ret)
1305                 return ret;
1306
1307         memset(&init, 0, sizeof(init));
1308         init.name = "orphan_parent";
1309         init.ops = &clk_dummy_rate_ops;
1310         ctx->parent_ctx.hw.init = &init;
1311         ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1312
1313         ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1314         if (ret)
1315                 return ret;
1316
1317         return 0;
1318 }
1319
1320 /*
1321  * Test that a mux-only clock, with an initial rate within a range,
1322  * will still have the same rate after the range has been enforced.
1323  *
1324  * See:
1325  * https://lore.kernel.org/linux-clk/7720158d-10a7-a17b-73a4-a8615c9c6d5c@collabora.com/
1326  */
1327 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
1328 {
1329         struct clk_single_parent_ctx *ctx = test->priv;
1330         struct clk_hw *hw = &ctx->hw;
1331         struct clk *clk = hw->clk;
1332         unsigned long rate, new_rate;
1333
1334         rate = clk_get_rate(clk);
1335         KUNIT_ASSERT_GT(test, rate, 0);
1336
1337         KUNIT_ASSERT_EQ(test,
1338                         clk_set_rate_range(clk,
1339                                            ctx->parent_ctx.rate - 1000,
1340                                            ctx->parent_ctx.rate + 1000),
1341                         0);
1342
1343         new_rate = clk_get_rate(clk);
1344         KUNIT_ASSERT_GT(test, new_rate, 0);
1345         KUNIT_EXPECT_EQ(test, rate, new_rate);
1346 }
1347
1348 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
1349         KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
1350         {}
1351 };
1352
1353 /*
1354  * Test suite for a basic mux clock with one parent. The parent is
1355  * registered after its child. The clock will thus be orphan when
1356  * registered, but will no longer be when the tests run.
1357  *
1358  * These tests are supposed to make sure a clock that used to be orphan
1359  * has a sane, consistent, behaviour.
1360  */
1361 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
1362         .name = "clk-orphan-transparent-single-parent-test",
1363         .init = clk_orphan_transparent_single_parent_mux_test_init,
1364         .exit = clk_single_parent_mux_test_exit,
1365         .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
1366 };
1367
1368 struct clk_single_parent_two_lvl_ctx {
1369         struct clk_dummy_context parent_parent_ctx;
1370         struct clk_dummy_context parent_ctx;
1371         struct clk_hw hw;
1372 };
1373
1374 static int
1375 clk_orphan_two_level_root_last_test_init(struct kunit *test)
1376 {
1377         struct clk_single_parent_two_lvl_ctx *ctx;
1378         int ret;
1379
1380         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1381         if (!ctx)
1382                 return -ENOMEM;
1383         test->priv = ctx;
1384
1385         ctx->parent_ctx.hw.init =
1386                 CLK_HW_INIT("intermediate-parent",
1387                             "root-parent",
1388                             &clk_dummy_single_parent_ops,
1389                             CLK_SET_RATE_PARENT);
1390         ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1391         if (ret)
1392                 return ret;
1393
1394         ctx->hw.init =
1395                 CLK_HW_INIT("test-clk", "intermediate-parent",
1396                             &clk_dummy_single_parent_ops,
1397                             CLK_SET_RATE_PARENT);
1398         ret = clk_hw_register(NULL, &ctx->hw);
1399         if (ret)
1400                 return ret;
1401
1402         ctx->parent_parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1403         ctx->parent_parent_ctx.hw.init =
1404                 CLK_HW_INIT_NO_PARENT("root-parent",
1405                                       &clk_dummy_rate_ops,
1406                                       0);
1407         ret = clk_hw_register(NULL, &ctx->parent_parent_ctx.hw);
1408         if (ret)
1409                 return ret;
1410
1411         return 0;
1412 }
1413
1414 static void
1415 clk_orphan_two_level_root_last_test_exit(struct kunit *test)
1416 {
1417         struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1418
1419         clk_hw_unregister(&ctx->hw);
1420         clk_hw_unregister(&ctx->parent_ctx.hw);
1421         clk_hw_unregister(&ctx->parent_parent_ctx.hw);
1422 }
1423
1424 /*
1425  * Test that, for a clock whose parent used to be orphan, clk_get_rate()
1426  * will return the proper rate.
1427  */
1428 static void
1429 clk_orphan_two_level_root_last_test_get_rate(struct kunit *test)
1430 {
1431         struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1432         struct clk_hw *hw = &ctx->hw;
1433         struct clk *clk = hw->clk;
1434         unsigned long rate;
1435
1436         rate = clk_get_rate(clk);
1437         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1438 }
1439
1440 /*
1441  * Test that, for a clock whose parent used to be orphan,
1442  * clk_set_rate_range() won't affect its rate if it is already within
1443  * range.
1444  *
1445  * See (for Exynos 4210):
1446  * https://lore.kernel.org/linux-clk/366a0232-bb4a-c357-6aa8-636e398e05eb@samsung.com/
1447  */
1448 static void
1449 clk_orphan_two_level_root_last_test_set_range(struct kunit *test)
1450 {
1451         struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1452         struct clk_hw *hw = &ctx->hw;
1453         struct clk *clk = hw->clk;
1454         unsigned long rate;
1455         int ret;
1456
1457         ret = clk_set_rate_range(clk,
1458                                  DUMMY_CLOCK_INIT_RATE - 1000,
1459                                  DUMMY_CLOCK_INIT_RATE + 1000);
1460         KUNIT_ASSERT_EQ(test, ret, 0);
1461
1462         rate = clk_get_rate(clk);
1463         KUNIT_ASSERT_GT(test, rate, 0);
1464         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1465 }
1466
1467 static struct kunit_case
1468 clk_orphan_two_level_root_last_test_cases[] = {
1469         KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate),
1470         KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range),
1471         {}
1472 };
1473
1474 /*
1475  * Test suite for a basic, transparent, clock with a parent that is also
1476  * such a clock. The parent's parent is registered last, while the
1477  * parent and its child are registered in that order. The intermediate
1478  * and leaf clocks will thus be orphan when registered, but the leaf
1479  * clock itself will always have its parent and will never be
1480  * reparented. Indeed, it's only orphan because its parent is.
1481  *
1482  * These tests are supposed to exercise the behaviour of the consumer
1483  * API when dealing with an orphan clock, and how we deal with the
1484  * transition to a valid parent.
1485  */
1486 static struct kunit_suite
1487 clk_orphan_two_level_root_last_test_suite = {
1488         .name = "clk-orphan-two-level-root-last-test",
1489         .init = clk_orphan_two_level_root_last_test_init,
1490         .exit = clk_orphan_two_level_root_last_test_exit,
1491         .test_cases = clk_orphan_two_level_root_last_test_cases,
1492 };
1493
1494 /*
1495  * Test that clk_set_rate_range() and clk_get_rate_range() are
1496  * consistent on a simple clock without any parent.
1497  */
1498 static void clk_range_test_get_range(struct kunit *test)
1499 {
1500         struct clk_dummy_context *ctx = test->priv;
1501         struct clk_hw *hw = &ctx->hw;
1502         struct clk *clk = hw->clk;
1503         unsigned long min, max;
1504         int ret;
1505
1506         ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1507         KUNIT_ASSERT_EQ(test, ret, 0);
1508
1509         clk_get_rate_range(clk, &min, &max);
1510         KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
1511         KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2);
1512 }
1513
1514 /*
1515  * Test that, on a simple clock without any parent, if a rate range is
1516  * set on a clk, it's properly reported by clk_get_rate_range() on all
1517  * the clk structure of that clock.
1518  */
1519 static void clk_range_test_get_range_multiple_clk(struct kunit *test)
1520 {
1521         struct clk_dummy_context *ctx = test->priv;
1522         struct clk_hw *hw = &ctx->hw;
1523         struct clk *user1, *user2;
1524         unsigned long min, max;
1525         int ret;
1526
1527         user1 = clk_hw_get_clk(hw, NULL);
1528         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1529
1530         user2 = clk_hw_get_clk(hw, NULL);
1531         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1532
1533         ret = clk_set_rate_range(user1,
1534                                  DUMMY_CLOCK_RATE_1,
1535                                  DUMMY_CLOCK_RATE_2);
1536         KUNIT_ASSERT_EQ(test, ret, 0);
1537
1538         clk_get_rate_range(user2, &min, &max);
1539         KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1);
1540         KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2);
1541 }
1542
1543 /*
1544  * Test that, on a simple clock without any parent, if a rate range is
1545  * set on struct clk_hw, it's properly reported by clk_get_rate_range().
1546  */
1547 static void clk_range_test_get_range_with_hw(struct kunit *test)
1548 {
1549         struct clk_dummy_context *ctx = test->priv;
1550         struct clk_hw *hw = &ctx->hw;
1551         struct clk *clk = hw->clk;
1552         unsigned long min, max;
1553         int ret;
1554
1555         clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1556
1557         ret = clk_set_rate_range(clk,
1558                                  DUMMY_CLOCK_RATE_1 + 1000,
1559                                  DUMMY_CLOCK_RATE_2 - 1000);
1560         KUNIT_ASSERT_EQ(test, ret, 0);
1561
1562         clk_get_rate_range(clk, &min, &max);
1563         KUNIT_EXPECT_EQ(test, min, DUMMY_CLOCK_RATE_1 + 1000);
1564         KUNIT_EXPECT_EQ(test, max, DUMMY_CLOCK_RATE_2 - 1000);
1565 }
1566
1567 /*
1568  * Test that clk_set_rate_range won't return an error for a valid range
1569  * and that it will make sure the rate of the clock is within the
1570  * boundaries.
1571  */
1572 static void clk_range_test_set_range(struct kunit *test)
1573 {
1574         struct clk_dummy_context *ctx = test->priv;
1575         struct clk_hw *hw = &ctx->hw;
1576         struct clk *clk = hw->clk;
1577         unsigned long rate;
1578
1579         KUNIT_ASSERT_EQ(test,
1580                         clk_set_rate_range(clk,
1581                                            DUMMY_CLOCK_RATE_1,
1582                                            DUMMY_CLOCK_RATE_2),
1583                         0);
1584
1585         rate = clk_get_rate(clk);
1586         KUNIT_ASSERT_GT(test, rate, 0);
1587         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1588         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1589 }
1590
1591 /*
1592  * Test that calling clk_set_rate_range with a minimum rate higher than
1593  * the maximum rate returns an error.
1594  */
1595 static void clk_range_test_set_range_invalid(struct kunit *test)
1596 {
1597         struct clk_dummy_context *ctx = test->priv;
1598         struct clk_hw *hw = &ctx->hw;
1599         struct clk *clk = hw->clk;
1600
1601         KUNIT_EXPECT_LT(test,
1602                         clk_set_rate_range(clk,
1603                                            DUMMY_CLOCK_RATE_1 + 1000,
1604                                            DUMMY_CLOCK_RATE_1),
1605                         0);
1606 }
1607
1608 /*
1609  * Test that users can't set multiple, disjoints, range that would be
1610  * impossible to meet.
1611  */
1612 static void clk_range_test_multiple_disjoints_range(struct kunit *test)
1613 {
1614         struct clk_dummy_context *ctx = test->priv;
1615         struct clk_hw *hw = &ctx->hw;
1616         struct clk *user1, *user2;
1617
1618         user1 = clk_hw_get_clk(hw, NULL);
1619         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1620
1621         user2 = clk_hw_get_clk(hw, NULL);
1622         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1623
1624         KUNIT_ASSERT_EQ(test,
1625                         clk_set_rate_range(user1, 1000, 2000),
1626                         0);
1627
1628         KUNIT_EXPECT_LT(test,
1629                         clk_set_rate_range(user2, 3000, 4000),
1630                         0);
1631
1632         clk_put(user2);
1633         clk_put(user1);
1634 }
1635
1636 /*
1637  * Test that if our clock has some boundaries and we try to round a rate
1638  * lower than the minimum, the returned rate will be within range.
1639  */
1640 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
1641 {
1642         struct clk_dummy_context *ctx = test->priv;
1643         struct clk_hw *hw = &ctx->hw;
1644         struct clk *clk = hw->clk;
1645         long rate;
1646
1647         KUNIT_ASSERT_EQ(test,
1648                         clk_set_rate_range(clk,
1649                                            DUMMY_CLOCK_RATE_1,
1650                                            DUMMY_CLOCK_RATE_2),
1651                         0);
1652
1653         rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1654         KUNIT_ASSERT_GT(test, rate, 0);
1655         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1656         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1657 }
1658
1659 /*
1660  * Test that if our clock has some boundaries and we try to set a rate
1661  * higher than the maximum, the new rate will be within range.
1662  */
1663 static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
1664 {
1665         struct clk_dummy_context *ctx = test->priv;
1666         struct clk_hw *hw = &ctx->hw;
1667         struct clk *clk = hw->clk;
1668         unsigned long rate;
1669
1670         KUNIT_ASSERT_EQ(test,
1671                         clk_set_rate_range(clk,
1672                                            DUMMY_CLOCK_RATE_1,
1673                                            DUMMY_CLOCK_RATE_2),
1674                         0);
1675
1676         KUNIT_ASSERT_EQ(test,
1677                         clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1678                         0);
1679
1680         rate = clk_get_rate(clk);
1681         KUNIT_ASSERT_GT(test, rate, 0);
1682         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1683         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1684 }
1685
1686 /*
1687  * Test that if our clock has some boundaries and we try to round and
1688  * set a rate lower than the minimum, the rate returned by
1689  * clk_round_rate() will be consistent with the new rate set by
1690  * clk_set_rate().
1691  */
1692 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
1693 {
1694         struct clk_dummy_context *ctx = test->priv;
1695         struct clk_hw *hw = &ctx->hw;
1696         struct clk *clk = hw->clk;
1697         long rounded;
1698
1699         KUNIT_ASSERT_EQ(test,
1700                         clk_set_rate_range(clk,
1701                                            DUMMY_CLOCK_RATE_1,
1702                                            DUMMY_CLOCK_RATE_2),
1703                         0);
1704
1705         rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1706         KUNIT_ASSERT_GT(test, rounded, 0);
1707
1708         KUNIT_ASSERT_EQ(test,
1709                         clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1710                         0);
1711
1712         KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1713 }
1714
1715 /*
1716  * Test that if our clock has some boundaries and we try to round a rate
1717  * higher than the maximum, the returned rate will be within range.
1718  */
1719 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
1720 {
1721         struct clk_dummy_context *ctx = test->priv;
1722         struct clk_hw *hw = &ctx->hw;
1723         struct clk *clk = hw->clk;
1724         long rate;
1725
1726         KUNIT_ASSERT_EQ(test,
1727                         clk_set_rate_range(clk,
1728                                            DUMMY_CLOCK_RATE_1,
1729                                            DUMMY_CLOCK_RATE_2),
1730                         0);
1731
1732         rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1733         KUNIT_ASSERT_GT(test, rate, 0);
1734         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1735         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1736 }
1737
1738 /*
1739  * Test that if our clock has some boundaries and we try to set a rate
1740  * higher than the maximum, the new rate will be within range.
1741  */
1742 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
1743 {
1744         struct clk_dummy_context *ctx = test->priv;
1745         struct clk_hw *hw = &ctx->hw;
1746         struct clk *clk = hw->clk;
1747         unsigned long rate;
1748
1749         KUNIT_ASSERT_EQ(test,
1750                         clk_set_rate_range(clk,
1751                                            DUMMY_CLOCK_RATE_1,
1752                                            DUMMY_CLOCK_RATE_2),
1753                         0);
1754
1755         KUNIT_ASSERT_EQ(test,
1756                         clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1757                         0);
1758
1759         rate = clk_get_rate(clk);
1760         KUNIT_ASSERT_GT(test, rate, 0);
1761         KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1762         KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1763 }
1764
1765 /*
1766  * Test that if our clock has some boundaries and we try to round and
1767  * set a rate higher than the maximum, the rate returned by
1768  * clk_round_rate() will be consistent with the new rate set by
1769  * clk_set_rate().
1770  */
1771 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
1772 {
1773         struct clk_dummy_context *ctx = test->priv;
1774         struct clk_hw *hw = &ctx->hw;
1775         struct clk *clk = hw->clk;
1776         long rounded;
1777
1778         KUNIT_ASSERT_EQ(test,
1779                         clk_set_rate_range(clk,
1780                                            DUMMY_CLOCK_RATE_1,
1781                                            DUMMY_CLOCK_RATE_2),
1782                         0);
1783
1784         rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1785         KUNIT_ASSERT_GT(test, rounded, 0);
1786
1787         KUNIT_ASSERT_EQ(test,
1788                         clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1789                         0);
1790
1791         KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1792 }
1793
1794 /*
1795  * Test that if our clock has a rate lower than the minimum set by a
1796  * call to clk_set_rate_range(), the rate will be raised to match the
1797  * new minimum.
1798  *
1799  * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1800  * modify the requested rate, which is our case in clk_dummy_rate_ops.
1801  */
1802 static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
1803 {
1804         struct clk_dummy_context *ctx = test->priv;
1805         struct clk_hw *hw = &ctx->hw;
1806         struct clk *clk = hw->clk;
1807         unsigned long rate;
1808
1809         KUNIT_ASSERT_EQ(test,
1810                         clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1811                         0);
1812
1813         KUNIT_ASSERT_EQ(test,
1814                         clk_set_rate_range(clk,
1815                                            DUMMY_CLOCK_RATE_1,
1816                                            DUMMY_CLOCK_RATE_2),
1817                         0);
1818
1819         rate = clk_get_rate(clk);
1820         KUNIT_ASSERT_GT(test, rate, 0);
1821         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1822 }
1823
1824 /*
1825  * Test that if our clock has a rate higher than the maximum set by a
1826  * call to clk_set_rate_range(), the rate will be lowered to match the
1827  * new maximum.
1828  *
1829  * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
1830  * modify the requested rate, which is our case in clk_dummy_rate_ops.
1831  */
1832 static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
1833 {
1834         struct clk_dummy_context *ctx = test->priv;
1835         struct clk_hw *hw = &ctx->hw;
1836         struct clk *clk = hw->clk;
1837         unsigned long rate;
1838
1839         KUNIT_ASSERT_EQ(test,
1840                         clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1841                         0);
1842
1843         KUNIT_ASSERT_EQ(test,
1844                         clk_set_rate_range(clk,
1845                                            DUMMY_CLOCK_RATE_1,
1846                                            DUMMY_CLOCK_RATE_2),
1847                         0);
1848
1849         rate = clk_get_rate(clk);
1850         KUNIT_ASSERT_GT(test, rate, 0);
1851         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1852 }
1853
1854 static struct kunit_case clk_range_test_cases[] = {
1855         KUNIT_CASE(clk_range_test_get_range),
1856         KUNIT_CASE(clk_range_test_get_range_with_hw),
1857         KUNIT_CASE(clk_range_test_get_range_multiple_clk),
1858         KUNIT_CASE(clk_range_test_set_range),
1859         KUNIT_CASE(clk_range_test_set_range_invalid),
1860         KUNIT_CASE(clk_range_test_multiple_disjoints_range),
1861         KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
1862         KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
1863         KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
1864         KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
1865         KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
1866         KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
1867         KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
1868         KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
1869         {}
1870 };
1871
1872 /*
1873  * Test suite for a basic rate clock, without any parent.
1874  *
1875  * These tests are supposed to exercise the rate range API
1876  * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
1877  * clk_drop_range).
1878  */
1879 static struct kunit_suite clk_range_test_suite = {
1880         .name = "clk-range-test",
1881         .init = clk_test_init,
1882         .exit = clk_test_exit,
1883         .test_cases = clk_range_test_cases,
1884 };
1885
1886 /*
1887  * Test that if we have several subsequent calls to
1888  * clk_set_rate_range(), the core will reevaluate whether a new rate is
1889  * needed each and every time.
1890  *
1891  * With clk_dummy_maximize_rate_ops, this means that the the rate will
1892  * trail along the maximum as it evolves.
1893  */
1894 static void clk_range_test_set_range_rate_maximized(struct kunit *test)
1895 {
1896         struct clk_dummy_context *ctx = test->priv;
1897         struct clk_hw *hw = &ctx->hw;
1898         struct clk *clk = hw->clk;
1899         unsigned long rate;
1900
1901         KUNIT_ASSERT_EQ(test,
1902                         clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1903                         0);
1904
1905         KUNIT_ASSERT_EQ(test,
1906                         clk_set_rate_range(clk,
1907                                            DUMMY_CLOCK_RATE_1,
1908                                            DUMMY_CLOCK_RATE_2),
1909                         0);
1910
1911         rate = clk_get_rate(clk);
1912         KUNIT_ASSERT_GT(test, rate, 0);
1913         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1914
1915         KUNIT_ASSERT_EQ(test,
1916                         clk_set_rate_range(clk,
1917                                            DUMMY_CLOCK_RATE_1,
1918                                            DUMMY_CLOCK_RATE_2 - 1000),
1919                         0);
1920
1921         rate = clk_get_rate(clk);
1922         KUNIT_ASSERT_GT(test, rate, 0);
1923         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1924
1925         KUNIT_ASSERT_EQ(test,
1926                         clk_set_rate_range(clk,
1927                                            DUMMY_CLOCK_RATE_1,
1928                                            DUMMY_CLOCK_RATE_2),
1929                         0);
1930
1931         rate = clk_get_rate(clk);
1932         KUNIT_ASSERT_GT(test, rate, 0);
1933         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1934 }
1935
1936 /*
1937  * Test that if we have several subsequent calls to
1938  * clk_set_rate_range(), across multiple users, the core will reevaluate
1939  * whether a new rate is needed each and every time.
1940  *
1941  * With clk_dummy_maximize_rate_ops, this means that the the rate will
1942  * trail along the maximum as it evolves.
1943  */
1944 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
1945 {
1946         struct clk_dummy_context *ctx = test->priv;
1947         struct clk_hw *hw = &ctx->hw;
1948         struct clk *clk = hw->clk;
1949         struct clk *user1, *user2;
1950         unsigned long rate;
1951
1952         user1 = clk_hw_get_clk(hw, NULL);
1953         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1954
1955         user2 = clk_hw_get_clk(hw, NULL);
1956         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1957
1958         KUNIT_ASSERT_EQ(test,
1959                         clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1960                         0);
1961
1962         KUNIT_ASSERT_EQ(test,
1963                         clk_set_rate_range(user1,
1964                                            0,
1965                                            DUMMY_CLOCK_RATE_2),
1966                         0);
1967
1968         rate = clk_get_rate(clk);
1969         KUNIT_ASSERT_GT(test, rate, 0);
1970         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1971
1972         KUNIT_ASSERT_EQ(test,
1973                         clk_set_rate_range(user2,
1974                                            0,
1975                                            DUMMY_CLOCK_RATE_1),
1976                         0);
1977
1978         rate = clk_get_rate(clk);
1979         KUNIT_ASSERT_GT(test, rate, 0);
1980         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1981
1982         KUNIT_ASSERT_EQ(test,
1983                         clk_drop_range(user2),
1984                         0);
1985
1986         rate = clk_get_rate(clk);
1987         KUNIT_ASSERT_GT(test, rate, 0);
1988         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1989
1990         clk_put(user2);
1991         clk_put(user1);
1992 }
1993
1994 /*
1995  * Test that if we have several subsequent calls to
1996  * clk_set_rate_range(), across multiple users, the core will reevaluate
1997  * whether a new rate is needed, including when a user drop its clock.
1998  *
1999  * With clk_dummy_maximize_rate_ops, this means that the rate will
2000  * trail along the maximum as it evolves.
2001  */
2002 static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
2003 {
2004         struct clk_dummy_context *ctx = test->priv;
2005         struct clk_hw *hw = &ctx->hw;
2006         struct clk *clk = hw->clk;
2007         struct clk *user1, *user2;
2008         unsigned long rate;
2009
2010         user1 = clk_hw_get_clk(hw, NULL);
2011         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2012
2013         user2 = clk_hw_get_clk(hw, NULL);
2014         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2015
2016         KUNIT_ASSERT_EQ(test,
2017                         clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
2018                         0);
2019
2020         KUNIT_ASSERT_EQ(test,
2021                         clk_set_rate_range(user1,
2022                                            0,
2023                                            DUMMY_CLOCK_RATE_2),
2024                         0);
2025
2026         rate = clk_get_rate(clk);
2027         KUNIT_ASSERT_GT(test, rate, 0);
2028         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2029
2030         KUNIT_ASSERT_EQ(test,
2031                         clk_set_rate_range(user2,
2032                                            0,
2033                                            DUMMY_CLOCK_RATE_1),
2034                         0);
2035
2036         rate = clk_get_rate(clk);
2037         KUNIT_ASSERT_GT(test, rate, 0);
2038         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2039
2040         clk_put(user2);
2041
2042         rate = clk_get_rate(clk);
2043         KUNIT_ASSERT_GT(test, rate, 0);
2044         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2045
2046         clk_put(user1);
2047 }
2048
2049 static struct kunit_case clk_range_maximize_test_cases[] = {
2050         KUNIT_CASE(clk_range_test_set_range_rate_maximized),
2051         KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
2052         KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
2053         {}
2054 };
2055
2056 /*
2057  * Test suite for a basic rate clock, without any parent.
2058  *
2059  * These tests are supposed to exercise the rate range API
2060  * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
2061  * clk_drop_range), with a driver that will always try to run at the
2062  * highest possible rate.
2063  */
2064 static struct kunit_suite clk_range_maximize_test_suite = {
2065         .name = "clk-range-maximize-test",
2066         .init = clk_maximize_test_init,
2067         .exit = clk_test_exit,
2068         .test_cases = clk_range_maximize_test_cases,
2069 };
2070
2071 /*
2072  * Test that if we have several subsequent calls to
2073  * clk_set_rate_range(), the core will reevaluate whether a new rate is
2074  * needed each and every time.
2075  *
2076  * With clk_dummy_minimize_rate_ops, this means that the the rate will
2077  * trail along the minimum as it evolves.
2078  */
2079 static void clk_range_test_set_range_rate_minimized(struct kunit *test)
2080 {
2081         struct clk_dummy_context *ctx = test->priv;
2082         struct clk_hw *hw = &ctx->hw;
2083         struct clk *clk = hw->clk;
2084         unsigned long rate;
2085
2086         KUNIT_ASSERT_EQ(test,
2087                         clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
2088                         0);
2089
2090         KUNIT_ASSERT_EQ(test,
2091                         clk_set_rate_range(clk,
2092                                            DUMMY_CLOCK_RATE_1,
2093                                            DUMMY_CLOCK_RATE_2),
2094                         0);
2095
2096         rate = clk_get_rate(clk);
2097         KUNIT_ASSERT_GT(test, rate, 0);
2098         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2099
2100         KUNIT_ASSERT_EQ(test,
2101                         clk_set_rate_range(clk,
2102                                            DUMMY_CLOCK_RATE_1 + 1000,
2103                                            DUMMY_CLOCK_RATE_2),
2104                         0);
2105
2106         rate = clk_get_rate(clk);
2107         KUNIT_ASSERT_GT(test, rate, 0);
2108         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
2109
2110         KUNIT_ASSERT_EQ(test,
2111                         clk_set_rate_range(clk,
2112                                            DUMMY_CLOCK_RATE_1,
2113                                            DUMMY_CLOCK_RATE_2),
2114                         0);
2115
2116         rate = clk_get_rate(clk);
2117         KUNIT_ASSERT_GT(test, rate, 0);
2118         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2119 }
2120
2121 /*
2122  * Test that if we have several subsequent calls to
2123  * clk_set_rate_range(), across multiple users, the core will reevaluate
2124  * whether a new rate is needed each and every time.
2125  *
2126  * With clk_dummy_minimize_rate_ops, this means that the the rate will
2127  * trail along the minimum as it evolves.
2128  */
2129 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
2130 {
2131         struct clk_dummy_context *ctx = test->priv;
2132         struct clk_hw *hw = &ctx->hw;
2133         struct clk *clk = hw->clk;
2134         struct clk *user1, *user2;
2135         unsigned long rate;
2136
2137         user1 = clk_hw_get_clk(hw, NULL);
2138         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2139
2140         user2 = clk_hw_get_clk(hw, NULL);
2141         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2142
2143         KUNIT_ASSERT_EQ(test,
2144                         clk_set_rate_range(user1,
2145                                            DUMMY_CLOCK_RATE_1,
2146                                            ULONG_MAX),
2147                         0);
2148
2149         rate = clk_get_rate(clk);
2150         KUNIT_ASSERT_GT(test, rate, 0);
2151         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2152
2153         KUNIT_ASSERT_EQ(test,
2154                         clk_set_rate_range(user2,
2155                                            DUMMY_CLOCK_RATE_2,
2156                                            ULONG_MAX),
2157                         0);
2158
2159         rate = clk_get_rate(clk);
2160         KUNIT_ASSERT_GT(test, rate, 0);
2161         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2162
2163         KUNIT_ASSERT_EQ(test,
2164                         clk_drop_range(user2),
2165                         0);
2166
2167         rate = clk_get_rate(clk);
2168         KUNIT_ASSERT_GT(test, rate, 0);
2169         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2170
2171         clk_put(user2);
2172         clk_put(user1);
2173 }
2174
2175 /*
2176  * Test that if we have several subsequent calls to
2177  * clk_set_rate_range(), across multiple users, the core will reevaluate
2178  * whether a new rate is needed, including when a user drop its clock.
2179  *
2180  * With clk_dummy_minimize_rate_ops, this means that the rate will
2181  * trail along the minimum as it evolves.
2182  */
2183 static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
2184 {
2185         struct clk_dummy_context *ctx = test->priv;
2186         struct clk_hw *hw = &ctx->hw;
2187         struct clk *clk = hw->clk;
2188         struct clk *user1, *user2;
2189         unsigned long rate;
2190
2191         user1 = clk_hw_get_clk(hw, NULL);
2192         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2193
2194         user2 = clk_hw_get_clk(hw, NULL);
2195         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2196
2197         KUNIT_ASSERT_EQ(test,
2198                         clk_set_rate_range(user1,
2199                                            DUMMY_CLOCK_RATE_1,
2200                                            ULONG_MAX),
2201                         0);
2202
2203         rate = clk_get_rate(clk);
2204         KUNIT_ASSERT_GT(test, rate, 0);
2205         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2206
2207         KUNIT_ASSERT_EQ(test,
2208                         clk_set_rate_range(user2,
2209                                            DUMMY_CLOCK_RATE_2,
2210                                            ULONG_MAX),
2211                         0);
2212
2213         rate = clk_get_rate(clk);
2214         KUNIT_ASSERT_GT(test, rate, 0);
2215         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2216
2217         clk_put(user2);
2218
2219         rate = clk_get_rate(clk);
2220         KUNIT_ASSERT_GT(test, rate, 0);
2221         KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2222
2223         clk_put(user1);
2224 }
2225
2226 static struct kunit_case clk_range_minimize_test_cases[] = {
2227         KUNIT_CASE(clk_range_test_set_range_rate_minimized),
2228         KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
2229         KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
2230         {}
2231 };
2232
2233 /*
2234  * Test suite for a basic rate clock, without any parent.
2235  *
2236  * These tests are supposed to exercise the rate range API
2237  * (clk_set_rate_range, clk_set_min_rate, clk_set_max_rate,
2238  * clk_drop_range), with a driver that will always try to run at the
2239  * lowest possible rate.
2240  */
2241 static struct kunit_suite clk_range_minimize_test_suite = {
2242         .name = "clk-range-minimize-test",
2243         .init = clk_minimize_test_init,
2244         .exit = clk_test_exit,
2245         .test_cases = clk_range_minimize_test_cases,
2246 };
2247
2248 struct clk_leaf_mux_ctx {
2249         struct clk_multiple_parent_ctx mux_ctx;
2250         struct clk_hw hw;
2251 };
2252
2253 static int
2254 clk_leaf_mux_set_rate_parent_test_init(struct kunit *test)
2255 {
2256         struct clk_leaf_mux_ctx *ctx;
2257         const char *top_parents[2] = { "parent-0", "parent-1" };
2258         int ret;
2259
2260         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2261         if (!ctx)
2262                 return -ENOMEM;
2263         test->priv = ctx;
2264
2265         ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2266                                                                     &clk_dummy_rate_ops,
2267                                                                     0);
2268         ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2269         ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2270         if (ret)
2271                 return ret;
2272
2273         ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2274                                                                     &clk_dummy_rate_ops,
2275                                                                     0);
2276         ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2277         ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2278         if (ret)
2279                 return ret;
2280
2281         ctx->mux_ctx.current_parent = 0;
2282         ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2283                                                    &clk_multiple_parents_mux_ops,
2284                                                    0);
2285         ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2286         if (ret)
2287                 return ret;
2288
2289         ctx->hw.init = CLK_HW_INIT_HW("test-clock", &ctx->mux_ctx.hw,
2290                                       &clk_dummy_single_parent_ops,
2291                                       CLK_SET_RATE_PARENT);
2292         ret = clk_hw_register(NULL, &ctx->hw);
2293         if (ret)
2294                 return ret;
2295
2296         return 0;
2297 }
2298
2299 static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test)
2300 {
2301         struct clk_leaf_mux_ctx *ctx = test->priv;
2302
2303         clk_hw_unregister(&ctx->hw);
2304         clk_hw_unregister(&ctx->mux_ctx.hw);
2305         clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2306         clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2307 }
2308
2309 /*
2310  * Test that, for a clock that will forward any rate request to its
2311  * parent, the rate request structure returned by __clk_determine_rate
2312  * is sane and will be what we expect.
2313  */
2314 static void clk_leaf_mux_set_rate_parent_determine_rate(struct kunit *test)
2315 {
2316         struct clk_leaf_mux_ctx *ctx = test->priv;
2317         struct clk_hw *hw = &ctx->hw;
2318         struct clk *clk = hw->clk;
2319         struct clk_rate_request req;
2320         unsigned long rate;
2321         int ret;
2322
2323         rate = clk_get_rate(clk);
2324         KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2325
2326         clk_hw_init_rate_request(hw, &req, DUMMY_CLOCK_RATE_2);
2327
2328         ret = __clk_determine_rate(hw, &req);
2329         KUNIT_ASSERT_EQ(test, ret, 0);
2330
2331         KUNIT_EXPECT_EQ(test, req.rate, DUMMY_CLOCK_RATE_2);
2332         KUNIT_EXPECT_EQ(test, req.best_parent_rate, DUMMY_CLOCK_RATE_2);
2333         KUNIT_EXPECT_PTR_EQ(test, req.best_parent_hw, &ctx->mux_ctx.hw);
2334 }
2335
2336 static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = {
2337         KUNIT_CASE(clk_leaf_mux_set_rate_parent_determine_rate),
2338         {}
2339 };
2340
2341 /*
2342  * Test suite for a clock whose parent is a mux with multiple parents.
2343  * The leaf clock has CLK_SET_RATE_PARENT, and will forward rate
2344  * requests to the mux, which will then select which parent is the best
2345  * fit for a given rate.
2346  *
2347  * These tests are supposed to exercise the behaviour of muxes, and the
2348  * proper selection of parents.
2349   */
2350 static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = {
2351         .name = "clk-leaf-mux-set-rate-parent",
2352         .init = clk_leaf_mux_set_rate_parent_test_init,
2353         .exit = clk_leaf_mux_set_rate_parent_test_exit,
2354         .test_cases = clk_leaf_mux_set_rate_parent_test_cases,
2355 };
2356
2357 kunit_test_suites(
2358         &clk_leaf_mux_set_rate_parent_test_suite,
2359         &clk_test_suite,
2360         &clk_multiple_parents_mux_test_suite,
2361         &clk_orphan_transparent_multiple_parent_mux_test_suite,
2362         &clk_orphan_transparent_single_parent_test_suite,
2363         &clk_orphan_two_level_root_last_test_suite,
2364         &clk_range_test_suite,
2365         &clk_range_maximize_test_suite,
2366         &clk_range_minimize_test_suite,
2367         &clk_single_parent_mux_test_suite,
2368         &clk_uncached_test_suite
2369 );
2370 MODULE_LICENSE("GPL v2");