xref: /linux/drivers/clk/clk_test.c (revision 502d45774af09f1c681c754c4b7cdfb5d7f72fd9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kunit tests for clk framework
4  */
5 #include <linux/clk.h>
6 #include <linux/clk-provider.h>
7 #include <linux/clk/clk-conf.h>
8 #include <linux/of.h>
9 #include <linux/platform_device.h>
10 #include <linux/units.h>
11 
12 /* Needed for clk_hw_get_clk() */
13 #include "clk.h"
14 
15 #include <kunit/clk.h>
16 #include <kunit/of.h>
17 #include <kunit/platform_device.h>
18 #include <kunit/test.h>
19 
20 #include "kunit_clk_assigned_rates.h"
21 #include "clk_parent_data_test.h"
22 
23 static const struct clk_ops empty_clk_ops = { };
24 
25 #define DUMMY_CLOCK_INIT_RATE		(42 * HZ_PER_MHZ)
26 #define DUMMY_CLOCK_RATE_1		(142 * HZ_PER_MHZ)
27 #define DUMMY_CLOCK_RATE_2		(242 * HZ_PER_MHZ)
28 
29 struct clk_dummy_context {
30 	struct clk_hw hw;
31 	unsigned long rate;
32 	struct clk_spread_spectrum sscs;
33 };
34 
clk_dummy_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)35 static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw,
36 					   unsigned long parent_rate)
37 {
38 	struct clk_dummy_context *ctx =
39 		container_of(hw, struct clk_dummy_context, hw);
40 
41 	return ctx->rate;
42 }
43 
clk_dummy_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)44 static int clk_dummy_determine_rate(struct clk_hw *hw,
45 				    struct clk_rate_request *req)
46 {
47 	/* Just return the same rate without modifying it */
48 	return 0;
49 }
50 
clk_dummy_maximize_rate(struct clk_hw * hw,struct clk_rate_request * req)51 static int clk_dummy_maximize_rate(struct clk_hw *hw,
52 				   struct clk_rate_request *req)
53 {
54 	/*
55 	 * If there's a maximum set, always run the clock at the maximum
56 	 * allowed.
57 	 */
58 	if (req->max_rate < ULONG_MAX)
59 		req->rate = req->max_rate;
60 
61 	return 0;
62 }
63 
clk_dummy_minimize_rate(struct clk_hw * hw,struct clk_rate_request * req)64 static int clk_dummy_minimize_rate(struct clk_hw *hw,
65 				   struct clk_rate_request *req)
66 {
67 	/*
68 	 * If there's a minimum set, always run the clock at the minimum
69 	 * allowed.
70 	 */
71 	if (req->min_rate > 0)
72 		req->rate = req->min_rate;
73 
74 	return 0;
75 }
76 
clk_dummy_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)77 static int clk_dummy_set_rate(struct clk_hw *hw,
78 			      unsigned long rate,
79 			      unsigned long parent_rate)
80 {
81 	struct clk_dummy_context *ctx =
82 		container_of(hw, struct clk_dummy_context, hw);
83 
84 	ctx->rate = rate;
85 	return 0;
86 }
87 
clk_dummy_set_spread_spectrum(struct clk_hw * hw,const struct clk_spread_spectrum * ss_conf)88 static int clk_dummy_set_spread_spectrum(struct clk_hw *hw,
89 					 const struct clk_spread_spectrum *ss_conf)
90 {
91 	struct clk_dummy_context *ctx =
92 		container_of(hw, struct clk_dummy_context, hw);
93 
94 	ctx->sscs = *ss_conf;
95 
96 	return 0;
97 }
98 
clk_dummy_single_set_parent(struct clk_hw * hw,u8 index)99 static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
100 {
101 	if (index >= clk_hw_get_num_parents(hw))
102 		return -EINVAL;
103 
104 	return 0;
105 }
106 
clk_dummy_single_get_parent(struct clk_hw * hw)107 static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
108 {
109 	return 0;
110 }
111 
112 static const struct clk_ops clk_dummy_rate_ops = {
113 	.recalc_rate = clk_dummy_recalc_rate,
114 	.determine_rate = clk_dummy_determine_rate,
115 	.set_rate = clk_dummy_set_rate,
116 	.set_spread_spectrum = clk_dummy_set_spread_spectrum,
117 };
118 
119 static const struct clk_ops clk_dummy_maximize_rate_ops = {
120 	.recalc_rate = clk_dummy_recalc_rate,
121 	.determine_rate = clk_dummy_maximize_rate,
122 	.set_rate = clk_dummy_set_rate,
123 	.set_spread_spectrum = clk_dummy_set_spread_spectrum,
124 };
125 
126 static const struct clk_ops clk_dummy_minimize_rate_ops = {
127 	.recalc_rate = clk_dummy_recalc_rate,
128 	.determine_rate = clk_dummy_minimize_rate,
129 	.set_rate = clk_dummy_set_rate,
130 	.set_spread_spectrum = clk_dummy_set_spread_spectrum,
131 };
132 
133 static const struct clk_ops clk_dummy_single_parent_ops = {
134 	/*
135 	 * FIXME: Even though we should probably be able to use
136 	 * __clk_mux_determine_rate() here, if we use it and call
137 	 * clk_round_rate() or clk_set_rate() with a rate lower than
138 	 * what all the parents can provide, it will return -EINVAL.
139 	 *
140 	 * This is due to the fact that it has the undocumented
141 	 * behaviour to always pick up the closest rate higher than the
142 	 * requested rate. If we get something lower, it thus considers
143 	 * that it's not acceptable and will return an error.
144 	 *
145 	 * It's somewhat inconsistent and creates a weird threshold
146 	 * between rates above the parent rate which would be rounded to
147 	 * what the parent can provide, but rates below will simply
148 	 * return an error.
149 	 */
150 	.determine_rate = __clk_mux_determine_rate_closest,
151 	.set_parent = clk_dummy_single_set_parent,
152 	.get_parent = clk_dummy_single_get_parent,
153 };
154 
155 struct clk_multiple_parent_ctx {
156 	struct clk_dummy_context parents_ctx[2];
157 	struct clk_hw hw;
158 	u8 current_parent;
159 };
160 
clk_multiple_parents_mux_set_parent(struct clk_hw * hw,u8 index)161 static int clk_multiple_parents_mux_set_parent(struct clk_hw *hw, u8 index)
162 {
163 	struct clk_multiple_parent_ctx *ctx =
164 		container_of(hw, struct clk_multiple_parent_ctx, hw);
165 
166 	if (index >= clk_hw_get_num_parents(hw))
167 		return -EINVAL;
168 
169 	ctx->current_parent = index;
170 
171 	return 0;
172 }
173 
clk_multiple_parents_mux_get_parent(struct clk_hw * hw)174 static u8 clk_multiple_parents_mux_get_parent(struct clk_hw *hw)
175 {
176 	struct clk_multiple_parent_ctx *ctx =
177 		container_of(hw, struct clk_multiple_parent_ctx, hw);
178 
179 	return ctx->current_parent;
180 }
181 
182 static const struct clk_ops clk_multiple_parents_mux_ops = {
183 	.get_parent = clk_multiple_parents_mux_get_parent,
184 	.set_parent = clk_multiple_parents_mux_set_parent,
185 	.determine_rate = __clk_mux_determine_rate_closest,
186 };
187 
188 static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = {
189 	.determine_rate = clk_hw_determine_rate_no_reparent,
190 	.get_parent = clk_multiple_parents_mux_get_parent,
191 	.set_parent = clk_multiple_parents_mux_set_parent,
192 };
193 
clk_test_init_with_ops(struct kunit * test,const struct clk_ops * ops)194 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
195 {
196 	struct clk_dummy_context *ctx;
197 	struct clk_init_data init = { };
198 	int ret;
199 
200 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
201 	if (!ctx)
202 		return -ENOMEM;
203 	ctx->rate = DUMMY_CLOCK_INIT_RATE;
204 	test->priv = ctx;
205 
206 	init.name = "test_dummy_rate";
207 	init.ops = ops;
208 	ctx->hw.init = &init;
209 
210 	ret = clk_hw_register(NULL, &ctx->hw);
211 	if (ret)
212 		return ret;
213 
214 	return 0;
215 }
216 
clk_test_init(struct kunit * test)217 static int clk_test_init(struct kunit *test)
218 {
219 	return clk_test_init_with_ops(test, &clk_dummy_rate_ops);
220 }
221 
clk_maximize_test_init(struct kunit * test)222 static int clk_maximize_test_init(struct kunit *test)
223 {
224 	return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops);
225 }
226 
clk_minimize_test_init(struct kunit * test)227 static int clk_minimize_test_init(struct kunit *test)
228 {
229 	return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops);
230 }
231 
clk_test_exit(struct kunit * test)232 static void clk_test_exit(struct kunit *test)
233 {
234 	struct clk_dummy_context *ctx = test->priv;
235 
236 	clk_hw_unregister(&ctx->hw);
237 }
238 
239 /*
240  * Test that the actual rate matches what is returned by clk_get_rate()
241  */
clk_test_get_rate(struct kunit * test)242 static void clk_test_get_rate(struct kunit *test)
243 {
244 	struct clk_dummy_context *ctx = test->priv;
245 	struct clk_hw *hw = &ctx->hw;
246 	struct clk *clk = clk_hw_get_clk(hw, NULL);
247 	unsigned long rate;
248 
249 	rate = clk_get_rate(clk);
250 	KUNIT_ASSERT_GT(test, rate, 0);
251 	KUNIT_EXPECT_EQ(test, rate, ctx->rate);
252 
253 	clk_put(clk);
254 }
255 
256 /*
257  * Test that, after a call to clk_set_rate(), the rate returned by
258  * clk_get_rate() matches.
259  *
260  * This assumes that clk_ops.determine_rate won't modify the requested rate,
261  * which is our case in clk_dummy_rate_ops.
262  */
clk_test_set_get_rate(struct kunit * test)263 static void clk_test_set_get_rate(struct kunit *test)
264 {
265 	struct clk_dummy_context *ctx = test->priv;
266 	struct clk_hw *hw = &ctx->hw;
267 	struct clk *clk = clk_hw_get_clk(hw, NULL);
268 	unsigned long rate;
269 
270 	KUNIT_ASSERT_EQ(test,
271 			clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
272 			0);
273 
274 	rate = clk_get_rate(clk);
275 	KUNIT_ASSERT_GT(test, rate, 0);
276 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
277 
278 	clk_put(clk);
279 }
280 
281 /*
282  * Test that, after several calls to clk_set_rate(), the rate returned
283  * by clk_get_rate() matches the last one.
284  *
285  * This assumes that clk_ops.determine_rate won't modify the requested rate,
286  * which is our case in clk_dummy_rate_ops.
287  */
clk_test_set_set_get_rate(struct kunit * test)288 static void clk_test_set_set_get_rate(struct kunit *test)
289 {
290 	struct clk_dummy_context *ctx = test->priv;
291 	struct clk_hw *hw = &ctx->hw;
292 	struct clk *clk = clk_hw_get_clk(hw, NULL);
293 	unsigned long rate;
294 
295 	KUNIT_ASSERT_EQ(test,
296 			clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
297 			0);
298 
299 	KUNIT_ASSERT_EQ(test,
300 			clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
301 			0);
302 
303 	rate = clk_get_rate(clk);
304 	KUNIT_ASSERT_GT(test, rate, 0);
305 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
306 
307 	clk_put(clk);
308 }
309 
310 /*
311  * Test that clk_round_rate and clk_set_rate are consistent and will
312  * return the same frequency.
313  */
clk_test_round_set_get_rate(struct kunit * test)314 static void clk_test_round_set_get_rate(struct kunit *test)
315 {
316 	struct clk_dummy_context *ctx = test->priv;
317 	struct clk_hw *hw = &ctx->hw;
318 	struct clk *clk = clk_hw_get_clk(hw, NULL);
319 	unsigned long set_rate;
320 	long rounded_rate;
321 
322 	rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
323 	KUNIT_ASSERT_GT(test, rounded_rate, 0);
324 	KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
325 
326 	KUNIT_ASSERT_EQ(test,
327 			clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
328 			0);
329 
330 	set_rate = clk_get_rate(clk);
331 	KUNIT_ASSERT_GT(test, set_rate, 0);
332 	KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
333 
334 	clk_put(clk);
335 }
336 
337 static struct kunit_case clk_test_cases[] = {
338 	KUNIT_CASE(clk_test_get_rate),
339 	KUNIT_CASE(clk_test_set_get_rate),
340 	KUNIT_CASE(clk_test_set_set_get_rate),
341 	KUNIT_CASE(clk_test_round_set_get_rate),
342 	{}
343 };
344 
345 /*
346  * Test suite for a basic rate clock, without any parent.
347  *
348  * These tests exercise the rate API with simple scenarios
349  */
350 static struct kunit_suite clk_test_suite = {
351 	.name = "clk-test",
352 	.init = clk_test_init,
353 	.exit = clk_test_exit,
354 	.test_cases = clk_test_cases,
355 };
356 
clk_uncached_test_init(struct kunit * test)357 static int clk_uncached_test_init(struct kunit *test)
358 {
359 	struct clk_dummy_context *ctx;
360 	int ret;
361 
362 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
363 	if (!ctx)
364 		return -ENOMEM;
365 	test->priv = ctx;
366 
367 	ctx->rate = DUMMY_CLOCK_INIT_RATE;
368 	ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
369 					     &clk_dummy_rate_ops,
370 					     CLK_GET_RATE_NOCACHE);
371 
372 	ret = clk_hw_register(NULL, &ctx->hw);
373 	if (ret)
374 		return ret;
375 
376 	return 0;
377 }
378 
379 /*
380  * Test that for an uncached clock, the clock framework doesn't cache
381  * the rate and clk_get_rate() will return the underlying clock rate
382  * even if it changed.
383  */
clk_test_uncached_get_rate(struct kunit * test)384 static void clk_test_uncached_get_rate(struct kunit *test)
385 {
386 	struct clk_dummy_context *ctx = test->priv;
387 	struct clk_hw *hw = &ctx->hw;
388 	struct clk *clk = clk_hw_get_clk(hw, NULL);
389 	unsigned long rate;
390 
391 	rate = clk_get_rate(clk);
392 	KUNIT_ASSERT_GT(test, rate, 0);
393 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
394 
395 	/* We change the rate behind the clock framework's back */
396 	ctx->rate = DUMMY_CLOCK_RATE_1;
397 	rate = clk_get_rate(clk);
398 	KUNIT_ASSERT_GT(test, rate, 0);
399 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
400 
401 	clk_put(clk);
402 }
403 
404 /*
405  * Test that for an uncached clock, clk_set_rate_range() will work
406  * properly if the rate hasn't changed.
407  */
clk_test_uncached_set_range(struct kunit * test)408 static void clk_test_uncached_set_range(struct kunit *test)
409 {
410 	struct clk_dummy_context *ctx = test->priv;
411 	struct clk_hw *hw = &ctx->hw;
412 	struct clk *clk = clk_hw_get_clk(hw, NULL);
413 	unsigned long rate;
414 
415 	KUNIT_ASSERT_EQ(test,
416 			clk_set_rate_range(clk,
417 					   DUMMY_CLOCK_RATE_1,
418 					   DUMMY_CLOCK_RATE_2),
419 			0);
420 
421 	rate = clk_get_rate(clk);
422 	KUNIT_ASSERT_GT(test, rate, 0);
423 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
424 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
425 
426 	clk_put(clk);
427 }
428 
429 /*
430  * Test that for an uncached clock, clk_set_rate_range() will work
431  * properly if the rate has changed in hardware.
432  *
433  * In this case, it means that if the rate wasn't initially in the range
434  * we're trying to set, but got changed at some point into the range
435  * without the kernel knowing about it, its rate shouldn't be affected.
436  */
clk_test_uncached_updated_rate_set_range(struct kunit * test)437 static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
438 {
439 	struct clk_dummy_context *ctx = test->priv;
440 	struct clk_hw *hw = &ctx->hw;
441 	struct clk *clk = clk_hw_get_clk(hw, NULL);
442 	unsigned long rate;
443 
444 	/* We change the rate behind the clock framework's back */
445 	ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
446 	KUNIT_ASSERT_EQ(test,
447 			clk_set_rate_range(clk,
448 					   DUMMY_CLOCK_RATE_1,
449 					   DUMMY_CLOCK_RATE_2),
450 			0);
451 
452 	rate = clk_get_rate(clk);
453 	KUNIT_ASSERT_GT(test, rate, 0);
454 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
455 
456 	clk_put(clk);
457 }
458 
459 static struct kunit_case clk_uncached_test_cases[] = {
460 	KUNIT_CASE(clk_test_uncached_get_rate),
461 	KUNIT_CASE(clk_test_uncached_set_range),
462 	KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
463 	{}
464 };
465 
466 /*
467  * Test suite for a basic, uncached, rate clock, without any parent.
468  *
469  * These tests exercise the rate API with simple scenarios
470  */
471 static struct kunit_suite clk_uncached_test_suite = {
472 	.name = "clk-uncached-test",
473 	.init = clk_uncached_test_init,
474 	.exit = clk_test_exit,
475 	.test_cases = clk_uncached_test_cases,
476 };
477 
478 static int
clk_multiple_parents_mux_test_init(struct kunit * test)479 clk_multiple_parents_mux_test_init(struct kunit *test)
480 {
481 	struct clk_multiple_parent_ctx *ctx;
482 	const char *parents[2] = { "parent-0", "parent-1"};
483 	int ret;
484 
485 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
486 	if (!ctx)
487 		return -ENOMEM;
488 	test->priv = ctx;
489 
490 	ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
491 							    &clk_dummy_rate_ops,
492 							    0);
493 	ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
494 	ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[0].hw);
495 	if (ret)
496 		return ret;
497 
498 	ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
499 							    &clk_dummy_rate_ops,
500 							    0);
501 	ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
502 	ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[1].hw);
503 	if (ret)
504 		return ret;
505 
506 	ctx->current_parent = 0;
507 	ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
508 					   &clk_multiple_parents_mux_ops,
509 					   CLK_SET_RATE_PARENT);
510 	ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
511 	if (ret)
512 		return ret;
513 
514 	return 0;
515 }
516 
517 /*
518  * Test that for a clock with multiple parents, clk_get_parent()
519  * actually returns the current one.
520  */
521 static void
clk_test_multiple_parents_mux_get_parent(struct kunit * test)522 clk_test_multiple_parents_mux_get_parent(struct kunit *test)
523 {
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);
527 	struct clk *parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
528 
529 	KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
530 
531 	clk_put(parent);
532 	clk_put(clk);
533 }
534 
535 /*
536  * Test that for a clock with a multiple parents, clk_has_parent()
537  * actually reports all of them as parents.
538  */
539 static void
clk_test_multiple_parents_mux_has_parent(struct kunit * test)540 clk_test_multiple_parents_mux_has_parent(struct kunit *test)
541 {
542 	struct clk_multiple_parent_ctx *ctx = test->priv;
543 	struct clk_hw *hw = &ctx->hw;
544 	struct clk *clk = clk_hw_get_clk(hw, NULL);
545 	struct clk *parent;
546 
547 	parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
548 	KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
549 	clk_put(parent);
550 
551 	parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
552 	KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
553 	clk_put(parent);
554 
555 	clk_put(clk);
556 }
557 
558 /*
559  * Test that for a clock with a multiple parents, if we set a range on
560  * that clock and the parent is changed, its rate after the reparenting
561  * is still within the range we asked for.
562  *
563  * FIXME: clk_set_parent() only does the reparenting but doesn't
564  * reevaluate whether the new clock rate is within its boundaries or
565  * not.
566  */
567 static void
clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit * test)568 clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
569 {
570 	struct clk_multiple_parent_ctx *ctx = test->priv;
571 	struct clk_hw *hw = &ctx->hw;
572 	struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
573 	struct clk *parent1, *parent2;
574 	unsigned long rate;
575 	int ret;
576 
577 	kunit_skip(test, "This needs to be fixed in the core.");
578 
579 	parent1 = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[0].hw, NULL);
580 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent1);
581 	KUNIT_ASSERT_TRUE(test, clk_is_match(clk_get_parent(clk), parent1));
582 
583 	parent2 = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[1].hw, NULL);
584 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent2);
585 
586 	ret = clk_set_rate(parent1, DUMMY_CLOCK_RATE_1);
587 	KUNIT_ASSERT_EQ(test, ret, 0);
588 
589 	ret = clk_set_rate(parent2, DUMMY_CLOCK_RATE_2);
590 	KUNIT_ASSERT_EQ(test, ret, 0);
591 
592 	ret = clk_set_rate_range(clk,
593 				 DUMMY_CLOCK_RATE_1 - 1000,
594 				 DUMMY_CLOCK_RATE_1 + 1000);
595 	KUNIT_ASSERT_EQ(test, ret, 0);
596 
597 	ret = clk_set_parent(clk, parent2);
598 	KUNIT_ASSERT_EQ(test, ret, 0);
599 
600 	rate = clk_get_rate(clk);
601 	KUNIT_ASSERT_GT(test, rate, 0);
602 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
603 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
604 }
605 
606 static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
607 	KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
608 	KUNIT_CASE(clk_test_multiple_parents_mux_has_parent),
609 	KUNIT_CASE(clk_test_multiple_parents_mux_set_range_set_parent_get_rate),
610 	{}
611 };
612 
613 /*
614  * Test suite for a basic mux clock with two parents, with
615  * CLK_SET_RATE_PARENT on the child.
616  *
617  * These tests exercise the consumer API and check that the state of the
618  * child and parents are sane and consistent.
619  */
620 static struct kunit_suite
621 clk_multiple_parents_mux_test_suite = {
622 	.name = "clk-multiple-parents-mux-test",
623 	.init = clk_multiple_parents_mux_test_init,
624 	.test_cases = clk_multiple_parents_mux_test_cases,
625 };
626 
627 static int
clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit * test)628 clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test)
629 {
630 	struct clk_multiple_parent_ctx *ctx;
631 	const char *parents[2] = { "missing-parent", "proper-parent"};
632 	int ret;
633 
634 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
635 	if (!ctx)
636 		return -ENOMEM;
637 	test->priv = ctx;
638 
639 	ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("proper-parent",
640 							    &clk_dummy_rate_ops,
641 							    0);
642 	ctx->parents_ctx[1].rate = DUMMY_CLOCK_INIT_RATE;
643 	ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[1].hw);
644 	if (ret)
645 		return ret;
646 
647 	ctx->hw.init = CLK_HW_INIT_PARENTS("test-orphan-mux", parents,
648 					   &clk_multiple_parents_mux_ops,
649 					   CLK_SET_RATE_PARENT);
650 	ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
651 	if (ret)
652 		return ret;
653 
654 	return 0;
655 }
656 
657 /*
658  * Test that, for a mux whose current parent hasn't been registered yet and is
659  * thus orphan, clk_get_parent() will return NULL.
660  */
661 static void
clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit * test)662 clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit *test)
663 {
664 	struct clk_multiple_parent_ctx *ctx = test->priv;
665 	struct clk_hw *hw = &ctx->hw;
666 	struct clk *clk = clk_hw_get_clk(hw, NULL);
667 
668 	KUNIT_EXPECT_PTR_EQ(test, clk_get_parent(clk), NULL);
669 
670 	clk_put(clk);
671 }
672 
673 /*
674  * Test that, for a mux whose current parent hasn't been registered yet,
675  * calling clk_set_parent() to a valid parent will properly update the
676  * mux parent and its orphan status.
677  */
678 static void
clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit * test)679 clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit *test)
680 {
681 	struct clk_multiple_parent_ctx *ctx = test->priv;
682 	struct clk_hw *hw = &ctx->hw;
683 	struct clk *clk = clk_hw_get_clk(hw, NULL);
684 	struct clk *parent, *new_parent;
685 	int ret;
686 
687 	parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
688 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
689 
690 	ret = clk_set_parent(clk, parent);
691 	KUNIT_ASSERT_EQ(test, ret, 0);
692 
693 	new_parent = clk_get_parent(clk);
694 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
695 	KUNIT_EXPECT_TRUE(test, clk_is_match(parent, new_parent));
696 
697 	clk_put(parent);
698 	clk_put(clk);
699 }
700 
701 /*
702  * Test that, for a mux that started orphan but got switched to a valid
703  * parent, calling clk_drop_range() on the mux won't affect the parent
704  * rate.
705  */
706 static void
clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit * test)707 clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit *test)
708 {
709 	struct clk_multiple_parent_ctx *ctx = test->priv;
710 	struct clk_hw *hw = &ctx->hw;
711 	struct clk *clk = clk_hw_get_clk(hw, NULL);
712 	struct clk *parent;
713 	unsigned long parent_rate, new_parent_rate;
714 	int ret;
715 
716 	parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
717 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
718 
719 	parent_rate = clk_get_rate(parent);
720 	KUNIT_ASSERT_GT(test, parent_rate, 0);
721 
722 	ret = clk_set_parent(clk, parent);
723 	KUNIT_ASSERT_EQ(test, ret, 0);
724 
725 	ret = clk_drop_range(clk);
726 	KUNIT_ASSERT_EQ(test, ret, 0);
727 
728 	new_parent_rate = clk_get_rate(clk);
729 	KUNIT_ASSERT_GT(test, new_parent_rate, 0);
730 	KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
731 
732 	clk_put(parent);
733 	clk_put(clk);
734 }
735 
736 /*
737  * Test that, for a mux that started orphan but got switched to a valid
738  * parent, the rate of the mux and its new parent are consistent.
739  */
740 static void
clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit * test)741 clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit *test)
742 {
743 	struct clk_multiple_parent_ctx *ctx = test->priv;
744 	struct clk_hw *hw = &ctx->hw;
745 	struct clk *clk = clk_hw_get_clk(hw, NULL);
746 	struct clk *parent;
747 	unsigned long parent_rate, rate;
748 	int ret;
749 
750 	parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
751 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
752 
753 	parent_rate = clk_get_rate(parent);
754 	KUNIT_ASSERT_GT(test, parent_rate, 0);
755 
756 	ret = clk_set_parent(clk, parent);
757 	KUNIT_ASSERT_EQ(test, ret, 0);
758 
759 	rate = clk_get_rate(clk);
760 	KUNIT_ASSERT_GT(test, rate, 0);
761 	KUNIT_EXPECT_EQ(test, parent_rate, rate);
762 
763 	clk_put(parent);
764 	clk_put(clk);
765 }
766 
767 /*
768  * Test that, for a mux that started orphan but got switched to a valid
769  * parent, calling clk_put() on the mux won't affect the parent rate.
770  */
771 static void
clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit * test)772 clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit *test)
773 {
774 	struct clk_multiple_parent_ctx *ctx = test->priv;
775 	struct clk *clk, *parent;
776 	unsigned long parent_rate, new_parent_rate;
777 	int ret;
778 
779 	parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
780 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
781 
782 	clk = clk_hw_get_clk(&ctx->hw, NULL);
783 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
784 
785 	parent_rate = clk_get_rate(parent);
786 	KUNIT_ASSERT_GT(test, parent_rate, 0);
787 
788 	ret = clk_set_parent(clk, parent);
789 	KUNIT_ASSERT_EQ(test, ret, 0);
790 
791 	clk_put(clk);
792 
793 	new_parent_rate = clk_get_rate(parent);
794 	KUNIT_ASSERT_GT(test, new_parent_rate, 0);
795 	KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
796 
797 	clk_put(parent);
798 }
799 
800 /*
801  * Test that, for a mux that started orphan but got switched to a valid
802  * parent, calling clk_set_rate_range() will affect the parent state if
803  * its rate is out of range.
804  */
805 static void
clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit * test)806 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit *test)
807 {
808 	struct clk_multiple_parent_ctx *ctx = test->priv;
809 	struct clk_hw *hw = &ctx->hw;
810 	struct clk *clk = clk_hw_get_clk(hw, NULL);
811 	struct clk *parent;
812 	unsigned long rate;
813 	int ret;
814 
815 	parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
816 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
817 
818 	ret = clk_set_parent(clk, parent);
819 	KUNIT_ASSERT_EQ(test, ret, 0);
820 
821 	ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
822 	KUNIT_ASSERT_EQ(test, ret, 0);
823 
824 	rate = clk_get_rate(clk);
825 	KUNIT_ASSERT_GT(test, rate, 0);
826 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
827 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
828 
829 	clk_put(parent);
830 	clk_put(clk);
831 }
832 
833 /*
834  * Test that, for a mux that started orphan but got switched to a valid
835  * parent, calling clk_set_rate_range() won't affect the parent state if
836  * its rate is within range.
837  */
838 static void
clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit * test)839 clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit *test)
840 {
841 	struct clk_multiple_parent_ctx *ctx = test->priv;
842 	struct clk_hw *hw = &ctx->hw;
843 	struct clk *clk = clk_hw_get_clk(hw, NULL);
844 	struct clk *parent;
845 	unsigned long parent_rate, new_parent_rate;
846 	int ret;
847 
848 	parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
849 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
850 
851 	parent_rate = clk_get_rate(parent);
852 	KUNIT_ASSERT_GT(test, parent_rate, 0);
853 
854 	ret = clk_set_parent(clk, parent);
855 	KUNIT_ASSERT_EQ(test, ret, 0);
856 
857 	ret = clk_set_rate_range(clk,
858 				 DUMMY_CLOCK_INIT_RATE - 1000,
859 				 DUMMY_CLOCK_INIT_RATE + 1000);
860 	KUNIT_ASSERT_EQ(test, ret, 0);
861 
862 	new_parent_rate = clk_get_rate(parent);
863 	KUNIT_ASSERT_GT(test, new_parent_rate, 0);
864 	KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
865 
866 	clk_put(parent);
867 	clk_put(clk);
868 }
869 
870 /*
871  * Test that, for a mux whose current parent hasn't been registered yet,
872  * calling clk_set_rate_range() will succeed, and will be taken into
873  * account when rounding a rate.
874  */
875 static void
clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit * test)876 clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit *test)
877 {
878 	struct clk_multiple_parent_ctx *ctx = test->priv;
879 	struct clk_hw *hw = &ctx->hw;
880 	struct clk *clk = clk_hw_get_clk(hw, NULL);
881 	long rate;
882 	int ret;
883 
884 	ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
885 	KUNIT_ASSERT_EQ(test, ret, 0);
886 
887 	rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
888 	KUNIT_ASSERT_GT(test, rate, 0);
889 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
890 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
891 
892 	clk_put(clk);
893 }
894 
895 /*
896  * Test that, for a mux that started orphan, was assigned and rate and
897  * then got switched to a valid parent, its rate is eventually within
898  * range.
899  *
900  * FIXME: Even though we update the rate as part of clk_set_parent(), we
901  * don't evaluate whether that new rate is within range and needs to be
902  * adjusted.
903  */
904 static void
clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit * test)905 clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit *test)
906 {
907 	struct clk_multiple_parent_ctx *ctx = test->priv;
908 	struct clk_hw *hw = &ctx->hw;
909 	struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
910 	struct clk *parent;
911 	unsigned long rate;
912 	int ret;
913 
914 	kunit_skip(test, "This needs to be fixed in the core.");
915 
916 	clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
917 
918 	parent = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[1].hw, NULL);
919 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
920 
921 	ret = clk_set_parent(clk, parent);
922 	KUNIT_ASSERT_EQ(test, ret, 0);
923 
924 	rate = clk_get_rate(clk);
925 	KUNIT_ASSERT_GT(test, rate, 0);
926 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
927 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
928 }
929 
930 static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = {
931 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_get_parent),
932 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent),
933 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range),
934 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate),
935 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_put),
936 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified),
937 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched),
938 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate),
939 	KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate),
940 	{}
941 };
942 
943 /*
944  * Test suite for a basic mux clock with two parents. The default parent
945  * isn't registered, only the second parent is. By default, the clock
946  * will thus be orphan.
947  *
948  * These tests exercise the behaviour of the consumer API when dealing
949  * with an orphan clock, and how we deal with the transition to a valid
950  * parent.
951  */
952 static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = {
953 	.name = "clk-orphan-transparent-multiple-parent-mux-test",
954 	.init = clk_orphan_transparent_multiple_parent_mux_test_init,
955 	.test_cases = clk_orphan_transparent_multiple_parent_mux_test_cases,
956 };
957 
958 struct clk_single_parent_ctx {
959 	struct clk_dummy_context parent_ctx;
960 	struct clk_hw hw;
961 };
962 
clk_single_parent_mux_test_init(struct kunit * test)963 static int clk_single_parent_mux_test_init(struct kunit *test)
964 {
965 	struct clk_single_parent_ctx *ctx;
966 	int ret;
967 
968 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
969 	if (!ctx)
970 		return -ENOMEM;
971 	test->priv = ctx;
972 
973 	ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
974 	ctx->parent_ctx.hw.init =
975 		CLK_HW_INIT_NO_PARENT("parent-clk",
976 				      &clk_dummy_rate_ops,
977 				      0);
978 
979 	ret = clk_hw_register_kunit(test, NULL, &ctx->parent_ctx.hw);
980 	if (ret)
981 		return ret;
982 
983 	ctx->hw.init = CLK_HW_INIT("test-clk", "parent-clk",
984 				   &clk_dummy_single_parent_ops,
985 				   CLK_SET_RATE_PARENT);
986 
987 	ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
988 	if (ret)
989 		return ret;
990 
991 	return 0;
992 }
993 
994 static void
clk_single_parent_mux_test_exit(struct kunit * test)995 clk_single_parent_mux_test_exit(struct kunit *test)
996 {
997 	struct clk_single_parent_ctx *ctx = test->priv;
998 
999 	clk_hw_unregister(&ctx->hw);
1000 	clk_hw_unregister(&ctx->parent_ctx.hw);
1001 }
1002 
1003 /*
1004  * Test that for a clock with a single parent, clk_get_parent() actually
1005  * returns the parent.
1006  */
1007 static void
clk_test_single_parent_mux_get_parent(struct kunit * test)1008 clk_test_single_parent_mux_get_parent(struct kunit *test)
1009 {
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);
1014 
1015 	KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
1016 
1017 	clk_put(parent);
1018 	clk_put(clk);
1019 }
1020 
1021 /*
1022  * Test that for a clock with a single parent, clk_has_parent() actually
1023  * reports it as a parent.
1024  */
1025 static void
clk_test_single_parent_mux_has_parent(struct kunit * test)1026 clk_test_single_parent_mux_has_parent(struct kunit *test)
1027 {
1028 	struct clk_single_parent_ctx *ctx = test->priv;
1029 	struct clk_hw *hw = &ctx->hw;
1030 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1031 	struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
1032 
1033 	KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
1034 
1035 	clk_put(parent);
1036 	clk_put(clk);
1037 }
1038 
1039 /*
1040  * Test that for a clock that can't modify its rate and with a single
1041  * parent, if we set disjoints range on the parent and then the child,
1042  * the second will return an error.
1043  *
1044  * FIXME: clk_set_rate_range() only considers the current clock when
1045  * evaluating whether ranges are disjoints and not the upstream clocks
1046  * ranges.
1047  */
1048 static void
clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit * test)1049 clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
1050 {
1051 	struct clk_single_parent_ctx *ctx = test->priv;
1052 	struct clk_hw *hw = &ctx->hw;
1053 	struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
1054 	struct clk *parent;
1055 	int ret;
1056 
1057 	kunit_skip(test, "This needs to be fixed in the core.");
1058 
1059 	parent = clk_get_parent(clk);
1060 	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1061 
1062 	ret = clk_set_rate_range(parent, 1000, 2000);
1063 	KUNIT_ASSERT_EQ(test, ret, 0);
1064 
1065 	ret = clk_set_rate_range(clk, 3000, 4000);
1066 	KUNIT_EXPECT_LT(test, ret, 0);
1067 }
1068 
1069 /*
1070  * Test that for a clock that can't modify its rate and with a single
1071  * parent, if we set disjoints range on the child and then the parent,
1072  * the second will return an error.
1073  *
1074  * FIXME: clk_set_rate_range() only considers the current clock when
1075  * evaluating whether ranges are disjoints and not the downstream clocks
1076  * ranges.
1077  */
1078 static void
clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit * test)1079 clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
1080 {
1081 	struct clk_single_parent_ctx *ctx = test->priv;
1082 	struct clk_hw *hw = &ctx->hw;
1083 	struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
1084 	struct clk *parent;
1085 	int ret;
1086 
1087 	kunit_skip(test, "This needs to be fixed in the core.");
1088 
1089 	parent = clk_get_parent(clk);
1090 	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1091 
1092 	ret = clk_set_rate_range(clk, 1000, 2000);
1093 	KUNIT_ASSERT_EQ(test, ret, 0);
1094 
1095 	ret = clk_set_rate_range(parent, 3000, 4000);
1096 	KUNIT_EXPECT_LT(test, ret, 0);
1097 }
1098 
1099 /*
1100  * Test that for a clock that can't modify its rate and with a single
1101  * parent, if we set a range on the parent and then call
1102  * clk_round_rate(), the boundaries of the parent are taken into
1103  * account.
1104  */
1105 static void
clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit * test)1106 clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test)
1107 {
1108 	struct clk_single_parent_ctx *ctx = test->priv;
1109 	struct clk_hw *hw = &ctx->hw;
1110 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1111 	struct clk *parent;
1112 	long rate;
1113 	int ret;
1114 
1115 	parent = clk_get_parent(clk);
1116 	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1117 
1118 	ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1119 	KUNIT_ASSERT_EQ(test, ret, 0);
1120 
1121 	rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1122 	KUNIT_ASSERT_GT(test, rate, 0);
1123 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1124 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1125 
1126 	clk_put(clk);
1127 }
1128 
1129 /*
1130  * Test that for a clock that can't modify its rate and with a single
1131  * parent, if we set a range on the parent and a more restrictive one on
1132  * the child, and then call clk_round_rate(), the boundaries of the
1133  * two clocks are taken into account.
1134  */
1135 static void
clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit * test)1136 clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test)
1137 {
1138 	struct clk_single_parent_ctx *ctx = test->priv;
1139 	struct clk_hw *hw = &ctx->hw;
1140 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1141 	struct clk *parent;
1142 	long rate;
1143 	int ret;
1144 
1145 	parent = clk_get_parent(clk);
1146 	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1147 
1148 	ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1149 	KUNIT_ASSERT_EQ(test, ret, 0);
1150 
1151 	ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1152 	KUNIT_ASSERT_EQ(test, ret, 0);
1153 
1154 	rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1155 	KUNIT_ASSERT_GT(test, rate, 0);
1156 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1157 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1158 
1159 	rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1160 	KUNIT_ASSERT_GT(test, rate, 0);
1161 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1162 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1163 
1164 	clk_put(clk);
1165 }
1166 
1167 /*
1168  * Test that for a clock that can't modify its rate and with a single
1169  * parent, if we set a range on the child and a more restrictive one on
1170  * the parent, and then call clk_round_rate(), the boundaries of the
1171  * two clocks are taken into account.
1172  */
1173 static void
clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit * test)1174 clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test)
1175 {
1176 	struct clk_single_parent_ctx *ctx = test->priv;
1177 	struct clk_hw *hw = &ctx->hw;
1178 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1179 	struct clk *parent;
1180 	long rate;
1181 	int ret;
1182 
1183 	parent = clk_get_parent(clk);
1184 	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
1185 
1186 	ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
1187 	KUNIT_ASSERT_EQ(test, ret, 0);
1188 
1189 	ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
1190 	KUNIT_ASSERT_EQ(test, ret, 0);
1191 
1192 	rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1193 	KUNIT_ASSERT_GT(test, rate, 0);
1194 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1195 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1196 
1197 	rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1198 	KUNIT_ASSERT_GT(test, rate, 0);
1199 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
1200 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1201 
1202 	clk_put(clk);
1203 }
1204 
1205 static struct kunit_case clk_single_parent_mux_test_cases[] = {
1206 	KUNIT_CASE(clk_test_single_parent_mux_get_parent),
1207 	KUNIT_CASE(clk_test_single_parent_mux_has_parent),
1208 	KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
1209 	KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
1210 	KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),
1211 	KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only),
1212 	KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller),
1213 	{}
1214 };
1215 
1216 /*
1217  * Test suite for a basic mux clock with one parent, with
1218  * CLK_SET_RATE_PARENT on the child.
1219  *
1220  * These tests exercise the consumer API and check that the state of the
1221  * child and parent are sane and consistent.
1222  */
1223 static struct kunit_suite
1224 clk_single_parent_mux_test_suite = {
1225 	.name = "clk-single-parent-mux-test",
1226 	.init = clk_single_parent_mux_test_init,
1227 	.test_cases = clk_single_parent_mux_test_cases,
1228 };
1229 
clk_orphan_transparent_single_parent_mux_test_init(struct kunit * test)1230 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
1231 {
1232 	struct clk_single_parent_ctx *ctx;
1233 	struct clk_init_data init = { };
1234 	const char * const parents[] = { "orphan_parent" };
1235 	int ret;
1236 
1237 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1238 	if (!ctx)
1239 		return -ENOMEM;
1240 	test->priv = ctx;
1241 
1242 	init.name = "test_orphan_dummy_parent";
1243 	init.ops = &clk_dummy_single_parent_ops;
1244 	init.parent_names = parents;
1245 	init.num_parents = ARRAY_SIZE(parents);
1246 	init.flags = CLK_SET_RATE_PARENT;
1247 	ctx->hw.init = &init;
1248 
1249 	ret = clk_hw_register(NULL, &ctx->hw);
1250 	if (ret)
1251 		return ret;
1252 
1253 	memset(&init, 0, sizeof(init));
1254 	init.name = "orphan_parent";
1255 	init.ops = &clk_dummy_rate_ops;
1256 	ctx->parent_ctx.hw.init = &init;
1257 	ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1258 
1259 	ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1260 	if (ret)
1261 		return ret;
1262 
1263 	return 0;
1264 }
1265 
1266 /*
1267  * Test that a mux-only clock, with an initial rate within a range,
1268  * will still have the same rate after the range has been enforced.
1269  *
1270  * See:
1271  * https://lore.kernel.org/linux-clk/7720158d-10a7-a17b-73a4-a8615c9c6d5c@collabora.com/
1272  */
clk_test_orphan_transparent_parent_mux_set_range(struct kunit * test)1273 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
1274 {
1275 	struct clk_single_parent_ctx *ctx = test->priv;
1276 	struct clk_hw *hw = &ctx->hw;
1277 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1278 	unsigned long rate, new_rate;
1279 
1280 	rate = clk_get_rate(clk);
1281 	KUNIT_ASSERT_GT(test, rate, 0);
1282 
1283 	KUNIT_ASSERT_EQ(test,
1284 			clk_set_rate_range(clk,
1285 					   ctx->parent_ctx.rate - 1000,
1286 					   ctx->parent_ctx.rate + 1000),
1287 			0);
1288 
1289 	new_rate = clk_get_rate(clk);
1290 	KUNIT_ASSERT_GT(test, new_rate, 0);
1291 	KUNIT_EXPECT_EQ(test, rate, new_rate);
1292 
1293 	clk_put(clk);
1294 }
1295 
1296 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
1297 	KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
1298 	{}
1299 };
1300 
1301 /*
1302  * Test suite for a basic mux clock with one parent. The parent is
1303  * registered after its child. The clock will thus be an orphan when
1304  * registered, but will no longer be when the tests run.
1305  *
1306  * These tests make sure a clock that used to be orphan has a sane,
1307  * consistent, behaviour.
1308  */
1309 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
1310 	.name = "clk-orphan-transparent-single-parent-test",
1311 	.init = clk_orphan_transparent_single_parent_mux_test_init,
1312 	.exit = clk_single_parent_mux_test_exit,
1313 	.test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
1314 };
1315 
1316 struct clk_single_parent_two_lvl_ctx {
1317 	struct clk_dummy_context parent_parent_ctx;
1318 	struct clk_dummy_context parent_ctx;
1319 	struct clk_hw hw;
1320 };
1321 
1322 static int
clk_orphan_two_level_root_last_test_init(struct kunit * test)1323 clk_orphan_two_level_root_last_test_init(struct kunit *test)
1324 {
1325 	struct clk_single_parent_two_lvl_ctx *ctx;
1326 	int ret;
1327 
1328 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
1329 	if (!ctx)
1330 		return -ENOMEM;
1331 	test->priv = ctx;
1332 
1333 	ctx->parent_ctx.hw.init =
1334 		CLK_HW_INIT("intermediate-parent",
1335 			    "root-parent",
1336 			    &clk_dummy_single_parent_ops,
1337 			    CLK_SET_RATE_PARENT);
1338 	ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
1339 	if (ret)
1340 		return ret;
1341 
1342 	ctx->hw.init =
1343 		CLK_HW_INIT("test-clk", "intermediate-parent",
1344 			    &clk_dummy_single_parent_ops,
1345 			    CLK_SET_RATE_PARENT);
1346 	ret = clk_hw_register(NULL, &ctx->hw);
1347 	if (ret)
1348 		return ret;
1349 
1350 	ctx->parent_parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
1351 	ctx->parent_parent_ctx.hw.init =
1352 		CLK_HW_INIT_NO_PARENT("root-parent",
1353 				      &clk_dummy_rate_ops,
1354 				      0);
1355 	ret = clk_hw_register(NULL, &ctx->parent_parent_ctx.hw);
1356 	if (ret)
1357 		return ret;
1358 
1359 	return 0;
1360 }
1361 
1362 static void
clk_orphan_two_level_root_last_test_exit(struct kunit * test)1363 clk_orphan_two_level_root_last_test_exit(struct kunit *test)
1364 {
1365 	struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1366 
1367 	clk_hw_unregister(&ctx->hw);
1368 	clk_hw_unregister(&ctx->parent_ctx.hw);
1369 	clk_hw_unregister(&ctx->parent_parent_ctx.hw);
1370 }
1371 
1372 /*
1373  * Test that, for a clock whose parent used to be orphan, clk_get_rate()
1374  * will return the proper rate.
1375  */
1376 static void
clk_orphan_two_level_root_last_test_get_rate(struct kunit * test)1377 clk_orphan_two_level_root_last_test_get_rate(struct kunit *test)
1378 {
1379 	struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1380 	struct clk_hw *hw = &ctx->hw;
1381 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1382 	unsigned long rate;
1383 
1384 	rate = clk_get_rate(clk);
1385 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1386 
1387 	clk_put(clk);
1388 }
1389 
1390 /*
1391  * Test that, for a clock whose parent used to be orphan,
1392  * clk_set_rate_range() won't affect its rate if it is already within
1393  * range.
1394  *
1395  * See (for Exynos 4210):
1396  * https://lore.kernel.org/linux-clk/366a0232-bb4a-c357-6aa8-636e398e05eb@samsung.com/
1397  */
1398 static void
clk_orphan_two_level_root_last_test_set_range(struct kunit * test)1399 clk_orphan_two_level_root_last_test_set_range(struct kunit *test)
1400 {
1401 	struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
1402 	struct clk_hw *hw = &ctx->hw;
1403 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1404 	unsigned long rate;
1405 	int ret;
1406 
1407 	ret = clk_set_rate_range(clk,
1408 				 DUMMY_CLOCK_INIT_RATE - 1000,
1409 				 DUMMY_CLOCK_INIT_RATE + 1000);
1410 	KUNIT_ASSERT_EQ(test, ret, 0);
1411 
1412 	rate = clk_get_rate(clk);
1413 	KUNIT_ASSERT_GT(test, rate, 0);
1414 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
1415 
1416 	clk_put(clk);
1417 }
1418 
1419 static struct kunit_case
1420 clk_orphan_two_level_root_last_test_cases[] = {
1421 	KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate),
1422 	KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range),
1423 	{}
1424 };
1425 
1426 /*
1427  * Test suite for a basic, transparent, clock with a parent that is also
1428  * such a clock. The parent's parent is registered last, while the
1429  * parent and its child are registered in that order. The intermediate
1430  * and leaf clocks will thus be orphan when registered, but the leaf
1431  * clock itself will always have its parent and will never be
1432  * reparented. Indeed, it's only orphan because its parent is.
1433  *
1434  * These tests exercise the behaviour of the consumer API when dealing
1435  * with an orphan clock, and how we deal with the transition to a valid
1436  * parent.
1437  */
1438 static struct kunit_suite
1439 clk_orphan_two_level_root_last_test_suite = {
1440 	.name = "clk-orphan-two-level-root-last-test",
1441 	.init = clk_orphan_two_level_root_last_test_init,
1442 	.exit = clk_orphan_two_level_root_last_test_exit,
1443 	.test_cases = clk_orphan_two_level_root_last_test_cases,
1444 };
1445 
1446 /*
1447  * Test that clk_set_rate_range won't return an error for a valid range
1448  * and that it will make sure the rate of the clock is within the
1449  * boundaries.
1450  */
clk_range_test_set_range(struct kunit * test)1451 static void clk_range_test_set_range(struct kunit *test)
1452 {
1453 	struct clk_dummy_context *ctx = test->priv;
1454 	struct clk_hw *hw = &ctx->hw;
1455 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1456 	unsigned long rate;
1457 
1458 	KUNIT_ASSERT_EQ(test,
1459 			clk_set_rate_range(clk,
1460 					   DUMMY_CLOCK_RATE_1,
1461 					   DUMMY_CLOCK_RATE_2),
1462 			0);
1463 
1464 	rate = clk_get_rate(clk);
1465 	KUNIT_ASSERT_GT(test, rate, 0);
1466 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1467 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1468 
1469 	clk_put(clk);
1470 }
1471 
1472 /*
1473  * Test that calling clk_set_rate_range with a minimum rate higher than
1474  * the maximum rate returns an error.
1475  */
clk_range_test_set_range_invalid(struct kunit * test)1476 static void clk_range_test_set_range_invalid(struct kunit *test)
1477 {
1478 	struct clk_dummy_context *ctx = test->priv;
1479 	struct clk_hw *hw = &ctx->hw;
1480 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1481 
1482 	KUNIT_EXPECT_LT(test,
1483 			clk_set_rate_range(clk,
1484 					   DUMMY_CLOCK_RATE_1 + 1000,
1485 					   DUMMY_CLOCK_RATE_1),
1486 			0);
1487 
1488 	clk_put(clk);
1489 }
1490 
1491 /*
1492  * Test that users can't set multiple, disjoints, range that would be
1493  * impossible to meet.
1494  */
clk_range_test_multiple_disjoints_range(struct kunit * test)1495 static void clk_range_test_multiple_disjoints_range(struct kunit *test)
1496 {
1497 	struct clk_dummy_context *ctx = test->priv;
1498 	struct clk_hw *hw = &ctx->hw;
1499 	struct clk *user1, *user2;
1500 
1501 	user1 = clk_hw_get_clk(hw, NULL);
1502 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1503 
1504 	user2 = clk_hw_get_clk(hw, NULL);
1505 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1506 
1507 	KUNIT_ASSERT_EQ(test,
1508 			clk_set_rate_range(user1, 1000, 2000),
1509 			0);
1510 
1511 	KUNIT_EXPECT_LT(test,
1512 			clk_set_rate_range(user2, 3000, 4000),
1513 			0);
1514 
1515 	clk_put(user2);
1516 	clk_put(user1);
1517 }
1518 
1519 /*
1520  * Test that if our clock has some boundaries and we try to round a rate
1521  * lower than the minimum, the returned rate will be within range.
1522  */
clk_range_test_set_range_round_rate_lower(struct kunit * test)1523 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
1524 {
1525 	struct clk_dummy_context *ctx = test->priv;
1526 	struct clk_hw *hw = &ctx->hw;
1527 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1528 	long rate;
1529 
1530 	KUNIT_ASSERT_EQ(test,
1531 			clk_set_rate_range(clk,
1532 					   DUMMY_CLOCK_RATE_1,
1533 					   DUMMY_CLOCK_RATE_2),
1534 			0);
1535 
1536 	rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1537 	KUNIT_ASSERT_GT(test, rate, 0);
1538 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1539 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1540 
1541 	clk_put(clk);
1542 }
1543 
1544 /*
1545  * Test that if our clock has some boundaries and we try to set a rate
1546  * higher than the maximum, the new rate will be within range.
1547  */
clk_range_test_set_range_set_rate_lower(struct kunit * test)1548 static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
1549 {
1550 	struct clk_dummy_context *ctx = test->priv;
1551 	struct clk_hw *hw = &ctx->hw;
1552 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1553 	unsigned long rate;
1554 
1555 	KUNIT_ASSERT_EQ(test,
1556 			clk_set_rate_range(clk,
1557 					   DUMMY_CLOCK_RATE_1,
1558 					   DUMMY_CLOCK_RATE_2),
1559 			0);
1560 
1561 	KUNIT_ASSERT_EQ(test,
1562 			clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1563 			0);
1564 
1565 	rate = clk_get_rate(clk);
1566 	KUNIT_ASSERT_GT(test, rate, 0);
1567 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1568 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1569 
1570 	clk_put(clk);
1571 }
1572 
1573 /*
1574  * Test that if our clock has some boundaries and we try to round and
1575  * set a rate lower than the minimum, the rate returned by
1576  * clk_round_rate() will be consistent with the new rate set by
1577  * clk_set_rate().
1578  */
clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit * test)1579 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
1580 {
1581 	struct clk_dummy_context *ctx = test->priv;
1582 	struct clk_hw *hw = &ctx->hw;
1583 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1584 	long rounded;
1585 
1586 	KUNIT_ASSERT_EQ(test,
1587 			clk_set_rate_range(clk,
1588 					   DUMMY_CLOCK_RATE_1,
1589 					   DUMMY_CLOCK_RATE_2),
1590 			0);
1591 
1592 	rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
1593 	KUNIT_ASSERT_GT(test, rounded, 0);
1594 
1595 	KUNIT_ASSERT_EQ(test,
1596 			clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1597 			0);
1598 
1599 	KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1600 
1601 	clk_put(clk);
1602 }
1603 
1604 /*
1605  * Test that if our clock has some boundaries and we try to round a rate
1606  * higher than the maximum, the returned rate will be within range.
1607  */
clk_range_test_set_range_round_rate_higher(struct kunit * test)1608 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
1609 {
1610 	struct clk_dummy_context *ctx = test->priv;
1611 	struct clk_hw *hw = &ctx->hw;
1612 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1613 	long rate;
1614 
1615 	KUNIT_ASSERT_EQ(test,
1616 			clk_set_rate_range(clk,
1617 					   DUMMY_CLOCK_RATE_1,
1618 					   DUMMY_CLOCK_RATE_2),
1619 			0);
1620 
1621 	rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1622 	KUNIT_ASSERT_GT(test, rate, 0);
1623 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1624 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1625 
1626 	clk_put(clk);
1627 }
1628 
1629 /*
1630  * Test that if our clock has some boundaries and we try to set a rate
1631  * higher than the maximum, the new rate will be within range.
1632  */
clk_range_test_set_range_set_rate_higher(struct kunit * test)1633 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
1634 {
1635 	struct clk_dummy_context *ctx = test->priv;
1636 	struct clk_hw *hw = &ctx->hw;
1637 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1638 	unsigned long rate;
1639 
1640 	KUNIT_ASSERT_EQ(test,
1641 			clk_set_rate_range(clk,
1642 					   DUMMY_CLOCK_RATE_1,
1643 					   DUMMY_CLOCK_RATE_2),
1644 			0);
1645 
1646 	KUNIT_ASSERT_EQ(test,
1647 			clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1648 			0);
1649 
1650 	rate = clk_get_rate(clk);
1651 	KUNIT_ASSERT_GT(test, rate, 0);
1652 	KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
1653 	KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
1654 
1655 	clk_put(clk);
1656 }
1657 
1658 /*
1659  * Test that if our clock has some boundaries and we try to round and
1660  * set a rate higher than the maximum, the rate returned by
1661  * clk_round_rate() will be consistent with the new rate set by
1662  * clk_set_rate().
1663  */
clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit * test)1664 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
1665 {
1666 	struct clk_dummy_context *ctx = test->priv;
1667 	struct clk_hw *hw = &ctx->hw;
1668 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1669 	long rounded;
1670 
1671 	KUNIT_ASSERT_EQ(test,
1672 			clk_set_rate_range(clk,
1673 					   DUMMY_CLOCK_RATE_1,
1674 					   DUMMY_CLOCK_RATE_2),
1675 			0);
1676 
1677 	rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
1678 	KUNIT_ASSERT_GT(test, rounded, 0);
1679 
1680 	KUNIT_ASSERT_EQ(test,
1681 			clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1682 			0);
1683 
1684 	KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
1685 
1686 	clk_put(clk);
1687 }
1688 
1689 /*
1690  * Test that if our clock has a rate lower than the minimum set by a
1691  * call to clk_set_rate_range(), the rate will be raised to match the
1692  * new minimum.
1693  *
1694  * This assumes that clk_ops.determine_rate won't modify the requested rate,
1695  * which is our case in clk_dummy_rate_ops.
1696  */
clk_range_test_set_range_get_rate_raised(struct kunit * test)1697 static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
1698 {
1699 	struct clk_dummy_context *ctx = test->priv;
1700 	struct clk_hw *hw = &ctx->hw;
1701 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1702 	unsigned long rate;
1703 
1704 	KUNIT_ASSERT_EQ(test,
1705 			clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1706 			0);
1707 
1708 	KUNIT_ASSERT_EQ(test,
1709 			clk_set_rate_range(clk,
1710 					   DUMMY_CLOCK_RATE_1,
1711 					   DUMMY_CLOCK_RATE_2),
1712 			0);
1713 
1714 	rate = clk_get_rate(clk);
1715 	KUNIT_ASSERT_GT(test, rate, 0);
1716 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1717 
1718 	clk_put(clk);
1719 }
1720 
1721 /*
1722  * Test that if our clock has a rate higher than the maximum set by a
1723  * call to clk_set_rate_range(), the rate will be lowered to match the
1724  * new maximum.
1725  *
1726  * This assumes that clk_ops.determine_rate won't modify the requested rate,
1727  * which is our case in clk_dummy_rate_ops.
1728  */
clk_range_test_set_range_get_rate_lowered(struct kunit * test)1729 static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
1730 {
1731 	struct clk_dummy_context *ctx = test->priv;
1732 	struct clk_hw *hw = &ctx->hw;
1733 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1734 	unsigned long rate;
1735 
1736 	KUNIT_ASSERT_EQ(test,
1737 			clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1738 			0);
1739 
1740 	KUNIT_ASSERT_EQ(test,
1741 			clk_set_rate_range(clk,
1742 					   DUMMY_CLOCK_RATE_1,
1743 					   DUMMY_CLOCK_RATE_2),
1744 			0);
1745 
1746 	rate = clk_get_rate(clk);
1747 	KUNIT_ASSERT_GT(test, rate, 0);
1748 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1749 
1750 	clk_put(clk);
1751 }
1752 
1753 static struct kunit_case clk_range_test_cases[] = {
1754 	KUNIT_CASE(clk_range_test_set_range),
1755 	KUNIT_CASE(clk_range_test_set_range_invalid),
1756 	KUNIT_CASE(clk_range_test_multiple_disjoints_range),
1757 	KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
1758 	KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
1759 	KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
1760 	KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
1761 	KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
1762 	KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
1763 	KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
1764 	KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
1765 	{}
1766 };
1767 
1768 /*
1769  * Test suite for a basic rate clock, without any parent.
1770  *
1771  * These tests exercise the rate range API: clk_set_rate_range(),
1772  * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range().
1773  */
1774 static struct kunit_suite clk_range_test_suite = {
1775 	.name = "clk-range-test",
1776 	.init = clk_test_init,
1777 	.exit = clk_test_exit,
1778 	.test_cases = clk_range_test_cases,
1779 };
1780 
1781 /*
1782  * Test that if we have several subsequent calls to
1783  * clk_set_rate_range(), the core will reevaluate whether a new rate is
1784  * needed each and every time.
1785  *
1786  * With clk_dummy_maximize_rate_ops, this means that the rate will
1787  * trail along the maximum as it evolves.
1788  */
clk_range_test_set_range_rate_maximized(struct kunit * test)1789 static void clk_range_test_set_range_rate_maximized(struct kunit *test)
1790 {
1791 	struct clk_dummy_context *ctx = test->priv;
1792 	struct clk_hw *hw = &ctx->hw;
1793 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1794 	unsigned long rate;
1795 
1796 	KUNIT_ASSERT_EQ(test,
1797 			clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1798 			0);
1799 
1800 	KUNIT_ASSERT_EQ(test,
1801 			clk_set_rate_range(clk,
1802 					   DUMMY_CLOCK_RATE_1,
1803 					   DUMMY_CLOCK_RATE_2),
1804 			0);
1805 
1806 	rate = clk_get_rate(clk);
1807 	KUNIT_ASSERT_GT(test, rate, 0);
1808 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1809 
1810 	KUNIT_ASSERT_EQ(test,
1811 			clk_set_rate_range(clk,
1812 					   DUMMY_CLOCK_RATE_1,
1813 					   DUMMY_CLOCK_RATE_2 - 1000),
1814 			0);
1815 
1816 	rate = clk_get_rate(clk);
1817 	KUNIT_ASSERT_GT(test, rate, 0);
1818 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
1819 
1820 	KUNIT_ASSERT_EQ(test,
1821 			clk_set_rate_range(clk,
1822 					   DUMMY_CLOCK_RATE_1,
1823 					   DUMMY_CLOCK_RATE_2),
1824 			0);
1825 
1826 	rate = clk_get_rate(clk);
1827 	KUNIT_ASSERT_GT(test, rate, 0);
1828 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1829 
1830 	clk_put(clk);
1831 }
1832 
1833 /*
1834  * Test that if we have several subsequent calls to
1835  * clk_set_rate_range(), across multiple users, the core will reevaluate
1836  * whether a new rate is needed each and every time.
1837  *
1838  * With clk_dummy_maximize_rate_ops, this means that the rate will
1839  * trail along the maximum as it evolves.
1840  */
clk_range_test_multiple_set_range_rate_maximized(struct kunit * test)1841 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
1842 {
1843 	struct clk_dummy_context *ctx = test->priv;
1844 	struct clk_hw *hw = &ctx->hw;
1845 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1846 	struct clk *user1, *user2;
1847 	unsigned long rate;
1848 
1849 	user1 = clk_hw_get_clk(hw, NULL);
1850 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1851 
1852 	user2 = clk_hw_get_clk(hw, NULL);
1853 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1854 
1855 	KUNIT_ASSERT_EQ(test,
1856 			clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1857 			0);
1858 
1859 	KUNIT_ASSERT_EQ(test,
1860 			clk_set_rate_range(user1,
1861 					   0,
1862 					   DUMMY_CLOCK_RATE_2),
1863 			0);
1864 
1865 	rate = clk_get_rate(clk);
1866 	KUNIT_ASSERT_GT(test, rate, 0);
1867 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1868 
1869 	KUNIT_ASSERT_EQ(test,
1870 			clk_set_rate_range(user2,
1871 					   0,
1872 					   DUMMY_CLOCK_RATE_1),
1873 			0);
1874 
1875 	rate = clk_get_rate(clk);
1876 	KUNIT_ASSERT_GT(test, rate, 0);
1877 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1878 
1879 	KUNIT_ASSERT_EQ(test,
1880 			clk_drop_range(user2),
1881 			0);
1882 
1883 	rate = clk_get_rate(clk);
1884 	KUNIT_ASSERT_GT(test, rate, 0);
1885 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1886 
1887 	clk_put(user2);
1888 	clk_put(user1);
1889 	clk_put(clk);
1890 }
1891 
1892 /*
1893  * Test that if we have several subsequent calls to
1894  * clk_set_rate_range(), across multiple users, the core will reevaluate
1895  * whether a new rate is needed, including when a user drop its clock.
1896  *
1897  * With clk_dummy_maximize_rate_ops, this means that the rate will
1898  * trail along the maximum as it evolves.
1899  */
clk_range_test_multiple_set_range_rate_put_maximized(struct kunit * test)1900 static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
1901 {
1902 	struct clk_dummy_context *ctx = test->priv;
1903 	struct clk_hw *hw = &ctx->hw;
1904 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1905 	struct clk *user1, *user2;
1906 	unsigned long rate;
1907 
1908 	user1 = clk_hw_get_clk(hw, NULL);
1909 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
1910 
1911 	user2 = clk_hw_get_clk(hw, NULL);
1912 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
1913 
1914 	KUNIT_ASSERT_EQ(test,
1915 			clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
1916 			0);
1917 
1918 	KUNIT_ASSERT_EQ(test,
1919 			clk_set_rate_range(user1,
1920 					   0,
1921 					   DUMMY_CLOCK_RATE_2),
1922 			0);
1923 
1924 	rate = clk_get_rate(clk);
1925 	KUNIT_ASSERT_GT(test, rate, 0);
1926 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1927 
1928 	KUNIT_ASSERT_EQ(test,
1929 			clk_set_rate_range(user2,
1930 					   0,
1931 					   DUMMY_CLOCK_RATE_1),
1932 			0);
1933 
1934 	rate = clk_get_rate(clk);
1935 	KUNIT_ASSERT_GT(test, rate, 0);
1936 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1937 
1938 	clk_put(user2);
1939 
1940 	rate = clk_get_rate(clk);
1941 	KUNIT_ASSERT_GT(test, rate, 0);
1942 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
1943 
1944 	clk_put(user1);
1945 	clk_put(clk);
1946 }
1947 
1948 static struct kunit_case clk_range_maximize_test_cases[] = {
1949 	KUNIT_CASE(clk_range_test_set_range_rate_maximized),
1950 	KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
1951 	KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
1952 	{}
1953 };
1954 
1955 /*
1956  * Test suite for a basic rate clock, without any parent.
1957  *
1958  * These tests exercise the rate range API: clk_set_rate_range(),
1959  * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
1960  * driver that will always try to run at the highest possible rate.
1961  */
1962 static struct kunit_suite clk_range_maximize_test_suite = {
1963 	.name = "clk-range-maximize-test",
1964 	.init = clk_maximize_test_init,
1965 	.exit = clk_test_exit,
1966 	.test_cases = clk_range_maximize_test_cases,
1967 };
1968 
1969 /*
1970  * Test that if we have several subsequent calls to
1971  * clk_set_rate_range(), the core will reevaluate whether a new rate is
1972  * needed each and every time.
1973  *
1974  * With clk_dummy_minimize_rate_ops, this means that the rate will
1975  * trail along the minimum as it evolves.
1976  */
clk_range_test_set_range_rate_minimized(struct kunit * test)1977 static void clk_range_test_set_range_rate_minimized(struct kunit *test)
1978 {
1979 	struct clk_dummy_context *ctx = test->priv;
1980 	struct clk_hw *hw = &ctx->hw;
1981 	struct clk *clk = clk_hw_get_clk(hw, NULL);
1982 	unsigned long rate;
1983 
1984 	KUNIT_ASSERT_EQ(test,
1985 			clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
1986 			0);
1987 
1988 	KUNIT_ASSERT_EQ(test,
1989 			clk_set_rate_range(clk,
1990 					   DUMMY_CLOCK_RATE_1,
1991 					   DUMMY_CLOCK_RATE_2),
1992 			0);
1993 
1994 	rate = clk_get_rate(clk);
1995 	KUNIT_ASSERT_GT(test, rate, 0);
1996 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
1997 
1998 	KUNIT_ASSERT_EQ(test,
1999 			clk_set_rate_range(clk,
2000 					   DUMMY_CLOCK_RATE_1 + 1000,
2001 					   DUMMY_CLOCK_RATE_2),
2002 			0);
2003 
2004 	rate = clk_get_rate(clk);
2005 	KUNIT_ASSERT_GT(test, rate, 0);
2006 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
2007 
2008 	KUNIT_ASSERT_EQ(test,
2009 			clk_set_rate_range(clk,
2010 					   DUMMY_CLOCK_RATE_1,
2011 					   DUMMY_CLOCK_RATE_2),
2012 			0);
2013 
2014 	rate = clk_get_rate(clk);
2015 	KUNIT_ASSERT_GT(test, rate, 0);
2016 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2017 
2018 	clk_put(clk);
2019 }
2020 
2021 /*
2022  * Test that if we have several subsequent calls to
2023  * clk_set_rate_range(), across multiple users, the core will reevaluate
2024  * whether a new rate is needed each and every time.
2025  *
2026  * With clk_dummy_minimize_rate_ops, this means that the rate will
2027  * trail along the minimum as it evolves.
2028  */
clk_range_test_multiple_set_range_rate_minimized(struct kunit * test)2029 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
2030 {
2031 	struct clk_dummy_context *ctx = test->priv;
2032 	struct clk_hw *hw = &ctx->hw;
2033 	struct clk *clk = clk_hw_get_clk(hw, NULL);
2034 	struct clk *user1, *user2;
2035 	unsigned long rate;
2036 
2037 	user1 = clk_hw_get_clk(hw, NULL);
2038 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2039 
2040 	user2 = clk_hw_get_clk(hw, NULL);
2041 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2042 
2043 	KUNIT_ASSERT_EQ(test,
2044 			clk_set_rate_range(user1,
2045 					   DUMMY_CLOCK_RATE_1,
2046 					   ULONG_MAX),
2047 			0);
2048 
2049 	rate = clk_get_rate(clk);
2050 	KUNIT_ASSERT_GT(test, rate, 0);
2051 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2052 
2053 	KUNIT_ASSERT_EQ(test,
2054 			clk_set_rate_range(user2,
2055 					   DUMMY_CLOCK_RATE_2,
2056 					   ULONG_MAX),
2057 			0);
2058 
2059 	rate = clk_get_rate(clk);
2060 	KUNIT_ASSERT_GT(test, rate, 0);
2061 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2062 
2063 	KUNIT_ASSERT_EQ(test,
2064 			clk_drop_range(user2),
2065 			0);
2066 
2067 	rate = clk_get_rate(clk);
2068 	KUNIT_ASSERT_GT(test, rate, 0);
2069 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2070 
2071 	clk_put(user2);
2072 	clk_put(user1);
2073 	clk_put(clk);
2074 }
2075 
2076 /*
2077  * Test that if we have several subsequent calls to
2078  * clk_set_rate_range(), across multiple users, the core will reevaluate
2079  * whether a new rate is needed, including when a user drop its clock.
2080  *
2081  * With clk_dummy_minimize_rate_ops, this means that the rate will
2082  * trail along the minimum as it evolves.
2083  */
clk_range_test_multiple_set_range_rate_put_minimized(struct kunit * test)2084 static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
2085 {
2086 	struct clk_dummy_context *ctx = test->priv;
2087 	struct clk_hw *hw = &ctx->hw;
2088 	struct clk *clk = clk_hw_get_clk(hw, NULL);
2089 	struct clk *user1, *user2;
2090 	unsigned long rate;
2091 
2092 	user1 = clk_hw_get_clk(hw, NULL);
2093 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
2094 
2095 	user2 = clk_hw_get_clk(hw, NULL);
2096 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
2097 
2098 	KUNIT_ASSERT_EQ(test,
2099 			clk_set_rate_range(user1,
2100 					   DUMMY_CLOCK_RATE_1,
2101 					   ULONG_MAX),
2102 			0);
2103 
2104 	rate = clk_get_rate(clk);
2105 	KUNIT_ASSERT_GT(test, rate, 0);
2106 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2107 
2108 	KUNIT_ASSERT_EQ(test,
2109 			clk_set_rate_range(user2,
2110 					   DUMMY_CLOCK_RATE_2,
2111 					   ULONG_MAX),
2112 			0);
2113 
2114 	rate = clk_get_rate(clk);
2115 	KUNIT_ASSERT_GT(test, rate, 0);
2116 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
2117 
2118 	clk_put(user2);
2119 
2120 	rate = clk_get_rate(clk);
2121 	KUNIT_ASSERT_GT(test, rate, 0);
2122 	KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2123 
2124 	clk_put(user1);
2125 	clk_put(clk);
2126 }
2127 
2128 static struct kunit_case clk_range_minimize_test_cases[] = {
2129 	KUNIT_CASE(clk_range_test_set_range_rate_minimized),
2130 	KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
2131 	KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
2132 	{}
2133 };
2134 
2135 /*
2136  * Test suite for a basic rate clock, without any parent.
2137  *
2138  * These tests exercise the rate range API: clk_set_rate_range(),
2139  * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
2140  * driver that will always try to run at the lowest possible rate.
2141  */
2142 static struct kunit_suite clk_range_minimize_test_suite = {
2143 	.name = "clk-range-minimize-test",
2144 	.init = clk_minimize_test_init,
2145 	.exit = clk_test_exit,
2146 	.test_cases = clk_range_minimize_test_cases,
2147 };
2148 
2149 struct clk_leaf_mux_ctx {
2150 	struct clk_multiple_parent_ctx mux_ctx;
2151 	struct clk_hw hw;
2152 	struct clk_hw parent;
2153 	struct clk_rate_request *req;
2154 	int (*determine_rate_func)(struct clk_hw *hw, struct clk_rate_request *req);
2155 };
2156 
clk_leaf_mux_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)2157 static int clk_leaf_mux_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
2158 {
2159 	struct clk_leaf_mux_ctx *ctx = container_of(hw, struct clk_leaf_mux_ctx, hw);
2160 	int ret;
2161 	struct clk_rate_request *parent_req = ctx->req;
2162 
2163 	clk_hw_forward_rate_request(hw, req, req->best_parent_hw, parent_req, req->rate);
2164 	ret = ctx->determine_rate_func(req->best_parent_hw, parent_req);
2165 	if (ret)
2166 		return ret;
2167 
2168 	req->rate = parent_req->rate;
2169 
2170 	return 0;
2171 }
2172 
2173 static const struct clk_ops clk_leaf_mux_set_rate_parent_ops = {
2174 	.determine_rate = clk_leaf_mux_determine_rate,
2175 	.set_parent = clk_dummy_single_set_parent,
2176 	.get_parent = clk_dummy_single_get_parent,
2177 };
2178 
2179 static int
clk_leaf_mux_set_rate_parent_test_init(struct kunit * test)2180 clk_leaf_mux_set_rate_parent_test_init(struct kunit *test)
2181 {
2182 	struct clk_leaf_mux_ctx *ctx;
2183 	const char *top_parents[2] = { "parent-0", "parent-1" };
2184 	int ret;
2185 
2186 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2187 	if (!ctx)
2188 		return -ENOMEM;
2189 	test->priv = ctx;
2190 
2191 	ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2192 								    &clk_dummy_rate_ops,
2193 								    0);
2194 	ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2195 	ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2196 	if (ret)
2197 		return ret;
2198 
2199 	ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2200 								    &clk_dummy_rate_ops,
2201 								    0);
2202 	ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2203 	ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2204 	if (ret)
2205 		return ret;
2206 
2207 	ctx->mux_ctx.current_parent = 0;
2208 	ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2209 						   &clk_multiple_parents_mux_ops,
2210 						   0);
2211 	ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2212 	if (ret)
2213 		return ret;
2214 
2215 	ctx->parent.init = CLK_HW_INIT_HW("test-parent", &ctx->mux_ctx.hw,
2216 					  &empty_clk_ops, CLK_SET_RATE_PARENT);
2217 	ret = clk_hw_register(NULL, &ctx->parent);
2218 	if (ret)
2219 		return ret;
2220 
2221 	ctx->hw.init = CLK_HW_INIT_HW("test-clock", &ctx->parent,
2222 				      &clk_leaf_mux_set_rate_parent_ops,
2223 				      CLK_SET_RATE_PARENT);
2224 	ret = clk_hw_register(NULL, &ctx->hw);
2225 	if (ret)
2226 		return ret;
2227 
2228 	return 0;
2229 }
2230 
clk_leaf_mux_set_rate_parent_test_exit(struct kunit * test)2231 static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test)
2232 {
2233 	struct clk_leaf_mux_ctx *ctx = test->priv;
2234 
2235 	clk_hw_unregister(&ctx->hw);
2236 	clk_hw_unregister(&ctx->parent);
2237 	clk_hw_unregister(&ctx->mux_ctx.hw);
2238 	clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2239 	clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2240 }
2241 
2242 struct clk_leaf_mux_set_rate_parent_determine_rate_test_case {
2243 	const char *desc;
2244 	int (*determine_rate_func)(struct clk_hw *hw, struct clk_rate_request *req);
2245 };
2246 
2247 static void
clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc(const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case * t,char * desc)2248 clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc(
2249 		const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *t, char *desc)
2250 {
2251 	strscpy(desc, t->desc, KUNIT_PARAM_DESC_SIZE);
2252 }
2253 
2254 static const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case
2255 clk_leaf_mux_set_rate_parent_determine_rate_test_cases[] = {
2256 	{
2257 		/*
2258 		 * Test that __clk_determine_rate() on the parent that can't
2259 		 * change rate doesn't return a clk_rate_request structure with
2260 		 * the best_parent_hw pointer pointing to the parent.
2261 		 */
2262 		.desc = "clk_leaf_mux_set_rate_parent__clk_determine_rate_proper_parent",
2263 		.determine_rate_func = __clk_determine_rate,
2264 	},
2265 	{
2266 		/*
2267 		 * Test that __clk_mux_determine_rate() on the parent that
2268 		 * can't change rate doesn't return a clk_rate_request
2269 		 * structure with the best_parent_hw pointer pointing to
2270 		 * the parent.
2271 		 */
2272 		.desc = "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_proper_parent",
2273 		.determine_rate_func = __clk_mux_determine_rate,
2274 	},
2275 	{
2276 		/*
2277 		 * Test that __clk_mux_determine_rate_closest() on the parent
2278 		 * that can't change rate doesn't return a clk_rate_request
2279 		 * structure with the best_parent_hw pointer pointing to
2280 		 * the parent.
2281 		 */
2282 		.desc = "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_closest_proper_parent",
2283 		.determine_rate_func = __clk_mux_determine_rate_closest,
2284 	},
2285 	{
2286 		/*
2287 		 * Test that clk_hw_determine_rate_no_reparent() on the parent
2288 		 * that can't change rate doesn't return a clk_rate_request
2289 		 * structure with the best_parent_hw pointer pointing to
2290 		 * the parent.
2291 		 */
2292 		.desc = "clk_leaf_mux_set_rate_parent_clk_hw_determine_rate_no_reparent_proper_parent",
2293 		.determine_rate_func = clk_hw_determine_rate_no_reparent,
2294 	},
2295 };
2296 
KUNIT_ARRAY_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,clk_leaf_mux_set_rate_parent_determine_rate_test_cases,clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc)2297 KUNIT_ARRAY_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,
2298 		  clk_leaf_mux_set_rate_parent_determine_rate_test_cases,
2299 		  clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc)
2300 
2301 /*
2302  * Test that when a clk that can't change rate itself calls a function like
2303  * __clk_determine_rate() on its parent it doesn't get back a clk_rate_request
2304  * structure that has the best_parent_hw pointer point to the clk_hw passed
2305  * into the determine rate function. See commit 262ca38f4b6e ("clk: Stop
2306  * forwarding clk_rate_requests to the parent") for more background.
2307  */
2308 static void clk_leaf_mux_set_rate_parent_determine_rate_test(struct kunit *test)
2309 {
2310 	struct clk_leaf_mux_ctx *ctx = test->priv;
2311 	struct clk_hw *hw = &ctx->hw;
2312 	struct clk *clk = clk_hw_get_clk(hw, NULL);
2313 	struct clk_rate_request req;
2314 	unsigned long rate;
2315 	const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *test_param;
2316 
2317 	test_param = test->param_value;
2318 	ctx->determine_rate_func = test_param->determine_rate_func;
2319 
2320 	ctx->req = &req;
2321 	rate = clk_get_rate(clk);
2322 	KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
2323 	KUNIT_ASSERT_EQ(test, DUMMY_CLOCK_RATE_2, clk_round_rate(clk, DUMMY_CLOCK_RATE_2));
2324 
2325 	KUNIT_EXPECT_EQ(test, req.rate, DUMMY_CLOCK_RATE_2);
2326 	KUNIT_EXPECT_EQ(test, req.best_parent_rate, DUMMY_CLOCK_RATE_2);
2327 	KUNIT_EXPECT_PTR_EQ(test, req.best_parent_hw, &ctx->mux_ctx.hw);
2328 
2329 	clk_put(clk);
2330 }
2331 
2332 static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = {
2333 	KUNIT_CASE_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,
2334 			 clk_leaf_mux_set_rate_parent_determine_rate_test_gen_params),
2335 	{}
2336 };
2337 
2338 /*
2339  * Test suite for a clock whose parent is a pass-through clk whose parent is a
2340  * mux with multiple parents. The leaf and pass-through clocks have the
2341  * CLK_SET_RATE_PARENT flag, and will forward rate requests to the mux, which
2342  * will then select which parent is the best fit for a given rate.
2343  *
2344  * These tests exercise the behaviour of muxes, and the proper selection
2345  * of parents.
2346  */
2347 static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = {
2348 	.name = "clk-leaf-mux-set-rate-parent",
2349 	.init = clk_leaf_mux_set_rate_parent_test_init,
2350 	.exit = clk_leaf_mux_set_rate_parent_test_exit,
2351 	.test_cases = clk_leaf_mux_set_rate_parent_test_cases,
2352 };
2353 
2354 struct clk_mux_notifier_rate_change {
2355 	bool done;
2356 	unsigned long old_rate;
2357 	unsigned long new_rate;
2358 	wait_queue_head_t wq;
2359 };
2360 
2361 struct clk_mux_notifier_ctx {
2362 	struct clk_multiple_parent_ctx mux_ctx;
2363 	struct clk *clk;
2364 	struct notifier_block clk_nb;
2365 	struct clk_mux_notifier_rate_change pre_rate_change;
2366 	struct clk_mux_notifier_rate_change post_rate_change;
2367 };
2368 
2369 #define NOTIFIER_TIMEOUT_MS 100
2370 
clk_mux_notifier_callback(struct notifier_block * nb,unsigned long action,void * data)2371 static int clk_mux_notifier_callback(struct notifier_block *nb,
2372 				     unsigned long action, void *data)
2373 {
2374 	struct clk_notifier_data *clk_data = data;
2375 	struct clk_mux_notifier_ctx *ctx = container_of(nb,
2376 							struct clk_mux_notifier_ctx,
2377 							clk_nb);
2378 
2379 	if (action & PRE_RATE_CHANGE) {
2380 		ctx->pre_rate_change.old_rate = clk_data->old_rate;
2381 		ctx->pre_rate_change.new_rate = clk_data->new_rate;
2382 		ctx->pre_rate_change.done = true;
2383 		wake_up_interruptible(&ctx->pre_rate_change.wq);
2384 	}
2385 
2386 	if (action & POST_RATE_CHANGE) {
2387 		ctx->post_rate_change.old_rate = clk_data->old_rate;
2388 		ctx->post_rate_change.new_rate = clk_data->new_rate;
2389 		ctx->post_rate_change.done = true;
2390 		wake_up_interruptible(&ctx->post_rate_change.wq);
2391 	}
2392 
2393 	return 0;
2394 }
2395 
clk_mux_notifier_test_init(struct kunit * test)2396 static int clk_mux_notifier_test_init(struct kunit *test)
2397 {
2398 	struct clk_mux_notifier_ctx *ctx;
2399 	const char *top_parents[2] = { "parent-0", "parent-1" };
2400 	int ret;
2401 
2402 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2403 	if (!ctx)
2404 		return -ENOMEM;
2405 	test->priv = ctx;
2406 	ctx->clk_nb.notifier_call = clk_mux_notifier_callback;
2407 	init_waitqueue_head(&ctx->pre_rate_change.wq);
2408 	init_waitqueue_head(&ctx->post_rate_change.wq);
2409 
2410 	ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2411 								    &clk_dummy_rate_ops,
2412 								    0);
2413 	ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2414 	ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
2415 	if (ret)
2416 		return ret;
2417 
2418 	ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2419 								    &clk_dummy_rate_ops,
2420 								    0);
2421 	ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2422 	ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
2423 	if (ret)
2424 		return ret;
2425 
2426 	ctx->mux_ctx.current_parent = 0;
2427 	ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
2428 						   &clk_multiple_parents_mux_ops,
2429 						   0);
2430 	ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
2431 	if (ret)
2432 		return ret;
2433 
2434 	ctx->clk = clk_hw_get_clk(&ctx->mux_ctx.hw, NULL);
2435 	ret = clk_notifier_register(ctx->clk, &ctx->clk_nb);
2436 	if (ret)
2437 		return ret;
2438 
2439 	return 0;
2440 }
2441 
clk_mux_notifier_test_exit(struct kunit * test)2442 static void clk_mux_notifier_test_exit(struct kunit *test)
2443 {
2444 	struct clk_mux_notifier_ctx *ctx = test->priv;
2445 	struct clk *clk = ctx->clk;
2446 
2447 	clk_notifier_unregister(clk, &ctx->clk_nb);
2448 	clk_put(clk);
2449 
2450 	clk_hw_unregister(&ctx->mux_ctx.hw);
2451 	clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
2452 	clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
2453 }
2454 
2455 /*
2456  * Test that if the we have a notifier registered on a mux, the core
2457  * will notify us when we switch to another parent, and with the proper
2458  * old and new rates.
2459  */
clk_mux_notifier_set_parent_test(struct kunit * test)2460 static void clk_mux_notifier_set_parent_test(struct kunit *test)
2461 {
2462 	struct clk_mux_notifier_ctx *ctx = test->priv;
2463 	struct clk_hw *hw = &ctx->mux_ctx.hw;
2464 	struct clk *clk = clk_hw_get_clk(hw, NULL);
2465 	struct clk *new_parent = clk_hw_get_clk(&ctx->mux_ctx.parents_ctx[1].hw, NULL);
2466 	int ret;
2467 
2468 	ret = clk_set_parent(clk, new_parent);
2469 	KUNIT_ASSERT_EQ(test, ret, 0);
2470 
2471 	ret = wait_event_interruptible_timeout(ctx->pre_rate_change.wq,
2472 					       ctx->pre_rate_change.done,
2473 					       msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
2474 	KUNIT_ASSERT_GT(test, ret, 0);
2475 
2476 	KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
2477 	KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
2478 
2479 	ret = wait_event_interruptible_timeout(ctx->post_rate_change.wq,
2480 					       ctx->post_rate_change.done,
2481 					       msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
2482 	KUNIT_ASSERT_GT(test, ret, 0);
2483 
2484 	KUNIT_EXPECT_EQ(test, ctx->post_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
2485 	KUNIT_EXPECT_EQ(test, ctx->post_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
2486 
2487 	clk_put(new_parent);
2488 	clk_put(clk);
2489 }
2490 
2491 static struct kunit_case clk_mux_notifier_test_cases[] = {
2492 	KUNIT_CASE(clk_mux_notifier_set_parent_test),
2493 	{}
2494 };
2495 
2496 /*
2497  * Test suite for a mux with multiple parents, and a notifier registered
2498  * on the mux.
2499  *
2500  * These tests exercise the behaviour of notifiers.
2501  */
2502 static struct kunit_suite clk_mux_notifier_test_suite = {
2503 	.name = "clk-mux-notifier",
2504 	.init = clk_mux_notifier_test_init,
2505 	.exit = clk_mux_notifier_test_exit,
2506 	.test_cases = clk_mux_notifier_test_cases,
2507 };
2508 
2509 static int
clk_mux_no_reparent_test_init(struct kunit * test)2510 clk_mux_no_reparent_test_init(struct kunit *test)
2511 {
2512 	struct clk_multiple_parent_ctx *ctx;
2513 	const char *parents[2] = { "parent-0", "parent-1"};
2514 	int ret;
2515 
2516 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2517 	if (!ctx)
2518 		return -ENOMEM;
2519 	test->priv = ctx;
2520 
2521 	ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
2522 							    &clk_dummy_rate_ops,
2523 							    0);
2524 	ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
2525 	ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
2526 	if (ret)
2527 		return ret;
2528 
2529 	ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
2530 							    &clk_dummy_rate_ops,
2531 							    0);
2532 	ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
2533 	ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
2534 	if (ret)
2535 		return ret;
2536 
2537 	ctx->current_parent = 0;
2538 	ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
2539 					   &clk_multiple_parents_no_reparent_mux_ops,
2540 					   0);
2541 	ret = clk_hw_register(NULL, &ctx->hw);
2542 	if (ret)
2543 		return ret;
2544 
2545 	return 0;
2546 }
2547 
2548 static void
clk_mux_no_reparent_test_exit(struct kunit * test)2549 clk_mux_no_reparent_test_exit(struct kunit *test)
2550 {
2551 	struct clk_multiple_parent_ctx *ctx = test->priv;
2552 
2553 	clk_hw_unregister(&ctx->hw);
2554 	clk_hw_unregister(&ctx->parents_ctx[0].hw);
2555 	clk_hw_unregister(&ctx->parents_ctx[1].hw);
2556 }
2557 
2558 /*
2559  * Test that if the we have a mux that cannot change parent and we call
2560  * clk_round_rate() on it with a rate that should cause it to change
2561  * parent, it won't.
2562  */
clk_mux_no_reparent_round_rate(struct kunit * test)2563 static void clk_mux_no_reparent_round_rate(struct kunit *test)
2564 {
2565 	struct clk_multiple_parent_ctx *ctx = test->priv;
2566 	struct clk_hw *hw = &ctx->hw;
2567 	struct clk *clk = clk_hw_get_clk(hw, NULL);
2568 	struct clk *other_parent, *parent;
2569 	unsigned long other_parent_rate;
2570 	unsigned long parent_rate;
2571 	long rounded_rate;
2572 
2573 	parent = clk_get_parent(clk);
2574 	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
2575 
2576 	parent_rate = clk_get_rate(parent);
2577 	KUNIT_ASSERT_GT(test, parent_rate, 0);
2578 
2579 	other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
2580 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
2581 	KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
2582 
2583 	other_parent_rate = clk_get_rate(other_parent);
2584 	KUNIT_ASSERT_GT(test, other_parent_rate, 0);
2585 	clk_put(other_parent);
2586 
2587 	rounded_rate = clk_round_rate(clk, other_parent_rate);
2588 	KUNIT_ASSERT_GT(test, rounded_rate, 0);
2589 	KUNIT_EXPECT_EQ(test, rounded_rate, parent_rate);
2590 
2591 	clk_put(clk);
2592 }
2593 
2594 /*
2595  * Test that if the we have a mux that cannot change parent and we call
2596  * clk_set_rate() on it with a rate that should cause it to change
2597  * parent, it won't.
2598  */
clk_mux_no_reparent_set_rate(struct kunit * test)2599 static void clk_mux_no_reparent_set_rate(struct kunit *test)
2600 {
2601 	struct clk_multiple_parent_ctx *ctx = test->priv;
2602 	struct clk_hw *hw = &ctx->hw;
2603 	struct clk *clk = clk_hw_get_clk(hw, NULL);
2604 	struct clk *other_parent, *parent;
2605 	unsigned long other_parent_rate;
2606 	unsigned long parent_rate;
2607 	unsigned long rate;
2608 	int ret;
2609 
2610 	parent = clk_get_parent(clk);
2611 	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
2612 
2613 	parent_rate = clk_get_rate(parent);
2614 	KUNIT_ASSERT_GT(test, parent_rate, 0);
2615 
2616 	other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
2617 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
2618 	KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
2619 
2620 	other_parent_rate = clk_get_rate(other_parent);
2621 	KUNIT_ASSERT_GT(test, other_parent_rate, 0);
2622 	clk_put(other_parent);
2623 
2624 	ret = clk_set_rate(clk, other_parent_rate);
2625 	KUNIT_ASSERT_EQ(test, ret, 0);
2626 
2627 	rate = clk_get_rate(clk);
2628 	KUNIT_ASSERT_GT(test, rate, 0);
2629 	KUNIT_EXPECT_EQ(test, rate, parent_rate);
2630 
2631 	clk_put(clk);
2632 }
2633 
2634 static struct kunit_case clk_mux_no_reparent_test_cases[] = {
2635 	KUNIT_CASE(clk_mux_no_reparent_round_rate),
2636 	KUNIT_CASE(clk_mux_no_reparent_set_rate),
2637 	{}
2638 };
2639 
2640 /*
2641  * Test suite for a clock mux that isn't allowed to change parent, using
2642  * the clk_hw_determine_rate_no_reparent() helper.
2643  *
2644  * These tests exercise that helper, and the proper selection of
2645  * rates and parents.
2646  */
2647 static struct kunit_suite clk_mux_no_reparent_test_suite = {
2648 	.name = "clk-mux-no-reparent",
2649 	.init = clk_mux_no_reparent_test_init,
2650 	.exit = clk_mux_no_reparent_test_exit,
2651 	.test_cases = clk_mux_no_reparent_test_cases,
2652 };
2653 
2654 struct clk_register_clk_parent_data_test_case {
2655 	const char *desc;
2656 	struct clk_parent_data pdata;
2657 };
2658 
2659 static void
clk_register_clk_parent_data_test_case_to_desc(const struct clk_register_clk_parent_data_test_case * t,char * desc)2660 clk_register_clk_parent_data_test_case_to_desc(
2661 		const struct clk_register_clk_parent_data_test_case *t, char *desc)
2662 {
2663 	strscpy(desc, t->desc, KUNIT_PARAM_DESC_SIZE);
2664 }
2665 
2666 static const struct clk_register_clk_parent_data_test_case
2667 clk_register_clk_parent_data_of_cases[] = {
2668 	{
2669 		/*
2670 		 * Test that a clk registered with a struct device_node can
2671 		 * find a parent based on struct clk_parent_data::index.
2672 		 */
2673 		.desc = "clk_parent_data_of_index_test",
2674 		.pdata.index = 0,
2675 	},
2676 	{
2677 		/*
2678 		 * Test that a clk registered with a struct device_node can
2679 		 * find a parent based on struct clk_parent_data::fwname.
2680 		 */
2681 		.desc = "clk_parent_data_of_fwname_test",
2682 		.pdata.fw_name = CLK_PARENT_DATA_PARENT1,
2683 	},
2684 	{
2685 		/*
2686 		 * Test that a clk registered with a struct device_node can
2687 		 * find a parent based on struct clk_parent_data::name.
2688 		 */
2689 		.desc = "clk_parent_data_of_name_test",
2690 		/* The index must be negative to indicate firmware not used */
2691 		.pdata.index = -1,
2692 		.pdata.name = CLK_PARENT_DATA_1MHZ_NAME,
2693 	},
2694 	{
2695 		/*
2696 		 * Test that a clk registered with a struct device_node can
2697 		 * find a parent based on struct
2698 		 * clk_parent_data::{fw_name,name}.
2699 		 */
2700 		.desc = "clk_parent_data_of_fwname_name_test",
2701 		.pdata.fw_name = CLK_PARENT_DATA_PARENT1,
2702 		.pdata.name = "not_matching",
2703 	},
2704 	{
2705 		/*
2706 		 * Test that a clk registered with a struct device_node can
2707 		 * find a parent based on struct clk_parent_data::{index,name}.
2708 		 * Index takes priority.
2709 		 */
2710 		.desc = "clk_parent_data_of_index_name_priority_test",
2711 		.pdata.index = 0,
2712 		.pdata.name = "not_matching",
2713 	},
2714 	{
2715 		/*
2716 		 * Test that a clk registered with a struct device_node can
2717 		 * find a parent based on struct
2718 		 * clk_parent_data::{index,fwname,name}. The fw_name takes
2719 		 * priority over index and name.
2720 		 */
2721 		.desc = "clk_parent_data_of_index_fwname_name_priority_test",
2722 		.pdata.index = 1,
2723 		.pdata.fw_name = CLK_PARENT_DATA_PARENT1,
2724 		.pdata.name = "not_matching",
2725 	},
2726 };
2727 
2728 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_of_test, clk_register_clk_parent_data_of_cases,
2729 		  clk_register_clk_parent_data_test_case_to_desc)
2730 
2731 /**
2732  * struct clk_register_clk_parent_data_of_ctx - Context for clk_parent_data OF tests
2733  * @np: device node of clk under test
2734  * @hw: clk_hw for clk under test
2735  */
2736 struct clk_register_clk_parent_data_of_ctx {
2737 	struct device_node *np;
2738 	struct clk_hw hw;
2739 };
2740 
clk_register_clk_parent_data_of_test_init(struct kunit * test)2741 static int clk_register_clk_parent_data_of_test_init(struct kunit *test)
2742 {
2743 	struct clk_register_clk_parent_data_of_ctx *ctx;
2744 
2745 	KUNIT_ASSERT_EQ(test, 0,
2746 			of_overlay_apply_kunit(test, kunit_clk_parent_data_test));
2747 
2748 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2749 	if (!ctx)
2750 		return -ENOMEM;
2751 	test->priv = ctx;
2752 
2753 	ctx->np = of_find_compatible_node(NULL, NULL, "test,clk-parent-data");
2754 	if (!ctx->np)
2755 		return -ENODEV;
2756 
2757 	of_node_put_kunit(test, ctx->np);
2758 
2759 	return 0;
2760 }
2761 
2762 /*
2763  * Test that a clk registered with a struct device_node can find a parent based on
2764  * struct clk_parent_data when the hw member isn't set.
2765  */
clk_register_clk_parent_data_of_test(struct kunit * test)2766 static void clk_register_clk_parent_data_of_test(struct kunit *test)
2767 {
2768 	struct clk_register_clk_parent_data_of_ctx *ctx = test->priv;
2769 	struct clk_hw *parent_hw;
2770 	const struct clk_register_clk_parent_data_test_case *test_param;
2771 	struct clk_init_data init = { };
2772 	struct clk *expected_parent, *actual_parent;
2773 
2774 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->np);
2775 
2776 	expected_parent = of_clk_get_kunit(test, ctx->np, 0);
2777 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
2778 
2779 	test_param = test->param_value;
2780 	init.parent_data = &test_param->pdata;
2781 	init.num_parents = 1;
2782 	init.name = "parent_data_of_test_clk";
2783 	init.ops = &clk_dummy_single_parent_ops;
2784 	ctx->hw.init = &init;
2785 	KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, ctx->np, &ctx->hw));
2786 
2787 	parent_hw = clk_hw_get_parent(&ctx->hw);
2788 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
2789 
2790 	actual_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
2791 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, actual_parent);
2792 
2793 	KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
2794 }
2795 
2796 static struct kunit_case clk_register_clk_parent_data_of_test_cases[] = {
2797 	KUNIT_CASE_PARAM(clk_register_clk_parent_data_of_test,
2798 			 clk_register_clk_parent_data_of_test_gen_params),
2799 	{}
2800 };
2801 
2802 /*
2803  * Test suite for registering clks with struct clk_parent_data and a struct
2804  * device_node.
2805  */
2806 static struct kunit_suite clk_register_clk_parent_data_of_suite = {
2807 	.name = "clk_register_clk_parent_data_of",
2808 	.init = clk_register_clk_parent_data_of_test_init,
2809 	.test_cases = clk_register_clk_parent_data_of_test_cases,
2810 };
2811 
2812 /**
2813  * struct platform_driver_dev_ctx - Context to stash platform device
2814  * @dev: device under test
2815  * @pdrv: driver to attach to find @dev
2816  */
2817 struct platform_driver_dev_ctx {
2818 	struct device *dev;
2819 	struct platform_driver pdrv;
2820 };
2821 
2822 static inline struct platform_driver_dev_ctx *
pdev_to_platform_driver_dev_ctx(struct platform_device * pdev)2823 pdev_to_platform_driver_dev_ctx(struct platform_device *pdev)
2824 {
2825 	return container_of(to_platform_driver(pdev->dev.driver),
2826 			    struct platform_driver_dev_ctx, pdrv);
2827 }
2828 
kunit_platform_driver_dev_probe(struct platform_device * pdev)2829 static int kunit_platform_driver_dev_probe(struct platform_device *pdev)
2830 {
2831 	struct platform_driver_dev_ctx *ctx;
2832 
2833 	ctx = pdev_to_platform_driver_dev_ctx(pdev);
2834 	ctx->dev = &pdev->dev;
2835 
2836 	return 0;
2837 }
2838 
2839 static struct device *
kunit_of_platform_driver_dev(struct kunit * test,const struct of_device_id * match_table)2840 kunit_of_platform_driver_dev(struct kunit *test, const struct of_device_id *match_table)
2841 {
2842 	struct platform_driver_dev_ctx *ctx;
2843 
2844 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
2845 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
2846 
2847 	ctx->pdrv.probe = kunit_platform_driver_dev_probe;
2848 	ctx->pdrv.driver.of_match_table = match_table;
2849 	ctx->pdrv.driver.name = __func__;
2850 	ctx->pdrv.driver.owner = THIS_MODULE;
2851 
2852 	KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
2853 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->dev);
2854 
2855 	return ctx->dev;
2856 }
2857 
2858 static const struct clk_register_clk_parent_data_test_case
2859 clk_register_clk_parent_data_device_cases[] = {
2860 	{
2861 		/*
2862 		 * Test that a clk registered with a struct device can find a
2863 		 * parent based on struct clk_parent_data::index.
2864 		 */
2865 		.desc = "clk_parent_data_device_index_test",
2866 		.pdata.index = 1,
2867 	},
2868 	{
2869 		/*
2870 		 * Test that a clk registered with a struct device can find a
2871 		 * parent based on struct clk_parent_data::fwname.
2872 		 */
2873 		.desc = "clk_parent_data_device_fwname_test",
2874 		.pdata.fw_name = CLK_PARENT_DATA_PARENT2,
2875 	},
2876 	{
2877 		/*
2878 		 * Test that a clk registered with a struct device can find a
2879 		 * parent based on struct clk_parent_data::name.
2880 		 */
2881 		.desc = "clk_parent_data_device_name_test",
2882 		/* The index must be negative to indicate firmware not used */
2883 		.pdata.index = -1,
2884 		.pdata.name = CLK_PARENT_DATA_50MHZ_NAME,
2885 	},
2886 	{
2887 		/*
2888 		 * Test that a clk registered with a struct device can find a
2889 		 * parent based on struct clk_parent_data::{fw_name,name}.
2890 		 */
2891 		.desc = "clk_parent_data_device_fwname_name_test",
2892 		.pdata.fw_name = CLK_PARENT_DATA_PARENT2,
2893 		.pdata.name = "not_matching",
2894 	},
2895 	{
2896 		/*
2897 		 * Test that a clk registered with a struct device can find a
2898 		 * parent based on struct clk_parent_data::{index,name}. Index
2899 		 * takes priority.
2900 		 */
2901 		.desc = "clk_parent_data_device_index_name_priority_test",
2902 		.pdata.index = 1,
2903 		.pdata.name = "not_matching",
2904 	},
2905 	{
2906 		/*
2907 		 * Test that a clk registered with a struct device can find a
2908 		 * parent based on struct clk_parent_data::{index,fwname,name}.
2909 		 * The fw_name takes priority over index and name.
2910 		 */
2911 		.desc = "clk_parent_data_device_index_fwname_name_priority_test",
2912 		.pdata.index = 0,
2913 		.pdata.fw_name = CLK_PARENT_DATA_PARENT2,
2914 		.pdata.name = "not_matching",
2915 	},
2916 };
2917 
KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_test,clk_register_clk_parent_data_device_cases,clk_register_clk_parent_data_test_case_to_desc)2918 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_test,
2919 		  clk_register_clk_parent_data_device_cases,
2920 		  clk_register_clk_parent_data_test_case_to_desc)
2921 
2922 /*
2923  * Test that a clk registered with a struct device can find a parent based on
2924  * struct clk_parent_data when the hw member isn't set.
2925  */
2926 static void clk_register_clk_parent_data_device_test(struct kunit *test)
2927 {
2928 	struct device *dev;
2929 	struct clk_hw *hw;
2930 	const struct clk_register_clk_parent_data_test_case *test_param;
2931 	struct clk_hw *parent_hw;
2932 	struct clk_init_data init = { };
2933 	struct clk *expected_parent, *actual_parent;
2934 	static const struct of_device_id match_table[] = {
2935 		{ .compatible = "test,clk-parent-data" },
2936 		{ }
2937 	};
2938 
2939 	dev = kunit_of_platform_driver_dev(test, match_table);
2940 
2941 	expected_parent = clk_get_kunit(test, dev, "50");
2942 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
2943 
2944 	hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
2945 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
2946 
2947 	test_param = test->param_value;
2948 	init.parent_data = &test_param->pdata;
2949 	init.num_parents = 1;
2950 	init.name = "parent_data_device_test_clk";
2951 	init.ops = &clk_dummy_single_parent_ops;
2952 	hw->init = &init;
2953 	KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, hw));
2954 
2955 	parent_hw = clk_hw_get_parent(hw);
2956 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
2957 
2958 	actual_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
2959 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, actual_parent);
2960 
2961 	KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
2962 }
2963 
2964 static const struct clk_register_clk_parent_data_test_case
2965 clk_register_clk_parent_data_device_hw_cases[] = {
2966 	{
2967 		/*
2968 		 * Test that a clk registered with a struct device can find a
2969 		 * parent based on struct clk_parent_data::hw.
2970 		 */
2971 		.desc = "clk_parent_data_device_hw_index_test",
2972 		/* The index must be negative to indicate firmware not used */
2973 		.pdata.index = -1,
2974 	},
2975 	{
2976 		/*
2977 		 * Test that a clk registered with a struct device can find a
2978 		 * parent based on struct clk_parent_data::hw when
2979 		 * struct clk_parent_data::fw_name is set.
2980 		 */
2981 		.desc = "clk_parent_data_device_hw_fwname_test",
2982 		.pdata.fw_name = CLK_PARENT_DATA_PARENT2,
2983 	},
2984 	{
2985 		/*
2986 		 * Test that a clk registered with a struct device can find a
2987 		 * parent based on struct clk_parent_data::hw when struct
2988 		 * clk_parent_data::name is set.
2989 		 */
2990 		.desc = "clk_parent_data_device_hw_name_test",
2991 		/* The index must be negative to indicate firmware not used */
2992 		.pdata.index = -1,
2993 		.pdata.name = CLK_PARENT_DATA_50MHZ_NAME,
2994 	},
2995 	{
2996 		/*
2997 		 * Test that a clk registered with a struct device can find a
2998 		 * parent based on struct clk_parent_data::hw when struct
2999 		 * clk_parent_data::{fw_name,name} are set.
3000 		 */
3001 		.desc = "clk_parent_data_device_hw_fwname_name_test",
3002 		.pdata.fw_name = CLK_PARENT_DATA_PARENT2,
3003 		.pdata.name = "not_matching",
3004 	},
3005 	{
3006 		/*
3007 		 * Test that a clk registered with a struct device can find a
3008 		 * parent based on struct clk_parent_data::hw when struct
3009 		 * clk_parent_data::index is set. The hw pointer takes
3010 		 * priority.
3011 		 */
3012 		.desc = "clk_parent_data_device_hw_index_priority_test",
3013 		.pdata.index = 0,
3014 	},
3015 	{
3016 		/*
3017 		 * Test that a clk registered with a struct device can find a
3018 		 * parent based on struct clk_parent_data::hw when
3019 		 * struct clk_parent_data::{index,fwname,name} are set.
3020 		 * The hw pointer takes priority over everything else.
3021 		 */
3022 		.desc = "clk_parent_data_device_hw_index_fwname_name_priority_test",
3023 		.pdata.index = 0,
3024 		.pdata.fw_name = CLK_PARENT_DATA_PARENT2,
3025 		.pdata.name = "not_matching",
3026 	},
3027 };
3028 
KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_hw_test,clk_register_clk_parent_data_device_hw_cases,clk_register_clk_parent_data_test_case_to_desc)3029 KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_hw_test,
3030 		  clk_register_clk_parent_data_device_hw_cases,
3031 		  clk_register_clk_parent_data_test_case_to_desc)
3032 
3033 /*
3034  * Test that a clk registered with a struct device can find a
3035  * parent based on struct clk_parent_data::hw.
3036  */
3037 static void clk_register_clk_parent_data_device_hw_test(struct kunit *test)
3038 {
3039 	struct device *dev;
3040 	struct clk_hw *hw;
3041 	const struct clk_register_clk_parent_data_test_case *test_param;
3042 	struct clk_dummy_context *parent;
3043 	struct clk_hw *parent_hw;
3044 	struct clk_parent_data pdata = { };
3045 	struct clk_init_data init = { };
3046 	static const struct of_device_id match_table[] = {
3047 		{ .compatible = "test,clk-parent-data" },
3048 		{ }
3049 	};
3050 
3051 	dev = kunit_of_platform_driver_dev(test, match_table);
3052 
3053 	parent = kunit_kzalloc(test, sizeof(*parent), GFP_KERNEL);
3054 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
3055 
3056 	parent_hw = &parent->hw;
3057 	parent_hw->init = CLK_HW_INIT_NO_PARENT("parent-clk",
3058 						&clk_dummy_rate_ops, 0);
3059 
3060 	KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, parent_hw));
3061 
3062 	hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
3063 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
3064 
3065 	test_param = test->param_value;
3066 	memcpy(&pdata, &test_param->pdata, sizeof(pdata));
3067 	pdata.hw = parent_hw;
3068 	init.parent_data = &pdata;
3069 	init.num_parents = 1;
3070 	init.ops = &clk_dummy_single_parent_ops;
3071 	init.name = "parent_data_device_hw_test_clk";
3072 	hw->init = &init;
3073 	KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, hw));
3074 
3075 	KUNIT_EXPECT_PTR_EQ(test, parent_hw, clk_hw_get_parent(hw));
3076 }
3077 
3078 static struct kunit_case clk_register_clk_parent_data_device_test_cases[] = {
3079 	KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_test,
3080 			 clk_register_clk_parent_data_device_test_gen_params),
3081 	KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_hw_test,
3082 			 clk_register_clk_parent_data_device_hw_test_gen_params),
3083 	{}
3084 };
3085 
clk_register_clk_parent_data_device_init(struct kunit * test)3086 static int clk_register_clk_parent_data_device_init(struct kunit *test)
3087 {
3088 	KUNIT_ASSERT_EQ(test, 0,
3089 			of_overlay_apply_kunit(test, kunit_clk_parent_data_test));
3090 
3091 	return 0;
3092 }
3093 
3094 /*
3095  * Test suite for registering clks with struct clk_parent_data and a struct
3096  * device.
3097  */
3098 static struct kunit_suite clk_register_clk_parent_data_device_suite = {
3099 	.name = "clk_register_clk_parent_data_device",
3100 	.init = clk_register_clk_parent_data_device_init,
3101 	.test_cases = clk_register_clk_parent_data_device_test_cases,
3102 };
3103 
3104 struct clk_assigned_rates_context {
3105 	struct clk_dummy_context clk0;
3106 	struct clk_dummy_context clk1;
3107 };
3108 
3109 /*
3110  * struct clk_assigned_rates_test_param - Test parameters for clk_assigned_rates test
3111  * @desc: Test description
3112  * @overlay_begin: Pointer to start of DT overlay to apply for test
3113  * @overlay_end: Pointer to end of DT overlay to apply for test
3114  * @rate0: Initial rate of first clk
3115  * @rate1: Initial rate of second clk
3116  * @sscs: Initial spread spectrum settings
3117  * @consumer_test: true if a consumer is being tested
3118  */
3119 struct clk_assigned_rates_test_param {
3120 	const char *desc;
3121 	u8 *overlay_begin;
3122 	u8 *overlay_end;
3123 	unsigned long rate0;
3124 	unsigned long rate1;
3125 	struct clk_spread_spectrum sscs;
3126 	bool consumer_test;
3127 };
3128 
3129 #define TEST_PARAM_OVERLAY(overlay_name)				\
3130 	.overlay_begin = of_overlay_begin(overlay_name),		\
3131 	.overlay_end = of_overlay_end(overlay_name)
3132 
3133 static void
clk_assigned_rates_register_clk(struct kunit * test,struct clk_dummy_context * ctx,struct device_node * np,const char * name,unsigned long rate,const struct clk_spread_spectrum * sscs)3134 clk_assigned_rates_register_clk(struct kunit *test,
3135 				struct clk_dummy_context *ctx,
3136 				struct device_node *np, const char *name,
3137 				unsigned long rate, const struct clk_spread_spectrum *sscs)
3138 {
3139 	struct clk_init_data init = { };
3140 
3141 	init.name = name;
3142 	init.ops = &clk_dummy_rate_ops;
3143 	ctx->hw.init = &init;
3144 	ctx->rate = rate;
3145 	ctx->sscs = *sscs;
3146 
3147 	KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, np, &ctx->hw));
3148 	KUNIT_ASSERT_EQ(test, ctx->rate, rate);
3149 }
3150 
3151 /*
3152  * Does most of the work of the test:
3153  *
3154  * 1. Apply the overlay to test
3155  * 2. Register the clk or clks to test
3156  * 3. Register the clk provider
3157  * 4. Apply clk defaults to the consumer device if this is a consumer test
3158  *
3159  * The tests will set different test_param values to test different scenarios
3160  * and validate that in their test functions.
3161  */
clk_assigned_rates_test_init(struct kunit * test)3162 static int clk_assigned_rates_test_init(struct kunit *test)
3163 {
3164 	struct device_node *np, *consumer;
3165 	struct clk_hw_onecell_data *data;
3166 	struct clk_assigned_rates_context *ctx;
3167 	u32 clk_cells;
3168 	const struct clk_assigned_rates_test_param *test_param;
3169 
3170 	test_param = test->param_value;
3171 
3172 	KUNIT_ASSERT_EQ(test, 0, __of_overlay_apply_kunit(test,
3173 							  test_param->overlay_begin,
3174 							  test_param->overlay_end));
3175 
3176 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
3177 		ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL));
3178 	test->priv = ctx;
3179 
3180 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
3181 		np = of_find_compatible_node(NULL, NULL, "test,clk-assigned-rates"));
3182 	of_node_put_kunit(test, np);
3183 
3184 	KUNIT_ASSERT_EQ(test, 0, of_property_read_u32(np, "#clock-cells", &clk_cells));
3185 	/* Only support #clock-cells = <0> or <1> */
3186 	KUNIT_ASSERT_LT(test, clk_cells, 2);
3187 
3188 	clk_assigned_rates_register_clk(test, &ctx->clk0, np,
3189 					"test_assigned_rate0", test_param->rate0,
3190 					&test_param->sscs);
3191 	if (clk_cells == 0) {
3192 		KUNIT_ASSERT_EQ(test, 0,
3193 				of_clk_add_hw_provider_kunit(test, np, of_clk_hw_simple_get,
3194 							     &ctx->clk0.hw));
3195 	} else if (clk_cells == 1) {
3196 		clk_assigned_rates_register_clk(test, &ctx->clk1, np,
3197 						"test_assigned_rate1", test_param->rate1,
3198 						&test_param->sscs);
3199 
3200 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
3201 			data = kunit_kzalloc(test, struct_size(data, hws, 2), GFP_KERNEL));
3202 		data->num = 2;
3203 		data->hws[0] = &ctx->clk0.hw;
3204 		data->hws[1] = &ctx->clk1.hw;
3205 
3206 		KUNIT_ASSERT_EQ(test, 0,
3207 				of_clk_add_hw_provider_kunit(test, np, of_clk_hw_onecell_get, data));
3208 	}
3209 
3210 	/* Consumers are optional */
3211 	if (test_param->consumer_test) {
3212 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
3213 			consumer = of_find_compatible_node(NULL, NULL, "test,clk-consumer"));
3214 		of_node_put_kunit(test, consumer);
3215 
3216 		KUNIT_ASSERT_EQ(test, 0, of_clk_set_defaults(consumer, false));
3217 	}
3218 
3219 	return 0;
3220 }
3221 
clk_assigned_rates_assigns_one(struct kunit * test)3222 static void clk_assigned_rates_assigns_one(struct kunit *test)
3223 {
3224 	struct clk_assigned_rates_context *ctx = test->priv;
3225 
3226 	KUNIT_EXPECT_EQ(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
3227 }
3228 
clk_assigned_rates_assigns_multiple(struct kunit * test)3229 static void clk_assigned_rates_assigns_multiple(struct kunit *test)
3230 {
3231 	struct clk_assigned_rates_context *ctx = test->priv;
3232 
3233 	KUNIT_EXPECT_EQ(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
3234 	KUNIT_EXPECT_EQ(test, ctx->clk1.rate, ASSIGNED_RATES_1_RATE);
3235 }
3236 
clk_assigned_rates_skips(struct kunit * test)3237 static void clk_assigned_rates_skips(struct kunit *test)
3238 {
3239 	struct clk_assigned_rates_context *ctx = test->priv;
3240 	const struct clk_assigned_rates_test_param *test_param = test->param_value;
3241 
3242 	KUNIT_EXPECT_NE(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
3243 	KUNIT_EXPECT_EQ(test, ctx->clk0.rate, test_param->rate0);
3244 }
3245 
3246 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_one);
3247 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_one_consumer);
3248 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_one);
3249 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_one_consumer);
3250 
3251 /* Test cases that assign one rate */
3252 static const struct clk_assigned_rates_test_param clk_assigned_rates_assigns_one_test_params[] = {
3253 	{
3254 		/*
3255 		 * Test that a single cell assigned-clock-rates property
3256 		 * assigns the rate when the property is in the provider.
3257 		 */
3258 		.desc = "provider assigns",
3259 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_one),
3260 	},
3261 	{
3262 		/*
3263 		 * Test that a single cell assigned-clock-rates property
3264 		 * assigns the rate when the property is in the consumer.
3265 		 */
3266 		.desc = "consumer assigns",
3267 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_one_consumer),
3268 		.consumer_test = true,
3269 	},
3270 	{
3271 		/*
3272 		 * Test that a single cell assigned-clock-rates-u64 property
3273 		 * assigns the rate when the property is in the provider.
3274 		 */
3275 		.desc = "provider assigns u64",
3276 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_one),
3277 	},
3278 	{
3279 		/*
3280 		 * Test that a single cell assigned-clock-rates-u64 property
3281 		 * assigns the rate when the property is in the consumer.
3282 		 */
3283 		.desc = "consumer assigns u64",
3284 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_one_consumer),
3285 		.consumer_test = true,
3286 	},
3287 };
3288 KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_assigns_one,
3289 		       clk_assigned_rates_assigns_one_test_params, desc)
3290 
3291 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_multiple);
3292 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_multiple_consumer);
3293 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_multiple);
3294 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_multiple_consumer);
3295 
3296 /* Test cases that assign multiple rates */
3297 static const struct clk_assigned_rates_test_param clk_assigned_rates_assigns_multiple_test_params[] = {
3298 	{
3299 		/*
3300 		 * Test that a multiple cell assigned-clock-rates property
3301 		 * assigns the rates when the property is in the provider.
3302 		 */
3303 		.desc = "provider assigns",
3304 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_multiple),
3305 	},
3306 	{
3307 		/*
3308 		 * Test that a multiple cell assigned-clock-rates property
3309 		 * assigns the rates when the property is in the consumer.
3310 		 */
3311 		.desc = "consumer assigns",
3312 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_multiple_consumer),
3313 		.consumer_test = true,
3314 	},
3315 	{
3316 		/*
3317 		 * Test that a single cell assigned-clock-rates-u64 property
3318 		 * assigns the rate when the property is in the provider.
3319 		 */
3320 		.desc = "provider assigns u64",
3321 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_multiple),
3322 	},
3323 	{
3324 		/*
3325 		 * Test that a multiple cell assigned-clock-rates-u64 property
3326 		 * assigns the rates when the property is in the consumer.
3327 		 */
3328 		.desc = "consumer assigns u64",
3329 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_multiple_consumer),
3330 		.consumer_test = true,
3331 	},
3332 };
3333 KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_assigns_multiple,
3334 		       clk_assigned_rates_assigns_multiple_test_params,
3335 		       desc)
3336 
3337 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_without);
3338 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_without_consumer);
3339 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_zero);
3340 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_zero_consumer);
3341 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_null);
3342 OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_null_consumer);
3343 
3344 /* Test cases that skip changing the rate due to malformed DT */
3345 static const struct clk_assigned_rates_test_param clk_assigned_rates_skips_test_params[] = {
3346 	{
3347 		/*
3348 		 * Test that an assigned-clock-rates property without an assigned-clocks
3349 		 * property fails when the property is in the provider.
3350 		 */
3351 		.desc = "provider missing assigned-clocks",
3352 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_without),
3353 		.rate0 = 3000,
3354 	},
3355 	{
3356 		/*
3357 		 * Test that an assigned-clock-rates property without an assigned-clocks
3358 		 * property fails when the property is in the consumer.
3359 		 */
3360 		.desc = "consumer missing assigned-clocks",
3361 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_without_consumer),
3362 		.rate0 = 3000,
3363 		.consumer_test = true,
3364 	},
3365 	{
3366 		/*
3367 		 * Test that an assigned-clock-rates property of zero doesn't
3368 		 * set a rate when the property is in the provider.
3369 		 */
3370 		.desc = "provider assigned-clock-rates of zero",
3371 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_zero),
3372 		.rate0 = 3000,
3373 	},
3374 	{
3375 		/*
3376 		 * Test that an assigned-clock-rates property of zero doesn't
3377 		 * set a rate when the property is in the consumer.
3378 		 */
3379 		.desc = "consumer assigned-clock-rates of zero",
3380 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_zero_consumer),
3381 		.rate0 = 3000,
3382 		.consumer_test = true,
3383 	},
3384 	{
3385 		/*
3386 		 * Test that an assigned-clocks property with a null phandle
3387 		 * doesn't set a rate when the property is in the provider.
3388 		 */
3389 		.desc = "provider assigned-clocks null phandle",
3390 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_null),
3391 		.rate0 = 3000,
3392 	},
3393 	{
3394 		/*
3395 		 * Test that an assigned-clocks property with a null phandle
3396 		 * doesn't set a rate when the property is in the consumer.
3397 		 */
3398 		.desc = "provider assigned-clocks null phandle",
3399 		TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_null_consumer),
3400 		.rate0 = 3000,
3401 		.consumer_test = true,
3402 	},
3403 };
3404 KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_skips,
3405 		       clk_assigned_rates_skips_test_params,
3406 		       desc)
3407 
3408 static struct kunit_case clk_assigned_rates_test_cases[] = {
3409 	KUNIT_CASE_PARAM(clk_assigned_rates_assigns_one,
3410 			 clk_assigned_rates_assigns_one_gen_params),
3411 	KUNIT_CASE_PARAM(clk_assigned_rates_assigns_multiple,
3412 			 clk_assigned_rates_assigns_multiple_gen_params),
3413 	KUNIT_CASE_PARAM(clk_assigned_rates_skips,
3414 			 clk_assigned_rates_skips_gen_params),
3415 	{}
3416 };
3417 
3418 /*
3419  * Test suite for assigned-clock-rates{-u64} DT property.
3420  */
3421 static struct kunit_suite clk_assigned_rates_suite = {
3422 	.name = "clk_assigned_rates",
3423 	.test_cases = clk_assigned_rates_test_cases,
3424 	.init = clk_assigned_rates_test_init,
3425 };
3426 
3427 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_one);
3428 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_one_consumer);
3429 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_multiple);
3430 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_multiple_consumer);
3431 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_without);
3432 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_without_consumer);
3433 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_zero);
3434 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_zero_consumer);
3435 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_null);
3436 OF_OVERLAY_DECLARE(kunit_clk_assigned_sscs_null_consumer);
3437 
clk_assigned_sscs_assigns_one(struct kunit * test)3438 static void clk_assigned_sscs_assigns_one(struct kunit *test)
3439 {
3440 	struct clk_assigned_rates_context *ctx = test->priv;
3441 
3442 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.modfreq_hz, ASSIGNED_SSCS_0_MODFREQ);
3443 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.spread_bp, ASSIGNED_SSCS_0_SPREAD);
3444 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.method, ASSIGNED_SSCS_0_METHOD);
3445 }
3446 
3447 /* Test cases that assign sscs for one clk */
3448 static const struct clk_assigned_rates_test_param clk_assigned_sscs_assigns_one_test_params[] = {
3449 	{
3450 		/*
3451 		 * Test that a single cell assigned-clock-sscs property
3452 		 * assigns the sscs when the property is in the provider.
3453 		 */
3454 		.desc = "provider assigns",
3455 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_one),
3456 	},
3457 	{
3458 		/*
3459 		 * Test that a single cell assigned-clock-sscs property
3460 		 * assigns the sscs when the property is in the consumer.
3461 		 */
3462 		.desc = "consumer assigns",
3463 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_one_consumer),
3464 		.consumer_test = true,
3465 	},
3466 };
KUNIT_ARRAY_PARAM_DESC(clk_assigned_sscs_assigns_one,clk_assigned_sscs_assigns_one_test_params,desc)3467 KUNIT_ARRAY_PARAM_DESC(clk_assigned_sscs_assigns_one,
3468 		       clk_assigned_sscs_assigns_one_test_params, desc)
3469 
3470 static void clk_assigned_sscs_assigns_multiple(struct kunit *test)
3471 {
3472 	struct clk_assigned_rates_context *ctx = test->priv;
3473 
3474 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.modfreq_hz, ASSIGNED_SSCS_0_MODFREQ);
3475 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.spread_bp, ASSIGNED_SSCS_0_SPREAD);
3476 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.method, ASSIGNED_SSCS_0_METHOD);
3477 	KUNIT_EXPECT_EQ(test, ctx->clk1.sscs.modfreq_hz, ASSIGNED_SSCS_1_MODFREQ);
3478 	KUNIT_EXPECT_EQ(test, ctx->clk1.sscs.spread_bp, ASSIGNED_SSCS_1_SPREAD);
3479 	KUNIT_EXPECT_EQ(test, ctx->clk1.sscs.method, ASSIGNED_SSCS_1_METHOD);
3480 }
3481 
3482 /* Test cases that assign sscs for multiple clks */
3483 static const
3484 struct clk_assigned_rates_test_param clk_assigned_sscs_assigns_multiple_test_params[] = {
3485 	{
3486 		/*
3487 		 * Test that a multiple cell assigned-clock-sscs property
3488 		 * assigns the sscs when the property is in the provider.
3489 		 */
3490 		.desc = "provider assigns",
3491 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_multiple),
3492 	},
3493 	{
3494 		/*
3495 		 * Test that a multiple cell assigned-clock-sscs property
3496 		 * assigns the sscs when the property is in the consumer.
3497 		 */
3498 		.desc = "consumer assigns",
3499 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_multiple_consumer),
3500 		.consumer_test = true,
3501 	},
3502 };
KUNIT_ARRAY_PARAM_DESC(clk_assigned_sscs_assigns_multiple,clk_assigned_sscs_assigns_multiple_test_params,desc)3503 KUNIT_ARRAY_PARAM_DESC(clk_assigned_sscs_assigns_multiple,
3504 		       clk_assigned_sscs_assigns_multiple_test_params,
3505 		       desc)
3506 
3507 static void clk_assigned_sscs_skips(struct kunit *test)
3508 {
3509 	struct clk_assigned_rates_context *ctx = test->priv;
3510 	const struct clk_assigned_rates_test_param *test_param = test->param_value;
3511 
3512 	KUNIT_EXPECT_NE(test, ctx->clk0.sscs.modfreq_hz, ASSIGNED_SSCS_0_MODFREQ);
3513 	KUNIT_EXPECT_NE(test, ctx->clk0.sscs.spread_bp, ASSIGNED_SSCS_0_SPREAD);
3514 	KUNIT_EXPECT_NE(test, ctx->clk0.sscs.method, ASSIGNED_SSCS_0_METHOD);
3515 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.modfreq_hz, test_param->sscs.modfreq_hz);
3516 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.spread_bp, test_param->sscs.spread_bp);
3517 	KUNIT_EXPECT_EQ(test, ctx->clk0.sscs.method, test_param->sscs.method);
3518 }
3519 
3520 /* Test cases that skip changing the sscs due to malformed DT */
3521 static const struct clk_assigned_rates_test_param clk_assigned_sscs_skips_test_params[] = {
3522 	{
3523 		/*
3524 		 * Test that an assigned-clock-sscs property without an assigned-clocks
3525 		 * property fails when the property is in the provider.
3526 		 */
3527 		.desc = "provider missing assigned-clocks",
3528 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_without),
3529 		.sscs = {50000, 60000, 3},
3530 	},
3531 	{
3532 		/*
3533 		 * Test that an assigned-clock-sscs property without an assigned-clocks
3534 		 * property fails when the property is in the consumer.
3535 		 */
3536 		.desc = "consumer missing assigned-clocks",
3537 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_without_consumer),
3538 		.sscs = {50000, 60000, 3},
3539 		.consumer_test = true,
3540 	},
3541 	{
3542 		/*
3543 		 * Test that an assigned-clock-sscs property of zero doesn't
3544 		 * set sscs when the property is in the provider.
3545 		 */
3546 		.desc = "provider assigned-clock-sscs of zero",
3547 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_zero),
3548 		.sscs = {50000, 60000, 3},
3549 	},
3550 	{
3551 		/*
3552 		 * Test that an assigned-clock-sscs property of zero doesn't
3553 		 * set sscs when the property is in the consumer.
3554 		 */
3555 		.desc = "consumer assigned-clock-sscs of zero",
3556 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_zero_consumer),
3557 		.sscs = {50000, 60000, 3},
3558 		.consumer_test = true,
3559 	},
3560 	{
3561 		/*
3562 		 * Test that an assigned-clocks property with a null phandle
3563 		 * doesn't set sscs when the property is in the provider.
3564 		 */
3565 		.desc = "provider assigned-clocks null phandle",
3566 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_null),
3567 		.sscs = {50000, 60000, 3},
3568 	},
3569 	{
3570 		/*
3571 		 * Test that an assigned-clocks property with a null phandle
3572 		 * doesn't set sscs when the property is in the consumer.
3573 		 */
3574 		.desc = "consumer assigned-clocks null phandle",
3575 		TEST_PARAM_OVERLAY(kunit_clk_assigned_sscs_null_consumer),
3576 		.sscs = {50000, 60000, 3},
3577 		.consumer_test = true,
3578 	},
3579 };
3580 KUNIT_ARRAY_PARAM_DESC(clk_assigned_sscs_skips,
3581 		       clk_assigned_sscs_skips_test_params,
3582 		       desc)
3583 
3584 static struct kunit_case clk_assigned_sscs_test_cases[] = {
3585 	KUNIT_CASE_PARAM(clk_assigned_sscs_assigns_one,
3586 			 clk_assigned_sscs_assigns_one_gen_params),
3587 	KUNIT_CASE_PARAM(clk_assigned_sscs_assigns_multiple,
3588 			 clk_assigned_sscs_assigns_multiple_gen_params),
3589 	KUNIT_CASE_PARAM(clk_assigned_sscs_skips,
3590 			 clk_assigned_sscs_skips_gen_params),
3591 	{}
3592 };
3593 
3594 /*
3595  * Test suite for assigned-clock-sscs DT property.
3596  */
3597 static struct kunit_suite clk_assigned_sscs_suite = {
3598 	.name = "clk_assigned_sscs",
3599 	.test_cases = clk_assigned_sscs_test_cases,
3600 	.init = clk_assigned_rates_test_init,
3601 };
3602 
3603 static const struct clk_init_data clk_hw_get_dev_of_node_init_data = {
3604 	.name = "clk_hw_get_dev_of_node",
3605 	.ops = &empty_clk_ops,
3606 };
3607 
3608 /*
3609  * Test that a clk registered with a struct device returns the device from
3610  * clk_hw_get_dev() and the node from clk_hw_get_of_node()
3611  */
clk_hw_register_dev_get_dev_returns_dev(struct kunit * test)3612 static void clk_hw_register_dev_get_dev_returns_dev(struct kunit *test)
3613 {
3614 	struct device *dev;
3615 	struct clk_hw *hw;
3616 	static const struct of_device_id match_table[] = {
3617 		{ .compatible = "test,clk-hw-get-dev-of-node" },
3618 		{ }
3619 	};
3620 
3621 	KUNIT_ASSERT_EQ(test, 0, of_overlay_apply_kunit(test, kunit_clk_hw_get_dev_of_node));
3622 
3623 	dev = kunit_of_platform_driver_dev(test, match_table);
3624 
3625 	hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
3626 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
3627 
3628 	hw->init = &clk_hw_get_dev_of_node_init_data;
3629 	KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, hw));
3630 
3631 	KUNIT_EXPECT_PTR_EQ(test, dev, clk_hw_get_dev(hw));
3632 	KUNIT_EXPECT_PTR_EQ(test, dev_of_node(dev), clk_hw_get_of_node(hw));
3633 }
3634 
3635 /*
3636  * Test that a clk registered with a struct device that's not associated with
3637  * an OF node returns the device from clk_hw_get_dev() and NULL from
3638  * clk_hw_get_of_node()
3639  */
clk_hw_register_dev_no_node_get_dev_returns_dev(struct kunit * test)3640 static void clk_hw_register_dev_no_node_get_dev_returns_dev(struct kunit *test)
3641 {
3642 	struct platform_device *pdev;
3643 	struct device *dev;
3644 	struct clk_hw *hw;
3645 
3646 	pdev = kunit_platform_device_alloc(test, "clk_hw_register_dev_no_node", -1);
3647 	KUNIT_ASSERT_NOT_NULL(test, pdev);
3648 	KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
3649 	dev = &pdev->dev;
3650 
3651 	hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
3652 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
3653 
3654 	hw->init = &clk_hw_get_dev_of_node_init_data;
3655 	KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, hw));
3656 
3657 	KUNIT_EXPECT_PTR_EQ(test, dev, clk_hw_get_dev(hw));
3658 	KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_of_node(hw));
3659 }
3660 
3661 /*
3662  * Test that a clk registered without a struct device returns NULL from
3663  * clk_hw_get_dev()
3664  */
clk_hw_register_NULL_get_dev_of_node_returns_NULL(struct kunit * test)3665 static void clk_hw_register_NULL_get_dev_of_node_returns_NULL(struct kunit *test)
3666 {
3667 	struct clk_hw *hw;
3668 
3669 	hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
3670 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
3671 
3672 	hw->init = &clk_hw_get_dev_of_node_init_data;
3673 
3674 	KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, NULL, hw));
3675 
3676 	KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_dev(hw));
3677 	KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_of_node(hw));
3678 }
3679 
3680 /*
3681  * Test that a clk registered with an of_node returns the node from
3682  * clk_hw_get_of_node() and NULL from clk_hw_get_dev()
3683  */
of_clk_hw_register_node_get_of_node_returns_node(struct kunit * test)3684 static void of_clk_hw_register_node_get_of_node_returns_node(struct kunit *test)
3685 {
3686 	struct device_node *np;
3687 	struct clk_hw *hw;
3688 
3689 	hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
3690 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
3691 
3692 	KUNIT_ASSERT_EQ(test, 0, of_overlay_apply_kunit(test, kunit_clk_hw_get_dev_of_node));
3693 
3694 	np = of_find_compatible_node(NULL, NULL, "test,clk-hw-get-dev-of-node");
3695 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
3696 	of_node_put_kunit(test, np);
3697 
3698 	hw->init = &clk_hw_get_dev_of_node_init_data;
3699 	KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, np, hw));
3700 
3701 	KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_dev(hw));
3702 	KUNIT_EXPECT_PTR_EQ(test, np, clk_hw_get_of_node(hw));
3703 }
3704 
3705 /*
3706  * Test that a clk registered without an of_node returns the node from
3707  * clk_hw_get_of_node() and clk_hw_get_dev()
3708  */
of_clk_hw_register_NULL_get_of_node_returns_NULL(struct kunit * test)3709 static void of_clk_hw_register_NULL_get_of_node_returns_NULL(struct kunit *test)
3710 {
3711 	struct clk_hw *hw;
3712 
3713 	hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
3714 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
3715 
3716 	hw->init = &clk_hw_get_dev_of_node_init_data;
3717 	KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, NULL, hw));
3718 
3719 	KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_dev(hw));
3720 	KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_of_node(hw));
3721 }
3722 
3723 static struct kunit_case clk_hw_get_dev_of_node_test_cases[] = {
3724 	KUNIT_CASE(clk_hw_register_dev_get_dev_returns_dev),
3725 	KUNIT_CASE(clk_hw_register_dev_no_node_get_dev_returns_dev),
3726 	KUNIT_CASE(clk_hw_register_NULL_get_dev_of_node_returns_NULL),
3727 	KUNIT_CASE(of_clk_hw_register_node_get_of_node_returns_node),
3728 	KUNIT_CASE(of_clk_hw_register_NULL_get_of_node_returns_NULL),
3729 	{}
3730 };
3731 
3732 /*
3733  * Test suite to verify clk_hw_get_dev() and clk_hw_get_of_node() when clk
3734  * registered with clk_hw_register() and of_clk_hw_register()
3735  */
3736 static struct kunit_suite clk_hw_get_dev_of_node_test_suite = {
3737 	.name = "clk_hw_get_dev_of_node_test_suite",
3738 	.test_cases = clk_hw_get_dev_of_node_test_cases,
3739 };
3740 
3741 
3742 kunit_test_suites(
3743 	&clk_assigned_rates_suite,
3744 	&clk_assigned_sscs_suite,
3745 	&clk_hw_get_dev_of_node_test_suite,
3746 	&clk_leaf_mux_set_rate_parent_test_suite,
3747 	&clk_test_suite,
3748 	&clk_multiple_parents_mux_test_suite,
3749 	&clk_mux_no_reparent_test_suite,
3750 	&clk_mux_notifier_test_suite,
3751 	&clk_orphan_transparent_multiple_parent_mux_test_suite,
3752 	&clk_orphan_transparent_single_parent_test_suite,
3753 	&clk_orphan_two_level_root_last_test_suite,
3754 	&clk_range_test_suite,
3755 	&clk_range_maximize_test_suite,
3756 	&clk_range_minimize_test_suite,
3757 	&clk_register_clk_parent_data_of_suite,
3758 	&clk_register_clk_parent_data_device_suite,
3759 	&clk_single_parent_mux_test_suite,
3760 	&clk_uncached_test_suite,
3761 );
3762 MODULE_DESCRIPTION("Kunit tests for clk framework");
3763 MODULE_LICENSE("GPL v2");
3764