bcm27xx: add support for linux v5.15
[openwrt/staging/noltari.git] / target / linux / bcm27xx / patches-5.15 / 950-0832-clk-Always-clamp-the-rounded-rate.patch
1 From 9db7cf9f810ff5092e664078165b2e066e546f6a Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Wed, 5 May 2021 15:35:34 +0200
4 Subject: [PATCH] clk: Always clamp the rounded rate
5
6 The current core while setting the min and max rate properly in the
7 clk_request structure will not make sure that the requested rate is
8 within these boundaries, leaving it to each and every driver to make
9 sure it is.
10
11 It's not clear if this was on purpose or not, but this introduces some
12 inconsistencies within the API.
13
14 For example, a user setting a range and then calling clk_round_rate()
15 with a value outside of that range will get the same value back
16 (ignoring any driver adjustements), effectively ignoring the range that
17 was just set.
18
19 Another one, arguably worse, is that it also makes clk_round_rate() and
20 clk_set_rate() behave differently if there's a range and the rate being
21 used for both is outside that range. As we have seen, the rate will be
22 returned unchanged by clk_round_rate(), but clk_set_rate() will error
23 out returning -EINVAL.
24
25 Let's make sure the framework will always clamp the rate to the current
26 range found on the clock, which will fix both these inconsistencies.
27
28 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
29 ---
30 drivers/clk/clk_test.c | 50 +++++++++++++++++++++++++++---------------
31 1 file changed, 32 insertions(+), 18 deletions(-)
32
33 --- a/drivers/clk/clk_test.c
34 +++ b/drivers/clk/clk_test.c
35 @@ -310,8 +310,7 @@ static void clk_range_test_multiple_disj
36
37 /*
38 * Test that if our clock has some boundaries and we try to round a rate
39 - * lower than the minimum, the returned rate won't be affected by the
40 - * boundaries.
41 + * lower than the minimum, the returned rate will be within range.
42 */
43 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
44 {
45 @@ -328,18 +327,20 @@ static void clk_range_test_set_range_rou
46
47 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
48 KUNIT_ASSERT_GT(test, rate, 0);
49 - KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
50 + KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
51 + KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
52 }
53
54 /*
55 * Test that if our clock has some boundaries and we try to set a rate
56 - * lower than the minimum, we'll get an error.
57 + * higher than the maximum, the new rate will be within range.
58 */
59 static void clk_range_test_set_range_set_rate_lower(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 KUNIT_ASSERT_EQ(test,
67 clk_set_rate_range(clk,
68 @@ -347,15 +348,21 @@ static void clk_range_test_set_range_set
69 DUMMY_CLOCK_RATE_2),
70 0);
71
72 - KUNIT_ASSERT_LT(test,
73 + KUNIT_ASSERT_EQ(test,
74 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
75 0);
76 +
77 + rate = clk_get_rate(clk);
78 + KUNIT_ASSERT_GT(test, rate, 0);
79 + KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
80 + KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
81 }
82
83 /*
84 * Test that if our clock has some boundaries and we try to round and
85 - * set a rate lower than the minimum, the values won't be consistent
86 - * between clk_round_rate() and clk_set_rate().
87 + * set a rate lower than the minimum, the rate returned by
88 + * clk_round_rate() will be consistent with the new rate set by
89 + * clk_set_rate().
90 */
91 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
92 {
93 @@ -373,17 +380,16 @@ static void clk_range_test_set_range_set
94 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
95 KUNIT_ASSERT_GT(test, rounded, 0);
96
97 - KUNIT_EXPECT_LT(test,
98 + KUNIT_ASSERT_EQ(test,
99 clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
100 0);
101
102 - KUNIT_EXPECT_NE(test, rounded, clk_get_rate(clk));
103 + KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
104 }
105
106 /*
107 * Test that if our clock has some boundaries and we try to round a rate
108 - * higher than the maximum, the returned rate won't be affected by the
109 - * boundaries.
110 + * higher than the maximum, the returned rate will be within range.
111 */
112 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
113 {
114 @@ -400,18 +406,20 @@ static void clk_range_test_set_range_rou
115
116 rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
117 KUNIT_ASSERT_GT(test, rate, 0);
118 - KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 + 1000);
119 + KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
120 + KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
121 }
122
123 /*
124 * Test that if our clock has some boundaries and we try to set a rate
125 - * lower than the maximum, we'll get an error.
126 + * higher than the maximum, the new rate will be within range.
127 */
128 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
129 {
130 struct clk_dummy_context *ctx = test->priv;
131 struct clk_hw *hw = &ctx->hw;
132 struct clk *clk = hw->clk;
133 + unsigned long rate;
134
135 KUNIT_ASSERT_EQ(test,
136 clk_set_rate_range(clk,
137 @@ -419,15 +427,21 @@ static void clk_range_test_set_range_set
138 DUMMY_CLOCK_RATE_2),
139 0);
140
141 - KUNIT_ASSERT_LT(test,
142 + KUNIT_ASSERT_EQ(test,
143 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
144 0);
145 +
146 + rate = clk_get_rate(clk);
147 + KUNIT_ASSERT_GT(test, rate, 0);
148 + KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
149 + KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
150 }
151
152 /*
153 * Test that if our clock has some boundaries and we try to round and
154 - * set a rate higher than the maximum, the values won't be consistent
155 - * between clk_round_rate() and clk_set_rate().
156 + * set a rate higher than the maximum, the rate returned by
157 + * clk_round_rate() will be consistent with the new rate set by
158 + * clk_set_rate().
159 */
160 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
161 {
162 @@ -445,11 +459,11 @@ static void clk_range_test_set_range_set
163 rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
164 KUNIT_ASSERT_GT(test, rounded, 0);
165
166 - KUNIT_EXPECT_LT(test,
167 + KUNIT_ASSERT_EQ(test,
168 clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
169 0);
170
171 - KUNIT_EXPECT_NE(test, rounded, clk_get_rate(clk));
172 + KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
173 }
174
175 /*