1 // SPDX-License-Identifier: MIT
3 * Copyright © 2020 Intel Corporation
5 #include <linux/kernel.h>
6 #include "intel_crtc.h"
8 #include "intel_display_types.h"
9 #include "intel_display.h"
10 #include "intel_dpll.h"
11 #include "intel_lvds.h"
12 #include "intel_panel.h"
13 #include "intel_sideband.h"
18 } dot, vco, n, m, m1, m2, p, p1;
25 static const struct intel_limit intel_limits_i8xx_dac = {
26 .dot = { .min = 25000, .max = 350000 },
27 .vco = { .min = 908000, .max = 1512000 },
28 .n = { .min = 2, .max = 16 },
29 .m = { .min = 96, .max = 140 },
30 .m1 = { .min = 18, .max = 26 },
31 .m2 = { .min = 6, .max = 16 },
32 .p = { .min = 4, .max = 128 },
33 .p1 = { .min = 2, .max = 33 },
34 .p2 = { .dot_limit = 165000,
35 .p2_slow = 4, .p2_fast = 2 },
38 static const struct intel_limit intel_limits_i8xx_dvo = {
39 .dot = { .min = 25000, .max = 350000 },
40 .vco = { .min = 908000, .max = 1512000 },
41 .n = { .min = 2, .max = 16 },
42 .m = { .min = 96, .max = 140 },
43 .m1 = { .min = 18, .max = 26 },
44 .m2 = { .min = 6, .max = 16 },
45 .p = { .min = 4, .max = 128 },
46 .p1 = { .min = 2, .max = 33 },
47 .p2 = { .dot_limit = 165000,
48 .p2_slow = 4, .p2_fast = 4 },
51 static const struct intel_limit intel_limits_i8xx_lvds = {
52 .dot = { .min = 25000, .max = 350000 },
53 .vco = { .min = 908000, .max = 1512000 },
54 .n = { .min = 2, .max = 16 },
55 .m = { .min = 96, .max = 140 },
56 .m1 = { .min = 18, .max = 26 },
57 .m2 = { .min = 6, .max = 16 },
58 .p = { .min = 4, .max = 128 },
59 .p1 = { .min = 1, .max = 6 },
60 .p2 = { .dot_limit = 165000,
61 .p2_slow = 14, .p2_fast = 7 },
64 static const struct intel_limit intel_limits_i9xx_sdvo = {
65 .dot = { .min = 20000, .max = 400000 },
66 .vco = { .min = 1400000, .max = 2800000 },
67 .n = { .min = 1, .max = 6 },
68 .m = { .min = 70, .max = 120 },
69 .m1 = { .min = 8, .max = 18 },
70 .m2 = { .min = 3, .max = 7 },
71 .p = { .min = 5, .max = 80 },
72 .p1 = { .min = 1, .max = 8 },
73 .p2 = { .dot_limit = 200000,
74 .p2_slow = 10, .p2_fast = 5 },
77 static const struct intel_limit intel_limits_i9xx_lvds = {
78 .dot = { .min = 20000, .max = 400000 },
79 .vco = { .min = 1400000, .max = 2800000 },
80 .n = { .min = 1, .max = 6 },
81 .m = { .min = 70, .max = 120 },
82 .m1 = { .min = 8, .max = 18 },
83 .m2 = { .min = 3, .max = 7 },
84 .p = { .min = 7, .max = 98 },
85 .p1 = { .min = 1, .max = 8 },
86 .p2 = { .dot_limit = 112000,
87 .p2_slow = 14, .p2_fast = 7 },
91 static const struct intel_limit intel_limits_g4x_sdvo = {
92 .dot = { .min = 25000, .max = 270000 },
93 .vco = { .min = 1750000, .max = 3500000},
94 .n = { .min = 1, .max = 4 },
95 .m = { .min = 104, .max = 138 },
96 .m1 = { .min = 17, .max = 23 },
97 .m2 = { .min = 5, .max = 11 },
98 .p = { .min = 10, .max = 30 },
99 .p1 = { .min = 1, .max = 3},
100 .p2 = { .dot_limit = 270000,
106 static const struct intel_limit intel_limits_g4x_hdmi = {
107 .dot = { .min = 22000, .max = 400000 },
108 .vco = { .min = 1750000, .max = 3500000},
109 .n = { .min = 1, .max = 4 },
110 .m = { .min = 104, .max = 138 },
111 .m1 = { .min = 16, .max = 23 },
112 .m2 = { .min = 5, .max = 11 },
113 .p = { .min = 5, .max = 80 },
114 .p1 = { .min = 1, .max = 8},
115 .p2 = { .dot_limit = 165000,
116 .p2_slow = 10, .p2_fast = 5 },
119 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
120 .dot = { .min = 20000, .max = 115000 },
121 .vco = { .min = 1750000, .max = 3500000 },
122 .n = { .min = 1, .max = 3 },
123 .m = { .min = 104, .max = 138 },
124 .m1 = { .min = 17, .max = 23 },
125 .m2 = { .min = 5, .max = 11 },
126 .p = { .min = 28, .max = 112 },
127 .p1 = { .min = 2, .max = 8 },
128 .p2 = { .dot_limit = 0,
129 .p2_slow = 14, .p2_fast = 14
133 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
134 .dot = { .min = 80000, .max = 224000 },
135 .vco = { .min = 1750000, .max = 3500000 },
136 .n = { .min = 1, .max = 3 },
137 .m = { .min = 104, .max = 138 },
138 .m1 = { .min = 17, .max = 23 },
139 .m2 = { .min = 5, .max = 11 },
140 .p = { .min = 14, .max = 42 },
141 .p1 = { .min = 2, .max = 6 },
142 .p2 = { .dot_limit = 0,
143 .p2_slow = 7, .p2_fast = 7
147 static const struct intel_limit pnv_limits_sdvo = {
148 .dot = { .min = 20000, .max = 400000},
149 .vco = { .min = 1700000, .max = 3500000 },
150 /* Pineview's Ncounter is a ring counter */
151 .n = { .min = 3, .max = 6 },
152 .m = { .min = 2, .max = 256 },
153 /* Pineview only has one combined m divider, which we treat as m2. */
154 .m1 = { .min = 0, .max = 0 },
155 .m2 = { .min = 0, .max = 254 },
156 .p = { .min = 5, .max = 80 },
157 .p1 = { .min = 1, .max = 8 },
158 .p2 = { .dot_limit = 200000,
159 .p2_slow = 10, .p2_fast = 5 },
162 static const struct intel_limit pnv_limits_lvds = {
163 .dot = { .min = 20000, .max = 400000 },
164 .vco = { .min = 1700000, .max = 3500000 },
165 .n = { .min = 3, .max = 6 },
166 .m = { .min = 2, .max = 256 },
167 .m1 = { .min = 0, .max = 0 },
168 .m2 = { .min = 0, .max = 254 },
169 .p = { .min = 7, .max = 112 },
170 .p1 = { .min = 1, .max = 8 },
171 .p2 = { .dot_limit = 112000,
172 .p2_slow = 14, .p2_fast = 14 },
175 /* Ironlake / Sandybridge
177 * We calculate clock using (register_value + 2) for N/M1/M2, so here
178 * the range value for them is (actual_value - 2).
180 static const struct intel_limit ilk_limits_dac = {
181 .dot = { .min = 25000, .max = 350000 },
182 .vco = { .min = 1760000, .max = 3510000 },
183 .n = { .min = 1, .max = 5 },
184 .m = { .min = 79, .max = 127 },
185 .m1 = { .min = 12, .max = 22 },
186 .m2 = { .min = 5, .max = 9 },
187 .p = { .min = 5, .max = 80 },
188 .p1 = { .min = 1, .max = 8 },
189 .p2 = { .dot_limit = 225000,
190 .p2_slow = 10, .p2_fast = 5 },
193 static const struct intel_limit ilk_limits_single_lvds = {
194 .dot = { .min = 25000, .max = 350000 },
195 .vco = { .min = 1760000, .max = 3510000 },
196 .n = { .min = 1, .max = 3 },
197 .m = { .min = 79, .max = 118 },
198 .m1 = { .min = 12, .max = 22 },
199 .m2 = { .min = 5, .max = 9 },
200 .p = { .min = 28, .max = 112 },
201 .p1 = { .min = 2, .max = 8 },
202 .p2 = { .dot_limit = 225000,
203 .p2_slow = 14, .p2_fast = 14 },
206 static const struct intel_limit ilk_limits_dual_lvds = {
207 .dot = { .min = 25000, .max = 350000 },
208 .vco = { .min = 1760000, .max = 3510000 },
209 .n = { .min = 1, .max = 3 },
210 .m = { .min = 79, .max = 127 },
211 .m1 = { .min = 12, .max = 22 },
212 .m2 = { .min = 5, .max = 9 },
213 .p = { .min = 14, .max = 56 },
214 .p1 = { .min = 2, .max = 8 },
215 .p2 = { .dot_limit = 225000,
216 .p2_slow = 7, .p2_fast = 7 },
219 /* LVDS 100mhz refclk limits. */
220 static const struct intel_limit ilk_limits_single_lvds_100m = {
221 .dot = { .min = 25000, .max = 350000 },
222 .vco = { .min = 1760000, .max = 3510000 },
223 .n = { .min = 1, .max = 2 },
224 .m = { .min = 79, .max = 126 },
225 .m1 = { .min = 12, .max = 22 },
226 .m2 = { .min = 5, .max = 9 },
227 .p = { .min = 28, .max = 112 },
228 .p1 = { .min = 2, .max = 8 },
229 .p2 = { .dot_limit = 225000,
230 .p2_slow = 14, .p2_fast = 14 },
233 static const struct intel_limit ilk_limits_dual_lvds_100m = {
234 .dot = { .min = 25000, .max = 350000 },
235 .vco = { .min = 1760000, .max = 3510000 },
236 .n = { .min = 1, .max = 3 },
237 .m = { .min = 79, .max = 126 },
238 .m1 = { .min = 12, .max = 22 },
239 .m2 = { .min = 5, .max = 9 },
240 .p = { .min = 14, .max = 42 },
241 .p1 = { .min = 2, .max = 6 },
242 .p2 = { .dot_limit = 225000,
243 .p2_slow = 7, .p2_fast = 7 },
246 static const struct intel_limit intel_limits_vlv = {
248 * These are the data rate limits (measured in fast clocks)
249 * since those are the strictest limits we have. The fast
250 * clock and actual rate limits are more relaxed, so checking
251 * them would make no difference.
253 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
254 .vco = { .min = 4000000, .max = 6000000 },
255 .n = { .min = 1, .max = 7 },
256 .m1 = { .min = 2, .max = 3 },
257 .m2 = { .min = 11, .max = 156 },
258 .p1 = { .min = 2, .max = 3 },
259 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
262 static const struct intel_limit intel_limits_chv = {
264 * These are the data rate limits (measured in fast clocks)
265 * since those are the strictest limits we have. The fast
266 * clock and actual rate limits are more relaxed, so checking
267 * them would make no difference.
269 .dot = { .min = 25000 * 5, .max = 540000 * 5},
270 .vco = { .min = 4800000, .max = 6480000 },
271 .n = { .min = 1, .max = 1 },
272 .m1 = { .min = 2, .max = 2 },
273 .m2 = { .min = 24 << 22, .max = 175 << 22 },
274 .p1 = { .min = 2, .max = 4 },
275 .p2 = { .p2_slow = 1, .p2_fast = 14 },
278 static const struct intel_limit intel_limits_bxt = {
279 /* FIXME: find real dot limits */
280 .dot = { .min = 0, .max = INT_MAX },
281 .vco = { .min = 4800000, .max = 6700000 },
282 .n = { .min = 1, .max = 1 },
283 .m1 = { .min = 2, .max = 2 },
284 /* FIXME: find real m2 limits */
285 .m2 = { .min = 2 << 22, .max = 255 << 22 },
286 .p1 = { .min = 2, .max = 4 },
287 .p2 = { .p2_slow = 1, .p2_fast = 20 },
291 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
292 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
293 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
294 * The helpers' return value is the rate of the clock that is fed to the
295 * display engine's pipe which can be the above fast dot clock rate or a
296 * divided-down version of it.
298 /* m1 is reserved as 0 in Pineview, n is a ring counter */
299 int pnv_calc_dpll_params(int refclk, struct dpll *clock)
301 clock->m = clock->m2 + 2;
302 clock->p = clock->p1 * clock->p2;
303 if (WARN_ON(clock->n == 0 || clock->p == 0))
305 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
306 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
311 static u32 i9xx_dpll_compute_m(struct dpll *dpll)
313 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
316 int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
318 clock->m = i9xx_dpll_compute_m(clock);
319 clock->p = clock->p1 * clock->p2;
320 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
322 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
323 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
328 int vlv_calc_dpll_params(int refclk, struct dpll *clock)
330 clock->m = clock->m1 * clock->m2;
331 clock->p = clock->p1 * clock->p2;
332 if (WARN_ON(clock->n == 0 || clock->p == 0))
334 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
335 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
337 return clock->dot / 5;
340 int chv_calc_dpll_params(int refclk, struct dpll *clock)
342 clock->m = clock->m1 * clock->m2;
343 clock->p = clock->p1 * clock->p2;
344 if (WARN_ON(clock->n == 0 || clock->p == 0))
346 clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
348 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
350 return clock->dot / 5;
354 * Returns whether the given set of divisors are valid for a given refclk with
355 * the given connectors.
357 static bool intel_pll_is_valid(struct drm_i915_private *dev_priv,
358 const struct intel_limit *limit,
359 const struct dpll *clock)
361 if (clock->n < limit->n.min || limit->n.max < clock->n)
363 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
365 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
367 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
370 if (!IS_PINEVIEW(dev_priv) && !IS_LP(dev_priv))
371 if (clock->m1 <= clock->m2)
374 if (!IS_LP(dev_priv)) {
375 if (clock->p < limit->p.min || limit->p.max < clock->p)
377 if (clock->m < limit->m.min || limit->m.max < clock->m)
381 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
383 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
384 * connector, etc., rather than just a single range.
386 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
393 i9xx_select_p2_div(const struct intel_limit *limit,
394 const struct intel_crtc_state *crtc_state,
397 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
399 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
401 * For LVDS just rely on its current settings for dual-channel.
402 * We haven't figured out how to reliably set up different
403 * single/dual channel state, if we even can.
405 if (intel_is_dual_link_lvds(dev_priv))
406 return limit->p2.p2_fast;
408 return limit->p2.p2_slow;
410 if (target < limit->p2.dot_limit)
411 return limit->p2.p2_slow;
413 return limit->p2.p2_fast;
418 * Returns a set of divisors for the desired target clock with the given
419 * refclk, or FALSE. The returned values represent the clock equation:
420 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
422 * Target and reference clocks are specified in kHz.
424 * If match_clock is provided, then best_clock P divider must match the P
425 * divider from @match_clock used for LVDS downclocking.
428 i9xx_find_best_dpll(const struct intel_limit *limit,
429 struct intel_crtc_state *crtc_state,
430 int target, int refclk, struct dpll *match_clock,
431 struct dpll *best_clock)
433 struct drm_device *dev = crtc_state->uapi.crtc->dev;
437 memset(best_clock, 0, sizeof(*best_clock));
439 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
441 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
443 for (clock.m2 = limit->m2.min;
444 clock.m2 <= limit->m2.max; clock.m2++) {
445 if (clock.m2 >= clock.m1)
447 for (clock.n = limit->n.min;
448 clock.n <= limit->n.max; clock.n++) {
449 for (clock.p1 = limit->p1.min;
450 clock.p1 <= limit->p1.max; clock.p1++) {
453 i9xx_calc_dpll_params(refclk, &clock);
454 if (!intel_pll_is_valid(to_i915(dev),
459 clock.p != match_clock->p)
462 this_err = abs(clock.dot - target);
463 if (this_err < err) {
472 return (err != target);
476 * Returns a set of divisors for the desired target clock with the given
477 * refclk, or FALSE. The returned values represent the clock equation:
478 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
480 * Target and reference clocks are specified in kHz.
482 * If match_clock is provided, then best_clock P divider must match the P
483 * divider from @match_clock used for LVDS downclocking.
486 pnv_find_best_dpll(const struct intel_limit *limit,
487 struct intel_crtc_state *crtc_state,
488 int target, int refclk, struct dpll *match_clock,
489 struct dpll *best_clock)
491 struct drm_device *dev = crtc_state->uapi.crtc->dev;
495 memset(best_clock, 0, sizeof(*best_clock));
497 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
499 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
501 for (clock.m2 = limit->m2.min;
502 clock.m2 <= limit->m2.max; clock.m2++) {
503 for (clock.n = limit->n.min;
504 clock.n <= limit->n.max; clock.n++) {
505 for (clock.p1 = limit->p1.min;
506 clock.p1 <= limit->p1.max; clock.p1++) {
509 pnv_calc_dpll_params(refclk, &clock);
510 if (!intel_pll_is_valid(to_i915(dev),
515 clock.p != match_clock->p)
518 this_err = abs(clock.dot - target);
519 if (this_err < err) {
528 return (err != target);
532 * Returns a set of divisors for the desired target clock with the given
533 * refclk, or FALSE. The returned values represent the clock equation:
534 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
536 * Target and reference clocks are specified in kHz.
538 * If match_clock is provided, then best_clock P divider must match the P
539 * divider from @match_clock used for LVDS downclocking.
542 g4x_find_best_dpll(const struct intel_limit *limit,
543 struct intel_crtc_state *crtc_state,
544 int target, int refclk, struct dpll *match_clock,
545 struct dpll *best_clock)
547 struct drm_device *dev = crtc_state->uapi.crtc->dev;
551 /* approximately equals target * 0.00585 */
552 int err_most = (target >> 8) + (target >> 9);
554 memset(best_clock, 0, sizeof(*best_clock));
556 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
558 max_n = limit->n.max;
559 /* based on hardware requirement, prefer smaller n to precision */
560 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
561 /* based on hardware requirement, prefere larger m1,m2 */
562 for (clock.m1 = limit->m1.max;
563 clock.m1 >= limit->m1.min; clock.m1--) {
564 for (clock.m2 = limit->m2.max;
565 clock.m2 >= limit->m2.min; clock.m2--) {
566 for (clock.p1 = limit->p1.max;
567 clock.p1 >= limit->p1.min; clock.p1--) {
570 i9xx_calc_dpll_params(refclk, &clock);
571 if (!intel_pll_is_valid(to_i915(dev),
576 this_err = abs(clock.dot - target);
577 if (this_err < err_most) {
591 * Check if the calculated PLL configuration is more optimal compared to the
592 * best configuration and error found so far. Return the calculated error.
594 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
595 const struct dpll *calculated_clock,
596 const struct dpll *best_clock,
597 unsigned int best_error_ppm,
598 unsigned int *error_ppm)
601 * For CHV ignore the error and consider only the P value.
602 * Prefer a bigger P value based on HW requirements.
604 if (IS_CHERRYVIEW(to_i915(dev))) {
607 return calculated_clock->p > best_clock->p;
610 if (drm_WARN_ON_ONCE(dev, !target_freq))
613 *error_ppm = div_u64(1000000ULL *
614 abs(target_freq - calculated_clock->dot),
617 * Prefer a better P value over a better (smaller) error if the error
618 * is small. Ensure this preference for future configurations too by
619 * setting the error to 0.
621 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
627 return *error_ppm + 10 < best_error_ppm;
631 * Returns a set of divisors for the desired target clock with the given
632 * refclk, or FALSE. The returned values represent the clock equation:
633 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
636 vlv_find_best_dpll(const struct intel_limit *limit,
637 struct intel_crtc_state *crtc_state,
638 int target, int refclk, struct dpll *match_clock,
639 struct dpll *best_clock)
641 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
642 struct drm_device *dev = crtc->base.dev;
644 unsigned int bestppm = 1000000;
645 /* min update 19.2 MHz */
646 int max_n = min(limit->n.max, refclk / 19200);
649 target *= 5; /* fast clock */
651 memset(best_clock, 0, sizeof(*best_clock));
653 /* based on hardware requirement, prefer smaller n to precision */
654 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
655 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
656 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
657 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
658 clock.p = clock.p1 * clock.p2;
659 /* based on hardware requirement, prefer bigger m1,m2 values */
660 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
663 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
666 vlv_calc_dpll_params(refclk, &clock);
668 if (!intel_pll_is_valid(to_i915(dev),
673 if (!vlv_PLL_is_optimal(dev, target,
691 * Returns a set of divisors for the desired target clock with the given
692 * refclk, or FALSE. The returned values represent the clock equation:
693 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
696 chv_find_best_dpll(const struct intel_limit *limit,
697 struct intel_crtc_state *crtc_state,
698 int target, int refclk, struct dpll *match_clock,
699 struct dpll *best_clock)
701 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
702 struct drm_device *dev = crtc->base.dev;
703 unsigned int best_error_ppm;
708 memset(best_clock, 0, sizeof(*best_clock));
709 best_error_ppm = 1000000;
712 * Based on hardware doc, the n always set to 1, and m1 always
713 * set to 2. If requires to support 200Mhz refclk, we need to
714 * revisit this because n may not 1 anymore.
718 target *= 5; /* fast clock */
720 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
721 for (clock.p2 = limit->p2.p2_fast;
722 clock.p2 >= limit->p2.p2_slow;
723 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
724 unsigned int error_ppm;
726 clock.p = clock.p1 * clock.p2;
728 m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
731 if (m2 > INT_MAX/clock.m1)
736 chv_calc_dpll_params(refclk, &clock);
738 if (!intel_pll_is_valid(to_i915(dev), limit, &clock))
741 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
742 best_error_ppm, &error_ppm))
746 best_error_ppm = error_ppm;
754 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
755 struct dpll *best_clock)
758 const struct intel_limit *limit = &intel_limits_bxt;
760 return chv_find_best_dpll(limit, crtc_state,
761 crtc_state->port_clock, refclk,
765 static u32 pnv_dpll_compute_fp(struct dpll *dpll)
767 return (1 << dpll->n) << 16 | dpll->m2;
770 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
771 struct intel_crtc_state *crtc_state,
772 struct dpll *reduced_clock)
774 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
777 if (IS_PINEVIEW(dev_priv)) {
778 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
780 fp2 = pnv_dpll_compute_fp(reduced_clock);
782 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
784 fp2 = i9xx_dpll_compute_fp(reduced_clock);
787 crtc_state->dpll_hw_state.fp0 = fp;
789 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
791 crtc_state->dpll_hw_state.fp1 = fp2;
793 crtc_state->dpll_hw_state.fp1 = fp;
797 static void i9xx_compute_dpll(struct intel_crtc *crtc,
798 struct intel_crtc_state *crtc_state,
799 struct dpll *reduced_clock)
801 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
803 struct dpll *clock = &crtc_state->dpll;
805 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
807 dpll = DPLL_VGA_MODE_DIS;
809 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
810 dpll |= DPLLB_MODE_LVDS;
812 dpll |= DPLLB_MODE_DAC_SERIAL;
814 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
815 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
816 dpll |= (crtc_state->pixel_multiplier - 1)
817 << SDVO_MULTIPLIER_SHIFT_HIRES;
820 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
821 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
822 dpll |= DPLL_SDVO_HIGH_SPEED;
824 if (intel_crtc_has_dp_encoder(crtc_state))
825 dpll |= DPLL_SDVO_HIGH_SPEED;
827 /* compute bitmask from p1 value */
828 if (IS_PINEVIEW(dev_priv))
829 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
831 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
832 if (IS_G4X(dev_priv) && reduced_clock)
833 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
837 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
840 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
843 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
846 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
849 if (DISPLAY_VER(dev_priv) >= 4)
850 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
852 if (crtc_state->sdvo_tv_clock)
853 dpll |= PLL_REF_INPUT_TVCLKINBC;
854 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
855 intel_panel_use_ssc(dev_priv))
856 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
858 dpll |= PLL_REF_INPUT_DREFCLK;
860 dpll |= DPLL_VCO_ENABLE;
861 crtc_state->dpll_hw_state.dpll = dpll;
863 if (DISPLAY_VER(dev_priv) >= 4) {
864 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
865 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
866 crtc_state->dpll_hw_state.dpll_md = dpll_md;
870 static void i8xx_compute_dpll(struct intel_crtc *crtc,
871 struct intel_crtc_state *crtc_state,
872 struct dpll *reduced_clock)
874 struct drm_device *dev = crtc->base.dev;
875 struct drm_i915_private *dev_priv = to_i915(dev);
877 struct dpll *clock = &crtc_state->dpll;
879 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
881 dpll = DPLL_VGA_MODE_DIS;
883 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
884 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
887 dpll |= PLL_P1_DIVIDE_BY_TWO;
889 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
891 dpll |= PLL_P2_DIVIDE_BY_4;
896 * "[Almador Errata}: For the correct operation of the muxed DVO pins
897 * (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
898 * GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
899 * Enable) must be set to “1” in both the DPLL A Control Register
900 * (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
902 * For simplicity We simply keep both bits always enabled in
903 * both DPLLS. The spec says we should disable the DVO 2X clock
904 * when not needed, but this seems to work fine in practice.
906 if (IS_I830(dev_priv) ||
907 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
908 dpll |= DPLL_DVO_2X_MODE;
910 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
911 intel_panel_use_ssc(dev_priv))
912 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
914 dpll |= PLL_REF_INPUT_DREFCLK;
916 dpll |= DPLL_VCO_ENABLE;
917 crtc_state->dpll_hw_state.dpll = dpll;
920 static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
921 struct intel_crtc_state *crtc_state)
923 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
924 struct intel_atomic_state *state =
925 to_intel_atomic_state(crtc_state->uapi.state);
927 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
928 DISPLAY_VER(dev_priv) >= 11) {
929 struct intel_encoder *encoder =
930 intel_get_crtc_new_encoder(state, crtc_state);
932 if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
933 drm_dbg_kms(&dev_priv->drm,
934 "failed to find PLL for pipe %c\n",
935 pipe_name(crtc->pipe));
943 static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
945 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
949 static void ilk_compute_dpll(struct intel_crtc *crtc,
950 struct intel_crtc_state *crtc_state,
951 struct dpll *reduced_clock)
953 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
957 /* Enable autotuning of the PLL clock (if permissible) */
959 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
960 if ((intel_panel_use_ssc(dev_priv) &&
961 dev_priv->vbt.lvds_ssc_freq == 100000) ||
962 (HAS_PCH_IBX(dev_priv) &&
963 intel_is_dual_link_lvds(dev_priv)))
965 } else if (crtc_state->sdvo_tv_clock) {
969 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
971 if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
975 fp2 = i9xx_dpll_compute_fp(reduced_clock);
977 if (reduced_clock->m < factor * reduced_clock->n)
985 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
986 dpll |= DPLLB_MODE_LVDS;
988 dpll |= DPLLB_MODE_DAC_SERIAL;
990 dpll |= (crtc_state->pixel_multiplier - 1)
991 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
993 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
994 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
995 dpll |= DPLL_SDVO_HIGH_SPEED;
997 if (intel_crtc_has_dp_encoder(crtc_state))
998 dpll |= DPLL_SDVO_HIGH_SPEED;
1001 * The high speed IO clock is only really required for
1002 * SDVO/HDMI/DP, but we also enable it for CRT to make it
1003 * possible to share the DPLL between CRT and HDMI. Enabling
1004 * the clock needlessly does no real harm, except use up a
1005 * bit of power potentially.
1007 * We'll limit this to IVB with 3 pipes, since it has only two
1008 * DPLLs and so DPLL sharing is the only way to get three pipes
1009 * driving PCH ports at the same time. On SNB we could do this,
1010 * and potentially avoid enabling the second DPLL, but it's not
1011 * clear if it''s a win or loss power wise. No point in doing
1012 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
1014 if (INTEL_NUM_PIPES(dev_priv) == 3 &&
1015 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
1016 dpll |= DPLL_SDVO_HIGH_SPEED;
1018 /* compute bitmask from p1 value */
1019 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
1021 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
1023 switch (crtc_state->dpll.p2) {
1025 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
1028 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
1031 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
1034 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
1038 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
1039 intel_panel_use_ssc(dev_priv))
1040 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
1042 dpll |= PLL_REF_INPUT_DREFCLK;
1044 dpll |= DPLL_VCO_ENABLE;
1046 crtc_state->dpll_hw_state.dpll = dpll;
1047 crtc_state->dpll_hw_state.fp0 = fp;
1048 crtc_state->dpll_hw_state.fp1 = fp2;
1051 static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
1052 struct intel_crtc_state *crtc_state)
1054 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1055 struct intel_atomic_state *state =
1056 to_intel_atomic_state(crtc_state->uapi.state);
1057 const struct intel_limit *limit;
1058 int refclk = 120000;
1060 memset(&crtc_state->dpll_hw_state, 0,
1061 sizeof(crtc_state->dpll_hw_state));
1063 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
1064 if (!crtc_state->has_pch_encoder)
1067 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1068 if (intel_panel_use_ssc(dev_priv)) {
1069 drm_dbg_kms(&dev_priv->drm,
1070 "using SSC reference clock of %d kHz\n",
1071 dev_priv->vbt.lvds_ssc_freq);
1072 refclk = dev_priv->vbt.lvds_ssc_freq;
1075 if (intel_is_dual_link_lvds(dev_priv)) {
1076 if (refclk == 100000)
1077 limit = &ilk_limits_dual_lvds_100m;
1079 limit = &ilk_limits_dual_lvds;
1081 if (refclk == 100000)
1082 limit = &ilk_limits_single_lvds_100m;
1084 limit = &ilk_limits_single_lvds;
1087 limit = &ilk_limits_dac;
1090 if (!crtc_state->clock_set &&
1091 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1092 refclk, NULL, &crtc_state->dpll)) {
1093 drm_err(&dev_priv->drm,
1094 "Couldn't find PLL settings for mode!\n");
1098 ilk_compute_dpll(crtc, crtc_state, NULL);
1100 if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
1101 drm_dbg_kms(&dev_priv->drm,
1102 "failed to find PLL for pipe %c\n",
1103 pipe_name(crtc->pipe));
1110 void vlv_compute_dpll(struct intel_crtc *crtc,
1111 struct intel_crtc_state *pipe_config)
1113 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
1114 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1115 if (crtc->pipe != PIPE_A)
1116 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1118 /* DPLL not used with DSI, but still need the rest set up */
1119 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
1120 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
1121 DPLL_EXT_BUFFER_ENABLE_VLV;
1123 pipe_config->dpll_hw_state.dpll_md =
1124 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1127 void chv_compute_dpll(struct intel_crtc *crtc,
1128 struct intel_crtc_state *pipe_config)
1130 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
1131 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1132 if (crtc->pipe != PIPE_A)
1133 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1135 /* DPLL not used with DSI, but still need the rest set up */
1136 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
1137 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
1139 pipe_config->dpll_hw_state.dpll_md =
1140 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1143 static int chv_crtc_compute_clock(struct intel_crtc *crtc,
1144 struct intel_crtc_state *crtc_state)
1146 int refclk = 100000;
1147 const struct intel_limit *limit = &intel_limits_chv;
1148 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1150 memset(&crtc_state->dpll_hw_state, 0,
1151 sizeof(crtc_state->dpll_hw_state));
1153 if (!crtc_state->clock_set &&
1154 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1155 refclk, NULL, &crtc_state->dpll)) {
1156 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
1160 chv_compute_dpll(crtc, crtc_state);
1165 static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
1166 struct intel_crtc_state *crtc_state)
1168 int refclk = 100000;
1169 const struct intel_limit *limit = &intel_limits_vlv;
1170 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1172 memset(&crtc_state->dpll_hw_state, 0,
1173 sizeof(crtc_state->dpll_hw_state));
1175 if (!crtc_state->clock_set &&
1176 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1177 refclk, NULL, &crtc_state->dpll)) {
1178 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
1182 vlv_compute_dpll(crtc, crtc_state);
1187 static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
1188 struct intel_crtc_state *crtc_state)
1190 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1191 const struct intel_limit *limit;
1194 memset(&crtc_state->dpll_hw_state, 0,
1195 sizeof(crtc_state->dpll_hw_state));
1197 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1198 if (intel_panel_use_ssc(dev_priv)) {
1199 refclk = dev_priv->vbt.lvds_ssc_freq;
1200 drm_dbg_kms(&dev_priv->drm,
1201 "using SSC reference clock of %d kHz\n",
1205 if (intel_is_dual_link_lvds(dev_priv))
1206 limit = &intel_limits_g4x_dual_channel_lvds;
1208 limit = &intel_limits_g4x_single_channel_lvds;
1209 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
1210 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
1211 limit = &intel_limits_g4x_hdmi;
1212 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
1213 limit = &intel_limits_g4x_sdvo;
1215 /* The option is for other outputs */
1216 limit = &intel_limits_i9xx_sdvo;
1219 if (!crtc_state->clock_set &&
1220 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1221 refclk, NULL, &crtc_state->dpll)) {
1222 drm_err(&dev_priv->drm,
1223 "Couldn't find PLL settings for mode!\n");
1227 i9xx_compute_dpll(crtc, crtc_state, NULL);
1232 static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
1233 struct intel_crtc_state *crtc_state)
1235 struct drm_device *dev = crtc->base.dev;
1236 struct drm_i915_private *dev_priv = to_i915(dev);
1237 const struct intel_limit *limit;
1240 memset(&crtc_state->dpll_hw_state, 0,
1241 sizeof(crtc_state->dpll_hw_state));
1243 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1244 if (intel_panel_use_ssc(dev_priv)) {
1245 refclk = dev_priv->vbt.lvds_ssc_freq;
1246 drm_dbg_kms(&dev_priv->drm,
1247 "using SSC reference clock of %d kHz\n",
1251 limit = &pnv_limits_lvds;
1253 limit = &pnv_limits_sdvo;
1256 if (!crtc_state->clock_set &&
1257 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1258 refclk, NULL, &crtc_state->dpll)) {
1259 drm_err(&dev_priv->drm,
1260 "Couldn't find PLL settings for mode!\n");
1264 i9xx_compute_dpll(crtc, crtc_state, NULL);
1269 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
1270 struct intel_crtc_state *crtc_state)
1272 struct drm_device *dev = crtc->base.dev;
1273 struct drm_i915_private *dev_priv = to_i915(dev);
1274 const struct intel_limit *limit;
1277 memset(&crtc_state->dpll_hw_state, 0,
1278 sizeof(crtc_state->dpll_hw_state));
1280 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1281 if (intel_panel_use_ssc(dev_priv)) {
1282 refclk = dev_priv->vbt.lvds_ssc_freq;
1283 drm_dbg_kms(&dev_priv->drm,
1284 "using SSC reference clock of %d kHz\n",
1288 limit = &intel_limits_i9xx_lvds;
1290 limit = &intel_limits_i9xx_sdvo;
1293 if (!crtc_state->clock_set &&
1294 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1295 refclk, NULL, &crtc_state->dpll)) {
1296 drm_err(&dev_priv->drm,
1297 "Couldn't find PLL settings for mode!\n");
1301 i9xx_compute_dpll(crtc, crtc_state, NULL);
1306 static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
1307 struct intel_crtc_state *crtc_state)
1309 struct drm_device *dev = crtc->base.dev;
1310 struct drm_i915_private *dev_priv = to_i915(dev);
1311 const struct intel_limit *limit;
1314 memset(&crtc_state->dpll_hw_state, 0,
1315 sizeof(crtc_state->dpll_hw_state));
1317 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1318 if (intel_panel_use_ssc(dev_priv)) {
1319 refclk = dev_priv->vbt.lvds_ssc_freq;
1320 drm_dbg_kms(&dev_priv->drm,
1321 "using SSC reference clock of %d kHz\n",
1325 limit = &intel_limits_i8xx_lvds;
1326 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
1327 limit = &intel_limits_i8xx_dvo;
1329 limit = &intel_limits_i8xx_dac;
1332 if (!crtc_state->clock_set &&
1333 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1334 refclk, NULL, &crtc_state->dpll)) {
1335 drm_err(&dev_priv->drm,
1336 "Couldn't find PLL settings for mode!\n");
1340 i8xx_compute_dpll(crtc, crtc_state, NULL);
1346 intel_dpll_init_clock_hook(struct drm_i915_private *dev_priv)
1348 if (DISPLAY_VER(dev_priv) >= 9 || HAS_DDI(dev_priv))
1349 dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
1350 else if (HAS_PCH_SPLIT(dev_priv))
1351 dev_priv->display.crtc_compute_clock = ilk_crtc_compute_clock;
1352 else if (IS_CHERRYVIEW(dev_priv))
1353 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
1354 else if (IS_VALLEYVIEW(dev_priv))
1355 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
1356 else if (IS_G4X(dev_priv))
1357 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
1358 else if (IS_PINEVIEW(dev_priv))
1359 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
1360 else if (DISPLAY_VER(dev_priv) != 2)
1361 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
1363 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
1366 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1368 if (IS_I830(dev_priv))
1371 return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1374 void i9xx_enable_pll(struct intel_crtc *crtc,
1375 const struct intel_crtc_state *crtc_state)
1377 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1378 i915_reg_t reg = DPLL(crtc->pipe);
1379 u32 dpll = crtc_state->dpll_hw_state.dpll;
1382 assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1384 /* PLL is protected by panel, make sure we can write it */
1385 if (i9xx_has_pps(dev_priv))
1386 assert_panel_unlocked(dev_priv, crtc->pipe);
1389 * Apparently we need to have VGA mode enabled prior to changing
1390 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1391 * dividers, even though the register value does change.
1393 intel_de_write(dev_priv, reg, dpll & ~DPLL_VGA_MODE_DIS);
1394 intel_de_write(dev_priv, reg, dpll);
1396 /* Wait for the clocks to stabilize. */
1397 intel_de_posting_read(dev_priv, reg);
1400 if (DISPLAY_VER(dev_priv) >= 4) {
1401 intel_de_write(dev_priv, DPLL_MD(crtc->pipe),
1402 crtc_state->dpll_hw_state.dpll_md);
1404 /* The pixel multiplier can only be updated once the
1405 * DPLL is enabled and the clocks are stable.
1407 * So write it again.
1409 intel_de_write(dev_priv, reg, dpll);
1412 /* We do this three times for luck */
1413 for (i = 0; i < 3; i++) {
1414 intel_de_write(dev_priv, reg, dpll);
1415 intel_de_posting_read(dev_priv, reg);
1416 udelay(150); /* wait for warmup */
1420 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv,
1426 * PLLB opamp always calibrates to max value of 0x3f, force enable it
1427 * and set it to a reasonable value instead.
1429 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
1430 reg_val &= 0xffffff00;
1431 reg_val |= 0x00000030;
1432 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
1434 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
1435 reg_val &= 0x00ffffff;
1436 reg_val |= 0x8c000000;
1437 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
1439 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
1440 reg_val &= 0xffffff00;
1441 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
1443 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
1444 reg_val &= 0x00ffffff;
1445 reg_val |= 0xb0000000;
1446 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
1449 static void _vlv_enable_pll(struct intel_crtc *crtc,
1450 const struct intel_crtc_state *pipe_config)
1452 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1453 enum pipe pipe = crtc->pipe;
1455 intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1456 intel_de_posting_read(dev_priv, DPLL(pipe));
1459 if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1460 drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
1463 void vlv_enable_pll(struct intel_crtc *crtc,
1464 const struct intel_crtc_state *pipe_config)
1466 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1467 enum pipe pipe = crtc->pipe;
1469 assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1471 /* PLL is protected by panel, make sure we can write it */
1472 assert_panel_unlocked(dev_priv, pipe);
1474 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1475 _vlv_enable_pll(crtc, pipe_config);
1477 intel_de_write(dev_priv, DPLL_MD(pipe),
1478 pipe_config->dpll_hw_state.dpll_md);
1479 intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1483 static void _chv_enable_pll(struct intel_crtc *crtc,
1484 const struct intel_crtc_state *pipe_config)
1486 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1487 enum pipe pipe = crtc->pipe;
1488 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1491 vlv_dpio_get(dev_priv);
1493 /* Enable back the 10bit clock to display controller */
1494 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1495 tmp |= DPIO_DCLKP_EN;
1496 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1498 vlv_dpio_put(dev_priv);
1501 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1506 intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1508 /* Check PLL is locked */
1509 if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1510 drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
1513 void chv_enable_pll(struct intel_crtc *crtc,
1514 const struct intel_crtc_state *pipe_config)
1516 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1517 enum pipe pipe = crtc->pipe;
1519 assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1521 /* PLL is protected by panel, make sure we can write it */
1522 assert_panel_unlocked(dev_priv, pipe);
1524 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1525 _chv_enable_pll(crtc, pipe_config);
1527 if (pipe != PIPE_A) {
1529 * WaPixelRepeatModeFixForC0:chv
1531 * DPLLCMD is AWOL. Use chicken bits to propagate
1532 * the value from DPLLBMD to either pipe B or C.
1534 intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1535 intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1536 pipe_config->dpll_hw_state.dpll_md);
1537 intel_de_write(dev_priv, CBR4_VLV, 0);
1538 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1541 * DPLLB VGA mode also seems to cause problems.
1542 * We should always have it disabled.
1544 drm_WARN_ON(&dev_priv->drm,
1545 (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1546 DPLL_VGA_MODE_DIS) == 0);
1548 intel_de_write(dev_priv, DPLL_MD(pipe),
1549 pipe_config->dpll_hw_state.dpll_md);
1550 intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1554 void vlv_prepare_pll(struct intel_crtc *crtc,
1555 const struct intel_crtc_state *pipe_config)
1557 struct drm_device *dev = crtc->base.dev;
1558 struct drm_i915_private *dev_priv = to_i915(dev);
1559 enum pipe pipe = crtc->pipe;
1561 u32 bestn, bestm1, bestm2, bestp1, bestp2;
1562 u32 coreclk, reg_val;
1565 intel_de_write(dev_priv, DPLL(pipe),
1566 pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
1568 /* No need to actually set up the DPLL with DSI */
1569 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
1572 vlv_dpio_get(dev_priv);
1574 bestn = pipe_config->dpll.n;
1575 bestm1 = pipe_config->dpll.m1;
1576 bestm2 = pipe_config->dpll.m2;
1577 bestp1 = pipe_config->dpll.p1;
1578 bestp2 = pipe_config->dpll.p2;
1580 /* See eDP HDMI DPIO driver vbios notes doc */
1582 /* PLL B needs special handling */
1584 vlv_pllb_recal_opamp(dev_priv, pipe);
1586 /* Set up Tx target for periodic Rcomp update */
1587 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
1589 /* Disable target IRef on PLL */
1590 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
1591 reg_val &= 0x00ffffff;
1592 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
1594 /* Disable fast lock */
1595 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
1597 /* Set idtafcrecal before PLL is enabled */
1598 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
1599 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
1600 mdiv |= ((bestn << DPIO_N_SHIFT));
1601 mdiv |= (1 << DPIO_K_SHIFT);
1604 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
1605 * but we don't support that).
1606 * Note: don't use the DAC post divider as it seems unstable.
1608 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
1609 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
1611 mdiv |= DPIO_ENABLE_CALIBRATION;
1612 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
1614 /* Set HBR and RBR LPF coefficients */
1615 if (pipe_config->port_clock == 162000 ||
1616 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
1617 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
1618 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
1621 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
1624 if (intel_crtc_has_dp_encoder(pipe_config)) {
1625 /* Use SSC source */
1627 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1630 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1632 } else { /* HDMI or VGA */
1633 /* Use bend source */
1635 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1638 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1642 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
1643 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
1644 if (intel_crtc_has_dp_encoder(pipe_config))
1645 coreclk |= 0x01000000;
1646 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
1648 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
1650 vlv_dpio_put(dev_priv);
1653 void chv_prepare_pll(struct intel_crtc *crtc,
1654 const struct intel_crtc_state *pipe_config)
1656 struct drm_device *dev = crtc->base.dev;
1657 struct drm_i915_private *dev_priv = to_i915(dev);
1658 enum pipe pipe = crtc->pipe;
1659 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1660 u32 loopfilter, tribuf_calcntr;
1661 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
1665 /* Enable Refclk and SSC */
1666 intel_de_write(dev_priv, DPLL(pipe),
1667 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
1669 /* No need to actually set up the DPLL with DSI */
1670 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
1673 bestn = pipe_config->dpll.n;
1674 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
1675 bestm1 = pipe_config->dpll.m1;
1676 bestm2 = pipe_config->dpll.m2 >> 22;
1677 bestp1 = pipe_config->dpll.p1;
1678 bestp2 = pipe_config->dpll.p2;
1679 vco = pipe_config->dpll.vco;
1683 vlv_dpio_get(dev_priv);
1685 /* p1 and p2 divider */
1686 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
1687 5 << DPIO_CHV_S1_DIV_SHIFT |
1688 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
1689 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
1690 1 << DPIO_CHV_K_DIV_SHIFT);
1692 /* Feedback post-divider - m2 */
1693 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
1695 /* Feedback refclk divider - n and m1 */
1696 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
1697 DPIO_CHV_M1_DIV_BY_2 |
1698 1 << DPIO_CHV_N_DIV_SHIFT);
1700 /* M2 fraction division */
1701 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
1703 /* M2 fraction division enable */
1704 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
1705 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
1706 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
1708 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
1709 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
1711 /* Program digital lock detect threshold */
1712 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
1713 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
1714 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
1715 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
1717 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
1718 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
1721 if (vco == 5400000) {
1722 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
1723 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
1724 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
1725 tribuf_calcntr = 0x9;
1726 } else if (vco <= 6200000) {
1727 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
1728 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
1729 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1730 tribuf_calcntr = 0x9;
1731 } else if (vco <= 6480000) {
1732 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
1733 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
1734 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1735 tribuf_calcntr = 0x8;
1737 /* Not supported. Apply the same limits as in the max case */
1738 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
1739 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
1740 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1743 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
1745 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
1746 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
1747 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
1748 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
1751 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
1752 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
1755 vlv_dpio_put(dev_priv);
1759 * vlv_force_pll_on - forcibly enable just the PLL
1760 * @dev_priv: i915 private structure
1761 * @pipe: pipe PLL to enable
1762 * @dpll: PLL configuration
1764 * Enable the PLL for @pipe using the supplied @dpll config. To be used
1765 * in cases where we need the PLL enabled even when @pipe is not going to
1768 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
1769 const struct dpll *dpll)
1771 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
1772 struct intel_crtc_state *pipe_config;
1774 pipe_config = intel_crtc_state_alloc(crtc);
1778 pipe_config->cpu_transcoder = (enum transcoder)pipe;
1779 pipe_config->pixel_multiplier = 1;
1780 pipe_config->dpll = *dpll;
1782 if (IS_CHERRYVIEW(dev_priv)) {
1783 chv_compute_dpll(crtc, pipe_config);
1784 chv_prepare_pll(crtc, pipe_config);
1785 chv_enable_pll(crtc, pipe_config);
1787 vlv_compute_dpll(crtc, pipe_config);
1788 vlv_prepare_pll(crtc, pipe_config);
1789 vlv_enable_pll(crtc, pipe_config);
1797 void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1801 /* Make sure the pipe isn't still relying on us */
1802 assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1804 val = DPLL_INTEGRATED_REF_CLK_VLV |
1805 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1807 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1809 intel_de_write(dev_priv, DPLL(pipe), val);
1810 intel_de_posting_read(dev_priv, DPLL(pipe));
1813 void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1815 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1818 /* Make sure the pipe isn't still relying on us */
1819 assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1821 val = DPLL_SSC_REF_CLK_CHV |
1822 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1824 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1826 intel_de_write(dev_priv, DPLL(pipe), val);
1827 intel_de_posting_read(dev_priv, DPLL(pipe));
1829 vlv_dpio_get(dev_priv);
1831 /* Disable 10bit clock to display controller */
1832 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1833 val &= ~DPIO_DCLKP_EN;
1834 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1836 vlv_dpio_put(dev_priv);
1839 void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1841 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1842 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1843 enum pipe pipe = crtc->pipe;
1845 /* Don't disable pipe or pipe PLLs if needed */
1846 if (IS_I830(dev_priv))
1849 /* Make sure the pipe isn't still relying on us */
1850 assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1852 intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1853 intel_de_posting_read(dev_priv, DPLL(pipe));
1858 * vlv_force_pll_off - forcibly disable just the PLL
1859 * @dev_priv: i915 private structure
1860 * @pipe: pipe PLL to disable
1862 * Disable the PLL for @pipe. To be used in cases where we need
1863 * the PLL enabled even when @pipe is not going to be enabled.
1865 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
1867 if (IS_CHERRYVIEW(dev_priv))
1868 chv_disable_pll(dev_priv, pipe);
1870 vlv_disable_pll(dev_priv, pipe);