[PATCH v4 1/8] clk: renesas: r8a78000: Add clk_hw to clk_map
Geert Uytterhoeven
geert at linux-m68k.org
Mon Oct 19 07:56:28 PDT 2026
Hi Laurent,
On Mon, 19 Oct 2026 at 16:42, Laurent Pinchart
<laurent.pinchart at ideasonboard.com> wrote:
> On Fri, Oct 16, 2026 at 05:55:40PM +0200, Geert Uytterhoeven wrote:
> > Prepare for mapping to other than fixed-rate clocks by adding a clk_hw
> > pointer to the clk_map structure, and populating it up-front.
> >
> > Move r8a78000_cpg_priv.fixed_hws into fill_clk_map(), as it is no longer
> > needed afterwards, and drop NUM_FIXED_CLKS.
> >
> > Signed-off-by: Geert Uytterhoeven <geert+renesas at glider.be>
> > --- a/drivers/clk/renesas/r8a78000-cpg.c
> > +++ b/drivers/clk/renesas/r8a78000-cpg.c
> > @@ -16,19 +16,24 @@
> >
> > #include <dt-bindings/clock/renesas,r8a78000-cpg.h>
> >
> > +struct clk_map_in {
>
> The "_in" suffix isn't very explicit.
I can add a comment...
> > + int dt_id; /* DT binding clock ID or -1 sentinel */
> > + u32 fw_id; /* FIXED_CLK() ID */
> > +};
> > +
> > struct clk_map {
> > int dt_id; /* DT binding clock ID or -1 sentinel */
> > u32 fw_id; /* FIXED_CLK() ID */
BTW, do you think it would be worthwhile to reuse clk_map_in here?
> > + struct clk_hw *hw;
> > };
> > @@ -92,35 +94,69 @@ static struct clk_hw *r8a78000_clk_get(struct of_phandle_args *spec,
> > return hw;
> > }
> >
> > -static int register_fixed_clks(struct r8a78000_cpg_priv *priv)
> > +static struct clk_hw **register_fixed_clks(struct device *dev)
> > {
> > - struct device *dev = priv->dev;
> > + unsigned int n = ARRAY_SIZE(fixed_clk_rates);
> > + struct clk_hw **hws, *hw;
> > unsigned long rate;
> > - struct clk_hw *hw;
> > const char *name;
> >
> > - for (unsigned int i = 0; i < ARRAY_SIZE(fixed_clk_rates); i++) {
> > + hws = devm_kcalloc(dev, n, sizeof(*hws), GFP_KERNEL);
> > + if (!hws)
> > + return ERR_PTR(-ENOMEM);
> > +
> > + for (unsigned int i = 0; i < n; i++) {
> > rate = fixed_clk_rates[i];
> > name = devm_kasprintf(dev, GFP_KERNEL, "cpg-%lu", rate);
> > if (!name)
> > - return -ENOMEM;
> > + return ERR_PTR(-ENOMEM);
> >
> > hw = devm_clk_hw_register_fixed_rate(dev, name, NULL, 0, rate);
> > if (IS_ERR(hw))
> > - return PTR_ERR(hw);
> > + return ERR_CAST(hw);
> >
> > - priv->fixed_hws[i] = hw;
> > + hws[i] = hw;
> > }
> >
> > - return 0;
> > + return hws;
> > +}
> > +
> > +static const struct clk_map *fill_clk_map(struct r8a78000_cpg_priv *priv,
> > + const struct clk_map_in *map_in)
> > +{
> > + struct device *dev = priv->dev;
> > + struct clk_hw **fixed_hws;
> > + struct clk_map *map;
> > + unsigned int i;
> > +
> > + fixed_hws = register_fixed_clks(dev);
> > + if (IS_ERR(fixed_hws))
> > + return ERR_CAST(fixed_hws);
> > +
> > + for (i = 0; map_in[i].dt_id >= 0; i++) { }
>
> This is avoidable by passing { map, ARRAY_SIZE(map) } as match data
> instead of just the map.
True, and I did consider this.
The same mechanism is used in the MDLC driver, where using ARRAY_SIZE()
would quickly became a bigger burden due to the presence of many
more maps. So for consistency, I kept it the same in both drivers.
An initialization macro might make it more manageable, though.
> > +
> > + map = devm_kcalloc(dev, i + 1, sizeof(*map), GFP_KERNEL);
> > + if (!map)
> > + return ERR_PTR(-ENOMEM);
> > +
> > + for (i = 0; ; i++) {
> > + map[i].dt_id = map_in[i].dt_id;
> > + if (map[i].dt_id < 0)
> > + break;
> > +
> > + map[i].fw_id = map_in[i].fw_id;
> > + map[i].hw = fixed_hws[map[i].fw_id];
> > + }
> > +
> > + devm_kfree(dev, fixed_hws);
>
> Why not plain kcalloc/kfree ?
To avoid having to free it manually at multiple (three) locations,
both in the error paths and the success path?
>
> > + return map;
> > }
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
More information about the linux-arm-kernel
mailing list