blob: 5978490dac9265719e886219f65f3d021eab3e58 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
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
Duncan Laurie8ca40132011-10-25 15:42:21 -070027#include <linux/dmi.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080028#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include "drmP.h"
31#include "drm.h"
32#include "drm_crtc.h"
33#include "drm_crtc_helper.h"
David Müllerf5afcd32011-01-06 12:29:32 +000034#include "drm_edid.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "intel_drv.h"
36#include "i915_drm.h"
37#include "i915_drv.h"
38
Keith Packarde7dbb2f2010-11-16 16:03:53 +080039/* 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)
46
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +000047struct intel_crt {
48 struct intel_encoder base;
Keith Packarde7dbb2f2010-11-16 16:03:53 +080049 bool force_hotplug_required;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +000050};
51
52static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
53{
54 return container_of(intel_attached_encoder(connector),
55 struct intel_crt, base);
56}
57
Jesse Barnesdf0323c2012-04-17 15:06:33 -070058static void pch_crt_dpms(struct drm_encoder *encoder, int mode)
Jesse Barnes79e53942008-11-07 14:24:08 -080059{
60 struct drm_device *dev = encoder->dev;
61 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdf0323c2012-04-17 15:06:33 -070062 u32 temp;
Jesse Barnes79e53942008-11-07 14:24:08 -080063
Jesse Barnesdf0323c2012-04-17 15:06:33 -070064 temp = I915_READ(PCH_ADPA);
65 temp &= ~ADPA_DAC_ENABLE;
Zhenyu Wang2c072452009-06-05 15:38:42 +080066
Jesse Barnesdf0323c2012-04-17 15:06:33 -070067 switch (mode) {
68 case DRM_MODE_DPMS_ON:
69 temp |= ADPA_DAC_ENABLE;
70 break;
71 case DRM_MODE_DPMS_STANDBY:
72 case DRM_MODE_DPMS_SUSPEND:
73 case DRM_MODE_DPMS_OFF:
74 /* Just leave port enable cleared */
75 break;
76 }
77
78 I915_WRITE(PCH_ADPA, temp);
79}
80
81static void gmch_crt_dpms(struct drm_encoder *encoder, int mode)
82{
83 struct drm_device *dev = encoder->dev;
84 struct drm_i915_private *dev_priv = dev->dev_private;
85 u32 temp;
86
87 temp = I915_READ(ADPA);
Jesse Barnes79e53942008-11-07 14:24:08 -080088 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
ling.ma@intel.comfebc7692009-06-25 11:55:57 +080089 temp &= ~ADPA_DAC_ENABLE;
Jesse Barnes79e53942008-11-07 14:24:08 -080090
Akshay Joshi0206e352011-08-16 15:34:10 -040091 switch (mode) {
Jesse Barnes79e53942008-11-07 14:24:08 -080092 case DRM_MODE_DPMS_ON:
93 temp |= ADPA_DAC_ENABLE;
94 break;
95 case DRM_MODE_DPMS_STANDBY:
96 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
97 break;
98 case DRM_MODE_DPMS_SUSPEND:
99 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
100 break;
101 case DRM_MODE_DPMS_OFF:
102 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
103 break;
104 }
105
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700106 I915_WRITE(ADPA, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -0800107}
108
109static int intel_crt_mode_valid(struct drm_connector *connector,
110 struct drm_display_mode *mode)
111{
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800112 struct drm_device *dev = connector->dev;
113
114 int max_clock = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800115 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
116 return MODE_NO_DBLESCAN;
117
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800118 if (mode->clock < 25000)
119 return MODE_CLOCK_LOW;
120
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100121 if (IS_GEN2(dev))
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800122 max_clock = 350000;
123 else
124 max_clock = 400000;
125 if (mode->clock > max_clock)
126 return MODE_CLOCK_HIGH;
Jesse Barnes79e53942008-11-07 14:24:08 -0800127
128 return MODE_OK;
129}
130
131static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
132 struct drm_display_mode *mode,
133 struct drm_display_mode *adjusted_mode)
134{
135 return true;
136}
137
138static void intel_crt_mode_set(struct drm_encoder *encoder,
139 struct drm_display_mode *mode,
140 struct drm_display_mode *adjusted_mode)
141{
142
143 struct drm_device *dev = encoder->dev;
144 struct drm_crtc *crtc = encoder->crtc;
145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 int dpll_md_reg;
148 u32 adpa, dpll_md;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800149 u32 adpa_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -0800150
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800151 dpll_md_reg = DPLL_MD(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -0800152
Eric Anholtbad720f2009-10-22 16:11:14 -0700153 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800154 adpa_reg = PCH_ADPA;
155 else
156 adpa_reg = ADPA;
157
Jesse Barnes79e53942008-11-07 14:24:08 -0800158 /*
159 * Disable separate mode multiplier used when cloning SDVO to CRT
160 * XXX this needs to be adjusted when we really are cloning
161 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100162 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800163 dpll_md = I915_READ(dpll_md_reg);
164 I915_WRITE(dpll_md_reg,
165 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
166 }
167
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800168 adpa = ADPA_HOTPLUG_BITS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800169 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
170 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
171 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
172 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
173
Jesse Barnes75770562011-10-12 09:01:58 -0700174 /* For CPT allow 3 pipe config, for others just use A or B */
175 if (HAS_PCH_CPT(dev))
176 adpa |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
177 else if (intel_crtc->pipe == 0)
178 adpa |= ADPA_PIPE_A_SELECT;
179 else
180 adpa |= ADPA_PIPE_B_SELECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800181
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800182 if (!HAS_PCH_SPLIT(dev))
183 I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);
184
Zhenyu Wang2c072452009-06-05 15:38:42 +0800185 I915_WRITE(adpa_reg, adpa);
186}
187
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500188static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800189{
190 struct drm_device *dev = connector->dev;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800191 struct intel_crt *crt = intel_attached_crt(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800192 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800193 u32 adpa;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800194 bool ret;
195
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800196 /* The first time through, trigger an explicit detection cycle */
197 if (crt->force_hotplug_required) {
198 bool turn_off_dac = HAS_PCH_SPLIT(dev);
199 u32 save_adpa;
Zhenyu Wang67941da2009-07-24 01:00:33 +0800200
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800201 crt->force_hotplug_required = 0;
Dave Airlied5dd96c2010-08-04 15:52:19 +1000202
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800203 save_adpa = adpa = I915_READ(PCH_ADPA);
204 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
Dave Airlied5dd96c2010-08-04 15:52:19 +1000205
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800206 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
207 if (turn_off_dac)
208 adpa &= ~ADPA_DAC_ENABLE;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800209
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800210 I915_WRITE(PCH_ADPA, adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800211
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800212 if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
213 1000))
214 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
Zhenyu Wang2c072452009-06-05 15:38:42 +0800215
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800216 if (turn_off_dac) {
217 I915_WRITE(PCH_ADPA, save_adpa);
218 POSTING_READ(PCH_ADPA);
219 }
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800220 }
221
Zhenyu Wang2c072452009-06-05 15:38:42 +0800222 /* Check the status to see if both blue and green are on now */
223 adpa = I915_READ(PCH_ADPA);
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800224 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800225 ret = true;
226 else
227 ret = false;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800228 DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800229
Zhenyu Wang2c072452009-06-05 15:38:42 +0800230 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800231}
232
233/**
234 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
235 *
236 * Not for i915G/i915GM
237 *
238 * \return true if CRT is connected.
239 * \return false if CRT is disconnected.
240 */
241static bool intel_crt_detect_hotplug(struct drm_connector *connector)
242{
243 struct drm_device *dev = connector->dev;
244 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson7a772c42010-05-24 16:46:29 -0400245 u32 hotplug_en, orig, stat;
246 bool ret = false;
Zhao Yakui771cb082009-03-03 18:07:52 +0800247 int i, tries = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800248
Eric Anholtbad720f2009-10-22 16:11:14 -0700249 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500250 return intel_ironlake_crt_detect_hotplug(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800251
Zhao Yakui771cb082009-03-03 18:07:52 +0800252 /*
253 * On 4 series desktop, CRT detect sequence need to be done twice
254 * to get a reliable result.
255 */
Jesse Barnes79e53942008-11-07 14:24:08 -0800256
Zhao Yakui771cb082009-03-03 18:07:52 +0800257 if (IS_G4X(dev) && !IS_GM45(dev))
258 tries = 2;
259 else
260 tries = 1;
Adam Jackson7a772c42010-05-24 16:46:29 -0400261 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
Zhao Yakui771cb082009-03-03 18:07:52 +0800262 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800263
Zhao Yakui771cb082009-03-03 18:07:52 +0800264 for (i = 0; i < tries ; i++) {
Zhao Yakui771cb082009-03-03 18:07:52 +0800265 /* turn on the FORCE_DETECT */
266 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Zhao Yakui771cb082009-03-03 18:07:52 +0800267 /* wait for FORCE_DETECT to go off */
Chris Wilson913d8d12010-08-07 11:01:35 +0100268 if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
269 CRT_HOTPLUG_FORCE_DETECT) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100270 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100271 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
Zhao Yakui771cb082009-03-03 18:07:52 +0800272 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800273
Adam Jackson7a772c42010-05-24 16:46:29 -0400274 stat = I915_READ(PORT_HOTPLUG_STAT);
275 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
276 ret = true;
Jesse Barnes79e53942008-11-07 14:24:08 -0800277
Adam Jackson7a772c42010-05-24 16:46:29 -0400278 /* clear the interrupt we just generated, if any */
279 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
280
281 /* and put the bits back */
282 I915_WRITE(PORT_HOTPLUG_EN, orig);
283
284 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800285}
286
David Müllerf5afcd32011-01-06 12:29:32 +0000287static bool intel_crt_detect_ddc(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -0800288{
David Müllerf5afcd32011-01-06 12:29:32 +0000289 struct intel_crt *crt = intel_attached_crt(connector);
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000290 struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800291
292 /* CRT should always be at 0, but check anyway */
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000293 if (crt->base.type != INTEL_OUTPUT_ANALOG)
Jesse Barnes79e53942008-11-07 14:24:08 -0800294 return false;
295
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000296 if (intel_ddc_probe(&crt->base, dev_priv->crt_ddc_pin)) {
David Müllerf5afcd32011-01-06 12:29:32 +0000297 struct edid *edid;
298 bool is_digital = false;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800299 struct i2c_adapter *i2c;
David Müllerf5afcd32011-01-06 12:29:32 +0000300
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800301 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
302 edid = drm_get_edid(connector, i2c);
David Müllerf5afcd32011-01-06 12:29:32 +0000303 /*
304 * This may be a DVI-I connector with a shared DDC
305 * link between analog and digital outputs, so we
306 * have to check the EDID input spec of the attached device.
Chris Wilsond3bcb752011-05-12 22:17:15 +0100307 *
308 * On the other hand, what should we do if it is a broken EDID?
David Müllerf5afcd32011-01-06 12:29:32 +0000309 */
310 if (edid != NULL) {
311 is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
312 connector->display_info.raw_edid = NULL;
313 kfree(edid);
314 }
315
316 if (!is_digital) {
317 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
318 return true;
Chris Wilsond3bcb752011-05-12 22:17:15 +0100319 } else {
320 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
David Müllerf5afcd32011-01-06 12:29:32 +0000321 }
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100322 }
323
324 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800325}
326
Ma Linge4a5d542009-05-26 11:31:00 +0800327static enum drm_connector_status
Chris Wilson71731882011-04-19 23:10:58 +0100328intel_crt_load_detect(struct intel_crt *crt)
Ma Linge4a5d542009-05-26 11:31:00 +0800329{
Chris Wilson71731882011-04-19 23:10:58 +0100330 struct drm_device *dev = crt->base.base.dev;
Ma Linge4a5d542009-05-26 11:31:00 +0800331 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson71731882011-04-19 23:10:58 +0100332 uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
Ma Linge4a5d542009-05-26 11:31:00 +0800333 uint32_t save_bclrpat;
334 uint32_t save_vtotal;
335 uint32_t vtotal, vactive;
336 uint32_t vsample;
337 uint32_t vblank, vblank_start, vblank_end;
338 uint32_t dsl;
339 uint32_t bclrpat_reg;
340 uint32_t vtotal_reg;
341 uint32_t vblank_reg;
342 uint32_t vsync_reg;
343 uint32_t pipeconf_reg;
344 uint32_t pipe_dsl_reg;
345 uint8_t st00;
346 enum drm_connector_status status;
347
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100348 DRM_DEBUG_KMS("starting load-detect on CRT\n");
349
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800350 bclrpat_reg = BCLRPAT(pipe);
351 vtotal_reg = VTOTAL(pipe);
352 vblank_reg = VBLANK(pipe);
353 vsync_reg = VSYNC(pipe);
354 pipeconf_reg = PIPECONF(pipe);
355 pipe_dsl_reg = PIPEDSL(pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800356
357 save_bclrpat = I915_READ(bclrpat_reg);
358 save_vtotal = I915_READ(vtotal_reg);
359 vblank = I915_READ(vblank_reg);
360
361 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
362 vactive = (save_vtotal & 0x7ff) + 1;
363
364 vblank_start = (vblank & 0xfff) + 1;
365 vblank_end = ((vblank >> 16) & 0xfff) + 1;
366
367 /* Set the border color to purple. */
368 I915_WRITE(bclrpat_reg, 0x500050);
369
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100370 if (!IS_GEN2(dev)) {
Ma Linge4a5d542009-05-26 11:31:00 +0800371 uint32_t pipeconf = I915_READ(pipeconf_reg);
372 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
Chris Wilson19c55da2010-08-09 14:50:53 +0100373 POSTING_READ(pipeconf_reg);
Ma Linge4a5d542009-05-26 11:31:00 +0800374 /* Wait for next Vblank to substitue
375 * border color for Color info */
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700376 intel_wait_for_vblank(dev, pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800377 st00 = I915_READ8(VGA_MSR_WRITE);
378 status = ((st00 & (1 << 4)) != 0) ?
379 connector_status_connected :
380 connector_status_disconnected;
381
382 I915_WRITE(pipeconf_reg, pipeconf);
383 } else {
384 bool restore_vblank = false;
385 int count, detect;
386
387 /*
388 * If there isn't any border, add some.
389 * Yes, this will flicker
390 */
391 if (vblank_start <= vactive && vblank_end >= vtotal) {
392 uint32_t vsync = I915_READ(vsync_reg);
393 uint32_t vsync_start = (vsync & 0xffff) + 1;
394
395 vblank_start = vsync_start;
396 I915_WRITE(vblank_reg,
397 (vblank_start - 1) |
398 ((vblank_end - 1) << 16));
399 restore_vblank = true;
400 }
401 /* sample in the vertical border, selecting the larger one */
402 if (vblank_start - vactive >= vtotal - vblank_end)
403 vsample = (vblank_start + vactive) >> 1;
404 else
405 vsample = (vtotal + vblank_end) >> 1;
406
407 /*
408 * Wait for the border to be displayed
409 */
410 while (I915_READ(pipe_dsl_reg) >= vactive)
411 ;
412 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
413 ;
414 /*
415 * Watch ST00 for an entire scanline
416 */
417 detect = 0;
418 count = 0;
419 do {
420 count++;
421 /* Read the ST00 VGA status register */
422 st00 = I915_READ8(VGA_MSR_WRITE);
423 if (st00 & (1 << 4))
424 detect++;
425 } while ((I915_READ(pipe_dsl_reg) == dsl));
426
427 /* restore vblank if necessary */
428 if (restore_vblank)
429 I915_WRITE(vblank_reg, vblank);
430 /*
431 * If more than 3/4 of the scanline detected a monitor,
432 * then it is assumed to be present. This works even on i830,
433 * where there isn't any way to force the border color across
434 * the screen
435 */
436 status = detect * 4 > count * 3 ?
437 connector_status_connected :
438 connector_status_disconnected;
439 }
440
441 /* Restore previous settings */
442 I915_WRITE(bclrpat_reg, save_bclrpat);
443
444 return status;
445}
446
Chris Wilson7b334fc2010-09-09 23:51:02 +0100447static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100448intel_crt_detect(struct drm_connector *connector, bool force)
Jesse Barnes79e53942008-11-07 14:24:08 -0800449{
450 struct drm_device *dev = connector->dev;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000451 struct intel_crt *crt = intel_attached_crt(connector);
Ma Linge4a5d542009-05-26 11:31:00 +0800452 enum drm_connector_status status;
Daniel Vettere95c8432012-04-20 21:03:36 +0200453 struct intel_load_detect_pipe tmp;
Jesse Barnes79e53942008-11-07 14:24:08 -0800454
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100455 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetteraaa37732012-06-16 15:30:32 +0200456 /* We can not rely on the HPD pin always being correctly wired
457 * up, for example many KVM do not pass it through, and so
458 * only trust an assertion that the monitor is connected.
459 */
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100460 if (intel_crt_detect_hotplug(connector)) {
461 DRM_DEBUG_KMS("CRT detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800462 return connector_status_connected;
Daniel Vetteraaa37732012-06-16 15:30:32 +0200463 } else
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800464 DRM_DEBUG_KMS("CRT not detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800465 }
466
David Müllerf5afcd32011-01-06 12:29:32 +0000467 if (intel_crt_detect_ddc(connector))
Jesse Barnes79e53942008-11-07 14:24:08 -0800468 return connector_status_connected;
469
Daniel Vetteraaa37732012-06-16 15:30:32 +0200470 /* Load detection is broken on HPD capable machines. Whoever wants a
471 * broken monitor (without edid) to work behind a broken kvm (that fails
472 * to have the right resistors for HP detection) needs to fix this up.
473 * For now just bail out. */
474 if (I915_HAS_HOTPLUG(dev))
475 return connector_status_disconnected;
476
Chris Wilson930a9e22010-09-14 11:07:23 +0100477 if (!force)
Chris Wilson7b334fc2010-09-09 23:51:02 +0100478 return connector->status;
479
Ma Linge4a5d542009-05-26 11:31:00 +0800480 /* for pre-945g platforms use load detect */
Daniel Vettere95c8432012-04-20 21:03:36 +0200481 if (intel_get_load_detect_pipe(&crt->base, connector, NULL,
482 &tmp)) {
483 if (intel_crt_detect_ddc(connector))
484 status = connector_status_connected;
485 else
486 status = intel_crt_load_detect(crt);
487 intel_release_load_detect_pipe(&crt->base, connector,
488 &tmp);
489 } else
490 status = connector_status_unknown;
Ma Linge4a5d542009-05-26 11:31:00 +0800491
492 return status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800493}
494
495static void intel_crt_destroy(struct drm_connector *connector)
496{
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 drm_sysfs_connector_remove(connector);
498 drm_connector_cleanup(connector);
499 kfree(connector);
500}
501
502static int intel_crt_get_modes(struct drm_connector *connector)
503{
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800504 struct drm_device *dev = connector->dev;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700505 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson890f3352010-09-14 16:46:59 +0100506 int ret;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800507 struct i2c_adapter *i2c;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800508
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800509 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
510 ret = intel_ddc_get_modes(connector, i2c);
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800511 if (ret || !IS_G4X(dev))
Chris Wilsonf899fc62010-07-20 15:44:45 -0700512 return ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800513
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800514 /* Try to probe digital port for output in DVI-I -> VGA mode. */
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800515 i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
516 return intel_ddc_get_modes(connector, i2c);
Jesse Barnes79e53942008-11-07 14:24:08 -0800517}
518
519static int intel_crt_set_property(struct drm_connector *connector,
520 struct drm_property *property,
521 uint64_t value)
522{
Jesse Barnes79e53942008-11-07 14:24:08 -0800523 return 0;
524}
525
Chris Wilsonf3269052011-01-24 15:17:08 +0000526static void intel_crt_reset(struct drm_connector *connector)
527{
528 struct drm_device *dev = connector->dev;
529 struct intel_crt *crt = intel_attached_crt(connector);
530
531 if (HAS_PCH_SPLIT(dev))
532 crt->force_hotplug_required = 1;
533}
534
Jesse Barnes79e53942008-11-07 14:24:08 -0800535/*
536 * Routines for controlling stuff on the analog port
537 */
538
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700539static const struct drm_encoder_helper_funcs pch_encoder_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800540 .mode_fixup = intel_crt_mode_fixup,
541 .prepare = intel_encoder_prepare,
542 .commit = intel_encoder_commit,
543 .mode_set = intel_crt_mode_set,
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700544 .dpms = pch_crt_dpms,
545};
546
547static const struct drm_encoder_helper_funcs gmch_encoder_funcs = {
548 .mode_fixup = intel_crt_mode_fixup,
549 .prepare = intel_encoder_prepare,
550 .commit = intel_encoder_commit,
551 .mode_set = intel_crt_mode_set,
552 .dpms = gmch_crt_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800553};
554
555static const struct drm_connector_funcs intel_crt_connector_funcs = {
Chris Wilsonf3269052011-01-24 15:17:08 +0000556 .reset = intel_crt_reset,
Keith Packardc9fb15f2009-05-30 20:42:28 -0700557 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800558 .detect = intel_crt_detect,
559 .fill_modes = drm_helper_probe_single_connector_modes,
560 .destroy = intel_crt_destroy,
561 .set_property = intel_crt_set_property,
562};
563
564static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
565 .mode_valid = intel_crt_mode_valid,
566 .get_modes = intel_crt_get_modes,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100567 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800568};
569
Jesse Barnes79e53942008-11-07 14:24:08 -0800570static const struct drm_encoder_funcs intel_crt_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100571 .destroy = intel_encoder_destroy,
Jesse Barnes79e53942008-11-07 14:24:08 -0800572};
573
Duncan Laurie8ca40132011-10-25 15:42:21 -0700574static int __init intel_no_crt_dmi_callback(const struct dmi_system_id *id)
575{
Daniel Vetterbc0daf42012-04-01 13:16:49 +0200576 DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
Duncan Laurie8ca40132011-10-25 15:42:21 -0700577 return 1;
578}
579
580static const struct dmi_system_id intel_no_crt[] = {
581 {
582 .callback = intel_no_crt_dmi_callback,
583 .ident = "ACER ZGB",
584 .matches = {
585 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
586 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
587 },
588 },
589 { }
590};
591
Jesse Barnes79e53942008-11-07 14:24:08 -0800592void intel_crt_init(struct drm_device *dev)
593{
594 struct drm_connector *connector;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000595 struct intel_crt *crt;
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800596 struct intel_connector *intel_connector;
David Müller (ELSOFT AG)db545012009-08-29 08:54:45 +0200597 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700598 const struct drm_encoder_helper_funcs *encoder_helper_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -0800599
Duncan Laurie8ca40132011-10-25 15:42:21 -0700600 /* Skip machines without VGA that falsely report hotplug events */
601 if (dmi_check_system(intel_no_crt))
602 return;
603
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000604 crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
605 if (!crt)
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 return;
607
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800608 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
609 if (!intel_connector) {
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000610 kfree(crt);
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800611 return;
612 }
613
614 connector = &intel_connector->base;
615 drm_connector_init(dev, &intel_connector->base,
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
617
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000618 drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800619 DRM_MODE_ENCODER_DAC);
620
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000621 intel_connector_attach_encoder(intel_connector, &crt->base);
Jesse Barnes79e53942008-11-07 14:24:08 -0800622
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000623 crt->base.type = INTEL_OUTPUT_ANALOG;
624 crt->base.clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT |
625 1 << INTEL_ANALOG_CLONE_BIT |
626 1 << INTEL_SDVO_LVDS_CLONE_BIT);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -0300627 if (IS_HASWELL(dev))
628 crt->base.crtc_mask = (1 << 0);
629 else
630 crt->base.crtc_mask = (1 << 0) | (1 << 1);
631
Daniel Vetterdbb02572012-01-28 14:49:23 +0100632 if (IS_GEN2(dev))
633 connector->interlace_allowed = 0;
634 else
635 connector->interlace_allowed = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 connector->doublescan_allowed = 0;
637
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700638 if (HAS_PCH_SPLIT(dev))
639 encoder_helper_funcs = &pch_encoder_funcs;
640 else
641 encoder_helper_funcs = &gmch_encoder_funcs;
642
643 drm_encoder_helper_add(&crt->base.base, encoder_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -0800644 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
645
646 drm_sysfs_connector_add(connector);
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800647
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000648 if (I915_HAS_HOTPLUG(dev))
649 connector->polled = DRM_CONNECTOR_POLL_HPD;
650 else
651 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
652
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800653 /*
654 * Configure the automatic hotplug detection stuff
655 */
656 crt->force_hotplug_required = 0;
657 if (HAS_PCH_SPLIT(dev)) {
658 u32 adpa;
659
660 adpa = I915_READ(PCH_ADPA);
661 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
662 adpa |= ADPA_HOTPLUG_BITS;
663 I915_WRITE(PCH_ADPA, adpa);
664 POSTING_READ(PCH_ADPA);
665
666 DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);
667 crt->force_hotplug_required = 1;
668 }
669
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800670 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800671}