Skip to content

Commit 350575a

Browse files
mripardbebarino
authored andcommitted
clk: tests: Add tests for uncached clock
The clock framework supports clocks that can have their rate changed without the kernel knowing about it using the CLK_GET_RATE_NOCACHE flag. As its name suggests, this flag turns off the rate caching in the clock framework, reading out the rate from the hardware any time we need to read it. Let's add a couple of tests to make sure it works as intended. Tested-by: Alexander Stein <[email protected]> # imx8mp Tested-by: Marek Szyprowski <[email protected]> # exynos4210, meson g12b Signed-off-by: Maxime Ripard <[email protected]> Link: https://lore.kernel.org/r/[email protected] Tested-by: Linux Kernel Functional Testing <[email protected]> Tested-by: Naresh Kamboju <[email protected]> Signed-off-by: Stephen Boyd <[email protected]>
1 parent 7d79c26 commit 350575a

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

drivers/clk/clk_test.c

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,96 @@ static struct kunit_suite clk_test_suite = {
270270
.test_cases = clk_test_cases,
271271
};
272272

273+
static int clk_uncached_test_init(struct kunit *test)
274+
{
275+
struct clk_dummy_context *ctx;
276+
int ret;
277+
278+
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
279+
if (!ctx)
280+
return -ENOMEM;
281+
test->priv = ctx;
282+
283+
ctx->rate = DUMMY_CLOCK_INIT_RATE;
284+
ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
285+
&clk_dummy_rate_ops,
286+
CLK_GET_RATE_NOCACHE);
287+
288+
ret = clk_hw_register(NULL, &ctx->hw);
289+
if (ret)
290+
return ret;
291+
292+
return 0;
293+
}
294+
295+
/*
296+
* Test that for an uncached clock, the clock framework doesn't cache
297+
* the rate and clk_get_rate() will return the underlying clock rate
298+
* even if it changed.
299+
*/
300+
static void clk_test_uncached_get_rate(struct kunit *test)
301+
{
302+
struct clk_dummy_context *ctx = test->priv;
303+
struct clk_hw *hw = &ctx->hw;
304+
struct clk *clk = clk_hw_get_clk(hw, NULL);
305+
unsigned long rate;
306+
307+
rate = clk_get_rate(clk);
308+
KUNIT_ASSERT_GT(test, rate, 0);
309+
KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
310+
311+
/* We change the rate behind the clock framework's back */
312+
ctx->rate = DUMMY_CLOCK_RATE_1;
313+
rate = clk_get_rate(clk);
314+
KUNIT_ASSERT_GT(test, rate, 0);
315+
KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
316+
317+
clk_put(clk);
318+
}
319+
320+
/*
321+
* Test that for an uncached clock, clk_set_rate_range() will work
322+
* properly if the rate hasn't changed.
323+
*/
324+
static void clk_test_uncached_set_range(struct kunit *test)
325+
{
326+
struct clk_dummy_context *ctx = test->priv;
327+
struct clk_hw *hw = &ctx->hw;
328+
struct clk *clk = clk_hw_get_clk(hw, NULL);
329+
unsigned long rate;
330+
331+
KUNIT_ASSERT_EQ(test,
332+
clk_set_rate_range(clk,
333+
DUMMY_CLOCK_RATE_1,
334+
DUMMY_CLOCK_RATE_2),
335+
0);
336+
337+
rate = clk_get_rate(clk);
338+
KUNIT_ASSERT_GT(test, rate, 0);
339+
KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
340+
KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
341+
342+
clk_put(clk);
343+
}
344+
345+
static struct kunit_case clk_uncached_test_cases[] = {
346+
KUNIT_CASE(clk_test_uncached_get_rate),
347+
KUNIT_CASE(clk_test_uncached_set_range),
348+
{}
349+
};
350+
351+
/*
352+
* Test suite for a basic, uncached, rate clock, without any parent.
353+
*
354+
* These tests exercise the rate API with simple scenarios
355+
*/
356+
static struct kunit_suite clk_uncached_test_suite = {
357+
.name = "clk-uncached-test",
358+
.init = clk_uncached_test_init,
359+
.exit = clk_test_exit,
360+
.test_cases = clk_uncached_test_cases,
361+
};
362+
273363
struct clk_single_parent_ctx {
274364
struct clk_dummy_context parent_ctx;
275365
struct clk_hw hw;
@@ -1077,6 +1167,7 @@ kunit_test_suites(
10771167
&clk_orphan_transparent_single_parent_test_suite,
10781168
&clk_range_test_suite,
10791169
&clk_range_maximize_test_suite,
1080-
&clk_range_minimize_test_suite
1170+
&clk_range_minimize_test_suite,
1171+
&clk_uncached_test_suite
10811172
);
10821173
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)