blob: 9792ea8df13e43e7e3f047661ed24c843f422981 [file] [log] [blame]
Eugeni Dodonov45244b82012-05-09 15:37:20 -03001/*
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,
45 0x00FFFFFF, 0x00040006 /* HDMI parameters */
46};
47
48static const u32 hsw_ddi_translations_fdi[] = {
49 0x00FFFFFF, 0x0007000E, /* FDI parameters */
50 0x00D75FFF, 0x000F000A,
51 0x00C30FFF, 0x00060006,
52 0x00AAAFFF, 0x001E0000,
53 0x00FFFFFF, 0x000F000A,
54 0x00D75FFF, 0x00160004,
55 0x00C30FFF, 0x001E0000,
56 0x00FFFFFF, 0x00060006,
57 0x00D75FFF, 0x001E0000,
58 0x00FFFFFF, 0x00040006 /* HDMI parameters */
59};
60
Jani Nikula20f4dbe2013-08-30 19:40:28 +030061enum port intel_ddi_get_encoder_port(struct intel_encoder *intel_encoder)
Paulo Zanonifc914632012-10-05 12:05:54 -030062{
Paulo Zanoni0bdee302012-10-15 15:51:38 -030063 struct drm_encoder *encoder = &intel_encoder->base;
Paulo Zanonifc914632012-10-05 12:05:54 -030064 int type = intel_encoder->type;
65
Paulo Zanoni174edf12012-10-26 19:05:50 -020066 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP ||
Paulo Zanoni00c09d72012-10-26 19:05:52 -020067 type == INTEL_OUTPUT_HDMI || type == INTEL_OUTPUT_UNKNOWN) {
Paulo Zanoni174edf12012-10-26 19:05:50 -020068 struct intel_digital_port *intel_dig_port =
69 enc_to_dig_port(encoder);
70 return intel_dig_port->port;
Paulo Zanoni0bdee302012-10-15 15:51:38 -030071
Paulo Zanonifc914632012-10-05 12:05:54 -030072 } else if (type == INTEL_OUTPUT_ANALOG) {
73 return PORT_E;
Paulo Zanoni0bdee302012-10-15 15:51:38 -030074
Paulo Zanonifc914632012-10-05 12:05:54 -030075 } else {
76 DRM_ERROR("Invalid DDI encoder type %d\n", type);
77 BUG();
78 }
79}
80
Eugeni Dodonov45244b82012-05-09 15:37:20 -030081/* On Haswell, DDI port buffers must be programmed with correct values
82 * in advance. The buffer values are different for FDI and DP modes,
83 * but the HDMI/DVI fields are shared among those. So we program the DDI
84 * in either FDI or DP modes only, as HDMI connections will work with both
85 * of those
86 */
Paulo Zanoniad8d2702013-08-05 17:25:56 -030087static void intel_prepare_ddi_buffers(struct drm_device *dev, enum port port)
Eugeni Dodonov45244b82012-05-09 15:37:20 -030088{
89 struct drm_i915_private *dev_priv = dev->dev_private;
90 u32 reg;
91 int i;
Paulo Zanoniad8d2702013-08-05 17:25:56 -030092 const u32 *ddi_translations = (port == PORT_E) ?
Eugeni Dodonov45244b82012-05-09 15:37:20 -030093 hsw_ddi_translations_fdi :
Paulo Zanoniad8d2702013-08-05 17:25:56 -030094 hsw_ddi_translations_dp;
Eugeni Dodonov45244b82012-05-09 15:37:20 -030095
Paulo Zanonif72d19f2013-08-05 17:25:55 -030096 for (i = 0, reg = DDI_BUF_TRANS(port);
97 i < ARRAY_SIZE(hsw_ddi_translations_fdi); i++) {
Eugeni Dodonov45244b82012-05-09 15:37:20 -030098 I915_WRITE(reg, ddi_translations[i]);
99 reg += 4;
100 }
101}
102
103/* Program DDI buffers translations for DP. By default, program ports A-D in DP
104 * mode and port E for FDI.
105 */
106void intel_prepare_ddi(struct drm_device *dev)
107{
108 int port;
109
Paulo Zanoni0d536cb2012-11-23 16:46:41 -0200110 if (!HAS_DDI(dev))
111 return;
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300112
Paulo Zanoniad8d2702013-08-05 17:25:56 -0300113 for (port = PORT_A; port <= PORT_E; port++)
114 intel_prepare_ddi_buffers(dev, port);
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300115}
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300116
117static const long hsw_ddi_buf_ctl_values[] = {
118 DDI_BUF_EMP_400MV_0DB_HSW,
119 DDI_BUF_EMP_400MV_3_5DB_HSW,
120 DDI_BUF_EMP_400MV_6DB_HSW,
121 DDI_BUF_EMP_400MV_9_5DB_HSW,
122 DDI_BUF_EMP_600MV_0DB_HSW,
123 DDI_BUF_EMP_600MV_3_5DB_HSW,
124 DDI_BUF_EMP_600MV_6DB_HSW,
125 DDI_BUF_EMP_800MV_0DB_HSW,
126 DDI_BUF_EMP_800MV_3_5DB_HSW
127};
128
Paulo Zanoni248138b2012-11-29 11:29:31 -0200129static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
130 enum port port)
131{
132 uint32_t reg = DDI_BUF_CTL(port);
133 int i;
134
135 for (i = 0; i < 8; i++) {
136 udelay(1);
137 if (I915_READ(reg) & DDI_BUF_IS_IDLE)
138 return;
139 }
140 DRM_ERROR("Timeout waiting for DDI BUF %c idle bit\n", port_name(port));
141}
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300142
143/* Starting with Haswell, different DDI ports can work in FDI mode for
144 * connection to the PCH-located connectors. For this, it is necessary to train
145 * both the DDI port and PCH receiver for the desired DDI buffer settings.
146 *
147 * The recommended port to work in FDI mode is DDI E, which we use here. Also,
148 * please note that when FDI mode is active on DDI E, it shares 2 lines with
149 * DDI A (which is used for eDP)
150 */
151
152void hsw_fdi_link_train(struct drm_crtc *crtc)
153{
154 struct drm_device *dev = crtc->dev;
155 struct drm_i915_private *dev_priv = dev->dev_private;
156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni04945642012-11-01 21:00:59 -0200157 u32 temp, i, rx_ctl_val;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300158
Paulo Zanoni04945642012-11-01 21:00:59 -0200159 /* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
160 * mode set "sequence for CRT port" document:
161 * - TP1 to TP2 time with the default value
162 * - FDI delay to 90h
Damien Lespiau8693a822013-05-03 18:48:11 +0100163 *
164 * WaFDIAutoLinkSetTimingOverrride:hsw
Paulo Zanoni04945642012-11-01 21:00:59 -0200165 */
166 I915_WRITE(_FDI_RXA_MISC, FDI_RX_PWRDN_LANE1_VAL(2) |
167 FDI_RX_PWRDN_LANE0_VAL(2) |
168 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
169
170 /* Enable the PCH Receiver FDI PLL */
Damien Lespiau3e683202012-12-11 18:48:29 +0000171 rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
Daniel Vetter33d29b12013-02-13 18:04:45 +0100172 FDI_RX_PLL_ENABLE |
Daniel Vetter627eb5a2013-04-29 19:33:42 +0200173 FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Paulo Zanoni04945642012-11-01 21:00:59 -0200174 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
175 POSTING_READ(_FDI_RXA_CTL);
176 udelay(220);
177
178 /* Switch from Rawclk to PCDclk */
179 rx_ctl_val |= FDI_PCDCLK;
180 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
181
182 /* Configure Port Clock Select */
183 I915_WRITE(PORT_CLK_SEL(PORT_E), intel_crtc->ddi_pll_sel);
184
185 /* Start the training iterating through available voltages and emphasis,
186 * testing each value twice. */
187 for (i = 0; i < ARRAY_SIZE(hsw_ddi_buf_ctl_values) * 2; i++) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300188 /* Configure DP_TP_CTL with auto-training */
189 I915_WRITE(DP_TP_CTL(PORT_E),
190 DP_TP_CTL_FDI_AUTOTRAIN |
191 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
192 DP_TP_CTL_LINK_TRAIN_PAT1 |
193 DP_TP_CTL_ENABLE);
194
Damien Lespiau876a8cd2012-12-11 18:48:30 +0000195 /* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
196 * DDI E does not support port reversal, the functionality is
197 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
198 * port reversal bit */
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300199 I915_WRITE(DDI_BUF_CTL(PORT_E),
Paulo Zanoni04945642012-11-01 21:00:59 -0200200 DDI_BUF_CTL_ENABLE |
Daniel Vetter33d29b12013-02-13 18:04:45 +0100201 ((intel_crtc->config.fdi_lanes - 1) << 1) |
Paulo Zanoni04945642012-11-01 21:00:59 -0200202 hsw_ddi_buf_ctl_values[i / 2]);
203 POSTING_READ(DDI_BUF_CTL(PORT_E));
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300204
205 udelay(600);
206
Paulo Zanoni04945642012-11-01 21:00:59 -0200207 /* Program PCH FDI Receiver TU */
208 I915_WRITE(_FDI_RXA_TUSIZE1, TU_SIZE(64));
Eugeni Dodonov4acf5182012-07-04 20:15:16 -0300209
Paulo Zanoni04945642012-11-01 21:00:59 -0200210 /* Enable PCH FDI Receiver with auto-training */
211 rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
212 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
213 POSTING_READ(_FDI_RXA_CTL);
214
215 /* Wait for FDI receiver lane calibration */
216 udelay(30);
217
218 /* Unset FDI_RX_MISC pwrdn lanes */
219 temp = I915_READ(_FDI_RXA_MISC);
220 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
221 I915_WRITE(_FDI_RXA_MISC, temp);
222 POSTING_READ(_FDI_RXA_MISC);
223
224 /* Wait for FDI auto training time */
225 udelay(5);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300226
227 temp = I915_READ(DP_TP_STATUS(PORT_E));
228 if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
Paulo Zanoni04945642012-11-01 21:00:59 -0200229 DRM_DEBUG_KMS("FDI link training done on step %d\n", i);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300230
231 /* Enable normal pixel sending for FDI */
232 I915_WRITE(DP_TP_CTL(PORT_E),
Paulo Zanoni04945642012-11-01 21:00:59 -0200233 DP_TP_CTL_FDI_AUTOTRAIN |
234 DP_TP_CTL_LINK_TRAIN_NORMAL |
235 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
236 DP_TP_CTL_ENABLE);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300237
Paulo Zanoni04945642012-11-01 21:00:59 -0200238 return;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300239 }
Paulo Zanoni04945642012-11-01 21:00:59 -0200240
Paulo Zanoni248138b2012-11-29 11:29:31 -0200241 temp = I915_READ(DDI_BUF_CTL(PORT_E));
242 temp &= ~DDI_BUF_CTL_ENABLE;
243 I915_WRITE(DDI_BUF_CTL(PORT_E), temp);
244 POSTING_READ(DDI_BUF_CTL(PORT_E));
245
Paulo Zanoni04945642012-11-01 21:00:59 -0200246 /* Disable DP_TP_CTL and FDI_RX_CTL and retry */
Paulo Zanoni248138b2012-11-29 11:29:31 -0200247 temp = I915_READ(DP_TP_CTL(PORT_E));
248 temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
249 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
250 I915_WRITE(DP_TP_CTL(PORT_E), temp);
251 POSTING_READ(DP_TP_CTL(PORT_E));
252
253 intel_wait_ddi_buf_idle(dev_priv, PORT_E);
Paulo Zanoni04945642012-11-01 21:00:59 -0200254
255 rx_ctl_val &= ~FDI_RX_ENABLE;
256 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
Paulo Zanoni248138b2012-11-29 11:29:31 -0200257 POSTING_READ(_FDI_RXA_CTL);
Paulo Zanoni04945642012-11-01 21:00:59 -0200258
259 /* Reset FDI_RX_MISC pwrdn lanes */
260 temp = I915_READ(_FDI_RXA_MISC);
261 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
262 temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
263 I915_WRITE(_FDI_RXA_MISC, temp);
Paulo Zanoni248138b2012-11-29 11:29:31 -0200264 POSTING_READ(_FDI_RXA_MISC);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300265 }
266
Paulo Zanoni04945642012-11-01 21:00:59 -0200267 DRM_ERROR("FDI link training failed!\n");
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300268}
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -0300269
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200270static void intel_ddi_mode_set(struct intel_encoder *encoder)
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300271{
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200272 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
273 int port = intel_ddi_get_encoder_port(encoder);
274 int pipe = crtc->pipe;
275 int type = encoder->type;
276 struct drm_display_mode *adjusted_mode = &crtc->config.adjusted_mode;
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300277
Damien Lespiaubf98a722013-04-19 14:27:31 +0100278 DRM_DEBUG_KMS("Preparing DDI mode on port %c, pipe %c\n",
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300279 port_name(port), pipe_name(pipe));
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300280
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200281 crtc->eld_vld = false;
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300282 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200283 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Damien Lespiau876a8cd2012-12-11 18:48:30 +0000284 struct intel_digital_port *intel_dig_port =
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200285 enc_to_dig_port(&encoder->base);
Wang Xingchao4f078542012-08-09 16:52:16 +0800286
Stéphane Marchesinbcf53de2013-07-12 13:54:41 -0700287 intel_dp->DP = intel_dig_port->saved_port_bits |
Damien Lespiau876a8cd2012-12-11 18:48:30 +0000288 DDI_BUF_CTL_ENABLE | DDI_BUF_EMP_400MV_0DB_HSW;
Daniel Vetter17aa6be2013-04-30 14:01:40 +0200289 intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300290
Takashi Iwai8fed6192012-11-19 18:06:51 +0100291 if (intel_dp->has_audio) {
292 DRM_DEBUG_DRIVER("DP audio on pipe %c on DDI\n",
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200293 pipe_name(crtc->pipe));
Takashi Iwai8fed6192012-11-19 18:06:51 +0100294
295 /* write eld */
296 DRM_DEBUG_DRIVER("DP audio: write eld information\n");
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200297 intel_write_eld(&encoder->base, adjusted_mode);
Takashi Iwai8fed6192012-11-19 18:06:51 +0100298 }
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300299 } else if (type == INTEL_OUTPUT_HDMI) {
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200300 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300301
302 if (intel_hdmi->has_audio) {
303 /* Proper support for digital audio needs a new logic
304 * and a new set of registers, so we leave it for future
305 * patch bombing.
306 */
307 DRM_DEBUG_DRIVER("HDMI audio on pipe %c on DDI\n",
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200308 pipe_name(crtc->pipe));
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300309
310 /* write eld */
311 DRM_DEBUG_DRIVER("HDMI audio: write eld information\n");
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200312 intel_write_eld(&encoder->base, adjusted_mode);
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300313 }
314
Daniel Vetterc7d8be32013-07-21 21:37:07 +0200315 intel_hdmi->set_infoframes(&encoder->base, adjusted_mode);
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300316 }
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300317}
318
319static struct intel_encoder *
320intel_ddi_get_crtc_encoder(struct drm_crtc *crtc)
321{
322 struct drm_device *dev = crtc->dev;
323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
324 struct intel_encoder *intel_encoder, *ret = NULL;
325 int num_encoders = 0;
326
327 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
328 ret = intel_encoder;
329 num_encoders++;
330 }
331
332 if (num_encoders != 1)
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300333 WARN(1, "%d encoders on crtc for pipe %c\n", num_encoders,
334 pipe_name(intel_crtc->pipe));
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300335
336 BUG_ON(ret == NULL);
337 return ret;
338}
339
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300340void intel_ddi_put_crtc_pll(struct drm_crtc *crtc)
341{
342 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
343 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
345 uint32_t val;
346
347 switch (intel_crtc->ddi_pll_sel) {
348 case PORT_CLK_SEL_SPLL:
349 plls->spll_refcount--;
350 if (plls->spll_refcount == 0) {
351 DRM_DEBUG_KMS("Disabling SPLL\n");
352 val = I915_READ(SPLL_CTL);
353 WARN_ON(!(val & SPLL_PLL_ENABLE));
354 I915_WRITE(SPLL_CTL, val & ~SPLL_PLL_ENABLE);
355 POSTING_READ(SPLL_CTL);
356 }
357 break;
358 case PORT_CLK_SEL_WRPLL1:
359 plls->wrpll1_refcount--;
360 if (plls->wrpll1_refcount == 0) {
361 DRM_DEBUG_KMS("Disabling WRPLL 1\n");
362 val = I915_READ(WRPLL_CTL1);
363 WARN_ON(!(val & WRPLL_PLL_ENABLE));
364 I915_WRITE(WRPLL_CTL1, val & ~WRPLL_PLL_ENABLE);
365 POSTING_READ(WRPLL_CTL1);
366 }
367 break;
368 case PORT_CLK_SEL_WRPLL2:
369 plls->wrpll2_refcount--;
370 if (plls->wrpll2_refcount == 0) {
371 DRM_DEBUG_KMS("Disabling WRPLL 2\n");
372 val = I915_READ(WRPLL_CTL2);
373 WARN_ON(!(val & WRPLL_PLL_ENABLE));
374 I915_WRITE(WRPLL_CTL2, val & ~WRPLL_PLL_ENABLE);
375 POSTING_READ(WRPLL_CTL2);
376 }
377 break;
378 }
379
380 WARN(plls->spll_refcount < 0, "Invalid SPLL refcount\n");
381 WARN(plls->wrpll1_refcount < 0, "Invalid WRPLL1 refcount\n");
382 WARN(plls->wrpll2_refcount < 0, "Invalid WRPLL2 refcount\n");
383
384 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_NONE;
385}
386
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100387#define LC_FREQ 2700
388#define LC_FREQ_2K (LC_FREQ * 2000)
389
390#define P_MIN 2
391#define P_MAX 64
392#define P_INC 2
393
394/* Constraints for PLL good behavior */
395#define REF_MIN 48
396#define REF_MAX 400
397#define VCO_MIN 2400
398#define VCO_MAX 4800
399
400#define ABS_DIFF(a, b) ((a > b) ? (a - b) : (b - a))
401
402struct wrpll_rnp {
403 unsigned p, n2, r2;
404};
405
406static unsigned wrpll_get_budget_for_freq(int clock)
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300407{
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100408 unsigned budget;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300409
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100410 switch (clock) {
411 case 25175000:
412 case 25200000:
413 case 27000000:
414 case 27027000:
415 case 37762500:
416 case 37800000:
417 case 40500000:
418 case 40541000:
419 case 54000000:
420 case 54054000:
421 case 59341000:
422 case 59400000:
423 case 72000000:
424 case 74176000:
425 case 74250000:
426 case 81000000:
427 case 81081000:
428 case 89012000:
429 case 89100000:
430 case 108000000:
431 case 108108000:
432 case 111264000:
433 case 111375000:
434 case 148352000:
435 case 148500000:
436 case 162000000:
437 case 162162000:
438 case 222525000:
439 case 222750000:
440 case 296703000:
441 case 297000000:
442 budget = 0;
443 break;
444 case 233500000:
445 case 245250000:
446 case 247750000:
447 case 253250000:
448 case 298000000:
449 budget = 1500;
450 break;
451 case 169128000:
452 case 169500000:
453 case 179500000:
454 case 202000000:
455 budget = 2000;
456 break;
457 case 256250000:
458 case 262500000:
459 case 270000000:
460 case 272500000:
461 case 273750000:
462 case 280750000:
463 case 281250000:
464 case 286000000:
465 case 291750000:
466 budget = 4000;
467 break;
468 case 267250000:
469 case 268500000:
470 budget = 5000;
471 break;
472 default:
473 budget = 1000;
474 break;
475 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300476
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100477 return budget;
478}
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300479
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100480static void wrpll_update_rnp(uint64_t freq2k, unsigned budget,
481 unsigned r2, unsigned n2, unsigned p,
482 struct wrpll_rnp *best)
483{
484 uint64_t a, b, c, d, diff, diff_best;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300485
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100486 /* No best (r,n,p) yet */
487 if (best->p == 0) {
488 best->p = p;
489 best->n2 = n2;
490 best->r2 = r2;
491 return;
492 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300493
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100494 /*
495 * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
496 * freq2k.
497 *
498 * delta = 1e6 *
499 * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
500 * freq2k;
501 *
502 * and we would like delta <= budget.
503 *
504 * If the discrepancy is above the PPM-based budget, always prefer to
505 * improve upon the previous solution. However, if you're within the
506 * budget, try to maximize Ref * VCO, that is N / (P * R^2).
507 */
508 a = freq2k * budget * p * r2;
509 b = freq2k * budget * best->p * best->r2;
510 diff = ABS_DIFF((freq2k * p * r2), (LC_FREQ_2K * n2));
511 diff_best = ABS_DIFF((freq2k * best->p * best->r2),
512 (LC_FREQ_2K * best->n2));
513 c = 1000000 * diff;
514 d = 1000000 * diff_best;
515
516 if (a < c && b < d) {
517 /* If both are above the budget, pick the closer */
518 if (best->p * best->r2 * diff < p * r2 * diff_best) {
519 best->p = p;
520 best->n2 = n2;
521 best->r2 = r2;
522 }
523 } else if (a >= c && b < d) {
524 /* If A is below the threshold but B is above it? Update. */
525 best->p = p;
526 best->n2 = n2;
527 best->r2 = r2;
528 } else if (a >= c && b >= d) {
529 /* Both are below the limit, so pick the higher n2/(r2*r2) */
530 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
531 best->p = p;
532 best->n2 = n2;
533 best->r2 = r2;
534 }
535 }
536 /* Otherwise a < c && b >= d, do nothing */
537}
538
539static void
540intel_ddi_calculate_wrpll(int clock /* in Hz */,
541 unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
542{
543 uint64_t freq2k;
544 unsigned p, n2, r2;
545 struct wrpll_rnp best = { 0, 0, 0 };
546 unsigned budget;
547
548 freq2k = clock / 100;
549
550 budget = wrpll_get_budget_for_freq(clock);
551
552 /* Special case handling for 540 pixel clock: bypass WR PLL entirely
553 * and directly pass the LC PLL to it. */
554 if (freq2k == 5400000) {
555 *n2_out = 2;
556 *p_out = 1;
557 *r2_out = 2;
558 return;
559 }
560
561 /*
562 * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
563 * the WR PLL.
564 *
565 * We want R so that REF_MIN <= Ref <= REF_MAX.
566 * Injecting R2 = 2 * R gives:
567 * REF_MAX * r2 > LC_FREQ * 2 and
568 * REF_MIN * r2 < LC_FREQ * 2
569 *
570 * Which means the desired boundaries for r2 are:
571 * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
572 *
573 */
574 for (r2 = LC_FREQ * 2 / REF_MAX + 1;
575 r2 <= LC_FREQ * 2 / REF_MIN;
576 r2++) {
577
578 /*
579 * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
580 *
581 * Once again we want VCO_MIN <= VCO <= VCO_MAX.
582 * Injecting R2 = 2 * R and N2 = 2 * N, we get:
583 * VCO_MAX * r2 > n2 * LC_FREQ and
584 * VCO_MIN * r2 < n2 * LC_FREQ)
585 *
586 * Which means the desired boundaries for n2 are:
587 * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
588 */
589 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
590 n2 <= VCO_MAX * r2 / LC_FREQ;
591 n2++) {
592
593 for (p = P_MIN; p <= P_MAX; p += P_INC)
594 wrpll_update_rnp(freq2k, budget,
595 r2, n2, p, &best);
596 }
597 }
598
599 *n2_out = best.n2;
600 *p_out = best.p;
601 *r2_out = best.r2;
602
603 DRM_DEBUG_KMS("WRPLL: %dHz refresh rate with p=%d, n2=%d r2=%d\n",
604 clock, *p_out, *n2_out, *r2_out);
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300605}
606
Daniel Vetterff9a6752013-06-01 17:16:21 +0200607bool intel_ddi_pll_mode_set(struct drm_crtc *crtc)
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300608{
609 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
610 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
Paulo Zanoni068759b2012-10-15 15:51:31 -0300611 struct drm_encoder *encoder = &intel_encoder->base;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300612 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
613 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
614 int type = intel_encoder->type;
615 enum pipe pipe = intel_crtc->pipe;
616 uint32_t reg, val;
Daniel Vetterff9a6752013-06-01 17:16:21 +0200617 int clock = intel_crtc->config.port_clock;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300618
619 /* TODO: reuse PLLs when possible (compare values) */
620
621 intel_ddi_put_crtc_pll(crtc);
622
Paulo Zanoni068759b2012-10-15 15:51:31 -0300623 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
624 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
625
626 switch (intel_dp->link_bw) {
627 case DP_LINK_BW_1_62:
628 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_LCPLL_810;
629 break;
630 case DP_LINK_BW_2_7:
631 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_LCPLL_1350;
632 break;
633 case DP_LINK_BW_5_4:
634 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_LCPLL_2700;
635 break;
636 default:
637 DRM_ERROR("Link bandwidth %d unsupported\n",
638 intel_dp->link_bw);
639 return false;
640 }
641
642 /* We don't need to turn any PLL on because we'll use LCPLL. */
643 return true;
644
645 } else if (type == INTEL_OUTPUT_HDMI) {
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100646 unsigned p, n2, r2;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300647
648 if (plls->wrpll1_refcount == 0) {
649 DRM_DEBUG_KMS("Using WRPLL 1 on pipe %c\n",
650 pipe_name(pipe));
651 plls->wrpll1_refcount++;
652 reg = WRPLL_CTL1;
653 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_WRPLL1;
654 } else if (plls->wrpll2_refcount == 0) {
655 DRM_DEBUG_KMS("Using WRPLL 2 on pipe %c\n",
656 pipe_name(pipe));
657 plls->wrpll2_refcount++;
658 reg = WRPLL_CTL2;
659 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_WRPLL2;
660 } else {
661 DRM_ERROR("No WRPLLs available!\n");
662 return false;
663 }
664
665 WARN(I915_READ(reg) & WRPLL_PLL_ENABLE,
666 "WRPLL already enabled\n");
667
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100668 intel_ddi_calculate_wrpll(clock * 1000, &r2, &n2, &p);
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300669
670 val = WRPLL_PLL_ENABLE | WRPLL_PLL_SELECT_LCPLL_2700 |
671 WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
672 WRPLL_DIVIDER_POST(p);
673
674 } else if (type == INTEL_OUTPUT_ANALOG) {
675 if (plls->spll_refcount == 0) {
676 DRM_DEBUG_KMS("Using SPLL on pipe %c\n",
677 pipe_name(pipe));
678 plls->spll_refcount++;
679 reg = SPLL_CTL;
680 intel_crtc->ddi_pll_sel = PORT_CLK_SEL_SPLL;
Damien Lespiau00037c22013-03-07 15:30:25 +0000681 } else {
682 DRM_ERROR("SPLL already in use\n");
683 return false;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300684 }
685
686 WARN(I915_READ(reg) & SPLL_PLL_ENABLE,
687 "SPLL already enabled\n");
688
Damien Lespiau39bc66c2012-10-11 15:24:04 +0100689 val = SPLL_PLL_ENABLE | SPLL_PLL_FREQ_1350MHz | SPLL_PLL_SSC;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300690
691 } else {
692 WARN(1, "Invalid DDI encoder type %d\n", type);
693 return false;
694 }
695
696 I915_WRITE(reg, val);
697 udelay(20);
698
699 return true;
700}
701
Paulo Zanonidae84792012-10-15 15:51:30 -0300702void intel_ddi_set_pipe_settings(struct drm_crtc *crtc)
703{
704 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
706 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +0200707 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanonidae84792012-10-15 15:51:30 -0300708 int type = intel_encoder->type;
709 uint32_t temp;
710
711 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
712
Paulo Zanonic9809792012-10-23 18:30:00 -0200713 temp = TRANS_MSA_SYNC_CLK;
Daniel Vetter965e0c42013-03-27 00:44:57 +0100714 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonidae84792012-10-15 15:51:30 -0300715 case 18:
Paulo Zanonic9809792012-10-23 18:30:00 -0200716 temp |= TRANS_MSA_6_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -0300717 break;
718 case 24:
Paulo Zanonic9809792012-10-23 18:30:00 -0200719 temp |= TRANS_MSA_8_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -0300720 break;
721 case 30:
Paulo Zanonic9809792012-10-23 18:30:00 -0200722 temp |= TRANS_MSA_10_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -0300723 break;
724 case 36:
Paulo Zanonic9809792012-10-23 18:30:00 -0200725 temp |= TRANS_MSA_12_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -0300726 break;
727 default:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +0100728 BUG();
Paulo Zanonidae84792012-10-15 15:51:30 -0300729 }
Paulo Zanonic9809792012-10-23 18:30:00 -0200730 I915_WRITE(TRANS_MSA_MISC(cpu_transcoder), temp);
Paulo Zanonidae84792012-10-15 15:51:30 -0300731 }
732}
733
Damien Lespiau8228c252013-03-07 15:30:27 +0000734void intel_ddi_enable_transcoder_func(struct drm_crtc *crtc)
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300735{
736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
737 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
Paulo Zanoni7739c332012-10-15 15:51:29 -0300738 struct drm_encoder *encoder = &intel_encoder->base;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300739 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
740 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +0200741 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni174edf12012-10-26 19:05:50 -0200742 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni7739c332012-10-15 15:51:29 -0300743 int type = intel_encoder->type;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300744 uint32_t temp;
745
Paulo Zanoniad80a812012-10-24 16:06:19 -0200746 /* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
747 temp = TRANS_DDI_FUNC_ENABLE;
Paulo Zanoni174edf12012-10-26 19:05:50 -0200748 temp |= TRANS_DDI_SELECT_PORT(port);
Paulo Zanonidfcef252012-08-08 14:15:29 -0300749
Daniel Vetter965e0c42013-03-27 00:44:57 +0100750 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonidfcef252012-08-08 14:15:29 -0300751 case 18:
Paulo Zanoniad80a812012-10-24 16:06:19 -0200752 temp |= TRANS_DDI_BPC_6;
Paulo Zanonidfcef252012-08-08 14:15:29 -0300753 break;
754 case 24:
Paulo Zanoniad80a812012-10-24 16:06:19 -0200755 temp |= TRANS_DDI_BPC_8;
Paulo Zanonidfcef252012-08-08 14:15:29 -0300756 break;
757 case 30:
Paulo Zanoniad80a812012-10-24 16:06:19 -0200758 temp |= TRANS_DDI_BPC_10;
Paulo Zanonidfcef252012-08-08 14:15:29 -0300759 break;
760 case 36:
Paulo Zanoniad80a812012-10-24 16:06:19 -0200761 temp |= TRANS_DDI_BPC_12;
Paulo Zanonidfcef252012-08-08 14:15:29 -0300762 break;
763 default:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +0100764 BUG();
Paulo Zanonidfcef252012-08-08 14:15:29 -0300765 }
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300766
Ville Syrjäläa6662832013-09-10 17:03:41 +0300767 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
Paulo Zanoniad80a812012-10-24 16:06:19 -0200768 temp |= TRANS_DDI_PVSYNC;
Ville Syrjäläa6662832013-09-10 17:03:41 +0300769 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
Paulo Zanoniad80a812012-10-24 16:06:19 -0200770 temp |= TRANS_DDI_PHSYNC;
Paulo Zanonif63eb7c42012-08-08 14:15:28 -0300771
Paulo Zanonie6f0bfc2012-10-23 18:30:04 -0200772 if (cpu_transcoder == TRANSCODER_EDP) {
773 switch (pipe) {
774 case PIPE_A:
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -0200775 /* Can only use the always-on power well for eDP when
776 * not using the panel fitter, and when not using motion
777 * blur mitigation (which we don't support). */
Chris Wilsonfd4daa92013-08-27 17:04:17 +0100778 if (intel_crtc->config.pch_pfit.enabled)
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -0200779 temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
780 else
781 temp |= TRANS_DDI_EDP_INPUT_A_ON;
Paulo Zanonie6f0bfc2012-10-23 18:30:04 -0200782 break;
783 case PIPE_B:
784 temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
785 break;
786 case PIPE_C:
787 temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
788 break;
789 default:
790 BUG();
791 break;
792 }
793 }
794
Paulo Zanoni7739c332012-10-15 15:51:29 -0300795 if (type == INTEL_OUTPUT_HDMI) {
796 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300797
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300798 if (intel_hdmi->has_hdmi_sink)
Paulo Zanoniad80a812012-10-24 16:06:19 -0200799 temp |= TRANS_DDI_MODE_SELECT_HDMI;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300800 else
Paulo Zanoniad80a812012-10-24 16:06:19 -0200801 temp |= TRANS_DDI_MODE_SELECT_DVI;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300802
Paulo Zanoni7739c332012-10-15 15:51:29 -0300803 } else if (type == INTEL_OUTPUT_ANALOG) {
Paulo Zanoniad80a812012-10-24 16:06:19 -0200804 temp |= TRANS_DDI_MODE_SELECT_FDI;
Daniel Vetter33d29b12013-02-13 18:04:45 +0100805 temp |= (intel_crtc->config.fdi_lanes - 1) << 1;
Paulo Zanoni7739c332012-10-15 15:51:29 -0300806
807 } else if (type == INTEL_OUTPUT_DISPLAYPORT ||
808 type == INTEL_OUTPUT_EDP) {
809 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
810
Paulo Zanoniad80a812012-10-24 16:06:19 -0200811 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
Paulo Zanoni7739c332012-10-15 15:51:29 -0300812
Daniel Vetter17aa6be2013-04-30 14:01:40 +0200813 temp |= DDI_PORT_WIDTH(intel_dp->lane_count);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300814 } else {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300815 WARN(1, "Invalid encoder type %d for pipe %c\n",
816 intel_encoder->type, pipe_name(pipe));
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300817 }
818
Paulo Zanoniad80a812012-10-24 16:06:19 -0200819 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300820}
821
Paulo Zanoniad80a812012-10-24 16:06:19 -0200822void intel_ddi_disable_transcoder_func(struct drm_i915_private *dev_priv,
823 enum transcoder cpu_transcoder)
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300824{
Paulo Zanoniad80a812012-10-24 16:06:19 -0200825 uint32_t reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300826 uint32_t val = I915_READ(reg);
827
Paulo Zanoniad80a812012-10-24 16:06:19 -0200828 val &= ~(TRANS_DDI_FUNC_ENABLE | TRANS_DDI_PORT_MASK);
829 val |= TRANS_DDI_PORT_NONE;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300830 I915_WRITE(reg, val);
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300831}
832
Paulo Zanonibcbc8892012-10-26 19:05:51 -0200833bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
834{
835 struct drm_device *dev = intel_connector->base.dev;
836 struct drm_i915_private *dev_priv = dev->dev_private;
837 struct intel_encoder *intel_encoder = intel_connector->encoder;
838 int type = intel_connector->base.connector_type;
839 enum port port = intel_ddi_get_encoder_port(intel_encoder);
840 enum pipe pipe = 0;
841 enum transcoder cpu_transcoder;
842 uint32_t tmp;
843
844 if (!intel_encoder->get_hw_state(intel_encoder, &pipe))
845 return false;
846
847 if (port == PORT_A)
848 cpu_transcoder = TRANSCODER_EDP;
849 else
Daniel Vetter1a240d42012-11-29 22:18:51 +0100850 cpu_transcoder = (enum transcoder) pipe;
Paulo Zanonibcbc8892012-10-26 19:05:51 -0200851
852 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
853
854 switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
855 case TRANS_DDI_MODE_SELECT_HDMI:
856 case TRANS_DDI_MODE_SELECT_DVI:
857 return (type == DRM_MODE_CONNECTOR_HDMIA);
858
859 case TRANS_DDI_MODE_SELECT_DP_SST:
860 if (type == DRM_MODE_CONNECTOR_eDP)
861 return true;
862 case TRANS_DDI_MODE_SELECT_DP_MST:
863 return (type == DRM_MODE_CONNECTOR_DisplayPort);
864
865 case TRANS_DDI_MODE_SELECT_FDI:
866 return (type == DRM_MODE_CONNECTOR_VGA);
867
868 default:
869 return false;
870 }
871}
872
Daniel Vetter85234cd2012-07-02 13:27:29 +0200873bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
874 enum pipe *pipe)
875{
876 struct drm_device *dev = encoder->base.dev;
877 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonife43d3f2012-10-15 15:51:39 -0300878 enum port port = intel_ddi_get_encoder_port(encoder);
Daniel Vetter85234cd2012-07-02 13:27:29 +0200879 u32 tmp;
880 int i;
881
Paulo Zanonife43d3f2012-10-15 15:51:39 -0300882 tmp = I915_READ(DDI_BUF_CTL(port));
Daniel Vetter85234cd2012-07-02 13:27:29 +0200883
884 if (!(tmp & DDI_BUF_CTL_ENABLE))
885 return false;
886
Paulo Zanoniad80a812012-10-24 16:06:19 -0200887 if (port == PORT_A) {
888 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
Daniel Vetter85234cd2012-07-02 13:27:29 +0200889
Paulo Zanoniad80a812012-10-24 16:06:19 -0200890 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
891 case TRANS_DDI_EDP_INPUT_A_ON:
892 case TRANS_DDI_EDP_INPUT_A_ONOFF:
893 *pipe = PIPE_A;
894 break;
895 case TRANS_DDI_EDP_INPUT_B_ONOFF:
896 *pipe = PIPE_B;
897 break;
898 case TRANS_DDI_EDP_INPUT_C_ONOFF:
899 *pipe = PIPE_C;
900 break;
901 }
902
903 return true;
904 } else {
905 for (i = TRANSCODER_A; i <= TRANSCODER_C; i++) {
906 tmp = I915_READ(TRANS_DDI_FUNC_CTL(i));
907
908 if ((tmp & TRANS_DDI_PORT_MASK)
909 == TRANS_DDI_SELECT_PORT(port)) {
910 *pipe = i;
911 return true;
912 }
Daniel Vetter85234cd2012-07-02 13:27:29 +0200913 }
914 }
915
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300916 DRM_DEBUG_KMS("No pipe for ddi port %c found\n", port_name(port));
Daniel Vetter85234cd2012-07-02 13:27:29 +0200917
Jesse Barnes22f9fe52013-04-02 10:03:55 -0700918 return false;
Daniel Vetter85234cd2012-07-02 13:27:29 +0200919}
920
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300921static uint32_t intel_ddi_get_crtc_pll(struct drm_i915_private *dev_priv,
922 enum pipe pipe)
923{
924 uint32_t temp, ret;
Damien Lespiaua42f7042013-03-25 15:16:14 +0000925 enum port port = I915_MAX_PORTS;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200926 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
927 pipe);
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300928 int i;
929
Paulo Zanoniad80a812012-10-24 16:06:19 -0200930 if (cpu_transcoder == TRANSCODER_EDP) {
931 port = PORT_A;
932 } else {
933 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
934 temp &= TRANS_DDI_PORT_MASK;
935
936 for (i = PORT_B; i <= PORT_E; i++)
937 if (temp == TRANS_DDI_SELECT_PORT(i))
938 port = i;
939 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300940
Damien Lespiaua42f7042013-03-25 15:16:14 +0000941 if (port == I915_MAX_PORTS) {
942 WARN(1, "Pipe %c enabled on an unknown port\n",
943 pipe_name(pipe));
944 ret = PORT_CLK_SEL_NONE;
945 } else {
946 ret = I915_READ(PORT_CLK_SEL(port));
947 DRM_DEBUG_KMS("Pipe %c connected to port %c using clock "
948 "0x%08x\n", pipe_name(pipe), port_name(port),
949 ret);
950 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300951
952 return ret;
953}
954
955void intel_ddi_setup_hw_pll_state(struct drm_device *dev)
956{
957 struct drm_i915_private *dev_priv = dev->dev_private;
958 enum pipe pipe;
959 struct intel_crtc *intel_crtc;
960
961 for_each_pipe(pipe) {
962 intel_crtc =
963 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
964
965 if (!intel_crtc->active)
966 continue;
967
968 intel_crtc->ddi_pll_sel = intel_ddi_get_crtc_pll(dev_priv,
969 pipe);
970
971 switch (intel_crtc->ddi_pll_sel) {
972 case PORT_CLK_SEL_SPLL:
973 dev_priv->ddi_plls.spll_refcount++;
974 break;
975 case PORT_CLK_SEL_WRPLL1:
976 dev_priv->ddi_plls.wrpll1_refcount++;
977 break;
978 case PORT_CLK_SEL_WRPLL2:
979 dev_priv->ddi_plls.wrpll2_refcount++;
980 break;
981 }
982 }
983}
984
Paulo Zanonifc914632012-10-05 12:05:54 -0300985void intel_ddi_enable_pipe_clock(struct intel_crtc *intel_crtc)
986{
987 struct drm_crtc *crtc = &intel_crtc->base;
988 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
989 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
990 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Daniel Vetter3b117c82013-04-17 20:15:07 +0200991 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanonifc914632012-10-05 12:05:54 -0300992
Paulo Zanonibb523fc2012-10-23 18:29:56 -0200993 if (cpu_transcoder != TRANSCODER_EDP)
994 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
995 TRANS_CLK_SEL_PORT(port));
Paulo Zanonifc914632012-10-05 12:05:54 -0300996}
997
998void intel_ddi_disable_pipe_clock(struct intel_crtc *intel_crtc)
999{
1000 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Daniel Vetter3b117c82013-04-17 20:15:07 +02001001 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanonifc914632012-10-05 12:05:54 -03001002
Paulo Zanonibb523fc2012-10-23 18:29:56 -02001003 if (cpu_transcoder != TRANSCODER_EDP)
1004 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1005 TRANS_CLK_SEL_DISABLED);
Paulo Zanonifc914632012-10-05 12:05:54 -03001006}
1007
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001008static void intel_ddi_pre_enable(struct intel_encoder *intel_encoder)
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001009{
Paulo Zanonic19b0662012-10-15 15:51:41 -03001010 struct drm_encoder *encoder = &intel_encoder->base;
1011 struct drm_crtc *crtc = encoder->crtc;
1012 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1014 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001015 int type = intel_encoder->type;
1016
1017 if (type == INTEL_OUTPUT_EDP) {
1018 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1019 ironlake_edp_panel_vdd_on(intel_dp);
1020 ironlake_edp_panel_on(intel_dp);
1021 ironlake_edp_panel_vdd_off(intel_dp, true);
1022 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001023
1024 WARN_ON(intel_crtc->ddi_pll_sel == PORT_CLK_SEL_NONE);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001025 I915_WRITE(PORT_CLK_SEL(port), intel_crtc->ddi_pll_sel);
Paulo Zanonic19b0662012-10-15 15:51:41 -03001026
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001027 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
Paulo Zanonic19b0662012-10-15 15:51:41 -03001028 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1029
1030 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1031 intel_dp_start_link_train(intel_dp);
1032 intel_dp_complete_link_train(intel_dp);
Imre Deak3ab9c632013-05-03 12:57:41 +03001033 if (port != PORT_A)
1034 intel_dp_stop_link_train(intel_dp);
Paulo Zanonic19b0662012-10-15 15:51:41 -03001035 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001036}
1037
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001038static void intel_ddi_post_disable(struct intel_encoder *intel_encoder)
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001039{
1040 struct drm_encoder *encoder = &intel_encoder->base;
1041 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
1042 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001043 int type = intel_encoder->type;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001044 uint32_t val;
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001045 bool wait = false;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001046
1047 val = I915_READ(DDI_BUF_CTL(port));
1048 if (val & DDI_BUF_CTL_ENABLE) {
1049 val &= ~DDI_BUF_CTL_ENABLE;
1050 I915_WRITE(DDI_BUF_CTL(port), val);
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001051 wait = true;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001052 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001053
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001054 val = I915_READ(DP_TP_CTL(port));
1055 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1056 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
1057 I915_WRITE(DP_TP_CTL(port), val);
1058
1059 if (wait)
1060 intel_wait_ddi_buf_idle(dev_priv, port);
1061
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001062 if (type == INTEL_OUTPUT_EDP) {
1063 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1064 ironlake_edp_panel_vdd_on(intel_dp);
1065 ironlake_edp_panel_off(intel_dp);
1066 }
1067
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001068 I915_WRITE(PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
1069}
1070
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001071static void intel_enable_ddi(struct intel_encoder *intel_encoder)
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001072{
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001073 struct drm_encoder *encoder = &intel_encoder->base;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001074 struct drm_crtc *crtc = encoder->crtc;
1075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1076 int pipe = intel_crtc->pipe;
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001077 struct drm_device *dev = encoder->dev;
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001078 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001079 enum port port = intel_ddi_get_encoder_port(intel_encoder);
1080 int type = intel_encoder->type;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001081 uint32_t tmp;
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001082
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001083 if (type == INTEL_OUTPUT_HDMI) {
Damien Lespiau876a8cd2012-12-11 18:48:30 +00001084 struct intel_digital_port *intel_dig_port =
1085 enc_to_dig_port(encoder);
1086
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001087 /* In HDMI/DVI mode, the port width, and swing/emphasis values
1088 * are ignored so nothing special needs to be done besides
1089 * enabling the port.
1090 */
Damien Lespiau876a8cd2012-12-11 18:48:30 +00001091 I915_WRITE(DDI_BUF_CTL(port),
Stéphane Marchesinbcf53de2013-07-12 13:54:41 -07001092 intel_dig_port->saved_port_bits |
1093 DDI_BUF_CTL_ENABLE);
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001094 } else if (type == INTEL_OUTPUT_EDP) {
1095 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1096
Imre Deak3ab9c632013-05-03 12:57:41 +03001097 if (port == PORT_A)
1098 intel_dp_stop_link_train(intel_dp);
1099
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001100 ironlake_edp_backlight_on(intel_dp);
Rodrigo Vivi49065572013-07-11 18:45:05 -03001101 intel_edp_psr_enable(intel_dp);
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001102 }
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001103
Paulo Zanonic77bf562013-05-03 12:15:40 -03001104 if (intel_crtc->eld_vld && type != INTEL_OUTPUT_EDP) {
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001105 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
1106 tmp |= ((AUDIO_OUTPUT_ENABLE_A | AUDIO_ELD_VALID_A) << (pipe * 4));
1107 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
1108 }
Daniel Vetter5ab432e2012-06-30 08:59:56 +02001109}
1110
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001111static void intel_disable_ddi(struct intel_encoder *intel_encoder)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02001112{
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001113 struct drm_encoder *encoder = &intel_encoder->base;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001114 struct drm_crtc *crtc = encoder->crtc;
1115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1116 int pipe = intel_crtc->pipe;
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001117 int type = intel_encoder->type;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001118 struct drm_device *dev = encoder->dev;
1119 struct drm_i915_private *dev_priv = dev->dev_private;
1120 uint32_t tmp;
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001121
Paulo Zanonic77bf562013-05-03 12:15:40 -03001122 if (intel_crtc->eld_vld && type != INTEL_OUTPUT_EDP) {
1123 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
1124 tmp &= ~((AUDIO_OUTPUT_ENABLE_A | AUDIO_ELD_VALID_A) <<
1125 (pipe * 4));
1126 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
1127 }
Paulo Zanoni2831d8422013-03-06 20:03:09 -03001128
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001129 if (type == INTEL_OUTPUT_EDP) {
1130 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1131
Rodrigo Vivi49065572013-07-11 18:45:05 -03001132 intel_edp_psr_disable(intel_dp);
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001133 ironlake_edp_backlight_off(intel_dp);
1134 }
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001135}
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001136
Paulo Zanonib8fc2f62012-10-23 18:30:05 -02001137int intel_ddi_get_cdclk_freq(struct drm_i915_private *dev_priv)
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001138{
Paulo Zanonia4006642013-08-06 18:57:11 -03001139 uint32_t lcpll = I915_READ(LCPLL_CTL);
1140
1141 if (lcpll & LCPLL_CD_SOURCE_FCLK)
1142 return 800000;
1143 else if (I915_READ(HSW_FUSE_STRAP) & HSW_CDCLK_LIMIT)
Paulo Zanonib2b877f2013-05-03 17:23:42 -03001144 return 450000;
Paulo Zanonia4006642013-08-06 18:57:11 -03001145 else if ((lcpll & LCPLL_CLK_FREQ_MASK) == LCPLL_CLK_FREQ_450)
Paulo Zanonib2b877f2013-05-03 17:23:42 -03001146 return 450000;
Paulo Zanonid567b072012-11-20 13:27:43 -02001147 else if (IS_ULT(dev_priv->dev))
Paulo Zanonib2b877f2013-05-03 17:23:42 -03001148 return 337500;
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001149 else
Paulo Zanonib2b877f2013-05-03 17:23:42 -03001150 return 540000;
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001151}
1152
1153void intel_ddi_pll_init(struct drm_device *dev)
1154{
1155 struct drm_i915_private *dev_priv = dev->dev_private;
1156 uint32_t val = I915_READ(LCPLL_CTL);
1157
1158 /* The LCPLL register should be turned on by the BIOS. For now let's
1159 * just check its state and print errors in case something is wrong.
1160 * Don't even try to turn it on.
1161 */
1162
Paulo Zanonib2b877f2013-05-03 17:23:42 -03001163 DRM_DEBUG_KMS("CDCLK running at %dKHz\n",
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001164 intel_ddi_get_cdclk_freq(dev_priv));
1165
1166 if (val & LCPLL_CD_SOURCE_FCLK)
1167 DRM_ERROR("CDCLK source is not LCPLL\n");
1168
1169 if (val & LCPLL_PLL_DISABLE)
1170 DRM_ERROR("LCPLL is disabled\n");
1171}
Paulo Zanonic19b0662012-10-15 15:51:41 -03001172
1173void intel_ddi_prepare_link_retrain(struct drm_encoder *encoder)
1174{
Paulo Zanoni174edf12012-10-26 19:05:50 -02001175 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
1176 struct intel_dp *intel_dp = &intel_dig_port->dp;
Paulo Zanonic19b0662012-10-15 15:51:41 -03001177 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
Paulo Zanoni174edf12012-10-26 19:05:50 -02001178 enum port port = intel_dig_port->port;
Paulo Zanonic19b0662012-10-15 15:51:41 -03001179 uint32_t val;
Syam Sidhardhanf3e227d2013-02-25 04:05:38 +05301180 bool wait = false;
Paulo Zanonic19b0662012-10-15 15:51:41 -03001181
1182 if (I915_READ(DP_TP_CTL(port)) & DP_TP_CTL_ENABLE) {
1183 val = I915_READ(DDI_BUF_CTL(port));
1184 if (val & DDI_BUF_CTL_ENABLE) {
1185 val &= ~DDI_BUF_CTL_ENABLE;
1186 I915_WRITE(DDI_BUF_CTL(port), val);
1187 wait = true;
1188 }
1189
1190 val = I915_READ(DP_TP_CTL(port));
1191 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1192 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
1193 I915_WRITE(DP_TP_CTL(port), val);
1194 POSTING_READ(DP_TP_CTL(port));
1195
1196 if (wait)
1197 intel_wait_ddi_buf_idle(dev_priv, port);
1198 }
1199
1200 val = DP_TP_CTL_ENABLE | DP_TP_CTL_MODE_SST |
1201 DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
Jani Nikula6aba5b62013-10-04 15:08:10 +03001202 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
Paulo Zanonic19b0662012-10-15 15:51:41 -03001203 val |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
1204 I915_WRITE(DP_TP_CTL(port), val);
1205 POSTING_READ(DP_TP_CTL(port));
1206
1207 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
1208 I915_WRITE(DDI_BUF_CTL(port), intel_dp->DP);
1209 POSTING_READ(DDI_BUF_CTL(port));
1210
1211 udelay(600);
1212}
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001213
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02001214void intel_ddi_fdi_disable(struct drm_crtc *crtc)
1215{
1216 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1217 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
1218 uint32_t val;
1219
1220 intel_ddi_post_disable(intel_encoder);
1221
1222 val = I915_READ(_FDI_RXA_CTL);
1223 val &= ~FDI_RX_ENABLE;
1224 I915_WRITE(_FDI_RXA_CTL, val);
1225
1226 val = I915_READ(_FDI_RXA_MISC);
1227 val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1228 val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
1229 I915_WRITE(_FDI_RXA_MISC, val);
1230
1231 val = I915_READ(_FDI_RXA_CTL);
1232 val &= ~FDI_PCDCLK;
1233 I915_WRITE(_FDI_RXA_CTL, val);
1234
1235 val = I915_READ(_FDI_RXA_CTL);
1236 val &= ~FDI_RX_PLL_ENABLE;
1237 I915_WRITE(_FDI_RXA_CTL, val);
1238}
1239
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001240static void intel_ddi_hot_plug(struct intel_encoder *intel_encoder)
1241{
1242 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
1243 int type = intel_encoder->type;
1244
1245 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP)
1246 intel_dp_check_link_status(intel_dp);
1247}
1248
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001249static void intel_ddi_get_config(struct intel_encoder *encoder,
1250 struct intel_crtc_config *pipe_config)
1251{
1252 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
1253 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
1254 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1255 u32 temp, flags = 0;
1256
1257 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1258 if (temp & TRANS_DDI_PHSYNC)
1259 flags |= DRM_MODE_FLAG_PHSYNC;
1260 else
1261 flags |= DRM_MODE_FLAG_NHSYNC;
1262 if (temp & TRANS_DDI_PVSYNC)
1263 flags |= DRM_MODE_FLAG_PVSYNC;
1264 else
1265 flags |= DRM_MODE_FLAG_NVSYNC;
1266
1267 pipe_config->adjusted_mode.flags |= flags;
Ville Syrjälä42571ae2013-09-06 23:29:00 +03001268
1269 switch (temp & TRANS_DDI_BPC_MASK) {
1270 case TRANS_DDI_BPC_6:
1271 pipe_config->pipe_bpp = 18;
1272 break;
1273 case TRANS_DDI_BPC_8:
1274 pipe_config->pipe_bpp = 24;
1275 break;
1276 case TRANS_DDI_BPC_10:
1277 pipe_config->pipe_bpp = 30;
1278 break;
1279 case TRANS_DDI_BPC_12:
1280 pipe_config->pipe_bpp = 36;
1281 break;
1282 default:
1283 break;
1284 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03001285
1286 switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
1287 case TRANS_DDI_MODE_SELECT_HDMI:
1288 case TRANS_DDI_MODE_SELECT_DVI:
1289 case TRANS_DDI_MODE_SELECT_FDI:
1290 break;
1291 case TRANS_DDI_MODE_SELECT_DP_SST:
1292 case TRANS_DDI_MODE_SELECT_DP_MST:
1293 pipe_config->has_dp_encoder = true;
1294 intel_dp_get_m_n(intel_crtc, pipe_config);
1295 break;
1296 default:
1297 break;
1298 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001299}
1300
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001301static void intel_ddi_destroy(struct drm_encoder *encoder)
1302{
1303 /* HDMI has nothing special to destroy, so we can go with this. */
1304 intel_dp_encoder_destroy(encoder);
1305}
1306
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001307static bool intel_ddi_compute_config(struct intel_encoder *encoder,
1308 struct intel_crtc_config *pipe_config)
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001309{
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001310 int type = encoder->type;
Daniel Vettereccb1402013-05-22 00:50:22 +02001311 int port = intel_ddi_get_encoder_port(encoder);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001312
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001313 WARN(type == INTEL_OUTPUT_UNKNOWN, "compute_config() on unknown output!\n");
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001314
Daniel Vettereccb1402013-05-22 00:50:22 +02001315 if (port == PORT_A)
1316 pipe_config->cpu_transcoder = TRANSCODER_EDP;
1317
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001318 if (type == INTEL_OUTPUT_HDMI)
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001319 return intel_hdmi_compute_config(encoder, pipe_config);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001320 else
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001321 return intel_dp_compute_config(encoder, pipe_config);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001322}
1323
1324static const struct drm_encoder_funcs intel_ddi_funcs = {
1325 .destroy = intel_ddi_destroy,
1326};
1327
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001328void intel_ddi_init(struct drm_device *dev, enum port port)
1329{
Damien Lespiau876a8cd2012-12-11 18:48:30 +00001330 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001331 struct intel_digital_port *intel_dig_port;
1332 struct intel_encoder *intel_encoder;
1333 struct drm_encoder *encoder;
1334 struct intel_connector *hdmi_connector = NULL;
1335 struct intel_connector *dp_connector = NULL;
1336
1337 intel_dig_port = kzalloc(sizeof(struct intel_digital_port), GFP_KERNEL);
1338 if (!intel_dig_port)
1339 return;
1340
1341 dp_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1342 if (!dp_connector) {
1343 kfree(intel_dig_port);
1344 return;
1345 }
1346
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001347 intel_encoder = &intel_dig_port->base;
1348 encoder = &intel_encoder->base;
1349
1350 drm_encoder_init(dev, encoder, &intel_ddi_funcs,
1351 DRM_MODE_ENCODER_TMDS);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001352
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001353 intel_encoder->compute_config = intel_ddi_compute_config;
Daniel Vetterc7d8be32013-07-21 21:37:07 +02001354 intel_encoder->mode_set = intel_ddi_mode_set;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001355 intel_encoder->enable = intel_enable_ddi;
1356 intel_encoder->pre_enable = intel_ddi_pre_enable;
1357 intel_encoder->disable = intel_disable_ddi;
1358 intel_encoder->post_disable = intel_ddi_post_disable;
1359 intel_encoder->get_hw_state = intel_ddi_get_hw_state;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001360 intel_encoder->get_config = intel_ddi_get_config;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001361
1362 intel_dig_port->port = port;
Stéphane Marchesinbcf53de2013-07-12 13:54:41 -07001363 intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
1364 (DDI_BUF_PORT_REVERSAL |
1365 DDI_A_4_LANES);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001366 intel_dig_port->dp.output_reg = DDI_BUF_CTL(port);
1367
1368 intel_encoder->type = INTEL_OUTPUT_UNKNOWN;
1369 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
1370 intel_encoder->cloneable = false;
1371 intel_encoder->hot_plug = intel_ddi_hot_plug;
1372
Paulo Zanonib2f246a2013-06-12 17:27:26 -03001373 if (!intel_dp_init_connector(intel_dig_port, dp_connector)) {
Paulo Zanoni15b1d172013-06-12 17:27:27 -03001374 drm_encoder_cleanup(encoder);
1375 kfree(intel_dig_port);
Paulo Zanonib2f246a2013-06-12 17:27:26 -03001376 kfree(dp_connector);
Paulo Zanoni16c25532013-06-12 17:27:25 -03001377 return;
Paulo Zanonib2f246a2013-06-12 17:27:26 -03001378 }
Daniel Vetter21a8e6a2013-04-10 23:28:35 +02001379
1380 if (intel_encoder->type != INTEL_OUTPUT_EDP) {
1381 hdmi_connector = kzalloc(sizeof(struct intel_connector),
1382 GFP_KERNEL);
1383 if (!hdmi_connector) {
1384 return;
1385 }
1386
1387 intel_dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
1388 intel_hdmi_init_connector(intel_dig_port, hdmi_connector);
1389 }
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001390}