xref: /dflybsd-src/sys/dev/drm/i915/intel_crt.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
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>
291487f786SFrançois Tigeot #include <linux/slab.h>
3018e26a6dSFrançois Tigeot #include <drm/drmP.h>
312c9916cdSFrançois Tigeot #include <drm/drm_atomic_helper.h>
3218e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
3318e26a6dSFrançois Tigeot #include <drm/drm_crtc_helper.h>
3418e26a6dSFrançois Tigeot #include <drm/drm_edid.h>
3518e26a6dSFrançois Tigeot #include "intel_drv.h"
365c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
37e3adcf8fSFrançois Tigeot #include "i915_drv.h"
38e3adcf8fSFrançois Tigeot 
39e3adcf8fSFrançois Tigeot /* Here's the desired hotplug mode */
40e3adcf8fSFrançois Tigeot #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 |		\
41e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_WARMUP_10MS |		\
42e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_SAMPLE_4S |			\
43e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_VOLTAGE_50 |		\
44e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_VOLREF_325MV |		\
45e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_ENABLE)
46e3adcf8fSFrançois Tigeot 
47e3adcf8fSFrançois Tigeot struct intel_crt {
48e3adcf8fSFrançois Tigeot 	struct intel_encoder base;
49e9243325SFrançois Tigeot 	/* DPMS state is stored in the connector, which we need in the
50e9243325SFrançois Tigeot 	 * encoder's enable/disable callbacks */
51e9243325SFrançois Tigeot 	struct intel_connector *connector;
52e3adcf8fSFrançois Tigeot 	bool force_hotplug_required;
53aee94f86SFrançois Tigeot 	i915_reg_t adpa_reg;
54e3adcf8fSFrançois Tigeot };
55e3adcf8fSFrançois Tigeot 
intel_encoder_to_crt(struct intel_encoder * encoder)5619df918dSFrançois Tigeot static struct intel_crt *intel_encoder_to_crt(struct intel_encoder *encoder)
57e3adcf8fSFrançois Tigeot {
5819df918dSFrançois Tigeot 	return container_of(encoder, struct intel_crt, base);
5919df918dSFrançois Tigeot }
6019df918dSFrançois Tigeot 
intel_attached_crt(struct drm_connector * connector)619edbd4a0SFrançois Tigeot static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
629edbd4a0SFrançois Tigeot {
639edbd4a0SFrançois Tigeot 	return intel_encoder_to_crt(intel_attached_encoder(connector));
649edbd4a0SFrançois Tigeot }
659edbd4a0SFrançois Tigeot 
intel_crt_get_hw_state(struct intel_encoder * encoder,enum i915_pipe * pipe)6619df918dSFrançois Tigeot static bool intel_crt_get_hw_state(struct intel_encoder *encoder,
6719df918dSFrançois Tigeot 				   enum i915_pipe *pipe)
6819df918dSFrançois Tigeot {
6919df918dSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
70303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
7119df918dSFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
7219df918dSFrançois Tigeot 	u32 tmp;
73aee94f86SFrançois Tigeot 	bool ret;
74e3adcf8fSFrançois Tigeot 
75a85cb24fSFrançois Tigeot 	if (!intel_display_power_get_if_enabled(dev_priv,
76a85cb24fSFrançois Tigeot 						encoder->power_domain))
77ba55f2f5SFrançois Tigeot 		return false;
78ba55f2f5SFrançois Tigeot 
79aee94f86SFrançois Tigeot 	ret = false;
80aee94f86SFrançois Tigeot 
8119df918dSFrançois Tigeot 	tmp = I915_READ(crt->adpa_reg);
8219df918dSFrançois Tigeot 
8319df918dSFrançois Tigeot 	if (!(tmp & ADPA_DAC_ENABLE))
84aee94f86SFrançois Tigeot 		goto out;
8519df918dSFrançois Tigeot 
861e12ee3bSFrançois Tigeot 	if (HAS_PCH_CPT(dev_priv))
8719df918dSFrançois Tigeot 		*pipe = PORT_TO_PIPE_CPT(tmp);
88e3adcf8fSFrançois Tigeot 	else
8919df918dSFrançois Tigeot 		*pipe = PORT_TO_PIPE(tmp);
90e3adcf8fSFrançois Tigeot 
91aee94f86SFrançois Tigeot 	ret = true;
92aee94f86SFrançois Tigeot out:
93a85cb24fSFrançois Tigeot 	intel_display_power_put(dev_priv, encoder->power_domain);
94aee94f86SFrançois Tigeot 
95aee94f86SFrançois Tigeot 	return ret;
9619df918dSFrançois Tigeot }
9719df918dSFrançois Tigeot 
intel_crt_get_flags(struct intel_encoder * encoder)989edbd4a0SFrançois Tigeot static unsigned int intel_crt_get_flags(struct intel_encoder *encoder)
995d0b1887SFrançois Tigeot {
100303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1015d0b1887SFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
1025d0b1887SFrançois Tigeot 	u32 tmp, flags = 0;
1035d0b1887SFrançois Tigeot 
1045d0b1887SFrançois Tigeot 	tmp = I915_READ(crt->adpa_reg);
1055d0b1887SFrançois Tigeot 
1065d0b1887SFrançois Tigeot 	if (tmp & ADPA_HSYNC_ACTIVE_HIGH)
1075d0b1887SFrançois Tigeot 		flags |= DRM_MODE_FLAG_PHSYNC;
1085d0b1887SFrançois Tigeot 	else
1095d0b1887SFrançois Tigeot 		flags |= DRM_MODE_FLAG_NHSYNC;
1105d0b1887SFrançois Tigeot 
1115d0b1887SFrançois Tigeot 	if (tmp & ADPA_VSYNC_ACTIVE_HIGH)
1125d0b1887SFrançois Tigeot 		flags |= DRM_MODE_FLAG_PVSYNC;
1135d0b1887SFrançois Tigeot 	else
1145d0b1887SFrançois Tigeot 		flags |= DRM_MODE_FLAG_NVSYNC;
1155d0b1887SFrançois Tigeot 
1169edbd4a0SFrançois Tigeot 	return flags;
1179edbd4a0SFrançois Tigeot }
1189edbd4a0SFrançois Tigeot 
intel_crt_get_config(struct intel_encoder * encoder,struct intel_crtc_state * pipe_config)1199edbd4a0SFrançois Tigeot static void intel_crt_get_config(struct intel_encoder *encoder,
1202c9916cdSFrançois Tigeot 				 struct intel_crtc_state *pipe_config)
1219edbd4a0SFrançois Tigeot {
1222c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.flags |= intel_crt_get_flags(encoder);
1239edbd4a0SFrançois Tigeot 
1248621f407SFrançois Tigeot 	pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
1259edbd4a0SFrançois Tigeot }
1269edbd4a0SFrançois Tigeot 
hsw_crt_get_config(struct intel_encoder * encoder,struct intel_crtc_state * pipe_config)1279edbd4a0SFrançois Tigeot static void hsw_crt_get_config(struct intel_encoder *encoder,
1282c9916cdSFrançois Tigeot 			       struct intel_crtc_state *pipe_config)
1299edbd4a0SFrançois Tigeot {
1308621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1318621f407SFrançois Tigeot 
1329edbd4a0SFrançois Tigeot 	intel_ddi_get_config(encoder, pipe_config);
1339edbd4a0SFrançois Tigeot 
1342c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.flags &= ~(DRM_MODE_FLAG_PHSYNC |
1359edbd4a0SFrançois Tigeot 					      DRM_MODE_FLAG_NHSYNC |
1369edbd4a0SFrançois Tigeot 					      DRM_MODE_FLAG_PVSYNC |
1379edbd4a0SFrançois Tigeot 					      DRM_MODE_FLAG_NVSYNC);
1382c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.flags |= intel_crt_get_flags(encoder);
1398621f407SFrançois Tigeot 
1408621f407SFrançois Tigeot 	pipe_config->base.adjusted_mode.crtc_clock = lpt_get_iclkip(dev_priv);
1415d0b1887SFrançois Tigeot }
1425d0b1887SFrançois Tigeot 
14319df918dSFrançois Tigeot /* Note: The caller is required to filter out dpms modes not supported by the
14419df918dSFrançois Tigeot  * platform. */
intel_crt_set_dpms(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state,int mode)1451e12ee3bSFrançois Tigeot static void intel_crt_set_dpms(struct intel_encoder *encoder,
146*3f2dd94aSFrançois Tigeot 			       const struct intel_crtc_state *crtc_state,
1471e12ee3bSFrançois Tigeot 			       int mode)
14819df918dSFrançois Tigeot {
1494be47400SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
15019df918dSFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
1511e12ee3bSFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1521e12ee3bSFrançois Tigeot 	const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
153ba55f2f5SFrançois Tigeot 	u32 adpa;
15419df918dSFrançois Tigeot 
1554be47400SFrançois Tigeot 	if (INTEL_GEN(dev_priv) >= 5)
156ba55f2f5SFrançois Tigeot 		adpa = ADPA_HOTPLUG_BITS;
157ba55f2f5SFrançois Tigeot 	else
158ba55f2f5SFrançois Tigeot 		adpa = 0;
159ba55f2f5SFrançois Tigeot 
160ba55f2f5SFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
161ba55f2f5SFrançois Tigeot 		adpa |= ADPA_HSYNC_ACTIVE_HIGH;
162ba55f2f5SFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
163ba55f2f5SFrançois Tigeot 		adpa |= ADPA_VSYNC_ACTIVE_HIGH;
164ba55f2f5SFrançois Tigeot 
165ba55f2f5SFrançois Tigeot 	/* For CPT allow 3 pipe config, for others just use A or B */
1661e12ee3bSFrançois Tigeot 	if (HAS_PCH_LPT(dev_priv))
167ba55f2f5SFrançois Tigeot 		; /* Those bits don't exist here */
1681e12ee3bSFrançois Tigeot 	else if (HAS_PCH_CPT(dev_priv))
169ba55f2f5SFrançois Tigeot 		adpa |= PORT_TRANS_SEL_CPT(crtc->pipe);
170ba55f2f5SFrançois Tigeot 	else if (crtc->pipe == 0)
171ba55f2f5SFrançois Tigeot 		adpa |= ADPA_PIPE_A_SELECT;
172ba55f2f5SFrançois Tigeot 	else
173ba55f2f5SFrançois Tigeot 		adpa |= ADPA_PIPE_B_SELECT;
174ba55f2f5SFrançois Tigeot 
1751e12ee3bSFrançois Tigeot 	if (!HAS_PCH_SPLIT(dev_priv))
176ba55f2f5SFrançois Tigeot 		I915_WRITE(BCLRPAT(crtc->pipe), 0);
177e3adcf8fSFrançois Tigeot 
178e3adcf8fSFrançois Tigeot 	switch (mode) {
179e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_ON:
180ba55f2f5SFrançois Tigeot 		adpa |= ADPA_DAC_ENABLE;
181e3adcf8fSFrançois Tigeot 		break;
182e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_STANDBY:
183ba55f2f5SFrançois Tigeot 		adpa |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
184e3adcf8fSFrançois Tigeot 		break;
185e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_SUSPEND:
186ba55f2f5SFrançois Tigeot 		adpa |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
187e3adcf8fSFrançois Tigeot 		break;
188e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_OFF:
189ba55f2f5SFrançois Tigeot 		adpa |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
190e3adcf8fSFrançois Tigeot 		break;
191e3adcf8fSFrançois Tigeot 	}
192e3adcf8fSFrançois Tigeot 
193ba55f2f5SFrançois Tigeot 	I915_WRITE(crt->adpa_reg, adpa);
19419df918dSFrançois Tigeot }
19519df918dSFrançois Tigeot 
intel_disable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)1961e12ee3bSFrançois Tigeot static void intel_disable_crt(struct intel_encoder *encoder,
197*3f2dd94aSFrançois Tigeot 			      const struct intel_crtc_state *old_crtc_state,
198*3f2dd94aSFrançois Tigeot 			      const struct drm_connector_state *old_conn_state)
19919df918dSFrançois Tigeot {
2001e12ee3bSFrançois Tigeot 	intel_crt_set_dpms(encoder, old_crtc_state, DRM_MODE_DPMS_OFF);
20119df918dSFrançois Tigeot }
20219df918dSFrançois Tigeot 
pch_disable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)2031e12ee3bSFrançois Tigeot static void pch_disable_crt(struct intel_encoder *encoder,
204*3f2dd94aSFrançois Tigeot 			    const struct intel_crtc_state *old_crtc_state,
205*3f2dd94aSFrançois Tigeot 			    const struct drm_connector_state *old_conn_state)
20619c468b4SFrançois Tigeot {
20719c468b4SFrançois Tigeot }
20819c468b4SFrançois Tigeot 
pch_post_disable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)2091e12ee3bSFrançois Tigeot static void pch_post_disable_crt(struct intel_encoder *encoder,
210*3f2dd94aSFrançois Tigeot 				 const struct intel_crtc_state *old_crtc_state,
211*3f2dd94aSFrançois Tigeot 				 const struct drm_connector_state *old_conn_state)
21219c468b4SFrançois Tigeot {
2131e12ee3bSFrançois Tigeot 	intel_disable_crt(encoder, old_crtc_state, old_conn_state);
21419c468b4SFrançois Tigeot }
21524edb884SFrançois Tigeot 
hsw_disable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)216*3f2dd94aSFrançois Tigeot static void hsw_disable_crt(struct intel_encoder *encoder,
217*3f2dd94aSFrançois Tigeot 			    const struct intel_crtc_state *old_crtc_state,
218*3f2dd94aSFrançois Tigeot 			    const struct drm_connector_state *old_conn_state)
219*3f2dd94aSFrançois Tigeot {
220*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc = old_crtc_state->base.crtc;
221*3f2dd94aSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
222*3f2dd94aSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
223*3f2dd94aSFrançois Tigeot 
224*3f2dd94aSFrançois Tigeot 	WARN_ON(!intel_crtc->config->has_pch_encoder);
225*3f2dd94aSFrançois Tigeot 
226*3f2dd94aSFrançois Tigeot 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
227*3f2dd94aSFrançois Tigeot }
228*3f2dd94aSFrançois Tigeot 
hsw_post_disable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)2291e12ee3bSFrançois Tigeot static void hsw_post_disable_crt(struct intel_encoder *encoder,
230*3f2dd94aSFrançois Tigeot 				 const struct intel_crtc_state *old_crtc_state,
231*3f2dd94aSFrançois Tigeot 				 const struct drm_connector_state *old_conn_state)
23219df918dSFrançois Tigeot {
2331e12ee3bSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2341e12ee3bSFrançois Tigeot 
2351e12ee3bSFrançois Tigeot 	pch_post_disable_crt(encoder, old_crtc_state, old_conn_state);
2361e12ee3bSFrançois Tigeot 
2371e12ee3bSFrançois Tigeot 	lpt_disable_pch_transcoder(dev_priv);
2381e12ee3bSFrançois Tigeot 	lpt_disable_iclkip(dev_priv);
2391e12ee3bSFrançois Tigeot 
2401e12ee3bSFrançois Tigeot 	intel_ddi_fdi_post_disable(encoder, old_crtc_state, old_conn_state);
241*3f2dd94aSFrançois Tigeot 
242*3f2dd94aSFrançois Tigeot 	WARN_ON(!old_crtc_state->has_pch_encoder);
243*3f2dd94aSFrançois Tigeot 
244*3f2dd94aSFrançois Tigeot 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
245*3f2dd94aSFrançois Tigeot }
246*3f2dd94aSFrançois Tigeot 
hsw_pre_pll_enable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * pipe_config,const struct drm_connector_state * conn_state)247*3f2dd94aSFrançois Tigeot static void hsw_pre_pll_enable_crt(struct intel_encoder *encoder,
248*3f2dd94aSFrançois Tigeot 				   const struct intel_crtc_state *pipe_config,
249*3f2dd94aSFrançois Tigeot 				   const struct drm_connector_state *conn_state)
250*3f2dd94aSFrançois Tigeot {
251*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc = pipe_config->base.crtc;
252*3f2dd94aSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
253*3f2dd94aSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
254*3f2dd94aSFrançois Tigeot 
255*3f2dd94aSFrançois Tigeot 	WARN_ON(!intel_crtc->config->has_pch_encoder);
256*3f2dd94aSFrançois Tigeot 
257*3f2dd94aSFrançois Tigeot 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
258*3f2dd94aSFrançois Tigeot }
259*3f2dd94aSFrançois Tigeot 
hsw_pre_enable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * pipe_config,const struct drm_connector_state * conn_state)260*3f2dd94aSFrançois Tigeot static void hsw_pre_enable_crt(struct intel_encoder *encoder,
261*3f2dd94aSFrançois Tigeot 			       const struct intel_crtc_state *pipe_config,
262*3f2dd94aSFrançois Tigeot 			       const struct drm_connector_state *conn_state)
263*3f2dd94aSFrançois Tigeot {
264*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc = pipe_config->base.crtc;
265*3f2dd94aSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
266*3f2dd94aSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
267*3f2dd94aSFrançois Tigeot 	int pipe = intel_crtc->pipe;
268*3f2dd94aSFrançois Tigeot 
269*3f2dd94aSFrançois Tigeot 	WARN_ON(!intel_crtc->config->has_pch_encoder);
270*3f2dd94aSFrançois Tigeot 
271*3f2dd94aSFrançois Tigeot 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
272*3f2dd94aSFrançois Tigeot 
273*3f2dd94aSFrançois Tigeot 	dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
274*3f2dd94aSFrançois Tigeot }
275*3f2dd94aSFrançois Tigeot 
hsw_enable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * pipe_config,const struct drm_connector_state * conn_state)276*3f2dd94aSFrançois Tigeot static void hsw_enable_crt(struct intel_encoder *encoder,
277*3f2dd94aSFrançois Tigeot 			   const struct intel_crtc_state *pipe_config,
278*3f2dd94aSFrançois Tigeot 			   const struct drm_connector_state *conn_state)
279*3f2dd94aSFrançois Tigeot {
280*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc = pipe_config->base.crtc;
281*3f2dd94aSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
282*3f2dd94aSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
283*3f2dd94aSFrançois Tigeot 	int pipe = intel_crtc->pipe;
284*3f2dd94aSFrançois Tigeot 
285*3f2dd94aSFrançois Tigeot 	WARN_ON(!intel_crtc->config->has_pch_encoder);
286*3f2dd94aSFrançois Tigeot 
287*3f2dd94aSFrançois Tigeot 	intel_crt_set_dpms(encoder, pipe_config, DRM_MODE_DPMS_ON);
288*3f2dd94aSFrançois Tigeot 
289*3f2dd94aSFrançois Tigeot 	intel_wait_for_vblank(dev_priv, pipe);
290*3f2dd94aSFrançois Tigeot 	intel_wait_for_vblank(dev_priv, pipe);
291*3f2dd94aSFrançois Tigeot 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
292*3f2dd94aSFrançois Tigeot 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
2931e12ee3bSFrançois Tigeot }
2941e12ee3bSFrançois Tigeot 
intel_enable_crt(struct intel_encoder * encoder,const struct intel_crtc_state * pipe_config,const struct drm_connector_state * conn_state)2951e12ee3bSFrançois Tigeot static void intel_enable_crt(struct intel_encoder *encoder,
296*3f2dd94aSFrançois Tigeot 			     const struct intel_crtc_state *pipe_config,
297*3f2dd94aSFrançois Tigeot 			     const struct drm_connector_state *conn_state)
2981e12ee3bSFrançois Tigeot {
2991e12ee3bSFrançois Tigeot 	intel_crt_set_dpms(encoder, pipe_config, DRM_MODE_DPMS_ON);
30019df918dSFrançois Tigeot }
30119df918dSFrançois Tigeot 
3029edbd4a0SFrançois Tigeot static enum drm_mode_status
intel_crt_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)3039edbd4a0SFrançois Tigeot intel_crt_mode_valid(struct drm_connector *connector,
304e3adcf8fSFrançois Tigeot 		     struct drm_display_mode *mode)
305e3adcf8fSFrançois Tigeot {
306e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
3071e12ee3bSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
3081e12ee3bSFrançois Tigeot 	int max_dotclk = dev_priv->max_dotclk_freq;
3098621f407SFrançois Tigeot 	int max_clock;
310e3adcf8fSFrançois Tigeot 
311e3adcf8fSFrançois Tigeot 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
312e3adcf8fSFrançois Tigeot 		return MODE_NO_DBLESCAN;
313e3adcf8fSFrançois Tigeot 
314e3adcf8fSFrançois Tigeot 	if (mode->clock < 25000)
315e3adcf8fSFrançois Tigeot 		return MODE_CLOCK_LOW;
316e3adcf8fSFrançois Tigeot 
3171e12ee3bSFrançois Tigeot 	if (HAS_PCH_LPT(dev_priv))
3188621f407SFrançois Tigeot 		max_clock = 180000;
3191e12ee3bSFrançois Tigeot 	else if (IS_VALLEYVIEW(dev_priv))
3208621f407SFrançois Tigeot 		/*
3218621f407SFrançois Tigeot 		 * 270 MHz due to current DPLL limits,
3228621f407SFrançois Tigeot 		 * DAC limit supposedly 355 MHz.
3238621f407SFrançois Tigeot 		 */
3248621f407SFrançois Tigeot 		max_clock = 270000;
3251e12ee3bSFrançois Tigeot 	else if (IS_GEN3(dev_priv) || IS_GEN4(dev_priv))
326e3adcf8fSFrançois Tigeot 		max_clock = 400000;
3278621f407SFrançois Tigeot 	else
3288621f407SFrançois Tigeot 		max_clock = 350000;
329e3adcf8fSFrançois Tigeot 	if (mode->clock > max_clock)
330e3adcf8fSFrançois Tigeot 		return MODE_CLOCK_HIGH;
331e3adcf8fSFrançois Tigeot 
332c0e85e96SFrançois Tigeot 	if (mode->clock > max_dotclk)
333c0e85e96SFrançois Tigeot 		return MODE_CLOCK_HIGH;
334c0e85e96SFrançois Tigeot 
33519df918dSFrançois Tigeot 	/* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
3361e12ee3bSFrançois Tigeot 	if (HAS_PCH_LPT(dev_priv) &&
33719df918dSFrançois Tigeot 	    (ironlake_get_lanes_required(mode->clock, 270000, 24) > 2))
33819df918dSFrançois Tigeot 		return MODE_CLOCK_HIGH;
33919df918dSFrançois Tigeot 
340e3adcf8fSFrançois Tigeot 	return MODE_OK;
341e3adcf8fSFrançois Tigeot }
342e3adcf8fSFrançois Tigeot 
intel_crt_compute_config(struct intel_encoder * encoder,struct intel_crtc_state * pipe_config,struct drm_connector_state * conn_state)3438e26cdf6SFrançois Tigeot static bool intel_crt_compute_config(struct intel_encoder *encoder,
3441e12ee3bSFrançois Tigeot 				     struct intel_crtc_state *pipe_config,
3451e12ee3bSFrançois Tigeot 				     struct drm_connector_state *conn_state)
346e3adcf8fSFrançois Tigeot {
347*3f2dd94aSFrançois Tigeot 	return true;
348*3f2dd94aSFrançois Tigeot }
349*3f2dd94aSFrançois Tigeot 
pch_crt_compute_config(struct intel_encoder * encoder,struct intel_crtc_state * pipe_config,struct drm_connector_state * conn_state)350*3f2dd94aSFrançois Tigeot static bool pch_crt_compute_config(struct intel_encoder *encoder,
351*3f2dd94aSFrançois Tigeot 				   struct intel_crtc_state *pipe_config,
352*3f2dd94aSFrançois Tigeot 				   struct drm_connector_state *conn_state)
353*3f2dd94aSFrançois Tigeot {
354*3f2dd94aSFrançois Tigeot 	pipe_config->has_pch_encoder = true;
355*3f2dd94aSFrançois Tigeot 
356*3f2dd94aSFrançois Tigeot 	return true;
357*3f2dd94aSFrançois Tigeot }
358*3f2dd94aSFrançois Tigeot 
hsw_crt_compute_config(struct intel_encoder * encoder,struct intel_crtc_state * pipe_config,struct drm_connector_state * conn_state)359*3f2dd94aSFrançois Tigeot static bool hsw_crt_compute_config(struct intel_encoder *encoder,
360*3f2dd94aSFrançois Tigeot 				   struct intel_crtc_state *pipe_config,
361*3f2dd94aSFrançois Tigeot 				   struct drm_connector_state *conn_state)
362*3f2dd94aSFrançois Tigeot {
3631e12ee3bSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3648e26cdf6SFrançois Tigeot 
3658e26cdf6SFrançois Tigeot 	pipe_config->has_pch_encoder = true;
3668e26cdf6SFrançois Tigeot 
3675d0b1887SFrançois Tigeot 	/* LPT FDI RX only supports 8bpc. */
3681e12ee3bSFrançois Tigeot 	if (HAS_PCH_LPT(dev_priv)) {
369c0e85e96SFrançois Tigeot 		if (pipe_config->bw_constrained && pipe_config->pipe_bpp < 24) {
370c0e85e96SFrançois Tigeot 			DRM_DEBUG_KMS("LPT only supports 24bpp\n");
371c0e85e96SFrançois Tigeot 			return false;
372c0e85e96SFrançois Tigeot 		}
373c0e85e96SFrançois Tigeot 
3745d0b1887SFrançois Tigeot 		pipe_config->pipe_bpp = 24;
375c0e85e96SFrançois Tigeot 	}
3765d0b1887SFrançois Tigeot 
377ba55f2f5SFrançois Tigeot 	/* FDI must always be 2.7 GHz */
378ba55f2f5SFrançois Tigeot 	pipe_config->port_clock = 135000 * 2;
379352ff8bdSFrançois Tigeot 
380e3adcf8fSFrançois Tigeot 	return true;
381e3adcf8fSFrançois Tigeot }
382e3adcf8fSFrançois Tigeot 
intel_ironlake_crt_detect_hotplug(struct drm_connector * connector)383e3adcf8fSFrançois Tigeot static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
384e3adcf8fSFrançois Tigeot {
385e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
386e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
387303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
388e3adcf8fSFrançois Tigeot 	u32 adpa;
389e3adcf8fSFrançois Tigeot 	bool ret;
390e3adcf8fSFrançois Tigeot 
391e3adcf8fSFrançois Tigeot 	/* The first time through, trigger an explicit detection cycle */
392e3adcf8fSFrançois Tigeot 	if (crt->force_hotplug_required) {
3931e12ee3bSFrançois Tigeot 		bool turn_off_dac = HAS_PCH_SPLIT(dev_priv);
394e3adcf8fSFrançois Tigeot 		u32 save_adpa;
395e3adcf8fSFrançois Tigeot 
396e3adcf8fSFrançois Tigeot 		crt->force_hotplug_required = 0;
397e3adcf8fSFrançois Tigeot 
398a2fdbec6SFrançois Tigeot 		save_adpa = adpa = I915_READ(crt->adpa_reg);
399e3adcf8fSFrançois Tigeot 		DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
400e3adcf8fSFrançois Tigeot 
401e3adcf8fSFrançois Tigeot 		adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
402e3adcf8fSFrançois Tigeot 		if (turn_off_dac)
403e3adcf8fSFrançois Tigeot 			adpa &= ~ADPA_DAC_ENABLE;
404e3adcf8fSFrançois Tigeot 
405a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, adpa);
406e3adcf8fSFrançois Tigeot 
4071487f786SFrançois Tigeot 		if (intel_wait_for_register(dev_priv,
4081487f786SFrançois Tigeot 					    crt->adpa_reg,
4091487f786SFrançois Tigeot 					    ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
41019df918dSFrançois Tigeot 					    1000))
41119df918dSFrançois Tigeot 			DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
412e3adcf8fSFrançois Tigeot 
413e3adcf8fSFrançois Tigeot 		if (turn_off_dac) {
414a2fdbec6SFrançois Tigeot 			I915_WRITE(crt->adpa_reg, save_adpa);
415a2fdbec6SFrançois Tigeot 			POSTING_READ(crt->adpa_reg);
416e3adcf8fSFrançois Tigeot 		}
417e3adcf8fSFrançois Tigeot 	}
418e3adcf8fSFrançois Tigeot 
419e3adcf8fSFrançois Tigeot 	/* Check the status to see if both blue and green are on now */
420a2fdbec6SFrançois Tigeot 	adpa = I915_READ(crt->adpa_reg);
421e3adcf8fSFrançois Tigeot 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
422e3adcf8fSFrançois Tigeot 		ret = true;
423e3adcf8fSFrançois Tigeot 	else
424e3adcf8fSFrançois Tigeot 		ret = false;
425e3adcf8fSFrançois Tigeot 	DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
426e3adcf8fSFrançois Tigeot 
427e3adcf8fSFrançois Tigeot 	return ret;
428e3adcf8fSFrançois Tigeot }
429e3adcf8fSFrançois Tigeot 
valleyview_crt_detect_hotplug(struct drm_connector * connector)43019df918dSFrançois Tigeot static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
43119df918dSFrançois Tigeot {
43219df918dSFrançois Tigeot 	struct drm_device *dev = connector->dev;
433a2fdbec6SFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
434303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
43571f41f3eSFrançois Tigeot 	bool reenable_hpd;
43619df918dSFrançois Tigeot 	u32 adpa;
43719df918dSFrançois Tigeot 	bool ret;
43819df918dSFrançois Tigeot 	u32 save_adpa;
43919df918dSFrançois Tigeot 
44071f41f3eSFrançois Tigeot 	/*
44171f41f3eSFrançois Tigeot 	 * Doing a force trigger causes a hpd interrupt to get sent, which can
44271f41f3eSFrançois Tigeot 	 * get us stuck in a loop if we're polling:
44371f41f3eSFrançois Tigeot 	 *  - We enable power wells and reset the ADPA
44471f41f3eSFrançois Tigeot 	 *  - output_poll_exec does force probe on VGA, triggering a hpd
44571f41f3eSFrançois Tigeot 	 *  - HPD handler waits for poll to unlock dev->mode_config.mutex
44671f41f3eSFrançois Tigeot 	 *  - output_poll_exec shuts off the ADPA, unlocks
44771f41f3eSFrançois Tigeot 	 *    dev->mode_config.mutex
44871f41f3eSFrançois Tigeot 	 *  - HPD handler runs, resets ADPA and brings us back to the start
44971f41f3eSFrançois Tigeot 	 *
45071f41f3eSFrançois Tigeot 	 * Just disable HPD interrupts here to prevent this
45171f41f3eSFrançois Tigeot 	 */
45271f41f3eSFrançois Tigeot 	reenable_hpd = intel_hpd_disable(dev_priv, crt->base.hpd_pin);
45371f41f3eSFrançois Tigeot 
454a2fdbec6SFrançois Tigeot 	save_adpa = adpa = I915_READ(crt->adpa_reg);
45519df918dSFrançois Tigeot 	DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
45619df918dSFrançois Tigeot 
45719df918dSFrançois Tigeot 	adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
45819df918dSFrançois Tigeot 
459a2fdbec6SFrançois Tigeot 	I915_WRITE(crt->adpa_reg, adpa);
46019df918dSFrançois Tigeot 
4611487f786SFrançois Tigeot 	if (intel_wait_for_register(dev_priv,
4621487f786SFrançois Tigeot 				    crt->adpa_reg,
4631487f786SFrançois Tigeot 				    ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
46419df918dSFrançois Tigeot 				    1000)) {
46519df918dSFrançois Tigeot 		DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
466a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, save_adpa);
46719df918dSFrançois Tigeot 	}
46819df918dSFrançois Tigeot 
46919df918dSFrançois Tigeot 	/* Check the status to see if both blue and green are on now */
470a2fdbec6SFrançois Tigeot 	adpa = I915_READ(crt->adpa_reg);
47119df918dSFrançois Tigeot 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
47219df918dSFrançois Tigeot 		ret = true;
47319df918dSFrançois Tigeot 	else
47419df918dSFrançois Tigeot 		ret = false;
47519df918dSFrançois Tigeot 
47619df918dSFrançois Tigeot 	DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
47719df918dSFrançois Tigeot 
47871f41f3eSFrançois Tigeot 	if (reenable_hpd)
47971f41f3eSFrançois Tigeot 		intel_hpd_enable(dev_priv, crt->base.hpd_pin);
48071f41f3eSFrançois Tigeot 
48119df918dSFrançois Tigeot 	return ret;
48219df918dSFrançois Tigeot }
48319df918dSFrançois Tigeot 
484e3adcf8fSFrançois Tigeot /**
485e3adcf8fSFrançois Tigeot  * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
486e3adcf8fSFrançois Tigeot  *
487e3adcf8fSFrançois Tigeot  * Not for i915G/i915GM
488e3adcf8fSFrançois Tigeot  *
489e3adcf8fSFrançois Tigeot  * \return true if CRT is connected.
490e3adcf8fSFrançois Tigeot  * \return false if CRT is disconnected.
491e3adcf8fSFrançois Tigeot  */
intel_crt_detect_hotplug(struct drm_connector * connector)492e3adcf8fSFrançois Tigeot static bool intel_crt_detect_hotplug(struct drm_connector *connector)
493e3adcf8fSFrançois Tigeot {
494e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
495303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
496352ff8bdSFrançois Tigeot 	u32 stat;
497e3adcf8fSFrançois Tigeot 	bool ret = false;
498e3adcf8fSFrançois Tigeot 	int i, tries = 0;
499e3adcf8fSFrançois Tigeot 
5001e12ee3bSFrançois Tigeot 	if (HAS_PCH_SPLIT(dev_priv))
501e3adcf8fSFrançois Tigeot 		return intel_ironlake_crt_detect_hotplug(connector);
502e3adcf8fSFrançois Tigeot 
5031e12ee3bSFrançois Tigeot 	if (IS_VALLEYVIEW(dev_priv))
50419df918dSFrançois Tigeot 		return valleyview_crt_detect_hotplug(connector);
50519df918dSFrançois Tigeot 
506e3adcf8fSFrançois Tigeot 	/*
507e3adcf8fSFrançois Tigeot 	 * On 4 series desktop, CRT detect sequence need to be done twice
508e3adcf8fSFrançois Tigeot 	 * to get a reliable result.
509e3adcf8fSFrançois Tigeot 	 */
510e3adcf8fSFrançois Tigeot 
5111e12ee3bSFrançois Tigeot 	if (IS_G4X(dev_priv) && !IS_GM45(dev_priv))
512e3adcf8fSFrançois Tigeot 		tries = 2;
513e3adcf8fSFrançois Tigeot 	else
514e3adcf8fSFrançois Tigeot 		tries = 1;
515e3adcf8fSFrançois Tigeot 
516e3adcf8fSFrançois Tigeot 	for (i = 0; i < tries ; i++) {
517e3adcf8fSFrançois Tigeot 		/* turn on the FORCE_DETECT */
518352ff8bdSFrançois Tigeot 		i915_hotplug_interrupt_update(dev_priv,
519352ff8bdSFrançois Tigeot 					      CRT_HOTPLUG_FORCE_DETECT,
520352ff8bdSFrançois Tigeot 					      CRT_HOTPLUG_FORCE_DETECT);
521e3adcf8fSFrançois Tigeot 		/* wait for FORCE_DETECT to go off */
5221487f786SFrançois Tigeot 		if (intel_wait_for_register(dev_priv, PORT_HOTPLUG_EN,
5231487f786SFrançois Tigeot 					    CRT_HOTPLUG_FORCE_DETECT, 0,
52419df918dSFrançois Tigeot 					    1000))
525e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
526e3adcf8fSFrançois Tigeot 	}
527e3adcf8fSFrançois Tigeot 
528e3adcf8fSFrançois Tigeot 	stat = I915_READ(PORT_HOTPLUG_STAT);
529e3adcf8fSFrançois Tigeot 	if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
530e3adcf8fSFrançois Tigeot 		ret = true;
531e3adcf8fSFrançois Tigeot 
532e3adcf8fSFrançois Tigeot 	/* clear the interrupt we just generated, if any */
533e3adcf8fSFrançois Tigeot 	I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
534e3adcf8fSFrançois Tigeot 
535352ff8bdSFrançois Tigeot 	i915_hotplug_interrupt_update(dev_priv, CRT_HOTPLUG_FORCE_DETECT, 0);
536e3adcf8fSFrançois Tigeot 
537e3adcf8fSFrançois Tigeot 	return ret;
538e3adcf8fSFrançois Tigeot }
539e3adcf8fSFrançois Tigeot 
intel_crt_get_edid(struct drm_connector * connector,struct i2c_adapter * i2c)54019df918dSFrançois Tigeot static struct edid *intel_crt_get_edid(struct drm_connector *connector,
541ba55f2f5SFrançois Tigeot 				struct i2c_adapter *i2c)
54219df918dSFrançois Tigeot {
54319df918dSFrançois Tigeot 	struct edid *edid;
54419df918dSFrançois Tigeot 
54519df918dSFrançois Tigeot 	edid = drm_get_edid(connector, i2c);
54619df918dSFrançois Tigeot 
54719df918dSFrançois Tigeot 	if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
54819df918dSFrançois Tigeot 		DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
54919df918dSFrançois Tigeot 		intel_gmbus_force_bit(i2c, true);
55019df918dSFrançois Tigeot 		edid = drm_get_edid(connector, i2c);
55119df918dSFrançois Tigeot 		intel_gmbus_force_bit(i2c, false);
55219df918dSFrançois Tigeot 	}
55319df918dSFrançois Tigeot 
55419df918dSFrançois Tigeot 	return edid;
55519df918dSFrançois Tigeot }
55619df918dSFrançois Tigeot 
55719df918dSFrançois Tigeot /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
intel_crt_ddc_get_modes(struct drm_connector * connector,struct i2c_adapter * adapter)55819df918dSFrançois Tigeot static int intel_crt_ddc_get_modes(struct drm_connector *connector,
559ba55f2f5SFrançois Tigeot 				struct i2c_adapter *adapter)
56019df918dSFrançois Tigeot {
56119df918dSFrançois Tigeot 	struct edid *edid;
56219df918dSFrançois Tigeot 	int ret;
56319df918dSFrançois Tigeot 
56419df918dSFrançois Tigeot 	edid = intel_crt_get_edid(connector, adapter);
56519df918dSFrançois Tigeot 	if (!edid)
56619df918dSFrançois Tigeot 		return 0;
56719df918dSFrançois Tigeot 
56819df918dSFrançois Tigeot 	ret = intel_connector_update_modes(connector, edid);
569158486a6SFrançois Tigeot 	kfree(edid);
57019df918dSFrançois Tigeot 
57119df918dSFrançois Tigeot 	return ret;
57219df918dSFrançois Tigeot }
57319df918dSFrançois Tigeot 
intel_crt_detect_ddc(struct drm_connector * connector)574e3adcf8fSFrançois Tigeot static bool intel_crt_detect_ddc(struct drm_connector *connector)
575e3adcf8fSFrançois Tigeot {
576e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
577303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(crt->base.base.dev);
578e3adcf8fSFrançois Tigeot 	struct edid *edid;
579ba55f2f5SFrançois Tigeot 	struct i2c_adapter *i2c;
5804be47400SFrançois Tigeot 	bool ret = false;
581e3adcf8fSFrançois Tigeot 
58219df918dSFrançois Tigeot 	BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
58319df918dSFrançois Tigeot 
5845d0b1887SFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
58519df918dSFrançois Tigeot 	edid = intel_crt_get_edid(connector, i2c);
58619df918dSFrançois Tigeot 
58719df918dSFrançois Tigeot 	if (edid) {
58819df918dSFrançois Tigeot 		bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
58919df918dSFrançois Tigeot 
590e3adcf8fSFrançois Tigeot 		/*
591e3adcf8fSFrançois Tigeot 		 * This may be a DVI-I connector with a shared DDC
592e3adcf8fSFrançois Tigeot 		 * link between analog and digital outputs, so we
593e3adcf8fSFrançois Tigeot 		 * have to check the EDID input spec of the attached device.
594e3adcf8fSFrançois Tigeot 		 */
595e3adcf8fSFrançois Tigeot 		if (!is_digital) {
596e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
5974be47400SFrançois Tigeot 			ret = true;
5984be47400SFrançois Tigeot 		} else {
599e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
6004be47400SFrançois Tigeot 		}
60119df918dSFrançois Tigeot 	} else {
60219df918dSFrançois Tigeot 		DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
603e3adcf8fSFrançois Tigeot 	}
60419df918dSFrançois Tigeot 
6055d0b1887SFrançois Tigeot 	kfree(edid);
606e3adcf8fSFrançois Tigeot 
6074be47400SFrançois Tigeot 	return ret;
608e3adcf8fSFrançois Tigeot }
609e3adcf8fSFrançois Tigeot 
610e3adcf8fSFrançois Tigeot static enum drm_connector_status
intel_crt_load_detect(struct intel_crt * crt,uint32_t pipe)611c0e85e96SFrançois Tigeot intel_crt_load_detect(struct intel_crt *crt, uint32_t pipe)
612e3adcf8fSFrançois Tigeot {
613e3adcf8fSFrançois Tigeot 	struct drm_device *dev = crt->base.base.dev;
614303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
615e3adcf8fSFrançois Tigeot 	uint32_t save_bclrpat;
616e3adcf8fSFrançois Tigeot 	uint32_t save_vtotal;
617e3adcf8fSFrançois Tigeot 	uint32_t vtotal, vactive;
618e3adcf8fSFrançois Tigeot 	uint32_t vsample;
619e3adcf8fSFrançois Tigeot 	uint32_t vblank, vblank_start, vblank_end;
620e3adcf8fSFrançois Tigeot 	uint32_t dsl;
621aee94f86SFrançois Tigeot 	i915_reg_t bclrpat_reg, vtotal_reg,
622aee94f86SFrançois Tigeot 		vblank_reg, vsync_reg, pipeconf_reg, pipe_dsl_reg;
623e3adcf8fSFrançois Tigeot 	uint8_t	st00;
624e3adcf8fSFrançois Tigeot 	enum drm_connector_status status;
625e3adcf8fSFrançois Tigeot 
626e3adcf8fSFrançois Tigeot 	DRM_DEBUG_KMS("starting load-detect on CRT\n");
627e3adcf8fSFrançois Tigeot 
628e3adcf8fSFrançois Tigeot 	bclrpat_reg = BCLRPAT(pipe);
629e3adcf8fSFrançois Tigeot 	vtotal_reg = VTOTAL(pipe);
630e3adcf8fSFrançois Tigeot 	vblank_reg = VBLANK(pipe);
631e3adcf8fSFrançois Tigeot 	vsync_reg = VSYNC(pipe);
632e3adcf8fSFrançois Tigeot 	pipeconf_reg = PIPECONF(pipe);
633e3adcf8fSFrançois Tigeot 	pipe_dsl_reg = PIPEDSL(pipe);
634e3adcf8fSFrançois Tigeot 
635e3adcf8fSFrançois Tigeot 	save_bclrpat = I915_READ(bclrpat_reg);
636e3adcf8fSFrançois Tigeot 	save_vtotal = I915_READ(vtotal_reg);
637e3adcf8fSFrançois Tigeot 	vblank = I915_READ(vblank_reg);
638e3adcf8fSFrançois Tigeot 
639e3adcf8fSFrançois Tigeot 	vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
640e3adcf8fSFrançois Tigeot 	vactive = (save_vtotal & 0x7ff) + 1;
641e3adcf8fSFrançois Tigeot 
642e3adcf8fSFrançois Tigeot 	vblank_start = (vblank & 0xfff) + 1;
643e3adcf8fSFrançois Tigeot 	vblank_end = ((vblank >> 16) & 0xfff) + 1;
644e3adcf8fSFrançois Tigeot 
645e3adcf8fSFrançois Tigeot 	/* Set the border color to purple. */
646e3adcf8fSFrançois Tigeot 	I915_WRITE(bclrpat_reg, 0x500050);
647e3adcf8fSFrançois Tigeot 
6481e12ee3bSFrançois Tigeot 	if (!IS_GEN2(dev_priv)) {
649e3adcf8fSFrançois Tigeot 		uint32_t pipeconf = I915_READ(pipeconf_reg);
650e3adcf8fSFrançois Tigeot 		I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
651e3adcf8fSFrançois Tigeot 		POSTING_READ(pipeconf_reg);
652e3adcf8fSFrançois Tigeot 		/* Wait for next Vblank to substitue
653e3adcf8fSFrançois Tigeot 		 * border color for Color info */
6544be47400SFrançois Tigeot 		intel_wait_for_vblank(dev_priv, pipe);
655aee94f86SFrançois Tigeot 		st00 = I915_READ8(_VGA_MSR_WRITE);
656e3adcf8fSFrançois Tigeot 		status = ((st00 & (1 << 4)) != 0) ?
657e3adcf8fSFrançois Tigeot 			connector_status_connected :
658e3adcf8fSFrançois Tigeot 			connector_status_disconnected;
659e3adcf8fSFrançois Tigeot 
660e3adcf8fSFrançois Tigeot 		I915_WRITE(pipeconf_reg, pipeconf);
661e3adcf8fSFrançois Tigeot 	} else {
662e3adcf8fSFrançois Tigeot 		bool restore_vblank = false;
663e3adcf8fSFrançois Tigeot 		int count, detect;
664e3adcf8fSFrançois Tigeot 
665e3adcf8fSFrançois Tigeot 		/*
666e3adcf8fSFrançois Tigeot 		* If there isn't any border, add some.
667e3adcf8fSFrançois Tigeot 		* Yes, this will flicker
668e3adcf8fSFrançois Tigeot 		*/
669e3adcf8fSFrançois Tigeot 		if (vblank_start <= vactive && vblank_end >= vtotal) {
670e3adcf8fSFrançois Tigeot 			uint32_t vsync = I915_READ(vsync_reg);
671e3adcf8fSFrançois Tigeot 			uint32_t vsync_start = (vsync & 0xffff) + 1;
672e3adcf8fSFrançois Tigeot 
673e3adcf8fSFrançois Tigeot 			vblank_start = vsync_start;
674e3adcf8fSFrançois Tigeot 			I915_WRITE(vblank_reg,
675e3adcf8fSFrançois Tigeot 				   (vblank_start - 1) |
676e3adcf8fSFrançois Tigeot 				   ((vblank_end - 1) << 16));
677e3adcf8fSFrançois Tigeot 			restore_vblank = true;
678e3adcf8fSFrançois Tigeot 		}
679e3adcf8fSFrançois Tigeot 		/* sample in the vertical border, selecting the larger one */
680e3adcf8fSFrançois Tigeot 		if (vblank_start - vactive >= vtotal - vblank_end)
681e3adcf8fSFrançois Tigeot 			vsample = (vblank_start + vactive) >> 1;
682e3adcf8fSFrançois Tigeot 		else
683e3adcf8fSFrançois Tigeot 			vsample = (vtotal + vblank_end) >> 1;
684e3adcf8fSFrançois Tigeot 
685e3adcf8fSFrançois Tigeot 		/*
686e3adcf8fSFrançois Tigeot 		 * Wait for the border to be displayed
687e3adcf8fSFrançois Tigeot 		 */
688e3adcf8fSFrançois Tigeot 		while (I915_READ(pipe_dsl_reg) >= vactive)
689e3adcf8fSFrançois Tigeot 			;
690e3adcf8fSFrançois Tigeot 		while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
691e3adcf8fSFrançois Tigeot 			;
692e3adcf8fSFrançois Tigeot 		/*
693e3adcf8fSFrançois Tigeot 		 * Watch ST00 for an entire scanline
694e3adcf8fSFrançois Tigeot 		 */
695e3adcf8fSFrançois Tigeot 		detect = 0;
696e3adcf8fSFrançois Tigeot 		count = 0;
697e3adcf8fSFrançois Tigeot 		do {
698e3adcf8fSFrançois Tigeot 			count++;
699e3adcf8fSFrançois Tigeot 			/* Read the ST00 VGA status register */
700aee94f86SFrançois Tigeot 			st00 = I915_READ8(_VGA_MSR_WRITE);
701e3adcf8fSFrançois Tigeot 			if (st00 & (1 << 4))
702e3adcf8fSFrançois Tigeot 				detect++;
703e3adcf8fSFrançois Tigeot 		} while ((I915_READ(pipe_dsl_reg) == dsl));
704e3adcf8fSFrançois Tigeot 
705e3adcf8fSFrançois Tigeot 		/* restore vblank if necessary */
706e3adcf8fSFrançois Tigeot 		if (restore_vblank)
707e3adcf8fSFrançois Tigeot 			I915_WRITE(vblank_reg, vblank);
708e3adcf8fSFrançois Tigeot 		/*
709e3adcf8fSFrançois Tigeot 		 * If more than 3/4 of the scanline detected a monitor,
710e3adcf8fSFrançois Tigeot 		 * then it is assumed to be present. This works even on i830,
711e3adcf8fSFrançois Tigeot 		 * where there isn't any way to force the border color across
712e3adcf8fSFrançois Tigeot 		 * the screen
713e3adcf8fSFrançois Tigeot 		 */
714e3adcf8fSFrançois Tigeot 		status = detect * 4 > count * 3 ?
715e3adcf8fSFrançois Tigeot 			 connector_status_connected :
716e3adcf8fSFrançois Tigeot 			 connector_status_disconnected;
717e3adcf8fSFrançois Tigeot 	}
718e3adcf8fSFrançois Tigeot 
719e3adcf8fSFrançois Tigeot 	/* Restore previous settings */
720e3adcf8fSFrançois Tigeot 	I915_WRITE(bclrpat_reg, save_bclrpat);
721e3adcf8fSFrançois Tigeot 
722e3adcf8fSFrançois Tigeot 	return status;
723e3adcf8fSFrançois Tigeot }
724e3adcf8fSFrançois Tigeot 
intel_spurious_crt_detect_dmi_callback(const struct dmi_system_id * id)7251e12ee3bSFrançois Tigeot static int intel_spurious_crt_detect_dmi_callback(const struct dmi_system_id *id)
7261e12ee3bSFrançois Tigeot {
7271e12ee3bSFrançois Tigeot 	DRM_DEBUG_DRIVER("Skipping CRT detection for %s\n", id->ident);
7281e12ee3bSFrançois Tigeot 	return 1;
7291e12ee3bSFrançois Tigeot }
7301e12ee3bSFrançois Tigeot 
7311e12ee3bSFrançois Tigeot static const struct dmi_system_id intel_spurious_crt_detect[] = {
7321e12ee3bSFrançois Tigeot 	{
7331e12ee3bSFrançois Tigeot 		.callback = intel_spurious_crt_detect_dmi_callback,
7341e12ee3bSFrançois Tigeot 		.ident = "ACER ZGB",
7351e12ee3bSFrançois Tigeot 		.matches = {
7361e12ee3bSFrançois Tigeot 			DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
7371e12ee3bSFrançois Tigeot 			DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
7381e12ee3bSFrançois Tigeot 		},
7391e12ee3bSFrançois Tigeot 	},
7401e12ee3bSFrançois Tigeot 	{
7411e12ee3bSFrançois Tigeot 		.callback = intel_spurious_crt_detect_dmi_callback,
7421e12ee3bSFrançois Tigeot 		.ident = "Intel DZ77BH-55K",
7431e12ee3bSFrançois Tigeot 		.matches = {
7441e12ee3bSFrançois Tigeot 			DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
7451e12ee3bSFrançois Tigeot 			DMI_MATCH(DMI_BOARD_NAME, "DZ77BH-55K"),
7461e12ee3bSFrançois Tigeot 		},
7471e12ee3bSFrançois Tigeot 	},
7481e12ee3bSFrançois Tigeot 	{ }
7491e12ee3bSFrançois Tigeot };
7501e12ee3bSFrançois Tigeot 
751a85cb24fSFrançois Tigeot static int
intel_crt_detect(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)752a85cb24fSFrançois Tigeot intel_crt_detect(struct drm_connector *connector,
753a85cb24fSFrançois Tigeot 		 struct drm_modeset_acquire_ctx *ctx,
754a85cb24fSFrançois Tigeot 		 bool force)
755e3adcf8fSFrançois Tigeot {
7564be47400SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
757e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
758ba55f2f5SFrançois Tigeot 	struct intel_encoder *intel_encoder = &crt->base;
759a85cb24fSFrançois Tigeot 	int status, ret;
760e3adcf8fSFrançois Tigeot 	struct intel_load_detect_pipe tmp;
761ba55f2f5SFrançois Tigeot 
7629edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
763b4efbf42Szrj 		      connector->base.id, connector->name,
7649edbd4a0SFrançois Tigeot 		      force);
7659edbd4a0SFrançois Tigeot 
7661e12ee3bSFrançois Tigeot 	/* Skip machines without VGA that falsely report hotplug events */
7671e12ee3bSFrançois Tigeot 	if (dmi_check_system(intel_spurious_crt_detect))
7681e12ee3bSFrançois Tigeot 		return connector_status_disconnected;
7691e12ee3bSFrançois Tigeot 
770a85cb24fSFrançois Tigeot 	intel_display_power_get(dev_priv, intel_encoder->power_domain);
771ba55f2f5SFrançois Tigeot 
7724be47400SFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev_priv)) {
77319df918dSFrançois Tigeot 		/* We can not rely on the HPD pin always being correctly wired
77419df918dSFrançois Tigeot 		 * up, for example many KVM do not pass it through, and so
77519df918dSFrançois Tigeot 		 * only trust an assertion that the monitor is connected.
77619df918dSFrançois Tigeot 		 */
777e3adcf8fSFrançois Tigeot 		if (intel_crt_detect_hotplug(connector)) {
778e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT detected via hotplug\n");
779ba55f2f5SFrançois Tigeot 			status = connector_status_connected;
780ba55f2f5SFrançois Tigeot 			goto out;
78119df918dSFrançois Tigeot 		} else
782e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT not detected via hotplug\n");
783e3adcf8fSFrançois Tigeot 	}
784e3adcf8fSFrançois Tigeot 
785ba55f2f5SFrançois Tigeot 	if (intel_crt_detect_ddc(connector)) {
786ba55f2f5SFrançois Tigeot 		status = connector_status_connected;
787ba55f2f5SFrançois Tigeot 		goto out;
788ba55f2f5SFrançois Tigeot 	}
789e3adcf8fSFrançois Tigeot 
79019df918dSFrançois Tigeot 	/* Load detection is broken on HPD capable machines. Whoever wants a
79119df918dSFrançois Tigeot 	 * broken monitor (without edid) to work behind a broken kvm (that fails
79219df918dSFrançois Tigeot 	 * to have the right resistors for HP detection) needs to fix this up.
79319df918dSFrançois Tigeot 	 * For now just bail out. */
794*3f2dd94aSFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev_priv) && !i915_modparams.load_detect_test) {
795ba55f2f5SFrançois Tigeot 		status = connector_status_disconnected;
796ba55f2f5SFrançois Tigeot 		goto out;
797ba55f2f5SFrançois Tigeot 	}
79819df918dSFrançois Tigeot 
799ba55f2f5SFrançois Tigeot 	if (!force) {
800ba55f2f5SFrançois Tigeot 		status = connector->status;
801ba55f2f5SFrançois Tigeot 		goto out;
802ba55f2f5SFrançois Tigeot 	}
803e3adcf8fSFrançois Tigeot 
804e3adcf8fSFrançois Tigeot 	/* for pre-945g platforms use load detect */
805a85cb24fSFrançois Tigeot 	ret = intel_get_load_detect_pipe(connector, NULL, &tmp, ctx);
806a85cb24fSFrançois Tigeot 	if (ret > 0) {
807e3adcf8fSFrançois Tigeot 		if (intel_crt_detect_ddc(connector))
808e3adcf8fSFrançois Tigeot 			status = connector_status_connected;
8094be47400SFrançois Tigeot 		else if (INTEL_GEN(dev_priv) < 4)
810c0e85e96SFrançois Tigeot 			status = intel_crt_load_detect(crt,
811c0e85e96SFrançois Tigeot 				to_intel_crtc(connector->state->crtc)->pipe);
812*3f2dd94aSFrançois Tigeot 		else if (i915_modparams.load_detect_test)
8138621f407SFrançois Tigeot 			status = connector_status_disconnected;
814477eb7f9SFrançois Tigeot 		else
815477eb7f9SFrançois Tigeot 			status = connector_status_unknown;
816a85cb24fSFrançois Tigeot 		intel_release_load_detect_pipe(connector, &tmp, ctx);
817a85cb24fSFrançois Tigeot 	} else if (ret == 0)
818e3adcf8fSFrançois Tigeot 		status = connector_status_unknown;
819a85cb24fSFrançois Tigeot 	else if (ret < 0)
820a85cb24fSFrançois Tigeot 		status = ret;
82124edb884SFrançois Tigeot 
822ba55f2f5SFrançois Tigeot out:
823a85cb24fSFrançois Tigeot 	intel_display_power_put(dev_priv, intel_encoder->power_domain);
824e3adcf8fSFrançois Tigeot 	return status;
825e3adcf8fSFrançois Tigeot }
826e3adcf8fSFrançois Tigeot 
intel_crt_destroy(struct drm_connector * connector)827e3adcf8fSFrançois Tigeot static void intel_crt_destroy(struct drm_connector *connector)
828e3adcf8fSFrançois Tigeot {
829e3adcf8fSFrançois Tigeot 	drm_connector_cleanup(connector);
8305d0b1887SFrançois Tigeot 	kfree(connector);
831e3adcf8fSFrançois Tigeot }
832e3adcf8fSFrançois Tigeot 
intel_crt_get_modes(struct drm_connector * connector)833e3adcf8fSFrançois Tigeot static int intel_crt_get_modes(struct drm_connector *connector)
834e3adcf8fSFrançois Tigeot {
835e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
836303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
837ba55f2f5SFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
838ba55f2f5SFrançois Tigeot 	struct intel_encoder *intel_encoder = &crt->base;
839e3adcf8fSFrançois Tigeot 	int ret;
840ba55f2f5SFrançois Tigeot 	struct i2c_adapter *i2c;
841ba55f2f5SFrançois Tigeot 
842a85cb24fSFrançois Tigeot 	intel_display_power_get(dev_priv, intel_encoder->power_domain);
843e3adcf8fSFrançois Tigeot 
8445d0b1887SFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
84519df918dSFrançois Tigeot 	ret = intel_crt_ddc_get_modes(connector, i2c);
8461e12ee3bSFrançois Tigeot 	if (ret || !IS_G4X(dev_priv))
847ba55f2f5SFrançois Tigeot 		goto out;
848e3adcf8fSFrançois Tigeot 
849e3adcf8fSFrançois Tigeot 	/* Try to probe digital port for output in DVI-I -> VGA mode. */
85019c468b4SFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPB);
851ba55f2f5SFrançois Tigeot 	ret = intel_crt_ddc_get_modes(connector, i2c);
852ba55f2f5SFrançois Tigeot 
853ba55f2f5SFrançois Tigeot out:
854a85cb24fSFrançois Tigeot 	intel_display_power_put(dev_priv, intel_encoder->power_domain);
855ba55f2f5SFrançois Tigeot 
856ba55f2f5SFrançois Tigeot 	return ret;
857e3adcf8fSFrançois Tigeot }
858e3adcf8fSFrançois Tigeot 
intel_crt_reset(struct drm_encoder * encoder)859bf017597SFrançois Tigeot void intel_crt_reset(struct drm_encoder *encoder)
860e3adcf8fSFrançois Tigeot {
8614be47400SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
862bf017597SFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(to_intel_encoder(encoder));
863e3adcf8fSFrançois Tigeot 
8644be47400SFrançois Tigeot 	if (INTEL_GEN(dev_priv) >= 5) {
86519df918dSFrançois Tigeot 		u32 adpa;
86619df918dSFrançois Tigeot 
867a2fdbec6SFrançois Tigeot 		adpa = I915_READ(crt->adpa_reg);
86819df918dSFrançois Tigeot 		adpa &= ~ADPA_CRT_HOTPLUG_MASK;
86919df918dSFrançois Tigeot 		adpa |= ADPA_HOTPLUG_BITS;
870a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, adpa);
871a2fdbec6SFrançois Tigeot 		POSTING_READ(crt->adpa_reg);
87219df918dSFrançois Tigeot 
8732c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("crt adpa set to 0x%x\n", adpa);
874e3adcf8fSFrançois Tigeot 		crt->force_hotplug_required = 1;
875e3adcf8fSFrançois Tigeot 	}
876e3adcf8fSFrançois Tigeot 
87719df918dSFrançois Tigeot }
87819df918dSFrançois Tigeot 
879e3adcf8fSFrançois Tigeot /*
880e3adcf8fSFrançois Tigeot  * Routines for controlling stuff on the analog port
881e3adcf8fSFrançois Tigeot  */
882e3adcf8fSFrançois Tigeot 
883e3adcf8fSFrançois Tigeot static const struct drm_connector_funcs intel_crt_connector_funcs = {
884e3adcf8fSFrançois Tigeot 	.fill_modes = drm_helper_probe_single_connector_modes,
8851487f786SFrançois Tigeot 	.late_register = intel_connector_register,
8861487f786SFrançois Tigeot 	.early_unregister = intel_connector_unregister,
887e3adcf8fSFrançois Tigeot 	.destroy = intel_crt_destroy,
8882c9916cdSFrançois Tigeot 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
889477eb7f9SFrançois Tigeot 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
890e3adcf8fSFrançois Tigeot };
891e3adcf8fSFrançois Tigeot 
892e3adcf8fSFrançois Tigeot static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
893a85cb24fSFrançois Tigeot 	.detect_ctx = intel_crt_detect,
894e3adcf8fSFrançois Tigeot 	.mode_valid = intel_crt_mode_valid,
895e3adcf8fSFrançois Tigeot 	.get_modes = intel_crt_get_modes,
896e3adcf8fSFrançois Tigeot };
897e3adcf8fSFrançois Tigeot 
898e3adcf8fSFrançois Tigeot static const struct drm_encoder_funcs intel_crt_enc_funcs = {
899bf017597SFrançois Tigeot 	.reset = intel_crt_reset,
900e3adcf8fSFrançois Tigeot 	.destroy = intel_encoder_destroy,
901e3adcf8fSFrançois Tigeot };
902e3adcf8fSFrançois Tigeot 
intel_crt_init(struct drm_i915_private * dev_priv)903a85cb24fSFrançois Tigeot void intel_crt_init(struct drm_i915_private *dev_priv)
904e3adcf8fSFrançois Tigeot {
905e3adcf8fSFrançois Tigeot 	struct drm_connector *connector;
906e3adcf8fSFrançois Tigeot 	struct intel_crt *crt;
907e3adcf8fSFrançois Tigeot 	struct intel_connector *intel_connector;
908aee94f86SFrançois Tigeot 	i915_reg_t adpa_reg;
909aee94f86SFrançois Tigeot 	u32 adpa;
910e3adcf8fSFrançois Tigeot 
9111e12ee3bSFrançois Tigeot 	if (HAS_PCH_SPLIT(dev_priv))
912aee94f86SFrançois Tigeot 		adpa_reg = PCH_ADPA;
9131e12ee3bSFrançois Tigeot 	else if (IS_VALLEYVIEW(dev_priv))
914aee94f86SFrançois Tigeot 		adpa_reg = VLV_ADPA;
915aee94f86SFrançois Tigeot 	else
916aee94f86SFrançois Tigeot 		adpa_reg = ADPA;
917aee94f86SFrançois Tigeot 
918aee94f86SFrançois Tigeot 	adpa = I915_READ(adpa_reg);
919aee94f86SFrançois Tigeot 	if ((adpa & ADPA_DAC_ENABLE) == 0) {
920aee94f86SFrançois Tigeot 		/*
921aee94f86SFrançois Tigeot 		 * On some machines (some IVB at least) CRT can be
922aee94f86SFrançois Tigeot 		 * fused off, but there's no known fuse bit to
923aee94f86SFrançois Tigeot 		 * indicate that. On these machine the ADPA register
924aee94f86SFrançois Tigeot 		 * works normally, except the DAC enable bit won't
925aee94f86SFrançois Tigeot 		 * take. So the only way to tell is attempt to enable
926aee94f86SFrançois Tigeot 		 * it and see what happens.
927aee94f86SFrançois Tigeot 		 */
928aee94f86SFrançois Tigeot 		I915_WRITE(adpa_reg, adpa | ADPA_DAC_ENABLE |
929aee94f86SFrançois Tigeot 			   ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
930aee94f86SFrançois Tigeot 		if ((I915_READ(adpa_reg) & ADPA_DAC_ENABLE) == 0)
931aee94f86SFrançois Tigeot 			return;
932aee94f86SFrançois Tigeot 		I915_WRITE(adpa_reg, adpa);
933aee94f86SFrançois Tigeot 	}
934aee94f86SFrançois Tigeot 
935159fc1d7SFrançois Tigeot 	crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
93619df918dSFrançois Tigeot 	if (!crt)
93719df918dSFrançois Tigeot 		return;
93819df918dSFrançois Tigeot 
939477eb7f9SFrançois Tigeot 	intel_connector = intel_connector_alloc();
94019df918dSFrançois Tigeot 	if (!intel_connector) {
941158486a6SFrançois Tigeot 		kfree(crt);
94219df918dSFrançois Tigeot 		return;
94319df918dSFrançois Tigeot 	}
944e3adcf8fSFrançois Tigeot 
945e3adcf8fSFrançois Tigeot 	connector = &intel_connector->base;
94619df918dSFrançois Tigeot 	crt->connector = intel_connector;
947a85cb24fSFrançois Tigeot 	drm_connector_init(&dev_priv->drm, &intel_connector->base,
948e3adcf8fSFrançois Tigeot 			   &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
949e3adcf8fSFrançois Tigeot 
950a85cb24fSFrançois Tigeot 	drm_encoder_init(&dev_priv->drm, &crt->base.base, &intel_crt_enc_funcs,
9511487f786SFrançois Tigeot 			 DRM_MODE_ENCODER_DAC, "CRT");
952e3adcf8fSFrançois Tigeot 
953e3adcf8fSFrançois Tigeot 	intel_connector_attach_encoder(intel_connector, &crt->base);
954e3adcf8fSFrançois Tigeot 
955e3adcf8fSFrançois Tigeot 	crt->base.type = INTEL_OUTPUT_ANALOG;
956ba55f2f5SFrançois Tigeot 	crt->base.cloneable = (1 << INTEL_OUTPUT_DVO) | (1 << INTEL_OUTPUT_HDMI);
9571e12ee3bSFrançois Tigeot 	if (IS_I830(dev_priv))
958e9243325SFrançois Tigeot 		crt->base.crtc_mask = (1 << 0);
959e9243325SFrançois Tigeot 	else
960e9243325SFrançois Tigeot 		crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
961e9243325SFrançois Tigeot 
9621e12ee3bSFrançois Tigeot 	if (IS_GEN2(dev_priv))
963e3adcf8fSFrançois Tigeot 		connector->interlace_allowed = 0;
964e3adcf8fSFrançois Tigeot 	else
965e3adcf8fSFrançois Tigeot 		connector->interlace_allowed = 1;
966e3adcf8fSFrançois Tigeot 	connector->doublescan_allowed = 0;
967e3adcf8fSFrançois Tigeot 
968aee94f86SFrançois Tigeot 	crt->adpa_reg = adpa_reg;
969e9243325SFrançois Tigeot 
970a85cb24fSFrançois Tigeot 	crt->base.power_domain = POWER_DOMAIN_PORT_CRT;
971a85cb24fSFrançois Tigeot 
9724be47400SFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev_priv) &&
9731e12ee3bSFrançois Tigeot 	    !dmi_check_system(intel_spurious_crt_detect))
9748e26cdf6SFrançois Tigeot 		crt->base.hpd_pin = HPD_CRT;
975*3f2dd94aSFrançois Tigeot 
9761e12ee3bSFrançois Tigeot 	if (HAS_DDI(dev_priv)) {
9771e12ee3bSFrançois Tigeot 		crt->base.port = PORT_E;
9789edbd4a0SFrançois Tigeot 		crt->base.get_config = hsw_crt_get_config;
97919df918dSFrançois Tigeot 		crt->base.get_hw_state = intel_ddi_get_hw_state;
980*3f2dd94aSFrançois Tigeot 		crt->base.compute_config = hsw_crt_compute_config;
981*3f2dd94aSFrançois Tigeot 		crt->base.pre_pll_enable = hsw_pre_pll_enable_crt;
982*3f2dd94aSFrançois Tigeot 		crt->base.pre_enable = hsw_pre_enable_crt;
983*3f2dd94aSFrançois Tigeot 		crt->base.enable = hsw_enable_crt;
984*3f2dd94aSFrançois Tigeot 		crt->base.disable = hsw_disable_crt;
9851e12ee3bSFrançois Tigeot 		crt->base.post_disable = hsw_post_disable_crt;
9869edbd4a0SFrançois Tigeot 	} else {
987*3f2dd94aSFrançois Tigeot 		if (HAS_PCH_SPLIT(dev_priv)) {
988*3f2dd94aSFrançois Tigeot 			crt->base.compute_config = pch_crt_compute_config;
989*3f2dd94aSFrançois Tigeot 			crt->base.disable = pch_disable_crt;
990*3f2dd94aSFrançois Tigeot 			crt->base.post_disable = pch_post_disable_crt;
991*3f2dd94aSFrançois Tigeot 		} else {
992*3f2dd94aSFrançois Tigeot 			crt->base.compute_config = intel_crt_compute_config;
993*3f2dd94aSFrançois Tigeot 			crt->base.disable = intel_disable_crt;
994*3f2dd94aSFrançois Tigeot 		}
9951e12ee3bSFrançois Tigeot 		crt->base.port = PORT_NONE;
9969edbd4a0SFrançois Tigeot 		crt->base.get_config = intel_crt_get_config;
99719df918dSFrançois Tigeot 		crt->base.get_hw_state = intel_crt_get_hw_state;
998*3f2dd94aSFrançois Tigeot 		crt->base.enable = intel_enable_crt;
9999edbd4a0SFrançois Tigeot 	}
100019df918dSFrançois Tigeot 	intel_connector->get_hw_state = intel_connector_get_hw_state;
100119df918dSFrançois Tigeot 
1002e3adcf8fSFrançois Tigeot 	drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
1003e3adcf8fSFrançois Tigeot 
10044be47400SFrançois Tigeot 	if (!I915_HAS_HOTPLUG(dev_priv))
10058e26cdf6SFrançois Tigeot 		intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1006e3adcf8fSFrançois Tigeot 
1007e3adcf8fSFrançois Tigeot 	/*
1008e3adcf8fSFrançois Tigeot 	 * Configure the automatic hotplug detection stuff
1009e3adcf8fSFrançois Tigeot 	 */
1010e3adcf8fSFrançois Tigeot 	crt->force_hotplug_required = 0;
1011e3adcf8fSFrançois Tigeot 
101219df918dSFrançois Tigeot 	/*
101319df918dSFrançois Tigeot 	 * TODO: find a proper way to discover whether we need to set the the
101419df918dSFrançois Tigeot 	 * polarity and link reversal bits or not, instead of relying on the
101519df918dSFrançois Tigeot 	 * BIOS.
101619df918dSFrançois Tigeot 	 */
10171e12ee3bSFrançois Tigeot 	if (HAS_PCH_LPT(dev_priv)) {
101819df918dSFrançois Tigeot 		u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
101919df918dSFrançois Tigeot 				 FDI_RX_LINK_REVERSAL_OVERRIDE;
102019df918dSFrançois Tigeot 
1021352ff8bdSFrançois Tigeot 		dev_priv->fdi_rx_config = I915_READ(FDI_RX_CTL(PIPE_A)) & fdi_config;
102219df918dSFrançois Tigeot 	}
1023ba55f2f5SFrançois Tigeot 
1024bf017597SFrançois Tigeot 	intel_crt_reset(&crt->base.base);
1025e3adcf8fSFrançois Tigeot }
1026