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;
42 temp = I915_READ(ADPA);
43 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
44 temp |= ADPA_DAC_ENABLE;
47 case DRM_MODE_DPMS_ON:
48 temp |= ADPA_DAC_ENABLE;
50 case DRM_MODE_DPMS_STANDBY:
51 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
53 case DRM_MODE_DPMS_SUSPEND:
54 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
56 case DRM_MODE_DPMS_OFF:
57 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
61 I915_WRITE(ADPA, temp);
64 static int intel_crt_mode_valid(struct drm_connector *connector,
65 struct drm_display_mode *mode)
67 struct drm_device *dev = connector->dev;
70 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
71 return MODE_NO_DBLESCAN;
73 if (mode->clock < 25000)
74 return MODE_CLOCK_LOW;
80 if (mode->clock > max_clock)
81 return MODE_CLOCK_HIGH;
86 static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
87 struct drm_display_mode *mode,
88 struct drm_display_mode *adjusted_mode)
93 static void intel_crt_mode_set(struct drm_encoder *encoder,
94 struct drm_display_mode *mode,
95 struct drm_display_mode *adjusted_mode)
98 struct drm_device *dev = encoder->dev;
99 struct drm_crtc *crtc = encoder->crtc;
100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
101 struct drm_i915_private *dev_priv = dev->dev_private;
105 if (intel_crtc->pipe == 0)
106 dpll_md_reg = DPLL_A_MD;
108 dpll_md_reg = DPLL_B_MD;
111 * Disable separate mode multiplier used when cloning SDVO to CRT
112 * XXX this needs to be adjusted when we really are cloning
115 dpll_md = I915_READ(dpll_md_reg);
116 I915_WRITE(dpll_md_reg,
117 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
121 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
122 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
123 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
124 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
126 if (intel_crtc->pipe == 0) {
127 adpa |= ADPA_PIPE_A_SELECT;
128 I915_WRITE(BCLRPAT_A, 0);
130 adpa |= ADPA_PIPE_B_SELECT;
131 I915_WRITE(BCLRPAT_B, 0);
134 I915_WRITE(ADPA, adpa);
138 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
140 * Not for i915G/i915GM
142 * \return true if CRT is connected.
143 * \return false if CRT is disconnected.
145 static bool intel_crt_detect_hotplug(struct drm_connector *connector)
147 struct drm_device *dev = connector->dev;
148 struct drm_i915_private *dev_priv = dev->dev_private;
152 * On 4 series desktop, CRT detect sequence need to be done twice
153 * to get a reliable result.
156 if (IS_G4X(dev) && !IS_GM45(dev))
160 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
161 hotplug_en &= CRT_FORCE_HOTPLUG_MASK;
162 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
165 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
167 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
169 for (i = 0; i < tries ; i++) {
170 unsigned long timeout;
171 /* turn on the FORCE_DETECT */
172 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
173 timeout = jiffies + msecs_to_jiffies(1000);
174 /* wait for FORCE_DETECT to go off */
176 if (!(I915_READ(PORT_HOTPLUG_EN) &
177 CRT_HOTPLUG_FORCE_DETECT))
180 } while (time_after(timeout, jiffies));
183 if ((I915_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) ==
184 CRT_HOTPLUG_MONITOR_COLOR)
190 static bool intel_crt_detect_ddc(struct drm_connector *connector)
192 struct intel_output *intel_output = to_intel_output(connector);
194 /* CRT should always be at 0, but check anyway */
195 if (intel_output->type != INTEL_OUTPUT_ANALOG)
198 return intel_ddc_probe(intel_output);
201 static enum drm_connector_status
202 intel_crt_load_detect(struct drm_crtc *crtc, struct intel_output *intel_output)
204 struct drm_encoder *encoder = &intel_output->enc;
205 struct drm_device *dev = encoder->dev;
206 struct drm_i915_private *dev_priv = dev->dev_private;
207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
208 uint32_t pipe = intel_crtc->pipe;
209 uint32_t save_bclrpat;
210 uint32_t save_vtotal;
211 uint32_t vtotal, vactive;
213 uint32_t vblank, vblank_start, vblank_end;
215 uint32_t bclrpat_reg;
219 uint32_t pipeconf_reg;
220 uint32_t pipe_dsl_reg;
222 enum drm_connector_status status;
225 bclrpat_reg = BCLRPAT_A;
226 vtotal_reg = VTOTAL_A;
227 vblank_reg = VBLANK_A;
229 pipeconf_reg = PIPEACONF;
230 pipe_dsl_reg = PIPEADSL;
232 bclrpat_reg = BCLRPAT_B;
233 vtotal_reg = VTOTAL_B;
234 vblank_reg = VBLANK_B;
236 pipeconf_reg = PIPEBCONF;
237 pipe_dsl_reg = PIPEBDSL;
240 save_bclrpat = I915_READ(bclrpat_reg);
241 save_vtotal = I915_READ(vtotal_reg);
242 vblank = I915_READ(vblank_reg);
244 vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
245 vactive = (save_vtotal & 0x7ff) + 1;
247 vblank_start = (vblank & 0xfff) + 1;
248 vblank_end = ((vblank >> 16) & 0xfff) + 1;
250 /* Set the border color to purple. */
251 I915_WRITE(bclrpat_reg, 0x500050);
254 uint32_t pipeconf = I915_READ(pipeconf_reg);
255 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
256 /* Wait for next Vblank to substitue
257 * border color for Color info */
258 intel_wait_for_vblank(dev);
259 st00 = I915_READ8(VGA_MSR_WRITE);
260 status = ((st00 & (1 << 4)) != 0) ?
261 connector_status_connected :
262 connector_status_disconnected;
264 I915_WRITE(pipeconf_reg, pipeconf);
266 bool restore_vblank = false;
270 * If there isn't any border, add some.
271 * Yes, this will flicker
273 if (vblank_start <= vactive && vblank_end >= vtotal) {
274 uint32_t vsync = I915_READ(vsync_reg);
275 uint32_t vsync_start = (vsync & 0xffff) + 1;
277 vblank_start = vsync_start;
278 I915_WRITE(vblank_reg,
280 ((vblank_end - 1) << 16));
281 restore_vblank = true;
283 /* sample in the vertical border, selecting the larger one */
284 if (vblank_start - vactive >= vtotal - vblank_end)
285 vsample = (vblank_start + vactive) >> 1;
287 vsample = (vtotal + vblank_end) >> 1;
290 * Wait for the border to be displayed
292 while (I915_READ(pipe_dsl_reg) >= vactive)
294 while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
297 * Watch ST00 for an entire scanline
303 /* Read the ST00 VGA status register */
304 st00 = I915_READ8(VGA_MSR_WRITE);
307 } while ((I915_READ(pipe_dsl_reg) == dsl));
309 /* restore vblank if necessary */
311 I915_WRITE(vblank_reg, vblank);
313 * If more than 3/4 of the scanline detected a monitor,
314 * then it is assumed to be present. This works even on i830,
315 * where there isn't any way to force the border color across
318 status = detect * 4 > count * 3 ?
319 connector_status_connected :
320 connector_status_disconnected;
323 /* Restore previous settings */
324 I915_WRITE(bclrpat_reg, save_bclrpat);
329 static enum drm_connector_status intel_crt_detect(struct drm_connector *connector)
331 struct drm_device *dev = connector->dev;
332 struct intel_output *intel_output = to_intel_output(connector);
333 struct drm_encoder *encoder = &intel_output->enc;
334 struct drm_crtc *crtc;
336 enum drm_connector_status status;
338 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
339 if (intel_crt_detect_hotplug(connector))
340 return connector_status_connected;
342 return connector_status_disconnected;
345 if (intel_crt_detect_ddc(connector))
346 return connector_status_connected;
348 /* for pre-945g platforms use load detect */
349 if (encoder->crtc && encoder->crtc->enabled) {
350 status = intel_crt_load_detect(encoder->crtc, intel_output);
352 crtc = intel_get_load_detect_pipe(intel_output,
355 status = intel_crt_load_detect(crtc, intel_output);
356 intel_release_load_detect_pipe(intel_output, dpms_mode);
358 status = connector_status_unknown;
364 static void intel_crt_destroy(struct drm_connector *connector)
366 struct intel_output *intel_output = to_intel_output(connector);
368 intel_i2c_destroy(intel_output->ddc_bus);
369 drm_sysfs_connector_remove(connector);
370 drm_connector_cleanup(connector);
374 static int intel_crt_get_modes(struct drm_connector *connector)
376 struct intel_output *intel_output = to_intel_output(connector);
377 return intel_ddc_get_modes(intel_output);
380 static int intel_crt_set_property(struct drm_connector *connector,
381 struct drm_property *property,
384 struct drm_device *dev = connector->dev;
386 if (property == dev->mode_config.dpms_property && connector->encoder)
387 intel_crt_dpms(connector->encoder, (uint32_t)(value & 0xf));
393 * Routines for controlling stuff on the analog port
396 static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
397 .dpms = intel_crt_dpms,
398 .mode_fixup = intel_crt_mode_fixup,
399 .prepare = intel_encoder_prepare,
400 .commit = intel_encoder_commit,
401 .mode_set = intel_crt_mode_set,
404 static const struct drm_connector_funcs intel_crt_connector_funcs = {
405 .detect = intel_crt_detect,
406 .fill_modes = drm_helper_probe_single_connector_modes,
407 .destroy = intel_crt_destroy,
408 .set_property = intel_crt_set_property,
411 static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
412 .mode_valid = intel_crt_mode_valid,
413 .get_modes = intel_crt_get_modes,
414 .best_encoder = intel_best_encoder,
417 static void intel_crt_enc_destroy(struct drm_encoder *encoder)
419 drm_encoder_cleanup(encoder);
422 static const struct drm_encoder_funcs intel_crt_enc_funcs = {
423 .destroy = intel_crt_enc_destroy,
426 void intel_crt_init(struct drm_device *dev)
428 struct drm_connector *connector;
429 struct intel_output *intel_output;
431 intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
435 connector = &intel_output->base;
436 drm_connector_init(dev, &intel_output->base,
437 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
439 drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
440 DRM_MODE_ENCODER_DAC);
442 drm_mode_connector_attach_encoder(&intel_output->base,
445 /* Set up the DDC bus. */
446 intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
447 if (!intel_output->ddc_bus) {
448 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
453 intel_output->type = INTEL_OUTPUT_ANALOG;
454 connector->interlace_allowed = 0;
455 connector->doublescan_allowed = 0;
457 drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
458 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
460 drm_sysfs_connector_add(connector);