xref: /openbmc/linux/drivers/clk/qcom/clk-alpha-pll.c (revision 15e3ae36)
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 #define PLL_CAL_VAL(p)		((p)->offset + (p)->regs[PLL_OFF_CAL_VAL])
60 
61 const u8 clk_alpha_pll_regs[][PLL_OFF_MAX_REGS] = {
62 	[CLK_ALPHA_PLL_TYPE_DEFAULT] =  {
63 		[PLL_OFF_L_VAL] = 0x04,
64 		[PLL_OFF_ALPHA_VAL] = 0x08,
65 		[PLL_OFF_ALPHA_VAL_U] = 0x0c,
66 		[PLL_OFF_USER_CTL] = 0x10,
67 		[PLL_OFF_USER_CTL_U] = 0x14,
68 		[PLL_OFF_CONFIG_CTL] = 0x18,
69 		[PLL_OFF_TEST_CTL] = 0x1c,
70 		[PLL_OFF_TEST_CTL_U] = 0x20,
71 		[PLL_OFF_STATUS] = 0x24,
72 	},
73 	[CLK_ALPHA_PLL_TYPE_HUAYRA] =  {
74 		[PLL_OFF_L_VAL] = 0x04,
75 		[PLL_OFF_ALPHA_VAL] = 0x08,
76 		[PLL_OFF_USER_CTL] = 0x10,
77 		[PLL_OFF_CONFIG_CTL] = 0x14,
78 		[PLL_OFF_CONFIG_CTL_U] = 0x18,
79 		[PLL_OFF_TEST_CTL] = 0x1c,
80 		[PLL_OFF_TEST_CTL_U] = 0x20,
81 		[PLL_OFF_STATUS] = 0x24,
82 	},
83 	[CLK_ALPHA_PLL_TYPE_BRAMMO] =  {
84 		[PLL_OFF_L_VAL] = 0x04,
85 		[PLL_OFF_ALPHA_VAL] = 0x08,
86 		[PLL_OFF_ALPHA_VAL_U] = 0x0c,
87 		[PLL_OFF_USER_CTL] = 0x10,
88 		[PLL_OFF_CONFIG_CTL] = 0x18,
89 		[PLL_OFF_TEST_CTL] = 0x1c,
90 		[PLL_OFF_STATUS] = 0x24,
91 	},
92 	[CLK_ALPHA_PLL_TYPE_FABIA] =  {
93 		[PLL_OFF_L_VAL] = 0x04,
94 		[PLL_OFF_USER_CTL] = 0x0c,
95 		[PLL_OFF_USER_CTL_U] = 0x10,
96 		[PLL_OFF_CONFIG_CTL] = 0x14,
97 		[PLL_OFF_CONFIG_CTL_U] = 0x18,
98 		[PLL_OFF_TEST_CTL] = 0x1c,
99 		[PLL_OFF_TEST_CTL_U] = 0x20,
100 		[PLL_OFF_STATUS] = 0x24,
101 		[PLL_OFF_OPMODE] = 0x2c,
102 		[PLL_OFF_FRAC] = 0x38,
103 	},
104 	[CLK_ALPHA_PLL_TYPE_TRION] = {
105 		[PLL_OFF_L_VAL] = 0x04,
106 		[PLL_OFF_CAL_L_VAL] = 0x08,
107 		[PLL_OFF_USER_CTL] = 0x0c,
108 		[PLL_OFF_USER_CTL_U] = 0x10,
109 		[PLL_OFF_USER_CTL_U1] = 0x14,
110 		[PLL_OFF_CONFIG_CTL] = 0x18,
111 		[PLL_OFF_CONFIG_CTL_U] = 0x1c,
112 		[PLL_OFF_CONFIG_CTL_U1] = 0x20,
113 		[PLL_OFF_TEST_CTL] = 0x24,
114 		[PLL_OFF_TEST_CTL_U] = 0x28,
115 		[PLL_OFF_STATUS] = 0x30,
116 		[PLL_OFF_OPMODE] = 0x38,
117 		[PLL_OFF_ALPHA_VAL] = 0x40,
118 		[PLL_OFF_CAL_VAL] = 0x44,
119 	},
120 	[CLK_ALPHA_PLL_TYPE_LUCID] =  {
121 		[PLL_OFF_L_VAL] = 0x04,
122 		[PLL_OFF_CAL_L_VAL] = 0x08,
123 		[PLL_OFF_USER_CTL] = 0x0c,
124 		[PLL_OFF_USER_CTL_U] = 0x10,
125 		[PLL_OFF_USER_CTL_U1] = 0x14,
126 		[PLL_OFF_CONFIG_CTL] = 0x18,
127 		[PLL_OFF_CONFIG_CTL_U] = 0x1c,
128 		[PLL_OFF_CONFIG_CTL_U1] = 0x20,
129 		[PLL_OFF_TEST_CTL] = 0x24,
130 		[PLL_OFF_TEST_CTL_U] = 0x28,
131 		[PLL_OFF_TEST_CTL_U1] = 0x2c,
132 		[PLL_OFF_STATUS] = 0x30,
133 		[PLL_OFF_OPMODE] = 0x38,
134 		[PLL_OFF_ALPHA_VAL] = 0x40,
135 	},
136 };
137 EXPORT_SYMBOL_GPL(clk_alpha_pll_regs);
138 
139 /*
140  * Even though 40 bits are present, use only 32 for ease of calculation.
141  */
142 #define ALPHA_REG_BITWIDTH	40
143 #define ALPHA_REG_16BIT_WIDTH	16
144 #define ALPHA_BITWIDTH		32U
145 #define ALPHA_SHIFT(w)		min(w, ALPHA_BITWIDTH)
146 
147 #define PLL_HUAYRA_M_WIDTH		8
148 #define PLL_HUAYRA_M_SHIFT		8
149 #define PLL_HUAYRA_M_MASK		0xff
150 #define PLL_HUAYRA_N_SHIFT		0
151 #define PLL_HUAYRA_N_MASK		0xff
152 #define PLL_HUAYRA_ALPHA_WIDTH		16
153 
154 #define PLL_STANDBY		0x0
155 #define PLL_RUN			0x1
156 #define PLL_OUT_MASK		0x7
157 #define PLL_RATE_MARGIN		500
158 
159 /* LUCID PLL specific settings and offsets */
160 #define LUCID_PLL_CAL_VAL	0x44
161 #define LUCID_PCAL_DONE		BIT(26)
162 
163 #define pll_alpha_width(p)					\
164 		((PLL_ALPHA_VAL_U(p) - PLL_ALPHA_VAL(p) == 4) ?	\
165 				 ALPHA_REG_BITWIDTH : ALPHA_REG_16BIT_WIDTH)
166 
167 #define pll_has_64bit_config(p)	((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
168 
169 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
170 					   struct clk_alpha_pll, clkr)
171 
172 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
173 					   struct clk_alpha_pll_postdiv, clkr)
174 
175 static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
176 			const char *action)
177 {
178 	u32 val;
179 	int count;
180 	int ret;
181 	const char *name = clk_hw_get_name(&pll->clkr.hw);
182 
183 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
184 	if (ret)
185 		return ret;
186 
187 	for (count = 100; count > 0; count--) {
188 		ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
189 		if (ret)
190 			return ret;
191 		if (inverse && !(val & mask))
192 			return 0;
193 		else if ((val & mask) == mask)
194 			return 0;
195 
196 		udelay(1);
197 	}
198 
199 	WARN(1, "%s failed to %s!\n", name, action);
200 	return -ETIMEDOUT;
201 }
202 
203 #define wait_for_pll_enable_active(pll) \
204 	wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
205 
206 #define wait_for_pll_enable_lock(pll) \
207 	wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
208 
209 #define wait_for_pll_disable(pll) \
210 	wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
211 
212 #define wait_for_pll_offline(pll) \
213 	wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
214 
215 #define wait_for_pll_update(pll) \
216 	wait_for_pll(pll, PLL_UPDATE, 1, "update")
217 
218 #define wait_for_pll_update_ack_set(pll) \
219 	wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
220 
221 #define wait_for_pll_update_ack_clear(pll) \
222 	wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
223 
224 void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
225 			     const struct alpha_pll_config *config)
226 {
227 	u32 val, mask;
228 
229 	regmap_write(regmap, PLL_L_VAL(pll), config->l);
230 	regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
231 	regmap_write(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
232 
233 	if (pll_has_64bit_config(pll))
234 		regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
235 			     config->config_ctl_hi_val);
236 
237 	if (pll_alpha_width(pll) > 32)
238 		regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
239 
240 	val = config->main_output_mask;
241 	val |= config->aux_output_mask;
242 	val |= config->aux2_output_mask;
243 	val |= config->early_output_mask;
244 	val |= config->pre_div_val;
245 	val |= config->post_div_val;
246 	val |= config->vco_val;
247 	val |= config->alpha_en_mask;
248 	val |= config->alpha_mode_mask;
249 
250 	mask = config->main_output_mask;
251 	mask |= config->aux_output_mask;
252 	mask |= config->aux2_output_mask;
253 	mask |= config->early_output_mask;
254 	mask |= config->pre_div_mask;
255 	mask |= config->post_div_mask;
256 	mask |= config->vco_mask;
257 
258 	regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
259 
260 	if (pll->flags & SUPPORTS_FSM_MODE)
261 		qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
262 }
263 EXPORT_SYMBOL_GPL(clk_alpha_pll_configure);
264 
265 static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
266 {
267 	int ret;
268 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
269 	u32 val;
270 
271 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
272 	if (ret)
273 		return ret;
274 
275 	val |= PLL_FSM_ENA;
276 
277 	if (pll->flags & SUPPORTS_OFFLINE_REQ)
278 		val &= ~PLL_OFFLINE_REQ;
279 
280 	ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
281 	if (ret)
282 		return ret;
283 
284 	/* Make sure enable request goes through before waiting for update */
285 	mb();
286 
287 	return wait_for_pll_enable_active(pll);
288 }
289 
290 static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
291 {
292 	int ret;
293 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
294 	u32 val;
295 
296 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
297 	if (ret)
298 		return;
299 
300 	if (pll->flags & SUPPORTS_OFFLINE_REQ) {
301 		ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
302 					 PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
303 		if (ret)
304 			return;
305 
306 		ret = wait_for_pll_offline(pll);
307 		if (ret)
308 			return;
309 	}
310 
311 	/* Disable hwfsm */
312 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
313 				 PLL_FSM_ENA, 0);
314 	if (ret)
315 		return;
316 
317 	wait_for_pll_disable(pll);
318 }
319 
320 static int pll_is_enabled(struct clk_hw *hw, u32 mask)
321 {
322 	int ret;
323 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
324 	u32 val;
325 
326 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
327 	if (ret)
328 		return ret;
329 
330 	return !!(val & mask);
331 }
332 
333 static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
334 {
335 	return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
336 }
337 
338 static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
339 {
340 	return pll_is_enabled(hw, PLL_LOCK_DET);
341 }
342 
343 static int clk_alpha_pll_enable(struct clk_hw *hw)
344 {
345 	int ret;
346 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
347 	u32 val, mask;
348 
349 	mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
350 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
351 	if (ret)
352 		return ret;
353 
354 	/* If in FSM mode, just vote for it */
355 	if (val & PLL_VOTE_FSM_ENA) {
356 		ret = clk_enable_regmap(hw);
357 		if (ret)
358 			return ret;
359 		return wait_for_pll_enable_active(pll);
360 	}
361 
362 	/* Skip if already enabled */
363 	if ((val & mask) == mask)
364 		return 0;
365 
366 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
367 				 PLL_BYPASSNL, PLL_BYPASSNL);
368 	if (ret)
369 		return ret;
370 
371 	/*
372 	 * H/W requires a 5us delay between disabling the bypass and
373 	 * de-asserting the reset.
374 	 */
375 	mb();
376 	udelay(5);
377 
378 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
379 				 PLL_RESET_N, PLL_RESET_N);
380 	if (ret)
381 		return ret;
382 
383 	ret = wait_for_pll_enable_lock(pll);
384 	if (ret)
385 		return ret;
386 
387 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
388 				 PLL_OUTCTRL, PLL_OUTCTRL);
389 
390 	/* Ensure that the write above goes through before returning. */
391 	mb();
392 	return ret;
393 }
394 
395 static void clk_alpha_pll_disable(struct clk_hw *hw)
396 {
397 	int ret;
398 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
399 	u32 val, mask;
400 
401 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
402 	if (ret)
403 		return;
404 
405 	/* If in FSM mode, just unvote it */
406 	if (val & PLL_VOTE_FSM_ENA) {
407 		clk_disable_regmap(hw);
408 		return;
409 	}
410 
411 	mask = PLL_OUTCTRL;
412 	regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
413 
414 	/* Delay of 2 output clock ticks required until output is disabled */
415 	mb();
416 	udelay(1);
417 
418 	mask = PLL_RESET_N | PLL_BYPASSNL;
419 	regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
420 }
421 
422 static unsigned long
423 alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
424 {
425 	return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
426 }
427 
428 static unsigned long
429 alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
430 		     u32 alpha_width)
431 {
432 	u64 remainder;
433 	u64 quotient;
434 
435 	quotient = rate;
436 	remainder = do_div(quotient, prate);
437 	*l = quotient;
438 
439 	if (!remainder) {
440 		*a = 0;
441 		return rate;
442 	}
443 
444 	/* Upper ALPHA_BITWIDTH bits of Alpha */
445 	quotient = remainder << ALPHA_SHIFT(alpha_width);
446 
447 	remainder = do_div(quotient, prate);
448 
449 	if (remainder)
450 		quotient++;
451 
452 	*a = quotient;
453 	return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
454 }
455 
456 static const struct pll_vco *
457 alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
458 {
459 	const struct pll_vco *v = pll->vco_table;
460 	const struct pll_vco *end = v + pll->num_vco;
461 
462 	for (; v < end; v++)
463 		if (rate >= v->min_freq && rate <= v->max_freq)
464 			return v;
465 
466 	return NULL;
467 }
468 
469 static unsigned long
470 clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
471 {
472 	u32 l, low, high, ctl;
473 	u64 a = 0, prate = parent_rate;
474 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
475 	u32 alpha_width = pll_alpha_width(pll);
476 
477 	regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
478 
479 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
480 	if (ctl & PLL_ALPHA_EN) {
481 		regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &low);
482 		if (alpha_width > 32) {
483 			regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
484 				    &high);
485 			a = (u64)high << 32 | low;
486 		} else {
487 			a = low & GENMASK(alpha_width - 1, 0);
488 		}
489 
490 		if (alpha_width > ALPHA_BITWIDTH)
491 			a >>= alpha_width - ALPHA_BITWIDTH;
492 	}
493 
494 	return alpha_pll_calc_rate(prate, l, a, alpha_width);
495 }
496 
497 
498 static int __clk_alpha_pll_update_latch(struct clk_alpha_pll *pll)
499 {
500 	int ret;
501 	u32 mode;
502 
503 	regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
504 
505 	/* Latch the input to the PLL */
506 	regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
507 			   PLL_UPDATE);
508 
509 	/* Wait for 2 reference cycle before checking ACK bit */
510 	udelay(1);
511 
512 	/*
513 	 * PLL will latch the new L, Alpha and freq control word.
514 	 * PLL will respond by raising PLL_ACK_LATCH output when new programming
515 	 * has been latched in and PLL is being updated. When
516 	 * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
517 	 * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
518 	 */
519 	if (mode & PLL_UPDATE_BYPASS) {
520 		ret = wait_for_pll_update_ack_set(pll);
521 		if (ret)
522 			return ret;
523 
524 		regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE, 0);
525 	} else {
526 		ret = wait_for_pll_update(pll);
527 		if (ret)
528 			return ret;
529 	}
530 
531 	ret = wait_for_pll_update_ack_clear(pll);
532 	if (ret)
533 		return ret;
534 
535 	/* Wait for PLL output to stabilize */
536 	udelay(10);
537 
538 	return 0;
539 }
540 
541 static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
542 				      int (*is_enabled)(struct clk_hw *))
543 {
544 	if (!is_enabled(&pll->clkr.hw) ||
545 	    !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
546 		return 0;
547 
548 	return __clk_alpha_pll_update_latch(pll);
549 }
550 
551 static int __clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
552 				    unsigned long prate,
553 				    int (*is_enabled)(struct clk_hw *))
554 {
555 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
556 	const struct pll_vco *vco;
557 	u32 l, alpha_width = pll_alpha_width(pll);
558 	u64 a;
559 
560 	rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
561 	vco = alpha_pll_find_vco(pll, rate);
562 	if (pll->vco_table && !vco) {
563 		pr_err("%s: alpha pll not in a valid vco range\n",
564 		       clk_hw_get_name(hw));
565 		return -EINVAL;
566 	}
567 
568 	regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
569 
570 	if (alpha_width > ALPHA_BITWIDTH)
571 		a <<= alpha_width - ALPHA_BITWIDTH;
572 
573 	if (alpha_width > 32)
574 		regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
575 
576 	regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
577 
578 	if (vco) {
579 		regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
580 				   PLL_VCO_MASK << PLL_VCO_SHIFT,
581 				   vco->val << PLL_VCO_SHIFT);
582 	}
583 
584 	regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
585 			   PLL_ALPHA_EN, PLL_ALPHA_EN);
586 
587 	return clk_alpha_pll_update_latch(pll, is_enabled);
588 }
589 
590 static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
591 				  unsigned long prate)
592 {
593 	return __clk_alpha_pll_set_rate(hw, rate, prate,
594 					clk_alpha_pll_is_enabled);
595 }
596 
597 static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
598 					unsigned long prate)
599 {
600 	return __clk_alpha_pll_set_rate(hw, rate, prate,
601 					clk_alpha_pll_hwfsm_is_enabled);
602 }
603 
604 static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
605 				     unsigned long *prate)
606 {
607 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
608 	u32 l, alpha_width = pll_alpha_width(pll);
609 	u64 a;
610 	unsigned long min_freq, max_freq;
611 
612 	rate = alpha_pll_round_rate(rate, *prate, &l, &a, alpha_width);
613 	if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
614 		return rate;
615 
616 	min_freq = pll->vco_table[0].min_freq;
617 	max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
618 
619 	return clamp(rate, min_freq, max_freq);
620 }
621 
622 static unsigned long
623 alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
624 {
625 	/*
626 	 * a contains 16 bit alpha_val in two’s compliment number in the range
627 	 * of [-0.5, 0.5).
628 	 */
629 	if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
630 		l -= 1;
631 
632 	return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
633 }
634 
635 static unsigned long
636 alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
637 			    u32 *l, u32 *a)
638 {
639 	u64 remainder;
640 	u64 quotient;
641 
642 	quotient = rate;
643 	remainder = do_div(quotient, prate);
644 	*l = quotient;
645 
646 	if (!remainder) {
647 		*a = 0;
648 		return rate;
649 	}
650 
651 	quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
652 	remainder = do_div(quotient, prate);
653 
654 	if (remainder)
655 		quotient++;
656 
657 	/*
658 	 * alpha_val should be in two’s compliment number in the range
659 	 * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
660 	 * since alpha value will be subtracted in this case.
661 	 */
662 	if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
663 		*l += 1;
664 
665 	*a = quotient;
666 	return alpha_huayra_pll_calc_rate(prate, *l, *a);
667 }
668 
669 static unsigned long
670 alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
671 {
672 	u64 rate = parent_rate, tmp;
673 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
674 	u32 l, alpha = 0, ctl, alpha_m, alpha_n;
675 
676 	regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
677 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
678 
679 	if (ctl & PLL_ALPHA_EN) {
680 		regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
681 		/*
682 		 * Depending upon alpha_mode, it can be treated as M/N value or
683 		 * as a two’s compliment number. When alpha_mode=1,
684 		 * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
685 		 *
686 		 *		Fout=FIN*(L+(M/N))
687 		 *
688 		 * M is a signed number (-128 to 127) and N is unsigned
689 		 * (0 to 255). M/N has to be within +/-0.5.
690 		 *
691 		 * When alpha_mode=0, it is a two’s compliment number in the
692 		 * range [-0.5, 0.5).
693 		 *
694 		 *		Fout=FIN*(L+(alpha_val)/2^16)
695 		 *
696 		 * where alpha_val is two’s compliment number.
697 		 */
698 		if (!(ctl & PLL_ALPHA_MODE))
699 			return alpha_huayra_pll_calc_rate(rate, l, alpha);
700 
701 		alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
702 		alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
703 
704 		rate *= l;
705 		tmp = parent_rate;
706 		if (alpha_m >= BIT(PLL_HUAYRA_M_WIDTH - 1)) {
707 			alpha_m = BIT(PLL_HUAYRA_M_WIDTH) - alpha_m;
708 			tmp *= alpha_m;
709 			do_div(tmp, alpha_n);
710 			rate -= tmp;
711 		} else {
712 			tmp *= alpha_m;
713 			do_div(tmp, alpha_n);
714 			rate += tmp;
715 		}
716 
717 		return rate;
718 	}
719 
720 	return alpha_huayra_pll_calc_rate(rate, l, alpha);
721 }
722 
723 static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
724 				     unsigned long prate)
725 {
726 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
727 	u32 l, a, ctl, cur_alpha = 0;
728 
729 	rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
730 
731 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
732 
733 	if (ctl & PLL_ALPHA_EN)
734 		regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
735 
736 	/*
737 	 * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
738 	 * without having to go through the power on sequence.
739 	 */
740 	if (clk_alpha_pll_is_enabled(hw)) {
741 		if (cur_alpha != a) {
742 			pr_err("%s: clock needs to be gated\n",
743 			       clk_hw_get_name(hw));
744 			return -EBUSY;
745 		}
746 
747 		regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
748 		/* Ensure that the write above goes to detect L val change. */
749 		mb();
750 		return wait_for_pll_enable_lock(pll);
751 	}
752 
753 	regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
754 	regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
755 
756 	if (a == 0)
757 		regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
758 				   PLL_ALPHA_EN, 0x0);
759 	else
760 		regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
761 				   PLL_ALPHA_EN | PLL_ALPHA_MODE, PLL_ALPHA_EN);
762 
763 	return 0;
764 }
765 
766 static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
767 					unsigned long *prate)
768 {
769 	u32 l, a;
770 
771 	return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
772 }
773 
774 static int trion_pll_is_enabled(struct clk_alpha_pll *pll,
775 				struct regmap *regmap)
776 {
777 	u32 mode_regval, opmode_regval;
778 	int ret;
779 
780 	ret = regmap_read(regmap, PLL_MODE(pll), &mode_regval);
781 	ret |= regmap_read(regmap, PLL_OPMODE(pll), &opmode_regval);
782 	if (ret)
783 		return 0;
784 
785 	return ((opmode_regval & PLL_RUN) && (mode_regval & PLL_OUTCTRL));
786 }
787 
788 static int clk_trion_pll_is_enabled(struct clk_hw *hw)
789 {
790 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
791 
792 	return trion_pll_is_enabled(pll, pll->clkr.regmap);
793 }
794 
795 static int clk_trion_pll_enable(struct clk_hw *hw)
796 {
797 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
798 	struct regmap *regmap = pll->clkr.regmap;
799 	u32 val;
800 	int ret;
801 
802 	ret = regmap_read(regmap, PLL_MODE(pll), &val);
803 	if (ret)
804 		return ret;
805 
806 	/* If in FSM mode, just vote for it */
807 	if (val & PLL_VOTE_FSM_ENA) {
808 		ret = clk_enable_regmap(hw);
809 		if (ret)
810 			return ret;
811 		return wait_for_pll_enable_active(pll);
812 	}
813 
814 	/* Set operation mode to RUN */
815 	regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
816 
817 	ret = wait_for_pll_enable_lock(pll);
818 	if (ret)
819 		return ret;
820 
821 	/* Enable the PLL outputs */
822 	ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
823 				 PLL_OUT_MASK, PLL_OUT_MASK);
824 	if (ret)
825 		return ret;
826 
827 	/* Enable the global PLL outputs */
828 	return regmap_update_bits(regmap, PLL_MODE(pll),
829 				 PLL_OUTCTRL, PLL_OUTCTRL);
830 }
831 
832 static void clk_trion_pll_disable(struct clk_hw *hw)
833 {
834 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
835 	struct regmap *regmap = pll->clkr.regmap;
836 	u32 val;
837 	int ret;
838 
839 	ret = regmap_read(regmap, PLL_MODE(pll), &val);
840 	if (ret)
841 		return;
842 
843 	/* If in FSM mode, just unvote it */
844 	if (val & PLL_VOTE_FSM_ENA) {
845 		clk_disable_regmap(hw);
846 		return;
847 	}
848 
849 	/* Disable the global PLL output */
850 	ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
851 	if (ret)
852 		return;
853 
854 	/* Disable the PLL outputs */
855 	ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
856 				 PLL_OUT_MASK, 0);
857 	if (ret)
858 		return;
859 
860 	/* Place the PLL mode in STANDBY */
861 	regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
862 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
863 }
864 
865 static unsigned long
866 clk_trion_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
867 {
868 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
869 	u32 l, frac, alpha_width = pll_alpha_width(pll);
870 
871 	regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
872 	regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &frac);
873 
874 	return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
875 }
876 
877 const struct clk_ops clk_alpha_pll_fixed_ops = {
878 	.enable = clk_alpha_pll_enable,
879 	.disable = clk_alpha_pll_disable,
880 	.is_enabled = clk_alpha_pll_is_enabled,
881 	.recalc_rate = clk_alpha_pll_recalc_rate,
882 };
883 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_ops);
884 
885 const struct clk_ops clk_alpha_pll_ops = {
886 	.enable = clk_alpha_pll_enable,
887 	.disable = clk_alpha_pll_disable,
888 	.is_enabled = clk_alpha_pll_is_enabled,
889 	.recalc_rate = clk_alpha_pll_recalc_rate,
890 	.round_rate = clk_alpha_pll_round_rate,
891 	.set_rate = clk_alpha_pll_set_rate,
892 };
893 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
894 
895 const struct clk_ops clk_alpha_pll_huayra_ops = {
896 	.enable = clk_alpha_pll_enable,
897 	.disable = clk_alpha_pll_disable,
898 	.is_enabled = clk_alpha_pll_is_enabled,
899 	.recalc_rate = alpha_pll_huayra_recalc_rate,
900 	.round_rate = alpha_pll_huayra_round_rate,
901 	.set_rate = alpha_pll_huayra_set_rate,
902 };
903 EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
904 
905 const struct clk_ops clk_alpha_pll_hwfsm_ops = {
906 	.enable = clk_alpha_pll_hwfsm_enable,
907 	.disable = clk_alpha_pll_hwfsm_disable,
908 	.is_enabled = clk_alpha_pll_hwfsm_is_enabled,
909 	.recalc_rate = clk_alpha_pll_recalc_rate,
910 	.round_rate = clk_alpha_pll_round_rate,
911 	.set_rate = clk_alpha_pll_hwfsm_set_rate,
912 };
913 EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
914 
915 const struct clk_ops clk_trion_fixed_pll_ops = {
916 	.enable = clk_trion_pll_enable,
917 	.disable = clk_trion_pll_disable,
918 	.is_enabled = clk_trion_pll_is_enabled,
919 	.recalc_rate = clk_trion_pll_recalc_rate,
920 	.round_rate = clk_alpha_pll_round_rate,
921 };
922 EXPORT_SYMBOL_GPL(clk_trion_fixed_pll_ops);
923 
924 static unsigned long
925 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
926 {
927 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
928 	u32 ctl;
929 
930 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
931 
932 	ctl >>= PLL_POST_DIV_SHIFT;
933 	ctl &= PLL_POST_DIV_MASK(pll);
934 
935 	return parent_rate >> fls(ctl);
936 }
937 
938 static const struct clk_div_table clk_alpha_div_table[] = {
939 	{ 0x0, 1 },
940 	{ 0x1, 2 },
941 	{ 0x3, 4 },
942 	{ 0x7, 8 },
943 	{ 0xf, 16 },
944 	{ }
945 };
946 
947 static const struct clk_div_table clk_alpha_2bit_div_table[] = {
948 	{ 0x0, 1 },
949 	{ 0x1, 2 },
950 	{ 0x3, 4 },
951 	{ }
952 };
953 
954 static long
955 clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
956 				 unsigned long *prate)
957 {
958 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
959 	const struct clk_div_table *table;
960 
961 	if (pll->width == 2)
962 		table = clk_alpha_2bit_div_table;
963 	else
964 		table = clk_alpha_div_table;
965 
966 	return divider_round_rate(hw, rate, prate, table,
967 				  pll->width, CLK_DIVIDER_POWER_OF_TWO);
968 }
969 
970 static long
971 clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
972 				    unsigned long *prate)
973 {
974 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
975 	u32 ctl, div;
976 
977 	regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
978 
979 	ctl >>= PLL_POST_DIV_SHIFT;
980 	ctl &= BIT(pll->width) - 1;
981 	div = 1 << fls(ctl);
982 
983 	if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
984 		*prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
985 
986 	return DIV_ROUND_UP_ULL((u64)*prate, div);
987 }
988 
989 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
990 					  unsigned long parent_rate)
991 {
992 	struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
993 	int div;
994 
995 	/* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
996 	div = DIV_ROUND_UP_ULL(parent_rate, rate) - 1;
997 
998 	return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
999 				  PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
1000 				  div << PLL_POST_DIV_SHIFT);
1001 }
1002 
1003 const struct clk_ops clk_alpha_pll_postdiv_ops = {
1004 	.recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
1005 	.round_rate = clk_alpha_pll_postdiv_round_rate,
1006 	.set_rate = clk_alpha_pll_postdiv_set_rate,
1007 };
1008 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
1009 
1010 const struct clk_ops clk_alpha_pll_postdiv_ro_ops = {
1011 	.round_rate = clk_alpha_pll_postdiv_round_ro_rate,
1012 	.recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
1013 };
1014 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);
1015 
1016 void clk_fabia_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1017 			     const struct alpha_pll_config *config)
1018 {
1019 	u32 val, mask;
1020 
1021 	if (config->l)
1022 		regmap_write(regmap, PLL_L_VAL(pll), config->l);
1023 
1024 	if (config->alpha)
1025 		regmap_write(regmap, PLL_FRAC(pll), config->alpha);
1026 
1027 	if (config->config_ctl_val)
1028 		regmap_write(regmap, PLL_CONFIG_CTL(pll),
1029 						config->config_ctl_val);
1030 
1031 	if (config->config_ctl_hi_val)
1032 		regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
1033 						config->config_ctl_hi_val);
1034 
1035 	if (config->user_ctl_val)
1036 		regmap_write(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
1037 
1038 	if (config->user_ctl_hi_val)
1039 		regmap_write(regmap, PLL_USER_CTL_U(pll),
1040 						config->user_ctl_hi_val);
1041 
1042 	if (config->test_ctl_val)
1043 		regmap_write(regmap, PLL_TEST_CTL(pll),
1044 						config->test_ctl_val);
1045 
1046 	if (config->test_ctl_hi_val)
1047 		regmap_write(regmap, PLL_TEST_CTL_U(pll),
1048 						config->test_ctl_hi_val);
1049 
1050 	if (config->post_div_mask) {
1051 		mask = config->post_div_mask;
1052 		val = config->post_div_val;
1053 		regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
1054 	}
1055 
1056 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
1057 							PLL_UPDATE_BYPASS);
1058 
1059 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1060 }
1061 EXPORT_SYMBOL_GPL(clk_fabia_pll_configure);
1062 
1063 static int alpha_pll_fabia_enable(struct clk_hw *hw)
1064 {
1065 	int ret;
1066 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1067 	u32 val, opmode_val;
1068 	struct regmap *regmap = pll->clkr.regmap;
1069 
1070 	ret = regmap_read(regmap, PLL_MODE(pll), &val);
1071 	if (ret)
1072 		return ret;
1073 
1074 	/* If in FSM mode, just vote for it */
1075 	if (val & PLL_VOTE_FSM_ENA) {
1076 		ret = clk_enable_regmap(hw);
1077 		if (ret)
1078 			return ret;
1079 		return wait_for_pll_enable_active(pll);
1080 	}
1081 
1082 	ret = regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
1083 	if (ret)
1084 		return ret;
1085 
1086 	/* Skip If PLL is already running */
1087 	if ((opmode_val & PLL_RUN) && (val & PLL_OUTCTRL))
1088 		return 0;
1089 
1090 	ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1091 	if (ret)
1092 		return ret;
1093 
1094 	ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1095 	if (ret)
1096 		return ret;
1097 
1098 	ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N,
1099 				 PLL_RESET_N);
1100 	if (ret)
1101 		return ret;
1102 
1103 	ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
1104 	if (ret)
1105 		return ret;
1106 
1107 	ret = wait_for_pll_enable_lock(pll);
1108 	if (ret)
1109 		return ret;
1110 
1111 	ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
1112 				 PLL_OUT_MASK, PLL_OUT_MASK);
1113 	if (ret)
1114 		return ret;
1115 
1116 	return regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL,
1117 				 PLL_OUTCTRL);
1118 }
1119 
1120 static void alpha_pll_fabia_disable(struct clk_hw *hw)
1121 {
1122 	int ret;
1123 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1124 	u32 val;
1125 	struct regmap *regmap = pll->clkr.regmap;
1126 
1127 	ret = regmap_read(regmap, PLL_MODE(pll), &val);
1128 	if (ret)
1129 		return;
1130 
1131 	/* If in FSM mode, just unvote it */
1132 	if (val & PLL_FSM_ENA) {
1133 		clk_disable_regmap(hw);
1134 		return;
1135 	}
1136 
1137 	ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1138 	if (ret)
1139 		return;
1140 
1141 	/* Disable main outputs */
1142 	ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
1143 	if (ret)
1144 		return;
1145 
1146 	/* Place the PLL in STANDBY */
1147 	regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1148 }
1149 
1150 static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw *hw,
1151 						unsigned long parent_rate)
1152 {
1153 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1154 	u32 l, frac, alpha_width = pll_alpha_width(pll);
1155 
1156 	regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
1157 	regmap_read(pll->clkr.regmap, PLL_FRAC(pll), &frac);
1158 
1159 	return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
1160 }
1161 
1162 static int alpha_pll_fabia_set_rate(struct clk_hw *hw, unsigned long rate,
1163 						unsigned long prate)
1164 {
1165 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1166 	u32 l, alpha_width = pll_alpha_width(pll);
1167 	u64 a;
1168 	unsigned long rrate, max = rate + PLL_RATE_MARGIN;
1169 
1170 	rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1171 
1172 	/*
1173 	 * Due to limited number of bits for fractional rate programming, the
1174 	 * rounded up rate could be marginally higher than the requested rate.
1175 	 */
1176 	if (rrate > (rate + PLL_RATE_MARGIN) || rrate < rate) {
1177 		pr_err("%s: Rounded rate %lu not within range [%lu, %lu)\n",
1178 		       clk_hw_get_name(hw), rrate, rate, max);
1179 		return -EINVAL;
1180 	}
1181 
1182 	regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1183 	regmap_write(pll->clkr.regmap, PLL_FRAC(pll), a);
1184 
1185 	return __clk_alpha_pll_update_latch(pll);
1186 }
1187 
1188 static int alpha_pll_fabia_prepare(struct clk_hw *hw)
1189 {
1190 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1191 	const struct pll_vco *vco;
1192 	struct clk_hw *parent_hw;
1193 	unsigned long cal_freq, rrate;
1194 	u32 cal_l, val, alpha_width = pll_alpha_width(pll);
1195 	const char *name = clk_hw_get_name(hw);
1196 	u64 a;
1197 	int ret;
1198 
1199 	/* Check if calibration needs to be done i.e. PLL is in reset */
1200 	ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1201 	if (ret)
1202 		return ret;
1203 
1204 	/* Return early if calibration is not needed. */
1205 	if (val & PLL_RESET_N)
1206 		return 0;
1207 
1208 	vco = alpha_pll_find_vco(pll, clk_hw_get_rate(hw));
1209 	if (!vco) {
1210 		pr_err("%s: alpha pll not in a valid vco range\n", name);
1211 		return -EINVAL;
1212 	}
1213 
1214 	cal_freq = DIV_ROUND_CLOSEST((pll->vco_table[0].min_freq +
1215 				pll->vco_table[0].max_freq) * 54, 100);
1216 
1217 	parent_hw = clk_hw_get_parent(hw);
1218 	if (!parent_hw)
1219 		return -EINVAL;
1220 
1221 	rrate = alpha_pll_round_rate(cal_freq, clk_hw_get_rate(parent_hw),
1222 					&cal_l, &a, alpha_width);
1223 	/*
1224 	 * Due to a limited number of bits for fractional rate programming, the
1225 	 * rounded up rate could be marginally higher than the requested rate.
1226 	 */
1227 	if (rrate > (cal_freq + PLL_RATE_MARGIN) || rrate < cal_freq)
1228 		return -EINVAL;
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_trion_pll_postdiv_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_trion_pll_postdiv_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_lucid_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1403 			     const struct alpha_pll_config *config)
1404 {
1405 	if (config->l)
1406 		regmap_write(regmap, PLL_L_VAL(pll), config->l);
1407 
1408 	regmap_write(regmap, PLL_CAL_L_VAL(pll), LUCID_PLL_CAL_VAL);
1409 
1410 	if (config->alpha)
1411 		regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
1412 
1413 	if (config->config_ctl_val)
1414 		regmap_write(regmap, PLL_CONFIG_CTL(pll),
1415 			     config->config_ctl_val);
1416 
1417 	if (config->config_ctl_hi_val)
1418 		regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
1419 			     config->config_ctl_hi_val);
1420 
1421 	if (config->config_ctl_hi1_val)
1422 		regmap_write(regmap, PLL_CONFIG_CTL_U1(pll),
1423 			     config->config_ctl_hi1_val);
1424 
1425 	if (config->user_ctl_val)
1426 		regmap_write(regmap, PLL_USER_CTL(pll),
1427 			     config->user_ctl_val);
1428 
1429 	if (config->user_ctl_hi_val)
1430 		regmap_write(regmap, PLL_USER_CTL_U(pll),
1431 			     config->user_ctl_hi_val);
1432 
1433 	if (config->user_ctl_hi1_val)
1434 		regmap_write(regmap, PLL_USER_CTL_U1(pll),
1435 			     config->user_ctl_hi1_val);
1436 
1437 	if (config->test_ctl_val)
1438 		regmap_write(regmap, PLL_TEST_CTL(pll),
1439 			     config->test_ctl_val);
1440 
1441 	if (config->test_ctl_hi_val)
1442 		regmap_write(regmap, PLL_TEST_CTL_U(pll),
1443 			     config->test_ctl_hi_val);
1444 
1445 	if (config->test_ctl_hi1_val)
1446 		regmap_write(regmap, PLL_TEST_CTL_U1(pll),
1447 			     config->test_ctl_hi1_val);
1448 
1449 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
1450 			   PLL_UPDATE_BYPASS);
1451 
1452 	/* Disable PLL output */
1453 	regmap_update_bits(regmap, PLL_MODE(pll),  PLL_OUTCTRL, 0);
1454 
1455 	/* Set operation mode to OFF */
1456 	regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1457 
1458 	/* Place the PLL in STANDBY mode */
1459 	regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1460 }
1461 EXPORT_SYMBOL_GPL(clk_lucid_pll_configure);
1462 
1463 /*
1464  * The Lucid PLL requires a power-on self-calibration which happens when the
1465  * PLL comes out of reset. Calibrate in case it is not completed.
1466  */
1467 static int alpha_pll_lucid_prepare(struct clk_hw *hw)
1468 {
1469 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1470 	u32 regval;
1471 	int ret;
1472 
1473 	/* Return early if calibration is not needed. */
1474 	regmap_read(pll->clkr.regmap, PLL_STATUS(pll), &regval);
1475 	if (regval & LUCID_PCAL_DONE)
1476 		return 0;
1477 
1478 	/* On/off to calibrate */
1479 	ret = clk_trion_pll_enable(hw);
1480 	if (!ret)
1481 		clk_trion_pll_disable(hw);
1482 
1483 	return ret;
1484 }
1485 
1486 static int alpha_pll_lucid_set_rate(struct clk_hw *hw, unsigned long rate,
1487 				    unsigned long prate)
1488 {
1489 	struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1490 	unsigned long rrate;
1491 	u32 regval, l, alpha_width = pll_alpha_width(pll);
1492 	u64 a;
1493 	int ret;
1494 
1495 	rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1496 
1497 	/*
1498 	 * Due to a limited number of bits for fractional rate programming, the
1499 	 * rounded up rate could be marginally higher than the requested rate.
1500 	 */
1501 	if (rrate > (rate + PLL_RATE_MARGIN) || rrate < rate) {
1502 		pr_err("Call set rate on the PLL with rounded rates!\n");
1503 		return -EINVAL;
1504 	}
1505 
1506 	regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1507 	regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
1508 
1509 	/* Latch the PLL input */
1510 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
1511 				 PLL_UPDATE, PLL_UPDATE);
1512 	if (ret)
1513 		return ret;
1514 
1515 	/* Wait for 2 reference cycles before checking the ACK bit. */
1516 	udelay(1);
1517 	regmap_read(pll->clkr.regmap, PLL_MODE(pll), &regval);
1518 	if (!(regval & ALPHA_PLL_ACK_LATCH)) {
1519 		pr_err("Lucid PLL latch failed. Output may be unstable!\n");
1520 		return -EINVAL;
1521 	}
1522 
1523 	/* Return the latch input to 0 */
1524 	ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
1525 				 PLL_UPDATE, 0);
1526 	if (ret)
1527 		return ret;
1528 
1529 	if (clk_hw_is_enabled(hw)) {
1530 		ret = wait_for_pll_enable_lock(pll);
1531 		if (ret)
1532 			return ret;
1533 	}
1534 
1535 	/* Wait for PLL output to stabilize */
1536 	udelay(100);
1537 	return 0;
1538 }
1539 
1540 const struct clk_ops clk_alpha_pll_lucid_ops = {
1541 	.prepare = alpha_pll_lucid_prepare,
1542 	.enable = clk_trion_pll_enable,
1543 	.disable = clk_trion_pll_disable,
1544 	.is_enabled = clk_trion_pll_is_enabled,
1545 	.recalc_rate = clk_trion_pll_recalc_rate,
1546 	.round_rate = clk_alpha_pll_round_rate,
1547 	.set_rate = alpha_pll_lucid_set_rate,
1548 };
1549 EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_ops);
1550 
1551 const struct clk_ops clk_alpha_pll_fixed_lucid_ops = {
1552 	.enable = clk_trion_pll_enable,
1553 	.disable = clk_trion_pll_disable,
1554 	.is_enabled = clk_trion_pll_is_enabled,
1555 	.recalc_rate = clk_trion_pll_recalc_rate,
1556 	.round_rate = clk_alpha_pll_round_rate,
1557 };
1558 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_lucid_ops);
1559 
1560 const struct clk_ops clk_alpha_pll_postdiv_lucid_ops = {
1561 	.recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
1562 	.round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
1563 	.set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
1564 };
1565 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_ops);
1566