Linux 4.18.10
[linux/fpc-iii.git] / drivers / clk / qcom / clk-alpha-pll.c
blob3c49a60072f149fa699ce1de70915b4937b11615
1 /*
2 * Copyright (c) 2015, 2018, 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"
21 #include "common.h"
23 #define PLL_MODE(p) ((p)->offset + 0x0)
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_UPDATE BIT(22)
36 # define PLL_UPDATE_BYPASS BIT(23)
37 # define PLL_OFFLINE_ACK BIT(28)
38 # define ALPHA_PLL_ACK_LATCH BIT(29)
39 # define PLL_ACTIVE_FLAG BIT(30)
40 # define PLL_LOCK_DET BIT(31)
42 #define PLL_L_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_L_VAL])
43 #define PLL_ALPHA_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL])
44 #define PLL_ALPHA_VAL_U(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL_U])
46 #define PLL_USER_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL])
47 # define PLL_POST_DIV_SHIFT 8
48 # define PLL_POST_DIV_MASK(p) GENMASK((p)->width, 0)
49 # define PLL_ALPHA_EN BIT(24)
50 # define PLL_ALPHA_MODE BIT(25)
51 # define PLL_VCO_SHIFT 20
52 # define PLL_VCO_MASK 0x3
54 #define PLL_USER_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U])
56 #define PLL_CONFIG_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL])
57 #define PLL_CONFIG_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U])
58 #define PLL_TEST_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL])
59 #define PLL_TEST_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U])
60 #define PLL_STATUS(p) ((p)->offset + (p)->regs[PLL_OFF_STATUS])
61 #define PLL_OPMODE(p) ((p)->offset + (p)->regs[PLL_OFF_OPMODE])
62 #define PLL_FRAC(p) ((p)->offset + (p)->regs[PLL_OFF_FRAC])
64 const u8 clk_alpha_pll_regs[][PLL_OFF_MAX_REGS] = {
65 [CLK_ALPHA_PLL_TYPE_DEFAULT] = {
66 [PLL_OFF_L_VAL] = 0x04,
67 [PLL_OFF_ALPHA_VAL] = 0x08,
68 [PLL_OFF_ALPHA_VAL_U] = 0x0c,
69 [PLL_OFF_USER_CTL] = 0x10,
70 [PLL_OFF_USER_CTL_U] = 0x14,
71 [PLL_OFF_CONFIG_CTL] = 0x18,
72 [PLL_OFF_TEST_CTL] = 0x1c,
73 [PLL_OFF_TEST_CTL_U] = 0x20,
74 [PLL_OFF_STATUS] = 0x24,
76 [CLK_ALPHA_PLL_TYPE_HUAYRA] = {
77 [PLL_OFF_L_VAL] = 0x04,
78 [PLL_OFF_ALPHA_VAL] = 0x08,
79 [PLL_OFF_USER_CTL] = 0x10,
80 [PLL_OFF_CONFIG_CTL] = 0x14,
81 [PLL_OFF_CONFIG_CTL_U] = 0x18,
82 [PLL_OFF_TEST_CTL] = 0x1c,
83 [PLL_OFF_TEST_CTL_U] = 0x20,
84 [PLL_OFF_STATUS] = 0x24,
86 [CLK_ALPHA_PLL_TYPE_BRAMMO] = {
87 [PLL_OFF_L_VAL] = 0x04,
88 [PLL_OFF_ALPHA_VAL] = 0x08,
89 [PLL_OFF_ALPHA_VAL_U] = 0x0c,
90 [PLL_OFF_USER_CTL] = 0x10,
91 [PLL_OFF_CONFIG_CTL] = 0x18,
92 [PLL_OFF_TEST_CTL] = 0x1c,
93 [PLL_OFF_STATUS] = 0x24,
95 [CLK_ALPHA_PLL_TYPE_FABIA] = {
96 [PLL_OFF_L_VAL] = 0x04,
97 [PLL_OFF_USER_CTL] = 0x0c,
98 [PLL_OFF_USER_CTL_U] = 0x10,
99 [PLL_OFF_CONFIG_CTL] = 0x14,
100 [PLL_OFF_CONFIG_CTL_U] = 0x18,
101 [PLL_OFF_TEST_CTL] = 0x1c,
102 [PLL_OFF_TEST_CTL_U] = 0x20,
103 [PLL_OFF_STATUS] = 0x24,
104 [PLL_OFF_OPMODE] = 0x2c,
105 [PLL_OFF_FRAC] = 0x38,
108 EXPORT_SYMBOL_GPL(clk_alpha_pll_regs);
111 * Even though 40 bits are present, use only 32 for ease of calculation.
113 #define ALPHA_REG_BITWIDTH 40
114 #define ALPHA_REG_16BIT_WIDTH 16
115 #define ALPHA_BITWIDTH 32U
116 #define ALPHA_SHIFT(w) min(w, ALPHA_BITWIDTH)
118 #define PLL_HUAYRA_M_WIDTH 8
119 #define PLL_HUAYRA_M_SHIFT 8
120 #define PLL_HUAYRA_M_MASK 0xff
121 #define PLL_HUAYRA_N_SHIFT 0
122 #define PLL_HUAYRA_N_MASK 0xff
123 #define PLL_HUAYRA_ALPHA_WIDTH 16
125 #define FABIA_OPMODE_STANDBY 0x0
126 #define FABIA_OPMODE_RUN 0x1
128 #define FABIA_PLL_OUT_MASK 0x7
129 #define FABIA_PLL_RATE_MARGIN 500
131 #define pll_alpha_width(p) \
132 ((PLL_ALPHA_VAL_U(p) - PLL_ALPHA_VAL(p) == 4) ? \
133 ALPHA_REG_BITWIDTH : ALPHA_REG_16BIT_WIDTH)
135 #define pll_has_64bit_config(p) ((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
137 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
138 struct clk_alpha_pll, clkr)
140 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
141 struct clk_alpha_pll_postdiv, clkr)
143 static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
144 const char *action)
146 u32 val;
147 int count;
148 int ret;
149 const char *name = clk_hw_get_name(&pll->clkr.hw);
151 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
152 if (ret)
153 return ret;
155 for (count = 100; count > 0; count--) {
156 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
157 if (ret)
158 return ret;
159 if (inverse && !(val & mask))
160 return 0;
161 else if ((val & mask) == mask)
162 return 0;
164 udelay(1);
167 WARN(1, "%s failed to %s!\n", name, action);
168 return -ETIMEDOUT;
171 #define wait_for_pll_enable_active(pll) \
172 wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
174 #define wait_for_pll_enable_lock(pll) \
175 wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
177 #define wait_for_pll_disable(pll) \
178 wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
180 #define wait_for_pll_offline(pll) \
181 wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
183 #define wait_for_pll_update(pll) \
184 wait_for_pll(pll, PLL_UPDATE, 1, "update")
186 #define wait_for_pll_update_ack_set(pll) \
187 wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
189 #define wait_for_pll_update_ack_clear(pll) \
190 wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
192 void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
193 const struct alpha_pll_config *config)
195 u32 val, mask;
197 regmap_write(regmap, PLL_L_VAL(pll), config->l);
198 regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
199 regmap_write(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
201 if (pll_has_64bit_config(pll))
202 regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
203 config->config_ctl_hi_val);
205 if (pll_alpha_width(pll) > 32)
206 regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
208 val = config->main_output_mask;
209 val |= config->aux_output_mask;
210 val |= config->aux2_output_mask;
211 val |= config->early_output_mask;
212 val |= config->pre_div_val;
213 val |= config->post_div_val;
214 val |= config->vco_val;
215 val |= config->alpha_en_mask;
216 val |= config->alpha_mode_mask;
218 mask = config->main_output_mask;
219 mask |= config->aux_output_mask;
220 mask |= config->aux2_output_mask;
221 mask |= config->early_output_mask;
222 mask |= config->pre_div_mask;
223 mask |= config->post_div_mask;
224 mask |= config->vco_mask;
226 regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
228 if (pll->flags & SUPPORTS_FSM_MODE)
229 qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
232 static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
234 int ret;
235 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
236 u32 val;
238 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
239 if (ret)
240 return ret;
242 val |= PLL_FSM_ENA;
244 if (pll->flags & SUPPORTS_OFFLINE_REQ)
245 val &= ~PLL_OFFLINE_REQ;
247 ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
248 if (ret)
249 return ret;
251 /* Make sure enable request goes through before waiting for update */
252 mb();
254 return wait_for_pll_enable_active(pll);
257 static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
259 int ret;
260 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
261 u32 val;
263 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
264 if (ret)
265 return;
267 if (pll->flags & SUPPORTS_OFFLINE_REQ) {
268 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
269 PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
270 if (ret)
271 return;
273 ret = wait_for_pll_offline(pll);
274 if (ret)
275 return;
278 /* Disable hwfsm */
279 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
280 PLL_FSM_ENA, 0);
281 if (ret)
282 return;
284 wait_for_pll_disable(pll);
287 static int pll_is_enabled(struct clk_hw *hw, u32 mask)
289 int ret;
290 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
291 u32 val;
293 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
294 if (ret)
295 return ret;
297 return !!(val & mask);
300 static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
302 return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
305 static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
307 return pll_is_enabled(hw, PLL_LOCK_DET);
310 static int clk_alpha_pll_enable(struct clk_hw *hw)
312 int ret;
313 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
314 u32 val, mask;
316 mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
317 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
318 if (ret)
319 return ret;
321 /* If in FSM mode, just vote for it */
322 if (val & PLL_VOTE_FSM_ENA) {
323 ret = clk_enable_regmap(hw);
324 if (ret)
325 return ret;
326 return wait_for_pll_enable_active(pll);
329 /* Skip if already enabled */
330 if ((val & mask) == mask)
331 return 0;
333 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
334 PLL_BYPASSNL, PLL_BYPASSNL);
335 if (ret)
336 return ret;
339 * H/W requires a 5us delay between disabling the bypass and
340 * de-asserting the reset.
342 mb();
343 udelay(5);
345 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
346 PLL_RESET_N, PLL_RESET_N);
347 if (ret)
348 return ret;
350 ret = wait_for_pll_enable_lock(pll);
351 if (ret)
352 return ret;
354 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
355 PLL_OUTCTRL, PLL_OUTCTRL);
357 /* Ensure that the write above goes through before returning. */
358 mb();
359 return ret;
362 static void clk_alpha_pll_disable(struct clk_hw *hw)
364 int ret;
365 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
366 u32 val, mask;
368 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
369 if (ret)
370 return;
372 /* If in FSM mode, just unvote it */
373 if (val & PLL_VOTE_FSM_ENA) {
374 clk_disable_regmap(hw);
375 return;
378 mask = PLL_OUTCTRL;
379 regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
381 /* Delay of 2 output clock ticks required until output is disabled */
382 mb();
383 udelay(1);
385 mask = PLL_RESET_N | PLL_BYPASSNL;
386 regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
389 static unsigned long
390 alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
392 return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
395 static unsigned long
396 alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
397 u32 alpha_width)
399 u64 remainder;
400 u64 quotient;
402 quotient = rate;
403 remainder = do_div(quotient, prate);
404 *l = quotient;
406 if (!remainder) {
407 *a = 0;
408 return rate;
411 /* Upper ALPHA_BITWIDTH bits of Alpha */
412 quotient = remainder << ALPHA_SHIFT(alpha_width);
414 remainder = do_div(quotient, prate);
416 if (remainder)
417 quotient++;
419 *a = quotient;
420 return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
423 static const struct pll_vco *
424 alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
426 const struct pll_vco *v = pll->vco_table;
427 const struct pll_vco *end = v + pll->num_vco;
429 for (; v < end; v++)
430 if (rate >= v->min_freq && rate <= v->max_freq)
431 return v;
433 return NULL;
436 static unsigned long
437 clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
439 u32 l, low, high, ctl;
440 u64 a = 0, prate = parent_rate;
441 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
442 u32 alpha_width = pll_alpha_width(pll);
444 regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
446 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
447 if (ctl & PLL_ALPHA_EN) {
448 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &low);
449 if (alpha_width > 32) {
450 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
451 &high);
452 a = (u64)high << 32 | low;
453 } else {
454 a = low & GENMASK(alpha_width - 1, 0);
457 if (alpha_width > ALPHA_BITWIDTH)
458 a >>= alpha_width - ALPHA_BITWIDTH;
461 return alpha_pll_calc_rate(prate, l, a, alpha_width);
465 static int __clk_alpha_pll_update_latch(struct clk_alpha_pll *pll)
467 int ret;
468 u32 mode;
470 regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
472 /* Latch the input to the PLL */
473 regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
474 PLL_UPDATE);
476 /* Wait for 2 reference cycle before checking ACK bit */
477 udelay(1);
480 * PLL will latch the new L, Alpha and freq control word.
481 * PLL will respond by raising PLL_ACK_LATCH output when new programming
482 * has been latched in and PLL is being updated. When
483 * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
484 * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
486 if (mode & PLL_UPDATE_BYPASS) {
487 ret = wait_for_pll_update_ack_set(pll);
488 if (ret)
489 return ret;
491 regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE, 0);
492 } else {
493 ret = wait_for_pll_update(pll);
494 if (ret)
495 return ret;
498 ret = wait_for_pll_update_ack_clear(pll);
499 if (ret)
500 return ret;
502 /* Wait for PLL output to stabilize */
503 udelay(10);
505 return 0;
508 static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
509 int (*is_enabled)(struct clk_hw *))
511 if (!is_enabled(&pll->clkr.hw) ||
512 !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
513 return 0;
515 return __clk_alpha_pll_update_latch(pll);
518 static int __clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
519 unsigned long prate,
520 int (*is_enabled)(struct clk_hw *))
522 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
523 const struct pll_vco *vco;
524 u32 l, alpha_width = pll_alpha_width(pll);
525 u64 a;
527 rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
528 vco = alpha_pll_find_vco(pll, rate);
529 if (pll->vco_table && !vco) {
530 pr_err("alpha pll not in a valid vco range\n");
531 return -EINVAL;
534 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
536 if (alpha_width > ALPHA_BITWIDTH)
537 a <<= alpha_width - ALPHA_BITWIDTH;
539 if (alpha_width > 32)
540 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
542 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
544 if (vco) {
545 regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
546 PLL_VCO_MASK << PLL_VCO_SHIFT,
547 vco->val << PLL_VCO_SHIFT);
550 regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
551 PLL_ALPHA_EN, PLL_ALPHA_EN);
553 return clk_alpha_pll_update_latch(pll, is_enabled);
556 static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
557 unsigned long prate)
559 return __clk_alpha_pll_set_rate(hw, rate, prate,
560 clk_alpha_pll_is_enabled);
563 static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
564 unsigned long prate)
566 return __clk_alpha_pll_set_rate(hw, rate, prate,
567 clk_alpha_pll_hwfsm_is_enabled);
570 static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
571 unsigned long *prate)
573 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
574 u32 l, alpha_width = pll_alpha_width(pll);
575 u64 a;
576 unsigned long min_freq, max_freq;
578 rate = alpha_pll_round_rate(rate, *prate, &l, &a, alpha_width);
579 if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
580 return rate;
582 min_freq = pll->vco_table[0].min_freq;
583 max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
585 return clamp(rate, min_freq, max_freq);
588 static unsigned long
589 alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
592 * a contains 16 bit alpha_val in two’s compliment number in the range
593 * of [-0.5, 0.5).
595 if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
596 l -= 1;
598 return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
601 static unsigned long
602 alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
603 u32 *l, u32 *a)
605 u64 remainder;
606 u64 quotient;
608 quotient = rate;
609 remainder = do_div(quotient, prate);
610 *l = quotient;
612 if (!remainder) {
613 *a = 0;
614 return rate;
617 quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
618 remainder = do_div(quotient, prate);
620 if (remainder)
621 quotient++;
624 * alpha_val should be in two’s compliment number in the range
625 * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
626 * since alpha value will be subtracted in this case.
628 if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
629 *l += 1;
631 *a = quotient;
632 return alpha_huayra_pll_calc_rate(prate, *l, *a);
635 static unsigned long
636 alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
638 u64 rate = parent_rate, tmp;
639 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
640 u32 l, alpha = 0, ctl, alpha_m, alpha_n;
642 regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
643 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
645 if (ctl & PLL_ALPHA_EN) {
646 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
648 * Depending upon alpha_mode, it can be treated as M/N value or
649 * as a two’s compliment number. When alpha_mode=1,
650 * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
652 * Fout=FIN*(L+(M/N))
654 * M is a signed number (-128 to 127) and N is unsigned
655 * (0 to 255). M/N has to be within +/-0.5.
657 * When alpha_mode=0, it is a two’s compliment number in the
658 * range [-0.5, 0.5).
660 * Fout=FIN*(L+(alpha_val)/2^16)
662 * where alpha_val is two’s compliment number.
664 if (!(ctl & PLL_ALPHA_MODE))
665 return alpha_huayra_pll_calc_rate(rate, l, alpha);
667 alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
668 alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
670 rate *= l;
671 tmp = parent_rate;
672 if (alpha_m >= BIT(PLL_HUAYRA_M_WIDTH - 1)) {
673 alpha_m = BIT(PLL_HUAYRA_M_WIDTH) - alpha_m;
674 tmp *= alpha_m;
675 do_div(tmp, alpha_n);
676 rate -= tmp;
677 } else {
678 tmp *= alpha_m;
679 do_div(tmp, alpha_n);
680 rate += tmp;
683 return rate;
686 return alpha_huayra_pll_calc_rate(rate, l, alpha);
689 static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
690 unsigned long prate)
692 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
693 u32 l, a, ctl, cur_alpha = 0;
695 rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
697 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
699 if (ctl & PLL_ALPHA_EN)
700 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
703 * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
704 * without having to go through the power on sequence.
706 if (clk_alpha_pll_is_enabled(hw)) {
707 if (cur_alpha != a) {
708 pr_err("clock needs to be gated %s\n",
709 clk_hw_get_name(hw));
710 return -EBUSY;
713 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
714 /* Ensure that the write above goes to detect L val change. */
715 mb();
716 return wait_for_pll_enable_lock(pll);
719 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
720 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
722 if (a == 0)
723 regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
724 PLL_ALPHA_EN, 0x0);
725 else
726 regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
727 PLL_ALPHA_EN | PLL_ALPHA_MODE, PLL_ALPHA_EN);
729 return 0;
732 static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
733 unsigned long *prate)
735 u32 l, a;
737 return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
740 const struct clk_ops clk_alpha_pll_ops = {
741 .enable = clk_alpha_pll_enable,
742 .disable = clk_alpha_pll_disable,
743 .is_enabled = clk_alpha_pll_is_enabled,
744 .recalc_rate = clk_alpha_pll_recalc_rate,
745 .round_rate = clk_alpha_pll_round_rate,
746 .set_rate = clk_alpha_pll_set_rate,
748 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
750 const struct clk_ops clk_alpha_pll_huayra_ops = {
751 .enable = clk_alpha_pll_enable,
752 .disable = clk_alpha_pll_disable,
753 .is_enabled = clk_alpha_pll_is_enabled,
754 .recalc_rate = alpha_pll_huayra_recalc_rate,
755 .round_rate = alpha_pll_huayra_round_rate,
756 .set_rate = alpha_pll_huayra_set_rate,
758 EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
760 const struct clk_ops clk_alpha_pll_hwfsm_ops = {
761 .enable = clk_alpha_pll_hwfsm_enable,
762 .disable = clk_alpha_pll_hwfsm_disable,
763 .is_enabled = clk_alpha_pll_hwfsm_is_enabled,
764 .recalc_rate = clk_alpha_pll_recalc_rate,
765 .round_rate = clk_alpha_pll_round_rate,
766 .set_rate = clk_alpha_pll_hwfsm_set_rate,
768 EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
770 static unsigned long
771 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
773 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
774 u32 ctl;
776 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
778 ctl >>= PLL_POST_DIV_SHIFT;
779 ctl &= PLL_POST_DIV_MASK(pll);
781 return parent_rate >> fls(ctl);
784 static const struct clk_div_table clk_alpha_div_table[] = {
785 { 0x0, 1 },
786 { 0x1, 2 },
787 { 0x3, 4 },
788 { 0x7, 8 },
789 { 0xf, 16 },
793 static const struct clk_div_table clk_alpha_2bit_div_table[] = {
794 { 0x0, 1 },
795 { 0x1, 2 },
796 { 0x3, 4 },
800 static long
801 clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
802 unsigned long *prate)
804 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
805 const struct clk_div_table *table;
807 if (pll->width == 2)
808 table = clk_alpha_2bit_div_table;
809 else
810 table = clk_alpha_div_table;
812 return divider_round_rate(hw, rate, prate, table,
813 pll->width, CLK_DIVIDER_POWER_OF_TWO);
816 static long
817 clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
818 unsigned long *prate)
820 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
821 u32 ctl, div;
823 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
825 ctl >>= PLL_POST_DIV_SHIFT;
826 ctl &= BIT(pll->width) - 1;
827 div = 1 << fls(ctl);
829 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
830 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
832 return DIV_ROUND_UP_ULL((u64)*prate, div);
835 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
836 unsigned long parent_rate)
838 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
839 int div;
841 /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
842 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
844 return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
845 PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
846 div << PLL_POST_DIV_SHIFT);
849 const struct clk_ops clk_alpha_pll_postdiv_ops = {
850 .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
851 .round_rate = clk_alpha_pll_postdiv_round_rate,
852 .set_rate = clk_alpha_pll_postdiv_set_rate,
854 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
856 const struct clk_ops clk_alpha_pll_postdiv_ro_ops = {
857 .round_rate = clk_alpha_pll_postdiv_round_ro_rate,
858 .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
860 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);
862 void clk_fabia_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
863 const struct alpha_pll_config *config)
865 u32 val, mask;
867 if (config->l)
868 regmap_write(regmap, PLL_L_VAL(pll), config->l);
870 if (config->alpha)
871 regmap_write(regmap, PLL_FRAC(pll), config->alpha);
873 if (config->config_ctl_val)
874 regmap_write(regmap, PLL_CONFIG_CTL(pll),
875 config->config_ctl_val);
877 if (config->post_div_mask) {
878 mask = config->post_div_mask;
879 val = config->post_div_val;
880 regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
883 regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
884 PLL_UPDATE_BYPASS);
886 regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
888 EXPORT_SYMBOL_GPL(clk_fabia_pll_configure);
890 static int alpha_pll_fabia_enable(struct clk_hw *hw)
892 int ret;
893 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
894 u32 val, opmode_val;
895 struct regmap *regmap = pll->clkr.regmap;
897 ret = regmap_read(regmap, PLL_MODE(pll), &val);
898 if (ret)
899 return ret;
901 /* If in FSM mode, just vote for it */
902 if (val & PLL_VOTE_FSM_ENA) {
903 ret = clk_enable_regmap(hw);
904 if (ret)
905 return ret;
906 return wait_for_pll_enable_active(pll);
909 ret = regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
910 if (ret)
911 return ret;
913 /* Skip If PLL is already running */
914 if ((opmode_val & FABIA_OPMODE_RUN) && (val & PLL_OUTCTRL))
915 return 0;
917 ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
918 if (ret)
919 return ret;
921 ret = regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_STANDBY);
922 if (ret)
923 return ret;
925 ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N,
926 PLL_RESET_N);
927 if (ret)
928 return ret;
930 ret = regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_RUN);
931 if (ret)
932 return ret;
934 ret = wait_for_pll_enable_lock(pll);
935 if (ret)
936 return ret;
938 ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
939 FABIA_PLL_OUT_MASK, FABIA_PLL_OUT_MASK);
940 if (ret)
941 return ret;
943 return regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL,
944 PLL_OUTCTRL);
947 static void alpha_pll_fabia_disable(struct clk_hw *hw)
949 int ret;
950 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
951 u32 val;
952 struct regmap *regmap = pll->clkr.regmap;
954 ret = regmap_read(regmap, PLL_MODE(pll), &val);
955 if (ret)
956 return;
958 /* If in FSM mode, just unvote it */
959 if (val & PLL_FSM_ENA) {
960 clk_disable_regmap(hw);
961 return;
964 ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
965 if (ret)
966 return;
968 /* Disable main outputs */
969 ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), FABIA_PLL_OUT_MASK,
971 if (ret)
972 return;
974 /* Place the PLL in STANDBY */
975 regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_STANDBY);
978 static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw *hw,
979 unsigned long parent_rate)
981 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
982 u32 l, frac, alpha_width = pll_alpha_width(pll);
984 regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
985 regmap_read(pll->clkr.regmap, PLL_FRAC(pll), &frac);
987 return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
990 static int alpha_pll_fabia_set_rate(struct clk_hw *hw, unsigned long rate,
991 unsigned long prate)
993 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
994 u32 val, l, alpha_width = pll_alpha_width(pll);
995 u64 a;
996 unsigned long rrate;
997 int ret = 0;
999 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1000 if (ret)
1001 return ret;
1003 rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1006 * Due to limited number of bits for fractional rate programming, the
1007 * rounded up rate could be marginally higher than the requested rate.
1009 if (rrate > (rate + FABIA_PLL_RATE_MARGIN) || rrate < rate) {
1010 pr_err("Call set rate on the PLL with rounded rates!\n");
1011 return -EINVAL;
1014 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1015 regmap_write(pll->clkr.regmap, PLL_FRAC(pll), a);
1017 return __clk_alpha_pll_update_latch(pll);
1020 const struct clk_ops clk_alpha_pll_fabia_ops = {
1021 .enable = alpha_pll_fabia_enable,
1022 .disable = alpha_pll_fabia_disable,
1023 .is_enabled = clk_alpha_pll_is_enabled,
1024 .set_rate = alpha_pll_fabia_set_rate,
1025 .recalc_rate = alpha_pll_fabia_recalc_rate,
1026 .round_rate = clk_alpha_pll_round_rate,
1028 EXPORT_SYMBOL_GPL(clk_alpha_pll_fabia_ops);
1030 const struct clk_ops clk_alpha_pll_fixed_fabia_ops = {
1031 .enable = alpha_pll_fabia_enable,
1032 .disable = alpha_pll_fabia_disable,
1033 .is_enabled = clk_alpha_pll_is_enabled,
1034 .recalc_rate = alpha_pll_fabia_recalc_rate,
1035 .round_rate = clk_alpha_pll_round_rate,
1037 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_fabia_ops);
1039 static unsigned long clk_alpha_pll_postdiv_fabia_recalc_rate(struct clk_hw *hw,
1040 unsigned long parent_rate)
1042 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1043 u32 i, div = 1, val;
1044 int ret;
1046 if (!pll->post_div_table) {
1047 pr_err("Missing the post_div_table for the PLL\n");
1048 return -EINVAL;
1051 ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
1052 if (ret)
1053 return ret;
1055 val >>= pll->post_div_shift;
1056 val &= BIT(pll->width) - 1;
1058 for (i = 0; i < pll->num_post_div; i++) {
1059 if (pll->post_div_table[i].val == val) {
1060 div = pll->post_div_table[i].div;
1061 break;
1065 return (parent_rate / div);
1068 static long clk_alpha_pll_postdiv_fabia_round_rate(struct clk_hw *hw,
1069 unsigned long rate, unsigned long *prate)
1071 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1073 if (!pll->post_div_table) {
1074 pr_err("Missing the post_div_table for the PLL\n");
1075 return -EINVAL;
1078 return divider_round_rate(hw, rate, prate, pll->post_div_table,
1079 pll->width, CLK_DIVIDER_ROUND_CLOSEST);
1082 static int clk_alpha_pll_postdiv_fabia_set_rate(struct clk_hw *hw,
1083 unsigned long rate, unsigned long parent_rate)
1085 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1086 int i, val = 0, div, ret;
1089 * If the PLL is in FSM mode, then treat set_rate callback as a
1090 * no-operation.
1092 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1093 if (ret)
1094 return ret;
1096 if (val & PLL_VOTE_FSM_ENA)
1097 return 0;
1099 if (!pll->post_div_table) {
1100 pr_err("Missing the post_div_table for the PLL\n");
1101 return -EINVAL;
1104 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
1105 for (i = 0; i < pll->num_post_div; i++) {
1106 if (pll->post_div_table[i].div == div) {
1107 val = pll->post_div_table[i].val;
1108 break;
1112 return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
1113 (BIT(pll->width) - 1) << pll->post_div_shift,
1114 val << pll->post_div_shift);
1117 const struct clk_ops clk_alpha_pll_postdiv_fabia_ops = {
1118 .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
1119 .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
1120 .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
1122 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_fabia_ops);