Skip to content

Commit 1234a2c

Browse files
mripardbebarino
authored andcommitted
clk: Introduce clk_core_has_parent()
We will need to know if a clk_core pointer has a given parent in other functions, so let's create a clk_core_has_parent() function that clk_has_parent() will call into. For good measure, let's add some unit tests as well to make sure it works properly. 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-20-maxime@cerno.tech Tested-by: Linux Kernel Functional Testing <lkft@linaro.org> Tested-by: Naresh Kamboju <naresh.kamboju@linaro.org> [sboyd@kernel.org: Move tmp declaration, fix conditional to check for current parent] Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent 666650b commit 1234a2c

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

drivers/clk/clk.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,27 @@ static bool mux_is_better_rate(unsigned long rate, unsigned long now,
539539
static int clk_core_round_rate_nolock(struct clk_core *core,
540540
struct clk_rate_request *req);
541541

542+
static bool clk_core_has_parent(struct clk_core *core, const struct clk_core *parent)
543+
{
544+
struct clk_core *tmp;
545+
unsigned int i;
546+
547+
/* Optimize for the case where the parent is already the parent. */
548+
if (core->parent == parent)
549+
return true;
550+
551+
for (i = 0; i < core->num_parents; i++) {
552+
tmp = clk_core_get_parent_by_index(core, i);
553+
if (!tmp)
554+
continue;
555+
556+
if (tmp == parent)
557+
return true;
558+
}
559+
560+
return false;
561+
}
562+
542563
int clk_mux_determine_rate_flags(struct clk_hw *hw,
543564
struct clk_rate_request *req,
544565
unsigned long flags)
@@ -2574,25 +2595,11 @@ void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
25742595
*/
25752596
bool clk_has_parent(struct clk *clk, struct clk *parent)
25762597
{
2577-
struct clk_core *core, *parent_core;
2578-
int i;
2579-
25802598
/* NULL clocks should be nops, so return success if either is NULL. */
25812599
if (!clk || !parent)
25822600
return true;
25832601

2584-
core = clk->core;
2585-
parent_core = parent->core;
2586-
2587-
/* Optimize for the case where the parent is already the parent. */
2588-
if (core->parent == parent_core)
2589-
return true;
2590-
2591-
for (i = 0; i < core->num_parents; i++)
2592-
if (!strcmp(core->parents[i].name, parent_core->name))
2593-
return true;
2594-
2595-
return false;
2602+
return clk_core_has_parent(clk->core, parent->core);
25962603
}
25972604
EXPORT_SYMBOL_GPL(clk_has_parent);
25982605

drivers/clk/clk_test.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,32 @@ clk_test_multiple_parents_mux_get_parent(struct kunit *test)
491491
clk_put(clk);
492492
}
493493

494+
/*
495+
* Test that for a clock with a multiple parents, clk_has_parent()
496+
* actually reports all of them as parents.
497+
*/
498+
static void
499+
clk_test_multiple_parents_mux_has_parent(struct kunit *test)
500+
{
501+
struct clk_multiple_parent_ctx *ctx = test->priv;
502+
struct clk_hw *hw = &ctx->hw;
503+
struct clk *clk = clk_hw_get_clk(hw, NULL);
504+
struct clk *parent;
505+
506+
parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
507+
KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
508+
clk_put(parent);
509+
510+
parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
511+
KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
512+
clk_put(parent);
513+
514+
clk_put(clk);
515+
}
516+
494517
static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
495518
KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
519+
KUNIT_CASE(clk_test_multiple_parents_mux_has_parent),
496520
{}
497521
};
498522

@@ -918,6 +942,24 @@ clk_test_single_parent_mux_get_parent(struct kunit *test)
918942
clk_put(clk);
919943
}
920944

945+
/*
946+
* Test that for a clock with a single parent, clk_has_parent() actually
947+
* reports it as a parent.
948+
*/
949+
static void
950+
clk_test_single_parent_mux_has_parent(struct kunit *test)
951+
{
952+
struct clk_single_parent_ctx *ctx = test->priv;
953+
struct clk_hw *hw = &ctx->hw;
954+
struct clk *clk = clk_hw_get_clk(hw, NULL);
955+
struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
956+
957+
KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
958+
959+
clk_put(parent);
960+
clk_put(clk);
961+
}
962+
921963
/*
922964
* Test that for a clock that can't modify its rate and with a single
923965
* parent, if we set disjoints range on the parent and then the child,
@@ -1022,6 +1064,7 @@ clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test
10221064

10231065
static struct kunit_case clk_single_parent_mux_test_cases[] = {
10241066
KUNIT_CASE(clk_test_single_parent_mux_get_parent),
1067+
KUNIT_CASE(clk_test_single_parent_mux_has_parent),
10251068
KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
10261069
KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
10271070
KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),

0 commit comments

Comments
 (0)