drm/i915: Add a pipeless ivybridge configuration
[linux-block.git] / drivers / gpu / drm / i915 / intel_sdvo.c
CommitLineData
79e53945
JB
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/i2c.h>
5a0e3ad6 29#include <linux/slab.h>
79e53945 30#include <linux/delay.h>
2d1a8a48 31#include <linux/export.h>
760285e7
DH
32#include <drm/drmP.h>
33#include <drm/drm_crtc.h>
34#include <drm/drm_edid.h>
ea5b213a 35#include "intel_drv.h"
760285e7 36#include <drm/i915_drm.h>
79e53945
JB
37#include "i915_drv.h"
38#include "intel_sdvo_regs.h"
39
14571b4c
ZW
40#define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
41#define SDVO_RGB_MASK (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
42#define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
a0b1c7a5 43#define SDVO_TV_MASK (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_YPRPB0)
14571b4c
ZW
44
45#define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
0206e353 46 SDVO_TV_MASK)
14571b4c
ZW
47
48#define IS_TV(c) (c->output_flag & SDVO_TV_MASK)
13946743 49#define IS_TMDS(c) (c->output_flag & SDVO_TMDS_MASK)
14571b4c 50#define IS_LVDS(c) (c->output_flag & SDVO_LVDS_MASK)
32aad86f 51#define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
52220085 52#define IS_DIGITAL(c) (c->output_flag & (SDVO_TMDS_MASK | SDVO_LVDS_MASK))
14571b4c 53
79e53945 54
2e88e40b 55static const char *tv_format_names[] = {
ce6feabd
ZY
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
ea5b213a
CW
67struct intel_sdvo {
68 struct intel_encoder base;
69
f899fc64 70 struct i2c_adapter *i2c;
f9c10a9b 71 u8 slave_addr;
e2f0ba97 72
e957d772
CW
73 struct i2c_adapter ddc;
74
e2f0ba97 75 /* Register for the SDVO device: SDVOB or SDVOC */
eef4eacb 76 uint32_t sdvo_reg;
79e53945 77
e2f0ba97
JB
78 /* Active outputs controlled by this SDVO output */
79 uint16_t controlled_output;
79e53945 80
e2f0ba97
JB
81 /*
82 * Capabilities of the SDVO device returned by
83 * i830_sdvo_get_capabilities()
84 */
79e53945 85 struct intel_sdvo_caps caps;
e2f0ba97
JB
86
87 /* Pixel clock limitations reported by the SDVO device, in kHz */
79e53945
JB
88 int pixel_clock_min, pixel_clock_max;
89
fb7a46f3 90 /*
91 * For multiple function SDVO device,
92 * this is for current attached outputs.
93 */
94 uint16_t attached_output;
95
cc68c81a
SF
96 /*
97 * Hotplug activation bits for this device
98 */
5fa7ac9c 99 uint16_t hotplug_active;
cc68c81a 100
e953fd7b
CW
101 /**
102 * This is used to select the color range of RBG outputs in HDMI mode.
103 * It is only valid when using TMDS encoding and 8 bit per color mode.
104 */
105 uint32_t color_range;
55bc60db 106 bool color_range_auto;
e953fd7b 107
e2f0ba97
JB
108 /**
109 * This is set if we're going to treat the device as TV-out.
110 *
111 * While we have these nice friendly flags for output types that ought
112 * to decide this for us, the S-Video output on our HDMI+S-Video card
113 * shows up as RGB1 (VGA).
114 */
115 bool is_tv;
116
eef4eacb
DV
117 /* On different gens SDVOB is at different places. */
118 bool is_sdvob;
119
ce6feabd 120 /* This is for current tv format name */
40039750 121 int tv_format_index;
ce6feabd 122
e2f0ba97
JB
123 /**
124 * This is set if we treat the device as HDMI, instead of DVI.
125 */
126 bool is_hdmi;
da79de97
CW
127 bool has_hdmi_monitor;
128 bool has_hdmi_audio;
abedc077 129 bool rgb_quant_range_selectable;
12682a97 130
7086c87f 131 /**
6c9547ff
CW
132 * This is set if we detect output of sdvo device as LVDS and
133 * have a valid fixed mode to use with the panel.
7086c87f
ML
134 */
135 bool is_lvds;
e2f0ba97 136
12682a97 137 /**
138 * This is sdvo fixed pannel mode pointer
139 */
140 struct drm_display_mode *sdvo_lvds_fixed_mode;
141
c751ce4f 142 /* DDC bus used by this SDVO encoder */
e2f0ba97 143 uint8_t ddc_bus;
e751823d
EE
144
145 /*
146 * the sdvo flag gets lost in round trip: dtd->adjusted_mode->dtd
147 */
148 uint8_t dtd_sdvo_flags;
14571b4c
ZW
149};
150
151struct intel_sdvo_connector {
615fb93f
CW
152 struct intel_connector base;
153
14571b4c
ZW
154 /* Mark the type of connector */
155 uint16_t output_flag;
156
c3e5f67b 157 enum hdmi_force_audio force_audio;
7f36e7ed 158
14571b4c 159 /* This contains all current supported TV format */
40039750 160 u8 tv_format_supported[TV_FORMAT_NUM];
14571b4c 161 int format_supported_num;
c5521706 162 struct drm_property *tv_format;
14571b4c 163
b9219c5e 164 /* add the property for the SDVO-TV */
c5521706
CW
165 struct drm_property *left;
166 struct drm_property *right;
167 struct drm_property *top;
168 struct drm_property *bottom;
169 struct drm_property *hpos;
170 struct drm_property *vpos;
171 struct drm_property *contrast;
172 struct drm_property *saturation;
173 struct drm_property *hue;
174 struct drm_property *sharpness;
175 struct drm_property *flicker_filter;
176 struct drm_property *flicker_filter_adaptive;
177 struct drm_property *flicker_filter_2d;
178 struct drm_property *tv_chroma_filter;
179 struct drm_property *tv_luma_filter;
e044218a 180 struct drm_property *dot_crawl;
b9219c5e
ZY
181
182 /* add the property for the SDVO-TV/LVDS */
c5521706 183 struct drm_property *brightness;
b9219c5e
ZY
184
185 /* Add variable to record current setting for the above property */
186 u32 left_margin, right_margin, top_margin, bottom_margin;
c5521706 187
b9219c5e
ZY
188 /* this is to get the range of margin.*/
189 u32 max_hscan, max_vscan;
190 u32 max_hpos, cur_hpos;
191 u32 max_vpos, cur_vpos;
192 u32 cur_brightness, max_brightness;
193 u32 cur_contrast, max_contrast;
194 u32 cur_saturation, max_saturation;
195 u32 cur_hue, max_hue;
c5521706
CW
196 u32 cur_sharpness, max_sharpness;
197 u32 cur_flicker_filter, max_flicker_filter;
198 u32 cur_flicker_filter_adaptive, max_flicker_filter_adaptive;
199 u32 cur_flicker_filter_2d, max_flicker_filter_2d;
200 u32 cur_tv_chroma_filter, max_tv_chroma_filter;
201 u32 cur_tv_luma_filter, max_tv_luma_filter;
e044218a 202 u32 cur_dot_crawl, max_dot_crawl;
79e53945
JB
203};
204
890f3359 205static struct intel_sdvo *to_intel_sdvo(struct drm_encoder *encoder)
ea5b213a 206{
4ef69c7a 207 return container_of(encoder, struct intel_sdvo, base.base);
ea5b213a
CW
208}
209
df0e9248
CW
210static struct intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
211{
212 return container_of(intel_attached_encoder(connector),
213 struct intel_sdvo, base);
214}
215
615fb93f
CW
216static struct intel_sdvo_connector *to_intel_sdvo_connector(struct drm_connector *connector)
217{
218 return container_of(to_intel_connector(connector), struct intel_sdvo_connector, base);
219}
220
fb7a46f3 221static bool
ea5b213a 222intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags);
32aad86f
CW
223static bool
224intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
225 struct intel_sdvo_connector *intel_sdvo_connector,
226 int type);
227static bool
228intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
229 struct intel_sdvo_connector *intel_sdvo_connector);
fb7a46f3 230
79e53945
JB
231/**
232 * Writes the SDVOB or SDVOC with the given value, but always writes both
233 * SDVOB and SDVOC to work around apparent hardware issues (according to
234 * comments in the BIOS).
235 */
ea5b213a 236static void intel_sdvo_write_sdvox(struct intel_sdvo *intel_sdvo, u32 val)
79e53945 237{
4ef69c7a 238 struct drm_device *dev = intel_sdvo->base.base.dev;
79e53945 239 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945
JB
240 u32 bval = val, cval = val;
241 int i;
242
ea5b213a
CW
243 if (intel_sdvo->sdvo_reg == PCH_SDVOB) {
244 I915_WRITE(intel_sdvo->sdvo_reg, val);
245 I915_READ(intel_sdvo->sdvo_reg);
461ed3ca
ZY
246 return;
247 }
248
e2debe91
PZ
249 if (intel_sdvo->sdvo_reg == GEN3_SDVOB)
250 cval = I915_READ(GEN3_SDVOC);
251 else
252 bval = I915_READ(GEN3_SDVOB);
253
79e53945
JB
254 /*
255 * Write the registers twice for luck. Sometimes,
256 * writing them only once doesn't appear to 'stick'.
257 * The BIOS does this too. Yay, magic
258 */
259 for (i = 0; i < 2; i++)
260 {
e2debe91
PZ
261 I915_WRITE(GEN3_SDVOB, bval);
262 I915_READ(GEN3_SDVOB);
263 I915_WRITE(GEN3_SDVOC, cval);
264 I915_READ(GEN3_SDVOC);
79e53945
JB
265 }
266}
267
32aad86f 268static bool intel_sdvo_read_byte(struct intel_sdvo *intel_sdvo, u8 addr, u8 *ch)
79e53945 269{
79e53945
JB
270 struct i2c_msg msgs[] = {
271 {
e957d772 272 .addr = intel_sdvo->slave_addr,
79e53945
JB
273 .flags = 0,
274 .len = 1,
e957d772 275 .buf = &addr,
79e53945
JB
276 },
277 {
e957d772 278 .addr = intel_sdvo->slave_addr,
79e53945
JB
279 .flags = I2C_M_RD,
280 .len = 1,
e957d772 281 .buf = ch,
79e53945
JB
282 }
283 };
32aad86f 284 int ret;
79e53945 285
f899fc64 286 if ((ret = i2c_transfer(intel_sdvo->i2c, msgs, 2)) == 2)
79e53945 287 return true;
79e53945 288
8a4c47f3 289 DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
79e53945
JB
290 return false;
291}
292
79e53945
JB
293#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
294/** Mapping of command numbers to names, for debug output */
005568be 295static const struct _sdvo_cmd_name {
e2f0ba97 296 u8 cmd;
2e88e40b 297 const char *name;
79e53945 298} sdvo_cmd_names[] = {
0206e353
AJ
299 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
300 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
301 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
302 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
303 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
304 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
305 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
306 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
307 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
308 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
309 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
310 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
311 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
312 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
313 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
314 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
315 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
316 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
317 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
318 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
319 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
320 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
321 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
322 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
323 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
324 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
325 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
326 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
327 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
328 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
329 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
330 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
331 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
332 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
333 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
334 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
335 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
336 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
337 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
338 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
339 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
340 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
341 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
342
343 /* Add the op code for SDVO enhancements */
344 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
345 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
346 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
347 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
348 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
349 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
350 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
351 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
352 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
353 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
354 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
355 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
356 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
357 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
358 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
359 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
360 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
361 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
362 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
363 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
364 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
365 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
366 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
367 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
368 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
369 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
370 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
371 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
372 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
373 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
374 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
375 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
376 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
377 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
378 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
379 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
380 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
381 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
382 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
383 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
384 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
385 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
386 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
387 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
388
389 /* HDMI op code */
390 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
391 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
392 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
393 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
394 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
395 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
396 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
397 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
398 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
399 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
400 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
401 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
402 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
403 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
404 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
405 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
406 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
407 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
408 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
409 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
79e53945
JB
410};
411
eef4eacb 412#define SDVO_NAME(svdo) ((svdo)->is_sdvob ? "SDVOB" : "SDVOC")
79e53945 413
ea5b213a 414static void intel_sdvo_debug_write(struct intel_sdvo *intel_sdvo, u8 cmd,
32aad86f 415 const void *args, int args_len)
79e53945 416{
79e53945
JB
417 int i;
418
8a4c47f3 419 DRM_DEBUG_KMS("%s: W: %02X ",
ea5b213a 420 SDVO_NAME(intel_sdvo), cmd);
79e53945 421 for (i = 0; i < args_len; i++)
342dc382 422 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
79e53945 423 for (; i < 8; i++)
342dc382 424 DRM_LOG_KMS(" ");
04ad327f 425 for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
79e53945 426 if (cmd == sdvo_cmd_names[i].cmd) {
342dc382 427 DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
79e53945
JB
428 break;
429 }
430 }
04ad327f 431 if (i == ARRAY_SIZE(sdvo_cmd_names))
342dc382 432 DRM_LOG_KMS("(%02X)", cmd);
433 DRM_LOG_KMS("\n");
79e53945 434}
79e53945 435
e957d772
CW
436static const char *cmd_status_names[] = {
437 "Power on",
438 "Success",
439 "Not supported",
440 "Invalid arg",
441 "Pending",
442 "Target not specified",
443 "Scaling not supported"
444};
445
32aad86f
CW
446static bool intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
447 const void *args, int args_len)
79e53945 448{
3bf3f452
BW
449 u8 *buf, status;
450 struct i2c_msg *msgs;
451 int i, ret = true;
452
0274df3e 453 /* Would be simpler to allocate both in one go ? */
5c67eeb6 454 buf = kzalloc(args_len * 2 + 2, GFP_KERNEL);
3bf3f452
BW
455 if (!buf)
456 return false;
457
458 msgs = kcalloc(args_len + 3, sizeof(*msgs), GFP_KERNEL);
0274df3e
AC
459 if (!msgs) {
460 kfree(buf);
3bf3f452 461 return false;
0274df3e 462 }
79e53945 463
ea5b213a 464 intel_sdvo_debug_write(intel_sdvo, cmd, args, args_len);
79e53945
JB
465
466 for (i = 0; i < args_len; i++) {
e957d772
CW
467 msgs[i].addr = intel_sdvo->slave_addr;
468 msgs[i].flags = 0;
469 msgs[i].len = 2;
470 msgs[i].buf = buf + 2 *i;
471 buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
472 buf[2*i + 1] = ((u8*)args)[i];
473 }
474 msgs[i].addr = intel_sdvo->slave_addr;
475 msgs[i].flags = 0;
476 msgs[i].len = 2;
477 msgs[i].buf = buf + 2*i;
478 buf[2*i + 0] = SDVO_I2C_OPCODE;
479 buf[2*i + 1] = cmd;
480
481 /* the following two are to read the response */
482 status = SDVO_I2C_CMD_STATUS;
483 msgs[i+1].addr = intel_sdvo->slave_addr;
484 msgs[i+1].flags = 0;
485 msgs[i+1].len = 1;
486 msgs[i+1].buf = &status;
487
488 msgs[i+2].addr = intel_sdvo->slave_addr;
489 msgs[i+2].flags = I2C_M_RD;
490 msgs[i+2].len = 1;
491 msgs[i+2].buf = &status;
492
493 ret = i2c_transfer(intel_sdvo->i2c, msgs, i+3);
494 if (ret < 0) {
495 DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
3bf3f452
BW
496 ret = false;
497 goto out;
e957d772
CW
498 }
499 if (ret != i+3) {
500 /* failure in I2C transfer */
501 DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
3bf3f452 502 ret = false;
e957d772
CW
503 }
504
3bf3f452
BW
505out:
506 kfree(msgs);
507 kfree(buf);
508 return ret;
79e53945
JB
509}
510
b5c616a7
CW
511static bool intel_sdvo_read_response(struct intel_sdvo *intel_sdvo,
512 void *response, int response_len)
79e53945 513{
fc37381c 514 u8 retry = 15; /* 5 quick checks, followed by 10 long checks */
b5c616a7 515 u8 status;
33b52961 516 int i;
79e53945 517
d121a5d2
CW
518 DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(intel_sdvo));
519
b5c616a7
CW
520 /*
521 * The documentation states that all commands will be
522 * processed within 15µs, and that we need only poll
523 * the status byte a maximum of 3 times in order for the
524 * command to be complete.
525 *
526 * Check 5 times in case the hardware failed to read the docs.
fc37381c
CW
527 *
528 * Also beware that the first response by many devices is to
529 * reply PENDING and stall for time. TVs are notorious for
530 * requiring longer than specified to complete their replies.
531 * Originally (in the DDX long ago), the delay was only ever 15ms
532 * with an additional delay of 30ms applied for TVs added later after
533 * many experiments. To accommodate both sets of delays, we do a
534 * sequence of slow checks if the device is falling behind and fails
535 * to reply within 5*15µs.
b5c616a7 536 */
d121a5d2
CW
537 if (!intel_sdvo_read_byte(intel_sdvo,
538 SDVO_I2C_CMD_STATUS,
539 &status))
540 goto log_fail;
541
fc37381c
CW
542 while (status == SDVO_CMD_STATUS_PENDING && --retry) {
543 if (retry < 10)
544 msleep(15);
545 else
546 udelay(15);
547
b5c616a7
CW
548 if (!intel_sdvo_read_byte(intel_sdvo,
549 SDVO_I2C_CMD_STATUS,
550 &status))
d121a5d2
CW
551 goto log_fail;
552 }
b5c616a7 553
79e53945 554 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
342dc382 555 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
79e53945 556 else
342dc382 557 DRM_LOG_KMS("(??? %d)", status);
79e53945 558
b5c616a7
CW
559 if (status != SDVO_CMD_STATUS_SUCCESS)
560 goto log_fail;
79e53945 561
b5c616a7
CW
562 /* Read the command response */
563 for (i = 0; i < response_len; i++) {
564 if (!intel_sdvo_read_byte(intel_sdvo,
565 SDVO_I2C_RETURN_0 + i,
566 &((u8 *)response)[i]))
567 goto log_fail;
e957d772 568 DRM_LOG_KMS(" %02X", ((u8 *)response)[i]);
b5c616a7 569 }
b5c616a7 570 DRM_LOG_KMS("\n");
b5c616a7 571 return true;
79e53945 572
b5c616a7 573log_fail:
d121a5d2 574 DRM_LOG_KMS("... failed\n");
b5c616a7 575 return false;
79e53945
JB
576}
577
b358d0a6 578static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
79e53945
JB
579{
580 if (mode->clock >= 100000)
581 return 1;
582 else if (mode->clock >= 50000)
583 return 2;
584 else
585 return 4;
586}
587
e957d772
CW
588static bool intel_sdvo_set_control_bus_switch(struct intel_sdvo *intel_sdvo,
589 u8 ddc_bus)
79e53945 590{
d121a5d2 591 /* This must be the immediately preceding write before the i2c xfer */
e957d772
CW
592 return intel_sdvo_write_cmd(intel_sdvo,
593 SDVO_CMD_SET_CONTROL_BUS_SWITCH,
594 &ddc_bus, 1);
79e53945
JB
595}
596
32aad86f 597static bool intel_sdvo_set_value(struct intel_sdvo *intel_sdvo, u8 cmd, const void *data, int len)
79e53945 598{
d121a5d2
CW
599 if (!intel_sdvo_write_cmd(intel_sdvo, cmd, data, len))
600 return false;
601
602 return intel_sdvo_read_response(intel_sdvo, NULL, 0);
32aad86f 603}
79e53945 604
32aad86f
CW
605static bool
606intel_sdvo_get_value(struct intel_sdvo *intel_sdvo, u8 cmd, void *value, int len)
607{
608 if (!intel_sdvo_write_cmd(intel_sdvo, cmd, NULL, 0))
609 return false;
79e53945 610
32aad86f
CW
611 return intel_sdvo_read_response(intel_sdvo, value, len);
612}
79e53945 613
32aad86f
CW
614static bool intel_sdvo_set_target_input(struct intel_sdvo *intel_sdvo)
615{
616 struct intel_sdvo_set_target_input_args targets = {0};
617 return intel_sdvo_set_value(intel_sdvo,
618 SDVO_CMD_SET_TARGET_INPUT,
619 &targets, sizeof(targets));
79e53945
JB
620}
621
622/**
623 * Return whether each input is trained.
624 *
625 * This function is making an assumption about the layout of the response,
626 * which should be checked against the docs.
627 */
ea5b213a 628static bool intel_sdvo_get_trained_inputs(struct intel_sdvo *intel_sdvo, bool *input_1, bool *input_2)
79e53945
JB
629{
630 struct intel_sdvo_get_trained_inputs_response response;
79e53945 631
1a3665c8 632 BUILD_BUG_ON(sizeof(response) != 1);
32aad86f
CW
633 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
634 &response, sizeof(response)))
79e53945
JB
635 return false;
636
637 *input_1 = response.input0_trained;
638 *input_2 = response.input1_trained;
639 return true;
640}
641
ea5b213a 642static bool intel_sdvo_set_active_outputs(struct intel_sdvo *intel_sdvo,
79e53945
JB
643 u16 outputs)
644{
32aad86f
CW
645 return intel_sdvo_set_value(intel_sdvo,
646 SDVO_CMD_SET_ACTIVE_OUTPUTS,
647 &outputs, sizeof(outputs));
79e53945
JB
648}
649
4ac41f47
DV
650static bool intel_sdvo_get_active_outputs(struct intel_sdvo *intel_sdvo,
651 u16 *outputs)
652{
653 return intel_sdvo_get_value(intel_sdvo,
654 SDVO_CMD_GET_ACTIVE_OUTPUTS,
655 outputs, sizeof(*outputs));
656}
657
ea5b213a 658static bool intel_sdvo_set_encoder_power_state(struct intel_sdvo *intel_sdvo,
79e53945
JB
659 int mode)
660{
32aad86f 661 u8 state = SDVO_ENCODER_STATE_ON;
79e53945
JB
662
663 switch (mode) {
664 case DRM_MODE_DPMS_ON:
665 state = SDVO_ENCODER_STATE_ON;
666 break;
667 case DRM_MODE_DPMS_STANDBY:
668 state = SDVO_ENCODER_STATE_STANDBY;
669 break;
670 case DRM_MODE_DPMS_SUSPEND:
671 state = SDVO_ENCODER_STATE_SUSPEND;
672 break;
673 case DRM_MODE_DPMS_OFF:
674 state = SDVO_ENCODER_STATE_OFF;
675 break;
676 }
677
32aad86f
CW
678 return intel_sdvo_set_value(intel_sdvo,
679 SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
79e53945
JB
680}
681
ea5b213a 682static bool intel_sdvo_get_input_pixel_clock_range(struct intel_sdvo *intel_sdvo,
79e53945
JB
683 int *clock_min,
684 int *clock_max)
685{
686 struct intel_sdvo_pixel_clock_range clocks;
79e53945 687
1a3665c8 688 BUILD_BUG_ON(sizeof(clocks) != 4);
32aad86f
CW
689 if (!intel_sdvo_get_value(intel_sdvo,
690 SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
691 &clocks, sizeof(clocks)))
79e53945
JB
692 return false;
693
694 /* Convert the values from units of 10 kHz to kHz. */
695 *clock_min = clocks.min * 10;
696 *clock_max = clocks.max * 10;
79e53945
JB
697 return true;
698}
699
ea5b213a 700static bool intel_sdvo_set_target_output(struct intel_sdvo *intel_sdvo,
79e53945
JB
701 u16 outputs)
702{
32aad86f
CW
703 return intel_sdvo_set_value(intel_sdvo,
704 SDVO_CMD_SET_TARGET_OUTPUT,
705 &outputs, sizeof(outputs));
79e53945
JB
706}
707
ea5b213a 708static bool intel_sdvo_set_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
79e53945
JB
709 struct intel_sdvo_dtd *dtd)
710{
32aad86f
CW
711 return intel_sdvo_set_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
712 intel_sdvo_set_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
79e53945
JB
713}
714
ea5b213a 715static bool intel_sdvo_set_input_timing(struct intel_sdvo *intel_sdvo,
79e53945
JB
716 struct intel_sdvo_dtd *dtd)
717{
ea5b213a 718 return intel_sdvo_set_timing(intel_sdvo,
79e53945
JB
719 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
720}
721
ea5b213a 722static bool intel_sdvo_set_output_timing(struct intel_sdvo *intel_sdvo,
79e53945
JB
723 struct intel_sdvo_dtd *dtd)
724{
ea5b213a 725 return intel_sdvo_set_timing(intel_sdvo,
79e53945
JB
726 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
727}
728
e2f0ba97 729static bool
ea5b213a 730intel_sdvo_create_preferred_input_timing(struct intel_sdvo *intel_sdvo,
e2f0ba97
JB
731 uint16_t clock,
732 uint16_t width,
733 uint16_t height)
734{
735 struct intel_sdvo_preferred_input_timing_args args;
e2f0ba97 736
e642c6f1 737 memset(&args, 0, sizeof(args));
e2f0ba97
JB
738 args.clock = clock;
739 args.width = width;
740 args.height = height;
e642c6f1 741 args.interlace = 0;
12682a97 742
ea5b213a
CW
743 if (intel_sdvo->is_lvds &&
744 (intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
745 intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
12682a97 746 args.scaled = 1;
747
32aad86f
CW
748 return intel_sdvo_set_value(intel_sdvo,
749 SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
750 &args, sizeof(args));
e2f0ba97
JB
751}
752
ea5b213a 753static bool intel_sdvo_get_preferred_input_timing(struct intel_sdvo *intel_sdvo,
e2f0ba97
JB
754 struct intel_sdvo_dtd *dtd)
755{
1a3665c8
CW
756 BUILD_BUG_ON(sizeof(dtd->part1) != 8);
757 BUILD_BUG_ON(sizeof(dtd->part2) != 8);
32aad86f
CW
758 return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
759 &dtd->part1, sizeof(dtd->part1)) &&
760 intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
761 &dtd->part2, sizeof(dtd->part2));
e2f0ba97 762}
79e53945 763
ea5b213a 764static bool intel_sdvo_set_clock_rate_mult(struct intel_sdvo *intel_sdvo, u8 val)
79e53945 765{
32aad86f 766 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
79e53945
JB
767}
768
e2f0ba97 769static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
32aad86f 770 const struct drm_display_mode *mode)
79e53945 771{
e2f0ba97
JB
772 uint16_t width, height;
773 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
774 uint16_t h_sync_offset, v_sync_offset;
6651819b 775 int mode_clock;
79e53945 776
c6ebd4c0
DV
777 width = mode->hdisplay;
778 height = mode->vdisplay;
79e53945
JB
779
780 /* do some mode translations */
c6ebd4c0
DV
781 h_blank_len = mode->htotal - mode->hdisplay;
782 h_sync_len = mode->hsync_end - mode->hsync_start;
79e53945 783
c6ebd4c0
DV
784 v_blank_len = mode->vtotal - mode->vdisplay;
785 v_sync_len = mode->vsync_end - mode->vsync_start;
79e53945 786
c6ebd4c0
DV
787 h_sync_offset = mode->hsync_start - mode->hdisplay;
788 v_sync_offset = mode->vsync_start - mode->vdisplay;
79e53945 789
6651819b 790 mode_clock = mode->clock;
6651819b
DV
791 mode_clock /= 10;
792 dtd->part1.clock = mode_clock;
793
e2f0ba97
JB
794 dtd->part1.h_active = width & 0xff;
795 dtd->part1.h_blank = h_blank_len & 0xff;
796 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
79e53945 797 ((h_blank_len >> 8) & 0xf);
e2f0ba97
JB
798 dtd->part1.v_active = height & 0xff;
799 dtd->part1.v_blank = v_blank_len & 0xff;
800 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
79e53945
JB
801 ((v_blank_len >> 8) & 0xf);
802
171a9e96 803 dtd->part2.h_sync_off = h_sync_offset & 0xff;
e2f0ba97
JB
804 dtd->part2.h_sync_width = h_sync_len & 0xff;
805 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
79e53945 806 (v_sync_len & 0xf);
e2f0ba97 807 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
79e53945
JB
808 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
809 ((v_sync_len & 0x30) >> 4);
810
e2f0ba97 811 dtd->part2.dtd_flags = 0x18;
59d92bfa
DV
812 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
813 dtd->part2.dtd_flags |= DTD_FLAG_INTERLACE;
79e53945 814 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
59d92bfa 815 dtd->part2.dtd_flags |= DTD_FLAG_HSYNC_POSITIVE;
79e53945 816 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
59d92bfa 817 dtd->part2.dtd_flags |= DTD_FLAG_VSYNC_POSITIVE;
e2f0ba97
JB
818
819 dtd->part2.sdvo_flags = 0;
820 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
821 dtd->part2.reserved = 0;
822}
823
824static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
32aad86f 825 const struct intel_sdvo_dtd *dtd)
e2f0ba97 826{
e2f0ba97
JB
827 mode->hdisplay = dtd->part1.h_active;
828 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
829 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
171a9e96 830 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
e2f0ba97
JB
831 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
832 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
833 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
834 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
835
836 mode->vdisplay = dtd->part1.v_active;
837 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
838 mode->vsync_start = mode->vdisplay;
839 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
171a9e96 840 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
e2f0ba97
JB
841 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
842 mode->vsync_end = mode->vsync_start +
843 (dtd->part2.v_sync_off_width & 0xf);
844 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
845 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
846 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
847
848 mode->clock = dtd->part1.clock * 10;
849
171a9e96 850 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
59d92bfa
DV
851 if (dtd->part2.dtd_flags & DTD_FLAG_INTERLACE)
852 mode->flags |= DRM_MODE_FLAG_INTERLACE;
853 if (dtd->part2.dtd_flags & DTD_FLAG_HSYNC_POSITIVE)
e2f0ba97 854 mode->flags |= DRM_MODE_FLAG_PHSYNC;
59d92bfa 855 if (dtd->part2.dtd_flags & DTD_FLAG_VSYNC_POSITIVE)
e2f0ba97
JB
856 mode->flags |= DRM_MODE_FLAG_PVSYNC;
857}
858
e27d8538 859static bool intel_sdvo_check_supp_encode(struct intel_sdvo *intel_sdvo)
e2f0ba97 860{
e27d8538 861 struct intel_sdvo_encode encode;
e2f0ba97 862
1a3665c8 863 BUILD_BUG_ON(sizeof(encode) != 2);
e27d8538
CW
864 return intel_sdvo_get_value(intel_sdvo,
865 SDVO_CMD_GET_SUPP_ENCODE,
866 &encode, sizeof(encode));
e2f0ba97
JB
867}
868
ea5b213a 869static bool intel_sdvo_set_encode(struct intel_sdvo *intel_sdvo,
c751ce4f 870 uint8_t mode)
e2f0ba97 871{
32aad86f 872 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
e2f0ba97
JB
873}
874
ea5b213a 875static bool intel_sdvo_set_colorimetry(struct intel_sdvo *intel_sdvo,
e2f0ba97
JB
876 uint8_t mode)
877{
32aad86f 878 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
e2f0ba97
JB
879}
880
881#if 0
ea5b213a 882static void intel_sdvo_dump_hdmi_buf(struct intel_sdvo *intel_sdvo)
e2f0ba97
JB
883{
884 int i, j;
885 uint8_t set_buf_index[2];
886 uint8_t av_split;
887 uint8_t buf_size;
888 uint8_t buf[48];
889 uint8_t *pos;
890
32aad86f 891 intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
e2f0ba97
JB
892
893 for (i = 0; i <= av_split; i++) {
894 set_buf_index[0] = i; set_buf_index[1] = 0;
c751ce4f 895 intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
e2f0ba97 896 set_buf_index, 2);
c751ce4f
EA
897 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
898 intel_sdvo_read_response(encoder, &buf_size, 1);
e2f0ba97
JB
899
900 pos = buf;
901 for (j = 0; j <= buf_size; j += 8) {
c751ce4f 902 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
e2f0ba97 903 NULL, 0);
c751ce4f 904 intel_sdvo_read_response(encoder, pos, 8);
e2f0ba97
JB
905 pos += 8;
906 }
907 }
908}
909#endif
910
b6e0e543
DV
911static bool intel_sdvo_write_infoframe(struct intel_sdvo *intel_sdvo,
912 unsigned if_index, uint8_t tx_rate,
913 uint8_t *data, unsigned length)
914{
915 uint8_t set_buf_index[2] = { if_index, 0 };
916 uint8_t hbuf_size, tmp[8];
917 int i;
918
919 if (!intel_sdvo_set_value(intel_sdvo,
920 SDVO_CMD_SET_HBUF_INDEX,
921 set_buf_index, 2))
922 return false;
923
924 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HBUF_INFO,
925 &hbuf_size, 1))
926 return false;
927
928 /* Buffer size is 0 based, hooray! */
929 hbuf_size++;
930
931 DRM_DEBUG_KMS("writing sdvo hbuf: %i, hbuf_size %i, hbuf_size: %i\n",
932 if_index, length, hbuf_size);
933
934 for (i = 0; i < hbuf_size; i += 8) {
935 memset(tmp, 0, 8);
936 if (i < length)
937 memcpy(tmp, data + i, min_t(unsigned, 8, length - i));
938
939 if (!intel_sdvo_set_value(intel_sdvo,
940 SDVO_CMD_SET_HBUF_DATA,
941 tmp, 8))
942 return false;
943 }
944
945 return intel_sdvo_set_value(intel_sdvo,
946 SDVO_CMD_SET_HBUF_TXRATE,
947 &tx_rate, 1);
948}
949
abedc077
VS
950static bool intel_sdvo_set_avi_infoframe(struct intel_sdvo *intel_sdvo,
951 const struct drm_display_mode *adjusted_mode)
e2f0ba97
JB
952{
953 struct dip_infoframe avi_if = {
954 .type = DIP_TYPE_AVI,
3c17fe4b 955 .ver = DIP_VERSION_AVI,
e2f0ba97
JB
956 .len = DIP_LEN_AVI,
957 };
81014b9d 958 uint8_t sdvo_data[4 + sizeof(avi_if.body.avi)];
50f3b016 959 struct intel_crtc *intel_crtc = to_intel_crtc(intel_sdvo->base.base.crtc);
3c17fe4b 960
abedc077 961 if (intel_sdvo->rgb_quant_range_selectable) {
50f3b016 962 if (intel_crtc->config.limited_color_range)
abedc077
VS
963 avi_if.body.avi.ITC_EC_Q_SC |= DIP_AVI_RGB_QUANT_RANGE_LIMITED;
964 else
965 avi_if.body.avi.ITC_EC_Q_SC |= DIP_AVI_RGB_QUANT_RANGE_FULL;
966 }
967
96b219fa
VS
968 avi_if.body.avi.VIC = drm_match_cea_mode(adjusted_mode);
969
3c17fe4b
DH
970 intel_dip_infoframe_csum(&avi_if);
971
81014b9d
DV
972 /* sdvo spec says that the ecc is handled by the hw, and it looks like
973 * we must not send the ecc field, either. */
974 memcpy(sdvo_data, &avi_if, 3);
975 sdvo_data[3] = avi_if.checksum;
976 memcpy(&sdvo_data[4], &avi_if.body, sizeof(avi_if.body.avi));
977
b6e0e543
DV
978 return intel_sdvo_write_infoframe(intel_sdvo, SDVO_HBUF_INDEX_AVI_IF,
979 SDVO_HBUF_TX_VSYNC,
980 sdvo_data, sizeof(sdvo_data));
e2f0ba97
JB
981}
982
32aad86f 983static bool intel_sdvo_set_tv_format(struct intel_sdvo *intel_sdvo)
7026d4ac 984{
ce6feabd 985 struct intel_sdvo_tv_format format;
40039750 986 uint32_t format_map;
ce6feabd 987
40039750 988 format_map = 1 << intel_sdvo->tv_format_index;
ce6feabd 989 memset(&format, 0, sizeof(format));
32aad86f 990 memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
ce6feabd 991
32aad86f
CW
992 BUILD_BUG_ON(sizeof(format) != 6);
993 return intel_sdvo_set_value(intel_sdvo,
994 SDVO_CMD_SET_TV_FORMAT,
995 &format, sizeof(format));
7026d4ac
ZW
996}
997
32aad86f
CW
998static bool
999intel_sdvo_set_output_timings_from_mode(struct intel_sdvo *intel_sdvo,
e811f5ae 1000 const struct drm_display_mode *mode)
e2f0ba97 1001{
32aad86f 1002 struct intel_sdvo_dtd output_dtd;
79e53945 1003
32aad86f
CW
1004 if (!intel_sdvo_set_target_output(intel_sdvo,
1005 intel_sdvo->attached_output))
1006 return false;
e2f0ba97 1007
32aad86f
CW
1008 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
1009 if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
1010 return false;
e2f0ba97 1011
32aad86f
CW
1012 return true;
1013}
1014
c9a29698
DV
1015/* Asks the sdvo controller for the preferred input mode given the output mode.
1016 * Unfortunately we have to set up the full output mode to do that. */
32aad86f 1017static bool
c9a29698 1018intel_sdvo_get_preferred_input_mode(struct intel_sdvo *intel_sdvo,
e811f5ae 1019 const struct drm_display_mode *mode,
c9a29698 1020 struct drm_display_mode *adjusted_mode)
32aad86f 1021{
c9a29698
DV
1022 struct intel_sdvo_dtd input_dtd;
1023
32aad86f
CW
1024 /* Reset the input timing to the screen. Assume always input 0. */
1025 if (!intel_sdvo_set_target_input(intel_sdvo))
1026 return false;
e2f0ba97 1027
32aad86f
CW
1028 if (!intel_sdvo_create_preferred_input_timing(intel_sdvo,
1029 mode->clock / 10,
1030 mode->hdisplay,
1031 mode->vdisplay))
1032 return false;
e2f0ba97 1033
32aad86f 1034 if (!intel_sdvo_get_preferred_input_timing(intel_sdvo,
c9a29698 1035 &input_dtd))
32aad86f 1036 return false;
e2f0ba97 1037
c9a29698 1038 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
e751823d 1039 intel_sdvo->dtd_sdvo_flags = input_dtd.part2.sdvo_flags;
79e53945 1040
32aad86f
CW
1041 return true;
1042}
12682a97 1043
6cc5f341
DV
1044static bool intel_sdvo_compute_config(struct intel_encoder *encoder,
1045 struct intel_crtc_config *pipe_config)
32aad86f 1046{
6cc5f341
DV
1047 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
1048 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
1049 struct drm_display_mode *mode = &pipe_config->requested_mode;
12682a97 1050
5d2d38dd
DV
1051 DRM_DEBUG_KMS("forcing bpc to 8 for SDVO\n");
1052 pipe_config->pipe_bpp = 8*3;
1053
5bfe2ac0
DV
1054 if (HAS_PCH_SPLIT(encoder->base.dev))
1055 pipe_config->has_pch_encoder = true;
1056
32aad86f
CW
1057 /* We need to construct preferred input timings based on our
1058 * output timings. To do that, we have to set the output
1059 * timings, even though this isn't really the right place in
1060 * the sequence to do it. Oh well.
1061 */
1062 if (intel_sdvo->is_tv) {
1063 if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo, mode))
1064 return false;
12682a97 1065
c9a29698
DV
1066 (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
1067 mode,
1068 adjusted_mode);
ea5b213a 1069 } else if (intel_sdvo->is_lvds) {
32aad86f 1070 if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo,
6c9547ff 1071 intel_sdvo->sdvo_lvds_fixed_mode))
e2f0ba97 1072 return false;
12682a97 1073
c9a29698
DV
1074 (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
1075 mode,
1076 adjusted_mode);
e2f0ba97 1077 }
32aad86f
CW
1078
1079 /* Make the CRTC code factor in the SDVO pixel multiplier. The
6c9547ff 1080 * SDVO device will factor out the multiplier during mode_set.
32aad86f 1081 */
6cc5f341
DV
1082 pipe_config->pixel_multiplier =
1083 intel_sdvo_get_pixel_multiplier(adjusted_mode);
1084 adjusted_mode->clock *= pipe_config->pixel_multiplier;
32aad86f 1085
55bc60db
VS
1086 if (intel_sdvo->color_range_auto) {
1087 /* See CEA-861-E - 5.1 Default Encoding Parameters */
4f3a8bc7
PZ
1088 /* FIXME: This bit is only valid when using TMDS encoding and 8
1089 * bit per color mode. */
55bc60db 1090 if (intel_sdvo->has_hdmi_monitor &&
18316c8c 1091 drm_match_cea_mode(adjusted_mode) > 1)
4f3a8bc7 1092 intel_sdvo->color_range = HDMI_COLOR_RANGE_16_235;
55bc60db
VS
1093 else
1094 intel_sdvo->color_range = 0;
1095 }
1096
3685a8f3 1097 if (intel_sdvo->color_range)
50f3b016 1098 pipe_config->limited_color_range = true;
3685a8f3 1099
e2f0ba97
JB
1100 return true;
1101}
1102
6cc5f341 1103static void intel_sdvo_mode_set(struct intel_encoder *intel_encoder)
e2f0ba97 1104{
6cc5f341 1105 struct drm_device *dev = intel_encoder->base.dev;
e2f0ba97 1106 struct drm_i915_private *dev_priv = dev->dev_private;
6cc5f341 1107 struct drm_crtc *crtc = intel_encoder->base.crtc;
e2f0ba97 1108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6cc5f341
DV
1109 struct drm_display_mode *adjusted_mode =
1110 &intel_crtc->config.adjusted_mode;
1111 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
1112 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&intel_encoder->base);
6c9547ff 1113 u32 sdvox;
e2f0ba97 1114 struct intel_sdvo_in_out_map in_out;
6651819b 1115 struct intel_sdvo_dtd input_dtd, output_dtd;
6c9547ff 1116 int rate;
e2f0ba97
JB
1117
1118 if (!mode)
1119 return;
1120
1121 /* First, set the input mapping for the first input to our controlled
1122 * output. This is only correct if we're a single-input device, in
1123 * which case the first input is the output from the appropriate SDVO
1124 * channel on the motherboard. In a two-input device, the first input
1125 * will be SDVOB and the second SDVOC.
1126 */
ea5b213a 1127 in_out.in0 = intel_sdvo->attached_output;
e2f0ba97
JB
1128 in_out.in1 = 0;
1129
c74696b9
PR
1130 intel_sdvo_set_value(intel_sdvo,
1131 SDVO_CMD_SET_IN_OUT_MAP,
1132 &in_out, sizeof(in_out));
e2f0ba97 1133
6c9547ff
CW
1134 /* Set the output timings to the screen */
1135 if (!intel_sdvo_set_target_output(intel_sdvo,
1136 intel_sdvo->attached_output))
1137 return;
e2f0ba97 1138
6651819b
DV
1139 /* lvds has a special fixed output timing. */
1140 if (intel_sdvo->is_lvds)
1141 intel_sdvo_get_dtd_from_mode(&output_dtd,
1142 intel_sdvo->sdvo_lvds_fixed_mode);
1143 else
1144 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
c8d4bb54
DV
1145 if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
1146 DRM_INFO("Setting output timings on %s failed\n",
1147 SDVO_NAME(intel_sdvo));
79e53945
JB
1148
1149 /* Set the input timing to the screen. Assume always input 0. */
32aad86f
CW
1150 if (!intel_sdvo_set_target_input(intel_sdvo))
1151 return;
79e53945 1152
97aaf910
CW
1153 if (intel_sdvo->has_hdmi_monitor) {
1154 intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_HDMI);
1155 intel_sdvo_set_colorimetry(intel_sdvo,
1156 SDVO_COLORIMETRY_RGB256);
abedc077 1157 intel_sdvo_set_avi_infoframe(intel_sdvo, adjusted_mode);
97aaf910
CW
1158 } else
1159 intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_DVI);
7026d4ac 1160
6c9547ff
CW
1161 if (intel_sdvo->is_tv &&
1162 !intel_sdvo_set_tv_format(intel_sdvo))
1163 return;
e2f0ba97 1164
6651819b
DV
1165 /* We have tried to get input timing in mode_fixup, and filled into
1166 * adjusted_mode.
1167 */
1168 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
e751823d
EE
1169 if (intel_sdvo->is_tv || intel_sdvo->is_lvds)
1170 input_dtd.part2.sdvo_flags = intel_sdvo->dtd_sdvo_flags;
c8d4bb54
DV
1171 if (!intel_sdvo_set_input_timing(intel_sdvo, &input_dtd))
1172 DRM_INFO("Setting input timings on %s failed\n",
1173 SDVO_NAME(intel_sdvo));
79e53945 1174
6cc5f341 1175 switch (intel_crtc->config.pixel_multiplier) {
6c9547ff 1176 default:
32aad86f
CW
1177 case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
1178 case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
1179 case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
79e53945 1180 }
32aad86f
CW
1181 if (!intel_sdvo_set_clock_rate_mult(intel_sdvo, rate))
1182 return;
79e53945
JB
1183
1184 /* Set the SDVO control regs. */
a6c45cf0 1185 if (INTEL_INFO(dev)->gen >= 4) {
ba68e086
PZ
1186 /* The real mode polarity is set by the SDVO commands, using
1187 * struct intel_sdvo_dtd. */
1188 sdvox = SDVO_VSYNC_ACTIVE_HIGH | SDVO_HSYNC_ACTIVE_HIGH;
3685a8f3 1189 if (!HAS_PCH_SPLIT(dev) && intel_sdvo->is_hdmi)
e953fd7b 1190 sdvox |= intel_sdvo->color_range;
6714afb1
CW
1191 if (INTEL_INFO(dev)->gen < 5)
1192 sdvox |= SDVO_BORDER_ENABLE;
e2f0ba97 1193 } else {
6c9547ff 1194 sdvox = I915_READ(intel_sdvo->sdvo_reg);
ea5b213a 1195 switch (intel_sdvo->sdvo_reg) {
e2debe91 1196 case GEN3_SDVOB:
e2f0ba97
JB
1197 sdvox &= SDVOB_PRESERVE_MASK;
1198 break;
e2debe91 1199 case GEN3_SDVOC:
e2f0ba97
JB
1200 sdvox &= SDVOC_PRESERVE_MASK;
1201 break;
1202 }
1203 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1204 }
3573c410
PZ
1205
1206 if (INTEL_PCH_TYPE(dev) >= PCH_CPT)
dc0fa718 1207 sdvox |= SDVO_PIPE_SEL_CPT(intel_crtc->pipe);
3573c410 1208 else
dc0fa718 1209 sdvox |= SDVO_PIPE_SEL(intel_crtc->pipe);
3573c410 1210
da79de97 1211 if (intel_sdvo->has_hdmi_audio)
6c9547ff 1212 sdvox |= SDVO_AUDIO_ENABLE;
79e53945 1213
a6c45cf0 1214 if (INTEL_INFO(dev)->gen >= 4) {
e2f0ba97
JB
1215 /* done in crtc_mode_set as the dpll_md reg must be written early */
1216 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1217 /* done in crtc_mode_set as it lives inside the dpll register */
79e53945 1218 } else {
6cc5f341
DV
1219 sdvox |= (intel_crtc->config.pixel_multiplier - 1)
1220 << SDVO_PORT_MULTIPLY_SHIFT;
79e53945
JB
1221 }
1222
6714afb1
CW
1223 if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL &&
1224 INTEL_INFO(dev)->gen < 5)
12682a97 1225 sdvox |= SDVO_STALL_SELECT;
ea5b213a 1226 intel_sdvo_write_sdvox(intel_sdvo, sdvox);
79e53945
JB
1227}
1228
4ac41f47 1229static bool intel_sdvo_connector_get_hw_state(struct intel_connector *connector)
79e53945 1230{
4ac41f47
DV
1231 struct intel_sdvo_connector *intel_sdvo_connector =
1232 to_intel_sdvo_connector(&connector->base);
1233 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(&connector->base);
bd6946e8 1234 struct drm_i915_private *dev_priv = intel_sdvo->base.base.dev->dev_private;
4ac41f47
DV
1235 u16 active_outputs;
1236
bd6946e8
DV
1237 if (!(I915_READ(intel_sdvo->sdvo_reg) & SDVO_ENABLE))
1238 return false;
1239
4ac41f47
DV
1240 intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
1241
1242 if (active_outputs & intel_sdvo_connector->output_flag)
1243 return true;
1244 else
1245 return false;
1246}
1247
1248static bool intel_sdvo_get_hw_state(struct intel_encoder *encoder,
1249 enum pipe *pipe)
1250{
1251 struct drm_device *dev = encoder->base.dev;
79e53945 1252 struct drm_i915_private *dev_priv = dev->dev_private;
4ac41f47
DV
1253 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
1254 u32 tmp;
1255
1256 tmp = I915_READ(intel_sdvo->sdvo_reg);
1257
1258 if (!(tmp & SDVO_ENABLE))
1259 return false;
1260
1261 if (HAS_PCH_CPT(dev))
1262 *pipe = PORT_TO_PIPE_CPT(tmp);
1263 else
1264 *pipe = PORT_TO_PIPE(tmp);
1265
1266 return true;
1267}
1268
ce22c320
DV
1269static void intel_disable_sdvo(struct intel_encoder *encoder)
1270{
1271 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
1272 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
1273 u32 temp;
1274
1275 intel_sdvo_set_active_outputs(intel_sdvo, 0);
1276 if (0)
1277 intel_sdvo_set_encoder_power_state(intel_sdvo,
1278 DRM_MODE_DPMS_OFF);
1279
1280 temp = I915_READ(intel_sdvo->sdvo_reg);
1281 if ((temp & SDVO_ENABLE) != 0) {
776ca7cf
CW
1282 /* HW workaround for IBX, we need to move the port to
1283 * transcoder A before disabling it. */
1284 if (HAS_PCH_IBX(encoder->base.dev)) {
1285 struct drm_crtc *crtc = encoder->base.crtc;
1286 int pipe = crtc ? to_intel_crtc(crtc)->pipe : -1;
1287
1288 if (temp & SDVO_PIPE_B_SELECT) {
1289 temp &= ~SDVO_PIPE_B_SELECT;
1290 I915_WRITE(intel_sdvo->sdvo_reg, temp);
1291 POSTING_READ(intel_sdvo->sdvo_reg);
1292
1293 /* Again we need to write this twice. */
1294 I915_WRITE(intel_sdvo->sdvo_reg, temp);
1295 POSTING_READ(intel_sdvo->sdvo_reg);
1296
1297 /* Transcoder selection bits only update
1298 * effectively on vblank. */
1299 if (crtc)
1300 intel_wait_for_vblank(encoder->base.dev, pipe);
1301 else
1302 msleep(50);
1303 }
1304 }
1305
ce22c320
DV
1306 intel_sdvo_write_sdvox(intel_sdvo, temp & ~SDVO_ENABLE);
1307 }
1308}
1309
1310static void intel_enable_sdvo(struct intel_encoder *encoder)
1311{
1312 struct drm_device *dev = encoder->base.dev;
1313 struct drm_i915_private *dev_priv = dev->dev_private;
1314 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
1315 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
79e53945 1316 u32 temp;
ce22c320
DV
1317 bool input1, input2;
1318 int i;
1319 u8 status;
1320
1321 temp = I915_READ(intel_sdvo->sdvo_reg);
776ca7cf
CW
1322 if ((temp & SDVO_ENABLE) == 0) {
1323 /* HW workaround for IBX, we need to move the port
dc0fa718
PZ
1324 * to transcoder A before disabling it, so restore it here. */
1325 if (HAS_PCH_IBX(dev))
1326 temp |= SDVO_PIPE_SEL(intel_crtc->pipe);
776ca7cf 1327
ce22c320 1328 intel_sdvo_write_sdvox(intel_sdvo, temp | SDVO_ENABLE);
776ca7cf 1329 }
ce22c320
DV
1330 for (i = 0; i < 2; i++)
1331 intel_wait_for_vblank(dev, intel_crtc->pipe);
1332
1333 status = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
1334 /* Warn if the device reported failure to sync.
1335 * A lot of SDVO devices fail to notify of sync, but it's
1336 * a given it the status is a success, we succeeded.
1337 */
1338 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1339 DRM_DEBUG_KMS("First %s output reported failure to "
1340 "sync\n", SDVO_NAME(intel_sdvo));
1341 }
1342
1343 if (0)
1344 intel_sdvo_set_encoder_power_state(intel_sdvo,
1345 DRM_MODE_DPMS_ON);
1346 intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
1347}
1348
b2cabb0e 1349static void intel_sdvo_dpms(struct drm_connector *connector, int mode)
79e53945 1350{
b2cabb0e
DV
1351 struct drm_crtc *crtc;
1352 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
1353
1354 /* dvo supports only 2 dpms states. */
1355 if (mode != DRM_MODE_DPMS_ON)
1356 mode = DRM_MODE_DPMS_OFF;
1357
1358 if (mode == connector->dpms)
1359 return;
1360
1361 connector->dpms = mode;
1362
1363 /* Only need to change hw state when actually enabled */
1364 crtc = intel_sdvo->base.base.crtc;
1365 if (!crtc) {
1366 intel_sdvo->base.connectors_active = false;
1367 return;
1368 }
79e53945
JB
1369
1370 if (mode != DRM_MODE_DPMS_ON) {
ea5b213a 1371 intel_sdvo_set_active_outputs(intel_sdvo, 0);
79e53945 1372 if (0)
ea5b213a 1373 intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
79e53945 1374
b2cabb0e
DV
1375 intel_sdvo->base.connectors_active = false;
1376
1377 intel_crtc_update_dpms(crtc);
79e53945 1378 } else {
b2cabb0e
DV
1379 intel_sdvo->base.connectors_active = true;
1380
1381 intel_crtc_update_dpms(crtc);
79e53945
JB
1382
1383 if (0)
ea5b213a
CW
1384 intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
1385 intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
79e53945 1386 }
0a91ca29 1387
b980514c 1388 intel_modeset_check_state(connector->dev);
79e53945
JB
1389}
1390
79e53945
JB
1391static int intel_sdvo_mode_valid(struct drm_connector *connector,
1392 struct drm_display_mode *mode)
1393{
df0e9248 1394 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
79e53945
JB
1395
1396 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1397 return MODE_NO_DBLESCAN;
1398
ea5b213a 1399 if (intel_sdvo->pixel_clock_min > mode->clock)
79e53945
JB
1400 return MODE_CLOCK_LOW;
1401
ea5b213a 1402 if (intel_sdvo->pixel_clock_max < mode->clock)
79e53945
JB
1403 return MODE_CLOCK_HIGH;
1404
8545423a 1405 if (intel_sdvo->is_lvds) {
ea5b213a 1406 if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
12682a97 1407 return MODE_PANEL;
1408
ea5b213a 1409 if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
12682a97 1410 return MODE_PANEL;
1411 }
1412
79e53945
JB
1413 return MODE_OK;
1414}
1415
ea5b213a 1416static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
79e53945 1417{
1a3665c8 1418 BUILD_BUG_ON(sizeof(*caps) != 8);
e957d772
CW
1419 if (!intel_sdvo_get_value(intel_sdvo,
1420 SDVO_CMD_GET_DEVICE_CAPS,
1421 caps, sizeof(*caps)))
1422 return false;
1423
1424 DRM_DEBUG_KMS("SDVO capabilities:\n"
1425 " vendor_id: %d\n"
1426 " device_id: %d\n"
1427 " device_rev_id: %d\n"
1428 " sdvo_version_major: %d\n"
1429 " sdvo_version_minor: %d\n"
1430 " sdvo_inputs_mask: %d\n"
1431 " smooth_scaling: %d\n"
1432 " sharp_scaling: %d\n"
1433 " up_scaling: %d\n"
1434 " down_scaling: %d\n"
1435 " stall_support: %d\n"
1436 " output_flags: %d\n",
1437 caps->vendor_id,
1438 caps->device_id,
1439 caps->device_rev_id,
1440 caps->sdvo_version_major,
1441 caps->sdvo_version_minor,
1442 caps->sdvo_inputs_mask,
1443 caps->smooth_scaling,
1444 caps->sharp_scaling,
1445 caps->up_scaling,
1446 caps->down_scaling,
1447 caps->stall_support,
1448 caps->output_flags);
1449
1450 return true;
79e53945
JB
1451}
1452
5fa7ac9c 1453static uint16_t intel_sdvo_get_hotplug_support(struct intel_sdvo *intel_sdvo)
79e53945 1454{
768b107e 1455 struct drm_device *dev = intel_sdvo->base.base.dev;
5fa7ac9c 1456 uint16_t hotplug;
79e53945 1457
768b107e
DV
1458 /* HW Erratum: SDVO Hotplug is broken on all i945G chips, there's noise
1459 * on the line. */
1460 if (IS_I945G(dev) || IS_I945GM(dev))
5fa7ac9c 1461 return 0;
768b107e 1462
5fa7ac9c
JN
1463 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1464 &hotplug, sizeof(hotplug)))
1465 return 0;
768b107e 1466
5fa7ac9c 1467 return hotplug;
79e53945
JB
1468}
1469
cc68c81a 1470static void intel_sdvo_enable_hotplug(struct intel_encoder *encoder)
79e53945 1471{
cc68c81a 1472 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
79e53945 1473
5fa7ac9c
JN
1474 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG,
1475 &intel_sdvo->hotplug_active, 2);
79e53945
JB
1476}
1477
fb7a46f3 1478static bool
ea5b213a 1479intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
fb7a46f3 1480{
bc65212c 1481 /* Is there more than one type of output? */
2294488d 1482 return hweight16(intel_sdvo->caps.output_flags) > 1;
fb7a46f3 1483}
1484
f899fc64 1485static struct edid *
e957d772 1486intel_sdvo_get_edid(struct drm_connector *connector)
f899fc64 1487{
e957d772
CW
1488 struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
1489 return drm_get_edid(connector, &sdvo->ddc);
f899fc64
CW
1490}
1491
ff482d83
CW
1492/* Mac mini hack -- use the same DDC as the analog connector */
1493static struct edid *
1494intel_sdvo_get_analog_edid(struct drm_connector *connector)
1495{
f899fc64 1496 struct drm_i915_private *dev_priv = connector->dev->dev_private;
ff482d83 1497
0c1dab89 1498 return drm_get_edid(connector,
3bd7d909
DK
1499 intel_gmbus_get_adapter(dev_priv,
1500 dev_priv->crt_ddc_pin));
ff482d83
CW
1501}
1502
c43b5634 1503static enum drm_connector_status
8bf38485 1504intel_sdvo_tmds_sink_detect(struct drm_connector *connector)
9dff6af8 1505{
df0e9248 1506 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
9d1a903d
CW
1507 enum drm_connector_status status;
1508 struct edid *edid;
9dff6af8 1509
e957d772 1510 edid = intel_sdvo_get_edid(connector);
57cdaf90 1511
ea5b213a 1512 if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
e957d772 1513 u8 ddc, saved_ddc = intel_sdvo->ddc_bus;
9d1a903d 1514
7c3f0a27
ZY
1515 /*
1516 * Don't use the 1 as the argument of DDC bus switch to get
1517 * the EDID. It is used for SDVO SPD ROM.
1518 */
9d1a903d 1519 for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
e957d772
CW
1520 intel_sdvo->ddc_bus = ddc;
1521 edid = intel_sdvo_get_edid(connector);
1522 if (edid)
7c3f0a27 1523 break;
7c3f0a27 1524 }
e957d772
CW
1525 /*
1526 * If we found the EDID on the other bus,
1527 * assume that is the correct DDC bus.
1528 */
1529 if (edid == NULL)
1530 intel_sdvo->ddc_bus = saved_ddc;
7c3f0a27 1531 }
9d1a903d
CW
1532
1533 /*
1534 * When there is no edid and no monitor is connected with VGA
1535 * port, try to use the CRT ddc to read the EDID for DVI-connector.
57cdaf90 1536 */
ff482d83
CW
1537 if (edid == NULL)
1538 edid = intel_sdvo_get_analog_edid(connector);
149c36a3 1539
2f551c84 1540 status = connector_status_unknown;
9dff6af8 1541 if (edid != NULL) {
149c36a3 1542 /* DDC bus is shared, match EDID to connector type */
9d1a903d
CW
1543 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
1544 status = connector_status_connected;
da79de97
CW
1545 if (intel_sdvo->is_hdmi) {
1546 intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
1547 intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
abedc077
VS
1548 intel_sdvo->rgb_quant_range_selectable =
1549 drm_rgb_quant_range_selectable(edid);
da79de97 1550 }
13946743
CW
1551 } else
1552 status = connector_status_disconnected;
9d1a903d
CW
1553 kfree(edid);
1554 }
7f36e7ed
CW
1555
1556 if (status == connector_status_connected) {
1557 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
c3e5f67b
DV
1558 if (intel_sdvo_connector->force_audio != HDMI_AUDIO_AUTO)
1559 intel_sdvo->has_hdmi_audio = (intel_sdvo_connector->force_audio == HDMI_AUDIO_ON);
7f36e7ed
CW
1560 }
1561
2b8d33f7 1562 return status;
9dff6af8
ML
1563}
1564
52220085
CW
1565static bool
1566intel_sdvo_connector_matches_edid(struct intel_sdvo_connector *sdvo,
1567 struct edid *edid)
1568{
1569 bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
1570 bool connector_is_digital = !!IS_DIGITAL(sdvo);
1571
1572 DRM_DEBUG_KMS("connector_is_digital? %d, monitor_is_digital? %d\n",
1573 connector_is_digital, monitor_is_digital);
1574 return connector_is_digital == monitor_is_digital;
1575}
1576
7b334fcb 1577static enum drm_connector_status
930a9e28 1578intel_sdvo_detect(struct drm_connector *connector, bool force)
79e53945 1579{
fb7a46f3 1580 uint16_t response;
df0e9248 1581 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
615fb93f 1582 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
14571b4c 1583 enum drm_connector_status ret;
79e53945 1584
fc37381c
CW
1585 if (!intel_sdvo_get_value(intel_sdvo,
1586 SDVO_CMD_GET_ATTACHED_DISPLAYS,
1587 &response, 2))
32aad86f 1588 return connector_status_unknown;
79e53945 1589
e957d772
CW
1590 DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
1591 response & 0xff, response >> 8,
1592 intel_sdvo_connector->output_flag);
e2f0ba97 1593
fb7a46f3 1594 if (response == 0)
79e53945 1595 return connector_status_disconnected;
fb7a46f3 1596
ea5b213a 1597 intel_sdvo->attached_output = response;
14571b4c 1598
97aaf910
CW
1599 intel_sdvo->has_hdmi_monitor = false;
1600 intel_sdvo->has_hdmi_audio = false;
abedc077 1601 intel_sdvo->rgb_quant_range_selectable = false;
97aaf910 1602
615fb93f 1603 if ((intel_sdvo_connector->output_flag & response) == 0)
14571b4c 1604 ret = connector_status_disconnected;
13946743 1605 else if (IS_TMDS(intel_sdvo_connector))
8bf38485 1606 ret = intel_sdvo_tmds_sink_detect(connector);
13946743
CW
1607 else {
1608 struct edid *edid;
1609
1610 /* if we have an edid check it matches the connection */
1611 edid = intel_sdvo_get_edid(connector);
1612 if (edid == NULL)
1613 edid = intel_sdvo_get_analog_edid(connector);
1614 if (edid != NULL) {
52220085
CW
1615 if (intel_sdvo_connector_matches_edid(intel_sdvo_connector,
1616 edid))
13946743 1617 ret = connector_status_connected;
52220085
CW
1618 else
1619 ret = connector_status_disconnected;
1620
13946743
CW
1621 kfree(edid);
1622 } else
1623 ret = connector_status_connected;
1624 }
14571b4c
ZW
1625
1626 /* May update encoder flag for like clock for SDVO TV, etc.*/
1627 if (ret == connector_status_connected) {
ea5b213a
CW
1628 intel_sdvo->is_tv = false;
1629 intel_sdvo->is_lvds = false;
1630 intel_sdvo->base.needs_tv_clock = false;
14571b4c
ZW
1631
1632 if (response & SDVO_TV_MASK) {
ea5b213a
CW
1633 intel_sdvo->is_tv = true;
1634 intel_sdvo->base.needs_tv_clock = true;
14571b4c
ZW
1635 }
1636 if (response & SDVO_LVDS_MASK)
8545423a 1637 intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
fb7a46f3 1638 }
14571b4c
ZW
1639
1640 return ret;
79e53945
JB
1641}
1642
e2f0ba97 1643static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
79e53945 1644{
ff482d83 1645 struct edid *edid;
79e53945
JB
1646
1647 /* set the bus switch and get the modes */
e957d772 1648 edid = intel_sdvo_get_edid(connector);
79e53945 1649
57cdaf90
KP
1650 /*
1651 * Mac mini hack. On this device, the DVI-I connector shares one DDC
1652 * link between analog and digital outputs. So, if the regular SDVO
1653 * DDC fails, check to see if the analog output is disconnected, in
1654 * which case we'll look there for the digital DDC data.
e2f0ba97 1655 */
f899fc64
CW
1656 if (edid == NULL)
1657 edid = intel_sdvo_get_analog_edid(connector);
1658
ff482d83 1659 if (edid != NULL) {
52220085
CW
1660 if (intel_sdvo_connector_matches_edid(to_intel_sdvo_connector(connector),
1661 edid)) {
0c1dab89
CW
1662 drm_mode_connector_update_edid_property(connector, edid);
1663 drm_add_edid_modes(connector, edid);
1664 }
13946743 1665
ff482d83 1666 kfree(edid);
e2f0ba97 1667 }
e2f0ba97
JB
1668}
1669
1670/*
1671 * Set of SDVO TV modes.
1672 * Note! This is in reply order (see loop in get_tv_modes).
1673 * XXX: all 60Hz refresh?
1674 */
b1f559ec 1675static const struct drm_display_mode sdvo_tv_modes[] = {
7026d4ac
ZW
1676 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1677 416, 0, 200, 201, 232, 233, 0,
e2f0ba97 1678 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1679 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1680 416, 0, 240, 241, 272, 273, 0,
e2f0ba97 1681 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1682 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1683 496, 0, 300, 301, 332, 333, 0,
e2f0ba97 1684 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1685 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1686 736, 0, 350, 351, 382, 383, 0,
e2f0ba97 1687 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1688 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1689 736, 0, 400, 401, 432, 433, 0,
e2f0ba97 1690 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1691 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1692 736, 0, 480, 481, 512, 513, 0,
e2f0ba97 1693 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1694 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1695 800, 0, 480, 481, 512, 513, 0,
e2f0ba97 1696 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1697 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1698 800, 0, 576, 577, 608, 609, 0,
e2f0ba97 1699 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1700 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1701 816, 0, 350, 351, 382, 383, 0,
e2f0ba97 1702 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1703 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1704 816, 0, 400, 401, 432, 433, 0,
e2f0ba97 1705 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1706 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1707 816, 0, 480, 481, 512, 513, 0,
e2f0ba97 1708 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1709 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1710 816, 0, 540, 541, 572, 573, 0,
e2f0ba97 1711 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1712 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1713 816, 0, 576, 577, 608, 609, 0,
e2f0ba97 1714 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1715 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1716 864, 0, 576, 577, 608, 609, 0,
e2f0ba97 1717 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1718 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1719 896, 0, 600, 601, 632, 633, 0,
e2f0ba97 1720 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1721 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1722 928, 0, 624, 625, 656, 657, 0,
e2f0ba97 1723 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1724 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1725 1016, 0, 766, 767, 798, 799, 0,
e2f0ba97 1726 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1727 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1728 1120, 0, 768, 769, 800, 801, 0,
e2f0ba97 1729 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1730 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1731 1376, 0, 1024, 1025, 1056, 1057, 0,
e2f0ba97
JB
1732 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1733};
1734
1735static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1736{
df0e9248 1737 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
7026d4ac 1738 struct intel_sdvo_sdtv_resolution_request tv_res;
ce6feabd
ZY
1739 uint32_t reply = 0, format_map = 0;
1740 int i;
e2f0ba97
JB
1741
1742 /* Read the list of supported input resolutions for the selected TV
1743 * format.
1744 */
40039750 1745 format_map = 1 << intel_sdvo->tv_format_index;
ce6feabd 1746 memcpy(&tv_res, &format_map,
32aad86f 1747 min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
ce6feabd 1748
32aad86f
CW
1749 if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
1750 return;
ce6feabd 1751
32aad86f 1752 BUILD_BUG_ON(sizeof(tv_res) != 3);
e957d772
CW
1753 if (!intel_sdvo_write_cmd(intel_sdvo,
1754 SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
32aad86f
CW
1755 &tv_res, sizeof(tv_res)))
1756 return;
1757 if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
e2f0ba97
JB
1758 return;
1759
1760 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
7026d4ac
ZW
1761 if (reply & (1 << i)) {
1762 struct drm_display_mode *nmode;
1763 nmode = drm_mode_duplicate(connector->dev,
32aad86f 1764 &sdvo_tv_modes[i]);
7026d4ac
ZW
1765 if (nmode)
1766 drm_mode_probed_add(connector, nmode);
1767 }
e2f0ba97
JB
1768}
1769
7086c87f
ML
1770static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1771{
df0e9248 1772 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
7086c87f 1773 struct drm_i915_private *dev_priv = connector->dev->dev_private;
12682a97 1774 struct drm_display_mode *newmode;
7086c87f
ML
1775
1776 /*
1777 * Attempt to get the mode list from DDC.
1778 * Assume that the preferred modes are
1779 * arranged in priority order.
1780 */
f899fc64 1781 intel_ddc_get_modes(connector, intel_sdvo->i2c);
7086c87f 1782 if (list_empty(&connector->probed_modes) == false)
12682a97 1783 goto end;
7086c87f
ML
1784
1785 /* Fetch modes from VBT */
1786 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
7086c87f
ML
1787 newmode = drm_mode_duplicate(connector->dev,
1788 dev_priv->sdvo_lvds_vbt_mode);
1789 if (newmode != NULL) {
1790 /* Guarantee the mode is preferred */
1791 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1792 DRM_MODE_TYPE_DRIVER);
1793 drm_mode_probed_add(connector, newmode);
1794 }
1795 }
12682a97 1796
1797end:
1798 list_for_each_entry(newmode, &connector->probed_modes, head) {
1799 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
ea5b213a 1800 intel_sdvo->sdvo_lvds_fixed_mode =
12682a97 1801 drm_mode_duplicate(connector->dev, newmode);
6c9547ff 1802
8545423a 1803 intel_sdvo->is_lvds = true;
12682a97 1804 break;
1805 }
1806 }
1807
7086c87f
ML
1808}
1809
e2f0ba97
JB
1810static int intel_sdvo_get_modes(struct drm_connector *connector)
1811{
615fb93f 1812 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
e2f0ba97 1813
615fb93f 1814 if (IS_TV(intel_sdvo_connector))
e2f0ba97 1815 intel_sdvo_get_tv_modes(connector);
615fb93f 1816 else if (IS_LVDS(intel_sdvo_connector))
7086c87f 1817 intel_sdvo_get_lvds_modes(connector);
e2f0ba97
JB
1818 else
1819 intel_sdvo_get_ddc_modes(connector);
1820
32aad86f 1821 return !list_empty(&connector->probed_modes);
79e53945
JB
1822}
1823
fcc8d672
CW
1824static void
1825intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
b9219c5e 1826{
615fb93f 1827 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
b9219c5e
ZY
1828 struct drm_device *dev = connector->dev;
1829
c5521706
CW
1830 if (intel_sdvo_connector->left)
1831 drm_property_destroy(dev, intel_sdvo_connector->left);
1832 if (intel_sdvo_connector->right)
1833 drm_property_destroy(dev, intel_sdvo_connector->right);
1834 if (intel_sdvo_connector->top)
1835 drm_property_destroy(dev, intel_sdvo_connector->top);
1836 if (intel_sdvo_connector->bottom)
1837 drm_property_destroy(dev, intel_sdvo_connector->bottom);
1838 if (intel_sdvo_connector->hpos)
1839 drm_property_destroy(dev, intel_sdvo_connector->hpos);
1840 if (intel_sdvo_connector->vpos)
1841 drm_property_destroy(dev, intel_sdvo_connector->vpos);
1842 if (intel_sdvo_connector->saturation)
1843 drm_property_destroy(dev, intel_sdvo_connector->saturation);
1844 if (intel_sdvo_connector->contrast)
1845 drm_property_destroy(dev, intel_sdvo_connector->contrast);
1846 if (intel_sdvo_connector->hue)
1847 drm_property_destroy(dev, intel_sdvo_connector->hue);
1848 if (intel_sdvo_connector->sharpness)
1849 drm_property_destroy(dev, intel_sdvo_connector->sharpness);
1850 if (intel_sdvo_connector->flicker_filter)
1851 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter);
1852 if (intel_sdvo_connector->flicker_filter_2d)
1853 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_2d);
1854 if (intel_sdvo_connector->flicker_filter_adaptive)
1855 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_adaptive);
1856 if (intel_sdvo_connector->tv_luma_filter)
1857 drm_property_destroy(dev, intel_sdvo_connector->tv_luma_filter);
1858 if (intel_sdvo_connector->tv_chroma_filter)
1859 drm_property_destroy(dev, intel_sdvo_connector->tv_chroma_filter);
e044218a
CW
1860 if (intel_sdvo_connector->dot_crawl)
1861 drm_property_destroy(dev, intel_sdvo_connector->dot_crawl);
c5521706
CW
1862 if (intel_sdvo_connector->brightness)
1863 drm_property_destroy(dev, intel_sdvo_connector->brightness);
b9219c5e
ZY
1864}
1865
79e53945
JB
1866static void intel_sdvo_destroy(struct drm_connector *connector)
1867{
615fb93f 1868 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
79e53945 1869
c5521706 1870 if (intel_sdvo_connector->tv_format)
ce6feabd 1871 drm_property_destroy(connector->dev,
c5521706 1872 intel_sdvo_connector->tv_format);
b9219c5e 1873
d2a82a6f 1874 intel_sdvo_destroy_enhance_property(connector);
79e53945
JB
1875 drm_sysfs_connector_remove(connector);
1876 drm_connector_cleanup(connector);
4b745b1e 1877 kfree(intel_sdvo_connector);
79e53945
JB
1878}
1879
1aad7ac0
CW
1880static bool intel_sdvo_detect_hdmi_audio(struct drm_connector *connector)
1881{
1882 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
1883 struct edid *edid;
1884 bool has_audio = false;
1885
1886 if (!intel_sdvo->is_hdmi)
1887 return false;
1888
1889 edid = intel_sdvo_get_edid(connector);
1890 if (edid != NULL && edid->input & DRM_EDID_INPUT_DIGITAL)
1891 has_audio = drm_detect_monitor_audio(edid);
38ab8a20 1892 kfree(edid);
1aad7ac0
CW
1893
1894 return has_audio;
1895}
1896
ce6feabd
ZY
1897static int
1898intel_sdvo_set_property(struct drm_connector *connector,
1899 struct drm_property *property,
1900 uint64_t val)
1901{
df0e9248 1902 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
615fb93f 1903 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
e953fd7b 1904 struct drm_i915_private *dev_priv = connector->dev->dev_private;
b9219c5e 1905 uint16_t temp_value;
32aad86f
CW
1906 uint8_t cmd;
1907 int ret;
ce6feabd 1908
662595df 1909 ret = drm_object_property_set_value(&connector->base, property, val);
32aad86f
CW
1910 if (ret)
1911 return ret;
ce6feabd 1912
3f43c48d 1913 if (property == dev_priv->force_audio_property) {
1aad7ac0
CW
1914 int i = val;
1915 bool has_audio;
1916
1917 if (i == intel_sdvo_connector->force_audio)
7f36e7ed
CW
1918 return 0;
1919
1aad7ac0 1920 intel_sdvo_connector->force_audio = i;
7f36e7ed 1921
c3e5f67b 1922 if (i == HDMI_AUDIO_AUTO)
1aad7ac0
CW
1923 has_audio = intel_sdvo_detect_hdmi_audio(connector);
1924 else
c3e5f67b 1925 has_audio = (i == HDMI_AUDIO_ON);
7f36e7ed 1926
1aad7ac0 1927 if (has_audio == intel_sdvo->has_hdmi_audio)
7f36e7ed 1928 return 0;
7f36e7ed 1929
1aad7ac0 1930 intel_sdvo->has_hdmi_audio = has_audio;
7f36e7ed
CW
1931 goto done;
1932 }
1933
e953fd7b 1934 if (property == dev_priv->broadcast_rgb_property) {
55bc60db
VS
1935 switch (val) {
1936 case INTEL_BROADCAST_RGB_AUTO:
1937 intel_sdvo->color_range_auto = true;
1938 break;
1939 case INTEL_BROADCAST_RGB_FULL:
1940 intel_sdvo->color_range_auto = false;
1941 intel_sdvo->color_range = 0;
1942 break;
1943 case INTEL_BROADCAST_RGB_LIMITED:
1944 intel_sdvo->color_range_auto = false;
4f3a8bc7
PZ
1945 /* FIXME: this bit is only valid when using TMDS
1946 * encoding and 8 bit per color mode. */
1947 intel_sdvo->color_range = HDMI_COLOR_RANGE_16_235;
55bc60db
VS
1948 break;
1949 default:
1950 return -EINVAL;
1951 }
7f36e7ed
CW
1952 goto done;
1953 }
1954
c5521706
CW
1955#define CHECK_PROPERTY(name, NAME) \
1956 if (intel_sdvo_connector->name == property) { \
1957 if (intel_sdvo_connector->cur_##name == temp_value) return 0; \
1958 if (intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
1959 cmd = SDVO_CMD_SET_##NAME; \
1960 intel_sdvo_connector->cur_##name = temp_value; \
1961 goto set_value; \
1962 }
1963
1964 if (property == intel_sdvo_connector->tv_format) {
32aad86f
CW
1965 if (val >= TV_FORMAT_NUM)
1966 return -EINVAL;
1967
40039750 1968 if (intel_sdvo->tv_format_index ==
615fb93f 1969 intel_sdvo_connector->tv_format_supported[val])
32aad86f 1970 return 0;
ce6feabd 1971
40039750 1972 intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[val];
c5521706 1973 goto done;
32aad86f 1974 } else if (IS_TV_OR_LVDS(intel_sdvo_connector)) {
b9219c5e 1975 temp_value = val;
c5521706 1976 if (intel_sdvo_connector->left == property) {
662595df 1977 drm_object_property_set_value(&connector->base,
c5521706 1978 intel_sdvo_connector->right, val);
615fb93f 1979 if (intel_sdvo_connector->left_margin == temp_value)
32aad86f 1980 return 0;
b9219c5e 1981
615fb93f
CW
1982 intel_sdvo_connector->left_margin = temp_value;
1983 intel_sdvo_connector->right_margin = temp_value;
1984 temp_value = intel_sdvo_connector->max_hscan -
c5521706 1985 intel_sdvo_connector->left_margin;
b9219c5e 1986 cmd = SDVO_CMD_SET_OVERSCAN_H;
c5521706
CW
1987 goto set_value;
1988 } else if (intel_sdvo_connector->right == property) {
662595df 1989 drm_object_property_set_value(&connector->base,
c5521706 1990 intel_sdvo_connector->left, val);
615fb93f 1991 if (intel_sdvo_connector->right_margin == temp_value)
32aad86f 1992 return 0;
b9219c5e 1993
615fb93f
CW
1994 intel_sdvo_connector->left_margin = temp_value;
1995 intel_sdvo_connector->right_margin = temp_value;
1996 temp_value = intel_sdvo_connector->max_hscan -
1997 intel_sdvo_connector->left_margin;
b9219c5e 1998 cmd = SDVO_CMD_SET_OVERSCAN_H;
c5521706
CW
1999 goto set_value;
2000 } else if (intel_sdvo_connector->top == property) {
662595df 2001 drm_object_property_set_value(&connector->base,
c5521706 2002 intel_sdvo_connector->bottom, val);
615fb93f 2003 if (intel_sdvo_connector->top_margin == temp_value)
32aad86f 2004 return 0;
b9219c5e 2005
615fb93f
CW
2006 intel_sdvo_connector->top_margin = temp_value;
2007 intel_sdvo_connector->bottom_margin = temp_value;
2008 temp_value = intel_sdvo_connector->max_vscan -
c5521706 2009 intel_sdvo_connector->top_margin;
b9219c5e 2010 cmd = SDVO_CMD_SET_OVERSCAN_V;
c5521706
CW
2011 goto set_value;
2012 } else if (intel_sdvo_connector->bottom == property) {
662595df 2013 drm_object_property_set_value(&connector->base,
c5521706 2014 intel_sdvo_connector->top, val);
615fb93f 2015 if (intel_sdvo_connector->bottom_margin == temp_value)
32aad86f
CW
2016 return 0;
2017
615fb93f
CW
2018 intel_sdvo_connector->top_margin = temp_value;
2019 intel_sdvo_connector->bottom_margin = temp_value;
2020 temp_value = intel_sdvo_connector->max_vscan -
c5521706 2021 intel_sdvo_connector->top_margin;
b9219c5e 2022 cmd = SDVO_CMD_SET_OVERSCAN_V;
c5521706
CW
2023 goto set_value;
2024 }
2025 CHECK_PROPERTY(hpos, HPOS)
2026 CHECK_PROPERTY(vpos, VPOS)
2027 CHECK_PROPERTY(saturation, SATURATION)
2028 CHECK_PROPERTY(contrast, CONTRAST)
2029 CHECK_PROPERTY(hue, HUE)
2030 CHECK_PROPERTY(brightness, BRIGHTNESS)
2031 CHECK_PROPERTY(sharpness, SHARPNESS)
2032 CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
2033 CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
2034 CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
2035 CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
2036 CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
e044218a 2037 CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
c5521706 2038 }
b9219c5e 2039
c5521706 2040 return -EINVAL; /* unknown property */
b9219c5e 2041
c5521706
CW
2042set_value:
2043 if (!intel_sdvo_set_value(intel_sdvo, cmd, &temp_value, 2))
2044 return -EIO;
b9219c5e 2045
b9219c5e 2046
c5521706 2047done:
c0c36b94
CW
2048 if (intel_sdvo->base.base.crtc)
2049 intel_crtc_restore_mode(intel_sdvo->base.base.crtc);
c5521706 2050
32aad86f 2051 return 0;
c5521706 2052#undef CHECK_PROPERTY
ce6feabd
ZY
2053}
2054
79e53945 2055static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
b2cabb0e 2056 .dpms = intel_sdvo_dpms,
79e53945
JB
2057 .detect = intel_sdvo_detect,
2058 .fill_modes = drm_helper_probe_single_connector_modes,
ce6feabd 2059 .set_property = intel_sdvo_set_property,
79e53945
JB
2060 .destroy = intel_sdvo_destroy,
2061};
2062
2063static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
2064 .get_modes = intel_sdvo_get_modes,
2065 .mode_valid = intel_sdvo_mode_valid,
df0e9248 2066 .best_encoder = intel_best_encoder,
79e53945
JB
2067};
2068
b358d0a6 2069static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
79e53945 2070{
890f3359 2071 struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
d2a82a6f 2072
ea5b213a 2073 if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
d2a82a6f 2074 drm_mode_destroy(encoder->dev,
ea5b213a 2075 intel_sdvo->sdvo_lvds_fixed_mode);
d2a82a6f 2076
e957d772 2077 i2c_del_adapter(&intel_sdvo->ddc);
ea5b213a 2078 intel_encoder_destroy(encoder);
79e53945
JB
2079}
2080
2081static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
2082 .destroy = intel_sdvo_enc_destroy,
2083};
2084
b66d8424
CW
2085static void
2086intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
2087{
2088 uint16_t mask = 0;
2089 unsigned int num_bits;
2090
2091 /* Make a mask of outputs less than or equal to our own priority in the
2092 * list.
2093 */
2094 switch (sdvo->controlled_output) {
2095 case SDVO_OUTPUT_LVDS1:
2096 mask |= SDVO_OUTPUT_LVDS1;
2097 case SDVO_OUTPUT_LVDS0:
2098 mask |= SDVO_OUTPUT_LVDS0;
2099 case SDVO_OUTPUT_TMDS1:
2100 mask |= SDVO_OUTPUT_TMDS1;
2101 case SDVO_OUTPUT_TMDS0:
2102 mask |= SDVO_OUTPUT_TMDS0;
2103 case SDVO_OUTPUT_RGB1:
2104 mask |= SDVO_OUTPUT_RGB1;
2105 case SDVO_OUTPUT_RGB0:
2106 mask |= SDVO_OUTPUT_RGB0;
2107 break;
2108 }
2109
2110 /* Count bits to find what number we are in the priority list. */
2111 mask &= sdvo->caps.output_flags;
2112 num_bits = hweight16(mask);
2113 /* If more than 3 outputs, default to DDC bus 3 for now. */
2114 if (num_bits > 3)
2115 num_bits = 3;
2116
2117 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
2118 sdvo->ddc_bus = 1 << num_bits;
2119}
79e53945 2120
e2f0ba97
JB
2121/**
2122 * Choose the appropriate DDC bus for control bus switch command for this
2123 * SDVO output based on the controlled output.
2124 *
2125 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
2126 * outputs, then LVDS outputs.
2127 */
2128static void
b1083333 2129intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
ea5b213a 2130 struct intel_sdvo *sdvo, u32 reg)
e2f0ba97 2131{
b1083333 2132 struct sdvo_device_mapping *mapping;
e2f0ba97 2133
eef4eacb 2134 if (sdvo->is_sdvob)
b1083333
AJ
2135 mapping = &(dev_priv->sdvo_mappings[0]);
2136 else
2137 mapping = &(dev_priv->sdvo_mappings[1]);
e2f0ba97 2138
b66d8424
CW
2139 if (mapping->initialized)
2140 sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
2141 else
2142 intel_sdvo_guess_ddc_bus(sdvo);
e2f0ba97
JB
2143}
2144
e957d772
CW
2145static void
2146intel_sdvo_select_i2c_bus(struct drm_i915_private *dev_priv,
2147 struct intel_sdvo *sdvo, u32 reg)
2148{
2149 struct sdvo_device_mapping *mapping;
46eb3036 2150 u8 pin;
e957d772 2151
eef4eacb 2152 if (sdvo->is_sdvob)
e957d772
CW
2153 mapping = &dev_priv->sdvo_mappings[0];
2154 else
2155 mapping = &dev_priv->sdvo_mappings[1];
2156
6cb1612a 2157 if (mapping->initialized && intel_gmbus_is_port_valid(mapping->i2c_pin))
e957d772 2158 pin = mapping->i2c_pin;
6cb1612a
JN
2159 else
2160 pin = GMBUS_PORT_DPB;
e957d772 2161
6cb1612a
JN
2162 sdvo->i2c = intel_gmbus_get_adapter(dev_priv, pin);
2163
2164 /* With gmbus we should be able to drive sdvo i2c at 2MHz, but somehow
2165 * our code totally fails once we start using gmbus. Hence fall back to
2166 * bit banging for now. */
2167 intel_gmbus_force_bit(sdvo->i2c, true);
e957d772
CW
2168}
2169
fbfcc4f3
JN
2170/* undo any changes intel_sdvo_select_i2c_bus() did to sdvo->i2c */
2171static void
2172intel_sdvo_unselect_i2c_bus(struct intel_sdvo *sdvo)
2173{
2174 intel_gmbus_force_bit(sdvo->i2c, false);
e957d772
CW
2175}
2176
e2f0ba97 2177static bool
e27d8538 2178intel_sdvo_is_hdmi_connector(struct intel_sdvo *intel_sdvo, int device)
e2f0ba97 2179{
97aaf910 2180 return intel_sdvo_check_supp_encode(intel_sdvo);
e2f0ba97
JB
2181}
2182
714605e4 2183static u8
eef4eacb 2184intel_sdvo_get_slave_addr(struct drm_device *dev, struct intel_sdvo *sdvo)
714605e4 2185{
2186 struct drm_i915_private *dev_priv = dev->dev_private;
2187 struct sdvo_device_mapping *my_mapping, *other_mapping;
2188
eef4eacb 2189 if (sdvo->is_sdvob) {
714605e4 2190 my_mapping = &dev_priv->sdvo_mappings[0];
2191 other_mapping = &dev_priv->sdvo_mappings[1];
2192 } else {
2193 my_mapping = &dev_priv->sdvo_mappings[1];
2194 other_mapping = &dev_priv->sdvo_mappings[0];
2195 }
2196
2197 /* If the BIOS described our SDVO device, take advantage of it. */
2198 if (my_mapping->slave_addr)
2199 return my_mapping->slave_addr;
2200
2201 /* If the BIOS only described a different SDVO device, use the
2202 * address that it isn't using.
2203 */
2204 if (other_mapping->slave_addr) {
2205 if (other_mapping->slave_addr == 0x70)
2206 return 0x72;
2207 else
2208 return 0x70;
2209 }
2210
2211 /* No SDVO device info is found for another DVO port,
2212 * so use mapping assumption we had before BIOS parsing.
2213 */
eef4eacb 2214 if (sdvo->is_sdvob)
714605e4 2215 return 0x70;
2216 else
2217 return 0x72;
2218}
2219
14571b4c 2220static void
df0e9248
CW
2221intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
2222 struct intel_sdvo *encoder)
14571b4c 2223{
df0e9248
CW
2224 drm_connector_init(encoder->base.base.dev,
2225 &connector->base.base,
2226 &intel_sdvo_connector_funcs,
2227 connector->base.base.connector_type);
6070a4a9 2228
df0e9248
CW
2229 drm_connector_helper_add(&connector->base.base,
2230 &intel_sdvo_connector_helper_funcs);
14571b4c 2231
8f4839e2 2232 connector->base.base.interlace_allowed = 1;
df0e9248
CW
2233 connector->base.base.doublescan_allowed = 0;
2234 connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
4ac41f47 2235 connector->base.get_hw_state = intel_sdvo_connector_get_hw_state;
14571b4c 2236
df0e9248
CW
2237 intel_connector_attach_encoder(&connector->base, &encoder->base);
2238 drm_sysfs_connector_add(&connector->base.base);
14571b4c 2239}
6070a4a9 2240
7f36e7ed 2241static void
55bc60db
VS
2242intel_sdvo_add_hdmi_properties(struct intel_sdvo *intel_sdvo,
2243 struct intel_sdvo_connector *connector)
7f36e7ed
CW
2244{
2245 struct drm_device *dev = connector->base.base.dev;
2246
3f43c48d 2247 intel_attach_force_audio_property(&connector->base.base);
55bc60db 2248 if (INTEL_INFO(dev)->gen >= 4 && IS_MOBILE(dev)) {
e953fd7b 2249 intel_attach_broadcast_rgb_property(&connector->base.base);
55bc60db
VS
2250 intel_sdvo->color_range_auto = true;
2251 }
7f36e7ed
CW
2252}
2253
fb7a46f3 2254static bool
ea5b213a 2255intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
fb7a46f3 2256{
4ef69c7a 2257 struct drm_encoder *encoder = &intel_sdvo->base.base;
14571b4c 2258 struct drm_connector *connector;
cc68c81a 2259 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
14571b4c 2260 struct intel_connector *intel_connector;
615fb93f 2261 struct intel_sdvo_connector *intel_sdvo_connector;
14571b4c 2262
615fb93f
CW
2263 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2264 if (!intel_sdvo_connector)
14571b4c
ZW
2265 return false;
2266
14571b4c 2267 if (device == 0) {
ea5b213a 2268 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
615fb93f 2269 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
14571b4c 2270 } else if (device == 1) {
ea5b213a 2271 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
615fb93f 2272 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
14571b4c
ZW
2273 }
2274
615fb93f 2275 intel_connector = &intel_sdvo_connector->base;
14571b4c 2276 connector = &intel_connector->base;
5fa7ac9c
JN
2277 if (intel_sdvo_get_hotplug_support(intel_sdvo) &
2278 intel_sdvo_connector->output_flag) {
cc68c81a 2279 connector->polled = DRM_CONNECTOR_POLL_HPD;
5fa7ac9c 2280 intel_sdvo->hotplug_active |= intel_sdvo_connector->output_flag;
cc68c81a
SF
2281 /* Some SDVO devices have one-shot hotplug interrupts.
2282 * Ensure that they get re-enabled when an interrupt happens.
2283 */
2284 intel_encoder->hot_plug = intel_sdvo_enable_hotplug;
2285 intel_sdvo_enable_hotplug(intel_encoder);
5fa7ac9c 2286 } else {
cc68c81a 2287 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
5fa7ac9c 2288 }
14571b4c
ZW
2289 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2290 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2291
e27d8538 2292 if (intel_sdvo_is_hdmi_connector(intel_sdvo, device)) {
14571b4c 2293 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
e27d8538 2294 intel_sdvo->is_hdmi = true;
14571b4c 2295 }
14571b4c 2296
df0e9248 2297 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
f797d221 2298 if (intel_sdvo->is_hdmi)
55bc60db 2299 intel_sdvo_add_hdmi_properties(intel_sdvo, intel_sdvo_connector);
14571b4c
ZW
2300
2301 return true;
2302}
2303
2304static bool
ea5b213a 2305intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
14571b4c 2306{
4ef69c7a
CW
2307 struct drm_encoder *encoder = &intel_sdvo->base.base;
2308 struct drm_connector *connector;
2309 struct intel_connector *intel_connector;
2310 struct intel_sdvo_connector *intel_sdvo_connector;
14571b4c 2311
615fb93f
CW
2312 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2313 if (!intel_sdvo_connector)
2314 return false;
14571b4c 2315
615fb93f 2316 intel_connector = &intel_sdvo_connector->base;
4ef69c7a
CW
2317 connector = &intel_connector->base;
2318 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2319 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
14571b4c 2320
4ef69c7a
CW
2321 intel_sdvo->controlled_output |= type;
2322 intel_sdvo_connector->output_flag = type;
14571b4c 2323
4ef69c7a
CW
2324 intel_sdvo->is_tv = true;
2325 intel_sdvo->base.needs_tv_clock = true;
14571b4c 2326
df0e9248 2327 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
14571b4c 2328
4ef69c7a 2329 if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
32aad86f 2330 goto err;
14571b4c 2331
4ef69c7a 2332 if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
32aad86f 2333 goto err;
14571b4c 2334
4ef69c7a 2335 return true;
32aad86f
CW
2336
2337err:
123d5c01 2338 intel_sdvo_destroy(connector);
32aad86f 2339 return false;
14571b4c
ZW
2340}
2341
2342static bool
ea5b213a 2343intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
14571b4c 2344{
4ef69c7a
CW
2345 struct drm_encoder *encoder = &intel_sdvo->base.base;
2346 struct drm_connector *connector;
2347 struct intel_connector *intel_connector;
2348 struct intel_sdvo_connector *intel_sdvo_connector;
14571b4c 2349
615fb93f
CW
2350 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2351 if (!intel_sdvo_connector)
2352 return false;
14571b4c 2353
615fb93f 2354 intel_connector = &intel_sdvo_connector->base;
4ef69c7a 2355 connector = &intel_connector->base;
eb1f8e4f 2356 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
4ef69c7a
CW
2357 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2358 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
2359
2360 if (device == 0) {
2361 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
2362 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2363 } else if (device == 1) {
2364 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
2365 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2366 }
2367
df0e9248
CW
2368 intel_sdvo_connector_init(intel_sdvo_connector,
2369 intel_sdvo);
4ef69c7a 2370 return true;
14571b4c
ZW
2371}
2372
2373static bool
ea5b213a 2374intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
14571b4c 2375{
4ef69c7a
CW
2376 struct drm_encoder *encoder = &intel_sdvo->base.base;
2377 struct drm_connector *connector;
2378 struct intel_connector *intel_connector;
2379 struct intel_sdvo_connector *intel_sdvo_connector;
14571b4c 2380
615fb93f
CW
2381 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2382 if (!intel_sdvo_connector)
2383 return false;
14571b4c 2384
615fb93f
CW
2385 intel_connector = &intel_sdvo_connector->base;
2386 connector = &intel_connector->base;
4ef69c7a
CW
2387 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2388 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2389
2390 if (device == 0) {
2391 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
2392 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2393 } else if (device == 1) {
2394 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
2395 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2396 }
2397
df0e9248 2398 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
4ef69c7a 2399 if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
32aad86f
CW
2400 goto err;
2401
2402 return true;
2403
2404err:
123d5c01 2405 intel_sdvo_destroy(connector);
32aad86f 2406 return false;
14571b4c
ZW
2407}
2408
2409static bool
ea5b213a 2410intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
14571b4c 2411{
ea5b213a
CW
2412 intel_sdvo->is_tv = false;
2413 intel_sdvo->base.needs_tv_clock = false;
2414 intel_sdvo->is_lvds = false;
fb7a46f3 2415
14571b4c 2416 /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
fb7a46f3 2417
14571b4c 2418 if (flags & SDVO_OUTPUT_TMDS0)
ea5b213a 2419 if (!intel_sdvo_dvi_init(intel_sdvo, 0))
14571b4c
ZW
2420 return false;
2421
2422 if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
ea5b213a 2423 if (!intel_sdvo_dvi_init(intel_sdvo, 1))
14571b4c
ZW
2424 return false;
2425
2426 /* TV has no XXX1 function block */
a1f4b7ff 2427 if (flags & SDVO_OUTPUT_SVID0)
ea5b213a 2428 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
14571b4c
ZW
2429 return false;
2430
2431 if (flags & SDVO_OUTPUT_CVBS0)
ea5b213a 2432 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
14571b4c 2433 return false;
fb7a46f3 2434
a0b1c7a5
CW
2435 if (flags & SDVO_OUTPUT_YPRPB0)
2436 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_YPRPB0))
2437 return false;
2438
14571b4c 2439 if (flags & SDVO_OUTPUT_RGB0)
ea5b213a 2440 if (!intel_sdvo_analog_init(intel_sdvo, 0))
14571b4c
ZW
2441 return false;
2442
2443 if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
ea5b213a 2444 if (!intel_sdvo_analog_init(intel_sdvo, 1))
14571b4c
ZW
2445 return false;
2446
2447 if (flags & SDVO_OUTPUT_LVDS0)
ea5b213a 2448 if (!intel_sdvo_lvds_init(intel_sdvo, 0))
14571b4c
ZW
2449 return false;
2450
2451 if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
ea5b213a 2452 if (!intel_sdvo_lvds_init(intel_sdvo, 1))
14571b4c 2453 return false;
fb7a46f3 2454
14571b4c 2455 if ((flags & SDVO_OUTPUT_MASK) == 0) {
fb7a46f3 2456 unsigned char bytes[2];
2457
ea5b213a
CW
2458 intel_sdvo->controlled_output = 0;
2459 memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
51c8b407 2460 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
ea5b213a 2461 SDVO_NAME(intel_sdvo),
51c8b407 2462 bytes[0], bytes[1]);
14571b4c 2463 return false;
fb7a46f3 2464 }
27f8227b 2465 intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
fb7a46f3 2466
14571b4c 2467 return true;
fb7a46f3 2468}
2469
d0ddfbd3
JN
2470static void intel_sdvo_output_cleanup(struct intel_sdvo *intel_sdvo)
2471{
2472 struct drm_device *dev = intel_sdvo->base.base.dev;
2473 struct drm_connector *connector, *tmp;
2474
2475 list_for_each_entry_safe(connector, tmp,
2476 &dev->mode_config.connector_list, head) {
2477 if (intel_attached_encoder(connector) == &intel_sdvo->base)
2478 intel_sdvo_destroy(connector);
2479 }
2480}
2481
32aad86f
CW
2482static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
2483 struct intel_sdvo_connector *intel_sdvo_connector,
2484 int type)
ce6feabd 2485{
4ef69c7a 2486 struct drm_device *dev = intel_sdvo->base.base.dev;
ce6feabd
ZY
2487 struct intel_sdvo_tv_format format;
2488 uint32_t format_map, i;
ce6feabd 2489
32aad86f
CW
2490 if (!intel_sdvo_set_target_output(intel_sdvo, type))
2491 return false;
ce6feabd 2492
1a3665c8 2493 BUILD_BUG_ON(sizeof(format) != 6);
32aad86f
CW
2494 if (!intel_sdvo_get_value(intel_sdvo,
2495 SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
2496 &format, sizeof(format)))
2497 return false;
ce6feabd 2498
32aad86f 2499 memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
ce6feabd
ZY
2500
2501 if (format_map == 0)
32aad86f 2502 return false;
ce6feabd 2503
615fb93f 2504 intel_sdvo_connector->format_supported_num = 0;
ce6feabd 2505 for (i = 0 ; i < TV_FORMAT_NUM; i++)
40039750
CW
2506 if (format_map & (1 << i))
2507 intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
ce6feabd
ZY
2508
2509
c5521706 2510 intel_sdvo_connector->tv_format =
32aad86f
CW
2511 drm_property_create(dev, DRM_MODE_PROP_ENUM,
2512 "mode", intel_sdvo_connector->format_supported_num);
c5521706 2513 if (!intel_sdvo_connector->tv_format)
fcc8d672 2514 return false;
ce6feabd 2515
615fb93f 2516 for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
ce6feabd 2517 drm_property_add_enum(
c5521706 2518 intel_sdvo_connector->tv_format, i,
40039750 2519 i, tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
ce6feabd 2520
40039750 2521 intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[0];
662595df 2522 drm_object_attach_property(&intel_sdvo_connector->base.base.base,
c5521706 2523 intel_sdvo_connector->tv_format, 0);
32aad86f 2524 return true;
ce6feabd
ZY
2525
2526}
2527
c5521706
CW
2528#define ENHANCEMENT(name, NAME) do { \
2529 if (enhancements.name) { \
2530 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
2531 !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
2532 return false; \
2533 intel_sdvo_connector->max_##name = data_value[0]; \
2534 intel_sdvo_connector->cur_##name = response; \
2535 intel_sdvo_connector->name = \
d9bc3c02 2536 drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
c5521706 2537 if (!intel_sdvo_connector->name) return false; \
662595df 2538 drm_object_attach_property(&connector->base, \
c5521706
CW
2539 intel_sdvo_connector->name, \
2540 intel_sdvo_connector->cur_##name); \
2541 DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
2542 data_value[0], data_value[1], response); \
2543 } \
0206e353 2544} while (0)
c5521706
CW
2545
2546static bool
2547intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
2548 struct intel_sdvo_connector *intel_sdvo_connector,
2549 struct intel_sdvo_enhancements_reply enhancements)
b9219c5e 2550{
4ef69c7a 2551 struct drm_device *dev = intel_sdvo->base.base.dev;
32aad86f 2552 struct drm_connector *connector = &intel_sdvo_connector->base.base;
b9219c5e
ZY
2553 uint16_t response, data_value[2];
2554
c5521706
CW
2555 /* when horizontal overscan is supported, Add the left/right property */
2556 if (enhancements.overscan_h) {
2557 if (!intel_sdvo_get_value(intel_sdvo,
2558 SDVO_CMD_GET_MAX_OVERSCAN_H,
2559 &data_value, 4))
2560 return false;
32aad86f 2561
c5521706
CW
2562 if (!intel_sdvo_get_value(intel_sdvo,
2563 SDVO_CMD_GET_OVERSCAN_H,
2564 &response, 2))
2565 return false;
fcc8d672 2566
c5521706
CW
2567 intel_sdvo_connector->max_hscan = data_value[0];
2568 intel_sdvo_connector->left_margin = data_value[0] - response;
2569 intel_sdvo_connector->right_margin = intel_sdvo_connector->left_margin;
2570 intel_sdvo_connector->left =
d9bc3c02 2571 drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
c5521706
CW
2572 if (!intel_sdvo_connector->left)
2573 return false;
fcc8d672 2574
662595df 2575 drm_object_attach_property(&connector->base,
c5521706
CW
2576 intel_sdvo_connector->left,
2577 intel_sdvo_connector->left_margin);
fcc8d672 2578
c5521706 2579 intel_sdvo_connector->right =
d9bc3c02 2580 drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
c5521706
CW
2581 if (!intel_sdvo_connector->right)
2582 return false;
32aad86f 2583
662595df 2584 drm_object_attach_property(&connector->base,
c5521706
CW
2585 intel_sdvo_connector->right,
2586 intel_sdvo_connector->right_margin);
2587 DRM_DEBUG_KMS("h_overscan: max %d, "
2588 "default %d, current %d\n",
2589 data_value[0], data_value[1], response);
2590 }
32aad86f 2591
c5521706
CW
2592 if (enhancements.overscan_v) {
2593 if (!intel_sdvo_get_value(intel_sdvo,
2594 SDVO_CMD_GET_MAX_OVERSCAN_V,
2595 &data_value, 4))
2596 return false;
fcc8d672 2597
c5521706
CW
2598 if (!intel_sdvo_get_value(intel_sdvo,
2599 SDVO_CMD_GET_OVERSCAN_V,
2600 &response, 2))
2601 return false;
32aad86f 2602
c5521706
CW
2603 intel_sdvo_connector->max_vscan = data_value[0];
2604 intel_sdvo_connector->top_margin = data_value[0] - response;
2605 intel_sdvo_connector->bottom_margin = intel_sdvo_connector->top_margin;
2606 intel_sdvo_connector->top =
d9bc3c02
SH
2607 drm_property_create_range(dev, 0,
2608 "top_margin", 0, data_value[0]);
c5521706
CW
2609 if (!intel_sdvo_connector->top)
2610 return false;
32aad86f 2611
662595df 2612 drm_object_attach_property(&connector->base,
c5521706
CW
2613 intel_sdvo_connector->top,
2614 intel_sdvo_connector->top_margin);
fcc8d672 2615
c5521706 2616 intel_sdvo_connector->bottom =
d9bc3c02
SH
2617 drm_property_create_range(dev, 0,
2618 "bottom_margin", 0, data_value[0]);
c5521706
CW
2619 if (!intel_sdvo_connector->bottom)
2620 return false;
32aad86f 2621
662595df 2622 drm_object_attach_property(&connector->base,
c5521706
CW
2623 intel_sdvo_connector->bottom,
2624 intel_sdvo_connector->bottom_margin);
2625 DRM_DEBUG_KMS("v_overscan: max %d, "
2626 "default %d, current %d\n",
2627 data_value[0], data_value[1], response);
2628 }
32aad86f 2629
c5521706
CW
2630 ENHANCEMENT(hpos, HPOS);
2631 ENHANCEMENT(vpos, VPOS);
2632 ENHANCEMENT(saturation, SATURATION);
2633 ENHANCEMENT(contrast, CONTRAST);
2634 ENHANCEMENT(hue, HUE);
2635 ENHANCEMENT(sharpness, SHARPNESS);
2636 ENHANCEMENT(brightness, BRIGHTNESS);
2637 ENHANCEMENT(flicker_filter, FLICKER_FILTER);
2638 ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
2639 ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
2640 ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
2641 ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
fcc8d672 2642
e044218a
CW
2643 if (enhancements.dot_crawl) {
2644 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
2645 return false;
2646
2647 intel_sdvo_connector->max_dot_crawl = 1;
2648 intel_sdvo_connector->cur_dot_crawl = response & 0x1;
2649 intel_sdvo_connector->dot_crawl =
d9bc3c02 2650 drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
e044218a
CW
2651 if (!intel_sdvo_connector->dot_crawl)
2652 return false;
2653
662595df 2654 drm_object_attach_property(&connector->base,
e044218a
CW
2655 intel_sdvo_connector->dot_crawl,
2656 intel_sdvo_connector->cur_dot_crawl);
2657 DRM_DEBUG_KMS("dot crawl: current %d\n", response);
2658 }
2659
c5521706
CW
2660 return true;
2661}
32aad86f 2662
c5521706
CW
2663static bool
2664intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
2665 struct intel_sdvo_connector *intel_sdvo_connector,
2666 struct intel_sdvo_enhancements_reply enhancements)
2667{
4ef69c7a 2668 struct drm_device *dev = intel_sdvo->base.base.dev;
c5521706
CW
2669 struct drm_connector *connector = &intel_sdvo_connector->base.base;
2670 uint16_t response, data_value[2];
32aad86f 2671
c5521706 2672 ENHANCEMENT(brightness, BRIGHTNESS);
fcc8d672 2673
c5521706
CW
2674 return true;
2675}
2676#undef ENHANCEMENT
32aad86f 2677
c5521706
CW
2678static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
2679 struct intel_sdvo_connector *intel_sdvo_connector)
2680{
2681 union {
2682 struct intel_sdvo_enhancements_reply reply;
2683 uint16_t response;
2684 } enhancements;
32aad86f 2685
1a3665c8
CW
2686 BUILD_BUG_ON(sizeof(enhancements) != 2);
2687
cf9a2f3a
CW
2688 enhancements.response = 0;
2689 intel_sdvo_get_value(intel_sdvo,
2690 SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
2691 &enhancements, sizeof(enhancements));
c5521706
CW
2692 if (enhancements.response == 0) {
2693 DRM_DEBUG_KMS("No enhancement is supported\n");
2694 return true;
b9219c5e 2695 }
32aad86f 2696
c5521706
CW
2697 if (IS_TV(intel_sdvo_connector))
2698 return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
0206e353 2699 else if (IS_LVDS(intel_sdvo_connector))
c5521706
CW
2700 return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
2701 else
2702 return true;
e957d772
CW
2703}
2704
2705static int intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
2706 struct i2c_msg *msgs,
2707 int num)
2708{
2709 struct intel_sdvo *sdvo = adapter->algo_data;
fcc8d672 2710
e957d772
CW
2711 if (!intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
2712 return -EIO;
2713
2714 return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
2715}
2716
2717static u32 intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
2718{
2719 struct intel_sdvo *sdvo = adapter->algo_data;
2720 return sdvo->i2c->algo->functionality(sdvo->i2c);
2721}
2722
2723static const struct i2c_algorithm intel_sdvo_ddc_proxy = {
2724 .master_xfer = intel_sdvo_ddc_proxy_xfer,
2725 .functionality = intel_sdvo_ddc_proxy_func
2726};
2727
2728static bool
2729intel_sdvo_init_ddc_proxy(struct intel_sdvo *sdvo,
2730 struct drm_device *dev)
2731{
2732 sdvo->ddc.owner = THIS_MODULE;
2733 sdvo->ddc.class = I2C_CLASS_DDC;
2734 snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
2735 sdvo->ddc.dev.parent = &dev->pdev->dev;
2736 sdvo->ddc.algo_data = sdvo;
2737 sdvo->ddc.algo = &intel_sdvo_ddc_proxy;
2738
2739 return i2c_add_adapter(&sdvo->ddc) == 0;
b9219c5e
ZY
2740}
2741
eef4eacb 2742bool intel_sdvo_init(struct drm_device *dev, uint32_t sdvo_reg, bool is_sdvob)
79e53945 2743{
b01f2c3a 2744 struct drm_i915_private *dev_priv = dev->dev_private;
21d40d37 2745 struct intel_encoder *intel_encoder;
ea5b213a 2746 struct intel_sdvo *intel_sdvo;
084b612e 2747 u32 hotplug_mask;
79e53945 2748 int i;
79e53945 2749
ea5b213a
CW
2750 intel_sdvo = kzalloc(sizeof(struct intel_sdvo), GFP_KERNEL);
2751 if (!intel_sdvo)
7d57382e 2752 return false;
79e53945 2753
56184e3d 2754 intel_sdvo->sdvo_reg = sdvo_reg;
eef4eacb
DV
2755 intel_sdvo->is_sdvob = is_sdvob;
2756 intel_sdvo->slave_addr = intel_sdvo_get_slave_addr(dev, intel_sdvo) >> 1;
56184e3d 2757 intel_sdvo_select_i2c_bus(dev_priv, intel_sdvo, sdvo_reg);
fbfcc4f3
JN
2758 if (!intel_sdvo_init_ddc_proxy(intel_sdvo, dev))
2759 goto err_i2c_bus;
e957d772 2760
56184e3d 2761 /* encoder type will be decided later */
ea5b213a 2762 intel_encoder = &intel_sdvo->base;
21d40d37 2763 intel_encoder->type = INTEL_OUTPUT_SDVO;
373a3cf7 2764 drm_encoder_init(dev, &intel_encoder->base, &intel_sdvo_enc_funcs, 0);
79e53945 2765
79e53945
JB
2766 /* Read the regs to test if we can talk to the device */
2767 for (i = 0; i < 0x40; i++) {
f899fc64
CW
2768 u8 byte;
2769
2770 if (!intel_sdvo_read_byte(intel_sdvo, i, &byte)) {
eef4eacb
DV
2771 DRM_DEBUG_KMS("No SDVO device found on %s\n",
2772 SDVO_NAME(intel_sdvo));
f899fc64 2773 goto err;
79e53945
JB
2774 }
2775 }
2776
084b612e
CW
2777 hotplug_mask = 0;
2778 if (IS_G4X(dev)) {
2779 hotplug_mask = intel_sdvo->is_sdvob ?
2780 SDVOB_HOTPLUG_INT_STATUS_G4X : SDVOC_HOTPLUG_INT_STATUS_G4X;
2781 } else if (IS_GEN4(dev)) {
2782 hotplug_mask = intel_sdvo->is_sdvob ?
2783 SDVOB_HOTPLUG_INT_STATUS_I965 : SDVOC_HOTPLUG_INT_STATUS_I965;
2784 } else {
2785 hotplug_mask = intel_sdvo->is_sdvob ?
2786 SDVOB_HOTPLUG_INT_STATUS_I915 : SDVOC_HOTPLUG_INT_STATUS_I915;
2787 }
619ac3b7 2788
4f770a5b
EE
2789 /* Only enable the hotplug irq if we need it, to work around noisy
2790 * hotplug lines.
2791 */
1d843f9d
EE
2792 if (intel_sdvo->hotplug_active)
2793 intel_encoder->hpd_pin = HPD_SDVO_B ? HPD_SDVO_B : HPD_SDVO_C;
2794
6cc5f341 2795 intel_encoder->compute_config = intel_sdvo_compute_config;
ce22c320 2796 intel_encoder->disable = intel_disable_sdvo;
6cc5f341 2797 intel_encoder->mode_set = intel_sdvo_mode_set;
ce22c320 2798 intel_encoder->enable = intel_enable_sdvo;
4ac41f47 2799 intel_encoder->get_hw_state = intel_sdvo_get_hw_state;
ce22c320 2800
af901ca1 2801 /* In default case sdvo lvds is false */
32aad86f 2802 if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
f899fc64 2803 goto err;
79e53945 2804
ea5b213a
CW
2805 if (intel_sdvo_output_setup(intel_sdvo,
2806 intel_sdvo->caps.output_flags) != true) {
eef4eacb
DV
2807 DRM_DEBUG_KMS("SDVO output failed to setup on %s\n",
2808 SDVO_NAME(intel_sdvo));
d0ddfbd3
JN
2809 /* Output_setup can leave behind connectors! */
2810 goto err_output;
79e53945
JB
2811 }
2812
e506d6fd
DV
2813 /*
2814 * Cloning SDVO with anything is often impossible, since the SDVO
2815 * encoder can request a special input timing mode. And even if that's
2816 * not the case we have evidence that cloning a plain unscaled mode with
2817 * VGA doesn't really work. Furthermore the cloning flags are way too
2818 * simplistic anyway to express such constraints, so just give up on
2819 * cloning for SDVO encoders.
2820 */
2821 intel_sdvo->base.cloneable = false;
2822
ea5b213a 2823 intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo, sdvo_reg);
e2f0ba97 2824
79e53945 2825 /* Set the input timing to the screen. Assume always input 0. */
32aad86f 2826 if (!intel_sdvo_set_target_input(intel_sdvo))
d0ddfbd3 2827 goto err_output;
79e53945 2828
32aad86f
CW
2829 if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
2830 &intel_sdvo->pixel_clock_min,
2831 &intel_sdvo->pixel_clock_max))
d0ddfbd3 2832 goto err_output;
79e53945 2833
8a4c47f3 2834 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
342dc382 2835 "clock range %dMHz - %dMHz, "
2836 "input 1: %c, input 2: %c, "
2837 "output 1: %c, output 2: %c\n",
ea5b213a
CW
2838 SDVO_NAME(intel_sdvo),
2839 intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
2840 intel_sdvo->caps.device_rev_id,
2841 intel_sdvo->pixel_clock_min / 1000,
2842 intel_sdvo->pixel_clock_max / 1000,
2843 (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2844 (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
342dc382 2845 /* check currently supported outputs */
ea5b213a 2846 intel_sdvo->caps.output_flags &
79e53945 2847 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
ea5b213a 2848 intel_sdvo->caps.output_flags &
79e53945 2849 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
7d57382e 2850 return true;
79e53945 2851
d0ddfbd3
JN
2852err_output:
2853 intel_sdvo_output_cleanup(intel_sdvo);
2854
f899fc64 2855err:
373a3cf7 2856 drm_encoder_cleanup(&intel_encoder->base);
e957d772 2857 i2c_del_adapter(&intel_sdvo->ddc);
fbfcc4f3
JN
2858err_i2c_bus:
2859 intel_sdvo_unselect_i2c_bus(intel_sdvo);
ea5b213a 2860 kfree(intel_sdvo);
79e53945 2861
7d57382e 2862 return false;
79e53945 2863}