1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI composite clock support
5 * Copyright (C) 2013 Texas Instruments, Inc.
10 #include <linux/clk-provider.h>
11 #include <linux/slab.h>
14 #include <linux/of_address.h>
15 #include <linux/clk/ti.h>
16 #include <linux/list.h>
21 #define pr_fmt(fmt) "%s: " fmt, __func__
23 static unsigned long ti_composite_recalc_rate(struct clk_hw *hw,
24 unsigned long parent_rate)
26 return ti_clk_divider_ops.recalc_rate(hw, parent_rate);
29 static long ti_composite_round_rate(struct clk_hw *hw, unsigned long rate,
35 static int ti_composite_set_rate(struct clk_hw *hw, unsigned long rate,
36 unsigned long parent_rate)
41 static const struct clk_ops ti_composite_divider_ops = {
42 .recalc_rate = &ti_composite_recalc_rate,
43 .round_rate = &ti_composite_round_rate,
44 .set_rate = &ti_composite_set_rate,
47 static const struct clk_ops ti_composite_gate_ops = {
48 .enable = &omap2_dflt_clk_enable,
49 .disable = &omap2_dflt_clk_disable,
50 .is_enabled = &omap2_dflt_clk_is_enabled,
53 struct component_clk {
55 const char **parent_names;
56 struct device_node *node;
59 struct list_head link;
62 static const char * const component_clk_types[] __initconst = {
63 "gate", "divider", "mux"
66 static LIST_HEAD(component_clks);
68 static struct device_node *_get_component_node(struct device_node *node, int i)
71 struct of_phandle_args clkspec;
73 rc = of_parse_phandle_with_args(node, "clocks", "#clock-cells", i,
81 static struct component_clk *_lookup_component(struct device_node *node)
83 struct component_clk *comp;
85 list_for_each_entry(comp, &component_clks, link) {
86 if (comp->node == node)
92 struct clk_hw_omap_comp {
94 struct device_node *comp_nodes[CLK_COMPONENT_TYPE_MAX];
95 struct component_clk *comp_clks[CLK_COMPONENT_TYPE_MAX];
98 static inline struct clk_hw *_get_hw(struct clk_hw_omap_comp *clk, int idx)
103 if (!clk->comp_clks[idx])
106 return clk->comp_clks[idx]->hw;
109 #define to_clk_hw_comp(_hw) container_of(_hw, struct clk_hw_omap_comp, hw)
111 static void __init _register_composite(void *user,
112 struct device_node *node)
114 struct clk_hw *hw = user;
116 struct clk_hw_omap_comp *cclk = to_clk_hw_comp(hw);
117 struct component_clk *comp;
119 const char **parent_names = NULL;
124 /* Check for presence of each component clock */
125 for (i = 0; i < CLK_COMPONENT_TYPE_MAX; i++) {
126 if (!cclk->comp_nodes[i])
129 comp = _lookup_component(cclk->comp_nodes[i]);
131 pr_debug("component %s not ready for %pOFn, retry\n",
132 cclk->comp_nodes[i]->name, node);
133 if (!ti_clk_retry_init(node, hw,
134 _register_composite))
139 if (cclk->comp_clks[comp->type] != NULL) {
140 pr_err("duplicate component types for %pOFn (%s)!\n",
141 node, component_clk_types[comp->type]);
145 cclk->comp_clks[comp->type] = comp;
147 /* Mark this node as found */
148 cclk->comp_nodes[i] = NULL;
151 /* All components exists, proceed with registration */
152 for (i = CLK_COMPONENT_TYPE_MAX - 1; i >= 0; i--) {
153 comp = cclk->comp_clks[i];
156 if (comp->num_parents) {
157 num_parents = comp->num_parents;
158 parent_names = comp->parent_names;
164 pr_err("%s: no parents found for %pOFn!\n", __func__, node);
168 name = ti_dt_clk_name(node);
169 clk = clk_register_composite(NULL, name,
170 parent_names, num_parents,
171 _get_hw(cclk, CLK_COMPONENT_TYPE_MUX),
173 _get_hw(cclk, CLK_COMPONENT_TYPE_DIVIDER),
174 &ti_composite_divider_ops,
175 _get_hw(cclk, CLK_COMPONENT_TYPE_GATE),
176 &ti_composite_gate_ops, 0);
179 ret = ti_clk_add_alias(NULL, clk, name);
184 of_clk_add_provider(node, of_clk_src_simple_get, clk);
188 /* Free component clock list entries */
189 for (i = 0; i < CLK_COMPONENT_TYPE_MAX; i++) {
190 if (!cclk->comp_clks[i])
192 list_del(&cclk->comp_clks[i]->link);
193 kfree(cclk->comp_clks[i]->parent_names);
194 kfree(cclk->comp_clks[i]);
200 static void __init of_ti_composite_clk_setup(struct device_node *node)
202 unsigned int num_clks;
204 struct clk_hw_omap_comp *cclk;
206 /* Number of component clocks to be put inside this clock */
207 num_clks = of_clk_get_parent_count(node);
210 pr_err("composite clk %pOFn must have component(s)\n", node);
214 cclk = kzalloc(sizeof(*cclk), GFP_KERNEL);
218 /* Get device node pointers for each component clock */
219 for (i = 0; i < num_clks; i++)
220 cclk->comp_nodes[i] = _get_component_node(node, i);
222 _register_composite(&cclk->hw, node);
224 CLK_OF_DECLARE(ti_composite_clock, "ti,composite-clock",
225 of_ti_composite_clk_setup);
228 * ti_clk_add_component - add a component clock to the pool
229 * @node: device node of the component clock
230 * @hw: hardware clock definition for the component clock
231 * @type: type of the component clock
233 * Adds a component clock to the list of available components, so that
234 * it can be registered by a composite clock.
236 int __init ti_clk_add_component(struct device_node *node, struct clk_hw *hw,
239 unsigned int num_parents;
240 const char **parent_names;
241 struct component_clk *clk;
243 num_parents = of_clk_get_parent_count(node);
246 pr_err("component-clock %pOFn must have parent(s)\n", node);
250 parent_names = kcalloc(num_parents, sizeof(char *), GFP_KERNEL);
254 of_clk_parent_fill(node, parent_names, num_parents);
256 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
262 clk->num_parents = num_parents;
263 clk->parent_names = parent_names;
267 list_add(&clk->link, &component_clks);