Skip to content

Commit 3afb072

Browse files
mripardbebarino
authored andcommitted
clk: Take into account uncached clocks in clk_set_rate_range()
clk_set_rate_range() will use the last requested rate for the clock when it calls into the driver set_rate hook. However, if CLK_GET_RATE_NOCACHE is set on that clock, the last requested rate might not be matching the current rate of the clock. In such a case, let's read out the rate from the hardware and use that in our set_rate instead. Tested-by: Alexander Stein <alexander.stein@ew.tq-group.com> # imx8mp Tested-by: Marek Szyprowski <m.szyprowski@samsung.com> # exynos4210, meson g12b Signed-off-by: Maxime Ripard <maxime@cerno.tech> Link: https://lore.kernel.org/r/20220816112530.1837489-13-maxime@cerno.tech Tested-by: Linux Kernel Functional Testing <lkft@linaro.org> Tested-by: Naresh Kamboju <naresh.kamboju@linaro.org> Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent 2e9cad1 commit 3afb072

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

drivers/clk/clk.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,10 @@ static int clk_set_rate_range_nolock(struct clk *clk,
23732373
goto out;
23742374
}
23752375

2376+
rate = clk->core->req_rate;
2377+
if (clk->core->flags & CLK_GET_RATE_NOCACHE)
2378+
rate = clk_core_get_rate_recalc(clk->core);
2379+
23762380
/*
23772381
* Since the boundaries have been changed, let's give the
23782382
* opportunity to the provider to adjust the clock rate based on
@@ -2390,7 +2394,7 @@ static int clk_set_rate_range_nolock(struct clk *clk,
23902394
* - the determine_rate() callback does not really check for
23912395
* this corner case when determining the rate
23922396
*/
2393-
rate = clamp(clk->core->req_rate, min, max);
2397+
rate = clamp(rate, min, max);
23942398
ret = clk_core_set_rate_nolock(clk->core, rate);
23952399
if (ret) {
23962400
/* rollback the changes */

drivers/clk/clk_test.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,40 @@ static void clk_test_uncached_set_range(struct kunit *test)
375375
clk_put(clk);
376376
}
377377

378+
/*
379+
* Test that for an uncached clock, clk_set_rate_range() will work
380+
* properly if the rate has changed in hardware.
381+
*
382+
* In this case, it means that if the rate wasn't initially in the range
383+
* we're trying to set, but got changed at some point into the range
384+
* without the kernel knowing about it, its rate shouldn't be affected.
385+
*/
386+
static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
387+
{
388+
struct clk_dummy_context *ctx = test->priv;
389+
struct clk_hw *hw = &ctx->hw;
390+
struct clk *clk = clk_hw_get_clk(hw, NULL);
391+
unsigned long rate;
392+
393+
/* We change the rate behind the clock framework's back */
394+
ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
395+
KUNIT_ASSERT_EQ(test,
396+
clk_set_rate_range(clk,
397+
DUMMY_CLOCK_RATE_1,
398+
DUMMY_CLOCK_RATE_2),
399+
0);
400+
401+
rate = clk_get_rate(clk);
402+
KUNIT_ASSERT_GT(test, rate, 0);
403+
KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
404+
405+
clk_put(clk);
406+
}
407+
378408
static struct kunit_case clk_uncached_test_cases[] = {
379409
KUNIT_CASE(clk_test_uncached_get_rate),
380410
KUNIT_CASE(clk_test_uncached_set_range),
411+
KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
381412
{}
382413
};
383414

0 commit comments

Comments
 (0)