intel_dvo.c revision 599be16c6ef70f349edaef43a0ee2712c1af7328
1/*
2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2007 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 *
24 * Authors:
25 *	Eric Anholt <eric@anholt.net>
26 */
27#include <linux/i2c.h>
28#include "drmP.h"
29#include "drm.h"
30#include "drm_crtc.h"
31#include "intel_drv.h"
32#include "i915_drm.h"
33#include "i915_drv.h"
34#include "dvo.h"
35
36#define SIL164_ADDR	0x38
37#define CH7xxx_ADDR	0x76
38#define TFP410_ADDR	0x38
39
40static struct intel_dvo_device intel_dvo_devices[] = {
41	{
42		.type = INTEL_DVO_CHIP_TMDS,
43		.name = "sil164",
44		.dvo_reg = DVOC,
45		.slave_addr = SIL164_ADDR,
46		.dev_ops = &sil164_ops,
47	},
48	{
49		.type = INTEL_DVO_CHIP_TMDS,
50		.name = "ch7xxx",
51		.dvo_reg = DVOC,
52		.slave_addr = CH7xxx_ADDR,
53		.dev_ops = &ch7xxx_ops,
54	},
55	{
56		.type = INTEL_DVO_CHIP_LVDS,
57		.name = "ivch",
58		.dvo_reg = DVOA,
59		.slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
60		.dev_ops = &ivch_ops,
61	},
62	{
63		.type = INTEL_DVO_CHIP_TMDS,
64		.name = "tfp410",
65		.dvo_reg = DVOC,
66		.slave_addr = TFP410_ADDR,
67		.dev_ops = &tfp410_ops,
68	},
69	{
70		.type = INTEL_DVO_CHIP_LVDS,
71		.name = "ch7017",
72		.dvo_reg = DVOC,
73		.slave_addr = 0x75,
74		.gpio = GPIOE,
75		.dev_ops = &ch7017_ops,
76	}
77};
78
79static void intel_dvo_dpms(struct drm_encoder *encoder, int mode)
80{
81	struct drm_i915_private *dev_priv = encoder->dev->dev_private;
82	struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
83	struct intel_dvo_device *dvo = intel_encoder->dev_priv;
84	u32 dvo_reg = dvo->dvo_reg;
85	u32 temp = I915_READ(dvo_reg);
86
87	if (mode == DRM_MODE_DPMS_ON) {
88		I915_WRITE(dvo_reg, temp | DVO_ENABLE);
89		I915_READ(dvo_reg);
90		dvo->dev_ops->dpms(dvo, mode);
91	} else {
92		dvo->dev_ops->dpms(dvo, mode);
93		I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
94		I915_READ(dvo_reg);
95	}
96}
97
98static int intel_dvo_mode_valid(struct drm_connector *connector,
99				struct drm_display_mode *mode)
100{
101	struct drm_encoder *encoder = intel_attached_encoder(connector);
102	struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
103	struct intel_dvo_device *dvo = intel_encoder->dev_priv;
104
105	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
106		return MODE_NO_DBLESCAN;
107
108	/* XXX: Validate clock range */
109
110	if (dvo->panel_fixed_mode) {
111		if (mode->hdisplay > dvo->panel_fixed_mode->hdisplay)
112			return MODE_PANEL;
113		if (mode->vdisplay > dvo->panel_fixed_mode->vdisplay)
114			return MODE_PANEL;
115	}
116
117	return dvo->dev_ops->mode_valid(dvo, mode);
118}
119
120static bool intel_dvo_mode_fixup(struct drm_encoder *encoder,
121				 struct drm_display_mode *mode,
122				 struct drm_display_mode *adjusted_mode)
123{
124	struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
125	struct intel_dvo_device *dvo = intel_encoder->dev_priv;
126
127	/* If we have timings from the BIOS for the panel, put them in
128	 * to the adjusted mode.  The CRTC will be set up for this mode,
129	 * with the panel scaling set up to source from the H/VDisplay
130	 * of the original mode.
131	 */
132	if (dvo->panel_fixed_mode != NULL) {
133#define C(x) adjusted_mode->x = dvo->panel_fixed_mode->x
134		C(hdisplay);
135		C(hsync_start);
136		C(hsync_end);
137		C(htotal);
138		C(vdisplay);
139		C(vsync_start);
140		C(vsync_end);
141		C(vtotal);
142		C(clock);
143		drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
144#undef C
145	}
146
147	if (dvo->dev_ops->mode_fixup)
148		return dvo->dev_ops->mode_fixup(dvo, mode, adjusted_mode);
149
150	return true;
151}
152
153static void intel_dvo_mode_set(struct drm_encoder *encoder,
154			       struct drm_display_mode *mode,
155			       struct drm_display_mode *adjusted_mode)
156{
157	struct drm_device *dev = encoder->dev;
158	struct drm_i915_private *dev_priv = dev->dev_private;
159	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
160	struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
161	struct intel_dvo_device *dvo = intel_encoder->dev_priv;
162	int pipe = intel_crtc->pipe;
163	u32 dvo_val;
164	u32 dvo_reg = dvo->dvo_reg, dvo_srcdim_reg;
165	int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
166
167	switch (dvo_reg) {
168	case DVOA:
169	default:
170		dvo_srcdim_reg = DVOA_SRCDIM;
171		break;
172	case DVOB:
173		dvo_srcdim_reg = DVOB_SRCDIM;
174		break;
175	case DVOC:
176		dvo_srcdim_reg = DVOC_SRCDIM;
177		break;
178	}
179
180	dvo->dev_ops->mode_set(dvo, mode, adjusted_mode);
181
182	/* Save the data order, since I don't know what it should be set to. */
183	dvo_val = I915_READ(dvo_reg) &
184		  (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
185	dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
186		   DVO_BLANK_ACTIVE_HIGH;
187
188	if (pipe == 1)
189		dvo_val |= DVO_PIPE_B_SELECT;
190	dvo_val |= DVO_PIPE_STALL;
191	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
192		dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
193	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
194		dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
195
196	I915_WRITE(dpll_reg, I915_READ(dpll_reg) | DPLL_DVO_HIGH_SPEED);
197
198	/*I915_WRITE(DVOB_SRCDIM,
199	  (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
200	  (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
201	I915_WRITE(dvo_srcdim_reg,
202		   (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
203		   (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
204	/*I915_WRITE(DVOB, dvo_val);*/
205	I915_WRITE(dvo_reg, dvo_val);
206}
207
208/**
209 * Detect the output connection on our DVO device.
210 *
211 * Unimplemented.
212 */
213static enum drm_connector_status intel_dvo_detect(struct drm_connector *connector)
214{
215	struct drm_encoder *encoder = intel_attached_encoder(connector);
216	struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
217	struct intel_dvo_device *dvo = intel_encoder->dev_priv;
218
219	return dvo->dev_ops->detect(dvo);
220}
221
222static int intel_dvo_get_modes(struct drm_connector *connector)
223{
224	struct drm_encoder *encoder = intel_attached_encoder(connector);
225	struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
226	struct intel_dvo_device *dvo = intel_encoder->dev_priv;
227
228	/* We should probably have an i2c driver get_modes function for those
229	 * devices which will have a fixed set of modes determined by the chip
230	 * (TV-out, for example), but for now with just TMDS and LVDS,
231	 * that's not the case.
232	 */
233	intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
234	if (!list_empty(&connector->probed_modes))
235		return 1;
236
237
238	if (dvo->panel_fixed_mode != NULL) {
239		struct drm_display_mode *mode;
240		mode = drm_mode_duplicate(connector->dev, dvo->panel_fixed_mode);
241		if (mode) {
242			drm_mode_probed_add(connector, mode);
243			return 1;
244		}
245	}
246	return 0;
247}
248
249static void intel_dvo_destroy (struct drm_connector *connector)
250{
251	drm_sysfs_connector_remove(connector);
252	drm_connector_cleanup(connector);
253	kfree(connector);
254}
255
256static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
257	.dpms = intel_dvo_dpms,
258	.mode_fixup = intel_dvo_mode_fixup,
259	.prepare = intel_encoder_prepare,
260	.mode_set = intel_dvo_mode_set,
261	.commit = intel_encoder_commit,
262};
263
264static const struct drm_connector_funcs intel_dvo_connector_funcs = {
265	.dpms = drm_helper_connector_dpms,
266	.detect = intel_dvo_detect,
267	.destroy = intel_dvo_destroy,
268	.fill_modes = drm_helper_probe_single_connector_modes,
269};
270
271static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
272	.mode_valid = intel_dvo_mode_valid,
273	.get_modes = intel_dvo_get_modes,
274	.best_encoder = intel_attached_encoder,
275};
276
277static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
278{
279	struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
280	struct intel_dvo_device *dvo = intel_encoder->dev_priv;
281
282	if (dvo) {
283		if (dvo->dev_ops->destroy)
284			dvo->dev_ops->destroy(dvo);
285		if (dvo->panel_fixed_mode)
286			kfree(dvo->panel_fixed_mode);
287	}
288	if (intel_encoder->i2c_bus)
289		intel_i2c_destroy(intel_encoder->i2c_bus);
290	if (intel_encoder->ddc_bus)
291		intel_i2c_destroy(intel_encoder->ddc_bus);
292	drm_encoder_cleanup(encoder);
293	kfree(intel_encoder);
294}
295
296static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
297	.destroy = intel_dvo_enc_destroy,
298};
299
300
301/**
302 * Attempts to get a fixed panel timing for LVDS (currently only the i830).
303 *
304 * Other chips with DVO LVDS will need to extend this to deal with the LVDS
305 * chip being on DVOB/C and having multiple pipes.
306 */
307static struct drm_display_mode *
308intel_dvo_get_current_mode (struct drm_connector *connector)
309{
310	struct drm_device *dev = connector->dev;
311	struct drm_i915_private *dev_priv = dev->dev_private;
312	struct drm_encoder *encoder = intel_attached_encoder(connector);
313	struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
314	struct intel_dvo_device *dvo = intel_encoder->dev_priv;
315	uint32_t dvo_reg = dvo->dvo_reg;
316	uint32_t dvo_val = I915_READ(dvo_reg);
317	struct drm_display_mode *mode = NULL;
318
319	/* If the DVO port is active, that'll be the LVDS, so we can pull out
320	 * its timings to get how the BIOS set up the panel.
321	 */
322	if (dvo_val & DVO_ENABLE) {
323		struct drm_crtc *crtc;
324		int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
325
326		crtc = intel_get_crtc_from_pipe(dev, pipe);
327		if (crtc) {
328			mode = intel_crtc_mode_get(dev, crtc);
329
330			if (mode) {
331				mode->type |= DRM_MODE_TYPE_PREFERRED;
332				if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
333					mode->flags |= DRM_MODE_FLAG_PHSYNC;
334				if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
335					mode->flags |= DRM_MODE_FLAG_PVSYNC;
336			}
337		}
338	}
339	return mode;
340}
341
342void intel_dvo_init(struct drm_device *dev)
343{
344	struct intel_encoder *intel_encoder;
345	struct intel_connector *intel_connector;
346	struct intel_dvo_device *dvo;
347	struct i2c_adapter *i2cbus = NULL;
348	int ret = 0;
349	int i;
350	int encoder_type = DRM_MODE_ENCODER_NONE;
351	intel_encoder = kzalloc (sizeof(struct intel_encoder), GFP_KERNEL);
352	if (!intel_encoder)
353		return;
354
355	intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
356	if (!intel_connector) {
357		kfree(intel_encoder);
358		return;
359	}
360
361	/* Set up the DDC bus */
362	intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
363	if (!intel_encoder->ddc_bus)
364		goto free_intel;
365
366	/* Now, try to find a controller */
367	for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
368		struct drm_connector *connector = &intel_connector->base;
369		int gpio;
370
371		dvo = &intel_dvo_devices[i];
372
373		/* Allow the I2C driver info to specify the GPIO to be used in
374		 * special cases, but otherwise default to what's defined
375		 * in the spec.
376		 */
377		if (dvo->gpio != 0)
378			gpio = dvo->gpio;
379		else if (dvo->type == INTEL_DVO_CHIP_LVDS)
380			gpio = GPIOB;
381		else
382			gpio = GPIOE;
383
384		/* Set up the I2C bus necessary for the chip we're probing.
385		 * It appears that everything is on GPIOE except for panels
386		 * on i830 laptops, which are on GPIOB (DVOA).
387		 */
388		if (i2cbus != NULL)
389			intel_i2c_destroy(i2cbus);
390		if (!(i2cbus = intel_i2c_create(dev, gpio,
391			gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
392			continue;
393		}
394
395		if (dvo->dev_ops!= NULL)
396			ret = dvo->dev_ops->init(dvo, i2cbus);
397		else
398			ret = false;
399
400		if (!ret)
401			continue;
402
403		intel_encoder->type = INTEL_OUTPUT_DVO;
404		intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
405		switch (dvo->type) {
406		case INTEL_DVO_CHIP_TMDS:
407			intel_encoder->clone_mask =
408				(1 << INTEL_DVO_TMDS_CLONE_BIT) |
409				(1 << INTEL_ANALOG_CLONE_BIT);
410			drm_connector_init(dev, connector,
411					   &intel_dvo_connector_funcs,
412					   DRM_MODE_CONNECTOR_DVII);
413			encoder_type = DRM_MODE_ENCODER_TMDS;
414			break;
415		case INTEL_DVO_CHIP_LVDS:
416			intel_encoder->clone_mask =
417				(1 << INTEL_DVO_LVDS_CLONE_BIT);
418			drm_connector_init(dev, connector,
419					   &intel_dvo_connector_funcs,
420					   DRM_MODE_CONNECTOR_LVDS);
421			encoder_type = DRM_MODE_ENCODER_LVDS;
422			break;
423		}
424
425		drm_connector_helper_add(connector,
426					 &intel_dvo_connector_helper_funcs);
427		connector->display_info.subpixel_order = SubPixelHorizontalRGB;
428		connector->interlace_allowed = false;
429		connector->doublescan_allowed = false;
430
431		intel_encoder->dev_priv = dvo;
432		intel_encoder->i2c_bus = i2cbus;
433
434		drm_encoder_init(dev, &intel_encoder->enc,
435				 &intel_dvo_enc_funcs, encoder_type);
436		drm_encoder_helper_add(&intel_encoder->enc,
437				       &intel_dvo_helper_funcs);
438
439		drm_mode_connector_attach_encoder(&intel_connector->base,
440						  &intel_encoder->enc);
441		if (dvo->type == INTEL_DVO_CHIP_LVDS) {
442			/* For our LVDS chipsets, we should hopefully be able
443			 * to dig the fixed panel mode out of the BIOS data.
444			 * However, it's in a different format from the BIOS
445			 * data on chipsets with integrated LVDS (stored in AIM
446			 * headers, likely), so for now, just get the current
447			 * mode being output through DVO.
448			 */
449			dvo->panel_fixed_mode =
450				intel_dvo_get_current_mode(connector);
451			dvo->panel_wants_dither = true;
452		}
453
454		drm_sysfs_connector_add(connector);
455		return;
456	}
457
458	intel_i2c_destroy(intel_encoder->ddc_bus);
459	/* Didn't find a chip, so tear down. */
460	if (i2cbus != NULL)
461		intel_i2c_destroy(i2cbus);
462free_intel:
463	kfree(intel_encoder);
464	kfree(intel_connector);
465}
466