xref: /dflybsd-src/sys/dev/drm/i915/intel_crt.c (revision 159fc1d722ce9dd763313e018f34a260d01af9f5)
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 
2718e26a6dSFrançois Tigeot #include <drm/drmP.h>
2818e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
2918e26a6dSFrançois Tigeot #include <drm/drm_crtc_helper.h>
3018e26a6dSFrançois Tigeot #include <drm/drm_edid.h>
3118e26a6dSFrançois Tigeot #include "intel_drv.h"
325c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
33e3adcf8fSFrançois Tigeot #include "i915_drv.h"
34e3adcf8fSFrançois Tigeot 
35e3adcf8fSFrançois Tigeot /* Here's the desired hotplug mode */
36e3adcf8fSFrançois Tigeot #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 |		\
37e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_WARMUP_10MS |		\
38e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_SAMPLE_4S |			\
39e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_VOLTAGE_50 |		\
40e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_VOLREF_325MV |		\
41e3adcf8fSFrançois Tigeot 			   ADPA_CRT_HOTPLUG_ENABLE)
42e3adcf8fSFrançois Tigeot 
43e3adcf8fSFrançois Tigeot struct intel_crt {
44e3adcf8fSFrançois Tigeot 	struct intel_encoder base;
45e9243325SFrançois Tigeot 	/* DPMS state is stored in the connector, which we need in the
46e9243325SFrançois Tigeot 	 * encoder's enable/disable callbacks */
47e9243325SFrançois Tigeot 	struct intel_connector *connector;
48e3adcf8fSFrançois Tigeot 	bool force_hotplug_required;
49e9243325SFrançois Tigeot 	u32 adpa_reg;
50e3adcf8fSFrançois Tigeot };
51e3adcf8fSFrançois Tigeot 
52e3adcf8fSFrançois Tigeot static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
53e3adcf8fSFrançois Tigeot {
54e3adcf8fSFrançois Tigeot 	return container_of(intel_attached_encoder(connector),
55e3adcf8fSFrançois Tigeot 			    struct intel_crt, base);
56e3adcf8fSFrançois Tigeot }
57e3adcf8fSFrançois Tigeot 
5819df918dSFrançois Tigeot static struct intel_crt *intel_encoder_to_crt(struct intel_encoder *encoder)
59e3adcf8fSFrançois Tigeot {
6019df918dSFrançois Tigeot 	return container_of(encoder, struct intel_crt, base);
6119df918dSFrançois Tigeot }
6219df918dSFrançois Tigeot 
6319df918dSFrançois Tigeot static bool intel_crt_get_hw_state(struct intel_encoder *encoder,
6419df918dSFrançois Tigeot 				   enum i915_pipe *pipe)
6519df918dSFrançois Tigeot {
6619df918dSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
67e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
6819df918dSFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
6919df918dSFrançois Tigeot 	u32 tmp;
70e3adcf8fSFrançois Tigeot 
7119df918dSFrançois Tigeot 	tmp = I915_READ(crt->adpa_reg);
7219df918dSFrançois Tigeot 
7319df918dSFrançois Tigeot 	if (!(tmp & ADPA_DAC_ENABLE))
7419df918dSFrançois Tigeot 		return false;
7519df918dSFrançois Tigeot 
7619df918dSFrançois Tigeot 	if (HAS_PCH_CPT(dev))
7719df918dSFrançois Tigeot 		*pipe = PORT_TO_PIPE_CPT(tmp);
78e3adcf8fSFrançois Tigeot 	else
7919df918dSFrançois Tigeot 		*pipe = PORT_TO_PIPE(tmp);
80e3adcf8fSFrançois Tigeot 
8119df918dSFrançois Tigeot 	return true;
8219df918dSFrançois Tigeot }
8319df918dSFrançois Tigeot 
8419df918dSFrançois Tigeot /* Note: The caller is required to filter out dpms modes not supported by the
8519df918dSFrançois Tigeot  * platform. */
8619df918dSFrançois Tigeot static void intel_crt_set_dpms(struct intel_encoder *encoder, int mode)
8719df918dSFrançois Tigeot {
8819df918dSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
8919df918dSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
9019df918dSFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
9119df918dSFrançois Tigeot 	u32 temp;
9219df918dSFrançois Tigeot 
9319df918dSFrançois Tigeot 	temp = I915_READ(crt->adpa_reg);
94e3adcf8fSFrançois Tigeot 	temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
95e3adcf8fSFrançois Tigeot 	temp &= ~ADPA_DAC_ENABLE;
96e3adcf8fSFrançois Tigeot 
97e3adcf8fSFrançois Tigeot 	switch (mode) {
98e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_ON:
99e3adcf8fSFrançois Tigeot 		temp |= ADPA_DAC_ENABLE;
100e3adcf8fSFrançois Tigeot 		break;
101e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_STANDBY:
102e3adcf8fSFrançois Tigeot 		temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
103e3adcf8fSFrançois Tigeot 		break;
104e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_SUSPEND:
105e3adcf8fSFrançois Tigeot 		temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
106e3adcf8fSFrançois Tigeot 		break;
107e3adcf8fSFrançois Tigeot 	case DRM_MODE_DPMS_OFF:
108e3adcf8fSFrançois Tigeot 		temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
109e3adcf8fSFrançois Tigeot 		break;
110e3adcf8fSFrançois Tigeot 	}
111e3adcf8fSFrançois Tigeot 
11219df918dSFrançois Tigeot 	I915_WRITE(crt->adpa_reg, temp);
11319df918dSFrançois Tigeot }
11419df918dSFrançois Tigeot 
11519df918dSFrançois Tigeot static void intel_disable_crt(struct intel_encoder *encoder)
11619df918dSFrançois Tigeot {
11719df918dSFrançois Tigeot 	intel_crt_set_dpms(encoder, DRM_MODE_DPMS_OFF);
11819df918dSFrançois Tigeot }
11919df918dSFrançois Tigeot 
12019df918dSFrançois Tigeot static void intel_enable_crt(struct intel_encoder *encoder)
12119df918dSFrançois Tigeot {
12219df918dSFrançois Tigeot 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
12319df918dSFrançois Tigeot 
12419df918dSFrançois Tigeot 	intel_crt_set_dpms(encoder, crt->connector->base.dpms);
12519df918dSFrançois Tigeot }
12619df918dSFrançois Tigeot 
12719df918dSFrançois Tigeot 
12819df918dSFrançois Tigeot static void intel_crt_dpms(struct drm_connector *connector, int mode)
12919df918dSFrançois Tigeot {
13019df918dSFrançois Tigeot 	struct drm_device *dev = connector->dev;
13119df918dSFrançois Tigeot 	struct intel_encoder *encoder = intel_attached_encoder(connector);
13219df918dSFrançois Tigeot 	struct drm_crtc *crtc;
13319df918dSFrançois Tigeot 	int old_dpms;
13419df918dSFrançois Tigeot 
13519df918dSFrançois Tigeot 	/* PCH platforms and VLV only support on/off. */
13619df918dSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5 && mode != DRM_MODE_DPMS_ON)
13719df918dSFrançois Tigeot 		mode = DRM_MODE_DPMS_OFF;
13819df918dSFrançois Tigeot 
13919df918dSFrançois Tigeot 	if (mode == connector->dpms)
14019df918dSFrançois Tigeot 		return;
14119df918dSFrançois Tigeot 
14219df918dSFrançois Tigeot 	old_dpms = connector->dpms;
14319df918dSFrançois Tigeot 	connector->dpms = mode;
14419df918dSFrançois Tigeot 
14519df918dSFrançois Tigeot 	/* Only need to change hw state when actually enabled */
14619df918dSFrançois Tigeot 	crtc = encoder->base.crtc;
14719df918dSFrançois Tigeot 	if (!crtc) {
14819df918dSFrançois Tigeot 		encoder->connectors_active = false;
14919df918dSFrançois Tigeot 		return;
15019df918dSFrançois Tigeot 	}
15119df918dSFrançois Tigeot 
15219df918dSFrançois Tigeot 	/* We need the pipe to run for anything but OFF. */
15319df918dSFrançois Tigeot 	if (mode == DRM_MODE_DPMS_OFF)
15419df918dSFrançois Tigeot 		encoder->connectors_active = false;
15519df918dSFrançois Tigeot 	else
15619df918dSFrançois Tigeot 		encoder->connectors_active = true;
15719df918dSFrançois Tigeot 
15819df918dSFrançois Tigeot 	if (mode < old_dpms) {
15919df918dSFrançois Tigeot 		/* From off to on, enable the pipe first. */
16019df918dSFrançois Tigeot 		intel_crtc_update_dpms(crtc);
16119df918dSFrançois Tigeot 
16219df918dSFrançois Tigeot 		intel_crt_set_dpms(encoder, mode);
16319df918dSFrançois Tigeot 	} else {
16419df918dSFrançois Tigeot 		intel_crt_set_dpms(encoder, mode);
16519df918dSFrançois Tigeot 
16619df918dSFrançois Tigeot 		intel_crtc_update_dpms(crtc);
16719df918dSFrançois Tigeot 	}
16819df918dSFrançois Tigeot 
16919df918dSFrançois Tigeot 	intel_modeset_check_state(connector->dev);
170e3adcf8fSFrançois Tigeot }
171e3adcf8fSFrançois Tigeot 
172e3adcf8fSFrançois Tigeot static int intel_crt_mode_valid(struct drm_connector *connector,
173e3adcf8fSFrançois Tigeot 				struct drm_display_mode *mode)
174e3adcf8fSFrançois Tigeot {
175e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
176e3adcf8fSFrançois Tigeot 
177e3adcf8fSFrançois Tigeot 	int max_clock = 0;
178e3adcf8fSFrançois Tigeot 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
179e3adcf8fSFrançois Tigeot 		return MODE_NO_DBLESCAN;
180e3adcf8fSFrançois Tigeot 
181e3adcf8fSFrançois Tigeot 	if (mode->clock < 25000)
182e3adcf8fSFrançois Tigeot 		return MODE_CLOCK_LOW;
183e3adcf8fSFrançois Tigeot 
184e3adcf8fSFrançois Tigeot 	if (IS_GEN2(dev))
185e3adcf8fSFrançois Tigeot 		max_clock = 350000;
186e3adcf8fSFrançois Tigeot 	else
187e3adcf8fSFrançois Tigeot 		max_clock = 400000;
188e3adcf8fSFrançois Tigeot 	if (mode->clock > max_clock)
189e3adcf8fSFrançois Tigeot 		return MODE_CLOCK_HIGH;
190e3adcf8fSFrançois Tigeot 
19119df918dSFrançois Tigeot 	/* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
19219df918dSFrançois Tigeot 	if (HAS_PCH_LPT(dev) &&
19319df918dSFrançois Tigeot 	    (ironlake_get_lanes_required(mode->clock, 270000, 24) > 2))
19419df918dSFrançois Tigeot 		return MODE_CLOCK_HIGH;
19519df918dSFrançois Tigeot 
196e3adcf8fSFrançois Tigeot 	return MODE_OK;
197e3adcf8fSFrançois Tigeot }
198e3adcf8fSFrançois Tigeot 
1998e26cdf6SFrançois Tigeot static bool intel_crt_compute_config(struct intel_encoder *encoder,
2008e26cdf6SFrançois Tigeot 				     struct intel_crtc_config *pipe_config)
201e3adcf8fSFrançois Tigeot {
2028e26cdf6SFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
2038e26cdf6SFrançois Tigeot 
2048e26cdf6SFrançois Tigeot 	if (HAS_PCH_SPLIT(dev))
2058e26cdf6SFrançois Tigeot 		pipe_config->has_pch_encoder = true;
2068e26cdf6SFrançois Tigeot 
207e3adcf8fSFrançois Tigeot 	return true;
208e3adcf8fSFrançois Tigeot }
209e3adcf8fSFrançois Tigeot 
210e3adcf8fSFrançois Tigeot static void intel_crt_mode_set(struct drm_encoder *encoder,
211e3adcf8fSFrançois Tigeot 			       struct drm_display_mode *mode,
212e3adcf8fSFrançois Tigeot 			       struct drm_display_mode *adjusted_mode)
213e3adcf8fSFrançois Tigeot {
214e3adcf8fSFrançois Tigeot 
215e3adcf8fSFrançois Tigeot 	struct drm_device *dev = encoder->dev;
216e3adcf8fSFrançois Tigeot 	struct drm_crtc *crtc = encoder->crtc;
21719df918dSFrançois Tigeot 	struct intel_crt *crt =
21819df918dSFrançois Tigeot 		intel_encoder_to_crt(to_intel_encoder(encoder));
219e3adcf8fSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
220e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
22119df918dSFrançois Tigeot 	u32 adpa;
222e3adcf8fSFrançois Tigeot 
223e3adcf8fSFrançois Tigeot 	if (HAS_PCH_SPLIT(dev))
224e3adcf8fSFrançois Tigeot 		adpa = ADPA_HOTPLUG_BITS;
22519df918dSFrançois Tigeot 	else
22619df918dSFrançois Tigeot 		adpa = 0;
22719df918dSFrançois Tigeot 
228e3adcf8fSFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
229e3adcf8fSFrançois Tigeot 		adpa |= ADPA_HSYNC_ACTIVE_HIGH;
230e3adcf8fSFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
231e3adcf8fSFrançois Tigeot 		adpa |= ADPA_VSYNC_ACTIVE_HIGH;
232e3adcf8fSFrançois Tigeot 
233e3adcf8fSFrançois Tigeot 	/* For CPT allow 3 pipe config, for others just use A or B */
23419df918dSFrançois Tigeot 	if (HAS_PCH_LPT(dev))
23519df918dSFrançois Tigeot 		; /* Those bits don't exist here */
23619df918dSFrançois Tigeot 	else if (HAS_PCH_CPT(dev))
237e3adcf8fSFrançois Tigeot 		adpa |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
238e3adcf8fSFrançois Tigeot 	else if (intel_crtc->pipe == 0)
239e3adcf8fSFrançois Tigeot 		adpa |= ADPA_PIPE_A_SELECT;
240e3adcf8fSFrançois Tigeot 	else
241e3adcf8fSFrançois Tigeot 		adpa |= ADPA_PIPE_B_SELECT;
242e3adcf8fSFrançois Tigeot 
243e3adcf8fSFrançois Tigeot 	if (!HAS_PCH_SPLIT(dev))
244e3adcf8fSFrançois Tigeot 		I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);
245e3adcf8fSFrançois Tigeot 
24619df918dSFrançois Tigeot 	I915_WRITE(crt->adpa_reg, adpa);
247e3adcf8fSFrançois Tigeot }
248e3adcf8fSFrançois Tigeot 
249e3adcf8fSFrançois Tigeot static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
250e3adcf8fSFrançois Tigeot {
251e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
252e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
253e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
254e3adcf8fSFrançois Tigeot 	u32 adpa;
255e3adcf8fSFrançois Tigeot 	bool ret;
256e3adcf8fSFrançois Tigeot 
257e3adcf8fSFrançois Tigeot 	/* The first time through, trigger an explicit detection cycle */
258e3adcf8fSFrançois Tigeot 	if (crt->force_hotplug_required) {
259e3adcf8fSFrançois Tigeot 		bool turn_off_dac = HAS_PCH_SPLIT(dev);
260e3adcf8fSFrançois Tigeot 		u32 save_adpa;
261e3adcf8fSFrançois Tigeot 
262e3adcf8fSFrançois Tigeot 		crt->force_hotplug_required = 0;
263e3adcf8fSFrançois Tigeot 
264a2fdbec6SFrançois Tigeot 		save_adpa = adpa = I915_READ(crt->adpa_reg);
265e3adcf8fSFrançois Tigeot 		DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
266e3adcf8fSFrançois Tigeot 
267e3adcf8fSFrançois Tigeot 		adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
268e3adcf8fSFrançois Tigeot 		if (turn_off_dac)
269e3adcf8fSFrançois Tigeot 			adpa &= ~ADPA_DAC_ENABLE;
270e3adcf8fSFrançois Tigeot 
271a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, adpa);
272e3adcf8fSFrançois Tigeot 
273a2fdbec6SFrançois Tigeot 		if (wait_for((I915_READ(crt->adpa_reg) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
27419df918dSFrançois Tigeot 			     1000))
27519df918dSFrançois Tigeot 			DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
276e3adcf8fSFrançois Tigeot 
277e3adcf8fSFrançois Tigeot 		if (turn_off_dac) {
278a2fdbec6SFrançois Tigeot 			I915_WRITE(crt->adpa_reg, save_adpa);
279a2fdbec6SFrançois Tigeot 			POSTING_READ(crt->adpa_reg);
280e3adcf8fSFrançois Tigeot 		}
281e3adcf8fSFrançois Tigeot 	}
282e3adcf8fSFrançois Tigeot 
283e3adcf8fSFrançois Tigeot 	/* Check the status to see if both blue and green are on now */
284a2fdbec6SFrançois Tigeot 	adpa = I915_READ(crt->adpa_reg);
285e3adcf8fSFrançois Tigeot 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
286e3adcf8fSFrançois Tigeot 		ret = true;
287e3adcf8fSFrançois Tigeot 	else
288e3adcf8fSFrançois Tigeot 		ret = false;
289e3adcf8fSFrançois Tigeot 	DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
290e3adcf8fSFrançois Tigeot 
291e3adcf8fSFrançois Tigeot 	return ret;
292e3adcf8fSFrançois Tigeot }
293e3adcf8fSFrançois Tigeot 
29419df918dSFrançois Tigeot static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
29519df918dSFrançois Tigeot {
29619df918dSFrançois Tigeot 	struct drm_device *dev = connector->dev;
297a2fdbec6SFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
29819df918dSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
29919df918dSFrançois Tigeot 	u32 adpa;
30019df918dSFrançois Tigeot 	bool ret;
30119df918dSFrançois Tigeot 	u32 save_adpa;
30219df918dSFrançois Tigeot 
303a2fdbec6SFrançois Tigeot 	save_adpa = adpa = I915_READ(crt->adpa_reg);
30419df918dSFrançois Tigeot 	DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
30519df918dSFrançois Tigeot 
30619df918dSFrançois Tigeot 	adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
30719df918dSFrançois Tigeot 
308a2fdbec6SFrançois Tigeot 	I915_WRITE(crt->adpa_reg, adpa);
30919df918dSFrançois Tigeot 
310a2fdbec6SFrançois Tigeot 	if (wait_for((I915_READ(crt->adpa_reg) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
31119df918dSFrançois Tigeot 		     1000)) {
31219df918dSFrançois Tigeot 		DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
313a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, save_adpa);
31419df918dSFrançois Tigeot 	}
31519df918dSFrançois Tigeot 
31619df918dSFrançois Tigeot 	/* Check the status to see if both blue and green are on now */
317a2fdbec6SFrançois Tigeot 	adpa = I915_READ(crt->adpa_reg);
31819df918dSFrançois Tigeot 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
31919df918dSFrançois Tigeot 		ret = true;
32019df918dSFrançois Tigeot 	else
32119df918dSFrançois Tigeot 		ret = false;
32219df918dSFrançois Tigeot 
32319df918dSFrançois Tigeot 	DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
32419df918dSFrançois Tigeot 
32519df918dSFrançois Tigeot 	/* FIXME: debug force function and remove */
32619df918dSFrançois Tigeot 	ret = true;
32719df918dSFrançois Tigeot 
32819df918dSFrançois Tigeot 	return ret;
32919df918dSFrançois Tigeot }
33019df918dSFrançois Tigeot 
331e3adcf8fSFrançois Tigeot /**
332e3adcf8fSFrançois Tigeot  * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
333e3adcf8fSFrançois Tigeot  *
334e3adcf8fSFrançois Tigeot  * Not for i915G/i915GM
335e3adcf8fSFrançois Tigeot  *
336e3adcf8fSFrançois Tigeot  * \return true if CRT is connected.
337e3adcf8fSFrançois Tigeot  * \return false if CRT is disconnected.
338e3adcf8fSFrançois Tigeot  */
339e3adcf8fSFrançois Tigeot static bool intel_crt_detect_hotplug(struct drm_connector *connector)
340e3adcf8fSFrançois Tigeot {
341e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
342e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
343e3adcf8fSFrançois Tigeot 	u32 hotplug_en, orig, stat;
344e3adcf8fSFrançois Tigeot 	bool ret = false;
345e3adcf8fSFrançois Tigeot 	int i, tries = 0;
346e3adcf8fSFrançois Tigeot 
347e3adcf8fSFrançois Tigeot 	if (HAS_PCH_SPLIT(dev))
348e3adcf8fSFrançois Tigeot 		return intel_ironlake_crt_detect_hotplug(connector);
349e3adcf8fSFrançois Tigeot 
35019df918dSFrançois Tigeot 	if (IS_VALLEYVIEW(dev))
35119df918dSFrançois Tigeot 		return valleyview_crt_detect_hotplug(connector);
35219df918dSFrançois Tigeot 
353e3adcf8fSFrançois Tigeot 	/*
354e3adcf8fSFrançois Tigeot 	 * On 4 series desktop, CRT detect sequence need to be done twice
355e3adcf8fSFrançois Tigeot 	 * to get a reliable result.
356e3adcf8fSFrançois Tigeot 	 */
357e3adcf8fSFrançois Tigeot 
358e3adcf8fSFrançois Tigeot 	if (IS_G4X(dev) && !IS_GM45(dev))
359e3adcf8fSFrançois Tigeot 		tries = 2;
360e3adcf8fSFrançois Tigeot 	else
361e3adcf8fSFrançois Tigeot 		tries = 1;
362e3adcf8fSFrançois Tigeot 	hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
363e3adcf8fSFrançois Tigeot 	hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
364e3adcf8fSFrançois Tigeot 
365e3adcf8fSFrançois Tigeot 	for (i = 0; i < tries ; i++) {
366e3adcf8fSFrançois Tigeot 		/* turn on the FORCE_DETECT */
367e3adcf8fSFrançois Tigeot 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
368e3adcf8fSFrançois Tigeot 		/* wait for FORCE_DETECT to go off */
36919df918dSFrançois Tigeot 		if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
37019df918dSFrançois Tigeot 			      CRT_HOTPLUG_FORCE_DETECT) == 0,
37119df918dSFrançois Tigeot 			     1000))
372e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
373e3adcf8fSFrançois Tigeot 	}
374e3adcf8fSFrançois Tigeot 
375e3adcf8fSFrançois Tigeot 	stat = I915_READ(PORT_HOTPLUG_STAT);
376e3adcf8fSFrançois Tigeot 	if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
377e3adcf8fSFrançois Tigeot 		ret = true;
378e3adcf8fSFrançois Tigeot 
379e3adcf8fSFrançois Tigeot 	/* clear the interrupt we just generated, if any */
380e3adcf8fSFrançois Tigeot 	I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
381e3adcf8fSFrançois Tigeot 
382e3adcf8fSFrançois Tigeot 	/* and put the bits back */
383e3adcf8fSFrançois Tigeot 	I915_WRITE(PORT_HOTPLUG_EN, orig);
384e3adcf8fSFrançois Tigeot 
385e3adcf8fSFrançois Tigeot 	return ret;
386e3adcf8fSFrançois Tigeot }
387e3adcf8fSFrançois Tigeot 
38819df918dSFrançois Tigeot static struct edid *intel_crt_get_edid(struct drm_connector *connector,
38919df918dSFrançois Tigeot 				struct device *i2c)
39019df918dSFrançois Tigeot {
39119df918dSFrançois Tigeot 	struct edid *edid;
39219df918dSFrançois Tigeot 
39319df918dSFrançois Tigeot 	edid = drm_get_edid(connector, i2c);
39419df918dSFrançois Tigeot 
39519df918dSFrançois Tigeot 	if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
39619df918dSFrançois Tigeot 		DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
39719df918dSFrançois Tigeot 		intel_gmbus_force_bit(i2c, true);
39819df918dSFrançois Tigeot 		edid = drm_get_edid(connector, i2c);
39919df918dSFrançois Tigeot 		intel_gmbus_force_bit(i2c, false);
40019df918dSFrançois Tigeot 	}
40119df918dSFrançois Tigeot 
40219df918dSFrançois Tigeot 	return edid;
40319df918dSFrançois Tigeot }
40419df918dSFrançois Tigeot 
40519df918dSFrançois Tigeot /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
40619df918dSFrançois Tigeot static int intel_crt_ddc_get_modes(struct drm_connector *connector,
40719df918dSFrançois Tigeot 				struct device *adapter)
40819df918dSFrançois Tigeot {
40919df918dSFrançois Tigeot 	struct edid *edid;
41019df918dSFrançois Tigeot 	int ret;
41119df918dSFrançois Tigeot 
41219df918dSFrançois Tigeot 	edid = intel_crt_get_edid(connector, adapter);
41319df918dSFrançois Tigeot 	if (!edid)
41419df918dSFrançois Tigeot 		return 0;
41519df918dSFrançois Tigeot 
41619df918dSFrançois Tigeot 	ret = intel_connector_update_modes(connector, edid);
417158486a6SFrançois Tigeot 	kfree(edid);
41819df918dSFrançois Tigeot 
41919df918dSFrançois Tigeot 	return ret;
42019df918dSFrançois Tigeot }
42119df918dSFrançois Tigeot 
422e3adcf8fSFrançois Tigeot static bool intel_crt_detect_ddc(struct drm_connector *connector)
423e3adcf8fSFrançois Tigeot {
424e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
425e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
426e3adcf8fSFrançois Tigeot 	struct edid *edid;
42719df918dSFrançois Tigeot 	struct device *i2c;
428e3adcf8fSFrançois Tigeot 
42919df918dSFrançois Tigeot 	BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
43019df918dSFrançois Tigeot 
43119df918dSFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
43219df918dSFrançois Tigeot 	edid = intel_crt_get_edid(connector, i2c);
43319df918dSFrançois Tigeot 
43419df918dSFrançois Tigeot 	if (edid) {
43519df918dSFrançois Tigeot 		bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
43619df918dSFrançois Tigeot 
437e3adcf8fSFrançois Tigeot 		/*
438e3adcf8fSFrançois Tigeot 		 * This may be a DVI-I connector with a shared DDC
439e3adcf8fSFrançois Tigeot 		 * link between analog and digital outputs, so we
440e3adcf8fSFrançois Tigeot 		 * have to check the EDID input spec of the attached device.
441e3adcf8fSFrançois Tigeot 		 */
442e3adcf8fSFrançois Tigeot 		if (!is_digital) {
443e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
444e3adcf8fSFrançois Tigeot 			return true;
44519df918dSFrançois Tigeot 		}
44619df918dSFrançois Tigeot 
447e3adcf8fSFrançois Tigeot 		DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
44819df918dSFrançois Tigeot 	} else {
44919df918dSFrançois Tigeot 		DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
450e3adcf8fSFrançois Tigeot 	}
45119df918dSFrançois Tigeot 
4525a3b77d5SFrançois Tigeot 	drm_free(edid, M_DRM);
453e3adcf8fSFrançois Tigeot 
454e3adcf8fSFrançois Tigeot 	return false;
455e3adcf8fSFrançois Tigeot }
456e3adcf8fSFrançois Tigeot 
457e3adcf8fSFrançois Tigeot static enum drm_connector_status
458e3adcf8fSFrançois Tigeot intel_crt_load_detect(struct intel_crt *crt)
459e3adcf8fSFrançois Tigeot {
460e3adcf8fSFrançois Tigeot 	struct drm_device *dev = crt->base.base.dev;
461e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
462e3adcf8fSFrançois Tigeot 	uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
463e3adcf8fSFrançois Tigeot 	uint32_t save_bclrpat;
464e3adcf8fSFrançois Tigeot 	uint32_t save_vtotal;
465e3adcf8fSFrançois Tigeot 	uint32_t vtotal, vactive;
466e3adcf8fSFrançois Tigeot 	uint32_t vsample;
467e3adcf8fSFrançois Tigeot 	uint32_t vblank, vblank_start, vblank_end;
468e3adcf8fSFrançois Tigeot 	uint32_t dsl;
469e3adcf8fSFrançois Tigeot 	uint32_t bclrpat_reg;
470e3adcf8fSFrançois Tigeot 	uint32_t vtotal_reg;
471e3adcf8fSFrançois Tigeot 	uint32_t vblank_reg;
472e3adcf8fSFrançois Tigeot 	uint32_t vsync_reg;
473e3adcf8fSFrançois Tigeot 	uint32_t pipeconf_reg;
474e3adcf8fSFrançois Tigeot 	uint32_t pipe_dsl_reg;
475e3adcf8fSFrançois Tigeot 	uint8_t	st00;
476e3adcf8fSFrançois Tigeot 	enum drm_connector_status status;
477e3adcf8fSFrançois Tigeot 
478e3adcf8fSFrançois Tigeot 	DRM_DEBUG_KMS("starting load-detect on CRT\n");
479e3adcf8fSFrançois Tigeot 
480e3adcf8fSFrançois Tigeot 	bclrpat_reg = BCLRPAT(pipe);
481e3adcf8fSFrançois Tigeot 	vtotal_reg = VTOTAL(pipe);
482e3adcf8fSFrançois Tigeot 	vblank_reg = VBLANK(pipe);
483e3adcf8fSFrançois Tigeot 	vsync_reg = VSYNC(pipe);
484e3adcf8fSFrançois Tigeot 	pipeconf_reg = PIPECONF(pipe);
485e3adcf8fSFrançois Tigeot 	pipe_dsl_reg = PIPEDSL(pipe);
486e3adcf8fSFrançois Tigeot 
487e3adcf8fSFrançois Tigeot 	save_bclrpat = I915_READ(bclrpat_reg);
488e3adcf8fSFrançois Tigeot 	save_vtotal = I915_READ(vtotal_reg);
489e3adcf8fSFrançois Tigeot 	vblank = I915_READ(vblank_reg);
490e3adcf8fSFrançois Tigeot 
491e3adcf8fSFrançois Tigeot 	vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
492e3adcf8fSFrançois Tigeot 	vactive = (save_vtotal & 0x7ff) + 1;
493e3adcf8fSFrançois Tigeot 
494e3adcf8fSFrançois Tigeot 	vblank_start = (vblank & 0xfff) + 1;
495e3adcf8fSFrançois Tigeot 	vblank_end = ((vblank >> 16) & 0xfff) + 1;
496e3adcf8fSFrançois Tigeot 
497e3adcf8fSFrançois Tigeot 	/* Set the border color to purple. */
498e3adcf8fSFrançois Tigeot 	I915_WRITE(bclrpat_reg, 0x500050);
499e3adcf8fSFrançois Tigeot 
500e3adcf8fSFrançois Tigeot 	if (!IS_GEN2(dev)) {
501e3adcf8fSFrançois Tigeot 		uint32_t pipeconf = I915_READ(pipeconf_reg);
502e3adcf8fSFrançois Tigeot 		I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
503e3adcf8fSFrançois Tigeot 		POSTING_READ(pipeconf_reg);
504e3adcf8fSFrançois Tigeot 		/* Wait for next Vblank to substitue
505e3adcf8fSFrançois Tigeot 		 * border color for Color info */
506e3adcf8fSFrançois Tigeot 		intel_wait_for_vblank(dev, pipe);
507e3adcf8fSFrançois Tigeot 		st00 = I915_READ8(VGA_MSR_WRITE);
508e3adcf8fSFrançois Tigeot 		status = ((st00 & (1 << 4)) != 0) ?
509e3adcf8fSFrançois Tigeot 			connector_status_connected :
510e3adcf8fSFrançois Tigeot 			connector_status_disconnected;
511e3adcf8fSFrançois Tigeot 
512e3adcf8fSFrançois Tigeot 		I915_WRITE(pipeconf_reg, pipeconf);
513e3adcf8fSFrançois Tigeot 	} else {
514e3adcf8fSFrançois Tigeot 		bool restore_vblank = false;
515e3adcf8fSFrançois Tigeot 		int count, detect;
516e3adcf8fSFrançois Tigeot 
517e3adcf8fSFrançois Tigeot 		/*
518e3adcf8fSFrançois Tigeot 		* If there isn't any border, add some.
519e3adcf8fSFrançois Tigeot 		* Yes, this will flicker
520e3adcf8fSFrançois Tigeot 		*/
521e3adcf8fSFrançois Tigeot 		if (vblank_start <= vactive && vblank_end >= vtotal) {
522e3adcf8fSFrançois Tigeot 			uint32_t vsync = I915_READ(vsync_reg);
523e3adcf8fSFrançois Tigeot 			uint32_t vsync_start = (vsync & 0xffff) + 1;
524e3adcf8fSFrançois Tigeot 
525e3adcf8fSFrançois Tigeot 			vblank_start = vsync_start;
526e3adcf8fSFrançois Tigeot 			I915_WRITE(vblank_reg,
527e3adcf8fSFrançois Tigeot 				   (vblank_start - 1) |
528e3adcf8fSFrançois Tigeot 				   ((vblank_end - 1) << 16));
529e3adcf8fSFrançois Tigeot 			restore_vblank = true;
530e3adcf8fSFrançois Tigeot 		}
531e3adcf8fSFrançois Tigeot 		/* sample in the vertical border, selecting the larger one */
532e3adcf8fSFrançois Tigeot 		if (vblank_start - vactive >= vtotal - vblank_end)
533e3adcf8fSFrançois Tigeot 			vsample = (vblank_start + vactive) >> 1;
534e3adcf8fSFrançois Tigeot 		else
535e3adcf8fSFrançois Tigeot 			vsample = (vtotal + vblank_end) >> 1;
536e3adcf8fSFrançois Tigeot 
537e3adcf8fSFrançois Tigeot 		/*
538e3adcf8fSFrançois Tigeot 		 * Wait for the border to be displayed
539e3adcf8fSFrançois Tigeot 		 */
540e3adcf8fSFrançois Tigeot 		while (I915_READ(pipe_dsl_reg) >= vactive)
541e3adcf8fSFrançois Tigeot 			;
542e3adcf8fSFrançois Tigeot 		while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
543e3adcf8fSFrançois Tigeot 			;
544e3adcf8fSFrançois Tigeot 		/*
545e3adcf8fSFrançois Tigeot 		 * Watch ST00 for an entire scanline
546e3adcf8fSFrançois Tigeot 		 */
547e3adcf8fSFrançois Tigeot 		detect = 0;
548e3adcf8fSFrançois Tigeot 		count = 0;
549e3adcf8fSFrançois Tigeot 		do {
550e3adcf8fSFrançois Tigeot 			count++;
551e3adcf8fSFrançois Tigeot 			/* Read the ST00 VGA status register */
552e3adcf8fSFrançois Tigeot 			st00 = I915_READ8(VGA_MSR_WRITE);
553e3adcf8fSFrançois Tigeot 			if (st00 & (1 << 4))
554e3adcf8fSFrançois Tigeot 				detect++;
555e3adcf8fSFrançois Tigeot 		} while ((I915_READ(pipe_dsl_reg) == dsl));
556e3adcf8fSFrançois Tigeot 
557e3adcf8fSFrançois Tigeot 		/* restore vblank if necessary */
558e3adcf8fSFrançois Tigeot 		if (restore_vblank)
559e3adcf8fSFrançois Tigeot 			I915_WRITE(vblank_reg, vblank);
560e3adcf8fSFrançois Tigeot 		/*
561e3adcf8fSFrançois Tigeot 		 * If more than 3/4 of the scanline detected a monitor,
562e3adcf8fSFrançois Tigeot 		 * then it is assumed to be present. This works even on i830,
563e3adcf8fSFrançois Tigeot 		 * where there isn't any way to force the border color across
564e3adcf8fSFrançois Tigeot 		 * the screen
565e3adcf8fSFrançois Tigeot 		 */
566e3adcf8fSFrançois Tigeot 		status = detect * 4 > count * 3 ?
567e3adcf8fSFrançois Tigeot 			 connector_status_connected :
568e3adcf8fSFrançois Tigeot 			 connector_status_disconnected;
569e3adcf8fSFrançois Tigeot 	}
570e3adcf8fSFrançois Tigeot 
571e3adcf8fSFrançois Tigeot 	/* Restore previous settings */
572e3adcf8fSFrançois Tigeot 	I915_WRITE(bclrpat_reg, save_bclrpat);
573e3adcf8fSFrançois Tigeot 
574e3adcf8fSFrançois Tigeot 	return status;
575e3adcf8fSFrançois Tigeot }
576e3adcf8fSFrançois Tigeot 
577e3adcf8fSFrançois Tigeot static enum drm_connector_status
578e3adcf8fSFrançois Tigeot intel_crt_detect(struct drm_connector *connector, bool force)
579e3adcf8fSFrançois Tigeot {
580e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
581e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
582e3adcf8fSFrançois Tigeot 	enum drm_connector_status status;
583e3adcf8fSFrançois Tigeot 	struct intel_load_detect_pipe tmp;
584e3adcf8fSFrançois Tigeot 
585e3adcf8fSFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev)) {
58619df918dSFrançois Tigeot 		/* We can not rely on the HPD pin always being correctly wired
58719df918dSFrançois Tigeot 		 * up, for example many KVM do not pass it through, and so
58819df918dSFrançois Tigeot 		 * only trust an assertion that the monitor is connected.
58919df918dSFrançois Tigeot 		 */
590e3adcf8fSFrançois Tigeot 		if (intel_crt_detect_hotplug(connector)) {
591e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT detected via hotplug\n");
592e3adcf8fSFrançois Tigeot 			return connector_status_connected;
59319df918dSFrançois Tigeot 		} else
594e3adcf8fSFrançois Tigeot 			DRM_DEBUG_KMS("CRT not detected via hotplug\n");
595e3adcf8fSFrançois Tigeot 	}
596e3adcf8fSFrançois Tigeot 
597e3adcf8fSFrançois Tigeot 	if (intel_crt_detect_ddc(connector))
598e3adcf8fSFrançois Tigeot 		return connector_status_connected;
599e3adcf8fSFrançois Tigeot 
60019df918dSFrançois Tigeot 	/* Load detection is broken on HPD capable machines. Whoever wants a
60119df918dSFrançois Tigeot 	 * broken monitor (without edid) to work behind a broken kvm (that fails
60219df918dSFrançois Tigeot 	 * to have the right resistors for HP detection) needs to fix this up.
60319df918dSFrançois Tigeot 	 * For now just bail out. */
60419df918dSFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev))
60519df918dSFrançois Tigeot 		return connector_status_disconnected;
60619df918dSFrançois Tigeot 
607e3adcf8fSFrançois Tigeot 	if (!force)
608e3adcf8fSFrançois Tigeot 		return connector->status;
609e3adcf8fSFrançois Tigeot 
610e3adcf8fSFrançois Tigeot 	/* for pre-945g platforms use load detect */
61119df918dSFrançois Tigeot 	if (intel_get_load_detect_pipe(connector, NULL, &tmp)) {
612e3adcf8fSFrançois Tigeot 		if (intel_crt_detect_ddc(connector))
613e3adcf8fSFrançois Tigeot 			status = connector_status_connected;
614e3adcf8fSFrançois Tigeot 		else
615e3adcf8fSFrançois Tigeot 			status = intel_crt_load_detect(crt);
61619df918dSFrançois Tigeot 		intel_release_load_detect_pipe(connector, &tmp);
617e3adcf8fSFrançois Tigeot 	} else
618e3adcf8fSFrançois Tigeot 		status = connector_status_unknown;
619e3adcf8fSFrançois Tigeot 
620e3adcf8fSFrançois Tigeot 	return status;
621e3adcf8fSFrançois Tigeot }
622e3adcf8fSFrançois Tigeot 
623e3adcf8fSFrançois Tigeot static void intel_crt_destroy(struct drm_connector *connector)
624e3adcf8fSFrançois Tigeot {
625e3adcf8fSFrançois Tigeot #if 0
626e3adcf8fSFrançois Tigeot 	drm_sysfs_connector_remove(connector);
627e3adcf8fSFrançois Tigeot #endif
628e3adcf8fSFrançois Tigeot 	drm_connector_cleanup(connector);
6295a3b77d5SFrançois Tigeot 	drm_free(connector, M_DRM);
630e3adcf8fSFrançois Tigeot }
631e3adcf8fSFrançois Tigeot 
632e3adcf8fSFrançois Tigeot static int intel_crt_get_modes(struct drm_connector *connector)
633e3adcf8fSFrançois Tigeot {
634e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
635e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
636e3adcf8fSFrançois Tigeot 	int ret;
63719df918dSFrançois Tigeot 	struct device *i2c;
638e3adcf8fSFrançois Tigeot 
63919df918dSFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
64019df918dSFrançois Tigeot 	ret = intel_crt_ddc_get_modes(connector, i2c);
641e3adcf8fSFrançois Tigeot 	if (ret || !IS_G4X(dev))
642e3adcf8fSFrançois Tigeot 		return ret;
643e3adcf8fSFrançois Tigeot 
644e3adcf8fSFrançois Tigeot 	/* Try to probe digital port for output in DVI-I -> VGA mode. */
64519df918dSFrançois Tigeot 	i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
64619df918dSFrançois Tigeot 	return intel_crt_ddc_get_modes(connector, i2c);
647e3adcf8fSFrançois Tigeot }
648e3adcf8fSFrançois Tigeot 
649e3adcf8fSFrançois Tigeot static int intel_crt_set_property(struct drm_connector *connector,
650e3adcf8fSFrançois Tigeot 				  struct drm_property *property,
651e3adcf8fSFrançois Tigeot 				  uint64_t value)
652e3adcf8fSFrançois Tigeot {
653e3adcf8fSFrançois Tigeot 	return 0;
654e3adcf8fSFrançois Tigeot }
655e3adcf8fSFrançois Tigeot 
656e3adcf8fSFrançois Tigeot static void intel_crt_reset(struct drm_connector *connector)
657e3adcf8fSFrançois Tigeot {
658e3adcf8fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
65919df918dSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
660e3adcf8fSFrançois Tigeot 	struct intel_crt *crt = intel_attached_crt(connector);
661e3adcf8fSFrançois Tigeot 
66219df918dSFrançois Tigeot 	if (HAS_PCH_SPLIT(dev)) {
66319df918dSFrançois Tigeot 		u32 adpa;
66419df918dSFrançois Tigeot 
665a2fdbec6SFrançois Tigeot 		adpa = I915_READ(crt->adpa_reg);
66619df918dSFrançois Tigeot 		adpa &= ~ADPA_CRT_HOTPLUG_MASK;
66719df918dSFrançois Tigeot 		adpa |= ADPA_HOTPLUG_BITS;
668a2fdbec6SFrançois Tigeot 		I915_WRITE(crt->adpa_reg, adpa);
669a2fdbec6SFrançois Tigeot 		POSTING_READ(crt->adpa_reg);
67019df918dSFrançois Tigeot 
67119df918dSFrançois Tigeot 		DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);
672e3adcf8fSFrançois Tigeot 		crt->force_hotplug_required = 1;
673e3adcf8fSFrançois Tigeot 	}
674e3adcf8fSFrançois Tigeot 
67519df918dSFrançois Tigeot }
67619df918dSFrançois Tigeot 
677e3adcf8fSFrançois Tigeot /*
678e3adcf8fSFrançois Tigeot  * Routines for controlling stuff on the analog port
679e3adcf8fSFrançois Tigeot  */
680e3adcf8fSFrançois Tigeot 
68119df918dSFrançois Tigeot static const struct drm_encoder_helper_funcs crt_encoder_funcs = {
682e3adcf8fSFrançois Tigeot 	.mode_set = intel_crt_mode_set,
68319df918dSFrançois Tigeot 	.disable = intel_encoder_noop,
684e3adcf8fSFrançois Tigeot };
685e3adcf8fSFrançois Tigeot 
686e3adcf8fSFrançois Tigeot static const struct drm_connector_funcs intel_crt_connector_funcs = {
687e3adcf8fSFrançois Tigeot 	.reset = intel_crt_reset,
68819df918dSFrançois Tigeot 	.dpms = intel_crt_dpms,
689e3adcf8fSFrançois Tigeot 	.detect = intel_crt_detect,
690e3adcf8fSFrançois Tigeot 	.fill_modes = drm_helper_probe_single_connector_modes,
691e3adcf8fSFrançois Tigeot 	.destroy = intel_crt_destroy,
692e3adcf8fSFrançois Tigeot 	.set_property = intel_crt_set_property,
693e3adcf8fSFrançois Tigeot };
694e3adcf8fSFrançois Tigeot 
695e3adcf8fSFrançois Tigeot static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
696e3adcf8fSFrançois Tigeot 	.mode_valid = intel_crt_mode_valid,
697e3adcf8fSFrançois Tigeot 	.get_modes = intel_crt_get_modes,
698e3adcf8fSFrançois Tigeot 	.best_encoder = intel_best_encoder,
699e3adcf8fSFrançois Tigeot };
700e3adcf8fSFrançois Tigeot 
701e3adcf8fSFrançois Tigeot static const struct drm_encoder_funcs intel_crt_enc_funcs = {
702e3adcf8fSFrançois Tigeot 	.destroy = intel_encoder_destroy,
703e3adcf8fSFrançois Tigeot };
704e3adcf8fSFrançois Tigeot 
70519df918dSFrançois Tigeot static int __init intel_no_crt_dmi_callback(const struct dmi_system_id *id)
706e3adcf8fSFrançois Tigeot {
70719df918dSFrançois Tigeot 	DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
708e3adcf8fSFrançois Tigeot 	return 1;
709e3adcf8fSFrançois Tigeot }
710e3adcf8fSFrançois Tigeot 
711e3adcf8fSFrançois Tigeot static const struct dmi_system_id intel_no_crt[] = {
712e3adcf8fSFrançois Tigeot 	{
713e3adcf8fSFrançois Tigeot 		.callback = intel_no_crt_dmi_callback,
714e3adcf8fSFrançois Tigeot 		.ident = "ACER ZGB",
715e3adcf8fSFrançois Tigeot 		.matches = {
716e3adcf8fSFrançois Tigeot 			DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
717e3adcf8fSFrançois Tigeot 			DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
718e3adcf8fSFrançois Tigeot 		},
719e3adcf8fSFrançois Tigeot 	},
720e3adcf8fSFrançois Tigeot 	{ }
721e3adcf8fSFrançois Tigeot };
722e3adcf8fSFrançois Tigeot 
723e3adcf8fSFrançois Tigeot void intel_crt_init(struct drm_device *dev)
724e3adcf8fSFrançois Tigeot {
725e3adcf8fSFrançois Tigeot 	struct drm_connector *connector;
726e3adcf8fSFrançois Tigeot 	struct intel_crt *crt;
727e3adcf8fSFrançois Tigeot 	struct intel_connector *intel_connector;
728e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
729e3adcf8fSFrançois Tigeot 
730e3adcf8fSFrançois Tigeot 	/* Skip machines without VGA that falsely report hotplug events */
731e3adcf8fSFrançois Tigeot 	if (dmi_check_system(intel_no_crt))
732e3adcf8fSFrançois Tigeot 		return;
733e3adcf8fSFrançois Tigeot 
734*159fc1d7SFrançois Tigeot 	crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
73519df918dSFrançois Tigeot 	if (!crt)
73619df918dSFrançois Tigeot 		return;
73719df918dSFrançois Tigeot 
738*159fc1d7SFrançois Tigeot 	intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
73919df918dSFrançois Tigeot 	if (!intel_connector) {
740158486a6SFrançois Tigeot 		kfree(crt);
74119df918dSFrançois Tigeot 		return;
74219df918dSFrançois Tigeot 	}
743e3adcf8fSFrançois Tigeot 
744e3adcf8fSFrançois Tigeot 	connector = &intel_connector->base;
74519df918dSFrançois Tigeot 	crt->connector = intel_connector;
746e3adcf8fSFrançois Tigeot 	drm_connector_init(dev, &intel_connector->base,
747e3adcf8fSFrançois Tigeot 			   &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
748e3adcf8fSFrançois Tigeot 
749e3adcf8fSFrançois Tigeot 	drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
750e3adcf8fSFrançois Tigeot 			 DRM_MODE_ENCODER_DAC);
751e3adcf8fSFrançois Tigeot 
752e3adcf8fSFrançois Tigeot 	intel_connector_attach_encoder(intel_connector, &crt->base);
753e3adcf8fSFrançois Tigeot 
754e3adcf8fSFrançois Tigeot 	crt->base.type = INTEL_OUTPUT_ANALOG;
75519df918dSFrançois Tigeot 	crt->base.cloneable = true;
756e9243325SFrançois Tigeot 	if (IS_I830(dev))
757e9243325SFrançois Tigeot 		crt->base.crtc_mask = (1 << 0);
758e9243325SFrançois Tigeot 	else
759e9243325SFrançois Tigeot 		crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
760e9243325SFrançois Tigeot 
761e3adcf8fSFrançois Tigeot 	if (IS_GEN2(dev))
762e3adcf8fSFrançois Tigeot 		connector->interlace_allowed = 0;
763e3adcf8fSFrançois Tigeot 	else
764e3adcf8fSFrançois Tigeot 		connector->interlace_allowed = 1;
765e3adcf8fSFrançois Tigeot 	connector->doublescan_allowed = 0;
766e3adcf8fSFrançois Tigeot 
767e9243325SFrançois Tigeot 	if (HAS_PCH_SPLIT(dev))
768e9243325SFrançois Tigeot 		crt->adpa_reg = PCH_ADPA;
769e9243325SFrançois Tigeot 	else if (IS_VALLEYVIEW(dev))
770e9243325SFrançois Tigeot 		crt->adpa_reg = VLV_ADPA;
771e9243325SFrançois Tigeot 	else
772e9243325SFrançois Tigeot 		crt->adpa_reg = ADPA;
773e9243325SFrançois Tigeot 
7748e26cdf6SFrançois Tigeot 	crt->base.compute_config = intel_crt_compute_config;
77519df918dSFrançois Tigeot 	crt->base.disable = intel_disable_crt;
77619df918dSFrançois Tigeot 	crt->base.enable = intel_enable_crt;
7778e26cdf6SFrançois Tigeot 	if (I915_HAS_HOTPLUG(dev))
7788e26cdf6SFrançois Tigeot 		crt->base.hpd_pin = HPD_CRT;
779a2fdbec6SFrançois Tigeot 	if (HAS_DDI(dev))
78019df918dSFrançois Tigeot 		crt->base.get_hw_state = intel_ddi_get_hw_state;
78119df918dSFrançois Tigeot 	else
78219df918dSFrançois Tigeot 		crt->base.get_hw_state = intel_crt_get_hw_state;
78319df918dSFrançois Tigeot 	intel_connector->get_hw_state = intel_connector_get_hw_state;
78419df918dSFrançois Tigeot 
78519df918dSFrançois Tigeot 	drm_encoder_helper_add(&crt->base.base, &crt_encoder_funcs);
786e3adcf8fSFrançois Tigeot 	drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
787e3adcf8fSFrançois Tigeot 
788e3adcf8fSFrançois Tigeot #if 0
789e3adcf8fSFrançois Tigeot 	drm_sysfs_connector_add(connector);
790e3adcf8fSFrançois Tigeot #endif
791e3adcf8fSFrançois Tigeot 
7928e26cdf6SFrançois Tigeot 	if (!I915_HAS_HOTPLUG(dev))
7938e26cdf6SFrançois Tigeot 		intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
794e3adcf8fSFrançois Tigeot 
795e3adcf8fSFrançois Tigeot 	/*
796e3adcf8fSFrançois Tigeot 	 * Configure the automatic hotplug detection stuff
797e3adcf8fSFrançois Tigeot 	 */
798e3adcf8fSFrançois Tigeot 	crt->force_hotplug_required = 0;
799e3adcf8fSFrançois Tigeot 
80019df918dSFrançois Tigeot 	/*
80119df918dSFrançois Tigeot 	 * TODO: find a proper way to discover whether we need to set the the
80219df918dSFrançois Tigeot 	 * polarity and link reversal bits or not, instead of relying on the
80319df918dSFrançois Tigeot 	 * BIOS.
80419df918dSFrançois Tigeot 	 */
80519df918dSFrançois Tigeot 	if (HAS_PCH_LPT(dev)) {
80619df918dSFrançois Tigeot 		u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
80719df918dSFrançois Tigeot 				 FDI_RX_LINK_REVERSAL_OVERRIDE;
80819df918dSFrançois Tigeot 
80919df918dSFrançois Tigeot 		dev_priv->fdi_rx_config = I915_READ(_FDI_RXA_CTL) & fdi_config;
81019df918dSFrançois Tigeot 	}
811e3adcf8fSFrançois Tigeot }
812