[PATCH 3/7] clk: mediatek: add mtk_clk_id_is_* helper functions
David Lechner
dlechner at baylibre.com
Thu Feb 5 22:12:38 CET 2026
Add helper functions to check if a clock ID corresponds to a particular
clock type (mux, gate, fdiv). This simplifies the code and makes it more
readable.
Additionally, it removes the restriction that fdivs_offs < muxes_offs <
gates_offs by making the checking more strict in some places. This will
allow future drivers to not have to define a mapping to meet this
requirement.
Signed-off-by: David Lechner <dlechner at baylibre.com>
---
drivers/clk/mediatek/clk-mtk.c | 128 ++++++++++++++++++++---------------------
1 file changed, 64 insertions(+), 64 deletions(-)
diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 0123b0ce7b1..3bd9021b9cf 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -85,6 +85,34 @@ static int mtk_common_clk_get_unmapped_id(struct clk *clk)
return -ENOENT;
}
+static bool mtk_clk_id_is_pll(const struct mtk_clk_tree *tree, int mapped_id)
+{
+ return tree->plls && mapped_id < tree->num_plls;
+}
+
+static bool mtk_clk_id_is_fclk(const struct mtk_clk_tree *tree, int mapped_id)
+{
+ return tree->fclks && mapped_id < tree->num_fclks;
+}
+
+static bool mtk_clk_id_is_fdiv(const struct mtk_clk_tree *tree, int mapped_id)
+{
+ return tree->fdivs && mapped_id >= tree->fdivs_offs &&
+ mapped_id < tree->fdivs_offs + tree->num_fdivs;
+}
+
+static bool mtk_clk_id_is_mux(const struct mtk_clk_tree *tree, int mapped_id)
+{
+ return tree->muxes && mapped_id >= tree->muxes_offs &&
+ mapped_id < tree->muxes_offs + tree->num_muxes;
+}
+
+static bool mtk_clk_id_is_gate(const struct mtk_clk_tree *tree, int mapped_id)
+{
+ return tree->gates && mapped_id >= tree->gates_offs &&
+ mapped_id < tree->gates_offs + tree->num_gates;
+}
+
static int mtk_dummy_enable(struct clk *clk)
{
return 0;
@@ -375,15 +403,10 @@ static const int mtk_apmixedsys_of_xlate(struct clk *clk,
return ret;
/* apmixedsys only uses plls and gates. */
+ if (!mtk_clk_id_is_pll(tree, clk->id) && !mtk_clk_id_is_gate(tree, clk->id))
+ return -ENOENT;
- if (tree->plls && clk->id < tree->num_plls)
- return 0;
-
- if (tree->gates && clk->id >= tree->gates_offs &&
- clk->id < tree->gates_offs + tree->num_gates)
- return 0;
-
- return -ENOENT;
+ return 0;
}
static unsigned long __mtk_pll_recalc_rate(const struct mtk_pll_data *pll,
@@ -497,7 +520,7 @@ static ulong mtk_apmixedsys_set_rate(struct clk *clk, ulong rate)
u32 pcw = 0;
u32 postdiv;
- if (priv->tree->gates && clk->id >= priv->tree->gates_offs)
+ if (!mtk_clk_id_is_pll(priv->tree, clk->id))
return -EINVAL;
mtk_pll_calc_values(priv, clk->id, &pcw, &postdiv, rate);
@@ -515,7 +538,7 @@ static ulong mtk_apmixedsys_get_rate(struct clk *clk)
u32 pcw;
/* GATE handling */
- if (priv->tree->gates && clk->id >= priv->tree->gates_offs) {
+ if (mtk_clk_id_is_gate(priv->tree, clk->id)) {
gate = &priv->tree->gates[clk->id - priv->tree->gates_offs];
return mtk_clk_find_parent_rate(clk, gate->parent, NULL);
}
@@ -541,7 +564,7 @@ static int mtk_apmixedsys_enable(struct clk *clk)
u32 r;
/* GATE handling */
- if (priv->tree->gates && clk->id >= priv->tree->gates_offs) {
+ if (mtk_clk_id_is_gate(priv->tree, clk->id)) {
gate = &priv->tree->gates[clk->id - priv->tree->gates_offs];
return mtk_gate_enable(priv->base, gate);
}
@@ -579,7 +602,7 @@ static int mtk_apmixedsys_disable(struct clk *clk)
u32 r;
/* GATE handling */
- if (priv->tree->gates && clk->id >= priv->tree->gates_offs) {
+ if (mtk_clk_id_is_gate(priv->tree, clk->id)) {
gate = &priv->tree->gates[clk->id - priv->tree->gates_offs];
return mtk_gate_disable(priv->base, gate);
}
@@ -649,23 +672,11 @@ static const int mtk_topckgen_of_xlate(struct clk *clk,
return ret;
/* topckgen only uses fclks, fdivs, muxes and gates. */
+ if (!mtk_clk_id_is_fclk(tree, clk->id) && !mtk_clk_id_is_fdiv(tree, clk->id) &&
+ !mtk_clk_id_is_mux(tree, clk->id) && !mtk_clk_id_is_gate(tree, clk->id))
+ return -ENOENT;
- if (tree->fclks && clk->id < tree->num_fclks)
- return 0;
-
- if (tree->fdivs && clk->id >= tree->fdivs_offs &&
- clk->id < tree->fdivs_offs + tree->num_fdivs)
- return 0;
-
- if (tree->muxes && clk->id >= tree->muxes_offs &&
- clk->id < tree->muxes_offs + tree->num_muxes)
- return 0;
-
- if (tree->gates && clk->id >= tree->gates_offs &&
- clk->id < tree->gates_offs + tree->num_gates)
- return 0;
-
- return -ENOENT;
+ return 0;
}
static ulong mtk_factor_recalc_rate(const struct mtk_fixed_factor *fdiv,
@@ -723,21 +734,22 @@ static ulong mtk_topckgen_get_rate(struct clk *clk)
struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
const struct mtk_clk_tree *tree = priv->tree;
- if (tree->gates && clk->id >= tree->gates_offs &&
- clk->id < tree->gates_offs + tree->num_gates) {
+ if (mtk_clk_id_is_fclk(tree, clk->id))
+ return tree->fclks[clk->id].rate;
+
+ if (mtk_clk_id_is_fdiv(tree, clk->id))
+ return mtk_topckgen_get_factor_rate(clk, clk->id - tree->fdivs_offs);
+
+ if (mtk_clk_id_is_mux(tree, clk->id))
+ return mtk_topckgen_get_mux_rate(clk, clk->id - tree->muxes_offs);
+
+ if (mtk_clk_id_is_gate(tree, clk->id)) {
const struct mtk_gate *gate = &tree->gates[clk->id - tree->gates_offs];
return mtk_clk_find_parent_rate(clk, gate->parent, NULL);
}
- if (clk->id < priv->tree->fdivs_offs)
- return priv->tree->fclks[clk->id].rate;
- else if (clk->id < priv->tree->muxes_offs)
- return mtk_topckgen_get_factor_rate(clk, clk->id -
- priv->tree->fdivs_offs);
- else
- return mtk_topckgen_get_mux_rate(clk, clk->id -
- priv->tree->muxes_offs);
+ return -ENOENT;
}
static int mtk_clk_mux_enable(struct clk *clk)
@@ -746,7 +758,7 @@ static int mtk_clk_mux_enable(struct clk *clk)
const struct mtk_composite *mux;
u32 val;
- if (clk->id < priv->tree->muxes_offs)
+ if (!mtk_clk_id_is_mux(priv->tree, clk->id))
return 0;
mux = &priv->tree->muxes[clk->id - priv->tree->muxes_offs];
@@ -778,8 +790,7 @@ static int mtk_topckgen_enable(struct clk *clk)
struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
const struct mtk_clk_tree *tree = priv->tree;
- if (tree->gates && clk->id >= tree->gates_offs &&
- clk->id < tree->gates_offs + tree->num_gates) {
+ if (mtk_clk_id_is_gate(tree, clk->id)) {
const struct mtk_gate *gate = &tree->gates[clk->id - tree->gates_offs];
return mtk_gate_enable(priv->base, gate);
@@ -794,7 +805,7 @@ static int mtk_clk_mux_disable(struct clk *clk)
const struct mtk_composite *mux;
u32 val;
- if (clk->id < priv->tree->muxes_offs)
+ if (!mtk_clk_id_is_mux(priv->tree, clk->id))
return 0;
mux = &priv->tree->muxes[clk->id - priv->tree->muxes_offs];
@@ -819,8 +830,7 @@ static int mtk_topckgen_disable(struct clk *clk)
struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
const struct mtk_clk_tree *tree = priv->tree;
- if (tree->gates && clk->id >= tree->gates_offs &&
- clk->id < tree->gates_offs + tree->num_gates) {
+ if (mtk_clk_id_is_gate(tree, clk->id)) {
const struct mtk_gate *gate = &tree->gates[clk->id - tree->gates_offs];
return mtk_gate_disable(priv->base, gate);
@@ -836,8 +846,7 @@ static int mtk_common_clk_set_parent(struct clk *clk, struct clk *parent)
int parent_unmapped_id;
u32 parent_type;
- if (!priv->tree->muxes || clk->id < priv->tree->muxes_offs ||
- clk->id >= priv->tree->muxes_offs + priv->tree->num_muxes)
+ if (!mtk_clk_id_is_mux(priv->tree, clk->id))
return 0;
if (!parent_priv)
@@ -917,20 +926,11 @@ static const int mtk_infrasys_of_xlate(struct clk *clk,
return ret;
/* ifrasys only uses fdivs, muxes and gates. */
+ if (!mtk_clk_id_is_fdiv(tree, clk->id) && !mtk_clk_id_is_mux(tree, clk->id) &&
+ !mtk_clk_id_is_gate(tree, clk->id))
+ return -ENOENT;
- if (tree->fdivs && clk->id >= tree->fdivs_offs &&
- clk->id < tree->fdivs_offs + tree->num_fdivs)
- return 0;
-
- if (tree->muxes && clk->id >= tree->muxes_offs &&
- clk->id < tree->muxes_offs + tree->num_muxes)
- return 0;
-
- if (tree->gates && clk->id >= tree->gates_offs &&
- clk->id < tree->gates_offs + tree->num_gates)
- return 0;
-
- return -ENOENT;
+ return 0;
}
static int mtk_clk_infrasys_enable(struct clk *clk)
@@ -939,7 +939,7 @@ static int mtk_clk_infrasys_enable(struct clk *clk)
const struct mtk_gate *gate;
/* MUX handling */
- if (!priv->tree->gates || clk->id < priv->tree->gates_offs)
+ if (!mtk_clk_id_is_gate(priv->tree, clk->id))
return mtk_clk_mux_enable(clk);
gate = &priv->tree->gates[clk->id - priv->tree->gates_offs];
@@ -952,7 +952,7 @@ static int mtk_clk_infrasys_disable(struct clk *clk)
const struct mtk_gate *gate;
/* MUX handling */
- if (!priv->tree->gates || clk->id < priv->tree->gates_offs)
+ if (!mtk_clk_id_is_gate(priv->tree, clk->id))
return mtk_clk_mux_disable(clk);
gate = &priv->tree->gates[clk->id - priv->tree->gates_offs];
@@ -1004,13 +1004,13 @@ static ulong mtk_infrasys_get_rate(struct clk *clk)
struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
ulong rate;
- if (clk->id < priv->tree->fdivs_offs) {
+ if (mtk_clk_id_is_fclk(priv->tree, clk->id)) {
rate = priv->tree->fclks[clk->id].rate;
- } else if (clk->id < priv->tree->muxes_offs) {
+ } else if (mtk_clk_id_is_fdiv(priv->tree, clk->id)) {
rate = mtk_infrasys_get_factor_rate(clk, clk->id -
priv->tree->fdivs_offs);
/* No gates defined or ID is a MUX */
- } else if (!priv->tree->gates || clk->id < priv->tree->gates_offs) {
+ } else if (!mtk_clk_id_is_gate(priv->tree, clk->id)) {
rate = mtk_infrasys_get_mux_rate(clk, clk->id -
priv->tree->muxes_offs);
/* Only valid with muxes + gates implementation */
--
2.43.0
More information about the U-Boot
mailing list