Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clk / qcom / clk-alpha-pll.c
blob21c357c26ec42a8e98d505d48dc62c4eaa569ef2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2015, 2018, The Linux Foundation. All rights reserved.
4 */
6 #include <linux/kernel.h>
7 #include <linux/export.h>
8 #include <linux/clk-provider.h>
9 #include <linux/regmap.h>
10 #include <linux/delay.h>
12 #include "clk-alpha-pll.h"
13 #include "common.h"
15 #define PLL_MODE(p) ((p)->offset + 0x0)
16 # define PLL_OUTCTRL BIT(0)
17 # define PLL_BYPASSNL BIT(1)
18 # define PLL_RESET_N BIT(2)
19 # define PLL_OFFLINE_REQ BIT(7)
20 # define PLL_LOCK_COUNT_SHIFT 8
21 # define PLL_LOCK_COUNT_MASK 0x3f
22 # define PLL_BIAS_COUNT_SHIFT 14
23 # define PLL_BIAS_COUNT_MASK 0x3f
24 # define PLL_VOTE_FSM_ENA BIT(20)
25 # define PLL_FSM_ENA BIT(20)
26 # define PLL_VOTE_FSM_RESET BIT(21)
27 # define PLL_UPDATE BIT(22)
28 # define PLL_UPDATE_BYPASS BIT(23)
29 # define PLL_OFFLINE_ACK BIT(28)
30 # define ALPHA_PLL_ACK_LATCH BIT(29)
31 # define PLL_ACTIVE_FLAG BIT(30)
32 # define PLL_LOCK_DET BIT(31)
34 #define PLL_L_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_L_VAL])
35 #define PLL_CAL_L_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_CAL_L_VAL])
36 #define PLL_ALPHA_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL])
37 #define PLL_ALPHA_VAL_U(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL_U])
39 #define PLL_USER_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL])
40 # define PLL_POST_DIV_SHIFT 8
41 # define PLL_POST_DIV_MASK(p) GENMASK((p)->width, 0)
42 # define PLL_ALPHA_EN BIT(24)
43 # define PLL_ALPHA_MODE BIT(25)
44 # define PLL_VCO_SHIFT 20
45 # define PLL_VCO_MASK 0x3
47 #define PLL_USER_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U])
48 #define PLL_USER_CTL_U1(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U1])
50 #define PLL_CONFIG_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL])
51 #define PLL_CONFIG_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U])
52 #define PLL_CONFIG_CTL_U1(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U1])
53 #define PLL_TEST_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL])
54 #define PLL_TEST_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U])
55 #define PLL_TEST_CTL_U1(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U1])
56 #define PLL_STATUS(p) ((p)->offset + (p)->regs[PLL_OFF_STATUS])
57 #define PLL_OPMODE(p) ((p)->offset + (p)->regs[PLL_OFF_OPMODE])
58 #define PLL_FRAC(p) ((p)->offset + (p)->regs[PLL_OFF_FRAC])
60 const u8 clk_alpha_pll_regs[][PLL_OFF_MAX_REGS] = {
61 [CLK_ALPHA_PLL_TYPE_DEFAULT] = {
62 [PLL_OFF_L_VAL] = 0x04,
63 [PLL_OFF_ALPHA_VAL] = 0x08,
64 [PLL_OFF_ALPHA_VAL_U] = 0x0c,
65 [PLL_OFF_USER_CTL] = 0x10,
66 [PLL_OFF_USER_CTL_U] = 0x14,
67 [PLL_OFF_CONFIG_CTL] = 0x18,
68 [PLL_OFF_TEST_CTL] = 0x1c,
69 [PLL_OFF_TEST_CTL_U] = 0x20,
70 [PLL_OFF_STATUS] = 0x24,
72 [CLK_ALPHA_PLL_TYPE_HUAYRA] = {
73 [PLL_OFF_L_VAL] = 0x04,
74 [PLL_OFF_ALPHA_VAL] = 0x08,
75 [PLL_OFF_USER_CTL] = 0x10,
76 [PLL_OFF_CONFIG_CTL] = 0x14,
77 [PLL_OFF_CONFIG_CTL_U] = 0x18,
78 [PLL_OFF_TEST_CTL] = 0x1c,
79 [PLL_OFF_TEST_CTL_U] = 0x20,
80 [PLL_OFF_STATUS] = 0x24,
82 [CLK_ALPHA_PLL_TYPE_BRAMMO] = {
83 [PLL_OFF_L_VAL] = 0x04,
84 [PLL_OFF_ALPHA_VAL] = 0x08,
85 [PLL_OFF_ALPHA_VAL_U] = 0x0c,
86 [PLL_OFF_USER_CTL] = 0x10,
87 [PLL_OFF_CONFIG_CTL] = 0x18,
88 [PLL_OFF_TEST_CTL] = 0x1c,
89 [PLL_OFF_STATUS] = 0x24,
91 [CLK_ALPHA_PLL_TYPE_FABIA] = {
92 [PLL_OFF_L_VAL] = 0x04,
93 [PLL_OFF_USER_CTL] = 0x0c,
94 [PLL_OFF_USER_CTL_U] = 0x10,
95 [PLL_OFF_CONFIG_CTL] = 0x14,
96 [PLL_OFF_CONFIG_CTL_U] = 0x18,
97 [PLL_OFF_TEST_CTL] = 0x1c,
98 [PLL_OFF_TEST_CTL_U] = 0x20,
99 [PLL_OFF_STATUS] = 0x24,
100 [PLL_OFF_OPMODE] = 0x2c,
101 [PLL_OFF_FRAC] = 0x38,
103 [CLK_ALPHA_PLL_TYPE_TRION] = {
104 [PLL_OFF_L_VAL] = 0x04,
105 [PLL_OFF_CAL_L_VAL] = 0x08,
106 [PLL_OFF_USER_CTL] = 0x0c,
107 [PLL_OFF_USER_CTL_U] = 0x10,
108 [PLL_OFF_USER_CTL_U1] = 0x14,
109 [PLL_OFF_CONFIG_CTL] = 0x18,
110 [PLL_OFF_CONFIG_CTL_U] = 0x1c,
111 [PLL_OFF_CONFIG_CTL_U1] = 0x20,
112 [PLL_OFF_TEST_CTL] = 0x24,
113 [PLL_OFF_TEST_CTL_U] = 0x28,
114 [PLL_OFF_TEST_CTL_U1] = 0x2c,
115 [PLL_OFF_STATUS] = 0x30,
116 [PLL_OFF_OPMODE] = 0x38,
117 [PLL_OFF_ALPHA_VAL] = 0x40,
119 [CLK_ALPHA_PLL_TYPE_AGERA] = {
120 [PLL_OFF_L_VAL] = 0x04,
121 [PLL_OFF_ALPHA_VAL] = 0x08,
122 [PLL_OFF_USER_CTL] = 0x0c,
123 [PLL_OFF_CONFIG_CTL] = 0x10,
124 [PLL_OFF_CONFIG_CTL_U] = 0x14,
125 [PLL_OFF_TEST_CTL] = 0x18,
126 [PLL_OFF_TEST_CTL_U] = 0x1c,
127 [PLL_OFF_STATUS] = 0x2c,
130 EXPORT_SYMBOL_GPL(clk_alpha_pll_regs);
133 * Even though 40 bits are present, use only 32 for ease of calculation.
135 #define ALPHA_REG_BITWIDTH 40
136 #define ALPHA_REG_16BIT_WIDTH 16
137 #define ALPHA_BITWIDTH 32U
138 #define ALPHA_SHIFT(w) min(w, ALPHA_BITWIDTH)
140 #define PLL_HUAYRA_M_WIDTH 8
141 #define PLL_HUAYRA_M_SHIFT 8
142 #define PLL_HUAYRA_M_MASK 0xff
143 #define PLL_HUAYRA_N_SHIFT 0
144 #define PLL_HUAYRA_N_MASK 0xff
145 #define PLL_HUAYRA_ALPHA_WIDTH 16
147 #define PLL_STANDBY 0x0
148 #define PLL_RUN 0x1
149 #define PLL_OUT_MASK 0x7
150 #define PLL_RATE_MARGIN 500
152 /* TRION PLL specific settings and offsets */
153 #define TRION_PLL_CAL_VAL 0x44
154 #define TRION_PCAL_DONE BIT(26)
156 /* LUCID PLL specific settings and offsets */
157 #define LUCID_PCAL_DONE BIT(27)
159 #define pll_alpha_width(p) \
160 ((PLL_ALPHA_VAL_U(p) - PLL_ALPHA_VAL(p) == 4) ? \
161 ALPHA_REG_BITWIDTH : ALPHA_REG_16BIT_WIDTH)
163 #define pll_has_64bit_config(p) ((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
165 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
166 struct clk_alpha_pll, clkr)
168 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
169 struct clk_alpha_pll_postdiv, clkr)
171 static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
172 const char *action)
174 u32 val;
175 int count;
176 int ret;
177 const char *name = clk_hw_get_name(&pll->clkr.hw);
179 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
180 if (ret)
181 return ret;
183 for (count = 100; count > 0; count--) {
184 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
185 if (ret)
186 return ret;
187 if (inverse && !(val & mask))
188 return 0;
189 else if ((val & mask) == mask)
190 return 0;
192 udelay(1);
195 WARN(1, "%s failed to %s!\n", name, action);
196 return -ETIMEDOUT;
199 #define wait_for_pll_enable_active(pll) \
200 wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
202 #define wait_for_pll_enable_lock(pll) \
203 wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
205 #define wait_for_pll_disable(pll) \
206 wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
208 #define wait_for_pll_offline(pll) \
209 wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
211 #define wait_for_pll_update(pll) \
212 wait_for_pll(pll, PLL_UPDATE, 1, "update")
214 #define wait_for_pll_update_ack_set(pll) \
215 wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
217 #define wait_for_pll_update_ack_clear(pll) \
218 wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
220 static void clk_alpha_pll_write_config(struct regmap *regmap, unsigned int reg,
221 unsigned int val)
223 if (val)
224 regmap_write(regmap, reg, val);
227 void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
228 const struct alpha_pll_config *config)
230 u32 val, mask;
232 regmap_write(regmap, PLL_L_VAL(pll), config->l);
233 regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
234 regmap_write(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
236 if (pll_has_64bit_config(pll))
237 regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
238 config->config_ctl_hi_val);
240 if (pll_alpha_width(pll) > 32)
241 regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
243 val = config->main_output_mask;
244 val |= config->aux_output_mask;
245 val |= config->aux2_output_mask;
246 val |= config->early_output_mask;
247 val |= config->pre_div_val;
248 val |= config->post_div_val;
249 val |= config->vco_val;
250 val |= config->alpha_en_mask;
251 val |= config->alpha_mode_mask;
253 mask = config->main_output_mask;
254 mask |= config->aux_output_mask;
255 mask |= config->aux2_output_mask;
256 mask |= config->early_output_mask;
257 mask |= config->pre_div_mask;
258 mask |= config->post_div_mask;
259 mask |= config->vco_mask;
261 regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
263 if (pll->flags & SUPPORTS_FSM_MODE)
264 qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
266 EXPORT_SYMBOL_GPL(clk_alpha_pll_configure);
268 static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
270 int ret;
271 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
272 u32 val;
274 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
275 if (ret)
276 return ret;
278 val |= PLL_FSM_ENA;
280 if (pll->flags & SUPPORTS_OFFLINE_REQ)
281 val &= ~PLL_OFFLINE_REQ;
283 ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
284 if (ret)
285 return ret;
287 /* Make sure enable request goes through before waiting for update */
288 mb();
290 return wait_for_pll_enable_active(pll);
293 static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
295 int ret;
296 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
297 u32 val;
299 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
300 if (ret)
301 return;
303 if (pll->flags & SUPPORTS_OFFLINE_REQ) {
304 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
305 PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
306 if (ret)
307 return;
309 ret = wait_for_pll_offline(pll);
310 if (ret)
311 return;
314 /* Disable hwfsm */
315 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
316 PLL_FSM_ENA, 0);
317 if (ret)
318 return;
320 wait_for_pll_disable(pll);
323 static int pll_is_enabled(struct clk_hw *hw, u32 mask)
325 int ret;
326 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
327 u32 val;
329 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
330 if (ret)
331 return ret;
333 return !!(val & mask);
336 static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
338 return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
341 static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
343 return pll_is_enabled(hw, PLL_LOCK_DET);
346 static int clk_alpha_pll_enable(struct clk_hw *hw)
348 int ret;
349 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
350 u32 val, mask;
352 mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
353 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
354 if (ret)
355 return ret;
357 /* If in FSM mode, just vote for it */
358 if (val & PLL_VOTE_FSM_ENA) {
359 ret = clk_enable_regmap(hw);
360 if (ret)
361 return ret;
362 return wait_for_pll_enable_active(pll);
365 /* Skip if already enabled */
366 if ((val & mask) == mask)
367 return 0;
369 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
370 PLL_BYPASSNL, PLL_BYPASSNL);
371 if (ret)
372 return ret;
375 * H/W requires a 5us delay between disabling the bypass and
376 * de-asserting the reset.
378 mb();
379 udelay(5);
381 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
382 PLL_RESET_N, PLL_RESET_N);
383 if (ret)
384 return ret;
386 ret = wait_for_pll_enable_lock(pll);
387 if (ret)
388 return ret;
390 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
391 PLL_OUTCTRL, PLL_OUTCTRL);
393 /* Ensure that the write above goes through before returning. */
394 mb();
395 return ret;
398 static void clk_alpha_pll_disable(struct clk_hw *hw)
400 int ret;
401 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
402 u32 val, mask;
404 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
405 if (ret)
406 return;
408 /* If in FSM mode, just unvote it */
409 if (val & PLL_VOTE_FSM_ENA) {
410 clk_disable_regmap(hw);
411 return;
414 mask = PLL_OUTCTRL;
415 regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
417 /* Delay of 2 output clock ticks required until output is disabled */
418 mb();
419 udelay(1);
421 mask = PLL_RESET_N | PLL_BYPASSNL;
422 regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
425 static unsigned long
426 alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
428 return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
431 static unsigned long
432 alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
433 u32 alpha_width)
435 u64 remainder;
436 u64 quotient;
438 quotient = rate;
439 remainder = do_div(quotient, prate);
440 *l = quotient;
442 if (!remainder) {
443 *a = 0;
444 return rate;
447 /* Upper ALPHA_BITWIDTH bits of Alpha */
448 quotient = remainder << ALPHA_SHIFT(alpha_width);
450 remainder = do_div(quotient, prate);
452 if (remainder)
453 quotient++;
455 *a = quotient;
456 return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
459 static const struct pll_vco *
460 alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
462 const struct pll_vco *v = pll->vco_table;
463 const struct pll_vco *end = v + pll->num_vco;
465 for (; v < end; v++)
466 if (rate >= v->min_freq && rate <= v->max_freq)
467 return v;
469 return NULL;
472 static unsigned long
473 clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
475 u32 l, low, high, ctl;
476 u64 a = 0, prate = parent_rate;
477 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
478 u32 alpha_width = pll_alpha_width(pll);
480 regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
482 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
483 if (ctl & PLL_ALPHA_EN) {
484 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &low);
485 if (alpha_width > 32) {
486 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
487 &high);
488 a = (u64)high << 32 | low;
489 } else {
490 a = low & GENMASK(alpha_width - 1, 0);
493 if (alpha_width > ALPHA_BITWIDTH)
494 a >>= alpha_width - ALPHA_BITWIDTH;
497 return alpha_pll_calc_rate(prate, l, a, alpha_width);
501 static int __clk_alpha_pll_update_latch(struct clk_alpha_pll *pll)
503 int ret;
504 u32 mode;
506 regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
508 /* Latch the input to the PLL */
509 regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
510 PLL_UPDATE);
512 /* Wait for 2 reference cycle before checking ACK bit */
513 udelay(1);
516 * PLL will latch the new L, Alpha and freq control word.
517 * PLL will respond by raising PLL_ACK_LATCH output when new programming
518 * has been latched in and PLL is being updated. When
519 * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
520 * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
522 if (mode & PLL_UPDATE_BYPASS) {
523 ret = wait_for_pll_update_ack_set(pll);
524 if (ret)
525 return ret;
527 regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE, 0);
528 } else {
529 ret = wait_for_pll_update(pll);
530 if (ret)
531 return ret;
534 ret = wait_for_pll_update_ack_clear(pll);
535 if (ret)
536 return ret;
538 /* Wait for PLL output to stabilize */
539 udelay(10);
541 return 0;
544 static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
545 int (*is_enabled)(struct clk_hw *))
547 if (!is_enabled(&pll->clkr.hw) ||
548 !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
549 return 0;
551 return __clk_alpha_pll_update_latch(pll);
554 static int __clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
555 unsigned long prate,
556 int (*is_enabled)(struct clk_hw *))
558 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
559 const struct pll_vco *vco;
560 u32 l, alpha_width = pll_alpha_width(pll);
561 u64 a;
563 rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
564 vco = alpha_pll_find_vco(pll, rate);
565 if (pll->vco_table && !vco) {
566 pr_err("%s: alpha pll not in a valid vco range\n",
567 clk_hw_get_name(hw));
568 return -EINVAL;
571 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
573 if (alpha_width > ALPHA_BITWIDTH)
574 a <<= alpha_width - ALPHA_BITWIDTH;
576 if (alpha_width > 32)
577 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
579 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
581 if (vco) {
582 regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
583 PLL_VCO_MASK << PLL_VCO_SHIFT,
584 vco->val << PLL_VCO_SHIFT);
587 regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
588 PLL_ALPHA_EN, PLL_ALPHA_EN);
590 return clk_alpha_pll_update_latch(pll, is_enabled);
593 static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
594 unsigned long prate)
596 return __clk_alpha_pll_set_rate(hw, rate, prate,
597 clk_alpha_pll_is_enabled);
600 static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
601 unsigned long prate)
603 return __clk_alpha_pll_set_rate(hw, rate, prate,
604 clk_alpha_pll_hwfsm_is_enabled);
607 static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
608 unsigned long *prate)
610 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
611 u32 l, alpha_width = pll_alpha_width(pll);
612 u64 a;
613 unsigned long min_freq, max_freq;
615 rate = alpha_pll_round_rate(rate, *prate, &l, &a, alpha_width);
616 if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
617 return rate;
619 min_freq = pll->vco_table[0].min_freq;
620 max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
622 return clamp(rate, min_freq, max_freq);
625 static unsigned long
626 alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
629 * a contains 16 bit alpha_val in two’s complement number in the range
630 * of [-0.5, 0.5).
632 if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
633 l -= 1;
635 return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
638 static unsigned long
639 alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
640 u32 *l, u32 *a)
642 u64 remainder;
643 u64 quotient;
645 quotient = rate;
646 remainder = do_div(quotient, prate);
647 *l = quotient;
649 if (!remainder) {
650 *a = 0;
651 return rate;
654 quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
655 remainder = do_div(quotient, prate);
657 if (remainder)
658 quotient++;
661 * alpha_val should be in two’s complement number in the range
662 * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
663 * since alpha value will be subtracted in this case.
665 if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
666 *l += 1;
668 *a = quotient;
669 return alpha_huayra_pll_calc_rate(prate, *l, *a);
672 static unsigned long
673 alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
675 u64 rate = parent_rate, tmp;
676 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
677 u32 l, alpha = 0, ctl, alpha_m, alpha_n;
679 regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
680 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
682 if (ctl & PLL_ALPHA_EN) {
683 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
685 * Depending upon alpha_mode, it can be treated as M/N value or
686 * as a two’s complement number. When alpha_mode=1,
687 * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
689 * Fout=FIN*(L+(M/N))
691 * M is a signed number (-128 to 127) and N is unsigned
692 * (0 to 255). M/N has to be within +/-0.5.
694 * When alpha_mode=0, it is a two’s complement number in the
695 * range [-0.5, 0.5).
697 * Fout=FIN*(L+(alpha_val)/2^16)
699 * where alpha_val is two’s complement number.
701 if (!(ctl & PLL_ALPHA_MODE))
702 return alpha_huayra_pll_calc_rate(rate, l, alpha);
704 alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
705 alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
707 rate *= l;
708 tmp = parent_rate;
709 if (alpha_m >= BIT(PLL_HUAYRA_M_WIDTH - 1)) {
710 alpha_m = BIT(PLL_HUAYRA_M_WIDTH) - alpha_m;
711 tmp *= alpha_m;
712 do_div(tmp, alpha_n);
713 rate -= tmp;
714 } else {
715 tmp *= alpha_m;
716 do_div(tmp, alpha_n);
717 rate += tmp;
720 return rate;
723 return alpha_huayra_pll_calc_rate(rate, l, alpha);
726 static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
727 unsigned long prate)
729 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
730 u32 l, a, ctl, cur_alpha = 0;
732 rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
734 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
736 if (ctl & PLL_ALPHA_EN)
737 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
740 * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
741 * without having to go through the power on sequence.
743 if (clk_alpha_pll_is_enabled(hw)) {
744 if (cur_alpha != a) {
745 pr_err("%s: clock needs to be gated\n",
746 clk_hw_get_name(hw));
747 return -EBUSY;
750 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
751 /* Ensure that the write above goes to detect L val change. */
752 mb();
753 return wait_for_pll_enable_lock(pll);
756 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
757 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
759 if (a == 0)
760 regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
761 PLL_ALPHA_EN, 0x0);
762 else
763 regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
764 PLL_ALPHA_EN | PLL_ALPHA_MODE, PLL_ALPHA_EN);
766 return 0;
769 static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
770 unsigned long *prate)
772 u32 l, a;
774 return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
777 static int trion_pll_is_enabled(struct clk_alpha_pll *pll,
778 struct regmap *regmap)
780 u32 mode_regval, opmode_regval;
781 int ret;
783 ret = regmap_read(regmap, PLL_MODE(pll), &mode_regval);
784 ret |= regmap_read(regmap, PLL_OPMODE(pll), &opmode_regval);
785 if (ret)
786 return 0;
788 return ((opmode_regval & PLL_RUN) && (mode_regval & PLL_OUTCTRL));
791 static int clk_trion_pll_is_enabled(struct clk_hw *hw)
793 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
795 return trion_pll_is_enabled(pll, pll->clkr.regmap);
798 static int clk_trion_pll_enable(struct clk_hw *hw)
800 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
801 struct regmap *regmap = pll->clkr.regmap;
802 u32 val;
803 int ret;
805 ret = regmap_read(regmap, PLL_MODE(pll), &val);
806 if (ret)
807 return ret;
809 /* If in FSM mode, just vote for it */
810 if (val & PLL_VOTE_FSM_ENA) {
811 ret = clk_enable_regmap(hw);
812 if (ret)
813 return ret;
814 return wait_for_pll_enable_active(pll);
817 /* Set operation mode to RUN */
818 regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
820 ret = wait_for_pll_enable_lock(pll);
821 if (ret)
822 return ret;
824 /* Enable the PLL outputs */
825 ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
826 PLL_OUT_MASK, PLL_OUT_MASK);
827 if (ret)
828 return ret;
830 /* Enable the global PLL outputs */
831 return regmap_update_bits(regmap, PLL_MODE(pll),
832 PLL_OUTCTRL, PLL_OUTCTRL);
835 static void clk_trion_pll_disable(struct clk_hw *hw)
837 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
838 struct regmap *regmap = pll->clkr.regmap;
839 u32 val;
840 int ret;
842 ret = regmap_read(regmap, PLL_MODE(pll), &val);
843 if (ret)
844 return;
846 /* If in FSM mode, just unvote it */
847 if (val & PLL_VOTE_FSM_ENA) {
848 clk_disable_regmap(hw);
849 return;
852 /* Disable the global PLL output */
853 ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
854 if (ret)
855 return;
857 /* Disable the PLL outputs */
858 ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
859 PLL_OUT_MASK, 0);
860 if (ret)
861 return;
863 /* Place the PLL mode in STANDBY */
864 regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
865 regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
868 static unsigned long
869 clk_trion_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
871 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
872 u32 l, frac, alpha_width = pll_alpha_width(pll);
874 regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
875 regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &frac);
877 return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
880 const struct clk_ops clk_alpha_pll_fixed_ops = {
881 .enable = clk_alpha_pll_enable,
882 .disable = clk_alpha_pll_disable,
883 .is_enabled = clk_alpha_pll_is_enabled,
884 .recalc_rate = clk_alpha_pll_recalc_rate,
886 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_ops);
888 const struct clk_ops clk_alpha_pll_ops = {
889 .enable = clk_alpha_pll_enable,
890 .disable = clk_alpha_pll_disable,
891 .is_enabled = clk_alpha_pll_is_enabled,
892 .recalc_rate = clk_alpha_pll_recalc_rate,
893 .round_rate = clk_alpha_pll_round_rate,
894 .set_rate = clk_alpha_pll_set_rate,
896 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
898 const struct clk_ops clk_alpha_pll_huayra_ops = {
899 .enable = clk_alpha_pll_enable,
900 .disable = clk_alpha_pll_disable,
901 .is_enabled = clk_alpha_pll_is_enabled,
902 .recalc_rate = alpha_pll_huayra_recalc_rate,
903 .round_rate = alpha_pll_huayra_round_rate,
904 .set_rate = alpha_pll_huayra_set_rate,
906 EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
908 const struct clk_ops clk_alpha_pll_hwfsm_ops = {
909 .enable = clk_alpha_pll_hwfsm_enable,
910 .disable = clk_alpha_pll_hwfsm_disable,
911 .is_enabled = clk_alpha_pll_hwfsm_is_enabled,
912 .recalc_rate = clk_alpha_pll_recalc_rate,
913 .round_rate = clk_alpha_pll_round_rate,
914 .set_rate = clk_alpha_pll_hwfsm_set_rate,
916 EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
918 const struct clk_ops clk_alpha_pll_fixed_trion_ops = {
919 .enable = clk_trion_pll_enable,
920 .disable = clk_trion_pll_disable,
921 .is_enabled = clk_trion_pll_is_enabled,
922 .recalc_rate = clk_trion_pll_recalc_rate,
923 .round_rate = clk_alpha_pll_round_rate,
925 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_trion_ops);
927 static unsigned long
928 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
930 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
931 u32 ctl;
933 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
935 ctl >>= PLL_POST_DIV_SHIFT;
936 ctl &= PLL_POST_DIV_MASK(pll);
938 return parent_rate >> fls(ctl);
941 static const struct clk_div_table clk_alpha_div_table[] = {
942 { 0x0, 1 },
943 { 0x1, 2 },
944 { 0x3, 4 },
945 { 0x7, 8 },
946 { 0xf, 16 },
950 static const struct clk_div_table clk_alpha_2bit_div_table[] = {
951 { 0x0, 1 },
952 { 0x1, 2 },
953 { 0x3, 4 },
957 static long
958 clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
959 unsigned long *prate)
961 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
962 const struct clk_div_table *table;
964 if (pll->width == 2)
965 table = clk_alpha_2bit_div_table;
966 else
967 table = clk_alpha_div_table;
969 return divider_round_rate(hw, rate, prate, table,
970 pll->width, CLK_DIVIDER_POWER_OF_TWO);
973 static long
974 clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
975 unsigned long *prate)
977 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
978 u32 ctl, div;
980 regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
982 ctl >>= PLL_POST_DIV_SHIFT;
983 ctl &= BIT(pll->width) - 1;
984 div = 1 << fls(ctl);
986 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
987 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
989 return DIV_ROUND_UP_ULL((u64)*prate, div);
992 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
993 unsigned long parent_rate)
995 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
996 int div;
998 /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
999 div = DIV_ROUND_UP_ULL(parent_rate, rate) - 1;
1001 return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
1002 PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
1003 div << PLL_POST_DIV_SHIFT);
1006 const struct clk_ops clk_alpha_pll_postdiv_ops = {
1007 .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
1008 .round_rate = clk_alpha_pll_postdiv_round_rate,
1009 .set_rate = clk_alpha_pll_postdiv_set_rate,
1011 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
1013 const struct clk_ops clk_alpha_pll_postdiv_ro_ops = {
1014 .round_rate = clk_alpha_pll_postdiv_round_ro_rate,
1015 .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
1017 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);
1019 void clk_fabia_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1020 const struct alpha_pll_config *config)
1022 u32 val, mask;
1024 clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
1025 clk_alpha_pll_write_config(regmap, PLL_FRAC(pll), config->alpha);
1026 clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
1027 config->config_ctl_val);
1028 clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
1029 config->config_ctl_hi_val);
1030 clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
1031 config->user_ctl_val);
1032 clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll),
1033 config->user_ctl_hi_val);
1034 clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
1035 config->test_ctl_val);
1036 clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
1037 config->test_ctl_hi_val);
1039 if (config->post_div_mask) {
1040 mask = config->post_div_mask;
1041 val = config->post_div_val;
1042 regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
1045 regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
1046 PLL_UPDATE_BYPASS);
1048 regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1050 EXPORT_SYMBOL_GPL(clk_fabia_pll_configure);
1052 static int alpha_pll_fabia_enable(struct clk_hw *hw)
1054 int ret;
1055 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1056 u32 val, opmode_val;
1057 struct regmap *regmap = pll->clkr.regmap;
1059 ret = regmap_read(regmap, PLL_MODE(pll), &val);
1060 if (ret)
1061 return ret;
1063 /* If in FSM mode, just vote for it */
1064 if (val & PLL_VOTE_FSM_ENA) {
1065 ret = clk_enable_regmap(hw);
1066 if (ret)
1067 return ret;
1068 return wait_for_pll_enable_active(pll);
1071 ret = regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
1072 if (ret)
1073 return ret;
1075 /* Skip If PLL is already running */
1076 if ((opmode_val & PLL_RUN) && (val & PLL_OUTCTRL))
1077 return 0;
1079 ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1080 if (ret)
1081 return ret;
1083 ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1084 if (ret)
1085 return ret;
1087 ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N,
1088 PLL_RESET_N);
1089 if (ret)
1090 return ret;
1092 ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
1093 if (ret)
1094 return ret;
1096 ret = wait_for_pll_enable_lock(pll);
1097 if (ret)
1098 return ret;
1100 ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
1101 PLL_OUT_MASK, PLL_OUT_MASK);
1102 if (ret)
1103 return ret;
1105 return regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL,
1106 PLL_OUTCTRL);
1109 static void alpha_pll_fabia_disable(struct clk_hw *hw)
1111 int ret;
1112 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1113 u32 val;
1114 struct regmap *regmap = pll->clkr.regmap;
1116 ret = regmap_read(regmap, PLL_MODE(pll), &val);
1117 if (ret)
1118 return;
1120 /* If in FSM mode, just unvote it */
1121 if (val & PLL_FSM_ENA) {
1122 clk_disable_regmap(hw);
1123 return;
1126 ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1127 if (ret)
1128 return;
1130 /* Disable main outputs */
1131 ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
1132 if (ret)
1133 return;
1135 /* Place the PLL in STANDBY */
1136 regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1139 static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw *hw,
1140 unsigned long parent_rate)
1142 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1143 u32 l, frac, alpha_width = pll_alpha_width(pll);
1145 regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
1146 regmap_read(pll->clkr.regmap, PLL_FRAC(pll), &frac);
1148 return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
1152 * Due to limited number of bits for fractional rate programming, the
1153 * rounded up rate could be marginally higher than the requested rate.
1155 static int alpha_pll_check_rate_margin(struct clk_hw *hw,
1156 unsigned long rrate, unsigned long rate)
1158 unsigned long rate_margin = rate + PLL_RATE_MARGIN;
1160 if (rrate > rate_margin || rrate < rate) {
1161 pr_err("%s: Rounded rate %lu not within range [%lu, %lu)\n",
1162 clk_hw_get_name(hw), rrate, rate, rate_margin);
1163 return -EINVAL;
1166 return 0;
1169 static int alpha_pll_fabia_set_rate(struct clk_hw *hw, unsigned long rate,
1170 unsigned long prate)
1172 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1173 u32 l, alpha_width = pll_alpha_width(pll);
1174 unsigned long rrate;
1175 int ret;
1176 u64 a;
1178 rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1180 ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1181 if (ret < 0)
1182 return ret;
1184 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1185 regmap_write(pll->clkr.regmap, PLL_FRAC(pll), a);
1187 return __clk_alpha_pll_update_latch(pll);
1190 static int alpha_pll_fabia_prepare(struct clk_hw *hw)
1192 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1193 const struct pll_vco *vco;
1194 struct clk_hw *parent_hw;
1195 unsigned long cal_freq, rrate;
1196 u32 cal_l, val, alpha_width = pll_alpha_width(pll);
1197 const char *name = clk_hw_get_name(hw);
1198 u64 a;
1199 int ret;
1201 /* Check if calibration needs to be done i.e. PLL is in reset */
1202 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1203 if (ret)
1204 return ret;
1206 /* Return early if calibration is not needed. */
1207 if (val & PLL_RESET_N)
1208 return 0;
1210 vco = alpha_pll_find_vco(pll, clk_hw_get_rate(hw));
1211 if (!vco) {
1212 pr_err("%s: alpha pll not in a valid vco range\n", name);
1213 return -EINVAL;
1216 cal_freq = DIV_ROUND_CLOSEST((pll->vco_table[0].min_freq +
1217 pll->vco_table[0].max_freq) * 54, 100);
1219 parent_hw = clk_hw_get_parent(hw);
1220 if (!parent_hw)
1221 return -EINVAL;
1223 rrate = alpha_pll_round_rate(cal_freq, clk_hw_get_rate(parent_hw),
1224 &cal_l, &a, alpha_width);
1226 ret = alpha_pll_check_rate_margin(hw, rrate, cal_freq);
1227 if (ret < 0)
1228 return ret;
1230 /* Setup PLL for calibration frequency */
1231 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), cal_l);
1233 /* Bringup the PLL at calibration frequency */
1234 ret = clk_alpha_pll_enable(hw);
1235 if (ret) {
1236 pr_err("%s: alpha pll calibration failed\n", name);
1237 return ret;
1240 clk_alpha_pll_disable(hw);
1242 return 0;
1245 const struct clk_ops clk_alpha_pll_fabia_ops = {
1246 .prepare = alpha_pll_fabia_prepare,
1247 .enable = alpha_pll_fabia_enable,
1248 .disable = alpha_pll_fabia_disable,
1249 .is_enabled = clk_alpha_pll_is_enabled,
1250 .set_rate = alpha_pll_fabia_set_rate,
1251 .recalc_rate = alpha_pll_fabia_recalc_rate,
1252 .round_rate = clk_alpha_pll_round_rate,
1254 EXPORT_SYMBOL_GPL(clk_alpha_pll_fabia_ops);
1256 const struct clk_ops clk_alpha_pll_fixed_fabia_ops = {
1257 .enable = alpha_pll_fabia_enable,
1258 .disable = alpha_pll_fabia_disable,
1259 .is_enabled = clk_alpha_pll_is_enabled,
1260 .recalc_rate = alpha_pll_fabia_recalc_rate,
1261 .round_rate = clk_alpha_pll_round_rate,
1263 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_fabia_ops);
1265 static unsigned long clk_alpha_pll_postdiv_fabia_recalc_rate(struct clk_hw *hw,
1266 unsigned long parent_rate)
1268 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1269 u32 i, div = 1, val;
1270 int ret;
1272 ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
1273 if (ret)
1274 return ret;
1276 val >>= pll->post_div_shift;
1277 val &= BIT(pll->width) - 1;
1279 for (i = 0; i < pll->num_post_div; i++) {
1280 if (pll->post_div_table[i].val == val) {
1281 div = pll->post_div_table[i].div;
1282 break;
1286 return (parent_rate / div);
1289 static unsigned long
1290 clk_trion_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
1292 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1293 struct regmap *regmap = pll->clkr.regmap;
1294 u32 i, div = 1, val;
1296 regmap_read(regmap, PLL_USER_CTL(pll), &val);
1298 val >>= pll->post_div_shift;
1299 val &= PLL_POST_DIV_MASK(pll);
1301 for (i = 0; i < pll->num_post_div; i++) {
1302 if (pll->post_div_table[i].val == val) {
1303 div = pll->post_div_table[i].div;
1304 break;
1308 return (parent_rate / div);
1311 static long
1312 clk_trion_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
1313 unsigned long *prate)
1315 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1317 return divider_round_rate(hw, rate, prate, pll->post_div_table,
1318 pll->width, CLK_DIVIDER_ROUND_CLOSEST);
1321 static int
1322 clk_trion_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
1323 unsigned long parent_rate)
1325 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1326 struct regmap *regmap = pll->clkr.regmap;
1327 int i, val = 0, div;
1329 div = DIV_ROUND_UP_ULL(parent_rate, rate);
1330 for (i = 0; i < pll->num_post_div; i++) {
1331 if (pll->post_div_table[i].div == div) {
1332 val = pll->post_div_table[i].val;
1333 break;
1337 return regmap_update_bits(regmap, PLL_USER_CTL(pll),
1338 PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
1339 val << PLL_POST_DIV_SHIFT);
1342 const struct clk_ops clk_alpha_pll_postdiv_trion_ops = {
1343 .recalc_rate = clk_trion_pll_postdiv_recalc_rate,
1344 .round_rate = clk_trion_pll_postdiv_round_rate,
1345 .set_rate = clk_trion_pll_postdiv_set_rate,
1347 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_trion_ops);
1349 static long clk_alpha_pll_postdiv_fabia_round_rate(struct clk_hw *hw,
1350 unsigned long rate, unsigned long *prate)
1352 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1354 return divider_round_rate(hw, rate, prate, pll->post_div_table,
1355 pll->width, CLK_DIVIDER_ROUND_CLOSEST);
1358 static int clk_alpha_pll_postdiv_fabia_set_rate(struct clk_hw *hw,
1359 unsigned long rate, unsigned long parent_rate)
1361 struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1362 int i, val = 0, div, ret;
1365 * If the PLL is in FSM mode, then treat set_rate callback as a
1366 * no-operation.
1368 ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1369 if (ret)
1370 return ret;
1372 if (val & PLL_VOTE_FSM_ENA)
1373 return 0;
1375 div = DIV_ROUND_UP_ULL(parent_rate, rate);
1376 for (i = 0; i < pll->num_post_div; i++) {
1377 if (pll->post_div_table[i].div == div) {
1378 val = pll->post_div_table[i].val;
1379 break;
1383 return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
1384 (BIT(pll->width) - 1) << pll->post_div_shift,
1385 val << pll->post_div_shift);
1388 const struct clk_ops clk_alpha_pll_postdiv_fabia_ops = {
1389 .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
1390 .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
1391 .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
1393 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_fabia_ops);
1396 * clk_lucid_pll_configure - configure the lucid pll
1398 * @pll: clk alpha pll
1399 * @regmap: register map
1400 * @config: configuration to apply for pll
1402 void clk_trion_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1403 const struct alpha_pll_config *config)
1405 clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
1406 regmap_write(regmap, PLL_CAL_L_VAL(pll), TRION_PLL_CAL_VAL);
1407 clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
1408 clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
1409 config->config_ctl_val);
1410 clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
1411 config->config_ctl_hi_val);
1412 clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll),
1413 config->config_ctl_hi1_val);
1414 clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
1415 config->user_ctl_val);
1416 clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll),
1417 config->user_ctl_hi_val);
1418 clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U1(pll),
1419 config->user_ctl_hi1_val);
1420 clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
1421 config->test_ctl_val);
1422 clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
1423 config->test_ctl_hi_val);
1424 clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll),
1425 config->test_ctl_hi1_val);
1427 regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
1428 PLL_UPDATE_BYPASS);
1430 /* Disable PLL output */
1431 regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1433 /* Set operation mode to OFF */
1434 regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1436 /* Place the PLL in STANDBY mode */
1437 regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1439 EXPORT_SYMBOL_GPL(clk_trion_pll_configure);
1442 * The TRION PLL requires a power-on self-calibration which happens when the
1443 * PLL comes out of reset. Calibrate in case it is not completed.
1445 static int __alpha_pll_trion_prepare(struct clk_hw *hw, u32 pcal_done)
1447 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1448 u32 regval;
1449 int ret;
1451 /* Return early if calibration is not needed. */
1452 regmap_read(pll->clkr.regmap, PLL_STATUS(pll), &regval);
1453 if (regval & pcal_done)
1454 return 0;
1456 /* On/off to calibrate */
1457 ret = clk_trion_pll_enable(hw);
1458 if (!ret)
1459 clk_trion_pll_disable(hw);
1461 return ret;
1464 static int alpha_pll_trion_prepare(struct clk_hw *hw)
1466 return __alpha_pll_trion_prepare(hw, TRION_PCAL_DONE);
1469 static int alpha_pll_lucid_prepare(struct clk_hw *hw)
1471 return __alpha_pll_trion_prepare(hw, LUCID_PCAL_DONE);
1474 static int alpha_pll_trion_set_rate(struct clk_hw *hw, unsigned long rate,
1475 unsigned long prate)
1477 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1478 unsigned long rrate;
1479 u32 regval, l, alpha_width = pll_alpha_width(pll);
1480 u64 a;
1481 int ret;
1483 rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1485 ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1486 if (ret < 0)
1487 return ret;
1489 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1490 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
1492 /* Latch the PLL input */
1493 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
1494 PLL_UPDATE, PLL_UPDATE);
1495 if (ret)
1496 return ret;
1498 /* Wait for 2 reference cycles before checking the ACK bit. */
1499 udelay(1);
1500 regmap_read(pll->clkr.regmap, PLL_MODE(pll), &regval);
1501 if (!(regval & ALPHA_PLL_ACK_LATCH)) {
1502 pr_err("Lucid PLL latch failed. Output may be unstable!\n");
1503 return -EINVAL;
1506 /* Return the latch input to 0 */
1507 ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
1508 PLL_UPDATE, 0);
1509 if (ret)
1510 return ret;
1512 if (clk_hw_is_enabled(hw)) {
1513 ret = wait_for_pll_enable_lock(pll);
1514 if (ret)
1515 return ret;
1518 /* Wait for PLL output to stabilize */
1519 udelay(100);
1520 return 0;
1523 const struct clk_ops clk_alpha_pll_trion_ops = {
1524 .prepare = alpha_pll_trion_prepare,
1525 .enable = clk_trion_pll_enable,
1526 .disable = clk_trion_pll_disable,
1527 .is_enabled = clk_trion_pll_is_enabled,
1528 .recalc_rate = clk_trion_pll_recalc_rate,
1529 .round_rate = clk_alpha_pll_round_rate,
1530 .set_rate = alpha_pll_trion_set_rate,
1532 EXPORT_SYMBOL_GPL(clk_alpha_pll_trion_ops);
1534 const struct clk_ops clk_alpha_pll_lucid_ops = {
1535 .prepare = alpha_pll_lucid_prepare,
1536 .enable = clk_trion_pll_enable,
1537 .disable = clk_trion_pll_disable,
1538 .is_enabled = clk_trion_pll_is_enabled,
1539 .recalc_rate = clk_trion_pll_recalc_rate,
1540 .round_rate = clk_alpha_pll_round_rate,
1541 .set_rate = alpha_pll_trion_set_rate,
1543 EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_ops);
1545 const struct clk_ops clk_alpha_pll_postdiv_lucid_ops = {
1546 .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
1547 .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
1548 .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
1550 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_ops);
1552 void clk_agera_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1553 const struct alpha_pll_config *config)
1555 clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
1556 clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
1557 clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
1558 config->user_ctl_val);
1559 clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
1560 config->config_ctl_val);
1561 clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
1562 config->config_ctl_hi_val);
1563 clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
1564 config->test_ctl_val);
1565 clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
1566 config->test_ctl_hi_val);
1568 EXPORT_SYMBOL_GPL(clk_agera_pll_configure);
1570 static int clk_alpha_pll_agera_set_rate(struct clk_hw *hw, unsigned long rate,
1571 unsigned long prate)
1573 struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1574 u32 l, alpha_width = pll_alpha_width(pll);
1575 int ret;
1576 unsigned long rrate;
1577 u64 a;
1579 rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1580 ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1581 if (ret < 0)
1582 return ret;
1584 /* change L_VAL without having to go through the power on sequence */
1585 regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1586 regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
1588 if (clk_hw_is_enabled(hw))
1589 return wait_for_pll_enable_lock(pll);
1591 return 0;
1594 const struct clk_ops clk_alpha_pll_agera_ops = {
1595 .enable = clk_alpha_pll_enable,
1596 .disable = clk_alpha_pll_disable,
1597 .is_enabled = clk_alpha_pll_is_enabled,
1598 .recalc_rate = alpha_pll_fabia_recalc_rate,
1599 .round_rate = clk_alpha_pll_round_rate,
1600 .set_rate = clk_alpha_pll_agera_set_rate,
1602 EXPORT_SYMBOL_GPL(clk_alpha_pll_agera_ops);