drm/i915: Remove incorrect comment about struct mutex
[linux-2.6-block.git] / drivers / gpu / drm / i915 / intel_ddi.c
CommitLineData
45244b87
ED
1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
28#include "i915_drv.h"
29#include "intel_drv.h"
30
31/* HDMI/DVI modes ignore everything but the last 2 items. So we share
32 * them for both DP and FDI transports, allowing those ports to
33 * automatically adapt to HDMI connections as well
34 */
35static const u32 hsw_ddi_translations_dp[] = {
36 0x00FFFFFF, 0x0006000E, /* DP parameters */
37 0x00D75FFF, 0x0005000A,
38 0x00C30FFF, 0x00040006,
39 0x80AAAFFF, 0x000B0000,
40 0x00FFFFFF, 0x0005000A,
41 0x00D75FFF, 0x000C0004,
42 0x80C30FFF, 0x000B0000,
43 0x00FFFFFF, 0x00040006,
44 0x80D75FFF, 0x000B0000,
45244b87
ED
45};
46
47static const u32 hsw_ddi_translations_fdi[] = {
48 0x00FFFFFF, 0x0007000E, /* FDI parameters */
49 0x00D75FFF, 0x000F000A,
50 0x00C30FFF, 0x00060006,
51 0x00AAAFFF, 0x001E0000,
52 0x00FFFFFF, 0x000F000A,
53 0x00D75FFF, 0x00160004,
54 0x00C30FFF, 0x001E0000,
55 0x00FFFFFF, 0x00060006,
56 0x00D75FFF, 0x001E0000,
6acab15a
PZ
57};
58
59static const u32 hsw_ddi_translations_hdmi[] = {
60 /* Idx NT mV diff T mV diff db */
61 0x00FFFFFF, 0x0006000E, /* 0: 400 400 0 */
62 0x00E79FFF, 0x000E000C, /* 1: 400 500 2 */
63 0x00D75FFF, 0x0005000A, /* 2: 400 600 3.5 */
64 0x00FFFFFF, 0x0005000A, /* 3: 600 600 0 */
65 0x00E79FFF, 0x001D0007, /* 4: 600 750 2 */
66 0x00D75FFF, 0x000C0004, /* 5: 600 900 3.5 */
67 0x00FFFFFF, 0x00040006, /* 6: 800 800 0 */
68 0x80E79FFF, 0x00030002, /* 7: 800 1000 2 */
69 0x00FFFFFF, 0x00140005, /* 8: 850 850 0 */
70 0x00FFFFFF, 0x000C0004, /* 9: 900 900 0 */
71 0x00FFFFFF, 0x001C0003, /* 10: 950 950 0 */
72 0x80FFFFFF, 0x00030002, /* 11: 1000 1000 0 */
45244b87
ED
73};
74
300644c7 75static const u32 bdw_ddi_translations_edp[] = {
e1b22732 76 0x00FFFFFF, 0x00000012, /* eDP parameters */
300644c7
PZ
77 0x00EBAFFF, 0x00020011,
78 0x00C71FFF, 0x0006000F,
79 0x00FFFFFF, 0x00020011,
80 0x00DB6FFF, 0x0005000F,
81 0x00BEEFFF, 0x000A000C,
82 0x00FFFFFF, 0x0005000F,
83 0x00DB6FFF, 0x000A000C,
84 0x00FFFFFF, 0x000A000C,
85 0x00FFFFFF, 0x00140006 /* HDMI parameters 800mV 0dB*/
86};
87
e58623cb
AR
88static const u32 bdw_ddi_translations_dp[] = {
89 0x00FFFFFF, 0x0007000E, /* DP parameters */
90 0x00D75FFF, 0x000E000A,
91 0x00BEFFFF, 0x00140006,
92 0x00FFFFFF, 0x000E000A,
93 0x00D75FFF, 0x00180004,
94 0x80CB2FFF, 0x001B0002,
95 0x00F7DFFF, 0x00180004,
96 0x80D75FFF, 0x001B0002,
97 0x80FFFFFF, 0x001B0002,
98 0x00FFFFFF, 0x00140006 /* HDMI parameters 800mV 0dB*/
99};
100
101static const u32 bdw_ddi_translations_fdi[] = {
102 0x00FFFFFF, 0x0001000E, /* FDI parameters */
103 0x00D75FFF, 0x0004000A,
104 0x00C30FFF, 0x00070006,
105 0x00AAAFFF, 0x000C0000,
106 0x00FFFFFF, 0x0004000A,
107 0x00D75FFF, 0x00090004,
108 0x00C30FFF, 0x000C0000,
109 0x00FFFFFF, 0x00070006,
110 0x00D75FFF, 0x000C0000,
111 0x00FFFFFF, 0x00140006 /* HDMI parameters 800mV 0dB*/
112};
113
20f4dbe4 114enum port intel_ddi_get_encoder_port(struct intel_encoder *intel_encoder)
fc914639 115{
0bdee30e 116 struct drm_encoder *encoder = &intel_encoder->base;
fc914639
PZ
117 int type = intel_encoder->type;
118
174edf1f 119 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP ||
00c09d70 120 type == INTEL_OUTPUT_HDMI || type == INTEL_OUTPUT_UNKNOWN) {
174edf1f
PZ
121 struct intel_digital_port *intel_dig_port =
122 enc_to_dig_port(encoder);
123 return intel_dig_port->port;
0bdee30e 124
fc914639
PZ
125 } else if (type == INTEL_OUTPUT_ANALOG) {
126 return PORT_E;
0bdee30e 127
fc914639
PZ
128 } else {
129 DRM_ERROR("Invalid DDI encoder type %d\n", type);
130 BUG();
131 }
132}
133
e58623cb
AR
134/*
135 * Starting with Haswell, DDI port buffers must be programmed with correct
136 * values in advance. The buffer values are different for FDI and DP modes,
45244b87
ED
137 * but the HDMI/DVI fields are shared among those. So we program the DDI
138 * in either FDI or DP modes only, as HDMI connections will work with both
139 * of those
140 */
ad8d270c 141static void intel_prepare_ddi_buffers(struct drm_device *dev, enum port port)
45244b87
ED
142{
143 struct drm_i915_private *dev_priv = dev->dev_private;
144 u32 reg;
145 int i;
6acab15a 146 int hdmi_level = dev_priv->vbt.ddi_port_info[port].hdmi_level_shift;
e58623cb
AR
147 const u32 *ddi_translations_fdi;
148 const u32 *ddi_translations_dp;
300644c7 149 const u32 *ddi_translations_edp;
e58623cb
AR
150 const u32 *ddi_translations;
151
152 if (IS_BROADWELL(dev)) {
153 ddi_translations_fdi = bdw_ddi_translations_fdi;
154 ddi_translations_dp = bdw_ddi_translations_dp;
300644c7 155 ddi_translations_edp = bdw_ddi_translations_edp;
e58623cb
AR
156 } else if (IS_HASWELL(dev)) {
157 ddi_translations_fdi = hsw_ddi_translations_fdi;
158 ddi_translations_dp = hsw_ddi_translations_dp;
300644c7 159 ddi_translations_edp = hsw_ddi_translations_dp;
e58623cb
AR
160 } else {
161 WARN(1, "ddi translation table missing\n");
300644c7 162 ddi_translations_edp = bdw_ddi_translations_dp;
e58623cb
AR
163 ddi_translations_fdi = bdw_ddi_translations_fdi;
164 ddi_translations_dp = bdw_ddi_translations_dp;
165 }
166
300644c7
PZ
167 switch (port) {
168 case PORT_A:
169 ddi_translations = ddi_translations_edp;
170 break;
171 case PORT_B:
172 case PORT_C:
300644c7
PZ
173 ddi_translations = ddi_translations_dp;
174 break;
77d8d009 175 case PORT_D:
5d8a7752 176 if (intel_dp_is_edp(dev, PORT_D))
77d8d009
PZ
177 ddi_translations = ddi_translations_edp;
178 else
179 ddi_translations = ddi_translations_dp;
180 break;
300644c7
PZ
181 case PORT_E:
182 ddi_translations = ddi_translations_fdi;
183 break;
184 default:
185 BUG();
186 }
45244b87 187
f72d19f0
PZ
188 for (i = 0, reg = DDI_BUF_TRANS(port);
189 i < ARRAY_SIZE(hsw_ddi_translations_fdi); i++) {
45244b87
ED
190 I915_WRITE(reg, ddi_translations[i]);
191 reg += 4;
192 }
6acab15a
PZ
193 /* Entry 9 is for HDMI: */
194 for (i = 0; i < 2; i++) {
195 I915_WRITE(reg, hsw_ddi_translations_hdmi[hdmi_level * 2 + i]);
196 reg += 4;
197 }
45244b87
ED
198}
199
200/* Program DDI buffers translations for DP. By default, program ports A-D in DP
201 * mode and port E for FDI.
202 */
203void intel_prepare_ddi(struct drm_device *dev)
204{
205 int port;
206
0d536cb4
PZ
207 if (!HAS_DDI(dev))
208 return;
45244b87 209
ad8d270c
PZ
210 for (port = PORT_A; port <= PORT_E; port++)
211 intel_prepare_ddi_buffers(dev, port);
45244b87 212}
c82e4d26
ED
213
214static const long hsw_ddi_buf_ctl_values[] = {
215 DDI_BUF_EMP_400MV_0DB_HSW,
216 DDI_BUF_EMP_400MV_3_5DB_HSW,
217 DDI_BUF_EMP_400MV_6DB_HSW,
218 DDI_BUF_EMP_400MV_9_5DB_HSW,
219 DDI_BUF_EMP_600MV_0DB_HSW,
220 DDI_BUF_EMP_600MV_3_5DB_HSW,
221 DDI_BUF_EMP_600MV_6DB_HSW,
222 DDI_BUF_EMP_800MV_0DB_HSW,
223 DDI_BUF_EMP_800MV_3_5DB_HSW
224};
225
248138b5
PZ
226static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
227 enum port port)
228{
229 uint32_t reg = DDI_BUF_CTL(port);
230 int i;
231
232 for (i = 0; i < 8; i++) {
233 udelay(1);
234 if (I915_READ(reg) & DDI_BUF_IS_IDLE)
235 return;
236 }
237 DRM_ERROR("Timeout waiting for DDI BUF %c idle bit\n", port_name(port));
238}
c82e4d26
ED
239
240/* Starting with Haswell, different DDI ports can work in FDI mode for
241 * connection to the PCH-located connectors. For this, it is necessary to train
242 * both the DDI port and PCH receiver for the desired DDI buffer settings.
243 *
244 * The recommended port to work in FDI mode is DDI E, which we use here. Also,
245 * please note that when FDI mode is active on DDI E, it shares 2 lines with
246 * DDI A (which is used for eDP)
247 */
248
249void hsw_fdi_link_train(struct drm_crtc *crtc)
250{
251 struct drm_device *dev = crtc->dev;
252 struct drm_i915_private *dev_priv = dev->dev_private;
253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
04945641 254 u32 temp, i, rx_ctl_val;
c82e4d26 255
04945641
PZ
256 /* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
257 * mode set "sequence for CRT port" document:
258 * - TP1 to TP2 time with the default value
259 * - FDI delay to 90h
8693a824
DL
260 *
261 * WaFDIAutoLinkSetTimingOverrride:hsw
04945641
PZ
262 */
263 I915_WRITE(_FDI_RXA_MISC, FDI_RX_PWRDN_LANE1_VAL(2) |
264 FDI_RX_PWRDN_LANE0_VAL(2) |
265 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
266
267 /* Enable the PCH Receiver FDI PLL */
3e68320e 268 rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
33d29b14 269 FDI_RX_PLL_ENABLE |
627eb5a3 270 FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
04945641
PZ
271 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
272 POSTING_READ(_FDI_RXA_CTL);
273 udelay(220);
274
275 /* Switch from Rawclk to PCDclk */
276 rx_ctl_val |= FDI_PCDCLK;
277 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
278
279 /* Configure Port Clock Select */
280 I915_WRITE(PORT_CLK_SEL(PORT_E), intel_crtc->ddi_pll_sel);
281
282 /* Start the training iterating through available voltages and emphasis,
283 * testing each value twice. */
284 for (i = 0; i < ARRAY_SIZE(hsw_ddi_buf_ctl_values) * 2; i++) {
c82e4d26
ED
285 /* Configure DP_TP_CTL with auto-training */
286 I915_WRITE(DP_TP_CTL(PORT_E),
287 DP_TP_CTL_FDI_AUTOTRAIN |
288 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
289 DP_TP_CTL_LINK_TRAIN_PAT1 |
290 DP_TP_CTL_ENABLE);
291
876a8cdf
DL
292 /* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
293 * DDI E does not support port reversal, the functionality is
294 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
295 * port reversal bit */
c82e4d26 296 I915_WRITE(DDI_BUF_CTL(PORT_E),
04945641 297 DDI_BUF_CTL_ENABLE |
33d29b14 298 ((intel_crtc->config.fdi_lanes - 1) << 1) |
04945641
PZ
299 hsw_ddi_buf_ctl_values[i / 2]);
300 POSTING_READ(DDI_BUF_CTL(PORT_E));
c82e4d26
ED
301
302 udelay(600);
303
04945641
PZ
304 /* Program PCH FDI Receiver TU */
305 I915_WRITE(_FDI_RXA_TUSIZE1, TU_SIZE(64));
306
307 /* Enable PCH FDI Receiver with auto-training */
308 rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
309 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
310 POSTING_READ(_FDI_RXA_CTL);
311
312 /* Wait for FDI receiver lane calibration */
313 udelay(30);
314
315 /* Unset FDI_RX_MISC pwrdn lanes */
316 temp = I915_READ(_FDI_RXA_MISC);
317 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
318 I915_WRITE(_FDI_RXA_MISC, temp);
319 POSTING_READ(_FDI_RXA_MISC);
320
321 /* Wait for FDI auto training time */
322 udelay(5);
c82e4d26
ED
323
324 temp = I915_READ(DP_TP_STATUS(PORT_E));
325 if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
04945641 326 DRM_DEBUG_KMS("FDI link training done on step %d\n", i);
c82e4d26
ED
327
328 /* Enable normal pixel sending for FDI */
329 I915_WRITE(DP_TP_CTL(PORT_E),
04945641
PZ
330 DP_TP_CTL_FDI_AUTOTRAIN |
331 DP_TP_CTL_LINK_TRAIN_NORMAL |
332 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
333 DP_TP_CTL_ENABLE);
c82e4d26 334
04945641 335 return;
c82e4d26 336 }
04945641 337
248138b5
PZ
338 temp = I915_READ(DDI_BUF_CTL(PORT_E));
339 temp &= ~DDI_BUF_CTL_ENABLE;
340 I915_WRITE(DDI_BUF_CTL(PORT_E), temp);
341 POSTING_READ(DDI_BUF_CTL(PORT_E));
342
04945641 343 /* Disable DP_TP_CTL and FDI_RX_CTL and retry */
248138b5
PZ
344 temp = I915_READ(DP_TP_CTL(PORT_E));
345 temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
346 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
347 I915_WRITE(DP_TP_CTL(PORT_E), temp);
348 POSTING_READ(DP_TP_CTL(PORT_E));
349
350 intel_wait_ddi_buf_idle(dev_priv, PORT_E);
04945641
PZ
351
352 rx_ctl_val &= ~FDI_RX_ENABLE;
353 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
248138b5 354 POSTING_READ(_FDI_RXA_CTL);
04945641
PZ
355
356 /* Reset FDI_RX_MISC pwrdn lanes */
357 temp = I915_READ(_FDI_RXA_MISC);
358 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
359 temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
360 I915_WRITE(_FDI_RXA_MISC, temp);
248138b5 361 POSTING_READ(_FDI_RXA_MISC);
c82e4d26
ED
362 }
363
04945641 364 DRM_ERROR("FDI link training failed!\n");
c82e4d26 365}
0e72a5b5 366
c7d8be30 367static void intel_ddi_mode_set(struct intel_encoder *encoder)
72662e10 368{
c7d8be30
DV
369 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
370 int port = intel_ddi_get_encoder_port(encoder);
371 int pipe = crtc->pipe;
372 int type = encoder->type;
373 struct drm_display_mode *adjusted_mode = &crtc->config.adjusted_mode;
72662e10 374
bf98a726 375 DRM_DEBUG_KMS("Preparing DDI mode on port %c, pipe %c\n",
247d89f6 376 port_name(port), pipe_name(pipe));
72662e10 377
c7d8be30 378 crtc->eld_vld = false;
247d89f6 379 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
c7d8be30 380 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
876a8cdf 381 struct intel_digital_port *intel_dig_port =
c7d8be30 382 enc_to_dig_port(&encoder->base);
4f07854d 383
bcf53de4 384 intel_dp->DP = intel_dig_port->saved_port_bits |
876a8cdf 385 DDI_BUF_CTL_ENABLE | DDI_BUF_EMP_400MV_0DB_HSW;
17aa6be9 386 intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
247d89f6 387
8fed6193
TI
388 if (intel_dp->has_audio) {
389 DRM_DEBUG_DRIVER("DP audio on pipe %c on DDI\n",
c7d8be30 390 pipe_name(crtc->pipe));
8fed6193
TI
391
392 /* write eld */
393 DRM_DEBUG_DRIVER("DP audio: write eld information\n");
c7d8be30 394 intel_write_eld(&encoder->base, adjusted_mode);
8fed6193 395 }
247d89f6 396 } else if (type == INTEL_OUTPUT_HDMI) {
c7d8be30 397 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
247d89f6
PZ
398
399 if (intel_hdmi->has_audio) {
400 /* Proper support for digital audio needs a new logic
401 * and a new set of registers, so we leave it for future
402 * patch bombing.
403 */
404 DRM_DEBUG_DRIVER("HDMI audio on pipe %c on DDI\n",
c7d8be30 405 pipe_name(crtc->pipe));
247d89f6
PZ
406
407 /* write eld */
408 DRM_DEBUG_DRIVER("HDMI audio: write eld information\n");
c7d8be30 409 intel_write_eld(&encoder->base, adjusted_mode);
247d89f6 410 }
72662e10 411
c7d8be30 412 intel_hdmi->set_infoframes(&encoder->base, adjusted_mode);
247d89f6 413 }
8d9ddbcb
PZ
414}
415
416static struct intel_encoder *
417intel_ddi_get_crtc_encoder(struct drm_crtc *crtc)
418{
419 struct drm_device *dev = crtc->dev;
420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
421 struct intel_encoder *intel_encoder, *ret = NULL;
422 int num_encoders = 0;
423
424 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
425 ret = intel_encoder;
426 num_encoders++;
427 }
428
429 if (num_encoders != 1)
84f44ce7
VS
430 WARN(1, "%d encoders on crtc for pipe %c\n", num_encoders,
431 pipe_name(intel_crtc->pipe));
8d9ddbcb
PZ
432
433 BUG_ON(ret == NULL);
434 return ret;
435}
436
6441ab5f
PZ
437void intel_ddi_put_crtc_pll(struct drm_crtc *crtc)
438{
439 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
440 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
442 uint32_t val;
443
444 switch (intel_crtc->ddi_pll_sel) {
445 case PORT_CLK_SEL_SPLL:
446 plls->spll_refcount--;
447 if (plls->spll_refcount == 0) {
448 DRM_DEBUG_KMS("Disabling SPLL\n");
449 val = I915_READ(SPLL_CTL);
450 WARN_ON(!(val & SPLL_PLL_ENABLE));
451 I915_WRITE(SPLL_CTL, val & ~SPLL_PLL_ENABLE);
452 POSTING_READ(SPLL_CTL);
453 }
454 break;
455 case PORT_CLK_SEL_WRPLL1:
456 plls->wrpll1_refcount--;
457 if (plls->wrpll1_refcount == 0) {
458 DRM_DEBUG_KMS("Disabling WRPLL 1\n");
459 val = I915_READ(WRPLL_CTL1);
460 WARN_ON(!(val & WRPLL_PLL_ENABLE));
461 I915_WRITE(WRPLL_CTL1, val & ~WRPLL_PLL_ENABLE);
462 POSTING_READ(WRPLL_CTL1);
463 }
464 break;
465 case PORT_CLK_SEL_WRPLL2:
466 plls->wrpll2_refcount--;
467 if (plls->wrpll2_refcount == 0) {
468 DRM_DEBUG_KMS("Disabling WRPLL 2\n");
469 val = I915_READ(WRPLL_CTL2);
470 WARN_ON(!(val & WRPLL_PLL_ENABLE));
471 I915_WRITE(WRPLL_CTL2, val & ~WRPLL_PLL_ENABLE);
472 POSTING_READ(WRPLL_CTL2);
473 }
474 break;
475 }
476
477 WARN(plls->spll_refcount < 0, "Invalid SPLL refcount\n");
478 WARN(plls->wrpll1_refcount < 0, "Invalid WRPLL1 refcount\n");
479 WARN(plls->wrpll2_refcount < 0, "Invalid WRPLL2 refcount\n");
480
481 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_NONE;
482}
483
1c0b85c5
DL
484#define LC_FREQ 2700
485#define LC_FREQ_2K (LC_FREQ * 2000)
486
487#define P_MIN 2
488#define P_MAX 64
489#define P_INC 2
490
491/* Constraints for PLL good behavior */
492#define REF_MIN 48
493#define REF_MAX 400
494#define VCO_MIN 2400
495#define VCO_MAX 4800
496
497#define ABS_DIFF(a, b) ((a > b) ? (a - b) : (b - a))
498
499struct wrpll_rnp {
500 unsigned p, n2, r2;
501};
502
503static unsigned wrpll_get_budget_for_freq(int clock)
6441ab5f 504{
1c0b85c5
DL
505 unsigned budget;
506
507 switch (clock) {
508 case 25175000:
509 case 25200000:
510 case 27000000:
511 case 27027000:
512 case 37762500:
513 case 37800000:
514 case 40500000:
515 case 40541000:
516 case 54000000:
517 case 54054000:
518 case 59341000:
519 case 59400000:
520 case 72000000:
521 case 74176000:
522 case 74250000:
523 case 81000000:
524 case 81081000:
525 case 89012000:
526 case 89100000:
527 case 108000000:
528 case 108108000:
529 case 111264000:
530 case 111375000:
531 case 148352000:
532 case 148500000:
533 case 162000000:
534 case 162162000:
535 case 222525000:
536 case 222750000:
537 case 296703000:
538 case 297000000:
539 budget = 0;
540 break;
541 case 233500000:
542 case 245250000:
543 case 247750000:
544 case 253250000:
545 case 298000000:
546 budget = 1500;
547 break;
548 case 169128000:
549 case 169500000:
550 case 179500000:
551 case 202000000:
552 budget = 2000;
553 break;
554 case 256250000:
555 case 262500000:
556 case 270000000:
557 case 272500000:
558 case 273750000:
559 case 280750000:
560 case 281250000:
561 case 286000000:
562 case 291750000:
563 budget = 4000;
564 break;
565 case 267250000:
566 case 268500000:
567 budget = 5000;
568 break;
569 default:
570 budget = 1000;
571 break;
572 }
6441ab5f 573
1c0b85c5
DL
574 return budget;
575}
576
577static void wrpll_update_rnp(uint64_t freq2k, unsigned budget,
578 unsigned r2, unsigned n2, unsigned p,
579 struct wrpll_rnp *best)
580{
581 uint64_t a, b, c, d, diff, diff_best;
6441ab5f 582
1c0b85c5
DL
583 /* No best (r,n,p) yet */
584 if (best->p == 0) {
585 best->p = p;
586 best->n2 = n2;
587 best->r2 = r2;
588 return;
589 }
6441ab5f 590
1c0b85c5
DL
591 /*
592 * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
593 * freq2k.
594 *
595 * delta = 1e6 *
596 * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
597 * freq2k;
598 *
599 * and we would like delta <= budget.
600 *
601 * If the discrepancy is above the PPM-based budget, always prefer to
602 * improve upon the previous solution. However, if you're within the
603 * budget, try to maximize Ref * VCO, that is N / (P * R^2).
604 */
605 a = freq2k * budget * p * r2;
606 b = freq2k * budget * best->p * best->r2;
607 diff = ABS_DIFF((freq2k * p * r2), (LC_FREQ_2K * n2));
608 diff_best = ABS_DIFF((freq2k * best->p * best->r2),
609 (LC_FREQ_2K * best->n2));
610 c = 1000000 * diff;
611 d = 1000000 * diff_best;
612
613 if (a < c && b < d) {
614 /* If both are above the budget, pick the closer */
615 if (best->p * best->r2 * diff < p * r2 * diff_best) {
616 best->p = p;
617 best->n2 = n2;
618 best->r2 = r2;
619 }
620 } else if (a >= c && b < d) {
621 /* If A is below the threshold but B is above it? Update. */
622 best->p = p;
623 best->n2 = n2;
624 best->r2 = r2;
625 } else if (a >= c && b >= d) {
626 /* Both are below the limit, so pick the higher n2/(r2*r2) */
627 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
628 best->p = p;
629 best->n2 = n2;
630 best->r2 = r2;
631 }
632 }
633 /* Otherwise a < c && b >= d, do nothing */
634}
635
11578553
JB
636static int intel_ddi_calc_wrpll_link(struct drm_i915_private *dev_priv,
637 int reg)
638{
639 int refclk = LC_FREQ;
640 int n, p, r;
641 u32 wrpll;
642
643 wrpll = I915_READ(reg);
644 switch (wrpll & SPLL_PLL_REF_MASK) {
645 case SPLL_PLL_SSC:
646 case SPLL_PLL_NON_SSC:
647 /*
648 * We could calculate spread here, but our checking
649 * code only cares about 5% accuracy, and spread is a max of
650 * 0.5% downspread.
651 */
652 refclk = 135;
653 break;
654 case SPLL_PLL_LCPLL:
655 refclk = LC_FREQ;
656 break;
657 default:
658 WARN(1, "bad wrpll refclk\n");
659 return 0;
660 }
661
662 r = wrpll & WRPLL_DIVIDER_REF_MASK;
663 p = (wrpll & WRPLL_DIVIDER_POST_MASK) >> WRPLL_DIVIDER_POST_SHIFT;
664 n = (wrpll & WRPLL_DIVIDER_FB_MASK) >> WRPLL_DIVIDER_FB_SHIFT;
665
666 return (LC_FREQ * n) / (p * r);
667}
668
669static void intel_ddi_clock_get(struct intel_encoder *encoder,
670 struct intel_crtc_config *pipe_config)
671{
672 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
673 enum port port = intel_ddi_get_encoder_port(encoder);
674 int link_clock = 0;
675 u32 val, pll;
676
677 val = I915_READ(PORT_CLK_SEL(port));
678 switch (val & PORT_CLK_SEL_MASK) {
679 case PORT_CLK_SEL_LCPLL_810:
680 link_clock = 81000;
681 break;
682 case PORT_CLK_SEL_LCPLL_1350:
683 link_clock = 135000;
684 break;
685 case PORT_CLK_SEL_LCPLL_2700:
686 link_clock = 270000;
687 break;
688 case PORT_CLK_SEL_WRPLL1:
689 link_clock = intel_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL1);
690 break;
691 case PORT_CLK_SEL_WRPLL2:
692 link_clock = intel_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL2);
693 break;
694 case PORT_CLK_SEL_SPLL:
695 pll = I915_READ(SPLL_CTL) & SPLL_PLL_FREQ_MASK;
696 if (pll == SPLL_PLL_FREQ_810MHz)
697 link_clock = 81000;
698 else if (pll == SPLL_PLL_FREQ_1350MHz)
699 link_clock = 135000;
700 else if (pll == SPLL_PLL_FREQ_2700MHz)
701 link_clock = 270000;
702 else {
703 WARN(1, "bad spll freq\n");
704 return;
705 }
706 break;
707 default:
708 WARN(1, "bad port clock sel\n");
709 return;
710 }
711
712 pipe_config->port_clock = link_clock * 2;
713
714 if (pipe_config->has_pch_encoder)
715 pipe_config->adjusted_mode.crtc_clock =
716 intel_dotclock_calculate(pipe_config->port_clock,
717 &pipe_config->fdi_m_n);
718 else if (pipe_config->has_dp_encoder)
719 pipe_config->adjusted_mode.crtc_clock =
720 intel_dotclock_calculate(pipe_config->port_clock,
721 &pipe_config->dp_m_n);
722 else
723 pipe_config->adjusted_mode.crtc_clock = pipe_config->port_clock;
724}
725
1c0b85c5
DL
726static void
727intel_ddi_calculate_wrpll(int clock /* in Hz */,
728 unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
729{
730 uint64_t freq2k;
731 unsigned p, n2, r2;
732 struct wrpll_rnp best = { 0, 0, 0 };
733 unsigned budget;
734
735 freq2k = clock / 100;
736
737 budget = wrpll_get_budget_for_freq(clock);
738
739 /* Special case handling for 540 pixel clock: bypass WR PLL entirely
740 * and directly pass the LC PLL to it. */
741 if (freq2k == 5400000) {
742 *n2_out = 2;
743 *p_out = 1;
744 *r2_out = 2;
745 return;
746 }
747
748 /*
749 * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
750 * the WR PLL.
751 *
752 * We want R so that REF_MIN <= Ref <= REF_MAX.
753 * Injecting R2 = 2 * R gives:
754 * REF_MAX * r2 > LC_FREQ * 2 and
755 * REF_MIN * r2 < LC_FREQ * 2
756 *
757 * Which means the desired boundaries for r2 are:
758 * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
759 *
760 */
761 for (r2 = LC_FREQ * 2 / REF_MAX + 1;
762 r2 <= LC_FREQ * 2 / REF_MIN;
763 r2++) {
764
765 /*
766 * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
767 *
768 * Once again we want VCO_MIN <= VCO <= VCO_MAX.
769 * Injecting R2 = 2 * R and N2 = 2 * N, we get:
770 * VCO_MAX * r2 > n2 * LC_FREQ and
771 * VCO_MIN * r2 < n2 * LC_FREQ)
772 *
773 * Which means the desired boundaries for n2 are:
774 * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
775 */
776 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
777 n2 <= VCO_MAX * r2 / LC_FREQ;
778 n2++) {
779
780 for (p = P_MIN; p <= P_MAX; p += P_INC)
781 wrpll_update_rnp(freq2k, budget,
782 r2, n2, p, &best);
783 }
784 }
6441ab5f 785
1c0b85c5
DL
786 *n2_out = best.n2;
787 *p_out = best.p;
788 *r2_out = best.r2;
6441ab5f
PZ
789}
790
566b734a
PZ
791/*
792 * Tries to find a PLL for the CRTC. If it finds, it increases the refcount and
793 * stores it in intel_crtc->ddi_pll_sel, so other mode sets won't be able to
794 * steal the selected PLL. You need to call intel_ddi_pll_enable to actually
795 * enable the PLL.
796 */
797bool intel_ddi_pll_select(struct intel_crtc *intel_crtc)
6441ab5f 798{
566b734a 799 struct drm_crtc *crtc = &intel_crtc->base;
6441ab5f 800 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
068759bd 801 struct drm_encoder *encoder = &intel_encoder->base;
6441ab5f
PZ
802 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
803 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
804 int type = intel_encoder->type;
805 enum pipe pipe = intel_crtc->pipe;
ff9a6750 806 int clock = intel_crtc->config.port_clock;
6441ab5f 807
6441ab5f
PZ
808 intel_ddi_put_crtc_pll(crtc);
809
068759bd
PZ
810 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
811 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
812
813 switch (intel_dp->link_bw) {
814 case DP_LINK_BW_1_62:
815 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_LCPLL_810;
816 break;
817 case DP_LINK_BW_2_7:
818 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_LCPLL_1350;
819 break;
820 case DP_LINK_BW_5_4:
821 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_LCPLL_2700;
822 break;
823 default:
824 DRM_ERROR("Link bandwidth %d unsupported\n",
825 intel_dp->link_bw);
826 return false;
827 }
828
068759bd 829 } else if (type == INTEL_OUTPUT_HDMI) {
566b734a 830 uint32_t reg, val;
1c0b85c5 831 unsigned p, n2, r2;
6441ab5f 832
0694001b
PZ
833 intel_ddi_calculate_wrpll(clock * 1000, &r2, &n2, &p);
834
835 val = WRPLL_PLL_ENABLE | WRPLL_PLL_SELECT_LCPLL_2700 |
836 WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
837 WRPLL_DIVIDER_POST(p);
838
839 if (val == I915_READ(WRPLL_CTL1)) {
840 DRM_DEBUG_KMS("Reusing WRPLL 1 on pipe %c\n",
841 pipe_name(pipe));
842 reg = WRPLL_CTL1;
843 } else if (val == I915_READ(WRPLL_CTL2)) {
844 DRM_DEBUG_KMS("Reusing WRPLL 2 on pipe %c\n",
845 pipe_name(pipe));
846 reg = WRPLL_CTL2;
847 } else if (plls->wrpll1_refcount == 0) {
6441ab5f
PZ
848 DRM_DEBUG_KMS("Using WRPLL 1 on pipe %c\n",
849 pipe_name(pipe));
6441ab5f 850 reg = WRPLL_CTL1;
6441ab5f
PZ
851 } else if (plls->wrpll2_refcount == 0) {
852 DRM_DEBUG_KMS("Using WRPLL 2 on pipe %c\n",
853 pipe_name(pipe));
6441ab5f 854 reg = WRPLL_CTL2;
6441ab5f
PZ
855 } else {
856 DRM_ERROR("No WRPLLs available!\n");
857 return false;
858 }
859
566b734a
PZ
860 DRM_DEBUG_KMS("WRPLL: %dKHz refresh rate with p=%d, n2=%d r2=%d\n",
861 clock, p, n2, r2);
862
0694001b
PZ
863 if (reg == WRPLL_CTL1) {
864 plls->wrpll1_refcount++;
865 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_WRPLL1;
866 } else {
867 plls->wrpll2_refcount++;
868 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_WRPLL2;
869 }
6441ab5f
PZ
870
871 } else if (type == INTEL_OUTPUT_ANALOG) {
872 if (plls->spll_refcount == 0) {
873 DRM_DEBUG_KMS("Using SPLL on pipe %c\n",
874 pipe_name(pipe));
875 plls->spll_refcount++;
6441ab5f 876 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_SPLL;
00037c2e
DL
877 } else {
878 DRM_ERROR("SPLL already in use\n");
879 return false;
6441ab5f
PZ
880 }
881
6441ab5f
PZ
882 } else {
883 WARN(1, "Invalid DDI encoder type %d\n", type);
884 return false;
885 }
886
6441ab5f
PZ
887 return true;
888}
889
566b734a
PZ
890/*
891 * To be called after intel_ddi_pll_select(). That one selects the PLL to be
892 * used, this one actually enables the PLL.
893 */
894void intel_ddi_pll_enable(struct intel_crtc *crtc)
895{
896 struct drm_device *dev = crtc->base.dev;
897 struct drm_i915_private *dev_priv = dev->dev_private;
898 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
899 int clock = crtc->config.port_clock;
900 uint32_t reg, cur_val, new_val;
901 int refcount;
902 const char *pll_name;
903 uint32_t enable_bit = (1 << 31);
904 unsigned int p, n2, r2;
905
906 BUILD_BUG_ON(enable_bit != SPLL_PLL_ENABLE);
907 BUILD_BUG_ON(enable_bit != WRPLL_PLL_ENABLE);
908
909 switch (crtc->ddi_pll_sel) {
910 case PORT_CLK_SEL_LCPLL_2700:
911 case PORT_CLK_SEL_LCPLL_1350:
912 case PORT_CLK_SEL_LCPLL_810:
913 /*
914 * LCPLL should always be enabled at this point of the mode set
915 * sequence, so nothing to do.
916 */
917 return;
918
919 case PORT_CLK_SEL_SPLL:
920 pll_name = "SPLL";
921 reg = SPLL_CTL;
922 refcount = plls->spll_refcount;
923 new_val = SPLL_PLL_ENABLE | SPLL_PLL_FREQ_1350MHz |
924 SPLL_PLL_SSC;
925 break;
926
927 case PORT_CLK_SEL_WRPLL1:
928 case PORT_CLK_SEL_WRPLL2:
929 if (crtc->ddi_pll_sel == PORT_CLK_SEL_WRPLL1) {
930 pll_name = "WRPLL1";
931 reg = WRPLL_CTL1;
932 refcount = plls->wrpll1_refcount;
933 } else {
934 pll_name = "WRPLL2";
935 reg = WRPLL_CTL2;
936 refcount = plls->wrpll2_refcount;
937 }
938
939 intel_ddi_calculate_wrpll(clock * 1000, &r2, &n2, &p);
940
941 new_val = WRPLL_PLL_ENABLE | WRPLL_PLL_SELECT_LCPLL_2700 |
942 WRPLL_DIVIDER_REFERENCE(r2) |
943 WRPLL_DIVIDER_FEEDBACK(n2) | WRPLL_DIVIDER_POST(p);
944
945 break;
946
947 case PORT_CLK_SEL_NONE:
948 WARN(1, "Bad selected pll: PORT_CLK_SEL_NONE\n");
949 return;
950 default:
951 WARN(1, "Bad selected pll: 0x%08x\n", crtc->ddi_pll_sel);
952 return;
953 }
954
955 cur_val = I915_READ(reg);
956
957 WARN(refcount < 1, "Bad %s refcount: %d\n", pll_name, refcount);
958 if (refcount == 1) {
959 WARN(cur_val & enable_bit, "%s already enabled\n", pll_name);
960 I915_WRITE(reg, new_val);
961 POSTING_READ(reg);
962 udelay(20);
963 } else {
964 WARN((cur_val & enable_bit) == 0, "%s disabled\n", pll_name);
965 }
966}
967
dae84799
PZ
968void intel_ddi_set_pipe_settings(struct drm_crtc *crtc)
969{
970 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
972 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
3b117c8f 973 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
dae84799
PZ
974 int type = intel_encoder->type;
975 uint32_t temp;
976
977 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
978
c9809791 979 temp = TRANS_MSA_SYNC_CLK;
965e0c48 980 switch (intel_crtc->config.pipe_bpp) {
dae84799 981 case 18:
c9809791 982 temp |= TRANS_MSA_6_BPC;
dae84799
PZ
983 break;
984 case 24:
c9809791 985 temp |= TRANS_MSA_8_BPC;
dae84799
PZ
986 break;
987 case 30:
c9809791 988 temp |= TRANS_MSA_10_BPC;
dae84799
PZ
989 break;
990 case 36:
c9809791 991 temp |= TRANS_MSA_12_BPC;
dae84799
PZ
992 break;
993 default:
4e53c2e0 994 BUG();
dae84799 995 }
c9809791 996 I915_WRITE(TRANS_MSA_MISC(cpu_transcoder), temp);
dae84799
PZ
997 }
998}
999
8228c251 1000void intel_ddi_enable_transcoder_func(struct drm_crtc *crtc)
8d9ddbcb
PZ
1001{
1002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1003 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
7739c33b 1004 struct drm_encoder *encoder = &intel_encoder->base;
c7670b10
PZ
1005 struct drm_device *dev = crtc->dev;
1006 struct drm_i915_private *dev_priv = dev->dev_private;
8d9ddbcb 1007 enum pipe pipe = intel_crtc->pipe;
3b117c8f 1008 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
174edf1f 1009 enum port port = intel_ddi_get_encoder_port(intel_encoder);
7739c33b 1010 int type = intel_encoder->type;
8d9ddbcb
PZ
1011 uint32_t temp;
1012
ad80a810
PZ
1013 /* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1014 temp = TRANS_DDI_FUNC_ENABLE;
174edf1f 1015 temp |= TRANS_DDI_SELECT_PORT(port);
dfcef252 1016
965e0c48 1017 switch (intel_crtc->config.pipe_bpp) {
dfcef252 1018 case 18:
ad80a810 1019 temp |= TRANS_DDI_BPC_6;
dfcef252
PZ
1020 break;
1021 case 24:
ad80a810 1022 temp |= TRANS_DDI_BPC_8;
dfcef252
PZ
1023 break;
1024 case 30:
ad80a810 1025 temp |= TRANS_DDI_BPC_10;
dfcef252
PZ
1026 break;
1027 case 36:
ad80a810 1028 temp |= TRANS_DDI_BPC_12;
dfcef252
PZ
1029 break;
1030 default:
4e53c2e0 1031 BUG();
dfcef252 1032 }
72662e10 1033
a666283e 1034 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
ad80a810 1035 temp |= TRANS_DDI_PVSYNC;
a666283e 1036 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
ad80a810 1037 temp |= TRANS_DDI_PHSYNC;
f63eb7c4 1038
e6f0bfc4
PZ
1039 if (cpu_transcoder == TRANSCODER_EDP) {
1040 switch (pipe) {
1041 case PIPE_A:
c7670b10
PZ
1042 /* On Haswell, can only use the always-on power well for
1043 * eDP when not using the panel fitter, and when not
1044 * using motion blur mitigation (which we don't
1045 * support). */
1046 if (IS_HASWELL(dev) && intel_crtc->config.pch_pfit.enabled)
d6dd9eb1
DV
1047 temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1048 else
1049 temp |= TRANS_DDI_EDP_INPUT_A_ON;
e6f0bfc4
PZ
1050 break;
1051 case PIPE_B:
1052 temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1053 break;
1054 case PIPE_C:
1055 temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1056 break;
1057 default:
1058 BUG();
1059 break;
1060 }
1061 }
1062
7739c33b
PZ
1063 if (type == INTEL_OUTPUT_HDMI) {
1064 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
8d9ddbcb
PZ
1065
1066 if (intel_hdmi->has_hdmi_sink)
ad80a810 1067 temp |= TRANS_DDI_MODE_SELECT_HDMI;
8d9ddbcb 1068 else
ad80a810 1069 temp |= TRANS_DDI_MODE_SELECT_DVI;
8d9ddbcb 1070
7739c33b 1071 } else if (type == INTEL_OUTPUT_ANALOG) {
ad80a810 1072 temp |= TRANS_DDI_MODE_SELECT_FDI;
33d29b14 1073 temp |= (intel_crtc->config.fdi_lanes - 1) << 1;
7739c33b
PZ
1074
1075 } else if (type == INTEL_OUTPUT_DISPLAYPORT ||
1076 type == INTEL_OUTPUT_EDP) {
1077 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1078
ad80a810 1079 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
7739c33b 1080
17aa6be9 1081 temp |= DDI_PORT_WIDTH(intel_dp->lane_count);
8d9ddbcb 1082 } else {
84f44ce7
VS
1083 WARN(1, "Invalid encoder type %d for pipe %c\n",
1084 intel_encoder->type, pipe_name(pipe));
8d9ddbcb
PZ
1085 }
1086
ad80a810 1087 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
8d9ddbcb 1088}
72662e10 1089
ad80a810
PZ
1090void intel_ddi_disable_transcoder_func(struct drm_i915_private *dev_priv,
1091 enum transcoder cpu_transcoder)
8d9ddbcb 1092{
ad80a810 1093 uint32_t reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
8d9ddbcb
PZ
1094 uint32_t val = I915_READ(reg);
1095
ad80a810
PZ
1096 val &= ~(TRANS_DDI_FUNC_ENABLE | TRANS_DDI_PORT_MASK);
1097 val |= TRANS_DDI_PORT_NONE;
8d9ddbcb 1098 I915_WRITE(reg, val);
72662e10
ED
1099}
1100
bcbc889b
PZ
1101bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1102{
1103 struct drm_device *dev = intel_connector->base.dev;
1104 struct drm_i915_private *dev_priv = dev->dev_private;
1105 struct intel_encoder *intel_encoder = intel_connector->encoder;
1106 int type = intel_connector->base.connector_type;
1107 enum port port = intel_ddi_get_encoder_port(intel_encoder);
1108 enum pipe pipe = 0;
1109 enum transcoder cpu_transcoder;
1110 uint32_t tmp;
1111
1112 if (!intel_encoder->get_hw_state(intel_encoder, &pipe))
1113 return false;
1114
1115 if (port == PORT_A)
1116 cpu_transcoder = TRANSCODER_EDP;
1117 else
1a240d4d 1118 cpu_transcoder = (enum transcoder) pipe;
bcbc889b
PZ
1119
1120 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1121
1122 switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
1123 case TRANS_DDI_MODE_SELECT_HDMI:
1124 case TRANS_DDI_MODE_SELECT_DVI:
1125 return (type == DRM_MODE_CONNECTOR_HDMIA);
1126
1127 case TRANS_DDI_MODE_SELECT_DP_SST:
1128 if (type == DRM_MODE_CONNECTOR_eDP)
1129 return true;
1130 case TRANS_DDI_MODE_SELECT_DP_MST:
1131 return (type == DRM_MODE_CONNECTOR_DisplayPort);
1132
1133 case TRANS_DDI_MODE_SELECT_FDI:
1134 return (type == DRM_MODE_CONNECTOR_VGA);
1135
1136 default:
1137 return false;
1138 }
1139}
1140
85234cdc
DV
1141bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
1142 enum pipe *pipe)
1143{
1144 struct drm_device *dev = encoder->base.dev;
1145 struct drm_i915_private *dev_priv = dev->dev_private;
fe43d3f5 1146 enum port port = intel_ddi_get_encoder_port(encoder);
85234cdc
DV
1147 u32 tmp;
1148 int i;
1149
fe43d3f5 1150 tmp = I915_READ(DDI_BUF_CTL(port));
85234cdc
DV
1151
1152 if (!(tmp & DDI_BUF_CTL_ENABLE))
1153 return false;
1154
ad80a810
PZ
1155 if (port == PORT_A) {
1156 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
85234cdc 1157
ad80a810
PZ
1158 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1159 case TRANS_DDI_EDP_INPUT_A_ON:
1160 case TRANS_DDI_EDP_INPUT_A_ONOFF:
1161 *pipe = PIPE_A;
1162 break;
1163 case TRANS_DDI_EDP_INPUT_B_ONOFF:
1164 *pipe = PIPE_B;
1165 break;
1166 case TRANS_DDI_EDP_INPUT_C_ONOFF:
1167 *pipe = PIPE_C;
1168 break;
1169 }
1170
1171 return true;
1172 } else {
1173 for (i = TRANSCODER_A; i <= TRANSCODER_C; i++) {
1174 tmp = I915_READ(TRANS_DDI_FUNC_CTL(i));
1175
1176 if ((tmp & TRANS_DDI_PORT_MASK)
1177 == TRANS_DDI_SELECT_PORT(port)) {
1178 *pipe = i;
1179 return true;
1180 }
85234cdc
DV
1181 }
1182 }
1183
84f44ce7 1184 DRM_DEBUG_KMS("No pipe for ddi port %c found\n", port_name(port));
85234cdc 1185
22f9fe50 1186 return false;
85234cdc
DV
1187}
1188
6441ab5f
PZ
1189static uint32_t intel_ddi_get_crtc_pll(struct drm_i915_private *dev_priv,
1190 enum pipe pipe)
1191{
1192 uint32_t temp, ret;
a42f704b 1193 enum port port = I915_MAX_PORTS;
ad80a810
PZ
1194 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1195 pipe);
6441ab5f
PZ
1196 int i;
1197
ad80a810
PZ
1198 if (cpu_transcoder == TRANSCODER_EDP) {
1199 port = PORT_A;
1200 } else {
1201 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1202 temp &= TRANS_DDI_PORT_MASK;
1203
1204 for (i = PORT_B; i <= PORT_E; i++)
1205 if (temp == TRANS_DDI_SELECT_PORT(i))
1206 port = i;
1207 }
6441ab5f 1208
a42f704b
DL
1209 if (port == I915_MAX_PORTS) {
1210 WARN(1, "Pipe %c enabled on an unknown port\n",
1211 pipe_name(pipe));
1212 ret = PORT_CLK_SEL_NONE;
1213 } else {
1214 ret = I915_READ(PORT_CLK_SEL(port));
1215 DRM_DEBUG_KMS("Pipe %c connected to port %c using clock "
1216 "0x%08x\n", pipe_name(pipe), port_name(port),
1217 ret);
1218 }
6441ab5f
PZ
1219
1220 return ret;
1221}
1222
1223void intel_ddi_setup_hw_pll_state(struct drm_device *dev)
1224{
1225 struct drm_i915_private *dev_priv = dev->dev_private;
1226 enum pipe pipe;
1227 struct intel_crtc *intel_crtc;
1228
0882dae9
PZ
1229 dev_priv->ddi_plls.spll_refcount = 0;
1230 dev_priv->ddi_plls.wrpll1_refcount = 0;
1231 dev_priv->ddi_plls.wrpll2_refcount = 0;
1232
6441ab5f
PZ
1233 for_each_pipe(pipe) {
1234 intel_crtc =
1235 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1236
0882dae9
PZ
1237 if (!intel_crtc->active) {
1238 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_NONE;
6441ab5f 1239 continue;
0882dae9 1240 }
6441ab5f
PZ
1241
1242 intel_crtc->ddi_pll_sel = intel_ddi_get_crtc_pll(dev_priv,
1243 pipe);
1244
1245 switch (intel_crtc->ddi_pll_sel) {
1246 case PORT_CLK_SEL_SPLL:
1247 dev_priv->ddi_plls.spll_refcount++;
1248 break;
1249 case PORT_CLK_SEL_WRPLL1:
1250 dev_priv->ddi_plls.wrpll1_refcount++;
1251 break;
1252 case PORT_CLK_SEL_WRPLL2:
1253 dev_priv->ddi_plls.wrpll2_refcount++;
1254 break;
1255 }
1256 }
1257}
1258
fc914639
PZ
1259void intel_ddi_enable_pipe_clock(struct intel_crtc *intel_crtc)
1260{
1261 struct drm_crtc *crtc = &intel_crtc->base;
1262 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1263 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
1264 enum port port = intel_ddi_get_encoder_port(intel_encoder);
3b117c8f 1265 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
fc914639 1266
bb523fc0
PZ
1267 if (cpu_transcoder != TRANSCODER_EDP)
1268 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1269 TRANS_CLK_SEL_PORT(port));
fc914639
PZ
1270}
1271
1272void intel_ddi_disable_pipe_clock(struct intel_crtc *intel_crtc)
1273{
1274 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3b117c8f 1275 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
fc914639 1276
bb523fc0
PZ
1277 if (cpu_transcoder != TRANSCODER_EDP)
1278 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1279 TRANS_CLK_SEL_DISABLED);
fc914639
PZ
1280}
1281
00c09d70 1282static void intel_ddi_pre_enable(struct intel_encoder *intel_encoder)
6441ab5f 1283{
c19b0669
PZ
1284 struct drm_encoder *encoder = &intel_encoder->base;
1285 struct drm_crtc *crtc = encoder->crtc;
1286 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
6441ab5f
PZ
1287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1288 enum port port = intel_ddi_get_encoder_port(intel_encoder);
82a4d9c0 1289 int type = intel_encoder->type;
6441ab5f 1290
82a4d9c0
PZ
1291 if (type == INTEL_OUTPUT_EDP) {
1292 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4be73780 1293 intel_edp_panel_on(intel_dp);
82a4d9c0 1294 }
6441ab5f 1295
82a4d9c0 1296 WARN_ON(intel_crtc->ddi_pll_sel == PORT_CLK_SEL_NONE);
6441ab5f 1297 I915_WRITE(PORT_CLK_SEL(port), intel_crtc->ddi_pll_sel);
c19b0669 1298
82a4d9c0 1299 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
c19b0669
PZ
1300 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1301
1302 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1303 intel_dp_start_link_train(intel_dp);
1304 intel_dp_complete_link_train(intel_dp);
3ab9c637
ID
1305 if (port != PORT_A)
1306 intel_dp_stop_link_train(intel_dp);
c19b0669 1307 }
6441ab5f
PZ
1308}
1309
00c09d70 1310static void intel_ddi_post_disable(struct intel_encoder *intel_encoder)
6441ab5f
PZ
1311{
1312 struct drm_encoder *encoder = &intel_encoder->base;
1313 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
1314 enum port port = intel_ddi_get_encoder_port(intel_encoder);
82a4d9c0 1315 int type = intel_encoder->type;
2886e93f 1316 uint32_t val;
a836bdf9 1317 bool wait = false;
2886e93f
PZ
1318
1319 val = I915_READ(DDI_BUF_CTL(port));
1320 if (val & DDI_BUF_CTL_ENABLE) {
1321 val &= ~DDI_BUF_CTL_ENABLE;
1322 I915_WRITE(DDI_BUF_CTL(port), val);
a836bdf9 1323 wait = true;
2886e93f 1324 }
6441ab5f 1325
a836bdf9
PZ
1326 val = I915_READ(DP_TP_CTL(port));
1327 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1328 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
1329 I915_WRITE(DP_TP_CTL(port), val);
1330
1331 if (wait)
1332 intel_wait_ddi_buf_idle(dev_priv, port);
1333
76bb80ed 1334 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
82a4d9c0 1335 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
76bb80ed 1336 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
4be73780 1337 intel_edp_panel_off(intel_dp);
82a4d9c0
PZ
1338 }
1339
6441ab5f
PZ
1340 I915_WRITE(PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
1341}
1342
00c09d70 1343static void intel_enable_ddi(struct intel_encoder *intel_encoder)
72662e10 1344{
6547fef8 1345 struct drm_encoder *encoder = &intel_encoder->base;
7b9f35a6
WX
1346 struct drm_crtc *crtc = encoder->crtc;
1347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1348 int pipe = intel_crtc->pipe;
6547fef8 1349 struct drm_device *dev = encoder->dev;
72662e10 1350 struct drm_i915_private *dev_priv = dev->dev_private;
6547fef8
PZ
1351 enum port port = intel_ddi_get_encoder_port(intel_encoder);
1352 int type = intel_encoder->type;
7b9f35a6 1353 uint32_t tmp;
72662e10 1354
6547fef8 1355 if (type == INTEL_OUTPUT_HDMI) {
876a8cdf
DL
1356 struct intel_digital_port *intel_dig_port =
1357 enc_to_dig_port(encoder);
1358
6547fef8
PZ
1359 /* In HDMI/DVI mode, the port width, and swing/emphasis values
1360 * are ignored so nothing special needs to be done besides
1361 * enabling the port.
1362 */
876a8cdf 1363 I915_WRITE(DDI_BUF_CTL(port),
bcf53de4
SM
1364 intel_dig_port->saved_port_bits |
1365 DDI_BUF_CTL_ENABLE);
d6c50ff8
PZ
1366 } else if (type == INTEL_OUTPUT_EDP) {
1367 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1368
3ab9c637
ID
1369 if (port == PORT_A)
1370 intel_dp_stop_link_train(intel_dp);
1371
4be73780 1372 intel_edp_backlight_on(intel_dp);
4906557e 1373 intel_edp_psr_enable(intel_dp);
6547fef8 1374 }
7b9f35a6 1375
c77bf565 1376 if (intel_crtc->eld_vld && type != INTEL_OUTPUT_EDP) {
7b9f35a6
WX
1377 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
1378 tmp |= ((AUDIO_OUTPUT_ENABLE_A | AUDIO_ELD_VALID_A) << (pipe * 4));
1379 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
1380 }
5ab432ef
DV
1381}
1382
00c09d70 1383static void intel_disable_ddi(struct intel_encoder *intel_encoder)
5ab432ef 1384{
d6c50ff8 1385 struct drm_encoder *encoder = &intel_encoder->base;
7b9f35a6
WX
1386 struct drm_crtc *crtc = encoder->crtc;
1387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1388 int pipe = intel_crtc->pipe;
d6c50ff8 1389 int type = intel_encoder->type;
7b9f35a6
WX
1390 struct drm_device *dev = encoder->dev;
1391 struct drm_i915_private *dev_priv = dev->dev_private;
1392 uint32_t tmp;
d6c50ff8 1393
c77bf565
PZ
1394 if (intel_crtc->eld_vld && type != INTEL_OUTPUT_EDP) {
1395 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
1396 tmp &= ~((AUDIO_OUTPUT_ENABLE_A | AUDIO_ELD_VALID_A) <<
1397 (pipe * 4));
1398 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
1399 }
2831d842 1400
d6c50ff8
PZ
1401 if (type == INTEL_OUTPUT_EDP) {
1402 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1403
4906557e 1404 intel_edp_psr_disable(intel_dp);
4be73780 1405 intel_edp_backlight_off(intel_dp);
d6c50ff8 1406 }
72662e10 1407}
79f689aa 1408
b8fc2f6a 1409int intel_ddi_get_cdclk_freq(struct drm_i915_private *dev_priv)
79f689aa 1410{
e39bf98a 1411 struct drm_device *dev = dev_priv->dev;
a4006641 1412 uint32_t lcpll = I915_READ(LCPLL_CTL);
e39bf98a 1413 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
a4006641 1414
e39bf98a 1415 if (lcpll & LCPLL_CD_SOURCE_FCLK) {
a4006641 1416 return 800000;
e39bf98a 1417 } else if (I915_READ(HSW_FUSE_STRAP) & HSW_CDCLK_LIMIT) {
b2b877ff 1418 return 450000;
e39bf98a 1419 } else if (freq == LCPLL_CLK_FREQ_450) {
b2b877ff 1420 return 450000;
e39bf98a
PZ
1421 } else if (IS_HASWELL(dev)) {
1422 if (IS_ULT(dev))
1423 return 337500;
1424 else
1425 return 540000;
1426 } else {
1427 if (freq == LCPLL_CLK_FREQ_54O_BDW)
1428 return 540000;
1429 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
1430 return 337500;
1431 else
1432 return 675000;
1433 }
79f689aa
PZ
1434}
1435
1436void intel_ddi_pll_init(struct drm_device *dev)
1437{
1438 struct drm_i915_private *dev_priv = dev->dev_private;
1439 uint32_t val = I915_READ(LCPLL_CTL);
1440
1441 /* The LCPLL register should be turned on by the BIOS. For now let's
1442 * just check its state and print errors in case something is wrong.
1443 * Don't even try to turn it on.
1444 */
1445
b2b877ff 1446 DRM_DEBUG_KMS("CDCLK running at %dKHz\n",
79f689aa
PZ
1447 intel_ddi_get_cdclk_freq(dev_priv));
1448
1449 if (val & LCPLL_CD_SOURCE_FCLK)
1450 DRM_ERROR("CDCLK source is not LCPLL\n");
1451
1452 if (val & LCPLL_PLL_DISABLE)
1453 DRM_ERROR("LCPLL is disabled\n");
1454}
c19b0669
PZ
1455
1456void intel_ddi_prepare_link_retrain(struct drm_encoder *encoder)
1457{
174edf1f
PZ
1458 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
1459 struct intel_dp *intel_dp = &intel_dig_port->dp;
c19b0669 1460 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
174edf1f 1461 enum port port = intel_dig_port->port;
c19b0669 1462 uint32_t val;
f3e227df 1463 bool wait = false;
c19b0669
PZ
1464
1465 if (I915_READ(DP_TP_CTL(port)) & DP_TP_CTL_ENABLE) {
1466 val = I915_READ(DDI_BUF_CTL(port));
1467 if (val & DDI_BUF_CTL_ENABLE) {
1468 val &= ~DDI_BUF_CTL_ENABLE;
1469 I915_WRITE(DDI_BUF_CTL(port), val);
1470 wait = true;
1471 }
1472
1473 val = I915_READ(DP_TP_CTL(port));
1474 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1475 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
1476 I915_WRITE(DP_TP_CTL(port), val);
1477 POSTING_READ(DP_TP_CTL(port));
1478
1479 if (wait)
1480 intel_wait_ddi_buf_idle(dev_priv, port);
1481 }
1482
1483 val = DP_TP_CTL_ENABLE | DP_TP_CTL_MODE_SST |
1484 DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
6aba5b6c 1485 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
c19b0669
PZ
1486 val |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
1487 I915_WRITE(DP_TP_CTL(port), val);
1488 POSTING_READ(DP_TP_CTL(port));
1489
1490 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
1491 I915_WRITE(DDI_BUF_CTL(port), intel_dp->DP);
1492 POSTING_READ(DDI_BUF_CTL(port));
1493
1494 udelay(600);
1495}
00c09d70 1496
1ad960f2
PZ
1497void intel_ddi_fdi_disable(struct drm_crtc *crtc)
1498{
1499 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1500 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
1501 uint32_t val;
1502
1503 intel_ddi_post_disable(intel_encoder);
1504
1505 val = I915_READ(_FDI_RXA_CTL);
1506 val &= ~FDI_RX_ENABLE;
1507 I915_WRITE(_FDI_RXA_CTL, val);
1508
1509 val = I915_READ(_FDI_RXA_MISC);
1510 val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1511 val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
1512 I915_WRITE(_FDI_RXA_MISC, val);
1513
1514 val = I915_READ(_FDI_RXA_CTL);
1515 val &= ~FDI_PCDCLK;
1516 I915_WRITE(_FDI_RXA_CTL, val);
1517
1518 val = I915_READ(_FDI_RXA_CTL);
1519 val &= ~FDI_RX_PLL_ENABLE;
1520 I915_WRITE(_FDI_RXA_CTL, val);
1521}
1522
00c09d70
PZ
1523static void intel_ddi_hot_plug(struct intel_encoder *intel_encoder)
1524{
1525 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
1526 int type = intel_encoder->type;
1527
1528 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP)
1529 intel_dp_check_link_status(intel_dp);
1530}
1531
6801c18c
VS
1532void intel_ddi_get_config(struct intel_encoder *encoder,
1533 struct intel_crtc_config *pipe_config)
045ac3b5
JB
1534{
1535 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
1536 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
1537 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1538 u32 temp, flags = 0;
1539
1540 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1541 if (temp & TRANS_DDI_PHSYNC)
1542 flags |= DRM_MODE_FLAG_PHSYNC;
1543 else
1544 flags |= DRM_MODE_FLAG_NHSYNC;
1545 if (temp & TRANS_DDI_PVSYNC)
1546 flags |= DRM_MODE_FLAG_PVSYNC;
1547 else
1548 flags |= DRM_MODE_FLAG_NVSYNC;
1549
1550 pipe_config->adjusted_mode.flags |= flags;
42571aef
VS
1551
1552 switch (temp & TRANS_DDI_BPC_MASK) {
1553 case TRANS_DDI_BPC_6:
1554 pipe_config->pipe_bpp = 18;
1555 break;
1556 case TRANS_DDI_BPC_8:
1557 pipe_config->pipe_bpp = 24;
1558 break;
1559 case TRANS_DDI_BPC_10:
1560 pipe_config->pipe_bpp = 30;
1561 break;
1562 case TRANS_DDI_BPC_12:
1563 pipe_config->pipe_bpp = 36;
1564 break;
1565 default:
1566 break;
1567 }
eb14cb74
VS
1568
1569 switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
1570 case TRANS_DDI_MODE_SELECT_HDMI:
1571 case TRANS_DDI_MODE_SELECT_DVI:
1572 case TRANS_DDI_MODE_SELECT_FDI:
1573 break;
1574 case TRANS_DDI_MODE_SELECT_DP_SST:
1575 case TRANS_DDI_MODE_SELECT_DP_MST:
1576 pipe_config->has_dp_encoder = true;
1577 intel_dp_get_m_n(intel_crtc, pipe_config);
1578 break;
1579 default:
1580 break;
1581 }
10214420
DV
1582
1583 if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp_bpp &&
1584 pipe_config->pipe_bpp > dev_priv->vbt.edp_bpp) {
1585 /*
1586 * This is a big fat ugly hack.
1587 *
1588 * Some machines in UEFI boot mode provide us a VBT that has 18
1589 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
1590 * unknown we fail to light up. Yet the same BIOS boots up with
1591 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
1592 * max, not what it tells us to use.
1593 *
1594 * Note: This will still be broken if the eDP panel is not lit
1595 * up by the BIOS, and thus we can't get the mode at module
1596 * load.
1597 */
1598 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
1599 pipe_config->pipe_bpp, dev_priv->vbt.edp_bpp);
1600 dev_priv->vbt.edp_bpp = pipe_config->pipe_bpp;
1601 }
11578553
JB
1602
1603 intel_ddi_clock_get(encoder, pipe_config);
045ac3b5
JB
1604}
1605
00c09d70
PZ
1606static void intel_ddi_destroy(struct drm_encoder *encoder)
1607{
1608 /* HDMI has nothing special to destroy, so we can go with this. */
1609 intel_dp_encoder_destroy(encoder);
1610}
1611
5bfe2ac0
DV
1612static bool intel_ddi_compute_config(struct intel_encoder *encoder,
1613 struct intel_crtc_config *pipe_config)
00c09d70 1614{
5bfe2ac0 1615 int type = encoder->type;
eccb140b 1616 int port = intel_ddi_get_encoder_port(encoder);
00c09d70 1617
5bfe2ac0 1618 WARN(type == INTEL_OUTPUT_UNKNOWN, "compute_config() on unknown output!\n");
00c09d70 1619
eccb140b
DV
1620 if (port == PORT_A)
1621 pipe_config->cpu_transcoder = TRANSCODER_EDP;
1622
00c09d70 1623 if (type == INTEL_OUTPUT_HDMI)
5bfe2ac0 1624 return intel_hdmi_compute_config(encoder, pipe_config);
00c09d70 1625 else
5bfe2ac0 1626 return intel_dp_compute_config(encoder, pipe_config);
00c09d70
PZ
1627}
1628
1629static const struct drm_encoder_funcs intel_ddi_funcs = {
1630 .destroy = intel_ddi_destroy,
1631};
1632
4a28ae58
PZ
1633static struct intel_connector *
1634intel_ddi_init_dp_connector(struct intel_digital_port *intel_dig_port)
1635{
1636 struct intel_connector *connector;
1637 enum port port = intel_dig_port->port;
1638
1639 connector = kzalloc(sizeof(*connector), GFP_KERNEL);
1640 if (!connector)
1641 return NULL;
1642
1643 intel_dig_port->dp.output_reg = DDI_BUF_CTL(port);
1644 if (!intel_dp_init_connector(intel_dig_port, connector)) {
1645 kfree(connector);
1646 return NULL;
1647 }
1648
1649 return connector;
1650}
1651
1652static struct intel_connector *
1653intel_ddi_init_hdmi_connector(struct intel_digital_port *intel_dig_port)
1654{
1655 struct intel_connector *connector;
1656 enum port port = intel_dig_port->port;
1657
1658 connector = kzalloc(sizeof(*connector), GFP_KERNEL);
1659 if (!connector)
1660 return NULL;
1661
1662 intel_dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
1663 intel_hdmi_init_connector(intel_dig_port, connector);
1664
1665 return connector;
1666}
1667
00c09d70
PZ
1668void intel_ddi_init(struct drm_device *dev, enum port port)
1669{
876a8cdf 1670 struct drm_i915_private *dev_priv = dev->dev_private;
00c09d70
PZ
1671 struct intel_digital_port *intel_dig_port;
1672 struct intel_encoder *intel_encoder;
1673 struct drm_encoder *encoder;
1674 struct intel_connector *hdmi_connector = NULL;
1675 struct intel_connector *dp_connector = NULL;
311a2094
PZ
1676 bool init_hdmi, init_dp;
1677
1678 init_hdmi = (dev_priv->vbt.ddi_port_info[port].supports_dvi ||
1679 dev_priv->vbt.ddi_port_info[port].supports_hdmi);
1680 init_dp = dev_priv->vbt.ddi_port_info[port].supports_dp;
1681 if (!init_dp && !init_hdmi) {
1682 DRM_DEBUG_KMS("VBT says port %c is not DVI/HDMI/DP compatible\n",
1683 port_name(port));
1684 init_hdmi = true;
1685 init_dp = true;
1686 }
00c09d70 1687
b14c5679 1688 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
00c09d70
PZ
1689 if (!intel_dig_port)
1690 return;
1691
00c09d70
PZ
1692 intel_encoder = &intel_dig_port->base;
1693 encoder = &intel_encoder->base;
1694
1695 drm_encoder_init(dev, encoder, &intel_ddi_funcs,
1696 DRM_MODE_ENCODER_TMDS);
00c09d70 1697
5bfe2ac0 1698 intel_encoder->compute_config = intel_ddi_compute_config;
c7d8be30 1699 intel_encoder->mode_set = intel_ddi_mode_set;
00c09d70
PZ
1700 intel_encoder->enable = intel_enable_ddi;
1701 intel_encoder->pre_enable = intel_ddi_pre_enable;
1702 intel_encoder->disable = intel_disable_ddi;
1703 intel_encoder->post_disable = intel_ddi_post_disable;
1704 intel_encoder->get_hw_state = intel_ddi_get_hw_state;
045ac3b5 1705 intel_encoder->get_config = intel_ddi_get_config;
00c09d70
PZ
1706
1707 intel_dig_port->port = port;
bcf53de4
SM
1708 intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
1709 (DDI_BUF_PORT_REVERSAL |
1710 DDI_A_4_LANES);
00c09d70
PZ
1711
1712 intel_encoder->type = INTEL_OUTPUT_UNKNOWN;
1713 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
1714 intel_encoder->cloneable = false;
1715 intel_encoder->hot_plug = intel_ddi_hot_plug;
1716
4a28ae58
PZ
1717 if (init_dp)
1718 dp_connector = intel_ddi_init_dp_connector(intel_dig_port);
21a8e6a4 1719
311a2094
PZ
1720 /* In theory we don't need the encoder->type check, but leave it just in
1721 * case we have some really bad VBTs... */
4a28ae58
PZ
1722 if (intel_encoder->type != INTEL_OUTPUT_EDP && init_hdmi)
1723 hdmi_connector = intel_ddi_init_hdmi_connector(intel_dig_port);
21a8e6a4 1724
4a28ae58
PZ
1725 if (!dp_connector && !hdmi_connector) {
1726 drm_encoder_cleanup(encoder);
1727 kfree(intel_dig_port);
21a8e6a4 1728 }
00c09d70 1729}