1 // SPDX-License-Identifier: MIT
3 * Copyright © 2020 Intel Corporation
5 #include <linux/kernel.h>
6 #include "intel_crtc.h"
7 #include "intel_display_types.h"
8 #include "intel_display.h"
9 #include "intel_dpll.h"
10 #include "intel_lvds.h"
11 #include "intel_panel.h"
12 #include "intel_sideband.h"
17 } dot, vco, n, m, m1, m2, p, p1;
24 static const struct intel_limit intel_limits_i8xx_dac = {
25 .dot = { .min = 25000, .max = 350000 },
26 .vco = { .min = 908000, .max = 1512000 },
27 .n = { .min = 2, .max = 16 },
28 .m = { .min = 96, .max = 140 },
29 .m1 = { .min = 18, .max = 26 },
30 .m2 = { .min = 6, .max = 16 },
31 .p = { .min = 4, .max = 128 },
32 .p1 = { .min = 2, .max = 33 },
33 .p2 = { .dot_limit = 165000,
34 .p2_slow = 4, .p2_fast = 2 },
37 static const struct intel_limit intel_limits_i8xx_dvo = {
38 .dot = { .min = 25000, .max = 350000 },
39 .vco = { .min = 908000, .max = 1512000 },
40 .n = { .min = 2, .max = 16 },
41 .m = { .min = 96, .max = 140 },
42 .m1 = { .min = 18, .max = 26 },
43 .m2 = { .min = 6, .max = 16 },
44 .p = { .min = 4, .max = 128 },
45 .p1 = { .min = 2, .max = 33 },
46 .p2 = { .dot_limit = 165000,
47 .p2_slow = 4, .p2_fast = 4 },
50 static const struct intel_limit intel_limits_i8xx_lvds = {
51 .dot = { .min = 25000, .max = 350000 },
52 .vco = { .min = 908000, .max = 1512000 },
53 .n = { .min = 2, .max = 16 },
54 .m = { .min = 96, .max = 140 },
55 .m1 = { .min = 18, .max = 26 },
56 .m2 = { .min = 6, .max = 16 },
57 .p = { .min = 4, .max = 128 },
58 .p1 = { .min = 1, .max = 6 },
59 .p2 = { .dot_limit = 165000,
60 .p2_slow = 14, .p2_fast = 7 },
63 static const struct intel_limit intel_limits_i9xx_sdvo = {
64 .dot = { .min = 20000, .max = 400000 },
65 .vco = { .min = 1400000, .max = 2800000 },
66 .n = { .min = 1, .max = 6 },
67 .m = { .min = 70, .max = 120 },
68 .m1 = { .min = 8, .max = 18 },
69 .m2 = { .min = 3, .max = 7 },
70 .p = { .min = 5, .max = 80 },
71 .p1 = { .min = 1, .max = 8 },
72 .p2 = { .dot_limit = 200000,
73 .p2_slow = 10, .p2_fast = 5 },
76 static const struct intel_limit intel_limits_i9xx_lvds = {
77 .dot = { .min = 20000, .max = 400000 },
78 .vco = { .min = 1400000, .max = 2800000 },
79 .n = { .min = 1, .max = 6 },
80 .m = { .min = 70, .max = 120 },
81 .m1 = { .min = 8, .max = 18 },
82 .m2 = { .min = 3, .max = 7 },
83 .p = { .min = 7, .max = 98 },
84 .p1 = { .min = 1, .max = 8 },
85 .p2 = { .dot_limit = 112000,
86 .p2_slow = 14, .p2_fast = 7 },
90 static const struct intel_limit intel_limits_g4x_sdvo = {
91 .dot = { .min = 25000, .max = 270000 },
92 .vco = { .min = 1750000, .max = 3500000},
93 .n = { .min = 1, .max = 4 },
94 .m = { .min = 104, .max = 138 },
95 .m1 = { .min = 17, .max = 23 },
96 .m2 = { .min = 5, .max = 11 },
97 .p = { .min = 10, .max = 30 },
98 .p1 = { .min = 1, .max = 3},
99 .p2 = { .dot_limit = 270000,
105 static const struct intel_limit intel_limits_g4x_hdmi = {
106 .dot = { .min = 22000, .max = 400000 },
107 .vco = { .min = 1750000, .max = 3500000},
108 .n = { .min = 1, .max = 4 },
109 .m = { .min = 104, .max = 138 },
110 .m1 = { .min = 16, .max = 23 },
111 .m2 = { .min = 5, .max = 11 },
112 .p = { .min = 5, .max = 80 },
113 .p1 = { .min = 1, .max = 8},
114 .p2 = { .dot_limit = 165000,
115 .p2_slow = 10, .p2_fast = 5 },
118 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
119 .dot = { .min = 20000, .max = 115000 },
120 .vco = { .min = 1750000, .max = 3500000 },
121 .n = { .min = 1, .max = 3 },
122 .m = { .min = 104, .max = 138 },
123 .m1 = { .min = 17, .max = 23 },
124 .m2 = { .min = 5, .max = 11 },
125 .p = { .min = 28, .max = 112 },
126 .p1 = { .min = 2, .max = 8 },
127 .p2 = { .dot_limit = 0,
128 .p2_slow = 14, .p2_fast = 14
132 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
133 .dot = { .min = 80000, .max = 224000 },
134 .vco = { .min = 1750000, .max = 3500000 },
135 .n = { .min = 1, .max = 3 },
136 .m = { .min = 104, .max = 138 },
137 .m1 = { .min = 17, .max = 23 },
138 .m2 = { .min = 5, .max = 11 },
139 .p = { .min = 14, .max = 42 },
140 .p1 = { .min = 2, .max = 6 },
141 .p2 = { .dot_limit = 0,
142 .p2_slow = 7, .p2_fast = 7
146 static const struct intel_limit pnv_limits_sdvo = {
147 .dot = { .min = 20000, .max = 400000},
148 .vco = { .min = 1700000, .max = 3500000 },
149 /* Pineview's Ncounter is a ring counter */
150 .n = { .min = 3, .max = 6 },
151 .m = { .min = 2, .max = 256 },
152 /* Pineview only has one combined m divider, which we treat as m2. */
153 .m1 = { .min = 0, .max = 0 },
154 .m2 = { .min = 0, .max = 254 },
155 .p = { .min = 5, .max = 80 },
156 .p1 = { .min = 1, .max = 8 },
157 .p2 = { .dot_limit = 200000,
158 .p2_slow = 10, .p2_fast = 5 },
161 static const struct intel_limit pnv_limits_lvds = {
162 .dot = { .min = 20000, .max = 400000 },
163 .vco = { .min = 1700000, .max = 3500000 },
164 .n = { .min = 3, .max = 6 },
165 .m = { .min = 2, .max = 256 },
166 .m1 = { .min = 0, .max = 0 },
167 .m2 = { .min = 0, .max = 254 },
168 .p = { .min = 7, .max = 112 },
169 .p1 = { .min = 1, .max = 8 },
170 .p2 = { .dot_limit = 112000,
171 .p2_slow = 14, .p2_fast = 14 },
174 /* Ironlake / Sandybridge
176 * We calculate clock using (register_value + 2) for N/M1/M2, so here
177 * the range value for them is (actual_value - 2).
179 static const struct intel_limit ilk_limits_dac = {
180 .dot = { .min = 25000, .max = 350000 },
181 .vco = { .min = 1760000, .max = 3510000 },
182 .n = { .min = 1, .max = 5 },
183 .m = { .min = 79, .max = 127 },
184 .m1 = { .min = 12, .max = 22 },
185 .m2 = { .min = 5, .max = 9 },
186 .p = { .min = 5, .max = 80 },
187 .p1 = { .min = 1, .max = 8 },
188 .p2 = { .dot_limit = 225000,
189 .p2_slow = 10, .p2_fast = 5 },
192 static const struct intel_limit ilk_limits_single_lvds = {
193 .dot = { .min = 25000, .max = 350000 },
194 .vco = { .min = 1760000, .max = 3510000 },
195 .n = { .min = 1, .max = 3 },
196 .m = { .min = 79, .max = 118 },
197 .m1 = { .min = 12, .max = 22 },
198 .m2 = { .min = 5, .max = 9 },
199 .p = { .min = 28, .max = 112 },
200 .p1 = { .min = 2, .max = 8 },
201 .p2 = { .dot_limit = 225000,
202 .p2_slow = 14, .p2_fast = 14 },
205 static const struct intel_limit ilk_limits_dual_lvds = {
206 .dot = { .min = 25000, .max = 350000 },
207 .vco = { .min = 1760000, .max = 3510000 },
208 .n = { .min = 1, .max = 3 },
209 .m = { .min = 79, .max = 127 },
210 .m1 = { .min = 12, .max = 22 },
211 .m2 = { .min = 5, .max = 9 },
212 .p = { .min = 14, .max = 56 },
213 .p1 = { .min = 2, .max = 8 },
214 .p2 = { .dot_limit = 225000,
215 .p2_slow = 7, .p2_fast = 7 },
218 /* LVDS 100mhz refclk limits. */
219 static const struct intel_limit ilk_limits_single_lvds_100m = {
220 .dot = { .min = 25000, .max = 350000 },
221 .vco = { .min = 1760000, .max = 3510000 },
222 .n = { .min = 1, .max = 2 },
223 .m = { .min = 79, .max = 126 },
224 .m1 = { .min = 12, .max = 22 },
225 .m2 = { .min = 5, .max = 9 },
226 .p = { .min = 28, .max = 112 },
227 .p1 = { .min = 2, .max = 8 },
228 .p2 = { .dot_limit = 225000,
229 .p2_slow = 14, .p2_fast = 14 },
232 static const struct intel_limit ilk_limits_dual_lvds_100m = {
233 .dot = { .min = 25000, .max = 350000 },
234 .vco = { .min = 1760000, .max = 3510000 },
235 .n = { .min = 1, .max = 3 },
236 .m = { .min = 79, .max = 126 },
237 .m1 = { .min = 12, .max = 22 },
238 .m2 = { .min = 5, .max = 9 },
239 .p = { .min = 14, .max = 42 },
240 .p1 = { .min = 2, .max = 6 },
241 .p2 = { .dot_limit = 225000,
242 .p2_slow = 7, .p2_fast = 7 },
245 static const struct intel_limit intel_limits_vlv = {
247 * These are the data rate limits (measured in fast clocks)
248 * since those are the strictest limits we have. The fast
249 * clock and actual rate limits are more relaxed, so checking
250 * them would make no difference.
252 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
253 .vco = { .min = 4000000, .max = 6000000 },
254 .n = { .min = 1, .max = 7 },
255 .m1 = { .min = 2, .max = 3 },
256 .m2 = { .min = 11, .max = 156 },
257 .p1 = { .min = 2, .max = 3 },
258 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
261 static const struct intel_limit intel_limits_chv = {
263 * These are the data rate limits (measured in fast clocks)
264 * since those are the strictest limits we have. The fast
265 * clock and actual rate limits are more relaxed, so checking
266 * them would make no difference.
268 .dot = { .min = 25000 * 5, .max = 540000 * 5},
269 .vco = { .min = 4800000, .max = 6480000 },
270 .n = { .min = 1, .max = 1 },
271 .m1 = { .min = 2, .max = 2 },
272 .m2 = { .min = 24 << 22, .max = 175 << 22 },
273 .p1 = { .min = 2, .max = 4 },
274 .p2 = { .p2_slow = 1, .p2_fast = 14 },
277 static const struct intel_limit intel_limits_bxt = {
278 /* FIXME: find real dot limits */
279 .dot = { .min = 0, .max = INT_MAX },
280 .vco = { .min = 4800000, .max = 6700000 },
281 .n = { .min = 1, .max = 1 },
282 .m1 = { .min = 2, .max = 2 },
283 /* FIXME: find real m2 limits */
284 .m2 = { .min = 2 << 22, .max = 255 << 22 },
285 .p1 = { .min = 2, .max = 4 },
286 .p2 = { .p2_slow = 1, .p2_fast = 20 },
290 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
291 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
292 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
293 * The helpers' return value is the rate of the clock that is fed to the
294 * display engine's pipe which can be the above fast dot clock rate or a
295 * divided-down version of it.
297 /* m1 is reserved as 0 in Pineview, n is a ring counter */
298 int pnv_calc_dpll_params(int refclk, struct dpll *clock)
300 clock->m = clock->m2 + 2;
301 clock->p = clock->p1 * clock->p2;
302 if (WARN_ON(clock->n == 0 || clock->p == 0))
304 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
305 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
310 static u32 i9xx_dpll_compute_m(struct dpll *dpll)
312 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
315 int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
317 clock->m = i9xx_dpll_compute_m(clock);
318 clock->p = clock->p1 * clock->p2;
319 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
321 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
322 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
327 int vlv_calc_dpll_params(int refclk, struct dpll *clock)
329 clock->m = clock->m1 * clock->m2;
330 clock->p = clock->p1 * clock->p2;
331 if (WARN_ON(clock->n == 0 || clock->p == 0))
333 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
334 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
336 return clock->dot / 5;
339 int chv_calc_dpll_params(int refclk, struct dpll *clock)
341 clock->m = clock->m1 * clock->m2;
342 clock->p = clock->p1 * clock->p2;
343 if (WARN_ON(clock->n == 0 || clock->p == 0))
345 clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
347 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
349 return clock->dot / 5;
353 * Returns whether the given set of divisors are valid for a given refclk with
354 * the given connectors.
356 static bool intel_pll_is_valid(struct drm_i915_private *dev_priv,
357 const struct intel_limit *limit,
358 const struct dpll *clock)
360 if (clock->n < limit->n.min || limit->n.max < clock->n)
362 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
364 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
366 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
369 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
370 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
371 if (clock->m1 <= clock->m2)
374 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
375 !IS_GEN9_LP(dev_priv)) {
376 if (clock->p < limit->p.min || limit->p.max < clock->p)
378 if (clock->m < limit->m.min || limit->m.max < clock->m)
382 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
384 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
385 * connector, etc., rather than just a single range.
387 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
394 i9xx_select_p2_div(const struct intel_limit *limit,
395 const struct intel_crtc_state *crtc_state,
398 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
400 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
402 * For LVDS just rely on its current settings for dual-channel.
403 * We haven't figured out how to reliably set up different
404 * single/dual channel state, if we even can.
406 if (intel_is_dual_link_lvds(dev_priv))
407 return limit->p2.p2_fast;
409 return limit->p2.p2_slow;
411 if (target < limit->p2.dot_limit)
412 return limit->p2.p2_slow;
414 return limit->p2.p2_fast;
419 * Returns a set of divisors for the desired target clock with the given
420 * refclk, or FALSE. The returned values represent the clock equation:
421 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
423 * Target and reference clocks are specified in kHz.
425 * If match_clock is provided, then best_clock P divider must match the P
426 * divider from @match_clock used for LVDS downclocking.
429 i9xx_find_best_dpll(const struct intel_limit *limit,
430 struct intel_crtc_state *crtc_state,
431 int target, int refclk, struct dpll *match_clock,
432 struct dpll *best_clock)
434 struct drm_device *dev = crtc_state->uapi.crtc->dev;
438 memset(best_clock, 0, sizeof(*best_clock));
440 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
442 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
444 for (clock.m2 = limit->m2.min;
445 clock.m2 <= limit->m2.max; clock.m2++) {
446 if (clock.m2 >= clock.m1)
448 for (clock.n = limit->n.min;
449 clock.n <= limit->n.max; clock.n++) {
450 for (clock.p1 = limit->p1.min;
451 clock.p1 <= limit->p1.max; clock.p1++) {
454 i9xx_calc_dpll_params(refclk, &clock);
455 if (!intel_pll_is_valid(to_i915(dev),
460 clock.p != match_clock->p)
463 this_err = abs(clock.dot - target);
464 if (this_err < err) {
473 return (err != target);
477 * Returns a set of divisors for the desired target clock with the given
478 * refclk, or FALSE. The returned values represent the clock equation:
479 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
481 * Target and reference clocks are specified in kHz.
483 * If match_clock is provided, then best_clock P divider must match the P
484 * divider from @match_clock used for LVDS downclocking.
487 pnv_find_best_dpll(const struct intel_limit *limit,
488 struct intel_crtc_state *crtc_state,
489 int target, int refclk, struct dpll *match_clock,
490 struct dpll *best_clock)
492 struct drm_device *dev = crtc_state->uapi.crtc->dev;
496 memset(best_clock, 0, sizeof(*best_clock));
498 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
500 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
502 for (clock.m2 = limit->m2.min;
503 clock.m2 <= limit->m2.max; clock.m2++) {
504 for (clock.n = limit->n.min;
505 clock.n <= limit->n.max; clock.n++) {
506 for (clock.p1 = limit->p1.min;
507 clock.p1 <= limit->p1.max; clock.p1++) {
510 pnv_calc_dpll_params(refclk, &clock);
511 if (!intel_pll_is_valid(to_i915(dev),
516 clock.p != match_clock->p)
519 this_err = abs(clock.dot - target);
520 if (this_err < err) {
529 return (err != target);
533 * Returns a set of divisors for the desired target clock with the given
534 * refclk, or FALSE. The returned values represent the clock equation:
535 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
537 * Target and reference clocks are specified in kHz.
539 * If match_clock is provided, then best_clock P divider must match the P
540 * divider from @match_clock used for LVDS downclocking.
543 g4x_find_best_dpll(const struct intel_limit *limit,
544 struct intel_crtc_state *crtc_state,
545 int target, int refclk, struct dpll *match_clock,
546 struct dpll *best_clock)
548 struct drm_device *dev = crtc_state->uapi.crtc->dev;
552 /* approximately equals target * 0.00585 */
553 int err_most = (target >> 8) + (target >> 9);
555 memset(best_clock, 0, sizeof(*best_clock));
557 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
559 max_n = limit->n.max;
560 /* based on hardware requirement, prefer smaller n to precision */
561 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
562 /* based on hardware requirement, prefere larger m1,m2 */
563 for (clock.m1 = limit->m1.max;
564 clock.m1 >= limit->m1.min; clock.m1--) {
565 for (clock.m2 = limit->m2.max;
566 clock.m2 >= limit->m2.min; clock.m2--) {
567 for (clock.p1 = limit->p1.max;
568 clock.p1 >= limit->p1.min; clock.p1--) {
571 i9xx_calc_dpll_params(refclk, &clock);
572 if (!intel_pll_is_valid(to_i915(dev),
577 this_err = abs(clock.dot - target);
578 if (this_err < err_most) {
592 * Check if the calculated PLL configuration is more optimal compared to the
593 * best configuration and error found so far. Return the calculated error.
595 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
596 const struct dpll *calculated_clock,
597 const struct dpll *best_clock,
598 unsigned int best_error_ppm,
599 unsigned int *error_ppm)
602 * For CHV ignore the error and consider only the P value.
603 * Prefer a bigger P value based on HW requirements.
605 if (IS_CHERRYVIEW(to_i915(dev))) {
608 return calculated_clock->p > best_clock->p;
611 if (drm_WARN_ON_ONCE(dev, !target_freq))
614 *error_ppm = div_u64(1000000ULL *
615 abs(target_freq - calculated_clock->dot),
618 * Prefer a better P value over a better (smaller) error if the error
619 * is small. Ensure this preference for future configurations too by
620 * setting the error to 0.
622 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
628 return *error_ppm + 10 < best_error_ppm;
632 * Returns a set of divisors for the desired target clock with the given
633 * refclk, or FALSE. The returned values represent the clock equation:
634 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
637 vlv_find_best_dpll(const struct intel_limit *limit,
638 struct intel_crtc_state *crtc_state,
639 int target, int refclk, struct dpll *match_clock,
640 struct dpll *best_clock)
642 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
643 struct drm_device *dev = crtc->base.dev;
645 unsigned int bestppm = 1000000;
646 /* min update 19.2 MHz */
647 int max_n = min(limit->n.max, refclk / 19200);
650 target *= 5; /* fast clock */
652 memset(best_clock, 0, sizeof(*best_clock));
654 /* based on hardware requirement, prefer smaller n to precision */
655 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
656 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
657 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
658 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
659 clock.p = clock.p1 * clock.p2;
660 /* based on hardware requirement, prefer bigger m1,m2 values */
661 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
664 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
667 vlv_calc_dpll_params(refclk, &clock);
669 if (!intel_pll_is_valid(to_i915(dev),
674 if (!vlv_PLL_is_optimal(dev, target,
692 * Returns a set of divisors for the desired target clock with the given
693 * refclk, or FALSE. The returned values represent the clock equation:
694 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
697 chv_find_best_dpll(const struct intel_limit *limit,
698 struct intel_crtc_state *crtc_state,
699 int target, int refclk, struct dpll *match_clock,
700 struct dpll *best_clock)
702 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
703 struct drm_device *dev = crtc->base.dev;
704 unsigned int best_error_ppm;
709 memset(best_clock, 0, sizeof(*best_clock));
710 best_error_ppm = 1000000;
713 * Based on hardware doc, the n always set to 1, and m1 always
714 * set to 2. If requires to support 200Mhz refclk, we need to
715 * revisit this because n may not 1 anymore.
719 target *= 5; /* fast clock */
721 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
722 for (clock.p2 = limit->p2.p2_fast;
723 clock.p2 >= limit->p2.p2_slow;
724 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
725 unsigned int error_ppm;
727 clock.p = clock.p1 * clock.p2;
729 m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
732 if (m2 > INT_MAX/clock.m1)
737 chv_calc_dpll_params(refclk, &clock);
739 if (!intel_pll_is_valid(to_i915(dev), limit, &clock))
742 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
743 best_error_ppm, &error_ppm))
747 best_error_ppm = error_ppm;
755 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
756 struct dpll *best_clock)
759 const struct intel_limit *limit = &intel_limits_bxt;
761 return chv_find_best_dpll(limit, crtc_state,
762 crtc_state->port_clock, refclk,
766 static u32 pnv_dpll_compute_fp(struct dpll *dpll)
768 return (1 << dpll->n) << 16 | dpll->m2;
771 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
772 struct intel_crtc_state *crtc_state,
773 struct dpll *reduced_clock)
775 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
778 if (IS_PINEVIEW(dev_priv)) {
779 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
781 fp2 = pnv_dpll_compute_fp(reduced_clock);
783 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
785 fp2 = i9xx_dpll_compute_fp(reduced_clock);
788 crtc_state->dpll_hw_state.fp0 = fp;
790 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
792 crtc_state->dpll_hw_state.fp1 = fp2;
794 crtc_state->dpll_hw_state.fp1 = fp;
798 static void i9xx_compute_dpll(struct intel_crtc *crtc,
799 struct intel_crtc_state *crtc_state,
800 struct dpll *reduced_clock)
802 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
804 struct dpll *clock = &crtc_state->dpll;
806 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
808 dpll = DPLL_VGA_MODE_DIS;
810 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
811 dpll |= DPLLB_MODE_LVDS;
813 dpll |= DPLLB_MODE_DAC_SERIAL;
815 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
816 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
817 dpll |= (crtc_state->pixel_multiplier - 1)
818 << SDVO_MULTIPLIER_SHIFT_HIRES;
821 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
822 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
823 dpll |= DPLL_SDVO_HIGH_SPEED;
825 if (intel_crtc_has_dp_encoder(crtc_state))
826 dpll |= DPLL_SDVO_HIGH_SPEED;
828 /* compute bitmask from p1 value */
829 if (IS_PINEVIEW(dev_priv))
830 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
832 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
833 if (IS_G4X(dev_priv) && reduced_clock)
834 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
838 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
841 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
844 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
847 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
850 if (DISPLAY_VER(dev_priv) >= 4)
851 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
853 if (crtc_state->sdvo_tv_clock)
854 dpll |= PLL_REF_INPUT_TVCLKINBC;
855 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
856 intel_panel_use_ssc(dev_priv))
857 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
859 dpll |= PLL_REF_INPUT_DREFCLK;
861 dpll |= DPLL_VCO_ENABLE;
862 crtc_state->dpll_hw_state.dpll = dpll;
864 if (DISPLAY_VER(dev_priv) >= 4) {
865 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
866 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
867 crtc_state->dpll_hw_state.dpll_md = dpll_md;
871 static void i8xx_compute_dpll(struct intel_crtc *crtc,
872 struct intel_crtc_state *crtc_state,
873 struct dpll *reduced_clock)
875 struct drm_device *dev = crtc->base.dev;
876 struct drm_i915_private *dev_priv = to_i915(dev);
878 struct dpll *clock = &crtc_state->dpll;
880 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
882 dpll = DPLL_VGA_MODE_DIS;
884 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
885 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
888 dpll |= PLL_P1_DIVIDE_BY_TWO;
890 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
892 dpll |= PLL_P2_DIVIDE_BY_4;
897 * "[Almador Errata}: For the correct operation of the muxed DVO pins
898 * (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
899 * GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
900 * Enable) must be set to “1” in both the DPLL A Control Register
901 * (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
903 * For simplicity We simply keep both bits always enabled in
904 * both DPLLS. The spec says we should disable the DVO 2X clock
905 * when not needed, but this seems to work fine in practice.
907 if (IS_I830(dev_priv) ||
908 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
909 dpll |= DPLL_DVO_2X_MODE;
911 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
912 intel_panel_use_ssc(dev_priv))
913 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
915 dpll |= PLL_REF_INPUT_DREFCLK;
917 dpll |= DPLL_VCO_ENABLE;
918 crtc_state->dpll_hw_state.dpll = dpll;
921 static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
922 struct intel_crtc_state *crtc_state)
924 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
925 struct intel_atomic_state *state =
926 to_intel_atomic_state(crtc_state->uapi.state);
928 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
929 DISPLAY_VER(dev_priv) >= 11) {
930 struct intel_encoder *encoder =
931 intel_get_crtc_new_encoder(state, crtc_state);
933 if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
934 drm_dbg_kms(&dev_priv->drm,
935 "failed to find PLL for pipe %c\n",
936 pipe_name(crtc->pipe));
944 static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
946 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
950 static void ilk_compute_dpll(struct intel_crtc *crtc,
951 struct intel_crtc_state *crtc_state,
952 struct dpll *reduced_clock)
954 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
958 /* Enable autotuning of the PLL clock (if permissible) */
960 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
961 if ((intel_panel_use_ssc(dev_priv) &&
962 dev_priv->vbt.lvds_ssc_freq == 100000) ||
963 (HAS_PCH_IBX(dev_priv) &&
964 intel_is_dual_link_lvds(dev_priv)))
966 } else if (crtc_state->sdvo_tv_clock) {
970 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
972 if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
976 fp2 = i9xx_dpll_compute_fp(reduced_clock);
978 if (reduced_clock->m < factor * reduced_clock->n)
986 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
987 dpll |= DPLLB_MODE_LVDS;
989 dpll |= DPLLB_MODE_DAC_SERIAL;
991 dpll |= (crtc_state->pixel_multiplier - 1)
992 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
994 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
995 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
996 dpll |= DPLL_SDVO_HIGH_SPEED;
998 if (intel_crtc_has_dp_encoder(crtc_state))
999 dpll |= DPLL_SDVO_HIGH_SPEED;
1002 * The high speed IO clock is only really required for
1003 * SDVO/HDMI/DP, but we also enable it for CRT to make it
1004 * possible to share the DPLL between CRT and HDMI. Enabling
1005 * the clock needlessly does no real harm, except use up a
1006 * bit of power potentially.
1008 * We'll limit this to IVB with 3 pipes, since it has only two
1009 * DPLLs and so DPLL sharing is the only way to get three pipes
1010 * driving PCH ports at the same time. On SNB we could do this,
1011 * and potentially avoid enabling the second DPLL, but it's not
1012 * clear if it''s a win or loss power wise. No point in doing
1013 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
1015 if (INTEL_NUM_PIPES(dev_priv) == 3 &&
1016 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
1017 dpll |= DPLL_SDVO_HIGH_SPEED;
1019 /* compute bitmask from p1 value */
1020 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
1022 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
1024 switch (crtc_state->dpll.p2) {
1026 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
1029 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
1032 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
1035 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
1039 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
1040 intel_panel_use_ssc(dev_priv))
1041 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
1043 dpll |= PLL_REF_INPUT_DREFCLK;
1045 dpll |= DPLL_VCO_ENABLE;
1047 crtc_state->dpll_hw_state.dpll = dpll;
1048 crtc_state->dpll_hw_state.fp0 = fp;
1049 crtc_state->dpll_hw_state.fp1 = fp2;
1052 static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
1053 struct intel_crtc_state *crtc_state)
1055 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1056 struct intel_atomic_state *state =
1057 to_intel_atomic_state(crtc_state->uapi.state);
1058 const struct intel_limit *limit;
1059 int refclk = 120000;
1061 memset(&crtc_state->dpll_hw_state, 0,
1062 sizeof(crtc_state->dpll_hw_state));
1064 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
1065 if (!crtc_state->has_pch_encoder)
1068 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1069 if (intel_panel_use_ssc(dev_priv)) {
1070 drm_dbg_kms(&dev_priv->drm,
1071 "using SSC reference clock of %d kHz\n",
1072 dev_priv->vbt.lvds_ssc_freq);
1073 refclk = dev_priv->vbt.lvds_ssc_freq;
1076 if (intel_is_dual_link_lvds(dev_priv)) {
1077 if (refclk == 100000)
1078 limit = &ilk_limits_dual_lvds_100m;
1080 limit = &ilk_limits_dual_lvds;
1082 if (refclk == 100000)
1083 limit = &ilk_limits_single_lvds_100m;
1085 limit = &ilk_limits_single_lvds;
1088 limit = &ilk_limits_dac;
1091 if (!crtc_state->clock_set &&
1092 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1093 refclk, NULL, &crtc_state->dpll)) {
1094 drm_err(&dev_priv->drm,
1095 "Couldn't find PLL settings for mode!\n");
1099 ilk_compute_dpll(crtc, crtc_state, NULL);
1101 if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
1102 drm_dbg_kms(&dev_priv->drm,
1103 "failed to find PLL for pipe %c\n",
1104 pipe_name(crtc->pipe));
1111 void vlv_compute_dpll(struct intel_crtc *crtc,
1112 struct intel_crtc_state *pipe_config)
1114 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
1115 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1116 if (crtc->pipe != PIPE_A)
1117 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1119 /* DPLL not used with DSI, but still need the rest set up */
1120 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
1121 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
1122 DPLL_EXT_BUFFER_ENABLE_VLV;
1124 pipe_config->dpll_hw_state.dpll_md =
1125 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1128 void chv_compute_dpll(struct intel_crtc *crtc,
1129 struct intel_crtc_state *pipe_config)
1131 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
1132 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1133 if (crtc->pipe != PIPE_A)
1134 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1136 /* DPLL not used with DSI, but still need the rest set up */
1137 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
1138 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
1140 pipe_config->dpll_hw_state.dpll_md =
1141 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1144 static int chv_crtc_compute_clock(struct intel_crtc *crtc,
1145 struct intel_crtc_state *crtc_state)
1147 int refclk = 100000;
1148 const struct intel_limit *limit = &intel_limits_chv;
1149 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1151 memset(&crtc_state->dpll_hw_state, 0,
1152 sizeof(crtc_state->dpll_hw_state));
1154 if (!crtc_state->clock_set &&
1155 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1156 refclk, NULL, &crtc_state->dpll)) {
1157 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
1161 chv_compute_dpll(crtc, crtc_state);
1166 static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
1167 struct intel_crtc_state *crtc_state)
1169 int refclk = 100000;
1170 const struct intel_limit *limit = &intel_limits_vlv;
1171 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1173 memset(&crtc_state->dpll_hw_state, 0,
1174 sizeof(crtc_state->dpll_hw_state));
1176 if (!crtc_state->clock_set &&
1177 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1178 refclk, NULL, &crtc_state->dpll)) {
1179 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
1183 vlv_compute_dpll(crtc, crtc_state);
1188 static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
1189 struct intel_crtc_state *crtc_state)
1191 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1192 const struct intel_limit *limit;
1195 memset(&crtc_state->dpll_hw_state, 0,
1196 sizeof(crtc_state->dpll_hw_state));
1198 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1199 if (intel_panel_use_ssc(dev_priv)) {
1200 refclk = dev_priv->vbt.lvds_ssc_freq;
1201 drm_dbg_kms(&dev_priv->drm,
1202 "using SSC reference clock of %d kHz\n",
1206 if (intel_is_dual_link_lvds(dev_priv))
1207 limit = &intel_limits_g4x_dual_channel_lvds;
1209 limit = &intel_limits_g4x_single_channel_lvds;
1210 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
1211 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
1212 limit = &intel_limits_g4x_hdmi;
1213 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
1214 limit = &intel_limits_g4x_sdvo;
1216 /* The option is for other outputs */
1217 limit = &intel_limits_i9xx_sdvo;
1220 if (!crtc_state->clock_set &&
1221 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1222 refclk, NULL, &crtc_state->dpll)) {
1223 drm_err(&dev_priv->drm,
1224 "Couldn't find PLL settings for mode!\n");
1228 i9xx_compute_dpll(crtc, crtc_state, NULL);
1233 static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
1234 struct intel_crtc_state *crtc_state)
1236 struct drm_device *dev = crtc->base.dev;
1237 struct drm_i915_private *dev_priv = to_i915(dev);
1238 const struct intel_limit *limit;
1241 memset(&crtc_state->dpll_hw_state, 0,
1242 sizeof(crtc_state->dpll_hw_state));
1244 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1245 if (intel_panel_use_ssc(dev_priv)) {
1246 refclk = dev_priv->vbt.lvds_ssc_freq;
1247 drm_dbg_kms(&dev_priv->drm,
1248 "using SSC reference clock of %d kHz\n",
1252 limit = &pnv_limits_lvds;
1254 limit = &pnv_limits_sdvo;
1257 if (!crtc_state->clock_set &&
1258 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1259 refclk, NULL, &crtc_state->dpll)) {
1260 drm_err(&dev_priv->drm,
1261 "Couldn't find PLL settings for mode!\n");
1265 i9xx_compute_dpll(crtc, crtc_state, NULL);
1270 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
1271 struct intel_crtc_state *crtc_state)
1273 struct drm_device *dev = crtc->base.dev;
1274 struct drm_i915_private *dev_priv = to_i915(dev);
1275 const struct intel_limit *limit;
1278 memset(&crtc_state->dpll_hw_state, 0,
1279 sizeof(crtc_state->dpll_hw_state));
1281 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1282 if (intel_panel_use_ssc(dev_priv)) {
1283 refclk = dev_priv->vbt.lvds_ssc_freq;
1284 drm_dbg_kms(&dev_priv->drm,
1285 "using SSC reference clock of %d kHz\n",
1289 limit = &intel_limits_i9xx_lvds;
1291 limit = &intel_limits_i9xx_sdvo;
1294 if (!crtc_state->clock_set &&
1295 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1296 refclk, NULL, &crtc_state->dpll)) {
1297 drm_err(&dev_priv->drm,
1298 "Couldn't find PLL settings for mode!\n");
1302 i9xx_compute_dpll(crtc, crtc_state, NULL);
1307 static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
1308 struct intel_crtc_state *crtc_state)
1310 struct drm_device *dev = crtc->base.dev;
1311 struct drm_i915_private *dev_priv = to_i915(dev);
1312 const struct intel_limit *limit;
1315 memset(&crtc_state->dpll_hw_state, 0,
1316 sizeof(crtc_state->dpll_hw_state));
1318 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1319 if (intel_panel_use_ssc(dev_priv)) {
1320 refclk = dev_priv->vbt.lvds_ssc_freq;
1321 drm_dbg_kms(&dev_priv->drm,
1322 "using SSC reference clock of %d kHz\n",
1326 limit = &intel_limits_i8xx_lvds;
1327 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
1328 limit = &intel_limits_i8xx_dvo;
1330 limit = &intel_limits_i8xx_dac;
1333 if (!crtc_state->clock_set &&
1334 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1335 refclk, NULL, &crtc_state->dpll)) {
1336 drm_err(&dev_priv->drm,
1337 "Couldn't find PLL settings for mode!\n");
1341 i8xx_compute_dpll(crtc, crtc_state, NULL);
1347 intel_dpll_init_clock_hook(struct drm_i915_private *dev_priv)
1349 if (DISPLAY_VER(dev_priv) >= 9 || HAS_DDI(dev_priv))
1350 dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
1351 else if (HAS_PCH_SPLIT(dev_priv))
1352 dev_priv->display.crtc_compute_clock = ilk_crtc_compute_clock;
1353 else if (IS_CHERRYVIEW(dev_priv))
1354 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
1355 else if (IS_VALLEYVIEW(dev_priv))
1356 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
1357 else if (IS_G4X(dev_priv))
1358 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
1359 else if (IS_PINEVIEW(dev_priv))
1360 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
1361 else if (!IS_DISPLAY_VER(dev_priv, 2))
1362 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
1364 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
1367 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1369 if (IS_I830(dev_priv))
1372 return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1375 void i9xx_enable_pll(struct intel_crtc *crtc,
1376 const struct intel_crtc_state *crtc_state)
1378 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1379 i915_reg_t reg = DPLL(crtc->pipe);
1380 u32 dpll = crtc_state->dpll_hw_state.dpll;
1383 assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1385 /* PLL is protected by panel, make sure we can write it */
1386 if (i9xx_has_pps(dev_priv))
1387 assert_panel_unlocked(dev_priv, crtc->pipe);
1390 * Apparently we need to have VGA mode enabled prior to changing
1391 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1392 * dividers, even though the register value does change.
1394 intel_de_write(dev_priv, reg, dpll & ~DPLL_VGA_MODE_DIS);
1395 intel_de_write(dev_priv, reg, dpll);
1397 /* Wait for the clocks to stabilize. */
1398 intel_de_posting_read(dev_priv, reg);
1401 if (DISPLAY_VER(dev_priv) >= 4) {
1402 intel_de_write(dev_priv, DPLL_MD(crtc->pipe),
1403 crtc_state->dpll_hw_state.dpll_md);
1405 /* The pixel multiplier can only be updated once the
1406 * DPLL is enabled and the clocks are stable.
1408 * So write it again.
1410 intel_de_write(dev_priv, reg, dpll);
1413 /* We do this three times for luck */
1414 for (i = 0; i < 3; i++) {
1415 intel_de_write(dev_priv, reg, dpll);
1416 intel_de_posting_read(dev_priv, reg);
1417 udelay(150); /* wait for warmup */
1421 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv,
1427 * PLLB opamp always calibrates to max value of 0x3f, force enable it
1428 * and set it to a reasonable value instead.
1430 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
1431 reg_val &= 0xffffff00;
1432 reg_val |= 0x00000030;
1433 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
1435 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
1436 reg_val &= 0x00ffffff;
1437 reg_val |= 0x8c000000;
1438 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
1440 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
1441 reg_val &= 0xffffff00;
1442 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
1444 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
1445 reg_val &= 0x00ffffff;
1446 reg_val |= 0xb0000000;
1447 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
1450 static void _vlv_enable_pll(struct intel_crtc *crtc,
1451 const struct intel_crtc_state *pipe_config)
1453 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1454 enum pipe pipe = crtc->pipe;
1456 intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1457 intel_de_posting_read(dev_priv, DPLL(pipe));
1460 if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1461 drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
1464 void vlv_enable_pll(struct intel_crtc *crtc,
1465 const struct intel_crtc_state *pipe_config)
1467 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1468 enum pipe pipe = crtc->pipe;
1470 assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1472 /* PLL is protected by panel, make sure we can write it */
1473 assert_panel_unlocked(dev_priv, pipe);
1475 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1476 _vlv_enable_pll(crtc, pipe_config);
1478 intel_de_write(dev_priv, DPLL_MD(pipe),
1479 pipe_config->dpll_hw_state.dpll_md);
1480 intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1484 static void _chv_enable_pll(struct intel_crtc *crtc,
1485 const struct intel_crtc_state *pipe_config)
1487 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1488 enum pipe pipe = crtc->pipe;
1489 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1492 vlv_dpio_get(dev_priv);
1494 /* Enable back the 10bit clock to display controller */
1495 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1496 tmp |= DPIO_DCLKP_EN;
1497 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1499 vlv_dpio_put(dev_priv);
1502 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1507 intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1509 /* Check PLL is locked */
1510 if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1511 drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
1514 void chv_enable_pll(struct intel_crtc *crtc,
1515 const struct intel_crtc_state *pipe_config)
1517 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1518 enum pipe pipe = crtc->pipe;
1520 assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1522 /* PLL is protected by panel, make sure we can write it */
1523 assert_panel_unlocked(dev_priv, pipe);
1525 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1526 _chv_enable_pll(crtc, pipe_config);
1528 if (pipe != PIPE_A) {
1530 * WaPixelRepeatModeFixForC0:chv
1532 * DPLLCMD is AWOL. Use chicken bits to propagate
1533 * the value from DPLLBMD to either pipe B or C.
1535 intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1536 intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1537 pipe_config->dpll_hw_state.dpll_md);
1538 intel_de_write(dev_priv, CBR4_VLV, 0);
1539 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1542 * DPLLB VGA mode also seems to cause problems.
1543 * We should always have it disabled.
1545 drm_WARN_ON(&dev_priv->drm,
1546 (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1547 DPLL_VGA_MODE_DIS) == 0);
1549 intel_de_write(dev_priv, DPLL_MD(pipe),
1550 pipe_config->dpll_hw_state.dpll_md);
1551 intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1555 void vlv_prepare_pll(struct intel_crtc *crtc,
1556 const struct intel_crtc_state *pipe_config)
1558 struct drm_device *dev = crtc->base.dev;
1559 struct drm_i915_private *dev_priv = to_i915(dev);
1560 enum pipe pipe = crtc->pipe;
1562 u32 bestn, bestm1, bestm2, bestp1, bestp2;
1563 u32 coreclk, reg_val;
1566 intel_de_write(dev_priv, DPLL(pipe),
1567 pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
1569 /* No need to actually set up the DPLL with DSI */
1570 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
1573 vlv_dpio_get(dev_priv);
1575 bestn = pipe_config->dpll.n;
1576 bestm1 = pipe_config->dpll.m1;
1577 bestm2 = pipe_config->dpll.m2;
1578 bestp1 = pipe_config->dpll.p1;
1579 bestp2 = pipe_config->dpll.p2;
1581 /* See eDP HDMI DPIO driver vbios notes doc */
1583 /* PLL B needs special handling */
1585 vlv_pllb_recal_opamp(dev_priv, pipe);
1587 /* Set up Tx target for periodic Rcomp update */
1588 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
1590 /* Disable target IRef on PLL */
1591 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
1592 reg_val &= 0x00ffffff;
1593 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
1595 /* Disable fast lock */
1596 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
1598 /* Set idtafcrecal before PLL is enabled */
1599 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
1600 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
1601 mdiv |= ((bestn << DPIO_N_SHIFT));
1602 mdiv |= (1 << DPIO_K_SHIFT);
1605 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
1606 * but we don't support that).
1607 * Note: don't use the DAC post divider as it seems unstable.
1609 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
1610 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
1612 mdiv |= DPIO_ENABLE_CALIBRATION;
1613 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
1615 /* Set HBR and RBR LPF coefficients */
1616 if (pipe_config->port_clock == 162000 ||
1617 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
1618 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
1619 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
1622 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
1625 if (intel_crtc_has_dp_encoder(pipe_config)) {
1626 /* Use SSC source */
1628 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1631 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1633 } else { /* HDMI or VGA */
1634 /* Use bend source */
1636 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1639 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1643 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
1644 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
1645 if (intel_crtc_has_dp_encoder(pipe_config))
1646 coreclk |= 0x01000000;
1647 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
1649 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
1651 vlv_dpio_put(dev_priv);
1654 void chv_prepare_pll(struct intel_crtc *crtc,
1655 const struct intel_crtc_state *pipe_config)
1657 struct drm_device *dev = crtc->base.dev;
1658 struct drm_i915_private *dev_priv = to_i915(dev);
1659 enum pipe pipe = crtc->pipe;
1660 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1661 u32 loopfilter, tribuf_calcntr;
1662 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
1666 /* Enable Refclk and SSC */
1667 intel_de_write(dev_priv, DPLL(pipe),
1668 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
1670 /* No need to actually set up the DPLL with DSI */
1671 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
1674 bestn = pipe_config->dpll.n;
1675 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
1676 bestm1 = pipe_config->dpll.m1;
1677 bestm2 = pipe_config->dpll.m2 >> 22;
1678 bestp1 = pipe_config->dpll.p1;
1679 bestp2 = pipe_config->dpll.p2;
1680 vco = pipe_config->dpll.vco;
1684 vlv_dpio_get(dev_priv);
1686 /* p1 and p2 divider */
1687 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
1688 5 << DPIO_CHV_S1_DIV_SHIFT |
1689 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
1690 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
1691 1 << DPIO_CHV_K_DIV_SHIFT);
1693 /* Feedback post-divider - m2 */
1694 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
1696 /* Feedback refclk divider - n and m1 */
1697 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
1698 DPIO_CHV_M1_DIV_BY_2 |
1699 1 << DPIO_CHV_N_DIV_SHIFT);
1701 /* M2 fraction division */
1702 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
1704 /* M2 fraction division enable */
1705 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
1706 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
1707 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
1709 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
1710 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
1712 /* Program digital lock detect threshold */
1713 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
1714 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
1715 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
1716 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
1718 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
1719 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
1722 if (vco == 5400000) {
1723 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
1724 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
1725 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
1726 tribuf_calcntr = 0x9;
1727 } else if (vco <= 6200000) {
1728 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
1729 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
1730 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1731 tribuf_calcntr = 0x9;
1732 } else if (vco <= 6480000) {
1733 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
1734 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
1735 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1736 tribuf_calcntr = 0x8;
1738 /* Not supported. Apply the same limits as in the max case */
1739 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
1740 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
1741 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1744 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
1746 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
1747 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
1748 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
1749 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
1752 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
1753 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
1756 vlv_dpio_put(dev_priv);
1760 * vlv_force_pll_on - forcibly enable just the PLL
1761 * @dev_priv: i915 private structure
1762 * @pipe: pipe PLL to enable
1763 * @dpll: PLL configuration
1765 * Enable the PLL for @pipe using the supplied @dpll config. To be used
1766 * in cases where we need the PLL enabled even when @pipe is not going to
1769 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
1770 const struct dpll *dpll)
1772 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
1773 struct intel_crtc_state *pipe_config;
1775 pipe_config = intel_crtc_state_alloc(crtc);
1779 pipe_config->cpu_transcoder = (enum transcoder)pipe;
1780 pipe_config->pixel_multiplier = 1;
1781 pipe_config->dpll = *dpll;
1783 if (IS_CHERRYVIEW(dev_priv)) {
1784 chv_compute_dpll(crtc, pipe_config);
1785 chv_prepare_pll(crtc, pipe_config);
1786 chv_enable_pll(crtc, pipe_config);
1788 vlv_compute_dpll(crtc, pipe_config);
1789 vlv_prepare_pll(crtc, pipe_config);
1790 vlv_enable_pll(crtc, pipe_config);
1798 void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1802 /* Make sure the pipe isn't still relying on us */
1803 assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1805 val = DPLL_INTEGRATED_REF_CLK_VLV |
1806 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1808 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1810 intel_de_write(dev_priv, DPLL(pipe), val);
1811 intel_de_posting_read(dev_priv, DPLL(pipe));
1814 void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1816 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1819 /* Make sure the pipe isn't still relying on us */
1820 assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1822 val = DPLL_SSC_REF_CLK_CHV |
1823 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1825 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1827 intel_de_write(dev_priv, DPLL(pipe), val);
1828 intel_de_posting_read(dev_priv, DPLL(pipe));
1830 vlv_dpio_get(dev_priv);
1832 /* Disable 10bit clock to display controller */
1833 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1834 val &= ~DPIO_DCLKP_EN;
1835 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1837 vlv_dpio_put(dev_priv);
1840 void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1842 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1843 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1844 enum pipe pipe = crtc->pipe;
1846 /* Don't disable pipe or pipe PLLs if needed */
1847 if (IS_I830(dev_priv))
1850 /* Make sure the pipe isn't still relying on us */
1851 assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1853 intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1854 intel_de_posting_read(dev_priv, DPLL(pipe));
1859 * vlv_force_pll_off - forcibly disable just the PLL
1860 * @dev_priv: i915 private structure
1861 * @pipe: pipe PLL to disable
1863 * Disable the PLL for @pipe. To be used in cases where we need
1864 * the PLL enabled even when @pipe is not going to be enabled.
1866 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
1868 if (IS_CHERRYVIEW(dev_priv))
1869 chv_disable_pll(dev_priv, pipe);
1871 vlv_disable_pll(dev_priv, pipe);