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 * Copyright 2006 Dave Airlie <airlied@linux.ie>
30 #include <linux/i2c.h>
34 #include "intel_drv.h"
39 #define SIL164_ADDR 0x38
40 #define CH7xxx_ADDR 0x76
41 #define TFP410_ADDR 0x38
43 extern struct intel_dvo_dev_ops sil164_ops;
44 extern struct intel_dvo_dev_ops ch7xxx_ops;
45 extern struct intel_dvo_dev_ops ivch_ops;
46 extern struct intel_dvo_dev_ops tfp410_ops;
47 extern struct intel_dvo_dev_ops ch7017_ops;
49 struct intel_dvo_device intel_dvo_devices[] = {
51 .type = INTEL_DVO_CHIP_TMDS,
54 .slave_addr = SIL164_ADDR,
55 .dev_ops = &sil164_ops,
58 .type = INTEL_DVO_CHIP_TMDS,
61 .slave_addr = CH7xxx_ADDR,
62 .dev_ops = &ch7xxx_ops,
65 .type = INTEL_DVO_CHIP_LVDS,
68 .slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
72 .type = INTEL_DVO_CHIP_TMDS,
75 .slave_addr = TFP410_ADDR,
76 .dev_ops = &tfp410_ops,
79 .type = INTEL_DVO_CHIP_LVDS,
84 .dev_ops = &ch7017_ops,
88 static void intel_dvo_dpms(struct drm_encoder *encoder, int mode)
90 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
91 struct intel_output *intel_output = enc_to_intel_output(encoder);
92 struct intel_dvo_device *dvo = intel_output->dev_priv;
93 u32 dvo_reg = dvo->dvo_reg;
94 u32 temp = I915_READ(dvo_reg);
96 if (mode == DPMSModeOn) {
97 I915_WRITE(dvo_reg, temp | DVO_ENABLE);
99 dvo->dev_ops->dpms(dvo, mode);
101 dvo->dev_ops->dpms(dvo, mode);
102 I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
107 static void intel_dvo_save(struct drm_connector *connector)
109 struct drm_i915_private *dev_priv = connector->dev->dev_private;
110 struct intel_output *intel_output = to_intel_output(connector);
111 struct intel_dvo_device *dvo = intel_output->dev_priv;
113 /* Each output should probably just save the registers it touches,
114 * but for now, use more overkill.
116 dev_priv->saveDVOA = I915_READ(DVOA);
117 dev_priv->saveDVOB = I915_READ(DVOB);
118 dev_priv->saveDVOC = I915_READ(DVOC);
120 dvo->dev_ops->save(dvo);
123 static void intel_dvo_restore(struct drm_connector *connector)
125 struct drm_i915_private *dev_priv = connector->dev->dev_private;
126 struct intel_output *intel_output = to_intel_output(connector);
127 struct intel_dvo_device *dvo = intel_output->dev_priv;
129 dvo->dev_ops->restore(dvo);
131 I915_WRITE(DVOA, dev_priv->saveDVOA);
132 I915_WRITE(DVOB, dev_priv->saveDVOB);
133 I915_WRITE(DVOC, dev_priv->saveDVOC);
136 static int intel_dvo_mode_valid(struct drm_connector *connector,
137 struct drm_display_mode *mode)
139 struct intel_output *intel_output = to_intel_output(connector);
140 struct intel_dvo_device *dvo = intel_output->dev_priv;
142 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
143 return MODE_NO_DBLESCAN;
145 /* XXX: Validate clock range */
147 if (dvo->panel_fixed_mode) {
148 if (mode->hdisplay > dvo->panel_fixed_mode->hdisplay)
150 if (mode->vdisplay > dvo->panel_fixed_mode->vdisplay)
154 return dvo->dev_ops->mode_valid(dvo, mode);
157 static bool intel_dvo_mode_fixup(struct drm_encoder *encoder,
158 struct drm_display_mode *mode,
159 struct drm_display_mode *adjusted_mode)
161 struct intel_output *intel_output = enc_to_intel_output(encoder);
162 struct intel_dvo_device *dvo = intel_output->dev_priv;
164 /* If we have timings from the BIOS for the panel, put them in
165 * to the adjusted mode. The CRTC will be set up for this mode,
166 * with the panel scaling set up to source from the H/VDisplay
167 * of the original mode.
169 if (dvo->panel_fixed_mode != NULL) {
170 #define C(x) adjusted_mode->x = dvo->panel_fixed_mode->x
180 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
184 if (dvo->dev_ops->mode_fixup)
185 return dvo->dev_ops->mode_fixup(dvo, mode, adjusted_mode);
190 static void intel_dvo_mode_set(struct drm_encoder *encoder,
191 struct drm_display_mode *mode,
192 struct drm_display_mode *adjusted_mode)
194 struct drm_device *dev = encoder->dev;
195 struct drm_i915_private *dev_priv = dev->dev_private;
196 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
197 struct intel_output *intel_output = enc_to_intel_output(encoder);
198 struct intel_dvo_device *dvo = intel_output->dev_priv;
199 int pipe = intel_crtc->pipe;
201 u32 dvo_reg = dvo->dvo_reg, dvo_srcdim_reg;
202 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
207 dvo_srcdim_reg = DVOA_SRCDIM;
210 dvo_srcdim_reg = DVOB_SRCDIM;
213 dvo_srcdim_reg = DVOC_SRCDIM;
217 dvo->dev_ops->mode_set(dvo, mode, adjusted_mode);
219 /* Save the data order, since I don't know what it should be set to. */
220 dvo_val = I915_READ(dvo_reg) &
221 (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
222 dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
223 DVO_BLANK_ACTIVE_HIGH;
226 dvo_val |= DVO_PIPE_B_SELECT;
227 dvo_val |= DVO_PIPE_STALL;
228 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
229 dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
230 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
231 dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
233 I915_WRITE(dpll_reg, I915_READ(dpll_reg) | DPLL_DVO_HIGH_SPEED);
235 /*I915_WRITE(DVOB_SRCDIM,
236 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
237 (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
238 I915_WRITE(dvo_srcdim_reg,
239 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
240 (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
241 /*I915_WRITE(DVOB, dvo_val);*/
242 I915_WRITE(dvo_reg, dvo_val);
246 * Detect the output connection on our DVO device.
250 static enum drm_connector_status intel_dvo_detect(struct drm_connector *connector)
252 struct intel_output *intel_output = to_intel_output(connector);
253 struct intel_dvo_device *dvo = intel_output->dev_priv;
255 return dvo->dev_ops->detect(dvo);
258 static int intel_dvo_get_modes(struct drm_connector *connector)
260 struct intel_output *intel_output = to_intel_output(connector);
261 struct intel_dvo_device *dvo = intel_output->dev_priv;
263 /* We should probably have an i2c driver get_modes function for those
264 * devices which will have a fixed set of modes determined by the chip
265 * (TV-out, for example), but for now with just TMDS and LVDS,
266 * that's not the case.
268 intel_ddc_get_modes(intel_output);
269 if (!list_empty(&connector->probed_modes))
273 if (intel_output->i2c_drv->vid_rec->get_modes)
275 modes = intel_output->i2c_drv->vid_rec->get_modes (intel_output->i2c_drv->dev_priv);
281 if (dvo->panel_fixed_mode != NULL) {
282 struct drm_display_mode *mode;
283 mode = drm_mode_duplicate(connector->dev, dvo->panel_fixed_mode);
285 drm_mode_probed_add(connector, mode);
292 static void intel_dvo_destroy (struct drm_connector *connector)
294 struct intel_output *intel_output = to_intel_output(connector);
295 struct intel_dvo_device *dvo = intel_output->dev_priv;
298 if (dvo->dev_ops->destroy)
299 dvo->dev_ops->destroy(dvo);
300 if (dvo->panel_fixed_mode)
301 kfree(dvo->panel_fixed_mode);
302 /* no need, in i830_dvoices[] now */
305 if (intel_output->i2c_bus)
306 intel_i2c_destroy(intel_output->i2c_bus);
307 if (intel_output->ddc_bus)
308 intel_i2c_destroy(intel_output->ddc_bus);
309 drm_sysfs_connector_remove(connector);
310 drm_connector_cleanup(connector);
314 #ifdef RANDR_GET_CRTC_INTERFACE
315 static struct drm_crtc *intel_dvo_get_crtc(struct drm_connector *connector)
317 struct drm_device *dev = connector->dev;
318 struct drm_i915_private *dev_priv = dev->dev_private;
319 struct intel_output *intel_output = to_intel_output(connector);
320 struct intel_dvo_device *dvo = intel_output->dev_priv;
321 int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT);
323 return intel_pipe_to_crtc(pScrn, pipe);
327 static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
328 .dpms = intel_dvo_dpms,
329 .mode_fixup = intel_dvo_mode_fixup,
330 .prepare = intel_encoder_prepare,
331 .mode_set = intel_dvo_mode_set,
332 .commit = intel_encoder_commit,
335 static const struct drm_connector_funcs intel_dvo_connector_funcs = {
336 .save = intel_dvo_save,
337 .restore = intel_dvo_restore,
338 .detect = intel_dvo_detect,
339 .destroy = intel_dvo_destroy,
340 .fill_modes = drm_helper_probe_single_connector_modes,
343 static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
344 .mode_valid = intel_dvo_mode_valid,
345 .get_modes = intel_dvo_get_modes,
346 .best_encoder = intel_best_encoder,
349 void intel_dvo_enc_destroy(struct drm_encoder *encoder)
351 drm_encoder_cleanup(encoder);
354 static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
355 .destroy = intel_dvo_enc_destroy,
360 * Attempts to get a fixed panel timing for LVDS (currently only the i830).
362 * Other chips with DVO LVDS will need to extend this to deal with the LVDS
363 * chip being on DVOB/C and having multiple pipes.
365 static struct drm_display_mode *
366 intel_dvo_get_current_mode (struct drm_connector *connector)
368 struct drm_device *dev = connector->dev;
369 struct drm_i915_private *dev_priv = dev->dev_private;
370 struct intel_output *intel_output = to_intel_output(connector);
371 struct intel_dvo_device *dvo = intel_output->dev_priv;
372 uint32_t dvo_reg = dvo->dvo_reg;
373 uint32_t dvo_val = I915_READ(dvo_reg);
374 struct drm_display_mode *mode = NULL;
376 /* If the DVO port is active, that'll be the LVDS, so we can pull out
377 * its timings to get how the BIOS set up the panel.
379 if (dvo_val & DVO_ENABLE) {
380 struct drm_crtc *crtc;
381 int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
383 crtc = intel_get_crtc_from_pipe(dev, pipe);
385 mode = intel_crtc_mode_get(dev, crtc);
388 mode->type |= DRM_MODE_TYPE_PREFERRED;
389 if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
390 mode->flags |= DRM_MODE_FLAG_PHSYNC;
391 if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
392 mode->flags |= DRM_MODE_FLAG_PVSYNC;
399 void intel_dvo_init(struct drm_device *dev)
401 struct intel_output *intel_output;
402 struct intel_dvo_device *dvo;
403 struct intel_i2c_chan *i2cbus = NULL;
407 int encoder_type = DRM_MODE_ENCODER_NONE;
408 intel_output = kzalloc (sizeof(struct intel_output), GFP_KERNEL);
412 /* Set up the DDC bus */
413 intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
414 if (!intel_output->ddc_bus)
417 /* Now, try to find a controller */
418 for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
419 struct drm_connector *connector = &intel_output->base;
422 dvo = &intel_dvo_devices[i];
424 /* Allow the I2C driver info to specify the GPIO to be used in
425 * special cases, but otherwise default to what's defined
430 else if (dvo->type == INTEL_DVO_CHIP_LVDS)
435 /* Set up the I2C bus necessary for the chip we're probing.
436 * It appears that everything is on GPIOE except for panels
437 * on i830 laptops, which are on GPIOB (DVOA).
439 if (gpio_inited != gpio) {
441 intel_i2c_destroy(i2cbus);
442 if (!(i2cbus = intel_i2c_create(dev, gpio,
443 gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
449 if (dvo->dev_ops!= NULL)
450 ret = dvo->dev_ops->init(dvo, i2cbus);
457 intel_output->type = INTEL_OUTPUT_DVO;
459 case INTEL_DVO_CHIP_TMDS:
460 // connector = DRM_MODE_CONNECTOR_DVID;
461 drm_connector_init(dev, connector, &intel_dvo_connector_funcs,
462 DRM_MODE_CONNECTOR_DVII);
463 encoder_type = DRM_MODE_ENCODER_TMDS;
465 case INTEL_DVO_CHIP_LVDS:
466 // connector = DRM_MODE_CONNECTOR_LVDS;
467 drm_connector_init(dev, connector, &intel_dvo_connector_funcs,
468 DRM_MODE_CONNECTOR_LVDS);
469 encoder_type = DRM_MODE_ENCODER_LVDS;
473 drm_connector_helper_add(connector, &intel_dvo_connector_helper_funcs);
474 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
475 connector->interlace_allowed = false;
476 connector->doublescan_allowed = false;
478 intel_output->dev_priv = dvo;
479 intel_output->i2c_bus = i2cbus;
481 drm_encoder_init(dev, &intel_output->enc, &intel_dvo_enc_funcs, encoder_type);
482 drm_encoder_helper_add(&intel_output->enc, &intel_dvo_helper_funcs);
484 drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
485 if (dvo->type == INTEL_DVO_CHIP_LVDS) {
486 /* For our LVDS chipsets, we should hopefully be able
487 * to dig the fixed panel mode out of the BIOS data.
488 * However, it's in a different format from the BIOS
489 * data on chipsets with integrated LVDS (stored in AIM
490 * headers, likely), so for now, just get the current
491 * mode being output through DVO.
493 dvo->panel_fixed_mode = intel_dvo_get_current_mode(connector);
494 dvo->panel_wants_dither = true;
497 drm_sysfs_connector_add(connector);
501 intel_i2c_destroy(intel_output->ddc_bus);
502 /* Didn't find a chip, so tear down. */
504 intel_i2c_destroy(i2cbus);