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 static struct intel_dvo_device intel_dvo_devices[] = {
42 .type = INTEL_DVO_CHIP_TMDS,
45 .slave_addr = SIL164_ADDR,
46 .dev_ops = &sil164_ops,
49 .type = INTEL_DVO_CHIP_TMDS,
52 .slave_addr = CH7xxx_ADDR,
53 .dev_ops = &ch7xxx_ops,
56 .type = INTEL_DVO_CHIP_LVDS,
59 .slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
63 .type = INTEL_DVO_CHIP_TMDS,
66 .slave_addr = TFP410_ADDR,
67 .dev_ops = &tfp410_ops,
70 .type = INTEL_DVO_CHIP_LVDS,
75 .dev_ops = &ch7017_ops,
79 static void intel_dvo_dpms(struct drm_encoder *encoder, int mode)
81 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
82 struct intel_output *intel_output = enc_to_intel_output(encoder);
83 struct intel_dvo_device *dvo = intel_output->dev_priv;
84 u32 dvo_reg = dvo->dvo_reg;
85 u32 temp = I915_READ(dvo_reg);
87 if (mode == DRM_MODE_DPMS_ON) {
88 I915_WRITE(dvo_reg, temp | DVO_ENABLE);
90 dvo->dev_ops->dpms(dvo, mode);
92 dvo->dev_ops->dpms(dvo, mode);
93 I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
98 static void intel_dvo_save(struct drm_connector *connector)
100 struct drm_i915_private *dev_priv = connector->dev->dev_private;
101 struct intel_output *intel_output = to_intel_output(connector);
102 struct intel_dvo_device *dvo = intel_output->dev_priv;
104 /* Each output should probably just save the registers it touches,
105 * but for now, use more overkill.
107 dev_priv->saveDVOA = I915_READ(DVOA);
108 dev_priv->saveDVOB = I915_READ(DVOB);
109 dev_priv->saveDVOC = I915_READ(DVOC);
111 dvo->dev_ops->save(dvo);
114 static void intel_dvo_restore(struct drm_connector *connector)
116 struct drm_i915_private *dev_priv = connector->dev->dev_private;
117 struct intel_output *intel_output = to_intel_output(connector);
118 struct intel_dvo_device *dvo = intel_output->dev_priv;
120 dvo->dev_ops->restore(dvo);
122 I915_WRITE(DVOA, dev_priv->saveDVOA);
123 I915_WRITE(DVOB, dev_priv->saveDVOB);
124 I915_WRITE(DVOC, dev_priv->saveDVOC);
127 static int intel_dvo_mode_valid(struct drm_connector *connector,
128 struct drm_display_mode *mode)
130 struct intel_output *intel_output = to_intel_output(connector);
131 struct intel_dvo_device *dvo = intel_output->dev_priv;
133 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
134 return MODE_NO_DBLESCAN;
136 /* XXX: Validate clock range */
138 if (dvo->panel_fixed_mode) {
139 if (mode->hdisplay > dvo->panel_fixed_mode->hdisplay)
141 if (mode->vdisplay > dvo->panel_fixed_mode->vdisplay)
145 return dvo->dev_ops->mode_valid(dvo, mode);
148 static bool intel_dvo_mode_fixup(struct drm_encoder *encoder,
149 struct drm_display_mode *mode,
150 struct drm_display_mode *adjusted_mode)
152 struct intel_output *intel_output = enc_to_intel_output(encoder);
153 struct intel_dvo_device *dvo = intel_output->dev_priv;
155 /* If we have timings from the BIOS for the panel, put them in
156 * to the adjusted mode. The CRTC will be set up for this mode,
157 * with the panel scaling set up to source from the H/VDisplay
158 * of the original mode.
160 if (dvo->panel_fixed_mode != NULL) {
161 #define C(x) adjusted_mode->x = dvo->panel_fixed_mode->x
171 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
175 if (dvo->dev_ops->mode_fixup)
176 return dvo->dev_ops->mode_fixup(dvo, mode, adjusted_mode);
181 static void intel_dvo_mode_set(struct drm_encoder *encoder,
182 struct drm_display_mode *mode,
183 struct drm_display_mode *adjusted_mode)
185 struct drm_device *dev = encoder->dev;
186 struct drm_i915_private *dev_priv = dev->dev_private;
187 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
188 struct intel_output *intel_output = enc_to_intel_output(encoder);
189 struct intel_dvo_device *dvo = intel_output->dev_priv;
190 int pipe = intel_crtc->pipe;
192 u32 dvo_reg = dvo->dvo_reg, dvo_srcdim_reg;
193 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
198 dvo_srcdim_reg = DVOA_SRCDIM;
201 dvo_srcdim_reg = DVOB_SRCDIM;
204 dvo_srcdim_reg = DVOC_SRCDIM;
208 dvo->dev_ops->mode_set(dvo, mode, adjusted_mode);
210 /* Save the data order, since I don't know what it should be set to. */
211 dvo_val = I915_READ(dvo_reg) &
212 (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
213 dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
214 DVO_BLANK_ACTIVE_HIGH;
217 dvo_val |= DVO_PIPE_B_SELECT;
218 dvo_val |= DVO_PIPE_STALL;
219 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
220 dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
221 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
222 dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
224 I915_WRITE(dpll_reg, I915_READ(dpll_reg) | DPLL_DVO_HIGH_SPEED);
226 /*I915_WRITE(DVOB_SRCDIM,
227 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
228 (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
229 I915_WRITE(dvo_srcdim_reg,
230 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
231 (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
232 /*I915_WRITE(DVOB, dvo_val);*/
233 I915_WRITE(dvo_reg, dvo_val);
237 * Detect the output connection on our DVO device.
241 static enum drm_connector_status intel_dvo_detect(struct drm_connector *connector)
243 struct intel_output *intel_output = to_intel_output(connector);
244 struct intel_dvo_device *dvo = intel_output->dev_priv;
246 return dvo->dev_ops->detect(dvo);
249 static int intel_dvo_get_modes(struct drm_connector *connector)
251 struct intel_output *intel_output = to_intel_output(connector);
252 struct intel_dvo_device *dvo = intel_output->dev_priv;
254 /* We should probably have an i2c driver get_modes function for those
255 * devices which will have a fixed set of modes determined by the chip
256 * (TV-out, for example), but for now with just TMDS and LVDS,
257 * that's not the case.
259 intel_ddc_get_modes(intel_output);
260 if (!list_empty(&connector->probed_modes))
264 if (dvo->panel_fixed_mode != NULL) {
265 struct drm_display_mode *mode;
266 mode = drm_mode_duplicate(connector->dev, dvo->panel_fixed_mode);
268 drm_mode_probed_add(connector, mode);
275 static void intel_dvo_destroy (struct drm_connector *connector)
277 struct intel_output *intel_output = to_intel_output(connector);
278 struct intel_dvo_device *dvo = intel_output->dev_priv;
281 if (dvo->dev_ops->destroy)
282 dvo->dev_ops->destroy(dvo);
283 if (dvo->panel_fixed_mode)
284 kfree(dvo->panel_fixed_mode);
285 /* no need, in i830_dvoices[] now */
288 if (intel_output->i2c_bus)
289 intel_i2c_destroy(intel_output->i2c_bus);
290 if (intel_output->ddc_bus)
291 intel_i2c_destroy(intel_output->ddc_bus);
292 drm_sysfs_connector_remove(connector);
293 drm_connector_cleanup(connector);
297 #ifdef RANDR_GET_CRTC_INTERFACE
298 static struct drm_crtc *intel_dvo_get_crtc(struct drm_connector *connector)
300 struct drm_device *dev = connector->dev;
301 struct drm_i915_private *dev_priv = dev->dev_private;
302 struct intel_output *intel_output = to_intel_output(connector);
303 struct intel_dvo_device *dvo = intel_output->dev_priv;
304 int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT);
306 return intel_pipe_to_crtc(pScrn, pipe);
310 static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
311 .dpms = intel_dvo_dpms,
312 .mode_fixup = intel_dvo_mode_fixup,
313 .prepare = intel_encoder_prepare,
314 .mode_set = intel_dvo_mode_set,
315 .commit = intel_encoder_commit,
318 static const struct drm_connector_funcs intel_dvo_connector_funcs = {
319 .dpms = drm_helper_connector_dpms,
320 .save = intel_dvo_save,
321 .restore = intel_dvo_restore,
322 .detect = intel_dvo_detect,
323 .destroy = intel_dvo_destroy,
324 .fill_modes = drm_helper_probe_single_connector_modes,
327 static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
328 .mode_valid = intel_dvo_mode_valid,
329 .get_modes = intel_dvo_get_modes,
330 .best_encoder = intel_best_encoder,
333 static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
335 drm_encoder_cleanup(encoder);
338 static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
339 .destroy = intel_dvo_enc_destroy,
344 * Attempts to get a fixed panel timing for LVDS (currently only the i830).
346 * Other chips with DVO LVDS will need to extend this to deal with the LVDS
347 * chip being on DVOB/C and having multiple pipes.
349 static struct drm_display_mode *
350 intel_dvo_get_current_mode (struct drm_connector *connector)
352 struct drm_device *dev = connector->dev;
353 struct drm_i915_private *dev_priv = dev->dev_private;
354 struct intel_output *intel_output = to_intel_output(connector);
355 struct intel_dvo_device *dvo = intel_output->dev_priv;
356 uint32_t dvo_reg = dvo->dvo_reg;
357 uint32_t dvo_val = I915_READ(dvo_reg);
358 struct drm_display_mode *mode = NULL;
360 /* If the DVO port is active, that'll be the LVDS, so we can pull out
361 * its timings to get how the BIOS set up the panel.
363 if (dvo_val & DVO_ENABLE) {
364 struct drm_crtc *crtc;
365 int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
367 crtc = intel_get_crtc_from_pipe(dev, pipe);
369 mode = intel_crtc_mode_get(dev, crtc);
372 mode->type |= DRM_MODE_TYPE_PREFERRED;
373 if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
374 mode->flags |= DRM_MODE_FLAG_PHSYNC;
375 if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
376 mode->flags |= DRM_MODE_FLAG_PVSYNC;
383 void intel_dvo_init(struct drm_device *dev)
385 struct intel_output *intel_output;
386 struct intel_dvo_device *dvo;
387 struct intel_i2c_chan *i2cbus = NULL;
391 int encoder_type = DRM_MODE_ENCODER_NONE;
392 intel_output = kzalloc (sizeof(struct intel_output), GFP_KERNEL);
396 /* Set up the DDC bus */
397 intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
398 if (!intel_output->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_output->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).
423 if (gpio_inited != gpio) {
425 intel_i2c_destroy(i2cbus);
426 if (!(i2cbus = intel_i2c_create(dev, gpio,
427 gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
433 if (dvo->dev_ops!= NULL)
434 ret = dvo->dev_ops->init(dvo, i2cbus);
441 intel_output->type = INTEL_OUTPUT_DVO;
443 case INTEL_DVO_CHIP_TMDS:
444 drm_connector_init(dev, connector,
445 &intel_dvo_connector_funcs,
446 DRM_MODE_CONNECTOR_DVII);
447 encoder_type = DRM_MODE_ENCODER_TMDS;
449 case INTEL_DVO_CHIP_LVDS:
450 drm_connector_init(dev, connector,
451 &intel_dvo_connector_funcs,
452 DRM_MODE_CONNECTOR_LVDS);
453 encoder_type = DRM_MODE_ENCODER_LVDS;
457 drm_connector_helper_add(connector,
458 &intel_dvo_connector_helper_funcs);
459 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
460 connector->interlace_allowed = false;
461 connector->doublescan_allowed = false;
463 intel_output->dev_priv = dvo;
464 intel_output->i2c_bus = i2cbus;
466 drm_encoder_init(dev, &intel_output->enc,
467 &intel_dvo_enc_funcs, encoder_type);
468 drm_encoder_helper_add(&intel_output->enc,
469 &intel_dvo_helper_funcs);
471 drm_mode_connector_attach_encoder(&intel_output->base,
473 if (dvo->type == INTEL_DVO_CHIP_LVDS) {
474 /* For our LVDS chipsets, we should hopefully be able
475 * to dig the fixed panel mode out of the BIOS data.
476 * However, it's in a different format from the BIOS
477 * data on chipsets with integrated LVDS (stored in AIM
478 * headers, likely), so for now, just get the current
479 * mode being output through DVO.
481 dvo->panel_fixed_mode =
482 intel_dvo_get_current_mode(connector);
483 dvo->panel_wants_dither = true;
486 drm_sysfs_connector_add(connector);
490 intel_i2c_destroy(intel_output->ddc_bus);
491 /* Didn't find a chip, so tear down. */
493 intel_i2c_destroy(i2cbus);