Some platforms require clock to be always running, e.g. because those clock supply devices which are not otherwise attached to the system. One example is a system where the SoC serves as a crystal oscillator replacement for a programmable logic device. The critical-clock property of a clock controller allows listing clock which must never be turned off. The implementation here is similar to "protected-clock", except protected clock property is currently driver specific. This patch attempts to make a generic implementation of "critical-clock" instead. Unlike "assigned-clocks", the "critical-clock" must be parsed much earlier in __clk_register() to assign CLK_IS_CRITICAL flag to clk_init_data .flags field. The parsing code obviously need to be cleaned up and factor out into separate function. The new match_clkspec() callback is used to determine whether struct clk_hw that is currently being registered matches the clock specifier in the DT "critical-clock" property, and if so, then the CLK_IS_CRITICAL is added to these newly registered clock. This callback is currently driver specific, although I suspect a common and/or generic version of the callback could be added. Also, this new callback could possibly be used to replace (*get) argument of of_clk_add_hw_provider() later on too. Signed-off-by: Marek Vasut <marex@xxxxxxx> Cc: Matti Vaittinen <matti.vaittinen@xxxxxxxxxxxxxxxxx> Cc: Michael Turquette <mturquette@xxxxxxxxxxxx> Cc: Rob Herring <robh+dt@xxxxxxxxxx> Cc: Stephen Boyd <sboyd@xxxxxxxxxx> Cc: devicetree@xxxxxxxxxxxxxxx Cc: linux-power@xxxxxxxxxxxxxxxxx To: linux-clk@xxxxxxxxxxxxxxx --- drivers/clk/clk.c | 41 ++++++++++++++++++++++++++++++++++++ include/linux/clk-provider.h | 3 +++ 2 files changed, 44 insertions(+) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 8de6a22498e70..1e1686fa76e01 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -3872,6 +3872,45 @@ static void clk_core_free_parent_map(struct clk_core *core) kfree(core->parents); } +static void +__clk_register_critical_clock(struct device_node *np, struct clk_core *core, + struct clk_hw *hw) +{ + struct of_phandle_args clkspec; + u32 clksize, clktotal; + int ret, i, index; + + if (!np) + return; + + if (!core->ops->match_clkspec) + return; + + if (of_property_read_u32(np, "#clock-cells", &clksize)) + return; + + /* Clock node with #clock-cells = <0> uses critical-clocks; */ + if (clksize == 0) { + if (of_property_read_bool(np, "critical-clocks") && + !core->ops->match_clkspec(hw, &clkspec)) + core->flags |= CLK_IS_CRITICAL; + return; + } + + clkspec.np = np; + clktotal = of_property_count_u32_elems(np, "critical-clocks"); + clktotal /= clksize; + for (index = 0; index < clktotal; index++) { + for (i = 0; i < clksize; i++) { + ret = of_property_read_u32_index(np, "critical-clocks", + (index * clksize) + i, + &(clkspec.args[i])); + } + if (!core->ops->match_clkspec(hw, &clkspec)) + core->flags |= CLK_IS_CRITICAL; + } +} + static struct clk * __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw) { @@ -3916,6 +3955,8 @@ __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw) core->min_rate = 0; core->max_rate = ULONG_MAX; + __clk_register_critical_clock(np, core, hw); + ret = clk_core_populate_parent_map(core, init); if (ret) goto fail_parents; diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h index 2faa6f7aa8a87..8bc0eedfeb2fd 100644 --- a/include/linux/clk-provider.h +++ b/include/linux/clk-provider.h @@ -205,6 +205,8 @@ struct clk_duty { * directory is provided as an argument. Called with * prepare_lock held. Returns 0 on success, -EERROR otherwise. * + * @match_clkspec: Check whether clk_hw matches DT clock specifier. + * Returns 0 on success, -EERROR otherwise. * * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow * implementations to split any work between atomic (enable) and sleepable @@ -252,6 +254,7 @@ struct clk_ops { int (*init)(struct clk_hw *hw); void (*terminate)(struct clk_hw *hw); void (*debug_init)(struct clk_hw *hw, struct dentry *dentry); + int (*match_clkspec)(struct clk_hw *hw, struct of_phandle_args *clkspec); }; /** -- 2.34.1