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>
26 * $FreeBSD: src/sys/dev/drm2/i915/intel_crt.c,v 1.1 2012/05/22 11:07:44 kib Exp $
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_edid.h>
33 #include "intel_drv.h"
34 #include <drm/i915_drm.h>
37 /* Here's the desired hotplug mode */
38 #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 | \
39 ADPA_CRT_HOTPLUG_WARMUP_10MS | \
40 ADPA_CRT_HOTPLUG_SAMPLE_4S | \
41 ADPA_CRT_HOTPLUG_VOLTAGE_50 | \
42 ADPA_CRT_HOTPLUG_VOLREF_325MV | \
43 ADPA_CRT_HOTPLUG_ENABLE)
46 struct intel_encoder base;
47 bool force_hotplug_required;
50 static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
52 return container_of(intel_attached_encoder(connector),
53 struct intel_crt, base);
56 static void intel_crt_dpms(struct drm_encoder *encoder, int mode)
58 struct drm_device *dev = encoder->dev;
59 struct drm_i915_private *dev_priv = dev->dev_private;
62 if (HAS_PCH_SPLIT(dev))
67 temp = I915_READ(reg);
68 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
69 temp &= ~ADPA_DAC_ENABLE;
72 case DRM_MODE_DPMS_ON:
73 temp |= ADPA_DAC_ENABLE;
75 case DRM_MODE_DPMS_STANDBY:
76 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
78 case DRM_MODE_DPMS_SUSPEND:
79 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
81 case DRM_MODE_DPMS_OFF:
82 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
86 I915_WRITE(reg, temp);
89 static int intel_crt_mode_valid(struct drm_connector *connector,
90 struct drm_display_mode *mode)
92 struct drm_device *dev = connector->dev;
95 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
96 return MODE_NO_DBLESCAN;
98 if (mode->clock < 25000)
99 return MODE_CLOCK_LOW;
105 if (mode->clock > max_clock)
106 return MODE_CLOCK_HIGH;
111 static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
112 const struct drm_display_mode *mode,
113 struct drm_display_mode *adjusted_mode)
118 static void intel_crt_mode_set(struct drm_encoder *encoder,
119 struct drm_display_mode *mode,
120 struct drm_display_mode *adjusted_mode)
123 struct drm_device *dev = encoder->dev;
124 struct drm_crtc *crtc = encoder->crtc;
125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
126 struct drm_i915_private *dev_priv = dev->dev_private;
131 dpll_md_reg = DPLL_MD(intel_crtc->pipe);
133 if (HAS_PCH_SPLIT(dev))
139 * Disable separate mode multiplier used when cloning SDVO to CRT
140 * XXX this needs to be adjusted when we really are cloning
142 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
143 dpll_md = I915_READ(dpll_md_reg);
144 I915_WRITE(dpll_md_reg,
145 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
148 adpa = ADPA_HOTPLUG_BITS;
149 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
150 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
151 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
152 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
154 /* For CPT allow 3 pipe config, for others just use A or B */
155 if (HAS_PCH_CPT(dev))
156 adpa |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
157 else if (intel_crtc->pipe == 0)
158 adpa |= ADPA_PIPE_A_SELECT;
160 adpa |= ADPA_PIPE_B_SELECT;
162 if (!HAS_PCH_SPLIT(dev))
163 I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);
165 I915_WRITE(adpa_reg, adpa);
168 static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
170 struct drm_device *dev = connector->dev;
171 struct intel_crt *crt = intel_attached_crt(connector);
172 struct drm_i915_private *dev_priv = dev->dev_private;
176 /* The first time through, trigger an explicit detection cycle */
177 if (crt->force_hotplug_required) {
178 bool turn_off_dac = HAS_PCH_SPLIT(dev);
181 crt->force_hotplug_required = 0;
183 save_adpa = adpa = I915_READ(PCH_ADPA);
184 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
186 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
188 adpa &= ~ADPA_DAC_ENABLE;
190 I915_WRITE(PCH_ADPA, adpa);
192 if (_intel_wait_for(dev,
193 (I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
195 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER\n");
198 I915_WRITE(PCH_ADPA, save_adpa);
199 POSTING_READ(PCH_ADPA);
203 /* Check the status to see if both blue and green are on now */
204 adpa = I915_READ(PCH_ADPA);
205 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
209 DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
215 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
217 * Not for i915G/i915GM
219 * \return true if CRT is connected.
220 * \return false if CRT is disconnected.
222 static bool intel_crt_detect_hotplug(struct drm_connector *connector)
224 struct drm_device *dev = connector->dev;
225 struct drm_i915_private *dev_priv = dev->dev_private;
226 u32 hotplug_en, orig, stat;
230 if (HAS_PCH_SPLIT(dev))
231 return intel_ironlake_crt_detect_hotplug(connector);
234 * On 4 series desktop, CRT detect sequence need to be done twice
235 * to get a reliable result.
238 if (IS_G4X(dev) && !IS_GM45(dev))
242 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
243 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
245 for (i = 0; i < tries ; i++) {
246 /* turn on the FORCE_DETECT */
247 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
248 /* wait for FORCE_DETECT to go off */
249 if (_intel_wait_for(dev,
250 (I915_READ(PORT_HOTPLUG_EN) & CRT_HOTPLUG_FORCE_DETECT) == 0,
252 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
255 stat = I915_READ(PORT_HOTPLUG_STAT);
256 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
259 /* clear the interrupt we just generated, if any */
260 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
262 /* and put the bits back */
263 I915_WRITE(PORT_HOTPLUG_EN, orig);
268 static bool intel_crt_detect_ddc(struct drm_connector *connector)
270 struct intel_crt *crt = intel_attached_crt(connector);
271 struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
273 /* CRT should always be at 0, but check anyway */
274 if (crt->base.type != INTEL_OUTPUT_ANALOG)
277 if (intel_ddc_probe(&crt->base, dev_priv->crt_ddc_pin)) {
279 bool is_digital = false;
281 edid = drm_get_edid(connector,
282 dev_priv->gmbus[dev_priv->crt_ddc_pin]);
284 * This may be a DVI-I connector with a shared DDC
285 * link between analog and digital outputs, so we
286 * have to check the EDID input spec of the attached device.
288 * On the other hand, what should we do if it is a broken EDID?
291 is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
292 connector->display_info.raw_edid = NULL;
293 drm_free(edid, DRM_MEM_KMS);
297 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
300 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
307 static enum drm_connector_status
308 intel_crt_load_detect(struct intel_crt *crt)
310 struct drm_device *dev = crt->base.base.dev;
311 struct drm_i915_private *dev_priv = dev->dev_private;
312 uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
313 uint32_t save_bclrpat;
314 uint32_t save_vtotal;
315 uint32_t vtotal, vactive;
317 uint32_t vblank, vblank_start, vblank_end;
319 uint32_t bclrpat_reg;
323 uint32_t pipeconf_reg;
324 uint32_t pipe_dsl_reg;
326 enum drm_connector_status status;
328 DRM_DEBUG_KMS("starting load-detect on CRT\n");
330 bclrpat_reg = BCLRPAT(pipe);
331 vtotal_reg = VTOTAL(pipe);
332 vblank_reg = VBLANK(pipe);
333 vsync_reg = VSYNC(pipe);
334 pipeconf_reg = PIPECONF(pipe);
335 pipe_dsl_reg = PIPEDSL(pipe);
337 save_bclrpat = I915_READ(bclrpat_reg);
338 save_vtotal = I915_READ(vtotal_reg);
339 vblank = I915_READ(vblank_reg);
341 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
342 vactive = (save_vtotal & 0x7ff) + 1;
344 vblank_start = (vblank & 0xfff) + 1;
345 vblank_end = ((vblank >> 16) & 0xfff) + 1;
347 /* Set the border color to purple. */
348 I915_WRITE(bclrpat_reg, 0x500050);
351 uint32_t pipeconf = I915_READ(pipeconf_reg);
352 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
353 POSTING_READ(pipeconf_reg);
354 /* Wait for next Vblank to substitue
355 * border color for Color info */
356 intel_wait_for_vblank(dev, pipe);
357 st00 = I915_READ8(VGA_MSR_WRITE);
358 status = ((st00 & (1 << 4)) != 0) ?
359 connector_status_connected :
360 connector_status_disconnected;
362 I915_WRITE(pipeconf_reg, pipeconf);
364 bool restore_vblank = false;
368 * If there isn't any border, add some.
369 * Yes, this will flicker
371 if (vblank_start <= vactive && vblank_end >= vtotal) {
372 uint32_t vsync = I915_READ(vsync_reg);
373 uint32_t vsync_start = (vsync & 0xffff) + 1;
375 vblank_start = vsync_start;
376 I915_WRITE(vblank_reg,
378 ((vblank_end - 1) << 16));
379 restore_vblank = true;
381 /* sample in the vertical border, selecting the larger one */
382 if (vblank_start - vactive >= vtotal - vblank_end)
383 vsample = (vblank_start + vactive) >> 1;
385 vsample = (vtotal + vblank_end) >> 1;
388 * Wait for the border to be displayed
390 while (I915_READ(pipe_dsl_reg) >= vactive)
392 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
395 * Watch ST00 for an entire scanline
401 /* Read the ST00 VGA status register */
402 st00 = I915_READ8(VGA_MSR_WRITE);
405 } while ((I915_READ(pipe_dsl_reg) == dsl));
407 /* restore vblank if necessary */
409 I915_WRITE(vblank_reg, vblank);
411 * If more than 3/4 of the scanline detected a monitor,
412 * then it is assumed to be present. This works even on i830,
413 * where there isn't any way to force the border color across
416 status = detect * 4 > count * 3 ?
417 connector_status_connected :
418 connector_status_disconnected;
421 /* Restore previous settings */
422 I915_WRITE(bclrpat_reg, save_bclrpat);
427 static enum drm_connector_status
428 intel_crt_detect(struct drm_connector *connector, bool force)
430 struct drm_device *dev = connector->dev;
431 struct intel_crt *crt = intel_attached_crt(connector);
432 enum drm_connector_status status;
433 struct intel_load_detect_pipe tmp;
435 if (I915_HAS_HOTPLUG(dev)) {
436 if (intel_crt_detect_hotplug(connector)) {
437 DRM_DEBUG_KMS("CRT detected via hotplug\n");
438 return connector_status_connected;
440 DRM_DEBUG_KMS("CRT not detected via hotplug\n");
441 return connector_status_disconnected;
445 if (intel_crt_detect_ddc(connector))
446 return connector_status_connected;
449 return connector->status;
451 /* for pre-945g platforms use load detect */
452 if (intel_get_load_detect_pipe(&crt->base, connector, NULL,
454 if (intel_crt_detect_ddc(connector))
455 status = connector_status_connected;
457 status = intel_crt_load_detect(crt);
458 intel_release_load_detect_pipe(&crt->base, connector,
461 status = connector_status_unknown;
466 static void intel_crt_destroy(struct drm_connector *connector)
470 drm_sysfs_connector_remove(connector);
472 drm_connector_cleanup(connector);
473 drm_free(connector, DRM_MEM_KMS);
476 static int intel_crt_get_modes(struct drm_connector *connector)
478 struct drm_device *dev = connector->dev;
479 struct drm_i915_private *dev_priv = dev->dev_private;
482 ret = intel_ddc_get_modes(connector,
483 dev_priv->gmbus[dev_priv->crt_ddc_pin]);
484 if (ret || !IS_G4X(dev))
487 /* Try to probe digital port for output in DVI-I -> VGA mode. */
488 return (intel_ddc_get_modes(connector,
489 dev_priv->gmbus[GMBUS_PORT_DPB]));
492 static int intel_crt_set_property(struct drm_connector *connector,
493 struct drm_property *property,
499 static void intel_crt_reset(struct drm_connector *connector)
501 struct drm_device *dev = connector->dev;
502 struct intel_crt *crt = intel_attached_crt(connector);
504 if (HAS_PCH_SPLIT(dev))
505 crt->force_hotplug_required = 1;
509 * Routines for controlling stuff on the analog port
512 static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
513 .dpms = intel_crt_dpms,
514 .mode_fixup = intel_crt_mode_fixup,
515 .prepare = intel_encoder_prepare,
516 .commit = intel_encoder_commit,
517 .mode_set = intel_crt_mode_set,
520 static const struct drm_connector_funcs intel_crt_connector_funcs = {
521 .reset = intel_crt_reset,
522 .dpms = drm_helper_connector_dpms,
523 .detect = intel_crt_detect,
524 .fill_modes = drm_helper_probe_single_connector_modes,
525 .destroy = intel_crt_destroy,
526 .set_property = intel_crt_set_property,
529 static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
530 .mode_valid = intel_crt_mode_valid,
531 .get_modes = intel_crt_get_modes,
532 .best_encoder = intel_best_encoder,
535 static const struct drm_encoder_funcs intel_crt_enc_funcs = {
536 .destroy = intel_encoder_destroy,
539 static int intel_no_crt_dmi_callback(const struct dmi_system_id *id)
541 DRM_DEBUG_KMS("Skipping CRT initialization for %s\n", id->ident);
545 static const struct dmi_system_id intel_no_crt[] = {
547 .callback = intel_no_crt_dmi_callback,
550 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
551 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
557 void intel_crt_init(struct drm_device *dev)
559 struct drm_connector *connector;
560 struct intel_crt *crt;
561 struct intel_connector *intel_connector;
562 struct drm_i915_private *dev_priv = dev->dev_private;
564 /* Skip machines without VGA that falsely report hotplug events */
565 if (dmi_check_system(intel_no_crt))
568 crt = kmalloc(sizeof(struct intel_crt), DRM_MEM_KMS, M_WAITOK | M_ZERO);
569 intel_connector = kmalloc(sizeof(struct intel_connector), DRM_MEM_KMS,
572 connector = &intel_connector->base;
573 drm_connector_init(dev, &intel_connector->base,
574 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
576 drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
577 DRM_MODE_ENCODER_DAC);
579 intel_connector_attach_encoder(intel_connector, &crt->base);
581 crt->base.type = INTEL_OUTPUT_ANALOG;
582 crt->base.clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT |
583 1 << INTEL_ANALOG_CLONE_BIT |
584 1 << INTEL_SDVO_LVDS_CLONE_BIT);
585 crt->base.crtc_mask = (1 << 0) | (1 << 1);
587 connector->interlace_allowed = 0;
589 connector->interlace_allowed = 1;
590 connector->doublescan_allowed = 0;
592 drm_encoder_helper_add(&crt->base.base, &intel_crt_helper_funcs);
593 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
596 drm_sysfs_connector_add(connector);
599 if (I915_HAS_HOTPLUG(dev))
600 connector->polled = DRM_CONNECTOR_POLL_HPD;
602 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
605 * Configure the automatic hotplug detection stuff
607 crt->force_hotplug_required = 0;
608 if (HAS_PCH_SPLIT(dev)) {
611 adpa = I915_READ(PCH_ADPA);
612 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
613 adpa |= ADPA_HOTPLUG_BITS;
614 I915_WRITE(PCH_ADPA, adpa);
615 POSTING_READ(PCH_ADPA);
617 DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);
618 crt->force_hotplug_required = 1;
621 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;