drm/i915: (re)init HPD interrupt storm statistics
[deliverable/linux.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);
1234 u16 active_outputs;
1235
1236 intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
1237
1238 if (active_outputs & intel_sdvo_connector->output_flag)
1239 return true;
1240 else
1241 return false;
1242}
1243
1244static bool intel_sdvo_get_hw_state(struct intel_encoder *encoder,
1245 enum pipe *pipe)
1246{
1247 struct drm_device *dev = encoder->base.dev;
79e53945 1248 struct drm_i915_private *dev_priv = dev->dev_private;
4ac41f47 1249 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
7a7d1fb7 1250 u16 active_outputs;
4ac41f47
DV
1251 u32 tmp;
1252
1253 tmp = I915_READ(intel_sdvo->sdvo_reg);
7a7d1fb7 1254 intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
4ac41f47 1255
7a7d1fb7 1256 if (!(tmp & SDVO_ENABLE) && (active_outputs == 0))
4ac41f47
DV
1257 return false;
1258
1259 if (HAS_PCH_CPT(dev))
1260 *pipe = PORT_TO_PIPE_CPT(tmp);
1261 else
1262 *pipe = PORT_TO_PIPE(tmp);
1263
1264 return true;
1265}
1266
ce22c320
DV
1267static void intel_disable_sdvo(struct intel_encoder *encoder)
1268{
1269 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
1270 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
1271 u32 temp;
1272
1273 intel_sdvo_set_active_outputs(intel_sdvo, 0);
1274 if (0)
1275 intel_sdvo_set_encoder_power_state(intel_sdvo,
1276 DRM_MODE_DPMS_OFF);
1277
1278 temp = I915_READ(intel_sdvo->sdvo_reg);
1279 if ((temp & SDVO_ENABLE) != 0) {
776ca7cf
CW
1280 /* HW workaround for IBX, we need to move the port to
1281 * transcoder A before disabling it. */
1282 if (HAS_PCH_IBX(encoder->base.dev)) {
1283 struct drm_crtc *crtc = encoder->base.crtc;
1284 int pipe = crtc ? to_intel_crtc(crtc)->pipe : -1;
1285
1286 if (temp & SDVO_PIPE_B_SELECT) {
1287 temp &= ~SDVO_PIPE_B_SELECT;
1288 I915_WRITE(intel_sdvo->sdvo_reg, temp);
1289 POSTING_READ(intel_sdvo->sdvo_reg);
1290
1291 /* Again we need to write this twice. */
1292 I915_WRITE(intel_sdvo->sdvo_reg, temp);
1293 POSTING_READ(intel_sdvo->sdvo_reg);
1294
1295 /* Transcoder selection bits only update
1296 * effectively on vblank. */
1297 if (crtc)
1298 intel_wait_for_vblank(encoder->base.dev, pipe);
1299 else
1300 msleep(50);
1301 }
1302 }
1303
ce22c320
DV
1304 intel_sdvo_write_sdvox(intel_sdvo, temp & ~SDVO_ENABLE);
1305 }
1306}
1307
1308static void intel_enable_sdvo(struct intel_encoder *encoder)
1309{
1310 struct drm_device *dev = encoder->base.dev;
1311 struct drm_i915_private *dev_priv = dev->dev_private;
1312 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
1313 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
79e53945 1314 u32 temp;
ce22c320
DV
1315 bool input1, input2;
1316 int i;
1317 u8 status;
1318
1319 temp = I915_READ(intel_sdvo->sdvo_reg);
776ca7cf
CW
1320 if ((temp & SDVO_ENABLE) == 0) {
1321 /* HW workaround for IBX, we need to move the port
dc0fa718
PZ
1322 * to transcoder A before disabling it, so restore it here. */
1323 if (HAS_PCH_IBX(dev))
1324 temp |= SDVO_PIPE_SEL(intel_crtc->pipe);
776ca7cf 1325
ce22c320 1326 intel_sdvo_write_sdvox(intel_sdvo, temp | SDVO_ENABLE);
776ca7cf 1327 }
ce22c320
DV
1328 for (i = 0; i < 2; i++)
1329 intel_wait_for_vblank(dev, intel_crtc->pipe);
1330
1331 status = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
1332 /* Warn if the device reported failure to sync.
1333 * A lot of SDVO devices fail to notify of sync, but it's
1334 * a given it the status is a success, we succeeded.
1335 */
1336 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1337 DRM_DEBUG_KMS("First %s output reported failure to "
1338 "sync\n", SDVO_NAME(intel_sdvo));
1339 }
1340
1341 if (0)
1342 intel_sdvo_set_encoder_power_state(intel_sdvo,
1343 DRM_MODE_DPMS_ON);
1344 intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
1345}
1346
b2cabb0e 1347static void intel_sdvo_dpms(struct drm_connector *connector, int mode)
79e53945 1348{
b2cabb0e
DV
1349 struct drm_crtc *crtc;
1350 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
1351
1352 /* dvo supports only 2 dpms states. */
1353 if (mode != DRM_MODE_DPMS_ON)
1354 mode = DRM_MODE_DPMS_OFF;
1355
1356 if (mode == connector->dpms)
1357 return;
1358
1359 connector->dpms = mode;
1360
1361 /* Only need to change hw state when actually enabled */
1362 crtc = intel_sdvo->base.base.crtc;
1363 if (!crtc) {
1364 intel_sdvo->base.connectors_active = false;
1365 return;
1366 }
79e53945
JB
1367
1368 if (mode != DRM_MODE_DPMS_ON) {
ea5b213a 1369 intel_sdvo_set_active_outputs(intel_sdvo, 0);
79e53945 1370 if (0)
ea5b213a 1371 intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
79e53945 1372
b2cabb0e
DV
1373 intel_sdvo->base.connectors_active = false;
1374
1375 intel_crtc_update_dpms(crtc);
79e53945 1376 } else {
b2cabb0e
DV
1377 intel_sdvo->base.connectors_active = true;
1378
1379 intel_crtc_update_dpms(crtc);
79e53945
JB
1380
1381 if (0)
ea5b213a
CW
1382 intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
1383 intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
79e53945 1384 }
0a91ca29 1385
b980514c 1386 intel_modeset_check_state(connector->dev);
79e53945
JB
1387}
1388
79e53945
JB
1389static int intel_sdvo_mode_valid(struct drm_connector *connector,
1390 struct drm_display_mode *mode)
1391{
df0e9248 1392 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
79e53945
JB
1393
1394 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1395 return MODE_NO_DBLESCAN;
1396
ea5b213a 1397 if (intel_sdvo->pixel_clock_min > mode->clock)
79e53945
JB
1398 return MODE_CLOCK_LOW;
1399
ea5b213a 1400 if (intel_sdvo->pixel_clock_max < mode->clock)
79e53945
JB
1401 return MODE_CLOCK_HIGH;
1402
8545423a 1403 if (intel_sdvo->is_lvds) {
ea5b213a 1404 if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
12682a97 1405 return MODE_PANEL;
1406
ea5b213a 1407 if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
12682a97 1408 return MODE_PANEL;
1409 }
1410
79e53945
JB
1411 return MODE_OK;
1412}
1413
ea5b213a 1414static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
79e53945 1415{
1a3665c8 1416 BUILD_BUG_ON(sizeof(*caps) != 8);
e957d772
CW
1417 if (!intel_sdvo_get_value(intel_sdvo,
1418 SDVO_CMD_GET_DEVICE_CAPS,
1419 caps, sizeof(*caps)))
1420 return false;
1421
1422 DRM_DEBUG_KMS("SDVO capabilities:\n"
1423 " vendor_id: %d\n"
1424 " device_id: %d\n"
1425 " device_rev_id: %d\n"
1426 " sdvo_version_major: %d\n"
1427 " sdvo_version_minor: %d\n"
1428 " sdvo_inputs_mask: %d\n"
1429 " smooth_scaling: %d\n"
1430 " sharp_scaling: %d\n"
1431 " up_scaling: %d\n"
1432 " down_scaling: %d\n"
1433 " stall_support: %d\n"
1434 " output_flags: %d\n",
1435 caps->vendor_id,
1436 caps->device_id,
1437 caps->device_rev_id,
1438 caps->sdvo_version_major,
1439 caps->sdvo_version_minor,
1440 caps->sdvo_inputs_mask,
1441 caps->smooth_scaling,
1442 caps->sharp_scaling,
1443 caps->up_scaling,
1444 caps->down_scaling,
1445 caps->stall_support,
1446 caps->output_flags);
1447
1448 return true;
79e53945
JB
1449}
1450
5fa7ac9c 1451static uint16_t intel_sdvo_get_hotplug_support(struct intel_sdvo *intel_sdvo)
79e53945 1452{
768b107e 1453 struct drm_device *dev = intel_sdvo->base.base.dev;
5fa7ac9c 1454 uint16_t hotplug;
79e53945 1455
768b107e
DV
1456 /* HW Erratum: SDVO Hotplug is broken on all i945G chips, there's noise
1457 * on the line. */
1458 if (IS_I945G(dev) || IS_I945GM(dev))
5fa7ac9c 1459 return 0;
768b107e 1460
5fa7ac9c
JN
1461 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1462 &hotplug, sizeof(hotplug)))
1463 return 0;
768b107e 1464
5fa7ac9c 1465 return hotplug;
79e53945
JB
1466}
1467
cc68c81a 1468static void intel_sdvo_enable_hotplug(struct intel_encoder *encoder)
79e53945 1469{
cc68c81a 1470 struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
79e53945 1471
5fa7ac9c
JN
1472 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG,
1473 &intel_sdvo->hotplug_active, 2);
79e53945
JB
1474}
1475
fb7a46f3 1476static bool
ea5b213a 1477intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
fb7a46f3 1478{
bc65212c 1479 /* Is there more than one type of output? */
2294488d 1480 return hweight16(intel_sdvo->caps.output_flags) > 1;
fb7a46f3 1481}
1482
f899fc64 1483static struct edid *
e957d772 1484intel_sdvo_get_edid(struct drm_connector *connector)
f899fc64 1485{
e957d772
CW
1486 struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
1487 return drm_get_edid(connector, &sdvo->ddc);
f899fc64
CW
1488}
1489
ff482d83
CW
1490/* Mac mini hack -- use the same DDC as the analog connector */
1491static struct edid *
1492intel_sdvo_get_analog_edid(struct drm_connector *connector)
1493{
f899fc64 1494 struct drm_i915_private *dev_priv = connector->dev->dev_private;
ff482d83 1495
0c1dab89 1496 return drm_get_edid(connector,
3bd7d909
DK
1497 intel_gmbus_get_adapter(dev_priv,
1498 dev_priv->crt_ddc_pin));
ff482d83
CW
1499}
1500
c43b5634 1501static enum drm_connector_status
8bf38485 1502intel_sdvo_tmds_sink_detect(struct drm_connector *connector)
9dff6af8 1503{
df0e9248 1504 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
9d1a903d
CW
1505 enum drm_connector_status status;
1506 struct edid *edid;
9dff6af8 1507
e957d772 1508 edid = intel_sdvo_get_edid(connector);
57cdaf90 1509
ea5b213a 1510 if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
e957d772 1511 u8 ddc, saved_ddc = intel_sdvo->ddc_bus;
9d1a903d 1512
7c3f0a27
ZY
1513 /*
1514 * Don't use the 1 as the argument of DDC bus switch to get
1515 * the EDID. It is used for SDVO SPD ROM.
1516 */
9d1a903d 1517 for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
e957d772
CW
1518 intel_sdvo->ddc_bus = ddc;
1519 edid = intel_sdvo_get_edid(connector);
1520 if (edid)
7c3f0a27 1521 break;
7c3f0a27 1522 }
e957d772
CW
1523 /*
1524 * If we found the EDID on the other bus,
1525 * assume that is the correct DDC bus.
1526 */
1527 if (edid == NULL)
1528 intel_sdvo->ddc_bus = saved_ddc;
7c3f0a27 1529 }
9d1a903d
CW
1530
1531 /*
1532 * When there is no edid and no monitor is connected with VGA
1533 * port, try to use the CRT ddc to read the EDID for DVI-connector.
57cdaf90 1534 */
ff482d83
CW
1535 if (edid == NULL)
1536 edid = intel_sdvo_get_analog_edid(connector);
149c36a3 1537
2f551c84 1538 status = connector_status_unknown;
9dff6af8 1539 if (edid != NULL) {
149c36a3 1540 /* DDC bus is shared, match EDID to connector type */
9d1a903d
CW
1541 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
1542 status = connector_status_connected;
da79de97
CW
1543 if (intel_sdvo->is_hdmi) {
1544 intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
1545 intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
abedc077
VS
1546 intel_sdvo->rgb_quant_range_selectable =
1547 drm_rgb_quant_range_selectable(edid);
da79de97 1548 }
13946743
CW
1549 } else
1550 status = connector_status_disconnected;
9d1a903d
CW
1551 kfree(edid);
1552 }
7f36e7ed
CW
1553
1554 if (status == connector_status_connected) {
1555 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
c3e5f67b
DV
1556 if (intel_sdvo_connector->force_audio != HDMI_AUDIO_AUTO)
1557 intel_sdvo->has_hdmi_audio = (intel_sdvo_connector->force_audio == HDMI_AUDIO_ON);
7f36e7ed
CW
1558 }
1559
2b8d33f7 1560 return status;
9dff6af8
ML
1561}
1562
52220085
CW
1563static bool
1564intel_sdvo_connector_matches_edid(struct intel_sdvo_connector *sdvo,
1565 struct edid *edid)
1566{
1567 bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
1568 bool connector_is_digital = !!IS_DIGITAL(sdvo);
1569
1570 DRM_DEBUG_KMS("connector_is_digital? %d, monitor_is_digital? %d\n",
1571 connector_is_digital, monitor_is_digital);
1572 return connector_is_digital == monitor_is_digital;
1573}
1574
7b334fcb 1575static enum drm_connector_status
930a9e28 1576intel_sdvo_detect(struct drm_connector *connector, bool force)
79e53945 1577{
fb7a46f3 1578 uint16_t response;
df0e9248 1579 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
615fb93f 1580 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
14571b4c 1581 enum drm_connector_status ret;
79e53945 1582
fc37381c
CW
1583 if (!intel_sdvo_get_value(intel_sdvo,
1584 SDVO_CMD_GET_ATTACHED_DISPLAYS,
1585 &response, 2))
32aad86f 1586 return connector_status_unknown;
79e53945 1587
e957d772
CW
1588 DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
1589 response & 0xff, response >> 8,
1590 intel_sdvo_connector->output_flag);
e2f0ba97 1591
fb7a46f3 1592 if (response == 0)
79e53945 1593 return connector_status_disconnected;
fb7a46f3 1594
ea5b213a 1595 intel_sdvo->attached_output = response;
14571b4c 1596
97aaf910
CW
1597 intel_sdvo->has_hdmi_monitor = false;
1598 intel_sdvo->has_hdmi_audio = false;
abedc077 1599 intel_sdvo->rgb_quant_range_selectable = false;
97aaf910 1600
615fb93f 1601 if ((intel_sdvo_connector->output_flag & response) == 0)
14571b4c 1602 ret = connector_status_disconnected;
13946743 1603 else if (IS_TMDS(intel_sdvo_connector))
8bf38485 1604 ret = intel_sdvo_tmds_sink_detect(connector);
13946743
CW
1605 else {
1606 struct edid *edid;
1607
1608 /* if we have an edid check it matches the connection */
1609 edid = intel_sdvo_get_edid(connector);
1610 if (edid == NULL)
1611 edid = intel_sdvo_get_analog_edid(connector);
1612 if (edid != NULL) {
52220085
CW
1613 if (intel_sdvo_connector_matches_edid(intel_sdvo_connector,
1614 edid))
13946743 1615 ret = connector_status_connected;
52220085
CW
1616 else
1617 ret = connector_status_disconnected;
1618
13946743
CW
1619 kfree(edid);
1620 } else
1621 ret = connector_status_connected;
1622 }
14571b4c
ZW
1623
1624 /* May update encoder flag for like clock for SDVO TV, etc.*/
1625 if (ret == connector_status_connected) {
ea5b213a
CW
1626 intel_sdvo->is_tv = false;
1627 intel_sdvo->is_lvds = false;
1628 intel_sdvo->base.needs_tv_clock = false;
14571b4c
ZW
1629
1630 if (response & SDVO_TV_MASK) {
ea5b213a
CW
1631 intel_sdvo->is_tv = true;
1632 intel_sdvo->base.needs_tv_clock = true;
14571b4c
ZW
1633 }
1634 if (response & SDVO_LVDS_MASK)
8545423a 1635 intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
fb7a46f3 1636 }
14571b4c
ZW
1637
1638 return ret;
79e53945
JB
1639}
1640
e2f0ba97 1641static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
79e53945 1642{
ff482d83 1643 struct edid *edid;
79e53945
JB
1644
1645 /* set the bus switch and get the modes */
e957d772 1646 edid = intel_sdvo_get_edid(connector);
79e53945 1647
57cdaf90
KP
1648 /*
1649 * Mac mini hack. On this device, the DVI-I connector shares one DDC
1650 * link between analog and digital outputs. So, if the regular SDVO
1651 * DDC fails, check to see if the analog output is disconnected, in
1652 * which case we'll look there for the digital DDC data.
e2f0ba97 1653 */
f899fc64
CW
1654 if (edid == NULL)
1655 edid = intel_sdvo_get_analog_edid(connector);
1656
ff482d83 1657 if (edid != NULL) {
52220085
CW
1658 if (intel_sdvo_connector_matches_edid(to_intel_sdvo_connector(connector),
1659 edid)) {
0c1dab89
CW
1660 drm_mode_connector_update_edid_property(connector, edid);
1661 drm_add_edid_modes(connector, edid);
1662 }
13946743 1663
ff482d83 1664 kfree(edid);
e2f0ba97 1665 }
e2f0ba97
JB
1666}
1667
1668/*
1669 * Set of SDVO TV modes.
1670 * Note! This is in reply order (see loop in get_tv_modes).
1671 * XXX: all 60Hz refresh?
1672 */
b1f559ec 1673static const struct drm_display_mode sdvo_tv_modes[] = {
7026d4ac
ZW
1674 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1675 416, 0, 200, 201, 232, 233, 0,
e2f0ba97 1676 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1677 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1678 416, 0, 240, 241, 272, 273, 0,
e2f0ba97 1679 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1680 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1681 496, 0, 300, 301, 332, 333, 0,
e2f0ba97 1682 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1683 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1684 736, 0, 350, 351, 382, 383, 0,
e2f0ba97 1685 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1686 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1687 736, 0, 400, 401, 432, 433, 0,
e2f0ba97 1688 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1689 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1690 736, 0, 480, 481, 512, 513, 0,
e2f0ba97 1691 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1692 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1693 800, 0, 480, 481, 512, 513, 0,
e2f0ba97 1694 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1695 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1696 800, 0, 576, 577, 608, 609, 0,
e2f0ba97 1697 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1698 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1699 816, 0, 350, 351, 382, 383, 0,
e2f0ba97 1700 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1701 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1702 816, 0, 400, 401, 432, 433, 0,
e2f0ba97 1703 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1704 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1705 816, 0, 480, 481, 512, 513, 0,
e2f0ba97 1706 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1707 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1708 816, 0, 540, 541, 572, 573, 0,
e2f0ba97 1709 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1710 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1711 816, 0, 576, 577, 608, 609, 0,
e2f0ba97 1712 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1713 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1714 864, 0, 576, 577, 608, 609, 0,
e2f0ba97 1715 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1716 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1717 896, 0, 600, 601, 632, 633, 0,
e2f0ba97 1718 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1719 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1720 928, 0, 624, 625, 656, 657, 0,
e2f0ba97 1721 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1722 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1723 1016, 0, 766, 767, 798, 799, 0,
e2f0ba97 1724 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1725 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1726 1120, 0, 768, 769, 800, 801, 0,
e2f0ba97 1727 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
7026d4ac
ZW
1728 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1729 1376, 0, 1024, 1025, 1056, 1057, 0,
e2f0ba97
JB
1730 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1731};
1732
1733static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1734{
df0e9248 1735 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
7026d4ac 1736 struct intel_sdvo_sdtv_resolution_request tv_res;
ce6feabd
ZY
1737 uint32_t reply = 0, format_map = 0;
1738 int i;
e2f0ba97
JB
1739
1740 /* Read the list of supported input resolutions for the selected TV
1741 * format.
1742 */
40039750 1743 format_map = 1 << intel_sdvo->tv_format_index;
ce6feabd 1744 memcpy(&tv_res, &format_map,
32aad86f 1745 min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
ce6feabd 1746
32aad86f
CW
1747 if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
1748 return;
ce6feabd 1749
32aad86f 1750 BUILD_BUG_ON(sizeof(tv_res) != 3);
e957d772
CW
1751 if (!intel_sdvo_write_cmd(intel_sdvo,
1752 SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
32aad86f
CW
1753 &tv_res, sizeof(tv_res)))
1754 return;
1755 if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
e2f0ba97
JB
1756 return;
1757
1758 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
7026d4ac
ZW
1759 if (reply & (1 << i)) {
1760 struct drm_display_mode *nmode;
1761 nmode = drm_mode_duplicate(connector->dev,
32aad86f 1762 &sdvo_tv_modes[i]);
7026d4ac
ZW
1763 if (nmode)
1764 drm_mode_probed_add(connector, nmode);
1765 }
e2f0ba97
JB
1766}
1767
7086c87f
ML
1768static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1769{
df0e9248 1770 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
7086c87f 1771 struct drm_i915_private *dev_priv = connector->dev->dev_private;
12682a97 1772 struct drm_display_mode *newmode;
7086c87f
ML
1773
1774 /*
1775 * Attempt to get the mode list from DDC.
1776 * Assume that the preferred modes are
1777 * arranged in priority order.
1778 */
f899fc64 1779 intel_ddc_get_modes(connector, intel_sdvo->i2c);
7086c87f 1780 if (list_empty(&connector->probed_modes) == false)
12682a97 1781 goto end;
7086c87f
ML
1782
1783 /* Fetch modes from VBT */
1784 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
7086c87f
ML
1785 newmode = drm_mode_duplicate(connector->dev,
1786 dev_priv->sdvo_lvds_vbt_mode);
1787 if (newmode != NULL) {
1788 /* Guarantee the mode is preferred */
1789 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1790 DRM_MODE_TYPE_DRIVER);
1791 drm_mode_probed_add(connector, newmode);
1792 }
1793 }
12682a97 1794
1795end:
1796 list_for_each_entry(newmode, &connector->probed_modes, head) {
1797 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
ea5b213a 1798 intel_sdvo->sdvo_lvds_fixed_mode =
12682a97 1799 drm_mode_duplicate(connector->dev, newmode);
6c9547ff 1800
8545423a 1801 intel_sdvo->is_lvds = true;
12682a97 1802 break;
1803 }
1804 }
1805
7086c87f
ML
1806}
1807
e2f0ba97
JB
1808static int intel_sdvo_get_modes(struct drm_connector *connector)
1809{
615fb93f 1810 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
e2f0ba97 1811
615fb93f 1812 if (IS_TV(intel_sdvo_connector))
e2f0ba97 1813 intel_sdvo_get_tv_modes(connector);
615fb93f 1814 else if (IS_LVDS(intel_sdvo_connector))
7086c87f 1815 intel_sdvo_get_lvds_modes(connector);
e2f0ba97
JB
1816 else
1817 intel_sdvo_get_ddc_modes(connector);
1818
32aad86f 1819 return !list_empty(&connector->probed_modes);
79e53945
JB
1820}
1821
fcc8d672
CW
1822static void
1823intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
b9219c5e 1824{
615fb93f 1825 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
b9219c5e
ZY
1826 struct drm_device *dev = connector->dev;
1827
c5521706
CW
1828 if (intel_sdvo_connector->left)
1829 drm_property_destroy(dev, intel_sdvo_connector->left);
1830 if (intel_sdvo_connector->right)
1831 drm_property_destroy(dev, intel_sdvo_connector->right);
1832 if (intel_sdvo_connector->top)
1833 drm_property_destroy(dev, intel_sdvo_connector->top);
1834 if (intel_sdvo_connector->bottom)
1835 drm_property_destroy(dev, intel_sdvo_connector->bottom);
1836 if (intel_sdvo_connector->hpos)
1837 drm_property_destroy(dev, intel_sdvo_connector->hpos);
1838 if (intel_sdvo_connector->vpos)
1839 drm_property_destroy(dev, intel_sdvo_connector->vpos);
1840 if (intel_sdvo_connector->saturation)
1841 drm_property_destroy(dev, intel_sdvo_connector->saturation);
1842 if (intel_sdvo_connector->contrast)
1843 drm_property_destroy(dev, intel_sdvo_connector->contrast);
1844 if (intel_sdvo_connector->hue)
1845 drm_property_destroy(dev, intel_sdvo_connector->hue);
1846 if (intel_sdvo_connector->sharpness)
1847 drm_property_destroy(dev, intel_sdvo_connector->sharpness);
1848 if (intel_sdvo_connector->flicker_filter)
1849 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter);
1850 if (intel_sdvo_connector->flicker_filter_2d)
1851 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_2d);
1852 if (intel_sdvo_connector->flicker_filter_adaptive)
1853 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_adaptive);
1854 if (intel_sdvo_connector->tv_luma_filter)
1855 drm_property_destroy(dev, intel_sdvo_connector->tv_luma_filter);
1856 if (intel_sdvo_connector->tv_chroma_filter)
1857 drm_property_destroy(dev, intel_sdvo_connector->tv_chroma_filter);
e044218a
CW
1858 if (intel_sdvo_connector->dot_crawl)
1859 drm_property_destroy(dev, intel_sdvo_connector->dot_crawl);
c5521706
CW
1860 if (intel_sdvo_connector->brightness)
1861 drm_property_destroy(dev, intel_sdvo_connector->brightness);
b9219c5e
ZY
1862}
1863
79e53945
JB
1864static void intel_sdvo_destroy(struct drm_connector *connector)
1865{
615fb93f 1866 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
79e53945 1867
c5521706 1868 if (intel_sdvo_connector->tv_format)
ce6feabd 1869 drm_property_destroy(connector->dev,
c5521706 1870 intel_sdvo_connector->tv_format);
b9219c5e 1871
d2a82a6f 1872 intel_sdvo_destroy_enhance_property(connector);
79e53945
JB
1873 drm_sysfs_connector_remove(connector);
1874 drm_connector_cleanup(connector);
4b745b1e 1875 kfree(intel_sdvo_connector);
79e53945
JB
1876}
1877
1aad7ac0
CW
1878static bool intel_sdvo_detect_hdmi_audio(struct drm_connector *connector)
1879{
1880 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
1881 struct edid *edid;
1882 bool has_audio = false;
1883
1884 if (!intel_sdvo->is_hdmi)
1885 return false;
1886
1887 edid = intel_sdvo_get_edid(connector);
1888 if (edid != NULL && edid->input & DRM_EDID_INPUT_DIGITAL)
1889 has_audio = drm_detect_monitor_audio(edid);
38ab8a20 1890 kfree(edid);
1aad7ac0
CW
1891
1892 return has_audio;
1893}
1894
ce6feabd
ZY
1895static int
1896intel_sdvo_set_property(struct drm_connector *connector,
1897 struct drm_property *property,
1898 uint64_t val)
1899{
df0e9248 1900 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
615fb93f 1901 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
e953fd7b 1902 struct drm_i915_private *dev_priv = connector->dev->dev_private;
b9219c5e 1903 uint16_t temp_value;
32aad86f
CW
1904 uint8_t cmd;
1905 int ret;
ce6feabd 1906
662595df 1907 ret = drm_object_property_set_value(&connector->base, property, val);
32aad86f
CW
1908 if (ret)
1909 return ret;
ce6feabd 1910
3f43c48d 1911 if (property == dev_priv->force_audio_property) {
1aad7ac0
CW
1912 int i = val;
1913 bool has_audio;
1914
1915 if (i == intel_sdvo_connector->force_audio)
7f36e7ed
CW
1916 return 0;
1917
1aad7ac0 1918 intel_sdvo_connector->force_audio = i;
7f36e7ed 1919
c3e5f67b 1920 if (i == HDMI_AUDIO_AUTO)
1aad7ac0
CW
1921 has_audio = intel_sdvo_detect_hdmi_audio(connector);
1922 else
c3e5f67b 1923 has_audio = (i == HDMI_AUDIO_ON);
7f36e7ed 1924
1aad7ac0 1925 if (has_audio == intel_sdvo->has_hdmi_audio)
7f36e7ed 1926 return 0;
7f36e7ed 1927
1aad7ac0 1928 intel_sdvo->has_hdmi_audio = has_audio;
7f36e7ed
CW
1929 goto done;
1930 }
1931
e953fd7b 1932 if (property == dev_priv->broadcast_rgb_property) {
55bc60db
VS
1933 switch (val) {
1934 case INTEL_BROADCAST_RGB_AUTO:
1935 intel_sdvo->color_range_auto = true;
1936 break;
1937 case INTEL_BROADCAST_RGB_FULL:
1938 intel_sdvo->color_range_auto = false;
1939 intel_sdvo->color_range = 0;
1940 break;
1941 case INTEL_BROADCAST_RGB_LIMITED:
1942 intel_sdvo->color_range_auto = false;
4f3a8bc7
PZ
1943 /* FIXME: this bit is only valid when using TMDS
1944 * encoding and 8 bit per color mode. */
1945 intel_sdvo->color_range = HDMI_COLOR_RANGE_16_235;
55bc60db
VS
1946 break;
1947 default:
1948 return -EINVAL;
1949 }
7f36e7ed
CW
1950 goto done;
1951 }
1952
c5521706
CW
1953#define CHECK_PROPERTY(name, NAME) \
1954 if (intel_sdvo_connector->name == property) { \
1955 if (intel_sdvo_connector->cur_##name == temp_value) return 0; \
1956 if (intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
1957 cmd = SDVO_CMD_SET_##NAME; \
1958 intel_sdvo_connector->cur_##name = temp_value; \
1959 goto set_value; \
1960 }
1961
1962 if (property == intel_sdvo_connector->tv_format) {
32aad86f
CW
1963 if (val >= TV_FORMAT_NUM)
1964 return -EINVAL;
1965
40039750 1966 if (intel_sdvo->tv_format_index ==
615fb93f 1967 intel_sdvo_connector->tv_format_supported[val])
32aad86f 1968 return 0;
ce6feabd 1969
40039750 1970 intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[val];
c5521706 1971 goto done;
32aad86f 1972 } else if (IS_TV_OR_LVDS(intel_sdvo_connector)) {
b9219c5e 1973 temp_value = val;
c5521706 1974 if (intel_sdvo_connector->left == property) {
662595df 1975 drm_object_property_set_value(&connector->base,
c5521706 1976 intel_sdvo_connector->right, val);
615fb93f 1977 if (intel_sdvo_connector->left_margin == temp_value)
32aad86f 1978 return 0;
b9219c5e 1979
615fb93f
CW
1980 intel_sdvo_connector->left_margin = temp_value;
1981 intel_sdvo_connector->right_margin = temp_value;
1982 temp_value = intel_sdvo_connector->max_hscan -
c5521706 1983 intel_sdvo_connector->left_margin;
b9219c5e 1984 cmd = SDVO_CMD_SET_OVERSCAN_H;
c5521706
CW
1985 goto set_value;
1986 } else if (intel_sdvo_connector->right == property) {
662595df 1987 drm_object_property_set_value(&connector->base,
c5521706 1988 intel_sdvo_connector->left, val);
615fb93f 1989 if (intel_sdvo_connector->right_margin == temp_value)
32aad86f 1990 return 0;
b9219c5e 1991
615fb93f
CW
1992 intel_sdvo_connector->left_margin = temp_value;
1993 intel_sdvo_connector->right_margin = temp_value;
1994 temp_value = intel_sdvo_connector->max_hscan -
1995 intel_sdvo_connector->left_margin;
b9219c5e 1996 cmd = SDVO_CMD_SET_OVERSCAN_H;
c5521706
CW
1997 goto set_value;
1998 } else if (intel_sdvo_connector->top == property) {
662595df 1999 drm_object_property_set_value(&connector->base,
c5521706 2000 intel_sdvo_connector->bottom, val);
615fb93f 2001 if (intel_sdvo_connector->top_margin == temp_value)
32aad86f 2002 return 0;
b9219c5e 2003
615fb93f
CW
2004 intel_sdvo_connector->top_margin = temp_value;
2005 intel_sdvo_connector->bottom_margin = temp_value;
2006 temp_value = intel_sdvo_connector->max_vscan -
c5521706 2007 intel_sdvo_connector->top_margin;
b9219c5e 2008 cmd = SDVO_CMD_SET_OVERSCAN_V;
c5521706
CW
2009 goto set_value;
2010 } else if (intel_sdvo_connector->bottom == property) {
662595df 2011 drm_object_property_set_value(&connector->base,
c5521706 2012 intel_sdvo_connector->top, val);
615fb93f 2013 if (intel_sdvo_connector->bottom_margin == temp_value)
32aad86f
CW
2014 return 0;
2015
615fb93f
CW
2016 intel_sdvo_connector->top_margin = temp_value;
2017 intel_sdvo_connector->bottom_margin = temp_value;
2018 temp_value = intel_sdvo_connector->max_vscan -
c5521706 2019 intel_sdvo_connector->top_margin;
b9219c5e 2020 cmd = SDVO_CMD_SET_OVERSCAN_V;
c5521706
CW
2021 goto set_value;
2022 }
2023 CHECK_PROPERTY(hpos, HPOS)
2024 CHECK_PROPERTY(vpos, VPOS)
2025 CHECK_PROPERTY(saturation, SATURATION)
2026 CHECK_PROPERTY(contrast, CONTRAST)
2027 CHECK_PROPERTY(hue, HUE)
2028 CHECK_PROPERTY(brightness, BRIGHTNESS)
2029 CHECK_PROPERTY(sharpness, SHARPNESS)
2030 CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
2031 CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
2032 CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
2033 CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
2034 CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
e044218a 2035 CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
c5521706 2036 }
b9219c5e 2037
c5521706 2038 return -EINVAL; /* unknown property */
b9219c5e 2039
c5521706
CW
2040set_value:
2041 if (!intel_sdvo_set_value(intel_sdvo, cmd, &temp_value, 2))
2042 return -EIO;
b9219c5e 2043
b9219c5e 2044
c5521706 2045done:
c0c36b94
CW
2046 if (intel_sdvo->base.base.crtc)
2047 intel_crtc_restore_mode(intel_sdvo->base.base.crtc);
c5521706 2048
32aad86f 2049 return 0;
c5521706 2050#undef CHECK_PROPERTY
ce6feabd
ZY
2051}
2052
79e53945 2053static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
b2cabb0e 2054 .dpms = intel_sdvo_dpms,
79e53945
JB
2055 .detect = intel_sdvo_detect,
2056 .fill_modes = drm_helper_probe_single_connector_modes,
ce6feabd 2057 .set_property = intel_sdvo_set_property,
79e53945
JB
2058 .destroy = intel_sdvo_destroy,
2059};
2060
2061static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
2062 .get_modes = intel_sdvo_get_modes,
2063 .mode_valid = intel_sdvo_mode_valid,
df0e9248 2064 .best_encoder = intel_best_encoder,
79e53945
JB
2065};
2066
b358d0a6 2067static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
79e53945 2068{
890f3359 2069 struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
d2a82a6f 2070
ea5b213a 2071 if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
d2a82a6f 2072 drm_mode_destroy(encoder->dev,
ea5b213a 2073 intel_sdvo->sdvo_lvds_fixed_mode);
d2a82a6f 2074
e957d772 2075 i2c_del_adapter(&intel_sdvo->ddc);
ea5b213a 2076 intel_encoder_destroy(encoder);
79e53945
JB
2077}
2078
2079static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
2080 .destroy = intel_sdvo_enc_destroy,
2081};
2082
b66d8424
CW
2083static void
2084intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
2085{
2086 uint16_t mask = 0;
2087 unsigned int num_bits;
2088
2089 /* Make a mask of outputs less than or equal to our own priority in the
2090 * list.
2091 */
2092 switch (sdvo->controlled_output) {
2093 case SDVO_OUTPUT_LVDS1:
2094 mask |= SDVO_OUTPUT_LVDS1;
2095 case SDVO_OUTPUT_LVDS0:
2096 mask |= SDVO_OUTPUT_LVDS0;
2097 case SDVO_OUTPUT_TMDS1:
2098 mask |= SDVO_OUTPUT_TMDS1;
2099 case SDVO_OUTPUT_TMDS0:
2100 mask |= SDVO_OUTPUT_TMDS0;
2101 case SDVO_OUTPUT_RGB1:
2102 mask |= SDVO_OUTPUT_RGB1;
2103 case SDVO_OUTPUT_RGB0:
2104 mask |= SDVO_OUTPUT_RGB0;
2105 break;
2106 }
2107
2108 /* Count bits to find what number we are in the priority list. */
2109 mask &= sdvo->caps.output_flags;
2110 num_bits = hweight16(mask);
2111 /* If more than 3 outputs, default to DDC bus 3 for now. */
2112 if (num_bits > 3)
2113 num_bits = 3;
2114
2115 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
2116 sdvo->ddc_bus = 1 << num_bits;
2117}
79e53945 2118
e2f0ba97
JB
2119/**
2120 * Choose the appropriate DDC bus for control bus switch command for this
2121 * SDVO output based on the controlled output.
2122 *
2123 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
2124 * outputs, then LVDS outputs.
2125 */
2126static void
b1083333 2127intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
ea5b213a 2128 struct intel_sdvo *sdvo, u32 reg)
e2f0ba97 2129{
b1083333 2130 struct sdvo_device_mapping *mapping;
e2f0ba97 2131
eef4eacb 2132 if (sdvo->is_sdvob)
b1083333
AJ
2133 mapping = &(dev_priv->sdvo_mappings[0]);
2134 else
2135 mapping = &(dev_priv->sdvo_mappings[1]);
e2f0ba97 2136
b66d8424
CW
2137 if (mapping->initialized)
2138 sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
2139 else
2140 intel_sdvo_guess_ddc_bus(sdvo);
e2f0ba97
JB
2141}
2142
e957d772
CW
2143static void
2144intel_sdvo_select_i2c_bus(struct drm_i915_private *dev_priv,
2145 struct intel_sdvo *sdvo, u32 reg)
2146{
2147 struct sdvo_device_mapping *mapping;
46eb3036 2148 u8 pin;
e957d772 2149
eef4eacb 2150 if (sdvo->is_sdvob)
e957d772
CW
2151 mapping = &dev_priv->sdvo_mappings[0];
2152 else
2153 mapping = &dev_priv->sdvo_mappings[1];
2154
6cb1612a 2155 if (mapping->initialized && intel_gmbus_is_port_valid(mapping->i2c_pin))
e957d772 2156 pin = mapping->i2c_pin;
6cb1612a
JN
2157 else
2158 pin = GMBUS_PORT_DPB;
e957d772 2159
6cb1612a
JN
2160 sdvo->i2c = intel_gmbus_get_adapter(dev_priv, pin);
2161
2162 /* With gmbus we should be able to drive sdvo i2c at 2MHz, but somehow
2163 * our code totally fails once we start using gmbus. Hence fall back to
2164 * bit banging for now. */
2165 intel_gmbus_force_bit(sdvo->i2c, true);
e957d772
CW
2166}
2167
fbfcc4f3
JN
2168/* undo any changes intel_sdvo_select_i2c_bus() did to sdvo->i2c */
2169static void
2170intel_sdvo_unselect_i2c_bus(struct intel_sdvo *sdvo)
2171{
2172 intel_gmbus_force_bit(sdvo->i2c, false);
e957d772
CW
2173}
2174
e2f0ba97 2175static bool
e27d8538 2176intel_sdvo_is_hdmi_connector(struct intel_sdvo *intel_sdvo, int device)
e2f0ba97 2177{
97aaf910 2178 return intel_sdvo_check_supp_encode(intel_sdvo);
e2f0ba97
JB
2179}
2180
714605e4 2181static u8
eef4eacb 2182intel_sdvo_get_slave_addr(struct drm_device *dev, struct intel_sdvo *sdvo)
714605e4 2183{
2184 struct drm_i915_private *dev_priv = dev->dev_private;
2185 struct sdvo_device_mapping *my_mapping, *other_mapping;
2186
eef4eacb 2187 if (sdvo->is_sdvob) {
714605e4 2188 my_mapping = &dev_priv->sdvo_mappings[0];
2189 other_mapping = &dev_priv->sdvo_mappings[1];
2190 } else {
2191 my_mapping = &dev_priv->sdvo_mappings[1];
2192 other_mapping = &dev_priv->sdvo_mappings[0];
2193 }
2194
2195 /* If the BIOS described our SDVO device, take advantage of it. */
2196 if (my_mapping->slave_addr)
2197 return my_mapping->slave_addr;
2198
2199 /* If the BIOS only described a different SDVO device, use the
2200 * address that it isn't using.
2201 */
2202 if (other_mapping->slave_addr) {
2203 if (other_mapping->slave_addr == 0x70)
2204 return 0x72;
2205 else
2206 return 0x70;
2207 }
2208
2209 /* No SDVO device info is found for another DVO port,
2210 * so use mapping assumption we had before BIOS parsing.
2211 */
eef4eacb 2212 if (sdvo->is_sdvob)
714605e4 2213 return 0x70;
2214 else
2215 return 0x72;
2216}
2217
14571b4c 2218static void
df0e9248
CW
2219intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
2220 struct intel_sdvo *encoder)
14571b4c 2221{
df0e9248
CW
2222 drm_connector_init(encoder->base.base.dev,
2223 &connector->base.base,
2224 &intel_sdvo_connector_funcs,
2225 connector->base.base.connector_type);
6070a4a9 2226
df0e9248
CW
2227 drm_connector_helper_add(&connector->base.base,
2228 &intel_sdvo_connector_helper_funcs);
14571b4c 2229
8f4839e2 2230 connector->base.base.interlace_allowed = 1;
df0e9248
CW
2231 connector->base.base.doublescan_allowed = 0;
2232 connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
4ac41f47 2233 connector->base.get_hw_state = intel_sdvo_connector_get_hw_state;
14571b4c 2234
df0e9248
CW
2235 intel_connector_attach_encoder(&connector->base, &encoder->base);
2236 drm_sysfs_connector_add(&connector->base.base);
14571b4c 2237}
6070a4a9 2238
7f36e7ed 2239static void
55bc60db
VS
2240intel_sdvo_add_hdmi_properties(struct intel_sdvo *intel_sdvo,
2241 struct intel_sdvo_connector *connector)
7f36e7ed
CW
2242{
2243 struct drm_device *dev = connector->base.base.dev;
2244
3f43c48d 2245 intel_attach_force_audio_property(&connector->base.base);
55bc60db 2246 if (INTEL_INFO(dev)->gen >= 4 && IS_MOBILE(dev)) {
e953fd7b 2247 intel_attach_broadcast_rgb_property(&connector->base.base);
55bc60db
VS
2248 intel_sdvo->color_range_auto = true;
2249 }
7f36e7ed
CW
2250}
2251
fb7a46f3 2252static bool
ea5b213a 2253intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
fb7a46f3 2254{
4ef69c7a 2255 struct drm_encoder *encoder = &intel_sdvo->base.base;
14571b4c 2256 struct drm_connector *connector;
cc68c81a 2257 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
14571b4c 2258 struct intel_connector *intel_connector;
615fb93f 2259 struct intel_sdvo_connector *intel_sdvo_connector;
14571b4c 2260
615fb93f
CW
2261 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2262 if (!intel_sdvo_connector)
14571b4c
ZW
2263 return false;
2264
14571b4c 2265 if (device == 0) {
ea5b213a 2266 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
615fb93f 2267 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
14571b4c 2268 } else if (device == 1) {
ea5b213a 2269 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
615fb93f 2270 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
14571b4c
ZW
2271 }
2272
615fb93f 2273 intel_connector = &intel_sdvo_connector->base;
14571b4c 2274 connector = &intel_connector->base;
5fa7ac9c
JN
2275 if (intel_sdvo_get_hotplug_support(intel_sdvo) &
2276 intel_sdvo_connector->output_flag) {
5fa7ac9c 2277 intel_sdvo->hotplug_active |= intel_sdvo_connector->output_flag;
cc68c81a
SF
2278 /* Some SDVO devices have one-shot hotplug interrupts.
2279 * Ensure that they get re-enabled when an interrupt happens.
2280 */
2281 intel_encoder->hot_plug = intel_sdvo_enable_hotplug;
2282 intel_sdvo_enable_hotplug(intel_encoder);
5fa7ac9c 2283 } else {
821450c6 2284 intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
5fa7ac9c 2285 }
14571b4c
ZW
2286 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2287 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2288
e27d8538 2289 if (intel_sdvo_is_hdmi_connector(intel_sdvo, device)) {
14571b4c 2290 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
e27d8538 2291 intel_sdvo->is_hdmi = true;
14571b4c 2292 }
14571b4c 2293
df0e9248 2294 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
f797d221 2295 if (intel_sdvo->is_hdmi)
55bc60db 2296 intel_sdvo_add_hdmi_properties(intel_sdvo, intel_sdvo_connector);
14571b4c
ZW
2297
2298 return true;
2299}
2300
2301static bool
ea5b213a 2302intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
14571b4c 2303{
4ef69c7a
CW
2304 struct drm_encoder *encoder = &intel_sdvo->base.base;
2305 struct drm_connector *connector;
2306 struct intel_connector *intel_connector;
2307 struct intel_sdvo_connector *intel_sdvo_connector;
14571b4c 2308
615fb93f
CW
2309 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2310 if (!intel_sdvo_connector)
2311 return false;
14571b4c 2312
615fb93f 2313 intel_connector = &intel_sdvo_connector->base;
4ef69c7a
CW
2314 connector = &intel_connector->base;
2315 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2316 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
14571b4c 2317
4ef69c7a
CW
2318 intel_sdvo->controlled_output |= type;
2319 intel_sdvo_connector->output_flag = type;
14571b4c 2320
4ef69c7a
CW
2321 intel_sdvo->is_tv = true;
2322 intel_sdvo->base.needs_tv_clock = true;
14571b4c 2323
df0e9248 2324 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
14571b4c 2325
4ef69c7a 2326 if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
32aad86f 2327 goto err;
14571b4c 2328
4ef69c7a 2329 if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
32aad86f 2330 goto err;
14571b4c 2331
4ef69c7a 2332 return true;
32aad86f
CW
2333
2334err:
123d5c01 2335 intel_sdvo_destroy(connector);
32aad86f 2336 return false;
14571b4c
ZW
2337}
2338
2339static bool
ea5b213a 2340intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
14571b4c 2341{
4ef69c7a
CW
2342 struct drm_encoder *encoder = &intel_sdvo->base.base;
2343 struct drm_connector *connector;
2344 struct intel_connector *intel_connector;
2345 struct intel_sdvo_connector *intel_sdvo_connector;
14571b4c 2346
615fb93f
CW
2347 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2348 if (!intel_sdvo_connector)
2349 return false;
14571b4c 2350
615fb93f 2351 intel_connector = &intel_sdvo_connector->base;
4ef69c7a 2352 connector = &intel_connector->base;
821450c6 2353 intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
4ef69c7a
CW
2354 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2355 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
2356
2357 if (device == 0) {
2358 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
2359 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2360 } else if (device == 1) {
2361 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
2362 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2363 }
2364
df0e9248
CW
2365 intel_sdvo_connector_init(intel_sdvo_connector,
2366 intel_sdvo);
4ef69c7a 2367 return true;
14571b4c
ZW
2368}
2369
2370static bool
ea5b213a 2371intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
14571b4c 2372{
4ef69c7a
CW
2373 struct drm_encoder *encoder = &intel_sdvo->base.base;
2374 struct drm_connector *connector;
2375 struct intel_connector *intel_connector;
2376 struct intel_sdvo_connector *intel_sdvo_connector;
14571b4c 2377
615fb93f
CW
2378 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2379 if (!intel_sdvo_connector)
2380 return false;
14571b4c 2381
615fb93f
CW
2382 intel_connector = &intel_sdvo_connector->base;
2383 connector = &intel_connector->base;
4ef69c7a
CW
2384 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2385 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2386
2387 if (device == 0) {
2388 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
2389 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2390 } else if (device == 1) {
2391 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
2392 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2393 }
2394
df0e9248 2395 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
4ef69c7a 2396 if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
32aad86f
CW
2397 goto err;
2398
2399 return true;
2400
2401err:
123d5c01 2402 intel_sdvo_destroy(connector);
32aad86f 2403 return false;
14571b4c
ZW
2404}
2405
2406static bool
ea5b213a 2407intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
14571b4c 2408{
ea5b213a
CW
2409 intel_sdvo->is_tv = false;
2410 intel_sdvo->base.needs_tv_clock = false;
2411 intel_sdvo->is_lvds = false;
fb7a46f3 2412
14571b4c 2413 /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
fb7a46f3 2414
14571b4c 2415 if (flags & SDVO_OUTPUT_TMDS0)
ea5b213a 2416 if (!intel_sdvo_dvi_init(intel_sdvo, 0))
14571b4c
ZW
2417 return false;
2418
2419 if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
ea5b213a 2420 if (!intel_sdvo_dvi_init(intel_sdvo, 1))
14571b4c
ZW
2421 return false;
2422
2423 /* TV has no XXX1 function block */
a1f4b7ff 2424 if (flags & SDVO_OUTPUT_SVID0)
ea5b213a 2425 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
14571b4c
ZW
2426 return false;
2427
2428 if (flags & SDVO_OUTPUT_CVBS0)
ea5b213a 2429 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
14571b4c 2430 return false;
fb7a46f3 2431
a0b1c7a5
CW
2432 if (flags & SDVO_OUTPUT_YPRPB0)
2433 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_YPRPB0))
2434 return false;
2435
14571b4c 2436 if (flags & SDVO_OUTPUT_RGB0)
ea5b213a 2437 if (!intel_sdvo_analog_init(intel_sdvo, 0))
14571b4c
ZW
2438 return false;
2439
2440 if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
ea5b213a 2441 if (!intel_sdvo_analog_init(intel_sdvo, 1))
14571b4c
ZW
2442 return false;
2443
2444 if (flags & SDVO_OUTPUT_LVDS0)
ea5b213a 2445 if (!intel_sdvo_lvds_init(intel_sdvo, 0))
14571b4c
ZW
2446 return false;
2447
2448 if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
ea5b213a 2449 if (!intel_sdvo_lvds_init(intel_sdvo, 1))
14571b4c 2450 return false;
fb7a46f3 2451
14571b4c 2452 if ((flags & SDVO_OUTPUT_MASK) == 0) {
fb7a46f3 2453 unsigned char bytes[2];
2454
ea5b213a
CW
2455 intel_sdvo->controlled_output = 0;
2456 memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
51c8b407 2457 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
ea5b213a 2458 SDVO_NAME(intel_sdvo),
51c8b407 2459 bytes[0], bytes[1]);
14571b4c 2460 return false;
fb7a46f3 2461 }
27f8227b 2462 intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
fb7a46f3 2463
14571b4c 2464 return true;
fb7a46f3 2465}
2466
d0ddfbd3
JN
2467static void intel_sdvo_output_cleanup(struct intel_sdvo *intel_sdvo)
2468{
2469 struct drm_device *dev = intel_sdvo->base.base.dev;
2470 struct drm_connector *connector, *tmp;
2471
2472 list_for_each_entry_safe(connector, tmp,
2473 &dev->mode_config.connector_list, head) {
2474 if (intel_attached_encoder(connector) == &intel_sdvo->base)
2475 intel_sdvo_destroy(connector);
2476 }
2477}
2478
32aad86f
CW
2479static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
2480 struct intel_sdvo_connector *intel_sdvo_connector,
2481 int type)
ce6feabd 2482{
4ef69c7a 2483 struct drm_device *dev = intel_sdvo->base.base.dev;
ce6feabd
ZY
2484 struct intel_sdvo_tv_format format;
2485 uint32_t format_map, i;
ce6feabd 2486
32aad86f
CW
2487 if (!intel_sdvo_set_target_output(intel_sdvo, type))
2488 return false;
ce6feabd 2489
1a3665c8 2490 BUILD_BUG_ON(sizeof(format) != 6);
32aad86f
CW
2491 if (!intel_sdvo_get_value(intel_sdvo,
2492 SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
2493 &format, sizeof(format)))
2494 return false;
ce6feabd 2495
32aad86f 2496 memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
ce6feabd
ZY
2497
2498 if (format_map == 0)
32aad86f 2499 return false;
ce6feabd 2500
615fb93f 2501 intel_sdvo_connector->format_supported_num = 0;
ce6feabd 2502 for (i = 0 ; i < TV_FORMAT_NUM; i++)
40039750
CW
2503 if (format_map & (1 << i))
2504 intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
ce6feabd
ZY
2505
2506
c5521706 2507 intel_sdvo_connector->tv_format =
32aad86f
CW
2508 drm_property_create(dev, DRM_MODE_PROP_ENUM,
2509 "mode", intel_sdvo_connector->format_supported_num);
c5521706 2510 if (!intel_sdvo_connector->tv_format)
fcc8d672 2511 return false;
ce6feabd 2512
615fb93f 2513 for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
ce6feabd 2514 drm_property_add_enum(
c5521706 2515 intel_sdvo_connector->tv_format, i,
40039750 2516 i, tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
ce6feabd 2517
40039750 2518 intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[0];
662595df 2519 drm_object_attach_property(&intel_sdvo_connector->base.base.base,
c5521706 2520 intel_sdvo_connector->tv_format, 0);
32aad86f 2521 return true;
ce6feabd
ZY
2522
2523}
2524
c5521706
CW
2525#define ENHANCEMENT(name, NAME) do { \
2526 if (enhancements.name) { \
2527 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
2528 !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
2529 return false; \
2530 intel_sdvo_connector->max_##name = data_value[0]; \
2531 intel_sdvo_connector->cur_##name = response; \
2532 intel_sdvo_connector->name = \
d9bc3c02 2533 drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
c5521706 2534 if (!intel_sdvo_connector->name) return false; \
662595df 2535 drm_object_attach_property(&connector->base, \
c5521706
CW
2536 intel_sdvo_connector->name, \
2537 intel_sdvo_connector->cur_##name); \
2538 DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
2539 data_value[0], data_value[1], response); \
2540 } \
0206e353 2541} while (0)
c5521706
CW
2542
2543static bool
2544intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
2545 struct intel_sdvo_connector *intel_sdvo_connector,
2546 struct intel_sdvo_enhancements_reply enhancements)
b9219c5e 2547{
4ef69c7a 2548 struct drm_device *dev = intel_sdvo->base.base.dev;
32aad86f 2549 struct drm_connector *connector = &intel_sdvo_connector->base.base;
b9219c5e
ZY
2550 uint16_t response, data_value[2];
2551
c5521706
CW
2552 /* when horizontal overscan is supported, Add the left/right property */
2553 if (enhancements.overscan_h) {
2554 if (!intel_sdvo_get_value(intel_sdvo,
2555 SDVO_CMD_GET_MAX_OVERSCAN_H,
2556 &data_value, 4))
2557 return false;
32aad86f 2558
c5521706
CW
2559 if (!intel_sdvo_get_value(intel_sdvo,
2560 SDVO_CMD_GET_OVERSCAN_H,
2561 &response, 2))
2562 return false;
fcc8d672 2563
c5521706
CW
2564 intel_sdvo_connector->max_hscan = data_value[0];
2565 intel_sdvo_connector->left_margin = data_value[0] - response;
2566 intel_sdvo_connector->right_margin = intel_sdvo_connector->left_margin;
2567 intel_sdvo_connector->left =
d9bc3c02 2568 drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
c5521706
CW
2569 if (!intel_sdvo_connector->left)
2570 return false;
fcc8d672 2571
662595df 2572 drm_object_attach_property(&connector->base,
c5521706
CW
2573 intel_sdvo_connector->left,
2574 intel_sdvo_connector->left_margin);
fcc8d672 2575
c5521706 2576 intel_sdvo_connector->right =
d9bc3c02 2577 drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
c5521706
CW
2578 if (!intel_sdvo_connector->right)
2579 return false;
32aad86f 2580
662595df 2581 drm_object_attach_property(&connector->base,
c5521706
CW
2582 intel_sdvo_connector->right,
2583 intel_sdvo_connector->right_margin);
2584 DRM_DEBUG_KMS("h_overscan: max %d, "
2585 "default %d, current %d\n",
2586 data_value[0], data_value[1], response);
2587 }
32aad86f 2588
c5521706
CW
2589 if (enhancements.overscan_v) {
2590 if (!intel_sdvo_get_value(intel_sdvo,
2591 SDVO_CMD_GET_MAX_OVERSCAN_V,
2592 &data_value, 4))
2593 return false;
fcc8d672 2594
c5521706
CW
2595 if (!intel_sdvo_get_value(intel_sdvo,
2596 SDVO_CMD_GET_OVERSCAN_V,
2597 &response, 2))
2598 return false;
32aad86f 2599
c5521706
CW
2600 intel_sdvo_connector->max_vscan = data_value[0];
2601 intel_sdvo_connector->top_margin = data_value[0] - response;
2602 intel_sdvo_connector->bottom_margin = intel_sdvo_connector->top_margin;
2603 intel_sdvo_connector->top =
d9bc3c02
SH
2604 drm_property_create_range(dev, 0,
2605 "top_margin", 0, data_value[0]);
c5521706
CW
2606 if (!intel_sdvo_connector->top)
2607 return false;
32aad86f 2608
662595df 2609 drm_object_attach_property(&connector->base,
c5521706
CW
2610 intel_sdvo_connector->top,
2611 intel_sdvo_connector->top_margin);
fcc8d672 2612
c5521706 2613 intel_sdvo_connector->bottom =
d9bc3c02
SH
2614 drm_property_create_range(dev, 0,
2615 "bottom_margin", 0, data_value[0]);
c5521706
CW
2616 if (!intel_sdvo_connector->bottom)
2617 return false;
32aad86f 2618
662595df 2619 drm_object_attach_property(&connector->base,
c5521706
CW
2620 intel_sdvo_connector->bottom,
2621 intel_sdvo_connector->bottom_margin);
2622 DRM_DEBUG_KMS("v_overscan: max %d, "
2623 "default %d, current %d\n",
2624 data_value[0], data_value[1], response);
2625 }
32aad86f 2626
c5521706
CW
2627 ENHANCEMENT(hpos, HPOS);
2628 ENHANCEMENT(vpos, VPOS);
2629 ENHANCEMENT(saturation, SATURATION);
2630 ENHANCEMENT(contrast, CONTRAST);
2631 ENHANCEMENT(hue, HUE);
2632 ENHANCEMENT(sharpness, SHARPNESS);
2633 ENHANCEMENT(brightness, BRIGHTNESS);
2634 ENHANCEMENT(flicker_filter, FLICKER_FILTER);
2635 ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
2636 ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
2637 ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
2638 ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
fcc8d672 2639
e044218a
CW
2640 if (enhancements.dot_crawl) {
2641 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
2642 return false;
2643
2644 intel_sdvo_connector->max_dot_crawl = 1;
2645 intel_sdvo_connector->cur_dot_crawl = response & 0x1;
2646 intel_sdvo_connector->dot_crawl =
d9bc3c02 2647 drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
e044218a
CW
2648 if (!intel_sdvo_connector->dot_crawl)
2649 return false;
2650
662595df 2651 drm_object_attach_property(&connector->base,
e044218a
CW
2652 intel_sdvo_connector->dot_crawl,
2653 intel_sdvo_connector->cur_dot_crawl);
2654 DRM_DEBUG_KMS("dot crawl: current %d\n", response);
2655 }
2656
c5521706
CW
2657 return true;
2658}
32aad86f 2659
c5521706
CW
2660static bool
2661intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
2662 struct intel_sdvo_connector *intel_sdvo_connector,
2663 struct intel_sdvo_enhancements_reply enhancements)
2664{
4ef69c7a 2665 struct drm_device *dev = intel_sdvo->base.base.dev;
c5521706
CW
2666 struct drm_connector *connector = &intel_sdvo_connector->base.base;
2667 uint16_t response, data_value[2];
32aad86f 2668
c5521706 2669 ENHANCEMENT(brightness, BRIGHTNESS);
fcc8d672 2670
c5521706
CW
2671 return true;
2672}
2673#undef ENHANCEMENT
32aad86f 2674
c5521706
CW
2675static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
2676 struct intel_sdvo_connector *intel_sdvo_connector)
2677{
2678 union {
2679 struct intel_sdvo_enhancements_reply reply;
2680 uint16_t response;
2681 } enhancements;
32aad86f 2682
1a3665c8
CW
2683 BUILD_BUG_ON(sizeof(enhancements) != 2);
2684
cf9a2f3a
CW
2685 enhancements.response = 0;
2686 intel_sdvo_get_value(intel_sdvo,
2687 SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
2688 &enhancements, sizeof(enhancements));
c5521706
CW
2689 if (enhancements.response == 0) {
2690 DRM_DEBUG_KMS("No enhancement is supported\n");
2691 return true;
b9219c5e 2692 }
32aad86f 2693
c5521706
CW
2694 if (IS_TV(intel_sdvo_connector))
2695 return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
0206e353 2696 else if (IS_LVDS(intel_sdvo_connector))
c5521706
CW
2697 return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
2698 else
2699 return true;
e957d772
CW
2700}
2701
2702static int intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
2703 struct i2c_msg *msgs,
2704 int num)
2705{
2706 struct intel_sdvo *sdvo = adapter->algo_data;
fcc8d672 2707
e957d772
CW
2708 if (!intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
2709 return -EIO;
2710
2711 return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
2712}
2713
2714static u32 intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
2715{
2716 struct intel_sdvo *sdvo = adapter->algo_data;
2717 return sdvo->i2c->algo->functionality(sdvo->i2c);
2718}
2719
2720static const struct i2c_algorithm intel_sdvo_ddc_proxy = {
2721 .master_xfer = intel_sdvo_ddc_proxy_xfer,
2722 .functionality = intel_sdvo_ddc_proxy_func
2723};
2724
2725static bool
2726intel_sdvo_init_ddc_proxy(struct intel_sdvo *sdvo,
2727 struct drm_device *dev)
2728{
2729 sdvo->ddc.owner = THIS_MODULE;
2730 sdvo->ddc.class = I2C_CLASS_DDC;
2731 snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
2732 sdvo->ddc.dev.parent = &dev->pdev->dev;
2733 sdvo->ddc.algo_data = sdvo;
2734 sdvo->ddc.algo = &intel_sdvo_ddc_proxy;
2735
2736 return i2c_add_adapter(&sdvo->ddc) == 0;
b9219c5e
ZY
2737}
2738
eef4eacb 2739bool intel_sdvo_init(struct drm_device *dev, uint32_t sdvo_reg, bool is_sdvob)
79e53945 2740{
b01f2c3a 2741 struct drm_i915_private *dev_priv = dev->dev_private;
21d40d37 2742 struct intel_encoder *intel_encoder;
ea5b213a 2743 struct intel_sdvo *intel_sdvo;
084b612e 2744 u32 hotplug_mask;
79e53945 2745 int i;
ea5b213a
CW
2746 intel_sdvo = kzalloc(sizeof(struct intel_sdvo), GFP_KERNEL);
2747 if (!intel_sdvo)
7d57382e 2748 return false;
79e53945 2749
56184e3d 2750 intel_sdvo->sdvo_reg = sdvo_reg;
eef4eacb
DV
2751 intel_sdvo->is_sdvob = is_sdvob;
2752 intel_sdvo->slave_addr = intel_sdvo_get_slave_addr(dev, intel_sdvo) >> 1;
56184e3d 2753 intel_sdvo_select_i2c_bus(dev_priv, intel_sdvo, sdvo_reg);
fbfcc4f3
JN
2754 if (!intel_sdvo_init_ddc_proxy(intel_sdvo, dev))
2755 goto err_i2c_bus;
e957d772 2756
56184e3d 2757 /* encoder type will be decided later */
ea5b213a 2758 intel_encoder = &intel_sdvo->base;
21d40d37 2759 intel_encoder->type = INTEL_OUTPUT_SDVO;
373a3cf7 2760 drm_encoder_init(dev, &intel_encoder->base, &intel_sdvo_enc_funcs, 0);
79e53945 2761
79e53945
JB
2762 /* Read the regs to test if we can talk to the device */
2763 for (i = 0; i < 0x40; i++) {
f899fc64
CW
2764 u8 byte;
2765
2766 if (!intel_sdvo_read_byte(intel_sdvo, i, &byte)) {
eef4eacb
DV
2767 DRM_DEBUG_KMS("No SDVO device found on %s\n",
2768 SDVO_NAME(intel_sdvo));
f899fc64 2769 goto err;
79e53945
JB
2770 }
2771 }
2772
084b612e
CW
2773 hotplug_mask = 0;
2774 if (IS_G4X(dev)) {
2775 hotplug_mask = intel_sdvo->is_sdvob ?
2776 SDVOB_HOTPLUG_INT_STATUS_G4X : SDVOC_HOTPLUG_INT_STATUS_G4X;
2777 } else if (IS_GEN4(dev)) {
2778 hotplug_mask = intel_sdvo->is_sdvob ?
2779 SDVOB_HOTPLUG_INT_STATUS_I965 : SDVOC_HOTPLUG_INT_STATUS_I965;
2780 } else {
2781 hotplug_mask = intel_sdvo->is_sdvob ?
2782 SDVOB_HOTPLUG_INT_STATUS_I915 : SDVOC_HOTPLUG_INT_STATUS_I915;
2783 }
619ac3b7 2784
4f770a5b
EE
2785 /* Only enable the hotplug irq if we need it, to work around noisy
2786 * hotplug lines.
2787 */
1d843f9d
EE
2788 if (intel_sdvo->hotplug_active)
2789 intel_encoder->hpd_pin = HPD_SDVO_B ? HPD_SDVO_B : HPD_SDVO_C;
2790
6cc5f341 2791 intel_encoder->compute_config = intel_sdvo_compute_config;
ce22c320 2792 intel_encoder->disable = intel_disable_sdvo;
6cc5f341 2793 intel_encoder->mode_set = intel_sdvo_mode_set;
ce22c320 2794 intel_encoder->enable = intel_enable_sdvo;
4ac41f47 2795 intel_encoder->get_hw_state = intel_sdvo_get_hw_state;
ce22c320 2796
af901ca1 2797 /* In default case sdvo lvds is false */
32aad86f 2798 if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
f899fc64 2799 goto err;
79e53945 2800
ea5b213a
CW
2801 if (intel_sdvo_output_setup(intel_sdvo,
2802 intel_sdvo->caps.output_flags) != true) {
eef4eacb
DV
2803 DRM_DEBUG_KMS("SDVO output failed to setup on %s\n",
2804 SDVO_NAME(intel_sdvo));
d0ddfbd3
JN
2805 /* Output_setup can leave behind connectors! */
2806 goto err_output;
79e53945
JB
2807 }
2808
e506d6fd
DV
2809 /*
2810 * Cloning SDVO with anything is often impossible, since the SDVO
2811 * encoder can request a special input timing mode. And even if that's
2812 * not the case we have evidence that cloning a plain unscaled mode with
2813 * VGA doesn't really work. Furthermore the cloning flags are way too
2814 * simplistic anyway to express such constraints, so just give up on
2815 * cloning for SDVO encoders.
2816 */
2817 intel_sdvo->base.cloneable = false;
2818
ea5b213a 2819 intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo, sdvo_reg);
e2f0ba97 2820
79e53945 2821 /* Set the input timing to the screen. Assume always input 0. */
32aad86f 2822 if (!intel_sdvo_set_target_input(intel_sdvo))
d0ddfbd3 2823 goto err_output;
79e53945 2824
32aad86f
CW
2825 if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
2826 &intel_sdvo->pixel_clock_min,
2827 &intel_sdvo->pixel_clock_max))
d0ddfbd3 2828 goto err_output;
79e53945 2829
8a4c47f3 2830 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
342dc382 2831 "clock range %dMHz - %dMHz, "
2832 "input 1: %c, input 2: %c, "
2833 "output 1: %c, output 2: %c\n",
ea5b213a
CW
2834 SDVO_NAME(intel_sdvo),
2835 intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
2836 intel_sdvo->caps.device_rev_id,
2837 intel_sdvo->pixel_clock_min / 1000,
2838 intel_sdvo->pixel_clock_max / 1000,
2839 (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2840 (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
342dc382 2841 /* check currently supported outputs */
ea5b213a 2842 intel_sdvo->caps.output_flags &
79e53945 2843 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
ea5b213a 2844 intel_sdvo->caps.output_flags &
79e53945 2845 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
7d57382e 2846 return true;
79e53945 2847
d0ddfbd3
JN
2848err_output:
2849 intel_sdvo_output_cleanup(intel_sdvo);
2850
f899fc64 2851err:
373a3cf7 2852 drm_encoder_cleanup(&intel_encoder->base);
e957d772 2853 i2c_del_adapter(&intel_sdvo->ddc);
fbfcc4f3
JN
2854err_i2c_bus:
2855 intel_sdvo_unselect_i2c_bus(intel_sdvo);
ea5b213a 2856 kfree(intel_sdvo);
79e53945 2857
7d57382e 2858 return false;
79e53945 2859}
This page took 0.784696 seconds and 5 git commands to generate.