blob: 76646c715a42d24e57c819010a898c8056558b21 [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;
Daniel Vetter540a8952012-07-11 16:27:57 +020050 u32 adpa_reg;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +000051};
52
53static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
54{
55 return container_of(intel_attached_encoder(connector),
56 struct intel_crt, base);
57}
58
Daniel Vetter540a8952012-07-11 16:27:57 +020059static struct intel_crt *intel_encoder_to_crt(struct intel_encoder *encoder)
60{
61 return container_of(encoder, struct intel_crt, base);
62}
63
Daniel Vetter21246042012-07-01 14:58:27 +020064static void intel_disable_crt(struct intel_encoder *encoder)
65{
66 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
67 struct intel_crt *crt = intel_encoder_to_crt(encoder);
68 u32 temp;
69
70 temp = I915_READ(crt->adpa_reg);
71 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
72 temp &= ~ADPA_DAC_ENABLE;
73 I915_WRITE(crt->adpa_reg, temp);
74}
75
76static void intel_enable_crt(struct intel_encoder *encoder)
77{
78 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
79 struct intel_crt *crt = intel_encoder_to_crt(encoder);
80 u32 temp;
81
82 temp = I915_READ(crt->adpa_reg);
83 temp |= ADPA_DAC_ENABLE;
84 I915_WRITE(crt->adpa_reg, temp);
85}
86
Daniel Vetterb2cabb02012-07-01 22:42:24 +020087/* Note: The caller is required to filter out dpms modes not supported by the
88 * platform. */
89static void intel_crt_set_dpms(struct intel_encoder *encoder, int mode)
Jesse Barnes79e53942008-11-07 14:24:08 -080090{
Daniel Vetterb2cabb02012-07-01 22:42:24 +020091 struct drm_device *dev = encoder->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080092 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterb2cabb02012-07-01 22:42:24 +020093 struct intel_crt *crt = intel_encoder_to_crt(encoder);
Jesse Barnesdf0323c2012-04-17 15:06:33 -070094 u32 temp;
Jesse Barnes79e53942008-11-07 14:24:08 -080095
Daniel Vetterb2cabb02012-07-01 22:42:24 +020096 temp = I915_READ(crt->adpa_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -080097 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
ling.ma@intel.comfebc7692009-06-25 11:55:57 +080098 temp &= ~ADPA_DAC_ENABLE;
Jesse Barnes79e53942008-11-07 14:24:08 -080099
Akshay Joshi0206e352011-08-16 15:34:10 -0400100 switch (mode) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800101 case DRM_MODE_DPMS_ON:
102 temp |= ADPA_DAC_ENABLE;
103 break;
104 case DRM_MODE_DPMS_STANDBY:
105 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
106 break;
107 case DRM_MODE_DPMS_SUSPEND:
108 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
109 break;
110 case DRM_MODE_DPMS_OFF:
111 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
112 break;
113 }
114
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200115 I915_WRITE(crt->adpa_reg, temp);
116}
117
118static void intel_crt_dpms(struct drm_connector *connector, int mode)
119{
120 struct drm_device *dev = connector->dev;
121 struct intel_encoder *encoder = intel_attached_encoder(connector);
122 struct drm_crtc *crtc;
123 int old_dpms;
124
125 /* PCH platforms and VLV only support on/off. */
126 if (INTEL_INFO(dev)->gen < 5 && mode != DRM_MODE_DPMS_ON)
127 mode = DRM_MODE_DPMS_OFF;
128
129 if (mode == connector->dpms)
130 return;
131
132 old_dpms = connector->dpms;
133 connector->dpms = mode;
134
135 /* Only need to change hw state when actually enabled */
136 crtc = encoder->base.crtc;
137 if (!crtc) {
138 encoder->connectors_active = false;
139 return;
140 }
141
142 /* We need the pipe to run for anything but OFF. */
143 if (mode == DRM_MODE_DPMS_OFF)
144 encoder->connectors_active = false;
145 else
146 encoder->connectors_active = true;
147
148 if (mode < old_dpms) {
149 /* From off to on, enable the pipe first. */
150 intel_crtc_update_dpms(crtc);
151
152 intel_crt_set_dpms(encoder, mode);
153 } else {
154 intel_crt_set_dpms(encoder, mode);
155
156 intel_crtc_update_dpms(crtc);
157 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800158}
159
160static int intel_crt_mode_valid(struct drm_connector *connector,
161 struct drm_display_mode *mode)
162{
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800163 struct drm_device *dev = connector->dev;
164
165 int max_clock = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800166 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
167 return MODE_NO_DBLESCAN;
168
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800169 if (mode->clock < 25000)
170 return MODE_CLOCK_LOW;
171
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100172 if (IS_GEN2(dev))
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800173 max_clock = 350000;
174 else
175 max_clock = 400000;
176 if (mode->clock > max_clock)
177 return MODE_CLOCK_HIGH;
Jesse Barnes79e53942008-11-07 14:24:08 -0800178
179 return MODE_OK;
180}
181
182static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200183 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -0800184 struct drm_display_mode *adjusted_mode)
185{
186 return true;
187}
188
189static void intel_crt_mode_set(struct drm_encoder *encoder,
190 struct drm_display_mode *mode,
191 struct drm_display_mode *adjusted_mode)
192{
193
194 struct drm_device *dev = encoder->dev;
195 struct drm_crtc *crtc = encoder->crtc;
Daniel Vetter540a8952012-07-11 16:27:57 +0200196 struct intel_crt *crt =
197 intel_encoder_to_crt(to_intel_encoder(encoder));
Jesse Barnes79e53942008-11-07 14:24:08 -0800198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
199 struct drm_i915_private *dev_priv = dev->dev_private;
200 int dpll_md_reg;
201 u32 adpa, dpll_md;
202
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800203 dpll_md_reg = DPLL_MD(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -0800204
205 /*
206 * Disable separate mode multiplier used when cloning SDVO to CRT
207 * XXX this needs to be adjusted when we really are cloning
208 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100209 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800210 dpll_md = I915_READ(dpll_md_reg);
211 I915_WRITE(dpll_md_reg,
212 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
213 }
214
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800215 adpa = ADPA_HOTPLUG_BITS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800216 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
217 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
218 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
219 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
220
Jesse Barnes75770562011-10-12 09:01:58 -0700221 /* For CPT allow 3 pipe config, for others just use A or B */
222 if (HAS_PCH_CPT(dev))
223 adpa |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
224 else if (intel_crtc->pipe == 0)
225 adpa |= ADPA_PIPE_A_SELECT;
226 else
227 adpa |= ADPA_PIPE_B_SELECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800228
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800229 if (!HAS_PCH_SPLIT(dev))
230 I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);
231
Daniel Vetter540a8952012-07-11 16:27:57 +0200232 I915_WRITE(crt->adpa_reg, adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800233}
234
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500235static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800236{
237 struct drm_device *dev = connector->dev;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800238 struct intel_crt *crt = intel_attached_crt(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800239 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800240 u32 adpa;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800241 bool ret;
242
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800243 /* The first time through, trigger an explicit detection cycle */
244 if (crt->force_hotplug_required) {
245 bool turn_off_dac = HAS_PCH_SPLIT(dev);
246 u32 save_adpa;
Zhenyu Wang67941da2009-07-24 01:00:33 +0800247
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800248 crt->force_hotplug_required = 0;
Dave Airlied5dd96c2010-08-04 15:52:19 +1000249
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800250 save_adpa = adpa = I915_READ(PCH_ADPA);
251 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
Dave Airlied5dd96c2010-08-04 15:52:19 +1000252
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800253 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
254 if (turn_off_dac)
255 adpa &= ~ADPA_DAC_ENABLE;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800256
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800257 I915_WRITE(PCH_ADPA, adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800258
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800259 if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
260 1000))
261 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
Zhenyu Wang2c072452009-06-05 15:38:42 +0800262
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800263 if (turn_off_dac) {
264 I915_WRITE(PCH_ADPA, save_adpa);
265 POSTING_READ(PCH_ADPA);
266 }
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800267 }
268
Zhenyu Wang2c072452009-06-05 15:38:42 +0800269 /* Check the status to see if both blue and green are on now */
270 adpa = I915_READ(PCH_ADPA);
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800271 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800272 ret = true;
273 else
274 ret = false;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800275 DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800276
Zhenyu Wang2c072452009-06-05 15:38:42 +0800277 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800278}
279
Jesse Barnes7d2c24e2012-06-15 11:55:15 -0700280static bool valleyview_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;
284 u32 adpa;
285 bool ret;
286 u32 save_adpa;
287
288 save_adpa = adpa = I915_READ(ADPA);
289 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
290
291 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
292
293 I915_WRITE(ADPA, adpa);
294
295 if (wait_for((I915_READ(ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
296 1000)) {
297 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
298 I915_WRITE(ADPA, save_adpa);
299 }
300
301 /* Check the status to see if both blue and green are on now */
302 adpa = I915_READ(ADPA);
303 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
304 ret = true;
305 else
306 ret = false;
307
308 DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
309
310 /* FIXME: debug force function and remove */
311 ret = true;
312
313 return ret;
314}
315
Jesse Barnes79e53942008-11-07 14:24:08 -0800316/**
317 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
318 *
319 * Not for i915G/i915GM
320 *
321 * \return true if CRT is connected.
322 * \return false if CRT is disconnected.
323 */
324static bool intel_crt_detect_hotplug(struct drm_connector *connector)
325{
326 struct drm_device *dev = connector->dev;
327 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson7a772c42010-05-24 16:46:29 -0400328 u32 hotplug_en, orig, stat;
329 bool ret = false;
Zhao Yakui771cb082009-03-03 18:07:52 +0800330 int i, tries = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800331
Eric Anholtbad720f2009-10-22 16:11:14 -0700332 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500333 return intel_ironlake_crt_detect_hotplug(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800334
Jesse Barnes7d2c24e2012-06-15 11:55:15 -0700335 if (IS_VALLEYVIEW(dev))
336 return valleyview_crt_detect_hotplug(connector);
337
Zhao Yakui771cb082009-03-03 18:07:52 +0800338 /*
339 * On 4 series desktop, CRT detect sequence need to be done twice
340 * to get a reliable result.
341 */
Jesse Barnes79e53942008-11-07 14:24:08 -0800342
Zhao Yakui771cb082009-03-03 18:07:52 +0800343 if (IS_G4X(dev) && !IS_GM45(dev))
344 tries = 2;
345 else
346 tries = 1;
Adam Jackson7a772c42010-05-24 16:46:29 -0400347 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
Zhao Yakui771cb082009-03-03 18:07:52 +0800348 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800349
Zhao Yakui771cb082009-03-03 18:07:52 +0800350 for (i = 0; i < tries ; i++) {
Zhao Yakui771cb082009-03-03 18:07:52 +0800351 /* turn on the FORCE_DETECT */
352 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Zhao Yakui771cb082009-03-03 18:07:52 +0800353 /* wait for FORCE_DETECT to go off */
Chris Wilson913d8d12010-08-07 11:01:35 +0100354 if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
355 CRT_HOTPLUG_FORCE_DETECT) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100356 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100357 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
Zhao Yakui771cb082009-03-03 18:07:52 +0800358 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800359
Adam Jackson7a772c42010-05-24 16:46:29 -0400360 stat = I915_READ(PORT_HOTPLUG_STAT);
361 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
362 ret = true;
Jesse Barnes79e53942008-11-07 14:24:08 -0800363
Adam Jackson7a772c42010-05-24 16:46:29 -0400364 /* clear the interrupt we just generated, if any */
365 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
366
367 /* and put the bits back */
368 I915_WRITE(PORT_HOTPLUG_EN, orig);
369
370 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800371}
372
David Müllerf5afcd32011-01-06 12:29:32 +0000373static bool intel_crt_detect_ddc(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -0800374{
David Müllerf5afcd32011-01-06 12:29:32 +0000375 struct intel_crt *crt = intel_attached_crt(connector);
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000376 struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200377 struct edid *edid;
378 struct i2c_adapter *i2c;
Jesse Barnes79e53942008-11-07 14:24:08 -0800379
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200380 BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
Jesse Barnes79e53942008-11-07 14:24:08 -0800381
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200382 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
383 edid = drm_get_edid(connector, i2c);
David Müllerf5afcd32011-01-06 12:29:32 +0000384
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200385 if (edid) {
386 bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
387
David Müllerf5afcd32011-01-06 12:29:32 +0000388 /*
389 * This may be a DVI-I connector with a shared DDC
390 * link between analog and digital outputs, so we
391 * have to check the EDID input spec of the attached device.
392 */
David Müllerf5afcd32011-01-06 12:29:32 +0000393 if (!is_digital) {
394 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
395 return true;
396 }
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200397
398 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
399 } else {
400 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100401 }
402
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200403 kfree(edid);
404
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100405 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800406}
407
Ma Linge4a5d542009-05-26 11:31:00 +0800408static enum drm_connector_status
Chris Wilson71731882011-04-19 23:10:58 +0100409intel_crt_load_detect(struct intel_crt *crt)
Ma Linge4a5d542009-05-26 11:31:00 +0800410{
Chris Wilson71731882011-04-19 23:10:58 +0100411 struct drm_device *dev = crt->base.base.dev;
Ma Linge4a5d542009-05-26 11:31:00 +0800412 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson71731882011-04-19 23:10:58 +0100413 uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
Ma Linge4a5d542009-05-26 11:31:00 +0800414 uint32_t save_bclrpat;
415 uint32_t save_vtotal;
416 uint32_t vtotal, vactive;
417 uint32_t vsample;
418 uint32_t vblank, vblank_start, vblank_end;
419 uint32_t dsl;
420 uint32_t bclrpat_reg;
421 uint32_t vtotal_reg;
422 uint32_t vblank_reg;
423 uint32_t vsync_reg;
424 uint32_t pipeconf_reg;
425 uint32_t pipe_dsl_reg;
426 uint8_t st00;
427 enum drm_connector_status status;
428
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100429 DRM_DEBUG_KMS("starting load-detect on CRT\n");
430
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800431 bclrpat_reg = BCLRPAT(pipe);
432 vtotal_reg = VTOTAL(pipe);
433 vblank_reg = VBLANK(pipe);
434 vsync_reg = VSYNC(pipe);
435 pipeconf_reg = PIPECONF(pipe);
436 pipe_dsl_reg = PIPEDSL(pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800437
438 save_bclrpat = I915_READ(bclrpat_reg);
439 save_vtotal = I915_READ(vtotal_reg);
440 vblank = I915_READ(vblank_reg);
441
442 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
443 vactive = (save_vtotal & 0x7ff) + 1;
444
445 vblank_start = (vblank & 0xfff) + 1;
446 vblank_end = ((vblank >> 16) & 0xfff) + 1;
447
448 /* Set the border color to purple. */
449 I915_WRITE(bclrpat_reg, 0x500050);
450
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100451 if (!IS_GEN2(dev)) {
Ma Linge4a5d542009-05-26 11:31:00 +0800452 uint32_t pipeconf = I915_READ(pipeconf_reg);
453 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
Chris Wilson19c55da2010-08-09 14:50:53 +0100454 POSTING_READ(pipeconf_reg);
Ma Linge4a5d542009-05-26 11:31:00 +0800455 /* Wait for next Vblank to substitue
456 * border color for Color info */
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700457 intel_wait_for_vblank(dev, pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800458 st00 = I915_READ8(VGA_MSR_WRITE);
459 status = ((st00 & (1 << 4)) != 0) ?
460 connector_status_connected :
461 connector_status_disconnected;
462
463 I915_WRITE(pipeconf_reg, pipeconf);
464 } else {
465 bool restore_vblank = false;
466 int count, detect;
467
468 /*
469 * If there isn't any border, add some.
470 * Yes, this will flicker
471 */
472 if (vblank_start <= vactive && vblank_end >= vtotal) {
473 uint32_t vsync = I915_READ(vsync_reg);
474 uint32_t vsync_start = (vsync & 0xffff) + 1;
475
476 vblank_start = vsync_start;
477 I915_WRITE(vblank_reg,
478 (vblank_start - 1) |
479 ((vblank_end - 1) << 16));
480 restore_vblank = true;
481 }
482 /* sample in the vertical border, selecting the larger one */
483 if (vblank_start - vactive >= vtotal - vblank_end)
484 vsample = (vblank_start + vactive) >> 1;
485 else
486 vsample = (vtotal + vblank_end) >> 1;
487
488 /*
489 * Wait for the border to be displayed
490 */
491 while (I915_READ(pipe_dsl_reg) >= vactive)
492 ;
493 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
494 ;
495 /*
496 * Watch ST00 for an entire scanline
497 */
498 detect = 0;
499 count = 0;
500 do {
501 count++;
502 /* Read the ST00 VGA status register */
503 st00 = I915_READ8(VGA_MSR_WRITE);
504 if (st00 & (1 << 4))
505 detect++;
506 } while ((I915_READ(pipe_dsl_reg) == dsl));
507
508 /* restore vblank if necessary */
509 if (restore_vblank)
510 I915_WRITE(vblank_reg, vblank);
511 /*
512 * If more than 3/4 of the scanline detected a monitor,
513 * then it is assumed to be present. This works even on i830,
514 * where there isn't any way to force the border color across
515 * the screen
516 */
517 status = detect * 4 > count * 3 ?
518 connector_status_connected :
519 connector_status_disconnected;
520 }
521
522 /* Restore previous settings */
523 I915_WRITE(bclrpat_reg, save_bclrpat);
524
525 return status;
526}
527
Chris Wilson7b334fc2010-09-09 23:51:02 +0100528static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100529intel_crt_detect(struct drm_connector *connector, bool force)
Jesse Barnes79e53942008-11-07 14:24:08 -0800530{
531 struct drm_device *dev = connector->dev;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000532 struct intel_crt *crt = intel_attached_crt(connector);
Ma Linge4a5d542009-05-26 11:31:00 +0800533 enum drm_connector_status status;
Daniel Vettere95c8432012-04-20 21:03:36 +0200534 struct intel_load_detect_pipe tmp;
Jesse Barnes79e53942008-11-07 14:24:08 -0800535
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100536 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetteraaa37732012-06-16 15:30:32 +0200537 /* We can not rely on the HPD pin always being correctly wired
538 * up, for example many KVM do not pass it through, and so
539 * only trust an assertion that the monitor is connected.
540 */
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100541 if (intel_crt_detect_hotplug(connector)) {
542 DRM_DEBUG_KMS("CRT detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800543 return connector_status_connected;
Daniel Vetteraaa37732012-06-16 15:30:32 +0200544 } else
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800545 DRM_DEBUG_KMS("CRT not detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800546 }
547
David Müllerf5afcd32011-01-06 12:29:32 +0000548 if (intel_crt_detect_ddc(connector))
Jesse Barnes79e53942008-11-07 14:24:08 -0800549 return connector_status_connected;
550
Daniel Vetteraaa37732012-06-16 15:30:32 +0200551 /* Load detection is broken on HPD capable machines. Whoever wants a
552 * broken monitor (without edid) to work behind a broken kvm (that fails
553 * to have the right resistors for HP detection) needs to fix this up.
554 * For now just bail out. */
555 if (I915_HAS_HOTPLUG(dev))
556 return connector_status_disconnected;
557
Chris Wilson930a9e22010-09-14 11:07:23 +0100558 if (!force)
Chris Wilson7b334fc2010-09-09 23:51:02 +0100559 return connector->status;
560
Ma Linge4a5d542009-05-26 11:31:00 +0800561 /* for pre-945g platforms use load detect */
Daniel Vetterd2434ab2012-08-12 21:20:10 +0200562 if (intel_get_load_detect_pipe(connector, NULL, &tmp)) {
Daniel Vettere95c8432012-04-20 21:03:36 +0200563 if (intel_crt_detect_ddc(connector))
564 status = connector_status_connected;
565 else
566 status = intel_crt_load_detect(crt);
Daniel Vetterd2434ab2012-08-12 21:20:10 +0200567 intel_release_load_detect_pipe(connector, &tmp);
Daniel Vettere95c8432012-04-20 21:03:36 +0200568 } else
569 status = connector_status_unknown;
Ma Linge4a5d542009-05-26 11:31:00 +0800570
571 return status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800572}
573
574static void intel_crt_destroy(struct drm_connector *connector)
575{
Jesse Barnes79e53942008-11-07 14:24:08 -0800576 drm_sysfs_connector_remove(connector);
577 drm_connector_cleanup(connector);
578 kfree(connector);
579}
580
581static int intel_crt_get_modes(struct drm_connector *connector)
582{
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800583 struct drm_device *dev = connector->dev;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700584 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson890f3352010-09-14 16:46:59 +0100585 int ret;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800586 struct i2c_adapter *i2c;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800587
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800588 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
589 ret = intel_ddc_get_modes(connector, i2c);
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800590 if (ret || !IS_G4X(dev))
Chris Wilsonf899fc62010-07-20 15:44:45 -0700591 return ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800592
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800593 /* Try to probe digital port for output in DVI-I -> VGA mode. */
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800594 i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
595 return intel_ddc_get_modes(connector, i2c);
Jesse Barnes79e53942008-11-07 14:24:08 -0800596}
597
598static int intel_crt_set_property(struct drm_connector *connector,
599 struct drm_property *property,
600 uint64_t value)
601{
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 return 0;
603}
604
Chris Wilsonf3269052011-01-24 15:17:08 +0000605static void intel_crt_reset(struct drm_connector *connector)
606{
607 struct drm_device *dev = connector->dev;
608 struct intel_crt *crt = intel_attached_crt(connector);
609
610 if (HAS_PCH_SPLIT(dev))
611 crt->force_hotplug_required = 1;
612}
613
Jesse Barnes79e53942008-11-07 14:24:08 -0800614/*
615 * Routines for controlling stuff on the analog port
616 */
617
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200618static const struct drm_encoder_helper_funcs crt_encoder_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800619 .mode_fixup = intel_crt_mode_fixup,
Daniel Vetter21246042012-07-01 14:58:27 +0200620 .prepare = intel_encoder_noop,
621 .commit = intel_encoder_noop,
Jesse Barnes79e53942008-11-07 14:24:08 -0800622 .mode_set = intel_crt_mode_set,
Daniel Vetter21246042012-07-01 14:58:27 +0200623 .disable = intel_encoder_disable,
Jesse Barnes79e53942008-11-07 14:24:08 -0800624};
625
626static const struct drm_connector_funcs intel_crt_connector_funcs = {
Chris Wilsonf3269052011-01-24 15:17:08 +0000627 .reset = intel_crt_reset,
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200628 .dpms = intel_crt_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 .detect = intel_crt_detect,
630 .fill_modes = drm_helper_probe_single_connector_modes,
631 .destroy = intel_crt_destroy,
632 .set_property = intel_crt_set_property,
633};
634
635static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
636 .mode_valid = intel_crt_mode_valid,
637 .get_modes = intel_crt_get_modes,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100638 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800639};
640
Jesse Barnes79e53942008-11-07 14:24:08 -0800641static const struct drm_encoder_funcs intel_crt_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100642 .destroy = intel_encoder_destroy,
Jesse Barnes79e53942008-11-07 14:24:08 -0800643};
644
Duncan Laurie8ca40132011-10-25 15:42:21 -0700645static int __init intel_no_crt_dmi_callback(const struct dmi_system_id *id)
646{
Daniel Vetterbc0daf42012-04-01 13:16:49 +0200647 DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
Duncan Laurie8ca40132011-10-25 15:42:21 -0700648 return 1;
649}
650
651static const struct dmi_system_id intel_no_crt[] = {
652 {
653 .callback = intel_no_crt_dmi_callback,
654 .ident = "ACER ZGB",
655 .matches = {
656 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
657 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
658 },
659 },
660 { }
661};
662
Jesse Barnes79e53942008-11-07 14:24:08 -0800663void intel_crt_init(struct drm_device *dev)
664{
665 struct drm_connector *connector;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000666 struct intel_crt *crt;
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800667 struct intel_connector *intel_connector;
David Müller (ELSOFT AG)db545012009-08-29 08:54:45 +0200668 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800669
Duncan Laurie8ca40132011-10-25 15:42:21 -0700670 /* Skip machines without VGA that falsely report hotplug events */
671 if (dmi_check_system(intel_no_crt))
672 return;
673
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000674 crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
675 if (!crt)
Jesse Barnes79e53942008-11-07 14:24:08 -0800676 return;
677
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800678 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
679 if (!intel_connector) {
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000680 kfree(crt);
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800681 return;
682 }
683
684 connector = &intel_connector->base;
685 drm_connector_init(dev, &intel_connector->base,
Jesse Barnes79e53942008-11-07 14:24:08 -0800686 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
687
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000688 drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800689 DRM_MODE_ENCODER_DAC);
690
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000691 intel_connector_attach_encoder(intel_connector, &crt->base);
Jesse Barnes79e53942008-11-07 14:24:08 -0800692
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000693 crt->base.type = INTEL_OUTPUT_ANALOG;
Daniel Vetter66a92782012-07-12 20:08:18 +0200694 crt->base.cloneable = true;
Eugeni Dodonov59c859d2012-05-09 15:37:19 -0300695 if (IS_HASWELL(dev))
696 crt->base.crtc_mask = (1 << 0);
697 else
Keith Packard08268742012-08-13 21:34:45 -0700698 crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -0300699
Daniel Vetterdbb02572012-01-28 14:49:23 +0100700 if (IS_GEN2(dev))
701 connector->interlace_allowed = 0;
702 else
703 connector->interlace_allowed = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 connector->doublescan_allowed = 0;
705
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700706 if (HAS_PCH_SPLIT(dev))
Daniel Vetter540a8952012-07-11 16:27:57 +0200707 crt->adpa_reg = PCH_ADPA;
708 else if (IS_VALLEYVIEW(dev))
709 crt->adpa_reg = VLV_ADPA;
710 else
711 crt->adpa_reg = ADPA;
712
Daniel Vetter21246042012-07-01 14:58:27 +0200713 crt->base.disable = intel_disable_crt;
714 crt->base.enable = intel_enable_crt;
715
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200716 drm_encoder_helper_add(&crt->base.base, &crt_encoder_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -0800717 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
718
719 drm_sysfs_connector_add(connector);
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800720
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000721 if (I915_HAS_HOTPLUG(dev))
722 connector->polled = DRM_CONNECTOR_POLL_HPD;
723 else
724 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
725
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800726 /*
727 * Configure the automatic hotplug detection stuff
728 */
729 crt->force_hotplug_required = 0;
730 if (HAS_PCH_SPLIT(dev)) {
731 u32 adpa;
732
733 adpa = I915_READ(PCH_ADPA);
734 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
735 adpa |= ADPA_HOTPLUG_BITS;
736 I915_WRITE(PCH_ADPA, adpa);
737 POSTING_READ(PCH_ADPA);
738
739 DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);
740 crt->force_hotplug_required = 1;
741 }
742
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800743 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800744}