1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015, 2018, The Linux Foundation. All rights reserved.
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"
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_ALPHA_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL])
36 #define PLL_ALPHA_VAL_U(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL_U])
38 #define PLL_USER_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL])
39 # define PLL_POST_DIV_SHIFT 8
40 # define PLL_POST_DIV_MASK(p) GENMASK((p)->width, 0)
41 # define PLL_ALPHA_EN BIT(24)
42 # define PLL_ALPHA_MODE BIT(25)
43 # define PLL_VCO_SHIFT 20
44 # define PLL_VCO_MASK 0x3
46 #define PLL_USER_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U])
48 #define PLL_CONFIG_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL])
49 #define PLL_CONFIG_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U])
50 #define PLL_TEST_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL])
51 #define PLL_TEST_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U])
52 #define PLL_STATUS(p) ((p)->offset + (p)->regs[PLL_OFF_STATUS])
53 #define PLL_OPMODE(p) ((p)->offset + (p)->regs[PLL_OFF_OPMODE])
54 #define PLL_FRAC(p) ((p)->offset + (p)->regs[PLL_OFF_FRAC])
56 const u8 clk_alpha_pll_regs
[][PLL_OFF_MAX_REGS
] = {
57 [CLK_ALPHA_PLL_TYPE_DEFAULT
] = {
58 [PLL_OFF_L_VAL
] = 0x04,
59 [PLL_OFF_ALPHA_VAL
] = 0x08,
60 [PLL_OFF_ALPHA_VAL_U
] = 0x0c,
61 [PLL_OFF_USER_CTL
] = 0x10,
62 [PLL_OFF_USER_CTL_U
] = 0x14,
63 [PLL_OFF_CONFIG_CTL
] = 0x18,
64 [PLL_OFF_TEST_CTL
] = 0x1c,
65 [PLL_OFF_TEST_CTL_U
] = 0x20,
66 [PLL_OFF_STATUS
] = 0x24,
68 [CLK_ALPHA_PLL_TYPE_HUAYRA
] = {
69 [PLL_OFF_L_VAL
] = 0x04,
70 [PLL_OFF_ALPHA_VAL
] = 0x08,
71 [PLL_OFF_USER_CTL
] = 0x10,
72 [PLL_OFF_CONFIG_CTL
] = 0x14,
73 [PLL_OFF_CONFIG_CTL_U
] = 0x18,
74 [PLL_OFF_TEST_CTL
] = 0x1c,
75 [PLL_OFF_TEST_CTL_U
] = 0x20,
76 [PLL_OFF_STATUS
] = 0x24,
78 [CLK_ALPHA_PLL_TYPE_BRAMMO
] = {
79 [PLL_OFF_L_VAL
] = 0x04,
80 [PLL_OFF_ALPHA_VAL
] = 0x08,
81 [PLL_OFF_ALPHA_VAL_U
] = 0x0c,
82 [PLL_OFF_USER_CTL
] = 0x10,
83 [PLL_OFF_CONFIG_CTL
] = 0x18,
84 [PLL_OFF_TEST_CTL
] = 0x1c,
85 [PLL_OFF_STATUS
] = 0x24,
87 [CLK_ALPHA_PLL_TYPE_FABIA
] = {
88 [PLL_OFF_L_VAL
] = 0x04,
89 [PLL_OFF_USER_CTL
] = 0x0c,
90 [PLL_OFF_USER_CTL_U
] = 0x10,
91 [PLL_OFF_CONFIG_CTL
] = 0x14,
92 [PLL_OFF_CONFIG_CTL_U
] = 0x18,
93 [PLL_OFF_TEST_CTL
] = 0x1c,
94 [PLL_OFF_TEST_CTL_U
] = 0x20,
95 [PLL_OFF_STATUS
] = 0x24,
96 [PLL_OFF_OPMODE
] = 0x2c,
97 [PLL_OFF_FRAC
] = 0x38,
100 EXPORT_SYMBOL_GPL(clk_alpha_pll_regs
);
103 * Even though 40 bits are present, use only 32 for ease of calculation.
105 #define ALPHA_REG_BITWIDTH 40
106 #define ALPHA_REG_16BIT_WIDTH 16
107 #define ALPHA_BITWIDTH 32U
108 #define ALPHA_SHIFT(w) min(w, ALPHA_BITWIDTH)
110 #define PLL_HUAYRA_M_WIDTH 8
111 #define PLL_HUAYRA_M_SHIFT 8
112 #define PLL_HUAYRA_M_MASK 0xff
113 #define PLL_HUAYRA_N_SHIFT 0
114 #define PLL_HUAYRA_N_MASK 0xff
115 #define PLL_HUAYRA_ALPHA_WIDTH 16
117 #define FABIA_OPMODE_STANDBY 0x0
118 #define FABIA_OPMODE_RUN 0x1
120 #define FABIA_PLL_OUT_MASK 0x7
121 #define FABIA_PLL_RATE_MARGIN 500
123 #define pll_alpha_width(p) \
124 ((PLL_ALPHA_VAL_U(p) - PLL_ALPHA_VAL(p) == 4) ? \
125 ALPHA_REG_BITWIDTH : ALPHA_REG_16BIT_WIDTH)
127 #define pll_has_64bit_config(p) ((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
129 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
130 struct clk_alpha_pll, clkr)
132 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
133 struct clk_alpha_pll_postdiv, clkr)
135 static int wait_for_pll(struct clk_alpha_pll
*pll
, u32 mask
, bool inverse
,
141 const char *name
= clk_hw_get_name(&pll
->clkr
.hw
);
143 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
147 for (count
= 100; count
> 0; count
--) {
148 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
151 if (inverse
&& !(val
& mask
))
153 else if ((val
& mask
) == mask
)
159 WARN(1, "%s failed to %s!\n", name
, action
);
163 #define wait_for_pll_enable_active(pll) \
164 wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
166 #define wait_for_pll_enable_lock(pll) \
167 wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
169 #define wait_for_pll_disable(pll) \
170 wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
172 #define wait_for_pll_offline(pll) \
173 wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
175 #define wait_for_pll_update(pll) \
176 wait_for_pll(pll, PLL_UPDATE, 1, "update")
178 #define wait_for_pll_update_ack_set(pll) \
179 wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
181 #define wait_for_pll_update_ack_clear(pll) \
182 wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
184 void clk_alpha_pll_configure(struct clk_alpha_pll
*pll
, struct regmap
*regmap
,
185 const struct alpha_pll_config
*config
)
189 regmap_write(regmap
, PLL_L_VAL(pll
), config
->l
);
190 regmap_write(regmap
, PLL_ALPHA_VAL(pll
), config
->alpha
);
191 regmap_write(regmap
, PLL_CONFIG_CTL(pll
), config
->config_ctl_val
);
193 if (pll_has_64bit_config(pll
))
194 regmap_write(regmap
, PLL_CONFIG_CTL_U(pll
),
195 config
->config_ctl_hi_val
);
197 if (pll_alpha_width(pll
) > 32)
198 regmap_write(regmap
, PLL_ALPHA_VAL_U(pll
), config
->alpha_hi
);
200 val
= config
->main_output_mask
;
201 val
|= config
->aux_output_mask
;
202 val
|= config
->aux2_output_mask
;
203 val
|= config
->early_output_mask
;
204 val
|= config
->pre_div_val
;
205 val
|= config
->post_div_val
;
206 val
|= config
->vco_val
;
207 val
|= config
->alpha_en_mask
;
208 val
|= config
->alpha_mode_mask
;
210 mask
= config
->main_output_mask
;
211 mask
|= config
->aux_output_mask
;
212 mask
|= config
->aux2_output_mask
;
213 mask
|= config
->early_output_mask
;
214 mask
|= config
->pre_div_mask
;
215 mask
|= config
->post_div_mask
;
216 mask
|= config
->vco_mask
;
218 regmap_update_bits(regmap
, PLL_USER_CTL(pll
), mask
, val
);
220 if (pll
->flags
& SUPPORTS_FSM_MODE
)
221 qcom_pll_set_fsm_mode(regmap
, PLL_MODE(pll
), 6, 0);
223 EXPORT_SYMBOL_GPL(clk_alpha_pll_configure
);
225 static int clk_alpha_pll_hwfsm_enable(struct clk_hw
*hw
)
228 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
231 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
237 if (pll
->flags
& SUPPORTS_OFFLINE_REQ
)
238 val
&= ~PLL_OFFLINE_REQ
;
240 ret
= regmap_write(pll
->clkr
.regmap
, PLL_MODE(pll
), val
);
244 /* Make sure enable request goes through before waiting for update */
247 return wait_for_pll_enable_active(pll
);
250 static void clk_alpha_pll_hwfsm_disable(struct clk_hw
*hw
)
253 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
256 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
260 if (pll
->flags
& SUPPORTS_OFFLINE_REQ
) {
261 ret
= regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
),
262 PLL_OFFLINE_REQ
, PLL_OFFLINE_REQ
);
266 ret
= wait_for_pll_offline(pll
);
272 ret
= regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
),
277 wait_for_pll_disable(pll
);
280 static int pll_is_enabled(struct clk_hw
*hw
, u32 mask
)
283 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
286 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
290 return !!(val
& mask
);
293 static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw
*hw
)
295 return pll_is_enabled(hw
, PLL_ACTIVE_FLAG
);
298 static int clk_alpha_pll_is_enabled(struct clk_hw
*hw
)
300 return pll_is_enabled(hw
, PLL_LOCK_DET
);
303 static int clk_alpha_pll_enable(struct clk_hw
*hw
)
306 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
309 mask
= PLL_OUTCTRL
| PLL_RESET_N
| PLL_BYPASSNL
;
310 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
314 /* If in FSM mode, just vote for it */
315 if (val
& PLL_VOTE_FSM_ENA
) {
316 ret
= clk_enable_regmap(hw
);
319 return wait_for_pll_enable_active(pll
);
322 /* Skip if already enabled */
323 if ((val
& mask
) == mask
)
326 ret
= regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
),
327 PLL_BYPASSNL
, PLL_BYPASSNL
);
332 * H/W requires a 5us delay between disabling the bypass and
333 * de-asserting the reset.
338 ret
= regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
),
339 PLL_RESET_N
, PLL_RESET_N
);
343 ret
= wait_for_pll_enable_lock(pll
);
347 ret
= regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
),
348 PLL_OUTCTRL
, PLL_OUTCTRL
);
350 /* Ensure that the write above goes through before returning. */
355 static void clk_alpha_pll_disable(struct clk_hw
*hw
)
358 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
361 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
365 /* If in FSM mode, just unvote it */
366 if (val
& PLL_VOTE_FSM_ENA
) {
367 clk_disable_regmap(hw
);
372 regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
), mask
, 0);
374 /* Delay of 2 output clock ticks required until output is disabled */
378 mask
= PLL_RESET_N
| PLL_BYPASSNL
;
379 regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
), mask
, 0);
383 alpha_pll_calc_rate(u64 prate
, u32 l
, u32 a
, u32 alpha_width
)
385 return (prate
* l
) + ((prate
* a
) >> ALPHA_SHIFT(alpha_width
));
389 alpha_pll_round_rate(unsigned long rate
, unsigned long prate
, u32
*l
, u64
*a
,
396 remainder
= do_div(quotient
, prate
);
404 /* Upper ALPHA_BITWIDTH bits of Alpha */
405 quotient
= remainder
<< ALPHA_SHIFT(alpha_width
);
407 remainder
= do_div(quotient
, prate
);
413 return alpha_pll_calc_rate(prate
, *l
, *a
, alpha_width
);
416 static const struct pll_vco
*
417 alpha_pll_find_vco(const struct clk_alpha_pll
*pll
, unsigned long rate
)
419 const struct pll_vco
*v
= pll
->vco_table
;
420 const struct pll_vco
*end
= v
+ pll
->num_vco
;
423 if (rate
>= v
->min_freq
&& rate
<= v
->max_freq
)
430 clk_alpha_pll_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
432 u32 l
, low
, high
, ctl
;
433 u64 a
= 0, prate
= parent_rate
;
434 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
435 u32 alpha_width
= pll_alpha_width(pll
);
437 regmap_read(pll
->clkr
.regmap
, PLL_L_VAL(pll
), &l
);
439 regmap_read(pll
->clkr
.regmap
, PLL_USER_CTL(pll
), &ctl
);
440 if (ctl
& PLL_ALPHA_EN
) {
441 regmap_read(pll
->clkr
.regmap
, PLL_ALPHA_VAL(pll
), &low
);
442 if (alpha_width
> 32) {
443 regmap_read(pll
->clkr
.regmap
, PLL_ALPHA_VAL_U(pll
),
445 a
= (u64
)high
<< 32 | low
;
447 a
= low
& GENMASK(alpha_width
- 1, 0);
450 if (alpha_width
> ALPHA_BITWIDTH
)
451 a
>>= alpha_width
- ALPHA_BITWIDTH
;
454 return alpha_pll_calc_rate(prate
, l
, a
, alpha_width
);
458 static int __clk_alpha_pll_update_latch(struct clk_alpha_pll
*pll
)
463 regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &mode
);
465 /* Latch the input to the PLL */
466 regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
), PLL_UPDATE
,
469 /* Wait for 2 reference cycle before checking ACK bit */
473 * PLL will latch the new L, Alpha and freq control word.
474 * PLL will respond by raising PLL_ACK_LATCH output when new programming
475 * has been latched in and PLL is being updated. When
476 * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
477 * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
479 if (mode
& PLL_UPDATE_BYPASS
) {
480 ret
= wait_for_pll_update_ack_set(pll
);
484 regmap_update_bits(pll
->clkr
.regmap
, PLL_MODE(pll
), PLL_UPDATE
, 0);
486 ret
= wait_for_pll_update(pll
);
491 ret
= wait_for_pll_update_ack_clear(pll
);
495 /* Wait for PLL output to stabilize */
501 static int clk_alpha_pll_update_latch(struct clk_alpha_pll
*pll
,
502 int (*is_enabled
)(struct clk_hw
*))
504 if (!is_enabled(&pll
->clkr
.hw
) ||
505 !(pll
->flags
& SUPPORTS_DYNAMIC_UPDATE
))
508 return __clk_alpha_pll_update_latch(pll
);
511 static int __clk_alpha_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
513 int (*is_enabled
)(struct clk_hw
*))
515 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
516 const struct pll_vco
*vco
;
517 u32 l
, alpha_width
= pll_alpha_width(pll
);
520 rate
= alpha_pll_round_rate(rate
, prate
, &l
, &a
, alpha_width
);
521 vco
= alpha_pll_find_vco(pll
, rate
);
522 if (pll
->vco_table
&& !vco
) {
523 pr_err("alpha pll not in a valid vco range\n");
527 regmap_write(pll
->clkr
.regmap
, PLL_L_VAL(pll
), l
);
529 if (alpha_width
> ALPHA_BITWIDTH
)
530 a
<<= alpha_width
- ALPHA_BITWIDTH
;
532 if (alpha_width
> 32)
533 regmap_write(pll
->clkr
.regmap
, PLL_ALPHA_VAL_U(pll
), a
>> 32);
535 regmap_write(pll
->clkr
.regmap
, PLL_ALPHA_VAL(pll
), a
);
538 regmap_update_bits(pll
->clkr
.regmap
, PLL_USER_CTL(pll
),
539 PLL_VCO_MASK
<< PLL_VCO_SHIFT
,
540 vco
->val
<< PLL_VCO_SHIFT
);
543 regmap_update_bits(pll
->clkr
.regmap
, PLL_USER_CTL(pll
),
544 PLL_ALPHA_EN
, PLL_ALPHA_EN
);
546 return clk_alpha_pll_update_latch(pll
, is_enabled
);
549 static int clk_alpha_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
552 return __clk_alpha_pll_set_rate(hw
, rate
, prate
,
553 clk_alpha_pll_is_enabled
);
556 static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw
*hw
, unsigned long rate
,
559 return __clk_alpha_pll_set_rate(hw
, rate
, prate
,
560 clk_alpha_pll_hwfsm_is_enabled
);
563 static long clk_alpha_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
564 unsigned long *prate
)
566 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
567 u32 l
, alpha_width
= pll_alpha_width(pll
);
569 unsigned long min_freq
, max_freq
;
571 rate
= alpha_pll_round_rate(rate
, *prate
, &l
, &a
, alpha_width
);
572 if (!pll
->vco_table
|| alpha_pll_find_vco(pll
, rate
))
575 min_freq
= pll
->vco_table
[0].min_freq
;
576 max_freq
= pll
->vco_table
[pll
->num_vco
- 1].max_freq
;
578 return clamp(rate
, min_freq
, max_freq
);
582 alpha_huayra_pll_calc_rate(u64 prate
, u32 l
, u32 a
)
585 * a contains 16 bit alpha_val in two’s compliment number in the range
588 if (a
>= BIT(PLL_HUAYRA_ALPHA_WIDTH
- 1))
591 return (prate
* l
) + (prate
* a
>> PLL_HUAYRA_ALPHA_WIDTH
);
595 alpha_huayra_pll_round_rate(unsigned long rate
, unsigned long prate
,
602 remainder
= do_div(quotient
, prate
);
610 quotient
= remainder
<< PLL_HUAYRA_ALPHA_WIDTH
;
611 remainder
= do_div(quotient
, prate
);
617 * alpha_val should be in two’s compliment number in the range
618 * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
619 * since alpha value will be subtracted in this case.
621 if (quotient
>= BIT(PLL_HUAYRA_ALPHA_WIDTH
- 1))
625 return alpha_huayra_pll_calc_rate(prate
, *l
, *a
);
629 alpha_pll_huayra_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
631 u64 rate
= parent_rate
, tmp
;
632 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
633 u32 l
, alpha
= 0, ctl
, alpha_m
, alpha_n
;
635 regmap_read(pll
->clkr
.regmap
, PLL_L_VAL(pll
), &l
);
636 regmap_read(pll
->clkr
.regmap
, PLL_USER_CTL(pll
), &ctl
);
638 if (ctl
& PLL_ALPHA_EN
) {
639 regmap_read(pll
->clkr
.regmap
, PLL_ALPHA_VAL(pll
), &alpha
);
641 * Depending upon alpha_mode, it can be treated as M/N value or
642 * as a two’s compliment number. When alpha_mode=1,
643 * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
647 * M is a signed number (-128 to 127) and N is unsigned
648 * (0 to 255). M/N has to be within +/-0.5.
650 * When alpha_mode=0, it is a two’s compliment number in the
653 * Fout=FIN*(L+(alpha_val)/2^16)
655 * where alpha_val is two’s compliment number.
657 if (!(ctl
& PLL_ALPHA_MODE
))
658 return alpha_huayra_pll_calc_rate(rate
, l
, alpha
);
660 alpha_m
= alpha
>> PLL_HUAYRA_M_SHIFT
& PLL_HUAYRA_M_MASK
;
661 alpha_n
= alpha
>> PLL_HUAYRA_N_SHIFT
& PLL_HUAYRA_N_MASK
;
665 if (alpha_m
>= BIT(PLL_HUAYRA_M_WIDTH
- 1)) {
666 alpha_m
= BIT(PLL_HUAYRA_M_WIDTH
) - alpha_m
;
668 do_div(tmp
, alpha_n
);
672 do_div(tmp
, alpha_n
);
679 return alpha_huayra_pll_calc_rate(rate
, l
, alpha
);
682 static int alpha_pll_huayra_set_rate(struct clk_hw
*hw
, unsigned long rate
,
685 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
686 u32 l
, a
, ctl
, cur_alpha
= 0;
688 rate
= alpha_huayra_pll_round_rate(rate
, prate
, &l
, &a
);
690 regmap_read(pll
->clkr
.regmap
, PLL_USER_CTL(pll
), &ctl
);
692 if (ctl
& PLL_ALPHA_EN
)
693 regmap_read(pll
->clkr
.regmap
, PLL_ALPHA_VAL(pll
), &cur_alpha
);
696 * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
697 * without having to go through the power on sequence.
699 if (clk_alpha_pll_is_enabled(hw
)) {
700 if (cur_alpha
!= a
) {
701 pr_err("clock needs to be gated %s\n",
702 clk_hw_get_name(hw
));
706 regmap_write(pll
->clkr
.regmap
, PLL_L_VAL(pll
), l
);
707 /* Ensure that the write above goes to detect L val change. */
709 return wait_for_pll_enable_lock(pll
);
712 regmap_write(pll
->clkr
.regmap
, PLL_L_VAL(pll
), l
);
713 regmap_write(pll
->clkr
.regmap
, PLL_ALPHA_VAL(pll
), a
);
716 regmap_update_bits(pll
->clkr
.regmap
, PLL_USER_CTL(pll
),
719 regmap_update_bits(pll
->clkr
.regmap
, PLL_USER_CTL(pll
),
720 PLL_ALPHA_EN
| PLL_ALPHA_MODE
, PLL_ALPHA_EN
);
725 static long alpha_pll_huayra_round_rate(struct clk_hw
*hw
, unsigned long rate
,
726 unsigned long *prate
)
730 return alpha_huayra_pll_round_rate(rate
, *prate
, &l
, &a
);
733 const struct clk_ops clk_alpha_pll_ops
= {
734 .enable
= clk_alpha_pll_enable
,
735 .disable
= clk_alpha_pll_disable
,
736 .is_enabled
= clk_alpha_pll_is_enabled
,
737 .recalc_rate
= clk_alpha_pll_recalc_rate
,
738 .round_rate
= clk_alpha_pll_round_rate
,
739 .set_rate
= clk_alpha_pll_set_rate
,
741 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops
);
743 const struct clk_ops clk_alpha_pll_huayra_ops
= {
744 .enable
= clk_alpha_pll_enable
,
745 .disable
= clk_alpha_pll_disable
,
746 .is_enabled
= clk_alpha_pll_is_enabled
,
747 .recalc_rate
= alpha_pll_huayra_recalc_rate
,
748 .round_rate
= alpha_pll_huayra_round_rate
,
749 .set_rate
= alpha_pll_huayra_set_rate
,
751 EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops
);
753 const struct clk_ops clk_alpha_pll_hwfsm_ops
= {
754 .enable
= clk_alpha_pll_hwfsm_enable
,
755 .disable
= clk_alpha_pll_hwfsm_disable
,
756 .is_enabled
= clk_alpha_pll_hwfsm_is_enabled
,
757 .recalc_rate
= clk_alpha_pll_recalc_rate
,
758 .round_rate
= clk_alpha_pll_round_rate
,
759 .set_rate
= clk_alpha_pll_hwfsm_set_rate
,
761 EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops
);
764 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
766 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
769 regmap_read(pll
->clkr
.regmap
, PLL_USER_CTL(pll
), &ctl
);
771 ctl
>>= PLL_POST_DIV_SHIFT
;
772 ctl
&= PLL_POST_DIV_MASK(pll
);
774 return parent_rate
>> fls(ctl
);
777 static const struct clk_div_table clk_alpha_div_table
[] = {
786 static const struct clk_div_table clk_alpha_2bit_div_table
[] = {
794 clk_alpha_pll_postdiv_round_rate(struct clk_hw
*hw
, unsigned long rate
,
795 unsigned long *prate
)
797 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
798 const struct clk_div_table
*table
;
801 table
= clk_alpha_2bit_div_table
;
803 table
= clk_alpha_div_table
;
805 return divider_round_rate(hw
, rate
, prate
, table
,
806 pll
->width
, CLK_DIVIDER_POWER_OF_TWO
);
810 clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw
*hw
, unsigned long rate
,
811 unsigned long *prate
)
813 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
816 regmap_read(pll
->clkr
.regmap
, PLL_USER_CTL(pll
), &ctl
);
818 ctl
>>= PLL_POST_DIV_SHIFT
;
819 ctl
&= BIT(pll
->width
) - 1;
822 if (clk_hw_get_flags(hw
) & CLK_SET_RATE_PARENT
)
823 *prate
= clk_hw_round_rate(clk_hw_get_parent(hw
), div
* rate
);
825 return DIV_ROUND_UP_ULL((u64
)*prate
, div
);
828 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw
*hw
, unsigned long rate
,
829 unsigned long parent_rate
)
831 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
834 /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
835 div
= DIV_ROUND_UP_ULL((u64
)parent_rate
, rate
) - 1;
837 return regmap_update_bits(pll
->clkr
.regmap
, PLL_USER_CTL(pll
),
838 PLL_POST_DIV_MASK(pll
) << PLL_POST_DIV_SHIFT
,
839 div
<< PLL_POST_DIV_SHIFT
);
842 const struct clk_ops clk_alpha_pll_postdiv_ops
= {
843 .recalc_rate
= clk_alpha_pll_postdiv_recalc_rate
,
844 .round_rate
= clk_alpha_pll_postdiv_round_rate
,
845 .set_rate
= clk_alpha_pll_postdiv_set_rate
,
847 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops
);
849 const struct clk_ops clk_alpha_pll_postdiv_ro_ops
= {
850 .round_rate
= clk_alpha_pll_postdiv_round_ro_rate
,
851 .recalc_rate
= clk_alpha_pll_postdiv_recalc_rate
,
853 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops
);
855 void clk_fabia_pll_configure(struct clk_alpha_pll
*pll
, struct regmap
*regmap
,
856 const struct alpha_pll_config
*config
)
861 regmap_write(regmap
, PLL_L_VAL(pll
), config
->l
);
864 regmap_write(regmap
, PLL_FRAC(pll
), config
->alpha
);
866 if (config
->config_ctl_val
)
867 regmap_write(regmap
, PLL_CONFIG_CTL(pll
),
868 config
->config_ctl_val
);
870 if (config
->post_div_mask
) {
871 mask
= config
->post_div_mask
;
872 val
= config
->post_div_val
;
873 regmap_update_bits(regmap
, PLL_USER_CTL(pll
), mask
, val
);
876 regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_UPDATE_BYPASS
,
879 regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_RESET_N
, PLL_RESET_N
);
881 EXPORT_SYMBOL_GPL(clk_fabia_pll_configure
);
883 static int alpha_pll_fabia_enable(struct clk_hw
*hw
)
886 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
888 struct regmap
*regmap
= pll
->clkr
.regmap
;
890 ret
= regmap_read(regmap
, PLL_MODE(pll
), &val
);
894 /* If in FSM mode, just vote for it */
895 if (val
& PLL_VOTE_FSM_ENA
) {
896 ret
= clk_enable_regmap(hw
);
899 return wait_for_pll_enable_active(pll
);
902 ret
= regmap_read(regmap
, PLL_OPMODE(pll
), &opmode_val
);
906 /* Skip If PLL is already running */
907 if ((opmode_val
& FABIA_OPMODE_RUN
) && (val
& PLL_OUTCTRL
))
910 ret
= regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_OUTCTRL
, 0);
914 ret
= regmap_write(regmap
, PLL_OPMODE(pll
), FABIA_OPMODE_STANDBY
);
918 ret
= regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_RESET_N
,
923 ret
= regmap_write(regmap
, PLL_OPMODE(pll
), FABIA_OPMODE_RUN
);
927 ret
= wait_for_pll_enable_lock(pll
);
931 ret
= regmap_update_bits(regmap
, PLL_USER_CTL(pll
),
932 FABIA_PLL_OUT_MASK
, FABIA_PLL_OUT_MASK
);
936 return regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_OUTCTRL
,
940 static void alpha_pll_fabia_disable(struct clk_hw
*hw
)
943 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
945 struct regmap
*regmap
= pll
->clkr
.regmap
;
947 ret
= regmap_read(regmap
, PLL_MODE(pll
), &val
);
951 /* If in FSM mode, just unvote it */
952 if (val
& PLL_FSM_ENA
) {
953 clk_disable_regmap(hw
);
957 ret
= regmap_update_bits(regmap
, PLL_MODE(pll
), PLL_OUTCTRL
, 0);
961 /* Disable main outputs */
962 ret
= regmap_update_bits(regmap
, PLL_USER_CTL(pll
), FABIA_PLL_OUT_MASK
,
967 /* Place the PLL in STANDBY */
968 regmap_write(regmap
, PLL_OPMODE(pll
), FABIA_OPMODE_STANDBY
);
971 static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw
*hw
,
972 unsigned long parent_rate
)
974 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
975 u32 l
, frac
, alpha_width
= pll_alpha_width(pll
);
977 regmap_read(pll
->clkr
.regmap
, PLL_L_VAL(pll
), &l
);
978 regmap_read(pll
->clkr
.regmap
, PLL_FRAC(pll
), &frac
);
980 return alpha_pll_calc_rate(parent_rate
, l
, frac
, alpha_width
);
983 static int alpha_pll_fabia_set_rate(struct clk_hw
*hw
, unsigned long rate
,
986 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
987 u32 val
, l
, alpha_width
= pll_alpha_width(pll
);
992 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
996 rrate
= alpha_pll_round_rate(rate
, prate
, &l
, &a
, alpha_width
);
999 * Due to limited number of bits for fractional rate programming, the
1000 * rounded up rate could be marginally higher than the requested rate.
1002 if (rrate
> (rate
+ FABIA_PLL_RATE_MARGIN
) || rrate
< rate
) {
1003 pr_err("Call set rate on the PLL with rounded rates!\n");
1007 regmap_write(pll
->clkr
.regmap
, PLL_L_VAL(pll
), l
);
1008 regmap_write(pll
->clkr
.regmap
, PLL_FRAC(pll
), a
);
1010 return __clk_alpha_pll_update_latch(pll
);
1013 const struct clk_ops clk_alpha_pll_fabia_ops
= {
1014 .enable
= alpha_pll_fabia_enable
,
1015 .disable
= alpha_pll_fabia_disable
,
1016 .is_enabled
= clk_alpha_pll_is_enabled
,
1017 .set_rate
= alpha_pll_fabia_set_rate
,
1018 .recalc_rate
= alpha_pll_fabia_recalc_rate
,
1019 .round_rate
= clk_alpha_pll_round_rate
,
1021 EXPORT_SYMBOL_GPL(clk_alpha_pll_fabia_ops
);
1023 const struct clk_ops clk_alpha_pll_fixed_fabia_ops
= {
1024 .enable
= alpha_pll_fabia_enable
,
1025 .disable
= alpha_pll_fabia_disable
,
1026 .is_enabled
= clk_alpha_pll_is_enabled
,
1027 .recalc_rate
= alpha_pll_fabia_recalc_rate
,
1028 .round_rate
= clk_alpha_pll_round_rate
,
1030 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_fabia_ops
);
1032 static unsigned long clk_alpha_pll_postdiv_fabia_recalc_rate(struct clk_hw
*hw
,
1033 unsigned long parent_rate
)
1035 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
1036 u32 i
, div
= 1, val
;
1039 if (!pll
->post_div_table
) {
1040 pr_err("Missing the post_div_table for the PLL\n");
1044 ret
= regmap_read(pll
->clkr
.regmap
, PLL_USER_CTL(pll
), &val
);
1048 val
>>= pll
->post_div_shift
;
1049 val
&= BIT(pll
->width
) - 1;
1051 for (i
= 0; i
< pll
->num_post_div
; i
++) {
1052 if (pll
->post_div_table
[i
].val
== val
) {
1053 div
= pll
->post_div_table
[i
].div
;
1058 return (parent_rate
/ div
);
1061 static long clk_alpha_pll_postdiv_fabia_round_rate(struct clk_hw
*hw
,
1062 unsigned long rate
, unsigned long *prate
)
1064 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
1066 if (!pll
->post_div_table
) {
1067 pr_err("Missing the post_div_table for the PLL\n");
1071 return divider_round_rate(hw
, rate
, prate
, pll
->post_div_table
,
1072 pll
->width
, CLK_DIVIDER_ROUND_CLOSEST
);
1075 static int clk_alpha_pll_postdiv_fabia_set_rate(struct clk_hw
*hw
,
1076 unsigned long rate
, unsigned long parent_rate
)
1078 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
1079 int i
, val
= 0, div
, ret
;
1082 * If the PLL is in FSM mode, then treat set_rate callback as a
1085 ret
= regmap_read(pll
->clkr
.regmap
, PLL_MODE(pll
), &val
);
1089 if (val
& PLL_VOTE_FSM_ENA
)
1092 if (!pll
->post_div_table
) {
1093 pr_err("Missing the post_div_table for the PLL\n");
1097 div
= DIV_ROUND_UP_ULL((u64
)parent_rate
, rate
);
1098 for (i
= 0; i
< pll
->num_post_div
; i
++) {
1099 if (pll
->post_div_table
[i
].div
== div
) {
1100 val
= pll
->post_div_table
[i
].val
;
1105 return regmap_update_bits(pll
->clkr
.regmap
, PLL_USER_CTL(pll
),
1106 (BIT(pll
->width
) - 1) << pll
->post_div_shift
,
1107 val
<< pll
->post_div_shift
);
1110 const struct clk_ops clk_alpha_pll_postdiv_fabia_ops
= {
1111 .recalc_rate
= clk_alpha_pll_postdiv_fabia_recalc_rate
,
1112 .round_rate
= clk_alpha_pll_postdiv_fabia_round_rate
,
1113 .set_rate
= clk_alpha_pll_postdiv_fabia_set_rate
,
1115 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_fabia_ops
);