blob: 389fcd2aea1f0f7fe90ec6e0dbb152214b735b7b [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
27#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080029#include "drmP.h"
30#include "drm.h"
31#include "drm_crtc.h"
32#include "drm_crtc_helper.h"
33#include "intel_drv.h"
34#include "i915_drm.h"
35#include "i915_drv.h"
36
37static void intel_crt_dpms(struct drm_encoder *encoder, int mode)
38{
39 struct drm_device *dev = encoder->dev;
40 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +080041 u32 temp, reg;
Jesse Barnes79e53942008-11-07 14:24:08 -080042
Eric Anholtbad720f2009-10-22 16:11:14 -070043 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +080044 reg = PCH_ADPA;
45 else
46 reg = ADPA;
47
48 temp = I915_READ(reg);
Jesse Barnes79e53942008-11-07 14:24:08 -080049 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
ling.ma@intel.comfebc7692009-06-25 11:55:57 +080050 temp &= ~ADPA_DAC_ENABLE;
Jesse Barnes79e53942008-11-07 14:24:08 -080051
52 switch(mode) {
53 case DRM_MODE_DPMS_ON:
54 temp |= ADPA_DAC_ENABLE;
55 break;
56 case DRM_MODE_DPMS_STANDBY:
57 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
58 break;
59 case DRM_MODE_DPMS_SUSPEND:
60 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
61 break;
62 case DRM_MODE_DPMS_OFF:
63 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
64 break;
65 }
66
Zhenyu Wang2c072452009-06-05 15:38:42 +080067 I915_WRITE(reg, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -080068}
69
70static int intel_crt_mode_valid(struct drm_connector *connector,
71 struct drm_display_mode *mode)
72{
Zhao Yakui6bcdcd92009-03-03 18:06:42 +080073 struct drm_device *dev = connector->dev;
74
75 int max_clock = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080076 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
77 return MODE_NO_DBLESCAN;
78
Zhao Yakui6bcdcd92009-03-03 18:06:42 +080079 if (mode->clock < 25000)
80 return MODE_CLOCK_LOW;
81
Chris Wilsona6c45cf2010-09-17 00:32:17 +010082 if (IS_GEN2(dev))
Zhao Yakui6bcdcd92009-03-03 18:06:42 +080083 max_clock = 350000;
84 else
85 max_clock = 400000;
86 if (mode->clock > max_clock)
87 return MODE_CLOCK_HIGH;
Jesse Barnes79e53942008-11-07 14:24:08 -080088
89 return MODE_OK;
90}
91
92static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
93 struct drm_display_mode *mode,
94 struct drm_display_mode *adjusted_mode)
95{
96 return true;
97}
98
99static void intel_crt_mode_set(struct drm_encoder *encoder,
100 struct drm_display_mode *mode,
101 struct drm_display_mode *adjusted_mode)
102{
103
104 struct drm_device *dev = encoder->dev;
105 struct drm_crtc *crtc = encoder->crtc;
106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
107 struct drm_i915_private *dev_priv = dev->dev_private;
108 int dpll_md_reg;
109 u32 adpa, dpll_md;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800110 u32 adpa_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -0800111
112 if (intel_crtc->pipe == 0)
113 dpll_md_reg = DPLL_A_MD;
114 else
115 dpll_md_reg = DPLL_B_MD;
116
Eric Anholtbad720f2009-10-22 16:11:14 -0700117 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800118 adpa_reg = PCH_ADPA;
119 else
120 adpa_reg = ADPA;
121
Jesse Barnes79e53942008-11-07 14:24:08 -0800122 /*
123 * Disable separate mode multiplier used when cloning SDVO to CRT
124 * XXX this needs to be adjusted when we really are cloning
125 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100126 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800127 dpll_md = I915_READ(dpll_md_reg);
128 I915_WRITE(dpll_md_reg,
129 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
130 }
131
132 adpa = 0;
133 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
134 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
135 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
136 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
137
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800138 if (intel_crtc->pipe == 0) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +0800139 if (HAS_PCH_CPT(dev))
140 adpa |= PORT_TRANS_A_SEL_CPT;
141 else
142 adpa |= ADPA_PIPE_A_SELECT;
Eric Anholtbad720f2009-10-22 16:11:14 -0700143 if (!HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800144 I915_WRITE(BCLRPAT_A, 0);
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800145 } else {
Zhenyu Wang8db9d772010-04-07 16:15:54 +0800146 if (HAS_PCH_CPT(dev))
147 adpa |= PORT_TRANS_B_SEL_CPT;
148 else
149 adpa |= ADPA_PIPE_B_SELECT;
Eric Anholtbad720f2009-10-22 16:11:14 -0700150 if (!HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800151 I915_WRITE(BCLRPAT_B, 0);
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800152 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800153
Zhenyu Wang2c072452009-06-05 15:38:42 +0800154 I915_WRITE(adpa_reg, adpa);
155}
156
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500157static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800158{
159 struct drm_device *dev = connector->dev;
160 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800161 u32 adpa, temp;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800162 bool ret;
Dave Airlied5dd96c2010-08-04 15:52:19 +1000163 bool turn_off_dac = false;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800164
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800165 temp = adpa = I915_READ(PCH_ADPA);
Zhenyu Wang67941da2009-07-24 01:00:33 +0800166
Dave Airlied5dd96c2010-08-04 15:52:19 +1000167 if (HAS_PCH_SPLIT(dev))
168 turn_off_dac = true;
169
170 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
171 if (turn_off_dac)
172 adpa &= ~ADPA_DAC_ENABLE;
173
174 /* disable HPD first */
175 I915_WRITE(PCH_ADPA, adpa);
176 (void)I915_READ(PCH_ADPA);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800177
178 adpa |= (ADPA_CRT_HOTPLUG_PERIOD_128 |
179 ADPA_CRT_HOTPLUG_WARMUP_10MS |
180 ADPA_CRT_HOTPLUG_SAMPLE_4S |
181 ADPA_CRT_HOTPLUG_VOLTAGE_50 | /* default */
182 ADPA_CRT_HOTPLUG_VOLREF_325MV |
183 ADPA_CRT_HOTPLUG_ENABLE |
184 ADPA_CRT_HOTPLUG_FORCE_TRIGGER);
185
Zhao Yakui28c97732009-10-09 11:39:41 +0800186 DRM_DEBUG_KMS("pch crt adpa 0x%x", adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800187 I915_WRITE(PCH_ADPA, adpa);
188
Chris Wilson913d8d12010-08-07 11:01:35 +0100189 if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100190 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100191 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
Zhenyu Wang2c072452009-06-05 15:38:42 +0800192
Dave Airlied5dd96c2010-08-04 15:52:19 +1000193 if (turn_off_dac) {
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800194 I915_WRITE(PCH_ADPA, temp);
195 (void)I915_READ(PCH_ADPA);
196 }
197
Zhenyu Wang2c072452009-06-05 15:38:42 +0800198 /* Check the status to see if both blue and green are on now */
199 adpa = I915_READ(PCH_ADPA);
Zhenyu Wang67941da2009-07-24 01:00:33 +0800200 adpa &= ADPA_CRT_HOTPLUG_MONITOR_MASK;
201 if ((adpa == ADPA_CRT_HOTPLUG_MONITOR_COLOR) ||
202 (adpa == ADPA_CRT_HOTPLUG_MONITOR_MONO))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800203 ret = true;
204 else
205 ret = false;
206
Zhenyu Wang2c072452009-06-05 15:38:42 +0800207 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800208}
209
210/**
211 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
212 *
213 * Not for i915G/i915GM
214 *
215 * \return true if CRT is connected.
216 * \return false if CRT is disconnected.
217 */
218static bool intel_crt_detect_hotplug(struct drm_connector *connector)
219{
220 struct drm_device *dev = connector->dev;
221 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson7a772c42010-05-24 16:46:29 -0400222 u32 hotplug_en, orig, stat;
223 bool ret = false;
Zhao Yakui771cb082009-03-03 18:07:52 +0800224 int i, tries = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800225
Eric Anholtbad720f2009-10-22 16:11:14 -0700226 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500227 return intel_ironlake_crt_detect_hotplug(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800228
Zhao Yakui771cb082009-03-03 18:07:52 +0800229 /*
230 * On 4 series desktop, CRT detect sequence need to be done twice
231 * to get a reliable result.
232 */
Jesse Barnes79e53942008-11-07 14:24:08 -0800233
Zhao Yakui771cb082009-03-03 18:07:52 +0800234 if (IS_G4X(dev) && !IS_GM45(dev))
235 tries = 2;
236 else
237 tries = 1;
Adam Jackson7a772c42010-05-24 16:46:29 -0400238 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
Zhao Yakui771cb082009-03-03 18:07:52 +0800239 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800240
Zhao Yakui771cb082009-03-03 18:07:52 +0800241 for (i = 0; i < tries ; i++) {
Zhao Yakui771cb082009-03-03 18:07:52 +0800242 /* turn on the FORCE_DETECT */
243 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Zhao Yakui771cb082009-03-03 18:07:52 +0800244 /* wait for FORCE_DETECT to go off */
Chris Wilson913d8d12010-08-07 11:01:35 +0100245 if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
246 CRT_HOTPLUG_FORCE_DETECT) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100247 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100248 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
Zhao Yakui771cb082009-03-03 18:07:52 +0800249 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800250
Adam Jackson7a772c42010-05-24 16:46:29 -0400251 stat = I915_READ(PORT_HOTPLUG_STAT);
252 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
253 ret = true;
Jesse Barnes79e53942008-11-07 14:24:08 -0800254
Adam Jackson7a772c42010-05-24 16:46:29 -0400255 /* clear the interrupt we just generated, if any */
256 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
257
258 /* and put the bits back */
259 I915_WRITE(PORT_HOTPLUG_EN, orig);
260
261 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800262}
263
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100264static bool intel_crt_ddc_probe(struct drm_i915_private *dev_priv, int ddc_bus)
265{
266 u8 buf;
267 struct i2c_msg msgs[] = {
268 {
269 .addr = 0xA0,
270 .flags = 0,
271 .len = 1,
272 .buf = &buf,
273 },
274 };
275 /* DDC monitor detect: Does it ACK a write to 0xA0? */
276 return i2c_transfer(&dev_priv->gmbus[ddc_bus].adapter, msgs, 1) == 1;
277}
278
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800279static bool intel_crt_detect_ddc(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -0800280{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100281 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700282 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800283
284 /* CRT should always be at 0, but check anyway */
Eric Anholt21d40d32010-03-25 11:11:14 -0700285 if (intel_encoder->type != INTEL_OUTPUT_ANALOG)
Jesse Barnes79e53942008-11-07 14:24:08 -0800286 return false;
287
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100288 if (intel_crt_ddc_probe(dev_priv, dev_priv->crt_ddc_pin)) {
289 DRM_DEBUG_KMS("CRT detected via DDC:0xa0\n");
290 return true;
291 }
292
293 if (intel_ddc_probe(intel_encoder, dev_priv->crt_ddc_pin)) {
294 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
295 return true;
296 }
297
298 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800299}
300
Ma Linge4a5d542009-05-26 11:31:00 +0800301static enum drm_connector_status
Eric Anholt21d40d32010-03-25 11:11:14 -0700302intel_crt_load_detect(struct drm_crtc *crtc, struct intel_encoder *intel_encoder)
Ma Linge4a5d542009-05-26 11:31:00 +0800303{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100304 struct drm_encoder *encoder = &intel_encoder->base;
Ma Linge4a5d542009-05-26 11:31:00 +0800305 struct drm_device *dev = encoder->dev;
306 struct drm_i915_private *dev_priv = dev->dev_private;
307 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
308 uint32_t pipe = intel_crtc->pipe;
309 uint32_t save_bclrpat;
310 uint32_t save_vtotal;
311 uint32_t vtotal, vactive;
312 uint32_t vsample;
313 uint32_t vblank, vblank_start, vblank_end;
314 uint32_t dsl;
315 uint32_t bclrpat_reg;
316 uint32_t vtotal_reg;
317 uint32_t vblank_reg;
318 uint32_t vsync_reg;
319 uint32_t pipeconf_reg;
320 uint32_t pipe_dsl_reg;
321 uint8_t st00;
322 enum drm_connector_status status;
323
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100324 DRM_DEBUG_KMS("starting load-detect on CRT\n");
325
Ma Linge4a5d542009-05-26 11:31:00 +0800326 if (pipe == 0) {
327 bclrpat_reg = BCLRPAT_A;
328 vtotal_reg = VTOTAL_A;
329 vblank_reg = VBLANK_A;
330 vsync_reg = VSYNC_A;
331 pipeconf_reg = PIPEACONF;
332 pipe_dsl_reg = PIPEADSL;
333 } else {
334 bclrpat_reg = BCLRPAT_B;
335 vtotal_reg = VTOTAL_B;
336 vblank_reg = VBLANK_B;
337 vsync_reg = VSYNC_B;
338 pipeconf_reg = PIPEBCONF;
339 pipe_dsl_reg = PIPEBDSL;
340 }
341
342 save_bclrpat = I915_READ(bclrpat_reg);
343 save_vtotal = I915_READ(vtotal_reg);
344 vblank = I915_READ(vblank_reg);
345
346 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
347 vactive = (save_vtotal & 0x7ff) + 1;
348
349 vblank_start = (vblank & 0xfff) + 1;
350 vblank_end = ((vblank >> 16) & 0xfff) + 1;
351
352 /* Set the border color to purple. */
353 I915_WRITE(bclrpat_reg, 0x500050);
354
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100355 if (!IS_GEN2(dev)) {
Ma Linge4a5d542009-05-26 11:31:00 +0800356 uint32_t pipeconf = I915_READ(pipeconf_reg);
357 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
Chris Wilson19c55da2010-08-09 14:50:53 +0100358 POSTING_READ(pipeconf_reg);
Ma Linge4a5d542009-05-26 11:31:00 +0800359 /* Wait for next Vblank to substitue
360 * border color for Color info */
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700361 intel_wait_for_vblank(dev, pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800362 st00 = I915_READ8(VGA_MSR_WRITE);
363 status = ((st00 & (1 << 4)) != 0) ?
364 connector_status_connected :
365 connector_status_disconnected;
366
367 I915_WRITE(pipeconf_reg, pipeconf);
368 } else {
369 bool restore_vblank = false;
370 int count, detect;
371
372 /*
373 * If there isn't any border, add some.
374 * Yes, this will flicker
375 */
376 if (vblank_start <= vactive && vblank_end >= vtotal) {
377 uint32_t vsync = I915_READ(vsync_reg);
378 uint32_t vsync_start = (vsync & 0xffff) + 1;
379
380 vblank_start = vsync_start;
381 I915_WRITE(vblank_reg,
382 (vblank_start - 1) |
383 ((vblank_end - 1) << 16));
384 restore_vblank = true;
385 }
386 /* sample in the vertical border, selecting the larger one */
387 if (vblank_start - vactive >= vtotal - vblank_end)
388 vsample = (vblank_start + vactive) >> 1;
389 else
390 vsample = (vtotal + vblank_end) >> 1;
391
392 /*
393 * Wait for the border to be displayed
394 */
395 while (I915_READ(pipe_dsl_reg) >= vactive)
396 ;
397 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
398 ;
399 /*
400 * Watch ST00 for an entire scanline
401 */
402 detect = 0;
403 count = 0;
404 do {
405 count++;
406 /* Read the ST00 VGA status register */
407 st00 = I915_READ8(VGA_MSR_WRITE);
408 if (st00 & (1 << 4))
409 detect++;
410 } while ((I915_READ(pipe_dsl_reg) == dsl));
411
412 /* restore vblank if necessary */
413 if (restore_vblank)
414 I915_WRITE(vblank_reg, vblank);
415 /*
416 * If more than 3/4 of the scanline detected a monitor,
417 * then it is assumed to be present. This works even on i830,
418 * where there isn't any way to force the border color across
419 * the screen
420 */
421 status = detect * 4 > count * 3 ?
422 connector_status_connected :
423 connector_status_disconnected;
424 }
425
426 /* Restore previous settings */
427 I915_WRITE(bclrpat_reg, save_bclrpat);
428
429 return status;
430}
431
Chris Wilson7b334fc2010-09-09 23:51:02 +0100432static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100433intel_crt_detect(struct drm_connector *connector, bool force)
Jesse Barnes79e53942008-11-07 14:24:08 -0800434{
435 struct drm_device *dev = connector->dev;
Chris Wilsondf0e9242010-09-09 16:20:55 +0100436 struct intel_encoder *encoder = intel_attached_encoder(connector);
Ma Linge4a5d542009-05-26 11:31:00 +0800437 struct drm_crtc *crtc;
438 int dpms_mode;
439 enum drm_connector_status status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800440
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100441 if (I915_HAS_HOTPLUG(dev)) {
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100442 if (intel_crt_detect_hotplug(connector)) {
443 DRM_DEBUG_KMS("CRT detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800444 return connector_status_connected;
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100445 } else
Jesse Barnes79e53942008-11-07 14:24:08 -0800446 return connector_status_disconnected;
447 }
448
Chris Wilsondf0e9242010-09-09 16:20:55 +0100449 if (intel_crt_detect_ddc(&encoder->base))
Jesse Barnes79e53942008-11-07 14:24:08 -0800450 return connector_status_connected;
451
Chris Wilson930a9e22010-09-14 11:07:23 +0100452 if (!force)
Chris Wilson7b334fc2010-09-09 23:51:02 +0100453 return connector->status;
454
Ma Linge4a5d542009-05-26 11:31:00 +0800455 /* for pre-945g platforms use load detect */
Chris Wilsondf0e9242010-09-09 16:20:55 +0100456 if (encoder->base.crtc && encoder->base.crtc->enabled) {
457 status = intel_crt_load_detect(encoder->base.crtc, encoder);
Ma Linge4a5d542009-05-26 11:31:00 +0800458 } else {
Chris Wilsondf0e9242010-09-09 16:20:55 +0100459 crtc = intel_get_load_detect_pipe(encoder, connector,
Ma Linge4a5d542009-05-26 11:31:00 +0800460 NULL, &dpms_mode);
461 if (crtc) {
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100462 if (intel_crt_detect_ddc(&encoder->base))
463 status = connector_status_connected;
464 else
465 status = intel_crt_load_detect(crtc, encoder);
Chris Wilsondf0e9242010-09-09 16:20:55 +0100466 intel_release_load_detect_pipe(encoder,
Zhenyu Wangc1c43972010-03-30 14:39:30 +0800467 connector, dpms_mode);
Ma Linge4a5d542009-05-26 11:31:00 +0800468 } else
469 status = connector_status_unknown;
470 }
471
472 return status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800473}
474
475static void intel_crt_destroy(struct drm_connector *connector)
476{
Jesse Barnes79e53942008-11-07 14:24:08 -0800477 drm_sysfs_connector_remove(connector);
478 drm_connector_cleanup(connector);
479 kfree(connector);
480}
481
482static int intel_crt_get_modes(struct drm_connector *connector)
483{
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800484 struct drm_device *dev = connector->dev;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700485 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson890f3352010-09-14 16:46:59 +0100486 int ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800487
Chris Wilsonf899fc62010-07-20 15:44:45 -0700488 ret = intel_ddc_get_modes(connector,
489 &dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800490 if (ret || !IS_G4X(dev))
Chris Wilsonf899fc62010-07-20 15:44:45 -0700491 return ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800492
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800493 /* Try to probe digital port for output in DVI-I -> VGA mode. */
Chris Wilsonf899fc62010-07-20 15:44:45 -0700494 return intel_ddc_get_modes(connector,
495 &dev_priv->gmbus[GMBUS_PORT_DPB].adapter);
Jesse Barnes79e53942008-11-07 14:24:08 -0800496}
497
498static int intel_crt_set_property(struct drm_connector *connector,
499 struct drm_property *property,
500 uint64_t value)
501{
Jesse Barnes79e53942008-11-07 14:24:08 -0800502 return 0;
503}
504
505/*
506 * Routines for controlling stuff on the analog port
507 */
508
509static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
510 .dpms = intel_crt_dpms,
511 .mode_fixup = intel_crt_mode_fixup,
512 .prepare = intel_encoder_prepare,
513 .commit = intel_encoder_commit,
514 .mode_set = intel_crt_mode_set,
515};
516
517static const struct drm_connector_funcs intel_crt_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700518 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800519 .detect = intel_crt_detect,
520 .fill_modes = drm_helper_probe_single_connector_modes,
521 .destroy = intel_crt_destroy,
522 .set_property = intel_crt_set_property,
523};
524
525static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
526 .mode_valid = intel_crt_mode_valid,
527 .get_modes = intel_crt_get_modes,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100528 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800529};
530
Jesse Barnes79e53942008-11-07 14:24:08 -0800531static const struct drm_encoder_funcs intel_crt_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100532 .destroy = intel_encoder_destroy,
Jesse Barnes79e53942008-11-07 14:24:08 -0800533};
534
535void intel_crt_init(struct drm_device *dev)
536{
537 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700538 struct intel_encoder *intel_encoder;
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800539 struct intel_connector *intel_connector;
David Müller (ELSOFT AG)db545012009-08-29 08:54:45 +0200540 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800541
Eric Anholt21d40d32010-03-25 11:11:14 -0700542 intel_encoder = kzalloc(sizeof(struct intel_encoder), GFP_KERNEL);
543 if (!intel_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -0800544 return;
545
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800546 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
547 if (!intel_connector) {
548 kfree(intel_encoder);
549 return;
550 }
551
552 connector = &intel_connector->base;
553 drm_connector_init(dev, &intel_connector->base,
Jesse Barnes79e53942008-11-07 14:24:08 -0800554 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
555
Chris Wilson4ef69c72010-09-09 15:14:28 +0100556 drm_encoder_init(dev, &intel_encoder->base, &intel_crt_enc_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800557 DRM_MODE_ENCODER_DAC);
558
Chris Wilsondf0e9242010-09-09 16:20:55 +0100559 intel_connector_attach_encoder(intel_connector, intel_encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -0800560
Eric Anholt21d40d32010-03-25 11:11:14 -0700561 intel_encoder->type = INTEL_OUTPUT_ANALOG;
562 intel_encoder->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
Ma Lingf8aed702009-08-24 13:50:24 +0800563 (1 << INTEL_ANALOG_CLONE_BIT) |
564 (1 << INTEL_SDVO_LVDS_CLONE_BIT);
Eric Anholt21d40d32010-03-25 11:11:14 -0700565 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Krzysztof Halasa734b4152010-05-25 18:41:46 +0200566 connector->interlace_allowed = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -0800567 connector->doublescan_allowed = 0;
568
Chris Wilson4ef69c72010-09-09 15:14:28 +0100569 drm_encoder_helper_add(&intel_encoder->base, &intel_crt_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -0800570 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
571
572 drm_sysfs_connector_add(connector);
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800573
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000574 if (I915_HAS_HOTPLUG(dev))
575 connector->polled = DRM_CONNECTOR_POLL_HPD;
576 else
577 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
578
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800579 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800580}