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>
28 #include <linux/slab.h>
32 #include "intel_drv.h"
37 #define SIL164_ADDR 0x38
38 #define CH7xxx_ADDR 0x76
39 #define TFP410_ADDR 0x38
41 static struct intel_dvo_device intel_dvo_devices
[] = {
43 .type
= INTEL_DVO_CHIP_TMDS
,
46 .slave_addr
= SIL164_ADDR
,
47 .dev_ops
= &sil164_ops
,
50 .type
= INTEL_DVO_CHIP_TMDS
,
53 .slave_addr
= CH7xxx_ADDR
,
54 .dev_ops
= &ch7xxx_ops
,
57 .type
= INTEL_DVO_CHIP_LVDS
,
60 .slave_addr
= 0x02, /* Might also be 0x44, 0x84, 0xc4 */
64 .type
= INTEL_DVO_CHIP_TMDS
,
67 .slave_addr
= TFP410_ADDR
,
68 .dev_ops
= &tfp410_ops
,
71 .type
= INTEL_DVO_CHIP_LVDS
,
76 .dev_ops
= &ch7017_ops
,
80 static void intel_dvo_dpms(struct drm_encoder
*encoder
, int mode
)
82 struct drm_i915_private
*dev_priv
= encoder
->dev
->dev_private
;
83 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
84 struct intel_dvo_device
*dvo
= intel_encoder
->dev_priv
;
85 u32 dvo_reg
= dvo
->dvo_reg
;
86 u32 temp
= I915_READ(dvo_reg
);
88 if (mode
== DRM_MODE_DPMS_ON
) {
89 I915_WRITE(dvo_reg
, temp
| DVO_ENABLE
);
91 dvo
->dev_ops
->dpms(dvo
, mode
);
93 dvo
->dev_ops
->dpms(dvo
, mode
);
94 I915_WRITE(dvo_reg
, temp
& ~DVO_ENABLE
);
99 static int intel_dvo_mode_valid(struct drm_connector
*connector
,
100 struct drm_display_mode
*mode
)
102 struct drm_encoder
*encoder
= intel_attached_encoder(connector
);
103 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
104 struct intel_dvo_device
*dvo
= intel_encoder
->dev_priv
;
106 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
107 return MODE_NO_DBLESCAN
;
109 /* XXX: Validate clock range */
111 if (dvo
->panel_fixed_mode
) {
112 if (mode
->hdisplay
> dvo
->panel_fixed_mode
->hdisplay
)
114 if (mode
->vdisplay
> dvo
->panel_fixed_mode
->vdisplay
)
118 return dvo
->dev_ops
->mode_valid(dvo
, mode
);
121 static bool intel_dvo_mode_fixup(struct drm_encoder
*encoder
,
122 struct drm_display_mode
*mode
,
123 struct drm_display_mode
*adjusted_mode
)
125 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
126 struct intel_dvo_device
*dvo
= intel_encoder
->dev_priv
;
128 /* If we have timings from the BIOS for the panel, put them in
129 * to the adjusted mode. The CRTC will be set up for this mode,
130 * with the panel scaling set up to source from the H/VDisplay
131 * of the original mode.
133 if (dvo
->panel_fixed_mode
!= NULL
) {
134 #define C(x) adjusted_mode->x = dvo->panel_fixed_mode->x
144 drm_mode_set_crtcinfo(adjusted_mode
, CRTC_INTERLACE_HALVE_V
);
148 if (dvo
->dev_ops
->mode_fixup
)
149 return dvo
->dev_ops
->mode_fixup(dvo
, mode
, adjusted_mode
);
154 static void intel_dvo_mode_set(struct drm_encoder
*encoder
,
155 struct drm_display_mode
*mode
,
156 struct drm_display_mode
*adjusted_mode
)
158 struct drm_device
*dev
= encoder
->dev
;
159 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
160 struct intel_crtc
*intel_crtc
= to_intel_crtc(encoder
->crtc
);
161 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
162 struct intel_dvo_device
*dvo
= intel_encoder
->dev_priv
;
163 int pipe
= intel_crtc
->pipe
;
165 u32 dvo_reg
= dvo
->dvo_reg
, dvo_srcdim_reg
;
166 int dpll_reg
= (pipe
== 0) ? DPLL_A
: DPLL_B
;
171 dvo_srcdim_reg
= DVOA_SRCDIM
;
174 dvo_srcdim_reg
= DVOB_SRCDIM
;
177 dvo_srcdim_reg
= DVOC_SRCDIM
;
181 dvo
->dev_ops
->mode_set(dvo
, mode
, adjusted_mode
);
183 /* Save the data order, since I don't know what it should be set to. */
184 dvo_val
= I915_READ(dvo_reg
) &
185 (DVO_PRESERVE_MASK
| DVO_DATA_ORDER_GBRG
);
186 dvo_val
|= DVO_DATA_ORDER_FP
| DVO_BORDER_ENABLE
|
187 DVO_BLANK_ACTIVE_HIGH
;
190 dvo_val
|= DVO_PIPE_B_SELECT
;
191 dvo_val
|= DVO_PIPE_STALL
;
192 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
193 dvo_val
|= DVO_HSYNC_ACTIVE_HIGH
;
194 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
195 dvo_val
|= DVO_VSYNC_ACTIVE_HIGH
;
197 I915_WRITE(dpll_reg
, I915_READ(dpll_reg
) | DPLL_DVO_HIGH_SPEED
);
199 /*I915_WRITE(DVOB_SRCDIM,
200 (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
201 (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
202 I915_WRITE(dvo_srcdim_reg
,
203 (adjusted_mode
->hdisplay
<< DVO_SRCDIM_HORIZONTAL_SHIFT
) |
204 (adjusted_mode
->vdisplay
<< DVO_SRCDIM_VERTICAL_SHIFT
));
205 /*I915_WRITE(DVOB, dvo_val);*/
206 I915_WRITE(dvo_reg
, dvo_val
);
210 * Detect the output connection on our DVO device.
214 static enum drm_connector_status
intel_dvo_detect(struct drm_connector
*connector
)
216 struct drm_encoder
*encoder
= intel_attached_encoder(connector
);
217 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
218 struct intel_dvo_device
*dvo
= intel_encoder
->dev_priv
;
220 return dvo
->dev_ops
->detect(dvo
);
223 static int intel_dvo_get_modes(struct drm_connector
*connector
)
225 struct drm_encoder
*encoder
= intel_attached_encoder(connector
);
226 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
227 struct intel_dvo_device
*dvo
= intel_encoder
->dev_priv
;
229 /* We should probably have an i2c driver get_modes function for those
230 * devices which will have a fixed set of modes determined by the chip
231 * (TV-out, for example), but for now with just TMDS and LVDS,
232 * that's not the case.
234 intel_ddc_get_modes(connector
, intel_encoder
->ddc_bus
);
235 if (!list_empty(&connector
->probed_modes
))
239 if (dvo
->panel_fixed_mode
!= NULL
) {
240 struct drm_display_mode
*mode
;
241 mode
= drm_mode_duplicate(connector
->dev
, dvo
->panel_fixed_mode
);
243 drm_mode_probed_add(connector
, mode
);
250 static void intel_dvo_destroy (struct drm_connector
*connector
)
252 drm_sysfs_connector_remove(connector
);
253 drm_connector_cleanup(connector
);
257 static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs
= {
258 .dpms
= intel_dvo_dpms
,
259 .mode_fixup
= intel_dvo_mode_fixup
,
260 .prepare
= intel_encoder_prepare
,
261 .mode_set
= intel_dvo_mode_set
,
262 .commit
= intel_encoder_commit
,
265 static const struct drm_connector_funcs intel_dvo_connector_funcs
= {
266 .dpms
= drm_helper_connector_dpms
,
267 .detect
= intel_dvo_detect
,
268 .destroy
= intel_dvo_destroy
,
269 .fill_modes
= drm_helper_probe_single_connector_modes
,
272 static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs
= {
273 .mode_valid
= intel_dvo_mode_valid
,
274 .get_modes
= intel_dvo_get_modes
,
275 .best_encoder
= intel_attached_encoder
,
278 static void intel_dvo_enc_destroy(struct drm_encoder
*encoder
)
280 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
281 struct intel_dvo_device
*dvo
= intel_encoder
->dev_priv
;
284 if (dvo
->dev_ops
->destroy
)
285 dvo
->dev_ops
->destroy(dvo
);
286 if (dvo
->panel_fixed_mode
)
287 kfree(dvo
->panel_fixed_mode
);
289 if (intel_encoder
->i2c_bus
)
290 intel_i2c_destroy(intel_encoder
->i2c_bus
);
291 if (intel_encoder
->ddc_bus
)
292 intel_i2c_destroy(intel_encoder
->ddc_bus
);
293 drm_encoder_cleanup(encoder
);
294 kfree(intel_encoder
);
297 static const struct drm_encoder_funcs intel_dvo_enc_funcs
= {
298 .destroy
= intel_dvo_enc_destroy
,
303 * Attempts to get a fixed panel timing for LVDS (currently only the i830).
305 * Other chips with DVO LVDS will need to extend this to deal with the LVDS
306 * chip being on DVOB/C and having multiple pipes.
308 static struct drm_display_mode
*
309 intel_dvo_get_current_mode (struct drm_connector
*connector
)
311 struct drm_device
*dev
= connector
->dev
;
312 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
313 struct drm_encoder
*encoder
= intel_attached_encoder(connector
);
314 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
315 struct intel_dvo_device
*dvo
= intel_encoder
->dev_priv
;
316 uint32_t dvo_reg
= dvo
->dvo_reg
;
317 uint32_t dvo_val
= I915_READ(dvo_reg
);
318 struct drm_display_mode
*mode
= NULL
;
320 /* If the DVO port is active, that'll be the LVDS, so we can pull out
321 * its timings to get how the BIOS set up the panel.
323 if (dvo_val
& DVO_ENABLE
) {
324 struct drm_crtc
*crtc
;
325 int pipe
= (dvo_val
& DVO_PIPE_B_SELECT
) ? 1 : 0;
327 crtc
= intel_get_crtc_from_pipe(dev
, pipe
);
329 mode
= intel_crtc_mode_get(dev
, crtc
);
332 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
333 if (dvo_val
& DVO_HSYNC_ACTIVE_HIGH
)
334 mode
->flags
|= DRM_MODE_FLAG_PHSYNC
;
335 if (dvo_val
& DVO_VSYNC_ACTIVE_HIGH
)
336 mode
->flags
|= DRM_MODE_FLAG_PVSYNC
;
343 void intel_dvo_init(struct drm_device
*dev
)
345 struct intel_encoder
*intel_encoder
;
346 struct intel_connector
*intel_connector
;
347 struct intel_dvo_device
*dvo
;
348 struct i2c_adapter
*i2cbus
= NULL
;
351 int encoder_type
= DRM_MODE_ENCODER_NONE
;
352 intel_encoder
= kzalloc (sizeof(struct intel_encoder
), GFP_KERNEL
);
356 intel_connector
= kzalloc(sizeof(struct intel_connector
), GFP_KERNEL
);
357 if (!intel_connector
) {
358 kfree(intel_encoder
);
362 /* Set up the DDC bus */
363 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, GPIOD
, "DVODDC_D");
364 if (!intel_encoder
->ddc_bus
)
367 /* Now, try to find a controller */
368 for (i
= 0; i
< ARRAY_SIZE(intel_dvo_devices
); i
++) {
369 struct drm_connector
*connector
= &intel_connector
->base
;
372 dvo
= &intel_dvo_devices
[i
];
374 /* Allow the I2C driver info to specify the GPIO to be used in
375 * special cases, but otherwise default to what's defined
380 else if (dvo
->type
== INTEL_DVO_CHIP_LVDS
)
385 /* Set up the I2C bus necessary for the chip we're probing.
386 * It appears that everything is on GPIOE except for panels
387 * on i830 laptops, which are on GPIOB (DVOA).
390 intel_i2c_destroy(i2cbus
);
391 if (!(i2cbus
= intel_i2c_create(dev
, gpio
,
392 gpio
== GPIOB
? "DVOI2C_B" : "DVOI2C_E"))) {
396 if (dvo
->dev_ops
!= NULL
)
397 ret
= dvo
->dev_ops
->init(dvo
, i2cbus
);
404 intel_encoder
->type
= INTEL_OUTPUT_DVO
;
405 intel_encoder
->crtc_mask
= (1 << 0) | (1 << 1);
407 case INTEL_DVO_CHIP_TMDS
:
408 intel_encoder
->clone_mask
=
409 (1 << INTEL_DVO_TMDS_CLONE_BIT
) |
410 (1 << INTEL_ANALOG_CLONE_BIT
);
411 drm_connector_init(dev
, connector
,
412 &intel_dvo_connector_funcs
,
413 DRM_MODE_CONNECTOR_DVII
);
414 encoder_type
= DRM_MODE_ENCODER_TMDS
;
416 case INTEL_DVO_CHIP_LVDS
:
417 intel_encoder
->clone_mask
=
418 (1 << INTEL_DVO_LVDS_CLONE_BIT
);
419 drm_connector_init(dev
, connector
,
420 &intel_dvo_connector_funcs
,
421 DRM_MODE_CONNECTOR_LVDS
);
422 encoder_type
= DRM_MODE_ENCODER_LVDS
;
426 drm_connector_helper_add(connector
,
427 &intel_dvo_connector_helper_funcs
);
428 connector
->display_info
.subpixel_order
= SubPixelHorizontalRGB
;
429 connector
->interlace_allowed
= false;
430 connector
->doublescan_allowed
= false;
432 intel_encoder
->dev_priv
= dvo
;
433 intel_encoder
->i2c_bus
= i2cbus
;
435 drm_encoder_init(dev
, &intel_encoder
->enc
,
436 &intel_dvo_enc_funcs
, encoder_type
);
437 drm_encoder_helper_add(&intel_encoder
->enc
,
438 &intel_dvo_helper_funcs
);
440 drm_mode_connector_attach_encoder(&intel_connector
->base
,
441 &intel_encoder
->enc
);
442 if (dvo
->type
== INTEL_DVO_CHIP_LVDS
) {
443 /* For our LVDS chipsets, we should hopefully be able
444 * to dig the fixed panel mode out of the BIOS data.
445 * However, it's in a different format from the BIOS
446 * data on chipsets with integrated LVDS (stored in AIM
447 * headers, likely), so for now, just get the current
448 * mode being output through DVO.
450 dvo
->panel_fixed_mode
=
451 intel_dvo_get_current_mode(connector
);
452 dvo
->panel_wants_dither
= true;
455 drm_sysfs_connector_add(connector
);
459 intel_i2c_destroy(intel_encoder
->ddc_bus
);
460 /* Didn't find a chip, so tear down. */
462 intel_i2c_destroy(i2cbus
);
464 kfree(intel_encoder
);
465 kfree(intel_connector
);