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"
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
,
149 const char *name
= clk_hw_get_name(&pll
->clkr
.hw
);
151 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
155 for (count
= 100; count
> 0; count
--) {
156 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
159 if (inverse
&& !(val
& mask
))
161 else if ((val
& mask
) == mask
)
167 WARN(1, "%s failed to %s!\n", name
, action
);
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
)
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
)
235 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
238 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
244 if (pll
->flags
& SUPPORTS_OFFLINE_REQ
)
245 val
&= ~PLL_OFFLINE_REQ
;
247 ret
= regmap_write(pll
->clkr
.regmap
, PLL_MODE(pll
), val
);
251 /* Make sure enable request goes through before waiting for update */
254 return wait_for_pll_enable_active(pll
);
257 static void clk_alpha_pll_hwfsm_disable(struct clk_hw
*hw
)
260 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
263 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
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
);
273 ret
= wait_for_pll_offline(pll
);
279 ret
= regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
),
284 wait_for_pll_disable(pll
);
287 static int pll_is_enabled(struct clk_hw
*hw
, u32 mask
)
290 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
293 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
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
)
313 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
316 mask
= PLL_OUTCTRL
| PLL_RESET_N
| PLL_BYPASSNL
;
317 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
321 /* If in FSM mode, just vote for it */
322 if (val
& PLL_VOTE_FSM_ENA
) {
323 ret
= clk_enable_regmap(hw
);
326 return wait_for_pll_enable_active(pll
);
329 /* Skip if already enabled */
330 if ((val
& mask
) == mask
)
333 ret
= regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
),
334 PLL_BYPASSNL
, PLL_BYPASSNL
);
339 * H/W requires a 5us delay between disabling the bypass and
340 * de-asserting the reset.
345 ret
= regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
),
346 PLL_RESET_N
, PLL_RESET_N
);
350 ret
= wait_for_pll_enable_lock(pll
);
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. */
362 static void clk_alpha_pll_disable(struct clk_hw
*hw
)
365 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
368 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
372 /* If in FSM mode, just unvote it */
373 if (val
& PLL_VOTE_FSM_ENA
) {
374 clk_disable_regmap(hw
);
379 regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
), mask
, 0);
381 /* Delay of 2 output clock ticks required until output is disabled */
385 mask
= PLL_RESET_N
| PLL_BYPASSNL
;
386 regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
), mask
, 0);
390 alpha_pll_calc_rate(u64 prate
, u32 l
, u32 a
, u32 alpha_width
)
392 return (prate
* l
) + ((prate
* a
) >> ALPHA_SHIFT(alpha_width
));
396 alpha_pll_round_rate(unsigned long rate
, unsigned long prate
, u32
*l
, u64
*a
,
403 remainder
= do_div(quotient
, prate
);
411 /* Upper ALPHA_BITWIDTH bits of Alpha */
412 quotient
= remainder
<< ALPHA_SHIFT(alpha_width
);
414 remainder
= do_div(quotient
, prate
);
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
;
430 if (rate
>= v
->min_freq
&& rate
<= v
->max_freq
)
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
),
452 a
= (u64
)high
<< 32 | low
;
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
)
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
,
476 /* Wait for 2 reference cycle before checking ACK bit */
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
);
491 regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
), PLL_UPDATE
, 0);
493 ret
= wait_for_pll_update(pll
);
498 ret
= wait_for_pll_update_ack_clear(pll
);
502 /* Wait for PLL output to stabilize */
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
))
515 return __clk_alpha_pll_update_latch(pll
);
518 static int __clk_alpha_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
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
);
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");
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
);
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
,
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
,
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
);
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
))
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
);
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
595 if (a
>= BIT(PLL_HUAYRA_ALPHA_WIDTH
- 1))
598 return (prate
* l
) + (prate
* a
>> PLL_HUAYRA_ALPHA_WIDTH
);
602 alpha_huayra_pll_round_rate(unsigned long rate
, unsigned long prate
,
609 remainder
= do_div(quotient
, prate
);
617 quotient
= remainder
<< PLL_HUAYRA_ALPHA_WIDTH
;
618 remainder
= do_div(quotient
, prate
);
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))
632 return alpha_huayra_pll_calc_rate(prate
, *l
, *a
);
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
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
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
;
672 if (alpha_m
>= BIT(PLL_HUAYRA_M_WIDTH
- 1)) {
673 alpha_m
= BIT(PLL_HUAYRA_M_WIDTH
) - alpha_m
;
675 do_div(tmp
, alpha_n
);
679 do_div(tmp
, alpha_n
);
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
,
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
));
713 regmap_write(pll
->clkr
.regmap
, PLL_L_VAL(pll
), l
);
714 /* Ensure that the write above goes to detect L val change. */
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
);
723 regmap_update_bits(pll
->clkr
.regmap
, PLL_USER_CTL(pll
),
726 regmap_update_bits(pll
->clkr
.regmap
, PLL_USER_CTL(pll
),
727 PLL_ALPHA_EN
| PLL_ALPHA_MODE
, PLL_ALPHA_EN
);
732 static long alpha_pll_huayra_round_rate(struct clk_hw
*hw
, unsigned long rate
,
733 unsigned long *prate
)
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
);
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
);
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
[] = {
793 static const struct clk_div_table clk_alpha_2bit_div_table
[] = {
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
;
808 table
= clk_alpha_2bit_div_table
;
810 table
= clk_alpha_div_table
;
812 return divider_round_rate(hw
, rate
, prate
, table
,
813 pll
->width
, CLK_DIVIDER_POWER_OF_TWO
);
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
);
823 regmap_read(pll
->clkr
.regmap
, PLL_USER_CTL(pll
), &ctl
);
825 ctl
>>= PLL_POST_DIV_SHIFT
;
826 ctl
&= BIT(pll
->width
) - 1;
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
);
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
)
868 regmap_write(regmap
, PLL_L_VAL(pll
), config
->l
);
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
,
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
)
893 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
895 struct regmap
*regmap
= pll
->clkr
.regmap
;
897 ret
= regmap_read(regmap
, PLL_MODE(pll
), &val
);
901 /* If in FSM mode, just vote for it */
902 if (val
& PLL_VOTE_FSM_ENA
) {
903 ret
= clk_enable_regmap(hw
);
906 return wait_for_pll_enable_active(pll
);
909 ret
= regmap_read(regmap
, PLL_OPMODE(pll
), &opmode_val
);
913 /* Skip If PLL is already running */
914 if ((opmode_val
& FABIA_OPMODE_RUN
) && (val
& PLL_OUTCTRL
))
917 ret
= regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_OUTCTRL
, 0);
921 ret
= regmap_write(regmap
, PLL_OPMODE(pll
), FABIA_OPMODE_STANDBY
);
925 ret
= regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_RESET_N
,
930 ret
= regmap_write(regmap
, PLL_OPMODE(pll
), FABIA_OPMODE_RUN
);
934 ret
= wait_for_pll_enable_lock(pll
);
938 ret
= regmap_update_bits(regmap
, PLL_USER_CTL(pll
),
939 FABIA_PLL_OUT_MASK
, FABIA_PLL_OUT_MASK
);
943 return regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_OUTCTRL
,
947 static void alpha_pll_fabia_disable(struct clk_hw
*hw
)
950 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
952 struct regmap
*regmap
= pll
->clkr
.regmap
;
954 ret
= regmap_read(regmap
, PLL_MODE(pll
), &val
);
958 /* If in FSM mode, just unvote it */
959 if (val
& PLL_FSM_ENA
) {
960 clk_disable_regmap(hw
);
964 ret
= regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_OUTCTRL
, 0);
968 /* Disable main outputs */
969 ret
= regmap_update_bits(regmap
, PLL_USER_CTL(pll
), FABIA_PLL_OUT_MASK
,
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
,
993 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
994 u32 val
, l
, alpha_width
= pll_alpha_width(pll
);
999 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
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");
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
;
1046 if (!pll
->post_div_table
) {
1047 pr_err("Missing the post_div_table for the PLL\n");
1051 ret
= regmap_read(pll
->clkr
.regmap
, PLL_USER_CTL(pll
), &val
);
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
;
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");
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
1092 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
1096 if (val
& PLL_VOTE_FSM_ENA
)
1099 if (!pll
->post_div_table
) {
1100 pr_err("Missing the post_div_table for the PLL\n");
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
;
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
);