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_output *output, int mode)
90 struct drm_i915_private *dev_priv = output->dev->dev_private;
91 struct intel_output *intel_output = output->driver_private;
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_output *output)
109 struct drm_i915_private *dev_priv = output->dev->dev_private;
110 struct intel_output *intel_output = output->driver_private;
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_output *output)
125 struct drm_i915_private *dev_priv = output->dev->dev_private;
126 struct intel_output *intel_output = output->driver_private;
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_output *output,
137 struct drm_display_mode *mode)
139 struct intel_output *intel_output = output->driver_private;
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_output *output,
158 struct drm_display_mode *mode,
159 struct drm_display_mode *adjusted_mode)
161 struct intel_output *intel_output = output->driver_private;
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_output *output,
191 struct drm_display_mode *mode,
192 struct drm_display_mode *adjusted_mode)
194 struct drm_device *dev = output->dev;
195 struct drm_i915_private *dev_priv = dev->dev_private;
196 struct intel_crtc *intel_crtc = output->crtc->driver_private;
197 struct intel_output *intel_output = output->driver_private;
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_output_status intel_dvo_detect(struct drm_output *output)
252 struct intel_output *intel_output = output->driver_private;
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_output *output)
260 struct intel_output *intel_output = output->driver_private;
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(output);
269 if (!list_empty(&output->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(output->dev, dvo->panel_fixed_mode);
285 drm_mode_probed_add(output, mode);
292 static void intel_dvo_destroy (struct drm_output *output)
294 struct intel_output *intel_output = output->driver_private;
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 */
306 if (intel_output->i2c_bus)
307 intel_i2c_destroy(intel_output->i2c_bus);
308 if (intel_output->ddc_bus)
309 intel_i2c_destroy(intel_output->ddc_bus);
311 output->driver_private = NULL;
315 #ifdef RANDR_GET_CRTC_INTERFACE
316 static struct drm_crtc *intel_dvo_get_crtc(struct drm_output *output)
318 struct drm_device *dev = output->dev;
319 struct drm_i915_private *dev_priv = dev->dev_private;
320 struct intel_output *intel_output = output->driver_private;
321 struct intel_dvo_device *dvo = intel_output->dev_priv;
322 int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT);
324 return intel_pipe_to_crtc(pScrn, pipe);
329 static const struct drm_output_funcs intel_dvo_output_funcs = {
330 .dpms = intel_dvo_dpms,
331 .save = intel_dvo_save,
332 .restore = intel_dvo_restore,
333 .mode_valid = intel_dvo_mode_valid,
334 .mode_fixup = intel_dvo_mode_fixup,
335 .prepare = intel_output_prepare,
336 .mode_set = intel_dvo_mode_set,
337 .commit = intel_output_commit,
338 .detect = intel_dvo_detect,
339 .get_modes = intel_dvo_get_modes,
340 .cleanup = intel_dvo_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_output *output)
352 struct drm_device *dev = output->dev;
353 struct drm_i915_private *dev_priv = dev->dev_private;
354 struct intel_output *intel_output = output->driver_private;
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 |= V_PHSYNC;
375 if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
376 mode->flags |= V_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 connector = ConnectorUnknown;
393 intel_output = kzalloc (sizeof(struct intel_output), GFP_KERNEL);
397 /* Set up the DDC bus */
398 intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
399 if (!intel_output->ddc_bus)
402 /* Now, try to find a controller */
403 for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
404 struct drm_output *output = NULL;
407 dvo = &intel_dvo_devices[i];
409 /* Allow the I2C driver info to specify the GPIO to be used in
410 * special cases, but otherwise default to what's defined
415 else if (dvo->type == INTEL_DVO_CHIP_LVDS)
420 /* Set up the I2C bus necessary for the chip we're probing.
421 * It appears that everything is on GPIOE except for panels
422 * on i830 laptops, which are on GPIOB (DVOA).
424 if (gpio_inited != gpio) {
426 intel_i2c_destroy(i2cbus);
427 if (!(i2cbus = intel_i2c_create(dev, gpio,
428 gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
434 if (dvo->dev_ops!= NULL)
435 ret = dvo->dev_ops->init(dvo, i2cbus);
442 intel_output->type = INTEL_OUTPUT_DVO;
444 case INTEL_DVO_CHIP_TMDS:
445 connector = ConnectorDVID;
446 output = drm_output_create(dev, &intel_dvo_output_funcs,
447 DRM_MODE_OUTPUT_TMDS);
449 case INTEL_DVO_CHIP_LVDS:
450 connector = ConnectorLVDS;
451 output = drm_output_create(dev, &intel_dvo_output_funcs,
452 DRM_MODE_OUTPUT_LVDS);
455 if (output == NULL) {
456 dvo->dev_ops->destroy(dvo);
460 output->driver_private = intel_output;
461 output->display_info.subpixel_order = SubPixelHorizontalRGB;
462 output->interlace_allowed = false;
463 output->doublescan_allowed = false;
465 intel_output->dev_priv = dvo;
466 intel_output->i2c_bus = i2cbus;
468 if (dvo->type == INTEL_DVO_CHIP_LVDS) {
469 /* For our LVDS chipsets, we should hopefully be able
470 * to dig the fixed panel mode out of the BIOS data.
471 * However, it's in a different format from the BIOS
472 * data on chipsets with integrated LVDS (stored in AIM
473 * headers, likely), so for now, just get the current
474 * mode being output through DVO.
476 dvo->panel_fixed_mode = intel_dvo_get_current_mode(output);
477 dvo->panel_wants_dither = true;
480 drm_sysfs_output_add(output);
481 drm_output_attach_property(output,
482 dev->mode_config.connector_type_property,
488 intel_i2c_destroy(intel_output->ddc_bus);
489 /* Didn't find a chip, so tear down. */
491 intel_i2c_destroy(i2cbus);