blob: 91238f0c39a5932697f9b0251bc98b63b7fc2d60 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
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>
29#include <linux/delay.h>
30#include "drmP.h"
31#include "drm.h"
32#include "drm_crtc.h"
33#include "intel_drv.h"
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +080034#include "drm_edid.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "i915_drm.h"
36#include "i915_drv.h"
37#include "intel_sdvo_regs.h"
38
39#undef SDVO_DEBUG
Jesse Barnes79e53942008-11-07 14:24:08 -080040struct intel_sdvo_priv {
Keith Packardf9c10a92009-05-30 12:16:25 -070041 u8 slave_addr;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080042
43 /* Register for the SDVO device: SDVOB or SDVOC */
Jesse Barnes79e53942008-11-07 14:24:08 -080044 int output_device;
45
Jesse Barnese2f0ba92009-02-02 15:11:52 -080046 /* Active outputs controlled by this SDVO output */
47 uint16_t controlled_output;
Jesse Barnes79e53942008-11-07 14:24:08 -080048
Jesse Barnese2f0ba92009-02-02 15:11:52 -080049 /*
50 * Capabilities of the SDVO device returned by
51 * i830_sdvo_get_capabilities()
52 */
Jesse Barnes79e53942008-11-07 14:24:08 -080053 struct intel_sdvo_caps caps;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080054
55 /* Pixel clock limitations reported by the SDVO device, in kHz */
Jesse Barnes79e53942008-11-07 14:24:08 -080056 int pixel_clock_min, pixel_clock_max;
57
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +080058 /*
59 * For multiple function SDVO device,
60 * this is for current attached outputs.
61 */
62 uint16_t attached_output;
63
Jesse Barnese2f0ba92009-02-02 15:11:52 -080064 /**
65 * This is set if we're going to treat the device as TV-out.
66 *
67 * While we have these nice friendly flags for output types that ought
68 * to decide this for us, the S-Video output on our HDMI+S-Video card
69 * shows up as RGB1 (VGA).
70 */
71 bool is_tv;
72
73 /**
74 * This is set if we treat the device as HDMI, instead of DVI.
75 */
76 bool is_hdmi;
ling.ma@intel.com12682a92009-06-30 11:35:35 +080077
Ma Ling7086c872009-05-13 11:20:06 +080078 /**
79 * This is set if we detect output of sdvo device as LVDS.
80 */
81 bool is_lvds;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080082
83 /**
ling.ma@intel.com12682a92009-06-30 11:35:35 +080084 * This is sdvo flags for input timing.
85 */
86 uint8_t sdvo_flags;
87
88 /**
89 * This is sdvo fixed pannel mode pointer
90 */
91 struct drm_display_mode *sdvo_lvds_fixed_mode;
92
93 /**
Jesse Barnese2f0ba92009-02-02 15:11:52 -080094 * Returned SDTV resolutions allowed for the current format, if the
95 * device reported it.
96 */
97 struct intel_sdvo_sdtv_resolution_reply sdtv_resolutions;
98
99 /**
100 * Current selected TV format.
101 *
102 * This is stored in the same structure that's passed to the device, for
103 * convenience.
104 */
105 struct intel_sdvo_tv_format tv_format;
106
107 /*
108 * supported encoding mode, used to determine whether HDMI is
109 * supported
110 */
111 struct intel_sdvo_encode encode;
112
113 /* DDC bus used by this SDVO output */
114 uint8_t ddc_bus;
115
Jesse Barnes79e53942008-11-07 14:24:08 -0800116 int save_sdvo_mult;
117 u16 save_active_outputs;
118 struct intel_sdvo_dtd save_input_dtd_1, save_input_dtd_2;
119 struct intel_sdvo_dtd save_output_dtd[16];
120 u32 save_SDVOX;
121};
122
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800123static bool
124intel_sdvo_output_setup(struct intel_output *intel_output, uint16_t flags);
125
Jesse Barnes79e53942008-11-07 14:24:08 -0800126/**
127 * Writes the SDVOB or SDVOC with the given value, but always writes both
128 * SDVOB and SDVOC to work around apparent hardware issues (according to
129 * comments in the BIOS).
130 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100131static void intel_sdvo_write_sdvox(struct intel_output *intel_output, u32 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800132{
133 struct drm_device *dev = intel_output->base.dev;
134 struct drm_i915_private *dev_priv = dev->dev_private;
135 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
136 u32 bval = val, cval = val;
137 int i;
138
139 if (sdvo_priv->output_device == SDVOB) {
140 cval = I915_READ(SDVOC);
141 } else {
142 bval = I915_READ(SDVOB);
143 }
144 /*
145 * Write the registers twice for luck. Sometimes,
146 * writing them only once doesn't appear to 'stick'.
147 * The BIOS does this too. Yay, magic
148 */
149 for (i = 0; i < 2; i++)
150 {
151 I915_WRITE(SDVOB, bval);
152 I915_READ(SDVOB);
153 I915_WRITE(SDVOC, cval);
154 I915_READ(SDVOC);
155 }
156}
157
158static bool intel_sdvo_read_byte(struct intel_output *intel_output, u8 addr,
159 u8 *ch)
160{
161 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
162 u8 out_buf[2];
163 u8 buf[2];
164 int ret;
165
166 struct i2c_msg msgs[] = {
167 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700168 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800169 .flags = 0,
170 .len = 1,
171 .buf = out_buf,
172 },
173 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700174 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800175 .flags = I2C_M_RD,
176 .len = 1,
177 .buf = buf,
178 }
179 };
180
181 out_buf[0] = addr;
182 out_buf[1] = 0;
183
Keith Packard308cd3a2009-06-14 11:56:18 -0700184 if ((ret = i2c_transfer(intel_output->i2c_bus, msgs, 2)) == 2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800185 {
186 *ch = buf[0];
187 return true;
188 }
189
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800190 DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
Jesse Barnes79e53942008-11-07 14:24:08 -0800191 return false;
192}
193
194static bool intel_sdvo_write_byte(struct intel_output *intel_output, int addr,
195 u8 ch)
196{
Keith Packardf9c10a92009-05-30 12:16:25 -0700197 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800198 u8 out_buf[2];
199 struct i2c_msg msgs[] = {
200 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700201 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800202 .flags = 0,
203 .len = 2,
204 .buf = out_buf,
205 }
206 };
207
208 out_buf[0] = addr;
209 out_buf[1] = ch;
210
Keith Packardf9c10a92009-05-30 12:16:25 -0700211 if (i2c_transfer(intel_output->i2c_bus, msgs, 1) == 1)
Jesse Barnes79e53942008-11-07 14:24:08 -0800212 {
213 return true;
214 }
215 return false;
216}
217
218#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
219/** Mapping of command numbers to names, for debug output */
Tobias Klauser005568b2009-02-09 22:02:42 +0100220static const struct _sdvo_cmd_name {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800221 u8 cmd;
222 char *name;
Jesse Barnes79e53942008-11-07 14:24:08 -0800223} sdvo_cmd_names[] = {
224 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
225 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
226 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
227 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
228 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
229 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
230 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
231 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
232 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
233 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
234 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
235 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
236 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
237 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
238 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
239 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
240 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
241 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
242 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
243 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
244 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
245 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
246 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
247 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
248 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
249 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
250 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
251 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
252 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
253 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
254 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
255 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
256 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
257 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
258 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800259 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
260 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
261 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
262 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
Jesse Barnes79e53942008-11-07 14:24:08 -0800263 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800264 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
265 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
266 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
267 /* HDMI op code */
268 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
269 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
270 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
271 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
272 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
273 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
274 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
275 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
276 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
277 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
278 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
279 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
280 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
281 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
282 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
283 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
284 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
285 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
286 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
287 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
Jesse Barnes79e53942008-11-07 14:24:08 -0800288};
289
290#define SDVO_NAME(dev_priv) ((dev_priv)->output_device == SDVOB ? "SDVOB" : "SDVOC")
291#define SDVO_PRIV(output) ((struct intel_sdvo_priv *) (output)->dev_priv)
292
293#ifdef SDVO_DEBUG
294static void intel_sdvo_debug_write(struct intel_output *intel_output, u8 cmd,
295 void *args, int args_len)
296{
297 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
298 int i;
299
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800300 DRM_DEBUG_KMS("%s: W: %02X ",
yakui_zhao342dc382009-06-02 14:12:00 +0800301 SDVO_NAME(sdvo_priv), cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800302 for (i = 0; i < args_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800303 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800304 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800305 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800306 for (i = 0; i < sizeof(sdvo_cmd_names) / sizeof(sdvo_cmd_names[0]); i++) {
307 if (cmd == sdvo_cmd_names[i].cmd) {
yakui_zhao342dc382009-06-02 14:12:00 +0800308 DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
Jesse Barnes79e53942008-11-07 14:24:08 -0800309 break;
310 }
311 }
312 if (i == sizeof(sdvo_cmd_names)/ sizeof(sdvo_cmd_names[0]))
yakui_zhao342dc382009-06-02 14:12:00 +0800313 DRM_LOG_KMS("(%02X)", cmd);
314 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800315}
316#else
317#define intel_sdvo_debug_write(o, c, a, l)
318#endif
319
320static void intel_sdvo_write_cmd(struct intel_output *intel_output, u8 cmd,
321 void *args, int args_len)
322{
323 int i;
324
325 intel_sdvo_debug_write(intel_output, cmd, args, args_len);
326
327 for (i = 0; i < args_len; i++) {
328 intel_sdvo_write_byte(intel_output, SDVO_I2C_ARG_0 - i,
329 ((u8*)args)[i]);
330 }
331
332 intel_sdvo_write_byte(intel_output, SDVO_I2C_OPCODE, cmd);
333}
334
335#ifdef SDVO_DEBUG
336static const char *cmd_status_names[] = {
337 "Power on",
338 "Success",
339 "Not supported",
340 "Invalid arg",
341 "Pending",
342 "Target not specified",
343 "Scaling not supported"
344};
345
346static void intel_sdvo_debug_response(struct intel_output *intel_output,
347 void *response, int response_len,
348 u8 status)
349{
350 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Zhenyu Wang33b52962009-03-24 14:02:40 +0800351 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -0800352
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800353 DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -0800354 for (i = 0; i < response_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800355 DRM_LOG_KMS("%02X ", ((u8 *)response)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800356 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800357 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800358 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
yakui_zhao342dc382009-06-02 14:12:00 +0800359 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800360 else
yakui_zhao342dc382009-06-02 14:12:00 +0800361 DRM_LOG_KMS("(??? %d)", status);
362 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800363}
364#else
365#define intel_sdvo_debug_response(o, r, l, s)
366#endif
367
368static u8 intel_sdvo_read_response(struct intel_output *intel_output,
369 void *response, int response_len)
370{
371 int i;
372 u8 status;
373 u8 retry = 50;
374
375 while (retry--) {
376 /* Read the command response */
377 for (i = 0; i < response_len; i++) {
378 intel_sdvo_read_byte(intel_output,
379 SDVO_I2C_RETURN_0 + i,
380 &((u8 *)response)[i]);
381 }
382
383 /* read the return status */
384 intel_sdvo_read_byte(intel_output, SDVO_I2C_CMD_STATUS,
385 &status);
386
387 intel_sdvo_debug_response(intel_output, response, response_len,
388 status);
389 if (status != SDVO_CMD_STATUS_PENDING)
390 return status;
391
392 mdelay(50);
393 }
394
395 return status;
396}
397
Hannes Ederb358d0a2008-12-18 21:18:47 +0100398static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800399{
400 if (mode->clock >= 100000)
401 return 1;
402 else if (mode->clock >= 50000)
403 return 2;
404 else
405 return 4;
406}
407
408/**
409 * Don't check status code from this as it switches the bus back to the
410 * SDVO chips which defeats the purpose of doing a bus switch in the first
411 * place.
412 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100413static void intel_sdvo_set_control_bus_switch(struct intel_output *intel_output,
414 u8 target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800415{
416 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CONTROL_BUS_SWITCH, &target, 1);
417}
418
419static bool intel_sdvo_set_target_input(struct intel_output *intel_output, bool target_0, bool target_1)
420{
421 struct intel_sdvo_set_target_input_args targets = {0};
422 u8 status;
423
424 if (target_0 && target_1)
425 return SDVO_CMD_STATUS_NOTSUPP;
426
427 if (target_1)
428 targets.target_1 = 1;
429
430 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_INPUT, &targets,
431 sizeof(targets));
432
433 status = intel_sdvo_read_response(intel_output, NULL, 0);
434
435 return (status == SDVO_CMD_STATUS_SUCCESS);
436}
437
438/**
439 * Return whether each input is trained.
440 *
441 * This function is making an assumption about the layout of the response,
442 * which should be checked against the docs.
443 */
444static bool intel_sdvo_get_trained_inputs(struct intel_output *intel_output, bool *input_1, bool *input_2)
445{
446 struct intel_sdvo_get_trained_inputs_response response;
447 u8 status;
448
449 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_TRAINED_INPUTS, NULL, 0);
450 status = intel_sdvo_read_response(intel_output, &response, sizeof(response));
451 if (status != SDVO_CMD_STATUS_SUCCESS)
452 return false;
453
454 *input_1 = response.input0_trained;
455 *input_2 = response.input1_trained;
456 return true;
457}
458
459static bool intel_sdvo_get_active_outputs(struct intel_output *intel_output,
460 u16 *outputs)
461{
462 u8 status;
463
464 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_OUTPUTS, NULL, 0);
465 status = intel_sdvo_read_response(intel_output, outputs, sizeof(*outputs));
466
467 return (status == SDVO_CMD_STATUS_SUCCESS);
468}
469
470static bool intel_sdvo_set_active_outputs(struct intel_output *intel_output,
471 u16 outputs)
472{
473 u8 status;
474
475 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_OUTPUTS, &outputs,
476 sizeof(outputs));
477 status = intel_sdvo_read_response(intel_output, NULL, 0);
478 return (status == SDVO_CMD_STATUS_SUCCESS);
479}
480
481static bool intel_sdvo_set_encoder_power_state(struct intel_output *intel_output,
482 int mode)
483{
484 u8 status, state = SDVO_ENCODER_STATE_ON;
485
486 switch (mode) {
487 case DRM_MODE_DPMS_ON:
488 state = SDVO_ENCODER_STATE_ON;
489 break;
490 case DRM_MODE_DPMS_STANDBY:
491 state = SDVO_ENCODER_STATE_STANDBY;
492 break;
493 case DRM_MODE_DPMS_SUSPEND:
494 state = SDVO_ENCODER_STATE_SUSPEND;
495 break;
496 case DRM_MODE_DPMS_OFF:
497 state = SDVO_ENCODER_STATE_OFF;
498 break;
499 }
500
501 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ENCODER_POWER_STATE, &state,
502 sizeof(state));
503 status = intel_sdvo_read_response(intel_output, NULL, 0);
504
505 return (status == SDVO_CMD_STATUS_SUCCESS);
506}
507
508static bool intel_sdvo_get_input_pixel_clock_range(struct intel_output *intel_output,
509 int *clock_min,
510 int *clock_max)
511{
512 struct intel_sdvo_pixel_clock_range clocks;
513 u8 status;
514
515 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
516 NULL, 0);
517
518 status = intel_sdvo_read_response(intel_output, &clocks, sizeof(clocks));
519
520 if (status != SDVO_CMD_STATUS_SUCCESS)
521 return false;
522
523 /* Convert the values from units of 10 kHz to kHz. */
524 *clock_min = clocks.min * 10;
525 *clock_max = clocks.max * 10;
526
527 return true;
528}
529
530static bool intel_sdvo_set_target_output(struct intel_output *intel_output,
531 u16 outputs)
532{
533 u8 status;
534
535 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_OUTPUT, &outputs,
536 sizeof(outputs));
537
538 status = intel_sdvo_read_response(intel_output, NULL, 0);
539 return (status == SDVO_CMD_STATUS_SUCCESS);
540}
541
542static bool intel_sdvo_get_timing(struct intel_output *intel_output, u8 cmd,
543 struct intel_sdvo_dtd *dtd)
544{
545 u8 status;
546
547 intel_sdvo_write_cmd(intel_output, cmd, NULL, 0);
548 status = intel_sdvo_read_response(intel_output, &dtd->part1,
549 sizeof(dtd->part1));
550 if (status != SDVO_CMD_STATUS_SUCCESS)
551 return false;
552
553 intel_sdvo_write_cmd(intel_output, cmd + 1, NULL, 0);
554 status = intel_sdvo_read_response(intel_output, &dtd->part2,
555 sizeof(dtd->part2));
556 if (status != SDVO_CMD_STATUS_SUCCESS)
557 return false;
558
559 return true;
560}
561
562static bool intel_sdvo_get_input_timing(struct intel_output *intel_output,
563 struct intel_sdvo_dtd *dtd)
564{
565 return intel_sdvo_get_timing(intel_output,
566 SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
567}
568
569static bool intel_sdvo_get_output_timing(struct intel_output *intel_output,
570 struct intel_sdvo_dtd *dtd)
571{
572 return intel_sdvo_get_timing(intel_output,
573 SDVO_CMD_GET_OUTPUT_TIMINGS_PART1, dtd);
574}
575
576static bool intel_sdvo_set_timing(struct intel_output *intel_output, u8 cmd,
577 struct intel_sdvo_dtd *dtd)
578{
579 u8 status;
580
581 intel_sdvo_write_cmd(intel_output, cmd, &dtd->part1, sizeof(dtd->part1));
582 status = intel_sdvo_read_response(intel_output, NULL, 0);
583 if (status != SDVO_CMD_STATUS_SUCCESS)
584 return false;
585
586 intel_sdvo_write_cmd(intel_output, cmd + 1, &dtd->part2, sizeof(dtd->part2));
587 status = intel_sdvo_read_response(intel_output, NULL, 0);
588 if (status != SDVO_CMD_STATUS_SUCCESS)
589 return false;
590
591 return true;
592}
593
594static bool intel_sdvo_set_input_timing(struct intel_output *intel_output,
595 struct intel_sdvo_dtd *dtd)
596{
597 return intel_sdvo_set_timing(intel_output,
598 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
599}
600
601static bool intel_sdvo_set_output_timing(struct intel_output *intel_output,
602 struct intel_sdvo_dtd *dtd)
603{
604 return intel_sdvo_set_timing(intel_output,
605 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
606}
607
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800608static bool
609intel_sdvo_create_preferred_input_timing(struct intel_output *output,
610 uint16_t clock,
611 uint16_t width,
612 uint16_t height)
613{
614 struct intel_sdvo_preferred_input_timing_args args;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800615 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800616 uint8_t status;
617
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800618 memset(&args, 0, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800619 args.clock = clock;
620 args.width = width;
621 args.height = height;
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800622 args.interlace = 0;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800623
624 if (sdvo_priv->is_lvds &&
625 (sdvo_priv->sdvo_lvds_fixed_mode->hdisplay != width ||
626 sdvo_priv->sdvo_lvds_fixed_mode->vdisplay != height))
627 args.scaled = 1;
628
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800629 intel_sdvo_write_cmd(output, SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
630 &args, sizeof(args));
631 status = intel_sdvo_read_response(output, NULL, 0);
632 if (status != SDVO_CMD_STATUS_SUCCESS)
633 return false;
634
635 return true;
636}
637
638static bool intel_sdvo_get_preferred_input_timing(struct intel_output *output,
639 struct intel_sdvo_dtd *dtd)
640{
641 bool status;
642
643 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
644 NULL, 0);
645
646 status = intel_sdvo_read_response(output, &dtd->part1,
647 sizeof(dtd->part1));
648 if (status != SDVO_CMD_STATUS_SUCCESS)
649 return false;
650
651 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
652 NULL, 0);
653
654 status = intel_sdvo_read_response(output, &dtd->part2,
655 sizeof(dtd->part2));
656 if (status != SDVO_CMD_STATUS_SUCCESS)
657 return false;
658
659 return false;
660}
Jesse Barnes79e53942008-11-07 14:24:08 -0800661
662static int intel_sdvo_get_clock_rate_mult(struct intel_output *intel_output)
663{
664 u8 response, status;
665
666 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_CLOCK_RATE_MULT, NULL, 0);
667 status = intel_sdvo_read_response(intel_output, &response, 1);
668
669 if (status != SDVO_CMD_STATUS_SUCCESS) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800670 DRM_DEBUG_KMS("Couldn't get SDVO clock rate multiplier\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800671 return SDVO_CLOCK_RATE_MULT_1X;
672 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800673 DRM_DEBUG_KMS("Current clock rate multiplier: %d\n", response);
Jesse Barnes79e53942008-11-07 14:24:08 -0800674 }
675
676 return response;
677}
678
679static bool intel_sdvo_set_clock_rate_mult(struct intel_output *intel_output, u8 val)
680{
681 u8 status;
682
683 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
684 status = intel_sdvo_read_response(intel_output, NULL, 0);
685 if (status != SDVO_CMD_STATUS_SUCCESS)
686 return false;
687
688 return true;
689}
690
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800691static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
692 struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800693{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800694 uint16_t width, height;
695 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
696 uint16_t h_sync_offset, v_sync_offset;
Jesse Barnes79e53942008-11-07 14:24:08 -0800697
698 width = mode->crtc_hdisplay;
699 height = mode->crtc_vdisplay;
700
701 /* do some mode translations */
702 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
703 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
704
705 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
706 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
707
708 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
709 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
710
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800711 dtd->part1.clock = mode->clock / 10;
712 dtd->part1.h_active = width & 0xff;
713 dtd->part1.h_blank = h_blank_len & 0xff;
714 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800715 ((h_blank_len >> 8) & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800716 dtd->part1.v_active = height & 0xff;
717 dtd->part1.v_blank = v_blank_len & 0xff;
718 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800719 ((v_blank_len >> 8) & 0xf);
720
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800721 dtd->part2.h_sync_off = h_sync_offset & 0xff;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800722 dtd->part2.h_sync_width = h_sync_len & 0xff;
723 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
Jesse Barnes79e53942008-11-07 14:24:08 -0800724 (v_sync_len & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800725 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800726 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
727 ((v_sync_len & 0x30) >> 4);
728
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800729 dtd->part2.dtd_flags = 0x18;
Jesse Barnes79e53942008-11-07 14:24:08 -0800730 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800731 dtd->part2.dtd_flags |= 0x2;
Jesse Barnes79e53942008-11-07 14:24:08 -0800732 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800733 dtd->part2.dtd_flags |= 0x4;
Jesse Barnes79e53942008-11-07 14:24:08 -0800734
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800735 dtd->part2.sdvo_flags = 0;
736 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
737 dtd->part2.reserved = 0;
738}
Jesse Barnes79e53942008-11-07 14:24:08 -0800739
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800740static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
741 struct intel_sdvo_dtd *dtd)
742{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800743 mode->hdisplay = dtd->part1.h_active;
744 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
745 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800746 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800747 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
748 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
749 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
750 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
751
752 mode->vdisplay = dtd->part1.v_active;
753 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
754 mode->vsync_start = mode->vdisplay;
755 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800756 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800757 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
758 mode->vsync_end = mode->vsync_start +
759 (dtd->part2.v_sync_off_width & 0xf);
760 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
761 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
762 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
763
764 mode->clock = dtd->part1.clock * 10;
765
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800766 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800767 if (dtd->part2.dtd_flags & 0x2)
768 mode->flags |= DRM_MODE_FLAG_PHSYNC;
769 if (dtd->part2.dtd_flags & 0x4)
770 mode->flags |= DRM_MODE_FLAG_PVSYNC;
771}
772
773static bool intel_sdvo_get_supp_encode(struct intel_output *output,
774 struct intel_sdvo_encode *encode)
775{
776 uint8_t status;
777
778 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
779 status = intel_sdvo_read_response(output, encode, sizeof(*encode));
780 if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
781 memset(encode, 0, sizeof(*encode));
782 return false;
783 }
784
785 return true;
786}
787
788static bool intel_sdvo_set_encode(struct intel_output *output, uint8_t mode)
789{
790 uint8_t status;
791
792 intel_sdvo_write_cmd(output, SDVO_CMD_SET_ENCODE, &mode, 1);
793 status = intel_sdvo_read_response(output, NULL, 0);
794
795 return (status == SDVO_CMD_STATUS_SUCCESS);
796}
797
798static bool intel_sdvo_set_colorimetry(struct intel_output *output,
799 uint8_t mode)
800{
801 uint8_t status;
802
803 intel_sdvo_write_cmd(output, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
804 status = intel_sdvo_read_response(output, NULL, 0);
805
806 return (status == SDVO_CMD_STATUS_SUCCESS);
807}
808
809#if 0
810static void intel_sdvo_dump_hdmi_buf(struct intel_output *output)
811{
812 int i, j;
813 uint8_t set_buf_index[2];
814 uint8_t av_split;
815 uint8_t buf_size;
816 uint8_t buf[48];
817 uint8_t *pos;
818
819 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
820 intel_sdvo_read_response(output, &av_split, 1);
821
822 for (i = 0; i <= av_split; i++) {
823 set_buf_index[0] = i; set_buf_index[1] = 0;
824 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX,
825 set_buf_index, 2);
826 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
827 intel_sdvo_read_response(output, &buf_size, 1);
828
829 pos = buf;
830 for (j = 0; j <= buf_size; j += 8) {
831 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_DATA,
832 NULL, 0);
833 intel_sdvo_read_response(output, pos, 8);
834 pos += 8;
835 }
836 }
837}
838#endif
839
840static void intel_sdvo_set_hdmi_buf(struct intel_output *output, int index,
841 uint8_t *data, int8_t size, uint8_t tx_rate)
842{
843 uint8_t set_buf_index[2];
844
845 set_buf_index[0] = index;
846 set_buf_index[1] = 0;
847
848 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX, set_buf_index, 2);
849
850 for (; size > 0; size -= 8) {
851 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_DATA, data, 8);
852 data += 8;
853 }
854
855 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
856}
857
858static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
859{
860 uint8_t csum = 0;
861 int i;
862
863 for (i = 0; i < size; i++)
864 csum += data[i];
865
866 return 0x100 - csum;
867}
868
869#define DIP_TYPE_AVI 0x82
870#define DIP_VERSION_AVI 0x2
871#define DIP_LEN_AVI 13
872
873struct dip_infoframe {
874 uint8_t type;
875 uint8_t version;
876 uint8_t len;
877 uint8_t checksum;
878 union {
879 struct {
880 /* Packet Byte #1 */
881 uint8_t S:2;
882 uint8_t B:2;
883 uint8_t A:1;
884 uint8_t Y:2;
885 uint8_t rsvd1:1;
886 /* Packet Byte #2 */
887 uint8_t R:4;
888 uint8_t M:2;
889 uint8_t C:2;
890 /* Packet Byte #3 */
891 uint8_t SC:2;
892 uint8_t Q:2;
893 uint8_t EC:3;
894 uint8_t ITC:1;
895 /* Packet Byte #4 */
896 uint8_t VIC:7;
897 uint8_t rsvd2:1;
898 /* Packet Byte #5 */
899 uint8_t PR:4;
900 uint8_t rsvd3:4;
901 /* Packet Byte #6~13 */
902 uint16_t top_bar_end;
903 uint16_t bottom_bar_start;
904 uint16_t left_bar_end;
905 uint16_t right_bar_start;
906 } avi;
907 struct {
908 /* Packet Byte #1 */
909 uint8_t channel_count:3;
910 uint8_t rsvd1:1;
911 uint8_t coding_type:4;
912 /* Packet Byte #2 */
913 uint8_t sample_size:2; /* SS0, SS1 */
914 uint8_t sample_frequency:3;
915 uint8_t rsvd2:3;
916 /* Packet Byte #3 */
917 uint8_t coding_type_private:5;
918 uint8_t rsvd3:3;
919 /* Packet Byte #4 */
920 uint8_t channel_allocation;
921 /* Packet Byte #5 */
922 uint8_t rsvd4:3;
923 uint8_t level_shift:4;
924 uint8_t downmix_inhibit:1;
925 } audio;
926 uint8_t payload[28];
927 } __attribute__ ((packed)) u;
928} __attribute__((packed));
929
930static void intel_sdvo_set_avi_infoframe(struct intel_output *output,
931 struct drm_display_mode * mode)
932{
933 struct dip_infoframe avi_if = {
934 .type = DIP_TYPE_AVI,
935 .version = DIP_VERSION_AVI,
936 .len = DIP_LEN_AVI,
937 };
938
939 avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
940 4 + avi_if.len);
941 intel_sdvo_set_hdmi_buf(output, 1, (uint8_t *)&avi_if, 4 + avi_if.len,
942 SDVO_HBUF_TX_VSYNC);
943}
944
Zhenyu Wang7026d4a2009-03-24 14:02:43 +0800945static void intel_sdvo_set_tv_format(struct intel_output *output)
946{
947 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
948 struct intel_sdvo_tv_format *format, unset;
949 u8 status;
950
951 format = &sdvo_priv->tv_format;
952 memset(&unset, 0, sizeof(unset));
953 if (memcmp(format, &unset, sizeof(*format))) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800954 DRM_DEBUG_KMS("%s: Choosing default TV format of NTSC-M\n",
Zhenyu Wang7026d4a2009-03-24 14:02:43 +0800955 SDVO_NAME(sdvo_priv));
956 format->ntsc_m = 1;
957 intel_sdvo_write_cmd(output, SDVO_CMD_SET_TV_FORMAT, format,
958 sizeof(*format));
959 status = intel_sdvo_read_response(output, NULL, 0);
960 if (status != SDVO_CMD_STATUS_SUCCESS)
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800961 DRM_DEBUG_KMS("%s: Failed to set TV format\n",
Zhenyu Wang7026d4a2009-03-24 14:02:43 +0800962 SDVO_NAME(sdvo_priv));
963 }
964}
965
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800966static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
967 struct drm_display_mode *mode,
968 struct drm_display_mode *adjusted_mode)
969{
970 struct intel_output *output = enc_to_intel_output(encoder);
971 struct intel_sdvo_priv *dev_priv = output->dev_priv;
972
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800973 if (dev_priv->is_tv) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800974 struct intel_sdvo_dtd output_dtd;
975 bool success;
976
977 /* We need to construct preferred input timings based on our
978 * output timings. To do that, we have to set the output
979 * timings, even though this isn't really the right place in
980 * the sequence to do it. Oh well.
981 */
982
983
984 /* Set output timings */
985 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
986 intel_sdvo_set_target_output(output,
987 dev_priv->controlled_output);
988 intel_sdvo_set_output_timing(output, &output_dtd);
989
990 /* Set the input timing to the screen. Assume always input 0. */
991 intel_sdvo_set_target_input(output, true, false);
992
993
994 success = intel_sdvo_create_preferred_input_timing(output,
995 mode->clock / 10,
996 mode->hdisplay,
997 mode->vdisplay);
998 if (success) {
999 struct intel_sdvo_dtd input_dtd;
1000
1001 intel_sdvo_get_preferred_input_timing(output,
1002 &input_dtd);
1003 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001004 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001005
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001006 drm_mode_set_crtcinfo(adjusted_mode, 0);
1007
1008 mode->clock = adjusted_mode->clock;
1009
1010 adjusted_mode->clock *=
1011 intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001012 } else {
1013 return false;
1014 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001015 } else if (dev_priv->is_lvds) {
1016 struct intel_sdvo_dtd output_dtd;
1017 bool success;
1018
1019 drm_mode_set_crtcinfo(dev_priv->sdvo_lvds_fixed_mode, 0);
1020 /* Set output timings */
1021 intel_sdvo_get_dtd_from_mode(&output_dtd,
1022 dev_priv->sdvo_lvds_fixed_mode);
1023
1024 intel_sdvo_set_target_output(output,
1025 dev_priv->controlled_output);
1026 intel_sdvo_set_output_timing(output, &output_dtd);
1027
1028 /* Set the input timing to the screen. Assume always input 0. */
1029 intel_sdvo_set_target_input(output, true, false);
1030
1031
1032 success = intel_sdvo_create_preferred_input_timing(
1033 output,
1034 mode->clock / 10,
1035 mode->hdisplay,
1036 mode->vdisplay);
1037
1038 if (success) {
1039 struct intel_sdvo_dtd input_dtd;
1040
1041 intel_sdvo_get_preferred_input_timing(output,
1042 &input_dtd);
1043 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
1044 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
1045
1046 drm_mode_set_crtcinfo(adjusted_mode, 0);
1047
1048 mode->clock = adjusted_mode->clock;
1049
1050 adjusted_mode->clock *=
1051 intel_sdvo_get_pixel_multiplier(mode);
1052 } else {
1053 return false;
1054 }
1055
1056 } else {
1057 /* Make the CRTC code factor in the SDVO pixel multiplier. The
1058 * SDVO device will be told of the multiplier during mode_set.
1059 */
1060 adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001061 }
1062 return true;
1063}
1064
1065static void intel_sdvo_mode_set(struct drm_encoder *encoder,
1066 struct drm_display_mode *mode,
1067 struct drm_display_mode *adjusted_mode)
1068{
1069 struct drm_device *dev = encoder->dev;
1070 struct drm_i915_private *dev_priv = dev->dev_private;
1071 struct drm_crtc *crtc = encoder->crtc;
1072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1073 struct intel_output *output = enc_to_intel_output(encoder);
1074 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1075 u32 sdvox = 0;
1076 int sdvo_pixel_multiply;
1077 struct intel_sdvo_in_out_map in_out;
1078 struct intel_sdvo_dtd input_dtd;
1079 u8 status;
1080
1081 if (!mode)
1082 return;
1083
1084 /* First, set the input mapping for the first input to our controlled
1085 * output. This is only correct if we're a single-input device, in
1086 * which case the first input is the output from the appropriate SDVO
1087 * channel on the motherboard. In a two-input device, the first input
1088 * will be SDVOB and the second SDVOC.
1089 */
1090 in_out.in0 = sdvo_priv->controlled_output;
1091 in_out.in1 = 0;
1092
1093 intel_sdvo_write_cmd(output, SDVO_CMD_SET_IN_OUT_MAP,
1094 &in_out, sizeof(in_out));
1095 status = intel_sdvo_read_response(output, NULL, 0);
1096
1097 if (sdvo_priv->is_hdmi) {
1098 intel_sdvo_set_avi_infoframe(output, mode);
1099 sdvox |= SDVO_AUDIO_ENABLE;
1100 }
1101
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001102 /* We have tried to get input timing in mode_fixup, and filled into
1103 adjusted_mode */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001104 if (sdvo_priv->is_tv || sdvo_priv->is_lvds) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001105 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001106 input_dtd.part2.sdvo_flags = sdvo_priv->sdvo_flags;
1107 } else
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001108 intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001109
1110 /* If it's a TV, we already set the output timing in mode_fixup.
1111 * Otherwise, the output timing is equal to the input timing.
1112 */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001113 if (!sdvo_priv->is_tv && !sdvo_priv->is_lvds) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001114 /* Set the output timing to the screen */
1115 intel_sdvo_set_target_output(output,
1116 sdvo_priv->controlled_output);
1117 intel_sdvo_set_output_timing(output, &input_dtd);
1118 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001119
1120 /* Set the input timing to the screen. Assume always input 0. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001121 intel_sdvo_set_target_input(output, true, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08001122
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001123 if (sdvo_priv->is_tv)
1124 intel_sdvo_set_tv_format(output);
1125
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001126 /* We would like to use intel_sdvo_create_preferred_input_timing() to
Jesse Barnes79e53942008-11-07 14:24:08 -08001127 * provide the device with a timing it can support, if it supports that
1128 * feature. However, presumably we would need to adjust the CRTC to
1129 * output the preferred timing, and we don't support that currently.
1130 */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001131#if 0
1132 success = intel_sdvo_create_preferred_input_timing(output, clock,
1133 width, height);
1134 if (success) {
1135 struct intel_sdvo_dtd *input_dtd;
1136
1137 intel_sdvo_get_preferred_input_timing(output, &input_dtd);
1138 intel_sdvo_set_input_timing(output, &input_dtd);
1139 }
1140#else
1141 intel_sdvo_set_input_timing(output, &input_dtd);
1142#endif
Jesse Barnes79e53942008-11-07 14:24:08 -08001143
1144 switch (intel_sdvo_get_pixel_multiplier(mode)) {
1145 case 1:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001146 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001147 SDVO_CLOCK_RATE_MULT_1X);
1148 break;
1149 case 2:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001150 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001151 SDVO_CLOCK_RATE_MULT_2X);
1152 break;
1153 case 4:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001154 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001155 SDVO_CLOCK_RATE_MULT_4X);
1156 break;
1157 }
1158
1159 /* Set the SDVO control regs. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001160 if (IS_I965G(dev)) {
1161 sdvox |= SDVO_BORDER_ENABLE |
1162 SDVO_VSYNC_ACTIVE_HIGH |
1163 SDVO_HSYNC_ACTIVE_HIGH;
1164 } else {
1165 sdvox |= I915_READ(sdvo_priv->output_device);
1166 switch (sdvo_priv->output_device) {
1167 case SDVOB:
1168 sdvox &= SDVOB_PRESERVE_MASK;
1169 break;
1170 case SDVOC:
1171 sdvox &= SDVOC_PRESERVE_MASK;
1172 break;
1173 }
1174 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1175 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001176 if (intel_crtc->pipe == 1)
1177 sdvox |= SDVO_PIPE_B_SELECT;
1178
1179 sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
1180 if (IS_I965G(dev)) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001181 /* done in crtc_mode_set as the dpll_md reg must be written early */
1182 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1183 /* done in crtc_mode_set as it lives inside the dpll register */
Jesse Barnes79e53942008-11-07 14:24:08 -08001184 } else {
1185 sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1186 }
1187
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001188 if (sdvo_priv->sdvo_flags & SDVO_NEED_TO_STALL)
1189 sdvox |= SDVO_STALL_SELECT;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001190 intel_sdvo_write_sdvox(output, sdvox);
Jesse Barnes79e53942008-11-07 14:24:08 -08001191}
1192
1193static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1194{
1195 struct drm_device *dev = encoder->dev;
1196 struct drm_i915_private *dev_priv = dev->dev_private;
1197 struct intel_output *intel_output = enc_to_intel_output(encoder);
1198 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1199 u32 temp;
1200
1201 if (mode != DRM_MODE_DPMS_ON) {
1202 intel_sdvo_set_active_outputs(intel_output, 0);
1203 if (0)
1204 intel_sdvo_set_encoder_power_state(intel_output, mode);
1205
1206 if (mode == DRM_MODE_DPMS_OFF) {
1207 temp = I915_READ(sdvo_priv->output_device);
1208 if ((temp & SDVO_ENABLE) != 0) {
1209 intel_sdvo_write_sdvox(intel_output, temp & ~SDVO_ENABLE);
1210 }
1211 }
1212 } else {
1213 bool input1, input2;
1214 int i;
1215 u8 status;
1216
1217 temp = I915_READ(sdvo_priv->output_device);
1218 if ((temp & SDVO_ENABLE) == 0)
1219 intel_sdvo_write_sdvox(intel_output, temp | SDVO_ENABLE);
1220 for (i = 0; i < 2; i++)
1221 intel_wait_for_vblank(dev);
1222
1223 status = intel_sdvo_get_trained_inputs(intel_output, &input1,
1224 &input2);
1225
1226
1227 /* Warn if the device reported failure to sync.
1228 * A lot of SDVO devices fail to notify of sync, but it's
1229 * a given it the status is a success, we succeeded.
1230 */
1231 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001232 DRM_DEBUG_KMS("First %s output reported failure to "
1233 "sync\n", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -08001234 }
1235
1236 if (0)
1237 intel_sdvo_set_encoder_power_state(intel_output, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001238 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->controlled_output);
Jesse Barnes79e53942008-11-07 14:24:08 -08001239 }
1240 return;
1241}
1242
1243static void intel_sdvo_save(struct drm_connector *connector)
1244{
1245 struct drm_device *dev = connector->dev;
1246 struct drm_i915_private *dev_priv = dev->dev_private;
1247 struct intel_output *intel_output = to_intel_output(connector);
1248 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1249 int o;
1250
1251 sdvo_priv->save_sdvo_mult = intel_sdvo_get_clock_rate_mult(intel_output);
1252 intel_sdvo_get_active_outputs(intel_output, &sdvo_priv->save_active_outputs);
1253
1254 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1255 intel_sdvo_set_target_input(intel_output, true, false);
1256 intel_sdvo_get_input_timing(intel_output,
1257 &sdvo_priv->save_input_dtd_1);
1258 }
1259
1260 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1261 intel_sdvo_set_target_input(intel_output, false, true);
1262 intel_sdvo_get_input_timing(intel_output,
1263 &sdvo_priv->save_input_dtd_2);
1264 }
1265
1266 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1267 {
1268 u16 this_output = (1 << o);
1269 if (sdvo_priv->caps.output_flags & this_output)
1270 {
1271 intel_sdvo_set_target_output(intel_output, this_output);
1272 intel_sdvo_get_output_timing(intel_output,
1273 &sdvo_priv->save_output_dtd[o]);
1274 }
1275 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001276 if (sdvo_priv->is_tv) {
1277 /* XXX: Save TV format/enhancements. */
1278 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001279
1280 sdvo_priv->save_SDVOX = I915_READ(sdvo_priv->output_device);
1281}
1282
1283static void intel_sdvo_restore(struct drm_connector *connector)
1284{
1285 struct drm_device *dev = connector->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001286 struct intel_output *intel_output = to_intel_output(connector);
1287 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1288 int o;
1289 int i;
1290 bool input1, input2;
1291 u8 status;
1292
1293 intel_sdvo_set_active_outputs(intel_output, 0);
1294
1295 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1296 {
1297 u16 this_output = (1 << o);
1298 if (sdvo_priv->caps.output_flags & this_output) {
1299 intel_sdvo_set_target_output(intel_output, this_output);
1300 intel_sdvo_set_output_timing(intel_output, &sdvo_priv->save_output_dtd[o]);
1301 }
1302 }
1303
1304 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1305 intel_sdvo_set_target_input(intel_output, true, false);
1306 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_1);
1307 }
1308
1309 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1310 intel_sdvo_set_target_input(intel_output, false, true);
1311 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_2);
1312 }
1313
1314 intel_sdvo_set_clock_rate_mult(intel_output, sdvo_priv->save_sdvo_mult);
1315
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001316 if (sdvo_priv->is_tv) {
1317 /* XXX: Restore TV format/enhancements. */
1318 }
1319
1320 intel_sdvo_write_sdvox(intel_output, sdvo_priv->save_SDVOX);
Jesse Barnes79e53942008-11-07 14:24:08 -08001321
1322 if (sdvo_priv->save_SDVOX & SDVO_ENABLE)
1323 {
1324 for (i = 0; i < 2; i++)
1325 intel_wait_for_vblank(dev);
1326 status = intel_sdvo_get_trained_inputs(intel_output, &input1, &input2);
1327 if (status == SDVO_CMD_STATUS_SUCCESS && !input1)
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001328 DRM_DEBUG_KMS("First %s output reported failure to "
1329 "sync\n", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -08001330 }
1331
1332 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->save_active_outputs);
1333}
1334
1335static int intel_sdvo_mode_valid(struct drm_connector *connector,
1336 struct drm_display_mode *mode)
1337{
1338 struct intel_output *intel_output = to_intel_output(connector);
1339 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1340
1341 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1342 return MODE_NO_DBLESCAN;
1343
1344 if (sdvo_priv->pixel_clock_min > mode->clock)
1345 return MODE_CLOCK_LOW;
1346
1347 if (sdvo_priv->pixel_clock_max < mode->clock)
1348 return MODE_CLOCK_HIGH;
1349
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001350 if (sdvo_priv->is_lvds == true) {
1351 if (sdvo_priv->sdvo_lvds_fixed_mode == NULL)
1352 return MODE_PANEL;
1353
1354 if (mode->hdisplay > sdvo_priv->sdvo_lvds_fixed_mode->hdisplay)
1355 return MODE_PANEL;
1356
1357 if (mode->vdisplay > sdvo_priv->sdvo_lvds_fixed_mode->vdisplay)
1358 return MODE_PANEL;
1359 }
1360
Jesse Barnes79e53942008-11-07 14:24:08 -08001361 return MODE_OK;
1362}
1363
1364static bool intel_sdvo_get_capabilities(struct intel_output *intel_output, struct intel_sdvo_caps *caps)
1365{
1366 u8 status;
1367
1368 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
1369 status = intel_sdvo_read_response(intel_output, caps, sizeof(*caps));
1370 if (status != SDVO_CMD_STATUS_SUCCESS)
1371 return false;
1372
1373 return true;
1374}
1375
1376struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
1377{
1378 struct drm_connector *connector = NULL;
1379 struct intel_output *iout = NULL;
1380 struct intel_sdvo_priv *sdvo;
1381
1382 /* find the sdvo connector */
1383 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1384 iout = to_intel_output(connector);
1385
1386 if (iout->type != INTEL_OUTPUT_SDVO)
1387 continue;
1388
1389 sdvo = iout->dev_priv;
1390
1391 if (sdvo->output_device == SDVOB && sdvoB)
1392 return connector;
1393
1394 if (sdvo->output_device == SDVOC && !sdvoB)
1395 return connector;
1396
1397 }
1398
1399 return NULL;
1400}
1401
1402int intel_sdvo_supports_hotplug(struct drm_connector *connector)
1403{
1404 u8 response[2];
1405 u8 status;
1406 struct intel_output *intel_output;
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001407 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08001408
1409 if (!connector)
1410 return 0;
1411
1412 intel_output = to_intel_output(connector);
1413
1414 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1415 status = intel_sdvo_read_response(intel_output, &response, 2);
1416
1417 if (response[0] !=0)
1418 return 1;
1419
1420 return 0;
1421}
1422
1423void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1424{
1425 u8 response[2];
1426 u8 status;
1427 struct intel_output *intel_output = to_intel_output(connector);
1428
1429 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1430 intel_sdvo_read_response(intel_output, &response, 2);
1431
1432 if (on) {
1433 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1434 status = intel_sdvo_read_response(intel_output, &response, 2);
1435
1436 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1437 } else {
1438 response[0] = 0;
1439 response[1] = 0;
1440 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1441 }
1442
1443 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1444 intel_sdvo_read_response(intel_output, &response, 2);
1445}
1446
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001447static bool
1448intel_sdvo_multifunc_encoder(struct intel_output *intel_output)
1449{
1450 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1451 int caps = 0;
1452
1453 if (sdvo_priv->caps.output_flags &
1454 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1455 caps++;
1456 if (sdvo_priv->caps.output_flags &
1457 (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1))
1458 caps++;
1459 if (sdvo_priv->caps.output_flags &
1460 (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_SVID0))
1461 caps++;
1462 if (sdvo_priv->caps.output_flags &
1463 (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_CVBS1))
1464 caps++;
1465 if (sdvo_priv->caps.output_flags &
1466 (SDVO_OUTPUT_YPRPB0 | SDVO_OUTPUT_YPRPB1))
1467 caps++;
1468
1469 if (sdvo_priv->caps.output_flags &
1470 (SDVO_OUTPUT_SCART0 | SDVO_OUTPUT_SCART1))
1471 caps++;
1472
1473 if (sdvo_priv->caps.output_flags &
1474 (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1))
1475 caps++;
1476
1477 return (caps > 1);
1478}
1479
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001480enum drm_connector_status
1481intel_sdvo_hdmi_sink_detect(struct drm_connector *connector, u16 response)
Ma Ling9dff6af2009-04-02 13:13:26 +08001482{
1483 struct intel_output *intel_output = to_intel_output(connector);
1484 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001485 enum drm_connector_status status = connector_status_connected;
Ma Ling9dff6af2009-04-02 13:13:26 +08001486 struct edid *edid = NULL;
1487
Ma Ling9dff6af2009-04-02 13:13:26 +08001488 edid = drm_get_edid(&intel_output->base,
Keith Packardf9c10a92009-05-30 12:16:25 -07001489 intel_output->ddc_bus);
Ma Ling9dff6af2009-04-02 13:13:26 +08001490 if (edid != NULL) {
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001491 /* Don't report the output as connected if it's a DVI-I
1492 * connector with a non-digital EDID coming out.
1493 */
1494 if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1495 if (edid->input & DRM_EDID_INPUT_DIGITAL)
1496 sdvo_priv->is_hdmi =
1497 drm_detect_hdmi_monitor(edid);
1498 else
1499 status = connector_status_disconnected;
1500 }
1501
Ma Ling9dff6af2009-04-02 13:13:26 +08001502 kfree(edid);
1503 intel_output->base.display_info.raw_edid = NULL;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001504
1505 } else if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1506 status = connector_status_disconnected;
1507
1508 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +08001509}
1510
Jesse Barnes79e53942008-11-07 14:24:08 -08001511static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
1512{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001513 uint16_t response;
Jesse Barnes79e53942008-11-07 14:24:08 -08001514 u8 status;
1515 struct intel_output *intel_output = to_intel_output(connector);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001516 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001517
1518 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
1519 status = intel_sdvo_read_response(intel_output, &response, 2);
1520
Dave Airlie51c8b402009-08-20 13:38:04 +10001521 DRM_DEBUG_KMS("SDVO response %d %d\n", response & 0xff, response >> 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001522
1523 if (status != SDVO_CMD_STATUS_SUCCESS)
1524 return connector_status_unknown;
1525
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001526 if (response == 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001527 return connector_status_disconnected;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001528
1529 if (intel_sdvo_multifunc_encoder(intel_output) &&
1530 sdvo_priv->attached_output != response) {
1531 if (sdvo_priv->controlled_output != response &&
1532 intel_sdvo_output_setup(intel_output, response) != true)
1533 return connector_status_unknown;
1534 sdvo_priv->attached_output = response;
1535 }
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001536 return intel_sdvo_hdmi_sink_detect(connector, response);
Jesse Barnes79e53942008-11-07 14:24:08 -08001537}
1538
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001539static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001540{
1541 struct intel_output *intel_output = to_intel_output(connector);
1542
1543 /* set the bus switch and get the modes */
Jesse Barnes79e53942008-11-07 14:24:08 -08001544 intel_ddc_get_modes(intel_output);
1545
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001546#if 0
1547 struct drm_device *dev = encoder->dev;
1548 struct drm_i915_private *dev_priv = dev->dev_private;
1549 /* Mac mini hack. On this device, I get DDC through the analog, which
1550 * load-detects as disconnected. I fail to DDC through the SDVO DDC,
1551 * but it does load-detect as connected. So, just steal the DDC bits
1552 * from analog when we fail at finding it the right way.
1553 */
1554 crt = xf86_config->output[0];
1555 intel_output = crt->driver_private;
1556 if (intel_output->type == I830_OUTPUT_ANALOG &&
1557 crt->funcs->detect(crt) == XF86OutputStatusDisconnected) {
1558 I830I2CInit(pScrn, &intel_output->pDDCBus, GPIOA, "CRTDDC_A");
1559 edid_mon = xf86OutputGetEDID(crt, intel_output->pDDCBus);
1560 xf86DestroyI2CBusRec(intel_output->pDDCBus, true, true);
1561 }
1562 if (edid_mon) {
1563 xf86OutputSetEDID(output, edid_mon);
1564 modes = xf86OutputGetEDIDModes(output);
1565 }
1566#endif
1567}
1568
1569/**
1570 * This function checks the current TV format, and chooses a default if
1571 * it hasn't been set.
1572 */
1573static void
1574intel_sdvo_check_tv_format(struct intel_output *output)
1575{
1576 struct intel_sdvo_priv *dev_priv = output->dev_priv;
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001577 struct intel_sdvo_tv_format format;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001578 uint8_t status;
1579
1580 intel_sdvo_write_cmd(output, SDVO_CMD_GET_TV_FORMAT, NULL, 0);
1581 status = intel_sdvo_read_response(output, &format, sizeof(format));
1582 if (status != SDVO_CMD_STATUS_SUCCESS)
1583 return;
1584
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001585 memcpy(&dev_priv->tv_format, &format, sizeof(format));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001586}
1587
1588/*
1589 * Set of SDVO TV modes.
1590 * Note! This is in reply order (see loop in get_tv_modes).
1591 * XXX: all 60Hz refresh?
1592 */
1593struct drm_display_mode sdvo_tv_modes[] = {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001594 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1595 416, 0, 200, 201, 232, 233, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001596 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001597 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1598 416, 0, 240, 241, 272, 273, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001599 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001600 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1601 496, 0, 300, 301, 332, 333, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001602 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001603 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1604 736, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001605 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001606 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1607 736, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001608 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001609 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1610 736, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001611 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001612 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1613 800, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001614 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001615 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1616 800, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001617 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001618 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1619 816, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001620 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001621 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1622 816, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001623 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001624 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1625 816, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001626 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001627 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1628 816, 0, 540, 541, 572, 573, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001629 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001630 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1631 816, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001632 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001633 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1634 864, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001635 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001636 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1637 896, 0, 600, 601, 632, 633, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001638 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001639 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1640 928, 0, 624, 625, 656, 657, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001641 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001642 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1643 1016, 0, 766, 767, 798, 799, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001644 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001645 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1646 1120, 0, 768, 769, 800, 801, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001647 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001648 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1649 1376, 0, 1024, 1025, 1056, 1057, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001650 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1651};
1652
1653static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1654{
1655 struct intel_output *output = to_intel_output(connector);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001656 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1657 struct intel_sdvo_sdtv_resolution_request tv_res;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001658 uint32_t reply = 0;
1659 uint8_t status;
1660 int i = 0;
1661
1662 intel_sdvo_check_tv_format(output);
1663
1664 /* Read the list of supported input resolutions for the selected TV
1665 * format.
1666 */
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001667 memset(&tv_res, 0, sizeof(tv_res));
1668 memcpy(&tv_res, &sdvo_priv->tv_format, sizeof(tv_res));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001669 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001670 &tv_res, sizeof(tv_res));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001671 status = intel_sdvo_read_response(output, &reply, 3);
1672 if (status != SDVO_CMD_STATUS_SUCCESS)
1673 return;
1674
1675 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001676 if (reply & (1 << i)) {
1677 struct drm_display_mode *nmode;
1678 nmode = drm_mode_duplicate(connector->dev,
1679 &sdvo_tv_modes[i]);
1680 if (nmode)
1681 drm_mode_probed_add(connector, nmode);
1682 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001683}
1684
Ma Ling7086c872009-05-13 11:20:06 +08001685static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1686{
1687 struct intel_output *intel_output = to_intel_output(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001688 struct drm_i915_private *dev_priv = connector->dev->dev_private;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001689 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1690 struct drm_display_mode *newmode;
Ma Ling7086c872009-05-13 11:20:06 +08001691
1692 /*
1693 * Attempt to get the mode list from DDC.
1694 * Assume that the preferred modes are
1695 * arranged in priority order.
1696 */
Ma Ling7086c872009-05-13 11:20:06 +08001697 intel_ddc_get_modes(intel_output);
1698 if (list_empty(&connector->probed_modes) == false)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001699 goto end;
Ma Ling7086c872009-05-13 11:20:06 +08001700
1701 /* Fetch modes from VBT */
1702 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
Ma Ling7086c872009-05-13 11:20:06 +08001703 newmode = drm_mode_duplicate(connector->dev,
1704 dev_priv->sdvo_lvds_vbt_mode);
1705 if (newmode != NULL) {
1706 /* Guarantee the mode is preferred */
1707 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1708 DRM_MODE_TYPE_DRIVER);
1709 drm_mode_probed_add(connector, newmode);
1710 }
1711 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001712
1713end:
1714 list_for_each_entry(newmode, &connector->probed_modes, head) {
1715 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1716 sdvo_priv->sdvo_lvds_fixed_mode =
1717 drm_mode_duplicate(connector->dev, newmode);
1718 break;
1719 }
1720 }
1721
Ma Ling7086c872009-05-13 11:20:06 +08001722}
1723
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001724static int intel_sdvo_get_modes(struct drm_connector *connector)
1725{
1726 struct intel_output *output = to_intel_output(connector);
1727 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1728
1729 if (sdvo_priv->is_tv)
1730 intel_sdvo_get_tv_modes(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001731 else if (sdvo_priv->is_lvds == true)
1732 intel_sdvo_get_lvds_modes(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001733 else
1734 intel_sdvo_get_ddc_modes(connector);
1735
Jesse Barnes79e53942008-11-07 14:24:08 -08001736 if (list_empty(&connector->probed_modes))
1737 return 0;
1738 return 1;
1739}
1740
1741static void intel_sdvo_destroy(struct drm_connector *connector)
1742{
1743 struct intel_output *intel_output = to_intel_output(connector);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001744 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001745
1746 if (intel_output->i2c_bus)
1747 intel_i2c_destroy(intel_output->i2c_bus);
Ma Ling619ac3b2009-05-18 16:12:46 +08001748 if (intel_output->ddc_bus)
1749 intel_i2c_destroy(intel_output->ddc_bus);
1750
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001751 if (sdvo_priv->sdvo_lvds_fixed_mode != NULL)
1752 drm_mode_destroy(connector->dev,
1753 sdvo_priv->sdvo_lvds_fixed_mode);
1754
Jesse Barnes79e53942008-11-07 14:24:08 -08001755 drm_sysfs_connector_remove(connector);
1756 drm_connector_cleanup(connector);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001757
Jesse Barnes79e53942008-11-07 14:24:08 -08001758 kfree(intel_output);
1759}
1760
1761static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
1762 .dpms = intel_sdvo_dpms,
1763 .mode_fixup = intel_sdvo_mode_fixup,
1764 .prepare = intel_encoder_prepare,
1765 .mode_set = intel_sdvo_mode_set,
1766 .commit = intel_encoder_commit,
1767};
1768
1769static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -07001770 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -08001771 .save = intel_sdvo_save,
1772 .restore = intel_sdvo_restore,
1773 .detect = intel_sdvo_detect,
1774 .fill_modes = drm_helper_probe_single_connector_modes,
1775 .destroy = intel_sdvo_destroy,
1776};
1777
1778static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
1779 .get_modes = intel_sdvo_get_modes,
1780 .mode_valid = intel_sdvo_mode_valid,
1781 .best_encoder = intel_best_encoder,
1782};
1783
Hannes Ederb358d0a2008-12-18 21:18:47 +01001784static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08001785{
1786 drm_encoder_cleanup(encoder);
1787}
1788
1789static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
1790 .destroy = intel_sdvo_enc_destroy,
1791};
1792
1793
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001794/**
1795 * Choose the appropriate DDC bus for control bus switch command for this
1796 * SDVO output based on the controlled output.
1797 *
1798 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1799 * outputs, then LVDS outputs.
1800 */
1801static void
1802intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
1803{
1804 uint16_t mask = 0;
1805 unsigned int num_bits;
1806
1807 /* Make a mask of outputs less than or equal to our own priority in the
1808 * list.
1809 */
1810 switch (dev_priv->controlled_output) {
1811 case SDVO_OUTPUT_LVDS1:
1812 mask |= SDVO_OUTPUT_LVDS1;
1813 case SDVO_OUTPUT_LVDS0:
1814 mask |= SDVO_OUTPUT_LVDS0;
1815 case SDVO_OUTPUT_TMDS1:
1816 mask |= SDVO_OUTPUT_TMDS1;
1817 case SDVO_OUTPUT_TMDS0:
1818 mask |= SDVO_OUTPUT_TMDS0;
1819 case SDVO_OUTPUT_RGB1:
1820 mask |= SDVO_OUTPUT_RGB1;
1821 case SDVO_OUTPUT_RGB0:
1822 mask |= SDVO_OUTPUT_RGB0;
1823 break;
1824 }
1825
1826 /* Count bits to find what number we are in the priority list. */
1827 mask &= dev_priv->caps.output_flags;
1828 num_bits = hweight16(mask);
1829 if (num_bits > 3) {
1830 /* if more than 3 outputs, default to DDC bus 3 for now */
1831 num_bits = 3;
1832 }
1833
1834 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1835 dev_priv->ddc_bus = 1 << num_bits;
1836}
1837
1838static bool
1839intel_sdvo_get_digital_encoding_mode(struct intel_output *output)
1840{
1841 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1842 uint8_t status;
1843
1844 intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
1845
1846 intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
1847 status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
1848 if (status != SDVO_CMD_STATUS_SUCCESS)
1849 return false;
1850 return true;
1851}
1852
Ma Ling619ac3b2009-05-18 16:12:46 +08001853static struct intel_output *
1854intel_sdvo_chan_to_intel_output(struct intel_i2c_chan *chan)
1855{
1856 struct drm_device *dev = chan->drm_dev;
1857 struct drm_connector *connector;
1858 struct intel_output *intel_output = NULL;
1859
1860 list_for_each_entry(connector,
1861 &dev->mode_config.connector_list, head) {
Keith Packardf9c10a92009-05-30 12:16:25 -07001862 if (to_intel_output(connector)->ddc_bus == &chan->adapter) {
Ma Ling619ac3b2009-05-18 16:12:46 +08001863 intel_output = to_intel_output(connector);
1864 break;
1865 }
1866 }
1867 return intel_output;
1868}
1869
1870static int intel_sdvo_master_xfer(struct i2c_adapter *i2c_adap,
1871 struct i2c_msg msgs[], int num)
1872{
1873 struct intel_output *intel_output;
1874 struct intel_sdvo_priv *sdvo_priv;
1875 struct i2c_algo_bit_data *algo_data;
Keith Packardf9c10a92009-05-30 12:16:25 -07001876 const struct i2c_algorithm *algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08001877
1878 algo_data = (struct i2c_algo_bit_data *)i2c_adap->algo_data;
1879 intel_output =
1880 intel_sdvo_chan_to_intel_output(
1881 (struct intel_i2c_chan *)(algo_data->data));
1882 if (intel_output == NULL)
1883 return -EINVAL;
1884
1885 sdvo_priv = intel_output->dev_priv;
Keith Packardf9c10a92009-05-30 12:16:25 -07001886 algo = intel_output->i2c_bus->algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08001887
1888 intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
1889 return algo->master_xfer(i2c_adap, msgs, num);
1890}
1891
1892static struct i2c_algorithm intel_sdvo_i2c_bit_algo = {
1893 .master_xfer = intel_sdvo_master_xfer,
1894};
1895
yakui_zhao714605e2009-05-31 17:18:07 +08001896static u8
1897intel_sdvo_get_slave_addr(struct drm_device *dev, int output_device)
1898{
1899 struct drm_i915_private *dev_priv = dev->dev_private;
1900 struct sdvo_device_mapping *my_mapping, *other_mapping;
1901
1902 if (output_device == SDVOB) {
1903 my_mapping = &dev_priv->sdvo_mappings[0];
1904 other_mapping = &dev_priv->sdvo_mappings[1];
1905 } else {
1906 my_mapping = &dev_priv->sdvo_mappings[1];
1907 other_mapping = &dev_priv->sdvo_mappings[0];
1908 }
1909
1910 /* If the BIOS described our SDVO device, take advantage of it. */
1911 if (my_mapping->slave_addr)
1912 return my_mapping->slave_addr;
1913
1914 /* If the BIOS only described a different SDVO device, use the
1915 * address that it isn't using.
1916 */
1917 if (other_mapping->slave_addr) {
1918 if (other_mapping->slave_addr == 0x70)
1919 return 0x72;
1920 else
1921 return 0x70;
1922 }
1923
1924 /* No SDVO device info is found for another DVO port,
1925 * so use mapping assumption we had before BIOS parsing.
1926 */
1927 if (output_device == SDVOB)
1928 return 0x70;
1929 else
1930 return 0x72;
1931}
1932
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001933static bool
1934intel_sdvo_output_setup(struct intel_output *intel_output, uint16_t flags)
1935{
1936 struct drm_connector *connector = &intel_output->base;
1937 struct drm_encoder *encoder = &intel_output->enc;
1938 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1939 bool ret = true, registered = false;
1940
1941 sdvo_priv->is_tv = false;
1942 intel_output->needs_tv_clock = false;
1943 sdvo_priv->is_lvds = false;
1944
1945 if (device_is_registered(&connector->kdev)) {
1946 drm_sysfs_connector_remove(connector);
1947 registered = true;
1948 }
1949
1950 if (flags &
1951 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1952 if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_TMDS0)
1953 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS0;
1954 else
1955 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS1;
1956
1957 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
1958 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
1959
1960 if (intel_sdvo_get_supp_encode(intel_output,
1961 &sdvo_priv->encode) &&
1962 intel_sdvo_get_digital_encoding_mode(intel_output) &&
1963 sdvo_priv->is_hdmi) {
1964 /* enable hdmi encoding mode if supported */
1965 intel_sdvo_set_encode(intel_output, SDVO_ENCODE_HDMI);
1966 intel_sdvo_set_colorimetry(intel_output,
1967 SDVO_COLORIMETRY_RGB256);
1968 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
1969 }
1970 } else if (flags & SDVO_OUTPUT_SVID0) {
1971
1972 sdvo_priv->controlled_output = SDVO_OUTPUT_SVID0;
1973 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
1974 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
1975 sdvo_priv->is_tv = true;
1976 intel_output->needs_tv_clock = true;
1977 } else if (flags & SDVO_OUTPUT_RGB0) {
1978
1979 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB0;
1980 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
1981 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
1982 } else if (flags & SDVO_OUTPUT_RGB1) {
1983
1984 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB1;
1985 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
1986 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
1987 } else if (flags & SDVO_OUTPUT_LVDS0) {
1988
1989 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS0;
1990 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
1991 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
1992 sdvo_priv->is_lvds = true;
1993 } else if (flags & SDVO_OUTPUT_LVDS1) {
1994
1995 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS1;
1996 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
1997 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
1998 sdvo_priv->is_lvds = true;
1999 } else {
2000
2001 unsigned char bytes[2];
2002
2003 sdvo_priv->controlled_output = 0;
2004 memcpy(bytes, &sdvo_priv->caps.output_flags, 2);
Dave Airlie51c8b402009-08-20 13:38:04 +10002005 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
2006 SDVO_NAME(sdvo_priv),
2007 bytes[0], bytes[1]);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002008 ret = false;
2009 }
2010
2011 if (ret && registered)
2012 ret = drm_sysfs_connector_add(connector) == 0 ? true : false;
2013
2014
2015 return ret;
2016
2017}
2018
Eric Anholt7d573822009-01-02 13:33:00 -08002019bool intel_sdvo_init(struct drm_device *dev, int output_device)
Jesse Barnes79e53942008-11-07 14:24:08 -08002020{
2021 struct drm_connector *connector;
2022 struct intel_output *intel_output;
2023 struct intel_sdvo_priv *sdvo_priv;
Keith Packardf9c10a92009-05-30 12:16:25 -07002024
Jesse Barnes79e53942008-11-07 14:24:08 -08002025 u8 ch[0x40];
2026 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08002027
2028 intel_output = kcalloc(sizeof(struct intel_output)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
2029 if (!intel_output) {
Eric Anholt7d573822009-01-02 13:33:00 -08002030 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002031 }
2032
Jesse Barnes79e53942008-11-07 14:24:08 -08002033 sdvo_priv = (struct intel_sdvo_priv *)(intel_output + 1);
Keith Packard308cd3a2009-06-14 11:56:18 -07002034 sdvo_priv->output_device = output_device;
2035
2036 intel_output->dev_priv = sdvo_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08002037 intel_output->type = INTEL_OUTPUT_SDVO;
2038
Jesse Barnes79e53942008-11-07 14:24:08 -08002039 /* setup the DDC bus. */
Keith Packard308cd3a2009-06-14 11:56:18 -07002040 if (output_device == SDVOB)
2041 intel_output->i2c_bus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOB");
2042 else
2043 intel_output->i2c_bus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOC");
2044
2045 if (!intel_output->i2c_bus)
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002046 goto err_inteloutput;
Jesse Barnes79e53942008-11-07 14:24:08 -08002047
Keith Packard308cd3a2009-06-14 11:56:18 -07002048 sdvo_priv->slave_addr = intel_sdvo_get_slave_addr(dev, output_device);
Jesse Barnes79e53942008-11-07 14:24:08 -08002049
Keith Packard308cd3a2009-06-14 11:56:18 -07002050 /* Save the bit-banging i2c functionality for use by the DDC wrapper */
2051 intel_sdvo_i2c_bit_algo.functionality = intel_output->i2c_bus->algo->functionality;
Jesse Barnes79e53942008-11-07 14:24:08 -08002052
Jesse Barnes79e53942008-11-07 14:24:08 -08002053 /* Read the regs to test if we can talk to the device */
2054 for (i = 0; i < 0x40; i++) {
2055 if (!intel_sdvo_read_byte(intel_output, i, &ch[i])) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002056 DRM_DEBUG_KMS("No SDVO device found on SDVO%c\n",
yakui_zhao342dc382009-06-02 14:12:00 +08002057 output_device == SDVOB ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002058 goto err_i2c;
2059 }
2060 }
2061
Ma Ling619ac3b2009-05-18 16:12:46 +08002062 /* setup the DDC bus. */
2063 if (output_device == SDVOB)
Keith Packard308cd3a2009-06-14 11:56:18 -07002064 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "SDVOB DDC BUS");
Ma Ling619ac3b2009-05-18 16:12:46 +08002065 else
Keith Packard308cd3a2009-06-14 11:56:18 -07002066 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "SDVOC DDC BUS");
Ma Ling619ac3b2009-05-18 16:12:46 +08002067
Keith Packard308cd3a2009-06-14 11:56:18 -07002068 if (intel_output->ddc_bus == NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002069 goto err_i2c;
2070
Keith Packard308cd3a2009-06-14 11:56:18 -07002071 /* Wrap with our custom algo which switches to DDC mode */
2072 intel_output->ddc_bus->algo = &intel_sdvo_i2c_bit_algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002073
Ma Ling7086c872009-05-13 11:20:06 +08002074 /* In defaut case sdvo lvds is false */
Jesse Barnes79e53942008-11-07 14:24:08 -08002075 intel_sdvo_get_capabilities(intel_output, &sdvo_priv->caps);
2076
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002077 if (intel_sdvo_output_setup(intel_output,
2078 sdvo_priv->caps.output_flags) != true) {
Dave Airlie51c8b402009-08-20 13:38:04 +10002079 DRM_DEBUG_KMS("SDVO output failed to setup on SDVO%c\n",
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002080 output_device == SDVOB ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002081 goto err_i2c;
2082 }
2083
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002084
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002085 connector = &intel_output->base;
2086 drm_connector_init(dev, connector, &intel_sdvo_connector_funcs,
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002087 connector->connector_type);
2088
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002089 drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
2090 connector->interlace_allowed = 0;
2091 connector->doublescan_allowed = 0;
2092 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
2093
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002094 drm_encoder_init(dev, &intel_output->enc,
2095 &intel_sdvo_enc_funcs, intel_output->enc.encoder_type);
2096
Jesse Barnes79e53942008-11-07 14:24:08 -08002097 drm_encoder_helper_add(&intel_output->enc, &intel_sdvo_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08002098
2099 drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
2100 drm_sysfs_connector_add(connector);
2101
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002102 intel_sdvo_select_ddc_bus(sdvo_priv);
2103
Jesse Barnes79e53942008-11-07 14:24:08 -08002104 /* Set the input timing to the screen. Assume always input 0. */
2105 intel_sdvo_set_target_input(intel_output, true, false);
2106
2107 intel_sdvo_get_input_pixel_clock_range(intel_output,
2108 &sdvo_priv->pixel_clock_min,
2109 &sdvo_priv->pixel_clock_max);
2110
2111
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002112 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
yakui_zhao342dc382009-06-02 14:12:00 +08002113 "clock range %dMHz - %dMHz, "
2114 "input 1: %c, input 2: %c, "
2115 "output 1: %c, output 2: %c\n",
2116 SDVO_NAME(sdvo_priv),
2117 sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
2118 sdvo_priv->caps.device_rev_id,
2119 sdvo_priv->pixel_clock_min / 1000,
2120 sdvo_priv->pixel_clock_max / 1000,
2121 (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2122 (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
2123 /* check currently supported outputs */
2124 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002125 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
yakui_zhao342dc382009-06-02 14:12:00 +08002126 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002127 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
2128
Eric Anholt7d573822009-01-02 13:33:00 -08002129 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08002130
2131err_i2c:
Keith Packard308cd3a2009-06-14 11:56:18 -07002132 if (intel_output->ddc_bus != NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002133 intel_i2c_destroy(intel_output->ddc_bus);
Keith Packard308cd3a2009-06-14 11:56:18 -07002134 if (intel_output->i2c_bus != NULL)
2135 intel_i2c_destroy(intel_output->i2c_bus);
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002136err_inteloutput:
Jesse Barnes79e53942008-11-07 14:24:08 -08002137 kfree(intel_output);
2138
Eric Anholt7d573822009-01-02 13:33:00 -08002139 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002140}