blob: ae3a3d545ef2e9288f25e77556b1d4a016187c17 [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 Vettere403fc92012-07-02 13:41:21 +020064static bool intel_crt_get_hw_state(struct intel_encoder *encoder,
65 enum pipe *pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080066{
Daniel Vettere403fc92012-07-02 13:41:21 +020067 struct drm_device *dev = encoder->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080068 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere403fc92012-07-02 13:41:21 +020069 struct intel_crt *crt = intel_encoder_to_crt(encoder);
70 u32 tmp;
Jesse Barnes79e53942008-11-07 14:24:08 -080071
Daniel Vettere403fc92012-07-02 13:41:21 +020072 tmp = I915_READ(crt->adpa_reg);
Zhenyu Wang2c072452009-06-05 15:38:42 +080073
Daniel Vettere403fc92012-07-02 13:41:21 +020074 if (!(tmp & ADPA_DAC_ENABLE))
75 return false;
Jesse Barnesdf0323c2012-04-17 15:06:33 -070076
Daniel Vettere403fc92012-07-02 13:41:21 +020077 if (HAS_PCH_CPT(dev))
78 *pipe = PORT_TO_PIPE_CPT(tmp);
79 else
80 *pipe = PORT_TO_PIPE(tmp);
81
82 return true;
Jesse Barnesdf0323c2012-04-17 15:06:33 -070083}
84
Daniel Vetter21246042012-07-01 14:58:27 +020085static void intel_disable_crt(struct intel_encoder *encoder)
Jesse Barnesdf0323c2012-04-17 15:06:33 -070086{
Daniel Vetter21246042012-07-01 14:58:27 +020087 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
88 struct intel_crt *crt = intel_encoder_to_crt(encoder);
Jesse Barnesdf0323c2012-04-17 15:06:33 -070089 u32 temp;
90
Daniel Vetter21246042012-07-01 14:58:27 +020091 temp = I915_READ(crt->adpa_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -080092 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
ling.ma@intel.comfebc7692009-06-25 11:55:57 +080093 temp &= ~ADPA_DAC_ENABLE;
Daniel Vetter21246042012-07-01 14:58:27 +020094 I915_WRITE(crt->adpa_reg, temp);
95}
Jesse Barnes79e53942008-11-07 14:24:08 -080096
Daniel Vetter21246042012-07-01 14:58:27 +020097static void intel_enable_crt(struct intel_encoder *encoder)
98{
99 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
100 struct intel_crt *crt = intel_encoder_to_crt(encoder);
101 u32 temp;
102
103 temp = I915_READ(crt->adpa_reg);
104 temp |= ADPA_DAC_ENABLE;
105 I915_WRITE(crt->adpa_reg, temp);
106}
107
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200108/* Note: The caller is required to filter out dpms modes not supported by the
109 * platform. */
110static void intel_crt_set_dpms(struct intel_encoder *encoder, int mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800111{
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200112 struct drm_device *dev = encoder->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800113 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200114 struct intel_crt *crt = intel_encoder_to_crt(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -0800115 u32 temp;
116
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200117 temp = I915_READ(crt->adpa_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800118 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
119 temp &= ~ADPA_DAC_ENABLE;
Jesse Barnesbd9e8412012-06-15 11:55:18 -0700120
Akshay Joshi0206e352011-08-16 15:34:10 -0400121 switch (mode) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800122 case DRM_MODE_DPMS_ON:
123 temp |= ADPA_DAC_ENABLE;
124 break;
125 case DRM_MODE_DPMS_STANDBY:
126 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
127 break;
128 case DRM_MODE_DPMS_SUSPEND:
129 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
130 break;
131 case DRM_MODE_DPMS_OFF:
132 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
133 break;
134 }
135
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200136 I915_WRITE(crt->adpa_reg, temp);
137}
138
139static void intel_crt_dpms(struct drm_connector *connector, int mode)
140{
141 struct drm_device *dev = connector->dev;
142 struct intel_encoder *encoder = intel_attached_encoder(connector);
143 struct drm_crtc *crtc;
144 int old_dpms;
145
146 /* PCH platforms and VLV only support on/off. */
147 if (INTEL_INFO(dev)->gen < 5 && mode != DRM_MODE_DPMS_ON)
148 mode = DRM_MODE_DPMS_OFF;
149
150 if (mode == connector->dpms)
151 return;
152
153 old_dpms = connector->dpms;
154 connector->dpms = mode;
155
156 /* Only need to change hw state when actually enabled */
157 crtc = encoder->base.crtc;
158 if (!crtc) {
159 encoder->connectors_active = false;
160 return;
161 }
162
163 /* We need the pipe to run for anything but OFF. */
164 if (mode == DRM_MODE_DPMS_OFF)
165 encoder->connectors_active = false;
166 else
167 encoder->connectors_active = true;
168
169 if (mode < old_dpms) {
170 /* From off to on, enable the pipe first. */
171 intel_crtc_update_dpms(crtc);
172
173 intel_crt_set_dpms(encoder, mode);
174 } else {
175 intel_crt_set_dpms(encoder, mode);
176
177 intel_crtc_update_dpms(crtc);
178 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +0200179
Daniel Vetterb9805142012-08-31 17:37:33 +0200180 intel_modeset_check_state(connector->dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800181}
182
183static int intel_crt_mode_valid(struct drm_connector *connector,
184 struct drm_display_mode *mode)
185{
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800186 struct drm_device *dev = connector->dev;
187
188 int max_clock = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800189 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
190 return MODE_NO_DBLESCAN;
191
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800192 if (mode->clock < 25000)
193 return MODE_CLOCK_LOW;
194
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100195 if (IS_GEN2(dev))
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800196 max_clock = 350000;
197 else
198 max_clock = 400000;
199 if (mode->clock > max_clock)
200 return MODE_CLOCK_HIGH;
Jesse Barnes79e53942008-11-07 14:24:08 -0800201
202 return MODE_OK;
203}
204
205static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200206 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -0800207 struct drm_display_mode *adjusted_mode)
208{
209 return true;
210}
211
212static void intel_crt_mode_set(struct drm_encoder *encoder,
213 struct drm_display_mode *mode,
214 struct drm_display_mode *adjusted_mode)
215{
216
217 struct drm_device *dev = encoder->dev;
218 struct drm_crtc *crtc = encoder->crtc;
Daniel Vetter540a8952012-07-11 16:27:57 +0200219 struct intel_crt *crt =
220 intel_encoder_to_crt(to_intel_encoder(encoder));
Jesse Barnes79e53942008-11-07 14:24:08 -0800221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
222 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eich6478d412012-10-14 16:33:11 +0200223 u32 adpa;
Jesse Barnes79e53942008-11-07 14:24:08 -0800224
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800225 adpa = ADPA_HOTPLUG_BITS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800226 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
227 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
228 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
229 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
230
Jesse Barnes75770562011-10-12 09:01:58 -0700231 /* For CPT allow 3 pipe config, for others just use A or B */
232 if (HAS_PCH_CPT(dev))
233 adpa |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
234 else if (intel_crtc->pipe == 0)
235 adpa |= ADPA_PIPE_A_SELECT;
236 else
237 adpa |= ADPA_PIPE_B_SELECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800238
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800239 if (!HAS_PCH_SPLIT(dev))
240 I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);
241
Daniel Vetter540a8952012-07-11 16:27:57 +0200242 I915_WRITE(crt->adpa_reg, adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800243}
244
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500245static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800246{
247 struct drm_device *dev = connector->dev;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800248 struct intel_crt *crt = intel_attached_crt(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800249 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800250 u32 adpa;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800251 bool ret;
252
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800253 /* The first time through, trigger an explicit detection cycle */
254 if (crt->force_hotplug_required) {
255 bool turn_off_dac = HAS_PCH_SPLIT(dev);
256 u32 save_adpa;
Zhenyu Wang67941da2009-07-24 01:00:33 +0800257
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800258 crt->force_hotplug_required = 0;
Dave Airlied5dd96c2010-08-04 15:52:19 +1000259
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800260 save_adpa = adpa = I915_READ(PCH_ADPA);
261 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
Dave Airlied5dd96c2010-08-04 15:52:19 +1000262
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800263 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
264 if (turn_off_dac)
265 adpa &= ~ADPA_DAC_ENABLE;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800266
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800267 I915_WRITE(PCH_ADPA, adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800268
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800269 if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
270 1000))
271 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
Zhenyu Wang2c072452009-06-05 15:38:42 +0800272
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800273 if (turn_off_dac) {
274 I915_WRITE(PCH_ADPA, save_adpa);
275 POSTING_READ(PCH_ADPA);
276 }
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800277 }
278
Zhenyu Wang2c072452009-06-05 15:38:42 +0800279 /* Check the status to see if both blue and green are on now */
280 adpa = I915_READ(PCH_ADPA);
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800281 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800282 ret = true;
283 else
284 ret = false;
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800285 DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800286
Zhenyu Wang2c072452009-06-05 15:38:42 +0800287 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800288}
289
Jesse Barnes7d2c24e2012-06-15 11:55:15 -0700290static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
291{
292 struct drm_device *dev = connector->dev;
293 struct drm_i915_private *dev_priv = dev->dev_private;
294 u32 adpa;
295 bool ret;
296 u32 save_adpa;
297
298 save_adpa = adpa = I915_READ(ADPA);
299 DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
300
301 adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
302
303 I915_WRITE(ADPA, adpa);
304
305 if (wait_for((I915_READ(ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
306 1000)) {
307 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
308 I915_WRITE(ADPA, save_adpa);
309 }
310
311 /* Check the status to see if both blue and green are on now */
312 adpa = I915_READ(ADPA);
313 if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
314 ret = true;
315 else
316 ret = false;
317
318 DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
319
320 /* FIXME: debug force function and remove */
321 ret = true;
322
323 return ret;
324}
325
Jesse Barnes79e53942008-11-07 14:24:08 -0800326/**
327 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
328 *
329 * Not for i915G/i915GM
330 *
331 * \return true if CRT is connected.
332 * \return false if CRT is disconnected.
333 */
334static bool intel_crt_detect_hotplug(struct drm_connector *connector)
335{
336 struct drm_device *dev = connector->dev;
337 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson7a772c42010-05-24 16:46:29 -0400338 u32 hotplug_en, orig, stat;
339 bool ret = false;
Zhao Yakui771cb082009-03-03 18:07:52 +0800340 int i, tries = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800341
Eric Anholtbad720f2009-10-22 16:11:14 -0700342 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500343 return intel_ironlake_crt_detect_hotplug(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800344
Jesse Barnes7d2c24e2012-06-15 11:55:15 -0700345 if (IS_VALLEYVIEW(dev))
346 return valleyview_crt_detect_hotplug(connector);
347
Zhao Yakui771cb082009-03-03 18:07:52 +0800348 /*
349 * On 4 series desktop, CRT detect sequence need to be done twice
350 * to get a reliable result.
351 */
Jesse Barnes79e53942008-11-07 14:24:08 -0800352
Zhao Yakui771cb082009-03-03 18:07:52 +0800353 if (IS_G4X(dev) && !IS_GM45(dev))
354 tries = 2;
355 else
356 tries = 1;
Adam Jackson7a772c42010-05-24 16:46:29 -0400357 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
Zhao Yakui771cb082009-03-03 18:07:52 +0800358 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800359
Zhao Yakui771cb082009-03-03 18:07:52 +0800360 for (i = 0; i < tries ; i++) {
Zhao Yakui771cb082009-03-03 18:07:52 +0800361 /* turn on the FORCE_DETECT */
362 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Zhao Yakui771cb082009-03-03 18:07:52 +0800363 /* wait for FORCE_DETECT to go off */
Chris Wilson913d8d12010-08-07 11:01:35 +0100364 if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
365 CRT_HOTPLUG_FORCE_DETECT) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100366 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100367 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
Zhao Yakui771cb082009-03-03 18:07:52 +0800368 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800369
Adam Jackson7a772c42010-05-24 16:46:29 -0400370 stat = I915_READ(PORT_HOTPLUG_STAT);
371 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
372 ret = true;
Jesse Barnes79e53942008-11-07 14:24:08 -0800373
Adam Jackson7a772c42010-05-24 16:46:29 -0400374 /* clear the interrupt we just generated, if any */
375 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
376
377 /* and put the bits back */
378 I915_WRITE(PORT_HOTPLUG_EN, orig);
379
380 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800381}
382
Jani Nikulaf1a2f5b2012-08-13 13:22:35 +0300383static struct edid *intel_crt_get_edid(struct drm_connector *connector,
384 struct i2c_adapter *i2c)
385{
386 struct edid *edid;
387
388 edid = drm_get_edid(connector, i2c);
389
390 if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
391 DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
392 intel_gmbus_force_bit(i2c, true);
393 edid = drm_get_edid(connector, i2c);
394 intel_gmbus_force_bit(i2c, false);
395 }
396
397 return edid;
398}
399
400/* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
401static int intel_crt_ddc_get_modes(struct drm_connector *connector,
402 struct i2c_adapter *adapter)
403{
404 struct edid *edid;
405
406 edid = intel_crt_get_edid(connector, adapter);
407 if (!edid)
408 return 0;
409
410 return intel_connector_update_modes(connector, edid);
411}
412
David Müllerf5afcd32011-01-06 12:29:32 +0000413static bool intel_crt_detect_ddc(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -0800414{
David Müllerf5afcd32011-01-06 12:29:32 +0000415 struct intel_crt *crt = intel_attached_crt(connector);
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000416 struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200417 struct edid *edid;
418 struct i2c_adapter *i2c;
Jesse Barnes79e53942008-11-07 14:24:08 -0800419
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200420 BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
Jesse Barnes79e53942008-11-07 14:24:08 -0800421
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200422 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
Jani Nikulaf1a2f5b2012-08-13 13:22:35 +0300423 edid = intel_crt_get_edid(connector, i2c);
David Müllerf5afcd32011-01-06 12:29:32 +0000424
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200425 if (edid) {
426 bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
427
David Müllerf5afcd32011-01-06 12:29:32 +0000428 /*
429 * This may be a DVI-I connector with a shared DDC
430 * link between analog and digital outputs, so we
431 * have to check the EDID input spec of the attached device.
432 */
David Müllerf5afcd32011-01-06 12:29:32 +0000433 if (!is_digital) {
434 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
435 return true;
436 }
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200437
438 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
439 } else {
440 DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100441 }
442
Daniel Vettera2bd1f52012-07-11 12:31:52 +0200443 kfree(edid);
444
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100445 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800446}
447
Ma Linge4a5d542009-05-26 11:31:00 +0800448static enum drm_connector_status
Chris Wilson71731882011-04-19 23:10:58 +0100449intel_crt_load_detect(struct intel_crt *crt)
Ma Linge4a5d542009-05-26 11:31:00 +0800450{
Chris Wilson71731882011-04-19 23:10:58 +0100451 struct drm_device *dev = crt->base.base.dev;
Ma Linge4a5d542009-05-26 11:31:00 +0800452 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson71731882011-04-19 23:10:58 +0100453 uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
Ma Linge4a5d542009-05-26 11:31:00 +0800454 uint32_t save_bclrpat;
455 uint32_t save_vtotal;
456 uint32_t vtotal, vactive;
457 uint32_t vsample;
458 uint32_t vblank, vblank_start, vblank_end;
459 uint32_t dsl;
460 uint32_t bclrpat_reg;
461 uint32_t vtotal_reg;
462 uint32_t vblank_reg;
463 uint32_t vsync_reg;
464 uint32_t pipeconf_reg;
465 uint32_t pipe_dsl_reg;
466 uint8_t st00;
467 enum drm_connector_status status;
468
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100469 DRM_DEBUG_KMS("starting load-detect on CRT\n");
470
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800471 bclrpat_reg = BCLRPAT(pipe);
472 vtotal_reg = VTOTAL(pipe);
473 vblank_reg = VBLANK(pipe);
474 vsync_reg = VSYNC(pipe);
475 pipeconf_reg = PIPECONF(pipe);
476 pipe_dsl_reg = PIPEDSL(pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800477
478 save_bclrpat = I915_READ(bclrpat_reg);
479 save_vtotal = I915_READ(vtotal_reg);
480 vblank = I915_READ(vblank_reg);
481
482 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
483 vactive = (save_vtotal & 0x7ff) + 1;
484
485 vblank_start = (vblank & 0xfff) + 1;
486 vblank_end = ((vblank >> 16) & 0xfff) + 1;
487
488 /* Set the border color to purple. */
489 I915_WRITE(bclrpat_reg, 0x500050);
490
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100491 if (!IS_GEN2(dev)) {
Ma Linge4a5d542009-05-26 11:31:00 +0800492 uint32_t pipeconf = I915_READ(pipeconf_reg);
493 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
Chris Wilson19c55da2010-08-09 14:50:53 +0100494 POSTING_READ(pipeconf_reg);
Ma Linge4a5d542009-05-26 11:31:00 +0800495 /* Wait for next Vblank to substitue
496 * border color for Color info */
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700497 intel_wait_for_vblank(dev, pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800498 st00 = I915_READ8(VGA_MSR_WRITE);
499 status = ((st00 & (1 << 4)) != 0) ?
500 connector_status_connected :
501 connector_status_disconnected;
502
503 I915_WRITE(pipeconf_reg, pipeconf);
504 } else {
505 bool restore_vblank = false;
506 int count, detect;
507
508 /*
509 * If there isn't any border, add some.
510 * Yes, this will flicker
511 */
512 if (vblank_start <= vactive && vblank_end >= vtotal) {
513 uint32_t vsync = I915_READ(vsync_reg);
514 uint32_t vsync_start = (vsync & 0xffff) + 1;
515
516 vblank_start = vsync_start;
517 I915_WRITE(vblank_reg,
518 (vblank_start - 1) |
519 ((vblank_end - 1) << 16));
520 restore_vblank = true;
521 }
522 /* sample in the vertical border, selecting the larger one */
523 if (vblank_start - vactive >= vtotal - vblank_end)
524 vsample = (vblank_start + vactive) >> 1;
525 else
526 vsample = (vtotal + vblank_end) >> 1;
527
528 /*
529 * Wait for the border to be displayed
530 */
531 while (I915_READ(pipe_dsl_reg) >= vactive)
532 ;
533 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
534 ;
535 /*
536 * Watch ST00 for an entire scanline
537 */
538 detect = 0;
539 count = 0;
540 do {
541 count++;
542 /* Read the ST00 VGA status register */
543 st00 = I915_READ8(VGA_MSR_WRITE);
544 if (st00 & (1 << 4))
545 detect++;
546 } while ((I915_READ(pipe_dsl_reg) == dsl));
547
548 /* restore vblank if necessary */
549 if (restore_vblank)
550 I915_WRITE(vblank_reg, vblank);
551 /*
552 * If more than 3/4 of the scanline detected a monitor,
553 * then it is assumed to be present. This works even on i830,
554 * where there isn't any way to force the border color across
555 * the screen
556 */
557 status = detect * 4 > count * 3 ?
558 connector_status_connected :
559 connector_status_disconnected;
560 }
561
562 /* Restore previous settings */
563 I915_WRITE(bclrpat_reg, save_bclrpat);
564
565 return status;
566}
567
Chris Wilson7b334fc2010-09-09 23:51:02 +0100568static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100569intel_crt_detect(struct drm_connector *connector, bool force)
Jesse Barnes79e53942008-11-07 14:24:08 -0800570{
571 struct drm_device *dev = connector->dev;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000572 struct intel_crt *crt = intel_attached_crt(connector);
Ma Linge4a5d542009-05-26 11:31:00 +0800573 enum drm_connector_status status;
Daniel Vettere95c8432012-04-20 21:03:36 +0200574 struct intel_load_detect_pipe tmp;
Jesse Barnes79e53942008-11-07 14:24:08 -0800575
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100576 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetteraaa37732012-06-16 15:30:32 +0200577 /* We can not rely on the HPD pin always being correctly wired
578 * up, for example many KVM do not pass it through, and so
579 * only trust an assertion that the monitor is connected.
580 */
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100581 if (intel_crt_detect_hotplug(connector)) {
582 DRM_DEBUG_KMS("CRT detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800583 return connector_status_connected;
Daniel Vetteraaa37732012-06-16 15:30:32 +0200584 } else
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800585 DRM_DEBUG_KMS("CRT not detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800586 }
587
David Müllerf5afcd32011-01-06 12:29:32 +0000588 if (intel_crt_detect_ddc(connector))
Jesse Barnes79e53942008-11-07 14:24:08 -0800589 return connector_status_connected;
590
Daniel Vetteraaa37732012-06-16 15:30:32 +0200591 /* Load detection is broken on HPD capable machines. Whoever wants a
592 * broken monitor (without edid) to work behind a broken kvm (that fails
593 * to have the right resistors for HP detection) needs to fix this up.
594 * For now just bail out. */
595 if (I915_HAS_HOTPLUG(dev))
596 return connector_status_disconnected;
597
Chris Wilson930a9e22010-09-14 11:07:23 +0100598 if (!force)
Chris Wilson7b334fc2010-09-09 23:51:02 +0100599 return connector->status;
600
Ma Linge4a5d542009-05-26 11:31:00 +0800601 /* for pre-945g platforms use load detect */
Daniel Vetterd2434ab2012-08-12 21:20:10 +0200602 if (intel_get_load_detect_pipe(connector, NULL, &tmp)) {
Daniel Vettere95c8432012-04-20 21:03:36 +0200603 if (intel_crt_detect_ddc(connector))
604 status = connector_status_connected;
605 else
606 status = intel_crt_load_detect(crt);
Daniel Vetterd2434ab2012-08-12 21:20:10 +0200607 intel_release_load_detect_pipe(connector, &tmp);
Daniel Vettere95c8432012-04-20 21:03:36 +0200608 } else
609 status = connector_status_unknown;
Ma Linge4a5d542009-05-26 11:31:00 +0800610
611 return status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800612}
613
614static void intel_crt_destroy(struct drm_connector *connector)
615{
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 drm_sysfs_connector_remove(connector);
617 drm_connector_cleanup(connector);
618 kfree(connector);
619}
620
621static int intel_crt_get_modes(struct drm_connector *connector)
622{
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800623 struct drm_device *dev = connector->dev;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700624 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson890f3352010-09-14 16:46:59 +0100625 int ret;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800626 struct i2c_adapter *i2c;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800627
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800628 i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->crt_ddc_pin);
Jani Nikulaf1a2f5b2012-08-13 13:22:35 +0300629 ret = intel_crt_ddc_get_modes(connector, i2c);
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800630 if (ret || !IS_G4X(dev))
Chris Wilsonf899fc62010-07-20 15:44:45 -0700631 return ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800632
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800633 /* Try to probe digital port for output in DVI-I -> VGA mode. */
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800634 i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
Jani Nikulaf1a2f5b2012-08-13 13:22:35 +0300635 return intel_crt_ddc_get_modes(connector, i2c);
Jesse Barnes79e53942008-11-07 14:24:08 -0800636}
637
638static int intel_crt_set_property(struct drm_connector *connector,
639 struct drm_property *property,
640 uint64_t value)
641{
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 return 0;
643}
644
Chris Wilsonf3269052011-01-24 15:17:08 +0000645static void intel_crt_reset(struct drm_connector *connector)
646{
647 struct drm_device *dev = connector->dev;
648 struct intel_crt *crt = intel_attached_crt(connector);
649
650 if (HAS_PCH_SPLIT(dev))
651 crt->force_hotplug_required = 1;
652}
653
Jesse Barnes79e53942008-11-07 14:24:08 -0800654/*
655 * Routines for controlling stuff on the analog port
656 */
657
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200658static const struct drm_encoder_helper_funcs crt_encoder_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 .mode_fixup = intel_crt_mode_fixup,
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 .mode_set = intel_crt_mode_set,
Daniel Vetter1f703852012-07-11 16:51:39 +0200661 .disable = intel_encoder_noop,
Jesse Barnes79e53942008-11-07 14:24:08 -0800662};
663
664static const struct drm_connector_funcs intel_crt_connector_funcs = {
Chris Wilsonf3269052011-01-24 15:17:08 +0000665 .reset = intel_crt_reset,
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200666 .dpms = intel_crt_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800667 .detect = intel_crt_detect,
668 .fill_modes = drm_helper_probe_single_connector_modes,
669 .destroy = intel_crt_destroy,
670 .set_property = intel_crt_set_property,
671};
672
673static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
674 .mode_valid = intel_crt_mode_valid,
675 .get_modes = intel_crt_get_modes,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100676 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800677};
678
Jesse Barnes79e53942008-11-07 14:24:08 -0800679static const struct drm_encoder_funcs intel_crt_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100680 .destroy = intel_encoder_destroy,
Jesse Barnes79e53942008-11-07 14:24:08 -0800681};
682
Duncan Laurie8ca40132011-10-25 15:42:21 -0700683static int __init intel_no_crt_dmi_callback(const struct dmi_system_id *id)
684{
Daniel Vetterbc0daf42012-04-01 13:16:49 +0200685 DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
Duncan Laurie8ca40132011-10-25 15:42:21 -0700686 return 1;
687}
688
689static const struct dmi_system_id intel_no_crt[] = {
690 {
691 .callback = intel_no_crt_dmi_callback,
692 .ident = "ACER ZGB",
693 .matches = {
694 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
695 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
696 },
697 },
698 { }
699};
700
Jesse Barnes79e53942008-11-07 14:24:08 -0800701void intel_crt_init(struct drm_device *dev)
702{
703 struct drm_connector *connector;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000704 struct intel_crt *crt;
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800705 struct intel_connector *intel_connector;
David Müller (ELSOFT AG)db545012009-08-29 08:54:45 +0200706 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800707
Duncan Laurie8ca40132011-10-25 15:42:21 -0700708 /* Skip machines without VGA that falsely report hotplug events */
709 if (dmi_check_system(intel_no_crt))
710 return;
711
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000712 crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
713 if (!crt)
Jesse Barnes79e53942008-11-07 14:24:08 -0800714 return;
715
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800716 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
717 if (!intel_connector) {
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000718 kfree(crt);
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800719 return;
720 }
721
722 connector = &intel_connector->base;
723 drm_connector_init(dev, &intel_connector->base,
Jesse Barnes79e53942008-11-07 14:24:08 -0800724 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
725
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000726 drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800727 DRM_MODE_ENCODER_DAC);
728
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000729 intel_connector_attach_encoder(intel_connector, &crt->base);
Jesse Barnes79e53942008-11-07 14:24:08 -0800730
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000731 crt->base.type = INTEL_OUTPUT_ANALOG;
Daniel Vetter66a92782012-07-12 20:08:18 +0200732 crt->base.cloneable = true;
Eugeni Dodonov59c859d2012-05-09 15:37:19 -0300733 if (IS_HASWELL(dev))
734 crt->base.crtc_mask = (1 << 0);
735 else
Keith Packard08268742012-08-13 21:34:45 -0700736 crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -0300737
Daniel Vetterdbb02572012-01-28 14:49:23 +0100738 if (IS_GEN2(dev))
739 connector->interlace_allowed = 0;
740 else
741 connector->interlace_allowed = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -0800742 connector->doublescan_allowed = 0;
743
Jesse Barnesdf0323c2012-04-17 15:06:33 -0700744 if (HAS_PCH_SPLIT(dev))
Daniel Vetter540a8952012-07-11 16:27:57 +0200745 crt->adpa_reg = PCH_ADPA;
746 else if (IS_VALLEYVIEW(dev))
747 crt->adpa_reg = VLV_ADPA;
748 else
749 crt->adpa_reg = ADPA;
750
Daniel Vetter21246042012-07-01 14:58:27 +0200751 crt->base.disable = intel_disable_crt;
752 crt->base.enable = intel_enable_crt;
Daniel Vettere403fc92012-07-02 13:41:21 +0200753 crt->base.get_hw_state = intel_crt_get_hw_state;
754 intel_connector->get_hw_state = intel_connector_get_hw_state;
Daniel Vetter21246042012-07-01 14:58:27 +0200755
Daniel Vetterb2cabb02012-07-01 22:42:24 +0200756 drm_encoder_helper_add(&crt->base.base, &crt_encoder_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -0800757 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
758
759 drm_sysfs_connector_add(connector);
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800760
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000761 if (I915_HAS_HOTPLUG(dev))
762 connector->polled = DRM_CONNECTOR_POLL_HPD;
763 else
764 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
765
Keith Packarde7dbb2f2010-11-16 16:03:53 +0800766 /*
767 * Configure the automatic hotplug detection stuff
768 */
769 crt->force_hotplug_required = 0;
770 if (HAS_PCH_SPLIT(dev)) {
771 u32 adpa;
772
773 adpa = I915_READ(PCH_ADPA);
774 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
775 adpa |= ADPA_HOTPLUG_BITS;
776 I915_WRITE(PCH_ADPA, adpa);
777 POSTING_READ(PCH_ADPA);
778
779 DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);
780 crt->force_hotplug_required = 1;
781 }
782
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800783 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800784}