blob: e38bc6769180a27c4aee30a21d72e8bbe640440d [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
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +000037struct intel_crt {
38 struct intel_encoder base;
39};
40
41static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
42{
43 return container_of(intel_attached_encoder(connector),
44 struct intel_crt, base);
45}
46
Jesse Barnes79e53942008-11-07 14:24:08 -080047static void intel_crt_dpms(struct drm_encoder *encoder, int mode)
48{
49 struct drm_device *dev = encoder->dev;
50 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +080051 u32 temp, reg;
Jesse Barnes79e53942008-11-07 14:24:08 -080052
Eric Anholtbad720f2009-10-22 16:11:14 -070053 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +080054 reg = PCH_ADPA;
55 else
56 reg = ADPA;
57
58 temp = I915_READ(reg);
Jesse Barnes79e53942008-11-07 14:24:08 -080059 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
ling.ma@intel.comfebc7692009-06-25 11:55:57 +080060 temp &= ~ADPA_DAC_ENABLE;
Jesse Barnes79e53942008-11-07 14:24:08 -080061
62 switch(mode) {
63 case DRM_MODE_DPMS_ON:
64 temp |= ADPA_DAC_ENABLE;
65 break;
66 case DRM_MODE_DPMS_STANDBY:
67 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
68 break;
69 case DRM_MODE_DPMS_SUSPEND:
70 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
71 break;
72 case DRM_MODE_DPMS_OFF:
73 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
74 break;
75 }
76
Zhenyu Wang2c072452009-06-05 15:38:42 +080077 I915_WRITE(reg, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -080078}
79
80static int intel_crt_mode_valid(struct drm_connector *connector,
81 struct drm_display_mode *mode)
82{
Zhao Yakui6bcdcd92009-03-03 18:06:42 +080083 struct drm_device *dev = connector->dev;
84
85 int max_clock = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080086 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
87 return MODE_NO_DBLESCAN;
88
Zhao Yakui6bcdcd92009-03-03 18:06:42 +080089 if (mode->clock < 25000)
90 return MODE_CLOCK_LOW;
91
Chris Wilsona6c45cf2010-09-17 00:32:17 +010092 if (IS_GEN2(dev))
Zhao Yakui6bcdcd92009-03-03 18:06:42 +080093 max_clock = 350000;
94 else
95 max_clock = 400000;
96 if (mode->clock > max_clock)
97 return MODE_CLOCK_HIGH;
Jesse Barnes79e53942008-11-07 14:24:08 -080098
99 return MODE_OK;
100}
101
102static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
103 struct drm_display_mode *mode,
104 struct drm_display_mode *adjusted_mode)
105{
106 return true;
107}
108
109static void intel_crt_mode_set(struct drm_encoder *encoder,
110 struct drm_display_mode *mode,
111 struct drm_display_mode *adjusted_mode)
112{
113
114 struct drm_device *dev = encoder->dev;
115 struct drm_crtc *crtc = encoder->crtc;
116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
117 struct drm_i915_private *dev_priv = dev->dev_private;
118 int dpll_md_reg;
119 u32 adpa, dpll_md;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800120 u32 adpa_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -0800121
122 if (intel_crtc->pipe == 0)
123 dpll_md_reg = DPLL_A_MD;
124 else
125 dpll_md_reg = DPLL_B_MD;
126
Eric Anholtbad720f2009-10-22 16:11:14 -0700127 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800128 adpa_reg = PCH_ADPA;
129 else
130 adpa_reg = ADPA;
131
Jesse Barnes79e53942008-11-07 14:24:08 -0800132 /*
133 * Disable separate mode multiplier used when cloning SDVO to CRT
134 * XXX this needs to be adjusted when we really are cloning
135 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100136 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800137 dpll_md = I915_READ(dpll_md_reg);
138 I915_WRITE(dpll_md_reg,
139 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
140 }
141
142 adpa = 0;
143 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
144 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
145 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
146 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
147
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800148 if (intel_crtc->pipe == 0) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +0800149 if (HAS_PCH_CPT(dev))
150 adpa |= PORT_TRANS_A_SEL_CPT;
151 else
152 adpa |= ADPA_PIPE_A_SELECT;
Eric Anholtbad720f2009-10-22 16:11:14 -0700153 if (!HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800154 I915_WRITE(BCLRPAT_A, 0);
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800155 } else {
Zhenyu Wang8db9d772010-04-07 16:15:54 +0800156 if (HAS_PCH_CPT(dev))
157 adpa |= PORT_TRANS_B_SEL_CPT;
158 else
159 adpa |= ADPA_PIPE_B_SELECT;
Eric Anholtbad720f2009-10-22 16:11:14 -0700160 if (!HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800161 I915_WRITE(BCLRPAT_B, 0);
Zhao Yakui6bcdcd92009-03-03 18:06:42 +0800162 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800163
Zhenyu Wang2c072452009-06-05 15:38:42 +0800164 I915_WRITE(adpa_reg, adpa);
165}
166
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500167static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800168{
169 struct drm_device *dev = connector->dev;
170 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800171 u32 adpa, temp;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800172 bool ret;
Dave Airlied5dd96c2010-08-04 15:52:19 +1000173 bool turn_off_dac = false;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800174
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800175 temp = adpa = I915_READ(PCH_ADPA);
Zhenyu Wang67941da2009-07-24 01:00:33 +0800176
Dave Airlied5dd96c2010-08-04 15:52:19 +1000177 if (HAS_PCH_SPLIT(dev))
178 turn_off_dac = true;
179
180 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
181 if (turn_off_dac)
182 adpa &= ~ADPA_DAC_ENABLE;
183
184 /* disable HPD first */
185 I915_WRITE(PCH_ADPA, adpa);
186 (void)I915_READ(PCH_ADPA);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800187
188 adpa |= (ADPA_CRT_HOTPLUG_PERIOD_128 |
189 ADPA_CRT_HOTPLUG_WARMUP_10MS |
190 ADPA_CRT_HOTPLUG_SAMPLE_4S |
191 ADPA_CRT_HOTPLUG_VOLTAGE_50 | /* default */
192 ADPA_CRT_HOTPLUG_VOLREF_325MV |
193 ADPA_CRT_HOTPLUG_ENABLE |
194 ADPA_CRT_HOTPLUG_FORCE_TRIGGER);
195
Zhao Yakui28c97732009-10-09 11:39:41 +0800196 DRM_DEBUG_KMS("pch crt adpa 0x%x", adpa);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800197 I915_WRITE(PCH_ADPA, adpa);
198
Chris Wilson913d8d12010-08-07 11:01:35 +0100199 if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100200 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100201 DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
Zhenyu Wang2c072452009-06-05 15:38:42 +0800202
Dave Airlied5dd96c2010-08-04 15:52:19 +1000203 if (turn_off_dac) {
Yuanhan Liu1510a972010-10-08 10:18:01 +0100204 /* Make sure hotplug is enabled */
205 I915_WRITE(PCH_ADPA, temp | ADPA_CRT_HOTPLUG_ENABLE);
Zhenyu Wanga4a6b902010-04-07 16:15:55 +0800206 (void)I915_READ(PCH_ADPA);
207 }
208
Zhenyu Wang2c072452009-06-05 15:38:42 +0800209 /* Check the status to see if both blue and green are on now */
210 adpa = I915_READ(PCH_ADPA);
Zhenyu Wang67941da2009-07-24 01:00:33 +0800211 adpa &= ADPA_CRT_HOTPLUG_MONITOR_MASK;
212 if ((adpa == ADPA_CRT_HOTPLUG_MONITOR_COLOR) ||
213 (adpa == ADPA_CRT_HOTPLUG_MONITOR_MONO))
Zhenyu Wang2c072452009-06-05 15:38:42 +0800214 ret = true;
215 else
216 ret = false;
217
Zhenyu Wang2c072452009-06-05 15:38:42 +0800218 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800219}
220
221/**
222 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
223 *
224 * Not for i915G/i915GM
225 *
226 * \return true if CRT is connected.
227 * \return false if CRT is disconnected.
228 */
229static bool intel_crt_detect_hotplug(struct drm_connector *connector)
230{
231 struct drm_device *dev = connector->dev;
232 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson7a772c42010-05-24 16:46:29 -0400233 u32 hotplug_en, orig, stat;
234 bool ret = false;
Zhao Yakui771cb082009-03-03 18:07:52 +0800235 int i, tries = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800236
Eric Anholtbad720f2009-10-22 16:11:14 -0700237 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500238 return intel_ironlake_crt_detect_hotplug(connector);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800239
Zhao Yakui771cb082009-03-03 18:07:52 +0800240 /*
241 * On 4 series desktop, CRT detect sequence need to be done twice
242 * to get a reliable result.
243 */
Jesse Barnes79e53942008-11-07 14:24:08 -0800244
Zhao Yakui771cb082009-03-03 18:07:52 +0800245 if (IS_G4X(dev) && !IS_GM45(dev))
246 tries = 2;
247 else
248 tries = 1;
Adam Jackson7a772c42010-05-24 16:46:29 -0400249 hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
Zhao Yakui771cb082009-03-03 18:07:52 +0800250 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800251
Zhao Yakui771cb082009-03-03 18:07:52 +0800252 for (i = 0; i < tries ; i++) {
Zhao Yakui771cb082009-03-03 18:07:52 +0800253 /* turn on the FORCE_DETECT */
254 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Zhao Yakui771cb082009-03-03 18:07:52 +0800255 /* wait for FORCE_DETECT to go off */
Chris Wilson913d8d12010-08-07 11:01:35 +0100256 if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
257 CRT_HOTPLUG_FORCE_DETECT) == 0,
Chris Wilson481b6af2010-08-23 17:43:35 +0100258 1000))
Chris Wilson79077312010-09-12 19:58:04 +0100259 DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
Zhao Yakui771cb082009-03-03 18:07:52 +0800260 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800261
Adam Jackson7a772c42010-05-24 16:46:29 -0400262 stat = I915_READ(PORT_HOTPLUG_STAT);
263 if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
264 ret = true;
Jesse Barnes79e53942008-11-07 14:24:08 -0800265
Adam Jackson7a772c42010-05-24 16:46:29 -0400266 /* clear the interrupt we just generated, if any */
267 I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
268
269 /* and put the bits back */
270 I915_WRITE(PORT_HOTPLUG_EN, orig);
271
272 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800273}
274
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100275static bool intel_crt_ddc_probe(struct drm_i915_private *dev_priv, int ddc_bus)
276{
277 u8 buf;
278 struct i2c_msg msgs[] = {
279 {
280 .addr = 0xA0,
281 .flags = 0,
282 .len = 1,
283 .buf = &buf,
284 },
285 };
286 /* DDC monitor detect: Does it ACK a write to 0xA0? */
287 return i2c_transfer(&dev_priv->gmbus[ddc_bus].adapter, msgs, 1) == 1;
288}
289
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000290static bool intel_crt_detect_ddc(struct intel_crt *crt)
Jesse Barnes79e53942008-11-07 14:24:08 -0800291{
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000292 struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800293
294 /* CRT should always be at 0, but check anyway */
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000295 if (crt->base.type != INTEL_OUTPUT_ANALOG)
Jesse Barnes79e53942008-11-07 14:24:08 -0800296 return false;
297
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100298 if (intel_crt_ddc_probe(dev_priv, dev_priv->crt_ddc_pin)) {
299 DRM_DEBUG_KMS("CRT detected via DDC:0xa0\n");
300 return true;
301 }
302
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000303 if (intel_ddc_probe(&crt->base, dev_priv->crt_ddc_pin)) {
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100304 DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
305 return true;
306 }
307
308 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800309}
310
Ma Linge4a5d542009-05-26 11:31:00 +0800311static enum drm_connector_status
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000312intel_crt_load_detect(struct drm_crtc *crtc, struct intel_crt *crt)
Ma Linge4a5d542009-05-26 11:31:00 +0800313{
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000314 struct drm_encoder *encoder = &crt->base.base;
Ma Linge4a5d542009-05-26 11:31:00 +0800315 struct drm_device *dev = encoder->dev;
316 struct drm_i915_private *dev_priv = dev->dev_private;
317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
318 uint32_t pipe = intel_crtc->pipe;
319 uint32_t save_bclrpat;
320 uint32_t save_vtotal;
321 uint32_t vtotal, vactive;
322 uint32_t vsample;
323 uint32_t vblank, vblank_start, vblank_end;
324 uint32_t dsl;
325 uint32_t bclrpat_reg;
326 uint32_t vtotal_reg;
327 uint32_t vblank_reg;
328 uint32_t vsync_reg;
329 uint32_t pipeconf_reg;
330 uint32_t pipe_dsl_reg;
331 uint8_t st00;
332 enum drm_connector_status status;
333
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100334 DRM_DEBUG_KMS("starting load-detect on CRT\n");
335
Ma Linge4a5d542009-05-26 11:31:00 +0800336 if (pipe == 0) {
337 bclrpat_reg = BCLRPAT_A;
338 vtotal_reg = VTOTAL_A;
339 vblank_reg = VBLANK_A;
340 vsync_reg = VSYNC_A;
341 pipeconf_reg = PIPEACONF;
342 pipe_dsl_reg = PIPEADSL;
343 } else {
344 bclrpat_reg = BCLRPAT_B;
345 vtotal_reg = VTOTAL_B;
346 vblank_reg = VBLANK_B;
347 vsync_reg = VSYNC_B;
348 pipeconf_reg = PIPEBCONF;
349 pipe_dsl_reg = PIPEBDSL;
350 }
351
352 save_bclrpat = I915_READ(bclrpat_reg);
353 save_vtotal = I915_READ(vtotal_reg);
354 vblank = I915_READ(vblank_reg);
355
356 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
357 vactive = (save_vtotal & 0x7ff) + 1;
358
359 vblank_start = (vblank & 0xfff) + 1;
360 vblank_end = ((vblank >> 16) & 0xfff) + 1;
361
362 /* Set the border color to purple. */
363 I915_WRITE(bclrpat_reg, 0x500050);
364
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100365 if (!IS_GEN2(dev)) {
Ma Linge4a5d542009-05-26 11:31:00 +0800366 uint32_t pipeconf = I915_READ(pipeconf_reg);
367 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
Chris Wilson19c55da2010-08-09 14:50:53 +0100368 POSTING_READ(pipeconf_reg);
Ma Linge4a5d542009-05-26 11:31:00 +0800369 /* Wait for next Vblank to substitue
370 * border color for Color info */
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700371 intel_wait_for_vblank(dev, pipe);
Ma Linge4a5d542009-05-26 11:31:00 +0800372 st00 = I915_READ8(VGA_MSR_WRITE);
373 status = ((st00 & (1 << 4)) != 0) ?
374 connector_status_connected :
375 connector_status_disconnected;
376
377 I915_WRITE(pipeconf_reg, pipeconf);
378 } else {
379 bool restore_vblank = false;
380 int count, detect;
381
382 /*
383 * If there isn't any border, add some.
384 * Yes, this will flicker
385 */
386 if (vblank_start <= vactive && vblank_end >= vtotal) {
387 uint32_t vsync = I915_READ(vsync_reg);
388 uint32_t vsync_start = (vsync & 0xffff) + 1;
389
390 vblank_start = vsync_start;
391 I915_WRITE(vblank_reg,
392 (vblank_start - 1) |
393 ((vblank_end - 1) << 16));
394 restore_vblank = true;
395 }
396 /* sample in the vertical border, selecting the larger one */
397 if (vblank_start - vactive >= vtotal - vblank_end)
398 vsample = (vblank_start + vactive) >> 1;
399 else
400 vsample = (vtotal + vblank_end) >> 1;
401
402 /*
403 * Wait for the border to be displayed
404 */
405 while (I915_READ(pipe_dsl_reg) >= vactive)
406 ;
407 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
408 ;
409 /*
410 * Watch ST00 for an entire scanline
411 */
412 detect = 0;
413 count = 0;
414 do {
415 count++;
416 /* Read the ST00 VGA status register */
417 st00 = I915_READ8(VGA_MSR_WRITE);
418 if (st00 & (1 << 4))
419 detect++;
420 } while ((I915_READ(pipe_dsl_reg) == dsl));
421
422 /* restore vblank if necessary */
423 if (restore_vblank)
424 I915_WRITE(vblank_reg, vblank);
425 /*
426 * If more than 3/4 of the scanline detected a monitor,
427 * then it is assumed to be present. This works even on i830,
428 * where there isn't any way to force the border color across
429 * the screen
430 */
431 status = detect * 4 > count * 3 ?
432 connector_status_connected :
433 connector_status_disconnected;
434 }
435
436 /* Restore previous settings */
437 I915_WRITE(bclrpat_reg, save_bclrpat);
438
439 return status;
440}
441
Chris Wilson7b334fc2010-09-09 23:51:02 +0100442static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100443intel_crt_detect(struct drm_connector *connector, bool force)
Jesse Barnes79e53942008-11-07 14:24:08 -0800444{
445 struct drm_device *dev = connector->dev;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000446 struct intel_crt *crt = intel_attached_crt(connector);
Ma Linge4a5d542009-05-26 11:31:00 +0800447 struct drm_crtc *crtc;
448 int dpms_mode;
449 enum drm_connector_status status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800450
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100451 if (I915_HAS_HOTPLUG(dev)) {
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100452 if (intel_crt_detect_hotplug(connector)) {
453 DRM_DEBUG_KMS("CRT detected via hotplug\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800454 return connector_status_connected;
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100455 } else
Jesse Barnes79e53942008-11-07 14:24:08 -0800456 return connector_status_disconnected;
457 }
458
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000459 if (intel_crt_detect_ddc(crt))
Jesse Barnes79e53942008-11-07 14:24:08 -0800460 return connector_status_connected;
461
Chris Wilson930a9e22010-09-14 11:07:23 +0100462 if (!force)
Chris Wilson7b334fc2010-09-09 23:51:02 +0100463 return connector->status;
464
Ma Linge4a5d542009-05-26 11:31:00 +0800465 /* for pre-945g platforms use load detect */
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000466 crtc = crt->base.base.crtc;
467 if (crtc && crtc->enabled) {
468 status = intel_crt_load_detect(crtc, crt);
Ma Linge4a5d542009-05-26 11:31:00 +0800469 } else {
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000470 crtc = intel_get_load_detect_pipe(&crt->base, connector,
Ma Linge4a5d542009-05-26 11:31:00 +0800471 NULL, &dpms_mode);
472 if (crtc) {
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000473 if (intel_crt_detect_ddc(crt))
Chris Wilson6ec3d0c2010-09-22 18:17:01 +0100474 status = connector_status_connected;
475 else
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000476 status = intel_crt_load_detect(crtc, crt);
477 intel_release_load_detect_pipe(&crt->base,
Zhenyu Wangc1c43972010-03-30 14:39:30 +0800478 connector, dpms_mode);
Ma Linge4a5d542009-05-26 11:31:00 +0800479 } else
480 status = connector_status_unknown;
481 }
482
483 return status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800484}
485
486static void intel_crt_destroy(struct drm_connector *connector)
487{
Jesse Barnes79e53942008-11-07 14:24:08 -0800488 drm_sysfs_connector_remove(connector);
489 drm_connector_cleanup(connector);
490 kfree(connector);
491}
492
493static int intel_crt_get_modes(struct drm_connector *connector)
494{
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800495 struct drm_device *dev = connector->dev;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700496 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson890f3352010-09-14 16:46:59 +0100497 int ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800498
Chris Wilsonf899fc62010-07-20 15:44:45 -0700499 ret = intel_ddc_get_modes(connector,
500 &dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800501 if (ret || !IS_G4X(dev))
Chris Wilsonf899fc62010-07-20 15:44:45 -0700502 return ret;
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800503
ling.ma@intel.com8e4d36b2009-06-30 11:35:34 +0800504 /* Try to probe digital port for output in DVI-I -> VGA mode. */
Chris Wilsonf899fc62010-07-20 15:44:45 -0700505 return intel_ddc_get_modes(connector,
506 &dev_priv->gmbus[GMBUS_PORT_DPB].adapter);
Jesse Barnes79e53942008-11-07 14:24:08 -0800507}
508
509static int intel_crt_set_property(struct drm_connector *connector,
510 struct drm_property *property,
511 uint64_t value)
512{
Jesse Barnes79e53942008-11-07 14:24:08 -0800513 return 0;
514}
515
516/*
517 * Routines for controlling stuff on the analog port
518 */
519
520static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
521 .dpms = intel_crt_dpms,
522 .mode_fixup = intel_crt_mode_fixup,
523 .prepare = intel_encoder_prepare,
524 .commit = intel_encoder_commit,
525 .mode_set = intel_crt_mode_set,
526};
527
528static const struct drm_connector_funcs intel_crt_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700529 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800530 .detect = intel_crt_detect,
531 .fill_modes = drm_helper_probe_single_connector_modes,
532 .destroy = intel_crt_destroy,
533 .set_property = intel_crt_set_property,
534};
535
536static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
537 .mode_valid = intel_crt_mode_valid,
538 .get_modes = intel_crt_get_modes,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100539 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800540};
541
Jesse Barnes79e53942008-11-07 14:24:08 -0800542static const struct drm_encoder_funcs intel_crt_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100543 .destroy = intel_encoder_destroy,
Jesse Barnes79e53942008-11-07 14:24:08 -0800544};
545
546void intel_crt_init(struct drm_device *dev)
547{
548 struct drm_connector *connector;
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000549 struct intel_crt *crt;
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800550 struct intel_connector *intel_connector;
David Müller (ELSOFT AG)db545012009-08-29 08:54:45 +0200551 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800552
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000553 crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
554 if (!crt)
Jesse Barnes79e53942008-11-07 14:24:08 -0800555 return;
556
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800557 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
558 if (!intel_connector) {
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000559 kfree(crt);
Zhenyu Wang454c1ca2010-03-29 15:53:23 +0800560 return;
561 }
562
563 connector = &intel_connector->base;
564 drm_connector_init(dev, &intel_connector->base,
Jesse Barnes79e53942008-11-07 14:24:08 -0800565 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
566
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000567 drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800568 DRM_MODE_ENCODER_DAC);
569
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000570 intel_connector_attach_encoder(intel_connector, &crt->base);
Jesse Barnes79e53942008-11-07 14:24:08 -0800571
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000572 crt->base.type = INTEL_OUTPUT_ANALOG;
573 crt->base.clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT |
574 1 << INTEL_ANALOG_CLONE_BIT |
575 1 << INTEL_SDVO_LVDS_CLONE_BIT);
576 crt->base.crtc_mask = (1 << 0) | (1 << 1);
Krzysztof Halasa734b4152010-05-25 18:41:46 +0200577 connector->interlace_allowed = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -0800578 connector->doublescan_allowed = 0;
579
Chris Wilsonc9a1c4c2010-11-16 10:58:37 +0000580 drm_encoder_helper_add(&crt->base.base, &intel_crt_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -0800581 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
582
583 drm_sysfs_connector_add(connector);
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800584
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000585 if (I915_HAS_HOTPLUG(dev))
586 connector->polled = DRM_CONNECTOR_POLL_HPD;
587 else
588 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
589
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800590 dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800591}