xref: /openbmc/linux/drivers/clk/qcom/clk-alpha-pll.c (revision 84b102f5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015, 2018, The Linux Foundation. All rights reserved.
4  */
5 
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>
11 
12 #include "clk-alpha-pll.h"
13 #include "common.h"
14 
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)
33 
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])
38 
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
46 
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])
49 
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])
59 
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,
71 	},
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,
81 	},
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,
90 	},
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,
102 	},
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,
118 	},
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,
128 	},
129 };
130 EXPORT_SYMBOL_GPL(clk_alpha_pll_regs);
131 
132 /*
133  * Even though 40 bits are present, use only 32 for ease of calculation.
134  */
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)
139 
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
146 
147 #define PLL_STANDBY		0x0
148 #define PLL_RUN			0x1
149 #define PLL_OUT_MASK		0x7
150 #define PLL_RATE_MARGIN		500
151 
152 /* TRION PLL specific settings and offsets */
153 #define TRION_PLL_CAL_VAL	0x44
154 #define TRION_PCAL_DONE		BIT(26)
155 
156 /* LUCID PLL specific settings and offsets */
157 #define LUCID_PCAL_DONE		BIT(27)
158 
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)
162 
163 #define pll_has_64bit_config(p)	((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
164 
165 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
166 					   struct clk_alpha_pll, clkr)
167 
168 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
169 					   struct clk_alpha_pll_postdiv, clkr)
170 
171 static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
172 			const char *action)
173 {
174 	u32 val;
175 	int count;
176 	int ret;
177 	const char *name = clk_hw_get_name(&pll->clkr.hw);
178 
179 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
180 	if (ret)
181 		return ret;
182 
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;
191 
192 		udelay(1);
193 	}
194 
195 	WARN(1, "%s failed to %s!\n", name, action);
196 	return -ETIMEDOUT;
197 }
198 
199 #define wait_for_pll_enable_active(pll) \
200 	wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
201 
202 #define wait_for_pll_enable_lock(pll) \
203 	wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
204 
205 #define wait_for_pll_disable(pll) \
206 	wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
207 
208 #define wait_for_pll_offline(pll) \
209 	wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
210 
211 #define wait_for_pll_update(pll) \
212 	wait_for_pll(pll, PLL_UPDATE, 1, "update")
213 
214 #define wait_for_pll_update_ack_set(pll) \
215 	wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
216 
217 #define wait_for_pll_update_ack_clear(pll) \
218 	wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
219 
220 static void clk_alpha_pll_write_config(struct regmap *regmap, unsigned int reg,
221 					unsigned int val)
222 {
223 	if (val)
224 		regmap_write(regmap, reg, val);
225 }
226 
227 void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
228 			     const struct alpha_pll_config *config)
229 {
230 	u32 val, mask;
231 
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);
235 
236 	if (pll_has_64bit_config(pll))
237 		regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
238 			     config->config_ctl_hi_val);
239 
240 	if (pll_alpha_width(pll) > 32)
241 		regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
242 
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;
252 
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;
260 
261 	regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
262 
263 	if (pll->flags & SUPPORTS_FSM_MODE)
264 		qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
265 }
266 EXPORT_SYMBOL_GPL(clk_alpha_pll_configure);
267 
268 static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
269 {
270 	int ret;
271 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
272 	u32 val;
273 
274 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
275 	if (ret)
276 		return ret;
277 
278 	val |= PLL_FSM_ENA;
279 
280 	if (pll->flags & SUPPORTS_OFFLINE_REQ)
281 		val &= ~PLL_OFFLINE_REQ;
282 
283 	ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
284 	if (ret)
285 		return ret;
286 
287 	/* Make sure enable request goes through before waiting for update */
288 	mb();
289 
290 	return wait_for_pll_enable_active(pll);
291 }
292 
293 static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
294 {
295 	int ret;
296 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
297 	u32 val;
298 
299 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
300 	if (ret)
301 		return;
302 
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;
308 
309 		ret = wait_for_pll_offline(pll);
310 		if (ret)
311 			return;
312 	}
313 
314 	/* Disable hwfsm */
315 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
316 				 PLL_FSM_ENA, 0);
317 	if (ret)
318 		return;
319 
320 	wait_for_pll_disable(pll);
321 }
322 
323 static int pll_is_enabled(struct clk_hw *hw, u32 mask)
324 {
325 	int ret;
326 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
327 	u32 val;
328 
329 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
330 	if (ret)
331 		return ret;
332 
333 	return !!(val & mask);
334 }
335 
336 static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
337 {
338 	return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
339 }
340 
341 static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
342 {
343 	return pll_is_enabled(hw, PLL_LOCK_DET);
344 }
345 
346 static int clk_alpha_pll_enable(struct clk_hw *hw)
347 {
348 	int ret;
349 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
350 	u32 val, mask;
351 
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;
356 
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);
363 	}
364 
365 	/* Skip if already enabled */
366 	if ((val & mask) == mask)
367 		return 0;
368 
369 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
370 				 PLL_BYPASSNL, PLL_BYPASSNL);
371 	if (ret)
372 		return ret;
373 
374 	/*
375 	 * H/W requires a 5us delay between disabling the bypass and
376 	 * de-asserting the reset.
377 	 */
378 	mb();
379 	udelay(5);
380 
381 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
382 				 PLL_RESET_N, PLL_RESET_N);
383 	if (ret)
384 		return ret;
385 
386 	ret = wait_for_pll_enable_lock(pll);
387 	if (ret)
388 		return ret;
389 
390 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
391 				 PLL_OUTCTRL, PLL_OUTCTRL);
392 
393 	/* Ensure that the write above goes through before returning. */
394 	mb();
395 	return ret;
396 }
397 
398 static void clk_alpha_pll_disable(struct clk_hw *hw)
399 {
400 	int ret;
401 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
402 	u32 val, mask;
403 
404 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
405 	if (ret)
406 		return;
407 
408 	/* If in FSM mode, just unvote it */
409 	if (val & PLL_VOTE_FSM_ENA) {
410 		clk_disable_regmap(hw);
411 		return;
412 	}
413 
414 	mask = PLL_OUTCTRL;
415 	regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
416 
417 	/* Delay of 2 output clock ticks required until output is disabled */
418 	mb();
419 	udelay(1);
420 
421 	mask = PLL_RESET_N | PLL_BYPASSNL;
422 	regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
423 }
424 
425 static unsigned long
426 alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
427 {
428 	return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
429 }
430 
431 static unsigned long
432 alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
433 		     u32 alpha_width)
434 {
435 	u64 remainder;
436 	u64 quotient;
437 
438 	quotient = rate;
439 	remainder = do_div(quotient, prate);
440 	*l = quotient;
441 
442 	if (!remainder) {
443 		*a = 0;
444 		return rate;
445 	}
446 
447 	/* Upper ALPHA_BITWIDTH bits of Alpha */
448 	quotient = remainder << ALPHA_SHIFT(alpha_width);
449 
450 	remainder = do_div(quotient, prate);
451 
452 	if (remainder)
453 		quotient++;
454 
455 	*a = quotient;
456 	return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
457 }
458 
459 static const struct pll_vco *
460 alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
461 {
462 	const struct pll_vco *v = pll->vco_table;
463 	const struct pll_vco *end = v + pll->num_vco;
464 
465 	for (; v < end; v++)
466 		if (rate >= v->min_freq && rate <= v->max_freq)
467 			return v;
468 
469 	return NULL;
470 }
471 
472 static unsigned long
473 clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
474 {
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);
479 
480 	regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
481 
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);
491 		}
492 
493 		if (alpha_width > ALPHA_BITWIDTH)
494 			a >>= alpha_width - ALPHA_BITWIDTH;
495 	}
496 
497 	return alpha_pll_calc_rate(prate, l, a, alpha_width);
498 }
499 
500 
501 static int __clk_alpha_pll_update_latch(struct clk_alpha_pll *pll)
502 {
503 	int ret;
504 	u32 mode;
505 
506 	regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
507 
508 	/* Latch the input to the PLL */
509 	regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
510 			   PLL_UPDATE);
511 
512 	/* Wait for 2 reference cycle before checking ACK bit */
513 	udelay(1);
514 
515 	/*
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.
521 	 */
522 	if (mode & PLL_UPDATE_BYPASS) {
523 		ret = wait_for_pll_update_ack_set(pll);
524 		if (ret)
525 			return ret;
526 
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;
532 	}
533 
534 	ret = wait_for_pll_update_ack_clear(pll);
535 	if (ret)
536 		return ret;
537 
538 	/* Wait for PLL output to stabilize */
539 	udelay(10);
540 
541 	return 0;
542 }
543 
544 static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
545 				      int (*is_enabled)(struct clk_hw *))
546 {
547 	if (!is_enabled(&pll->clkr.hw) ||
548 	    !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
549 		return 0;
550 
551 	return __clk_alpha_pll_update_latch(pll);
552 }
553 
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 *))
557 {
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;
562 
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;
569 	}
570 
571 	regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
572 
573 	if (alpha_width > ALPHA_BITWIDTH)
574 		a <<= alpha_width - ALPHA_BITWIDTH;
575 
576 	if (alpha_width > 32)
577 		regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
578 
579 	regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
580 
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);
585 	}
586 
587 	regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
588 			   PLL_ALPHA_EN, PLL_ALPHA_EN);
589 
590 	return clk_alpha_pll_update_latch(pll, is_enabled);
591 }
592 
593 static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
594 				  unsigned long prate)
595 {
596 	return __clk_alpha_pll_set_rate(hw, rate, prate,
597 					clk_alpha_pll_is_enabled);
598 }
599 
600 static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
601 					unsigned long prate)
602 {
603 	return __clk_alpha_pll_set_rate(hw, rate, prate,
604 					clk_alpha_pll_hwfsm_is_enabled);
605 }
606 
607 static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
608 				     unsigned long *prate)
609 {
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;
614 
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;
618 
619 	min_freq = pll->vco_table[0].min_freq;
620 	max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
621 
622 	return clamp(rate, min_freq, max_freq);
623 }
624 
625 static unsigned long
626 alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
627 {
628 	/*
629 	 * a contains 16 bit alpha_val in two’s complement number in the range
630 	 * of [-0.5, 0.5).
631 	 */
632 	if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
633 		l -= 1;
634 
635 	return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
636 }
637 
638 static unsigned long
639 alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
640 			    u32 *l, u32 *a)
641 {
642 	u64 remainder;
643 	u64 quotient;
644 
645 	quotient = rate;
646 	remainder = do_div(quotient, prate);
647 	*l = quotient;
648 
649 	if (!remainder) {
650 		*a = 0;
651 		return rate;
652 	}
653 
654 	quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
655 	remainder = do_div(quotient, prate);
656 
657 	if (remainder)
658 		quotient++;
659 
660 	/*
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.
664 	 */
665 	if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
666 		*l += 1;
667 
668 	*a = quotient;
669 	return alpha_huayra_pll_calc_rate(prate, *l, *a);
670 }
671 
672 static unsigned long
673 alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
674 {
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;
678 
679 	regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
680 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
681 
682 	if (ctl & PLL_ALPHA_EN) {
683 		regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
684 		/*
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
688 		 *
689 		 *		Fout=FIN*(L+(M/N))
690 		 *
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.
693 		 *
694 		 * When alpha_mode=0, it is a two’s complement number in the
695 		 * range [-0.5, 0.5).
696 		 *
697 		 *		Fout=FIN*(L+(alpha_val)/2^16)
698 		 *
699 		 * where alpha_val is two’s complement number.
700 		 */
701 		if (!(ctl & PLL_ALPHA_MODE))
702 			return alpha_huayra_pll_calc_rate(rate, l, alpha);
703 
704 		alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
705 		alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
706 
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;
718 		}
719 
720 		return rate;
721 	}
722 
723 	return alpha_huayra_pll_calc_rate(rate, l, alpha);
724 }
725 
726 static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
727 				     unsigned long prate)
728 {
729 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
730 	u32 l, a, ctl, cur_alpha = 0;
731 
732 	rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
733 
734 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
735 
736 	if (ctl & PLL_ALPHA_EN)
737 		regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
738 
739 	/*
740 	 * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
741 	 * without having to go through the power on sequence.
742 	 */
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;
748 		}
749 
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);
754 	}
755 
756 	regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
757 	regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
758 
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);
765 
766 	return 0;
767 }
768 
769 static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
770 					unsigned long *prate)
771 {
772 	u32 l, a;
773 
774 	return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
775 }
776 
777 static int trion_pll_is_enabled(struct clk_alpha_pll *pll,
778 				struct regmap *regmap)
779 {
780 	u32 mode_regval, opmode_regval;
781 	int ret;
782 
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;
787 
788 	return ((opmode_regval & PLL_RUN) && (mode_regval & PLL_OUTCTRL));
789 }
790 
791 static int clk_trion_pll_is_enabled(struct clk_hw *hw)
792 {
793 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
794 
795 	return trion_pll_is_enabled(pll, pll->clkr.regmap);
796 }
797 
798 static int clk_trion_pll_enable(struct clk_hw *hw)
799 {
800 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
801 	struct regmap *regmap = pll->clkr.regmap;
802 	u32 val;
803 	int ret;
804 
805 	ret = regmap_read(regmap, PLL_MODE(pll), &val);
806 	if (ret)
807 		return ret;
808 
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);
815 	}
816 
817 	/* Set operation mode to RUN */
818 	regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
819 
820 	ret = wait_for_pll_enable_lock(pll);
821 	if (ret)
822 		return ret;
823 
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;
829 
830 	/* Enable the global PLL outputs */
831 	return regmap_update_bits(regmap, PLL_MODE(pll),
832 				 PLL_OUTCTRL, PLL_OUTCTRL);
833 }
834 
835 static void clk_trion_pll_disable(struct clk_hw *hw)
836 {
837 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
838 	struct regmap *regmap = pll->clkr.regmap;
839 	u32 val;
840 	int ret;
841 
842 	ret = regmap_read(regmap, PLL_MODE(pll), &val);
843 	if (ret)
844 		return;
845 
846 	/* If in FSM mode, just unvote it */
847 	if (val & PLL_VOTE_FSM_ENA) {
848 		clk_disable_regmap(hw);
849 		return;
850 	}
851 
852 	/* Disable the global PLL output */
853 	ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
854 	if (ret)
855 		return;
856 
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;
862 
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);
866 }
867 
868 static unsigned long
869 clk_trion_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
870 {
871 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
872 	u32 l, frac, alpha_width = pll_alpha_width(pll);
873 
874 	regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
875 	regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &frac);
876 
877 	return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
878 }
879 
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,
885 };
886 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_ops);
887 
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,
895 };
896 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
897 
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,
905 };
906 EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
907 
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,
915 };
916 EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
917 
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,
924 };
925 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_trion_ops);
926 
927 static unsigned long
928 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
929 {
930 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
931 	u32 ctl;
932 
933 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
934 
935 	ctl >>= PLL_POST_DIV_SHIFT;
936 	ctl &= PLL_POST_DIV_MASK(pll);
937 
938 	return parent_rate >> fls(ctl);
939 }
940 
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 },
947 	{ }
948 };
949 
950 static const struct clk_div_table clk_alpha_2bit_div_table[] = {
951 	{ 0x0, 1 },
952 	{ 0x1, 2 },
953 	{ 0x3, 4 },
954 	{ }
955 };
956 
957 static long
958 clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
959 				 unsigned long *prate)
960 {
961 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
962 	const struct clk_div_table *table;
963 
964 	if (pll->width == 2)
965 		table = clk_alpha_2bit_div_table;
966 	else
967 		table = clk_alpha_div_table;
968 
969 	return divider_round_rate(hw, rate, prate, table,
970 				  pll->width, CLK_DIVIDER_POWER_OF_TWO);
971 }
972 
973 static long
974 clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
975 				    unsigned long *prate)
976 {
977 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
978 	u32 ctl, div;
979 
980 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
981 
982 	ctl >>= PLL_POST_DIV_SHIFT;
983 	ctl &= BIT(pll->width) - 1;
984 	div = 1 << fls(ctl);
985 
986 	if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
987 		*prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
988 
989 	return DIV_ROUND_UP_ULL((u64)*prate, div);
990 }
991 
992 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
993 					  unsigned long parent_rate)
994 {
995 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
996 	int div;
997 
998 	/* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
999 	div = DIV_ROUND_UP_ULL(parent_rate, rate) - 1;
1000 
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);
1004 }
1005 
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,
1010 };
1011 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
1012 
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,
1016 };
1017 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);
1018 
1019 void clk_fabia_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1020 			     const struct alpha_pll_config *config)
1021 {
1022 	u32 val, mask;
1023 
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);
1038 
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);
1043 	}
1044 
1045 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
1046 							PLL_UPDATE_BYPASS);
1047 
1048 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1049 }
1050 EXPORT_SYMBOL_GPL(clk_fabia_pll_configure);
1051 
1052 static int alpha_pll_fabia_enable(struct clk_hw *hw)
1053 {
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;
1058 
1059 	ret = regmap_read(regmap, PLL_MODE(pll), &val);
1060 	if (ret)
1061 		return ret;
1062 
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);
1069 	}
1070 
1071 	ret = regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
1072 	if (ret)
1073 		return ret;
1074 
1075 	/* Skip If PLL is already running */
1076 	if ((opmode_val & PLL_RUN) && (val & PLL_OUTCTRL))
1077 		return 0;
1078 
1079 	ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1080 	if (ret)
1081 		return ret;
1082 
1083 	ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1084 	if (ret)
1085 		return ret;
1086 
1087 	ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N,
1088 				 PLL_RESET_N);
1089 	if (ret)
1090 		return ret;
1091 
1092 	ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
1093 	if (ret)
1094 		return ret;
1095 
1096 	ret = wait_for_pll_enable_lock(pll);
1097 	if (ret)
1098 		return ret;
1099 
1100 	ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
1101 				 PLL_OUT_MASK, PLL_OUT_MASK);
1102 	if (ret)
1103 		return ret;
1104 
1105 	return regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL,
1106 				 PLL_OUTCTRL);
1107 }
1108 
1109 static void alpha_pll_fabia_disable(struct clk_hw *hw)
1110 {
1111 	int ret;
1112 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1113 	u32 val;
1114 	struct regmap *regmap = pll->clkr.regmap;
1115 
1116 	ret = regmap_read(regmap, PLL_MODE(pll), &val);
1117 	if (ret)
1118 		return;
1119 
1120 	/* If in FSM mode, just unvote it */
1121 	if (val & PLL_FSM_ENA) {
1122 		clk_disable_regmap(hw);
1123 		return;
1124 	}
1125 
1126 	ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1127 	if (ret)
1128 		return;
1129 
1130 	/* Disable main outputs */
1131 	ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
1132 	if (ret)
1133 		return;
1134 
1135 	/* Place the PLL in STANDBY */
1136 	regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1137 }
1138 
1139 static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw *hw,
1140 						unsigned long parent_rate)
1141 {
1142 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1143 	u32 l, frac, alpha_width = pll_alpha_width(pll);
1144 
1145 	regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
1146 	regmap_read(pll->clkr.regmap, PLL_FRAC(pll), &frac);
1147 
1148 	return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
1149 }
1150 
1151 /*
1152  * Due to limited number of bits for fractional rate programming, the
1153  * rounded up rate could be marginally higher than the requested rate.
1154  */
1155 static int alpha_pll_check_rate_margin(struct clk_hw *hw,
1156 			unsigned long rrate, unsigned long rate)
1157 {
1158 	unsigned long rate_margin = rate + PLL_RATE_MARGIN;
1159 
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;
1164 	}
1165 
1166 	return 0;
1167 }
1168 
1169 static int alpha_pll_fabia_set_rate(struct clk_hw *hw, unsigned long rate,
1170 						unsigned long prate)
1171 {
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;
1177 
1178 	rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1179 
1180 	ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1181 	if (ret < 0)
1182 		return ret;
1183 
1184 	regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1185 	regmap_write(pll->clkr.regmap, PLL_FRAC(pll), a);
1186 
1187 	return __clk_alpha_pll_update_latch(pll);
1188 }
1189 
1190 static int alpha_pll_fabia_prepare(struct clk_hw *hw)
1191 {
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;
1200 
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;
1205 
1206 	/* Return early if calibration is not needed. */
1207 	if (val & PLL_RESET_N)
1208 		return 0;
1209 
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;
1214 	}
1215 
1216 	cal_freq = DIV_ROUND_CLOSEST((pll->vco_table[0].min_freq +
1217 				pll->vco_table[0].max_freq) * 54, 100);
1218 
1219 	parent_hw = clk_hw_get_parent(hw);
1220 	if (!parent_hw)
1221 		return -EINVAL;
1222 
1223 	rrate = alpha_pll_round_rate(cal_freq, clk_hw_get_rate(parent_hw),
1224 					&cal_l, &a, alpha_width);
1225 
1226 	ret = alpha_pll_check_rate_margin(hw, rrate, cal_freq);
1227 	if (ret < 0)
1228 		return ret;
1229 
1230 	/* Setup PLL for calibration frequency */
1231 	regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), cal_l);
1232 
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;
1238 	}
1239 
1240 	clk_alpha_pll_disable(hw);
1241 
1242 	return 0;
1243 }
1244 
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,
1253 };
1254 EXPORT_SYMBOL_GPL(clk_alpha_pll_fabia_ops);
1255 
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,
1262 };
1263 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_fabia_ops);
1264 
1265 static unsigned long clk_alpha_pll_postdiv_fabia_recalc_rate(struct clk_hw *hw,
1266 					unsigned long parent_rate)
1267 {
1268 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1269 	u32 i, div = 1, val;
1270 	int ret;
1271 
1272 	ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
1273 	if (ret)
1274 		return ret;
1275 
1276 	val >>= pll->post_div_shift;
1277 	val &= BIT(pll->width) - 1;
1278 
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;
1283 		}
1284 	}
1285 
1286 	return (parent_rate / div);
1287 }
1288 
1289 static unsigned long
1290 clk_trion_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
1291 {
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;
1295 
1296 	regmap_read(regmap, PLL_USER_CTL(pll), &val);
1297 
1298 	val >>= pll->post_div_shift;
1299 	val &= PLL_POST_DIV_MASK(pll);
1300 
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;
1305 		}
1306 	}
1307 
1308 	return (parent_rate / div);
1309 }
1310 
1311 static long
1312 clk_trion_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
1313 				 unsigned long *prate)
1314 {
1315 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1316 
1317 	return divider_round_rate(hw, rate, prate, pll->post_div_table,
1318 				  pll->width, CLK_DIVIDER_ROUND_CLOSEST);
1319 };
1320 
1321 static int
1322 clk_trion_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
1323 			       unsigned long parent_rate)
1324 {
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;
1328 
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;
1334 		}
1335 	}
1336 
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);
1340 }
1341 
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,
1346 };
1347 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_trion_ops);
1348 
1349 static long clk_alpha_pll_postdiv_fabia_round_rate(struct clk_hw *hw,
1350 				unsigned long rate, unsigned long *prate)
1351 {
1352 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1353 
1354 	return divider_round_rate(hw, rate, prate, pll->post_div_table,
1355 				pll->width, CLK_DIVIDER_ROUND_CLOSEST);
1356 }
1357 
1358 static int clk_alpha_pll_postdiv_fabia_set_rate(struct clk_hw *hw,
1359 				unsigned long rate, unsigned long parent_rate)
1360 {
1361 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1362 	int i, val = 0, div, ret;
1363 
1364 	/*
1365 	 * If the PLL is in FSM mode, then treat set_rate callback as a
1366 	 * no-operation.
1367 	 */
1368 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1369 	if (ret)
1370 		return ret;
1371 
1372 	if (val & PLL_VOTE_FSM_ENA)
1373 		return 0;
1374 
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;
1380 		}
1381 	}
1382 
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);
1386 }
1387 
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,
1392 };
1393 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_fabia_ops);
1394 
1395 /**
1396  * clk_lucid_pll_configure - configure the lucid pll
1397  *
1398  * @pll: clk alpha pll
1399  * @regmap: register map
1400  * @config: configuration to apply for pll
1401  */
1402 void clk_trion_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1403 			     const struct alpha_pll_config *config)
1404 {
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);
1426 
1427 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
1428 			   PLL_UPDATE_BYPASS);
1429 
1430 	/* Disable PLL output */
1431 	regmap_update_bits(regmap, PLL_MODE(pll),  PLL_OUTCTRL, 0);
1432 
1433 	/* Set operation mode to OFF */
1434 	regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1435 
1436 	/* Place the PLL in STANDBY mode */
1437 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1438 }
1439 EXPORT_SYMBOL_GPL(clk_trion_pll_configure);
1440 
1441 /*
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.
1444  */
1445 static int __alpha_pll_trion_prepare(struct clk_hw *hw, u32 pcal_done)
1446 {
1447 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1448 	u32 regval;
1449 	int ret;
1450 
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;
1455 
1456 	/* On/off to calibrate */
1457 	ret = clk_trion_pll_enable(hw);
1458 	if (!ret)
1459 		clk_trion_pll_disable(hw);
1460 
1461 	return ret;
1462 }
1463 
1464 static int alpha_pll_trion_prepare(struct clk_hw *hw)
1465 {
1466 	return __alpha_pll_trion_prepare(hw, TRION_PCAL_DONE);
1467 }
1468 
1469 static int alpha_pll_lucid_prepare(struct clk_hw *hw)
1470 {
1471 	return __alpha_pll_trion_prepare(hw, LUCID_PCAL_DONE);
1472 }
1473 
1474 static int alpha_pll_trion_set_rate(struct clk_hw *hw, unsigned long rate,
1475 				    unsigned long prate)
1476 {
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;
1482 
1483 	rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1484 
1485 	ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1486 	if (ret < 0)
1487 		return ret;
1488 
1489 	regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1490 	regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
1491 
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;
1497 
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;
1504 	}
1505 
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;
1511 
1512 	if (clk_hw_is_enabled(hw)) {
1513 		ret = wait_for_pll_enable_lock(pll);
1514 		if (ret)
1515 			return ret;
1516 	}
1517 
1518 	/* Wait for PLL output to stabilize */
1519 	udelay(100);
1520 	return 0;
1521 }
1522 
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,
1531 };
1532 EXPORT_SYMBOL_GPL(clk_alpha_pll_trion_ops);
1533 
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,
1542 };
1543 EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_ops);
1544 
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,
1549 };
1550 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_ops);
1551 
1552 void clk_agera_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1553 			const struct alpha_pll_config *config)
1554 {
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);
1567 }
1568 EXPORT_SYMBOL_GPL(clk_agera_pll_configure);
1569 
1570 static int clk_alpha_pll_agera_set_rate(struct clk_hw *hw, unsigned long rate,
1571 							unsigned long prate)
1572 {
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;
1578 
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;
1583 
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);
1587 
1588 	if (clk_hw_is_enabled(hw))
1589 		return wait_for_pll_enable_lock(pll);
1590 
1591 	return 0;
1592 }
1593 
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,
1601 };
1602 EXPORT_SYMBOL_GPL(clk_alpha_pll_agera_ops);
1603