drm/i915: fix VGA detect on IGDNG
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / drm / i915 / intel_crt.c
blob4cf8e2e88a40eb56aae2052fedab7af3c03b45c4
1 /*
2 * Copyright © 2006-2007 Intel Corporation
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:
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.
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.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/i2c.h>
28 #include "drmP.h"
29 #include "drm.h"
30 #include "drm_crtc.h"
31 #include "drm_crtc_helper.h"
32 #include "intel_drv.h"
33 #include "i915_drm.h"
34 #include "i915_drv.h"
36 static void intel_crt_dpms(struct drm_encoder *encoder, int mode)
38 struct drm_device *dev = encoder->dev;
39 struct drm_i915_private *dev_priv = dev->dev_private;
40 u32 temp, reg;
42 if (IS_IGDNG(dev))
43 reg = PCH_ADPA;
44 else
45 reg = ADPA;
47 temp = I915_READ(reg);
48 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
49 temp &= ~ADPA_DAC_ENABLE;
51 switch(mode) {
52 case DRM_MODE_DPMS_ON:
53 temp |= ADPA_DAC_ENABLE;
54 break;
55 case DRM_MODE_DPMS_STANDBY:
56 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
57 break;
58 case DRM_MODE_DPMS_SUSPEND:
59 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
60 break;
61 case DRM_MODE_DPMS_OFF:
62 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
63 break;
66 I915_WRITE(reg, temp);
69 static int intel_crt_mode_valid(struct drm_connector *connector,
70 struct drm_display_mode *mode)
72 struct drm_device *dev = connector->dev;
74 int max_clock = 0;
75 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
76 return MODE_NO_DBLESCAN;
78 if (mode->clock < 25000)
79 return MODE_CLOCK_LOW;
81 if (!IS_I9XX(dev))
82 max_clock = 350000;
83 else
84 max_clock = 400000;
85 if (mode->clock > max_clock)
86 return MODE_CLOCK_HIGH;
88 return MODE_OK;
91 static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
92 struct drm_display_mode *mode,
93 struct drm_display_mode *adjusted_mode)
95 return true;
98 static void intel_crt_mode_set(struct drm_encoder *encoder,
99 struct drm_display_mode *mode,
100 struct drm_display_mode *adjusted_mode)
103 struct drm_device *dev = encoder->dev;
104 struct drm_crtc *crtc = encoder->crtc;
105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
106 struct drm_i915_private *dev_priv = dev->dev_private;
107 int dpll_md_reg;
108 u32 adpa, dpll_md;
109 u32 adpa_reg;
111 if (intel_crtc->pipe == 0)
112 dpll_md_reg = DPLL_A_MD;
113 else
114 dpll_md_reg = DPLL_B_MD;
116 if (IS_IGDNG(dev))
117 adpa_reg = PCH_ADPA;
118 else
119 adpa_reg = ADPA;
122 * Disable separate mode multiplier used when cloning SDVO to CRT
123 * XXX this needs to be adjusted when we really are cloning
125 if (IS_I965G(dev) && !IS_IGDNG(dev)) {
126 dpll_md = I915_READ(dpll_md_reg);
127 I915_WRITE(dpll_md_reg,
128 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
131 adpa = 0;
132 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
133 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
134 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
135 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
137 if (intel_crtc->pipe == 0) {
138 adpa |= ADPA_PIPE_A_SELECT;
139 if (!IS_IGDNG(dev))
140 I915_WRITE(BCLRPAT_A, 0);
141 } else {
142 adpa |= ADPA_PIPE_B_SELECT;
143 if (!IS_IGDNG(dev))
144 I915_WRITE(BCLRPAT_B, 0);
147 I915_WRITE(adpa_reg, adpa);
150 static bool intel_igdng_crt_detect_hotplug(struct drm_connector *connector)
152 struct drm_device *dev = connector->dev;
153 struct drm_i915_private *dev_priv = dev->dev_private;
154 u32 adpa, temp;
155 bool ret;
157 temp = adpa = I915_READ(PCH_ADPA);
159 adpa &= ~ADPA_DAC_ENABLE;
160 I915_WRITE(PCH_ADPA, adpa);
162 adpa &= ~ADPA_CRT_HOTPLUG_MASK;
164 adpa |= (ADPA_CRT_HOTPLUG_PERIOD_128 |
165 ADPA_CRT_HOTPLUG_WARMUP_10MS |
166 ADPA_CRT_HOTPLUG_SAMPLE_4S |
167 ADPA_CRT_HOTPLUG_VOLTAGE_50 | /* default */
168 ADPA_CRT_HOTPLUG_VOLREF_325MV |
169 ADPA_CRT_HOTPLUG_ENABLE |
170 ADPA_CRT_HOTPLUG_FORCE_TRIGGER);
172 DRM_DEBUG("pch crt adpa 0x%x", adpa);
173 I915_WRITE(PCH_ADPA, adpa);
175 while ((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) != 0)
178 /* Check the status to see if both blue and green are on now */
179 adpa = I915_READ(PCH_ADPA);
180 adpa &= ADPA_CRT_HOTPLUG_MONITOR_MASK;
181 if ((adpa == ADPA_CRT_HOTPLUG_MONITOR_COLOR) ||
182 (adpa == ADPA_CRT_HOTPLUG_MONITOR_MONO))
183 ret = true;
184 else
185 ret = false;
187 /* restore origin register */
188 I915_WRITE(PCH_ADPA, temp);
189 return ret;
193 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
195 * Not for i915G/i915GM
197 * \return true if CRT is connected.
198 * \return false if CRT is disconnected.
200 static bool intel_crt_detect_hotplug(struct drm_connector *connector)
202 struct drm_device *dev = connector->dev;
203 struct drm_i915_private *dev_priv = dev->dev_private;
204 u32 hotplug_en;
205 int i, tries = 0;
207 if (IS_IGDNG(dev))
208 return intel_igdng_crt_detect_hotplug(connector);
211 * On 4 series desktop, CRT detect sequence need to be done twice
212 * to get a reliable result.
215 if (IS_G4X(dev) && !IS_GM45(dev))
216 tries = 2;
217 else
218 tries = 1;
219 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
220 hotplug_en &= CRT_FORCE_HOTPLUG_MASK;
221 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
223 if (IS_G4X(dev))
224 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
226 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
228 for (i = 0; i < tries ; i++) {
229 unsigned long timeout;
230 /* turn on the FORCE_DETECT */
231 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
232 timeout = jiffies + msecs_to_jiffies(1000);
233 /* wait for FORCE_DETECT to go off */
234 do {
235 if (!(I915_READ(PORT_HOTPLUG_EN) &
236 CRT_HOTPLUG_FORCE_DETECT))
237 break;
238 msleep(1);
239 } while (time_after(timeout, jiffies));
242 if ((I915_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) ==
243 CRT_HOTPLUG_MONITOR_COLOR)
244 return true;
246 return false;
249 static bool intel_crt_detect_ddc(struct drm_connector *connector)
251 struct intel_output *intel_output = to_intel_output(connector);
253 /* CRT should always be at 0, but check anyway */
254 if (intel_output->type != INTEL_OUTPUT_ANALOG)
255 return false;
257 return intel_ddc_probe(intel_output);
260 static enum drm_connector_status
261 intel_crt_load_detect(struct drm_crtc *crtc, struct intel_output *intel_output)
263 struct drm_encoder *encoder = &intel_output->enc;
264 struct drm_device *dev = encoder->dev;
265 struct drm_i915_private *dev_priv = dev->dev_private;
266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
267 uint32_t pipe = intel_crtc->pipe;
268 uint32_t save_bclrpat;
269 uint32_t save_vtotal;
270 uint32_t vtotal, vactive;
271 uint32_t vsample;
272 uint32_t vblank, vblank_start, vblank_end;
273 uint32_t dsl;
274 uint32_t bclrpat_reg;
275 uint32_t vtotal_reg;
276 uint32_t vblank_reg;
277 uint32_t vsync_reg;
278 uint32_t pipeconf_reg;
279 uint32_t pipe_dsl_reg;
280 uint8_t st00;
281 enum drm_connector_status status;
283 if (pipe == 0) {
284 bclrpat_reg = BCLRPAT_A;
285 vtotal_reg = VTOTAL_A;
286 vblank_reg = VBLANK_A;
287 vsync_reg = VSYNC_A;
288 pipeconf_reg = PIPEACONF;
289 pipe_dsl_reg = PIPEADSL;
290 } else {
291 bclrpat_reg = BCLRPAT_B;
292 vtotal_reg = VTOTAL_B;
293 vblank_reg = VBLANK_B;
294 vsync_reg = VSYNC_B;
295 pipeconf_reg = PIPEBCONF;
296 pipe_dsl_reg = PIPEBDSL;
299 save_bclrpat = I915_READ(bclrpat_reg);
300 save_vtotal = I915_READ(vtotal_reg);
301 vblank = I915_READ(vblank_reg);
303 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
304 vactive = (save_vtotal & 0x7ff) + 1;
306 vblank_start = (vblank & 0xfff) + 1;
307 vblank_end = ((vblank >> 16) & 0xfff) + 1;
309 /* Set the border color to purple. */
310 I915_WRITE(bclrpat_reg, 0x500050);
312 if (IS_I9XX(dev)) {
313 uint32_t pipeconf = I915_READ(pipeconf_reg);
314 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
315 /* Wait for next Vblank to substitue
316 * border color for Color info */
317 intel_wait_for_vblank(dev);
318 st00 = I915_READ8(VGA_MSR_WRITE);
319 status = ((st00 & (1 << 4)) != 0) ?
320 connector_status_connected :
321 connector_status_disconnected;
323 I915_WRITE(pipeconf_reg, pipeconf);
324 } else {
325 bool restore_vblank = false;
326 int count, detect;
329 * If there isn't any border, add some.
330 * Yes, this will flicker
332 if (vblank_start <= vactive && vblank_end >= vtotal) {
333 uint32_t vsync = I915_READ(vsync_reg);
334 uint32_t vsync_start = (vsync & 0xffff) + 1;
336 vblank_start = vsync_start;
337 I915_WRITE(vblank_reg,
338 (vblank_start - 1) |
339 ((vblank_end - 1) << 16));
340 restore_vblank = true;
342 /* sample in the vertical border, selecting the larger one */
343 if (vblank_start - vactive >= vtotal - vblank_end)
344 vsample = (vblank_start + vactive) >> 1;
345 else
346 vsample = (vtotal + vblank_end) >> 1;
349 * Wait for the border to be displayed
351 while (I915_READ(pipe_dsl_reg) >= vactive)
353 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
356 * Watch ST00 for an entire scanline
358 detect = 0;
359 count = 0;
360 do {
361 count++;
362 /* Read the ST00 VGA status register */
363 st00 = I915_READ8(VGA_MSR_WRITE);
364 if (st00 & (1 << 4))
365 detect++;
366 } while ((I915_READ(pipe_dsl_reg) == dsl));
368 /* restore vblank if necessary */
369 if (restore_vblank)
370 I915_WRITE(vblank_reg, vblank);
372 * If more than 3/4 of the scanline detected a monitor,
373 * then it is assumed to be present. This works even on i830,
374 * where there isn't any way to force the border color across
375 * the screen
377 status = detect * 4 > count * 3 ?
378 connector_status_connected :
379 connector_status_disconnected;
382 /* Restore previous settings */
383 I915_WRITE(bclrpat_reg, save_bclrpat);
385 return status;
388 static enum drm_connector_status intel_crt_detect(struct drm_connector *connector)
390 struct drm_device *dev = connector->dev;
391 struct intel_output *intel_output = to_intel_output(connector);
392 struct drm_encoder *encoder = &intel_output->enc;
393 struct drm_crtc *crtc;
394 int dpms_mode;
395 enum drm_connector_status status;
397 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
398 if (intel_crt_detect_hotplug(connector))
399 return connector_status_connected;
400 else
401 return connector_status_disconnected;
404 if (intel_crt_detect_ddc(connector))
405 return connector_status_connected;
407 /* for pre-945g platforms use load detect */
408 if (encoder->crtc && encoder->crtc->enabled) {
409 status = intel_crt_load_detect(encoder->crtc, intel_output);
410 } else {
411 crtc = intel_get_load_detect_pipe(intel_output,
412 NULL, &dpms_mode);
413 if (crtc) {
414 status = intel_crt_load_detect(crtc, intel_output);
415 intel_release_load_detect_pipe(intel_output, dpms_mode);
416 } else
417 status = connector_status_unknown;
420 return status;
423 static void intel_crt_destroy(struct drm_connector *connector)
425 struct intel_output *intel_output = to_intel_output(connector);
427 intel_i2c_destroy(intel_output->ddc_bus);
428 drm_sysfs_connector_remove(connector);
429 drm_connector_cleanup(connector);
430 kfree(connector);
433 static int intel_crt_get_modes(struct drm_connector *connector)
435 int ret;
436 struct intel_output *intel_output = to_intel_output(connector);
437 struct i2c_adapter *ddcbus;
438 struct drm_device *dev = connector->dev;
441 ret = intel_ddc_get_modes(intel_output);
442 if (ret || !IS_G4X(dev))
443 goto end;
445 ddcbus = intel_output->ddc_bus;
446 /* Try to probe digital port for output in DVI-I -> VGA mode. */
447 intel_output->ddc_bus =
448 intel_i2c_create(connector->dev, GPIOD, "CRTDDC_D");
450 if (!intel_output->ddc_bus) {
451 intel_output->ddc_bus = ddcbus;
452 dev_printk(KERN_ERR, &connector->dev->pdev->dev,
453 "DDC bus registration failed for CRTDDC_D.\n");
454 goto end;
456 /* Try to get modes by GPIOD port */
457 ret = intel_ddc_get_modes(intel_output);
458 intel_i2c_destroy(ddcbus);
460 end:
461 return ret;
465 static int intel_crt_set_property(struct drm_connector *connector,
466 struct drm_property *property,
467 uint64_t value)
469 return 0;
473 * Routines for controlling stuff on the analog port
476 static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
477 .dpms = intel_crt_dpms,
478 .mode_fixup = intel_crt_mode_fixup,
479 .prepare = intel_encoder_prepare,
480 .commit = intel_encoder_commit,
481 .mode_set = intel_crt_mode_set,
484 static const struct drm_connector_funcs intel_crt_connector_funcs = {
485 .dpms = drm_helper_connector_dpms,
486 .detect = intel_crt_detect,
487 .fill_modes = drm_helper_probe_single_connector_modes,
488 .destroy = intel_crt_destroy,
489 .set_property = intel_crt_set_property,
492 static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
493 .mode_valid = intel_crt_mode_valid,
494 .get_modes = intel_crt_get_modes,
495 .best_encoder = intel_best_encoder,
498 static void intel_crt_enc_destroy(struct drm_encoder *encoder)
500 drm_encoder_cleanup(encoder);
503 static const struct drm_encoder_funcs intel_crt_enc_funcs = {
504 .destroy = intel_crt_enc_destroy,
507 void intel_crt_init(struct drm_device *dev)
509 struct drm_connector *connector;
510 struct intel_output *intel_output;
511 u32 i2c_reg;
513 intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
514 if (!intel_output)
515 return;
517 connector = &intel_output->base;
518 drm_connector_init(dev, &intel_output->base,
519 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
521 drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
522 DRM_MODE_ENCODER_DAC);
524 drm_mode_connector_attach_encoder(&intel_output->base,
525 &intel_output->enc);
527 /* Set up the DDC bus. */
528 if (IS_IGDNG(dev))
529 i2c_reg = PCH_GPIOA;
530 else
531 i2c_reg = GPIOA;
532 intel_output->ddc_bus = intel_i2c_create(dev, i2c_reg, "CRTDDC_A");
533 if (!intel_output->ddc_bus) {
534 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
535 "failed.\n");
536 return;
539 intel_output->type = INTEL_OUTPUT_ANALOG;
540 connector->interlace_allowed = 0;
541 connector->doublescan_allowed = 0;
543 drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
544 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
546 drm_sysfs_connector_add(connector);