2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/export.h>
16 #include <linux/clk-provider.h>
17 #include <linux/regmap.h>
18 #include <linux/delay.h>
20 #include "clk-alpha-pll.h"
23 # define PLL_OUTCTRL BIT(0)
24 # define PLL_BYPASSNL BIT(1)
25 # define PLL_RESET_N BIT(2)
26 # define PLL_LOCK_COUNT_SHIFT 8
27 # define PLL_LOCK_COUNT_MASK 0x3f
28 # define PLL_BIAS_COUNT_SHIFT 14
29 # define PLL_BIAS_COUNT_MASK 0x3f
30 # define PLL_VOTE_FSM_ENA BIT(20)
31 # define PLL_VOTE_FSM_RESET BIT(21)
32 # define PLL_ACTIVE_FLAG BIT(30)
33 # define PLL_LOCK_DET BIT(31)
35 #define PLL_L_VAL 0x04
36 #define PLL_ALPHA_VAL 0x08
37 #define PLL_ALPHA_VAL_U 0x0c
39 #define PLL_USER_CTL 0x10
40 # define PLL_POST_DIV_SHIFT 8
41 # define PLL_POST_DIV_MASK 0xf
42 # define PLL_ALPHA_EN BIT(24)
43 # define PLL_VCO_SHIFT 20
44 # define PLL_VCO_MASK 0x3
46 #define PLL_USER_CTL_U 0x14
48 #define PLL_CONFIG_CTL 0x18
49 #define PLL_TEST_CTL 0x1c
50 #define PLL_TEST_CTL_U 0x20
51 #define PLL_STATUS 0x24
54 * Even though 40 bits are present, use only 32 for ease of calculation.
56 #define ALPHA_REG_BITWIDTH 40
57 #define ALPHA_BITWIDTH 32
59 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
60 struct clk_alpha_pll, clkr)
62 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
63 struct clk_alpha_pll_postdiv, clkr)
65 static int wait_for_pll(struct clk_alpha_pll
*pll
)
70 const char *name
= clk_hw_get_name(&pll
->clkr
.hw
);
73 ret
= regmap_read(pll
->clkr
.regmap
, off
+ PLL_MODE
, &val
);
77 if (val
& PLL_VOTE_FSM_ENA
)
78 mask
= PLL_ACTIVE_FLAG
;
82 /* Wait for pll to enable. */
83 for (count
= 100; count
> 0; count
--) {
84 ret
= regmap_read(pll
->clkr
.regmap
, off
+ PLL_MODE
, &val
);
87 if ((val
& mask
) == mask
)
93 WARN(1, "%s didn't enable after voting for it!\n", name
);
97 static int clk_alpha_pll_enable(struct clk_hw
*hw
)
100 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
105 mask
= PLL_OUTCTRL
| PLL_RESET_N
| PLL_BYPASSNL
;
106 ret
= regmap_read(pll
->clkr
.regmap
, off
+ PLL_MODE
, &val
);
110 /* If in FSM mode, just vote for it */
111 if (val
& PLL_VOTE_FSM_ENA
) {
112 ret
= clk_enable_regmap(hw
);
115 return wait_for_pll(pll
);
118 /* Skip if already enabled */
119 if ((val
& mask
) == mask
)
122 ret
= regmap_update_bits(pll
->clkr
.regmap
, off
+ PLL_MODE
,
123 PLL_BYPASSNL
, PLL_BYPASSNL
);
128 * H/W requires a 5us delay between disabling the bypass and
129 * de-asserting the reset.
134 ret
= regmap_update_bits(pll
->clkr
.regmap
, off
+ PLL_MODE
,
135 PLL_RESET_N
, PLL_RESET_N
);
139 ret
= wait_for_pll(pll
);
143 ret
= regmap_update_bits(pll
->clkr
.regmap
, off
+ PLL_MODE
,
144 PLL_OUTCTRL
, PLL_OUTCTRL
);
146 /* Ensure that the write above goes through before returning. */
151 static void clk_alpha_pll_disable(struct clk_hw
*hw
)
154 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
159 ret
= regmap_read(pll
->clkr
.regmap
, off
+ PLL_MODE
, &val
);
163 /* If in FSM mode, just unvote it */
164 if (val
& PLL_VOTE_FSM_ENA
) {
165 clk_disable_regmap(hw
);
170 regmap_update_bits(pll
->clkr
.regmap
, off
+ PLL_MODE
, mask
, 0);
172 /* Delay of 2 output clock ticks required until output is disabled */
176 mask
= PLL_RESET_N
| PLL_BYPASSNL
;
177 regmap_update_bits(pll
->clkr
.regmap
, off
+ PLL_MODE
, mask
, 0);
180 static unsigned long alpha_pll_calc_rate(u64 prate
, u32 l
, u32 a
)
182 return (prate
* l
) + ((prate
* a
) >> ALPHA_BITWIDTH
);
186 alpha_pll_round_rate(unsigned long rate
, unsigned long prate
, u32
*l
, u64
*a
)
192 remainder
= do_div(quotient
, prate
);
200 /* Upper ALPHA_BITWIDTH bits of Alpha */
201 quotient
= remainder
<< ALPHA_BITWIDTH
;
202 remainder
= do_div(quotient
, prate
);
208 return alpha_pll_calc_rate(prate
, *l
, *a
);
211 static const struct pll_vco
*
212 alpha_pll_find_vco(const struct clk_alpha_pll
*pll
, unsigned long rate
)
214 const struct pll_vco
*v
= pll
->vco_table
;
215 const struct pll_vco
*end
= v
+ pll
->num_vco
;
218 if (rate
>= v
->min_freq
&& rate
<= v
->max_freq
)
225 clk_alpha_pll_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
227 u32 l
, low
, high
, ctl
;
228 u64 a
= 0, prate
= parent_rate
;
229 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
230 u32 off
= pll
->offset
;
232 regmap_read(pll
->clkr
.regmap
, off
+ PLL_L_VAL
, &l
);
234 regmap_read(pll
->clkr
.regmap
, off
+ PLL_USER_CTL
, &ctl
);
235 if (ctl
& PLL_ALPHA_EN
) {
236 regmap_read(pll
->clkr
.regmap
, off
+ PLL_ALPHA_VAL
, &low
);
237 regmap_read(pll
->clkr
.regmap
, off
+ PLL_ALPHA_VAL_U
, &high
);
238 a
= (u64
)high
<< 32 | low
;
239 a
>>= ALPHA_REG_BITWIDTH
- ALPHA_BITWIDTH
;
242 return alpha_pll_calc_rate(prate
, l
, a
);
245 static int clk_alpha_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
248 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
249 const struct pll_vco
*vco
;
250 u32 l
, off
= pll
->offset
;
253 rate
= alpha_pll_round_rate(rate
, prate
, &l
, &a
);
254 vco
= alpha_pll_find_vco(pll
, rate
);
256 pr_err("alpha pll not in a valid vco range\n");
260 a
<<= (ALPHA_REG_BITWIDTH
- ALPHA_BITWIDTH
);
262 regmap_write(pll
->clkr
.regmap
, off
+ PLL_L_VAL
, l
);
263 regmap_write(pll
->clkr
.regmap
, off
+ PLL_ALPHA_VAL
, a
);
264 regmap_write(pll
->clkr
.regmap
, off
+ PLL_ALPHA_VAL_U
, a
>> 32);
266 regmap_update_bits(pll
->clkr
.regmap
, off
+ PLL_USER_CTL
,
267 PLL_VCO_MASK
<< PLL_VCO_SHIFT
,
268 vco
->val
<< PLL_VCO_SHIFT
);
270 regmap_update_bits(pll
->clkr
.regmap
, off
+ PLL_USER_CTL
, PLL_ALPHA_EN
,
276 static long clk_alpha_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
277 unsigned long *prate
)
279 struct clk_alpha_pll
*pll
= to_clk_alpha_pll(hw
);
282 unsigned long min_freq
, max_freq
;
284 rate
= alpha_pll_round_rate(rate
, *prate
, &l
, &a
);
285 if (alpha_pll_find_vco(pll
, rate
))
288 min_freq
= pll
->vco_table
[0].min_freq
;
289 max_freq
= pll
->vco_table
[pll
->num_vco
- 1].max_freq
;
291 return clamp(rate
, min_freq
, max_freq
);
294 const struct clk_ops clk_alpha_pll_ops
= {
295 .enable
= clk_alpha_pll_enable
,
296 .disable
= clk_alpha_pll_disable
,
297 .recalc_rate
= clk_alpha_pll_recalc_rate
,
298 .round_rate
= clk_alpha_pll_round_rate
,
299 .set_rate
= clk_alpha_pll_set_rate
,
301 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops
);
304 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
306 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
309 regmap_read(pll
->clkr
.regmap
, pll
->offset
+ PLL_USER_CTL
, &ctl
);
311 ctl
>>= PLL_POST_DIV_SHIFT
;
312 ctl
&= PLL_POST_DIV_MASK
;
314 return parent_rate
>> fls(ctl
);
317 static const struct clk_div_table clk_alpha_div_table
[] = {
327 clk_alpha_pll_postdiv_round_rate(struct clk_hw
*hw
, unsigned long rate
,
328 unsigned long *prate
)
330 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
332 return divider_round_rate(hw
, rate
, prate
, clk_alpha_div_table
,
333 pll
->width
, CLK_DIVIDER_POWER_OF_TWO
);
336 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw
*hw
, unsigned long rate
,
337 unsigned long parent_rate
)
339 struct clk_alpha_pll_postdiv
*pll
= to_clk_alpha_pll_postdiv(hw
);
342 /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
343 div
= DIV_ROUND_UP_ULL((u64
)parent_rate
, rate
) - 1;
345 return regmap_update_bits(pll
->clkr
.regmap
, pll
->offset
+ PLL_USER_CTL
,
346 PLL_POST_DIV_MASK
<< PLL_POST_DIV_SHIFT
,
347 div
<< PLL_POST_DIV_SHIFT
);
350 const struct clk_ops clk_alpha_pll_postdiv_ops
= {
351 .recalc_rate
= clk_alpha_pll_postdiv_recalc_rate
,
352 .round_rate
= clk_alpha_pll_postdiv_round_rate
,
353 .set_rate
= clk_alpha_pll_postdiv_set_rate
,
355 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops
);