xref: /dflybsd-src/sys/dev/drm/i915/intel_crt.c (revision 352ff8bd644eb2f17a1b01fa757342a01f012051)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2006-2007 Intel Corporation
3e3adcf8fSFrançois Tigeot  *
4e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10e3adcf8fSFrançois Tigeot  *
11e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13e3adcf8fSFrançois Tigeot  * Software.
14e3adcf8fSFrançois Tigeot  *
15e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e3adcf8fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21e3adcf8fSFrançois Tigeot  * DEALINGS IN THE SOFTWARE.
22e3adcf8fSFrançois Tigeot  *
23e3adcf8fSFrançois Tigeot  * Authors:
24e3adcf8fSFrançois Tigeot  *	Eric Anholt <eric@anholt.net>
25e3adcf8fSFrançois Tigeot  */
26e3adcf8fSFrançois Tigeot 
275d0b1887SFrançois Tigeot #include <linux/dmi.h>
285d0b1887SFrançois Tigeot #include <linux/i2c.h>
2918e26a6dSFrançois Tigeot #include <drm/drmP.h>
302c9916cdSFrançois Tigeot #include <drm/drm_atomic_helper.h>
3118e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
3218e26a6dSFrançois Tigeot #include <drm/drm_crtc_helper.h>
3318e26a6dSFrançois Tigeot #include <drm/drm_edid.h>
3418e26a6dSFrançois Tigeot #include "intel_drv.h"
355c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
36e3adcf8fSFrançois Tigeot #include "i915_drv.h"
37e3adcf8fSFrançois Tigeot 
38e3adcf8fSFrançois Tigeot /* Here's the desired hotplug mode */
39e3adcf8fSFrançois Tigeot #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 |		\
40e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_WARMUP_10MS |		\
41e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_SAMPLE_4S |			\
42e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_VOLTAGE_50 |		\
43e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_VOLREF_325MV |		\
44e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_ENABLE)
45e3adcf8fSFrançois Tigeot 
46e3adcf8fSFrançois Tigeot struct intel_crt {
47e3adcf8fSFrançois Tigeot 	struct intel_encoder base;
48e9243325SFrançois Tigeot 	/* DPMS state is stored in the connector, which we need in the
49e9243325SFrançois Tigeot 	 * encoder's enable/disable callbacks */
50e9243325SFrançois Tigeot 	struct intel_connector *connector;
51e3adcf8fSFrançois Tigeot 	bool force_hotplug_required;
52e9243325SFrançois Tigeot 	u32 adpa_reg;
53e3adcf8fSFrançois Tigeot };
54e3adcf8fSFrançois Tigeot 
5519df918dSFrançois Tigeot static struct intel_crt *intel_encoder_to_crt(struct intel_encoder *encoder)
56e3adcf8fSFrançois Tigeot {
5719df918dSFrançois Tigeot 	return container_of(encoder, struct intel_crt, base);
5819df918dSFrançois Tigeot }
5919df918dSFrançois Tigeot 
609edbd4a0SFrançois Tigeot static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
619edbd4a0SFrançois Tigeot {
629edbd4a0SFrançois Tigeot 	return intel_encoder_to_crt(intel_attached_encoder(connector));
639edbd4a0SFrançois Tigeot }
649edbd4a0SFrançois Tigeot 
6519df918dSFrançois Tigeot static bool intel_crt_get_hw_state(struct intel_encoder *encoder,
6619df918dSFrançois Tigeot 				   enum i915_pipe *pipe)
6719df918dSFrançois Tigeot {
6819df918dSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
69e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
7019df918dSFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
71ba55f2f5SFrançois Tigeot 	enum intel_display_power_domain power_domain;
7219df918dSFrançois Tigeot 	u32 tmp;
73e3adcf8fSFrançois Tigeot 
74ba55f2f5SFrançois Tigeot 	power_domain = intel_display_port_power_domain(encoder);
752c9916cdSFrançois Tigeot 	if (!intel_display_power_is_enabled(dev_priv, power_domain))
76ba55f2f5SFrançois Tigeot 		return false;
77ba55f2f5SFrançois Tigeot 
7819df918dSFrançois Tigeot 	tmp = I915_READ(crt->adpa_reg);
7919df918dSFrançois Tigeot 
8019df918dSFrançois Tigeot 	if (!(tmp & ADPA_DAC_ENABLE))
8119df918dSFrançois Tigeot 		return false;
8219df918dSFrançois Tigeot 
8319df918dSFrançois Tigeot 	if (HAS_PCH_CPT(dev))
8419df918dSFrançois Tigeot 		*pipe = PORT_TO_PIPE_CPT(tmp);
85e3adcf8fSFrançois Tigeot 	else
8619df918dSFrançois Tigeot 		*pipe = PORT_TO_PIPE(tmp);
87e3adcf8fSFrançois Tigeot 
8819df918dSFrançois Tigeot 	return true;
8919df918dSFrançois Tigeot }
9019df918dSFrançois Tigeot 
919edbd4a0SFrançois Tigeot static unsigned int intel_crt_get_flags(struct intel_encoder *encoder)
925d0b1887SFrançois Tigeot {
935d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
945d0b1887SFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
955d0b1887SFrançois Tigeot 	u32 tmp, flags = 0;
965d0b1887SFrançois Tigeot 
975d0b1887SFrançois Tigeot 	tmp = I915_READ(crt->adpa_reg);
985d0b1887SFrançois Tigeot 
995d0b1887SFrançois Tigeot 	if (tmp & ADPA_HSYNC_ACTIVE_HIGH)
1005d0b1887SFrançois Tigeot 		flags |= DRM_MODE_FLAG_PHSYNC;
1015d0b1887SFrançois Tigeot 	else
1025d0b1887SFrançois Tigeot 		flags |= DRM_MODE_FLAG_NHSYNC;
1035d0b1887SFrançois Tigeot 
1045d0b1887SFrançois Tigeot 	if (tmp & ADPA_VSYNC_ACTIVE_HIGH)
1055d0b1887SFrançois Tigeot 		flags |= DRM_MODE_FLAG_PVSYNC;
1065d0b1887SFrançois Tigeot 	else
1075d0b1887SFrançois Tigeot 		flags |= DRM_MODE_FLAG_NVSYNC;
1085d0b1887SFrançois Tigeot 
1099edbd4a0SFrançois Tigeot 	return flags;
1109edbd4a0SFrançois Tigeot }
1119edbd4a0SFrançois Tigeot 
1129edbd4a0SFrançois Tigeot static void intel_crt_get_config(struct intel_encoder *encoder,
1132c9916cdSFrançois Tigeot 				 struct intel_crtc_state *pipe_config)
1149edbd4a0SFrançois Tigeot {
1159edbd4a0SFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
1169edbd4a0SFrançois Tigeot 	int dotclock;
1179edbd4a0SFrançois Tigeot 
1182c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.flags |= intel_crt_get_flags(encoder);
1199edbd4a0SFrançois Tigeot 
1209edbd4a0SFrançois Tigeot 	dotclock = pipe_config->port_clock;
1219edbd4a0SFrançois Tigeot 
1229edbd4a0SFrançois Tigeot 	if (HAS_PCH_SPLIT(dev))
1239edbd4a0SFrançois Tigeot 		ironlake_check_encoder_dotclock(pipe_config, dotclock);
1249edbd4a0SFrançois Tigeot 
1252c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.crtc_clock = dotclock;
1269edbd4a0SFrançois Tigeot }
1279edbd4a0SFrançois Tigeot 
1289edbd4a0SFrançois Tigeot static void hsw_crt_get_config(struct intel_encoder *encoder,
1292c9916cdSFrançois Tigeot 			       struct intel_crtc_state *pipe_config)
1309edbd4a0SFrançois Tigeot {
1319edbd4a0SFrançois Tigeot 	intel_ddi_get_config(encoder, pipe_config);
1329edbd4a0SFrançois Tigeot 
1332c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.flags &= ~(DRM_MODE_FLAG_PHSYNC |
1349edbd4a0SFrançois Tigeot 					      DRM_MODE_FLAG_NHSYNC |
1359edbd4a0SFrançois Tigeot 					      DRM_MODE_FLAG_PVSYNC |
1369edbd4a0SFrançois Tigeot 					      DRM_MODE_FLAG_NVSYNC);
1372c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.flags |= intel_crt_get_flags(encoder);
1385d0b1887SFrançois Tigeot }
1395d0b1887SFrançois Tigeot 
14019df918dSFrançois Tigeot /* Note: The caller is required to filter out dpms modes not supported by the
14119df918dSFrançois Tigeot  * platform. */
14219df918dSFrançois Tigeot static void intel_crt_set_dpms(struct intel_encoder *encoder, int mode)
14319df918dSFrançois Tigeot {
14419df918dSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
14519df918dSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
14619df918dSFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
147ba55f2f5SFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
148*352ff8bdSFrançois Tigeot 	const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
149ba55f2f5SFrançois Tigeot 	u32 adpa;
15019df918dSFrançois Tigeot 
151ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5)
152ba55f2f5SFrançois Tigeot 		adpa = ADPA_HOTPLUG_BITS;
153ba55f2f5SFrançois Tigeot 	else
154ba55f2f5SFrançois Tigeot 		adpa = 0;
155ba55f2f5SFrançois Tigeot 
156ba55f2f5SFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
157ba55f2f5SFrançois Tigeot 		adpa |= ADPA_HSYNC_ACTIVE_HIGH;
158ba55f2f5SFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
159ba55f2f5SFrançois Tigeot 		adpa |= ADPA_VSYNC_ACTIVE_HIGH;
160ba55f2f5SFrançois Tigeot 
161ba55f2f5SFrançois Tigeot 	/* For CPT allow 3 pipe config, for others just use A or B */
162ba55f2f5SFrançois Tigeot 	if (HAS_PCH_LPT(dev))
163ba55f2f5SFrançois Tigeot 		; /* Those bits don't exist here */
164ba55f2f5SFrançois Tigeot 	else if (HAS_PCH_CPT(dev))
165ba55f2f5SFrançois Tigeot 		adpa |= PORT_TRANS_SEL_CPT(crtc->pipe);
166ba55f2f5SFrançois Tigeot 	else if (crtc->pipe == 0)
167ba55f2f5SFrançois Tigeot 		adpa |= ADPA_PIPE_A_SELECT;
168ba55f2f5SFrançois Tigeot 	else
169ba55f2f5SFrançois Tigeot 		adpa |= ADPA_PIPE_B_SELECT;
170ba55f2f5SFrançois Tigeot 
171ba55f2f5SFrançois Tigeot 	if (!HAS_PCH_SPLIT(dev))
172ba55f2f5SFrançois Tigeot 		I915_WRITE(BCLRPAT(crtc->pipe), 0);
173e3adcf8fSFrançois Tigeot 
174e3adcf8fSFrançois Tigeot 	switch (mode) {
175e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_ON:
176ba55f2f5SFrançois Tigeot 		adpa |= ADPA_DAC_ENABLE;
177e3adcf8fSFrançois Tigeot 		break;
178e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_STANDBY:
179ba55f2f5SFrançois Tigeot 		adpa |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
180e3adcf8fSFrançois Tigeot 		break;
181e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_SUSPEND:
182ba55f2f5SFrançois Tigeot 		adpa |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
183e3adcf8fSFrançois Tigeot 		break;
184e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_OFF:
185ba55f2f5SFrançois Tigeot 		adpa |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
186e3adcf8fSFrançois Tigeot 		break;
187e3adcf8fSFrançois Tigeot 	}
188e3adcf8fSFrançois Tigeot 
189ba55f2f5SFrançois Tigeot 	I915_WRITE(crt->adpa_reg, adpa);
19019df918dSFrançois Tigeot }
19119df918dSFrançois Tigeot 
19219df918dSFrançois Tigeot static void intel_disable_crt(struct intel_encoder *encoder)
19319df918dSFrançois Tigeot {
19419df918dSFrançois Tigeot 	intel_crt_set_dpms(encoder, DRM_MODE_DPMS_OFF);
19519df918dSFrançois Tigeot }
19619df918dSFrançois Tigeot 
19719c468b4SFrançois Tigeot static void pch_disable_crt(struct intel_encoder *encoder)
19819c468b4SFrançois Tigeot {
19919c468b4SFrançois Tigeot }
20019c468b4SFrançois Tigeot 
20119c468b4SFrançois Tigeot static void pch_post_disable_crt(struct intel_encoder *encoder)
20219c468b4SFrançois Tigeot {
20319c468b4SFrançois Tigeot 	intel_disable_crt(encoder);
20419c468b4SFrançois Tigeot }
20524edb884SFrançois Tigeot 
20619df918dSFrançois Tigeot static void intel_enable_crt(struct intel_encoder *encoder)
20719df918dSFrançois Tigeot {
20819df918dSFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
20919df918dSFrançois Tigeot 
21019df918dSFrançois Tigeot 	intel_crt_set_dpms(encoder, crt->connector->base.dpms);
21119df918dSFrançois Tigeot }
21219df918dSFrançois Tigeot 
2139edbd4a0SFrançois Tigeot static enum drm_mode_status
2149edbd4a0SFrançois Tigeot intel_crt_mode_valid(struct drm_connector *connector,
215e3adcf8fSFrançois Tigeot 		     struct drm_display_mode *mode)
216e3adcf8fSFrançois Tigeot {
217e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
218e3adcf8fSFrançois Tigeot 
219e3adcf8fSFrançois Tigeot 	int max_clock = 0;
220e3adcf8fSFrançois Tigeot 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
221e3adcf8fSFrançois Tigeot 		return MODE_NO_DBLESCAN;
222e3adcf8fSFrançois Tigeot 
223e3adcf8fSFrançois Tigeot 	if (mode->clock < 25000)
224e3adcf8fSFrançois Tigeot 		return MODE_CLOCK_LOW;
225e3adcf8fSFrançois Tigeot 
226e3adcf8fSFrançois Tigeot 	if (IS_GEN2(dev))
227e3adcf8fSFrançois Tigeot 		max_clock = 350000;
228e3adcf8fSFrançois Tigeot 	else
229e3adcf8fSFrançois Tigeot 		max_clock = 400000;
230e3adcf8fSFrançois Tigeot 	if (mode->clock > max_clock)
231e3adcf8fSFrançois Tigeot 		return MODE_CLOCK_HIGH;
232e3adcf8fSFrançois Tigeot 
23319df918dSFrançois Tigeot 	/* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
23419df918dSFrançois Tigeot 	if (HAS_PCH_LPT(dev) &&
23519df918dSFrançois Tigeot 	    (ironlake_get_lanes_required(mode->clock, 270000, 24) > 2))
23619df918dSFrançois Tigeot 		return MODE_CLOCK_HIGH;
23719df918dSFrançois Tigeot 
238e3adcf8fSFrançois Tigeot 	return MODE_OK;
239e3adcf8fSFrançois Tigeot }
240e3adcf8fSFrançois Tigeot 
2418e26cdf6SFrançois Tigeot static bool intel_crt_compute_config(struct intel_encoder *encoder,
2422c9916cdSFrançois Tigeot 				     struct intel_crtc_state *pipe_config)
243e3adcf8fSFrançois Tigeot {
2448e26cdf6SFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
2458e26cdf6SFrançois Tigeot 
2468e26cdf6SFrançois Tigeot 	if (HAS_PCH_SPLIT(dev))
2478e26cdf6SFrançois Tigeot 		pipe_config->has_pch_encoder = true;
2488e26cdf6SFrançois Tigeot 
2495d0b1887SFrançois Tigeot 	/* LPT FDI RX only supports 8bpc. */
2505d0b1887SFrançois Tigeot 	if (HAS_PCH_LPT(dev))
2515d0b1887SFrançois Tigeot 		pipe_config->pipe_bpp = 24;
2525d0b1887SFrançois Tigeot 
253ba55f2f5SFrançois Tigeot 	/* FDI must always be 2.7 GHz */
25424edb884SFrançois Tigeot 	if (HAS_DDI(dev)) {
25524edb884SFrançois Tigeot 		pipe_config->ddi_pll_sel = PORT_CLK_SEL_SPLL;
256ba55f2f5SFrançois Tigeot 		pipe_config->port_clock = 135000 * 2;
257*352ff8bdSFrançois Tigeot 
258*352ff8bdSFrançois Tigeot 		pipe_config->dpll_hw_state.wrpll = 0;
259*352ff8bdSFrançois Tigeot 		pipe_config->dpll_hw_state.spll =
260*352ff8bdSFrançois Tigeot 			SPLL_PLL_ENABLE | SPLL_PLL_FREQ_1350MHz | SPLL_PLL_SSC;
26124edb884SFrançois Tigeot 	}
262ba55f2f5SFrançois Tigeot 
263e3adcf8fSFrançois Tigeot 	return true;
264e3adcf8fSFrançois Tigeot }
265e3adcf8fSFrançois Tigeot 
266e3adcf8fSFrançois Tigeot static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
267e3adcf8fSFrançois Tigeot {
268e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
269e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
270e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
271e3adcf8fSFrançois Tigeot 	u32 adpa;
272e3adcf8fSFrançois Tigeot 	bool ret;
273e3adcf8fSFrançois Tigeot 
274e3adcf8fSFrançois Tigeot 	/* The first time through, trigger an explicit detection cycle */
275e3adcf8fSFrançois Tigeot 	if (crt->force_hotplug_required) {
276e3adcf8fSFrançois Tigeot 		bool turn_off_dac = HAS_PCH_SPLIT(dev);
277e3adcf8fSFrançois Tigeot 		u32 save_adpa;
278e3adcf8fSFrançois Tigeot 
279e3adcf8fSFrançois Tigeot 		crt->force_hotplug_required = 0;
280e3adcf8fSFrançois Tigeot 
281a2fdbec6SFrançois Tigeot 		save_adpa = adpa = I915_READ(crt->adpa_reg);
282e3adcf8fSFrançois Tigeot 		DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
283e3adcf8fSFrançois Tigeot 
284e3adcf8fSFrançois Tigeot 		adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
285e3adcf8fSFrançois Tigeot 		if (turn_off_dac)
286e3adcf8fSFrançois Tigeot 			adpa &= ~ADPA_DAC_ENABLE;
287e3adcf8fSFrançois Tigeot 
288a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, adpa);
289e3adcf8fSFrançois Tigeot 
290a2fdbec6SFrançois Tigeot 		if (wait_for((I915_READ(crt->adpa_reg) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
29119df918dSFrançois Tigeot 			     1000))
29219df918dSFrançois Tigeot 			DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
293e3adcf8fSFrançois Tigeot 
294e3adcf8fSFrançois Tigeot 		if (turn_off_dac) {
295a2fdbec6SFrançois Tigeot 			I915_WRITE(crt->adpa_reg, save_adpa);
296a2fdbec6SFrançois Tigeot 			POSTING_READ(crt->adpa_reg);
297e3adcf8fSFrançois Tigeot 		}
298e3adcf8fSFrançois Tigeot 	}
299e3adcf8fSFrançois Tigeot 
300e3adcf8fSFrançois Tigeot 	/* Check the status to see if both blue and green are on now */
301a2fdbec6SFrançois Tigeot 	adpa = I915_READ(crt->adpa_reg);
302e3adcf8fSFrançois Tigeot 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
303e3adcf8fSFrançois Tigeot 		ret = true;
304e3adcf8fSFrançois Tigeot 	else
305e3adcf8fSFrançois Tigeot 		ret = false;
306e3adcf8fSFrançois Tigeot 	DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
307e3adcf8fSFrançois Tigeot 
308e3adcf8fSFrançois Tigeot 	return ret;
309e3adcf8fSFrançois Tigeot }
310e3adcf8fSFrançois Tigeot 
31119df918dSFrançois Tigeot static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
31219df918dSFrançois Tigeot {
31319df918dSFrançois Tigeot 	struct drm_device *dev = connector->dev;
314a2fdbec6SFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
31519df918dSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
31619df918dSFrançois Tigeot 	u32 adpa;
31719df918dSFrançois Tigeot 	bool ret;
31819df918dSFrançois Tigeot 	u32 save_adpa;
31919df918dSFrançois Tigeot 
320a2fdbec6SFrançois Tigeot 	save_adpa = adpa = I915_READ(crt->adpa_reg);
32119df918dSFrançois Tigeot 	DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
32219df918dSFrançois Tigeot 
32319df918dSFrançois Tigeot 	adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
32419df918dSFrançois Tigeot 
325a2fdbec6SFrançois Tigeot 	I915_WRITE(crt->adpa_reg, adpa);
32619df918dSFrançois Tigeot 
327a2fdbec6SFrançois Tigeot 	if (wait_for((I915_READ(crt->adpa_reg) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
32819df918dSFrançois Tigeot 		     1000)) {
32919df918dSFrançois Tigeot 		DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
330a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, save_adpa);
33119df918dSFrançois Tigeot 	}
33219df918dSFrançois Tigeot 
33319df918dSFrançois Tigeot 	/* Check the status to see if both blue and green are on now */
334a2fdbec6SFrançois Tigeot 	adpa = I915_READ(crt->adpa_reg);
33519df918dSFrançois Tigeot 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
33619df918dSFrançois Tigeot 		ret = true;
33719df918dSFrançois Tigeot 	else
33819df918dSFrançois Tigeot 		ret = false;
33919df918dSFrançois Tigeot 
34019df918dSFrançois Tigeot 	DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
34119df918dSFrançois Tigeot 
34219df918dSFrançois Tigeot 	return ret;
34319df918dSFrançois Tigeot }
34419df918dSFrançois Tigeot 
345e3adcf8fSFrançois Tigeot /**
346e3adcf8fSFrançois Tigeot  * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
347e3adcf8fSFrançois Tigeot  *
348e3adcf8fSFrançois Tigeot  * Not for i915G/i915GM
349e3adcf8fSFrançois Tigeot  *
350e3adcf8fSFrançois Tigeot  * \return true if CRT is connected.
351e3adcf8fSFrançois Tigeot  * \return false if CRT is disconnected.
352e3adcf8fSFrançois Tigeot  */
353e3adcf8fSFrançois Tigeot static bool intel_crt_detect_hotplug(struct drm_connector *connector)
354e3adcf8fSFrançois Tigeot {
355e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
356e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
357*352ff8bdSFrançois Tigeot 	u32 stat;
358e3adcf8fSFrançois Tigeot 	bool ret = false;
359e3adcf8fSFrançois Tigeot 	int i, tries = 0;
360e3adcf8fSFrançois Tigeot 
361e3adcf8fSFrançois Tigeot 	if (HAS_PCH_SPLIT(dev))
362e3adcf8fSFrançois Tigeot 		return intel_ironlake_crt_detect_hotplug(connector);
363e3adcf8fSFrançois Tigeot 
36419df918dSFrançois Tigeot 	if (IS_VALLEYVIEW(dev))
36519df918dSFrançois Tigeot 		return valleyview_crt_detect_hotplug(connector);
36619df918dSFrançois Tigeot 
367e3adcf8fSFrançois Tigeot 	/*
368e3adcf8fSFrançois Tigeot 	 * On 4 series desktop, CRT detect sequence need to be done twice
369e3adcf8fSFrançois Tigeot 	 * to get a reliable result.
370e3adcf8fSFrançois Tigeot 	 */
371e3adcf8fSFrançois Tigeot 
372e3adcf8fSFrançois Tigeot 	if (IS_G4X(dev) && !IS_GM45(dev))
373e3adcf8fSFrançois Tigeot 		tries = 2;
374e3adcf8fSFrançois Tigeot 	else
375e3adcf8fSFrançois Tigeot 		tries = 1;
376e3adcf8fSFrançois Tigeot 
377e3adcf8fSFrançois Tigeot 	for (i = 0; i < tries ; i++) {
378e3adcf8fSFrançois Tigeot 		/* turn on the FORCE_DETECT */
379*352ff8bdSFrançois Tigeot 		i915_hotplug_interrupt_update(dev_priv,
380*352ff8bdSFrançois Tigeot 					      CRT_HOTPLUG_FORCE_DETECT,
381*352ff8bdSFrançois Tigeot 					      CRT_HOTPLUG_FORCE_DETECT);
382e3adcf8fSFrançois Tigeot 		/* wait for FORCE_DETECT to go off */
38319df918dSFrançois Tigeot 		if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
38419df918dSFrançois Tigeot 			      CRT_HOTPLUG_FORCE_DETECT) == 0,
38519df918dSFrançois Tigeot 			     1000))
386e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
387e3adcf8fSFrançois Tigeot 	}
388e3adcf8fSFrançois Tigeot 
389e3adcf8fSFrançois Tigeot 	stat = I915_READ(PORT_HOTPLUG_STAT);
390e3adcf8fSFrançois Tigeot 	if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
391e3adcf8fSFrançois Tigeot 		ret = true;
392e3adcf8fSFrançois Tigeot 
393e3adcf8fSFrançois Tigeot 	/* clear the interrupt we just generated, if any */
394e3adcf8fSFrançois Tigeot 	I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
395e3adcf8fSFrançois Tigeot 
396*352ff8bdSFrançois Tigeot 	i915_hotplug_interrupt_update(dev_priv, CRT_HOTPLUG_FORCE_DETECT, 0);
397e3adcf8fSFrançois Tigeot 
398e3adcf8fSFrançois Tigeot 	return ret;
399e3adcf8fSFrançois Tigeot }
400e3adcf8fSFrançois Tigeot 
40119df918dSFrançois Tigeot static struct edid *intel_crt_get_edid(struct drm_connector *connector,
402ba55f2f5SFrançois Tigeot 				struct i2c_adapter *i2c)
40319df918dSFrançois Tigeot {
40419df918dSFrançois Tigeot 	struct edid *edid;
40519df918dSFrançois Tigeot 
40619df918dSFrançois Tigeot 	edid = drm_get_edid(connector, i2c);
40719df918dSFrançois Tigeot 
40819df918dSFrançois Tigeot 	if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
40919df918dSFrançois Tigeot 		DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
41019df918dSFrançois Tigeot 		intel_gmbus_force_bit(i2c, true);
41119df918dSFrançois Tigeot 		edid = drm_get_edid(connector, i2c);
41219df918dSFrançois Tigeot 		intel_gmbus_force_bit(i2c, false);
41319df918dSFrançois Tigeot 	}
41419df918dSFrançois Tigeot 
41519df918dSFrançois Tigeot 	return edid;
41619df918dSFrançois Tigeot }
41719df918dSFrançois Tigeot 
41819df918dSFrançois Tigeot /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
41919df918dSFrançois Tigeot static int intel_crt_ddc_get_modes(struct drm_connector *connector,
420ba55f2f5SFrançois Tigeot 				struct i2c_adapter *adapter)
42119df918dSFrançois Tigeot {
42219df918dSFrançois Tigeot 	struct edid *edid;
42319df918dSFrançois Tigeot 	int ret;
42419df918dSFrançois Tigeot 
42519df918dSFrançois Tigeot 	edid = intel_crt_get_edid(connector, adapter);
42619df918dSFrançois Tigeot 	if (!edid)
42719df918dSFrançois Tigeot 		return 0;
42819df918dSFrançois Tigeot 
42919df918dSFrançois Tigeot 	ret = intel_connector_update_modes(connector, edid);
430158486a6SFrançois Tigeot 	kfree(edid);
43119df918dSFrançois Tigeot 
43219df918dSFrançois Tigeot 	return ret;
43319df918dSFrançois Tigeot }
43419df918dSFrançois Tigeot 
435e3adcf8fSFrançois Tigeot static bool intel_crt_detect_ddc(struct drm_connector *connector)
436e3adcf8fSFrançois Tigeot {
437e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
438e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
439e3adcf8fSFrançois Tigeot 	struct edid *edid;
440ba55f2f5SFrançois Tigeot 	struct i2c_adapter *i2c;
441e3adcf8fSFrançois Tigeot 
44219df918dSFrançois Tigeot 	BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
44319df918dSFrançois Tigeot 
4445d0b1887SFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
44519df918dSFrançois Tigeot 	edid = intel_crt_get_edid(connector, i2c);
44619df918dSFrançois Tigeot 
44719df918dSFrançois Tigeot 	if (edid) {
44819df918dSFrançois Tigeot 		bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
44919df918dSFrançois Tigeot 
450e3adcf8fSFrançois Tigeot 		/*
451e3adcf8fSFrançois Tigeot 		 * This may be a DVI-I connector with a shared DDC
452e3adcf8fSFrançois Tigeot 		 * link between analog and digital outputs, so we
453e3adcf8fSFrançois Tigeot 		 * have to check the EDID input spec of the attached device.
454e3adcf8fSFrançois Tigeot 		 */
455e3adcf8fSFrançois Tigeot 		if (!is_digital) {
456e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
457e3adcf8fSFrançois Tigeot 			return true;
45819df918dSFrançois Tigeot 		}
45919df918dSFrançois Tigeot 
460e3adcf8fSFrançois Tigeot 		DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
46119df918dSFrançois Tigeot 	} else {
46219df918dSFrançois Tigeot 		DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
463e3adcf8fSFrançois Tigeot 	}
46419df918dSFrançois Tigeot 
4655d0b1887SFrançois Tigeot 	kfree(edid);
466e3adcf8fSFrançois Tigeot 
467e3adcf8fSFrançois Tigeot 	return false;
468e3adcf8fSFrançois Tigeot }
469e3adcf8fSFrançois Tigeot 
470e3adcf8fSFrançois Tigeot static enum drm_connector_status
471e3adcf8fSFrançois Tigeot intel_crt_load_detect(struct intel_crt *crt)
472e3adcf8fSFrançois Tigeot {
473e3adcf8fSFrançois Tigeot 	struct drm_device *dev = crt->base.base.dev;
474e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
475e3adcf8fSFrançois Tigeot 	uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
476e3adcf8fSFrançois Tigeot 	uint32_t save_bclrpat;
477e3adcf8fSFrançois Tigeot 	uint32_t save_vtotal;
478e3adcf8fSFrançois Tigeot 	uint32_t vtotal, vactive;
479e3adcf8fSFrançois Tigeot 	uint32_t vsample;
480e3adcf8fSFrançois Tigeot 	uint32_t vblank, vblank_start, vblank_end;
481e3adcf8fSFrançois Tigeot 	uint32_t dsl;
482e3adcf8fSFrançois Tigeot 	uint32_t bclrpat_reg;
483e3adcf8fSFrançois Tigeot 	uint32_t vtotal_reg;
484e3adcf8fSFrançois Tigeot 	uint32_t vblank_reg;
485e3adcf8fSFrançois Tigeot 	uint32_t vsync_reg;
486e3adcf8fSFrançois Tigeot 	uint32_t pipeconf_reg;
487e3adcf8fSFrançois Tigeot 	uint32_t pipe_dsl_reg;
488e3adcf8fSFrançois Tigeot 	uint8_t	st00;
489e3adcf8fSFrançois Tigeot 	enum drm_connector_status status;
490e3adcf8fSFrançois Tigeot 
491e3adcf8fSFrançois Tigeot 	DRM_DEBUG_KMS("starting load-detect on CRT\n");
492e3adcf8fSFrançois Tigeot 
493e3adcf8fSFrançois Tigeot 	bclrpat_reg = BCLRPAT(pipe);
494e3adcf8fSFrançois Tigeot 	vtotal_reg = VTOTAL(pipe);
495e3adcf8fSFrançois Tigeot 	vblank_reg = VBLANK(pipe);
496e3adcf8fSFrançois Tigeot 	vsync_reg = VSYNC(pipe);
497e3adcf8fSFrançois Tigeot 	pipeconf_reg = PIPECONF(pipe);
498e3adcf8fSFrançois Tigeot 	pipe_dsl_reg = PIPEDSL(pipe);
499e3adcf8fSFrançois Tigeot 
500e3adcf8fSFrançois Tigeot 	save_bclrpat = I915_READ(bclrpat_reg);
501e3adcf8fSFrançois Tigeot 	save_vtotal = I915_READ(vtotal_reg);
502e3adcf8fSFrançois Tigeot 	vblank = I915_READ(vblank_reg);
503e3adcf8fSFrançois Tigeot 
504e3adcf8fSFrançois Tigeot 	vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
505e3adcf8fSFrançois Tigeot 	vactive = (save_vtotal & 0x7ff) + 1;
506e3adcf8fSFrançois Tigeot 
507e3adcf8fSFrançois Tigeot 	vblank_start = (vblank & 0xfff) + 1;
508e3adcf8fSFrançois Tigeot 	vblank_end = ((vblank >> 16) & 0xfff) + 1;
509e3adcf8fSFrançois Tigeot 
510e3adcf8fSFrançois Tigeot 	/* Set the border color to purple. */
511e3adcf8fSFrançois Tigeot 	I915_WRITE(bclrpat_reg, 0x500050);
512e3adcf8fSFrançois Tigeot 
513e3adcf8fSFrançois Tigeot 	if (!IS_GEN2(dev)) {
514e3adcf8fSFrançois Tigeot 		uint32_t pipeconf = I915_READ(pipeconf_reg);
515e3adcf8fSFrançois Tigeot 		I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
516e3adcf8fSFrançois Tigeot 		POSTING_READ(pipeconf_reg);
517e3adcf8fSFrançois Tigeot 		/* Wait for next Vblank to substitue
518e3adcf8fSFrançois Tigeot 		 * border color for Color info */
519e3adcf8fSFrançois Tigeot 		intel_wait_for_vblank(dev, pipe);
520e3adcf8fSFrançois Tigeot 		st00 = I915_READ8(VGA_MSR_WRITE);
521e3adcf8fSFrançois Tigeot 		status = ((st00 & (1 << 4)) != 0) ?
522e3adcf8fSFrançois Tigeot 			connector_status_connected :
523e3adcf8fSFrançois Tigeot 			connector_status_disconnected;
524e3adcf8fSFrançois Tigeot 
525e3adcf8fSFrançois Tigeot 		I915_WRITE(pipeconf_reg, pipeconf);
526e3adcf8fSFrançois Tigeot 	} else {
527e3adcf8fSFrançois Tigeot 		bool restore_vblank = false;
528e3adcf8fSFrançois Tigeot 		int count, detect;
529e3adcf8fSFrançois Tigeot 
530e3adcf8fSFrançois Tigeot 		/*
531e3adcf8fSFrançois Tigeot 		* If there isn't any border, add some.
532e3adcf8fSFrançois Tigeot 		* Yes, this will flicker
533e3adcf8fSFrançois Tigeot 		*/
534e3adcf8fSFrançois Tigeot 		if (vblank_start <= vactive && vblank_end >= vtotal) {
535e3adcf8fSFrançois Tigeot 			uint32_t vsync = I915_READ(vsync_reg);
536e3adcf8fSFrançois Tigeot 			uint32_t vsync_start = (vsync & 0xffff) + 1;
537e3adcf8fSFrançois Tigeot 
538e3adcf8fSFrançois Tigeot 			vblank_start = vsync_start;
539e3adcf8fSFrançois Tigeot 			I915_WRITE(vblank_reg,
540e3adcf8fSFrançois Tigeot 				   (vblank_start - 1) |
541e3adcf8fSFrançois Tigeot 				   ((vblank_end - 1) << 16));
542e3adcf8fSFrançois Tigeot 			restore_vblank = true;
543e3adcf8fSFrançois Tigeot 		}
544e3adcf8fSFrançois Tigeot 		/* sample in the vertical border, selecting the larger one */
545e3adcf8fSFrançois Tigeot 		if (vblank_start - vactive >= vtotal - vblank_end)
546e3adcf8fSFrançois Tigeot 			vsample = (vblank_start + vactive) >> 1;
547e3adcf8fSFrançois Tigeot 		else
548e3adcf8fSFrançois Tigeot 			vsample = (vtotal + vblank_end) >> 1;
549e3adcf8fSFrançois Tigeot 
550e3adcf8fSFrançois Tigeot 		/*
551e3adcf8fSFrançois Tigeot 		 * Wait for the border to be displayed
552e3adcf8fSFrançois Tigeot 		 */
553e3adcf8fSFrançois Tigeot 		while (I915_READ(pipe_dsl_reg) >= vactive)
554e3adcf8fSFrançois Tigeot 			;
555e3adcf8fSFrançois Tigeot 		while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
556e3adcf8fSFrançois Tigeot 			;
557e3adcf8fSFrançois Tigeot 		/*
558e3adcf8fSFrançois Tigeot 		 * Watch ST00 for an entire scanline
559e3adcf8fSFrançois Tigeot 		 */
560e3adcf8fSFrançois Tigeot 		detect = 0;
561e3adcf8fSFrançois Tigeot 		count = 0;
562e3adcf8fSFrançois Tigeot 		do {
563e3adcf8fSFrançois Tigeot 			count++;
564e3adcf8fSFrançois Tigeot 			/* Read the ST00 VGA status register */
565e3adcf8fSFrançois Tigeot 			st00 = I915_READ8(VGA_MSR_WRITE);
566e3adcf8fSFrançois Tigeot 			if (st00 & (1 << 4))
567e3adcf8fSFrançois Tigeot 				detect++;
568e3adcf8fSFrançois Tigeot 		} while ((I915_READ(pipe_dsl_reg) == dsl));
569e3adcf8fSFrançois Tigeot 
570e3adcf8fSFrançois Tigeot 		/* restore vblank if necessary */
571e3adcf8fSFrançois Tigeot 		if (restore_vblank)
572e3adcf8fSFrançois Tigeot 			I915_WRITE(vblank_reg, vblank);
573e3adcf8fSFrançois Tigeot 		/*
574e3adcf8fSFrançois Tigeot 		 * If more than 3/4 of the scanline detected a monitor,
575e3adcf8fSFrançois Tigeot 		 * then it is assumed to be present. This works even on i830,
576e3adcf8fSFrançois Tigeot 		 * where there isn't any way to force the border color across
577e3adcf8fSFrançois Tigeot 		 * the screen
578e3adcf8fSFrançois Tigeot 		 */
579e3adcf8fSFrançois Tigeot 		status = detect * 4 > count * 3 ?
580e3adcf8fSFrançois Tigeot 			 connector_status_connected :
581e3adcf8fSFrançois Tigeot 			 connector_status_disconnected;
582e3adcf8fSFrançois Tigeot 	}
583e3adcf8fSFrançois Tigeot 
584e3adcf8fSFrançois Tigeot 	/* Restore previous settings */
585e3adcf8fSFrançois Tigeot 	I915_WRITE(bclrpat_reg, save_bclrpat);
586e3adcf8fSFrançois Tigeot 
587e3adcf8fSFrançois Tigeot 	return status;
588e3adcf8fSFrançois Tigeot }
589e3adcf8fSFrançois Tigeot 
590e3adcf8fSFrançois Tigeot static enum drm_connector_status
591e3adcf8fSFrançois Tigeot intel_crt_detect(struct drm_connector *connector, bool force)
592e3adcf8fSFrançois Tigeot {
593e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
594ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
595e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
596ba55f2f5SFrançois Tigeot 	struct intel_encoder *intel_encoder = &crt->base;
597ba55f2f5SFrançois Tigeot 	enum intel_display_power_domain power_domain;
598e3adcf8fSFrançois Tigeot 	enum drm_connector_status status;
599e3adcf8fSFrançois Tigeot 	struct intel_load_detect_pipe tmp;
600ba55f2f5SFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
601ba55f2f5SFrançois Tigeot 
6029edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
603b4efbf42Szrj 		      connector->base.id, connector->name,
6049edbd4a0SFrançois Tigeot 		      force);
6059edbd4a0SFrançois Tigeot 
606ba55f2f5SFrançois Tigeot 	power_domain = intel_display_port_power_domain(intel_encoder);
607ba55f2f5SFrançois Tigeot 	intel_display_power_get(dev_priv, power_domain);
608ba55f2f5SFrançois Tigeot 
609e3adcf8fSFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev)) {
61019df918dSFrançois Tigeot 		/* We can not rely on the HPD pin always being correctly wired
61119df918dSFrançois Tigeot 		 * up, for example many KVM do not pass it through, and so
61219df918dSFrançois Tigeot 		 * only trust an assertion that the monitor is connected.
61319df918dSFrançois Tigeot 		 */
614e3adcf8fSFrançois Tigeot 		if (intel_crt_detect_hotplug(connector)) {
615e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT detected via hotplug\n");
616ba55f2f5SFrançois Tigeot 			status = connector_status_connected;
617ba55f2f5SFrançois Tigeot 			goto out;
61819df918dSFrançois Tigeot 		} else
619e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT not detected via hotplug\n");
620e3adcf8fSFrançois Tigeot 	}
621e3adcf8fSFrançois Tigeot 
622ba55f2f5SFrançois Tigeot 	if (intel_crt_detect_ddc(connector)) {
623ba55f2f5SFrançois Tigeot 		status = connector_status_connected;
624ba55f2f5SFrançois Tigeot 		goto out;
625ba55f2f5SFrançois Tigeot 	}
626e3adcf8fSFrançois Tigeot 
62719df918dSFrançois Tigeot 	/* Load detection is broken on HPD capable machines. Whoever wants a
62819df918dSFrançois Tigeot 	 * broken monitor (without edid) to work behind a broken kvm (that fails
62919df918dSFrançois Tigeot 	 * to have the right resistors for HP detection) needs to fix this up.
63019df918dSFrançois Tigeot 	 * For now just bail out. */
631477eb7f9SFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev) && !i915.load_detect_test) {
632ba55f2f5SFrançois Tigeot 		status = connector_status_disconnected;
633ba55f2f5SFrançois Tigeot 		goto out;
634ba55f2f5SFrançois Tigeot 	}
63519df918dSFrançois Tigeot 
636ba55f2f5SFrançois Tigeot 	if (!force) {
637ba55f2f5SFrançois Tigeot 		status = connector->status;
638ba55f2f5SFrançois Tigeot 		goto out;
639ba55f2f5SFrançois Tigeot 	}
640e3adcf8fSFrançois Tigeot 
64124edb884SFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
64224edb884SFrançois Tigeot 
643e3adcf8fSFrançois Tigeot 	/* for pre-945g platforms use load detect */
644ba55f2f5SFrançois Tigeot 	if (intel_get_load_detect_pipe(connector, NULL, &tmp, &ctx)) {
645e3adcf8fSFrançois Tigeot 		if (intel_crt_detect_ddc(connector))
646e3adcf8fSFrançois Tigeot 			status = connector_status_connected;
647477eb7f9SFrançois Tigeot 		else if (INTEL_INFO(dev)->gen < 4)
648e3adcf8fSFrançois Tigeot 			status = intel_crt_load_detect(crt);
649477eb7f9SFrançois Tigeot 		else
650477eb7f9SFrançois Tigeot 			status = connector_status_unknown;
651477eb7f9SFrançois Tigeot 		intel_release_load_detect_pipe(connector, &tmp, &ctx);
652e3adcf8fSFrançois Tigeot 	} else
653e3adcf8fSFrançois Tigeot 		status = connector_status_unknown;
654e3adcf8fSFrançois Tigeot 
65524edb884SFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
65624edb884SFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
65724edb884SFrançois Tigeot 
658ba55f2f5SFrançois Tigeot out:
659ba55f2f5SFrançois Tigeot 	intel_display_power_put(dev_priv, power_domain);
660e3adcf8fSFrançois Tigeot 	return status;
661e3adcf8fSFrançois Tigeot }
662e3adcf8fSFrançois Tigeot 
663e3adcf8fSFrançois Tigeot static void intel_crt_destroy(struct drm_connector *connector)
664e3adcf8fSFrançois Tigeot {
665e3adcf8fSFrançois Tigeot 	drm_connector_cleanup(connector);
6665d0b1887SFrançois Tigeot 	kfree(connector);
667e3adcf8fSFrançois Tigeot }
668e3adcf8fSFrançois Tigeot 
669e3adcf8fSFrançois Tigeot static int intel_crt_get_modes(struct drm_connector *connector)
670e3adcf8fSFrançois Tigeot {
671e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
672e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
673ba55f2f5SFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
674ba55f2f5SFrançois Tigeot 	struct intel_encoder *intel_encoder = &crt->base;
675ba55f2f5SFrançois Tigeot 	enum intel_display_power_domain power_domain;
676e3adcf8fSFrançois Tigeot 	int ret;
677ba55f2f5SFrançois Tigeot 	struct i2c_adapter *i2c;
678ba55f2f5SFrançois Tigeot 
679ba55f2f5SFrançois Tigeot 	power_domain = intel_display_port_power_domain(intel_encoder);
680ba55f2f5SFrançois Tigeot 	intel_display_power_get(dev_priv, power_domain);
681e3adcf8fSFrançois Tigeot 
6825d0b1887SFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
68319df918dSFrançois Tigeot 	ret = intel_crt_ddc_get_modes(connector, i2c);
684e3adcf8fSFrançois Tigeot 	if (ret || !IS_G4X(dev))
685ba55f2f5SFrançois Tigeot 		goto out;
686e3adcf8fSFrançois Tigeot 
687e3adcf8fSFrançois Tigeot 	/* Try to probe digital port for output in DVI-I -> VGA mode. */
68819c468b4SFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPB);
689ba55f2f5SFrançois Tigeot 	ret = intel_crt_ddc_get_modes(connector, i2c);
690ba55f2f5SFrançois Tigeot 
691ba55f2f5SFrançois Tigeot out:
692ba55f2f5SFrançois Tigeot 	intel_display_power_put(dev_priv, power_domain);
693ba55f2f5SFrançois Tigeot 
694ba55f2f5SFrançois Tigeot 	return ret;
695e3adcf8fSFrançois Tigeot }
696e3adcf8fSFrançois Tigeot 
697e3adcf8fSFrançois Tigeot static int intel_crt_set_property(struct drm_connector *connector,
698e3adcf8fSFrançois Tigeot 				  struct drm_property *property,
699e3adcf8fSFrançois Tigeot 				  uint64_t value)
700e3adcf8fSFrançois Tigeot {
701e3adcf8fSFrançois Tigeot 	return 0;
702e3adcf8fSFrançois Tigeot }
703e3adcf8fSFrançois Tigeot 
704e3adcf8fSFrançois Tigeot static void intel_crt_reset(struct drm_connector *connector)
705e3adcf8fSFrançois Tigeot {
706e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
70719df918dSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
708e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
709e3adcf8fSFrançois Tigeot 
7109edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
71119df918dSFrançois Tigeot 		u32 adpa;
71219df918dSFrançois Tigeot 
713a2fdbec6SFrançois Tigeot 		adpa = I915_READ(crt->adpa_reg);
71419df918dSFrançois Tigeot 		adpa &= ~ADPA_CRT_HOTPLUG_MASK;
71519df918dSFrançois Tigeot 		adpa |= ADPA_HOTPLUG_BITS;
716a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, adpa);
717a2fdbec6SFrançois Tigeot 		POSTING_READ(crt->adpa_reg);
71819df918dSFrançois Tigeot 
7192c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("crt adpa set to 0x%x\n", adpa);
720e3adcf8fSFrançois Tigeot 		crt->force_hotplug_required = 1;
721e3adcf8fSFrançois Tigeot 	}
722e3adcf8fSFrançois Tigeot 
72319df918dSFrançois Tigeot }
72419df918dSFrançois Tigeot 
725e3adcf8fSFrançois Tigeot /*
726e3adcf8fSFrançois Tigeot  * Routines for controlling stuff on the analog port
727e3adcf8fSFrançois Tigeot  */
728e3adcf8fSFrançois Tigeot 
729e3adcf8fSFrançois Tigeot static const struct drm_connector_funcs intel_crt_connector_funcs = {
730e3adcf8fSFrançois Tigeot 	.reset = intel_crt_reset,
731a05eeebfSFrançois Tigeot 	.dpms = drm_atomic_helper_connector_dpms,
732e3adcf8fSFrançois Tigeot 	.detect = intel_crt_detect,
733e3adcf8fSFrançois Tigeot 	.fill_modes = drm_helper_probe_single_connector_modes,
734e3adcf8fSFrançois Tigeot 	.destroy = intel_crt_destroy,
735e3adcf8fSFrançois Tigeot 	.set_property = intel_crt_set_property,
7362c9916cdSFrançois Tigeot 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
737477eb7f9SFrançois Tigeot 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
7382c9916cdSFrançois Tigeot 	.atomic_get_property = intel_connector_atomic_get_property,
739e3adcf8fSFrançois Tigeot };
740e3adcf8fSFrançois Tigeot 
741e3adcf8fSFrançois Tigeot static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
742e3adcf8fSFrançois Tigeot 	.mode_valid = intel_crt_mode_valid,
743e3adcf8fSFrançois Tigeot 	.get_modes = intel_crt_get_modes,
744e3adcf8fSFrançois Tigeot 	.best_encoder = intel_best_encoder,
745e3adcf8fSFrançois Tigeot };
746e3adcf8fSFrançois Tigeot 
747e3adcf8fSFrançois Tigeot static const struct drm_encoder_funcs intel_crt_enc_funcs = {
748e3adcf8fSFrançois Tigeot 	.destroy = intel_encoder_destroy,
749e3adcf8fSFrançois Tigeot };
750e3adcf8fSFrançois Tigeot 
75124edb884SFrançois Tigeot static int intel_no_crt_dmi_callback(const struct dmi_system_id *id)
752e3adcf8fSFrançois Tigeot {
75319df918dSFrançois Tigeot 	DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
754e3adcf8fSFrançois Tigeot 	return 1;
755e3adcf8fSFrançois Tigeot }
756e3adcf8fSFrançois Tigeot 
757e3adcf8fSFrançois Tigeot static const struct dmi_system_id intel_no_crt[] = {
758e3adcf8fSFrançois Tigeot 	{
759e3adcf8fSFrançois Tigeot 		.callback = intel_no_crt_dmi_callback,
760e3adcf8fSFrançois Tigeot 		.ident = "ACER ZGB",
761e3adcf8fSFrançois Tigeot 		.matches = {
762e3adcf8fSFrançois Tigeot 			DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
763e3adcf8fSFrançois Tigeot 			DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
764e3adcf8fSFrançois Tigeot 		},
765e3adcf8fSFrançois Tigeot 	},
766ba55f2f5SFrançois Tigeot 	{
767ba55f2f5SFrançois Tigeot 		.callback = intel_no_crt_dmi_callback,
768ba55f2f5SFrançois Tigeot 		.ident = "DELL XPS 8700",
769ba55f2f5SFrançois Tigeot 		.matches = {
770ba55f2f5SFrançois Tigeot 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
771ba55f2f5SFrançois Tigeot 			DMI_MATCH(DMI_PRODUCT_NAME, "XPS 8700"),
772ba55f2f5SFrançois Tigeot 		},
773ba55f2f5SFrançois Tigeot 	},
774e3adcf8fSFrançois Tigeot 	{ }
775e3adcf8fSFrançois Tigeot };
776e3adcf8fSFrançois Tigeot 
777e3adcf8fSFrançois Tigeot void intel_crt_init(struct drm_device *dev)
778e3adcf8fSFrançois Tigeot {
779e3adcf8fSFrançois Tigeot 	struct drm_connector *connector;
780e3adcf8fSFrançois Tigeot 	struct intel_crt *crt;
781e3adcf8fSFrançois Tigeot 	struct intel_connector *intel_connector;
782e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
783e3adcf8fSFrançois Tigeot 
784e3adcf8fSFrançois Tigeot 	/* Skip machines without VGA that falsely report hotplug events */
785e3adcf8fSFrançois Tigeot 	if (dmi_check_system(intel_no_crt))
786e3adcf8fSFrançois Tigeot 		return;
787e3adcf8fSFrançois Tigeot 
788159fc1d7SFrançois Tigeot 	crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
78919df918dSFrançois Tigeot 	if (!crt)
79019df918dSFrançois Tigeot 		return;
79119df918dSFrançois Tigeot 
792477eb7f9SFrançois Tigeot 	intel_connector = intel_connector_alloc();
79319df918dSFrançois Tigeot 	if (!intel_connector) {
794158486a6SFrançois Tigeot 		kfree(crt);
79519df918dSFrançois Tigeot 		return;
79619df918dSFrançois Tigeot 	}
797e3adcf8fSFrançois Tigeot 
798e3adcf8fSFrançois Tigeot 	connector = &intel_connector->base;
79919df918dSFrançois Tigeot 	crt->connector = intel_connector;
800e3adcf8fSFrançois Tigeot 	drm_connector_init(dev, &intel_connector->base,
801e3adcf8fSFrançois Tigeot 			   &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
802e3adcf8fSFrançois Tigeot 
803e3adcf8fSFrançois Tigeot 	drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
804e3adcf8fSFrançois Tigeot 			 DRM_MODE_ENCODER_DAC);
805e3adcf8fSFrançois Tigeot 
806e3adcf8fSFrançois Tigeot 	intel_connector_attach_encoder(intel_connector, &crt->base);
807e3adcf8fSFrançois Tigeot 
808e3adcf8fSFrançois Tigeot 	crt->base.type = INTEL_OUTPUT_ANALOG;
809ba55f2f5SFrançois Tigeot 	crt->base.cloneable = (1 << INTEL_OUTPUT_DVO) | (1 << INTEL_OUTPUT_HDMI);
810e9243325SFrançois Tigeot 	if (IS_I830(dev))
811e9243325SFrançois Tigeot 		crt->base.crtc_mask = (1 << 0);
812e9243325SFrançois Tigeot 	else
813e9243325SFrançois Tigeot 		crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
814e9243325SFrançois Tigeot 
815e3adcf8fSFrançois Tigeot 	if (IS_GEN2(dev))
816e3adcf8fSFrançois Tigeot 		connector->interlace_allowed = 0;
817e3adcf8fSFrançois Tigeot 	else
818e3adcf8fSFrançois Tigeot 		connector->interlace_allowed = 1;
819e3adcf8fSFrançois Tigeot 	connector->doublescan_allowed = 0;
820e3adcf8fSFrançois Tigeot 
821e9243325SFrançois Tigeot 	if (HAS_PCH_SPLIT(dev))
822e9243325SFrançois Tigeot 		crt->adpa_reg = PCH_ADPA;
823e9243325SFrançois Tigeot 	else if (IS_VALLEYVIEW(dev))
824e9243325SFrançois Tigeot 		crt->adpa_reg = VLV_ADPA;
825e9243325SFrançois Tigeot 	else
826e9243325SFrançois Tigeot 		crt->adpa_reg = ADPA;
827e9243325SFrançois Tigeot 
8288e26cdf6SFrançois Tigeot 	crt->base.compute_config = intel_crt_compute_config;
82919c468b4SFrançois Tigeot 	if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev)) {
83019c468b4SFrançois Tigeot 		crt->base.disable = pch_disable_crt;
83119c468b4SFrançois Tigeot 		crt->base.post_disable = pch_post_disable_crt;
83219c468b4SFrançois Tigeot 	} else {
83319df918dSFrançois Tigeot 		crt->base.disable = intel_disable_crt;
83419c468b4SFrançois Tigeot 	}
83519df918dSFrançois Tigeot 	crt->base.enable = intel_enable_crt;
8368e26cdf6SFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev))
8378e26cdf6SFrançois Tigeot 		crt->base.hpd_pin = HPD_CRT;
8389edbd4a0SFrançois Tigeot 	if (HAS_DDI(dev)) {
8399edbd4a0SFrançois Tigeot 		crt->base.get_config = hsw_crt_get_config;
84019df918dSFrançois Tigeot 		crt->base.get_hw_state = intel_ddi_get_hw_state;
8419edbd4a0SFrançois Tigeot 	} else {
8429edbd4a0SFrançois Tigeot 		crt->base.get_config = intel_crt_get_config;
84319df918dSFrançois Tigeot 		crt->base.get_hw_state = intel_crt_get_hw_state;
8449edbd4a0SFrançois Tigeot 	}
84519df918dSFrançois Tigeot 	intel_connector->get_hw_state = intel_connector_get_hw_state;
846ba55f2f5SFrançois Tigeot 	intel_connector->unregister = intel_connector_unregister;
84719df918dSFrançois Tigeot 
848e3adcf8fSFrançois Tigeot 	drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
849e3adcf8fSFrançois Tigeot 
850c6f73aabSFrançois Tigeot 	drm_connector_register(connector);
851e3adcf8fSFrançois Tigeot 
8528e26cdf6SFrançois Tigeot 	if (!I915_HAS_HOTPLUG(dev))
8538e26cdf6SFrançois Tigeot 		intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
854e3adcf8fSFrançois Tigeot 
855e3adcf8fSFrançois Tigeot 	/*
856e3adcf8fSFrançois Tigeot 	 * Configure the automatic hotplug detection stuff
857e3adcf8fSFrançois Tigeot 	 */
858e3adcf8fSFrançois Tigeot 	crt->force_hotplug_required = 0;
859e3adcf8fSFrançois Tigeot 
86019df918dSFrançois Tigeot 	/*
86119df918dSFrançois Tigeot 	 * TODO: find a proper way to discover whether we need to set the the
86219df918dSFrançois Tigeot 	 * polarity and link reversal bits or not, instead of relying on the
86319df918dSFrançois Tigeot 	 * BIOS.
86419df918dSFrançois Tigeot 	 */
86519df918dSFrançois Tigeot 	if (HAS_PCH_LPT(dev)) {
86619df918dSFrançois Tigeot 		u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
86719df918dSFrançois Tigeot 				 FDI_RX_LINK_REVERSAL_OVERRIDE;
86819df918dSFrançois Tigeot 
869*352ff8bdSFrançois Tigeot 		dev_priv->fdi_rx_config = I915_READ(FDI_RX_CTL(PIPE_A)) & fdi_config;
87019df918dSFrançois Tigeot 	}
871ba55f2f5SFrançois Tigeot 
872ba55f2f5SFrançois Tigeot 	intel_crt_reset(connector);
873e3adcf8fSFrançois Tigeot }
874