1 // SPDX-License-Identifier: GPL-2.0-only
3 * OMAP APLL clock support
5 * Copyright (C) 2013 Texas Instruments, Inc.
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/string.h>
17 #include <linux/log2.h>
19 #include <linux/of_address.h>
20 #include <linux/clk/ti.h>
21 #include <linux/delay.h>
25 #define APLL_FORCE_LOCK 0x1
26 #define APLL_AUTO_IDLE 0x2
27 #define MAX_APLL_WAIT_TRIES 1000000
30 #define pr_fmt(fmt) "%s: " fmt, __func__
32 static int dra7_apll_enable(struct clk_hw *hw)
34 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
45 clk_name = clk_hw_get_name(&clk->hw);
47 state <<= __ffs(ad->idlest_mask);
49 /* Check is already locked */
50 v = ti_clk_ll_ops->clk_readl(&ad->idlest_reg);
52 if ((v & ad->idlest_mask) == state)
55 v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
56 v &= ~ad->enable_mask;
57 v |= APLL_FORCE_LOCK << __ffs(ad->enable_mask);
58 ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
60 state <<= __ffs(ad->idlest_mask);
63 v = ti_clk_ll_ops->clk_readl(&ad->idlest_reg);
64 if ((v & ad->idlest_mask) == state)
66 if (i > MAX_APLL_WAIT_TRIES)
72 if (i == MAX_APLL_WAIT_TRIES) {
73 pr_warn("clock: %s failed transition to '%s'\n",
74 clk_name, (state) ? "locked" : "bypassed");
77 pr_debug("clock: %s transition to '%s' in %d loops\n",
78 clk_name, (state) ? "locked" : "bypassed", i);
83 static void dra7_apll_disable(struct clk_hw *hw)
85 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
92 state <<= __ffs(ad->idlest_mask);
94 v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
95 v &= ~ad->enable_mask;
96 v |= APLL_AUTO_IDLE << __ffs(ad->enable_mask);
97 ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
100 static int dra7_apll_is_enabled(struct clk_hw *hw)
102 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
103 struct dpll_data *ad;
108 v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
109 v &= ad->enable_mask;
111 v >>= __ffs(ad->enable_mask);
113 return v == APLL_AUTO_IDLE ? 0 : 1;
116 static u8 dra7_init_apll_parent(struct clk_hw *hw)
121 static const struct clk_ops apll_ck_ops = {
122 .enable = &dra7_apll_enable,
123 .disable = &dra7_apll_disable,
124 .is_enabled = &dra7_apll_is_enabled,
125 .get_parent = &dra7_init_apll_parent,
128 static void __init omap_clk_register_apll(void *user,
129 struct device_node *node)
131 struct clk_hw *hw = user;
132 struct clk_hw_omap *clk_hw = to_clk_hw_omap(hw);
133 struct dpll_data *ad = clk_hw->dpll_data;
136 const struct clk_init_data *init = clk_hw->hw.init;
138 clk = of_clk_get(node, 0);
140 pr_debug("clk-ref for %pOFn not ready, retry\n",
142 if (!ti_clk_retry_init(node, hw, omap_clk_register_apll))
148 ad->clk_ref = __clk_get_hw(clk);
150 clk = of_clk_get(node, 1);
152 pr_debug("clk-bypass for %pOFn not ready, retry\n",
154 if (!ti_clk_retry_init(node, hw, omap_clk_register_apll))
160 ad->clk_bypass = __clk_get_hw(clk);
162 name = ti_dt_clk_name(node);
163 clk = of_ti_clk_register_omap_hw(node, &clk_hw->hw, name);
165 of_clk_add_provider(node, of_clk_src_simple_get, clk);
166 kfree(init->parent_names);
172 kfree(clk_hw->dpll_data);
173 kfree(init->parent_names);
178 static void __init of_dra7_apll_setup(struct device_node *node)
180 struct dpll_data *ad = NULL;
181 struct clk_hw_omap *clk_hw = NULL;
182 struct clk_init_data *init = NULL;
183 const char **parent_names = NULL;
186 ad = kzalloc(sizeof(*ad), GFP_KERNEL);
187 clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
188 init = kzalloc(sizeof(*init), GFP_KERNEL);
189 if (!ad || !clk_hw || !init)
192 clk_hw->dpll_data = ad;
193 clk_hw->hw.init = init;
195 init->name = ti_dt_clk_name(node);
196 init->ops = &apll_ck_ops;
198 init->num_parents = of_clk_get_parent_count(node);
199 if (init->num_parents < 1) {
200 pr_err("dra7 apll %pOFn must have parent(s)\n", node);
204 parent_names = kcalloc(init->num_parents, sizeof(char *), GFP_KERNEL);
208 of_clk_parent_fill(node, parent_names, init->num_parents);
210 init->parent_names = parent_names;
212 ret = ti_clk_get_reg_addr(node, 0, &ad->control_reg);
213 ret |= ti_clk_get_reg_addr(node, 1, &ad->idlest_reg);
218 ad->idlest_mask = 0x1;
219 ad->enable_mask = 0x3;
221 omap_clk_register_apll(&clk_hw->hw, node);
230 CLK_OF_DECLARE(dra7_apll_clock, "ti,dra7-apll-clock", of_dra7_apll_setup);
232 #define OMAP2_EN_APLL_LOCKED 0x3
233 #define OMAP2_EN_APLL_STOPPED 0x0
235 static int omap2_apll_is_enabled(struct clk_hw *hw)
237 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
238 struct dpll_data *ad = clk->dpll_data;
241 v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
242 v &= ad->enable_mask;
244 v >>= __ffs(ad->enable_mask);
246 return v == OMAP2_EN_APLL_LOCKED ? 1 : 0;
249 static unsigned long omap2_apll_recalc(struct clk_hw *hw,
250 unsigned long parent_rate)
252 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
254 if (omap2_apll_is_enabled(hw))
255 return clk->fixed_rate;
260 static int omap2_apll_enable(struct clk_hw *hw)
262 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
263 struct dpll_data *ad = clk->dpll_data;
267 v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
268 v &= ~ad->enable_mask;
269 v |= OMAP2_EN_APLL_LOCKED << __ffs(ad->enable_mask);
270 ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
273 v = ti_clk_ll_ops->clk_readl(&ad->idlest_reg);
274 if (v & ad->idlest_mask)
276 if (i > MAX_APLL_WAIT_TRIES)
282 if (i == MAX_APLL_WAIT_TRIES) {
283 pr_warn("%s failed to transition to locked\n",
284 clk_hw_get_name(&clk->hw));
291 static void omap2_apll_disable(struct clk_hw *hw)
293 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
294 struct dpll_data *ad = clk->dpll_data;
297 v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
298 v &= ~ad->enable_mask;
299 v |= OMAP2_EN_APLL_STOPPED << __ffs(ad->enable_mask);
300 ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
303 static const struct clk_ops omap2_apll_ops = {
304 .enable = &omap2_apll_enable,
305 .disable = &omap2_apll_disable,
306 .is_enabled = &omap2_apll_is_enabled,
307 .recalc_rate = &omap2_apll_recalc,
310 static void omap2_apll_set_autoidle(struct clk_hw_omap *clk, u32 val)
312 struct dpll_data *ad = clk->dpll_data;
315 v = ti_clk_ll_ops->clk_readl(&ad->autoidle_reg);
316 v &= ~ad->autoidle_mask;
317 v |= val << __ffs(ad->autoidle_mask);
318 ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
321 #define OMAP2_APLL_AUTOIDLE_LOW_POWER_STOP 0x3
322 #define OMAP2_APLL_AUTOIDLE_DISABLE 0x0
324 static void omap2_apll_allow_idle(struct clk_hw_omap *clk)
326 omap2_apll_set_autoidle(clk, OMAP2_APLL_AUTOIDLE_LOW_POWER_STOP);
329 static void omap2_apll_deny_idle(struct clk_hw_omap *clk)
331 omap2_apll_set_autoidle(clk, OMAP2_APLL_AUTOIDLE_DISABLE);
334 static const struct clk_hw_omap_ops omap2_apll_hwops = {
335 .allow_idle = &omap2_apll_allow_idle,
336 .deny_idle = &omap2_apll_deny_idle,
339 static void __init of_omap2_apll_setup(struct device_node *node)
341 struct dpll_data *ad = NULL;
342 struct clk_hw_omap *clk_hw = NULL;
343 struct clk_init_data *init = NULL;
346 const char *parent_name;
350 ad = kzalloc(sizeof(*ad), GFP_KERNEL);
351 clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
352 init = kzalloc(sizeof(*init), GFP_KERNEL);
354 if (!ad || !clk_hw || !init)
357 clk_hw->dpll_data = ad;
358 clk_hw->hw.init = init;
359 init->ops = &omap2_apll_ops;
360 name = ti_dt_clk_name(node);
362 clk_hw->ops = &omap2_apll_hwops;
364 init->num_parents = of_clk_get_parent_count(node);
365 if (init->num_parents != 1) {
366 pr_err("%pOFn must have one parent\n", node);
370 parent_name = of_clk_get_parent_name(node, 0);
371 init->parent_names = &parent_name;
373 if (of_property_read_u32(node, "ti,clock-frequency", &val)) {
374 pr_err("%pOFn missing clock-frequency\n", node);
377 clk_hw->fixed_rate = val;
379 clk_hw->enable_bit = ti_clk_get_legacy_bit_shift(node);
380 ad->enable_mask = 0x3 << clk_hw->enable_bit;
381 ad->autoidle_mask = 0x3 << clk_hw->enable_bit;
383 if (of_property_read_u32(node, "ti,idlest-shift", &val)) {
384 pr_err("%pOFn missing idlest-shift\n", node);
388 ad->idlest_mask = 1 << val;
390 ret = ti_clk_get_reg_addr(node, 0, &ad->control_reg);
391 ret |= ti_clk_get_reg_addr(node, 1, &ad->autoidle_reg);
392 ret |= ti_clk_get_reg_addr(node, 2, &ad->idlest_reg);
397 name = ti_dt_clk_name(node);
398 clk = of_ti_clk_register_omap_hw(node, &clk_hw->hw, name);
400 of_clk_add_provider(node, of_clk_src_simple_get, clk);
409 CLK_OF_DECLARE(omap2_apll_clock, "ti,omap2-apll-clock",
410 of_omap2_apll_setup);