8f540b7660d5e99396bfdf5deca2bea0e9221493
[openwrt/staging/jow.git] /
1 From 4ec4cfbf63a697a5b1c82b400d28afd0347d555a Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Fri, 8 Apr 2022 10:06:11 +0200
4 Subject: [PATCH] clk: Take into account uncached clocks in
5 clk_set_rate_range()
6
7 clk_set_rate_range() will use the last requested rate for the clock when
8 it calls into the driver set_rate hook.
9
10 However, if CLK_GET_RATE_NOCACHE is set on that clock, the last
11 requested rate might not be matching the current rate of the clock. In
12 such a case, let's read out the rate from the hardware and use that in
13 our set_rate instead.
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.c | 6 +++++-
20 drivers/clk/clk_test.c | 28 ++++++++++++++++++++++++++++
21 2 files changed, 33 insertions(+), 1 deletion(-)
22
23 --- a/drivers/clk/clk.c
24 +++ b/drivers/clk/clk.c
25 @@ -2365,6 +2365,10 @@ static int clk_set_rate_range_nolock(str
26 goto out;
27 }
28
29 + rate = clk->core->req_rate;
30 + if (clk->core->flags & CLK_GET_RATE_NOCACHE)
31 + rate = clk_core_get_rate_recalc(clk->core);
32 +
33 /*
34 * Since the boundaries have been changed, let's give the
35 * opportunity to the provider to adjust the clock rate based on
36 @@ -2382,7 +2386,7 @@ static int clk_set_rate_range_nolock(str
37 * - the determine_rate() callback does not really check for
38 * this corner case when determining the rate
39 */
40 - rate = clamp(clk->core->req_rate, min, max);
41 + rate = clamp(rate, min, max);
42 ret = clk_core_set_rate_nolock(clk->core, rate);
43 if (ret) {
44 /* rollback the changes */
45 --- a/drivers/clk/clk_test.c
46 +++ b/drivers/clk/clk_test.c
47 @@ -362,9 +362,37 @@ static void clk_test_uncached_set_range(
48 KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
49 }
50
51 +/*
52 + * Test that for an uncached clock, clk_set_rate_range() will work
53 + * properly if the rate has changed in hardware.
54 + *
55 + * In this case, it means that if the rate wasn't initially in the range
56 + * we're trying to set, but got changed at some point into the range
57 + * without the kernel knowing about it, its rate shouldn't be affected.
58 + */
59 +static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
60 +{
61 + struct clk_dummy_context *ctx = test->priv;
62 + struct clk_hw *hw = &ctx->hw;
63 + struct clk *clk = hw->clk;
64 + unsigned long rate;
65 +
66 + ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
67 + KUNIT_ASSERT_EQ(test,
68 + clk_set_rate_range(clk,
69 + DUMMY_CLOCK_RATE_1,
70 + DUMMY_CLOCK_RATE_2),
71 + 0);
72 +
73 + rate = clk_get_rate(clk);
74 + KUNIT_ASSERT_GT(test, rate, 0);
75 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
76 +}
77 +
78 static struct kunit_case clk_uncached_test_cases[] = {
79 KUNIT_CASE(clk_test_uncached_get_rate),
80 KUNIT_CASE(clk_test_uncached_set_range),
81 + KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
82 {}
83 };
84