2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2007 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Eric Anholt <eric@anholt.net>
27 #include <linux/i2c.h>
28 #include <linux/slab.h>
32 #include "intel_drv.h"
37 #define SIL164_ADDR 0x38
38 #define CH7xxx_ADDR 0x76
39 #define TFP410_ADDR 0x38
41 static struct intel_dvo_device intel_dvo_devices[] = {
43 .type = INTEL_DVO_CHIP_TMDS,
46 .slave_addr = SIL164_ADDR,
47 .dev_ops = &sil164_ops,
50 .type = INTEL_DVO_CHIP_TMDS,
53 .slave_addr = CH7xxx_ADDR,
54 .dev_ops = &ch7xxx_ops,
57 .type = INTEL_DVO_CHIP_LVDS,
60 .slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
64 .type = INTEL_DVO_CHIP_TMDS,
67 .slave_addr = TFP410_ADDR,
68 .dev_ops = &tfp410_ops,
71 .type = INTEL_DVO_CHIP_LVDS,
76 .dev_ops = &ch7017_ops,
80 static void intel_dvo_dpms(struct drm_encoder *encoder, int mode)
82 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
83 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
84 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
85 u32 dvo_reg = dvo->dvo_reg;
86 u32 temp = I915_READ(dvo_reg);
88 if (mode == DRM_MODE_DPMS_ON) {
89 I915_WRITE(dvo_reg, temp | DVO_ENABLE);
91 dvo->dev_ops->dpms(dvo, mode);
93 dvo->dev_ops->dpms(dvo, mode);
94 I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
99 static void intel_dvo_save(struct drm_connector *connector)
101 struct drm_i915_private *dev_priv = connector->dev->dev_private;
102 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
103 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
105 /* Each output should probably just save the registers it touches,
106 * but for now, use more overkill.
108 dev_priv->saveDVOA = I915_READ(DVOA);
109 dev_priv->saveDVOB = I915_READ(DVOB);
110 dev_priv->saveDVOC = I915_READ(DVOC);
112 dvo->dev_ops->save(dvo);
115 static void intel_dvo_restore(struct drm_connector *connector)
117 struct drm_i915_private *dev_priv = connector->dev->dev_private;
118 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
119 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
121 dvo->dev_ops->restore(dvo);
123 I915_WRITE(DVOA, dev_priv->saveDVOA);
124 I915_WRITE(DVOB, dev_priv->saveDVOB);
125 I915_WRITE(DVOC, dev_priv->saveDVOC);
128 static int intel_dvo_mode_valid(struct drm_connector *connector,
129 struct drm_display_mode *mode)
131 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
132 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
134 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
135 return MODE_NO_DBLESCAN;
137 /* XXX: Validate clock range */
139 if (dvo->panel_fixed_mode) {
140 if (mode->hdisplay > dvo->panel_fixed_mode->hdisplay)
142 if (mode->vdisplay > dvo->panel_fixed_mode->vdisplay)
146 return dvo->dev_ops->mode_valid(dvo, mode);
149 static bool intel_dvo_mode_fixup(struct drm_encoder *encoder,
150 struct drm_display_mode *mode,
151 struct drm_display_mode *adjusted_mode)
153 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
154 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
156 /* If we have timings from the BIOS for the panel, put them in
157 * to the adjusted mode. The CRTC will be set up for this mode,
158 * with the panel scaling set up to source from the H/VDisplay
159 * of the original mode.
161 if (dvo->panel_fixed_mode != NULL) {
162 #define C(x) adjusted_mode->x = dvo->panel_fixed_mode->x
172 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
176 if (dvo->dev_ops->mode_fixup)
177 return dvo->dev_ops->mode_fixup(dvo, mode, adjusted_mode);
182 static void intel_dvo_mode_set(struct drm_encoder *encoder,
183 struct drm_display_mode *mode,
184 struct drm_display_mode *adjusted_mode)
186 struct drm_device *dev = encoder->dev;
187 struct drm_i915_private *dev_priv = dev->dev_private;
188 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
189 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
190 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
191 int pipe = intel_crtc->pipe;
193 u32 dvo_reg = dvo->dvo_reg, dvo_srcdim_reg;
194 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
199 dvo_srcdim_reg = DVOA_SRCDIM;
202 dvo_srcdim_reg = DVOB_SRCDIM;
205 dvo_srcdim_reg = DVOC_SRCDIM;
209 dvo->dev_ops->mode_set(dvo, mode, adjusted_mode);
211 /* Save the data order, since I don't know what it should be set to. */
212 dvo_val = I915_READ(dvo_reg) &
213 (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
214 dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
215 DVO_BLANK_ACTIVE_HIGH;
218 dvo_val |= DVO_PIPE_B_SELECT;
219 dvo_val |= DVO_PIPE_STALL;
220 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
221 dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
222 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
223 dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
225 I915_WRITE(dpll_reg, I915_READ(dpll_reg) | DPLL_DVO_HIGH_SPEED);
227 /*I915_WRITE(DVOB_SRCDIM,
228 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
229 (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
230 I915_WRITE(dvo_srcdim_reg,
231 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
232 (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
233 /*I915_WRITE(DVOB, dvo_val);*/
234 I915_WRITE(dvo_reg, dvo_val);
238 * Detect the output connection on our DVO device.
242 static enum drm_connector_status intel_dvo_detect(struct drm_connector *connector)
244 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
245 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
247 return dvo->dev_ops->detect(dvo);
250 static int intel_dvo_get_modes(struct drm_connector *connector)
252 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
253 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
255 /* We should probably have an i2c driver get_modes function for those
256 * devices which will have a fixed set of modes determined by the chip
257 * (TV-out, for example), but for now with just TMDS and LVDS,
258 * that's not the case.
260 intel_ddc_get_modes(intel_encoder);
261 if (!list_empty(&connector->probed_modes))
265 if (dvo->panel_fixed_mode != NULL) {
266 struct drm_display_mode *mode;
267 mode = drm_mode_duplicate(connector->dev, dvo->panel_fixed_mode);
269 drm_mode_probed_add(connector, mode);
276 static void intel_dvo_destroy (struct drm_connector *connector)
278 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
279 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
282 if (dvo->dev_ops->destroy)
283 dvo->dev_ops->destroy(dvo);
284 if (dvo->panel_fixed_mode)
285 kfree(dvo->panel_fixed_mode);
286 /* no need, in i830_dvoices[] now */
289 if (intel_encoder->i2c_bus)
290 intel_i2c_destroy(intel_encoder->i2c_bus);
291 if (intel_encoder->ddc_bus)
292 intel_i2c_destroy(intel_encoder->ddc_bus);
293 drm_sysfs_connector_remove(connector);
294 drm_connector_cleanup(connector);
295 kfree(intel_encoder);
298 #ifdef RANDR_GET_CRTC_INTERFACE
299 static struct drm_crtc *intel_dvo_get_crtc(struct drm_connector *connector)
301 struct drm_device *dev = connector->dev;
302 struct drm_i915_private *dev_priv = dev->dev_private;
303 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
304 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
305 int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT);
307 return intel_pipe_to_crtc(pScrn, pipe);
311 static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
312 .dpms = intel_dvo_dpms,
313 .mode_fixup = intel_dvo_mode_fixup,
314 .prepare = intel_encoder_prepare,
315 .mode_set = intel_dvo_mode_set,
316 .commit = intel_encoder_commit,
319 static const struct drm_connector_funcs intel_dvo_connector_funcs = {
320 .dpms = drm_helper_connector_dpms,
321 .save = intel_dvo_save,
322 .restore = intel_dvo_restore,
323 .detect = intel_dvo_detect,
324 .destroy = intel_dvo_destroy,
325 .fill_modes = drm_helper_probe_single_connector_modes,
328 static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
329 .mode_valid = intel_dvo_mode_valid,
330 .get_modes = intel_dvo_get_modes,
331 .best_encoder = intel_best_encoder,
334 static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
336 drm_encoder_cleanup(encoder);
339 static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
340 .destroy = intel_dvo_enc_destroy,
345 * Attempts to get a fixed panel timing for LVDS (currently only the i830).
347 * Other chips with DVO LVDS will need to extend this to deal with the LVDS
348 * chip being on DVOB/C and having multiple pipes.
350 static struct drm_display_mode *
351 intel_dvo_get_current_mode (struct drm_connector *connector)
353 struct drm_device *dev = connector->dev;
354 struct drm_i915_private *dev_priv = dev->dev_private;
355 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
356 struct intel_dvo_device *dvo = intel_encoder->dev_priv;
357 uint32_t dvo_reg = dvo->dvo_reg;
358 uint32_t dvo_val = I915_READ(dvo_reg);
359 struct drm_display_mode *mode = NULL;
361 /* If the DVO port is active, that'll be the LVDS, so we can pull out
362 * its timings to get how the BIOS set up the panel.
364 if (dvo_val & DVO_ENABLE) {
365 struct drm_crtc *crtc;
366 int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
368 crtc = intel_get_crtc_from_pipe(dev, pipe);
370 mode = intel_crtc_mode_get(dev, crtc);
373 mode->type |= DRM_MODE_TYPE_PREFERRED;
374 if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
375 mode->flags |= DRM_MODE_FLAG_PHSYNC;
376 if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
377 mode->flags |= DRM_MODE_FLAG_PVSYNC;
384 void intel_dvo_init(struct drm_device *dev)
386 struct intel_encoder *intel_encoder;
387 struct intel_dvo_device *dvo;
388 struct i2c_adapter *i2cbus = NULL;
391 int encoder_type = DRM_MODE_ENCODER_NONE;
392 intel_encoder = kzalloc (sizeof(struct intel_encoder), GFP_KERNEL);
396 /* Set up the DDC bus */
397 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
398 if (!intel_encoder->ddc_bus)
401 /* Now, try to find a controller */
402 for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
403 struct drm_connector *connector = &intel_encoder->base;
406 dvo = &intel_dvo_devices[i];
408 /* Allow the I2C driver info to specify the GPIO to be used in
409 * special cases, but otherwise default to what's defined
414 else if (dvo->type == INTEL_DVO_CHIP_LVDS)
419 /* Set up the I2C bus necessary for the chip we're probing.
420 * It appears that everything is on GPIOE except for panels
421 * on i830 laptops, which are on GPIOB (DVOA).
424 intel_i2c_destroy(i2cbus);
425 if (!(i2cbus = intel_i2c_create(dev, gpio,
426 gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
430 if (dvo->dev_ops!= NULL)
431 ret = dvo->dev_ops->init(dvo, i2cbus);
438 intel_encoder->type = INTEL_OUTPUT_DVO;
439 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
441 case INTEL_DVO_CHIP_TMDS:
442 intel_encoder->clone_mask =
443 (1 << INTEL_DVO_TMDS_CLONE_BIT) |
444 (1 << INTEL_ANALOG_CLONE_BIT);
445 drm_connector_init(dev, connector,
446 &intel_dvo_connector_funcs,
447 DRM_MODE_CONNECTOR_DVII);
448 encoder_type = DRM_MODE_ENCODER_TMDS;
450 case INTEL_DVO_CHIP_LVDS:
451 intel_encoder->clone_mask =
452 (1 << INTEL_DVO_LVDS_CLONE_BIT);
453 drm_connector_init(dev, connector,
454 &intel_dvo_connector_funcs,
455 DRM_MODE_CONNECTOR_LVDS);
456 encoder_type = DRM_MODE_ENCODER_LVDS;
460 drm_connector_helper_add(connector,
461 &intel_dvo_connector_helper_funcs);
462 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
463 connector->interlace_allowed = false;
464 connector->doublescan_allowed = false;
466 intel_encoder->dev_priv = dvo;
467 intel_encoder->i2c_bus = i2cbus;
469 drm_encoder_init(dev, &intel_encoder->enc,
470 &intel_dvo_enc_funcs, encoder_type);
471 drm_encoder_helper_add(&intel_encoder->enc,
472 &intel_dvo_helper_funcs);
474 drm_mode_connector_attach_encoder(&intel_encoder->base,
475 &intel_encoder->enc);
476 if (dvo->type == INTEL_DVO_CHIP_LVDS) {
477 /* For our LVDS chipsets, we should hopefully be able
478 * to dig the fixed panel mode out of the BIOS data.
479 * However, it's in a different format from the BIOS
480 * data on chipsets with integrated LVDS (stored in AIM
481 * headers, likely), so for now, just get the current
482 * mode being output through DVO.
484 dvo->panel_fixed_mode =
485 intel_dvo_get_current_mode(connector);
486 dvo->panel_wants_dither = true;
489 drm_sysfs_connector_add(connector);
493 intel_i2c_destroy(intel_encoder->ddc_bus);
494 /* Didn't find a chip, so tear down. */
496 intel_i2c_destroy(i2cbus);
498 kfree(intel_encoder);