1 /*
2  * Copyright 2006 Dave Airlie <airlied@linux.ie>
3  * Copyright © 2006-2007 Intel Corporation
4  *   Jesse Barnes <jesse.barnes@intel.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *	Eric Anholt <eric@anholt.net>
27  */
28 #include <linux/module.h>
29 #include <linux/i2c.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <drm/drmP.h>
33 #include <drm/drm_crtc.h>
34 #include <drm/drm_edid.h>
35 #include "psb_intel_drv.h"
36 #include <drm/gma_drm.h>
37 #include "psb_drv.h"
38 #include "psb_intel_sdvo_regs.h"
39 #include "psb_intel_reg.h"
40 
41 #define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
42 #define SDVO_RGB_MASK  (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
43 #define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
44 #define SDVO_TV_MASK   (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0)
45 
46 #define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
47                          SDVO_TV_MASK)
48 
49 #define IS_TV(c)	(c->output_flag & SDVO_TV_MASK)
50 #define IS_TMDS(c)	(c->output_flag & SDVO_TMDS_MASK)
51 #define IS_LVDS(c)	(c->output_flag & SDVO_LVDS_MASK)
52 #define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
53 
54 
55 static const char *tv_format_names[] = {
56 	"NTSC_M"   , "NTSC_J"  , "NTSC_443",
57 	"PAL_B"    , "PAL_D"   , "PAL_G"   ,
58 	"PAL_H"    , "PAL_I"   , "PAL_M"   ,
59 	"PAL_N"    , "PAL_NC"  , "PAL_60"  ,
60 	"SECAM_B"  , "SECAM_D" , "SECAM_G" ,
61 	"SECAM_K"  , "SECAM_K1", "SECAM_L" ,
62 	"SECAM_60"
63 };
64 
65 #define TV_FORMAT_NUM  (sizeof(tv_format_names) / sizeof(*tv_format_names))
66 
67 struct psb_intel_sdvo {
68 	struct gma_encoder base;
69 
70 	struct i2c_adapter *i2c;
71 	u8 slave_addr;
72 
73 	struct i2c_adapter ddc;
74 
75 	/* Register for the SDVO device: SDVOB or SDVOC */
76 	int sdvo_reg;
77 
78 	/* Active outputs controlled by this SDVO output */
79 	uint16_t controlled_output;
80 
81 	/*
82 	 * Capabilities of the SDVO device returned by
83 	 * i830_sdvo_get_capabilities()
84 	 */
85 	struct psb_intel_sdvo_caps caps;
86 
87 	/* Pixel clock limitations reported by the SDVO device, in kHz */
88 	int pixel_clock_min, pixel_clock_max;
89 
90 	/*
91 	* For multiple function SDVO device,
92 	* this is for current attached outputs.
93 	*/
94 	uint16_t attached_output;
95 
96 	/**
97 	 * This is used to select the color range of RBG outputs in HDMI mode.
98 	 * It is only valid when using TMDS encoding and 8 bit per color mode.
99 	 */
100 	uint32_t color_range;
101 
102 	/**
103 	 * This is set if we're going to treat the device as TV-out.
104 	 *
105 	 * While we have these nice friendly flags for output types that ought
106 	 * to decide this for us, the S-Video output on our HDMI+S-Video card
107 	 * shows up as RGB1 (VGA).
108 	 */
109 	bool is_tv;
110 
111 	/* This is for current tv format name */
112 	int tv_format_index;
113 
114 	/**
115 	 * This is set if we treat the device as HDMI, instead of DVI.
116 	 */
117 	bool is_hdmi;
118 	bool has_hdmi_monitor;
119 	bool has_hdmi_audio;
120 
121 	/**
122 	 * This is set if we detect output of sdvo device as LVDS and
123 	 * have a valid fixed mode to use with the panel.
124 	 */
125 	bool is_lvds;
126 
127 	/**
128 	 * This is sdvo fixed pannel mode pointer
129 	 */
130 	struct drm_display_mode *sdvo_lvds_fixed_mode;
131 
132 	/* DDC bus used by this SDVO encoder */
133 	uint8_t ddc_bus;
134 
135 	/* Input timings for adjusted_mode */
136 	struct psb_intel_sdvo_dtd input_dtd;
137 
138 	/* Saved SDVO output states */
139 	uint32_t saveSDVO; /* Can be SDVOB or SDVOC depending on sdvo_reg */
140 };
141 
142 struct psb_intel_sdvo_connector {
143 	struct gma_connector base;
144 
145 	/* Mark the type of connector */
146 	uint16_t output_flag;
147 
148 	int force_audio;
149 
150 	/* This contains all current supported TV format */
151 	u8 tv_format_supported[TV_FORMAT_NUM];
152 	int   format_supported_num;
153 	struct drm_property *tv_format;
154 
155 	/* add the property for the SDVO-TV */
156 	struct drm_property *left;
157 	struct drm_property *right;
158 	struct drm_property *top;
159 	struct drm_property *bottom;
160 	struct drm_property *hpos;
161 	struct drm_property *vpos;
162 	struct drm_property *contrast;
163 	struct drm_property *saturation;
164 	struct drm_property *hue;
165 	struct drm_property *sharpness;
166 	struct drm_property *flicker_filter;
167 	struct drm_property *flicker_filter_adaptive;
168 	struct drm_property *flicker_filter_2d;
169 	struct drm_property *tv_chroma_filter;
170 	struct drm_property *tv_luma_filter;
171 	struct drm_property *dot_crawl;
172 
173 	/* add the property for the SDVO-TV/LVDS */
174 	struct drm_property *brightness;
175 
176 	/* Add variable to record current setting for the above property */
177 	u32	left_margin, right_margin, top_margin, bottom_margin;
178 
179 	/* this is to get the range of margin.*/
180 	u32	max_hscan,  max_vscan;
181 	u32	max_hpos, cur_hpos;
182 	u32	max_vpos, cur_vpos;
183 	u32	cur_brightness, max_brightness;
184 	u32	cur_contrast,	max_contrast;
185 	u32	cur_saturation, max_saturation;
186 	u32	cur_hue,	max_hue;
187 	u32	cur_sharpness,	max_sharpness;
188 	u32	cur_flicker_filter,		max_flicker_filter;
189 	u32	cur_flicker_filter_adaptive,	max_flicker_filter_adaptive;
190 	u32	cur_flicker_filter_2d,		max_flicker_filter_2d;
191 	u32	cur_tv_chroma_filter,	max_tv_chroma_filter;
192 	u32	cur_tv_luma_filter,	max_tv_luma_filter;
193 	u32	cur_dot_crawl,	max_dot_crawl;
194 };
195 
196 static struct psb_intel_sdvo *to_psb_intel_sdvo(struct drm_encoder *encoder)
197 {
198 	return container_of(encoder, struct psb_intel_sdvo, base.base);
199 }
200 
201 static struct psb_intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
202 {
203 	return container_of(gma_attached_encoder(connector),
204 			    struct psb_intel_sdvo, base);
205 }
206 
207 static struct psb_intel_sdvo_connector *to_psb_intel_sdvo_connector(struct drm_connector *connector)
208 {
209 	return container_of(to_gma_connector(connector), struct psb_intel_sdvo_connector, base);
210 }
211 
212 static bool
213 psb_intel_sdvo_output_setup(struct psb_intel_sdvo *psb_intel_sdvo, uint16_t flags);
214 static bool
215 psb_intel_sdvo_tv_create_property(struct psb_intel_sdvo *psb_intel_sdvo,
216 			      struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
217 			      int type);
218 static bool
219 psb_intel_sdvo_create_enhance_property(struct psb_intel_sdvo *psb_intel_sdvo,
220 				   struct psb_intel_sdvo_connector *psb_intel_sdvo_connector);
221 
222 /**
223  * Writes the SDVOB or SDVOC with the given value, but always writes both
224  * SDVOB and SDVOC to work around apparent hardware issues (according to
225  * comments in the BIOS).
226  */
227 static void psb_intel_sdvo_write_sdvox(struct psb_intel_sdvo *psb_intel_sdvo, u32 val)
228 {
229 	struct drm_device *dev = psb_intel_sdvo->base.base.dev;
230 	u32 bval = val, cval = val;
231 	int i, j;
232 	int need_aux = IS_MRST(dev) ? 1 : 0;
233 
234 	for (j = 0; j <= need_aux; j++) {
235 		if (psb_intel_sdvo->sdvo_reg == SDVOB)
236 			cval = REG_READ_WITH_AUX(SDVOC, j);
237 		else
238 			bval = REG_READ_WITH_AUX(SDVOB, j);
239 
240 		/*
241 		* Write the registers twice for luck. Sometimes,
242 		* writing them only once doesn't appear to 'stick'.
243 		* The BIOS does this too. Yay, magic
244 		*/
245 		for (i = 0; i < 2; i++) {
246 			REG_WRITE_WITH_AUX(SDVOB, bval, j);
247 			REG_READ_WITH_AUX(SDVOB, j);
248 			REG_WRITE_WITH_AUX(SDVOC, cval, j);
249 			REG_READ_WITH_AUX(SDVOC, j);
250 		}
251 	}
252 }
253 
254 static bool psb_intel_sdvo_read_byte(struct psb_intel_sdvo *psb_intel_sdvo, u8 addr, u8 *ch)
255 {
256 	struct i2c_msg msgs[] = {
257 		{
258 			.addr = psb_intel_sdvo->slave_addr,
259 			.flags = 0,
260 			.len = 1,
261 			.buf = &addr,
262 		},
263 		{
264 			.addr = psb_intel_sdvo->slave_addr,
265 			.flags = I2C_M_RD,
266 			.len = 1,
267 			.buf = ch,
268 		}
269 	};
270 	int ret;
271 
272 	if ((ret = i2c_transfer(psb_intel_sdvo->i2c, msgs, 2)) == 2)
273 		return true;
274 
275 	DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
276 	return false;
277 }
278 
279 #define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
280 /** Mapping of command numbers to names, for debug output */
281 static const struct _sdvo_cmd_name {
282 	u8 cmd;
283 	const char *name;
284 } sdvo_cmd_names[] = {
285     SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
286     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
287     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
288     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
289     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
290     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
291     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
292     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
293     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
294     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
295     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
296     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
297     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
298     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
299     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
300     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
301     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
302     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
303     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
304     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
305     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
306     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
307     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
308     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
309     SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
310     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
311     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
312     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
313     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
314     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
315     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
316     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
317     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
318     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
319     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
320     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
321     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
322     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
323     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
324     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
325     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
326     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
327     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
328 
329     /* Add the op code for SDVO enhancements */
330     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
331     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
332     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
333     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
334     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
335     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
336     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
337     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
338     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
339     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
340     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
341     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
342     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
343     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
344     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
345     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
346     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
347     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
348     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
349     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
350     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
351     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
352     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
353     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
354     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
355     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
356     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
357     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
358     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
359     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
360     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
361     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
362     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
363     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
364     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
365     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
366     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
367     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
368     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
369     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
370     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
371     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
372     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
373     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
374 
375     /* HDMI op code */
376     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
377     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
378     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
379     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
380     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
381     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
382     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
383     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
384     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
385     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
386     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
387     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
388     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
389     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
390     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
391     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
392     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
393     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
394     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
395     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
396 };
397 
398 #define IS_SDVOB(reg)	(reg == SDVOB)
399 #define SDVO_NAME(svdo) (IS_SDVOB((svdo)->sdvo_reg) ? "SDVOB" : "SDVOC")
400 
401 static void psb_intel_sdvo_debug_write(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
402 				   const void *args, int args_len)
403 {
404 	int i;
405 
406 	DRM_DEBUG_KMS("%s: W: %02X ",
407 				SDVO_NAME(psb_intel_sdvo), cmd);
408 	for (i = 0; i < args_len; i++)
409 		DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
410 	for (; i < 8; i++)
411 		DRM_LOG_KMS("   ");
412 	for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
413 		if (cmd == sdvo_cmd_names[i].cmd) {
414 			DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
415 			break;
416 		}
417 	}
418 	if (i == ARRAY_SIZE(sdvo_cmd_names))
419 		DRM_LOG_KMS("(%02X)", cmd);
420 	DRM_LOG_KMS("\n");
421 }
422 
423 static const char *cmd_status_names[] = {
424 	"Power on",
425 	"Success",
426 	"Not supported",
427 	"Invalid arg",
428 	"Pending",
429 	"Target not specified",
430 	"Scaling not supported"
431 };
432 
433 static bool psb_intel_sdvo_write_cmd(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
434 				 const void *args, int args_len)
435 {
436 	u8 buf[args_len*2 + 2], status;
437 	struct i2c_msg msgs[args_len + 3];
438 	int i, ret;
439 
440 	psb_intel_sdvo_debug_write(psb_intel_sdvo, cmd, args, args_len);
441 
442 	for (i = 0; i < args_len; i++) {
443 		msgs[i].addr = psb_intel_sdvo->slave_addr;
444 		msgs[i].flags = 0;
445 		msgs[i].len = 2;
446 		msgs[i].buf = buf + 2 *i;
447 		buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
448 		buf[2*i + 1] = ((u8*)args)[i];
449 	}
450 	msgs[i].addr = psb_intel_sdvo->slave_addr;
451 	msgs[i].flags = 0;
452 	msgs[i].len = 2;
453 	msgs[i].buf = buf + 2*i;
454 	buf[2*i + 0] = SDVO_I2C_OPCODE;
455 	buf[2*i + 1] = cmd;
456 
457 	/* the following two are to read the response */
458 	status = SDVO_I2C_CMD_STATUS;
459 	msgs[i+1].addr = psb_intel_sdvo->slave_addr;
460 	msgs[i+1].flags = 0;
461 	msgs[i+1].len = 1;
462 	msgs[i+1].buf = &status;
463 
464 	msgs[i+2].addr = psb_intel_sdvo->slave_addr;
465 	msgs[i+2].flags = I2C_M_RD;
466 	msgs[i+2].len = 1;
467 	msgs[i+2].buf = &status;
468 
469 	ret = i2c_transfer(psb_intel_sdvo->i2c, msgs, i+3);
470 	if (ret < 0) {
471 		DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
472 		return false;
473 	}
474 	if (ret != i+3) {
475 		/* failure in I2C transfer */
476 		DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
477 		return false;
478 	}
479 
480 	return true;
481 }
482 
483 static bool psb_intel_sdvo_read_response(struct psb_intel_sdvo *psb_intel_sdvo,
484 				     void *response, int response_len)
485 {
486 	u8 retry = 5;
487 	u8 status;
488 	int i;
489 
490 	DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(psb_intel_sdvo));
491 
492 	/*
493 	 * The documentation states that all commands will be
494 	 * processed within 15µs, and that we need only poll
495 	 * the status byte a maximum of 3 times in order for the
496 	 * command to be complete.
497 	 *
498 	 * Check 5 times in case the hardware failed to read the docs.
499 	 */
500 	if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
501 				  SDVO_I2C_CMD_STATUS,
502 				  &status))
503 		goto log_fail;
504 
505 	while ((status == SDVO_CMD_STATUS_PENDING ||
506 		status == SDVO_CMD_STATUS_TARGET_NOT_SPECIFIED) && retry--) {
507 		udelay(15);
508 		if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
509 					  SDVO_I2C_CMD_STATUS,
510 					  &status))
511 			goto log_fail;
512 	}
513 
514 	if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
515 		DRM_LOG_KMS("(%s)", cmd_status_names[status]);
516 	else
517 		DRM_LOG_KMS("(??? %d)", status);
518 
519 	if (status != SDVO_CMD_STATUS_SUCCESS)
520 		goto log_fail;
521 
522 	/* Read the command response */
523 	for (i = 0; i < response_len; i++) {
524 		if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
525 					  SDVO_I2C_RETURN_0 + i,
526 					  &((u8 *)response)[i]))
527 			goto log_fail;
528 		DRM_LOG_KMS(" %02X", ((u8 *)response)[i]);
529 	}
530 	DRM_LOG_KMS("\n");
531 	return true;
532 
533 log_fail:
534 	DRM_LOG_KMS("... failed\n");
535 	return false;
536 }
537 
538 static int psb_intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
539 {
540 	if (mode->clock >= 100000)
541 		return 1;
542 	else if (mode->clock >= 50000)
543 		return 2;
544 	else
545 		return 4;
546 }
547 
548 static bool psb_intel_sdvo_set_control_bus_switch(struct psb_intel_sdvo *psb_intel_sdvo,
549 					      u8 ddc_bus)
550 {
551 	/* This must be the immediately preceding write before the i2c xfer */
552 	return psb_intel_sdvo_write_cmd(psb_intel_sdvo,
553 				    SDVO_CMD_SET_CONTROL_BUS_SWITCH,
554 				    &ddc_bus, 1);
555 }
556 
557 static bool psb_intel_sdvo_set_value(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd, const void *data, int len)
558 {
559 	if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo, cmd, data, len))
560 		return false;
561 
562 	return psb_intel_sdvo_read_response(psb_intel_sdvo, NULL, 0);
563 }
564 
565 static bool
566 psb_intel_sdvo_get_value(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd, void *value, int len)
567 {
568 	if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo, cmd, NULL, 0))
569 		return false;
570 
571 	return psb_intel_sdvo_read_response(psb_intel_sdvo, value, len);
572 }
573 
574 static bool psb_intel_sdvo_set_target_input(struct psb_intel_sdvo *psb_intel_sdvo)
575 {
576 	struct psb_intel_sdvo_set_target_input_args targets = {0};
577 	return psb_intel_sdvo_set_value(psb_intel_sdvo,
578 				    SDVO_CMD_SET_TARGET_INPUT,
579 				    &targets, sizeof(targets));
580 }
581 
582 /**
583  * Return whether each input is trained.
584  *
585  * This function is making an assumption about the layout of the response,
586  * which should be checked against the docs.
587  */
588 static bool psb_intel_sdvo_get_trained_inputs(struct psb_intel_sdvo *psb_intel_sdvo, bool *input_1, bool *input_2)
589 {
590 	struct psb_intel_sdvo_get_trained_inputs_response response;
591 
592 	BUILD_BUG_ON(sizeof(response) != 1);
593 	if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
594 				  &response, sizeof(response)))
595 		return false;
596 
597 	*input_1 = response.input0_trained;
598 	*input_2 = response.input1_trained;
599 	return true;
600 }
601 
602 static bool psb_intel_sdvo_set_active_outputs(struct psb_intel_sdvo *psb_intel_sdvo,
603 					  u16 outputs)
604 {
605 	return psb_intel_sdvo_set_value(psb_intel_sdvo,
606 				    SDVO_CMD_SET_ACTIVE_OUTPUTS,
607 				    &outputs, sizeof(outputs));
608 }
609 
610 static bool psb_intel_sdvo_set_encoder_power_state(struct psb_intel_sdvo *psb_intel_sdvo,
611 					       int mode)
612 {
613 	u8 state = SDVO_ENCODER_STATE_ON;
614 
615 	switch (mode) {
616 	case DRM_MODE_DPMS_ON:
617 		state = SDVO_ENCODER_STATE_ON;
618 		break;
619 	case DRM_MODE_DPMS_STANDBY:
620 		state = SDVO_ENCODER_STATE_STANDBY;
621 		break;
622 	case DRM_MODE_DPMS_SUSPEND:
623 		state = SDVO_ENCODER_STATE_SUSPEND;
624 		break;
625 	case DRM_MODE_DPMS_OFF:
626 		state = SDVO_ENCODER_STATE_OFF;
627 		break;
628 	}
629 
630 	return psb_intel_sdvo_set_value(psb_intel_sdvo,
631 				    SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
632 }
633 
634 static bool psb_intel_sdvo_get_input_pixel_clock_range(struct psb_intel_sdvo *psb_intel_sdvo,
635 						   int *clock_min,
636 						   int *clock_max)
637 {
638 	struct psb_intel_sdvo_pixel_clock_range clocks;
639 
640 	BUILD_BUG_ON(sizeof(clocks) != 4);
641 	if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
642 				  SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
643 				  &clocks, sizeof(clocks)))
644 		return false;
645 
646 	/* Convert the values from units of 10 kHz to kHz. */
647 	*clock_min = clocks.min * 10;
648 	*clock_max = clocks.max * 10;
649 	return true;
650 }
651 
652 static bool psb_intel_sdvo_set_target_output(struct psb_intel_sdvo *psb_intel_sdvo,
653 					 u16 outputs)
654 {
655 	return psb_intel_sdvo_set_value(psb_intel_sdvo,
656 				    SDVO_CMD_SET_TARGET_OUTPUT,
657 				    &outputs, sizeof(outputs));
658 }
659 
660 static bool psb_intel_sdvo_set_timing(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
661 				  struct psb_intel_sdvo_dtd *dtd)
662 {
663 	return psb_intel_sdvo_set_value(psb_intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
664 		psb_intel_sdvo_set_value(psb_intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
665 }
666 
667 static bool psb_intel_sdvo_set_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
668 					 struct psb_intel_sdvo_dtd *dtd)
669 {
670 	return psb_intel_sdvo_set_timing(psb_intel_sdvo,
671 				     SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
672 }
673 
674 static bool psb_intel_sdvo_set_output_timing(struct psb_intel_sdvo *psb_intel_sdvo,
675 					 struct psb_intel_sdvo_dtd *dtd)
676 {
677 	return psb_intel_sdvo_set_timing(psb_intel_sdvo,
678 				     SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
679 }
680 
681 static bool
682 psb_intel_sdvo_create_preferred_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
683 					 uint16_t clock,
684 					 uint16_t width,
685 					 uint16_t height)
686 {
687 	struct psb_intel_sdvo_preferred_input_timing_args args;
688 
689 	memset(&args, 0, sizeof(args));
690 	args.clock = clock;
691 	args.width = width;
692 	args.height = height;
693 	args.interlace = 0;
694 
695 	if (psb_intel_sdvo->is_lvds &&
696 	   (psb_intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
697 	    psb_intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
698 		args.scaled = 1;
699 
700 	return psb_intel_sdvo_set_value(psb_intel_sdvo,
701 				    SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
702 				    &args, sizeof(args));
703 }
704 
705 static bool psb_intel_sdvo_get_preferred_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
706 						  struct psb_intel_sdvo_dtd *dtd)
707 {
708 	BUILD_BUG_ON(sizeof(dtd->part1) != 8);
709 	BUILD_BUG_ON(sizeof(dtd->part2) != 8);
710 	return psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
711 				    &dtd->part1, sizeof(dtd->part1)) &&
712 		psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
713 				     &dtd->part2, sizeof(dtd->part2));
714 }
715 
716 static bool psb_intel_sdvo_set_clock_rate_mult(struct psb_intel_sdvo *psb_intel_sdvo, u8 val)
717 {
718 	return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
719 }
720 
721 static void psb_intel_sdvo_get_dtd_from_mode(struct psb_intel_sdvo_dtd *dtd,
722 					 const struct drm_display_mode *mode)
723 {
724 	uint16_t width, height;
725 	uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
726 	uint16_t h_sync_offset, v_sync_offset;
727 
728 	width = mode->crtc_hdisplay;
729 	height = mode->crtc_vdisplay;
730 
731 	/* do some mode translations */
732 	h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
733 	h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
734 
735 	v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
736 	v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
737 
738 	h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
739 	v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
740 
741 	dtd->part1.clock = mode->clock / 10;
742 	dtd->part1.h_active = width & 0xff;
743 	dtd->part1.h_blank = h_blank_len & 0xff;
744 	dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
745 		((h_blank_len >> 8) & 0xf);
746 	dtd->part1.v_active = height & 0xff;
747 	dtd->part1.v_blank = v_blank_len & 0xff;
748 	dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
749 		((v_blank_len >> 8) & 0xf);
750 
751 	dtd->part2.h_sync_off = h_sync_offset & 0xff;
752 	dtd->part2.h_sync_width = h_sync_len & 0xff;
753 	dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
754 		(v_sync_len & 0xf);
755 	dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
756 		((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
757 		((v_sync_len & 0x30) >> 4);
758 
759 	dtd->part2.dtd_flags = 0x18;
760 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
761 		dtd->part2.dtd_flags |= 0x2;
762 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
763 		dtd->part2.dtd_flags |= 0x4;
764 
765 	dtd->part2.sdvo_flags = 0;
766 	dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
767 	dtd->part2.reserved = 0;
768 }
769 
770 static void psb_intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
771 					 const struct psb_intel_sdvo_dtd *dtd)
772 {
773 	mode->hdisplay = dtd->part1.h_active;
774 	mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
775 	mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
776 	mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
777 	mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
778 	mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
779 	mode->htotal = mode->hdisplay + dtd->part1.h_blank;
780 	mode->htotal += (dtd->part1.h_high & 0xf) << 8;
781 
782 	mode->vdisplay = dtd->part1.v_active;
783 	mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
784 	mode->vsync_start = mode->vdisplay;
785 	mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
786 	mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
787 	mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
788 	mode->vsync_end = mode->vsync_start +
789 		(dtd->part2.v_sync_off_width & 0xf);
790 	mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
791 	mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
792 	mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
793 
794 	mode->clock = dtd->part1.clock * 10;
795 
796 	mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
797 	if (dtd->part2.dtd_flags & 0x2)
798 		mode->flags |= DRM_MODE_FLAG_PHSYNC;
799 	if (dtd->part2.dtd_flags & 0x4)
800 		mode->flags |= DRM_MODE_FLAG_PVSYNC;
801 }
802 
803 static bool psb_intel_sdvo_check_supp_encode(struct psb_intel_sdvo *psb_intel_sdvo)
804 {
805 	struct psb_intel_sdvo_encode encode;
806 
807 	BUILD_BUG_ON(sizeof(encode) != 2);
808 	return psb_intel_sdvo_get_value(psb_intel_sdvo,
809 				  SDVO_CMD_GET_SUPP_ENCODE,
810 				  &encode, sizeof(encode));
811 }
812 
813 static bool psb_intel_sdvo_set_encode(struct psb_intel_sdvo *psb_intel_sdvo,
814 				  uint8_t mode)
815 {
816 	return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
817 }
818 
819 static bool psb_intel_sdvo_set_colorimetry(struct psb_intel_sdvo *psb_intel_sdvo,
820 				       uint8_t mode)
821 {
822 	return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
823 }
824 
825 #if 0
826 static void psb_intel_sdvo_dump_hdmi_buf(struct psb_intel_sdvo *psb_intel_sdvo)
827 {
828 	int i, j;
829 	uint8_t set_buf_index[2];
830 	uint8_t av_split;
831 	uint8_t buf_size;
832 	uint8_t buf[48];
833 	uint8_t *pos;
834 
835 	psb_intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
836 
837 	for (i = 0; i <= av_split; i++) {
838 		set_buf_index[0] = i; set_buf_index[1] = 0;
839 		psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
840 				     set_buf_index, 2);
841 		psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
842 		psb_intel_sdvo_read_response(encoder, &buf_size, 1);
843 
844 		pos = buf;
845 		for (j = 0; j <= buf_size; j += 8) {
846 			psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
847 					     NULL, 0);
848 			psb_intel_sdvo_read_response(encoder, pos, 8);
849 			pos += 8;
850 		}
851 	}
852 }
853 #endif
854 
855 static bool psb_intel_sdvo_set_avi_infoframe(struct psb_intel_sdvo *psb_intel_sdvo)
856 {
857 	DRM_INFO("HDMI is not supported yet");
858 
859 	return false;
860 #if 0
861 	struct dip_infoframe avi_if = {
862 		.type = DIP_TYPE_AVI,
863 		.ver = DIP_VERSION_AVI,
864 		.len = DIP_LEN_AVI,
865 	};
866 	uint8_t tx_rate = SDVO_HBUF_TX_VSYNC;
867 	uint8_t set_buf_index[2] = { 1, 0 };
868 	uint64_t *data = (uint64_t *)&avi_if;
869 	unsigned i;
870 
871 	intel_dip_infoframe_csum(&avi_if);
872 
873 	if (!psb_intel_sdvo_set_value(psb_intel_sdvo,
874 				  SDVO_CMD_SET_HBUF_INDEX,
875 				  set_buf_index, 2))
876 		return false;
877 
878 	for (i = 0; i < sizeof(avi_if); i += 8) {
879 		if (!psb_intel_sdvo_set_value(psb_intel_sdvo,
880 					  SDVO_CMD_SET_HBUF_DATA,
881 					  data, 8))
882 			return false;
883 		data++;
884 	}
885 
886 	return psb_intel_sdvo_set_value(psb_intel_sdvo,
887 				    SDVO_CMD_SET_HBUF_TXRATE,
888 				    &tx_rate, 1);
889 #endif
890 }
891 
892 static bool psb_intel_sdvo_set_tv_format(struct psb_intel_sdvo *psb_intel_sdvo)
893 {
894 	struct psb_intel_sdvo_tv_format format;
895 	uint32_t format_map;
896 
897 	format_map = 1 << psb_intel_sdvo->tv_format_index;
898 	memset(&format, 0, sizeof(format));
899 	memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
900 
901 	BUILD_BUG_ON(sizeof(format) != 6);
902 	return psb_intel_sdvo_set_value(psb_intel_sdvo,
903 				    SDVO_CMD_SET_TV_FORMAT,
904 				    &format, sizeof(format));
905 }
906 
907 static bool
908 psb_intel_sdvo_set_output_timings_from_mode(struct psb_intel_sdvo *psb_intel_sdvo,
909 					const struct drm_display_mode *mode)
910 {
911 	struct psb_intel_sdvo_dtd output_dtd;
912 
913 	if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
914 					  psb_intel_sdvo->attached_output))
915 		return false;
916 
917 	psb_intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
918 	if (!psb_intel_sdvo_set_output_timing(psb_intel_sdvo, &output_dtd))
919 		return false;
920 
921 	return true;
922 }
923 
924 static bool
925 psb_intel_sdvo_set_input_timings_for_mode(struct psb_intel_sdvo *psb_intel_sdvo,
926 					const struct drm_display_mode *mode,
927 					struct drm_display_mode *adjusted_mode)
928 {
929 	/* Reset the input timing to the screen. Assume always input 0. */
930 	if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
931 		return false;
932 
933 	if (!psb_intel_sdvo_create_preferred_input_timing(psb_intel_sdvo,
934 						      mode->clock / 10,
935 						      mode->hdisplay,
936 						      mode->vdisplay))
937 		return false;
938 
939 	if (!psb_intel_sdvo_get_preferred_input_timing(psb_intel_sdvo,
940 						   &psb_intel_sdvo->input_dtd))
941 		return false;
942 
943 	psb_intel_sdvo_get_mode_from_dtd(adjusted_mode, &psb_intel_sdvo->input_dtd);
944 
945 	drm_mode_set_crtcinfo(adjusted_mode, 0);
946 	return true;
947 }
948 
949 static bool psb_intel_sdvo_mode_fixup(struct drm_encoder *encoder,
950 				  const struct drm_display_mode *mode,
951 				  struct drm_display_mode *adjusted_mode)
952 {
953 	struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
954 	int multiplier;
955 
956 	/* We need to construct preferred input timings based on our
957 	 * output timings.  To do that, we have to set the output
958 	 * timings, even though this isn't really the right place in
959 	 * the sequence to do it. Oh well.
960 	 */
961 	if (psb_intel_sdvo->is_tv) {
962 		if (!psb_intel_sdvo_set_output_timings_from_mode(psb_intel_sdvo, mode))
963 			return false;
964 
965 		(void) psb_intel_sdvo_set_input_timings_for_mode(psb_intel_sdvo,
966 							     mode,
967 							     adjusted_mode);
968 	} else if (psb_intel_sdvo->is_lvds) {
969 		if (!psb_intel_sdvo_set_output_timings_from_mode(psb_intel_sdvo,
970 							     psb_intel_sdvo->sdvo_lvds_fixed_mode))
971 			return false;
972 
973 		(void) psb_intel_sdvo_set_input_timings_for_mode(psb_intel_sdvo,
974 							     mode,
975 							     adjusted_mode);
976 	}
977 
978 	/* Make the CRTC code factor in the SDVO pixel multiplier.  The
979 	 * SDVO device will factor out the multiplier during mode_set.
980 	 */
981 	multiplier = psb_intel_sdvo_get_pixel_multiplier(adjusted_mode);
982 	psb_intel_mode_set_pixel_multiplier(adjusted_mode, multiplier);
983 
984 	return true;
985 }
986 
987 static void psb_intel_sdvo_mode_set(struct drm_encoder *encoder,
988 				struct drm_display_mode *mode,
989 				struct drm_display_mode *adjusted_mode)
990 {
991 	struct drm_device *dev = encoder->dev;
992 	struct drm_crtc *crtc = encoder->crtc;
993 	struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
994 	struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
995 	u32 sdvox;
996 	struct psb_intel_sdvo_in_out_map in_out;
997 	struct psb_intel_sdvo_dtd input_dtd;
998 	int pixel_multiplier = psb_intel_mode_get_pixel_multiplier(adjusted_mode);
999 	int rate;
1000 	int need_aux = IS_MRST(dev) ? 1 : 0;
1001 
1002 	if (!mode)
1003 		return;
1004 
1005 	/* First, set the input mapping for the first input to our controlled
1006 	 * output. This is only correct if we're a single-input device, in
1007 	 * which case the first input is the output from the appropriate SDVO
1008 	 * channel on the motherboard.  In a two-input device, the first input
1009 	 * will be SDVOB and the second SDVOC.
1010 	 */
1011 	in_out.in0 = psb_intel_sdvo->attached_output;
1012 	in_out.in1 = 0;
1013 
1014 	psb_intel_sdvo_set_value(psb_intel_sdvo,
1015 			     SDVO_CMD_SET_IN_OUT_MAP,
1016 			     &in_out, sizeof(in_out));
1017 
1018 	/* Set the output timings to the screen */
1019 	if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
1020 					  psb_intel_sdvo->attached_output))
1021 		return;
1022 
1023 	/* We have tried to get input timing in mode_fixup, and filled into
1024 	 * adjusted_mode.
1025 	 */
1026 	if (psb_intel_sdvo->is_tv || psb_intel_sdvo->is_lvds) {
1027 		input_dtd = psb_intel_sdvo->input_dtd;
1028 	} else {
1029 		/* Set the output timing to the screen */
1030 		if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
1031 						  psb_intel_sdvo->attached_output))
1032 			return;
1033 
1034 		psb_intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
1035 		(void) psb_intel_sdvo_set_output_timing(psb_intel_sdvo, &input_dtd);
1036 	}
1037 
1038 	/* Set the input timing to the screen. Assume always input 0. */
1039 	if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
1040 		return;
1041 
1042 	if (psb_intel_sdvo->has_hdmi_monitor) {
1043 		psb_intel_sdvo_set_encode(psb_intel_sdvo, SDVO_ENCODE_HDMI);
1044 		psb_intel_sdvo_set_colorimetry(psb_intel_sdvo,
1045 					   SDVO_COLORIMETRY_RGB256);
1046 		psb_intel_sdvo_set_avi_infoframe(psb_intel_sdvo);
1047 	} else
1048 		psb_intel_sdvo_set_encode(psb_intel_sdvo, SDVO_ENCODE_DVI);
1049 
1050 	if (psb_intel_sdvo->is_tv &&
1051 	    !psb_intel_sdvo_set_tv_format(psb_intel_sdvo))
1052 		return;
1053 
1054 	(void) psb_intel_sdvo_set_input_timing(psb_intel_sdvo, &input_dtd);
1055 
1056 	switch (pixel_multiplier) {
1057 	default:
1058 	case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
1059 	case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
1060 	case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
1061 	}
1062 	if (!psb_intel_sdvo_set_clock_rate_mult(psb_intel_sdvo, rate))
1063 		return;
1064 
1065 	/* Set the SDVO control regs. */
1066 	if (need_aux)
1067 		sdvox = REG_READ_AUX(psb_intel_sdvo->sdvo_reg);
1068 	else
1069 		sdvox = REG_READ(psb_intel_sdvo->sdvo_reg);
1070 
1071 	switch (psb_intel_sdvo->sdvo_reg) {
1072 	case SDVOB:
1073 		sdvox &= SDVOB_PRESERVE_MASK;
1074 		break;
1075 	case SDVOC:
1076 		sdvox &= SDVOC_PRESERVE_MASK;
1077 		break;
1078 	}
1079 	sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1080 
1081 	if (gma_crtc->pipe == 1)
1082 		sdvox |= SDVO_PIPE_B_SELECT;
1083 	if (psb_intel_sdvo->has_hdmi_audio)
1084 		sdvox |= SDVO_AUDIO_ENABLE;
1085 
1086 	/* FIXME: Check if this is needed for PSB
1087 	sdvox |= (pixel_multiplier - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1088 	*/
1089 
1090 	if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL)
1091 		sdvox |= SDVO_STALL_SELECT;
1092 	psb_intel_sdvo_write_sdvox(psb_intel_sdvo, sdvox);
1093 }
1094 
1095 static void psb_intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1096 {
1097 	struct drm_device *dev = encoder->dev;
1098 	struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
1099 	u32 temp;
1100 	int i;
1101 	int need_aux = IS_MRST(dev) ? 1 : 0;
1102 
1103 	switch (mode) {
1104 	case DRM_MODE_DPMS_ON:
1105 		DRM_DEBUG("DPMS_ON");
1106 		break;
1107 	case DRM_MODE_DPMS_OFF:
1108 		DRM_DEBUG("DPMS_OFF");
1109 		break;
1110 	default:
1111 		DRM_DEBUG("DPMS: %d", mode);
1112 	}
1113 
1114 	if (mode != DRM_MODE_DPMS_ON) {
1115 		psb_intel_sdvo_set_active_outputs(psb_intel_sdvo, 0);
1116 		if (0)
1117 			psb_intel_sdvo_set_encoder_power_state(psb_intel_sdvo, mode);
1118 
1119 		if (mode == DRM_MODE_DPMS_OFF) {
1120 			if (need_aux)
1121 				temp = REG_READ_AUX(psb_intel_sdvo->sdvo_reg);
1122 			else
1123 				temp = REG_READ(psb_intel_sdvo->sdvo_reg);
1124 
1125 			if ((temp & SDVO_ENABLE) != 0) {
1126 				psb_intel_sdvo_write_sdvox(psb_intel_sdvo, temp & ~SDVO_ENABLE);
1127 			}
1128 		}
1129 	} else {
1130 		bool input1, input2;
1131 		u8 status;
1132 
1133 		if (need_aux)
1134 			temp = REG_READ_AUX(psb_intel_sdvo->sdvo_reg);
1135 		else
1136 			temp = REG_READ(psb_intel_sdvo->sdvo_reg);
1137 
1138 		if ((temp & SDVO_ENABLE) == 0)
1139 			psb_intel_sdvo_write_sdvox(psb_intel_sdvo, temp | SDVO_ENABLE);
1140 
1141 		for (i = 0; i < 2; i++)
1142 			gma_wait_for_vblank(dev);
1143 
1144 		status = psb_intel_sdvo_get_trained_inputs(psb_intel_sdvo, &input1, &input2);
1145 		/* Warn if the device reported failure to sync.
1146 		 * A lot of SDVO devices fail to notify of sync, but it's
1147 		 * a given it the status is a success, we succeeded.
1148 		 */
1149 		if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1150 			DRM_DEBUG_KMS("First %s output reported failure to "
1151 					"sync\n", SDVO_NAME(psb_intel_sdvo));
1152 		}
1153 
1154 		if (0)
1155 			psb_intel_sdvo_set_encoder_power_state(psb_intel_sdvo, mode);
1156 		psb_intel_sdvo_set_active_outputs(psb_intel_sdvo, psb_intel_sdvo->attached_output);
1157 	}
1158 	return;
1159 }
1160 
1161 static int psb_intel_sdvo_mode_valid(struct drm_connector *connector,
1162 				 struct drm_display_mode *mode)
1163 {
1164 	struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1165 
1166 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1167 		return MODE_NO_DBLESCAN;
1168 
1169 	if (psb_intel_sdvo->pixel_clock_min > mode->clock)
1170 		return MODE_CLOCK_LOW;
1171 
1172 	if (psb_intel_sdvo->pixel_clock_max < mode->clock)
1173 		return MODE_CLOCK_HIGH;
1174 
1175 	if (psb_intel_sdvo->is_lvds) {
1176 		if (mode->hdisplay > psb_intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
1177 			return MODE_PANEL;
1178 
1179 		if (mode->vdisplay > psb_intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
1180 			return MODE_PANEL;
1181 	}
1182 
1183 	return MODE_OK;
1184 }
1185 
1186 static bool psb_intel_sdvo_get_capabilities(struct psb_intel_sdvo *psb_intel_sdvo, struct psb_intel_sdvo_caps *caps)
1187 {
1188 	BUILD_BUG_ON(sizeof(*caps) != 8);
1189 	if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
1190 				  SDVO_CMD_GET_DEVICE_CAPS,
1191 				  caps, sizeof(*caps)))
1192 		return false;
1193 
1194 	DRM_DEBUG_KMS("SDVO capabilities:\n"
1195 		      "  vendor_id: %d\n"
1196 		      "  device_id: %d\n"
1197 		      "  device_rev_id: %d\n"
1198 		      "  sdvo_version_major: %d\n"
1199 		      "  sdvo_version_minor: %d\n"
1200 		      "  sdvo_inputs_mask: %d\n"
1201 		      "  smooth_scaling: %d\n"
1202 		      "  sharp_scaling: %d\n"
1203 		      "  up_scaling: %d\n"
1204 		      "  down_scaling: %d\n"
1205 		      "  stall_support: %d\n"
1206 		      "  output_flags: %d\n",
1207 		      caps->vendor_id,
1208 		      caps->device_id,
1209 		      caps->device_rev_id,
1210 		      caps->sdvo_version_major,
1211 		      caps->sdvo_version_minor,
1212 		      caps->sdvo_inputs_mask,
1213 		      caps->smooth_scaling,
1214 		      caps->sharp_scaling,
1215 		      caps->up_scaling,
1216 		      caps->down_scaling,
1217 		      caps->stall_support,
1218 		      caps->output_flags);
1219 
1220 	return true;
1221 }
1222 
1223 /* No use! */
1224 #if 0
1225 struct drm_connector* psb_intel_sdvo_find(struct drm_device *dev, int sdvoB)
1226 {
1227 	struct drm_connector *connector = NULL;
1228 	struct psb_intel_sdvo *iout = NULL;
1229 	struct psb_intel_sdvo *sdvo;
1230 
1231 	/* find the sdvo connector */
1232 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1233 		iout = to_psb_intel_sdvo(connector);
1234 
1235 		if (iout->type != INTEL_OUTPUT_SDVO)
1236 			continue;
1237 
1238 		sdvo = iout->dev_priv;
1239 
1240 		if (sdvo->sdvo_reg == SDVOB && sdvoB)
1241 			return connector;
1242 
1243 		if (sdvo->sdvo_reg == SDVOC && !sdvoB)
1244 			return connector;
1245 
1246 	}
1247 
1248 	return NULL;
1249 }
1250 
1251 int psb_intel_sdvo_supports_hotplug(struct drm_connector *connector)
1252 {
1253 	u8 response[2];
1254 	u8 status;
1255 	struct psb_intel_sdvo *psb_intel_sdvo;
1256 	DRM_DEBUG_KMS("\n");
1257 
1258 	if (!connector)
1259 		return 0;
1260 
1261 	psb_intel_sdvo = to_psb_intel_sdvo(connector);
1262 
1263 	return psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1264 				    &response, 2) && response[0];
1265 }
1266 
1267 void psb_intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1268 {
1269 	u8 response[2];
1270 	u8 status;
1271 	struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(connector);
1272 
1273 	psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1274 	psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1275 
1276 	if (on) {
1277 		psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1278 		status = psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1279 
1280 		psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1281 	} else {
1282 		response[0] = 0;
1283 		response[1] = 0;
1284 		psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1285 	}
1286 
1287 	psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1288 	psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1289 }
1290 #endif
1291 
1292 static bool
1293 psb_intel_sdvo_multifunc_encoder(struct psb_intel_sdvo *psb_intel_sdvo)
1294 {
1295 	/* Is there more than one type of output? */
1296 	int caps = psb_intel_sdvo->caps.output_flags & 0xf;
1297 	return caps & -caps;
1298 }
1299 
1300 static struct edid *
1301 psb_intel_sdvo_get_edid(struct drm_connector *connector)
1302 {
1303 	struct psb_intel_sdvo *sdvo = intel_attached_sdvo(connector);
1304 	return drm_get_edid(connector, &sdvo->ddc);
1305 }
1306 
1307 /* Mac mini hack -- use the same DDC as the analog connector */
1308 static struct edid *
1309 psb_intel_sdvo_get_analog_edid(struct drm_connector *connector)
1310 {
1311 	struct drm_psb_private *dev_priv = connector->dev->dev_private;
1312 
1313 	return drm_get_edid(connector,
1314 			    &dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);
1315 }
1316 
1317 static enum drm_connector_status
1318 psb_intel_sdvo_hdmi_sink_detect(struct drm_connector *connector)
1319 {
1320 	struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1321 	enum drm_connector_status status;
1322 	struct edid *edid;
1323 
1324 	edid = psb_intel_sdvo_get_edid(connector);
1325 
1326 	if (edid == NULL && psb_intel_sdvo_multifunc_encoder(psb_intel_sdvo)) {
1327 		u8 ddc, saved_ddc = psb_intel_sdvo->ddc_bus;
1328 
1329 		/*
1330 		 * Don't use the 1 as the argument of DDC bus switch to get
1331 		 * the EDID. It is used for SDVO SPD ROM.
1332 		 */
1333 		for (ddc = psb_intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
1334 			psb_intel_sdvo->ddc_bus = ddc;
1335 			edid = psb_intel_sdvo_get_edid(connector);
1336 			if (edid)
1337 				break;
1338 		}
1339 		/*
1340 		 * If we found the EDID on the other bus,
1341 		 * assume that is the correct DDC bus.
1342 		 */
1343 		if (edid == NULL)
1344 			psb_intel_sdvo->ddc_bus = saved_ddc;
1345 	}
1346 
1347 	/*
1348 	 * When there is no edid and no monitor is connected with VGA
1349 	 * port, try to use the CRT ddc to read the EDID for DVI-connector.
1350 	 */
1351 	if (edid == NULL)
1352 		edid = psb_intel_sdvo_get_analog_edid(connector);
1353 
1354 	status = connector_status_unknown;
1355 	if (edid != NULL) {
1356 		/* DDC bus is shared, match EDID to connector type */
1357 		if (edid->input & DRM_EDID_INPUT_DIGITAL) {
1358 			status = connector_status_connected;
1359 			if (psb_intel_sdvo->is_hdmi) {
1360 				psb_intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
1361 				psb_intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
1362 			}
1363 		} else
1364 			status = connector_status_disconnected;
1365 		kfree(edid);
1366 	}
1367 
1368 	if (status == connector_status_connected) {
1369 		struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1370 		if (psb_intel_sdvo_connector->force_audio)
1371 			psb_intel_sdvo->has_hdmi_audio = psb_intel_sdvo_connector->force_audio > 0;
1372 	}
1373 
1374 	return status;
1375 }
1376 
1377 static enum drm_connector_status
1378 psb_intel_sdvo_detect(struct drm_connector *connector, bool force)
1379 {
1380 	uint16_t response;
1381 	struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1382 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1383 	enum drm_connector_status ret;
1384 
1385 	if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo,
1386 				  SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0))
1387 		return connector_status_unknown;
1388 
1389 	/* add 30ms delay when the output type might be TV */
1390 	if (psb_intel_sdvo->caps.output_flags &
1391 	    (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_CVBS0))
1392 		mdelay(30);
1393 
1394 	if (!psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2))
1395 		return connector_status_unknown;
1396 
1397 	DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
1398 		      response & 0xff, response >> 8,
1399 		      psb_intel_sdvo_connector->output_flag);
1400 
1401 	if (response == 0)
1402 		return connector_status_disconnected;
1403 
1404 	psb_intel_sdvo->attached_output = response;
1405 
1406 	psb_intel_sdvo->has_hdmi_monitor = false;
1407 	psb_intel_sdvo->has_hdmi_audio = false;
1408 
1409 	if ((psb_intel_sdvo_connector->output_flag & response) == 0)
1410 		ret = connector_status_disconnected;
1411 	else if (IS_TMDS(psb_intel_sdvo_connector))
1412 		ret = psb_intel_sdvo_hdmi_sink_detect(connector);
1413 	else {
1414 		struct edid *edid;
1415 
1416 		/* if we have an edid check it matches the connection */
1417 		edid = psb_intel_sdvo_get_edid(connector);
1418 		if (edid == NULL)
1419 			edid = psb_intel_sdvo_get_analog_edid(connector);
1420 		if (edid != NULL) {
1421 			if (edid->input & DRM_EDID_INPUT_DIGITAL)
1422 				ret = connector_status_disconnected;
1423 			else
1424 				ret = connector_status_connected;
1425 			kfree(edid);
1426 		} else
1427 			ret = connector_status_connected;
1428 	}
1429 
1430 	/* May update encoder flag for like clock for SDVO TV, etc.*/
1431 	if (ret == connector_status_connected) {
1432 		psb_intel_sdvo->is_tv = false;
1433 		psb_intel_sdvo->is_lvds = false;
1434 		psb_intel_sdvo->base.needs_tv_clock = false;
1435 
1436 		if (response & SDVO_TV_MASK) {
1437 			psb_intel_sdvo->is_tv = true;
1438 			psb_intel_sdvo->base.needs_tv_clock = true;
1439 		}
1440 		if (response & SDVO_LVDS_MASK)
1441 			psb_intel_sdvo->is_lvds = psb_intel_sdvo->sdvo_lvds_fixed_mode != NULL;
1442 	}
1443 
1444 	return ret;
1445 }
1446 
1447 static void psb_intel_sdvo_get_ddc_modes(struct drm_connector *connector)
1448 {
1449 	struct edid *edid;
1450 
1451 	/* set the bus switch and get the modes */
1452 	edid = psb_intel_sdvo_get_edid(connector);
1453 
1454 	/*
1455 	 * Mac mini hack.  On this device, the DVI-I connector shares one DDC
1456 	 * link between analog and digital outputs. So, if the regular SDVO
1457 	 * DDC fails, check to see if the analog output is disconnected, in
1458 	 * which case we'll look there for the digital DDC data.
1459 	 */
1460 	if (edid == NULL)
1461 		edid = psb_intel_sdvo_get_analog_edid(connector);
1462 
1463 	if (edid != NULL) {
1464 		struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1465 		bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
1466 		bool connector_is_digital = !!IS_TMDS(psb_intel_sdvo_connector);
1467 
1468 		if (connector_is_digital == monitor_is_digital) {
1469 			drm_mode_connector_update_edid_property(connector, edid);
1470 			drm_add_edid_modes(connector, edid);
1471 		}
1472 
1473 		kfree(edid);
1474 	}
1475 }
1476 
1477 /*
1478  * Set of SDVO TV modes.
1479  * Note!  This is in reply order (see loop in get_tv_modes).
1480  * XXX: all 60Hz refresh?
1481  */
1482 static const struct drm_display_mode sdvo_tv_modes[] = {
1483 	{ DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1484 		   416, 0, 200, 201, 232, 233, 0,
1485 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1486 	{ DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1487 		   416, 0, 240, 241, 272, 273, 0,
1488 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1489 	{ DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1490 		   496, 0, 300, 301, 332, 333, 0,
1491 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1492 	{ DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1493 		   736, 0, 350, 351, 382, 383, 0,
1494 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1495 	{ DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1496 		   736, 0, 400, 401, 432, 433, 0,
1497 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1498 	{ DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1499 		   736, 0, 480, 481, 512, 513, 0,
1500 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1501 	{ DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1502 		   800, 0, 480, 481, 512, 513, 0,
1503 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1504 	{ DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1505 		   800, 0, 576, 577, 608, 609, 0,
1506 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1507 	{ DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1508 		   816, 0, 350, 351, 382, 383, 0,
1509 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1510 	{ DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1511 		   816, 0, 400, 401, 432, 433, 0,
1512 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1513 	{ DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1514 		   816, 0, 480, 481, 512, 513, 0,
1515 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1516 	{ DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1517 		   816, 0, 540, 541, 572, 573, 0,
1518 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1519 	{ DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1520 		   816, 0, 576, 577, 608, 609, 0,
1521 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1522 	{ DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1523 		   864, 0, 576, 577, 608, 609, 0,
1524 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1525 	{ DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1526 		   896, 0, 600, 601, 632, 633, 0,
1527 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1528 	{ DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1529 		   928, 0, 624, 625, 656, 657, 0,
1530 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1531 	{ DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1532 		   1016, 0, 766, 767, 798, 799, 0,
1533 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1534 	{ DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1535 		   1120, 0, 768, 769, 800, 801, 0,
1536 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1537 	{ DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1538 		   1376, 0, 1024, 1025, 1056, 1057, 0,
1539 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1540 };
1541 
1542 static void psb_intel_sdvo_get_tv_modes(struct drm_connector *connector)
1543 {
1544 	struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1545 	struct psb_intel_sdvo_sdtv_resolution_request tv_res;
1546 	uint32_t reply = 0, format_map = 0;
1547 	int i;
1548 
1549 	/* Read the list of supported input resolutions for the selected TV
1550 	 * format.
1551 	 */
1552 	format_map = 1 << psb_intel_sdvo->tv_format_index;
1553 	memcpy(&tv_res, &format_map,
1554 	       min(sizeof(format_map), sizeof(struct psb_intel_sdvo_sdtv_resolution_request)));
1555 
1556 	if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo, psb_intel_sdvo->attached_output))
1557 		return;
1558 
1559 	BUILD_BUG_ON(sizeof(tv_res) != 3);
1560 	if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo,
1561 				  SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
1562 				  &tv_res, sizeof(tv_res)))
1563 		return;
1564 	if (!psb_intel_sdvo_read_response(psb_intel_sdvo, &reply, 3))
1565 		return;
1566 
1567 	for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
1568 		if (reply & (1 << i)) {
1569 			struct drm_display_mode *nmode;
1570 			nmode = drm_mode_duplicate(connector->dev,
1571 						   &sdvo_tv_modes[i]);
1572 			if (nmode)
1573 				drm_mode_probed_add(connector, nmode);
1574 		}
1575 }
1576 
1577 static void psb_intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1578 {
1579 	struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1580 	struct drm_psb_private *dev_priv = connector->dev->dev_private;
1581 	struct drm_display_mode *newmode;
1582 
1583 	/*
1584 	 * Attempt to get the mode list from DDC.
1585 	 * Assume that the preferred modes are
1586 	 * arranged in priority order.
1587 	 */
1588 	psb_intel_ddc_get_modes(connector, psb_intel_sdvo->i2c);
1589 	if (list_empty(&connector->probed_modes) == false)
1590 		goto end;
1591 
1592 	/* Fetch modes from VBT */
1593 	if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
1594 		newmode = drm_mode_duplicate(connector->dev,
1595 					     dev_priv->sdvo_lvds_vbt_mode);
1596 		if (newmode != NULL) {
1597 			/* Guarantee the mode is preferred */
1598 			newmode->type = (DRM_MODE_TYPE_PREFERRED |
1599 					 DRM_MODE_TYPE_DRIVER);
1600 			drm_mode_probed_add(connector, newmode);
1601 		}
1602 	}
1603 
1604 end:
1605 	list_for_each_entry(newmode, &connector->probed_modes, head) {
1606 		if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1607 			psb_intel_sdvo->sdvo_lvds_fixed_mode =
1608 				drm_mode_duplicate(connector->dev, newmode);
1609 
1610 			drm_mode_set_crtcinfo(psb_intel_sdvo->sdvo_lvds_fixed_mode,
1611 					      0);
1612 
1613 			psb_intel_sdvo->is_lvds = true;
1614 			break;
1615 		}
1616 	}
1617 
1618 }
1619 
1620 static int psb_intel_sdvo_get_modes(struct drm_connector *connector)
1621 {
1622 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1623 
1624 	if (IS_TV(psb_intel_sdvo_connector))
1625 		psb_intel_sdvo_get_tv_modes(connector);
1626 	else if (IS_LVDS(psb_intel_sdvo_connector))
1627 		psb_intel_sdvo_get_lvds_modes(connector);
1628 	else
1629 		psb_intel_sdvo_get_ddc_modes(connector);
1630 
1631 	return !list_empty(&connector->probed_modes);
1632 }
1633 
1634 static void
1635 psb_intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
1636 {
1637 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1638 	struct drm_device *dev = connector->dev;
1639 
1640 	if (psb_intel_sdvo_connector->left)
1641 		drm_property_destroy(dev, psb_intel_sdvo_connector->left);
1642 	if (psb_intel_sdvo_connector->right)
1643 		drm_property_destroy(dev, psb_intel_sdvo_connector->right);
1644 	if (psb_intel_sdvo_connector->top)
1645 		drm_property_destroy(dev, psb_intel_sdvo_connector->top);
1646 	if (psb_intel_sdvo_connector->bottom)
1647 		drm_property_destroy(dev, psb_intel_sdvo_connector->bottom);
1648 	if (psb_intel_sdvo_connector->hpos)
1649 		drm_property_destroy(dev, psb_intel_sdvo_connector->hpos);
1650 	if (psb_intel_sdvo_connector->vpos)
1651 		drm_property_destroy(dev, psb_intel_sdvo_connector->vpos);
1652 	if (psb_intel_sdvo_connector->saturation)
1653 		drm_property_destroy(dev, psb_intel_sdvo_connector->saturation);
1654 	if (psb_intel_sdvo_connector->contrast)
1655 		drm_property_destroy(dev, psb_intel_sdvo_connector->contrast);
1656 	if (psb_intel_sdvo_connector->hue)
1657 		drm_property_destroy(dev, psb_intel_sdvo_connector->hue);
1658 	if (psb_intel_sdvo_connector->sharpness)
1659 		drm_property_destroy(dev, psb_intel_sdvo_connector->sharpness);
1660 	if (psb_intel_sdvo_connector->flicker_filter)
1661 		drm_property_destroy(dev, psb_intel_sdvo_connector->flicker_filter);
1662 	if (psb_intel_sdvo_connector->flicker_filter_2d)
1663 		drm_property_destroy(dev, psb_intel_sdvo_connector->flicker_filter_2d);
1664 	if (psb_intel_sdvo_connector->flicker_filter_adaptive)
1665 		drm_property_destroy(dev, psb_intel_sdvo_connector->flicker_filter_adaptive);
1666 	if (psb_intel_sdvo_connector->tv_luma_filter)
1667 		drm_property_destroy(dev, psb_intel_sdvo_connector->tv_luma_filter);
1668 	if (psb_intel_sdvo_connector->tv_chroma_filter)
1669 		drm_property_destroy(dev, psb_intel_sdvo_connector->tv_chroma_filter);
1670 	if (psb_intel_sdvo_connector->dot_crawl)
1671 		drm_property_destroy(dev, psb_intel_sdvo_connector->dot_crawl);
1672 	if (psb_intel_sdvo_connector->brightness)
1673 		drm_property_destroy(dev, psb_intel_sdvo_connector->brightness);
1674 }
1675 
1676 static void psb_intel_sdvo_destroy(struct drm_connector *connector)
1677 {
1678 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1679 
1680 	if (psb_intel_sdvo_connector->tv_format)
1681 		drm_property_destroy(connector->dev,
1682 				     psb_intel_sdvo_connector->tv_format);
1683 
1684 	psb_intel_sdvo_destroy_enhance_property(connector);
1685 	drm_sysfs_connector_remove(connector);
1686 	drm_connector_cleanup(connector);
1687 	kfree(connector);
1688 }
1689 
1690 static bool psb_intel_sdvo_detect_hdmi_audio(struct drm_connector *connector)
1691 {
1692 	struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1693 	struct edid *edid;
1694 	bool has_audio = false;
1695 
1696 	if (!psb_intel_sdvo->is_hdmi)
1697 		return false;
1698 
1699 	edid = psb_intel_sdvo_get_edid(connector);
1700 	if (edid != NULL && edid->input & DRM_EDID_INPUT_DIGITAL)
1701 		has_audio = drm_detect_monitor_audio(edid);
1702 
1703 	return has_audio;
1704 }
1705 
1706 static int
1707 psb_intel_sdvo_set_property(struct drm_connector *connector,
1708 			struct drm_property *property,
1709 			uint64_t val)
1710 {
1711 	struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1712 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1713 	struct drm_psb_private *dev_priv = connector->dev->dev_private;
1714 	uint16_t temp_value;
1715 	uint8_t cmd;
1716 	int ret;
1717 
1718 	ret = drm_object_property_set_value(&connector->base, property, val);
1719 	if (ret)
1720 		return ret;
1721 
1722 	if (property == dev_priv->force_audio_property) {
1723 		int i = val;
1724 		bool has_audio;
1725 
1726 		if (i == psb_intel_sdvo_connector->force_audio)
1727 			return 0;
1728 
1729 		psb_intel_sdvo_connector->force_audio = i;
1730 
1731 		if (i == 0)
1732 			has_audio = psb_intel_sdvo_detect_hdmi_audio(connector);
1733 		else
1734 			has_audio = i > 0;
1735 
1736 		if (has_audio == psb_intel_sdvo->has_hdmi_audio)
1737 			return 0;
1738 
1739 		psb_intel_sdvo->has_hdmi_audio = has_audio;
1740 		goto done;
1741 	}
1742 
1743 	if (property == dev_priv->broadcast_rgb_property) {
1744 		if (val == !!psb_intel_sdvo->color_range)
1745 			return 0;
1746 
1747 		psb_intel_sdvo->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
1748 		goto done;
1749 	}
1750 
1751 #define CHECK_PROPERTY(name, NAME) \
1752 	if (psb_intel_sdvo_connector->name == property) { \
1753 		if (psb_intel_sdvo_connector->cur_##name == temp_value) return 0; \
1754 		if (psb_intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
1755 		cmd = SDVO_CMD_SET_##NAME; \
1756 		psb_intel_sdvo_connector->cur_##name = temp_value; \
1757 		goto set_value; \
1758 	}
1759 
1760 	if (property == psb_intel_sdvo_connector->tv_format) {
1761 		if (val >= TV_FORMAT_NUM)
1762 			return -EINVAL;
1763 
1764 		if (psb_intel_sdvo->tv_format_index ==
1765 		    psb_intel_sdvo_connector->tv_format_supported[val])
1766 			return 0;
1767 
1768 		psb_intel_sdvo->tv_format_index = psb_intel_sdvo_connector->tv_format_supported[val];
1769 		goto done;
1770 	} else if (IS_TV_OR_LVDS(psb_intel_sdvo_connector)) {
1771 		temp_value = val;
1772 		if (psb_intel_sdvo_connector->left == property) {
1773 			drm_object_property_set_value(&connector->base,
1774 							 psb_intel_sdvo_connector->right, val);
1775 			if (psb_intel_sdvo_connector->left_margin == temp_value)
1776 				return 0;
1777 
1778 			psb_intel_sdvo_connector->left_margin = temp_value;
1779 			psb_intel_sdvo_connector->right_margin = temp_value;
1780 			temp_value = psb_intel_sdvo_connector->max_hscan -
1781 				psb_intel_sdvo_connector->left_margin;
1782 			cmd = SDVO_CMD_SET_OVERSCAN_H;
1783 			goto set_value;
1784 		} else if (psb_intel_sdvo_connector->right == property) {
1785 			drm_object_property_set_value(&connector->base,
1786 							 psb_intel_sdvo_connector->left, val);
1787 			if (psb_intel_sdvo_connector->right_margin == temp_value)
1788 				return 0;
1789 
1790 			psb_intel_sdvo_connector->left_margin = temp_value;
1791 			psb_intel_sdvo_connector->right_margin = temp_value;
1792 			temp_value = psb_intel_sdvo_connector->max_hscan -
1793 				psb_intel_sdvo_connector->left_margin;
1794 			cmd = SDVO_CMD_SET_OVERSCAN_H;
1795 			goto set_value;
1796 		} else if (psb_intel_sdvo_connector->top == property) {
1797 			drm_object_property_set_value(&connector->base,
1798 							 psb_intel_sdvo_connector->bottom, val);
1799 			if (psb_intel_sdvo_connector->top_margin == temp_value)
1800 				return 0;
1801 
1802 			psb_intel_sdvo_connector->top_margin = temp_value;
1803 			psb_intel_sdvo_connector->bottom_margin = temp_value;
1804 			temp_value = psb_intel_sdvo_connector->max_vscan -
1805 				psb_intel_sdvo_connector->top_margin;
1806 			cmd = SDVO_CMD_SET_OVERSCAN_V;
1807 			goto set_value;
1808 		} else if (psb_intel_sdvo_connector->bottom == property) {
1809 			drm_object_property_set_value(&connector->base,
1810 							 psb_intel_sdvo_connector->top, val);
1811 			if (psb_intel_sdvo_connector->bottom_margin == temp_value)
1812 				return 0;
1813 
1814 			psb_intel_sdvo_connector->top_margin = temp_value;
1815 			psb_intel_sdvo_connector->bottom_margin = temp_value;
1816 			temp_value = psb_intel_sdvo_connector->max_vscan -
1817 				psb_intel_sdvo_connector->top_margin;
1818 			cmd = SDVO_CMD_SET_OVERSCAN_V;
1819 			goto set_value;
1820 		}
1821 		CHECK_PROPERTY(hpos, HPOS)
1822 		CHECK_PROPERTY(vpos, VPOS)
1823 		CHECK_PROPERTY(saturation, SATURATION)
1824 		CHECK_PROPERTY(contrast, CONTRAST)
1825 		CHECK_PROPERTY(hue, HUE)
1826 		CHECK_PROPERTY(brightness, BRIGHTNESS)
1827 		CHECK_PROPERTY(sharpness, SHARPNESS)
1828 		CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
1829 		CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
1830 		CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
1831 		CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
1832 		CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
1833 		CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
1834 	}
1835 
1836 	return -EINVAL; /* unknown property */
1837 
1838 set_value:
1839 	if (!psb_intel_sdvo_set_value(psb_intel_sdvo, cmd, &temp_value, 2))
1840 		return -EIO;
1841 
1842 
1843 done:
1844 	if (psb_intel_sdvo->base.base.crtc) {
1845 		struct drm_crtc *crtc = psb_intel_sdvo->base.base.crtc;
1846 		drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
1847 					 crtc->y, crtc->fb);
1848 	}
1849 
1850 	return 0;
1851 #undef CHECK_PROPERTY
1852 }
1853 
1854 static void psb_intel_sdvo_save(struct drm_connector *connector)
1855 {
1856 	struct drm_device *dev = connector->dev;
1857 	struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
1858 	struct psb_intel_sdvo *sdvo = to_psb_intel_sdvo(&gma_encoder->base);
1859 
1860 	sdvo->saveSDVO = REG_READ(sdvo->sdvo_reg);
1861 }
1862 
1863 static void psb_intel_sdvo_restore(struct drm_connector *connector)
1864 {
1865 	struct drm_device *dev = connector->dev;
1866 	struct drm_encoder *encoder = &gma_attached_encoder(connector)->base;
1867 	struct psb_intel_sdvo *sdvo = to_psb_intel_sdvo(encoder);
1868 	struct drm_crtc *crtc = encoder->crtc;
1869 
1870 	REG_WRITE(sdvo->sdvo_reg, sdvo->saveSDVO);
1871 
1872 	/* Force a full mode set on the crtc. We're supposed to have the
1873 	   mode_config lock already. */
1874 	if (connector->status == connector_status_connected)
1875 		drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
1876 					 NULL);
1877 }
1878 
1879 static const struct drm_encoder_helper_funcs psb_intel_sdvo_helper_funcs = {
1880 	.dpms = psb_intel_sdvo_dpms,
1881 	.mode_fixup = psb_intel_sdvo_mode_fixup,
1882 	.prepare = gma_encoder_prepare,
1883 	.mode_set = psb_intel_sdvo_mode_set,
1884 	.commit = gma_encoder_commit,
1885 };
1886 
1887 static const struct drm_connector_funcs psb_intel_sdvo_connector_funcs = {
1888 	.dpms = drm_helper_connector_dpms,
1889 	.save = psb_intel_sdvo_save,
1890 	.restore = psb_intel_sdvo_restore,
1891 	.detect = psb_intel_sdvo_detect,
1892 	.fill_modes = drm_helper_probe_single_connector_modes,
1893 	.set_property = psb_intel_sdvo_set_property,
1894 	.destroy = psb_intel_sdvo_destroy,
1895 };
1896 
1897 static const struct drm_connector_helper_funcs psb_intel_sdvo_connector_helper_funcs = {
1898 	.get_modes = psb_intel_sdvo_get_modes,
1899 	.mode_valid = psb_intel_sdvo_mode_valid,
1900 	.best_encoder = gma_best_encoder,
1901 };
1902 
1903 static void psb_intel_sdvo_enc_destroy(struct drm_encoder *encoder)
1904 {
1905 	struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
1906 
1907 	if (psb_intel_sdvo->sdvo_lvds_fixed_mode != NULL)
1908 		drm_mode_destroy(encoder->dev,
1909 				 psb_intel_sdvo->sdvo_lvds_fixed_mode);
1910 
1911 	i2c_del_adapter(&psb_intel_sdvo->ddc);
1912 	gma_encoder_destroy(encoder);
1913 }
1914 
1915 static const struct drm_encoder_funcs psb_intel_sdvo_enc_funcs = {
1916 	.destroy = psb_intel_sdvo_enc_destroy,
1917 };
1918 
1919 static void
1920 psb_intel_sdvo_guess_ddc_bus(struct psb_intel_sdvo *sdvo)
1921 {
1922 	/* FIXME: At the moment, ddc_bus = 2 is the only thing that works.
1923 	 * We need to figure out if this is true for all available poulsbo
1924 	 * hardware, or if we need to fiddle with the guessing code above.
1925 	 * The problem might go away if we can parse sdvo mappings from bios */
1926 	sdvo->ddc_bus = 2;
1927 
1928 #if 0
1929 	uint16_t mask = 0;
1930 	unsigned int num_bits;
1931 
1932 	/* Make a mask of outputs less than or equal to our own priority in the
1933 	 * list.
1934 	 */
1935 	switch (sdvo->controlled_output) {
1936 	case SDVO_OUTPUT_LVDS1:
1937 		mask |= SDVO_OUTPUT_LVDS1;
1938 	case SDVO_OUTPUT_LVDS0:
1939 		mask |= SDVO_OUTPUT_LVDS0;
1940 	case SDVO_OUTPUT_TMDS1:
1941 		mask |= SDVO_OUTPUT_TMDS1;
1942 	case SDVO_OUTPUT_TMDS0:
1943 		mask |= SDVO_OUTPUT_TMDS0;
1944 	case SDVO_OUTPUT_RGB1:
1945 		mask |= SDVO_OUTPUT_RGB1;
1946 	case SDVO_OUTPUT_RGB0:
1947 		mask |= SDVO_OUTPUT_RGB0;
1948 		break;
1949 	}
1950 
1951 	/* Count bits to find what number we are in the priority list. */
1952 	mask &= sdvo->caps.output_flags;
1953 	num_bits = hweight16(mask);
1954 	/* If more than 3 outputs, default to DDC bus 3 for now. */
1955 	if (num_bits > 3)
1956 		num_bits = 3;
1957 
1958 	/* Corresponds to SDVO_CONTROL_BUS_DDCx */
1959 	sdvo->ddc_bus = 1 << num_bits;
1960 #endif
1961 }
1962 
1963 /**
1964  * Choose the appropriate DDC bus for control bus switch command for this
1965  * SDVO output based on the controlled output.
1966  *
1967  * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1968  * outputs, then LVDS outputs.
1969  */
1970 static void
1971 psb_intel_sdvo_select_ddc_bus(struct drm_psb_private *dev_priv,
1972 			  struct psb_intel_sdvo *sdvo, u32 reg)
1973 {
1974 	struct sdvo_device_mapping *mapping;
1975 
1976 	if (IS_SDVOB(reg))
1977 		mapping = &(dev_priv->sdvo_mappings[0]);
1978 	else
1979 		mapping = &(dev_priv->sdvo_mappings[1]);
1980 
1981 	if (mapping->initialized)
1982 		sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
1983 	else
1984 		psb_intel_sdvo_guess_ddc_bus(sdvo);
1985 }
1986 
1987 static void
1988 psb_intel_sdvo_select_i2c_bus(struct drm_psb_private *dev_priv,
1989 			  struct psb_intel_sdvo *sdvo, u32 reg)
1990 {
1991 	struct sdvo_device_mapping *mapping;
1992 	u8 pin, speed;
1993 
1994 	if (IS_SDVOB(reg))
1995 		mapping = &dev_priv->sdvo_mappings[0];
1996 	else
1997 		mapping = &dev_priv->sdvo_mappings[1];
1998 
1999 	pin = GMBUS_PORT_DPB;
2000 	speed = GMBUS_RATE_1MHZ >> 8;
2001 	if (mapping->initialized) {
2002 		pin = mapping->i2c_pin;
2003 		speed = mapping->i2c_speed;
2004 	}
2005 
2006 	if (pin < GMBUS_NUM_PORTS) {
2007 		sdvo->i2c = &dev_priv->gmbus[pin].adapter;
2008 		gma_intel_gmbus_set_speed(sdvo->i2c, speed);
2009 		gma_intel_gmbus_force_bit(sdvo->i2c, true);
2010 	} else
2011 		sdvo->i2c = &dev_priv->gmbus[GMBUS_PORT_DPB].adapter;
2012 }
2013 
2014 static bool
2015 psb_intel_sdvo_is_hdmi_connector(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2016 {
2017 	return psb_intel_sdvo_check_supp_encode(psb_intel_sdvo);
2018 }
2019 
2020 static u8
2021 psb_intel_sdvo_get_slave_addr(struct drm_device *dev, int sdvo_reg)
2022 {
2023 	struct drm_psb_private *dev_priv = dev->dev_private;
2024 	struct sdvo_device_mapping *my_mapping, *other_mapping;
2025 
2026 	if (IS_SDVOB(sdvo_reg)) {
2027 		my_mapping = &dev_priv->sdvo_mappings[0];
2028 		other_mapping = &dev_priv->sdvo_mappings[1];
2029 	} else {
2030 		my_mapping = &dev_priv->sdvo_mappings[1];
2031 		other_mapping = &dev_priv->sdvo_mappings[0];
2032 	}
2033 
2034 	/* If the BIOS described our SDVO device, take advantage of it. */
2035 	if (my_mapping->slave_addr)
2036 		return my_mapping->slave_addr;
2037 
2038 	/* If the BIOS only described a different SDVO device, use the
2039 	 * address that it isn't using.
2040 	 */
2041 	if (other_mapping->slave_addr) {
2042 		if (other_mapping->slave_addr == 0x70)
2043 			return 0x72;
2044 		else
2045 			return 0x70;
2046 	}
2047 
2048 	/* No SDVO device info is found for another DVO port,
2049 	 * so use mapping assumption we had before BIOS parsing.
2050 	 */
2051 	if (IS_SDVOB(sdvo_reg))
2052 		return 0x70;
2053 	else
2054 		return 0x72;
2055 }
2056 
2057 static void
2058 psb_intel_sdvo_connector_init(struct psb_intel_sdvo_connector *connector,
2059 			  struct psb_intel_sdvo *encoder)
2060 {
2061 	drm_connector_init(encoder->base.base.dev,
2062 			   &connector->base.base,
2063 			   &psb_intel_sdvo_connector_funcs,
2064 			   connector->base.base.connector_type);
2065 
2066 	drm_connector_helper_add(&connector->base.base,
2067 				 &psb_intel_sdvo_connector_helper_funcs);
2068 
2069 	connector->base.base.interlace_allowed = 0;
2070 	connector->base.base.doublescan_allowed = 0;
2071 	connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
2072 
2073 	gma_connector_attach_encoder(&connector->base, &encoder->base);
2074 	drm_sysfs_connector_add(&connector->base.base);
2075 }
2076 
2077 static void
2078 psb_intel_sdvo_add_hdmi_properties(struct psb_intel_sdvo_connector *connector)
2079 {
2080 	/* FIXME: We don't support HDMI at the moment
2081 	struct drm_device *dev = connector->base.base.dev;
2082 
2083 	intel_attach_force_audio_property(&connector->base.base);
2084 	intel_attach_broadcast_rgb_property(&connector->base.base);
2085 	*/
2086 }
2087 
2088 static bool
2089 psb_intel_sdvo_dvi_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2090 {
2091 	struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2092 	struct drm_connector *connector;
2093 	struct gma_connector *intel_connector;
2094 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2095 
2096 	psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2097 	if (!psb_intel_sdvo_connector)
2098 		return false;
2099 
2100 	if (device == 0) {
2101 		psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
2102 		psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
2103 	} else if (device == 1) {
2104 		psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
2105 		psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
2106 	}
2107 
2108 	intel_connector = &psb_intel_sdvo_connector->base;
2109 	connector = &intel_connector->base;
2110 	// connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
2111 	encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2112 	connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2113 
2114 	if (psb_intel_sdvo_is_hdmi_connector(psb_intel_sdvo, device)) {
2115 		connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
2116 		psb_intel_sdvo->is_hdmi = true;
2117 	}
2118 	psb_intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2119 				       (1 << INTEL_ANALOG_CLONE_BIT));
2120 
2121 	psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2122 	if (psb_intel_sdvo->is_hdmi)
2123 		psb_intel_sdvo_add_hdmi_properties(psb_intel_sdvo_connector);
2124 
2125 	return true;
2126 }
2127 
2128 static bool
2129 psb_intel_sdvo_tv_init(struct psb_intel_sdvo *psb_intel_sdvo, int type)
2130 {
2131 	struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2132 	struct drm_connector *connector;
2133 	struct gma_connector *intel_connector;
2134 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2135 
2136 	psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2137 	if (!psb_intel_sdvo_connector)
2138 		return false;
2139 
2140 	intel_connector = &psb_intel_sdvo_connector->base;
2141 	connector = &intel_connector->base;
2142 	encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2143 	connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
2144 
2145 	psb_intel_sdvo->controlled_output |= type;
2146 	psb_intel_sdvo_connector->output_flag = type;
2147 
2148 	psb_intel_sdvo->is_tv = true;
2149 	psb_intel_sdvo->base.needs_tv_clock = true;
2150 	psb_intel_sdvo->base.clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
2151 
2152 	psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2153 
2154 	if (!psb_intel_sdvo_tv_create_property(psb_intel_sdvo, psb_intel_sdvo_connector, type))
2155 		goto err;
2156 
2157 	if (!psb_intel_sdvo_create_enhance_property(psb_intel_sdvo, psb_intel_sdvo_connector))
2158 		goto err;
2159 
2160 	return true;
2161 
2162 err:
2163 	psb_intel_sdvo_destroy(connector);
2164 	return false;
2165 }
2166 
2167 static bool
2168 psb_intel_sdvo_analog_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2169 {
2170 	struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2171 	struct drm_connector *connector;
2172 	struct gma_connector *intel_connector;
2173 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2174 
2175 	psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2176 	if (!psb_intel_sdvo_connector)
2177 		return false;
2178 
2179 	intel_connector = &psb_intel_sdvo_connector->base;
2180 	connector = &intel_connector->base;
2181 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
2182 	encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2183 	connector->connector_type = DRM_MODE_CONNECTOR_VGA;
2184 
2185 	if (device == 0) {
2186 		psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
2187 		psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2188 	} else if (device == 1) {
2189 		psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
2190 		psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2191 	}
2192 
2193 	psb_intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2194 				       (1 << INTEL_ANALOG_CLONE_BIT));
2195 
2196 	psb_intel_sdvo_connector_init(psb_intel_sdvo_connector,
2197 				  psb_intel_sdvo);
2198 	return true;
2199 }
2200 
2201 static bool
2202 psb_intel_sdvo_lvds_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2203 {
2204 	struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2205 	struct drm_connector *connector;
2206 	struct gma_connector *intel_connector;
2207 	struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2208 
2209 	psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2210 	if (!psb_intel_sdvo_connector)
2211 		return false;
2212 
2213 	intel_connector = &psb_intel_sdvo_connector->base;
2214 	connector = &intel_connector->base;
2215 	encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2216 	connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2217 
2218 	if (device == 0) {
2219 		psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
2220 		psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2221 	} else if (device == 1) {
2222 		psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
2223 		psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2224 	}
2225 
2226 	psb_intel_sdvo->base.clone_mask = ((1 << INTEL_ANALOG_CLONE_BIT) |
2227 				       (1 << INTEL_SDVO_LVDS_CLONE_BIT));
2228 
2229 	psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2230 	if (!psb_intel_sdvo_create_enhance_property(psb_intel_sdvo, psb_intel_sdvo_connector))
2231 		goto err;
2232 
2233 	return true;
2234 
2235 err:
2236 	psb_intel_sdvo_destroy(connector);
2237 	return false;
2238 }
2239 
2240 static bool
2241 psb_intel_sdvo_output_setup(struct psb_intel_sdvo *psb_intel_sdvo, uint16_t flags)
2242 {
2243 	psb_intel_sdvo->is_tv = false;
2244 	psb_intel_sdvo->base.needs_tv_clock = false;
2245 	psb_intel_sdvo->is_lvds = false;
2246 
2247 	/* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
2248 
2249 	if (flags & SDVO_OUTPUT_TMDS0)
2250 		if (!psb_intel_sdvo_dvi_init(psb_intel_sdvo, 0))
2251 			return false;
2252 
2253 	if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
2254 		if (!psb_intel_sdvo_dvi_init(psb_intel_sdvo, 1))
2255 			return false;
2256 
2257 	/* TV has no XXX1 function block */
2258 	if (flags & SDVO_OUTPUT_SVID0)
2259 		if (!psb_intel_sdvo_tv_init(psb_intel_sdvo, SDVO_OUTPUT_SVID0))
2260 			return false;
2261 
2262 	if (flags & SDVO_OUTPUT_CVBS0)
2263 		if (!psb_intel_sdvo_tv_init(psb_intel_sdvo, SDVO_OUTPUT_CVBS0))
2264 			return false;
2265 
2266 	if (flags & SDVO_OUTPUT_RGB0)
2267 		if (!psb_intel_sdvo_analog_init(psb_intel_sdvo, 0))
2268 			return false;
2269 
2270 	if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
2271 		if (!psb_intel_sdvo_analog_init(psb_intel_sdvo, 1))
2272 			return false;
2273 
2274 	if (flags & SDVO_OUTPUT_LVDS0)
2275 		if (!psb_intel_sdvo_lvds_init(psb_intel_sdvo, 0))
2276 			return false;
2277 
2278 	if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
2279 		if (!psb_intel_sdvo_lvds_init(psb_intel_sdvo, 1))
2280 			return false;
2281 
2282 	if ((flags & SDVO_OUTPUT_MASK) == 0) {
2283 		unsigned char bytes[2];
2284 
2285 		psb_intel_sdvo->controlled_output = 0;
2286 		memcpy(bytes, &psb_intel_sdvo->caps.output_flags, 2);
2287 		DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
2288 			      SDVO_NAME(psb_intel_sdvo),
2289 			      bytes[0], bytes[1]);
2290 		return false;
2291 	}
2292 	psb_intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1);
2293 
2294 	return true;
2295 }
2296 
2297 static bool psb_intel_sdvo_tv_create_property(struct psb_intel_sdvo *psb_intel_sdvo,
2298 					  struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2299 					  int type)
2300 {
2301 	struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2302 	struct psb_intel_sdvo_tv_format format;
2303 	uint32_t format_map, i;
2304 
2305 	if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo, type))
2306 		return false;
2307 
2308 	BUILD_BUG_ON(sizeof(format) != 6);
2309 	if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2310 				  SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
2311 				  &format, sizeof(format)))
2312 		return false;
2313 
2314 	memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
2315 
2316 	if (format_map == 0)
2317 		return false;
2318 
2319 	psb_intel_sdvo_connector->format_supported_num = 0;
2320 	for (i = 0 ; i < TV_FORMAT_NUM; i++)
2321 		if (format_map & (1 << i))
2322 			psb_intel_sdvo_connector->tv_format_supported[psb_intel_sdvo_connector->format_supported_num++] = i;
2323 
2324 
2325 	psb_intel_sdvo_connector->tv_format =
2326 			drm_property_create(dev, DRM_MODE_PROP_ENUM,
2327 					    "mode", psb_intel_sdvo_connector->format_supported_num);
2328 	if (!psb_intel_sdvo_connector->tv_format)
2329 		return false;
2330 
2331 	for (i = 0; i < psb_intel_sdvo_connector->format_supported_num; i++)
2332 		drm_property_add_enum(
2333 				psb_intel_sdvo_connector->tv_format, i,
2334 				i, tv_format_names[psb_intel_sdvo_connector->tv_format_supported[i]]);
2335 
2336 	psb_intel_sdvo->tv_format_index = psb_intel_sdvo_connector->tv_format_supported[0];
2337 	drm_object_attach_property(&psb_intel_sdvo_connector->base.base.base,
2338 				      psb_intel_sdvo_connector->tv_format, 0);
2339 	return true;
2340 
2341 }
2342 
2343 #define ENHANCEMENT(name, NAME) do { \
2344 	if (enhancements.name) { \
2345 		if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
2346 		    !psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
2347 			return false; \
2348 		psb_intel_sdvo_connector->max_##name = data_value[0]; \
2349 		psb_intel_sdvo_connector->cur_##name = response; \
2350 		psb_intel_sdvo_connector->name = \
2351 			drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
2352 		if (!psb_intel_sdvo_connector->name) return false; \
2353 		drm_object_attach_property(&connector->base, \
2354 					      psb_intel_sdvo_connector->name, \
2355 					      psb_intel_sdvo_connector->cur_##name); \
2356 		DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
2357 			      data_value[0], data_value[1], response); \
2358 	} \
2359 } while(0)
2360 
2361 static bool
2362 psb_intel_sdvo_create_enhance_property_tv(struct psb_intel_sdvo *psb_intel_sdvo,
2363 				      struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2364 				      struct psb_intel_sdvo_enhancements_reply enhancements)
2365 {
2366 	struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2367 	struct drm_connector *connector = &psb_intel_sdvo_connector->base.base;
2368 	uint16_t response, data_value[2];
2369 
2370 	/* when horizontal overscan is supported, Add the left/right  property */
2371 	if (enhancements.overscan_h) {
2372 		if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2373 					  SDVO_CMD_GET_MAX_OVERSCAN_H,
2374 					  &data_value, 4))
2375 			return false;
2376 
2377 		if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2378 					  SDVO_CMD_GET_OVERSCAN_H,
2379 					  &response, 2))
2380 			return false;
2381 
2382 		psb_intel_sdvo_connector->max_hscan = data_value[0];
2383 		psb_intel_sdvo_connector->left_margin = data_value[0] - response;
2384 		psb_intel_sdvo_connector->right_margin = psb_intel_sdvo_connector->left_margin;
2385 		psb_intel_sdvo_connector->left =
2386 			drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
2387 		if (!psb_intel_sdvo_connector->left)
2388 			return false;
2389 
2390 		drm_object_attach_property(&connector->base,
2391 					      psb_intel_sdvo_connector->left,
2392 					      psb_intel_sdvo_connector->left_margin);
2393 
2394 		psb_intel_sdvo_connector->right =
2395 			drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
2396 		if (!psb_intel_sdvo_connector->right)
2397 			return false;
2398 
2399 		drm_object_attach_property(&connector->base,
2400 					      psb_intel_sdvo_connector->right,
2401 					      psb_intel_sdvo_connector->right_margin);
2402 		DRM_DEBUG_KMS("h_overscan: max %d, "
2403 			      "default %d, current %d\n",
2404 			      data_value[0], data_value[1], response);
2405 	}
2406 
2407 	if (enhancements.overscan_v) {
2408 		if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2409 					  SDVO_CMD_GET_MAX_OVERSCAN_V,
2410 					  &data_value, 4))
2411 			return false;
2412 
2413 		if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2414 					  SDVO_CMD_GET_OVERSCAN_V,
2415 					  &response, 2))
2416 			return false;
2417 
2418 		psb_intel_sdvo_connector->max_vscan = data_value[0];
2419 		psb_intel_sdvo_connector->top_margin = data_value[0] - response;
2420 		psb_intel_sdvo_connector->bottom_margin = psb_intel_sdvo_connector->top_margin;
2421 		psb_intel_sdvo_connector->top =
2422 			drm_property_create_range(dev, 0, "top_margin", 0, data_value[0]);
2423 		if (!psb_intel_sdvo_connector->top)
2424 			return false;
2425 
2426 		drm_object_attach_property(&connector->base,
2427 					      psb_intel_sdvo_connector->top,
2428 					      psb_intel_sdvo_connector->top_margin);
2429 
2430 		psb_intel_sdvo_connector->bottom =
2431 			drm_property_create_range(dev, 0, "bottom_margin", 0, data_value[0]);
2432 		if (!psb_intel_sdvo_connector->bottom)
2433 			return false;
2434 
2435 		drm_object_attach_property(&connector->base,
2436 					      psb_intel_sdvo_connector->bottom,
2437 					      psb_intel_sdvo_connector->bottom_margin);
2438 		DRM_DEBUG_KMS("v_overscan: max %d, "
2439 			      "default %d, current %d\n",
2440 			      data_value[0], data_value[1], response);
2441 	}
2442 
2443 	ENHANCEMENT(hpos, HPOS);
2444 	ENHANCEMENT(vpos, VPOS);
2445 	ENHANCEMENT(saturation, SATURATION);
2446 	ENHANCEMENT(contrast, CONTRAST);
2447 	ENHANCEMENT(hue, HUE);
2448 	ENHANCEMENT(sharpness, SHARPNESS);
2449 	ENHANCEMENT(brightness, BRIGHTNESS);
2450 	ENHANCEMENT(flicker_filter, FLICKER_FILTER);
2451 	ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
2452 	ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
2453 	ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
2454 	ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
2455 
2456 	if (enhancements.dot_crawl) {
2457 		if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
2458 			return false;
2459 
2460 		psb_intel_sdvo_connector->max_dot_crawl = 1;
2461 		psb_intel_sdvo_connector->cur_dot_crawl = response & 0x1;
2462 		psb_intel_sdvo_connector->dot_crawl =
2463 			drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
2464 		if (!psb_intel_sdvo_connector->dot_crawl)
2465 			return false;
2466 
2467 		drm_object_attach_property(&connector->base,
2468 					      psb_intel_sdvo_connector->dot_crawl,
2469 					      psb_intel_sdvo_connector->cur_dot_crawl);
2470 		DRM_DEBUG_KMS("dot crawl: current %d\n", response);
2471 	}
2472 
2473 	return true;
2474 }
2475 
2476 static bool
2477 psb_intel_sdvo_create_enhance_property_lvds(struct psb_intel_sdvo *psb_intel_sdvo,
2478 					struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2479 					struct psb_intel_sdvo_enhancements_reply enhancements)
2480 {
2481 	struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2482 	struct drm_connector *connector = &psb_intel_sdvo_connector->base.base;
2483 	uint16_t response, data_value[2];
2484 
2485 	ENHANCEMENT(brightness, BRIGHTNESS);
2486 
2487 	return true;
2488 }
2489 #undef ENHANCEMENT
2490 
2491 static bool psb_intel_sdvo_create_enhance_property(struct psb_intel_sdvo *psb_intel_sdvo,
2492 					       struct psb_intel_sdvo_connector *psb_intel_sdvo_connector)
2493 {
2494 	union {
2495 		struct psb_intel_sdvo_enhancements_reply reply;
2496 		uint16_t response;
2497 	} enhancements;
2498 
2499 	BUILD_BUG_ON(sizeof(enhancements) != 2);
2500 
2501 	enhancements.response = 0;
2502 	psb_intel_sdvo_get_value(psb_intel_sdvo,
2503 			     SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
2504 			     &enhancements, sizeof(enhancements));
2505 	if (enhancements.response == 0) {
2506 		DRM_DEBUG_KMS("No enhancement is supported\n");
2507 		return true;
2508 	}
2509 
2510 	if (IS_TV(psb_intel_sdvo_connector))
2511 		return psb_intel_sdvo_create_enhance_property_tv(psb_intel_sdvo, psb_intel_sdvo_connector, enhancements.reply);
2512 	else if(IS_LVDS(psb_intel_sdvo_connector))
2513 		return psb_intel_sdvo_create_enhance_property_lvds(psb_intel_sdvo, psb_intel_sdvo_connector, enhancements.reply);
2514 	else
2515 		return true;
2516 }
2517 
2518 static int psb_intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
2519 				     struct i2c_msg *msgs,
2520 				     int num)
2521 {
2522 	struct psb_intel_sdvo *sdvo = adapter->algo_data;
2523 
2524 	if (!psb_intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
2525 		return -EIO;
2526 
2527 	return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
2528 }
2529 
2530 static u32 psb_intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
2531 {
2532 	struct psb_intel_sdvo *sdvo = adapter->algo_data;
2533 	return sdvo->i2c->algo->functionality(sdvo->i2c);
2534 }
2535 
2536 static const struct i2c_algorithm psb_intel_sdvo_ddc_proxy = {
2537 	.master_xfer	= psb_intel_sdvo_ddc_proxy_xfer,
2538 	.functionality	= psb_intel_sdvo_ddc_proxy_func
2539 };
2540 
2541 static bool
2542 psb_intel_sdvo_init_ddc_proxy(struct psb_intel_sdvo *sdvo,
2543 			  struct drm_device *dev)
2544 {
2545 	sdvo->ddc.owner = THIS_MODULE;
2546 	sdvo->ddc.class = I2C_CLASS_DDC;
2547 	snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
2548 	sdvo->ddc.dev.parent = &dev->pdev->dev;
2549 	sdvo->ddc.algo_data = sdvo;
2550 	sdvo->ddc.algo = &psb_intel_sdvo_ddc_proxy;
2551 
2552 	return i2c_add_adapter(&sdvo->ddc) == 0;
2553 }
2554 
2555 bool psb_intel_sdvo_init(struct drm_device *dev, int sdvo_reg)
2556 {
2557 	struct drm_psb_private *dev_priv = dev->dev_private;
2558 	struct gma_encoder *gma_encoder;
2559 	struct psb_intel_sdvo *psb_intel_sdvo;
2560 	int i;
2561 
2562 	psb_intel_sdvo = kzalloc(sizeof(struct psb_intel_sdvo), GFP_KERNEL);
2563 	if (!psb_intel_sdvo)
2564 		return false;
2565 
2566 	psb_intel_sdvo->sdvo_reg = sdvo_reg;
2567 	psb_intel_sdvo->slave_addr = psb_intel_sdvo_get_slave_addr(dev, sdvo_reg) >> 1;
2568 	psb_intel_sdvo_select_i2c_bus(dev_priv, psb_intel_sdvo, sdvo_reg);
2569 	if (!psb_intel_sdvo_init_ddc_proxy(psb_intel_sdvo, dev)) {
2570 		kfree(psb_intel_sdvo);
2571 		return false;
2572 	}
2573 
2574 	/* encoder type will be decided later */
2575 	gma_encoder = &psb_intel_sdvo->base;
2576 	gma_encoder->type = INTEL_OUTPUT_SDVO;
2577 	drm_encoder_init(dev, &gma_encoder->base, &psb_intel_sdvo_enc_funcs, 0);
2578 
2579 	/* Read the regs to test if we can talk to the device */
2580 	for (i = 0; i < 0x40; i++) {
2581 		u8 byte;
2582 
2583 		if (!psb_intel_sdvo_read_byte(psb_intel_sdvo, i, &byte)) {
2584 			DRM_DEBUG_KMS("No SDVO device found on SDVO%c\n",
2585 				      IS_SDVOB(sdvo_reg) ? 'B' : 'C');
2586 			goto err;
2587 		}
2588 	}
2589 
2590 	if (IS_SDVOB(sdvo_reg))
2591 		dev_priv->hotplug_supported_mask |= SDVOB_HOTPLUG_INT_STATUS;
2592 	else
2593 		dev_priv->hotplug_supported_mask |= SDVOC_HOTPLUG_INT_STATUS;
2594 
2595 	drm_encoder_helper_add(&gma_encoder->base, &psb_intel_sdvo_helper_funcs);
2596 
2597 	/* In default case sdvo lvds is false */
2598 	if (!psb_intel_sdvo_get_capabilities(psb_intel_sdvo, &psb_intel_sdvo->caps))
2599 		goto err;
2600 
2601 	if (psb_intel_sdvo_output_setup(psb_intel_sdvo,
2602 				    psb_intel_sdvo->caps.output_flags) != true) {
2603 		DRM_DEBUG_KMS("SDVO output failed to setup on SDVO%c\n",
2604 			      IS_SDVOB(sdvo_reg) ? 'B' : 'C');
2605 		goto err;
2606 	}
2607 
2608 	psb_intel_sdvo_select_ddc_bus(dev_priv, psb_intel_sdvo, sdvo_reg);
2609 
2610 	/* Set the input timing to the screen. Assume always input 0. */
2611 	if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
2612 		goto err;
2613 
2614 	if (!psb_intel_sdvo_get_input_pixel_clock_range(psb_intel_sdvo,
2615 						    &psb_intel_sdvo->pixel_clock_min,
2616 						    &psb_intel_sdvo->pixel_clock_max))
2617 		goto err;
2618 
2619 	DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
2620 			"clock range %dMHz - %dMHz, "
2621 			"input 1: %c, input 2: %c, "
2622 			"output 1: %c, output 2: %c\n",
2623 			SDVO_NAME(psb_intel_sdvo),
2624 			psb_intel_sdvo->caps.vendor_id, psb_intel_sdvo->caps.device_id,
2625 			psb_intel_sdvo->caps.device_rev_id,
2626 			psb_intel_sdvo->pixel_clock_min / 1000,
2627 			psb_intel_sdvo->pixel_clock_max / 1000,
2628 			(psb_intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2629 			(psb_intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
2630 			/* check currently supported outputs */
2631 			psb_intel_sdvo->caps.output_flags &
2632 			(SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
2633 			psb_intel_sdvo->caps.output_flags &
2634 			(SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
2635 	return true;
2636 
2637 err:
2638 	drm_encoder_cleanup(&gma_encoder->base);
2639 	i2c_del_adapter(&psb_intel_sdvo->ddc);
2640 	kfree(psb_intel_sdvo);
2641 
2642 	return false;
2643 }
2644