blob: 61d55d3141c7bbb9e0ecf00a085872e54fcfbc88 [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
Jesse Barnesbd9e8412012-06-15 11:55:18 -070091 if (IS_VALLEYVIEW(dev) && mode != DRM_MODE_DPMS_ON)
92 mode = DRM_MODE_DPMS_OFF;
93
Akshay Joshi0206e352011-08-16 15:34:10 -040094 switch (mode) {
Jesse Barnes79e53942008-11-07 14:24:08 -080095 case DRM_MODE_DPMS_ON:
96 temp |= ADPA_DAC_ENABLE;
97 break;
98 case DRM_MODE_DPMS_STANDBY:
99 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
100 break;
101 case DRM_MODE_DPMS_SUSPEND:
102 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
103 break;
104 case DRM_MODE_DPMS_OFF:
105 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
106 break;
107 }
108
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700109 I915_WRITE(ADPA, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -0800110}
111
112static int intel_crt_mode_valid(struct drm_connector *connector,
113 struct drm_display_mode *mode)
114{
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800115 struct drm_device *dev = connector->dev;
116
117 int max_clock = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800118 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
119 return MODE_NO_DBLESCAN;
120
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800121 if (mode->clock < 25000)
122 return MODE_CLOCK_LOW;
123
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100124 if (IS_GEN2(dev))
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800125 max_clock = 350000;
126 else
127 max_clock = 400000;
128 if (mode->clock > max_clock)
129 return MODE_CLOCK_HIGH;
Jesse Barnes79e53942008-11-07 14:24:08 -0800130
131 return MODE_OK;
132}
133
134static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
135 struct drm_display_mode *mode,
136 struct drm_display_mode *adjusted_mode)
137{
138 return true;
139}
140
141static void intel_crt_mode_set(struct drm_encoder *encoder,
142 struct drm_display_mode *mode,
143 struct drm_display_mode *adjusted_mode)
144{
145
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;
150 int dpll_md_reg;
151 u32 adpa, dpll_md;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800152 u32 adpa_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -0800153
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800154 dpll_md_reg = DPLL_MD(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -0800155
Eric Anholtbad720f2009-10-22 16:11:14 -0700156 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800157 adpa_reg = PCH_ADPA;
158 else
159 adpa_reg = ADPA;
160
Jesse Barnes79e53942008-11-07 14:24:08 -0800161 /*
162 * Disable separate mode multiplier used when cloning SDVO to CRT
163 * XXX this needs to be adjusted when we really are cloning
164 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100165 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800166 dpll_md = I915_READ(dpll_md_reg);
167 I915_WRITE(dpll_md_reg,
168 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
169 }
170
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800171 adpa = ADPA_HOTPLUG_BITS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800172 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;
176
Jesse Barnes75770562011-10-12 09:01:58 -0700177 /* 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;
182 else
183 adpa |= ADPA_PIPE_B_SELECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800184
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800185 if (!HAS_PCH_SPLIT(dev))
186 I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);
187
Zhenyu Wang2c072452009-06-05 15:38:42 +0800188 I915_WRITE(adpa_reg, adpa);
189}
190
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500191static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800192{
193 struct drm_device *dev = connector->dev;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800194 struct intel_crt *crt = intel_attached_crt(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800195 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800196 u32 adpa;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800197 bool ret;
198
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800199 /* The first time through, trigger an explicit detection cycle */
200 if (crt->force_hotplug_required) {
201 bool turn_off_dac = HAS_PCH_SPLIT(dev);
202 u32 save_adpa;
Zhenyu Wang67941da2009-07-24 01:00:33 +0800203
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800204 crt->force_hotplug_required = 0;
Dave Airlied5dd96c2010-08-04 15:52:19 +1000205
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800206 save_adpa = adpa = I915_READ(PCH_ADPA);
207 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
Dave Airlied5dd96c2010-08-04 15:52:19 +1000208
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800209 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
210 if (turn_off_dac)
211 adpa &= ~ADPA_DAC_ENABLE;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800212
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800213 I915_WRITE(PCH_ADPA, adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800214
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800215 if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
216 1000))
217 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
Zhenyu Wang2c072452009-06-05 15:38:42 +0800218
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800219 if (turn_off_dac) {
220 I915_WRITE(PCH_ADPA, save_adpa);
221 POSTING_READ(PCH_ADPA);
222 }
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800223 }
224
Zhenyu Wang2c072452009-06-05 15:38:42 +0800225 /* Check the status to see if both blue and green are on now */
226 adpa = I915_READ(PCH_ADPA);
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800227 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800228 ret = true;
229 else
230 ret = false;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800231 DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800232
Zhenyu Wang2c072452009-06-05 15:38:42 +0800233 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800234}
235
Jesse Barnes7d2c24e2012-06-15 11:55:15 -0700236static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
237{
238 struct drm_device *dev = connector->dev;
239 struct drm_i915_private *dev_priv = dev->dev_private;
240 u32 adpa;
241 bool ret;
242 u32 save_adpa;
243
244 save_adpa = adpa = I915_READ(ADPA);
245 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
246
247 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
248
249 I915_WRITE(ADPA, adpa);
250
251 if (wait_for((I915_READ(ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
252 1000)) {
253 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
254 I915_WRITE(ADPA, save_adpa);
255 }
256
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)
260 ret = true;
261 else
262 ret = false;
263
264 DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
265
266 /* FIXME: debug force function and remove */
267 ret = true;
268
269 return ret;
270}
271
Jesse Barnes79e53942008-11-07 14:24:08 -0800272/**
273 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
274 *
275 * Not for i915G/i915GM
276 *
277 * \return true if CRT is connected.
278 * \return false if CRT is disconnected.
279 */
280static bool intel_crt_detect_hotplug(struct drm_connector *connector)
281{
282 struct drm_device *dev = connector->dev;
283 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson7a772c42010-05-24 16:46:29 -0400284 u32 hotplug_en, orig, stat;
285 bool ret = false;
Zhao Yakui771cb082009-03-03 18:07:52 +0800286 int i, tries = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800287
Eric Anholtbad720f2009-10-22 16:11:14 -0700288 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500289 return intel_ironlake_crt_detect_hotplug(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800290
Jesse Barnes7d2c24e2012-06-15 11:55:15 -0700291 if (IS_VALLEYVIEW(dev))
292 return valleyview_crt_detect_hotplug(connector);
293
Zhao Yakui771cb082009-03-03 18:07:52 +0800294 /*
295 * On 4 series desktop, CRT detect sequence need to be done twice
296 * to get a reliable result.
297 */
Jesse Barnes79e53942008-11-07 14:24:08 -0800298
Zhao Yakui771cb082009-03-03 18:07:52 +0800299 if (IS_G4X(dev) && !IS_GM45(dev))
300 tries = 2;
301 else
302 tries = 1;
Adam Jackson7a772c42010-05-24 16:46:29 -0400303 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
Zhao Yakui771cb082009-03-03 18:07:52 +0800304 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800305
Zhao Yakui771cb082009-03-03 18:07:52 +0800306 for (i = 0; i < tries ; i++) {
Zhao Yakui771cb082009-03-03 18:07:52 +0800307 /* turn on the FORCE_DETECT */
308 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Zhao Yakui771cb082009-03-03 18:07:52 +0800309 /* wait for FORCE_DETECT to go off */
Chris Wilson913d8d12010-08-07 11:01:35 +0100310 if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
311 CRT_HOTPLUG_FORCE_DETECT) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100312 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100313 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
Zhao Yakui771cb082009-03-03 18:07:52 +0800314 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800315
Adam Jackson7a772c42010-05-24 16:46:29 -0400316 stat = I915_READ(PORT_HOTPLUG_STAT);
317 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
318 ret = true;
Jesse Barnes79e53942008-11-07 14:24:08 -0800319
Adam Jackson7a772c42010-05-24 16:46:29 -0400320 /* clear the interrupt we just generated, if any */
321 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
322
323 /* and put the bits back */
324 I915_WRITE(PORT_HOTPLUG_EN, orig);
325
326 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800327}
328
David Müllerf5afcd32011-01-06 12:29:32 +0000329static bool intel_crt_detect_ddc(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -0800330{
David Müllerf5afcd32011-01-06 12:29:32 +0000331 struct intel_crt *crt = intel_attached_crt(connector);
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000332 struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800333
334 /* CRT should always be at 0, but check anyway */
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000335 if (crt->base.type != INTEL_OUTPUT_ANALOG)
Jesse Barnes79e53942008-11-07 14:24:08 -0800336 return false;
337
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000338 if (intel_ddc_probe(&crt->base, dev_priv->crt_ddc_pin)) {
David Müllerf5afcd32011-01-06 12:29:32 +0000339 struct edid *edid;
340 bool is_digital = false;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800341 struct i2c_adapter *i2c;
David Müllerf5afcd32011-01-06 12:29:32 +0000342
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800343 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
344 edid = drm_get_edid(connector, i2c);
David Müllerf5afcd32011-01-06 12:29:32 +0000345 /*
346 * This may be a DVI-I connector with a shared DDC
347 * link between analog and digital outputs, so we
348 * have to check the EDID input spec of the attached device.
Chris Wilsond3bcb752011-05-12 22:17:15 +0100349 *
350 * On the other hand, what should we do if it is a broken EDID?
David Müllerf5afcd32011-01-06 12:29:32 +0000351 */
352 if (edid != NULL) {
353 is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
354 connector->display_info.raw_edid = NULL;
355 kfree(edid);
356 }
357
358 if (!is_digital) {
359 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
360 return true;
Chris Wilsond3bcb752011-05-12 22:17:15 +0100361 } else {
362 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
David Müllerf5afcd32011-01-06 12:29:32 +0000363 }
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100364 }
365
366 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800367}
368
Ma Linge4a5d542009-05-26 11:31:00 +0800369static enum drm_connector_status
Chris Wilson71731882011-04-19 23:10:58 +0100370intel_crt_load_detect(struct intel_crt *crt)
Ma Linge4a5d542009-05-26 11:31:00 +0800371{
Chris Wilson71731882011-04-19 23:10:58 +0100372 struct drm_device *dev = crt->base.base.dev;
Ma Linge4a5d542009-05-26 11:31:00 +0800373 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson71731882011-04-19 23:10:58 +0100374 uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
Ma Linge4a5d542009-05-26 11:31:00 +0800375 uint32_t save_bclrpat;
376 uint32_t save_vtotal;
377 uint32_t vtotal, vactive;
378 uint32_t vsample;
379 uint32_t vblank, vblank_start, vblank_end;
380 uint32_t dsl;
381 uint32_t bclrpat_reg;
382 uint32_t vtotal_reg;
383 uint32_t vblank_reg;
384 uint32_t vsync_reg;
385 uint32_t pipeconf_reg;
386 uint32_t pipe_dsl_reg;
387 uint8_t st00;
388 enum drm_connector_status status;
389
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100390 DRM_DEBUG_KMS("starting load-detect on CRT\n");
391
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800392 bclrpat_reg = BCLRPAT(pipe);
393 vtotal_reg = VTOTAL(pipe);
394 vblank_reg = VBLANK(pipe);
395 vsync_reg = VSYNC(pipe);
396 pipeconf_reg = PIPECONF(pipe);
397 pipe_dsl_reg = PIPEDSL(pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800398
399 save_bclrpat = I915_READ(bclrpat_reg);
400 save_vtotal = I915_READ(vtotal_reg);
401 vblank = I915_READ(vblank_reg);
402
403 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
404 vactive = (save_vtotal & 0x7ff) + 1;
405
406 vblank_start = (vblank & 0xfff) + 1;
407 vblank_end = ((vblank >> 16) & 0xfff) + 1;
408
409 /* Set the border color to purple. */
410 I915_WRITE(bclrpat_reg, 0x500050);
411
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100412 if (!IS_GEN2(dev)) {
Ma Linge4a5d542009-05-26 11:31:00 +0800413 uint32_t pipeconf = I915_READ(pipeconf_reg);
414 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
Chris Wilson19c55da2010-08-09 14:50:53 +0100415 POSTING_READ(pipeconf_reg);
Ma Linge4a5d542009-05-26 11:31:00 +0800416 /* Wait for next Vblank to substitue
417 * border color for Color info */
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700418 intel_wait_for_vblank(dev, pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800419 st00 = I915_READ8(VGA_MSR_WRITE);
420 status = ((st00 & (1 << 4)) != 0) ?
421 connector_status_connected :
422 connector_status_disconnected;
423
424 I915_WRITE(pipeconf_reg, pipeconf);
425 } else {
426 bool restore_vblank = false;
427 int count, detect;
428
429 /*
430 * If there isn't any border, add some.
431 * Yes, this will flicker
432 */
433 if (vblank_start <= vactive && vblank_end >= vtotal) {
434 uint32_t vsync = I915_READ(vsync_reg);
435 uint32_t vsync_start = (vsync & 0xffff) + 1;
436
437 vblank_start = vsync_start;
438 I915_WRITE(vblank_reg,
439 (vblank_start - 1) |
440 ((vblank_end - 1) << 16));
441 restore_vblank = true;
442 }
443 /* sample in the vertical border, selecting the larger one */
444 if (vblank_start - vactive >= vtotal - vblank_end)
445 vsample = (vblank_start + vactive) >> 1;
446 else
447 vsample = (vtotal + vblank_end) >> 1;
448
449 /*
450 * Wait for the border to be displayed
451 */
452 while (I915_READ(pipe_dsl_reg) >= vactive)
453 ;
454 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
455 ;
456 /*
457 * Watch ST00 for an entire scanline
458 */
459 detect = 0;
460 count = 0;
461 do {
462 count++;
463 /* Read the ST00 VGA status register */
464 st00 = I915_READ8(VGA_MSR_WRITE);
465 if (st00 & (1 << 4))
466 detect++;
467 } while ((I915_READ(pipe_dsl_reg) == dsl));
468
469 /* restore vblank if necessary */
470 if (restore_vblank)
471 I915_WRITE(vblank_reg, vblank);
472 /*
473 * If more than 3/4 of the scanline detected a monitor,
474 * then it is assumed to be present. This works even on i830,
475 * where there isn't any way to force the border color across
476 * the screen
477 */
478 status = detect * 4 > count * 3 ?
479 connector_status_connected :
480 connector_status_disconnected;
481 }
482
483 /* Restore previous settings */
484 I915_WRITE(bclrpat_reg, save_bclrpat);
485
486 return status;
487}
488
Chris Wilson7b334fc2010-09-09 23:51:02 +0100489static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100490intel_crt_detect(struct drm_connector *connector, bool force)
Jesse Barnes79e53942008-11-07 14:24:08 -0800491{
492 struct drm_device *dev = connector->dev;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000493 struct intel_crt *crt = intel_attached_crt(connector);
Ma Linge4a5d542009-05-26 11:31:00 +0800494 enum drm_connector_status status;
Daniel Vettere95c8432012-04-20 21:03:36 +0200495 struct intel_load_detect_pipe tmp;
Jesse Barnes79e53942008-11-07 14:24:08 -0800496
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100497 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetteraaa37732012-06-16 15:30:32 +0200498 /* We can not rely on the HPD pin always being correctly wired
499 * up, for example many KVM do not pass it through, and so
500 * only trust an assertion that the monitor is connected.
501 */
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100502 if (intel_crt_detect_hotplug(connector)) {
503 DRM_DEBUG_KMS("CRT detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800504 return connector_status_connected;
Daniel Vetteraaa37732012-06-16 15:30:32 +0200505 } else
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800506 DRM_DEBUG_KMS("CRT not detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800507 }
508
David Müllerf5afcd32011-01-06 12:29:32 +0000509 if (intel_crt_detect_ddc(connector))
Jesse Barnes79e53942008-11-07 14:24:08 -0800510 return connector_status_connected;
511
Daniel Vetteraaa37732012-06-16 15:30:32 +0200512 /* Load detection is broken on HPD capable machines. Whoever wants a
513 * broken monitor (without edid) to work behind a broken kvm (that fails
514 * to have the right resistors for HP detection) needs to fix this up.
515 * For now just bail out. */
516 if (I915_HAS_HOTPLUG(dev))
517 return connector_status_disconnected;
518
Chris Wilson930a9e22010-09-14 11:07:23 +0100519 if (!force)
Chris Wilson7b334fc2010-09-09 23:51:02 +0100520 return connector->status;
521
Ma Linge4a5d542009-05-26 11:31:00 +0800522 /* for pre-945g platforms use load detect */
Daniel Vettere95c8432012-04-20 21:03:36 +0200523 if (intel_get_load_detect_pipe(&crt->base, connector, NULL,
524 &tmp)) {
525 if (intel_crt_detect_ddc(connector))
526 status = connector_status_connected;
527 else
528 status = intel_crt_load_detect(crt);
529 intel_release_load_detect_pipe(&crt->base, connector,
530 &tmp);
531 } else
532 status = connector_status_unknown;
Ma Linge4a5d542009-05-26 11:31:00 +0800533
534 return status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800535}
536
537static void intel_crt_destroy(struct drm_connector *connector)
538{
Jesse Barnes79e53942008-11-07 14:24:08 -0800539 drm_sysfs_connector_remove(connector);
540 drm_connector_cleanup(connector);
541 kfree(connector);
542}
543
544static int intel_crt_get_modes(struct drm_connector *connector)
545{
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800546 struct drm_device *dev = connector->dev;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700547 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson890f3352010-09-14 16:46:59 +0100548 int ret;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800549 struct i2c_adapter *i2c;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800550
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800551 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
552 ret = intel_ddc_get_modes(connector, i2c);
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800553 if (ret || !IS_G4X(dev))
Chris Wilsonf899fc62010-07-20 15:44:45 -0700554 return ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800555
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800556 /* Try to probe digital port for output in DVI-I -> VGA mode. */
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800557 i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
558 return intel_ddc_get_modes(connector, i2c);
Jesse Barnes79e53942008-11-07 14:24:08 -0800559}
560
561static int intel_crt_set_property(struct drm_connector *connector,
562 struct drm_property *property,
563 uint64_t value)
564{
Jesse Barnes79e53942008-11-07 14:24:08 -0800565 return 0;
566}
567
Chris Wilsonf3269052011-01-24 15:17:08 +0000568static void intel_crt_reset(struct drm_connector *connector)
569{
570 struct drm_device *dev = connector->dev;
571 struct intel_crt *crt = intel_attached_crt(connector);
572
573 if (HAS_PCH_SPLIT(dev))
574 crt->force_hotplug_required = 1;
575}
576
Jesse Barnes79e53942008-11-07 14:24:08 -0800577/*
578 * Routines for controlling stuff on the analog port
579 */
580
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700581static const struct drm_encoder_helper_funcs pch_encoder_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800582 .mode_fixup = intel_crt_mode_fixup,
583 .prepare = intel_encoder_prepare,
584 .commit = intel_encoder_commit,
585 .mode_set = intel_crt_mode_set,
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700586 .dpms = pch_crt_dpms,
587};
588
589static const struct drm_encoder_helper_funcs gmch_encoder_funcs = {
590 .mode_fixup = intel_crt_mode_fixup,
591 .prepare = intel_encoder_prepare,
592 .commit = intel_encoder_commit,
593 .mode_set = intel_crt_mode_set,
594 .dpms = gmch_crt_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800595};
596
597static const struct drm_connector_funcs intel_crt_connector_funcs = {
Chris Wilsonf3269052011-01-24 15:17:08 +0000598 .reset = intel_crt_reset,
Keith Packardc9fb15f2009-05-30 20:42:28 -0700599 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800600 .detect = intel_crt_detect,
601 .fill_modes = drm_helper_probe_single_connector_modes,
602 .destroy = intel_crt_destroy,
603 .set_property = intel_crt_set_property,
604};
605
606static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
607 .mode_valid = intel_crt_mode_valid,
608 .get_modes = intel_crt_get_modes,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100609 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800610};
611
Jesse Barnes79e53942008-11-07 14:24:08 -0800612static const struct drm_encoder_funcs intel_crt_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100613 .destroy = intel_encoder_destroy,
Jesse Barnes79e53942008-11-07 14:24:08 -0800614};
615
Duncan Laurie8ca40132011-10-25 15:42:21 -0700616static int __init intel_no_crt_dmi_callback(const struct dmi_system_id *id)
617{
Daniel Vetterbc0daf42012-04-01 13:16:49 +0200618 DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
Duncan Laurie8ca40132011-10-25 15:42:21 -0700619 return 1;
620}
621
622static const struct dmi_system_id intel_no_crt[] = {
623 {
624 .callback = intel_no_crt_dmi_callback,
625 .ident = "ACER ZGB",
626 .matches = {
627 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
628 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
629 },
630 },
631 { }
632};
633
Jesse Barnes79e53942008-11-07 14:24:08 -0800634void intel_crt_init(struct drm_device *dev)
635{
636 struct drm_connector *connector;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000637 struct intel_crt *crt;
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800638 struct intel_connector *intel_connector;
David Müller (ELSOFT AG)db545012009-08-29 08:54:45 +0200639 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700640 const struct drm_encoder_helper_funcs *encoder_helper_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -0800641
Duncan Laurie8ca40132011-10-25 15:42:21 -0700642 /* Skip machines without VGA that falsely report hotplug events */
643 if (dmi_check_system(intel_no_crt))
644 return;
645
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000646 crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
647 if (!crt)
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 return;
649
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800650 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
651 if (!intel_connector) {
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000652 kfree(crt);
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800653 return;
654 }
655
656 connector = &intel_connector->base;
657 drm_connector_init(dev, &intel_connector->base,
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
659
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000660 drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 DRM_MODE_ENCODER_DAC);
662
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000663 intel_connector_attach_encoder(intel_connector, &crt->base);
Jesse Barnes79e53942008-11-07 14:24:08 -0800664
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000665 crt->base.type = INTEL_OUTPUT_ANALOG;
666 crt->base.clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT |
667 1 << INTEL_ANALOG_CLONE_BIT |
668 1 << INTEL_SDVO_LVDS_CLONE_BIT);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -0300669 if (IS_HASWELL(dev))
670 crt->base.crtc_mask = (1 << 0);
671 else
672 crt->base.crtc_mask = (1 << 0) | (1 << 1);
673
Daniel Vetterdbb02572012-01-28 14:49:23 +0100674 if (IS_GEN2(dev))
675 connector->interlace_allowed = 0;
676 else
677 connector->interlace_allowed = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -0800678 connector->doublescan_allowed = 0;
679
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700680 if (HAS_PCH_SPLIT(dev))
681 encoder_helper_funcs = &pch_encoder_funcs;
682 else
683 encoder_helper_funcs = &gmch_encoder_funcs;
684
685 drm_encoder_helper_add(&crt->base.base, encoder_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -0800686 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
687
688 drm_sysfs_connector_add(connector);
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800689
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000690 if (I915_HAS_HOTPLUG(dev))
691 connector->polled = DRM_CONNECTOR_POLL_HPD;
692 else
693 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
694
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800695 /*
696 * Configure the automatic hotplug detection stuff
697 */
698 crt->force_hotplug_required = 0;
699 if (HAS_PCH_SPLIT(dev)) {
700 u32 adpa;
701
702 adpa = I915_READ(PCH_ADPA);
703 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
704 adpa |= ADPA_HOTPLUG_BITS;
705 I915_WRITE(PCH_ADPA, adpa);
706 POSTING_READ(PCH_ADPA);
707
708 DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);
709 crt->force_hotplug_required = 1;
710 }
711
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800712 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800713}