1 // SPDX-License-Identifier: GPL-2.0-only
7 #include <linux/clk-provider.h>
8 #include <linux/export.h>
9 #include <linux/slab.h>
20 static inline struct clk_cpu *to_clk_cpu(struct clk_hw *hw)
22 return container_of(hw, struct clk_cpu, hw);
25 static unsigned long clk_cpu_recalc_rate(struct clk_hw *hw,
26 unsigned long parent_rate)
28 struct clk_cpu *cpu = to_clk_cpu(hw);
30 return clk_get_rate(cpu->div);
33 static long clk_cpu_round_rate(struct clk_hw *hw, unsigned long rate,
36 struct clk_cpu *cpu = to_clk_cpu(hw);
38 return clk_round_rate(cpu->pll, rate);
41 static int clk_cpu_set_rate(struct clk_hw *hw, unsigned long rate,
42 unsigned long parent_rate)
44 struct clk_cpu *cpu = to_clk_cpu(hw);
47 /* switch to PLL bypass clock */
48 ret = clk_set_parent(cpu->mux, cpu->step);
53 ret = clk_set_rate(cpu->pll, rate);
55 clk_set_parent(cpu->mux, cpu->pll);
58 /* switch back to PLL clock */
59 clk_set_parent(cpu->mux, cpu->pll);
61 /* Ensure the divider is what we expect */
62 clk_set_rate(cpu->div, rate);
67 static const struct clk_ops clk_cpu_ops = {
68 .recalc_rate = clk_cpu_recalc_rate,
69 .round_rate = clk_cpu_round_rate,
70 .set_rate = clk_cpu_set_rate,
73 struct clk_hw *imx_clk_hw_cpu(const char *name, const char *parent_name,
74 struct clk *div, struct clk *mux, struct clk *pll,
79 struct clk_init_data init;
82 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
84 return ERR_PTR(-ENOMEM);
92 init.ops = &clk_cpu_ops;
93 init.flags = CLK_IS_CRITICAL;
94 init.parent_names = &parent_name;
100 ret = clk_hw_register(NULL, hw);
108 EXPORT_SYMBOL_GPL(imx_clk_hw_cpu);