bcm27xx: add support for linux v5.15
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-5.15 / 950-0840-clk-tests-Add-tests-for-uncached-clock.patch
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
5
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.
8
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
11 read it.
12
13 Let's add a couple of tests to make sure it works as intended.
14
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>
18 ---
19 drivers/clk/clk_test.c | 88 +++++++++++++++++++++++++++++++++++++++++-
20 1 file changed, 87 insertions(+), 1 deletion(-)
21
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,
26 };
27
28 +static int clk_uncached_test_init(struct kunit *test)
29 +{
30 + struct clk_dummy_context *ctx;
31 + int ret;
32 +
33 + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
34 + if (!ctx)
35 + return -ENOMEM;
36 + test->priv = ctx;
37 +
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);
42 +
43 + ret = clk_hw_register(NULL, &ctx->hw);
44 + if (ret)
45 + return ret;
46 +
47 + return 0;
48 +}
49 +
50 +/*
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.
54 + */
55 +static void clk_test_uncached_get_rate(struct kunit *test)
56 +{
57 + struct clk_dummy_context *ctx = test->priv;
58 + struct clk_hw *hw = &ctx->hw;
59 + struct clk *clk = hw->clk;
60 + unsigned long rate;
61 +
62 + rate = clk_get_rate(clk);
63 + KUNIT_ASSERT_GT(test, rate, 0);
64 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
65 +
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);
70 +}
71 +
72 +/*
73 + * Test that for an uncached clock, clk_set_rate_range() will work
74 + * properly if the rate hasn't changed.
75 + */
76 +static void clk_test_uncached_set_range(struct kunit *test)
77 +{
78 + struct clk_dummy_context *ctx = test->priv;
79 + struct clk_hw *hw = &ctx->hw;
80 + struct clk *clk = hw->clk;
81 + unsigned long rate;
82 +
83 + KUNIT_ASSERT_EQ(test,
84 + clk_set_rate_range(clk,
85 + DUMMY_CLOCK_RATE_1,
86 + DUMMY_CLOCK_RATE_2),
87 + 0);
88 +
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);
93 +}
94 +
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),
98 + {}
99 +};
100 +
101 +/*
102 + * Test suite for a basic, uncached, rate clock, without any parent.
103 + *
104 + * These tests are supposed to exercise the rate API with simple scenarios
105 + */
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,
111 +};
112 +
113 struct clk_single_parent_ctx {
114 struct clk_dummy_context parent_ctx;
115 struct clk_hw hw;
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
123 );
124 MODULE_LICENSE("GPL v2");