xref: /linux/drivers/gpu/drm/i915/display/intel_dpll.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/string_helpers.h>
8 
9 #include <drm/drm_print.h>
10 
11 #include "intel_atomic.h"
12 #include "intel_crtc.h"
13 #include "intel_cx0_phy.h"
14 #include "intel_de.h"
15 #include "intel_display.h"
16 #include "intel_display_regs.h"
17 #include "intel_display_types.h"
18 #include "intel_dpio_phy.h"
19 #include "intel_dpll.h"
20 #include "intel_lt_phy.h"
21 #include "intel_lvds.h"
22 #include "intel_lvds_regs.h"
23 #include "intel_panel.h"
24 #include "intel_pps.h"
25 #include "vlv_dpio_phy_regs.h"
26 #include "vlv_sideband.h"
27 
28 struct intel_dpll_global_funcs {
29 	int (*crtc_compute_clock)(struct intel_atomic_state *state,
30 				  struct intel_crtc *crtc);
31 	int (*crtc_get_dpll)(struct intel_atomic_state *state,
32 			     struct intel_crtc *crtc);
33 };
34 
35 struct intel_limit {
36 	struct {
37 		int min, max;
38 	} dot, vco, n, m, m1, m2, p, p1;
39 
40 	struct {
41 		int dot_limit;
42 		int p2_slow, p2_fast;
43 	} p2;
44 };
45 static const struct intel_limit intel_limits_i8xx_dac = {
46 	.dot = { .min = 25000, .max = 350000 },
47 	.vco = { .min = 908000, .max = 1512000 },
48 	.n = { .min = 2, .max = 16 },
49 	.m = { .min = 96, .max = 140 },
50 	.m1 = { .min = 18, .max = 26 },
51 	.m2 = { .min = 6, .max = 16 },
52 	.p = { .min = 4, .max = 128 },
53 	.p1 = { .min = 2, .max = 33 },
54 	.p2 = { .dot_limit = 165000,
55 		.p2_slow = 4, .p2_fast = 2 },
56 };
57 
58 static const struct intel_limit intel_limits_i8xx_dvo = {
59 	.dot = { .min = 25000, .max = 350000 },
60 	.vco = { .min = 908000, .max = 1512000 },
61 	.n = { .min = 2, .max = 16 },
62 	.m = { .min = 96, .max = 140 },
63 	.m1 = { .min = 18, .max = 26 },
64 	.m2 = { .min = 6, .max = 16 },
65 	.p = { .min = 4, .max = 128 },
66 	.p1 = { .min = 2, .max = 33 },
67 	.p2 = { .dot_limit = 165000,
68 		.p2_slow = 4, .p2_fast = 4 },
69 };
70 
71 static const struct intel_limit intel_limits_i8xx_lvds = {
72 	.dot = { .min = 25000, .max = 350000 },
73 	.vco = { .min = 908000, .max = 1512000 },
74 	.n = { .min = 2, .max = 16 },
75 	.m = { .min = 96, .max = 140 },
76 	.m1 = { .min = 18, .max = 26 },
77 	.m2 = { .min = 6, .max = 16 },
78 	.p = { .min = 4, .max = 128 },
79 	.p1 = { .min = 1, .max = 6 },
80 	.p2 = { .dot_limit = 165000,
81 		.p2_slow = 14, .p2_fast = 7 },
82 };
83 
84 static const struct intel_limit intel_limits_i9xx_sdvo = {
85 	.dot = { .min = 20000, .max = 400000 },
86 	.vco = { .min = 1400000, .max = 2800000 },
87 	.n = { .min = 1, .max = 6 },
88 	.m = { .min = 70, .max = 120 },
89 	.m1 = { .min = 8, .max = 18 },
90 	.m2 = { .min = 3, .max = 7 },
91 	.p = { .min = 5, .max = 80 },
92 	.p1 = { .min = 1, .max = 8 },
93 	.p2 = { .dot_limit = 200000,
94 		.p2_slow = 10, .p2_fast = 5 },
95 };
96 
97 static const struct intel_limit intel_limits_i9xx_lvds = {
98 	.dot = { .min = 20000, .max = 400000 },
99 	.vco = { .min = 1400000, .max = 2800000 },
100 	.n = { .min = 1, .max = 6 },
101 	.m = { .min = 70, .max = 120 },
102 	.m1 = { .min = 8, .max = 18 },
103 	.m2 = { .min = 3, .max = 7 },
104 	.p = { .min = 7, .max = 98 },
105 	.p1 = { .min = 1, .max = 8 },
106 	.p2 = { .dot_limit = 112000,
107 		.p2_slow = 14, .p2_fast = 7 },
108 };
109 
110 
111 static const struct intel_limit intel_limits_g4x_sdvo = {
112 	.dot = { .min = 25000, .max = 270000 },
113 	.vco = { .min = 1750000, .max = 3500000},
114 	.n = { .min = 1, .max = 4 },
115 	.m = { .min = 104, .max = 138 },
116 	.m1 = { .min = 17, .max = 23 },
117 	.m2 = { .min = 5, .max = 11 },
118 	.p = { .min = 10, .max = 30 },
119 	.p1 = { .min = 1, .max = 3},
120 	.p2 = { .dot_limit = 270000,
121 		.p2_slow = 10,
122 		.p2_fast = 10
123 	},
124 };
125 
126 static const struct intel_limit intel_limits_g4x_hdmi = {
127 	.dot = { .min = 22000, .max = 400000 },
128 	.vco = { .min = 1750000, .max = 3500000},
129 	.n = { .min = 1, .max = 4 },
130 	.m = { .min = 104, .max = 138 },
131 	.m1 = { .min = 16, .max = 23 },
132 	.m2 = { .min = 5, .max = 11 },
133 	.p = { .min = 5, .max = 80 },
134 	.p1 = { .min = 1, .max = 8},
135 	.p2 = { .dot_limit = 165000,
136 		.p2_slow = 10, .p2_fast = 5 },
137 };
138 
139 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
140 	.dot = { .min = 20000, .max = 115000 },
141 	.vco = { .min = 1750000, .max = 3500000 },
142 	.n = { .min = 1, .max = 3 },
143 	.m = { .min = 104, .max = 138 },
144 	.m1 = { .min = 17, .max = 23 },
145 	.m2 = { .min = 5, .max = 11 },
146 	.p = { .min = 28, .max = 112 },
147 	.p1 = { .min = 2, .max = 8 },
148 	.p2 = { .dot_limit = 0,
149 		.p2_slow = 14, .p2_fast = 14
150 	},
151 };
152 
153 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
154 	.dot = { .min = 80000, .max = 224000 },
155 	.vco = { .min = 1750000, .max = 3500000 },
156 	.n = { .min = 1, .max = 3 },
157 	.m = { .min = 104, .max = 138 },
158 	.m1 = { .min = 17, .max = 23 },
159 	.m2 = { .min = 5, .max = 11 },
160 	.p = { .min = 14, .max = 42 },
161 	.p1 = { .min = 2, .max = 6 },
162 	.p2 = { .dot_limit = 0,
163 		.p2_slow = 7, .p2_fast = 7
164 	},
165 };
166 
167 static const struct intel_limit pnv_limits_sdvo = {
168 	.dot = { .min = 20000, .max = 400000},
169 	.vco = { .min = 1700000, .max = 3500000 },
170 	/* Pineview's Ncounter is a ring counter */
171 	.n = { .min = 3, .max = 6 },
172 	.m = { .min = 2, .max = 256 },
173 	/* Pineview only has one combined m divider, which we treat as m2. */
174 	.m1 = { .min = 0, .max = 0 },
175 	.m2 = { .min = 0, .max = 254 },
176 	.p = { .min = 5, .max = 80 },
177 	.p1 = { .min = 1, .max = 8 },
178 	.p2 = { .dot_limit = 200000,
179 		.p2_slow = 10, .p2_fast = 5 },
180 };
181 
182 static const struct intel_limit pnv_limits_lvds = {
183 	.dot = { .min = 20000, .max = 400000 },
184 	.vco = { .min = 1700000, .max = 3500000 },
185 	.n = { .min = 3, .max = 6 },
186 	.m = { .min = 2, .max = 256 },
187 	.m1 = { .min = 0, .max = 0 },
188 	.m2 = { .min = 0, .max = 254 },
189 	.p = { .min = 7, .max = 112 },
190 	.p1 = { .min = 1, .max = 8 },
191 	.p2 = { .dot_limit = 112000,
192 		.p2_slow = 14, .p2_fast = 14 },
193 };
194 
195 /* Ironlake / Sandybridge
196  *
197  * We calculate clock using (register_value + 2) for N/M1/M2, so here
198  * the range value for them is (actual_value - 2).
199  */
200 static const struct intel_limit ilk_limits_dac = {
201 	.dot = { .min = 25000, .max = 350000 },
202 	.vco = { .min = 1760000, .max = 3510000 },
203 	.n = { .min = 1, .max = 5 },
204 	.m = { .min = 79, .max = 127 },
205 	.m1 = { .min = 12, .max = 22 },
206 	.m2 = { .min = 5, .max = 9 },
207 	.p = { .min = 5, .max = 80 },
208 	.p1 = { .min = 1, .max = 8 },
209 	.p2 = { .dot_limit = 225000,
210 		.p2_slow = 10, .p2_fast = 5 },
211 };
212 
213 static const struct intel_limit ilk_limits_single_lvds = {
214 	.dot = { .min = 25000, .max = 350000 },
215 	.vco = { .min = 1760000, .max = 3510000 },
216 	.n = { .min = 1, .max = 3 },
217 	.m = { .min = 79, .max = 118 },
218 	.m1 = { .min = 12, .max = 22 },
219 	.m2 = { .min = 5, .max = 9 },
220 	.p = { .min = 28, .max = 112 },
221 	.p1 = { .min = 2, .max = 8 },
222 	.p2 = { .dot_limit = 225000,
223 		.p2_slow = 14, .p2_fast = 14 },
224 };
225 
226 static const struct intel_limit ilk_limits_dual_lvds = {
227 	.dot = { .min = 25000, .max = 350000 },
228 	.vco = { .min = 1760000, .max = 3510000 },
229 	.n = { .min = 1, .max = 3 },
230 	.m = { .min = 79, .max = 127 },
231 	.m1 = { .min = 12, .max = 22 },
232 	.m2 = { .min = 5, .max = 9 },
233 	.p = { .min = 14, .max = 56 },
234 	.p1 = { .min = 2, .max = 8 },
235 	.p2 = { .dot_limit = 225000,
236 		.p2_slow = 7, .p2_fast = 7 },
237 };
238 
239 /* LVDS 100mhz refclk limits. */
240 static const struct intel_limit ilk_limits_single_lvds_100m = {
241 	.dot = { .min = 25000, .max = 350000 },
242 	.vco = { .min = 1760000, .max = 3510000 },
243 	.n = { .min = 1, .max = 2 },
244 	.m = { .min = 79, .max = 126 },
245 	.m1 = { .min = 12, .max = 22 },
246 	.m2 = { .min = 5, .max = 9 },
247 	.p = { .min = 28, .max = 112 },
248 	.p1 = { .min = 2, .max = 8 },
249 	.p2 = { .dot_limit = 225000,
250 		.p2_slow = 14, .p2_fast = 14 },
251 };
252 
253 static const struct intel_limit ilk_limits_dual_lvds_100m = {
254 	.dot = { .min = 25000, .max = 350000 },
255 	.vco = { .min = 1760000, .max = 3510000 },
256 	.n = { .min = 1, .max = 3 },
257 	.m = { .min = 79, .max = 126 },
258 	.m1 = { .min = 12, .max = 22 },
259 	.m2 = { .min = 5, .max = 9 },
260 	.p = { .min = 14, .max = 42 },
261 	.p1 = { .min = 2, .max = 6 },
262 	.p2 = { .dot_limit = 225000,
263 		.p2_slow = 7, .p2_fast = 7 },
264 };
265 
266 static const struct intel_limit intel_limits_vlv = {
267 	 /*
268 	  * These are based on the data rate limits (measured in fast clocks)
269 	  * since those are the strictest limits we have. The fast
270 	  * clock and actual rate limits are more relaxed, so checking
271 	  * them would make no difference.
272 	  */
273 	.dot = { .min = 25000, .max = 270000 },
274 	.vco = { .min = 4000000, .max = 6000000 },
275 	.n = { .min = 1, .max = 7 },
276 	.m1 = { .min = 2, .max = 3 },
277 	.m2 = { .min = 11, .max = 156 },
278 	.p1 = { .min = 2, .max = 3 },
279 	.p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
280 };
281 
282 static const struct intel_limit intel_limits_chv = {
283 	/*
284 	 * These are based on the data rate limits (measured in fast clocks)
285 	 * since those are the strictest limits we have.  The fast
286 	 * clock and actual rate limits are more relaxed, so checking
287 	 * them would make no difference.
288 	 */
289 	.dot = { .min = 25000, .max = 540000 },
290 	.vco = { .min = 4800000, .max = 6480000 },
291 	.n = { .min = 1, .max = 1 },
292 	.m1 = { .min = 2, .max = 2 },
293 	.m2 = { .min = 24 << 22, .max = 175 << 22 },
294 	.p1 = { .min = 2, .max = 4 },
295 	.p2 = {	.p2_slow = 1, .p2_fast = 14 },
296 };
297 
298 static const struct intel_limit intel_limits_bxt = {
299 	.dot = { .min = 25000, .max = 594000 },
300 	.vco = { .min = 4800000, .max = 6700000 },
301 	.n = { .min = 1, .max = 1 },
302 	.m1 = { .min = 2, .max = 2 },
303 	/* FIXME: find real m2 limits */
304 	.m2 = { .min = 2 << 22, .max = 255 << 22 },
305 	.p1 = { .min = 2, .max = 4 },
306 	.p2 = { .p2_slow = 1, .p2_fast = 20 },
307 };
308 
309 /*
310  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
311  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
312  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
313  * The helpers' return value is the rate of the clock that is fed to the
314  * display engine's pipe which can be the above fast dot clock rate or a
315  * divided-down version of it.
316  */
317 /* m1 is reserved as 0 in Pineview, n is a ring counter */
318 static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
319 {
320 	clock->m = clock->m2 + 2;
321 	clock->p = clock->p1 * clock->p2;
322 
323 	clock->vco = clock->n == 0 ? 0 :
324 		DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
325 	clock->dot = clock->p == 0 ? 0 :
326 		DIV_ROUND_CLOSEST(clock->vco, clock->p);
327 
328 	return clock->dot;
329 }
330 
331 static u32 i9xx_dpll_compute_m(const struct dpll *dpll)
332 {
333 	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
334 }
335 
336 int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
337 {
338 	clock->m = i9xx_dpll_compute_m(clock);
339 	clock->p = clock->p1 * clock->p2;
340 
341 	clock->vco = clock->n + 2 == 0 ? 0 :
342 		DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
343 	clock->dot = clock->p == 0 ? 0 :
344 		DIV_ROUND_CLOSEST(clock->vco, clock->p);
345 
346 	return clock->dot;
347 }
348 
349 static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
350 {
351 	clock->m = clock->m1 * clock->m2;
352 	clock->p = clock->p1 * clock->p2 * 5;
353 
354 	clock->vco = clock->n == 0 ? 0 :
355 		DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
356 	clock->dot = clock->p == 0 ? 0 :
357 		DIV_ROUND_CLOSEST(clock->vco, clock->p);
358 
359 	return clock->dot;
360 }
361 
362 int chv_calc_dpll_params(int refclk, struct dpll *clock)
363 {
364 	clock->m = clock->m1 * clock->m2;
365 	clock->p = clock->p1 * clock->p2 * 5;
366 
367 	clock->vco = clock->n == 0 ? 0 :
368 		DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m), clock->n << 22);
369 	clock->dot = clock->p == 0 ? 0 :
370 		DIV_ROUND_CLOSEST(clock->vco, clock->p);
371 
372 	return clock->dot;
373 }
374 
375 static int i9xx_pll_refclk(const struct intel_crtc_state *crtc_state)
376 {
377 	struct intel_display *display = to_intel_display(crtc_state);
378 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
379 
380 	if ((hw_state->dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
381 		return display->vbt.lvds_ssc_freq;
382 	else if (HAS_PCH_SPLIT(display))
383 		return 120000;
384 	else if (DISPLAY_VER(display) != 2)
385 		return 96000;
386 	else
387 		return 48000;
388 }
389 
390 void i9xx_dpll_get_hw_state(struct intel_crtc *crtc,
391 			    struct intel_dpll_hw_state *dpll_hw_state)
392 {
393 	struct intel_display *display = to_intel_display(crtc);
394 	struct i9xx_dpll_hw_state *hw_state = &dpll_hw_state->i9xx;
395 
396 	if (DISPLAY_VER(display) >= 4) {
397 		u32 tmp;
398 
399 		/* No way to read it out on pipes B and C */
400 		if (display->platform.cherryview && crtc->pipe != PIPE_A)
401 			tmp = display->state.chv_dpll_md[crtc->pipe];
402 		else
403 			tmp = intel_de_read(display,
404 					    DPLL_MD(display, crtc->pipe));
405 
406 		hw_state->dpll_md = tmp;
407 	}
408 
409 	hw_state->dpll = intel_de_read(display, DPLL(display, crtc->pipe));
410 
411 	if (!display->platform.valleyview && !display->platform.cherryview) {
412 		hw_state->fp0 = intel_de_read(display, FP0(crtc->pipe));
413 		hw_state->fp1 = intel_de_read(display, FP1(crtc->pipe));
414 	} else {
415 		/* Mask out read-only status bits. */
416 		hw_state->dpll &= ~(DPLL_LOCK_VLV |
417 				    DPLL_PORTC_READY_MASK |
418 				    DPLL_PORTB_READY_MASK);
419 	}
420 }
421 
422 /* Returns the clock of the currently programmed mode of the given pipe. */
423 void i9xx_crtc_clock_get(struct intel_crtc_state *crtc_state)
424 {
425 	struct intel_display *display = to_intel_display(crtc_state);
426 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
427 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
428 	u32 dpll = hw_state->dpll;
429 	u32 fp;
430 	struct dpll clock;
431 	int port_clock;
432 	int refclk = i9xx_pll_refclk(crtc_state);
433 
434 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
435 		fp = hw_state->fp0;
436 	else
437 		fp = hw_state->fp1;
438 
439 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
440 	if (display->platform.pineview) {
441 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
442 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
443 	} else {
444 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
445 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
446 	}
447 
448 	if (DISPLAY_VER(display) != 2) {
449 		if (display->platform.pineview)
450 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
451 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
452 		else
453 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
454 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
455 
456 		switch (dpll & DPLL_MODE_MASK) {
457 		case DPLLB_MODE_DAC_SERIAL:
458 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
459 				5 : 10;
460 			break;
461 		case DPLLB_MODE_LVDS:
462 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
463 				7 : 14;
464 			break;
465 		default:
466 			drm_dbg_kms(display->drm,
467 				    "Unknown DPLL mode %08x in programmed "
468 				    "mode\n", (int)(dpll & DPLL_MODE_MASK));
469 			return;
470 		}
471 
472 		if (display->platform.pineview)
473 			port_clock = pnv_calc_dpll_params(refclk, &clock);
474 		else
475 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
476 	} else {
477 		enum pipe lvds_pipe;
478 
479 		if (display->platform.i85x &&
480 		    intel_lvds_port_enabled(display, LVDS, &lvds_pipe) &&
481 		    lvds_pipe == crtc->pipe) {
482 			u32 lvds = intel_de_read(display, LVDS);
483 
484 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
485 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
486 
487 			if (lvds & LVDS_CLKB_POWER_UP)
488 				clock.p2 = 7;
489 			else
490 				clock.p2 = 14;
491 		} else {
492 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
493 				clock.p1 = 2;
494 			else {
495 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
496 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
497 			}
498 			if (dpll & PLL_P2_DIVIDE_BY_4)
499 				clock.p2 = 4;
500 			else
501 				clock.p2 = 2;
502 		}
503 
504 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
505 	}
506 
507 	/*
508 	 * This value includes pixel_multiplier. We will use
509 	 * port_clock to compute adjusted_mode.crtc_clock in the
510 	 * encoder's get_config() function.
511 	 */
512 	crtc_state->port_clock = port_clock;
513 }
514 
515 void vlv_crtc_clock_get(struct intel_crtc_state *crtc_state)
516 {
517 	struct intel_display *display = to_intel_display(crtc_state);
518 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
519 	enum dpio_channel ch = vlv_pipe_to_channel(crtc->pipe);
520 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
521 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
522 	int refclk = 100000;
523 	struct dpll clock;
524 	u32 tmp;
525 
526 	/* In case of DSI, DPLL will not be used */
527 	if ((hw_state->dpll & DPLL_VCO_ENABLE) == 0)
528 		return;
529 
530 	vlv_dpio_get(display);
531 	tmp = vlv_dpio_read(display, phy, VLV_PLL_DW3(ch));
532 	vlv_dpio_put(display);
533 
534 	clock.m1 = REG_FIELD_GET(DPIO_M1_DIV_MASK, tmp);
535 	clock.m2 = REG_FIELD_GET(DPIO_M2_DIV_MASK, tmp);
536 	clock.n = REG_FIELD_GET(DPIO_N_DIV_MASK, tmp);
537 	clock.p1 = REG_FIELD_GET(DPIO_P1_DIV_MASK, tmp);
538 	clock.p2 = REG_FIELD_GET(DPIO_P2_DIV_MASK, tmp);
539 
540 	crtc_state->port_clock = vlv_calc_dpll_params(refclk, &clock);
541 }
542 
543 void chv_crtc_clock_get(struct intel_crtc_state *crtc_state)
544 {
545 	struct intel_display *display = to_intel_display(crtc_state);
546 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
547 	enum dpio_channel ch = vlv_pipe_to_channel(crtc->pipe);
548 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
549 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
550 	struct dpll clock;
551 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
552 	int refclk = 100000;
553 
554 	/* In case of DSI, DPLL will not be used */
555 	if ((hw_state->dpll & DPLL_VCO_ENABLE) == 0)
556 		return;
557 
558 	vlv_dpio_get(display);
559 	cmn_dw13 = vlv_dpio_read(display, phy, CHV_CMN_DW13(ch));
560 	pll_dw0 = vlv_dpio_read(display, phy, CHV_PLL_DW0(ch));
561 	pll_dw1 = vlv_dpio_read(display, phy, CHV_PLL_DW1(ch));
562 	pll_dw2 = vlv_dpio_read(display, phy, CHV_PLL_DW2(ch));
563 	pll_dw3 = vlv_dpio_read(display, phy, CHV_PLL_DW3(ch));
564 	vlv_dpio_put(display);
565 
566 	clock.m1 = REG_FIELD_GET(DPIO_CHV_M1_DIV_MASK, pll_dw1) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
567 	clock.m2 = REG_FIELD_GET(DPIO_CHV_M2_DIV_MASK, pll_dw0) << 22;
568 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
569 		clock.m2 |= REG_FIELD_GET(DPIO_CHV_M2_FRAC_DIV_MASK, pll_dw2);
570 	clock.n = REG_FIELD_GET(DPIO_CHV_N_DIV_MASK, pll_dw1);
571 	clock.p1 = REG_FIELD_GET(DPIO_CHV_P1_DIV_MASK, cmn_dw13);
572 	clock.p2 = REG_FIELD_GET(DPIO_CHV_P2_DIV_MASK, cmn_dw13);
573 
574 	crtc_state->port_clock = chv_calc_dpll_params(refclk, &clock);
575 }
576 
577 /*
578  * Returns whether the given set of divisors are valid for a given refclk with
579  * the given connectors.
580  */
581 static bool intel_pll_is_valid(struct intel_display *display,
582 			       const struct intel_limit *limit,
583 			       const struct dpll *clock)
584 {
585 	if (clock->n < limit->n.min || limit->n.max < clock->n)
586 		return false;
587 	if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
588 		return false;
589 	if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
590 		return false;
591 	if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
592 		return false;
593 
594 	if (!display->platform.pineview &&
595 	    !display->platform.valleyview && !display->platform.cherryview &&
596 	    !display->platform.broxton && !display->platform.geminilake)
597 		if (clock->m1 <= clock->m2)
598 			return false;
599 
600 	if (!display->platform.valleyview && !display->platform.cherryview &&
601 	    !display->platform.broxton && !display->platform.geminilake) {
602 		if (clock->p < limit->p.min || limit->p.max < clock->p)
603 			return false;
604 		if (clock->m < limit->m.min || limit->m.max < clock->m)
605 			return false;
606 	}
607 
608 	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
609 		return false;
610 	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
611 	 * connector, etc., rather than just a single range.
612 	 */
613 	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
614 		return false;
615 
616 	return true;
617 }
618 
619 static int
620 i9xx_select_p2_div(const struct intel_limit *limit,
621 		   const struct intel_crtc_state *crtc_state,
622 		   int target)
623 {
624 	struct intel_display *display = to_intel_display(crtc_state);
625 
626 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
627 		/*
628 		 * For LVDS just rely on its current settings for dual-channel.
629 		 * We haven't figured out how to reliably set up different
630 		 * single/dual channel state, if we even can.
631 		 */
632 		if (intel_is_dual_link_lvds(display))
633 			return limit->p2.p2_fast;
634 		else
635 			return limit->p2.p2_slow;
636 	} else {
637 		if (target < limit->p2.dot_limit)
638 			return limit->p2.p2_slow;
639 		else
640 			return limit->p2.p2_fast;
641 	}
642 }
643 
644 /*
645  * Returns a set of divisors for the desired target clock with the given
646  * refclk, or FALSE.
647  *
648  * Target and reference clocks are specified in kHz.
649  *
650  * If match_clock is provided, then best_clock P divider must match the P
651  * divider from @match_clock used for LVDS downclocking.
652  */
653 static bool
654 i9xx_find_best_dpll(const struct intel_limit *limit,
655 		    struct intel_crtc_state *crtc_state,
656 		    int target, int refclk,
657 		    const struct dpll *match_clock,
658 		    struct dpll *best_clock)
659 {
660 	struct intel_display *display = to_intel_display(crtc_state);
661 	struct dpll clock;
662 	int err = target;
663 
664 	memset(best_clock, 0, sizeof(*best_clock));
665 
666 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
667 
668 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
669 	     clock.m1++) {
670 		for (clock.m2 = limit->m2.min;
671 		     clock.m2 <= limit->m2.max; clock.m2++) {
672 			if (clock.m2 >= clock.m1)
673 				break;
674 			for (clock.n = limit->n.min;
675 			     clock.n <= limit->n.max; clock.n++) {
676 				for (clock.p1 = limit->p1.min;
677 					clock.p1 <= limit->p1.max; clock.p1++) {
678 					int this_err;
679 
680 					i9xx_calc_dpll_params(refclk, &clock);
681 					if (!intel_pll_is_valid(display,
682 								limit,
683 								&clock))
684 						continue;
685 					if (match_clock &&
686 					    clock.p != match_clock->p)
687 						continue;
688 
689 					this_err = abs(clock.dot - target);
690 					if (this_err < err) {
691 						*best_clock = clock;
692 						err = this_err;
693 					}
694 				}
695 			}
696 		}
697 	}
698 
699 	return (err != target);
700 }
701 
702 /*
703  * Returns a set of divisors for the desired target clock with the given
704  * refclk, or FALSE.
705  *
706  * Target and reference clocks are specified in kHz.
707  *
708  * If match_clock is provided, then best_clock P divider must match the P
709  * divider from @match_clock used for LVDS downclocking.
710  */
711 static bool
712 pnv_find_best_dpll(const struct intel_limit *limit,
713 		   struct intel_crtc_state *crtc_state,
714 		   int target, int refclk,
715 		   const struct dpll *match_clock,
716 		   struct dpll *best_clock)
717 {
718 	struct intel_display *display = to_intel_display(crtc_state);
719 	struct dpll clock;
720 	int err = target;
721 
722 	memset(best_clock, 0, sizeof(*best_clock));
723 
724 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
725 
726 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
727 	     clock.m1++) {
728 		for (clock.m2 = limit->m2.min;
729 		     clock.m2 <= limit->m2.max; clock.m2++) {
730 			for (clock.n = limit->n.min;
731 			     clock.n <= limit->n.max; clock.n++) {
732 				for (clock.p1 = limit->p1.min;
733 					clock.p1 <= limit->p1.max; clock.p1++) {
734 					int this_err;
735 
736 					pnv_calc_dpll_params(refclk, &clock);
737 					if (!intel_pll_is_valid(display,
738 								limit,
739 								&clock))
740 						continue;
741 					if (match_clock &&
742 					    clock.p != match_clock->p)
743 						continue;
744 
745 					this_err = abs(clock.dot - target);
746 					if (this_err < err) {
747 						*best_clock = clock;
748 						err = this_err;
749 					}
750 				}
751 			}
752 		}
753 	}
754 
755 	return (err != target);
756 }
757 
758 /*
759  * Returns a set of divisors for the desired target clock with the given
760  * refclk, or FALSE.
761  *
762  * Target and reference clocks are specified in kHz.
763  *
764  * If match_clock is provided, then best_clock P divider must match the P
765  * divider from @match_clock used for LVDS downclocking.
766  */
767 static bool
768 g4x_find_best_dpll(const struct intel_limit *limit,
769 		   struct intel_crtc_state *crtc_state,
770 		   int target, int refclk,
771 		   const struct dpll *match_clock,
772 		   struct dpll *best_clock)
773 {
774 	struct intel_display *display = to_intel_display(crtc_state);
775 	struct dpll clock;
776 	int max_n;
777 	bool found = false;
778 	/* approximately equals target * 0.00585 */
779 	int err_most = (target >> 8) + (target >> 9);
780 
781 	memset(best_clock, 0, sizeof(*best_clock));
782 
783 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
784 
785 	max_n = limit->n.max;
786 	/* based on hardware requirement, prefer smaller n to precision */
787 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
788 		/* based on hardware requirement, prefer larger m1,m2 */
789 		for (clock.m1 = limit->m1.max;
790 		     clock.m1 >= limit->m1.min; clock.m1--) {
791 			for (clock.m2 = limit->m2.max;
792 			     clock.m2 >= limit->m2.min; clock.m2--) {
793 				for (clock.p1 = limit->p1.max;
794 				     clock.p1 >= limit->p1.min; clock.p1--) {
795 					int this_err;
796 
797 					i9xx_calc_dpll_params(refclk, &clock);
798 					if (!intel_pll_is_valid(display,
799 								limit,
800 								&clock))
801 						continue;
802 
803 					this_err = abs(clock.dot - target);
804 					if (this_err < err_most) {
805 						*best_clock = clock;
806 						err_most = this_err;
807 						max_n = clock.n;
808 						found = true;
809 					}
810 				}
811 			}
812 		}
813 	}
814 	return found;
815 }
816 
817 /*
818  * Check if the calculated PLL configuration is more optimal compared to the
819  * best configuration and error found so far. Return the calculated error.
820  */
821 static bool vlv_PLL_is_optimal(struct intel_display *display, int target_freq,
822 			       const struct dpll *calculated_clock,
823 			       const struct dpll *best_clock,
824 			       unsigned int best_error_ppm,
825 			       unsigned int *error_ppm)
826 {
827 	/*
828 	 * For CHV ignore the error and consider only the P value.
829 	 * Prefer a bigger P value based on HW requirements.
830 	 */
831 	if (display->platform.cherryview) {
832 		*error_ppm = 0;
833 
834 		return calculated_clock->p > best_clock->p;
835 	}
836 
837 	if (drm_WARN_ON_ONCE(display->drm, !target_freq))
838 		return false;
839 
840 	*error_ppm = div_u64(1000000ULL *
841 				abs(target_freq - calculated_clock->dot),
842 			     target_freq);
843 	/*
844 	 * Prefer a better P value over a better (smaller) error if the error
845 	 * is small. Ensure this preference for future configurations too by
846 	 * setting the error to 0.
847 	 */
848 	if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
849 		*error_ppm = 0;
850 
851 		return true;
852 	}
853 
854 	return *error_ppm + 10 < best_error_ppm;
855 }
856 
857 /*
858  * Returns a set of divisors for the desired target clock with the given
859  * refclk, or FALSE.
860  */
861 static bool
862 vlv_find_best_dpll(const struct intel_limit *limit,
863 		   struct intel_crtc_state *crtc_state,
864 		   int target, int refclk,
865 		   const struct dpll *match_clock,
866 		   struct dpll *best_clock)
867 {
868 	struct intel_display *display = to_intel_display(crtc_state);
869 	struct dpll clock;
870 	unsigned int bestppm = 1000000;
871 	/* min update 19.2 MHz */
872 	int max_n = min(limit->n.max, refclk / 19200);
873 	bool found = false;
874 
875 	memset(best_clock, 0, sizeof(*best_clock));
876 
877 	/* based on hardware requirement, prefer smaller n to precision */
878 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
879 		for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
880 			for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
881 			     clock.p2 -= clock.p2 > 10 ? 2 : 1) {
882 				clock.p = clock.p1 * clock.p2 * 5;
883 				/* based on hardware requirement, prefer bigger m1,m2 values */
884 				for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
885 					unsigned int ppm;
886 
887 					clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
888 								     refclk * clock.m1);
889 
890 					vlv_calc_dpll_params(refclk, &clock);
891 
892 					if (!intel_pll_is_valid(display,
893 								limit,
894 								&clock))
895 						continue;
896 
897 					if (!vlv_PLL_is_optimal(display, target,
898 								&clock,
899 								best_clock,
900 								bestppm, &ppm))
901 						continue;
902 
903 					*best_clock = clock;
904 					bestppm = ppm;
905 					found = true;
906 				}
907 			}
908 		}
909 	}
910 
911 	return found;
912 }
913 
914 /*
915  * Returns a set of divisors for the desired target clock with the given
916  * refclk, or FALSE.
917  */
918 static bool
919 chv_find_best_dpll(const struct intel_limit *limit,
920 		   struct intel_crtc_state *crtc_state,
921 		   int target, int refclk,
922 		   const struct dpll *match_clock,
923 		   struct dpll *best_clock)
924 {
925 	struct intel_display *display = to_intel_display(crtc_state);
926 	unsigned int best_error_ppm;
927 	struct dpll clock;
928 	u64 m2;
929 	int found = false;
930 
931 	memset(best_clock, 0, sizeof(*best_clock));
932 	best_error_ppm = 1000000;
933 
934 	/*
935 	 * Based on hardware doc, the n always set to 1, and m1 always
936 	 * set to 2.  If requires to support 200Mhz refclk, we need to
937 	 * revisit this because n may not 1 anymore.
938 	 */
939 	clock.n = 1;
940 	clock.m1 = 2;
941 
942 	for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
943 		for (clock.p2 = limit->p2.p2_fast;
944 				clock.p2 >= limit->p2.p2_slow;
945 				clock.p2 -= clock.p2 > 10 ? 2 : 1) {
946 			unsigned int error_ppm;
947 
948 			clock.p = clock.p1 * clock.p2 * 5;
949 
950 			m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
951 						   refclk * clock.m1);
952 
953 			if (m2 > INT_MAX/clock.m1)
954 				continue;
955 
956 			clock.m2 = m2;
957 
958 			chv_calc_dpll_params(refclk, &clock);
959 
960 			if (!intel_pll_is_valid(display, limit, &clock))
961 				continue;
962 
963 			if (!vlv_PLL_is_optimal(display, target, &clock, best_clock,
964 						best_error_ppm, &error_ppm))
965 				continue;
966 
967 			*best_clock = clock;
968 			best_error_ppm = error_ppm;
969 			found = true;
970 		}
971 	}
972 
973 	return found;
974 }
975 
976 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
977 			struct dpll *best_clock)
978 {
979 	const struct intel_limit *limit = &intel_limits_bxt;
980 	int refclk = 100000;
981 
982 	return chv_find_best_dpll(limit, crtc_state,
983 				  crtc_state->port_clock, refclk,
984 				  NULL, best_clock);
985 }
986 
987 u32 i9xx_dpll_compute_fp(const struct dpll *dpll)
988 {
989 	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
990 }
991 
992 static u32 pnv_dpll_compute_fp(const struct dpll *dpll)
993 {
994 	return (1 << dpll->n) << 16 | dpll->m2;
995 }
996 
997 static u32 i965_dpll_md(const struct intel_crtc_state *crtc_state)
998 {
999 	return (crtc_state->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1000 }
1001 
1002 static u32 i9xx_dpll(const struct intel_crtc_state *crtc_state,
1003 		     const struct dpll *clock,
1004 		     const struct dpll *reduced_clock)
1005 {
1006 	struct intel_display *display = to_intel_display(crtc_state);
1007 	u32 dpll;
1008 
1009 	dpll = DPLL_VCO_ENABLE | DPLL_VGA_MODE_DIS;
1010 
1011 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
1012 		dpll |= DPLLB_MODE_LVDS;
1013 	else
1014 		dpll |= DPLLB_MODE_DAC_SERIAL;
1015 
1016 	if (display->platform.i945g || display->platform.i945gm ||
1017 	    display->platform.g33 || display->platform.pineview) {
1018 		dpll |= (crtc_state->pixel_multiplier - 1)
1019 			<< SDVO_MULTIPLIER_SHIFT_HIRES;
1020 	}
1021 
1022 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
1023 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1024 		dpll |= DPLL_SDVO_HIGH_SPEED;
1025 
1026 	if (intel_crtc_has_dp_encoder(crtc_state))
1027 		dpll |= DPLL_SDVO_HIGH_SPEED;
1028 
1029 	/* compute bitmask from p1 value */
1030 	if (display->platform.g4x) {
1031 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
1032 		dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
1033 	} else if (display->platform.pineview) {
1034 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
1035 		WARN_ON(reduced_clock->p1 != clock->p1);
1036 	} else {
1037 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
1038 		WARN_ON(reduced_clock->p1 != clock->p1);
1039 	}
1040 
1041 	switch (clock->p2) {
1042 	case 5:
1043 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
1044 		break;
1045 	case 7:
1046 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
1047 		break;
1048 	case 10:
1049 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
1050 		break;
1051 	case 14:
1052 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
1053 		break;
1054 	}
1055 	WARN_ON(reduced_clock->p2 != clock->p2);
1056 
1057 	if (DISPLAY_VER(display) >= 4)
1058 		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
1059 
1060 	if (crtc_state->sdvo_tv_clock)
1061 		dpll |= PLL_REF_INPUT_TVCLKINBC;
1062 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
1063 		 intel_panel_use_ssc(display))
1064 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
1065 	else
1066 		dpll |= PLL_REF_INPUT_DREFCLK;
1067 
1068 	return dpll;
1069 }
1070 
1071 static void i9xx_compute_dpll(struct intel_crtc_state *crtc_state,
1072 			      const struct dpll *clock,
1073 			      const struct dpll *reduced_clock)
1074 {
1075 	struct intel_display *display = to_intel_display(crtc_state);
1076 	struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
1077 
1078 	if (display->platform.pineview) {
1079 		hw_state->fp0 = pnv_dpll_compute_fp(clock);
1080 		hw_state->fp1 = pnv_dpll_compute_fp(reduced_clock);
1081 	} else {
1082 		hw_state->fp0 = i9xx_dpll_compute_fp(clock);
1083 		hw_state->fp1 = i9xx_dpll_compute_fp(reduced_clock);
1084 	}
1085 
1086 	hw_state->dpll = i9xx_dpll(crtc_state, clock, reduced_clock);
1087 
1088 	if (DISPLAY_VER(display) >= 4)
1089 		hw_state->dpll_md = i965_dpll_md(crtc_state);
1090 }
1091 
1092 static u32 i8xx_dpll(const struct intel_crtc_state *crtc_state,
1093 		     const struct dpll *clock,
1094 		     const struct dpll *reduced_clock)
1095 {
1096 	struct intel_display *display = to_intel_display(crtc_state);
1097 	u32 dpll;
1098 
1099 	dpll = DPLL_VCO_ENABLE | DPLL_VGA_MODE_DIS;
1100 
1101 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1102 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
1103 	} else {
1104 		if (clock->p1 == 2)
1105 			dpll |= PLL_P1_DIVIDE_BY_TWO;
1106 		else
1107 			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
1108 		if (clock->p2 == 4)
1109 			dpll |= PLL_P2_DIVIDE_BY_4;
1110 	}
1111 	WARN_ON(reduced_clock->p1 != clock->p1);
1112 	WARN_ON(reduced_clock->p2 != clock->p2);
1113 
1114 	/*
1115 	 * Bspec:
1116 	 * "[Almador Errata}: For the correct operation of the muxed DVO pins
1117 	 *  (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
1118 	 *  GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
1119 	 *  Enable) must be set to “1” in both the DPLL A Control Register
1120 	 *  (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
1121 	 *
1122 	 * For simplicity We simply keep both bits always enabled in
1123 	 * both DPLLS. The spec says we should disable the DVO 2X clock
1124 	 * when not needed, but this seems to work fine in practice.
1125 	 */
1126 	if (display->platform.i830 ||
1127 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
1128 		dpll |= DPLL_DVO_2X_MODE;
1129 
1130 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
1131 	    intel_panel_use_ssc(display))
1132 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
1133 	else
1134 		dpll |= PLL_REF_INPUT_DREFCLK;
1135 
1136 	return dpll;
1137 }
1138 
1139 static void i8xx_compute_dpll(struct intel_crtc_state *crtc_state,
1140 			      const struct dpll *clock,
1141 			      const struct dpll *reduced_clock)
1142 {
1143 	struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
1144 
1145 	hw_state->fp0 = i9xx_dpll_compute_fp(clock);
1146 	hw_state->fp1 = i9xx_dpll_compute_fp(reduced_clock);
1147 
1148 	hw_state->dpll = i8xx_dpll(crtc_state, clock, reduced_clock);
1149 }
1150 
1151 static int hsw_crtc_compute_clock(struct intel_atomic_state *state,
1152 				  struct intel_crtc *crtc)
1153 {
1154 	struct intel_display *display = to_intel_display(state);
1155 	struct intel_crtc_state *crtc_state =
1156 		intel_atomic_get_new_crtc_state(state, crtc);
1157 	struct intel_encoder *encoder =
1158 		intel_get_crtc_new_encoder(state, crtc_state);
1159 	int ret;
1160 
1161 	if (DISPLAY_VER(display) < 11 &&
1162 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1163 		return 0;
1164 
1165 	ret = intel_dpll_compute(state, crtc, encoder);
1166 	if (ret)
1167 		return ret;
1168 
1169 	/* FIXME this is a mess */
1170 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1171 		return 0;
1172 
1173 	/* CRT dotclock is determined via other means */
1174 	if (!crtc_state->has_pch_encoder)
1175 		crtc_state->hw.adjusted_mode.crtc_clock = intel_crtc_dotclock(crtc_state);
1176 
1177 	return 0;
1178 }
1179 
1180 static int hsw_crtc_get_dpll(struct intel_atomic_state *state,
1181 			     struct intel_crtc *crtc)
1182 {
1183 	struct intel_display *display = to_intel_display(state);
1184 	struct intel_crtc_state *crtc_state =
1185 		intel_atomic_get_new_crtc_state(state, crtc);
1186 	struct intel_encoder *encoder =
1187 		intel_get_crtc_new_encoder(state, crtc_state);
1188 
1189 	if (DISPLAY_VER(display) < 11 &&
1190 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1191 		return 0;
1192 
1193 	return intel_dpll_reserve(state, crtc, encoder);
1194 }
1195 
1196 static int ilk_fb_cb_factor(const struct intel_crtc_state *crtc_state)
1197 {
1198 	struct intel_display *display = to_intel_display(crtc_state);
1199 
1200 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
1201 	    ((intel_panel_use_ssc(display) && display->vbt.lvds_ssc_freq == 100000) ||
1202 	     (HAS_PCH_IBX(display) && intel_is_dual_link_lvds(display))))
1203 		return 25;
1204 
1205 	if (crtc_state->sdvo_tv_clock)
1206 		return 20;
1207 
1208 	return 21;
1209 }
1210 
1211 static bool ilk_needs_fb_cb_tune(const struct dpll *dpll, int factor)
1212 {
1213 	return dpll->m < factor * dpll->n;
1214 }
1215 
1216 static u32 ilk_dpll_compute_fp(const struct dpll *clock, int factor)
1217 {
1218 	u32 fp;
1219 
1220 	fp = i9xx_dpll_compute_fp(clock);
1221 	if (ilk_needs_fb_cb_tune(clock, factor))
1222 		fp |= FP_CB_TUNE;
1223 
1224 	return fp;
1225 }
1226 
1227 static u32 ilk_dpll(const struct intel_crtc_state *crtc_state,
1228 		    const struct dpll *clock,
1229 		    const struct dpll *reduced_clock)
1230 {
1231 	struct intel_display *display = to_intel_display(crtc_state);
1232 	u32 dpll;
1233 
1234 	dpll = DPLL_VCO_ENABLE;
1235 
1236 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
1237 		dpll |= DPLLB_MODE_LVDS;
1238 	else
1239 		dpll |= DPLLB_MODE_DAC_SERIAL;
1240 
1241 	dpll |= (crtc_state->pixel_multiplier - 1)
1242 		<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
1243 
1244 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
1245 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1246 		dpll |= DPLL_SDVO_HIGH_SPEED;
1247 
1248 	if (intel_crtc_has_dp_encoder(crtc_state))
1249 		dpll |= DPLL_SDVO_HIGH_SPEED;
1250 
1251 	/*
1252 	 * The high speed IO clock is only really required for
1253 	 * SDVO/HDMI/DP, but we also enable it for CRT to make it
1254 	 * possible to share the DPLL between CRT and HDMI. Enabling
1255 	 * the clock needlessly does no real harm, except use up a
1256 	 * bit of power potentially.
1257 	 *
1258 	 * We'll limit this to IVB with 3 pipes, since it has only two
1259 	 * DPLLs and so DPLL sharing is the only way to get three pipes
1260 	 * driving PCH ports at the same time. On SNB we could do this,
1261 	 * and potentially avoid enabling the second DPLL, but it's not
1262 	 * clear if it''s a win or loss power wise. No point in doing
1263 	 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
1264 	 */
1265 	if (INTEL_NUM_PIPES(display) == 3 &&
1266 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
1267 		dpll |= DPLL_SDVO_HIGH_SPEED;
1268 
1269 	/* compute bitmask from p1 value */
1270 	dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
1271 	/* also FPA1 */
1272 	dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
1273 
1274 	switch (clock->p2) {
1275 	case 5:
1276 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
1277 		break;
1278 	case 7:
1279 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
1280 		break;
1281 	case 10:
1282 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
1283 		break;
1284 	case 14:
1285 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
1286 		break;
1287 	}
1288 	WARN_ON(reduced_clock->p2 != clock->p2);
1289 
1290 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
1291 	    intel_panel_use_ssc(display))
1292 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
1293 	else
1294 		dpll |= PLL_REF_INPUT_DREFCLK;
1295 
1296 	return dpll;
1297 }
1298 
1299 static void ilk_compute_dpll(struct intel_crtc_state *crtc_state,
1300 			     const struct dpll *clock,
1301 			     const struct dpll *reduced_clock)
1302 {
1303 	struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
1304 	int factor = ilk_fb_cb_factor(crtc_state);
1305 
1306 	hw_state->fp0 = ilk_dpll_compute_fp(clock, factor);
1307 	hw_state->fp1 = ilk_dpll_compute_fp(reduced_clock, factor);
1308 
1309 	hw_state->dpll = ilk_dpll(crtc_state, clock, reduced_clock);
1310 }
1311 
1312 static int ilk_crtc_compute_clock(struct intel_atomic_state *state,
1313 				  struct intel_crtc *crtc)
1314 {
1315 	struct intel_display *display = to_intel_display(state);
1316 	struct intel_crtc_state *crtc_state =
1317 		intel_atomic_get_new_crtc_state(state, crtc);
1318 	const struct intel_limit *limit;
1319 	int refclk = 120000;
1320 	int ret;
1321 
1322 	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
1323 	if (!crtc_state->has_pch_encoder)
1324 		return 0;
1325 
1326 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1327 		if (intel_panel_use_ssc(display)) {
1328 			drm_dbg_kms(display->drm,
1329 				    "using SSC reference clock of %d kHz\n",
1330 				    display->vbt.lvds_ssc_freq);
1331 			refclk = display->vbt.lvds_ssc_freq;
1332 		}
1333 
1334 		if (intel_is_dual_link_lvds(display)) {
1335 			if (refclk == 100000)
1336 				limit = &ilk_limits_dual_lvds_100m;
1337 			else
1338 				limit = &ilk_limits_dual_lvds;
1339 		} else {
1340 			if (refclk == 100000)
1341 				limit = &ilk_limits_single_lvds_100m;
1342 			else
1343 				limit = &ilk_limits_single_lvds;
1344 		}
1345 	} else {
1346 		limit = &ilk_limits_dac;
1347 	}
1348 
1349 	if (!crtc_state->clock_set &&
1350 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1351 				refclk, NULL, &crtc_state->dpll))
1352 		return -EINVAL;
1353 
1354 	i9xx_calc_dpll_params(refclk, &crtc_state->dpll);
1355 
1356 	ilk_compute_dpll(crtc_state, &crtc_state->dpll,
1357 			 &crtc_state->dpll);
1358 
1359 	ret = intel_dpll_compute(state, crtc, NULL);
1360 	if (ret)
1361 		return ret;
1362 
1363 	crtc_state->port_clock = crtc_state->dpll.dot;
1364 	crtc_state->hw.adjusted_mode.crtc_clock = intel_crtc_dotclock(crtc_state);
1365 
1366 	return ret;
1367 }
1368 
1369 static int ilk_crtc_get_dpll(struct intel_atomic_state *state,
1370 			     struct intel_crtc *crtc)
1371 {
1372 	struct intel_crtc_state *crtc_state =
1373 		intel_atomic_get_new_crtc_state(state, crtc);
1374 
1375 	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
1376 	if (!crtc_state->has_pch_encoder)
1377 		return 0;
1378 
1379 	return intel_dpll_reserve(state, crtc, NULL);
1380 }
1381 
1382 static u32 vlv_dpll(const struct intel_crtc_state *crtc_state)
1383 {
1384 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1385 	u32 dpll;
1386 
1387 	dpll = DPLL_INTEGRATED_REF_CLK_VLV |
1388 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1389 
1390 	if (crtc->pipe != PIPE_A)
1391 		dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1392 
1393 	/* DPLL not used with DSI, but still need the rest set up */
1394 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1395 		dpll |= DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV;
1396 
1397 	return dpll;
1398 }
1399 
1400 void vlv_compute_dpll(struct intel_crtc_state *crtc_state)
1401 {
1402 	struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
1403 
1404 	hw_state->dpll = vlv_dpll(crtc_state);
1405 	hw_state->dpll_md = i965_dpll_md(crtc_state);
1406 }
1407 
1408 static u32 chv_dpll(const struct intel_crtc_state *crtc_state)
1409 {
1410 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1411 	u32 dpll;
1412 
1413 	dpll = DPLL_SSC_REF_CLK_CHV |
1414 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1415 
1416 	if (crtc->pipe != PIPE_A)
1417 		dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1418 
1419 	/* DPLL not used with DSI, but still need the rest set up */
1420 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1421 		dpll |= DPLL_VCO_ENABLE;
1422 
1423 	return dpll;
1424 }
1425 
1426 void chv_compute_dpll(struct intel_crtc_state *crtc_state)
1427 {
1428 	struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
1429 
1430 	hw_state->dpll = chv_dpll(crtc_state);
1431 	hw_state->dpll_md = i965_dpll_md(crtc_state);
1432 }
1433 
1434 static int chv_crtc_compute_clock(struct intel_atomic_state *state,
1435 				  struct intel_crtc *crtc)
1436 {
1437 	struct intel_crtc_state *crtc_state =
1438 		intel_atomic_get_new_crtc_state(state, crtc);
1439 	const struct intel_limit *limit = &intel_limits_chv;
1440 	int refclk = 100000;
1441 
1442 	if (!crtc_state->clock_set &&
1443 	    !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1444 				refclk, NULL, &crtc_state->dpll))
1445 		return -EINVAL;
1446 
1447 	chv_calc_dpll_params(refclk, &crtc_state->dpll);
1448 
1449 	chv_compute_dpll(crtc_state);
1450 
1451 	/* FIXME this is a mess */
1452 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1453 		return 0;
1454 
1455 	crtc_state->port_clock = crtc_state->dpll.dot;
1456 	crtc_state->hw.adjusted_mode.crtc_clock = intel_crtc_dotclock(crtc_state);
1457 
1458 	return 0;
1459 }
1460 
1461 static int vlv_crtc_compute_clock(struct intel_atomic_state *state,
1462 				  struct intel_crtc *crtc)
1463 {
1464 	struct intel_crtc_state *crtc_state =
1465 		intel_atomic_get_new_crtc_state(state, crtc);
1466 	const struct intel_limit *limit = &intel_limits_vlv;
1467 	int refclk = 100000;
1468 
1469 	if (!crtc_state->clock_set &&
1470 	    !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1471 				refclk, NULL, &crtc_state->dpll))
1472 		return -EINVAL;
1473 
1474 	vlv_calc_dpll_params(refclk, &crtc_state->dpll);
1475 
1476 	vlv_compute_dpll(crtc_state);
1477 
1478 	/* FIXME this is a mess */
1479 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1480 		return 0;
1481 
1482 	crtc_state->port_clock = crtc_state->dpll.dot;
1483 	crtc_state->hw.adjusted_mode.crtc_clock = intel_crtc_dotclock(crtc_state);
1484 
1485 	return 0;
1486 }
1487 
1488 static int g4x_crtc_compute_clock(struct intel_atomic_state *state,
1489 				  struct intel_crtc *crtc)
1490 {
1491 	struct intel_display *display = to_intel_display(state);
1492 	struct intel_crtc_state *crtc_state =
1493 		intel_atomic_get_new_crtc_state(state, crtc);
1494 	const struct intel_limit *limit;
1495 	int refclk = 96000;
1496 
1497 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1498 		if (intel_panel_use_ssc(display)) {
1499 			refclk = display->vbt.lvds_ssc_freq;
1500 			drm_dbg_kms(display->drm,
1501 				    "using SSC reference clock of %d kHz\n",
1502 				    refclk);
1503 		}
1504 
1505 		if (intel_is_dual_link_lvds(display))
1506 			limit = &intel_limits_g4x_dual_channel_lvds;
1507 		else
1508 			limit = &intel_limits_g4x_single_channel_lvds;
1509 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
1510 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
1511 		limit = &intel_limits_g4x_hdmi;
1512 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
1513 		limit = &intel_limits_g4x_sdvo;
1514 	} else {
1515 		/* The option is for other outputs */
1516 		limit = &intel_limits_i9xx_sdvo;
1517 	}
1518 
1519 	if (!crtc_state->clock_set &&
1520 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1521 				refclk, NULL, &crtc_state->dpll))
1522 		return -EINVAL;
1523 
1524 	i9xx_calc_dpll_params(refclk, &crtc_state->dpll);
1525 
1526 	i9xx_compute_dpll(crtc_state, &crtc_state->dpll,
1527 			  &crtc_state->dpll);
1528 
1529 	crtc_state->port_clock = crtc_state->dpll.dot;
1530 	/* FIXME this is a mess */
1531 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_TVOUT))
1532 		crtc_state->hw.adjusted_mode.crtc_clock = intel_crtc_dotclock(crtc_state);
1533 
1534 	return 0;
1535 }
1536 
1537 static int pnv_crtc_compute_clock(struct intel_atomic_state *state,
1538 				  struct intel_crtc *crtc)
1539 {
1540 	struct intel_display *display = to_intel_display(state);
1541 	struct intel_crtc_state *crtc_state =
1542 		intel_atomic_get_new_crtc_state(state, crtc);
1543 	const struct intel_limit *limit;
1544 	int refclk = 96000;
1545 
1546 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1547 		if (intel_panel_use_ssc(display)) {
1548 			refclk = display->vbt.lvds_ssc_freq;
1549 			drm_dbg_kms(display->drm,
1550 				    "using SSC reference clock of %d kHz\n",
1551 				    refclk);
1552 		}
1553 
1554 		limit = &pnv_limits_lvds;
1555 	} else {
1556 		limit = &pnv_limits_sdvo;
1557 	}
1558 
1559 	if (!crtc_state->clock_set &&
1560 	    !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1561 				refclk, NULL, &crtc_state->dpll))
1562 		return -EINVAL;
1563 
1564 	pnv_calc_dpll_params(refclk, &crtc_state->dpll);
1565 
1566 	i9xx_compute_dpll(crtc_state, &crtc_state->dpll,
1567 			  &crtc_state->dpll);
1568 
1569 	crtc_state->port_clock = crtc_state->dpll.dot;
1570 	crtc_state->hw.adjusted_mode.crtc_clock = intel_crtc_dotclock(crtc_state);
1571 
1572 	return 0;
1573 }
1574 
1575 static int i9xx_crtc_compute_clock(struct intel_atomic_state *state,
1576 				   struct intel_crtc *crtc)
1577 {
1578 	struct intel_display *display = to_intel_display(state);
1579 	struct intel_crtc_state *crtc_state =
1580 		intel_atomic_get_new_crtc_state(state, crtc);
1581 	const struct intel_limit *limit;
1582 	int refclk = 96000;
1583 
1584 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1585 		if (intel_panel_use_ssc(display)) {
1586 			refclk = display->vbt.lvds_ssc_freq;
1587 			drm_dbg_kms(display->drm,
1588 				    "using SSC reference clock of %d kHz\n",
1589 				    refclk);
1590 		}
1591 
1592 		limit = &intel_limits_i9xx_lvds;
1593 	} else {
1594 		limit = &intel_limits_i9xx_sdvo;
1595 	}
1596 
1597 	if (!crtc_state->clock_set &&
1598 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1599 				 refclk, NULL, &crtc_state->dpll))
1600 		return -EINVAL;
1601 
1602 	i9xx_calc_dpll_params(refclk, &crtc_state->dpll);
1603 
1604 	i9xx_compute_dpll(crtc_state, &crtc_state->dpll,
1605 			  &crtc_state->dpll);
1606 
1607 	crtc_state->port_clock = crtc_state->dpll.dot;
1608 	/* FIXME this is a mess */
1609 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_TVOUT))
1610 		crtc_state->hw.adjusted_mode.crtc_clock = intel_crtc_dotclock(crtc_state);
1611 
1612 	return 0;
1613 }
1614 
1615 static int i8xx_crtc_compute_clock(struct intel_atomic_state *state,
1616 				   struct intel_crtc *crtc)
1617 {
1618 	struct intel_display *display = to_intel_display(state);
1619 	struct intel_crtc_state *crtc_state =
1620 		intel_atomic_get_new_crtc_state(state, crtc);
1621 	const struct intel_limit *limit;
1622 	int refclk = 48000;
1623 
1624 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1625 		if (intel_panel_use_ssc(display)) {
1626 			refclk = display->vbt.lvds_ssc_freq;
1627 			drm_dbg_kms(display->drm,
1628 				    "using SSC reference clock of %d kHz\n",
1629 				    refclk);
1630 		}
1631 
1632 		limit = &intel_limits_i8xx_lvds;
1633 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
1634 		limit = &intel_limits_i8xx_dvo;
1635 	} else {
1636 		limit = &intel_limits_i8xx_dac;
1637 	}
1638 
1639 	if (!crtc_state->clock_set &&
1640 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1641 				 refclk, NULL, &crtc_state->dpll))
1642 		return -EINVAL;
1643 
1644 	i9xx_calc_dpll_params(refclk, &crtc_state->dpll);
1645 
1646 	i8xx_compute_dpll(crtc_state, &crtc_state->dpll,
1647 			  &crtc_state->dpll);
1648 
1649 	crtc_state->port_clock = crtc_state->dpll.dot;
1650 	crtc_state->hw.adjusted_mode.crtc_clock = intel_crtc_dotclock(crtc_state);
1651 
1652 	return 0;
1653 }
1654 
1655 static const struct intel_dpll_global_funcs xe3plpd_dpll_funcs = {
1656 	.crtc_compute_clock = hsw_crtc_compute_clock,
1657 	.crtc_get_dpll = hsw_crtc_get_dpll,
1658 };
1659 
1660 static const struct intel_dpll_global_funcs mtl_dpll_funcs = {
1661 	.crtc_compute_clock = hsw_crtc_compute_clock,
1662 	.crtc_get_dpll = hsw_crtc_get_dpll,
1663 };
1664 
1665 static const struct intel_dpll_global_funcs dg2_dpll_funcs = {
1666 	.crtc_compute_clock = hsw_crtc_compute_clock,
1667 	.crtc_get_dpll = hsw_crtc_get_dpll,
1668 };
1669 
1670 static const struct intel_dpll_global_funcs hsw_dpll_funcs = {
1671 	.crtc_compute_clock = hsw_crtc_compute_clock,
1672 	.crtc_get_dpll = hsw_crtc_get_dpll,
1673 };
1674 
1675 static const struct intel_dpll_global_funcs ilk_dpll_funcs = {
1676 	.crtc_compute_clock = ilk_crtc_compute_clock,
1677 	.crtc_get_dpll = ilk_crtc_get_dpll,
1678 };
1679 
1680 static const struct intel_dpll_global_funcs chv_dpll_funcs = {
1681 	.crtc_compute_clock = chv_crtc_compute_clock,
1682 };
1683 
1684 static const struct intel_dpll_global_funcs vlv_dpll_funcs = {
1685 	.crtc_compute_clock = vlv_crtc_compute_clock,
1686 };
1687 
1688 static const struct intel_dpll_global_funcs g4x_dpll_funcs = {
1689 	.crtc_compute_clock = g4x_crtc_compute_clock,
1690 };
1691 
1692 static const struct intel_dpll_global_funcs pnv_dpll_funcs = {
1693 	.crtc_compute_clock = pnv_crtc_compute_clock,
1694 };
1695 
1696 static const struct intel_dpll_global_funcs i9xx_dpll_funcs = {
1697 	.crtc_compute_clock = i9xx_crtc_compute_clock,
1698 };
1699 
1700 static const struct intel_dpll_global_funcs i8xx_dpll_funcs = {
1701 	.crtc_compute_clock = i8xx_crtc_compute_clock,
1702 };
1703 
1704 int intel_dpll_crtc_compute_clock(struct intel_atomic_state *state,
1705 				  struct intel_crtc *crtc)
1706 {
1707 	struct intel_display *display = to_intel_display(state);
1708 	struct intel_crtc_state *crtc_state =
1709 		intel_atomic_get_new_crtc_state(state, crtc);
1710 	int ret;
1711 
1712 	drm_WARN_ON(display->drm, !intel_crtc_needs_modeset(crtc_state));
1713 
1714 	memset(&crtc_state->dpll_hw_state, 0,
1715 	       sizeof(crtc_state->dpll_hw_state));
1716 
1717 	if (!crtc_state->hw.enable)
1718 		return 0;
1719 
1720 	ret = display->dpll.funcs->crtc_compute_clock(state, crtc);
1721 	if (ret) {
1722 		drm_dbg_kms(display->drm, "[CRTC:%d:%s] Couldn't calculate DPLL settings\n",
1723 			    crtc->base.base.id, crtc->base.name);
1724 		return ret;
1725 	}
1726 
1727 	return 0;
1728 }
1729 
1730 int intel_dpll_crtc_get_dpll(struct intel_atomic_state *state,
1731 			     struct intel_crtc *crtc)
1732 {
1733 	struct intel_display *display = to_intel_display(state);
1734 	struct intel_crtc_state *crtc_state =
1735 		intel_atomic_get_new_crtc_state(state, crtc);
1736 	int ret;
1737 
1738 	drm_WARN_ON(display->drm, !intel_crtc_needs_modeset(crtc_state));
1739 	drm_WARN_ON(display->drm, !crtc_state->hw.enable && crtc_state->intel_dpll);
1740 
1741 	if (!crtc_state->hw.enable || crtc_state->intel_dpll)
1742 		return 0;
1743 
1744 	if (!display->dpll.funcs->crtc_get_dpll)
1745 		return 0;
1746 
1747 	ret = display->dpll.funcs->crtc_get_dpll(state, crtc);
1748 	if (ret) {
1749 		drm_dbg_kms(display->drm, "[CRTC:%d:%s] Couldn't get a shared DPLL\n",
1750 			    crtc->base.base.id, crtc->base.name);
1751 		return ret;
1752 	}
1753 
1754 	return 0;
1755 }
1756 
1757 void
1758 intel_dpll_init_clock_hook(struct intel_display *display)
1759 {
1760 	if (HAS_LT_PHY(display))
1761 		display->dpll.funcs = &xe3plpd_dpll_funcs;
1762 	else if (DISPLAY_VER(display) >= 14)
1763 		display->dpll.funcs = &mtl_dpll_funcs;
1764 	else if (display->platform.dg2)
1765 		display->dpll.funcs = &dg2_dpll_funcs;
1766 	else if (DISPLAY_VER(display) >= 9 || HAS_DDI(display))
1767 		display->dpll.funcs = &hsw_dpll_funcs;
1768 	else if (HAS_PCH_SPLIT(display))
1769 		display->dpll.funcs = &ilk_dpll_funcs;
1770 	else if (display->platform.cherryview)
1771 		display->dpll.funcs = &chv_dpll_funcs;
1772 	else if (display->platform.valleyview)
1773 		display->dpll.funcs = &vlv_dpll_funcs;
1774 	else if (display->platform.g4x)
1775 		display->dpll.funcs = &g4x_dpll_funcs;
1776 	else if (display->platform.pineview)
1777 		display->dpll.funcs = &pnv_dpll_funcs;
1778 	else if (DISPLAY_VER(display) != 2)
1779 		display->dpll.funcs = &i9xx_dpll_funcs;
1780 	else
1781 		display->dpll.funcs = &i8xx_dpll_funcs;
1782 }
1783 
1784 static bool i9xx_has_pps(struct intel_display *display)
1785 {
1786 	if (display->platform.i830)
1787 		return false;
1788 
1789 	return display->platform.pineview || display->platform.mobile;
1790 }
1791 
1792 void i9xx_enable_pll(const struct intel_crtc_state *crtc_state)
1793 {
1794 	struct intel_display *display = to_intel_display(crtc_state);
1795 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1796 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
1797 	enum pipe pipe = crtc->pipe;
1798 	int i;
1799 
1800 	assert_transcoder_disabled(display, crtc_state->cpu_transcoder);
1801 
1802 	/* PLL is protected by panel, make sure we can write it */
1803 	if (i9xx_has_pps(display))
1804 		assert_pps_unlocked(display, pipe);
1805 
1806 	intel_de_write(display, FP0(pipe), hw_state->fp0);
1807 	intel_de_write(display, FP1(pipe), hw_state->fp1);
1808 
1809 	/*
1810 	 * Apparently we need to have VGA mode enabled prior to changing
1811 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1812 	 * dividers, even though the register value does change.
1813 	 */
1814 	intel_de_write(display, DPLL(display, pipe),
1815 		       hw_state->dpll & ~DPLL_VGA_MODE_DIS);
1816 	intel_de_write(display, DPLL(display, pipe), hw_state->dpll);
1817 
1818 	/* Wait for the clocks to stabilize. */
1819 	intel_de_posting_read(display, DPLL(display, pipe));
1820 	udelay(150);
1821 
1822 	if (DISPLAY_VER(display) >= 4) {
1823 		intel_de_write(display, DPLL_MD(display, pipe),
1824 			       hw_state->dpll_md);
1825 	} else {
1826 		/* The pixel multiplier can only be updated once the
1827 		 * DPLL is enabled and the clocks are stable.
1828 		 *
1829 		 * So write it again.
1830 		 */
1831 		intel_de_write(display, DPLL(display, pipe), hw_state->dpll);
1832 	}
1833 
1834 	/* We do this three times for luck */
1835 	for (i = 0; i < 3; i++) {
1836 		intel_de_write(display, DPLL(display, pipe), hw_state->dpll);
1837 		intel_de_posting_read(display, DPLL(display, pipe));
1838 		udelay(150); /* wait for warmup */
1839 	}
1840 }
1841 
1842 static void vlv_pllb_recal_opamp(struct intel_display *display,
1843 				 enum dpio_phy phy, enum dpio_channel ch)
1844 {
1845 	u32 tmp;
1846 
1847 	/*
1848 	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
1849 	 * and set it to a reasonable value instead.
1850 	 */
1851 	tmp = vlv_dpio_read(display, phy, VLV_PLL_DW17(ch));
1852 	tmp &= 0xffffff00;
1853 	tmp |= 0x00000030;
1854 	vlv_dpio_write(display, phy, VLV_PLL_DW17(ch), tmp);
1855 
1856 	tmp = vlv_dpio_read(display, phy, VLV_REF_DW11);
1857 	tmp &= 0x00ffffff;
1858 	tmp |= 0x8c000000;
1859 	vlv_dpio_write(display, phy, VLV_REF_DW11, tmp);
1860 
1861 	tmp = vlv_dpio_read(display, phy, VLV_PLL_DW17(ch));
1862 	tmp &= 0xffffff00;
1863 	vlv_dpio_write(display, phy, VLV_PLL_DW17(ch), tmp);
1864 
1865 	tmp = vlv_dpio_read(display, phy, VLV_REF_DW11);
1866 	tmp &= 0x00ffffff;
1867 	tmp |= 0xb0000000;
1868 	vlv_dpio_write(display, phy, VLV_REF_DW11, tmp);
1869 }
1870 
1871 static void vlv_prepare_pll(const struct intel_crtc_state *crtc_state)
1872 {
1873 	struct intel_display *display = to_intel_display(crtc_state);
1874 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1875 	const struct dpll *clock = &crtc_state->dpll;
1876 	enum dpio_channel ch = vlv_pipe_to_channel(crtc->pipe);
1877 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
1878 	enum pipe pipe = crtc->pipe;
1879 	u32 tmp, coreclk;
1880 
1881 	vlv_dpio_get(display);
1882 
1883 	/* See eDP HDMI DPIO driver vbios notes doc */
1884 
1885 	/* PLL B needs special handling */
1886 	if (pipe == PIPE_B)
1887 		vlv_pllb_recal_opamp(display, phy, ch);
1888 
1889 	/* Set up Tx target for periodic Rcomp update */
1890 	vlv_dpio_write(display, phy, VLV_PCS_DW17_BCAST, 0x0100000f);
1891 
1892 	/* Disable target IRef on PLL */
1893 	tmp = vlv_dpio_read(display, phy, VLV_PLL_DW16(ch));
1894 	tmp &= 0x00ffffff;
1895 	vlv_dpio_write(display, phy, VLV_PLL_DW16(ch), tmp);
1896 
1897 	/* Disable fast lock */
1898 	vlv_dpio_write(display, phy, VLV_CMN_DW0, 0x610);
1899 
1900 	/* Set idtafcrecal before PLL is enabled */
1901 	tmp = DPIO_M1_DIV(clock->m1) |
1902 		DPIO_M2_DIV(clock->m2) |
1903 		DPIO_P1_DIV(clock->p1) |
1904 		DPIO_P2_DIV(clock->p2) |
1905 		DPIO_N_DIV(clock->n) |
1906 		DPIO_K_DIV(1);
1907 
1908 	/*
1909 	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
1910 	 * but we don't support that).
1911 	 * Note: don't use the DAC post divider as it seems unstable.
1912 	 */
1913 	tmp |= DPIO_S1_DIV(DPIO_S1_DIV_HDMIDP);
1914 	vlv_dpio_write(display, phy, VLV_PLL_DW3(ch), tmp);
1915 
1916 	tmp |= DPIO_ENABLE_CALIBRATION;
1917 	vlv_dpio_write(display, phy, VLV_PLL_DW3(ch), tmp);
1918 
1919 	/* Set HBR and RBR LPF coefficients */
1920 	if (crtc_state->port_clock == 162000 ||
1921 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG) ||
1922 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1923 		vlv_dpio_write(display, phy, VLV_PLL_DW18(ch), 0x009f0003);
1924 	else
1925 		vlv_dpio_write(display, phy, VLV_PLL_DW18(ch), 0x00d0000f);
1926 
1927 	if (intel_crtc_has_dp_encoder(crtc_state)) {
1928 		/* Use SSC source */
1929 		if (pipe == PIPE_A)
1930 			vlv_dpio_write(display, phy, VLV_PLL_DW5(ch), 0x0df40000);
1931 		else
1932 			vlv_dpio_write(display, phy, VLV_PLL_DW5(ch), 0x0df70000);
1933 	} else { /* HDMI or VGA */
1934 		/* Use bend source */
1935 		if (pipe == PIPE_A)
1936 			vlv_dpio_write(display, phy, VLV_PLL_DW5(ch), 0x0df70000);
1937 		else
1938 			vlv_dpio_write(display, phy, VLV_PLL_DW5(ch), 0x0df40000);
1939 	}
1940 
1941 	coreclk = vlv_dpio_read(display, phy, VLV_PLL_DW7(ch));
1942 	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
1943 	if (intel_crtc_has_dp_encoder(crtc_state))
1944 		coreclk |= 0x01000000;
1945 	vlv_dpio_write(display, phy, VLV_PLL_DW7(ch), coreclk);
1946 
1947 	vlv_dpio_write(display, phy, VLV_PLL_DW19(ch), 0x87871000);
1948 
1949 	vlv_dpio_put(display);
1950 }
1951 
1952 static void _vlv_enable_pll(const struct intel_crtc_state *crtc_state)
1953 {
1954 	struct intel_display *display = to_intel_display(crtc_state);
1955 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1956 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
1957 	enum pipe pipe = crtc->pipe;
1958 
1959 	intel_de_write(display, DPLL(display, pipe), hw_state->dpll);
1960 	intel_de_posting_read(display, DPLL(display, pipe));
1961 	udelay(150);
1962 
1963 	if (intel_de_wait_for_set_ms(display, DPLL(display, pipe), DPLL_LOCK_VLV, 1))
1964 		drm_err(display->drm, "DPLL %d failed to lock\n", pipe);
1965 }
1966 
1967 void vlv_enable_pll(const struct intel_crtc_state *crtc_state)
1968 {
1969 	struct intel_display *display = to_intel_display(crtc_state);
1970 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1971 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
1972 	enum pipe pipe = crtc->pipe;
1973 
1974 	assert_transcoder_disabled(display, crtc_state->cpu_transcoder);
1975 
1976 	/* PLL is protected by panel, make sure we can write it */
1977 	assert_pps_unlocked(display, pipe);
1978 
1979 	/* Enable Refclk */
1980 	intel_de_write(display, DPLL(display, pipe),
1981 		       hw_state->dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
1982 
1983 	if (hw_state->dpll & DPLL_VCO_ENABLE) {
1984 		vlv_prepare_pll(crtc_state);
1985 		_vlv_enable_pll(crtc_state);
1986 	}
1987 
1988 	intel_de_write(display, DPLL_MD(display, pipe), hw_state->dpll_md);
1989 	intel_de_posting_read(display, DPLL_MD(display, pipe));
1990 }
1991 
1992 static void chv_prepare_pll(const struct intel_crtc_state *crtc_state)
1993 {
1994 	struct intel_display *display = to_intel_display(crtc_state);
1995 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1996 	const struct dpll *clock = &crtc_state->dpll;
1997 	enum dpio_channel ch = vlv_pipe_to_channel(crtc->pipe);
1998 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
1999 	u32 tmp, loopfilter, tribuf_calcntr;
2000 	u32 m2_frac;
2001 
2002 	m2_frac = clock->m2 & 0x3fffff;
2003 
2004 	vlv_dpio_get(display);
2005 
2006 	/* p1 and p2 divider */
2007 	vlv_dpio_write(display, phy, CHV_CMN_DW13(ch),
2008 		       DPIO_CHV_S1_DIV(5) |
2009 		       DPIO_CHV_P1_DIV(clock->p1) |
2010 		       DPIO_CHV_P2_DIV(clock->p2) |
2011 		       DPIO_CHV_K_DIV(1));
2012 
2013 	/* Feedback post-divider - m2 */
2014 	vlv_dpio_write(display, phy, CHV_PLL_DW0(ch),
2015 		       DPIO_CHV_M2_DIV(clock->m2 >> 22));
2016 
2017 	/* Feedback refclk divider - n and m1 */
2018 	vlv_dpio_write(display, phy, CHV_PLL_DW1(ch),
2019 		       DPIO_CHV_M1_DIV(DPIO_CHV_M1_DIV_BY_2) |
2020 		       DPIO_CHV_N_DIV(1));
2021 
2022 	/* M2 fraction division */
2023 	vlv_dpio_write(display, phy, CHV_PLL_DW2(ch),
2024 		       DPIO_CHV_M2_FRAC_DIV(m2_frac));
2025 
2026 	/* M2 fraction division enable */
2027 	tmp = vlv_dpio_read(display, phy, CHV_PLL_DW3(ch));
2028 	tmp &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
2029 	tmp |= DPIO_CHV_FEEDFWD_GAIN(2);
2030 	if (m2_frac)
2031 		tmp |= DPIO_CHV_FRAC_DIV_EN;
2032 	vlv_dpio_write(display, phy, CHV_PLL_DW3(ch), tmp);
2033 
2034 	/* Program digital lock detect threshold */
2035 	tmp = vlv_dpio_read(display, phy, CHV_PLL_DW9(ch));
2036 	tmp &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
2037 		      DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
2038 	tmp |= DPIO_CHV_INT_LOCK_THRESHOLD(0x5);
2039 	if (!m2_frac)
2040 		tmp |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
2041 	vlv_dpio_write(display, phy, CHV_PLL_DW9(ch), tmp);
2042 
2043 	/* Loop filter */
2044 	if (clock->vco == 5400000) {
2045 		loopfilter = DPIO_CHV_PROP_COEFF(0x3) |
2046 			DPIO_CHV_INT_COEFF(0x8) |
2047 			DPIO_CHV_GAIN_CTRL(0x1);
2048 		tribuf_calcntr = 0x9;
2049 	} else if (clock->vco <= 6200000) {
2050 		loopfilter = DPIO_CHV_PROP_COEFF(0x5) |
2051 			DPIO_CHV_INT_COEFF(0xB) |
2052 			DPIO_CHV_GAIN_CTRL(0x3);
2053 		tribuf_calcntr = 0x9;
2054 	} else if (clock->vco <= 6480000) {
2055 		loopfilter = DPIO_CHV_PROP_COEFF(0x4) |
2056 			DPIO_CHV_INT_COEFF(0x9) |
2057 			DPIO_CHV_GAIN_CTRL(0x3);
2058 		tribuf_calcntr = 0x8;
2059 	} else {
2060 		/* Not supported. Apply the same limits as in the max case */
2061 		loopfilter = DPIO_CHV_PROP_COEFF(0x4) |
2062 			DPIO_CHV_INT_COEFF(0x9) |
2063 			DPIO_CHV_GAIN_CTRL(0x3);
2064 		tribuf_calcntr = 0;
2065 	}
2066 	vlv_dpio_write(display, phy, CHV_PLL_DW6(ch), loopfilter);
2067 
2068 	tmp = vlv_dpio_read(display, phy, CHV_PLL_DW8(ch));
2069 	tmp &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
2070 	tmp |= DPIO_CHV_TDC_TARGET_CNT(tribuf_calcntr);
2071 	vlv_dpio_write(display, phy, CHV_PLL_DW8(ch), tmp);
2072 
2073 	/* AFC Recal */
2074 	vlv_dpio_write(display, phy, CHV_CMN_DW14(ch),
2075 		       vlv_dpio_read(display, phy, CHV_CMN_DW14(ch)) |
2076 		       DPIO_AFC_RECAL);
2077 
2078 	vlv_dpio_put(display);
2079 }
2080 
2081 static void _chv_enable_pll(const struct intel_crtc_state *crtc_state)
2082 {
2083 	struct intel_display *display = to_intel_display(crtc_state);
2084 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2085 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
2086 	enum dpio_channel ch = vlv_pipe_to_channel(crtc->pipe);
2087 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
2088 	enum pipe pipe = crtc->pipe;
2089 	u32 tmp;
2090 
2091 	vlv_dpio_get(display);
2092 
2093 	/* Enable back the 10bit clock to display controller */
2094 	tmp = vlv_dpio_read(display, phy, CHV_CMN_DW14(ch));
2095 	tmp |= DPIO_DCLKP_EN;
2096 	vlv_dpio_write(display, phy, CHV_CMN_DW14(ch), tmp);
2097 
2098 	vlv_dpio_put(display);
2099 
2100 	/*
2101 	 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
2102 	 */
2103 	udelay(1);
2104 
2105 	/* Enable PLL */
2106 	intel_de_write(display, DPLL(display, pipe), hw_state->dpll);
2107 
2108 	/* Check PLL is locked */
2109 	if (intel_de_wait_for_set_ms(display, DPLL(display, pipe), DPLL_LOCK_VLV, 1))
2110 		drm_err(display->drm, "PLL %d failed to lock\n", pipe);
2111 }
2112 
2113 void chv_enable_pll(const struct intel_crtc_state *crtc_state)
2114 {
2115 	struct intel_display *display = to_intel_display(crtc_state);
2116 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2117 	const struct i9xx_dpll_hw_state *hw_state = &crtc_state->dpll_hw_state.i9xx;
2118 	enum pipe pipe = crtc->pipe;
2119 
2120 	assert_transcoder_disabled(display, crtc_state->cpu_transcoder);
2121 
2122 	/* PLL is protected by panel, make sure we can write it */
2123 	assert_pps_unlocked(display, pipe);
2124 
2125 	/* Enable Refclk and SSC */
2126 	intel_de_write(display, DPLL(display, pipe),
2127 		       hw_state->dpll & ~DPLL_VCO_ENABLE);
2128 
2129 	if (hw_state->dpll & DPLL_VCO_ENABLE) {
2130 		chv_prepare_pll(crtc_state);
2131 		_chv_enable_pll(crtc_state);
2132 	}
2133 
2134 	if (pipe != PIPE_A) {
2135 		/*
2136 		 * WaPixelRepeatModeFixForC0:chv
2137 		 *
2138 		 * DPLLCMD is AWOL. Use chicken bits to propagate
2139 		 * the value from DPLLBMD to either pipe B or C.
2140 		 */
2141 		intel_de_write(display, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
2142 		intel_de_write(display, DPLL_MD(display, PIPE_B),
2143 			       hw_state->dpll_md);
2144 		intel_de_write(display, CBR4_VLV, 0);
2145 		display->state.chv_dpll_md[pipe] = hw_state->dpll_md;
2146 
2147 		/*
2148 		 * DPLLB VGA mode also seems to cause problems.
2149 		 * We should always have it disabled.
2150 		 */
2151 		drm_WARN_ON(display->drm,
2152 			    (intel_de_read(display, DPLL(display, PIPE_B)) &
2153 			     DPLL_VGA_MODE_DIS) == 0);
2154 	} else {
2155 		intel_de_write(display, DPLL_MD(display, pipe),
2156 			       hw_state->dpll_md);
2157 		intel_de_posting_read(display, DPLL_MD(display, pipe));
2158 	}
2159 }
2160 
2161 /**
2162  * vlv_force_pll_on - forcibly enable just the PLL
2163  * @display: display device
2164  * @pipe: pipe PLL to enable
2165  * @dpll: PLL configuration
2166  *
2167  * Enable the PLL for @pipe using the supplied @dpll config. To be used
2168  * in cases where we need the PLL enabled even when @pipe is not going to
2169  * be enabled.
2170  */
2171 int vlv_force_pll_on(struct intel_display *display, enum pipe pipe,
2172 		     const struct dpll *dpll)
2173 {
2174 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe);
2175 	struct intel_crtc_state *crtc_state;
2176 
2177 	crtc_state = intel_crtc_state_alloc(crtc);
2178 	if (!crtc_state)
2179 		return -ENOMEM;
2180 
2181 	crtc_state->cpu_transcoder = (enum transcoder)pipe;
2182 	crtc_state->pixel_multiplier = 1;
2183 	crtc_state->dpll = *dpll;
2184 	crtc_state->output_types = BIT(INTEL_OUTPUT_EDP);
2185 
2186 	if (display->platform.cherryview) {
2187 		chv_compute_dpll(crtc_state);
2188 		chv_enable_pll(crtc_state);
2189 	} else {
2190 		vlv_compute_dpll(crtc_state);
2191 		vlv_enable_pll(crtc_state);
2192 	}
2193 
2194 	intel_crtc_destroy_state(&crtc->base, &crtc_state->uapi);
2195 
2196 	return 0;
2197 }
2198 
2199 void vlv_disable_pll(struct intel_display *display, enum pipe pipe)
2200 {
2201 	u32 val;
2202 
2203 	/* Make sure the pipe isn't still relying on us */
2204 	assert_transcoder_disabled(display, (enum transcoder)pipe);
2205 
2206 	val = DPLL_INTEGRATED_REF_CLK_VLV |
2207 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
2208 	if (pipe != PIPE_A)
2209 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
2210 
2211 	intel_de_write(display, DPLL(display, pipe), val);
2212 	intel_de_posting_read(display, DPLL(display, pipe));
2213 }
2214 
2215 void chv_disable_pll(struct intel_display *display, enum pipe pipe)
2216 {
2217 	enum dpio_channel ch = vlv_pipe_to_channel(pipe);
2218 	enum dpio_phy phy = vlv_pipe_to_phy(pipe);
2219 	u32 val;
2220 
2221 	/* Make sure the pipe isn't still relying on us */
2222 	assert_transcoder_disabled(display, (enum transcoder)pipe);
2223 
2224 	val = DPLL_SSC_REF_CLK_CHV |
2225 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
2226 	if (pipe != PIPE_A)
2227 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
2228 
2229 	intel_de_write(display, DPLL(display, pipe), val);
2230 	intel_de_posting_read(display, DPLL(display, pipe));
2231 
2232 	vlv_dpio_get(display);
2233 
2234 	/* Disable 10bit clock to display controller */
2235 	val = vlv_dpio_read(display, phy, CHV_CMN_DW14(ch));
2236 	val &= ~DPIO_DCLKP_EN;
2237 	vlv_dpio_write(display, phy, CHV_CMN_DW14(ch), val);
2238 
2239 	vlv_dpio_put(display);
2240 }
2241 
2242 void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
2243 {
2244 	struct intel_display *display = to_intel_display(crtc_state);
2245 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2246 	enum pipe pipe = crtc->pipe;
2247 
2248 	/* Don't disable pipe or pipe PLLs if needed */
2249 	if (display->platform.i830)
2250 		return;
2251 
2252 	/* Make sure the pipe isn't still relying on us */
2253 	assert_transcoder_disabled(display, crtc_state->cpu_transcoder);
2254 
2255 	intel_de_write(display, DPLL(display, pipe), DPLL_VGA_MODE_DIS);
2256 	intel_de_posting_read(display, DPLL(display, pipe));
2257 }
2258 
2259 
2260 /**
2261  * vlv_force_pll_off - forcibly disable just the PLL
2262  * @display: display device
2263  * @pipe: pipe PLL to disable
2264  *
2265  * Disable the PLL for @pipe. To be used in cases where we need
2266  * the PLL enabled even when @pipe is not going to be enabled.
2267  */
2268 void vlv_force_pll_off(struct intel_display *display, enum pipe pipe)
2269 {
2270 	if (display->platform.cherryview)
2271 		chv_disable_pll(display, pipe);
2272 	else
2273 		vlv_disable_pll(display, pipe);
2274 }
2275 
2276 /* Only for pre-ILK configs */
2277 static void assert_pll(struct intel_display *display,
2278 		       enum pipe pipe, bool state)
2279 {
2280 	bool cur_state;
2281 
2282 	cur_state = intel_de_read(display, DPLL(display, pipe)) & DPLL_VCO_ENABLE;
2283 	INTEL_DISPLAY_STATE_WARN(display, cur_state != state,
2284 				 "PLL state assertion failure (expected %s, current %s)\n",
2285 				 str_on_off(state), str_on_off(cur_state));
2286 }
2287 
2288 void assert_pll_enabled(struct intel_display *display, enum pipe pipe)
2289 {
2290 	assert_pll(display, pipe, true);
2291 }
2292 
2293 void assert_pll_disabled(struct intel_display *display, enum pipe pipe)
2294 {
2295 	assert_pll(display, pipe, false);
2296 }
2297 
2298 bool intel_dpll_clock_matches(int clock1, int clock2)
2299 {
2300 	return abs(clock1 - clock2) <= 1;
2301 }
2302