blob: c8f1c0db446d1c9e9e9acfe743907f76cd947309 [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>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/drmP.h>
31#include <drm/drm_crtc.h>
32#include <drm/drm_crtc_helper.h>
33#include <drm/drm_edid.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "i915_drv.h"
37
Keith Packarde7dbb2f2010-11-16 16:03:53 +080038/* Here's the desired hotplug mode */
39#define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 | \
40 ADPA_CRT_HOTPLUG_WARMUP_10MS | \
41 ADPA_CRT_HOTPLUG_SAMPLE_4S | \
42 ADPA_CRT_HOTPLUG_VOLTAGE_50 | \
43 ADPA_CRT_HOTPLUG_VOLREF_325MV | \
44 ADPA_CRT_HOTPLUG_ENABLE)
45
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +000046struct intel_crt {
47 struct intel_encoder base;
Keith Packarde7dbb2f2010-11-16 16:03:53 +080048 bool force_hotplug_required;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +000049};
50
51static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
52{
53 return container_of(intel_attached_encoder(connector),
54 struct intel_crt, base);
55}
56
Jesse Barnesdf0323c2012-04-17 15:06:33 -070057static void pch_crt_dpms(struct drm_encoder *encoder, int mode)
Jesse Barnes79e53942008-11-07 14:24:08 -080058{
59 struct drm_device *dev = encoder->dev;
60 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdf0323c2012-04-17 15:06:33 -070061 u32 temp;
Jesse Barnes79e53942008-11-07 14:24:08 -080062
Jesse Barnesdf0323c2012-04-17 15:06:33 -070063 temp = I915_READ(PCH_ADPA);
64 temp &= ~ADPA_DAC_ENABLE;
Zhenyu Wang2c072452009-06-05 15:38:42 +080065
Jesse Barnesdf0323c2012-04-17 15:06:33 -070066 switch (mode) {
67 case DRM_MODE_DPMS_ON:
68 temp |= ADPA_DAC_ENABLE;
69 break;
70 case DRM_MODE_DPMS_STANDBY:
71 case DRM_MODE_DPMS_SUSPEND:
72 case DRM_MODE_DPMS_OFF:
73 /* Just leave port enable cleared */
74 break;
75 }
76
77 I915_WRITE(PCH_ADPA, temp);
78}
79
80static void gmch_crt_dpms(struct drm_encoder *encoder, int mode)
81{
82 struct drm_device *dev = encoder->dev;
83 struct drm_i915_private *dev_priv = dev->dev_private;
84 u32 temp;
85
86 temp = I915_READ(ADPA);
Jesse Barnes79e53942008-11-07 14:24:08 -080087 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
ling.ma@intel.comfebc7692009-06-25 11:55:57 +080088 temp &= ~ADPA_DAC_ENABLE;
Jesse Barnes79e53942008-11-07 14:24:08 -080089
Jesse Barnesbd9e8412012-06-15 11:55:18 -070090 if (IS_VALLEYVIEW(dev) && mode != DRM_MODE_DPMS_ON)
91 mode = DRM_MODE_DPMS_OFF;
92
Akshay Joshi0206e352011-08-16 15:34:10 -040093 switch (mode) {
Jesse Barnes79e53942008-11-07 14:24:08 -080094 case DRM_MODE_DPMS_ON:
95 temp |= ADPA_DAC_ENABLE;
96 break;
97 case DRM_MODE_DPMS_STANDBY:
98 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
99 break;
100 case DRM_MODE_DPMS_SUSPEND:
101 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
102 break;
103 case DRM_MODE_DPMS_OFF:
104 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
105 break;
106 }
107
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700108 I915_WRITE(ADPA, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -0800109}
110
111static int intel_crt_mode_valid(struct drm_connector *connector,
112 struct drm_display_mode *mode)
113{
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800114 struct drm_device *dev = connector->dev;
115
116 int max_clock = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800117 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
118 return MODE_NO_DBLESCAN;
119
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800120 if (mode->clock < 25000)
121 return MODE_CLOCK_LOW;
122
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100123 if (IS_GEN2(dev))
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800124 max_clock = 350000;
125 else
126 max_clock = 400000;
127 if (mode->clock > max_clock)
128 return MODE_CLOCK_HIGH;
Jesse Barnes79e53942008-11-07 14:24:08 -0800129
130 return MODE_OK;
131}
132
133static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200134 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -0800135 struct drm_display_mode *adjusted_mode)
136{
137 return true;
138}
139
140static void intel_crt_mode_set(struct drm_encoder *encoder,
141 struct drm_display_mode *mode,
142 struct drm_display_mode *adjusted_mode)
143{
144
145 struct drm_device *dev = encoder->dev;
146 struct drm_crtc *crtc = encoder->crtc;
147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 int dpll_md_reg;
150 u32 adpa, dpll_md;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800151 u32 adpa_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -0800152
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800153 dpll_md_reg = DPLL_MD(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -0800154
Eric Anholtbad720f2009-10-22 16:11:14 -0700155 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800156 adpa_reg = PCH_ADPA;
157 else
158 adpa_reg = ADPA;
159
Jesse Barnes79e53942008-11-07 14:24:08 -0800160 /*
161 * Disable separate mode multiplier used when cloning SDVO to CRT
162 * XXX this needs to be adjusted when we really are cloning
163 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100164 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800165 dpll_md = I915_READ(dpll_md_reg);
166 I915_WRITE(dpll_md_reg,
167 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
168 }
169
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800170 adpa = ADPA_HOTPLUG_BITS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800171 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
172 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
173 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
174 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
175
Jesse Barnes75770562011-10-12 09:01:58 -0700176 /* For CPT allow 3 pipe config, for others just use A or B */
177 if (HAS_PCH_CPT(dev))
178 adpa |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
179 else if (intel_crtc->pipe == 0)
180 adpa |= ADPA_PIPE_A_SELECT;
181 else
182 adpa |= ADPA_PIPE_B_SELECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800183
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800184 if (!HAS_PCH_SPLIT(dev))
185 I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);
186
Zhenyu Wang2c072452009-06-05 15:38:42 +0800187 I915_WRITE(adpa_reg, adpa);
188}
189
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500190static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800191{
192 struct drm_device *dev = connector->dev;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800193 struct intel_crt *crt = intel_attached_crt(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800194 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800195 u32 adpa;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800196 bool ret;
197
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800198 /* The first time through, trigger an explicit detection cycle */
199 if (crt->force_hotplug_required) {
200 bool turn_off_dac = HAS_PCH_SPLIT(dev);
201 u32 save_adpa;
Zhenyu Wang67941da2009-07-24 01:00:33 +0800202
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800203 crt->force_hotplug_required = 0;
Dave Airlied5dd96c2010-08-04 15:52:19 +1000204
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800205 save_adpa = adpa = I915_READ(PCH_ADPA);
206 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
Dave Airlied5dd96c2010-08-04 15:52:19 +1000207
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800208 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
209 if (turn_off_dac)
210 adpa &= ~ADPA_DAC_ENABLE;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800211
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800212 I915_WRITE(PCH_ADPA, adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800213
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800214 if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
215 1000))
216 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
Zhenyu Wang2c072452009-06-05 15:38:42 +0800217
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800218 if (turn_off_dac) {
219 I915_WRITE(PCH_ADPA, save_adpa);
220 POSTING_READ(PCH_ADPA);
221 }
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800222 }
223
Zhenyu Wang2c072452009-06-05 15:38:42 +0800224 /* Check the status to see if both blue and green are on now */
225 adpa = I915_READ(PCH_ADPA);
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800226 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800227 ret = true;
228 else
229 ret = false;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800230 DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800231
Zhenyu Wang2c072452009-06-05 15:38:42 +0800232 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800233}
234
Jesse Barnes7d2c24e2012-06-15 11:55:15 -0700235static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
236{
237 struct drm_device *dev = connector->dev;
238 struct drm_i915_private *dev_priv = dev->dev_private;
239 u32 adpa;
240 bool ret;
241 u32 save_adpa;
242
243 save_adpa = adpa = I915_READ(ADPA);
244 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
245
246 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
247
248 I915_WRITE(ADPA, adpa);
249
250 if (wait_for((I915_READ(ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
251 1000)) {
252 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
253 I915_WRITE(ADPA, save_adpa);
254 }
255
256 /* Check the status to see if both blue and green are on now */
257 adpa = I915_READ(ADPA);
258 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
259 ret = true;
260 else
261 ret = false;
262
263 DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
264
265 /* FIXME: debug force function and remove */
266 ret = true;
267
268 return ret;
269}
270
Jesse Barnes79e53942008-11-07 14:24:08 -0800271/**
272 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
273 *
274 * Not for i915G/i915GM
275 *
276 * \return true if CRT is connected.
277 * \return false if CRT is disconnected.
278 */
279static bool intel_crt_detect_hotplug(struct drm_connector *connector)
280{
281 struct drm_device *dev = connector->dev;
282 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson7a772c42010-05-24 16:46:29 -0400283 u32 hotplug_en, orig, stat;
284 bool ret = false;
Zhao Yakui771cb082009-03-03 18:07:52 +0800285 int i, tries = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800286
Eric Anholtbad720f2009-10-22 16:11:14 -0700287 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500288 return intel_ironlake_crt_detect_hotplug(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800289
Jesse Barnes7d2c24e2012-06-15 11:55:15 -0700290 if (IS_VALLEYVIEW(dev))
291 return valleyview_crt_detect_hotplug(connector);
292
Zhao Yakui771cb082009-03-03 18:07:52 +0800293 /*
294 * On 4 series desktop, CRT detect sequence need to be done twice
295 * to get a reliable result.
296 */
Jesse Barnes79e53942008-11-07 14:24:08 -0800297
Zhao Yakui771cb082009-03-03 18:07:52 +0800298 if (IS_G4X(dev) && !IS_GM45(dev))
299 tries = 2;
300 else
301 tries = 1;
Adam Jackson7a772c42010-05-24 16:46:29 -0400302 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
Zhao Yakui771cb082009-03-03 18:07:52 +0800303 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800304
Zhao Yakui771cb082009-03-03 18:07:52 +0800305 for (i = 0; i < tries ; i++) {
Zhao Yakui771cb082009-03-03 18:07:52 +0800306 /* turn on the FORCE_DETECT */
307 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Zhao Yakui771cb082009-03-03 18:07:52 +0800308 /* wait for FORCE_DETECT to go off */
Chris Wilson913d8d12010-08-07 11:01:35 +0100309 if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
310 CRT_HOTPLUG_FORCE_DETECT) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100311 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100312 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
Zhao Yakui771cb082009-03-03 18:07:52 +0800313 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800314
Adam Jackson7a772c42010-05-24 16:46:29 -0400315 stat = I915_READ(PORT_HOTPLUG_STAT);
316 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
317 ret = true;
Jesse Barnes79e53942008-11-07 14:24:08 -0800318
Adam Jackson7a772c42010-05-24 16:46:29 -0400319 /* clear the interrupt we just generated, if any */
320 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
321
322 /* and put the bits back */
323 I915_WRITE(PORT_HOTPLUG_EN, orig);
324
325 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800326}
327
Jani Nikulaf1a2f5b2012-08-13 13:22:35 +0300328static struct edid *intel_crt_get_edid(struct drm_connector *connector,
329 struct i2c_adapter *i2c)
330{
331 struct edid *edid;
332
333 edid = drm_get_edid(connector, i2c);
334
335 if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
336 DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
337 intel_gmbus_force_bit(i2c, true);
338 edid = drm_get_edid(connector, i2c);
339 intel_gmbus_force_bit(i2c, false);
340 }
341
342 return edid;
343}
344
345/* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
346static int intel_crt_ddc_get_modes(struct drm_connector *connector,
347 struct i2c_adapter *adapter)
348{
349 struct edid *edid;
350
351 edid = intel_crt_get_edid(connector, adapter);
352 if (!edid)
353 return 0;
354
355 return intel_connector_update_modes(connector, edid);
356}
357
David Müllerf5afcd32011-01-06 12:29:32 +0000358static bool intel_crt_detect_ddc(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -0800359{
David Müllerf5afcd32011-01-06 12:29:32 +0000360 struct intel_crt *crt = intel_attached_crt(connector);
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000361 struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200362 struct edid *edid;
363 struct i2c_adapter *i2c;
Jesse Barnes79e53942008-11-07 14:24:08 -0800364
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200365 BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
Jesse Barnes79e53942008-11-07 14:24:08 -0800366
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200367 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
Jani Nikulaf1a2f5b2012-08-13 13:22:35 +0300368 edid = intel_crt_get_edid(connector, i2c);
David Müllerf5afcd32011-01-06 12:29:32 +0000369
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200370 if (edid) {
371 bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
372
David Müllerf5afcd32011-01-06 12:29:32 +0000373 /*
374 * This may be a DVI-I connector with a shared DDC
375 * link between analog and digital outputs, so we
376 * have to check the EDID input spec of the attached device.
377 */
David Müllerf5afcd32011-01-06 12:29:32 +0000378 if (!is_digital) {
379 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
380 return true;
381 }
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200382
383 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
384 } else {
385 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100386 }
387
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200388 kfree(edid);
389
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100390 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800391}
392
Ma Linge4a5d542009-05-26 11:31:00 +0800393static enum drm_connector_status
Chris Wilson71731882011-04-19 23:10:58 +0100394intel_crt_load_detect(struct intel_crt *crt)
Ma Linge4a5d542009-05-26 11:31:00 +0800395{
Chris Wilson71731882011-04-19 23:10:58 +0100396 struct drm_device *dev = crt->base.base.dev;
Ma Linge4a5d542009-05-26 11:31:00 +0800397 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson71731882011-04-19 23:10:58 +0100398 uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
Ma Linge4a5d542009-05-26 11:31:00 +0800399 uint32_t save_bclrpat;
400 uint32_t save_vtotal;
401 uint32_t vtotal, vactive;
402 uint32_t vsample;
403 uint32_t vblank, vblank_start, vblank_end;
404 uint32_t dsl;
405 uint32_t bclrpat_reg;
406 uint32_t vtotal_reg;
407 uint32_t vblank_reg;
408 uint32_t vsync_reg;
409 uint32_t pipeconf_reg;
410 uint32_t pipe_dsl_reg;
411 uint8_t st00;
412 enum drm_connector_status status;
413
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100414 DRM_DEBUG_KMS("starting load-detect on CRT\n");
415
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800416 bclrpat_reg = BCLRPAT(pipe);
417 vtotal_reg = VTOTAL(pipe);
418 vblank_reg = VBLANK(pipe);
419 vsync_reg = VSYNC(pipe);
420 pipeconf_reg = PIPECONF(pipe);
421 pipe_dsl_reg = PIPEDSL(pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800422
423 save_bclrpat = I915_READ(bclrpat_reg);
424 save_vtotal = I915_READ(vtotal_reg);
425 vblank = I915_READ(vblank_reg);
426
427 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
428 vactive = (save_vtotal & 0x7ff) + 1;
429
430 vblank_start = (vblank & 0xfff) + 1;
431 vblank_end = ((vblank >> 16) & 0xfff) + 1;
432
433 /* Set the border color to purple. */
434 I915_WRITE(bclrpat_reg, 0x500050);
435
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100436 if (!IS_GEN2(dev)) {
Ma Linge4a5d542009-05-26 11:31:00 +0800437 uint32_t pipeconf = I915_READ(pipeconf_reg);
438 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
Chris Wilson19c55da2010-08-09 14:50:53 +0100439 POSTING_READ(pipeconf_reg);
Ma Linge4a5d542009-05-26 11:31:00 +0800440 /* Wait for next Vblank to substitue
441 * border color for Color info */
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700442 intel_wait_for_vblank(dev, pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800443 st00 = I915_READ8(VGA_MSR_WRITE);
444 status = ((st00 & (1 << 4)) != 0) ?
445 connector_status_connected :
446 connector_status_disconnected;
447
448 I915_WRITE(pipeconf_reg, pipeconf);
449 } else {
450 bool restore_vblank = false;
451 int count, detect;
452
453 /*
454 * If there isn't any border, add some.
455 * Yes, this will flicker
456 */
457 if (vblank_start <= vactive && vblank_end >= vtotal) {
458 uint32_t vsync = I915_READ(vsync_reg);
459 uint32_t vsync_start = (vsync & 0xffff) + 1;
460
461 vblank_start = vsync_start;
462 I915_WRITE(vblank_reg,
463 (vblank_start - 1) |
464 ((vblank_end - 1) << 16));
465 restore_vblank = true;
466 }
467 /* sample in the vertical border, selecting the larger one */
468 if (vblank_start - vactive >= vtotal - vblank_end)
469 vsample = (vblank_start + vactive) >> 1;
470 else
471 vsample = (vtotal + vblank_end) >> 1;
472
473 /*
474 * Wait for the border to be displayed
475 */
476 while (I915_READ(pipe_dsl_reg) >= vactive)
477 ;
478 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
479 ;
480 /*
481 * Watch ST00 for an entire scanline
482 */
483 detect = 0;
484 count = 0;
485 do {
486 count++;
487 /* Read the ST00 VGA status register */
488 st00 = I915_READ8(VGA_MSR_WRITE);
489 if (st00 & (1 << 4))
490 detect++;
491 } while ((I915_READ(pipe_dsl_reg) == dsl));
492
493 /* restore vblank if necessary */
494 if (restore_vblank)
495 I915_WRITE(vblank_reg, vblank);
496 /*
497 * If more than 3/4 of the scanline detected a monitor,
498 * then it is assumed to be present. This works even on i830,
499 * where there isn't any way to force the border color across
500 * the screen
501 */
502 status = detect * 4 > count * 3 ?
503 connector_status_connected :
504 connector_status_disconnected;
505 }
506
507 /* Restore previous settings */
508 I915_WRITE(bclrpat_reg, save_bclrpat);
509
510 return status;
511}
512
Chris Wilson7b334fc2010-09-09 23:51:02 +0100513static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100514intel_crt_detect(struct drm_connector *connector, bool force)
Jesse Barnes79e53942008-11-07 14:24:08 -0800515{
516 struct drm_device *dev = connector->dev;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000517 struct intel_crt *crt = intel_attached_crt(connector);
Ma Linge4a5d542009-05-26 11:31:00 +0800518 enum drm_connector_status status;
Daniel Vettere95c8432012-04-20 21:03:36 +0200519 struct intel_load_detect_pipe tmp;
Jesse Barnes79e53942008-11-07 14:24:08 -0800520
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100521 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetteraaa37732012-06-16 15:30:32 +0200522 /* We can not rely on the HPD pin always being correctly wired
523 * up, for example many KVM do not pass it through, and so
524 * only trust an assertion that the monitor is connected.
525 */
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100526 if (intel_crt_detect_hotplug(connector)) {
527 DRM_DEBUG_KMS("CRT detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800528 return connector_status_connected;
Daniel Vetteraaa37732012-06-16 15:30:32 +0200529 } else
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800530 DRM_DEBUG_KMS("CRT not detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800531 }
532
David Müllerf5afcd32011-01-06 12:29:32 +0000533 if (intel_crt_detect_ddc(connector))
Jesse Barnes79e53942008-11-07 14:24:08 -0800534 return connector_status_connected;
535
Daniel Vetteraaa37732012-06-16 15:30:32 +0200536 /* Load detection is broken on HPD capable machines. Whoever wants a
537 * broken monitor (without edid) to work behind a broken kvm (that fails
538 * to have the right resistors for HP detection) needs to fix this up.
539 * For now just bail out. */
540 if (I915_HAS_HOTPLUG(dev))
541 return connector_status_disconnected;
542
Chris Wilson930a9e22010-09-14 11:07:23 +0100543 if (!force)
Chris Wilson7b334fc2010-09-09 23:51:02 +0100544 return connector->status;
545
Ma Linge4a5d542009-05-26 11:31:00 +0800546 /* for pre-945g platforms use load detect */
Daniel Vettere95c8432012-04-20 21:03:36 +0200547 if (intel_get_load_detect_pipe(&crt->base, connector, NULL,
548 &tmp)) {
549 if (intel_crt_detect_ddc(connector))
550 status = connector_status_connected;
551 else
552 status = intel_crt_load_detect(crt);
553 intel_release_load_detect_pipe(&crt->base, connector,
554 &tmp);
555 } else
556 status = connector_status_unknown;
Ma Linge4a5d542009-05-26 11:31:00 +0800557
558 return status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800559}
560
561static void intel_crt_destroy(struct drm_connector *connector)
562{
Jesse Barnes79e53942008-11-07 14:24:08 -0800563 drm_sysfs_connector_remove(connector);
564 drm_connector_cleanup(connector);
565 kfree(connector);
566}
567
568static int intel_crt_get_modes(struct drm_connector *connector)
569{
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800570 struct drm_device *dev = connector->dev;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700571 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson890f3352010-09-14 16:46:59 +0100572 int ret;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800573 struct i2c_adapter *i2c;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800574
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800575 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
Jani Nikulaf1a2f5b2012-08-13 13:22:35 +0300576 ret = intel_crt_ddc_get_modes(connector, i2c);
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800577 if (ret || !IS_G4X(dev))
Chris Wilsonf899fc62010-07-20 15:44:45 -0700578 return ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800579
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800580 /* Try to probe digital port for output in DVI-I -> VGA mode. */
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800581 i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
Jani Nikulaf1a2f5b2012-08-13 13:22:35 +0300582 return intel_crt_ddc_get_modes(connector, i2c);
Jesse Barnes79e53942008-11-07 14:24:08 -0800583}
584
585static int intel_crt_set_property(struct drm_connector *connector,
586 struct drm_property *property,
587 uint64_t value)
588{
Jesse Barnes79e53942008-11-07 14:24:08 -0800589 return 0;
590}
591
Chris Wilsonf3269052011-01-24 15:17:08 +0000592static void intel_crt_reset(struct drm_connector *connector)
593{
594 struct drm_device *dev = connector->dev;
595 struct intel_crt *crt = intel_attached_crt(connector);
596
597 if (HAS_PCH_SPLIT(dev))
598 crt->force_hotplug_required = 1;
599}
600
Jesse Barnes79e53942008-11-07 14:24:08 -0800601/*
602 * Routines for controlling stuff on the analog port
603 */
604
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700605static const struct drm_encoder_helper_funcs pch_encoder_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 .mode_fixup = intel_crt_mode_fixup,
607 .prepare = intel_encoder_prepare,
608 .commit = intel_encoder_commit,
609 .mode_set = intel_crt_mode_set,
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700610 .dpms = pch_crt_dpms,
611};
612
613static const struct drm_encoder_helper_funcs gmch_encoder_funcs = {
614 .mode_fixup = intel_crt_mode_fixup,
615 .prepare = intel_encoder_prepare,
616 .commit = intel_encoder_commit,
617 .mode_set = intel_crt_mode_set,
618 .dpms = gmch_crt_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800619};
620
621static const struct drm_connector_funcs intel_crt_connector_funcs = {
Chris Wilsonf3269052011-01-24 15:17:08 +0000622 .reset = intel_crt_reset,
Keith Packardc9fb15f2009-05-30 20:42:28 -0700623 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800624 .detect = intel_crt_detect,
625 .fill_modes = drm_helper_probe_single_connector_modes,
626 .destroy = intel_crt_destroy,
627 .set_property = intel_crt_set_property,
628};
629
630static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
631 .mode_valid = intel_crt_mode_valid,
632 .get_modes = intel_crt_get_modes,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100633 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800634};
635
Jesse Barnes79e53942008-11-07 14:24:08 -0800636static const struct drm_encoder_funcs intel_crt_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100637 .destroy = intel_encoder_destroy,
Jesse Barnes79e53942008-11-07 14:24:08 -0800638};
639
Duncan Laurie8ca40132011-10-25 15:42:21 -0700640static int __init intel_no_crt_dmi_callback(const struct dmi_system_id *id)
641{
Daniel Vetterbc0daf42012-04-01 13:16:49 +0200642 DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
Duncan Laurie8ca40132011-10-25 15:42:21 -0700643 return 1;
644}
645
646static const struct dmi_system_id intel_no_crt[] = {
647 {
648 .callback = intel_no_crt_dmi_callback,
649 .ident = "ACER ZGB",
650 .matches = {
651 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
652 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
653 },
654 },
655 { }
656};
657
Jesse Barnes79e53942008-11-07 14:24:08 -0800658void intel_crt_init(struct drm_device *dev)
659{
660 struct drm_connector *connector;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000661 struct intel_crt *crt;
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800662 struct intel_connector *intel_connector;
David Müller (ELSOFT AG)db545012009-08-29 08:54:45 +0200663 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700664 const struct drm_encoder_helper_funcs *encoder_helper_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -0800665
Duncan Laurie8ca40132011-10-25 15:42:21 -0700666 /* Skip machines without VGA that falsely report hotplug events */
667 if (dmi_check_system(intel_no_crt))
668 return;
669
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000670 crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
671 if (!crt)
Jesse Barnes79e53942008-11-07 14:24:08 -0800672 return;
673
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800674 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
675 if (!intel_connector) {
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000676 kfree(crt);
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800677 return;
678 }
679
680 connector = &intel_connector->base;
681 drm_connector_init(dev, &intel_connector->base,
Jesse Barnes79e53942008-11-07 14:24:08 -0800682 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
683
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000684 drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800685 DRM_MODE_ENCODER_DAC);
686
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000687 intel_connector_attach_encoder(intel_connector, &crt->base);
Jesse Barnes79e53942008-11-07 14:24:08 -0800688
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000689 crt->base.type = INTEL_OUTPUT_ANALOG;
690 crt->base.clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT |
691 1 << INTEL_ANALOG_CLONE_BIT |
692 1 << INTEL_SDVO_LVDS_CLONE_BIT);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -0300693 if (IS_HASWELL(dev))
694 crt->base.crtc_mask = (1 << 0);
695 else
696 crt->base.crtc_mask = (1 << 0) | (1 << 1);
697
Daniel Vetterdbb02572012-01-28 14:49:23 +0100698 if (IS_GEN2(dev))
699 connector->interlace_allowed = 0;
700 else
701 connector->interlace_allowed = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 connector->doublescan_allowed = 0;
703
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700704 if (HAS_PCH_SPLIT(dev))
705 encoder_helper_funcs = &pch_encoder_funcs;
706 else
707 encoder_helper_funcs = &gmch_encoder_funcs;
708
709 drm_encoder_helper_add(&crt->base.base, encoder_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -0800710 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
711
712 drm_sysfs_connector_add(connector);
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800713
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000714 if (I915_HAS_HOTPLUG(dev))
715 connector->polled = DRM_CONNECTOR_POLL_HPD;
716 else
717 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
718
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800719 /*
720 * Configure the automatic hotplug detection stuff
721 */
722 crt->force_hotplug_required = 0;
723 if (HAS_PCH_SPLIT(dev)) {
724 u32 adpa;
725
726 adpa = I915_READ(PCH_ADPA);
727 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
728 adpa |= ADPA_HOTPLUG_BITS;
729 I915_WRITE(PCH_ADPA, adpa);
730 POSTING_READ(PCH_ADPA);
731
732 DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);
733 crt->force_hotplug_required = 1;
734 }
735
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800736 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800737}