mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 v2 3/6] clk: starfive: jh71x0: Add generic CRG probe helper
Date: Wed, 18 Nov 2026 02:05:20 -0800	[thread overview]
Message-ID: <20261118100523.11395-4-changhuang.liang@starfivetech.com> (raw)
In-Reply-To: <20261118100523.11395-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 and reset
auxiliary device name. The reset auxiliary device is registered with
jh71x0_reset_controller_register().

The clock table is made of struct jh71x0_crg_clk_data entries, where
each parent is described by a struct jh71x0_parent_data: either the
name of an external clock or the index of another clock of the CRG.
This way the external clocks need no index of their own, and the number
of clocks a CRG can provide is not limited by the width of a parent
index. The existing JH71X0_GATE(), JH71X0__DIV(), ... macros can be
used to fill the table.

The JH7100 and JH7110 drivers keep their own probe functions and the
index based struct jh71x0_clk_data for now.

The clock index space of these CRGs is sparse, so reject parents that
refer to an index that is not registered, and also make jh71x0_clk_get()
reject such indexes when the domain info is available.

Signed-off-by: Changhuang Liang <changhuang.liang@starfivetech.com>
---
 drivers/clk/starfive/clk-starfive-jh71x0.c | 92 +++++++++++++++++++++-
 drivers/clk/starfive/clk-starfive-jh71x0.h | 34 ++++++++
 2 files changed, 123 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/starfive/clk-starfive-jh71x0.c b/drivers/clk/starfive/clk-starfive-jh71x0.c
index db8c3571ea5e..97f8782df211 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,84 @@ 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_flex(&pdev->dev, *priv, reg, info->num_clk);
+	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++) {
+			const struct jh71x0_parent_data *parent =
+				&info->clk_data[idx].parents[i];
+
+			if (parent->fw_name) {
+				parents[i].fw_name = parent->fw_name;
+				continue;
+			}
+
+			if (parent->idx >= info->num_clk ||
+			    !info->clk_data[parent->idx].name)
+				return dev_err_probe(priv->dev, -EINVAL,
+						     "clock %s: invalid parent index %u\n",
+						     init.name, parent->idx);
+
+			parents[i].hw = &priv->reg[parent->idx].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..775c1012a889 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,31 @@ struct jh71x0_clk_data {
 	u8 parents[4];
 };
 
+/*
+ * Parent of a clock registered by jh71x0_crg_probe(): either an external clock
+ * of the CRG, looked up by its name in the "clock-names" property, or another
+ * clock provided by the CRG itself.
+ */
+struct jh71x0_parent_data {
+	const char *fw_name;	/* external clock if not NULL */
+	unsigned int idx;	/* index of a clock of this CRG */
+};
+
+#define JH71X0_EXT_PARENT(_fw_name)	{ .fw_name = _fw_name }
+#define JH71X0_INT_PARENT(_idx)		{ .idx = _idx }
+
+/*
+ * Clock data for jh71x0_crg_probe(). The JH71X0_GATE(), JH71X0__DIV(), ...
+ * macros below can be used to fill it, with the parents described by
+ * JH71X0_EXT_PARENT() or JH71X0_INT_PARENT().
+ */
+struct jh71x0_crg_clk_data {
+	const char *name;
+	unsigned long flags;
+	u32 max;
+	struct jh71x0_parent_data parents[4];
+};
+
 #define JH71X0_GATE(_idx, _name, _flags, _parent)				\
 [_idx] = {									\
 	.name = _name,								\
@@ -109,6 +135,12 @@ struct jh71x0_clk {
 	unsigned int max_div;
 };
 
+struct jh71x0_crg_domain_info {
+	const struct jh71x0_crg_clk_data *clk_data;
+	unsigned int num_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 +148,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 +158,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


  parent reply	other threads:[~2026-11-18 10:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 10:05 [PATCH v2 0/6] Add System-0 CRG for JHB100 SoC Changhuang Liang
2026-11-18 10:05 ` [PATCH v2 1/6] clk: starfive: jh7110-sys: Fix memory leak in reset controller register Changhuang Liang
2026-11-18 10:05 ` [PATCH v2 2/6] clk: starfive: jh71x0: Move reset controller registration to jh71x0 common code Changhuang Liang
2026-11-18 10:05 ` Changhuang Liang [this message]
2026-11-18 10:05 ` [PATCH v2 4/6] clk: starfive: Add JHB100 System-0 clock generator driver Changhuang Liang
2026-11-18 10:05 ` [PATCH v2 5/6] reset: starfive: jh71x0: Support discontiguous reset IDs Changhuang Liang
2026-11-18 10:39   ` Philipp Zabel
2026-11-18 10:05 ` [PATCH v2 6/6] reset: starfive: Add StarFive JHB100 System-0 reset driver Changhuang Liang
2026-11-18 10:39   ` Philipp Zabel

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=20261118100523.11395-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®