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
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.
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/i2c.h>
31 #include "drm_crtc_helper.h"
32 #include "intel_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
;
47 temp
= I915_READ(reg
);
48 temp
&= ~(ADPA_HSYNC_CNTL_DISABLE
| ADPA_VSYNC_CNTL_DISABLE
);
49 temp
&= ~ADPA_DAC_ENABLE
;
52 case DRM_MODE_DPMS_ON
:
53 temp
|= ADPA_DAC_ENABLE
;
55 case DRM_MODE_DPMS_STANDBY
:
56 temp
|= ADPA_DAC_ENABLE
| ADPA_HSYNC_CNTL_DISABLE
;
58 case DRM_MODE_DPMS_SUSPEND
:
59 temp
|= ADPA_DAC_ENABLE
| ADPA_VSYNC_CNTL_DISABLE
;
61 case DRM_MODE_DPMS_OFF
:
62 temp
|= ADPA_HSYNC_CNTL_DISABLE
| ADPA_VSYNC_CNTL_DISABLE
;
66 I915_WRITE(reg
, temp
);
69 if (mode
== DRM_MODE_DPMS_OFF
) {
71 temp
= I915_READ(PORT_HOTPLUG_EN
);
72 temp
&= ~CRT_EOS_INT_EN
;
73 I915_WRITE(PORT_HOTPLUG_EN
, temp
);
75 temp
= I915_READ(PORT_HOTPLUG_STAT
);
76 if (temp
& CRT_EOS_INT_STATUS
)
77 I915_WRITE(PORT_HOTPLUG_STAT
,
80 /* turn on DAC. EOS interrupt must be enabled after DAC
81 * is enabled, so it sounds not good to enable it in
82 * i915_driver_irq_postinstall()
83 * wait 12.5ms after DAC is enabled
86 temp
= I915_READ(PORT_HOTPLUG_STAT
);
87 if (temp
& CRT_EOS_INT_STATUS
)
88 I915_WRITE(PORT_HOTPLUG_STAT
,
90 temp
= I915_READ(PORT_HOTPLUG_EN
);
91 temp
|= CRT_EOS_INT_EN
;
92 I915_WRITE(PORT_HOTPLUG_EN
, temp
);
97 static int intel_crt_mode_valid(struct drm_connector
*connector
,
98 struct drm_display_mode
*mode
)
100 struct drm_device
*dev
= connector
->dev
;
103 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
104 return MODE_NO_DBLESCAN
;
106 if (mode
->clock
< 25000)
107 return MODE_CLOCK_LOW
;
113 if (mode
->clock
> max_clock
)
114 return MODE_CLOCK_HIGH
;
119 static bool intel_crt_mode_fixup(struct drm_encoder
*encoder
,
120 struct drm_display_mode
*mode
,
121 struct drm_display_mode
*adjusted_mode
)
126 static void intel_crt_mode_set(struct drm_encoder
*encoder
,
127 struct drm_display_mode
*mode
,
128 struct drm_display_mode
*adjusted_mode
)
131 struct drm_device
*dev
= encoder
->dev
;
132 struct drm_crtc
*crtc
= encoder
->crtc
;
133 struct intel_crtc
*intel_crtc
= to_intel_crtc(crtc
);
134 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
139 if (intel_crtc
->pipe
== 0)
140 dpll_md_reg
= DPLL_A_MD
;
142 dpll_md_reg
= DPLL_B_MD
;
150 * Disable separate mode multiplier used when cloning SDVO to CRT
151 * XXX this needs to be adjusted when we really are cloning
153 if (IS_I965G(dev
) && !IS_IGDNG(dev
)) {
154 dpll_md
= I915_READ(dpll_md_reg
);
155 I915_WRITE(dpll_md_reg
,
156 dpll_md
& ~DPLL_MD_UDI_MULTIPLIER_MASK
);
160 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
161 adpa
|= ADPA_HSYNC_ACTIVE_HIGH
;
162 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
163 adpa
|= ADPA_VSYNC_ACTIVE_HIGH
;
165 if (intel_crtc
->pipe
== 0) {
166 adpa
|= ADPA_PIPE_A_SELECT
;
168 I915_WRITE(BCLRPAT_A
, 0);
170 adpa
|= ADPA_PIPE_B_SELECT
;
172 I915_WRITE(BCLRPAT_B
, 0);
175 I915_WRITE(adpa_reg
, adpa
);
178 static bool intel_igdng_crt_detect_hotplug(struct drm_connector
*connector
)
180 struct drm_device
*dev
= connector
->dev
;
181 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
185 adpa
= I915_READ(PCH_ADPA
);
187 adpa
&= ~ADPA_CRT_HOTPLUG_MASK
;
189 adpa
|= (ADPA_CRT_HOTPLUG_PERIOD_128
|
190 ADPA_CRT_HOTPLUG_WARMUP_10MS
|
191 ADPA_CRT_HOTPLUG_SAMPLE_4S
|
192 ADPA_CRT_HOTPLUG_VOLTAGE_50
| /* default */
193 ADPA_CRT_HOTPLUG_VOLREF_325MV
|
194 ADPA_CRT_HOTPLUG_ENABLE
|
195 ADPA_CRT_HOTPLUG_FORCE_TRIGGER
);
197 DRM_DEBUG("pch crt adpa 0x%x", adpa
);
198 I915_WRITE(PCH_ADPA
, adpa
);
200 while ((I915_READ(PCH_ADPA
) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER
) != 0)
203 /* Check the status to see if both blue and green are on now */
204 adpa
= I915_READ(PCH_ADPA
);
205 adpa
&= ADPA_CRT_HOTPLUG_MONITOR_MASK
;
206 if ((adpa
== ADPA_CRT_HOTPLUG_MONITOR_COLOR
) ||
207 (adpa
== ADPA_CRT_HOTPLUG_MONITOR_MONO
))
216 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
218 * Not for i915G/i915GM
220 * \return true if CRT is connected.
221 * \return false if CRT is disconnected.
223 static bool intel_crt_detect_hotplug(struct drm_connector
*connector
)
225 struct drm_device
*dev
= connector
->dev
;
226 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
231 return intel_igdng_crt_detect_hotplug(connector
);
234 * On 4 series desktop, CRT detect sequence need to be done twice
235 * to get a reliable result.
238 if (IS_G4X(dev
) && !IS_GM45(dev
))
242 hotplug_en
= I915_READ(PORT_HOTPLUG_EN
);
243 hotplug_en
&= CRT_FORCE_HOTPLUG_MASK
;
244 hotplug_en
|= CRT_HOTPLUG_FORCE_DETECT
;
247 hotplug_en
|= CRT_HOTPLUG_ACTIVATION_PERIOD_64
;
249 hotplug_en
|= CRT_HOTPLUG_VOLTAGE_COMPARE_50
;
251 for (i
= 0; i
< tries
; i
++) {
252 unsigned long timeout
;
253 /* turn on the FORCE_DETECT */
254 I915_WRITE(PORT_HOTPLUG_EN
, hotplug_en
);
255 timeout
= jiffies
+ msecs_to_jiffies(1000);
256 /* wait for FORCE_DETECT to go off */
258 if (!(I915_READ(PORT_HOTPLUG_EN
) &
259 CRT_HOTPLUG_FORCE_DETECT
))
262 } while (time_after(timeout
, jiffies
));
265 if ((I915_READ(PORT_HOTPLUG_STAT
) & CRT_HOTPLUG_MONITOR_MASK
) ==
266 CRT_HOTPLUG_MONITOR_COLOR
)
272 static bool intel_crt_detect_ddc(struct drm_connector
*connector
)
274 struct intel_output
*intel_output
= to_intel_output(connector
);
276 /* CRT should always be at 0, but check anyway */
277 if (intel_output
->type
!= INTEL_OUTPUT_ANALOG
)
280 return intel_ddc_probe(intel_output
);
283 static enum drm_connector_status
284 intel_crt_load_detect(struct drm_crtc
*crtc
, struct intel_output
*intel_output
)
286 struct drm_encoder
*encoder
= &intel_output
->enc
;
287 struct drm_device
*dev
= encoder
->dev
;
288 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
289 struct intel_crtc
*intel_crtc
= to_intel_crtc(crtc
);
290 uint32_t pipe
= intel_crtc
->pipe
;
291 uint32_t save_bclrpat
;
292 uint32_t save_vtotal
;
293 uint32_t vtotal
, vactive
;
295 uint32_t vblank
, vblank_start
, vblank_end
;
297 uint32_t bclrpat_reg
;
301 uint32_t pipeconf_reg
;
302 uint32_t pipe_dsl_reg
;
304 enum drm_connector_status status
;
307 bclrpat_reg
= BCLRPAT_A
;
308 vtotal_reg
= VTOTAL_A
;
309 vblank_reg
= VBLANK_A
;
311 pipeconf_reg
= PIPEACONF
;
312 pipe_dsl_reg
= PIPEADSL
;
314 bclrpat_reg
= BCLRPAT_B
;
315 vtotal_reg
= VTOTAL_B
;
316 vblank_reg
= VBLANK_B
;
318 pipeconf_reg
= PIPEBCONF
;
319 pipe_dsl_reg
= PIPEBDSL
;
322 save_bclrpat
= I915_READ(bclrpat_reg
);
323 save_vtotal
= I915_READ(vtotal_reg
);
324 vblank
= I915_READ(vblank_reg
);
326 vtotal
= ((save_vtotal
>> 16) & 0xfff) + 1;
327 vactive
= (save_vtotal
& 0x7ff) + 1;
329 vblank_start
= (vblank
& 0xfff) + 1;
330 vblank_end
= ((vblank
>> 16) & 0xfff) + 1;
332 /* Set the border color to purple. */
333 I915_WRITE(bclrpat_reg
, 0x500050);
336 uint32_t pipeconf
= I915_READ(pipeconf_reg
);
337 I915_WRITE(pipeconf_reg
, pipeconf
| PIPECONF_FORCE_BORDER
);
338 /* Wait for next Vblank to substitue
339 * border color for Color info */
340 intel_wait_for_vblank(dev
);
341 st00
= I915_READ8(VGA_MSR_WRITE
);
342 status
= ((st00
& (1 << 4)) != 0) ?
343 connector_status_connected
:
344 connector_status_disconnected
;
346 I915_WRITE(pipeconf_reg
, pipeconf
);
348 bool restore_vblank
= false;
352 * If there isn't any border, add some.
353 * Yes, this will flicker
355 if (vblank_start
<= vactive
&& vblank_end
>= vtotal
) {
356 uint32_t vsync
= I915_READ(vsync_reg
);
357 uint32_t vsync_start
= (vsync
& 0xffff) + 1;
359 vblank_start
= vsync_start
;
360 I915_WRITE(vblank_reg
,
362 ((vblank_end
- 1) << 16));
363 restore_vblank
= true;
365 /* sample in the vertical border, selecting the larger one */
366 if (vblank_start
- vactive
>= vtotal
- vblank_end
)
367 vsample
= (vblank_start
+ vactive
) >> 1;
369 vsample
= (vtotal
+ vblank_end
) >> 1;
372 * Wait for the border to be displayed
374 while (I915_READ(pipe_dsl_reg
) >= vactive
)
376 while ((dsl
= I915_READ(pipe_dsl_reg
)) <= vsample
)
379 * Watch ST00 for an entire scanline
385 /* Read the ST00 VGA status register */
386 st00
= I915_READ8(VGA_MSR_WRITE
);
389 } while ((I915_READ(pipe_dsl_reg
) == dsl
));
391 /* restore vblank if necessary */
393 I915_WRITE(vblank_reg
, vblank
);
395 * If more than 3/4 of the scanline detected a monitor,
396 * then it is assumed to be present. This works even on i830,
397 * where there isn't any way to force the border color across
400 status
= detect
* 4 > count
* 3 ?
401 connector_status_connected
:
402 connector_status_disconnected
;
405 /* Restore previous settings */
406 I915_WRITE(bclrpat_reg
, save_bclrpat
);
411 static enum drm_connector_status
intel_crt_detect(struct drm_connector
*connector
)
413 struct drm_device
*dev
= connector
->dev
;
414 struct intel_output
*intel_output
= to_intel_output(connector
);
415 struct drm_encoder
*encoder
= &intel_output
->enc
;
416 struct drm_crtc
*crtc
;
418 enum drm_connector_status status
;
420 if (IS_I9XX(dev
) && !IS_I915G(dev
) && !IS_I915GM(dev
)) {
421 if (intel_crt_detect_hotplug(connector
))
422 return connector_status_connected
;
424 return connector_status_disconnected
;
427 if (intel_crt_detect_ddc(connector
))
428 return connector_status_connected
;
430 /* for pre-945g platforms use load detect */
431 if (encoder
->crtc
&& encoder
->crtc
->enabled
) {
432 status
= intel_crt_load_detect(encoder
->crtc
, intel_output
);
434 crtc
= intel_get_load_detect_pipe(intel_output
,
437 status
= intel_crt_load_detect(crtc
, intel_output
);
438 intel_release_load_detect_pipe(intel_output
, dpms_mode
);
440 status
= connector_status_unknown
;
446 static void intel_crt_destroy(struct drm_connector
*connector
)
448 struct intel_output
*intel_output
= to_intel_output(connector
);
450 intel_i2c_destroy(intel_output
->ddc_bus
);
451 drm_sysfs_connector_remove(connector
);
452 drm_connector_cleanup(connector
);
456 static int intel_crt_get_modes(struct drm_connector
*connector
)
459 struct intel_output
*intel_output
= to_intel_output(connector
);
460 struct i2c_adapter
*ddcbus
;
461 struct drm_device
*dev
= connector
->dev
;
464 ret
= intel_ddc_get_modes(intel_output
);
465 if (ret
|| !IS_G4X(dev
))
468 ddcbus
= intel_output
->ddc_bus
;
469 /* Try to probe digital port for output in DVI-I -> VGA mode. */
470 intel_output
->ddc_bus
=
471 intel_i2c_create(connector
->dev
, GPIOD
, "CRTDDC_D");
473 if (!intel_output
->ddc_bus
) {
474 intel_output
->ddc_bus
= ddcbus
;
475 dev_printk(KERN_ERR
, &connector
->dev
->pdev
->dev
,
476 "DDC bus registration failed for CRTDDC_D.\n");
479 /* Try to get modes by GPIOD port */
480 ret
= intel_ddc_get_modes(intel_output
);
481 intel_i2c_destroy(ddcbus
);
488 static int intel_crt_set_property(struct drm_connector
*connector
,
489 struct drm_property
*property
,
496 * Routines for controlling stuff on the analog port
499 static const struct drm_encoder_helper_funcs intel_crt_helper_funcs
= {
500 .dpms
= intel_crt_dpms
,
501 .mode_fixup
= intel_crt_mode_fixup
,
502 .prepare
= intel_encoder_prepare
,
503 .commit
= intel_encoder_commit
,
504 .mode_set
= intel_crt_mode_set
,
507 static const struct drm_connector_funcs intel_crt_connector_funcs
= {
508 .dpms
= drm_helper_connector_dpms
,
509 .detect
= intel_crt_detect
,
510 .fill_modes
= drm_helper_probe_single_connector_modes
,
511 .destroy
= intel_crt_destroy
,
512 .set_property
= intel_crt_set_property
,
515 static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs
= {
516 .mode_valid
= intel_crt_mode_valid
,
517 .get_modes
= intel_crt_get_modes
,
518 .best_encoder
= intel_best_encoder
,
521 static void intel_crt_enc_destroy(struct drm_encoder
*encoder
)
523 drm_encoder_cleanup(encoder
);
526 static const struct drm_encoder_funcs intel_crt_enc_funcs
= {
527 .destroy
= intel_crt_enc_destroy
,
530 void intel_crt_init(struct drm_device
*dev
)
532 struct drm_connector
*connector
;
533 struct intel_output
*intel_output
;
534 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
537 intel_output
= kzalloc(sizeof(struct intel_output
), GFP_KERNEL
);
541 connector
= &intel_output
->base
;
542 drm_connector_init(dev
, &intel_output
->base
,
543 &intel_crt_connector_funcs
, DRM_MODE_CONNECTOR_VGA
);
545 drm_encoder_init(dev
, &intel_output
->enc
, &intel_crt_enc_funcs
,
546 DRM_MODE_ENCODER_DAC
);
548 drm_mode_connector_attach_encoder(&intel_output
->base
,
551 /* Set up the DDC bus. */
556 /* Use VBT information for CRT DDC if available */
557 if (dev_priv
->crt_ddc_bus
!= -1)
558 i2c_reg
= dev_priv
->crt_ddc_bus
;
560 intel_output
->ddc_bus
= intel_i2c_create(dev
, i2c_reg
, "CRTDDC_A");
561 if (!intel_output
->ddc_bus
) {
562 dev_printk(KERN_ERR
, &dev
->pdev
->dev
, "DDC bus registration "
567 intel_output
->type
= INTEL_OUTPUT_ANALOG
;
568 intel_output
->clone_mask
= (1 << INTEL_SDVO_NON_TV_CLONE_BIT
) |
569 (1 << INTEL_ANALOG_CLONE_BIT
) |
570 (1 << INTEL_SDVO_LVDS_CLONE_BIT
);
571 intel_output
->crtc_mask
= (1 << 0) | (1 << 1);
572 connector
->interlace_allowed
= 0;
573 connector
->doublescan_allowed
= 0;
575 drm_encoder_helper_add(&intel_output
->enc
, &intel_crt_helper_funcs
);
576 drm_connector_helper_add(connector
, &intel_crt_connector_helper_funcs
);
578 drm_sysfs_connector_add(connector
);