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>
31 #include "intel_drv.h"
36 #define SIL164_ADDR 0x38
37 #define CH7xxx_ADDR 0x76
38 #define TFP410_ADDR 0x38
40 extern struct intel_dvo_dev_ops sil164_ops;
41 extern struct intel_dvo_dev_ops ch7xxx_ops;
42 extern struct intel_dvo_dev_ops ivch_ops;
43 extern struct intel_dvo_dev_ops tfp410_ops;
44 extern struct intel_dvo_dev_ops ch7017_ops;
46 static struct intel_dvo_device intel_dvo_devices[] = {
48 .type = INTEL_DVO_CHIP_TMDS,
51 .slave_addr = SIL164_ADDR,
52 .dev_ops = &sil164_ops,
55 .type = INTEL_DVO_CHIP_TMDS,
58 .slave_addr = CH7xxx_ADDR,
59 .dev_ops = &ch7xxx_ops,
62 .type = INTEL_DVO_CHIP_LVDS,
65 .slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
69 .type = INTEL_DVO_CHIP_TMDS,
72 .slave_addr = TFP410_ADDR,
73 .dev_ops = &tfp410_ops,
76 .type = INTEL_DVO_CHIP_LVDS,
81 .dev_ops = &ch7017_ops,
85 static void intel_dvo_dpms(struct drm_encoder *encoder, int mode)
87 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
88 struct intel_output *intel_output = enc_to_intel_output(encoder);
89 struct intel_dvo_device *dvo = intel_output->dev_priv;
90 u32 dvo_reg = dvo->dvo_reg;
91 u32 temp = I915_READ(dvo_reg);
93 if (mode == DRM_MODE_DPMS_ON) {
94 I915_WRITE(dvo_reg, temp | DVO_ENABLE);
96 dvo->dev_ops->dpms(dvo, mode);
98 dvo->dev_ops->dpms(dvo, mode);
99 I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
104 static void intel_dvo_save(struct drm_connector *connector)
106 struct drm_i915_private *dev_priv = connector->dev->dev_private;
107 struct intel_output *intel_output = to_intel_output(connector);
108 struct intel_dvo_device *dvo = intel_output->dev_priv;
110 /* Each output should probably just save the registers it touches,
111 * but for now, use more overkill.
113 dev_priv->saveDVOA = I915_READ(DVOA);
114 dev_priv->saveDVOB = I915_READ(DVOB);
115 dev_priv->saveDVOC = I915_READ(DVOC);
117 dvo->dev_ops->save(dvo);
120 static void intel_dvo_restore(struct drm_connector *connector)
122 struct drm_i915_private *dev_priv = connector->dev->dev_private;
123 struct intel_output *intel_output = to_intel_output(connector);
124 struct intel_dvo_device *dvo = intel_output->dev_priv;
126 dvo->dev_ops->restore(dvo);
128 I915_WRITE(DVOA, dev_priv->saveDVOA);
129 I915_WRITE(DVOB, dev_priv->saveDVOB);
130 I915_WRITE(DVOC, dev_priv->saveDVOC);
133 static int intel_dvo_mode_valid(struct drm_connector *connector,
134 struct drm_display_mode *mode)
136 struct intel_output *intel_output = to_intel_output(connector);
137 struct intel_dvo_device *dvo = intel_output->dev_priv;
139 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
140 return MODE_NO_DBLESCAN;
142 /* XXX: Validate clock range */
144 if (dvo->panel_fixed_mode) {
145 if (mode->hdisplay > dvo->panel_fixed_mode->hdisplay)
147 if (mode->vdisplay > dvo->panel_fixed_mode->vdisplay)
151 return dvo->dev_ops->mode_valid(dvo, mode);
154 static bool intel_dvo_mode_fixup(struct drm_encoder *encoder,
155 struct drm_display_mode *mode,
156 struct drm_display_mode *adjusted_mode)
158 struct intel_output *intel_output = enc_to_intel_output(encoder);
159 struct intel_dvo_device *dvo = intel_output->dev_priv;
161 /* If we have timings from the BIOS for the panel, put them in
162 * to the adjusted mode. The CRTC will be set up for this mode,
163 * with the panel scaling set up to source from the H/VDisplay
164 * of the original mode.
166 if (dvo->panel_fixed_mode != NULL) {
167 #define C(x) adjusted_mode->x = dvo->panel_fixed_mode->x
177 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
181 if (dvo->dev_ops->mode_fixup)
182 return dvo->dev_ops->mode_fixup(dvo, mode, adjusted_mode);
187 static void intel_dvo_mode_set(struct drm_encoder *encoder,
188 struct drm_display_mode *mode,
189 struct drm_display_mode *adjusted_mode)
191 struct drm_device *dev = encoder->dev;
192 struct drm_i915_private *dev_priv = dev->dev_private;
193 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
194 struct intel_output *intel_output = enc_to_intel_output(encoder);
195 struct intel_dvo_device *dvo = intel_output->dev_priv;
196 int pipe = intel_crtc->pipe;
198 u32 dvo_reg = dvo->dvo_reg, dvo_srcdim_reg;
199 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
204 dvo_srcdim_reg = DVOA_SRCDIM;
207 dvo_srcdim_reg = DVOB_SRCDIM;
210 dvo_srcdim_reg = DVOC_SRCDIM;
214 dvo->dev_ops->mode_set(dvo, mode, adjusted_mode);
216 /* Save the data order, since I don't know what it should be set to. */
217 dvo_val = I915_READ(dvo_reg) &
218 (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
219 dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
220 DVO_BLANK_ACTIVE_HIGH;
223 dvo_val |= DVO_PIPE_B_SELECT;
224 dvo_val |= DVO_PIPE_STALL;
225 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
226 dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
227 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
228 dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
230 I915_WRITE(dpll_reg, I915_READ(dpll_reg) | DPLL_DVO_HIGH_SPEED);
232 /*I915_WRITE(DVOB_SRCDIM,
233 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
234 (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
235 I915_WRITE(dvo_srcdim_reg,
236 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
237 (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
238 /*I915_WRITE(DVOB, dvo_val);*/
239 I915_WRITE(dvo_reg, dvo_val);
243 * Detect the output connection on our DVO device.
247 static enum drm_connector_status intel_dvo_detect(struct drm_connector *connector)
249 struct intel_output *intel_output = to_intel_output(connector);
250 struct intel_dvo_device *dvo = intel_output->dev_priv;
252 return dvo->dev_ops->detect(dvo);
255 static int intel_dvo_get_modes(struct drm_connector *connector)
257 struct intel_output *intel_output = to_intel_output(connector);
258 struct intel_dvo_device *dvo = intel_output->dev_priv;
260 /* We should probably have an i2c driver get_modes function for those
261 * devices which will have a fixed set of modes determined by the chip
262 * (TV-out, for example), but for now with just TMDS and LVDS,
263 * that's not the case.
265 intel_ddc_get_modes(intel_output);
266 if (!list_empty(&connector->probed_modes))
270 if (dvo->panel_fixed_mode != NULL) {
271 struct drm_display_mode *mode;
272 mode = drm_mode_duplicate(connector->dev, dvo->panel_fixed_mode);
274 drm_mode_probed_add(connector, mode);
281 static void intel_dvo_destroy (struct drm_connector *connector)
283 struct intel_output *intel_output = to_intel_output(connector);
284 struct intel_dvo_device *dvo = intel_output->dev_priv;
287 if (dvo->dev_ops->destroy)
288 dvo->dev_ops->destroy(dvo);
289 if (dvo->panel_fixed_mode)
290 kfree(dvo->panel_fixed_mode);
291 /* no need, in i830_dvoices[] now */
294 if (intel_output->i2c_bus)
295 intel_i2c_destroy(intel_output->i2c_bus);
296 if (intel_output->ddc_bus)
297 intel_i2c_destroy(intel_output->ddc_bus);
298 drm_sysfs_connector_remove(connector);
299 drm_connector_cleanup(connector);
303 #ifdef RANDR_GET_CRTC_INTERFACE
304 static struct drm_crtc *intel_dvo_get_crtc(struct drm_connector *connector)
306 struct drm_device *dev = connector->dev;
307 struct drm_i915_private *dev_priv = dev->dev_private;
308 struct intel_output *intel_output = to_intel_output(connector);
309 struct intel_dvo_device *dvo = intel_output->dev_priv;
310 int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT);
312 return intel_pipe_to_crtc(pScrn, pipe);
316 static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
317 .dpms = intel_dvo_dpms,
318 .mode_fixup = intel_dvo_mode_fixup,
319 .prepare = intel_encoder_prepare,
320 .mode_set = intel_dvo_mode_set,
321 .commit = intel_encoder_commit,
324 static const struct drm_connector_funcs intel_dvo_connector_funcs = {
325 .save = intel_dvo_save,
326 .restore = intel_dvo_restore,
327 .detect = intel_dvo_detect,
328 .destroy = intel_dvo_destroy,
329 .fill_modes = drm_helper_probe_single_connector_modes,
332 static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
333 .mode_valid = intel_dvo_mode_valid,
334 .get_modes = intel_dvo_get_modes,
335 .best_encoder = intel_best_encoder,
338 static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
340 drm_encoder_cleanup(encoder);
343 static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
344 .destroy = intel_dvo_enc_destroy,
349 * Attempts to get a fixed panel timing for LVDS (currently only the i830).
351 * Other chips with DVO LVDS will need to extend this to deal with the LVDS
352 * chip being on DVOB/C and having multiple pipes.
354 static struct drm_display_mode *
355 intel_dvo_get_current_mode (struct drm_connector *connector)
357 struct drm_device *dev = connector->dev;
358 struct drm_i915_private *dev_priv = dev->dev_private;
359 struct intel_output *intel_output = to_intel_output(connector);
360 struct intel_dvo_device *dvo = intel_output->dev_priv;
361 uint32_t dvo_reg = dvo->dvo_reg;
362 uint32_t dvo_val = I915_READ(dvo_reg);
363 struct drm_display_mode *mode = NULL;
365 /* If the DVO port is active, that'll be the LVDS, so we can pull out
366 * its timings to get how the BIOS set up the panel.
368 if (dvo_val & DVO_ENABLE) {
369 struct drm_crtc *crtc;
370 int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
372 crtc = intel_get_crtc_from_pipe(dev, pipe);
374 mode = intel_crtc_mode_get(dev, crtc);
377 mode->type |= DRM_MODE_TYPE_PREFERRED;
378 if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
379 mode->flags |= DRM_MODE_FLAG_PHSYNC;
380 if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
381 mode->flags |= DRM_MODE_FLAG_PVSYNC;
388 void intel_dvo_init(struct drm_device *dev)
390 struct intel_output *intel_output;
391 struct intel_dvo_device *dvo;
392 struct intel_i2c_chan *i2cbus = NULL;
396 int encoder_type = DRM_MODE_ENCODER_NONE;
397 intel_output = kzalloc (sizeof(struct intel_output), GFP_KERNEL);
401 /* Set up the DDC bus */
402 intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
403 if (!intel_output->ddc_bus)
406 /* Now, try to find a controller */
407 for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
408 struct drm_connector *connector = &intel_output->base;
411 dvo = &intel_dvo_devices[i];
413 /* Allow the I2C driver info to specify the GPIO to be used in
414 * special cases, but otherwise default to what's defined
419 else if (dvo->type == INTEL_DVO_CHIP_LVDS)
424 /* Set up the I2C bus necessary for the chip we're probing.
425 * It appears that everything is on GPIOE except for panels
426 * on i830 laptops, which are on GPIOB (DVOA).
428 if (gpio_inited != gpio) {
430 intel_i2c_destroy(i2cbus);
431 if (!(i2cbus = intel_i2c_create(dev, gpio,
432 gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
438 if (dvo->dev_ops!= NULL)
439 ret = dvo->dev_ops->init(dvo, i2cbus);
446 intel_output->type = INTEL_OUTPUT_DVO;
448 case INTEL_DVO_CHIP_TMDS:
449 drm_connector_init(dev, connector,
450 &intel_dvo_connector_funcs,
451 DRM_MODE_CONNECTOR_DVII);
452 encoder_type = DRM_MODE_ENCODER_TMDS;
454 case INTEL_DVO_CHIP_LVDS:
455 drm_connector_init(dev, connector,
456 &intel_dvo_connector_funcs,
457 DRM_MODE_CONNECTOR_LVDS);
458 encoder_type = DRM_MODE_ENCODER_LVDS;
462 drm_connector_helper_add(connector,
463 &intel_dvo_connector_helper_funcs);
464 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
465 connector->interlace_allowed = false;
466 connector->doublescan_allowed = false;
468 intel_output->dev_priv = dvo;
469 intel_output->i2c_bus = i2cbus;
471 drm_encoder_init(dev, &intel_output->enc,
472 &intel_dvo_enc_funcs, encoder_type);
473 drm_encoder_helper_add(&intel_output->enc,
474 &intel_dvo_helper_funcs);
476 drm_mode_connector_attach_encoder(&intel_output->base,
478 if (dvo->type == INTEL_DVO_CHIP_LVDS) {
479 /* For our LVDS chipsets, we should hopefully be able
480 * to dig the fixed panel mode out of the BIOS data.
481 * However, it's in a different format from the BIOS
482 * data on chipsets with integrated LVDS (stored in AIM
483 * headers, likely), so for now, just get the current
484 * mode being output through DVO.
486 dvo->panel_fixed_mode =
487 intel_dvo_get_current_mode(connector);
488 dvo->panel_wants_dither = true;
491 drm_sysfs_connector_add(connector);
495 intel_i2c_destroy(intel_output->ddc_bus);
496 /* Didn't find a chip, so tear down. */
498 intel_i2c_destroy(i2cbus);