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 & V_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 & V_PHSYNC)
229 dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
230 if (adjusted_mode->flags & V_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_connector_cleanup(connector);
313 #ifdef RANDR_GET_CRTC_INTERFACE
314 static struct drm_crtc *intel_dvo_get_crtc(struct drm_connector *connector)
316 struct drm_device *dev = connector->dev;
317 struct drm_i915_private *dev_priv = dev->dev_private;
318 struct intel_output *intel_output = to_intel_output(connector);
319 struct intel_dvo_device *dvo = intel_output->dev_priv;
320 int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT);
322 return intel_pipe_to_crtc(pScrn, pipe);
326 static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
327 .dpms = intel_dvo_dpms,
328 .mode_fixup = intel_dvo_mode_fixup,
329 .prepare = intel_encoder_prepare,
330 .mode_set = intel_dvo_mode_set,
331 .commit = intel_encoder_commit,
334 static const struct drm_connector_funcs intel_dvo_connector_funcs = {
335 .save = intel_dvo_save,
336 .restore = intel_dvo_restore,
337 .detect = intel_dvo_detect,
338 .destroy = intel_dvo_destroy,
339 .fill_modes = drm_helper_probe_single_connector_modes,
342 static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
343 .mode_valid = intel_dvo_mode_valid,
344 .get_modes = intel_dvo_get_modes,
345 .best_encoder = intel_best_encoder,
348 void intel_dvo_enc_destroy(struct drm_encoder *encoder)
350 drm_encoder_cleanup(encoder);
353 static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
354 .destroy = intel_dvo_enc_destroy,
359 * Attempts to get a fixed panel timing for LVDS (currently only the i830).
361 * Other chips with DVO LVDS will need to extend this to deal with the LVDS
362 * chip being on DVOB/C and having multiple pipes.
364 static struct drm_display_mode *
365 intel_dvo_get_current_mode (struct drm_connector *connector)
367 struct drm_device *dev = connector->dev;
368 struct drm_i915_private *dev_priv = dev->dev_private;
369 struct intel_output *intel_output = to_intel_output(connector);
370 struct intel_dvo_device *dvo = intel_output->dev_priv;
371 uint32_t dvo_reg = dvo->dvo_reg;
372 uint32_t dvo_val = I915_READ(dvo_reg);
373 struct drm_display_mode *mode = NULL;
375 /* If the DVO port is active, that'll be the LVDS, so we can pull out
376 * its timings to get how the BIOS set up the panel.
378 if (dvo_val & DVO_ENABLE) {
379 struct drm_crtc *crtc;
380 int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
382 crtc = intel_get_crtc_from_pipe(dev, pipe);
384 mode = intel_crtc_mode_get(dev, crtc);
387 mode->type |= DRM_MODE_TYPE_PREFERRED;
388 if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
389 mode->flags |= V_PHSYNC;
390 if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
391 mode->flags |= V_PVSYNC;
398 void intel_dvo_init(struct drm_device *dev)
400 struct intel_output *intel_output;
401 struct intel_dvo_device *dvo;
402 struct intel_i2c_chan *i2cbus = NULL;
406 int encoder_type = DRM_MODE_ENCODER_NONE;
407 intel_output = kzalloc (sizeof(struct intel_output), GFP_KERNEL);
411 /* Set up the DDC bus */
412 intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
413 if (!intel_output->ddc_bus)
416 /* Now, try to find a controller */
417 for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
418 struct drm_connector *connector = &intel_output->base;
421 dvo = &intel_dvo_devices[i];
423 /* Allow the I2C driver info to specify the GPIO to be used in
424 * special cases, but otherwise default to what's defined
429 else if (dvo->type == INTEL_DVO_CHIP_LVDS)
434 /* Set up the I2C bus necessary for the chip we're probing.
435 * It appears that everything is on GPIOE except for panels
436 * on i830 laptops, which are on GPIOB (DVOA).
438 if (gpio_inited != gpio) {
440 intel_i2c_destroy(i2cbus);
441 if (!(i2cbus = intel_i2c_create(dev, gpio,
442 gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
448 if (dvo->dev_ops!= NULL)
449 ret = dvo->dev_ops->init(dvo, i2cbus);
456 intel_output->type = INTEL_OUTPUT_DVO;
458 case INTEL_DVO_CHIP_TMDS:
459 // connector = DRM_MODE_CONNECTOR_DVID;
460 drm_connector_init(dev, connector, &intel_dvo_connector_funcs,
461 DRM_MODE_CONNECTOR_DVII);
462 encoder_type = DRM_MODE_ENCODER_TMDS;
464 case INTEL_DVO_CHIP_LVDS:
465 // connector = DRM_MODE_CONNECTOR_LVDS;
466 drm_connector_init(dev, connector, &intel_dvo_connector_funcs,
467 DRM_MODE_CONNECTOR_LVDS);
468 encoder_type = DRM_MODE_ENCODER_LVDS;
472 drm_connector_helper_add(connector, &intel_dvo_connector_helper_funcs);
473 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
474 connector->interlace_allowed = false;
475 connector->doublescan_allowed = false;
477 intel_output->dev_priv = dvo;
478 intel_output->i2c_bus = i2cbus;
480 drm_encoder_init(dev, &intel_output->enc, &intel_dvo_enc_funcs, encoder_type);
481 drm_encoder_helper_add(&intel_output->enc, &intel_dvo_helper_funcs);
483 drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
484 if (dvo->type == INTEL_DVO_CHIP_LVDS) {
485 /* For our LVDS chipsets, we should hopefully be able
486 * to dig the fixed panel mode out of the BIOS data.
487 * However, it's in a different format from the BIOS
488 * data on chipsets with integrated LVDS (stored in AIM
489 * headers, likely), so for now, just get the current
490 * mode being output through DVO.
492 dvo->panel_fixed_mode = intel_dvo_get_current_mode(connector);
493 dvo->panel_wants_dither = true;
496 drm_sysfs_connector_add(connector);
500 intel_i2c_destroy(intel_output->ddc_bus);
501 /* Didn't find a chip, so tear down. */
503 intel_i2c_destroy(i2cbus);