From: Changhuang Liang <changhuang.liang@starfivetech.com>
To: Brian Masney <bmasney@redhat.com>,
Jerome Brunet <jbrunet+clk@baylibre.com>,
Stephen Boyd <sboyd@kernel.org>,
Philipp Zabel <p.zabel@pengutronix.de>,
Emil Renner Berthing <kernel@esmil.dk>
Cc: Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-riscv@lists.infradead.org,
Hal Feng <hal.feng@starfivetech.com>,
Changhuang Liang <changhuang.liang@starfivetech.com>
Subject: [PATCH v1 3/6] clk: starfive: jh71x0: Add generic CRG probe helper
Date: Tue, 13 Oct 2026 03:27:04 -0700 [thread overview]
Message-ID: <20261013102707.9207-4-changhuang.liang@starfivetech.com> (raw)
In-Reply-To: <20261013102707.9207-1-changhuang.liang@starfivetech.com>
The StarFive JHB100 SoC has several clock and reset generators (CRGs)
which share the same probe flow: map the registers, register all the
clocks described by a table, add the clock provider and register the
auxiliary reset device.
Add jh71x0_crg_probe() together with struct jh71x0_crg_domain_info so
that each CRG driver only needs to provide its clock table, external
clock names and reset auxiliary device name. The reset auxiliary device
is registered with jh71x0_reset_controller_register().
Parents that refer to an external clock are tagged with
JH71X0_EXT_CLK_FLAG in the parent index, so the external clock indexes
of a driver do not depend on how many clocks the CRG provides and do
not shift when a clock is added to the binding.
The clock index space of these CRGs is sparse, so also make
jh71x0_clk_get() reject indexes that were never registered when the
domain info is available.
Signed-off-by: Changhuang Liang <changhuang.liang@starfivetech.com>
---
drivers/clk/starfive/clk-starfive-jh71x0.c | 96 +++++++++++++++++++++-
drivers/clk/starfive/clk-starfive-jh71x0.h | 21 +++++
2 files changed, 114 insertions(+), 3 deletions(-)
diff --git a/drivers/clk/starfive/clk-starfive-jh71x0.c b/drivers/clk/starfive/clk-starfive-jh71x0.c
index 37bab563c5e0..52c5ae737dbd 100644
--- a/drivers/clk/starfive/clk-starfive-jh71x0.c
+++ b/drivers/clk/starfive/clk-starfive-jh71x0.c
@@ -10,6 +10,7 @@
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/io.h>
+#include <linux/property.h>
#include <soc/starfive/reset-starfive-jh71x0.h>
#include "clk-starfive-jh71x0.h"
@@ -333,10 +334,14 @@ struct clk_hw *jh71x0_clk_get(struct of_phandle_args *clkspec, void *data)
struct jh71x0_clk_priv *priv = data;
unsigned int idx = clkspec->args[0];
- if (idx < priv->num_reg)
- return &priv->reg[idx].hw;
+ if (idx >= priv->num_reg)
+ return ERR_PTR(-EINVAL);
- return ERR_PTR(-EINVAL);
+ /* Index space is sparse: reject holes that were never registered. */
+ if (priv->info && !priv->info->clk_data[idx].name)
+ return ERR_PTR(-ENOENT);
+
+ return &priv->reg[idx].hw;
}
EXPORT_SYMBOL_GPL(jh71x0_clk_get);
@@ -361,3 +366,88 @@ int jh71x0_reset_controller_register(struct jh71x0_clk_priv *priv,
return 0;
}
EXPORT_SYMBOL_GPL(jh71x0_reset_controller_register);
+
+int jh71x0_crg_probe(struct platform_device *pdev)
+{
+ const struct jh71x0_crg_domain_info *info;
+ struct jh71x0_clk_priv *priv;
+ unsigned int idx;
+ int ret;
+
+ info = device_get_match_data(&pdev->dev);
+ if (!info)
+ return -ENODEV;
+
+ priv = devm_kzalloc(&pdev->dev, struct_size(priv, reg, info->num_clk),
+ GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ spin_lock_init(&priv->rmw_lock);
+ priv->info = info;
+ priv->num_reg = info->num_clk;
+ priv->dev = &pdev->dev;
+ priv->base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(priv->base))
+ return PTR_ERR(priv->base);
+
+ for (idx = 0; idx < info->num_clk; idx++) {
+ u32 max = info->clk_data[idx].max;
+ struct clk_parent_data parents[4] = {};
+ struct clk_init_data init = {
+ .name = info->clk_data[idx].name,
+ .ops = starfive_jh71x0_clk_ops(max),
+ .parent_data = parents,
+ .num_parents =
+ ((max & JH71X0_CLK_MUX_MASK) >> JH71X0_CLK_MUX_SHIFT) + 1,
+ .flags = info->clk_data[idx].flags,
+ };
+ struct jh71x0_clk *clk = &priv->reg[idx];
+ unsigned int i;
+
+ if (!init.name)
+ continue;
+
+ if (init.num_parents > ARRAY_SIZE(parents))
+ return dev_err_probe(priv->dev, -EINVAL,
+ "clock %s: too many parents (%u > %zu)\n",
+ init.name, init.num_parents, ARRAY_SIZE(parents));
+
+ for (i = 0; i < init.num_parents; i++) {
+ unsigned int pidx = info->clk_data[idx].parents[i];
+
+ if (pidx & JH71X0_EXT_CLK_FLAG) {
+ pidx &= ~JH71X0_EXT_CLK_FLAG;
+ if (pidx >= info->num_ext_clk)
+ return dev_err_probe(priv->dev, -EINVAL,
+ "clock %s: invalid external parent %u\n",
+ init.name, pidx);
+
+ parents[i].fw_name = info->ext_clk[pidx];
+ } else {
+ if (pidx >= info->num_clk)
+ return dev_err_probe(priv->dev, -EINVAL,
+ "clock %s: invalid parent index %u\n",
+ init.name, pidx);
+
+ parents[i].hw = &priv->reg[pidx].hw;
+ }
+ }
+
+ clk->hw.init = &init;
+ clk->idx = idx;
+ clk->max_div = max & JH71X0_CLK_DIV_MASK;
+
+ ret = devm_clk_hw_register(&pdev->dev, &clk->hw);
+ if (ret)
+ return dev_err_probe(priv->dev, ret, "Failed to register clock %s\n",
+ init.name);
+ }
+
+ ret = devm_of_clk_add_hw_provider(&pdev->dev, jh71x0_clk_get, priv);
+ if (ret)
+ return ret;
+
+ return jh71x0_reset_controller_register(priv, info->rst_name);
+}
+EXPORT_SYMBOL_GPL(jh71x0_crg_probe);
diff --git a/drivers/clk/starfive/clk-starfive-jh71x0.h b/drivers/clk/starfive/clk-starfive-jh71x0.h
index 322a05157faf..666d44e6a752 100644
--- a/drivers/clk/starfive/clk-starfive-jh71x0.h
+++ b/drivers/clk/starfive/clk-starfive-jh71x0.h
@@ -5,6 +5,7 @@
#include <linux/bits.h>
#include <linux/clk-provider.h>
#include <linux/device.h>
+#include <linux/platform_device.h>
#include <linux/spinlock.h>
/* register fields */
@@ -29,6 +30,16 @@ struct jh71x0_clk_data {
u8 parents[4];
};
+/*
+ * Parent indexes with this bit set refer to an external clock, i.e. an entry
+ * of jh71x0_crg_domain_info::ext_clk, instead of a clock provided by the CRG
+ * itself. This keeps the external clock indexes independent of the number of
+ * clocks the CRG provides, which in turn must stay below the flag; drivers
+ * check this with a static_assert() on their clock table.
+ */
+#define JH71X0_EXT_CLK_FLAG BIT(7)
+#define JH71X0_EXT_CLK(_n) (JH71X0_EXT_CLK_FLAG | (_n))
+
#define JH71X0_GATE(_idx, _name, _flags, _parent) \
[_idx] = { \
.name = _name, \
@@ -109,6 +120,14 @@ struct jh71x0_clk {
unsigned int max_div;
};
+struct jh71x0_crg_domain_info {
+ const struct jh71x0_clk_data *clk_data;
+ unsigned int num_clk;
+ const char * const *ext_clk;
+ unsigned int num_ext_clk;
+ const char *rst_name;
+};
+
struct jh71x0_clk_priv {
/* protect clk enable and set rate/parent from happening at the same time */
spinlock_t rmw_lock;
@@ -116,6 +135,7 @@ struct jh71x0_clk_priv {
void __iomem *base;
struct clk *original_clk;
struct notifier_block pll_clk_nb;
+ const struct jh71x0_crg_domain_info *info;
struct clk_hw *pll[3];
unsigned int num_reg;
struct jh71x0_clk reg[] __counted_by(num_reg);
@@ -125,5 +145,6 @@ const struct clk_ops *starfive_jh71x0_clk_ops(u32 max);
struct clk_hw *jh71x0_clk_get(struct of_phandle_args *clkspec, void *data);
int jh71x0_reset_controller_register(struct jh71x0_clk_priv *priv,
const char *adev_name);
+int jh71x0_crg_probe(struct platform_device *pdev);
#endif
--
2.25.1
next prev parent reply other threads:[~2026-10-13 10:27 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 10:27 [PATCH v1 0/6] Add System-0 CRG for JHB100 SoC Changhuang Liang
2026-10-13 10:27 ` [PATCH v1 1/6] clk: starfive: jh7110-sys: Fix memory leak in reset controller register Changhuang Liang
2026-10-13 10:27 ` [PATCH v1 2/6] clk: starfive: jh71x0: Move reset controller registration to jh71x0 common code Changhuang Liang
2026-10-13 10:27 ` Changhuang Liang [this message]
2026-10-13 10:27 ` [PATCH v1 4/6] clk: starfive: Add JHB100 System-0 clock generator driver Changhuang Liang
2026-10-13 10:27 ` [PATCH v1 5/6] reset: starfive: jh71x0: Support discontiguous reset IDs Changhuang Liang
2026-10-13 10:27 ` [PATCH v1 6/6] reset: starfive: Add StarFive JHB100 System-0 reset driver Changhuang Liang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261013102707.9207-4-changhuang.liang@starfivetech.com \
--to=changhuang.liang@starfivetech.com \
--cc=bmasney@redhat.com \
--cc=conor+dt@kernel.org \
--cc=hal.feng@starfivetech.com \
--cc=jbrunet+clk@baylibre.com \
--cc=kernel@esmil.dk \
--cc=krzk+dt@kernel.org \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=p.zabel@pengutronix.de \
--cc=robh@kernel.org \
--cc=sboyd@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®