1 From b28798f3ae1a63169dd57e5b5cb55848172aa4c6 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Fri, 8 Apr 2022 09:54:39 +0200
4 Subject: [PATCH] clk: tests: Add tests for uncached clock
6 The clock framework supports clocks that can have their rate changed
7 without the kernel knowing about it using the CLK_GET_RATE_NOCACHE flag.
9 As its name suggests, this flag turns off the rate caching in the clock
10 framework, reading out the rate from the hardware any time we need to
13 Let's add a couple of tests to make sure it works as intended.
15 Tested-by: Alexander Stein <alexander.stein@ew.tq-group.com> # imx8mp
16 Tested-by: Marek Szyprowski <m.szyprowski@samsung.com> # exynos4210, meson g12b
17 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
19 drivers/clk/clk_test.c | 88 +++++++++++++++++++++++++++++++++++++++++-
20 1 file changed, 87 insertions(+), 1 deletion(-)
22 --- a/drivers/clk/clk_test.c
23 +++ b/drivers/clk/clk_test.c
24 @@ -262,6 +262,91 @@ static struct kunit_suite clk_test_suite
25 .test_cases = clk_test_cases,
28 +static int clk_uncached_test_init(struct kunit *test)
30 + struct clk_dummy_context *ctx;
33 + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
38 + ctx->rate = DUMMY_CLOCK_INIT_RATE;
39 + ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
40 + &clk_dummy_rate_ops,
41 + CLK_GET_RATE_NOCACHE);
43 + ret = clk_hw_register(NULL, &ctx->hw);
51 + * Test that for an uncached clock, the clock framework doesn't cache
52 + * the rate and clk_get_rate() will return the underlying clock rate
53 + * even if it changed.
55 +static void clk_test_uncached_get_rate(struct kunit *test)
57 + struct clk_dummy_context *ctx = test->priv;
58 + struct clk_hw *hw = &ctx->hw;
59 + struct clk *clk = hw->clk;
62 + rate = clk_get_rate(clk);
63 + KUNIT_ASSERT_GT(test, rate, 0);
64 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
66 + ctx->rate = DUMMY_CLOCK_RATE_1;
67 + rate = clk_get_rate(clk);
68 + KUNIT_ASSERT_GT(test, rate, 0);
69 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
73 + * Test that for an uncached clock, clk_set_rate_range() will work
74 + * properly if the rate hasn't changed.
76 +static void clk_test_uncached_set_range(struct kunit *test)
78 + struct clk_dummy_context *ctx = test->priv;
79 + struct clk_hw *hw = &ctx->hw;
80 + struct clk *clk = hw->clk;
83 + KUNIT_ASSERT_EQ(test,
84 + clk_set_rate_range(clk,
86 + DUMMY_CLOCK_RATE_2),
89 + rate = clk_get_rate(clk);
90 + KUNIT_ASSERT_GT(test, rate, 0);
91 + KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
92 + KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
95 +static struct kunit_case clk_uncached_test_cases[] = {
96 + KUNIT_CASE(clk_test_uncached_get_rate),
97 + KUNIT_CASE(clk_test_uncached_set_range),
102 + * Test suite for a basic, uncached, rate clock, without any parent.
104 + * These tests are supposed to exercise the rate API with simple scenarios
106 +static struct kunit_suite clk_uncached_test_suite = {
107 + .name = "clk-uncached-test",
108 + .init = clk_uncached_test_init,
109 + .exit = clk_test_exit,
110 + .test_cases = clk_uncached_test_cases,
113 struct clk_single_parent_ctx {
114 struct clk_dummy_context parent_ctx;
116 @@ -1042,6 +1127,7 @@ kunit_test_suites(
117 &clk_orphan_transparent_single_parent_test_suite,
118 &clk_range_test_suite,
119 &clk_range_maximize_test_suite,
120 - &clk_range_minimize_test_suite
121 + &clk_range_minimize_test_suite,
122 + &clk_uncached_test_suite
124 MODULE_LICENSE("GPL v2");