@@ -365,6 +365,189 @@ struct clk_single_parent_ctx {
365365 struct clk_hw hw ;
366366};
367367
368+ static int clk_single_parent_mux_test_init (struct kunit * test )
369+ {
370+ struct clk_single_parent_ctx * ctx ;
371+ int ret ;
372+
373+ ctx = kunit_kzalloc (test , sizeof (* ctx ), GFP_KERNEL );
374+ if (!ctx )
375+ return - ENOMEM ;
376+ test -> priv = ctx ;
377+
378+ ctx -> parent_ctx .rate = DUMMY_CLOCK_INIT_RATE ;
379+ ctx -> parent_ctx .hw .init =
380+ CLK_HW_INIT_NO_PARENT ("parent-clk" ,
381+ & clk_dummy_rate_ops ,
382+ 0 );
383+
384+ ret = clk_hw_register (NULL , & ctx -> parent_ctx .hw );
385+ if (ret )
386+ return ret ;
387+
388+ ctx -> hw .init = CLK_HW_INIT ("test-clk" , "parent-clk" ,
389+ & clk_dummy_single_parent_ops ,
390+ CLK_SET_RATE_PARENT );
391+
392+ ret = clk_hw_register (NULL , & ctx -> hw );
393+ if (ret )
394+ return ret ;
395+
396+ return 0 ;
397+ }
398+
399+ static void
400+ clk_single_parent_mux_test_exit (struct kunit * test )
401+ {
402+ struct clk_single_parent_ctx * ctx = test -> priv ;
403+
404+ clk_hw_unregister (& ctx -> hw );
405+ clk_hw_unregister (& ctx -> parent_ctx .hw );
406+ }
407+
408+ /*
409+ * Test that for a clock with a single parent, clk_get_parent() actually
410+ * returns the parent.
411+ */
412+ static void
413+ clk_test_single_parent_mux_get_parent (struct kunit * test )
414+ {
415+ struct clk_single_parent_ctx * ctx = test -> priv ;
416+ struct clk_hw * hw = & ctx -> hw ;
417+ struct clk * clk = clk_hw_get_clk (hw , NULL );
418+ struct clk * parent = clk_hw_get_clk (& ctx -> parent_ctx .hw , NULL );
419+
420+ KUNIT_EXPECT_TRUE (test , clk_is_match (clk_get_parent (clk ), parent ));
421+
422+ clk_put (parent );
423+ clk_put (clk );
424+ }
425+
426+ /*
427+ * Test that for a clock that can't modify its rate and with a single
428+ * parent, if we set disjoints range on the parent and then the child,
429+ * the second will return an error.
430+ *
431+ * FIXME: clk_set_rate_range() only considers the current clock when
432+ * evaluating whether ranges are disjoints and not the upstream clocks
433+ * ranges.
434+ */
435+ static void
436+ clk_test_single_parent_mux_set_range_disjoint_child_last (struct kunit * test )
437+ {
438+ struct clk_single_parent_ctx * ctx = test -> priv ;
439+ struct clk_hw * hw = & ctx -> hw ;
440+ struct clk * clk = clk_hw_get_clk (hw , NULL );
441+ struct clk * parent ;
442+ int ret ;
443+
444+ kunit_skip (test , "This needs to be fixed in the core." );
445+
446+ parent = clk_get_parent (clk );
447+ KUNIT_ASSERT_PTR_NE (test , parent , NULL );
448+
449+ ret = clk_set_rate_range (parent , 1000 , 2000 );
450+ KUNIT_ASSERT_EQ (test , ret , 0 );
451+
452+ ret = clk_set_rate_range (clk , 3000 , 4000 );
453+ KUNIT_EXPECT_LT (test , ret , 0 );
454+
455+ clk_put (clk );
456+ }
457+
458+ /*
459+ * Test that for a clock that can't modify its rate and with a single
460+ * parent, if we set disjoints range on the child and then the parent,
461+ * the second will return an error.
462+ *
463+ * FIXME: clk_set_rate_range() only considers the current clock when
464+ * evaluating whether ranges are disjoints and not the downstream clocks
465+ * ranges.
466+ */
467+ static void
468+ clk_test_single_parent_mux_set_range_disjoint_parent_last (struct kunit * test )
469+ {
470+ struct clk_single_parent_ctx * ctx = test -> priv ;
471+ struct clk_hw * hw = & ctx -> hw ;
472+ struct clk * clk = clk_hw_get_clk (hw , NULL );
473+ struct clk * parent ;
474+ int ret ;
475+
476+ kunit_skip (test , "This needs to be fixed in the core." );
477+
478+ parent = clk_get_parent (clk );
479+ KUNIT_ASSERT_PTR_NE (test , parent , NULL );
480+
481+ ret = clk_set_rate_range (clk , 1000 , 2000 );
482+ KUNIT_ASSERT_EQ (test , ret , 0 );
483+
484+ ret = clk_set_rate_range (parent , 3000 , 4000 );
485+ KUNIT_EXPECT_LT (test , ret , 0 );
486+
487+ clk_put (clk );
488+ }
489+
490+ /*
491+ * Test that for a clock that can't modify its rate and with a single
492+ * parent, if we set a range on the parent and a more restrictive one on
493+ * the child, and then call clk_round_rate(), the boundaries of the
494+ * two clocks are taken into account.
495+ */
496+ static void
497+ clk_test_single_parent_mux_set_range_round_rate_child_smaller (struct kunit * test )
498+ {
499+ struct clk_single_parent_ctx * ctx = test -> priv ;
500+ struct clk_hw * hw = & ctx -> hw ;
501+ struct clk * clk = clk_hw_get_clk (hw , NULL );
502+ struct clk * parent ;
503+ unsigned long rate ;
504+ int ret ;
505+
506+ parent = clk_get_parent (clk );
507+ KUNIT_ASSERT_PTR_NE (test , parent , NULL );
508+
509+ ret = clk_set_rate_range (parent , DUMMY_CLOCK_RATE_1 , DUMMY_CLOCK_RATE_2 );
510+ KUNIT_ASSERT_EQ (test , ret , 0 );
511+
512+ ret = clk_set_rate_range (clk , DUMMY_CLOCK_RATE_1 + 1000 , DUMMY_CLOCK_RATE_2 - 1000 );
513+ KUNIT_ASSERT_EQ (test , ret , 0 );
514+
515+ rate = clk_round_rate (clk , DUMMY_CLOCK_RATE_1 - 1000 );
516+ KUNIT_ASSERT_GT (test , rate , 0 );
517+ KUNIT_EXPECT_GE (test , rate , DUMMY_CLOCK_RATE_1 + 1000 );
518+ KUNIT_EXPECT_LE (test , rate , DUMMY_CLOCK_RATE_2 - 1000 );
519+
520+ rate = clk_round_rate (clk , DUMMY_CLOCK_RATE_2 + 1000 );
521+ KUNIT_ASSERT_GT (test , rate , 0 );
522+ KUNIT_EXPECT_GE (test , rate , DUMMY_CLOCK_RATE_1 + 1000 );
523+ KUNIT_EXPECT_LE (test , rate , DUMMY_CLOCK_RATE_2 - 1000 );
524+
525+ clk_put (clk );
526+ }
527+
528+ static struct kunit_case clk_single_parent_mux_test_cases [] = {
529+ KUNIT_CASE (clk_test_single_parent_mux_get_parent ),
530+ KUNIT_CASE (clk_test_single_parent_mux_set_range_disjoint_child_last ),
531+ KUNIT_CASE (clk_test_single_parent_mux_set_range_disjoint_parent_last ),
532+ KUNIT_CASE (clk_test_single_parent_mux_set_range_round_rate_child_smaller ),
533+ {}
534+ };
535+
536+ /*
537+ * Test suite for a basic mux clock with one parent, with
538+ * CLK_SET_RATE_PARENT on the child.
539+ *
540+ * These tests exercise the consumer API and check that the state of the
541+ * child and parent are sane and consistent.
542+ */
543+ static struct kunit_suite
544+ clk_single_parent_mux_test_suite = {
545+ .name = "clk-single-parent-mux-test" ,
546+ .init = clk_single_parent_mux_test_init ,
547+ .exit = clk_single_parent_mux_test_exit ,
548+ .test_cases = clk_single_parent_mux_test_cases ,
549+ };
550+
368551static int clk_orphan_transparent_single_parent_mux_test_init (struct kunit * test )
369552{
370553 struct clk_single_parent_ctx * ctx ;
@@ -401,14 +584,6 @@ static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test
401584 return 0 ;
402585}
403586
404- static void clk_orphan_transparent_single_parent_mux_test_exit (struct kunit * test )
405- {
406- struct clk_single_parent_ctx * ctx = test -> priv ;
407-
408- clk_hw_unregister (& ctx -> hw );
409- clk_hw_unregister (& ctx -> parent_ctx .hw );
410- }
411-
412587/*
413588 * Test that a mux-only clock, with an initial rate within a range,
414589 * will still have the same rate after the range has been enforced.
@@ -455,7 +630,7 @@ static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] =
455630static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
456631 .name = "clk-orphan-transparent-single-parent-test" ,
457632 .init = clk_orphan_transparent_single_parent_mux_test_init ,
458- .exit = clk_orphan_transparent_single_parent_mux_test_exit ,
633+ .exit = clk_single_parent_mux_test_exit ,
459634 .test_cases = clk_orphan_transparent_single_parent_mux_test_cases ,
460635};
461636
@@ -1168,6 +1343,7 @@ kunit_test_suites(
11681343 & clk_range_test_suite ,
11691344 & clk_range_maximize_test_suite ,
11701345 & clk_range_minimize_test_suite ,
1346+ & clk_single_parent_mux_test_suite ,
11711347 & clk_uncached_test_suite
11721348);
11731349MODULE_LICENSE ("GPL v2" );
0 commit comments