xref: /linux/drivers/gpu/drm/i915/display/intel_crt.c (revision ab475966455ce285c2c9978a3e3bfe97d75ff8d4)
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Eric Anholt <eric@anholt.net>
25  */
26 
27 #include <linux/dmi.h>
28 #include <linux/i2c.h>
29 #include <linux/slab.h>
30 
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_edid.h>
34 #include <drm/drm_probe_helper.h>
35 
36 #include "i915_drv.h"
37 #include "i915_irq.h"
38 #include "i915_reg.h"
39 #include "intel_connector.h"
40 #include "intel_crt.h"
41 #include "intel_crtc.h"
42 #include "intel_ddi.h"
43 #include "intel_ddi_buf_trans.h"
44 #include "intel_de.h"
45 #include "intel_display_types.h"
46 #include "intel_fdi.h"
47 #include "intel_fdi_regs.h"
48 #include "intel_fifo_underrun.h"
49 #include "intel_gmbus.h"
50 #include "intel_hotplug.h"
51 #include "intel_hotplug_irq.h"
52 #include "intel_load_detect.h"
53 #include "intel_pch_display.h"
54 #include "intel_pch_refclk.h"
55 
56 /* Here's the desired hotplug mode */
57 #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 |		\
58 			   ADPA_CRT_HOTPLUG_WARMUP_10MS |		\
59 			   ADPA_CRT_HOTPLUG_SAMPLE_4S |			\
60 			   ADPA_CRT_HOTPLUG_VOLTAGE_50 |		\
61 			   ADPA_CRT_HOTPLUG_VOLREF_325MV |		\
62 			   ADPA_CRT_HOTPLUG_ENABLE)
63 
64 struct intel_crt {
65 	struct intel_encoder base;
66 	/* DPMS state is stored in the connector, which we need in the
67 	 * encoder's enable/disable callbacks */
68 	struct intel_connector *connector;
69 	bool force_hotplug_required;
70 	i915_reg_t adpa_reg;
71 };
72 
73 static struct intel_crt *intel_encoder_to_crt(struct intel_encoder *encoder)
74 {
75 	return container_of(encoder, struct intel_crt, base);
76 }
77 
78 static struct intel_crt *intel_attached_crt(struct intel_connector *connector)
79 {
80 	return intel_encoder_to_crt(intel_attached_encoder(connector));
81 }
82 
83 bool intel_crt_port_enabled(struct drm_i915_private *dev_priv,
84 			    i915_reg_t adpa_reg, enum pipe *pipe)
85 {
86 	u32 val;
87 
88 	val = intel_de_read(dev_priv, adpa_reg);
89 
90 	/* asserts want to know the pipe even if the port is disabled */
91 	if (HAS_PCH_CPT(dev_priv))
92 		*pipe = (val & ADPA_PIPE_SEL_MASK_CPT) >> ADPA_PIPE_SEL_SHIFT_CPT;
93 	else
94 		*pipe = (val & ADPA_PIPE_SEL_MASK) >> ADPA_PIPE_SEL_SHIFT;
95 
96 	return val & ADPA_DAC_ENABLE;
97 }
98 
99 static bool intel_crt_get_hw_state(struct intel_encoder *encoder,
100 				   enum pipe *pipe)
101 {
102 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
103 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
104 	intel_wakeref_t wakeref;
105 	bool ret;
106 
107 	wakeref = intel_display_power_get_if_enabled(dev_priv,
108 						     encoder->power_domain);
109 	if (!wakeref)
110 		return false;
111 
112 	ret = intel_crt_port_enabled(dev_priv, crt->adpa_reg, pipe);
113 
114 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
115 
116 	return ret;
117 }
118 
119 static unsigned int intel_crt_get_flags(struct intel_encoder *encoder)
120 {
121 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
122 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
123 	u32 tmp, flags = 0;
124 
125 	tmp = intel_de_read(dev_priv, crt->adpa_reg);
126 
127 	if (tmp & ADPA_HSYNC_ACTIVE_HIGH)
128 		flags |= DRM_MODE_FLAG_PHSYNC;
129 	else
130 		flags |= DRM_MODE_FLAG_NHSYNC;
131 
132 	if (tmp & ADPA_VSYNC_ACTIVE_HIGH)
133 		flags |= DRM_MODE_FLAG_PVSYNC;
134 	else
135 		flags |= DRM_MODE_FLAG_NVSYNC;
136 
137 	return flags;
138 }
139 
140 static void intel_crt_get_config(struct intel_encoder *encoder,
141 				 struct intel_crtc_state *pipe_config)
142 {
143 	pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
144 
145 	pipe_config->hw.adjusted_mode.flags |= intel_crt_get_flags(encoder);
146 
147 	pipe_config->hw.adjusted_mode.crtc_clock = pipe_config->port_clock;
148 }
149 
150 static void hsw_crt_get_config(struct intel_encoder *encoder,
151 			       struct intel_crtc_state *pipe_config)
152 {
153 	lpt_pch_get_config(pipe_config);
154 
155 	hsw_ddi_get_config(encoder, pipe_config);
156 
157 	pipe_config->hw.adjusted_mode.flags &= ~(DRM_MODE_FLAG_PHSYNC |
158 					      DRM_MODE_FLAG_NHSYNC |
159 					      DRM_MODE_FLAG_PVSYNC |
160 					      DRM_MODE_FLAG_NVSYNC);
161 	pipe_config->hw.adjusted_mode.flags |= intel_crt_get_flags(encoder);
162 }
163 
164 /* Note: The caller is required to filter out dpms modes not supported by the
165  * platform. */
166 static void intel_crt_set_dpms(struct intel_encoder *encoder,
167 			       const struct intel_crtc_state *crtc_state,
168 			       int mode)
169 {
170 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
171 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
172 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
173 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
174 	u32 adpa;
175 
176 	if (DISPLAY_VER(dev_priv) >= 5)
177 		adpa = ADPA_HOTPLUG_BITS;
178 	else
179 		adpa = 0;
180 
181 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
182 		adpa |= ADPA_HSYNC_ACTIVE_HIGH;
183 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
184 		adpa |= ADPA_VSYNC_ACTIVE_HIGH;
185 
186 	/* For CPT allow 3 pipe config, for others just use A or B */
187 	if (HAS_PCH_LPT(dev_priv))
188 		; /* Those bits don't exist here */
189 	else if (HAS_PCH_CPT(dev_priv))
190 		adpa |= ADPA_PIPE_SEL_CPT(crtc->pipe);
191 	else
192 		adpa |= ADPA_PIPE_SEL(crtc->pipe);
193 
194 	if (!HAS_PCH_SPLIT(dev_priv))
195 		intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
196 
197 	switch (mode) {
198 	case DRM_MODE_DPMS_ON:
199 		adpa |= ADPA_DAC_ENABLE;
200 		break;
201 	case DRM_MODE_DPMS_STANDBY:
202 		adpa |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
203 		break;
204 	case DRM_MODE_DPMS_SUSPEND:
205 		adpa |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
206 		break;
207 	case DRM_MODE_DPMS_OFF:
208 		adpa |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
209 		break;
210 	}
211 
212 	intel_de_write(dev_priv, crt->adpa_reg, adpa);
213 }
214 
215 static void intel_disable_crt(struct intel_atomic_state *state,
216 			      struct intel_encoder *encoder,
217 			      const struct intel_crtc_state *old_crtc_state,
218 			      const struct drm_connector_state *old_conn_state)
219 {
220 	intel_crt_set_dpms(encoder, old_crtc_state, DRM_MODE_DPMS_OFF);
221 }
222 
223 static void pch_disable_crt(struct intel_atomic_state *state,
224 			    struct intel_encoder *encoder,
225 			    const struct intel_crtc_state *old_crtc_state,
226 			    const struct drm_connector_state *old_conn_state)
227 {
228 }
229 
230 static void pch_post_disable_crt(struct intel_atomic_state *state,
231 				 struct intel_encoder *encoder,
232 				 const struct intel_crtc_state *old_crtc_state,
233 				 const struct drm_connector_state *old_conn_state)
234 {
235 	intel_disable_crt(state, encoder, old_crtc_state, old_conn_state);
236 }
237 
238 static void hsw_disable_crt(struct intel_atomic_state *state,
239 			    struct intel_encoder *encoder,
240 			    const struct intel_crtc_state *old_crtc_state,
241 			    const struct drm_connector_state *old_conn_state)
242 {
243 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
244 
245 	drm_WARN_ON(&dev_priv->drm, !old_crtc_state->has_pch_encoder);
246 
247 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
248 }
249 
250 static void hsw_post_disable_crt(struct intel_atomic_state *state,
251 				 struct intel_encoder *encoder,
252 				 const struct intel_crtc_state *old_crtc_state,
253 				 const struct drm_connector_state *old_conn_state)
254 {
255 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
256 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
257 
258 	intel_crtc_vblank_off(old_crtc_state);
259 
260 	intel_disable_transcoder(old_crtc_state);
261 
262 	intel_ddi_disable_transcoder_func(old_crtc_state);
263 
264 	ilk_pfit_disable(old_crtc_state);
265 
266 	intel_ddi_disable_transcoder_clock(old_crtc_state);
267 
268 	pch_post_disable_crt(state, encoder, old_crtc_state, old_conn_state);
269 
270 	lpt_pch_disable(state, crtc);
271 
272 	hsw_fdi_disable(encoder);
273 
274 	drm_WARN_ON(&dev_priv->drm, !old_crtc_state->has_pch_encoder);
275 
276 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
277 }
278 
279 static void hsw_pre_pll_enable_crt(struct intel_atomic_state *state,
280 				   struct intel_encoder *encoder,
281 				   const struct intel_crtc_state *crtc_state,
282 				   const struct drm_connector_state *conn_state)
283 {
284 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
285 
286 	drm_WARN_ON(&dev_priv->drm, !crtc_state->has_pch_encoder);
287 
288 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
289 }
290 
291 static void hsw_pre_enable_crt(struct intel_atomic_state *state,
292 			       struct intel_encoder *encoder,
293 			       const struct intel_crtc_state *crtc_state,
294 			       const struct drm_connector_state *conn_state)
295 {
296 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
297 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
298 	enum pipe pipe = crtc->pipe;
299 
300 	drm_WARN_ON(&dev_priv->drm, !crtc_state->has_pch_encoder);
301 
302 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
303 
304 	hsw_fdi_link_train(encoder, crtc_state);
305 
306 	intel_ddi_enable_transcoder_clock(encoder, crtc_state);
307 }
308 
309 static void hsw_enable_crt(struct intel_atomic_state *state,
310 			   struct intel_encoder *encoder,
311 			   const struct intel_crtc_state *crtc_state,
312 			   const struct drm_connector_state *conn_state)
313 {
314 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
315 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
316 	enum pipe pipe = crtc->pipe;
317 
318 	drm_WARN_ON(&dev_priv->drm, !crtc_state->has_pch_encoder);
319 
320 	intel_ddi_enable_transcoder_func(encoder, crtc_state);
321 
322 	intel_enable_transcoder(crtc_state);
323 
324 	lpt_pch_enable(state, crtc);
325 
326 	intel_crtc_vblank_on(crtc_state);
327 
328 	intel_crt_set_dpms(encoder, crtc_state, DRM_MODE_DPMS_ON);
329 
330 	intel_crtc_wait_for_next_vblank(crtc);
331 	intel_crtc_wait_for_next_vblank(crtc);
332 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
333 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
334 }
335 
336 static void intel_enable_crt(struct intel_atomic_state *state,
337 			     struct intel_encoder *encoder,
338 			     const struct intel_crtc_state *crtc_state,
339 			     const struct drm_connector_state *conn_state)
340 {
341 	intel_crt_set_dpms(encoder, crtc_state, DRM_MODE_DPMS_ON);
342 }
343 
344 static enum drm_mode_status
345 intel_crt_mode_valid(struct drm_connector *connector,
346 		     struct drm_display_mode *mode)
347 {
348 	struct drm_device *dev = connector->dev;
349 	struct drm_i915_private *dev_priv = to_i915(dev);
350 	int max_dotclk = dev_priv->max_dotclk_freq;
351 	int max_clock;
352 
353 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
354 		return MODE_NO_DBLESCAN;
355 
356 	if (mode->clock < 25000)
357 		return MODE_CLOCK_LOW;
358 
359 	if (HAS_PCH_LPT(dev_priv))
360 		max_clock = 180000;
361 	else if (IS_VALLEYVIEW(dev_priv))
362 		/*
363 		 * 270 MHz due to current DPLL limits,
364 		 * DAC limit supposedly 355 MHz.
365 		 */
366 		max_clock = 270000;
367 	else if (IS_DISPLAY_VER(dev_priv, 3, 4))
368 		max_clock = 400000;
369 	else
370 		max_clock = 350000;
371 	if (mode->clock > max_clock)
372 		return MODE_CLOCK_HIGH;
373 
374 	if (mode->clock > max_dotclk)
375 		return MODE_CLOCK_HIGH;
376 
377 	/* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
378 	if (HAS_PCH_LPT(dev_priv) &&
379 	    ilk_get_lanes_required(mode->clock, 270000, 24) > 2)
380 		return MODE_CLOCK_HIGH;
381 
382 	/* HSW/BDW FDI limited to 4k */
383 	if (mode->hdisplay > 4096)
384 		return MODE_H_ILLEGAL;
385 
386 	return MODE_OK;
387 }
388 
389 static int intel_crt_compute_config(struct intel_encoder *encoder,
390 				    struct intel_crtc_state *pipe_config,
391 				    struct drm_connector_state *conn_state)
392 {
393 	struct drm_display_mode *adjusted_mode =
394 		&pipe_config->hw.adjusted_mode;
395 
396 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
397 		return -EINVAL;
398 
399 	pipe_config->sink_format = INTEL_OUTPUT_FORMAT_RGB;
400 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
401 
402 	return 0;
403 }
404 
405 static int pch_crt_compute_config(struct intel_encoder *encoder,
406 				  struct intel_crtc_state *pipe_config,
407 				  struct drm_connector_state *conn_state)
408 {
409 	struct drm_display_mode *adjusted_mode =
410 		&pipe_config->hw.adjusted_mode;
411 
412 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
413 		return -EINVAL;
414 
415 	pipe_config->has_pch_encoder = true;
416 	if (!intel_fdi_compute_pipe_bpp(pipe_config))
417 		return -EINVAL;
418 
419 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
420 
421 	return 0;
422 }
423 
424 static int hsw_crt_compute_config(struct intel_encoder *encoder,
425 				  struct intel_crtc_state *pipe_config,
426 				  struct drm_connector_state *conn_state)
427 {
428 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
429 	struct drm_display_mode *adjusted_mode =
430 		&pipe_config->hw.adjusted_mode;
431 
432 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
433 		return -EINVAL;
434 
435 	/* HSW/BDW FDI limited to 4k */
436 	if (adjusted_mode->crtc_hdisplay > 4096 ||
437 	    adjusted_mode->crtc_hblank_start > 4096)
438 		return -EINVAL;
439 
440 	pipe_config->has_pch_encoder = true;
441 	if (!intel_fdi_compute_pipe_bpp(pipe_config))
442 		return -EINVAL;
443 
444 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
445 
446 	/* LPT FDI RX only supports 8bpc. */
447 	if (HAS_PCH_LPT(dev_priv)) {
448 		/* TODO: Check crtc_state->max_link_bpp_x16 instead of bw_constrained */
449 		if (pipe_config->bw_constrained && pipe_config->pipe_bpp < 24) {
450 			drm_dbg_kms(&dev_priv->drm,
451 				    "LPT only supports 24bpp\n");
452 			return -EINVAL;
453 		}
454 
455 		pipe_config->pipe_bpp = 24;
456 	}
457 
458 	/* FDI must always be 2.7 GHz */
459 	pipe_config->port_clock = 135000 * 2;
460 
461 	pipe_config->enhanced_framing = true;
462 
463 	adjusted_mode->crtc_clock = lpt_iclkip(pipe_config);
464 
465 	return 0;
466 }
467 
468 static bool ilk_crt_detect_hotplug(struct drm_connector *connector)
469 {
470 	struct drm_device *dev = connector->dev;
471 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
472 	struct drm_i915_private *dev_priv = to_i915(dev);
473 	u32 adpa;
474 	bool ret;
475 
476 	/* The first time through, trigger an explicit detection cycle */
477 	if (crt->force_hotplug_required) {
478 		bool turn_off_dac = HAS_PCH_SPLIT(dev_priv);
479 		u32 save_adpa;
480 
481 		crt->force_hotplug_required = false;
482 
483 		save_adpa = adpa = intel_de_read(dev_priv, crt->adpa_reg);
484 		drm_dbg_kms(&dev_priv->drm,
485 			    "trigger hotplug detect cycle: adpa=0x%x\n", adpa);
486 
487 		adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
488 		if (turn_off_dac)
489 			adpa &= ~ADPA_DAC_ENABLE;
490 
491 		intel_de_write(dev_priv, crt->adpa_reg, adpa);
492 
493 		if (intel_de_wait_for_clear(dev_priv,
494 					    crt->adpa_reg,
495 					    ADPA_CRT_HOTPLUG_FORCE_TRIGGER,
496 					    1000))
497 			drm_dbg_kms(&dev_priv->drm,
498 				    "timed out waiting for FORCE_TRIGGER");
499 
500 		if (turn_off_dac) {
501 			intel_de_write(dev_priv, crt->adpa_reg, save_adpa);
502 			intel_de_posting_read(dev_priv, crt->adpa_reg);
503 		}
504 	}
505 
506 	/* Check the status to see if both blue and green are on now */
507 	adpa = intel_de_read(dev_priv, crt->adpa_reg);
508 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
509 		ret = true;
510 	else
511 		ret = false;
512 	drm_dbg_kms(&dev_priv->drm, "ironlake hotplug adpa=0x%x, result %d\n",
513 		    adpa, ret);
514 
515 	return ret;
516 }
517 
518 static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
519 {
520 	struct drm_device *dev = connector->dev;
521 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
522 	struct drm_i915_private *dev_priv = to_i915(dev);
523 	bool reenable_hpd;
524 	u32 adpa;
525 	bool ret;
526 	u32 save_adpa;
527 
528 	/*
529 	 * Doing a force trigger causes a hpd interrupt to get sent, which can
530 	 * get us stuck in a loop if we're polling:
531 	 *  - We enable power wells and reset the ADPA
532 	 *  - output_poll_exec does force probe on VGA, triggering a hpd
533 	 *  - HPD handler waits for poll to unlock dev->mode_config.mutex
534 	 *  - output_poll_exec shuts off the ADPA, unlocks
535 	 *    dev->mode_config.mutex
536 	 *  - HPD handler runs, resets ADPA and brings us back to the start
537 	 *
538 	 * Just disable HPD interrupts here to prevent this
539 	 */
540 	reenable_hpd = intel_hpd_disable(dev_priv, crt->base.hpd_pin);
541 
542 	save_adpa = adpa = intel_de_read(dev_priv, crt->adpa_reg);
543 	drm_dbg_kms(&dev_priv->drm,
544 		    "trigger hotplug detect cycle: adpa=0x%x\n", adpa);
545 
546 	adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
547 
548 	intel_de_write(dev_priv, crt->adpa_reg, adpa);
549 
550 	if (intel_de_wait_for_clear(dev_priv, crt->adpa_reg,
551 				    ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 1000)) {
552 		drm_dbg_kms(&dev_priv->drm,
553 			    "timed out waiting for FORCE_TRIGGER");
554 		intel_de_write(dev_priv, crt->adpa_reg, save_adpa);
555 	}
556 
557 	/* Check the status to see if both blue and green are on now */
558 	adpa = intel_de_read(dev_priv, crt->adpa_reg);
559 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
560 		ret = true;
561 	else
562 		ret = false;
563 
564 	drm_dbg_kms(&dev_priv->drm,
565 		    "valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
566 
567 	if (reenable_hpd)
568 		intel_hpd_enable(dev_priv, crt->base.hpd_pin);
569 
570 	return ret;
571 }
572 
573 static bool intel_crt_detect_hotplug(struct drm_connector *connector)
574 {
575 	struct drm_device *dev = connector->dev;
576 	struct drm_i915_private *dev_priv = to_i915(dev);
577 	u32 stat;
578 	bool ret = false;
579 	int i, tries = 0;
580 
581 	if (HAS_PCH_SPLIT(dev_priv))
582 		return ilk_crt_detect_hotplug(connector);
583 
584 	if (IS_VALLEYVIEW(dev_priv))
585 		return valleyview_crt_detect_hotplug(connector);
586 
587 	/*
588 	 * On 4 series desktop, CRT detect sequence need to be done twice
589 	 * to get a reliable result.
590 	 */
591 
592 	if (IS_G45(dev_priv))
593 		tries = 2;
594 	else
595 		tries = 1;
596 
597 	for (i = 0; i < tries ; i++) {
598 		/* turn on the FORCE_DETECT */
599 		i915_hotplug_interrupt_update(dev_priv,
600 					      CRT_HOTPLUG_FORCE_DETECT,
601 					      CRT_HOTPLUG_FORCE_DETECT);
602 		/* wait for FORCE_DETECT to go off */
603 		if (intel_de_wait_for_clear(dev_priv, PORT_HOTPLUG_EN,
604 					    CRT_HOTPLUG_FORCE_DETECT, 1000))
605 			drm_dbg_kms(&dev_priv->drm,
606 				    "timed out waiting for FORCE_DETECT to go off");
607 	}
608 
609 	stat = intel_de_read(dev_priv, PORT_HOTPLUG_STAT);
610 	if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
611 		ret = true;
612 
613 	/* clear the interrupt we just generated, if any */
614 	intel_de_write(dev_priv, PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
615 
616 	i915_hotplug_interrupt_update(dev_priv, CRT_HOTPLUG_FORCE_DETECT, 0);
617 
618 	return ret;
619 }
620 
621 static const struct drm_edid *intel_crt_get_edid(struct drm_connector *connector,
622 						 struct i2c_adapter *ddc)
623 {
624 	const struct drm_edid *drm_edid;
625 
626 	drm_edid = drm_edid_read_ddc(connector, ddc);
627 
628 	if (!drm_edid && !intel_gmbus_is_forced_bit(ddc)) {
629 		drm_dbg_kms(connector->dev,
630 			    "CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
631 		intel_gmbus_force_bit(ddc, true);
632 		drm_edid = drm_edid_read_ddc(connector, ddc);
633 		intel_gmbus_force_bit(ddc, false);
634 	}
635 
636 	return drm_edid;
637 }
638 
639 /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
640 static int intel_crt_ddc_get_modes(struct drm_connector *connector,
641 				   struct i2c_adapter *ddc)
642 {
643 	const struct drm_edid *drm_edid;
644 	int ret;
645 
646 	drm_edid = intel_crt_get_edid(connector, ddc);
647 	if (!drm_edid)
648 		return 0;
649 
650 	ret = intel_connector_update_modes(connector, drm_edid);
651 
652 	drm_edid_free(drm_edid);
653 
654 	return ret;
655 }
656 
657 static bool intel_crt_detect_ddc(struct drm_connector *connector)
658 {
659 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
660 	struct drm_i915_private *dev_priv = to_i915(crt->base.base.dev);
661 	const struct drm_edid *drm_edid;
662 	bool ret = false;
663 
664 	drm_edid = intel_crt_get_edid(connector, connector->ddc);
665 
666 	if (drm_edid) {
667 		/*
668 		 * This may be a DVI-I connector with a shared DDC
669 		 * link between analog and digital outputs, so we
670 		 * have to check the EDID input spec of the attached device.
671 		 */
672 		if (drm_edid_is_digital(drm_edid)) {
673 			drm_dbg_kms(&dev_priv->drm,
674 				    "CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
675 		} else {
676 			drm_dbg_kms(&dev_priv->drm,
677 				    "CRT detected via DDC:0x50 [EDID]\n");
678 			ret = true;
679 		}
680 	} else {
681 		drm_dbg_kms(&dev_priv->drm,
682 			    "CRT not detected via DDC:0x50 [no valid EDID found]\n");
683 	}
684 
685 	drm_edid_free(drm_edid);
686 
687 	return ret;
688 }
689 
690 static enum drm_connector_status
691 intel_crt_load_detect(struct intel_crt *crt, enum pipe pipe)
692 {
693 	struct drm_device *dev = crt->base.base.dev;
694 	struct drm_i915_private *dev_priv = to_i915(dev);
695 	enum transcoder cpu_transcoder = (enum transcoder)pipe;
696 	u32 save_bclrpat;
697 	u32 save_vtotal;
698 	u32 vtotal, vactive;
699 	u32 vsample;
700 	u32 vblank, vblank_start, vblank_end;
701 	u32 dsl;
702 	u8 st00;
703 	enum drm_connector_status status;
704 
705 	drm_dbg_kms(&dev_priv->drm, "starting load-detect on CRT\n");
706 
707 	save_bclrpat = intel_de_read(dev_priv, BCLRPAT(cpu_transcoder));
708 	save_vtotal = intel_de_read(dev_priv, TRANS_VTOTAL(cpu_transcoder));
709 	vblank = intel_de_read(dev_priv, TRANS_VBLANK(cpu_transcoder));
710 
711 	vtotal = REG_FIELD_GET(VTOTAL_MASK, save_vtotal) + 1;
712 	vactive = REG_FIELD_GET(VACTIVE_MASK, save_vtotal) + 1;
713 
714 	vblank_start = REG_FIELD_GET(VBLANK_START_MASK, vblank) + 1;
715 	vblank_end = REG_FIELD_GET(VBLANK_END_MASK, vblank) + 1;
716 
717 	/* Set the border color to purple. */
718 	intel_de_write(dev_priv, BCLRPAT(cpu_transcoder), 0x500050);
719 
720 	if (DISPLAY_VER(dev_priv) != 2) {
721 		u32 transconf = intel_de_read(dev_priv, TRANSCONF(cpu_transcoder));
722 
723 		intel_de_write(dev_priv, TRANSCONF(cpu_transcoder),
724 			       transconf | TRANSCONF_FORCE_BORDER);
725 		intel_de_posting_read(dev_priv, TRANSCONF(cpu_transcoder));
726 		/* Wait for next Vblank to substitue
727 		 * border color for Color info */
728 		intel_crtc_wait_for_next_vblank(intel_crtc_for_pipe(dev_priv, pipe));
729 		st00 = intel_de_read8(dev_priv, _VGA_MSR_WRITE);
730 		status = ((st00 & (1 << 4)) != 0) ?
731 			connector_status_connected :
732 			connector_status_disconnected;
733 
734 		intel_de_write(dev_priv, TRANSCONF(cpu_transcoder), transconf);
735 	} else {
736 		bool restore_vblank = false;
737 		int count, detect;
738 
739 		/*
740 		* If there isn't any border, add some.
741 		* Yes, this will flicker
742 		*/
743 		if (vblank_start <= vactive && vblank_end >= vtotal) {
744 			u32 vsync = intel_de_read(dev_priv, TRANS_VSYNC(cpu_transcoder));
745 			u32 vsync_start = REG_FIELD_GET(VSYNC_START_MASK, vsync) + 1;
746 
747 			vblank_start = vsync_start;
748 			intel_de_write(dev_priv, TRANS_VBLANK(cpu_transcoder),
749 				       VBLANK_START(vblank_start - 1) |
750 				       VBLANK_END(vblank_end - 1));
751 			restore_vblank = true;
752 		}
753 		/* sample in the vertical border, selecting the larger one */
754 		if (vblank_start - vactive >= vtotal - vblank_end)
755 			vsample = (vblank_start + vactive) >> 1;
756 		else
757 			vsample = (vtotal + vblank_end) >> 1;
758 
759 		/*
760 		 * Wait for the border to be displayed
761 		 */
762 		while (intel_de_read(dev_priv, PIPEDSL(pipe)) >= vactive)
763 			;
764 		while ((dsl = intel_de_read(dev_priv, PIPEDSL(pipe))) <= vsample)
765 			;
766 		/*
767 		 * Watch ST00 for an entire scanline
768 		 */
769 		detect = 0;
770 		count = 0;
771 		do {
772 			count++;
773 			/* Read the ST00 VGA status register */
774 			st00 = intel_de_read8(dev_priv, _VGA_MSR_WRITE);
775 			if (st00 & (1 << 4))
776 				detect++;
777 		} while ((intel_de_read(dev_priv, PIPEDSL(pipe)) == dsl));
778 
779 		/* restore vblank if necessary */
780 		if (restore_vblank)
781 			intel_de_write(dev_priv, TRANS_VBLANK(cpu_transcoder), vblank);
782 		/*
783 		 * If more than 3/4 of the scanline detected a monitor,
784 		 * then it is assumed to be present. This works even on i830,
785 		 * where there isn't any way to force the border color across
786 		 * the screen
787 		 */
788 		status = detect * 4 > count * 3 ?
789 			 connector_status_connected :
790 			 connector_status_disconnected;
791 	}
792 
793 	/* Restore previous settings */
794 	intel_de_write(dev_priv, BCLRPAT(cpu_transcoder), save_bclrpat);
795 
796 	return status;
797 }
798 
799 static int intel_spurious_crt_detect_dmi_callback(const struct dmi_system_id *id)
800 {
801 	DRM_DEBUG_DRIVER("Skipping CRT detection for %s\n", id->ident);
802 	return 1;
803 }
804 
805 static const struct dmi_system_id intel_spurious_crt_detect[] = {
806 	{
807 		.callback = intel_spurious_crt_detect_dmi_callback,
808 		.ident = "ACER ZGB",
809 		.matches = {
810 			DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
811 			DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
812 		},
813 	},
814 	{
815 		.callback = intel_spurious_crt_detect_dmi_callback,
816 		.ident = "Intel DZ77BH-55K",
817 		.matches = {
818 			DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
819 			DMI_MATCH(DMI_BOARD_NAME, "DZ77BH-55K"),
820 		},
821 	},
822 	{ }
823 };
824 
825 static int
826 intel_crt_detect(struct drm_connector *connector,
827 		 struct drm_modeset_acquire_ctx *ctx,
828 		 bool force)
829 {
830 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
831 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
832 	struct intel_encoder *intel_encoder = &crt->base;
833 	struct drm_atomic_state *state;
834 	intel_wakeref_t wakeref;
835 	int status;
836 
837 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s] force=%d\n",
838 		    connector->base.id, connector->name,
839 		    force);
840 
841 	if (!intel_display_device_enabled(dev_priv))
842 		return connector_status_disconnected;
843 
844 	if (dev_priv->params.load_detect_test) {
845 		wakeref = intel_display_power_get(dev_priv,
846 						  intel_encoder->power_domain);
847 		goto load_detect;
848 	}
849 
850 	/* Skip machines without VGA that falsely report hotplug events */
851 	if (dmi_check_system(intel_spurious_crt_detect))
852 		return connector_status_disconnected;
853 
854 	wakeref = intel_display_power_get(dev_priv,
855 					  intel_encoder->power_domain);
856 
857 	if (I915_HAS_HOTPLUG(dev_priv)) {
858 		/* We can not rely on the HPD pin always being correctly wired
859 		 * up, for example many KVM do not pass it through, and so
860 		 * only trust an assertion that the monitor is connected.
861 		 */
862 		if (intel_crt_detect_hotplug(connector)) {
863 			drm_dbg_kms(&dev_priv->drm,
864 				    "CRT detected via hotplug\n");
865 			status = connector_status_connected;
866 			goto out;
867 		} else
868 			drm_dbg_kms(&dev_priv->drm,
869 				    "CRT not detected via hotplug\n");
870 	}
871 
872 	if (intel_crt_detect_ddc(connector)) {
873 		status = connector_status_connected;
874 		goto out;
875 	}
876 
877 	/* Load detection is broken on HPD capable machines. Whoever wants a
878 	 * broken monitor (without edid) to work behind a broken kvm (that fails
879 	 * to have the right resistors for HP detection) needs to fix this up.
880 	 * For now just bail out. */
881 	if (I915_HAS_HOTPLUG(dev_priv)) {
882 		status = connector_status_disconnected;
883 		goto out;
884 	}
885 
886 load_detect:
887 	if (!force) {
888 		status = connector->status;
889 		goto out;
890 	}
891 
892 	/* for pre-945g platforms use load detect */
893 	state = intel_load_detect_get_pipe(connector, ctx);
894 	if (IS_ERR(state)) {
895 		status = PTR_ERR(state);
896 	} else if (!state) {
897 		status = connector_status_unknown;
898 	} else {
899 		if (intel_crt_detect_ddc(connector))
900 			status = connector_status_connected;
901 		else if (DISPLAY_VER(dev_priv) < 4)
902 			status = intel_crt_load_detect(crt,
903 				to_intel_crtc(connector->state->crtc)->pipe);
904 		else if (dev_priv->params.load_detect_test)
905 			status = connector_status_disconnected;
906 		else
907 			status = connector_status_unknown;
908 		intel_load_detect_release_pipe(connector, state, ctx);
909 	}
910 
911 out:
912 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
913 
914 	return status;
915 }
916 
917 static int intel_crt_get_modes(struct drm_connector *connector)
918 {
919 	struct drm_device *dev = connector->dev;
920 	struct drm_i915_private *dev_priv = to_i915(dev);
921 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
922 	struct intel_encoder *intel_encoder = &crt->base;
923 	intel_wakeref_t wakeref;
924 	struct i2c_adapter *ddc;
925 	int ret;
926 
927 	wakeref = intel_display_power_get(dev_priv,
928 					  intel_encoder->power_domain);
929 
930 	ret = intel_crt_ddc_get_modes(connector, connector->ddc);
931 	if (ret || !IS_G4X(dev_priv))
932 		goto out;
933 
934 	/* Try to probe digital port for output in DVI-I -> VGA mode. */
935 	ddc = intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPB);
936 	ret = intel_crt_ddc_get_modes(connector, ddc);
937 
938 out:
939 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
940 
941 	return ret;
942 }
943 
944 void intel_crt_reset(struct drm_encoder *encoder)
945 {
946 	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
947 	struct intel_crt *crt = intel_encoder_to_crt(to_intel_encoder(encoder));
948 
949 	if (DISPLAY_VER(dev_priv) >= 5) {
950 		u32 adpa;
951 
952 		adpa = intel_de_read(dev_priv, crt->adpa_reg);
953 		adpa &= ~ADPA_CRT_HOTPLUG_MASK;
954 		adpa |= ADPA_HOTPLUG_BITS;
955 		intel_de_write(dev_priv, crt->adpa_reg, adpa);
956 		intel_de_posting_read(dev_priv, crt->adpa_reg);
957 
958 		drm_dbg_kms(&dev_priv->drm, "crt adpa set to 0x%x\n", adpa);
959 		crt->force_hotplug_required = true;
960 	}
961 
962 }
963 
964 /*
965  * Routines for controlling stuff on the analog port
966  */
967 
968 static const struct drm_connector_funcs intel_crt_connector_funcs = {
969 	.fill_modes = drm_helper_probe_single_connector_modes,
970 	.late_register = intel_connector_register,
971 	.early_unregister = intel_connector_unregister,
972 	.destroy = intel_connector_destroy,
973 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
974 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
975 };
976 
977 static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
978 	.detect_ctx = intel_crt_detect,
979 	.mode_valid = intel_crt_mode_valid,
980 	.get_modes = intel_crt_get_modes,
981 };
982 
983 static const struct drm_encoder_funcs intel_crt_enc_funcs = {
984 	.reset = intel_crt_reset,
985 	.destroy = intel_encoder_destroy,
986 };
987 
988 void intel_crt_init(struct drm_i915_private *dev_priv)
989 {
990 	struct drm_connector *connector;
991 	struct intel_crt *crt;
992 	struct intel_connector *intel_connector;
993 	i915_reg_t adpa_reg;
994 	u8 ddc_pin;
995 	u32 adpa;
996 
997 	if (HAS_PCH_SPLIT(dev_priv))
998 		adpa_reg = PCH_ADPA;
999 	else if (IS_VALLEYVIEW(dev_priv))
1000 		adpa_reg = VLV_ADPA;
1001 	else
1002 		adpa_reg = ADPA;
1003 
1004 	adpa = intel_de_read(dev_priv, adpa_reg);
1005 	if ((adpa & ADPA_DAC_ENABLE) == 0) {
1006 		/*
1007 		 * On some machines (some IVB at least) CRT can be
1008 		 * fused off, but there's no known fuse bit to
1009 		 * indicate that. On these machine the ADPA register
1010 		 * works normally, except the DAC enable bit won't
1011 		 * take. So the only way to tell is attempt to enable
1012 		 * it and see what happens.
1013 		 */
1014 		intel_de_write(dev_priv, adpa_reg,
1015 			       adpa | ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
1016 		if ((intel_de_read(dev_priv, adpa_reg) & ADPA_DAC_ENABLE) == 0)
1017 			return;
1018 		intel_de_write(dev_priv, adpa_reg, adpa);
1019 	}
1020 
1021 	crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
1022 	if (!crt)
1023 		return;
1024 
1025 	intel_connector = intel_connector_alloc();
1026 	if (!intel_connector) {
1027 		kfree(crt);
1028 		return;
1029 	}
1030 
1031 	ddc_pin = dev_priv->display.vbt.crt_ddc_pin;
1032 
1033 	connector = &intel_connector->base;
1034 	crt->connector = intel_connector;
1035 	drm_connector_init_with_ddc(&dev_priv->drm, connector,
1036 				    &intel_crt_connector_funcs,
1037 				    DRM_MODE_CONNECTOR_VGA,
1038 				    intel_gmbus_get_adapter(dev_priv, ddc_pin));
1039 
1040 	drm_encoder_init(&dev_priv->drm, &crt->base.base, &intel_crt_enc_funcs,
1041 			 DRM_MODE_ENCODER_DAC, "CRT");
1042 
1043 	intel_connector_attach_encoder(intel_connector, &crt->base);
1044 
1045 	crt->base.type = INTEL_OUTPUT_ANALOG;
1046 	crt->base.cloneable = BIT(INTEL_OUTPUT_DVO) | BIT(INTEL_OUTPUT_HDMI);
1047 	if (IS_I830(dev_priv))
1048 		crt->base.pipe_mask = BIT(PIPE_A);
1049 	else
1050 		crt->base.pipe_mask = ~0;
1051 
1052 	if (DISPLAY_VER(dev_priv) != 2)
1053 		connector->interlace_allowed = true;
1054 
1055 	crt->adpa_reg = adpa_reg;
1056 
1057 	crt->base.power_domain = POWER_DOMAIN_PORT_CRT;
1058 
1059 	if (I915_HAS_HOTPLUG(dev_priv) &&
1060 	    !dmi_check_system(intel_spurious_crt_detect)) {
1061 		crt->base.hpd_pin = HPD_CRT;
1062 		crt->base.hotplug = intel_encoder_hotplug;
1063 		intel_connector->polled = DRM_CONNECTOR_POLL_HPD;
1064 	} else {
1065 		intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1066 	}
1067 
1068 	if (HAS_DDI(dev_priv)) {
1069 		assert_port_valid(dev_priv, PORT_E);
1070 
1071 		crt->base.port = PORT_E;
1072 		crt->base.get_config = hsw_crt_get_config;
1073 		crt->base.get_hw_state = intel_ddi_get_hw_state;
1074 		crt->base.compute_config = hsw_crt_compute_config;
1075 		crt->base.pre_pll_enable = hsw_pre_pll_enable_crt;
1076 		crt->base.pre_enable = hsw_pre_enable_crt;
1077 		crt->base.enable = hsw_enable_crt;
1078 		crt->base.disable = hsw_disable_crt;
1079 		crt->base.post_disable = hsw_post_disable_crt;
1080 		crt->base.enable_clock = hsw_ddi_enable_clock;
1081 		crt->base.disable_clock = hsw_ddi_disable_clock;
1082 		crt->base.is_clock_enabled = hsw_ddi_is_clock_enabled;
1083 
1084 		intel_ddi_buf_trans_init(&crt->base);
1085 	} else {
1086 		if (HAS_PCH_SPLIT(dev_priv)) {
1087 			crt->base.compute_config = pch_crt_compute_config;
1088 			crt->base.disable = pch_disable_crt;
1089 			crt->base.post_disable = pch_post_disable_crt;
1090 		} else {
1091 			crt->base.compute_config = intel_crt_compute_config;
1092 			crt->base.disable = intel_disable_crt;
1093 		}
1094 		crt->base.port = PORT_NONE;
1095 		crt->base.get_config = intel_crt_get_config;
1096 		crt->base.get_hw_state = intel_crt_get_hw_state;
1097 		crt->base.enable = intel_enable_crt;
1098 	}
1099 	intel_connector->get_hw_state = intel_connector_get_hw_state;
1100 
1101 	drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
1102 
1103 	/*
1104 	 * TODO: find a proper way to discover whether we need to set the the
1105 	 * polarity and link reversal bits or not, instead of relying on the
1106 	 * BIOS.
1107 	 */
1108 	if (HAS_PCH_LPT(dev_priv)) {
1109 		u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
1110 				 FDI_RX_LINK_REVERSAL_OVERRIDE;
1111 
1112 		dev_priv->display.fdi.rx_config = intel_de_read(dev_priv,
1113 								FDI_RX_CTL(PIPE_A)) & fdi_config;
1114 	}
1115 
1116 	intel_crt_reset(&crt->base.base);
1117 }
1118