[PATCH v1 3/6] clk: starfive: jh71x0: Add generic CRG probe helper

Jerome Brunet jbrunet at baylibre.com
Sun Nov 15 06:18:28 PST 2026


> 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 at 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)

I'm really not a fan of this hack.
In addition to being hackish it will limit your controllers to 127 internal clocks
which I'm pretty sure is a limit you'll hit eventually.

I'd prefer if a parent was defined by a struct of yours, something like

struct jh71x0_parent_data {
	const char *fw_name; // <= external if not NULL
	unsigned int internal_id; // <= id in your table if the above is NULL
}

With this, the bit, the macro that goes with and the table of external clock
can all go away.

I suppose it means some change in your existing drivers too

-- 
Jerome




More information about the linux-riscv mailing list