2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/export.h>
16 #include <linux/clk-provider.h>
17 #include <linux/regmap.h>
18 #include <linux/delay.h>
20 #include "clk-alpha-pll.h"
24 # define PLL_OUTCTRL BIT(0)
25 # define PLL_BYPASSNL BIT(1)
26 # define PLL_RESET_N BIT(2)
27 # define PLL_OFFLINE_REQ BIT(7)
28 # define PLL_LOCK_COUNT_SHIFT 8
29 # define PLL_LOCK_COUNT_MASK 0x3f
30 # define PLL_BIAS_COUNT_SHIFT 14
31 # define PLL_BIAS_COUNT_MASK 0x3f
32 # define PLL_VOTE_FSM_ENA BIT(20)
33 # define PLL_FSM_ENA BIT(20)
34 # define PLL_VOTE_FSM_RESET BIT(21)
35 # define PLL_OFFLINE_ACK BIT(28)
36 # define PLL_ACTIVE_FLAG BIT(30)
37 # define PLL_LOCK_DET BIT(31)
39 #define PLL_L_VAL 0x04
40 #define PLL_ALPHA_VAL 0x08
41 #define PLL_ALPHA_VAL_U 0x0c
43 #define PLL_USER_CTL 0x10
44 # define PLL_POST_DIV_SHIFT 8
45 # define PLL_POST_DIV_MASK 0xf
46 # define PLL_ALPHA_EN BIT(24)
47 # define PLL_VCO_SHIFT 20
48 # define PLL_VCO_MASK 0x3
50 #define PLL_USER_CTL_U 0x14
52 #define PLL_CONFIG_CTL 0x18
53 #define PLL_CONFIG_CTL_U 0x20
54 #define PLL_TEST_CTL 0x1c
55 #define PLL_TEST_CTL_U 0x20
56 #define PLL_STATUS 0x24
59 * Even though 40 bits are present, use only 32 for ease of calculation.
61 #define ALPHA_REG_BITWIDTH 40
62 #define ALPHA_BITWIDTH 32
63 #define ALPHA_16BIT_MASK 0xffff
65 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
66 struct clk_alpha_pll, clkr)
68 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
69 struct clk_alpha_pll_postdiv, clkr)
71 static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
77 const char *name = clk_hw_get_name(&pll->clkr.hw);
80 ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
84 for (count = 100; count > 0; count--) {
85 ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
88 if (inverse && !(val & mask))
90 else if ((val & mask) == mask)
96 WARN(1, "%s failed to %s!\n", name, action);
100 #define wait_for_pll_enable_active(pll) \
101 wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
103 #define wait_for_pll_enable_lock(pll) \
104 wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
106 #define wait_for_pll_disable(pll) \
107 wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
109 #define wait_for_pll_offline(pll) \
110 wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
112 void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
113 const struct alpha_pll_config *config)
116 u32 off = pll->offset;
118 regmap_write(regmap, off + PLL_L_VAL, config->l);
119 regmap_write(regmap, off + PLL_ALPHA_VAL, config->alpha);
120 regmap_write(regmap, off + PLL_CONFIG_CTL, config->config_ctl_val);
121 regmap_write(regmap, off + PLL_CONFIG_CTL_U, config->config_ctl_hi_val);
123 val = config->main_output_mask;
124 val |= config->aux_output_mask;
125 val |= config->aux2_output_mask;
126 val |= config->early_output_mask;
127 val |= config->pre_div_val;
128 val |= config->post_div_val;
129 val |= config->vco_val;
131 mask = config->main_output_mask;
132 mask |= config->aux_output_mask;
133 mask |= config->aux2_output_mask;
134 mask |= config->early_output_mask;
135 mask |= config->pre_div_mask;
136 mask |= config->post_div_mask;
137 mask |= config->vco_mask;
139 regmap_update_bits(regmap, off + PLL_USER_CTL, mask, val);
141 if (pll->flags & SUPPORTS_FSM_MODE)
142 qcom_pll_set_fsm_mode(regmap, off + PLL_MODE, 6, 0);
145 static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
149 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
152 ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
158 if (pll->flags & SUPPORTS_OFFLINE_REQ)
159 val &= ~PLL_OFFLINE_REQ;
161 ret = regmap_write(pll->clkr.regmap, off + PLL_MODE, val);
165 /* Make sure enable request goes through before waiting for update */
168 return wait_for_pll_enable_active(pll);
171 static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
175 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
178 ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
182 if (pll->flags & SUPPORTS_OFFLINE_REQ) {
183 ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
184 PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
188 ret = wait_for_pll_offline(pll);
194 ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
199 wait_for_pll_disable(pll);
202 static int clk_alpha_pll_enable(struct clk_hw *hw)
205 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
210 mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
211 ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
215 /* If in FSM mode, just vote for it */
216 if (val & PLL_VOTE_FSM_ENA) {
217 ret = clk_enable_regmap(hw);
220 return wait_for_pll_enable_active(pll);
223 /* Skip if already enabled */
224 if ((val & mask) == mask)
227 ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
228 PLL_BYPASSNL, PLL_BYPASSNL);
233 * H/W requires a 5us delay between disabling the bypass and
234 * de-asserting the reset.
239 ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
240 PLL_RESET_N, PLL_RESET_N);
244 ret = wait_for_pll_enable_lock(pll);
248 ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
249 PLL_OUTCTRL, PLL_OUTCTRL);
251 /* Ensure that the write above goes through before returning. */
256 static void clk_alpha_pll_disable(struct clk_hw *hw)
259 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
264 ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
268 /* If in FSM mode, just unvote it */
269 if (val & PLL_VOTE_FSM_ENA) {
270 clk_disable_regmap(hw);
275 regmap_update_bits(pll->clkr.regmap, off + PLL_MODE, mask, 0);
277 /* Delay of 2 output clock ticks required until output is disabled */
281 mask = PLL_RESET_N | PLL_BYPASSNL;
282 regmap_update_bits(pll->clkr.regmap, off + PLL_MODE, mask, 0);
285 static unsigned long alpha_pll_calc_rate(u64 prate, u32 l, u32 a)
287 return (prate * l) + ((prate * a) >> ALPHA_BITWIDTH);
291 alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a)
297 remainder = do_div(quotient, prate);
305 /* Upper ALPHA_BITWIDTH bits of Alpha */
306 quotient = remainder << ALPHA_BITWIDTH;
307 remainder = do_div(quotient, prate);
313 return alpha_pll_calc_rate(prate, *l, *a);
316 static const struct pll_vco *
317 alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
319 const struct pll_vco *v = pll->vco_table;
320 const struct pll_vco *end = v + pll->num_vco;
323 if (rate >= v->min_freq && rate <= v->max_freq)
330 clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
332 u32 l, low, high, ctl;
333 u64 a = 0, prate = parent_rate;
334 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
335 u32 off = pll->offset;
337 regmap_read(pll->clkr.regmap, off + PLL_L_VAL, &l);
339 regmap_read(pll->clkr.regmap, off + PLL_USER_CTL, &ctl);
340 if (ctl & PLL_ALPHA_EN) {
341 regmap_read(pll->clkr.regmap, off + PLL_ALPHA_VAL, &low);
342 if (pll->flags & SUPPORTS_16BIT_ALPHA) {
343 a = low & ALPHA_16BIT_MASK;
345 regmap_read(pll->clkr.regmap, off + PLL_ALPHA_VAL_U,
347 a = (u64)high << 32 | low;
348 a >>= ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH;
352 return alpha_pll_calc_rate(prate, l, a);
355 static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
358 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
359 const struct pll_vco *vco;
360 u32 l, off = pll->offset;
363 rate = alpha_pll_round_rate(rate, prate, &l, &a);
364 vco = alpha_pll_find_vco(pll, rate);
366 pr_err("alpha pll not in a valid vco range\n");
370 regmap_write(pll->clkr.regmap, off + PLL_L_VAL, l);
372 if (pll->flags & SUPPORTS_16BIT_ALPHA) {
373 regmap_write(pll->clkr.regmap, off + PLL_ALPHA_VAL,
374 a & ALPHA_16BIT_MASK);
376 a <<= (ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH);
377 regmap_write(pll->clkr.regmap, off + PLL_ALPHA_VAL_U, a >> 32);
380 regmap_update_bits(pll->clkr.regmap, off + PLL_USER_CTL,
381 PLL_VCO_MASK << PLL_VCO_SHIFT,
382 vco->val << PLL_VCO_SHIFT);
384 regmap_update_bits(pll->clkr.regmap, off + PLL_USER_CTL, PLL_ALPHA_EN,
390 static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
391 unsigned long *prate)
393 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
396 unsigned long min_freq, max_freq;
398 rate = alpha_pll_round_rate(rate, *prate, &l, &a);
399 if (alpha_pll_find_vco(pll, rate))
402 min_freq = pll->vco_table[0].min_freq;
403 max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
405 return clamp(rate, min_freq, max_freq);
408 const struct clk_ops clk_alpha_pll_ops = {
409 .enable = clk_alpha_pll_enable,
410 .disable = clk_alpha_pll_disable,
411 .recalc_rate = clk_alpha_pll_recalc_rate,
412 .round_rate = clk_alpha_pll_round_rate,
413 .set_rate = clk_alpha_pll_set_rate,
415 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
417 const struct clk_ops clk_alpha_pll_hwfsm_ops = {
418 .enable = clk_alpha_pll_hwfsm_enable,
419 .disable = clk_alpha_pll_hwfsm_disable,
420 .recalc_rate = clk_alpha_pll_recalc_rate,
421 .round_rate = clk_alpha_pll_round_rate,
422 .set_rate = clk_alpha_pll_set_rate,
424 EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
427 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
429 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
432 regmap_read(pll->clkr.regmap, pll->offset + PLL_USER_CTL, &ctl);
434 ctl >>= PLL_POST_DIV_SHIFT;
435 ctl &= PLL_POST_DIV_MASK;
437 return parent_rate >> fls(ctl);
440 static const struct clk_div_table clk_alpha_div_table[] = {
450 clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
451 unsigned long *prate)
453 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
455 return divider_round_rate(hw, rate, prate, clk_alpha_div_table,
456 pll->width, CLK_DIVIDER_POWER_OF_TWO);
459 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
460 unsigned long parent_rate)
462 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
465 /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
466 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
468 return regmap_update_bits(pll->clkr.regmap, pll->offset + PLL_USER_CTL,
469 PLL_POST_DIV_MASK << PLL_POST_DIV_SHIFT,
470 div << PLL_POST_DIV_SHIFT);
473 const struct clk_ops clk_alpha_pll_postdiv_ops = {
474 .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
475 .round_rate = clk_alpha_pll_postdiv_round_rate,
476 .set_rate = clk_alpha_pll_postdiv_set_rate,
478 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);