1 From c5511ae59ece75d80acdaf37ebd08aca51f80188 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Thu, 7 Apr 2022 14:58:34 +0200
4 Subject: [PATCH] clk: tests: Add tests for mux with multiple parents
6 We'll need to test a few corner cases that occur when we have a mux
7 clock whose default parent is missing.
9 For now, let's create the context structure and the trivial ops, along
10 with a test suite that just tests trivial things for now, without
11 considering the orphan case.
13 Tested-by: Alexander Stein <alexander.stein@ew.tq-group.com> # imx8mp
14 Tested-by: Marek Szyprowski <m.szyprowski@samsung.com> # exynos4210, meson g12b
15 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
17 drivers/clk/clk_test.c | 119 +++++++++++++++++++++++++++++++++++++++++
18 1 file changed, 119 insertions(+)
20 --- a/drivers/clk/clk_test.c
21 +++ b/drivers/clk/clk_test.c
22 @@ -108,6 +108,39 @@ static const struct clk_ops clk_dummy_si
23 .get_parent = clk_dummy_single_get_parent,
26 +struct clk_multiple_parent_ctx {
27 + struct clk_dummy_context parents_ctx[2];
32 +static int clk_multiple_parents_mux_set_parent(struct clk_hw *hw, u8 index)
34 + struct clk_multiple_parent_ctx *ctx =
35 + container_of(hw, struct clk_multiple_parent_ctx, hw);
37 + if (index >= clk_hw_get_num_parents(hw))
40 + ctx->current_parent = index;
45 +static u8 clk_multiple_parents_mux_get_parent(struct clk_hw *hw)
47 + struct clk_multiple_parent_ctx *ctx =
48 + container_of(hw, struct clk_multiple_parent_ctx, hw);
50 + return ctx->current_parent;
53 +static const struct clk_ops clk_multiple_parents_mux_ops = {
54 + .get_parent = clk_multiple_parents_mux_get_parent,
55 + .set_parent = clk_multiple_parents_mux_set_parent,
56 + .determine_rate = __clk_mux_determine_rate_closest,
59 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
61 struct clk_dummy_context *ctx;
62 @@ -347,6 +380,91 @@ static struct kunit_suite clk_uncached_t
63 .test_cases = clk_uncached_test_cases,
67 +clk_multiple_parents_mux_test_init(struct kunit *test)
69 + struct clk_multiple_parent_ctx *ctx;
70 + const char *parents[2] = { "parent-0", "parent-1"};
73 + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
78 + ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
79 + &clk_dummy_rate_ops,
81 + ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
82 + ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
86 + ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
87 + &clk_dummy_rate_ops,
89 + ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
90 + ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
94 + ctx->current_parent = 0;
95 + ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
96 + &clk_multiple_parents_mux_ops,
97 + CLK_SET_RATE_PARENT);
98 + ret = clk_hw_register(NULL, &ctx->hw);
106 +clk_multiple_parents_mux_test_exit(struct kunit *test)
108 + struct clk_multiple_parent_ctx *ctx = test->priv;
110 + clk_hw_unregister(&ctx->hw);
111 + clk_hw_unregister(&ctx->parents_ctx[0].hw);
112 + clk_hw_unregister(&ctx->parents_ctx[1].hw);
116 + * Test that for a clock with multiple parents, clk_get_parent()
117 + * actually returns the current one.
120 +clk_test_multiple_parents_mux_get_parent(struct kunit *test)
122 + struct clk_multiple_parent_ctx *ctx = test->priv;
123 + struct clk_hw *hw = &ctx->hw;
124 + struct clk *clk = hw->clk;
125 + struct clk *parent;
127 + parent = clk_get_parent(clk);
128 + KUNIT_EXPECT_TRUE(test, clk_is_match(parent, ctx->parents_ctx[0].hw.clk));
131 +static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
132 + KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
137 + * Test suite for a basic mux clock with two parents, with
138 + * CLK_SET_RATE_PARENT on the child.
140 + * These tests are supposed to exercise the consumer API and check that
141 + * the state of the child and parents are sane and consistent.
143 +static struct kunit_suite
144 +clk_multiple_parents_mux_test_suite = {
145 + .name = "clk-multiple-parents-mux-test",
146 + .init = clk_multiple_parents_mux_test_init,
147 + .exit = clk_multiple_parents_mux_test_exit,
148 + .test_cases = clk_multiple_parents_mux_test_cases,
151 struct clk_single_parent_ctx {
152 struct clk_dummy_context parent_ctx;
154 @@ -1291,6 +1409,7 @@ static struct kunit_suite clk_range_mini
158 + &clk_multiple_parents_mux_test_suite,
159 &clk_orphan_transparent_single_parent_test_suite,
160 &clk_range_test_suite,
161 &clk_range_maximize_test_suite,