2 * Copyright © 2006-2007 Intel Corporation
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:
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
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.
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/dmi.h>
28 #include <linux/i2c.h>
29 #include <linux/slab.h>
33 #include "drm_crtc_helper.h"
35 #include "intel_drv.h"
39 /* Here's the desired hotplug mode */
40 #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 | \
41 ADPA_CRT_HOTPLUG_WARMUP_10MS | \
42 ADPA_CRT_HOTPLUG_SAMPLE_4S | \
43 ADPA_CRT_HOTPLUG_VOLTAGE_50 | \
44 ADPA_CRT_HOTPLUG_VOLREF_325MV | \
45 ADPA_CRT_HOTPLUG_ENABLE)
48 struct intel_encoder base;
49 bool force_hotplug_required;
52 static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
54 return container_of(intel_attached_encoder(connector),
55 struct intel_crt, base);
58 static void pch_crt_dpms(struct drm_encoder *encoder, int mode)
60 struct drm_device *dev = encoder->dev;
61 struct drm_i915_private *dev_priv = dev->dev_private;
64 temp = I915_READ(PCH_ADPA);
65 temp &= ~ADPA_DAC_ENABLE;
68 case DRM_MODE_DPMS_ON:
69 temp |= ADPA_DAC_ENABLE;
71 case DRM_MODE_DPMS_STANDBY:
72 case DRM_MODE_DPMS_SUSPEND:
73 case DRM_MODE_DPMS_OFF:
74 /* Just leave port enable cleared */
78 I915_WRITE(PCH_ADPA, temp);
81 static void gmch_crt_dpms(struct drm_encoder *encoder, int mode)
83 struct drm_device *dev = encoder->dev;
84 struct drm_i915_private *dev_priv = dev->dev_private;
87 temp = I915_READ(ADPA);
88 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
89 temp &= ~ADPA_DAC_ENABLE;
91 if (IS_VALLEYVIEW(dev) && mode != DRM_MODE_DPMS_ON)
92 mode = DRM_MODE_DPMS_OFF;
95 case DRM_MODE_DPMS_ON:
96 temp |= ADPA_DAC_ENABLE;
98 case DRM_MODE_DPMS_STANDBY:
99 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
101 case DRM_MODE_DPMS_SUSPEND:
102 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
104 case DRM_MODE_DPMS_OFF:
105 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
109 I915_WRITE(ADPA, temp);
112 static int intel_crt_mode_valid(struct drm_connector *connector,
113 struct drm_display_mode *mode)
115 struct drm_device *dev = connector->dev;
118 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
119 return MODE_NO_DBLESCAN;
121 if (mode->clock < 25000)
122 return MODE_CLOCK_LOW;
128 if (mode->clock > max_clock)
129 return MODE_CLOCK_HIGH;
134 static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
135 const struct drm_display_mode *mode,
136 struct drm_display_mode *adjusted_mode)
141 static void intel_crt_mode_set(struct drm_encoder *encoder,
142 struct drm_display_mode *mode,
143 struct drm_display_mode *adjusted_mode)
146 struct drm_device *dev = encoder->dev;
147 struct drm_crtc *crtc = encoder->crtc;
148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
149 struct drm_i915_private *dev_priv = dev->dev_private;
154 dpll_md_reg = DPLL_MD(intel_crtc->pipe);
156 if (HAS_PCH_SPLIT(dev))
162 * Disable separate mode multiplier used when cloning SDVO to CRT
163 * XXX this needs to be adjusted when we really are cloning
165 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
166 dpll_md = I915_READ(dpll_md_reg);
167 I915_WRITE(dpll_md_reg,
168 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
171 adpa = ADPA_HOTPLUG_BITS;
172 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
173 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
174 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
175 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
177 /* For CPT allow 3 pipe config, for others just use A or B */
178 if (HAS_PCH_CPT(dev))
179 adpa |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
180 else if (intel_crtc->pipe == 0)
181 adpa |= ADPA_PIPE_A_SELECT;
183 adpa |= ADPA_PIPE_B_SELECT;
185 if (!HAS_PCH_SPLIT(dev))
186 I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);
188 I915_WRITE(adpa_reg, adpa);
191 static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
193 struct drm_device *dev = connector->dev;
194 struct intel_crt *crt = intel_attached_crt(connector);
195 struct drm_i915_private *dev_priv = dev->dev_private;
199 /* The first time through, trigger an explicit detection cycle */
200 if (crt->force_hotplug_required) {
201 bool turn_off_dac = HAS_PCH_SPLIT(dev);
204 crt->force_hotplug_required = 0;
206 save_adpa = adpa = I915_READ(PCH_ADPA);
207 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
209 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
211 adpa &= ~ADPA_DAC_ENABLE;
213 I915_WRITE(PCH_ADPA, adpa);
215 if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
217 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
220 I915_WRITE(PCH_ADPA, save_adpa);
221 POSTING_READ(PCH_ADPA);
225 /* Check the status to see if both blue and green are on now */
226 adpa = I915_READ(PCH_ADPA);
227 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
231 DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
236 static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
238 struct drm_device *dev = connector->dev;
239 struct drm_i915_private *dev_priv = dev->dev_private;
244 save_adpa = adpa = I915_READ(ADPA);
245 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
247 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
249 I915_WRITE(ADPA, adpa);
251 if (wait_for((I915_READ(ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
253 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
254 I915_WRITE(ADPA, save_adpa);
257 /* Check the status to see if both blue and green are on now */
258 adpa = I915_READ(ADPA);
259 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
264 DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
266 /* FIXME: debug force function and remove */
273 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
275 * Not for i915G/i915GM
277 * \return true if CRT is connected.
278 * \return false if CRT is disconnected.
280 static bool intel_crt_detect_hotplug(struct drm_connector *connector)
282 struct drm_device *dev = connector->dev;
283 struct drm_i915_private *dev_priv = dev->dev_private;
284 u32 hotplug_en, orig, stat;
288 if (HAS_PCH_SPLIT(dev))
289 return intel_ironlake_crt_detect_hotplug(connector);
291 if (IS_VALLEYVIEW(dev))
292 return valleyview_crt_detect_hotplug(connector);
295 * On 4 series desktop, CRT detect sequence need to be done twice
296 * to get a reliable result.
299 if (IS_G4X(dev) && !IS_GM45(dev))
303 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
304 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
306 for (i = 0; i < tries ; i++) {
307 /* turn on the FORCE_DETECT */
308 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
309 /* wait for FORCE_DETECT to go off */
310 if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
311 CRT_HOTPLUG_FORCE_DETECT) == 0,
313 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
316 stat = I915_READ(PORT_HOTPLUG_STAT);
317 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
320 /* clear the interrupt we just generated, if any */
321 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
323 /* and put the bits back */
324 I915_WRITE(PORT_HOTPLUG_EN, orig);
329 static bool intel_crt_detect_ddc(struct drm_connector *connector)
331 struct intel_crt *crt = intel_attached_crt(connector);
332 struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
334 struct i2c_adapter *i2c;
336 BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
338 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
339 edid = drm_get_edid(connector, i2c);
342 bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
345 * This may be a DVI-I connector with a shared DDC
346 * link between analog and digital outputs, so we
347 * have to check the EDID input spec of the attached device.
350 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
354 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
356 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
364 static enum drm_connector_status
365 intel_crt_load_detect(struct intel_crt *crt)
367 struct drm_device *dev = crt->base.base.dev;
368 struct drm_i915_private *dev_priv = dev->dev_private;
369 uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
370 uint32_t save_bclrpat;
371 uint32_t save_vtotal;
372 uint32_t vtotal, vactive;
374 uint32_t vblank, vblank_start, vblank_end;
376 uint32_t bclrpat_reg;
380 uint32_t pipeconf_reg;
381 uint32_t pipe_dsl_reg;
383 enum drm_connector_status status;
385 DRM_DEBUG_KMS("starting load-detect on CRT\n");
387 bclrpat_reg = BCLRPAT(pipe);
388 vtotal_reg = VTOTAL(pipe);
389 vblank_reg = VBLANK(pipe);
390 vsync_reg = VSYNC(pipe);
391 pipeconf_reg = PIPECONF(pipe);
392 pipe_dsl_reg = PIPEDSL(pipe);
394 save_bclrpat = I915_READ(bclrpat_reg);
395 save_vtotal = I915_READ(vtotal_reg);
396 vblank = I915_READ(vblank_reg);
398 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
399 vactive = (save_vtotal & 0x7ff) + 1;
401 vblank_start = (vblank & 0xfff) + 1;
402 vblank_end = ((vblank >> 16) & 0xfff) + 1;
404 /* Set the border color to purple. */
405 I915_WRITE(bclrpat_reg, 0x500050);
408 uint32_t pipeconf = I915_READ(pipeconf_reg);
409 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
410 POSTING_READ(pipeconf_reg);
411 /* Wait for next Vblank to substitue
412 * border color for Color info */
413 intel_wait_for_vblank(dev, pipe);
414 st00 = I915_READ8(VGA_MSR_WRITE);
415 status = ((st00 & (1 << 4)) != 0) ?
416 connector_status_connected :
417 connector_status_disconnected;
419 I915_WRITE(pipeconf_reg, pipeconf);
421 bool restore_vblank = false;
425 * If there isn't any border, add some.
426 * Yes, this will flicker
428 if (vblank_start <= vactive && vblank_end >= vtotal) {
429 uint32_t vsync = I915_READ(vsync_reg);
430 uint32_t vsync_start = (vsync & 0xffff) + 1;
432 vblank_start = vsync_start;
433 I915_WRITE(vblank_reg,
435 ((vblank_end - 1) << 16));
436 restore_vblank = true;
438 /* sample in the vertical border, selecting the larger one */
439 if (vblank_start - vactive >= vtotal - vblank_end)
440 vsample = (vblank_start + vactive) >> 1;
442 vsample = (vtotal + vblank_end) >> 1;
445 * Wait for the border to be displayed
447 while (I915_READ(pipe_dsl_reg) >= vactive)
449 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
452 * Watch ST00 for an entire scanline
458 /* Read the ST00 VGA status register */
459 st00 = I915_READ8(VGA_MSR_WRITE);
462 } while ((I915_READ(pipe_dsl_reg) == dsl));
464 /* restore vblank if necessary */
466 I915_WRITE(vblank_reg, vblank);
468 * If more than 3/4 of the scanline detected a monitor,
469 * then it is assumed to be present. This works even on i830,
470 * where there isn't any way to force the border color across
473 status = detect * 4 > count * 3 ?
474 connector_status_connected :
475 connector_status_disconnected;
478 /* Restore previous settings */
479 I915_WRITE(bclrpat_reg, save_bclrpat);
484 static enum drm_connector_status
485 intel_crt_detect(struct drm_connector *connector, bool force)
487 struct drm_device *dev = connector->dev;
488 struct intel_crt *crt = intel_attached_crt(connector);
489 enum drm_connector_status status;
490 struct intel_load_detect_pipe tmp;
492 if (I915_HAS_HOTPLUG(dev)) {
493 /* We can not rely on the HPD pin always being correctly wired
494 * up, for example many KVM do not pass it through, and so
495 * only trust an assertion that the monitor is connected.
497 if (intel_crt_detect_hotplug(connector)) {
498 DRM_DEBUG_KMS("CRT detected via hotplug\n");
499 return connector_status_connected;
501 DRM_DEBUG_KMS("CRT not detected via hotplug\n");
504 if (intel_crt_detect_ddc(connector))
505 return connector_status_connected;
507 /* Load detection is broken on HPD capable machines. Whoever wants a
508 * broken monitor (without edid) to work behind a broken kvm (that fails
509 * to have the right resistors for HP detection) needs to fix this up.
510 * For now just bail out. */
511 if (I915_HAS_HOTPLUG(dev))
512 return connector_status_disconnected;
515 return connector->status;
517 /* for pre-945g platforms use load detect */
518 if (intel_get_load_detect_pipe(&crt->base, connector, NULL,
520 if (intel_crt_detect_ddc(connector))
521 status = connector_status_connected;
523 status = intel_crt_load_detect(crt);
524 intel_release_load_detect_pipe(&crt->base, connector,
527 status = connector_status_unknown;
532 static void intel_crt_destroy(struct drm_connector *connector)
534 drm_sysfs_connector_remove(connector);
535 drm_connector_cleanup(connector);
539 static int intel_crt_get_modes(struct drm_connector *connector)
541 struct drm_device *dev = connector->dev;
542 struct drm_i915_private *dev_priv = dev->dev_private;
544 struct i2c_adapter *i2c;
546 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
547 ret = intel_ddc_get_modes(connector, i2c);
548 if (ret || !IS_G4X(dev))
551 /* Try to probe digital port for output in DVI-I -> VGA mode. */
552 i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
553 return intel_ddc_get_modes(connector, i2c);
556 static int intel_crt_set_property(struct drm_connector *connector,
557 struct drm_property *property,
563 static void intel_crt_reset(struct drm_connector *connector)
565 struct drm_device *dev = connector->dev;
566 struct intel_crt *crt = intel_attached_crt(connector);
568 if (HAS_PCH_SPLIT(dev))
569 crt->force_hotplug_required = 1;
573 * Routines for controlling stuff on the analog port
576 static const struct drm_encoder_helper_funcs pch_encoder_funcs = {
577 .mode_fixup = intel_crt_mode_fixup,
578 .prepare = intel_encoder_prepare,
579 .commit = intel_encoder_commit,
580 .mode_set = intel_crt_mode_set,
581 .dpms = pch_crt_dpms,
584 static const struct drm_encoder_helper_funcs gmch_encoder_funcs = {
585 .mode_fixup = intel_crt_mode_fixup,
586 .prepare = intel_encoder_prepare,
587 .commit = intel_encoder_commit,
588 .mode_set = intel_crt_mode_set,
589 .dpms = gmch_crt_dpms,
592 static const struct drm_connector_funcs intel_crt_connector_funcs = {
593 .reset = intel_crt_reset,
594 .dpms = drm_helper_connector_dpms,
595 .detect = intel_crt_detect,
596 .fill_modes = drm_helper_probe_single_connector_modes,
597 .destroy = intel_crt_destroy,
598 .set_property = intel_crt_set_property,
601 static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
602 .mode_valid = intel_crt_mode_valid,
603 .get_modes = intel_crt_get_modes,
604 .best_encoder = intel_best_encoder,
607 static const struct drm_encoder_funcs intel_crt_enc_funcs = {
608 .destroy = intel_encoder_destroy,
611 static int __init intel_no_crt_dmi_callback(const struct dmi_system_id *id)
613 DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
617 static const struct dmi_system_id intel_no_crt[] = {
619 .callback = intel_no_crt_dmi_callback,
622 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
623 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
629 void intel_crt_init(struct drm_device *dev)
631 struct drm_connector *connector;
632 struct intel_crt *crt;
633 struct intel_connector *intel_connector;
634 struct drm_i915_private *dev_priv = dev->dev_private;
635 const struct drm_encoder_helper_funcs *encoder_helper_funcs;
637 /* Skip machines without VGA that falsely report hotplug events */
638 if (dmi_check_system(intel_no_crt))
641 crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
645 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
646 if (!intel_connector) {
651 connector = &intel_connector->base;
652 drm_connector_init(dev, &intel_connector->base,
653 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
655 drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
656 DRM_MODE_ENCODER_DAC);
658 intel_connector_attach_encoder(intel_connector, &crt->base);
660 crt->base.type = INTEL_OUTPUT_ANALOG;
661 crt->base.clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT |
662 1 << INTEL_ANALOG_CLONE_BIT |
663 1 << INTEL_SDVO_LVDS_CLONE_BIT);
665 crt->base.crtc_mask = (1 << 0);
667 crt->base.crtc_mask = (1 << 0) | (1 << 1);
670 connector->interlace_allowed = 0;
672 connector->interlace_allowed = 1;
673 connector->doublescan_allowed = 0;
675 if (HAS_PCH_SPLIT(dev))
676 encoder_helper_funcs = &pch_encoder_funcs;
678 encoder_helper_funcs = &gmch_encoder_funcs;
680 drm_encoder_helper_add(&crt->base.base, encoder_helper_funcs);
681 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
683 drm_sysfs_connector_add(connector);
685 if (I915_HAS_HOTPLUG(dev))
686 connector->polled = DRM_CONNECTOR_POLL_HPD;
688 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
691 * Configure the automatic hotplug detection stuff
693 crt->force_hotplug_required = 0;
694 if (HAS_PCH_SPLIT(dev)) {
697 adpa = I915_READ(PCH_ADPA);
698 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
699 adpa |= ADPA_HOTPLUG_BITS;
700 I915_WRITE(PCH_ADPA, adpa);
701 POSTING_READ(PCH_ADPA);
703 DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);
704 crt->force_hotplug_required = 1;
707 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;